]> Pileus Git - ~andy/gtk/blob - gtk/gtktoolbutton.c
Merge branch 'gtk-2-90'
[~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 (GTK_IS_MISC (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 (GTK_IS_MISC (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 #GtkMisc 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   g_return_val_if_fail (icon_widget == NULL || GTK_IS_MISC (icon_widget), NULL);
947
948   button = g_object_new (GTK_TYPE_TOOL_BUTTON,
949                          "label", label,
950                          "icon-widget", icon_widget,
951                          NULL);
952
953   return GTK_TOOL_ITEM (button);  
954 }
955
956 /**
957  * gtk_tool_button_set_label:
958  * @button: a #GtkToolButton
959  * @label: (allow-none): a string that will be used as label, or %NULL.
960  *
961  * Sets @label as the label used for the tool button. The "label" property
962  * only has an effect if not overridden by a non-%NULL "label_widget" property.
963  * If both the "label_widget" and "label" properties are %NULL, the label
964  * is determined by the "stock_id" property. If the "stock_id" property is also
965  * %NULL, @button will not have a label.
966  * 
967  * Since: 2.4
968  **/
969 void
970 gtk_tool_button_set_label (GtkToolButton *button,
971                            const gchar   *label)
972 {
973   gchar *old_label;
974   gchar *elided_label;
975   AtkObject *accessible;
976   
977   g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
978
979   old_label = button->priv->label_text;
980
981   button->priv->label_text = g_strdup (label);
982   button->priv->contents_invalid = TRUE;     
983
984   if (label)
985     {
986       elided_label = _gtk_toolbar_elide_underscores (label);
987       accessible = gtk_widget_get_accessible (GTK_WIDGET (button->priv->button));
988       atk_object_set_name (accessible, elided_label);
989       g_free (elided_label);
990     }
991
992   g_free (old_label);
993  
994   g_object_notify (G_OBJECT (button), "label");
995 }
996
997 /**
998  * gtk_tool_button_get_label:
999  * @button: a #GtkToolButton
1000  * 
1001  * Returns the label used by the tool button, or %NULL if the tool button
1002  * doesn't have a label. or uses a the label from a stock item. The returned
1003  * string is owned by GTK+, and must not be modified or freed.
1004  * 
1005  * Return value: The label, or %NULL
1006  * 
1007  * Since: 2.4
1008  **/
1009 G_CONST_RETURN gchar *
1010 gtk_tool_button_get_label (GtkToolButton *button)
1011 {
1012   g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1013
1014   return button->priv->label_text;
1015 }
1016
1017 /**
1018  * gtk_tool_button_set_use_underline:
1019  * @button: a #GtkToolButton
1020  * @use_underline: whether the button label has the form "_Open"
1021  *
1022  * If set, an underline in the label property indicates that the next character
1023  * should be used for the mnemonic accelerator key in the overflow menu. For
1024  * example, if the label property is "_Open" and @use_underline is %TRUE,
1025  * the label on the tool button will be "Open" and the item on the overflow
1026  * menu will have an underlined 'O'.
1027  * 
1028  * Labels shown on tool buttons never have mnemonics on them; this property
1029  * only affects the menu item on the overflow menu.
1030  * 
1031  * Since: 2.4
1032  **/
1033 void
1034 gtk_tool_button_set_use_underline (GtkToolButton *button,
1035                                    gboolean       use_underline)
1036 {
1037   g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
1038
1039   use_underline = use_underline != FALSE;
1040
1041   if (use_underline != button->priv->use_underline)
1042     {
1043       button->priv->use_underline = use_underline;
1044       button->priv->contents_invalid = TRUE;
1045
1046       g_object_notify (G_OBJECT (button), "use-underline");
1047     }
1048 }
1049
1050 /**
1051  * gtk_tool_button_get_use_underline:
1052  * @button: a #GtkToolButton
1053  * 
1054  * Returns whether underscores in the label property are used as mnemonics
1055  * on menu items on the overflow menu. See gtk_tool_button_set_use_underline().
1056  * 
1057  * Return value: %TRUE if underscores in the label property are used as
1058  * mnemonics on menu items on the overflow menu.
1059  * 
1060  * Since: 2.4
1061  **/
1062 gboolean
1063 gtk_tool_button_get_use_underline (GtkToolButton *button)
1064 {
1065   g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), FALSE);
1066
1067   return button->priv->use_underline;
1068 }
1069
1070 /**
1071  * gtk_tool_button_set_stock_id:
1072  * @button: a #GtkToolButton
1073  * @stock_id: (allow-none): a name of a stock item, or %NULL
1074  *
1075  * Sets the name of the stock item. See gtk_tool_button_new_from_stock().
1076  * The stock_id property only has an effect if not
1077  * overridden by non-%NULL "label" and "icon_widget" properties.
1078  * 
1079  * Since: 2.4
1080  **/
1081 void
1082 gtk_tool_button_set_stock_id (GtkToolButton *button,
1083                               const gchar   *stock_id)
1084 {
1085   gchar *old_stock_id;
1086   
1087   g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
1088
1089   old_stock_id = button->priv->stock_id;
1090
1091   button->priv->stock_id = g_strdup (stock_id);
1092   button->priv->contents_invalid = TRUE;
1093
1094   g_free (old_stock_id);
1095   
1096   g_object_notify (G_OBJECT (button), "stock-id");
1097 }
1098
1099 /**
1100  * gtk_tool_button_get_stock_id:
1101  * @button: a #GtkToolButton
1102  * 
1103  * Returns the name of the stock item. See gtk_tool_button_set_stock_id().
1104  * The returned string is owned by GTK+ and must not be freed or modifed.
1105  * 
1106  * Return value: the name of the stock item for @button.
1107  * 
1108  * Since: 2.4
1109  **/
1110 G_CONST_RETURN gchar *
1111 gtk_tool_button_get_stock_id (GtkToolButton *button)
1112 {
1113   g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1114
1115   return button->priv->stock_id;
1116 }
1117
1118 /**
1119  * gtk_tool_button_set_icon_name
1120  * @button: a #GtkToolButton
1121  * @icon_name: (allow-none): the name of the themed icon
1122  *
1123  * Sets the icon for the tool button from a named themed icon.
1124  * See the docs for #GtkIconTheme for more details.
1125  * The "icon_name" property only has an effect if not
1126  * overridden by non-%NULL "label", "icon_widget" and "stock_id"
1127  * properties.
1128  * 
1129  * Since: 2.8
1130  **/
1131 void
1132 gtk_tool_button_set_icon_name (GtkToolButton *button,
1133                                const gchar   *icon_name)
1134 {
1135   gchar *old_icon_name;
1136
1137   g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
1138
1139   old_icon_name = button->priv->icon_name;
1140
1141   button->priv->icon_name = g_strdup (icon_name);
1142   button->priv->contents_invalid = TRUE; 
1143
1144   g_free (old_icon_name);
1145
1146   g_object_notify (G_OBJECT (button), "icon-name");
1147 }
1148
1149 /**
1150  * gtk_tool_button_get_icon_name
1151  * @button: a #GtkToolButton
1152  * 
1153  * Returns the name of the themed icon for the tool button,
1154  * see gtk_tool_button_set_icon_name().
1155  *
1156  * Returns: the icon name or %NULL if the tool button has
1157  * no themed icon
1158  * 
1159  * Since: 2.8
1160  **/
1161 G_CONST_RETURN gchar*
1162 gtk_tool_button_get_icon_name (GtkToolButton *button)
1163 {
1164   g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1165
1166   return button->priv->icon_name;
1167 }
1168
1169 /**
1170  * gtk_tool_button_set_icon_widget:
1171  * @button: a #GtkToolButton
1172  * @icon_widget: (allow-none): the widget used as icon, or %NULL
1173  *
1174  * Sets @icon as the widget used as icon on @button. If @icon_widget is
1175  * %NULL the icon is determined by the "stock_id" property. If the
1176  * "stock_id" property is also %NULL, @button will not have an icon.
1177  * 
1178  * Since: 2.4
1179  **/
1180 void
1181 gtk_tool_button_set_icon_widget (GtkToolButton *button,
1182                                  GtkWidget     *icon_widget)
1183 {
1184   g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
1185   g_return_if_fail (icon_widget == NULL || GTK_IS_WIDGET (icon_widget));
1186
1187   if (icon_widget != button->priv->icon_widget)
1188     {
1189       if (button->priv->icon_widget)
1190         {
1191           if (button->priv->icon_widget->parent)
1192             gtk_container_remove (GTK_CONTAINER (button->priv->icon_widget->parent),
1193                                     button->priv->icon_widget);
1194
1195           g_object_unref (button->priv->icon_widget);
1196         }
1197       
1198       if (icon_widget)
1199         g_object_ref_sink (icon_widget);
1200
1201       button->priv->icon_widget = icon_widget;
1202       button->priv->contents_invalid = TRUE;
1203       
1204       g_object_notify (G_OBJECT (button), "icon-widget");
1205     }
1206 }
1207
1208 /**
1209  * gtk_tool_button_set_label_widget:
1210  * @button: a #GtkToolButton
1211  * @label_widget: (allow-none): the widget used as label, or %NULL
1212  *
1213  * Sets @label_widget as the widget that will be used as the label
1214  * for @button. If @label_widget is %NULL the "label" property is used
1215  * as label. If "label" is also %NULL, the label in the stock item
1216  * determined by the "stock_id" property is used as label. If
1217  * "stock_id" is also %NULL, @button does not have a label.
1218  * 
1219  * Since: 2.4
1220  **/
1221 void
1222 gtk_tool_button_set_label_widget (GtkToolButton *button,
1223                                   GtkWidget     *label_widget)
1224 {
1225   g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
1226   g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1227
1228   if (label_widget != button->priv->label_widget)
1229     {
1230       if (button->priv->label_widget)
1231         {
1232           if (button->priv->label_widget->parent)
1233             gtk_container_remove (GTK_CONTAINER (button->priv->label_widget->parent),
1234                                   button->priv->label_widget);
1235           
1236           g_object_unref (button->priv->label_widget);
1237         }
1238       
1239       if (label_widget)
1240         g_object_ref_sink (label_widget);
1241
1242       button->priv->label_widget = label_widget;
1243       button->priv->contents_invalid = TRUE;
1244       
1245       g_object_notify (G_OBJECT (button), "label-widget");
1246     }
1247 }
1248
1249 /**
1250  * gtk_tool_button_get_label_widget:
1251  * @button: a #GtkToolButton
1252  * 
1253  * Returns the widget used as label on @button. See
1254  * gtk_tool_button_set_label_widget().
1255  * 
1256  * Return value: The widget used as label on @button, or %NULL.
1257  * 
1258  * Since: 2.4
1259  **/
1260 GtkWidget *
1261 gtk_tool_button_get_label_widget (GtkToolButton *button)
1262 {
1263   g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1264
1265   return button->priv->label_widget;
1266 }
1267
1268 /**
1269  * gtk_tool_button_get_icon_widget:
1270  * @button: a #GtkToolButton
1271  * 
1272  * Return the widget used as icon widget on @button. See
1273  * gtk_tool_button_set_icon_widget().
1274  * 
1275  * Return value: The widget used as icon on @button, or %NULL.
1276  * 
1277  * Since: 2.4
1278  **/
1279 GtkWidget *
1280 gtk_tool_button_get_icon_widget (GtkToolButton *button)
1281 {
1282   g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1283
1284   return button->priv->icon_widget;
1285 }
1286
1287 GtkWidget *
1288 _gtk_tool_button_get_button (GtkToolButton *button)
1289 {
1290   g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1291
1292   return button->priv->button;
1293 }
1294
1295
1296 #define __GTK_TOOL_BUTTON_C__
1297 #include "gtkaliasdef.c"