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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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.
19 #include "gdk/gdkkeysyms.h"
20 #include "gtkbindings.h"
22 #include "gtkmenubar.h"
23 #include "gtkmenuitem.h"
31 #define BORDER_SPACING 0
32 #define CHILD_SPACING 3
35 static void gtk_menu_bar_class_init (GtkMenuBarClass *klass);
36 static void gtk_menu_bar_init (GtkMenuBar *menu_bar);
37 static void gtk_menu_bar_set_arg (GtkObject *object,
40 static void gtk_menu_bar_get_arg (GtkObject *object,
43 static void gtk_menu_bar_size_request (GtkWidget *widget,
44 GtkRequisition *requisition);
45 static void gtk_menu_bar_size_allocate (GtkWidget *widget,
46 GtkAllocation *allocation);
47 static void gtk_menu_bar_paint (GtkWidget *widget,
49 static void gtk_menu_bar_draw (GtkWidget *widget,
51 static gint gtk_menu_bar_expose (GtkWidget *widget,
52 GdkEventExpose *event);
56 gtk_menu_bar_get_type (void)
58 static GtkType menu_bar_type = 0;
62 static const GtkTypeInfo menu_bar_info =
66 sizeof (GtkMenuBarClass),
67 (GtkClassInitFunc) gtk_menu_bar_class_init,
68 (GtkObjectInitFunc) gtk_menu_bar_init,
69 /* reserved_1 */ NULL,
70 /* reserved_2 */ NULL,
71 (GtkClassInitFunc) NULL,
74 menu_bar_type = gtk_type_unique (gtk_menu_shell_get_type (), &menu_bar_info);
81 gtk_menu_bar_class_init (GtkMenuBarClass *class)
83 GtkObjectClass *object_class;
84 GtkWidgetClass *widget_class;
85 GtkMenuShellClass *menu_shell_class;
87 GtkBindingSet *binding_set;
89 object_class = (GtkObjectClass*) class;
90 widget_class = (GtkWidgetClass*) class;
91 menu_shell_class = (GtkMenuShellClass*) class;
93 gtk_object_add_arg_type ("GtkMenuBar::shadow", GTK_TYPE_SHADOW_TYPE, GTK_ARG_READWRITE, ARG_SHADOW);
95 object_class->set_arg = gtk_menu_bar_set_arg;
96 object_class->get_arg = gtk_menu_bar_get_arg;
98 widget_class->draw = gtk_menu_bar_draw;
99 widget_class->size_request = gtk_menu_bar_size_request;
100 widget_class->size_allocate = gtk_menu_bar_size_allocate;
101 widget_class->expose_event = gtk_menu_bar_expose;
103 menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
105 binding_set = gtk_binding_set_by_class (class);
106 gtk_binding_entry_add_signal (binding_set,
109 GTK_TYPE_MENU_DIRECTION_TYPE,
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,
120 GTK_MENU_DIR_PARENT);
121 gtk_binding_entry_add_signal (binding_set,
124 GTK_TYPE_MENU_DIRECTION_TYPE,
129 gtk_menu_bar_init (GtkMenuBar *menu_bar)
131 menu_bar->shadow_type = GTK_SHADOW_OUT;
135 gtk_menu_bar_set_arg (GtkObject *object,
139 GtkMenuBar *menu_bar;
141 menu_bar = GTK_MENU_BAR (object);
146 gtk_menu_bar_set_shadow_type (menu_bar, GTK_VALUE_ENUM (*arg));
154 gtk_menu_bar_get_arg (GtkObject *object,
158 GtkMenuBar *menu_bar;
160 menu_bar = GTK_MENU_BAR (object);
165 GTK_VALUE_ENUM (*arg) = menu_bar->shadow_type;
168 arg->type = GTK_TYPE_INVALID;
174 gtk_menu_bar_new (void)
176 return GTK_WIDGET (gtk_type_new (gtk_menu_bar_get_type ()));
180 gtk_menu_bar_append (GtkMenuBar *menu_bar,
183 gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), child);
187 gtk_menu_bar_prepend (GtkMenuBar *menu_bar,
190 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu_bar), child);
194 gtk_menu_bar_insert (GtkMenuBar *menu_bar,
198 gtk_menu_shell_insert (GTK_MENU_SHELL (menu_bar), child, position);
203 gtk_menu_bar_size_request (GtkWidget *widget,
204 GtkRequisition *requisition)
206 GtkMenuBar *menu_bar;
207 GtkMenuShell *menu_shell;
212 g_return_if_fail (widget != NULL);
213 g_return_if_fail (GTK_IS_MENU_BAR (widget));
214 g_return_if_fail (requisition != NULL);
216 requisition->width = 0;
217 requisition->height = 0;
219 if (GTK_WIDGET_VISIBLE (widget))
221 menu_bar = GTK_MENU_BAR (widget);
222 menu_shell = GTK_MENU_SHELL (widget);
225 children = menu_shell->children;
229 child = children->data;
230 children = children->next;
232 if (GTK_WIDGET_VISIBLE (child))
234 GTK_MENU_ITEM (child)->show_submenu_indicator = FALSE;
235 gtk_widget_size_request (child, &child->requisition);
237 requisition->width += child->requisition.width;
238 requisition->height = MAX (requisition->height, child->requisition.height);
239 /* Support for the right justified help menu */
240 if ( (children == NULL) && (GTK_IS_MENU_ITEM(child))
241 && (GTK_MENU_ITEM(child)->right_justify))
243 requisition->width += CHILD_SPACING;
250 requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
251 widget->style->klass->xthickness +
253 requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
254 widget->style->klass->ythickness +
258 requisition->width += 2 * CHILD_SPACING * (nchildren - 1);
263 gtk_menu_bar_size_allocate (GtkWidget *widget,
264 GtkAllocation *allocation)
266 GtkMenuBar *menu_bar;
267 GtkMenuShell *menu_shell;
270 GtkAllocation child_allocation;
273 g_return_if_fail (widget != NULL);
274 g_return_if_fail (GTK_IS_MENU_BAR (widget));
275 g_return_if_fail (allocation != NULL);
277 menu_bar = GTK_MENU_BAR (widget);
278 menu_shell = GTK_MENU_SHELL (widget);
280 widget->allocation = *allocation;
281 if (GTK_WIDGET_REALIZED (widget))
282 gdk_window_move_resize (widget->window,
283 allocation->x, allocation->y,
284 allocation->width, allocation->height);
286 if (menu_shell->children)
288 child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
289 widget->style->klass->xthickness +
291 offset = child_allocation.x; /* Window edge to menubar start */
293 child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
294 widget->style->klass->ythickness +
296 child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2);
298 children = menu_shell->children;
301 child = children->data;
302 children = children->next;
304 /* Support for the right justified help menu */
305 if ( (children == NULL) && (GTK_IS_MENU_ITEM(child))
306 && (GTK_MENU_ITEM(child)->right_justify))
308 child_allocation.x = allocation->width -
309 child->requisition.width - CHILD_SPACING - offset;
311 if (GTK_WIDGET_VISIBLE (child))
313 child_allocation.width = child->requisition.width;
315 gtk_widget_size_allocate (child, &child_allocation);
317 child_allocation.x += child_allocation.width + CHILD_SPACING * 2;
324 gtk_menu_bar_set_shadow_type (GtkMenuBar *menu_bar,
327 g_return_if_fail (menu_bar != NULL);
328 g_return_if_fail (GTK_IS_MENU_BAR (menu_bar));
330 if ((GtkShadowType) menu_bar->shadow_type != type)
332 menu_bar->shadow_type = type;
334 if (GTK_WIDGET_DRAWABLE (menu_bar))
336 gtk_widget_queue_clear (GTK_WIDGET (menu_bar));
338 gtk_widget_queue_resize (GTK_WIDGET (menu_bar));
343 gtk_menu_bar_paint (GtkWidget *widget, GdkRectangle *area)
345 g_return_if_fail (widget != NULL);
346 g_return_if_fail (GTK_IS_MENU_BAR (widget));
348 if (GTK_WIDGET_DRAWABLE (widget))
350 gtk_paint_box (widget->style,
353 GTK_MENU_BAR (widget)->shadow_type,
354 area, widget, "menubar",
361 gtk_menu_bar_draw (GtkWidget *widget,
364 GtkMenuShell *menu_shell;
366 GdkRectangle child_area;
369 g_return_if_fail (widget != NULL);
370 g_return_if_fail (GTK_IS_MENU_BAR (widget));
371 g_return_if_fail (area != NULL);
373 if (GTK_WIDGET_DRAWABLE (widget))
375 gtk_menu_bar_paint (widget, area);
377 menu_shell = GTK_MENU_SHELL (widget);
379 children = menu_shell->children;
382 child = children->data;
383 children = children->next;
385 if (gtk_widget_intersect (child, area, &child_area))
386 gtk_widget_draw (child, &child_area);
392 gtk_menu_bar_expose (GtkWidget *widget,
393 GdkEventExpose *event)
395 GtkMenuShell *menu_shell;
396 GdkEventExpose child_event;
400 g_return_val_if_fail (widget != NULL, FALSE);
401 g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
402 g_return_val_if_fail (event != NULL, FALSE);
404 if (GTK_WIDGET_DRAWABLE (widget))
406 gtk_menu_bar_paint (widget, &event->area);
408 menu_shell = GTK_MENU_SHELL (widget);
409 child_event = *event;
411 children = menu_shell->children;
414 child = children->data;
415 children = children->next;
417 if (GTK_WIDGET_NO_WINDOW (child) &&
418 gtk_widget_intersect (child, &event->area, &child_event.area))
419 gtk_widget_event (child, (GdkEvent*) &child_event);