]> Pileus Git - ~andy/gtk/blob - gtk/gtktoolbutton.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtktoolbutton.c
1 /* gtktoolbutton.c
2  *
3  * Copyright (C) 2002 Anders Carlsson <andersca@gnome.org>
4  * Copyright (C) 2002 James Henstridge <james@daa.com.au>
5  * Copyright (C) 2003 Soeren Sandmann <sandmann@daimi.au.dk>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "config.h"
22 #include "gtktoolbutton.h"
23 #include "gtkbutton.h"
24 #include "gtkiconfactory.h"
25 #include "gtkimage.h"
26 #include "gtkimagemenuitem.h"
27 #include "gtklabel.h"
28 #include "gtkstock.h"
29 #include "gtkbox.h"
30 #include "gtkintl.h"
31 #include "gtktoolbar.h"
32 #include "gtkactivatable.h"
33 #include "gtkactionable.h"
34 #include "gtkprivate.h"
35
36 #include <string.h>
37
38
39 /**
40  * SECTION:gtktoolbutton
41  * @Short_description: A GtkToolItem subclass that displays buttons
42  * @Title: GtkToolButton
43  * @See_also: #GtkToolbar, #GtkMenuToolButton, #GtkToggleToolButton,
44  *   #GtkRadioToolButton, #GtkSeparatorToolItem
45  *
46  * #GtkToolButton<!-- -->s are #GtkToolItems containing buttons.
47  *
48  * Use gtk_tool_button_new() to create a new #GtkToolButton. Use
49  * gtk_tool_button_new_from_stock() to create a #GtkToolButton
50  * containing a stock item.
51  *
52  * The label of a #GtkToolButton is determined by the properties
53  * #GtkToolButton:label-widget, #GtkToolButton:label, and
54  * #GtkToolButton:stock-id. If #GtkToolButton:label-widget is
55  * non-%NULL, then that widget is used as the label. Otherwise, if
56  * #GtkToolButton:label is non-%NULL, that string is used as the label.
57  * Otherwise, if #GtkToolButton:stock-id is non-%NULL, the label is
58  * determined by the stock item. Otherwise, the button does not have a label.
59  *
60  * The icon of a #GtkToolButton is determined by the properties
61  * #GtkToolButton:icon-widget and #GtkToolButton:stock-id. If
62  * #GtkToolButton:icon-widget is non-%NULL, then
63  * that widget is used as the icon. Otherwise, if #GtkToolButton:stock-id is
64  * non-%NULL, the icon is determined by the stock item. Otherwise,
65  * the button does not have a icon.
66  */
67
68
69 #define MENU_ID "gtk-tool-button-menu-id"
70
71 enum {
72   CLICKED,
73   LAST_SIGNAL
74 };
75
76 enum {
77   PROP_0,
78   PROP_LABEL,
79   PROP_USE_UNDERLINE,
80   PROP_LABEL_WIDGET,
81   PROP_STOCK_ID,
82   PROP_ICON_NAME,
83   PROP_ICON_WIDGET,
84   PROP_ACTION_NAME,
85   PROP_ACTION_TARGET
86 };
87
88 static void gtk_tool_button_init          (GtkToolButton      *button,
89                                            GtkToolButtonClass *klass);
90 static void gtk_tool_button_class_init    (GtkToolButtonClass *klass);
91 static void gtk_tool_button_set_property  (GObject            *object,
92                                            guint               prop_id,
93                                            const GValue       *value,
94                                            GParamSpec         *pspec);
95 static void gtk_tool_button_get_property  (GObject            *object,
96                                            guint               prop_id,
97                                            GValue             *value,
98                                            GParamSpec         *pspec);
99 static void gtk_tool_button_property_notify (GObject          *object,
100                                              GParamSpec       *pspec);
101 static void gtk_tool_button_finalize      (GObject            *object);
102
103 static void gtk_tool_button_toolbar_reconfigured (GtkToolItem *tool_item);
104 static gboolean   gtk_tool_button_create_menu_proxy (GtkToolItem     *item);
105 static void       button_clicked                    (GtkWidget       *widget,
106                                                      GtkToolButton   *button);
107 static void gtk_tool_button_style_updated  (GtkWidget          *widget);
108
109 static void gtk_tool_button_construct_contents (GtkToolItem *tool_item);
110
111 static void gtk_tool_button_actionable_iface_init      (GtkActionableInterface *iface);
112 static void gtk_tool_button_activatable_interface_init (GtkActivatableIface  *iface);
113 static void gtk_tool_button_update                     (GtkActivatable       *activatable,
114                                                         GtkAction            *action,
115                                                         const gchar          *property_name);
116 static void gtk_tool_button_sync_action_properties     (GtkActivatable       *activatable,
117                                                         GtkAction            *action);
118
119
120 struct _GtkToolButtonPrivate
121 {
122   GtkWidget *button;
123
124   gchar *stock_id;
125   gchar *icon_name;
126   gchar *label_text;
127   GtkWidget *label_widget;
128   GtkWidget *icon_widget;
129
130   GtkSizeGroup *text_size_group;
131
132   guint use_underline : 1;
133   guint contents_invalid : 1;
134 };
135
136 static GObjectClass        *parent_class = NULL;
137 static GtkActivatableIface *parent_activatable_iface;
138 static guint                toolbutton_signals[LAST_SIGNAL] = { 0 };
139
140 GType
141 gtk_tool_button_get_type (void)
142 {
143   static GType type = 0;
144   
145   if (!type)
146     {
147       const GInterfaceInfo actionable_info =
148       {
149         (GInterfaceInitFunc) gtk_tool_button_actionable_iface_init,
150         (GInterfaceFinalizeFunc) NULL,
151         NULL
152       };
153       const GInterfaceInfo activatable_info =
154       {
155         (GInterfaceInitFunc) gtk_tool_button_activatable_interface_init,
156         (GInterfaceFinalizeFunc) NULL,
157         NULL
158       };
159
160       type = g_type_register_static_simple (GTK_TYPE_TOOL_ITEM,
161                                             I_("GtkToolButton"),
162                                             sizeof (GtkToolButtonClass),
163                                             (GClassInitFunc) gtk_tool_button_class_init,
164                                             sizeof (GtkToolButton),
165                                             (GInstanceInitFunc) gtk_tool_button_init,
166                                             0);
167
168       g_type_add_interface_static (type, GTK_TYPE_ACTIONABLE, &actionable_info);
169       g_type_add_interface_static (type, GTK_TYPE_ACTIVATABLE,
170                                    &activatable_info);
171     }
172   return type;
173 }
174
175 static void
176 gtk_tool_button_class_init (GtkToolButtonClass *klass)
177 {
178   GObjectClass *object_class;
179   GtkWidgetClass *widget_class;
180   GtkToolItemClass *tool_item_class;
181   
182   parent_class = g_type_class_peek_parent (klass);
183   
184   object_class = (GObjectClass *)klass;
185   widget_class = (GtkWidgetClass *)klass;
186   tool_item_class = (GtkToolItemClass *)klass;
187   
188   object_class->set_property = gtk_tool_button_set_property;
189   object_class->get_property = gtk_tool_button_get_property;
190   object_class->notify = gtk_tool_button_property_notify;
191   object_class->finalize = gtk_tool_button_finalize;
192
193   widget_class->style_updated = gtk_tool_button_style_updated;
194
195   tool_item_class->create_menu_proxy = gtk_tool_button_create_menu_proxy;
196   tool_item_class->toolbar_reconfigured = gtk_tool_button_toolbar_reconfigured;
197   
198   klass->button_type = GTK_TYPE_BUTTON;
199
200   /* Properties are interpreted like this:
201    *
202    *          - if the tool button has an icon_widget, then that widget
203    *            will be used as the icon. Otherwise, if the tool button
204    *            has a stock id, the corresponding stock icon will be
205    *            used. Otherwise, if the tool button has an icon name,
206    *            the corresponding icon from the theme will be used.
207    *            Otherwise, the tool button will not have an icon.
208    *
209    *          - if the tool button has a label_widget then that widget
210    *            will be used as the label. Otherwise, if the tool button
211    *            has a label text, that text will be used as label. Otherwise,
212    *            if the toolbutton has a stock id, the corresponding text
213    *            will be used as label. Otherwise, if the tool button has
214    *            an icon name, the corresponding icon name from the theme will
215    *            be used. Otherwise, the toolbutton will have an empty label.
216    *
217    *          - The use_underline property only has an effect when the label
218    *            on the toolbutton comes from the label property (ie. not from
219    *            label_widget or from stock_id).
220    *
221    *            In that case, if use_underline is set,
222    *
223    *                    - underscores are removed from the label text before
224    *                      the label is shown on the toolbutton unless the
225    *                      underscore is followed by another underscore
226    *
227    *                    - an underscore indicates that the next character when
228    *                      used in the overflow menu should be used as a
229    *                      mnemonic.
230    *
231    *            In short: use_underline = TRUE means that the label text has
232    *            the form "_Open" and the toolbar should take appropriate
233    *            action.
234    */
235
236   g_object_class_install_property (object_class,
237                                    PROP_LABEL,
238                                    g_param_spec_string ("label",
239                                                         P_("Label"),
240                                                         P_("Text to show in the item."),
241                                                         NULL,
242                                                         GTK_PARAM_READWRITE));
243   g_object_class_install_property (object_class,
244                                    PROP_USE_UNDERLINE,
245                                    g_param_spec_boolean ("use-underline",
246                                                          P_("Use underline"),
247                                                          P_("If set, an underline in the label property indicates that the next character should be used for the mnemonic accelerator key in the overflow menu"),
248                                                          FALSE,
249                                                          GTK_PARAM_READWRITE));
250   g_object_class_install_property (object_class,
251                                    PROP_LABEL_WIDGET,
252                                    g_param_spec_object ("label-widget",
253                                                         P_("Label widget"),
254                                                         P_("Widget to use as the item label"),
255                                                         GTK_TYPE_WIDGET,
256                                                         GTK_PARAM_READWRITE));
257   g_object_class_install_property (object_class,
258                                    PROP_STOCK_ID,
259                                    g_param_spec_string ("stock-id",
260                                                         P_("Stock Id"),
261                                                         P_("The stock icon displayed on the item"),
262                                                         NULL,
263                                                         GTK_PARAM_READWRITE));
264
265   /**
266    * GtkToolButton:icon-name:
267    * 
268    * The name of the themed icon displayed on the item.
269    * This property only has an effect if not overridden by "label", 
270    * "icon_widget" or "stock_id" properties.
271    *
272    * Since: 2.8 
273    */
274   g_object_class_install_property (object_class,
275                                    PROP_ICON_NAME,
276                                    g_param_spec_string ("icon-name",
277                                                         P_("Icon name"),
278                                                         P_("The name of the themed icon displayed on the item"),
279                                                         NULL,
280                                                         GTK_PARAM_READWRITE));
281   g_object_class_install_property (object_class,
282                                    PROP_ICON_WIDGET,
283                                    g_param_spec_object ("icon-widget",
284                                                         P_("Icon widget"),
285                                                         P_("Icon widget to display in the item"),
286                                                         GTK_TYPE_WIDGET,
287                                                         GTK_PARAM_READWRITE));
288
289   g_object_class_override_property (object_class, PROP_ACTION_NAME, "action-name");
290   g_object_class_override_property (object_class, PROP_ACTION_TARGET, "action-target");
291
292   /**
293    * GtkButton:icon-spacing:
294    * 
295    * Spacing in pixels between the icon and label.
296    * 
297    * Since: 2.10
298    */
299   gtk_widget_class_install_style_property (widget_class,
300                                            g_param_spec_int ("icon-spacing",
301                                                              P_("Icon spacing"),
302                                                              P_("Spacing in pixels between the icon and label"),
303                                                              0,
304                                                              G_MAXINT,
305                                                              3,
306                                                              GTK_PARAM_READWRITE));
307
308 /**
309  * GtkToolButton::clicked:
310  * @toolbutton: the object that emitted the signal
311  *
312  * This signal is emitted when the tool button is clicked with the mouse
313  * or activated with the keyboard.
314  **/
315   toolbutton_signals[CLICKED] =
316     g_signal_new (I_("clicked"),
317                   G_OBJECT_CLASS_TYPE (klass),
318                   G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
319                   G_STRUCT_OFFSET (GtkToolButtonClass, clicked),
320                   NULL, NULL,
321                   g_cclosure_marshal_VOID__VOID,
322                   G_TYPE_NONE, 0);
323   
324   g_type_class_add_private (object_class, sizeof (GtkToolButtonPrivate));
325 }
326
327 static void
328 gtk_tool_button_init (GtkToolButton      *button,
329                       GtkToolButtonClass *klass)
330 {
331   GtkToolItem *toolitem = GTK_TOOL_ITEM (button);
332
333   button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
334                                               GTK_TYPE_TOOL_BUTTON,
335                                               GtkToolButtonPrivate);
336
337   button->priv->contents_invalid = TRUE;
338
339   gtk_tool_item_set_homogeneous (toolitem, TRUE);
340
341   /* create button */
342   button->priv->button = g_object_new (klass->button_type, NULL);
343   gtk_button_set_focus_on_click (GTK_BUTTON (button->priv->button), FALSE);
344   g_signal_connect_object (button->priv->button, "clicked",
345                            G_CALLBACK (button_clicked), button, 0);
346
347   gtk_container_add (GTK_CONTAINER (button), button->priv->button);
348   gtk_widget_show (button->priv->button);
349 }
350
351 static void
352 gtk_tool_button_construct_contents (GtkToolItem *tool_item)
353 {
354   GtkToolButton *button = GTK_TOOL_BUTTON (tool_item);
355   GtkWidget *child;
356   GtkWidget *label = NULL;
357   GtkWidget *icon = NULL;
358   GtkToolbarStyle style;
359   gboolean need_label = FALSE;
360   gboolean need_icon = FALSE;
361   GtkIconSize icon_size;
362   GtkWidget *box = NULL;
363   guint icon_spacing;
364   GtkOrientation text_orientation = GTK_ORIENTATION_HORIZONTAL;
365   GtkSizeGroup *size_group = NULL;
366   GtkWidget *parent;
367
368   button->priv->contents_invalid = FALSE;
369
370   gtk_widget_style_get (GTK_WIDGET (tool_item), 
371                         "icon-spacing", &icon_spacing,
372                         NULL);
373
374   if (button->priv->icon_widget)
375     {
376       parent = gtk_widget_get_parent (button->priv->icon_widget);
377       if (parent)
378         {
379           gtk_container_remove (GTK_CONTAINER (parent),
380                                 button->priv->icon_widget);
381         }
382     }
383
384   if (button->priv->label_widget)
385     {
386       parent = gtk_widget_get_parent (button->priv->label_widget);
387       if (parent)
388         {
389           gtk_container_remove (GTK_CONTAINER (parent),
390                                 button->priv->label_widget);
391         }
392     }
393
394   child = gtk_bin_get_child (GTK_BIN (button->priv->button));
395   if (child)
396     {
397       /* Note: we are not destroying the label_widget or icon_widget
398        * here because they were removed from their containers above
399        */
400       gtk_widget_destroy (child);
401     }
402
403   style = gtk_tool_item_get_toolbar_style (GTK_TOOL_ITEM (button));
404   
405   if (style != GTK_TOOLBAR_TEXT)
406     need_icon = TRUE;
407
408   if (style != GTK_TOOLBAR_ICONS && style != GTK_TOOLBAR_BOTH_HORIZ)
409     need_label = TRUE;
410
411   if (style == GTK_TOOLBAR_BOTH_HORIZ &&
412       (gtk_tool_item_get_is_important (GTK_TOOL_ITEM (button)) ||
413        gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button)) == GTK_ORIENTATION_VERTICAL ||
414        gtk_tool_item_get_text_orientation (GTK_TOOL_ITEM (button)) == GTK_ORIENTATION_VERTICAL))
415     {
416       need_label = TRUE;
417     }
418   
419   if (style == GTK_TOOLBAR_ICONS && button->priv->icon_widget == NULL &&
420       button->priv->stock_id == NULL && button->priv->icon_name == NULL)
421     {
422       need_label = TRUE;
423       need_icon = FALSE;
424       style = GTK_TOOLBAR_TEXT;
425     }
426
427   if (style == GTK_TOOLBAR_TEXT && button->priv->label_widget == NULL &&
428       button->priv->stock_id == NULL && button->priv->label_text == NULL)
429     {
430       need_label = FALSE;
431       need_icon = TRUE;
432       style = GTK_TOOLBAR_ICONS;
433     }
434
435   if (need_label)
436     {
437       if (button->priv->label_widget)
438         {
439           label = button->priv->label_widget;
440         }
441       else
442         {
443           GtkStockItem stock_item;
444           gboolean elide;
445           gchar *label_text;
446
447           if (button->priv->label_text)
448             {
449               label_text = button->priv->label_text;
450               elide = button->priv->use_underline;
451             }
452           else if (button->priv->stock_id && gtk_stock_lookup (button->priv->stock_id, &stock_item))
453             {
454               label_text = stock_item.label;
455               elide = TRUE;
456             }
457           else
458             {
459               label_text = "";
460               elide = FALSE;
461             }
462
463           if (elide)
464             label_text = _gtk_toolbar_elide_underscores (label_text);
465           else
466             label_text = g_strdup (label_text);
467
468           label = gtk_label_new (label_text);
469
470           g_free (label_text);
471           
472           gtk_widget_show (label);
473         }
474
475       if (GTK_IS_LABEL (label))
476         {
477           gtk_label_set_ellipsize (GTK_LABEL (label),
478                                    gtk_tool_item_get_ellipsize_mode (GTK_TOOL_ITEM (button)));
479           text_orientation = gtk_tool_item_get_text_orientation (GTK_TOOL_ITEM (button));
480           if (text_orientation == GTK_ORIENTATION_HORIZONTAL)
481             {
482               gtk_label_set_angle (GTK_LABEL (label), 0);
483               gtk_misc_set_alignment (GTK_MISC (label),
484                                       gtk_tool_item_get_text_alignment (GTK_TOOL_ITEM (button)),
485                                       0.5);
486             }
487           else
488             {
489               gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_NONE);
490               if (gtk_widget_get_direction (GTK_WIDGET (tool_item)) == GTK_TEXT_DIR_RTL)
491                 gtk_label_set_angle (GTK_LABEL (label), -90);
492               else
493                 gtk_label_set_angle (GTK_LABEL (label), 90);
494               gtk_misc_set_alignment (GTK_MISC (label),
495                                       0.5,
496                                       1 - gtk_tool_item_get_text_alignment (GTK_TOOL_ITEM (button)));
497             }
498         }
499     }
500
501   icon_size = gtk_tool_item_get_icon_size (GTK_TOOL_ITEM (button));
502   if (need_icon)
503     {
504       if (button->priv->icon_widget)
505         {
506           icon = button->priv->icon_widget;
507           
508           if (GTK_IS_IMAGE (icon))
509             {
510               g_object_set (button->priv->icon_widget,
511                             "icon-size", icon_size,
512                             NULL);
513             }
514         }
515       else if (button->priv->stock_id && 
516                gtk_icon_factory_lookup_default (button->priv->stock_id))
517         {
518           icon = gtk_image_new_from_stock (button->priv->stock_id, icon_size);
519           gtk_widget_show (icon);
520         }
521       else if (button->priv->icon_name)
522         {
523           icon = gtk_image_new_from_icon_name (button->priv->icon_name, icon_size);
524           gtk_widget_show (icon);
525         }
526
527       if (GTK_IS_MISC (icon) && text_orientation == GTK_ORIENTATION_HORIZONTAL)
528         gtk_misc_set_alignment (GTK_MISC (icon),
529                                 1.0 - gtk_tool_item_get_text_alignment (GTK_TOOL_ITEM (button)),
530                                 0.5);
531       else if (GTK_IS_MISC (icon))
532         gtk_misc_set_alignment (GTK_MISC (icon),
533                                 0.5,
534                                 gtk_tool_item_get_text_alignment (GTK_TOOL_ITEM (button)));
535
536       if (icon)
537         {
538           size_group = gtk_tool_item_get_text_size_group (GTK_TOOL_ITEM (button));
539           if (size_group != NULL)
540             gtk_size_group_add_widget (size_group, icon);
541         }
542     }
543
544   switch (style)
545     {
546     case GTK_TOOLBAR_ICONS:
547       if (icon)
548         gtk_container_add (GTK_CONTAINER (button->priv->button), icon);
549       break;
550
551     case GTK_TOOLBAR_BOTH:
552       if (text_orientation == GTK_ORIENTATION_HORIZONTAL)
553         box = gtk_box_new (GTK_ORIENTATION_VERTICAL, icon_spacing);
554       else
555         box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, icon_spacing);
556       if (icon)
557         gtk_box_pack_start (GTK_BOX (box), icon, TRUE, TRUE, 0);
558       gtk_box_pack_end (GTK_BOX (box), label, FALSE, TRUE, 0);
559       gtk_container_add (GTK_CONTAINER (button->priv->button), box);
560       break;
561
562     case GTK_TOOLBAR_BOTH_HORIZ:
563       if (text_orientation == GTK_ORIENTATION_HORIZONTAL)
564         {
565           box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, icon_spacing);
566           if (icon)
567             gtk_box_pack_start (GTK_BOX (box), icon, label? FALSE : TRUE, TRUE, 0);
568           if (label)
569             gtk_box_pack_end (GTK_BOX (box), label, TRUE, TRUE, 0);
570         }
571       else
572         {
573           box = gtk_box_new (GTK_ORIENTATION_VERTICAL, icon_spacing);
574           if (icon)
575             gtk_box_pack_end (GTK_BOX (box), icon, label ? FALSE : TRUE, TRUE, 0);
576           if (label)
577             gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
578         }
579       gtk_container_add (GTK_CONTAINER (button->priv->button), box);
580       break;
581
582     case GTK_TOOLBAR_TEXT:
583       gtk_container_add (GTK_CONTAINER (button->priv->button), label);
584       break;
585     }
586
587   if (box)
588     gtk_widget_show (box);
589
590   gtk_button_set_relief (GTK_BUTTON (button->priv->button),
591                          gtk_tool_item_get_relief_style (GTK_TOOL_ITEM (button)));
592
593   gtk_tool_item_rebuild_menu (tool_item);
594   
595   gtk_widget_queue_resize (GTK_WIDGET (button));
596 }
597
598 static void
599 gtk_tool_button_set_property (GObject         *object,
600                               guint            prop_id,
601                               const GValue    *value,
602                               GParamSpec      *pspec)
603 {
604   GtkToolButton *button = GTK_TOOL_BUTTON (object);
605   
606   switch (prop_id)
607     {
608     case PROP_LABEL:
609       gtk_tool_button_set_label (button, g_value_get_string (value));
610       break;
611     case PROP_USE_UNDERLINE:
612       gtk_tool_button_set_use_underline (button, g_value_get_boolean (value));
613       break;
614     case PROP_LABEL_WIDGET:
615       gtk_tool_button_set_label_widget (button, g_value_get_object (value));
616       break;
617     case PROP_STOCK_ID:
618       gtk_tool_button_set_stock_id (button, g_value_get_string (value));
619       break;
620     case PROP_ICON_NAME:
621       gtk_tool_button_set_icon_name (button, g_value_get_string (value));
622       break;
623     case PROP_ICON_WIDGET:
624       gtk_tool_button_set_icon_widget (button, g_value_get_object (value));
625       break;
626     case PROP_ACTION_NAME:
627       g_object_set_property (G_OBJECT (button->priv->button), "action-name", value);
628       break;
629     case PROP_ACTION_TARGET:
630       g_object_set_property (G_OBJECT (button->priv->button), "action-target", value);
631       break;
632     default:
633       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
634       break;
635     }
636 }
637
638 static void
639 gtk_tool_button_property_notify (GObject          *object,
640                                  GParamSpec       *pspec)
641 {
642   GtkToolButton *button = GTK_TOOL_BUTTON (object);
643
644   if (button->priv->contents_invalid ||
645       strcmp ("is-important", pspec->name) == 0)
646     gtk_tool_button_construct_contents (GTK_TOOL_ITEM (object));
647
648   if (parent_class->notify)
649     parent_class->notify (object, pspec);
650 }
651
652 static void
653 gtk_tool_button_get_property (GObject         *object,
654                               guint            prop_id,
655                               GValue          *value,
656                               GParamSpec      *pspec)
657 {
658   GtkToolButton *button = GTK_TOOL_BUTTON (object);
659
660   switch (prop_id)
661     {
662     case PROP_LABEL:
663       g_value_set_string (value, gtk_tool_button_get_label (button));
664       break;
665     case PROP_LABEL_WIDGET:
666       g_value_set_object (value, gtk_tool_button_get_label_widget (button));
667       break;
668     case PROP_USE_UNDERLINE:
669       g_value_set_boolean (value, gtk_tool_button_get_use_underline (button));
670       break;
671     case PROP_STOCK_ID:
672       g_value_set_string (value, button->priv->stock_id);
673       break;
674     case PROP_ICON_NAME:
675       g_value_set_string (value, button->priv->icon_name);
676       break;
677     case PROP_ICON_WIDGET:
678       g_value_set_object (value, button->priv->icon_widget);
679       break;
680     case PROP_ACTION_NAME:
681       g_object_get_property (G_OBJECT (button->priv->button), "action-name", value);
682       break;
683     case PROP_ACTION_TARGET:
684       g_object_get_property (G_OBJECT (button->priv->button), "action-target", value);
685       break;
686     default:
687       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
688       break;
689     }
690 }
691
692 static const gchar *
693 gtk_tool_button_get_action_name (GtkActionable *actionable)
694 {
695   GtkToolButton *button = GTK_TOOL_BUTTON (actionable);
696
697   return gtk_actionable_get_action_name (GTK_ACTIONABLE (button->priv->button));
698 }
699
700 static void
701 gtk_tool_button_set_action_name (GtkActionable *actionable,
702                                  const gchar   *action_name)
703 {
704   GtkToolButton *button = GTK_TOOL_BUTTON (actionable);
705
706   gtk_actionable_set_action_name (GTK_ACTIONABLE (button->priv->button), action_name);
707 }
708
709 static GVariant *
710 gtk_tool_button_get_action_target_value (GtkActionable *actionable)
711 {
712   GtkToolButton *button = GTK_TOOL_BUTTON (actionable);
713
714   return gtk_actionable_get_action_target_value (GTK_ACTIONABLE (button->priv->button));
715 }
716
717 static void
718 gtk_tool_button_set_action_target_value (GtkActionable *actionable,
719                                          GVariant      *action_target)
720 {
721   GtkToolButton *button = GTK_TOOL_BUTTON (actionable);
722
723   gtk_actionable_set_action_target_value (GTK_ACTIONABLE (button->priv->button), action_target);
724 }
725
726 static void
727 gtk_tool_button_actionable_iface_init (GtkActionableInterface *iface)
728 {
729   iface->get_action_name = gtk_tool_button_get_action_name;
730   iface->set_action_name = gtk_tool_button_set_action_name;
731   iface->get_action_target_value = gtk_tool_button_get_action_target_value;
732   iface->set_action_target_value = gtk_tool_button_set_action_target_value;
733 }
734
735 static void
736 gtk_tool_button_finalize (GObject *object)
737 {
738   GtkToolButton *button = GTK_TOOL_BUTTON (object);
739
740   g_free (button->priv->stock_id);
741   g_free (button->priv->icon_name);
742   g_free (button->priv->label_text);
743
744   if (button->priv->label_widget)
745     g_object_unref (button->priv->label_widget);
746
747   if (button->priv->icon_widget)
748     g_object_unref (button->priv->icon_widget);
749   
750   parent_class->finalize (object);
751 }
752
753 static GtkWidget *
754 clone_image_menu_size (GtkImage *image, GtkSettings *settings)
755 {
756   GtkImageType storage_type = gtk_image_get_storage_type (image);
757
758   if (storage_type == GTK_IMAGE_STOCK)
759     {
760       gchar *stock_id;
761       gtk_image_get_stock (image, &stock_id, NULL);
762       return gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
763     }
764   else if (storage_type == GTK_IMAGE_ICON_NAME)
765     {
766       const gchar *icon_name;
767       gtk_image_get_icon_name (image, &icon_name, NULL);
768       return gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
769     }
770   else if (storage_type == GTK_IMAGE_ICON_SET)
771     {
772       GtkIconSet *icon_set;
773       gtk_image_get_icon_set (image, &icon_set, NULL);
774       return gtk_image_new_from_icon_set (icon_set, GTK_ICON_SIZE_MENU);
775     }
776   else if (storage_type == GTK_IMAGE_GICON)
777     {
778       GIcon *icon;
779       gtk_image_get_gicon (image, &icon, NULL);
780       return gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_MENU);
781     }
782   else if (storage_type == GTK_IMAGE_PIXBUF)
783     {
784       gint width, height;
785       
786       if (settings &&
787           gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
788                                              &width, &height))
789         {
790           GdkPixbuf *src_pixbuf, *dest_pixbuf;
791           GtkWidget *cloned_image;
792
793           src_pixbuf = gtk_image_get_pixbuf (image);
794           dest_pixbuf = gdk_pixbuf_scale_simple (src_pixbuf, width, height,
795                                                  GDK_INTERP_BILINEAR);
796
797           cloned_image = gtk_image_new_from_pixbuf (dest_pixbuf);
798           g_object_unref (dest_pixbuf);
799
800           return cloned_image;
801         }
802     }
803
804   return NULL;
805 }
806       
807 static gboolean
808 gtk_tool_button_create_menu_proxy (GtkToolItem *item)
809 {
810   GtkToolButton *button = GTK_TOOL_BUTTON (item);
811   GtkWidget *menu_item;
812   GtkWidget *menu_image = NULL;
813   GtkStockItem stock_item;
814   gboolean use_mnemonic = TRUE;
815   const char *label;
816
817   if (_gtk_tool_item_create_menu_proxy (item))
818     return TRUE;
819  
820   if (GTK_IS_LABEL (button->priv->label_widget))
821     {
822       label = gtk_label_get_label (GTK_LABEL (button->priv->label_widget));
823       use_mnemonic = gtk_label_get_use_underline (GTK_LABEL (button->priv->label_widget));
824     }
825   else if (button->priv->label_text)
826     {
827       label = button->priv->label_text;
828       use_mnemonic = button->priv->use_underline;
829     }
830   else if (button->priv->stock_id && gtk_stock_lookup (button->priv->stock_id, &stock_item))
831     {
832       label = stock_item.label;
833     }
834   else
835     {
836       label = "";
837     }
838   
839   if (use_mnemonic)
840     menu_item = gtk_image_menu_item_new_with_mnemonic (label);
841   else
842     menu_item = gtk_image_menu_item_new_with_label (label);
843
844   if (GTK_IS_IMAGE (button->priv->icon_widget))
845     {
846       menu_image = clone_image_menu_size (GTK_IMAGE (button->priv->icon_widget),
847                                           gtk_widget_get_settings (GTK_WIDGET (button)));
848     }
849   else if (button->priv->stock_id)
850     {
851       menu_image = gtk_image_new_from_stock (button->priv->stock_id, GTK_ICON_SIZE_MENU);
852     }
853
854   if (menu_image)
855     gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item), menu_image);
856
857   g_signal_connect_closure_by_id (menu_item,
858                                   g_signal_lookup ("activate", G_OBJECT_TYPE (menu_item)), 0,
859                                   g_cclosure_new_object_swap (G_CALLBACK (gtk_button_clicked),
860                                                               G_OBJECT (GTK_TOOL_BUTTON (button)->priv->button)),
861                                   FALSE);
862
863   gtk_tool_item_set_proxy_menu_item (GTK_TOOL_ITEM (button), MENU_ID, menu_item);
864   
865   return TRUE;
866 }
867
868 static void
869 button_clicked (GtkWidget     *widget,
870                 GtkToolButton *button)
871 {
872   GtkAction *action;
873
874   action = gtk_activatable_get_related_action (GTK_ACTIVATABLE (button));
875   
876   if (action)
877     gtk_action_activate (action);
878
879   g_signal_emit_by_name (button, "clicked");
880 }
881
882 static void
883 gtk_tool_button_toolbar_reconfigured (GtkToolItem *tool_item)
884 {
885   gtk_tool_button_construct_contents (tool_item);
886 }
887
888 static void 
889 gtk_tool_button_update_icon_spacing (GtkToolButton *button)
890 {
891   GtkWidget *box;
892   guint spacing;
893
894   box = gtk_bin_get_child (GTK_BIN (button->priv->button));
895   if (GTK_IS_BOX (box))
896     {
897       gtk_widget_style_get (GTK_WIDGET (button), 
898                             "icon-spacing", &spacing,
899                             NULL);
900       gtk_box_set_spacing (GTK_BOX (box), spacing);      
901     }
902 }
903
904 static void
905 gtk_tool_button_style_updated (GtkWidget *widget)
906 {
907   GTK_WIDGET_CLASS (parent_class)->style_updated (widget);
908
909   gtk_tool_button_update_icon_spacing (GTK_TOOL_BUTTON (widget));
910 }
911
912 static void 
913 gtk_tool_button_activatable_interface_init (GtkActivatableIface  *iface)
914 {
915   parent_activatable_iface = g_type_interface_peek_parent (iface);
916   iface->update = gtk_tool_button_update;
917   iface->sync_action_properties = gtk_tool_button_sync_action_properties;
918 }
919
920 static void
921 gtk_tool_button_update (GtkActivatable *activatable,
922                         GtkAction      *action,
923                         const gchar    *property_name)
924 {
925   GtkToolButton *button;
926   GtkWidget *image;
927
928   parent_activatable_iface->update (activatable, action, property_name);
929
930   if (!gtk_activatable_get_use_action_appearance (activatable))
931     return;
932
933   button = GTK_TOOL_BUTTON (activatable);
934   
935   if (strcmp (property_name, "short-label") == 0)
936     gtk_tool_button_set_label (button, gtk_action_get_short_label (action));
937   else if (strcmp (property_name, "stock-id") == 0)
938     gtk_tool_button_set_stock_id (button, gtk_action_get_stock_id (action));
939   else if (strcmp (property_name, "gicon") == 0)
940     {
941       const gchar *stock_id = gtk_action_get_stock_id (action);
942       GIcon *icon = gtk_action_get_gicon (action);
943       GtkIconSize icon_size = GTK_ICON_SIZE_BUTTON;
944
945       if ((stock_id && gtk_icon_factory_lookup_default (stock_id)) || !icon)
946         image = NULL;
947       else 
948         {   
949           image = gtk_tool_button_get_icon_widget (button);
950           icon_size = gtk_tool_item_get_icon_size (GTK_TOOL_ITEM (button));
951
952           if (!image)
953             image = gtk_image_new ();
954         }
955
956       gtk_tool_button_set_icon_widget (button, image);
957       gtk_image_set_from_gicon (GTK_IMAGE (image), icon, icon_size);
958
959     }
960   else if (strcmp (property_name, "icon-name") == 0)
961     gtk_tool_button_set_icon_name (button, gtk_action_get_icon_name (action));
962 }
963
964 static void
965 gtk_tool_button_sync_action_properties (GtkActivatable *activatable,
966                                         GtkAction      *action)
967 {
968   GtkToolButton *button;
969   GIcon         *icon;
970   const gchar   *stock_id;
971
972   parent_activatable_iface->sync_action_properties (activatable, action);
973
974   if (!action)
975     return;
976
977   if (!gtk_activatable_get_use_action_appearance (activatable))
978     return;
979
980   button = GTK_TOOL_BUTTON (activatable);
981   stock_id = gtk_action_get_stock_id (action);
982
983   gtk_tool_button_set_label (button, gtk_action_get_short_label (action));
984   gtk_tool_button_set_use_underline (button, TRUE);
985   gtk_tool_button_set_stock_id (button, stock_id);
986   gtk_tool_button_set_icon_name (button, gtk_action_get_icon_name (action));
987
988   if (stock_id && gtk_icon_factory_lookup_default (stock_id))
989       gtk_tool_button_set_icon_widget (button, NULL);
990   else if ((icon = gtk_action_get_gicon (action)) != NULL)
991     {
992       GtkIconSize icon_size = gtk_tool_item_get_icon_size (GTK_TOOL_ITEM (button));
993       GtkWidget  *image = gtk_tool_button_get_icon_widget (button);
994       
995       if (!image)
996         {
997           image = gtk_image_new ();
998           gtk_widget_show (image);
999           gtk_tool_button_set_icon_widget (button, image);
1000         }
1001
1002       gtk_image_set_from_gicon (GTK_IMAGE (image), icon, icon_size);
1003     }
1004   else if (gtk_action_get_icon_name (action))
1005     gtk_tool_button_set_icon_name (button, gtk_action_get_icon_name (action));
1006   else
1007     gtk_tool_button_set_label (button, gtk_action_get_short_label (action));
1008 }
1009
1010 /**
1011  * gtk_tool_button_new_from_stock:
1012  * @stock_id: the name of the stock item 
1013  *
1014  * Creates a new #GtkToolButton containing the image and text from a
1015  * stock item. Some stock ids have preprocessor macros like #GTK_STOCK_OK
1016  * and #GTK_STOCK_APPLY.
1017  *
1018  * It is an error if @stock_id is not a name of a stock item.
1019  * 
1020  * Return value: A new #GtkToolButton
1021  * 
1022  * Since: 2.4
1023  **/
1024 GtkToolItem *
1025 gtk_tool_button_new_from_stock (const gchar *stock_id)
1026 {
1027   GtkToolButton *button;
1028
1029   g_return_val_if_fail (stock_id != NULL, NULL);
1030     
1031   button = g_object_new (GTK_TYPE_TOOL_BUTTON,
1032                          "stock-id", stock_id,
1033                          NULL);
1034
1035   return GTK_TOOL_ITEM (button);
1036 }
1037
1038 /**
1039  * gtk_tool_button_new:
1040  * @label: (allow-none): a string that will be used as label, or %NULL
1041  * @icon_widget: (allow-none): a widget that will be used as the button contents, or %NULL
1042  *
1043  * Creates a new %GtkToolButton using @icon_widget as contents and @label as
1044  * label.
1045  *
1046  * Return value: A new #GtkToolButton
1047  * 
1048  * Since: 2.4
1049  **/
1050 GtkToolItem *
1051 gtk_tool_button_new (GtkWidget   *icon_widget,
1052                      const gchar *label)
1053 {
1054   GtkToolButton *button;
1055
1056   g_return_val_if_fail (icon_widget == NULL || GTK_IS_WIDGET (icon_widget), NULL);
1057
1058   button = g_object_new (GTK_TYPE_TOOL_BUTTON,
1059                          "label", label,
1060                          "icon-widget", icon_widget,
1061                          NULL);
1062
1063   return GTK_TOOL_ITEM (button);  
1064 }
1065
1066 /**
1067  * gtk_tool_button_set_label:
1068  * @button: a #GtkToolButton
1069  * @label: (allow-none): a string that will be used as label, or %NULL.
1070  *
1071  * Sets @label as the label used for the tool button. The "label" property
1072  * only has an effect if not overridden by a non-%NULL "label_widget" property.
1073  * If both the "label_widget" and "label" properties are %NULL, the label
1074  * is determined by the "stock_id" property. If the "stock_id" property is also
1075  * %NULL, @button will not have a label.
1076  * 
1077  * Since: 2.4
1078  **/
1079 void
1080 gtk_tool_button_set_label (GtkToolButton *button,
1081                            const gchar   *label)
1082 {
1083   gchar *old_label;
1084   gchar *elided_label;
1085   AtkObject *accessible;
1086   
1087   g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
1088
1089   old_label = button->priv->label_text;
1090
1091   button->priv->label_text = g_strdup (label);
1092   button->priv->contents_invalid = TRUE;     
1093
1094   if (label)
1095     {
1096       elided_label = _gtk_toolbar_elide_underscores (label);
1097       accessible = gtk_widget_get_accessible (GTK_WIDGET (button->priv->button));
1098       atk_object_set_name (accessible, elided_label);
1099       g_free (elided_label);
1100     }
1101
1102   g_free (old_label);
1103  
1104   g_object_notify (G_OBJECT (button), "label");
1105 }
1106
1107 /**
1108  * gtk_tool_button_get_label:
1109  * @button: a #GtkToolButton
1110  * 
1111  * Returns the label used by the tool button, or %NULL if the tool button
1112  * doesn't have a label. or uses a the label from a stock item. The returned
1113  * string is owned by GTK+, and must not be modified or freed.
1114  * 
1115  * Return value: The label, or %NULL
1116  * 
1117  * Since: 2.4
1118  **/
1119 const gchar *
1120 gtk_tool_button_get_label (GtkToolButton *button)
1121 {
1122   g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1123
1124   return button->priv->label_text;
1125 }
1126
1127 /**
1128  * gtk_tool_button_set_use_underline:
1129  * @button: a #GtkToolButton
1130  * @use_underline: whether the button label has the form "_Open"
1131  *
1132  * If set, an underline in the label property indicates that the next character
1133  * should be used for the mnemonic accelerator key in the overflow menu. For
1134  * example, if the label property is "_Open" and @use_underline is %TRUE,
1135  * the label on the tool button will be "Open" and the item on the overflow
1136  * menu will have an underlined 'O'.
1137  * 
1138  * Labels shown on tool buttons never have mnemonics on them; this property
1139  * only affects the menu item on the overflow menu.
1140  * 
1141  * Since: 2.4
1142  **/
1143 void
1144 gtk_tool_button_set_use_underline (GtkToolButton *button,
1145                                    gboolean       use_underline)
1146 {
1147   g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
1148
1149   use_underline = use_underline != FALSE;
1150
1151   if (use_underline != button->priv->use_underline)
1152     {
1153       button->priv->use_underline = use_underline;
1154       button->priv->contents_invalid = TRUE;
1155
1156       g_object_notify (G_OBJECT (button), "use-underline");
1157     }
1158 }
1159
1160 /**
1161  * gtk_tool_button_get_use_underline:
1162  * @button: a #GtkToolButton
1163  * 
1164  * Returns whether underscores in the label property are used as mnemonics
1165  * on menu items on the overflow menu. See gtk_tool_button_set_use_underline().
1166  * 
1167  * Return value: %TRUE if underscores in the label property are used as
1168  * mnemonics on menu items on the overflow menu.
1169  * 
1170  * Since: 2.4
1171  **/
1172 gboolean
1173 gtk_tool_button_get_use_underline (GtkToolButton *button)
1174 {
1175   g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), FALSE);
1176
1177   return button->priv->use_underline;
1178 }
1179
1180 /**
1181  * gtk_tool_button_set_stock_id:
1182  * @button: a #GtkToolButton
1183  * @stock_id: (allow-none): a name of a stock item, or %NULL
1184  *
1185  * Sets the name of the stock item. See gtk_tool_button_new_from_stock().
1186  * The stock_id property only has an effect if not
1187  * overridden by non-%NULL "label" and "icon_widget" properties.
1188  * 
1189  * Since: 2.4
1190  **/
1191 void
1192 gtk_tool_button_set_stock_id (GtkToolButton *button,
1193                               const gchar   *stock_id)
1194 {
1195   gchar *old_stock_id;
1196   
1197   g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
1198
1199   old_stock_id = button->priv->stock_id;
1200
1201   button->priv->stock_id = g_strdup (stock_id);
1202   button->priv->contents_invalid = TRUE;
1203
1204   g_free (old_stock_id);
1205   
1206   g_object_notify (G_OBJECT (button), "stock-id");
1207 }
1208
1209 /**
1210  * gtk_tool_button_get_stock_id:
1211  * @button: a #GtkToolButton
1212  * 
1213  * Returns the name of the stock item. See gtk_tool_button_set_stock_id().
1214  * The returned string is owned by GTK+ and must not be freed or modifed.
1215  * 
1216  * Return value: the name of the stock item for @button.
1217  * 
1218  * Since: 2.4
1219  **/
1220 const gchar *
1221 gtk_tool_button_get_stock_id (GtkToolButton *button)
1222 {
1223   g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1224
1225   return button->priv->stock_id;
1226 }
1227
1228 /**
1229  * gtk_tool_button_set_icon_name:
1230  * @button: a #GtkToolButton
1231  * @icon_name: (allow-none): the name of the themed icon
1232  *
1233  * Sets the icon for the tool button from a named themed icon.
1234  * See the docs for #GtkIconTheme for more details.
1235  * The "icon_name" property only has an effect if not
1236  * overridden by non-%NULL "label", "icon_widget" and "stock_id"
1237  * properties.
1238  * 
1239  * Since: 2.8
1240  **/
1241 void
1242 gtk_tool_button_set_icon_name (GtkToolButton *button,
1243                                const gchar   *icon_name)
1244 {
1245   gchar *old_icon_name;
1246
1247   g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
1248
1249   old_icon_name = button->priv->icon_name;
1250
1251   button->priv->icon_name = g_strdup (icon_name);
1252   button->priv->contents_invalid = TRUE; 
1253
1254   g_free (old_icon_name);
1255
1256   g_object_notify (G_OBJECT (button), "icon-name");
1257 }
1258
1259 /**
1260  * gtk_tool_button_get_icon_name:
1261  * @button: a #GtkToolButton
1262  *
1263  * Returns the name of the themed icon for the tool button,
1264  * see gtk_tool_button_set_icon_name().
1265  *
1266  * Returns: the icon name or %NULL if the tool button has
1267  * no themed icon
1268  *
1269  * Since: 2.8
1270  **/
1271 const gchar*
1272 gtk_tool_button_get_icon_name (GtkToolButton *button)
1273 {
1274   g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1275
1276   return button->priv->icon_name;
1277 }
1278
1279 /**
1280  * gtk_tool_button_set_icon_widget:
1281  * @button: a #GtkToolButton
1282  * @icon_widget: (allow-none): the widget used as icon, or %NULL
1283  *
1284  * Sets @icon as the widget used as icon on @button. If @icon_widget is
1285  * %NULL the icon is determined by the "stock_id" property. If the
1286  * "stock_id" property is also %NULL, @button will not have an icon.
1287  * 
1288  * Since: 2.4
1289  **/
1290 void
1291 gtk_tool_button_set_icon_widget (GtkToolButton *button,
1292                                  GtkWidget     *icon_widget)
1293 {
1294   g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
1295   g_return_if_fail (icon_widget == NULL || GTK_IS_WIDGET (icon_widget));
1296
1297   if (icon_widget != button->priv->icon_widget)
1298     {
1299       if (button->priv->icon_widget)
1300         {
1301           GtkWidget *parent;
1302
1303           parent = gtk_widget_get_parent (button->priv->icon_widget);
1304           if (parent)
1305             gtk_container_remove (GTK_CONTAINER (parent),
1306                                   button->priv->icon_widget);
1307
1308           g_object_unref (button->priv->icon_widget);
1309         }
1310       
1311       if (icon_widget)
1312         g_object_ref_sink (icon_widget);
1313
1314       button->priv->icon_widget = icon_widget;
1315       button->priv->contents_invalid = TRUE;
1316       
1317       g_object_notify (G_OBJECT (button), "icon-widget");
1318     }
1319 }
1320
1321 /**
1322  * gtk_tool_button_set_label_widget:
1323  * @button: a #GtkToolButton
1324  * @label_widget: (allow-none): the widget used as label, or %NULL
1325  *
1326  * Sets @label_widget as the widget that will be used as the label
1327  * for @button. If @label_widget is %NULL the "label" property is used
1328  * as label. If "label" is also %NULL, the label in the stock item
1329  * determined by the "stock_id" property is used as label. If
1330  * "stock_id" is also %NULL, @button does not have a label.
1331  * 
1332  * Since: 2.4
1333  **/
1334 void
1335 gtk_tool_button_set_label_widget (GtkToolButton *button,
1336                                   GtkWidget     *label_widget)
1337 {
1338   g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
1339   g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1340
1341   if (label_widget != button->priv->label_widget)
1342     {
1343       if (button->priv->label_widget)
1344         {
1345           GtkWidget *parent;
1346
1347           parent = gtk_widget_get_parent (button->priv->label_widget);
1348           if (parent)
1349             gtk_container_remove (GTK_CONTAINER (parent),
1350                                   button->priv->label_widget);
1351
1352           g_object_unref (button->priv->label_widget);
1353         }
1354       
1355       if (label_widget)
1356         g_object_ref_sink (label_widget);
1357
1358       button->priv->label_widget = label_widget;
1359       button->priv->contents_invalid = TRUE;
1360       
1361       g_object_notify (G_OBJECT (button), "label-widget");
1362     }
1363 }
1364
1365 /**
1366  * gtk_tool_button_get_label_widget:
1367  * @button: a #GtkToolButton
1368  *
1369  * Returns the widget used as label on @button.
1370  * See gtk_tool_button_set_label_widget().
1371  *
1372  * Return value: (transfer none): The widget used as label
1373  *     on @button, or %NULL.
1374  *
1375  * Since: 2.4
1376  **/
1377 GtkWidget *
1378 gtk_tool_button_get_label_widget (GtkToolButton *button)
1379 {
1380   g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1381
1382   return button->priv->label_widget;
1383 }
1384
1385 /**
1386  * gtk_tool_button_get_icon_widget:
1387  * @button: a #GtkToolButton
1388  *
1389  * Return the widget used as icon widget on @button.
1390  * See gtk_tool_button_set_icon_widget().
1391  *
1392  * Return value: (transfer none): The widget used as icon
1393  *     on @button, or %NULL.
1394  *
1395  * Since: 2.4
1396  **/
1397 GtkWidget *
1398 gtk_tool_button_get_icon_widget (GtkToolButton *button)
1399 {
1400   g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1401
1402   return button->priv->icon_widget;
1403 }
1404
1405 GtkWidget *
1406 _gtk_tool_button_get_button (GtkToolButton *button)
1407 {
1408   g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1409
1410   return button->priv->button;
1411 }