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