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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
/* GTK - The GIMP Toolkit
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This 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.
*
* This 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 this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "gtkscrolledwindow.h"
#include "gtksignal.h"
#define SCROLLBAR_SPACING 5
static void gtk_scrolled_window_class_init (GtkScrolledWindowClass *klass);
static void gtk_scrolled_window_init (GtkScrolledWindow *scrolled_window);
static void gtk_scrolled_window_destroy (GtkObject *object);
static void gtk_scrolled_window_map (GtkWidget *widget);
static void gtk_scrolled_window_unmap (GtkWidget *widget);
static void gtk_scrolled_window_draw (GtkWidget *widget,
GdkRectangle *area);
static void gtk_scrolled_window_size_request (GtkWidget *widget,
GtkRequisition *requisition);
static void gtk_scrolled_window_size_allocate (GtkWidget *widget,
GtkAllocation *allocation);
static void gtk_scrolled_window_add (GtkContainer *container,
GtkWidget *widget);
static void gtk_scrolled_window_remove (GtkContainer *container,
GtkWidget *widget);
static void gtk_scrolled_window_foreach (GtkContainer *container,
GtkCallback callback,
gpointer callback_data);
static void gtk_scrolled_window_viewport_allocate (GtkWidget *widget,
GtkAllocation *allocation);
static void gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
gpointer data);
static GtkContainerClass *parent_class = NULL;
guint
gtk_scrolled_window_get_type ()
{
static guint scrolled_window_type = 0;
if (!scrolled_window_type)
{
GtkTypeInfo scrolled_window_info =
{
"GtkScrolledWindow",
sizeof (GtkScrolledWindow),
sizeof (GtkScrolledWindowClass),
(GtkClassInitFunc) gtk_scrolled_window_class_init,
(GtkObjectInitFunc) gtk_scrolled_window_init,
(GtkArgFunc) NULL,
};
scrolled_window_type = gtk_type_unique (gtk_container_get_type (), &scrolled_window_info);
}
return scrolled_window_type;
}
static void
gtk_scrolled_window_class_init (GtkScrolledWindowClass *class)
{
GtkObjectClass *object_class;
GtkWidgetClass *widget_class;
GtkContainerClass *container_class;
object_class = (GtkObjectClass*) class;
widget_class = (GtkWidgetClass*) class;
container_class = (GtkContainerClass*) class;
parent_class = gtk_type_class (gtk_container_get_type ());
object_class->destroy = gtk_scrolled_window_destroy;
widget_class->map = gtk_scrolled_window_map;
widget_class->unmap = gtk_scrolled_window_unmap;
widget_class->draw = gtk_scrolled_window_draw;
widget_class->size_request = gtk_scrolled_window_size_request;
widget_class->size_allocate = gtk_scrolled_window_size_allocate;
container_class->add = gtk_scrolled_window_add;
container_class->remove = gtk_scrolled_window_remove;
container_class->foreach = gtk_scrolled_window_foreach;
}
static void
gtk_scrolled_window_init (GtkScrolledWindow *scrolled_window)
{
GTK_WIDGET_SET_FLAGS (scrolled_window, GTK_NO_WINDOW);
scrolled_window->hscrollbar = NULL;
scrolled_window->vscrollbar = NULL;
scrolled_window->hscrollbar_policy = GTK_POLICY_ALWAYS;
scrolled_window->vscrollbar_policy = GTK_POLICY_ALWAYS;
}
GtkWidget*
gtk_scrolled_window_new (GtkAdjustment *hadjustment,
GtkAdjustment *vadjustment)
{
GtkScrolledWindow *scrolled_window;
scrolled_window = gtk_type_new (gtk_scrolled_window_get_type ());
scrolled_window->viewport = gtk_viewport_new (hadjustment, vadjustment);
hadjustment = gtk_viewport_get_hadjustment (GTK_VIEWPORT (scrolled_window->viewport));
vadjustment = gtk_viewport_get_vadjustment (GTK_VIEWPORT (scrolled_window->viewport));
gtk_signal_connect (GTK_OBJECT (hadjustment), "changed",
(GtkSignalFunc) gtk_scrolled_window_adjustment_changed,
(gpointer) scrolled_window);
gtk_signal_connect (GTK_OBJECT (vadjustment), "changed",
(GtkSignalFunc) gtk_scrolled_window_adjustment_changed,
(gpointer) scrolled_window);
scrolled_window->hscrollbar = gtk_hscrollbar_new (hadjustment);
scrolled_window->vscrollbar = gtk_vscrollbar_new (vadjustment);
gtk_widget_set_parent (scrolled_window->viewport, GTK_WIDGET (scrolled_window));
gtk_widget_set_parent (scrolled_window->hscrollbar, GTK_WIDGET (scrolled_window));
gtk_widget_set_parent (scrolled_window->vscrollbar, GTK_WIDGET (scrolled_window));
gtk_widget_show (scrolled_window->viewport);
gtk_widget_show (scrolled_window->hscrollbar);
gtk_widget_show (scrolled_window->vscrollbar);
return GTK_WIDGET (scrolled_window);
}
GtkAdjustment*
gtk_scrolled_window_get_hadjustment (GtkScrolledWindow *scrolled_window)
{
g_return_val_if_fail (scrolled_window != NULL, NULL);
g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
return gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar));
}
GtkAdjustment*
gtk_scrolled_window_get_vadjustment (GtkScrolledWindow *scrolled_window)
{
g_return_val_if_fail (scrolled_window != NULL, NULL);
g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
return gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar));
}
void
gtk_scrolled_window_set_policy (GtkScrolledWindow *scrolled_window,
GtkPolicyType hscrollbar_policy,
GtkPolicyType vscrollbar_policy)
{
g_return_if_fail (scrolled_window != NULL);
g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
if ((scrolled_window->hscrollbar_policy != hscrollbar_policy) ||
(scrolled_window->vscrollbar_policy != vscrollbar_policy))
{
scrolled_window->hscrollbar_policy = hscrollbar_policy;
scrolled_window->vscrollbar_policy = vscrollbar_policy;
if (GTK_WIDGET (scrolled_window)->parent)
gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
}
}
static void
gtk_scrolled_window_destroy (GtkObject *object)
{
GtkScrolledWindow *scrolled_window;
g_return_if_fail (object != NULL);
g_return_if_fail (GTK_IS_SCROLLED_WINDOW (object));
scrolled_window = GTK_SCROLLED_WINDOW (object);
gtk_widget_destroy (scrolled_window->viewport);
gtk_widget_destroy (scrolled_window->hscrollbar);
gtk_widget_destroy (scrolled_window->vscrollbar);
if (GTK_OBJECT_CLASS (parent_class)->destroy)
(* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}
static void
gtk_scrolled_window_map (GtkWidget *widget)
{
GtkScrolledWindow *scrolled_window;
g_return_if_fail (widget != NULL);
g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
if (!GTK_WIDGET_MAPPED (widget))
{
GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
scrolled_window = GTK_SCROLLED_WINDOW (widget);
if (GTK_WIDGET_VISIBLE (scrolled_window->viewport) &&
!GTK_WIDGET_MAPPED (scrolled_window->viewport))
gtk_widget_map (scrolled_window->viewport);
if (GTK_WIDGET_VISIBLE (scrolled_window->hscrollbar) &&
!GTK_WIDGET_MAPPED (scrolled_window->hscrollbar))
gtk_widget_map (scrolled_window->hscrollbar);
if (GTK_WIDGET_VISIBLE (scrolled_window->vscrollbar) &&
!GTK_WIDGET_MAPPED (scrolled_window->vscrollbar))
gtk_widget_map (scrolled_window->vscrollbar);
}
}
static void
gtk_scrolled_window_unmap (GtkWidget *widget)
{
GtkScrolledWindow *scrolled_window;
g_return_if_fail (widget != NULL);
g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
if (GTK_WIDGET_MAPPED (widget))
{
GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
scrolled_window = GTK_SCROLLED_WINDOW (widget);
if (GTK_WIDGET_MAPPED (scrolled_window->viewport))
gtk_widget_unmap (scrolled_window->viewport);
if (GTK_WIDGET_MAPPED (scrolled_window->hscrollbar))
gtk_widget_unmap (scrolled_window->hscrollbar);
if (GTK_WIDGET_MAPPED (scrolled_window->vscrollbar))
gtk_widget_unmap (scrolled_window->vscrollbar);
}
}
static void
gtk_scrolled_window_draw (GtkWidget *widget,
GdkRectangle *area)
{
GtkScrolledWindow *scrolled_window;
GdkRectangle child_area;
g_return_if_fail (widget != NULL);
g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
g_return_if_fail (area != NULL);
if (GTK_WIDGET_DRAWABLE (widget))
{
scrolled_window = GTK_SCROLLED_WINDOW (widget);
if (gtk_widget_intersect (scrolled_window->viewport, area, &child_area))
gtk_widget_draw (scrolled_window->viewport, &child_area);
if (gtk_widget_intersect (scrolled_window->hscrollbar, area, &child_area))
gtk_widget_draw (scrolled_window->hscrollbar, &child_area);
if (gtk_widget_intersect (scrolled_window->vscrollbar, area, &child_area))
gtk_widget_draw (scrolled_window->vscrollbar, &child_area);
}
}
static void
gtk_scrolled_window_size_request (GtkWidget *widget,
GtkRequisition *requisition)
{
GtkScrolledWindow *scrolled_window;
gint extra_height;
gint extra_width;
g_return_if_fail (widget != NULL);
g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
g_return_if_fail (requisition != NULL);
scrolled_window = GTK_SCROLLED_WINDOW (widget);
requisition->width = 0;
requisition->height = 0;
if (GTK_WIDGET_VISIBLE (scrolled_window->viewport))
{
gtk_widget_size_request (scrolled_window->viewport, &scrolled_window->viewport->requisition);
requisition->width += scrolled_window->viewport->requisition.width;
requisition->height += scrolled_window->viewport->requisition.height;
}
extra_width = 0;
extra_height = 0;
if ((scrolled_window->hscrollbar_policy == GTK_POLICY_AUTOMATIC) ||
GTK_WIDGET_VISIBLE (scrolled_window->hscrollbar))
{
gtk_widget_size_request (scrolled_window->hscrollbar,
&scrolled_window->hscrollbar->requisition);
requisition->width = MAX (requisition->width, scrolled_window->hscrollbar->requisition.width);
extra_height = SCROLLBAR_SPACING + scrolled_window->hscrollbar->requisition.height;
}
if ((scrolled_window->vscrollbar_policy == GTK_POLICY_AUTOMATIC) ||
GTK_WIDGET_VISIBLE (scrolled_window->vscrollbar))
{
gtk_widget_size_request (scrolled_window->vscrollbar,
&scrolled_window->vscrollbar->requisition);
requisition->height = MAX (requisition->height, scrolled_window->vscrollbar->requisition.height);
extra_width = SCROLLBAR_SPACING + scrolled_window->vscrollbar->requisition.width;
}
requisition->width += GTK_CONTAINER (widget)->border_width * 2 + extra_width;
requisition->height += GTK_CONTAINER (widget)->border_width * 2 + extra_height;
}
static void
gtk_scrolled_window_size_allocate (GtkWidget *widget,
GtkAllocation *allocation)
{
GtkScrolledWindow *scrolled_window;
GtkAllocation viewport_allocation;
GtkAllocation child_allocation;
guint previous_hvis;
guint previous_vvis;
g_return_if_fail (widget != NULL);
g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
g_return_if_fail (allocation != NULL);
scrolled_window = GTK_SCROLLED_WINDOW (widget);
widget->allocation = *allocation;
gtk_scrolled_window_viewport_allocate (widget, &viewport_allocation);
gtk_container_disable_resize (GTK_CONTAINER (scrolled_window));
if (GTK_WIDGET_VISIBLE (scrolled_window->viewport))
{
do {
gtk_scrolled_window_viewport_allocate (widget, &viewport_allocation);
child_allocation.x = viewport_allocation.x + allocation->x;
child_allocation.y = viewport_allocation.y + allocation->y;
child_allocation.width = viewport_allocation.width;
child_allocation.height = viewport_allocation.height;
previous_hvis = GTK_WIDGET_VISIBLE (scrolled_window->hscrollbar);
previous_vvis = GTK_WIDGET_VISIBLE (scrolled_window->vscrollbar);
gtk_widget_size_allocate (scrolled_window->viewport, &child_allocation);
} while ((previous_hvis != GTK_WIDGET_VISIBLE (scrolled_window->hscrollbar)) ||
(previous_vvis != GTK_WIDGET_VISIBLE (scrolled_window->vscrollbar)));
}
if (GTK_WIDGET_VISIBLE (scrolled_window->hscrollbar))
{
child_allocation.x = viewport_allocation.x;
child_allocation.y = viewport_allocation.y + viewport_allocation.height + SCROLLBAR_SPACING;
child_allocation.width = viewport_allocation.width;
child_allocation.height = scrolled_window->hscrollbar->requisition.height;
child_allocation.x += allocation->x;
child_allocation.y += allocation->y;
gtk_widget_size_allocate (scrolled_window->hscrollbar, &child_allocation);
}
if (GTK_WIDGET_VISIBLE (scrolled_window->vscrollbar))
{
child_allocation.x = viewport_allocation.x + viewport_allocation.width + SCROLLBAR_SPACING;
child_allocation.y = viewport_allocation.y;
child_allocation.width = scrolled_window->vscrollbar->requisition.width;
child_allocation.height = viewport_allocation.height;
child_allocation.x += allocation->x;
child_allocation.y += allocation->y;
gtk_widget_size_allocate (scrolled_window->vscrollbar, &child_allocation);
}
gtk_container_enable_resize (GTK_CONTAINER (scrolled_window));
}
static void
gtk_scrolled_window_add (GtkContainer *container,
GtkWidget *widget)
{
GtkScrolledWindow *scrolled_window;
g_return_if_fail (container != NULL);
g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
g_return_if_fail (widget != NULL);
scrolled_window = GTK_SCROLLED_WINDOW (container);
gtk_container_add (GTK_CONTAINER (scrolled_window->viewport), widget);
}
static void
gtk_scrolled_window_remove (GtkContainer *container,
GtkWidget *widget)
{
GtkScrolledWindow *scrolled_window;
g_return_if_fail (container != NULL);
g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
g_return_if_fail (widget != NULL);
scrolled_window = GTK_SCROLLED_WINDOW (container);
gtk_container_remove (GTK_CONTAINER (scrolled_window->viewport), widget);
}
static void
gtk_scrolled_window_foreach (GtkContainer *container,
GtkCallback callback,
gpointer callback_data)
{
GtkScrolledWindow *scrolled_window;
g_return_if_fail (container != NULL);
g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
g_return_if_fail (callback != NULL);
scrolled_window = GTK_SCROLLED_WINDOW (container);
(* callback) (scrolled_window->viewport, callback_data);
}
static void
gtk_scrolled_window_viewport_allocate (GtkWidget *widget,
GtkAllocation *allocation)
{
GtkScrolledWindow *scrolled_window;
g_return_if_fail (widget != NULL);
g_return_if_fail (allocation != NULL);
scrolled_window = GTK_SCROLLED_WINDOW (widget);
allocation->x = GTK_CONTAINER (widget)->border_width;
allocation->y = GTK_CONTAINER (widget)->border_width;
allocation->width = widget->allocation.width - allocation->x * 2;
allocation->height = widget->allocation.height - allocation->y * 2;
if (GTK_WIDGET_VISIBLE (scrolled_window->vscrollbar))
allocation->width -= scrolled_window->vscrollbar->requisition.width + SCROLLBAR_SPACING;
if (GTK_WIDGET_VISIBLE (scrolled_window->hscrollbar))
allocation->height -= scrolled_window->hscrollbar->requisition.height + SCROLLBAR_SPACING;
}
static void
gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
gpointer data)
{
GtkScrolledWindow *scrolled_win;
GtkWidget *scrollbar;
gint hide_scrollbar;
gint policy;
g_return_if_fail (adjustment != NULL);
g_return_if_fail (data != NULL);
scrolled_win = GTK_SCROLLED_WINDOW (data);
if (adjustment == gtk_range_get_adjustment (GTK_RANGE (scrolled_win->hscrollbar)))
{
scrollbar = scrolled_win->hscrollbar;
policy = scrolled_win->hscrollbar_policy;
}
else if (adjustment == gtk_range_get_adjustment (GTK_RANGE (scrolled_win->vscrollbar)))
{
scrollbar = scrolled_win->vscrollbar;
policy = scrolled_win->vscrollbar_policy;
}
else
{
g_warning ("could not determine which adjustment scrollbar received change signal for");
return;
}
if (policy == GTK_POLICY_AUTOMATIC)
{
hide_scrollbar = FALSE;
if ((adjustment->upper - adjustment->lower) <= adjustment->page_size)
hide_scrollbar = TRUE;
if (hide_scrollbar)
{
if (GTK_WIDGET_VISIBLE (scrollbar))
gtk_widget_hide (scrollbar);
}
else
{
if (!GTK_WIDGET_VISIBLE (scrollbar))
gtk_widget_show (scrollbar);
}
}
}
|