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"
44 #include "gtkmainprivate.h"
45 #include "gtkmarshalers.h"
47 #include "gtkorientable.h"
48 #include "gtkradiobutton.h"
49 #include "gtkradiotoolbutton.h"
50 #include "gtkseparatormenuitem.h"
51 #include "gtkseparatortoolitem.h"
53 #include "gtktoolshell.h"
55 #include "gtkprivate.h"
57 #include "gtktypebuiltins.h"
62 * @Short_description: Create bars of buttons and other widgets
64 * @See_also: #GtkToolItem
66 * A toolbar is created with a call to gtk_toolbar_new().
68 * A toolbar can contain instances of a subclass of #GtkToolItem. To add
69 * a #GtkToolItem to the a toolbar, use gtk_toolbar_insert(). To remove
70 * an item from the toolbar use gtk_container_remove(). To add a button
71 * to the toolbar, add an instance of #GtkToolButton.
73 * Toolbar items can be visually grouped by adding instances of
74 * #GtkSeparatorToolItem to the toolbar. If the GtkToolbar child property
75 * "expand" is #TRUE and the property #GtkSeparatorToolItem:draw is set to
76 * #FALSE, the effect is to force all following items to the end of the toolbar.
78 * Creating a context menu for the toolbar can be done by connecting to
79 * the #GtkToolbar::popup-context-menu signal.
83 typedef struct _ToolbarContent ToolbarContent;
85 #define DEFAULT_IPADDING 0
87 #define DEFAULT_SPACE_SIZE 12
88 #define DEFAULT_SPACE_STYLE GTK_TOOLBAR_SPACE_LINE
89 #define SPACE_LINE_DIVISION 10.0
90 #define SPACE_LINE_START 2.0
91 #define SPACE_LINE_END 8.0
93 #define DEFAULT_ICON_SIZE GTK_ICON_SIZE_LARGE_TOOLBAR
94 #define DEFAULT_TOOLBAR_STYLE GTK_TOOLBAR_BOTH
95 #define DEFAULT_ANIMATION_STATE TRUE
97 #define MAX_HOMOGENEOUS_N_CHARS 13 /* Items that are wider than this do not participate
98 * in the homogeneous game. In units of
99 * pango_font_get_estimated_char_width().
101 #define SLIDE_SPEED 600.0 /* How fast the items slide, in pixels per second */
102 #define ACCEL_THRESHOLD 0.18 /* After how much time in seconds will items start speeding up */
105 struct _GtkToolbarPrivate
108 GtkSettings *settings;
110 GtkIconSize icon_size;
111 GtkToolbarStyle style;
113 GtkToolItem *highlight_tool_item;
115 GtkWidget *arrow_button;
117 GdkWindow *event_window;
123 GtkWidgetPath *sibling_path;
125 gulong settings_connection;
128 gint button_maxw; /* maximum width of homogeneous children */
129 gint button_maxh; /* maximum height of homogeneous children */
130 gint max_homogeneous_pixels;
133 GtkOrientation orientation;
136 guint icon_size_set : 1;
137 guint is_sliding : 1;
138 guint need_rebuild : 1; /* whether the overflow menu should be regenerated */
140 guint show_arrow : 1;
155 /* Child properties */
159 CHILD_PROP_HOMOGENEOUS
179 static void gtk_toolbar_set_property (GObject *object,
183 static void gtk_toolbar_get_property (GObject *object,
187 static gint gtk_toolbar_draw (GtkWidget *widget,
189 static void gtk_toolbar_realize (GtkWidget *widget);
190 static void gtk_toolbar_unrealize (GtkWidget *widget);
191 static void gtk_toolbar_get_preferred_width (GtkWidget *widget,
194 static void gtk_toolbar_get_preferred_height (GtkWidget *widget,
198 static void gtk_toolbar_size_allocate (GtkWidget *widget,
199 GtkAllocation *allocation);
200 static void gtk_toolbar_style_updated (GtkWidget *widget);
201 static gboolean gtk_toolbar_focus (GtkWidget *widget,
202 GtkDirectionType dir);
203 static void gtk_toolbar_move_focus (GtkWidget *widget,
204 GtkDirectionType dir);
205 static void gtk_toolbar_screen_changed (GtkWidget *widget,
206 GdkScreen *previous_screen);
207 static void gtk_toolbar_map (GtkWidget *widget);
208 static void gtk_toolbar_unmap (GtkWidget *widget);
209 static void gtk_toolbar_set_child_property (GtkContainer *container,
214 static void gtk_toolbar_get_child_property (GtkContainer *container,
219 static void gtk_toolbar_finalize (GObject *object);
220 static void gtk_toolbar_dispose (GObject *object);
221 static void gtk_toolbar_show_all (GtkWidget *widget);
222 static void gtk_toolbar_add (GtkContainer *container,
224 static void gtk_toolbar_remove (GtkContainer *container,
226 static void gtk_toolbar_forall (GtkContainer *container,
227 gboolean include_internals,
228 GtkCallback callback,
229 gpointer callback_data);
230 static GType gtk_toolbar_child_type (GtkContainer *container);
231 static GtkWidgetPath * gtk_toolbar_get_path_for_child
232 (GtkContainer *container,
234 static void gtk_toolbar_invalidate_order (GtkToolbar *toolbar);
236 static void gtk_toolbar_orientation_changed (GtkToolbar *toolbar,
237 GtkOrientation orientation);
238 static void gtk_toolbar_real_style_changed (GtkToolbar *toolbar,
239 GtkToolbarStyle style);
240 static gboolean gtk_toolbar_focus_home_or_end (GtkToolbar *toolbar,
241 gboolean focus_home);
242 static gboolean gtk_toolbar_button_press (GtkWidget *toolbar,
243 GdkEventButton *event);
244 static gboolean gtk_toolbar_arrow_button_press (GtkWidget *button,
245 GdkEventButton *event,
246 GtkToolbar *toolbar);
247 static void gtk_toolbar_arrow_button_clicked (GtkWidget *button,
248 GtkToolbar *toolbar);
249 static void gtk_toolbar_update_button_relief (GtkToolbar *toolbar);
250 static gboolean gtk_toolbar_popup_menu (GtkWidget *toolbar);
251 static void gtk_toolbar_reconfigured (GtkToolbar *toolbar);
253 static GtkReliefStyle get_button_relief (GtkToolbar *toolbar);
254 static gint get_internal_padding (GtkToolbar *toolbar);
255 static gint get_max_child_expand (GtkToolbar *toolbar);
256 static GtkShadowType get_shadow_type (GtkToolbar *toolbar);
258 /* methods on ToolbarContent 'class' */
259 static ToolbarContent *toolbar_content_new_tool_item (GtkToolbar *toolbar,
261 gboolean is_placeholder,
263 static void toolbar_content_remove (ToolbarContent *content,
264 GtkToolbar *toolbar);
265 static void toolbar_content_free (ToolbarContent *content);
266 static void toolbar_content_draw (ToolbarContent *content,
267 GtkContainer *container,
269 static gboolean toolbar_content_visible (ToolbarContent *content,
270 GtkToolbar *toolbar);
271 static void toolbar_content_size_request (ToolbarContent *content,
273 GtkRequisition *requisition);
274 static gboolean toolbar_content_is_homogeneous (ToolbarContent *content,
275 GtkToolbar *toolbar);
276 static gboolean toolbar_content_is_placeholder (ToolbarContent *content);
277 static gboolean toolbar_content_disappearing (ToolbarContent *content);
278 static ItemState toolbar_content_get_state (ToolbarContent *content);
279 static gboolean toolbar_content_child_visible (ToolbarContent *content);
280 static void toolbar_content_get_goal_allocation (ToolbarContent *content,
281 GtkAllocation *allocation);
282 static void toolbar_content_get_allocation (ToolbarContent *content,
283 GtkAllocation *allocation);
284 static void toolbar_content_set_start_allocation (ToolbarContent *content,
285 GtkAllocation *new_start_allocation);
286 static void toolbar_content_get_start_allocation (ToolbarContent *content,
287 GtkAllocation *start_allocation);
288 static gboolean toolbar_content_get_expand (ToolbarContent *content);
289 static void toolbar_content_set_goal_allocation (ToolbarContent *content,
290 GtkAllocation *allocation);
291 static void toolbar_content_set_child_visible (ToolbarContent *content,
294 static void toolbar_content_size_allocate (ToolbarContent *content,
295 GtkAllocation *allocation);
296 static void toolbar_content_set_state (ToolbarContent *content,
297 ItemState new_state);
298 static GtkWidget * toolbar_content_get_widget (ToolbarContent *content);
299 static void toolbar_content_set_disappearing (ToolbarContent *content,
300 gboolean disappearing);
301 static void toolbar_content_set_size_request (ToolbarContent *content,
304 static void toolbar_content_toolbar_reconfigured (ToolbarContent *content,
305 GtkToolbar *toolbar);
306 static GtkWidget * toolbar_content_retrieve_menu_item (ToolbarContent *content);
307 static gboolean toolbar_content_has_proxy_menu_item (ToolbarContent *content);
308 static gboolean toolbar_content_is_separator (ToolbarContent *content);
309 static void toolbar_content_show_all (ToolbarContent *content);
310 static void toolbar_content_set_expand (ToolbarContent *content,
313 static void toolbar_tool_shell_iface_init (GtkToolShellIface *iface);
314 static GtkIconSize toolbar_get_icon_size (GtkToolShell *shell);
315 static GtkOrientation toolbar_get_orientation (GtkToolShell *shell);
316 static GtkToolbarStyle toolbar_get_style (GtkToolShell *shell);
317 static GtkReliefStyle toolbar_get_relief_style (GtkToolShell *shell);
318 static void toolbar_rebuild_menu (GtkToolShell *shell);
321 G_DEFINE_TYPE_WITH_CODE (GtkToolbar, gtk_toolbar, GTK_TYPE_CONTAINER,
322 G_IMPLEMENT_INTERFACE (GTK_TYPE_TOOL_SHELL,
323 toolbar_tool_shell_iface_init)
324 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
327 static guint toolbar_signals[LAST_SIGNAL] = { 0 };
331 add_arrow_bindings (GtkBindingSet *binding_set,
333 GtkDirectionType dir)
335 guint keypad_keysym = keysym - GDK_KEY_Left + GDK_KEY_KP_Left;
337 gtk_binding_entry_add_signal (binding_set, keysym, 0,
339 GTK_TYPE_DIRECTION_TYPE, dir);
340 gtk_binding_entry_add_signal (binding_set, keypad_keysym, 0,
342 GTK_TYPE_DIRECTION_TYPE, dir);
346 add_ctrl_tab_bindings (GtkBindingSet *binding_set,
347 GdkModifierType modifiers,
348 GtkDirectionType direction)
350 gtk_binding_entry_add_signal (binding_set,
351 GDK_KEY_Tab, GDK_CONTROL_MASK | modifiers,
353 GTK_TYPE_DIRECTION_TYPE, direction);
354 gtk_binding_entry_add_signal (binding_set,
355 GDK_KEY_KP_Tab, GDK_CONTROL_MASK | modifiers,
357 GTK_TYPE_DIRECTION_TYPE, direction);
361 gtk_toolbar_class_init (GtkToolbarClass *klass)
363 GObjectClass *gobject_class;
364 GtkWidgetClass *widget_class;
365 GtkContainerClass *container_class;
366 GtkBindingSet *binding_set;
368 gobject_class = (GObjectClass *)klass;
369 widget_class = (GtkWidgetClass *)klass;
370 container_class = (GtkContainerClass *)klass;
372 gobject_class->set_property = gtk_toolbar_set_property;
373 gobject_class->get_property = gtk_toolbar_get_property;
374 gobject_class->finalize = gtk_toolbar_finalize;
375 gobject_class->dispose = gtk_toolbar_dispose;
377 widget_class->button_press_event = gtk_toolbar_button_press;
378 widget_class->draw = gtk_toolbar_draw;
379 widget_class->get_preferred_width = gtk_toolbar_get_preferred_width;
380 widget_class->get_preferred_height = gtk_toolbar_get_preferred_height;
381 widget_class->size_allocate = gtk_toolbar_size_allocate;
382 widget_class->style_updated = gtk_toolbar_style_updated;
383 widget_class->focus = gtk_toolbar_focus;
385 gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_TOOL_BAR);
387 /* need to override the base class function via override_class_handler,
388 * because the signal slot is not available in GtkWidgetClass
390 g_signal_override_class_handler ("move-focus",
392 G_CALLBACK (gtk_toolbar_move_focus));
394 widget_class->screen_changed = gtk_toolbar_screen_changed;
395 widget_class->realize = gtk_toolbar_realize;
396 widget_class->unrealize = gtk_toolbar_unrealize;
397 widget_class->map = gtk_toolbar_map;
398 widget_class->unmap = gtk_toolbar_unmap;
399 widget_class->popup_menu = gtk_toolbar_popup_menu;
400 widget_class->show_all = gtk_toolbar_show_all;
402 container_class->add = gtk_toolbar_add;
403 container_class->remove = gtk_toolbar_remove;
404 container_class->forall = gtk_toolbar_forall;
405 container_class->child_type = gtk_toolbar_child_type;
406 container_class->get_child_property = gtk_toolbar_get_child_property;
407 container_class->set_child_property = gtk_toolbar_set_child_property;
408 container_class->get_path_for_child = gtk_toolbar_get_path_for_child;
410 klass->orientation_changed = gtk_toolbar_orientation_changed;
411 klass->style_changed = gtk_toolbar_real_style_changed;
414 * GtkToolbar::orientation-changed:
415 * @toolbar: the object which emitted the signal
416 * @orientation: the new #GtkOrientation of the toolbar
418 * Emitted when the orientation of the toolbar changes.
420 toolbar_signals[ORIENTATION_CHANGED] =
421 g_signal_new (I_("orientation-changed"),
422 G_OBJECT_CLASS_TYPE (klass),
424 G_STRUCT_OFFSET (GtkToolbarClass, orientation_changed),
426 g_cclosure_marshal_VOID__ENUM,
428 GTK_TYPE_ORIENTATION);
430 * GtkToolbar::style-changed:
431 * @toolbar: The #GtkToolbar which emitted the signal
432 * @style: the new #GtkToolbarStyle of the toolbar
434 * Emitted when the style of the toolbar changes.
436 toolbar_signals[STYLE_CHANGED] =
437 g_signal_new (I_("style-changed"),
438 G_OBJECT_CLASS_TYPE (klass),
440 G_STRUCT_OFFSET (GtkToolbarClass, style_changed),
442 g_cclosure_marshal_VOID__ENUM,
444 GTK_TYPE_TOOLBAR_STYLE);
446 * GtkToolbar::popup-context-menu:
447 * @toolbar: the #GtkToolbar which emitted the signal
448 * @x: the x coordinate of the point where the menu should appear
449 * @y: the y coordinate of the point where the menu should appear
450 * @button: the mouse button the user pressed, or -1
452 * Emitted when the user right-clicks the toolbar or uses the
453 * keybinding to display a popup menu.
455 * Application developers should handle this signal if they want
456 * to display a context menu on the toolbar. The context-menu should
457 * appear at the coordinates given by @x and @y. The mouse button
458 * number is given by the @button parameter. If the menu was popped
459 * up using the keybaord, @button is -1.
461 * Return value: return %TRUE if the signal was handled, %FALSE if not
463 toolbar_signals[POPUP_CONTEXT_MENU] =
464 g_signal_new (I_("popup-context-menu"),
465 G_OBJECT_CLASS_TYPE (klass),
467 G_STRUCT_OFFSET (GtkToolbarClass, popup_context_menu),
468 _gtk_boolean_handled_accumulator, NULL,
469 _gtk_marshal_BOOLEAN__INT_INT_INT,
471 G_TYPE_INT, G_TYPE_INT,
475 * GtkToolbar::focus-home-or-end:
476 * @toolbar: the #GtkToolbar which emitted the signal
477 * @focus_home: %TRUE if the first item should be focused
479 * A keybinding signal used internally by GTK+. This signal can't
480 * be used in application code
482 * Return value: %TRUE if the signal was handled, %FALSE if not
484 toolbar_signals[FOCUS_HOME_OR_END] =
485 g_signal_new_class_handler (I_("focus-home-or-end"),
486 G_OBJECT_CLASS_TYPE (klass),
487 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
488 G_CALLBACK (gtk_toolbar_focus_home_or_end),
490 _gtk_marshal_BOOLEAN__BOOLEAN,
495 g_object_class_override_property (gobject_class,
499 g_object_class_install_property (gobject_class,
501 g_param_spec_enum ("toolbar-style",
503 P_("How to draw the toolbar"),
504 GTK_TYPE_TOOLBAR_STYLE,
505 DEFAULT_TOOLBAR_STYLE,
506 GTK_PARAM_READWRITE));
507 g_object_class_install_property (gobject_class,
509 g_param_spec_boolean ("show-arrow",
511 P_("If an arrow should be shown if the toolbar doesn't fit"),
513 GTK_PARAM_READWRITE));
516 * GtkToolbar:icon-size:
518 * The size of the icons in a toolbar is normally determined by
519 * the toolbar-icon-size setting. When this property is set, it
520 * overrides the setting.
522 * This should only be used for special-purpose toolbars, normal
523 * application toolbars should respect the user preferences for the
528 g_object_class_install_property (gobject_class,
530 g_param_spec_int ("icon-size",
532 P_("Size of icons in this toolbar"),
535 GTK_PARAM_READWRITE));
538 * GtkToolbar:icon-size-set:
540 * Is %TRUE if the icon-size property has been set.
544 g_object_class_install_property (gobject_class,
546 g_param_spec_boolean ("icon-size-set",
548 P_("Whether the icon-size property has been set"),
550 GTK_PARAM_READWRITE));
552 /* child properties */
553 gtk_container_class_install_child_property (container_class,
555 g_param_spec_boolean ("expand",
557 P_("Whether the item should receive extra space when the toolbar grows"),
559 GTK_PARAM_READWRITE));
561 gtk_container_class_install_child_property (container_class,
562 CHILD_PROP_HOMOGENEOUS,
563 g_param_spec_boolean ("homogeneous",
565 P_("Whether the item should be the same size as other homogeneous items"),
567 GTK_PARAM_READWRITE));
569 /* style properties */
570 gtk_widget_class_install_style_property (widget_class,
571 g_param_spec_int ("space-size",
573 P_("Size of spacers"),
577 GTK_PARAM_READABLE));
579 gtk_widget_class_install_style_property (widget_class,
580 g_param_spec_int ("internal-padding",
581 P_("Internal padding"),
582 P_("Amount of border space between the toolbar shadow and the buttons"),
586 GTK_PARAM_READABLE));
588 gtk_widget_class_install_style_property (widget_class,
589 g_param_spec_int ("max-child-expand",
590 P_("Maximum child expand"),
591 P_("Maximum amount of space an expandable item will be given"),
595 GTK_PARAM_READABLE));
597 gtk_widget_class_install_style_property (widget_class,
598 g_param_spec_enum ("space-style",
600 P_("Whether spacers are vertical lines or just blank"),
601 GTK_TYPE_TOOLBAR_SPACE_STYLE,
603 GTK_PARAM_READABLE));
605 gtk_widget_class_install_style_property (widget_class,
606 g_param_spec_enum ("button-relief",
608 P_("Type of bevel around toolbar buttons"),
609 GTK_TYPE_RELIEF_STYLE,
611 GTK_PARAM_READABLE));
612 gtk_widget_class_install_style_property (widget_class,
613 g_param_spec_enum ("shadow-type",
615 P_("Style of bevel around the toolbar"),
616 GTK_TYPE_SHADOW_TYPE,
618 GTK_PARAM_READABLE));
620 binding_set = gtk_binding_set_by_class (klass);
622 add_arrow_bindings (binding_set, GDK_KEY_Left, GTK_DIR_LEFT);
623 add_arrow_bindings (binding_set, GDK_KEY_Right, GTK_DIR_RIGHT);
624 add_arrow_bindings (binding_set, GDK_KEY_Up, GTK_DIR_UP);
625 add_arrow_bindings (binding_set, GDK_KEY_Down, GTK_DIR_DOWN);
627 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
628 "focus-home-or-end", 1,
629 G_TYPE_BOOLEAN, TRUE);
630 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
631 "focus-home-or-end", 1,
632 G_TYPE_BOOLEAN, TRUE);
633 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
634 "focus-home-or-end", 1,
635 G_TYPE_BOOLEAN, FALSE);
636 gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
637 "focus-home-or-end", 1,
638 G_TYPE_BOOLEAN, FALSE);
640 add_ctrl_tab_bindings (binding_set, 0, GTK_DIR_TAB_FORWARD);
641 add_ctrl_tab_bindings (binding_set, GDK_SHIFT_MASK, GTK_DIR_TAB_BACKWARD);
643 g_type_class_add_private (gobject_class, sizeof (GtkToolbarPrivate));
647 toolbar_tool_shell_iface_init (GtkToolShellIface *iface)
649 iface->get_icon_size = toolbar_get_icon_size;
650 iface->get_orientation = toolbar_get_orientation;
651 iface->get_style = toolbar_get_style;
652 iface->get_relief_style = toolbar_get_relief_style;
653 iface->rebuild_menu = toolbar_rebuild_menu;
657 gtk_toolbar_init (GtkToolbar *toolbar)
659 GtkToolbarPrivate *priv;
660 GtkStyleContext *context;
662 toolbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (toolbar,
665 priv = toolbar->priv;
667 gtk_widget_set_can_focus (GTK_WIDGET (toolbar), FALSE);
668 gtk_widget_set_has_window (GTK_WIDGET (toolbar), FALSE);
670 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
671 priv->style = DEFAULT_TOOLBAR_STYLE;
672 priv->icon_size = DEFAULT_ICON_SIZE;
673 priv->animation = DEFAULT_ANIMATION_STATE;
675 priv->arrow_button = gtk_toggle_button_new ();
676 g_signal_connect (priv->arrow_button, "button-press-event",
677 G_CALLBACK (gtk_toolbar_arrow_button_press), toolbar);
678 g_signal_connect (priv->arrow_button, "clicked",
679 G_CALLBACK (gtk_toolbar_arrow_button_clicked), toolbar);
680 gtk_button_set_relief (GTK_BUTTON (priv->arrow_button),
681 get_button_relief (toolbar));
683 gtk_button_set_focus_on_click (GTK_BUTTON (priv->arrow_button), FALSE);
685 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
686 gtk_widget_set_name (priv->arrow, "gtk-toolbar-arrow");
687 gtk_widget_show (priv->arrow);
688 gtk_container_add (GTK_CONTAINER (priv->arrow_button), priv->arrow);
690 gtk_widget_set_parent (priv->arrow_button, GTK_WIDGET (toolbar));
692 /* which child position a drop will occur at */
694 priv->show_arrow = TRUE;
695 priv->settings = NULL;
697 priv->max_homogeneous_pixels = -1;
699 priv->timer = g_timer_new ();
701 context = gtk_widget_get_style_context (GTK_WIDGET (toolbar));
702 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLBAR);
706 gtk_toolbar_set_property (GObject *object,
711 GtkToolbar *toolbar = GTK_TOOLBAR (object);
712 GtkToolbarPrivate *priv = toolbar->priv;
716 case PROP_ORIENTATION:
717 g_signal_emit (toolbar, toolbar_signals[ORIENTATION_CHANGED], 0,
718 g_value_get_enum (value));
720 case PROP_TOOLBAR_STYLE:
721 gtk_toolbar_set_style (toolbar, g_value_get_enum (value));
723 case PROP_SHOW_ARROW:
724 gtk_toolbar_set_show_arrow (toolbar, g_value_get_boolean (value));
727 gtk_toolbar_set_icon_size (toolbar, g_value_get_int (value));
729 case PROP_ICON_SIZE_SET:
730 if (g_value_get_boolean (value))
731 priv->icon_size_set = TRUE;
733 gtk_toolbar_unset_icon_size (toolbar);
736 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
742 gtk_toolbar_get_property (GObject *object,
747 GtkToolbar *toolbar = GTK_TOOLBAR (object);
748 GtkToolbarPrivate *priv = toolbar->priv;
752 case PROP_ORIENTATION:
753 g_value_set_enum (value, priv->orientation);
755 case PROP_TOOLBAR_STYLE:
756 g_value_set_enum (value, priv->style);
758 case PROP_SHOW_ARROW:
759 g_value_set_boolean (value, priv->show_arrow);
762 g_value_set_int (value, gtk_toolbar_get_icon_size (toolbar));
764 case PROP_ICON_SIZE_SET:
765 g_value_set_boolean (value, priv->icon_size_set);
768 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
774 gtk_toolbar_map (GtkWidget *widget)
776 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
777 GtkToolbarPrivate *priv = toolbar->priv;
779 GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->map (widget);
781 if (priv->event_window)
782 gdk_window_show_unraised (priv->event_window);
786 gtk_toolbar_unmap (GtkWidget *widget)
788 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
789 GtkToolbarPrivate *priv = toolbar->priv;
791 if (priv->event_window)
792 gdk_window_hide (priv->event_window);
794 GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->unmap (widget);
798 gtk_toolbar_realize (GtkWidget *widget)
800 GtkAllocation allocation;
801 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
802 GtkToolbarPrivate *priv = toolbar->priv;
804 GdkWindowAttr attributes;
805 gint attributes_mask;
808 gtk_widget_set_realized (widget, TRUE);
810 gtk_widget_get_allocation (widget, &allocation);
811 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
813 attributes.wclass = GDK_INPUT_ONLY;
814 attributes.window_type = GDK_WINDOW_CHILD;
815 attributes.x = allocation.x + border_width;
816 attributes.y = allocation.y + border_width;
817 attributes.width = allocation.width - border_width * 2;
818 attributes.height = allocation.height - border_width * 2;
819 attributes.event_mask = gtk_widget_get_events (widget);
820 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
821 GDK_BUTTON_RELEASE_MASK |
822 GDK_ENTER_NOTIFY_MASK |
823 GDK_LEAVE_NOTIFY_MASK);
825 attributes_mask = GDK_WA_X | GDK_WA_Y;
827 window = gtk_widget_get_parent_window (widget);
828 gtk_widget_set_window (widget, window);
829 g_object_ref (window);
831 priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
832 &attributes, attributes_mask);
833 gdk_window_set_user_data (priv->event_window, toolbar);
837 gtk_toolbar_unrealize (GtkWidget *widget)
839 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
840 GtkToolbarPrivate *priv = toolbar->priv;
842 if (priv->event_window)
844 gdk_window_set_user_data (priv->event_window, NULL);
845 gdk_window_destroy (priv->event_window);
846 priv->event_window = NULL;
849 GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->unrealize (widget);
853 gtk_toolbar_draw (GtkWidget *widget,
856 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
857 GtkToolbarPrivate *priv = toolbar->priv;
858 GtkStyleContext *context;
863 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
864 context = gtk_widget_get_style_context (widget);
865 state = gtk_widget_get_state_flags (widget);
867 gtk_style_context_save (context);
868 gtk_style_context_set_state (context, state);
870 gtk_render_background (context, cr, border_width, border_width,
871 gtk_widget_get_allocated_width (widget) - 2 * border_width,
872 gtk_widget_get_allocated_height (widget) - 2 * border_width);
873 gtk_render_frame (context, cr, border_width, border_width,
874 gtk_widget_get_allocated_width (widget) - 2 * border_width,
875 gtk_widget_get_allocated_height (widget) - 2 * border_width);
877 for (list = priv->content; list != NULL; list = list->next)
879 ToolbarContent *content = list->data;
881 toolbar_content_draw (content, GTK_CONTAINER (widget), cr);
884 gtk_container_propagate_draw (GTK_CONTAINER (widget),
888 gtk_style_context_restore (context);
894 gtk_toolbar_size_request (GtkWidget *widget,
895 GtkRequisition *requisition)
897 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
898 GtkToolbarPrivate *priv = toolbar->priv;
900 gint max_child_height;
901 gint max_child_width;
902 gint max_homogeneous_child_width;
903 gint max_homogeneous_child_height;
904 gint homogeneous_size;
906 gint pack_front_size;
909 GtkRequisition arrow_requisition;
911 max_homogeneous_child_width = 0;
912 max_homogeneous_child_height = 0;
914 max_child_height = 0;
915 for (list = priv->content; list != NULL; list = list->next)
917 GtkRequisition requisition;
918 ToolbarContent *content = list->data;
920 if (!toolbar_content_visible (content, toolbar))
923 toolbar_content_size_request (content, toolbar, &requisition);
925 max_child_width = MAX (max_child_width, requisition.width);
926 max_child_height = MAX (max_child_height, requisition.height);
928 if (toolbar_content_is_homogeneous (content, toolbar))
930 max_homogeneous_child_width = MAX (max_homogeneous_child_width, requisition.width);
931 max_homogeneous_child_height = MAX (max_homogeneous_child_height, requisition.height);
935 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
936 homogeneous_size = max_homogeneous_child_width;
938 homogeneous_size = max_homogeneous_child_height;
941 for (list = priv->content; list != NULL; list = list->next)
943 ToolbarContent *content = list->data;
946 if (!toolbar_content_visible (content, toolbar))
949 if (toolbar_content_is_homogeneous (content, toolbar))
951 size = homogeneous_size;
955 GtkRequisition requisition;
957 toolbar_content_size_request (content, toolbar, &requisition);
959 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
960 size = requisition.width;
962 size = requisition.height;
965 pack_front_size += size;
968 if (priv->show_arrow)
970 gtk_widget_get_preferred_size (priv->arrow_button,
971 &arrow_requisition, NULL);
973 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
974 long_req = arrow_requisition.width;
976 long_req = arrow_requisition.height;
978 /* There is no point requesting space for the arrow if that would take
979 * up more space than all the items combined
981 long_req = MIN (long_req, pack_front_size);
985 arrow_requisition.height = 0;
986 arrow_requisition.width = 0;
988 long_req = pack_front_size;
991 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
993 requisition->width = long_req;
994 requisition->height = MAX (max_child_height, arrow_requisition.height);
998 requisition->height = long_req;
999 requisition->width = MAX (max_child_width, arrow_requisition.width);
1003 ipadding = get_internal_padding (toolbar);
1005 border_width = gtk_container_get_border_width (GTK_CONTAINER (toolbar));
1006 requisition->width += 2 * (ipadding + border_width);
1007 requisition->height += 2 * (ipadding + border_width);
1009 if (get_shadow_type (toolbar) != GTK_SHADOW_NONE)
1011 GtkStyleContext *context;
1012 GtkStateFlags state;
1015 context = gtk_widget_get_style_context (widget);
1016 state = gtk_widget_get_state_flags (widget);
1017 gtk_style_context_get_padding (context, state, &padding);
1019 requisition->width += padding.left + padding.right;
1020 requisition->height += padding.top + padding.bottom;
1023 priv->button_maxw = max_homogeneous_child_width;
1024 priv->button_maxh = max_homogeneous_child_height;
1028 gtk_toolbar_get_preferred_width (GtkWidget *widget,
1032 GtkRequisition requisition;
1034 gtk_toolbar_size_request (widget, &requisition);
1036 *minimum = *natural = requisition.width;
1040 gtk_toolbar_get_preferred_height (GtkWidget *widget,
1044 GtkRequisition requisition;
1046 gtk_toolbar_size_request (widget, &requisition);
1048 *minimum = *natural = requisition.height;
1052 position (GtkToolbar *toolbar,
1057 GtkToolbarPrivate *priv = toolbar->priv;
1060 if (!priv->animation)
1063 if (elapsed <= ACCEL_THRESHOLD)
1065 n_pixels = SLIDE_SPEED * elapsed;
1069 /* The formula is a second degree polynomial in
1070 * @elapsed that has the line SLIDE_SPEED * @elapsed
1071 * as tangent for @elapsed == ACCEL_THRESHOLD.
1072 * This makes @n_pixels a smooth function of elapsed time.
1074 n_pixels = (SLIDE_SPEED / ACCEL_THRESHOLD) * elapsed * elapsed -
1075 SLIDE_SPEED * elapsed + SLIDE_SPEED * ACCEL_THRESHOLD;
1079 return MIN (from + n_pixels, to);
1081 return MAX (from - n_pixels, to);
1085 compute_intermediate_allocation (GtkToolbar *toolbar,
1086 const GtkAllocation *start,
1087 const GtkAllocation *goal,
1088 GtkAllocation *intermediate)
1090 GtkToolbarPrivate *priv = toolbar->priv;
1091 gdouble elapsed = g_timer_elapsed (priv->timer, NULL);
1093 intermediate->x = position (toolbar, start->x, goal->x, elapsed);
1094 intermediate->y = position (toolbar, start->y, goal->y, elapsed);
1095 intermediate->width = position (toolbar, start->x + start->width,
1096 goal->x + goal->width,
1097 elapsed) - intermediate->x;
1098 intermediate->height = position (toolbar, start->y + start->height,
1099 goal->y + goal->height,
1100 elapsed) - intermediate->y;
1104 fixup_allocation_for_rtl (gint total_size,
1105 GtkAllocation *allocation)
1107 allocation->x += (total_size - (2 * allocation->x + allocation->width));
1111 fixup_allocation_for_vertical (GtkAllocation *allocation)
1115 tmp = allocation->x;
1116 allocation->x = allocation->y;
1117 allocation->y = tmp;
1119 tmp = allocation->width;
1120 allocation->width = allocation->height;
1121 allocation->height = tmp;
1125 get_item_size (GtkToolbar *toolbar,
1126 ToolbarContent *content)
1128 GtkToolbarPrivate *priv = toolbar->priv;
1129 GtkRequisition requisition;
1131 toolbar_content_size_request (content, toolbar, &requisition);
1133 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1135 if (toolbar_content_is_homogeneous (content, toolbar))
1136 return priv->button_maxw;
1138 return requisition.width;
1142 if (toolbar_content_is_homogeneous (content, toolbar))
1143 return priv->button_maxh;
1145 return requisition.height;
1150 slide_idle_handler (gpointer data)
1152 GtkToolbar *toolbar = GTK_TOOLBAR (data);
1153 GtkToolbarPrivate *priv = toolbar->priv;
1156 if (priv->need_sync)
1159 priv->need_sync = FALSE;
1162 for (list = priv->content; list != NULL; list = list->next)
1164 ToolbarContent *content = list->data;
1166 GtkAllocation goal_allocation;
1167 GtkAllocation allocation;
1170 state = toolbar_content_get_state (content);
1171 toolbar_content_get_goal_allocation (content, &goal_allocation);
1172 toolbar_content_get_allocation (content, &allocation);
1176 if (state == NOT_ALLOCATED)
1178 /* an unallocated item means that size allocate has to
1179 * called at least once more
1184 /* An invisible item with a goal allocation of
1185 * 0 is already at its goal.
1187 if ((state == NORMAL || state == OVERFLOWN) &&
1188 ((goal_allocation.width != 0 &&
1189 goal_allocation.height != 0) ||
1190 toolbar_content_child_visible (content)))
1192 if ((goal_allocation.x != allocation.x ||
1193 goal_allocation.y != allocation.y ||
1194 goal_allocation.width != allocation.width ||
1195 goal_allocation.height != allocation.height))
1197 /* An item is not in its right position yet. Note
1198 * that OVERFLOWN items do get an allocation in
1199 * gtk_toolbar_size_allocate(). This way you can see
1200 * them slide back in when you drag an item off the
1207 if (toolbar_content_is_placeholder (content) &&
1208 toolbar_content_disappearing (content) &&
1209 toolbar_content_child_visible (content))
1211 /* A disappearing placeholder is still visible.
1219 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1225 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1227 priv->is_sliding = FALSE;
1234 rect_within (GtkAllocation *a1,
1237 return (a1->x >= a2->x &&
1238 a1->x + a1->width <= a2->x + a2->width &&
1240 a1->y + a1->height <= a2->y + a2->height);
1244 gtk_toolbar_begin_sliding (GtkToolbar *toolbar)
1246 GtkAllocation allocation;
1247 GtkWidget *widget = GTK_WIDGET (toolbar);
1248 GtkToolbarPrivate *priv = toolbar->priv;
1249 GtkStyleContext *context;
1250 GtkStateFlags state;
1259 /* Start the sliding. This function copies the allocation of every
1260 * item into content->start_allocation. For items that haven't
1261 * been allocated yet, we calculate their position and save that
1262 * in start_allocatino along with zero width and zero height.
1264 * FIXME: It would be nice if we could share this code with
1265 * the equivalent in gtk_widget_size_allocate().
1267 priv->is_sliding = TRUE;
1270 priv->idle_id = gdk_threads_add_idle (slide_idle_handler, toolbar);
1272 gtk_widget_get_allocation (widget, &allocation);
1273 context = gtk_widget_get_style_context (widget);
1274 state = gtk_widget_get_state_flags (widget);
1275 gtk_style_context_get_padding (context, state, &padding);
1277 rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1278 vertical = (priv->orientation == GTK_ORIENTATION_VERTICAL);
1279 border_width = get_internal_padding (toolbar) + gtk_container_get_border_width (GTK_CONTAINER (toolbar));
1283 cur_x = allocation.width - border_width - padding.right;
1284 cur_y = allocation.height - border_width - padding.top;
1288 cur_x = border_width + padding.left;
1289 cur_y = border_width + padding.top;
1292 cur_x += allocation.x;
1293 cur_y += allocation.y;
1295 for (list = priv->content; list != NULL; list = list->next)
1297 ToolbarContent *content = list->data;
1298 GtkAllocation new_start_allocation;
1299 GtkAllocation item_allocation;
1302 state = toolbar_content_get_state (content);
1303 toolbar_content_get_allocation (content, &item_allocation);
1305 if ((state == NORMAL &&
1306 rect_within (&item_allocation, &allocation)) ||
1309 new_start_allocation = item_allocation;
1313 new_start_allocation.x = cur_x;
1314 new_start_allocation.y = cur_y;
1318 new_start_allocation.width = allocation.width -
1320 padding.left - padding.right;
1321 new_start_allocation.height = 0;
1325 new_start_allocation.width = 0;
1326 new_start_allocation.height = allocation.height -
1328 padding.top - padding.bottom;
1333 cur_y = new_start_allocation.y + new_start_allocation.height;
1335 cur_x = new_start_allocation.x;
1337 cur_x = new_start_allocation.x + new_start_allocation.width;
1339 toolbar_content_set_start_allocation (content, &new_start_allocation);
1342 /* This resize will run before the first idle handler. This
1343 * will make sure that items get the right goal allocation
1344 * so that the idle handler will not immediately return
1347 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1348 g_timer_reset (priv->timer);
1352 gtk_toolbar_stop_sliding (GtkToolbar *toolbar)
1354 GtkToolbarPrivate *priv = toolbar->priv;
1356 if (priv->is_sliding)
1360 priv->is_sliding = FALSE;
1364 g_source_remove (priv->idle_id);
1368 list = priv->content;
1371 ToolbarContent *content = list->data;
1374 if (toolbar_content_is_placeholder (content))
1376 toolbar_content_remove (content, toolbar);
1377 toolbar_content_free (content);
1381 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1386 remove_item (GtkWidget *menu_item,
1389 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (menu_item)),
1394 menu_deactivated (GtkWidget *menu,
1395 GtkToolbar *toolbar)
1397 GtkToolbarPrivate *priv = toolbar->priv;
1399 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
1403 menu_detached (GtkWidget *widget,
1406 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1407 GtkToolbarPrivate *priv = toolbar->priv;
1413 rebuild_menu (GtkToolbar *toolbar)
1415 GtkToolbarPrivate *priv = toolbar->priv;
1416 GList *list, *children;
1420 priv->menu = GTK_MENU (gtk_menu_new());
1421 gtk_menu_attach_to_widget (priv->menu,
1422 GTK_WIDGET (toolbar),
1425 g_signal_connect (priv->menu, "deactivate",
1426 G_CALLBACK (menu_deactivated), toolbar);
1429 gtk_container_foreach (GTK_CONTAINER (priv->menu), remove_item, NULL);
1431 for (list = priv->content; list != NULL; list = list->next)
1433 ToolbarContent *content = list->data;
1435 if (toolbar_content_get_state (content) == OVERFLOWN &&
1436 !toolbar_content_is_placeholder (content))
1438 GtkWidget *menu_item = toolbar_content_retrieve_menu_item (content);
1442 g_assert (GTK_IS_MENU_ITEM (menu_item));
1443 gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), menu_item);
1448 /* Remove leading and trailing separator items */
1449 children = gtk_container_get_children (GTK_CONTAINER (priv->menu));
1452 while (list && GTK_IS_SEPARATOR_MENU_ITEM (list->data))
1454 GtkWidget *child = list->data;
1456 gtk_container_remove (GTK_CONTAINER (priv->menu), child);
1459 g_list_free (children);
1461 /* Regenerate the list of children so we don't try to remove items twice */
1462 children = gtk_container_get_children (GTK_CONTAINER (priv->menu));
1464 list = g_list_last (children);
1465 while (list && GTK_IS_SEPARATOR_MENU_ITEM (list->data))
1467 GtkWidget *child = list->data;
1469 gtk_container_remove (GTK_CONTAINER (priv->menu), child);
1472 g_list_free (children);
1474 priv->need_rebuild = FALSE;
1478 gtk_toolbar_size_allocate (GtkWidget *widget,
1479 GtkAllocation *allocation)
1481 GtkAllocation widget_allocation;
1482 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1483 GtkToolbarPrivate *priv = toolbar->priv;
1484 GtkAllocation *allocations;
1485 ItemState *new_states;
1486 GtkAllocation arrow_allocation;
1487 GtkStyleContext *context;
1488 GtkStateFlags state;
1491 gint size, pos, short_size;
1494 gboolean need_arrow;
1495 gint n_expand_items;
1497 gint available_size;
1500 GtkRequisition arrow_requisition;
1501 gboolean overflowing;
1502 gboolean size_changed;
1503 GtkAllocation item_area;
1504 GtkShadowType shadow_type;
1506 context = gtk_widget_get_style_context (widget);
1507 state = gtk_widget_get_state_flags (widget);
1508 gtk_style_context_get_padding (context, state, &padding);
1510 gtk_widget_get_allocation (widget, &widget_allocation);
1511 size_changed = FALSE;
1512 if (widget_allocation.x != allocation->x ||
1513 widget_allocation.y != allocation->y ||
1514 widget_allocation.width != allocation->width ||
1515 widget_allocation.height != allocation->height)
1517 size_changed = TRUE;
1521 gtk_toolbar_stop_sliding (toolbar);
1523 gtk_widget_set_allocation (widget, allocation);
1525 border_width = gtk_container_get_border_width (GTK_CONTAINER (toolbar));
1527 if (gtk_widget_get_realized (widget))
1528 gdk_window_move_resize (priv->event_window,
1529 allocation->x + border_width,
1530 allocation->y + border_width,
1531 allocation->width - border_width * 2,
1532 allocation->height - border_width * 2);
1534 border_width += get_internal_padding (toolbar);
1536 gtk_widget_get_preferred_size (priv->arrow_button,
1537 &arrow_requisition, NULL);
1539 shadow_type = get_shadow_type (toolbar);
1541 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1543 available_size = size = allocation->width - 2 * border_width;
1544 short_size = allocation->height - 2 * border_width;
1545 arrow_size = arrow_requisition.width;
1547 if (shadow_type != GTK_SHADOW_NONE)
1549 available_size -= padding.left + padding.right;
1550 short_size -= padding.top + padding.bottom;
1555 available_size = size = allocation->height - 2 * border_width;
1556 short_size = allocation->width - 2 * border_width;
1557 arrow_size = arrow_requisition.height;
1559 if (shadow_type != GTK_SHADOW_NONE)
1561 available_size -= padding.top + padding.bottom;
1562 short_size -= padding.left + padding.right;
1566 n_items = g_list_length (priv->content);
1567 allocations = g_new0 (GtkAllocation, n_items);
1568 new_states = g_new0 (ItemState, n_items);
1572 for (list = priv->content; list != NULL; list = list->next)
1574 ToolbarContent *content = list->data;
1576 if (toolbar_content_visible (content, toolbar))
1578 needed_size += get_item_size (toolbar, content);
1580 /* Do we need an arrow?
1582 * Assume we don't, and see if any non-separator item
1583 * with a proxy menu item is then going to overflow.
1585 if (needed_size > available_size &&
1588 toolbar_content_has_proxy_menu_item (content) &&
1589 !toolbar_content_is_separator (content))
1597 size = available_size - arrow_size;
1599 size = available_size;
1601 /* calculate widths and states of items */
1602 overflowing = FALSE;
1603 for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1605 ToolbarContent *content = list->data;
1608 if (!toolbar_content_visible (content, toolbar))
1610 new_states[i] = HIDDEN;
1614 item_size = get_item_size (toolbar, content);
1615 if (item_size <= size && !overflowing)
1618 allocations[i].width = item_size;
1619 new_states[i] = NORMAL;
1624 new_states[i] = OVERFLOWN;
1625 allocations[i].width = item_size;
1629 /* calculate width of arrow */
1632 arrow_allocation.width = arrow_size;
1633 arrow_allocation.height = MAX (short_size, 1);
1636 /* expand expandable items */
1638 /* We don't expand when there is an overflow menu,
1639 * because that leads to weird jumps when items get
1640 * moved to the overflow menu and the expanding
1641 * items suddenly get a lot of extra space
1645 gint max_child_expand;
1648 for (i = 0, list = priv->content; list != NULL; list = list->next, ++i)
1650 ToolbarContent *content = list->data;
1652 if (toolbar_content_get_expand (content) && new_states[i] == NORMAL)
1656 max_child_expand = get_max_child_expand (toolbar);
1657 for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1659 ToolbarContent *content = list->data;
1661 if (toolbar_content_get_expand (content) && new_states[i] == NORMAL)
1663 gint extra = size / n_expand_items;
1664 if (size % n_expand_items != 0)
1667 if (extra > max_child_expand)
1668 extra = max_child_expand;
1670 allocations[i].width += extra;
1676 g_assert (n_expand_items == 0);
1679 /* position items */
1681 for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1683 /* Both NORMAL and OVERFLOWN items get a position.
1684 * This ensures that sliding will work for OVERFLOWN items too.
1686 if (new_states[i] == NORMAL || new_states[i] == OVERFLOWN)
1688 allocations[i].x = pos;
1689 allocations[i].y = border_width;
1690 allocations[i].height = short_size;
1692 pos += allocations[i].width;
1696 /* position arrow */
1699 arrow_allocation.x = available_size - border_width - arrow_allocation.width;
1700 arrow_allocation.y = border_width;
1703 item_area.x = border_width;
1704 item_area.y = border_width;
1705 item_area.width = available_size - (need_arrow? arrow_size : 0);
1706 item_area.height = short_size;
1708 /* fix up allocations in the vertical or RTL cases */
1709 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1711 for (i = 0; i < n_items; ++i)
1712 fixup_allocation_for_vertical (&(allocations[i]));
1715 fixup_allocation_for_vertical (&arrow_allocation);
1717 fixup_allocation_for_vertical (&item_area);
1719 else if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
1721 for (i = 0; i < n_items; ++i)
1722 fixup_allocation_for_rtl (available_size, &(allocations[i]));
1725 fixup_allocation_for_rtl (available_size, &arrow_allocation);
1727 fixup_allocation_for_rtl (available_size, &item_area);
1730 /* translate the items by allocation->(x,y) */
1731 for (i = 0; i < n_items; ++i)
1733 allocations[i].x += allocation->x;
1734 allocations[i].y += allocation->y;
1736 if (shadow_type != GTK_SHADOW_NONE)
1738 allocations[i].x += padding.left;
1739 allocations[i].y += padding.top;
1745 arrow_allocation.x += allocation->x;
1746 arrow_allocation.y += allocation->y;
1748 if (shadow_type != GTK_SHADOW_NONE)
1750 arrow_allocation.x += padding.left;
1751 arrow_allocation.y += padding.top;
1755 item_area.x += allocation->x;
1756 item_area.y += allocation->y;
1757 if (shadow_type != GTK_SHADOW_NONE)
1759 item_area.x += padding.left;
1760 item_area.y += padding.top;
1763 /* did anything change? */
1764 for (list = priv->content, i = 0; list != NULL; list = list->next, i++)
1766 ToolbarContent *content = list->data;
1768 if (toolbar_content_get_state (content) == NORMAL &&
1769 new_states[i] != NORMAL)
1771 /* an item disappeared and we didn't change size, so begin sliding */
1773 gtk_toolbar_begin_sliding (toolbar);
1777 /* finally allocate the items */
1778 if (priv->is_sliding)
1780 for (list = priv->content, i = 0; list != NULL; list = list->next, i++)
1782 ToolbarContent *content = list->data;
1784 toolbar_content_set_goal_allocation (content, &(allocations[i]));
1788 for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1790 ToolbarContent *content = list->data;
1792 if (new_states[i] == OVERFLOWN || new_states[i] == NORMAL)
1794 GtkAllocation alloc;
1795 GtkAllocation start_allocation = { 0, };
1796 GtkAllocation goal_allocation;
1798 if (priv->is_sliding)
1800 toolbar_content_get_start_allocation (content, &start_allocation);
1801 toolbar_content_get_goal_allocation (content, &goal_allocation);
1803 compute_intermediate_allocation (toolbar,
1808 priv->need_sync = TRUE;
1812 alloc = allocations[i];
1815 if (alloc.width <= 0 || alloc.height <= 0)
1817 toolbar_content_set_child_visible (content, toolbar, FALSE);
1821 if (!rect_within (&alloc, &item_area))
1823 toolbar_content_set_child_visible (content, toolbar, FALSE);
1824 toolbar_content_size_allocate (content, &alloc);
1828 toolbar_content_set_child_visible (content, toolbar, TRUE);
1829 toolbar_content_size_allocate (content, &alloc);
1835 toolbar_content_set_child_visible (content, toolbar, FALSE);
1838 toolbar_content_set_state (content, new_states[i]);
1841 if (priv->menu && priv->need_rebuild)
1842 rebuild_menu (toolbar);
1846 gtk_widget_size_allocate (GTK_WIDGET (priv->arrow_button),
1848 gtk_widget_show (GTK_WIDGET (priv->arrow_button));
1852 gtk_widget_hide (GTK_WIDGET (priv->arrow_button));
1854 if (priv->menu && gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
1855 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
1858 g_free (allocations);
1859 g_free (new_states);
1863 gtk_toolbar_update_button_relief (GtkToolbar *toolbar)
1865 GtkToolbarPrivate *priv = toolbar->priv;
1866 GtkReliefStyle relief;
1868 relief = get_button_relief (toolbar);
1870 if (relief != gtk_button_get_relief (GTK_BUTTON (priv->arrow_button)))
1872 gtk_toolbar_reconfigured (toolbar);
1874 gtk_button_set_relief (GTK_BUTTON (priv->arrow_button), relief);
1879 gtk_toolbar_style_updated (GtkWidget *widget)
1881 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1882 GtkToolbarPrivate *priv = toolbar->priv;
1884 GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->style_updated (widget);
1886 priv->max_homogeneous_pixels = -1;
1888 if (gtk_widget_get_realized (widget))
1889 gtk_style_context_set_background (gtk_widget_get_style_context (widget),
1890 gtk_widget_get_window (widget));
1892 gtk_toolbar_update_button_relief (GTK_TOOLBAR (widget));
1896 gtk_toolbar_list_children_in_focus_order (GtkToolbar *toolbar,
1897 GtkDirectionType dir)
1899 GtkToolbarPrivate *priv = toolbar->priv;
1900 GList *result = NULL;
1904 /* generate list of children in reverse logical order */
1906 for (list = priv->content; list != NULL; list = list->next)
1908 ToolbarContent *content = list->data;
1911 widget = toolbar_content_get_widget (content);
1914 result = g_list_prepend (result, widget);
1917 result = g_list_prepend (result, priv->arrow_button);
1919 rtl = (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL);
1921 /* move in logical order when
1923 * - dir is TAB_FORWARD
1925 * - in RTL mode and moving left or up
1927 * - in LTR mode and moving right or down
1929 if (dir == GTK_DIR_TAB_FORWARD ||
1930 (rtl && (dir == GTK_DIR_UP || dir == GTK_DIR_LEFT)) ||
1931 (!rtl && (dir == GTK_DIR_DOWN || dir == GTK_DIR_RIGHT)))
1933 result = g_list_reverse (result);
1940 gtk_toolbar_focus_home_or_end (GtkToolbar *toolbar,
1941 gboolean focus_home)
1943 GList *children, *list;
1944 GtkDirectionType dir = focus_home? GTK_DIR_RIGHT : GTK_DIR_LEFT;
1946 children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);
1948 if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
1950 children = g_list_reverse (children);
1952 dir = (dir == GTK_DIR_RIGHT)? GTK_DIR_LEFT : GTK_DIR_RIGHT;
1955 for (list = children; list != NULL; list = list->next)
1957 GtkWidget *child = list->data;
1959 if (gtk_container_get_focus_child (GTK_CONTAINER (toolbar)) == child)
1962 if (gtk_widget_get_mapped (child) && gtk_widget_child_focus (child, dir))
1966 g_list_free (children);
1971 /* Keybinding handler. This function is called when the user presses
1972 * Ctrl TAB or an arrow key.
1975 gtk_toolbar_move_focus (GtkWidget *widget,
1976 GtkDirectionType dir)
1978 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1979 GtkContainer *container = GTK_CONTAINER (toolbar);
1980 GtkWidget *focus_child;
1982 gboolean try_focus = FALSE;
1985 focus_child = gtk_container_get_focus_child (container);
1987 if (focus_child && gtk_widget_child_focus (focus_child, dir))
1990 children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);
1992 for (list = children; list != NULL; list = list->next)
1994 GtkWidget *child = list->data;
1996 if (try_focus && gtk_widget_get_mapped (child) && gtk_widget_child_focus (child, dir))
1999 if (child == focus_child)
2003 g_list_free (children);
2006 /* The focus handler for the toolbar. It called when the user presses
2007 * TAB or otherwise tries to focus the toolbar.
2010 gtk_toolbar_focus (GtkWidget *widget,
2011 GtkDirectionType dir)
2013 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
2014 GList *children, *list;
2015 gboolean result = FALSE;
2017 /* if focus is already somewhere inside the toolbar then return FALSE.
2018 * The only way focus can stay inside the toolbar is when the user presses
2019 * arrow keys or Ctrl TAB (both of which are handled by the
2020 * gtk_toolbar_move_focus() keybinding function.
2022 if (gtk_container_get_focus_child (GTK_CONTAINER (widget)))
2025 children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);
2027 for (list = children; list != NULL; list = list->next)
2029 GtkWidget *child = list->data;
2031 if (gtk_widget_get_mapped (child) && gtk_widget_child_focus (child, dir))
2038 g_list_free (children);
2043 static GtkSettings *
2044 toolbar_get_settings (GtkToolbar *toolbar)
2046 return toolbar->priv->settings;
2050 style_change_notify (GtkToolbar *toolbar)
2052 GtkToolbarPrivate *priv = toolbar->priv;
2054 if (!priv->style_set)
2056 /* pretend it was set, then unset, thus reverting to new default */
2057 priv->style_set = TRUE;
2058 gtk_toolbar_unset_style (toolbar);
2063 icon_size_change_notify (GtkToolbar *toolbar)
2065 GtkToolbarPrivate *priv = toolbar->priv;
2067 if (!priv->icon_size_set)
2069 /* pretend it was set, then unset, thus reverting to new default */
2070 priv->icon_size_set = TRUE;
2071 gtk_toolbar_unset_icon_size (toolbar);
2076 animation_change_notify (GtkToolbar *toolbar)
2078 GtkToolbarPrivate *priv = toolbar->priv;
2079 GtkSettings *settings = toolbar_get_settings (toolbar);
2083 g_object_get (settings,
2084 "gtk-enable-animations", &animation,
2087 animation = DEFAULT_ANIMATION_STATE;
2089 priv->animation = animation;
2093 settings_change_notify (GtkSettings *settings,
2094 const GParamSpec *pspec,
2095 GtkToolbar *toolbar)
2097 if (! strcmp (pspec->name, "gtk-toolbar-style"))
2098 style_change_notify (toolbar);
2099 else if (! strcmp (pspec->name, "gtk-toolbar-icon-size"))
2100 icon_size_change_notify (toolbar);
2101 else if (! strcmp (pspec->name, "gtk-enable-animations"))
2102 animation_change_notify (toolbar);
2106 gtk_toolbar_screen_changed (GtkWidget *widget,
2107 GdkScreen *previous_screen)
2109 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
2110 GtkToolbarPrivate *priv = toolbar->priv;
2111 GtkSettings *old_settings = toolbar_get_settings (toolbar);
2112 GtkSettings *settings;
2114 if (gtk_widget_has_screen (GTK_WIDGET (toolbar)))
2115 settings = gtk_widget_get_settings (GTK_WIDGET (toolbar));
2119 if (settings == old_settings)
2124 g_signal_handler_disconnect (old_settings, priv->settings_connection);
2126 g_object_unref (old_settings);
2131 priv->settings_connection =
2132 g_signal_connect (settings, "notify",
2133 G_CALLBACK (settings_change_notify),
2136 priv->settings = g_object_ref (settings);
2139 priv->settings = NULL;
2141 style_change_notify (toolbar);
2142 icon_size_change_notify (toolbar);
2143 animation_change_notify (toolbar);
2147 find_drop_index (GtkToolbar *toolbar,
2151 GtkToolbarPrivate *priv = toolbar->priv;
2152 GList *interesting_content;
2154 GtkOrientation orientation;
2155 GtkTextDirection direction;
2156 gint best_distance = G_MAXINT;
2160 ToolbarContent *best_content;
2161 GtkAllocation allocation;
2163 /* list items we care about wrt. drag and drop */
2164 interesting_content = NULL;
2165 for (list = priv->content; list != NULL; list = list->next)
2167 ToolbarContent *content = list->data;
2169 if (toolbar_content_get_state (content) == NORMAL)
2170 interesting_content = g_list_prepend (interesting_content, content);
2172 interesting_content = g_list_reverse (interesting_content);
2174 if (!interesting_content)
2177 orientation = priv->orientation;
2178 direction = gtk_widget_get_direction (GTK_WIDGET (toolbar));
2180 /* distance to first interesting item */
2181 best_content = interesting_content->data;
2182 toolbar_content_get_allocation (best_content, &allocation);
2184 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2188 if (direction == GTK_TEXT_DIR_LTR)
2191 pos = allocation.x + allocation.width;
2199 best_content = NULL;
2200 best_distance = ABS (pos - cursor);
2202 /* distance to far end of each item */
2203 for (list = interesting_content; list != NULL; list = list->next)
2205 ToolbarContent *content = list->data;
2207 toolbar_content_get_allocation (content, &allocation);
2209 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2211 if (direction == GTK_TEXT_DIR_LTR)
2212 pos = allocation.x + allocation.width;
2218 pos = allocation.y + allocation.height;
2221 distance = ABS (pos - cursor);
2223 if (distance < best_distance)
2225 best_distance = distance;
2226 best_content = content;
2230 g_list_free (interesting_content);
2235 return g_list_index (priv->content, best_content) + 1;
2239 reset_all_placeholders (GtkToolbar *toolbar)
2241 GtkToolbarPrivate *priv = toolbar->priv;
2244 for (list = priv->content; list != NULL; list = list->next)
2246 ToolbarContent *content = list->data;
2247 if (toolbar_content_is_placeholder (content))
2248 toolbar_content_set_disappearing (content, TRUE);
2253 physical_to_logical (GtkToolbar *toolbar,
2256 GtkToolbarPrivate *priv = toolbar->priv;
2260 g_assert (physical >= 0);
2263 for (list = priv->content; list && physical > 0; list = list->next)
2265 ToolbarContent *content = list->data;
2267 if (!toolbar_content_is_placeholder (content))
2272 g_assert (physical == 0);
2278 logical_to_physical (GtkToolbar *toolbar,
2281 GtkToolbarPrivate *priv = toolbar->priv;
2285 g_assert (logical >= 0);
2288 for (list = priv->content; list; list = list->next)
2290 ToolbarContent *content = list->data;
2292 if (!toolbar_content_is_placeholder (content))
2302 g_assert (logical == 0);
2308 * gtk_toolbar_set_drop_highlight_item:
2309 * @toolbar: a #GtkToolbar
2310 * @tool_item: (allow-none): a #GtkToolItem, or %NULL to turn of highlighting
2311 * @index_: a position on @toolbar
2313 * Highlights @toolbar to give an idea of what it would look like
2314 * if @item was added to @toolbar at the position indicated by @index_.
2315 * If @item is %NULL, highlighting is turned off. In that case @index_
2318 * The @tool_item passed to this function must not be part of any widget
2319 * hierarchy. When an item is set as drop highlight item it can not
2320 * added to any widget hierarchy or used as highlight item for another
2326 gtk_toolbar_set_drop_highlight_item (GtkToolbar *toolbar,
2327 GtkToolItem *tool_item,
2330 ToolbarContent *content;
2331 GtkToolbarPrivate *priv;
2333 GtkRequisition requisition;
2334 GtkRequisition old_requisition;
2335 gboolean restart_sliding;
2337 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2338 g_return_if_fail (tool_item == NULL || GTK_IS_TOOL_ITEM (tool_item));
2340 priv = toolbar->priv;
2344 if (priv->highlight_tool_item)
2346 gtk_widget_unparent (GTK_WIDGET (priv->highlight_tool_item));
2347 g_object_unref (priv->highlight_tool_item);
2348 priv->highlight_tool_item = NULL;
2351 reset_all_placeholders (toolbar);
2352 gtk_toolbar_begin_sliding (toolbar);
2356 n_items = gtk_toolbar_get_n_items (toolbar);
2357 if (index_ < 0 || index_ > n_items)
2360 if (tool_item != priv->highlight_tool_item)
2362 if (priv->highlight_tool_item)
2363 g_object_unref (priv->highlight_tool_item);
2365 g_object_ref_sink (tool_item);
2367 priv->highlight_tool_item = tool_item;
2369 gtk_widget_set_parent (GTK_WIDGET (priv->highlight_tool_item),
2370 GTK_WIDGET (toolbar));
2373 index_ = logical_to_physical (toolbar, index_);
2375 content = g_list_nth_data (priv->content, index_);
2379 ToolbarContent *prev_content;
2381 prev_content = g_list_nth_data (priv->content, index_ - 1);
2383 if (prev_content && toolbar_content_is_placeholder (prev_content))
2384 content = prev_content;
2387 if (!content || !toolbar_content_is_placeholder (content))
2389 GtkWidget *placeholder;
2391 placeholder = GTK_WIDGET (gtk_separator_tool_item_new ());
2393 content = toolbar_content_new_tool_item (toolbar,
2394 GTK_TOOL_ITEM (placeholder),
2396 gtk_widget_show (placeholder);
2400 g_assert (toolbar_content_is_placeholder (content));
2402 gtk_widget_get_preferred_size (GTK_WIDGET (priv->highlight_tool_item),
2403 &requisition, NULL);
2405 toolbar_content_set_expand (content, gtk_tool_item_get_expand (tool_item));
2407 restart_sliding = FALSE;
2408 toolbar_content_size_request (content, toolbar, &old_requisition);
2409 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2411 requisition.height = -1;
2412 if (requisition.width != old_requisition.width)
2413 restart_sliding = TRUE;
2417 requisition.width = -1;
2418 if (requisition.height != old_requisition.height)
2419 restart_sliding = TRUE;
2422 if (toolbar_content_disappearing (content))
2423 restart_sliding = TRUE;
2425 reset_all_placeholders (toolbar);
2426 toolbar_content_set_disappearing (content, FALSE);
2428 toolbar_content_set_size_request (content,
2429 requisition.width, requisition.height);
2431 if (restart_sliding)
2432 gtk_toolbar_begin_sliding (toolbar);
2436 gtk_toolbar_get_child_property (GtkContainer *container,
2442 GtkToolItem *item = GTK_TOOL_ITEM (child);
2444 switch (property_id)
2446 case CHILD_PROP_HOMOGENEOUS:
2447 g_value_set_boolean (value, gtk_tool_item_get_homogeneous (item));
2450 case CHILD_PROP_EXPAND:
2451 g_value_set_boolean (value, gtk_tool_item_get_expand (item));
2455 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
2461 gtk_toolbar_set_child_property (GtkContainer *container,
2464 const GValue *value,
2467 switch (property_id)
2469 case CHILD_PROP_HOMOGENEOUS:
2470 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (child), g_value_get_boolean (value));
2473 case CHILD_PROP_EXPAND:
2474 gtk_tool_item_set_expand (GTK_TOOL_ITEM (child), g_value_get_boolean (value));
2478 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
2484 gtk_toolbar_show_all (GtkWidget *widget)
2486 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
2487 GtkToolbarPrivate *priv = toolbar->priv;
2490 for (list = priv->content; list != NULL; list = list->next)
2492 ToolbarContent *content = list->data;
2494 toolbar_content_show_all (content);
2497 gtk_widget_show (widget);
2501 gtk_toolbar_add (GtkContainer *container,
2504 GtkToolbar *toolbar = GTK_TOOLBAR (container);
2506 gtk_toolbar_insert (toolbar, GTK_TOOL_ITEM (widget), -1);
2510 gtk_toolbar_remove (GtkContainer *container,
2513 GtkToolbar *toolbar = GTK_TOOLBAR (container);
2514 GtkToolbarPrivate *priv = toolbar->priv;
2515 ToolbarContent *content_to_remove;
2518 content_to_remove = NULL;
2519 for (list = priv->content; list != NULL; list = list->next)
2521 ToolbarContent *content = list->data;
2524 child = toolbar_content_get_widget (content);
2525 if (child && child == widget)
2527 content_to_remove = content;
2532 g_return_if_fail (content_to_remove != NULL);
2534 toolbar_content_remove (content_to_remove, toolbar);
2535 toolbar_content_free (content_to_remove);
2539 gtk_toolbar_forall (GtkContainer *container,
2540 gboolean include_internals,
2541 GtkCallback callback,
2542 gpointer callback_data)
2544 GtkToolbar *toolbar = GTK_TOOLBAR (container);
2545 GtkToolbarPrivate *priv = toolbar->priv;
2548 g_return_if_fail (callback != NULL);
2550 list = priv->content;
2553 ToolbarContent *content = list->data;
2554 GList *next = list->next;
2556 if (include_internals || !toolbar_content_is_placeholder (content))
2558 GtkWidget *child = toolbar_content_get_widget (content);
2561 callback (child, callback_data);
2567 if (include_internals && priv->arrow_button)
2568 callback (priv->arrow_button, callback_data);
2572 gtk_toolbar_child_type (GtkContainer *container)
2574 return GTK_TYPE_TOOL_ITEM;
2578 gtk_toolbar_reconfigured (GtkToolbar *toolbar)
2580 GtkToolbarPrivate *priv = toolbar->priv;
2583 list = priv->content;
2586 ToolbarContent *content = list->data;
2587 GList *next = list->next;
2589 toolbar_content_toolbar_reconfigured (content, toolbar);
2596 gtk_toolbar_orientation_changed (GtkToolbar *toolbar,
2597 GtkOrientation orientation)
2599 GtkToolbarPrivate *priv = toolbar->priv;
2601 if (priv->orientation != orientation)
2603 priv->orientation = orientation;
2605 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2606 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2608 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
2610 gtk_toolbar_reconfigured (toolbar);
2612 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
2613 g_object_notify (G_OBJECT (toolbar), "orientation");
2618 gtk_toolbar_real_style_changed (GtkToolbar *toolbar,
2619 GtkToolbarStyle style)
2621 GtkToolbarPrivate *priv = toolbar->priv;
2623 if (priv->style != style)
2625 priv->style = style;
2627 gtk_toolbar_reconfigured (toolbar);
2629 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
2630 g_object_notify (G_OBJECT (toolbar), "toolbar-style");
2635 menu_position_func (GtkMenu *menu,
2641 GtkAllocation allocation;
2642 GtkToolbar *toolbar = GTK_TOOLBAR (user_data);
2643 GtkToolbarPrivate *priv = toolbar->priv;
2645 GtkRequisition menu_req;
2646 GdkRectangle monitor;
2650 gtk_widget_get_preferred_size (priv->arrow_button,
2652 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
2655 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
2656 monitor_num = gdk_screen_get_monitor_at_window (screen,
2657 gtk_widget_get_window (priv->arrow_button));
2658 if (monitor_num < 0)
2660 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2662 gtk_widget_get_allocation (priv->arrow_button, &allocation);
2664 gdk_window_get_origin (gtk_button_get_event_window (GTK_BUTTON (priv->arrow_button)), x, y);
2665 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2667 if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_LTR)
2668 *x += allocation.width - req.width;
2670 *x += req.width - menu_req.width;
2672 if ((*y + allocation.height + menu_req.height) <= monitor.y + monitor.height)
2673 *y += allocation.height;
2674 else if ((*y - menu_req.height) >= monitor.y)
2675 *y -= menu_req.height;
2676 else if (monitor.y + monitor.height - (*y + allocation.height) > *y)
2677 *y += allocation.height;
2679 *y -= menu_req.height;
2683 if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_LTR)
2684 *x += allocation.width;
2686 *x -= menu_req.width;
2688 if (*y + menu_req.height > monitor.y + monitor.height &&
2689 *y + allocation.height - monitor.y > monitor.y + monitor.height - *y)
2690 *y += allocation.height - menu_req.height;
2697 show_menu (GtkToolbar *toolbar,
2698 GdkEventButton *event)
2700 GtkToolbarPrivate *priv = toolbar->priv;
2702 rebuild_menu (toolbar);
2704 gtk_widget_show_all (GTK_WIDGET (priv->menu));
2706 gtk_menu_popup (priv->menu, NULL, NULL,
2707 menu_position_func, toolbar,
2708 event? event->button : 0,
2709 event? event->time : gtk_get_current_event_time());
2713 gtk_toolbar_arrow_button_clicked (GtkWidget *button,
2714 GtkToolbar *toolbar)
2716 GtkToolbarPrivate *priv = toolbar->priv;
2718 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->arrow_button)) &&
2719 (!priv->menu || !gtk_widget_get_visible (GTK_WIDGET (priv->menu))))
2721 /* We only get here when the button is clicked with the keyboard,
2722 * because mouse button presses result in the menu being shown so
2723 * that priv->menu would be non-NULL and visible.
2725 show_menu (toolbar, NULL);
2726 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
2731 gtk_toolbar_arrow_button_press (GtkWidget *button,
2732 GdkEventButton *event,
2733 GtkToolbar *toolbar)
2735 show_menu (toolbar, event);
2736 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
2742 gtk_toolbar_button_press (GtkWidget *toolbar,
2743 GdkEventButton *event)
2747 if (_gtk_button_event_triggers_context_menu (event))
2749 gboolean return_value;
2751 g_signal_emit (toolbar, toolbar_signals[POPUP_CONTEXT_MENU], 0,
2752 (int)event->x_root, (int)event->y_root, event->button,
2755 return return_value;
2758 window = gtk_widget_get_toplevel (toolbar);
2762 gboolean window_drag = FALSE;
2764 gtk_widget_style_get (toolbar,
2765 "window-dragging", &window_drag,
2770 gtk_window_begin_move_drag (GTK_WINDOW (window),
2784 gtk_toolbar_popup_menu (GtkWidget *toolbar)
2786 gboolean return_value;
2787 /* This function is the handler for the "popup menu" keybinding,
2788 * ie., it is called when the user presses Shift F10
2790 g_signal_emit (toolbar, toolbar_signals[POPUP_CONTEXT_MENU], 0,
2791 -1, -1, -1, &return_value);
2793 return return_value;
2799 * Creates a new toolbar.
2801 * Return Value: the newly-created toolbar.
2804 gtk_toolbar_new (void)
2806 GtkToolbar *toolbar;
2808 toolbar = g_object_new (GTK_TYPE_TOOLBAR, NULL);
2810 return GTK_WIDGET (toolbar);
2814 * gtk_toolbar_insert:
2815 * @toolbar: a #GtkToolbar
2816 * @item: a #GtkToolItem
2817 * @pos: the position of the new item
2819 * Insert a #GtkToolItem into the toolbar at position @pos. If @pos is
2820 * 0 the item is prepended to the start of the toolbar. If @pos is
2821 * negative, the item is appended to the end of the toolbar.
2826 gtk_toolbar_insert (GtkToolbar *toolbar,
2830 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2831 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2834 pos = logical_to_physical (toolbar, pos);
2836 toolbar_content_new_tool_item (toolbar, item, FALSE, pos);
2840 * gtk_toolbar_get_item_index:
2841 * @toolbar: a #GtkToolbar
2842 * @item: a #GtkToolItem that is a child of @toolbar
2844 * Returns the position of @item on the toolbar, starting from 0.
2845 * It is an error if @item is not a child of the toolbar.
2847 * Return value: the position of item on the toolbar.
2852 gtk_toolbar_get_item_index (GtkToolbar *toolbar,
2855 GtkToolbarPrivate *priv;
2859 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), -1);
2860 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2861 g_return_val_if_fail (gtk_widget_get_parent (GTK_WIDGET (item)) == GTK_WIDGET (toolbar), -1);
2863 priv = toolbar->priv;
2866 for (list = priv->content; list != NULL; list = list->next)
2868 ToolbarContent *content = list->data;
2871 widget = toolbar_content_get_widget (content);
2873 if (item == GTK_TOOL_ITEM (widget))
2879 return physical_to_logical (toolbar, n);
2883 * gtk_toolbar_set_style:
2884 * @toolbar: a #GtkToolbar.
2885 * @style: the new style for @toolbar.
2887 * Alters the view of @toolbar to display either icons only, text only, or both.
2890 gtk_toolbar_set_style (GtkToolbar *toolbar,
2891 GtkToolbarStyle style)
2893 GtkToolbarPrivate *priv;
2895 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2897 priv = toolbar->priv;
2899 priv->style_set = TRUE;
2900 g_signal_emit (toolbar, toolbar_signals[STYLE_CHANGED], 0, style);
2904 * gtk_toolbar_get_style:
2905 * @toolbar: a #GtkToolbar
2907 * Retrieves whether the toolbar has text, icons, or both . See
2908 * gtk_toolbar_set_style().
2910 * Return value: the current style of @toolbar
2913 gtk_toolbar_get_style (GtkToolbar *toolbar)
2915 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), DEFAULT_TOOLBAR_STYLE);
2917 return toolbar->priv->style;
2921 * gtk_toolbar_unset_style:
2922 * @toolbar: a #GtkToolbar
2924 * Unsets a toolbar style set with gtk_toolbar_set_style(), so that
2925 * user preferences will be used to determine the toolbar style.
2928 gtk_toolbar_unset_style (GtkToolbar *toolbar)
2930 GtkToolbarPrivate *priv;
2931 GtkToolbarStyle style;
2933 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2935 priv = toolbar->priv;
2937 if (priv->style_set)
2939 GtkSettings *settings = toolbar_get_settings (toolbar);
2942 g_object_get (settings,
2943 "gtk-toolbar-style", &style,
2946 style = DEFAULT_TOOLBAR_STYLE;
2948 if (style != priv->style)
2949 g_signal_emit (toolbar, toolbar_signals[STYLE_CHANGED], 0, style);
2951 priv->style_set = FALSE;
2956 * gtk_toolbar_get_n_items:
2957 * @toolbar: a #GtkToolbar
2959 * Returns the number of items on the toolbar.
2961 * Return value: the number of items on the toolbar
2966 gtk_toolbar_get_n_items (GtkToolbar *toolbar)
2968 GtkToolbarPrivate *priv;
2970 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), -1);
2972 priv = toolbar->priv;
2974 return physical_to_logical (toolbar, g_list_length (priv->content));
2978 * gtk_toolbar_get_nth_item:
2979 * @toolbar: a #GtkToolbar
2980 * @n: A position on the toolbar
2982 * Returns the @n<!-- -->'th item on @toolbar, or %NULL if the
2983 * toolbar does not contain an @n<!-- -->'th item.
2985 * Return value: (transfer none): The @n<!-- -->'th #GtkToolItem on @toolbar,
2986 * or %NULL if there isn't an @n<!-- -->'th item.
2991 gtk_toolbar_get_nth_item (GtkToolbar *toolbar,
2994 GtkToolbarPrivate *priv;
2995 ToolbarContent *content;
2998 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), NULL);
3000 priv = toolbar->priv;
3002 n_items = gtk_toolbar_get_n_items (toolbar);
3004 if (n < 0 || n >= n_items)
3007 content = g_list_nth_data (priv->content, logical_to_physical (toolbar, n));
3010 g_assert (!toolbar_content_is_placeholder (content));
3012 return GTK_TOOL_ITEM (toolbar_content_get_widget (content));
3016 * gtk_toolbar_get_icon_size:
3017 * @toolbar: a #GtkToolbar
3019 * Retrieves the icon size for the toolbar. See gtk_toolbar_set_icon_size().
3021 * Return value: (type int): the current icon size for the icons on
3025 gtk_toolbar_get_icon_size (GtkToolbar *toolbar)
3027 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), DEFAULT_ICON_SIZE);
3029 return toolbar->priv->icon_size;
3033 * gtk_toolbar_get_relief_style:
3034 * @toolbar: a #GtkToolbar
3036 * Returns the relief style of buttons on @toolbar. See
3037 * gtk_button_set_relief().
3039 * Return value: The relief style of buttons on @toolbar.
3044 gtk_toolbar_get_relief_style (GtkToolbar *toolbar)
3046 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), GTK_RELIEF_NONE);
3048 return get_button_relief (toolbar);
3052 * gtk_toolbar_set_show_arrow:
3053 * @toolbar: a #GtkToolbar
3054 * @show_arrow: Whether to show an overflow menu
3056 * Sets whether to show an overflow menu when
3057 * @toolbar doesn't have room for all items on it. If %TRUE,
3058 * items that there are not room are available through an
3064 gtk_toolbar_set_show_arrow (GtkToolbar *toolbar,
3065 gboolean show_arrow)
3067 GtkToolbarPrivate *priv;
3069 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3071 priv = toolbar->priv;
3073 show_arrow = show_arrow != FALSE;
3075 if (priv->show_arrow != show_arrow)
3077 priv->show_arrow = show_arrow;
3079 if (!priv->show_arrow)
3080 gtk_widget_hide (priv->arrow_button);
3082 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3083 g_object_notify (G_OBJECT (toolbar), "show-arrow");
3088 * gtk_toolbar_get_show_arrow:
3089 * @toolbar: a #GtkToolbar
3091 * Returns whether the toolbar has an overflow menu.
3092 * See gtk_toolbar_set_show_arrow().
3094 * Return value: %TRUE if the toolbar has an overflow menu.
3099 gtk_toolbar_get_show_arrow (GtkToolbar *toolbar)
3101 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
3103 return toolbar->priv->show_arrow;
3107 * gtk_toolbar_get_drop_index:
3108 * @toolbar: a #GtkToolbar
3109 * @x: x coordinate of a point on the toolbar
3110 * @y: y coordinate of a point on the toolbar
3112 * Returns the position corresponding to the indicated point on
3113 * @toolbar. This is useful when dragging items to the toolbar:
3114 * this function returns the position a new item should be
3117 * @x and @y are in @toolbar coordinates.
3119 * Return value: The position corresponding to the point (@x, @y) on the toolbar.
3124 gtk_toolbar_get_drop_index (GtkToolbar *toolbar,
3128 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), -1);
3130 return physical_to_logical (toolbar, find_drop_index (toolbar, x, y));
3134 gtk_toolbar_dispose (GObject *object)
3136 GtkToolbar *toolbar = GTK_TOOLBAR (object);
3137 GtkToolbarPrivate *priv = toolbar->priv;
3139 if (priv->arrow_button)
3141 gtk_widget_unparent (priv->arrow_button);
3142 priv->arrow_button = NULL;
3147 g_signal_handlers_disconnect_by_func (priv->menu,
3148 menu_deactivated, toolbar);
3149 gtk_widget_destroy (GTK_WIDGET (priv->menu));
3153 G_OBJECT_CLASS (gtk_toolbar_parent_class)->dispose (object);
3157 gtk_toolbar_finalize (GObject *object)
3159 GtkToolbar *toolbar = GTK_TOOLBAR (object);
3160 GtkToolbarPrivate *priv = toolbar->priv;
3162 if (priv->sibling_path != NULL)
3163 gtk_widget_path_unref (priv->sibling_path);
3165 g_list_free_full (priv->content, (GDestroyNotify)toolbar_content_free);
3167 g_timer_destroy (priv->timer);
3170 g_source_remove (priv->idle_id);
3172 G_OBJECT_CLASS (gtk_toolbar_parent_class)->finalize (object);
3176 * gtk_toolbar_set_icon_size:
3177 * @toolbar: A #GtkToolbar
3178 * @icon_size: (type int): The #GtkIconSize that stock icons in the
3179 * toolbar shall have.
3181 * This function sets the size of stock icons in the toolbar. You
3182 * can call it both before you add the icons and after they've been
3183 * added. The size you set will override user preferences for the default
3186 * This should only be used for special-purpose toolbars, normal
3187 * application toolbars should respect the user preferences for the
3191 gtk_toolbar_set_icon_size (GtkToolbar *toolbar,
3192 GtkIconSize icon_size)
3194 GtkToolbarPrivate *priv;
3196 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3197 g_return_if_fail (icon_size != GTK_ICON_SIZE_INVALID);
3199 priv = toolbar->priv;
3201 if (!priv->icon_size_set)
3203 priv->icon_size_set = TRUE;
3204 g_object_notify (G_OBJECT (toolbar), "icon-size-set");
3207 if (priv->icon_size == icon_size)
3210 priv->icon_size = icon_size;
3211 g_object_notify (G_OBJECT (toolbar), "icon-size");
3213 gtk_toolbar_reconfigured (toolbar);
3215 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3219 * gtk_toolbar_unset_icon_size:
3220 * @toolbar: a #GtkToolbar
3222 * Unsets toolbar icon size set with gtk_toolbar_set_icon_size(), so that
3223 * user preferences will be used to determine the icon size.
3226 gtk_toolbar_unset_icon_size (GtkToolbar *toolbar)
3228 GtkToolbarPrivate *priv;
3231 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3233 priv = toolbar->priv;
3235 if (priv->icon_size_set)
3237 GtkSettings *settings = toolbar_get_settings (toolbar);
3241 g_object_get (settings,
3242 "gtk-toolbar-icon-size", &size,
3246 size = DEFAULT_ICON_SIZE;
3248 if (size != priv->icon_size)
3250 gtk_toolbar_set_icon_size (toolbar, size);
3251 g_object_notify (G_OBJECT (toolbar), "icon-size");
3254 priv->icon_size_set = FALSE;
3255 g_object_notify (G_OBJECT (toolbar), "icon-size-set");
3260 * ToolbarContent methods
3268 struct _ToolbarContent
3273 GtkAllocation allocation;
3274 GtkAllocation start_allocation;
3275 GtkAllocation goal_allocation;
3276 guint is_placeholder : 1;
3277 guint disappearing : 1;
3282 toolbar_item_visiblity_notify_cb (GObject *obj,
3286 GtkToolbar *toolbar = user_data;
3288 gtk_toolbar_invalidate_order (toolbar);
3291 static ToolbarContent *
3292 toolbar_content_new_tool_item (GtkToolbar *toolbar,
3294 gboolean is_placeholder,
3297 GtkToolbarPrivate *priv = toolbar->priv;
3298 ToolbarContent *content;
3300 content = g_slice_new0 (ToolbarContent);
3302 content->state = NOT_ALLOCATED;
3303 content->item = item;
3304 content->is_placeholder = is_placeholder;
3306 priv->content = g_list_insert (priv->content, content, pos);
3308 gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (toolbar));
3309 gtk_toolbar_invalidate_order (toolbar);
3311 g_signal_connect (content->item, "notify::visible",
3312 G_CALLBACK (toolbar_item_visiblity_notify_cb), toolbar);
3314 if (!is_placeholder)
3316 priv->num_children++;
3318 gtk_toolbar_stop_sliding (toolbar);
3321 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3322 priv->need_rebuild = TRUE;
3328 toolbar_content_remove (ToolbarContent *content,
3329 GtkToolbar *toolbar)
3331 GtkToolbarPrivate *priv = toolbar->priv;
3333 gtk_toolbar_invalidate_order (toolbar);
3334 gtk_widget_unparent (GTK_WIDGET (content->item));
3336 g_signal_handlers_disconnect_by_func (content->item,
3337 toolbar_item_visiblity_notify_cb,
3340 priv->content = g_list_remove (priv->content, content);
3342 if (!toolbar_content_is_placeholder (content))
3343 priv->num_children--;
3345 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3346 priv->need_rebuild = TRUE;
3350 toolbar_content_free (ToolbarContent *content)
3352 g_slice_free (ToolbarContent, content);
3356 calculate_max_homogeneous_pixels (GtkWidget *widget)
3358 PangoContext *context;
3359 PangoFontMetrics *metrics;
3360 const PangoFontDescription *font_desc;
3361 GtkStyleContext *style_context;
3362 GtkStateFlags state;
3365 context = gtk_widget_get_pango_context (widget);
3366 style_context = gtk_widget_get_style_context (widget);
3367 state = gtk_widget_get_state_flags (widget);
3369 font_desc = gtk_style_context_get_font (style_context, state);
3371 metrics = pango_context_get_metrics (context, font_desc,
3372 pango_context_get_language (context));
3373 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3374 pango_font_metrics_unref (metrics);
3376 return PANGO_PIXELS (MAX_HOMOGENEOUS_N_CHARS * char_width);
3380 toolbar_content_draw (ToolbarContent *content,
3381 GtkContainer *container,
3386 if (content->is_placeholder)
3389 widget = GTK_WIDGET (content->item);
3392 gtk_container_propagate_draw (container, widget, cr);
3396 toolbar_content_visible (ToolbarContent *content,
3397 GtkToolbar *toolbar)
3399 GtkToolbarPrivate *priv = toolbar->priv;
3402 item = content->item;
3404 if (!gtk_widget_get_visible (GTK_WIDGET (item)))
3407 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
3408 gtk_tool_item_get_visible_horizontal (item))
3411 if (priv->orientation == GTK_ORIENTATION_VERTICAL &&
3412 gtk_tool_item_get_visible_vertical (item))
3419 toolbar_content_size_request (ToolbarContent *content,
3420 GtkToolbar *toolbar,
3421 GtkRequisition *requisition)
3423 gtk_widget_get_preferred_size (GTK_WIDGET (content->item),
3425 if (content->is_placeholder &&
3426 content->disappearing)
3428 requisition->width = 0;
3429 requisition->height = 0;
3434 toolbar_content_is_homogeneous (ToolbarContent *content,
3435 GtkToolbar *toolbar)
3437 GtkToolbarPrivate *priv = toolbar->priv;
3438 GtkRequisition requisition;
3441 if (priv->max_homogeneous_pixels < 0)
3443 priv->max_homogeneous_pixels =
3444 calculate_max_homogeneous_pixels (GTK_WIDGET (toolbar));
3447 toolbar_content_size_request (content, toolbar, &requisition);
3449 if (requisition.width > priv->max_homogeneous_pixels)
3452 result = gtk_tool_item_get_homogeneous (content->item) &&
3453 !GTK_IS_SEPARATOR_TOOL_ITEM (content->item);
3455 if (gtk_tool_item_get_is_important (content->item) &&
3456 priv->style == GTK_TOOLBAR_BOTH_HORIZ &&
3457 priv->orientation == GTK_ORIENTATION_HORIZONTAL)
3466 toolbar_content_is_placeholder (ToolbarContent *content)
3468 if (content->is_placeholder)
3475 toolbar_content_disappearing (ToolbarContent *content)
3477 if (content->disappearing)
3484 toolbar_content_get_state (ToolbarContent *content)
3486 return content->state;
3490 toolbar_content_child_visible (ToolbarContent *content)
3492 return gtk_widget_get_child_visible (GTK_WIDGET (content->item));
3496 toolbar_content_get_goal_allocation (ToolbarContent *content,
3497 GtkAllocation *allocation)
3499 *allocation = content->goal_allocation;
3503 toolbar_content_get_allocation (ToolbarContent *content,
3504 GtkAllocation *allocation)
3506 *allocation = content->allocation;
3510 toolbar_content_set_start_allocation (ToolbarContent *content,
3511 GtkAllocation *allocation)
3513 content->start_allocation = *allocation;
3517 toolbar_content_get_expand (ToolbarContent *content)
3519 if (!content->disappearing &&
3520 gtk_tool_item_get_expand (content->item))
3527 toolbar_content_set_goal_allocation (ToolbarContent *content,
3528 GtkAllocation *allocation)
3530 content->goal_allocation = *allocation;
3534 toolbar_content_set_child_visible (ToolbarContent *content,
3535 GtkToolbar *toolbar,
3538 gtk_widget_set_child_visible (GTK_WIDGET (content->item),
3543 toolbar_content_get_start_allocation (ToolbarContent *content,
3544 GtkAllocation *start_allocation)
3546 *start_allocation = content->start_allocation;
3550 toolbar_content_size_allocate (ToolbarContent *content,
3551 GtkAllocation *allocation)
3553 content->allocation = *allocation;
3554 gtk_widget_size_allocate (GTK_WIDGET (content->item),
3559 toolbar_content_set_state (ToolbarContent *content,
3562 content->state = state;
3566 toolbar_content_get_widget (ToolbarContent *content)
3568 return GTK_WIDGET (content->item);
3573 toolbar_content_set_disappearing (ToolbarContent *content,
3574 gboolean disappearing)
3576 content->disappearing = disappearing;
3580 toolbar_content_set_size_request (ToolbarContent *content,
3584 gtk_widget_set_size_request (GTK_WIDGET (content->item),
3589 toolbar_content_toolbar_reconfigured (ToolbarContent *content,
3590 GtkToolbar *toolbar)
3592 gtk_tool_item_toolbar_reconfigured (content->item);
3596 toolbar_content_retrieve_menu_item (ToolbarContent *content)
3598 return gtk_tool_item_retrieve_proxy_menu_item (content->item);
3602 toolbar_content_has_proxy_menu_item (ToolbarContent *content)
3604 GtkWidget *menu_item;
3606 if (content->has_menu == YES)
3608 else if (content->has_menu == NO)
3611 menu_item = toolbar_content_retrieve_menu_item (content);
3613 content->has_menu = menu_item? YES : NO;
3615 return menu_item != NULL;
3619 toolbar_content_set_unknown_menu_status (ToolbarContent *content)
3621 content->has_menu = UNKNOWN;
3625 toolbar_content_is_separator (ToolbarContent *content)
3627 return GTK_IS_SEPARATOR_TOOL_ITEM (content->item);
3631 toolbar_content_set_expand (ToolbarContent *content,
3634 gtk_tool_item_set_expand (content->item, expand);
3638 toolbar_content_show_all (ToolbarContent *content)
3642 widget = toolbar_content_get_widget (content);
3644 gtk_widget_show_all (widget);
3650 static GtkReliefStyle
3651 get_button_relief (GtkToolbar *toolbar)
3653 GtkReliefStyle button_relief = GTK_RELIEF_NORMAL;
3655 gtk_widget_style_get (GTK_WIDGET (toolbar),
3656 "button-relief", &button_relief,
3659 return button_relief;
3663 get_internal_padding (GtkToolbar *toolbar)
3667 gtk_widget_style_get (GTK_WIDGET (toolbar),
3668 "internal-padding", &ipadding,
3675 get_max_child_expand (GtkToolbar *toolbar)
3677 gint mexpand = G_MAXINT;
3679 gtk_widget_style_get (GTK_WIDGET (toolbar),
3680 "max-child-expand", &mexpand,
3685 static GtkShadowType
3686 get_shadow_type (GtkToolbar *toolbar)
3688 GtkShadowType shadow_type;
3690 gtk_widget_style_get (GTK_WIDGET (toolbar),
3691 "shadow-type", &shadow_type,
3697 /* GTK+ internal methods */
3700 _gtk_toolbar_get_default_space_size (void)
3702 return DEFAULT_SPACE_SIZE;
3706 _gtk_toolbar_paint_space_line (GtkWidget *widget,
3707 GtkToolbar *toolbar,
3710 GtkOrientation orientation;
3711 GtkStyleContext *context;
3712 GtkStateFlags state;
3715 const gdouble start_fraction = (SPACE_LINE_START / SPACE_LINE_DIVISION);
3716 const gdouble end_fraction = (SPACE_LINE_END / SPACE_LINE_DIVISION);
3718 g_return_if_fail (GTK_IS_WIDGET (widget));
3720 orientation = toolbar ? toolbar->priv->orientation : GTK_ORIENTATION_HORIZONTAL;
3722 context = gtk_widget_get_style_context (widget);
3723 state = gtk_widget_get_state_flags (widget);
3724 width = gtk_widget_get_allocated_width (widget);
3725 height = gtk_widget_get_allocated_height (widget);
3726 gtk_style_context_get_padding (context, state, &padding);
3728 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3730 gboolean wide_separators;
3731 gint separator_width;
3733 gtk_widget_style_get (widget,
3734 "wide-separators", &wide_separators,
3735 "separator-width", &separator_width,
3738 if (wide_separators)
3739 gtk_render_frame (context, cr,
3740 (width - separator_width) / 2,
3741 height * start_fraction,
3743 height * (end_fraction - start_fraction));
3745 gtk_render_line (context, cr,
3746 (width - padding.left) / 2,
3747 height * start_fraction,
3748 (width - padding.left) / 2,
3749 height * end_fraction);
3753 gboolean wide_separators;
3754 gint separator_height;
3756 gtk_widget_style_get (widget,
3757 "wide-separators", &wide_separators,
3758 "separator-height", &separator_height,
3761 if (wide_separators)
3762 gtk_render_frame (context, cr,
3763 width * start_fraction,
3764 (height - separator_height) / 2,
3765 width * (end_fraction - start_fraction),
3768 gtk_render_line (context, cr,
3769 width * start_fraction,
3770 (height - padding.top) / 2,
3771 width * end_fraction,
3772 (height - padding.top) / 2);
3777 _gtk_toolbar_elide_underscores (const gchar *original)
3780 const gchar *p, *end;
3782 gboolean last_underscore;
3787 len = strlen (original);
3788 q = result = g_malloc (len + 1);
3789 last_underscore = FALSE;
3791 end = original + len;
3792 for (p = original; p < end; p++)
3794 if (!last_underscore && *p == '_')
3795 last_underscore = TRUE;
3798 last_underscore = FALSE;
3799 if (original + 2 <= p && p + 1 <= end &&
3800 p[-2] == '(' && p[-1] == '_' && p[0] != '_' && p[1] == ')')
3811 if (last_underscore)
3820 toolbar_get_icon_size (GtkToolShell *shell)
3822 GtkToolbar *toolbar = GTK_TOOLBAR (shell);
3823 GtkToolbarPrivate *priv = toolbar->priv;
3825 return priv->icon_size;
3828 static GtkOrientation
3829 toolbar_get_orientation (GtkToolShell *shell)
3831 GtkToolbar *toolbar = GTK_TOOLBAR (shell);
3832 GtkToolbarPrivate *priv = toolbar->priv;
3834 return priv->orientation;
3837 static GtkToolbarStyle
3838 toolbar_get_style (GtkToolShell *shell)
3840 GtkToolbar *toolbar = GTK_TOOLBAR (shell);
3841 GtkToolbarPrivate *priv = toolbar->priv;
3846 static GtkReliefStyle
3847 toolbar_get_relief_style (GtkToolShell *shell)
3849 return get_button_relief (GTK_TOOLBAR (shell));
3853 toolbar_rebuild_menu (GtkToolShell *shell)
3855 GtkToolbar *toolbar = GTK_TOOLBAR (shell);
3856 GtkToolbarPrivate *priv = toolbar->priv;
3859 priv->need_rebuild = TRUE;
3861 for (list = priv->content; list != NULL; list = list->next)
3863 ToolbarContent *content = list->data;
3865 toolbar_content_set_unknown_menu_status (content);
3868 gtk_widget_queue_resize (GTK_WIDGET (shell));
3871 typedef struct _CountingData CountingData;
3872 struct _CountingData {
3880 count_widget_position (GtkWidget *widget,
3883 CountingData *count = data;
3885 if (!gtk_widget_get_visible (widget))
3888 if (count->widget == widget)
3889 count->found = TRUE;
3890 else if (count->found)
3897 gtk_toolbar_get_visible_position (GtkToolbar *toolbar,
3900 CountingData count = { child, FALSE, 0, 0 };
3902 if (child == (GtkWidget*)toolbar->priv->highlight_tool_item)
3905 /* foreach iterates in visible order */
3906 gtk_container_forall (GTK_CONTAINER (toolbar),
3907 count_widget_position,
3910 g_assert (count.found);
3912 if (toolbar->priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
3913 gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
3916 return count.before;
3920 add_widget_to_path (gpointer data,
3923 GtkWidget *widget = data;
3924 GtkWidgetPath *path = user_data;
3926 if (gtk_widget_get_visible (widget))
3927 gtk_widget_path_append_for_widget (path, widget);
3930 static GtkWidgetPath *
3931 gtk_toolbar_get_path_for_child (GtkContainer *container,
3934 GtkWidgetPath *path;
3935 GtkToolbar *toolbar;
3936 GtkToolbarPrivate *priv;
3939 toolbar = GTK_TOOLBAR (container);
3940 priv = toolbar->priv;
3942 if (priv->sibling_path == NULL)
3946 /* build a path for all the visible children;
3947 * get_children works in visible order
3949 priv->sibling_path = gtk_widget_path_new ();
3950 children = gtk_container_get_children (container);
3952 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
3953 gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
3954 children = g_list_reverse (children);
3956 g_list_foreach (children, add_widget_to_path, priv->sibling_path);
3957 g_list_free (children);
3960 path = gtk_widget_path_copy (gtk_widget_get_path (GTK_WIDGET (container)));
3961 if (gtk_widget_get_visible (child))
3963 vis_index = gtk_toolbar_get_visible_position (toolbar, child);
3965 if (vis_index < gtk_widget_path_length (priv->sibling_path))
3966 gtk_widget_path_append_with_siblings (path,
3970 gtk_widget_path_append_for_widget (path, child);
3973 gtk_widget_path_append_for_widget (path, child);
3979 gtk_toolbar_invalidate_order (GtkToolbar *toolbar)
3981 GtkToolbarPrivate *priv = toolbar->priv;
3983 if (priv->sibling_path != NULL)
3985 gtk_widget_path_unref (priv->sibling_path);
3986 priv->sibling_path = NULL;
3988 gtk_container_foreach (GTK_CONTAINER (toolbar),
3989 (GtkCallback) gtk_widget_reset_style,