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 "gtkmenuitem.h"
21 #include "gtkoptionmenu.h"
22 #include "gtksignal.h"
25 #define CHILD_LEFT_SPACING 5
26 #define CHILD_RIGHT_SPACING 1
27 #define CHILD_TOP_SPACING 1
28 #define CHILD_BOTTOM_SPACING 1
29 #define OPTION_INDICATOR_WIDTH 12
30 #define OPTION_INDICATOR_HEIGHT 8
31 #define OPTION_INDICATOR_SPACING 2
34 static void gtk_option_menu_class_init (GtkOptionMenuClass *klass);
35 static void gtk_option_menu_init (GtkOptionMenu *option_menu);
36 static void gtk_option_menu_destroy (GtkObject *object);
37 static void gtk_option_menu_size_request (GtkWidget *widget,
38 GtkRequisition *requisition);
39 static void gtk_option_menu_size_allocate (GtkWidget *widget,
40 GtkAllocation *allocation);
41 static void gtk_option_menu_paint (GtkWidget *widget,
43 static void gtk_option_menu_draw (GtkWidget *widget,
45 static gint gtk_option_menu_expose (GtkWidget *widget,
46 GdkEventExpose *event);
47 static gint gtk_option_menu_button_press (GtkWidget *widget,
48 GdkEventButton *event);
49 static void gtk_option_menu_deactivate (GtkMenuShell *menu_shell,
50 GtkOptionMenu *option_menu);
51 static void gtk_option_menu_update_contents (GtkOptionMenu *option_menu);
52 static void gtk_option_menu_remove_contents (GtkOptionMenu *option_menu);
53 static void gtk_option_menu_calc_size (GtkOptionMenu *option_menu);
54 static void gtk_option_menu_position (GtkMenu *menu,
58 static void gtk_option_menu_show_all (GtkWidget *widget);
59 static void gtk_option_menu_hide_all (GtkWidget *widget);
60 static GtkType gtk_option_menu_child_type (GtkContainer *container);
63 static GtkButtonClass *parent_class = NULL;
67 gtk_option_menu_get_type (void)
69 static GtkType option_menu_type = 0;
71 if (!option_menu_type)
73 GtkTypeInfo option_menu_info =
76 sizeof (GtkOptionMenu),
77 sizeof (GtkOptionMenuClass),
78 (GtkClassInitFunc) gtk_option_menu_class_init,
79 (GtkObjectInitFunc) gtk_option_menu_init,
84 option_menu_type = gtk_type_unique (gtk_button_get_type (), &option_menu_info);
87 return option_menu_type;
91 gtk_option_menu_class_init (GtkOptionMenuClass *class)
93 GtkObjectClass *object_class;
94 GtkWidgetClass *widget_class;
95 GtkButtonClass *button_class;
96 GtkContainerClass *container_class;
98 object_class = (GtkObjectClass*) class;
99 widget_class = (GtkWidgetClass*) class;
100 button_class = (GtkButtonClass*) class;
101 container_class = (GtkContainerClass*) class;
103 parent_class = gtk_type_class (gtk_button_get_type ());
105 object_class->destroy = gtk_option_menu_destroy;
107 widget_class->draw = gtk_option_menu_draw;
108 widget_class->draw_focus = NULL;
109 widget_class->size_request = gtk_option_menu_size_request;
110 widget_class->size_allocate = gtk_option_menu_size_allocate;
111 widget_class->expose_event = gtk_option_menu_expose;
112 widget_class->button_press_event = gtk_option_menu_button_press;
113 widget_class->show_all = gtk_option_menu_show_all;
114 widget_class->hide_all = gtk_option_menu_hide_all;
116 container_class->child_type = gtk_option_menu_child_type;
120 gtk_option_menu_child_type (GtkContainer *container)
122 return GTK_TYPE_NONE;
126 gtk_option_menu_init (GtkOptionMenu *option_menu)
128 GTK_WIDGET_UNSET_FLAGS (option_menu, GTK_CAN_FOCUS);
130 option_menu->menu = NULL;
131 option_menu->menu_item = NULL;
132 option_menu->width = 0;
133 option_menu->height = 0;
137 gtk_option_menu_new (void)
139 return GTK_WIDGET (gtk_type_new (gtk_option_menu_get_type ()));
143 gtk_option_menu_get_menu (GtkOptionMenu *option_menu)
145 g_return_val_if_fail (option_menu != NULL, NULL);
146 g_return_val_if_fail (GTK_IS_OPTION_MENU (option_menu), NULL);
148 return option_menu->menu;
152 gtk_option_menu_detacher (GtkWidget *widget,
155 GtkOptionMenu *option_menu;
157 g_return_if_fail (widget != NULL);
158 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
160 option_menu = GTK_OPTION_MENU (widget);
161 g_return_if_fail (option_menu->menu == (GtkWidget*) menu);
163 gtk_option_menu_remove_contents (option_menu);
164 gtk_signal_disconnect_by_data (GTK_OBJECT (option_menu->menu),
167 option_menu->menu = NULL;
171 gtk_option_menu_set_menu (GtkOptionMenu *option_menu,
174 g_return_if_fail (option_menu != NULL);
175 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
176 g_return_if_fail (menu != NULL);
177 g_return_if_fail (GTK_IS_MENU (menu));
179 if (option_menu->menu != menu)
181 gtk_option_menu_remove_menu (option_menu);
183 option_menu->menu = menu;
184 gtk_menu_attach_to_widget (GTK_MENU (menu),
185 GTK_WIDGET (option_menu),
186 gtk_option_menu_detacher);
188 gtk_option_menu_calc_size (option_menu);
190 gtk_signal_connect (GTK_OBJECT (option_menu->menu), "deactivate",
191 (GtkSignalFunc) gtk_option_menu_deactivate,
194 if (GTK_WIDGET (option_menu)->parent)
195 gtk_widget_queue_resize (GTK_WIDGET (option_menu));
197 gtk_option_menu_update_contents (option_menu);
202 gtk_option_menu_remove_menu (GtkOptionMenu *option_menu)
204 g_return_if_fail (option_menu != NULL);
205 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
207 if (option_menu->menu)
208 gtk_menu_detach (GTK_MENU (option_menu->menu));
212 gtk_option_menu_set_history (GtkOptionMenu *option_menu,
215 GtkWidget *menu_item;
217 g_return_if_fail (option_menu != NULL);
218 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
220 if (option_menu->menu)
222 gtk_menu_set_active (GTK_MENU (option_menu->menu), index);
223 menu_item = gtk_menu_get_active (GTK_MENU (option_menu->menu));
225 if (menu_item != option_menu->menu_item)
227 gtk_option_menu_remove_contents (option_menu);
228 gtk_option_menu_update_contents (option_menu);
235 gtk_option_menu_destroy (GtkObject *object)
237 GtkOptionMenu *option_menu;
239 g_return_if_fail (object != NULL);
240 g_return_if_fail (GTK_IS_OPTION_MENU (object));
242 option_menu = GTK_OPTION_MENU (object);
244 if (option_menu->menu)
245 gtk_widget_destroy (option_menu->menu);
247 if (GTK_OBJECT_CLASS (parent_class)->destroy)
248 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
252 gtk_option_menu_size_request (GtkWidget *widget,
253 GtkRequisition *requisition)
255 GtkOptionMenu *option_menu;
258 g_return_if_fail (widget != NULL);
259 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
260 g_return_if_fail (requisition != NULL);
262 option_menu = GTK_OPTION_MENU (widget);
264 requisition->width = ((GTK_CONTAINER (widget)->border_width +
265 GTK_WIDGET (widget)->style->klass->xthickness) * 2 +
267 OPTION_INDICATOR_WIDTH +
268 OPTION_INDICATOR_SPACING * 5 +
269 CHILD_LEFT_SPACING + CHILD_RIGHT_SPACING);
270 requisition->height = ((GTK_CONTAINER (widget)->border_width +
271 GTK_WIDGET (widget)->style->klass->ythickness) * 2 +
272 option_menu->height +
273 CHILD_TOP_SPACING + CHILD_BOTTOM_SPACING);
275 tmp = (requisition->height - option_menu->height +
276 OPTION_INDICATOR_HEIGHT + OPTION_INDICATOR_SPACING * 2);
277 requisition->height = MAX (requisition->height, tmp);
281 gtk_option_menu_size_allocate (GtkWidget *widget,
282 GtkAllocation *allocation)
285 GtkAllocation child_allocation;
287 g_return_if_fail (widget != NULL);
288 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
289 g_return_if_fail (allocation != NULL);
291 widget->allocation = *allocation;
292 if (GTK_WIDGET_REALIZED (widget))
293 gdk_window_move_resize (widget->window,
294 allocation->x, allocation->y,
295 allocation->width, allocation->height);
297 child = GTK_BUTTON (widget)->child;
298 if (child && GTK_WIDGET_VISIBLE (child))
300 child_allocation.x = (GTK_CONTAINER (widget)->border_width +
301 GTK_WIDGET (widget)->style->klass->xthickness);
302 child_allocation.y = (GTK_CONTAINER (widget)->border_width +
303 GTK_WIDGET (widget)->style->klass->ythickness);
304 child_allocation.width = (allocation->width - child_allocation.x * 2 -
305 OPTION_INDICATOR_WIDTH - OPTION_INDICATOR_SPACING * 5 -
306 CHILD_LEFT_SPACING - CHILD_RIGHT_SPACING);
307 child_allocation.height = (allocation->height - child_allocation.y * 2 -
308 CHILD_TOP_SPACING - CHILD_BOTTOM_SPACING);
309 child_allocation.x += CHILD_LEFT_SPACING;
310 child_allocation.y += CHILD_RIGHT_SPACING;
312 gtk_widget_size_allocate (child, &child_allocation);
317 gtk_option_menu_paint (GtkWidget *widget,
320 GdkRectangle restrict_area;
321 GdkRectangle new_area;
323 g_return_if_fail (widget != NULL);
324 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
325 g_return_if_fail (area != NULL);
327 if (GTK_WIDGET_DRAWABLE (widget))
329 restrict_area.x = GTK_CONTAINER (widget)->border_width;
330 restrict_area.y = GTK_CONTAINER (widget)->border_width;
331 restrict_area.width = widget->allocation.width - restrict_area.x * 2;
332 restrict_area.height = widget->allocation.height - restrict_area.y * 2;
334 if (gdk_rectangle_intersect (area, &restrict_area, &new_area))
336 gtk_style_set_background (widget->style, widget->window, GTK_WIDGET_STATE (widget));
337 gdk_window_clear_area (widget->window,
338 new_area.x, new_area.y,
339 new_area.width, new_area.height);
341 gtk_draw_shadow (widget->style, widget->window,
342 GTK_WIDGET_STATE (widget), GTK_SHADOW_OUT,
343 restrict_area.x, restrict_area.y,
344 restrict_area.width, restrict_area.height);
346 gtk_draw_shadow (widget->style, widget->window,
347 GTK_WIDGET_STATE (widget), GTK_SHADOW_OUT,
348 restrict_area.x + restrict_area.width - restrict_area.x -
349 OPTION_INDICATOR_WIDTH - OPTION_INDICATOR_SPACING * 4,
350 restrict_area.y + (restrict_area.height - OPTION_INDICATOR_HEIGHT) / 2,
351 OPTION_INDICATOR_WIDTH, OPTION_INDICATOR_HEIGHT);
357 gtk_option_menu_draw (GtkWidget *widget,
361 GdkRectangle child_area;
363 g_return_if_fail (widget != NULL);
364 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
365 g_return_if_fail (area != NULL);
367 if (GTK_WIDGET_DRAWABLE (widget))
369 gtk_option_menu_paint (widget, area);
371 child = GTK_BUTTON (widget)->child;
372 if (child && gtk_widget_intersect (child, area, &child_area))
373 gtk_widget_draw (child, &child_area);
378 gtk_option_menu_expose (GtkWidget *widget,
379 GdkEventExpose *event)
382 GdkEventExpose child_event;
385 g_return_val_if_fail (widget != NULL, FALSE);
386 g_return_val_if_fail (GTK_IS_OPTION_MENU (widget), FALSE);
387 g_return_val_if_fail (event != NULL, FALSE);
389 if (GTK_WIDGET_DRAWABLE (widget))
391 gtk_option_menu_paint (widget, &event->area);
394 /* The following code tries to draw the child in two places at
395 * once. It fails miserably for several reasons
397 * - If the child is not no-window, removing generates
398 * more expose events. Bad, bad, bad.
400 * - Even if the child is no-window, removing it now (properly)
401 * clears the space where it was, so it does no good
405 remove_child = FALSE;
406 child = GTK_BUTTON (widget)->child;
410 if (!GTK_OPTION_MENU (widget)->menu)
412 gtk_option_menu_update_contents (GTK_OPTION_MENU (widget));
413 child = GTK_BUTTON (widget)->child;
419 child_event = *event;
421 if (GTK_WIDGET_NO_WINDOW (child) &&
422 gtk_widget_intersect (child, &event->area, &child_event.area))
423 gtk_widget_event (child, (GdkEvent*) &child_event);
426 gtk_option_menu_remove_contents (GTK_OPTION_MENU (widget));
428 remove_child = FALSE;
429 child = GTK_BUTTON (widget)->child;
430 child_event = *event;
431 if (child && GTK_WIDGET_NO_WINDOW (child) &&
432 gtk_widget_intersect (child, &event->area, &child_event.area))
433 gtk_widget_event (child, (GdkEvent*) &child_event);
442 gtk_option_menu_button_press (GtkWidget *widget,
443 GdkEventButton *event)
445 GtkOptionMenu *option_menu;
447 g_return_val_if_fail (widget != NULL, FALSE);
448 g_return_val_if_fail (GTK_IS_OPTION_MENU (widget), FALSE);
449 g_return_val_if_fail (event != NULL, FALSE);
451 if ((event->type == GDK_BUTTON_PRESS) &&
452 (event->button == 1))
454 option_menu = GTK_OPTION_MENU (widget);
455 gtk_option_menu_remove_contents (option_menu);
456 gtk_menu_popup (GTK_MENU (option_menu->menu), NULL, NULL,
457 gtk_option_menu_position, option_menu,
458 event->button, event->time);
465 gtk_option_menu_deactivate (GtkMenuShell *menu_shell,
466 GtkOptionMenu *option_menu)
468 g_return_if_fail (menu_shell != NULL);
469 g_return_if_fail (option_menu != NULL);
470 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
472 gtk_option_menu_update_contents (option_menu);
476 gtk_option_menu_update_contents (GtkOptionMenu *option_menu)
480 g_return_if_fail (option_menu != NULL);
481 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
483 if (option_menu->menu)
485 gtk_option_menu_remove_contents (option_menu);
487 option_menu->menu_item = gtk_menu_get_active (GTK_MENU (option_menu->menu));
488 if (option_menu->menu_item)
490 gtk_widget_ref (option_menu->menu_item);
491 child = GTK_BIN (option_menu->menu_item)->child;
494 gtk_container_block_resize (GTK_CONTAINER (option_menu));
495 if (GTK_BUTTON (option_menu)->child)
496 gtk_container_remove (GTK_CONTAINER (option_menu),
497 GTK_BUTTON (option_menu)->child);
498 if (GTK_WIDGET (option_menu)->state != child->state)
499 gtk_widget_set_state (child, GTK_WIDGET (option_menu)->state);
500 gtk_widget_reparent (child, GTK_WIDGET (option_menu));
501 gtk_container_unblock_resize (GTK_CONTAINER (option_menu));
504 gtk_widget_size_request (child, &child->requisition);
505 gtk_widget_size_allocate (GTK_WIDGET (option_menu),
506 &(GTK_WIDGET (option_menu)->allocation));
508 if (GTK_WIDGET_DRAWABLE (option_menu))
509 gtk_widget_queue_draw (GTK_WIDGET (option_menu));
515 gtk_option_menu_remove_contents (GtkOptionMenu *option_menu)
517 g_return_if_fail (option_menu != NULL);
518 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
520 if (GTK_BUTTON (option_menu)->child)
522 gtk_container_block_resize (GTK_CONTAINER (option_menu));
523 if (GTK_WIDGET (option_menu->menu_item)->state != GTK_BUTTON (option_menu)->child->state)
524 gtk_widget_set_state (GTK_BUTTON (option_menu)->child,
525 GTK_WIDGET (option_menu->menu_item)->state);
526 gtk_widget_reparent (GTK_BUTTON (option_menu)->child, option_menu->menu_item);
527 gtk_widget_unref (option_menu->menu_item);
528 option_menu->menu_item = NULL;
529 gtk_container_unblock_resize (GTK_CONTAINER (option_menu));
534 gtk_option_menu_calc_size (GtkOptionMenu *option_menu)
539 g_return_if_fail (option_menu != NULL);
540 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
542 option_menu->width = 0;
543 option_menu->height = 0;
545 if (option_menu->menu)
547 children = GTK_MENU_SHELL (option_menu->menu)->children;
550 child = children->data;
551 children = children->next;
553 if (GTK_WIDGET_VISIBLE (child))
555 gtk_widget_size_request (child, &child->requisition);
557 option_menu->width = MAX (option_menu->width, child->requisition.width);
558 option_menu->height = MAX (option_menu->height, child->requisition.height);
565 gtk_option_menu_position (GtkMenu *menu,
570 GtkOptionMenu *option_menu;
582 g_return_if_fail (user_data != NULL);
583 g_return_if_fail (GTK_IS_OPTION_MENU (user_data));
585 option_menu = GTK_OPTION_MENU (user_data);
587 width = GTK_WIDGET (menu)->allocation.width;
588 height = GTK_WIDGET (menu)->allocation.height;
590 active = gtk_menu_get_active (GTK_MENU (option_menu->menu));
591 children = GTK_MENU_SHELL (option_menu->menu)->children;
592 gdk_window_get_origin (GTK_WIDGET (option_menu)->window, &menu_xpos, &menu_ypos);
594 menu_ypos += GTK_WIDGET (option_menu)->allocation.height / 2 - 2;
597 menu_ypos -= active->requisition.height / 2;
601 child = children->data;
606 menu_ypos -= child->allocation.height;
607 children = children->next;
610 screen_width = gdk_screen_width ();
611 screen_height = gdk_screen_height ();
619 else if ((menu_ypos + height) > screen_height)
621 menu_ypos -= ((menu_ypos + height) - screen_height);
627 if ((menu_xpos + GTK_WIDGET (option_menu)->allocation.width + width) <= screen_width)
628 menu_xpos += GTK_WIDGET (option_menu)->allocation.width;
635 else if ((menu_xpos + width) > screen_width)
636 menu_xpos -= ((menu_xpos + width) - screen_width);
644 gtk_option_menu_show_all (GtkWidget *widget)
646 GtkContainer *container;
647 GtkOptionMenu *option_menu;
649 g_return_if_fail (widget != NULL);
650 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
651 container = GTK_CONTAINER (widget);
652 option_menu = GTK_OPTION_MENU (widget);
654 gtk_widget_show (widget);
655 gtk_container_foreach (container, (GtkCallback) gtk_widget_show_all, NULL);
656 if (option_menu->menu)
657 gtk_widget_show_all (option_menu->menu);
658 if (option_menu->menu_item)
659 gtk_widget_show_all (option_menu->menu_item);
664 gtk_option_menu_hide_all (GtkWidget *widget)
666 GtkContainer *container;
668 g_return_if_fail (widget != NULL);
669 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
670 container = GTK_CONTAINER (widget);
672 gtk_widget_hide (widget);
673 gtk_container_foreach (container, (GtkCallback) gtk_widget_hide_all, NULL);