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