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
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
|
/* 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-2001. 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/.
*/
/**
* SECTION:gtkbutton
* @Short_description: A widget that emits a signal when clicked on
* @Title: GtkButton
*
* The #GtkButton widget is generally used to trigger a callback function that is
* called when the button is pressed. The various signals and how to use them
* are outlined below.
*
* The #GtkButton widget can hold any valid child widget. That is, it can hold
* almost any other standard #GtkWidget. The most commonly used child is the
* #GtkLabel.
*
* # CSS nodes
*
* GtkButton has a single CSS node with name button. The node will get the
* style classes .image-button or .text-button, if the content is just an
* image or label, respectively. It may also receive the .flat style class.
*
* Other style classes that are commonly used with GtkButton include
* .suggested-action and .destructive-action. In special cases, buttons
* can be made round by adding the .circular style class.
*
* Button-like widgets like #GtkToggleButton, #GtkMenuButton, #GtkVolumeButton,
* #GtkLockButton, #GtkColorButton, #GtkFontButton or #GtkFileChooserButton use
* style classes such as .toggle, .popup, .scale, .lock, .color, .file
* to differentiate themselves from a plain GtkButton.
*/
#include "config.h"
#include "gtkbuttonprivate.h"
#include "gtkactionhelperprivate.h"
#include "gtkapplicationprivate.h"
#include "gtkbox.h"
#include "gtkcheckbutton.h"
#include "gtkcontainerprivate.h"
#include "gtkgesturemultipress.h"
#include "gtkimage.h"
#include "gtkintl.h"
#include "gtklabel.h"
#include "gtkmain.h"
#include "gtkmarshalers.h"
#include "gtkprivate.h"
#include "gtksizerequest.h"
#include "gtktypebuiltins.h"
#include "gtkwidgetprivate.h"
#include "a11y/gtkbuttonaccessible.h"
#include <string.h>
/* Time out before giving up on getting a key release when animating
* the close button.
*/
#define ACTIVATE_TIMEOUT 250
struct _GtkButtonPrivate
{
GtkActionHelper *action_helper;
GdkDevice *grab_keyboard;
GtkGesture *gesture;
guint activate_timeout;
guint button_down : 1;
guint in_button : 1;
guint use_underline : 1;
guint child_type : 2;
};
enum {
CLICKED,
ENTER,
LEAVE,
ACTIVATE,
LAST_SIGNAL
};
enum {
PROP_0,
PROP_LABEL,
PROP_RELIEF,
PROP_USE_UNDERLINE,
PROP_ICON_NAME,
/* actionable properties */
PROP_ACTION_NAME,
PROP_ACTION_TARGET,
LAST_PROP = PROP_ACTION_NAME
};
enum {
LABEL_CHILD,
ICON_CHILD,
WIDGET_CHILD
};
static void gtk_button_finalize (GObject *object);
static void gtk_button_dispose (GObject *object);
static void gtk_button_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void gtk_button_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static void gtk_button_display_changed (GtkWidget *widget,
GdkDisplay *previous_display);
static void gtk_button_unrealize (GtkWidget * widget);
static gint gtk_button_key_release (GtkWidget * widget, GdkEventKey * event);
static void gtk_real_button_clicked (GtkButton * button);
static void gtk_real_button_activate (GtkButton *button);
static void gtk_button_update_state (GtkButton *button);
static void gtk_button_finish_activate (GtkButton *button,
gboolean do_it);
static void gtk_button_state_flags_changed (GtkWidget *widget,
GtkStateFlags previous_state);
static void gtk_button_grab_notify (GtkWidget *widget,
gboolean was_grabbed);
static void gtk_button_do_release (GtkButton *button,
gboolean emit_clicked);
static void gtk_button_actionable_iface_init (GtkActionableInterface *iface);
static void gtk_button_measure_ (GtkWidget *widget,
GtkOrientation orientation,
int for_size,
int *minimum,
int *natural,
int *minimum_baseline,
int *natural_baseline);
static void gtk_button_set_child_type (GtkButton *button, guint child_type);
static GParamSpec *props[LAST_PROP] = { NULL, };
static guint button_signals[LAST_SIGNAL] = { 0 };
G_DEFINE_TYPE_WITH_CODE (GtkButton, gtk_button, GTK_TYPE_BIN,
G_ADD_PRIVATE (GtkButton)
G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIONABLE, gtk_button_actionable_iface_init))
static void
gtk_button_add (GtkContainer *container, GtkWidget *child)
{
GtkButton *button = GTK_BUTTON (container);
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
if (priv->child_type != WIDGET_CHILD)
gtk_container_remove (container, gtk_bin_get_child (GTK_BIN (button)));
gtk_button_set_child_type (button, WIDGET_CHILD);
GTK_CONTAINER_CLASS (gtk_button_parent_class)->add (container, child);
}
static void
gtk_button_remove (GtkContainer *container, GtkWidget *child)
{
GtkButton *button = GTK_BUTTON (container);
gtk_button_set_child_type (button, WIDGET_CHILD);
GTK_CONTAINER_CLASS (gtk_button_parent_class)->remove (container, child);
}
static void
gtk_button_unmap (GtkWidget *widget)
{
GtkButtonPrivate *priv = gtk_button_get_instance_private (GTK_BUTTON (widget));
priv->in_button = FALSE;
GTK_WIDGET_CLASS (gtk_button_parent_class)->unmap (widget);
}
static void
gtk_button_class_init (GtkButtonClass *klass)
{
GObjectClass *gobject_class;
GtkWidgetClass *widget_class;
GtkContainerClass *container_class;
gobject_class = G_OBJECT_CLASS (klass);
widget_class = (GtkWidgetClass*) klass;
container_class = GTK_CONTAINER_CLASS (klass);
gobject_class->dispose = gtk_button_dispose;
gobject_class->finalize = gtk_button_finalize;
gobject_class->set_property = gtk_button_set_property;
gobject_class->get_property = gtk_button_get_property;
widget_class->measure = gtk_button_measure_;
widget_class->display_changed = gtk_button_display_changed;
widget_class->unrealize = gtk_button_unrealize;
widget_class->key_release_event = gtk_button_key_release;
widget_class->state_flags_changed = gtk_button_state_flags_changed;
widget_class->grab_notify = gtk_button_grab_notify;
widget_class->unmap = gtk_button_unmap;
container_class->add = gtk_button_add;
container_class->remove = gtk_button_remove;
klass->clicked = NULL;
klass->activate = gtk_real_button_activate;
props[PROP_LABEL] =
g_param_spec_string ("label",
P_("Label"),
P_("Text of the label widget inside the button, if the button contains a label widget"),
NULL,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
props[PROP_USE_UNDERLINE] =
g_param_spec_boolean ("use-underline",
P_("Use underline"),
P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
FALSE,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
props[PROP_RELIEF] =
g_param_spec_enum ("relief",
P_("Border relief"),
P_("The border relief style"),
GTK_TYPE_RELIEF_STYLE,
GTK_RELIEF_NORMAL,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
props[PROP_ICON_NAME] =
g_param_spec_string ("icon-name",
P_("Icon Name"),
P_("The name of the icon used to automatically populate the button"),
NULL,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
g_object_class_install_properties (gobject_class, LAST_PROP, props);
g_object_class_override_property (gobject_class, PROP_ACTION_NAME, "action-name");
g_object_class_override_property (gobject_class, PROP_ACTION_TARGET, "action-target");
/**
* GtkButton::clicked:
* @button: the object that received the signal
*
* Emitted when the button has been activated (pressed and released).
*/
button_signals[CLICKED] =
g_signal_new (I_("clicked"),
G_OBJECT_CLASS_TYPE (gobject_class),
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GtkButtonClass, clicked),
NULL, NULL,
NULL,
G_TYPE_NONE, 0);
/**
* GtkButton::activate:
* @widget: the object which received the signal.
*
* The ::activate signal on GtkButton is an action signal and
* emitting it causes the button to animate press then release.
* Applications should never connect to this signal, but use the
* #GtkButton::clicked signal.
*/
button_signals[ACTIVATE] =
g_signal_new (I_("activate"),
G_OBJECT_CLASS_TYPE (gobject_class),
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GtkButtonClass, activate),
NULL, NULL,
NULL,
G_TYPE_NONE, 0);
widget_class->activate_signal = button_signals[ACTIVATE];
gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_BUTTON_ACCESSIBLE);
gtk_widget_class_set_css_name (widget_class, I_("button"));
}
static void
multipress_pressed_cb (GtkGestureMultiPress *gesture,
guint n_press,
gdouble x,
gdouble y,
GtkWidget *widget)
{
GtkButton *button = GTK_BUTTON (widget);
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
if (gtk_widget_get_focus_on_click (widget) && !gtk_widget_has_focus (widget))
gtk_widget_grab_focus (widget);
priv->in_button = TRUE;
if (!priv->activate_timeout)
{
priv->button_down = TRUE;
gtk_button_update_state (button);
}
gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_CLAIMED);
}
static gboolean
touch_release_in_button (GtkButton *button)
{
GdkEvent *event;
gdouble x, y;
event = gtk_get_current_event ();
if (!event)
return FALSE;
if (gdk_event_get_event_type (event) != GDK_TOUCH_END)
{
g_object_unref (event);
return FALSE;
}
gdk_event_get_coords (event, &x, &y);
g_object_unref (event);
if (gtk_widget_contains (GTK_WIDGET (button), x, y))
return TRUE;
return FALSE;
}
static void
multipress_released_cb (GtkGestureMultiPress *gesture,
guint n_press,
gdouble x,
gdouble y,
GtkWidget *widget)
{
GtkButton *button = GTK_BUTTON (widget);
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
GdkEventSequence *sequence;
gtk_button_do_release (button,
gtk_widget_is_sensitive (GTK_WIDGET (button)) &&
(priv->in_button ||
touch_release_in_button (button)));
sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
if (sequence)
{
priv->in_button = FALSE;
gtk_button_update_state (button);
}
}
static void
multipress_gesture_update_cb (GtkGesture *gesture,
GdkEventSequence *sequence,
GtkButton *button)
{
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
gboolean in_button;
gdouble x, y;
if (sequence != gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture)))
return;
gtk_gesture_get_point (gesture, sequence, &x, &y);
in_button = gtk_widget_contains (GTK_WIDGET (button), x, y);
if (priv->in_button != in_button)
{
priv->in_button = in_button;
gtk_button_update_state (button);
}
}
static void
multipress_gesture_cancel_cb (GtkGesture *gesture,
GdkEventSequence *sequence,
GtkButton *button)
{
gtk_button_do_release (button, FALSE);
}
static void
gtk_button_set_child_type (GtkButton *button, guint child_type)
{
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
if (priv->child_type == child_type)
return;
if (child_type != LABEL_CHILD)
g_object_notify_by_pspec (G_OBJECT (button), props[PROP_LABEL]);
else if (child_type != ICON_CHILD)
g_object_notify_by_pspec (G_OBJECT (button), props[PROP_ICON_NAME]);
priv->child_type = child_type;
}
static void
gtk_button_init (GtkButton *button)
{
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
gtk_widget_set_can_focus (GTK_WIDGET (button), TRUE);
gtk_widget_set_receives_default (GTK_WIDGET (button), TRUE);
gtk_widget_set_has_surface (GTK_WIDGET (button), FALSE);
priv->in_button = FALSE;
priv->button_down = FALSE;
priv->use_underline = FALSE;
priv->child_type = WIDGET_CHILD;
priv->gesture = gtk_gesture_multi_press_new (GTK_WIDGET (button));
gtk_gesture_single_set_touch_only (GTK_GESTURE_SINGLE (priv->gesture), FALSE);
gtk_gesture_single_set_exclusive (GTK_GESTURE_SINGLE (priv->gesture), TRUE);
gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (priv->gesture), GDK_BUTTON_PRIMARY);
g_signal_connect (priv->gesture, "pressed", G_CALLBACK (multipress_pressed_cb), button);
g_signal_connect (priv->gesture, "released", G_CALLBACK (multipress_released_cb), button);
g_signal_connect (priv->gesture, "update", G_CALLBACK (multipress_gesture_update_cb), button);
g_signal_connect (priv->gesture, "cancel", G_CALLBACK (multipress_gesture_cancel_cb), button);
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (priv->gesture), GTK_PHASE_CAPTURE);
}
static void
gtk_button_finalize (GObject *object)
{
GtkButton *button = GTK_BUTTON (object);
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
g_clear_object (&priv->gesture);
G_OBJECT_CLASS (gtk_button_parent_class)->finalize (object);
}
static void
gtk_button_dispose (GObject *object)
{
GtkButton *button = GTK_BUTTON (object);
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
g_clear_object (&priv->action_helper);
G_OBJECT_CLASS (gtk_button_parent_class)->dispose (object);
}
static void
gtk_button_set_action_name (GtkActionable *actionable,
const gchar *action_name)
{
GtkButton *button = GTK_BUTTON (actionable);
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
if (!priv->action_helper)
priv->action_helper = gtk_action_helper_new (actionable);
g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
if (action_name)
g_signal_connect_after (button, "clicked", G_CALLBACK (gtk_real_button_clicked), NULL);
gtk_action_helper_set_action_name (priv->action_helper, action_name);
}
static void
gtk_button_set_action_target_value (GtkActionable *actionable,
GVariant *action_target)
{
GtkButton *button = GTK_BUTTON (actionable);
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
if (!priv->action_helper)
priv->action_helper = gtk_action_helper_new (actionable);
gtk_action_helper_set_action_target_value (priv->action_helper, action_target);
}
static void
gtk_button_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkButton *button = GTK_BUTTON (object);
switch (prop_id)
{
case PROP_LABEL:
gtk_button_set_label (button, g_value_get_string (value));
break;
case PROP_RELIEF:
gtk_button_set_relief (button, g_value_get_enum (value));
break;
case PROP_USE_UNDERLINE:
gtk_button_set_use_underline (button, g_value_get_boolean (value));
break;
case PROP_ICON_NAME:
gtk_button_set_icon_name (button, g_value_get_string (value));
break;
case PROP_ACTION_NAME:
gtk_button_set_action_name (GTK_ACTIONABLE (button), g_value_get_string (value));
break;
case PROP_ACTION_TARGET:
gtk_button_set_action_target_value (GTK_ACTIONABLE (button), g_value_get_variant (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_button_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkButton *button = GTK_BUTTON (object);
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
switch (prop_id)
{
case PROP_LABEL:
g_value_set_string (value, gtk_button_get_label (button));
break;
case PROP_RELIEF:
g_value_set_enum (value, gtk_button_get_relief (button));
break;
case PROP_USE_UNDERLINE:
g_value_set_boolean (value, priv->use_underline);
break;
case PROP_ICON_NAME:
g_value_set_string (value, gtk_button_get_icon_name (button));
break;
case PROP_ACTION_NAME:
g_value_set_string (value, gtk_action_helper_get_action_name (priv->action_helper));
break;
case PROP_ACTION_TARGET:
g_value_set_variant (value, gtk_action_helper_get_action_target_value (priv->action_helper));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static const gchar *
gtk_button_get_action_name (GtkActionable *actionable)
{
GtkButton *button = GTK_BUTTON (actionable);
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
return gtk_action_helper_get_action_name (priv->action_helper);
}
static GVariant *
gtk_button_get_action_target_value (GtkActionable *actionable)
{
GtkButton *button = GTK_BUTTON (actionable);
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
return gtk_action_helper_get_action_target_value (priv->action_helper);
}
static void
gtk_button_actionable_iface_init (GtkActionableInterface *iface)
{
iface->get_action_name = gtk_button_get_action_name;
iface->set_action_name = gtk_button_set_action_name;
iface->get_action_target_value = gtk_button_get_action_target_value;
iface->set_action_target_value = gtk_button_set_action_target_value;
}
/**
* gtk_button_new:
*
* Creates a new #GtkButton widget. To add a child widget to the button,
* use gtk_container_add().
*
* Returns: The newly created #GtkButton widget.
*/
GtkWidget*
gtk_button_new (void)
{
return g_object_new (GTK_TYPE_BUTTON, NULL);
}
/**
* gtk_button_new_with_label:
* @label: The text you want the #GtkLabel to hold.
*
* Creates a #GtkButton widget with a #GtkLabel child containing the given
* text.
*
* Returns: The newly created #GtkButton widget.
*/
GtkWidget*
gtk_button_new_with_label (const gchar *label)
{
return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
}
/**
* gtk_button_new_from_icon_name:
* @icon_name: (nullable): an icon name or %NULL
*
* Creates a new button containing an icon from the current icon theme.
*
* If the icon name isn’t known, a “broken image” icon will be
* displayed instead. If the current icon theme is changed, the icon
* will be updated appropriately.
*
* Returns: a new #GtkButton displaying the themed icon
*/
GtkWidget*
gtk_button_new_from_icon_name (const gchar *icon_name)
{
GtkWidget *button;
button = g_object_new (GTK_TYPE_BUTTON,
"icon-name", icon_name,
NULL);
return button;
}
/**
* gtk_button_new_with_mnemonic:
* @label: The text of the button, with an underscore in front of the
* mnemonic character
*
* Creates a new #GtkButton containing a label.
* If characters in @label are preceded by an underscore, they are underlined.
* If you need a literal underscore character in a label, use “__” (two
* underscores). The first underlined character represents a keyboard
* accelerator called a mnemonic.
* Pressing Alt and that key activates the button.
*
* Returns: a new #GtkButton
*/
GtkWidget*
gtk_button_new_with_mnemonic (const gchar *label)
{
return g_object_new (GTK_TYPE_BUTTON, "label", label, "use-underline", TRUE, NULL);
}
/**
* gtk_button_clicked:
* @button: The #GtkButton you want to send the signal to.
*
* Emits a #GtkButton::clicked signal to the given #GtkButton.
*/
void
gtk_button_clicked (GtkButton *button)
{
g_return_if_fail (GTK_IS_BUTTON (button));
g_signal_emit (button, button_signals[CLICKED], 0);
}
/**
* gtk_button_set_relief:
* @button: The #GtkButton you want to set relief styles of
* @relief: The GtkReliefStyle as described above
*
* Sets the relief style of the edges of the given #GtkButton widget.
* Two styles exist, %GTK_RELIEF_NORMAL and %GTK_RELIEF_NONE.
* The default style is, as one can guess, %GTK_RELIEF_NORMAL.
*/
void
gtk_button_set_relief (GtkButton *button,
GtkReliefStyle relief)
{
GtkStyleContext *context;
GtkReliefStyle old_relief;
g_return_if_fail (GTK_IS_BUTTON (button));
old_relief = gtk_button_get_relief (button);
if (old_relief != relief)
{
context = gtk_widget_get_style_context (GTK_WIDGET (button));
if (relief == GTK_RELIEF_NONE)
gtk_style_context_add_class (context, GTK_STYLE_CLASS_FLAT);
else
gtk_style_context_remove_class (context, GTK_STYLE_CLASS_FLAT);
g_object_notify_by_pspec (G_OBJECT (button), props[PROP_RELIEF]);
}
}
/**
* gtk_button_get_relief:
* @button: The #GtkButton you want the #GtkReliefStyle from.
*
* Returns the current relief style of the given #GtkButton.
*
* Returns: The current #GtkReliefStyle
*/
GtkReliefStyle
gtk_button_get_relief (GtkButton *button)
{
GtkStyleContext *context;
g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
context = gtk_widget_get_style_context (GTK_WIDGET (button));
if (gtk_style_context_has_class (context, GTK_STYLE_CLASS_FLAT))
return GTK_RELIEF_NONE;
else
return GTK_RELIEF_NORMAL;
}
static void
gtk_button_unrealize (GtkWidget *widget)
{
GtkButton *button = GTK_BUTTON (widget);
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
if (priv->activate_timeout)
gtk_button_finish_activate (button, FALSE);
GTK_WIDGET_CLASS (gtk_button_parent_class)->unrealize (widget);
}
static void
gtk_button_do_release (GtkButton *button,
gboolean emit_clicked)
{
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
if (priv->button_down)
{
priv->button_down = FALSE;
if (priv->activate_timeout)
return;
if (emit_clicked)
gtk_button_clicked (button);
gtk_button_update_state (button);
}
}
static gboolean
gtk_button_key_release (GtkWidget *widget,
GdkEventKey *event)
{
GtkButton *button = GTK_BUTTON (widget);
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
if (priv->activate_timeout)
{
gtk_button_finish_activate (button, TRUE);
return TRUE;
}
else if (GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event)
return GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event (widget, event);
else
return FALSE;
}
static void
gtk_real_button_clicked (GtkButton *button)
{
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
if (priv->action_helper)
gtk_action_helper_activate (priv->action_helper);
}
static gboolean
button_activate_timeout (gpointer data)
{
gtk_button_finish_activate (data, TRUE);
return FALSE;
}
static void
gtk_real_button_activate (GtkButton *button)
{
GtkWidget *widget = GTK_WIDGET (button);
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
GdkDevice *device;
device = gtk_get_current_event_device ();
if (device && gdk_device_get_source (device) != GDK_SOURCE_KEYBOARD)
device = gdk_device_get_associated_device (device);
if (gtk_widget_get_realized (widget) && !priv->activate_timeout)
{
/* bgo#626336 - Only grab if we have a device (from an event), not if we
* were activated programmatically when no event is available.
*/
if (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
{
gtk_device_grab_add (widget, device, TRUE);
priv->grab_keyboard = device;
}
priv->activate_timeout = g_timeout_add (ACTIVATE_TIMEOUT, button_activate_timeout, button);
g_source_set_name_by_id (priv->activate_timeout, "[gtk+] button_activate_timeout");
priv->button_down = TRUE;
gtk_button_update_state (button);
}
}
static void
gtk_button_finish_activate (GtkButton *button,
gboolean do_it)
{
GtkWidget *widget = GTK_WIDGET (button);
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
g_source_remove (priv->activate_timeout);
priv->activate_timeout = 0;
if (priv->grab_keyboard)
{
gdk_seat_ungrab (gdk_device_get_seat (priv->grab_keyboard));
gtk_device_grab_remove (widget, priv->grab_keyboard);
priv->grab_keyboard = NULL;
}
priv->button_down = FALSE;
gtk_button_update_state (button);
if (do_it)
gtk_button_clicked (button);
}
static void
gtk_button_measure_ (GtkWidget *widget,
GtkOrientation orientation,
int for_size,
int *minimum,
int *natural,
int *minimum_baseline,
int *natural_baseline)
{
GtkWidget *child = gtk_bin_get_child (GTK_BIN (widget));
if (child && gtk_widget_get_visible (child))
{
gtk_widget_measure (child,
orientation,
for_size,
minimum, natural,
minimum_baseline, natural_baseline);
}
}
/**
* gtk_button_set_label:
* @button: a #GtkButton
* @label: a string
*
* Sets the text of the label of the button to @label.
*
* This will also clear any previously set labels.
*/
void
gtk_button_set_label (GtkButton *button,
const gchar *label)
{
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
GtkWidget *child;
GtkStyleContext *context;
g_return_if_fail (GTK_IS_BUTTON (button));
context = gtk_widget_get_style_context (GTK_WIDGET (button));
child = gtk_bin_get_child (GTK_BIN (button));
if (priv->child_type != LABEL_CHILD || child == NULL)
{
if (child != NULL)
gtk_container_remove (GTK_CONTAINER (button), child);
child = gtk_label_new (NULL);
if (priv->use_underline)
{
gtk_label_set_use_underline (GTK_LABEL (child), priv->use_underline);
gtk_label_set_mnemonic_widget (GTK_LABEL (child), GTK_WIDGET (button));
}
if (GTK_IS_CHECK_BUTTON (button))
{
gtk_label_set_xalign (GTK_LABEL (child), 0.0);
}
gtk_container_add (GTK_CONTAINER (button), child);
gtk_style_context_remove_class (context, "image-button");
gtk_style_context_add_class (context, "text-button");
}
gtk_label_set_label (GTK_LABEL (child), label);
gtk_button_set_child_type (button, LABEL_CHILD);
g_object_notify_by_pspec (G_OBJECT (button), props[PROP_LABEL]);
}
/**
* gtk_button_get_label:
* @button: a #GtkButton
*
* Fetches the text from the label of the button, as set by
* gtk_button_set_label(). If the label text has not
* been set the return value will be %NULL. This will be the
* case if you create an empty button with gtk_button_new() to
* use as a container.
*
* Returns: (nullable): The text of the label widget. This string is owned
* by the widget and must not be modified or freed.
*/
const gchar *
gtk_button_get_label (GtkButton *button)
{
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
if (priv->child_type == LABEL_CHILD)
{
GtkWidget *child = gtk_bin_get_child (GTK_BIN (button));
return gtk_label_get_label (GTK_LABEL (child));
}
return NULL;
}
/**
* gtk_button_set_use_underline:
* @button: a #GtkButton
* @use_underline: %TRUE if underlines in the text indicate mnemonics
*
* If true, an underline in the text of the button label indicates
* the next character should be used for the mnemonic accelerator key.
*/
void
gtk_button_set_use_underline (GtkButton *button,
gboolean use_underline)
{
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
g_return_if_fail (GTK_IS_BUTTON (button));
use_underline = use_underline != FALSE;
if (use_underline != priv->use_underline)
{
if (priv->child_type == LABEL_CHILD)
{
GtkWidget *child;
child = gtk_bin_get_child (GTK_BIN (button));
gtk_label_set_use_underline (GTK_LABEL (child), use_underline);
gtk_label_set_mnemonic_widget (GTK_LABEL (child), GTK_WIDGET (button));
}
priv->use_underline = use_underline;
g_object_notify_by_pspec (G_OBJECT (button), props[PROP_USE_UNDERLINE]);
}
}
/**
* gtk_button_get_use_underline:
* @button: a #GtkButton
*
* Returns whether an embedded underline in the button label indicates a
* mnemonic. See gtk_button_set_use_underline().
*
* Returns: %TRUE if an embedded underline in the button label
* indicates the mnemonic accelerator keys.
*/
gboolean
gtk_button_get_use_underline (GtkButton *button)
{
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
return priv->use_underline;
}
static void
gtk_button_update_state (GtkButton *button)
{
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
GtkStateFlags new_state;
gboolean depressed;
if (priv->activate_timeout)
depressed = TRUE;
else
depressed = priv->in_button && priv->button_down;
new_state = gtk_widget_get_state_flags (GTK_WIDGET (button)) & ~(GTK_STATE_FLAG_ACTIVE);
if (depressed)
new_state |= GTK_STATE_FLAG_ACTIVE;
gtk_widget_set_state_flags (GTK_WIDGET (button), new_state, TRUE);
}
static void
gtk_button_display_changed (GtkWidget *widget,
GdkDisplay *previous_display)
{
GtkButton *button = GTK_BUTTON (widget);
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
/* If the button is being pressed while the display changes the
release might never occur, so we reset the state. */
if (priv->button_down)
{
priv->button_down = FALSE;
gtk_button_update_state (button);
}
}
static void
gtk_button_state_flags_changed (GtkWidget *widget,
GtkStateFlags previous_state)
{
GtkButton *button = GTK_BUTTON (widget);
if (!gtk_widget_is_sensitive (widget))
gtk_button_do_release (button, FALSE);
}
static void
gtk_button_grab_notify (GtkWidget *widget,
gboolean was_grabbed)
{
GtkButton *button = GTK_BUTTON (widget);
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
if (priv->activate_timeout &&
priv->grab_keyboard &&
gtk_widget_device_is_shadowed (widget, priv->grab_keyboard))
gtk_button_finish_activate (button, FALSE);
if (!was_grabbed)
gtk_button_do_release (button, FALSE);
}
/**
* gtk_button_set_icon_name:
* @button: A #GtkButton
* @icon_name: A icon name
*
* Adds a #GtkImage with the given icon name as a child. The icon will be
* of size %GTK_ICON_SIZE_BUTTON. If @button already contains a child widget,
* that child widget will be removed and replaced with the image.
*/
void
gtk_button_set_icon_name (GtkButton *button,
const char *icon_name)
{
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
GtkWidget *child;
GtkStyleContext *context;
g_return_if_fail (GTK_IS_BUTTON (button));
g_return_if_fail (icon_name != NULL);
child = gtk_bin_get_child (GTK_BIN (button));
context = gtk_widget_get_style_context (GTK_WIDGET (button));
if (priv->child_type != ICON_CHILD || child == NULL)
{
if (child != NULL)
gtk_container_remove (GTK_CONTAINER (button), child);
child = gtk_image_new_from_icon_name (icon_name);
gtk_container_add (GTK_CONTAINER (button), child);
gtk_style_context_remove_class (context, "text-button");
gtk_style_context_add_class (context, "image-button");
}
else
{
gtk_image_set_from_icon_name (GTK_IMAGE (child), icon_name);
}
gtk_button_set_child_type (button, ICON_CHILD);
g_object_notify_by_pspec (G_OBJECT (button), props[PROP_ICON_NAME]);
}
/**
* gtk_button_get_icon_name:
* @button: A #GtkButton
*
* Returns the icon name set via gtk_button_set_icon_name().
*
* Returns: (nullable): The icon name set via gtk_button_set_icon_name()
*/
const char *
gtk_button_get_icon_name (GtkButton *button)
{
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
if (priv->child_type == ICON_CHILD)
{
GtkWidget *child = gtk_bin_get_child (GTK_BIN (button));
return gtk_image_get_icon_name (GTK_IMAGE (child));
}
return NULL;
}
GtkGesture *
gtk_button_get_gesture (GtkButton *button)
{
GtkButtonPrivate *priv = gtk_button_get_instance_private (button);
return priv->gesture;
}
|