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, see <http://www.gnu.org/licenses/>.
24 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GTK+ Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
36 #include "gtktoolbar.h"
39 #include "gtkbindings.h"
40 #include "gtkcontainerprivate.h"
44 #include "gtkmarshalers.h"
46 #include "gtkorientable.h"
47 #include "gtkorientableprivate.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"
58 #include "gtkwidgetpath.h"
59 #include "gtkwidgetprivate.h"
64 * @Short_description: Create bars of buttons and other widgets
66 * @See_also: #GtkToolItem
68 * A toolbar is created with a call to gtk_toolbar_new().
70 * A toolbar can contain instances of a subclass of #GtkToolItem. To add
71 * a #GtkToolItem to the a toolbar, use gtk_toolbar_insert(). To remove
72 * an item from the toolbar use gtk_container_remove(). To add a button
73 * to the toolbar, add an instance of #GtkToolButton.
75 * Toolbar items can be visually grouped by adding instances of
76 * #GtkSeparatorToolItem to the toolbar. If the GtkToolbar child property
77 * "expand" is #TRUE and the property #GtkSeparatorToolItem:draw is set to
78 * #FALSE, the effect is to force all following items to the end of the toolbar.
80 * Creating a context menu for the toolbar can be done by connecting to
81 * the #GtkToolbar::popup-context-menu signal.
85 typedef struct _ToolbarContent ToolbarContent;
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 gulong settings_connection;
126 gint button_maxw; /* maximum width of homogeneous children */
127 gint button_maxh; /* maximum height of homogeneous children */
128 gint max_homogeneous_pixels;
131 GtkOrientation orientation;
134 guint icon_size_set : 1;
135 guint is_sliding : 1;
136 guint need_rebuild : 1; /* whether the overflow menu should be regenerated */
138 guint show_arrow : 1;
153 /* Child properties */
157 CHILD_PROP_HOMOGENEOUS
177 static void gtk_toolbar_set_property (GObject *object,
181 static void gtk_toolbar_get_property (GObject *object,
185 static gint gtk_toolbar_draw (GtkWidget *widget,
187 static void gtk_toolbar_realize (GtkWidget *widget);
188 static void gtk_toolbar_unrealize (GtkWidget *widget);
189 static void gtk_toolbar_get_preferred_width (GtkWidget *widget,
192 static void gtk_toolbar_get_preferred_height (GtkWidget *widget,
196 static void gtk_toolbar_size_allocate (GtkWidget *widget,
197 GtkAllocation *allocation);
198 static void gtk_toolbar_style_updated (GtkWidget *widget);
199 static gboolean gtk_toolbar_focus (GtkWidget *widget,
200 GtkDirectionType dir);
201 static void gtk_toolbar_move_focus (GtkWidget *widget,
202 GtkDirectionType dir);
203 static void gtk_toolbar_screen_changed (GtkWidget *widget,
204 GdkScreen *previous_screen);
205 static void gtk_toolbar_map (GtkWidget *widget);
206 static void gtk_toolbar_unmap (GtkWidget *widget);
207 static void gtk_toolbar_set_child_property (GtkContainer *container,
212 static void gtk_toolbar_get_child_property (GtkContainer *container,
217 static void gtk_toolbar_finalize (GObject *object);
218 static void gtk_toolbar_dispose (GObject *object);
219 static void gtk_toolbar_show_all (GtkWidget *widget);
220 static void gtk_toolbar_add (GtkContainer *container,
222 static void gtk_toolbar_remove (GtkContainer *container,
224 static void gtk_toolbar_forall (GtkContainer *container,
225 gboolean include_internals,
226 GtkCallback callback,
227 gpointer callback_data);
228 static GType gtk_toolbar_child_type (GtkContainer *container);
229 static GtkWidgetPath * gtk_toolbar_get_path_for_child
230 (GtkContainer *container,
232 static void gtk_toolbar_invalidate_order (GtkToolbar *toolbar);
234 static void gtk_toolbar_direction_changed (GtkWidget *widget,
235 GtkTextDirection previous_direction);
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_max_child_expand (GtkToolbar *toolbar);
256 /* methods on ToolbarContent 'class' */
257 static ToolbarContent *toolbar_content_new_tool_item (GtkToolbar *toolbar,
259 gboolean is_placeholder,
261 static void toolbar_content_remove (ToolbarContent *content,
262 GtkToolbar *toolbar);
263 static void toolbar_content_free (ToolbarContent *content);
264 static void toolbar_content_draw (ToolbarContent *content,
265 GtkContainer *container,
267 static gboolean toolbar_content_visible (ToolbarContent *content,
268 GtkToolbar *toolbar);
269 static void toolbar_content_size_request (ToolbarContent *content,
271 GtkRequisition *requisition);
272 static gboolean toolbar_content_is_homogeneous (ToolbarContent *content,
273 GtkToolbar *toolbar);
274 static gboolean toolbar_content_is_placeholder (ToolbarContent *content);
275 static gboolean toolbar_content_disappearing (ToolbarContent *content);
276 static ItemState toolbar_content_get_state (ToolbarContent *content);
277 static gboolean toolbar_content_child_visible (ToolbarContent *content);
278 static void toolbar_content_get_goal_allocation (ToolbarContent *content,
279 GtkAllocation *allocation);
280 static void toolbar_content_get_allocation (ToolbarContent *content,
281 GtkAllocation *allocation);
282 static void toolbar_content_set_start_allocation (ToolbarContent *content,
283 GtkAllocation *new_start_allocation);
284 static void toolbar_content_get_start_allocation (ToolbarContent *content,
285 GtkAllocation *start_allocation);
286 static gboolean toolbar_content_get_expand (ToolbarContent *content);
287 static void toolbar_content_set_goal_allocation (ToolbarContent *content,
288 GtkAllocation *allocation);
289 static void toolbar_content_set_child_visible (ToolbarContent *content,
292 static void toolbar_content_size_allocate (ToolbarContent *content,
293 GtkAllocation *allocation);
294 static void toolbar_content_set_state (ToolbarContent *content,
295 ItemState new_state);
296 static GtkWidget * toolbar_content_get_widget (ToolbarContent *content);
297 static void toolbar_content_set_disappearing (ToolbarContent *content,
298 gboolean disappearing);
299 static void toolbar_content_set_size_request (ToolbarContent *content,
302 static void toolbar_content_toolbar_reconfigured (ToolbarContent *content,
303 GtkToolbar *toolbar);
304 static GtkWidget * toolbar_content_retrieve_menu_item (ToolbarContent *content);
305 static gboolean toolbar_content_has_proxy_menu_item (ToolbarContent *content);
306 static gboolean toolbar_content_is_separator (ToolbarContent *content);
307 static void toolbar_content_show_all (ToolbarContent *content);
308 static void toolbar_content_set_expand (ToolbarContent *content,
311 static void toolbar_tool_shell_iface_init (GtkToolShellIface *iface);
312 static GtkIconSize toolbar_get_icon_size (GtkToolShell *shell);
313 static GtkOrientation toolbar_get_orientation (GtkToolShell *shell);
314 static GtkToolbarStyle toolbar_get_style (GtkToolShell *shell);
315 static GtkReliefStyle toolbar_get_relief_style (GtkToolShell *shell);
316 static void toolbar_rebuild_menu (GtkToolShell *shell);
319 G_DEFINE_TYPE_WITH_CODE (GtkToolbar, gtk_toolbar, GTK_TYPE_CONTAINER,
320 G_IMPLEMENT_INTERFACE (GTK_TYPE_TOOL_SHELL,
321 toolbar_tool_shell_iface_init)
322 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
325 static guint toolbar_signals[LAST_SIGNAL] = { 0 };
329 add_arrow_bindings (GtkBindingSet *binding_set,
331 GtkDirectionType dir)
333 guint keypad_keysym = keysym - GDK_KEY_Left + GDK_KEY_KP_Left;
335 gtk_binding_entry_add_signal (binding_set, keysym, 0,
337 GTK_TYPE_DIRECTION_TYPE, dir);
338 gtk_binding_entry_add_signal (binding_set, keypad_keysym, 0,
340 GTK_TYPE_DIRECTION_TYPE, dir);
344 add_ctrl_tab_bindings (GtkBindingSet *binding_set,
345 GdkModifierType modifiers,
346 GtkDirectionType direction)
348 gtk_binding_entry_add_signal (binding_set,
349 GDK_KEY_Tab, GDK_CONTROL_MASK | modifiers,
351 GTK_TYPE_DIRECTION_TYPE, direction);
352 gtk_binding_entry_add_signal (binding_set,
353 GDK_KEY_KP_Tab, GDK_CONTROL_MASK | modifiers,
355 GTK_TYPE_DIRECTION_TYPE, direction);
359 gtk_toolbar_class_init (GtkToolbarClass *klass)
361 GObjectClass *gobject_class;
362 GtkWidgetClass *widget_class;
363 GtkContainerClass *container_class;
364 GtkBindingSet *binding_set;
366 gobject_class = (GObjectClass *)klass;
367 widget_class = (GtkWidgetClass *)klass;
368 container_class = (GtkContainerClass *)klass;
370 gobject_class->set_property = gtk_toolbar_set_property;
371 gobject_class->get_property = gtk_toolbar_get_property;
372 gobject_class->finalize = gtk_toolbar_finalize;
373 gobject_class->dispose = gtk_toolbar_dispose;
375 widget_class->button_press_event = gtk_toolbar_button_press;
376 widget_class->draw = gtk_toolbar_draw;
377 widget_class->get_preferred_width = gtk_toolbar_get_preferred_width;
378 widget_class->get_preferred_height = gtk_toolbar_get_preferred_height;
379 widget_class->size_allocate = gtk_toolbar_size_allocate;
380 widget_class->style_updated = gtk_toolbar_style_updated;
381 widget_class->focus = gtk_toolbar_focus;
383 gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_TOOL_BAR);
385 /* need to override the base class function via override_class_handler,
386 * because the signal slot is not available in GtkWidgetClass
388 g_signal_override_class_handler ("move-focus",
390 G_CALLBACK (gtk_toolbar_move_focus));
392 widget_class->screen_changed = gtk_toolbar_screen_changed;
393 widget_class->realize = gtk_toolbar_realize;
394 widget_class->unrealize = gtk_toolbar_unrealize;
395 widget_class->map = gtk_toolbar_map;
396 widget_class->unmap = gtk_toolbar_unmap;
397 widget_class->popup_menu = gtk_toolbar_popup_menu;
398 widget_class->show_all = gtk_toolbar_show_all;
399 widget_class->direction_changed = gtk_toolbar_direction_changed;
401 container_class->add = gtk_toolbar_add;
402 container_class->remove = gtk_toolbar_remove;
403 container_class->forall = gtk_toolbar_forall;
404 container_class->child_type = gtk_toolbar_child_type;
405 container_class->get_child_property = gtk_toolbar_get_child_property;
406 container_class->set_child_property = gtk_toolbar_set_child_property;
407 container_class->get_path_for_child = gtk_toolbar_get_path_for_child;
409 klass->orientation_changed = gtk_toolbar_orientation_changed;
410 klass->style_changed = gtk_toolbar_real_style_changed;
413 * GtkToolbar::orientation-changed:
414 * @toolbar: the object which emitted the signal
415 * @orientation: the new #GtkOrientation of the toolbar
417 * Emitted when the orientation of the toolbar changes.
419 toolbar_signals[ORIENTATION_CHANGED] =
420 g_signal_new (I_("orientation-changed"),
421 G_OBJECT_CLASS_TYPE (klass),
423 G_STRUCT_OFFSET (GtkToolbarClass, orientation_changed),
425 g_cclosure_marshal_VOID__ENUM,
427 GTK_TYPE_ORIENTATION);
429 * GtkToolbar::style-changed:
430 * @toolbar: The #GtkToolbar which emitted the signal
431 * @style: the new #GtkToolbarStyle of the toolbar
433 * Emitted when the style of the toolbar changes.
435 toolbar_signals[STYLE_CHANGED] =
436 g_signal_new (I_("style-changed"),
437 G_OBJECT_CLASS_TYPE (klass),
439 G_STRUCT_OFFSET (GtkToolbarClass, style_changed),
441 g_cclosure_marshal_VOID__ENUM,
443 GTK_TYPE_TOOLBAR_STYLE);
445 * GtkToolbar::popup-context-menu:
446 * @toolbar: the #GtkToolbar which emitted the signal
447 * @x: the x coordinate of the point where the menu should appear
448 * @y: the y coordinate of the point where the menu should appear
449 * @button: the mouse button the user pressed, or -1
451 * Emitted when the user right-clicks the toolbar or uses the
452 * keybinding to display a popup menu.
454 * Application developers should handle this signal if they want
455 * to display a context menu on the toolbar. The context-menu should
456 * appear at the coordinates given by @x and @y. The mouse button
457 * number is given by the @button parameter. If the menu was popped
458 * up using the keybaord, @button is -1.
460 * Return value: return %TRUE if the signal was handled, %FALSE if not
462 toolbar_signals[POPUP_CONTEXT_MENU] =
463 g_signal_new (I_("popup-context-menu"),
464 G_OBJECT_CLASS_TYPE (klass),
466 G_STRUCT_OFFSET (GtkToolbarClass, popup_context_menu),
467 _gtk_boolean_handled_accumulator, NULL,
468 _gtk_marshal_BOOLEAN__INT_INT_INT,
470 G_TYPE_INT, G_TYPE_INT,
474 * GtkToolbar::focus-home-or-end:
475 * @toolbar: the #GtkToolbar which emitted the signal
476 * @focus_home: %TRUE if the first item should be focused
478 * A keybinding signal used internally by GTK+. This signal can't
479 * be used in application code
481 * Return value: %TRUE if the signal was handled, %FALSE if not
483 toolbar_signals[FOCUS_HOME_OR_END] =
484 g_signal_new_class_handler (I_("focus-home-or-end"),
485 G_OBJECT_CLASS_TYPE (klass),
486 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
487 G_CALLBACK (gtk_toolbar_focus_home_or_end),
489 _gtk_marshal_BOOLEAN__BOOLEAN,
494 g_object_class_override_property (gobject_class,
498 g_object_class_install_property (gobject_class,
500 g_param_spec_enum ("toolbar-style",
502 P_("How to draw the toolbar"),
503 GTK_TYPE_TOOLBAR_STYLE,
504 DEFAULT_TOOLBAR_STYLE,
505 GTK_PARAM_READWRITE));
506 g_object_class_install_property (gobject_class,
508 g_param_spec_boolean ("show-arrow",
510 P_("If an arrow should be shown if the toolbar doesn't fit"),
512 GTK_PARAM_READWRITE));
515 * GtkToolbar:icon-size:
517 * The size of the icons in a toolbar is normally determined by
518 * the toolbar-icon-size setting. When this property is set, it
519 * overrides the setting.
521 * This should only be used for special-purpose toolbars, normal
522 * application toolbars should respect the user preferences for the
527 g_object_class_install_property (gobject_class,
529 g_param_spec_int ("icon-size",
531 P_("Size of icons in this toolbar"),
534 GTK_PARAM_READWRITE));
537 * GtkToolbar:icon-size-set:
539 * Is %TRUE if the icon-size property has been set.
543 g_object_class_install_property (gobject_class,
545 g_param_spec_boolean ("icon-size-set",
547 P_("Whether the icon-size property has been set"),
549 GTK_PARAM_READWRITE));
551 /* child properties */
552 gtk_container_class_install_child_property (container_class,
554 g_param_spec_boolean ("expand",
556 P_("Whether the item should receive extra space when the toolbar grows"),
558 GTK_PARAM_READWRITE));
560 gtk_container_class_install_child_property (container_class,
561 CHILD_PROP_HOMOGENEOUS,
562 g_param_spec_boolean ("homogeneous",
564 P_("Whether the item should be the same size as other homogeneous items"),
566 GTK_PARAM_READWRITE));
568 /* style properties */
569 gtk_widget_class_install_style_property (widget_class,
570 g_param_spec_int ("space-size",
572 P_("Size of spacers"),
576 GTK_PARAM_READABLE));
579 * GtkToolbar:internal-padding:
581 * Amount of border space between the toolbar shadow and the buttons.
583 * Deprecated: 3.6: Use the standard padding CSS property
584 * (through objects like #GtkStyleContext and #GtkCssProvider); the value
585 * of this style property is ignored.
587 gtk_widget_class_install_style_property (widget_class,
588 g_param_spec_int ("internal-padding",
589 P_("Internal padding"),
590 P_("Amount of border space between the toolbar shadow and the buttons"),
594 GTK_PARAM_READABLE));
596 gtk_widget_class_install_style_property (widget_class,
597 g_param_spec_int ("max-child-expand",
598 P_("Maximum child expand"),
599 P_("Maximum amount of space an expandable item will be given"),
603 GTK_PARAM_READABLE));
605 gtk_widget_class_install_style_property (widget_class,
606 g_param_spec_enum ("space-style",
608 P_("Whether spacers are vertical lines or just blank"),
609 GTK_TYPE_TOOLBAR_SPACE_STYLE,
611 GTK_PARAM_READABLE));
613 gtk_widget_class_install_style_property (widget_class,
614 g_param_spec_enum ("button-relief",
616 P_("Type of bevel around toolbar buttons"),
617 GTK_TYPE_RELIEF_STYLE,
619 GTK_PARAM_READABLE));
621 * GtkToolbar:shadow-type:
623 * Style of bevel around the toolbar.
625 * Deprecated: 3.6: Use the standard border CSS property
626 * (through objects like #GtkStyleContext and #GtkCssProvider); the value
627 * of this style property is ignored.
629 gtk_widget_class_install_style_property (widget_class,
630 g_param_spec_enum ("shadow-type",
632 P_("Style of bevel around the toolbar"),
633 GTK_TYPE_SHADOW_TYPE,
635 GTK_PARAM_READABLE));
637 binding_set = gtk_binding_set_by_class (klass);
639 add_arrow_bindings (binding_set, GDK_KEY_Left, GTK_DIR_LEFT);
640 add_arrow_bindings (binding_set, GDK_KEY_Right, GTK_DIR_RIGHT);
641 add_arrow_bindings (binding_set, GDK_KEY_Up, GTK_DIR_UP);
642 add_arrow_bindings (binding_set, GDK_KEY_Down, GTK_DIR_DOWN);
644 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
645 "focus-home-or-end", 1,
646 G_TYPE_BOOLEAN, TRUE);
647 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
648 "focus-home-or-end", 1,
649 G_TYPE_BOOLEAN, TRUE);
650 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
651 "focus-home-or-end", 1,
652 G_TYPE_BOOLEAN, FALSE);
653 gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
654 "focus-home-or-end", 1,
655 G_TYPE_BOOLEAN, FALSE);
657 add_ctrl_tab_bindings (binding_set, 0, GTK_DIR_TAB_FORWARD);
658 add_ctrl_tab_bindings (binding_set, GDK_SHIFT_MASK, GTK_DIR_TAB_BACKWARD);
660 g_type_class_add_private (gobject_class, sizeof (GtkToolbarPrivate));
664 toolbar_tool_shell_iface_init (GtkToolShellIface *iface)
666 iface->get_icon_size = toolbar_get_icon_size;
667 iface->get_orientation = toolbar_get_orientation;
668 iface->get_style = toolbar_get_style;
669 iface->get_relief_style = toolbar_get_relief_style;
670 iface->rebuild_menu = toolbar_rebuild_menu;
674 gtk_toolbar_init (GtkToolbar *toolbar)
676 GtkToolbarPrivate *priv;
677 GtkStyleContext *context;
679 toolbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (toolbar,
682 priv = toolbar->priv;
684 gtk_widget_set_can_focus (GTK_WIDGET (toolbar), FALSE);
685 gtk_widget_set_has_window (GTK_WIDGET (toolbar), FALSE);
687 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
688 priv->style = DEFAULT_TOOLBAR_STYLE;
689 priv->icon_size = DEFAULT_ICON_SIZE;
690 priv->animation = DEFAULT_ANIMATION_STATE;
692 priv->arrow_button = gtk_toggle_button_new ();
693 g_signal_connect (priv->arrow_button, "button-press-event",
694 G_CALLBACK (gtk_toolbar_arrow_button_press), toolbar);
695 g_signal_connect (priv->arrow_button, "clicked",
696 G_CALLBACK (gtk_toolbar_arrow_button_clicked), toolbar);
697 gtk_button_set_relief (GTK_BUTTON (priv->arrow_button),
698 get_button_relief (toolbar));
700 gtk_button_set_focus_on_click (GTK_BUTTON (priv->arrow_button), FALSE);
702 priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
703 gtk_widget_set_name (priv->arrow, "gtk-toolbar-arrow");
704 gtk_widget_show (priv->arrow);
705 gtk_container_add (GTK_CONTAINER (priv->arrow_button), priv->arrow);
707 gtk_widget_set_parent (priv->arrow_button, GTK_WIDGET (toolbar));
709 /* which child position a drop will occur at */
711 priv->show_arrow = TRUE;
712 priv->settings = NULL;
714 priv->max_homogeneous_pixels = -1;
716 priv->timer = g_timer_new ();
718 context = gtk_widget_get_style_context (GTK_WIDGET (toolbar));
719 gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLBAR);
723 gtk_toolbar_set_property (GObject *object,
728 GtkToolbar *toolbar = GTK_TOOLBAR (object);
729 GtkToolbarPrivate *priv = toolbar->priv;
733 case PROP_ORIENTATION:
734 g_signal_emit (toolbar, toolbar_signals[ORIENTATION_CHANGED], 0,
735 g_value_get_enum (value));
737 case PROP_TOOLBAR_STYLE:
738 gtk_toolbar_set_style (toolbar, g_value_get_enum (value));
740 case PROP_SHOW_ARROW:
741 gtk_toolbar_set_show_arrow (toolbar, g_value_get_boolean (value));
744 gtk_toolbar_set_icon_size (toolbar, g_value_get_int (value));
746 case PROP_ICON_SIZE_SET:
747 if (g_value_get_boolean (value))
748 priv->icon_size_set = TRUE;
750 gtk_toolbar_unset_icon_size (toolbar);
753 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
759 gtk_toolbar_get_property (GObject *object,
764 GtkToolbar *toolbar = GTK_TOOLBAR (object);
765 GtkToolbarPrivate *priv = toolbar->priv;
769 case PROP_ORIENTATION:
770 g_value_set_enum (value, priv->orientation);
772 case PROP_TOOLBAR_STYLE:
773 g_value_set_enum (value, priv->style);
775 case PROP_SHOW_ARROW:
776 g_value_set_boolean (value, priv->show_arrow);
779 g_value_set_int (value, gtk_toolbar_get_icon_size (toolbar));
781 case PROP_ICON_SIZE_SET:
782 g_value_set_boolean (value, priv->icon_size_set);
785 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
791 gtk_toolbar_map (GtkWidget *widget)
793 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
794 GtkToolbarPrivate *priv = toolbar->priv;
796 GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->map (widget);
798 if (priv->event_window)
799 gdk_window_show_unraised (priv->event_window);
803 gtk_toolbar_unmap (GtkWidget *widget)
805 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
806 GtkToolbarPrivate *priv = toolbar->priv;
808 if (priv->event_window)
809 gdk_window_hide (priv->event_window);
811 GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->unmap (widget);
815 gtk_toolbar_realize (GtkWidget *widget)
817 GtkAllocation allocation;
818 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
819 GtkToolbarPrivate *priv = toolbar->priv;
821 GdkWindowAttr attributes;
822 gint attributes_mask;
825 gtk_widget_set_realized (widget, TRUE);
827 gtk_widget_get_allocation (widget, &allocation);
828 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
830 attributes.wclass = GDK_INPUT_ONLY;
831 attributes.window_type = GDK_WINDOW_CHILD;
832 attributes.x = allocation.x + border_width;
833 attributes.y = allocation.y + border_width;
834 attributes.width = allocation.width - border_width * 2;
835 attributes.height = allocation.height - border_width * 2;
836 attributes.event_mask = gtk_widget_get_events (widget);
837 attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
838 GDK_BUTTON_RELEASE_MASK |
839 GDK_ENTER_NOTIFY_MASK |
840 GDK_LEAVE_NOTIFY_MASK);
842 attributes_mask = GDK_WA_X | GDK_WA_Y;
844 window = gtk_widget_get_parent_window (widget);
845 gtk_widget_set_window (widget, window);
846 g_object_ref (window);
848 priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
849 &attributes, attributes_mask);
850 gtk_widget_register_window (widget, priv->event_window);
854 gtk_toolbar_unrealize (GtkWidget *widget)
856 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
857 GtkToolbarPrivate *priv = toolbar->priv;
859 if (priv->event_window)
861 gtk_widget_unregister_window (widget, priv->event_window);
862 gdk_window_destroy (priv->event_window);
863 priv->event_window = NULL;
866 GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->unrealize (widget);
870 gtk_toolbar_draw (GtkWidget *widget,
873 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
874 GtkToolbarPrivate *priv = toolbar->priv;
875 GtkStyleContext *context;
879 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
880 context = gtk_widget_get_style_context (widget);
882 gtk_render_background (context, cr, border_width, border_width,
883 gtk_widget_get_allocated_width (widget) - 2 * border_width,
884 gtk_widget_get_allocated_height (widget) - 2 * border_width);
885 gtk_render_frame (context, cr, border_width, border_width,
886 gtk_widget_get_allocated_width (widget) - 2 * border_width,
887 gtk_widget_get_allocated_height (widget) - 2 * border_width);
889 for (list = priv->content; list != NULL; list = list->next)
891 ToolbarContent *content = list->data;
893 toolbar_content_draw (content, GTK_CONTAINER (widget), cr);
896 gtk_container_propagate_draw (GTK_CONTAINER (widget),
904 get_widget_padding_and_border (GtkWidget *widget,
907 GtkStyleContext *context;
911 context = gtk_widget_get_style_context (widget);
912 state = gtk_style_context_get_state (context);
914 gtk_style_context_get_padding (context, state, padding);
915 gtk_style_context_get_border (context, state, &tmp);
917 padding->top += tmp.top;
918 padding->right += tmp.right;
919 padding->bottom += tmp.bottom;
920 padding->left += tmp.left;
924 gtk_toolbar_size_request (GtkWidget *widget,
925 GtkRequisition *requisition)
927 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
928 GtkToolbarPrivate *priv = toolbar->priv;
930 gint max_child_height;
931 gint max_child_width;
932 gint max_homogeneous_child_width;
933 gint max_homogeneous_child_height;
934 gint homogeneous_size;
936 gint pack_front_size;
939 GtkRequisition arrow_requisition;
941 max_homogeneous_child_width = 0;
942 max_homogeneous_child_height = 0;
944 max_child_height = 0;
945 for (list = priv->content; list != NULL; list = list->next)
947 GtkRequisition requisition;
948 ToolbarContent *content = list->data;
950 if (!toolbar_content_visible (content, toolbar))
953 toolbar_content_size_request (content, toolbar, &requisition);
955 max_child_width = MAX (max_child_width, requisition.width);
956 max_child_height = MAX (max_child_height, requisition.height);
958 if (toolbar_content_is_homogeneous (content, toolbar))
960 max_homogeneous_child_width = MAX (max_homogeneous_child_width, requisition.width);
961 max_homogeneous_child_height = MAX (max_homogeneous_child_height, requisition.height);
965 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
966 homogeneous_size = max_homogeneous_child_width;
968 homogeneous_size = max_homogeneous_child_height;
971 for (list = priv->content; list != NULL; list = list->next)
973 ToolbarContent *content = list->data;
976 if (!toolbar_content_visible (content, toolbar))
979 if (toolbar_content_is_homogeneous (content, toolbar))
981 size = homogeneous_size;
985 GtkRequisition requisition;
987 toolbar_content_size_request (content, toolbar, &requisition);
989 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
990 size = requisition.width;
992 size = requisition.height;
995 pack_front_size += size;
998 if (priv->show_arrow)
1000 gtk_widget_get_preferred_size (priv->arrow_button,
1001 &arrow_requisition, NULL);
1003 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1004 long_req = arrow_requisition.width;
1006 long_req = arrow_requisition.height;
1008 /* There is no point requesting space for the arrow if that would take
1009 * up more space than all the items combined
1011 long_req = MIN (long_req, pack_front_size);
1015 arrow_requisition.height = 0;
1016 arrow_requisition.width = 0;
1018 long_req = pack_front_size;
1021 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1023 requisition->width = long_req;
1024 requisition->height = MAX (max_child_height, arrow_requisition.height);
1028 requisition->height = long_req;
1029 requisition->width = MAX (max_child_width, arrow_requisition.width);
1033 border_width = gtk_container_get_border_width (GTK_CONTAINER (toolbar));
1034 get_widget_padding_and_border (widget, &padding);
1036 requisition->width += 2 * border_width + padding.left + padding.right;
1037 requisition->height += 2 * border_width + padding.top + padding.bottom;
1039 priv->button_maxw = max_homogeneous_child_width;
1040 priv->button_maxh = max_homogeneous_child_height;
1044 gtk_toolbar_get_preferred_width (GtkWidget *widget,
1048 GtkRequisition requisition;
1050 gtk_toolbar_size_request (widget, &requisition);
1052 *minimum = *natural = requisition.width;
1056 gtk_toolbar_get_preferred_height (GtkWidget *widget,
1060 GtkRequisition requisition;
1062 gtk_toolbar_size_request (widget, &requisition);
1064 *minimum = *natural = requisition.height;
1068 position (GtkToolbar *toolbar,
1073 GtkToolbarPrivate *priv = toolbar->priv;
1076 if (!priv->animation)
1079 if (elapsed <= ACCEL_THRESHOLD)
1081 n_pixels = SLIDE_SPEED * elapsed;
1085 /* The formula is a second degree polynomial in
1086 * @elapsed that has the line SLIDE_SPEED * @elapsed
1087 * as tangent for @elapsed == ACCEL_THRESHOLD.
1088 * This makes @n_pixels a smooth function of elapsed time.
1090 n_pixels = (SLIDE_SPEED / ACCEL_THRESHOLD) * elapsed * elapsed -
1091 SLIDE_SPEED * elapsed + SLIDE_SPEED * ACCEL_THRESHOLD;
1095 return MIN (from + n_pixels, to);
1097 return MAX (from - n_pixels, to);
1101 compute_intermediate_allocation (GtkToolbar *toolbar,
1102 const GtkAllocation *start,
1103 const GtkAllocation *goal,
1104 GtkAllocation *intermediate)
1106 GtkToolbarPrivate *priv = toolbar->priv;
1107 gdouble elapsed = g_timer_elapsed (priv->timer, NULL);
1109 intermediate->x = position (toolbar, start->x, goal->x, elapsed);
1110 intermediate->y = position (toolbar, start->y, goal->y, elapsed);
1111 intermediate->width = position (toolbar, start->x + start->width,
1112 goal->x + goal->width,
1113 elapsed) - intermediate->x;
1114 intermediate->height = position (toolbar, start->y + start->height,
1115 goal->y + goal->height,
1116 elapsed) - intermediate->y;
1120 fixup_allocation_for_rtl (gint total_size,
1121 GtkAllocation *allocation)
1123 allocation->x += (total_size - (2 * allocation->x + allocation->width));
1127 fixup_allocation_for_vertical (GtkAllocation *allocation)
1131 tmp = allocation->x;
1132 allocation->x = allocation->y;
1133 allocation->y = tmp;
1135 tmp = allocation->width;
1136 allocation->width = allocation->height;
1137 allocation->height = tmp;
1141 get_item_size (GtkToolbar *toolbar,
1142 ToolbarContent *content)
1144 GtkToolbarPrivate *priv = toolbar->priv;
1145 GtkRequisition requisition;
1147 toolbar_content_size_request (content, toolbar, &requisition);
1149 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1151 if (toolbar_content_is_homogeneous (content, toolbar))
1152 return priv->button_maxw;
1154 return requisition.width;
1158 if (toolbar_content_is_homogeneous (content, toolbar))
1159 return priv->button_maxh;
1161 return requisition.height;
1166 slide_idle_handler (gpointer data)
1168 GtkToolbar *toolbar = GTK_TOOLBAR (data);
1169 GtkToolbarPrivate *priv = toolbar->priv;
1172 if (priv->need_sync)
1175 priv->need_sync = FALSE;
1178 for (list = priv->content; list != NULL; list = list->next)
1180 ToolbarContent *content = list->data;
1182 GtkAllocation goal_allocation;
1183 GtkAllocation allocation;
1186 state = toolbar_content_get_state (content);
1187 toolbar_content_get_goal_allocation (content, &goal_allocation);
1188 toolbar_content_get_allocation (content, &allocation);
1192 if (state == NOT_ALLOCATED)
1194 /* an unallocated item means that size allocate has to
1195 * called at least once more
1200 /* An invisible item with a goal allocation of
1201 * 0 is already at its goal.
1203 if ((state == NORMAL || state == OVERFLOWN) &&
1204 ((goal_allocation.width != 0 &&
1205 goal_allocation.height != 0) ||
1206 toolbar_content_child_visible (content)))
1208 if ((goal_allocation.x != allocation.x ||
1209 goal_allocation.y != allocation.y ||
1210 goal_allocation.width != allocation.width ||
1211 goal_allocation.height != allocation.height))
1213 /* An item is not in its right position yet. Note
1214 * that OVERFLOWN items do get an allocation in
1215 * gtk_toolbar_size_allocate(). This way you can see
1216 * them slide back in when you drag an item off the
1223 if (toolbar_content_is_placeholder (content) &&
1224 toolbar_content_disappearing (content) &&
1225 toolbar_content_child_visible (content))
1227 /* A disappearing placeholder is still visible.
1235 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1241 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1243 priv->is_sliding = FALSE;
1250 rect_within (GtkAllocation *a1,
1253 return (a1->x >= a2->x &&
1254 a1->x + a1->width <= a2->x + a2->width &&
1256 a1->y + a1->height <= a2->y + a2->height);
1260 gtk_toolbar_begin_sliding (GtkToolbar *toolbar)
1262 GtkAllocation allocation;
1263 GtkWidget *widget = GTK_WIDGET (toolbar);
1264 GtkToolbarPrivate *priv = toolbar->priv;
1273 /* Start the sliding. This function copies the allocation of every
1274 * item into content->start_allocation. For items that haven't
1275 * been allocated yet, we calculate their position and save that
1276 * in start_allocatino along with zero width and zero height.
1278 * FIXME: It would be nice if we could share this code with
1279 * the equivalent in gtk_widget_size_allocate().
1281 priv->is_sliding = TRUE;
1284 priv->idle_id = gdk_threads_add_idle (slide_idle_handler, toolbar);
1286 gtk_widget_get_allocation (widget, &allocation);
1288 rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1289 vertical = (priv->orientation == GTK_ORIENTATION_VERTICAL);
1291 border_width = gtk_container_get_border_width (GTK_CONTAINER (toolbar));
1292 get_widget_padding_and_border (GTK_WIDGET (toolbar), &padding);
1296 cur_x = allocation.width - border_width - padding.right;
1297 cur_y = allocation.height - border_width - padding.top;
1301 cur_x = border_width + padding.left;
1302 cur_y = border_width + padding.top;
1305 cur_x += allocation.x;
1306 cur_y += allocation.y;
1308 for (list = priv->content; list != NULL; list = list->next)
1310 ToolbarContent *content = list->data;
1311 GtkAllocation new_start_allocation;
1312 GtkAllocation item_allocation;
1315 state = toolbar_content_get_state (content);
1316 toolbar_content_get_allocation (content, &item_allocation);
1318 if ((state == NORMAL &&
1319 rect_within (&item_allocation, &allocation)) ||
1322 new_start_allocation = item_allocation;
1326 new_start_allocation.x = cur_x;
1327 new_start_allocation.y = cur_y;
1331 new_start_allocation.width = allocation.width -
1333 padding.left - padding.right;
1334 new_start_allocation.height = 0;
1338 new_start_allocation.width = 0;
1339 new_start_allocation.height = allocation.height -
1341 padding.top - padding.bottom;
1346 cur_y = new_start_allocation.y + new_start_allocation.height;
1348 cur_x = new_start_allocation.x;
1350 cur_x = new_start_allocation.x + new_start_allocation.width;
1352 toolbar_content_set_start_allocation (content, &new_start_allocation);
1355 /* This resize will run before the first idle handler. This
1356 * will make sure that items get the right goal allocation
1357 * so that the idle handler will not immediately return
1360 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1361 g_timer_reset (priv->timer);
1365 gtk_toolbar_stop_sliding (GtkToolbar *toolbar)
1367 GtkToolbarPrivate *priv = toolbar->priv;
1369 if (priv->is_sliding)
1373 priv->is_sliding = FALSE;
1377 g_source_remove (priv->idle_id);
1381 list = priv->content;
1384 ToolbarContent *content = list->data;
1387 if (toolbar_content_is_placeholder (content))
1389 toolbar_content_remove (content, toolbar);
1390 toolbar_content_free (content);
1394 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1399 remove_item (GtkWidget *menu_item,
1402 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (menu_item)),
1407 menu_deactivated (GtkWidget *menu,
1408 GtkToolbar *toolbar)
1410 GtkToolbarPrivate *priv = toolbar->priv;
1412 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
1416 menu_detached (GtkWidget *widget,
1419 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1420 GtkToolbarPrivate *priv = toolbar->priv;
1426 rebuild_menu (GtkToolbar *toolbar)
1428 GtkToolbarPrivate *priv = toolbar->priv;
1429 GList *list, *children;
1433 priv->menu = GTK_MENU (gtk_menu_new());
1434 gtk_menu_attach_to_widget (priv->menu,
1435 GTK_WIDGET (toolbar),
1438 g_signal_connect (priv->menu, "deactivate",
1439 G_CALLBACK (menu_deactivated), toolbar);
1442 gtk_container_foreach (GTK_CONTAINER (priv->menu), remove_item, NULL);
1444 for (list = priv->content; list != NULL; list = list->next)
1446 ToolbarContent *content = list->data;
1448 if (toolbar_content_get_state (content) == OVERFLOWN &&
1449 !toolbar_content_is_placeholder (content))
1451 GtkWidget *menu_item = toolbar_content_retrieve_menu_item (content);
1455 g_assert (GTK_IS_MENU_ITEM (menu_item));
1456 gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), menu_item);
1461 /* Remove leading and trailing separator items */
1462 children = gtk_container_get_children (GTK_CONTAINER (priv->menu));
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 /* Regenerate the list of children so we don't try to remove items twice */
1475 children = gtk_container_get_children (GTK_CONTAINER (priv->menu));
1477 list = g_list_last (children);
1478 while (list && GTK_IS_SEPARATOR_MENU_ITEM (list->data))
1480 GtkWidget *child = list->data;
1482 gtk_container_remove (GTK_CONTAINER (priv->menu), child);
1485 g_list_free (children);
1487 priv->need_rebuild = FALSE;
1491 gtk_toolbar_size_allocate (GtkWidget *widget,
1492 GtkAllocation *allocation)
1494 GtkAllocation widget_allocation;
1495 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1496 GtkToolbarPrivate *priv = toolbar->priv;
1497 GtkAllocation *allocations;
1498 ItemState *new_states;
1499 GtkAllocation arrow_allocation;
1502 gint size, pos, short_size;
1505 gboolean need_arrow;
1506 gint n_expand_items;
1508 gint available_size;
1511 GtkRequisition arrow_requisition;
1512 gboolean overflowing;
1513 gboolean size_changed;
1514 GtkAllocation item_area;
1516 gtk_widget_get_allocation (widget, &widget_allocation);
1517 size_changed = FALSE;
1518 if (widget_allocation.x != allocation->x ||
1519 widget_allocation.y != allocation->y ||
1520 widget_allocation.width != allocation->width ||
1521 widget_allocation.height != allocation->height)
1523 size_changed = TRUE;
1527 gtk_toolbar_stop_sliding (toolbar);
1529 gtk_widget_set_allocation (widget, allocation);
1531 border_width = gtk_container_get_border_width (GTK_CONTAINER (toolbar));
1533 if (gtk_widget_get_realized (widget))
1534 gdk_window_move_resize (priv->event_window,
1535 allocation->x + border_width,
1536 allocation->y + border_width,
1537 allocation->width - border_width * 2,
1538 allocation->height - border_width * 2);
1541 gtk_widget_get_preferred_size (priv->arrow_button,
1542 &arrow_requisition, NULL);
1543 get_widget_padding_and_border (widget, &padding);
1545 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1547 available_size = size = allocation->width - 2 * border_width - padding.left - padding.right;
1548 short_size = allocation->height - 2 * border_width - padding.top - padding.bottom;
1549 arrow_size = arrow_requisition.width;
1553 available_size = size = allocation->height - 2 * border_width - padding.top - padding.bottom;
1554 short_size = allocation->width - 2 * border_width - padding.left - padding.right;
1555 arrow_size = arrow_requisition.height;
1558 n_items = g_list_length (priv->content);
1559 allocations = g_new0 (GtkAllocation, n_items);
1560 new_states = g_new0 (ItemState, n_items);
1564 for (list = priv->content; list != NULL; list = list->next)
1566 ToolbarContent *content = list->data;
1568 if (toolbar_content_visible (content, toolbar))
1570 needed_size += get_item_size (toolbar, content);
1572 /* Do we need an arrow?
1574 * Assume we don't, and see if any non-separator item
1575 * with a proxy menu item is then going to overflow.
1577 if (needed_size > available_size &&
1580 toolbar_content_has_proxy_menu_item (content) &&
1581 !toolbar_content_is_separator (content))
1589 size = available_size - arrow_size;
1591 size = available_size;
1593 /* calculate widths and states of items */
1594 overflowing = FALSE;
1595 for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1597 ToolbarContent *content = list->data;
1600 if (!toolbar_content_visible (content, toolbar))
1602 new_states[i] = HIDDEN;
1606 item_size = get_item_size (toolbar, content);
1607 if (item_size <= size && !overflowing)
1610 allocations[i].width = item_size;
1611 new_states[i] = NORMAL;
1616 new_states[i] = OVERFLOWN;
1617 allocations[i].width = item_size;
1621 /* calculate width of arrow */
1624 arrow_allocation.width = arrow_size;
1625 arrow_allocation.height = MAX (short_size, 1);
1628 /* expand expandable items */
1630 /* We don't expand when there is an overflow menu,
1631 * because that leads to weird jumps when items get
1632 * moved to the overflow menu and the expanding
1633 * items suddenly get a lot of extra space
1637 gint max_child_expand;
1640 for (i = 0, list = priv->content; list != NULL; list = list->next, ++i)
1642 ToolbarContent *content = list->data;
1644 if (toolbar_content_get_expand (content) && new_states[i] == NORMAL)
1648 max_child_expand = get_max_child_expand (toolbar);
1649 for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1651 ToolbarContent *content = list->data;
1653 if (toolbar_content_get_expand (content) && new_states[i] == NORMAL)
1655 gint extra = size / n_expand_items;
1656 if (size % n_expand_items != 0)
1659 if (extra > max_child_expand)
1660 extra = max_child_expand;
1662 allocations[i].width += extra;
1668 g_assert (n_expand_items == 0);
1671 /* position items */
1673 for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1675 /* Both NORMAL and OVERFLOWN items get a position.
1676 * This ensures that sliding will work for OVERFLOWN items too.
1678 if (new_states[i] == NORMAL || new_states[i] == OVERFLOWN)
1680 allocations[i].x = pos;
1681 allocations[i].y = border_width;
1682 allocations[i].height = short_size;
1684 pos += allocations[i].width;
1688 /* position arrow */
1691 arrow_allocation.x = available_size - border_width - arrow_allocation.width;
1692 arrow_allocation.y = border_width;
1695 item_area.x = border_width;
1696 item_area.y = border_width;
1697 item_area.width = available_size - (need_arrow? arrow_size : 0);
1698 item_area.height = short_size;
1700 /* fix up allocations in the vertical or RTL cases */
1701 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1703 for (i = 0; i < n_items; ++i)
1704 fixup_allocation_for_vertical (&(allocations[i]));
1707 fixup_allocation_for_vertical (&arrow_allocation);
1709 fixup_allocation_for_vertical (&item_area);
1711 else if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
1713 for (i = 0; i < n_items; ++i)
1714 fixup_allocation_for_rtl (available_size, &(allocations[i]));
1717 fixup_allocation_for_rtl (available_size, &arrow_allocation);
1719 fixup_allocation_for_rtl (available_size, &item_area);
1722 /* translate the items by allocation->(x,y) */
1723 for (i = 0; i < n_items; ++i)
1725 allocations[i].x += allocation->x + padding.left;
1726 allocations[i].y += allocation->y + padding.top;
1731 arrow_allocation.x += allocation->x + padding.left;
1732 arrow_allocation.y += allocation->y + padding.top;
1735 item_area.x += allocation->x + padding.left;
1736 item_area.y += allocation->y + padding.top;
1738 /* did anything change? */
1739 for (list = priv->content, i = 0; list != NULL; list = list->next, i++)
1741 ToolbarContent *content = list->data;
1743 if (toolbar_content_get_state (content) == NORMAL &&
1744 new_states[i] != NORMAL)
1746 /* an item disappeared and we didn't change size, so begin sliding */
1748 gtk_toolbar_begin_sliding (toolbar);
1752 /* finally allocate the items */
1753 if (priv->is_sliding)
1755 for (list = priv->content, i = 0; list != NULL; list = list->next, i++)
1757 ToolbarContent *content = list->data;
1759 toolbar_content_set_goal_allocation (content, &(allocations[i]));
1763 for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1765 ToolbarContent *content = list->data;
1767 if (new_states[i] == OVERFLOWN || new_states[i] == NORMAL)
1769 GtkAllocation alloc;
1770 GtkAllocation start_allocation = { 0, };
1771 GtkAllocation goal_allocation;
1773 if (priv->is_sliding)
1775 toolbar_content_get_start_allocation (content, &start_allocation);
1776 toolbar_content_get_goal_allocation (content, &goal_allocation);
1778 compute_intermediate_allocation (toolbar,
1783 priv->need_sync = TRUE;
1787 alloc = allocations[i];
1790 if (alloc.width <= 0 || alloc.height <= 0)
1792 toolbar_content_set_child_visible (content, toolbar, FALSE);
1796 if (!rect_within (&alloc, &item_area))
1798 toolbar_content_set_child_visible (content, toolbar, FALSE);
1799 toolbar_content_size_allocate (content, &alloc);
1803 toolbar_content_set_child_visible (content, toolbar, TRUE);
1804 toolbar_content_size_allocate (content, &alloc);
1810 toolbar_content_set_child_visible (content, toolbar, FALSE);
1813 toolbar_content_set_state (content, new_states[i]);
1816 if (priv->menu && priv->need_rebuild)
1817 rebuild_menu (toolbar);
1821 gtk_widget_size_allocate (GTK_WIDGET (priv->arrow_button),
1823 gtk_widget_show (GTK_WIDGET (priv->arrow_button));
1827 gtk_widget_hide (GTK_WIDGET (priv->arrow_button));
1829 if (priv->menu && gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
1830 gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
1833 g_free (allocations);
1834 g_free (new_states);
1838 gtk_toolbar_update_button_relief (GtkToolbar *toolbar)
1840 GtkToolbarPrivate *priv = toolbar->priv;
1841 GtkReliefStyle relief;
1843 relief = get_button_relief (toolbar);
1845 if (relief != gtk_button_get_relief (GTK_BUTTON (priv->arrow_button)))
1847 gtk_toolbar_reconfigured (toolbar);
1849 gtk_button_set_relief (GTK_BUTTON (priv->arrow_button), relief);
1854 gtk_toolbar_style_updated (GtkWidget *widget)
1856 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1857 GtkToolbarPrivate *priv = toolbar->priv;
1859 GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->style_updated (widget);
1861 priv->max_homogeneous_pixels = -1;
1862 gtk_toolbar_update_button_relief (GTK_TOOLBAR (widget));
1866 gtk_toolbar_list_children_in_focus_order (GtkToolbar *toolbar,
1867 GtkDirectionType dir)
1869 GtkToolbarPrivate *priv = toolbar->priv;
1870 GList *result = NULL;
1874 /* generate list of children in reverse logical order */
1876 for (list = priv->content; list != NULL; list = list->next)
1878 ToolbarContent *content = list->data;
1881 widget = toolbar_content_get_widget (content);
1884 result = g_list_prepend (result, widget);
1887 result = g_list_prepend (result, priv->arrow_button);
1889 rtl = (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL);
1891 /* move in logical order when
1893 * - dir is TAB_FORWARD
1895 * - in RTL mode and moving left or up
1897 * - in LTR mode and moving right or down
1899 if (dir == GTK_DIR_TAB_FORWARD ||
1900 (rtl && (dir == GTK_DIR_UP || dir == GTK_DIR_LEFT)) ||
1901 (!rtl && (dir == GTK_DIR_DOWN || dir == GTK_DIR_RIGHT)))
1903 result = g_list_reverse (result);
1910 gtk_toolbar_focus_home_or_end (GtkToolbar *toolbar,
1911 gboolean focus_home)
1913 GList *children, *list;
1914 GtkDirectionType dir = focus_home? GTK_DIR_RIGHT : GTK_DIR_LEFT;
1916 children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);
1918 if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
1920 children = g_list_reverse (children);
1922 dir = (dir == GTK_DIR_RIGHT)? GTK_DIR_LEFT : GTK_DIR_RIGHT;
1925 for (list = children; list != NULL; list = list->next)
1927 GtkWidget *child = list->data;
1929 if (gtk_container_get_focus_child (GTK_CONTAINER (toolbar)) == child)
1932 if (gtk_widget_get_mapped (child) && gtk_widget_child_focus (child, dir))
1936 g_list_free (children);
1941 /* Keybinding handler. This function is called when the user presses
1942 * Ctrl TAB or an arrow key.
1945 gtk_toolbar_move_focus (GtkWidget *widget,
1946 GtkDirectionType dir)
1948 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1949 GtkContainer *container = GTK_CONTAINER (toolbar);
1950 GtkWidget *focus_child;
1952 gboolean try_focus = FALSE;
1955 focus_child = gtk_container_get_focus_child (container);
1957 if (focus_child && gtk_widget_child_focus (focus_child, dir))
1960 children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);
1962 for (list = children; list != NULL; list = list->next)
1964 GtkWidget *child = list->data;
1966 if (try_focus && gtk_widget_get_mapped (child) && gtk_widget_child_focus (child, dir))
1969 if (child == focus_child)
1973 g_list_free (children);
1976 /* The focus handler for the toolbar. It called when the user presses
1977 * TAB or otherwise tries to focus the toolbar.
1980 gtk_toolbar_focus (GtkWidget *widget,
1981 GtkDirectionType dir)
1983 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1984 GList *children, *list;
1985 gboolean result = FALSE;
1987 /* if focus is already somewhere inside the toolbar then return FALSE.
1988 * The only way focus can stay inside the toolbar is when the user presses
1989 * arrow keys or Ctrl TAB (both of which are handled by the
1990 * gtk_toolbar_move_focus() keybinding function.
1992 if (gtk_container_get_focus_child (GTK_CONTAINER (widget)))
1995 children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);
1997 for (list = children; list != NULL; list = list->next)
1999 GtkWidget *child = list->data;
2001 if (gtk_widget_get_mapped (child) && gtk_widget_child_focus (child, dir))
2008 g_list_free (children);
2013 static GtkSettings *
2014 toolbar_get_settings (GtkToolbar *toolbar)
2016 return toolbar->priv->settings;
2020 style_change_notify (GtkToolbar *toolbar)
2022 GtkToolbarPrivate *priv = toolbar->priv;
2024 if (!priv->style_set)
2026 /* pretend it was set, then unset, thus reverting to new default */
2027 priv->style_set = TRUE;
2028 gtk_toolbar_unset_style (toolbar);
2033 icon_size_change_notify (GtkToolbar *toolbar)
2035 GtkToolbarPrivate *priv = toolbar->priv;
2037 if (!priv->icon_size_set)
2039 /* pretend it was set, then unset, thus reverting to new default */
2040 priv->icon_size_set = TRUE;
2041 gtk_toolbar_unset_icon_size (toolbar);
2046 animation_change_notify (GtkToolbar *toolbar)
2048 GtkToolbarPrivate *priv = toolbar->priv;
2049 GtkSettings *settings = toolbar_get_settings (toolbar);
2053 g_object_get (settings,
2054 "gtk-enable-animations", &animation,
2057 animation = DEFAULT_ANIMATION_STATE;
2059 priv->animation = animation;
2063 settings_change_notify (GtkSettings *settings,
2064 const GParamSpec *pspec,
2065 GtkToolbar *toolbar)
2067 if (! strcmp (pspec->name, "gtk-toolbar-style"))
2068 style_change_notify (toolbar);
2069 else if (! strcmp (pspec->name, "gtk-toolbar-icon-size"))
2070 icon_size_change_notify (toolbar);
2071 else if (! strcmp (pspec->name, "gtk-enable-animations"))
2072 animation_change_notify (toolbar);
2076 gtk_toolbar_screen_changed (GtkWidget *widget,
2077 GdkScreen *previous_screen)
2079 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
2080 GtkToolbarPrivate *priv = toolbar->priv;
2081 GtkSettings *old_settings = toolbar_get_settings (toolbar);
2082 GtkSettings *settings;
2084 if (gtk_widget_has_screen (GTK_WIDGET (toolbar)))
2085 settings = gtk_widget_get_settings (GTK_WIDGET (toolbar));
2089 if (settings == old_settings)
2094 g_signal_handler_disconnect (old_settings, priv->settings_connection);
2095 priv->settings_connection = 0;
2096 g_object_unref (old_settings);
2101 priv->settings_connection =
2102 g_signal_connect (settings, "notify",
2103 G_CALLBACK (settings_change_notify),
2106 priv->settings = g_object_ref (settings);
2109 priv->settings = NULL;
2111 style_change_notify (toolbar);
2112 icon_size_change_notify (toolbar);
2113 animation_change_notify (toolbar);
2117 find_drop_index (GtkToolbar *toolbar,
2121 GtkToolbarPrivate *priv = toolbar->priv;
2122 GList *interesting_content;
2124 GtkOrientation orientation;
2125 GtkTextDirection direction;
2126 gint best_distance = G_MAXINT;
2130 ToolbarContent *best_content;
2131 GtkAllocation allocation;
2133 /* list items we care about wrt. drag and drop */
2134 interesting_content = NULL;
2135 for (list = priv->content; list != NULL; list = list->next)
2137 ToolbarContent *content = list->data;
2139 if (toolbar_content_get_state (content) == NORMAL)
2140 interesting_content = g_list_prepend (interesting_content, content);
2142 interesting_content = g_list_reverse (interesting_content);
2144 if (!interesting_content)
2147 orientation = priv->orientation;
2148 direction = gtk_widget_get_direction (GTK_WIDGET (toolbar));
2150 /* distance to first interesting item */
2151 best_content = interesting_content->data;
2152 toolbar_content_get_allocation (best_content, &allocation);
2154 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2158 if (direction == GTK_TEXT_DIR_LTR)
2161 pos = allocation.x + allocation.width;
2169 best_content = NULL;
2170 best_distance = ABS (pos - cursor);
2172 /* distance to far end of each item */
2173 for (list = interesting_content; list != NULL; list = list->next)
2175 ToolbarContent *content = list->data;
2177 toolbar_content_get_allocation (content, &allocation);
2179 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2181 if (direction == GTK_TEXT_DIR_LTR)
2182 pos = allocation.x + allocation.width;
2188 pos = allocation.y + allocation.height;
2191 distance = ABS (pos - cursor);
2193 if (distance < best_distance)
2195 best_distance = distance;
2196 best_content = content;
2200 g_list_free (interesting_content);
2205 return g_list_index (priv->content, best_content) + 1;
2209 reset_all_placeholders (GtkToolbar *toolbar)
2211 GtkToolbarPrivate *priv = toolbar->priv;
2214 for (list = priv->content; list != NULL; list = list->next)
2216 ToolbarContent *content = list->data;
2217 if (toolbar_content_is_placeholder (content))
2218 toolbar_content_set_disappearing (content, TRUE);
2223 physical_to_logical (GtkToolbar *toolbar,
2226 GtkToolbarPrivate *priv = toolbar->priv;
2230 g_assert (physical >= 0);
2233 for (list = priv->content; list && physical > 0; list = list->next)
2235 ToolbarContent *content = list->data;
2237 if (!toolbar_content_is_placeholder (content))
2242 g_assert (physical == 0);
2248 logical_to_physical (GtkToolbar *toolbar,
2251 GtkToolbarPrivate *priv = toolbar->priv;
2255 g_assert (logical >= 0);
2258 for (list = priv->content; list; list = list->next)
2260 ToolbarContent *content = list->data;
2262 if (!toolbar_content_is_placeholder (content))
2272 g_assert (logical == 0);
2278 * gtk_toolbar_set_drop_highlight_item:
2279 * @toolbar: a #GtkToolbar
2280 * @tool_item: (allow-none): a #GtkToolItem, or %NULL to turn of highlighting
2281 * @index_: a position on @toolbar
2283 * Highlights @toolbar to give an idea of what it would look like
2284 * if @item was added to @toolbar at the position indicated by @index_.
2285 * If @item is %NULL, highlighting is turned off. In that case @index_
2288 * The @tool_item passed to this function must not be part of any widget
2289 * hierarchy. When an item is set as drop highlight item it can not
2290 * added to any widget hierarchy or used as highlight item for another
2296 gtk_toolbar_set_drop_highlight_item (GtkToolbar *toolbar,
2297 GtkToolItem *tool_item,
2300 ToolbarContent *content;
2301 GtkToolbarPrivate *priv;
2303 GtkRequisition requisition;
2304 GtkRequisition old_requisition;
2305 gboolean restart_sliding;
2307 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2308 g_return_if_fail (tool_item == NULL || GTK_IS_TOOL_ITEM (tool_item));
2310 priv = toolbar->priv;
2314 if (priv->highlight_tool_item)
2316 gtk_widget_unparent (GTK_WIDGET (priv->highlight_tool_item));
2317 g_object_unref (priv->highlight_tool_item);
2318 priv->highlight_tool_item = NULL;
2321 reset_all_placeholders (toolbar);
2322 gtk_toolbar_begin_sliding (toolbar);
2326 n_items = gtk_toolbar_get_n_items (toolbar);
2327 if (index_ < 0 || index_ > n_items)
2330 if (tool_item != priv->highlight_tool_item)
2332 if (priv->highlight_tool_item)
2333 g_object_unref (priv->highlight_tool_item);
2335 g_object_ref_sink (tool_item);
2337 priv->highlight_tool_item = tool_item;
2339 gtk_widget_set_parent (GTK_WIDGET (priv->highlight_tool_item),
2340 GTK_WIDGET (toolbar));
2343 index_ = logical_to_physical (toolbar, index_);
2345 content = g_list_nth_data (priv->content, index_);
2349 ToolbarContent *prev_content;
2351 prev_content = g_list_nth_data (priv->content, index_ - 1);
2353 if (prev_content && toolbar_content_is_placeholder (prev_content))
2354 content = prev_content;
2357 if (!content || !toolbar_content_is_placeholder (content))
2359 GtkWidget *placeholder;
2361 placeholder = GTK_WIDGET (gtk_separator_tool_item_new ());
2363 content = toolbar_content_new_tool_item (toolbar,
2364 GTK_TOOL_ITEM (placeholder),
2366 gtk_widget_show (placeholder);
2370 g_assert (toolbar_content_is_placeholder (content));
2372 gtk_widget_get_preferred_size (GTK_WIDGET (priv->highlight_tool_item),
2373 &requisition, NULL);
2375 toolbar_content_set_expand (content, gtk_tool_item_get_expand (tool_item));
2377 restart_sliding = FALSE;
2378 toolbar_content_size_request (content, toolbar, &old_requisition);
2379 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2381 requisition.height = -1;
2382 if (requisition.width != old_requisition.width)
2383 restart_sliding = TRUE;
2387 requisition.width = -1;
2388 if (requisition.height != old_requisition.height)
2389 restart_sliding = TRUE;
2392 if (toolbar_content_disappearing (content))
2393 restart_sliding = TRUE;
2395 reset_all_placeholders (toolbar);
2396 toolbar_content_set_disappearing (content, FALSE);
2398 toolbar_content_set_size_request (content,
2399 requisition.width, requisition.height);
2401 if (restart_sliding)
2402 gtk_toolbar_begin_sliding (toolbar);
2406 gtk_toolbar_get_child_property (GtkContainer *container,
2412 GtkToolItem *item = GTK_TOOL_ITEM (child);
2414 switch (property_id)
2416 case CHILD_PROP_HOMOGENEOUS:
2417 g_value_set_boolean (value, gtk_tool_item_get_homogeneous (item));
2420 case CHILD_PROP_EXPAND:
2421 g_value_set_boolean (value, gtk_tool_item_get_expand (item));
2425 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
2431 gtk_toolbar_set_child_property (GtkContainer *container,
2434 const GValue *value,
2437 switch (property_id)
2439 case CHILD_PROP_HOMOGENEOUS:
2440 gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (child), g_value_get_boolean (value));
2443 case CHILD_PROP_EXPAND:
2444 gtk_tool_item_set_expand (GTK_TOOL_ITEM (child), g_value_get_boolean (value));
2448 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
2454 gtk_toolbar_show_all (GtkWidget *widget)
2456 GtkToolbar *toolbar = GTK_TOOLBAR (widget);
2457 GtkToolbarPrivate *priv = toolbar->priv;
2460 for (list = priv->content; list != NULL; list = list->next)
2462 ToolbarContent *content = list->data;
2464 toolbar_content_show_all (content);
2467 gtk_widget_show (widget);
2471 gtk_toolbar_add (GtkContainer *container,
2474 GtkToolbar *toolbar = GTK_TOOLBAR (container);
2476 gtk_toolbar_insert (toolbar, GTK_TOOL_ITEM (widget), -1);
2480 gtk_toolbar_remove (GtkContainer *container,
2483 GtkToolbar *toolbar = GTK_TOOLBAR (container);
2484 GtkToolbarPrivate *priv = toolbar->priv;
2485 ToolbarContent *content_to_remove;
2488 content_to_remove = NULL;
2489 for (list = priv->content; list != NULL; list = list->next)
2491 ToolbarContent *content = list->data;
2494 child = toolbar_content_get_widget (content);
2495 if (child && child == widget)
2497 content_to_remove = content;
2502 g_return_if_fail (content_to_remove != NULL);
2504 toolbar_content_remove (content_to_remove, toolbar);
2505 toolbar_content_free (content_to_remove);
2509 gtk_toolbar_forall (GtkContainer *container,
2510 gboolean include_internals,
2511 GtkCallback callback,
2512 gpointer callback_data)
2514 GtkToolbar *toolbar = GTK_TOOLBAR (container);
2515 GtkToolbarPrivate *priv = toolbar->priv;
2518 g_return_if_fail (callback != NULL);
2520 list = priv->content;
2523 ToolbarContent *content = list->data;
2524 GList *next = list->next;
2526 if (include_internals || !toolbar_content_is_placeholder (content))
2528 GtkWidget *child = toolbar_content_get_widget (content);
2531 callback (child, callback_data);
2537 if (include_internals && priv->arrow_button)
2538 callback (priv->arrow_button, callback_data);
2542 gtk_toolbar_child_type (GtkContainer *container)
2544 return GTK_TYPE_TOOL_ITEM;
2548 gtk_toolbar_reconfigured (GtkToolbar *toolbar)
2550 GtkToolbarPrivate *priv = toolbar->priv;
2553 list = priv->content;
2556 ToolbarContent *content = list->data;
2557 GList *next = list->next;
2559 toolbar_content_toolbar_reconfigured (content, toolbar);
2566 gtk_toolbar_orientation_changed (GtkToolbar *toolbar,
2567 GtkOrientation orientation)
2569 GtkToolbarPrivate *priv = toolbar->priv;
2571 if (priv->orientation != orientation)
2573 priv->orientation = orientation;
2575 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2576 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2578 gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
2580 gtk_toolbar_reconfigured (toolbar);
2582 _gtk_orientable_set_style_classes (GTK_ORIENTABLE (toolbar));
2583 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
2584 g_object_notify (G_OBJECT (toolbar), "orientation");
2589 gtk_toolbar_real_style_changed (GtkToolbar *toolbar,
2590 GtkToolbarStyle style)
2592 GtkToolbarPrivate *priv = toolbar->priv;
2594 if (priv->style != style)
2596 priv->style = style;
2598 gtk_toolbar_reconfigured (toolbar);
2600 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
2601 g_object_notify (G_OBJECT (toolbar), "toolbar-style");
2606 menu_position_func (GtkMenu *menu,
2612 GtkAllocation allocation;
2613 GtkToolbar *toolbar = GTK_TOOLBAR (user_data);
2614 GtkToolbarPrivate *priv = toolbar->priv;
2616 GtkRequisition menu_req;
2617 GdkRectangle monitor;
2621 gtk_widget_get_preferred_size (priv->arrow_button,
2623 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
2626 screen = gtk_widget_get_screen (GTK_WIDGET (menu));
2627 monitor_num = gdk_screen_get_monitor_at_window (screen,
2628 gtk_widget_get_window (priv->arrow_button));
2629 if (monitor_num < 0)
2631 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
2633 gtk_widget_get_allocation (priv->arrow_button, &allocation);
2635 gdk_window_get_origin (gtk_button_get_event_window (GTK_BUTTON (priv->arrow_button)), x, y);
2636 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2638 if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_LTR)
2639 *x += allocation.width - req.width;
2641 *x += req.width - menu_req.width;
2643 if ((*y + allocation.height + menu_req.height) <= monitor.y + monitor.height)
2644 *y += allocation.height;
2645 else if ((*y - menu_req.height) >= monitor.y)
2646 *y -= menu_req.height;
2647 else if (monitor.y + monitor.height - (*y + allocation.height) > *y)
2648 *y += allocation.height;
2650 *y -= menu_req.height;
2654 if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_LTR)
2655 *x += allocation.width;
2657 *x -= menu_req.width;
2659 if (*y + menu_req.height > monitor.y + monitor.height &&
2660 *y + allocation.height - monitor.y > monitor.y + monitor.height - *y)
2661 *y += allocation.height - menu_req.height;
2668 show_menu (GtkToolbar *toolbar,
2669 GdkEventButton *event)
2671 GtkToolbarPrivate *priv = toolbar->priv;
2673 rebuild_menu (toolbar);
2675 gtk_widget_show_all (GTK_WIDGET (priv->menu));
2677 gtk_menu_popup (priv->menu, NULL, NULL,
2678 menu_position_func, toolbar,
2679 event? event->button : 0,
2680 event? event->time : gtk_get_current_event_time());
2684 gtk_toolbar_arrow_button_clicked (GtkWidget *button,
2685 GtkToolbar *toolbar)
2687 GtkToolbarPrivate *priv = toolbar->priv;
2689 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->arrow_button)) &&
2690 (!priv->menu || !gtk_widget_get_visible (GTK_WIDGET (priv->menu))))
2692 /* We only get here when the button is clicked with the keyboard,
2693 * because mouse button presses result in the menu being shown so
2694 * that priv->menu would be non-NULL and visible.
2696 show_menu (toolbar, NULL);
2697 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
2702 gtk_toolbar_arrow_button_press (GtkWidget *button,
2703 GdkEventButton *event,
2704 GtkToolbar *toolbar)
2706 show_menu (toolbar, event);
2707 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
2713 gtk_toolbar_button_press (GtkWidget *toolbar,
2714 GdkEventButton *event)
2718 if (gdk_event_triggers_context_menu ((GdkEvent *) event))
2720 gboolean return_value;
2722 g_signal_emit (toolbar, toolbar_signals[POPUP_CONTEXT_MENU], 0,
2723 (int)event->x_root, (int)event->y_root, event->button,
2726 return return_value;
2729 if (event->type != GDK_BUTTON_PRESS)
2732 window = gtk_widget_get_toplevel (toolbar);
2736 gboolean window_drag = FALSE;
2738 gtk_widget_style_get (toolbar,
2739 "window-dragging", &window_drag,
2744 gtk_window_begin_move_drag (GTK_WINDOW (window),
2758 gtk_toolbar_popup_menu (GtkWidget *toolbar)
2760 gboolean return_value;
2761 /* This function is the handler for the "popup menu" keybinding,
2762 * ie., it is called when the user presses Shift F10
2764 g_signal_emit (toolbar, toolbar_signals[POPUP_CONTEXT_MENU], 0,
2765 -1, -1, -1, &return_value);
2767 return return_value;
2773 * Creates a new toolbar.
2775 * Return Value: the newly-created toolbar.
2778 gtk_toolbar_new (void)
2780 GtkToolbar *toolbar;
2782 toolbar = g_object_new (GTK_TYPE_TOOLBAR, NULL);
2784 return GTK_WIDGET (toolbar);
2788 * gtk_toolbar_insert:
2789 * @toolbar: a #GtkToolbar
2790 * @item: a #GtkToolItem
2791 * @pos: the position of the new item
2793 * Insert a #GtkToolItem into the toolbar at position @pos. If @pos is
2794 * 0 the item is prepended to the start of the toolbar. If @pos is
2795 * negative, the item is appended to the end of the toolbar.
2800 gtk_toolbar_insert (GtkToolbar *toolbar,
2804 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2805 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2808 pos = logical_to_physical (toolbar, pos);
2810 toolbar_content_new_tool_item (toolbar, item, FALSE, pos);
2814 * gtk_toolbar_get_item_index:
2815 * @toolbar: a #GtkToolbar
2816 * @item: a #GtkToolItem that is a child of @toolbar
2818 * Returns the position of @item on the toolbar, starting from 0.
2819 * It is an error if @item is not a child of the toolbar.
2821 * Return value: the position of item on the toolbar.
2826 gtk_toolbar_get_item_index (GtkToolbar *toolbar,
2829 GtkToolbarPrivate *priv;
2833 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), -1);
2834 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2835 g_return_val_if_fail (gtk_widget_get_parent (GTK_WIDGET (item)) == GTK_WIDGET (toolbar), -1);
2837 priv = toolbar->priv;
2840 for (list = priv->content; list != NULL; list = list->next)
2842 ToolbarContent *content = list->data;
2845 widget = toolbar_content_get_widget (content);
2847 if (item == GTK_TOOL_ITEM (widget))
2853 return physical_to_logical (toolbar, n);
2857 * gtk_toolbar_set_style:
2858 * @toolbar: a #GtkToolbar.
2859 * @style: the new style for @toolbar.
2861 * Alters the view of @toolbar to display either icons only, text only, or both.
2864 gtk_toolbar_set_style (GtkToolbar *toolbar,
2865 GtkToolbarStyle style)
2867 GtkToolbarPrivate *priv;
2869 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2871 priv = toolbar->priv;
2873 priv->style_set = TRUE;
2874 g_signal_emit (toolbar, toolbar_signals[STYLE_CHANGED], 0, style);
2878 * gtk_toolbar_get_style:
2879 * @toolbar: a #GtkToolbar
2881 * Retrieves whether the toolbar has text, icons, or both . See
2882 * gtk_toolbar_set_style().
2884 * Return value: the current style of @toolbar
2887 gtk_toolbar_get_style (GtkToolbar *toolbar)
2889 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), DEFAULT_TOOLBAR_STYLE);
2891 return toolbar->priv->style;
2895 * gtk_toolbar_unset_style:
2896 * @toolbar: a #GtkToolbar
2898 * Unsets a toolbar style set with gtk_toolbar_set_style(), so that
2899 * user preferences will be used to determine the toolbar style.
2902 gtk_toolbar_unset_style (GtkToolbar *toolbar)
2904 GtkToolbarPrivate *priv;
2905 GtkToolbarStyle style;
2907 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2909 priv = toolbar->priv;
2911 if (priv->style_set)
2913 GtkSettings *settings = toolbar_get_settings (toolbar);
2916 g_object_get (settings,
2917 "gtk-toolbar-style", &style,
2920 style = DEFAULT_TOOLBAR_STYLE;
2922 if (style != priv->style)
2923 g_signal_emit (toolbar, toolbar_signals[STYLE_CHANGED], 0, style);
2925 priv->style_set = FALSE;
2930 * gtk_toolbar_get_n_items:
2931 * @toolbar: a #GtkToolbar
2933 * Returns the number of items on the toolbar.
2935 * Return value: the number of items on the toolbar
2940 gtk_toolbar_get_n_items (GtkToolbar *toolbar)
2942 GtkToolbarPrivate *priv;
2944 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), -1);
2946 priv = toolbar->priv;
2948 return physical_to_logical (toolbar, g_list_length (priv->content));
2952 * gtk_toolbar_get_nth_item:
2953 * @toolbar: a #GtkToolbar
2954 * @n: A position on the toolbar
2956 * Returns the @n<!-- -->'th item on @toolbar, or %NULL if the
2957 * toolbar does not contain an @n<!-- -->'th item.
2959 * Return value: (transfer none): The @n<!-- -->'th #GtkToolItem on @toolbar,
2960 * or %NULL if there isn't an @n<!-- -->'th item.
2965 gtk_toolbar_get_nth_item (GtkToolbar *toolbar,
2968 GtkToolbarPrivate *priv;
2969 ToolbarContent *content;
2972 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), NULL);
2974 priv = toolbar->priv;
2976 n_items = gtk_toolbar_get_n_items (toolbar);
2978 if (n < 0 || n >= n_items)
2981 content = g_list_nth_data (priv->content, logical_to_physical (toolbar, n));
2984 g_assert (!toolbar_content_is_placeholder (content));
2986 return GTK_TOOL_ITEM (toolbar_content_get_widget (content));
2990 * gtk_toolbar_get_icon_size:
2991 * @toolbar: a #GtkToolbar
2993 * Retrieves the icon size for the toolbar. See gtk_toolbar_set_icon_size().
2995 * Return value: (type int): the current icon size for the icons on
2999 gtk_toolbar_get_icon_size (GtkToolbar *toolbar)
3001 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), DEFAULT_ICON_SIZE);
3003 return toolbar->priv->icon_size;
3007 * gtk_toolbar_get_relief_style:
3008 * @toolbar: a #GtkToolbar
3010 * Returns the relief style of buttons on @toolbar. See
3011 * gtk_button_set_relief().
3013 * Return value: The relief style of buttons on @toolbar.
3018 gtk_toolbar_get_relief_style (GtkToolbar *toolbar)
3020 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), GTK_RELIEF_NONE);
3022 return get_button_relief (toolbar);
3026 * gtk_toolbar_set_show_arrow:
3027 * @toolbar: a #GtkToolbar
3028 * @show_arrow: Whether to show an overflow menu
3030 * Sets whether to show an overflow menu when
3031 * @toolbar doesn't have room for all items on it. If %TRUE,
3032 * items that there are not room are available through an
3038 gtk_toolbar_set_show_arrow (GtkToolbar *toolbar,
3039 gboolean show_arrow)
3041 GtkToolbarPrivate *priv;
3043 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3045 priv = toolbar->priv;
3047 show_arrow = show_arrow != FALSE;
3049 if (priv->show_arrow != show_arrow)
3051 priv->show_arrow = show_arrow;
3053 if (!priv->show_arrow)
3054 gtk_widget_hide (priv->arrow_button);
3056 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3057 g_object_notify (G_OBJECT (toolbar), "show-arrow");
3062 * gtk_toolbar_get_show_arrow:
3063 * @toolbar: a #GtkToolbar
3065 * Returns whether the toolbar has an overflow menu.
3066 * See gtk_toolbar_set_show_arrow().
3068 * Return value: %TRUE if the toolbar has an overflow menu.
3073 gtk_toolbar_get_show_arrow (GtkToolbar *toolbar)
3075 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
3077 return toolbar->priv->show_arrow;
3081 * gtk_toolbar_get_drop_index:
3082 * @toolbar: a #GtkToolbar
3083 * @x: x coordinate of a point on the toolbar
3084 * @y: y coordinate of a point on the toolbar
3086 * Returns the position corresponding to the indicated point on
3087 * @toolbar. This is useful when dragging items to the toolbar:
3088 * this function returns the position a new item should be
3091 * @x and @y are in @toolbar coordinates.
3093 * Return value: The position corresponding to the point (@x, @y) on the toolbar.
3098 gtk_toolbar_get_drop_index (GtkToolbar *toolbar,
3102 g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), -1);
3104 return physical_to_logical (toolbar, find_drop_index (toolbar, x, y));
3108 gtk_toolbar_dispose (GObject *object)
3110 GtkToolbar *toolbar = GTK_TOOLBAR (object);
3111 GtkToolbarPrivate *priv = toolbar->priv;
3113 if (priv->arrow_button)
3115 gtk_widget_unparent (priv->arrow_button);
3116 priv->arrow_button = NULL;
3121 g_signal_handlers_disconnect_by_func (priv->menu,
3122 menu_deactivated, toolbar);
3123 gtk_widget_destroy (GTK_WIDGET (priv->menu));
3127 if (priv->settings_connection > 0)
3129 g_signal_handler_disconnect (priv->settings, priv->settings_connection);
3130 priv->settings_connection = 0;
3133 g_clear_object (&priv->settings);
3135 G_OBJECT_CLASS (gtk_toolbar_parent_class)->dispose (object);
3139 gtk_toolbar_finalize (GObject *object)
3141 GtkToolbar *toolbar = GTK_TOOLBAR (object);
3142 GtkToolbarPrivate *priv = toolbar->priv;
3144 g_list_free_full (priv->content, (GDestroyNotify)toolbar_content_free);
3146 g_timer_destroy (priv->timer);
3149 g_source_remove (priv->idle_id);
3151 G_OBJECT_CLASS (gtk_toolbar_parent_class)->finalize (object);
3155 * gtk_toolbar_set_icon_size:
3156 * @toolbar: A #GtkToolbar
3157 * @icon_size: (type int): The #GtkIconSize that stock icons in the
3158 * toolbar shall have.
3160 * This function sets the size of stock icons in the toolbar. You
3161 * can call it both before you add the icons and after they've been
3162 * added. The size you set will override user preferences for the default
3165 * This should only be used for special-purpose toolbars, normal
3166 * application toolbars should respect the user preferences for the
3170 gtk_toolbar_set_icon_size (GtkToolbar *toolbar,
3171 GtkIconSize icon_size)
3173 GtkToolbarPrivate *priv;
3175 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3176 g_return_if_fail (icon_size != GTK_ICON_SIZE_INVALID);
3178 priv = toolbar->priv;
3180 if (!priv->icon_size_set)
3182 priv->icon_size_set = TRUE;
3183 g_object_notify (G_OBJECT (toolbar), "icon-size-set");
3186 if (priv->icon_size == icon_size)
3189 priv->icon_size = icon_size;
3190 g_object_notify (G_OBJECT (toolbar), "icon-size");
3192 gtk_toolbar_reconfigured (toolbar);
3194 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3198 * gtk_toolbar_unset_icon_size:
3199 * @toolbar: a #GtkToolbar
3201 * Unsets toolbar icon size set with gtk_toolbar_set_icon_size(), so that
3202 * user preferences will be used to determine the icon size.
3205 gtk_toolbar_unset_icon_size (GtkToolbar *toolbar)
3207 GtkToolbarPrivate *priv;
3210 g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3212 priv = toolbar->priv;
3214 if (priv->icon_size_set)
3216 GtkSettings *settings = toolbar_get_settings (toolbar);
3220 g_object_get (settings,
3221 "gtk-toolbar-icon-size", &size,
3225 size = DEFAULT_ICON_SIZE;
3227 if (size != priv->icon_size)
3229 gtk_toolbar_set_icon_size (toolbar, size);
3230 g_object_notify (G_OBJECT (toolbar), "icon-size");
3233 priv->icon_size_set = FALSE;
3234 g_object_notify (G_OBJECT (toolbar), "icon-size-set");
3239 * ToolbarContent methods
3247 struct _ToolbarContent
3252 GtkAllocation allocation;
3253 GtkAllocation start_allocation;
3254 GtkAllocation goal_allocation;
3255 guint is_placeholder : 1;
3256 guint disappearing : 1;
3261 toolbar_item_visiblity_notify_cb (GObject *obj,
3265 GtkToolbar *toolbar = user_data;
3267 gtk_toolbar_invalidate_order (toolbar);
3270 static ToolbarContent *
3271 toolbar_content_new_tool_item (GtkToolbar *toolbar,
3273 gboolean is_placeholder,
3276 GtkToolbarPrivate *priv = toolbar->priv;
3277 ToolbarContent *content;
3279 content = g_slice_new0 (ToolbarContent);
3281 content->state = NOT_ALLOCATED;
3282 content->item = item;
3283 content->is_placeholder = is_placeholder;
3285 priv->content = g_list_insert (priv->content, content, pos);
3287 gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (toolbar));
3288 gtk_toolbar_invalidate_order (toolbar);
3290 g_signal_connect (content->item, "notify::visible",
3291 G_CALLBACK (toolbar_item_visiblity_notify_cb), toolbar);
3293 if (!is_placeholder)
3295 priv->num_children++;
3297 gtk_toolbar_stop_sliding (toolbar);
3300 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3301 priv->need_rebuild = TRUE;
3307 toolbar_content_remove (ToolbarContent *content,
3308 GtkToolbar *toolbar)
3310 GtkToolbarPrivate *priv = toolbar->priv;
3312 gtk_toolbar_invalidate_order (toolbar);
3313 gtk_widget_unparent (GTK_WIDGET (content->item));
3315 g_signal_handlers_disconnect_by_func (content->item,
3316 toolbar_item_visiblity_notify_cb,
3319 priv->content = g_list_remove (priv->content, content);
3321 if (!toolbar_content_is_placeholder (content))
3322 priv->num_children--;
3324 gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3325 priv->need_rebuild = TRUE;
3329 toolbar_content_free (ToolbarContent *content)
3331 g_slice_free (ToolbarContent, content);
3335 calculate_max_homogeneous_pixels (GtkWidget *widget)
3337 PangoContext *context;
3338 PangoFontMetrics *metrics;
3341 context = gtk_widget_get_pango_context (widget);
3343 metrics = pango_context_get_metrics (context,
3344 pango_context_get_font_description (context),
3345 pango_context_get_language (context));
3346 char_width = pango_font_metrics_get_approximate_char_width (metrics);
3347 pango_font_metrics_unref (metrics);
3349 return PANGO_PIXELS (MAX_HOMOGENEOUS_N_CHARS * char_width);
3353 toolbar_content_draw (ToolbarContent *content,
3354 GtkContainer *container,
3359 if (content->is_placeholder)
3362 widget = GTK_WIDGET (content->item);
3365 gtk_container_propagate_draw (container, widget, cr);
3369 toolbar_content_visible (ToolbarContent *content,
3370 GtkToolbar *toolbar)
3372 GtkToolbarPrivate *priv = toolbar->priv;
3375 item = content->item;
3377 if (!gtk_widget_get_visible (GTK_WIDGET (item)))
3380 if (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
3381 gtk_tool_item_get_visible_horizontal (item))
3384 if (priv->orientation == GTK_ORIENTATION_VERTICAL &&
3385 gtk_tool_item_get_visible_vertical (item))
3392 toolbar_content_size_request (ToolbarContent *content,
3393 GtkToolbar *toolbar,
3394 GtkRequisition *requisition)
3396 gtk_widget_get_preferred_size (GTK_WIDGET (content->item),
3398 if (content->is_placeholder &&
3399 content->disappearing)
3401 requisition->width = 0;
3402 requisition->height = 0;
3407 toolbar_content_is_homogeneous (ToolbarContent *content,
3408 GtkToolbar *toolbar)
3410 GtkToolbarPrivate *priv = toolbar->priv;
3411 GtkRequisition requisition;
3414 if (priv->max_homogeneous_pixels < 0)
3416 priv->max_homogeneous_pixels =
3417 calculate_max_homogeneous_pixels (GTK_WIDGET (toolbar));
3420 toolbar_content_size_request (content, toolbar, &requisition);
3422 if (requisition.width > priv->max_homogeneous_pixels)
3425 result = gtk_tool_item_get_homogeneous (content->item) &&
3426 !GTK_IS_SEPARATOR_TOOL_ITEM (content->item);
3428 if (gtk_tool_item_get_is_important (content->item) &&
3429 priv->style == GTK_TOOLBAR_BOTH_HORIZ &&
3430 priv->orientation == GTK_ORIENTATION_HORIZONTAL)
3439 toolbar_content_is_placeholder (ToolbarContent *content)
3441 if (content->is_placeholder)
3448 toolbar_content_disappearing (ToolbarContent *content)
3450 if (content->disappearing)
3457 toolbar_content_get_state (ToolbarContent *content)
3459 return content->state;
3463 toolbar_content_child_visible (ToolbarContent *content)
3465 return gtk_widget_get_child_visible (GTK_WIDGET (content->item));
3469 toolbar_content_get_goal_allocation (ToolbarContent *content,
3470 GtkAllocation *allocation)
3472 *allocation = content->goal_allocation;
3476 toolbar_content_get_allocation (ToolbarContent *content,
3477 GtkAllocation *allocation)
3479 *allocation = content->allocation;
3483 toolbar_content_set_start_allocation (ToolbarContent *content,
3484 GtkAllocation *allocation)
3486 content->start_allocation = *allocation;
3490 toolbar_content_get_expand (ToolbarContent *content)
3492 if (!content->disappearing &&
3493 gtk_tool_item_get_expand (content->item))
3500 toolbar_content_set_goal_allocation (ToolbarContent *content,
3501 GtkAllocation *allocation)
3503 content->goal_allocation = *allocation;
3507 toolbar_content_set_child_visible (ToolbarContent *content,
3508 GtkToolbar *toolbar,
3511 gtk_widget_set_child_visible (GTK_WIDGET (content->item),
3516 toolbar_content_get_start_allocation (ToolbarContent *content,
3517 GtkAllocation *start_allocation)
3519 *start_allocation = content->start_allocation;
3523 toolbar_content_size_allocate (ToolbarContent *content,
3524 GtkAllocation *allocation)
3526 content->allocation = *allocation;
3527 gtk_widget_size_allocate (GTK_WIDGET (content->item),
3532 toolbar_content_set_state (ToolbarContent *content,
3535 content->state = state;
3539 toolbar_content_get_widget (ToolbarContent *content)
3541 return GTK_WIDGET (content->item);
3546 toolbar_content_set_disappearing (ToolbarContent *content,
3547 gboolean disappearing)
3549 content->disappearing = disappearing;
3553 toolbar_content_set_size_request (ToolbarContent *content,
3557 gtk_widget_set_size_request (GTK_WIDGET (content->item),
3562 toolbar_content_toolbar_reconfigured (ToolbarContent *content,
3563 GtkToolbar *toolbar)
3565 gtk_tool_item_toolbar_reconfigured (content->item);
3569 toolbar_content_retrieve_menu_item (ToolbarContent *content)
3571 return gtk_tool_item_retrieve_proxy_menu_item (content->item);
3575 toolbar_content_has_proxy_menu_item (ToolbarContent *content)
3577 GtkWidget *menu_item;
3579 if (content->has_menu == YES)
3581 else if (content->has_menu == NO)
3584 menu_item = toolbar_content_retrieve_menu_item (content);
3586 content->has_menu = menu_item? YES : NO;
3588 return menu_item != NULL;
3592 toolbar_content_set_unknown_menu_status (ToolbarContent *content)
3594 content->has_menu = UNKNOWN;
3598 toolbar_content_is_separator (ToolbarContent *content)
3600 return GTK_IS_SEPARATOR_TOOL_ITEM (content->item);
3604 toolbar_content_set_expand (ToolbarContent *content,
3607 gtk_tool_item_set_expand (content->item, expand);
3611 toolbar_content_show_all (ToolbarContent *content)
3615 widget = toolbar_content_get_widget (content);
3617 gtk_widget_show_all (widget);
3623 static GtkReliefStyle
3624 get_button_relief (GtkToolbar *toolbar)
3626 GtkReliefStyle button_relief = GTK_RELIEF_NORMAL;
3628 gtk_widget_style_get (GTK_WIDGET (toolbar),
3629 "button-relief", &button_relief,
3632 return button_relief;
3636 get_max_child_expand (GtkToolbar *toolbar)
3638 gint mexpand = G_MAXINT;
3640 gtk_widget_style_get (GTK_WIDGET (toolbar),
3641 "max-child-expand", &mexpand,
3646 /* GTK+ internal methods */
3649 _gtk_toolbar_get_default_space_size (void)
3651 return DEFAULT_SPACE_SIZE;
3655 _gtk_toolbar_paint_space_line (GtkWidget *widget,
3656 GtkToolbar *toolbar,
3659 GtkOrientation orientation;
3660 GtkStyleContext *context;
3661 GtkStateFlags state;
3664 const gdouble start_fraction = (SPACE_LINE_START / SPACE_LINE_DIVISION);
3665 const gdouble end_fraction = (SPACE_LINE_END / SPACE_LINE_DIVISION);
3667 g_return_if_fail (GTK_IS_WIDGET (widget));
3669 orientation = toolbar ? toolbar->priv->orientation : GTK_ORIENTATION_HORIZONTAL;
3671 context = gtk_widget_get_style_context (widget);
3672 state = gtk_widget_get_state_flags (widget);
3673 width = gtk_widget_get_allocated_width (widget);
3674 height = gtk_widget_get_allocated_height (widget);
3675 gtk_style_context_get_padding (context, state, &padding);
3677 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3679 gboolean wide_separators;
3680 gint separator_width;
3682 gtk_widget_style_get (widget,
3683 "wide-separators", &wide_separators,
3684 "separator-width", &separator_width,
3687 if (wide_separators)
3688 gtk_render_frame (context, cr,
3689 (width - separator_width) / 2,
3690 height * start_fraction,
3692 height * (end_fraction - start_fraction));
3694 gtk_render_line (context, cr,
3695 (width - padding.left) / 2,
3696 height * start_fraction,
3697 (width - padding.left) / 2,
3698 height * end_fraction);
3702 gboolean wide_separators;
3703 gint separator_height;
3705 gtk_widget_style_get (widget,
3706 "wide-separators", &wide_separators,
3707 "separator-height", &separator_height,
3710 if (wide_separators)
3711 gtk_render_frame (context, cr,
3712 width * start_fraction,
3713 (height - separator_height) / 2,
3714 width * (end_fraction - start_fraction),
3717 gtk_render_line (context, cr,
3718 width * start_fraction,
3719 (height - padding.top) / 2,
3720 width * end_fraction,
3721 (height - padding.top) / 2);
3726 _gtk_toolbar_elide_underscores (const gchar *original)
3729 const gchar *p, *end;
3731 gboolean last_underscore;
3736 len = strlen (original);
3737 q = result = g_malloc (len + 1);
3738 last_underscore = FALSE;
3740 end = original + len;
3741 for (p = original; p < end; p++)
3743 if (!last_underscore && *p == '_')
3744 last_underscore = TRUE;
3747 last_underscore = FALSE;
3748 if (original + 2 <= p && p + 1 <= end &&
3749 p[-2] == '(' && p[-1] == '_' && p[0] != '_' && p[1] == ')')
3760 if (last_underscore)
3769 toolbar_get_icon_size (GtkToolShell *shell)
3771 GtkToolbar *toolbar = GTK_TOOLBAR (shell);
3772 GtkToolbarPrivate *priv = toolbar->priv;
3774 return priv->icon_size;
3777 static GtkOrientation
3778 toolbar_get_orientation (GtkToolShell *shell)
3780 GtkToolbar *toolbar = GTK_TOOLBAR (shell);
3781 GtkToolbarPrivate *priv = toolbar->priv;
3783 return priv->orientation;
3786 static GtkToolbarStyle
3787 toolbar_get_style (GtkToolShell *shell)
3789 GtkToolbar *toolbar = GTK_TOOLBAR (shell);
3790 GtkToolbarPrivate *priv = toolbar->priv;
3795 static GtkReliefStyle
3796 toolbar_get_relief_style (GtkToolShell *shell)
3798 return get_button_relief (GTK_TOOLBAR (shell));
3802 toolbar_rebuild_menu (GtkToolShell *shell)
3804 GtkToolbar *toolbar = GTK_TOOLBAR (shell);
3805 GtkToolbarPrivate *priv = toolbar->priv;
3808 priv->need_rebuild = TRUE;
3810 for (list = priv->content; list != NULL; list = list->next)
3812 ToolbarContent *content = list->data;
3814 toolbar_content_set_unknown_menu_status (content);
3817 gtk_widget_queue_resize (GTK_WIDGET (shell));
3820 typedef struct _CountingData CountingData;
3821 struct _CountingData {
3829 count_widget_position (GtkWidget *widget,
3832 CountingData *count = data;
3834 if (!gtk_widget_get_visible (widget))
3837 if (count->widget == widget)
3838 count->found = TRUE;
3839 else if (count->found)
3846 gtk_toolbar_get_visible_position (GtkToolbar *toolbar,
3849 CountingData count = { child, FALSE, 0, 0 };
3851 if (child == (GtkWidget*)toolbar->priv->highlight_tool_item)
3854 /* foreach iterates in visible order */
3855 gtk_container_forall (GTK_CONTAINER (toolbar),
3856 count_widget_position,
3859 g_assert (count.found);
3861 if (toolbar->priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
3862 gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
3865 return count.before;
3869 add_widget_to_path (gpointer data,
3872 GtkWidget *widget = data;
3873 GtkWidgetPath *path = user_data;
3875 if (gtk_widget_get_visible (widget))
3876 gtk_widget_path_append_for_widget (path, widget);
3879 static GtkWidgetPath *
3880 gtk_toolbar_get_path_for_child (GtkContainer *container,
3883 GtkWidgetPath *path;
3884 GtkToolbar *toolbar;
3885 GtkToolbarPrivate *priv;
3886 GtkWidgetPath *sibling_path;
3890 toolbar = GTK_TOOLBAR (container);
3891 priv = toolbar->priv;
3893 /* build a path for all the visible children;
3894 * get_children works in visible order
3896 sibling_path = gtk_widget_path_new ();
3897 children = _gtk_container_get_all_children (container);
3899 if (priv->orientation != GTK_ORIENTATION_HORIZONTAL ||
3900 gtk_widget_get_direction (GTK_WIDGET (toolbar)) != GTK_TEXT_DIR_RTL)
3901 children = g_list_reverse (children);
3903 g_list_foreach (children, add_widget_to_path, sibling_path);
3904 g_list_free (children);
3906 path = _gtk_widget_create_path (GTK_WIDGET (container));
3907 if (gtk_widget_get_visible (child))
3909 vis_index = gtk_toolbar_get_visible_position (toolbar, child);
3911 if (vis_index < gtk_widget_path_length (sibling_path))
3912 gtk_widget_path_append_with_siblings (path,
3916 gtk_widget_path_append_for_widget (path, child);
3919 gtk_widget_path_append_for_widget (path, child);
3921 gtk_widget_path_unref (sibling_path);
3926 gtk_toolbar_invalidate_order_foreach (GtkWidget *widget)
3928 _gtk_widget_invalidate_style_context (widget, GTK_CSS_CHANGE_POSITION | GTK_CSS_CHANGE_SIBLING_POSITION);
3932 gtk_toolbar_invalidate_order (GtkToolbar *toolbar)
3934 gtk_container_forall (GTK_CONTAINER (toolbar),
3935 (GtkCallback) gtk_toolbar_invalidate_order_foreach,
3940 gtk_toolbar_direction_changed (GtkWidget *widget,
3941 GtkTextDirection previous_direction)
3943 GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->direction_changed (widget, previous_direction);
3945 gtk_toolbar_invalidate_order (GTK_TOOLBAR (widget));