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 #include "gdk/gdkkeysyms.h"
28 #include "gtkbindings.h"
30 #include "gtkmenubar.h"
31 #include "gtkmenuitem.h"
39 #define BORDER_SPACING 0
40 #define CHILD_SPACING 3
43 static void gtk_menu_bar_class_init (GtkMenuBarClass *klass);
44 static void gtk_menu_bar_init (GtkMenuBar *menu_bar);
45 static void gtk_menu_bar_set_arg (GtkObject *object,
48 static void gtk_menu_bar_get_arg (GtkObject *object,
51 static void gtk_menu_bar_size_request (GtkWidget *widget,
52 GtkRequisition *requisition);
53 static void gtk_menu_bar_size_allocate (GtkWidget *widget,
54 GtkAllocation *allocation);
55 static void gtk_menu_bar_paint (GtkWidget *widget,
57 static gint gtk_menu_bar_expose (GtkWidget *widget,
58 GdkEventExpose *event);
62 gtk_menu_bar_get_type (void)
64 static GtkType menu_bar_type = 0;
68 static const GtkTypeInfo menu_bar_info =
72 sizeof (GtkMenuBarClass),
73 (GtkClassInitFunc) gtk_menu_bar_class_init,
74 (GtkObjectInitFunc) gtk_menu_bar_init,
75 /* reserved_1 */ NULL,
76 /* reserved_2 */ NULL,
77 (GtkClassInitFunc) NULL,
80 menu_bar_type = gtk_type_unique (gtk_menu_shell_get_type (), &menu_bar_info);
87 gtk_menu_bar_class_init (GtkMenuBarClass *class)
89 GtkObjectClass *object_class;
90 GtkWidgetClass *widget_class;
91 GtkMenuShellClass *menu_shell_class;
93 GtkBindingSet *binding_set;
95 object_class = (GtkObjectClass*) class;
96 widget_class = (GtkWidgetClass*) class;
97 menu_shell_class = (GtkMenuShellClass*) class;
99 gtk_object_add_arg_type ("GtkMenuBar::shadow", GTK_TYPE_SHADOW_TYPE, GTK_ARG_READWRITE, ARG_SHADOW);
101 object_class->set_arg = gtk_menu_bar_set_arg;
102 object_class->get_arg = gtk_menu_bar_get_arg;
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;
108 menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
110 binding_set = gtk_binding_set_by_class (class);
111 gtk_binding_entry_add_signal (binding_set,
114 GTK_TYPE_MENU_DIRECTION_TYPE,
116 gtk_binding_entry_add_signal (binding_set,
119 GTK_TYPE_MENU_DIRECTION_TYPE,
121 gtk_binding_entry_add_signal (binding_set,
124 GTK_TYPE_MENU_DIRECTION_TYPE,
125 GTK_MENU_DIR_PARENT);
126 gtk_binding_entry_add_signal (binding_set,
129 GTK_TYPE_MENU_DIRECTION_TYPE,
134 gtk_menu_bar_init (GtkMenuBar *menu_bar)
136 menu_bar->shadow_type = GTK_SHADOW_OUT;
140 gtk_menu_bar_set_arg (GtkObject *object,
144 GtkMenuBar *menu_bar;
146 menu_bar = GTK_MENU_BAR (object);
151 gtk_menu_bar_set_shadow_type (menu_bar, GTK_VALUE_ENUM (*arg));
159 gtk_menu_bar_get_arg (GtkObject *object,
163 GtkMenuBar *menu_bar;
165 menu_bar = GTK_MENU_BAR (object);
170 GTK_VALUE_ENUM (*arg) = menu_bar->shadow_type;
173 arg->type = GTK_TYPE_INVALID;
179 gtk_menu_bar_new (void)
181 return GTK_WIDGET (gtk_type_new (gtk_menu_bar_get_type ()));
185 gtk_menu_bar_append (GtkMenuBar *menu_bar,
188 gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), child);
192 gtk_menu_bar_prepend (GtkMenuBar *menu_bar,
195 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu_bar), child);
199 gtk_menu_bar_insert (GtkMenuBar *menu_bar,
203 gtk_menu_shell_insert (GTK_MENU_SHELL (menu_bar), child, position);
208 gtk_menu_bar_size_request (GtkWidget *widget,
209 GtkRequisition *requisition)
211 GtkMenuBar *menu_bar;
212 GtkMenuShell *menu_shell;
216 GtkRequisition child_requisition;
218 g_return_if_fail (widget != NULL);
219 g_return_if_fail (GTK_IS_MENU_BAR (widget));
220 g_return_if_fail (requisition != NULL);
222 requisition->width = 0;
223 requisition->height = 0;
225 if (GTK_WIDGET_VISIBLE (widget))
227 menu_bar = GTK_MENU_BAR (widget);
228 menu_shell = GTK_MENU_SHELL (widget);
231 children = menu_shell->children;
235 child = children->data;
236 children = children->next;
238 if (GTK_WIDGET_VISIBLE (child))
242 GTK_MENU_ITEM (child)->show_submenu_indicator = FALSE;
243 gtk_widget_size_request (child, &child_requisition);
244 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
247 requisition->width += child_requisition.width;
248 requisition->width += toggle_size;
250 requisition->height = MAX (requisition->height, child_requisition.height);
251 /* Support for the right justified help menu */
252 if ((children == NULL) && GTK_IS_MENU_ITEM(child) &&
253 GTK_MENU_ITEM(child)->right_justify)
255 requisition->width += CHILD_SPACING;
262 requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
263 widget->style->xthickness +
265 requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
266 widget->style->ythickness +
270 requisition->width += 2 * CHILD_SPACING * (nchildren - 1);
275 gtk_menu_bar_size_allocate (GtkWidget *widget,
276 GtkAllocation *allocation)
278 GtkMenuBar *menu_bar;
279 GtkMenuShell *menu_shell;
282 GtkAllocation child_allocation;
283 GtkRequisition child_requisition;
286 g_return_if_fail (widget != NULL);
287 g_return_if_fail (GTK_IS_MENU_BAR (widget));
288 g_return_if_fail (allocation != NULL);
290 menu_bar = GTK_MENU_BAR (widget);
291 menu_shell = GTK_MENU_SHELL (widget);
293 widget->allocation = *allocation;
294 if (GTK_WIDGET_REALIZED (widget))
295 gdk_window_move_resize (widget->window,
296 allocation->x, allocation->y,
297 allocation->width, allocation->height);
299 if (menu_shell->children)
301 child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
302 widget->style->xthickness +
304 offset = child_allocation.x; /* Window edge to menubar start */
306 child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
307 widget->style->ythickness +
309 child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
311 children = menu_shell->children;
316 child = children->data;
317 children = children->next;
319 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child),
321 gtk_widget_get_child_requisition (child, &child_requisition);
323 child_requisition.width += toggle_size;
325 /* Support for the right justified help menu */
326 if ( (children == NULL) && (GTK_IS_MENU_ITEM(child))
327 && (GTK_MENU_ITEM(child)->right_justify))
329 child_allocation.x = allocation->width -
330 child_requisition.width - CHILD_SPACING - offset;
332 if (GTK_WIDGET_VISIBLE (child))
334 child_allocation.width = child_requisition.width;
336 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
338 gtk_widget_size_allocate (child, &child_allocation);
340 child_allocation.x += child_allocation.width + CHILD_SPACING * 2;
347 gtk_menu_bar_set_shadow_type (GtkMenuBar *menu_bar,
350 g_return_if_fail (menu_bar != NULL);
351 g_return_if_fail (GTK_IS_MENU_BAR (menu_bar));
353 if ((GtkShadowType) menu_bar->shadow_type != type)
355 menu_bar->shadow_type = type;
357 if (GTK_WIDGET_DRAWABLE (menu_bar))
359 gtk_widget_queue_clear (GTK_WIDGET (menu_bar));
361 gtk_widget_queue_resize (GTK_WIDGET (menu_bar));
366 gtk_menu_bar_paint (GtkWidget *widget, GdkRectangle *area)
368 g_return_if_fail (widget != NULL);
369 g_return_if_fail (GTK_IS_MENU_BAR (widget));
371 if (GTK_WIDGET_DRAWABLE (widget))
373 gtk_paint_box (widget->style,
376 GTK_MENU_BAR (widget)->shadow_type,
377 area, widget, "menubar",
384 gtk_menu_bar_expose (GtkWidget *widget,
385 GdkEventExpose *event)
387 GtkMenuShell *menu_shell;
388 GdkEventExpose child_event;
392 g_return_val_if_fail (widget != NULL, FALSE);
393 g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
394 g_return_val_if_fail (event != NULL, FALSE);
396 if (GTK_WIDGET_DRAWABLE (widget))
398 gtk_menu_bar_paint (widget, &event->area);
400 menu_shell = GTK_MENU_SHELL (widget);
401 child_event = *event;
403 children = menu_shell->children;
406 child = children->data;
407 children = children->next;
409 if (GTK_WIDGET_NO_WINDOW (child) &&
410 gtk_widget_intersect (child, &event->area, &child_event.area))
411 gtk_widget_event (child, (GdkEvent*) &child_event);