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, see <http://www.gnu.org/licenses/>.
22 #include "gtktoolbutton.h"
23 #include "gtkbutton.h"
24 #include "gtkiconfactory.h"
26 #include "gtkimagemenuitem.h"
31 #include "gtktoolbar.h"
32 #include "gtkactivatable.h"
33 #include "gtkactionable.h"
34 #include "gtkprivate.h"
40 * SECTION:gtktoolbutton
41 * @Short_description: A GtkToolItem subclass that displays buttons
42 * @Title: GtkToolButton
43 * @See_also: #GtkToolbar, #GtkMenuToolButton, #GtkToggleToolButton,
44 * #GtkRadioToolButton, #GtkSeparatorToolItem
46 * #GtkToolButton<!-- -->s are #GtkToolItems containing buttons.
48 * Use gtk_tool_button_new() to create a new #GtkToolButton. Use
49 * gtk_tool_button_new_from_stock() to create a #GtkToolButton
50 * containing a stock item.
52 * The label of a #GtkToolButton is determined by the properties
53 * #GtkToolButton:label-widget, #GtkToolButton:label, and
54 * #GtkToolButton:stock-id. If #GtkToolButton:label-widget is
55 * non-%NULL, then that widget is used as the label. Otherwise, if
56 * #GtkToolButton:label is non-%NULL, that string is used as the label.
57 * Otherwise, if #GtkToolButton:stock-id is non-%NULL, the label is
58 * determined by the stock item. Otherwise, the button does not have a label.
60 * The icon of a #GtkToolButton is determined by the properties
61 * #GtkToolButton:icon-widget and #GtkToolButton:stock-id. If
62 * #GtkToolButton:icon-widget is non-%NULL, then
63 * that widget is used as the icon. Otherwise, if #GtkToolButton:stock-id is
64 * non-%NULL, the icon is determined by the stock item. Otherwise,
65 * the button does not have a icon.
69 #define MENU_ID "gtk-tool-button-menu-id"
88 static void gtk_tool_button_init (GtkToolButton *button,
89 GtkToolButtonClass *klass);
90 static void gtk_tool_button_class_init (GtkToolButtonClass *klass);
91 static void gtk_tool_button_set_property (GObject *object,
95 static void gtk_tool_button_get_property (GObject *object,
99 static void gtk_tool_button_property_notify (GObject *object,
101 static void gtk_tool_button_finalize (GObject *object);
103 static void gtk_tool_button_toolbar_reconfigured (GtkToolItem *tool_item);
104 static gboolean gtk_tool_button_create_menu_proxy (GtkToolItem *item);
105 static void button_clicked (GtkWidget *widget,
106 GtkToolButton *button);
107 static void gtk_tool_button_style_updated (GtkWidget *widget);
109 static void gtk_tool_button_construct_contents (GtkToolItem *tool_item);
111 static void gtk_tool_button_actionable_iface_init (GtkActionableInterface *iface);
112 static void gtk_tool_button_activatable_interface_init (GtkActivatableIface *iface);
113 static void gtk_tool_button_update (GtkActivatable *activatable,
115 const gchar *property_name);
116 static void gtk_tool_button_sync_action_properties (GtkActivatable *activatable,
120 struct _GtkToolButtonPrivate
127 GtkWidget *label_widget;
128 GtkWidget *icon_widget;
130 GtkSizeGroup *text_size_group;
132 guint use_underline : 1;
133 guint contents_invalid : 1;
136 static GObjectClass *parent_class = NULL;
137 static GtkActivatableIface *parent_activatable_iface;
138 static guint toolbutton_signals[LAST_SIGNAL] = { 0 };
141 gtk_tool_button_get_type (void)
143 static GType type = 0;
147 const GInterfaceInfo actionable_info =
149 (GInterfaceInitFunc) gtk_tool_button_actionable_iface_init,
150 (GInterfaceFinalizeFunc) NULL,
153 const GInterfaceInfo activatable_info =
155 (GInterfaceInitFunc) gtk_tool_button_activatable_interface_init,
156 (GInterfaceFinalizeFunc) NULL,
160 type = g_type_register_static_simple (GTK_TYPE_TOOL_ITEM,
162 sizeof (GtkToolButtonClass),
163 (GClassInitFunc) gtk_tool_button_class_init,
164 sizeof (GtkToolButton),
165 (GInstanceInitFunc) gtk_tool_button_init,
168 g_type_add_interface_static (type, GTK_TYPE_ACTIONABLE, &actionable_info);
169 g_type_add_interface_static (type, GTK_TYPE_ACTIVATABLE,
176 gtk_tool_button_class_init (GtkToolButtonClass *klass)
178 GObjectClass *object_class;
179 GtkWidgetClass *widget_class;
180 GtkToolItemClass *tool_item_class;
182 parent_class = g_type_class_peek_parent (klass);
184 object_class = (GObjectClass *)klass;
185 widget_class = (GtkWidgetClass *)klass;
186 tool_item_class = (GtkToolItemClass *)klass;
188 object_class->set_property = gtk_tool_button_set_property;
189 object_class->get_property = gtk_tool_button_get_property;
190 object_class->notify = gtk_tool_button_property_notify;
191 object_class->finalize = gtk_tool_button_finalize;
193 widget_class->style_updated = gtk_tool_button_style_updated;
195 tool_item_class->create_menu_proxy = gtk_tool_button_create_menu_proxy;
196 tool_item_class->toolbar_reconfigured = gtk_tool_button_toolbar_reconfigured;
198 klass->button_type = GTK_TYPE_BUTTON;
200 /* Properties are interpreted like this:
202 * - if the tool button has an icon_widget, then that widget
203 * will be used as the icon. Otherwise, if the tool button
204 * has a stock id, the corresponding stock icon will be
205 * used. Otherwise, if the tool button has an icon name,
206 * the corresponding icon from the theme will be used.
207 * Otherwise, the tool button will not have an icon.
209 * - if the tool button has a label_widget then that widget
210 * will be used as the label. Otherwise, if the tool button
211 * has a label text, that text will be used as label. Otherwise,
212 * if the toolbutton has a stock id, the corresponding text
213 * will be used as label. Otherwise, if the tool button has
214 * an icon name, the corresponding icon name from the theme will
215 * be used. Otherwise, the toolbutton will have an empty label.
217 * - The use_underline property only has an effect when the label
218 * on the toolbutton comes from the label property (ie. not from
219 * label_widget or from stock_id).
221 * In that case, if use_underline is set,
223 * - underscores are removed from the label text before
224 * the label is shown on the toolbutton unless the
225 * underscore is followed by another underscore
227 * - an underscore indicates that the next character when
228 * used in the overflow menu should be used as a
231 * In short: use_underline = TRUE means that the label text has
232 * the form "_Open" and the toolbar should take appropriate
236 g_object_class_install_property (object_class,
238 g_param_spec_string ("label",
240 P_("Text to show in the item."),
242 GTK_PARAM_READWRITE));
243 g_object_class_install_property (object_class,
245 g_param_spec_boolean ("use-underline",
247 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"),
249 GTK_PARAM_READWRITE));
250 g_object_class_install_property (object_class,
252 g_param_spec_object ("label-widget",
254 P_("Widget to use as the item label"),
256 GTK_PARAM_READWRITE));
257 g_object_class_install_property (object_class,
259 g_param_spec_string ("stock-id",
261 P_("The stock icon displayed on the item"),
263 GTK_PARAM_READWRITE));
266 * GtkToolButton:icon-name:
268 * The name of the themed icon displayed on the item.
269 * This property only has an effect if not overridden by "label",
270 * "icon_widget" or "stock_id" properties.
274 g_object_class_install_property (object_class,
276 g_param_spec_string ("icon-name",
278 P_("The name of the themed icon displayed on the item"),
280 GTK_PARAM_READWRITE));
281 g_object_class_install_property (object_class,
283 g_param_spec_object ("icon-widget",
285 P_("Icon widget to display in the item"),
287 GTK_PARAM_READWRITE));
289 g_object_class_override_property (object_class, PROP_ACTION_NAME, "action-name");
290 g_object_class_override_property (object_class, PROP_ACTION_TARGET, "action-target");
293 * GtkButton:icon-spacing:
295 * Spacing in pixels between the icon and label.
299 gtk_widget_class_install_style_property (widget_class,
300 g_param_spec_int ("icon-spacing",
302 P_("Spacing in pixels between the icon and label"),
306 GTK_PARAM_READWRITE));
309 * GtkToolButton::clicked:
310 * @toolbutton: the object that emitted the signal
312 * This signal is emitted when the tool button is clicked with the mouse
313 * or activated with the keyboard.
315 toolbutton_signals[CLICKED] =
316 g_signal_new (I_("clicked"),
317 G_OBJECT_CLASS_TYPE (klass),
318 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
319 G_STRUCT_OFFSET (GtkToolButtonClass, clicked),
321 g_cclosure_marshal_VOID__VOID,
324 g_type_class_add_private (object_class, sizeof (GtkToolButtonPrivate));
328 gtk_tool_button_init (GtkToolButton *button,
329 GtkToolButtonClass *klass)
331 GtkToolItem *toolitem = GTK_TOOL_ITEM (button);
333 button->priv = G_TYPE_INSTANCE_GET_PRIVATE (button,
334 GTK_TYPE_TOOL_BUTTON,
335 GtkToolButtonPrivate);
337 button->priv->contents_invalid = TRUE;
339 gtk_tool_item_set_homogeneous (toolitem, TRUE);
342 button->priv->button = g_object_new (klass->button_type, NULL);
343 gtk_button_set_focus_on_click (GTK_BUTTON (button->priv->button), FALSE);
344 g_signal_connect_object (button->priv->button, "clicked",
345 G_CALLBACK (button_clicked), button, 0);
347 gtk_container_add (GTK_CONTAINER (button), button->priv->button);
348 gtk_widget_show (button->priv->button);
352 gtk_tool_button_construct_contents (GtkToolItem *tool_item)
354 GtkToolButton *button = GTK_TOOL_BUTTON (tool_item);
356 GtkWidget *label = NULL;
357 GtkWidget *icon = NULL;
358 GtkToolbarStyle style;
359 gboolean need_label = FALSE;
360 gboolean need_icon = FALSE;
361 GtkIconSize icon_size;
362 GtkWidget *box = NULL;
364 GtkOrientation text_orientation = GTK_ORIENTATION_HORIZONTAL;
365 GtkSizeGroup *size_group = NULL;
368 button->priv->contents_invalid = FALSE;
370 gtk_widget_style_get (GTK_WIDGET (tool_item),
371 "icon-spacing", &icon_spacing,
374 if (button->priv->icon_widget)
376 parent = gtk_widget_get_parent (button->priv->icon_widget);
379 gtk_container_remove (GTK_CONTAINER (parent),
380 button->priv->icon_widget);
384 if (button->priv->label_widget)
386 parent = gtk_widget_get_parent (button->priv->label_widget);
389 gtk_container_remove (GTK_CONTAINER (parent),
390 button->priv->label_widget);
394 child = gtk_bin_get_child (GTK_BIN (button->priv->button));
397 /* Note: we are not destroying the label_widget or icon_widget
398 * here because they were removed from their containers above
400 gtk_widget_destroy (child);
403 style = gtk_tool_item_get_toolbar_style (GTK_TOOL_ITEM (button));
405 if (style != GTK_TOOLBAR_TEXT)
408 if (style != GTK_TOOLBAR_ICONS && style != GTK_TOOLBAR_BOTH_HORIZ)
411 if (style == GTK_TOOLBAR_BOTH_HORIZ &&
412 (gtk_tool_item_get_is_important (GTK_TOOL_ITEM (button)) ||
413 gtk_tool_item_get_orientation (GTK_TOOL_ITEM (button)) == GTK_ORIENTATION_VERTICAL ||
414 gtk_tool_item_get_text_orientation (GTK_TOOL_ITEM (button)) == GTK_ORIENTATION_VERTICAL))
419 if (style == GTK_TOOLBAR_ICONS && button->priv->icon_widget == NULL &&
420 button->priv->stock_id == NULL && button->priv->icon_name == NULL)
424 style = GTK_TOOLBAR_TEXT;
427 if (style == GTK_TOOLBAR_TEXT && button->priv->label_widget == NULL &&
428 button->priv->stock_id == NULL && button->priv->label_text == NULL)
432 style = GTK_TOOLBAR_ICONS;
437 if (button->priv->label_widget)
439 label = button->priv->label_widget;
443 GtkStockItem stock_item;
447 if (button->priv->label_text)
449 label_text = button->priv->label_text;
450 elide = button->priv->use_underline;
452 else if (button->priv->stock_id && gtk_stock_lookup (button->priv->stock_id, &stock_item))
454 label_text = stock_item.label;
464 label_text = _gtk_toolbar_elide_underscores (label_text);
466 label_text = g_strdup (label_text);
468 label = gtk_label_new (label_text);
472 gtk_widget_show (label);
475 if (GTK_IS_LABEL (label))
477 gtk_label_set_ellipsize (GTK_LABEL (label),
478 gtk_tool_item_get_ellipsize_mode (GTK_TOOL_ITEM (button)));
479 text_orientation = gtk_tool_item_get_text_orientation (GTK_TOOL_ITEM (button));
480 if (text_orientation == GTK_ORIENTATION_HORIZONTAL)
482 gtk_label_set_angle (GTK_LABEL (label), 0);
483 gtk_misc_set_alignment (GTK_MISC (label),
484 gtk_tool_item_get_text_alignment (GTK_TOOL_ITEM (button)),
489 gtk_label_set_ellipsize (GTK_LABEL (label), PANGO_ELLIPSIZE_NONE);
490 if (gtk_widget_get_direction (GTK_WIDGET (tool_item)) == GTK_TEXT_DIR_RTL)
491 gtk_label_set_angle (GTK_LABEL (label), -90);
493 gtk_label_set_angle (GTK_LABEL (label), 90);
494 gtk_misc_set_alignment (GTK_MISC (label),
496 1 - gtk_tool_item_get_text_alignment (GTK_TOOL_ITEM (button)));
501 icon_size = gtk_tool_item_get_icon_size (GTK_TOOL_ITEM (button));
504 if (button->priv->icon_widget)
506 icon = button->priv->icon_widget;
508 if (GTK_IS_IMAGE (icon))
510 g_object_set (button->priv->icon_widget,
511 "icon-size", icon_size,
515 else if (button->priv->stock_id &&
516 gtk_icon_factory_lookup_default (button->priv->stock_id))
518 icon = gtk_image_new_from_stock (button->priv->stock_id, icon_size);
519 gtk_widget_show (icon);
521 else if (button->priv->icon_name)
523 icon = gtk_image_new_from_icon_name (button->priv->icon_name, icon_size);
524 gtk_widget_show (icon);
527 if (GTK_IS_MISC (icon) && text_orientation == GTK_ORIENTATION_HORIZONTAL)
528 gtk_misc_set_alignment (GTK_MISC (icon),
529 1.0 - gtk_tool_item_get_text_alignment (GTK_TOOL_ITEM (button)),
531 else if (GTK_IS_MISC (icon))
532 gtk_misc_set_alignment (GTK_MISC (icon),
534 gtk_tool_item_get_text_alignment (GTK_TOOL_ITEM (button)));
538 size_group = gtk_tool_item_get_text_size_group (GTK_TOOL_ITEM (button));
539 if (size_group != NULL)
540 gtk_size_group_add_widget (size_group, icon);
546 case GTK_TOOLBAR_ICONS:
548 gtk_container_add (GTK_CONTAINER (button->priv->button), icon);
551 case GTK_TOOLBAR_BOTH:
552 if (text_orientation == GTK_ORIENTATION_HORIZONTAL)
553 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, icon_spacing);
555 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, icon_spacing);
557 gtk_box_pack_start (GTK_BOX (box), icon, TRUE, TRUE, 0);
558 gtk_box_pack_end (GTK_BOX (box), label, FALSE, TRUE, 0);
559 gtk_container_add (GTK_CONTAINER (button->priv->button), box);
562 case GTK_TOOLBAR_BOTH_HORIZ:
563 if (text_orientation == GTK_ORIENTATION_HORIZONTAL)
565 box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, icon_spacing);
567 gtk_box_pack_start (GTK_BOX (box), icon, label? FALSE : TRUE, TRUE, 0);
569 gtk_box_pack_end (GTK_BOX (box), label, TRUE, TRUE, 0);
573 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, icon_spacing);
575 gtk_box_pack_end (GTK_BOX (box), icon, label ? FALSE : TRUE, TRUE, 0);
577 gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
579 gtk_container_add (GTK_CONTAINER (button->priv->button), box);
582 case GTK_TOOLBAR_TEXT:
583 gtk_container_add (GTK_CONTAINER (button->priv->button), label);
588 gtk_widget_show (box);
590 gtk_button_set_relief (GTK_BUTTON (button->priv->button),
591 gtk_tool_item_get_relief_style (GTK_TOOL_ITEM (button)));
593 gtk_tool_item_rebuild_menu (tool_item);
595 gtk_widget_queue_resize (GTK_WIDGET (button));
599 gtk_tool_button_set_property (GObject *object,
604 GtkToolButton *button = GTK_TOOL_BUTTON (object);
609 gtk_tool_button_set_label (button, g_value_get_string (value));
611 case PROP_USE_UNDERLINE:
612 gtk_tool_button_set_use_underline (button, g_value_get_boolean (value));
614 case PROP_LABEL_WIDGET:
615 gtk_tool_button_set_label_widget (button, g_value_get_object (value));
618 gtk_tool_button_set_stock_id (button, g_value_get_string (value));
621 gtk_tool_button_set_icon_name (button, g_value_get_string (value));
623 case PROP_ICON_WIDGET:
624 gtk_tool_button_set_icon_widget (button, g_value_get_object (value));
626 case PROP_ACTION_NAME:
627 g_object_set_property (G_OBJECT (button->priv->button), "action-name", value);
629 case PROP_ACTION_TARGET:
630 g_object_set_property (G_OBJECT (button->priv->button), "action-target", value);
633 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
639 gtk_tool_button_property_notify (GObject *object,
642 GtkToolButton *button = GTK_TOOL_BUTTON (object);
644 if (button->priv->contents_invalid ||
645 strcmp ("is-important", pspec->name) == 0)
646 gtk_tool_button_construct_contents (GTK_TOOL_ITEM (object));
648 if (parent_class->notify)
649 parent_class->notify (object, pspec);
653 gtk_tool_button_get_property (GObject *object,
658 GtkToolButton *button = GTK_TOOL_BUTTON (object);
663 g_value_set_string (value, gtk_tool_button_get_label (button));
665 case PROP_LABEL_WIDGET:
666 g_value_set_object (value, gtk_tool_button_get_label_widget (button));
668 case PROP_USE_UNDERLINE:
669 g_value_set_boolean (value, gtk_tool_button_get_use_underline (button));
672 g_value_set_string (value, button->priv->stock_id);
675 g_value_set_string (value, button->priv->icon_name);
677 case PROP_ICON_WIDGET:
678 g_value_set_object (value, button->priv->icon_widget);
680 case PROP_ACTION_NAME:
681 g_object_get_property (G_OBJECT (button->priv->button), "action-name", value);
683 case PROP_ACTION_TARGET:
684 g_object_get_property (G_OBJECT (button->priv->button), "action-target", value);
687 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
693 gtk_tool_button_get_action_name (GtkActionable *actionable)
695 GtkToolButton *button = GTK_TOOL_BUTTON (actionable);
697 return gtk_actionable_get_action_name (GTK_ACTIONABLE (button->priv->button));
701 gtk_tool_button_set_action_name (GtkActionable *actionable,
702 const gchar *action_name)
704 GtkToolButton *button = GTK_TOOL_BUTTON (actionable);
706 gtk_actionable_set_action_name (GTK_ACTIONABLE (button->priv->button), action_name);
710 gtk_tool_button_get_action_target_value (GtkActionable *actionable)
712 GtkToolButton *button = GTK_TOOL_BUTTON (actionable);
714 return gtk_actionable_get_action_target_value (GTK_ACTIONABLE (button->priv->button));
718 gtk_tool_button_set_action_target_value (GtkActionable *actionable,
719 GVariant *action_target)
721 GtkToolButton *button = GTK_TOOL_BUTTON (actionable);
723 gtk_actionable_set_action_target_value (GTK_ACTIONABLE (button->priv->button), action_target);
727 gtk_tool_button_actionable_iface_init (GtkActionableInterface *iface)
729 iface->get_action_name = gtk_tool_button_get_action_name;
730 iface->set_action_name = gtk_tool_button_set_action_name;
731 iface->get_action_target_value = gtk_tool_button_get_action_target_value;
732 iface->set_action_target_value = gtk_tool_button_set_action_target_value;
736 gtk_tool_button_finalize (GObject *object)
738 GtkToolButton *button = GTK_TOOL_BUTTON (object);
740 g_free (button->priv->stock_id);
741 g_free (button->priv->icon_name);
742 g_free (button->priv->label_text);
744 if (button->priv->label_widget)
745 g_object_unref (button->priv->label_widget);
747 if (button->priv->icon_widget)
748 g_object_unref (button->priv->icon_widget);
750 parent_class->finalize (object);
754 clone_image_menu_size (GtkImage *image, GtkSettings *settings)
756 GtkImageType storage_type = gtk_image_get_storage_type (image);
758 if (storage_type == GTK_IMAGE_STOCK)
761 gtk_image_get_stock (image, &stock_id, NULL);
762 return gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
764 else if (storage_type == GTK_IMAGE_ICON_NAME)
766 const gchar *icon_name;
767 gtk_image_get_icon_name (image, &icon_name, NULL);
768 return gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
770 else if (storage_type == GTK_IMAGE_ICON_SET)
772 GtkIconSet *icon_set;
773 gtk_image_get_icon_set (image, &icon_set, NULL);
774 return gtk_image_new_from_icon_set (icon_set, GTK_ICON_SIZE_MENU);
776 else if (storage_type == GTK_IMAGE_GICON)
779 gtk_image_get_gicon (image, &icon, NULL);
780 return gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_MENU);
782 else if (storage_type == GTK_IMAGE_PIXBUF)
787 gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
790 GdkPixbuf *src_pixbuf, *dest_pixbuf;
791 GtkWidget *cloned_image;
793 src_pixbuf = gtk_image_get_pixbuf (image);
794 dest_pixbuf = gdk_pixbuf_scale_simple (src_pixbuf, width, height,
795 GDK_INTERP_BILINEAR);
797 cloned_image = gtk_image_new_from_pixbuf (dest_pixbuf);
798 g_object_unref (dest_pixbuf);
808 gtk_tool_button_create_menu_proxy (GtkToolItem *item)
810 GtkToolButton *button = GTK_TOOL_BUTTON (item);
811 GtkWidget *menu_item;
812 GtkWidget *menu_image = NULL;
813 GtkStockItem stock_item;
814 gboolean use_mnemonic = TRUE;
817 if (_gtk_tool_item_create_menu_proxy (item))
820 if (GTK_IS_LABEL (button->priv->label_widget))
822 label = gtk_label_get_label (GTK_LABEL (button->priv->label_widget));
823 use_mnemonic = gtk_label_get_use_underline (GTK_LABEL (button->priv->label_widget));
825 else if (button->priv->label_text)
827 label = button->priv->label_text;
828 use_mnemonic = button->priv->use_underline;
830 else if (button->priv->stock_id && gtk_stock_lookup (button->priv->stock_id, &stock_item))
832 label = stock_item.label;
840 menu_item = gtk_image_menu_item_new_with_mnemonic (label);
842 menu_item = gtk_image_menu_item_new_with_label (label);
844 if (GTK_IS_IMAGE (button->priv->icon_widget))
846 menu_image = clone_image_menu_size (GTK_IMAGE (button->priv->icon_widget),
847 gtk_widget_get_settings (GTK_WIDGET (button)));
849 else if (button->priv->stock_id)
851 menu_image = gtk_image_new_from_stock (button->priv->stock_id, GTK_ICON_SIZE_MENU);
855 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item), menu_image);
857 g_signal_connect_closure_by_id (menu_item,
858 g_signal_lookup ("activate", G_OBJECT_TYPE (menu_item)), 0,
859 g_cclosure_new_object_swap (G_CALLBACK (gtk_button_clicked),
860 G_OBJECT (GTK_TOOL_BUTTON (button)->priv->button)),
863 gtk_tool_item_set_proxy_menu_item (GTK_TOOL_ITEM (button), MENU_ID, menu_item);
869 button_clicked (GtkWidget *widget,
870 GtkToolButton *button)
874 action = gtk_activatable_get_related_action (GTK_ACTIVATABLE (button));
877 gtk_action_activate (action);
879 g_signal_emit_by_name (button, "clicked");
883 gtk_tool_button_toolbar_reconfigured (GtkToolItem *tool_item)
885 gtk_tool_button_construct_contents (tool_item);
889 gtk_tool_button_update_icon_spacing (GtkToolButton *button)
894 box = gtk_bin_get_child (GTK_BIN (button->priv->button));
895 if (GTK_IS_BOX (box))
897 gtk_widget_style_get (GTK_WIDGET (button),
898 "icon-spacing", &spacing,
900 gtk_box_set_spacing (GTK_BOX (box), spacing);
905 gtk_tool_button_style_updated (GtkWidget *widget)
907 GTK_WIDGET_CLASS (parent_class)->style_updated (widget);
909 gtk_tool_button_update_icon_spacing (GTK_TOOL_BUTTON (widget));
913 gtk_tool_button_activatable_interface_init (GtkActivatableIface *iface)
915 parent_activatable_iface = g_type_interface_peek_parent (iface);
916 iface->update = gtk_tool_button_update;
917 iface->sync_action_properties = gtk_tool_button_sync_action_properties;
921 gtk_tool_button_update (GtkActivatable *activatable,
923 const gchar *property_name)
925 GtkToolButton *button;
928 parent_activatable_iface->update (activatable, action, property_name);
930 if (!gtk_activatable_get_use_action_appearance (activatable))
933 button = GTK_TOOL_BUTTON (activatable);
935 if (strcmp (property_name, "short-label") == 0)
936 gtk_tool_button_set_label (button, gtk_action_get_short_label (action));
937 else if (strcmp (property_name, "stock-id") == 0)
938 gtk_tool_button_set_stock_id (button, gtk_action_get_stock_id (action));
939 else if (strcmp (property_name, "gicon") == 0)
941 const gchar *stock_id = gtk_action_get_stock_id (action);
942 GIcon *icon = gtk_action_get_gicon (action);
943 GtkIconSize icon_size = GTK_ICON_SIZE_BUTTON;
945 if ((stock_id && gtk_icon_factory_lookup_default (stock_id)) || !icon)
949 image = gtk_tool_button_get_icon_widget (button);
950 icon_size = gtk_tool_item_get_icon_size (GTK_TOOL_ITEM (button));
953 image = gtk_image_new ();
956 gtk_tool_button_set_icon_widget (button, image);
957 gtk_image_set_from_gicon (GTK_IMAGE (image), icon, icon_size);
960 else if (strcmp (property_name, "icon-name") == 0)
961 gtk_tool_button_set_icon_name (button, gtk_action_get_icon_name (action));
965 gtk_tool_button_sync_action_properties (GtkActivatable *activatable,
968 GtkToolButton *button;
970 const gchar *stock_id;
972 parent_activatable_iface->sync_action_properties (activatable, action);
977 if (!gtk_activatable_get_use_action_appearance (activatable))
980 button = GTK_TOOL_BUTTON (activatable);
981 stock_id = gtk_action_get_stock_id (action);
983 gtk_tool_button_set_label (button, gtk_action_get_short_label (action));
984 gtk_tool_button_set_use_underline (button, TRUE);
985 gtk_tool_button_set_stock_id (button, stock_id);
986 gtk_tool_button_set_icon_name (button, gtk_action_get_icon_name (action));
988 if (stock_id && gtk_icon_factory_lookup_default (stock_id))
989 gtk_tool_button_set_icon_widget (button, NULL);
990 else if ((icon = gtk_action_get_gicon (action)) != NULL)
992 GtkIconSize icon_size = gtk_tool_item_get_icon_size (GTK_TOOL_ITEM (button));
993 GtkWidget *image = gtk_tool_button_get_icon_widget (button);
997 image = gtk_image_new ();
998 gtk_widget_show (image);
999 gtk_tool_button_set_icon_widget (button, image);
1002 gtk_image_set_from_gicon (GTK_IMAGE (image), icon, icon_size);
1004 else if (gtk_action_get_icon_name (action))
1005 gtk_tool_button_set_icon_name (button, gtk_action_get_icon_name (action));
1007 gtk_tool_button_set_label (button, gtk_action_get_short_label (action));
1011 * gtk_tool_button_new_from_stock:
1012 * @stock_id: the name of the stock item
1014 * Creates a new #GtkToolButton containing the image and text from a
1015 * stock item. Some stock ids have preprocessor macros like #GTK_STOCK_OK
1016 * and #GTK_STOCK_APPLY.
1018 * It is an error if @stock_id is not a name of a stock item.
1020 * Return value: A new #GtkToolButton
1025 gtk_tool_button_new_from_stock (const gchar *stock_id)
1027 GtkToolButton *button;
1029 g_return_val_if_fail (stock_id != NULL, NULL);
1031 button = g_object_new (GTK_TYPE_TOOL_BUTTON,
1032 "stock-id", stock_id,
1035 return GTK_TOOL_ITEM (button);
1039 * gtk_tool_button_new:
1040 * @label: (allow-none): a string that will be used as label, or %NULL
1041 * @icon_widget: (allow-none): a widget that will be used as the button contents, or %NULL
1043 * Creates a new %GtkToolButton using @icon_widget as contents and @label as
1046 * Return value: A new #GtkToolButton
1051 gtk_tool_button_new (GtkWidget *icon_widget,
1054 GtkToolButton *button;
1056 g_return_val_if_fail (icon_widget == NULL || GTK_IS_WIDGET (icon_widget), NULL);
1058 button = g_object_new (GTK_TYPE_TOOL_BUTTON,
1060 "icon-widget", icon_widget,
1063 return GTK_TOOL_ITEM (button);
1067 * gtk_tool_button_set_label:
1068 * @button: a #GtkToolButton
1069 * @label: (allow-none): a string that will be used as label, or %NULL.
1071 * Sets @label as the label used for the tool button. The "label" property
1072 * only has an effect if not overridden by a non-%NULL "label_widget" property.
1073 * If both the "label_widget" and "label" properties are %NULL, the label
1074 * is determined by the "stock_id" property. If the "stock_id" property is also
1075 * %NULL, @button will not have a label.
1080 gtk_tool_button_set_label (GtkToolButton *button,
1084 gchar *elided_label;
1085 AtkObject *accessible;
1087 g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
1089 old_label = button->priv->label_text;
1091 button->priv->label_text = g_strdup (label);
1092 button->priv->contents_invalid = TRUE;
1096 elided_label = _gtk_toolbar_elide_underscores (label);
1097 accessible = gtk_widget_get_accessible (GTK_WIDGET (button->priv->button));
1098 atk_object_set_name (accessible, elided_label);
1099 g_free (elided_label);
1104 g_object_notify (G_OBJECT (button), "label");
1108 * gtk_tool_button_get_label:
1109 * @button: a #GtkToolButton
1111 * Returns the label used by the tool button, or %NULL if the tool button
1112 * doesn't have a label. or uses a the label from a stock item. The returned
1113 * string is owned by GTK+, and must not be modified or freed.
1115 * Return value: The label, or %NULL
1120 gtk_tool_button_get_label (GtkToolButton *button)
1122 g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1124 return button->priv->label_text;
1128 * gtk_tool_button_set_use_underline:
1129 * @button: a #GtkToolButton
1130 * @use_underline: whether the button label has the form "_Open"
1132 * If set, an underline in the label property indicates that the next character
1133 * should be used for the mnemonic accelerator key in the overflow menu. For
1134 * example, if the label property is "_Open" and @use_underline is %TRUE,
1135 * the label on the tool button will be "Open" and the item on the overflow
1136 * menu will have an underlined 'O'.
1138 * Labels shown on tool buttons never have mnemonics on them; this property
1139 * only affects the menu item on the overflow menu.
1144 gtk_tool_button_set_use_underline (GtkToolButton *button,
1145 gboolean use_underline)
1147 g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
1149 use_underline = use_underline != FALSE;
1151 if (use_underline != button->priv->use_underline)
1153 button->priv->use_underline = use_underline;
1154 button->priv->contents_invalid = TRUE;
1156 g_object_notify (G_OBJECT (button), "use-underline");
1161 * gtk_tool_button_get_use_underline:
1162 * @button: a #GtkToolButton
1164 * Returns whether underscores in the label property are used as mnemonics
1165 * on menu items on the overflow menu. See gtk_tool_button_set_use_underline().
1167 * Return value: %TRUE if underscores in the label property are used as
1168 * mnemonics on menu items on the overflow menu.
1173 gtk_tool_button_get_use_underline (GtkToolButton *button)
1175 g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), FALSE);
1177 return button->priv->use_underline;
1181 * gtk_tool_button_set_stock_id:
1182 * @button: a #GtkToolButton
1183 * @stock_id: (allow-none): a name of a stock item, or %NULL
1185 * Sets the name of the stock item. See gtk_tool_button_new_from_stock().
1186 * The stock_id property only has an effect if not
1187 * overridden by non-%NULL "label" and "icon_widget" properties.
1192 gtk_tool_button_set_stock_id (GtkToolButton *button,
1193 const gchar *stock_id)
1195 gchar *old_stock_id;
1197 g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
1199 old_stock_id = button->priv->stock_id;
1201 button->priv->stock_id = g_strdup (stock_id);
1202 button->priv->contents_invalid = TRUE;
1204 g_free (old_stock_id);
1206 g_object_notify (G_OBJECT (button), "stock-id");
1210 * gtk_tool_button_get_stock_id:
1211 * @button: a #GtkToolButton
1213 * Returns the name of the stock item. See gtk_tool_button_set_stock_id().
1214 * The returned string is owned by GTK+ and must not be freed or modifed.
1216 * Return value: the name of the stock item for @button.
1221 gtk_tool_button_get_stock_id (GtkToolButton *button)
1223 g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1225 return button->priv->stock_id;
1229 * gtk_tool_button_set_icon_name:
1230 * @button: a #GtkToolButton
1231 * @icon_name: (allow-none): the name of the themed icon
1233 * Sets the icon for the tool button from a named themed icon.
1234 * See the docs for #GtkIconTheme for more details.
1235 * The "icon_name" property only has an effect if not
1236 * overridden by non-%NULL "label", "icon_widget" and "stock_id"
1242 gtk_tool_button_set_icon_name (GtkToolButton *button,
1243 const gchar *icon_name)
1245 gchar *old_icon_name;
1247 g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
1249 old_icon_name = button->priv->icon_name;
1251 button->priv->icon_name = g_strdup (icon_name);
1252 button->priv->contents_invalid = TRUE;
1254 g_free (old_icon_name);
1256 g_object_notify (G_OBJECT (button), "icon-name");
1260 * gtk_tool_button_get_icon_name:
1261 * @button: a #GtkToolButton
1263 * Returns the name of the themed icon for the tool button,
1264 * see gtk_tool_button_set_icon_name().
1266 * Returns: the icon name or %NULL if the tool button has
1272 gtk_tool_button_get_icon_name (GtkToolButton *button)
1274 g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1276 return button->priv->icon_name;
1280 * gtk_tool_button_set_icon_widget:
1281 * @button: a #GtkToolButton
1282 * @icon_widget: (allow-none): the widget used as icon, or %NULL
1284 * Sets @icon as the widget used as icon on @button. If @icon_widget is
1285 * %NULL the icon is determined by the "stock_id" property. If the
1286 * "stock_id" property is also %NULL, @button will not have an icon.
1291 gtk_tool_button_set_icon_widget (GtkToolButton *button,
1292 GtkWidget *icon_widget)
1294 g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
1295 g_return_if_fail (icon_widget == NULL || GTK_IS_WIDGET (icon_widget));
1297 if (icon_widget != button->priv->icon_widget)
1299 if (button->priv->icon_widget)
1303 parent = gtk_widget_get_parent (button->priv->icon_widget);
1305 gtk_container_remove (GTK_CONTAINER (parent),
1306 button->priv->icon_widget);
1308 g_object_unref (button->priv->icon_widget);
1312 g_object_ref_sink (icon_widget);
1314 button->priv->icon_widget = icon_widget;
1315 button->priv->contents_invalid = TRUE;
1317 g_object_notify (G_OBJECT (button), "icon-widget");
1322 * gtk_tool_button_set_label_widget:
1323 * @button: a #GtkToolButton
1324 * @label_widget: (allow-none): the widget used as label, or %NULL
1326 * Sets @label_widget as the widget that will be used as the label
1327 * for @button. If @label_widget is %NULL the "label" property is used
1328 * as label. If "label" is also %NULL, the label in the stock item
1329 * determined by the "stock_id" property is used as label. If
1330 * "stock_id" is also %NULL, @button does not have a label.
1335 gtk_tool_button_set_label_widget (GtkToolButton *button,
1336 GtkWidget *label_widget)
1338 g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
1339 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1341 if (label_widget != button->priv->label_widget)
1343 if (button->priv->label_widget)
1347 parent = gtk_widget_get_parent (button->priv->label_widget);
1349 gtk_container_remove (GTK_CONTAINER (parent),
1350 button->priv->label_widget);
1352 g_object_unref (button->priv->label_widget);
1356 g_object_ref_sink (label_widget);
1358 button->priv->label_widget = label_widget;
1359 button->priv->contents_invalid = TRUE;
1361 g_object_notify (G_OBJECT (button), "label-widget");
1366 * gtk_tool_button_get_label_widget:
1367 * @button: a #GtkToolButton
1369 * Returns the widget used as label on @button.
1370 * See gtk_tool_button_set_label_widget().
1372 * Return value: (transfer none): The widget used as label
1373 * on @button, or %NULL.
1378 gtk_tool_button_get_label_widget (GtkToolButton *button)
1380 g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1382 return button->priv->label_widget;
1386 * gtk_tool_button_get_icon_widget:
1387 * @button: a #GtkToolButton
1389 * Return the widget used as icon widget on @button.
1390 * See gtk_tool_button_set_icon_widget().
1392 * Return value: (transfer none): The widget used as icon
1393 * on @button, or %NULL.
1398 gtk_tool_button_get_icon_widget (GtkToolButton *button)
1400 g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1402 return button->priv->icon_widget;
1406 _gtk_tool_button_get_button (GtkToolButton *button)
1408 g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
1410 return button->priv->button;