]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenubar.c
gtk/gtkmenubar.c: use accessor functions to access GtkWidget
[~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           GtkStyle *style;
349
350           style = gtk_widget_get_style (widget);
351           requisition->width += style->xthickness * 2;
352           requisition->height += style->ythickness * 2;
353         }
354     }
355 }
356
357 static void
358 gtk_menu_bar_size_allocate (GtkWidget     *widget,
359                             GtkAllocation *allocation)
360 {
361   GtkMenuBar *menu_bar;
362   GtkMenuShell *menu_shell;
363   GtkMenuBarPrivate *priv;
364   GtkWidget *child;
365   GList *children;
366   GtkAllocation child_allocation;
367   GtkRequisition child_requisition;
368   guint offset;
369   GtkTextDirection direction;
370   gint ltr_x, ltr_y;
371   gint ipadding;
372   guint border_width;
373
374   g_return_if_fail (GTK_IS_MENU_BAR (widget));
375   g_return_if_fail (allocation != NULL);
376
377   menu_bar = GTK_MENU_BAR (widget);
378   menu_shell = GTK_MENU_SHELL (widget);
379   priv = menu_bar->priv;
380
381   direction = gtk_widget_get_direction (widget);
382
383   gtk_widget_set_allocation (widget, allocation);
384
385   if (gtk_widget_get_realized (widget))
386     gdk_window_move_resize (gtk_widget_get_window (widget),
387                             allocation->x, allocation->y,
388                             allocation->width, allocation->height);
389
390   gtk_widget_style_get (widget, "internal-padding", &ipadding, NULL);
391   
392   if (menu_shell->children)
393     {
394       border_width = gtk_container_get_border_width (GTK_CONTAINER (menu_bar));
395       child_allocation.x = (border_width +
396                             ipadding + 
397                             BORDER_SPACING);
398       child_allocation.y = (border_width +
399                             BORDER_SPACING);
400       
401       if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
402         {
403           GtkStyle *style;
404
405           style = gtk_widget_get_style (widget);
406           child_allocation.x += style->xthickness;
407           child_allocation.y += style->ythickness;
408         }
409       
410       if (priv->pack_direction == GTK_PACK_DIRECTION_LTR ||
411           priv->pack_direction == GTK_PACK_DIRECTION_RTL)
412         {
413           child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
414           
415           offset = child_allocation.x;  /* Window edge to menubar start */
416           ltr_x = child_allocation.x;
417           
418           children = menu_shell->children;
419           while (children)
420             {
421               gint toggle_size;          
422               
423               child = children->data;
424               children = children->next;
425               
426               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
427                                                  &toggle_size);
428               gtk_widget_get_child_requisition (child, &child_requisition);
429             
430               if (priv->child_pack_direction == GTK_PACK_DIRECTION_LTR ||
431                   priv->child_pack_direction == GTK_PACK_DIRECTION_RTL)
432                 child_requisition.width += toggle_size;
433               else
434                 child_requisition.height += toggle_size;
435               
436               /* Support for the right justified help menu */
437               if ((children == NULL) && (GTK_IS_MENU_ITEM(child))
438                   && (GTK_MENU_ITEM(child)->right_justify)) 
439                 {
440                   ltr_x = allocation->width -
441                     child_requisition.width - offset;
442                 }
443               if (gtk_widget_get_visible (child))
444                 {
445                   if ((direction == GTK_TEXT_DIR_LTR) == (priv->pack_direction == GTK_PACK_DIRECTION_LTR))
446                     child_allocation.x = ltr_x;
447                   else
448                     child_allocation.x = allocation->width -
449                       child_requisition.width - ltr_x; 
450                   
451                   child_allocation.width = child_requisition.width;
452                   
453                   gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
454                                                       toggle_size);
455                   gtk_widget_size_allocate (child, &child_allocation);
456                   
457                   ltr_x += child_allocation.width;
458                 }
459             }
460         }
461       else
462         {
463           child_allocation.width = MAX (1, (gint)allocation->width - child_allocation.x * 2);
464           
465           offset = child_allocation.y;  /* Window edge to menubar start */
466           ltr_y = child_allocation.y;
467           
468           children = menu_shell->children;
469           while (children)
470             {
471               gint toggle_size;          
472               
473               child = children->data;
474               children = children->next;
475               
476               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
477                                                  &toggle_size);
478               gtk_widget_get_child_requisition (child, &child_requisition);
479               
480               if (priv->child_pack_direction == GTK_PACK_DIRECTION_LTR ||
481                   priv->child_pack_direction == GTK_PACK_DIRECTION_RTL)
482                 child_requisition.width += toggle_size;
483               else
484                 child_requisition.height += toggle_size;
485               
486               /* Support for the right justified help menu */
487               if ((children == NULL) && (GTK_IS_MENU_ITEM(child))
488                   && (GTK_MENU_ITEM(child)->right_justify)) 
489                 {
490                   ltr_y = allocation->height -
491                     child_requisition.height - offset;
492                 }
493               if (gtk_widget_get_visible (child))
494                 {
495                   if ((direction == GTK_TEXT_DIR_LTR) ==
496                       (priv->pack_direction == GTK_PACK_DIRECTION_TTB))
497                     child_allocation.y = ltr_y;
498                   else
499                     child_allocation.y = allocation->height -
500                       child_requisition.height - ltr_y; 
501                   child_allocation.height = child_requisition.height;
502                   
503                   gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
504                                                       toggle_size);
505                   gtk_widget_size_allocate (child, &child_allocation);
506                   
507                   ltr_y += child_allocation.height;
508                 }
509             }
510         }
511     }
512 }
513
514 static void
515 gtk_menu_bar_paint (GtkWidget    *widget,
516                     GdkRectangle *area)
517 {
518   g_return_if_fail (GTK_IS_MENU_BAR (widget));
519
520   if (gtk_widget_is_drawable (widget))
521     {
522       GtkAllocation allocation;
523       guint border;
524
525       border = gtk_container_get_border_width (GTK_CONTAINER (widget));
526       gtk_widget_get_allocation (widget, &allocation);
527
528       gtk_paint_box (gtk_widget_get_style (widget),
529                      gtk_widget_get_window (widget),
530                      gtk_widget_get_state (widget),
531                      get_shadow_type (GTK_MENU_BAR (widget)),
532                      area, widget, "menubar",
533                      border, border,
534                      allocation.width - border * 2,
535                      allocation.height - border * 2);
536     }
537 }
538
539 static gint
540 gtk_menu_bar_expose (GtkWidget      *widget,
541                      GdkEventExpose *event)
542 {
543   g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
544   g_return_val_if_fail (event != NULL, FALSE);
545
546   if (gtk_widget_is_drawable (widget))
547     {
548       gtk_menu_bar_paint (widget, &event->area);
549
550       GTK_WIDGET_CLASS (gtk_menu_bar_parent_class)->expose_event (widget, event);
551     }
552
553   return FALSE;
554 }
555
556 static GList *
557 get_menu_bars (GtkWindow *window)
558 {
559   return g_object_get_data (G_OBJECT (window), "gtk-menu-bar-list");
560 }
561
562 static GList *
563 get_viewable_menu_bars (GtkWindow *window)
564 {
565   GList *menu_bars;
566   GList *viewable_menu_bars = NULL;
567
568   for (menu_bars = get_menu_bars (window);
569        menu_bars;
570        menu_bars = menu_bars->next)
571     {
572       GtkWidget *widget = menu_bars->data;
573       gboolean viewable = TRUE;
574       
575       while (widget)
576         {
577           if (!gtk_widget_get_mapped (widget))
578             viewable = FALSE;
579
580           widget = gtk_widget_get_parent (widget);
581         }
582
583       if (viewable)
584         viewable_menu_bars = g_list_prepend (viewable_menu_bars, menu_bars->data);
585     }
586
587   return g_list_reverse (viewable_menu_bars);
588 }
589
590 static void
591 set_menu_bars (GtkWindow *window,
592                GList     *menubars)
593 {
594   g_object_set_data (G_OBJECT (window), I_("gtk-menu-bar-list"), menubars);
595 }
596
597 static gboolean
598 window_key_press_handler (GtkWidget   *widget,
599                           GdkEventKey *event,
600                           gpointer     data)
601 {
602   gchar *accel = NULL;
603   gboolean retval = FALSE;
604   
605   g_object_get (gtk_widget_get_settings (widget),
606                 "gtk-menu-bar-accel", &accel,
607                 NULL);
608
609   if (accel && *accel)
610     {
611       guint keyval = 0;
612       GdkModifierType mods = 0;
613
614       gtk_accelerator_parse (accel, &keyval, &mods);
615
616       if (keyval == 0)
617         g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
618
619       /* FIXME this is wrong, needs to be in the global accel resolution
620        * thing, to properly consider i18n etc., but that probably requires
621        * AccelGroup changes etc.
622        */
623       if (event->keyval == keyval &&
624           ((event->state & gtk_accelerator_get_default_mod_mask ()) ==
625            (mods & gtk_accelerator_get_default_mod_mask ())))
626         {
627           GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (widget));
628           GList *menubars;
629
630           menubars = _gtk_container_focus_sort (GTK_CONTAINER (widget), tmp_menubars,
631                                                 GTK_DIR_TAB_FORWARD, NULL);
632           g_list_free (tmp_menubars);
633           
634           if (menubars)
635             {
636               GtkMenuShell *menu_shell = GTK_MENU_SHELL (menubars->data);
637
638               _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
639               _gtk_menu_shell_activate (menu_shell);
640               gtk_menu_shell_select_first (menu_shell, FALSE);
641               
642               g_list_free (menubars);
643               
644               retval = TRUE;          
645             }
646         }
647     }
648
649   g_free (accel);
650
651   return retval;
652 }
653
654 static void
655 add_to_window (GtkWindow  *window,
656                GtkMenuBar *menubar)
657 {
658   GList *menubars = get_menu_bars (window);
659
660   if (!menubars)
661     {
662       g_signal_connect (window,
663                         "key-press-event",
664                         G_CALLBACK (window_key_press_handler),
665                         NULL);
666     }
667
668   set_menu_bars (window, g_list_prepend (menubars, menubar));
669 }
670
671 static void
672 remove_from_window (GtkWindow  *window,
673                     GtkMenuBar *menubar)
674 {
675   GList *menubars = get_menu_bars (window);
676
677   menubars = g_list_remove (menubars, menubar);
678
679   if (!menubars)
680     {
681       g_signal_handlers_disconnect_by_func (window,
682                                             window_key_press_handler,
683                                             NULL);
684     }
685
686   set_menu_bars (window, menubars);
687 }
688
689 static void
690 gtk_menu_bar_hierarchy_changed (GtkWidget *widget,
691                                 GtkWidget *old_toplevel)
692 {
693   GtkWidget *toplevel;  
694   GtkMenuBar *menubar;
695
696   menubar = GTK_MENU_BAR (widget);
697
698   toplevel = gtk_widget_get_toplevel (widget);
699
700   if (old_toplevel)
701     remove_from_window (GTK_WINDOW (old_toplevel), menubar);
702   
703   if (gtk_widget_is_toplevel (toplevel))
704     add_to_window (GTK_WINDOW (toplevel), menubar);
705 }
706
707 /**
708  * _gtk_menu_bar_cycle_focus:
709  * @menubar: a #GtkMenuBar
710  * @dir: direction in which to cycle the focus
711  * 
712  * Move the focus between menubars in the toplevel.
713  **/
714 void
715 _gtk_menu_bar_cycle_focus (GtkMenuBar       *menubar,
716                            GtkDirectionType  dir)
717 {
718   GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menubar));
719   GtkMenuItem *to_activate = NULL;
720
721   if (gtk_widget_is_toplevel (toplevel))
722     {
723       GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (toplevel));
724       GList *menubars;
725       GList *current;
726
727       menubars = _gtk_container_focus_sort (GTK_CONTAINER (toplevel), tmp_menubars,
728                                             dir, GTK_WIDGET (menubar));
729       g_list_free (tmp_menubars);
730
731       if (menubars)
732         {
733           current = g_list_find (menubars, menubar);
734
735           if (current && current->next)
736             {
737               GtkMenuShell *new_menushell = GTK_MENU_SHELL (current->next->data);
738               if (new_menushell->children)
739                 to_activate = new_menushell->children->data;
740             }
741         }
742           
743       g_list_free (menubars);
744     }
745
746   gtk_menu_shell_cancel (GTK_MENU_SHELL (menubar));
747
748   if (to_activate)
749     g_signal_emit_by_name (to_activate, "activate_item");
750 }
751
752 static GtkShadowType
753 get_shadow_type (GtkMenuBar *menubar)
754 {
755   GtkShadowType shadow_type = GTK_SHADOW_OUT;
756   
757   gtk_widget_style_get (GTK_WIDGET (menubar),
758                         "shadow-type", &shadow_type,
759                         NULL);
760
761   return shadow_type;
762 }
763
764 static gint
765 gtk_menu_bar_get_popup_delay (GtkMenuShell *menu_shell)
766 {
767   gint popup_delay;
768   
769   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
770                 "gtk-menu-bar-popup-delay", &popup_delay,
771                 NULL);
772
773   return popup_delay;
774 }
775
776 static void
777 gtk_menu_bar_move_current (GtkMenuShell         *menu_shell,
778                            GtkMenuDirectionType  direction)
779 {
780   GtkMenuBar *menubar = GTK_MENU_BAR (menu_shell);
781   GtkTextDirection text_dir;
782   GtkPackDirection pack_dir;
783
784   text_dir = gtk_widget_get_direction (GTK_WIDGET (menubar));
785   pack_dir = gtk_menu_bar_get_pack_direction (menubar);
786   
787   if (pack_dir == GTK_PACK_DIRECTION_LTR || pack_dir == GTK_PACK_DIRECTION_RTL)
788      {
789       if ((text_dir == GTK_TEXT_DIR_RTL) == (pack_dir == GTK_PACK_DIRECTION_LTR))
790         {
791           switch (direction) 
792             {      
793             case GTK_MENU_DIR_PREV:
794               direction = GTK_MENU_DIR_NEXT;
795               break;
796             case GTK_MENU_DIR_NEXT:
797               direction = GTK_MENU_DIR_PREV;
798               break;
799             default: ;
800             }
801         }
802     }
803   else
804     {
805       switch (direction) 
806         {
807         case GTK_MENU_DIR_PARENT:
808           if ((text_dir == GTK_TEXT_DIR_LTR) == (pack_dir == GTK_PACK_DIRECTION_TTB))
809             direction = GTK_MENU_DIR_PREV;
810           else
811             direction = GTK_MENU_DIR_NEXT;
812           break;
813         case GTK_MENU_DIR_CHILD:
814           if ((text_dir == GTK_TEXT_DIR_LTR) == (pack_dir == GTK_PACK_DIRECTION_TTB))
815             direction = GTK_MENU_DIR_NEXT;
816           else
817             direction = GTK_MENU_DIR_PREV;
818           break;
819         case GTK_MENU_DIR_PREV:
820           if (text_dir == GTK_TEXT_DIR_RTL)       
821             direction = GTK_MENU_DIR_CHILD;
822           else
823             direction = GTK_MENU_DIR_PARENT;
824           break;
825         case GTK_MENU_DIR_NEXT:
826           if (text_dir == GTK_TEXT_DIR_RTL)       
827             direction = GTK_MENU_DIR_PARENT;
828           else
829             direction = GTK_MENU_DIR_CHILD;
830           break;
831         default: ;
832         }
833     }
834   
835   GTK_MENU_SHELL_CLASS (gtk_menu_bar_parent_class)->move_current (menu_shell, direction);
836 }
837
838 /**
839  * gtk_menu_bar_get_pack_direction:
840  * @menubar: a #GtkMenuBar
841  * 
842  * Retrieves the current pack direction of the menubar. 
843  * See gtk_menu_bar_set_pack_direction().
844  *
845  * Return value: the pack direction
846  *
847  * Since: 2.8
848  */
849 GtkPackDirection
850 gtk_menu_bar_get_pack_direction (GtkMenuBar *menubar)
851 {
852   g_return_val_if_fail (GTK_IS_MENU_BAR (menubar), 
853                         GTK_PACK_DIRECTION_LTR);
854
855   return menubar->priv->pack_direction;
856 }
857
858 /**
859  * gtk_menu_bar_set_pack_direction:
860  * @menubar: a #GtkMenuBar
861  * @pack_dir: a new #GtkPackDirection
862  * 
863  * Sets how items should be packed inside a menubar.
864  * 
865  * Since: 2.8
866  */
867 void
868 gtk_menu_bar_set_pack_direction (GtkMenuBar       *menubar,
869                                  GtkPackDirection  pack_dir)
870 {
871   GtkMenuBarPrivate *priv;
872   GList *l;
873
874   g_return_if_fail (GTK_IS_MENU_BAR (menubar));
875
876   priv = menubar->priv;
877
878   if (priv->pack_direction != pack_dir)
879     {
880       priv->pack_direction = pack_dir;
881
882       gtk_widget_queue_resize (GTK_WIDGET (menubar));
883
884       for (l = GTK_MENU_SHELL (menubar)->children; l; l = l->next)
885         gtk_widget_queue_resize (GTK_WIDGET (l->data));
886
887       g_object_notify (G_OBJECT (menubar), "pack-direction");
888     }
889 }
890
891 /**
892  * gtk_menu_bar_get_child_pack_direction:
893  * @menubar: a #GtkMenuBar
894  * 
895  * Retrieves the current child pack direction of the menubar.
896  * See gtk_menu_bar_set_child_pack_direction().
897  *
898  * Return value: the child pack direction
899  *
900  * Since: 2.8
901  */
902 GtkPackDirection
903 gtk_menu_bar_get_child_pack_direction (GtkMenuBar *menubar)
904 {
905   g_return_val_if_fail (GTK_IS_MENU_BAR (menubar), 
906                         GTK_PACK_DIRECTION_LTR);
907
908   return menubar->priv->child_pack_direction;
909 }
910
911 /**
912  * gtk_menu_bar_set_child_pack_direction:
913  * @menubar: a #GtkMenuBar
914  * @child_pack_dir: a new #GtkPackDirection
915  * 
916  * Sets how widgets should be packed inside the children of a menubar.
917  * 
918  * Since: 2.8
919  */
920 void
921 gtk_menu_bar_set_child_pack_direction (GtkMenuBar       *menubar,
922                                        GtkPackDirection  child_pack_dir)
923 {
924   GtkMenuBarPrivate *priv;
925   GList *l;
926
927   g_return_if_fail (GTK_IS_MENU_BAR (menubar));
928
929   priv = menubar->priv;
930
931   if (priv->child_pack_direction != child_pack_dir)
932     {
933       priv->child_pack_direction = child_pack_dir;
934
935       gtk_widget_queue_resize (GTK_WIDGET (menubar));
936
937       for (l = GTK_MENU_SHELL (menubar)->children; l; l = l->next)
938         gtk_widget_queue_resize (GTK_WIDGET (l->data));
939
940       g_object_notify (G_OBJECT (menubar), "child-pack-direction");
941     }
942 }