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