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