]> Pileus Git - ~andy/gtk/blob - gtk/gtktoolbar.c
8b1caddb770a4aa0b80f5280a4b35389431be8fa
[~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, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 /*
26  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
27  * file for a list of people on the GTK+ Team.  See the ChangeLog
28  * files for a list of changes.  These files are distributed with
29  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
30  */
31
32 #undef GTK_DISABLE_DEPRECATED
33
34 #include <config.h>
35 #include "gtkarrow.h"
36 #include "gtktoolbar.h"
37 #include "gtktoolshell.h"
38 #include "gtkradiotoolbutton.h"
39 #include "gtkseparatortoolitem.h"
40 #include "gtkmenu.h"
41 #include "gtkradiobutton.h"
42 #include "gtktoolbar.h"
43 #include "gtkbindings.h"
44 #include <gdk/gdkkeysyms.h>
45 #include "gtkmarshalers.h"
46 #include "gtkmain.h"
47 #include "gtkstock.h"
48 #include "gtklabel.h"
49 #include "gtkprivate.h"
50 #include "gtkintl.h"
51 #include <string.h>
52 #include "gtkhbox.h"
53 #include "gtkvbox.h"
54 #include "gtkimage.h"
55 #include "gtkseparatormenuitem.h"
56 #include "gtkalias.h"
57 #include <math.h>
58
59 typedef struct _ToolbarContent ToolbarContent;
60
61 #define DEFAULT_IPADDING    0
62
63 #define DEFAULT_SPACE_SIZE  12
64 #define DEFAULT_SPACE_STYLE GTK_TOOLBAR_SPACE_LINE
65 #define SPACE_LINE_DIVISION 10.0
66 #define SPACE_LINE_START    2.0
67 #define SPACE_LINE_END      8.0
68
69 #define DEFAULT_ICON_SIZE GTK_ICON_SIZE_LARGE_TOOLBAR
70 #define DEFAULT_TOOLBAR_STYLE GTK_TOOLBAR_BOTH
71 #define DEFAULT_ANIMATION_STATE TRUE
72
73 #define MAX_HOMOGENEOUS_N_CHARS 13 /* Items that are wider than this do not participate
74                                     * in the homogeneous game. In units of
75                                     * pango_font_get_estimated_char_width().
76                                     */
77 #define SLIDE_SPEED 600.0          /* How fast the items slide, in pixels per second */
78 #define ACCEL_THRESHOLD 0.18       /* After how much time in seconds will items start speeding up */
79
80 #define MIXED_API_WARNING                                               \
81     "Mixing deprecated and non-deprecated GtkToolbar API is not allowed"
82
83
84 /* Properties */
85 enum {
86   PROP_0,
87   PROP_ORIENTATION,
88   PROP_TOOLBAR_STYLE,
89   PROP_SHOW_ARROW,
90   PROP_TOOLTIPS,
91   PROP_ICON_SIZE,
92   PROP_ICON_SIZE_SET
93 };
94
95 /* Child properties */
96 enum {
97   CHILD_PROP_0,
98   CHILD_PROP_EXPAND,
99   CHILD_PROP_HOMOGENEOUS
100 };
101
102 /* Signals */
103 enum {
104   ORIENTATION_CHANGED,
105   STYLE_CHANGED,
106   POPUP_CONTEXT_MENU,
107   FOCUS_HOME_OR_END,
108   LAST_SIGNAL
109 };
110
111 /* API mode */
112 typedef enum {
113   DONT_KNOW,
114   OLD_API,
115   NEW_API
116 } ApiMode;
117
118 typedef enum {
119   TOOL_ITEM,
120   COMPATIBILITY
121 } ContentType;
122
123 typedef enum {
124   NOT_ALLOCATED,
125   NORMAL,
126   HIDDEN,
127   OVERFLOWN
128 } ItemState;
129
130 struct _GtkToolbarPrivate
131 {
132   GList *       content;
133   
134   GtkWidget *   arrow;
135   GtkWidget *   arrow_button;
136   GtkMenu *     menu;
137   
138   GdkWindow *   event_window;
139   ApiMode       api_mode;
140   GtkSettings * settings;
141   int           idle_id;
142   GtkToolItem * highlight_tool_item;
143   gint          max_homogeneous_pixels;
144   
145   GTimer *      timer;
146
147   gulong        settings_connection;
148
149   guint         show_arrow : 1;
150   guint         need_sync : 1;
151   guint         is_sliding : 1;
152   guint         need_rebuild : 1;  /* whether the overflow menu should be regenerated */
153   guint         animation : 1;
154 };
155
156 static void       gtk_toolbar_set_property         (GObject             *object,
157                                                     guint                prop_id,
158                                                     const GValue        *value,
159                                                     GParamSpec          *pspec);
160 static void       gtk_toolbar_get_property         (GObject             *object,
161                                                     guint                prop_id,
162                                                     GValue              *value,
163                                                     GParamSpec          *pspec);
164 static gint       gtk_toolbar_expose               (GtkWidget           *widget,
165                                                     GdkEventExpose      *event);
166 static void       gtk_toolbar_realize              (GtkWidget           *widget);
167 static void       gtk_toolbar_unrealize            (GtkWidget           *widget);
168 static void       gtk_toolbar_size_request         (GtkWidget           *widget,
169                                                     GtkRequisition      *requisition);
170 static void       gtk_toolbar_size_allocate        (GtkWidget           *widget,
171                                                     GtkAllocation       *allocation);
172 static void       gtk_toolbar_style_set            (GtkWidget           *widget,
173                                                     GtkStyle            *prev_style);
174 static gboolean   gtk_toolbar_focus                (GtkWidget           *widget,
175                                                     GtkDirectionType     dir);
176 static void       gtk_toolbar_move_focus           (GtkWidget           *widget,
177                                                     GtkDirectionType     dir);
178 static void       gtk_toolbar_screen_changed       (GtkWidget           *widget,
179                                                     GdkScreen           *previous_screen);
180 static void       gtk_toolbar_map                  (GtkWidget           *widget);
181 static void       gtk_toolbar_unmap                (GtkWidget           *widget);
182 static void       gtk_toolbar_set_child_property   (GtkContainer        *container,
183                                                     GtkWidget           *child,
184                                                     guint                property_id,
185                                                     const GValue        *value,
186                                                     GParamSpec          *pspec);
187 static void       gtk_toolbar_get_child_property   (GtkContainer        *container,
188                                                     GtkWidget           *child,
189                                                     guint                property_id,
190                                                     GValue              *value,
191                                                     GParamSpec          *pspec);
192 static void       gtk_toolbar_finalize             (GObject             *object);
193 static void       gtk_toolbar_show_all             (GtkWidget           *widget);
194 static void       gtk_toolbar_hide_all             (GtkWidget           *widget);
195 static void       gtk_toolbar_add                  (GtkContainer        *container,
196                                                     GtkWidget           *widget);
197 static void       gtk_toolbar_remove               (GtkContainer        *container,
198                                                     GtkWidget           *widget);
199 static void       gtk_toolbar_forall               (GtkContainer        *container,
200                                                     gboolean             include_internals,
201                                                     GtkCallback          callback,
202                                                     gpointer             callback_data);
203 static GType      gtk_toolbar_child_type           (GtkContainer        *container);
204 static void       gtk_toolbar_orientation_changed  (GtkToolbar          *toolbar,
205                                                     GtkOrientation       orientation);
206 static void       gtk_toolbar_real_style_changed   (GtkToolbar          *toolbar,
207                                                     GtkToolbarStyle      style);
208 static gboolean   gtk_toolbar_focus_home_or_end    (GtkToolbar          *toolbar,
209                                                     gboolean             focus_home);
210 static gboolean   gtk_toolbar_button_press         (GtkWidget           *toolbar,
211                                                     GdkEventButton      *event);
212 static gboolean   gtk_toolbar_arrow_button_press   (GtkWidget           *button,
213                                                     GdkEventButton      *event,
214                                                     GtkToolbar          *toolbar);
215 static void       gtk_toolbar_arrow_button_clicked (GtkWidget           *button,
216                                                     GtkToolbar          *toolbar);
217 static void       gtk_toolbar_update_button_relief (GtkToolbar          *toolbar);
218 static gboolean   gtk_toolbar_popup_menu           (GtkWidget           *toolbar);
219 static GtkWidget *internal_insert_element          (GtkToolbar          *toolbar,
220                                                     GtkToolbarChildType  type,
221                                                     GtkWidget           *widget,
222                                                     const char          *text,
223                                                     const char          *tooltip_text,
224                                                     const char          *tooltip_private_text,
225                                                     GtkWidget           *icon,
226                                                     GtkSignalFunc        callback,
227                                                     gpointer             user_data,
228                                                     gint                 position,
229                                                     gboolean             use_stock);
230 static void       gtk_toolbar_reconfigured         (GtkToolbar          *toolbar);
231 static gboolean   gtk_toolbar_check_new_api        (GtkToolbar          *toolbar);
232 static gboolean   gtk_toolbar_check_old_api        (GtkToolbar          *toolbar);
233
234 static GtkReliefStyle       get_button_relief    (GtkToolbar *toolbar);
235 static gint                 get_internal_padding (GtkToolbar *toolbar);
236 static gint                 get_max_child_expand (GtkToolbar *toolbar);
237 static GtkShadowType        get_shadow_type      (GtkToolbar *toolbar);
238 static gint                 get_space_size       (GtkToolbar *toolbar);
239 static GtkToolbarSpaceStyle get_space_style      (GtkToolbar *toolbar);
240
241 /* methods on ToolbarContent 'class' */
242 static ToolbarContent *toolbar_content_new_tool_item        (GtkToolbar          *toolbar,
243                                                              GtkToolItem         *item,
244                                                              gboolean             is_placeholder,
245                                                              gint                 pos);
246 static ToolbarContent *toolbar_content_new_compatibility    (GtkToolbar          *toolbar,
247                                                              GtkToolbarChildType  type,
248                                                              GtkWidget           *widget,
249                                                              GtkWidget           *icon,
250                                                              GtkWidget           *label,
251                                                              gint                 pos);
252 static void            toolbar_content_remove               (ToolbarContent      *content,
253                                                              GtkToolbar          *toolbar);
254 static void            toolbar_content_free                 (ToolbarContent      *content);
255 static void            toolbar_content_expose               (ToolbarContent      *content,
256                                                              GtkContainer        *container,
257                                                              GdkEventExpose      *expose);
258 static gboolean        toolbar_content_visible              (ToolbarContent      *content,
259                                                              GtkToolbar          *toolbar);
260 static void            toolbar_content_size_request         (ToolbarContent      *content,
261                                                              GtkToolbar          *toolbar,
262                                                              GtkRequisition      *requisition);
263 static gboolean        toolbar_content_is_homogeneous       (ToolbarContent      *content,
264                                                              GtkToolbar          *toolbar);
265 static gboolean        toolbar_content_is_placeholder       (ToolbarContent      *content);
266 static gboolean        toolbar_content_disappearing         (ToolbarContent      *content);
267 static ItemState       toolbar_content_get_state            (ToolbarContent      *content);
268 static gboolean        toolbar_content_child_visible        (ToolbarContent      *content);
269 static void            toolbar_content_get_goal_allocation  (ToolbarContent      *content,
270                                                              GtkAllocation       *allocation);
271 static void            toolbar_content_get_allocation       (ToolbarContent      *content,
272                                                              GtkAllocation       *allocation);
273 static void            toolbar_content_set_start_allocation (ToolbarContent      *content,
274                                                              GtkAllocation       *new_start_allocation);
275 static void            toolbar_content_get_start_allocation (ToolbarContent      *content,
276                                                              GtkAllocation       *start_allocation);
277 static gboolean        toolbar_content_get_expand           (ToolbarContent      *content);
278 static void            toolbar_content_set_goal_allocation  (ToolbarContent      *content,
279                                                              GtkAllocation       *allocation);
280 static void            toolbar_content_set_child_visible    (ToolbarContent      *content,
281                                                              GtkToolbar          *toolbar,
282                                                              gboolean             visible);
283 static void            toolbar_content_size_allocate        (ToolbarContent      *content,
284                                                              GtkAllocation       *allocation);
285 static void            toolbar_content_set_state            (ToolbarContent      *content,
286                                                              ItemState            new_state);
287 static GtkWidget *     toolbar_content_get_widget           (ToolbarContent      *content);
288 static void            toolbar_content_set_disappearing     (ToolbarContent      *content,
289                                                              gboolean             disappearing);
290 static void            toolbar_content_set_size_request     (ToolbarContent      *content,
291                                                              gint                 width,
292                                                              gint                 height);
293 static void            toolbar_content_toolbar_reconfigured (ToolbarContent      *content,
294                                                              GtkToolbar          *toolbar);
295 static GtkWidget *     toolbar_content_retrieve_menu_item   (ToolbarContent      *content);
296 static gboolean        toolbar_content_has_proxy_menu_item  (ToolbarContent      *content);
297 static gboolean        toolbar_content_is_separator         (ToolbarContent      *content);
298 static void            toolbar_content_show_all             (ToolbarContent      *content);
299 static void            toolbar_content_hide_all             (ToolbarContent      *content);
300 static void            toolbar_content_set_expand           (ToolbarContent      *content,
301                                                              gboolean             expand);
302
303 static void            toolbar_tool_shell_iface_init        (GtkToolShellIface   *iface);
304 static GtkIconSize     toolbar_get_icon_size                (GtkToolShell        *shell);
305 static GtkOrientation  toolbar_get_orientation              (GtkToolShell        *shell);
306 static GtkToolbarStyle toolbar_get_style                    (GtkToolShell        *shell);
307 static GtkReliefStyle  toolbar_get_relief_style             (GtkToolShell        *shell);
308 static void            toolbar_rebuild_menu                 (GtkToolShell        *shell);
309
310 #define GTK_TOOLBAR_GET_PRIVATE(o)  \
311   (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_TOOLBAR, GtkToolbarPrivate))
312
313 static guint                    toolbar_signals [LAST_SIGNAL] = { 0 };
314
315 G_DEFINE_TYPE_WITH_CODE (GtkToolbar, gtk_toolbar, GTK_TYPE_CONTAINER,
316                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TOOL_SHELL, toolbar_tool_shell_iface_init))
317
318 static void
319 add_arrow_bindings (GtkBindingSet   *binding_set,
320                     guint            keysym,
321                     GtkDirectionType dir)
322 {
323   guint keypad_keysym = keysym - GDK_Left + GDK_KP_Left;
324   
325   gtk_binding_entry_add_signal (binding_set, keysym, 0,
326                                 "move_focus", 1,
327                                 GTK_TYPE_DIRECTION_TYPE, dir);
328   gtk_binding_entry_add_signal (binding_set, keypad_keysym, 0,
329                                 "move_focus", 1,
330                                 GTK_TYPE_DIRECTION_TYPE, dir);
331 }
332
333 static void
334 add_ctrl_tab_bindings (GtkBindingSet    *binding_set,
335                        GdkModifierType   modifiers,
336                        GtkDirectionType  direction)
337 {
338   gtk_binding_entry_add_signal (binding_set,
339                                 GDK_Tab, GDK_CONTROL_MASK | modifiers,
340                                 "move_focus", 1,
341                                 GTK_TYPE_DIRECTION_TYPE, direction);
342   gtk_binding_entry_add_signal (binding_set,
343                                 GDK_KP_Tab, GDK_CONTROL_MASK | modifiers,
344                                 "move_focus", 1,
345                                 GTK_TYPE_DIRECTION_TYPE, direction);
346 }
347
348 static void
349 toolbar_tool_shell_iface_init (GtkToolShellIface *iface)
350 {
351   iface->get_icon_size    = toolbar_get_icon_size;
352   iface->get_orientation  = toolbar_get_orientation;
353   iface->get_style        = toolbar_get_style;
354   iface->get_relief_style = toolbar_get_relief_style;
355   iface->rebuild_menu     = toolbar_rebuild_menu;
356 }
357
358 static void
359 gtk_toolbar_class_init (GtkToolbarClass *klass)
360 {
361   GObjectClass *gobject_class;
362   GtkWidgetClass *widget_class;
363   GtkContainerClass *container_class;
364   GtkBindingSet *binding_set;
365   
366   gobject_class = (GObjectClass *)klass;
367   widget_class = (GtkWidgetClass *)klass;
368   container_class = (GtkContainerClass *)klass;
369   
370   gobject_class->set_property = gtk_toolbar_set_property;
371   gobject_class->get_property = gtk_toolbar_get_property;
372   gobject_class->finalize = gtk_toolbar_finalize;
373   
374   widget_class->button_press_event = gtk_toolbar_button_press;
375   widget_class->expose_event = gtk_toolbar_expose;
376   widget_class->size_request = gtk_toolbar_size_request;
377   widget_class->size_allocate = gtk_toolbar_size_allocate;
378   widget_class->style_set = gtk_toolbar_style_set;
379   widget_class->focus = gtk_toolbar_focus;
380
381   /* need to override the base class function via override_class_closure,
382    * because the signal slot is not available in GtkWidgetClass
383    */
384   g_signal_override_class_closure (g_signal_lookup ("move_focus",
385                                                     GTK_TYPE_WIDGET),
386                                    GTK_TYPE_TOOLBAR,
387                                    g_cclosure_new (G_CALLBACK (gtk_toolbar_move_focus),
388                                                    NULL, NULL));
389
390   widget_class->screen_changed = gtk_toolbar_screen_changed;
391   widget_class->realize = gtk_toolbar_realize;
392   widget_class->unrealize = gtk_toolbar_unrealize;
393   widget_class->map = gtk_toolbar_map;
394   widget_class->unmap = gtk_toolbar_unmap;
395   widget_class->popup_menu = gtk_toolbar_popup_menu;
396   widget_class->show_all = gtk_toolbar_show_all;
397   widget_class->hide_all = gtk_toolbar_hide_all;
398   
399   container_class->add    = gtk_toolbar_add;
400   container_class->remove = gtk_toolbar_remove;
401   container_class->forall = gtk_toolbar_forall;
402   container_class->child_type = gtk_toolbar_child_type;
403   container_class->get_child_property = gtk_toolbar_get_child_property;
404   container_class->set_child_property = gtk_toolbar_set_child_property;
405   
406   klass->orientation_changed = gtk_toolbar_orientation_changed;
407   klass->style_changed = gtk_toolbar_real_style_changed;
408   
409   /**
410    * GtkToolbar::orientation-changed:
411    * @toolbar: the object which emitted the signal
412    * @orientation: the new #GtkOrientation of the toolbar
413    *
414    * Emitted when the orientation of the toolbar changes.
415    */
416   toolbar_signals[ORIENTATION_CHANGED] =
417     g_signal_new (I_("orientation-changed"),
418                   G_OBJECT_CLASS_TYPE (klass),
419                   G_SIGNAL_RUN_FIRST,
420                   G_STRUCT_OFFSET (GtkToolbarClass, orientation_changed),
421                   NULL, NULL,
422                   g_cclosure_marshal_VOID__ENUM,
423                   G_TYPE_NONE, 1,
424                   GTK_TYPE_ORIENTATION);
425   /**
426    * GtkToolbar::style-changed:
427    * @toolbar: The #GtkToolbar which emitted the signal
428    * @style: the new #GtkToolbarStyle of the toolbar
429    *
430    * Emitted when the style of the toolbar changes. 
431    */
432   toolbar_signals[STYLE_CHANGED] =
433     g_signal_new (I_("style-changed"),
434                   G_OBJECT_CLASS_TYPE (klass),
435                   G_SIGNAL_RUN_FIRST,
436                   G_STRUCT_OFFSET (GtkToolbarClass, style_changed),
437                   NULL, NULL,
438                   g_cclosure_marshal_VOID__ENUM,
439                   G_TYPE_NONE, 1,
440                   GTK_TYPE_TOOLBAR_STYLE);
441   /**
442    * GtkToolbar::popup-context-menu:
443    * @toolbar: the #GtkToolbar which emitted the signal
444    * @x: the x coordinate of the point where the menu should appear
445    * @y: the y coordinate of the point where the menu should appear
446    * @button: the mouse button the user pressed, or -1
447    *
448    * Emitted when the user right-clicks the toolbar or uses the
449    * keybinding to display a popup menu.
450    *
451    * Application developers should handle this signal if they want
452    * to display a context menu on the toolbar. The context-menu should
453    * appear at the coordinates given by @x and @y. The mouse button
454    * number is given by the @button parameter. If the menu was popped
455    * up using the keybaord, @button is -1.
456    *
457    * Return value: return %TRUE if the signal was handled, %FALSE if not
458    */
459   toolbar_signals[POPUP_CONTEXT_MENU] =
460     g_signal_new (I_("popup_context_menu"),
461                   G_OBJECT_CLASS_TYPE (klass),
462                   G_SIGNAL_RUN_LAST,
463                   G_STRUCT_OFFSET (GtkToolbarClass, popup_context_menu),
464                   _gtk_boolean_handled_accumulator, NULL,
465                   _gtk_marshal_BOOLEAN__INT_INT_INT,
466                   G_TYPE_BOOLEAN, 3,
467                   G_TYPE_INT, G_TYPE_INT,
468                   G_TYPE_INT);
469
470   /**
471    * GtkToolbar::focus-home-or-end:
472    * @toolbar: the #GtkToolbar which emitted the signal
473    * @focus_home: %TRUE if the first item should be focused
474    *
475    * A keybinding signal used internally by GTK+. This signal can't
476    * be used in application code
477    *
478    * Return value: %TRUE if the signal was handled, %FALSE if not
479    */
480   toolbar_signals[FOCUS_HOME_OR_END] =
481     _gtk_binding_signal_new (I_("focus_home_or_end"),
482                              G_OBJECT_CLASS_TYPE (klass),
483                              G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
484                              G_CALLBACK (gtk_toolbar_focus_home_or_end),
485                              NULL, NULL,
486                              _gtk_marshal_BOOLEAN__BOOLEAN,
487                              G_TYPE_BOOLEAN, 1,
488                              G_TYPE_BOOLEAN);
489   
490   /* properties */
491   g_object_class_install_property (gobject_class,
492                                    PROP_ORIENTATION,
493                                    g_param_spec_enum ("orientation",
494                                                       P_("Orientation"),
495                                                       P_("The orientation of the toolbar"),
496                                                       GTK_TYPE_ORIENTATION,
497                                                       GTK_ORIENTATION_HORIZONTAL,
498                                                       GTK_PARAM_READWRITE));
499   
500   g_object_class_install_property (gobject_class,
501                                    PROP_TOOLBAR_STYLE,
502                                    g_param_spec_enum ("toolbar-style",
503                                                       P_("Toolbar Style"),
504                                                       P_("How to draw the toolbar"),
505                                                       GTK_TYPE_TOOLBAR_STYLE,
506                                                       DEFAULT_TOOLBAR_STYLE,
507                                                       GTK_PARAM_READWRITE));
508   g_object_class_install_property (gobject_class,
509                                    PROP_SHOW_ARROW,
510                                    g_param_spec_boolean ("show-arrow",
511                                                          P_("Show Arrow"),
512                                                          P_("If an arrow should be shown if the toolbar doesn't fit"),
513                                                          TRUE,
514                                                          GTK_PARAM_READWRITE));
515   
516
517   /**
518    * GtkToolbar:tooltips:
519    * 
520    * If the tooltips of the toolbar should be active or not.
521    * 
522    * Since: 2.8
523    */
524   g_object_class_install_property (gobject_class,
525                                    PROP_TOOLTIPS,
526                                    g_param_spec_boolean ("tooltips",
527                                                          P_("Tooltips"),
528                                                          P_("If the tooltips of the toolbar should be active or not"),
529                                                          TRUE,
530                                                          GTK_PARAM_READWRITE));
531   
532
533   /**
534    * GtkToolbar:icon-size:
535    *
536    * The size of the icons in a toolbar is normally determined by
537    * the toolbar-icon-size setting. When this property is set, it 
538    * overrides the setting. 
539    * 
540    * This should only be used for special-purpose toolbars, normal
541    * application toolbars should respect the user preferences for the
542    * size of icons.
543    *
544    * Since: 2.10
545    */
546   g_object_class_install_property (gobject_class,
547                                    PROP_ICON_SIZE,
548                                    g_param_spec_enum ("icon-size",
549                                                       P_("Icon size"),
550                                                       P_("Size of icons in this toolbar"),
551                                                       GTK_TYPE_ICON_SIZE,
552                                                       DEFAULT_ICON_SIZE,
553                                                       GTK_PARAM_READWRITE));  
554
555   /**
556    * GtkToolbar:icon-size-set:
557    *
558    * Is %TRUE if the icon-size property has been set.
559    *
560    * Since: 2.10
561    */
562   g_object_class_install_property (gobject_class,
563                                    PROP_ICON_SIZE_SET,
564                                    g_param_spec_boolean ("icon-size-set",
565                                                          P_("Icon size set"),
566                                                          P_("Whether the icon-size property has been set"),
567                                                          FALSE,
568                                                          GTK_PARAM_READWRITE));  
569
570   /* child properties */
571   gtk_container_class_install_child_property (container_class,
572                                               CHILD_PROP_EXPAND,
573                                               g_param_spec_boolean ("expand", 
574                                                                     P_("Expand"), 
575                                                                     P_("Whether the item should receive extra space when the toolbar grows"),
576                                                                     TRUE,
577                                                                     GTK_PARAM_READWRITE));
578   
579   gtk_container_class_install_child_property (container_class,
580                                               CHILD_PROP_HOMOGENEOUS,
581                                               g_param_spec_boolean ("homogeneous", 
582                                                                     P_("Homogeneous"), 
583                                                                     P_("Whether the item should be the same size as other homogeneous items"),
584                                                                     TRUE,
585                                                                     GTK_PARAM_READWRITE));
586   
587   /* style properties */
588   gtk_widget_class_install_style_property (widget_class,
589                                            g_param_spec_int ("space-size",
590                                                              P_("Spacer size"),
591                                                              P_("Size of spacers"),
592                                                              0,
593                                                              G_MAXINT,
594                                                              DEFAULT_SPACE_SIZE,
595                                                              GTK_PARAM_READABLE));
596   
597   gtk_widget_class_install_style_property (widget_class,
598                                            g_param_spec_int ("internal-padding",
599                                                              P_("Internal padding"),
600                                                              P_("Amount of border space between the toolbar shadow and the buttons"),
601                                                              0,
602                                                              G_MAXINT,
603                                                              DEFAULT_IPADDING,
604                                                              GTK_PARAM_READABLE));
605
606   gtk_widget_class_install_style_property (widget_class,
607                                            g_param_spec_int ("max-child-expand",
608                                                              P_("Maximum child expand"),
609                                                              P_("Maximum amount of space an expandable item will be given"),
610                                                              0,
611                                                              G_MAXINT,
612                                                              G_MAXINT,
613                                                              GTK_PARAM_READABLE));
614
615   gtk_widget_class_install_style_property (widget_class,
616                                            g_param_spec_enum ("space-style",
617                                                               P_("Space style"),
618                                                               P_("Whether spacers are vertical lines or just blank"),
619                                                               GTK_TYPE_TOOLBAR_SPACE_STYLE,
620                                                               DEFAULT_SPACE_STYLE,
621                                                               GTK_PARAM_READABLE));
622   
623   gtk_widget_class_install_style_property (widget_class,
624                                            g_param_spec_enum ("button-relief",
625                                                               P_("Button relief"),
626                                                               P_("Type of bevel around toolbar buttons"),
627                                                               GTK_TYPE_RELIEF_STYLE,
628                                                               GTK_RELIEF_NONE,
629                                                               GTK_PARAM_READABLE));
630   gtk_widget_class_install_style_property (widget_class,
631                                            g_param_spec_enum ("shadow-type",
632                                                               P_("Shadow type"),
633                                                               P_("Style of bevel around the toolbar"),
634                                                               GTK_TYPE_SHADOW_TYPE,
635                                                               GTK_SHADOW_OUT,
636                                                               GTK_PARAM_READABLE));
637   
638   gtk_settings_install_property (g_param_spec_enum ("gtk-toolbar-style",
639                                                     P_("Toolbar style"),
640                                                     P_("Whether default toolbars have text only, text and icons, icons only, etc."),
641                                                     GTK_TYPE_TOOLBAR_STYLE,
642                                                     DEFAULT_TOOLBAR_STYLE,
643                                                     GTK_PARAM_READWRITE));
644   
645   gtk_settings_install_property (g_param_spec_enum ("gtk-toolbar-icon-size",
646                                                     P_("Toolbar icon size"),
647                                                     P_("Size of icons in default toolbars"),
648                                                     GTK_TYPE_ICON_SIZE,
649                                                     DEFAULT_ICON_SIZE,
650                                                     GTK_PARAM_READWRITE));  
651
652   binding_set = gtk_binding_set_by_class (klass);
653   
654   add_arrow_bindings (binding_set, GDK_Left, GTK_DIR_LEFT);
655   add_arrow_bindings (binding_set, GDK_Right, GTK_DIR_RIGHT);
656   add_arrow_bindings (binding_set, GDK_Up, GTK_DIR_UP);
657   add_arrow_bindings (binding_set, GDK_Down, GTK_DIR_DOWN);
658   
659   gtk_binding_entry_add_signal (binding_set, GDK_KP_Home, 0,
660                                 "focus_home_or_end", 1,
661                                 G_TYPE_BOOLEAN, TRUE);
662   gtk_binding_entry_add_signal (binding_set, GDK_Home, 0,
663                                 "focus_home_or_end", 1,
664                                 G_TYPE_BOOLEAN, TRUE);
665   gtk_binding_entry_add_signal (binding_set, GDK_KP_End, 0,
666                                 "focus_home_or_end", 1,
667                                 G_TYPE_BOOLEAN, FALSE);
668   gtk_binding_entry_add_signal (binding_set, GDK_End, 0,
669                                 "focus_home_or_end", 1,
670                                 G_TYPE_BOOLEAN, FALSE);
671   
672   add_ctrl_tab_bindings (binding_set, 0, GTK_DIR_TAB_FORWARD);
673   add_ctrl_tab_bindings (binding_set, GDK_SHIFT_MASK, GTK_DIR_TAB_BACKWARD);
674   
675   g_type_class_add_private (gobject_class, sizeof (GtkToolbarPrivate));  
676 }
677
678 static void
679 gtk_toolbar_init (GtkToolbar *toolbar)
680 {
681   GtkToolbarPrivate *priv;
682   
683   GTK_WIDGET_UNSET_FLAGS (toolbar, GTK_CAN_FOCUS);
684   GTK_WIDGET_SET_FLAGS (toolbar, GTK_NO_WINDOW);
685   
686   priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
687   
688   toolbar->orientation = GTK_ORIENTATION_HORIZONTAL;
689   toolbar->style = DEFAULT_TOOLBAR_STYLE;
690   toolbar->icon_size = DEFAULT_ICON_SIZE;
691   priv->animation = DEFAULT_ANIMATION_STATE;
692   toolbar->tooltips = gtk_tooltips_new ();
693   g_object_ref_sink (toolbar->tooltips);
694   
695   priv->arrow_button = gtk_toggle_button_new ();
696   g_signal_connect (priv->arrow_button, "button_press_event",
697                     G_CALLBACK (gtk_toolbar_arrow_button_press), toolbar);
698   g_signal_connect (priv->arrow_button, "clicked",
699                     G_CALLBACK (gtk_toolbar_arrow_button_clicked), toolbar);
700   gtk_button_set_relief (GTK_BUTTON (priv->arrow_button),
701                          get_button_relief (toolbar));
702   
703   priv->api_mode = DONT_KNOW;
704   
705   gtk_button_set_focus_on_click (GTK_BUTTON (priv->arrow_button), FALSE);
706   
707   priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
708   gtk_widget_set_name (priv->arrow, "gtk-toolbar-arrow");
709   gtk_widget_show (priv->arrow);
710   gtk_container_add (GTK_CONTAINER (priv->arrow_button), priv->arrow);
711   
712   gtk_widget_set_parent (priv->arrow_button, GTK_WIDGET (toolbar));
713   
714   /* which child position a drop will occur at */
715   priv->menu = NULL;
716   priv->show_arrow = TRUE;
717   priv->settings = NULL;
718   
719   priv->max_homogeneous_pixels = -1;
720   
721   priv->timer = g_timer_new ();
722 }
723
724 static void
725 gtk_toolbar_set_property (GObject      *object,
726                           guint         prop_id,
727                           const GValue *value,
728                           GParamSpec   *pspec)
729 {
730   GtkToolbar *toolbar = GTK_TOOLBAR (object);
731   
732   switch (prop_id)
733     {
734     case PROP_ORIENTATION:
735       gtk_toolbar_set_orientation (toolbar, g_value_get_enum (value));
736       break;
737     case PROP_TOOLBAR_STYLE:
738       gtk_toolbar_set_style (toolbar, g_value_get_enum (value));
739       break;
740     case PROP_SHOW_ARROW:
741       gtk_toolbar_set_show_arrow (toolbar, g_value_get_boolean (value));
742       break;
743     case PROP_TOOLTIPS:
744       gtk_toolbar_set_tooltips (toolbar, g_value_get_boolean (value));
745       break;
746     case PROP_ICON_SIZE:
747       gtk_toolbar_set_icon_size (toolbar, g_value_get_enum (value));
748       break;
749     case PROP_ICON_SIZE_SET:
750       if (g_value_get_boolean (value))
751         toolbar->icon_size_set = TRUE;
752       else
753         gtk_toolbar_unset_icon_size (toolbar);
754       break;
755     default:
756       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
757       break;
758     }
759 }
760
761 static void
762 gtk_toolbar_get_property (GObject    *object,
763                           guint       prop_id,
764                           GValue     *value,
765                           GParamSpec *pspec)
766 {
767   GtkToolbar *toolbar = GTK_TOOLBAR (object);
768   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
769   
770   switch (prop_id)
771     {
772     case PROP_ORIENTATION:
773       g_value_set_enum (value, toolbar->orientation);
774       break;
775     case PROP_TOOLBAR_STYLE:
776       g_value_set_enum (value, toolbar->style);
777       break;
778     case PROP_SHOW_ARROW:
779       g_value_set_boolean (value, priv->show_arrow);
780       break;
781     case PROP_TOOLTIPS:
782       g_value_set_boolean (value, gtk_toolbar_get_tooltips (toolbar));
783       break;
784     case PROP_ICON_SIZE:
785       g_value_set_enum (value, gtk_toolbar_get_icon_size (toolbar));
786       break;
787     case PROP_ICON_SIZE_SET:
788       g_value_set_boolean (value, toolbar->icon_size_set);
789       break;
790     default:
791       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
792       break;
793     }
794 }
795
796 static void
797 gtk_toolbar_map (GtkWidget *widget)
798 {
799   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (widget);
800   
801   GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->map (widget);
802   
803   if (priv->event_window)
804     gdk_window_show_unraised (priv->event_window);
805 }
806
807 static void
808 gtk_toolbar_unmap (GtkWidget *widget)
809 {
810   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (widget);
811   
812   if (priv->event_window)
813     gdk_window_hide (priv->event_window);
814   
815   GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->unmap (widget);
816 }
817
818 static void
819 gtk_toolbar_realize (GtkWidget *widget)
820 {
821   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
822   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
823   
824   GdkWindowAttr attributes;
825   gint attributes_mask;
826   gint border_width;
827   
828   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
829   
830   border_width = GTK_CONTAINER (widget)->border_width;
831   
832   attributes.wclass = GDK_INPUT_ONLY;
833   attributes.window_type = GDK_WINDOW_CHILD;
834   attributes.x = widget->allocation.x + border_width;
835   attributes.y = widget->allocation.y + border_width;
836   attributes.width = widget->allocation.width - border_width * 2;
837   attributes.height = widget->allocation.height - border_width * 2;
838   attributes.event_mask = gtk_widget_get_events (widget);
839   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
840                             GDK_BUTTON_RELEASE_MASK |
841                             GDK_ENTER_NOTIFY_MASK |
842                             GDK_LEAVE_NOTIFY_MASK);
843   
844   attributes_mask = GDK_WA_X | GDK_WA_Y;
845   
846   widget->window = gtk_widget_get_parent_window (widget);
847   g_object_ref (widget->window);
848   widget->style = gtk_style_attach (widget->style, widget->window);
849   
850   priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
851                                        &attributes, attributes_mask);
852   gdk_window_set_user_data (priv->event_window, toolbar);
853 }
854
855 static void
856 gtk_toolbar_unrealize (GtkWidget *widget)
857 {
858   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (widget);
859   
860   if (priv->event_window)
861     {
862       gdk_window_set_user_data (priv->event_window, NULL);
863       gdk_window_destroy (priv->event_window);
864       priv->event_window = NULL;
865     }
866   
867   if (GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->unrealize)
868     GTK_WIDGET_CLASS (gtk_toolbar_parent_class)->unrealize (widget);
869 }
870
871 static gint
872 gtk_toolbar_expose (GtkWidget      *widget,
873                     GdkEventExpose *event)
874 {
875   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
876   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
877   
878   GList *list;
879   gint border_width;
880   
881   border_width = GTK_CONTAINER (widget)->border_width;
882   
883   if (GTK_WIDGET_DRAWABLE (widget))
884     {
885       gtk_paint_box (widget->style,
886                      widget->window,
887                      GTK_WIDGET_STATE (widget),
888                      get_shadow_type (toolbar),
889                      &event->area, widget, "toolbar",
890                      border_width + widget->allocation.x,
891                      border_width + widget->allocation.y,
892                      widget->allocation.width - 2 * border_width,
893                      widget->allocation.height - 2 * border_width);
894     }
895   
896   for (list = priv->content; list != NULL; list = list->next)
897     {
898       ToolbarContent *content = list->data;
899       
900       toolbar_content_expose (content, GTK_CONTAINER (widget), event);
901     }
902   
903   gtk_container_propagate_expose (GTK_CONTAINER (widget),
904                                   priv->arrow_button,
905                                   event);
906   
907   return FALSE;
908 }
909
910 static void
911 gtk_toolbar_size_request (GtkWidget      *widget,
912                           GtkRequisition *requisition)
913 {
914   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
915   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
916   GList *list;
917   gint max_child_height;
918   gint max_child_width;
919   gint max_homogeneous_child_width;
920   gint max_homogeneous_child_height;
921   gint homogeneous_size;
922   gint long_req;
923   gint pack_front_size;
924   gint ipadding;
925   GtkRequisition arrow_requisition;
926   
927   max_homogeneous_child_width = 0;
928   max_homogeneous_child_height = 0;
929   max_child_width = 0;
930   max_child_height = 0;
931   for (list = priv->content; list != NULL; list = list->next)
932     {
933       GtkRequisition requisition;
934       ToolbarContent *content = list->data;
935       
936       if (!toolbar_content_visible (content, toolbar))
937         continue;
938       
939       toolbar_content_size_request (content, toolbar, &requisition);
940
941       max_child_width = MAX (max_child_width, requisition.width);
942       max_child_height = MAX (max_child_height, requisition.height);
943       
944       if (toolbar_content_is_homogeneous (content, toolbar))
945         {
946           max_homogeneous_child_width = MAX (max_homogeneous_child_width, requisition.width);
947           max_homogeneous_child_height = MAX (max_homogeneous_child_height, requisition.height);
948         }
949     }
950   
951   if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
952     homogeneous_size = max_homogeneous_child_width;
953   else
954     homogeneous_size = max_homogeneous_child_height;
955   
956   pack_front_size = 0;
957   for (list = priv->content; list != NULL; list = list->next)
958     {
959       ToolbarContent *content = list->data;
960       guint size;
961       
962       if (!toolbar_content_visible (content, toolbar))
963         continue;
964
965       if (toolbar_content_is_homogeneous (content, toolbar))
966         {
967           size = homogeneous_size;
968         }
969       else
970         {
971           GtkRequisition requisition;
972           
973           toolbar_content_size_request (content, toolbar, &requisition);
974           
975           if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
976             size = requisition.width;
977           else
978             size = requisition.height;
979         }
980
981       pack_front_size += size;
982     }
983   
984   if (priv->show_arrow && priv->api_mode == NEW_API)
985     {
986       gtk_widget_size_request (priv->arrow_button, &arrow_requisition);
987       
988       if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
989         long_req = arrow_requisition.width;
990       else
991         long_req = arrow_requisition.height;
992       
993       /* There is no point requesting space for the arrow if that would take
994        * up more space than all the items combined
995        */
996       long_req = MIN (long_req, pack_front_size);
997     }
998   else
999     {
1000       arrow_requisition.height = 0;
1001       arrow_requisition.width = 0;
1002       
1003       long_req = pack_front_size;
1004     }
1005   
1006   if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
1007     {
1008       requisition->width = long_req;
1009       requisition->height = MAX (max_child_height, arrow_requisition.height);
1010     }
1011   else
1012     {
1013       requisition->height = long_req;
1014       requisition->width = MAX (max_child_width, arrow_requisition.width);
1015     }
1016   
1017   /* Extra spacing */
1018   ipadding = get_internal_padding (toolbar);
1019   
1020   requisition->width += 2 * (ipadding + GTK_CONTAINER (toolbar)->border_width);
1021   requisition->height += 2 * (ipadding + GTK_CONTAINER (toolbar)->border_width);
1022   
1023   if (get_shadow_type (toolbar) != GTK_SHADOW_NONE)
1024     {
1025       requisition->width += 2 * widget->style->xthickness;
1026       requisition->height += 2 * widget->style->ythickness;
1027     }
1028   
1029   toolbar->button_maxw = max_homogeneous_child_width;
1030   toolbar->button_maxh = max_homogeneous_child_height;
1031 }
1032
1033 static gint
1034 position (GtkToolbar *toolbar,
1035           gint        from,
1036           gint        to,
1037           gdouble     elapsed)
1038 {
1039   gint n_pixels;
1040
1041   if (! GTK_TOOLBAR_GET_PRIVATE (toolbar)->animation)
1042     return to;
1043
1044   if (elapsed <= ACCEL_THRESHOLD)
1045     {
1046       n_pixels = SLIDE_SPEED * elapsed;
1047     }
1048   else
1049     {
1050       /* The formula is a second degree polynomial in
1051        * @elapsed that has the line SLIDE_SPEED * @elapsed
1052        * as tangent for @elapsed == ACCEL_THRESHOLD.
1053        * This makes @n_pixels a smooth function of elapsed time.
1054        */
1055       n_pixels = (SLIDE_SPEED / ACCEL_THRESHOLD) * elapsed * elapsed -
1056         SLIDE_SPEED * elapsed + SLIDE_SPEED * ACCEL_THRESHOLD;
1057     }
1058
1059   if (to > from)
1060     return MIN (from + n_pixels, to);
1061   else
1062     return MAX (from - n_pixels, to);
1063 }
1064
1065 static void
1066 compute_intermediate_allocation (GtkToolbar          *toolbar,
1067                                  const GtkAllocation *start,
1068                                  const GtkAllocation *goal,
1069                                  GtkAllocation       *intermediate)
1070 {
1071   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1072   gdouble elapsed = g_timer_elapsed (priv->timer, NULL);
1073
1074   intermediate->x      = position (toolbar, start->x, goal->x, elapsed);
1075   intermediate->y      = position (toolbar, start->y, goal->y, elapsed);
1076   intermediate->width  = position (toolbar, start->x + start->width,
1077                                    goal->x + goal->width,
1078                                    elapsed) - intermediate->x;
1079   intermediate->height = position (toolbar, start->y + start->height,
1080                                    goal->y + goal->height,
1081                                    elapsed) - intermediate->y;
1082 }
1083
1084 static void
1085 fixup_allocation_for_rtl (gint           total_size,
1086                           GtkAllocation *allocation)
1087 {
1088   allocation->x += (total_size - (2 * allocation->x + allocation->width));
1089 }
1090
1091 static void
1092 fixup_allocation_for_vertical (GtkAllocation *allocation)
1093 {
1094   gint tmp;
1095   
1096   tmp = allocation->x;
1097   allocation->x = allocation->y;
1098   allocation->y = tmp;
1099   
1100   tmp = allocation->width;
1101   allocation->width = allocation->height;
1102   allocation->height = tmp;
1103 }
1104
1105 static gint
1106 get_item_size (GtkToolbar     *toolbar,
1107                ToolbarContent *content)
1108 {
1109   GtkRequisition requisition;
1110   
1111   toolbar_content_size_request (content, toolbar, &requisition);
1112   
1113   if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
1114     {
1115       if (toolbar_content_is_homogeneous (content, toolbar))
1116         return toolbar->button_maxw;
1117       else
1118         return requisition.width;
1119     }
1120   else
1121     {
1122       if (toolbar_content_is_homogeneous (content, toolbar))
1123         return toolbar->button_maxh;
1124       else
1125         return requisition.height;
1126     }
1127 }
1128
1129 static gboolean
1130 slide_idle_handler (gpointer data)
1131 {
1132   GtkToolbar *toolbar = data;
1133   GtkToolbarPrivate *priv;
1134   GList *list;
1135   
1136   priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1137   
1138   if (priv->need_sync)
1139     {
1140       gdk_flush ();
1141       priv->need_sync = FALSE;
1142     }
1143   
1144   for (list = priv->content; list != NULL; list = list->next)
1145     {
1146       ToolbarContent *content = list->data;
1147       ItemState state;
1148       GtkAllocation goal_allocation;
1149       GtkAllocation allocation;
1150       gboolean cont;
1151
1152       state = toolbar_content_get_state (content);
1153       toolbar_content_get_goal_allocation (content, &goal_allocation);
1154       toolbar_content_get_allocation (content, &allocation);
1155       
1156       cont = FALSE;
1157       
1158       if (state == NOT_ALLOCATED)
1159         {
1160           /* an unallocated item means that size allocate has to
1161            * called at least once more
1162            */
1163           cont = TRUE;
1164         }
1165
1166       /* An invisible item with a goal allocation of
1167        * 0 is already at its goal.
1168        */
1169       if ((state == NORMAL || state == OVERFLOWN) &&
1170           ((goal_allocation.width != 0 &&
1171             goal_allocation.height != 0) ||
1172            toolbar_content_child_visible (content)))
1173         {
1174           if ((goal_allocation.x != allocation.x ||
1175                goal_allocation.y != allocation.y ||
1176                goal_allocation.width != allocation.width ||
1177                goal_allocation.height != allocation.height))
1178             {
1179               /* An item is not in its right position yet. Note
1180                * that OVERFLOWN items do get an allocation in
1181                * gtk_toolbar_size_allocate(). This way you can see
1182                * them slide back in when you drag an item off the
1183                * toolbar.
1184                */
1185               cont = TRUE;
1186             }
1187         }
1188
1189       if (toolbar_content_is_placeholder (content) &&
1190           toolbar_content_disappearing (content) &&
1191           toolbar_content_child_visible (content))
1192         {
1193           /* A disappearing placeholder is still visible.
1194            */
1195              
1196           cont = TRUE;
1197         }
1198       
1199       if (cont)
1200         {
1201           gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1202           
1203           GDK_THREADS_LEAVE ();
1204           return TRUE;
1205         }
1206     }
1207   
1208   priv->is_sliding = FALSE;
1209   priv->idle_id = 0;
1210
1211   return FALSE;
1212 }
1213
1214 static gboolean
1215 rect_within (GtkAllocation *a1,
1216              GtkAllocation *a2)
1217 {
1218   return (a1->x >= a2->x                         &&
1219           a1->x + a1->width <= a2->x + a2->width &&
1220           a1->y >= a2->y                         &&
1221           a1->y + a1->height <= a2->y + a2->height);
1222 }
1223
1224 static void
1225 gtk_toolbar_begin_sliding (GtkToolbar *toolbar)
1226 {
1227   GtkWidget *widget = GTK_WIDGET (toolbar);
1228   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1229   GList *list;
1230   gint cur_x;
1231   gint cur_y;
1232   gint border_width;
1233   gboolean rtl;
1234   gboolean vertical;
1235   
1236   /* Start the sliding. This function copies the allocation of every
1237    * item into content->start_allocation. For items that haven't
1238    * been allocated yet, we calculate their position and save that
1239    * in start_allocatino along with zero width and zero height.
1240    *
1241    * FIXME: It would be nice if we could share this code with
1242    * the equivalent in gtk_widget_size_allocate().
1243    */
1244   priv->is_sliding = TRUE;
1245   
1246   if (!priv->idle_id)
1247     priv->idle_id = gdk_threads_add_idle (slide_idle_handler, toolbar);
1248   
1249   rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
1250   vertical = (toolbar->orientation == GTK_ORIENTATION_VERTICAL);
1251   border_width = get_internal_padding (toolbar) + GTK_CONTAINER (toolbar)->border_width;
1252   
1253   if (rtl)
1254     {
1255       cur_x = widget->allocation.width - border_width - widget->style->xthickness;
1256       cur_y = widget->allocation.height - border_width - widget->style->ythickness;
1257     }
1258   else
1259     {
1260       cur_x = border_width + widget->style->xthickness;
1261       cur_y = border_width + widget->style->ythickness;
1262     }
1263   
1264   cur_x += widget->allocation.x;
1265   cur_y += widget->allocation.y;
1266   
1267   for (list = priv->content; list != NULL; list = list->next)
1268     {
1269       ToolbarContent *content = list->data;
1270       GtkAllocation new_start_allocation;
1271       GtkAllocation item_allocation;
1272       ItemState state;
1273       
1274       state = toolbar_content_get_state (content);
1275       toolbar_content_get_allocation (content, &item_allocation);
1276       
1277       if ((state == NORMAL &&
1278            rect_within (&item_allocation, &(widget->allocation))) ||
1279           state == OVERFLOWN)
1280         {
1281           new_start_allocation = item_allocation;
1282         }
1283       else
1284         {
1285           new_start_allocation.x = cur_x;
1286           new_start_allocation.y = cur_y;
1287           
1288           if (vertical)
1289             {
1290               new_start_allocation.width = widget->allocation.width -
1291                 2 * border_width - 2 * widget->style->xthickness;
1292               new_start_allocation.height = 0;
1293             }
1294           else
1295             {
1296               new_start_allocation.width = 0;
1297               new_start_allocation.height = widget->allocation.height -
1298                 2 * border_width - 2 * widget->style->ythickness;
1299             }
1300         }
1301       
1302       if (vertical)
1303         cur_y = new_start_allocation.y + new_start_allocation.height;
1304       else if (rtl)
1305         cur_x = new_start_allocation.x;
1306       else
1307         cur_x = new_start_allocation.x + new_start_allocation.width;
1308       
1309       toolbar_content_set_start_allocation (content, &new_start_allocation);
1310     }
1311
1312   /* This resize will run before the first idle handler. This
1313    * will make sure that items get the right goal allocation
1314    * so that the idle handler will not immediately return
1315    * FALSE
1316    */
1317   gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1318   g_timer_reset (priv->timer);
1319 }
1320
1321 static void
1322 gtk_toolbar_stop_sliding (GtkToolbar *toolbar)
1323 {
1324   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1325   
1326   if (priv->is_sliding)
1327     {
1328       GList *list;
1329       
1330       priv->is_sliding = FALSE;
1331       
1332       if (priv->idle_id)
1333         {
1334           g_source_remove (priv->idle_id);
1335           priv->idle_id = 0;
1336         }
1337       
1338       list = priv->content;
1339       while (list)
1340         {
1341           ToolbarContent *content = list->data;
1342           list = list->next;
1343
1344           if (toolbar_content_is_placeholder (content))
1345             {
1346               toolbar_content_remove (content, toolbar);
1347               toolbar_content_free (content);
1348             }
1349         }
1350       
1351       gtk_widget_queue_resize_no_redraw (GTK_WIDGET (toolbar));
1352     }
1353 }
1354
1355 static void
1356 remove_item (GtkWidget *menu_item,
1357              gpointer   data)
1358 {
1359   gtk_container_remove (GTK_CONTAINER (menu_item->parent), menu_item);
1360 }
1361
1362 static void
1363 menu_deactivated (GtkWidget  *menu,
1364                   GtkToolbar *toolbar)
1365 {
1366   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1367   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
1368 }
1369
1370 static void
1371 menu_detached (GtkWidget  *toolbar,
1372                GtkMenu    *menu)
1373 {
1374   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1375   priv->menu = NULL;
1376 }
1377
1378 static void
1379 rebuild_menu (GtkToolbar *toolbar)
1380 {
1381   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1382   GList *list, *children;
1383   
1384   if (!priv->menu)
1385     {
1386       priv->menu = GTK_MENU (gtk_menu_new());
1387       gtk_menu_attach_to_widget (priv->menu,
1388                                  GTK_WIDGET (toolbar),
1389                                  menu_detached);
1390
1391       g_signal_connect (priv->menu, "deactivate", G_CALLBACK (menu_deactivated), toolbar);
1392     }
1393
1394   gtk_container_foreach (GTK_CONTAINER (priv->menu), remove_item, NULL);
1395   
1396   for (list = priv->content; list != NULL; list = list->next)
1397     {
1398       ToolbarContent *content = list->data;
1399       
1400       if (toolbar_content_get_state (content) == OVERFLOWN &&
1401           !toolbar_content_is_placeholder (content))
1402         {
1403           GtkWidget *menu_item = toolbar_content_retrieve_menu_item (content);
1404           
1405           if (menu_item)
1406             {
1407               g_assert (GTK_IS_MENU_ITEM (menu_item));
1408               gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), menu_item);
1409             }
1410         }
1411     }
1412
1413   /* Remove leading and trailing separator items */
1414   children = gtk_container_get_children (GTK_CONTAINER (priv->menu));
1415   
1416   list = children;
1417   while (list && GTK_IS_SEPARATOR_MENU_ITEM (list->data))
1418     {
1419       GtkWidget *child = list->data;
1420       
1421       gtk_container_remove (GTK_CONTAINER (priv->menu), child);
1422       list = list->next;
1423     }
1424   g_list_free (children);
1425
1426   /* Regenerate the list of children so we don't try to remove items twice */
1427   children = gtk_container_get_children (GTK_CONTAINER (priv->menu));
1428
1429   list = g_list_last (children);
1430   while (list && GTK_IS_SEPARATOR_MENU_ITEM (list->data))
1431     {
1432       GtkWidget *child = list->data;
1433
1434       gtk_container_remove (GTK_CONTAINER (priv->menu), child);
1435       list = list->prev;
1436     }
1437   g_list_free (children);
1438
1439   priv->need_rebuild = FALSE;
1440 }
1441
1442 static void
1443 gtk_toolbar_size_allocate (GtkWidget     *widget,
1444                            GtkAllocation *allocation)
1445 {
1446   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1447   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1448   GtkAllocation *allocations;
1449   ItemState *new_states;
1450   GtkAllocation arrow_allocation;
1451   gint arrow_size;
1452   gint size, pos, short_size;
1453   GList *list;
1454   gint i;
1455   gboolean need_arrow;
1456   gint n_expand_items;
1457   gint border_width;
1458   gint available_size;
1459   gint n_items;
1460   gint needed_size;
1461   GtkRequisition arrow_requisition;
1462   gboolean overflowing;
1463   gboolean size_changed;
1464   gdouble elapsed;
1465   GtkAllocation item_area;
1466   GtkShadowType shadow_type;
1467   
1468   size_changed = FALSE;
1469   if (widget->allocation.x != allocation->x             ||
1470       widget->allocation.y != allocation->y             ||
1471       widget->allocation.width != allocation->width     ||
1472       widget->allocation.height != allocation->height)
1473     {
1474       size_changed = TRUE;
1475     }
1476   
1477   if (size_changed)
1478     gtk_toolbar_stop_sliding (toolbar);
1479   
1480   widget->allocation = *allocation;
1481   
1482   border_width = GTK_CONTAINER (toolbar)->border_width;
1483   
1484   if (GTK_WIDGET_REALIZED (widget))
1485     {
1486       gdk_window_move_resize (priv->event_window,
1487                               allocation->x + border_width,
1488                               allocation->y + border_width,
1489                               allocation->width - border_width * 2,
1490                               allocation->height - border_width * 2);
1491     }
1492   
1493   border_width += get_internal_padding (toolbar);
1494   
1495   gtk_widget_get_child_requisition (GTK_WIDGET (priv->arrow_button),
1496                                     &arrow_requisition);
1497   
1498   shadow_type = get_shadow_type (toolbar);
1499
1500   if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
1501     {
1502       available_size = size = allocation->width - 2 * border_width;
1503       short_size = allocation->height - 2 * border_width;
1504       arrow_size = arrow_requisition.width;
1505       
1506       if (shadow_type != GTK_SHADOW_NONE)
1507         {
1508           available_size -= 2 * widget->style->xthickness;
1509           short_size -= 2 * widget->style->ythickness;
1510         }
1511     }
1512   else
1513     {
1514       available_size = size = allocation->height - 2 * border_width;
1515       short_size = allocation->width - 2 * border_width;
1516       arrow_size = arrow_requisition.height;
1517       
1518       if (shadow_type != GTK_SHADOW_NONE)
1519         {
1520           available_size -= 2 * widget->style->ythickness;
1521           short_size -= 2 * widget->style->xthickness;
1522         }
1523     }
1524   
1525   n_items = g_list_length (priv->content);
1526   allocations = g_new0 (GtkAllocation, n_items);
1527   new_states = g_new0 (ItemState, n_items);
1528   
1529   needed_size = 0;
1530   need_arrow = FALSE;
1531   for (list = priv->content; list != NULL; list = list->next)
1532     {
1533       ToolbarContent *content = list->data;
1534       
1535       if (toolbar_content_visible (content, toolbar))
1536         {
1537           needed_size += get_item_size (toolbar, content);
1538
1539           /* Do we need an arrow?
1540            *
1541            * Assume we don't, and see if any non-separator item with a
1542            * proxy menu item is then going to overflow.
1543            */
1544           if (needed_size > available_size                      &&
1545               !need_arrow                                       &&
1546               priv->show_arrow                                  &&
1547               priv->api_mode == NEW_API                         &&
1548               toolbar_content_has_proxy_menu_item (content)     &&
1549               !toolbar_content_is_separator (content))
1550             {
1551               need_arrow = TRUE;
1552             }
1553         }
1554     }
1555   
1556   if (need_arrow)
1557     size = available_size - arrow_size;
1558   else
1559     size = available_size;
1560   
1561   /* calculate widths and states of items */
1562   overflowing = FALSE;
1563   for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1564     {
1565       ToolbarContent *content = list->data;
1566       gint item_size;
1567       
1568       if (!toolbar_content_visible (content, toolbar))
1569         {
1570           new_states[i] = HIDDEN;
1571           continue;
1572         }
1573       
1574       item_size = get_item_size (toolbar, content);
1575       if (item_size <= size && !overflowing)
1576         {
1577           size -= item_size;
1578           allocations[i].width = item_size;
1579           new_states[i] = NORMAL;
1580         }
1581       else
1582         {
1583           overflowing = TRUE;
1584           new_states[i] = OVERFLOWN;
1585           allocations[i].width = item_size;
1586         }
1587     }
1588   
1589   /* calculate width of arrow */  
1590   if (need_arrow)
1591     {
1592       arrow_allocation.width = arrow_size;
1593       arrow_allocation.height = MAX (short_size, 1);
1594     }
1595   
1596   /* expand expandable items */
1597   
1598   /* We don't expand when there is an overflow menu, because that leads to
1599    * weird jumps when items get moved to the overflow menu and the expanding
1600    * items suddenly get a lot of extra space
1601    */
1602   if (!overflowing)
1603     {
1604       gint max_child_expand;
1605       n_expand_items = 0;
1606       
1607       for (i = 0, list = priv->content; list != NULL; list = list->next, ++i)
1608         {
1609           ToolbarContent *content = list->data;
1610           
1611           if (toolbar_content_get_expand (content) && new_states[i] == NORMAL)
1612             n_expand_items++;
1613         }
1614       
1615       max_child_expand = get_max_child_expand (toolbar);
1616       for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1617         {
1618           ToolbarContent *content = list->data;
1619           
1620           if (toolbar_content_get_expand (content) && new_states[i] == NORMAL)
1621             {
1622               gint extra = size / n_expand_items;
1623               if (size % n_expand_items != 0)
1624                 extra++;
1625
1626               if (extra > max_child_expand)
1627                 extra = max_child_expand;
1628
1629               allocations[i].width += extra;
1630               size -= extra;
1631               n_expand_items--;
1632             }
1633         }
1634       
1635       g_assert (n_expand_items == 0);
1636     }
1637   
1638   /* position items */
1639   pos = border_width;
1640   for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1641     {
1642       /* both NORMAL and OVERFLOWN items get a position. This ensures
1643        * that sliding will work for OVERFLOWN items too
1644        */
1645       if (new_states[i] == NORMAL ||
1646           new_states[i] == OVERFLOWN)
1647         {
1648           allocations[i].x = pos;
1649           allocations[i].y = border_width;
1650           allocations[i].height = short_size;
1651           
1652           pos += allocations[i].width;
1653         }
1654     }
1655   
1656   /* position arrow */
1657   if (need_arrow)
1658     {
1659       arrow_allocation.x = available_size - border_width - arrow_allocation.width;
1660       arrow_allocation.y = border_width;
1661     }
1662   
1663   item_area.x = border_width;
1664   item_area.y = border_width;
1665   item_area.width = available_size - (need_arrow? arrow_size : 0);
1666   item_area.height = short_size;
1667
1668   /* fix up allocations in the vertical or RTL cases */
1669   if (toolbar->orientation == GTK_ORIENTATION_VERTICAL)
1670     {
1671       for (i = 0; i < n_items; ++i)
1672         fixup_allocation_for_vertical (&(allocations[i]));
1673       
1674       if (need_arrow)
1675         fixup_allocation_for_vertical (&arrow_allocation);
1676
1677       fixup_allocation_for_vertical (&item_area);
1678     }
1679   else if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
1680     {
1681       for (i = 0; i < n_items; ++i)
1682         fixup_allocation_for_rtl (available_size, &(allocations[i]));
1683       
1684       if (need_arrow)
1685         fixup_allocation_for_rtl (available_size, &arrow_allocation);
1686
1687       fixup_allocation_for_rtl (available_size, &item_area);
1688     }
1689   
1690   /* translate the items by allocation->(x,y) */
1691   for (i = 0; i < n_items; ++i)
1692     {
1693       allocations[i].x += allocation->x;
1694       allocations[i].y += allocation->y;
1695       
1696       if (shadow_type != GTK_SHADOW_NONE)
1697         {
1698           allocations[i].x += widget->style->xthickness;
1699           allocations[i].y += widget->style->ythickness;
1700         }
1701     }
1702   
1703   if (need_arrow)
1704     {
1705       arrow_allocation.x += allocation->x;
1706       arrow_allocation.y += allocation->y;
1707       
1708       if (shadow_type != GTK_SHADOW_NONE)
1709         {
1710           arrow_allocation.x += widget->style->xthickness;
1711           arrow_allocation.y += widget->style->ythickness;
1712         }
1713     }
1714
1715   item_area.x += allocation->x;
1716   item_area.y += allocation->y;
1717   if (shadow_type != GTK_SHADOW_NONE)
1718     {
1719       item_area.x += widget->style->xthickness;
1720       item_area.y += widget->style->ythickness;
1721     }
1722
1723   /* did anything change? */
1724   for (list = priv->content, i = 0; list != NULL; list = list->next, i++)
1725     {
1726       ToolbarContent *content = list->data;
1727       
1728       if (toolbar_content_get_state (content) == NORMAL &&
1729           new_states[i] != NORMAL)
1730         {
1731           /* an item disappeared and we didn't change size, so begin sliding */
1732           if (!size_changed && priv->api_mode == NEW_API)
1733             gtk_toolbar_begin_sliding (toolbar);
1734         }
1735     }
1736   
1737   /* finally allocate the items */
1738   if (priv->is_sliding)
1739     {
1740       for (list = priv->content, i = 0; list != NULL; list = list->next, i++)
1741         {
1742           ToolbarContent *content = list->data;
1743           
1744           toolbar_content_set_goal_allocation (content, &(allocations[i]));
1745         }
1746     }
1747
1748   elapsed = g_timer_elapsed (priv->timer, NULL);
1749   for (list = priv->content, i = 0; list != NULL; list = list->next, ++i)
1750     {
1751       ToolbarContent *content = list->data;
1752
1753       if (new_states[i] == OVERFLOWN ||
1754           new_states[i] == NORMAL)
1755         {
1756           GtkAllocation alloc;
1757           GtkAllocation start_allocation = { 0, };
1758           GtkAllocation goal_allocation;
1759
1760           if (priv->is_sliding)
1761             {
1762               toolbar_content_get_start_allocation (content, &start_allocation);
1763               toolbar_content_get_goal_allocation (content, &goal_allocation);
1764               
1765               compute_intermediate_allocation (toolbar,
1766                                                &start_allocation,
1767                                                &goal_allocation,
1768                                                &alloc);
1769
1770               priv->need_sync = TRUE;
1771             }
1772           else
1773             {
1774               alloc = allocations[i];
1775             }
1776
1777           if (alloc.width <= 0 || alloc.height <= 0)
1778             {
1779               toolbar_content_set_child_visible (content, toolbar, FALSE);
1780             }
1781           else
1782             {
1783               if (!rect_within (&alloc, &item_area))
1784                 {
1785                   toolbar_content_set_child_visible (content, toolbar, FALSE);
1786                   toolbar_content_size_allocate (content, &alloc);
1787                 }
1788               else
1789                 {
1790                   toolbar_content_set_child_visible (content, toolbar, TRUE);
1791                   toolbar_content_size_allocate (content, &alloc);
1792                 }
1793             }
1794         }
1795       else
1796         {
1797           toolbar_content_set_child_visible (content, toolbar, FALSE);
1798         }
1799           
1800       toolbar_content_set_state (content, new_states[i]);
1801     }
1802   
1803   if (priv->menu && priv->need_rebuild)
1804     rebuild_menu (toolbar);
1805   
1806   if (need_arrow)
1807     {
1808       gtk_widget_size_allocate (GTK_WIDGET (priv->arrow_button),
1809                                 &arrow_allocation);
1810       gtk_widget_show (GTK_WIDGET (priv->arrow_button));
1811     }
1812   else
1813     {
1814       gtk_widget_hide (GTK_WIDGET (priv->arrow_button));
1815
1816       if (priv->menu && GTK_WIDGET_VISIBLE (priv->menu))
1817         gtk_menu_shell_deactivate (GTK_MENU_SHELL (priv->menu));
1818     }
1819
1820   g_free (allocations);
1821   g_free (new_states);
1822 }
1823
1824 static void
1825 gtk_toolbar_update_button_relief (GtkToolbar *toolbar)
1826 {
1827   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1828   GtkReliefStyle relief;
1829
1830   relief = get_button_relief (toolbar);
1831
1832   if (relief != gtk_button_get_relief (GTK_BUTTON (priv->arrow_button)))
1833     {
1834       gtk_toolbar_reconfigured (toolbar);
1835   
1836       gtk_button_set_relief (GTK_BUTTON (priv->arrow_button), relief);
1837     }
1838 }
1839
1840 static void
1841 gtk_toolbar_style_set (GtkWidget *widget,
1842                        GtkStyle  *prev_style)
1843 {
1844   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (widget);
1845   
1846   priv->max_homogeneous_pixels = -1;
1847
1848   if (GTK_WIDGET_REALIZED (widget))
1849     gtk_style_set_background (widget->style, widget->window, widget->state);
1850   
1851   if (prev_style)
1852     gtk_toolbar_update_button_relief (GTK_TOOLBAR (widget));
1853 }
1854
1855 static GList *
1856 gtk_toolbar_list_children_in_focus_order (GtkToolbar       *toolbar,
1857                                           GtkDirectionType  dir)
1858 {
1859   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1860   GList *result = NULL;
1861   GList *list;
1862   gboolean rtl;
1863   
1864   /* generate list of children in reverse logical order */
1865   
1866   for (list = priv->content; list != NULL; list = list->next)
1867     {
1868       ToolbarContent *content = list->data;
1869       GtkWidget *widget;
1870       
1871       widget = toolbar_content_get_widget (content);
1872       
1873       if (widget)
1874         result = g_list_prepend (result, widget);
1875     }
1876   
1877   result = g_list_prepend (result, priv->arrow_button);
1878   
1879   rtl = (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL);
1880   
1881   /* move in logical order when
1882    *
1883    *    - dir is TAB_FORWARD
1884    *
1885    *    - in RTL mode and moving left or up
1886    *
1887    *    - in LTR mode and moving right or down
1888    */
1889   if (dir == GTK_DIR_TAB_FORWARD                                        ||
1890       (rtl  && (dir == GTK_DIR_UP   || dir == GTK_DIR_LEFT))            ||
1891       (!rtl && (dir == GTK_DIR_DOWN || dir == GTK_DIR_RIGHT)))
1892     {
1893       result = g_list_reverse (result);
1894     }
1895   
1896   return result;
1897 }
1898
1899 static gboolean
1900 gtk_toolbar_focus_home_or_end (GtkToolbar *toolbar,
1901                                gboolean    focus_home)
1902 {
1903   GList *children, *list;
1904   GtkDirectionType dir = focus_home? GTK_DIR_RIGHT : GTK_DIR_LEFT;
1905   
1906   children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);
1907   
1908   if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
1909     {
1910       children = g_list_reverse (children);
1911       
1912       dir = (dir == GTK_DIR_RIGHT)? GTK_DIR_LEFT : GTK_DIR_RIGHT;
1913     }
1914   
1915   for (list = children; list != NULL; list = list->next)
1916     {
1917       GtkWidget *child = list->data;
1918       
1919       if (GTK_CONTAINER (toolbar)->focus_child == child)
1920         break;
1921       
1922       if (GTK_WIDGET_MAPPED (child) && gtk_widget_child_focus (child, dir))
1923         break;
1924     }
1925   
1926   g_list_free (children);
1927   
1928   return TRUE;
1929 }   
1930
1931 /* Keybinding handler. This function is called when the user presses
1932  * Ctrl TAB or an arrow key.
1933  */
1934 static void
1935 gtk_toolbar_move_focus (GtkWidget        *widget,
1936                         GtkDirectionType  dir)
1937 {
1938   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1939   GtkContainer *container = GTK_CONTAINER (toolbar);
1940   GList *list;
1941   gboolean try_focus = FALSE;
1942   GList *children;
1943
1944   if (container->focus_child &&
1945       gtk_widget_child_focus (container->focus_child, dir))
1946     {
1947       return;
1948     }
1949   
1950   children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);
1951   
1952   for (list = children; list != NULL; list = list->next)
1953     {
1954       GtkWidget *child = list->data;
1955       
1956       if (try_focus && GTK_WIDGET_MAPPED (child) && gtk_widget_child_focus (child, dir))
1957         break;
1958       
1959       if (child == GTK_CONTAINER (toolbar)->focus_child)
1960         try_focus = TRUE;
1961     }
1962   
1963   g_list_free (children);
1964 }
1965
1966 /* The focus handler for the toolbar. It called when the user presses
1967  * TAB or otherwise tries to focus the toolbar.
1968  */
1969 static gboolean
1970 gtk_toolbar_focus (GtkWidget        *widget,
1971                    GtkDirectionType  dir)
1972 {
1973   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1974   GList *children, *list;
1975   gboolean result = FALSE;
1976
1977   /* if focus is already somewhere inside the toolbar then return FALSE.
1978    * The only way focus can stay inside the toolbar is when the user presses
1979    * arrow keys or Ctrl TAB (both of which are handled by the
1980    * gtk_toolbar_move_focus() keybinding function.
1981    */
1982   if (GTK_CONTAINER (widget)->focus_child)
1983     return FALSE;
1984
1985   children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);
1986
1987   for (list = children; list != NULL; list = list->next)
1988     {
1989       GtkWidget *child = list->data;
1990       
1991       if (GTK_WIDGET_MAPPED (child) && gtk_widget_child_focus (child, dir))
1992         {
1993           result = TRUE;
1994           break;
1995         }
1996     }
1997
1998   g_list_free (children);
1999
2000   return result;
2001 }
2002
2003 static GtkSettings *
2004 toolbar_get_settings (GtkToolbar *toolbar)
2005 {
2006   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2007   return priv->settings;
2008 }
2009
2010 static void
2011 style_change_notify (GtkToolbar *toolbar)
2012 {
2013   if (!toolbar->style_set)
2014     {
2015       /* pretend it was set, then unset, thus reverting to new default */
2016       toolbar->style_set = TRUE;
2017       gtk_toolbar_unset_style (toolbar);
2018     }
2019 }
2020
2021 static void
2022 icon_size_change_notify (GtkToolbar *toolbar)
2023 {
2024   if (!toolbar->icon_size_set)
2025     {
2026       /* pretend it was set, then unset, thus reverting to new default */
2027       toolbar->icon_size_set = TRUE;
2028       gtk_toolbar_unset_icon_size (toolbar);
2029     }
2030 }
2031
2032 static void
2033 animation_change_notify (GtkToolbar *toolbar)
2034 {
2035   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2036   GtkSettings *settings = toolbar_get_settings (toolbar);
2037   gboolean animation;
2038
2039   if (settings)
2040     g_object_get (settings,
2041                   "gtk-enable-animations", &animation,
2042                   NULL);
2043   else
2044     animation = DEFAULT_ANIMATION_STATE;
2045
2046   priv->animation = animation;
2047 }
2048
2049 static void
2050 settings_change_notify (GtkSettings      *settings,
2051                         const GParamSpec *pspec,
2052                         GtkToolbar       *toolbar)
2053 {
2054   if (! strcmp (pspec->name, "gtk-toolbar-style"))
2055     style_change_notify (toolbar);
2056   else if (! strcmp (pspec->name, "gtk-toolbar-icon-size"))
2057     icon_size_change_notify (toolbar);
2058   else if (! strcmp (pspec->name, "gtk-enable-animations"))
2059     animation_change_notify (toolbar);
2060 }
2061
2062 static void
2063 gtk_toolbar_screen_changed (GtkWidget *widget,
2064                             GdkScreen *previous_screen)
2065 {
2066   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (widget);
2067   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
2068   GtkSettings *old_settings = toolbar_get_settings (toolbar);
2069   GtkSettings *settings;
2070   
2071   if (gtk_widget_has_screen (GTK_WIDGET (toolbar)))
2072     settings = gtk_widget_get_settings (GTK_WIDGET (toolbar));
2073   else
2074     settings = NULL;
2075   
2076   if (settings == old_settings)
2077     return;
2078   
2079   if (old_settings)
2080     {
2081       g_signal_handler_disconnect (old_settings, priv->settings_connection);
2082
2083       g_object_unref (old_settings);
2084     }
2085
2086   if (settings)
2087     {
2088       priv->settings_connection =
2089         g_signal_connect (settings, "notify",
2090                           G_CALLBACK (settings_change_notify),
2091                           toolbar);
2092
2093       priv->settings = g_object_ref (settings);
2094     }
2095   else
2096     priv->settings = NULL;
2097
2098   style_change_notify (toolbar);
2099   icon_size_change_notify (toolbar);
2100   animation_change_notify (toolbar);
2101 }
2102
2103 static int
2104 find_drop_index (GtkToolbar *toolbar,
2105                  gint        x,
2106                  gint        y)
2107 {
2108   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2109   GList *interesting_content;
2110   GList *list;
2111   GtkOrientation orientation;
2112   GtkTextDirection direction;
2113   gint best_distance = G_MAXINT;
2114   gint distance;
2115   gint cursor;
2116   gint pos;
2117   ToolbarContent *best_content;
2118   GtkAllocation allocation;
2119   
2120   /* list items we care about wrt. drag and drop */
2121   interesting_content = NULL;
2122   for (list = priv->content; list != NULL; list = list->next)
2123     {
2124       ToolbarContent *content = list->data;
2125       
2126       if (toolbar_content_get_state (content) == NORMAL)
2127         interesting_content = g_list_prepend (interesting_content, content);
2128     }
2129   interesting_content = g_list_reverse (interesting_content);
2130   
2131   if (!interesting_content)
2132     return 0;
2133   
2134   orientation = toolbar->orientation;
2135   direction = gtk_widget_get_direction (GTK_WIDGET (toolbar));
2136   
2137   /* distance to first interesting item */
2138   best_content = interesting_content->data;
2139   toolbar_content_get_allocation (best_content, &allocation);
2140   
2141   if (orientation == GTK_ORIENTATION_HORIZONTAL)
2142     {
2143       cursor = x;
2144       
2145       if (direction == GTK_TEXT_DIR_LTR)
2146         pos = allocation.x;
2147       else
2148         pos = allocation.x + allocation.width;
2149     }
2150   else
2151     {
2152       cursor = y;
2153       pos = allocation.y;
2154     }
2155   
2156   best_content = NULL;
2157   best_distance = ABS (pos - cursor);
2158   
2159   /* distance to far end of each item */
2160   for (list = interesting_content; list != NULL; list = list->next)
2161     {
2162       ToolbarContent *content = list->data;
2163       
2164       toolbar_content_get_allocation (content, &allocation);
2165       
2166       if (orientation == GTK_ORIENTATION_HORIZONTAL)
2167         {
2168           if (direction == GTK_TEXT_DIR_LTR)
2169             pos = allocation.x + allocation.width;
2170           else
2171             pos = allocation.x;
2172         }
2173       else
2174         {
2175           pos = allocation.y + allocation.height;
2176         }
2177       
2178       distance = ABS (pos - cursor);
2179       
2180       if (distance < best_distance)
2181         {
2182           best_distance = distance;
2183           best_content = content;
2184         }
2185     }
2186   
2187   g_list_free (interesting_content);
2188   
2189   if (!best_content)
2190     return 0;
2191   else
2192     return g_list_index (priv->content, best_content) + 1;
2193 }
2194
2195 static void
2196 reset_all_placeholders (GtkToolbar *toolbar)
2197 {
2198   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2199   GList *list;
2200   
2201   for (list = priv->content; list != NULL; list = list->next)
2202     {
2203       ToolbarContent *content = list->data;
2204       if (toolbar_content_is_placeholder (content))
2205         toolbar_content_set_disappearing (content, TRUE);
2206     }
2207 }
2208
2209 static gint
2210 physical_to_logical (GtkToolbar *toolbar,
2211                      gint        physical)
2212 {
2213   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2214   GList *list;
2215   int logical;
2216   
2217   g_assert (physical >= 0);
2218   
2219   logical = 0;
2220   for (list = priv->content; list && physical > 0; list = list->next)
2221     {
2222       ToolbarContent *content = list->data;
2223       
2224       if (!toolbar_content_is_placeholder (content))
2225         logical++;
2226       physical--;
2227     }
2228   
2229   g_assert (physical == 0);
2230   
2231   return logical;
2232 }
2233
2234 static gint
2235 logical_to_physical (GtkToolbar *toolbar,
2236                      gint        logical)
2237 {
2238   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2239   GList *list;
2240   gint physical;
2241   
2242   g_assert (logical >= 0);
2243   
2244   physical = 0;
2245   for (list = priv->content; list; list = list->next)
2246     {
2247       ToolbarContent *content = list->data;
2248       
2249       if (!toolbar_content_is_placeholder (content))
2250         {
2251           if (logical == 0)
2252             break;
2253           logical--;
2254         }
2255       
2256       physical++;
2257     }
2258   
2259   g_assert (logical == 0);
2260   
2261   return physical;
2262 }
2263
2264 /**
2265  * gtk_toolbar_set_drop_highlight_item:
2266  * @toolbar: a #GtkToolbar
2267  * @tool_item: a #GtkToolItem, or %NULL to turn of highlighting
2268  * @index_: a position on @toolbar
2269  * 
2270  * Highlights @toolbar to give an idea of what it would look like
2271  * if @item was added to @toolbar at the position indicated by @index_. 
2272  * If @item is %NULL, highlighting is turned off. In that case @index_ 
2273  * is ignored.
2274  *
2275  * The @tool_item passed to this function must not be part of any widget
2276  * hierarchy. When an item is set as drop highlight item it can not
2277  * added to any widget hierarchy or used as highlight item for another
2278  * toolbar.
2279  * 
2280  * Since: 2.4
2281  **/
2282 void
2283 gtk_toolbar_set_drop_highlight_item (GtkToolbar  *toolbar,
2284                                      GtkToolItem *tool_item,
2285                                      gint         index_)
2286 {
2287   ToolbarContent *content;
2288   GtkToolbarPrivate *priv;
2289   gint n_items;
2290   GtkRequisition requisition;
2291   GtkRequisition old_requisition;
2292   gboolean restart_sliding;
2293   
2294   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2295   g_return_if_fail (tool_item == NULL || GTK_IS_TOOL_ITEM (tool_item));
2296   
2297   gtk_toolbar_check_new_api (toolbar);
2298   
2299   priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2300   
2301   if (!tool_item)
2302     {
2303       if (priv->highlight_tool_item)
2304         {
2305           gtk_widget_unparent (GTK_WIDGET (priv->highlight_tool_item));
2306           g_object_unref (priv->highlight_tool_item);
2307           priv->highlight_tool_item = NULL;
2308         }
2309       
2310       reset_all_placeholders (toolbar);
2311       gtk_toolbar_begin_sliding (toolbar);
2312       return;
2313     }
2314   
2315   n_items = gtk_toolbar_get_n_items (toolbar);
2316   if (index_ < 0 || index_ > n_items)
2317     index_ = n_items;
2318   
2319   if (tool_item != priv->highlight_tool_item)
2320     {
2321       if (priv->highlight_tool_item)
2322         g_object_unref (priv->highlight_tool_item);
2323       
2324       g_object_ref_sink (tool_item);
2325       
2326       priv->highlight_tool_item = tool_item;
2327       
2328       gtk_widget_set_parent (GTK_WIDGET (priv->highlight_tool_item),
2329                              GTK_WIDGET (toolbar));
2330     }
2331   
2332   index_ = logical_to_physical (toolbar, index_);
2333   
2334   content = g_list_nth_data (priv->content, index_);
2335   
2336   if (index_ > 0)
2337     {
2338       ToolbarContent *prev_content;
2339       
2340       prev_content = g_list_nth_data (priv->content, index_ - 1);
2341       
2342       if (prev_content && toolbar_content_is_placeholder (prev_content))
2343         content = prev_content;
2344     }
2345   
2346   if (!content || !toolbar_content_is_placeholder (content))
2347     {
2348       GtkWidget *placeholder;
2349       
2350       placeholder = GTK_WIDGET (gtk_separator_tool_item_new ());
2351
2352       content = toolbar_content_new_tool_item (toolbar,
2353                                                GTK_TOOL_ITEM (placeholder),
2354                                                TRUE, index_);
2355       gtk_widget_show (placeholder);
2356     }
2357   
2358   g_assert (content);
2359   g_assert (toolbar_content_is_placeholder (content));
2360   
2361   gtk_widget_size_request (GTK_WIDGET (priv->highlight_tool_item),
2362                            &requisition);
2363
2364   toolbar_content_set_expand (content, gtk_tool_item_get_expand (tool_item));
2365   
2366   restart_sliding = FALSE;
2367   toolbar_content_size_request (content, toolbar, &old_requisition);
2368   if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
2369     {
2370       requisition.height = -1;
2371       if (requisition.width != old_requisition.width)
2372         restart_sliding = TRUE;
2373     }
2374   else
2375     {
2376       requisition.width = -1;
2377       if (requisition.height != old_requisition.height)
2378         restart_sliding = TRUE;
2379     }
2380
2381   if (toolbar_content_disappearing (content))
2382     restart_sliding = TRUE;
2383   
2384   reset_all_placeholders (toolbar);
2385   toolbar_content_set_disappearing (content, FALSE);
2386   
2387   toolbar_content_set_size_request (content,
2388                                     requisition.width, requisition.height);
2389   
2390   if (restart_sliding)
2391     gtk_toolbar_begin_sliding (toolbar);
2392 }
2393
2394 static void
2395 gtk_toolbar_get_child_property (GtkContainer *container,
2396                                 GtkWidget    *child,
2397                                 guint         property_id,
2398                                 GValue       *value,
2399                                 GParamSpec   *pspec)
2400 {
2401   GtkToolItem *item = GTK_TOOL_ITEM (child);
2402   
2403   switch (property_id)
2404     {
2405     case CHILD_PROP_HOMOGENEOUS:
2406       g_value_set_boolean (value, gtk_tool_item_get_homogeneous (item));
2407       break;
2408       
2409     case CHILD_PROP_EXPAND:
2410       g_value_set_boolean (value, gtk_tool_item_get_expand (item));
2411       break;
2412       
2413     default:
2414       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
2415       break;
2416     }
2417 }
2418
2419 static void
2420 gtk_toolbar_set_child_property (GtkContainer *container,
2421                                 GtkWidget    *child,
2422                                 guint         property_id,
2423                                 const GValue *value,
2424                                 GParamSpec   *pspec)
2425 {
2426   switch (property_id)
2427     {
2428     case CHILD_PROP_HOMOGENEOUS:
2429       gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (child), g_value_get_boolean (value));
2430       break;
2431       
2432     case CHILD_PROP_EXPAND:
2433       gtk_tool_item_set_expand (GTK_TOOL_ITEM (child), g_value_get_boolean (value));
2434       break;
2435       
2436     default:
2437       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
2438       break;
2439     }
2440 }
2441
2442 static void
2443 gtk_toolbar_show_all (GtkWidget *widget)
2444 {
2445   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (widget);
2446   GList *list;
2447
2448   for (list = priv->content; list != NULL; list = list->next)
2449     {
2450       ToolbarContent *content = list->data;
2451       
2452       toolbar_content_show_all (content);
2453     }
2454   
2455   gtk_widget_show (widget);
2456 }
2457
2458 static void
2459 gtk_toolbar_hide_all (GtkWidget *widget)
2460 {
2461   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (widget);
2462   GList *list;
2463
2464   for (list = priv->content; list != NULL; list = list->next)
2465     {
2466       ToolbarContent *content = list->data;
2467       
2468       toolbar_content_hide_all (content);
2469     }
2470
2471   gtk_widget_hide (widget);
2472 }
2473
2474 static void
2475 gtk_toolbar_add (GtkContainer *container,
2476                  GtkWidget    *widget)
2477 {
2478   GtkToolbar *toolbar;
2479   
2480   g_return_if_fail (GTK_IS_TOOLBAR (container));
2481   g_return_if_fail (widget != NULL);
2482   
2483   toolbar = GTK_TOOLBAR (container);
2484   
2485   if (GTK_IS_TOOL_ITEM (widget))
2486     gtk_toolbar_insert (toolbar, GTK_TOOL_ITEM (widget), -1);
2487   else
2488     gtk_toolbar_append_widget (toolbar, widget, NULL, NULL);
2489 }
2490
2491 static void
2492 gtk_toolbar_remove (GtkContainer *container,
2493                     GtkWidget    *widget)
2494 {
2495   GtkToolbar *toolbar;
2496   GtkToolbarPrivate *priv;
2497   ToolbarContent *content_to_remove;
2498   GList *list;
2499   
2500   g_return_if_fail (GTK_IS_TOOLBAR (container));
2501   g_return_if_fail (GTK_IS_WIDGET (widget));
2502   
2503   toolbar = GTK_TOOLBAR (container);
2504   priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2505   
2506   content_to_remove = NULL;
2507   for (list = priv->content; list != NULL; list = list->next)
2508     {
2509       ToolbarContent *content = list->data;
2510       GtkWidget *child;
2511       
2512       child = toolbar_content_get_widget (content);
2513       if (child && child == widget)
2514         {
2515           content_to_remove = content;
2516           break;
2517         }
2518     }
2519   
2520   g_return_if_fail (content_to_remove != NULL);
2521   
2522   toolbar_content_remove (content_to_remove, toolbar);
2523   toolbar_content_free (content_to_remove);
2524 }
2525
2526 static void
2527 gtk_toolbar_forall (GtkContainer *container,
2528                     gboolean      include_internals,
2529                     GtkCallback   callback,
2530                     gpointer      callback_data)
2531 {
2532   GtkToolbar *toolbar = GTK_TOOLBAR (container);
2533   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2534   GList *list;
2535   
2536   g_return_if_fail (callback != NULL);
2537   
2538   list = priv->content;
2539   while (list)
2540     {
2541       ToolbarContent *content = list->data;
2542       GList *next = list->next;
2543       
2544       if (include_internals || !toolbar_content_is_placeholder (content))
2545         {
2546           GtkWidget *child = toolbar_content_get_widget (content);
2547           
2548           if (child)
2549             callback (child, callback_data);
2550         }
2551       
2552       list = next;
2553     }
2554   
2555   if (include_internals)
2556     callback (priv->arrow_button, callback_data);
2557 }
2558
2559 static GType
2560 gtk_toolbar_child_type (GtkContainer *container)
2561 {
2562   return GTK_TYPE_TOOL_ITEM;
2563 }
2564
2565 static void
2566 gtk_toolbar_reconfigured (GtkToolbar *toolbar)
2567 {
2568   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2569   GList *list;
2570   
2571   list = priv->content;
2572   while (list)
2573     {
2574       ToolbarContent *content = list->data;
2575       GList *next = list->next;
2576       
2577       toolbar_content_toolbar_reconfigured (content, toolbar);
2578       
2579       list = next;
2580     }
2581 }
2582
2583 static void
2584 gtk_toolbar_orientation_changed (GtkToolbar    *toolbar,
2585                                  GtkOrientation orientation)
2586 {
2587   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2588   if (toolbar->orientation != orientation)
2589     {
2590       toolbar->orientation = orientation;
2591       
2592       if (orientation == GTK_ORIENTATION_HORIZONTAL)
2593         gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
2594       else
2595         gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
2596       
2597       gtk_toolbar_reconfigured (toolbar);
2598       
2599       gtk_widget_queue_resize (GTK_WIDGET (toolbar));
2600       g_object_notify (G_OBJECT (toolbar), "orientation");
2601     }
2602 }
2603
2604 static void
2605 gtk_toolbar_real_style_changed (GtkToolbar     *toolbar,
2606                                 GtkToolbarStyle style)
2607 {
2608   if (toolbar->style != style)
2609     {
2610       toolbar->style = style;
2611       
2612       gtk_toolbar_reconfigured (toolbar);
2613       
2614       gtk_widget_queue_resize (GTK_WIDGET (toolbar));
2615       g_object_notify (G_OBJECT (toolbar), "toolbar-style");
2616     }
2617 }
2618
2619 static void
2620 menu_position_func (GtkMenu  *menu,
2621                     gint     *x,
2622                     gint     *y,
2623                     gboolean *push_in,
2624                     gpointer  user_data)
2625 {
2626   GtkToolbar *toolbar = GTK_TOOLBAR (user_data);
2627   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2628   GtkRequisition req;
2629   GtkRequisition menu_req;
2630   GdkRectangle monitor;
2631   gint monitor_num;
2632   GdkScreen *screen;
2633   
2634   gtk_widget_size_request (priv->arrow_button, &req);
2635   gtk_widget_size_request (GTK_WIDGET (menu), &menu_req);
2636   
2637   screen = gtk_widget_get_screen (GTK_WIDGET (menu));
2638   monitor_num = gdk_screen_get_monitor_at_window (screen, priv->arrow_button->window);
2639   if (monitor_num < 0)
2640     monitor_num = 0;
2641   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2642
2643   gdk_window_get_origin (GTK_BUTTON (priv->arrow_button)->event_window, x, y);
2644   if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
2645     {
2646       if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_LTR) 
2647         *x += priv->arrow_button->allocation.width - req.width;
2648       else 
2649         *x += req.width - menu_req.width;
2650
2651       if ((*y + priv->arrow_button->allocation.height + menu_req.height) <= monitor.y + monitor.height)
2652         *y += priv->arrow_button->allocation.height;
2653       else if ((*y - menu_req.height) >= monitor.y)
2654         *y -= menu_req.height;
2655       else if (monitor.y + monitor.height - (*y + priv->arrow_button->allocation.height) > *y)
2656         *y += priv->arrow_button->allocation.height;
2657       else
2658         *y -= menu_req.height;
2659     }
2660   else 
2661     {
2662       if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_LTR) 
2663         *x += priv->arrow_button->allocation.width;
2664       else 
2665         *x -= menu_req.width;
2666
2667       if (*y + menu_req.height > monitor.y + monitor.height &&
2668           *y + priv->arrow_button->allocation.height - monitor.y > monitor.y + monitor.height - *y)
2669         *y += priv->arrow_button->allocation.height - menu_req.height;
2670     }
2671
2672   *push_in = FALSE;
2673 }
2674
2675 static void
2676 show_menu (GtkToolbar     *toolbar,
2677            GdkEventButton *event)
2678 {
2679   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2680
2681   rebuild_menu (toolbar);
2682
2683   gtk_widget_show_all (GTK_WIDGET (priv->menu));
2684
2685   gtk_menu_popup (priv->menu, NULL, NULL,
2686                   menu_position_func, toolbar,
2687                   event? event->button : 0,
2688                   event? event->time : gtk_get_current_event_time());
2689 }
2690
2691 static void
2692 gtk_toolbar_arrow_button_clicked (GtkWidget  *button,
2693                                   GtkToolbar *toolbar)
2694 {
2695   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);  
2696   
2697   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->arrow_button)) &&
2698       (!priv->menu || !GTK_WIDGET_VISIBLE (priv->menu)))
2699     {
2700       /* We only get here when the button is clicked with the keyboard,
2701        * because mouse button presses result in the menu being shown so
2702        * that priv->menu would be non-NULL and visible.
2703        */
2704       show_menu (toolbar, NULL);
2705       gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
2706     }
2707 }
2708
2709 static gboolean
2710 gtk_toolbar_arrow_button_press (GtkWidget      *button,
2711                                 GdkEventButton *event,
2712                                 GtkToolbar     *toolbar)
2713 {
2714   show_menu (toolbar, event);
2715   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
2716   
2717   return TRUE;
2718 }
2719
2720 static gboolean
2721 gtk_toolbar_button_press (GtkWidget      *toolbar,
2722                           GdkEventButton *event)
2723 {
2724   if (event->button == 3)
2725     {
2726       gboolean return_value;
2727       
2728       g_signal_emit (toolbar, toolbar_signals[POPUP_CONTEXT_MENU], 0,
2729                      (int)event->x_root, (int)event->y_root, event->button,
2730                      &return_value);
2731       
2732       return return_value;
2733     }
2734   
2735   return FALSE;
2736 }
2737
2738 static gboolean
2739 gtk_toolbar_popup_menu (GtkWidget *toolbar)
2740 {
2741   gboolean return_value;
2742   /* This function is the handler for the "popup menu" keybinding,
2743    * ie., it is called when the user presses Shift F10
2744    */
2745   g_signal_emit (toolbar, toolbar_signals[POPUP_CONTEXT_MENU], 0,
2746                  -1, -1, -1, &return_value);
2747   
2748   return return_value;
2749 }
2750
2751 /**
2752  * gtk_toolbar_new:
2753  * 
2754  * Creates a new toolbar. 
2755  
2756  * Return Value: the newly-created toolbar.
2757  **/
2758 GtkWidget *
2759 gtk_toolbar_new (void)
2760 {
2761   GtkToolbar *toolbar;
2762   
2763   toolbar = g_object_new (GTK_TYPE_TOOLBAR, NULL);
2764   
2765   return GTK_WIDGET (toolbar);
2766 }
2767
2768 /**
2769  * gtk_toolbar_insert:
2770  * @toolbar: a #GtkToolbar
2771  * @item: a #GtkToolItem
2772  * @pos: the position of the new item
2773  *
2774  * Insert a #GtkToolItem into the toolbar at position @pos. If @pos is
2775  * 0 the item is prepended to the start of the toolbar. If @pos is
2776  * negative, the item is appended to the end of the toolbar.
2777  *
2778  * Since: 2.4
2779  **/
2780 void
2781 gtk_toolbar_insert (GtkToolbar  *toolbar,
2782                     GtkToolItem *item,
2783                     gint         pos)
2784 {
2785   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2786   g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2787   
2788   if (!gtk_toolbar_check_new_api (toolbar))
2789     return;
2790   
2791   if (pos >= 0)
2792     pos = logical_to_physical (toolbar, pos);
2793
2794   toolbar_content_new_tool_item (toolbar, item, FALSE, pos);
2795 }
2796
2797 /**
2798  * gtk_toolbar_get_item_index:
2799  * @toolbar: a #GtkToolbar
2800  * @item: a #GtkToolItem that is a child of @toolbar
2801  * 
2802  * Returns the position of @item on the toolbar, starting from 0.
2803  * It is an error if @item is not a child of the toolbar.
2804  * 
2805  * Return value: the position of item on the toolbar.
2806  * 
2807  * Since: 2.4
2808  **/
2809 gint
2810 gtk_toolbar_get_item_index (GtkToolbar  *toolbar,
2811                             GtkToolItem *item)
2812 {
2813   GtkToolbarPrivate *priv;
2814   GList *list;
2815   int n;
2816   
2817   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), -1);
2818   g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2819   g_return_val_if_fail (GTK_WIDGET (item)->parent == GTK_WIDGET (toolbar), -1);
2820   
2821   if (!gtk_toolbar_check_new_api (toolbar))
2822     return -1;
2823   
2824   priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2825   
2826   n = 0;
2827   for (list = priv->content; list != NULL; list = list->next)
2828     {
2829       ToolbarContent *content = list->data;
2830       GtkWidget *widget;
2831       
2832       widget = toolbar_content_get_widget (content);
2833       
2834       if (item == GTK_TOOL_ITEM (widget))
2835         break;
2836       
2837       ++n;
2838     }
2839   
2840   return physical_to_logical (toolbar, n);
2841 }
2842
2843 /**
2844  * gtk_toolbar_set_orientation:
2845  * @toolbar: a #GtkToolbar.
2846  * @orientation: a new #GtkOrientation.
2847  * 
2848  * Sets whether a toolbar should appear horizontally or vertically.
2849  **/
2850 void
2851 gtk_toolbar_set_orientation (GtkToolbar     *toolbar,
2852                              GtkOrientation  orientation)
2853 {
2854   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2855   
2856   g_signal_emit (toolbar, toolbar_signals[ORIENTATION_CHANGED], 0, orientation);
2857 }
2858
2859 /**
2860  * gtk_toolbar_get_orientation:
2861  * @toolbar: a #GtkToolbar
2862  * 
2863  * Retrieves the current orientation of the toolbar. See
2864  * gtk_toolbar_set_orientation().
2865  *
2866  * Return value: the orientation
2867  **/
2868 GtkOrientation
2869 gtk_toolbar_get_orientation (GtkToolbar *toolbar)
2870 {
2871   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), GTK_ORIENTATION_HORIZONTAL);
2872   
2873   return toolbar->orientation;
2874 }
2875
2876 /**
2877  * gtk_toolbar_set_style:
2878  * @toolbar: a #GtkToolbar.
2879  * @style: the new style for @toolbar.
2880  * 
2881  * Alters the view of @toolbar to display either icons only, text only, or both.
2882  **/
2883 void
2884 gtk_toolbar_set_style (GtkToolbar      *toolbar,
2885                        GtkToolbarStyle  style)
2886 {
2887   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2888   
2889   toolbar->style_set = TRUE;  
2890   g_signal_emit (toolbar, toolbar_signals[STYLE_CHANGED], 0, style);
2891 }
2892
2893 /**
2894  * gtk_toolbar_get_style:
2895  * @toolbar: a #GtkToolbar
2896  *
2897  * Retrieves whether the toolbar has text, icons, or both . See
2898  * gtk_toolbar_set_style().
2899  
2900  * Return value: the current style of @toolbar
2901  **/
2902 GtkToolbarStyle
2903 gtk_toolbar_get_style (GtkToolbar *toolbar)
2904 {
2905   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), DEFAULT_TOOLBAR_STYLE);
2906   
2907   return toolbar->style;
2908 }
2909
2910 /**
2911  * gtk_toolbar_unset_style:
2912  * @toolbar: a #GtkToolbar
2913  * 
2914  * Unsets a toolbar style set with gtk_toolbar_set_style(), so that
2915  * user preferences will be used to determine the toolbar style.
2916  **/
2917 void
2918 gtk_toolbar_unset_style (GtkToolbar *toolbar)
2919 {
2920   GtkToolbarStyle style;
2921   
2922   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2923   
2924   if (toolbar->style_set)
2925     {
2926       GtkSettings *settings = toolbar_get_settings (toolbar);
2927       
2928       if (settings)
2929         g_object_get (settings,
2930                       "gtk-toolbar-style", &style,
2931                       NULL);
2932       else
2933         style = DEFAULT_TOOLBAR_STYLE;
2934       
2935       if (style != toolbar->style)
2936         g_signal_emit (toolbar, toolbar_signals[STYLE_CHANGED], 0, style);
2937       
2938       toolbar->style_set = FALSE;
2939     }
2940 }
2941
2942 /**
2943  * gtk_toolbar_set_tooltips:
2944  * @toolbar: a #GtkToolbar.
2945  * @enable: set to %FALSE to disable the tooltips, or %TRUE to enable them.
2946  * 
2947  * Sets if the tooltips of a toolbar should be active or not.
2948  **/
2949 void
2950 gtk_toolbar_set_tooltips (GtkToolbar *toolbar,
2951                           gboolean    enable)
2952 {
2953   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2954   
2955   if (enable)
2956     gtk_tooltips_enable (toolbar->tooltips);
2957   else
2958     gtk_tooltips_disable (toolbar->tooltips);
2959
2960   g_object_notify (G_OBJECT (toolbar), "tooltips");
2961 }
2962
2963 /**
2964  * gtk_toolbar_get_tooltips:
2965  * @toolbar: a #GtkToolbar
2966  *
2967  * Retrieves whether tooltips are enabled. See
2968  * gtk_toolbar_set_tooltips().
2969  *
2970  * Return value: %TRUE if tooltips are enabled
2971  **/
2972 gboolean
2973 gtk_toolbar_get_tooltips (GtkToolbar *toolbar)
2974 {
2975   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
2976   
2977   return TRUE;
2978 }
2979
2980 /**
2981  * gtk_toolbar_get_n_items:
2982  * @toolbar: a #GtkToolbar
2983  * 
2984  * Returns the number of items on the toolbar.
2985  * 
2986  * Return value: the number of items on the toolbar
2987  * 
2988  * Since: 2.4
2989  **/
2990 gint
2991 gtk_toolbar_get_n_items (GtkToolbar *toolbar)
2992 {
2993   GtkToolbarPrivate *priv;
2994   
2995   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), -1);
2996   
2997   if (!gtk_toolbar_check_new_api (toolbar))
2998     return -1;
2999   
3000   priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
3001   
3002   return physical_to_logical (toolbar, g_list_length (priv->content));
3003 }
3004
3005 /**
3006  * gtk_toolbar_get_nth_item:
3007  * @toolbar: a #GtkToolbar
3008  * @n: A position on the toolbar
3009  *
3010  * Returns the @n<!-- -->'th item on @toolbar, or %NULL if the
3011  * toolbar does not contain an @n<!-- -->'th item.
3012  * 
3013  * Return value: The @n<!-- -->'th #GtkToolItem on @toolbar, or %NULL if there
3014  * isn't an @n<!-- -->'th item.
3015  * 
3016  * Since: 2.4
3017  **/
3018 GtkToolItem *
3019 gtk_toolbar_get_nth_item (GtkToolbar *toolbar,
3020                           gint        n)
3021 {
3022   GtkToolbarPrivate *priv;
3023   ToolbarContent *content;
3024   gint n_items;
3025   
3026   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), NULL);
3027   
3028   if (!gtk_toolbar_check_new_api (toolbar))
3029     return NULL;
3030   
3031   n_items = gtk_toolbar_get_n_items (toolbar);
3032   
3033   if (n < 0 || n >= n_items)
3034     return NULL;
3035   
3036   priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
3037   
3038   content = g_list_nth_data (priv->content, logical_to_physical (toolbar, n));
3039   
3040   g_assert (content);
3041   g_assert (!toolbar_content_is_placeholder (content));
3042   
3043   return GTK_TOOL_ITEM (toolbar_content_get_widget (content));
3044 }
3045
3046 /**
3047  * gtk_toolbar_get_icon_size:
3048  * @toolbar: a #GtkToolbar
3049  *
3050  * Retrieves the icon size for the toolbar. See gtk_toolbar_set_icon_size().
3051  *
3052  * Return value: the current icon size for the icons on the toolbar.
3053  **/
3054 GtkIconSize
3055 gtk_toolbar_get_icon_size (GtkToolbar *toolbar)
3056 {
3057   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), DEFAULT_ICON_SIZE);
3058   
3059   return toolbar->icon_size;
3060 }
3061
3062 /**
3063  * gtk_toolbar_get_relief_style:
3064  * @toolbar: a #GtkToolbar
3065  * 
3066  * Returns the relief style of buttons on @toolbar. See
3067  * gtk_button_set_relief().
3068  * 
3069  * Return value: The relief style of buttons on @toolbar.
3070  * 
3071  * Since: 2.4
3072  **/
3073 GtkReliefStyle
3074 gtk_toolbar_get_relief_style (GtkToolbar *toolbar)
3075 {
3076   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), GTK_RELIEF_NONE);
3077   
3078   return get_button_relief (toolbar);
3079 }
3080
3081 /**
3082  * gtk_toolbar_set_show_arrow:
3083  * @toolbar: a #GtkToolbar
3084  * @show_arrow: Whether to show an overflow menu
3085  * 
3086  * Sets whether to show an overflow menu when
3087  * @toolbar doesn't have room for all items on it. If %TRUE,
3088  * items that there are not room are available through an
3089  * overflow menu.
3090  * 
3091  * Since: 2.4
3092  **/
3093 void
3094 gtk_toolbar_set_show_arrow (GtkToolbar *toolbar,
3095                             gboolean    show_arrow)
3096 {
3097   GtkToolbarPrivate *priv;
3098   
3099   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3100   
3101   priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
3102   show_arrow = show_arrow != FALSE;
3103   
3104   if (priv->show_arrow != show_arrow)
3105     {
3106       priv->show_arrow = show_arrow;
3107       
3108       if (!priv->show_arrow)
3109         gtk_widget_hide (priv->arrow_button);
3110       
3111       gtk_widget_queue_resize (GTK_WIDGET (toolbar));      
3112       g_object_notify (G_OBJECT (toolbar), "show-arrow");
3113     }
3114 }
3115
3116 /**
3117  * gtk_toolbar_get_show_arrow:
3118  * @toolbar: a #GtkToolbar
3119  * 
3120  * Returns whether the toolbar has an overflow menu.
3121  * See gtk_toolbar_set_show_arrow().
3122  * 
3123  * Return value: %TRUE if the toolbar has an overflow menu.
3124  * 
3125  * Since: 2.4
3126  **/
3127 gboolean
3128 gtk_toolbar_get_show_arrow (GtkToolbar *toolbar)
3129 {
3130   GtkToolbarPrivate *priv;
3131   
3132   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
3133   
3134   if (!gtk_toolbar_check_new_api (toolbar))
3135     return FALSE;
3136   
3137   priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
3138   
3139   return priv->show_arrow;
3140 }
3141
3142 /**
3143  * gtk_toolbar_get_drop_index:
3144  * @toolbar: a #GtkToolbar
3145  * @x: x coordinate of a point on the toolbar
3146  * @y: y coordinate of a point on the toolbar
3147  *
3148  * Returns the position corresponding to the indicated point on
3149  * @toolbar. This is useful when dragging items to the toolbar:
3150  * this function returns the position a new item should be
3151  * inserted.
3152  *
3153  * @x and @y are in @toolbar coordinates.
3154  * 
3155  * Return value: The position corresponding to the point (@x, @y) on the toolbar.
3156  * 
3157  * Since: 2.4
3158  **/
3159 gint
3160 gtk_toolbar_get_drop_index (GtkToolbar *toolbar,
3161                             gint        x,
3162                             gint        y)
3163 {
3164   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), -1);
3165   
3166   if (!gtk_toolbar_check_new_api (toolbar))
3167     return -1;
3168   
3169   return physical_to_logical (toolbar, find_drop_index (toolbar, x, y));
3170 }
3171
3172 static void
3173 gtk_toolbar_finalize (GObject *object)
3174 {
3175   GList *list;
3176   GtkToolbar *toolbar = GTK_TOOLBAR (object);
3177   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
3178   
3179   if (toolbar->tooltips)
3180     g_object_unref (toolbar->tooltips);
3181   
3182   if (priv->arrow_button)
3183     gtk_widget_unparent (priv->arrow_button);
3184
3185   for (list = priv->content; list != NULL; list = list->next)
3186     {
3187       ToolbarContent *content = list->data;
3188
3189       toolbar_content_free (content);
3190     }
3191   
3192   g_list_free (priv->content);
3193   g_list_free (toolbar->children);
3194   
3195   g_timer_destroy (priv->timer);
3196   
3197   if (priv->menu)
3198     gtk_widget_destroy (GTK_WIDGET (priv->menu));
3199   
3200   if (priv->idle_id)
3201     g_source_remove (priv->idle_id);
3202
3203   G_OBJECT_CLASS (gtk_toolbar_parent_class)->finalize (object);
3204 }
3205
3206 /**
3207  * gtk_toolbar_set_icon_size:
3208  * @toolbar: A #GtkToolbar
3209  * @icon_size: The #GtkIconSize that stock icons in the toolbar shall have.
3210  *
3211  * This function sets the size of stock icons in the toolbar. You
3212  * can call it both before you add the icons and after they've been
3213  * added. The size you set will override user preferences for the default
3214  * icon size.
3215  * 
3216  * This should only be used for special-purpose toolbars, normal
3217  * application toolbars should respect the user preferences for the
3218  * size of icons.
3219  **/
3220 void
3221 gtk_toolbar_set_icon_size (GtkToolbar  *toolbar,
3222                            GtkIconSize  icon_size)
3223 {
3224   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3225   g_return_if_fail (icon_size != GTK_ICON_SIZE_INVALID);
3226   
3227   if (!toolbar->icon_size_set)
3228     {
3229       toolbar->icon_size_set = TRUE;  
3230       g_object_notify (G_OBJECT (toolbar), "icon-size-set");
3231     }
3232
3233   if (toolbar->icon_size == icon_size)
3234     return;
3235   
3236   toolbar->icon_size = icon_size;
3237   g_object_notify (G_OBJECT (toolbar), "icon-size");
3238   
3239   gtk_toolbar_reconfigured (toolbar);
3240   
3241   gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3242 }
3243
3244 /**
3245  * gtk_toolbar_unset_icon_size:
3246  * @toolbar: a #GtkToolbar
3247  * 
3248  * Unsets toolbar icon size set with gtk_toolbar_set_icon_size(), so that
3249  * user preferences will be used to determine the icon size.
3250  **/
3251 void
3252 gtk_toolbar_unset_icon_size (GtkToolbar *toolbar)
3253 {
3254   GtkIconSize size;
3255   
3256   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3257   
3258   if (toolbar->icon_size_set)
3259     {
3260       GtkSettings *settings = toolbar_get_settings (toolbar);
3261       
3262       if (settings)
3263         {
3264           g_object_get (settings,
3265                         "gtk-toolbar-icon-size", &size,
3266                         NULL);
3267         }
3268       else
3269         size = DEFAULT_ICON_SIZE;
3270       
3271       if (size != toolbar->icon_size)
3272         {
3273           gtk_toolbar_set_icon_size (toolbar, size);
3274           g_object_notify (G_OBJECT (toolbar), "icon-size");      
3275         }
3276       
3277       toolbar->icon_size_set = FALSE;
3278       g_object_notify (G_OBJECT (toolbar), "icon-size-set");      
3279     }
3280 }
3281
3282 /*
3283  * Deprecated API
3284  */
3285
3286 /**
3287  * gtk_toolbar_append_item:
3288  * @toolbar: a #GtkToolbar.
3289  * @text: give your toolbar button a label.
3290  * @tooltip_text: a string that appears when the user holds the mouse over this item.
3291  * @tooltip_private_text: use with #GtkTipsQuery.
3292  * @icon: a #GtkWidget that should be used as the button's icon.
3293  * @callback: the function to be executed when the button is pressed.
3294  * @user_data: a pointer to any data you wish to be passed to the callback.
3295  * 
3296  * Inserts a new item into the toolbar. You must specify the position
3297  * in the toolbar where it will be inserted.
3298  *
3299  * @callback must be a pointer to a function taking a #GtkWidget and a gpointer as
3300  * arguments. Use the GTK_SIGNAL_FUNC() to cast the function to #GtkSignalFunc.
3301  *
3302  * Return value: the new toolbar item as a #GtkWidget.
3303  **/
3304 GtkWidget *
3305 gtk_toolbar_append_item (GtkToolbar    *toolbar,
3306                          const char    *text,
3307                          const char    *tooltip_text,
3308                          const char    *tooltip_private_text,
3309                          GtkWidget     *icon,
3310                          GtkSignalFunc  callback,
3311                          gpointer       user_data)
3312 {
3313   return gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_BUTTON,
3314                                      NULL, text,
3315                                      tooltip_text, tooltip_private_text,
3316                                      icon, callback, user_data,
3317                                      toolbar->num_children);
3318 }
3319
3320 /**
3321  * gtk_toolbar_prepend_item:
3322  * @toolbar: a #GtkToolbar.
3323  * @text: give your toolbar button a label.
3324  * @tooltip_text: a string that appears when the user holds the mouse over this item.
3325  * @tooltip_private_text: use with #GtkTipsQuery.
3326  * @icon: a #GtkWidget that should be used as the button's icon.
3327  * @callback: the function to be executed when the button is pressed.
3328  * @user_data: a pointer to any data you wish to be passed to the callback.
3329  * 
3330  * Adds a new button to the beginning (top or left edges) of the given toolbar.
3331  *
3332  * @callback must be a pointer to a function taking a #GtkWidget and a gpointer as
3333  * arguments. Use the GTK_SIGNAL_FUNC() to cast the function to #GtkSignalFunc.
3334  *
3335  * Return value: the new toolbar item as a #GtkWidget.
3336  **/
3337 GtkWidget *
3338 gtk_toolbar_prepend_item (GtkToolbar    *toolbar,
3339                           const char    *text,
3340                           const char    *tooltip_text,
3341                           const char    *tooltip_private_text,
3342                           GtkWidget     *icon,
3343                           GtkSignalFunc  callback,
3344                           gpointer       user_data)
3345 {
3346   return gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_BUTTON,
3347                                      NULL, text,
3348                                      tooltip_text, tooltip_private_text,
3349                                      icon, callback, user_data,
3350                                      0);
3351 }
3352
3353 /**
3354  * gtk_toolbar_insert_item:
3355  * @toolbar: a #GtkToolbar.
3356  * @text: give your toolbar button a label.
3357  * @tooltip_text: a string that appears when the user holds the mouse over this item.
3358  * @tooltip_private_text: use with #GtkTipsQuery.
3359  * @icon: a #GtkWidget that should be used as the button's icon.
3360  * @callback: the function to be executed when the button is pressed.
3361  * @user_data: a pointer to any data you wish to be passed to the callback.
3362  * @position: the number of widgets to insert this item after.
3363  * 
3364  * Inserts a new item into the toolbar. You must specify the position in the
3365  * toolbar where it will be inserted.
3366  *
3367  * @callback must be a pointer to a function taking a #GtkWidget and a gpointer as
3368  * arguments. Use the GTK_SIGNAL_FUNC() to cast the function to #GtkSignalFunc.
3369  *
3370  * Return value: the new toolbar item as a #GtkWidget.
3371  **/
3372 GtkWidget *
3373 gtk_toolbar_insert_item (GtkToolbar    *toolbar,
3374                          const char    *text,
3375                          const char    *tooltip_text,
3376                          const char    *tooltip_private_text,
3377                          GtkWidget     *icon,
3378                          GtkSignalFunc  callback,
3379                          gpointer       user_data,
3380                          gint           position)
3381 {
3382   return gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_BUTTON,
3383                                      NULL, text,
3384                                      tooltip_text, tooltip_private_text,
3385                                      icon, callback, user_data,
3386                                      position);
3387 }
3388
3389 /**
3390  * gtk_toolbar_insert_stock:
3391  * @toolbar: A #GtkToolbar
3392  * @stock_id: The id of the stock item you want to insert
3393  * @tooltip_text: The text in the tooltip of the toolbar button
3394  * @tooltip_private_text: The private text of the tooltip
3395  * @callback: The callback called when the toolbar button is clicked.
3396  * @user_data: user data passed to callback
3397  * @position: The position the button shall be inserted at.
3398  *            -1 means at the end.
3399  *
3400  * Inserts a stock item at the specified position of the toolbar.  If
3401  * @stock_id is not a known stock item ID, it's inserted verbatim,
3402  * except that underscores used to mark mnemonics are removed.
3403  *
3404  * @callback must be a pointer to a function taking a #GtkWidget and a gpointer as
3405  * arguments. Use the GTK_SIGNAL_FUNC() to cast the function to #GtkSignalFunc.
3406  *
3407  * Returns: the inserted widget
3408  */
3409 GtkWidget*
3410 gtk_toolbar_insert_stock (GtkToolbar      *toolbar,
3411                           const gchar     *stock_id,
3412                           const char      *tooltip_text,
3413                           const char      *tooltip_private_text,
3414                           GtkSignalFunc    callback,
3415                           gpointer         user_data,
3416                           gint             position)
3417 {
3418   return internal_insert_element (toolbar, GTK_TOOLBAR_CHILD_BUTTON,
3419                                   NULL, stock_id,
3420                                   tooltip_text, tooltip_private_text,
3421                                   NULL, callback, user_data,
3422                                   position, TRUE);
3423 }
3424
3425 /**
3426  * gtk_toolbar_append_space:
3427  * @toolbar: a #GtkToolbar.
3428  * 
3429  * Adds a new space to the end of the toolbar.
3430  **/
3431 void
3432 gtk_toolbar_append_space (GtkToolbar *toolbar)
3433 {
3434   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_SPACE,
3435                               NULL, NULL,
3436                               NULL, NULL,
3437                               NULL, NULL, NULL,
3438                               toolbar->num_children);
3439 }
3440
3441 /**
3442  * gtk_toolbar_prepend_space:
3443  * @toolbar: a #GtkToolbar.
3444  * 
3445  * Adds a new space to the beginning of the toolbar.
3446  **/
3447 void
3448 gtk_toolbar_prepend_space (GtkToolbar *toolbar)
3449 {
3450   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_SPACE,
3451                               NULL, NULL,
3452                               NULL, NULL,
3453                               NULL, NULL, NULL,
3454                               0);
3455 }
3456
3457 /**
3458  * gtk_toolbar_insert_space:
3459  * @toolbar: a #GtkToolbar
3460  * @position: the number of widgets after which a space should be inserted.
3461  * 
3462  * Inserts a new space in the toolbar at the specified position.
3463  **/
3464 void
3465 gtk_toolbar_insert_space (GtkToolbar *toolbar,
3466                           gint        position)
3467 {
3468   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_SPACE,
3469                               NULL, NULL,
3470                               NULL, NULL,
3471                               NULL, NULL, NULL,
3472                               position);
3473 }
3474
3475 /**
3476  * gtk_toolbar_remove_space:
3477  * @toolbar: a #GtkToolbar.
3478  * @position: the index of the space to remove.
3479  * 
3480  * Removes a space from the specified position.
3481  **/
3482 void
3483 gtk_toolbar_remove_space (GtkToolbar *toolbar,
3484                           gint        position)
3485 {
3486   GtkToolbarPrivate *priv;
3487   ToolbarContent *content;
3488   
3489   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3490   
3491   if (!gtk_toolbar_check_old_api (toolbar))
3492     return;
3493   
3494   priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
3495   
3496   content = g_list_nth_data (priv->content, position);
3497   
3498   if (!content)
3499     {
3500       g_warning ("Toolbar position %d doesn't exist", position);
3501       return;
3502     }
3503   
3504   if (!toolbar_content_is_separator (content))
3505     {
3506       g_warning ("Toolbar position %d is not a space", position);
3507       return;
3508     }
3509   
3510   toolbar_content_remove (content, toolbar);
3511   toolbar_content_free (content);
3512 }
3513
3514 /**
3515  * gtk_toolbar_append_widget:
3516  * @toolbar: a #GtkToolbar.
3517  * @widget: a #GtkWidget to add to the toolbar. 
3518  * @tooltip_text: the element's tooltip.
3519  * @tooltip_private_text: used for context-sensitive help about this toolbar element.
3520  * 
3521  * Adds a widget to the end of the given toolbar.
3522  **/ 
3523 void
3524 gtk_toolbar_append_widget (GtkToolbar  *toolbar,
3525                            GtkWidget   *widget,
3526                            const gchar *tooltip_text,
3527                            const gchar *tooltip_private_text)
3528 {
3529   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_WIDGET,
3530                               widget, NULL,
3531                               tooltip_text, tooltip_private_text,
3532                               NULL, NULL, NULL,
3533                               toolbar->num_children);
3534 }
3535
3536 /**
3537  * gtk_toolbar_prepend_widget:
3538  * @toolbar: a #GtkToolbar.
3539  * @widget: a #GtkWidget to add to the toolbar. 
3540  * @tooltip_text: the element's tooltip.
3541  * @tooltip_private_text: used for context-sensitive help about this toolbar element.
3542  * 
3543  * Adds a widget to the beginning of the given toolbar.
3544  **/ 
3545 void
3546 gtk_toolbar_prepend_widget (GtkToolbar  *toolbar,
3547                             GtkWidget   *widget,
3548                             const gchar *tooltip_text,
3549                             const gchar *tooltip_private_text)
3550 {
3551   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_WIDGET,
3552                               widget, NULL,
3553                               tooltip_text, tooltip_private_text,
3554                               NULL, NULL, NULL,
3555                               0);
3556 }
3557
3558 /**
3559  * gtk_toolbar_insert_widget:
3560  * @toolbar: a #GtkToolbar.
3561  * @widget: a #GtkWidget to add to the toolbar. 
3562  * @tooltip_text: the element's tooltip.
3563  * @tooltip_private_text: used for context-sensitive help about this toolbar element.
3564  * @position: the number of widgets to insert this widget after.
3565  * 
3566  * Inserts a widget in the toolbar at the given position.
3567  **/ 
3568 void
3569 gtk_toolbar_insert_widget (GtkToolbar *toolbar,
3570                            GtkWidget  *widget,
3571                            const char *tooltip_text,
3572                            const char *tooltip_private_text,
3573                            gint        position)
3574 {
3575   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_WIDGET,
3576                               widget, NULL,
3577                               tooltip_text, tooltip_private_text,
3578                               NULL, NULL, NULL,
3579                               position);
3580 }
3581
3582 /**
3583  * gtk_toolbar_append_element:
3584  * @toolbar: a #GtkToolbar.
3585  * @type: a value of type #GtkToolbarChildType that determines what @widget will be.
3586  * @widget: a #GtkWidget, or %NULL.
3587  * @text: the element's label.
3588  * @tooltip_text: the element's tooltip.
3589  * @tooltip_private_text: used for context-sensitive help about this toolbar element.
3590  * @icon: a #GtkWidget that provides pictorial representation of the element's function.
3591  * @callback: the function to be executed when the button is pressed.
3592  * @user_data: any data you wish to pass to the callback.
3593  * 
3594  * Adds a new element to the end of a toolbar.
3595  * 
3596  * If @type == %GTK_TOOLBAR_CHILD_WIDGET, @widget is used as the new element.
3597  * If @type == %GTK_TOOLBAR_CHILD_RADIOBUTTON, @widget is used to determine
3598  * the radio group for the new element. In all other cases, @widget must
3599  * be %NULL.
3600  * 
3601  * @callback must be a pointer to a function taking a #GtkWidget and a gpointer as
3602  * arguments. Use the GTK_SIGNAL_FUNC() to cast the function to #GtkSignalFunc.
3603  *
3604  * Return value: the new toolbar element as a #GtkWidget.
3605  **/
3606 GtkWidget*
3607 gtk_toolbar_append_element (GtkToolbar          *toolbar,
3608                             GtkToolbarChildType  type,
3609                             GtkWidget           *widget,
3610                             const char          *text,
3611                             const char          *tooltip_text,
3612                             const char          *tooltip_private_text,
3613                             GtkWidget           *icon,
3614                             GtkSignalFunc        callback,
3615                             gpointer             user_data)
3616 {
3617   return gtk_toolbar_insert_element (toolbar, type, widget, text,
3618                                      tooltip_text, tooltip_private_text,
3619                                      icon, callback, user_data,
3620                                      toolbar->num_children);
3621 }
3622
3623 /**
3624  * gtk_toolbar_prepend_element:
3625  * @toolbar: a #GtkToolbar.
3626  * @type: a value of type #GtkToolbarChildType that determines what @widget will be.
3627  * @widget: a #GtkWidget, or %NULL
3628  * @text: the element's label.
3629  * @tooltip_text: the element's tooltip.
3630  * @tooltip_private_text: used for context-sensitive help about this toolbar element.
3631  * @icon: a #GtkWidget that provides pictorial representation of the element's function.
3632  * @callback: the function to be executed when the button is pressed.
3633  * @user_data: any data you wish to pass to the callback.
3634  *  
3635  * Adds a new element to the beginning of a toolbar.
3636  * 
3637  * If @type == %GTK_TOOLBAR_CHILD_WIDGET, @widget is used as the new element.
3638  * If @type == %GTK_TOOLBAR_CHILD_RADIOBUTTON, @widget is used to determine
3639  * the radio group for the new element. In all other cases, @widget must
3640  * be %NULL.
3641  * 
3642  * @callback must be a pointer to a function taking a #GtkWidget and a gpointer as
3643  * arguments. Use the GTK_SIGNAL_FUNC() to cast the function to #GtkSignalFunc.
3644  *
3645  * Return value: the new toolbar element as a #GtkWidget.
3646  **/
3647 GtkWidget *
3648 gtk_toolbar_prepend_element (GtkToolbar          *toolbar,
3649                              GtkToolbarChildType  type,
3650                              GtkWidget           *widget,
3651                              const char          *text,
3652                              const char          *tooltip_text,
3653                              const char          *tooltip_private_text,
3654                              GtkWidget           *icon,
3655                              GtkSignalFunc        callback,
3656                              gpointer             user_data)
3657 {
3658   return gtk_toolbar_insert_element (toolbar, type, widget, text,
3659                                      tooltip_text, tooltip_private_text,
3660                                      icon, callback, user_data, 0);
3661 }
3662
3663 /**
3664  * gtk_toolbar_insert_element:
3665  * @toolbar: a #GtkToolbar.
3666  * @type: a value of type #GtkToolbarChildType that determines what @widget
3667  *   will be.
3668  * @widget: a #GtkWidget, or %NULL. 
3669  * @text: the element's label.
3670  * @tooltip_text: the element's tooltip.
3671  * @tooltip_private_text: used for context-sensitive help about this toolbar element.
3672  * @icon: a #GtkWidget that provides pictorial representation of the element's function.
3673  * @callback: the function to be executed when the button is pressed.
3674  * @user_data: any data you wish to pass to the callback.
3675  * @position: the number of widgets to insert this element after.
3676  *
3677  * Inserts a new element in the toolbar at the given position. 
3678  *
3679  * If @type == %GTK_TOOLBAR_CHILD_WIDGET, @widget is used as the new element.
3680  * If @type == %GTK_TOOLBAR_CHILD_RADIOBUTTON, @widget is used to determine
3681  * the radio group for the new element. In all other cases, @widget must
3682  * be %NULL.
3683  *
3684  * @callback must be a pointer to a function taking a #GtkWidget and a gpointer as
3685  * arguments. Use the GTK_SIGNAL_FUNC() to cast the function to #GtkSignalFunc.
3686  *
3687  * Return value: the new toolbar element as a #GtkWidget.
3688  **/
3689 GtkWidget *
3690 gtk_toolbar_insert_element (GtkToolbar          *toolbar,
3691                             GtkToolbarChildType  type,
3692                             GtkWidget           *widget,
3693                             const char          *text,
3694                             const char          *tooltip_text,
3695                             const char          *tooltip_private_text,
3696                             GtkWidget           *icon,
3697                             GtkSignalFunc        callback,
3698                             gpointer             user_data,
3699                             gint                 position)
3700 {
3701   return internal_insert_element (toolbar, type, widget, text,
3702                                   tooltip_text, tooltip_private_text,
3703                                   icon, callback, user_data, position, FALSE);
3704 }
3705
3706 static void
3707 set_child_packing_and_visibility(GtkToolbar      *toolbar,
3708                                  GtkToolbarChild *child)
3709 {
3710   GtkWidget *box;
3711   gboolean   expand;
3712
3713   box = gtk_bin_get_child (GTK_BIN (child->widget));
3714   
3715   g_return_if_fail (GTK_IS_BOX (box));
3716   
3717   if (child->label)
3718     {
3719       expand = (toolbar->style != GTK_TOOLBAR_BOTH);
3720       
3721       gtk_box_set_child_packing (GTK_BOX (box), child->label,
3722                                  expand, expand, 0, GTK_PACK_END);
3723       
3724       if (toolbar->style != GTK_TOOLBAR_ICONS)
3725         gtk_widget_show (child->label);
3726       else
3727         gtk_widget_hide (child->label);
3728     }
3729   
3730   if (child->icon)
3731     {
3732       expand = (toolbar->style != GTK_TOOLBAR_BOTH_HORIZ);
3733       
3734       gtk_box_set_child_packing (GTK_BOX (box), child->icon,
3735                                  expand, expand, 0, GTK_PACK_END);
3736       
3737       if (toolbar->style != GTK_TOOLBAR_TEXT)
3738         gtk_widget_show (child->icon);
3739       else
3740         gtk_widget_hide (child->icon);
3741     }
3742 }
3743
3744 static GtkWidget *
3745 internal_insert_element (GtkToolbar          *toolbar,
3746                          GtkToolbarChildType  type,
3747                          GtkWidget           *widget,
3748                          const char          *text,
3749                          const char          *tooltip_text,
3750                          const char          *tooltip_private_text,
3751                          GtkWidget           *icon,
3752                          GtkSignalFunc        callback,
3753                          gpointer             user_data,
3754                          gint                 position,
3755                          gboolean             use_stock)
3756 {
3757   GtkWidget *box;
3758   ToolbarContent *content;
3759   char *free_me = NULL;
3760
3761   GtkWidget *child_widget;
3762   GtkWidget *child_label;
3763   GtkWidget *child_icon;
3764
3765   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), NULL);
3766   if (type == GTK_TOOLBAR_CHILD_WIDGET)
3767     g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
3768   else if (type != GTK_TOOLBAR_CHILD_RADIOBUTTON)
3769     g_return_val_if_fail (widget == NULL, NULL);
3770   if (GTK_IS_TOOL_ITEM (widget))
3771     g_warning (MIXED_API_WARNING);
3772   
3773   if (!gtk_toolbar_check_old_api (toolbar))
3774     return NULL;
3775   
3776   child_widget = NULL;
3777   child_label = NULL;
3778   child_icon = NULL;
3779   
3780   switch (type)
3781     {
3782     case GTK_TOOLBAR_CHILD_SPACE:
3783       break;
3784       
3785     case GTK_TOOLBAR_CHILD_WIDGET:
3786       child_widget = widget;
3787       break;
3788       
3789     case GTK_TOOLBAR_CHILD_BUTTON:
3790     case GTK_TOOLBAR_CHILD_TOGGLEBUTTON:
3791     case GTK_TOOLBAR_CHILD_RADIOBUTTON:
3792       if (type == GTK_TOOLBAR_CHILD_BUTTON)
3793         {
3794           child_widget = gtk_button_new ();
3795         }
3796       else if (type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON)
3797         {
3798           child_widget = gtk_toggle_button_new ();
3799           gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (child_widget), FALSE);
3800         }
3801       else /* type == GTK_TOOLBAR_CHILD_RADIOBUTTON */
3802         {
3803           GSList *group = NULL;
3804
3805           if (widget)
3806             group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (widget));
3807           
3808           child_widget = gtk_radio_button_new (group);
3809           gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (child_widget), FALSE);
3810         }
3811
3812       gtk_button_set_relief (GTK_BUTTON (child_widget), get_button_relief (toolbar));
3813       gtk_button_set_focus_on_click (GTK_BUTTON (child_widget), FALSE);
3814       
3815       if (callback)
3816         {
3817           g_signal_connect (child_widget, "clicked",
3818                             callback, user_data);
3819         }
3820       
3821       if (toolbar->style == GTK_TOOLBAR_BOTH_HORIZ)
3822         box = gtk_hbox_new (FALSE, 0);
3823       else
3824         box = gtk_vbox_new (FALSE, 0);
3825
3826       gtk_container_add (GTK_CONTAINER (child_widget), box);
3827       gtk_widget_show (box);
3828       
3829       if (text && use_stock)
3830         {
3831           GtkStockItem stock_item;
3832           if (gtk_stock_lookup (text, &stock_item))
3833             {
3834               if (!icon)
3835                 icon = gtk_image_new_from_stock (text, toolbar->icon_size);
3836           
3837               text = free_me = _gtk_toolbar_elide_underscores (stock_item.label);
3838             }
3839         }
3840       
3841       if (text)
3842         {
3843           child_label = gtk_label_new (text);
3844           
3845           gtk_container_add (GTK_CONTAINER (box), child_label);
3846         }
3847       
3848       if (icon)
3849         {
3850           child_icon = GTK_WIDGET (icon);
3851           gtk_container_add (GTK_CONTAINER (box), child_icon);
3852         }
3853       
3854       gtk_widget_show (child_widget);
3855       break;
3856       
3857     default:
3858       g_assert_not_reached ();
3859       break;
3860     }
3861   
3862   if ((type != GTK_TOOLBAR_CHILD_SPACE) && tooltip_text)
3863     {
3864       gtk_tooltips_set_tip (toolbar->tooltips, child_widget,
3865                             tooltip_text, tooltip_private_text);
3866     }
3867   
3868   content = toolbar_content_new_compatibility (toolbar, type, child_widget,
3869                                                child_icon, child_label, position);
3870   
3871   g_free (free_me);
3872   
3873   return child_widget;
3874 }
3875
3876 /*
3877  * ToolbarContent methods
3878  */
3879 typedef enum {
3880   UNKNOWN,
3881   YES,
3882   NO
3883 } TriState;
3884
3885 struct _ToolbarContent
3886 {
3887   ContentType   type;
3888   ItemState     state;
3889   
3890   union
3891   {
3892     struct
3893     {
3894       GtkToolItem *     item;
3895       GtkAllocation     start_allocation;
3896       GtkAllocation     goal_allocation;
3897       guint             is_placeholder : 1;
3898       guint             disappearing : 1;
3899       guint             has_menu : 2;
3900     } tool_item;
3901     
3902     struct
3903     {
3904       GtkToolbarChild   child;
3905       GtkAllocation     space_allocation;
3906       guint             space_visible : 1;
3907     } compatibility;
3908   } u;
3909 };
3910
3911 static ToolbarContent *
3912 toolbar_content_new_tool_item (GtkToolbar  *toolbar,
3913                                GtkToolItem *item,
3914                                gboolean     is_placeholder,
3915                                gint         pos)
3916 {
3917   ToolbarContent *content;
3918   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
3919   
3920   content = g_slice_new0 (ToolbarContent);
3921   
3922   content->type = TOOL_ITEM;
3923   content->state = NOT_ALLOCATED;
3924   content->u.tool_item.item = item;
3925   content->u.tool_item.is_placeholder = is_placeholder;
3926   
3927   gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (toolbar));
3928
3929   priv->content = g_list_insert (priv->content, content, pos);
3930   
3931   if (!is_placeholder)
3932     {
3933       toolbar->num_children++;
3934
3935       gtk_toolbar_stop_sliding (toolbar);
3936     }
3937
3938   gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3939   priv->need_rebuild = TRUE;
3940   
3941   return content;
3942 }
3943
3944 static ToolbarContent *
3945 toolbar_content_new_compatibility (GtkToolbar          *toolbar,
3946                                    GtkToolbarChildType  type,
3947                                    GtkWidget            *widget,
3948                                    GtkWidget            *icon,
3949                                    GtkWidget            *label,
3950                                    gint                  pos)
3951 {
3952   ToolbarContent *content;
3953   GtkToolbarChild *child;
3954   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
3955   
3956   content = g_slice_new0 (ToolbarContent);
3957
3958   child = &(content->u.compatibility.child);
3959   
3960   content->type = COMPATIBILITY;
3961   child->type = type;
3962   child->widget = widget;
3963   child->icon = icon;
3964   child->label = label;
3965   
3966   if (type != GTK_TOOLBAR_CHILD_SPACE)
3967     {
3968       gtk_widget_set_parent (child->widget, GTK_WIDGET (toolbar));
3969     }
3970   else
3971     {
3972       content->u.compatibility.space_visible = TRUE;
3973       gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3974     }
3975  
3976   if (type == GTK_TOOLBAR_CHILD_BUTTON ||
3977       type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON ||
3978       type == GTK_TOOLBAR_CHILD_RADIOBUTTON)
3979     {
3980       set_child_packing_and_visibility (toolbar, child);
3981     }
3982
3983   priv->content = g_list_insert (priv->content, content, pos);
3984   toolbar->children = g_list_insert (toolbar->children, child, pos);
3985   priv->need_rebuild = TRUE;
3986   
3987   toolbar->num_children++;
3988   
3989   return content;
3990 }
3991
3992 static void
3993 toolbar_content_remove (ToolbarContent *content,
3994                         GtkToolbar     *toolbar)
3995 {
3996   GtkToolbarChild *child;
3997   GtkToolbarPrivate *priv;
3998
3999   priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
4000   
4001   switch (content->type)
4002     {
4003     case TOOL_ITEM:
4004       gtk_widget_unparent (GTK_WIDGET (content->u.tool_item.item));
4005       break;
4006       
4007     case COMPATIBILITY:
4008       child = &(content->u.compatibility.child);
4009       
4010       if (child->type != GTK_TOOLBAR_CHILD_SPACE)
4011         {
4012           g_object_ref (child->widget);
4013           gtk_widget_unparent (child->widget);
4014           gtk_widget_destroy (child->widget);
4015           g_object_unref (child->widget);
4016         }
4017       
4018       toolbar->children = g_list_remove (toolbar->children, child);
4019       break;
4020     }
4021
4022   priv->content = g_list_remove (priv->content, content);
4023
4024   if (!toolbar_content_is_placeholder (content))
4025     toolbar->num_children--;
4026
4027   gtk_widget_queue_resize (GTK_WIDGET (toolbar));
4028   priv->need_rebuild = TRUE;
4029 }
4030
4031 static void
4032 toolbar_content_free (ToolbarContent *content)
4033 {
4034   g_slice_free (ToolbarContent, content);
4035 }
4036
4037 static gint
4038 calculate_max_homogeneous_pixels (GtkWidget *widget)
4039 {
4040   PangoContext *context;
4041   PangoFontMetrics *metrics;
4042   gint char_width;
4043   
4044   context = gtk_widget_get_pango_context (widget);
4045   metrics = pango_context_get_metrics (context,
4046                                        widget->style->font_desc,
4047                                        pango_context_get_language (context));
4048   char_width = pango_font_metrics_get_approximate_char_width (metrics);
4049   pango_font_metrics_unref (metrics);
4050   
4051   return PANGO_PIXELS (MAX_HOMOGENEOUS_N_CHARS * char_width);
4052 }
4053
4054 static void
4055 toolbar_content_expose (ToolbarContent *content,
4056                         GtkContainer   *container,
4057                         GdkEventExpose *expose)
4058 {
4059   GtkToolbar *toolbar = GTK_TOOLBAR (container);
4060   GtkToolbarChild *child;
4061   GtkWidget *widget = NULL; /* quiet gcc */
4062   
4063   switch (content->type)
4064     {
4065     case TOOL_ITEM:
4066       if (!content->u.tool_item.is_placeholder)
4067         widget = GTK_WIDGET (content->u.tool_item.item);
4068       break;
4069       
4070     case COMPATIBILITY:
4071       child = &(content->u.compatibility.child);
4072       
4073       if (child->type == GTK_TOOLBAR_CHILD_SPACE)
4074         {
4075           if (content->u.compatibility.space_visible &&
4076               get_space_style (toolbar) == GTK_TOOLBAR_SPACE_LINE)
4077              _gtk_toolbar_paint_space_line (GTK_WIDGET (toolbar), toolbar,
4078                                             &expose->area,
4079                                             &content->u.compatibility.space_allocation);
4080           return;
4081         }
4082       
4083       widget = child->widget;
4084       break;
4085     }
4086   
4087   if (widget)
4088     gtk_container_propagate_expose (container, widget, expose);
4089 }
4090
4091 static gboolean
4092 toolbar_content_visible (ToolbarContent *content,
4093                          GtkToolbar     *toolbar)
4094 {
4095   GtkToolItem *item;
4096   
4097   switch (content->type)
4098     {
4099     case TOOL_ITEM:
4100       item = content->u.tool_item.item;
4101       
4102       if (!GTK_WIDGET_VISIBLE (item))
4103         return FALSE;
4104       
4105       if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL &&
4106           gtk_tool_item_get_visible_horizontal (item))
4107         return TRUE;
4108       
4109       if ((toolbar->orientation == GTK_ORIENTATION_VERTICAL &&
4110            gtk_tool_item_get_visible_vertical (item)))
4111         return TRUE;
4112       
4113       return FALSE;
4114       break;
4115       
4116     case COMPATIBILITY:
4117       if (content->u.compatibility.child.type != GTK_TOOLBAR_CHILD_SPACE)
4118         return GTK_WIDGET_VISIBLE (content->u.compatibility.child.widget);
4119       else
4120         return TRUE;
4121       break;
4122     }
4123   
4124   g_assert_not_reached ();
4125   return FALSE;
4126 }
4127
4128 static void
4129 toolbar_content_size_request (ToolbarContent *content,
4130                               GtkToolbar     *toolbar,
4131                               GtkRequisition *requisition)
4132 {
4133   gint space_size;
4134   
4135   switch (content->type)
4136     {
4137     case TOOL_ITEM:
4138       gtk_widget_size_request (GTK_WIDGET (content->u.tool_item.item),
4139                                requisition);
4140       if (content->u.tool_item.is_placeholder &&
4141           content->u.tool_item.disappearing)
4142         {
4143           requisition->width = 0;
4144           requisition->height = 0;
4145         }
4146       break;
4147       
4148     case COMPATIBILITY:
4149       space_size = get_space_size (toolbar);
4150       
4151       if (content->u.compatibility.child.type != GTK_TOOLBAR_CHILD_SPACE)
4152         {
4153           gtk_widget_size_request (content->u.compatibility.child.widget,
4154                                    requisition);
4155         }
4156       else
4157         {
4158           if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
4159             {
4160               requisition->width = space_size;
4161               requisition->height = 0;
4162             }
4163           else
4164             {
4165               requisition->height = space_size;
4166               requisition->width = 0;
4167             }
4168         }
4169       
4170       break;
4171     }
4172 }
4173
4174 static gboolean
4175 toolbar_content_is_homogeneous (ToolbarContent *content,
4176                                 GtkToolbar     *toolbar)
4177 {
4178   gboolean result = FALSE;      /* quiet gcc */
4179   GtkRequisition requisition;
4180   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
4181   
4182   if (priv->max_homogeneous_pixels < 0)
4183     {
4184       priv->max_homogeneous_pixels =
4185         calculate_max_homogeneous_pixels (GTK_WIDGET (toolbar));
4186     }
4187   
4188   toolbar_content_size_request (content, toolbar, &requisition);
4189   
4190   if (requisition.width > priv->max_homogeneous_pixels)
4191     return FALSE;
4192   
4193   switch (content->type)
4194     {
4195     case TOOL_ITEM:
4196       result = gtk_tool_item_get_homogeneous (content->u.tool_item.item) &&
4197         !GTK_IS_SEPARATOR_TOOL_ITEM (content->u.tool_item.item);
4198       
4199       if (gtk_tool_item_get_is_important (content->u.tool_item.item) &&
4200           toolbar->style == GTK_TOOLBAR_BOTH_HORIZ &&
4201           toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
4202         {
4203           result = FALSE;
4204         }
4205       break;
4206       
4207     case COMPATIBILITY:
4208       if (content->u.compatibility.child.type == GTK_TOOLBAR_CHILD_BUTTON ||
4209           content->u.compatibility.child.type == GTK_TOOLBAR_CHILD_RADIOBUTTON ||
4210           content->u.compatibility.child.type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON)
4211         {
4212           result = TRUE;
4213         }
4214       else
4215         {
4216           result = FALSE;
4217         }
4218       break;
4219     }
4220   
4221   return result;
4222 }
4223
4224 static gboolean
4225 toolbar_content_is_placeholder (ToolbarContent *content)
4226 {
4227   if (content->type == TOOL_ITEM && content->u.tool_item.is_placeholder)
4228     return TRUE;
4229   
4230   return FALSE;
4231 }
4232
4233 static gboolean
4234 toolbar_content_disappearing (ToolbarContent *content)
4235 {
4236   if (content->type == TOOL_ITEM && content->u.tool_item.disappearing)
4237     return TRUE;
4238   
4239   return FALSE;
4240 }
4241
4242 static ItemState
4243 toolbar_content_get_state (ToolbarContent *content)
4244 {
4245   return content->state;
4246 }
4247
4248 static gboolean
4249 toolbar_content_child_visible (ToolbarContent *content)
4250 {
4251   switch (content->type)
4252     {
4253     case TOOL_ITEM:
4254       return GTK_WIDGET_CHILD_VISIBLE (content->u.tool_item.item);
4255       break;
4256       
4257     case COMPATIBILITY:
4258       if (content->u.compatibility.child.type != GTK_TOOLBAR_CHILD_SPACE)
4259         {
4260           return GTK_WIDGET_CHILD_VISIBLE (content->u.compatibility.child.widget);
4261         }
4262       else
4263         {
4264           return content->u.compatibility.space_visible;
4265         }
4266       break;
4267     }
4268   
4269   return FALSE; /* quiet gcc */
4270 }
4271
4272 static void
4273 toolbar_content_get_goal_allocation (ToolbarContent *content,
4274                                      GtkAllocation  *allocation)
4275 {
4276   switch (content->type)
4277     {
4278     case TOOL_ITEM:
4279       *allocation = content->u.tool_item.goal_allocation;
4280       break;
4281       
4282     case COMPATIBILITY:
4283       /* Goal allocations are only relevant when we are
4284        * using the new API, so we should never get here
4285        */
4286       g_assert_not_reached ();
4287       break;
4288     }
4289 }
4290
4291 static void
4292 toolbar_content_get_allocation (ToolbarContent *content,
4293                                 GtkAllocation  *allocation)
4294 {
4295   GtkToolbarChild *child;
4296   
4297   switch (content->type)
4298     {
4299     case TOOL_ITEM:
4300       *allocation = GTK_WIDGET (content->u.tool_item.item)->allocation;
4301       break;
4302       
4303     case COMPATIBILITY:
4304       child = &(content->u.compatibility.child);
4305       
4306       if (child->type == GTK_TOOLBAR_CHILD_SPACE)
4307         *allocation = content->u.compatibility.space_allocation;
4308       else
4309         *allocation = child->widget->allocation;
4310       break;
4311     }
4312 }
4313
4314 static void
4315 toolbar_content_set_start_allocation (ToolbarContent *content,
4316                                       GtkAllocation  *allocation)
4317 {
4318   switch (content->type)
4319     {
4320     case TOOL_ITEM:
4321       content->u.tool_item.start_allocation = *allocation;
4322       break;
4323       
4324     case COMPATIBILITY:
4325       /* start_allocation is only relevant when using the new API */
4326       g_assert_not_reached ();
4327       break;
4328     }
4329 }
4330
4331 static gboolean
4332 toolbar_content_get_expand (ToolbarContent *content)
4333 {
4334   if (content->type == TOOL_ITEM &&
4335       gtk_tool_item_get_expand (content->u.tool_item.item) &&
4336       !content->u.tool_item.disappearing)
4337     {
4338       return TRUE;
4339     }
4340   
4341   return FALSE;
4342 }
4343
4344 static void
4345 toolbar_content_set_goal_allocation (ToolbarContent *content,
4346                                      GtkAllocation  *allocation)
4347 {
4348   switch (content->type)
4349     {
4350     case TOOL_ITEM:
4351       content->u.tool_item.goal_allocation = *allocation;
4352       break;
4353       
4354     case COMPATIBILITY:
4355       /* Only relevant when using new API */
4356       g_assert_not_reached ();
4357       break;
4358     }
4359 }
4360
4361 static void
4362 toolbar_content_set_child_visible (ToolbarContent *content,
4363                                    GtkToolbar     *toolbar,
4364                                    gboolean        visible)
4365 {
4366   GtkToolbarChild *child;
4367   
4368   switch (content->type)
4369     {
4370     case TOOL_ITEM:
4371       gtk_widget_set_child_visible (GTK_WIDGET (content->u.tool_item.item),
4372                                     visible);
4373       break;
4374       
4375     case COMPATIBILITY:
4376       child = &(content->u.compatibility.child);
4377       
4378       if (child->type != GTK_TOOLBAR_CHILD_SPACE)
4379         {
4380           gtk_widget_set_child_visible (child->widget, visible);
4381         }
4382       else
4383         {
4384           if (content->u.compatibility.space_visible != visible)
4385             {
4386               content->u.compatibility.space_visible = visible;
4387               gtk_widget_queue_draw (GTK_WIDGET (toolbar));
4388             }
4389         }
4390       break;
4391     }
4392 }
4393
4394 static void
4395 toolbar_content_get_start_allocation (ToolbarContent *content,
4396                                       GtkAllocation  *start_allocation)
4397 {
4398   switch (content->type)
4399     {
4400     case TOOL_ITEM:
4401       *start_allocation = content->u.tool_item.start_allocation;
4402       break;
4403       
4404     case COMPATIBILITY:
4405       /* Only relevant for new API */
4406       g_assert_not_reached ();
4407       break;
4408     }
4409 }
4410
4411 static void
4412 toolbar_content_size_allocate (ToolbarContent *content,
4413                                GtkAllocation  *allocation)
4414 {
4415   switch (content->type)
4416     {
4417     case TOOL_ITEM:
4418       gtk_widget_size_allocate (GTK_WIDGET (content->u.tool_item.item),
4419                                 allocation);
4420       break;
4421       
4422     case COMPATIBILITY:
4423       if (content->u.compatibility.child.type != GTK_TOOLBAR_CHILD_SPACE)
4424         {
4425           gtk_widget_size_allocate (content->u.compatibility.child.widget,
4426                                     allocation);
4427         }
4428       else
4429         {
4430           content->u.compatibility.space_allocation = *allocation;
4431         }
4432       break;
4433     }
4434 }
4435
4436 static void
4437 toolbar_content_set_state (ToolbarContent *content,
4438                            ItemState       state)
4439 {
4440   content->state = state;
4441 }
4442
4443 static GtkWidget *
4444 toolbar_content_get_widget (ToolbarContent *content)
4445 {
4446   GtkToolbarChild *child;
4447   
4448   switch (content->type)
4449     {
4450     case TOOL_ITEM:
4451       return GTK_WIDGET (content->u.tool_item.item);
4452       break;
4453       
4454     case COMPATIBILITY:
4455       child = &(content->u.compatibility.child);
4456       if (child->type != GTK_TOOLBAR_CHILD_SPACE)
4457         return child->widget;
4458       else
4459         return NULL;
4460       break;
4461     }
4462   
4463   return NULL;
4464 }
4465
4466 static void
4467 toolbar_content_set_disappearing (ToolbarContent *content,
4468                                   gboolean        disappearing)
4469 {
4470   switch (content->type)
4471     {
4472     case TOOL_ITEM:
4473       content->u.tool_item.disappearing = disappearing;
4474       break;
4475       
4476     case COMPATIBILITY:
4477       /* Only relevant for new API */
4478       g_assert_not_reached ();
4479       break;
4480     }
4481 }
4482
4483 static void
4484 toolbar_content_set_size_request (ToolbarContent *content,
4485                                   gint            width,
4486                                   gint            height)
4487 {
4488   switch (content->type)
4489     {
4490     case TOOL_ITEM:
4491       gtk_widget_set_size_request (GTK_WIDGET (content->u.tool_item.item),
4492                                    width, height);
4493       break;
4494       
4495     case COMPATIBILITY:
4496       /* Setting size requests only happens with sliding,
4497        * so not relevant here
4498        */
4499       g_assert_not_reached ();
4500       break;
4501     }
4502 }
4503
4504 static void
4505 toolbar_child_reconfigure (GtkToolbar      *toolbar,
4506                            GtkToolbarChild *child)
4507 {
4508   GtkWidget *box;
4509   GtkImage *image;
4510   GtkToolbarStyle style;
4511   GtkIconSize icon_size;
4512   GtkReliefStyle relief;
4513   gchar *stock_id;
4514   
4515   style = gtk_toolbar_get_style (toolbar);
4516   icon_size = gtk_toolbar_get_icon_size (toolbar);
4517   relief = gtk_toolbar_get_relief_style (toolbar);
4518   
4519   /* style */
4520   if (child->type == GTK_TOOLBAR_CHILD_BUTTON ||
4521       child->type == GTK_TOOLBAR_CHILD_RADIOBUTTON ||
4522       child->type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON)
4523     {
4524       box = gtk_bin_get_child (GTK_BIN (child->widget));
4525       
4526       if (style == GTK_TOOLBAR_BOTH && GTK_IS_HBOX (box))
4527         {
4528           GtkWidget *vbox;
4529           
4530           vbox = gtk_vbox_new (FALSE, 0);
4531           
4532           if (child->label)
4533             gtk_widget_reparent (child->label, vbox);
4534           if (child->icon)
4535             gtk_widget_reparent (child->icon, vbox);
4536           
4537           gtk_widget_destroy (box);
4538           gtk_container_add (GTK_CONTAINER (child->widget), vbox);
4539           
4540           gtk_widget_show (vbox);
4541         }
4542       else if (style == GTK_TOOLBAR_BOTH_HORIZ && GTK_IS_VBOX (box))
4543         {
4544           GtkWidget *hbox;
4545           
4546           hbox = gtk_hbox_new (FALSE, 0);
4547           
4548           if (child->label)
4549             gtk_widget_reparent (child->label, hbox);
4550           if (child->icon)
4551             gtk_widget_reparent (child->icon, hbox);
4552           
4553           gtk_widget_destroy (box);
4554           gtk_container_add (GTK_CONTAINER (child->widget), hbox);
4555           
4556           gtk_widget_show (hbox);
4557         }
4558
4559       set_child_packing_and_visibility (toolbar, child);
4560     }
4561   
4562   /* icon size */
4563   
4564   if ((child->type == GTK_TOOLBAR_CHILD_BUTTON ||
4565        child->type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON ||
4566        child->type == GTK_TOOLBAR_CHILD_RADIOBUTTON) &&
4567       GTK_IS_IMAGE (child->icon))
4568     {
4569       image = GTK_IMAGE (child->icon);
4570       if (gtk_image_get_storage_type (image) == GTK_IMAGE_STOCK)
4571         {
4572           gtk_image_get_stock (image, &stock_id, NULL);
4573           stock_id = g_strdup (stock_id);
4574           gtk_image_set_from_stock (image,
4575                                     stock_id,
4576                                     icon_size);
4577           g_free (stock_id);
4578         }
4579     }
4580   
4581   /* relief */
4582   if (child->type == GTK_TOOLBAR_CHILD_BUTTON ||
4583       child->type == GTK_TOOLBAR_CHILD_RADIOBUTTON ||
4584       child->type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON)
4585     {
4586       gtk_button_set_relief (GTK_BUTTON (child->widget), relief);
4587     }
4588 }
4589
4590 static void
4591 toolbar_content_toolbar_reconfigured (ToolbarContent *content,
4592                                       GtkToolbar     *toolbar)
4593 {
4594   switch (content->type)
4595     {
4596     case TOOL_ITEM:
4597       _gtk_tool_item_toolbar_reconfigured (content->u.tool_item.item);
4598       break;
4599       
4600     case COMPATIBILITY:
4601       toolbar_child_reconfigure (toolbar, &(content->u.compatibility.child));
4602       break;
4603     }
4604 }
4605
4606 static GtkWidget *
4607 toolbar_content_retrieve_menu_item (ToolbarContent *content)
4608 {
4609   if (content->type == TOOL_ITEM)
4610     return gtk_tool_item_retrieve_proxy_menu_item (content->u.tool_item.item);
4611   
4612   /* FIXME - we might actually be able to do something meaningful here */
4613   return NULL; 
4614 }
4615
4616 static gboolean
4617 toolbar_content_has_proxy_menu_item (ToolbarContent *content)
4618 {
4619   if (content->type == TOOL_ITEM)
4620     {
4621       GtkWidget *menu_item;
4622
4623       if (content->u.tool_item.has_menu == YES)
4624         return TRUE;
4625       else if (content->u.tool_item.has_menu == NO)
4626         return FALSE;
4627
4628       menu_item = toolbar_content_retrieve_menu_item (content);
4629
4630       content->u.tool_item.has_menu = menu_item? YES : NO;
4631       
4632       return menu_item != NULL;
4633     }
4634   else
4635     {
4636       return FALSE;
4637     }
4638 }
4639
4640 static void
4641 toolbar_content_set_unknown_menu_status (ToolbarContent *content)
4642 {
4643   if (content->type == TOOL_ITEM)
4644     content->u.tool_item.has_menu = UNKNOWN;
4645 }
4646
4647 static gboolean
4648 toolbar_content_is_separator (ToolbarContent *content)
4649 {
4650   GtkToolbarChild *child;
4651   
4652   switch (content->type)
4653     {
4654     case TOOL_ITEM:
4655       return GTK_IS_SEPARATOR_TOOL_ITEM (content->u.tool_item.item);
4656       break;
4657       
4658     case COMPATIBILITY:
4659       child = &(content->u.compatibility.child);
4660       return (child->type == GTK_TOOLBAR_CHILD_SPACE);
4661       break;
4662     }
4663   
4664   return FALSE;
4665 }
4666
4667 static void
4668 toolbar_content_set_expand (ToolbarContent *content,
4669                             gboolean        expand)
4670 {
4671   if (content->type == TOOL_ITEM)
4672     gtk_tool_item_set_expand (content->u.tool_item.item, expand);
4673 }
4674
4675 static gboolean
4676 ignore_show_and_hide_all (ToolbarContent *content)
4677 {
4678   if (content->type == COMPATIBILITY)
4679     {
4680       GtkToolbarChildType type = content->u.compatibility.child.type;
4681       
4682       if (type == GTK_TOOLBAR_CHILD_BUTTON ||
4683           type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON ||
4684           type == GTK_TOOLBAR_CHILD_RADIOBUTTON)
4685         {
4686           return TRUE;
4687         }
4688     }
4689   
4690   return FALSE;
4691 }
4692
4693 static void
4694 toolbar_content_show_all (ToolbarContent  *content)
4695 {
4696   GtkWidget *widget;
4697   
4698   if (ignore_show_and_hide_all (content))
4699     return;
4700
4701   widget = toolbar_content_get_widget (content);
4702   if (widget)
4703     gtk_widget_show_all (widget);
4704 }
4705
4706 static void
4707 toolbar_content_hide_all (ToolbarContent  *content)
4708 {
4709   GtkWidget *widget;
4710   
4711   if (ignore_show_and_hide_all (content))
4712     return;
4713
4714   widget = toolbar_content_get_widget (content);
4715   if (widget)
4716     gtk_widget_hide_all (widget);
4717 }
4718
4719 /*
4720  * Getters
4721  */
4722 static gint
4723 get_space_size (GtkToolbar *toolbar)
4724 {
4725   gint space_size = DEFAULT_SPACE_SIZE;
4726   
4727   if (toolbar)
4728     {
4729       gtk_widget_style_get (GTK_WIDGET (toolbar),
4730                             "space-size", &space_size,
4731                             NULL);
4732     }
4733   
4734   return space_size;
4735 }
4736
4737 static GtkToolbarSpaceStyle
4738 get_space_style (GtkToolbar *toolbar)
4739 {
4740   GtkToolbarSpaceStyle space_style = DEFAULT_SPACE_STYLE;
4741
4742   if (toolbar)
4743     {
4744       gtk_widget_style_get (GTK_WIDGET (toolbar),
4745                             "space-style", &space_style,
4746                             NULL);
4747     }
4748   
4749   return space_style;  
4750 }
4751
4752 static GtkReliefStyle
4753 get_button_relief (GtkToolbar *toolbar)
4754 {
4755   GtkReliefStyle button_relief = GTK_RELIEF_NORMAL;
4756   
4757   gtk_widget_ensure_style (GTK_WIDGET (toolbar));
4758   
4759   gtk_widget_style_get (GTK_WIDGET (toolbar),
4760                         "button-relief", &button_relief,
4761                         NULL);
4762   
4763   return button_relief;
4764 }
4765
4766 static gint
4767 get_internal_padding (GtkToolbar *toolbar)
4768 {
4769   gint ipadding = 0;
4770   
4771   gtk_widget_style_get (GTK_WIDGET (toolbar),
4772                         "internal-padding", &ipadding,
4773                         NULL);
4774   
4775   return ipadding;
4776 }
4777
4778 static gint
4779 get_max_child_expand (GtkToolbar *toolbar)
4780 {
4781   gint mexpand = G_MAXINT;
4782
4783   gtk_widget_style_get (GTK_WIDGET (toolbar),
4784                         "max-child-expand", &mexpand,
4785                         NULL);
4786   return mexpand;
4787 }
4788
4789 static GtkShadowType
4790 get_shadow_type (GtkToolbar *toolbar)
4791 {
4792   GtkShadowType shadow_type;
4793   
4794   gtk_widget_style_get (GTK_WIDGET (toolbar),
4795                         "shadow-type", &shadow_type,
4796                         NULL);
4797   
4798   return shadow_type;
4799 }
4800
4801 /*
4802  * API checks
4803  */
4804 static gboolean
4805 gtk_toolbar_check_old_api (GtkToolbar *toolbar)
4806 {
4807   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
4808   
4809   if (priv->api_mode == NEW_API)
4810     {
4811       g_warning (MIXED_API_WARNING);
4812       return FALSE;
4813     }
4814   
4815   priv->api_mode = OLD_API;
4816   return TRUE;
4817 }
4818
4819 static gboolean
4820 gtk_toolbar_check_new_api (GtkToolbar *toolbar)
4821 {
4822   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
4823   
4824   if (priv->api_mode == OLD_API)
4825     {
4826       g_warning (MIXED_API_WARNING);
4827       return FALSE;
4828     }
4829   
4830   priv->api_mode = NEW_API;
4831   return TRUE;
4832 }
4833
4834 /* GTK+ internal methods */
4835
4836 gint
4837 _gtk_toolbar_get_default_space_size (void)
4838 {
4839   return DEFAULT_SPACE_SIZE;
4840 }
4841
4842 void
4843 _gtk_toolbar_paint_space_line (GtkWidget       *widget,
4844                                GtkToolbar      *toolbar,
4845                                GdkRectangle    *area,
4846                                GtkAllocation   *allocation)
4847 {
4848   const double start_fraction = (SPACE_LINE_START / SPACE_LINE_DIVISION);
4849   const double end_fraction = (SPACE_LINE_END / SPACE_LINE_DIVISION);
4850   
4851   GtkOrientation orientation;
4852
4853   g_return_if_fail (GTK_IS_WIDGET (widget));
4854   
4855   orientation = toolbar? toolbar->orientation : GTK_ORIENTATION_HORIZONTAL;
4856
4857   if (orientation == GTK_ORIENTATION_HORIZONTAL)
4858     {
4859       gboolean wide_separators;
4860       gint     separator_width;
4861
4862       gtk_widget_style_get (widget,
4863                             "wide-separators", &wide_separators,
4864                             "separator-width", &separator_width,
4865                             NULL);
4866
4867       if (wide_separators)
4868         gtk_paint_box (widget->style, widget->window,
4869                        GTK_WIDGET_STATE (widget), GTK_SHADOW_ETCHED_OUT,
4870                        area, widget, "vseparator",
4871                        allocation->x + (allocation->width - separator_width) / 2,
4872                        allocation->y + allocation->height * start_fraction,
4873                        separator_width,
4874                        allocation->height * (end_fraction - start_fraction));
4875       else
4876         gtk_paint_vline (widget->style, widget->window,
4877                          GTK_WIDGET_STATE (widget), area, widget,
4878                          "toolbar",
4879                          allocation->y + allocation->height * start_fraction,
4880                          allocation->y + allocation->height * end_fraction,
4881                          allocation->x + (allocation->width - widget->style->xthickness) / 2);
4882     }
4883   else
4884     {
4885       gboolean wide_separators;
4886       gint     separator_height;
4887
4888       gtk_widget_style_get (widget,
4889                             "wide-separators",  &wide_separators,
4890                             "separator-height", &separator_height,
4891                             NULL);
4892
4893       if (wide_separators)
4894         gtk_paint_box (widget->style, widget->window,
4895                        GTK_WIDGET_STATE (widget), GTK_SHADOW_ETCHED_OUT,
4896                        area, widget, "hseparator",
4897                        allocation->x + allocation->width * start_fraction,
4898                        allocation->y + (allocation->height - separator_height) / 2,
4899                        allocation->width * (end_fraction - start_fraction),
4900                        separator_height);
4901       else
4902         gtk_paint_hline (widget->style, widget->window,
4903                          GTK_WIDGET_STATE (widget), area, widget,
4904                          "toolbar",
4905                          allocation->x + allocation->width * start_fraction,
4906                          allocation->x + allocation->width * end_fraction,
4907                          allocation->y + (allocation->height - widget->style->ythickness) / 2);
4908     }
4909 }
4910
4911 gchar *
4912 _gtk_toolbar_elide_underscores (const gchar *original)
4913 {
4914   gchar *q, *result;
4915   const gchar *p, *end;
4916   gsize len;
4917   gboolean last_underscore;
4918   
4919   if (!original)
4920     return NULL;
4921
4922   len = strlen (original);
4923   q = result = g_malloc (len + 1);
4924   last_underscore = FALSE;
4925   
4926   end = original + len;
4927   for (p = original; p < end; p++)
4928     {
4929       if (!last_underscore && *p == '_')
4930         last_underscore = TRUE;
4931       else
4932         {
4933           last_underscore = FALSE;
4934           if (original + 2 <= p && p + 1 <= end && 
4935               p[-2] == '(' && p[-1] == '_' && p[0] != '_' && p[1] == ')')
4936             {
4937               q--;
4938               *q = '\0';
4939               p++;
4940             }
4941           else
4942             *q++ = *p;
4943         }
4944     }
4945
4946   if (last_underscore)
4947     *q++ = '_';
4948   
4949   *q = '\0';
4950   
4951   return result;
4952 }
4953
4954 static GtkIconSize
4955 toolbar_get_icon_size (GtkToolShell *shell)
4956 {
4957   return GTK_TOOLBAR (shell)->icon_size;
4958 }
4959
4960 static GtkOrientation
4961 toolbar_get_orientation (GtkToolShell *shell)
4962 {
4963   return GTK_TOOLBAR (shell)->orientation;
4964 }
4965
4966 static GtkToolbarStyle
4967 toolbar_get_style (GtkToolShell *shell)
4968 {
4969   return GTK_TOOLBAR (shell)->style;
4970 }
4971
4972 static GtkReliefStyle
4973 toolbar_get_relief_style (GtkToolShell *shell)
4974 {
4975   return get_button_relief (GTK_TOOLBAR (shell));
4976 }
4977
4978 static void
4979 toolbar_rebuild_menu (GtkToolShell *shell)
4980 {
4981   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (shell);
4982   GList *list;
4983
4984   priv->need_rebuild = TRUE;
4985
4986   for (list = priv->content; list != NULL; list = list->next)
4987     {
4988       ToolbarContent *content = list->data;
4989
4990       toolbar_content_set_unknown_menu_status (content);
4991     }
4992   
4993   gtk_widget_queue_resize (GTK_WIDGET (shell));
4994 }
4995
4996 #define __GTK_TOOLBAR_C__
4997 #include "gtkaliasdef.c"