1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* nautilus-horizontal-splitter.c - A horizontal splitter with a semi gradient look
Copyright (C) 1999, 2000 Eazel, Inc.
The Gnome Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The Gnome Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with the Gnome Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Authors: Ramiro Estrugo <ramiro@eazel.com>
*/
#include <config.h>
#include "nautilus-horizontal-splitter.h"
#include "nautilus-gtk-macros.h"
#include <stdlib.h>
struct NautilusHorizontalSplitterDetails {
gboolean down;
double down_position;
guint32 down_time;
int saved_size;
};
/* Bar width currently hardcoded to 7 */
#define BAR_WIDTH 7
#define CLOSED_THRESHOLD 4
#define NOMINAL_SIZE 148
#define SPLITTER_CLICK_SLOP 1
#define SPLITTER_CLICK_TIMEOUT 400
/* NautilusHorizontalSplitterClass methods */
static void nautilus_horizontal_splitter_initialize_class (NautilusHorizontalSplitterClass *horizontal_splitter_class);
static void nautilus_horizontal_splitter_initialize (NautilusHorizontalSplitter *horizontal_splitter);
static gboolean nautilus_horizontal_splitter_button_press (GtkWidget *widget,
GdkEventButton *event);
static gboolean nautilus_horizontal_splitter_button_release (GtkWidget *widget,
GdkEventButton *event);
static gboolean nautilus_horizontal_splitter_motion (GtkWidget *widget,
GdkEventMotion *event);
/* GtkObjectClass methods */
static void nautilus_horizontal_splitter_destroy (GtkObject *object);
/* GtkWidgetClass methods */
static void nautilus_horizontal_splitter_draw (GtkWidget *widget,
GdkRectangle *area);
NAUTILUS_DEFINE_CLASS_BOILERPLATE (NautilusHorizontalSplitter,
nautilus_horizontal_splitter,
E_TYPE_HPANED)
/* GtkObjectClass methods */
static void
nautilus_horizontal_splitter_initialize_class (NautilusHorizontalSplitterClass *horizontal_splitter_class)
{
GtkObjectClass *object_class;
GtkWidgetClass *widget_class;
object_class = GTK_OBJECT_CLASS (horizontal_splitter_class);
widget_class = GTK_WIDGET_CLASS (horizontal_splitter_class);
/* GtkObjectClass */
object_class->destroy = nautilus_horizontal_splitter_destroy;
/* GtkWidgetClass */
widget_class->draw = nautilus_horizontal_splitter_draw;
widget_class->button_press_event = nautilus_horizontal_splitter_button_press;
widget_class->button_release_event = nautilus_horizontal_splitter_button_release;
widget_class->motion_notify_event = nautilus_horizontal_splitter_motion;
}
static void
nautilus_horizontal_splitter_initialize (NautilusHorizontalSplitter *horizontal_splitter)
{
horizontal_splitter->details = g_new0 (NautilusHorizontalSplitterDetails, 1);
e_paned_set_handle_size (E_PANED (horizontal_splitter), BAR_WIDTH);
}
/* GtkObjectClass methods */
static void
nautilus_horizontal_splitter_destroy (GtkObject *object)
{
NautilusHorizontalSplitter *horizontal_splitter;
horizontal_splitter = NAUTILUS_HORIZONTAL_SPLITTER (object);
g_free (horizontal_splitter->details);
/* Chain */
NAUTILUS_CALL_PARENT (GTK_OBJECT_CLASS, destroy, (object));
}
static void
draw_resize_bar (GtkWidget *widget,
GdkWindow *window,
const GdkRectangle *area)
{
GtkStyle *style;
GdkGC *gcs[BAR_WIDTH];
guint i;
g_assert (widget != NULL);
g_assert (window != NULL);
g_assert (area != NULL);
g_assert (area->width == BAR_WIDTH);
style = widget->style;
g_assert (style != NULL);
gcs[0] = style->fg_gc[GTK_STATE_NORMAL];
gcs[1] = style->fg_gc[GTK_STATE_SELECTED];
gcs[2] = style->light_gc[GTK_STATE_ACTIVE];
gcs[3] = style->bg_gc[GTK_STATE_NORMAL];
gcs[4] = style->mid_gc[GTK_STATE_ACTIVE];
gcs[5] = style->dark_gc[GTK_STATE_NORMAL];
gcs[6] = style->fg_gc[GTK_STATE_NORMAL];
for (i = 0; i < BAR_WIDTH; i++)
{
gdk_draw_line (window,
gcs[i],
area->x + i,
area->y,
area->x + i,
area->y + area->height - 1);
}
}
/* Number of ridges in the thumb currently hardcoded to 8 */
#define NUM_RIDGES 8
/* These control the layout of the ridges */
#define RIDGE_HEIGHT 2
#define RIDGE_EDGE_OFFSET 2
#define BETWEEN_RIDGE_OFFSET 1
static void
draw_resize_bar_thumb (GtkWidget *widget,
GdkWindow *window,
const GdkRectangle *area)
{
EPaned *paned;
GtkStyle *style;
GdkGC *light_gc;
GdkGC *dark_gc;
guint total_thumb_height;
g_assert (widget != NULL);
g_assert (window != NULL);
g_assert (area != NULL);
g_assert (area->width == BAR_WIDTH);
style = widget->style;
g_assert (style != NULL);
paned = E_PANED (widget);
light_gc = style->light_gc[GTK_STATE_ACTIVE];
dark_gc = style->dark_gc[GTK_STATE_NORMAL];
total_thumb_height = (NUM_RIDGES * RIDGE_HEIGHT) + ((NUM_RIDGES - 1) * BETWEEN_RIDGE_OFFSET);
/* Make sure the thumb aint bigger than the handle */
if (total_thumb_height > paned->handle_height)
{
total_thumb_height = paned->handle_height;
}
/* Draw the thumb only if we have enough space for at least one ridge */
if (total_thumb_height > RIDGE_HEIGHT)
{
gint y = paned->handle_ypos + (paned->handle_height - total_thumb_height) / 2;
guint i;
for (i = 0; i < NUM_RIDGES; i++)
{
gint x1 = area->x + RIDGE_EDGE_OFFSET;
gint x2 = area->x + BAR_WIDTH - RIDGE_EDGE_OFFSET;
gdk_draw_line (window,
dark_gc,
x1,
y,
x2,
y);
y += BETWEEN_RIDGE_OFFSET;
gdk_draw_line (window,
light_gc,
x1,
y,
x2,
y);
y += RIDGE_HEIGHT;
}
}
}
/* GtkWidgetClass methods */
static void
nautilus_horizontal_splitter_draw (GtkWidget *widget,
GdkRectangle *area)
{
EPaned *paned;
GdkRectangle handle_area, child_area;
guint16 border_width;
g_return_if_fail (widget != NULL);
g_return_if_fail (E_IS_PANED (widget));
if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget)) {
paned = E_PANED (widget);
border_width = GTK_CONTAINER (paned)->border_width;
gdk_window_clear_area (widget->window,
area->x, area->y, area->width,
area->height);
if (e_paned_handle_shown(paned)) {
handle_area.x = paned->handle_xpos;
handle_area.y = paned->handle_ypos;
handle_area.width = paned->handle_size;
handle_area.height = paned->handle_height;
if (gdk_rectangle_intersect (&handle_area, area, &child_area)) {
child_area.x -= paned->handle_xpos;
child_area.y -= paned->handle_ypos;
/* Simply things by always drawing the full width of the bar. */
child_area.x = 0;
child_area.width = BAR_WIDTH;
draw_resize_bar (widget, paned->handle, &child_area);
draw_resize_bar_thumb (widget, paned->handle, &child_area);
}
}
/* Redraw the children
*/
if (paned->child1 && gtk_widget_intersect (paned->child1, area, &child_area)) {
gtk_widget_draw (paned->child1, &child_area);
}
if (paned->child2 && gtk_widget_intersect (paned->child2, area, &child_area)) {
gtk_widget_draw (paned->child2, &child_area);
}
}
}
void
nautilus_horizontal_splitter_expand (NautilusHorizontalSplitter *splitter)
{
int position;
g_return_if_fail (NAUTILUS_IS_HORIZONTAL_SPLITTER (splitter));
position = e_paned_get_position (E_PANED (splitter));
if (position >= CLOSED_THRESHOLD) {
return;
}
position = splitter->details->saved_size;
if (position < CLOSED_THRESHOLD) {
position = NOMINAL_SIZE;
}
e_paned_set_position (E_PANED (splitter), position);
}
void
nautilus_horizontal_splitter_collapse (NautilusHorizontalSplitter *splitter)
{
int position;
g_return_if_fail (NAUTILUS_IS_HORIZONTAL_SPLITTER (splitter));
position = e_paned_get_position (E_PANED (splitter));
if (position < CLOSED_THRESHOLD) {
return;
}
splitter->details->saved_size = position;
e_paned_set_position (E_PANED (splitter), 0);
}
/* routine to toggle the open/closed state of the splitter */
void
nautilus_horizontal_splitter_toggle_position (NautilusHorizontalSplitter *splitter)
{
g_return_if_fail (NAUTILUS_IS_HORIZONTAL_SPLITTER (splitter));
if (e_paned_get_position (E_PANED (splitter)) >= CLOSED_THRESHOLD) {
nautilus_horizontal_splitter_collapse (splitter);
} else {
nautilus_horizontal_splitter_expand (splitter);
}
}
/* NautilusHorizontalSplitter public methods */
GtkWidget *
nautilus_horizontal_splitter_new (void)
{
return gtk_widget_new (nautilus_horizontal_splitter_get_type (), NULL);
}
/* handle mouse downs by remembering the position and the time */
static gboolean
nautilus_horizontal_splitter_button_press (GtkWidget *widget, GdkEventButton *event)
{
NautilusHorizontalSplitter *splitter;
splitter = NAUTILUS_HORIZONTAL_SPLITTER (widget);
if (event->window != E_PANED (widget)->handle) {
splitter->details->down = FALSE;
} else {
splitter->details->down = TRUE;
splitter->details->down_position = event->x;
splitter->details->down_time = event->time;
}
return NAUTILUS_CALL_PARENT_WITH_RETURN_VALUE
(GTK_WIDGET_CLASS, button_press_event, (widget, event));
}
static void
splitter_xor_line (EPaned *paned)
{
GtkWidget *widget;
GdkGCValues values;
guint16 xpos, half_width;
gint8 dash_list[2];
widget = GTK_WIDGET(paned);
if (!paned->xor_gc) {
values.function = GDK_INVERT;
values.subwindow_mode = GDK_INCLUDE_INFERIORS;
paned->xor_gc = gdk_gc_new_with_values (widget->window, &values,
GDK_GC_FUNCTION | GDK_GC_SUBWINDOW);
}
gdk_gc_set_line_attributes (paned->xor_gc, 1, GDK_LINE_ON_OFF_DASH,
GDK_CAP_NOT_LAST, GDK_JOIN_BEVEL);
/* Make line appear as every other pixel dash */
dash_list[0] = 1;
dash_list[1] = 1;
gdk_gc_set_dashes (paned->xor_gc, 1, dash_list, 2);
xpos = paned->child1_size + GTK_CONTAINER (paned)->border_width + paned->handle_size / 2;
half_width = paned->handle_size / 2;
gdk_draw_line (widget->window, paned->xor_gc, xpos - half_width, 0, xpos - half_width,
widget->allocation.height - 1);
gdk_draw_line (widget->window, paned->xor_gc, xpos + half_width, 0, xpos + half_width,
widget->allocation.height - 1);
}
/* handle mouse ups by seeing if it was a tap and toggling the open state accordingly */
static gboolean
nautilus_horizontal_splitter_button_release (GtkWidget *widget, GdkEventButton *event)
{
NautilusHorizontalSplitter *splitter;
int delta, delta_time;
EPaned *paned;
splitter = NAUTILUS_HORIZONTAL_SPLITTER (widget);
if (event->window == E_PANED (widget)->handle
&& splitter->details->down) {
delta = abs (event->x - splitter->details->down_position);
delta_time = abs (splitter->details->down_time - event->time);
if (delta < SPLITTER_CLICK_SLOP && delta_time < SPLITTER_CLICK_TIMEOUT) {
nautilus_horizontal_splitter_toggle_position (splitter);
}
}
splitter->details->down = FALSE;
paned = E_PANED (widget);
if (paned->in_drag && (event->button == 1)) {
splitter_xor_line (paned);
paned->in_drag = FALSE;
paned->position_set = TRUE;
gdk_pointer_ungrab (event->time);
gtk_widget_queue_resize (GTK_WIDGET (paned));
return TRUE;
}
return FALSE;
}
static gboolean
nautilus_horizontal_splitter_motion (GtkWidget *widget, GdkEventMotion *event)
{
EPaned *paned;
gint x;
g_return_val_if_fail (widget != NULL, FALSE);
g_return_val_if_fail (E_IS_PANED (widget), FALSE);
paned = E_PANED (widget);
if (event->is_hint || event->window != widget->window) {
gtk_widget_get_pointer(widget, &x, NULL);
} else {
x = event->x;
}
if (paned->in_drag) {
gint size = x - GTK_CONTAINER (paned)->border_width - paned->handle_size / 2;
splitter_xor_line (paned);
paned->child1_size = CLAMP (e_paned_quantized_size (paned, size), paned->min_position, paned->max_position);
splitter_xor_line (paned);
}
return TRUE;
}
|