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