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"
42 #include "gtkcontainerprivate.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_dispose (GObject *object);
222 static void gtk_toolbar_show_all (GtkWidget *widget);
223 static void gtk_toolbar_add (GtkContainer *container,
225 static void gtk_toolbar_remove (GtkContainer *container,
227 static void gtk_toolbar_forall (GtkContainer *container,
228 gboolean include_internals,
229 GtkCallback callback,
230 gpointer callback_data);
231 static GType gtk_toolbar_child_type (GtkContainer *container);
232 static GtkWidgetPath * gtk_toolbar_get_path_for_child
233 (GtkContainer *container,
235 static void gtk_toolbar_invalidate_order (GtkToolbar *toolbar);
237 static void gtk_toolbar_direction_changed (GtkWidget *widget,
238 GtkTextDirection previous_direction);
239 static void gtk_toolbar_orientation_changed (GtkToolbar *toolbar,
240 GtkOrientation orientation);
241 static void gtk_toolbar_real_style_changed (GtkToolbar *toolbar,
242 GtkToolbarStyle style);
243 static gboolean gtk_toolbar_focus_home_or_end (GtkToolbar *toolbar,
244 gboolean focus_home);
245 static gboolean gtk_toolbar_button_press (GtkWidget *toolbar,
246 GdkEventButton *event);
247 static gboolean gtk_toolbar_arrow_button_press (GtkWidget *button,
248 GdkEventButton *event,
249 GtkToolbar *toolbar);
250 static void gtk_toolbar_arrow_button_clicked (GtkWidget *button,
251 GtkToolbar *toolbar);
252 static void gtk_toolbar_update_button_relief (GtkToolbar *toolbar);
253 static gboolean gtk_toolbar_popup_menu (GtkWidget *toolbar);
254 static void gtk_toolbar_reconfigured (GtkToolbar *toolbar);
256 static GtkReliefStyle get_button_relief (GtkToolbar *toolbar);
257 static gint get_internal_padding (GtkToolbar *toolbar);
258 static gint get_max_child_expand (GtkToolbar *toolbar);
259 static GtkShadowType get_shadow_type (GtkToolbar *toolbar);
261 /* methods on ToolbarContent 'class' */
262 static ToolbarContent *toolbar_content_new_tool_item (GtkToolbar *toolbar,
264 gboolean is_placeholder,
266 static void toolbar_content_remove (ToolbarContent *content,
267 GtkToolbar *toolbar);
268 static void toolbar_content_free (ToolbarContent *content);
269 static void toolbar_content_draw (ToolbarContent *content,
270 GtkContainer *container,
272 static gboolean toolbar_content_visible (ToolbarContent *content,
273 GtkToolbar *toolbar);
274 static void toolbar_content_size_request (ToolbarContent *content,
276 GtkRequisition *requisition);
277 static gboolean toolbar_content_is_homogeneous (ToolbarContent *content,
278 GtkToolbar *toolbar);
279 static gboolean toolbar_content_is_placeholder (ToolbarContent *content);
280 static gboolean toolbar_content_disappearing (ToolbarContent *content);
281 static ItemState toolbar_content_get_state (ToolbarContent *content);
282 static gboolean toolbar_content_child_visible (ToolbarContent *content);
283 static void toolbar_content_get_goal_allocation (ToolbarContent *content,
284 GtkAllocation *allocation);
285 static void toolbar_content_get_allocation (ToolbarContent *content,
286 GtkAllocation *allocation);
287 static void toolbar_content_set_start_allocation (ToolbarContent *content,
288 GtkAllocation *new_start_allocation);
289 static void toolbar_content_get_start_allocation (ToolbarContent *content,
290 GtkAllocation *start_allocation);
291 static gboolean toolbar_content_get_expand (ToolbarContent *content);
292 static void toolbar_content_set_goal_allocation (ToolbarContent *content,
293 GtkAllocation *allocation);
294 static void toolbar_content_set_child_visible (ToolbarContent *content,
297 static void toolbar_content_size_allocate (ToolbarContent *content,
298 GtkAllocation *allocation);
299 static void toolbar_content_set_state (ToolbarContent *content,
300 ItemState new_state);
301 static GtkWidget * toolbar_content_get_widget (ToolbarContent *content);
302 static void toolbar_content_set_disappearing (ToolbarContent *content,
303 gboolean disappearing);
304 static void toolbar_content_set_size_request (ToolbarContent *content,
307 static void toolbar_content_toolbar_reconfigured (ToolbarContent *content,
308 GtkToolbar *toolbar);
309 static GtkWidget * toolbar_content_retrieve_menu_item (ToolbarContent *content);
310 static gboolean toolbar_content_has_proxy_menu_item (ToolbarContent *content);
311 static gboolean toolbar_content_is_separator (ToolbarContent *content);
312 static void toolbar_content_show_all (ToolbarContent *content);
313 static void toolbar_content_set_expand (ToolbarContent *content,
316 static void toolbar_tool_shell_iface_init (GtkToolShellIface *iface);
317 static GtkIconSize toolbar_get_icon_size (GtkToolShell *shell);
318 static GtkOrientation toolbar_get_orientation (GtkToolShell *shell);
319 static GtkToolbarStyle toolbar_get_style (GtkToolShell *shell);
320 static GtkReliefStyle toolbar_get_relief_style (GtkToolShell *shell);
321 static void toolbar_rebuild_menu (GtkToolShell *shell);
324 G_DEFINE_TYPE_WITH_CODE (GtkToolbar, gtk_toolbar, GTK_TYPE_CONTAINER,
325 G_IMPLEMENT_INTERFACE (GTK_TYPE_TOOL_SHELL,
326 toolbar_tool_shell_iface_init)
327 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
330 static guint toolbar_signals[LAST_SIGNAL] = { 0 };
334 add_arrow_bindings (GtkBindingSet *binding_set,
336 GtkDirectionType dir)
338 guint keypad_keysym = keysym - GDK_KEY_Left + GDK_KEY_KP_Left;
340 gtk_binding_entry_add_signal (binding_set, keysym, 0,
342 GTK_TYPE_DIRECTION_TYPE, dir);
343 gtk_binding_entry_add_signal (binding_set, keypad_keysym, 0,
345 GTK_TYPE_DIRECTION_TYPE, dir);
349 add_ctrl_tab_bindings (GtkBindingSet *binding_set,
350 GdkModifierType modifiers,
351 GtkDirectionType direction)
353 gtk_binding_entry_add_signal (binding_set,
354 GDK_KEY_Tab, GDK_CONTROL_MASK | modifiers,
356 GTK_TYPE_DIRECTION_TYPE, direction);
357 gtk_binding_entry_add_signal (binding_set,
358 GDK_KEY_KP_Tab, GDK_CONTROL_MASK | modifiers,
360 GTK_TYPE_DIRECTION_TYPE, direction);
364 gtk_toolbar_class_init (GtkToolbarClass *klass)
366 GObjectClass *gobject_class;
367 GtkWidgetClass *widget_class;
368 GtkContainerClass *container_class;
369 GtkBindingSet *binding_set;
371 gobject_class = (GObjectClass *)klass;
372 widget_class = (GtkWidgetClass *)klass;
373 container_class = (GtkContainerClass *)klass;
375 gobject_class->set_property = gtk_toolbar_set_property;
376 gobject_class->get_property = gtk_toolbar_get_property;
377 gobject_class->finalize = gtk_toolbar_finalize;
378 gobject_class->dispose = gtk_toolbar_dispose;
380 widget_class->button_press_event = gtk_toolbar_button_press;
381 widget_class->draw = gtk_toolbar_draw;
382 widget_class->get_preferred_width = gtk_toolbar_get_preferred_width;
383 widget_class->get_preferred_height = gtk_toolbar_get_preferred_height;
384 widget_class->size_allocate = gtk_toolbar_size_allocate;
385 widget_class->style_updated = gtk_toolbar_style_updated;
386 widget_class->focus = gtk_toolbar_focus;
388 gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_TOOL_BAR);
390 /* need to override the base class function via override_class_handler,
391 * because the signal slot is not available in GtkWidgetClass
393 g_signal_override_class_handler ("move-focus",
395 G_CALLBACK (gtk_toolbar_move_focus));
397 widget_class->screen_changed = gtk_toolbar_screen_changed;
398 widget_class->realize = gtk_toolbar_realize;
399 widget_class->unrealize = gtk_toolbar_unrealize;
400 widget_class->map = gtk_toolbar_map;
401 widget_class->unmap = gtk_toolbar_unmap;
402 widget_class->popup_menu = gtk_toolbar_popup_menu;
403 widget_class->show_all = gtk_toolbar_show_all;
404 widget_class->direction_changed = gtk_toolbar_direction_changed;
406 container_class->add = gtk_toolbar_add;
407 container_class->remove = gtk_toolbar_remove;
408 container_class->forall = gtk_toolbar_forall;
409 container_class->child_type = gtk_toolbar_child_type;
410 container_class->get_child_property = gtk_toolbar_get_child_property;
411 container_class->set_child_property = gtk_toolbar_set_child_property;
412 container_class->get_path_for_child = gtk_toolbar_get_path_for_child;
414 klass->orientation_changed = gtk_toolbar_orientation_changed;
415 klass->style_changed = gtk_toolbar_real_style_changed;
418 * GtkToolbar::orientation-changed:
419 * @toolbar: the object which emitted the signal
420 * @orientation: the new #GtkOrientation of the toolbar
422 * Emitted when the orientation of the toolbar changes.
424 toolbar_signals[ORIENTATION_CHANGED] =
425 g_signal_new (I_("orientation-changed"),
426 G_OBJECT_CLASS_TYPE (klass),
428 G_STRUCT_OFFSET (GtkToolbarClass, orientation_changed),
430 g_cclosure_marshal_VOID__ENUM,
432 GTK_TYPE_ORIENTATION);
434 * GtkToolbar::style-changed:
435 * @toolbar: The #GtkToolbar which emitted the signal
436 * @style: the new #GtkToolbarStyle of the toolbar
438 * Emitted when the style of the toolbar changes.
440 toolbar_signals[STYLE_CHANGED] =
441 g_signal_new (I_("style-changed"),
442 G_OBJECT_CLASS_TYPE (klass),
444 G_STRUCT_OFFSET (GtkToolbarClass, style_changed),
446 g_cclosure_marshal_VOID__ENUM,
448 GTK_TYPE_TOOLBAR_STYLE);
450 * GtkToolbar::popup-context-menu:
451 * @toolbar: the #GtkToolbar which emitted the signal
452 * @x: the x coordinate of the point where the menu should appear
453 * @y: the y coordinate of the point where the menu should appear
454 * @button: the mouse button the user pressed, or -1
456 * Emitted when the user right-clicks the toolbar or uses the
457 * keybinding to display a popup menu.
459 * Application developers should handle this signal if they want
460 * to display a context menu on the toolbar. The context-menu should
461 * appear at the coordinates given by @x and @y. The mouse button
462 * number is given by the @button parameter. If the menu was popped
463 * up using the keybaord, @button is -1.
465 * Return value: return %TRUE if the signal was handled, %FALSE if not
467 toolbar_signals[POPUP_CONTEXT_MENU] =
468 g_signal_new (I_("popup-context-menu"),
469 G_OBJECT_CLASS_TYPE (klass),
471 G_STRUCT_OFFSET (GtkToolbarClass, popup_context_menu),
472 _gtk_boolean_handled_accumulator, NULL,
473 _gtk_marshal_BOOLEAN__INT_INT_INT,
475 G_TYPE_INT, G_TYPE_INT,
479 * GtkToolbar::focus-home-or-end:
480 * @toolbar: the #GtkToolbar which emitted the signal
481 * @focus_home: %TRUE if the first item should be focused
483 * A keybinding signal used internally by GTK+. This signal can't
484 * be used in application code
486 * Return value: %TRUE if the signal was handled, %FALSE if not
488 toolbar_signals[FOCUS_HOME_OR_END] =
489 g_signal_new_class_handler (I_("focus-home-or-end"),
490 G_OBJECT_CLASS_TYPE (klass),
491 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
492 G_CALLBACK (gtk_toolbar_focus_home_or_end),
494 _gtk_marshal_BOOLEAN__BOOLEAN,
499 g_object_class_override_property (gobject_class,
503 g_object_class_install_property (gobject_class,
505 g_param_spec_enum ("toolbar-style",
507 P_("How to draw the toolbar"),
508 GTK_TYPE_TOOLBAR_STYLE,
509 DEFAULT_TOOLBAR_STYLE,
510 GTK_PARAM_READWRITE));
511 g_object_class_install_property (gobject_class,
513 g_param_spec_boolean ("show-arrow",
515 P_("If an arrow should be shown if the toolbar doesn't fit"),
517 GTK_PARAM_READWRITE));
520 * GtkToolbar:icon-size:
522 * The size of the icons in a toolbar is normally determined by
523 * the toolbar-icon-size setting. When this property is set, it
524 * overrides the setting.
526 * This should only be used for special-purpose toolbars, normal
527 * application toolbars should respect the user preferences for the
532 g_object_class_install_property (gobject_class,
534 g_param_spec_int ("icon-size",
536 P_("Size of icons in this toolbar"),
539 GTK_PARAM_READWRITE));
542 * GtkToolbar:icon-size-set:
544 * Is %TRUE if the icon-size property has been set.
548 g_object_class_install_property (gobject_class,
550 g_param_spec_boolean ("icon-size-set",
552 P_("Whether the icon-size property has been set"),
554 GTK_PARAM_READWRITE));
556 /* child properties */
557 gtk_container_class_install_child_property (container_class,
559 g_param_spec_boolean ("expand",
561 P_("Whether the item should receive extra space when the toolbar grows"),
563 GTK_PARAM_READWRITE));
565 gtk_container_class_install_child_property (container_class,
566 CHILD_PROP_HOMOGENEOUS,
567 g_param_spec_boolean ("homogeneous",
569 P_("Whether the item should be the same size as other homogeneous items"),
571 GTK_PARAM_READWRITE));
573 /* style properties */
574 gtk_widget_class_install_style_property (widget_class,
575 g_param_spec_int ("space-size",
577 P_("Size of spacers"),
581 GTK_PARAM_READABLE));
583 gtk_widget_class_install_style_property (widget_class,
584 g_param_spec_int ("internal-padding",
585 P_("Internal padding"),
586 P_("Amount of border space between the toolbar shadow and the buttons"),
590 GTK_PARAM_READABLE));
592 gtk_widget_class_install_style_property (widget_class,
593 g_param_spec_int ("max-child-expand",
594 P_("Maximum child expand"),
595 P_("Maximum amount of space an expandable item will be given"),
599 GTK_PARAM_READABLE));
601 gtk_widget_class_install_style_property (widget_class,
602 g_param_spec_enum ("space-style",
604 P_("Whether spacers are vertical lines or just blank"),
605 GTK_TYPE_TOOLBAR_SPACE_STYLE,
607 GTK_PARAM_READABLE));
609 gtk_widget_class_install_style_property (widget_class,
610 g_param_spec_enum ("button-relief",
612 P_("Type of bevel around toolbar buttons"),
613 GTK_TYPE_RELIEF_STYLE,
615 GTK_PARAM_READABLE));
616 gtk_widget_class_install_style_property (widget_class,
617 g_param_spec_enum ("shadow-type",
619 P_("Style of bevel around the toolbar"),
620 GTK_TYPE_SHADOW_TYPE,
622 GTK_PARAM_READABLE));
624 binding_set = gtk_binding_set_by_class (klass);
626 add_arrow_bindings (binding_set, GDK_KEY_Left, GTK_DIR_LEFT);
627 add_arrow_bindings (binding_set, GDK_KEY_Right, GTK_DIR_RIGHT);
628 add_arrow_bindings (binding_set, GDK_KEY_Up, GTK_DIR_UP);
629 add_arrow_bindings (binding_set, GDK_KEY_Down, GTK_DIR_DOWN);
631 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
632 "focus-home-or-end", 1,
633 G_TYPE_BOOLEAN, TRUE);
634 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
635 "focus-home-or-end", 1,
636 G_TYPE_BOOLEAN, TRUE);
637 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
638 "focus-home-or-end", 1,
639 G_TYPE_BOOLEAN, FALSE);
640 gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
641 "focus-home-or-end", 1,
642 G_TYPE_BOOLEAN, FALSE);
644 add_ctrl_tab_bindings (binding_set, 0, GTK_DIR_TAB_FORWARD);
645 add_ctrl_tab_bindings (binding_set, GDK_SHIFT_MASK, GTK_DIR_TAB_BACKWARD);
647 g_type_class_add_private (gobject_class, sizeof (GtkToolbarPrivate));
651 toolbar_tool_shell_iface_init (GtkToolShellIface *iface)
653 iface->get_icon_size = toolbar_get_icon_size;
654 iface->get_orientation = toolbar_get_orientation;
655 iface->get_style = toolbar_get_style;
656 iface->get_relief_style = toolbar_get_relief_style;
657 iface->rebuild_menu = toolbar_rebuild_menu;
661 gtk_toolbar_init (GtkToolbar *toolbar)
663 GtkToolbarPrivate *priv;
664 GtkStyleContext *context;
666 toolbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (toolbar,
669 priv = toolbar->priv;
671 gtk_widget_set_can_focus (GTK_WIDGET (toolbar), FALSE);
672 gtk_widget_set_has_window (GTK_WIDGET (toolbar), FALSE);
674 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
675 priv->style = DEFAULT_TOOLBAR_STYLE;
676 priv->icon_size = DEFAULT_ICON_SIZE;
677 priv->animation = DEFAULT_ANIMATION_STATE;
679 priv->arrow_button = gtk_toggle_button_new ();
680 g_signal_connect (priv->arrow_button, "button-press-event",
681 G_CALLBACK (gtk_toolbar_arrow_button_press), toolbar);
682 g_signal_connect (priv->arrow_button, "clicked",
683 G_CALLBACK (gtk_toolbar_arrow_button_clicked), toolbar);
684 gtk_button_set_relief (GTK_BUTTON (priv->arrow_button),
685 get_button_relief (toolbar));
687 gtk_button_set_focus_on_click (GTK_BUTTON (priv->arrow_button), FALSE);
689 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
690 gtk_widget_set_name (priv->arrow, "gtk-toolbar-arrow");
691 gtk_widget_show (priv->arrow);
692 gtk_container_add (GTK_CONTAINER (priv->arrow_button), priv->arrow);
694 gtk_widget_set_parent (priv->arrow_button, GTK_WIDGET (toolbar));
696 /* which child position a drop will occur at */
698 priv->show_arrow = TRUE;
699 priv->settings = NULL;
701 priv->max_homogeneous_pixels = -1;
703 priv->timer = g_timer_new ();
705 context = gtk_widget_get_style_context (GTK_WIDGET (toolbar));
706 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLBAR);
710 gtk_toolbar_set_property (GObject *object,
715 GtkToolbar *toolbar = GTK_TOOLBAR (object);
716 GtkToolbarPrivate *priv = toolbar->priv;
720 case PROP_ORIENTATION:
721 g_signal_emit (toolbar, toolbar_signals[ORIENTATION_CHANGED], 0,
722 g_value_get_enum (value));
724 case PROP_TOOLBAR_STYLE:
725 gtk_toolbar_set_style (toolbar, g_value_get_enum (value));
727 case PROP_SHOW_ARROW:
728 gtk_toolbar_set_show_arrow (toolbar, g_value_get_boolean (value));
731 gtk_toolbar_set_icon_size (toolbar, g_value_get_int (value));
733 case PROP_ICON_SIZE_SET:
734 if (g_value_get_boolean (value))
735 priv->icon_size_set = TRUE;
737 gtk_toolbar_unset_icon_size (toolbar);
740 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
746 gtk_toolbar_get_property (GObject *object,
751 GtkToolbar *toolbar = GTK_TOOLBAR (object);
752 GtkToolbarPrivate *priv = toolbar->priv;
756 case PROP_ORIENTATION:
757 g_value_set_enum (value, priv->orientation);
759 case PROP_TOOLBAR_STYLE:
760 g_value_set_enum (value, priv->style);
762 case PROP_SHOW_ARROW:
763 g_value_set_boolean (value, priv->show_arrow);
766 g_value_set_int (value, gtk_toolbar_get_icon_size (toolbar));
768 case PROP_ICON_SIZE_SET:
769 g_value_set_boolean (value, priv->icon_size_set);
772 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
778 gtk_toolbar_map (GtkWidget *widget)
780 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
781 GtkToolbarPrivate *priv = toolbar->priv;
783 GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->map (widget);
785 if (priv->event_window)
786 gdk_window_show_unraised (priv->event_window);
790 gtk_toolbar_unmap (GtkWidget *widget)
792 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
793 GtkToolbarPrivate *priv = toolbar->priv;
795 if (priv->event_window)
796 gdk_window_hide (priv->event_window);
798 GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->unmap (widget);
802 gtk_toolbar_realize (GtkWidget *widget)
804 GtkAllocation allocation;
805 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
806 GtkToolbarPrivate *priv = toolbar->priv;
808 GdkWindowAttr attributes;
809 gint attributes_mask;
812 gtk_widget_set_realized (widget, TRUE);
814 gtk_widget_get_allocation (widget, &allocation);
815 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
817 attributes.wclass = GDK_INPUT_ONLY;
818 attributes.window_type = GDK_WINDOW_CHILD;
819 attributes.x = allocation.x + border_width;
820 attributes.y = allocation.y + border_width;
821 attributes.width = allocation.width - border_width * 2;
822 attributes.height = allocation.height - border_width * 2;
823 attributes.event_mask = gtk_widget_get_events (widget);
824 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
825 GDK_BUTTON_RELEASE_MASK |
826 GDK_ENTER_NOTIFY_MASK |
827 GDK_LEAVE_NOTIFY_MASK);
829 attributes_mask = GDK_WA_X | GDK_WA_Y;
831 window = gtk_widget_get_parent_window (widget);
832 gtk_widget_set_window (widget, window);
833 g_object_ref (window);
835 priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
836 &attributes, attributes_mask);
837 gdk_window_set_user_data (priv->event_window, toolbar);
841 gtk_toolbar_unrealize (GtkWidget *widget)
843 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
844 GtkToolbarPrivate *priv = toolbar->priv;
846 if (priv->event_window)
848 gdk_window_set_user_data (priv->event_window, NULL);
849 gdk_window_destroy (priv->event_window);
850 priv->event_window = NULL;
853 GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->unrealize (widget);
857 gtk_toolbar_draw (GtkWidget *widget,
860 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
861 GtkToolbarPrivate *priv = toolbar->priv;
862 GtkStyleContext *context;
867 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
868 context = gtk_widget_get_style_context (widget);
869 state = gtk_widget_get_state_flags (widget);
871 gtk_style_context_save (context);
872 gtk_style_context_set_state (context, state);
874 gtk_render_background (context, cr, border_width, border_width,
875 gtk_widget_get_allocated_width (widget) - 2 * border_width,
876 gtk_widget_get_allocated_height (widget) - 2 * border_width);
877 gtk_render_frame (context, cr, border_width, border_width,
878 gtk_widget_get_allocated_width (widget) - 2 * border_width,
879 gtk_widget_get_allocated_height (widget) - 2 * border_width);
881 for (list = priv->content; list != NULL; list = list->next)
883 ToolbarContent *content = list->data;
885 toolbar_content_draw (content, GTK_CONTAINER (widget), cr);
888 gtk_container_propagate_draw (GTK_CONTAINER (widget),
892 gtk_style_context_restore (context);
898 gtk_toolbar_size_request (GtkWidget *widget,
899 GtkRequisition *requisition)
901 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
902 GtkToolbarPrivate *priv = toolbar->priv;
904 gint max_child_height;
905 gint max_child_width;
906 gint max_homogeneous_child_width;
907 gint max_homogeneous_child_height;
908 gint homogeneous_size;
910 gint pack_front_size;
913 GtkRequisition arrow_requisition;
915 max_homogeneous_child_width = 0;
916 max_homogeneous_child_height = 0;
918 max_child_height = 0;
919 for (list = priv->content; list != NULL; list = list->next)
921 GtkRequisition requisition;
922 ToolbarContent *content = list->data;
924 if (!toolbar_content_visible (content, toolbar))
927 toolbar_content_size_request (content, toolbar, &requisition);
929 max_child_width = MAX (max_child_width, requisition.width);
930 max_child_height = MAX (max_child_height, requisition.height);
932 if (toolbar_content_is_homogeneous (content, toolbar))
934 max_homogeneous_child_width = MAX (max_homogeneous_child_width, requisition.width);
935 max_homogeneous_child_height = MAX (max_homogeneous_child_height, requisition.height);
939 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
940 homogeneous_size = max_homogeneous_child_width;
942 homogeneous_size = max_homogeneous_child_height;
945 for (list = priv->content; list != NULL; list = list->next)
947 ToolbarContent *content = list->data;
950 if (!toolbar_content_visible (content, toolbar))
953 if (toolbar_content_is_homogeneous (content, toolbar))
955 size = homogeneous_size;
959 GtkRequisition requisition;
961 toolbar_content_size_request (content, toolbar, &requisition);
963 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
964 size = requisition.width;
966 size = requisition.height;
969 pack_front_size += size;
972 if (priv->show_arrow)
974 gtk_widget_get_preferred_size (priv->arrow_button,
975 &arrow_requisition, NULL);
977 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
978 long_req = arrow_requisition.width;
980 long_req = arrow_requisition.height;
982 /* There is no point requesting space for the arrow if that would take
983 * up more space than all the items combined
985 long_req = MIN (long_req, pack_front_size);
989 arrow_requisition.height = 0;
990 arrow_requisition.width = 0;
992 long_req = pack_front_size;
995 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
997 requisition->width = long_req;
998 requisition->height = MAX (max_child_height, arrow_requisition.height);
1002 requisition->height = long_req;
1003 requisition->width = MAX (max_child_width, arrow_requisition.width);
1007 ipadding = get_internal_padding (toolbar);
1009 border_width = gtk_container_get_border_width (GTK_CONTAINER (toolbar));
1010 requisition->width += 2 * (ipadding + border_width);
1011 requisition->height += 2 * (ipadding + border_width);
1013 if (get_shadow_type (toolbar) != GTK_SHADOW_NONE)
1015 GtkStyleContext *context;
1016 GtkStateFlags state;
1019 context = gtk_widget_get_style_context (widget);
1020 state = gtk_widget_get_state_flags (widget);
1021 gtk_style_context_get_padding (context, state, &padding);
1023 requisition->width += padding.left + padding.right;
1024 requisition->height += padding.top + padding.bottom;
1027 priv->button_maxw = max_homogeneous_child_width;
1028 priv->button_maxh = max_homogeneous_child_height;
1032 gtk_toolbar_get_preferred_width (GtkWidget *widget,
1036 GtkRequisition requisition;
1038 gtk_toolbar_size_request (widget, &requisition);
1040 *minimum = *natural = requisition.width;
1044 gtk_toolbar_get_preferred_height (GtkWidget *widget,
1048 GtkRequisition requisition;
1050 gtk_toolbar_size_request (widget, &requisition);
1052 *minimum = *natural = requisition.height;
1056 position (GtkToolbar *toolbar,
1061 GtkToolbarPrivate *priv = toolbar->priv;
1064 if (!priv->animation)
1067 if (elapsed <= ACCEL_THRESHOLD)
1069 n_pixels = SLIDE_SPEED * elapsed;
1073 /* The formula is a second degree polynomial in
1074 * @elapsed that has the line SLIDE_SPEED * @elapsed
1075 * as tangent for @elapsed == ACCEL_THRESHOLD.
1076 * This makes @n_pixels a smooth function of elapsed time.
1078 n_pixels = (SLIDE_SPEED / ACCEL_THRESHOLD) * elapsed * elapsed -
1079 SLIDE_SPEED * elapsed + SLIDE_SPEED * ACCEL_THRESHOLD;
1083 return MIN (from + n_pixels, to);
1085 return MAX (from - n_pixels, to);
1089 compute_intermediate_allocation (GtkToolbar *toolbar,
1090 const GtkAllocation *start,
1091 const GtkAllocation *goal,
1092 GtkAllocation *intermediate)
1094 GtkToolbarPrivate *priv = toolbar->priv;
1095 gdouble elapsed = g_timer_elapsed (priv->timer, NULL);
1097 intermediate->x = position (toolbar, start->x, goal->x, elapsed);
1098 intermediate->y = position (toolbar, start->y, goal->y, elapsed);
1099 intermediate->width = position (toolbar, start->x + start->width,
1100 goal->x + goal->width,
1101 elapsed) - intermediate->x;
1102 intermediate->height = position (toolbar, start->y + start->height,
1103 goal->y + goal->height,
1104 elapsed) - intermediate->y;
1108 fixup_allocation_for_rtl (gint total_size,
1109 GtkAllocation *allocation)
1111 allocation->x += (total_size - (2 * allocation->x + allocation->width));
1115 fixup_allocation_for_vertical (GtkAllocation *allocation)
1119 tmp = allocation->x;
1120 allocation->x = allocation->y;
1121 allocation->y = tmp;
1123 tmp = allocation->width;
1124 allocation->width = allocation->height;
1125 allocation->height = tmp;
1129 get_item_size (GtkToolbar *toolbar,
1130 ToolbarContent *content)
1132 GtkToolbarPrivate *priv = toolbar->priv;
1133 GtkRequisition requisition;
1135 toolbar_content_size_request (content, toolbar, &requisition);
1137 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1139 if (toolbar_content_is_homogeneous (content, toolbar))
1140 return priv->button_maxw;
1142 return requisition.width;
1146 if (toolbar_content_is_homogeneous (content, toolbar))
1147 return priv->button_maxh;
1149 return requisition.height;
1154 slide_idle_handler (gpointer data)
1156 GtkToolbar *toolbar = GTK_TOOLBAR (data);
1157 GtkToolbarPrivate *priv = toolbar->priv;
1160 if (priv->need_sync)
1163 priv->need_sync = FALSE;
1166 for (list = priv->content; list != NULL; list = list->next)
1168 ToolbarContent *content = list->data;
1170 GtkAllocation goal_allocation;
1171 GtkAllocation allocation;
1174 state = toolbar_content_get_state (content);
1175 toolbar_content_get_goal_allocation (content, &goal_allocation);
1176 toolbar_content_get_allocation (content, &allocation);
1180 if (state == NOT_ALLOCATED)
1182 /* an unallocated item means that size allocate has to
1183 * called at least once more
1188 /* An invisible item with a goal allocation of
1189 * 0 is already at its goal.
1191 if ((state == NORMAL || state == OVERFLOWN) &&
1192 ((goal_allocation.width != 0 &&
1193 goal_allocation.height != 0) ||
1194 toolbar_content_child_visible (content)))
1196 if ((goal_allocation.x != allocation.x ||
1197 goal_allocation.y != allocation.y ||
1198 goal_allocation.width != allocation.width ||
1199 goal_allocation.height != allocation.height))
1201 /* An item is not in its right position yet. Note
1202 * that OVERFLOWN items do get an allocation in
1203 * gtk_toolbar_size_allocate(). This way you can see
1204 * them slide back in when you drag an item off the
1211 if (toolbar_content_is_placeholder (content) &&
1212 toolbar_content_disappearing (content) &&
1213 toolbar_content_child_visible (content))
1215 /* A disappearing placeholder is still visible.
1223 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1229 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1231 priv->is_sliding = FALSE;
1238 rect_within (GtkAllocation *a1,
1241 return (a1->x >= a2->x &&
1242 a1->x + a1->width <= a2->x + a2->width &&
1244 a1->y + a1->height <= a2->y + a2->height);
1248 gtk_toolbar_begin_sliding (GtkToolbar *toolbar)
1250 GtkAllocation allocation;
1251 GtkWidget *widget = GTK_WIDGET (toolbar);
1252 GtkToolbarPrivate *priv = toolbar->priv;
1253 GtkStyleContext *context;
1254 GtkStateFlags state;
1263 /* Start the sliding. This function copies the allocation of every
1264 * item into content->start_allocation. For items that haven't
1265 * been allocated yet, we calculate their position and save that
1266 * in start_allocatino along with zero width and zero height.
1268 * FIXME: It would be nice if we could share this code with
1269 * the equivalent in gtk_widget_size_allocate().
1271 priv->is_sliding = TRUE;
1274 priv->idle_id = gdk_threads_add_idle (slide_idle_handler, toolbar);
1276 gtk_widget_get_allocation (widget, &allocation);
1277 context = gtk_widget_get_style_context (widget);
1278 state = gtk_widget_get_state_flags (widget);
1279 gtk_style_context_get_padding (context, state, &padding);
1281 rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1282 vertical = (priv->orientation == GTK_ORIENTATION_VERTICAL);
1283 border_width = get_internal_padding (toolbar) + gtk_container_get_border_width (GTK_CONTAINER (toolbar));
1287 cur_x = allocation.width - border_width - padding.right;
1288 cur_y = allocation.height - border_width - padding.top;
1292 cur_x = border_width + padding.left;
1293 cur_y = border_width + padding.top;
1296 cur_x += allocation.x;
1297 cur_y += allocation.y;
1299 for (list = priv->content; list != NULL; list = list->next)
1301 ToolbarContent *content = list->data;
1302 GtkAllocation new_start_allocation;
1303 GtkAllocation item_allocation;
1306 state = toolbar_content_get_state (content);
1307 toolbar_content_get_allocation (content, &item_allocation);
1309 if ((state == NORMAL &&
1310 rect_within (&item_allocation, &allocation)) ||
1313 new_start_allocation = item_allocation;
1317 new_start_allocation.x = cur_x;
1318 new_start_allocation.y = cur_y;
1322 new_start_allocation.width = allocation.width -
1324 padding.left - padding.right;
1325 new_start_allocation.height = 0;
1329 new_start_allocation.width = 0;
1330 new_start_allocation.height = allocation.height -
1332 padding.top - padding.bottom;
1337 cur_y = new_start_allocation.y + new_start_allocation.height;
1339 cur_x = new_start_allocation.x;
1341 cur_x = new_start_allocation.x + new_start_allocation.width;
1343 toolbar_content_set_start_allocation (content, &new_start_allocation);
1346 /* This resize will run before the first idle handler. This
1347 * will make sure that items get the right goal allocation
1348 * so that the idle handler will not immediately return
1351 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1352 g_timer_reset (priv->timer);
1356 gtk_toolbar_stop_sliding (GtkToolbar *toolbar)
1358 GtkToolbarPrivate *priv = toolbar->priv;
1360 if (priv->is_sliding)
1364 priv->is_sliding = FALSE;
1368 g_source_remove (priv->idle_id);
1372 list = priv->content;
1375 ToolbarContent *content = list->data;
1378 if (toolbar_content_is_placeholder (content))
1380 toolbar_content_remove (content, toolbar);
1381 toolbar_content_free (content);
1385 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1390 remove_item (GtkWidget *menu_item,
1393 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (menu_item)),
1398 menu_deactivated (GtkWidget *menu,
1399 GtkToolbar *toolbar)
1401 GtkToolbarPrivate *priv = toolbar->priv;
1403 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
1407 menu_detached (GtkWidget *widget,
1410 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1411 GtkToolbarPrivate *priv = toolbar->priv;
1417 rebuild_menu (GtkToolbar *toolbar)
1419 GtkToolbarPrivate *priv = toolbar->priv;
1420 GList *list, *children;
1424 priv->menu = GTK_MENU (gtk_menu_new());
1425 gtk_menu_attach_to_widget (priv->menu,
1426 GTK_WIDGET (toolbar),
1429 g_signal_connect (priv->menu, "deactivate",
1430 G_CALLBACK (menu_deactivated), toolbar);
1433 gtk_container_foreach (GTK_CONTAINER (priv->menu), remove_item, NULL);
1435 for (list = priv->content; list != NULL; list = list->next)
1437 ToolbarContent *content = list->data;
1439 if (toolbar_content_get_state (content) == OVERFLOWN &&
1440 !toolbar_content_is_placeholder (content))
1442 GtkWidget *menu_item = toolbar_content_retrieve_menu_item (content);
1446 g_assert (GTK_IS_MENU_ITEM (menu_item));
1447 gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), menu_item);
1452 /* Remove leading and trailing separator items */
1453 children = gtk_container_get_children (GTK_CONTAINER (priv->menu));
1456 while (list && GTK_IS_SEPARATOR_MENU_ITEM (list->data))
1458 GtkWidget *child = list->data;
1460 gtk_container_remove (GTK_CONTAINER (priv->menu), child);
1463 g_list_free (children);
1465 /* Regenerate the list of children so we don't try to remove items twice */
1466 children = gtk_container_get_children (GTK_CONTAINER (priv->menu));
1468 list = g_list_last (children);
1469 while (list && GTK_IS_SEPARATOR_MENU_ITEM (list->data))
1471 GtkWidget *child = list->data;
1473 gtk_container_remove (GTK_CONTAINER (priv->menu), child);
1476 g_list_free (children);
1478 priv->need_rebuild = FALSE;
1482 gtk_toolbar_size_allocate (GtkWidget *widget,
1483 GtkAllocation *allocation)
1485 GtkAllocation widget_allocation;
1486 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1487 GtkToolbarPrivate *priv = toolbar->priv;
1488 GtkAllocation *allocations;
1489 ItemState *new_states;
1490 GtkAllocation arrow_allocation;
1491 GtkStyleContext *context;
1492 GtkStateFlags state;
1495 gint size, pos, short_size;
1498 gboolean need_arrow;
1499 gint n_expand_items;
1501 gint available_size;
1504 GtkRequisition arrow_requisition;
1505 gboolean overflowing;
1506 gboolean size_changed;
1507 GtkAllocation item_area;
1508 GtkShadowType shadow_type;
1510 context = gtk_widget_get_style_context (widget);
1511 state = gtk_widget_get_state_flags (widget);
1512 gtk_style_context_get_padding (context, state, &padding);
1514 gtk_widget_get_allocation (widget, &widget_allocation);
1515 size_changed = FALSE;
1516 if (widget_allocation.x != allocation->x ||
1517 widget_allocation.y != allocation->y ||
1518 widget_allocation.width != allocation->width ||
1519 widget_allocation.height != allocation->height)
1521 size_changed = TRUE;
1525 gtk_toolbar_stop_sliding (toolbar);
1527 gtk_widget_set_allocation (widget, allocation);
1529 border_width = gtk_container_get_border_width (GTK_CONTAINER (toolbar));
1531 if (gtk_widget_get_realized (widget))
1532 gdk_window_move_resize (priv->event_window,
1533 allocation->x + border_width,
1534 allocation->y + border_width,
1535 allocation->width - border_width * 2,
1536 allocation->height - border_width * 2);
1538 border_width += get_internal_padding (toolbar);
1540 gtk_widget_get_preferred_size (priv->arrow_button,
1541 &arrow_requisition, NULL);
1543 shadow_type = get_shadow_type (toolbar);
1545 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1547 available_size = size = allocation->width - 2 * border_width;
1548 short_size = allocation->height - 2 * border_width;
1549 arrow_size = arrow_requisition.width;
1551 if (shadow_type != GTK_SHADOW_NONE)
1553 available_size -= padding.left + padding.right;
1554 short_size -= padding.top + padding.bottom;
1559 available_size = size = allocation->height - 2 * border_width;
1560 short_size = allocation->width - 2 * border_width;
1561 arrow_size = arrow_requisition.height;
1563 if (shadow_type != GTK_SHADOW_NONE)
1565 available_size -= padding.top + padding.bottom;
1566 short_size -= padding.left + padding.right;
1570 n_items = g_list_length (priv->content);
1571 allocations = g_new0 (GtkAllocation, n_items);
1572 new_states = g_new0 (ItemState, n_items);
1576 for (list = priv->content; list != NULL; list = list->next)
1578 ToolbarContent *content = list->data;
1580 if (toolbar_content_visible (content, toolbar))
1582 needed_size += get_item_size (toolbar, content);
1584 /* Do we need an arrow?
1586 * Assume we don't, and see if any non-separator item
1587 * with a proxy menu item is then going to overflow.
1589 if (needed_size > available_size &&
1592 toolbar_content_has_proxy_menu_item (content) &&
1593 !toolbar_content_is_separator (content))
1601 size = available_size - arrow_size;
1603 size = available_size;
1605 /* calculate widths and states of items */
1606 overflowing = FALSE;
1607 for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1609 ToolbarContent *content = list->data;
1612 if (!toolbar_content_visible (content, toolbar))
1614 new_states[i] = HIDDEN;
1618 item_size = get_item_size (toolbar, content);
1619 if (item_size <= size && !overflowing)
1622 allocations[i].width = item_size;
1623 new_states[i] = NORMAL;
1628 new_states[i] = OVERFLOWN;
1629 allocations[i].width = item_size;
1633 /* calculate width of arrow */
1636 arrow_allocation.width = arrow_size;
1637 arrow_allocation.height = MAX (short_size, 1);
1640 /* expand expandable items */
1642 /* We don't expand when there is an overflow menu,
1643 * because that leads to weird jumps when items get
1644 * moved to the overflow menu and the expanding
1645 * items suddenly get a lot of extra space
1649 gint max_child_expand;
1652 for (i = 0, list = priv->content; list != NULL; list = list->next, ++i)
1654 ToolbarContent *content = list->data;
1656 if (toolbar_content_get_expand (content) && new_states[i] == NORMAL)
1660 max_child_expand = get_max_child_expand (toolbar);
1661 for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1663 ToolbarContent *content = list->data;
1665 if (toolbar_content_get_expand (content) && new_states[i] == NORMAL)
1667 gint extra = size / n_expand_items;
1668 if (size % n_expand_items != 0)
1671 if (extra > max_child_expand)
1672 extra = max_child_expand;
1674 allocations[i].width += extra;
1680 g_assert (n_expand_items == 0);
1683 /* position items */
1685 for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1687 /* Both NORMAL and OVERFLOWN items get a position.
1688 * This ensures that sliding will work for OVERFLOWN items too.
1690 if (new_states[i] == NORMAL || new_states[i] == OVERFLOWN)
1692 allocations[i].x = pos;
1693 allocations[i].y = border_width;
1694 allocations[i].height = short_size;
1696 pos += allocations[i].width;
1700 /* position arrow */
1703 arrow_allocation.x = available_size - border_width - arrow_allocation.width;
1704 arrow_allocation.y = border_width;
1707 item_area.x = border_width;
1708 item_area.y = border_width;
1709 item_area.width = available_size - (need_arrow? arrow_size : 0);
1710 item_area.height = short_size;
1712 /* fix up allocations in the vertical or RTL cases */
1713 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1715 for (i = 0; i < n_items; ++i)
1716 fixup_allocation_for_vertical (&(allocations[i]));
1719 fixup_allocation_for_vertical (&arrow_allocation);
1721 fixup_allocation_for_vertical (&item_area);
1723 else if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
1725 for (i = 0; i < n_items; ++i)
1726 fixup_allocation_for_rtl (available_size, &(allocations[i]));
1729 fixup_allocation_for_rtl (available_size, &arrow_allocation);
1731 fixup_allocation_for_rtl (available_size, &item_area);
1734 /* translate the items by allocation->(x,y) */
1735 for (i = 0; i < n_items; ++i)
1737 allocations[i].x += allocation->x;
1738 allocations[i].y += allocation->y;
1740 if (shadow_type != GTK_SHADOW_NONE)
1742 allocations[i].x += padding.left;
1743 allocations[i].y += padding.top;
1749 arrow_allocation.x += allocation->x;
1750 arrow_allocation.y += allocation->y;
1752 if (shadow_type != GTK_SHADOW_NONE)
1754 arrow_allocation.x += padding.left;
1755 arrow_allocation.y += padding.top;
1759 item_area.x += allocation->x;
1760 item_area.y += allocation->y;
1761 if (shadow_type != GTK_SHADOW_NONE)
1763 item_area.x += padding.left;
1764 item_area.y += padding.top;
1767 /* did anything change? */
1768 for (list = priv->content, i = 0; list != NULL; list = list->next, i++)
1770 ToolbarContent *content = list->data;
1772 if (toolbar_content_get_state (content) == NORMAL &&
1773 new_states[i] != NORMAL)
1775 /* an item disappeared and we didn't change size, so begin sliding */
1777 gtk_toolbar_begin_sliding (toolbar);
1781 /* finally allocate the items */
1782 if (priv->is_sliding)
1784 for (list = priv->content, i = 0; list != NULL; list = list->next, i++)
1786 ToolbarContent *content = list->data;
1788 toolbar_content_set_goal_allocation (content, &(allocations[i]));
1792 for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1794 ToolbarContent *content = list->data;
1796 if (new_states[i] == OVERFLOWN || new_states[i] == NORMAL)
1798 GtkAllocation alloc;
1799 GtkAllocation start_allocation = { 0, };
1800 GtkAllocation goal_allocation;
1802 if (priv->is_sliding)
1804 toolbar_content_get_start_allocation (content, &start_allocation);
1805 toolbar_content_get_goal_allocation (content, &goal_allocation);
1807 compute_intermediate_allocation (toolbar,
1812 priv->need_sync = TRUE;
1816 alloc = allocations[i];
1819 if (alloc.width <= 0 || alloc.height <= 0)
1821 toolbar_content_set_child_visible (content, toolbar, FALSE);
1825 if (!rect_within (&alloc, &item_area))
1827 toolbar_content_set_child_visible (content, toolbar, FALSE);
1828 toolbar_content_size_allocate (content, &alloc);
1832 toolbar_content_set_child_visible (content, toolbar, TRUE);
1833 toolbar_content_size_allocate (content, &alloc);
1839 toolbar_content_set_child_visible (content, toolbar, FALSE);
1842 toolbar_content_set_state (content, new_states[i]);
1845 if (priv->menu && priv->need_rebuild)
1846 rebuild_menu (toolbar);
1850 gtk_widget_size_allocate (GTK_WIDGET (priv->arrow_button),
1852 gtk_widget_show (GTK_WIDGET (priv->arrow_button));
1856 gtk_widget_hide (GTK_WIDGET (priv->arrow_button));
1858 if (priv->menu && gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
1859 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
1862 g_free (allocations);
1863 g_free (new_states);
1867 gtk_toolbar_update_button_relief (GtkToolbar *toolbar)
1869 GtkToolbarPrivate *priv = toolbar->priv;
1870 GtkReliefStyle relief;
1872 relief = get_button_relief (toolbar);
1874 if (relief != gtk_button_get_relief (GTK_BUTTON (priv->arrow_button)))
1876 gtk_toolbar_reconfigured (toolbar);
1878 gtk_button_set_relief (GTK_BUTTON (priv->arrow_button), relief);
1883 gtk_toolbar_style_updated (GtkWidget *widget)
1885 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1886 GtkToolbarPrivate *priv = toolbar->priv;
1888 GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->style_updated (widget);
1890 priv->max_homogeneous_pixels = -1;
1892 if (gtk_widget_get_realized (widget))
1893 gtk_style_context_set_background (gtk_widget_get_style_context (widget),
1894 gtk_widget_get_window (widget));
1896 gtk_toolbar_update_button_relief (GTK_TOOLBAR (widget));
1900 gtk_toolbar_list_children_in_focus_order (GtkToolbar *toolbar,
1901 GtkDirectionType dir)
1903 GtkToolbarPrivate *priv = toolbar->priv;
1904 GList *result = NULL;
1908 /* generate list of children in reverse logical order */
1910 for (list = priv->content; list != NULL; list = list->next)
1912 ToolbarContent *content = list->data;
1915 widget = toolbar_content_get_widget (content);
1918 result = g_list_prepend (result, widget);
1921 result = g_list_prepend (result, priv->arrow_button);
1923 rtl = (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL);
1925 /* move in logical order when
1927 * - dir is TAB_FORWARD
1929 * - in RTL mode and moving left or up
1931 * - in LTR mode and moving right or down
1933 if (dir == GTK_DIR_TAB_FORWARD ||
1934 (rtl && (dir == GTK_DIR_UP || dir == GTK_DIR_LEFT)) ||
1935 (!rtl && (dir == GTK_DIR_DOWN || dir == GTK_DIR_RIGHT)))
1937 result = g_list_reverse (result);
1944 gtk_toolbar_focus_home_or_end (GtkToolbar *toolbar,
1945 gboolean focus_home)
1947 GList *children, *list;
1948 GtkDirectionType dir = focus_home? GTK_DIR_RIGHT : GTK_DIR_LEFT;
1950 children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);
1952 if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
1954 children = g_list_reverse (children);
1956 dir = (dir == GTK_DIR_RIGHT)? GTK_DIR_LEFT : GTK_DIR_RIGHT;
1959 for (list = children; list != NULL; list = list->next)
1961 GtkWidget *child = list->data;
1963 if (gtk_container_get_focus_child (GTK_CONTAINER (toolbar)) == child)
1966 if (gtk_widget_get_mapped (child) && gtk_widget_child_focus (child, dir))
1970 g_list_free (children);
1975 /* Keybinding handler. This function is called when the user presses
1976 * Ctrl TAB or an arrow key.
1979 gtk_toolbar_move_focus (GtkWidget *widget,
1980 GtkDirectionType dir)
1982 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1983 GtkContainer *container = GTK_CONTAINER (toolbar);
1984 GtkWidget *focus_child;
1986 gboolean try_focus = FALSE;
1989 focus_child = gtk_container_get_focus_child (container);
1991 if (focus_child && gtk_widget_child_focus (focus_child, dir))
1994 children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);
1996 for (list = children; list != NULL; list = list->next)
1998 GtkWidget *child = list->data;
2000 if (try_focus && gtk_widget_get_mapped (child) && gtk_widget_child_focus (child, dir))
2003 if (child == focus_child)
2007 g_list_free (children);
2010 /* The focus handler for the toolbar. It called when the user presses
2011 * TAB or otherwise tries to focus the toolbar.
2014 gtk_toolbar_focus (GtkWidget *widget,
2015 GtkDirectionType dir)
2017 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
2018 GList *children, *list;
2019 gboolean result = FALSE;
2021 /* if focus is already somewhere inside the toolbar then return FALSE.
2022 * The only way focus can stay inside the toolbar is when the user presses
2023 * arrow keys or Ctrl TAB (both of which are handled by the
2024 * gtk_toolbar_move_focus() keybinding function.
2026 if (gtk_container_get_focus_child (GTK_CONTAINER (widget)))
2029 children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);
2031 for (list = children; list != NULL; list = list->next)
2033 GtkWidget *child = list->data;
2035 if (gtk_widget_get_mapped (child) && gtk_widget_child_focus (child, dir))
2042 g_list_free (children);
2047 static GtkSettings *
2048 toolbar_get_settings (GtkToolbar *toolbar)
2050 return toolbar->priv->settings;
2054 style_change_notify (GtkToolbar *toolbar)
2056 GtkToolbarPrivate *priv = toolbar->priv;
2058 if (!priv->style_set)
2060 /* pretend it was set, then unset, thus reverting to new default */
2061 priv->style_set = TRUE;
2062 gtk_toolbar_unset_style (toolbar);
2067 icon_size_change_notify (GtkToolbar *toolbar)
2069 GtkToolbarPrivate *priv = toolbar->priv;
2071 if (!priv->icon_size_set)
2073 /* pretend it was set, then unset, thus reverting to new default */
2074 priv->icon_size_set = TRUE;
2075 gtk_toolbar_unset_icon_size (toolbar);
2080 animation_change_notify (GtkToolbar *toolbar)
2082 GtkToolbarPrivate *priv = toolbar->priv;
2083 GtkSettings *settings = toolbar_get_settings (toolbar);
2087 g_object_get (settings,
2088 "gtk-enable-animations", &animation,
2091 animation = DEFAULT_ANIMATION_STATE;
2093 priv->animation = animation;
2097 settings_change_notify (GtkSettings *settings,
2098 const GParamSpec *pspec,
2099 GtkToolbar *toolbar)
2101 if (! strcmp (pspec->name, "gtk-toolbar-style"))
2102 style_change_notify (toolbar);
2103 else if (! strcmp (pspec->name, "gtk-toolbar-icon-size"))
2104 icon_size_change_notify (toolbar);
2105 else if (! strcmp (pspec->name, "gtk-enable-animations"))
2106 animation_change_notify (toolbar);
2110 gtk_toolbar_screen_changed (GtkWidget *widget,
2111 GdkScreen *previous_screen)
2113 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
2114 GtkToolbarPrivate *priv = toolbar->priv;
2115 GtkSettings *old_settings = toolbar_get_settings (toolbar);
2116 GtkSettings *settings;
2118 if (gtk_widget_has_screen (GTK_WIDGET (toolbar)))
2119 settings = gtk_widget_get_settings (GTK_WIDGET (toolbar));
2123 if (settings == old_settings)
2128 g_signal_handler_disconnect (old_settings, priv->settings_connection);
2130 g_object_unref (old_settings);
2135 priv->settings_connection =
2136 g_signal_connect (settings, "notify",
2137 G_CALLBACK (settings_change_notify),
2140 priv->settings = g_object_ref (settings);
2143 priv->settings = NULL;
2145 style_change_notify (toolbar);
2146 icon_size_change_notify (toolbar);
2147 animation_change_notify (toolbar);
2151 find_drop_index (GtkToolbar *toolbar,
2155 GtkToolbarPrivate *priv = toolbar->priv;
2156 GList *interesting_content;
2158 GtkOrientation orientation;
2159 GtkTextDirection direction;
2160 gint best_distance = G_MAXINT;
2164 ToolbarContent *best_content;
2165 GtkAllocation allocation;
2167 /* list items we care about wrt. drag and drop */
2168 interesting_content = NULL;
2169 for (list = priv->content; list != NULL; list = list->next)
2171 ToolbarContent *content = list->data;
2173 if (toolbar_content_get_state (content) == NORMAL)
2174 interesting_content = g_list_prepend (interesting_content, content);
2176 interesting_content = g_list_reverse (interesting_content);
2178 if (!interesting_content)
2181 orientation = priv->orientation;
2182 direction = gtk_widget_get_direction (GTK_WIDGET (toolbar));
2184 /* distance to first interesting item */
2185 best_content = interesting_content->data;
2186 toolbar_content_get_allocation (best_content, &allocation);
2188 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2192 if (direction == GTK_TEXT_DIR_LTR)
2195 pos = allocation.x + allocation.width;
2203 best_content = NULL;
2204 best_distance = ABS (pos - cursor);
2206 /* distance to far end of each item */
2207 for (list = interesting_content; list != NULL; list = list->next)
2209 ToolbarContent *content = list->data;
2211 toolbar_content_get_allocation (content, &allocation);
2213 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2215 if (direction == GTK_TEXT_DIR_LTR)
2216 pos = allocation.x + allocation.width;
2222 pos = allocation.y + allocation.height;
2225 distance = ABS (pos - cursor);
2227 if (distance < best_distance)
2229 best_distance = distance;
2230 best_content = content;
2234 g_list_free (interesting_content);
2239 return g_list_index (priv->content, best_content) + 1;
2243 reset_all_placeholders (GtkToolbar *toolbar)
2245 GtkToolbarPrivate *priv = toolbar->priv;
2248 for (list = priv->content; list != NULL; list = list->next)
2250 ToolbarContent *content = list->data;
2251 if (toolbar_content_is_placeholder (content))
2252 toolbar_content_set_disappearing (content, TRUE);
2257 physical_to_logical (GtkToolbar *toolbar,
2260 GtkToolbarPrivate *priv = toolbar->priv;
2264 g_assert (physical >= 0);
2267 for (list = priv->content; list && physical > 0; list = list->next)
2269 ToolbarContent *content = list->data;
2271 if (!toolbar_content_is_placeholder (content))
2276 g_assert (physical == 0);
2282 logical_to_physical (GtkToolbar *toolbar,
2285 GtkToolbarPrivate *priv = toolbar->priv;
2289 g_assert (logical >= 0);
2292 for (list = priv->content; list; list = list->next)
2294 ToolbarContent *content = list->data;
2296 if (!toolbar_content_is_placeholder (content))
2306 g_assert (logical == 0);
2312 * gtk_toolbar_set_drop_highlight_item:
2313 * @toolbar: a #GtkToolbar
2314 * @tool_item: (allow-none): a #GtkToolItem, or %NULL to turn of highlighting
2315 * @index_: a position on @toolbar
2317 * Highlights @toolbar to give an idea of what it would look like
2318 * if @item was added to @toolbar at the position indicated by @index_.
2319 * If @item is %NULL, highlighting is turned off. In that case @index_
2322 * The @tool_item passed to this function must not be part of any widget
2323 * hierarchy. When an item is set as drop highlight item it can not
2324 * added to any widget hierarchy or used as highlight item for another
2330 gtk_toolbar_set_drop_highlight_item (GtkToolbar *toolbar,
2331 GtkToolItem *tool_item,
2334 ToolbarContent *content;
2335 GtkToolbarPrivate *priv;
2337 GtkRequisition requisition;
2338 GtkRequisition old_requisition;
2339 gboolean restart_sliding;
2341 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2342 g_return_if_fail (tool_item == NULL || GTK_IS_TOOL_ITEM (tool_item));
2344 priv = toolbar->priv;
2348 if (priv->highlight_tool_item)
2350 gtk_widget_unparent (GTK_WIDGET (priv->highlight_tool_item));
2351 g_object_unref (priv->highlight_tool_item);
2352 priv->highlight_tool_item = NULL;
2355 reset_all_placeholders (toolbar);
2356 gtk_toolbar_begin_sliding (toolbar);
2360 n_items = gtk_toolbar_get_n_items (toolbar);
2361 if (index_ < 0 || index_ > n_items)
2364 if (tool_item != priv->highlight_tool_item)
2366 if (priv->highlight_tool_item)
2367 g_object_unref (priv->highlight_tool_item);
2369 g_object_ref_sink (tool_item);
2371 priv->highlight_tool_item = tool_item;
2373 gtk_widget_set_parent (GTK_WIDGET (priv->highlight_tool_item),
2374 GTK_WIDGET (toolbar));
2377 index_ = logical_to_physical (toolbar, index_);
2379 content = g_list_nth_data (priv->content, index_);
2383 ToolbarContent *prev_content;
2385 prev_content = g_list_nth_data (priv->content, index_ - 1);
2387 if (prev_content && toolbar_content_is_placeholder (prev_content))
2388 content = prev_content;
2391 if (!content || !toolbar_content_is_placeholder (content))
2393 GtkWidget *placeholder;
2395 placeholder = GTK_WIDGET (gtk_separator_tool_item_new ());
2397 content = toolbar_content_new_tool_item (toolbar,
2398 GTK_TOOL_ITEM (placeholder),
2400 gtk_widget_show (placeholder);
2404 g_assert (toolbar_content_is_placeholder (content));
2406 gtk_widget_get_preferred_size (GTK_WIDGET (priv->highlight_tool_item),
2407 &requisition, NULL);
2409 toolbar_content_set_expand (content, gtk_tool_item_get_expand (tool_item));
2411 restart_sliding = FALSE;
2412 toolbar_content_size_request (content, toolbar, &old_requisition);
2413 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2415 requisition.height = -1;
2416 if (requisition.width != old_requisition.width)
2417 restart_sliding = TRUE;
2421 requisition.width = -1;
2422 if (requisition.height != old_requisition.height)
2423 restart_sliding = TRUE;
2426 if (toolbar_content_disappearing (content))
2427 restart_sliding = TRUE;
2429 reset_all_placeholders (toolbar);
2430 toolbar_content_set_disappearing (content, FALSE);
2432 toolbar_content_set_size_request (content,
2433 requisition.width, requisition.height);
2435 if (restart_sliding)
2436 gtk_toolbar_begin_sliding (toolbar);
2440 gtk_toolbar_get_child_property (GtkContainer *container,
2446 GtkToolItem *item = GTK_TOOL_ITEM (child);
2448 switch (property_id)
2450 case CHILD_PROP_HOMOGENEOUS:
2451 g_value_set_boolean (value, gtk_tool_item_get_homogeneous (item));
2454 case CHILD_PROP_EXPAND:
2455 g_value_set_boolean (value, gtk_tool_item_get_expand (item));
2459 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
2465 gtk_toolbar_set_child_property (GtkContainer *container,
2468 const GValue *value,
2471 switch (property_id)
2473 case CHILD_PROP_HOMOGENEOUS:
2474 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (child), g_value_get_boolean (value));
2477 case CHILD_PROP_EXPAND:
2478 gtk_tool_item_set_expand (GTK_TOOL_ITEM (child), g_value_get_boolean (value));
2482 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
2488 gtk_toolbar_show_all (GtkWidget *widget)
2490 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
2491 GtkToolbarPrivate *priv = toolbar->priv;
2494 for (list = priv->content; list != NULL; list = list->next)
2496 ToolbarContent *content = list->data;
2498 toolbar_content_show_all (content);
2501 gtk_widget_show (widget);
2505 gtk_toolbar_add (GtkContainer *container,
2508 GtkToolbar *toolbar = GTK_TOOLBAR (container);
2510 gtk_toolbar_insert (toolbar, GTK_TOOL_ITEM (widget), -1);
2514 gtk_toolbar_remove (GtkContainer *container,
2517 GtkToolbar *toolbar = GTK_TOOLBAR (container);
2518 GtkToolbarPrivate *priv = toolbar->priv;
2519 ToolbarContent *content_to_remove;
2522 content_to_remove = NULL;
2523 for (list = priv->content; list != NULL; list = list->next)
2525 ToolbarContent *content = list->data;
2528 child = toolbar_content_get_widget (content);
2529 if (child && child == widget)
2531 content_to_remove = content;
2536 g_return_if_fail (content_to_remove != NULL);
2538 toolbar_content_remove (content_to_remove, toolbar);
2539 toolbar_content_free (content_to_remove);
2543 gtk_toolbar_forall (GtkContainer *container,
2544 gboolean include_internals,
2545 GtkCallback callback,
2546 gpointer callback_data)
2548 GtkToolbar *toolbar = GTK_TOOLBAR (container);
2549 GtkToolbarPrivate *priv = toolbar->priv;
2552 g_return_if_fail (callback != NULL);
2554 list = priv->content;
2557 ToolbarContent *content = list->data;
2558 GList *next = list->next;
2560 if (include_internals || !toolbar_content_is_placeholder (content))
2562 GtkWidget *child = toolbar_content_get_widget (content);
2565 callback (child, callback_data);
2571 if (include_internals && priv->arrow_button)
2572 callback (priv->arrow_button, callback_data);
2576 gtk_toolbar_child_type (GtkContainer *container)
2578 return GTK_TYPE_TOOL_ITEM;
2582 gtk_toolbar_reconfigured (GtkToolbar *toolbar)
2584 GtkToolbarPrivate *priv = toolbar->priv;
2587 list = priv->content;
2590 ToolbarContent *content = list->data;
2591 GList *next = list->next;
2593 toolbar_content_toolbar_reconfigured (content, toolbar);
2600 gtk_toolbar_orientation_changed (GtkToolbar *toolbar,
2601 GtkOrientation orientation)
2603 GtkToolbarPrivate *priv = toolbar->priv;
2605 if (priv->orientation != orientation)
2607 priv->orientation = orientation;
2609 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2610 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2612 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
2614 gtk_toolbar_reconfigured (toolbar);
2616 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
2617 g_object_notify (G_OBJECT (toolbar), "orientation");
2622 gtk_toolbar_real_style_changed (GtkToolbar *toolbar,
2623 GtkToolbarStyle style)
2625 GtkToolbarPrivate *priv = toolbar->priv;
2627 if (priv->style != style)
2629 priv->style = style;
2631 gtk_toolbar_reconfigured (toolbar);
2633 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
2634 g_object_notify (G_OBJECT (toolbar), "toolbar-style");
2639 menu_position_func (GtkMenu *menu,
2645 GtkAllocation allocation;
2646 GtkToolbar *toolbar = GTK_TOOLBAR (user_data);
2647 GtkToolbarPrivate *priv = toolbar->priv;
2649 GtkRequisition menu_req;
2650 GdkRectangle monitor;
2654 gtk_widget_get_preferred_size (priv->arrow_button,
2656 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
2659 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
2660 monitor_num = gdk_screen_get_monitor_at_window (screen,
2661 gtk_widget_get_window (priv->arrow_button));
2662 if (monitor_num < 0)
2664 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2666 gtk_widget_get_allocation (priv->arrow_button, &allocation);
2668 gdk_window_get_origin (gtk_button_get_event_window (GTK_BUTTON (priv->arrow_button)), x, y);
2669 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2671 if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_LTR)
2672 *x += allocation.width - req.width;
2674 *x += req.width - menu_req.width;
2676 if ((*y + allocation.height + menu_req.height) <= monitor.y + monitor.height)
2677 *y += allocation.height;
2678 else if ((*y - menu_req.height) >= monitor.y)
2679 *y -= menu_req.height;
2680 else if (monitor.y + monitor.height - (*y + allocation.height) > *y)
2681 *y += allocation.height;
2683 *y -= menu_req.height;
2687 if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_LTR)
2688 *x += allocation.width;
2690 *x -= menu_req.width;
2692 if (*y + menu_req.height > monitor.y + monitor.height &&
2693 *y + allocation.height - monitor.y > monitor.y + monitor.height - *y)
2694 *y += allocation.height - menu_req.height;
2701 show_menu (GtkToolbar *toolbar,
2702 GdkEventButton *event)
2704 GtkToolbarPrivate *priv = toolbar->priv;
2706 rebuild_menu (toolbar);
2708 gtk_widget_show_all (GTK_WIDGET (priv->menu));
2710 gtk_menu_popup (priv->menu, NULL, NULL,
2711 menu_position_func, toolbar,
2712 event? event->button : 0,
2713 event? event->time : gtk_get_current_event_time());
2717 gtk_toolbar_arrow_button_clicked (GtkWidget *button,
2718 GtkToolbar *toolbar)
2720 GtkToolbarPrivate *priv = toolbar->priv;
2722 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->arrow_button)) &&
2723 (!priv->menu || !gtk_widget_get_visible (GTK_WIDGET (priv->menu))))
2725 /* We only get here when the button is clicked with the keyboard,
2726 * because mouse button presses result in the menu being shown so
2727 * that priv->menu would be non-NULL and visible.
2729 show_menu (toolbar, NULL);
2730 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
2735 gtk_toolbar_arrow_button_press (GtkWidget *button,
2736 GdkEventButton *event,
2737 GtkToolbar *toolbar)
2739 show_menu (toolbar, event);
2740 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
2746 gtk_toolbar_button_press (GtkWidget *toolbar,
2747 GdkEventButton *event)
2751 if (_gtk_button_event_triggers_context_menu (event))
2753 gboolean return_value;
2755 g_signal_emit (toolbar, toolbar_signals[POPUP_CONTEXT_MENU], 0,
2756 (int)event->x_root, (int)event->y_root, event->button,
2759 return return_value;
2762 window = gtk_widget_get_toplevel (toolbar);
2766 gboolean window_drag = FALSE;
2768 gtk_widget_style_get (toolbar,
2769 "window-dragging", &window_drag,
2774 gtk_window_begin_move_drag (GTK_WINDOW (window),
2788 gtk_toolbar_popup_menu (GtkWidget *toolbar)
2790 gboolean return_value;
2791 /* This function is the handler for the "popup menu" keybinding,
2792 * ie., it is called when the user presses Shift F10
2794 g_signal_emit (toolbar, toolbar_signals[POPUP_CONTEXT_MENU], 0,
2795 -1, -1, -1, &return_value);
2797 return return_value;
2803 * Creates a new toolbar.
2805 * Return Value: the newly-created toolbar.
2808 gtk_toolbar_new (void)
2810 GtkToolbar *toolbar;
2812 toolbar = g_object_new (GTK_TYPE_TOOLBAR, NULL);
2814 return GTK_WIDGET (toolbar);
2818 * gtk_toolbar_insert:
2819 * @toolbar: a #GtkToolbar
2820 * @item: a #GtkToolItem
2821 * @pos: the position of the new item
2823 * Insert a #GtkToolItem into the toolbar at position @pos. If @pos is
2824 * 0 the item is prepended to the start of the toolbar. If @pos is
2825 * negative, the item is appended to the end of the toolbar.
2830 gtk_toolbar_insert (GtkToolbar *toolbar,
2834 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2835 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2838 pos = logical_to_physical (toolbar, pos);
2840 toolbar_content_new_tool_item (toolbar, item, FALSE, pos);
2844 * gtk_toolbar_get_item_index:
2845 * @toolbar: a #GtkToolbar
2846 * @item: a #GtkToolItem that is a child of @toolbar
2848 * Returns the position of @item on the toolbar, starting from 0.
2849 * It is an error if @item is not a child of the toolbar.
2851 * Return value: the position of item on the toolbar.
2856 gtk_toolbar_get_item_index (GtkToolbar *toolbar,
2859 GtkToolbarPrivate *priv;
2863 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), -1);
2864 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2865 g_return_val_if_fail (gtk_widget_get_parent (GTK_WIDGET (item)) == GTK_WIDGET (toolbar), -1);
2867 priv = toolbar->priv;
2870 for (list = priv->content; list != NULL; list = list->next)
2872 ToolbarContent *content = list->data;
2875 widget = toolbar_content_get_widget (content);
2877 if (item == GTK_TOOL_ITEM (widget))
2883 return physical_to_logical (toolbar, n);
2887 * gtk_toolbar_set_style:
2888 * @toolbar: a #GtkToolbar.
2889 * @style: the new style for @toolbar.
2891 * Alters the view of @toolbar to display either icons only, text only, or both.
2894 gtk_toolbar_set_style (GtkToolbar *toolbar,
2895 GtkToolbarStyle style)
2897 GtkToolbarPrivate *priv;
2899 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2901 priv = toolbar->priv;
2903 priv->style_set = TRUE;
2904 g_signal_emit (toolbar, toolbar_signals[STYLE_CHANGED], 0, style);
2908 * gtk_toolbar_get_style:
2909 * @toolbar: a #GtkToolbar
2911 * Retrieves whether the toolbar has text, icons, or both . See
2912 * gtk_toolbar_set_style().
2914 * Return value: the current style of @toolbar
2917 gtk_toolbar_get_style (GtkToolbar *toolbar)
2919 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), DEFAULT_TOOLBAR_STYLE);
2921 return toolbar->priv->style;
2925 * gtk_toolbar_unset_style:
2926 * @toolbar: a #GtkToolbar
2928 * Unsets a toolbar style set with gtk_toolbar_set_style(), so that
2929 * user preferences will be used to determine the toolbar style.
2932 gtk_toolbar_unset_style (GtkToolbar *toolbar)
2934 GtkToolbarPrivate *priv;
2935 GtkToolbarStyle style;
2937 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2939 priv = toolbar->priv;
2941 if (priv->style_set)
2943 GtkSettings *settings = toolbar_get_settings (toolbar);
2946 g_object_get (settings,
2947 "gtk-toolbar-style", &style,
2950 style = DEFAULT_TOOLBAR_STYLE;
2952 if (style != priv->style)
2953 g_signal_emit (toolbar, toolbar_signals[STYLE_CHANGED], 0, style);
2955 priv->style_set = FALSE;
2960 * gtk_toolbar_get_n_items:
2961 * @toolbar: a #GtkToolbar
2963 * Returns the number of items on the toolbar.
2965 * Return value: the number of items on the toolbar
2970 gtk_toolbar_get_n_items (GtkToolbar *toolbar)
2972 GtkToolbarPrivate *priv;
2974 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), -1);
2976 priv = toolbar->priv;
2978 return physical_to_logical (toolbar, g_list_length (priv->content));
2982 * gtk_toolbar_get_nth_item:
2983 * @toolbar: a #GtkToolbar
2984 * @n: A position on the toolbar
2986 * Returns the @n<!-- -->'th item on @toolbar, or %NULL if the
2987 * toolbar does not contain an @n<!-- -->'th item.
2989 * Return value: (transfer none): The @n<!-- -->'th #GtkToolItem on @toolbar,
2990 * or %NULL if there isn't an @n<!-- -->'th item.
2995 gtk_toolbar_get_nth_item (GtkToolbar *toolbar,
2998 GtkToolbarPrivate *priv;
2999 ToolbarContent *content;
3002 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), NULL);
3004 priv = toolbar->priv;
3006 n_items = gtk_toolbar_get_n_items (toolbar);
3008 if (n < 0 || n >= n_items)
3011 content = g_list_nth_data (priv->content, logical_to_physical (toolbar, n));
3014 g_assert (!toolbar_content_is_placeholder (content));
3016 return GTK_TOOL_ITEM (toolbar_content_get_widget (content));
3020 * gtk_toolbar_get_icon_size:
3021 * @toolbar: a #GtkToolbar
3023 * Retrieves the icon size for the toolbar. See gtk_toolbar_set_icon_size().
3025 * Return value: (type int): the current icon size for the icons on
3029 gtk_toolbar_get_icon_size (GtkToolbar *toolbar)
3031 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), DEFAULT_ICON_SIZE);
3033 return toolbar->priv->icon_size;
3037 * gtk_toolbar_get_relief_style:
3038 * @toolbar: a #GtkToolbar
3040 * Returns the relief style of buttons on @toolbar. See
3041 * gtk_button_set_relief().
3043 * Return value: The relief style of buttons on @toolbar.
3048 gtk_toolbar_get_relief_style (GtkToolbar *toolbar)
3050 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), GTK_RELIEF_NONE);
3052 return get_button_relief (toolbar);
3056 * gtk_toolbar_set_show_arrow:
3057 * @toolbar: a #GtkToolbar
3058 * @show_arrow: Whether to show an overflow menu
3060 * Sets whether to show an overflow menu when
3061 * @toolbar doesn't have room for all items on it. If %TRUE,
3062 * items that there are not room are available through an
3068 gtk_toolbar_set_show_arrow (GtkToolbar *toolbar,
3069 gboolean show_arrow)
3071 GtkToolbarPrivate *priv;
3073 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3075 priv = toolbar->priv;
3077 show_arrow = show_arrow != FALSE;
3079 if (priv->show_arrow != show_arrow)
3081 priv->show_arrow = show_arrow;
3083 if (!priv->show_arrow)
3084 gtk_widget_hide (priv->arrow_button);
3086 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3087 g_object_notify (G_OBJECT (toolbar), "show-arrow");
3092 * gtk_toolbar_get_show_arrow:
3093 * @toolbar: a #GtkToolbar
3095 * Returns whether the toolbar has an overflow menu.
3096 * See gtk_toolbar_set_show_arrow().
3098 * Return value: %TRUE if the toolbar has an overflow menu.
3103 gtk_toolbar_get_show_arrow (GtkToolbar *toolbar)
3105 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
3107 return toolbar->priv->show_arrow;
3111 * gtk_toolbar_get_drop_index:
3112 * @toolbar: a #GtkToolbar
3113 * @x: x coordinate of a point on the toolbar
3114 * @y: y coordinate of a point on the toolbar
3116 * Returns the position corresponding to the indicated point on
3117 * @toolbar. This is useful when dragging items to the toolbar:
3118 * this function returns the position a new item should be
3121 * @x and @y are in @toolbar coordinates.
3123 * Return value: The position corresponding to the point (@x, @y) on the toolbar.
3128 gtk_toolbar_get_drop_index (GtkToolbar *toolbar,
3132 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), -1);
3134 return physical_to_logical (toolbar, find_drop_index (toolbar, x, y));
3138 gtk_toolbar_dispose (GObject *object)
3140 GtkToolbar *toolbar = GTK_TOOLBAR (object);
3141 GtkToolbarPrivate *priv = toolbar->priv;
3143 if (priv->arrow_button)
3145 gtk_widget_unparent (priv->arrow_button);
3146 priv->arrow_button = NULL;
3151 g_signal_handlers_disconnect_by_func (priv->menu,
3152 menu_deactivated, toolbar);
3153 gtk_widget_destroy (GTK_WIDGET (priv->menu));
3157 G_OBJECT_CLASS (gtk_toolbar_parent_class)->dispose (object);
3161 gtk_toolbar_finalize (GObject *object)
3163 GtkToolbar *toolbar = GTK_TOOLBAR (object);
3164 GtkToolbarPrivate *priv = toolbar->priv;
3166 if (priv->sibling_path != NULL)
3167 gtk_widget_path_unref (priv->sibling_path);
3169 g_list_free_full (priv->content, (GDestroyNotify)toolbar_content_free);
3171 g_timer_destroy (priv->timer);
3174 g_source_remove (priv->idle_id);
3176 G_OBJECT_CLASS (gtk_toolbar_parent_class)->finalize (object);
3180 * gtk_toolbar_set_icon_size:
3181 * @toolbar: A #GtkToolbar
3182 * @icon_size: (type int): The #GtkIconSize that stock icons in the
3183 * toolbar shall have.
3185 * This function sets the size of stock icons in the toolbar. You
3186 * can call it both before you add the icons and after they've been
3187 * added. The size you set will override user preferences for the default
3190 * This should only be used for special-purpose toolbars, normal
3191 * application toolbars should respect the user preferences for the
3195 gtk_toolbar_set_icon_size (GtkToolbar *toolbar,
3196 GtkIconSize icon_size)
3198 GtkToolbarPrivate *priv;
3200 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3201 g_return_if_fail (icon_size != GTK_ICON_SIZE_INVALID);
3203 priv = toolbar->priv;
3205 if (!priv->icon_size_set)
3207 priv->icon_size_set = TRUE;
3208 g_object_notify (G_OBJECT (toolbar), "icon-size-set");
3211 if (priv->icon_size == icon_size)
3214 priv->icon_size = icon_size;
3215 g_object_notify (G_OBJECT (toolbar), "icon-size");
3217 gtk_toolbar_reconfigured (toolbar);
3219 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3223 * gtk_toolbar_unset_icon_size:
3224 * @toolbar: a #GtkToolbar
3226 * Unsets toolbar icon size set with gtk_toolbar_set_icon_size(), so that
3227 * user preferences will be used to determine the icon size.
3230 gtk_toolbar_unset_icon_size (GtkToolbar *toolbar)
3232 GtkToolbarPrivate *priv;
3235 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3237 priv = toolbar->priv;
3239 if (priv->icon_size_set)
3241 GtkSettings *settings = toolbar_get_settings (toolbar);
3245 g_object_get (settings,
3246 "gtk-toolbar-icon-size", &size,
3250 size = DEFAULT_ICON_SIZE;
3252 if (size != priv->icon_size)
3254 gtk_toolbar_set_icon_size (toolbar, size);
3255 g_object_notify (G_OBJECT (toolbar), "icon-size");
3258 priv->icon_size_set = FALSE;
3259 g_object_notify (G_OBJECT (toolbar), "icon-size-set");
3264 * ToolbarContent methods
3272 struct _ToolbarContent
3277 GtkAllocation allocation;
3278 GtkAllocation start_allocation;
3279 GtkAllocation goal_allocation;
3280 guint is_placeholder : 1;
3281 guint disappearing : 1;
3286 toolbar_item_visiblity_notify_cb (GObject *obj,
3290 GtkToolbar *toolbar = user_data;
3292 gtk_toolbar_invalidate_order (toolbar);
3295 static ToolbarContent *
3296 toolbar_content_new_tool_item (GtkToolbar *toolbar,
3298 gboolean is_placeholder,
3301 GtkToolbarPrivate *priv = toolbar->priv;
3302 ToolbarContent *content;
3304 content = g_slice_new0 (ToolbarContent);
3306 content->state = NOT_ALLOCATED;
3307 content->item = item;
3308 content->is_placeholder = is_placeholder;
3310 priv->content = g_list_insert (priv->content, content, pos);
3312 gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (toolbar));
3313 gtk_toolbar_invalidate_order (toolbar);
3315 g_signal_connect (content->item, "notify::visible",
3316 G_CALLBACK (toolbar_item_visiblity_notify_cb), toolbar);
3318 if (!is_placeholder)
3320 priv->num_children++;
3322 gtk_toolbar_stop_sliding (toolbar);
3325 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3326 priv->need_rebuild = TRUE;
3332 toolbar_content_remove (ToolbarContent *content,
3333 GtkToolbar *toolbar)
3335 GtkToolbarPrivate *priv = toolbar->priv;
3337 gtk_toolbar_invalidate_order (toolbar);
3338 gtk_widget_unparent (GTK_WIDGET (content->item));
3340 g_signal_handlers_disconnect_by_func (content->item,
3341 toolbar_item_visiblity_notify_cb,
3344 priv->content = g_list_remove (priv->content, content);
3346 if (!toolbar_content_is_placeholder (content))
3347 priv->num_children--;
3349 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3350 priv->need_rebuild = TRUE;
3354 toolbar_content_free (ToolbarContent *content)
3356 g_slice_free (ToolbarContent, content);
3360 calculate_max_homogeneous_pixels (GtkWidget *widget)
3362 PangoContext *context;
3363 PangoFontMetrics *metrics;
3364 const PangoFontDescription *font_desc;
3365 GtkStyleContext *style_context;
3366 GtkStateFlags state;
3369 context = gtk_widget_get_pango_context (widget);
3370 style_context = gtk_widget_get_style_context (widget);
3371 state = gtk_widget_get_state_flags (widget);
3373 font_desc = gtk_style_context_get_font (style_context, state);
3375 metrics = pango_context_get_metrics (context, font_desc,
3376 pango_context_get_language (context));
3377 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3378 pango_font_metrics_unref (metrics);
3380 return PANGO_PIXELS (MAX_HOMOGENEOUS_N_CHARS * char_width);
3384 toolbar_content_draw (ToolbarContent *content,
3385 GtkContainer *container,
3390 if (content->is_placeholder)
3393 widget = GTK_WIDGET (content->item);
3396 gtk_container_propagate_draw (container, widget, cr);
3400 toolbar_content_visible (ToolbarContent *content,
3401 GtkToolbar *toolbar)
3403 GtkToolbarPrivate *priv = toolbar->priv;
3406 item = content->item;
3408 if (!gtk_widget_get_visible (GTK_WIDGET (item)))
3411 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
3412 gtk_tool_item_get_visible_horizontal (item))
3415 if (priv->orientation == GTK_ORIENTATION_VERTICAL &&
3416 gtk_tool_item_get_visible_vertical (item))
3423 toolbar_content_size_request (ToolbarContent *content,
3424 GtkToolbar *toolbar,
3425 GtkRequisition *requisition)
3427 gtk_widget_get_preferred_size (GTK_WIDGET (content->item),
3429 if (content->is_placeholder &&
3430 content->disappearing)
3432 requisition->width = 0;
3433 requisition->height = 0;
3438 toolbar_content_is_homogeneous (ToolbarContent *content,
3439 GtkToolbar *toolbar)
3441 GtkToolbarPrivate *priv = toolbar->priv;
3442 GtkRequisition requisition;
3445 if (priv->max_homogeneous_pixels < 0)
3447 priv->max_homogeneous_pixels =
3448 calculate_max_homogeneous_pixels (GTK_WIDGET (toolbar));
3451 toolbar_content_size_request (content, toolbar, &requisition);
3453 if (requisition.width > priv->max_homogeneous_pixels)
3456 result = gtk_tool_item_get_homogeneous (content->item) &&
3457 !GTK_IS_SEPARATOR_TOOL_ITEM (content->item);
3459 if (gtk_tool_item_get_is_important (content->item) &&
3460 priv->style == GTK_TOOLBAR_BOTH_HORIZ &&
3461 priv->orientation == GTK_ORIENTATION_HORIZONTAL)
3470 toolbar_content_is_placeholder (ToolbarContent *content)
3472 if (content->is_placeholder)
3479 toolbar_content_disappearing (ToolbarContent *content)
3481 if (content->disappearing)
3488 toolbar_content_get_state (ToolbarContent *content)
3490 return content->state;
3494 toolbar_content_child_visible (ToolbarContent *content)
3496 return gtk_widget_get_child_visible (GTK_WIDGET (content->item));
3500 toolbar_content_get_goal_allocation (ToolbarContent *content,
3501 GtkAllocation *allocation)
3503 *allocation = content->goal_allocation;
3507 toolbar_content_get_allocation (ToolbarContent *content,
3508 GtkAllocation *allocation)
3510 *allocation = content->allocation;
3514 toolbar_content_set_start_allocation (ToolbarContent *content,
3515 GtkAllocation *allocation)
3517 content->start_allocation = *allocation;
3521 toolbar_content_get_expand (ToolbarContent *content)
3523 if (!content->disappearing &&
3524 gtk_tool_item_get_expand (content->item))
3531 toolbar_content_set_goal_allocation (ToolbarContent *content,
3532 GtkAllocation *allocation)
3534 content->goal_allocation = *allocation;
3538 toolbar_content_set_child_visible (ToolbarContent *content,
3539 GtkToolbar *toolbar,
3542 gtk_widget_set_child_visible (GTK_WIDGET (content->item),
3547 toolbar_content_get_start_allocation (ToolbarContent *content,
3548 GtkAllocation *start_allocation)
3550 *start_allocation = content->start_allocation;
3554 toolbar_content_size_allocate (ToolbarContent *content,
3555 GtkAllocation *allocation)
3557 content->allocation = *allocation;
3558 gtk_widget_size_allocate (GTK_WIDGET (content->item),
3563 toolbar_content_set_state (ToolbarContent *content,
3566 content->state = state;
3570 toolbar_content_get_widget (ToolbarContent *content)
3572 return GTK_WIDGET (content->item);
3577 toolbar_content_set_disappearing (ToolbarContent *content,
3578 gboolean disappearing)
3580 content->disappearing = disappearing;
3584 toolbar_content_set_size_request (ToolbarContent *content,
3588 gtk_widget_set_size_request (GTK_WIDGET (content->item),
3593 toolbar_content_toolbar_reconfigured (ToolbarContent *content,
3594 GtkToolbar *toolbar)
3596 gtk_tool_item_toolbar_reconfigured (content->item);
3600 toolbar_content_retrieve_menu_item (ToolbarContent *content)
3602 return gtk_tool_item_retrieve_proxy_menu_item (content->item);
3606 toolbar_content_has_proxy_menu_item (ToolbarContent *content)
3608 GtkWidget *menu_item;
3610 if (content->has_menu == YES)
3612 else if (content->has_menu == NO)
3615 menu_item = toolbar_content_retrieve_menu_item (content);
3617 content->has_menu = menu_item? YES : NO;
3619 return menu_item != NULL;
3623 toolbar_content_set_unknown_menu_status (ToolbarContent *content)
3625 content->has_menu = UNKNOWN;
3629 toolbar_content_is_separator (ToolbarContent *content)
3631 return GTK_IS_SEPARATOR_TOOL_ITEM (content->item);
3635 toolbar_content_set_expand (ToolbarContent *content,
3638 gtk_tool_item_set_expand (content->item, expand);
3642 toolbar_content_show_all (ToolbarContent *content)
3646 widget = toolbar_content_get_widget (content);
3648 gtk_widget_show_all (widget);
3654 static GtkReliefStyle
3655 get_button_relief (GtkToolbar *toolbar)
3657 GtkReliefStyle button_relief = GTK_RELIEF_NORMAL;
3659 gtk_widget_style_get (GTK_WIDGET (toolbar),
3660 "button-relief", &button_relief,
3663 return button_relief;
3667 get_internal_padding (GtkToolbar *toolbar)
3671 gtk_widget_style_get (GTK_WIDGET (toolbar),
3672 "internal-padding", &ipadding,
3679 get_max_child_expand (GtkToolbar *toolbar)
3681 gint mexpand = G_MAXINT;
3683 gtk_widget_style_get (GTK_WIDGET (toolbar),
3684 "max-child-expand", &mexpand,
3689 static GtkShadowType
3690 get_shadow_type (GtkToolbar *toolbar)
3692 GtkShadowType shadow_type;
3694 gtk_widget_style_get (GTK_WIDGET (toolbar),
3695 "shadow-type", &shadow_type,
3701 /* GTK+ internal methods */
3704 _gtk_toolbar_get_default_space_size (void)
3706 return DEFAULT_SPACE_SIZE;
3710 _gtk_toolbar_paint_space_line (GtkWidget *widget,
3711 GtkToolbar *toolbar,
3714 GtkOrientation orientation;
3715 GtkStyleContext *context;
3716 GtkStateFlags state;
3719 const gdouble start_fraction = (SPACE_LINE_START / SPACE_LINE_DIVISION);
3720 const gdouble end_fraction = (SPACE_LINE_END / SPACE_LINE_DIVISION);
3722 g_return_if_fail (GTK_IS_WIDGET (widget));
3724 orientation = toolbar ? toolbar->priv->orientation : GTK_ORIENTATION_HORIZONTAL;
3726 context = gtk_widget_get_style_context (widget);
3727 state = gtk_widget_get_state_flags (widget);
3728 width = gtk_widget_get_allocated_width (widget);
3729 height = gtk_widget_get_allocated_height (widget);
3730 gtk_style_context_get_padding (context, state, &padding);
3732 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3734 gboolean wide_separators;
3735 gint separator_width;
3737 gtk_widget_style_get (widget,
3738 "wide-separators", &wide_separators,
3739 "separator-width", &separator_width,
3742 if (wide_separators)
3743 gtk_render_frame (context, cr,
3744 (width - separator_width) / 2,
3745 height * start_fraction,
3747 height * (end_fraction - start_fraction));
3749 gtk_render_line (context, cr,
3750 (width - padding.left) / 2,
3751 height * start_fraction,
3752 (width - padding.left) / 2,
3753 height * end_fraction);
3757 gboolean wide_separators;
3758 gint separator_height;
3760 gtk_widget_style_get (widget,
3761 "wide-separators", &wide_separators,
3762 "separator-height", &separator_height,
3765 if (wide_separators)
3766 gtk_render_frame (context, cr,
3767 width * start_fraction,
3768 (height - separator_height) / 2,
3769 width * (end_fraction - start_fraction),
3772 gtk_render_line (context, cr,
3773 width * start_fraction,
3774 (height - padding.top) / 2,
3775 width * end_fraction,
3776 (height - padding.top) / 2);
3781 _gtk_toolbar_elide_underscores (const gchar *original)
3784 const gchar *p, *end;
3786 gboolean last_underscore;
3791 len = strlen (original);
3792 q = result = g_malloc (len + 1);
3793 last_underscore = FALSE;
3795 end = original + len;
3796 for (p = original; p < end; p++)
3798 if (!last_underscore && *p == '_')
3799 last_underscore = TRUE;
3802 last_underscore = FALSE;
3803 if (original + 2 <= p && p + 1 <= end &&
3804 p[-2] == '(' && p[-1] == '_' && p[0] != '_' && p[1] == ')')
3815 if (last_underscore)
3824 toolbar_get_icon_size (GtkToolShell *shell)
3826 GtkToolbar *toolbar = GTK_TOOLBAR (shell);
3827 GtkToolbarPrivate *priv = toolbar->priv;
3829 return priv->icon_size;
3832 static GtkOrientation
3833 toolbar_get_orientation (GtkToolShell *shell)
3835 GtkToolbar *toolbar = GTK_TOOLBAR (shell);
3836 GtkToolbarPrivate *priv = toolbar->priv;
3838 return priv->orientation;
3841 static GtkToolbarStyle
3842 toolbar_get_style (GtkToolShell *shell)
3844 GtkToolbar *toolbar = GTK_TOOLBAR (shell);
3845 GtkToolbarPrivate *priv = toolbar->priv;
3850 static GtkReliefStyle
3851 toolbar_get_relief_style (GtkToolShell *shell)
3853 return get_button_relief (GTK_TOOLBAR (shell));
3857 toolbar_rebuild_menu (GtkToolShell *shell)
3859 GtkToolbar *toolbar = GTK_TOOLBAR (shell);
3860 GtkToolbarPrivate *priv = toolbar->priv;
3863 priv->need_rebuild = TRUE;
3865 for (list = priv->content; list != NULL; list = list->next)
3867 ToolbarContent *content = list->data;
3869 toolbar_content_set_unknown_menu_status (content);
3872 gtk_widget_queue_resize (GTK_WIDGET (shell));
3875 typedef struct _CountingData CountingData;
3876 struct _CountingData {
3884 count_widget_position (GtkWidget *widget,
3887 CountingData *count = data;
3889 if (!gtk_widget_get_visible (widget))
3892 if (count->widget == widget)
3893 count->found = TRUE;
3894 else if (count->found)
3901 gtk_toolbar_get_visible_position (GtkToolbar *toolbar,
3904 CountingData count = { child, FALSE, 0, 0 };
3906 if (child == (GtkWidget*)toolbar->priv->highlight_tool_item)
3909 /* foreach iterates in visible order */
3910 gtk_container_forall (GTK_CONTAINER (toolbar),
3911 count_widget_position,
3914 g_assert (count.found);
3916 if (toolbar->priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
3917 gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
3920 return count.before;
3924 add_widget_to_path (gpointer data,
3927 GtkWidget *widget = data;
3928 GtkWidgetPath *path = user_data;
3930 if (gtk_widget_get_visible (widget))
3931 gtk_widget_path_append_for_widget (path, widget);
3934 static GtkWidgetPath *
3935 gtk_toolbar_get_path_for_child (GtkContainer *container,
3938 GtkWidgetPath *path;
3939 GtkToolbar *toolbar;
3940 GtkToolbarPrivate *priv;
3943 toolbar = GTK_TOOLBAR (container);
3944 priv = toolbar->priv;
3946 if (priv->sibling_path == NULL)
3950 /* build a path for all the visible children;
3951 * get_children works in visible order
3953 priv->sibling_path = gtk_widget_path_new ();
3954 children = _gtk_container_get_all_children (container);
3956 if (priv->orientation != GTK_ORIENTATION_HORIZONTAL ||
3957 gtk_widget_get_direction (GTK_WIDGET (toolbar)) != GTK_TEXT_DIR_RTL)
3958 children = g_list_reverse (children);
3960 g_list_foreach (children, add_widget_to_path, priv->sibling_path);
3961 g_list_free (children);
3964 path = gtk_widget_path_copy (gtk_widget_get_path (GTK_WIDGET (container)));
3965 if (gtk_widget_get_visible (child))
3967 vis_index = gtk_toolbar_get_visible_position (toolbar, child);
3969 if (vis_index < gtk_widget_path_length (priv->sibling_path))
3970 gtk_widget_path_append_with_siblings (path,
3974 gtk_widget_path_append_for_widget (path, child);
3977 gtk_widget_path_append_for_widget (path, child);
3983 gtk_toolbar_invalidate_order (GtkToolbar *toolbar)
3985 GtkToolbarPrivate *priv = toolbar->priv;
3987 if (priv->sibling_path != NULL)
3989 gtk_widget_path_unref (priv->sibling_path);
3990 priv->sibling_path = NULL;
3992 gtk_container_forall (GTK_CONTAINER (toolbar),
3993 (GtkCallback) gtk_widget_reset_style,
3999 gtk_toolbar_direction_changed (GtkWidget *widget,
4000 GtkTextDirection previous_direction)
4002 GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->direction_changed (widget, previous_direction);
4004 gtk_toolbar_invalidate_order (GTK_TOOLBAR (widget));