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