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