]> Pileus Git - ~andy/gtk/blob - gtk/gtktoolbar.c
261bfc15c128d81d0d2bf41fbde8c4b32449caf3
[~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_set_can_focus (GTK_WIDGET (toolbar), FALSE);
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_is_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_get_visible (GTK_WIDGET (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_get_visible (GTK_WIDGET (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: (type int): the current icon size for the icons on
3041  * the toolbar.
3042  **/
3043 GtkIconSize
3044 gtk_toolbar_get_icon_size (GtkToolbar *toolbar)
3045 {
3046   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), DEFAULT_ICON_SIZE);
3047   
3048   return toolbar->icon_size;
3049 }
3050
3051 /**
3052  * gtk_toolbar_get_relief_style:
3053  * @toolbar: a #GtkToolbar
3054  * 
3055  * Returns the relief style of buttons on @toolbar. See
3056  * gtk_button_set_relief().
3057  * 
3058  * Return value: The relief style of buttons on @toolbar.
3059  * 
3060  * Since: 2.4
3061  **/
3062 GtkReliefStyle
3063 gtk_toolbar_get_relief_style (GtkToolbar *toolbar)
3064 {
3065   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), GTK_RELIEF_NONE);
3066   
3067   return get_button_relief (toolbar);
3068 }
3069
3070 /**
3071  * gtk_toolbar_set_show_arrow:
3072  * @toolbar: a #GtkToolbar
3073  * @show_arrow: Whether to show an overflow menu
3074  * 
3075  * Sets whether to show an overflow menu when
3076  * @toolbar doesn't have room for all items on it. If %TRUE,
3077  * items that there are not room are available through an
3078  * overflow menu.
3079  * 
3080  * Since: 2.4
3081  **/
3082 void
3083 gtk_toolbar_set_show_arrow (GtkToolbar *toolbar,
3084                             gboolean    show_arrow)
3085 {
3086   GtkToolbarPrivate *priv;
3087   
3088   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3089   
3090   priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
3091   show_arrow = show_arrow != FALSE;
3092   
3093   if (priv->show_arrow != show_arrow)
3094     {
3095       priv->show_arrow = show_arrow;
3096       
3097       if (!priv->show_arrow)
3098         gtk_widget_hide (priv->arrow_button);
3099       
3100       gtk_widget_queue_resize (GTK_WIDGET (toolbar));      
3101       g_object_notify (G_OBJECT (toolbar), "show-arrow");
3102     }
3103 }
3104
3105 /**
3106  * gtk_toolbar_get_show_arrow:
3107  * @toolbar: a #GtkToolbar
3108  * 
3109  * Returns whether the toolbar has an overflow menu.
3110  * See gtk_toolbar_set_show_arrow().
3111  * 
3112  * Return value: %TRUE if the toolbar has an overflow menu.
3113  * 
3114  * Since: 2.4
3115  **/
3116 gboolean
3117 gtk_toolbar_get_show_arrow (GtkToolbar *toolbar)
3118 {
3119   GtkToolbarPrivate *priv;
3120   
3121   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
3122   
3123   if (!gtk_toolbar_check_new_api (toolbar))
3124     return FALSE;
3125   
3126   priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
3127   
3128   return priv->show_arrow;
3129 }
3130
3131 /**
3132  * gtk_toolbar_get_drop_index:
3133  * @toolbar: a #GtkToolbar
3134  * @x: x coordinate of a point on the toolbar
3135  * @y: y coordinate of a point on the toolbar
3136  *
3137  * Returns the position corresponding to the indicated point on
3138  * @toolbar. This is useful when dragging items to the toolbar:
3139  * this function returns the position a new item should be
3140  * inserted.
3141  *
3142  * @x and @y are in @toolbar coordinates.
3143  * 
3144  * Return value: The position corresponding to the point (@x, @y) on the toolbar.
3145  * 
3146  * Since: 2.4
3147  **/
3148 gint
3149 gtk_toolbar_get_drop_index (GtkToolbar *toolbar,
3150                             gint        x,
3151                             gint        y)
3152 {
3153   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), -1);
3154   
3155   if (!gtk_toolbar_check_new_api (toolbar))
3156     return -1;
3157   
3158   return physical_to_logical (toolbar, find_drop_index (toolbar, x, y));
3159 }
3160
3161 static void
3162 gtk_toolbar_finalize (GObject *object)
3163 {
3164   GList *list;
3165   GtkToolbar *toolbar = GTK_TOOLBAR (object);
3166   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
3167   
3168   if (toolbar->tooltips)
3169     g_object_unref (toolbar->tooltips);
3170   
3171   if (priv->arrow_button)
3172     gtk_widget_unparent (priv->arrow_button);
3173
3174   for (list = priv->content; list != NULL; list = list->next)
3175     {
3176       ToolbarContent *content = list->data;
3177
3178       toolbar_content_free (content);
3179     }
3180   
3181   g_list_free (priv->content);
3182   g_list_free (toolbar->children);
3183   
3184   g_timer_destroy (priv->timer);
3185   
3186   if (priv->menu)
3187     gtk_widget_destroy (GTK_WIDGET (priv->menu));
3188   
3189   if (priv->idle_id)
3190     g_source_remove (priv->idle_id);
3191
3192   G_OBJECT_CLASS (gtk_toolbar_parent_class)->finalize (object);
3193 }
3194
3195 /**
3196  * gtk_toolbar_set_icon_size:
3197  * @toolbar: A #GtkToolbar
3198  * @icon_size: (type int): The #GtkIconSize that stock icons in the
3199  *     toolbar shall have.
3200  *
3201  * This function sets the size of stock icons in the toolbar. You
3202  * can call it both before you add the icons and after they've been
3203  * added. The size you set will override user preferences for the default
3204  * icon size.
3205  * 
3206  * This should only be used for special-purpose toolbars, normal
3207  * application toolbars should respect the user preferences for the
3208  * size of icons.
3209  **/
3210 void
3211 gtk_toolbar_set_icon_size (GtkToolbar  *toolbar,
3212                            GtkIconSize  icon_size)
3213 {
3214   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3215   g_return_if_fail (icon_size != GTK_ICON_SIZE_INVALID);
3216   
3217   if (!toolbar->icon_size_set)
3218     {
3219       toolbar->icon_size_set = TRUE;  
3220       g_object_notify (G_OBJECT (toolbar), "icon-size-set");
3221     }
3222
3223   if (toolbar->icon_size == icon_size)
3224     return;
3225   
3226   toolbar->icon_size = icon_size;
3227   g_object_notify (G_OBJECT (toolbar), "icon-size");
3228   
3229   gtk_toolbar_reconfigured (toolbar);
3230   
3231   gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3232 }
3233
3234 /**
3235  * gtk_toolbar_unset_icon_size:
3236  * @toolbar: a #GtkToolbar
3237  * 
3238  * Unsets toolbar icon size set with gtk_toolbar_set_icon_size(), so that
3239  * user preferences will be used to determine the icon size.
3240  **/
3241 void
3242 gtk_toolbar_unset_icon_size (GtkToolbar *toolbar)
3243 {
3244   GtkIconSize size;
3245   
3246   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3247   
3248   if (toolbar->icon_size_set)
3249     {
3250       GtkSettings *settings = toolbar_get_settings (toolbar);
3251       
3252       if (settings)
3253         {
3254           g_object_get (settings,
3255                         "gtk-toolbar-icon-size", &size,
3256                         NULL);
3257         }
3258       else
3259         size = DEFAULT_ICON_SIZE;
3260       
3261       if (size != toolbar->icon_size)
3262         {
3263           gtk_toolbar_set_icon_size (toolbar, size);
3264           g_object_notify (G_OBJECT (toolbar), "icon-size");      
3265         }
3266       
3267       toolbar->icon_size_set = FALSE;
3268       g_object_notify (G_OBJECT (toolbar), "icon-size-set");      
3269     }
3270 }
3271
3272 /*
3273  * Deprecated API
3274  */
3275
3276 /**
3277  * gtk_toolbar_append_item:
3278  * @toolbar: a #GtkToolbar.
3279  * @text: give your toolbar button a label.
3280  * @tooltip_text: a string that appears when the user holds the mouse over this item.
3281  * @tooltip_private_text: use with #GtkTipsQuery.
3282  * @icon: a #GtkWidget that should be used as the button's icon.
3283  * @callback: the function to be executed when the button is pressed.
3284  * @user_data: a pointer to any data you wish to be passed to the callback.
3285  *
3286  * Inserts a new item into the toolbar. You must specify the position
3287  * in the toolbar where it will be inserted.
3288  *
3289  * @callback must be a pointer to a function taking a #GtkWidget and a gpointer as
3290  * arguments. Use G_CALLBACK() to cast the function to #GCallback.
3291  *
3292  * Return value: the new toolbar item as a #GtkWidget.
3293  *
3294  * Deprecated: 2.4: Use gtk_toolbar_insert() instead.
3295  **/
3296 GtkWidget *
3297 gtk_toolbar_append_item (GtkToolbar    *toolbar,
3298                          const char    *text,
3299                          const char    *tooltip_text,
3300                          const char    *tooltip_private_text,
3301                          GtkWidget     *icon,
3302                          GCallback      callback,
3303                          gpointer       user_data)
3304 {
3305   return gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_BUTTON,
3306                                      NULL, text,
3307                                      tooltip_text, tooltip_private_text,
3308                                      icon, callback, user_data,
3309                                      toolbar->num_children);
3310 }
3311
3312 /**
3313  * gtk_toolbar_prepend_item:
3314  * @toolbar: a #GtkToolbar.
3315  * @text: give your toolbar button a label.
3316  * @tooltip_text: a string that appears when the user holds the mouse over this item.
3317  * @tooltip_private_text: use with #GtkTipsQuery.
3318  * @icon: a #GtkWidget that should be used as the button's icon.
3319  * @callback: the function to be executed when the button is pressed.
3320  * @user_data: a pointer to any data you wish to be passed to the callback.
3321  *
3322  * Adds a new button to the beginning (top or left edges) of the given toolbar.
3323  *
3324  * @callback must be a pointer to a function taking a #GtkWidget and a gpointer as
3325  * arguments. Use G_CALLBACK() to cast the function to #GCallback.
3326  *
3327  * Return value: the new toolbar item as a #GtkWidget.
3328  *
3329  * Deprecated: 2.4: Use gtk_toolbar_insert() instead.
3330  **/
3331 GtkWidget *
3332 gtk_toolbar_prepend_item (GtkToolbar    *toolbar,
3333                           const char    *text,
3334                           const char    *tooltip_text,
3335                           const char    *tooltip_private_text,
3336                           GtkWidget     *icon,
3337                           GCallback      callback,
3338                           gpointer       user_data)
3339 {
3340   return gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_BUTTON,
3341                                      NULL, text,
3342                                      tooltip_text, tooltip_private_text,
3343                                      icon, callback, user_data,
3344                                      0);
3345 }
3346
3347 /**
3348  * gtk_toolbar_insert_item:
3349  * @toolbar: a #GtkToolbar.
3350  * @text: give your toolbar button a label.
3351  * @tooltip_text: a string that appears when the user holds the mouse over this item.
3352  * @tooltip_private_text: use with #GtkTipsQuery.
3353  * @icon: a #GtkWidget that should be used as the button's icon.
3354  * @callback: the function to be executed when the button is pressed.
3355  * @user_data: a pointer to any data you wish to be passed to the callback.
3356  * @position: the number of widgets to insert this item after.
3357  *
3358  * Inserts a new item into the toolbar. You must specify the position in the
3359  * toolbar where it will be inserted.
3360  *
3361  * @callback must be a pointer to a function taking a #GtkWidget and a gpointer as
3362  * arguments. Use G_CALLBACK() to cast the function to #GCallback.
3363  *
3364  * Return value: the new toolbar item as a #GtkWidget.
3365  *
3366  * Deprecated: 2.4: Use gtk_toolbar_insert() instead.
3367  **/
3368 GtkWidget *
3369 gtk_toolbar_insert_item (GtkToolbar    *toolbar,
3370                          const char    *text,
3371                          const char    *tooltip_text,
3372                          const char    *tooltip_private_text,
3373                          GtkWidget     *icon,
3374                          GCallback      callback,
3375                          gpointer       user_data,
3376                          gint           position)
3377 {
3378   return gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_BUTTON,
3379                                      NULL, text,
3380                                      tooltip_text, tooltip_private_text,
3381                                      icon, callback, user_data,
3382                                      position);
3383 }
3384
3385 /**
3386  * gtk_toolbar_insert_stock:
3387  * @toolbar: A #GtkToolbar
3388  * @stock_id: The id of the stock item you want to insert
3389  * @tooltip_text: The text in the tooltip of the toolbar button
3390  * @tooltip_private_text: The private text of the tooltip
3391  * @callback: The callback called when the toolbar button is clicked.
3392  * @user_data: user data passed to callback
3393  * @position: The position the button shall be inserted at.
3394  *            -1 means at the end.
3395  *
3396  * Inserts a stock item at the specified position of the toolbar.  If
3397  * @stock_id is not a known stock item ID, it's inserted verbatim,
3398  * except that underscores used to mark mnemonics are removed.
3399  *
3400  * @callback must be a pointer to a function taking a #GtkWidget and a gpointer as
3401  * arguments. Use G_CALLBACK() to cast the function to #GCallback.
3402  *
3403  * Returns: the inserted widget
3404  *
3405  * Deprecated: 2.4: Use gtk_toolbar_insert() instead.
3406  */
3407 GtkWidget*
3408 gtk_toolbar_insert_stock (GtkToolbar      *toolbar,
3409                           const gchar     *stock_id,
3410                           const char      *tooltip_text,
3411                           const char      *tooltip_private_text,
3412                           GCallback        callback,
3413                           gpointer         user_data,
3414                           gint             position)
3415 {
3416   return internal_insert_element (toolbar, GTK_TOOLBAR_CHILD_BUTTON,
3417                                   NULL, stock_id,
3418                                   tooltip_text, tooltip_private_text,
3419                                   NULL, callback, user_data,
3420                                   position, TRUE);
3421 }
3422
3423 /**
3424  * gtk_toolbar_append_space:
3425  * @toolbar: a #GtkToolbar.
3426  *
3427  * Adds a new space to the end of the toolbar.
3428  *
3429  * Deprecated: 2.4: Use gtk_toolbar_insert() instead.
3430  **/
3431 void
3432 gtk_toolbar_append_space (GtkToolbar *toolbar)
3433 {
3434   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_SPACE,
3435                               NULL, NULL,
3436                               NULL, NULL,
3437                               NULL, NULL, NULL,
3438                               toolbar->num_children);
3439 }
3440
3441 /**
3442  * gtk_toolbar_prepend_space:
3443  * @toolbar: a #GtkToolbar.
3444  *
3445  * Adds a new space to the beginning of the toolbar.
3446  *
3447  * Deprecated: 2.4: Use gtk_toolbar_insert() instead.
3448  **/
3449 void
3450 gtk_toolbar_prepend_space (GtkToolbar *toolbar)
3451 {
3452   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_SPACE,
3453                               NULL, NULL,
3454                               NULL, NULL,
3455                               NULL, NULL, NULL,
3456                               0);
3457 }
3458
3459 /**
3460  * gtk_toolbar_insert_space:
3461  * @toolbar: a #GtkToolbar
3462  * @position: the number of widgets after which a space should be inserted.
3463  *
3464  * Inserts a new space in the toolbar at the specified position.
3465  *
3466  * Deprecated: 2.4: Use gtk_toolbar_insert() instead.
3467  **/
3468 void
3469 gtk_toolbar_insert_space (GtkToolbar *toolbar,
3470                           gint        position)
3471 {
3472   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_SPACE,
3473                               NULL, NULL,
3474                               NULL, NULL,
3475                               NULL, NULL, NULL,
3476                               position);
3477 }
3478
3479 /**
3480  * gtk_toolbar_remove_space:
3481  * @toolbar: a #GtkToolbar.
3482  * @position: the index of the space to remove.
3483  *
3484  * Removes a space from the specified position.
3485  *
3486  * Deprecated: 2.4: Use gtk_toolbar_insert() instead.
3487  **/
3488 void
3489 gtk_toolbar_remove_space (GtkToolbar *toolbar,
3490                           gint        position)
3491 {
3492   GtkToolbarPrivate *priv;
3493   ToolbarContent *content;
3494   
3495   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
3496   
3497   if (!gtk_toolbar_check_old_api (toolbar))
3498     return;
3499   
3500   priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
3501   
3502   content = g_list_nth_data (priv->content, position);
3503   
3504   if (!content)
3505     {
3506       g_warning ("Toolbar position %d doesn't exist", position);
3507       return;
3508     }
3509   
3510   if (!toolbar_content_is_separator (content))
3511     {
3512       g_warning ("Toolbar position %d is not a space", position);
3513       return;
3514     }
3515   
3516   toolbar_content_remove (content, toolbar);
3517   toolbar_content_free (content);
3518 }
3519
3520 /**
3521  * gtk_toolbar_append_widget:
3522  * @toolbar: a #GtkToolbar.
3523  * @widget: a #GtkWidget to add to the toolbar.
3524  * @tooltip_text: (allow-none): the element's tooltip.
3525  * @tooltip_private_text: (allow-none): used for context-sensitive help about this toolbar element.
3526  *
3527  * Adds a widget to the end of the given toolbar.
3528  *
3529  * Deprecated: 2.4: Use gtk_toolbar_insert() instead.
3530  **/
3531 void
3532 gtk_toolbar_append_widget (GtkToolbar  *toolbar,
3533                            GtkWidget   *widget,
3534                            const gchar *tooltip_text,
3535                            const gchar *tooltip_private_text)
3536 {
3537   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_WIDGET,
3538                               widget, NULL,
3539                               tooltip_text, tooltip_private_text,
3540                               NULL, NULL, NULL,
3541                               toolbar->num_children);
3542 }
3543
3544 /**
3545  * gtk_toolbar_prepend_widget:
3546  * @toolbar: a #GtkToolbar.
3547  * @widget: a #GtkWidget to add to the toolbar.
3548  * @tooltip_text: (allow-none): the element's tooltip.
3549  * @tooltip_private_text: (allow-none): used for context-sensitive help about this toolbar element.
3550  *
3551  * Adds a widget to the beginning of the given toolbar.
3552  *
3553  * Deprecated: 2.4: Use gtk_toolbar_insert() instead.
3554  **/
3555 void
3556 gtk_toolbar_prepend_widget (GtkToolbar  *toolbar,
3557                             GtkWidget   *widget,
3558                             const gchar *tooltip_text,
3559                             const gchar *tooltip_private_text)
3560 {
3561   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_WIDGET,
3562                               widget, NULL,
3563                               tooltip_text, tooltip_private_text,
3564                               NULL, NULL, NULL,
3565                               0);
3566 }
3567
3568 /**
3569  * gtk_toolbar_insert_widget:
3570  * @toolbar: a #GtkToolbar.
3571  * @widget: a #GtkWidget to add to the toolbar.
3572  * @tooltip_text: (allow-none): the element's tooltip.
3573  * @tooltip_private_text: (allow-none): used for context-sensitive help about this toolbar element.
3574  * @position: the number of widgets to insert this widget after.
3575  *
3576  * Inserts a widget in the toolbar at the given position.
3577  *
3578  * Deprecated: 2.4: Use gtk_toolbar_insert() instead.
3579  **/ 
3580 void
3581 gtk_toolbar_insert_widget (GtkToolbar *toolbar,
3582                            GtkWidget  *widget,
3583                            const char *tooltip_text,
3584                            const char *tooltip_private_text,
3585                            gint        position)
3586 {
3587   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_WIDGET,
3588                               widget, NULL,
3589                               tooltip_text, tooltip_private_text,
3590                               NULL, NULL, NULL,
3591                               position);
3592 }
3593
3594 /**
3595  * gtk_toolbar_append_element:
3596  * @toolbar: a #GtkToolbar.
3597  * @type: a value of type #GtkToolbarChildType that determines what @widget will be.
3598  * @widget: (allow-none): a #GtkWidget, or %NULL.
3599  * @text: the element's label.
3600  * @tooltip_text: the element's tooltip.
3601  * @tooltip_private_text: used for context-sensitive help about this toolbar element.
3602  * @icon: a #GtkWidget that provides pictorial representation of the element's function.
3603  * @callback: the function to be executed when the button is pressed.
3604  * @user_data: any data you wish to pass to the callback.
3605  * 
3606  * Adds a new element to the end of a toolbar.
3607  * 
3608  * If @type == %GTK_TOOLBAR_CHILD_WIDGET, @widget is used as the new element.
3609  * If @type == %GTK_TOOLBAR_CHILD_RADIOBUTTON, @widget is used to determine
3610  * the radio group for the new element. In all other cases, @widget must
3611  * be %NULL.
3612  * 
3613  * @callback must be a pointer to a function taking a #GtkWidget and a gpointer as
3614  * arguments. Use G_CALLBACK() to cast the function to #GCallback.
3615  *
3616  * Return value: the new toolbar element as a #GtkWidget.
3617  *
3618  * Deprecated: 2.4: Use gtk_toolbar_insert() instead.
3619  **/
3620 GtkWidget*
3621 gtk_toolbar_append_element (GtkToolbar          *toolbar,
3622                             GtkToolbarChildType  type,
3623                             GtkWidget           *widget,
3624                             const char          *text,
3625                             const char          *tooltip_text,
3626                             const char          *tooltip_private_text,
3627                             GtkWidget           *icon,
3628                             GCallback            callback,
3629                             gpointer             user_data)
3630 {
3631   return gtk_toolbar_insert_element (toolbar, type, widget, text,
3632                                      tooltip_text, tooltip_private_text,
3633                                      icon, callback, user_data,
3634                                      toolbar->num_children);
3635 }
3636
3637 /**
3638  * gtk_toolbar_prepend_element:
3639  * @toolbar: a #GtkToolbar.
3640  * @type: a value of type #GtkToolbarChildType that determines what @widget will be.
3641  * @widget: (allow-none): a #GtkWidget, or %NULL
3642  * @text: the element's label.
3643  * @tooltip_text: the element's tooltip.
3644  * @tooltip_private_text: used for context-sensitive help about this toolbar element.
3645  * @icon: a #GtkWidget that provides pictorial representation of the element's function.
3646  * @callback: the function to be executed when the button is pressed.
3647  * @user_data: any data you wish to pass to the callback.
3648  *  
3649  * Adds a new element to the beginning of a toolbar.
3650  * 
3651  * If @type == %GTK_TOOLBAR_CHILD_WIDGET, @widget is used as the new element.
3652  * If @type == %GTK_TOOLBAR_CHILD_RADIOBUTTON, @widget is used to determine
3653  * the radio group for the new element. In all other cases, @widget must
3654  * be %NULL.
3655  * 
3656  * @callback must be a pointer to a function taking a #GtkWidget and a gpointer as
3657  * arguments. Use G_CALLBACK() to cast the function to #GCallback.
3658  *
3659  * Return value: the new toolbar element as a #GtkWidget.
3660  *
3661  * Deprecated: 2.4: Use gtk_toolbar_insert() instead.
3662  **/
3663 GtkWidget *
3664 gtk_toolbar_prepend_element (GtkToolbar          *toolbar,
3665                              GtkToolbarChildType  type,
3666                              GtkWidget           *widget,
3667                              const char          *text,
3668                              const char          *tooltip_text,
3669                              const char          *tooltip_private_text,
3670                              GtkWidget           *icon,
3671                              GCallback            callback,
3672                              gpointer             user_data)
3673 {
3674   return gtk_toolbar_insert_element (toolbar, type, widget, text,
3675                                      tooltip_text, tooltip_private_text,
3676                                      icon, callback, user_data, 0);
3677 }
3678
3679 /**
3680  * gtk_toolbar_insert_element:
3681  * @toolbar: a #GtkToolbar.
3682  * @type: a value of type #GtkToolbarChildType that determines what @widget
3683  *   will be.
3684  * @widget: (allow-none): a #GtkWidget, or %NULL. 
3685  * @text: the element's label.
3686  * @tooltip_text: the element's tooltip.
3687  * @tooltip_private_text: used for context-sensitive help about this toolbar element.
3688  * @icon: a #GtkWidget that provides pictorial representation of the element's function.
3689  * @callback: the function to be executed when the button is pressed.
3690  * @user_data: any data you wish to pass to the callback.
3691  * @position: the number of widgets to insert this element after.
3692  *
3693  * Inserts a new element in the toolbar at the given position. 
3694  *
3695  * If @type == %GTK_TOOLBAR_CHILD_WIDGET, @widget is used as the new element.
3696  * If @type == %GTK_TOOLBAR_CHILD_RADIOBUTTON, @widget is used to determine
3697  * the radio group for the new element. In all other cases, @widget must
3698  * be %NULL.
3699  *
3700  * @callback must be a pointer to a function taking a #GtkWidget and a gpointer as
3701  * arguments. Use G_CALLBACK() to cast the function to #GCallback.
3702  *
3703  * Return value: the new toolbar element as a #GtkWidget.
3704  *
3705  * Deprecated: 2.4: Use gtk_toolbar_insert() instead.
3706  **/
3707 GtkWidget *
3708 gtk_toolbar_insert_element (GtkToolbar          *toolbar,
3709                             GtkToolbarChildType  type,
3710                             GtkWidget           *widget,
3711                             const char          *text,
3712                             const char          *tooltip_text,
3713                             const char          *tooltip_private_text,
3714                             GtkWidget           *icon,
3715                             GCallback            callback,
3716                             gpointer             user_data,
3717                             gint                 position)
3718 {
3719   return internal_insert_element (toolbar, type, widget, text,
3720                                   tooltip_text, tooltip_private_text,
3721                                   icon, callback, user_data, position, FALSE);
3722 }
3723
3724 static void
3725 set_child_packing_and_visibility(GtkToolbar      *toolbar,
3726                                  GtkToolbarChild *child)
3727 {
3728   GtkWidget *box;
3729   gboolean   expand;
3730
3731   box = gtk_bin_get_child (GTK_BIN (child->widget));
3732   
3733   g_return_if_fail (GTK_IS_BOX (box));
3734   
3735   if (child->label)
3736     {
3737       expand = (toolbar->style != GTK_TOOLBAR_BOTH);
3738       
3739       gtk_box_set_child_packing (GTK_BOX (box), child->label,
3740                                  expand, expand, 0, GTK_PACK_END);
3741       
3742       if (toolbar->style != GTK_TOOLBAR_ICONS)
3743         gtk_widget_show (child->label);
3744       else
3745         gtk_widget_hide (child->label);
3746     }
3747   
3748   if (child->icon)
3749     {
3750       expand = (toolbar->style != GTK_TOOLBAR_BOTH_HORIZ);
3751       
3752       gtk_box_set_child_packing (GTK_BOX (box), child->icon,
3753                                  expand, expand, 0, GTK_PACK_END);
3754       
3755       if (toolbar->style != GTK_TOOLBAR_TEXT)
3756         gtk_widget_show (child->icon);
3757       else
3758         gtk_widget_hide (child->icon);
3759     }
3760 }
3761
3762 static GtkWidget *
3763 internal_insert_element (GtkToolbar          *toolbar,
3764                          GtkToolbarChildType  type,
3765                          GtkWidget           *widget,
3766                          const char          *text,
3767                          const char          *tooltip_text,
3768                          const char          *tooltip_private_text,
3769                          GtkWidget           *icon,
3770                          GCallback            callback,
3771                          gpointer             user_data,
3772                          gint                 position,
3773                          gboolean             use_stock)
3774 {
3775   GtkWidget *box;
3776   ToolbarContent *content;
3777   char *free_me = NULL;
3778
3779   GtkWidget *child_widget;
3780   GtkWidget *child_label;
3781   GtkWidget *child_icon;
3782
3783   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), NULL);
3784   if (type == GTK_TOOLBAR_CHILD_WIDGET)
3785     g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
3786   else if (type != GTK_TOOLBAR_CHILD_RADIOBUTTON)
3787     g_return_val_if_fail (widget == NULL, NULL);
3788   if (GTK_IS_TOOL_ITEM (widget))
3789     g_warning (MIXED_API_WARNING);
3790   
3791   if (!gtk_toolbar_check_old_api (toolbar))
3792     return NULL;
3793   
3794   child_widget = NULL;
3795   child_label = NULL;
3796   child_icon = NULL;
3797   
3798   switch (type)
3799     {
3800     case GTK_TOOLBAR_CHILD_SPACE:
3801       break;
3802       
3803     case GTK_TOOLBAR_CHILD_WIDGET:
3804       child_widget = widget;
3805       break;
3806       
3807     case GTK_TOOLBAR_CHILD_BUTTON:
3808     case GTK_TOOLBAR_CHILD_TOGGLEBUTTON:
3809     case GTK_TOOLBAR_CHILD_RADIOBUTTON:
3810       if (type == GTK_TOOLBAR_CHILD_BUTTON)
3811         {
3812           child_widget = gtk_button_new ();
3813         }
3814       else if (type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON)
3815         {
3816           child_widget = gtk_toggle_button_new ();
3817           gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (child_widget), FALSE);
3818         }
3819       else /* type == GTK_TOOLBAR_CHILD_RADIOBUTTON */
3820         {
3821           GSList *group = NULL;
3822
3823           if (widget)
3824             group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (widget));
3825           
3826           child_widget = gtk_radio_button_new (group);
3827           gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (child_widget), FALSE);
3828         }
3829
3830       gtk_button_set_relief (GTK_BUTTON (child_widget), get_button_relief (toolbar));
3831       gtk_button_set_focus_on_click (GTK_BUTTON (child_widget), FALSE);
3832       
3833       if (callback)
3834         {
3835           g_signal_connect (child_widget, "clicked",
3836                             callback, user_data);
3837         }
3838       
3839       if (toolbar->style == GTK_TOOLBAR_BOTH_HORIZ)
3840         box = gtk_hbox_new (FALSE, 0);
3841       else
3842         box = gtk_vbox_new (FALSE, 0);
3843
3844       gtk_container_add (GTK_CONTAINER (child_widget), box);
3845       gtk_widget_show (box);
3846       
3847       if (text && use_stock)
3848         {
3849           GtkStockItem stock_item;
3850           if (gtk_stock_lookup (text, &stock_item))
3851             {
3852               if (!icon)
3853                 icon = gtk_image_new_from_stock (text, toolbar->icon_size);
3854           
3855               text = free_me = _gtk_toolbar_elide_underscores (stock_item.label);
3856             }
3857         }
3858       
3859       if (text)
3860         {
3861           child_label = gtk_label_new (text);
3862           
3863           gtk_container_add (GTK_CONTAINER (box), child_label);
3864         }
3865       
3866       if (icon)
3867         {
3868           child_icon = GTK_WIDGET (icon);
3869           gtk_container_add (GTK_CONTAINER (box), child_icon);
3870         }
3871       
3872       gtk_widget_show (child_widget);
3873       break;
3874       
3875     default:
3876       g_assert_not_reached ();
3877       break;
3878     }
3879   
3880   if ((type != GTK_TOOLBAR_CHILD_SPACE) && tooltip_text)
3881     {
3882       gtk_tooltips_set_tip (toolbar->tooltips, child_widget,
3883                             tooltip_text, tooltip_private_text);
3884     }
3885   
3886   content = toolbar_content_new_compatibility (toolbar, type, child_widget,
3887                                                child_icon, child_label, position);
3888   
3889   g_free (free_me);
3890   
3891   return child_widget;
3892 }
3893
3894 /*
3895  * ToolbarContent methods
3896  */
3897 typedef enum {
3898   UNKNOWN,
3899   YES,
3900   NO
3901 } TriState;
3902
3903 struct _ToolbarContent
3904 {
3905   ContentType   type;
3906   ItemState     state;
3907   
3908   union
3909   {
3910     struct
3911     {
3912       GtkToolItem *     item;
3913       GtkAllocation     start_allocation;
3914       GtkAllocation     goal_allocation;
3915       guint             is_placeholder : 1;
3916       guint             disappearing : 1;
3917       guint             has_menu : 2;
3918     } tool_item;
3919     
3920     struct
3921     {
3922       GtkToolbarChild   child;
3923       GtkAllocation     space_allocation;
3924       guint             space_visible : 1;
3925     } compatibility;
3926   } u;
3927 };
3928
3929 static ToolbarContent *
3930 toolbar_content_new_tool_item (GtkToolbar  *toolbar,
3931                                GtkToolItem *item,
3932                                gboolean     is_placeholder,
3933                                gint         pos)
3934 {
3935   ToolbarContent *content;
3936   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
3937   
3938   content = g_slice_new0 (ToolbarContent);
3939   
3940   content->type = TOOL_ITEM;
3941   content->state = NOT_ALLOCATED;
3942   content->u.tool_item.item = item;
3943   content->u.tool_item.is_placeholder = is_placeholder;
3944   
3945   gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (toolbar));
3946
3947   priv->content = g_list_insert (priv->content, content, pos);
3948   
3949   if (!is_placeholder)
3950     {
3951       toolbar->num_children++;
3952
3953       gtk_toolbar_stop_sliding (toolbar);
3954     }
3955
3956   gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3957   priv->need_rebuild = TRUE;
3958   
3959   return content;
3960 }
3961
3962 static ToolbarContent *
3963 toolbar_content_new_compatibility (GtkToolbar          *toolbar,
3964                                    GtkToolbarChildType  type,
3965                                    GtkWidget            *widget,
3966                                    GtkWidget            *icon,
3967                                    GtkWidget            *label,
3968                                    gint                  pos)
3969 {
3970   ToolbarContent *content;
3971   GtkToolbarChild *child;
3972   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
3973   
3974   content = g_slice_new0 (ToolbarContent);
3975
3976   child = &(content->u.compatibility.child);
3977   
3978   content->type = COMPATIBILITY;
3979   child->type = type;
3980   child->widget = widget;
3981   child->icon = icon;
3982   child->label = label;
3983   
3984   if (type != GTK_TOOLBAR_CHILD_SPACE)
3985     {
3986       gtk_widget_set_parent (child->widget, GTK_WIDGET (toolbar));
3987     }
3988   else
3989     {
3990       content->u.compatibility.space_visible = TRUE;
3991       gtk_widget_queue_resize (GTK_WIDGET (toolbar));
3992     }
3993  
3994   if (type == GTK_TOOLBAR_CHILD_BUTTON ||
3995       type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON ||
3996       type == GTK_TOOLBAR_CHILD_RADIOBUTTON)
3997     {
3998       set_child_packing_and_visibility (toolbar, child);
3999     }
4000
4001   priv->content = g_list_insert (priv->content, content, pos);
4002   toolbar->children = g_list_insert (toolbar->children, child, pos);
4003   priv->need_rebuild = TRUE;
4004   
4005   toolbar->num_children++;
4006   
4007   return content;
4008 }
4009
4010 static void
4011 toolbar_content_remove (ToolbarContent *content,
4012                         GtkToolbar     *toolbar)
4013 {
4014   GtkToolbarChild *child;
4015   GtkToolbarPrivate *priv;
4016
4017   priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
4018   
4019   switch (content->type)
4020     {
4021     case TOOL_ITEM:
4022       gtk_widget_unparent (GTK_WIDGET (content->u.tool_item.item));
4023       break;
4024       
4025     case COMPATIBILITY:
4026       child = &(content->u.compatibility.child);
4027       
4028       if (child->type != GTK_TOOLBAR_CHILD_SPACE)
4029         {
4030           g_object_ref (child->widget);
4031           gtk_widget_unparent (child->widget);
4032           gtk_widget_destroy (child->widget);
4033           g_object_unref (child->widget);
4034         }
4035       
4036       toolbar->children = g_list_remove (toolbar->children, child);
4037       break;
4038     }
4039
4040   priv->content = g_list_remove (priv->content, content);
4041
4042   if (!toolbar_content_is_placeholder (content))
4043     toolbar->num_children--;
4044
4045   gtk_widget_queue_resize (GTK_WIDGET (toolbar));
4046   priv->need_rebuild = TRUE;
4047 }
4048
4049 static void
4050 toolbar_content_free (ToolbarContent *content)
4051 {
4052   g_slice_free (ToolbarContent, content);
4053 }
4054
4055 static gint
4056 calculate_max_homogeneous_pixels (GtkWidget *widget)
4057 {
4058   PangoContext *context;
4059   PangoFontMetrics *metrics;
4060   gint char_width;
4061   
4062   context = gtk_widget_get_pango_context (widget);
4063   metrics = pango_context_get_metrics (context,
4064                                        widget->style->font_desc,
4065                                        pango_context_get_language (context));
4066   char_width = pango_font_metrics_get_approximate_char_width (metrics);
4067   pango_font_metrics_unref (metrics);
4068   
4069   return PANGO_PIXELS (MAX_HOMOGENEOUS_N_CHARS * char_width);
4070 }
4071
4072 static void
4073 toolbar_content_expose (ToolbarContent *content,
4074                         GtkContainer   *container,
4075                         GdkEventExpose *expose)
4076 {
4077   GtkToolbar *toolbar = GTK_TOOLBAR (container);
4078   GtkToolbarChild *child;
4079   GtkWidget *widget = NULL; /* quiet gcc */
4080   
4081   switch (content->type)
4082     {
4083     case TOOL_ITEM:
4084       if (!content->u.tool_item.is_placeholder)
4085         widget = GTK_WIDGET (content->u.tool_item.item);
4086       break;
4087       
4088     case COMPATIBILITY:
4089       child = &(content->u.compatibility.child);
4090       
4091       if (child->type == GTK_TOOLBAR_CHILD_SPACE)
4092         {
4093           if (content->u.compatibility.space_visible &&
4094               get_space_style (toolbar) == GTK_TOOLBAR_SPACE_LINE)
4095              _gtk_toolbar_paint_space_line (GTK_WIDGET (toolbar), toolbar,
4096                                             &expose->area,
4097                                             &content->u.compatibility.space_allocation);
4098           return;
4099         }
4100       
4101       widget = child->widget;
4102       break;
4103     }
4104   
4105   if (widget)
4106     gtk_container_propagate_expose (container, widget, expose);
4107 }
4108
4109 static gboolean
4110 toolbar_content_visible (ToolbarContent *content,
4111                          GtkToolbar     *toolbar)
4112 {
4113   GtkToolItem *item;
4114   
4115   switch (content->type)
4116     {
4117     case TOOL_ITEM:
4118       item = content->u.tool_item.item;
4119       
4120       if (!gtk_widget_get_visible (GTK_WIDGET (item)))
4121         return FALSE;
4122       
4123       if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL &&
4124           gtk_tool_item_get_visible_horizontal (item))
4125         return TRUE;
4126       
4127       if ((toolbar->orientation == GTK_ORIENTATION_VERTICAL &&
4128            gtk_tool_item_get_visible_vertical (item)))
4129         return TRUE;
4130       
4131       return FALSE;
4132       break;
4133       
4134     case COMPATIBILITY:
4135       if (content->u.compatibility.child.type != GTK_TOOLBAR_CHILD_SPACE)
4136         return gtk_widget_get_visible (content->u.compatibility.child.widget);
4137       else
4138         return TRUE;
4139       break;
4140     }
4141   
4142   g_assert_not_reached ();
4143   return FALSE;
4144 }
4145
4146 static void
4147 toolbar_content_size_request (ToolbarContent *content,
4148                               GtkToolbar     *toolbar,
4149                               GtkRequisition *requisition)
4150 {
4151   gint space_size;
4152   
4153   switch (content->type)
4154     {
4155     case TOOL_ITEM:
4156       gtk_widget_size_request (GTK_WIDGET (content->u.tool_item.item),
4157                                requisition);
4158       if (content->u.tool_item.is_placeholder &&
4159           content->u.tool_item.disappearing)
4160         {
4161           requisition->width = 0;
4162           requisition->height = 0;
4163         }
4164       break;
4165       
4166     case COMPATIBILITY:
4167       space_size = get_space_size (toolbar);
4168       
4169       if (content->u.compatibility.child.type != GTK_TOOLBAR_CHILD_SPACE)
4170         {
4171           gtk_widget_size_request (content->u.compatibility.child.widget,
4172                                    requisition);
4173         }
4174       else
4175         {
4176           if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
4177             {
4178               requisition->width = space_size;
4179               requisition->height = 0;
4180             }
4181           else
4182             {
4183               requisition->height = space_size;
4184               requisition->width = 0;
4185             }
4186         }
4187       
4188       break;
4189     }
4190 }
4191
4192 static gboolean
4193 toolbar_content_is_homogeneous (ToolbarContent *content,
4194                                 GtkToolbar     *toolbar)
4195 {
4196   gboolean result = FALSE;      /* quiet gcc */
4197   GtkRequisition requisition;
4198   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
4199   
4200   if (priv->max_homogeneous_pixels < 0)
4201     {
4202       priv->max_homogeneous_pixels =
4203         calculate_max_homogeneous_pixels (GTK_WIDGET (toolbar));
4204     }
4205   
4206   toolbar_content_size_request (content, toolbar, &requisition);
4207   
4208   if (requisition.width > priv->max_homogeneous_pixels)
4209     return FALSE;
4210   
4211   switch (content->type)
4212     {
4213     case TOOL_ITEM:
4214       result = gtk_tool_item_get_homogeneous (content->u.tool_item.item) &&
4215         !GTK_IS_SEPARATOR_TOOL_ITEM (content->u.tool_item.item);
4216       
4217       if (gtk_tool_item_get_is_important (content->u.tool_item.item) &&
4218           toolbar->style == GTK_TOOLBAR_BOTH_HORIZ &&
4219           toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
4220         {
4221           result = FALSE;
4222         }
4223       break;
4224       
4225     case COMPATIBILITY:
4226       if (content->u.compatibility.child.type == GTK_TOOLBAR_CHILD_BUTTON ||
4227           content->u.compatibility.child.type == GTK_TOOLBAR_CHILD_RADIOBUTTON ||
4228           content->u.compatibility.child.type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON)
4229         {
4230           result = TRUE;
4231         }
4232       else
4233         {
4234           result = FALSE;
4235         }
4236       break;
4237     }
4238   
4239   return result;
4240 }
4241
4242 static gboolean
4243 toolbar_content_is_placeholder (ToolbarContent *content)
4244 {
4245   if (content->type == TOOL_ITEM && content->u.tool_item.is_placeholder)
4246     return TRUE;
4247   
4248   return FALSE;
4249 }
4250
4251 static gboolean
4252 toolbar_content_disappearing (ToolbarContent *content)
4253 {
4254   if (content->type == TOOL_ITEM && content->u.tool_item.disappearing)
4255     return TRUE;
4256   
4257   return FALSE;
4258 }
4259
4260 static ItemState
4261 toolbar_content_get_state (ToolbarContent *content)
4262 {
4263   return content->state;
4264 }
4265
4266 static gboolean
4267 toolbar_content_child_visible (ToolbarContent *content)
4268 {
4269   switch (content->type)
4270     {
4271     case TOOL_ITEM:
4272       return GTK_WIDGET_CHILD_VISIBLE (content->u.tool_item.item);
4273       break;
4274       
4275     case COMPATIBILITY:
4276       if (content->u.compatibility.child.type != GTK_TOOLBAR_CHILD_SPACE)
4277         {
4278           return GTK_WIDGET_CHILD_VISIBLE (content->u.compatibility.child.widget);
4279         }
4280       else
4281         {
4282           return content->u.compatibility.space_visible;
4283         }
4284       break;
4285     }
4286   
4287   return FALSE; /* quiet gcc */
4288 }
4289
4290 static void
4291 toolbar_content_get_goal_allocation (ToolbarContent *content,
4292                                      GtkAllocation  *allocation)
4293 {
4294   switch (content->type)
4295     {
4296     case TOOL_ITEM:
4297       *allocation = content->u.tool_item.goal_allocation;
4298       break;
4299       
4300     case COMPATIBILITY:
4301       /* Goal allocations are only relevant when we are
4302        * using the new API, so we should never get here
4303        */
4304       g_assert_not_reached ();
4305       break;
4306     }
4307 }
4308
4309 static void
4310 toolbar_content_get_allocation (ToolbarContent *content,
4311                                 GtkAllocation  *allocation)
4312 {
4313   GtkToolbarChild *child;
4314   
4315   switch (content->type)
4316     {
4317     case TOOL_ITEM:
4318       *allocation = GTK_WIDGET (content->u.tool_item.item)->allocation;
4319       break;
4320       
4321     case COMPATIBILITY:
4322       child = &(content->u.compatibility.child);
4323       
4324       if (child->type == GTK_TOOLBAR_CHILD_SPACE)
4325         *allocation = content->u.compatibility.space_allocation;
4326       else
4327         *allocation = child->widget->allocation;
4328       break;
4329     }
4330 }
4331
4332 static void
4333 toolbar_content_set_start_allocation (ToolbarContent *content,
4334                                       GtkAllocation  *allocation)
4335 {
4336   switch (content->type)
4337     {
4338     case TOOL_ITEM:
4339       content->u.tool_item.start_allocation = *allocation;
4340       break;
4341       
4342     case COMPATIBILITY:
4343       /* start_allocation is only relevant when using the new API */
4344       g_assert_not_reached ();
4345       break;
4346     }
4347 }
4348
4349 static gboolean
4350 toolbar_content_get_expand (ToolbarContent *content)
4351 {
4352   if (content->type == TOOL_ITEM &&
4353       gtk_tool_item_get_expand (content->u.tool_item.item) &&
4354       !content->u.tool_item.disappearing)
4355     {
4356       return TRUE;
4357     }
4358   
4359   return FALSE;
4360 }
4361
4362 static void
4363 toolbar_content_set_goal_allocation (ToolbarContent *content,
4364                                      GtkAllocation  *allocation)
4365 {
4366   switch (content->type)
4367     {
4368     case TOOL_ITEM:
4369       content->u.tool_item.goal_allocation = *allocation;
4370       break;
4371       
4372     case COMPATIBILITY:
4373       /* Only relevant when using new API */
4374       g_assert_not_reached ();
4375       break;
4376     }
4377 }
4378
4379 static void
4380 toolbar_content_set_child_visible (ToolbarContent *content,
4381                                    GtkToolbar     *toolbar,
4382                                    gboolean        visible)
4383 {
4384   GtkToolbarChild *child;
4385   
4386   switch (content->type)
4387     {
4388     case TOOL_ITEM:
4389       gtk_widget_set_child_visible (GTK_WIDGET (content->u.tool_item.item),
4390                                     visible);
4391       break;
4392       
4393     case COMPATIBILITY:
4394       child = &(content->u.compatibility.child);
4395       
4396       if (child->type != GTK_TOOLBAR_CHILD_SPACE)
4397         {
4398           gtk_widget_set_child_visible (child->widget, visible);
4399         }
4400       else
4401         {
4402           if (content->u.compatibility.space_visible != visible)
4403             {
4404               content->u.compatibility.space_visible = visible;
4405               gtk_widget_queue_draw (GTK_WIDGET (toolbar));
4406             }
4407         }
4408       break;
4409     }
4410 }
4411
4412 static void
4413 toolbar_content_get_start_allocation (ToolbarContent *content,
4414                                       GtkAllocation  *start_allocation)
4415 {
4416   switch (content->type)
4417     {
4418     case TOOL_ITEM:
4419       *start_allocation = content->u.tool_item.start_allocation;
4420       break;
4421       
4422     case COMPATIBILITY:
4423       /* Only relevant for new API */
4424       g_assert_not_reached ();
4425       break;
4426     }
4427 }
4428
4429 static void
4430 toolbar_content_size_allocate (ToolbarContent *content,
4431                                GtkAllocation  *allocation)
4432 {
4433   switch (content->type)
4434     {
4435     case TOOL_ITEM:
4436       gtk_widget_size_allocate (GTK_WIDGET (content->u.tool_item.item),
4437                                 allocation);
4438       break;
4439       
4440     case COMPATIBILITY:
4441       if (content->u.compatibility.child.type != GTK_TOOLBAR_CHILD_SPACE)
4442         {
4443           gtk_widget_size_allocate (content->u.compatibility.child.widget,
4444                                     allocation);
4445         }
4446       else
4447         {
4448           content->u.compatibility.space_allocation = *allocation;
4449         }
4450       break;
4451     }
4452 }
4453
4454 static void
4455 toolbar_content_set_state (ToolbarContent *content,
4456                            ItemState       state)
4457 {
4458   content->state = state;
4459 }
4460
4461 static GtkWidget *
4462 toolbar_content_get_widget (ToolbarContent *content)
4463 {
4464   GtkToolbarChild *child;
4465   
4466   switch (content->type)
4467     {
4468     case TOOL_ITEM:
4469       return GTK_WIDGET (content->u.tool_item.item);
4470       break;
4471       
4472     case COMPATIBILITY:
4473       child = &(content->u.compatibility.child);
4474       if (child->type != GTK_TOOLBAR_CHILD_SPACE)
4475         return child->widget;
4476       else
4477         return NULL;
4478       break;
4479     }
4480   
4481   return NULL;
4482 }
4483
4484 static void
4485 toolbar_content_set_disappearing (ToolbarContent *content,
4486                                   gboolean        disappearing)
4487 {
4488   switch (content->type)
4489     {
4490     case TOOL_ITEM:
4491       content->u.tool_item.disappearing = disappearing;
4492       break;
4493       
4494     case COMPATIBILITY:
4495       /* Only relevant for new API */
4496       g_assert_not_reached ();
4497       break;
4498     }
4499 }
4500
4501 static void
4502 toolbar_content_set_size_request (ToolbarContent *content,
4503                                   gint            width,
4504                                   gint            height)
4505 {
4506   switch (content->type)
4507     {
4508     case TOOL_ITEM:
4509       gtk_widget_set_size_request (GTK_WIDGET (content->u.tool_item.item),
4510                                    width, height);
4511       break;
4512       
4513     case COMPATIBILITY:
4514       /* Setting size requests only happens with sliding,
4515        * so not relevant here
4516        */
4517       g_assert_not_reached ();
4518       break;
4519     }
4520 }
4521
4522 static void
4523 toolbar_child_reconfigure (GtkToolbar      *toolbar,
4524                            GtkToolbarChild *child)
4525 {
4526   GtkWidget *box;
4527   GtkImage *image;
4528   GtkToolbarStyle style;
4529   GtkIconSize icon_size;
4530   GtkReliefStyle relief;
4531   gchar *stock_id;
4532   
4533   style = gtk_toolbar_get_style (toolbar);
4534   icon_size = gtk_toolbar_get_icon_size (toolbar);
4535   relief = gtk_toolbar_get_relief_style (toolbar);
4536   
4537   /* style */
4538   if (child->type == GTK_TOOLBAR_CHILD_BUTTON ||
4539       child->type == GTK_TOOLBAR_CHILD_RADIOBUTTON ||
4540       child->type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON)
4541     {
4542       box = gtk_bin_get_child (GTK_BIN (child->widget));
4543       
4544       if (style == GTK_TOOLBAR_BOTH && GTK_IS_HBOX (box))
4545         {
4546           GtkWidget *vbox;
4547           
4548           vbox = gtk_vbox_new (FALSE, 0);
4549           
4550           if (child->label)
4551             gtk_widget_reparent (child->label, vbox);
4552           if (child->icon)
4553             gtk_widget_reparent (child->icon, vbox);
4554           
4555           gtk_widget_destroy (box);
4556           gtk_container_add (GTK_CONTAINER (child->widget), vbox);
4557           
4558           gtk_widget_show (vbox);
4559         }
4560       else if (style == GTK_TOOLBAR_BOTH_HORIZ && GTK_IS_VBOX (box))
4561         {
4562           GtkWidget *hbox;
4563           
4564           hbox = gtk_hbox_new (FALSE, 0);
4565           
4566           if (child->label)
4567             gtk_widget_reparent (child->label, hbox);
4568           if (child->icon)
4569             gtk_widget_reparent (child->icon, hbox);
4570           
4571           gtk_widget_destroy (box);
4572           gtk_container_add (GTK_CONTAINER (child->widget), hbox);
4573           
4574           gtk_widget_show (hbox);
4575         }
4576
4577       set_child_packing_and_visibility (toolbar, child);
4578     }
4579   
4580   /* icon size */
4581   
4582   if ((child->type == GTK_TOOLBAR_CHILD_BUTTON ||
4583        child->type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON ||
4584        child->type == GTK_TOOLBAR_CHILD_RADIOBUTTON) &&
4585       GTK_IS_IMAGE (child->icon))
4586     {
4587       image = GTK_IMAGE (child->icon);
4588       if (gtk_image_get_storage_type (image) == GTK_IMAGE_STOCK)
4589         {
4590           gtk_image_get_stock (image, &stock_id, NULL);
4591           stock_id = g_strdup (stock_id);
4592           gtk_image_set_from_stock (image,
4593                                     stock_id,
4594                                     icon_size);
4595           g_free (stock_id);
4596         }
4597     }
4598   
4599   /* relief */
4600   if (child->type == GTK_TOOLBAR_CHILD_BUTTON ||
4601       child->type == GTK_TOOLBAR_CHILD_RADIOBUTTON ||
4602       child->type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON)
4603     {
4604       gtk_button_set_relief (GTK_BUTTON (child->widget), relief);
4605     }
4606 }
4607
4608 static void
4609 toolbar_content_toolbar_reconfigured (ToolbarContent *content,
4610                                       GtkToolbar     *toolbar)
4611 {
4612   switch (content->type)
4613     {
4614     case TOOL_ITEM:
4615       gtk_tool_item_toolbar_reconfigured (content->u.tool_item.item);
4616       break;
4617       
4618     case COMPATIBILITY:
4619       toolbar_child_reconfigure (toolbar, &(content->u.compatibility.child));
4620       break;
4621     }
4622 }
4623
4624 static GtkWidget *
4625 toolbar_content_retrieve_menu_item (ToolbarContent *content)
4626 {
4627   if (content->type == TOOL_ITEM)
4628     return gtk_tool_item_retrieve_proxy_menu_item (content->u.tool_item.item);
4629   
4630   /* FIXME - we might actually be able to do something meaningful here */
4631   return NULL; 
4632 }
4633
4634 static gboolean
4635 toolbar_content_has_proxy_menu_item (ToolbarContent *content)
4636 {
4637   if (content->type == TOOL_ITEM)
4638     {
4639       GtkWidget *menu_item;
4640
4641       if (content->u.tool_item.has_menu == YES)
4642         return TRUE;
4643       else if (content->u.tool_item.has_menu == NO)
4644         return FALSE;
4645
4646       menu_item = toolbar_content_retrieve_menu_item (content);
4647
4648       content->u.tool_item.has_menu = menu_item? YES : NO;
4649       
4650       return menu_item != NULL;
4651     }
4652   else
4653     {
4654       return FALSE;
4655     }
4656 }
4657
4658 static void
4659 toolbar_content_set_unknown_menu_status (ToolbarContent *content)
4660 {
4661   if (content->type == TOOL_ITEM)
4662     content->u.tool_item.has_menu = UNKNOWN;
4663 }
4664
4665 static gboolean
4666 toolbar_content_is_separator (ToolbarContent *content)
4667 {
4668   GtkToolbarChild *child;
4669   
4670   switch (content->type)
4671     {
4672     case TOOL_ITEM:
4673       return GTK_IS_SEPARATOR_TOOL_ITEM (content->u.tool_item.item);
4674       break;
4675       
4676     case COMPATIBILITY:
4677       child = &(content->u.compatibility.child);
4678       return (child->type == GTK_TOOLBAR_CHILD_SPACE);
4679       break;
4680     }
4681   
4682   return FALSE;
4683 }
4684
4685 static void
4686 toolbar_content_set_expand (ToolbarContent *content,
4687                             gboolean        expand)
4688 {
4689   if (content->type == TOOL_ITEM)
4690     gtk_tool_item_set_expand (content->u.tool_item.item, expand);
4691 }
4692
4693 static gboolean
4694 ignore_show_and_hide_all (ToolbarContent *content)
4695 {
4696   if (content->type == COMPATIBILITY)
4697     {
4698       GtkToolbarChildType type = content->u.compatibility.child.type;
4699       
4700       if (type == GTK_TOOLBAR_CHILD_BUTTON ||
4701           type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON ||
4702           type == GTK_TOOLBAR_CHILD_RADIOBUTTON)
4703         {
4704           return TRUE;
4705         }
4706     }
4707   
4708   return FALSE;
4709 }
4710
4711 static void
4712 toolbar_content_show_all (ToolbarContent  *content)
4713 {
4714   GtkWidget *widget;
4715   
4716   if (ignore_show_and_hide_all (content))
4717     return;
4718
4719   widget = toolbar_content_get_widget (content);
4720   if (widget)
4721     gtk_widget_show_all (widget);
4722 }
4723
4724 static void
4725 toolbar_content_hide_all (ToolbarContent  *content)
4726 {
4727   GtkWidget *widget;
4728   
4729   if (ignore_show_and_hide_all (content))
4730     return;
4731
4732   widget = toolbar_content_get_widget (content);
4733   if (widget)
4734     gtk_widget_hide_all (widget);
4735 }
4736
4737 /*
4738  * Getters
4739  */
4740 static gint
4741 get_space_size (GtkToolbar *toolbar)
4742 {
4743   gint space_size = DEFAULT_SPACE_SIZE;
4744   
4745   if (toolbar)
4746     {
4747       gtk_widget_style_get (GTK_WIDGET (toolbar),
4748                             "space-size", &space_size,
4749                             NULL);
4750     }
4751   
4752   return space_size;
4753 }
4754
4755 static GtkToolbarSpaceStyle
4756 get_space_style (GtkToolbar *toolbar)
4757 {
4758   GtkToolbarSpaceStyle space_style = DEFAULT_SPACE_STYLE;
4759
4760   if (toolbar)
4761     {
4762       gtk_widget_style_get (GTK_WIDGET (toolbar),
4763                             "space-style", &space_style,
4764                             NULL);
4765     }
4766   
4767   return space_style;  
4768 }
4769
4770 static GtkReliefStyle
4771 get_button_relief (GtkToolbar *toolbar)
4772 {
4773   GtkReliefStyle button_relief = GTK_RELIEF_NORMAL;
4774   
4775   gtk_widget_ensure_style (GTK_WIDGET (toolbar));
4776   
4777   gtk_widget_style_get (GTK_WIDGET (toolbar),
4778                         "button-relief", &button_relief,
4779                         NULL);
4780   
4781   return button_relief;
4782 }
4783
4784 static gint
4785 get_internal_padding (GtkToolbar *toolbar)
4786 {
4787   gint ipadding = 0;
4788   
4789   gtk_widget_style_get (GTK_WIDGET (toolbar),
4790                         "internal-padding", &ipadding,
4791                         NULL);
4792   
4793   return ipadding;
4794 }
4795
4796 static gint
4797 get_max_child_expand (GtkToolbar *toolbar)
4798 {
4799   gint mexpand = G_MAXINT;
4800
4801   gtk_widget_style_get (GTK_WIDGET (toolbar),
4802                         "max-child-expand", &mexpand,
4803                         NULL);
4804   return mexpand;
4805 }
4806
4807 static GtkShadowType
4808 get_shadow_type (GtkToolbar *toolbar)
4809 {
4810   GtkShadowType shadow_type;
4811   
4812   gtk_widget_style_get (GTK_WIDGET (toolbar),
4813                         "shadow-type", &shadow_type,
4814                         NULL);
4815   
4816   return shadow_type;
4817 }
4818
4819 /*
4820  * API checks
4821  */
4822 static gboolean
4823 gtk_toolbar_check_old_api (GtkToolbar *toolbar)
4824 {
4825   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
4826   
4827   if (priv->api_mode == NEW_API)
4828     {
4829       g_warning (MIXED_API_WARNING);
4830       return FALSE;
4831     }
4832   
4833   priv->api_mode = OLD_API;
4834   return TRUE;
4835 }
4836
4837 static gboolean
4838 gtk_toolbar_check_new_api (GtkToolbar *toolbar)
4839 {
4840   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
4841   
4842   if (priv->api_mode == OLD_API)
4843     {
4844       g_warning (MIXED_API_WARNING);
4845       return FALSE;
4846     }
4847   
4848   priv->api_mode = NEW_API;
4849   return TRUE;
4850 }
4851
4852 /* GTK+ internal methods */
4853
4854 gint
4855 _gtk_toolbar_get_default_space_size (void)
4856 {
4857   return DEFAULT_SPACE_SIZE;
4858 }
4859
4860 void
4861 _gtk_toolbar_paint_space_line (GtkWidget           *widget,
4862                                GtkToolbar          *toolbar,
4863                                const GdkRectangle  *area,
4864                                const GtkAllocation *allocation)
4865 {
4866   const double start_fraction = (SPACE_LINE_START / SPACE_LINE_DIVISION);
4867   const double end_fraction = (SPACE_LINE_END / SPACE_LINE_DIVISION);
4868   
4869   GtkOrientation orientation;
4870
4871   g_return_if_fail (GTK_IS_WIDGET (widget));
4872   
4873   orientation = toolbar? toolbar->orientation : GTK_ORIENTATION_HORIZONTAL;
4874
4875   if (orientation == GTK_ORIENTATION_HORIZONTAL)
4876     {
4877       gboolean wide_separators;
4878       gint     separator_width;
4879
4880       gtk_widget_style_get (widget,
4881                             "wide-separators", &wide_separators,
4882                             "separator-width", &separator_width,
4883                             NULL);
4884
4885       if (wide_separators)
4886         gtk_paint_box (widget->style, widget->window,
4887                        GTK_WIDGET_STATE (widget), GTK_SHADOW_ETCHED_OUT,
4888                        area, widget, "vseparator",
4889                        allocation->x + (allocation->width - separator_width) / 2,
4890                        allocation->y + allocation->height * start_fraction,
4891                        separator_width,
4892                        allocation->height * (end_fraction - start_fraction));
4893       else
4894         gtk_paint_vline (widget->style, widget->window,
4895                          GTK_WIDGET_STATE (widget), area, widget,
4896                          "toolbar",
4897                          allocation->y + allocation->height * start_fraction,
4898                          allocation->y + allocation->height * end_fraction,
4899                          allocation->x + (allocation->width - widget->style->xthickness) / 2);
4900     }
4901   else
4902     {
4903       gboolean wide_separators;
4904       gint     separator_height;
4905
4906       gtk_widget_style_get (widget,
4907                             "wide-separators",  &wide_separators,
4908                             "separator-height", &separator_height,
4909                             NULL);
4910
4911       if (wide_separators)
4912         gtk_paint_box (widget->style, widget->window,
4913                        GTK_WIDGET_STATE (widget), GTK_SHADOW_ETCHED_OUT,
4914                        area, widget, "hseparator",
4915                        allocation->x + allocation->width * start_fraction,
4916                        allocation->y + (allocation->height - separator_height) / 2,
4917                        allocation->width * (end_fraction - start_fraction),
4918                        separator_height);
4919       else
4920         gtk_paint_hline (widget->style, widget->window,
4921                          GTK_WIDGET_STATE (widget), area, widget,
4922                          "toolbar",
4923                          allocation->x + allocation->width * start_fraction,
4924                          allocation->x + allocation->width * end_fraction,
4925                          allocation->y + (allocation->height - widget->style->ythickness) / 2);
4926     }
4927 }
4928
4929 gchar *
4930 _gtk_toolbar_elide_underscores (const gchar *original)
4931 {
4932   gchar *q, *result;
4933   const gchar *p, *end;
4934   gsize len;
4935   gboolean last_underscore;
4936   
4937   if (!original)
4938     return NULL;
4939
4940   len = strlen (original);
4941   q = result = g_malloc (len + 1);
4942   last_underscore = FALSE;
4943   
4944   end = original + len;
4945   for (p = original; p < end; p++)
4946     {
4947       if (!last_underscore && *p == '_')
4948         last_underscore = TRUE;
4949       else
4950         {
4951           last_underscore = FALSE;
4952           if (original + 2 <= p && p + 1 <= end && 
4953               p[-2] == '(' && p[-1] == '_' && p[0] != '_' && p[1] == ')')
4954             {
4955               q--;
4956               *q = '\0';
4957               p++;
4958             }
4959           else
4960             *q++ = *p;
4961         }
4962     }
4963
4964   if (last_underscore)
4965     *q++ = '_';
4966   
4967   *q = '\0';
4968   
4969   return result;
4970 }
4971
4972 static GtkIconSize
4973 toolbar_get_icon_size (GtkToolShell *shell)
4974 {
4975   return GTK_TOOLBAR (shell)->icon_size;
4976 }
4977
4978 static GtkOrientation
4979 toolbar_get_orientation (GtkToolShell *shell)
4980 {
4981   return GTK_TOOLBAR (shell)->orientation;
4982 }
4983
4984 static GtkToolbarStyle
4985 toolbar_get_style (GtkToolShell *shell)
4986 {
4987   return GTK_TOOLBAR (shell)->style;
4988 }
4989
4990 static GtkReliefStyle
4991 toolbar_get_relief_style (GtkToolShell *shell)
4992 {
4993   return get_button_relief (GTK_TOOLBAR (shell));
4994 }
4995
4996 static void
4997 toolbar_rebuild_menu (GtkToolShell *shell)
4998 {
4999   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (shell);
5000   GList *list;
5001
5002   priv->need_rebuild = TRUE;
5003
5004   for (list = priv->content; list != NULL; list = list->next)
5005     {
5006       ToolbarContent *content = list->data;
5007
5008       toolbar_content_set_unknown_menu_status (content);
5009     }
5010   
5011   gtk_widget_queue_resize (GTK_WIDGET (shell));
5012 }
5013
5014 #define __GTK_TOOLBAR_C__
5015 #include "gtkaliasdef.c"