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.
20 #include "gtkmenubar.h"
21 #include "gtkmenuitem.h"
24 #define BORDER_SPACING 2
25 #define CHILD_SPACING 3
28 static void gtk_menu_bar_class_init (GtkMenuBarClass *klass);
29 static void gtk_menu_bar_init (GtkMenuBar *menu_bar);
30 static void gtk_menu_bar_size_request (GtkWidget *widget,
31 GtkRequisition *requisition);
32 static void gtk_menu_bar_size_allocate (GtkWidget *widget,
33 GtkAllocation *allocation);
34 static void gtk_menu_bar_paint (GtkWidget *widget);
35 static void gtk_menu_bar_draw (GtkWidget *widget,
37 static gint gtk_menu_bar_expose (GtkWidget *widget,
38 GdkEventExpose *event);
42 gtk_menu_bar_get_type (void)
44 static GtkType menu_bar_type = 0;
48 GtkTypeInfo menu_bar_info =
52 sizeof (GtkMenuBarClass),
53 (GtkClassInitFunc) gtk_menu_bar_class_init,
54 (GtkObjectInitFunc) gtk_menu_bar_init,
55 /* reversed_1 */ NULL,
56 /* reversed_2 */ NULL,
57 (GtkClassInitFunc) NULL,
60 menu_bar_type = gtk_type_unique (gtk_menu_shell_get_type (), &menu_bar_info);
67 gtk_menu_bar_class_init (GtkMenuBarClass *class)
69 GtkWidgetClass *widget_class;
70 GtkMenuShellClass *menu_shell_class;
72 widget_class = (GtkWidgetClass*) class;
73 menu_shell_class = (GtkMenuShellClass*) class;
75 widget_class->draw = gtk_menu_bar_draw;
76 widget_class->size_request = gtk_menu_bar_size_request;
77 widget_class->size_allocate = gtk_menu_bar_size_allocate;
78 widget_class->expose_event = gtk_menu_bar_expose;
80 menu_shell_class->submenu_placement = GTK_TOP_BOTTOM;
84 gtk_menu_bar_init (GtkMenuBar *menu_bar)
89 gtk_menu_bar_new (void)
91 return GTK_WIDGET (gtk_type_new (gtk_menu_bar_get_type ()));
95 gtk_menu_bar_append (GtkMenuBar *menu_bar,
98 gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), child);
102 gtk_menu_bar_prepend (GtkMenuBar *menu_bar,
105 gtk_menu_shell_prepend (GTK_MENU_SHELL (menu_bar), child);
109 gtk_menu_bar_insert (GtkMenuBar *menu_bar,
113 gtk_menu_shell_insert (GTK_MENU_SHELL (menu_bar), child, position);
118 gtk_menu_bar_size_request (GtkWidget *widget,
119 GtkRequisition *requisition)
121 GtkMenuBar *menu_bar;
122 GtkMenuShell *menu_shell;
127 g_return_if_fail (widget != NULL);
128 g_return_if_fail (GTK_IS_MENU_BAR (widget));
129 g_return_if_fail (requisition != NULL);
131 requisition->width = 0;
132 requisition->height = 0;
134 if (GTK_WIDGET_VISIBLE (widget))
136 menu_bar = GTK_MENU_BAR (widget);
137 menu_shell = GTK_MENU_SHELL (widget);
140 children = menu_shell->children;
144 child = children->data;
145 children = children->next;
147 if (GTK_WIDGET_VISIBLE (child))
149 GTK_MENU_ITEM (child)->show_submenu_indicator = FALSE;
150 gtk_widget_size_request (child, &child->requisition);
152 requisition->width += child->requisition.width;
153 requisition->height = MAX (requisition->height, child->requisition.height);
154 /* Support for the right justified help menu */
155 if ( (children == NULL) && (GTK_IS_MENU_ITEM(child))
156 && (GTK_MENU_ITEM(child)->right_justify))
158 requisition->width += CHILD_SPACING;
165 requisition->width += (GTK_CONTAINER (menu_bar)->border_width +
166 widget->style->klass->xthickness +
168 requisition->height += (GTK_CONTAINER (menu_bar)->border_width +
169 widget->style->klass->ythickness +
173 requisition->width += 2 * CHILD_SPACING * (nchildren - 1);
178 gtk_menu_bar_size_allocate (GtkWidget *widget,
179 GtkAllocation *allocation)
181 GtkMenuBar *menu_bar;
182 GtkMenuShell *menu_shell;
185 GtkAllocation child_allocation;
188 g_return_if_fail (widget != NULL);
189 g_return_if_fail (GTK_IS_MENU_BAR (widget));
190 g_return_if_fail (allocation != NULL);
192 menu_bar = GTK_MENU_BAR (widget);
193 menu_shell = GTK_MENU_SHELL (widget);
195 widget->allocation = *allocation;
196 if (GTK_WIDGET_REALIZED (widget))
197 gdk_window_move_resize (widget->window,
198 allocation->x, allocation->y,
199 allocation->width, allocation->height);
201 if (menu_shell->children)
203 child_allocation.x = (GTK_CONTAINER (menu_bar)->border_width +
204 widget->style->klass->xthickness +
206 offset = child_allocation.x; /* Window edge to menubar start */
208 child_allocation.y = (GTK_CONTAINER (menu_bar)->border_width +
209 widget->style->klass->ythickness +
211 child_allocation.height = MAX (1, allocation->height - child_allocation.y * 2);
213 children = menu_shell->children;
216 child = children->data;
217 children = children->next;
219 /* Support for the right justified help menu */
220 if ( (children == NULL) && (GTK_IS_MENU_ITEM(child))
221 && (GTK_MENU_ITEM(child)->right_justify))
223 child_allocation.x = allocation->width -
224 child->requisition.width - CHILD_SPACING - offset;
226 if (GTK_WIDGET_VISIBLE (child))
228 child_allocation.width = child->requisition.width;
230 gtk_widget_size_allocate (child, &child_allocation);
232 child_allocation.x += child_allocation.width + CHILD_SPACING * 2;
239 gtk_menu_bar_paint (GtkWidget *widget)
241 g_return_if_fail (widget != NULL);
242 g_return_if_fail (GTK_IS_MENU_BAR (widget));
244 if (GTK_WIDGET_DRAWABLE (widget))
246 gtk_draw_shadow (widget->style,
251 widget->allocation.width,
252 widget->allocation.height);
257 gtk_menu_bar_draw (GtkWidget *widget,
260 GtkMenuShell *menu_shell;
262 GdkRectangle child_area;
265 g_return_if_fail (widget != NULL);
266 g_return_if_fail (GTK_IS_MENU_BAR (widget));
267 g_return_if_fail (area != NULL);
269 if (GTK_WIDGET_DRAWABLE (widget))
271 gtk_menu_bar_paint (widget);
273 menu_shell = GTK_MENU_SHELL (widget);
275 children = menu_shell->children;
278 child = children->data;
279 children = children->next;
281 if (gtk_widget_intersect (child, area, &child_area))
282 gtk_widget_draw (child, &child_area);
288 gtk_menu_bar_expose (GtkWidget *widget,
289 GdkEventExpose *event)
291 GtkMenuShell *menu_shell;
293 GdkEventExpose child_event;
296 g_return_val_if_fail (widget != NULL, FALSE);
297 g_return_val_if_fail (GTK_IS_MENU_BAR (widget), FALSE);
298 g_return_val_if_fail (event != NULL, FALSE);
300 if (GTK_WIDGET_DRAWABLE (widget))
302 gtk_menu_bar_paint (widget);
304 menu_shell = GTK_MENU_SHELL (widget);
305 child_event = *event;
307 children = menu_shell->children;
310 child = children->data;
311 children = children->next;
313 if (GTK_WIDGET_NO_WINDOW (child) &&
314 gtk_widget_intersect (child, &event->area, &child_event.area))
315 gtk_widget_event (child, (GdkEvent*) &child_event);