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.
21 * Modified by the GTK+ Team and others 1997-1999. 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/.
28 #include "gtkmenuitem.h"
29 #include "gtkoptionmenu.h"
30 #include "gtksignal.h"
31 #include "gdk/gdkkeysyms.h"
34 #define CHILD_LEFT_SPACING 5
35 #define CHILD_RIGHT_SPACING 1
36 #define CHILD_TOP_SPACING 1
37 #define CHILD_BOTTOM_SPACING 1
38 #define OPTION_INDICATOR_WIDTH 12
39 #define OPTION_INDICATOR_HEIGHT 8
40 #define OPTION_INDICATOR_SPACING 2
43 static void gtk_option_menu_class_init (GtkOptionMenuClass *klass);
44 static void gtk_option_menu_init (GtkOptionMenu *option_menu);
45 static void gtk_option_menu_destroy (GtkObject *object);
46 static void gtk_option_menu_size_request (GtkWidget *widget,
47 GtkRequisition *requisition);
48 static void gtk_option_menu_size_allocate (GtkWidget *widget,
49 GtkAllocation *allocation);
50 static void gtk_option_menu_paint (GtkWidget *widget,
52 static void gtk_option_menu_draw (GtkWidget *widget,
54 static gint gtk_option_menu_expose (GtkWidget *widget,
55 GdkEventExpose *event);
56 static gint gtk_option_menu_button_press (GtkWidget *widget,
57 GdkEventButton *event);
58 static gint gtk_option_menu_key_press (GtkWidget *widget,
60 static void gtk_option_menu_deactivate (GtkMenuShell *menu_shell,
61 GtkOptionMenu *option_menu);
62 static void gtk_option_menu_update_contents (GtkOptionMenu *option_menu);
63 static void gtk_option_menu_remove_contents (GtkOptionMenu *option_menu);
64 static void gtk_option_menu_calc_size (GtkOptionMenu *option_menu);
65 static void gtk_option_menu_position (GtkMenu *menu,
69 static void gtk_option_menu_show_all (GtkWidget *widget);
70 static void gtk_option_menu_hide_all (GtkWidget *widget);
71 static GtkType gtk_option_menu_child_type (GtkContainer *container);
75 static GtkButtonClass *parent_class = NULL;
79 gtk_option_menu_get_type (void)
81 static GtkType option_menu_type = 0;
83 if (!option_menu_type)
85 static const GtkTypeInfo option_menu_info =
88 sizeof (GtkOptionMenu),
89 sizeof (GtkOptionMenuClass),
90 (GtkClassInitFunc) gtk_option_menu_class_init,
91 (GtkObjectInitFunc) gtk_option_menu_init,
92 /* reserved_1 */ NULL,
93 /* reserved_2 */ NULL,
94 (GtkClassInitFunc) NULL,
97 option_menu_type = gtk_type_unique (gtk_button_get_type (), &option_menu_info);
100 return option_menu_type;
104 gtk_option_menu_class_init (GtkOptionMenuClass *class)
106 GtkObjectClass *object_class;
107 GtkWidgetClass *widget_class;
108 GtkButtonClass *button_class;
109 GtkContainerClass *container_class;
111 object_class = (GtkObjectClass*) class;
112 widget_class = (GtkWidgetClass*) class;
113 button_class = (GtkButtonClass*) class;
114 container_class = (GtkContainerClass*) class;
116 parent_class = gtk_type_class (gtk_button_get_type ());
118 object_class->destroy = gtk_option_menu_destroy;
120 widget_class->draw = gtk_option_menu_draw;
121 widget_class->size_request = gtk_option_menu_size_request;
122 widget_class->size_allocate = gtk_option_menu_size_allocate;
123 widget_class->expose_event = gtk_option_menu_expose;
124 widget_class->button_press_event = gtk_option_menu_button_press;
125 widget_class->key_press_event = gtk_option_menu_key_press;
126 widget_class->show_all = gtk_option_menu_show_all;
127 widget_class->hide_all = gtk_option_menu_hide_all;
129 container_class->child_type = gtk_option_menu_child_type;
133 gtk_option_menu_child_type (GtkContainer *container)
135 return GTK_TYPE_NONE;
139 gtk_option_menu_init (GtkOptionMenu *option_menu)
141 GTK_WIDGET_SET_FLAGS (option_menu, GTK_CAN_FOCUS);
142 GTK_WIDGET_UNSET_FLAGS (option_menu, GTK_CAN_DEFAULT | GTK_RECEIVES_DEFAULT);
144 option_menu->menu = NULL;
145 option_menu->menu_item = NULL;
146 option_menu->width = 0;
147 option_menu->height = 0;
151 gtk_option_menu_new (void)
153 return GTK_WIDGET (gtk_type_new (gtk_option_menu_get_type ()));
157 gtk_option_menu_get_menu (GtkOptionMenu *option_menu)
159 g_return_val_if_fail (option_menu != NULL, NULL);
160 g_return_val_if_fail (GTK_IS_OPTION_MENU (option_menu), NULL);
162 return option_menu->menu;
166 gtk_option_menu_detacher (GtkWidget *widget,
169 GtkOptionMenu *option_menu;
171 g_return_if_fail (widget != NULL);
172 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
174 option_menu = GTK_OPTION_MENU (widget);
175 g_return_if_fail (option_menu->menu == (GtkWidget*) menu);
177 gtk_option_menu_remove_contents (option_menu);
178 gtk_signal_disconnect_by_data (GTK_OBJECT (option_menu->menu),
181 option_menu->menu = NULL;
185 gtk_option_menu_set_menu (GtkOptionMenu *option_menu,
188 g_return_if_fail (option_menu != NULL);
189 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
190 g_return_if_fail (menu != NULL);
191 g_return_if_fail (GTK_IS_MENU (menu));
193 if (option_menu->menu != menu)
195 gtk_option_menu_remove_menu (option_menu);
197 option_menu->menu = menu;
198 gtk_menu_attach_to_widget (GTK_MENU (menu),
199 GTK_WIDGET (option_menu),
200 gtk_option_menu_detacher);
202 gtk_option_menu_calc_size (option_menu);
204 gtk_signal_connect (GTK_OBJECT (option_menu->menu), "deactivate",
205 (GtkSignalFunc) gtk_option_menu_deactivate,
208 if (GTK_WIDGET (option_menu)->parent)
209 gtk_widget_queue_resize (GTK_WIDGET (option_menu));
211 gtk_option_menu_update_contents (option_menu);
216 gtk_option_menu_remove_menu (GtkOptionMenu *option_menu)
218 g_return_if_fail (option_menu != NULL);
219 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
221 if (option_menu->menu)
222 gtk_menu_detach (GTK_MENU (option_menu->menu));
226 gtk_option_menu_set_history (GtkOptionMenu *option_menu,
229 GtkWidget *menu_item;
231 g_return_if_fail (option_menu != NULL);
232 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
234 if (option_menu->menu)
236 gtk_menu_set_active (GTK_MENU (option_menu->menu), index);
237 menu_item = gtk_menu_get_active (GTK_MENU (option_menu->menu));
239 if (menu_item != option_menu->menu_item)
241 gtk_option_menu_remove_contents (option_menu);
242 gtk_option_menu_update_contents (option_menu);
248 gtk_option_menu_get_history (GtkOptionMenu *option_menu)
250 GtkWidget *active_widget;
252 g_return_val_if_fail (GTK_IS_OPTION_MENU (option_menu), -1);
254 if (option_menu->menu)
256 active_widget = gtk_menu_get_active (GTK_MENU (option_menu->menu));
259 return g_list_index (GTK_MENU_SHELL (option_menu->menu)->children,
269 gtk_option_menu_destroy (GtkObject *object)
271 GtkOptionMenu *option_menu;
273 g_return_if_fail (object != NULL);
274 g_return_if_fail (GTK_IS_OPTION_MENU (object));
276 option_menu = GTK_OPTION_MENU (object);
278 if (option_menu->menu)
279 gtk_widget_destroy (option_menu->menu);
281 if (GTK_OBJECT_CLASS (parent_class)->destroy)
282 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
286 gtk_option_menu_size_request (GtkWidget *widget,
287 GtkRequisition *requisition)
289 GtkOptionMenu *option_menu;
292 g_return_if_fail (widget != NULL);
293 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
294 g_return_if_fail (requisition != NULL);
296 option_menu = GTK_OPTION_MENU (widget);
298 requisition->width = ((GTK_CONTAINER (widget)->border_width +
299 GTK_WIDGET (widget)->style->klass->xthickness) * 2 +
301 OPTION_INDICATOR_WIDTH +
302 OPTION_INDICATOR_SPACING * 5 +
303 CHILD_LEFT_SPACING + CHILD_RIGHT_SPACING + 2);
304 requisition->height = ((GTK_CONTAINER (widget)->border_width +
305 GTK_WIDGET (widget)->style->klass->ythickness) * 2 +
306 option_menu->height +
307 CHILD_TOP_SPACING + CHILD_BOTTOM_SPACING + 2);
309 tmp = (requisition->height - option_menu->height +
310 OPTION_INDICATOR_HEIGHT + OPTION_INDICATOR_SPACING * 2);
311 requisition->height = MAX (requisition->height, tmp);
315 gtk_option_menu_size_allocate (GtkWidget *widget,
316 GtkAllocation *allocation)
319 GtkAllocation child_allocation;
321 g_return_if_fail (widget != NULL);
322 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
323 g_return_if_fail (allocation != NULL);
325 widget->allocation = *allocation;
326 if (GTK_WIDGET_REALIZED (widget))
327 gdk_window_move_resize (widget->window,
328 allocation->x, allocation->y,
329 allocation->width, allocation->height);
331 child = GTK_BIN (widget)->child;
332 if (child && GTK_WIDGET_VISIBLE (child))
334 child_allocation.x = (GTK_CONTAINER (widget)->border_width +
335 GTK_WIDGET (widget)->style->klass->xthickness) + 1;
336 child_allocation.y = (GTK_CONTAINER (widget)->border_width +
337 GTK_WIDGET (widget)->style->klass->ythickness) + 1;
338 child_allocation.width = MAX (1, (gint)allocation->width - child_allocation.x * 2 -
339 OPTION_INDICATOR_WIDTH - OPTION_INDICATOR_SPACING * 5 -
340 CHILD_LEFT_SPACING - CHILD_RIGHT_SPACING - 2);
341 child_allocation.height = MAX (1, (gint)allocation->height - child_allocation.y * 2 -
342 CHILD_TOP_SPACING - CHILD_BOTTOM_SPACING - 2);
343 child_allocation.x += CHILD_LEFT_SPACING;
344 child_allocation.y += CHILD_RIGHT_SPACING;
346 gtk_widget_size_allocate (child, &child_allocation);
351 gtk_option_menu_paint (GtkWidget *widget,
354 GdkRectangle button_area;
356 g_return_if_fail (widget != NULL);
357 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
358 g_return_if_fail (area != NULL);
360 if (GTK_WIDGET_DRAWABLE (widget))
362 button_area.x = GTK_CONTAINER (widget)->border_width + 1;
363 button_area.y = GTK_CONTAINER (widget)->border_width + 1;
364 button_area.width = widget->allocation.width - button_area.x * 2;
365 button_area.height = widget->allocation.height - button_area.y * 2;
367 /* This is evil, and should be elimated here and in the button
368 * code. The point is to clear the focus, and make it
369 * sort of transparent if it isn't there.
371 gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
372 gdk_window_clear_area (widget->window, area->x, area->y, area->width, area->height);
374 gtk_paint_box(widget->style, widget->window,
375 GTK_WIDGET_STATE (widget), GTK_SHADOW_OUT,
376 area, widget, "optionmenu",
377 button_area.x, button_area.y,
378 button_area.width, button_area.height);
380 gtk_paint_tab (widget->style, widget->window,
381 GTK_WIDGET_STATE (widget), GTK_SHADOW_OUT,
382 area, widget, "optionmenutab",
383 button_area.x + button_area.width - button_area.x -
384 OPTION_INDICATOR_WIDTH - OPTION_INDICATOR_SPACING * 4,
385 button_area.y + (button_area.height - OPTION_INDICATOR_HEIGHT) / 2,
386 OPTION_INDICATOR_WIDTH, OPTION_INDICATOR_HEIGHT);
388 if (GTK_WIDGET_HAS_FOCUS (widget))
389 gtk_paint_focus (widget->style, widget->window,
390 area, widget, "button",
393 button_area.width + 1,
394 button_area.height + 1);
399 gtk_option_menu_draw (GtkWidget *widget,
403 GdkRectangle child_area;
405 g_return_if_fail (widget != NULL);
406 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
407 g_return_if_fail (area != NULL);
409 if (GTK_WIDGET_DRAWABLE (widget))
411 gtk_option_menu_paint (widget, area);
413 child = GTK_BIN (widget)->child;
414 if (child && gtk_widget_intersect (child, area, &child_area))
415 gtk_widget_draw (child, &child_area);
420 gtk_option_menu_expose (GtkWidget *widget,
421 GdkEventExpose *event)
424 GdkEventExpose child_event;
427 g_return_val_if_fail (widget != NULL, FALSE);
428 g_return_val_if_fail (GTK_IS_OPTION_MENU (widget), FALSE);
429 g_return_val_if_fail (event != NULL, FALSE);
431 if (GTK_WIDGET_DRAWABLE (widget))
433 gtk_option_menu_paint (widget, &event->area);
436 /* The following code tries to draw the child in two places at
437 * once. It fails miserably for several reasons
439 * - If the child is not no-window, removing generates
440 * more expose events. Bad, bad, bad.
442 * - Even if the child is no-window, removing it now (properly)
443 * clears the space where it was, so it does no good
447 remove_child = FALSE;
448 child = GTK_BUTTON (widget)->child;
452 if (!GTK_OPTION_MENU (widget)->menu)
454 gtk_option_menu_update_contents (GTK_OPTION_MENU (widget));
455 child = GTK_BUTTON (widget)->child;
461 child_event = *event;
463 if (GTK_WIDGET_NO_WINDOW (child) &&
464 gtk_widget_intersect (child, &event->area, &child_event.area))
465 gtk_widget_event (child, (GdkEvent*) &child_event);
468 gtk_option_menu_remove_contents (GTK_OPTION_MENU (widget));
470 remove_child = FALSE;
471 child = GTK_BIN (widget)->child;
472 child_event = *event;
473 if (child && GTK_WIDGET_NO_WINDOW (child) &&
474 gtk_widget_intersect (child, &event->area, &child_event.area))
475 gtk_widget_event (child, (GdkEvent*) &child_event);
484 gtk_option_menu_button_press (GtkWidget *widget,
485 GdkEventButton *event)
487 GtkOptionMenu *option_menu;
489 g_return_val_if_fail (widget != NULL, FALSE);
490 g_return_val_if_fail (GTK_IS_OPTION_MENU (widget), FALSE);
491 g_return_val_if_fail (event != NULL, FALSE);
493 option_menu = GTK_OPTION_MENU (widget);
495 if ((event->type == GDK_BUTTON_PRESS) &&
496 (event->button == 1))
498 gtk_option_menu_remove_contents (option_menu);
499 gtk_menu_popup (GTK_MENU (option_menu->menu), NULL, NULL,
500 gtk_option_menu_position, option_menu,
501 event->button, event->time);
509 gtk_option_menu_key_press (GtkWidget *widget,
512 GtkOptionMenu *option_menu;
514 g_return_val_if_fail (widget != NULL, FALSE);
515 g_return_val_if_fail (GTK_IS_OPTION_MENU (widget), FALSE);
516 g_return_val_if_fail (event != NULL, FALSE);
518 option_menu = GTK_OPTION_MENU (widget);
520 switch (event->keyval)
523 gtk_option_menu_remove_contents (option_menu);
524 gtk_menu_popup (GTK_MENU (option_menu->menu), NULL, NULL,
525 gtk_option_menu_position, option_menu,
534 gtk_option_menu_deactivate (GtkMenuShell *menu_shell,
535 GtkOptionMenu *option_menu)
537 g_return_if_fail (menu_shell != NULL);
538 g_return_if_fail (option_menu != NULL);
539 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
541 gtk_option_menu_update_contents (option_menu);
545 gtk_option_menu_update_contents (GtkOptionMenu *option_menu)
548 GtkRequisition child_requisition;
550 g_return_if_fail (option_menu != NULL);
551 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
553 if (option_menu->menu)
555 gtk_option_menu_remove_contents (option_menu);
557 option_menu->menu_item = gtk_menu_get_active (GTK_MENU (option_menu->menu));
558 if (option_menu->menu_item)
560 gtk_widget_ref (option_menu->menu_item);
561 child = GTK_BIN (option_menu->menu_item)->child;
564 if (GTK_BIN (option_menu)->child)
565 gtk_container_remove (GTK_CONTAINER (option_menu),
566 GTK_BIN (option_menu)->child);
567 if (GTK_WIDGET (option_menu)->state != child->state)
568 gtk_widget_set_state (child, GTK_WIDGET (option_menu)->state);
569 gtk_widget_reparent (child, GTK_WIDGET (option_menu));
572 gtk_widget_size_request (child, &child_requisition);
573 gtk_widget_size_allocate (GTK_WIDGET (option_menu),
574 &(GTK_WIDGET (option_menu)->allocation));
576 if (GTK_WIDGET_DRAWABLE (option_menu))
577 gtk_widget_queue_draw (GTK_WIDGET (option_menu));
583 gtk_option_menu_remove_contents (GtkOptionMenu *option_menu)
585 g_return_if_fail (option_menu != NULL);
586 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
588 if (GTK_BIN (option_menu)->child)
590 if (GTK_WIDGET (option_menu->menu_item)->state != GTK_BIN (option_menu)->child->state)
591 gtk_widget_set_state (GTK_BIN (option_menu)->child,
592 GTK_WIDGET (option_menu->menu_item)->state);
593 gtk_widget_reparent (GTK_BIN (option_menu)->child, option_menu->menu_item);
594 gtk_widget_unref (option_menu->menu_item);
595 option_menu->menu_item = NULL;
600 gtk_option_menu_calc_size (GtkOptionMenu *option_menu)
604 GtkRequisition child_requisition;
606 g_return_if_fail (option_menu != NULL);
607 g_return_if_fail (GTK_IS_OPTION_MENU (option_menu));
609 option_menu->width = 0;
610 option_menu->height = 0;
612 if (option_menu->menu)
614 children = GTK_MENU_SHELL (option_menu->menu)->children;
617 child = children->data;
618 children = children->next;
620 if (GTK_WIDGET_VISIBLE (child))
622 gtk_widget_size_request (child, &child_requisition);
624 option_menu->width = MAX (option_menu->width, child_requisition.width);
625 option_menu->height = MAX (option_menu->height, child_requisition.height);
632 gtk_option_menu_position (GtkMenu *menu,
637 GtkOptionMenu *option_menu;
649 g_return_if_fail (user_data != NULL);
650 g_return_if_fail (GTK_IS_OPTION_MENU (user_data));
652 option_menu = GTK_OPTION_MENU (user_data);
654 width = GTK_WIDGET (menu)->allocation.width;
655 height = GTK_WIDGET (menu)->allocation.height;
657 active = gtk_menu_get_active (GTK_MENU (option_menu->menu));
658 children = GTK_MENU_SHELL (option_menu->menu)->children;
659 gdk_window_get_origin (GTK_WIDGET (option_menu)->window, &menu_xpos, &menu_ypos);
661 menu_ypos += GTK_WIDGET (option_menu)->allocation.height / 2 - 2;
664 menu_ypos -= active->requisition.height / 2;
668 child = children->data;
673 if (GTK_WIDGET_VISIBLE (child))
674 menu_ypos -= child->allocation.height;
676 children = children->next;
679 screen_width = gdk_screen_width ();
680 screen_height = gdk_screen_height ();
688 else if ((menu_ypos + height) > screen_height)
690 menu_ypos -= ((menu_ypos + height) - screen_height);
696 if ((menu_xpos + GTK_WIDGET (option_menu)->allocation.width + width) <= screen_width)
697 menu_xpos += GTK_WIDGET (option_menu)->allocation.width;
704 else if ((menu_xpos + width) > screen_width)
705 menu_xpos -= ((menu_xpos + width) - screen_width);
713 gtk_option_menu_show_all (GtkWidget *widget)
715 GtkContainer *container;
716 GtkOptionMenu *option_menu;
718 g_return_if_fail (widget != NULL);
719 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
720 container = GTK_CONTAINER (widget);
721 option_menu = GTK_OPTION_MENU (widget);
723 gtk_widget_show (widget);
724 gtk_container_foreach (container, (GtkCallback) gtk_widget_show_all, NULL);
725 if (option_menu->menu)
726 gtk_widget_show_all (option_menu->menu);
727 if (option_menu->menu_item)
728 gtk_widget_show_all (option_menu->menu_item);
733 gtk_option_menu_hide_all (GtkWidget *widget)
735 GtkContainer *container;
737 g_return_if_fail (widget != NULL);
738 g_return_if_fail (GTK_IS_OPTION_MENU (widget));
739 container = GTK_CONTAINER (widget);
741 gtk_widget_hide (widget);
742 gtk_container_foreach (container, (GtkCallback) gtk_widget_hide_all, NULL);