]> Pileus Git - ~andy/gtk/blob - gtk/gtkbutton.c
button: Remove is_drawable() check
[~andy/gtk] / gtk / gtkbutton.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2001.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 /**
28  * SECTION:gtkbutton
29  * @Short_description: A widget that creates a signal when clicked on
30  * @Title: GtkButton
31  *
32  * The #GtkButton widget is generally used to attach a function to that is
33  * called when the button is pressed.  The various signals and how to use them
34  * are outlined below.
35  *
36  * The #GtkButton widget can hold any valid child widget.  That is it can hold
37  * most any other standard #GtkWidget.  The most commonly used child is the
38  * #GtkLabel.
39  */
40
41 #include "config.h"
42 #include <string.h>
43 #include "gtkalignment.h"
44 #include "gtkbutton.h"
45 #include "gtklabel.h"
46 #include "gtkmain.h"
47 #include "gtkmarshalers.h"
48 #include "gtkimage.h"
49 #include "gtkhbox.h"
50 #include "gtkvbox.h"
51 #include "gtkstock.h"
52 #include "gtkiconfactory.h"
53 #include "gtkactivatable.h"
54 #include "gtksizerequest.h"
55 #include "gtkprivate.h"
56 #include "gtkintl.h"
57
58
59 static const GtkBorder default_default_border = { 1, 1, 1, 1 };
60 static const GtkBorder default_default_outside_border = { 0, 0, 0, 0 };
61 static const GtkBorder default_inner_border = { 1, 1, 1, 1 };
62
63 /* Time out before giving up on getting a key release when animating
64  * the close button.
65  */
66 #define ACTIVATE_TIMEOUT 250
67
68 enum {
69   PRESSED,
70   RELEASED,
71   CLICKED,
72   ENTER,
73   LEAVE,
74   ACTIVATE,
75   LAST_SIGNAL
76 };
77
78 enum {
79   PROP_0,
80   PROP_LABEL,
81   PROP_IMAGE,
82   PROP_RELIEF,
83   PROP_USE_UNDERLINE,
84   PROP_USE_STOCK,
85   PROP_FOCUS_ON_CLICK,
86   PROP_XALIGN,
87   PROP_YALIGN,
88   PROP_IMAGE_POSITION,
89
90   /* activatable properties */
91   PROP_ACTIVATABLE_RELATED_ACTION,
92   PROP_ACTIVATABLE_USE_ACTION_APPEARANCE
93 };
94
95 #define GTK_BUTTON_GET_PRIVATE(o)       (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_BUTTON, GtkButtonPrivate))
96 typedef struct _GtkButtonPrivate GtkButtonPrivate;
97
98 struct _GtkButtonPrivate
99 {
100   gfloat          xalign;
101   gfloat          yalign;
102   GtkWidget      *image;
103   guint           align_set             : 1;
104   guint           image_is_stock        : 1;
105   guint           use_action_appearance : 1;
106   guint32         grab_time;
107   GdkDevice      *grab_keyboard;
108   GtkPositionType image_position;
109   GtkAction      *action;
110 };
111
112 static void gtk_button_destroy        (GtkObject          *object);
113 static void gtk_button_dispose        (GObject            *object);
114 static void gtk_button_set_property   (GObject            *object,
115                                        guint               prop_id,
116                                        const GValue       *value,
117                                        GParamSpec         *pspec);
118 static void gtk_button_get_property   (GObject            *object,
119                                        guint               prop_id,
120                                        GValue             *value,
121                                        GParamSpec         *pspec);
122 static void gtk_button_screen_changed (GtkWidget          *widget,
123                                        GdkScreen          *previous_screen);
124 static void gtk_button_realize (GtkWidget * widget);
125 static void gtk_button_unrealize (GtkWidget * widget);
126 static void gtk_button_map (GtkWidget * widget);
127 static void gtk_button_unmap (GtkWidget * widget);
128 static void gtk_button_style_set (GtkWidget * widget, GtkStyle * prev_style);
129 static void gtk_button_size_allocate (GtkWidget * widget,
130                                       GtkAllocation * allocation);
131 static gint gtk_button_expose (GtkWidget * widget, GdkEventExpose * event);
132 static gint gtk_button_button_press (GtkWidget * widget,
133                                      GdkEventButton * event);
134 static gint gtk_button_button_release (GtkWidget * widget,
135                                        GdkEventButton * event);
136 static gint gtk_button_grab_broken (GtkWidget * widget,
137                                     GdkEventGrabBroken * event);
138 static gint gtk_button_key_release (GtkWidget * widget, GdkEventKey * event);
139 static gint gtk_button_enter_notify (GtkWidget * widget,
140                                      GdkEventCrossing * event);
141 static gint gtk_button_leave_notify (GtkWidget * widget,
142                                      GdkEventCrossing * event);
143 static void gtk_real_button_pressed (GtkButton * button);
144 static void gtk_real_button_released (GtkButton * button);
145 static void gtk_real_button_clicked (GtkButton * button);
146 static void gtk_real_button_activate  (GtkButton          *button);
147 static void gtk_button_update_state   (GtkButton          *button);
148 static void gtk_button_add            (GtkContainer       *container,
149                                        GtkWidget          *widget);
150 static GType gtk_button_child_type    (GtkContainer       *container);
151 static void gtk_button_finish_activate (GtkButton         *button,
152                                         gboolean           do_it);
153
154 static GObject* gtk_button_constructor (GType                  type,
155                                         guint                  n_construct_properties,
156                                         GObjectConstructParam *construct_params);
157 static void gtk_button_construct_child (GtkButton             *button);
158 static void gtk_button_state_changed   (GtkWidget             *widget,
159                                         GtkStateType           previous_state);
160 static void gtk_button_grab_notify     (GtkWidget             *widget,
161                                         gboolean               was_grabbed);
162
163
164 static void gtk_button_activatable_interface_init(GtkActivatableIface  *iface);
165 static void gtk_button_update                    (GtkActivatable       *activatable,
166                                                   GtkAction            *action,
167                                                   const gchar          *property_name);
168 static void gtk_button_sync_action_properties    (GtkActivatable       *activatable,
169                                                   GtkAction            *action);
170 static void gtk_button_set_related_action        (GtkButton            *button,
171                                                   GtkAction            *action);
172 static void gtk_button_set_use_action_appearance (GtkButton            *button,
173                                                   gboolean              use_appearance);
174
175 static void gtk_button_size_request_init         (GtkSizeRequestIface *iface);
176 static void gtk_button_get_width                 (GtkSizeRequest      *widget,
177                                                   gint                *minimum_size,
178                                                   gint                *natural_size);
179 static void gtk_button_get_height                (GtkSizeRequest      *widget,
180                                                   gint                *minimum_size,
181                                                   gint                *natural_size);
182   
183 static guint button_signals[LAST_SIGNAL] = { 0 };
184
185 G_DEFINE_TYPE_WITH_CODE (GtkButton, gtk_button, GTK_TYPE_BIN,
186                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ACTIVATABLE,
187                                                 gtk_button_activatable_interface_init)
188                          G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
189                                                 gtk_button_size_request_init))
190
191 static void
192 gtk_button_class_init (GtkButtonClass *klass)
193 {
194   GObjectClass *gobject_class;
195   GtkObjectClass *object_class;
196   GtkWidgetClass *widget_class;
197   GtkContainerClass *container_class;
198
199   gobject_class = G_OBJECT_CLASS (klass);
200   object_class = (GtkObjectClass*) klass;
201   widget_class = (GtkWidgetClass*) klass;
202   container_class = (GtkContainerClass*) klass;
203   
204   gobject_class->constructor  = gtk_button_constructor;
205   gobject_class->dispose      = gtk_button_dispose;
206   gobject_class->set_property = gtk_button_set_property;
207   gobject_class->get_property = gtk_button_get_property;
208
209   object_class->destroy = gtk_button_destroy;
210
211   widget_class->screen_changed = gtk_button_screen_changed;
212   widget_class->realize = gtk_button_realize;
213   widget_class->unrealize = gtk_button_unrealize;
214   widget_class->map = gtk_button_map;
215   widget_class->unmap = gtk_button_unmap;
216   widget_class->style_set = gtk_button_style_set;
217   widget_class->size_allocate = gtk_button_size_allocate;
218   widget_class->expose_event = gtk_button_expose;
219   widget_class->button_press_event = gtk_button_button_press;
220   widget_class->button_release_event = gtk_button_button_release;
221   widget_class->grab_broken_event = gtk_button_grab_broken;
222   widget_class->key_release_event = gtk_button_key_release;
223   widget_class->enter_notify_event = gtk_button_enter_notify;
224   widget_class->leave_notify_event = gtk_button_leave_notify;
225   widget_class->state_changed = gtk_button_state_changed;
226   widget_class->grab_notify = gtk_button_grab_notify;
227
228   container_class->child_type = gtk_button_child_type;
229   container_class->add = gtk_button_add;
230   gtk_container_class_handle_border_width (container_class);
231
232   klass->pressed = gtk_real_button_pressed;
233   klass->released = gtk_real_button_released;
234   klass->clicked = NULL;
235   klass->enter = gtk_button_update_state;
236   klass->leave = gtk_button_update_state;
237   klass->activate = gtk_real_button_activate;
238
239   g_object_class_install_property (gobject_class,
240                                    PROP_LABEL,
241                                    g_param_spec_string ("label",
242                                                         P_("Label"),
243                                                         P_("Text of the label widget inside the button, if the button contains a label widget"),
244                                                         NULL,
245                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
246   
247   g_object_class_install_property (gobject_class,
248                                    PROP_USE_UNDERLINE,
249                                    g_param_spec_boolean ("use-underline",
250                                                          P_("Use underline"),
251                                                          P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
252                                                         FALSE,
253                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
254   
255   g_object_class_install_property (gobject_class,
256                                    PROP_USE_STOCK,
257                                    g_param_spec_boolean ("use-stock",
258                                                          P_("Use stock"),
259                                                          P_("If set, the label is used to pick a stock item instead of being displayed"),
260                                                         FALSE,
261                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
262   
263   g_object_class_install_property (gobject_class,
264                                    PROP_FOCUS_ON_CLICK,
265                                    g_param_spec_boolean ("focus-on-click",
266                                                          P_("Focus on click"),
267                                                          P_("Whether the button grabs focus when it is clicked with the mouse"),
268                                                          TRUE,
269                                                          GTK_PARAM_READWRITE));
270   
271   g_object_class_install_property (gobject_class,
272                                    PROP_RELIEF,
273                                    g_param_spec_enum ("relief",
274                                                       P_("Border relief"),
275                                                       P_("The border relief style"),
276                                                       GTK_TYPE_RELIEF_STYLE,
277                                                       GTK_RELIEF_NORMAL,
278                                                       GTK_PARAM_READWRITE));
279   
280   /**
281    * GtkButton:xalign:
282    *
283    * If the child of the button is a #GtkMisc or #GtkAlignment, this property 
284    * can be used to control it's horizontal alignment. 0.0 is left aligned, 
285    * 1.0 is right aligned.
286    *
287    * Since: 2.4
288    */
289   g_object_class_install_property (gobject_class,
290                                    PROP_XALIGN,
291                                    g_param_spec_float("xalign",
292                                                       P_("Horizontal alignment for child"),
293                                                       P_("Horizontal position of child in available space. 0.0 is left aligned, 1.0 is right aligned"),
294                                                       0.0,
295                                                       1.0,
296                                                       0.5,
297                                                       GTK_PARAM_READWRITE));
298
299   /**
300    * GtkButton:yalign:
301    *
302    * If the child of the button is a #GtkMisc or #GtkAlignment, this property 
303    * can be used to control it's vertical alignment. 0.0 is top aligned, 
304    * 1.0 is bottom aligned.
305    *
306    * Since: 2.4
307    */
308   g_object_class_install_property (gobject_class,
309                                    PROP_YALIGN,
310                                    g_param_spec_float("yalign",
311                                                       P_("Vertical alignment for child"),
312                                                       P_("Vertical position of child in available space. 0.0 is top aligned, 1.0 is bottom aligned"),
313                                                       0.0,
314                                                       1.0,
315                                                       0.5,
316                                                       GTK_PARAM_READWRITE));
317
318   /**
319    * GtkButton::image:
320    *
321    * The child widget to appear next to the button text.
322    *
323    * Since: 2.6
324    */
325   g_object_class_install_property (gobject_class,
326                                    PROP_IMAGE,
327                                    g_param_spec_object ("image",
328                                                         P_("Image widget"),
329                                                         P_("Child widget to appear next to the button text"),
330                                                         GTK_TYPE_WIDGET,
331                                                         GTK_PARAM_READWRITE));
332
333   /**
334    * GtkButton:image-position:
335    *
336    * The position of the image relative to the text inside the button.
337    *
338    * Since: 2.10
339    */
340   g_object_class_install_property (gobject_class,
341                                    PROP_IMAGE_POSITION,
342                                    g_param_spec_enum ("image-position",
343                                             P_("Image position"),
344                                                       P_("The position of the image relative to the text"),
345                                                       GTK_TYPE_POSITION_TYPE,
346                                                       GTK_POS_LEFT,
347                                                       GTK_PARAM_READWRITE));
348
349   g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_RELATED_ACTION, "related-action");
350   g_object_class_override_property (gobject_class, PROP_ACTIVATABLE_USE_ACTION_APPEARANCE, "use-action-appearance");
351
352   /**
353    * GtkButton::pressed:
354    * @button: the object that received the signal
355    *
356    * Emitted when the button is pressed.
357    *
358    * Deprecated: 2.8: Use the #GtkWidget::button-press-event signal.
359    */ 
360   button_signals[PRESSED] =
361     g_signal_new (I_("pressed"),
362                   G_OBJECT_CLASS_TYPE (object_class),
363                   G_SIGNAL_RUN_FIRST,
364                   G_STRUCT_OFFSET (GtkButtonClass, pressed),
365                   NULL, NULL,
366                   _gtk_marshal_VOID__VOID,
367                   G_TYPE_NONE, 0);
368
369   /**
370    * GtkButton::released:
371    * @button: the object that received the signal
372    *
373    * Emitted when the button is released.
374    *
375    * Deprecated: 2.8: Use the #GtkWidget::button-release-event signal.
376    */ 
377   button_signals[RELEASED] =
378     g_signal_new (I_("released"),
379                   G_OBJECT_CLASS_TYPE (object_class),
380                   G_SIGNAL_RUN_FIRST,
381                   G_STRUCT_OFFSET (GtkButtonClass, released),
382                   NULL, NULL,
383                   _gtk_marshal_VOID__VOID,
384                   G_TYPE_NONE, 0);
385
386   /**
387    * GtkButton::clicked:
388    * @button: the object that received the signal
389    *
390    * Emitted when the button has been activated (pressed and released).
391    */ 
392   button_signals[CLICKED] =
393     g_signal_new (I_("clicked"),
394                   G_OBJECT_CLASS_TYPE (object_class),
395                   G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
396                   G_STRUCT_OFFSET (GtkButtonClass, clicked),
397                   NULL, NULL,
398                   _gtk_marshal_VOID__VOID,
399                   G_TYPE_NONE, 0);
400
401   /**
402    * GtkButton::enter:
403    * @button: the object that received the signal
404    *
405    * Emitted when the pointer enters the button.
406    *
407    * Deprecated: 2.8: Use the #GtkWidget::enter-notify-event signal.
408    */ 
409   button_signals[ENTER] =
410     g_signal_new (I_("enter"),
411                   G_OBJECT_CLASS_TYPE (object_class),
412                   G_SIGNAL_RUN_FIRST,
413                   G_STRUCT_OFFSET (GtkButtonClass, enter),
414                   NULL, NULL,
415                   _gtk_marshal_VOID__VOID,
416                   G_TYPE_NONE, 0);
417
418   /**
419    * GtkButton::leave:
420    * @button: the object that received the signal
421    *
422    * Emitted when the pointer leaves the button.
423    *
424    * Deprecated: 2.8: Use the #GtkWidget::leave-notify-event signal.
425    */ 
426   button_signals[LEAVE] =
427     g_signal_new (I_("leave"),
428                   G_OBJECT_CLASS_TYPE (object_class),
429                   G_SIGNAL_RUN_FIRST,
430                   G_STRUCT_OFFSET (GtkButtonClass, leave),
431                   NULL, NULL,
432                   _gtk_marshal_VOID__VOID,
433                   G_TYPE_NONE, 0);
434
435   /**
436    * GtkButton::activate:
437    * @widget: the object which received the signal.
438    *
439    * The ::activate signal on GtkButton is an action signal and
440    * emitting it causes the button to animate press then release. 
441    * Applications should never connect to this signal, but use the
442    * #GtkButton::clicked signal.
443    */
444   button_signals[ACTIVATE] =
445     g_signal_new (I_("activate"),
446                   G_OBJECT_CLASS_TYPE (object_class),
447                   G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
448                   G_STRUCT_OFFSET (GtkButtonClass, activate),
449                   NULL, NULL,
450                   _gtk_marshal_VOID__VOID,
451                   G_TYPE_NONE, 0);
452   widget_class->activate_signal = button_signals[ACTIVATE];
453
454   /**
455    * GtkButton:default-border:
456    *
457    * The "default-border" style property defines the extra space to add
458    * around a button that can become the default widget of its window.
459    * For more information about default widgets, see gtk_widget_grab_default().
460    */
461
462   gtk_widget_class_install_style_property (widget_class,
463                                            g_param_spec_boxed ("default-border",
464                                                                P_("Default Spacing"),
465                                                                P_("Extra space to add for GTK_CAN_DEFAULT buttons"),
466                                                                GTK_TYPE_BORDER,
467                                                                GTK_PARAM_READABLE));
468
469   /**
470    * GtkButton:default-outside-border:
471    *
472    * The "default-outside-border" style property defines the extra outside
473    * space to add around a button that can become the default widget of its
474    * window. Extra outside space is always drawn outside the button border.
475    * For more information about default widgets, see gtk_widget_grab_default().
476    */
477   gtk_widget_class_install_style_property (widget_class,
478                                            g_param_spec_boxed ("default-outside-border",
479                                                                P_("Default Outside Spacing"),
480                                                                P_("Extra space to add for GTK_CAN_DEFAULT buttons that is always drawn outside the border"),
481                                                                GTK_TYPE_BORDER,
482                                                                GTK_PARAM_READABLE));
483   gtk_widget_class_install_style_property (widget_class,
484                                            g_param_spec_int ("child-displacement-x",
485                                                              P_("Child X Displacement"),
486                                                              P_("How far in the x direction to move the child when the button is depressed"),
487                                                              G_MININT,
488                                                              G_MAXINT,
489                                                              0,
490                                                              GTK_PARAM_READABLE));
491   gtk_widget_class_install_style_property (widget_class,
492                                            g_param_spec_int ("child-displacement-y",
493                                                              P_("Child Y Displacement"),
494                                                              P_("How far in the y direction to move the child when the button is depressed"),
495                                                              G_MININT,
496                                                              G_MAXINT,
497                                                              0,
498                                                              GTK_PARAM_READABLE));
499
500   /**
501    * GtkButton:displace-focus:
502    *
503    * Whether the child_displacement_x/child_displacement_y properties 
504    * should also affect the focus rectangle.
505    *
506    * Since: 2.6
507    */
508   gtk_widget_class_install_style_property (widget_class,
509                                            g_param_spec_boolean ("displace-focus",
510                                                                  P_("Displace focus"),
511                                                                  P_("Whether the child_displacement_x/_y properties should also affect the focus rectangle"),
512                                                                  FALSE,
513                                                                  GTK_PARAM_READABLE));
514
515   /**
516    * GtkButton:inner-border:
517    *
518    * Sets the border between the button edges and child.
519    *
520    * Since: 2.10
521    */
522   gtk_widget_class_install_style_property (widget_class,
523                                            g_param_spec_boxed ("inner-border",
524                                                                P_("Inner Border"),
525                                                                P_("Border between button edges and child."),
526                                                                GTK_TYPE_BORDER,
527                                                                GTK_PARAM_READABLE));
528
529   /**
530    * GtkButton::image-spacing:
531    *
532    * Spacing in pixels between the image and label.
533    *
534    * Since: 2.10
535    */
536   gtk_widget_class_install_style_property (widget_class,
537                                            g_param_spec_int ("image-spacing",
538                                                              P_("Image spacing"),
539                                                              P_("Spacing in pixels between the image and label"),
540                                                              0,
541                                                              G_MAXINT,
542                                                              2,
543                                                              GTK_PARAM_READABLE));
544
545   /**
546    * GtkSettings::gtk-button-images:
547    *
548    * Whether images should be shown on buttons
549    *
550    * Since: 2.4
551    */
552   gtk_settings_install_property (g_param_spec_boolean ("gtk-button-images",
553                                                        P_("Show button images"),
554                                                        P_("Whether images should be shown on buttons"),
555                                                        TRUE,
556                                                        GTK_PARAM_READWRITE));
557
558   g_type_class_add_private (gobject_class, sizeof (GtkButtonPrivate));
559 }
560
561 static void
562 gtk_button_init (GtkButton *button)
563 {
564   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
565
566   gtk_widget_set_can_focus (GTK_WIDGET (button), TRUE);
567   gtk_widget_set_receives_default (GTK_WIDGET (button), TRUE);
568   gtk_widget_set_has_window (GTK_WIDGET (button), FALSE);
569
570   button->label_text = NULL;
571   
572   button->constructed = FALSE;
573   button->in_button = FALSE;
574   button->button_down = FALSE;
575   button->relief = GTK_RELIEF_NORMAL;
576   button->use_stock = FALSE;
577   button->use_underline = FALSE;
578   button->depressed = FALSE;
579   button->depress_on_activate = TRUE;
580   button->focus_on_click = TRUE;
581
582   priv->xalign = 0.5;
583   priv->yalign = 0.5;
584   priv->align_set = 0;
585   priv->image_is_stock = TRUE;
586   priv->image_position = GTK_POS_LEFT;
587   priv->use_action_appearance = TRUE;
588 }
589
590 static void
591 gtk_button_destroy (GtkObject *object)
592 {
593   GtkButton *button = GTK_BUTTON (object);
594   
595   if (button->label_text)
596     {
597       g_free (button->label_text);
598       button->label_text = NULL;
599     }
600
601   GTK_OBJECT_CLASS (gtk_button_parent_class)->destroy (object);
602 }
603
604 static GObject*
605 gtk_button_constructor (GType                  type,
606                         guint                  n_construct_properties,
607                         GObjectConstructParam *construct_params)
608 {
609   GObject *object;
610   GtkButton *button;
611
612   object = G_OBJECT_CLASS (gtk_button_parent_class)->constructor (type,
613                                                                   n_construct_properties,
614                                                                   construct_params);
615
616   button = GTK_BUTTON (object);
617   button->constructed = TRUE;
618
619   if (button->label_text != NULL)
620     gtk_button_construct_child (button);
621   
622   return object;
623 }
624
625
626 static GType
627 gtk_button_child_type  (GtkContainer     *container)
628 {
629   if (!gtk_bin_get_child (GTK_BIN (container)))
630     return GTK_TYPE_WIDGET;
631   else
632     return G_TYPE_NONE;
633 }
634
635 static void
636 maybe_set_alignment (GtkButton *button,
637                      GtkWidget *widget)
638 {
639   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
640
641   if (GTK_IS_MISC (widget))
642     {
643       GtkMisc *misc = GTK_MISC (widget);
644       
645       if (priv->align_set)
646         gtk_misc_set_alignment (misc, priv->xalign, priv->yalign);
647     }
648   else if (GTK_IS_ALIGNMENT (widget))
649     {
650       GtkAlignment *alignment = GTK_ALIGNMENT (widget);
651       gfloat xscale, yscale;
652
653       g_object_get (alignment,
654                     "xscale", &xscale,
655                     "yscale", &yscale,
656                     NULL);
657
658       if (priv->align_set)
659         gtk_alignment_set (alignment,
660                            priv->xalign, priv->yalign,
661                            xscale, yscale);
662     }
663 }
664
665 static void
666 gtk_button_add (GtkContainer *container,
667                 GtkWidget    *widget)
668 {
669   maybe_set_alignment (GTK_BUTTON (container), widget);
670
671   GTK_CONTAINER_CLASS (gtk_button_parent_class)->add (container, widget);
672 }
673
674 static void 
675 gtk_button_dispose (GObject *object)
676 {
677   GtkButton *button = GTK_BUTTON (object);
678   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
679
680   if (priv->action)
681     {
682       gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), NULL);
683       priv->action = NULL;
684     }
685   G_OBJECT_CLASS (gtk_button_parent_class)->dispose (object);
686 }
687
688 static void
689 gtk_button_set_property (GObject         *object,
690                          guint            prop_id,
691                          const GValue    *value,
692                          GParamSpec      *pspec)
693 {
694   GtkButton *button = GTK_BUTTON (object);
695   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
696
697   switch (prop_id)
698     {
699     case PROP_LABEL:
700       gtk_button_set_label (button, g_value_get_string (value));
701       break;
702     case PROP_IMAGE:
703       gtk_button_set_image (button, (GtkWidget *) g_value_get_object (value));
704       break;
705     case PROP_RELIEF:
706       gtk_button_set_relief (button, g_value_get_enum (value));
707       break;
708     case PROP_USE_UNDERLINE:
709       gtk_button_set_use_underline (button, g_value_get_boolean (value));
710       break;
711     case PROP_USE_STOCK:
712       gtk_button_set_use_stock (button, g_value_get_boolean (value));
713       break;
714     case PROP_FOCUS_ON_CLICK:
715       gtk_button_set_focus_on_click (button, g_value_get_boolean (value));
716       break;
717     case PROP_XALIGN:
718       gtk_button_set_alignment (button, g_value_get_float (value), priv->yalign);
719       break;
720     case PROP_YALIGN:
721       gtk_button_set_alignment (button, priv->xalign, g_value_get_float (value));
722       break;
723     case PROP_IMAGE_POSITION:
724       gtk_button_set_image_position (button, g_value_get_enum (value));
725       break;
726     case PROP_ACTIVATABLE_RELATED_ACTION:
727       gtk_button_set_related_action (button, g_value_get_object (value));
728       break;
729     case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
730       gtk_button_set_use_action_appearance (button, g_value_get_boolean (value));
731       break;
732     default:
733       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
734       break;
735     }
736 }
737
738 static void
739 gtk_button_get_property (GObject         *object,
740                          guint            prop_id,
741                          GValue          *value,
742                          GParamSpec      *pspec)
743 {
744   GtkButton *button = GTK_BUTTON (object);
745   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
746
747   switch (prop_id)
748     {
749     case PROP_LABEL:
750       g_value_set_string (value, button->label_text);
751       break;
752     case PROP_IMAGE:
753       g_value_set_object (value, (GObject *)priv->image);
754       break;
755     case PROP_RELIEF:
756       g_value_set_enum (value, gtk_button_get_relief (button));
757       break;
758     case PROP_USE_UNDERLINE:
759       g_value_set_boolean (value, button->use_underline);
760       break;
761     case PROP_USE_STOCK:
762       g_value_set_boolean (value, button->use_stock);
763       break;
764     case PROP_FOCUS_ON_CLICK:
765       g_value_set_boolean (value, button->focus_on_click);
766       break;
767     case PROP_XALIGN:
768       g_value_set_float (value, priv->xalign);
769       break;
770     case PROP_YALIGN:
771       g_value_set_float (value, priv->yalign);
772       break;
773     case PROP_IMAGE_POSITION:
774       g_value_set_enum (value, priv->image_position);
775       break;
776     case PROP_ACTIVATABLE_RELATED_ACTION:
777       g_value_set_object (value, priv->action);
778       break;
779     case PROP_ACTIVATABLE_USE_ACTION_APPEARANCE:
780       g_value_set_boolean (value, priv->use_action_appearance);
781       break;
782     default:
783       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
784       break;
785     }
786 }
787
788 static void 
789 gtk_button_activatable_interface_init (GtkActivatableIface  *iface)
790 {
791   iface->update = gtk_button_update;
792   iface->sync_action_properties = gtk_button_sync_action_properties;
793 }
794
795 static void
796 activatable_update_stock_id (GtkButton *button,
797                              GtkAction *action)
798 {
799   if (!gtk_button_get_use_stock (button))
800     return;
801
802   gtk_button_set_label (button, gtk_action_get_stock_id (action));
803 }
804
805 static void
806 activatable_update_short_label (GtkButton *button,
807                                 GtkAction *action)
808 {
809   GtkWidget *child;
810   GtkWidget *image;
811
812   if (gtk_button_get_use_stock (button))
813     return;
814
815   image = gtk_button_get_image (button);
816
817   /* Dont touch custom child... */
818   child = gtk_bin_get_child (GTK_BIN (button));
819   if (GTK_IS_IMAGE (image) ||
820       child == NULL ||
821       GTK_IS_LABEL (child))
822     {
823       gtk_button_set_label (button, gtk_action_get_short_label (action));
824       gtk_button_set_use_underline (button, TRUE);
825     }
826 }
827
828 static void
829 activatable_update_icon_name (GtkButton *button,
830                               GtkAction *action)
831 {
832   GtkWidget *image;
833               
834   if (gtk_button_get_use_stock (button))
835     return;
836
837   image = gtk_button_get_image (button);
838
839   if (GTK_IS_IMAGE (image) &&
840       (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
841        gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_ICON_NAME))
842     gtk_image_set_from_icon_name (GTK_IMAGE (image),
843                                   gtk_action_get_icon_name (action), GTK_ICON_SIZE_MENU);
844 }
845
846 static void
847 activatable_update_gicon (GtkButton *button,
848                           GtkAction *action)
849 {
850   GtkWidget *image = gtk_button_get_image (button);
851   GIcon *icon = gtk_action_get_gicon (action);
852   
853   if (GTK_IS_IMAGE (image) &&
854       (gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_EMPTY ||
855        gtk_image_get_storage_type (GTK_IMAGE (image)) == GTK_IMAGE_GICON))
856     gtk_image_set_from_gicon (GTK_IMAGE (image), icon, GTK_ICON_SIZE_BUTTON);
857 }
858
859 static void 
860 gtk_button_update (GtkActivatable *activatable,
861                    GtkAction      *action,
862                    const gchar    *property_name)
863 {
864   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable);
865
866   if (strcmp (property_name, "visible") == 0)
867     {
868       if (gtk_action_is_visible (action))
869         gtk_widget_show (GTK_WIDGET (activatable));
870       else
871         gtk_widget_hide (GTK_WIDGET (activatable));
872     }
873   else if (strcmp (property_name, "sensitive") == 0)
874     gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
875
876   if (!priv->use_action_appearance)
877     return;
878
879   if (strcmp (property_name, "stock-id") == 0)
880     activatable_update_stock_id (GTK_BUTTON (activatable), action);
881   else if (strcmp (property_name, "gicon") == 0)
882     activatable_update_gicon (GTK_BUTTON (activatable), action);
883   else if (strcmp (property_name, "short-label") == 0)
884     activatable_update_short_label (GTK_BUTTON (activatable), action);
885   else if (strcmp (property_name, "icon-name") == 0)
886     activatable_update_icon_name (GTK_BUTTON (activatable), action);
887 }
888
889 static void
890 gtk_button_sync_action_properties (GtkActivatable *activatable,
891                                    GtkAction      *action)
892 {
893   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (activatable);
894
895   if (!action)
896     return;
897
898   if (gtk_action_is_visible (action))
899     gtk_widget_show (GTK_WIDGET (activatable));
900   else
901     gtk_widget_hide (GTK_WIDGET (activatable));
902   
903   gtk_widget_set_sensitive (GTK_WIDGET (activatable), gtk_action_is_sensitive (action));
904   
905   if (priv->use_action_appearance)
906     {
907       activatable_update_stock_id (GTK_BUTTON (activatable), action);
908       activatable_update_short_label (GTK_BUTTON (activatable), action);
909       activatable_update_gicon (GTK_BUTTON (activatable), action);
910       activatable_update_icon_name (GTK_BUTTON (activatable), action);
911     }
912 }
913
914 static void
915 gtk_button_set_related_action (GtkButton *button,
916                                GtkAction *action)
917 {
918   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
919
920   if (priv->action == action)
921     return;
922
923   /* This should be a default handler, but for compatibility reasons
924    * we need to support derived classes that don't chain up their
925    * clicked handler.
926    */
927   g_signal_handlers_disconnect_by_func (button, gtk_real_button_clicked, NULL);
928   if (action)
929     g_signal_connect_after (button, "clicked",
930                             G_CALLBACK (gtk_real_button_clicked), NULL);
931
932   gtk_activatable_do_set_related_action (GTK_ACTIVATABLE (button), action);
933
934   priv->action = action;
935 }
936
937 static void
938 gtk_button_set_use_action_appearance (GtkButton *button,
939                                       gboolean   use_appearance)
940 {
941   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
942
943   if (priv->use_action_appearance != use_appearance)
944     {
945       priv->use_action_appearance = use_appearance;
946
947       gtk_activatable_sync_action_properties (GTK_ACTIVATABLE (button), priv->action);
948     }
949 }
950
951 /**
952  * gtk_button_new:
953  *
954  * Creates a new #GtkButton widget. To add a child widget to the button,
955  * use gtk_container_add().
956  *
957  * Returns: The newly created #GtkButton widget.
958  */
959 GtkWidget*
960 gtk_button_new (void)
961 {
962   return g_object_new (GTK_TYPE_BUTTON, NULL);
963 }
964
965 static gboolean
966 show_image (GtkButton *button)
967 {
968   gboolean show;
969   
970   if (button->label_text)
971     {
972       GtkSettings *settings;
973
974       settings = gtk_widget_get_settings (GTK_WIDGET (button));        
975       g_object_get (settings, "gtk-button-images", &show, NULL);
976     }
977   else
978     show = TRUE;
979
980   return show;
981 }
982
983 static void
984 gtk_button_construct_child (GtkButton *button)
985 {
986   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
987   GtkStockItem item;
988   GtkWidget *child;
989   GtkWidget *label;
990   GtkWidget *box;
991   GtkWidget *align;
992   GtkWidget *image = NULL;
993   gchar *label_text = NULL;
994   gint image_spacing;
995
996   if (!button->constructed)
997     return;
998
999   if (!button->label_text && !priv->image)
1000     return;
1001
1002   gtk_widget_style_get (GTK_WIDGET (button),
1003                         "image-spacing", &image_spacing,
1004                         NULL);
1005
1006   if (priv->image && !priv->image_is_stock)
1007     {
1008       GtkWidget *parent;
1009
1010       image = g_object_ref (priv->image);
1011
1012       parent = gtk_widget_get_parent (image);
1013       if (parent)
1014         gtk_container_remove (GTK_CONTAINER (parent), image);
1015     }
1016
1017   priv->image = NULL;
1018
1019   child = gtk_bin_get_child (GTK_BIN (button));
1020   if (child)
1021     gtk_container_remove (GTK_CONTAINER (button), child);
1022
1023   if (button->use_stock &&
1024       button->label_text &&
1025       gtk_stock_lookup (button->label_text, &item))
1026     {
1027       if (!image)
1028         image = g_object_ref (gtk_image_new_from_stock (button->label_text, GTK_ICON_SIZE_BUTTON));
1029
1030       label_text = item.label;
1031     }
1032   else
1033     label_text = button->label_text;
1034
1035   if (image)
1036     {
1037       priv->image = image;
1038       g_object_set (priv->image,
1039                     "visible", show_image (button),
1040                     "no-show-all", TRUE,
1041                     NULL);
1042
1043       if (priv->image_position == GTK_POS_LEFT ||
1044           priv->image_position == GTK_POS_RIGHT)
1045         box = gtk_hbox_new (FALSE, image_spacing);
1046       else
1047         box = gtk_vbox_new (FALSE, image_spacing);
1048
1049       if (priv->align_set)
1050         align = gtk_alignment_new (priv->xalign, priv->yalign, 0.0, 0.0);
1051       else
1052         align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1053
1054       if (priv->image_position == GTK_POS_LEFT ||
1055           priv->image_position == GTK_POS_TOP)
1056         gtk_box_pack_start (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1057       else
1058         gtk_box_pack_end (GTK_BOX (box), priv->image, FALSE, FALSE, 0);
1059
1060       if (label_text)
1061         {
1062           if (button->use_underline || button->use_stock)
1063             {
1064               label = gtk_label_new_with_mnemonic (label_text);
1065               gtk_label_set_mnemonic_widget (GTK_LABEL (label),
1066                                              GTK_WIDGET (button));
1067             }
1068           else
1069             label = gtk_label_new (label_text);
1070
1071           if (priv->image_position == GTK_POS_RIGHT ||
1072               priv->image_position == GTK_POS_BOTTOM)
1073             gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
1074           else
1075             gtk_box_pack_end (GTK_BOX (box), label, FALSE, FALSE, 0);
1076         }
1077
1078       gtk_container_add (GTK_CONTAINER (button), align);
1079       gtk_container_add (GTK_CONTAINER (align), box);
1080       gtk_widget_show_all (align);
1081
1082       g_object_unref (image);
1083
1084       return;
1085     }
1086
1087   if (button->use_underline || button->use_stock)
1088     {
1089       label = gtk_label_new_with_mnemonic (button->label_text);
1090       gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (button));
1091     }
1092   else
1093     label = gtk_label_new (button->label_text);
1094
1095   if (priv->align_set)
1096     gtk_misc_set_alignment (GTK_MISC (label), priv->xalign, priv->yalign);
1097
1098   gtk_widget_show (label);
1099   gtk_container_add (GTK_CONTAINER (button), label);
1100 }
1101
1102
1103 /**
1104  * gtk_button_new_with_label:
1105  * @label: The text you want the #GtkLabel to hold.
1106  *
1107  * Creates a #GtkButton widget with a #GtkLabel child containing the given
1108  * text.
1109  *
1110  * Returns: The newly created #GtkButton widget.
1111  */
1112 GtkWidget*
1113 gtk_button_new_with_label (const gchar *label)
1114 {
1115   return g_object_new (GTK_TYPE_BUTTON, "label", label, NULL);
1116 }
1117
1118 /**
1119  * gtk_button_new_from_stock:
1120  * @stock_id: the name of the stock item 
1121  *
1122  * Creates a new #GtkButton containing the image and text from a stock item.
1123  * Some stock ids have preprocessor macros like #GTK_STOCK_OK and
1124  * #GTK_STOCK_APPLY.
1125  *
1126  * If @stock_id is unknown, then it will be treated as a mnemonic
1127  * label (as for gtk_button_new_with_mnemonic()).
1128  *
1129  * Returns: a new #GtkButton
1130  **/
1131 GtkWidget*
1132 gtk_button_new_from_stock (const gchar *stock_id)
1133 {
1134   return g_object_new (GTK_TYPE_BUTTON,
1135                        "label", stock_id,
1136                        "use-stock", TRUE,
1137                        "use-underline", TRUE,
1138                        NULL);
1139 }
1140
1141 /**
1142  * gtk_button_new_with_mnemonic:
1143  * @label: The text of the button, with an underscore in front of the
1144  *         mnemonic character
1145  * @returns: a new #GtkButton
1146  *
1147  * Creates a new #GtkButton containing a label.
1148  * If characters in @label are preceded by an underscore, they are underlined.
1149  * If you need a literal underscore character in a label, use '__' (two 
1150  * underscores). The first underlined character represents a keyboard 
1151  * accelerator called a mnemonic.
1152  * Pressing Alt and that key activates the button.
1153  **/
1154 GtkWidget*
1155 gtk_button_new_with_mnemonic (const gchar *label)
1156 {
1157   return g_object_new (GTK_TYPE_BUTTON, "label", label, "use-underline", TRUE,  NULL);
1158 }
1159
1160 /**
1161  * gtk_button_pressed:
1162  * @button: The #GtkButton you want to send the signal to.
1163  *
1164  * Emits a #GtkButton::pressed signal to the given #GtkButton.
1165  *
1166  * Deprecated: 2.20: Use the #GtkWidget::button-press-event signal.
1167  */
1168 void
1169 gtk_button_pressed (GtkButton *button)
1170 {
1171   g_return_if_fail (GTK_IS_BUTTON (button));
1172
1173   
1174   g_signal_emit (button, button_signals[PRESSED], 0);
1175 }
1176
1177 /**
1178  * gtk_button_released:
1179  * @button: The #GtkButton you want to send the signal to.
1180  *
1181  * Emits a #GtkButton::released signal to the given #GtkButton.
1182  *
1183  * Deprecated: 2.20: Use the #GtkWidget::button-release-event signal.
1184  */
1185 void
1186 gtk_button_released (GtkButton *button)
1187 {
1188   g_return_if_fail (GTK_IS_BUTTON (button));
1189
1190   g_signal_emit (button, button_signals[RELEASED], 0);
1191 }
1192
1193 /**
1194  * gtk_button_clicked:
1195  * @button: The #GtkButton you want to send the signal to.
1196  *
1197  * Emits a #GtkButton::clicked signal to the given #GtkButton.
1198  */
1199 void
1200 gtk_button_clicked (GtkButton *button)
1201 {
1202   g_return_if_fail (GTK_IS_BUTTON (button));
1203
1204   g_signal_emit (button, button_signals[CLICKED], 0);
1205 }
1206
1207 /**
1208  * gtk_button_enter:
1209  * @button: The #GtkButton you want to send the signal to.
1210  *
1211  * Emits a #GtkButton::enter signal to the given #GtkButton.
1212  *
1213  * Deprecated: 2.20: Use the #GtkWidget::enter-notify-event signal.
1214  */
1215 void
1216 gtk_button_enter (GtkButton *button)
1217 {
1218   g_return_if_fail (GTK_IS_BUTTON (button));
1219
1220   g_signal_emit (button, button_signals[ENTER], 0);
1221 }
1222
1223 /**
1224  * gtk_button_leave:
1225  * @button: The #GtkButton you want to send the signal to.
1226  *
1227  * Emits a #GtkButton::leave signal to the given #GtkButton.
1228  *
1229  * Deprecated: 2.20: Use the #GtkWidget::leave-notify-event signal.
1230  */
1231 void
1232 gtk_button_leave (GtkButton *button)
1233 {
1234   g_return_if_fail (GTK_IS_BUTTON (button));
1235
1236   g_signal_emit (button, button_signals[LEAVE], 0);
1237 }
1238
1239 /**
1240  * gtk_button_set_relief:
1241  * @button: The #GtkButton you want to set relief styles of.
1242  * @newstyle: The GtkReliefStyle as described above.
1243  *
1244  * Sets the relief style of the edges of the given #GtkButton widget.
1245  * Three styles exist, GTK_RELIEF_NORMAL, GTK_RELIEF_HALF, GTK_RELIEF_NONE.
1246  * The default style is, as one can guess, GTK_RELIEF_NORMAL.
1247  *
1248  * <!-- FIXME: put pictures of each style -->
1249  */
1250 void
1251 gtk_button_set_relief (GtkButton *button,
1252                        GtkReliefStyle newrelief)
1253 {
1254   g_return_if_fail (GTK_IS_BUTTON (button));
1255
1256   if (newrelief != button->relief) 
1257     {
1258        button->relief = newrelief;
1259        g_object_notify (G_OBJECT (button), "relief");
1260        gtk_widget_queue_draw (GTK_WIDGET (button));
1261     }
1262 }
1263
1264 /**
1265  * gtk_button_get_relief:
1266  * @button: The #GtkButton you want the #GtkReliefStyle from.
1267  *
1268  * Returns the current relief style of the given #GtkButton.
1269  *
1270  * Returns: The current #GtkReliefStyle
1271  */
1272 GtkReliefStyle
1273 gtk_button_get_relief (GtkButton *button)
1274 {
1275   g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_RELIEF_NORMAL);
1276
1277   return button->relief;
1278 }
1279
1280 static void
1281 gtk_button_realize (GtkWidget *widget)
1282 {
1283   GtkButton *button = GTK_BUTTON (widget);
1284   GtkAllocation allocation;
1285   GdkWindow *window;
1286   GdkWindowAttr attributes;
1287   gint attributes_mask;
1288
1289   gtk_widget_get_allocation (widget, &allocation);
1290
1291   gtk_widget_set_realized (widget, TRUE);
1292
1293   attributes.window_type = GDK_WINDOW_CHILD;
1294   attributes.x = allocation.x;
1295   attributes.y = allocation.y;
1296   attributes.width = allocation.width;
1297   attributes.height = allocation.height;
1298   attributes.wclass = GDK_INPUT_ONLY;
1299   attributes.event_mask = gtk_widget_get_events (widget);
1300   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
1301                             GDK_BUTTON_RELEASE_MASK |
1302                             GDK_ENTER_NOTIFY_MASK |
1303                             GDK_LEAVE_NOTIFY_MASK);
1304
1305   attributes_mask = GDK_WA_X | GDK_WA_Y;
1306
1307   window = gtk_widget_get_parent_window (widget);
1308   gtk_widget_set_window (widget, window);
1309   g_object_ref (window);
1310
1311   button->event_window = gdk_window_new (window,
1312                                          &attributes, attributes_mask);
1313   gdk_window_set_user_data (button->event_window, button);
1314
1315   gtk_widget_style_attach (widget);
1316 }
1317
1318 static void
1319 gtk_button_unrealize (GtkWidget *widget)
1320 {
1321   GtkButton *button = GTK_BUTTON (widget);
1322
1323   if (button->activate_timeout)
1324     gtk_button_finish_activate (button, FALSE);
1325
1326   if (button->event_window)
1327     {
1328       gdk_window_set_user_data (button->event_window, NULL);
1329       gdk_window_destroy (button->event_window);
1330       button->event_window = NULL;
1331     }
1332   
1333   GTK_WIDGET_CLASS (gtk_button_parent_class)->unrealize (widget);
1334 }
1335
1336 static void
1337 gtk_button_map (GtkWidget *widget)
1338 {
1339   GtkButton *button = GTK_BUTTON (widget);
1340   
1341   GTK_WIDGET_CLASS (gtk_button_parent_class)->map (widget);
1342
1343   if (button->event_window)
1344     gdk_window_show (button->event_window);
1345 }
1346
1347 static void
1348 gtk_button_unmap (GtkWidget *widget)
1349 {
1350   GtkButton *button = GTK_BUTTON (widget);
1351     
1352   if (button->event_window)
1353     gdk_window_hide (button->event_window);
1354
1355   GTK_WIDGET_CLASS (gtk_button_parent_class)->unmap (widget);
1356 }
1357
1358 static void
1359 gtk_button_update_image_spacing (GtkButton *button)
1360 {
1361   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1362   GtkWidget *child; 
1363   gint spacing;
1364
1365   /* Keep in sync with gtk_button_construct_child,
1366    * we only want to update the spacing if the box 
1367    * was constructed there.
1368    */
1369   if (!button->constructed || !priv->image)
1370     return;
1371
1372   child = gtk_bin_get_child (GTK_BIN (button));
1373   if (GTK_IS_ALIGNMENT (child))
1374     {
1375       child = gtk_bin_get_child (GTK_BIN (child));
1376       if (GTK_IS_BOX (child))
1377         {
1378           gtk_widget_style_get (GTK_WIDGET (button),
1379                                 "image-spacing", &spacing,
1380                                 NULL);
1381
1382           gtk_box_set_spacing (GTK_BOX (child), spacing);
1383         }
1384     }   
1385 }
1386
1387 static void
1388 gtk_button_style_set (GtkWidget *widget,
1389                       GtkStyle  *prev_style)
1390 {
1391   gtk_button_update_image_spacing (GTK_BUTTON (widget));
1392 }
1393
1394 static void
1395 gtk_button_get_props (GtkButton *button,
1396                       GtkBorder *default_border,
1397                       GtkBorder *default_outside_border,
1398                       GtkBorder *inner_border,
1399                       gboolean  *interior_focus)
1400 {
1401   GtkWidget *widget =  GTK_WIDGET (button);
1402   GtkBorder *tmp_border;
1403
1404   if (default_border)
1405     {
1406       gtk_widget_style_get (widget, "default-border", &tmp_border, NULL);
1407
1408       if (tmp_border)
1409         {
1410           *default_border = *tmp_border;
1411           gtk_border_free (tmp_border);
1412         }
1413       else
1414         *default_border = default_default_border;
1415     }
1416
1417   if (default_outside_border)
1418     {
1419       gtk_widget_style_get (widget, "default-outside-border", &tmp_border, NULL);
1420
1421       if (tmp_border)
1422         {
1423           *default_outside_border = *tmp_border;
1424           gtk_border_free (tmp_border);
1425         }
1426       else
1427         *default_outside_border = default_default_outside_border;
1428     }
1429
1430   if (inner_border)
1431     {
1432       gtk_widget_style_get (widget, "inner-border", &tmp_border, NULL);
1433
1434       if (tmp_border)
1435         {
1436           *inner_border = *tmp_border;
1437           gtk_border_free (tmp_border);
1438         }
1439       else
1440         *inner_border = default_inner_border;
1441     }
1442
1443   if (interior_focus)
1444     gtk_widget_style_get (widget, "interior-focus", interior_focus, NULL);
1445 }
1446
1447 static void
1448 gtk_button_size_allocate (GtkWidget     *widget,
1449                           GtkAllocation *allocation)
1450 {
1451   GtkButton *button = GTK_BUTTON (widget);
1452   GtkAllocation child_allocation;
1453   GtkStyle *style;
1454   GtkWidget *child;
1455
1456   gint xthickness, ythickness;
1457   GtkBorder default_border;
1458   GtkBorder inner_border;
1459   gint focus_width;
1460   gint focus_pad;
1461
1462   style = gtk_widget_get_style (widget);
1463   xthickness = style->xthickness;
1464   ythickness = style->ythickness;
1465
1466   gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1467   gtk_widget_style_get (GTK_WIDGET (widget),
1468                         "focus-line-width", &focus_width,
1469                         "focus-padding", &focus_pad,
1470                         NULL);
1471
1472   gtk_widget_set_allocation (widget, allocation);
1473
1474   if (gtk_widget_get_realized (widget))
1475     gdk_window_move_resize (button->event_window,
1476                             allocation->x,
1477                             allocation->y,
1478                             allocation->width,
1479                             allocation->height);
1480
1481   child = gtk_bin_get_child (GTK_BIN (button));
1482   if (child && gtk_widget_get_visible (child))
1483     {
1484       child_allocation.x = allocation->x + inner_border.left + xthickness;
1485       child_allocation.y = allocation->y + inner_border.top + ythickness;
1486
1487       child_allocation.width =
1488         allocation->width -
1489         xthickness * 2 -
1490         inner_border.left -
1491         inner_border.right;
1492
1493       child_allocation.height = 
1494         allocation->height -
1495         ythickness * 2 -
1496         inner_border.top -
1497         inner_border.bottom;
1498
1499       if (gtk_widget_get_can_default (GTK_WIDGET (button)))
1500         {
1501           child_allocation.x += default_border.left;
1502           child_allocation.y += default_border.top;
1503           child_allocation.width =  child_allocation.width - default_border.left - default_border.right;
1504           child_allocation.height = child_allocation.height - default_border.top - default_border.bottom;
1505         }
1506
1507       if (gtk_widget_get_can_focus (GTK_WIDGET (button)))
1508         {
1509           child_allocation.x += focus_width + focus_pad;
1510           child_allocation.y += focus_width + focus_pad;
1511           child_allocation.width =  child_allocation.width - (focus_width + focus_pad) * 2;
1512           child_allocation.height = child_allocation.height - (focus_width + focus_pad) * 2;
1513         }
1514
1515       if (button->depressed)
1516         {
1517           gint child_displacement_x;
1518           gint child_displacement_y;
1519           
1520           gtk_widget_style_get (widget,
1521                                 "child-displacement-x", &child_displacement_x, 
1522                                 "child-displacement-y", &child_displacement_y,
1523                                 NULL);
1524           child_allocation.x += child_displacement_x;
1525           child_allocation.y += child_displacement_y;
1526         }
1527
1528       child_allocation.width  = MAX (1, child_allocation.width);
1529       child_allocation.height = MAX (1, child_allocation.height);
1530
1531       gtk_widget_size_allocate (child, &child_allocation);
1532     }
1533 }
1534
1535 void
1536 _gtk_button_paint (GtkButton          *button,
1537                    const GdkRectangle *area,
1538                    GtkStateType        state_type,
1539                    GtkShadowType       shadow_type,
1540                    const gchar        *main_detail,
1541                    const gchar        *default_detail)
1542 {
1543   GtkWidget *widget;
1544   gint width, height;
1545   gint x, y;
1546   GtkBorder default_border;
1547   GtkBorder default_outside_border;
1548   gboolean interior_focus;
1549   gint focus_width;
1550   gint focus_pad;
1551   GtkAllocation allocation;
1552   GdkWindow *window;
1553   GtkStyle *style;
1554
1555   widget = GTK_WIDGET (button);
1556
1557   gtk_button_get_props (button, &default_border, &default_outside_border, NULL, &interior_focus);
1558   gtk_widget_style_get (widget,
1559                         "focus-line-width", &focus_width,
1560                         "focus-padding", &focus_pad,
1561                         NULL); 
1562
1563   gtk_widget_get_allocation (widget, &allocation);
1564   style = gtk_widget_get_style (widget);
1565   window = gtk_widget_get_window (widget);
1566
1567   x = allocation.x;
1568   y = allocation.y;
1569   width = allocation.width;
1570   height = allocation.height;
1571
1572   if (gtk_widget_has_default (widget) &&
1573       GTK_BUTTON (widget)->relief == GTK_RELIEF_NORMAL)
1574     {
1575       gtk_paint_box (style, window,
1576                      GTK_STATE_NORMAL, GTK_SHADOW_IN,
1577                      area, widget, "buttondefault",
1578                      x, y, width, height);
1579
1580       x += default_border.left;
1581       y += default_border.top;
1582       width -= default_border.left + default_border.right;
1583       height -= default_border.top + default_border.bottom;
1584     }
1585   else if (gtk_widget_get_can_default (widget))
1586     {
1587       x += default_outside_border.left;
1588       y += default_outside_border.top;
1589       width -= default_outside_border.left + default_outside_border.right;
1590       height -= default_outside_border.top + default_outside_border.bottom;
1591     }
1592    
1593   if (!interior_focus && gtk_widget_has_focus (widget))
1594     {
1595       x += focus_width + focus_pad;
1596       y += focus_width + focus_pad;
1597       width -= 2 * (focus_width + focus_pad);
1598       height -= 2 * (focus_width + focus_pad);
1599     }
1600
1601   if (button->relief != GTK_RELIEF_NONE || button->depressed ||
1602       gtk_widget_get_state(widget) == GTK_STATE_PRELIGHT)
1603     gtk_paint_box (style, window,
1604                    state_type,
1605                    shadow_type, area, widget, "button",
1606                    x, y, width, height);
1607    
1608   if (gtk_widget_has_focus (widget))
1609     {
1610       gint child_displacement_x;
1611       gint child_displacement_y;
1612       gboolean displace_focus;
1613       
1614       gtk_widget_style_get (widget,
1615                             "child-displacement-y", &child_displacement_y,
1616                             "child-displacement-x", &child_displacement_x,
1617                             "displace-focus", &displace_focus,
1618                             NULL);
1619
1620       if (interior_focus)
1621         {
1622           x += style->xthickness + focus_pad;
1623           y += style->ythickness + focus_pad;
1624           width -= 2 * (style->xthickness + focus_pad);
1625           height -=  2 * (style->ythickness + focus_pad);
1626         }
1627       else
1628         {
1629           x -= focus_width + focus_pad;
1630           y -= focus_width + focus_pad;
1631           width += 2 * (focus_width + focus_pad);
1632           height += 2 * (focus_width + focus_pad);
1633         }
1634
1635       if (button->depressed && displace_focus)
1636         {
1637           x += child_displacement_x;
1638           y += child_displacement_y;
1639         }
1640
1641       gtk_paint_focus (style, window,
1642                        gtk_widget_get_state (widget),
1643                        area, widget, "button",
1644                        x, y, width, height);
1645     }
1646 }
1647
1648 static gboolean
1649 gtk_button_expose (GtkWidget      *widget,
1650                    GdkEventExpose *event)
1651 {
1652   if (gtk_widget_is_drawable (widget))
1653     {
1654       GtkButton *button = GTK_BUTTON (widget);
1655       
1656       _gtk_button_paint (button, &event->area,
1657                          gtk_widget_get_state (widget),
1658                          button->depressed ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
1659                          "button", "buttondefault");
1660
1661       GTK_WIDGET_CLASS (gtk_button_parent_class)->expose_event (widget, event);
1662     }
1663
1664   return FALSE;
1665 }
1666
1667 static gboolean
1668 gtk_button_button_press (GtkWidget      *widget,
1669                          GdkEventButton *event)
1670 {
1671   GtkButton *button;
1672
1673   if (event->type == GDK_BUTTON_PRESS)
1674     {
1675       button = GTK_BUTTON (widget);
1676
1677       if (button->focus_on_click && !gtk_widget_has_focus (widget))
1678         gtk_widget_grab_focus (widget);
1679
1680       if (event->button == 1)
1681         gtk_button_pressed (button);
1682     }
1683
1684   return TRUE;
1685 }
1686
1687 static gboolean
1688 gtk_button_button_release (GtkWidget      *widget,
1689                            GdkEventButton *event)
1690 {
1691   GtkButton *button;
1692
1693   if (event->button == 1)
1694     {
1695       button = GTK_BUTTON (widget);
1696       gtk_button_released (button);
1697     }
1698
1699   return TRUE;
1700 }
1701
1702 static gboolean
1703 gtk_button_grab_broken (GtkWidget          *widget,
1704                         GdkEventGrabBroken *event)
1705 {
1706   GtkButton *button = GTK_BUTTON (widget);
1707   gboolean save_in;
1708   
1709   /* Simulate a button release without the pointer in the button */
1710   if (button->button_down)
1711     {
1712       save_in = button->in_button;
1713       button->in_button = FALSE;
1714       gtk_button_released (button);
1715       if (save_in != button->in_button)
1716         {
1717           button->in_button = save_in;
1718           gtk_button_update_state (button);
1719         }
1720     }
1721
1722   return TRUE;
1723 }
1724
1725 static gboolean
1726 gtk_button_key_release (GtkWidget   *widget,
1727                         GdkEventKey *event)
1728 {
1729   GtkButton *button = GTK_BUTTON (widget);
1730
1731   if (button->activate_timeout)
1732     {
1733       gtk_button_finish_activate (button, TRUE);
1734       return TRUE;
1735     }
1736   else if (GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event)
1737     return GTK_WIDGET_CLASS (gtk_button_parent_class)->key_release_event (widget, event);
1738   else
1739     return FALSE;
1740 }
1741
1742 static gboolean
1743 gtk_button_enter_notify (GtkWidget        *widget,
1744                          GdkEventCrossing *event)
1745 {
1746   GtkButton *button;
1747   GtkWidget *event_widget;
1748
1749   button = GTK_BUTTON (widget);
1750   event_widget = gtk_get_event_widget ((GdkEvent*) event);
1751
1752   if ((event_widget == widget) &&
1753       (event->detail != GDK_NOTIFY_INFERIOR))
1754     {
1755       button->in_button = TRUE;
1756       gtk_button_enter (button);
1757     }
1758
1759   return FALSE;
1760 }
1761
1762 static gboolean
1763 gtk_button_leave_notify (GtkWidget        *widget,
1764                          GdkEventCrossing *event)
1765 {
1766   GtkButton *button;
1767   GtkWidget *event_widget;
1768
1769   button = GTK_BUTTON (widget);
1770   event_widget = gtk_get_event_widget ((GdkEvent*) event);
1771
1772   if ((event_widget == widget) &&
1773       (event->detail != GDK_NOTIFY_INFERIOR) &&
1774       (gtk_widget_get_sensitive (event_widget)))
1775     {
1776       button->in_button = FALSE;
1777       gtk_button_leave (button);
1778     }
1779
1780   return FALSE;
1781 }
1782
1783 static void
1784 gtk_real_button_pressed (GtkButton *button)
1785 {
1786   if (button->activate_timeout)
1787     return;
1788   
1789   button->button_down = TRUE;
1790   gtk_button_update_state (button);
1791 }
1792
1793 static void
1794 gtk_real_button_released (GtkButton *button)
1795 {
1796   if (button->button_down)
1797     {
1798       button->button_down = FALSE;
1799
1800       if (button->activate_timeout)
1801         return;
1802       
1803       if (button->in_button)
1804         gtk_button_clicked (button);
1805
1806       gtk_button_update_state (button);
1807     }
1808 }
1809
1810 static void 
1811 gtk_real_button_clicked (GtkButton *button)
1812 {
1813   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
1814
1815   if (priv->action)
1816     gtk_action_activate (priv->action);
1817 }
1818
1819 static gboolean
1820 button_activate_timeout (gpointer data)
1821 {
1822   gtk_button_finish_activate (data, TRUE);
1823
1824   return FALSE;
1825 }
1826
1827 static void
1828 gtk_real_button_activate (GtkButton *button)
1829 {
1830   GtkWidget *widget = GTK_WIDGET (button);
1831   GtkButtonPrivate *priv;
1832   GdkDevice *device;
1833   guint32 time;
1834
1835   priv = GTK_BUTTON_GET_PRIVATE (button);
1836   device = gtk_get_current_event_device ();
1837
1838   if (device && device->source != GDK_SOURCE_KEYBOARD)
1839     device = gdk_device_get_associated_device (device);
1840
1841   g_return_if_fail (device && device->source == GDK_SOURCE_KEYBOARD);
1842
1843   if (gtk_widget_get_realized (widget) && !button->activate_timeout)
1844     {
1845       time = gtk_get_current_event_time ();
1846
1847       if (gdk_device_grab (device, button->event_window,
1848                            GDK_OWNERSHIP_WINDOW, TRUE,
1849                            GDK_KEY_PRESS | GDK_KEY_RELEASE,
1850                            NULL, time) == GDK_GRAB_SUCCESS)
1851         {
1852           gtk_device_grab_add (widget, device, TRUE);
1853           priv->grab_keyboard = device;
1854           priv->grab_time = time;
1855         }
1856
1857       button->activate_timeout = gdk_threads_add_timeout (ACTIVATE_TIMEOUT,
1858                                                 button_activate_timeout,
1859                                                 button);
1860       button->button_down = TRUE;
1861       gtk_button_update_state (button);
1862       gtk_widget_queue_draw (GTK_WIDGET (button));
1863     }
1864 }
1865
1866 static void
1867 gtk_button_finish_activate (GtkButton *button,
1868                             gboolean   do_it)
1869 {
1870   GtkWidget *widget = GTK_WIDGET (button);
1871   GtkButtonPrivate *priv;
1872   
1873   priv = GTK_BUTTON_GET_PRIVATE (button);
1874
1875   g_source_remove (button->activate_timeout);
1876   button->activate_timeout = 0;
1877
1878   if (priv->grab_keyboard)
1879     {
1880       gdk_device_ungrab (priv->grab_keyboard, priv->grab_time);
1881       gtk_device_grab_remove (widget, priv->grab_keyboard);
1882       priv->grab_keyboard = NULL;
1883     }
1884
1885   button->button_down = FALSE;
1886
1887   gtk_button_update_state (button);
1888   gtk_widget_queue_draw (GTK_WIDGET (button));
1889
1890   if (do_it)
1891     gtk_button_clicked (button);
1892 }
1893
1894
1895 static void
1896 gtk_button_size_request_init (GtkSizeRequestIface *iface)
1897 {
1898   iface->get_width  = gtk_button_get_width;
1899   iface->get_height = gtk_button_get_height;
1900 }
1901
1902 static void
1903 gtk_button_get_size (GtkSizeRequest *widget,
1904                      GtkOrientation  orientation,
1905                      gint           *minimum_size,
1906                      gint           *natural_size)
1907 {
1908   GtkButton *button = GTK_BUTTON (widget);
1909   GtkStyle *style;
1910   GtkWidget *child;
1911   GtkBorder default_border;
1912   GtkBorder inner_border;
1913   gint focus_width;
1914   gint focus_pad;
1915   gint minimum, natural;
1916
1917   gtk_button_get_props (button, &default_border, NULL, &inner_border, NULL);
1918   gtk_widget_style_get (GTK_WIDGET (widget),
1919                         "focus-line-width", &focus_width,
1920                         "focus-padding", &focus_pad,
1921                         NULL);
1922
1923   style = gtk_widget_get_style (GTK_WIDGET (widget));
1924
1925   if (orientation == GTK_ORIENTATION_HORIZONTAL)
1926     {
1927       minimum = (style->xthickness * 2 +
1928                  inner_border.left + inner_border.right);
1929
1930       if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1931         minimum += default_border.left + default_border.right;
1932     }
1933   else
1934     {
1935       minimum = (style->ythickness * 2 +
1936                  inner_border.top + inner_border.bottom);
1937
1938       if (gtk_widget_get_can_default (GTK_WIDGET (widget)))
1939         minimum += default_border.top + default_border.bottom;
1940     }  
1941
1942   minimum += 2 * (focus_width + focus_pad);
1943   natural = minimum;
1944
1945   if ((child = gtk_bin_get_child (GTK_BIN (button))) && 
1946       gtk_widget_get_visible (child))
1947     {
1948       gint child_min, child_nat;
1949
1950       if (orientation == GTK_ORIENTATION_HORIZONTAL)
1951         gtk_size_request_get_width (GTK_SIZE_REQUEST (child), 
1952                                     &child_min, &child_nat);
1953       else
1954         gtk_size_request_get_height (GTK_SIZE_REQUEST (child), 
1955                                      &child_min, &child_nat);
1956
1957       minimum += child_min;
1958       natural += child_nat;
1959     }
1960
1961   if (minimum_size)
1962     *minimum_size = minimum;
1963
1964   if (natural_size)
1965     *natural_size = natural;
1966 }
1967
1968 static void 
1969 gtk_button_get_width (GtkSizeRequest      *widget,
1970                       gint                *minimum_size,
1971                       gint                *natural_size)
1972 {
1973   gtk_button_get_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
1974 }
1975
1976 static void 
1977 gtk_button_get_height (GtkSizeRequest      *widget,
1978                        gint                *minimum_size,
1979                        gint                *natural_size)
1980 {
1981   gtk_button_get_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
1982 }
1983
1984 /**
1985  * gtk_button_set_label:
1986  * @button: a #GtkButton
1987  * @label: a string
1988  *
1989  * Sets the text of the label of the button to @str. This text is
1990  * also used to select the stock item if gtk_button_set_use_stock()
1991  * is used.
1992  *
1993  * This will also clear any previously set labels.
1994  **/
1995 void
1996 gtk_button_set_label (GtkButton   *button,
1997                       const gchar *label)
1998 {
1999   gchar *new_label;
2000   
2001   g_return_if_fail (GTK_IS_BUTTON (button));
2002
2003   new_label = g_strdup (label);
2004   g_free (button->label_text);
2005   button->label_text = new_label;
2006   
2007   gtk_button_construct_child (button);
2008   
2009   g_object_notify (G_OBJECT (button), "label");
2010 }
2011
2012 /**
2013  * gtk_button_get_label:
2014  * @button: a #GtkButton
2015  *
2016  * Fetches the text from the label of the button, as set by
2017  * gtk_button_set_label(). If the label text has not 
2018  * been set the return value will be %NULL. This will be the 
2019  * case if you create an empty button with gtk_button_new() to 
2020  * use as a container.
2021  *
2022  * Return value: The text of the label widget. This string is owned
2023  * by the widget and must not be modified or freed.
2024  **/
2025 G_CONST_RETURN gchar *
2026 gtk_button_get_label (GtkButton *button)
2027 {
2028   g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2029   
2030   return button->label_text;
2031 }
2032
2033 /**
2034  * gtk_button_set_use_underline:
2035  * @button: a #GtkButton
2036  * @use_underline: %TRUE if underlines in the text indicate mnemonics
2037  *
2038  * If true, an underline in the text of the button label indicates
2039  * the next character should be used for the mnemonic accelerator key.
2040  */
2041 void
2042 gtk_button_set_use_underline (GtkButton *button,
2043                               gboolean   use_underline)
2044 {
2045   g_return_if_fail (GTK_IS_BUTTON (button));
2046
2047   use_underline = use_underline != FALSE;
2048
2049   if (use_underline != button->use_underline)
2050     {
2051       button->use_underline = use_underline;
2052   
2053       gtk_button_construct_child (button);
2054       
2055       g_object_notify (G_OBJECT (button), "use-underline");
2056     }
2057 }
2058
2059 /**
2060  * gtk_button_get_use_underline:
2061  * @button: a #GtkButton
2062  *
2063  * Returns whether an embedded underline in the button label indicates a
2064  * mnemonic. See gtk_button_set_use_underline ().
2065  *
2066  * Return value: %TRUE if an embedded underline in the button label
2067  *               indicates the mnemonic accelerator keys.
2068  **/
2069 gboolean
2070 gtk_button_get_use_underline (GtkButton *button)
2071 {
2072   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2073   
2074   return button->use_underline;
2075 }
2076
2077 /**
2078  * gtk_button_set_use_stock:
2079  * @button: a #GtkButton
2080  * @use_stock: %TRUE if the button should use a stock item
2081  *
2082  * If %TRUE, the label set on the button is used as a
2083  * stock id to select the stock item for the button.
2084  */
2085 void
2086 gtk_button_set_use_stock (GtkButton *button,
2087                           gboolean   use_stock)
2088 {
2089   g_return_if_fail (GTK_IS_BUTTON (button));
2090
2091   use_stock = use_stock != FALSE;
2092
2093   if (use_stock != button->use_stock)
2094     {
2095       button->use_stock = use_stock;
2096   
2097       gtk_button_construct_child (button);
2098       
2099       g_object_notify (G_OBJECT (button), "use-stock");
2100     }
2101 }
2102
2103 /**
2104  * gtk_button_get_use_stock:
2105  * @button: a #GtkButton
2106  *
2107  * Returns whether the button label is a stock item.
2108  *
2109  * Return value: %TRUE if the button label is used to
2110  *               select a stock item instead of being
2111  *               used directly as the label text.
2112  */
2113 gboolean
2114 gtk_button_get_use_stock (GtkButton *button)
2115 {
2116   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2117   
2118   return button->use_stock;
2119 }
2120
2121 /**
2122  * gtk_button_set_focus_on_click:
2123  * @button: a #GtkButton
2124  * @focus_on_click: whether the button grabs focus when clicked with the mouse
2125  *
2126  * Sets whether the button will grab focus when it is clicked with the mouse.
2127  * Making mouse clicks not grab focus is useful in places like toolbars where
2128  * you don't want the keyboard focus removed from the main area of the
2129  * application.
2130  *
2131  * Since: 2.4
2132  **/
2133 void
2134 gtk_button_set_focus_on_click (GtkButton *button,
2135                                gboolean   focus_on_click)
2136 {
2137   g_return_if_fail (GTK_IS_BUTTON (button));
2138   
2139   focus_on_click = focus_on_click != FALSE;
2140
2141   if (button->focus_on_click != focus_on_click)
2142     {
2143       button->focus_on_click = focus_on_click;
2144       
2145       g_object_notify (G_OBJECT (button), "focus-on-click");
2146     }
2147 }
2148
2149 /**
2150  * gtk_button_get_focus_on_click:
2151  * @button: a #GtkButton
2152  *
2153  * Returns whether the button grabs focus when it is clicked with the mouse.
2154  * See gtk_button_set_focus_on_click().
2155  *
2156  * Return value: %TRUE if the button grabs focus when it is clicked with
2157  *               the mouse.
2158  *
2159  * Since: 2.4
2160  **/
2161 gboolean
2162 gtk_button_get_focus_on_click (GtkButton *button)
2163 {
2164   g_return_val_if_fail (GTK_IS_BUTTON (button), FALSE);
2165   
2166   return button->focus_on_click;
2167 }
2168
2169 /**
2170  * gtk_button_set_alignment:
2171  * @button: a #GtkButton
2172  * @xalign: the horizontal position of the child, 0.0 is left aligned, 
2173  *   1.0 is right aligned
2174  * @yalign: the vertical position of the child, 0.0 is top aligned, 
2175  *   1.0 is bottom aligned
2176  *
2177  * Sets the alignment of the child. This property has no effect unless 
2178  * the child is a #GtkMisc or a #GtkAligment.
2179  *
2180  * Since: 2.4
2181  */
2182 void
2183 gtk_button_set_alignment (GtkButton *button,
2184                           gfloat     xalign,
2185                           gfloat     yalign)
2186 {
2187   GtkButtonPrivate *priv;
2188
2189   g_return_if_fail (GTK_IS_BUTTON (button));
2190   
2191   priv = GTK_BUTTON_GET_PRIVATE (button);
2192
2193   priv->xalign = xalign;
2194   priv->yalign = yalign;
2195   priv->align_set = 1;
2196
2197   maybe_set_alignment (button, gtk_bin_get_child (GTK_BIN (button)));
2198
2199   g_object_freeze_notify (G_OBJECT (button));
2200   g_object_notify (G_OBJECT (button), "xalign");
2201   g_object_notify (G_OBJECT (button), "yalign");
2202   g_object_thaw_notify (G_OBJECT (button));
2203 }
2204
2205 /**
2206  * gtk_button_get_alignment:
2207  * @button: a #GtkButton
2208  * @xalign: return location for horizontal alignment
2209  * @yalign: return location for vertical alignment
2210  *
2211  * Gets the alignment of the child in the button.
2212  *
2213  * Since: 2.4
2214  */
2215 void
2216 gtk_button_get_alignment (GtkButton *button,
2217                           gfloat    *xalign,
2218                           gfloat    *yalign)
2219 {
2220   GtkButtonPrivate *priv;
2221
2222   g_return_if_fail (GTK_IS_BUTTON (button));
2223   
2224   priv = GTK_BUTTON_GET_PRIVATE (button);
2225  
2226   if (xalign) 
2227     *xalign = priv->xalign;
2228
2229   if (yalign)
2230     *yalign = priv->yalign;
2231 }
2232
2233 /**
2234  * _gtk_button_set_depressed:
2235  * @button: a #GtkButton
2236  * @depressed: %TRUE if the button should be drawn with a recessed shadow.
2237  *
2238  * Sets whether the button is currently drawn as down or not. This is 
2239  * purely a visual setting, and is meant only for use by derived widgets
2240  * such as #GtkToggleButton.
2241  **/
2242 void
2243 _gtk_button_set_depressed (GtkButton *button,
2244                            gboolean   depressed)
2245 {
2246   GtkWidget *widget = GTK_WIDGET (button);
2247
2248   depressed = depressed != FALSE;
2249
2250   if (depressed != button->depressed)
2251     {
2252       button->depressed = depressed;
2253       gtk_widget_queue_resize (widget);
2254     }
2255 }
2256
2257 static void
2258 gtk_button_update_state (GtkButton *button)
2259 {
2260   gboolean depressed;
2261   GtkStateType new_state;
2262
2263   if (button->activate_timeout)
2264     depressed = button->depress_on_activate;
2265   else
2266     depressed = button->in_button && button->button_down;
2267
2268   if (button->in_button && (!button->button_down || !depressed))
2269     new_state = GTK_STATE_PRELIGHT;
2270   else
2271     new_state = depressed ? GTK_STATE_ACTIVE : GTK_STATE_NORMAL;
2272
2273   _gtk_button_set_depressed (button, depressed); 
2274   gtk_widget_set_state (GTK_WIDGET (button), new_state);
2275 }
2276
2277 static void 
2278 show_image_change_notify (GtkButton *button)
2279 {
2280   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
2281
2282   if (priv->image) 
2283     {
2284       if (show_image (button))
2285         gtk_widget_show (priv->image);
2286       else
2287         gtk_widget_hide (priv->image);
2288     }
2289 }
2290
2291 static void
2292 traverse_container (GtkWidget *widget,
2293                     gpointer   data)
2294 {
2295   if (GTK_IS_BUTTON (widget))
2296     show_image_change_notify (GTK_BUTTON (widget));
2297   else if (GTK_IS_CONTAINER (widget))
2298     gtk_container_forall (GTK_CONTAINER (widget), traverse_container, NULL);
2299 }
2300
2301 static void
2302 gtk_button_setting_changed (GtkSettings *settings)
2303 {
2304   GList *list, *l;
2305
2306   list = gtk_window_list_toplevels ();
2307
2308   for (l = list; l; l = l->next)
2309     gtk_container_forall (GTK_CONTAINER (l->data), 
2310                           traverse_container, NULL);
2311
2312   g_list_free (list);
2313 }
2314
2315
2316 static void
2317 gtk_button_screen_changed (GtkWidget *widget,
2318                            GdkScreen *previous_screen)
2319 {
2320   GtkButton *button;
2321   GtkSettings *settings;
2322   guint show_image_connection;
2323
2324   if (!gtk_widget_has_screen (widget))
2325     return;
2326
2327   button = GTK_BUTTON (widget);
2328
2329   /* If the button is being pressed while the screen changes the
2330     release might never occur, so we reset the state. */
2331   if (button->button_down)
2332     {
2333       button->button_down = FALSE;
2334       gtk_button_update_state (button);
2335     }
2336
2337   settings = gtk_widget_get_settings (widget);
2338
2339   show_image_connection = 
2340     GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (settings), 
2341                                          "gtk-button-connection"));
2342   
2343   if (show_image_connection)
2344     return;
2345
2346   show_image_connection =
2347     g_signal_connect (settings, "notify::gtk-button-images",
2348                       G_CALLBACK (gtk_button_setting_changed), NULL);
2349   g_object_set_data (G_OBJECT (settings), 
2350                      I_("gtk-button-connection"),
2351                      GUINT_TO_POINTER (show_image_connection));
2352
2353   show_image_change_notify (button);
2354 }
2355
2356 static void
2357 gtk_button_state_changed (GtkWidget    *widget,
2358                           GtkStateType  previous_state)
2359 {
2360   GtkButton *button = GTK_BUTTON (widget);
2361
2362   if (!gtk_widget_is_sensitive (widget))
2363     {
2364       button->in_button = FALSE;
2365       gtk_real_button_released (button);
2366     }
2367 }
2368
2369 static void
2370 gtk_button_grab_notify (GtkWidget *widget,
2371                         gboolean   was_grabbed)
2372 {
2373   GtkButton *button = GTK_BUTTON (widget);
2374   GtkButtonPrivate *priv = GTK_BUTTON_GET_PRIVATE (button);
2375   gboolean save_in;
2376
2377   if (button->activate_timeout &&
2378       priv->grab_keyboard &&
2379       gtk_widget_device_is_shadowed (widget, priv->grab_keyboard))
2380     gtk_button_finish_activate (button, FALSE);
2381
2382   if (!was_grabbed)
2383     {
2384       save_in = button->in_button;
2385       button->in_button = FALSE; 
2386       gtk_real_button_released (button);
2387       if (save_in != button->in_button)
2388         {
2389           button->in_button = save_in;
2390           gtk_button_update_state (button);
2391         }
2392     }
2393 }
2394
2395 /**
2396  * gtk_button_set_image:
2397  * @button: a #GtkButton
2398  * @image: a widget to set as the image for the button
2399  *
2400  * Set the image of @button to the given widget. Note that
2401  * it depends on the #GtkSettings:gtk-button-images setting whether the
2402  * image will be displayed or not, you don't have to call
2403  * gtk_widget_show() on @image yourself.
2404  *
2405  * Since: 2.6
2406  */ 
2407 void
2408 gtk_button_set_image (GtkButton *button,
2409                       GtkWidget *image)
2410 {
2411   GtkButtonPrivate *priv;
2412   GtkWidget *parent;
2413
2414   g_return_if_fail (GTK_IS_BUTTON (button));
2415   g_return_if_fail (image == NULL || GTK_IS_WIDGET (image));
2416
2417   priv = GTK_BUTTON_GET_PRIVATE (button);
2418
2419   if (priv->image)
2420     {
2421       parent = gtk_widget_get_parent (priv->image);
2422       if (parent)
2423         gtk_container_remove (GTK_CONTAINER (parent), priv->image);
2424     }
2425
2426   priv->image = image;
2427   priv->image_is_stock = (image == NULL);
2428
2429   gtk_button_construct_child (button);
2430
2431   g_object_notify (G_OBJECT (button), "image");
2432 }
2433
2434 /**
2435  * gtk_button_get_image:
2436  * @button: a #GtkButton
2437  *
2438  * Gets the widget that is currenty set as the image of @button.
2439  * This may have been explicitly set by gtk_button_set_image()
2440  * or constructed by gtk_button_new_from_stock().
2441  *
2442  * Return value: (transfer none): a #GtkWidget or %NULL in case there is no image
2443  *
2444  * Since: 2.6
2445  */
2446 GtkWidget *
2447 gtk_button_get_image (GtkButton *button)
2448 {
2449   GtkButtonPrivate *priv;
2450
2451   g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2452
2453   priv = GTK_BUTTON_GET_PRIVATE (button);
2454   
2455   return priv->image;
2456 }
2457
2458 /**
2459  * gtk_button_set_image_position:
2460  * @button: a #GtkButton
2461  * @position: the position
2462  *
2463  * Sets the position of the image relative to the text 
2464  * inside the button.
2465  *
2466  * Since: 2.10
2467  */ 
2468 void
2469 gtk_button_set_image_position (GtkButton       *button,
2470                                GtkPositionType  position)
2471 {
2472
2473   GtkButtonPrivate *priv;
2474
2475   g_return_if_fail (GTK_IS_BUTTON (button));
2476   g_return_if_fail (position >= GTK_POS_LEFT && position <= GTK_POS_BOTTOM);
2477   
2478   priv = GTK_BUTTON_GET_PRIVATE (button);
2479
2480   if (priv->image_position != position)
2481     {
2482       priv->image_position = position;
2483
2484       gtk_button_construct_child (button);
2485
2486       g_object_notify (G_OBJECT (button), "image-position");
2487     }
2488 }
2489
2490 /**
2491  * gtk_button_get_image_position:
2492  * @button: a #GtkButton
2493  *
2494  * Gets the position of the image relative to the text 
2495  * inside the button.
2496  *
2497  * Return value: the position
2498  *
2499  * Since: 2.10
2500  */
2501 GtkPositionType
2502 gtk_button_get_image_position (GtkButton *button)
2503 {
2504   GtkButtonPrivate *priv;
2505
2506   g_return_val_if_fail (GTK_IS_BUTTON (button), GTK_POS_LEFT);
2507
2508   priv = GTK_BUTTON_GET_PRIVATE (button);
2509   
2510   return priv->image_position;
2511 }
2512
2513
2514 /**
2515  * gtk_button_get_event_window:
2516  * @button: a #GtkButton
2517  *
2518  * Returns the button's event window if it is realized, %NULL otherwise.
2519  * This function should be rarely needed.
2520  *
2521  * Return value: (transfer none): @button's event window.
2522  *
2523  * Since: 2.22
2524  */
2525 GdkWindow*
2526 gtk_button_get_event_window (GtkButton *button)
2527 {
2528   g_return_val_if_fail (GTK_IS_BUTTON (button), NULL);
2529
2530   return button->event_window;
2531 }