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>
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.
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.
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.
24 #include "gtktoolbutton.h"
25 #include "gtkbutton.h"
27 #include "gtkiconfactory.h"
29 #include "gtkimagemenuitem.h"
34 #include "gtktoolbar.h"
35 #include "gtkactivatable.h"
36 #include "gtkprivate.h"
41 #define MENU_ID "gtk-tool-button-menu-id"
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,
65 static void gtk_tool_button_get_property (GObject *object,
69 static void gtk_tool_button_property_notify (GObject *object,
71 static void gtk_tool_button_finalize (GObject *object);
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);
80 static void gtk_tool_button_construct_contents (GtkToolItem *tool_item);
82 static void gtk_tool_button_activatable_interface_init (GtkActivatableIface *iface);
83 static void gtk_tool_button_update (GtkActivatable *activatable,
85 const gchar *property_name);
86 static void gtk_tool_button_sync_action_properties (GtkActivatable *activatable,
90 struct _GtkToolButtonPrivate
97 GtkWidget *label_widget;
98 GtkWidget *icon_widget;
100 GtkSizeGroup *text_size_group;
102 guint use_underline : 1;
103 guint contents_invalid : 1;
106 static GObjectClass *parent_class = NULL;
107 static GtkActivatableIface *parent_activatable_iface;
108 static guint toolbutton_signals[LAST_SIGNAL] = { 0 };
110 #define GTK_TOOL_BUTTON_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_TOOL_BUTTON, GtkToolButtonPrivate))
113 gtk_tool_button_get_type (void)
115 static GType type = 0;
119 const GInterfaceInfo activatable_info =
121 (GInterfaceInitFunc) gtk_tool_button_activatable_interface_init,
122 (GInterfaceFinalizeFunc) NULL,
126 type = g_type_register_static_simple (GTK_TYPE_TOOL_ITEM,
128 sizeof (GtkToolButtonClass),
129 (GClassInitFunc) gtk_tool_button_class_init,
130 sizeof (GtkToolButton),
131 (GInstanceInitFunc) gtk_tool_button_init,
134 g_type_add_interface_static (type, GTK_TYPE_ACTIVATABLE,
141 gtk_tool_button_class_init (GtkToolButtonClass *klass)
143 GObjectClass *object_class;
144 GtkWidgetClass *widget_class;
145 GtkToolItemClass *tool_item_class;
147 parent_class = g_type_class_peek_parent (klass);
149 object_class = (GObjectClass *)klass;
150 widget_class = (GtkWidgetClass *)klass;
151 tool_item_class = (GtkToolItemClass *)klass;
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;
158 widget_class->style_set = gtk_tool_button_style_set;
160 tool_item_class->create_menu_proxy = gtk_tool_button_create_menu_proxy;
161 tool_item_class->toolbar_reconfigured = gtk_tool_button_toolbar_reconfigured;
163 klass->button_type = GTK_TYPE_BUTTON;
165 /* Properties are interpreted like this:
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.
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.
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).
186 * In that case, if use_underline is set,
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
192 * - an underscore indicates that the next character when
193 * used in the overflow menu should be used as a
196 * In short: use_underline = TRUE means that the label text has
197 * the form "_Open" and the toolbar should take appropriate
201 g_object_class_install_property (object_class,
203 g_param_spec_string ("label",
205 P_("Text to show in the item."),
207 GTK_PARAM_READWRITE));
208 g_object_class_install_property (object_class,
210 g_param_spec_boolean ("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"),
214 GTK_PARAM_READWRITE));
215 g_object_class_install_property (object_class,
217 g_param_spec_object ("label-widget",
219 P_("Widget to use as the item label"),
221 GTK_PARAM_READWRITE));
222 g_object_class_install_property (object_class,
224 g_param_spec_string ("stock-id",
226 P_("The stock icon displayed on the item"),
228 GTK_PARAM_READWRITE));
231 * GtkToolButton:icon-name:
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.
239 g_object_class_install_property (object_class,
241 g_param_spec_string ("icon-name",
243 P_("The name of the themed icon displayed on the item"),
245 GTK_PARAM_READWRITE));
246 g_object_class_install_property (object_class,
248 g_param_spec_object ("icon-widget",
250 P_("Icon widget to display in the item"),
252 GTK_PARAM_READWRITE));
255 * GtkButton:icon-spacing:
257 * Spacing in pixels between the icon and label.
261 gtk_widget_class_install_style_property (widget_class,
262 g_param_spec_int ("icon-spacing",
264 P_("Spacing in pixels between the icon and label"),
268 GTK_PARAM_READWRITE));
271 * GtkToolButton::clicked:
272 * @toolbutton: the object that emitted the signal
274 * This signal is emitted when the tool button is clicked with the mouse
275 * or activated with the keyboard.
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),
283 g_cclosure_marshal_VOID__VOID,
286 g_type_class_add_private (object_class, sizeof (GtkToolButtonPrivate));
290 gtk_tool_button_init (GtkToolButton *button,
291 GtkToolButtonClass *klass)
293 GtkToolItem *toolitem = GTK_TOOL_ITEM (button);
295 button->priv = GTK_TOOL_BUTTON_GET_PRIVATE (button);
297 button->priv->contents_invalid = TRUE;
299 gtk_tool_item_set_homogeneous (toolitem, TRUE);
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);
307 gtk_container_add (GTK_CONTAINER (button), button->priv->button);
308 gtk_widget_show (button->priv->button);
312 gtk_tool_button_construct_contents (GtkToolItem *tool_item)
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;
323 GtkOrientation text_orientation = GTK_ORIENTATION_HORIZONTAL;
324 GtkSizeGroup *size_group = NULL;
326 button->priv->contents_invalid = FALSE;
328 gtk_widget_style_get (GTK_WIDGET (tool_item),
329 "icon-spacing", &icon_spacing,
332 if (button->priv->icon_widget && button->priv->icon_widget->parent)
334 gtk_container_remove (GTK_CONTAINER (button->priv->icon_widget->parent),
335 button->priv->icon_widget);
338 if (button->priv->label_widget && button->priv->label_widget->parent)
340 gtk_container_remove (GTK_CONTAINER (button->priv->label_widget->parent),
341 button->priv->label_widget);
344 if (GTK_BIN (button->priv->button)->child)
346 /* Note: we are not destroying the label_widget or icon_widget
347 * here because they were removed from their containers above
349 gtk_widget_destroy (GTK_BIN (button->priv->button)->child);
352 style = gtk_tool_item_get_toolbar_style (GTK_TOOL_ITEM (button));
354 if (style != GTK_TOOLBAR_TEXT)
357 if (style != GTK_TOOLBAR_ICONS && style != GTK_TOOLBAR_BOTH_HORIZ)
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))
368 if (style == GTK_TOOLBAR_ICONS && button->priv->icon_widget == NULL &&
369 button->priv->stock_id == NULL && button->priv->icon_name == NULL)
373 style = GTK_TOOLBAR_TEXT;
376 if (style == GTK_TOOLBAR_TEXT && button->priv->label_widget == NULL &&
377 button->priv->stock_id == NULL && button->priv->label_text == NULL)
381 style = GTK_TOOLBAR_ICONS;
386 if (button->priv->label_widget)
388 label = button->priv->label_widget;
392 GtkStockItem stock_item;
396 if (button->priv->label_text)
398 label_text = button->priv->label_text;
399 elide = button->priv->use_underline;
401 else if (button->priv->stock_id && gtk_stock_lookup (button->priv->stock_id, &stock_item))
403 label_text = stock_item.label;
413 label_text = _gtk_toolbar_elide_underscores (label_text);
415 label_text = g_strdup (label_text);
417 label = gtk_label_new (label_text);
421 gtk_widget_show (label);
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)
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)),
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);
440 gtk_label_set_angle (GTK_LABEL (label), 90);
441 gtk_misc_set_alignment (GTK_MISC (label),
443 1 - gtk_tool_item_get_text_alignment (GTK_TOOL_ITEM (button)));
447 icon_size = gtk_tool_item_get_icon_size (GTK_TOOL_ITEM (button));
450 if (button->priv->icon_widget)
452 icon = button->priv->icon_widget;
454 if (GTK_IS_IMAGE (icon))
456 g_object_set (button->priv->icon_widget,
457 "icon-size", icon_size,
461 else if (button->priv->stock_id &&
462 gtk_icon_factory_lookup_default (button->priv->stock_id))
464 icon = gtk_image_new_from_stock (button->priv->stock_id, icon_size);
465 gtk_widget_show (icon);
467 else if (button->priv->icon_name)
469 icon = gtk_image_new_from_icon_name (button->priv->icon_name, icon_size);
470 gtk_widget_show (icon);
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)),
478 gtk_misc_set_alignment (GTK_MISC (icon),
480 gtk_tool_item_get_text_alignment (GTK_TOOL_ITEM (button)));
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);
492 case GTK_TOOLBAR_ICONS:
494 gtk_container_add (GTK_CONTAINER (button->priv->button), icon);
497 case GTK_TOOLBAR_BOTH:
498 if (text_orientation == GTK_ORIENTATION_HORIZONTAL)
499 box = gtk_vbox_new (FALSE, icon_spacing);
501 box = gtk_hbox_new (FALSE, icon_spacing);
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);
508 case GTK_TOOLBAR_BOTH_HORIZ:
509 if (text_orientation == GTK_ORIENTATION_HORIZONTAL)
511 box = gtk_hbox_new (FALSE, icon_spacing);
513 gtk_box_pack_start (GTK_BOX (box), icon, label? FALSE : TRUE, TRUE, 0);
515 gtk_box_pack_end (GTK_BOX (box), label, TRUE, TRUE, 0);
519 box = gtk_vbox_new (FALSE, icon_spacing);
521 gtk_box_pack_end (GTK_BOX (box), icon, label ? FALSE : TRUE, TRUE, 0);
523 gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
525 gtk_container_add (GTK_CONTAINER (button->priv->button), box);
528 case GTK_TOOLBAR_TEXT:
529 gtk_container_add (GTK_CONTAINER (button->priv->button), label);
534 gtk_widget_show (box);
536 gtk_button_set_relief (GTK_BUTTON (button->priv->button),
537 gtk_tool_item_get_relief_style (GTK_TOOL_ITEM (button)));
539 gtk_tool_item_rebuild_menu (tool_item);
541 gtk_widget_queue_resize (GTK_WIDGET (button));
545 gtk_tool_button_set_property (GObject *object,
550 GtkToolButton *button = GTK_TOOL_BUTTON (object);
555 gtk_tool_button_set_label (button, g_value_get_string (value));
557 case PROP_USE_UNDERLINE:
558 gtk_tool_button_set_use_underline (button, g_value_get_boolean (value));
560 case PROP_LABEL_WIDGET:
561 gtk_tool_button_set_label_widget (button, g_value_get_object (value));
564 gtk_tool_button_set_stock_id (button, g_value_get_string (value));
567 gtk_tool_button_set_icon_name (button, g_value_get_string (value));
569 case PROP_ICON_WIDGET:
570 gtk_tool_button_set_icon_widget (button, g_value_get_object (value));
573 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
579 gtk_tool_button_property_notify (GObject *object,
582 GtkToolButton *button = GTK_TOOL_BUTTON (object);
584 if (button->priv->contents_invalid ||
585 strcmp ("is-important", pspec->name) == 0)
586 gtk_tool_button_construct_contents (GTK_TOOL_ITEM (object));
588 if (parent_class->notify)
589 parent_class->notify (object, pspec);
593 gtk_tool_button_get_property (GObject *object,
598 GtkToolButton *button = GTK_TOOL_BUTTON (object);
603 g_value_set_string (value, gtk_tool_button_get_label (button));
605 case PROP_LABEL_WIDGET:
606 g_value_set_object (value, gtk_tool_button_get_label_widget (button));
608 case PROP_USE_UNDERLINE:
609 g_value_set_boolean (value, gtk_tool_button_get_use_underline (button));
612 g_value_set_string (value, button->priv->stock_id);
615 g_value_set_string (value, button->priv->icon_name);
617 case PROP_ICON_WIDGET:
618 g_value_set_object (value, button->priv->icon_widget);
621 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
627 gtk_tool_button_finalize (GObject *object)
629 GtkToolButton *button = GTK_TOOL_BUTTON (object);
631 g_free (button->priv->stock_id);
632 g_free (button->priv->icon_name);
633 g_free (button->priv->label_text);
635 if (button->priv->label_widget)
636 g_object_unref (button->priv->label_widget);
638 if (button->priv->icon_widget)
639 g_object_unref (button->priv->icon_widget);
641 parent_class->finalize (object);
645 clone_image_menu_size (GtkImage *image, GtkSettings *settings)
647 GtkImageType storage_type = gtk_image_get_storage_type (image);
649 if (storage_type == GTK_IMAGE_STOCK)
652 gtk_image_get_stock (image, &stock_id, NULL);
653 return gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
655 else if (storage_type == GTK_IMAGE_ICON_NAME)
657 const gchar *icon_name;
658 gtk_image_get_icon_name (image, &icon_name, NULL);
659 return gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
661 else if (storage_type == GTK_IMAGE_ICON_SET)
663 GtkIconSet *icon_set;
664 gtk_image_get_icon_set (image, &icon_set, NULL);
665 return gtk_image_new_from_icon_set (icon_set, GTK_ICON_SIZE_MENU);
667 else if (storage_type == GTK_IMAGE_GICON)
670 gtk_image_get_gicon (image, &icon, NULL);
671 return gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_MENU);
673 else if (storage_type == GTK_IMAGE_PIXBUF)
678 gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
681 GdkPixbuf *src_pixbuf, *dest_pixbuf;
682 GtkWidget *cloned_image;
684 src_pixbuf = gtk_image_get_pixbuf (image);
685 dest_pixbuf = gdk_pixbuf_scale_simple (src_pixbuf, width, height,
686 GDK_INTERP_BILINEAR);
688 cloned_image = gtk_image_new_from_pixbuf (dest_pixbuf);
689 g_object_unref (dest_pixbuf);
699 gtk_tool_button_create_menu_proxy (GtkToolItem *item)
701 GtkToolButton *button = GTK_TOOL_BUTTON (item);
702 GtkWidget *menu_item;
703 GtkWidget *menu_image = NULL;
704 GtkStockItem stock_item;
705 gboolean use_mnemonic = TRUE;
708 if (_gtk_tool_item_create_menu_proxy (item))
711 if (GTK_IS_LABEL (button->priv->label_widget))
713 label = gtk_label_get_label (GTK_LABEL (button->priv->label_widget));
714 use_mnemonic = gtk_label_get_use_underline (GTK_LABEL (button->priv->label_widget));
716 else if (button->priv->label_text)
718 label = button->priv->label_text;
719 use_mnemonic = button->priv->use_underline;
721 else if (button->priv->stock_id && gtk_stock_lookup (button->priv->stock_id, &stock_item))
723 label = stock_item.label;
731 menu_item = gtk_image_menu_item_new_with_mnemonic (label);
733 menu_item = gtk_image_menu_item_new_with_label (label);
735 if (GTK_IS_IMAGE (button->priv->icon_widget))
737 menu_image = clone_image_menu_size (GTK_IMAGE (button->priv->icon_widget),
738 gtk_widget_get_settings (GTK_WIDGET (button)));
740 else if (button->priv->stock_id)
742 menu_image = gtk_image_new_from_stock (button->priv->stock_id, GTK_ICON_SIZE_MENU);
746 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item), menu_image);
748 g_signal_connect_closure_by_id (menu_item,
749 g_signal_lookup ("activate", G_OBJECT_TYPE (menu_item)), 0,
750 g_cclosure_new_object_swap (G_CALLBACK (gtk_button_clicked),
751 G_OBJECT (GTK_TOOL_BUTTON (button)->priv->button)),
754 gtk_tool_item_set_proxy_menu_item (GTK_TOOL_ITEM (button), MENU_ID, menu_item);
760 button_clicked (GtkWidget *widget,
761 GtkToolButton *button)
765 action = gtk_activatable_get_related_action (GTK_ACTIVATABLE (button));
768 gtk_action_activate (action);
770 g_signal_emit_by_name (button, "clicked");
774 gtk_tool_button_toolbar_reconfigured (GtkToolItem *tool_item)
776 gtk_tool_button_construct_contents (tool_item);
780 gtk_tool_button_update_icon_spacing (GtkToolButton *button)
785 box = GTK_BIN (button->priv->button)->child;
786 if (GTK_IS_BOX (box))
788 gtk_widget_style_get (GTK_WIDGET (button),
789 "icon-spacing", &spacing,
791 gtk_box_set_spacing (GTK_BOX (box), spacing);
796 gtk_tool_button_style_set (GtkWidget *widget,
797 GtkStyle *prev_style)
799 gtk_tool_button_update_icon_spacing (GTK_TOOL_BUTTON (widget));
803 gtk_tool_button_activatable_interface_init (GtkActivatableIface *iface)
805 parent_activatable_iface = g_type_interface_peek_parent (iface);
806 iface->update = gtk_tool_button_update;
807 iface->sync_action_properties = gtk_tool_button_sync_action_properties;
811 gtk_tool_button_update (GtkActivatable *activatable,
813 const gchar *property_name)
815 GtkToolButton *button;
818 parent_activatable_iface->update (activatable, action, property_name);
820 if (!gtk_activatable_get_use_action_appearance (activatable))
823 button = GTK_TOOL_BUTTON (activatable);
825 if (strcmp (property_name, "short-label") == 0)
826 gtk_tool_button_set_label (button, gtk_action_get_short_label (action));
827 else if (strcmp (property_name, "stock-id") == 0)
828 gtk_tool_button_set_stock_id (button, gtk_action_get_stock_id (action));
829 else if (strcmp (property_name, "gicon") == 0)
831 const gchar *stock_id = gtk_action_get_stock_id (action);
832 GIcon *icon = gtk_action_get_gicon (action);
833 GtkIconSize icon_size = GTK_ICON_SIZE_BUTTON;
835 if ((stock_id && gtk_icon_factory_lookup_default (stock_id)) || !icon)
839 image = gtk_tool_button_get_icon_widget (button);
840 icon_size = gtk_tool_item_get_icon_size (GTK_TOOL_ITEM (button));
843 image = gtk_image_new ();
846 gtk_tool_button_set_icon_widget (button, image);
847 gtk_image_set_from_gicon (GTK_IMAGE (image), icon, icon_size);
850 else if (strcmp (property_name, "icon-name") == 0)
851 gtk_tool_button_set_icon_name (button, gtk_action_get_icon_name (action));
855 gtk_tool_button_sync_action_properties (GtkActivatable *activatable,
858 GtkToolButton *button;
860 const gchar *stock_id;
862 parent_activatable_iface->sync_action_properties (activatable, action);
867 if (!gtk_activatable_get_use_action_appearance (activatable))
870 button = GTK_TOOL_BUTTON (activatable);
871 stock_id = gtk_action_get_stock_id (action);
873 gtk_tool_button_set_label (button, gtk_action_get_short_label (action));
874 gtk_tool_button_set_use_underline (button, TRUE);
875 gtk_tool_button_set_stock_id (button, stock_id);
876 gtk_tool_button_set_icon_name (button, gtk_action_get_icon_name (action));
878 if (stock_id && gtk_icon_factory_lookup_default (stock_id))
879 gtk_tool_button_set_icon_widget (button, NULL);
880 else if ((icon = gtk_action_get_gicon (action)) != NULL)
882 GtkIconSize icon_size = gtk_tool_item_get_icon_size (GTK_TOOL_ITEM (button));
883 GtkWidget *image = gtk_tool_button_get_icon_widget (button);
887 image = gtk_image_new ();
888 gtk_widget_show (image);
889 gtk_tool_button_set_icon_widget (button, image);
892 gtk_image_set_from_gicon (GTK_IMAGE (image), icon, icon_size);
894 else if (gtk_action_get_icon_name (action))
895 gtk_tool_button_set_icon_name (button, gtk_action_get_icon_name (action));
897 gtk_tool_button_set_label (button, gtk_action_get_short_label (action));
901 * gtk_tool_button_new_from_stock:
902 * @stock_id: the name of the stock item
904 * Creates a new #GtkToolButton containing the image and text from a
905 * stock item. Some stock ids have preprocessor macros like #GTK_STOCK_OK
906 * and #GTK_STOCK_APPLY.
908 * It is an error if @stock_id is not a name of a stock item.
910 * Return value: A new #GtkToolButton
915 gtk_tool_button_new_from_stock (const gchar *stock_id)
917 GtkToolButton *button;
919 g_return_val_if_fail (stock_id != NULL, NULL);
921 button = g_object_new (GTK_TYPE_TOOL_BUTTON,
922 "stock-id", stock_id,
925 return GTK_TOOL_ITEM (button);
929 * gtk_tool_button_new:
930 * @label: (allow-none): a string that will be used as label, or %NULL
931 * @icon_widget: (allow-none): a #GtkMisc widget that will be used as icon widget, or %NULL
933 * Creates a new %GtkToolButton using @icon_widget as icon and @label as
936 * Return value: A new #GtkToolButton
941 gtk_tool_button_new (GtkWidget *icon_widget,
944 GtkToolButton *button;
946 g_return_val_if_fail (icon_widget == NULL || GTK_IS_MISC (icon_widget), NULL);
948 button = g_object_new (GTK_TYPE_TOOL_BUTTON,
950 "icon-widget", icon_widget,
953 return GTK_TOOL_ITEM (button);
957 * gtk_tool_button_set_label:
958 * @button: a #GtkToolButton
959 * @label: (allow-none): a string that will be used as label, or %NULL.
961 * Sets @label as the label used for the tool button. The "label" property
962 * only has an effect if not overridden by a non-%NULL "label_widget" property.
963 * If both the "label_widget" and "label" properties are %NULL, the label
964 * is determined by the "stock_id" property. If the "stock_id" property is also
965 * %NULL, @button will not have a label.
970 gtk_tool_button_set_label (GtkToolButton *button,
975 AtkObject *accessible;
977 g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
979 old_label = button->priv->label_text;
981 button->priv->label_text = g_strdup (label);
982 button->priv->contents_invalid = TRUE;
986 elided_label = _gtk_toolbar_elide_underscores (label);
987 accessible = gtk_widget_get_accessible (GTK_WIDGET (button->priv->button));
988 atk_object_set_name (accessible, elided_label);
989 g_free (elided_label);
994 g_object_notify (G_OBJECT (button), "label");
998 * gtk_tool_button_get_label:
999 * @button: a #GtkToolButton
1001 * Returns the label used by the tool button, or %NULL if the tool button
1002 * doesn't have a label. or uses a the label from a stock item. The returned
1003 * string is owned by GTK+, and must not be modified or freed.
1005 * Return value: The label, or %NULL
1009 G_CONST_RETURN gchar *
1010 gtk_tool_button_get_label (GtkToolButton *button)
1012 g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1014 return button->priv->label_text;
1018 * gtk_tool_button_set_use_underline:
1019 * @button: a #GtkToolButton
1020 * @use_underline: whether the button label has the form "_Open"
1022 * If set, an underline in the label property indicates that the next character
1023 * should be used for the mnemonic accelerator key in the overflow menu. For
1024 * example, if the label property is "_Open" and @use_underline is %TRUE,
1025 * the label on the tool button will be "Open" and the item on the overflow
1026 * menu will have an underlined 'O'.
1028 * Labels shown on tool buttons never have mnemonics on them; this property
1029 * only affects the menu item on the overflow menu.
1034 gtk_tool_button_set_use_underline (GtkToolButton *button,
1035 gboolean use_underline)
1037 g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
1039 use_underline = use_underline != FALSE;
1041 if (use_underline != button->priv->use_underline)
1043 button->priv->use_underline = use_underline;
1044 button->priv->contents_invalid = TRUE;
1046 g_object_notify (G_OBJECT (button), "use-underline");
1051 * gtk_tool_button_get_use_underline:
1052 * @button: a #GtkToolButton
1054 * Returns whether underscores in the label property are used as mnemonics
1055 * on menu items on the overflow menu. See gtk_tool_button_set_use_underline().
1057 * Return value: %TRUE if underscores in the label property are used as
1058 * mnemonics on menu items on the overflow menu.
1063 gtk_tool_button_get_use_underline (GtkToolButton *button)
1065 g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), FALSE);
1067 return button->priv->use_underline;
1071 * gtk_tool_button_set_stock_id:
1072 * @button: a #GtkToolButton
1073 * @stock_id: (allow-none): a name of a stock item, or %NULL
1075 * Sets the name of the stock item. See gtk_tool_button_new_from_stock().
1076 * The stock_id property only has an effect if not
1077 * overridden by non-%NULL "label" and "icon_widget" properties.
1082 gtk_tool_button_set_stock_id (GtkToolButton *button,
1083 const gchar *stock_id)
1085 gchar *old_stock_id;
1087 g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
1089 old_stock_id = button->priv->stock_id;
1091 button->priv->stock_id = g_strdup (stock_id);
1092 button->priv->contents_invalid = TRUE;
1094 g_free (old_stock_id);
1096 g_object_notify (G_OBJECT (button), "stock-id");
1100 * gtk_tool_button_get_stock_id:
1101 * @button: a #GtkToolButton
1103 * Returns the name of the stock item. See gtk_tool_button_set_stock_id().
1104 * The returned string is owned by GTK+ and must not be freed or modifed.
1106 * Return value: the name of the stock item for @button.
1110 G_CONST_RETURN gchar *
1111 gtk_tool_button_get_stock_id (GtkToolButton *button)
1113 g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1115 return button->priv->stock_id;
1119 * gtk_tool_button_set_icon_name
1120 * @button: a #GtkToolButton
1121 * @icon_name: (allow-none): the name of the themed icon
1123 * Sets the icon for the tool button from a named themed icon.
1124 * See the docs for #GtkIconTheme for more details.
1125 * The "icon_name" property only has an effect if not
1126 * overridden by non-%NULL "label", "icon_widget" and "stock_id"
1132 gtk_tool_button_set_icon_name (GtkToolButton *button,
1133 const gchar *icon_name)
1135 gchar *old_icon_name;
1137 g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
1139 old_icon_name = button->priv->icon_name;
1141 button->priv->icon_name = g_strdup (icon_name);
1142 button->priv->contents_invalid = TRUE;
1144 g_free (old_icon_name);
1146 g_object_notify (G_OBJECT (button), "icon-name");
1150 * gtk_tool_button_get_icon_name
1151 * @button: a #GtkToolButton
1153 * Returns the name of the themed icon for the tool button,
1154 * see gtk_tool_button_set_icon_name().
1156 * Returns: the icon name or %NULL if the tool button has
1161 G_CONST_RETURN gchar*
1162 gtk_tool_button_get_icon_name (GtkToolButton *button)
1164 g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1166 return button->priv->icon_name;
1170 * gtk_tool_button_set_icon_widget:
1171 * @button: a #GtkToolButton
1172 * @icon_widget: (allow-none): the widget used as icon, or %NULL
1174 * Sets @icon as the widget used as icon on @button. If @icon_widget is
1175 * %NULL the icon is determined by the "stock_id" property. If the
1176 * "stock_id" property is also %NULL, @button will not have an icon.
1181 gtk_tool_button_set_icon_widget (GtkToolButton *button,
1182 GtkWidget *icon_widget)
1184 g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
1185 g_return_if_fail (icon_widget == NULL || GTK_IS_WIDGET (icon_widget));
1187 if (icon_widget != button->priv->icon_widget)
1189 if (button->priv->icon_widget)
1191 if (button->priv->icon_widget->parent)
1192 gtk_container_remove (GTK_CONTAINER (button->priv->icon_widget->parent),
1193 button->priv->icon_widget);
1195 g_object_unref (button->priv->icon_widget);
1199 g_object_ref_sink (icon_widget);
1201 button->priv->icon_widget = icon_widget;
1202 button->priv->contents_invalid = TRUE;
1204 g_object_notify (G_OBJECT (button), "icon-widget");
1209 * gtk_tool_button_set_label_widget:
1210 * @button: a #GtkToolButton
1211 * @label_widget: (allow-none): the widget used as label, or %NULL
1213 * Sets @label_widget as the widget that will be used as the label
1214 * for @button. If @label_widget is %NULL the "label" property is used
1215 * as label. If "label" is also %NULL, the label in the stock item
1216 * determined by the "stock_id" property is used as label. If
1217 * "stock_id" is also %NULL, @button does not have a label.
1222 gtk_tool_button_set_label_widget (GtkToolButton *button,
1223 GtkWidget *label_widget)
1225 g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
1226 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1228 if (label_widget != button->priv->label_widget)
1230 if (button->priv->label_widget)
1232 if (button->priv->label_widget->parent)
1233 gtk_container_remove (GTK_CONTAINER (button->priv->label_widget->parent),
1234 button->priv->label_widget);
1236 g_object_unref (button->priv->label_widget);
1240 g_object_ref_sink (label_widget);
1242 button->priv->label_widget = label_widget;
1243 button->priv->contents_invalid = TRUE;
1245 g_object_notify (G_OBJECT (button), "label-widget");
1250 * gtk_tool_button_get_label_widget:
1251 * @button: a #GtkToolButton
1253 * Returns the widget used as label on @button. See
1254 * gtk_tool_button_set_label_widget().
1256 * Return value: The widget used as label on @button, or %NULL.
1261 gtk_tool_button_get_label_widget (GtkToolButton *button)
1263 g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1265 return button->priv->label_widget;
1269 * gtk_tool_button_get_icon_widget:
1270 * @button: a #GtkToolButton
1272 * Return the widget used as icon widget on @button. See
1273 * gtk_tool_button_set_icon_widget().
1275 * Return value: The widget used as icon on @button, or %NULL.
1280 gtk_tool_button_get_icon_widget (GtkToolButton *button)
1282 g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1284 return button->priv->icon_widget;
1288 _gtk_tool_button_get_button (GtkToolButton *button)
1290 g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1292 return button->priv->button;
1296 #define __GTK_TOOL_BUTTON_C__
1297 #include "gtkaliasdef.c"