]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenubar.c
Add hidden aliases for exported symbols which are used internally in order
[~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 "gtkalias.h"
32 #include "gtkbindings.h"
33 #include "gtkmain.h"
34 #include "gtkmarshalers.h"
35 #include "gtkmenubar.h"
36 #include "gtkmenuitem.h"
37 #include "gtksettings.h"
38 #include "gtkintl.h"
39 #include "gtkwindow.h"
40
41
42 #define BORDER_SPACING  0
43 #define DEFAULT_IPADDING 1
44
45 static void gtk_menu_bar_class_init        (GtkMenuBarClass *klass);
46 static void gtk_menu_bar_size_request      (GtkWidget       *widget,
47                                             GtkRequisition  *requisition);
48 static void gtk_menu_bar_size_allocate     (GtkWidget       *widget,
49                                             GtkAllocation   *allocation);
50 static void gtk_menu_bar_paint             (GtkWidget       *widget,
51                                             GdkRectangle    *area);
52 static gint gtk_menu_bar_expose            (GtkWidget       *widget,
53                                             GdkEventExpose  *event);
54 static void gtk_menu_bar_hierarchy_changed (GtkWidget       *widget,
55                                             GtkWidget       *old_toplevel);
56 static gint gtk_menu_bar_get_popup_delay  (GtkMenuShell    *menu_shell);
57                                             
58
59 static GtkShadowType get_shadow_type   (GtkMenuBar      *menubar);
60
61 static GtkMenuShellClass *parent_class = NULL;
62
63 GType
64 gtk_menu_bar_get_type (void)
65 {
66   static GType menu_bar_type = 0;
67
68   if (!menu_bar_type)
69     {
70       static const GTypeInfo menu_bar_info =
71       {
72         sizeof (GtkMenuBarClass),
73         NULL,           /* base_init */
74         NULL,           /* base_finalize */
75         (GClassInitFunc) gtk_menu_bar_class_init,
76         NULL,           /* class_finalize */
77         NULL,           /* class_data */
78         sizeof (GtkMenuBar),
79         0,              /* n_preallocs */
80         NULL,           /* instance_init */
81       };
82
83       menu_bar_type = g_type_register_static (GTK_TYPE_MENU_SHELL, "GtkMenuBar",
84                                               &menu_bar_info, 0);
85     }
86
87   return menu_bar_type;
88 }
89
90 static void
91 gtk_menu_bar_class_init (GtkMenuBarClass *class)
92 {
93   GtkObjectClass *object_class;
94   GtkWidgetClass *widget_class;
95   GtkMenuShellClass *menu_shell_class;
96
97   GtkBindingSet *binding_set;
98
99   parent_class = g_type_class_peek_parent (class);
100   
101   object_class = (GtkObjectClass*) class;
102   widget_class = (GtkWidgetClass*) class;
103   menu_shell_class = (GtkMenuShellClass*) class;
104
105   widget_class->size_request = gtk_menu_bar_size_request;
106   widget_class->size_allocate = gtk_menu_bar_size_allocate;
107   widget_class->expose_event = gtk_menu_bar_expose;
108   widget_class->hierarchy_changed = gtk_menu_bar_hierarchy_changed;
109   
110   menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
111   menu_shell_class->get_popup_delay = gtk_menu_bar_get_popup_delay;
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   gtk_widget_class_install_style_property (widget_class,
156                                            g_param_spec_enum ("shadow_type",
157                                                               P_("Shadow type"),
158                                                               P_("Style of bevel around the menubar"),
159                                                               GTK_TYPE_SHADOW_TYPE,
160                                                               GTK_SHADOW_OUT,
161                                                               G_PARAM_READABLE));
162
163   gtk_widget_class_install_style_property (widget_class,
164                                            g_param_spec_int ("internal_padding",
165                                                              P_("Internal padding"),
166                                                              P_("Amount of border space between the menubar shadow and the menu items"),
167                                                              0,
168                                                              G_MAXINT,
169                                                              DEFAULT_IPADDING,
170                                                              G_PARAM_READABLE));
171
172   gtk_settings_install_property (g_param_spec_int ("gtk-menu-bar-popup-delay",
173                                                    P_("Delay before drop down menus appear"),
174                                                    P_("Delay before the submenus of a menu bar appear"),
175                                                    0,
176                                                    G_MAXINT,
177                                                    0,
178                                                    G_PARAM_READWRITE));
179 }
180  
181 GtkWidget*
182 gtk_menu_bar_new (void)
183 {
184   return g_object_new (GTK_TYPE_MENU_BAR, NULL);
185 }
186
187 static void
188 gtk_menu_bar_size_request (GtkWidget      *widget,
189                            GtkRequisition *requisition)
190 {
191   GtkMenuBar *menu_bar;
192   GtkMenuShell *menu_shell;
193   GtkWidget *child;
194   GList *children;
195   gint nchildren;
196   GtkRequisition child_requisition;
197   gint ipadding;
198
199   g_return_if_fail (GTK_IS_MENU_BAR (widget));
200   g_return_if_fail (requisition != NULL);
201
202   requisition->width = 0;
203   requisition->height = 0;
204   
205   if (GTK_WIDGET_VISIBLE (widget))
206     {
207       menu_bar = GTK_MENU_BAR (widget);
208       menu_shell = GTK_MENU_SHELL (widget);
209
210       nchildren = 0;
211       children = menu_shell->children;
212
213       while (children)
214         {
215           child = children->data;
216           children = children->next;
217
218           if (GTK_WIDGET_VISIBLE (child))
219             {
220               gint toggle_size;
221               
222               GTK_MENU_ITEM (child)->show_submenu_indicator = FALSE;
223               gtk_widget_size_request (child, &child_requisition);
224               gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
225                                                  &toggle_size);
226               
227               requisition->width += child_requisition.width;
228               requisition->width += toggle_size;
229               
230               requisition->height = MAX (requisition->height, child_requisition.height);
231               nchildren += 1;
232             }
233         }
234
235       gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
236       
237       requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
238                              ipadding + 
239                              BORDER_SPACING) * 2;
240       requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
241                               ipadding +
242                               BORDER_SPACING) * 2;
243
244       if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
245         {
246           requisition->width += widget->style->xthickness * 2;
247           requisition->height += widget->style->ythickness * 2;
248         }
249     }
250 }
251
252 static void
253 gtk_menu_bar_size_allocate (GtkWidget     *widget,
254                             GtkAllocation *allocation)
255 {
256   GtkMenuBar *menu_bar;
257   GtkMenuShell *menu_shell;
258   GtkWidget *child;
259   GList *children;
260   GtkAllocation child_allocation;
261   GtkRequisition child_requisition;
262   guint offset;
263   GtkTextDirection direction;
264   gint ltr_x;
265   gint ipadding;
266
267   g_return_if_fail (GTK_IS_MENU_BAR (widget));
268   g_return_if_fail (allocation != NULL);
269
270   menu_bar = GTK_MENU_BAR (widget);
271   menu_shell = GTK_MENU_SHELL (widget);
272
273   direction = gtk_widget_get_direction (widget);
274
275   widget->allocation = *allocation;
276   if (GTK_WIDGET_REALIZED (widget))
277     gdk_window_move_resize (widget->window,
278                             allocation->x, allocation->y,
279                             allocation->width, allocation->height);
280
281   gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
282   
283   if (menu_shell->children)
284     {
285       child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
286                             ipadding + 
287                             BORDER_SPACING);
288       child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
289                             BORDER_SPACING);
290
291       if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
292         {
293           child_allocation.x += widget->style->xthickness;
294           child_allocation.y += widget->style->ythickness;
295         }
296       
297       child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
298
299       offset = child_allocation.x;      /* Window edge to menubar start */
300       ltr_x = child_allocation.x;
301
302       children = menu_shell->children;
303       while (children)
304         {
305           gint toggle_size;          
306
307           child = children->data;
308           children = children->next;
309
310           gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
311                                              &toggle_size);
312           gtk_widget_get_child_requisition (child, &child_requisition);
313
314           child_requisition.width += toggle_size;
315           
316           /* Support for the right justified help menu */
317           if ((children == NULL) && (GTK_IS_MENU_ITEM(child))
318               && (GTK_MENU_ITEM(child)->right_justify)) 
319             {
320               ltr_x = allocation->width -
321                 child_requisition.width - offset;
322             }
323           if (GTK_WIDGET_VISIBLE (child))
324             {
325               if (direction == GTK_TEXT_DIR_LTR) 
326                 child_allocation.x = ltr_x;
327               else
328                 child_allocation.x = allocation->width -
329                   child_requisition.width - ltr_x; 
330
331               child_allocation.width = child_requisition.width;
332
333               gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
334                                                   toggle_size);
335               gtk_widget_size_allocate (child, &child_allocation);
336
337               ltr_x += child_allocation.width;
338             }
339         }
340     }
341 }
342
343 static void
344 gtk_menu_bar_paint (GtkWidget *widget, GdkRectangle *area)
345 {
346   g_return_if_fail (GTK_IS_MENU_BAR (widget));
347
348   if (GTK_WIDGET_DRAWABLE (widget))
349     {
350       gint border;
351
352       border = GTK_CONTAINER (widget)->border_width;
353       
354       gtk_paint_box (widget->style,
355                      widget->window,
356                      GTK_WIDGET_STATE (widget),
357                      get_shadow_type (GTK_MENU_BAR (widget)),
358                      area, widget, "menubar",
359                      border, border,
360                      widget->allocation.width - border * 2,
361                      widget->allocation.height - border * 2);
362     }
363 }
364
365 static gint
366 gtk_menu_bar_expose (GtkWidget      *widget,
367                      GdkEventExpose *event)
368 {
369   g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
370   g_return_val_if_fail (event != NULL, FALSE);
371
372   if (GTK_WIDGET_DRAWABLE (widget))
373     {
374       gtk_menu_bar_paint (widget, &event->area);
375
376       (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
377     }
378
379   return FALSE;
380 }
381
382 static GList *
383 get_menu_bars (GtkWindow *window)
384 {
385   return g_object_get_data (G_OBJECT (window), "gtk-menu-bar-list");
386 }
387
388 static GList *
389 get_viewable_menu_bars (GtkWindow *window)
390 {
391   GList *menu_bars;
392   GList *viewable_menu_bars = NULL;
393
394   for (menu_bars = get_menu_bars (window);
395        menu_bars;
396        menu_bars = menu_bars->next)
397     {
398       GtkWidget *widget = menu_bars->data;
399       gboolean viewable = TRUE;
400       
401       while (widget)
402         {
403           if (!GTK_WIDGET_MAPPED (widget))
404             viewable = FALSE;
405           
406           widget = widget->parent;
407         }
408
409       if (viewable)
410         viewable_menu_bars = g_list_prepend (viewable_menu_bars, menu_bars->data);
411     }
412
413   return g_list_reverse (viewable_menu_bars);
414 }
415
416 static void
417 set_menu_bars (GtkWindow *window,
418                GList     *menubars)
419 {
420   g_object_set_data (G_OBJECT (window), "gtk-menu-bar-list", menubars);
421 }
422
423 static gboolean
424 window_key_press_handler (GtkWidget   *widget,
425                           GdkEventKey *event,
426                           gpointer     data)
427 {
428   gchar *accel = NULL;
429   gboolean retval = FALSE;
430   
431   g_object_get (gtk_widget_get_settings (widget),
432                 "gtk-menu-bar-accel",
433                 &accel,
434                 NULL);
435
436   if (accel)
437     {
438       guint keyval = 0;
439       GdkModifierType mods = 0;
440
441       gtk_accelerator_parse (accel, &keyval, &mods);
442
443       if (keyval == 0)
444         g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
445
446       /* FIXME this is wrong, needs to be in the global accel resolution
447        * thing, to properly consider i18n etc., but that probably requires
448        * AccelGroup changes etc.
449        */
450       if (event->keyval == keyval &&
451           ((event->state & gtk_accelerator_get_default_mod_mask ()) ==
452            (mods & gtk_accelerator_get_default_mod_mask ())))
453         {
454           GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (widget));
455           GList *menubars;
456
457           menubars = _gtk_container_focus_sort (GTK_CONTAINER (widget), tmp_menubars,
458                                                 GTK_DIR_TAB_FORWARD, NULL);
459           g_list_free (tmp_menubars);
460           
461           if (menubars)
462             {
463               GtkMenuShell *menu_shell = GTK_MENU_SHELL (menubars->data);
464
465               _gtk_menu_shell_activate (menu_shell);
466               gtk_menu_shell_select_first (menu_shell, FALSE);
467               
468               g_list_free (menubars);
469               
470               retval = TRUE;          
471             }
472         }
473
474       g_free (accel);
475     }
476
477   return retval;
478 }
479
480 static void
481 add_to_window (GtkWindow  *window,
482                GtkMenuBar *menubar)
483 {
484   GList *menubars = get_menu_bars (window);
485
486   if (!menubars)
487     {
488       g_signal_connect (window,
489                         "key_press_event",
490                         G_CALLBACK (window_key_press_handler),
491                         NULL);
492     }
493
494   set_menu_bars (window, g_list_prepend (menubars, menubar));
495 }
496
497 static void
498 remove_from_window (GtkWindow  *window,
499                     GtkMenuBar *menubar)
500 {
501   GList *menubars = get_menu_bars (window);
502
503   menubars = g_object_get_data (G_OBJECT (window),
504                                     "gtk-menu-bar-list");
505
506   menubars = g_list_remove (menubars, menubar);
507
508   if (!menubars)
509     {
510       g_signal_handlers_disconnect_by_func (window,
511                                             window_key_press_handler,
512                                             NULL);
513     }
514
515   set_menu_bars (window, menubars);
516 }
517
518 static void
519 gtk_menu_bar_hierarchy_changed (GtkWidget *widget,
520                                 GtkWidget *old_toplevel)
521 {
522   GtkWidget *toplevel;  
523   GtkMenuBar *menubar;
524
525   menubar = GTK_MENU_BAR (widget);
526
527   toplevel = gtk_widget_get_toplevel (widget);
528
529   if (old_toplevel)
530     remove_from_window (GTK_WINDOW (old_toplevel), menubar);
531   
532   if (GTK_WIDGET_TOPLEVEL (toplevel))
533     add_to_window (GTK_WINDOW (toplevel), menubar);
534 }
535
536 /**
537  * _gtk_menu_bar_cycle_focus:
538  * @menubar: a #GtkMenuBar
539  * @dir: direction in which to cycle the focus
540  * 
541  * Move the focus between menubars in the toplevel.
542  **/
543 void
544 _gtk_menu_bar_cycle_focus (GtkMenuBar       *menubar,
545                            GtkDirectionType  dir)
546 {
547   GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menubar));
548   GtkMenuItem *to_activate = NULL;
549
550   if (GTK_WIDGET_TOPLEVEL (toplevel))
551     {
552       GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (toplevel));
553       GList *menubars;
554       GList *current;
555
556       menubars = _gtk_container_focus_sort (GTK_CONTAINER (toplevel), tmp_menubars,
557                                             dir, GTK_WIDGET (menubar));
558       g_list_free (tmp_menubars);
559
560       if (menubars)
561         {
562           current = g_list_find (menubars, menubar);
563
564           if (current && current->next)
565             {
566               GtkMenuShell *new_menushell = GTK_MENU_SHELL (current->next->data);
567               if (new_menushell->children)
568                 to_activate = new_menushell->children->data;
569             }
570         }
571           
572       g_list_free (menubars);
573     }
574
575   gtk_menu_shell_cancel (GTK_MENU_SHELL (menubar));
576
577   if (to_activate)
578     g_signal_emit_by_name (to_activate, "activate_item");
579 }
580
581 static GtkShadowType
582 get_shadow_type (GtkMenuBar *menubar)
583 {
584   GtkShadowType shadow_type = GTK_SHADOW_OUT;
585   
586   gtk_widget_style_get (GTK_WIDGET (menubar),
587                         "shadow_type", &shadow_type,
588                         NULL);
589
590   return shadow_type;
591 }
592
593 static gint
594 gtk_menu_bar_get_popup_delay (GtkMenuShell *menu_shell)
595 {
596   gint popup_delay;
597   
598   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
599                 "gtk-menu-bar-popup-delay", &popup_delay,
600                 NULL);
601
602   return popup_delay;
603 }