Repository navigation
Expand file tree
/
Copy pathgtkscrolledwindow.c
More file actions
4663 lines (3984 loc) · 156 KB
/
Copy pathgtkscrolledwindow.c
File metadata and controls
4663 lines (3984 loc) · 156 KB
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
/* 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, see <http://www.gnu.org/licenses/>.
*/
/*
* 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 "config.h"
#include "gtkscrolledwindow.h"
#include "gtkadjustment.h"
#include "gtkadjustmentprivate.h"
#include "gtkbindings.h"
#include "gtkcsscustomgadgetprivate.h"
#include "gtkdnd.h"
#include "gtkintl.h"
#include "gtkmain.h"
#include "gtkmarshalers.h"
#include "gtkprivate.h"
#include "gtkscrollable.h"
#include "gtkscrollbar.h"
#include "gtkrangeprivate.h"
#include "gtktypebuiltins.h"
#include "gtkviewport.h"
#include "gtkwidgetprivate.h"
#include "gtkwindow.h"
#include "gtkkineticscrolling.h"
#include "a11y/gtkscrolledwindowaccessible.h"
#include "gtkstylecontextprivate.h"
#include "gtkprogresstrackerprivate.h"
#include "gtksettingsprivate.h"
#include <math.h>
/**
* SECTION:gtkscrolledwindow
* @Short_description: Adds scrollbars to its child widget
* @Title: GtkScrolledWindow
* @See_also: #GtkScrollable, #GtkViewport, #GtkAdjustment
*
* GtkScrolledWindow is a #GtkBin subclass: it’s a container
* the accepts a single child widget. GtkScrolledWindow adds scrollbars
* to the child widget and optionally draws a beveled frame around the
* child widget.
*
* The scrolled window can work in two ways. Some widgets have native
* scrolling support; these widgets implement the #GtkScrollable interface.
* Widgets with native scroll support include #GtkTreeView, #GtkTextView,
* and #GtkLayout.
*
* For widgets that lack native scrolling support, the #GtkViewport
* widget acts as an adaptor class, implementing scrollability for child
* widgets that lack their own scrolling capabilities. Use #GtkViewport
* to scroll child widgets such as #GtkGrid, #GtkBox, and so on.
*
* If a widget has native scrolling abilities, it can be added to the
* GtkScrolledWindow with gtk_container_add(). If a widget does not, you
* must first add the widget to a #GtkViewport, then add the #GtkViewport
* to the scrolled window. gtk_container_add() will do this for you for
* widgets that don’t implement #GtkScrollable natively, so you can
* ignore the presence of the viewport.
*
* If gtk_container_add() has added a #GtkViewport for you, you can remove
* both your added child widget from the #GtkViewport and the #GtkViewport
* from the GtkScrolledWindow with either of the calls
* |[<!-- language="C" -->
* gtk_container_remove (GTK_CONTAINER (scrolled_window),
* child_widget);
* // or
* gtk_container_remove (GTK_CONTAINER (scrolled_window),
* gtk_bin_get_child (GTK_BIN (scrolled_window)));
* ]|
*
* The position of the scrollbars is controlled by the scroll adjustments.
* See #GtkAdjustment for the fields in an adjustment — for
* #GtkScrollbar, used by GtkScrolledWindow, the “value” field
* represents the position of the scrollbar, which must be between the
* “lower” field and “upper - page_size.” The “page_size” field
* represents the size of the visible scrollable area. The
* “step_increment” and “page_increment” fields are used when the user
* asks to step down (using the small stepper arrows) or page down (using
* for example the PageDown key).
*
* If a GtkScrolledWindow doesn’t behave quite as you would like, or
* doesn’t have exactly the right layout, it’s very possible to set up
* your own scrolling with #GtkScrollbar and for example a #GtkGrid.
*
* # Touch support
*
* GtkScrolledWindow has built-in support for touch devices. When a
* touchscreen is used, swiping will move the scrolled window, and will
* expose 'kinetic' behavior. This can be turned off with the
* #GtkScrolledWindow:kinetic-scrolling property if it is undesired.
*
* GtkScrolledWindow also displays visual 'overshoot' indication when
* the content is pulled beyond the end, and this situation can be
* captured with the #GtkScrolledWindow::edge-overshot signal.
*
* If no mouse device is present, the scrollbars will overlayed as
* narrow, auto-hiding indicators over the content. If traditional
* scrollbars are desired although no mouse is present, this behaviour
* can be turned off with the #GtkScrolledWindow:overlay-scrolling
* property.
*
* # CSS nodes
*
* GtkScrolledWindow has a main CSS node with name scrolledwindow.
*
* It uses subnodes with names overshoot and undershoot to
* draw the overflow and underflow indications. These nodes get
* the .left, .right, .top or .bottom style class added depending
* on where the indication is drawn.
*
* GtkScrolledWindow also sets the positional style classes (.left,
* .right, .top, .bottom) and style classes related to overlay
* scrolling (.overlay-indicator, .dragging, .hovering) on its scrollbars.
*
* If both scrollbars are visible, the area where they meet is drawn
* with a subnode named junction.
*/
/* 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_size_request().
*
* a scrolled window needs (for implementing 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 TOUCH_BYPASS_CAPTURED_THRESHOLD 30
/* Kinetic scrolling */
#define MAX_OVERSHOOT_DISTANCE 100
#define DECELERATION_FRICTION 4
#define OVERSHOOT_FRICTION 20
#define SCROLL_CAPTURE_THRESHOLD_MS 150
/* Animated scrolling */
#define ANIMATION_DURATION 200
/* Overlay scrollbars */
#define INDICATOR_FADE_OUT_DELAY 2000
#define INDICATOR_FADE_OUT_DURATION 1000
#define INDICATOR_FADE_OUT_TIME 500
#define INDICATOR_CLOSE_DISTANCE 5
#define INDICATOR_FAR_DISTANCE 10
/* Scrolled off indication */
#define UNDERSHOOT_SIZE 40
typedef struct
{
GtkWidget *scrollbar;
GdkWindow *window;
gboolean over; /* either mouse over, or while dragging */
gint64 last_scroll_time;
guint conceil_timer;
gdouble current_pos;
gdouble source_pos;
gdouble target_pos;
GtkProgressTracker tracker;
guint tick_id;
guint over_timeout_id;
} Indicator;
typedef struct
{
gdouble dx;
gdouble dy;
guint32 evtime;
} ScrollHistoryElem;
struct _GtkScrolledWindowPrivate
{
GtkWidget *hscrollbar;
GtkWidget *vscrollbar;
GdkWindow *view_window;
GtkCssGadget *gadget;
GtkCssNode *overshoot_node[4];
GtkCssNode *undershoot_node[4];
Indicator hindicator;
Indicator vindicator;
GtkCornerType window_placement;
guint16 shadow_type;
guint hscrollbar_policy : 2;
guint vscrollbar_policy : 2;
guint hscrollbar_visible : 1;
guint vscrollbar_visible : 1;
guint focus_out : 1; /* used by ::move-focus-out implementation */
guint overlay_scrolling : 1;
guint use_indicators : 1;
guint auto_added_viewport : 1;
guint propagate_natural_width : 1;
guint propagate_natural_height : 1;
gint min_content_width;
gint min_content_height;
gint max_content_width;
gint max_content_height;
guint scroll_events_overshoot_id;
/* Kinetic scrolling */
GtkGesture *long_press_gesture;
GtkGesture *swipe_gesture;
GArray *scroll_history;
GdkDevice *scroll_device;
GdkWindow *scroll_window;
GdkCursor *scroll_cursor;
/* These two gestures are mutually exclusive */
GtkGesture *drag_gesture;
GtkGesture *pan_gesture;
gdouble drag_start_x;
gdouble drag_start_y;
GdkDevice *drag_device;
guint kinetic_scrolling : 1;
guint capture_button_press : 1;
guint in_drag : 1;
guint deceleration_id;
gdouble x_velocity;
gdouble y_velocity;
gdouble unclamped_hadj_value;
gdouble unclamped_vadj_value;
};
typedef struct
{
GtkScrolledWindow *scrolled_window;
gint64 last_deceleration_time;
GtkKineticScrolling *hscrolling;
GtkKineticScrolling *vscrolling;
} KineticScrollData;
enum {
PROP_0,
PROP_HADJUSTMENT,
PROP_VADJUSTMENT,
PROP_HSCROLLBAR_POLICY,
PROP_VSCROLLBAR_POLICY,
PROP_WINDOW_PLACEMENT,
PROP_SHADOW_TYPE,
PROP_MIN_CONTENT_WIDTH,
PROP_MIN_CONTENT_HEIGHT,
PROP_KINETIC_SCROLLING,
PROP_OVERLAY_SCROLLING,
PROP_MAX_CONTENT_WIDTH,
PROP_MAX_CONTENT_HEIGHT,
PROP_PROPAGATE_NATURAL_WIDTH,
PROP_PROPAGATE_NATURAL_HEIGHT,
NUM_PROPERTIES
};
/* Signals */
enum
{
SCROLL_CHILD,
MOVE_FOCUS_OUT,
EDGE_OVERSHOT,
EDGE_REACHED,
LAST_SIGNAL
};
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 void gtk_scrolled_window_finalize (GObject *object);
static void gtk_scrolled_window_destroy (GtkWidget *widget);
static void gtk_scrolled_window_snapshot (GtkWidget *widget,
GtkSnapshot *snapshot);
static void gtk_scrolled_window_size_allocate (GtkWidget *widget,
GtkAllocation *allocation);
static gboolean gtk_scrolled_window_scroll_event (GtkWidget *widget,
GdkEventScroll *event);
static gboolean gtk_scrolled_window_focus (GtkWidget *widget,
GtkDirectionType direction);
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 gboolean gtk_scrolled_window_scroll_child (GtkScrolledWindow *scrolled_window,
GtkScrollType scroll,
gboolean horizontal);
static void gtk_scrolled_window_move_focus_out (GtkScrolledWindow *scrolled_window,
GtkDirectionType direction_type);
static void gtk_scrolled_window_relative_allocation(GtkWidget *widget,
GtkAllocation *allocation);
static void gtk_scrolled_window_inner_allocation (GtkWidget *widget,
GtkAllocation *rect);
static void gtk_scrolled_window_allocate_scrollbar (GtkScrolledWindow *scrolled_window,
GtkWidget *scrollbar,
GtkAllocation *allocation);
static void gtk_scrolled_window_allocate_child (GtkScrolledWindow *swindow,
GtkAllocation *relative_allocation);
static void gtk_scrolled_window_adjustment_changed (GtkAdjustment *adjustment,
gpointer data);
static void gtk_scrolled_window_adjustment_value_changed (GtkAdjustment *adjustment,
gpointer data);
static gboolean gtk_widget_should_animate (GtkWidget *widget);
static void gtk_scrolled_window_measure_ (GtkWidget *widget,
GtkOrientation orientation,
int for_size,
int *minimum,
int *natural,
int *minimum_baseline,
int *natural_baseline);
static void gtk_scrolled_window_map (GtkWidget *widget);
static void gtk_scrolled_window_unmap (GtkWidget *widget);
static void gtk_scrolled_window_realize (GtkWidget *widget);
static void gtk_scrolled_window_unrealize (GtkWidget *widget);
static void gtk_scrolled_window_grab_notify (GtkWidget *widget,
gboolean was_grabbed);
static void _gtk_scrolled_window_set_adjustment_value (GtkScrolledWindow *scrolled_window,
GtkAdjustment *adjustment,
gdouble value);
static void gtk_scrolled_window_cancel_deceleration (GtkScrolledWindow *scrolled_window);
static gboolean _gtk_scrolled_window_get_overshoot (GtkScrolledWindow *scrolled_window,
gint *overshoot_x,
gint *overshoot_y);
static void gtk_scrolled_window_start_deceleration (GtkScrolledWindow *scrolled_window);
static void gtk_scrolled_window_update_use_indicators (GtkScrolledWindow *scrolled_window);
static void remove_indicator (GtkScrolledWindow *sw,
Indicator *indicator);
static void indicator_stop_fade (Indicator *indicator);
static gboolean maybe_hide_indicator (gpointer data);
static void indicator_start_fade (Indicator *indicator,
gdouble pos);
static void indicator_set_over (Indicator *indicator,
gboolean over);
static guint signals[LAST_SIGNAL] = {0};
static GParamSpec *properties[NUM_PROPERTIES];
G_DEFINE_TYPE_WITH_PRIVATE (GtkScrolledWindow, gtk_scrolled_window, GTK_TYPE_BIN)
static void
add_scroll_binding (GtkBindingSet *binding_set,
guint keyval,
GdkModifierType mask,
GtkScrollType scroll,
gboolean horizontal)
{
guint keypad_keyval = keyval - GDK_KEY_Left + GDK_KEY_KP_Left;
gtk_binding_entry_add_signal (binding_set, keyval, mask,
"scroll-child", 2,
GTK_TYPE_SCROLL_TYPE, scroll,
G_TYPE_BOOLEAN, horizontal);
gtk_binding_entry_add_signal (binding_set, keypad_keyval, mask,
"scroll-child", 2,
GTK_TYPE_SCROLL_TYPE, scroll,
G_TYPE_BOOLEAN, horizontal);
}
static void
add_tab_bindings (GtkBindingSet *binding_set,
GdkModifierType modifiers,
GtkDirectionType direction)
{
gtk_binding_entry_add_signal (binding_set, GDK_KEY_Tab, modifiers,
"move-focus-out", 1,
GTK_TYPE_DIRECTION_TYPE, direction);
gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Tab, modifiers,
"move-focus-out", 1,
GTK_TYPE_DIRECTION_TYPE, direction);
}
static gboolean
gtk_scrolled_window_leave_notify (GtkWidget *widget,
GdkEventCrossing *event)
{
GtkScrolledWindowPrivate *priv = GTK_SCROLLED_WINDOW (widget)->priv;
if (priv->use_indicators && event->detail != GDK_NOTIFY_INFERIOR)
{
indicator_set_over (&priv->hindicator, FALSE);
indicator_set_over (&priv->vindicator, FALSE);
}
return GDK_EVENT_PROPAGATE;
}
static void
update_scrollbar_positions (GtkScrolledWindow *scrolled_window)
{
GtkScrolledWindowPrivate *priv = scrolled_window->priv;
GtkStyleContext *context;
gboolean is_rtl;
if (priv->hscrollbar != NULL)
{
context = gtk_widget_get_style_context (priv->hscrollbar);
if (priv->window_placement == GTK_CORNER_TOP_LEFT ||
priv->window_placement == GTK_CORNER_TOP_RIGHT)
{
gtk_style_context_add_class (context, GTK_STYLE_CLASS_BOTTOM);
gtk_style_context_remove_class (context, GTK_STYLE_CLASS_TOP);
}
else
{
gtk_style_context_remove_class (context, GTK_STYLE_CLASS_BOTTOM);
gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOP);
}
}
if (priv->vscrollbar != NULL)
{
context = gtk_widget_get_style_context (priv->vscrollbar);
is_rtl = _gtk_widget_get_direction (GTK_WIDGET (scrolled_window)) == GTK_TEXT_DIR_RTL;
if ((is_rtl &&
(priv->window_placement == GTK_CORNER_TOP_RIGHT ||
priv->window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
(!is_rtl &&
(priv->window_placement == GTK_CORNER_TOP_LEFT ||
priv->window_placement == GTK_CORNER_BOTTOM_LEFT)))
{
gtk_style_context_add_class (context, GTK_STYLE_CLASS_RIGHT);
gtk_style_context_remove_class (context, GTK_STYLE_CLASS_LEFT);
}
else
{
gtk_style_context_remove_class (context, GTK_STYLE_CLASS_RIGHT);
gtk_style_context_add_class (context, GTK_STYLE_CLASS_LEFT);
}
}
}
static void
gtk_scrolled_window_direction_changed (GtkWidget *widget,
GtkTextDirection previous_dir)
{
GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
update_scrollbar_positions (scrolled_window);
GTK_WIDGET_CLASS (gtk_scrolled_window_parent_class)->direction_changed (widget, previous_dir);
}
static void
gtk_scrolled_window_class_init (GtkScrolledWindowClass *class)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
GtkWidgetClass *widget_class;
GtkContainerClass *container_class;
GtkBindingSet *binding_set;
widget_class = (GtkWidgetClass*) class;
container_class = (GtkContainerClass*) class;
gobject_class->set_property = gtk_scrolled_window_set_property;
gobject_class->get_property = gtk_scrolled_window_get_property;
gobject_class->finalize = gtk_scrolled_window_finalize;
widget_class->destroy = gtk_scrolled_window_destroy;
widget_class->snapshot = gtk_scrolled_window_snapshot;
widget_class->size_allocate = gtk_scrolled_window_size_allocate;
widget_class->scroll_event = gtk_scrolled_window_scroll_event;
widget_class->focus = gtk_scrolled_window_focus;
widget_class->measure = gtk_scrolled_window_measure_;
widget_class->map = gtk_scrolled_window_map;
widget_class->unmap = gtk_scrolled_window_unmap;
widget_class->grab_notify = gtk_scrolled_window_grab_notify;
widget_class->realize = gtk_scrolled_window_realize;
widget_class->unrealize = gtk_scrolled_window_unrealize;
widget_class->leave_notify_event = gtk_scrolled_window_leave_notify;
widget_class->direction_changed = gtk_scrolled_window_direction_changed;
container_class->add = gtk_scrolled_window_add;
container_class->remove = gtk_scrolled_window_remove;
container_class->forall = gtk_scrolled_window_forall;
class->scroll_child = gtk_scrolled_window_scroll_child;
class->move_focus_out = gtk_scrolled_window_move_focus_out;
properties[PROP_HADJUSTMENT] =
g_param_spec_object ("hadjustment",
P_("Horizontal Adjustment"),
P_("The GtkAdjustment for the horizontal position"),
GTK_TYPE_ADJUSTMENT,
GTK_PARAM_READWRITE|G_PARAM_CONSTRUCT);
properties[PROP_VADJUSTMENT] =
g_param_spec_object ("vadjustment",
P_("Vertical Adjustment"),
P_("The GtkAdjustment for the vertical position"),
GTK_TYPE_ADJUSTMENT,
GTK_PARAM_READWRITE|G_PARAM_CONSTRUCT);
properties[PROP_HSCROLLBAR_POLICY] =
g_param_spec_enum ("hscrollbar-policy",
P_("Horizontal Scrollbar Policy"),
P_("When the horizontal scrollbar is displayed"),
GTK_TYPE_POLICY_TYPE,
GTK_POLICY_AUTOMATIC,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
properties[PROP_VSCROLLBAR_POLICY] =
g_param_spec_enum ("vscrollbar-policy",
P_("Vertical Scrollbar Policy"),
P_("When the vertical scrollbar is displayed"),
GTK_TYPE_POLICY_TYPE,
GTK_POLICY_AUTOMATIC,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
properties[PROP_WINDOW_PLACEMENT] =
g_param_spec_enum ("window-placement",
P_("Window Placement"),
P_("Where the contents are located with respect to the scrollbars."),
GTK_TYPE_CORNER_TYPE,
GTK_CORNER_TOP_LEFT,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
properties[PROP_SHADOW_TYPE] =
g_param_spec_enum ("shadow-type",
P_("Shadow Type"),
P_("Style of bevel around the contents"),
GTK_TYPE_SHADOW_TYPE,
GTK_SHADOW_NONE,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkScrolledWindow:min-content-width:
*
* The minimum content width of @scrolled_window, or -1 if not set.
*
* Since: 3.0
*/
properties[PROP_MIN_CONTENT_WIDTH] =
g_param_spec_int ("min-content-width",
P_("Minimum Content Width"),
P_("The minimum width that the scrolled window will allocate to its content"),
-1, G_MAXINT, -1,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkScrolledWindow:min-content-height:
*
* The minimum content height of @scrolled_window, or -1 if not set.
*
* Since: 3.0
*/
properties[PROP_MIN_CONTENT_HEIGHT] =
g_param_spec_int ("min-content-height",
P_("Minimum Content Height"),
P_("The minimum height that the scrolled window will allocate to its content"),
-1, G_MAXINT, -1,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkScrolledWindow:kinetic-scrolling:
*
* Whether kinetic scrolling is enabled or not. Kinetic scrolling
* only applies to devices with source %GDK_SOURCE_TOUCHSCREEN.
*
* Since: 3.4
*/
properties[PROP_KINETIC_SCROLLING] =
g_param_spec_boolean ("kinetic-scrolling",
P_("Kinetic Scrolling"),
P_("Kinetic scrolling mode."),
TRUE,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkScrolledWindow:overlay-scrolling:
*
* Whether overlay scrolling is enabled or not. If it is, the
* scrollbars are only added as traditional widgets when a mouse
* is present. Otherwise, they are overlayed on top of the content,
* as narrow indicators.
*
* Since: 3.16
*/
properties[PROP_OVERLAY_SCROLLING] =
g_param_spec_boolean ("overlay-scrolling",
P_("Overlay Scrolling"),
P_("Overlay scrolling mode"),
TRUE,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkScrolledWindow:max-content-width:
*
* The maximum content width of @scrolled_window, or -1 if not set.
*
* Since: 3.22
*/
properties[PROP_MAX_CONTENT_WIDTH] =
g_param_spec_int ("max-content-width",
P_("Maximum Content Width"),
P_("The maximum width that the scrolled window will allocate to its content"),
-1, G_MAXINT, -1,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkScrolledWindow:max-content-height:
*
* The maximum content height of @scrolled_window, or -1 if not set.
*
* Since: 3.22
*/
properties[PROP_MAX_CONTENT_HEIGHT] =
g_param_spec_int ("max-content-height",
P_("Maximum Content Height"),
P_("The maximum height that the scrolled window will allocate to its content"),
-1, G_MAXINT, -1,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkScrolledWindow:propagate-natural-width:
*
* Whether the natural width of the child should be calculated and propagated
* through the scrolled windows requested natural width.
*
* This is useful in cases where an attempt should be made to allocate exactly
* enough space for the natural size of the child.
*
* Since: 3.22
*/
properties[PROP_PROPAGATE_NATURAL_WIDTH] =
g_param_spec_boolean ("propagate-natural-width",
P_("Propagate Natural Width"),
P_("Propagate Natural Width"),
FALSE,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkScrolledWindow:propagate-natural-height:
*
* Whether the natural height of the child should be calculated and propagated
* through the scrolled windows requested natural height.
*
* This is useful in cases where an attempt should be made to allocate exactly
* enough space for the natural size of the child.
*
* Since: 3.22
*/
properties[PROP_PROPAGATE_NATURAL_HEIGHT] =
g_param_spec_boolean ("propagate-natural-height",
P_("Propagate Natural Height"),
P_("Propagate Natural Height"),
FALSE,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
g_object_class_install_properties (gobject_class, NUM_PROPERTIES, properties);
/**
* GtkScrolledWindow::scroll-child:
* @scrolled_window: a #GtkScrolledWindow
* @scroll: a #GtkScrollType describing how much to scroll
* @horizontal: whether the keybinding scrolls the child
* horizontally or not
*
* The ::scroll-child signal is a
* [keybinding signal][GtkBindingSignal]
* which gets emitted when a keybinding that scrolls is pressed.
* The horizontal or vertical adjustment is updated which triggers a
* signal that the scrolled windows child may listen to and scroll itself.
*/
signals[SCROLL_CHILD] =
g_signal_new (I_("scroll-child"),
G_TYPE_FROM_CLASS (gobject_class),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GtkScrolledWindowClass, scroll_child),
NULL, NULL,
_gtk_marshal_BOOLEAN__ENUM_BOOLEAN,
G_TYPE_BOOLEAN, 2,
GTK_TYPE_SCROLL_TYPE,
G_TYPE_BOOLEAN);
/**
* GtkScrolledWindow::move-focus-out:
* @scrolled_window: a #GtkScrolledWindow
* @direction_type: either %GTK_DIR_TAB_FORWARD or
* %GTK_DIR_TAB_BACKWARD
*
* The ::move-focus-out signal is a
* [keybinding signal][GtkBindingSignal] which gets
* emitted when focus is moved away from the scrolled window by a
* keybinding. The #GtkWidget::move-focus signal is emitted with
* @direction_type on this scrolled windows toplevel parent in the
* container hierarchy. The default bindings for this signal are
* `Tab + Ctrl` and `Tab + Ctrl + Shift`.
*/
signals[MOVE_FOCUS_OUT] =
g_signal_new (I_("move-focus-out"),
G_TYPE_FROM_CLASS (gobject_class),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GtkScrolledWindowClass, move_focus_out),
NULL, NULL,
NULL,
G_TYPE_NONE, 1,
GTK_TYPE_DIRECTION_TYPE);
/**
* GtkScrolledWindow::edge-overshot:
* @scrolled_window: a #GtkScrolledWindow
* @pos: edge side that was hit
*
* The ::edge-overshot signal is emitted whenever user initiated scrolling
* makes the scrolledwindow firmly surpass (ie. with some edge resistance)
* the lower or upper limits defined by the adjustment in that orientation.
*
* A similar behavior without edge resistance is provided by the
* #GtkScrolledWindow::edge-reached signal.
*
* Note: The @pos argument is LTR/RTL aware, so callers should be aware too
* if intending to provide behavior on horizontal edges.
*
* Since: 3.16
*/
signals[EDGE_OVERSHOT] =
g_signal_new (I_("edge-overshot"),
G_TYPE_FROM_CLASS (gobject_class),
G_SIGNAL_RUN_LAST, 0,
NULL, NULL,
g_cclosure_marshal_generic,
G_TYPE_NONE, 1, GTK_TYPE_POSITION_TYPE);
/**
* GtkScrolledWindow::edge-reached:
* @scrolled_window: a #GtkScrolledWindow
* @pos: edge side that was reached
*
* The ::edge-reached signal is emitted whenever user-initiated scrolling
* makes the scrolledwindow exactly reaches the lower or upper limits
* defined by the adjustment in that orientation.
*
* A similar behavior with edge resistance is provided by the
* #GtkScrolledWindow::edge-overshot signal.
*
* Note: The @pos argument is LTR/RTL aware, so callers should be aware too
* if intending to provide behavior on horizontal edges.
*
* Since: 3.16
*/
signals[EDGE_REACHED] =
g_signal_new (I_("edge-reached"),
G_TYPE_FROM_CLASS (gobject_class),
G_SIGNAL_RUN_LAST, 0,
NULL, NULL,
g_cclosure_marshal_generic,
G_TYPE_NONE, 1, GTK_TYPE_POSITION_TYPE);
binding_set = gtk_binding_set_by_class (class);
add_scroll_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, TRUE);
add_scroll_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD, TRUE);
add_scroll_binding (binding_set, GDK_KEY_Up, GDK_CONTROL_MASK, GTK_SCROLL_STEP_BACKWARD, FALSE);
add_scroll_binding (binding_set, GDK_KEY_Down, GDK_CONTROL_MASK, GTK_SCROLL_STEP_FORWARD, FALSE);
add_scroll_binding (binding_set, GDK_KEY_Page_Up, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_BACKWARD, TRUE);
add_scroll_binding (binding_set, GDK_KEY_Page_Down, GDK_CONTROL_MASK, GTK_SCROLL_PAGE_FORWARD, TRUE);
add_scroll_binding (binding_set, GDK_KEY_Page_Up, 0, GTK_SCROLL_PAGE_BACKWARD, FALSE);
add_scroll_binding (binding_set, GDK_KEY_Page_Down, 0, GTK_SCROLL_PAGE_FORWARD, FALSE);
add_scroll_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK, GTK_SCROLL_START, TRUE);
add_scroll_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK, GTK_SCROLL_END, TRUE);
add_scroll_binding (binding_set, GDK_KEY_Home, 0, GTK_SCROLL_START, FALSE);
add_scroll_binding (binding_set, GDK_KEY_End, 0, GTK_SCROLL_END, FALSE);
add_tab_bindings (binding_set, GDK_CONTROL_MASK, GTK_DIR_TAB_FORWARD);
add_tab_bindings (binding_set, GDK_CONTROL_MASK | GDK_SHIFT_MASK, GTK_DIR_TAB_BACKWARD);
gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_SCROLLED_WINDOW_ACCESSIBLE);
gtk_widget_class_set_css_name (widget_class, "scrolledwindow");
}
static gboolean
may_hscroll (GtkScrolledWindow *scrolled_window)
{
GtkScrolledWindowPrivate *priv = scrolled_window->priv;
return priv->hscrollbar_visible || priv->hscrollbar_policy == GTK_POLICY_EXTERNAL;
}
static gboolean
may_vscroll (GtkScrolledWindow *scrolled_window)
{
GtkScrolledWindowPrivate *priv = scrolled_window->priv;
return priv->vscrollbar_visible || priv->vscrollbar_policy == GTK_POLICY_EXTERNAL;
}
static inline gboolean
policy_may_be_visible (GtkPolicyType policy)
{
return policy == GTK_POLICY_ALWAYS || policy == GTK_POLICY_AUTOMATIC;
}
static void
scrolled_window_drag_begin_cb (GtkScrolledWindow *scrolled_window,
gdouble start_x,
gdouble start_y,
GtkGesture *gesture)
{
GtkScrolledWindowPrivate *priv = scrolled_window->priv;
GtkEventSequenceState state;
GdkEventSequence *sequence;
GtkWidget *event_widget;
const GdkEvent *event;
priv->in_drag = FALSE;
priv->drag_start_x = priv->unclamped_hadj_value;
priv->drag_start_y = priv->unclamped_vadj_value;
gtk_scrolled_window_cancel_deceleration (scrolled_window);
sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
event = gtk_gesture_get_last_event (gesture, sequence);
event_widget = gtk_get_event_widget ((GdkEvent *) event);
if (event_widget == priv->vscrollbar || event_widget == priv->hscrollbar ||
(!may_hscroll (scrolled_window) && !may_vscroll (scrolled_window)))
state = GTK_EVENT_SEQUENCE_DENIED;
else if (priv->capture_button_press)
state = GTK_EVENT_SEQUENCE_CLAIMED;
else
return;
gtk_gesture_set_sequence_state (gesture, sequence, state);
}
static void
gtk_scrolled_window_invalidate_overshoot (GtkScrolledWindow *scrolled_window)
{
GtkAllocation child_allocation;
gint overshoot_x, overshoot_y;
if (!_gtk_scrolled_window_get_overshoot (scrolled_window, &overshoot_x, &overshoot_y))
return;
gtk_scrolled_window_relative_allocation (GTK_WIDGET (scrolled_window),
&child_allocation);
if (overshoot_x != 0)
{
gtk_widget_queue_draw_area (GTK_WIDGET (scrolled_window),
overshoot_x < 0 ? child_allocation.x :
child_allocation.x + child_allocation.width - MAX_OVERSHOOT_DISTANCE,
child_allocation.y,
MAX_OVERSHOOT_DISTANCE,
child_allocation.height);
}
if (overshoot_y != 0)
{
gtk_widget_queue_draw_area (GTK_WIDGET (scrolled_window),
child_allocation.x,
overshoot_y < 0 ? child_allocation.y :
child_allocation.y + child_allocation.height - MAX_OVERSHOOT_DISTANCE,
child_allocation.width,
MAX_OVERSHOOT_DISTANCE);
}
}
static void
scrolled_window_drag_update_cb (GtkScrolledWindow *scrolled_window,
gdouble offset_x,
gdouble offset_y,
GtkGesture *gesture)
{
GtkScrolledWindowPrivate *priv = scrolled_window->priv;
GtkAdjustment *hadjustment;
GtkAdjustment *vadjustment;
gdouble dx, dy;
gtk_scrolled_window_invalidate_overshoot (scrolled_window);
if (!priv->capture_button_press)
{
GdkEventSequence *sequence;
sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
gtk_gesture_set_sequence_state (gesture, sequence,
GTK_EVENT_SEQUENCE_CLAIMED);
}
hadjustment = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
if (hadjustment && may_hscroll (scrolled_window))
{
dx = priv->drag_start_x - offset_x;
_gtk_scrolled_window_set_adjustment_value (scrolled_window,
hadjustment, dx);
}
vadjustment = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
if (vadjustment && may_vscroll (scrolled_window))
{
dy = priv->drag_start_y - offset_y;
_gtk_scrolled_window_set_adjustment_value (scrolled_window,
vadjustment, dy);
}
gtk_scrolled_window_invalidate_overshoot (scrolled_window);
}
static void
scrolled_window_drag_end_cb (GtkScrolledWindow *scrolled_window,
GdkEventSequence *sequence,
GtkGesture *gesture)
{
GtkScrolledWindowPrivate *priv = scrolled_window->priv;
if (!priv->in_drag || !gtk_gesture_handles_sequence (gesture, sequence))
gtk_gesture_set_state (gesture, GTK_EVENT_SEQUENCE_DENIED);
}
static void
gtk_scrolled_window_decelerate (GtkScrolledWindow *scrolled_window,
gdouble x_velocity,
gdouble y_velocity)
{
GtkScrolledWindowPrivate *priv = scrolled_window->priv;
gboolean overshoot;
overshoot = _gtk_scrolled_window_get_overshoot (scrolled_window, NULL, NULL);
priv->x_velocity = x_velocity;
priv->y_velocity = y_velocity;
/* Zero out vector components for which we don't scroll */
if (!may_hscroll (scrolled_window))
priv->x_velocity = 0;
if (!may_vscroll (scrolled_window))