1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * GtkToolbar copyright (C) Federico Mena
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
28 #include "gtkbutton.h"
29 #include "gtktogglebutton.h"
30 #include "gtkradiobutton.h"
34 #include "gtktoolbar.h"
36 #include "gtkiconfactory.h"
38 #include "gtksettings.h"
42 #define DEFAULT_IPADDING 0
43 #define DEFAULT_SPACE_SIZE 5
44 #define DEFAULT_SPACE_STYLE GTK_TOOLBAR_SPACE_LINE
46 #define DEFAULT_ICON_SIZE GTK_ICON_SIZE_LARGE_TOOLBAR
48 #define SPACE_LINE_DIVISION 10
49 #define SPACE_LINE_START 3
50 #define SPACE_LINE_END 7
64 typedef struct _GtkToolbarChildSpace GtkToolbarChildSpace;
65 struct _GtkToolbarChildSpace
67 GtkToolbarChild child;
69 gint alloc_x, alloc_y;
72 static void gtk_toolbar_class_init (GtkToolbarClass *class);
73 static void gtk_toolbar_init (GtkToolbar *toolbar);
74 static void gtk_toolbar_finalize (GObject *object);
75 static void gtk_toolbar_set_property (GObject *object,
79 static void gtk_toolbar_get_property (GObject *object,
83 static void gtk_toolbar_destroy (GtkObject *object);
84 static gint gtk_toolbar_expose (GtkWidget *widget,
85 GdkEventExpose *event);
86 static void gtk_toolbar_size_request (GtkWidget *widget,
87 GtkRequisition *requisition);
88 static void gtk_toolbar_size_allocate (GtkWidget *widget,
89 GtkAllocation *allocation);
90 static void gtk_toolbar_style_set (GtkWidget *widget,
91 GtkStyle *prev_style);
92 static gboolean gtk_toolbar_focus (GtkWidget *widget,
93 GtkDirectionType dir);
94 static void gtk_toolbar_show_all (GtkWidget *widget);
95 static void gtk_toolbar_add (GtkContainer *container,
97 static void gtk_toolbar_remove (GtkContainer *container,
99 static void gtk_toolbar_forall (GtkContainer *container,
100 gboolean include_internals,
101 GtkCallback callback,
102 gpointer callback_data);
104 static void gtk_real_toolbar_orientation_changed (GtkToolbar *toolbar,
105 GtkOrientation orientation);
106 static void gtk_real_toolbar_style_changed (GtkToolbar *toolbar,
107 GtkToolbarStyle style);
109 static GtkWidget * gtk_toolbar_internal_insert_element (GtkToolbar *toolbar,
110 GtkToolbarChildType type,
113 const char *tooltip_text,
114 const char *tooltip_private_text,
116 GtkSignalFunc callback,
119 gboolean has_mnemonic);
121 static GtkWidget * gtk_toolbar_internal_insert_item (GtkToolbar *toolbar,
123 const char *tooltip_text,
124 const char *tooltip_private_text,
126 GtkSignalFunc callback,
129 gboolean has_mnemonic);
131 static void gtk_toolbar_update_button_relief (GtkToolbar *toolbar);
133 static GtkReliefStyle get_button_relief (GtkToolbar *toolbar);
134 static gint get_space_size (GtkToolbar *toolbar);
135 static GtkToolbarSpaceStyle get_space_style (GtkToolbar *toolbar);
138 static GtkContainerClass *parent_class;
140 static guint toolbar_signals[LAST_SIGNAL] = { 0 };
144 gtk_toolbar_get_type (void)
146 static GtkType toolbar_type = 0;
150 static const GtkTypeInfo toolbar_info =
154 sizeof (GtkToolbarClass),
155 (GtkClassInitFunc) gtk_toolbar_class_init,
156 (GtkObjectInitFunc) gtk_toolbar_init,
157 /* reserved_1 */ NULL,
158 /* reserved_2 */ NULL,
159 (GtkClassInitFunc) NULL,
162 toolbar_type = gtk_type_unique (gtk_container_get_type (), &toolbar_info);
169 gtk_toolbar_class_init (GtkToolbarClass *class)
171 GObjectClass *gobject_class;
172 GtkObjectClass *object_class;
173 GtkWidgetClass *widget_class;
174 GtkContainerClass *container_class;
176 gobject_class = G_OBJECT_CLASS (class);
177 object_class = (GtkObjectClass *) class;
178 widget_class = (GtkWidgetClass *) class;
179 container_class = (GtkContainerClass *) class;
181 parent_class = gtk_type_class (gtk_container_get_type ());
183 gobject_class->finalize = gtk_toolbar_finalize;
185 object_class->destroy = gtk_toolbar_destroy;
186 gobject_class->set_property = gtk_toolbar_set_property;
187 gobject_class->get_property = gtk_toolbar_get_property;
189 widget_class->expose_event = gtk_toolbar_expose;
190 widget_class->size_request = gtk_toolbar_size_request;
191 widget_class->size_allocate = gtk_toolbar_size_allocate;
192 widget_class->style_set = gtk_toolbar_style_set;
193 widget_class->show_all = gtk_toolbar_show_all;
194 widget_class->focus = gtk_toolbar_focus;
196 container_class->add = gtk_toolbar_add;
197 container_class->remove = gtk_toolbar_remove;
198 container_class->forall = gtk_toolbar_forall;
200 class->orientation_changed = gtk_real_toolbar_orientation_changed;
201 class->style_changed = gtk_real_toolbar_style_changed;
203 toolbar_signals[ORIENTATION_CHANGED] =
204 gtk_signal_new ("orientation_changed",
206 GTK_CLASS_TYPE (object_class),
207 GTK_SIGNAL_OFFSET (GtkToolbarClass, orientation_changed),
208 gtk_marshal_VOID__ENUM,
210 GTK_TYPE_ORIENTATION);
211 toolbar_signals[STYLE_CHANGED] =
212 gtk_signal_new ("style_changed",
214 GTK_CLASS_TYPE (object_class),
215 GTK_SIGNAL_OFFSET (GtkToolbarClass, style_changed),
216 gtk_marshal_VOID__ENUM,
218 GTK_TYPE_TOOLBAR_STYLE);
220 g_object_class_install_property (gobject_class,
222 g_param_spec_enum ("orientation",
224 _("The orientation of the toolbar"),
225 GTK_TYPE_ORIENTATION,
226 GTK_ORIENTATION_HORIZONTAL,
229 g_object_class_install_property (gobject_class,
231 g_param_spec_enum ("toolbar_style",
233 _("How to draw the toolbar"),
234 GTK_TYPE_TOOLBAR_STYLE,
239 gtk_widget_class_install_style_property (widget_class,
240 g_param_spec_int ("space_size",
242 _("Size of spacers"),
248 gtk_widget_class_install_style_property (widget_class,
249 g_param_spec_int ("internal_padding",
250 _("Internal padding"),
251 _("Amount of border space between the toolbar shadow and the buttons"),
257 gtk_widget_class_install_style_property (widget_class,
258 g_param_spec_enum ("space_style",
260 _("Whether spacers are vertical lines or just blank"),
261 GTK_TYPE_TOOLBAR_SPACE_STYLE,
266 gtk_widget_class_install_style_property (widget_class,
267 g_param_spec_enum ("button_relief",
269 _("Type of bevel around toolbar buttons"),
270 GTK_TYPE_RELIEF_STYLE,
274 gtk_widget_class_install_style_property (widget_class,
275 g_param_spec_enum ("shadow_type",
277 _("Style of bevel around the toolbar"),
278 GTK_TYPE_SHADOW_TYPE,
282 gtk_settings_install_property (g_param_spec_enum ("gtk-toolbar-style",
284 _("Whether default toolbars have text only, text and icons, icons only, etc."),
285 GTK_TYPE_TOOLBAR_STYLE,
289 gtk_settings_install_property (g_param_spec_enum ("gtk-toolbar-icon-size",
290 _("Toolbar icon size"),
291 _("Size of icons in default toolbars"),
293 GTK_ICON_SIZE_LARGE_TOOLBAR,
298 style_change_notify (GObject *object,
304 toolbar = GTK_TOOLBAR (data);
306 if (!toolbar->style_set)
308 /* pretend it was set, then unset, thus reverting to new default */
309 toolbar->style_set = TRUE;
310 gtk_toolbar_unset_style (toolbar);
315 icon_size_change_notify (GObject *object,
321 toolbar = GTK_TOOLBAR (data);
323 if (!toolbar->icon_size_set)
325 /* pretend it was set, then unset, thus reverting to new default */
326 toolbar->icon_size_set = TRUE;
327 gtk_toolbar_unset_icon_size (toolbar);
332 gtk_toolbar_init (GtkToolbar *toolbar)
334 GTK_WIDGET_SET_FLAGS (toolbar, GTK_NO_WINDOW);
335 GTK_WIDGET_UNSET_FLAGS (toolbar, GTK_CAN_FOCUS);
337 toolbar->num_children = 0;
338 toolbar->children = NULL;
339 toolbar->orientation = GTK_ORIENTATION_HORIZONTAL;
340 toolbar->icon_size = DEFAULT_ICON_SIZE;
341 toolbar->tooltips = gtk_tooltips_new ();
342 toolbar->button_maxw = 0;
343 toolbar->button_maxh = 0;
345 toolbar->style_set = FALSE;
346 toolbar->icon_size_set = FALSE;
347 g_object_get (gtk_settings_get_default (),
348 "gtk-toolbar-icon-size",
354 toolbar->style_set_connection =
355 g_signal_connect (G_OBJECT (gtk_settings_get_default ()),
356 "notify::gtk-toolbar-style",
357 G_CALLBACK (style_change_notify),
360 toolbar->icon_size_connection =
361 g_signal_connect (G_OBJECT (gtk_settings_get_default ()),
362 "notify::gtk-toolbar-icon-size",
363 G_CALLBACK (icon_size_change_notify),
368 gtk_toolbar_finalize (GObject *object)
372 toolbar = GTK_TOOLBAR (object);
374 g_signal_handler_disconnect (G_OBJECT (gtk_settings_get_default ()),
375 toolbar->style_set_connection);
376 g_signal_handler_disconnect (G_OBJECT (gtk_settings_get_default ()),
377 toolbar->icon_size_connection);
379 G_OBJECT_CLASS (parent_class)->finalize (object);
383 gtk_toolbar_set_property (GObject *object,
388 GtkToolbar *toolbar = GTK_TOOLBAR (object);
392 case PROP_ORIENTATION:
393 gtk_toolbar_set_orientation (toolbar, g_value_get_enum (value));
395 case PROP_TOOLBAR_STYLE:
396 gtk_toolbar_set_style (toolbar, g_value_get_enum (value));
402 gtk_toolbar_get_property (GObject *object,
407 GtkToolbar *toolbar = GTK_TOOLBAR (object);
411 case PROP_ORIENTATION:
412 g_value_set_enum (value, toolbar->orientation);
414 case PROP_TOOLBAR_STYLE:
415 g_value_set_enum (value, toolbar->style);
418 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
424 gtk_toolbar_new (void)
428 toolbar = gtk_type_new (gtk_toolbar_get_type ());
430 return GTK_WIDGET (toolbar);
434 gtk_toolbar_destroy (GtkObject *object)
439 g_return_if_fail (GTK_IS_TOOLBAR (object));
441 toolbar = GTK_TOOLBAR (object);
443 if (toolbar->tooltips)
445 gtk_object_unref (GTK_OBJECT (toolbar->tooltips));
446 toolbar->tooltips = NULL;
449 for (children = toolbar->children; children; children = children->next)
451 GtkToolbarChild *child;
453 child = children->data;
455 if (child->type != GTK_TOOLBAR_CHILD_SPACE)
457 gtk_widget_ref (child->widget);
458 gtk_widget_unparent (child->widget);
459 gtk_widget_destroy (child->widget);
460 gtk_widget_unref (child->widget);
465 g_list_free (toolbar->children);
466 toolbar->children = NULL;
468 GTK_OBJECT_CLASS (parent_class)->destroy (object);
472 gtk_toolbar_paint_space_line (GtkWidget *widget,
474 GtkToolbarChild *child)
477 GtkToolbarChildSpace *child_space;
480 g_return_if_fail (GTK_IS_TOOLBAR (widget));
481 g_return_if_fail (child != NULL);
482 g_return_if_fail (child->type == GTK_TOOLBAR_CHILD_SPACE);
484 toolbar = GTK_TOOLBAR (widget);
486 child_space = (GtkToolbarChildSpace *) child;
487 space_size = get_space_size (toolbar);
489 if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
490 gtk_paint_vline (widget->style, widget->window,
491 GTK_WIDGET_STATE (widget), area, widget,
493 child_space->alloc_y + toolbar->button_maxh *
494 SPACE_LINE_START / SPACE_LINE_DIVISION,
495 child_space->alloc_y + toolbar->button_maxh *
496 SPACE_LINE_END / SPACE_LINE_DIVISION,
497 child_space->alloc_x +
499 widget->style->xthickness) / 2);
501 gtk_paint_hline (widget->style, widget->window,
502 GTK_WIDGET_STATE (widget), area, widget,
504 child_space->alloc_x + toolbar->button_maxw *
505 SPACE_LINE_START / SPACE_LINE_DIVISION,
506 child_space->alloc_x + toolbar->button_maxw *
507 SPACE_LINE_END / SPACE_LINE_DIVISION,
508 child_space->alloc_y +
510 widget->style->ythickness) / 2);
514 gtk_toolbar_expose (GtkWidget *widget,
515 GdkEventExpose *event)
519 GtkToolbarChild *child;
522 g_return_val_if_fail (GTK_IS_TOOLBAR (widget), FALSE);
523 g_return_val_if_fail (event != NULL, FALSE);
525 border_width = GTK_CONTAINER (widget)->border_width;
527 if (GTK_WIDGET_DRAWABLE (widget))
529 GtkShadowType shadow_type;
531 toolbar = GTK_TOOLBAR (widget);
533 gtk_widget_style_get (widget, "shadow_type", &shadow_type, NULL);
535 gtk_paint_box (widget->style,
537 GTK_WIDGET_STATE (widget),
539 &event->area, widget, "toolbar",
540 widget->allocation.x + border_width,
541 widget->allocation.y + border_width,
542 widget->allocation.width - border_width,
543 widget->allocation.height - border_width);
545 for (children = toolbar->children; children; children = children->next)
547 child = children->data;
549 if (child->type == GTK_TOOLBAR_CHILD_SPACE)
551 if (get_space_style (toolbar) == GTK_TOOLBAR_SPACE_LINE)
552 gtk_toolbar_paint_space_line (widget, &event->area, child);
555 gtk_container_propagate_expose (GTK_CONTAINER (widget),
565 gtk_toolbar_size_request (GtkWidget *widget,
566 GtkRequisition *requisition)
570 GtkToolbarChild *child;
572 gint button_maxw, button_maxh;
573 gint widget_maxw, widget_maxh;
574 GtkRequisition child_requisition;
578 g_return_if_fail (GTK_IS_TOOLBAR (widget));
579 g_return_if_fail (requisition != NULL);
581 toolbar = GTK_TOOLBAR (widget);
583 requisition->width = GTK_CONTAINER (toolbar)->border_width * 2;
584 requisition->height = GTK_CONTAINER (toolbar)->border_width * 2;
591 space_size = get_space_size (toolbar);
593 for (children = toolbar->children; children; children = children->next)
595 child = children->data;
599 case GTK_TOOLBAR_CHILD_SPACE:
600 if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
601 requisition->width += space_size;
603 requisition->height += space_size;
607 case GTK_TOOLBAR_CHILD_BUTTON:
608 case GTK_TOOLBAR_CHILD_RADIOBUTTON:
609 case GTK_TOOLBAR_CHILD_TOGGLEBUTTON:
610 if (GTK_WIDGET_VISIBLE (child->widget))
612 gtk_widget_size_request (child->widget, &child_requisition);
615 button_maxw = MAX (button_maxw, child_requisition.width);
616 button_maxh = MAX (button_maxh, child_requisition.height);
621 case GTK_TOOLBAR_CHILD_WIDGET:
622 if (GTK_WIDGET_VISIBLE (child->widget))
624 gtk_widget_size_request (child->widget, &child_requisition);
626 widget_maxw = MAX (widget_maxw, child_requisition.width);
627 widget_maxh = MAX (widget_maxh, child_requisition.height);
629 if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
630 requisition->width += child_requisition.width;
632 requisition->height += child_requisition.height;
638 g_assert_not_reached ();
642 if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
644 requisition->width += nbuttons * button_maxw;
645 requisition->height += MAX (button_maxh, widget_maxh);
649 requisition->width += MAX (button_maxw, widget_maxw);
650 requisition->height += nbuttons * button_maxh;
654 gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
656 requisition->width += 2 * (widget->style->xthickness + ipadding);
657 requisition->height += 2 * (widget->style->ythickness + ipadding);
659 toolbar->button_maxw = button_maxw;
660 toolbar->button_maxh = button_maxh;
664 gtk_toolbar_size_allocate (GtkWidget *widget,
665 GtkAllocation *allocation)
669 GtkToolbarChild *child;
670 GtkToolbarChildSpace *child_space;
672 GtkRequisition child_requisition;
673 gint x_border_width, y_border_width;
677 g_return_if_fail (GTK_IS_TOOLBAR (widget));
678 g_return_if_fail (allocation != NULL);
680 toolbar = GTK_TOOLBAR (widget);
681 widget->allocation = *allocation;
683 x_border_width = GTK_CONTAINER (toolbar)->border_width;
684 y_border_width = GTK_CONTAINER (toolbar)->border_width;
686 gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
688 x_border_width += 2 * (widget->style->xthickness + ipadding);
689 y_border_width += 2 * (widget->style->ythickness + ipadding);
691 if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
692 alloc.x = allocation->x + x_border_width;
694 alloc.y = allocation->y + y_border_width;
696 space_size = get_space_size (toolbar);
698 for (children = toolbar->children; children; children = children->next)
700 child = children->data;
704 case GTK_TOOLBAR_CHILD_SPACE:
706 child_space = (GtkToolbarChildSpace *) child;
708 if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
710 child_space->alloc_x = alloc.x;
711 child_space->alloc_y = allocation->y + (allocation->height - toolbar->button_maxh) / 2;
712 alloc.x += space_size;
716 child_space->alloc_x = allocation->x + (allocation->width - toolbar->button_maxw) / 2;
717 child_space->alloc_y = alloc.y;
718 alloc.y += space_size;
723 case GTK_TOOLBAR_CHILD_BUTTON:
724 case GTK_TOOLBAR_CHILD_RADIOBUTTON:
725 case GTK_TOOLBAR_CHILD_TOGGLEBUTTON:
726 if (!GTK_WIDGET_VISIBLE (child->widget))
729 alloc.width = toolbar->button_maxw;
730 alloc.height = toolbar->button_maxh;
732 if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
733 alloc.y = allocation->y + (allocation->height - toolbar->button_maxh) / 2;
735 alloc.x = allocation->x + (allocation->width - toolbar->button_maxw) / 2;
737 gtk_widget_size_allocate (child->widget, &alloc);
739 if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
740 alloc.x += toolbar->button_maxw;
742 alloc.y += toolbar->button_maxh;
746 case GTK_TOOLBAR_CHILD_WIDGET:
747 if (!GTK_WIDGET_VISIBLE (child->widget))
750 gtk_widget_get_child_requisition (child->widget, &child_requisition);
752 alloc.width = child_requisition.width;
753 alloc.height = child_requisition.height;
755 if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
756 alloc.y = allocation->y + (allocation->height - child_requisition.height) / 2;
758 alloc.x = allocation->x + (allocation->width - child_requisition.width) / 2;
760 gtk_widget_size_allocate (child->widget, &alloc);
762 if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
763 alloc.x += child_requisition.width;
765 alloc.y += child_requisition.height;
770 g_assert_not_reached ();
776 gtk_toolbar_style_set (GtkWidget *widget,
777 GtkStyle *prev_style)
780 gtk_toolbar_update_button_relief (GTK_TOOLBAR (widget));
784 gtk_toolbar_focus (GtkWidget *widget,
785 GtkDirectionType dir)
787 /* Focus can't go in toolbars */
793 child_show_all (GtkWidget *widget)
795 /* Don't show our own children, since that would
796 * show labels we may intend to hide in icons-only mode
798 if (!g_object_get_data (G_OBJECT (widget),
799 "gtk-toolbar-is-child"))
800 gtk_widget_show_all (widget);
804 gtk_toolbar_show_all (GtkWidget *widget)
806 gtk_container_foreach (GTK_CONTAINER (widget),
807 (GtkCallback) child_show_all,
809 gtk_widget_show (widget);
813 gtk_toolbar_add (GtkContainer *container,
816 g_return_if_fail (GTK_IS_TOOLBAR (container));
817 g_return_if_fail (widget != NULL);
819 gtk_toolbar_append_widget (GTK_TOOLBAR (container), widget, NULL, NULL);
823 gtk_toolbar_remove (GtkContainer *container,
828 GtkToolbarChild *child;
830 g_return_if_fail (GTK_IS_TOOLBAR (container));
831 g_return_if_fail (widget != NULL);
833 toolbar = GTK_TOOLBAR (container);
835 for (children = toolbar->children; children; children = children->next)
837 child = children->data;
839 if ((child->type != GTK_TOOLBAR_CHILD_SPACE) && (child->widget == widget))
841 gboolean was_visible;
843 was_visible = GTK_WIDGET_VISIBLE (widget);
844 gtk_widget_unparent (widget);
846 toolbar->children = g_list_remove_link (toolbar->children, children);
848 g_list_free (children);
849 toolbar->num_children--;
851 if (was_visible && GTK_WIDGET_VISIBLE (container))
852 gtk_widget_queue_resize (GTK_WIDGET (container));
860 gtk_toolbar_forall (GtkContainer *container,
861 gboolean include_internals,
862 GtkCallback callback,
863 gpointer callback_data)
867 GtkToolbarChild *child;
869 g_return_if_fail (GTK_IS_TOOLBAR (container));
870 g_return_if_fail (callback != NULL);
872 toolbar = GTK_TOOLBAR (container);
874 for (children = toolbar->children; children; children = children->next)
876 child = children->data;
878 if (child->type != GTK_TOOLBAR_CHILD_SPACE)
879 (*callback) (child->widget, callback_data);
884 gtk_toolbar_append_item (GtkToolbar *toolbar,
886 const char *tooltip_text,
887 const char *tooltip_private_text,
889 GtkSignalFunc callback,
892 return gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_BUTTON,
894 tooltip_text, tooltip_private_text,
895 icon, callback, user_data,
896 toolbar->num_children);
900 gtk_toolbar_prepend_item (GtkToolbar *toolbar,
902 const char *tooltip_text,
903 const char *tooltip_private_text,
905 GtkSignalFunc callback,
908 return gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_BUTTON,
910 tooltip_text, tooltip_private_text,
911 icon, callback, user_data,
916 gtk_toolbar_internal_insert_item (GtkToolbar *toolbar,
918 const char *tooltip_text,
919 const char *tooltip_private_text,
921 GtkSignalFunc callback,
924 gboolean has_mnemonic)
926 return gtk_toolbar_internal_insert_element (toolbar, GTK_TOOLBAR_CHILD_BUTTON,
928 tooltip_text, tooltip_private_text,
929 icon, callback, user_data,
930 position, has_mnemonic);
934 gtk_toolbar_insert_item (GtkToolbar *toolbar,
936 const char *tooltip_text,
937 const char *tooltip_private_text,
939 GtkSignalFunc callback,
943 return gtk_toolbar_internal_insert_item (toolbar,
944 text, tooltip_text, tooltip_private_text,
945 icon, callback, user_data,
950 * gtk_toolbar_set_icon_size:
951 * @toolbar: A #GtkToolbar
952 * @icon_size: The #GtkIconSize that stock icons in the toolbar shall have.
954 * This function sets the size of stock icons in the toolbar. You
955 * can call it both before you add the icons and after they've been
956 * added. The size you set will override user preferences for the default
960 gtk_toolbar_set_icon_size (GtkToolbar *toolbar,
961 GtkIconSize icon_size)
964 GtkToolbarChild *child;
968 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
970 toolbar->icon_size_set = TRUE;
972 if (toolbar->icon_size == icon_size)
975 toolbar->icon_size = icon_size;
977 for (children = toolbar->children; children; children = children->next)
979 child = children->data;
980 if (child->type == GTK_TOOLBAR_CHILD_BUTTON &&
981 GTK_IS_IMAGE (child->icon))
983 image = GTK_IMAGE (child->icon);
984 if (gtk_image_get_storage_type (image) == GTK_IMAGE_STOCK)
986 gtk_image_get_stock (image, &stock_id, NULL);
987 stock_id = g_strdup (stock_id);
988 gtk_image_set_from_stock (image,
996 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
1000 * gtk_toolbar_get_icon_size:
1001 * @toolbar: a #GtkToolbar
1003 * Retrieves the icon size fo the toolbar. See gtk_toolbar_set_icon_size().
1005 * Return value: the current icon size for the icons on the toolbar.
1008 gtk_toolbar_get_icon_size (GtkToolbar *toolbar)
1010 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), GTK_ICON_SIZE_LARGE_TOOLBAR);
1012 return toolbar->icon_size;
1016 * gtk_toolbar_unset_icon_size:
1017 * @toolbar: a #GtkToolbar
1019 * Unsets toolbar icon size set with gtk_toolbar_set_icon_size(), so that
1020 * user preferences will be used to determine the icon size.
1023 gtk_toolbar_unset_icon_size (GtkToolbar *toolbar)
1027 if (toolbar->icon_size_set)
1029 g_object_get (gtk_settings_get_default (),
1030 "gtk-toolbar-icon-size",
1033 if (size != toolbar->icon_size)
1034 gtk_toolbar_set_icon_size (toolbar, size);
1036 toolbar->icon_size_set = FALSE;
1041 * gtk_toolbar_insert_stock:
1042 * @toolbar: A #GtkToolbar
1043 * @stock_id: The id of the stock item you want to insert
1044 * @tooltip_text: The text in the tooltip of the toolbar button
1045 * @tooltip_private_text: The private text of the tooltip
1046 * @callback: The callback called when the toolbar button is clicked.
1047 * @user_data: user data passed to callback
1048 * @position: The position the button shall be inserted at.
1049 * -1 means at the end.
1051 * Inserts a stock item at the specified position of the toolbar. If
1052 * @stock_id is not a known stock item ID, it's inserted verbatim,
1053 * except that underscores are used to mark mnemonics (see
1054 * gtk_label_new_with_mnemonic()).
1056 * Returns: the inserted widget
1059 gtk_toolbar_insert_stock (GtkToolbar *toolbar,
1060 const gchar *stock_id,
1061 const char *tooltip_text,
1062 const char *tooltip_private_text,
1063 GtkSignalFunc callback,
1070 if (gtk_stock_lookup (stock_id, &item))
1072 image = gtk_image_new_from_stock (stock_id, toolbar->icon_size);
1074 return gtk_toolbar_internal_insert_item (toolbar,
1077 tooltip_private_text,
1085 return gtk_toolbar_internal_insert_item (toolbar,
1088 tooltip_private_text,
1099 gtk_toolbar_append_space (GtkToolbar *toolbar)
1101 gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_SPACE,
1105 toolbar->num_children);
1109 gtk_toolbar_prepend_space (GtkToolbar *toolbar)
1111 gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_SPACE,
1119 gtk_toolbar_insert_space (GtkToolbar *toolbar,
1122 gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_SPACE,
1130 gtk_toolbar_remove_space (GtkToolbar *toolbar,
1134 GtkToolbarChild *child;
1137 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1140 for (children = toolbar->children; children; children = children->next)
1142 child = children->data;
1146 if (child->type == GTK_TOOLBAR_CHILD_SPACE)
1148 toolbar->children = g_list_remove_link (toolbar->children, children);
1150 g_list_free (children);
1151 toolbar->num_children--;
1153 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
1157 g_warning ("Toolbar position %d is not a space", position);
1166 g_warning ("Toolbar position %d doesn't exist", position);
1170 gtk_toolbar_append_widget (GtkToolbar *toolbar,
1172 const gchar *tooltip_text,
1173 const gchar *tooltip_private_text)
1175 gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_WIDGET,
1177 tooltip_text, tooltip_private_text,
1179 toolbar->num_children);
1183 gtk_toolbar_prepend_widget (GtkToolbar *toolbar,
1185 const gchar *tooltip_text,
1186 const gchar *tooltip_private_text)
1188 gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_WIDGET,
1190 tooltip_text, tooltip_private_text,
1196 gtk_toolbar_insert_widget (GtkToolbar *toolbar,
1198 const char *tooltip_text,
1199 const char *tooltip_private_text,
1202 gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_WIDGET,
1204 tooltip_text, tooltip_private_text,
1210 gtk_toolbar_append_element (GtkToolbar *toolbar,
1211 GtkToolbarChildType type,
1214 const char *tooltip_text,
1215 const char *tooltip_private_text,
1217 GtkSignalFunc callback,
1220 return gtk_toolbar_insert_element (toolbar, type, widget, text,
1221 tooltip_text, tooltip_private_text,
1222 icon, callback, user_data,
1223 toolbar->num_children);
1227 gtk_toolbar_prepend_element (GtkToolbar *toolbar,
1228 GtkToolbarChildType type,
1231 const char *tooltip_text,
1232 const char *tooltip_private_text,
1234 GtkSignalFunc callback,
1237 return gtk_toolbar_insert_element (toolbar, type, widget, text,
1238 tooltip_text, tooltip_private_text,
1239 icon, callback, user_data, 0);
1243 gtk_toolbar_insert_element (GtkToolbar *toolbar,
1244 GtkToolbarChildType type,
1247 const char *tooltip_text,
1248 const char *tooltip_private_text,
1250 GtkSignalFunc callback,
1254 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), NULL);
1255 if (type == GTK_TOOLBAR_CHILD_WIDGET)
1257 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1259 else if (type != GTK_TOOLBAR_CHILD_RADIOBUTTON)
1260 g_return_val_if_fail (widget == NULL, NULL);
1262 return gtk_toolbar_internal_insert_element (toolbar, type, widget, text,
1263 tooltip_text, tooltip_private_text,
1264 icon, callback, user_data,
1269 gtk_toolbar_internal_insert_element (GtkToolbar *toolbar,
1270 GtkToolbarChildType type,
1273 const char *tooltip_text,
1274 const char *tooltip_private_text,
1276 GtkSignalFunc callback,
1279 gboolean has_mnemonic)
1281 GtkToolbarChild *child;
1284 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), NULL);
1285 if (type == GTK_TOOLBAR_CHILD_WIDGET)
1287 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1289 else if (type != GTK_TOOLBAR_CHILD_RADIOBUTTON)
1290 g_return_val_if_fail (widget == NULL, NULL);
1292 if (type == GTK_TOOLBAR_CHILD_SPACE)
1293 child = (GtkToolbarChild *) g_new (GtkToolbarChildSpace, 1);
1295 child = g_new (GtkToolbarChild, 1);
1299 child->label = NULL;
1303 case GTK_TOOLBAR_CHILD_SPACE:
1304 child->widget = NULL;
1305 ((GtkToolbarChildSpace *) child)->alloc_x =
1306 ((GtkToolbarChildSpace *) child)->alloc_y = 0;
1309 case GTK_TOOLBAR_CHILD_WIDGET:
1310 child->widget = widget;
1313 case GTK_TOOLBAR_CHILD_BUTTON:
1314 case GTK_TOOLBAR_CHILD_TOGGLEBUTTON:
1315 case GTK_TOOLBAR_CHILD_RADIOBUTTON:
1316 if (type == GTK_TOOLBAR_CHILD_BUTTON)
1318 child->widget = gtk_button_new ();
1319 gtk_button_set_relief (GTK_BUTTON (child->widget), get_button_relief (toolbar));
1321 else if (type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON)
1323 child->widget = gtk_toggle_button_new ();
1324 gtk_button_set_relief (GTK_BUTTON (child->widget), get_button_relief (toolbar));
1325 gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (child->widget),
1330 child->widget = gtk_radio_button_new (widget
1331 ? gtk_radio_button_get_group (GTK_RADIO_BUTTON (widget))
1333 gtk_button_set_relief (GTK_BUTTON (child->widget), get_button_relief (toolbar));
1334 gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (child->widget), FALSE);
1337 GTK_WIDGET_UNSET_FLAGS (child->widget, GTK_CAN_FOCUS);
1340 gtk_signal_connect (GTK_OBJECT (child->widget), "clicked",
1341 callback, user_data);
1343 if (toolbar->style == GTK_TOOLBAR_BOTH_HORIZ)
1344 box = gtk_hbox_new (FALSE, 0);
1346 box = gtk_vbox_new (FALSE, 0);
1347 gtk_container_add (GTK_CONTAINER (child->widget), box);
1348 gtk_widget_show (box);
1353 child->label = gtk_label_new_with_mnemonic (text);
1355 child->label = gtk_label_new (text);
1356 gtk_box_pack_end (GTK_BOX (box), child->label, FALSE, FALSE, 0);
1357 if (toolbar->style != GTK_TOOLBAR_ICONS)
1358 gtk_widget_show (child->label);
1363 child->icon = GTK_WIDGET (icon);
1364 gtk_box_pack_end (GTK_BOX (box), child->icon, FALSE, FALSE, 0);
1365 if (toolbar->style != GTK_TOOLBAR_TEXT)
1366 gtk_widget_show (child->icon);
1369 if (type != GTK_TOOLBAR_CHILD_WIDGET)
1371 /* Mark child as ours */
1372 g_object_set_data (G_OBJECT (child->widget),
1373 "gtk-toolbar-is-child",
1374 GINT_TO_POINTER (TRUE));
1376 gtk_widget_show (child->widget);
1380 g_assert_not_reached ();
1383 if ((type != GTK_TOOLBAR_CHILD_SPACE) && tooltip_text)
1384 gtk_tooltips_set_tip (toolbar->tooltips, child->widget,
1385 tooltip_text, tooltip_private_text);
1387 toolbar->children = g_list_insert (toolbar->children, child, position);
1388 toolbar->num_children++;
1390 if (type != GTK_TOOLBAR_CHILD_SPACE)
1391 gtk_widget_set_parent (child->widget, GTK_WIDGET (toolbar));
1393 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
1395 return child->widget;
1399 gtk_toolbar_set_orientation (GtkToolbar *toolbar,
1400 GtkOrientation orientation)
1402 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1404 gtk_signal_emit (GTK_OBJECT (toolbar), toolbar_signals[ORIENTATION_CHANGED], orientation);
1408 * gtk_toolbar_get_orientation:
1409 * @toolbar: a #GtkToolbar
1411 * Retrieves the current orientation of the toolbar. See
1412 * gtk_toolbar_set_orientation().
1414 * Return value: the orientation
1417 gtk_toolbar_get_orientation (GtkToolbar *toolbar)
1419 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), GTK_ORIENTATION_HORIZONTAL);
1421 return toolbar->orientation;
1425 gtk_toolbar_set_style (GtkToolbar *toolbar,
1426 GtkToolbarStyle style)
1428 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1430 toolbar->style_set = TRUE;
1431 gtk_signal_emit (GTK_OBJECT (toolbar), toolbar_signals[STYLE_CHANGED], style);
1435 * gtk_toolbar_get_style:
1436 * @toolbar: a #GtkToolbar
1438 * Retrieves whether the toolbar has text, icons, or both . See
1439 * gtk_toolbar_set_style().
1441 * Return value: the current style of @toolbar
1444 gtk_toolbar_get_style (GtkToolbar *toolbar)
1446 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), GTK_TOOLBAR_BOTH);
1448 return toolbar->style;
1452 * gtk_toolbar_unset_style:
1453 * @toolbar: a #GtkToolbar
1455 * Unsets a toolbar style set with gtk_toolbar_set_style(), so that
1456 * user preferences will be used to determine the toolbar style.
1459 gtk_toolbar_unset_style (GtkToolbar *toolbar)
1461 GtkToolbarStyle style;
1463 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1465 if (toolbar->style_set)
1467 g_object_get (gtk_settings_get_default (),
1468 "gtk-toolbar-style",
1472 if (style != toolbar->style)
1473 gtk_signal_emit (GTK_OBJECT (toolbar), toolbar_signals[STYLE_CHANGED], style);
1475 toolbar->style_set = FALSE;
1480 gtk_toolbar_set_tooltips (GtkToolbar *toolbar,
1483 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1486 gtk_tooltips_enable (toolbar->tooltips);
1488 gtk_tooltips_disable (toolbar->tooltips);
1492 * gtk_toolbar_get_tooltips:
1493 * @toolbar: a #GtkToolbar
1495 * Retrieves whether tooltips are enabled. See
1496 * gtk_toolbar_set_tooltips().
1498 * Return value: %TRUE if tooltips are enabled
1501 gtk_toolbar_get_tooltips (GtkToolbar *toolbar)
1503 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
1505 return toolbar->tooltips->enabled;
1509 gtk_toolbar_update_button_relief (GtkToolbar *toolbar)
1512 GtkToolbarChild *child;
1513 GtkReliefStyle relief;
1515 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1517 relief = get_button_relief (toolbar);
1519 for (children = toolbar->children; children; children = children->next)
1521 child = children->data;
1522 if (child->type == GTK_TOOLBAR_CHILD_BUTTON ||
1523 child->type == GTK_TOOLBAR_CHILD_RADIOBUTTON ||
1524 child->type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON)
1525 gtk_button_set_relief (GTK_BUTTON (child->widget), relief);
1530 gtk_real_toolbar_orientation_changed (GtkToolbar *toolbar,
1531 GtkOrientation orientation)
1533 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1535 if (toolbar->orientation != orientation)
1537 toolbar->orientation = orientation;
1538 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
1539 g_object_notify (G_OBJECT (toolbar), "orientation");
1544 gtk_real_toolbar_style_changed (GtkToolbar *toolbar,
1545 GtkToolbarStyle style)
1548 GtkToolbarChild *child;
1549 GtkWidget* box = NULL;
1551 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
1553 if (toolbar->style != style)
1555 toolbar->style = style;
1557 for (children = toolbar->children; children; children = children->next)
1559 child = children->data;
1561 if (child->type == GTK_TOOLBAR_CHILD_BUTTON ||
1562 child->type == GTK_TOOLBAR_CHILD_RADIOBUTTON ||
1563 child->type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON)
1566 case GTK_TOOLBAR_ICONS:
1567 if (child->icon && !GTK_WIDGET_VISIBLE (child->icon))
1568 gtk_widget_show (child->icon);
1570 if (child->label && GTK_WIDGET_VISIBLE (child->label))
1571 gtk_widget_hide (child->label);
1575 case GTK_TOOLBAR_TEXT:
1576 if (child->icon && GTK_WIDGET_VISIBLE (child->icon))
1577 gtk_widget_hide (child->icon);
1579 if (child->label && !GTK_WIDGET_VISIBLE (child->label))
1580 gtk_widget_show (child->label);
1584 case GTK_TOOLBAR_BOTH:
1585 if (child->icon && !GTK_WIDGET_VISIBLE (child->icon))
1586 gtk_widget_show (child->icon);
1588 if (child->label && !GTK_WIDGET_VISIBLE (child->label))
1589 gtk_widget_show (child->label);
1591 box = (GtkWidget*)gtk_container_get_children (GTK_CONTAINER (child->widget))->data;
1593 if (GTK_IS_HBOX (box))
1597 gtk_object_ref (GTK_OBJECT (child->icon));
1598 gtk_container_remove (GTK_CONTAINER (box),
1603 gtk_object_ref (GTK_OBJECT (child->label));
1604 gtk_container_remove (GTK_CONTAINER (box),
1607 gtk_container_remove (GTK_CONTAINER (child->widget),
1610 box = gtk_vbox_new (FALSE, 0);
1611 gtk_widget_show (box);
1615 gtk_box_pack_end (GTK_BOX (box), child->label, FALSE, FALSE, 0);
1616 gtk_object_unref (GTK_OBJECT (child->label));
1620 gtk_box_pack_end (GTK_BOX (box), child->icon, FALSE, FALSE, 0);
1621 gtk_object_unref (GTK_OBJECT (child->icon));
1623 gtk_container_add (GTK_CONTAINER (child->widget),
1629 case GTK_TOOLBAR_BOTH_HORIZ:
1630 if (child->icon && !GTK_WIDGET_VISIBLE (child->icon))
1631 gtk_widget_show (child->icon);
1632 if (child->label && !GTK_WIDGET_VISIBLE (child->label))
1633 gtk_widget_show (child->label);
1635 box = (GtkWidget*)gtk_container_get_children (GTK_CONTAINER (child->widget))->data;
1637 if (GTK_IS_VBOX (box))
1641 gtk_object_ref (GTK_OBJECT (child->icon));
1642 gtk_container_remove (GTK_CONTAINER (box),
1647 gtk_object_ref (GTK_OBJECT (child->label));
1648 gtk_container_remove (GTK_CONTAINER (box),
1651 gtk_container_remove (GTK_CONTAINER (child->widget),
1654 box = gtk_hbox_new (FALSE, 0);
1655 gtk_widget_show (box);
1659 gtk_box_pack_end (GTK_BOX (box), child->label, TRUE, TRUE, 0);
1660 gtk_object_unref (GTK_OBJECT (child->label));
1664 gtk_box_pack_end (GTK_BOX (box), child->icon, FALSE, FALSE, 0);
1665 gtk_object_unref (GTK_OBJECT (child->icon));
1667 gtk_container_add (GTK_CONTAINER (child->widget), box);
1674 g_assert_not_reached ();
1678 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
1679 g_object_notify (G_OBJECT (toolbar), "toolbar_style");
1684 static GtkReliefStyle
1685 get_button_relief (GtkToolbar *toolbar)
1687 GtkReliefStyle button_relief = GTK_RELIEF_NORMAL;
1689 gtk_widget_style_get (GTK_WIDGET (toolbar),
1690 "button_relief", &button_relief,
1693 return button_relief;
1697 get_space_size (GtkToolbar *toolbar)
1699 gint space_size = DEFAULT_SPACE_SIZE;
1701 gtk_widget_style_get (GTK_WIDGET (toolbar),
1702 "space_size", &space_size,
1708 static GtkToolbarSpaceStyle
1709 get_space_style (GtkToolbar *toolbar)
1711 GtkToolbarSpaceStyle space_style = DEFAULT_SPACE_STYLE;
1713 gtk_widget_style_get (GTK_WIDGET (toolbar),
1714 "space_style", &space_style,