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