1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
27 #define GTK_MENU_INTERNALS
30 #include "gdk/gdkkeysyms.h"
32 #include "gtkbindings.h"
34 #include "gtkmarshalers.h"
35 #include "gtkmenubar.h"
36 #include "gtkmenuitem.h"
37 #include "gtksettings.h"
39 #include "gtkwindow.h"
42 #define BORDER_SPACING 0
43 #define DEFAULT_IPADDING 1
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,
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);
59 static GtkShadowType get_shadow_type (GtkMenuBar *menubar);
61 static GtkMenuShellClass *parent_class = NULL;
64 gtk_menu_bar_get_type (void)
66 static GType menu_bar_type = 0;
70 static const GTypeInfo menu_bar_info =
72 sizeof (GtkMenuBarClass),
74 NULL, /* base_finalize */
75 (GClassInitFunc) gtk_menu_bar_class_init,
76 NULL, /* class_finalize */
77 NULL, /* class_data */
80 NULL, /* instance_init */
83 menu_bar_type = g_type_register_static (GTK_TYPE_MENU_SHELL, "GtkMenuBar",
91 gtk_menu_bar_class_init (GtkMenuBarClass *class)
93 GtkObjectClass *object_class;
94 GtkWidgetClass *widget_class;
95 GtkMenuShellClass *menu_shell_class;
97 GtkBindingSet *binding_set;
99 parent_class = g_type_class_peek_parent (class);
101 object_class = (GtkObjectClass*) class;
102 widget_class = (GtkWidgetClass*) class;
103 menu_shell_class = (GtkMenuShellClass*) class;
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;
110 menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
111 menu_shell_class->get_popup_delay = gtk_menu_bar_get_popup_delay;
113 binding_set = gtk_binding_set_by_class (class);
114 gtk_binding_entry_add_signal (binding_set,
117 GTK_TYPE_MENU_DIRECTION_TYPE,
119 gtk_binding_entry_add_signal (binding_set,
122 GTK_TYPE_MENU_DIRECTION_TYPE,
124 gtk_binding_entry_add_signal (binding_set,
127 GTK_TYPE_MENU_DIRECTION_TYPE,
129 gtk_binding_entry_add_signal (binding_set,
132 GTK_TYPE_MENU_DIRECTION_TYPE,
134 gtk_binding_entry_add_signal (binding_set,
137 GTK_TYPE_MENU_DIRECTION_TYPE,
138 GTK_MENU_DIR_PARENT);
139 gtk_binding_entry_add_signal (binding_set,
142 GTK_TYPE_MENU_DIRECTION_TYPE,
143 GTK_MENU_DIR_PARENT);
144 gtk_binding_entry_add_signal (binding_set,
147 GTK_TYPE_MENU_DIRECTION_TYPE,
149 gtk_binding_entry_add_signal (binding_set,
152 GTK_TYPE_MENU_DIRECTION_TYPE,
155 gtk_widget_class_install_style_property (widget_class,
156 g_param_spec_enum ("shadow_type",
158 P_("Style of bevel around the menubar"),
159 GTK_TYPE_SHADOW_TYPE,
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"),
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"),
182 gtk_menu_bar_new (void)
184 return g_object_new (GTK_TYPE_MENU_BAR, NULL);
188 gtk_menu_bar_size_request (GtkWidget *widget,
189 GtkRequisition *requisition)
191 GtkMenuBar *menu_bar;
192 GtkMenuShell *menu_shell;
196 GtkRequisition child_requisition;
199 g_return_if_fail (GTK_IS_MENU_BAR (widget));
200 g_return_if_fail (requisition != NULL);
202 requisition->width = 0;
203 requisition->height = 0;
205 if (GTK_WIDGET_VISIBLE (widget))
207 menu_bar = GTK_MENU_BAR (widget);
208 menu_shell = GTK_MENU_SHELL (widget);
211 children = menu_shell->children;
215 child = children->data;
216 children = children->next;
218 if (GTK_WIDGET_VISIBLE (child))
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),
227 requisition->width += child_requisition.width;
228 requisition->width += toggle_size;
230 requisition->height = MAX (requisition->height, child_requisition.height);
235 gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
237 requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
240 requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
244 if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
246 requisition->width += widget->style->xthickness * 2;
247 requisition->height += widget->style->ythickness * 2;
253 gtk_menu_bar_size_allocate (GtkWidget *widget,
254 GtkAllocation *allocation)
256 GtkMenuBar *menu_bar;
257 GtkMenuShell *menu_shell;
260 GtkAllocation child_allocation;
261 GtkRequisition child_requisition;
263 GtkTextDirection direction;
267 g_return_if_fail (GTK_IS_MENU_BAR (widget));
268 g_return_if_fail (allocation != NULL);
270 menu_bar = GTK_MENU_BAR (widget);
271 menu_shell = GTK_MENU_SHELL (widget);
273 direction = gtk_widget_get_direction (widget);
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);
281 gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
283 if (menu_shell->children)
285 child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
288 child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
291 if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
293 child_allocation.x += widget->style->xthickness;
294 child_allocation.y += widget->style->ythickness;
297 child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
299 offset = child_allocation.x; /* Window edge to menubar start */
300 ltr_x = child_allocation.x;
302 children = menu_shell->children;
307 child = children->data;
308 children = children->next;
310 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
312 gtk_widget_get_child_requisition (child, &child_requisition);
314 child_requisition.width += toggle_size;
316 /* Support for the right justified help menu */
317 if ((children == NULL) && (GTK_IS_MENU_ITEM(child))
318 && (GTK_MENU_ITEM(child)->right_justify))
320 ltr_x = allocation->width -
321 child_requisition.width - offset;
323 if (GTK_WIDGET_VISIBLE (child))
325 if (direction == GTK_TEXT_DIR_LTR)
326 child_allocation.x = ltr_x;
328 child_allocation.x = allocation->width -
329 child_requisition.width - ltr_x;
331 child_allocation.width = child_requisition.width;
333 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
335 gtk_widget_size_allocate (child, &child_allocation);
337 ltr_x += child_allocation.width;
344 gtk_menu_bar_paint (GtkWidget *widget, GdkRectangle *area)
346 g_return_if_fail (GTK_IS_MENU_BAR (widget));
348 if (GTK_WIDGET_DRAWABLE (widget))
352 border = GTK_CONTAINER (widget)->border_width;
354 gtk_paint_box (widget->style,
356 GTK_WIDGET_STATE (widget),
357 get_shadow_type (GTK_MENU_BAR (widget)),
358 area, widget, "menubar",
360 widget->allocation.width - border * 2,
361 widget->allocation.height - border * 2);
366 gtk_menu_bar_expose (GtkWidget *widget,
367 GdkEventExpose *event)
369 g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
370 g_return_val_if_fail (event != NULL, FALSE);
372 if (GTK_WIDGET_DRAWABLE (widget))
374 gtk_menu_bar_paint (widget, &event->area);
376 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
383 get_menu_bars (GtkWindow *window)
385 return g_object_get_data (G_OBJECT (window), "gtk-menu-bar-list");
389 get_viewable_menu_bars (GtkWindow *window)
392 GList *viewable_menu_bars = NULL;
394 for (menu_bars = get_menu_bars (window);
396 menu_bars = menu_bars->next)
398 GtkWidget *widget = menu_bars->data;
399 gboolean viewable = TRUE;
403 if (!GTK_WIDGET_MAPPED (widget))
406 widget = widget->parent;
410 viewable_menu_bars = g_list_prepend (viewable_menu_bars, menu_bars->data);
413 return g_list_reverse (viewable_menu_bars);
417 set_menu_bars (GtkWindow *window,
420 g_object_set_data (G_OBJECT (window), "gtk-menu-bar-list", menubars);
424 window_key_press_handler (GtkWidget *widget,
429 gboolean retval = FALSE;
431 g_object_get (gtk_widget_get_settings (widget),
432 "gtk-menu-bar-accel",
439 GdkModifierType mods = 0;
441 gtk_accelerator_parse (accel, &keyval, &mods);
444 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
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.
450 if (event->keyval == keyval &&
451 ((event->state & gtk_accelerator_get_default_mod_mask ()) ==
452 (mods & gtk_accelerator_get_default_mod_mask ())))
454 GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (widget));
457 menubars = _gtk_container_focus_sort (GTK_CONTAINER (widget), tmp_menubars,
458 GTK_DIR_TAB_FORWARD, NULL);
459 g_list_free (tmp_menubars);
463 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menubars->data);
465 _gtk_menu_shell_activate (menu_shell);
466 gtk_menu_shell_select_first (menu_shell, FALSE);
468 g_list_free (menubars);
481 add_to_window (GtkWindow *window,
484 GList *menubars = get_menu_bars (window);
488 g_signal_connect (window,
490 G_CALLBACK (window_key_press_handler),
494 set_menu_bars (window, g_list_prepend (menubars, menubar));
498 remove_from_window (GtkWindow *window,
501 GList *menubars = get_menu_bars (window);
503 menubars = g_object_get_data (G_OBJECT (window),
504 "gtk-menu-bar-list");
506 menubars = g_list_remove (menubars, menubar);
510 g_signal_handlers_disconnect_by_func (window,
511 window_key_press_handler,
515 set_menu_bars (window, menubars);
519 gtk_menu_bar_hierarchy_changed (GtkWidget *widget,
520 GtkWidget *old_toplevel)
525 menubar = GTK_MENU_BAR (widget);
527 toplevel = gtk_widget_get_toplevel (widget);
530 remove_from_window (GTK_WINDOW (old_toplevel), menubar);
532 if (GTK_WIDGET_TOPLEVEL (toplevel))
533 add_to_window (GTK_WINDOW (toplevel), menubar);
537 * _gtk_menu_bar_cycle_focus:
538 * @menubar: a #GtkMenuBar
539 * @dir: direction in which to cycle the focus
541 * Move the focus between menubars in the toplevel.
544 _gtk_menu_bar_cycle_focus (GtkMenuBar *menubar,
545 GtkDirectionType dir)
547 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menubar));
548 GtkMenuItem *to_activate = NULL;
550 if (GTK_WIDGET_TOPLEVEL (toplevel))
552 GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (toplevel));
556 menubars = _gtk_container_focus_sort (GTK_CONTAINER (toplevel), tmp_menubars,
557 dir, GTK_WIDGET (menubar));
558 g_list_free (tmp_menubars);
562 current = g_list_find (menubars, menubar);
564 if (current && current->next)
566 GtkMenuShell *new_menushell = GTK_MENU_SHELL (current->next->data);
567 if (new_menushell->children)
568 to_activate = new_menushell->children->data;
572 g_list_free (menubars);
575 gtk_menu_shell_cancel (GTK_MENU_SHELL (menubar));
578 g_signal_emit_by_name (to_activate, "activate_item");
582 get_shadow_type (GtkMenuBar *menubar)
584 GtkShadowType shadow_type = GTK_SHADOW_OUT;
586 gtk_widget_style_get (GTK_WIDGET (menubar),
587 "shadow_type", &shadow_type,
594 gtk_menu_bar_get_popup_delay (GtkMenuShell *menu_shell)
598 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
599 "gtk-menu-bar-popup-delay", &popup_delay,