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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
|
/* 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 Lesser 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#include "gtkscrolledwindow.h"
#include "gtksignal.h"
#include "gtkintl.h"
/* scrolled window policy and size requisition handling:
*
* gtk size requisition works as follows:
* a widget upon size-request reports the width and height that it finds
* to be best suited to display its contents, including children.
* the width and/or height reported from a widget upon size requisition
* may be overidden by the user by specifying a width and/or height
* other than 0 through gtk_widget_set_usize().
*
* a scrolled window needs (for imlementing all three policy types) to
* request its width and height based on two different rationales.
* 1) the user wants the scrolled window to just fit into the space
* that it gets allocated for a specifc dimension.
* 1.1) this does not apply if the user specified a concrete value
* value for that specific dimension by either specifying usize for the
* scrolled window or for its child.
* 2) the user wants the scrolled window to take as much space up as
* is desired by the child for a specifc dimension (i.e. POLICY_NEVER).
*
* also, kinda obvious:
* 3) a user would certainly not have choosen a scrolled window as a container
* for the child, if the resulting allocation takes up more space than the
* child would have allocated without the scrolled window.
*
* conclusions:
* A) from 1) follows: the scrolled window shouldn't request more space for a
* specifc dimension than is required at minimum.
* B) from 1.1) follows: the requisition may be overidden by usize of the scrolled
* window (done automatically) or by usize of the child (needs to be checked).
* C) from 2) follows: for POLICY_NEVER, the scrolled window simply reports the
* child's dimension.
* D) from 3) follows: the scrolled window child's minimum width and minimum height
* under A) at least correspond to the space taken up by its scrollbars.
*/
#define SCROLLBAR_SPACING(w) (GTK_SCROLLED_WINDOW_GET_CLASS (w)->scrollbar_spacing)
#define DEFAULT_SCROLLBAR_SPACING 3
enum {
PROP_0,
PROP_HADJUSTMENT,
PROP_VADJUSTMENT,
PROP_HSCROLLBAR_POLICY,
PROP_VSCROLLBAR_POLICY,
PROP_WINDOW_PLACEMENT,
PROP_SHADOW_TYPE,
PROP_LAST
};
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_finalize (GObject *object);
static void gtk_scrolled_window_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void gtk_scrolled_window_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static gint gtk_scrolled_window_expose (GtkWidget *widget,
GdkEventExpose *event);
static void gtk_scrolled_window_size_request (GtkWidget *widget,
GtkRequisition *requisition);
static void gtk_scrolled_window_size_allocate (GtkWidget *widget,
GtkAllocation *allocation);
static gint gtk_scrolled_window_scroll_event (GtkWidget *widget,
GdkEventScroll *event);
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_forall (GtkContainer *container,
gboolean include_internals,
GtkCallback callback,
gpointer callback_data);
static void gtk_scrolled_window_relative_allocation(GtkWidget *widget,
GtkAllocation *allocation);
static void gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
gpointer data);
static GtkContainerClass *parent_class = NULL;
GtkType
gtk_scrolled_window_get_type (void)
{
static GtkType scrolled_window_type = 0;
if (!scrolled_window_type)
{
static const GtkTypeInfo scrolled_window_info =
{
"GtkScrolledWindow",
sizeof (GtkScrolledWindow),
sizeof (GtkScrolledWindowClass),
(GtkClassInitFunc) gtk_scrolled_window_class_init,
(GtkObjectInitFunc) gtk_scrolled_window_init,
/* reserved_1 */ NULL,
/* reserved_2 */ NULL,
(GtkClassInitFunc) NULL,
};
scrolled_window_type = gtk_type_unique (GTK_TYPE_BIN, &scrolled_window_info);
}
return scrolled_window_type;
}
static void
gtk_scrolled_window_class_init (GtkScrolledWindowClass *class)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (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_TYPE_BIN);
gobject_class->finalize = gtk_scrolled_window_finalize;
gobject_class->set_property = gtk_scrolled_window_set_property;
gobject_class->get_property = gtk_scrolled_window_get_property;
object_class->destroy = gtk_scrolled_window_destroy;
widget_class->expose_event = gtk_scrolled_window_expose;
widget_class->size_request = gtk_scrolled_window_size_request;
widget_class->size_allocate = gtk_scrolled_window_size_allocate;
widget_class->scroll_event = gtk_scrolled_window_scroll_event;
container_class->add = gtk_scrolled_window_add;
container_class->remove = gtk_scrolled_window_remove;
container_class->forall = gtk_scrolled_window_forall;
class->scrollbar_spacing = DEFAULT_SCROLLBAR_SPACING;
g_object_class_install_property (gobject_class,
PROP_HADJUSTMENT,
g_param_spec_object ("hadjustment",
_("Horizontal Adjustment"),
_("The GtkAdjustment for the horizontal position."),
GTK_TYPE_ADJUSTMENT,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (gobject_class,
PROP_VADJUSTMENT,
g_param_spec_object ("vadjustment",
_("Vertical Adjustment"),
_("The GtkAdjustment for the vertical position."),
GTK_TYPE_ADJUSTMENT,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
g_object_class_install_property (gobject_class,
PROP_HSCROLLBAR_POLICY,
g_param_spec_enum ("hscrollbar_policy",
_("Horizontal Scrollbar Policy"),
_("When the horizontal scrollbar is displayed"),
GTK_TYPE_POLICY_TYPE,
GTK_POLICY_ALWAYS,
G_PARAM_READABLE | G_PARAM_WRITABLE));
g_object_class_install_property (gobject_class,
PROP_VSCROLLBAR_POLICY,
g_param_spec_enum ("vscrollbar_policy",
_("Vertical Scrollbar Policy"),
_("When the vertical scrollbar is displayed"),
GTK_TYPE_POLICY_TYPE,
GTK_POLICY_ALWAYS,
G_PARAM_READABLE | G_PARAM_WRITABLE));
g_object_class_install_property (gobject_class,
PROP_WINDOW_PLACEMENT,
g_param_spec_enum ("window_placement",
_("Window Placement"),
_("Where the contents are located with respect to the scrollbars"),
GTK_TYPE_CORNER_TYPE,
GTK_CORNER_TOP_LEFT,
G_PARAM_READABLE | G_PARAM_WRITABLE));
g_object_class_install_property (gobject_class,
PROP_SHADOW_TYPE,
g_param_spec_enum ("shadow_type",
_("Shadow Type"),
_("Style of bevel around the contents"),
GTK_TYPE_SHADOW_TYPE,
GTK_SHADOW_NONE,
G_PARAM_READABLE | G_PARAM_WRITABLE));
}
static void
gtk_scrolled_window_init (GtkScrolledWindow *scrolled_window)
{
GTK_WIDGET_SET_FLAGS (scrolled_window, GTK_NO_WINDOW);
gtk_container_set_resize_mode (GTK_CONTAINER (scrolled_window), GTK_RESIZE_QUEUE);
scrolled_window->hscrollbar = NULL;
scrolled_window->vscrollbar = NULL;
scrolled_window->hscrollbar_policy = GTK_POLICY_ALWAYS;
scrolled_window->vscrollbar_policy = GTK_POLICY_ALWAYS;
scrolled_window->hscrollbar_visible = FALSE;
scrolled_window->vscrollbar_visible = FALSE;
scrolled_window->window_placement = GTK_CORNER_TOP_LEFT;
}
GtkWidget*
gtk_scrolled_window_new (GtkAdjustment *hadjustment,
GtkAdjustment *vadjustment)
{
GtkWidget *scrolled_window;
if (hadjustment)
g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadjustment), NULL);
if (vadjustment)
g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadjustment), NULL);
scrolled_window = gtk_widget_new (GTK_TYPE_SCROLLED_WINDOW,
"hadjustment", hadjustment,
"vadjustment", vadjustment,
NULL);
return scrolled_window;
}
void
gtk_scrolled_window_set_hadjustment (GtkScrolledWindow *scrolled_window,
GtkAdjustment *hadjustment)
{
GtkBin *bin;
g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
if (hadjustment)
g_return_if_fail (GTK_IS_ADJUSTMENT (hadjustment));
else
hadjustment = (GtkAdjustment*) gtk_object_new (GTK_TYPE_ADJUSTMENT, NULL);
bin = GTK_BIN (scrolled_window);
if (!scrolled_window->hscrollbar)
{
gtk_widget_push_composite_child ();
scrolled_window->hscrollbar = gtk_hscrollbar_new (hadjustment);
gtk_widget_set_composite_name (scrolled_window->hscrollbar, "hscrollbar");
gtk_widget_pop_composite_child ();
gtk_widget_set_parent (scrolled_window->hscrollbar, GTK_WIDGET (scrolled_window));
gtk_widget_ref (scrolled_window->hscrollbar);
gtk_widget_show (scrolled_window->hscrollbar);
}
else
{
GtkAdjustment *old_adjustment;
old_adjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar));
if (old_adjustment == hadjustment)
return;
gtk_signal_disconnect_by_func (GTK_OBJECT (old_adjustment),
GTK_SIGNAL_FUNC (gtk_scrolled_window_adjustment_changed),
scrolled_window);
gtk_range_set_adjustment (GTK_RANGE (scrolled_window->hscrollbar),
hadjustment);
}
hadjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar));
gtk_signal_connect (GTK_OBJECT (hadjustment),
"changed",
GTK_SIGNAL_FUNC (gtk_scrolled_window_adjustment_changed),
scrolled_window);
gtk_scrolled_window_adjustment_changed (hadjustment, scrolled_window);
if (bin->child)
gtk_widget_set_scroll_adjustments (bin->child,
gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)),
gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar)));
g_object_notify (G_OBJECT (scrolled_window), "hadjustment");
}
void
gtk_scrolled_window_set_vadjustment (GtkScrolledWindow *scrolled_window,
GtkAdjustment *vadjustment)
{
GtkBin *bin;
g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
if (vadjustment)
g_return_if_fail (GTK_IS_ADJUSTMENT (vadjustment));
else
vadjustment = (GtkAdjustment*) gtk_object_new (GTK_TYPE_ADJUSTMENT, NULL);
bin = GTK_BIN (scrolled_window);
if (!scrolled_window->vscrollbar)
{
gtk_widget_push_composite_child ();
scrolled_window->vscrollbar = gtk_vscrollbar_new (vadjustment);
gtk_widget_set_composite_name (scrolled_window->vscrollbar, "vscrollbar");
gtk_widget_pop_composite_child ();
gtk_widget_set_parent (scrolled_window->vscrollbar, GTK_WIDGET (scrolled_window));
gtk_widget_ref (scrolled_window->vscrollbar);
gtk_widget_show (scrolled_window->vscrollbar);
}
else
{
GtkAdjustment *old_adjustment;
old_adjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar));
if (old_adjustment == vadjustment)
return;
gtk_signal_disconnect_by_func (GTK_OBJECT (old_adjustment),
GTK_SIGNAL_FUNC (gtk_scrolled_window_adjustment_changed),
scrolled_window);
gtk_range_set_adjustment (GTK_RANGE (scrolled_window->vscrollbar),
vadjustment);
}
vadjustment = gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar));
gtk_signal_connect (GTK_OBJECT (vadjustment),
"changed",
GTK_SIGNAL_FUNC (gtk_scrolled_window_adjustment_changed),
scrolled_window);
gtk_scrolled_window_adjustment_changed (vadjustment, scrolled_window);
if (bin->child)
gtk_widget_set_scroll_adjustments (bin->child,
gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)),
gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar)));
g_object_notify (G_OBJECT (scrolled_window), "vadjustment");
}
GtkAdjustment*
gtk_scrolled_window_get_hadjustment (GtkScrolledWindow *scrolled_window)
{
g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
return (scrolled_window->hscrollbar ?
gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)) :
NULL);
}
GtkAdjustment*
gtk_scrolled_window_get_vadjustment (GtkScrolledWindow *scrolled_window)
{
g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), NULL);
return (scrolled_window->vscrollbar ?
gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar)) :
NULL);
}
void
gtk_scrolled_window_set_policy (GtkScrolledWindow *scrolled_window,
GtkPolicyType hscrollbar_policy,
GtkPolicyType vscrollbar_policy)
{
GObject *object = G_OBJECT (scrolled_window);
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;
gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
g_object_freeze_notify (object);
g_object_notify (object, "hscrollbar_policy");
g_object_notify (object, "vscrollbar_policy");
g_object_thaw_notify (object);
}
}
/**
* gtk_scrolled_window_get_policy:
* @scrolled_window: a #GtkScrolledWindow
* @hscrollbar_policy: location to store the policy for the horizontal scrollbar, or %NULL.
* @vscrollbar_policy: location to store the policy for the horizontal scrollbar, or %NULL.
*
* Retrieves the current policy values for the horizontal and vertical
* scrollbars. See gtk_scrolled_window_set_policy().
**/
void
gtk_scrolled_window_get_policy (GtkScrolledWindow *scrolled_window,
GtkPolicyType *hscrollbar_policy,
GtkPolicyType *vscrollbar_policy)
{
g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
if (hscrollbar_policy)
*hscrollbar_policy = scrolled_window->hscrollbar_policy;
if (vscrollbar_policy)
*vscrollbar_policy = scrolled_window->vscrollbar_policy;
}
void
gtk_scrolled_window_set_placement (GtkScrolledWindow *scrolled_window,
GtkCornerType window_placement)
{
g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
if (scrolled_window->window_placement != window_placement)
{
scrolled_window->window_placement = window_placement;
gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
g_object_notify (G_OBJECT (scrolled_window), "window_placement");
}
}
/**
* gtk_scrolled_window_get_placement:
* @scrolled_window: a #GtkScrolledWindow
*
* Gets the placement of the scrollbars for the scrolled window. See
* gtk_scrolled_window_set_placement().
*
* Return value: the current placement value.
**/
GtkCornerType
gtk_scrolled_window_get_placement (GtkScrolledWindow *scrolled_window)
{
g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_CORNER_TOP_LEFT);
return scrolled_window->window_placement;
}
/**
* gtk_scrolled_window_set_shadow_type:
* @scrolled_window: a #GtkScrolledWindow
* @type: kind of shadow to draw around scrolled window contents
*
* Changes the type of shadow drawn around the contents of
* @scrolled_window.
*
**/
void
gtk_scrolled_window_set_shadow_type (GtkScrolledWindow *scrolled_window,
GtkShadowType type)
{
g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
g_return_if_fail (type >= GTK_SHADOW_NONE && type <= GTK_SHADOW_ETCHED_OUT);
if (scrolled_window->shadow_type != type)
{
scrolled_window->shadow_type = type;
if (GTK_WIDGET_DRAWABLE (scrolled_window))
gtk_widget_queue_clear (GTK_WIDGET (scrolled_window));
gtk_widget_queue_resize (GTK_WIDGET (scrolled_window));
g_object_notify (G_OBJECT (scrolled_window), "shadow_type");
}
}
/**
* gtk_scrolled_window_get_shadow_type:
* @scrolled_window: a #GtkScrolledWindow
*
* Gets the shadow type of the scrolled window. See
* gtk_scrolled_window_set_shadow_type().
*
* Return value: the current shadow type
**/
GtkShadowType
gtk_scrolled_window_get_shadow_type (GtkScrolledWindow *scrolled_window)
{
g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_NONE);
return scrolled_window->shadow_type;
}
static void
gtk_scrolled_window_destroy (GtkObject *object)
{
GtkScrolledWindow *scrolled_window;
g_return_if_fail (GTK_IS_SCROLLED_WINDOW (object));
scrolled_window = GTK_SCROLLED_WINDOW (object);
gtk_widget_unparent (scrolled_window->hscrollbar);
gtk_widget_unparent (scrolled_window->vscrollbar);
gtk_widget_destroy (scrolled_window->hscrollbar);
gtk_widget_destroy (scrolled_window->vscrollbar);
GTK_OBJECT_CLASS (parent_class)->destroy (object);
}
static void
gtk_scrolled_window_finalize (GObject *object)
{
GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
gtk_widget_unref (scrolled_window->hscrollbar);
gtk_widget_unref (scrolled_window->vscrollbar);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gtk_scrolled_window_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
switch (prop_id)
{
case PROP_HADJUSTMENT:
gtk_scrolled_window_set_hadjustment (scrolled_window,
g_value_get_object (value));
break;
case PROP_VADJUSTMENT:
gtk_scrolled_window_set_vadjustment (scrolled_window,
g_value_get_object (value));
break;
case PROP_HSCROLLBAR_POLICY:
gtk_scrolled_window_set_policy (scrolled_window,
g_value_get_enum (value),
scrolled_window->vscrollbar_policy);
break;
case PROP_VSCROLLBAR_POLICY:
gtk_scrolled_window_set_policy (scrolled_window,
scrolled_window->hscrollbar_policy,
g_value_get_enum (value));
break;
case PROP_WINDOW_PLACEMENT:
gtk_scrolled_window_set_placement (scrolled_window,
g_value_get_enum (value));
break;
case PROP_SHADOW_TYPE:
gtk_scrolled_window_set_shadow_type (scrolled_window,
g_value_get_enum (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_scrolled_window_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (object);
switch (prop_id)
{
case PROP_HADJUSTMENT:
g_value_set_object (value,
G_OBJECT (gtk_scrolled_window_get_hadjustment (scrolled_window)));
break;
case PROP_VADJUSTMENT:
g_value_set_object (value,
G_OBJECT (gtk_scrolled_window_get_vadjustment (scrolled_window)));
break;
case PROP_HSCROLLBAR_POLICY:
g_value_set_enum (value, scrolled_window->hscrollbar_policy);
break;
case PROP_VSCROLLBAR_POLICY:
g_value_set_enum (value, scrolled_window->vscrollbar_policy);
break;
case PROP_WINDOW_PLACEMENT:
g_value_set_enum (value, scrolled_window->window_placement);
break;
case PROP_SHADOW_TYPE:
g_value_set_enum (value, scrolled_window->shadow_type);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_scrolled_window_paint (GtkWidget *widget,
GdkRectangle *area)
{
GtkAllocation relative_allocation;
GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
{
gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
relative_allocation.x -= widget->style->xthickness;
relative_allocation.y -= widget->style->ythickness;
relative_allocation.width += 2 * widget->style->xthickness;
relative_allocation.height += 2 * widget->style->ythickness;
gtk_paint_shadow (widget->style, widget->window,
GTK_STATE_NORMAL, scrolled_window->shadow_type,
area, widget, "scrolled_window",
widget->allocation.x + relative_allocation.x,
widget->allocation.y + relative_allocation.y,
relative_allocation.width,
relative_allocation.height);
}
}
static gint
gtk_scrolled_window_expose (GtkWidget *widget,
GdkEventExpose *event)
{
if (GTK_WIDGET_DRAWABLE (widget))
{
gtk_scrolled_window_paint (widget, &event->area);
(* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
}
return FALSE;
}
static void
gtk_scrolled_window_forall (GtkContainer *container,
gboolean include_internals,
GtkCallback callback,
gpointer callback_data)
{
g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
g_return_if_fail (callback != NULL);
GTK_CONTAINER_CLASS (parent_class)->forall (container,
include_internals,
callback,
callback_data);
if (include_internals)
{
GtkScrolledWindow *scrolled_window;
scrolled_window = GTK_SCROLLED_WINDOW (container);
if (scrolled_window->vscrollbar)
callback (scrolled_window->vscrollbar, callback_data);
if (scrolled_window->hscrollbar)
callback (scrolled_window->hscrollbar, callback_data);
}
}
static void
gtk_scrolled_window_size_request (GtkWidget *widget,
GtkRequisition *requisition)
{
GtkScrolledWindow *scrolled_window;
GtkBin *bin;
gint extra_width;
gint extra_height;
GtkRequisition hscrollbar_requisition;
GtkRequisition vscrollbar_requisition;
GtkRequisition child_requisition;
g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
g_return_if_fail (requisition != NULL);
scrolled_window = GTK_SCROLLED_WINDOW (widget);
bin = GTK_BIN (scrolled_window);
extra_width = 0;
extra_height = 0;
requisition->width = 0;
requisition->height = 0;
gtk_widget_size_request (scrolled_window->hscrollbar,
&hscrollbar_requisition);
gtk_widget_size_request (scrolled_window->vscrollbar,
&vscrollbar_requisition);
if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
{
gtk_widget_size_request (bin->child, &child_requisition);
if (scrolled_window->hscrollbar_policy == GTK_POLICY_NEVER)
requisition->width += child_requisition.width;
else
{
GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (bin->child, FALSE);
if (aux_info && aux_info->width > 0)
{
requisition->width += aux_info->width;
extra_width = -1;
}
else
requisition->width += vscrollbar_requisition.width;
}
if (scrolled_window->vscrollbar_policy == GTK_POLICY_NEVER)
requisition->height += child_requisition.height;
else
{
GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (bin->child, FALSE);
if (aux_info && aux_info->height > 0)
{
requisition->height += aux_info->height;
extra_height = -1;
}
else
requisition->height += hscrollbar_requisition.height;
}
}
if (scrolled_window->hscrollbar_policy == GTK_POLICY_AUTOMATIC ||
scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS)
{
requisition->width = MAX (requisition->width, hscrollbar_requisition.width);
if (!extra_height || scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS)
extra_height = SCROLLBAR_SPACING (scrolled_window) + hscrollbar_requisition.height;
}
if (scrolled_window->vscrollbar_policy == GTK_POLICY_AUTOMATIC ||
scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS)
{
requisition->height = MAX (requisition->height, vscrollbar_requisition.height);
if (!extra_height || scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS)
extra_width = SCROLLBAR_SPACING (scrolled_window) + vscrollbar_requisition.width;
}
requisition->width += GTK_CONTAINER (widget)->border_width * 2 + MAX (0, extra_width);
requisition->height += GTK_CONTAINER (widget)->border_width * 2 + MAX (0, extra_height);
if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
{
requisition->width += 2 * widget->style->xthickness;
requisition->height += 2 * widget->style->ythickness;
}
}
static void
gtk_scrolled_window_relative_allocation (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;
if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
{
allocation->x += widget->style->xthickness;
allocation->y += widget->style->ythickness;
}
allocation->width = MAX (1, (gint)widget->allocation.width - allocation->x * 2);
allocation->height = MAX (1, (gint)widget->allocation.height - allocation->y * 2);
if (scrolled_window->vscrollbar_visible)
{
GtkRequisition vscrollbar_requisition;
gtk_widget_get_child_requisition (scrolled_window->vscrollbar,
&vscrollbar_requisition);
if (scrolled_window->window_placement == GTK_CORNER_TOP_RIGHT ||
scrolled_window->window_placement == GTK_CORNER_BOTTOM_RIGHT)
allocation->x += (vscrollbar_requisition.width +
SCROLLBAR_SPACING (scrolled_window));
allocation->width = MAX (1, (gint)allocation->width -
((gint)vscrollbar_requisition.width +
(gint)SCROLLBAR_SPACING (scrolled_window)));
}
if (scrolled_window->hscrollbar_visible)
{
GtkRequisition hscrollbar_requisition;
gtk_widget_get_child_requisition (scrolled_window->hscrollbar,
&hscrollbar_requisition);
if (scrolled_window->window_placement == GTK_CORNER_BOTTOM_LEFT ||
scrolled_window->window_placement == GTK_CORNER_BOTTOM_RIGHT)
allocation->y += (hscrollbar_requisition.height +
SCROLLBAR_SPACING (scrolled_window));
allocation->height = MAX (1, (gint)allocation->height -
((gint)hscrollbar_requisition.height +
(gint)SCROLLBAR_SPACING (scrolled_window)));
}
}
static void
gtk_scrolled_window_size_allocate (GtkWidget *widget,
GtkAllocation *allocation)
{
GtkScrolledWindow *scrolled_window;
GtkBin *bin;
GtkAllocation relative_allocation;
GtkAllocation child_allocation;
g_return_if_fail (GTK_IS_SCROLLED_WINDOW (widget));
g_return_if_fail (allocation != NULL);
scrolled_window = GTK_SCROLLED_WINDOW (widget);
bin = GTK_BIN (scrolled_window);
widget->allocation = *allocation;
if (scrolled_window->hscrollbar_policy == GTK_POLICY_ALWAYS)
scrolled_window->hscrollbar_visible = TRUE;
else if (scrolled_window->hscrollbar_policy == GTK_POLICY_NEVER)
scrolled_window->hscrollbar_visible = FALSE;
if (scrolled_window->vscrollbar_policy == GTK_POLICY_ALWAYS)
scrolled_window->vscrollbar_visible = TRUE;
else if (scrolled_window->vscrollbar_policy == GTK_POLICY_NEVER)
scrolled_window->vscrollbar_visible = FALSE;
if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
{
gboolean previous_hvis;
gboolean previous_vvis;
guint count = 0;
do
{
gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
child_allocation.x = relative_allocation.x + allocation->x;
child_allocation.y = relative_allocation.y + allocation->y;
child_allocation.width = relative_allocation.width;
child_allocation.height = relative_allocation.height;
previous_hvis = scrolled_window->hscrollbar_visible;
previous_vvis = scrolled_window->vscrollbar_visible;
gtk_widget_size_allocate (bin->child, &child_allocation);
/* If, after the first iteration, the hscrollbar and the
* vscrollbar flip visiblity, then we need both.
*/
if (count &&
previous_hvis != scrolled_window->hscrollbar_visible &&
previous_vvis != scrolled_window->vscrollbar_visible)
{
scrolled_window->hscrollbar_visible = TRUE;
scrolled_window->vscrollbar_visible = TRUE;
/* a new resize is already queued at this point,
* so we will immediatedly get reinvoked
*/
return;
}
count++;
}
while (previous_hvis != scrolled_window->hscrollbar_visible ||
previous_vvis != scrolled_window->vscrollbar_visible);
}
else
gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
if (scrolled_window->hscrollbar_visible)
{
GtkRequisition hscrollbar_requisition;
gtk_widget_get_child_requisition (scrolled_window->hscrollbar,
&hscrollbar_requisition);
if (!GTK_WIDGET_VISIBLE (scrolled_window->hscrollbar))
gtk_widget_show (scrolled_window->hscrollbar);
child_allocation.x = relative_allocation.x;
if (scrolled_window->window_placement == GTK_CORNER_TOP_LEFT ||
scrolled_window->window_placement == GTK_CORNER_TOP_RIGHT)
child_allocation.y = (relative_allocation.y +
relative_allocation.height +
SCROLLBAR_SPACING (scrolled_window) +
(scrolled_window->shadow_type == GTK_SHADOW_NONE ?
0 : widget->style->ythickness));
else
child_allocation.y = GTK_CONTAINER (scrolled_window)->border_width;
child_allocation.width = relative_allocation.width;
child_allocation.height = hscrollbar_requisition.height;
child_allocation.x += allocation->x;
child_allocation.y += allocation->y;
if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
{
child_allocation.x -= widget->style->xthickness;
child_allocation.width += 2 * widget->style->xthickness;
}
gtk_widget_size_allocate (scrolled_window->hscrollbar, &child_allocation);
}
else if (GTK_WIDGET_VISIBLE (scrolled_window->hscrollbar))
gtk_widget_hide (scrolled_window->hscrollbar);
if (scrolled_window->vscrollbar_visible)
{
GtkRequisition vscrollbar_requisition;
if (!GTK_WIDGET_VISIBLE (scrolled_window->vscrollbar))
gtk_widget_show (scrolled_window->vscrollbar);
gtk_widget_get_child_requisition (scrolled_window->vscrollbar,
&vscrollbar_requisition);
if (scrolled_window->window_placement == GTK_CORNER_TOP_LEFT ||
scrolled_window->window_placement == GTK_CORNER_BOTTOM_LEFT)
child_allocation.x = (relative_allocation.x +
relative_allocation.width +
SCROLLBAR_SPACING (scrolled_window) +
(scrolled_window->shadow_type == GTK_SHADOW_NONE ?
0 : widget->style->xthickness));
else
child_allocation.x = GTK_CONTAINER (scrolled_window)->border_width;
child_allocation.y = relative_allocation.y;
child_allocation.width = vscrollbar_requisition.width;
child_allocation.height = relative_allocation.height;
child_allocation.x += allocation->x;
child_allocation.y += allocation->y;
if (scrolled_window->shadow_type != GTK_SHADOW_NONE)
{
child_allocation.y -= widget->style->ythickness;
child_allocation.height += 2 * widget->style->ythickness;
}
gtk_widget_size_allocate (scrolled_window->vscrollbar, &child_allocation);
}
else if (GTK_WIDGET_VISIBLE (scrolled_window->vscrollbar))
gtk_widget_hide (scrolled_window->vscrollbar);
}
static gint
gtk_scrolled_window_scroll_event (GtkWidget *widget,
GdkEventScroll *event)
{
GtkWidget *range;
g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (widget), FALSE);
g_return_val_if_fail (event != NULL, FALSE);
if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_DOWN)
range = GTK_SCROLLED_WINDOW (widget)->vscrollbar;
else
range = GTK_SCROLLED_WINDOW (widget)->hscrollbar;
if (range && GTK_WIDGET_VISIBLE (range))
{
GtkAdjustment *adj = GTK_RANGE (range)->adjustment;
gdouble new_value;
if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_LEFT)
new_value = adj->value - adj->page_increment / 2;
else
new_value = adj->value + adj->page_increment / 2;
new_value = CLAMP (new_value, adj->lower, adj->upper - adj->page_size);
gtk_adjustment_set_value (adj, new_value);
return TRUE;
}
return FALSE;
}
static void
gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
gpointer data)
{
GtkScrolledWindow *scrolled_win;
g_return_if_fail (adjustment != NULL);
g_return_if_fail (data != NULL);
scrolled_win = GTK_SCROLLED_WINDOW (data);
if (scrolled_win->hscrollbar &&
adjustment == gtk_range_get_adjustment (GTK_RANGE (scrolled_win->hscrollbar)))
{
if (scrolled_win->hscrollbar_policy == GTK_POLICY_AUTOMATIC)
{
gboolean visible;
visible = scrolled_win->hscrollbar_visible;
scrolled_win->hscrollbar_visible = (adjustment->upper - adjustment->lower >
adjustment->page_size);
if (scrolled_win->hscrollbar_visible != visible)
gtk_widget_queue_resize (GTK_WIDGET (scrolled_win));
}
}
else if (scrolled_win->vscrollbar &&
adjustment == gtk_range_get_adjustment (GTK_RANGE (scrolled_win->vscrollbar)))
{
if (scrolled_win->vscrollbar_policy == GTK_POLICY_AUTOMATIC)
{
gboolean visible;
visible = scrolled_win->vscrollbar_visible;
scrolled_win->vscrollbar_visible = (adjustment->upper - adjustment->lower >
adjustment->page_size);
if (scrolled_win->vscrollbar_visible != visible)
gtk_widget_queue_resize (GTK_WIDGET (scrolled_win));
}
}
}
static void
gtk_scrolled_window_add (GtkContainer *container,
GtkWidget *child)
{
GtkScrolledWindow *scrolled_window;
GtkBin *bin;
bin = GTK_BIN (container);
g_return_if_fail (bin->child == NULL);
scrolled_window = GTK_SCROLLED_WINDOW (container);
bin->child = child;
gtk_widget_set_parent (child, GTK_WIDGET (bin));
/* this is a temporary message */
if (!gtk_widget_set_scroll_adjustments (child,
gtk_range_get_adjustment (GTK_RANGE (scrolled_window->hscrollbar)),
gtk_range_get_adjustment (GTK_RANGE (scrolled_window->vscrollbar))))
g_warning ("gtk_scrolled_window_add(): cannot add non scrollable widget "
"use gtk_scrolled_window_add_with_viewport() instead");
}
static void
gtk_scrolled_window_remove (GtkContainer *container,
GtkWidget *child)
{
g_return_if_fail (GTK_IS_SCROLLED_WINDOW (container));
g_return_if_fail (child != NULL);
g_return_if_fail (GTK_BIN (container)->child == child);
gtk_widget_set_scroll_adjustments (child, NULL, NULL);
/* chain parent class handler to remove child */
GTK_CONTAINER_CLASS (parent_class)->remove (container, child);
}
void
gtk_scrolled_window_add_with_viewport (GtkScrolledWindow *scrolled_window,
GtkWidget *child)
{
GtkBin *bin;
GtkWidget *viewport;
g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolled_window));
g_return_if_fail (GTK_IS_WIDGET (child));
g_return_if_fail (child->parent == NULL);
bin = GTK_BIN (scrolled_window);
if (bin->child != NULL)
{
g_return_if_fail (GTK_IS_VIEWPORT (bin->child));
g_return_if_fail (GTK_BIN (bin->child)->child == NULL);
viewport = bin->child;
}
else
{
viewport =
gtk_viewport_new (gtk_scrolled_window_get_hadjustment (scrolled_window),
gtk_scrolled_window_get_vadjustment (scrolled_window));
gtk_container_add (GTK_CONTAINER (scrolled_window), viewport);
}
gtk_widget_show (viewport);
gtk_container_add (GTK_CONTAINER (viewport), child);
}
|