]> Pileus Git - ~andy/gtk/blob - gtk/gtktoolbar.c
Updated Persian translations
[~andy/gtk] / gtk / gtktoolbar.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  * GtkToolbar copyright (C) Federico Mena
4  *
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>
8  *
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.
13  *
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.
18  *
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/>.
21  */
22
23 /*
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/.
28  */
29
30
31 #include "config.h"
32
33 #include <math.h>
34 #include <string.h>
35
36 #include "gtktoolbar.h"
37
38 #include "gtkarrow.h"
39 #include "gtkbindings.h"
40 #include "gtkcontainerprivate.h"
41 #include "gtkimage.h"
42 #include "gtklabel.h"
43 #include "gtkmain.h"
44 #include "gtkmarshalers.h"
45 #include "gtkmenu.h"
46 #include "gtkorientable.h"
47 #include "gtkradiobutton.h"
48 #include "gtkradiotoolbutton.h"
49 #include "gtkseparatormenuitem.h"
50 #include "gtkseparatortoolitem.h"
51 #include "gtkstock.h"
52 #include "gtktoolshell.h"
53 #include "gtkbox.h"
54 #include "gtkprivate.h"
55 #include "gtkintl.h"
56 #include "gtktypebuiltins.h"
57 #include "gtkwidgetpath.h"
58 #include "gtkwidgetprivate.h"
59
60
61 /**
62  * SECTION:gtktoolbar
63  * @Short_description: Create bars of buttons and other widgets
64  * @Title: GtkToolbar
65  * @See_also: #GtkToolItem
66  *
67  * A toolbar is created with a call to gtk_toolbar_new().
68  *
69  * A toolbar can contain instances of a subclass of #GtkToolItem. To add
70  * a #GtkToolItem to the a toolbar, use gtk_toolbar_insert(). To remove
71  * an item from the toolbar use gtk_container_remove(). To add a button
72  * to the toolbar, add an instance of #GtkToolButton.
73  *
74  * Toolbar items can be visually grouped by adding instances of
75  * #GtkSeparatorToolItem to the toolbar. If the GtkToolbar child property
76  * "expand" is #TRUE and the property #GtkSeparatorToolItem:draw is set to
77  * #FALSE, the effect is to force all following items to the end of the toolbar.
78  *
79  * Creating a context menu for the toolbar can be done by connecting to
80  * the #GtkToolbar::popup-context-menu signal.
81  */
82
83
84 typedef struct _ToolbarContent ToolbarContent;
85
86 #define DEFAULT_IPADDING    0
87
88 #define DEFAULT_SPACE_SIZE  12
89 #define DEFAULT_SPACE_STYLE GTK_TOOLBAR_SPACE_LINE
90 #define SPACE_LINE_DIVISION 10.0
91 #define SPACE_LINE_START    2.0
92 #define SPACE_LINE_END      8.0
93
94 #define DEFAULT_ICON_SIZE GTK_ICON_SIZE_LARGE_TOOLBAR
95 #define DEFAULT_TOOLBAR_STYLE GTK_TOOLBAR_BOTH
96 #define DEFAULT_ANIMATION_STATE TRUE
97
98 #define MAX_HOMOGENEOUS_N_CHARS 13 /* Items that are wider than this do not participate
99                                     * in the homogeneous game. In units of
100                                     * pango_font_get_estimated_char_width().
101                                     */
102 #define SLIDE_SPEED 600.0          /* How fast the items slide, in pixels per second */
103 #define ACCEL_THRESHOLD 0.18       /* After how much time in seconds will items start speeding up */
104
105
106 struct _GtkToolbarPrivate
107 {
108   GtkMenu         *menu;
109   GtkSettings     *settings;
110
111   GtkIconSize      icon_size;
112   GtkToolbarStyle  style;
113
114   GtkToolItem     *highlight_tool_item;
115   GtkWidget       *arrow;
116   GtkWidget       *arrow_button;
117
118   GdkWindow       *event_window;
119
120   GList           *content;
121
122   GTimer          *timer;
123
124   gulong           settings_connection;
125
126   gint             idle_id;
127   gint             button_maxw;         /* maximum width of homogeneous children */
128   gint             button_maxh;         /* maximum height of homogeneous children */
129   gint             max_homogeneous_pixels;
130   gint             num_children;
131
132   GtkOrientation   orientation;
133
134   guint            animation : 1;
135   guint            icon_size_set : 1;
136   guint            is_sliding : 1;
137   guint            need_rebuild : 1;  /* whether the overflow menu should be regenerated */
138   guint            need_sync : 1;
139   guint            show_arrow : 1;
140   guint            style_set     : 1;
141 };
142
143 /* Properties */
144 enum {
145   PROP_0,
146   PROP_ORIENTATION,
147   PROP_TOOLBAR_STYLE,
148   PROP_SHOW_ARROW,
149   PROP_TOOLTIPS,
150   PROP_ICON_SIZE,
151   PROP_ICON_SIZE_SET
152 };
153
154 /* Child properties */
155 enum {
156   CHILD_PROP_0,
157   CHILD_PROP_EXPAND,
158   CHILD_PROP_HOMOGENEOUS
159 };
160
161 /* Signals */
162 enum {
163   ORIENTATION_CHANGED,
164   STYLE_CHANGED,
165   POPUP_CONTEXT_MENU,
166   FOCUS_HOME_OR_END,
167   LAST_SIGNAL
168 };
169
170 typedef enum {
171   NOT_ALLOCATED,
172   NORMAL,
173   HIDDEN,
174   OVERFLOWN
175 } ItemState;
176
177
178 static void       gtk_toolbar_set_property         (GObject             *object,
179                                                     guint                prop_id,
180                                                     const GValue        *value,
181                                                     GParamSpec          *pspec);
182 static void       gtk_toolbar_get_property         (GObject             *object,
183                                                     guint                prop_id,
184                                                     GValue              *value,
185                                                     GParamSpec          *pspec);
186 static gint       gtk_toolbar_draw                 (GtkWidget           *widget,
187                                                     cairo_t             *cr);
188 static void       gtk_toolbar_realize              (GtkWidget           *widget);
189 static void       gtk_toolbar_unrealize            (GtkWidget           *widget);
190 static void       gtk_toolbar_get_preferred_width  (GtkWidget           *widget,
191                                                     gint                *minimum,
192                                                     gint                *natural);
193 static void       gtk_toolbar_get_preferred_height (GtkWidget           *widget,
194                                                     gint                *minimum,
195                                                     gint                *natural);
196
197 static void       gtk_toolbar_size_allocate        (GtkWidget           *widget,
198                                                     GtkAllocation       *allocation);
199 static void       gtk_toolbar_style_updated        (GtkWidget           *widget);
200 static gboolean   gtk_toolbar_focus                (GtkWidget           *widget,
201                                                     GtkDirectionType     dir);
202 static void       gtk_toolbar_move_focus           (GtkWidget           *widget,
203                                                     GtkDirectionType     dir);
204 static void       gtk_toolbar_screen_changed       (GtkWidget           *widget,
205                                                     GdkScreen           *previous_screen);
206 static void       gtk_toolbar_map                  (GtkWidget           *widget);
207 static void       gtk_toolbar_unmap                (GtkWidget           *widget);
208 static void       gtk_toolbar_set_child_property   (GtkContainer        *container,
209                                                     GtkWidget           *child,
210                                                     guint                property_id,
211                                                     const GValue        *value,
212                                                     GParamSpec          *pspec);
213 static void       gtk_toolbar_get_child_property   (GtkContainer        *container,
214                                                     GtkWidget           *child,
215                                                     guint                property_id,
216                                                     GValue              *value,
217                                                     GParamSpec          *pspec);
218 static void       gtk_toolbar_finalize             (GObject             *object);
219 static void       gtk_toolbar_dispose              (GObject             *object);
220 static void       gtk_toolbar_show_all             (GtkWidget           *widget);
221 static void       gtk_toolbar_add                  (GtkContainer        *container,
222                                                     GtkWidget           *widget);
223 static void       gtk_toolbar_remove               (GtkContainer        *container,
224                                                     GtkWidget           *widget);
225 static void       gtk_toolbar_forall               (GtkContainer        *container,
226                                                     gboolean             include_internals,
227                                                     GtkCallback          callback,
228                                                     gpointer             callback_data);
229 static GType      gtk_toolbar_child_type           (GtkContainer        *container);
230 static GtkWidgetPath * gtk_toolbar_get_path_for_child
231                                                   (GtkContainer        *container,
232                                                    GtkWidget           *child);
233 static void       gtk_toolbar_invalidate_order    (GtkToolbar           *toolbar);
234
235 static void       gtk_toolbar_direction_changed    (GtkWidget           *widget,
236                                                     GtkTextDirection     previous_direction);
237 static void       gtk_toolbar_orientation_changed  (GtkToolbar          *toolbar,
238                                                     GtkOrientation       orientation);
239 static void       gtk_toolbar_real_style_changed   (GtkToolbar          *toolbar,
240                                                     GtkToolbarStyle      style);
241 static gboolean   gtk_toolbar_focus_home_or_end    (GtkToolbar          *toolbar,
242                                                     gboolean             focus_home);
243 static gboolean   gtk_toolbar_button_press         (GtkWidget           *toolbar,
244                                                     GdkEventButton      *event);
245 static gboolean   gtk_toolbar_arrow_button_press   (GtkWidget           *button,
246                                                     GdkEventButton      *event,
247                                                     GtkToolbar          *toolbar);
248 static void       gtk_toolbar_arrow_button_clicked (GtkWidget           *button,
249                                                     GtkToolbar          *toolbar);
250 static void       gtk_toolbar_update_button_relief (GtkToolbar          *toolbar);
251 static gboolean   gtk_toolbar_popup_menu           (GtkWidget           *toolbar);
252 static void       gtk_toolbar_reconfigured         (GtkToolbar          *toolbar);
253
254 static GtkReliefStyle       get_button_relief    (GtkToolbar *toolbar);
255 static gint                 get_internal_padding (GtkToolbar *toolbar);
256 static gint                 get_max_child_expand (GtkToolbar *toolbar);
257 static GtkShadowType        get_shadow_type      (GtkToolbar *toolbar);
258
259 /* methods on ToolbarContent 'class' */
260 static ToolbarContent *toolbar_content_new_tool_item        (GtkToolbar          *toolbar,
261                                                              GtkToolItem         *item,
262                                                              gboolean             is_placeholder,
263                                                              gint                 pos);
264 static void            toolbar_content_remove               (ToolbarContent      *content,
265                                                              GtkToolbar          *toolbar);
266 static void            toolbar_content_free                 (ToolbarContent      *content);
267 static void            toolbar_content_draw                 (ToolbarContent      *content,
268                                                              GtkContainer        *container,
269                                                              cairo_t             *cr);
270 static gboolean        toolbar_content_visible              (ToolbarContent      *content,
271                                                              GtkToolbar          *toolbar);
272 static void            toolbar_content_size_request         (ToolbarContent      *content,
273                                                              GtkToolbar          *toolbar,
274                                                              GtkRequisition      *requisition);
275 static gboolean        toolbar_content_is_homogeneous       (ToolbarContent      *content,
276                                                              GtkToolbar          *toolbar);
277 static gboolean        toolbar_content_is_placeholder       (ToolbarContent      *content);
278 static gboolean        toolbar_content_disappearing         (ToolbarContent      *content);
279 static ItemState       toolbar_content_get_state            (ToolbarContent      *content);
280 static gboolean        toolbar_content_child_visible        (ToolbarContent      *content);
281 static void            toolbar_content_get_goal_allocation  (ToolbarContent      *content,
282                                                              GtkAllocation       *allocation);
283 static void            toolbar_content_get_allocation       (ToolbarContent      *content,
284                                                              GtkAllocation       *allocation);
285 static void            toolbar_content_set_start_allocation (ToolbarContent      *content,
286                                                              GtkAllocation       *new_start_allocation);
287 static void            toolbar_content_get_start_allocation (ToolbarContent      *content,
288                                                              GtkAllocation       *start_allocation);
289 static gboolean        toolbar_content_get_expand           (ToolbarContent      *content);
290 static void            toolbar_content_set_goal_allocation  (ToolbarContent      *content,
291                                                              GtkAllocation       *allocation);
292 static void            toolbar_content_set_child_visible    (ToolbarContent      *content,
293                                                              GtkToolbar          *toolbar,
294                                                              gboolean             visible);
295 static void            toolbar_content_size_allocate        (ToolbarContent      *content,
296                                                              GtkAllocation       *allocation);
297 static void            toolbar_content_set_state            (ToolbarContent      *content,
298                                                              ItemState            new_state);
299 static GtkWidget *     toolbar_content_get_widget           (ToolbarContent      *content);
300 static void            toolbar_content_set_disappearing     (ToolbarContent      *content,
301                                                              gboolean             disappearing);
302 static void            toolbar_content_set_size_request     (ToolbarContent      *content,
303                                                              gint                 width,
304                                                              gint                 height);
305 static void            toolbar_content_toolbar_reconfigured (ToolbarContent      *content,
306                                                              GtkToolbar          *toolbar);
307 static GtkWidget *     toolbar_content_retrieve_menu_item   (ToolbarContent      *content);
308 static gboolean        toolbar_content_has_proxy_menu_item  (ToolbarContent      *content);
309 static gboolean        toolbar_content_is_separator         (ToolbarContent      *content);
310 static void            toolbar_content_show_all             (ToolbarContent      *content);
311 static void            toolbar_content_set_expand           (ToolbarContent      *content,
312                                                              gboolean             expand);
313
314 static void            toolbar_tool_shell_iface_init        (GtkToolShellIface   *iface);
315 static GtkIconSize     toolbar_get_icon_size                (GtkToolShell        *shell);
316 static GtkOrientation  toolbar_get_orientation              (GtkToolShell        *shell);
317 static GtkToolbarStyle toolbar_get_style                    (GtkToolShell        *shell);
318 static GtkReliefStyle  toolbar_get_relief_style             (GtkToolShell        *shell);
319 static void            toolbar_rebuild_menu                 (GtkToolShell        *shell);
320
321
322 G_DEFINE_TYPE_WITH_CODE (GtkToolbar, gtk_toolbar, GTK_TYPE_CONTAINER,
323                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TOOL_SHELL,
324                                                 toolbar_tool_shell_iface_init)
325                          G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE,
326                                                 NULL))
327
328 static guint toolbar_signals[LAST_SIGNAL] = { 0 };
329
330
331 static void
332 add_arrow_bindings (GtkBindingSet   *binding_set,
333                     guint            keysym,
334                     GtkDirectionType dir)
335 {
336   guint keypad_keysym = keysym - GDK_KEY_Left + GDK_KEY_KP_Left;
337   
338   gtk_binding_entry_add_signal (binding_set, keysym, 0,
339                                 "move-focus", 1,
340                                 GTK_TYPE_DIRECTION_TYPE, dir);
341   gtk_binding_entry_add_signal (binding_set, keypad_keysym, 0,
342                                 "move-focus", 1,
343                                 GTK_TYPE_DIRECTION_TYPE, dir);
344 }
345
346 static void
347 add_ctrl_tab_bindings (GtkBindingSet    *binding_set,
348                        GdkModifierType   modifiers,
349                        GtkDirectionType  direction)
350 {
351   gtk_binding_entry_add_signal (binding_set,
352                                 GDK_KEY_Tab, GDK_CONTROL_MASK | modifiers,
353                                 "move-focus", 1,
354                                 GTK_TYPE_DIRECTION_TYPE, direction);
355   gtk_binding_entry_add_signal (binding_set,
356                                 GDK_KEY_KP_Tab, GDK_CONTROL_MASK | modifiers,
357                                 "move-focus", 1,
358                                 GTK_TYPE_DIRECTION_TYPE, direction);
359 }
360
361 static void
362 gtk_toolbar_class_init (GtkToolbarClass *klass)
363 {
364   GObjectClass *gobject_class;
365   GtkWidgetClass *widget_class;
366   GtkContainerClass *container_class;
367   GtkBindingSet *binding_set;
368   
369   gobject_class = (GObjectClass *)klass;
370   widget_class = (GtkWidgetClass *)klass;
371   container_class = (GtkContainerClass *)klass;
372   
373   gobject_class->set_property = gtk_toolbar_set_property;
374   gobject_class->get_property = gtk_toolbar_get_property;
375   gobject_class->finalize = gtk_toolbar_finalize;
376   gobject_class->dispose = gtk_toolbar_dispose;
377   
378   widget_class->button_press_event = gtk_toolbar_button_press;
379   widget_class->draw = gtk_toolbar_draw;
380   widget_class->get_preferred_width = gtk_toolbar_get_preferred_width;
381   widget_class->get_preferred_height = gtk_toolbar_get_preferred_height;
382   widget_class->size_allocate = gtk_toolbar_size_allocate;
383   widget_class->style_updated = gtk_toolbar_style_updated;
384   widget_class->focus = gtk_toolbar_focus;
385
386   gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_TOOL_BAR);
387
388   /* need to override the base class function via override_class_handler,
389    * because the signal slot is not available in GtkWidgetClass
390    */
391   g_signal_override_class_handler ("move-focus",
392                                    GTK_TYPE_TOOLBAR,
393                                    G_CALLBACK (gtk_toolbar_move_focus));
394
395   widget_class->screen_changed = gtk_toolbar_screen_changed;
396   widget_class->realize = gtk_toolbar_realize;
397   widget_class->unrealize = gtk_toolbar_unrealize;
398   widget_class->map = gtk_toolbar_map;
399   widget_class->unmap = gtk_toolbar_unmap;
400   widget_class->popup_menu = gtk_toolbar_popup_menu;
401   widget_class->show_all = gtk_toolbar_show_all;
402   widget_class->direction_changed = gtk_toolbar_direction_changed;
403   
404   container_class->add    = gtk_toolbar_add;
405   container_class->remove = gtk_toolbar_remove;
406   container_class->forall = gtk_toolbar_forall;
407   container_class->child_type = gtk_toolbar_child_type;
408   container_class->get_child_property = gtk_toolbar_get_child_property;
409   container_class->set_child_property = gtk_toolbar_set_child_property;
410   container_class->get_path_for_child = gtk_toolbar_get_path_for_child;
411
412   klass->orientation_changed = gtk_toolbar_orientation_changed;
413   klass->style_changed = gtk_toolbar_real_style_changed;
414   
415   /**
416    * GtkToolbar::orientation-changed:
417    * @toolbar: the object which emitted the signal
418    * @orientation: the new #GtkOrientation of the toolbar
419    *
420    * Emitted when the orientation of the toolbar changes.
421    */
422   toolbar_signals[ORIENTATION_CHANGED] =
423     g_signal_new (I_("orientation-changed"),
424                   G_OBJECT_CLASS_TYPE (klass),
425                   G_SIGNAL_RUN_FIRST,
426                   G_STRUCT_OFFSET (GtkToolbarClass, orientation_changed),
427                   NULL, NULL,
428                   g_cclosure_marshal_VOID__ENUM,
429                   G_TYPE_NONE, 1,
430                   GTK_TYPE_ORIENTATION);
431   /**
432    * GtkToolbar::style-changed:
433    * @toolbar: The #GtkToolbar which emitted the signal
434    * @style: the new #GtkToolbarStyle of the toolbar
435    *
436    * Emitted when the style of the toolbar changes. 
437    */
438   toolbar_signals[STYLE_CHANGED] =
439     g_signal_new (I_("style-changed"),
440                   G_OBJECT_CLASS_TYPE (klass),
441                   G_SIGNAL_RUN_FIRST,
442                   G_STRUCT_OFFSET (GtkToolbarClass, style_changed),
443                   NULL, NULL,
444                   g_cclosure_marshal_VOID__ENUM,
445                   G_TYPE_NONE, 1,
446                   GTK_TYPE_TOOLBAR_STYLE);
447   /**
448    * GtkToolbar::popup-context-menu:
449    * @toolbar: the #GtkToolbar which emitted the signal
450    * @x: the x coordinate of the point where the menu should appear
451    * @y: the y coordinate of the point where the menu should appear
452    * @button: the mouse button the user pressed, or -1
453    *
454    * Emitted when the user right-clicks the toolbar or uses the
455    * keybinding to display a popup menu.
456    *
457    * Application developers should handle this signal if they want
458    * to display a context menu on the toolbar. The context-menu should
459    * appear at the coordinates given by @x and @y. The mouse button
460    * number is given by the @button parameter. If the menu was popped
461    * up using the keybaord, @button is -1.
462    *
463    * Return value: return %TRUE if the signal was handled, %FALSE if not
464    */
465   toolbar_signals[POPUP_CONTEXT_MENU] =
466     g_signal_new (I_("popup-context-menu"),
467                   G_OBJECT_CLASS_TYPE (klass),
468                   G_SIGNAL_RUN_LAST,
469                   G_STRUCT_OFFSET (GtkToolbarClass, popup_context_menu),
470                   _gtk_boolean_handled_accumulator, NULL,
471                   _gtk_marshal_BOOLEAN__INT_INT_INT,
472                   G_TYPE_BOOLEAN, 3,
473                   G_TYPE_INT, G_TYPE_INT,
474                   G_TYPE_INT);
475
476   /**
477    * GtkToolbar::focus-home-or-end:
478    * @toolbar: the #GtkToolbar which emitted the signal
479    * @focus_home: %TRUE if the first item should be focused
480    *
481    * A keybinding signal used internally by GTK+. This signal can't
482    * be used in application code
483    *
484    * Return value: %TRUE if the signal was handled, %FALSE if not
485    */
486   toolbar_signals[FOCUS_HOME_OR_END] =
487     g_signal_new_class_handler (I_("focus-home-or-end"),
488                                 G_OBJECT_CLASS_TYPE (klass),
489                                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
490                                 G_CALLBACK (gtk_toolbar_focus_home_or_end),
491                                 NULL, NULL,
492                                 _gtk_marshal_BOOLEAN__BOOLEAN,
493                                 G_TYPE_BOOLEAN, 1,
494                                 G_TYPE_BOOLEAN);
495
496   /* properties */
497   g_object_class_override_property (gobject_class,
498                                     PROP_ORIENTATION,
499                                     "orientation");
500
501   g_object_class_install_property (gobject_class,
502                                    PROP_TOOLBAR_STYLE,
503                                    g_param_spec_enum ("toolbar-style",
504                                                       P_("Toolbar Style"),
505                                                       P_("How to draw the toolbar"),
506                                                       GTK_TYPE_TOOLBAR_STYLE,
507                                                       DEFAULT_TOOLBAR_STYLE,
508                                                       GTK_PARAM_READWRITE));
509   g_object_class_install_property (gobject_class,
510                                    PROP_SHOW_ARROW,
511                                    g_param_spec_boolean ("show-arrow",
512                                                          P_("Show Arrow"),
513                                                          P_("If an arrow should be shown if the toolbar doesn't fit"),
514                                                          TRUE,
515                                                          GTK_PARAM_READWRITE));
516
517   /**
518    * GtkToolbar:icon-size:
519    *
520    * The size of the icons in a toolbar is normally determined by
521    * the toolbar-icon-size setting. When this property is set, it 
522    * overrides the setting. 
523    * 
524    * This should only be used for special-purpose toolbars, normal
525    * application toolbars should respect the user preferences for the
526    * size of icons.
527    *
528    * Since: 2.10
529    */
530   g_object_class_install_property (gobject_class,
531                                    PROP_ICON_SIZE,
532                                    g_param_spec_int ("icon-size",
533                                                      P_("Icon size"),
534                                                      P_("Size of icons in this toolbar"),
535                                                      0, G_MAXINT,
536                                                      DEFAULT_ICON_SIZE,
537                                                      GTK_PARAM_READWRITE));  
538
539   /**
540    * GtkToolbar:icon-size-set:
541    *
542    * Is %TRUE if the icon-size property has been set.
543    *
544    * Since: 2.10
545    */
546   g_object_class_install_property (gobject_class,
547                                    PROP_ICON_SIZE_SET,
548                                    g_param_spec_boolean ("icon-size-set",
549                                                          P_("Icon size set"),
550                                                          P_("Whether the icon-size property has been set"),
551                                                          FALSE,
552                                                          GTK_PARAM_READWRITE));  
553
554   /* child properties */
555   gtk_container_class_install_child_property (container_class,
556                                               CHILD_PROP_EXPAND,
557                                               g_param_spec_boolean ("expand", 
558                                                                     P_("Expand"), 
559                                                                     P_("Whether the item should receive extra space when the toolbar grows"),
560                                                                     FALSE,
561                                                                     GTK_PARAM_READWRITE));
562   
563   gtk_container_class_install_child_property (container_class,
564                                               CHILD_PROP_HOMOGENEOUS,
565                                               g_param_spec_boolean ("homogeneous", 
566                                                                     P_("Homogeneous"), 
567                                                                     P_("Whether the item should be the same size as other homogeneous items"),
568                                                                     FALSE,
569                                                                     GTK_PARAM_READWRITE));
570   
571   /* style properties */
572   gtk_widget_class_install_style_property (widget_class,
573                                            g_param_spec_int ("space-size",
574                                                              P_("Spacer size"),
575                                                              P_("Size of spacers"),
576                                                              0,
577                                                              G_MAXINT,
578                                                              DEFAULT_SPACE_SIZE,
579                                                              GTK_PARAM_READABLE));
580   
581   gtk_widget_class_install_style_property (widget_class,
582                                            g_param_spec_int ("internal-padding",
583                                                              P_("Internal padding"),
584                                                              P_("Amount of border space between the toolbar shadow and the buttons"),
585                                                              0,
586                                                              G_MAXINT,
587                                                              DEFAULT_IPADDING,
588                                                              GTK_PARAM_READABLE));
589
590   gtk_widget_class_install_style_property (widget_class,
591                                            g_param_spec_int ("max-child-expand",
592                                                              P_("Maximum child expand"),
593                                                              P_("Maximum amount of space an expandable item will be given"),
594                                                              0,
595                                                              G_MAXINT,
596                                                              G_MAXINT,
597                                                              GTK_PARAM_READABLE));
598
599   gtk_widget_class_install_style_property (widget_class,
600                                            g_param_spec_enum ("space-style",
601                                                               P_("Space style"),
602                                                               P_("Whether spacers are vertical lines or just blank"),
603                                                               GTK_TYPE_TOOLBAR_SPACE_STYLE,
604                                                               DEFAULT_SPACE_STYLE,
605                                                               GTK_PARAM_READABLE));
606   
607   gtk_widget_class_install_style_property (widget_class,
608                                            g_param_spec_enum ("button-relief",
609                                                               P_("Button relief"),
610                                                               P_("Type of bevel around toolbar buttons"),
611                                                               GTK_TYPE_RELIEF_STYLE,
612                                                               GTK_RELIEF_NONE,
613                                                               GTK_PARAM_READABLE));
614   gtk_widget_class_install_style_property (widget_class,
615                                            g_param_spec_enum ("shadow-type",
616                                                               P_("Shadow type"),
617                                                               P_("Style of bevel around the toolbar"),
618                                                               GTK_TYPE_SHADOW_TYPE,
619                                                               GTK_SHADOW_OUT,
620                                                               GTK_PARAM_READABLE));
621
622   binding_set = gtk_binding_set_by_class (klass);
623   
624   add_arrow_bindings (binding_set, GDK_KEY_Left, GTK_DIR_LEFT);
625   add_arrow_bindings (binding_set, GDK_KEY_Right, GTK_DIR_RIGHT);
626   add_arrow_bindings (binding_set, GDK_KEY_Up, GTK_DIR_UP);
627   add_arrow_bindings (binding_set, GDK_KEY_Down, GTK_DIR_DOWN);
628   
629   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
630                                 "focus-home-or-end", 1,
631                                 G_TYPE_BOOLEAN, TRUE);
632   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
633                                 "focus-home-or-end", 1,
634                                 G_TYPE_BOOLEAN, TRUE);
635   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
636                                 "focus-home-or-end", 1,
637                                 G_TYPE_BOOLEAN, FALSE);
638   gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
639                                 "focus-home-or-end", 1,
640                                 G_TYPE_BOOLEAN, FALSE);
641   
642   add_ctrl_tab_bindings (binding_set, 0, GTK_DIR_TAB_FORWARD);
643   add_ctrl_tab_bindings (binding_set, GDK_SHIFT_MASK, GTK_DIR_TAB_BACKWARD);
644
645   g_type_class_add_private (gobject_class, sizeof (GtkToolbarPrivate));
646 }
647
648 static void
649 toolbar_tool_shell_iface_init (GtkToolShellIface *iface)
650 {
651   iface->get_icon_size    = toolbar_get_icon_size;
652   iface->get_orientation  = toolbar_get_orientation;
653   iface->get_style        = toolbar_get_style;
654   iface->get_relief_style = toolbar_get_relief_style;
655   iface->rebuild_menu     = toolbar_rebuild_menu;
656 }
657
658 static void
659 gtk_toolbar_init (GtkToolbar *toolbar)
660 {
661   GtkToolbarPrivate *priv;
662   GtkStyleContext *context;
663
664   toolbar->priv = G_TYPE_INSTANCE_GET_PRIVATE (toolbar,
665                                                GTK_TYPE_TOOLBAR,
666                                                GtkToolbarPrivate);
667   priv = toolbar->priv;
668
669   gtk_widget_set_can_focus (GTK_WIDGET (toolbar), FALSE);
670   gtk_widget_set_has_window (GTK_WIDGET (toolbar), FALSE);
671
672   priv->orientation = GTK_ORIENTATION_HORIZONTAL;
673   priv->style = DEFAULT_TOOLBAR_STYLE;
674   priv->icon_size = DEFAULT_ICON_SIZE;
675   priv->animation = DEFAULT_ANIMATION_STATE;
676
677   priv->arrow_button = gtk_toggle_button_new ();
678   g_signal_connect (priv->arrow_button, "button-press-event",
679                     G_CALLBACK (gtk_toolbar_arrow_button_press), toolbar);
680   g_signal_connect (priv->arrow_button, "clicked",
681                     G_CALLBACK (gtk_toolbar_arrow_button_clicked), toolbar);
682   gtk_button_set_relief (GTK_BUTTON (priv->arrow_button),
683                          get_button_relief (toolbar));
684
685   gtk_button_set_focus_on_click (GTK_BUTTON (priv->arrow_button), FALSE);
686
687   priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
688   gtk_widget_set_name (priv->arrow, "gtk-toolbar-arrow");
689   gtk_widget_show (priv->arrow);
690   gtk_container_add (GTK_CONTAINER (priv->arrow_button), priv->arrow);
691   
692   gtk_widget_set_parent (priv->arrow_button, GTK_WIDGET (toolbar));
693   
694   /* which child position a drop will occur at */
695   priv->menu = NULL;
696   priv->show_arrow = TRUE;
697   priv->settings = NULL;
698   
699   priv->max_homogeneous_pixels = -1;
700   
701   priv->timer = g_timer_new ();
702
703   context = gtk_widget_get_style_context (GTK_WIDGET (toolbar));
704   gtk_style_context_add_class (context, GTK_STYLE_CLASS_TOOLBAR);
705 }
706
707 static void
708 gtk_toolbar_set_property (GObject      *object,
709                           guint         prop_id,
710                           const GValue *value,
711                           GParamSpec   *pspec)
712 {
713   GtkToolbar *toolbar = GTK_TOOLBAR (object);
714   GtkToolbarPrivate *priv = toolbar->priv;
715
716   switch (prop_id)
717     {
718     case PROP_ORIENTATION:
719       g_signal_emit (toolbar, toolbar_signals[ORIENTATION_CHANGED], 0,
720                      g_value_get_enum (value));
721       break;
722     case PROP_TOOLBAR_STYLE:
723       gtk_toolbar_set_style (toolbar, g_value_get_enum (value));
724       break;
725     case PROP_SHOW_ARROW:
726       gtk_toolbar_set_show_arrow (toolbar, g_value_get_boolean (value));
727       break;
728     case PROP_ICON_SIZE:
729       gtk_toolbar_set_icon_size (toolbar, g_value_get_int (value));
730       break;
731     case PROP_ICON_SIZE_SET:
732       if (g_value_get_boolean (value))
733         priv->icon_size_set = TRUE;
734       else
735         gtk_toolbar_unset_icon_size (toolbar);
736       break;
737     default:
738       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
739       break;
740     }
741 }
742
743 static void
744 gtk_toolbar_get_property (GObject    *object,
745                           guint       prop_id,
746                           GValue     *value,
747                           GParamSpec *pspec)
748 {
749   GtkToolbar *toolbar = GTK_TOOLBAR (object);
750   GtkToolbarPrivate *priv = toolbar->priv;
751
752   switch (prop_id)
753     {
754     case PROP_ORIENTATION:
755       g_value_set_enum (value, priv->orientation);
756       break;
757     case PROP_TOOLBAR_STYLE:
758       g_value_set_enum (value, priv->style);
759       break;
760     case PROP_SHOW_ARROW:
761       g_value_set_boolean (value, priv->show_arrow);
762       break;
763     case PROP_ICON_SIZE:
764       g_value_set_int (value, gtk_toolbar_get_icon_size (toolbar));
765       break;
766     case PROP_ICON_SIZE_SET:
767       g_value_set_boolean (value, priv->icon_size_set);
768       break;
769     default:
770       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
771       break;
772     }
773 }
774
775 static void
776 gtk_toolbar_map (GtkWidget *widget)
777 {
778   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
779   GtkToolbarPrivate *priv = toolbar->priv;
780
781   GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->map (widget);
782
783   if (priv->event_window)
784     gdk_window_show_unraised (priv->event_window);
785 }
786
787 static void
788 gtk_toolbar_unmap (GtkWidget *widget)
789 {
790   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
791   GtkToolbarPrivate *priv = toolbar->priv;
792
793   if (priv->event_window)
794     gdk_window_hide (priv->event_window);
795   
796   GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->unmap (widget);
797 }
798
799 static void
800 gtk_toolbar_realize (GtkWidget *widget)
801 {
802   GtkAllocation allocation;
803   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
804   GtkToolbarPrivate *priv = toolbar->priv;
805   GdkWindow *window;
806   GdkWindowAttr attributes;
807   gint attributes_mask;
808   guint border_width;
809
810   gtk_widget_set_realized (widget, TRUE);
811
812   gtk_widget_get_allocation (widget, &allocation);
813   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
814
815   attributes.wclass = GDK_INPUT_ONLY;
816   attributes.window_type = GDK_WINDOW_CHILD;
817   attributes.x = allocation.x + border_width;
818   attributes.y = allocation.y + border_width;
819   attributes.width = allocation.width - border_width * 2;
820   attributes.height = allocation.height - border_width * 2;
821   attributes.event_mask = gtk_widget_get_events (widget);
822   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
823                             GDK_BUTTON_RELEASE_MASK |
824                             GDK_ENTER_NOTIFY_MASK |
825                             GDK_LEAVE_NOTIFY_MASK);
826
827   attributes_mask = GDK_WA_X | GDK_WA_Y;
828
829   window = gtk_widget_get_parent_window (widget);
830   gtk_widget_set_window (widget, window);
831   g_object_ref (window);
832
833   priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
834                                        &attributes, attributes_mask);
835   gdk_window_set_user_data (priv->event_window, toolbar);
836 }
837
838 static void
839 gtk_toolbar_unrealize (GtkWidget *widget)
840 {
841   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
842   GtkToolbarPrivate *priv = toolbar->priv;
843
844   if (priv->event_window)
845     {
846       gdk_window_set_user_data (priv->event_window, NULL);
847       gdk_window_destroy (priv->event_window);
848       priv->event_window = NULL;
849     }
850
851   GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->unrealize (widget);
852 }
853
854 static gint
855 gtk_toolbar_draw (GtkWidget *widget,
856                   cairo_t   *cr)
857 {
858   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
859   GtkToolbarPrivate *priv = toolbar->priv;
860   GtkStyleContext *context;
861   GList *list;
862   guint border_width;
863
864   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
865   context = gtk_widget_get_style_context (widget);
866
867   gtk_render_background (context, cr, border_width, border_width,
868                          gtk_widget_get_allocated_width (widget) - 2 * border_width,
869                          gtk_widget_get_allocated_height (widget) - 2 * border_width);
870   gtk_render_frame (context, cr, border_width, border_width,
871                     gtk_widget_get_allocated_width (widget) - 2 * border_width,
872                     gtk_widget_get_allocated_height (widget) - 2 * border_width);
873
874   for (list = priv->content; list != NULL; list = list->next)
875     {
876       ToolbarContent *content = list->data;
877       
878       toolbar_content_draw (content, GTK_CONTAINER (widget), cr);
879     }
880   
881   gtk_container_propagate_draw (GTK_CONTAINER (widget),
882                                 priv->arrow_button,
883                                 cr);
884
885   return FALSE;
886 }
887
888 static void
889 gtk_toolbar_size_request (GtkWidget      *widget,
890                           GtkRequisition *requisition)
891 {
892   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
893   GtkToolbarPrivate *priv = toolbar->priv;
894   GList *list;
895   gint max_child_height;
896   gint max_child_width;
897   gint max_homogeneous_child_width;
898   gint max_homogeneous_child_height;
899   gint homogeneous_size;
900   gint long_req;
901   gint pack_front_size;
902   gint ipadding;
903   guint border_width;
904   GtkRequisition arrow_requisition;
905   
906   max_homogeneous_child_width = 0;
907   max_homogeneous_child_height = 0;
908   max_child_width = 0;
909   max_child_height = 0;
910   for (list = priv->content; list != NULL; list = list->next)
911     {
912       GtkRequisition requisition;
913       ToolbarContent *content = list->data;
914       
915       if (!toolbar_content_visible (content, toolbar))
916         continue;
917       
918       toolbar_content_size_request (content, toolbar, &requisition);
919
920       max_child_width = MAX (max_child_width, requisition.width);
921       max_child_height = MAX (max_child_height, requisition.height);
922       
923       if (toolbar_content_is_homogeneous (content, toolbar))
924         {
925           max_homogeneous_child_width = MAX (max_homogeneous_child_width, requisition.width);
926           max_homogeneous_child_height = MAX (max_homogeneous_child_height, requisition.height);
927         }
928     }
929   
930   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
931     homogeneous_size = max_homogeneous_child_width;
932   else
933     homogeneous_size = max_homogeneous_child_height;
934   
935   pack_front_size = 0;
936   for (list = priv->content; list != NULL; list = list->next)
937     {
938       ToolbarContent *content = list->data;
939       guint size;
940       
941       if (!toolbar_content_visible (content, toolbar))
942         continue;
943
944       if (toolbar_content_is_homogeneous (content, toolbar))
945         {
946           size = homogeneous_size;
947         }
948       else
949         {
950           GtkRequisition requisition;
951           
952           toolbar_content_size_request (content, toolbar, &requisition);
953           
954           if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
955             size = requisition.width;
956           else
957             size = requisition.height;
958         }
959
960       pack_front_size += size;
961     }
962   
963   if (priv->show_arrow)
964     {
965       gtk_widget_get_preferred_size (priv->arrow_button,
966                                      &arrow_requisition, NULL);
967
968       if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
969         long_req = arrow_requisition.width;
970       else
971         long_req = arrow_requisition.height;
972       
973       /* There is no point requesting space for the arrow if that would take
974        * up more space than all the items combined
975        */
976       long_req = MIN (long_req, pack_front_size);
977     }
978   else
979     {
980       arrow_requisition.height = 0;
981       arrow_requisition.width = 0;
982       
983       long_req = pack_front_size;
984     }
985   
986   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
987     {
988       requisition->width = long_req;
989       requisition->height = MAX (max_child_height, arrow_requisition.height);
990     }
991   else
992     {
993       requisition->height = long_req;
994       requisition->width = MAX (max_child_width, arrow_requisition.width);
995     }
996   
997   /* Extra spacing */
998   ipadding = get_internal_padding (toolbar);
999
1000   border_width = gtk_container_get_border_width (GTK_CONTAINER (toolbar));
1001   requisition->width += 2 * (ipadding + border_width);
1002   requisition->height += 2 * (ipadding + border_width);
1003   
1004   if (get_shadow_type (toolbar) != GTK_SHADOW_NONE)
1005     {
1006       GtkStyleContext *context;
1007       GtkStateFlags state;
1008       GtkBorder padding;
1009
1010       context = gtk_widget_get_style_context (widget);
1011       state = gtk_widget_get_state_flags (widget);
1012       gtk_style_context_get_padding (context, state, &padding);
1013
1014       requisition->width += padding.left + padding.right;
1015       requisition->height += padding.top + padding.bottom;
1016     }
1017   
1018   priv->button_maxw = max_homogeneous_child_width;
1019   priv->button_maxh = max_homogeneous_child_height;
1020 }
1021
1022 static void
1023 gtk_toolbar_get_preferred_width (GtkWidget *widget,
1024                                  gint      *minimum,
1025                                  gint      *natural)
1026 {
1027   GtkRequisition requisition;
1028
1029   gtk_toolbar_size_request (widget, &requisition);
1030
1031   *minimum = *natural = requisition.width;
1032 }
1033
1034 static void
1035 gtk_toolbar_get_preferred_height (GtkWidget *widget,
1036                                   gint      *minimum,
1037                                   gint      *natural)
1038 {
1039   GtkRequisition requisition;
1040
1041   gtk_toolbar_size_request (widget, &requisition);
1042
1043   *minimum = *natural = requisition.height;
1044 }
1045
1046 static gint
1047 position (GtkToolbar *toolbar,
1048           gint        from,
1049           gint        to,
1050           gdouble     elapsed)
1051 {
1052   GtkToolbarPrivate *priv = toolbar->priv;
1053   gint n_pixels;
1054
1055   if (!priv->animation)
1056     return to;
1057
1058   if (elapsed <= ACCEL_THRESHOLD)
1059     {
1060       n_pixels = SLIDE_SPEED * elapsed;
1061     }
1062   else
1063     {
1064       /* The formula is a second degree polynomial in
1065        * @elapsed that has the line SLIDE_SPEED * @elapsed
1066        * as tangent for @elapsed == ACCEL_THRESHOLD.
1067        * This makes @n_pixels a smooth function of elapsed time.
1068        */
1069       n_pixels = (SLIDE_SPEED / ACCEL_THRESHOLD) * elapsed * elapsed -
1070         SLIDE_SPEED * elapsed + SLIDE_SPEED * ACCEL_THRESHOLD;
1071     }
1072
1073   if (to > from)
1074     return MIN (from + n_pixels, to);
1075   else
1076     return MAX (from - n_pixels, to);
1077 }
1078
1079 static void
1080 compute_intermediate_allocation (GtkToolbar          *toolbar,
1081                                  const GtkAllocation *start,
1082                                  const GtkAllocation *goal,
1083                                  GtkAllocation       *intermediate)
1084 {
1085   GtkToolbarPrivate *priv = toolbar->priv;
1086   gdouble elapsed = g_timer_elapsed (priv->timer, NULL);
1087
1088   intermediate->x      = position (toolbar, start->x, goal->x, elapsed);
1089   intermediate->y      = position (toolbar, start->y, goal->y, elapsed);
1090   intermediate->width  = position (toolbar, start->x + start->width,
1091                                    goal->x + goal->width,
1092                                    elapsed) - intermediate->x;
1093   intermediate->height = position (toolbar, start->y + start->height,
1094                                    goal->y + goal->height,
1095                                    elapsed) - intermediate->y;
1096 }
1097
1098 static void
1099 fixup_allocation_for_rtl (gint           total_size,
1100                           GtkAllocation *allocation)
1101 {
1102   allocation->x += (total_size - (2 * allocation->x + allocation->width));
1103 }
1104
1105 static void
1106 fixup_allocation_for_vertical (GtkAllocation *allocation)
1107 {
1108   gint tmp;
1109   
1110   tmp = allocation->x;
1111   allocation->x = allocation->y;
1112   allocation->y = tmp;
1113   
1114   tmp = allocation->width;
1115   allocation->width = allocation->height;
1116   allocation->height = tmp;
1117 }
1118
1119 static gint
1120 get_item_size (GtkToolbar     *toolbar,
1121                ToolbarContent *content)
1122 {
1123   GtkToolbarPrivate *priv = toolbar->priv;
1124   GtkRequisition requisition;
1125   
1126   toolbar_content_size_request (content, toolbar, &requisition);
1127
1128   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1129     {
1130       if (toolbar_content_is_homogeneous (content, toolbar))
1131         return priv->button_maxw;
1132       else
1133         return requisition.width;
1134     }
1135   else
1136     {
1137       if (toolbar_content_is_homogeneous (content, toolbar))
1138         return priv->button_maxh;
1139       else
1140         return requisition.height;
1141     }
1142 }
1143
1144 static gboolean
1145 slide_idle_handler (gpointer data)
1146 {
1147   GtkToolbar *toolbar = GTK_TOOLBAR (data);
1148   GtkToolbarPrivate *priv = toolbar->priv;
1149   GList *list;
1150
1151   if (priv->need_sync)
1152     {
1153       gdk_flush ();
1154       priv->need_sync = FALSE;
1155     }
1156   
1157   for (list = priv->content; list != NULL; list = list->next)
1158     {
1159       ToolbarContent *content = list->data;
1160       ItemState state;
1161       GtkAllocation goal_allocation;
1162       GtkAllocation allocation;
1163       gboolean cont;
1164
1165       state = toolbar_content_get_state (content);
1166       toolbar_content_get_goal_allocation (content, &goal_allocation);
1167       toolbar_content_get_allocation (content, &allocation);
1168       
1169       cont = FALSE;
1170       
1171       if (state == NOT_ALLOCATED)
1172         {
1173           /* an unallocated item means that size allocate has to
1174            * called at least once more
1175            */
1176           cont = TRUE;
1177         }
1178
1179       /* An invisible item with a goal allocation of
1180        * 0 is already at its goal.
1181        */
1182       if ((state == NORMAL || state == OVERFLOWN) &&
1183           ((goal_allocation.width != 0 &&
1184             goal_allocation.height != 0) ||
1185            toolbar_content_child_visible (content)))
1186         {
1187           if ((goal_allocation.x != allocation.x ||
1188                goal_allocation.y != allocation.y ||
1189                goal_allocation.width != allocation.width ||
1190                goal_allocation.height != allocation.height))
1191             {
1192               /* An item is not in its right position yet. Note
1193                * that OVERFLOWN items do get an allocation in
1194                * gtk_toolbar_size_allocate(). This way you can see
1195                * them slide back in when you drag an item off the
1196                * toolbar.
1197                */
1198               cont = TRUE;
1199             }
1200         }
1201
1202       if (toolbar_content_is_placeholder (content) &&
1203           toolbar_content_disappearing (content) &&
1204           toolbar_content_child_visible (content))
1205         {
1206           /* A disappearing placeholder is still visible.
1207            */
1208              
1209           cont = TRUE;
1210         }
1211       
1212       if (cont)
1213         {
1214           gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1215           
1216           return TRUE;
1217         }
1218     }
1219   
1220   gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1221
1222   priv->is_sliding = FALSE;
1223   priv->idle_id = 0;
1224
1225   return FALSE;
1226 }
1227
1228 static gboolean
1229 rect_within (GtkAllocation *a1,
1230              GtkAllocation *a2)
1231 {
1232   return (a1->x >= a2->x                         &&
1233           a1->x + a1->width <= a2->x + a2->width &&
1234           a1->y >= a2->y                         &&
1235           a1->y + a1->height <= a2->y + a2->height);
1236 }
1237
1238 static void
1239 gtk_toolbar_begin_sliding (GtkToolbar *toolbar)
1240 {
1241   GtkAllocation allocation;
1242   GtkWidget *widget = GTK_WIDGET (toolbar);
1243   GtkToolbarPrivate *priv = toolbar->priv;
1244   GtkStyleContext *context;
1245   GtkStateFlags state;
1246   GtkBorder padding;
1247   GList *list;
1248   gint cur_x;
1249   gint cur_y;
1250   gint border_width;
1251   gboolean rtl;
1252   gboolean vertical;
1253   
1254   /* Start the sliding. This function copies the allocation of every
1255    * item into content->start_allocation. For items that haven't
1256    * been allocated yet, we calculate their position and save that
1257    * in start_allocatino along with zero width and zero height.
1258    *
1259    * FIXME: It would be nice if we could share this code with
1260    * the equivalent in gtk_widget_size_allocate().
1261    */
1262   priv->is_sliding = TRUE;
1263   
1264   if (!priv->idle_id)
1265     priv->idle_id = gdk_threads_add_idle (slide_idle_handler, toolbar);
1266
1267   gtk_widget_get_allocation (widget, &allocation);
1268   context = gtk_widget_get_style_context (widget);
1269   state = gtk_widget_get_state_flags (widget);
1270   gtk_style_context_get_padding (context, state, &padding);
1271
1272   rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1273   vertical = (priv->orientation == GTK_ORIENTATION_VERTICAL);
1274   border_width = get_internal_padding (toolbar) + gtk_container_get_border_width (GTK_CONTAINER (toolbar));
1275   
1276   if (rtl)
1277     {
1278       cur_x = allocation.width - border_width - padding.right;
1279       cur_y = allocation.height - border_width - padding.top;
1280     }
1281   else
1282     {
1283       cur_x = border_width + padding.left;
1284       cur_y = border_width + padding.top;
1285     }
1286
1287   cur_x += allocation.x;
1288   cur_y += allocation.y;
1289
1290   for (list = priv->content; list != NULL; list = list->next)
1291     {
1292       ToolbarContent *content = list->data;
1293       GtkAllocation new_start_allocation;
1294       GtkAllocation item_allocation;
1295       ItemState state;
1296       
1297       state = toolbar_content_get_state (content);
1298       toolbar_content_get_allocation (content, &item_allocation);
1299       
1300       if ((state == NORMAL &&
1301            rect_within (&item_allocation, &allocation)) ||
1302           state == OVERFLOWN)
1303         {
1304           new_start_allocation = item_allocation;
1305         }
1306       else
1307         {
1308           new_start_allocation.x = cur_x;
1309           new_start_allocation.y = cur_y;
1310           
1311           if (vertical)
1312             {
1313               new_start_allocation.width = allocation.width -
1314                                            2 * border_width -
1315                                            padding.left - padding.right;
1316               new_start_allocation.height = 0;
1317             }
1318           else
1319             {
1320               new_start_allocation.width = 0;
1321               new_start_allocation.height = allocation.height -
1322                                             2 * border_width -
1323                                             padding.top - padding.bottom;
1324             }
1325         }
1326       
1327       if (vertical)
1328         cur_y = new_start_allocation.y + new_start_allocation.height;
1329       else if (rtl)
1330         cur_x = new_start_allocation.x;
1331       else
1332         cur_x = new_start_allocation.x + new_start_allocation.width;
1333       
1334       toolbar_content_set_start_allocation (content, &new_start_allocation);
1335     }
1336
1337   /* This resize will run before the first idle handler. This
1338    * will make sure that items get the right goal allocation
1339    * so that the idle handler will not immediately return
1340    * FALSE
1341    */
1342   gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1343   g_timer_reset (priv->timer);
1344 }
1345
1346 static void
1347 gtk_toolbar_stop_sliding (GtkToolbar *toolbar)
1348 {
1349   GtkToolbarPrivate *priv = toolbar->priv;
1350
1351   if (priv->is_sliding)
1352     {
1353       GList *list;
1354       
1355       priv->is_sliding = FALSE;
1356       
1357       if (priv->idle_id)
1358         {
1359           g_source_remove (priv->idle_id);
1360           priv->idle_id = 0;
1361         }
1362       
1363       list = priv->content;
1364       while (list)
1365         {
1366           ToolbarContent *content = list->data;
1367           list = list->next;
1368
1369           if (toolbar_content_is_placeholder (content))
1370             {
1371               toolbar_content_remove (content, toolbar);
1372               toolbar_content_free (content);
1373             }
1374         }
1375       
1376       gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1377     }
1378 }
1379
1380 static void
1381 remove_item (GtkWidget *menu_item,
1382              gpointer   data)
1383 {
1384   gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (menu_item)),
1385                         menu_item);
1386 }
1387
1388 static void
1389 menu_deactivated (GtkWidget  *menu,
1390                   GtkToolbar *toolbar)
1391 {
1392   GtkToolbarPrivate *priv = toolbar->priv;
1393
1394   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
1395 }
1396
1397 static void
1398 menu_detached (GtkWidget  *widget,
1399                GtkMenu    *menu)
1400 {
1401   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1402   GtkToolbarPrivate *priv = toolbar->priv;
1403
1404   priv->menu = NULL;
1405 }
1406
1407 static void
1408 rebuild_menu (GtkToolbar *toolbar)
1409 {
1410   GtkToolbarPrivate *priv = toolbar->priv;
1411   GList *list, *children;
1412
1413   if (!priv->menu)
1414     {
1415       priv->menu = GTK_MENU (gtk_menu_new());
1416       gtk_menu_attach_to_widget (priv->menu,
1417                                  GTK_WIDGET (toolbar),
1418                                  menu_detached);
1419
1420       g_signal_connect (priv->menu, "deactivate",
1421                         G_CALLBACK (menu_deactivated), toolbar);
1422     }
1423
1424   gtk_container_foreach (GTK_CONTAINER (priv->menu), remove_item, NULL);
1425   
1426   for (list = priv->content; list != NULL; list = list->next)
1427     {
1428       ToolbarContent *content = list->data;
1429       
1430       if (toolbar_content_get_state (content) == OVERFLOWN &&
1431           !toolbar_content_is_placeholder (content))
1432         {
1433           GtkWidget *menu_item = toolbar_content_retrieve_menu_item (content);
1434           
1435           if (menu_item)
1436             {
1437               g_assert (GTK_IS_MENU_ITEM (menu_item));
1438               gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), menu_item);
1439             }
1440         }
1441     }
1442
1443   /* Remove leading and trailing separator items */
1444   children = gtk_container_get_children (GTK_CONTAINER (priv->menu));
1445   
1446   list = children;
1447   while (list && GTK_IS_SEPARATOR_MENU_ITEM (list->data))
1448     {
1449       GtkWidget *child = list->data;
1450       
1451       gtk_container_remove (GTK_CONTAINER (priv->menu), child);
1452       list = list->next;
1453     }
1454   g_list_free (children);
1455
1456   /* Regenerate the list of children so we don't try to remove items twice */
1457   children = gtk_container_get_children (GTK_CONTAINER (priv->menu));
1458
1459   list = g_list_last (children);
1460   while (list && GTK_IS_SEPARATOR_MENU_ITEM (list->data))
1461     {
1462       GtkWidget *child = list->data;
1463
1464       gtk_container_remove (GTK_CONTAINER (priv->menu), child);
1465       list = list->prev;
1466     }
1467   g_list_free (children);
1468
1469   priv->need_rebuild = FALSE;
1470 }
1471
1472 static void
1473 gtk_toolbar_size_allocate (GtkWidget     *widget,
1474                            GtkAllocation *allocation)
1475 {
1476   GtkAllocation widget_allocation;
1477   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1478   GtkToolbarPrivate *priv = toolbar->priv;
1479   GtkAllocation *allocations;
1480   ItemState *new_states;
1481   GtkAllocation arrow_allocation;
1482   GtkStyleContext *context;
1483   GtkStateFlags state;
1484   GtkBorder padding;
1485   gint arrow_size;
1486   gint size, pos, short_size;
1487   GList *list;
1488   gint i;
1489   gboolean need_arrow;
1490   gint n_expand_items;
1491   gint border_width;
1492   gint available_size;
1493   gint n_items;
1494   gint needed_size;
1495   GtkRequisition arrow_requisition;
1496   gboolean overflowing;
1497   gboolean size_changed;
1498   GtkAllocation item_area;
1499   GtkShadowType shadow_type;
1500
1501   context = gtk_widget_get_style_context (widget);
1502   state = gtk_widget_get_state_flags (widget);
1503   gtk_style_context_get_padding (context, state, &padding);
1504
1505   gtk_widget_get_allocation (widget, &widget_allocation);
1506   size_changed = FALSE;
1507   if (widget_allocation.x != allocation->x ||
1508       widget_allocation.y != allocation->y ||
1509       widget_allocation.width != allocation->width ||
1510       widget_allocation.height != allocation->height)
1511     {
1512       size_changed = TRUE;
1513     }
1514
1515   if (size_changed)
1516     gtk_toolbar_stop_sliding (toolbar);
1517
1518   gtk_widget_set_allocation (widget, allocation);
1519
1520   border_width = gtk_container_get_border_width (GTK_CONTAINER (toolbar));
1521
1522   if (gtk_widget_get_realized (widget))
1523     gdk_window_move_resize (priv->event_window,
1524                             allocation->x + border_width,
1525                             allocation->y + border_width,
1526                             allocation->width - border_width * 2,
1527                             allocation->height - border_width * 2);
1528
1529   border_width += get_internal_padding (toolbar);
1530
1531   gtk_widget_get_preferred_size (priv->arrow_button,
1532                                  &arrow_requisition, NULL);
1533
1534   shadow_type = get_shadow_type (toolbar);
1535
1536   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
1537     {
1538       available_size = size = allocation->width - 2 * border_width;
1539       short_size = allocation->height - 2 * border_width;
1540       arrow_size = arrow_requisition.width;
1541
1542       if (shadow_type != GTK_SHADOW_NONE)
1543         {
1544           available_size -= padding.left + padding.right;
1545           short_size -= padding.top + padding.bottom;
1546         }
1547     }
1548   else
1549     {
1550       available_size = size = allocation->height - 2 * border_width;
1551       short_size = allocation->width - 2 * border_width;
1552       arrow_size = arrow_requisition.height;
1553
1554       if (shadow_type != GTK_SHADOW_NONE)
1555         {
1556           available_size -= padding.top + padding.bottom;
1557           short_size -= padding.left + padding.right;
1558         }
1559     }
1560
1561   n_items = g_list_length (priv->content);
1562   allocations = g_new0 (GtkAllocation, n_items);
1563   new_states = g_new0 (ItemState, n_items);
1564
1565   needed_size = 0;
1566   need_arrow = FALSE;
1567   for (list = priv->content; list != NULL; list = list->next)
1568     {
1569       ToolbarContent *content = list->data;
1570
1571       if (toolbar_content_visible (content, toolbar))
1572         {
1573           needed_size += get_item_size (toolbar, content);
1574
1575           /* Do we need an arrow?
1576            *
1577            * Assume we don't, and see if any non-separator item
1578            * with a proxy menu item is then going to overflow.
1579            */
1580           if (needed_size > available_size &&
1581               !need_arrow &&
1582               priv->show_arrow &&
1583               toolbar_content_has_proxy_menu_item (content) &&
1584               !toolbar_content_is_separator (content))
1585             {
1586               need_arrow = TRUE;
1587             }
1588         }
1589     }
1590
1591   if (need_arrow)
1592     size = available_size - arrow_size;
1593   else
1594     size = available_size;
1595
1596   /* calculate widths and states of items */
1597   overflowing = FALSE;
1598   for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1599     {
1600       ToolbarContent *content = list->data;
1601       gint item_size;
1602
1603       if (!toolbar_content_visible (content, toolbar))
1604         {
1605           new_states[i] = HIDDEN;
1606           continue;
1607         }
1608
1609       item_size = get_item_size (toolbar, content);
1610       if (item_size <= size && !overflowing)
1611         {
1612           size -= item_size;
1613           allocations[i].width = item_size;
1614           new_states[i] = NORMAL;
1615         }
1616       else
1617         {
1618           overflowing = TRUE;
1619           new_states[i] = OVERFLOWN;
1620           allocations[i].width = item_size;
1621         }
1622     }
1623
1624   /* calculate width of arrow */
1625   if (need_arrow)
1626     {
1627       arrow_allocation.width = arrow_size;
1628       arrow_allocation.height = MAX (short_size, 1);
1629     }
1630
1631   /* expand expandable items */
1632
1633   /* We don't expand when there is an overflow menu,
1634    * because that leads to weird jumps when items get
1635    * moved to the overflow menu and the expanding
1636    * items suddenly get a lot of extra space
1637    */
1638   if (!overflowing)
1639     {
1640       gint max_child_expand;
1641       n_expand_items = 0;
1642
1643       for (i = 0, list = priv->content; list != NULL; list = list->next, ++i)
1644         {
1645           ToolbarContent *content = list->data;
1646
1647           if (toolbar_content_get_expand (content) && new_states[i] == NORMAL)
1648             n_expand_items++;
1649         }
1650
1651       max_child_expand = get_max_child_expand (toolbar);
1652       for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1653         {
1654           ToolbarContent *content = list->data;
1655
1656           if (toolbar_content_get_expand (content) && new_states[i] == NORMAL)
1657             {
1658               gint extra = size / n_expand_items;
1659               if (size % n_expand_items != 0)
1660                 extra++;
1661
1662               if (extra > max_child_expand)
1663                 extra = max_child_expand;
1664
1665               allocations[i].width += extra;
1666               size -= extra;
1667               n_expand_items--;
1668             }
1669         }
1670
1671       g_assert (n_expand_items == 0);
1672     }
1673
1674   /* position items */
1675   pos = border_width;
1676   for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1677     {
1678       /* Both NORMAL and OVERFLOWN items get a position.
1679        * This ensures that sliding will work for OVERFLOWN items too.
1680        */
1681       if (new_states[i] == NORMAL || new_states[i] == OVERFLOWN)
1682         {
1683           allocations[i].x = pos;
1684           allocations[i].y = border_width;
1685           allocations[i].height = short_size;
1686
1687           pos += allocations[i].width;
1688         }
1689     }
1690
1691   /* position arrow */
1692   if (need_arrow)
1693     {
1694       arrow_allocation.x = available_size - border_width - arrow_allocation.width;
1695       arrow_allocation.y = border_width;
1696     }
1697
1698   item_area.x = border_width;
1699   item_area.y = border_width;
1700   item_area.width = available_size - (need_arrow? arrow_size : 0);
1701   item_area.height = short_size;
1702
1703   /* fix up allocations in the vertical or RTL cases */
1704   if (priv->orientation == GTK_ORIENTATION_VERTICAL)
1705     {
1706       for (i = 0; i < n_items; ++i)
1707         fixup_allocation_for_vertical (&(allocations[i]));
1708
1709       if (need_arrow)
1710         fixup_allocation_for_vertical (&arrow_allocation);
1711
1712       fixup_allocation_for_vertical (&item_area);
1713     }
1714   else if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
1715     {
1716       for (i = 0; i < n_items; ++i)
1717         fixup_allocation_for_rtl (available_size, &(allocations[i]));
1718
1719       if (need_arrow)
1720         fixup_allocation_for_rtl (available_size, &arrow_allocation);
1721
1722       fixup_allocation_for_rtl (available_size, &item_area);
1723     }
1724
1725   /* translate the items by allocation->(x,y) */
1726   for (i = 0; i < n_items; ++i)
1727     {
1728       allocations[i].x += allocation->x;
1729       allocations[i].y += allocation->y;
1730
1731       if (shadow_type != GTK_SHADOW_NONE)
1732         {
1733           allocations[i].x += padding.left;
1734           allocations[i].y += padding.top;
1735         }
1736     }
1737
1738   if (need_arrow)
1739     {
1740       arrow_allocation.x += allocation->x;
1741       arrow_allocation.y += allocation->y;
1742
1743       if (shadow_type != GTK_SHADOW_NONE)
1744         {
1745           arrow_allocation.x += padding.left;
1746           arrow_allocation.y += padding.top;
1747         }
1748     }
1749
1750   item_area.x += allocation->x;
1751   item_area.y += allocation->y;
1752   if (shadow_type != GTK_SHADOW_NONE)
1753     {
1754       item_area.x += padding.left;
1755       item_area.y += padding.top;
1756     }
1757
1758   /* did anything change? */
1759   for (list = priv->content, i = 0; list != NULL; list = list->next, i++)
1760     {
1761       ToolbarContent *content = list->data;
1762
1763       if (toolbar_content_get_state (content) == NORMAL &&
1764           new_states[i] != NORMAL)
1765         {
1766           /* an item disappeared and we didn't change size, so begin sliding */
1767           if (!size_changed)
1768             gtk_toolbar_begin_sliding (toolbar);
1769         }
1770     }
1771
1772   /* finally allocate the items */
1773   if (priv->is_sliding)
1774     {
1775       for (list = priv->content, i = 0; list != NULL; list = list->next, i++)
1776         {
1777           ToolbarContent *content = list->data;
1778
1779           toolbar_content_set_goal_allocation (content, &(allocations[i]));
1780         }
1781     }
1782
1783   for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1784     {
1785       ToolbarContent *content = list->data;
1786
1787       if (new_states[i] == OVERFLOWN || new_states[i] == NORMAL)
1788         {
1789           GtkAllocation alloc;
1790           GtkAllocation start_allocation = { 0, };
1791           GtkAllocation goal_allocation;
1792
1793           if (priv->is_sliding)
1794             {
1795               toolbar_content_get_start_allocation (content, &start_allocation);
1796               toolbar_content_get_goal_allocation (content, &goal_allocation);
1797
1798               compute_intermediate_allocation (toolbar,
1799                                                &start_allocation,
1800                                                &goal_allocation,
1801                                                &alloc);
1802
1803               priv->need_sync = TRUE;
1804             }
1805           else
1806             {
1807               alloc = allocations[i];
1808             }
1809
1810           if (alloc.width <= 0 || alloc.height <= 0)
1811             {
1812               toolbar_content_set_child_visible (content, toolbar, FALSE);
1813             }
1814           else
1815             {
1816               if (!rect_within (&alloc, &item_area))
1817                 {
1818                   toolbar_content_set_child_visible (content, toolbar, FALSE);
1819                   toolbar_content_size_allocate (content, &alloc);
1820                 }
1821               else
1822                 {
1823                   toolbar_content_set_child_visible (content, toolbar, TRUE);
1824                   toolbar_content_size_allocate (content, &alloc);
1825                 }
1826             }
1827         }
1828       else
1829         {
1830           toolbar_content_set_child_visible (content, toolbar, FALSE);
1831         }
1832
1833       toolbar_content_set_state (content, new_states[i]);
1834     }
1835
1836   if (priv->menu && priv->need_rebuild)
1837     rebuild_menu (toolbar);
1838
1839   if (need_arrow)
1840     {
1841       gtk_widget_size_allocate (GTK_WIDGET (priv->arrow_button),
1842                                 &arrow_allocation);
1843       gtk_widget_show (GTK_WIDGET (priv->arrow_button));
1844     }
1845   else
1846     {
1847       gtk_widget_hide (GTK_WIDGET (priv->arrow_button));
1848
1849       if (priv->menu && gtk_widget_get_visible (GTK_WIDGET (priv->menu)))
1850         gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
1851     }
1852
1853   g_free (allocations);
1854   g_free (new_states);
1855 }
1856
1857 static void
1858 gtk_toolbar_update_button_relief (GtkToolbar *toolbar)
1859 {
1860   GtkToolbarPrivate *priv = toolbar->priv;
1861   GtkReliefStyle relief;
1862
1863   relief = get_button_relief (toolbar);
1864
1865   if (relief != gtk_button_get_relief (GTK_BUTTON (priv->arrow_button)))
1866     {
1867       gtk_toolbar_reconfigured (toolbar);
1868   
1869       gtk_button_set_relief (GTK_BUTTON (priv->arrow_button), relief);
1870     }
1871 }
1872
1873 static void
1874 gtk_toolbar_style_updated (GtkWidget *widget)
1875 {
1876   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1877   GtkToolbarPrivate *priv = toolbar->priv;
1878
1879   GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->style_updated (widget);
1880
1881   priv->max_homogeneous_pixels = -1;
1882   gtk_toolbar_update_button_relief (GTK_TOOLBAR (widget));
1883 }
1884
1885 static GList *
1886 gtk_toolbar_list_children_in_focus_order (GtkToolbar       *toolbar,
1887                                           GtkDirectionType  dir)
1888 {
1889   GtkToolbarPrivate *priv = toolbar->priv;
1890   GList *result = NULL;
1891   GList *list;
1892   gboolean rtl;
1893   
1894   /* generate list of children in reverse logical order */
1895   
1896   for (list = priv->content; list != NULL; list = list->next)
1897     {
1898       ToolbarContent *content = list->data;
1899       GtkWidget *widget;
1900       
1901       widget = toolbar_content_get_widget (content);
1902       
1903       if (widget)
1904         result = g_list_prepend (result, widget);
1905     }
1906   
1907   result = g_list_prepend (result, priv->arrow_button);
1908   
1909   rtl = (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL);
1910   
1911   /* move in logical order when
1912    *
1913    *    - dir is TAB_FORWARD
1914    *
1915    *    - in RTL mode and moving left or up
1916    *
1917    *    - in LTR mode and moving right or down
1918    */
1919   if (dir == GTK_DIR_TAB_FORWARD                                        ||
1920       (rtl  && (dir == GTK_DIR_UP   || dir == GTK_DIR_LEFT))            ||
1921       (!rtl && (dir == GTK_DIR_DOWN || dir == GTK_DIR_RIGHT)))
1922     {
1923       result = g_list_reverse (result);
1924     }
1925   
1926   return result;
1927 }
1928
1929 static gboolean
1930 gtk_toolbar_focus_home_or_end (GtkToolbar *toolbar,
1931                                gboolean    focus_home)
1932 {
1933   GList *children, *list;
1934   GtkDirectionType dir = focus_home? GTK_DIR_RIGHT : GTK_DIR_LEFT;
1935   
1936   children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);
1937   
1938   if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
1939     {
1940       children = g_list_reverse (children);
1941       
1942       dir = (dir == GTK_DIR_RIGHT)? GTK_DIR_LEFT : GTK_DIR_RIGHT;
1943     }
1944   
1945   for (list = children; list != NULL; list = list->next)
1946     {
1947       GtkWidget *child = list->data;
1948
1949       if (gtk_container_get_focus_child (GTK_CONTAINER (toolbar)) == child)
1950         break;
1951       
1952       if (gtk_widget_get_mapped (child) && gtk_widget_child_focus (child, dir))
1953         break;
1954     }
1955   
1956   g_list_free (children);
1957   
1958   return TRUE;
1959 }   
1960
1961 /* Keybinding handler. This function is called when the user presses
1962  * Ctrl TAB or an arrow key.
1963  */
1964 static void
1965 gtk_toolbar_move_focus (GtkWidget        *widget,
1966                         GtkDirectionType  dir)
1967 {
1968   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1969   GtkContainer *container = GTK_CONTAINER (toolbar);
1970   GtkWidget *focus_child;
1971   GList *list;
1972   gboolean try_focus = FALSE;
1973   GList *children;
1974
1975   focus_child = gtk_container_get_focus_child (container);
1976
1977   if (focus_child && gtk_widget_child_focus (focus_child, dir))
1978     return;
1979   
1980   children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);
1981   
1982   for (list = children; list != NULL; list = list->next)
1983     {
1984       GtkWidget *child = list->data;
1985       
1986       if (try_focus && gtk_widget_get_mapped (child) && gtk_widget_child_focus (child, dir))
1987         break;
1988       
1989       if (child == focus_child)
1990         try_focus = TRUE;
1991     }
1992   
1993   g_list_free (children);
1994 }
1995
1996 /* The focus handler for the toolbar. It called when the user presses
1997  * TAB or otherwise tries to focus the toolbar.
1998  */
1999 static gboolean
2000 gtk_toolbar_focus (GtkWidget        *widget,
2001                    GtkDirectionType  dir)
2002 {
2003   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
2004   GList *children, *list;
2005   gboolean result = FALSE;
2006
2007   /* if focus is already somewhere inside the toolbar then return FALSE.
2008    * The only way focus can stay inside the toolbar is when the user presses
2009    * arrow keys or Ctrl TAB (both of which are handled by the
2010    * gtk_toolbar_move_focus() keybinding function.
2011    */
2012   if (gtk_container_get_focus_child (GTK_CONTAINER (widget)))
2013     return FALSE;
2014
2015   children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);
2016
2017   for (list = children; list != NULL; list = list->next)
2018     {
2019       GtkWidget *child = list->data;
2020       
2021       if (gtk_widget_get_mapped (child) && gtk_widget_child_focus (child, dir))
2022         {
2023           result = TRUE;
2024           break;
2025         }
2026     }
2027
2028   g_list_free (children);
2029
2030   return result;
2031 }
2032
2033 static GtkSettings *
2034 toolbar_get_settings (GtkToolbar *toolbar)
2035 {
2036   return toolbar->priv->settings;
2037 }
2038
2039 static void
2040 style_change_notify (GtkToolbar *toolbar)
2041 {
2042   GtkToolbarPrivate *priv = toolbar->priv;
2043
2044   if (!priv->style_set)
2045     {
2046       /* pretend it was set, then unset, thus reverting to new default */
2047       priv->style_set = TRUE;
2048       gtk_toolbar_unset_style (toolbar);
2049     }
2050 }
2051
2052 static void
2053 icon_size_change_notify (GtkToolbar *toolbar)
2054 {
2055   GtkToolbarPrivate *priv = toolbar->priv;
2056
2057   if (!priv->icon_size_set)
2058     {
2059       /* pretend it was set, then unset, thus reverting to new default */
2060       priv->icon_size_set = TRUE;
2061       gtk_toolbar_unset_icon_size (toolbar);
2062     }
2063 }
2064
2065 static void
2066 animation_change_notify (GtkToolbar *toolbar)
2067 {
2068   GtkToolbarPrivate *priv = toolbar->priv;
2069   GtkSettings *settings = toolbar_get_settings (toolbar);
2070   gboolean animation;
2071
2072   if (settings)
2073     g_object_get (settings,
2074                   "gtk-enable-animations", &animation,
2075                   NULL);
2076   else
2077     animation = DEFAULT_ANIMATION_STATE;
2078
2079   priv->animation = animation;
2080 }
2081
2082 static void
2083 settings_change_notify (GtkSettings      *settings,
2084                         const GParamSpec *pspec,
2085                         GtkToolbar       *toolbar)
2086 {
2087   if (! strcmp (pspec->name, "gtk-toolbar-style"))
2088     style_change_notify (toolbar);
2089   else if (! strcmp (pspec->name, "gtk-toolbar-icon-size"))
2090     icon_size_change_notify (toolbar);
2091   else if (! strcmp (pspec->name, "gtk-enable-animations"))
2092     animation_change_notify (toolbar);
2093 }
2094
2095 static void
2096 gtk_toolbar_screen_changed (GtkWidget *widget,
2097                             GdkScreen *previous_screen)
2098 {
2099   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
2100   GtkToolbarPrivate *priv = toolbar->priv;
2101   GtkSettings *old_settings = toolbar_get_settings (toolbar);
2102   GtkSettings *settings;
2103   
2104   if (gtk_widget_has_screen (GTK_WIDGET (toolbar)))
2105     settings = gtk_widget_get_settings (GTK_WIDGET (toolbar));
2106   else
2107     settings = NULL;
2108   
2109   if (settings == old_settings)
2110     return;
2111   
2112   if (old_settings)
2113     {
2114       g_signal_handler_disconnect (old_settings, priv->settings_connection);
2115
2116       g_object_unref (old_settings);
2117     }
2118
2119   if (settings)
2120     {
2121       priv->settings_connection =
2122         g_signal_connect (settings, "notify",
2123                           G_CALLBACK (settings_change_notify),
2124                           toolbar);
2125
2126       priv->settings = g_object_ref (settings);
2127     }
2128   else
2129     priv->settings = NULL;
2130
2131   style_change_notify (toolbar);
2132   icon_size_change_notify (toolbar);
2133   animation_change_notify (toolbar);
2134 }
2135
2136 static int
2137 find_drop_index (GtkToolbar *toolbar,
2138                  gint        x,
2139                  gint        y)
2140 {
2141   GtkToolbarPrivate *priv = toolbar->priv;
2142   GList *interesting_content;
2143   GList *list;
2144   GtkOrientation orientation;
2145   GtkTextDirection direction;
2146   gint best_distance = G_MAXINT;
2147   gint distance;
2148   gint cursor;
2149   gint pos;
2150   ToolbarContent *best_content;
2151   GtkAllocation allocation;
2152   
2153   /* list items we care about wrt. drag and drop */
2154   interesting_content = NULL;
2155   for (list = priv->content; list != NULL; list = list->next)
2156     {
2157       ToolbarContent *content = list->data;
2158       
2159       if (toolbar_content_get_state (content) == NORMAL)
2160         interesting_content = g_list_prepend (interesting_content, content);
2161     }
2162   interesting_content = g_list_reverse (interesting_content);
2163   
2164   if (!interesting_content)
2165     return 0;
2166   
2167   orientation = priv->orientation;
2168   direction = gtk_widget_get_direction (GTK_WIDGET (toolbar));
2169   
2170   /* distance to first interesting item */
2171   best_content = interesting_content->data;
2172   toolbar_content_get_allocation (best_content, &allocation);
2173   
2174   if (orientation == GTK_ORIENTATION_HORIZONTAL)
2175     {
2176       cursor = x;
2177       
2178       if (direction == GTK_TEXT_DIR_LTR)
2179         pos = allocation.x;
2180       else
2181         pos = allocation.x + allocation.width;
2182     }
2183   else
2184     {
2185       cursor = y;
2186       pos = allocation.y;
2187     }
2188   
2189   best_content = NULL;
2190   best_distance = ABS (pos - cursor);
2191   
2192   /* distance to far end of each item */
2193   for (list = interesting_content; list != NULL; list = list->next)
2194     {
2195       ToolbarContent *content = list->data;
2196       
2197       toolbar_content_get_allocation (content, &allocation);
2198       
2199       if (orientation == GTK_ORIENTATION_HORIZONTAL)
2200         {
2201           if (direction == GTK_TEXT_DIR_LTR)
2202             pos = allocation.x + allocation.width;
2203           else
2204             pos = allocation.x;
2205         }
2206       else
2207         {
2208           pos = allocation.y + allocation.height;
2209         }
2210       
2211       distance = ABS (pos - cursor);
2212       
2213       if (distance < best_distance)
2214         {
2215           best_distance = distance;
2216           best_content = content;
2217         }
2218     }
2219   
2220   g_list_free (interesting_content);
2221   
2222   if (!best_content)
2223     return 0;
2224   else
2225     return g_list_index (priv->content, best_content) + 1;
2226 }
2227
2228 static void
2229 reset_all_placeholders (GtkToolbar *toolbar)
2230 {
2231   GtkToolbarPrivate *priv = toolbar->priv;
2232   GList *list;
2233   
2234   for (list = priv->content; list != NULL; list = list->next)
2235     {
2236       ToolbarContent *content = list->data;
2237       if (toolbar_content_is_placeholder (content))
2238         toolbar_content_set_disappearing (content, TRUE);
2239     }
2240 }
2241
2242 static gint
2243 physical_to_logical (GtkToolbar *toolbar,
2244                      gint        physical)
2245 {
2246   GtkToolbarPrivate *priv = toolbar->priv;
2247   GList *list;
2248   int logical;
2249   
2250   g_assert (physical >= 0);
2251   
2252   logical = 0;
2253   for (list = priv->content; list && physical > 0; list = list->next)
2254     {
2255       ToolbarContent *content = list->data;
2256       
2257       if (!toolbar_content_is_placeholder (content))
2258         logical++;
2259       physical--;
2260     }
2261   
2262   g_assert (physical == 0);
2263   
2264   return logical;
2265 }
2266
2267 static gint
2268 logical_to_physical (GtkToolbar *toolbar,
2269                      gint        logical)
2270 {
2271   GtkToolbarPrivate *priv = toolbar->priv;
2272   GList *list;
2273   gint physical;
2274   
2275   g_assert (logical >= 0);
2276   
2277   physical = 0;
2278   for (list = priv->content; list; list = list->next)
2279     {
2280       ToolbarContent *content = list->data;
2281       
2282       if (!toolbar_content_is_placeholder (content))
2283         {
2284           if (logical == 0)
2285             break;
2286           logical--;
2287         }
2288       
2289       physical++;
2290     }
2291   
2292   g_assert (logical == 0);
2293   
2294   return physical;
2295 }
2296
2297 /**
2298  * gtk_toolbar_set_drop_highlight_item:
2299  * @toolbar: a #GtkToolbar
2300  * @tool_item: (allow-none): a #GtkToolItem, or %NULL to turn of highlighting
2301  * @index_: a position on @toolbar
2302  *
2303  * Highlights @toolbar to give an idea of what it would look like
2304  * if @item was added to @toolbar at the position indicated by @index_.
2305  * If @item is %NULL, highlighting is turned off. In that case @index_ 
2306  * is ignored.
2307  *
2308  * The @tool_item passed to this function must not be part of any widget
2309  * hierarchy. When an item is set as drop highlight item it can not
2310  * added to any widget hierarchy or used as highlight item for another
2311  * toolbar.
2312  * 
2313  * Since: 2.4
2314  **/
2315 void
2316 gtk_toolbar_set_drop_highlight_item (GtkToolbar  *toolbar,
2317                                      GtkToolItem *tool_item,
2318                                      gint         index_)
2319 {
2320   ToolbarContent *content;
2321   GtkToolbarPrivate *priv;
2322   gint n_items;
2323   GtkRequisition requisition;
2324   GtkRequisition old_requisition;
2325   gboolean restart_sliding;
2326   
2327   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2328   g_return_if_fail (tool_item == NULL || GTK_IS_TOOL_ITEM (tool_item));
2329
2330   priv = toolbar->priv;
2331
2332   if (!tool_item)
2333     {
2334       if (priv->highlight_tool_item)
2335         {
2336           gtk_widget_unparent (GTK_WIDGET (priv->highlight_tool_item));
2337           g_object_unref (priv->highlight_tool_item);
2338           priv->highlight_tool_item = NULL;
2339         }
2340       
2341       reset_all_placeholders (toolbar);
2342       gtk_toolbar_begin_sliding (toolbar);
2343       return;
2344     }
2345   
2346   n_items = gtk_toolbar_get_n_items (toolbar);
2347   if (index_ < 0 || index_ > n_items)
2348     index_ = n_items;
2349   
2350   if (tool_item != priv->highlight_tool_item)
2351     {
2352       if (priv->highlight_tool_item)
2353         g_object_unref (priv->highlight_tool_item);
2354       
2355       g_object_ref_sink (tool_item);
2356       
2357       priv->highlight_tool_item = tool_item;
2358       
2359       gtk_widget_set_parent (GTK_WIDGET (priv->highlight_tool_item),
2360                              GTK_WIDGET (toolbar));
2361     }
2362   
2363   index_ = logical_to_physical (toolbar, index_);
2364   
2365   content = g_list_nth_data (priv->content, index_);
2366   
2367   if (index_ > 0)
2368     {
2369       ToolbarContent *prev_content;
2370       
2371       prev_content = g_list_nth_data (priv->content, index_ - 1);
2372       
2373       if (prev_content && toolbar_content_is_placeholder (prev_content))
2374         content = prev_content;
2375     }
2376   
2377   if (!content || !toolbar_content_is_placeholder (content))
2378     {
2379       GtkWidget *placeholder;
2380       
2381       placeholder = GTK_WIDGET (gtk_separator_tool_item_new ());
2382
2383       content = toolbar_content_new_tool_item (toolbar,
2384                                                GTK_TOOL_ITEM (placeholder),
2385                                                TRUE, index_);
2386       gtk_widget_show (placeholder);
2387     }
2388   
2389   g_assert (content);
2390   g_assert (toolbar_content_is_placeholder (content));
2391
2392   gtk_widget_get_preferred_size (GTK_WIDGET (priv->highlight_tool_item),
2393                                  &requisition, NULL);
2394
2395   toolbar_content_set_expand (content, gtk_tool_item_get_expand (tool_item));
2396   
2397   restart_sliding = FALSE;
2398   toolbar_content_size_request (content, toolbar, &old_requisition);
2399   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2400     {
2401       requisition.height = -1;
2402       if (requisition.width != old_requisition.width)
2403         restart_sliding = TRUE;
2404     }
2405   else
2406     {
2407       requisition.width = -1;
2408       if (requisition.height != old_requisition.height)
2409         restart_sliding = TRUE;
2410     }
2411
2412   if (toolbar_content_disappearing (content))
2413     restart_sliding = TRUE;
2414   
2415   reset_all_placeholders (toolbar);
2416   toolbar_content_set_disappearing (content, FALSE);
2417   
2418   toolbar_content_set_size_request (content,
2419                                     requisition.width, requisition.height);
2420   
2421   if (restart_sliding)
2422     gtk_toolbar_begin_sliding (toolbar);
2423 }
2424
2425 static void
2426 gtk_toolbar_get_child_property (GtkContainer *container,
2427                                 GtkWidget    *child,
2428                                 guint         property_id,
2429                                 GValue       *value,
2430                                 GParamSpec   *pspec)
2431 {
2432   GtkToolItem *item = GTK_TOOL_ITEM (child);
2433   
2434   switch (property_id)
2435     {
2436     case CHILD_PROP_HOMOGENEOUS:
2437       g_value_set_boolean (value, gtk_tool_item_get_homogeneous (item));
2438       break;
2439       
2440     case CHILD_PROP_EXPAND:
2441       g_value_set_boolean (value, gtk_tool_item_get_expand (item));
2442       break;
2443       
2444     default:
2445       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
2446       break;
2447     }
2448 }
2449
2450 static void
2451 gtk_toolbar_set_child_property (GtkContainer *container,
2452                                 GtkWidget    *child,
2453                                 guint         property_id,
2454                                 const GValue *value,
2455                                 GParamSpec   *pspec)
2456 {
2457   switch (property_id)
2458     {
2459     case CHILD_PROP_HOMOGENEOUS:
2460       gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (child), g_value_get_boolean (value));
2461       break;
2462       
2463     case CHILD_PROP_EXPAND:
2464       gtk_tool_item_set_expand (GTK_TOOL_ITEM (child), g_value_get_boolean (value));
2465       break;
2466       
2467     default:
2468       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
2469       break;
2470     }
2471 }
2472
2473 static void
2474 gtk_toolbar_show_all (GtkWidget *widget)
2475 {
2476   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
2477   GtkToolbarPrivate *priv = toolbar->priv;
2478   GList *list;
2479
2480   for (list = priv->content; list != NULL; list = list->next)
2481     {
2482       ToolbarContent *content = list->data;
2483       
2484       toolbar_content_show_all (content);
2485     }
2486   
2487   gtk_widget_show (widget);
2488 }
2489
2490 static void
2491 gtk_toolbar_add (GtkContainer *container,
2492                  GtkWidget    *widget)
2493 {
2494   GtkToolbar *toolbar = GTK_TOOLBAR (container);
2495
2496   gtk_toolbar_insert (toolbar, GTK_TOOL_ITEM (widget), -1);
2497 }
2498
2499 static void
2500 gtk_toolbar_remove (GtkContainer *container,
2501                     GtkWidget    *widget)
2502 {
2503   GtkToolbar *toolbar = GTK_TOOLBAR (container);
2504   GtkToolbarPrivate *priv = toolbar->priv;
2505   ToolbarContent *content_to_remove;
2506   GList *list;
2507
2508   content_to_remove = NULL;
2509   for (list = priv->content; list != NULL; list = list->next)
2510     {
2511       ToolbarContent *content = list->data;
2512       GtkWidget *child;
2513       
2514       child = toolbar_content_get_widget (content);
2515       if (child && child == widget)
2516         {
2517           content_to_remove = content;
2518           break;
2519         }
2520     }
2521   
2522   g_return_if_fail (content_to_remove != NULL);
2523   
2524   toolbar_content_remove (content_to_remove, toolbar);
2525   toolbar_content_free (content_to_remove);
2526 }
2527
2528 static void
2529 gtk_toolbar_forall (GtkContainer *container,
2530                     gboolean      include_internals,
2531                     GtkCallback   callback,
2532                     gpointer      callback_data)
2533 {
2534   GtkToolbar *toolbar = GTK_TOOLBAR (container);
2535   GtkToolbarPrivate *priv = toolbar->priv;
2536   GList *list;
2537
2538   g_return_if_fail (callback != NULL);
2539
2540   list = priv->content;
2541   while (list)
2542     {
2543       ToolbarContent *content = list->data;
2544       GList *next = list->next;
2545
2546       if (include_internals || !toolbar_content_is_placeholder (content))
2547         {
2548           GtkWidget *child = toolbar_content_get_widget (content);
2549
2550           if (child)
2551             callback (child, callback_data);
2552         }
2553
2554       list = next;
2555     }
2556
2557   if (include_internals && priv->arrow_button)
2558     callback (priv->arrow_button, callback_data);
2559 }
2560
2561 static GType
2562 gtk_toolbar_child_type (GtkContainer *container)
2563 {
2564   return GTK_TYPE_TOOL_ITEM;
2565 }
2566
2567 static void
2568 gtk_toolbar_reconfigured (GtkToolbar *toolbar)
2569 {
2570   GtkToolbarPrivate *priv = toolbar->priv;
2571   GList *list;
2572   
2573   list = priv->content;
2574   while (list)
2575     {
2576       ToolbarContent *content = list->data;
2577       GList *next = list->next;
2578       
2579       toolbar_content_toolbar_reconfigured (content, toolbar);
2580       
2581       list = next;
2582     }
2583 }
2584
2585 static void
2586 gtk_toolbar_orientation_changed (GtkToolbar    *toolbar,
2587                                  GtkOrientation orientation)
2588 {
2589   GtkToolbarPrivate *priv = toolbar->priv;
2590
2591   if (priv->orientation != orientation)
2592     {
2593       priv->orientation = orientation;
2594       
2595       if (orientation == GTK_ORIENTATION_HORIZONTAL)
2596         gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2597       else
2598         gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
2599       
2600       gtk_toolbar_reconfigured (toolbar);
2601       
2602       gtk_widget_queue_resize (GTK_WIDGET (toolbar));
2603       g_object_notify (G_OBJECT (toolbar), "orientation");
2604     }
2605 }
2606
2607 static void
2608 gtk_toolbar_real_style_changed (GtkToolbar     *toolbar,
2609                                 GtkToolbarStyle style)
2610 {
2611   GtkToolbarPrivate *priv = toolbar->priv;
2612
2613   if (priv->style != style)
2614     {
2615       priv->style = style;
2616
2617       gtk_toolbar_reconfigured (toolbar);
2618       
2619       gtk_widget_queue_resize (GTK_WIDGET (toolbar));
2620       g_object_notify (G_OBJECT (toolbar), "toolbar-style");
2621     }
2622 }
2623
2624 static void
2625 menu_position_func (GtkMenu  *menu,
2626                     gint     *x,
2627                     gint     *y,
2628                     gboolean *push_in,
2629                     gpointer  user_data)
2630 {
2631   GtkAllocation allocation;
2632   GtkToolbar *toolbar = GTK_TOOLBAR (user_data);
2633   GtkToolbarPrivate *priv = toolbar->priv;
2634   GtkRequisition req;
2635   GtkRequisition menu_req;
2636   GdkRectangle monitor;
2637   gint monitor_num;
2638   GdkScreen *screen;
2639
2640   gtk_widget_get_preferred_size (priv->arrow_button,
2641                                  &req, NULL);
2642   gtk_widget_get_preferred_size (GTK_WIDGET (menu),
2643                                  &menu_req, NULL);
2644
2645   screen = gtk_widget_get_screen (GTK_WIDGET (menu));
2646   monitor_num = gdk_screen_get_monitor_at_window (screen,
2647                                                   gtk_widget_get_window (priv->arrow_button));
2648   if (monitor_num < 0)
2649     monitor_num = 0;
2650   gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
2651
2652   gtk_widget_get_allocation (priv->arrow_button, &allocation);
2653
2654   gdk_window_get_origin (gtk_button_get_event_window (GTK_BUTTON (priv->arrow_button)), x, y);
2655   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2656     {
2657       if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_LTR) 
2658         *x += allocation.width - req.width;
2659       else 
2660         *x += req.width - menu_req.width;
2661
2662       if ((*y + allocation.height + menu_req.height) <= monitor.y + monitor.height)
2663         *y += allocation.height;
2664       else if ((*y - menu_req.height) >= monitor.y)
2665         *y -= menu_req.height;
2666       else if (monitor.y + monitor.height - (*y + allocation.height) > *y)
2667         *y += allocation.height;
2668       else
2669         *y -= menu_req.height;
2670     }
2671   else 
2672     {
2673       if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_LTR) 
2674         *x += allocation.width;
2675       else 
2676         *x -= menu_req.width;
2677
2678       if (*y + menu_req.height > monitor.y + monitor.height &&
2679           *y + allocation.height - monitor.y > monitor.y + monitor.height - *y)
2680         *y += allocation.height - menu_req.height;
2681     }
2682
2683   *push_in = FALSE;
2684 }
2685
2686 static void
2687 show_menu (GtkToolbar     *toolbar,
2688            GdkEventButton *event)
2689 {
2690   GtkToolbarPrivate *priv = toolbar->priv;
2691
2692   rebuild_menu (toolbar);
2693
2694   gtk_widget_show_all (GTK_WIDGET (priv->menu));
2695
2696   gtk_menu_popup (priv->menu, NULL, NULL,
2697                   menu_position_func, toolbar,
2698                   event? event->button : 0,
2699                   event? event->time : gtk_get_current_event_time());
2700 }
2701
2702 static void
2703 gtk_toolbar_arrow_button_clicked (GtkWidget  *button,
2704                                   GtkToolbar *toolbar)
2705 {
2706   GtkToolbarPrivate *priv = toolbar->priv;
2707
2708   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->arrow_button)) &&
2709       (!priv->menu || !gtk_widget_get_visible (GTK_WIDGET (priv->menu))))
2710     {
2711       /* We only get here when the button is clicked with the keyboard,
2712        * because mouse button presses result in the menu being shown so
2713        * that priv->menu would be non-NULL and visible.
2714        */
2715       show_menu (toolbar, NULL);
2716       gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
2717     }
2718 }
2719
2720 static gboolean
2721 gtk_toolbar_arrow_button_press (GtkWidget      *button,
2722                                 GdkEventButton *event,
2723                                 GtkToolbar     *toolbar)
2724 {
2725   show_menu (toolbar, event);
2726   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
2727   
2728   return TRUE;
2729 }
2730
2731 static gboolean
2732 gtk_toolbar_button_press (GtkWidget      *toolbar,
2733                           GdkEventButton *event)
2734 {
2735   GtkWidget *window;
2736
2737   if (gdk_event_triggers_context_menu ((GdkEvent *) event))
2738     {
2739       gboolean return_value;
2740
2741       g_signal_emit (toolbar, toolbar_signals[POPUP_CONTEXT_MENU], 0,
2742                      (int)event->x_root, (int)event->y_root, event->button,
2743                      &return_value);
2744
2745       return return_value;
2746     }
2747
2748   if (event->type != GDK_BUTTON_PRESS)
2749     return FALSE;
2750
2751   window = gtk_widget_get_toplevel (toolbar);
2752
2753   if (window)
2754     {
2755       gboolean window_drag = FALSE;
2756
2757       gtk_widget_style_get (toolbar,
2758                             "window-dragging", &window_drag,
2759                             NULL);
2760
2761       if (window_drag)
2762         {
2763           gtk_window_begin_move_drag (GTK_WINDOW (window),
2764                                       event->button,
2765                                       event->x_root,
2766                                       event->y_root,
2767                                       event->time);
2768
2769           return TRUE;
2770         }
2771     }
2772
2773   return FALSE;
2774 }
2775
2776 static gboolean
2777 gtk_toolbar_popup_menu (GtkWidget *toolbar)
2778 {
2779   gboolean return_value;
2780   /* This function is the handler for the "popup menu" keybinding,
2781    * ie., it is called when the user presses Shift F10
2782    */
2783   g_signal_emit (toolbar, toolbar_signals[POPUP_CONTEXT_MENU], 0,
2784                  -1, -1, -1, &return_value);
2785   
2786   return return_value;
2787 }
2788
2789 /**
2790  * gtk_toolbar_new:
2791  * 
2792  * Creates a new toolbar. 
2793  
2794  * Return Value: the newly-created toolbar.
2795  **/
2796 GtkWidget *
2797 gtk_toolbar_new (void)
2798 {
2799   GtkToolbar *toolbar;
2800   
2801   toolbar = g_object_new (GTK_TYPE_TOOLBAR, NULL);
2802   
2803   return GTK_WIDGET (toolbar);
2804 }
2805
2806 /**
2807  * gtk_toolbar_insert:
2808  * @toolbar: a #GtkToolbar
2809  * @item: a #GtkToolItem
2810  * @pos: the position of the new item
2811  *
2812  * Insert a #GtkToolItem into the toolbar at position @pos. If @pos is
2813  * 0 the item is prepended to the start of the toolbar. If @pos is
2814  * negative, the item is appended to the end of the toolbar.
2815  *
2816  * Since: 2.4
2817  **/
2818 void
2819 gtk_toolbar_insert (GtkToolbar  *toolbar,
2820                     GtkToolItem *item,
2821                     gint         pos)
2822 {
2823   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2824   g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2825   
2826   if (pos >= 0)
2827     pos = logical_to_physical (toolbar, pos);
2828
2829   toolbar_content_new_tool_item (toolbar, item, FALSE, pos);
2830 }
2831
2832 /**
2833  * gtk_toolbar_get_item_index:
2834  * @toolbar: a #GtkToolbar
2835  * @item: a #GtkToolItem that is a child of @toolbar
2836  * 
2837  * Returns the position of @item on the toolbar, starting from 0.
2838  * It is an error if @item is not a child of the toolbar.
2839  * 
2840  * Return value: the position of item on the toolbar.
2841  * 
2842  * Since: 2.4
2843  **/
2844 gint
2845 gtk_toolbar_get_item_index (GtkToolbar  *toolbar,
2846                             GtkToolItem *item)
2847 {
2848   GtkToolbarPrivate *priv;
2849   GList *list;
2850   int n;
2851   
2852   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), -1);
2853   g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2854   g_return_val_if_fail (gtk_widget_get_parent (GTK_WIDGET (item)) == GTK_WIDGET (toolbar), -1);
2855
2856   priv = toolbar->priv;
2857
2858   n = 0;
2859   for (list = priv->content; list != NULL; list = list->next)
2860     {
2861       ToolbarContent *content = list->data;
2862       GtkWidget *widget;
2863       
2864       widget = toolbar_content_get_widget (content);
2865       
2866       if (item == GTK_TOOL_ITEM (widget))
2867         break;
2868       
2869       ++n;
2870     }
2871   
2872   return physical_to_logical (toolbar, n);
2873 }
2874
2875 /**
2876  * gtk_toolbar_set_style:
2877  * @toolbar: a #GtkToolbar.
2878  * @style: the new style for @toolbar.
2879  * 
2880  * Alters the view of @toolbar to display either icons only, text only, or both.
2881  **/
2882 void
2883 gtk_toolbar_set_style (GtkToolbar      *toolbar,
2884                        GtkToolbarStyle  style)
2885 {
2886   GtkToolbarPrivate *priv;
2887
2888   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2889
2890   priv = toolbar->priv;
2891
2892   priv->style_set = TRUE;
2893   g_signal_emit (toolbar, toolbar_signals[STYLE_CHANGED], 0, style);
2894 }
2895
2896 /**
2897  * gtk_toolbar_get_style:
2898  * @toolbar: a #GtkToolbar
2899  *
2900  * Retrieves whether the toolbar has text, icons, or both . See
2901  * gtk_toolbar_set_style().
2902  
2903  * Return value: the current style of @toolbar
2904  **/
2905 GtkToolbarStyle
2906 gtk_toolbar_get_style (GtkToolbar *toolbar)
2907 {
2908   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), DEFAULT_TOOLBAR_STYLE);
2909
2910   return toolbar->priv->style;
2911 }
2912
2913 /**
2914  * gtk_toolbar_unset_style:
2915  * @toolbar: a #GtkToolbar
2916  * 
2917  * Unsets a toolbar style set with gtk_toolbar_set_style(), so that
2918  * user preferences will be used to determine the toolbar style.
2919  **/
2920 void
2921 gtk_toolbar_unset_style (GtkToolbar *toolbar)
2922 {
2923   GtkToolbarPrivate *priv;
2924   GtkToolbarStyle style;
2925   
2926   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2927
2928   priv = toolbar->priv;
2929
2930   if (priv->style_set)
2931     {
2932       GtkSettings *settings = toolbar_get_settings (toolbar);
2933       
2934       if (settings)
2935         g_object_get (settings,
2936                       "gtk-toolbar-style", &style,
2937                       NULL);
2938       else
2939         style = DEFAULT_TOOLBAR_STYLE;
2940
2941       if (style != priv->style)
2942         g_signal_emit (toolbar, toolbar_signals[STYLE_CHANGED], 0, style);
2943
2944       priv->style_set = FALSE;
2945     }
2946 }
2947
2948 /**
2949  * gtk_toolbar_get_n_items:
2950  * @toolbar: a #GtkToolbar
2951  * 
2952  * Returns the number of items on the toolbar.
2953  * 
2954  * Return value: the number of items on the toolbar
2955  * 
2956  * Since: 2.4
2957  **/
2958 gint
2959 gtk_toolbar_get_n_items (GtkToolbar *toolbar)
2960 {
2961   GtkToolbarPrivate *priv;
2962
2963   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), -1);
2964
2965   priv = toolbar->priv;
2966
2967   return physical_to_logical (toolbar, g_list_length (priv->content));
2968 }
2969
2970 /**
2971  * gtk_toolbar_get_nth_item:
2972  * @toolbar: a #GtkToolbar
2973  * @n: A position on the toolbar
2974  *
2975  * Returns the @n<!-- -->'th item on @toolbar, or %NULL if the
2976  * toolbar does not contain an @n<!-- -->'th item.
2977  *
2978  * Return value: (transfer none): The @n<!-- -->'th #GtkToolItem on @toolbar,
2979  *     or %NULL if there isn't an @n<!-- -->'th item.
2980  *
2981  * Since: 2.4
2982  **/
2983 GtkToolItem *
2984 gtk_toolbar_get_nth_item (GtkToolbar *toolbar,
2985                           gint        n)
2986 {
2987   GtkToolbarPrivate *priv;
2988   ToolbarContent *content;
2989   gint n_items;
2990   
2991   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), NULL);
2992
2993   priv = toolbar->priv;
2994
2995   n_items = gtk_toolbar_get_n_items (toolbar);
2996   
2997   if (n < 0 || n >= n_items)
2998     return NULL;
2999
3000   content = g_list_nth_data (priv->content, logical_to_physical (toolbar, n));
3001   
3002   g_assert (content);
3003   g_assert (!toolbar_content_is_placeholder (content));
3004   
3005   return GTK_TOOL_ITEM (toolbar_content_get_widget (content));
3006 }
3007
3008 /**
3009  * gtk_toolbar_get_icon_size:
3010  * @toolbar: a #GtkToolbar
3011  *
3012  * Retrieves the icon size for the toolbar. See gtk_toolbar_set_icon_size().
3013  *
3014  * Return value: (type int): the current icon size for the icons on
3015  * the toolbar.
3016  **/
3017 GtkIconSize
3018 gtk_toolbar_get_icon_size (GtkToolbar *toolbar)
3019 {
3020   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), DEFAULT_ICON_SIZE);
3021
3022   return toolbar->priv->icon_size;
3023 }
3024
3025 /**
3026  * gtk_toolbar_get_relief_style:
3027  * @toolbar: a #GtkToolbar
3028  * 
3029  * Returns the relief style of buttons on @toolbar. See
3030  * gtk_button_set_relief().
3031  * 
3032  * Return value: The relief style of buttons on @toolbar.
3033  * 
3034  * Since: 2.4
3035  **/
3036 GtkReliefStyle
3037 gtk_toolbar_get_relief_style (GtkToolbar *toolbar)
3038 {
3039   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), GTK_RELIEF_NONE);
3040   
3041   return get_button_relief (toolbar);
3042 }
3043
3044 /**
3045  * gtk_toolbar_set_show_arrow:
3046  * @toolbar: a #GtkToolbar
3047  * @show_arrow: Whether to show an overflow menu
3048  * 
3049  * Sets whether to show an overflow menu when
3050  * @toolbar doesn't have room for all items on it. If %TRUE,
3051  * items that there are not room are available through an
3052  * overflow menu.
3053  * 
3054  * Since: 2.4
3055  **/
3056 void
3057 gtk_toolbar_set_show_arrow (GtkToolbar *toolbar,
3058                             gboolean    show_arrow)
3059 {
3060   GtkToolbarPrivate *priv;
3061
3062   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3063
3064   priv = toolbar->priv;
3065
3066   show_arrow = show_arrow != FALSE;
3067   
3068   if (priv->show_arrow != show_arrow)
3069     {
3070       priv->show_arrow = show_arrow;
3071       
3072       if (!priv->show_arrow)
3073         gtk_widget_hide (priv->arrow_button);
3074       
3075       gtk_widget_queue_resize (GTK_WIDGET (toolbar));      
3076       g_object_notify (G_OBJECT (toolbar), "show-arrow");
3077     }
3078 }
3079
3080 /**
3081  * gtk_toolbar_get_show_arrow:
3082  * @toolbar: a #GtkToolbar
3083  * 
3084  * Returns whether the toolbar has an overflow menu.
3085  * See gtk_toolbar_set_show_arrow().
3086  * 
3087  * Return value: %TRUE if the toolbar has an overflow menu.
3088  * 
3089  * Since: 2.4
3090  **/
3091 gboolean
3092 gtk_toolbar_get_show_arrow (GtkToolbar *toolbar)
3093 {
3094   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
3095
3096   return toolbar->priv->show_arrow;
3097 }
3098
3099 /**
3100  * gtk_toolbar_get_drop_index:
3101  * @toolbar: a #GtkToolbar
3102  * @x: x coordinate of a point on the toolbar
3103  * @y: y coordinate of a point on the toolbar
3104  *
3105  * Returns the position corresponding to the indicated point on
3106  * @toolbar. This is useful when dragging items to the toolbar:
3107  * this function returns the position a new item should be
3108  * inserted.
3109  *
3110  * @x and @y are in @toolbar coordinates.
3111  * 
3112  * Return value: The position corresponding to the point (@x, @y) on the toolbar.
3113  * 
3114  * Since: 2.4
3115  **/
3116 gint
3117 gtk_toolbar_get_drop_index (GtkToolbar *toolbar,
3118                             gint        x,
3119                             gint        y)
3120 {
3121   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), -1);
3122   
3123   return physical_to_logical (toolbar, find_drop_index (toolbar, x, y));
3124 }
3125
3126 static void
3127 gtk_toolbar_dispose (GObject *object)
3128 {
3129   GtkToolbar *toolbar = GTK_TOOLBAR (object);
3130   GtkToolbarPrivate *priv = toolbar->priv;
3131
3132   if (priv->arrow_button)
3133     {
3134       gtk_widget_unparent (priv->arrow_button);
3135       priv->arrow_button = NULL;
3136     }
3137
3138   if (priv->menu)
3139     {
3140       g_signal_handlers_disconnect_by_func (priv->menu,
3141                                             menu_deactivated, toolbar);
3142       gtk_widget_destroy (GTK_WIDGET (priv->menu));
3143       priv->menu = NULL;
3144     }
3145
3146  G_OBJECT_CLASS (gtk_toolbar_parent_class)->dispose (object);
3147 }
3148
3149 static void
3150 gtk_toolbar_finalize (GObject *object)
3151 {
3152   GtkToolbar *toolbar = GTK_TOOLBAR (object);
3153   GtkToolbarPrivate *priv = toolbar->priv;
3154
3155   g_list_free_full (priv->content, (GDestroyNotify)toolbar_content_free);
3156
3157   g_timer_destroy (priv->timer);
3158
3159   if (priv->idle_id)
3160     g_source_remove (priv->idle_id);
3161
3162   G_OBJECT_CLASS (gtk_toolbar_parent_class)->finalize (object);
3163 }
3164
3165 /**
3166  * gtk_toolbar_set_icon_size:
3167  * @toolbar: A #GtkToolbar
3168  * @icon_size: (type int): The #GtkIconSize that stock icons in the
3169  *     toolbar shall have.
3170  *
3171  * This function sets the size of stock icons in the toolbar. You
3172  * can call it both before you add the icons and after they've been
3173  * added. The size you set will override user preferences for the default
3174  * icon size.
3175  * 
3176  * This should only be used for special-purpose toolbars, normal
3177  * application toolbars should respect the user preferences for the
3178  * size of icons.
3179  **/
3180 void
3181 gtk_toolbar_set_icon_size (GtkToolbar  *toolbar,
3182                            GtkIconSize  icon_size)
3183 {
3184   GtkToolbarPrivate *priv;
3185
3186   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3187   g_return_if_fail (icon_size != GTK_ICON_SIZE_INVALID);
3188
3189   priv = toolbar->priv;
3190
3191   if (!priv->icon_size_set)
3192     {
3193       priv->icon_size_set = TRUE;
3194       g_object_notify (G_OBJECT (toolbar), "icon-size-set");
3195     }
3196
3197   if (priv->icon_size == icon_size)
3198     return;
3199
3200   priv->icon_size = icon_size;
3201   g_object_notify (G_OBJECT (toolbar), "icon-size");
3202   
3203   gtk_toolbar_reconfigured (toolbar);
3204   
3205   gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3206 }
3207
3208 /**
3209  * gtk_toolbar_unset_icon_size:
3210  * @toolbar: a #GtkToolbar
3211  * 
3212  * Unsets toolbar icon size set with gtk_toolbar_set_icon_size(), so that
3213  * user preferences will be used to determine the icon size.
3214  **/
3215 void
3216 gtk_toolbar_unset_icon_size (GtkToolbar *toolbar)
3217 {
3218   GtkToolbarPrivate *priv;
3219   GtkIconSize size;
3220
3221   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3222
3223   priv = toolbar->priv;
3224
3225   if (priv->icon_size_set)
3226     {
3227       GtkSettings *settings = toolbar_get_settings (toolbar);
3228       
3229       if (settings)
3230         {
3231           g_object_get (settings,
3232                         "gtk-toolbar-icon-size", &size,
3233                         NULL);
3234         }
3235       else
3236         size = DEFAULT_ICON_SIZE;
3237
3238       if (size != priv->icon_size)
3239         {
3240           gtk_toolbar_set_icon_size (toolbar, size);
3241           g_object_notify (G_OBJECT (toolbar), "icon-size");      
3242         }
3243
3244       priv->icon_size_set = FALSE;
3245       g_object_notify (G_OBJECT (toolbar), "icon-size-set");      
3246     }
3247 }
3248
3249 /*
3250  * ToolbarContent methods
3251  */
3252 typedef enum {
3253   UNKNOWN,
3254   YES,
3255   NO
3256 } TriState;
3257
3258 struct _ToolbarContent
3259 {
3260   ItemState      state;
3261
3262   GtkToolItem   *item;
3263   GtkAllocation  allocation;
3264   GtkAllocation  start_allocation;
3265   GtkAllocation  goal_allocation;
3266   guint          is_placeholder : 1;
3267   guint          disappearing : 1;
3268   guint          has_menu : 2;
3269 };
3270
3271 static void
3272 toolbar_item_visiblity_notify_cb (GObject *obj,
3273                                   GParamSpec *pspec,
3274                                   gpointer user_data)
3275 {
3276   GtkToolbar *toolbar = user_data;
3277
3278   gtk_toolbar_invalidate_order (toolbar);
3279 }
3280
3281 static ToolbarContent *
3282 toolbar_content_new_tool_item (GtkToolbar  *toolbar,
3283                                GtkToolItem *item,
3284                                gboolean     is_placeholder,
3285                                gint         pos)
3286 {
3287   GtkToolbarPrivate *priv = toolbar->priv;
3288   ToolbarContent *content;
3289
3290   content = g_slice_new0 (ToolbarContent);
3291   
3292   content->state = NOT_ALLOCATED;
3293   content->item = item;
3294   content->is_placeholder = is_placeholder;
3295
3296   priv->content = g_list_insert (priv->content, content, pos);
3297
3298   gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (toolbar));
3299   gtk_toolbar_invalidate_order (toolbar);
3300
3301   g_signal_connect (content->item, "notify::visible",
3302                     G_CALLBACK (toolbar_item_visiblity_notify_cb), toolbar);
3303
3304   if (!is_placeholder)
3305     {
3306       priv->num_children++;
3307
3308       gtk_toolbar_stop_sliding (toolbar);
3309     }
3310
3311   gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3312   priv->need_rebuild = TRUE;
3313   
3314   return content;
3315 }
3316
3317 static void
3318 toolbar_content_remove (ToolbarContent *content,
3319                         GtkToolbar     *toolbar)
3320 {
3321   GtkToolbarPrivate *priv = toolbar->priv;
3322
3323   gtk_toolbar_invalidate_order (toolbar);
3324   gtk_widget_unparent (GTK_WIDGET (content->item));
3325
3326   g_signal_handlers_disconnect_by_func (content->item,
3327                                         toolbar_item_visiblity_notify_cb,
3328                                         toolbar);
3329
3330   priv->content = g_list_remove (priv->content, content);
3331
3332   if (!toolbar_content_is_placeholder (content))
3333     priv->num_children--;
3334
3335   gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3336   priv->need_rebuild = TRUE;
3337 }
3338
3339 static void
3340 toolbar_content_free (ToolbarContent *content)
3341 {
3342   g_slice_free (ToolbarContent, content);
3343 }
3344
3345 static gint
3346 calculate_max_homogeneous_pixels (GtkWidget *widget)
3347 {
3348   PangoContext *context;
3349   PangoFontMetrics *metrics;
3350   const PangoFontDescription *font_desc;
3351   GtkStyleContext *style_context;
3352   GtkStateFlags state;
3353   gint char_width;
3354   
3355   context = gtk_widget_get_pango_context (widget);
3356   style_context = gtk_widget_get_style_context (widget);
3357   state = gtk_widget_get_state_flags (widget);
3358
3359   font_desc = gtk_style_context_get_font (style_context, state);
3360
3361   metrics = pango_context_get_metrics (context, font_desc,
3362                                        pango_context_get_language (context));
3363   char_width = pango_font_metrics_get_approximate_char_width (metrics);
3364   pango_font_metrics_unref (metrics);
3365   
3366   return PANGO_PIXELS (MAX_HOMOGENEOUS_N_CHARS * char_width);
3367 }
3368
3369 static void
3370 toolbar_content_draw (ToolbarContent *content,
3371                       GtkContainer   *container,
3372                       cairo_t        *cr)
3373 {
3374   GtkWidget *widget;
3375
3376   if (content->is_placeholder)
3377     return;
3378   
3379   widget = GTK_WIDGET (content->item);
3380
3381   if (widget)
3382     gtk_container_propagate_draw (container, widget, cr);
3383 }
3384
3385 static gboolean
3386 toolbar_content_visible (ToolbarContent *content,
3387                          GtkToolbar     *toolbar)
3388 {
3389   GtkToolbarPrivate *priv = toolbar->priv;
3390   GtkToolItem *item;
3391
3392   item = content->item;
3393
3394   if (!gtk_widget_get_visible (GTK_WIDGET (item)))
3395     return FALSE;
3396
3397   if (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
3398       gtk_tool_item_get_visible_horizontal (item))
3399     return TRUE;
3400
3401   if (priv->orientation == GTK_ORIENTATION_VERTICAL &&
3402       gtk_tool_item_get_visible_vertical (item))
3403     return TRUE;
3404       
3405   return FALSE;
3406 }
3407
3408 static void
3409 toolbar_content_size_request (ToolbarContent *content,
3410                               GtkToolbar     *toolbar,
3411                               GtkRequisition *requisition)
3412 {
3413   gtk_widget_get_preferred_size (GTK_WIDGET (content->item),
3414                                  requisition, NULL);
3415   if (content->is_placeholder &&
3416       content->disappearing)
3417     {
3418       requisition->width = 0;
3419       requisition->height = 0;
3420     }
3421 }
3422
3423 static gboolean
3424 toolbar_content_is_homogeneous (ToolbarContent *content,
3425                                 GtkToolbar     *toolbar)
3426 {
3427   GtkToolbarPrivate *priv = toolbar->priv;
3428   GtkRequisition requisition;
3429   gboolean result;
3430   
3431   if (priv->max_homogeneous_pixels < 0)
3432     {
3433       priv->max_homogeneous_pixels =
3434         calculate_max_homogeneous_pixels (GTK_WIDGET (toolbar));
3435     }
3436   
3437   toolbar_content_size_request (content, toolbar, &requisition);
3438   
3439   if (requisition.width > priv->max_homogeneous_pixels)
3440     return FALSE;
3441
3442   result = gtk_tool_item_get_homogeneous (content->item) &&
3443            !GTK_IS_SEPARATOR_TOOL_ITEM (content->item);
3444
3445   if (gtk_tool_item_get_is_important (content->item) &&
3446       priv->style == GTK_TOOLBAR_BOTH_HORIZ &&
3447       priv->orientation == GTK_ORIENTATION_HORIZONTAL)
3448     {
3449       result = FALSE;
3450     }
3451
3452   return result;
3453 }
3454
3455 static gboolean
3456 toolbar_content_is_placeholder (ToolbarContent *content)
3457 {
3458   if (content->is_placeholder)
3459     return TRUE;
3460   
3461   return FALSE;
3462 }
3463
3464 static gboolean
3465 toolbar_content_disappearing (ToolbarContent *content)
3466 {
3467   if (content->disappearing)
3468     return TRUE;
3469   
3470   return FALSE;
3471 }
3472
3473 static ItemState
3474 toolbar_content_get_state (ToolbarContent *content)
3475 {
3476   return content->state;
3477 }
3478
3479 static gboolean
3480 toolbar_content_child_visible (ToolbarContent *content)
3481 {
3482   return gtk_widget_get_child_visible (GTK_WIDGET (content->item));
3483 }
3484
3485 static void
3486 toolbar_content_get_goal_allocation (ToolbarContent *content,
3487                                      GtkAllocation  *allocation)
3488 {
3489   *allocation = content->goal_allocation;
3490 }
3491
3492 static void
3493 toolbar_content_get_allocation (ToolbarContent *content,
3494                                 GtkAllocation  *allocation)
3495 {
3496   *allocation = content->allocation;
3497 }
3498
3499 static void
3500 toolbar_content_set_start_allocation (ToolbarContent *content,
3501                                       GtkAllocation  *allocation)
3502 {
3503   content->start_allocation = *allocation;
3504 }
3505
3506 static gboolean
3507 toolbar_content_get_expand (ToolbarContent *content)
3508 {
3509   if (!content->disappearing &&
3510       gtk_tool_item_get_expand (content->item))
3511     return TRUE;
3512
3513   return FALSE;
3514 }
3515
3516 static void
3517 toolbar_content_set_goal_allocation (ToolbarContent *content,
3518                                      GtkAllocation  *allocation)
3519 {
3520   content->goal_allocation = *allocation;
3521 }
3522
3523 static void
3524 toolbar_content_set_child_visible (ToolbarContent *content,
3525                                    GtkToolbar     *toolbar,
3526                                    gboolean        visible)
3527 {
3528   gtk_widget_set_child_visible (GTK_WIDGET (content->item),
3529                                 visible);
3530 }
3531
3532 static void
3533 toolbar_content_get_start_allocation (ToolbarContent *content,
3534                                       GtkAllocation  *start_allocation)
3535 {
3536   *start_allocation = content->start_allocation;
3537 }
3538
3539 static void
3540 toolbar_content_size_allocate (ToolbarContent *content,
3541                                GtkAllocation  *allocation)
3542 {
3543   content->allocation = *allocation;
3544   gtk_widget_size_allocate (GTK_WIDGET (content->item),
3545                             allocation);
3546 }
3547
3548 static void
3549 toolbar_content_set_state (ToolbarContent *content,
3550                            ItemState       state)
3551 {
3552   content->state = state;
3553 }
3554
3555 static GtkWidget *
3556 toolbar_content_get_widget (ToolbarContent *content)
3557 {
3558   return GTK_WIDGET (content->item);
3559 }
3560
3561
3562 static void
3563 toolbar_content_set_disappearing (ToolbarContent *content,
3564                                   gboolean        disappearing)
3565 {
3566   content->disappearing = disappearing;
3567 }
3568
3569 static void
3570 toolbar_content_set_size_request (ToolbarContent *content,
3571                                   gint            width,
3572                                   gint            height)
3573 {
3574   gtk_widget_set_size_request (GTK_WIDGET (content->item),
3575                                width, height);
3576 }
3577
3578 static void
3579 toolbar_content_toolbar_reconfigured (ToolbarContent *content,
3580                                       GtkToolbar     *toolbar)
3581 {
3582   gtk_tool_item_toolbar_reconfigured (content->item);
3583 }
3584
3585 static GtkWidget *
3586 toolbar_content_retrieve_menu_item (ToolbarContent *content)
3587 {
3588   return gtk_tool_item_retrieve_proxy_menu_item (content->item);
3589 }
3590
3591 static gboolean
3592 toolbar_content_has_proxy_menu_item (ToolbarContent *content)
3593 {
3594   GtkWidget *menu_item;
3595
3596   if (content->has_menu == YES)
3597     return TRUE;
3598   else if (content->has_menu == NO)
3599     return FALSE;
3600
3601   menu_item = toolbar_content_retrieve_menu_item (content);
3602
3603   content->has_menu = menu_item? YES : NO;
3604
3605   return menu_item != NULL;
3606 }
3607
3608 static void
3609 toolbar_content_set_unknown_menu_status (ToolbarContent *content)
3610 {
3611   content->has_menu = UNKNOWN;
3612 }
3613
3614 static gboolean
3615 toolbar_content_is_separator (ToolbarContent *content)
3616 {
3617   return GTK_IS_SEPARATOR_TOOL_ITEM (content->item);
3618 }
3619
3620 static void
3621 toolbar_content_set_expand (ToolbarContent *content,
3622                             gboolean        expand)
3623 {
3624   gtk_tool_item_set_expand (content->item, expand);
3625 }
3626
3627 static void
3628 toolbar_content_show_all (ToolbarContent  *content)
3629 {
3630   GtkWidget *widget;
3631   
3632   widget = toolbar_content_get_widget (content);
3633   if (widget)
3634     gtk_widget_show_all (widget);
3635 }
3636
3637 /*
3638  * Getters
3639  */
3640 static GtkReliefStyle
3641 get_button_relief (GtkToolbar *toolbar)
3642 {
3643   GtkReliefStyle button_relief = GTK_RELIEF_NORMAL;
3644
3645   gtk_widget_style_get (GTK_WIDGET (toolbar),
3646                         "button-relief", &button_relief,
3647                         NULL);
3648   
3649   return button_relief;
3650 }
3651
3652 static gint
3653 get_internal_padding (GtkToolbar *toolbar)
3654 {
3655   gint ipadding = 0;
3656   
3657   gtk_widget_style_get (GTK_WIDGET (toolbar),
3658                         "internal-padding", &ipadding,
3659                         NULL);
3660   
3661   return ipadding;
3662 }
3663
3664 static gint
3665 get_max_child_expand (GtkToolbar *toolbar)
3666 {
3667   gint mexpand = G_MAXINT;
3668
3669   gtk_widget_style_get (GTK_WIDGET (toolbar),
3670                         "max-child-expand", &mexpand,
3671                         NULL);
3672   return mexpand;
3673 }
3674
3675 static GtkShadowType
3676 get_shadow_type (GtkToolbar *toolbar)
3677 {
3678   GtkShadowType shadow_type;
3679   
3680   gtk_widget_style_get (GTK_WIDGET (toolbar),
3681                         "shadow-type", &shadow_type,
3682                         NULL);
3683   
3684   return shadow_type;
3685 }
3686
3687 /* GTK+ internal methods */
3688
3689 gint
3690 _gtk_toolbar_get_default_space_size (void)
3691 {
3692   return DEFAULT_SPACE_SIZE;
3693 }
3694
3695 void
3696 _gtk_toolbar_paint_space_line (GtkWidget           *widget,
3697                                GtkToolbar          *toolbar,
3698                                cairo_t             *cr)
3699 {
3700   GtkOrientation orientation;
3701   GtkStyleContext *context;
3702   GtkStateFlags state;
3703   GtkBorder padding;
3704   gint width, height;
3705   const gdouble start_fraction = (SPACE_LINE_START / SPACE_LINE_DIVISION);
3706   const gdouble end_fraction = (SPACE_LINE_END / SPACE_LINE_DIVISION);
3707
3708   g_return_if_fail (GTK_IS_WIDGET (widget));
3709
3710   orientation = toolbar ? toolbar->priv->orientation : GTK_ORIENTATION_HORIZONTAL;
3711
3712   context = gtk_widget_get_style_context (widget);
3713   state = gtk_widget_get_state_flags (widget);
3714   width = gtk_widget_get_allocated_width (widget);
3715   height = gtk_widget_get_allocated_height (widget);
3716   gtk_style_context_get_padding (context, state, &padding);
3717
3718   if (orientation == GTK_ORIENTATION_HORIZONTAL)
3719     {
3720       gboolean wide_separators;
3721       gint     separator_width;
3722
3723       gtk_widget_style_get (widget,
3724                             "wide-separators", &wide_separators,
3725                             "separator-width", &separator_width,
3726                             NULL);
3727
3728       if (wide_separators)
3729         gtk_render_frame (context, cr,
3730                           (width - separator_width) / 2,
3731                           height * start_fraction,
3732                           separator_width,
3733                           height * (end_fraction - start_fraction));
3734       else
3735         gtk_render_line (context, cr,
3736                          (width - padding.left) / 2,
3737                          height * start_fraction,
3738                          (width - padding.left) / 2,
3739                          height * end_fraction);
3740     }
3741   else
3742     {
3743       gboolean wide_separators;
3744       gint     separator_height;
3745
3746       gtk_widget_style_get (widget,
3747                             "wide-separators",  &wide_separators,
3748                             "separator-height", &separator_height,
3749                             NULL);
3750
3751       if (wide_separators)
3752         gtk_render_frame (context, cr,
3753                           width * start_fraction,
3754                           (height - separator_height) / 2,
3755                           width * (end_fraction - start_fraction),
3756                           separator_height);
3757       else
3758         gtk_render_line (context, cr,
3759                          width * start_fraction,
3760                          (height - padding.top) / 2,
3761                          width * end_fraction,
3762                          (height - padding.top) / 2);
3763     }
3764 }
3765
3766 gchar *
3767 _gtk_toolbar_elide_underscores (const gchar *original)
3768 {
3769   gchar *q, *result;
3770   const gchar *p, *end;
3771   gsize len;
3772   gboolean last_underscore;
3773   
3774   if (!original)
3775     return NULL;
3776
3777   len = strlen (original);
3778   q = result = g_malloc (len + 1);
3779   last_underscore = FALSE;
3780   
3781   end = original + len;
3782   for (p = original; p < end; p++)
3783     {
3784       if (!last_underscore && *p == '_')
3785         last_underscore = TRUE;
3786       else
3787         {
3788           last_underscore = FALSE;
3789           if (original + 2 <= p && p + 1 <= end && 
3790               p[-2] == '(' && p[-1] == '_' && p[0] != '_' && p[1] == ')')
3791             {
3792               q--;
3793               *q = '\0';
3794               p++;
3795             }
3796           else
3797             *q++ = *p;
3798         }
3799     }
3800
3801   if (last_underscore)
3802     *q++ = '_';
3803   
3804   *q = '\0';
3805   
3806   return result;
3807 }
3808
3809 static GtkIconSize
3810 toolbar_get_icon_size (GtkToolShell *shell)
3811 {
3812   GtkToolbar *toolbar = GTK_TOOLBAR (shell);
3813   GtkToolbarPrivate *priv = toolbar->priv;
3814
3815   return priv->icon_size;
3816 }
3817
3818 static GtkOrientation
3819 toolbar_get_orientation (GtkToolShell *shell)
3820 {
3821   GtkToolbar *toolbar = GTK_TOOLBAR (shell);
3822   GtkToolbarPrivate *priv = toolbar->priv;
3823
3824   return priv->orientation;
3825 }
3826
3827 static GtkToolbarStyle
3828 toolbar_get_style (GtkToolShell *shell)
3829 {
3830   GtkToolbar *toolbar = GTK_TOOLBAR (shell);
3831   GtkToolbarPrivate *priv = toolbar->priv;
3832
3833   return priv->style;
3834 }
3835
3836 static GtkReliefStyle
3837 toolbar_get_relief_style (GtkToolShell *shell)
3838 {
3839   return get_button_relief (GTK_TOOLBAR (shell));
3840 }
3841
3842 static void
3843 toolbar_rebuild_menu (GtkToolShell *shell)
3844 {
3845   GtkToolbar *toolbar = GTK_TOOLBAR (shell);
3846   GtkToolbarPrivate *priv = toolbar->priv;
3847   GList *list;
3848
3849   priv->need_rebuild = TRUE;
3850
3851   for (list = priv->content; list != NULL; list = list->next)
3852     {
3853       ToolbarContent *content = list->data;
3854
3855       toolbar_content_set_unknown_menu_status (content);
3856     }
3857   
3858   gtk_widget_queue_resize (GTK_WIDGET (shell));
3859 }
3860
3861 typedef struct _CountingData CountingData;
3862 struct _CountingData {
3863   GtkWidget *widget;
3864   gboolean found;
3865   guint before;
3866   guint after;
3867 };
3868
3869 static void
3870 count_widget_position (GtkWidget *widget,
3871                        gpointer   data)
3872 {
3873   CountingData *count = data;
3874
3875   if (!gtk_widget_get_visible (widget))
3876     return;
3877
3878   if (count->widget == widget)
3879     count->found = TRUE;
3880   else if (count->found)
3881     count->after++;
3882   else
3883     count->before++;
3884 }
3885
3886 static guint
3887 gtk_toolbar_get_visible_position (GtkToolbar *toolbar,
3888                                   GtkWidget  *child)
3889 {
3890   CountingData count = { child, FALSE, 0, 0 };
3891
3892   if (child == (GtkWidget*)toolbar->priv->highlight_tool_item)
3893     return 0;
3894
3895   /* foreach iterates in visible order */
3896   gtk_container_forall (GTK_CONTAINER (toolbar),
3897                         count_widget_position,
3898                         &count);
3899
3900   g_assert (count.found);
3901
3902   if (toolbar->priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
3903       gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
3904     return count.after;
3905
3906   return count.before;
3907 }
3908
3909 static void
3910 add_widget_to_path (gpointer data,
3911                     gpointer user_data)
3912 {
3913   GtkWidget *widget = data;
3914   GtkWidgetPath *path = user_data;
3915
3916   if (gtk_widget_get_visible (widget))
3917     gtk_widget_path_append_for_widget (path, widget);
3918 }
3919
3920 static GtkWidgetPath *
3921 gtk_toolbar_get_path_for_child (GtkContainer *container,
3922                                 GtkWidget    *child)
3923 {
3924   GtkWidgetPath *path;
3925   GtkToolbar *toolbar;
3926   GtkToolbarPrivate *priv;
3927   GtkWidgetPath *sibling_path;
3928   gint vis_index;
3929   GList *children;
3930
3931   toolbar = GTK_TOOLBAR (container);
3932   priv = toolbar->priv;
3933
3934   /* build a path for all the visible children;
3935    * get_children works in visible order
3936    */
3937   sibling_path = gtk_widget_path_new ();
3938   children = _gtk_container_get_all_children (container);
3939
3940   if (priv->orientation != GTK_ORIENTATION_HORIZONTAL ||
3941       gtk_widget_get_direction (GTK_WIDGET (toolbar)) != GTK_TEXT_DIR_RTL)
3942     children = g_list_reverse (children);
3943
3944   g_list_foreach (children, add_widget_to_path, sibling_path);
3945   g_list_free (children);
3946
3947   path = _gtk_widget_create_path (GTK_WIDGET (container));
3948   if (gtk_widget_get_visible (child))
3949     {
3950       vis_index = gtk_toolbar_get_visible_position (toolbar, child);
3951
3952       if (vis_index < gtk_widget_path_length (sibling_path))
3953         gtk_widget_path_append_with_siblings (path,
3954                                               sibling_path,
3955                                               vis_index);
3956       else
3957         gtk_widget_path_append_for_widget (path, child);
3958     }
3959   else
3960     gtk_widget_path_append_for_widget (path, child);
3961
3962   gtk_widget_path_unref (sibling_path);
3963   return path;
3964 }
3965
3966 static void
3967 gtk_toolbar_invalidate_order_foreach (GtkWidget *widget)
3968 {
3969   _gtk_widget_invalidate_style_context (widget, GTK_CSS_CHANGE_POSITION | GTK_CSS_CHANGE_SIBLING_POSITION);
3970 }
3971
3972 static void
3973 gtk_toolbar_invalidate_order (GtkToolbar *toolbar)
3974 {
3975   gtk_container_forall (GTK_CONTAINER (toolbar),
3976                         (GtkCallback) gtk_toolbar_invalidate_order_foreach,
3977                         NULL);
3978 }
3979
3980 static void
3981 gtk_toolbar_direction_changed (GtkWidget        *widget,
3982                                GtkTextDirection  previous_direction)
3983 {
3984   GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->direction_changed (widget, previous_direction);
3985
3986   gtk_toolbar_invalidate_order (GTK_TOOLBAR (widget));
3987 }
3988