]> Pileus Git - ~andy/gtk/blob - gtk/gtktoolbar.c
fix a type check, (#116947, Krasimir Angelov)
[~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@codefactory.se>
6  * Copyright (C) 2002 James Henstridge <james@daa.com.au>
7  * Copyright (C) 2003 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 "gtkarrow.h"
35 #include "gtktoolbar.h"
36 #include "gtkradiotoolbutton.h"
37 #include "gtkseparatortoolitem.h"
38 #include "gtkmenu.h"
39 #include "gtkradiobutton.h"
40 #include "gtktoolbar.h"
41 #include "gtkbindings.h"
42 #include <gdk/gdkkeysyms.h>
43 #include "gtkmarshalers.h"
44 #include "gtkmain.h"
45 #include "gtkstock.h"
46 #include "gtklabel.h"
47 #include "gtkprivate.h"
48 #include "gtkintl.h"
49 #include <string.h>
50
51 #define DEFAULT_IPADDING 0
52
53 /* note: keep in sync with DEFAULT_SPACE_SIZE and DEFAULT_SPACE_STYLE in gtkseparatortoolitem.c */
54 #define DEFAULT_SPACE_SIZE  5
55 #define DEFAULT_SPACE_STYLE GTK_TOOLBAR_SPACE_LINE
56
57 #define DEFAULT_ICON_SIZE GTK_ICON_SIZE_LARGE_TOOLBAR
58 #define DEFAULT_TOOLBAR_STYLE GTK_TOOLBAR_BOTH
59
60 enum {
61   PROP_0,
62   PROP_ORIENTATION,
63   PROP_TOOLBAR_STYLE,
64   PROP_SHOW_ARROW
65 };
66
67 enum {
68   CHILD_PROP_0,
69   CHILD_PROP_EXPAND,
70   CHILD_PROP_HOMOGENEOUS,
71   CHILD_PROP_PACK_END,
72 };
73
74 enum {
75   ORIENTATION_CHANGED,
76   STYLE_CHANGED,
77   POPUP_CONTEXT_MENU,
78   MOVE_FOCUS,
79   FOCUS_HOME_OR_END,
80   LAST_SIGNAL
81 };
82
83 static void gtk_toolbar_init       (GtkToolbar      *toolbar);
84 static void gtk_toolbar_class_init (GtkToolbarClass *klass);
85
86 static void gtk_toolbar_set_property (GObject      *object,
87                                       guint         prop_id,
88                                       const GValue *value,
89                                       GParamSpec   *pspec);
90 static void gtk_toolbar_get_property (GObject      *object,
91                                       guint         prop_id,
92                                       GValue       *value,
93                                       GParamSpec   *pspec);
94
95 static gint     gtk_toolbar_expose         (GtkWidget        *widget,
96                                             GdkEventExpose   *event);
97 static void     gtk_toolbar_realize        (GtkWidget        *widget);
98 static void     gtk_toolbar_unrealize      (GtkWidget        *widget);
99 static void     gtk_toolbar_size_request   (GtkWidget        *widget,
100                                             GtkRequisition   *requisition);
101 static void     gtk_toolbar_size_allocate  (GtkWidget        *widget,
102                                             GtkAllocation    *allocation);
103 static void     gtk_toolbar_style_set      (GtkWidget        *widget,
104                                             GtkStyle         *prev_style);
105 static void     gtk_toolbar_direction_changed (GtkWidget        *widget,
106                                                GtkTextDirection  previous_direction);
107 static gboolean gtk_toolbar_focus          (GtkWidget        *widget,
108                                             GtkDirectionType  dir);
109 static void     gtk_toolbar_screen_changed (GtkWidget        *widget,
110                                             GdkScreen        *previous_screen);
111 static void     gtk_toolbar_map            (GtkWidget        *widget);
112 static void     gtk_toolbar_unmap          (GtkWidget        *widget);
113
114 static void     gtk_toolbar_drag_leave  (GtkWidget      *widget,
115                                          GdkDragContext *context,
116                                          guint           time_);
117 static gboolean gtk_toolbar_drag_motion (GtkWidget      *widget,
118                                          GdkDragContext *context,
119                                          gint            x,
120                                          gint            y,
121                                          guint           time_);
122 static void     gtk_toolbar_set_child_property (GtkContainer    *container,
123                                                 GtkWidget       *child,
124                                                 guint            property_id,
125                                                 const GValue    *value,
126                                                 GParamSpec      *pspec);
127 static void    gtk_toolbar_get_child_property (GtkContainer    *container,
128                                                GtkWidget       *child,
129                                                guint            property_id,
130                                                GValue          *value,
131                                                GParamSpec      *pspec);
132 static void gtk_toolbar_finalize (GObject *object);
133
134
135 static void  gtk_toolbar_add        (GtkContainer *container,
136                                      GtkWidget    *widget);
137 static void  gtk_toolbar_remove     (GtkContainer *container,
138                                      GtkWidget    *widget);
139 static void  gtk_toolbar_forall     (GtkContainer *container,
140                                      gboolean      include_internals,
141                                      GtkCallback   callback,
142                                      gpointer      callback_data);
143 static GType gtk_toolbar_child_type (GtkContainer *container);
144
145 static void gtk_toolbar_real_orientation_changed (GtkToolbar      *toolbar,
146                                                   GtkOrientation   orientation);
147 static void gtk_toolbar_real_style_changed       (GtkToolbar      *toolbar,
148                                                   GtkToolbarStyle  style);
149
150 static gboolean gtk_toolbar_move_focus        (GtkToolbar       *toolbar,
151                                                GtkDirectionType  dir);
152 static gboolean gtk_toolbar_focus_home_or_end (GtkToolbar       *toolbar,
153                                                gboolean          focus_home);
154
155 static gboolean             gtk_toolbar_button_press         (GtkWidget      *toolbar,
156                                                               GdkEventButton *event);
157 static gboolean             gtk_toolbar_arrow_button_press (GtkWidget      *button,
158                                                               GdkEventButton *event,
159                                                               GtkToolbar     *toolbar);
160 static void                 gtk_toolbar_arrow_button_clicked (GtkWidget      *button,
161                                                                GtkToolbar     *toolbar);
162 static void                 gtk_toolbar_update_button_relief (GtkToolbar     *toolbar);
163 static GtkReliefStyle       get_button_relief                (GtkToolbar     *toolbar);
164 static gint                 get_space_size                   (GtkToolbar     *toolbar);
165 static gint                 get_internal_padding             (GtkToolbar     *toolbar);
166 static void                 gtk_toolbar_remove_tool_item     (GtkToolbar     *toolbar,
167                                                               GtkToolItem    *item);
168 static gboolean             gtk_toolbar_popup_menu           (GtkWidget      *toolbar);
169
170 static GtkWidget *gtk_toolbar_internal_insert_element (GtkToolbar          *toolbar,
171                                                        GtkToolbarChildType  type,
172                                                        GtkWidget           *widget,
173                                                        const char          *text,
174                                                        const char          *tooltip_text,
175                                                        const char          *tooltip_private_text,
176                                                        GtkWidget           *icon,
177                                                        GtkSignalFunc        callback,
178                                                        gpointer             user_data,
179                                                        gint                 position,
180                                                        gboolean             use_stock);
181
182
183 typedef enum {
184   DONT_KNOW,
185   OLD_API,
186   NEW_API
187 } ApiMode;
188
189 struct _GtkToolbarPrivate
190 {
191   GList     *items;
192   
193   GtkWidget *arrow;
194   GtkWidget *arrow_button;
195   
196   gboolean   show_arrow;
197
198   gint       drop_index;
199   GdkWindow *drag_highlight;
200   GtkMenu   *menu;
201
202   GdkWindow *event_window;
203   ApiMode    api_mode;
204   GtkSettings *settings;
205 };
206
207 static GtkContainerClass *parent_class = NULL;
208 static guint toolbar_signals [LAST_SIGNAL] = { 0 };
209
210 GType
211 gtk_toolbar_get_type (void)
212 {
213   static GtkType type = 0;
214
215   if (!type)
216     {
217       static const GTypeInfo type_info =
218         {
219           sizeof (GtkToolbarClass),
220           (GBaseInitFunc) NULL,
221           (GBaseFinalizeFunc) NULL,
222           (GClassInitFunc) gtk_toolbar_class_init,
223           (GClassFinalizeFunc) NULL,
224           NULL,
225           sizeof (GtkToolbar),
226           0, /* n_preallocs */
227           (GInstanceInitFunc) gtk_toolbar_init,
228         };
229
230       type = g_type_register_static (GTK_TYPE_CONTAINER,
231                                      "GtkToolbar",
232                                      &type_info, 0);
233     }
234   
235   return type;
236 }
237
238 static void
239 add_arrow_bindings (GtkBindingSet   *binding_set,
240                     guint            keysym,
241                     GtkDirectionType dir)
242 {
243   guint keypad_keysym = keysym - GDK_Left + GDK_KP_Left;
244   
245   gtk_binding_entry_add_signal (binding_set, keysym, 0,
246                                 "move_focus", 1,
247                                 GTK_TYPE_DIRECTION_TYPE, dir);
248   gtk_binding_entry_add_signal (binding_set, keypad_keysym, 0,
249                                 "move_focus", 1,
250                                 GTK_TYPE_DIRECTION_TYPE, dir);
251 }
252
253 static void
254 add_ctrl_tab_bindings (GtkBindingSet    *binding_set,
255                        GdkModifierType   modifiers,
256                        GtkDirectionType  direction)
257 {
258   gtk_binding_entry_add_signal (binding_set,
259                                 GDK_Tab, GDK_CONTROL_MASK | modifiers,
260                                 "move_focus", 1,
261                                 GTK_TYPE_DIRECTION_TYPE, direction);
262   gtk_binding_entry_add_signal (binding_set,
263                                 GDK_KP_Tab, GDK_CONTROL_MASK | modifiers,
264                                 "move_focus", 1,
265                                 GTK_TYPE_DIRECTION_TYPE, direction);
266 }
267
268 static void
269 gtk_toolbar_class_init (GtkToolbarClass *klass)
270 {
271   GObjectClass *gobject_class;
272   GtkWidgetClass *widget_class;
273   GtkContainerClass *container_class;
274   GtkBindingSet *binding_set;
275
276   parent_class = g_type_class_peek_parent (klass);
277   
278   gobject_class = (GObjectClass *)klass;
279   widget_class = (GtkWidgetClass *)klass;
280   container_class = (GtkContainerClass *)klass;
281   
282   gobject_class->set_property = gtk_toolbar_set_property;
283   gobject_class->get_property = gtk_toolbar_get_property;
284   gobject_class->finalize = gtk_toolbar_finalize;
285
286   widget_class->button_press_event = gtk_toolbar_button_press;
287   widget_class->expose_event = gtk_toolbar_expose;
288   widget_class->size_request = gtk_toolbar_size_request;
289   widget_class->size_allocate = gtk_toolbar_size_allocate;
290   widget_class->style_set = gtk_toolbar_style_set;
291   widget_class->direction_changed = gtk_toolbar_direction_changed;
292   widget_class->focus = gtk_toolbar_focus;
293   widget_class->screen_changed = gtk_toolbar_screen_changed;
294   widget_class->realize = gtk_toolbar_realize;
295   widget_class->unrealize = gtk_toolbar_unrealize;
296   widget_class->map = gtk_toolbar_map;
297   widget_class->unmap = gtk_toolbar_unmap;
298   widget_class->popup_menu = gtk_toolbar_popup_menu;
299   
300   widget_class->drag_leave = gtk_toolbar_drag_leave;
301   widget_class->drag_motion = gtk_toolbar_drag_motion;
302   
303   container_class->add    = gtk_toolbar_add;
304   container_class->remove = gtk_toolbar_remove;
305   container_class->forall = gtk_toolbar_forall;
306   container_class->child_type = gtk_toolbar_child_type;
307   container_class->get_child_property = gtk_toolbar_get_child_property;
308   container_class->set_child_property = gtk_toolbar_set_child_property;
309   
310   klass->orientation_changed = gtk_toolbar_real_orientation_changed;
311   klass->style_changed = gtk_toolbar_real_style_changed;
312   
313   toolbar_signals[ORIENTATION_CHANGED] =
314     g_signal_new ("orientation_changed",
315                   G_OBJECT_CLASS_TYPE (klass),
316                   G_SIGNAL_RUN_FIRST,
317                   G_STRUCT_OFFSET (GtkToolbarClass, orientation_changed),
318                   NULL, NULL,
319                   g_cclosure_marshal_VOID__ENUM,
320                   G_TYPE_NONE, 1,
321                   GTK_TYPE_ORIENTATION);
322   toolbar_signals[STYLE_CHANGED] =
323     g_signal_new ("style_changed",
324                   G_OBJECT_CLASS_TYPE (klass),
325                   G_SIGNAL_RUN_FIRST,
326                   G_STRUCT_OFFSET (GtkToolbarClass, style_changed),
327                   NULL, NULL,
328                   g_cclosure_marshal_VOID__ENUM,
329                   G_TYPE_NONE, 1,
330                   GTK_TYPE_TOOLBAR_STYLE);
331   toolbar_signals[POPUP_CONTEXT_MENU] =
332     g_signal_new ("popup_context_menu",
333                   G_OBJECT_CLASS_TYPE (klass),
334                   G_SIGNAL_RUN_LAST,
335                   G_STRUCT_OFFSET (GtkToolbarClass, popup_context_menu),
336                   _gtk_boolean_handled_accumulator, NULL,
337                   _gtk_marshal_BOOLEAN__INT_INT_INT,
338                   G_TYPE_BOOLEAN, 3,
339                   G_TYPE_INT, G_TYPE_INT,
340                   G_TYPE_INT);
341   toolbar_signals[MOVE_FOCUS] =
342     _gtk_binding_signal_new ("move_focus",
343                              G_TYPE_FROM_CLASS (klass),
344                              G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
345                              G_CALLBACK (gtk_toolbar_move_focus),
346                              NULL, NULL,
347                              _gtk_marshal_BOOLEAN__ENUM,
348                              G_TYPE_BOOLEAN, 1,
349                              GTK_TYPE_DIRECTION_TYPE);
350   toolbar_signals[FOCUS_HOME_OR_END] =
351     _gtk_binding_signal_new ("focus_home_or_end",
352                              G_OBJECT_CLASS_TYPE (klass),
353                              G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
354                              G_CALLBACK (gtk_toolbar_focus_home_or_end),
355                              NULL, NULL,
356                              _gtk_marshal_BOOLEAN__BOOLEAN,
357                              G_TYPE_BOOLEAN, 1,
358                              G_TYPE_BOOLEAN);
359
360   /* properties */
361   g_object_class_install_property (gobject_class,
362                                    PROP_ORIENTATION,
363                                    g_param_spec_enum ("orientation",
364                                                       _("Orientation"),
365                                                       _("The orientation of the toolbar"),
366                                                       GTK_TYPE_ORIENTATION,
367                                                       GTK_ORIENTATION_HORIZONTAL,
368                                                       G_PARAM_READWRITE));
369
370   g_object_class_install_property (gobject_class,
371                                    PROP_TOOLBAR_STYLE,
372                                    g_param_spec_enum ("toolbar_style",
373                                                       _("Toolbar Style"),
374                                                       _("How to draw the toolbar"),
375                                                       GTK_TYPE_TOOLBAR_STYLE,
376                                                       GTK_TOOLBAR_ICONS,
377                                                       G_PARAM_READWRITE));
378   g_object_class_install_property (gobject_class,
379                                    PROP_SHOW_ARROW,
380                                    g_param_spec_boolean ("show_arrow",
381                                                          _("Show Arrow"),
382                                                          _("If an arrow should be shown if the toolbar doesn't fit"),
383                                                          FALSE,
384                                                          G_PARAM_READWRITE));
385
386   /* child properties */
387   gtk_container_class_install_child_property (container_class,
388                                               CHILD_PROP_EXPAND,
389                                               g_param_spec_boolean ("expand", 
390                                                                     _("Expand"), 
391                                                                     _("Whether the item should receive extra space when the toolbar grows"),
392                                                                     TRUE,
393                                                                     G_PARAM_READWRITE));
394
395   gtk_container_class_install_child_property (container_class,
396                                               CHILD_PROP_HOMOGENEOUS,
397                                               g_param_spec_boolean ("homogeneous", 
398                                                                     _("Homogeneous"), 
399                                                                     _("Whether the item should be the same size as other homogeneous items"),
400                                                                     TRUE,
401                                                                     G_PARAM_READWRITE));
402
403   gtk_container_class_install_child_property (container_class,
404                                               CHILD_PROP_PACK_END,
405                                               g_param_spec_uint ("pack_end", 
406                                                                  _("Pack End"), 
407                                                                  _("Whether the item is positioned at the end of the toolbar"),
408                                                                  0, G_MAXINT, 0,
409                                                                  G_PARAM_READWRITE));
410
411   /* style properties */
412   gtk_widget_class_install_style_property (widget_class,
413                                            g_param_spec_int ("space_size",
414                                                              _("Spacer size"),
415                                                              _("Size of spacers"),
416                                                              0,
417                                                              G_MAXINT,
418                                                              DEFAULT_SPACE_SIZE,
419                                                              G_PARAM_READABLE));
420   
421   gtk_widget_class_install_style_property (widget_class,
422                                            g_param_spec_int ("internal_padding",
423                                                              _("Internal padding"),
424                                                              _("Amount of border space between the toolbar shadow and the buttons"),
425                                                              0,
426                                                              G_MAXINT,
427                                                              DEFAULT_IPADDING,
428                                                              G_PARAM_READABLE));
429
430   gtk_widget_class_install_style_property (widget_class,
431                                            g_param_spec_enum ("space_style",
432                                                              _("Space style"),
433                                                              _("Whether spacers are vertical lines or just blank"),
434                                                               GTK_TYPE_TOOLBAR_SPACE_STYLE,
435                                                               DEFAULT_SPACE_STYLE,
436                                                               G_PARAM_READABLE));
437   
438   gtk_widget_class_install_style_property (widget_class,
439                                            g_param_spec_enum ("button_relief",
440                                                               _("Button relief"),
441                                                               _("Type of bevel around toolbar buttons"),
442                                                               GTK_TYPE_RELIEF_STYLE,
443                                                               GTK_RELIEF_NONE,
444                                                               G_PARAM_READABLE));
445   gtk_widget_class_install_style_property (widget_class,
446                                            g_param_spec_enum ("shadow_type",
447                                                               _("Shadow type"),
448                                                               _("Style of bevel around the toolbar"),
449                                                               GTK_TYPE_SHADOW_TYPE,
450                                                               GTK_SHADOW_OUT,
451                                                               G_PARAM_READABLE));
452
453   gtk_settings_install_property (g_param_spec_enum ("gtk-toolbar-style",
454                                                     _("Toolbar style"),
455                                                     _("Whether default toolbars have text only, text and icons, icons only, etc."),
456                                                     GTK_TYPE_TOOLBAR_STYLE,
457                                                     DEFAULT_TOOLBAR_STYLE,
458                                                     G_PARAM_READWRITE));
459
460   gtk_settings_install_property (g_param_spec_enum ("gtk-toolbar-icon-size",
461                                                     _("Toolbar icon size"),
462                                                     _("Size of icons in default toolbars"),
463                                                     GTK_TYPE_ICON_SIZE,
464                                                     DEFAULT_ICON_SIZE,
465                                                     G_PARAM_READWRITE));  
466
467   binding_set = gtk_binding_set_by_class (klass);
468
469   add_arrow_bindings (binding_set, GDK_Left, GTK_DIR_LEFT);
470   add_arrow_bindings (binding_set, GDK_Right, GTK_DIR_RIGHT);
471   add_arrow_bindings (binding_set, GDK_Up, GTK_DIR_UP);
472   add_arrow_bindings (binding_set, GDK_Down, GTK_DIR_DOWN);
473
474   gtk_binding_entry_add_signal (binding_set, GDK_KP_Home, 0,
475                                 "focus_home_or_end", 1,
476                                 G_TYPE_BOOLEAN, TRUE);
477   gtk_binding_entry_add_signal (binding_set, GDK_Home, 0,
478                                 "focus_home_or_end", 1,
479                                 G_TYPE_BOOLEAN, TRUE);
480   gtk_binding_entry_add_signal (binding_set, GDK_KP_End, 0,
481                                 "focus_home_or_end", 1,
482                                 G_TYPE_BOOLEAN, FALSE);
483   gtk_binding_entry_add_signal (binding_set, GDK_End, 0,
484                                 "focus_home_or_end", 1,
485                                 G_TYPE_BOOLEAN, FALSE);
486
487   add_ctrl_tab_bindings (binding_set, 0, GTK_DIR_TAB_FORWARD);
488   add_ctrl_tab_bindings (binding_set, GDK_SHIFT_MASK, GTK_DIR_TAB_BACKWARD);
489
490   g_type_class_add_private (gobject_class, sizeof (GtkToolbarPrivate));  
491 }
492
493 static void
494 gtk_toolbar_init (GtkToolbar *toolbar)
495 {
496   GtkToolbarPrivate *priv;
497   
498   GTK_WIDGET_UNSET_FLAGS (toolbar, GTK_CAN_FOCUS);
499   GTK_WIDGET_SET_FLAGS (toolbar, GTK_NO_WINDOW);
500
501   priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
502   
503   toolbar->orientation = GTK_ORIENTATION_HORIZONTAL;
504   toolbar->style = DEFAULT_TOOLBAR_STYLE;
505   toolbar->icon_size = DEFAULT_ICON_SIZE;
506   toolbar->tooltips = gtk_tooltips_new ();
507   g_object_ref (toolbar->tooltips);
508   gtk_object_sink (GTK_OBJECT (toolbar->tooltips));
509   
510   priv->arrow_button = gtk_toggle_button_new ();
511   g_signal_connect (priv->arrow_button, "button_press_event",
512                     G_CALLBACK (gtk_toolbar_arrow_button_press), toolbar);
513   g_signal_connect (priv->arrow_button, "clicked",
514                     G_CALLBACK (gtk_toolbar_arrow_button_clicked), toolbar);
515   gtk_button_set_relief (GTK_BUTTON (priv->arrow_button),
516                          get_button_relief (toolbar));
517
518   priv->api_mode = DONT_KNOW;
519   
520   gtk_button_set_focus_on_click (GTK_BUTTON (priv->arrow_button), FALSE);
521   
522   priv->arrow = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_NONE);
523   gtk_widget_show (priv->arrow);
524   gtk_container_add (GTK_CONTAINER (priv->arrow_button), priv->arrow);
525   
526   gtk_widget_set_parent (priv->arrow_button, GTK_WIDGET (toolbar));
527
528   /* which child position a drop will occur at */
529   priv->drop_index = -1;
530   priv->drag_highlight = NULL;
531
532   priv->menu = NULL;
533
534   priv->settings = NULL;
535 }
536
537 static gboolean
538 toolbar_item_visible (GtkToolbar  *toolbar,
539                       GtkToolItem *item)
540 {
541   if (GTK_WIDGET_VISIBLE (item) &&
542       ((toolbar->orientation == GTK_ORIENTATION_HORIZONTAL &&
543         gtk_tool_item_get_visible_horizontal (item)) ||
544        (toolbar->orientation == GTK_ORIENTATION_VERTICAL &&
545         gtk_tool_item_get_visible_vertical (item))))
546     {
547       GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
548       
549       /* With the old toolbar you could hide a button by calling gtk_widget_hide()
550        * on it. This doesn't work with the new API because the GtkToolItem will not be
551        * hidden.
552        */
553       if (priv->api_mode == OLD_API)
554         {
555           GtkWidget *bin_child = GTK_BIN (item)->child;
556           
557           if (bin_child && !GTK_WIDGET_VISIBLE (bin_child))
558             return FALSE;
559         }
560       
561       return TRUE;
562     }
563   
564   return FALSE;
565 }
566
567 static void
568 toolbar_item_set_is_overflow (GtkToolItem *item,
569                               gboolean     is_overflow)
570 {
571   g_object_set_data (G_OBJECT (item), "gtk-toolbar-item-is-overflow", GINT_TO_POINTER (is_overflow));
572 }
573
574 static gboolean
575 toolbar_item_get_is_overflow (GtkToolItem *item)
576 {
577   gpointer result;
578
579   result = g_object_get_data (G_OBJECT (item), "gtk-toolbar-item-is-overflow");
580
581   return GPOINTER_TO_INT (result);
582 }
583
584 static void
585 gtk_toolbar_set_property (GObject     *object,
586                           guint        prop_id,
587                           const GValue *value,
588                           GParamSpec   *pspec)
589 {
590   GtkToolbar *toolbar = GTK_TOOLBAR (object);
591
592   switch (prop_id)
593     {
594     case PROP_ORIENTATION:
595       gtk_toolbar_set_orientation (toolbar, g_value_get_enum (value));
596       break;
597     case PROP_TOOLBAR_STYLE:
598       gtk_toolbar_set_style (toolbar, g_value_get_enum (value));
599       break;
600     case PROP_SHOW_ARROW:
601       gtk_toolbar_set_show_arrow (toolbar, g_value_get_boolean (value));
602       break;
603     default:
604       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
605       break;
606     }
607 }
608
609 static void
610 gtk_toolbar_get_property (GObject    *object,
611                           guint       prop_id,
612                           GValue     *value,
613                           GParamSpec *pspec)
614 {
615   GtkToolbar *toolbar = GTK_TOOLBAR (object);
616   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
617   
618   switch (prop_id)
619     {
620     case PROP_ORIENTATION:
621       g_value_set_enum (value, toolbar->orientation);
622       break;
623     case PROP_TOOLBAR_STYLE:
624       g_value_set_enum (value, toolbar->style);
625       break;
626     case PROP_SHOW_ARROW:
627       g_value_set_boolean (value, priv->show_arrow);
628       break;
629     default:
630       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
631       break;
632     }
633 }
634
635 static void
636 gtk_toolbar_map (GtkWidget *widget)
637 {
638   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (widget);
639
640   GTK_WIDGET_CLASS (parent_class)->map (widget);
641
642   if (priv->event_window)
643     gdk_window_show_unraised (priv->event_window);
644 }
645
646 static void
647 gtk_toolbar_unmap (GtkWidget *widget)
648 {
649   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (widget);
650
651   if (priv->event_window)
652     gdk_window_hide (priv->event_window);
653
654   GTK_WIDGET_CLASS (parent_class)->unmap (widget);
655 }
656
657 static void
658 gtk_toolbar_realize (GtkWidget *widget)
659 {
660   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
661   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
662
663   GdkWindowAttr attributes;
664   gint attributes_mask;
665   gint border_width;
666
667   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
668
669   border_width = GTK_CONTAINER (widget)->border_width;
670
671   attributes.wclass = GDK_INPUT_ONLY;
672   attributes.window_type = GDK_WINDOW_CHILD;
673   attributes.x = widget->allocation.x + border_width;
674   attributes.y = widget->allocation.y + border_width;
675   attributes.width = widget->allocation.width - border_width * 2;
676   attributes.height = widget->allocation.height - border_width * 2;
677   attributes.event_mask = gtk_widget_get_events (widget);
678   /* FIXME: does GDK_EXPOSURE_MASK make sense for an input-only window?
679    * If it doesn't, then it should be removed here and in gtkbutton.c,
680    * gtkmenuitem.c, and maybe other places
681    */
682   attributes.event_mask |= (GDK_EXPOSURE_MASK |
683                             GDK_BUTTON_PRESS_MASK |
684                             GDK_BUTTON_RELEASE_MASK |
685                             GDK_ENTER_NOTIFY_MASK |
686                             GDK_LEAVE_NOTIFY_MASK);
687
688   attributes_mask = GDK_WA_X | GDK_WA_Y;
689
690   widget->window = gtk_widget_get_parent_window (widget);
691   g_object_ref (widget->window);
692   widget->style = gtk_style_attach (widget->style, widget->window);
693   
694   priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
695                                        &attributes, attributes_mask);
696   gdk_window_set_user_data (priv->event_window, toolbar);
697 }
698
699 static void
700 gtk_toolbar_unrealize (GtkWidget *widget)
701 {
702   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (widget);
703
704   if (priv->drag_highlight)
705     {
706       gdk_window_set_user_data (priv->drag_highlight, NULL);
707       gdk_window_destroy (priv->drag_highlight);
708       priv->drag_highlight = NULL;
709     }
710
711   if (priv->event_window)
712     {
713       gdk_window_set_user_data (priv->event_window, NULL);
714       gdk_window_destroy (priv->event_window);
715       priv->event_window = NULL;
716     }
717
718   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
719     (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
720 }
721
722 static gint
723 gtk_toolbar_expose (GtkWidget      *widget,
724                     GdkEventExpose *event)
725 {
726   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
727   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
728   
729   GList *items;
730   gint border_width;
731   
732   border_width = GTK_CONTAINER (widget)->border_width;
733   
734   if (GTK_WIDGET_DRAWABLE (widget))
735     {
736       GtkShadowType shadow_type;
737
738       gtk_widget_style_get (widget, "shadow_type", &shadow_type, NULL);
739       
740       gtk_paint_box (widget->style,
741                      widget->window,
742                      GTK_WIDGET_STATE (widget),
743                      shadow_type,
744                      &event->area, widget, "toolbar",
745                      border_width + widget->allocation.x,
746                      border_width + widget->allocation.y,
747                      widget->allocation.width - 2 * border_width,
748                      widget->allocation.height - 2 * border_width);
749     }
750
751   items = priv->items;
752   while (items)
753     {
754       GtkToolItem *item = GTK_TOOL_ITEM (items->data);
755
756       gtk_container_propagate_expose (GTK_CONTAINER (widget),
757                                       GTK_WIDGET (item),
758                                       event);
759       
760       items = items->next;
761     }
762
763   gtk_container_propagate_expose (GTK_CONTAINER (widget),
764                                   priv->arrow_button,
765                                   event);
766
767   return FALSE;
768 }
769
770 static void
771 gtk_toolbar_size_request (GtkWidget      *widget,
772                           GtkRequisition *requisition)
773 {
774   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
775   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
776   gint space_size = get_space_size (toolbar);
777   GList *list;
778   gint max_child_height;
779   gint max_child_width;
780   gint max_homogeneous_child_width;
781   gint max_homogeneous_child_height;
782   gint homogeneous_size;
783   gint long_req;
784   gint pack_end_size;
785   gint pack_front_size;
786   gint ipadding;
787   GtkRequisition arrow_requisition;
788
789   max_homogeneous_child_width = 0;
790   max_homogeneous_child_height = 0;
791   max_child_width = 0;
792   max_child_height = 0;
793   for (list = priv->items; list != NULL; list = list->next)
794     {
795       GtkRequisition requisition;
796       GtkToolItem *item = list->data;
797       
798       if (!toolbar_item_visible (toolbar, item))
799         continue;
800
801       gtk_widget_size_request (GTK_WIDGET (item), &requisition);
802       
803       max_child_width = MAX (max_child_width, requisition.width);
804       max_child_height = MAX (max_child_height, requisition.height);
805
806       if (gtk_tool_item_get_homogeneous (item) && GTK_BIN (item)->child)
807         {
808           max_homogeneous_child_width = MAX (max_homogeneous_child_width, requisition.width);
809           max_homogeneous_child_height = MAX (max_homogeneous_child_height, requisition.height);
810         }
811     }
812   
813   if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
814     homogeneous_size = max_homogeneous_child_width;
815   else
816     homogeneous_size = max_homogeneous_child_height;
817   
818   pack_end_size = 0;
819   pack_front_size = 0;
820   for (list = priv->items; list != NULL; list = list->next)
821     {
822       GtkToolItem *item = list->data;
823       guint size;
824       
825       if (!toolbar_item_visible (toolbar, item))
826         continue;
827       
828       if (!GTK_BIN (item)->child)
829         {
830           size = space_size;
831         }
832       else if (gtk_tool_item_get_homogeneous (item))
833         {
834           size = homogeneous_size;
835         }
836       else
837         {
838           GtkRequisition requisition;
839           
840           gtk_widget_size_request (GTK_WIDGET (item), &requisition);
841
842           if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
843             size = requisition.width;
844           else
845             size = requisition.height;
846         }
847       
848       if (gtk_tool_item_get_pack_end (item))
849         pack_end_size += size;
850       else
851         pack_front_size += size;
852     }
853   
854   if (priv->show_arrow)
855     {
856       gtk_widget_size_request (priv->arrow_button, &arrow_requisition);
857       
858       if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
859         long_req = arrow_requisition.width;
860       else
861         long_req = arrow_requisition.height;
862
863       /* There is no point requesting space for the arrow if that would take
864        * up more space than all the items combined
865        */
866       long_req = MIN (long_req, pack_front_size + pack_end_size);
867     }
868   else
869     {
870       arrow_requisition.height = 0;
871       arrow_requisition.width = 0;
872       
873       long_req = pack_end_size + pack_front_size;
874     }
875   
876   if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
877     {
878       requisition->width = long_req;
879       requisition->height = MAX (max_child_height, arrow_requisition.height);
880     }
881   else
882     {
883       requisition->height = long_req;
884       requisition->width = MAX (max_child_width, arrow_requisition.width);
885     }
886   
887   /* Extra spacing */
888   ipadding = get_internal_padding (toolbar);
889
890   requisition->width += 2 * (ipadding + GTK_CONTAINER (toolbar)->border_width);
891   requisition->height += 2 * (ipadding + GTK_CONTAINER (toolbar)->border_width);
892   
893   toolbar->button_maxw = max_homogeneous_child_width;
894   toolbar->button_maxh = max_homogeneous_child_height;
895 }
896
897 static void
898 fixup_allocation_for_rtl (gint           total_size,
899                           GtkAllocation *allocation)
900 {
901   allocation->x += (total_size - (2 * allocation->x + allocation->width));
902 }
903
904 static void
905 fixup_allocation_for_vertical (GtkAllocation *allocation)
906 {
907   gint tmp;
908   
909   tmp = allocation->x;
910   allocation->x = allocation->y;
911   allocation->y = tmp;
912   
913   tmp = allocation->width;
914   allocation->width = allocation->height;
915   allocation->height = tmp;
916 }
917
918 static gint
919 get_item_size (GtkToolbar *toolbar,
920                GtkWidget  *child)
921 {
922   GtkRequisition requisition;
923   GtkToolItem *item = GTK_TOOL_ITEM (child);
924
925   if (!GTK_BIN (item)->child)
926     return get_space_size (toolbar);
927
928   gtk_widget_get_child_requisition (child, &requisition);
929   
930   if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
931     {
932       if (gtk_tool_item_get_homogeneous (item))
933         return toolbar->button_maxw;
934       else
935         return requisition.width;
936     }
937   else
938     {
939       if (gtk_tool_item_get_homogeneous (item))
940         return toolbar->button_maxh;
941       else
942         return requisition.height;
943     }
944 }
945
946 static void
947 gtk_toolbar_size_allocate (GtkWidget     *widget,
948                            GtkAllocation *allocation)
949 {
950   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
951   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
952   gint space_size;
953   GtkAllocation *allocations;
954   GtkAllocation arrow_allocation;
955   gint arrow_size;
956   gint size, pos, short_size;
957   GList *list;
958   gint i;
959   gboolean need_arrow;
960   gint n_expand_items;
961   gint border_width;
962   gint available_size;
963   gint n_items;
964   gint needed_size;
965   GList *items;
966   GtkRequisition arrow_requisition;
967
968   widget->allocation = *allocation;
969
970   space_size = get_space_size (toolbar);
971
972   border_width = GTK_CONTAINER (toolbar)->border_width;
973
974   if (GTK_WIDGET_REALIZED (widget))
975     {
976       gdk_window_move_resize (priv->event_window,
977                               allocation->x + border_width,
978                               allocation->y + border_width,
979                               allocation->width - border_width * 2,
980                               allocation->height - border_width * 2);
981     }
982   
983   border_width += get_internal_padding (toolbar);
984   
985   gtk_widget_get_child_requisition (GTK_WIDGET (priv->arrow_button),
986                                     &arrow_requisition);
987   
988   if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
989     {
990       available_size = size = allocation->width - 2 * border_width;
991       short_size = allocation->height - 2 * border_width;
992       arrow_size = arrow_requisition.width;
993     }
994   else
995     {
996       available_size = size = allocation->height - 2 * border_width;
997       short_size = allocation->width - 2 * border_width;
998       arrow_size = arrow_requisition.height;
999     }
1000
1001   n_items = g_list_length (priv->items);
1002   allocations = g_new0 (GtkAllocation, n_items);
1003
1004   needed_size = 0;
1005   for (list = priv->items; list != NULL; list = list->next)
1006     {
1007       GtkToolItem *item = list->data;
1008       
1009       if (toolbar_item_visible (toolbar, item))
1010         needed_size += get_item_size (toolbar, GTK_WIDGET (item));
1011     }
1012
1013   need_arrow = (needed_size > available_size) && priv->show_arrow;
1014
1015   if (need_arrow)
1016     size = available_size - arrow_size;
1017   else
1018     size = available_size;
1019
1020   items = g_list_copy (priv->items);
1021
1022   /* calculate widths of pack end items */
1023   for (list = g_list_last (items), i = 0; list != NULL; list = list->prev, ++i)
1024     {
1025       GtkToolItem *item = list->data;
1026       GtkAllocation *allocation = &(allocations[n_items - i - 1]);
1027       gint item_size;
1028       
1029       if (!gtk_tool_item_get_pack_end (item) || !toolbar_item_visible (toolbar, item))
1030         continue;
1031
1032       item_size = get_item_size (toolbar, GTK_WIDGET (item));
1033       if (item_size <= size)
1034         {
1035           size -= item_size;
1036           allocation->width = item_size;
1037           toolbar_item_set_is_overflow (item, FALSE);
1038         }
1039       else
1040         {
1041           while (list)
1042             {
1043               item = list->data;
1044               if (gtk_tool_item_get_pack_end (item))
1045                 toolbar_item_set_is_overflow (item, TRUE);
1046               
1047               list = list->prev;
1048             }
1049           break;
1050         }
1051     }
1052
1053   /* calculate widths of pack front items */
1054   for (list = items, i = 0; list != NULL; list = list->next, ++i)
1055     {
1056       GtkToolItem *item = list->data;
1057       gint item_size;
1058
1059       if (gtk_tool_item_get_pack_end (item) || !toolbar_item_visible (toolbar, item))
1060         continue;
1061
1062       item_size = get_item_size (toolbar, GTK_WIDGET (item));
1063       if (item_size <= size)
1064         {
1065           size -= item_size;
1066           allocations[i].width = item_size;
1067           toolbar_item_set_is_overflow (item, FALSE);
1068         }
1069       else
1070         {
1071           while (list)
1072             {
1073               item = list->data;
1074               if (!gtk_tool_item_get_pack_end (item))
1075                 toolbar_item_set_is_overflow (item, TRUE);
1076               list = list->next;
1077             }
1078           break;
1079         }
1080     }
1081
1082   if (need_arrow)
1083     {
1084       arrow_allocation.width = arrow_size;
1085       arrow_allocation.height = short_size;
1086     }
1087   
1088   /* expand expandable items */
1089   n_expand_items = 0;
1090   for (list = priv->items; list != NULL; list = list->next)
1091     {
1092       GtkToolItem *item = list->data;
1093       
1094       if (toolbar_item_visible (toolbar, item) &&
1095           gtk_tool_item_get_expand (item) &&
1096           !toolbar_item_get_is_overflow (item) && GTK_BIN (item)->child)
1097         {
1098           n_expand_items++;
1099         }
1100     }
1101   
1102   for (list = items, i = 0; list != NULL; list = list->next, ++i)
1103     {
1104       GtkToolItem *item = list->data;
1105       
1106       if (toolbar_item_visible (toolbar, item) && gtk_tool_item_get_expand (item) &&
1107           !toolbar_item_get_is_overflow (item) && GTK_BIN (item)->child)
1108         {
1109           gint extra = size / n_expand_items;
1110           if (size % n_expand_items != 0)
1111             extra++;
1112
1113           allocations[i].width += extra;
1114           size -= extra;
1115           n_expand_items--;
1116         }
1117     }
1118
1119   g_assert (n_expand_items == 0);
1120   
1121   /* position pack front items */
1122   pos = border_width;
1123   for (list = items, i = 0; list != NULL; list = list->next, ++i)
1124     {
1125       GtkToolItem *item = list->data;
1126       
1127       if (toolbar_item_visible (toolbar, item) && !toolbar_item_get_is_overflow (item) && !gtk_tool_item_get_pack_end (item))
1128         {
1129           allocations[i].x = pos;
1130           allocations[i].y = border_width;
1131           allocations[i].height = short_size;
1132
1133           pos += allocations[i].width;
1134         }
1135     }
1136
1137   /* position pack end items */
1138   pos = available_size + border_width;
1139   for (list = g_list_last (items), i = 0; list != NULL; list = list->prev, ++i)
1140     {
1141       GtkToolItem *item = list->data;
1142       
1143       if (toolbar_item_visible (toolbar, item) && !toolbar_item_get_is_overflow (item) && gtk_tool_item_get_pack_end (item))
1144         {
1145           GtkAllocation *allocation = &(allocations[n_items - i - 1]);
1146
1147           allocation->x = pos - allocation->width;
1148           allocation->y = border_width;
1149           allocation->height = short_size;
1150           
1151           pos -= allocation->width;
1152         }
1153     }
1154
1155   /* position arrow */
1156   if (need_arrow)
1157     {
1158       arrow_allocation.x = pos - arrow_allocation.width;
1159       arrow_allocation.y = border_width;
1160     }
1161   
1162   /* fix up allocations in the vertical or RTL cases */
1163   if (toolbar->orientation == GTK_ORIENTATION_VERTICAL)
1164     {
1165       for (i = 0; i < n_items; ++i)
1166         fixup_allocation_for_vertical (&(allocations[i]));
1167       
1168       if (need_arrow)
1169         fixup_allocation_for_vertical (&arrow_allocation);
1170     }
1171   else if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
1172     {
1173       for (i = 0; i < n_items; ++i)
1174         fixup_allocation_for_rtl (available_size, &(allocations[i]));
1175
1176       if (need_arrow)
1177         fixup_allocation_for_rtl (available_size, &arrow_allocation);
1178     }
1179   
1180   /* translate the items by allocation->(x,y) */
1181   for (i = 0; i < n_items; ++i)
1182     {
1183       allocations[i].x += allocation->x;
1184       allocations[i].y += allocation->y;
1185     }
1186
1187   if (need_arrow)
1188     {
1189       arrow_allocation.x += allocation->x;
1190       arrow_allocation.y += allocation->y;
1191     }
1192   
1193   /* finally allocate the items */
1194   for (list = items, i = 0; list != NULL; list = list->next, i++)
1195     {
1196       GtkToolItem *item = list->data;
1197       
1198       if (toolbar_item_visible (toolbar, item) && !toolbar_item_get_is_overflow (item))
1199         {
1200           gtk_widget_size_allocate (GTK_WIDGET (item), &(allocations[i]));
1201           gtk_widget_set_child_visible (GTK_WIDGET (item), TRUE);
1202         }
1203       else
1204         {
1205           gtk_widget_set_child_visible (GTK_WIDGET (item), FALSE);
1206         }
1207     }
1208
1209   if (need_arrow)
1210     {
1211       gtk_widget_size_allocate (GTK_WIDGET (priv->arrow_button),
1212                                 &arrow_allocation);
1213       gtk_widget_show (GTK_WIDGET (priv->arrow_button));
1214     }
1215   else
1216     {
1217       gtk_widget_hide (GTK_WIDGET (priv->arrow_button));
1218     }
1219   
1220   g_free (allocations);
1221   g_list_free (items);
1222 }
1223
1224 static void
1225 gtk_toolbar_style_set (GtkWidget *widget,
1226                        GtkStyle  *prev_style)
1227 {
1228   if (GTK_WIDGET_REALIZED (widget))
1229     gtk_style_set_background (widget->style, widget->window, widget->state);
1230
1231   if (prev_style)
1232     gtk_toolbar_update_button_relief (GTK_TOOLBAR (widget));
1233 }
1234
1235 static void 
1236 gtk_toolbar_direction_changed (GtkWidget        *widget,
1237                                GtkTextDirection  previous_dir)
1238 {
1239   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1240   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1241   
1242   if (toolbar->orientation == GTK_ORIENTATION_VERTICAL)
1243     {
1244       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
1245         gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
1246       else 
1247         gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_LEFT, GTK_SHADOW_NONE);
1248     }
1249
1250   GTK_WIDGET_CLASS (parent_class)->direction_changed (widget, previous_dir);
1251 }
1252
1253 static GList *
1254 gtk_toolbar_list_children_in_focus_order (GtkToolbar       *toolbar,
1255                                           GtkDirectionType  dir)
1256 {
1257   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1258   GList *result = NULL;
1259   GList *list;
1260   gboolean rtl;
1261
1262   /* generate list of children in reverse logical order */
1263   
1264   for (list = priv->items; list != NULL; list = list->next)
1265     {
1266       GtkToolItem *item = list->data;
1267       if (!gtk_tool_item_get_pack_end (item))
1268         result = g_list_prepend (result, item);
1269     }
1270
1271   for (list = priv->items; list != NULL; list = list->next)
1272     {
1273       GtkToolItem *item = list->data;
1274
1275       if (gtk_tool_item_get_pack_end (item))
1276         result = g_list_prepend (result, item);
1277     }
1278
1279   result = g_list_prepend (result, priv->arrow_button);
1280
1281   rtl = (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL);
1282   
1283   /* move in logical order when
1284    *
1285    *    - dir is TAB_FORWARD
1286    *
1287    *    - in RTL mode and moving left or up
1288    *
1289    *    - in LTR mode and moving right or down
1290    */
1291   if (dir == GTK_DIR_TAB_FORWARD                                        ||
1292       (rtl  && (dir == GTK_DIR_UP   || dir == GTK_DIR_LEFT))            ||
1293       (!rtl && (dir == GTK_DIR_DOWN || dir == GTK_DIR_RIGHT)))
1294     {
1295       result = g_list_reverse (result);
1296     }
1297
1298   return result;
1299 }
1300
1301 static gboolean
1302 gtk_toolbar_focus_home_or_end (GtkToolbar *toolbar,
1303                                gboolean    focus_home)
1304 {
1305   GList *children, *list;
1306   GtkDirectionType dir = focus_home? GTK_DIR_RIGHT : GTK_DIR_LEFT;
1307
1308   children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);
1309
1310   if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL)
1311     {
1312       children = g_list_reverse (children);
1313       
1314       dir = (dir == GTK_DIR_RIGHT)? GTK_DIR_LEFT : GTK_DIR_RIGHT;
1315     }
1316
1317   for (list = children; list != NULL; list = list->next)
1318     {
1319       GtkWidget *child = list->data;
1320       
1321       if (GTK_CONTAINER (toolbar)->focus_child == child)
1322         break;
1323
1324       if (GTK_WIDGET_MAPPED (child) && gtk_widget_child_focus (child, dir))
1325         break;
1326     }
1327
1328   g_list_free (children);
1329   
1330   return TRUE;
1331 }   
1332
1333 /* Keybinding handler. This function is called when the user presses
1334  * Ctrl TAB or an arrow key.
1335  */
1336 static gboolean
1337 gtk_toolbar_move_focus (GtkToolbar       *toolbar,
1338                         GtkDirectionType  dir)
1339 {
1340   GList *list;
1341   gboolean try_focus = FALSE;
1342   GList *children;
1343   GtkContainer *container = GTK_CONTAINER (toolbar);
1344
1345   if (container->focus_child &&
1346       gtk_widget_child_focus (container->focus_child, dir))
1347     {
1348       return TRUE;
1349     }
1350   
1351   children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);
1352
1353   for (list = children; list != NULL; list = list->next)
1354     {
1355       GtkWidget *child = list->data;
1356       
1357       if (try_focus && GTK_WIDGET_MAPPED (child) && gtk_widget_child_focus (child, dir))
1358         break;
1359
1360       if (child == GTK_CONTAINER (toolbar)->focus_child)
1361         try_focus = TRUE;
1362     }
1363
1364   g_list_free (children);
1365
1366   return TRUE;
1367 }
1368
1369 /* The focus handler for the toolbar. It called when the user presses
1370  * TAB or otherwise tries to focus the toolbar.
1371  */
1372 static gboolean
1373 gtk_toolbar_focus (GtkWidget        *widget,
1374                    GtkDirectionType  dir)
1375 {
1376   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1377   GList *children, *list;
1378  
1379   /* if focus is already somewhere inside the toolbar then return FALSE.
1380    * The only way focus can stay inside the toolbar is when the user presses
1381    * arrow keys or Ctrl TAB (both of which are handled by the
1382    * gtk_toolbar_move_focus() keybinding function.
1383    */
1384   if (GTK_CONTAINER (widget)->focus_child)
1385     return FALSE;
1386
1387   children = gtk_toolbar_list_children_in_focus_order (toolbar, dir);
1388
1389   for (list = children; list != NULL; list = list->next)
1390     {
1391       GtkWidget *child = list->data;
1392       
1393       if (GTK_WIDGET_MAPPED (child) && gtk_widget_child_focus (child, dir))
1394         return TRUE;
1395     }
1396
1397   g_list_free (children);
1398   
1399   return FALSE;
1400 }
1401
1402 static void
1403 style_change_notify (GtkToolbar *toolbar)
1404 {
1405   if (!toolbar->style_set)
1406     {
1407       /* pretend it was set, then unset, thus reverting to new default */
1408       toolbar->style_set = TRUE; 
1409       gtk_toolbar_unset_style (toolbar);
1410     }
1411 }
1412
1413 static void
1414 icon_size_change_notify (GtkToolbar *toolbar)
1415
1416   if (!toolbar->icon_size_set)
1417     {
1418       /* pretend it was set, then unset, thus reverting to new default */
1419       toolbar->icon_size_set = TRUE; 
1420       gtk_toolbar_unset_icon_size (toolbar);
1421     }
1422 }
1423
1424 static GtkSettings *
1425 toolbar_get_settings (GtkToolbar *toolbar)
1426 {
1427   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1428   return priv->settings;
1429 }
1430
1431 static void
1432 gtk_toolbar_screen_changed (GtkWidget *widget,
1433                             GdkScreen *previous_screen)
1434 {
1435   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (widget);
1436   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1437   GtkSettings *old_settings = toolbar_get_settings (toolbar);
1438   GtkSettings *settings;
1439
1440   if (gtk_widget_has_screen (GTK_WIDGET (toolbar)))
1441     settings = gtk_widget_get_settings (GTK_WIDGET (toolbar));
1442   else
1443     settings = NULL;
1444
1445   if (settings == old_settings)
1446     return;
1447
1448   if (old_settings)
1449     {
1450       g_signal_handler_disconnect (old_settings, toolbar->style_set_connection);
1451       g_signal_handler_disconnect (old_settings, toolbar->icon_size_connection);
1452
1453       g_object_unref (old_settings);
1454     }
1455
1456   if (settings)
1457     {
1458       toolbar->style_set_connection =
1459         g_signal_connect_swapped (settings,
1460                                   "notify::gtk-toolbar-style",
1461                                   G_CALLBACK (style_change_notify),
1462                                   toolbar);
1463       toolbar->icon_size_connection =
1464         g_signal_connect_swapped (settings,
1465                                   "notify::gtk-toolbar-icon-size",
1466                                   G_CALLBACK (icon_size_change_notify),
1467                                   toolbar);
1468
1469       g_object_ref (settings);
1470       priv->settings = settings;
1471     }
1472   else
1473     priv->settings = NULL;
1474
1475   style_change_notify (toolbar);
1476   icon_size_change_notify (toolbar);
1477 }
1478
1479 static void
1480 find_drop_pos (GtkToolbar *toolbar,
1481                gint        x,
1482                gint        y,
1483                gint       *drop_index,
1484                gint       *drop_pos)
1485 {
1486   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1487   GtkOrientation orientation;
1488   GtkTextDirection direction;
1489   GList *items;
1490   GtkToolItem *item;
1491   gint border_width;
1492   gint best_distance, best_pos, best_index, index;
1493
1494   orientation = toolbar->orientation;
1495   direction = gtk_widget_get_direction (GTK_WIDGET (toolbar));
1496   border_width = GTK_CONTAINER (toolbar)->border_width + get_internal_padding (toolbar);
1497
1498   items = priv->items;
1499   if (!items)
1500     {
1501       *drop_index = 0;
1502       if (orientation == GTK_ORIENTATION_HORIZONTAL)
1503         {
1504           if (direction == GTK_TEXT_DIR_LTR) 
1505             *drop_pos = border_width;
1506           else
1507             *drop_pos = GTK_WIDGET (toolbar)->allocation.width - border_width;
1508         }
1509       else
1510         {
1511           *drop_pos = border_width;
1512         }
1513       return;
1514     }
1515
1516   /* initial conditions */
1517   item = GTK_TOOL_ITEM (items->data);
1518   best_index = 0;
1519   if (orientation == GTK_ORIENTATION_HORIZONTAL)
1520     {
1521       if (direction == GTK_TEXT_DIR_LTR)
1522         best_pos = GTK_WIDGET (item)->allocation.x;
1523       else
1524         best_pos = GTK_WIDGET (item)->allocation.x +
1525           GTK_WIDGET (item)->allocation.width;
1526       best_distance = ABS (best_pos - x);
1527     }
1528   else
1529     {
1530       best_pos = GTK_WIDGET (item)->allocation.y;
1531       best_distance = ABS (best_pos - y);
1532     }
1533
1534   index = 0;
1535   while (items)
1536     {
1537       item = GTK_TOOL_ITEM (items->data);
1538       index++;
1539       if (GTK_WIDGET_DRAWABLE (item) && !gtk_tool_item_get_pack_end (item))
1540         {
1541           gint pos, distance;
1542
1543           if (orientation == GTK_ORIENTATION_HORIZONTAL)
1544             {
1545               if (direction == GTK_TEXT_DIR_LTR)
1546                 pos = GTK_WIDGET (item)->allocation.x +
1547                   GTK_WIDGET (item)->allocation.width;
1548               else
1549                 pos = GTK_WIDGET (item)->allocation.x;
1550               distance = ABS (pos - x);
1551             }
1552           else
1553             {
1554               pos = GTK_WIDGET (item)->allocation.y +
1555                 GTK_WIDGET (item)->allocation.height;
1556               distance = ABS (pos - y);
1557             }
1558           if (distance < best_distance)
1559             {
1560               best_index = index;
1561               best_pos = pos;
1562               best_distance = distance;
1563             }
1564         }
1565       items = items->next;
1566     }
1567   *drop_index = best_index;
1568   *drop_pos = best_pos;
1569 }
1570
1571 static void
1572 gtk_toolbar_drag_leave (GtkWidget      *widget,
1573                         GdkDragContext *context,
1574                         guint           time_)
1575 {
1576   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1577   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1578
1579   if (priv->drag_highlight)
1580     {
1581       gdk_window_set_user_data (priv->drag_highlight, NULL);
1582       gdk_window_destroy (priv->drag_highlight);
1583       priv->drag_highlight = NULL;
1584     }
1585
1586   priv->drop_index = -1;
1587 }
1588
1589 static gboolean
1590 gtk_toolbar_drag_motion (GtkWidget      *widget,
1591                          GdkDragContext *context,
1592                          gint            x,
1593                          gint            y,
1594                          guint           time_)
1595 {
1596   GtkToolbar *toolbar = GTK_TOOLBAR (widget);
1597   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1598   gint new_index, new_pos;
1599
1600   find_drop_pos(toolbar, x, y, &new_index, &new_pos);
1601
1602   if (!priv->drag_highlight)
1603     {
1604       GdkWindowAttr attributes;
1605       guint attributes_mask;
1606
1607       attributes.window_type = GDK_WINDOW_CHILD;
1608       attributes.wclass = GDK_INPUT_OUTPUT;
1609       attributes.visual = gtk_widget_get_visual (widget);
1610       attributes.colormap = gtk_widget_get_colormap (widget);
1611       attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK;
1612       attributes.width = 1;
1613       attributes.height = 1;
1614       attributes_mask = GDK_WA_VISUAL | GDK_WA_COLORMAP;
1615       priv->drag_highlight = gdk_window_new (widget->window,
1616                                              &attributes, attributes_mask);
1617       gdk_window_set_user_data (priv->drag_highlight, widget);
1618       gdk_window_set_background (priv->drag_highlight,
1619                                  &widget->style->fg[widget->state]);
1620     }
1621
1622   if (priv->drop_index < 0 ||
1623       priv->drop_index != new_index)
1624     {
1625       gint border_width = GTK_CONTAINER (toolbar)->border_width;
1626       priv->drop_index = new_index;
1627       if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
1628         {
1629           gdk_window_move_resize (priv->drag_highlight,
1630                                   widget->allocation.x + new_pos - 1,
1631                                   widget->allocation.y + border_width,
1632                                   2, widget->allocation.height-border_width*2);
1633         }
1634       else
1635         {
1636           gdk_window_move_resize (priv->drag_highlight,
1637                                   widget->allocation.x + border_width,
1638                                   widget->allocation.y + new_pos - 1,
1639                                   widget->allocation.width-border_width*2, 2);
1640         }
1641     }
1642
1643   gdk_window_show (priv->drag_highlight);
1644
1645   gdk_drag_status (context, context->suggested_action, time_);
1646
1647   return TRUE;
1648 }
1649
1650 static void
1651 gtk_toolbar_get_child_property (GtkContainer *container,
1652                                 GtkWidget    *child,
1653                                 guint         property_id,
1654                                 GValue       *value,
1655                                 GParamSpec   *pspec)
1656 {
1657   GtkToolItem *item = GTK_TOOL_ITEM (child);
1658   
1659   switch (property_id)
1660     {
1661     case CHILD_PROP_PACK_END:
1662       g_value_set_boolean (value, gtk_tool_item_get_pack_end (item));
1663       break;
1664
1665     case CHILD_PROP_HOMOGENEOUS:
1666       g_value_set_boolean (value, gtk_tool_item_get_homogeneous (item));
1667       break;
1668
1669     case CHILD_PROP_EXPAND:
1670       g_value_set_boolean (value, gtk_tool_item_get_expand (item));
1671       break;
1672
1673     default:
1674       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1675       break;
1676     }
1677 }
1678
1679 static void
1680 gtk_toolbar_set_child_property (GtkContainer *container,
1681                                 GtkWidget    *child,
1682                                 guint         property_id,
1683                                 const GValue *value,
1684                                 GParamSpec   *pspec)
1685 {
1686   switch (property_id)
1687     {
1688     case CHILD_PROP_PACK_END:
1689       gtk_tool_item_set_pack_end (GTK_TOOL_ITEM (child), g_value_get_boolean (value));
1690       break;
1691
1692     case CHILD_PROP_HOMOGENEOUS:
1693       gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (child), g_value_get_boolean (value));
1694       break;
1695
1696     case CHILD_PROP_EXPAND:
1697       gtk_tool_item_set_homogeneous (GTK_TOOL_ITEM (child), g_value_get_boolean (value));
1698       break;
1699       
1700     default:
1701       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1702       break;
1703     }
1704 }
1705
1706 static void
1707 gtk_toolbar_add (GtkContainer *container,
1708                  GtkWidget    *widget)
1709 {
1710   GtkToolbar *toolbar;
1711   
1712   g_return_if_fail (GTK_IS_TOOLBAR (container));
1713   g_return_if_fail (widget != NULL);
1714
1715   toolbar = GTK_TOOLBAR (container);
1716   
1717   if (GTK_IS_TOOL_ITEM (widget))
1718     gtk_toolbar_insert (toolbar, GTK_TOOL_ITEM (widget), 0);
1719   else
1720     gtk_toolbar_append_widget (toolbar, widget, NULL, NULL);
1721 }
1722
1723 static void
1724 gtk_toolbar_remove (GtkContainer *container,
1725                     GtkWidget    *widget)
1726 {
1727   GtkToolbar *toolbar;
1728   GtkToolItem *item = NULL;
1729   
1730   g_return_if_fail (GTK_IS_TOOLBAR (container));
1731   g_return_if_fail (GTK_IS_WIDGET (widget));
1732
1733   toolbar = GTK_TOOLBAR (container);
1734
1735   if (GTK_IS_TOOL_ITEM (widget))
1736     {
1737       item = GTK_TOOL_ITEM (widget);
1738     }
1739   else
1740     {
1741       GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1742       GList *list;
1743       
1744       for (list = priv->items; list != NULL; list = list->next)
1745         {
1746           if (GTK_BIN (list->data)->child == widget)
1747             {
1748               item = list->data;
1749               break;
1750             }
1751         }
1752     }
1753
1754   g_return_if_fail (item != NULL);
1755
1756   gtk_toolbar_remove_tool_item (GTK_TOOLBAR (container), item);
1757 }
1758
1759 static void
1760 gtk_toolbar_forall (GtkContainer *container,
1761                     gboolean      include_internals,
1762                     GtkCallback   callback,
1763                     gpointer      callback_data)
1764 {
1765   GtkToolbar *toolbar = GTK_TOOLBAR (container);
1766   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1767   GList *items;
1768
1769   g_return_if_fail (callback != NULL);
1770
1771   items = priv->items;
1772       
1773   while (items)
1774     {
1775       GtkToolItem *item = GTK_TOOL_ITEM (items->data);
1776       
1777       items = items->next;
1778       
1779       (*callback) (GTK_WIDGET (item), callback_data);
1780     }
1781   
1782   if (include_internals)
1783     (* callback) (priv->arrow_button, callback_data);
1784 }
1785
1786 static GType
1787 gtk_toolbar_child_type (GtkContainer *container)
1788 {
1789   return GTK_TYPE_TOOL_ITEM;
1790 }
1791
1792 static void
1793 gtk_toolbar_reconfigured (GtkToolbar *toolbar)
1794 {
1795   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1796   GList *items;
1797
1798   items = priv->items;
1799   while (items)
1800     {
1801       GtkToolItem *item = GTK_TOOL_ITEM (items->data);
1802       
1803       gtk_tool_item_toolbar_reconfigured (item);
1804       
1805       items = items->next;
1806     }
1807 }
1808
1809 static void
1810 gtk_toolbar_real_orientation_changed (GtkToolbar    *toolbar,
1811                                       GtkOrientation orientation)
1812 {
1813   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1814   if (toolbar->orientation != orientation)
1815     {
1816       toolbar->orientation = orientation;
1817
1818       if (orientation == GTK_ORIENTATION_HORIZONTAL)
1819         gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_DOWN, GTK_SHADOW_NONE);
1820       else if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_LTR)
1821         gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_RIGHT, GTK_SHADOW_NONE);
1822       else 
1823         gtk_arrow_set (GTK_ARROW (priv->arrow), GTK_ARROW_LEFT, GTK_SHADOW_NONE);
1824
1825       gtk_toolbar_reconfigured (toolbar);
1826
1827       gtk_widget_queue_resize (GTK_WIDGET (toolbar));
1828       g_object_notify (G_OBJECT (toolbar), "orientation");
1829     }
1830 }
1831
1832 static void
1833 gtk_toolbar_real_style_changed (GtkToolbar     *toolbar,
1834                                 GtkToolbarStyle style)
1835 {
1836   if (toolbar->style != style)
1837     {
1838       toolbar->style = style;
1839
1840       gtk_toolbar_reconfigured (toolbar);
1841
1842       gtk_widget_queue_resize (GTK_WIDGET (toolbar));
1843       g_object_notify (G_OBJECT (toolbar), "toolbar_style");
1844     }
1845 }
1846
1847 static void
1848 menu_position_func (GtkMenu  *menu,
1849                     gint     *x,
1850                     gint     *y,
1851                     gboolean *push_in,
1852                     gpointer  user_data)
1853 {
1854   GtkToolbar *toolbar = GTK_TOOLBAR (user_data);
1855   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1856   GtkRequisition req;
1857   GtkRequisition menu_req;
1858   
1859   gdk_window_get_origin (GTK_BUTTON (priv->arrow_button)->event_window, x, y);
1860   gtk_widget_size_request (priv->arrow_button, &req);
1861   gtk_widget_size_request (GTK_WIDGET (menu), &menu_req);
1862   
1863   if (toolbar->orientation == GTK_ORIENTATION_HORIZONTAL)
1864     {
1865       *y += priv->arrow_button->allocation.height;
1866       if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_LTR) 
1867         *x += priv->arrow_button->allocation.width - req.width;
1868       else 
1869         *x += req.width - menu_req.width;
1870     }
1871   else 
1872     {
1873       if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_LTR) 
1874         *x += priv->arrow_button->allocation.width;
1875       else 
1876         *x -= menu_req.width;
1877       *y += priv->arrow_button->allocation.height - req.height;      
1878     }
1879   
1880   *push_in = TRUE;
1881 }
1882
1883 static void
1884 menu_deactivated (GtkWidget  *menu,
1885                   GtkToolbar *toolbar)
1886 {
1887   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1888   
1889   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->arrow_button), FALSE);
1890 }
1891
1892 static void
1893 remove_item (GtkWidget *menu_item,
1894              gpointer   data)
1895 {
1896   gtk_container_remove (GTK_CONTAINER (menu_item->parent), menu_item);
1897 }
1898
1899 static void
1900 show_menu (GtkToolbar     *toolbar,
1901            GdkEventButton *event)
1902 {
1903   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
1904   GList *list;
1905   
1906   if (priv->menu)
1907     {
1908       gtk_container_foreach (GTK_CONTAINER (priv->menu), remove_item, NULL);
1909       gtk_widget_destroy (GTK_WIDGET (priv->menu));
1910     }
1911
1912   priv->menu = GTK_MENU (gtk_menu_new ());
1913   g_signal_connect (priv->menu, "deactivate", G_CALLBACK (menu_deactivated), toolbar);
1914
1915   for (list = priv->items; list != NULL; list = list->next)
1916     {
1917       GtkToolItem *item = list->data;
1918
1919       if (toolbar_item_visible (toolbar, item) && toolbar_item_get_is_overflow (item))
1920         {
1921           GtkWidget *menu_item = gtk_tool_item_retrieve_proxy_menu_item (item);
1922
1923           if (menu_item)
1924             {
1925               g_assert (GTK_IS_MENU_ITEM (menu_item));
1926               gtk_menu_shell_append (GTK_MENU_SHELL (priv->menu), menu_item);
1927             }
1928         }
1929     }
1930
1931   gtk_widget_show_all (GTK_WIDGET (priv->menu));
1932
1933   gtk_menu_popup (GTK_MENU (priv->menu), NULL, NULL,
1934                   menu_position_func, toolbar,
1935                   event? event->button : 0, event? event->time : gtk_get_current_event_time());
1936 }
1937
1938 static void
1939 gtk_toolbar_arrow_button_clicked (GtkWidget  *button,
1940                                   GtkToolbar *toolbar)
1941 {
1942   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);  
1943
1944   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->arrow_button)) &&
1945       (!priv->menu || !GTK_WIDGET_VISIBLE (GTK_WIDGET (priv->menu))))
1946     {
1947       /* We only get here when the button is clicked with the keybaord,
1948        * because mouse button presses result in the menu being shown so
1949        * that priv->menu would be non-NULL and visible.
1950        */
1951       show_menu (toolbar, NULL);
1952       gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
1953     }
1954 }
1955
1956 static gboolean
1957 gtk_toolbar_arrow_button_press (GtkWidget      *button,
1958                                 GdkEventButton *event,
1959                                 GtkToolbar     *toolbar)
1960 {
1961   show_menu (toolbar, event);
1962   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
1963  
1964   return TRUE;
1965 }
1966
1967 static gboolean
1968 gtk_toolbar_button_press (GtkWidget      *toolbar,
1969                           GdkEventButton *event)
1970 {
1971   if (event->button == 3)
1972     {
1973       gboolean return_value;
1974     
1975       g_signal_emit (toolbar, toolbar_signals[POPUP_CONTEXT_MENU], 0,
1976                      (int)event->x_root, (int)event->y_root, event->button,
1977                      &return_value);
1978
1979       return return_value;
1980     }
1981
1982   return FALSE;
1983 }
1984
1985 static gboolean
1986 gtk_toolbar_popup_menu (GtkWidget *toolbar)
1987 {
1988   gboolean return_value;
1989   /* This function is the handler for the "popup menu" keybinding,
1990    * ie., it is called when the user presses Shift F10
1991    */
1992   g_signal_emit (toolbar, toolbar_signals[POPUP_CONTEXT_MENU], 0,
1993                  -1, -1, -1, &return_value);
1994
1995   return return_value;
1996 }
1997
1998 static void
1999 gtk_toolbar_update_button_relief (GtkToolbar *toolbar)
2000 {
2001   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2002
2003   gtk_toolbar_reconfigured (toolbar);
2004
2005   gtk_button_set_relief (GTK_BUTTON (priv->arrow_button), get_button_relief (toolbar));
2006 }
2007
2008 static GtkReliefStyle
2009 get_button_relief (GtkToolbar *toolbar)
2010 {
2011   GtkReliefStyle button_relief = GTK_RELIEF_NORMAL;
2012
2013   gtk_widget_ensure_style (GTK_WIDGET (toolbar));
2014   
2015   gtk_widget_style_get (GTK_WIDGET (toolbar),
2016                         "button_relief", &button_relief,
2017                         NULL);
2018
2019   return button_relief;
2020 }
2021
2022 static gint
2023 get_space_size (GtkToolbar *toolbar)
2024 {
2025   gint space_size = DEFAULT_SPACE_SIZE;
2026
2027   gtk_widget_style_get (GTK_WIDGET (toolbar),
2028                         "space_size", &space_size,
2029                         NULL);
2030
2031   return space_size;
2032 }
2033
2034 static gint
2035 get_internal_padding (GtkToolbar *toolbar)
2036 {
2037   gint ipadding = 0;
2038
2039   gtk_widget_style_get (GTK_WIDGET (toolbar),
2040                         "internal_padding", &ipadding,
2041                         NULL);
2042
2043   return ipadding;
2044 }
2045
2046 static gboolean
2047 gtk_toolbar_check_old_api (GtkToolbar *toolbar)
2048 {
2049   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2050
2051   if (priv->api_mode == NEW_API)
2052     {
2053       g_warning ("mixing deprecated and non-deprecated GtkToolbar API is not allowed");
2054       return FALSE;
2055     }
2056
2057   priv->api_mode = OLD_API;
2058   return TRUE;
2059 }
2060
2061 static gboolean
2062 gtk_toolbar_check_new_api (GtkToolbar *toolbar)
2063 {
2064   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2065
2066   if (priv->api_mode == OLD_API)
2067     {
2068       g_warning ("mixing deprecated and non-deprecated GtkToolbar API is not allowed");
2069       return FALSE;
2070     }
2071   
2072   priv->api_mode = NEW_API;
2073   return TRUE;
2074 }
2075
2076 static void
2077 gtk_toolbar_insert_tool_item (GtkToolbar  *toolbar,
2078                               GtkToolItem *item,
2079                               gint         pos)
2080 {
2081   GtkToolbarPrivate *priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2082   
2083   priv->items = g_list_insert (priv->items, item, pos);
2084   toolbar->num_children++;
2085
2086   gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (toolbar));
2087 }
2088
2089 static void
2090 gtk_toolbar_remove_tool_item (GtkToolbar  *toolbar,
2091                               GtkToolItem *item)
2092 {
2093   GtkToolbarPrivate *priv;
2094   GList *tmp;
2095   gint nth_child;
2096
2097   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2098   priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2099   g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2100   g_return_if_fail (g_list_find (priv->items, item));
2101   
2102   nth_child = 0;
2103
2104   for (tmp = priv->items; tmp != NULL; tmp = tmp->next)
2105     {
2106       if (tmp->data == item)
2107         break;
2108
2109       nth_child++;
2110     }
2111
2112   priv->items = g_list_remove (priv->items, item);
2113
2114   gtk_widget_unparent (GTK_WIDGET (item));
2115
2116   if (priv->api_mode == OLD_API)
2117     {
2118       GtkToolbarChild *toolbar_child;
2119
2120       toolbar_child = g_list_nth_data (toolbar->children, nth_child);
2121       toolbar->children = g_list_remove (toolbar->children, toolbar_child);
2122
2123       g_free (toolbar_child);
2124     }
2125
2126   gtk_widget_queue_resize (GTK_WIDGET (toolbar));
2127 }
2128
2129 GtkWidget *
2130 gtk_toolbar_new (void)
2131 {
2132   GtkToolbar *toolbar;
2133
2134   toolbar = g_object_new (GTK_TYPE_TOOLBAR, NULL);
2135
2136   return GTK_WIDGET (toolbar);
2137 }
2138
2139 void
2140 gtk_toolbar_insert (GtkToolbar  *toolbar,
2141                     GtkToolItem *item,
2142                     gint         pos)
2143 {
2144   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2145   g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2146
2147   if (!gtk_toolbar_check_new_api (toolbar))
2148     return;
2149   
2150   gtk_toolbar_insert_tool_item (toolbar, item, pos);
2151 }
2152
2153 gint
2154 gtk_toolbar_get_item_index (GtkToolbar  *toolbar,
2155                             GtkToolItem *item)
2156 {
2157   GtkToolbarPrivate *priv;
2158
2159   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), -1);
2160   g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2161
2162   if (!gtk_toolbar_check_new_api (toolbar))
2163     return -1;
2164   
2165   priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2166   g_return_val_if_fail (g_list_find (priv->items, item) != NULL, -1);
2167
2168   return g_list_index (priv->items, item);
2169 }
2170
2171 void
2172 gtk_toolbar_set_orientation (GtkToolbar     *toolbar,
2173                              GtkOrientation  orientation)
2174 {
2175   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2176
2177   g_signal_emit (toolbar, toolbar_signals[ORIENTATION_CHANGED], 0, orientation);
2178 }
2179
2180 GtkOrientation
2181 gtk_toolbar_get_orientation (GtkToolbar *toolbar)
2182 {
2183   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), GTK_ORIENTATION_HORIZONTAL);
2184
2185   return toolbar->orientation;
2186 }
2187
2188 void
2189 gtk_toolbar_set_style (GtkToolbar      *toolbar,
2190                        GtkToolbarStyle  style)
2191 {
2192   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2193
2194   toolbar->style_set = TRUE;  
2195   g_signal_emit (toolbar, toolbar_signals[STYLE_CHANGED], 0, style);
2196
2197   
2198 }
2199
2200 GtkToolbarStyle
2201 gtk_toolbar_get_style (GtkToolbar *toolbar)
2202 {
2203   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), DEFAULT_TOOLBAR_STYLE);
2204
2205   return toolbar->style;
2206 }
2207
2208 void
2209 gtk_toolbar_unset_style (GtkToolbar *toolbar)
2210 {
2211   GtkToolbarStyle style;
2212
2213   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2214
2215   if (toolbar->style_set)
2216     {
2217       GtkSettings *settings = toolbar_get_settings (toolbar);
2218
2219       if (settings)
2220         g_object_get (settings,
2221                       "gtk-toolbar-style", &style,
2222                       NULL);
2223       else
2224         style = DEFAULT_TOOLBAR_STYLE;
2225
2226       if (style != toolbar->style)
2227         g_signal_emit (toolbar, toolbar_signals[STYLE_CHANGED], 0, style);
2228
2229       toolbar->style_set = FALSE;
2230     }
2231 }
2232
2233 void
2234 gtk_toolbar_set_tooltips (GtkToolbar *toolbar,
2235                           gboolean    enable)
2236 {
2237   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2238
2239   if (enable)
2240     gtk_tooltips_enable (toolbar->tooltips);
2241   else
2242     gtk_tooltips_disable (toolbar->tooltips);
2243 }
2244
2245 gboolean
2246 gtk_toolbar_get_tooltips (GtkToolbar *toolbar)
2247 {
2248   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
2249
2250   return toolbar->tooltips->enabled;
2251 }
2252
2253 gint
2254 gtk_toolbar_get_n_items (GtkToolbar *toolbar)
2255 {
2256   GtkToolbarPrivate *priv;
2257
2258   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), -1);
2259
2260   if (!gtk_toolbar_check_new_api (toolbar))
2261     return -1;
2262   
2263   priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2264   
2265   return g_list_length (priv->items);
2266 }
2267
2268 /*
2269  * returns NULL if n is out of range
2270  */
2271 GtkToolItem *
2272 gtk_toolbar_get_nth_item (GtkToolbar *toolbar,
2273                           gint        n)
2274 {
2275   GtkToolbarPrivate *priv;
2276
2277   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), NULL);
2278
2279   if (!gtk_toolbar_check_new_api (toolbar))
2280     return NULL;
2281   
2282   priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2283   
2284   return g_list_nth_data (priv->items, n);
2285 }
2286
2287 void
2288 gtk_toolbar_set_icon_size (GtkToolbar  *toolbar,
2289                            GtkIconSize  icon_size)
2290 {
2291   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2292
2293   toolbar->icon_size_set = TRUE;
2294
2295   if (toolbar->icon_size == icon_size)
2296     return;
2297
2298   toolbar->icon_size = icon_size;
2299
2300   gtk_toolbar_reconfigured (toolbar);
2301
2302   gtk_widget_queue_resize (GTK_WIDGET (toolbar));
2303 }
2304
2305 GtkIconSize
2306 gtk_toolbar_get_icon_size (GtkToolbar *toolbar)
2307 {
2308   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), DEFAULT_ICON_SIZE);
2309
2310   return toolbar->icon_size;
2311 }
2312
2313 GtkReliefStyle
2314 gtk_toolbar_get_relief_style (GtkToolbar *toolbar)
2315 {
2316   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), GTK_RELIEF_NONE);
2317
2318   return get_button_relief (toolbar);
2319 }
2320
2321 void
2322 gtk_toolbar_unset_icon_size (GtkToolbar *toolbar)
2323 {
2324   GtkIconSize size;
2325
2326   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2327   
2328   if (toolbar->icon_size_set)
2329     {
2330       GtkSettings *settings = toolbar_get_settings (toolbar);
2331
2332       if (settings)
2333         {
2334           g_object_get (settings,
2335                         "gtk-toolbar-icon-size", &size,
2336                         NULL);
2337         }
2338       else
2339         size = DEFAULT_ICON_SIZE;
2340
2341       if (size != toolbar->icon_size)
2342         gtk_toolbar_set_icon_size (toolbar, size);
2343
2344       toolbar->icon_size_set = FALSE;
2345     }
2346 }
2347
2348 void
2349 gtk_toolbar_set_show_arrow (GtkToolbar *toolbar,
2350                             gboolean    show_arrow)
2351 {
2352   GtkToolbarPrivate *priv;
2353   
2354   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2355
2356   priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2357   show_arrow = show_arrow != FALSE;
2358
2359   if (priv->show_arrow != show_arrow)
2360     {
2361       priv->show_arrow = show_arrow;
2362       
2363       if (!priv->show_arrow)
2364         gtk_widget_hide (priv->arrow_button);
2365       
2366       gtk_widget_queue_resize (GTK_WIDGET (toolbar));      
2367       g_object_notify (G_OBJECT (toolbar), "show_arrow");
2368     }
2369 }
2370
2371 gboolean
2372 gtk_toolbar_get_show_arrow (GtkToolbar *toolbar)
2373 {
2374   GtkToolbarPrivate *priv;
2375
2376   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
2377
2378   if (!gtk_toolbar_check_new_api (toolbar))
2379     return FALSE;
2380   
2381   priv = GTK_TOOLBAR_GET_PRIVATE (toolbar);
2382   
2383   return priv->show_arrow;
2384 }
2385
2386 gint
2387 gtk_toolbar_get_drop_index (GtkToolbar *toolbar,
2388                             gint        x,
2389                             gint        y)
2390 {
2391   gint drop_index, drop_pos;
2392
2393   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), FALSE);
2394
2395   if (!gtk_toolbar_check_new_api (toolbar))
2396     return -1;
2397   
2398   find_drop_pos (toolbar, x, y, &drop_index, &drop_pos);
2399
2400   return drop_index;
2401 }
2402
2403 GtkWidget *
2404 gtk_toolbar_append_item (GtkToolbar    *toolbar,
2405                          const char    *text,
2406                          const char    *tooltip_text,
2407                          const char    *tooltip_private_text,
2408                          GtkWidget     *icon,
2409                          GtkSignalFunc  callback,
2410                          gpointer       user_data)
2411 {
2412   return gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_BUTTON,
2413                                      NULL, text,
2414                                      tooltip_text, tooltip_private_text,
2415                                      icon, callback, user_data,
2416                                      toolbar->num_children);
2417 }
2418
2419 GtkWidget *
2420 gtk_toolbar_prepend_item (GtkToolbar    *toolbar,
2421                           const char    *text,
2422                           const char    *tooltip_text,
2423                           const char    *tooltip_private_text,
2424                           GtkWidget     *icon,
2425                           GtkSignalFunc  callback,
2426                           gpointer       user_data)
2427 {
2428   return gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_BUTTON,
2429                                      NULL, text,
2430                                      tooltip_text, tooltip_private_text,
2431                                      icon, callback, user_data,
2432                                      0);
2433 }
2434
2435 GtkWidget *
2436 gtk_toolbar_insert_item (GtkToolbar    *toolbar,
2437                          const char    *text,
2438                          const char    *tooltip_text,
2439                          const char    *tooltip_private_text,
2440                          GtkWidget     *icon,
2441                          GtkSignalFunc  callback,
2442                          gpointer       user_data,
2443                          gint           position)
2444 {
2445   return gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_BUTTON,
2446                                      NULL, text,
2447                                      tooltip_text, tooltip_private_text,
2448                                      icon, callback, user_data,
2449                                      position);
2450 }
2451
2452 GtkWidget*
2453 gtk_toolbar_insert_stock (GtkToolbar      *toolbar,
2454                           const gchar     *stock_id,
2455                           const char      *tooltip_text,
2456                           const char      *tooltip_private_text,
2457                           GtkSignalFunc    callback,
2458                           gpointer         user_data,
2459                           gint             position)
2460 {
2461   return gtk_toolbar_internal_insert_element (toolbar, GTK_TOOLBAR_CHILD_BUTTON,
2462                                               NULL, stock_id,
2463                                               tooltip_text, tooltip_private_text,
2464                                               NULL, callback, user_data,
2465                                               position, TRUE);
2466 }
2467
2468 void
2469 gtk_toolbar_append_space (GtkToolbar *toolbar)
2470 {
2471   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_SPACE,
2472                               NULL, NULL,
2473                               NULL, NULL,
2474                               NULL, NULL, NULL,
2475                               toolbar->num_children);
2476 }
2477
2478 void
2479 gtk_toolbar_prepend_space (GtkToolbar *toolbar)
2480 {
2481   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_SPACE,
2482                               NULL, NULL,
2483                               NULL, NULL,
2484                               NULL, NULL, NULL,
2485                               0);
2486 }
2487
2488 void
2489 gtk_toolbar_insert_space (GtkToolbar *toolbar,
2490                           gint        position)
2491 {
2492   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_SPACE,
2493                               NULL, NULL,
2494                               NULL, NULL,
2495                               NULL, NULL, NULL,
2496                               position);
2497 }
2498
2499 void
2500 gtk_toolbar_remove_space (GtkToolbar *toolbar,
2501                           gint        position)
2502 {
2503   GtkToolItem *item;
2504
2505   g_return_if_fail (GTK_IS_TOOLBAR (toolbar));
2506
2507   if (!gtk_toolbar_check_old_api (toolbar))
2508     return;
2509   
2510   item = g_list_nth_data (toolbar->children, position);
2511
2512   if (!item)
2513     {
2514       g_warning ("Toolbar position %d doesn't exist", position);
2515       return;
2516     }
2517
2518   if (GTK_BIN (item)->child)
2519     {
2520       g_warning ("Toolbar position %d is not a space", position);
2521     }
2522
2523   gtk_toolbar_remove_tool_item (toolbar, item);
2524 }
2525
2526 void
2527 gtk_toolbar_append_widget (GtkToolbar  *toolbar,
2528                            GtkWidget   *widget,
2529                            const gchar *tooltip_text,
2530                            const gchar *tooltip_private_text)
2531 {
2532   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_WIDGET,
2533                               widget, NULL,
2534                               tooltip_text, tooltip_private_text,
2535                               NULL, NULL, NULL,
2536                               toolbar->num_children);
2537 }
2538
2539 void
2540 gtk_toolbar_prepend_widget (GtkToolbar  *toolbar,
2541                             GtkWidget   *widget,
2542                             const gchar *tooltip_text,
2543                             const gchar *tooltip_private_text)
2544 {
2545   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_WIDGET,
2546                               widget, NULL,
2547                               tooltip_text, tooltip_private_text,
2548                               NULL, NULL, NULL,
2549                               0);
2550 }
2551
2552 void
2553 gtk_toolbar_insert_widget (GtkToolbar *toolbar,
2554                            GtkWidget  *widget,
2555                            const char *tooltip_text,
2556                            const char *tooltip_private_text,
2557                            gint        position)
2558 {
2559   gtk_toolbar_insert_element (toolbar, GTK_TOOLBAR_CHILD_WIDGET,
2560                               widget, NULL,
2561                               tooltip_text, tooltip_private_text,
2562                               NULL, NULL, NULL,
2563                               position);
2564 }
2565
2566 GtkWidget*
2567 gtk_toolbar_append_element (GtkToolbar          *toolbar,
2568                             GtkToolbarChildType  type,
2569                             GtkWidget           *widget,
2570                             const char          *text,
2571                             const char          *tooltip_text,
2572                             const char          *tooltip_private_text,
2573                             GtkWidget           *icon,
2574                             GtkSignalFunc        callback,
2575                             gpointer             user_data)
2576 {
2577   return gtk_toolbar_insert_element (toolbar, type, widget, text,
2578                                      tooltip_text, tooltip_private_text,
2579                                      icon, callback, user_data,
2580                                      toolbar->num_children);
2581 }
2582
2583 GtkWidget *
2584 gtk_toolbar_prepend_element (GtkToolbar          *toolbar,
2585                              GtkToolbarChildType  type,
2586                              GtkWidget           *widget,
2587                              const char          *text,
2588                              const char          *tooltip_text,
2589                              const char          *tooltip_private_text,
2590                              GtkWidget           *icon,
2591                              GtkSignalFunc        callback,
2592                              gpointer             user_data)
2593 {
2594   return gtk_toolbar_insert_element (toolbar, type, widget, text,
2595                                      tooltip_text, tooltip_private_text,
2596                                      icon, callback, user_data, 0);
2597 }
2598
2599 GtkWidget *
2600 gtk_toolbar_insert_element (GtkToolbar          *toolbar,
2601                             GtkToolbarChildType  type,
2602                             GtkWidget           *widget,
2603                             const char          *text,
2604                             const char          *tooltip_text,
2605                             const char          *tooltip_private_text,
2606                             GtkWidget           *icon,
2607                             GtkSignalFunc        callback,
2608                             gpointer             user_data,
2609                             gint                 position)
2610 {
2611   return gtk_toolbar_internal_insert_element (toolbar, type, widget, text,
2612                                               tooltip_text, tooltip_private_text,
2613                                               icon, callback, user_data, position, FALSE);
2614 }
2615
2616 gchar *
2617 _gtk_toolbar_elide_underscores (const gchar *original)
2618 {
2619   gchar *q, *result;
2620   const gchar *p;
2621   gboolean last_underscore;
2622
2623   q = result = g_malloc (strlen (original) + 1);
2624   last_underscore = FALSE;
2625   
2626   for (p = original; *p; p++)
2627     {
2628       if (!last_underscore && *p == '_')
2629         last_underscore = TRUE;
2630       else
2631         {
2632           last_underscore = FALSE;
2633           *q++ = *p;
2634         }
2635     }
2636   
2637   *q = '\0';
2638   
2639   return result;
2640 }
2641
2642 static GtkWidget *
2643 gtk_toolbar_internal_insert_element (GtkToolbar          *toolbar,
2644                                      GtkToolbarChildType  type,
2645                                      GtkWidget           *widget,
2646                                      const char          *text,
2647                                      const char          *tooltip_text,
2648                                      const char          *tooltip_private_text,
2649                                      GtkWidget           *icon,
2650                                      GtkSignalFunc        callback,
2651                                      gpointer             user_data,
2652                                      gint                 position,
2653                                      gboolean             use_stock)
2654 {
2655   GtkToolbarChild *child;
2656   GtkToolItem *item = NULL;
2657   
2658   g_return_val_if_fail (GTK_IS_TOOLBAR (toolbar), NULL);
2659   
2660   if (!gtk_toolbar_check_old_api (toolbar))
2661     return NULL;
2662   
2663   if (type == GTK_TOOLBAR_CHILD_WIDGET)
2664     g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2665   else if (type != GTK_TOOLBAR_CHILD_RADIOBUTTON)
2666     g_return_val_if_fail (widget == NULL, NULL);
2667
2668   child = g_new (GtkToolbarChild, 1);
2669
2670   child->type = type;
2671   child->icon = NULL;
2672   child->label = NULL;
2673
2674   switch (type)
2675     {
2676     case GTK_TOOLBAR_CHILD_SPACE:
2677       item = gtk_separator_tool_item_new ();
2678       child->widget = NULL;
2679       break;
2680
2681     case GTK_TOOLBAR_CHILD_WIDGET:
2682       item = gtk_tool_item_new ();
2683       child->widget = widget;
2684       gtk_container_add (GTK_CONTAINER (item), child->widget);
2685       break;
2686
2687     case GTK_TOOLBAR_CHILD_BUTTON:
2688       item = gtk_tool_button_new (NULL, NULL);
2689       child->widget = GTK_TOOL_BUTTON (item)->button;
2690       break;
2691       
2692     case GTK_TOOLBAR_CHILD_TOGGLEBUTTON:
2693       item = gtk_toggle_tool_button_new ();
2694       child->widget = GTK_TOOL_BUTTON (item)->button;
2695       break;
2696
2697     case GTK_TOOLBAR_CHILD_RADIOBUTTON:
2698       item = gtk_radio_tool_button_new (widget
2699                                         ? gtk_radio_button_get_group (GTK_RADIO_BUTTON (widget))
2700                                         : NULL);
2701       child->widget = GTK_TOOL_BUTTON (item)->button;
2702       break;
2703     }
2704
2705   gtk_widget_show (GTK_WIDGET (item));
2706   
2707   if (type == GTK_TOOLBAR_CHILD_BUTTON ||
2708       type == GTK_TOOLBAR_CHILD_RADIOBUTTON ||
2709       type == GTK_TOOLBAR_CHILD_TOGGLEBUTTON)
2710     {
2711       if (text)
2712         {
2713           if (use_stock)
2714             {
2715               GtkStockItem stock_item;
2716               gchar *label_text;
2717
2718               gtk_tool_button_set_stock_id (GTK_TOOL_BUTTON (item), text);
2719
2720               gtk_stock_lookup (text, &stock_item);
2721               label_text = _gtk_toolbar_elide_underscores (stock_item.label);
2722               child->label = GTK_WIDGET (gtk_label_new (label_text));
2723               g_free (label_text);
2724             }
2725           else
2726             {
2727               child->label = gtk_label_new (text);
2728             }
2729           gtk_tool_button_set_label_widget (GTK_TOOL_BUTTON (item), child->label);
2730           gtk_widget_show (child->label);
2731         }
2732
2733       if (icon)
2734         {
2735           child->icon = icon;
2736           gtk_tool_button_set_icon_widget (GTK_TOOL_BUTTON (item), icon);
2737
2738           /* Applications depend on the toolbar showing the widget for them */
2739           gtk_widget_show (GTK_WIDGET (icon));
2740         }
2741
2742       /*
2743        * We need to connect to the button's clicked callback because some
2744        * programs may rely on that the widget in the callback is a GtkButton
2745        */
2746       if (callback)
2747         g_signal_connect (child->widget, "clicked",
2748                           callback, user_data);
2749     }
2750
2751   if ((type != GTK_TOOLBAR_CHILD_SPACE) && tooltip_text)
2752     gtk_tool_item_set_tooltip (item, toolbar->tooltips,
2753                                tooltip_text, tooltip_private_text);
2754   
2755   toolbar->children = g_list_insert (toolbar->children, child, position);
2756
2757   gtk_toolbar_insert_tool_item (toolbar, item, position);
2758
2759   return child->widget;
2760 }
2761
2762 static void
2763 gtk_toolbar_finalize (GObject *object)
2764 {
2765   GList *list;
2766   GtkToolbar *toolbar = GTK_TOOLBAR (object);
2767
2768   if (toolbar->tooltips)
2769     g_object_unref (toolbar->tooltips);
2770
2771   for (list = toolbar->children; list != NULL; list = list->next)
2772     g_free (list->data);
2773
2774   g_list_free (toolbar->children);
2775   
2776   G_OBJECT_CLASS (parent_class)->finalize (object);
2777 }