3 * Copyright (C) 2002 Anders Carlsson <andersca@codefactory.se>
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.
23 #include "gtktoolbutton.h"
24 #include "gtkbutton.h"
26 #include "gtkiconfactory.h"
28 #include "gtkimagemenuitem.h"
33 #include "gtktoolbar.h"
34 #include "gtkiconfactory.h"
38 #define MENU_ID "gtk-tool-button-menu-id"
54 static void gtk_tool_button_init (GtkToolButton *button,
55 GtkToolButtonClass *klass);
56 static void gtk_tool_button_class_init (GtkToolButtonClass *klass);
57 static void gtk_tool_button_size_request (GtkWidget *widget,
58 GtkRequisition *requisition);
59 static void gtk_tool_button_size_allocate (GtkWidget *widget,
60 GtkAllocation *allocation);
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_finalize (GObject *object);
71 static void gtk_tool_button_toolbar_reconfigured (GtkToolItem *tool_item);
72 static gboolean gtk_tool_button_create_menu_proxy (GtkToolItem *item);
73 static void button_clicked (GtkWidget *widget,
74 GtkToolButton *button);
76 static void gtk_tool_button_construct_contents (GtkToolItem *tool_item);
78 static GObjectClass *parent_class = NULL;
79 static guint toolbutton_signals[LAST_SIGNAL] = { 0 };
81 struct _GtkToolButtonPrivate
87 GtkWidget *label_widget;
88 GtkWidget *icon_widget;
90 guint use_underline : 1;
94 gtk_tool_button_get_type (void)
96 static GtkType type = 0;
100 static const GTypeInfo type_info =
102 sizeof (GtkToolButtonClass),
103 (GBaseInitFunc) NULL,
104 (GBaseFinalizeFunc) NULL,
105 (GClassInitFunc) gtk_tool_button_class_init,
106 (GClassFinalizeFunc) NULL,
108 sizeof (GtkToolButton),
110 (GInstanceInitFunc) gtk_tool_button_init,
113 type = g_type_register_static (GTK_TYPE_TOOL_ITEM,
121 gtk_tool_button_class_init (GtkToolButtonClass *klass)
123 GObjectClass *object_class;
124 GtkWidgetClass *widget_class;
125 GtkToolItemClass *tool_item_class;
127 parent_class = g_type_class_peek_parent (klass);
129 object_class = (GObjectClass *)klass;
130 widget_class = (GtkWidgetClass *)klass;
131 tool_item_class = (GtkToolItemClass *)klass;
133 object_class->set_property = gtk_tool_button_set_property;
134 object_class->get_property = gtk_tool_button_get_property;
135 object_class->finalize = gtk_tool_button_finalize;
137 widget_class->size_request = gtk_tool_button_size_request;
138 widget_class->size_allocate = gtk_tool_button_size_allocate;
140 tool_item_class->create_menu_proxy = gtk_tool_button_create_menu_proxy;
141 tool_item_class->toolbar_reconfigured = gtk_tool_button_toolbar_reconfigured;
143 klass->button_type = GTK_TYPE_BUTTON;
145 /* Properties are interpreted like this:
147 * - if the tool button has an icon_widget, then that widget
148 * will be used as the icon. Otherwise, if the tool button
149 * has a stock id, the corresponding stock icon will be
150 * used. Otherwise, the tool button will not have an icon.
152 * - if the tool button has a label_widget then that widget
153 * will be used as the label. Otherwise, if the tool button
154 * has a label text, that text will be used as label. Otherwise,
155 * if the toolbutton has a stock id, the corresponding text
156 * will be used as label. Otherwise, the toolbutton will
157 * have an empty label.
159 * - The use_underline property only has an effect when the label
160 * on the toolbutton comes from the label property (ie. not from
161 * label_widget or from stock_id).
163 * In that case, if use_underline is set,
165 * - underscores are removed from the label text before
166 * the label is shown on the toolbutton unless the
167 * underscore is followed by another underscore
169 * - an underscore indicates that the next character when
170 * used in the overflow menu should be used as a mnemonic.
172 * In short: use_underline = TRUE means that the label text has
173 * the form "_Open" and the toolbar should take appropriate action.
176 g_object_class_install_property (object_class,
178 g_param_spec_string ("label",
180 _("Text to show in the item."),
183 g_object_class_install_property (object_class,
185 g_param_spec_boolean ("use_underline",
187 _("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"),
190 g_object_class_install_property (object_class,
192 g_param_spec_object ("label_widget",
194 _("Widget to use as the item label"),
197 g_object_class_install_property (object_class,
199 g_param_spec_string ("stock_id",
201 _("The stock icon displayed on the item"),
204 g_object_class_install_property (object_class,
206 g_param_spec_object ("icon_widget",
208 _("Icon widget to display in the item"),
212 toolbutton_signals[CLICKED] =
213 g_signal_new ("clicked",
214 G_OBJECT_CLASS_TYPE (klass),
216 G_STRUCT_OFFSET (GtkToolButtonClass, clicked),
218 g_cclosure_marshal_VOID__VOID,
223 gtk_tool_button_init (GtkToolButton *button,
224 GtkToolButtonClass *klass)
226 GtkToolItem *toolitem = GTK_TOOL_ITEM (button);
228 button->priv = g_new0 (GtkToolButtonPrivate, 1);
230 gtk_tool_item_set_homogeneous (toolitem, TRUE);
233 button->priv->button = g_object_new (klass->button_type, NULL);
234 gtk_button_set_focus_on_click (GTK_BUTTON (button->priv->button), FALSE);
235 g_signal_connect_object (button->priv->button, "clicked",
236 G_CALLBACK (button_clicked), button, 0);
238 gtk_container_add (GTK_CONTAINER (button), button->priv->button);
239 gtk_widget_show (button->priv->button);
243 gtk_tool_button_size_request (GtkWidget *widget,
244 GtkRequisition *requisition)
246 GtkWidget *child = GTK_BIN (widget)->child;
248 if (child && GTK_WIDGET_VISIBLE (child))
250 gtk_widget_size_request (child, requisition);
254 requisition->width = 0;
255 requisition->height = 0;
258 requisition->width += GTK_CONTAINER (widget)->border_width * 2;
259 requisition->height += GTK_CONTAINER (widget)->border_width * 2;
263 gtk_tool_button_size_allocate (GtkWidget *widget,
264 GtkAllocation *allocation)
266 GtkToolItem *toolitem = GTK_TOOL_ITEM (widget);
267 GtkAllocation child_allocation;
269 GtkWidget *child = GTK_BIN (widget)->child;
271 widget->allocation = *allocation;
272 border_width = GTK_CONTAINER (widget)->border_width;
274 if (gtk_tool_item_get_use_drag_window (toolitem) && GTK_WIDGET_REALIZED (widget))
275 gdk_window_move_resize (_gtk_tool_item_get_drag_window (toolitem),
276 widget->allocation.x + border_width,
277 widget->allocation.y + border_width,
278 widget->allocation.width - border_width * 2,
279 widget->allocation.height - border_width * 2);
281 if (child && GTK_WIDGET_VISIBLE (child))
283 child_allocation.x = allocation->x + border_width;
284 child_allocation.y = allocation->y + border_width;
285 child_allocation.width = allocation->width - 2 * border_width;
286 child_allocation.height = allocation->height - 2 * border_width;
288 gtk_widget_size_allocate (child, &child_allocation);
293 gtk_tool_button_construct_contents (GtkToolItem *tool_item)
295 GtkToolButton *button = GTK_TOOL_BUTTON (tool_item);
296 GtkWidget *label = NULL;
297 GtkWidget *icon = NULL;
298 GtkToolbarStyle style;
299 gboolean need_label = FALSE;
300 gboolean need_icon = FALSE;
301 GtkIconSize icon_size;
302 GtkWidget *box = NULL;
304 if (gtk_tool_item_get_proxy_menu_item (tool_item, MENU_ID))
306 /* Remove item, so it will be recreated on the next
307 * create_proxy_menu_item()
309 gtk_tool_item_set_proxy_menu_item (tool_item, MENU_ID, NULL);
312 if (button->priv->icon_widget && button->priv->icon_widget->parent)
314 gtk_container_remove (GTK_CONTAINER (button->priv->icon_widget->parent),
315 button->priv->icon_widget);
318 if (button->priv->label_widget && button->priv->label_widget->parent)
320 gtk_container_remove (GTK_CONTAINER (button->priv->label_widget->parent),
321 button->priv->label_widget);
324 if (GTK_BIN (button->priv->button)->child)
326 /* Note: we are not destroying the label_widget or icon_widget
327 * here because they were removed from their containers above
329 gtk_widget_destroy (GTK_BIN (button->priv->button)->child);
332 style = gtk_tool_item_get_toolbar_style (GTK_TOOL_ITEM (button));
334 if (style != GTK_TOOLBAR_TEXT)
337 if (style != GTK_TOOLBAR_ICONS)
342 if (button->priv->label_widget)
344 label = button->priv->label_widget;
348 GtkStockItem stock_item;
352 if (button->priv->label_text)
354 label_text = button->priv->label_text;
355 elide = button->priv->use_underline;
357 else if (button->priv->stock_id && gtk_stock_lookup (button->priv->stock_id, &stock_item))
359 label_text = stock_item.label;
369 label_text = _gtk_toolbar_elide_underscores (label_text);
371 label_text = g_strdup (label_text);
373 label = gtk_label_new (label_text);
377 gtk_widget_show (label);
381 icon_size = gtk_tool_item_get_icon_size (GTK_TOOL_ITEM (button));
384 if (button->priv->icon_widget)
386 icon = button->priv->icon_widget;
388 if (GTK_IS_IMAGE (icon))
390 g_object_set (G_OBJECT (button->priv->icon_widget),
391 "icon-size", icon_size,
395 else if (button->priv->stock_id)
397 icon = gtk_image_new_from_stock (button->priv->stock_id, icon_size);
398 gtk_widget_show (icon);
404 case GTK_TOOLBAR_ICONS:
406 gtk_container_add (GTK_CONTAINER (button->priv->button), icon);
409 case GTK_TOOLBAR_BOTH:
410 box = gtk_vbox_new (FALSE, 0);
411 gtk_box_pack_start (GTK_BOX (box), icon, TRUE, TRUE, 0);
412 gtk_box_pack_start (GTK_BOX (box), label, FALSE, TRUE, 0);
413 gtk_container_add (GTK_CONTAINER (button->priv->button), box);
416 case GTK_TOOLBAR_BOTH_HORIZ:
417 box = gtk_hbox_new (FALSE, 0);
418 gtk_box_pack_start (GTK_BOX (box), icon, FALSE, TRUE, 0);
419 gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);
420 gtk_container_add (GTK_CONTAINER (button->priv->button), box);
423 case GTK_TOOLBAR_TEXT:
424 gtk_container_add (GTK_CONTAINER (button->priv->button), label);
429 gtk_widget_show (box);
431 gtk_button_set_relief (GTK_BUTTON (button->priv->button),
432 gtk_tool_item_get_relief_style (GTK_TOOL_ITEM (button)));
434 gtk_widget_queue_resize (GTK_WIDGET (button));
438 gtk_tool_button_set_property (GObject *object,
443 GtkToolButton *button = GTK_TOOL_BUTTON (object);
448 gtk_tool_button_set_label (button, g_value_get_string (value));
450 case PROP_USE_UNDERLINE:
451 gtk_tool_button_set_use_underline (button, g_value_get_boolean (value));
453 case PROP_LABEL_WIDGET:
454 gtk_tool_button_set_label_widget (button, g_value_get_object (value));
457 gtk_tool_button_set_stock_id (button, g_value_get_string (value));
459 case PROP_ICON_WIDGET:
460 gtk_tool_button_set_icon_widget (button, g_value_get_object (value));
463 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
468 gtk_tool_button_get_property (GObject *object,
473 GtkToolButton *button = GTK_TOOL_BUTTON (object);
478 g_value_set_string (value, gtk_tool_button_get_label (button));
480 case PROP_LABEL_WIDGET:
481 g_value_set_object (value, gtk_tool_button_get_label_widget (button));
483 case PROP_USE_UNDERLINE:
484 g_value_set_boolean (value, gtk_tool_button_get_use_underline (button));
487 g_value_set_string (value, button->priv->stock_id);
489 case PROP_ICON_WIDGET:
490 g_value_set_object (value, button->priv->icon_widget);
493 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
498 gtk_tool_button_finalize (GObject *object)
500 GtkToolButton *button = GTK_TOOL_BUTTON (object);
502 g_free (button->priv->stock_id);
503 button->priv->stock_id = NULL;
505 parent_class->finalize (object);
509 clone_image_menu_size (GtkImage *image, GtkSettings *settings)
511 GtkImageType storage_type = gtk_image_get_storage_type (image);
513 if (storage_type == GTK_IMAGE_STOCK)
516 gtk_image_get_stock (image, &stock_id, NULL);
517 return gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
519 else if (storage_type == GTK_IMAGE_ICON_SET)
521 GtkIconSet *icon_set;
522 gtk_image_get_icon_set (image, &icon_set, NULL);
523 return gtk_image_new_from_icon_set (icon_set, GTK_ICON_SIZE_MENU);
525 else if (storage_type == GTK_IMAGE_PIXBUF)
530 gtk_icon_size_lookup_for_settings (settings, GTK_ICON_SIZE_MENU,
533 GdkPixbuf *src_pixbuf, *dest_pixbuf;
535 src_pixbuf = gtk_image_get_pixbuf (image);
536 dest_pixbuf = gdk_pixbuf_scale_simple (src_pixbuf, width, height,
537 GDK_INTERP_BILINEAR);
539 return gtk_image_new_from_pixbuf (dest_pixbuf);
547 gtk_tool_button_create_menu_proxy (GtkToolItem *item)
549 GtkToolButton *button = GTK_TOOL_BUTTON (item);
550 GtkWidget *menu_item;
551 GtkWidget *menu_image = NULL;
552 GtkStockItem stock_item;
553 gboolean use_mnemonic = TRUE;
556 if (button->priv->label_widget && GTK_IS_LABEL (button->priv->label_widget))
558 label = gtk_label_get_label (GTK_LABEL (button->priv->label_widget));
559 use_mnemonic = gtk_label_get_use_underline (GTK_LABEL (button->priv->label_widget));
561 else if (button->priv->label_text)
563 label = button->priv->label_text;
564 use_mnemonic = button->priv->use_underline;
566 else if (button->priv->stock_id && gtk_stock_lookup (button->priv->stock_id, &stock_item))
568 label = stock_item.label;
569 use_mnemonic = FALSE;
574 use_mnemonic = FALSE;
578 menu_item = gtk_image_menu_item_new_with_mnemonic (label);
580 menu_item = gtk_image_menu_item_new_with_label (label);
582 if (button->priv->icon_widget && GTK_IS_IMAGE (button->priv->icon_widget))
584 menu_image = clone_image_menu_size (GTK_IMAGE (button->priv->icon_widget),
585 gtk_widget_get_settings (GTK_WIDGET (button)));
587 else if (button->priv->stock_id)
589 menu_image = gtk_image_new_from_stock (button->priv->stock_id, GTK_ICON_SIZE_MENU);
593 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item), menu_image);
595 g_signal_connect_closure_by_id (menu_item,
596 g_signal_lookup ("activate", G_OBJECT_TYPE (menu_item)), 0,
597 g_cclosure_new_object_swap (G_CALLBACK (gtk_button_clicked),
598 G_OBJECT (GTK_TOOL_BUTTON (button)->priv->button)),
601 gtk_tool_item_set_proxy_menu_item (GTK_TOOL_ITEM (button), MENU_ID, menu_item);
607 button_clicked (GtkWidget *widget,
608 GtkToolButton *button)
610 g_signal_emit_by_name (button, "clicked");
614 gtk_tool_button_toolbar_reconfigured (GtkToolItem *tool_item)
616 gtk_tool_button_construct_contents (tool_item);
620 gtk_tool_button_new_from_stock (const gchar *stock_id)
622 GtkToolButton *button;
624 g_return_val_if_fail (stock_id != NULL, NULL);
626 button = g_object_new (GTK_TYPE_TOOL_BUTTON,
627 "stock_id", stock_id,
630 return GTK_TOOL_ITEM (button);
634 gtk_tool_button_new (const gchar *label,
635 GtkWidget *icon_widget)
637 GtkToolButton *button;
639 button = g_object_new (GTK_TYPE_TOOL_BUTTON,
643 gtk_tool_button_set_label (button, label);
646 gtk_tool_button_set_icon_widget (button, icon_widget);
648 return GTK_TOOL_ITEM (button);
652 gtk_tool_button_set_label (GtkToolButton *button,
657 g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
659 old_label = button->priv->label_text;
661 button->priv->label_text = g_strdup (label);
662 gtk_tool_button_construct_contents (GTK_TOOL_ITEM (button));
664 g_object_notify (G_OBJECT (button), "label");
670 G_CONST_RETURN gchar *
671 gtk_tool_button_get_label (GtkToolButton *button)
673 g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
675 return button->priv->label_text;
679 gtk_tool_button_set_use_underline (GtkToolButton *button,
680 gboolean use_underline)
682 g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
684 use_underline = use_underline != FALSE;
686 if (use_underline != button->priv->use_underline)
688 button->priv->use_underline = use_underline;
690 gtk_tool_button_construct_contents (GTK_TOOL_ITEM (button));
692 g_object_notify (G_OBJECT (button), "use_underline");
697 gtk_tool_button_get_use_underline (GtkToolButton *button)
699 g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), FALSE);
701 return button->priv->use_underline;
705 gtk_tool_button_set_stock_id (GtkToolButton *button,
706 const gchar *stock_id)
710 g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
712 old_stock_id = button->priv->stock_id;
714 button->priv->stock_id = g_strdup (stock_id);
715 gtk_tool_button_construct_contents (GTK_TOOL_ITEM (button));
717 g_object_notify (G_OBJECT (button), "stock_id");
719 g_free (old_stock_id);
722 G_CONST_RETURN gchar *
723 gtk_tool_button_get_stock_id (GtkToolButton *button)
725 g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
727 return button->priv->stock_id;
731 gtk_tool_button_set_icon_widget (GtkToolButton *button,
734 g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
735 g_return_if_fail (icon == NULL || GTK_IS_WIDGET (icon));
737 if (icon != button->priv->icon_widget)
739 if (button->priv->icon_widget)
740 g_object_unref (G_OBJECT (button->priv->icon_widget));
745 gtk_object_sink (GTK_OBJECT (icon));
748 button->priv->icon_widget = icon;
750 gtk_tool_button_construct_contents (GTK_TOOL_ITEM (button));
752 g_object_notify (G_OBJECT (button), "icon_widget");
757 gtk_tool_button_set_label_widget (GtkToolButton *button,
758 GtkWidget *label_widget)
760 g_return_if_fail (GTK_IS_TOOL_BUTTON (button));
761 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
763 if (label_widget != button->priv->label_widget)
765 if (button->priv->label_widget)
766 g_object_unref (button->priv->label_widget);
770 g_object_ref (label_widget);
771 gtk_object_sink (GTK_OBJECT (label_widget));
774 button->priv->label_widget = label_widget;
776 gtk_tool_button_construct_contents (GTK_TOOL_ITEM (button));
778 g_object_notify (G_OBJECT (button), "label_widget");
783 gtk_tool_button_get_label_widget (GtkToolButton *button)
785 g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
787 return button->priv->label_widget;
791 gtk_tool_button_get_icon_widget (GtkToolButton *button)
793 g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
795 return button->priv->icon_widget;
799 _gtk_tool_button_get_button (GtkToolButton *button)
801 g_return_val_if_fail (GTK_IS_TOOL_BUTTON (button), NULL);
803 return button->priv->button;