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