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
29 #include "gdk/gdkkeysyms.h"
30 #include "gtkbindings.h"
32 #include "gtkmarshalers.h"
33 #include "gtkmenubar.h"
34 #include "gtkmenuitem.h"
35 #include "gtksettings.h"
37 #include "gtkwindow.h"
40 #define BORDER_SPACING 0
41 #define DEFAULT_IPADDING 1
43 static void gtk_menu_bar_class_init (GtkMenuBarClass *klass);
44 static void gtk_menu_bar_size_request (GtkWidget *widget,
45 GtkRequisition *requisition);
46 static void gtk_menu_bar_size_allocate (GtkWidget *widget,
47 GtkAllocation *allocation);
48 static void gtk_menu_bar_paint (GtkWidget *widget,
50 static gint gtk_menu_bar_expose (GtkWidget *widget,
51 GdkEventExpose *event);
52 static void gtk_menu_bar_hierarchy_changed (GtkWidget *widget,
53 GtkWidget *old_toplevel);
54 static gint gtk_menu_bar_get_popup_delay (GtkMenuShell *menu_shell);
57 static GtkShadowType get_shadow_type (GtkMenuBar *menubar);
59 static GtkMenuShellClass *parent_class = NULL;
62 gtk_menu_bar_get_type (void)
64 static GType menu_bar_type = 0;
68 static const GTypeInfo menu_bar_info =
70 sizeof (GtkMenuBarClass),
72 NULL, /* base_finalize */
73 (GClassInitFunc) gtk_menu_bar_class_init,
74 NULL, /* class_finalize */
75 NULL, /* class_data */
78 NULL, /* instance_init */
81 menu_bar_type = g_type_register_static (GTK_TYPE_MENU_SHELL, "GtkMenuBar",
89 gtk_menu_bar_class_init (GtkMenuBarClass *class)
91 GtkObjectClass *object_class;
92 GtkWidgetClass *widget_class;
93 GtkMenuShellClass *menu_shell_class;
95 GtkBindingSet *binding_set;
97 parent_class = g_type_class_peek_parent (class);
99 object_class = (GtkObjectClass*) class;
100 widget_class = (GtkWidgetClass*) class;
101 menu_shell_class = (GtkMenuShellClass*) class;
103 widget_class->size_request = gtk_menu_bar_size_request;
104 widget_class->size_allocate = gtk_menu_bar_size_allocate;
105 widget_class->expose_event = gtk_menu_bar_expose;
106 widget_class->hierarchy_changed = gtk_menu_bar_hierarchy_changed;
108 menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
109 menu_shell_class->get_popup_delay = gtk_menu_bar_get_popup_delay;
111 binding_set = gtk_binding_set_by_class (class);
112 gtk_binding_entry_add_signal (binding_set,
115 GTK_TYPE_MENU_DIRECTION_TYPE,
117 gtk_binding_entry_add_signal (binding_set,
120 GTK_TYPE_MENU_DIRECTION_TYPE,
122 gtk_binding_entry_add_signal (binding_set,
125 GTK_TYPE_MENU_DIRECTION_TYPE,
127 gtk_binding_entry_add_signal (binding_set,
130 GTK_TYPE_MENU_DIRECTION_TYPE,
132 gtk_binding_entry_add_signal (binding_set,
135 GTK_TYPE_MENU_DIRECTION_TYPE,
136 GTK_MENU_DIR_PARENT);
137 gtk_binding_entry_add_signal (binding_set,
140 GTK_TYPE_MENU_DIRECTION_TYPE,
141 GTK_MENU_DIR_PARENT);
142 gtk_binding_entry_add_signal (binding_set,
145 GTK_TYPE_MENU_DIRECTION_TYPE,
147 gtk_binding_entry_add_signal (binding_set,
150 GTK_TYPE_MENU_DIRECTION_TYPE,
153 gtk_widget_class_install_style_property (widget_class,
154 g_param_spec_enum ("shadow_type",
156 P_("Style of bevel around the menubar"),
157 GTK_TYPE_SHADOW_TYPE,
161 gtk_widget_class_install_style_property (widget_class,
162 g_param_spec_int ("internal_padding",
163 P_("Internal padding"),
164 P_("Amount of border space between the menubar shadow and the menu items"),
170 gtk_settings_install_property (g_param_spec_int ("gtk-menu-bar-popup-delay",
171 P_("Delay before drop down menus appear"),
172 P_("Delay before the submenus of a menu bar appear"),
180 gtk_menu_bar_new (void)
182 return g_object_new (GTK_TYPE_MENU_BAR, NULL);
186 gtk_menu_bar_size_request (GtkWidget *widget,
187 GtkRequisition *requisition)
189 GtkMenuBar *menu_bar;
190 GtkMenuShell *menu_shell;
194 GtkRequisition child_requisition;
197 g_return_if_fail (GTK_IS_MENU_BAR (widget));
198 g_return_if_fail (requisition != NULL);
200 requisition->width = 0;
201 requisition->height = 0;
203 if (GTK_WIDGET_VISIBLE (widget))
205 menu_bar = GTK_MENU_BAR (widget);
206 menu_shell = GTK_MENU_SHELL (widget);
209 children = menu_shell->children;
213 child = children->data;
214 children = children->next;
216 if (GTK_WIDGET_VISIBLE (child))
220 GTK_MENU_ITEM (child)->show_submenu_indicator = FALSE;
221 gtk_widget_size_request (child, &child_requisition);
222 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
225 requisition->width += child_requisition.width;
226 requisition->width += toggle_size;
228 requisition->height = MAX (requisition->height, child_requisition.height);
233 gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
235 requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
238 requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
242 if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
244 requisition->width += widget->style->xthickness * 2;
245 requisition->height += widget->style->ythickness * 2;
251 gtk_menu_bar_size_allocate (GtkWidget *widget,
252 GtkAllocation *allocation)
254 GtkMenuBar *menu_bar;
255 GtkMenuShell *menu_shell;
258 GtkAllocation child_allocation;
259 GtkRequisition child_requisition;
261 GtkTextDirection direction;
265 g_return_if_fail (GTK_IS_MENU_BAR (widget));
266 g_return_if_fail (allocation != NULL);
268 menu_bar = GTK_MENU_BAR (widget);
269 menu_shell = GTK_MENU_SHELL (widget);
271 direction = gtk_widget_get_direction (widget);
273 widget->allocation = *allocation;
274 if (GTK_WIDGET_REALIZED (widget))
275 gdk_window_move_resize (widget->window,
276 allocation->x, allocation->y,
277 allocation->width, allocation->height);
279 gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
281 if (menu_shell->children)
283 child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
286 child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
289 if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
291 child_allocation.x += widget->style->xthickness;
292 child_allocation.y += widget->style->ythickness;
295 child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
297 offset = child_allocation.x; /* Window edge to menubar start */
298 ltr_x = child_allocation.x;
300 children = menu_shell->children;
305 child = children->data;
306 children = children->next;
308 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
310 gtk_widget_get_child_requisition (child, &child_requisition);
312 child_requisition.width += toggle_size;
314 /* Support for the right justified help menu */
315 if ((children == NULL) && (GTK_IS_MENU_ITEM(child))
316 && (GTK_MENU_ITEM(child)->right_justify))
318 ltr_x = allocation->width -
319 child_requisition.width - offset;
321 if (GTK_WIDGET_VISIBLE (child))
323 if (direction == GTK_TEXT_DIR_LTR)
324 child_allocation.x = ltr_x;
326 child_allocation.x = allocation->width -
327 child_requisition.width - ltr_x;
329 child_allocation.width = child_requisition.width;
331 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
333 gtk_widget_size_allocate (child, &child_allocation);
335 ltr_x += child_allocation.width;
342 gtk_menu_bar_paint (GtkWidget *widget, GdkRectangle *area)
344 g_return_if_fail (GTK_IS_MENU_BAR (widget));
346 if (GTK_WIDGET_DRAWABLE (widget))
350 border = GTK_CONTAINER (widget)->border_width;
352 gtk_paint_box (widget->style,
354 GTK_WIDGET_STATE (widget),
355 get_shadow_type (GTK_MENU_BAR (widget)),
356 area, widget, "menubar",
358 widget->allocation.width - border * 2,
359 widget->allocation.height - border * 2);
364 gtk_menu_bar_expose (GtkWidget *widget,
365 GdkEventExpose *event)
367 g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
368 g_return_val_if_fail (event != NULL, FALSE);
370 if (GTK_WIDGET_DRAWABLE (widget))
372 gtk_menu_bar_paint (widget, &event->area);
374 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
381 get_menu_bars (GtkWindow *window)
383 return g_object_get_data (G_OBJECT (window), "gtk-menu-bar-list");
387 get_viewable_menu_bars (GtkWindow *window)
390 GList *viewable_menu_bars = NULL;
392 for (menu_bars = get_menu_bars (window);
394 menu_bars = menu_bars->next)
396 GtkWidget *widget = menu_bars->data;
397 gboolean viewable = TRUE;
401 if (!GTK_WIDGET_MAPPED (widget))
404 widget = widget->parent;
408 viewable_menu_bars = g_list_prepend (viewable_menu_bars, menu_bars->data);
411 return g_list_reverse (viewable_menu_bars);
415 set_menu_bars (GtkWindow *window,
418 g_object_set_data (G_OBJECT (window), "gtk-menu-bar-list", menubars);
422 window_key_press_handler (GtkWidget *widget,
427 gboolean retval = FALSE;
429 g_object_get (gtk_widget_get_settings (widget),
430 "gtk-menu-bar-accel",
437 GdkModifierType mods = 0;
439 gtk_accelerator_parse (accel, &keyval, &mods);
442 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
444 /* FIXME this is wrong, needs to be in the global accel resolution
445 * thing, to properly consider i18n etc., but that probably requires
446 * AccelGroup changes etc.
448 if (event->keyval == keyval &&
449 ((event->state & gtk_accelerator_get_default_mod_mask ()) ==
450 (mods & gtk_accelerator_get_default_mod_mask ())))
452 GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (widget));
455 menubars = _gtk_container_focus_sort (GTK_CONTAINER (widget), tmp_menubars,
456 GTK_DIR_TAB_FORWARD, NULL);
457 g_list_free (tmp_menubars);
461 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menubars->data);
463 _gtk_menu_shell_activate (menu_shell);
464 gtk_menu_shell_select_first (menu_shell, FALSE);
466 g_list_free (menubars);
479 add_to_window (GtkWindow *window,
482 GList *menubars = get_menu_bars (window);
486 g_signal_connect (window,
488 G_CALLBACK (window_key_press_handler),
492 set_menu_bars (window, g_list_prepend (menubars, menubar));
496 remove_from_window (GtkWindow *window,
499 GList *menubars = get_menu_bars (window);
501 menubars = g_object_get_data (G_OBJECT (window),
502 "gtk-menu-bar-list");
504 menubars = g_list_remove (menubars, menubar);
508 g_signal_handlers_disconnect_by_func (window,
509 window_key_press_handler,
513 set_menu_bars (window, menubars);
517 gtk_menu_bar_hierarchy_changed (GtkWidget *widget,
518 GtkWidget *old_toplevel)
523 menubar = GTK_MENU_BAR (widget);
525 toplevel = gtk_widget_get_toplevel (widget);
528 remove_from_window (GTK_WINDOW (old_toplevel), menubar);
530 if (GTK_WIDGET_TOPLEVEL (toplevel))
531 add_to_window (GTK_WINDOW (toplevel), menubar);
535 * _gtk_menu_bar_cycle_focus:
536 * @menubar: a #GtkMenuBar
537 * @dir: direction in which to cycle the focus
539 * Move the focus between menubars in the toplevel.
542 _gtk_menu_bar_cycle_focus (GtkMenuBar *menubar,
543 GtkDirectionType dir)
545 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menubar));
546 GtkMenuItem *to_activate = NULL;
548 if (GTK_WIDGET_TOPLEVEL (toplevel))
550 GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (toplevel));
554 menubars = _gtk_container_focus_sort (GTK_CONTAINER (toplevel), tmp_menubars,
555 dir, GTK_WIDGET (menubar));
556 g_list_free (tmp_menubars);
560 current = g_list_find (menubars, menubar);
562 if (current && current->next)
564 GtkMenuShell *new_menushell = GTK_MENU_SHELL (current->next->data);
565 if (new_menushell->children)
566 to_activate = new_menushell->children->data;
570 g_list_free (menubars);
573 gtk_menu_shell_cancel (GTK_MENU_SHELL (menubar));
576 g_signal_emit_by_name (to_activate, "activate_item");
580 get_shadow_type (GtkMenuBar *menubar)
582 GtkShadowType shadow_type = GTK_SHADOW_OUT;
584 gtk_widget_style_get (GTK_WIDGET (menubar),
585 "shadow_type", &shadow_type,
592 gtk_menu_bar_get_popup_delay (GtkMenuShell *menu_shell)
596 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
597 "gtk-menu-bar-popup-delay", &popup_delay,