1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * GtkToolbar copyright (C) Federico Mena
5 * Copyright (C) 2002 Anders Carlsson <andersca@gnome.org>
6 * Copyright (C) 2002 James Henstridge <james@daa.com.au>
7 * Copyright (C) 2003, 2004 Soeren Sandmann <sandmann@daimi.au.dk>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
27 * file for a list of people on the GTK+ Team. See the ChangeLog
28 * files for a list of changes. These files are distributed with
29 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
38 #include "gtktoolbar.h"
41 #include "gtkbindings.h"
45 #include "gtkmainprivate.h"
46 #include "gtkmarshalers.h"
48 #include "gtkorientable.h"
49 #include "gtkradiobutton.h"
50 #include "gtkradiotoolbutton.h"
51 #include "gtkseparatormenuitem.h"
52 #include "gtkseparatortoolitem.h"
54 #include "gtktoolshell.h"
56 #include "gtkprivate.h"
58 #include "gtktypebuiltins.h"
63 * @Short_description: Create bars of buttons and other widgets
65 * @See_also: #GtkToolItem
67 * A toolbar is created with a call to gtk_toolbar_new().
69 * A toolbar can contain instances of a subclass of #GtkToolItem. To add
70 * a #GtkToolItem to the a toolbar, use gtk_toolbar_insert(). To remove
71 * an item from the toolbar use gtk_container_remove(). To add a button
72 * to the toolbar, add an instance of #GtkToolButton.
74 * Toolbar items can be visually grouped by adding instances of
75 * #GtkSeparatorToolItem to the toolbar. If the GtkToolbar child property
76 * "expand" is #TRUE and the property #GtkSeparatorToolItem:draw is set to
77 * #FALSE, the effect is to force all following items to the end of the toolbar.
79 * Creating a context menu for the toolbar can be done by connecting to
80 * the #GtkToolbar::popup-context-menu signal.
84 typedef struct _ToolbarContent ToolbarContent;
86 #define DEFAULT_IPADDING 0
88 #define DEFAULT_SPACE_SIZE 12
89 #define DEFAULT_SPACE_STYLE GTK_TOOLBAR_SPACE_LINE
90 #define SPACE_LINE_DIVISION 10.0
91 #define SPACE_LINE_START 2.0
92 #define SPACE_LINE_END 8.0
94 #define DEFAULT_ICON_SIZE GTK_ICON_SIZE_LARGE_TOOLBAR
95 #define DEFAULT_TOOLBAR_STYLE GTK_TOOLBAR_BOTH
96 #define DEFAULT_ANIMATION_STATE TRUE
98 #define MAX_HOMOGENEOUS_N_CHARS 13 /* Items that are wider than this do not participate
99 * in the homogeneous game. In units of
100 * pango_font_get_estimated_char_width().
102 #define SLIDE_SPEED 600.0 /* How fast the items slide, in pixels per second */
103 #define ACCEL_THRESHOLD 0.18 /* After how much time in seconds will items start speeding up */
106 struct _GtkToolbarPrivate
109 GtkSettings *settings;
111 GtkIconSize icon_size;
112 GtkToolbarStyle style;
114 GtkToolItem *highlight_tool_item;
116 GtkWidget *arrow_button;
118 GdkWindow *event_window;
124 GtkWidgetPath *sibling_path;
126 gulong settings_connection;
129 gint button_maxw; /* maximum width of homogeneous children */
130 gint button_maxh; /* maximum height of homogeneous children */
131 gint max_homogeneous_pixels;
134 GtkOrientation orientation;
137 guint icon_size_set : 1;
138 guint is_sliding : 1;
139 guint need_rebuild : 1; /* whether the overflow menu should be regenerated */
141 guint show_arrow : 1;
156 /* Child properties */
160 CHILD_PROP_HOMOGENEOUS
180 static void gtk_toolbar_set_property (GObject *object,
184 static void gtk_toolbar_get_property (GObject *object,
188 static gint gtk_toolbar_draw (GtkWidget *widget,
190 static void gtk_toolbar_realize (GtkWidget *widget);
191 static void gtk_toolbar_unrealize (GtkWidget *widget);
192 static void gtk_toolbar_get_preferred_width (GtkWidget *widget,
195 static void gtk_toolbar_get_preferred_height (GtkWidget *widget,
199 static void gtk_toolbar_size_allocate (GtkWidget *widget,
200 GtkAllocation *allocation);
201 static void gtk_toolbar_style_updated (GtkWidget *widget);
202 static gboolean gtk_toolbar_focus (GtkWidget *widget,
203 GtkDirectionType dir);
204 static void gtk_toolbar_move_focus (GtkWidget *widget,
205 GtkDirectionType dir);
206 static void gtk_toolbar_screen_changed (GtkWidget *widget,
207 GdkScreen *previous_screen);
208 static void gtk_toolbar_map (GtkWidget *widget);
209 static void gtk_toolbar_unmap (GtkWidget *widget);
210 static void gtk_toolbar_set_child_property (GtkContainer *container,
215 static void gtk_toolbar_get_child_property (GtkContainer *container,
220 static void gtk_toolbar_finalize (GObject *object);
221 static void gtk_toolbar_show_all (GtkWidget *widget);
222 static void gtk_toolbar_add (GtkContainer *container,
224 static void gtk_toolbar_remove (GtkContainer *container,
226 static void gtk_toolbar_forall (GtkContainer *container,
227 gboolean include_internals,
228 GtkCallback callback,
229 gpointer callback_data);
230 static GType gtk_toolbar_child_type (GtkContainer *container);
231 static GtkWidgetPath * gtk_toolbar_get_path_for_child
232 (GtkContainer *container,
234 static void gtk_toolbar_invalidate_order (GtkToolbar *toolbar);
236 static void gtk_toolbar_orientation_changed (GtkToolbar *toolbar,
237 GtkOrientation orientation);
238 static void gtk_toolbar_real_style_changed (GtkToolbar *toolbar,
239 GtkToolbarStyle style);
240 static gboolean gtk_toolbar_focus_home_or_end (GtkToolbar *toolbar,
241 gboolean focus_home);
242 static gboolean gtk_toolbar_button_press (GtkWidget *toolbar,
243 GdkEventButton *event);
244 static gboolean gtk_toolbar_arrow_button_press (GtkWidget *button,
245 GdkEventButton *event,
246 GtkToolbar *toolbar);
247 static void gtk_toolbar_arrow_button_clicked (GtkWidget *button,
248 GtkToolbar *toolbar);
249 static void gtk_toolbar_update_button_relief (GtkToolbar *toolbar);
250 static gboolean gtk_toolbar_popup_menu (GtkWidget *toolbar);
251 static void gtk_toolbar_reconfigured (GtkToolbar *toolbar);
253 static GtkReliefStyle get_button_relief (GtkToolbar *toolbar);
254 static gint get_internal_padding (GtkToolbar *toolbar);
255 static gint get_max_child_expand (GtkToolbar *toolbar);
256 static GtkShadowType get_shadow_type (GtkToolbar *toolbar);
258 /* methods on ToolbarContent 'class' */
259 static ToolbarContent *toolbar_content_new_tool_item (GtkToolbar *toolbar,
261 gboolean is_placeholder,
263 static void toolbar_content_remove (ToolbarContent *content,
264 GtkToolbar *toolbar);
265 static void toolbar_content_free (ToolbarContent *content);
266 static void toolbar_content_draw (ToolbarContent *content,
267 GtkContainer *container,
269 static gboolean toolbar_content_visible (ToolbarContent *content,
270 GtkToolbar *toolbar);
271 static void toolbar_content_size_request (ToolbarContent *content,
273 GtkRequisition *requisition);
274 static gboolean toolbar_content_is_homogeneous (ToolbarContent *content,
275 GtkToolbar *toolbar);
276 static gboolean toolbar_content_is_placeholder (ToolbarContent *content);
277 static gboolean toolbar_content_disappearing (ToolbarContent *content);
278 static ItemState toolbar_content_get_state (ToolbarContent *content);
279 static gboolean toolbar_content_child_visible (ToolbarContent *content);
280 static void toolbar_content_get_goal_allocation (ToolbarContent *content,
281 GtkAllocation *allocation);
282 static void toolbar_content_get_allocation (ToolbarContent *content,
283 GtkAllocation *allocation);
284 static void toolbar_content_set_start_allocation (ToolbarContent *content,
285 GtkAllocation *new_start_allocation);
286 static void toolbar_content_get_start_allocation (ToolbarContent *content,
287 GtkAllocation *start_allocation);
288 static gboolean toolbar_content_get_expand (ToolbarContent *content);
289 static void toolbar_content_set_goal_allocation (ToolbarContent *content,
290 GtkAllocation *allocation);
291 static void toolbar_content_set_child_visible (ToolbarContent *content,
294 static void toolbar_content_size_allocate (ToolbarContent *content,
295 GtkAllocation *allocation);
296 static void toolbar_content_set_state (ToolbarContent *content,
297 ItemState new_state);
298 static GtkWidget * toolbar_content_get_widget (ToolbarContent *content);
299 static void toolbar_content_set_disappearing (ToolbarContent *content,
300 gboolean disappearing);
301 static void toolbar_content_set_size_request (ToolbarContent *content,
304 static void toolbar_content_toolbar_reconfigured (ToolbarContent *content,
305 GtkToolbar *toolbar);
306 static GtkWidget * toolbar_content_retrieve_menu_item (ToolbarContent *content);
307 static gboolean toolbar_content_has_proxy_menu_item (ToolbarContent *content);
308 static gboolean toolbar_content_is_separator (ToolbarContent *content);
309 static void toolbar_content_show_all (ToolbarContent *content);
310 static void toolbar_content_set_expand (ToolbarContent *content,
313 static void toolbar_tool_shell_iface_init (GtkToolShellIface *iface);
314 static GtkIconSize toolbar_get_icon_size (GtkToolShell *shell);
315 static GtkOrientation toolbar_get_orientation (GtkToolShell *shell);
316 static GtkToolbarStyle toolbar_get_style (GtkToolShell *shell);
317 static GtkReliefStyle toolbar_get_relief_style (GtkToolShell *shell);
318 static void toolbar_rebuild_menu (GtkToolShell *shell);
321 G_DEFINE_TYPE_WITH_CODE (GtkToolbar, gtk_toolbar, GTK_TYPE_CONTAINER,
322 G_IMPLEMENT_INTERFACE (GTK_TYPE_TOOL_SHELL,
323 toolbar_tool_shell_iface_init)
324 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
327 static guint toolbar_signals[LAST_SIGNAL] = { 0 };
331 add_arrow_bindings (GtkBindingSet *binding_set,
333 GtkDirectionType dir)
335 guint keypad_keysym = keysym - GDK_KEY_Left + GDK_KEY_KP_Left;
337 gtk_binding_entry_add_signal (binding_set, keysym, 0,
339 GTK_TYPE_DIRECTION_TYPE, dir);
340 gtk_binding_entry_add_signal (binding_set, keypad_keysym, 0,
342 GTK_TYPE_DIRECTION_TYPE, dir);
346 add_ctrl_tab_bindings (GtkBindingSet *binding_set,
347 GdkModifierType modifiers,
348 GtkDirectionType direction)
350 gtk_binding_entry_add_signal (binding_set,
351 GDK_KEY_Tab, GDK_CONTROL_MASK | modifiers,
353 GTK_TYPE_DIRECTION_TYPE, direction);
354 gtk_binding_entry_add_signal (binding_set,
355 GDK_KEY_KP_Tab, GDK_CONTROL_MASK | modifiers,
357 GTK_TYPE_DIRECTION_TYPE, direction);
361 gtk_toolbar_class_init (GtkToolbarClass *klass)
363 GObjectClass *gobject_class;
364 GtkWidgetClass *widget_class;
365 GtkContainerClass *container_class;
366 GtkBindingSet *binding_set;
368 gobject_class = (GObjectClass *)klass;
369 widget_class = (GtkWidgetClass *)klass;
370 container_class = (GtkContainerClass *)klass;
372 gobject_class->set_property = gtk_toolbar_set_property;
373 gobject_class->get_property = gtk_toolbar_get_property;
374 gobject_class->finalize = gtk_toolbar_finalize;
376 widget_class->button_press_event = gtk_toolbar_button_press;
377 widget_class->draw = gtk_toolbar_draw;
378 widget_class->get_preferred_width = gtk_toolbar_get_preferred_width;
379 widget_class->get_preferred_height = gtk_toolbar_get_preferred_height;
380 widget_class->size_allocate = gtk_toolbar_size_allocate;
381 widget_class->style_updated = gtk_toolbar_style_updated;
382 widget_class->focus = gtk_toolbar_focus;
384 /* need to override the base class function via override_class_handler,
385 * because the signal slot is not available in GtkWidgetClass
387 g_signal_override_class_handler ("move-focus",
389 G_CALLBACK (gtk_toolbar_move_focus));
391 widget_class->screen_changed = gtk_toolbar_screen_changed;
392 widget_class->realize = gtk_toolbar_realize;
393 widget_class->unrealize = gtk_toolbar_unrealize;
394 widget_class->map = gtk_toolbar_map;
395 widget_class->unmap = gtk_toolbar_unmap;
396 widget_class->popup_menu = gtk_toolbar_popup_menu;
397 widget_class->show_all = gtk_toolbar_show_all;
399 container_class->add = gtk_toolbar_add;
400 container_class->remove = gtk_toolbar_remove;
401 container_class->forall = gtk_toolbar_forall;
402 container_class->child_type = gtk_toolbar_child_type;
403 container_class->get_child_property = gtk_toolbar_get_child_property;
404 container_class->set_child_property = gtk_toolbar_set_child_property;
405 container_class->get_path_for_child = gtk_toolbar_get_path_for_child;
407 klass->orientation_changed = gtk_toolbar_orientation_changed;
408 klass->style_changed = gtk_toolbar_real_style_changed;
411 * GtkToolbar::orientation-changed:
412 * @toolbar: the object which emitted the signal
413 * @orientation: the new #GtkOrientation of the toolbar
415 * Emitted when the orientation of the toolbar changes.
417 toolbar_signals[ORIENTATION_CHANGED] =
418 g_signal_new (I_("orientation-changed"),
419 G_OBJECT_CLASS_TYPE (klass),
421 G_STRUCT_OFFSET (GtkToolbarClass, orientation_changed),
423 g_cclosure_marshal_VOID__ENUM,
425 GTK_TYPE_ORIENTATION);
427 * GtkToolbar::style-changed:
428 * @toolbar: The #GtkToolbar which emitted the signal
429 * @style: the new #GtkToolbarStyle of the toolbar
431 * Emitted when the style of the toolbar changes.
433 toolbar_signals[STYLE_CHANGED] =
434 g_signal_new (I_("style-changed"),
435 G_OBJECT_CLASS_TYPE (klass),
437 G_STRUCT_OFFSET (GtkToolbarClass, style_changed),
439 g_cclosure_marshal_VOID__ENUM,
441 GTK_TYPE_TOOLBAR_STYLE);
443 * GtkToolbar::popup-context-menu:
444 * @toolbar: the #GtkToolbar which emitted the signal
445 * @x: the x coordinate of the point where the menu should appear
446 * @y: the y coordinate of the point where the menu should appear
447 * @button: the mouse button the user pressed, or -1
449 * Emitted when the user right-clicks the toolbar or uses the
450 * keybinding to display a popup menu.
452 * Application developers should handle this signal if they want
453 * to display a context menu on the toolbar. The context-menu should
454 * appear at the coordinates given by @x and @y. The mouse button
455 * number is given by the @button parameter. If the menu was popped
456 * up using the keybaord, @button is -1.
458 * Return value: return %TRUE if the signal was handled, %FALSE if not
460 toolbar_signals[POPUP_CONTEXT_MENU] =
461 g_signal_new (I_("popup-context-menu"),
462 G_OBJECT_CLASS_TYPE (klass),
464 G_STRUCT_OFFSET (GtkToolbarClass, popup_context_menu),
465 _gtk_boolean_handled_accumulator, NULL,
466 _gtk_marshal_BOOLEAN__INT_INT_INT,
468 G_TYPE_INT, G_TYPE_INT,
472 * GtkToolbar::focus-home-or-end:
473 * @toolbar: the #GtkToolbar which emitted the signal
474 * @focus_home: %TRUE if the first item should be focused
476 * A keybinding signal used internally by GTK+. This signal can't
477 * be used in application code
479 * Return value: %TRUE if the signal was handled, %FALSE if not
481 toolbar_signals[FOCUS_HOME_OR_END] =
482 g_signal_new_class_handler (I_("focus-home-or-end"),
483 G_OBJECT_CLASS_TYPE (klass),
484 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
485 G_CALLBACK (gtk_toolbar_focus_home_or_end),
487 _gtk_marshal_BOOLEAN__BOOLEAN,
492 g_object_class_override_property (gobject_class,
496 g_object_class_install_property (gobject_class,
498 g_param_spec_enum ("toolbar-style",
500 P_("How to draw the toolbar"),
501 GTK_TYPE_TOOLBAR_STYLE,
502 DEFAULT_TOOLBAR_STYLE,
503 GTK_PARAM_READWRITE));
504 g_object_class_install_property (gobject_class,
506 g_param_spec_boolean ("show-arrow",
508 P_("If an arrow should be shown if the toolbar doesn't fit"),
510 GTK_PARAM_READWRITE));
513 * GtkToolbar:icon-size:
515 * The size of the icons in a toolbar is normally determined by
516 * the toolbar-icon-size setting. When this property is set, it
517 * overrides the setting.
519 * This should only be used for special-purpose toolbars, normal
520 * application toolbars should respect the user preferences for the
525 g_object_class_install_property (gobject_class,
527 g_param_spec_int ("icon-size",
529 P_("Size of icons in this toolbar"),
532 GTK_PARAM_READWRITE));
535 * GtkToolbar:icon-size-set:
537 * Is %TRUE if the icon-size property has been set.
541 g_object_class_install_property (gobject_class,
543 g_param_spec_boolean ("icon-size-set",
545 P_("Whether the icon-size property has been set"),
547 GTK_PARAM_READWRITE));
549 /* child properties */
550 gtk_container_class_install_child_property (container_class,
552 g_param_spec_boolean ("expand",
554 P_("Whether the item should receive extra space when the toolbar grows"),
556 GTK_PARAM_READWRITE));
558 gtk_container_class_install_child_property (container_class,
559 CHILD_PROP_HOMOGENEOUS,
560 g_param_spec_boolean ("homogeneous",
562 P_("Whether the item should be the same size as other homogeneous items"),
564 GTK_PARAM_READWRITE));
566 /* style properties */
567 gtk_widget_class_install_style_property (widget_class,
568 g_param_spec_int ("space-size",
570 P_("Size of spacers"),
574 GTK_PARAM_READABLE));
576 gtk_widget_class_install_style_property (widget_class,
577 g_param_spec_int ("internal-padding",
578 P_("Internal padding"),
579 P_("Amount of border space between the toolbar shadow and the buttons"),
583 GTK_PARAM_READABLE));
585 gtk_widget_class_install_style_property (widget_class,
586 g_param_spec_int ("max-child-expand",
587 P_("Maximum child expand"),
588 P_("Maximum amount of space an expandable item will be given"),
592 GTK_PARAM_READABLE));
594 gtk_widget_class_install_style_property (widget_class,
595 g_param_spec_enum ("space-style",
597 P_("Whether spacers are vertical lines or just blank"),
598 GTK_TYPE_TOOLBAR_SPACE_STYLE,
600 GTK_PARAM_READABLE));
602 gtk_widget_class_install_style_property (widget_class,
603 g_param_spec_enum ("button-relief",
605 P_("Type of bevel around toolbar buttons"),
606 GTK_TYPE_RELIEF_STYLE,
608 GTK_PARAM_READABLE));
609 gtk_widget_class_install_style_property (widget_class,
610 g_param_spec_enum ("shadow-type",
612 P_("Style of bevel around the toolbar"),
613 GTK_TYPE_SHADOW_TYPE,
615 GTK_PARAM_READABLE));
617 binding_set = gtk_binding_set_by_class (klass);
619 add_arrow_bindings (binding_set, GDK_KEY_Left, GTK_DIR_LEFT);
620 add_arrow_bindings (binding_set, GDK_KEY_Right, GTK_DIR_RIGHT);
621 add_arrow_bindings (binding_set, GDK_KEY_Up, GTK_DIR_UP);
622 add_arrow_bindings (binding_set, GDK_KEY_Down, GTK_DIR_DOWN);
624 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
625 "focus-home-or-end", 1,
626 G_TYPE_BOOLEAN, TRUE);
627 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
628 "focus-home-or-end", 1,
629 G_TYPE_BOOLEAN, TRUE);
630 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
631 "focus-home-or-end", 1,
632 G_TYPE_BOOLEAN, FALSE);
633 gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
634 "focus-home-or-end", 1,
635 G_TYPE_BOOLEAN, FALSE);
637 add_ctrl_tab_bindings (binding_set, 0, GTK_DIR_TAB_FORWARD);
638 add_ctrl_tab_bindings (binding_set, GDK_SHIFT_MASK, GTK_DIR_TAB_BACKWARD);
640 g_type_class_add_private (gobject_class, sizeof (GtkToolbarPrivate));
644 toolbar_tool_shell_iface_init (GtkToolShellIface *iface)
646 iface->get_icon_size = toolbar_get_icon_size;
647 iface->get_orientation = toolbar_get_orientation;
648 iface->get_style = toolbar_get_style;
649 iface->get_relief_style = toolbar_get_relief_style;
650 iface->rebuild_menu = toolbar_rebuild_menu;
654 gtk_toolbar_init (GtkToolbar *toolbar)
656 GtkToolbarPrivate *priv;
657 GtkStyleContext *context;
659 toolbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (toolbar,
662 priv = toolbar->priv;
664 gtk_widget_set_can_focus (GTK_WIDGET (toolbar), FALSE);
665 gtk_widget_set_has_window (GTK_WIDGET (toolbar), FALSE);
667 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
668 priv->style = DEFAULT_TOOLBAR_STYLE;
669 priv->icon_size = DEFAULT_ICON_SIZE;
670 priv->animation = DEFAULT_ANIMATION_STATE;
672 priv->arrow_button = gtk_toggle_button_new ();
673 g_signal_connect (priv->arrow_button, "button-press-event",
674 G_CALLBACK (gtk_toolbar_arrow_button_press), toolbar);
675 g_signal_connect (priv->arrow_button, "clicked",
676 G_CALLBACK (gtk_toolbar_arrow_button_clicked), toolbar);
677 gtk_button_set_relief (GTK_BUTTON (priv->arrow_button),
678 get_button_relief (toolbar));
680 gtk_button_set_focus_on_click (GTK_BUTTON (priv->arrow_button), FALSE);
682 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
683 gtk_widget_set_name (priv->arrow, "gtk-toolbar-arrow");
684 gtk_widget_show (priv->arrow);
685 gtk_container_add (GTK_CONTAINER (priv->arrow_button), priv->arrow);
687 gtk_widget_set_parent (priv->arrow_button, GTK_WIDGET (toolbar));
689 /* which child position a drop will occur at */
691 priv->show_arrow = TRUE;
692 priv->settings = NULL;
694 priv->max_homogeneous_pixels = -1;
696 priv->timer = g_timer_new ();
698 context = gtk_widget_get_style_context (GTK_WIDGET (toolbar));
699 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLBAR);
703 gtk_toolbar_set_property (GObject *object,
708 GtkToolbar *toolbar = GTK_TOOLBAR (object);
709 GtkToolbarPrivate *priv = toolbar->priv;
713 case PROP_ORIENTATION:
714 g_signal_emit (toolbar, toolbar_signals[ORIENTATION_CHANGED], 0,
715 g_value_get_enum (value));
717 case PROP_TOOLBAR_STYLE:
718 gtk_toolbar_set_style (toolbar, g_value_get_enum (value));
720 case PROP_SHOW_ARROW:
721 gtk_toolbar_set_show_arrow (toolbar, g_value_get_boolean (value));
724 gtk_toolbar_set_icon_size (toolbar, g_value_get_int (value));
726 case PROP_ICON_SIZE_SET:
727 if (g_value_get_boolean (value))
728 priv->icon_size_set = TRUE;
730 gtk_toolbar_unset_icon_size (toolbar);
733 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
739 gtk_toolbar_get_property (GObject *object,
744 GtkToolbar *toolbar = GTK_TOOLBAR (object);
745 GtkToolbarPrivate *priv = toolbar->priv;
749 case PROP_ORIENTATION:
750 g_value_set_enum (value, priv->orientation);
752 case PROP_TOOLBAR_STYLE:
753 g_value_set_enum (value, priv->style);
755 case PROP_SHOW_ARROW:
756 g_value_set_boolean (value, priv->show_arrow);
759 g_value_set_int (value, gtk_toolbar_get_icon_size (toolbar));
761 case PROP_ICON_SIZE_SET:
762 g_value_set_boolean (value, priv->icon_size_set);
765 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
771 gtk_toolbar_map (GtkWidget *widget)
773 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
774 GtkToolbarPrivate *priv = toolbar->priv;
776 GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->map (widget);
778 if (priv->event_window)
779 gdk_window_show_unraised (priv->event_window);
783 gtk_toolbar_unmap (GtkWidget *widget)
785 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
786 GtkToolbarPrivate *priv = toolbar->priv;
788 if (priv->event_window)
789 gdk_window_hide (priv->event_window);
791 GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->unmap (widget);
795 gtk_toolbar_realize (GtkWidget *widget)
797 GtkAllocation allocation;
798 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
799 GtkToolbarPrivate *priv = toolbar->priv;
801 GdkWindowAttr attributes;
802 gint attributes_mask;
805 gtk_widget_set_realized (widget, TRUE);
807 gtk_widget_get_allocation (widget, &allocation);
808 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
810 attributes.wclass = GDK_INPUT_ONLY;
811 attributes.window_type = GDK_WINDOW_CHILD;
812 attributes.x = allocation.x + border_width;
813 attributes.y = allocation.y + border_width;
814 attributes.width = allocation.width - border_width * 2;
815 attributes.height = allocation.height - border_width * 2;
816 attributes.event_mask = gtk_widget_get_events (widget);
817 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
818 GDK_BUTTON_RELEASE_MASK |
819 GDK_ENTER_NOTIFY_MASK |
820 GDK_LEAVE_NOTIFY_MASK);
822 attributes_mask = GDK_WA_X | GDK_WA_Y;
824 window = gtk_widget_get_parent_window (widget);
825 gtk_widget_set_window (widget, window);
826 g_object_ref (window);
828 priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
829 &attributes, attributes_mask);
830 gdk_window_set_user_data (priv->event_window, toolbar);
834 gtk_toolbar_unrealize (GtkWidget *widget)
836 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
837 GtkToolbarPrivate *priv = toolbar->priv;
839 if (priv->event_window)
841 gdk_window_set_user_data (priv->event_window, NULL);
842 gdk_window_destroy (priv->event_window);
843 priv->event_window = NULL;
846 GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->unrealize (widget);
850 gtk_toolbar_draw (GtkWidget *widget,
853 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
854 GtkToolbarPrivate *priv = toolbar->priv;
855 GtkStyleContext *context;
860 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
861 context = gtk_widget_get_style_context (widget);
862 state = gtk_widget_get_state_flags (widget);
864 gtk_style_context_save (context);
865 gtk_style_context_set_state (context, state);
867 gtk_render_background (context, cr, border_width, border_width,
868 gtk_widget_get_allocated_width (widget) - 2 * border_width,
869 gtk_widget_get_allocated_height (widget) - 2 * border_width);
870 gtk_render_frame (context, cr, border_width, border_width,
871 gtk_widget_get_allocated_width (widget) - 2 * border_width,
872 gtk_widget_get_allocated_height (widget) - 2 * border_width);
874 for (list = priv->content; list != NULL; list = list->next)
876 ToolbarContent *content = list->data;
878 toolbar_content_draw (content, GTK_CONTAINER (widget), cr);
881 gtk_container_propagate_draw (GTK_CONTAINER (widget),
885 gtk_style_context_restore (context);
891 gtk_toolbar_size_request (GtkWidget *widget,
892 GtkRequisition *requisition)
894 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
895 GtkToolbarPrivate *priv = toolbar->priv;
897 gint max_child_height;
898 gint max_child_width;
899 gint max_homogeneous_child_width;
900 gint max_homogeneous_child_height;
901 gint homogeneous_size;
903 gint pack_front_size;
906 GtkRequisition arrow_requisition;
908 max_homogeneous_child_width = 0;
909 max_homogeneous_child_height = 0;
911 max_child_height = 0;
912 for (list = priv->content; list != NULL; list = list->next)
914 GtkRequisition requisition;
915 ToolbarContent *content = list->data;
917 if (!toolbar_content_visible (content, toolbar))
920 toolbar_content_size_request (content, toolbar, &requisition);
922 max_child_width = MAX (max_child_width, requisition.width);
923 max_child_height = MAX (max_child_height, requisition.height);
925 if (toolbar_content_is_homogeneous (content, toolbar))
927 max_homogeneous_child_width = MAX (max_homogeneous_child_width, requisition.width);
928 max_homogeneous_child_height = MAX (max_homogeneous_child_height, requisition.height);
932 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
933 homogeneous_size = max_homogeneous_child_width;
935 homogeneous_size = max_homogeneous_child_height;
938 for (list = priv->content; list != NULL; list = list->next)
940 ToolbarContent *content = list->data;
943 if (!toolbar_content_visible (content, toolbar))
946 if (toolbar_content_is_homogeneous (content, toolbar))
948 size = homogeneous_size;
952 GtkRequisition requisition;
954 toolbar_content_size_request (content, toolbar, &requisition);
956 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
957 size = requisition.width;
959 size = requisition.height;
962 pack_front_size += size;
965 if (priv->show_arrow)
967 gtk_widget_get_preferred_size (priv->arrow_button,
968 &arrow_requisition, NULL);
970 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
971 long_req = arrow_requisition.width;
973 long_req = arrow_requisition.height;
975 /* There is no point requesting space for the arrow if that would take
976 * up more space than all the items combined
978 long_req = MIN (long_req, pack_front_size);
982 arrow_requisition.height = 0;
983 arrow_requisition.width = 0;
985 long_req = pack_front_size;
988 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
990 requisition->width = long_req;
991 requisition->height = MAX (max_child_height, arrow_requisition.height);
995 requisition->height = long_req;
996 requisition->width = MAX (max_child_width, arrow_requisition.width);
1000 ipadding = get_internal_padding (toolbar);
1002 border_width = gtk_container_get_border_width (GTK_CONTAINER (toolbar));
1003 requisition->width += 2 * (ipadding + border_width);
1004 requisition->height += 2 * (ipadding + border_width);
1006 if (get_shadow_type (toolbar) != GTK_SHADOW_NONE)
1008 GtkStyleContext *context;
1009 GtkStateFlags state;
1012 context = gtk_widget_get_style_context (widget);
1013 state = gtk_widget_get_state_flags (widget);
1014 gtk_style_context_get_padding (context, state, &padding);
1016 requisition->width += padding.left + padding.right;
1017 requisition->height += padding.top + padding.bottom;
1020 priv->button_maxw = max_homogeneous_child_width;
1021 priv->button_maxh = max_homogeneous_child_height;
1025 gtk_toolbar_get_preferred_width (GtkWidget *widget,
1029 GtkRequisition requisition;
1031 gtk_toolbar_size_request (widget, &requisition);
1033 *minimum = *natural = requisition.width;
1037 gtk_toolbar_get_preferred_height (GtkWidget *widget,
1041 GtkRequisition requisition;
1043 gtk_toolbar_size_request (widget, &requisition);
1045 *minimum = *natural = requisition.height;
1049 position (GtkToolbar *toolbar,
1054 GtkToolbarPrivate *priv = toolbar->priv;
1057 if (!priv->animation)
1060 if (elapsed <= ACCEL_THRESHOLD)
1062 n_pixels = SLIDE_SPEED * elapsed;
1066 /* The formula is a second degree polynomial in
1067 * @elapsed that has the line SLIDE_SPEED * @elapsed
1068 * as tangent for @elapsed == ACCEL_THRESHOLD.
1069 * This makes @n_pixels a smooth function of elapsed time.
1071 n_pixels = (SLIDE_SPEED / ACCEL_THRESHOLD) * elapsed * elapsed -
1072 SLIDE_SPEED * elapsed + SLIDE_SPEED * ACCEL_THRESHOLD;
1076 return MIN (from + n_pixels, to);
1078 return MAX (from - n_pixels, to);
1082 compute_intermediate_allocation (GtkToolbar *toolbar,
1083 const GtkAllocation *start,
1084 const GtkAllocation *goal,
1085 GtkAllocation *intermediate)
1087 GtkToolbarPrivate *priv = toolbar->priv;
1088 gdouble elapsed = g_timer_elapsed (priv->timer, NULL);
1090 intermediate->x = position (toolbar, start->x, goal->x, elapsed);
1091 intermediate->y = position (toolbar, start->y, goal->y, elapsed);
1092 intermediate->width = position (toolbar, start->x + start->width,
1093 goal->x + goal->width,
1094 elapsed) - intermediate->x;
1095 intermediate->height = position (toolbar, start->y + start->height,
1096 goal->y + goal->height,
1097 elapsed) - intermediate->y;
1101 fixup_allocation_for_rtl (gint total_size,
1102 GtkAllocation *allocation)
1104 allocation->x += (total_size - (2 * allocation->x + allocation->width));
1108 fixup_allocation_for_vertical (GtkAllocation *allocation)
1112 tmp = allocation->x;
1113 allocation->x = allocation->y;
1114 allocation->y = tmp;
1116 tmp = allocation->width;
1117 allocation->width = allocation->height;
1118 allocation->height = tmp;
1122 get_item_size (GtkToolbar *toolbar,
1123 ToolbarContent *content)
1125 GtkToolbarPrivate *priv = toolbar->priv;
1126 GtkRequisition requisition;
1128 toolbar_content_size_request (content, toolbar, &requisition);
1130 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1132 if (toolbar_content_is_homogeneous (content, toolbar))
1133 return priv->button_maxw;
1135 return requisition.width;
1139 if (toolbar_content_is_homogeneous (content, toolbar))
1140 return priv->button_maxh;
1142 return requisition.height;
1147 slide_idle_handler (gpointer data)
1149 GtkToolbar *toolbar = GTK_TOOLBAR (data);
1150 GtkToolbarPrivate *priv = toolbar->priv;
1153 if (priv->need_sync)
1156 priv->need_sync = FALSE;
1159 for (list = priv->content; list != NULL; list = list->next)
1161 ToolbarContent *content = list->data;
1163 GtkAllocation goal_allocation;
1164 GtkAllocation allocation;
1167 state = toolbar_content_get_state (content);
1168 toolbar_content_get_goal_allocation (content, &goal_allocation);
1169 toolbar_content_get_allocation (content, &allocation);
1173 if (state == NOT_ALLOCATED)
1175 /* an unallocated item means that size allocate has to
1176 * called at least once more
1181 /* An invisible item with a goal allocation of
1182 * 0 is already at its goal.
1184 if ((state == NORMAL || state == OVERFLOWN) &&
1185 ((goal_allocation.width != 0 &&
1186 goal_allocation.height != 0) ||
1187 toolbar_content_child_visible (content)))
1189 if ((goal_allocation.x != allocation.x ||
1190 goal_allocation.y != allocation.y ||
1191 goal_allocation.width != allocation.width ||
1192 goal_allocation.height != allocation.height))
1194 /* An item is not in its right position yet. Note
1195 * that OVERFLOWN items do get an allocation in
1196 * gtk_toolbar_size_allocate(). This way you can see
1197 * them slide back in when you drag an item off the
1204 if (toolbar_content_is_placeholder (content) &&
1205 toolbar_content_disappearing (content) &&
1206 toolbar_content_child_visible (content))
1208 /* A disappearing placeholder is still visible.
1216 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1222 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1224 priv->is_sliding = FALSE;
1231 rect_within (GtkAllocation *a1,
1234 return (a1->x >= a2->x &&
1235 a1->x + a1->width <= a2->x + a2->width &&
1237 a1->y + a1->height <= a2->y + a2->height);
1241 gtk_toolbar_begin_sliding (GtkToolbar *toolbar)
1243 GtkAllocation allocation;
1244 GtkWidget *widget = GTK_WIDGET (toolbar);
1245 GtkToolbarPrivate *priv = toolbar->priv;
1246 GtkStyleContext *context;
1247 GtkStateFlags state;
1256 /* Start the sliding. This function copies the allocation of every
1257 * item into content->start_allocation. For items that haven't
1258 * been allocated yet, we calculate their position and save that
1259 * in start_allocatino along with zero width and zero height.
1261 * FIXME: It would be nice if we could share this code with
1262 * the equivalent in gtk_widget_size_allocate().
1264 priv->is_sliding = TRUE;
1267 priv->idle_id = gdk_threads_add_idle (slide_idle_handler, toolbar);
1269 gtk_widget_get_allocation (widget, &allocation);
1270 context = gtk_widget_get_style_context (widget);
1271 state = gtk_widget_get_state_flags (widget);
1272 gtk_style_context_get_padding (context, state, &padding);
1274 rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1275 vertical = (priv->orientation == GTK_ORIENTATION_VERTICAL);
1276 border_width = get_internal_padding (toolbar) + gtk_container_get_border_width (GTK_CONTAINER (toolbar));
1280 cur_x = allocation.width - border_width - padding.right;
1281 cur_y = allocation.height - border_width - padding.top;
1285 cur_x = border_width + padding.left;
1286 cur_y = border_width + padding.top;
1289 cur_x += allocation.x;
1290 cur_y += allocation.y;
1292 for (list = priv->content; list != NULL; list = list->next)
1294 ToolbarContent *content = list->data;
1295 GtkAllocation new_start_allocation;
1296 GtkAllocation item_allocation;
1299 state = toolbar_content_get_state (content);
1300 toolbar_content_get_allocation (content, &item_allocation);
1302 if ((state == NORMAL &&
1303 rect_within (&item_allocation, &allocation)) ||
1306 new_start_allocation = item_allocation;
1310 new_start_allocation.x = cur_x;
1311 new_start_allocation.y = cur_y;
1315 new_start_allocation.width = allocation.width -
1317 padding.left - padding.right;
1318 new_start_allocation.height = 0;
1322 new_start_allocation.width = 0;
1323 new_start_allocation.height = allocation.height -
1325 padding.top - padding.bottom;
1330 cur_y = new_start_allocation.y + new_start_allocation.height;
1332 cur_x = new_start_allocation.x;
1334 cur_x = new_start_allocation.x + new_start_allocation.width;
1336 toolbar_content_set_start_allocation (content, &new_start_allocation);
1339 /* This resize will run before the first idle handler. This
1340 * will make sure that items get the right goal allocation
1341 * so that the idle handler will not immediately return
1344 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1345 g_timer_reset (priv->timer);
1349 gtk_toolbar_stop_sliding (GtkToolbar *toolbar)
1351 GtkToolbarPrivate *priv = toolbar->priv;
1353 if (priv->is_sliding)
1357 priv->is_sliding = FALSE;
1361 g_source_remove (priv->idle_id);
1365 list = priv->content;
1368 ToolbarContent *content = list->data;
1371 if (toolbar_content_is_placeholder (content))
1373 toolbar_content_remove (content, toolbar);
1374 toolbar_content_free (content);
1378 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1383 remove_item (GtkWidget *menu_item,
1386 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (menu_item)),
1391 menu_deactivated (GtkWidget *menu,
1392 GtkToolbar *toolbar)
1394 GtkToolbarPrivate *priv = toolbar->priv;
1396 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
1400 menu_detached (GtkWidget *widget,
1403 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1404 GtkToolbarPrivate *priv = toolbar->priv;
1410 rebuild_menu (GtkToolbar *toolbar)
1412 GtkToolbarPrivate *priv = toolbar->priv;
1413 GList *list, *children;
1417 priv->menu = GTK_MENU (gtk_menu_new());
1418 gtk_menu_attach_to_widget (priv->menu,
1419 GTK_WIDGET (toolbar),
1422 g_signal_connect (priv->menu, "deactivate",
1423 G_CALLBACK (menu_deactivated), toolbar);
1426 gtk_container_foreach (GTK_CONTAINER (priv->menu), remove_item, NULL);
1428 for (list = priv->content; list != NULL; list = list->next)
1430 ToolbarContent *content = list->data;
1432 if (toolbar_content_get_state (content) == OVERFLOWN &&
1433 !toolbar_content_is_placeholder (content))
1435 GtkWidget *menu_item = toolbar_content_retrieve_menu_item (content);
1439 g_assert (GTK_IS_MENU_ITEM (menu_item));
1440 gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), menu_item);
1445 /* Remove leading and trailing separator items */
1446 children = gtk_container_get_children (GTK_CONTAINER (priv->menu));
1449 while (list && GTK_IS_SEPARATOR_MENU_ITEM (list->data))
1451 GtkWidget *child = list->data;
1453 gtk_container_remove (GTK_CONTAINER (priv->menu), child);
1456 g_list_free (children);
1458 /* Regenerate the list of children so we don't try to remove items twice */
1459 children = gtk_container_get_children (GTK_CONTAINER (priv->menu));
1461 list = g_list_last (children);
1462 while (list && GTK_IS_SEPARATOR_MENU_ITEM (list->data))
1464 GtkWidget *child = list->data;
1466 gtk_container_remove (GTK_CONTAINER (priv->menu), child);
1469 g_list_free (children);
1471 priv->need_rebuild = FALSE;
1475 gtk_toolbar_size_allocate (GtkWidget *widget,
1476 GtkAllocation *allocation)
1478 GtkAllocation widget_allocation;
1479 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1480 GtkToolbarPrivate *priv = toolbar->priv;
1481 GtkAllocation *allocations;
1482 ItemState *new_states;
1483 GtkAllocation arrow_allocation;
1484 GtkStyleContext *context;
1485 GtkStateFlags state;
1488 gint size, pos, short_size;
1491 gboolean need_arrow;
1492 gint n_expand_items;
1494 gint available_size;
1497 GtkRequisition arrow_requisition;
1498 gboolean overflowing;
1499 gboolean size_changed;
1500 GtkAllocation item_area;
1501 GtkShadowType shadow_type;
1503 context = gtk_widget_get_style_context (widget);
1504 state = gtk_widget_get_state_flags (widget);
1505 gtk_style_context_get_padding (context, state, &padding);
1507 gtk_widget_get_allocation (widget, &widget_allocation);
1508 size_changed = FALSE;
1509 if (widget_allocation.x != allocation->x ||
1510 widget_allocation.y != allocation->y ||
1511 widget_allocation.width != allocation->width ||
1512 widget_allocation.height != allocation->height)
1514 size_changed = TRUE;
1518 gtk_toolbar_stop_sliding (toolbar);
1520 gtk_widget_set_allocation (widget, allocation);
1522 border_width = gtk_container_get_border_width (GTK_CONTAINER (toolbar));
1524 if (gtk_widget_get_realized (widget))
1525 gdk_window_move_resize (priv->event_window,
1526 allocation->x + border_width,
1527 allocation->y + border_width,
1528 allocation->width - border_width * 2,
1529 allocation->height - border_width * 2);
1531 border_width += get_internal_padding (toolbar);
1533 gtk_widget_get_preferred_size (priv->arrow_button,
1534 &arrow_requisition, NULL);
1536 shadow_type = get_shadow_type (toolbar);
1538 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1540 available_size = size = allocation->width - 2 * border_width;
1541 short_size = allocation->height - 2 * border_width;
1542 arrow_size = arrow_requisition.width;
1544 if (shadow_type != GTK_SHADOW_NONE)
1546 available_size -= padding.left + padding.right;
1547 short_size -= padding.top + padding.bottom;
1552 available_size = size = allocation->height - 2 * border_width;
1553 short_size = allocation->width - 2 * border_width;
1554 arrow_size = arrow_requisition.height;
1556 if (shadow_type != GTK_SHADOW_NONE)
1558 available_size -= padding.top + padding.bottom;
1559 short_size -= padding.left + padding.right;
1563 n_items = g_list_length (priv->content);
1564 allocations = g_new0 (GtkAllocation, n_items);
1565 new_states = g_new0 (ItemState, n_items);
1569 for (list = priv->content; list != NULL; list = list->next)
1571 ToolbarContent *content = list->data;
1573 if (toolbar_content_visible (content, toolbar))
1575 needed_size += get_item_size (toolbar, content);
1577 /* Do we need an arrow?
1579 * Assume we don't, and see if any non-separator item
1580 * with a proxy menu item is then going to overflow.
1582 if (needed_size > available_size &&
1585 toolbar_content_has_proxy_menu_item (content) &&
1586 !toolbar_content_is_separator (content))
1594 size = available_size - arrow_size;
1596 size = available_size;
1598 /* calculate widths and states of items */
1599 overflowing = FALSE;
1600 for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1602 ToolbarContent *content = list->data;
1605 if (!toolbar_content_visible (content, toolbar))
1607 new_states[i] = HIDDEN;
1611 item_size = get_item_size (toolbar, content);
1612 if (item_size <= size && !overflowing)
1615 allocations[i].width = item_size;
1616 new_states[i] = NORMAL;
1621 new_states[i] = OVERFLOWN;
1622 allocations[i].width = item_size;
1626 /* calculate width of arrow */
1629 arrow_allocation.width = arrow_size;
1630 arrow_allocation.height = MAX (short_size, 1);
1633 /* expand expandable items */
1635 /* We don't expand when there is an overflow menu,
1636 * because that leads to weird jumps when items get
1637 * moved to the overflow menu and the expanding
1638 * items suddenly get a lot of extra space
1642 gint max_child_expand;
1645 for (i = 0, list = priv->content; list != NULL; list = list->next, ++i)
1647 ToolbarContent *content = list->data;
1649 if (toolbar_content_get_expand (content) && new_states[i] == NORMAL)
1653 max_child_expand = get_max_child_expand (toolbar);
1654 for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1656 ToolbarContent *content = list->data;
1658 if (toolbar_content_get_expand (content) && new_states[i] == NORMAL)
1660 gint extra = size / n_expand_items;
1661 if (size % n_expand_items != 0)
1664 if (extra > max_child_expand)
1665 extra = max_child_expand;
1667 allocations[i].width += extra;
1673 g_assert (n_expand_items == 0);
1676 /* position items */
1678 for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1680 /* Both NORMAL and OVERFLOWN items get a position.
1681 * This ensures that sliding will work for OVERFLOWN items too.
1683 if (new_states[i] == NORMAL || new_states[i] == OVERFLOWN)
1685 allocations[i].x = pos;
1686 allocations[i].y = border_width;
1687 allocations[i].height = short_size;
1689 pos += allocations[i].width;
1693 /* position arrow */
1696 arrow_allocation.x = available_size - border_width - arrow_allocation.width;
1697 arrow_allocation.y = border_width;
1700 item_area.x = border_width;
1701 item_area.y = border_width;
1702 item_area.width = available_size - (need_arrow? arrow_size : 0);
1703 item_area.height = short_size;
1705 /* fix up allocations in the vertical or RTL cases */
1706 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1708 for (i = 0; i < n_items; ++i)
1709 fixup_allocation_for_vertical (&(allocations[i]));
1712 fixup_allocation_for_vertical (&arrow_allocation);
1714 fixup_allocation_for_vertical (&item_area);
1716 else if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
1718 for (i = 0; i < n_items; ++i)
1719 fixup_allocation_for_rtl (available_size, &(allocations[i]));
1722 fixup_allocation_for_rtl (available_size, &arrow_allocation);
1724 fixup_allocation_for_rtl (available_size, &item_area);
1727 /* translate the items by allocation->(x,y) */
1728 for (i = 0; i < n_items; ++i)
1730 allocations[i].x += allocation->x;
1731 allocations[i].y += allocation->y;
1733 if (shadow_type != GTK_SHADOW_NONE)
1735 allocations[i].x += padding.left;
1736 allocations[i].y += padding.top;
1742 arrow_allocation.x += allocation->x;
1743 arrow_allocation.y += allocation->y;
1745 if (shadow_type != GTK_SHADOW_NONE)
1747 arrow_allocation.x += padding.left;
1748 arrow_allocation.y += padding.top;
1752 item_area.x += allocation->x;
1753 item_area.y += allocation->y;
1754 if (shadow_type != GTK_SHADOW_NONE)
1756 item_area.x += padding.left;
1757 item_area.y += padding.top;
1760 /* did anything change? */
1761 for (list = priv->content, i = 0; list != NULL; list = list->next, i++)
1763 ToolbarContent *content = list->data;
1765 if (toolbar_content_get_state (content) == NORMAL &&
1766 new_states[i] != NORMAL)
1768 /* an item disappeared and we didn't change size, so begin sliding */
1770 gtk_toolbar_begin_sliding (toolbar);
1774 /* finally allocate the items */
1775 if (priv->is_sliding)
1777 for (list = priv->content, i = 0; list != NULL; list = list->next, i++)
1779 ToolbarContent *content = list->data;
1781 toolbar_content_set_goal_allocation (content, &(allocations[i]));
1785 for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1787 ToolbarContent *content = list->data;
1789 if (new_states[i] == OVERFLOWN || new_states[i] == NORMAL)
1791 GtkAllocation alloc;
1792 GtkAllocation start_allocation = { 0, };
1793 GtkAllocation goal_allocation;
1795 if (priv->is_sliding)
1797 toolbar_content_get_start_allocation (content, &start_allocation);
1798 toolbar_content_get_goal_allocation (content, &goal_allocation);
1800 compute_intermediate_allocation (toolbar,
1805 priv->need_sync = TRUE;
1809 alloc = allocations[i];
1812 if (alloc.width <= 0 || alloc.height <= 0)
1814 toolbar_content_set_child_visible (content, toolbar, FALSE);
1818 if (!rect_within (&alloc, &item_area))
1820 toolbar_content_set_child_visible (content, toolbar, FALSE);
1821 toolbar_content_size_allocate (content, &alloc);
1825 toolbar_content_set_child_visible (content, toolbar, TRUE);
1826 toolbar_content_size_allocate (content, &alloc);
1832 toolbar_content_set_child_visible (content, toolbar, FALSE);
1835 toolbar_content_set_state (content, new_states[i]);
1838 if (priv->menu && priv->need_rebuild)
1839 rebuild_menu (toolbar);
1843 gtk_widget_size_allocate (GTK_WIDGET (priv->arrow_button),
1845 gtk_widget_show (GTK_WIDGET (priv->arrow_button));
1849 gtk_widget_hide (GTK_WIDGET (priv->arrow_button));
1851 if (priv->menu && gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
1852 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
1855 g_free (allocations);
1856 g_free (new_states);
1860 gtk_toolbar_update_button_relief (GtkToolbar *toolbar)
1862 GtkToolbarPrivate *priv = toolbar->priv;
1863 GtkReliefStyle relief;
1865 relief = get_button_relief (toolbar);
1867 if (relief != gtk_button_get_relief (GTK_BUTTON (priv->arrow_button)))
1869 gtk_toolbar_reconfigured (toolbar);
1871 gtk_button_set_relief (GTK_BUTTON (priv->arrow_button), relief);
1876 gtk_toolbar_style_updated (GtkWidget *widget)
1878 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1879 GtkToolbarPrivate *priv = toolbar->priv;
1881 GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->style_updated (widget);
1883 priv->max_homogeneous_pixels = -1;
1885 if (gtk_widget_get_realized (widget))
1886 gtk_style_context_set_background (gtk_widget_get_style_context (widget),
1887 gtk_widget_get_window (widget));
1889 gtk_toolbar_update_button_relief (GTK_TOOLBAR (widget));
1893 gtk_toolbar_list_children_in_focus_order (GtkToolbar *toolbar,
1894 GtkDirectionType dir)
1896 GtkToolbarPrivate *priv = toolbar->priv;
1897 GList *result = NULL;
1901 /* generate list of children in reverse logical order */
1903 for (list = priv->content; list != NULL; list = list->next)
1905 ToolbarContent *content = list->data;
1908 widget = toolbar_content_get_widget (content);
1911 result = g_list_prepend (result, widget);
1914 result = g_list_prepend (result, priv->arrow_button);
1916 rtl = (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL);
1918 /* move in logical order when
1920 * - dir is TAB_FORWARD
1922 * - in RTL mode and moving left or up
1924 * - in LTR mode and moving right or down
1926 if (dir == GTK_DIR_TAB_FORWARD ||
1927 (rtl && (dir == GTK_DIR_UP || dir == GTK_DIR_LEFT)) ||
1928 (!rtl && (dir == GTK_DIR_DOWN || dir == GTK_DIR_RIGHT)))
1930 result = g_list_reverse (result);
1937 gtk_toolbar_focus_home_or_end (GtkToolbar *toolbar,
1938 gboolean focus_home)
1940 GList *children, *list;
1941 GtkDirectionType dir = focus_home? GTK_DIR_RIGHT : GTK_DIR_LEFT;
1943 children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);
1945 if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
1947 children = g_list_reverse (children);
1949 dir = (dir == GTK_DIR_RIGHT)? GTK_DIR_LEFT : GTK_DIR_RIGHT;
1952 for (list = children; list != NULL; list = list->next)
1954 GtkWidget *child = list->data;
1956 if (gtk_container_get_focus_child (GTK_CONTAINER (toolbar)) == child)
1959 if (gtk_widget_get_mapped (child) && gtk_widget_child_focus (child, dir))
1963 g_list_free (children);
1968 /* Keybinding handler. This function is called when the user presses
1969 * Ctrl TAB or an arrow key.
1972 gtk_toolbar_move_focus (GtkWidget *widget,
1973 GtkDirectionType dir)
1975 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1976 GtkContainer *container = GTK_CONTAINER (toolbar);
1977 GtkWidget *focus_child;
1979 gboolean try_focus = FALSE;
1982 focus_child = gtk_container_get_focus_child (container);
1984 if (focus_child && gtk_widget_child_focus (focus_child, dir))
1987 children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);
1989 for (list = children; list != NULL; list = list->next)
1991 GtkWidget *child = list->data;
1993 if (try_focus && gtk_widget_get_mapped (child) && gtk_widget_child_focus (child, dir))
1996 if (child == focus_child)
2000 g_list_free (children);
2003 /* The focus handler for the toolbar. It called when the user presses
2004 * TAB or otherwise tries to focus the toolbar.
2007 gtk_toolbar_focus (GtkWidget *widget,
2008 GtkDirectionType dir)
2010 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
2011 GList *children, *list;
2012 gboolean result = FALSE;
2014 /* if focus is already somewhere inside the toolbar then return FALSE.
2015 * The only way focus can stay inside the toolbar is when the user presses
2016 * arrow keys or Ctrl TAB (both of which are handled by the
2017 * gtk_toolbar_move_focus() keybinding function.
2019 if (gtk_container_get_focus_child (GTK_CONTAINER (widget)))
2022 children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);
2024 for (list = children; list != NULL; list = list->next)
2026 GtkWidget *child = list->data;
2028 if (gtk_widget_get_mapped (child) && gtk_widget_child_focus (child, dir))
2035 g_list_free (children);
2040 static GtkSettings *
2041 toolbar_get_settings (GtkToolbar *toolbar)
2043 return toolbar->priv->settings;
2047 style_change_notify (GtkToolbar *toolbar)
2049 GtkToolbarPrivate *priv = toolbar->priv;
2051 if (!priv->style_set)
2053 /* pretend it was set, then unset, thus reverting to new default */
2054 priv->style_set = TRUE;
2055 gtk_toolbar_unset_style (toolbar);
2060 icon_size_change_notify (GtkToolbar *toolbar)
2062 GtkToolbarPrivate *priv = toolbar->priv;
2064 if (!priv->icon_size_set)
2066 /* pretend it was set, then unset, thus reverting to new default */
2067 priv->icon_size_set = TRUE;
2068 gtk_toolbar_unset_icon_size (toolbar);
2073 animation_change_notify (GtkToolbar *toolbar)
2075 GtkToolbarPrivate *priv = toolbar->priv;
2076 GtkSettings *settings = toolbar_get_settings (toolbar);
2080 g_object_get (settings,
2081 "gtk-enable-animations", &animation,
2084 animation = DEFAULT_ANIMATION_STATE;
2086 priv->animation = animation;
2090 settings_change_notify (GtkSettings *settings,
2091 const GParamSpec *pspec,
2092 GtkToolbar *toolbar)
2094 if (! strcmp (pspec->name, "gtk-toolbar-style"))
2095 style_change_notify (toolbar);
2096 else if (! strcmp (pspec->name, "gtk-toolbar-icon-size"))
2097 icon_size_change_notify (toolbar);
2098 else if (! strcmp (pspec->name, "gtk-enable-animations"))
2099 animation_change_notify (toolbar);
2103 gtk_toolbar_screen_changed (GtkWidget *widget,
2104 GdkScreen *previous_screen)
2106 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
2107 GtkToolbarPrivate *priv = toolbar->priv;
2108 GtkSettings *old_settings = toolbar_get_settings (toolbar);
2109 GtkSettings *settings;
2111 if (gtk_widget_has_screen (GTK_WIDGET (toolbar)))
2112 settings = gtk_widget_get_settings (GTK_WIDGET (toolbar));
2116 if (settings == old_settings)
2121 g_signal_handler_disconnect (old_settings, priv->settings_connection);
2123 g_object_unref (old_settings);
2128 priv->settings_connection =
2129 g_signal_connect (settings, "notify",
2130 G_CALLBACK (settings_change_notify),
2133 priv->settings = g_object_ref (settings);
2136 priv->settings = NULL;
2138 style_change_notify (toolbar);
2139 icon_size_change_notify (toolbar);
2140 animation_change_notify (toolbar);
2144 find_drop_index (GtkToolbar *toolbar,
2148 GtkToolbarPrivate *priv = toolbar->priv;
2149 GList *interesting_content;
2151 GtkOrientation orientation;
2152 GtkTextDirection direction;
2153 gint best_distance = G_MAXINT;
2157 ToolbarContent *best_content;
2158 GtkAllocation allocation;
2160 /* list items we care about wrt. drag and drop */
2161 interesting_content = NULL;
2162 for (list = priv->content; list != NULL; list = list->next)
2164 ToolbarContent *content = list->data;
2166 if (toolbar_content_get_state (content) == NORMAL)
2167 interesting_content = g_list_prepend (interesting_content, content);
2169 interesting_content = g_list_reverse (interesting_content);
2171 if (!interesting_content)
2174 orientation = priv->orientation;
2175 direction = gtk_widget_get_direction (GTK_WIDGET (toolbar));
2177 /* distance to first interesting item */
2178 best_content = interesting_content->data;
2179 toolbar_content_get_allocation (best_content, &allocation);
2181 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2185 if (direction == GTK_TEXT_DIR_LTR)
2188 pos = allocation.x + allocation.width;
2196 best_content = NULL;
2197 best_distance = ABS (pos - cursor);
2199 /* distance to far end of each item */
2200 for (list = interesting_content; list != NULL; list = list->next)
2202 ToolbarContent *content = list->data;
2204 toolbar_content_get_allocation (content, &allocation);
2206 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2208 if (direction == GTK_TEXT_DIR_LTR)
2209 pos = allocation.x + allocation.width;
2215 pos = allocation.y + allocation.height;
2218 distance = ABS (pos - cursor);
2220 if (distance < best_distance)
2222 best_distance = distance;
2223 best_content = content;
2227 g_list_free (interesting_content);
2232 return g_list_index (priv->content, best_content) + 1;
2236 reset_all_placeholders (GtkToolbar *toolbar)
2238 GtkToolbarPrivate *priv = toolbar->priv;
2241 for (list = priv->content; list != NULL; list = list->next)
2243 ToolbarContent *content = list->data;
2244 if (toolbar_content_is_placeholder (content))
2245 toolbar_content_set_disappearing (content, TRUE);
2250 physical_to_logical (GtkToolbar *toolbar,
2253 GtkToolbarPrivate *priv = toolbar->priv;
2257 g_assert (physical >= 0);
2260 for (list = priv->content; list && physical > 0; list = list->next)
2262 ToolbarContent *content = list->data;
2264 if (!toolbar_content_is_placeholder (content))
2269 g_assert (physical == 0);
2275 logical_to_physical (GtkToolbar *toolbar,
2278 GtkToolbarPrivate *priv = toolbar->priv;
2282 g_assert (logical >= 0);
2285 for (list = priv->content; list; list = list->next)
2287 ToolbarContent *content = list->data;
2289 if (!toolbar_content_is_placeholder (content))
2299 g_assert (logical == 0);
2305 * gtk_toolbar_set_drop_highlight_item:
2306 * @toolbar: a #GtkToolbar
2307 * @tool_item: (allow-none): a #GtkToolItem, or %NULL to turn of highlighting
2308 * @index_: a position on @toolbar
2310 * Highlights @toolbar to give an idea of what it would look like
2311 * if @item was added to @toolbar at the position indicated by @index_.
2312 * If @item is %NULL, highlighting is turned off. In that case @index_
2315 * The @tool_item passed to this function must not be part of any widget
2316 * hierarchy. When an item is set as drop highlight item it can not
2317 * added to any widget hierarchy or used as highlight item for another
2323 gtk_toolbar_set_drop_highlight_item (GtkToolbar *toolbar,
2324 GtkToolItem *tool_item,
2327 ToolbarContent *content;
2328 GtkToolbarPrivate *priv;
2330 GtkRequisition requisition;
2331 GtkRequisition old_requisition;
2332 gboolean restart_sliding;
2334 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2335 g_return_if_fail (tool_item == NULL || GTK_IS_TOOL_ITEM (tool_item));
2337 priv = toolbar->priv;
2341 if (priv->highlight_tool_item)
2343 gtk_widget_unparent (GTK_WIDGET (priv->highlight_tool_item));
2344 g_object_unref (priv->highlight_tool_item);
2345 priv->highlight_tool_item = NULL;
2348 reset_all_placeholders (toolbar);
2349 gtk_toolbar_begin_sliding (toolbar);
2353 n_items = gtk_toolbar_get_n_items (toolbar);
2354 if (index_ < 0 || index_ > n_items)
2357 if (tool_item != priv->highlight_tool_item)
2359 if (priv->highlight_tool_item)
2360 g_object_unref (priv->highlight_tool_item);
2362 g_object_ref_sink (tool_item);
2364 priv->highlight_tool_item = tool_item;
2366 gtk_widget_set_parent (GTK_WIDGET (priv->highlight_tool_item),
2367 GTK_WIDGET (toolbar));
2370 index_ = logical_to_physical (toolbar, index_);
2372 content = g_list_nth_data (priv->content, index_);
2376 ToolbarContent *prev_content;
2378 prev_content = g_list_nth_data (priv->content, index_ - 1);
2380 if (prev_content && toolbar_content_is_placeholder (prev_content))
2381 content = prev_content;
2384 if (!content || !toolbar_content_is_placeholder (content))
2386 GtkWidget *placeholder;
2388 placeholder = GTK_WIDGET (gtk_separator_tool_item_new ());
2390 content = toolbar_content_new_tool_item (toolbar,
2391 GTK_TOOL_ITEM (placeholder),
2393 gtk_widget_show (placeholder);
2397 g_assert (toolbar_content_is_placeholder (content));
2399 gtk_widget_get_preferred_size (GTK_WIDGET (priv->highlight_tool_item),
2400 &requisition, NULL);
2402 toolbar_content_set_expand (content, gtk_tool_item_get_expand (tool_item));
2404 restart_sliding = FALSE;
2405 toolbar_content_size_request (content, toolbar, &old_requisition);
2406 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2408 requisition.height = -1;
2409 if (requisition.width != old_requisition.width)
2410 restart_sliding = TRUE;
2414 requisition.width = -1;
2415 if (requisition.height != old_requisition.height)
2416 restart_sliding = TRUE;
2419 if (toolbar_content_disappearing (content))
2420 restart_sliding = TRUE;
2422 reset_all_placeholders (toolbar);
2423 toolbar_content_set_disappearing (content, FALSE);
2425 toolbar_content_set_size_request (content,
2426 requisition.width, requisition.height);
2428 if (restart_sliding)
2429 gtk_toolbar_begin_sliding (toolbar);
2433 gtk_toolbar_get_child_property (GtkContainer *container,
2439 GtkToolItem *item = GTK_TOOL_ITEM (child);
2441 switch (property_id)
2443 case CHILD_PROP_HOMOGENEOUS:
2444 g_value_set_boolean (value, gtk_tool_item_get_homogeneous (item));
2447 case CHILD_PROP_EXPAND:
2448 g_value_set_boolean (value, gtk_tool_item_get_expand (item));
2452 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
2458 gtk_toolbar_set_child_property (GtkContainer *container,
2461 const GValue *value,
2464 switch (property_id)
2466 case CHILD_PROP_HOMOGENEOUS:
2467 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (child), g_value_get_boolean (value));
2470 case CHILD_PROP_EXPAND:
2471 gtk_tool_item_set_expand (GTK_TOOL_ITEM (child), g_value_get_boolean (value));
2475 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
2481 gtk_toolbar_show_all (GtkWidget *widget)
2483 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
2484 GtkToolbarPrivate *priv = toolbar->priv;
2487 for (list = priv->content; list != NULL; list = list->next)
2489 ToolbarContent *content = list->data;
2491 toolbar_content_show_all (content);
2494 gtk_widget_show (widget);
2498 gtk_toolbar_add (GtkContainer *container,
2501 GtkToolbar *toolbar = GTK_TOOLBAR (container);
2503 gtk_toolbar_insert (toolbar, GTK_TOOL_ITEM (widget), -1);
2507 gtk_toolbar_remove (GtkContainer *container,
2510 GtkToolbar *toolbar = GTK_TOOLBAR (container);
2511 GtkToolbarPrivate *priv = toolbar->priv;
2512 ToolbarContent *content_to_remove;
2515 content_to_remove = NULL;
2516 for (list = priv->content; list != NULL; list = list->next)
2518 ToolbarContent *content = list->data;
2521 child = toolbar_content_get_widget (content);
2522 if (child && child == widget)
2524 content_to_remove = content;
2529 g_return_if_fail (content_to_remove != NULL);
2531 toolbar_content_remove (content_to_remove, toolbar);
2532 toolbar_content_free (content_to_remove);
2536 gtk_toolbar_forall (GtkContainer *container,
2537 gboolean include_internals,
2538 GtkCallback callback,
2539 gpointer callback_data)
2541 GtkToolbar *toolbar = GTK_TOOLBAR (container);
2542 GtkToolbarPrivate *priv = toolbar->priv;
2545 g_return_if_fail (callback != NULL);
2547 list = priv->content;
2550 ToolbarContent *content = list->data;
2551 GList *next = list->next;
2553 if (include_internals || !toolbar_content_is_placeholder (content))
2555 GtkWidget *child = toolbar_content_get_widget (content);
2558 callback (child, callback_data);
2564 if (include_internals)
2565 callback (priv->arrow_button, callback_data);
2569 gtk_toolbar_child_type (GtkContainer *container)
2571 return GTK_TYPE_TOOL_ITEM;
2575 gtk_toolbar_reconfigured (GtkToolbar *toolbar)
2577 GtkToolbarPrivate *priv = toolbar->priv;
2580 list = priv->content;
2583 ToolbarContent *content = list->data;
2584 GList *next = list->next;
2586 toolbar_content_toolbar_reconfigured (content, toolbar);
2593 gtk_toolbar_orientation_changed (GtkToolbar *toolbar,
2594 GtkOrientation orientation)
2596 GtkToolbarPrivate *priv = toolbar->priv;
2598 if (priv->orientation != orientation)
2600 priv->orientation = orientation;
2602 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2603 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2605 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
2607 gtk_toolbar_reconfigured (toolbar);
2609 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
2610 g_object_notify (G_OBJECT (toolbar), "orientation");
2615 gtk_toolbar_real_style_changed (GtkToolbar *toolbar,
2616 GtkToolbarStyle style)
2618 GtkToolbarPrivate *priv = toolbar->priv;
2620 if (priv->style != style)
2622 priv->style = style;
2624 gtk_toolbar_reconfigured (toolbar);
2626 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
2627 g_object_notify (G_OBJECT (toolbar), "toolbar-style");
2632 menu_position_func (GtkMenu *menu,
2638 GtkAllocation allocation;
2639 GtkToolbar *toolbar = GTK_TOOLBAR (user_data);
2640 GtkToolbarPrivate *priv = toolbar->priv;
2642 GtkRequisition menu_req;
2643 GdkRectangle monitor;
2647 gtk_widget_get_preferred_size (priv->arrow_button,
2649 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
2652 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
2653 monitor_num = gdk_screen_get_monitor_at_window (screen,
2654 gtk_widget_get_window (priv->arrow_button));
2655 if (monitor_num < 0)
2657 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2659 gtk_widget_get_allocation (priv->arrow_button, &allocation);
2661 gdk_window_get_origin (gtk_button_get_event_window (GTK_BUTTON (priv->arrow_button)), x, y);
2662 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2664 if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_LTR)
2665 *x += allocation.width - req.width;
2667 *x += req.width - menu_req.width;
2669 if ((*y + allocation.height + menu_req.height) <= monitor.y + monitor.height)
2670 *y += allocation.height;
2671 else if ((*y - menu_req.height) >= monitor.y)
2672 *y -= menu_req.height;
2673 else if (monitor.y + monitor.height - (*y + allocation.height) > *y)
2674 *y += allocation.height;
2676 *y -= menu_req.height;
2680 if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_LTR)
2681 *x += allocation.width;
2683 *x -= menu_req.width;
2685 if (*y + menu_req.height > monitor.y + monitor.height &&
2686 *y + allocation.height - monitor.y > monitor.y + monitor.height - *y)
2687 *y += allocation.height - menu_req.height;
2694 show_menu (GtkToolbar *toolbar,
2695 GdkEventButton *event)
2697 GtkToolbarPrivate *priv = toolbar->priv;
2699 rebuild_menu (toolbar);
2701 gtk_widget_show_all (GTK_WIDGET (priv->menu));
2703 gtk_menu_popup (priv->menu, NULL, NULL,
2704 menu_position_func, toolbar,
2705 event? event->button : 0,
2706 event? event->time : gtk_get_current_event_time());
2710 gtk_toolbar_arrow_button_clicked (GtkWidget *button,
2711 GtkToolbar *toolbar)
2713 GtkToolbarPrivate *priv = toolbar->priv;
2715 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->arrow_button)) &&
2716 (!priv->menu || !gtk_widget_get_visible (GTK_WIDGET (priv->menu))))
2718 /* We only get here when the button is clicked with the keyboard,
2719 * because mouse button presses result in the menu being shown so
2720 * that priv->menu would be non-NULL and visible.
2722 show_menu (toolbar, NULL);
2723 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
2728 gtk_toolbar_arrow_button_press (GtkWidget *button,
2729 GdkEventButton *event,
2730 GtkToolbar *toolbar)
2732 show_menu (toolbar, event);
2733 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
2739 gtk_toolbar_button_press (GtkWidget *toolbar,
2740 GdkEventButton *event)
2744 if (event->button == 3)
2746 gboolean return_value;
2748 g_signal_emit (toolbar, toolbar_signals[POPUP_CONTEXT_MENU], 0,
2749 (int)event->x_root, (int)event->y_root, event->button,
2752 return return_value;
2755 window = gtk_widget_get_toplevel (toolbar);
2759 gboolean window_drag = FALSE;
2761 gtk_widget_style_get (toolbar,
2762 "window-dragging", &window_drag,
2767 gtk_window_begin_move_drag (GTK_WINDOW (window),
2781 gtk_toolbar_popup_menu (GtkWidget *toolbar)
2783 gboolean return_value;
2784 /* This function is the handler for the "popup menu" keybinding,
2785 * ie., it is called when the user presses Shift F10
2787 g_signal_emit (toolbar, toolbar_signals[POPUP_CONTEXT_MENU], 0,
2788 -1, -1, -1, &return_value);
2790 return return_value;
2796 * Creates a new toolbar.
2798 * Return Value: the newly-created toolbar.
2801 gtk_toolbar_new (void)
2803 GtkToolbar *toolbar;
2805 toolbar = g_object_new (GTK_TYPE_TOOLBAR, NULL);
2807 return GTK_WIDGET (toolbar);
2811 * gtk_toolbar_insert:
2812 * @toolbar: a #GtkToolbar
2813 * @item: a #GtkToolItem
2814 * @pos: the position of the new item
2816 * Insert a #GtkToolItem into the toolbar at position @pos. If @pos is
2817 * 0 the item is prepended to the start of the toolbar. If @pos is
2818 * negative, the item is appended to the end of the toolbar.
2823 gtk_toolbar_insert (GtkToolbar *toolbar,
2827 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2828 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2831 pos = logical_to_physical (toolbar, pos);
2833 toolbar_content_new_tool_item (toolbar, item, FALSE, pos);
2837 * gtk_toolbar_get_item_index:
2838 * @toolbar: a #GtkToolbar
2839 * @item: a #GtkToolItem that is a child of @toolbar
2841 * Returns the position of @item on the toolbar, starting from 0.
2842 * It is an error if @item is not a child of the toolbar.
2844 * Return value: the position of item on the toolbar.
2849 gtk_toolbar_get_item_index (GtkToolbar *toolbar,
2852 GtkToolbarPrivate *priv;
2856 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), -1);
2857 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2858 g_return_val_if_fail (gtk_widget_get_parent (GTK_WIDGET (item)) == GTK_WIDGET (toolbar), -1);
2860 priv = toolbar->priv;
2863 for (list = priv->content; list != NULL; list = list->next)
2865 ToolbarContent *content = list->data;
2868 widget = toolbar_content_get_widget (content);
2870 if (item == GTK_TOOL_ITEM (widget))
2876 return physical_to_logical (toolbar, n);
2880 * gtk_toolbar_set_style:
2881 * @toolbar: a #GtkToolbar.
2882 * @style: the new style for @toolbar.
2884 * Alters the view of @toolbar to display either icons only, text only, or both.
2887 gtk_toolbar_set_style (GtkToolbar *toolbar,
2888 GtkToolbarStyle style)
2890 GtkToolbarPrivate *priv;
2892 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2894 priv = toolbar->priv;
2896 priv->style_set = TRUE;
2897 g_signal_emit (toolbar, toolbar_signals[STYLE_CHANGED], 0, style);
2901 * gtk_toolbar_get_style:
2902 * @toolbar: a #GtkToolbar
2904 * Retrieves whether the toolbar has text, icons, or both . See
2905 * gtk_toolbar_set_style().
2907 * Return value: the current style of @toolbar
2910 gtk_toolbar_get_style (GtkToolbar *toolbar)
2912 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), DEFAULT_TOOLBAR_STYLE);
2914 return toolbar->priv->style;
2918 * gtk_toolbar_unset_style:
2919 * @toolbar: a #GtkToolbar
2921 * Unsets a toolbar style set with gtk_toolbar_set_style(), so that
2922 * user preferences will be used to determine the toolbar style.
2925 gtk_toolbar_unset_style (GtkToolbar *toolbar)
2927 GtkToolbarPrivate *priv;
2928 GtkToolbarStyle style;
2930 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2932 priv = toolbar->priv;
2934 if (priv->style_set)
2936 GtkSettings *settings = toolbar_get_settings (toolbar);
2939 g_object_get (settings,
2940 "gtk-toolbar-style", &style,
2943 style = DEFAULT_TOOLBAR_STYLE;
2945 if (style != priv->style)
2946 g_signal_emit (toolbar, toolbar_signals[STYLE_CHANGED], 0, style);
2948 priv->style_set = FALSE;
2953 * gtk_toolbar_get_n_items:
2954 * @toolbar: a #GtkToolbar
2956 * Returns the number of items on the toolbar.
2958 * Return value: the number of items on the toolbar
2963 gtk_toolbar_get_n_items (GtkToolbar *toolbar)
2965 GtkToolbarPrivate *priv;
2967 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), -1);
2969 priv = toolbar->priv;
2971 return physical_to_logical (toolbar, g_list_length (priv->content));
2975 * gtk_toolbar_get_nth_item:
2976 * @toolbar: a #GtkToolbar
2977 * @n: A position on the toolbar
2979 * Returns the @n<!-- -->'th item on @toolbar, or %NULL if the
2980 * toolbar does not contain an @n<!-- -->'th item.
2982 * Return value: (transfer none): The @n<!-- -->'th #GtkToolItem on @toolbar,
2983 * or %NULL if there isn't an @n<!-- -->'th item.
2988 gtk_toolbar_get_nth_item (GtkToolbar *toolbar,
2991 GtkToolbarPrivate *priv;
2992 ToolbarContent *content;
2995 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), NULL);
2997 priv = toolbar->priv;
2999 n_items = gtk_toolbar_get_n_items (toolbar);
3001 if (n < 0 || n >= n_items)
3004 content = g_list_nth_data (priv->content, logical_to_physical (toolbar, n));
3007 g_assert (!toolbar_content_is_placeholder (content));
3009 return GTK_TOOL_ITEM (toolbar_content_get_widget (content));
3013 * gtk_toolbar_get_icon_size:
3014 * @toolbar: a #GtkToolbar
3016 * Retrieves the icon size for the toolbar. See gtk_toolbar_set_icon_size().
3018 * Return value: (type int): the current icon size for the icons on
3022 gtk_toolbar_get_icon_size (GtkToolbar *toolbar)
3024 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), DEFAULT_ICON_SIZE);
3026 return toolbar->priv->icon_size;
3030 * gtk_toolbar_get_relief_style:
3031 * @toolbar: a #GtkToolbar
3033 * Returns the relief style of buttons on @toolbar. See
3034 * gtk_button_set_relief().
3036 * Return value: The relief style of buttons on @toolbar.
3041 gtk_toolbar_get_relief_style (GtkToolbar *toolbar)
3043 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), GTK_RELIEF_NONE);
3045 return get_button_relief (toolbar);
3049 * gtk_toolbar_set_show_arrow:
3050 * @toolbar: a #GtkToolbar
3051 * @show_arrow: Whether to show an overflow menu
3053 * Sets whether to show an overflow menu when
3054 * @toolbar doesn't have room for all items on it. If %TRUE,
3055 * items that there are not room are available through an
3061 gtk_toolbar_set_show_arrow (GtkToolbar *toolbar,
3062 gboolean show_arrow)
3064 GtkToolbarPrivate *priv;
3066 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3068 priv = toolbar->priv;
3070 show_arrow = show_arrow != FALSE;
3072 if (priv->show_arrow != show_arrow)
3074 priv->show_arrow = show_arrow;
3076 if (!priv->show_arrow)
3077 gtk_widget_hide (priv->arrow_button);
3079 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3080 g_object_notify (G_OBJECT (toolbar), "show-arrow");
3085 * gtk_toolbar_get_show_arrow:
3086 * @toolbar: a #GtkToolbar
3088 * Returns whether the toolbar has an overflow menu.
3089 * See gtk_toolbar_set_show_arrow().
3091 * Return value: %TRUE if the toolbar has an overflow menu.
3096 gtk_toolbar_get_show_arrow (GtkToolbar *toolbar)
3098 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
3100 return toolbar->priv->show_arrow;
3104 * gtk_toolbar_get_drop_index:
3105 * @toolbar: a #GtkToolbar
3106 * @x: x coordinate of a point on the toolbar
3107 * @y: y coordinate of a point on the toolbar
3109 * Returns the position corresponding to the indicated point on
3110 * @toolbar. This is useful when dragging items to the toolbar:
3111 * this function returns the position a new item should be
3114 * @x and @y are in @toolbar coordinates.
3116 * Return value: The position corresponding to the point (@x, @y) on the toolbar.
3121 gtk_toolbar_get_drop_index (GtkToolbar *toolbar,
3125 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), -1);
3127 return physical_to_logical (toolbar, find_drop_index (toolbar, x, y));
3131 gtk_toolbar_finalize (GObject *object)
3134 GtkToolbar *toolbar = GTK_TOOLBAR (object);
3135 GtkToolbarPrivate *priv = toolbar->priv;
3137 if (priv->arrow_button)
3138 gtk_widget_unparent (priv->arrow_button);
3140 for (list = priv->content; list != NULL; list = list->next)
3142 ToolbarContent *content = list->data;
3144 toolbar_content_free (content);
3147 g_list_free (priv->content);
3149 g_timer_destroy (priv->timer);
3153 g_signal_handlers_disconnect_by_func (priv->menu,
3154 menu_deactivated, toolbar);
3155 gtk_widget_destroy (GTK_WIDGET (priv->menu));
3159 g_source_remove (priv->idle_id);
3161 G_OBJECT_CLASS (gtk_toolbar_parent_class)->finalize (object);
3165 * gtk_toolbar_set_icon_size:
3166 * @toolbar: A #GtkToolbar
3167 * @icon_size: (type int): The #GtkIconSize that stock icons in the
3168 * toolbar shall have.
3170 * This function sets the size of stock icons in the toolbar. You
3171 * can call it both before you add the icons and after they've been
3172 * added. The size you set will override user preferences for the default
3175 * This should only be used for special-purpose toolbars, normal
3176 * application toolbars should respect the user preferences for the
3180 gtk_toolbar_set_icon_size (GtkToolbar *toolbar,
3181 GtkIconSize icon_size)
3183 GtkToolbarPrivate *priv;
3185 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3186 g_return_if_fail (icon_size != GTK_ICON_SIZE_INVALID);
3188 priv = toolbar->priv;
3190 if (!priv->icon_size_set)
3192 priv->icon_size_set = TRUE;
3193 g_object_notify (G_OBJECT (toolbar), "icon-size-set");
3196 if (priv->icon_size == icon_size)
3199 priv->icon_size = icon_size;
3200 g_object_notify (G_OBJECT (toolbar), "icon-size");
3202 gtk_toolbar_reconfigured (toolbar);
3204 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3208 * gtk_toolbar_unset_icon_size:
3209 * @toolbar: a #GtkToolbar
3211 * Unsets toolbar icon size set with gtk_toolbar_set_icon_size(), so that
3212 * user preferences will be used to determine the icon size.
3215 gtk_toolbar_unset_icon_size (GtkToolbar *toolbar)
3217 GtkToolbarPrivate *priv;
3220 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3222 priv = toolbar->priv;
3224 if (priv->icon_size_set)
3226 GtkSettings *settings = toolbar_get_settings (toolbar);
3230 g_object_get (settings,
3231 "gtk-toolbar-icon-size", &size,
3235 size = DEFAULT_ICON_SIZE;
3237 if (size != priv->icon_size)
3239 gtk_toolbar_set_icon_size (toolbar, size);
3240 g_object_notify (G_OBJECT (toolbar), "icon-size");
3243 priv->icon_size_set = FALSE;
3244 g_object_notify (G_OBJECT (toolbar), "icon-size-set");
3249 * ToolbarContent methods
3257 struct _ToolbarContent
3262 GtkAllocation allocation;
3263 GtkAllocation start_allocation;
3264 GtkAllocation goal_allocation;
3265 guint is_placeholder : 1;
3266 guint disappearing : 1;
3271 toolbar_item_visiblity_notify_cb (GObject *obj,
3275 GtkToolbar *toolbar = user_data;
3277 gtk_toolbar_invalidate_order (toolbar);
3280 static ToolbarContent *
3281 toolbar_content_new_tool_item (GtkToolbar *toolbar,
3283 gboolean is_placeholder,
3286 GtkToolbarPrivate *priv = toolbar->priv;
3287 ToolbarContent *content;
3289 content = g_slice_new0 (ToolbarContent);
3291 content->state = NOT_ALLOCATED;
3292 content->item = item;
3293 content->is_placeholder = is_placeholder;
3295 priv->content = g_list_insert (priv->content, content, pos);
3297 gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (toolbar));
3298 gtk_toolbar_invalidate_order (toolbar);
3300 g_signal_connect (content->item, "notify::visible",
3301 G_CALLBACK (toolbar_item_visiblity_notify_cb), toolbar);
3303 if (!is_placeholder)
3305 priv->num_children++;
3307 gtk_toolbar_stop_sliding (toolbar);
3310 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3311 priv->need_rebuild = TRUE;
3317 toolbar_content_remove (ToolbarContent *content,
3318 GtkToolbar *toolbar)
3320 GtkToolbarPrivate *priv = toolbar->priv;
3322 gtk_toolbar_invalidate_order (toolbar);
3323 gtk_widget_unparent (GTK_WIDGET (content->item));
3325 g_signal_handlers_disconnect_by_func (content->item,
3326 toolbar_item_visiblity_notify_cb,
3329 priv->content = g_list_remove (priv->content, content);
3331 if (!toolbar_content_is_placeholder (content))
3332 priv->num_children--;
3334 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3335 priv->need_rebuild = TRUE;
3339 toolbar_content_free (ToolbarContent *content)
3341 g_slice_free (ToolbarContent, content);
3345 calculate_max_homogeneous_pixels (GtkWidget *widget)
3347 PangoContext *context;
3348 PangoFontMetrics *metrics;
3349 const PangoFontDescription *font_desc;
3350 GtkStyleContext *style_context;
3351 GtkStateFlags state;
3354 context = gtk_widget_get_pango_context (widget);
3355 style_context = gtk_widget_get_style_context (widget);
3356 state = gtk_widget_get_state_flags (widget);
3358 font_desc = gtk_style_context_get_font (style_context, state);
3360 metrics = pango_context_get_metrics (context, font_desc,
3361 pango_context_get_language (context));
3362 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3363 pango_font_metrics_unref (metrics);
3365 return PANGO_PIXELS (MAX_HOMOGENEOUS_N_CHARS * char_width);
3369 toolbar_content_draw (ToolbarContent *content,
3370 GtkContainer *container,
3375 if (content->is_placeholder)
3378 widget = GTK_WIDGET (content->item);
3381 gtk_container_propagate_draw (container, widget, cr);
3385 toolbar_content_visible (ToolbarContent *content,
3386 GtkToolbar *toolbar)
3388 GtkToolbarPrivate *priv = toolbar->priv;
3391 item = content->item;
3393 if (!gtk_widget_get_visible (GTK_WIDGET (item)))
3396 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
3397 gtk_tool_item_get_visible_horizontal (item))
3400 if (priv->orientation == GTK_ORIENTATION_VERTICAL &&
3401 gtk_tool_item_get_visible_vertical (item))
3408 toolbar_content_size_request (ToolbarContent *content,
3409 GtkToolbar *toolbar,
3410 GtkRequisition *requisition)
3412 gtk_widget_get_preferred_size (GTK_WIDGET (content->item),
3414 if (content->is_placeholder &&
3415 content->disappearing)
3417 requisition->width = 0;
3418 requisition->height = 0;
3423 toolbar_content_is_homogeneous (ToolbarContent *content,
3424 GtkToolbar *toolbar)
3426 GtkToolbarPrivate *priv = toolbar->priv;
3427 GtkRequisition requisition;
3430 if (priv->max_homogeneous_pixels < 0)
3432 priv->max_homogeneous_pixels =
3433 calculate_max_homogeneous_pixels (GTK_WIDGET (toolbar));
3436 toolbar_content_size_request (content, toolbar, &requisition);
3438 if (requisition.width > priv->max_homogeneous_pixels)
3441 result = gtk_tool_item_get_homogeneous (content->item) &&
3442 !GTK_IS_SEPARATOR_TOOL_ITEM (content->item);
3444 if (gtk_tool_item_get_is_important (content->item) &&
3445 priv->style == GTK_TOOLBAR_BOTH_HORIZ &&
3446 priv->orientation == GTK_ORIENTATION_HORIZONTAL)
3455 toolbar_content_is_placeholder (ToolbarContent *content)
3457 if (content->is_placeholder)
3464 toolbar_content_disappearing (ToolbarContent *content)
3466 if (content->disappearing)
3473 toolbar_content_get_state (ToolbarContent *content)
3475 return content->state;
3479 toolbar_content_child_visible (ToolbarContent *content)
3481 return gtk_widget_get_child_visible (GTK_WIDGET (content->item));
3485 toolbar_content_get_goal_allocation (ToolbarContent *content,
3486 GtkAllocation *allocation)
3488 *allocation = content->goal_allocation;
3492 toolbar_content_get_allocation (ToolbarContent *content,
3493 GtkAllocation *allocation)
3495 *allocation = content->allocation;
3499 toolbar_content_set_start_allocation (ToolbarContent *content,
3500 GtkAllocation *allocation)
3502 content->start_allocation = *allocation;
3506 toolbar_content_get_expand (ToolbarContent *content)
3508 if (!content->disappearing &&
3509 gtk_tool_item_get_expand (content->item))
3516 toolbar_content_set_goal_allocation (ToolbarContent *content,
3517 GtkAllocation *allocation)
3519 content->goal_allocation = *allocation;
3523 toolbar_content_set_child_visible (ToolbarContent *content,
3524 GtkToolbar *toolbar,
3527 gtk_widget_set_child_visible (GTK_WIDGET (content->item),
3532 toolbar_content_get_start_allocation (ToolbarContent *content,
3533 GtkAllocation *start_allocation)
3535 *start_allocation = content->start_allocation;
3539 toolbar_content_size_allocate (ToolbarContent *content,
3540 GtkAllocation *allocation)
3542 content->allocation = *allocation;
3543 gtk_widget_size_allocate (GTK_WIDGET (content->item),
3548 toolbar_content_set_state (ToolbarContent *content,
3551 content->state = state;
3555 toolbar_content_get_widget (ToolbarContent *content)
3557 return GTK_WIDGET (content->item);
3562 toolbar_content_set_disappearing (ToolbarContent *content,
3563 gboolean disappearing)
3565 content->disappearing = disappearing;
3569 toolbar_content_set_size_request (ToolbarContent *content,
3573 gtk_widget_set_size_request (GTK_WIDGET (content->item),
3578 toolbar_content_toolbar_reconfigured (ToolbarContent *content,
3579 GtkToolbar *toolbar)
3581 gtk_tool_item_toolbar_reconfigured (content->item);
3585 toolbar_content_retrieve_menu_item (ToolbarContent *content)
3587 return gtk_tool_item_retrieve_proxy_menu_item (content->item);
3591 toolbar_content_has_proxy_menu_item (ToolbarContent *content)
3593 GtkWidget *menu_item;
3595 if (content->has_menu == YES)
3597 else if (content->has_menu == NO)
3600 menu_item = toolbar_content_retrieve_menu_item (content);
3602 content->has_menu = menu_item? YES : NO;
3604 return menu_item != NULL;
3608 toolbar_content_set_unknown_menu_status (ToolbarContent *content)
3610 content->has_menu = UNKNOWN;
3614 toolbar_content_is_separator (ToolbarContent *content)
3616 return GTK_IS_SEPARATOR_TOOL_ITEM (content->item);
3620 toolbar_content_set_expand (ToolbarContent *content,
3623 gtk_tool_item_set_expand (content->item, expand);
3627 toolbar_content_show_all (ToolbarContent *content)
3631 widget = toolbar_content_get_widget (content);
3633 gtk_widget_show_all (widget);
3639 static GtkReliefStyle
3640 get_button_relief (GtkToolbar *toolbar)
3642 GtkReliefStyle button_relief = GTK_RELIEF_NORMAL;
3644 gtk_widget_style_get (GTK_WIDGET (toolbar),
3645 "button-relief", &button_relief,
3648 return button_relief;
3652 get_internal_padding (GtkToolbar *toolbar)
3656 gtk_widget_style_get (GTK_WIDGET (toolbar),
3657 "internal-padding", &ipadding,
3664 get_max_child_expand (GtkToolbar *toolbar)
3666 gint mexpand = G_MAXINT;
3668 gtk_widget_style_get (GTK_WIDGET (toolbar),
3669 "max-child-expand", &mexpand,
3674 static GtkShadowType
3675 get_shadow_type (GtkToolbar *toolbar)
3677 GtkShadowType shadow_type;
3679 gtk_widget_style_get (GTK_WIDGET (toolbar),
3680 "shadow-type", &shadow_type,
3686 /* GTK+ internal methods */
3689 _gtk_toolbar_get_default_space_size (void)
3691 return DEFAULT_SPACE_SIZE;
3695 _gtk_toolbar_paint_space_line (GtkWidget *widget,
3696 GtkToolbar *toolbar,
3699 GtkOrientation orientation;
3700 GtkStyleContext *context;
3701 GtkStateFlags state;
3704 const gdouble start_fraction = (SPACE_LINE_START / SPACE_LINE_DIVISION);
3705 const gdouble end_fraction = (SPACE_LINE_END / SPACE_LINE_DIVISION);
3707 g_return_if_fail (GTK_IS_WIDGET (widget));
3709 orientation = toolbar ? toolbar->priv->orientation : GTK_ORIENTATION_HORIZONTAL;
3711 context = gtk_widget_get_style_context (widget);
3712 state = gtk_widget_get_state_flags (widget);
3713 width = gtk_widget_get_allocated_width (widget);
3714 height = gtk_widget_get_allocated_height (widget);
3715 gtk_style_context_get_padding (context, state, &padding);
3717 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3719 gboolean wide_separators;
3720 gint separator_width;
3722 gtk_widget_style_get (widget,
3723 "wide-separators", &wide_separators,
3724 "separator-width", &separator_width,
3727 if (wide_separators)
3728 gtk_render_frame (context, cr,
3729 (width - separator_width) / 2,
3730 height * start_fraction,
3732 height * (end_fraction - start_fraction));
3734 gtk_render_line (context, cr,
3735 (width - padding.left) / 2,
3736 height * start_fraction,
3737 (width - padding.left) / 2,
3738 height * end_fraction);
3742 gboolean wide_separators;
3743 gint separator_height;
3745 gtk_widget_style_get (widget,
3746 "wide-separators", &wide_separators,
3747 "separator-height", &separator_height,
3750 if (wide_separators)
3751 gtk_render_frame (context, cr,
3752 width * start_fraction,
3753 (height - separator_height) / 2,
3754 width * (end_fraction - start_fraction),
3757 gtk_render_line (context, cr,
3758 width * start_fraction,
3759 (height - padding.top) / 2,
3760 width * end_fraction,
3761 (height - padding.top) / 2);
3766 _gtk_toolbar_elide_underscores (const gchar *original)
3769 const gchar *p, *end;
3771 gboolean last_underscore;
3776 len = strlen (original);
3777 q = result = g_malloc (len + 1);
3778 last_underscore = FALSE;
3780 end = original + len;
3781 for (p = original; p < end; p++)
3783 if (!last_underscore && *p == '_')
3784 last_underscore = TRUE;
3787 last_underscore = FALSE;
3788 if (original + 2 <= p && p + 1 <= end &&
3789 p[-2] == '(' && p[-1] == '_' && p[0] != '_' && p[1] == ')')
3800 if (last_underscore)
3809 toolbar_get_icon_size (GtkToolShell *shell)
3811 GtkToolbar *toolbar = GTK_TOOLBAR (shell);
3812 GtkToolbarPrivate *priv = toolbar->priv;
3814 return priv->icon_size;
3817 static GtkOrientation
3818 toolbar_get_orientation (GtkToolShell *shell)
3820 GtkToolbar *toolbar = GTK_TOOLBAR (shell);
3821 GtkToolbarPrivate *priv = toolbar->priv;
3823 return priv->orientation;
3826 static GtkToolbarStyle
3827 toolbar_get_style (GtkToolShell *shell)
3829 GtkToolbar *toolbar = GTK_TOOLBAR (shell);
3830 GtkToolbarPrivate *priv = toolbar->priv;
3835 static GtkReliefStyle
3836 toolbar_get_relief_style (GtkToolShell *shell)
3838 return get_button_relief (GTK_TOOLBAR (shell));
3842 toolbar_rebuild_menu (GtkToolShell *shell)
3844 GtkToolbar *toolbar = GTK_TOOLBAR (shell);
3845 GtkToolbarPrivate *priv = toolbar->priv;
3848 priv->need_rebuild = TRUE;
3850 for (list = priv->content; list != NULL; list = list->next)
3852 ToolbarContent *content = list->data;
3854 toolbar_content_set_unknown_menu_status (content);
3857 gtk_widget_queue_resize (GTK_WIDGET (shell));
3860 typedef struct _CountingData CountingData;
3861 struct _CountingData {
3869 count_widget_position (GtkWidget *widget,
3872 CountingData *count = data;
3874 if (!gtk_widget_get_visible (widget))
3877 if (count->widget == widget)
3878 count->found = TRUE;
3879 else if (count->found)
3886 gtk_toolbar_get_visible_position (GtkToolbar *toolbar,
3889 CountingData count = { child, FALSE, 0, 0 };
3891 /* foreach iterates in visible order */
3892 gtk_container_forall (GTK_CONTAINER (toolbar),
3893 count_widget_position,
3896 g_assert (count.found);
3898 if (toolbar->priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
3899 gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
3902 return count.before;
3906 add_widget_to_path (gpointer data,
3909 GtkWidget *widget = data;
3910 GtkWidgetPath *path = user_data;
3912 if (gtk_widget_get_visible (widget))
3913 gtk_widget_path_append_for_widget (path, widget);
3916 static GtkWidgetPath *
3917 gtk_toolbar_get_path_for_child (GtkContainer *container,
3920 GtkWidgetPath *path;
3921 GtkToolbar *toolbar;
3922 GtkToolbarPrivate *priv;
3925 toolbar = GTK_TOOLBAR (container);
3926 priv = toolbar->priv;
3928 if (priv->sibling_path == NULL)
3932 /* build a path for all the visible children;
3933 * get_children works in visible order
3935 priv->sibling_path = gtk_widget_path_new ();
3936 children = gtk_container_get_children (container);
3938 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
3939 gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
3940 children = g_list_reverse (children);
3942 g_list_foreach (children, add_widget_to_path, priv->sibling_path);
3943 g_list_free (children);
3946 path = gtk_widget_path_copy (gtk_widget_get_path (GTK_WIDGET (container)));
3947 if (gtk_widget_get_visible (child))
3949 vis_index = gtk_toolbar_get_visible_position (toolbar, child);
3951 if (vis_index < gtk_widget_path_length (priv->sibling_path))
3952 gtk_widget_path_append_with_siblings (path,
3956 gtk_widget_path_append_for_widget (path, child);
3959 gtk_widget_path_append_for_widget (path, child);
3965 gtk_toolbar_invalidate_order (GtkToolbar *toolbar)
3967 GtkToolbarPrivate *priv = toolbar->priv;
3969 if (priv->sibling_path != NULL)
3971 gtk_widget_path_unref (priv->sibling_path);
3972 priv->sibling_path = NULL;
3974 gtk_container_foreach (GTK_CONTAINER (toolbar),
3975 (GtkCallback) gtk_widget_reset_style,