]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenubar.c
Move deprectated menu enumerations to a private header
[~andy/gtk] / gtk / gtkmenubar.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "config.h"
28 #include "gdk/gdkkeysyms.h"
29 #include "gtkbindings.h"
30 #include "gtkmain.h"
31 #include "gtkmarshalers.h"
32 #include "gtkmenubar.h"
33 #include "gtkmenuitem.h"
34 #include "gtkmenuprivate.h"
35 #include "gtksettings.h"
36 #include "gtkintl.h"
37 #include "gtkwindow.h"
38 #include "gtkprivate.h"
39
40 #define BORDER_SPACING  0
41 #define DEFAULT_IPADDING 1
42
43 /* Properties */
44 enum {
45   PROP_0,
46   PROP_PACK_DIRECTION,
47   PROP_CHILD_PACK_DIRECTION
48 };
49
50 struct _GtkMenuBarPrivate
51 {
52   GtkPackDirection pack_direction;
53   GtkPackDirection child_pack_direction;
54 };
55
56
57 static void gtk_menu_bar_set_property      (GObject             *object,
58                                             guint                prop_id,
59                                             const GValue        *value,
60                                             GParamSpec          *pspec);
61 static void gtk_menu_bar_get_property      (GObject             *object,
62                                             guint                prop_id,
63                                             GValue              *value,
64                                             GParamSpec          *pspec);
65 static void gtk_menu_bar_size_request      (GtkWidget       *widget,
66                                             GtkRequisition  *requisition);
67 static void gtk_menu_bar_size_allocate     (GtkWidget       *widget,
68                                             GtkAllocation   *allocation);
69 static void gtk_menu_bar_paint             (GtkWidget       *widget,
70                                             GdkRectangle    *area);
71 static gint gtk_menu_bar_expose            (GtkWidget       *widget,
72                                             GdkEventExpose  *event);
73 static void gtk_menu_bar_hierarchy_changed (GtkWidget       *widget,
74                                             GtkWidget       *old_toplevel);
75 static gint gtk_menu_bar_get_popup_delay   (GtkMenuShell    *menu_shell);
76 static void gtk_menu_bar_move_current      (GtkMenuShell     *menu_shell,
77                                             GtkMenuDirectionType direction);
78
79 static GtkShadowType get_shadow_type   (GtkMenuBar      *menubar);
80
81 G_DEFINE_TYPE (GtkMenuBar, gtk_menu_bar, GTK_TYPE_MENU_SHELL)
82
83 static void
84 gtk_menu_bar_class_init (GtkMenuBarClass *class)
85 {
86   GObjectClass *gobject_class;
87   GtkWidgetClass *widget_class;
88   GtkMenuShellClass *menu_shell_class;
89
90   GtkBindingSet *binding_set;
91
92   gobject_class = (GObjectClass*) class;
93   widget_class = (GtkWidgetClass*) class;
94   menu_shell_class = (GtkMenuShellClass*) class;
95
96   gobject_class->get_property = gtk_menu_bar_get_property;
97   gobject_class->set_property = gtk_menu_bar_set_property;
98
99   widget_class->size_request = gtk_menu_bar_size_request;
100   widget_class->size_allocate = gtk_menu_bar_size_allocate;
101   widget_class->expose_event = gtk_menu_bar_expose;
102   widget_class->hierarchy_changed = gtk_menu_bar_hierarchy_changed;
103   
104   menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
105   menu_shell_class->get_popup_delay = gtk_menu_bar_get_popup_delay;
106   menu_shell_class->move_current = gtk_menu_bar_move_current;
107
108   binding_set = gtk_binding_set_by_class (class);
109   gtk_binding_entry_add_signal (binding_set,
110                                 GDK_Left, 0,
111                                 "move-current", 1,
112                                 GTK_TYPE_MENU_DIRECTION_TYPE,
113                                 GTK_MENU_DIR_PREV);
114   gtk_binding_entry_add_signal (binding_set,
115                                 GDK_KP_Left, 0,
116                                 "move-current", 1,
117                                 GTK_TYPE_MENU_DIRECTION_TYPE,
118                                 GTK_MENU_DIR_PREV);
119   gtk_binding_entry_add_signal (binding_set,
120                                 GDK_Right, 0,
121                                 "move-current", 1,
122                                 GTK_TYPE_MENU_DIRECTION_TYPE,
123                                 GTK_MENU_DIR_NEXT);
124   gtk_binding_entry_add_signal (binding_set,
125                                 GDK_KP_Right, 0,
126                                 "move-current", 1,
127                                 GTK_TYPE_MENU_DIRECTION_TYPE,
128                                 GTK_MENU_DIR_NEXT);
129   gtk_binding_entry_add_signal (binding_set,
130                                 GDK_Up, 0,
131                                 "move-current", 1,
132                                 GTK_TYPE_MENU_DIRECTION_TYPE,
133                                 GTK_MENU_DIR_PARENT);
134   gtk_binding_entry_add_signal (binding_set,
135                                 GDK_KP_Up, 0,
136                                 "move-current", 1,
137                                 GTK_TYPE_MENU_DIRECTION_TYPE,
138                                 GTK_MENU_DIR_PARENT);
139   gtk_binding_entry_add_signal (binding_set,
140                                 GDK_Down, 0,
141                                 "move-current", 1,
142                                 GTK_TYPE_MENU_DIRECTION_TYPE,
143                                 GTK_MENU_DIR_CHILD);
144   gtk_binding_entry_add_signal (binding_set,
145                                 GDK_KP_Down, 0,
146                                 "move-current", 1,
147                                 GTK_TYPE_MENU_DIRECTION_TYPE,
148                                 GTK_MENU_DIR_CHILD);
149
150   /**
151    * GtkMenuBar:pack-direction:
152    *
153    * The pack direction of the menubar. It determines how
154    * menuitems are arranged in the menubar.
155    *
156    * Since: 2.8
157    */
158   g_object_class_install_property (gobject_class,
159                                    PROP_PACK_DIRECTION,
160                                    g_param_spec_enum ("pack-direction",
161                                                       P_("Pack direction"),
162                                                       P_("The pack direction of the menubar"),
163                                                       GTK_TYPE_PACK_DIRECTION,
164                                                       GTK_PACK_DIRECTION_LTR,
165                                                       GTK_PARAM_READWRITE));
166   
167   /**
168    * GtkMenuBar:child-pack-direction:
169    *
170    * The child pack direction of the menubar. It determines how
171    * the widgets contained in child menuitems are arranged.
172    *
173    * Since: 2.8
174    */
175   g_object_class_install_property (gobject_class,
176                                    PROP_CHILD_PACK_DIRECTION,
177                                    g_param_spec_enum ("child-pack-direction",
178                                                       P_("Child Pack direction"),
179                                                       P_("The child pack direction of the menubar"),
180                                                       GTK_TYPE_PACK_DIRECTION,
181                                                       GTK_PACK_DIRECTION_LTR,
182                                                       GTK_PARAM_READWRITE));
183   
184
185   gtk_widget_class_install_style_property (widget_class,
186                                            g_param_spec_enum ("shadow-type",
187                                                               P_("Shadow type"),
188                                                               P_("Style of bevel around the menubar"),
189                                                               GTK_TYPE_SHADOW_TYPE,
190                                                               GTK_SHADOW_OUT,
191                                                               GTK_PARAM_READABLE));
192
193   gtk_widget_class_install_style_property (widget_class,
194                                            g_param_spec_int ("internal-padding",
195                                                              P_("Internal padding"),
196                                                              P_("Amount of border space between the menubar shadow and the menu items"),
197                                                              0,
198                                                              G_MAXINT,
199                                                              DEFAULT_IPADDING,
200                                                              GTK_PARAM_READABLE));
201
202   gtk_settings_install_property (g_param_spec_int ("gtk-menu-bar-popup-delay",
203                                                    P_("Delay before drop down menus appear"),
204                                                    P_("Delay before the submenus of a menu bar appear"),
205                                                    0,
206                                                    G_MAXINT,
207                                                    0,
208                                                    GTK_PARAM_READWRITE));
209
210   g_type_class_add_private (gobject_class, sizeof (GtkMenuBarPrivate));
211 }
212
213 static void
214 gtk_menu_bar_init (GtkMenuBar *menu_bar)
215 {
216   menu_bar->priv = G_TYPE_INSTANCE_GET_PRIVATE (menu_bar,
217                                                 GTK_TYPE_MENU_BAR,
218                                                 GtkMenuBarPrivate);
219 }
220
221 GtkWidget*
222 gtk_menu_bar_new (void)
223 {
224   return g_object_new (GTK_TYPE_MENU_BAR, NULL);
225 }
226
227 static void
228 gtk_menu_bar_set_property (GObject      *object,
229                            guint         prop_id,
230                            const GValue *value,
231                            GParamSpec   *pspec)
232 {
233   GtkMenuBar *menubar = GTK_MENU_BAR (object);
234   
235   switch (prop_id)
236     {
237     case PROP_PACK_DIRECTION:
238       gtk_menu_bar_set_pack_direction (menubar, g_value_get_enum (value));
239       break;
240     case PROP_CHILD_PACK_DIRECTION:
241       gtk_menu_bar_set_child_pack_direction (menubar, g_value_get_enum (value));
242       break;
243     default:
244       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
245       break;
246     }
247 }
248
249 static void
250 gtk_menu_bar_get_property (GObject    *object,
251                            guint       prop_id,
252                            GValue     *value,
253                            GParamSpec *pspec)
254 {
255   GtkMenuBar *menubar = GTK_MENU_BAR (object);
256   
257   switch (prop_id)
258     {
259     case PROP_PACK_DIRECTION:
260       g_value_set_enum (value, gtk_menu_bar_get_pack_direction (menubar));
261       break;
262     case PROP_CHILD_PACK_DIRECTION:
263       g_value_set_enum (value, gtk_menu_bar_get_child_pack_direction (menubar));
264       break;
265     default:
266       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
267       break;
268     }
269 }
270
271 static void
272 gtk_menu_bar_size_request (GtkWidget      *widget,
273                            GtkRequisition *requisition)
274 {
275   GtkMenuBar *menu_bar;
276   GtkMenuBarPrivate *priv;
277   GtkMenuShell *menu_shell;
278   GtkWidget *child;
279   GList *children;
280   gint nchildren;
281   GtkRequisition child_requisition;
282   gint ipadding;
283   guint border_width;
284
285   g_return_if_fail (GTK_IS_MENU_BAR (widget));
286   g_return_if_fail (requisition != NULL);
287
288   requisition->width = 0;
289   requisition->height = 0;
290   
291   if (gtk_widget_get_visible (widget))
292     {
293       menu_bar = GTK_MENU_BAR (widget);
294       menu_shell = GTK_MENU_SHELL (widget);
295       priv = menu_bar->priv;
296
297       nchildren = 0;
298       children = menu_shell->children;
299
300       while (children)
301         {
302           child = children->data;
303           children = children->next;
304
305           if (gtk_widget_get_visible (child))
306             {
307               gint toggle_size;
308
309               GTK_MENU_ITEM (child)->show_submenu_indicator = FALSE;
310               gtk_widget_size_request (child, &child_requisition);
311               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
312                                                  &toggle_size);
313
314               if (priv->child_pack_direction == GTK_PACK_DIRECTION_LTR ||
315                   priv->child_pack_direction == GTK_PACK_DIRECTION_RTL)
316                 child_requisition.width += toggle_size;
317               else
318                 child_requisition.height += toggle_size;
319
320               if (priv->pack_direction == GTK_PACK_DIRECTION_LTR ||
321                   priv->pack_direction == GTK_PACK_DIRECTION_RTL)
322                 {
323                   requisition->width += child_requisition.width;
324                   requisition->height = MAX (requisition->height, child_requisition.height);
325                 }
326               else
327                 {
328                   requisition->width = MAX (requisition->width, child_requisition.width);
329                   requisition->height += child_requisition.height;
330                 }
331               nchildren += 1;
332             }
333         }
334
335       gtk_widget_style_get (widget, "internal-padding", &ipadding, NULL);
336
337       border_width = gtk_container_get_border_width (GTK_CONTAINER (menu_bar));
338       requisition->width += (border_width +
339                              ipadding + 
340                              BORDER_SPACING) * 2;
341       requisition->height += (border_width +
342                               ipadding +
343                               BORDER_SPACING) * 2;
344
345       if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
346         {
347           GtkStyle *style;
348
349           style = gtk_widget_get_style (widget);
350           requisition->width += style->xthickness * 2;
351           requisition->height += style->ythickness * 2;
352         }
353     }
354 }
355
356 static void
357 gtk_menu_bar_size_allocate (GtkWidget     *widget,
358                             GtkAllocation *allocation)
359 {
360   GtkMenuBar *menu_bar;
361   GtkMenuShell *menu_shell;
362   GtkMenuBarPrivate *priv;
363   GtkWidget *child;
364   GList *children;
365   GtkAllocation child_allocation;
366   GtkRequisition child_requisition;
367   guint offset;
368   GtkTextDirection direction;
369   gint ltr_x, ltr_y;
370   gint ipadding;
371   guint border_width;
372
373   g_return_if_fail (GTK_IS_MENU_BAR (widget));
374   g_return_if_fail (allocation != NULL);
375
376   menu_bar = GTK_MENU_BAR (widget);
377   menu_shell = GTK_MENU_SHELL (widget);
378   priv = menu_bar->priv;
379
380   direction = gtk_widget_get_direction (widget);
381
382   gtk_widget_set_allocation (widget, allocation);
383
384   if (gtk_widget_get_realized (widget))
385     gdk_window_move_resize (gtk_widget_get_window (widget),
386                             allocation->x, allocation->y,
387                             allocation->width, allocation->height);
388
389   gtk_widget_style_get (widget, "internal-padding", &ipadding, NULL);
390   
391   if (menu_shell->children)
392     {
393       border_width = gtk_container_get_border_width (GTK_CONTAINER (menu_bar));
394       child_allocation.x = (border_width +
395                             ipadding + 
396                             BORDER_SPACING);
397       child_allocation.y = (border_width +
398                             BORDER_SPACING);
399       
400       if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
401         {
402           GtkStyle *style;
403
404           style = gtk_widget_get_style (widget);
405           child_allocation.x += style->xthickness;
406           child_allocation.y += style->ythickness;
407         }
408       
409       if (priv->pack_direction == GTK_PACK_DIRECTION_LTR ||
410           priv->pack_direction == GTK_PACK_DIRECTION_RTL)
411         {
412           child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
413           
414           offset = child_allocation.x;  /* Window edge to menubar start */
415           ltr_x = child_allocation.x;
416           
417           children = menu_shell->children;
418           while (children)
419             {
420               gint toggle_size;          
421               
422               child = children->data;
423               children = children->next;
424               
425               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
426                                                  &toggle_size);
427               gtk_widget_get_child_requisition (child, &child_requisition);
428             
429               if (priv->child_pack_direction == GTK_PACK_DIRECTION_LTR ||
430                   priv->child_pack_direction == GTK_PACK_DIRECTION_RTL)
431                 child_requisition.width += toggle_size;
432               else
433                 child_requisition.height += toggle_size;
434               
435               /* Support for the right justified help menu */
436               if ((children == NULL) && (GTK_IS_MENU_ITEM(child))
437                   && (GTK_MENU_ITEM(child)->right_justify)) 
438                 {
439                   ltr_x = allocation->width -
440                     child_requisition.width - offset;
441                 }
442               if (gtk_widget_get_visible (child))
443                 {
444                   if ((direction == GTK_TEXT_DIR_LTR) == (priv->pack_direction == GTK_PACK_DIRECTION_LTR))
445                     child_allocation.x = ltr_x;
446                   else
447                     child_allocation.x = allocation->width -
448                       child_requisition.width - ltr_x; 
449                   
450                   child_allocation.width = child_requisition.width;
451                   
452                   gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
453                                                       toggle_size);
454                   gtk_widget_size_allocate (child, &child_allocation);
455                   
456                   ltr_x += child_allocation.width;
457                 }
458             }
459         }
460       else
461         {
462           child_allocation.width = MAX (1, (gint)allocation->width - child_allocation.x * 2);
463           
464           offset = child_allocation.y;  /* Window edge to menubar start */
465           ltr_y = child_allocation.y;
466           
467           children = menu_shell->children;
468           while (children)
469             {
470               gint toggle_size;          
471               
472               child = children->data;
473               children = children->next;
474               
475               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
476                                                  &toggle_size);
477               gtk_widget_get_child_requisition (child, &child_requisition);
478               
479               if (priv->child_pack_direction == GTK_PACK_DIRECTION_LTR ||
480                   priv->child_pack_direction == GTK_PACK_DIRECTION_RTL)
481                 child_requisition.width += toggle_size;
482               else
483                 child_requisition.height += toggle_size;
484               
485               /* Support for the right justified help menu */
486               if ((children == NULL) && (GTK_IS_MENU_ITEM(child))
487                   && (GTK_MENU_ITEM(child)->right_justify)) 
488                 {
489                   ltr_y = allocation->height -
490                     child_requisition.height - offset;
491                 }
492               if (gtk_widget_get_visible (child))
493                 {
494                   if ((direction == GTK_TEXT_DIR_LTR) ==
495                       (priv->pack_direction == GTK_PACK_DIRECTION_TTB))
496                     child_allocation.y = ltr_y;
497                   else
498                     child_allocation.y = allocation->height -
499                       child_requisition.height - ltr_y; 
500                   child_allocation.height = child_requisition.height;
501                   
502                   gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
503                                                       toggle_size);
504                   gtk_widget_size_allocate (child, &child_allocation);
505                   
506                   ltr_y += child_allocation.height;
507                 }
508             }
509         }
510     }
511 }
512
513 static void
514 gtk_menu_bar_paint (GtkWidget    *widget,
515                     GdkRectangle *area)
516 {
517   g_return_if_fail (GTK_IS_MENU_BAR (widget));
518
519   if (gtk_widget_is_drawable (widget))
520     {
521       GtkAllocation allocation;
522       guint border;
523
524       border = gtk_container_get_border_width (GTK_CONTAINER (widget));
525       gtk_widget_get_allocation (widget, &allocation);
526
527       gtk_paint_box (gtk_widget_get_style (widget),
528                      gtk_widget_get_window (widget),
529                      gtk_widget_get_state (widget),
530                      get_shadow_type (GTK_MENU_BAR (widget)),
531                      area, widget, "menubar",
532                      border, border,
533                      allocation.width - border * 2,
534                      allocation.height - border * 2);
535     }
536 }
537
538 static gint
539 gtk_menu_bar_expose (GtkWidget      *widget,
540                      GdkEventExpose *event)
541 {
542   g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
543   g_return_val_if_fail (event != NULL, FALSE);
544
545   if (gtk_widget_is_drawable (widget))
546     {
547       gtk_menu_bar_paint (widget, &event->area);
548
549       GTK_WIDGET_CLASS (gtk_menu_bar_parent_class)->expose_event (widget, event);
550     }
551
552   return FALSE;
553 }
554
555 static GList *
556 get_menu_bars (GtkWindow *window)
557 {
558   return g_object_get_data (G_OBJECT (window), "gtk-menu-bar-list");
559 }
560
561 static GList *
562 get_viewable_menu_bars (GtkWindow *window)
563 {
564   GList *menu_bars;
565   GList *viewable_menu_bars = NULL;
566
567   for (menu_bars = get_menu_bars (window);
568        menu_bars;
569        menu_bars = menu_bars->next)
570     {
571       GtkWidget *widget = menu_bars->data;
572       gboolean viewable = TRUE;
573       
574       while (widget)
575         {
576           if (!gtk_widget_get_mapped (widget))
577             viewable = FALSE;
578
579           widget = gtk_widget_get_parent (widget);
580         }
581
582       if (viewable)
583         viewable_menu_bars = g_list_prepend (viewable_menu_bars, menu_bars->data);
584     }
585
586   return g_list_reverse (viewable_menu_bars);
587 }
588
589 static void
590 set_menu_bars (GtkWindow *window,
591                GList     *menubars)
592 {
593   g_object_set_data (G_OBJECT (window), I_("gtk-menu-bar-list"), menubars);
594 }
595
596 static gboolean
597 window_key_press_handler (GtkWidget   *widget,
598                           GdkEventKey *event,
599                           gpointer     data)
600 {
601   gchar *accel = NULL;
602   gboolean retval = FALSE;
603   
604   g_object_get (gtk_widget_get_settings (widget),
605                 "gtk-menu-bar-accel", &accel,
606                 NULL);
607
608   if (accel && *accel)
609     {
610       guint keyval = 0;
611       GdkModifierType mods = 0;
612
613       gtk_accelerator_parse (accel, &keyval, &mods);
614
615       if (keyval == 0)
616         g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
617
618       /* FIXME this is wrong, needs to be in the global accel resolution
619        * thing, to properly consider i18n etc., but that probably requires
620        * AccelGroup changes etc.
621        */
622       if (event->keyval == keyval &&
623           ((event->state & gtk_accelerator_get_default_mod_mask ()) ==
624            (mods & gtk_accelerator_get_default_mod_mask ())))
625         {
626           GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (widget));
627           GList *menubars;
628
629           menubars = _gtk_container_focus_sort (GTK_CONTAINER (widget), tmp_menubars,
630                                                 GTK_DIR_TAB_FORWARD, NULL);
631           g_list_free (tmp_menubars);
632           
633           if (menubars)
634             {
635               GtkMenuShell *menu_shell = GTK_MENU_SHELL (menubars->data);
636
637               _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
638               _gtk_menu_shell_activate (menu_shell);
639               gtk_menu_shell_select_first (menu_shell, FALSE);
640               
641               g_list_free (menubars);
642               
643               retval = TRUE;          
644             }
645         }
646     }
647
648   g_free (accel);
649
650   return retval;
651 }
652
653 static void
654 add_to_window (GtkWindow  *window,
655                GtkMenuBar *menubar)
656 {
657   GList *menubars = get_menu_bars (window);
658
659   if (!menubars)
660     {
661       g_signal_connect (window,
662                         "key-press-event",
663                         G_CALLBACK (window_key_press_handler),
664                         NULL);
665     }
666
667   set_menu_bars (window, g_list_prepend (menubars, menubar));
668 }
669
670 static void
671 remove_from_window (GtkWindow  *window,
672                     GtkMenuBar *menubar)
673 {
674   GList *menubars = get_menu_bars (window);
675
676   menubars = g_list_remove (menubars, menubar);
677
678   if (!menubars)
679     {
680       g_signal_handlers_disconnect_by_func (window,
681                                             window_key_press_handler,
682                                             NULL);
683     }
684
685   set_menu_bars (window, menubars);
686 }
687
688 static void
689 gtk_menu_bar_hierarchy_changed (GtkWidget *widget,
690                                 GtkWidget *old_toplevel)
691 {
692   GtkWidget *toplevel;  
693   GtkMenuBar *menubar;
694
695   menubar = GTK_MENU_BAR (widget);
696
697   toplevel = gtk_widget_get_toplevel (widget);
698
699   if (old_toplevel)
700     remove_from_window (GTK_WINDOW (old_toplevel), menubar);
701   
702   if (gtk_widget_is_toplevel (toplevel))
703     add_to_window (GTK_WINDOW (toplevel), menubar);
704 }
705
706 /**
707  * _gtk_menu_bar_cycle_focus:
708  * @menubar: a #GtkMenuBar
709  * @dir: direction in which to cycle the focus
710  * 
711  * Move the focus between menubars in the toplevel.
712  **/
713 void
714 _gtk_menu_bar_cycle_focus (GtkMenuBar       *menubar,
715                            GtkDirectionType  dir)
716 {
717   GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menubar));
718   GtkMenuItem *to_activate = NULL;
719
720   if (gtk_widget_is_toplevel (toplevel))
721     {
722       GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (toplevel));
723       GList *menubars;
724       GList *current;
725
726       menubars = _gtk_container_focus_sort (GTK_CONTAINER (toplevel), tmp_menubars,
727                                             dir, GTK_WIDGET (menubar));
728       g_list_free (tmp_menubars);
729
730       if (menubars)
731         {
732           current = g_list_find (menubars, menubar);
733
734           if (current && current->next)
735             {
736               GtkMenuShell *new_menushell = GTK_MENU_SHELL (current->next->data);
737               if (new_menushell->children)
738                 to_activate = new_menushell->children->data;
739             }
740         }
741           
742       g_list_free (menubars);
743     }
744
745   gtk_menu_shell_cancel (GTK_MENU_SHELL (menubar));
746
747   if (to_activate)
748     g_signal_emit_by_name (to_activate, "activate_item");
749 }
750
751 static GtkShadowType
752 get_shadow_type (GtkMenuBar *menubar)
753 {
754   GtkShadowType shadow_type = GTK_SHADOW_OUT;
755   
756   gtk_widget_style_get (GTK_WIDGET (menubar),
757                         "shadow-type", &shadow_type,
758                         NULL);
759
760   return shadow_type;
761 }
762
763 static gint
764 gtk_menu_bar_get_popup_delay (GtkMenuShell *menu_shell)
765 {
766   gint popup_delay;
767   
768   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
769                 "gtk-menu-bar-popup-delay", &popup_delay,
770                 NULL);
771
772   return popup_delay;
773 }
774
775 static void
776 gtk_menu_bar_move_current (GtkMenuShell         *menu_shell,
777                            GtkMenuDirectionType  direction)
778 {
779   GtkMenuBar *menubar = GTK_MENU_BAR (menu_shell);
780   GtkTextDirection text_dir;
781   GtkPackDirection pack_dir;
782
783   text_dir = gtk_widget_get_direction (GTK_WIDGET (menubar));
784   pack_dir = gtk_menu_bar_get_pack_direction (menubar);
785   
786   if (pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL)
787      {
788       if ((text_dir == GTK_TEXT_DIR_RTL) == (pack_dir == GTK_PACK_DIRECTION_LTR))
789         {
790           switch (direction) 
791             {      
792             case GTK_MENU_DIR_PREV:
793               direction = GTK_MENU_DIR_NEXT;
794               break;
795             case GTK_MENU_DIR_NEXT:
796               direction = GTK_MENU_DIR_PREV;
797               break;
798             default: ;
799             }
800         }
801     }
802   else
803     {
804       switch (direction) 
805         {
806         case GTK_MENU_DIR_PARENT:
807           if ((text_dir == GTK_TEXT_DIR_LTR) == (pack_dir == GTK_PACK_DIRECTION_TTB))
808             direction = GTK_MENU_DIR_PREV;
809           else
810             direction = GTK_MENU_DIR_NEXT;
811           break;
812         case GTK_MENU_DIR_CHILD:
813           if ((text_dir == GTK_TEXT_DIR_LTR) == (pack_dir == GTK_PACK_DIRECTION_TTB))
814             direction = GTK_MENU_DIR_NEXT;
815           else
816             direction = GTK_MENU_DIR_PREV;
817           break;
818         case GTK_MENU_DIR_PREV:
819           if (text_dir == GTK_TEXT_DIR_RTL)       
820             direction = GTK_MENU_DIR_CHILD;
821           else
822             direction = GTK_MENU_DIR_PARENT;
823           break;
824         case GTK_MENU_DIR_NEXT:
825           if (text_dir == GTK_TEXT_DIR_RTL)       
826             direction = GTK_MENU_DIR_PARENT;
827           else
828             direction = GTK_MENU_DIR_CHILD;
829           break;
830         default: ;
831         }
832     }
833   
834   GTK_MENU_SHELL_CLASS (gtk_menu_bar_parent_class)->move_current (menu_shell, direction);
835 }
836
837 /**
838  * gtk_menu_bar_get_pack_direction:
839  * @menubar: a #GtkMenuBar
840  * 
841  * Retrieves the current pack direction of the menubar. 
842  * See gtk_menu_bar_set_pack_direction().
843  *
844  * Return value: the pack direction
845  *
846  * Since: 2.8
847  */
848 GtkPackDirection
849 gtk_menu_bar_get_pack_direction (GtkMenuBar *menubar)
850 {
851   g_return_val_if_fail (GTK_IS_MENU_BAR (menubar), 
852                         GTK_PACK_DIRECTION_LTR);
853
854   return menubar->priv->pack_direction;
855 }
856
857 /**
858  * gtk_menu_bar_set_pack_direction:
859  * @menubar: a #GtkMenuBar
860  * @pack_dir: a new #GtkPackDirection
861  * 
862  * Sets how items should be packed inside a menubar.
863  * 
864  * Since: 2.8
865  */
866 void
867 gtk_menu_bar_set_pack_direction (GtkMenuBar       *menubar,
868                                  GtkPackDirection  pack_dir)
869 {
870   GtkMenuBarPrivate *priv;
871   GList *l;
872
873   g_return_if_fail (GTK_IS_MENU_BAR (menubar));
874
875   priv = menubar->priv;
876
877   if (priv->pack_direction != pack_dir)
878     {
879       priv->pack_direction = pack_dir;
880
881       gtk_widget_queue_resize (GTK_WIDGET (menubar));
882
883       for (l = GTK_MENU_SHELL (menubar)->children; l; l = l->next)
884         gtk_widget_queue_resize (GTK_WIDGET (l->data));
885
886       g_object_notify (G_OBJECT (menubar), "pack-direction");
887     }
888 }
889
890 /**
891  * gtk_menu_bar_get_child_pack_direction:
892  * @menubar: a #GtkMenuBar
893  * 
894  * Retrieves the current child pack direction of the menubar.
895  * See gtk_menu_bar_set_child_pack_direction().
896  *
897  * Return value: the child pack direction
898  *
899  * Since: 2.8
900  */
901 GtkPackDirection
902 gtk_menu_bar_get_child_pack_direction (GtkMenuBar *menubar)
903 {
904   g_return_val_if_fail (GTK_IS_MENU_BAR (menubar), 
905                         GTK_PACK_DIRECTION_LTR);
906
907   return menubar->priv->child_pack_direction;
908 }
909
910 /**
911  * gtk_menu_bar_set_child_pack_direction:
912  * @menubar: a #GtkMenuBar
913  * @child_pack_dir: a new #GtkPackDirection
914  * 
915  * Sets how widgets should be packed inside the children of a menubar.
916  * 
917  * Since: 2.8
918  */
919 void
920 gtk_menu_bar_set_child_pack_direction (GtkMenuBar       *menubar,
921                                        GtkPackDirection  child_pack_dir)
922 {
923   GtkMenuBarPrivate *priv;
924   GList *l;
925
926   g_return_if_fail (GTK_IS_MENU_BAR (menubar));
927
928   priv = menubar->priv;
929
930   if (priv->child_pack_direction != child_pack_dir)
931     {
932       priv->child_pack_direction = child_pack_dir;
933
934       gtk_widget_queue_resize (GTK_WIDGET (menubar));
935
936       for (l = GTK_MENU_SHELL (menubar)->children; l; l = l->next)
937         gtk_widget_queue_resize (GTK_WIDGET (l->data));
938
939       g_object_notify (G_OBJECT (menubar), "child-pack-direction");
940     }
941 }