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"
31 #include "gtkbindings.h"
33 #include "gtkmarshalers.h"
34 #include "gtkmenubar.h"
35 #include "gtkmenuitem.h"
36 #include "gtksettings.h"
38 #include "gtkwindow.h"
41 #define BORDER_SPACING 0
42 #define DEFAULT_IPADDING 1
44 static void gtk_menu_bar_class_init (GtkMenuBarClass *klass);
45 static void gtk_menu_bar_size_request (GtkWidget *widget,
46 GtkRequisition *requisition);
47 static void gtk_menu_bar_size_allocate (GtkWidget *widget,
48 GtkAllocation *allocation);
49 static void gtk_menu_bar_paint (GtkWidget *widget,
51 static gint gtk_menu_bar_expose (GtkWidget *widget,
52 GdkEventExpose *event);
53 static void gtk_menu_bar_hierarchy_changed (GtkWidget *widget,
54 GtkWidget *old_toplevel);
55 static gint gtk_menu_bar_get_popup_delay (GtkMenuShell *menu_shell);
58 static GtkShadowType get_shadow_type (GtkMenuBar *menubar);
60 static GtkMenuShellClass *parent_class = NULL;
63 gtk_menu_bar_get_type (void)
65 static GType menu_bar_type = 0;
69 static const GTypeInfo menu_bar_info =
71 sizeof (GtkMenuBarClass),
73 NULL, /* base_finalize */
74 (GClassInitFunc) gtk_menu_bar_class_init,
75 NULL, /* class_finalize */
76 NULL, /* class_data */
79 NULL, /* instance_init */
82 menu_bar_type = g_type_register_static (GTK_TYPE_MENU_SHELL, "GtkMenuBar",
90 gtk_menu_bar_class_init (GtkMenuBarClass *class)
92 GtkObjectClass *object_class;
93 GtkWidgetClass *widget_class;
94 GtkMenuShellClass *menu_shell_class;
96 GtkBindingSet *binding_set;
98 parent_class = g_type_class_peek_parent (class);
100 object_class = (GtkObjectClass*) class;
101 widget_class = (GtkWidgetClass*) class;
102 menu_shell_class = (GtkMenuShellClass*) class;
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;
109 menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
110 menu_shell_class->get_popup_delay = gtk_menu_bar_get_popup_delay;
112 binding_set = gtk_binding_set_by_class (class);
113 gtk_binding_entry_add_signal (binding_set,
116 GTK_TYPE_MENU_DIRECTION_TYPE,
118 gtk_binding_entry_add_signal (binding_set,
121 GTK_TYPE_MENU_DIRECTION_TYPE,
123 gtk_binding_entry_add_signal (binding_set,
126 GTK_TYPE_MENU_DIRECTION_TYPE,
128 gtk_binding_entry_add_signal (binding_set,
131 GTK_TYPE_MENU_DIRECTION_TYPE,
133 gtk_binding_entry_add_signal (binding_set,
136 GTK_TYPE_MENU_DIRECTION_TYPE,
137 GTK_MENU_DIR_PARENT);
138 gtk_binding_entry_add_signal (binding_set,
141 GTK_TYPE_MENU_DIRECTION_TYPE,
142 GTK_MENU_DIR_PARENT);
143 gtk_binding_entry_add_signal (binding_set,
146 GTK_TYPE_MENU_DIRECTION_TYPE,
148 gtk_binding_entry_add_signal (binding_set,
151 GTK_TYPE_MENU_DIRECTION_TYPE,
154 gtk_widget_class_install_style_property (widget_class,
155 g_param_spec_enum ("shadow_type",
157 P_("Style of bevel around the menubar"),
158 GTK_TYPE_SHADOW_TYPE,
162 gtk_widget_class_install_style_property (widget_class,
163 g_param_spec_int ("internal_padding",
164 P_("Internal padding"),
165 P_("Amount of border space between the menubar shadow and the menu items"),
171 gtk_settings_install_property (g_param_spec_int ("gtk-menu-bar-popup-delay",
172 P_("Delay before drop down menus appear"),
173 P_("Delay before the submenus of a menu bar appear"),
181 gtk_menu_bar_new (void)
183 return g_object_new (GTK_TYPE_MENU_BAR, NULL);
187 gtk_menu_bar_size_request (GtkWidget *widget,
188 GtkRequisition *requisition)
190 GtkMenuBar *menu_bar;
191 GtkMenuShell *menu_shell;
195 GtkRequisition child_requisition;
198 g_return_if_fail (GTK_IS_MENU_BAR (widget));
199 g_return_if_fail (requisition != NULL);
201 requisition->width = 0;
202 requisition->height = 0;
204 if (GTK_WIDGET_VISIBLE (widget))
206 menu_bar = GTK_MENU_BAR (widget);
207 menu_shell = GTK_MENU_SHELL (widget);
210 children = menu_shell->children;
214 child = children->data;
215 children = children->next;
217 if (GTK_WIDGET_VISIBLE (child))
221 GTK_MENU_ITEM (child)->show_submenu_indicator = FALSE;
222 gtk_widget_size_request (child, &child_requisition);
223 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
226 requisition->width += child_requisition.width;
227 requisition->width += toggle_size;
229 requisition->height = MAX (requisition->height, child_requisition.height);
234 gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
236 requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
239 requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
243 if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
245 requisition->width += widget->style->xthickness * 2;
246 requisition->height += widget->style->ythickness * 2;
252 gtk_menu_bar_size_allocate (GtkWidget *widget,
253 GtkAllocation *allocation)
255 GtkMenuBar *menu_bar;
256 GtkMenuShell *menu_shell;
259 GtkAllocation child_allocation;
260 GtkRequisition child_requisition;
262 GtkTextDirection direction;
266 g_return_if_fail (GTK_IS_MENU_BAR (widget));
267 g_return_if_fail (allocation != NULL);
269 menu_bar = GTK_MENU_BAR (widget);
270 menu_shell = GTK_MENU_SHELL (widget);
272 direction = gtk_widget_get_direction (widget);
274 widget->allocation = *allocation;
275 if (GTK_WIDGET_REALIZED (widget))
276 gdk_window_move_resize (widget->window,
277 allocation->x, allocation->y,
278 allocation->width, allocation->height);
280 gtk_widget_style_get (widget, "internal_padding", &ipadding, NULL);
282 if (menu_shell->children)
284 child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
287 child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
290 if (get_shadow_type (menu_bar) != GTK_SHADOW_NONE)
292 child_allocation.x += widget->style->xthickness;
293 child_allocation.y += widget->style->ythickness;
296 child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
298 offset = child_allocation.x; /* Window edge to menubar start */
299 ltr_x = child_allocation.x;
301 children = menu_shell->children;
306 child = children->data;
307 children = children->next;
309 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
311 gtk_widget_get_child_requisition (child, &child_requisition);
313 child_requisition.width += toggle_size;
315 /* Support for the right justified help menu */
316 if ((children == NULL) && (GTK_IS_MENU_ITEM(child))
317 && (GTK_MENU_ITEM(child)->right_justify))
319 ltr_x = allocation->width -
320 child_requisition.width - offset;
322 if (GTK_WIDGET_VISIBLE (child))
324 if (direction == GTK_TEXT_DIR_LTR)
325 child_allocation.x = ltr_x;
327 child_allocation.x = allocation->width -
328 child_requisition.width - ltr_x;
330 child_allocation.width = child_requisition.width;
332 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
334 gtk_widget_size_allocate (child, &child_allocation);
336 ltr_x += child_allocation.width;
343 gtk_menu_bar_paint (GtkWidget *widget, GdkRectangle *area)
345 g_return_if_fail (GTK_IS_MENU_BAR (widget));
347 if (GTK_WIDGET_DRAWABLE (widget))
351 border = GTK_CONTAINER (widget)->border_width;
353 gtk_paint_box (widget->style,
355 GTK_WIDGET_STATE (widget),
356 get_shadow_type (GTK_MENU_BAR (widget)),
357 area, widget, "menubar",
359 widget->allocation.width - border * 2,
360 widget->allocation.height - border * 2);
365 gtk_menu_bar_expose (GtkWidget *widget,
366 GdkEventExpose *event)
368 g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
369 g_return_val_if_fail (event != NULL, FALSE);
371 if (GTK_WIDGET_DRAWABLE (widget))
373 gtk_menu_bar_paint (widget, &event->area);
375 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
382 get_menu_bars (GtkWindow *window)
384 return g_object_get_data (G_OBJECT (window), "gtk-menu-bar-list");
388 get_viewable_menu_bars (GtkWindow *window)
391 GList *viewable_menu_bars = NULL;
393 for (menu_bars = get_menu_bars (window);
395 menu_bars = menu_bars->next)
397 GtkWidget *widget = menu_bars->data;
398 gboolean viewable = TRUE;
402 if (!GTK_WIDGET_MAPPED (widget))
405 widget = widget->parent;
409 viewable_menu_bars = g_list_prepend (viewable_menu_bars, menu_bars->data);
412 return g_list_reverse (viewable_menu_bars);
416 set_menu_bars (GtkWindow *window,
419 g_object_set_data (G_OBJECT (window), "gtk-menu-bar-list", menubars);
423 window_key_press_handler (GtkWidget *widget,
428 gboolean retval = FALSE;
430 g_object_get (gtk_widget_get_settings (widget),
431 "gtk-menu-bar-accel",
438 GdkModifierType mods = 0;
440 gtk_accelerator_parse (accel, &keyval, &mods);
443 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
445 /* FIXME this is wrong, needs to be in the global accel resolution
446 * thing, to properly consider i18n etc., but that probably requires
447 * AccelGroup changes etc.
449 if (event->keyval == keyval &&
450 ((event->state & gtk_accelerator_get_default_mod_mask ()) ==
451 (mods & gtk_accelerator_get_default_mod_mask ())))
453 GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (widget));
456 menubars = _gtk_container_focus_sort (GTK_CONTAINER (widget), tmp_menubars,
457 GTK_DIR_TAB_FORWARD, NULL);
458 g_list_free (tmp_menubars);
462 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menubars->data);
464 _gtk_menu_shell_activate (menu_shell);
465 gtk_menu_shell_select_first (menu_shell, FALSE);
467 g_list_free (menubars);
480 add_to_window (GtkWindow *window,
483 GList *menubars = get_menu_bars (window);
487 g_signal_connect (window,
489 G_CALLBACK (window_key_press_handler),
493 set_menu_bars (window, g_list_prepend (menubars, menubar));
497 remove_from_window (GtkWindow *window,
500 GList *menubars = get_menu_bars (window);
502 menubars = g_object_get_data (G_OBJECT (window),
503 "gtk-menu-bar-list");
505 menubars = g_list_remove (menubars, menubar);
509 g_signal_handlers_disconnect_by_func (window,
510 window_key_press_handler,
514 set_menu_bars (window, menubars);
518 gtk_menu_bar_hierarchy_changed (GtkWidget *widget,
519 GtkWidget *old_toplevel)
524 menubar = GTK_MENU_BAR (widget);
526 toplevel = gtk_widget_get_toplevel (widget);
529 remove_from_window (GTK_WINDOW (old_toplevel), menubar);
531 if (GTK_WIDGET_TOPLEVEL (toplevel))
532 add_to_window (GTK_WINDOW (toplevel), menubar);
536 * _gtk_menu_bar_cycle_focus:
537 * @menubar: a #GtkMenuBar
538 * @dir: direction in which to cycle the focus
540 * Move the focus between menubars in the toplevel.
543 _gtk_menu_bar_cycle_focus (GtkMenuBar *menubar,
544 GtkDirectionType dir)
546 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (menubar));
547 GtkMenuItem *to_activate = NULL;
549 if (GTK_WIDGET_TOPLEVEL (toplevel))
551 GList *tmp_menubars = get_viewable_menu_bars (GTK_WINDOW (toplevel));
555 menubars = _gtk_container_focus_sort (GTK_CONTAINER (toplevel), tmp_menubars,
556 dir, GTK_WIDGET (menubar));
557 g_list_free (tmp_menubars);
561 current = g_list_find (menubars, menubar);
563 if (current && current->next)
565 GtkMenuShell *new_menushell = GTK_MENU_SHELL (current->next->data);
566 if (new_menushell->children)
567 to_activate = new_menushell->children->data;
571 g_list_free (menubars);
574 gtk_menu_shell_cancel (GTK_MENU_SHELL (menubar));
577 g_signal_emit_by_name (to_activate, "activate_item");
581 get_shadow_type (GtkMenuBar *menubar)
583 GtkShadowType shadow_type = GTK_SHADOW_OUT;
585 gtk_widget_style_get (GTK_WIDGET (menubar),
586 "shadow_type", &shadow_type,
593 gtk_menu_bar_get_popup_delay (GtkMenuShell *menu_shell)
597 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
598 "gtk-menu-bar-popup-delay", &popup_delay,