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 <string.h> /* memset */
28 #include "gdk/gdkkeysyms.h"
29 #include "gtkaccelmap.h"
30 #include "gtkbindings.h"
34 #include "gtkmenuitem.h"
35 #include "gtksignal.h"
36 #include "gtkwindow.h"
38 #include "gtkvscrollbar.h"
39 #include "gtksettings.h"
43 #define MENU_ITEM_CLASS(w) GTK_MENU_ITEM_GET_CLASS (w)
44 #define MENU_NEEDS_RESIZE(m) GTK_MENU_SHELL (m)->menu_flag
46 #define SUBMENU_NAV_REGION_PADDING 2
47 #define SUBMENU_NAV_HYSTERESIS_TIMEOUT 333
49 #define MENU_SCROLL_STEP 10
50 #define MENU_SCROLL_ARROW_HEIGHT 16
51 #define MENU_SCROLL_FAST_ZONE 4
52 #define MENU_SCROLL_TIMEOUT1 150
53 #define MENU_SCROLL_TIMEOUT2 50
55 typedef struct _GtkMenuAttachData GtkMenuAttachData;
57 struct _GtkMenuAttachData
59 GtkWidget *attach_widget;
60 GtkMenuDetachFunc detacher;
68 static void gtk_menu_class_init (GtkMenuClass *klass);
69 static void gtk_menu_init (GtkMenu *menu);
70 static void gtk_menu_set_property (GObject *object,
74 static void gtk_menu_get_property (GObject *object,
78 static void gtk_menu_destroy (GtkObject *object);
79 static void gtk_menu_finalize (GObject *object);
80 static void gtk_menu_realize (GtkWidget *widget);
81 static void gtk_menu_unrealize (GtkWidget *widget);
82 static void gtk_menu_size_request (GtkWidget *widget,
83 GtkRequisition *requisition);
84 static void gtk_menu_size_allocate (GtkWidget *widget,
85 GtkAllocation *allocation);
86 static void gtk_menu_paint (GtkWidget *widget,
87 GdkEventExpose *expose);
88 static void gtk_menu_show (GtkWidget *widget);
89 static gboolean gtk_menu_expose (GtkWidget *widget,
90 GdkEventExpose *event);
91 static gboolean gtk_menu_key_press (GtkWidget *widget,
93 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
94 GdkEventMotion *event);
95 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
96 GdkEventCrossing *event);
97 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
98 GdkEventCrossing *event);
99 static void gtk_menu_scroll_to (GtkMenu *menu,
101 static void gtk_menu_stop_scrolling (GtkMenu *menu);
102 static gboolean gtk_menu_scroll_timeout (gpointer data);
103 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
104 GtkWidget *menu_item);
105 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
106 GtkWidget *menu_item);
107 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
110 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
112 static void gtk_menu_handle_scrolling (GtkMenu *menu,
114 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
116 static void gtk_menu_style_set (GtkWidget *widget,
117 GtkStyle *previous_style);
120 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
121 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
122 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
125 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
126 GtkMenuItem *menu_item,
127 GdkEventCrossing *event);
129 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
130 static void gtk_menu_show_all (GtkWidget *widget);
131 static void gtk_menu_hide_all (GtkWidget *widget);
132 static void gtk_menu_position (GtkMenu *menu);
133 static void gtk_menu_reparent (GtkMenu *menu,
134 GtkWidget *new_parent,
136 static void gtk_menu_remove (GtkContainer *menu,
139 static void gtk_menu_update_title (GtkMenu *menu);
141 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
142 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
144 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
145 gboolean group_changed);
147 static GtkMenuShellClass *parent_class = NULL;
148 static const gchar *attach_data_key = "gtk-menu-attach-data";
151 gtk_menu_get_type (void)
153 static GtkType menu_type = 0;
157 static const GtkTypeInfo menu_info =
161 sizeof (GtkMenuClass),
162 (GtkClassInitFunc) gtk_menu_class_init,
163 (GtkObjectInitFunc) gtk_menu_init,
164 /* reserved_1 */ NULL,
165 /* reserved_2 */ NULL,
166 (GtkClassInitFunc) NULL,
169 menu_type = gtk_type_unique (gtk_menu_shell_get_type (), &menu_info);
176 gtk_menu_class_init (GtkMenuClass *class)
178 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
179 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
180 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
181 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
182 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
183 GtkBindingSet *binding_set;
185 parent_class = g_type_class_peek_parent (class);
187 gobject_class->finalize = gtk_menu_finalize;
188 gobject_class->set_property = gtk_menu_set_property;
189 gobject_class->get_property = gtk_menu_get_property;
191 g_object_class_install_property (gobject_class,
193 g_param_spec_string ("tearoff-title",
195 _("A title that may be displayed by the window manager when this menu is torn-off."),
197 G_PARAM_READABLE | G_PARAM_WRITABLE));
198 object_class->destroy = gtk_menu_destroy;
200 widget_class->realize = gtk_menu_realize;
201 widget_class->unrealize = gtk_menu_unrealize;
202 widget_class->size_request = gtk_menu_size_request;
203 widget_class->size_allocate = gtk_menu_size_allocate;
204 widget_class->show = gtk_menu_show;
205 widget_class->expose_event = gtk_menu_expose;
206 widget_class->key_press_event = gtk_menu_key_press;
207 widget_class->motion_notify_event = gtk_menu_motion_notify;
208 widget_class->show_all = gtk_menu_show_all;
209 widget_class->hide_all = gtk_menu_hide_all;
210 widget_class->enter_notify_event = gtk_menu_enter_notify;
211 widget_class->leave_notify_event = gtk_menu_leave_notify;
212 widget_class->style_set = gtk_menu_style_set;
214 container_class->remove = gtk_menu_remove;
216 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
217 menu_shell_class->deactivate = gtk_menu_deactivate;
218 menu_shell_class->select_item = gtk_menu_select_item;
219 menu_shell_class->insert = gtk_menu_real_insert;
221 binding_set = gtk_binding_set_by_class (class);
222 gtk_binding_entry_add_signal (binding_set,
225 GTK_TYPE_MENU_DIRECTION_TYPE,
227 gtk_binding_entry_add_signal (binding_set,
230 GTK_TYPE_MENU_DIRECTION_TYPE,
232 gtk_binding_entry_add_signal (binding_set,
235 GTK_TYPE_MENU_DIRECTION_TYPE,
237 gtk_binding_entry_add_signal (binding_set,
240 GTK_TYPE_MENU_DIRECTION_TYPE,
242 gtk_binding_entry_add_signal (binding_set,
245 GTK_TYPE_MENU_DIRECTION_TYPE,
246 GTK_MENU_DIR_PARENT);
247 gtk_binding_entry_add_signal (binding_set,
250 GTK_TYPE_MENU_DIRECTION_TYPE,
251 GTK_MENU_DIR_PARENT);
252 gtk_binding_entry_add_signal (binding_set,
255 GTK_TYPE_MENU_DIRECTION_TYPE,
257 gtk_binding_entry_add_signal (binding_set,
260 GTK_TYPE_MENU_DIRECTION_TYPE,
263 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
264 _("Can change accelerators"),
265 _("Whether menu accelerators can be changed by pressing a key over the menu item."),
272 gtk_menu_set_property (GObject *object,
279 menu = GTK_MENU (object);
283 case PROP_TEAROFF_TITLE:
284 gtk_menu_set_title (menu, g_value_get_string (value));
287 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
293 gtk_menu_get_property (GObject *object,
300 menu = GTK_MENU (object);
304 case PROP_TEAROFF_TITLE:
305 g_value_set_string (value, gtk_menu_get_title (menu));
308 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
314 gtk_menu_window_event (GtkWidget *window,
318 gboolean handled = FALSE;
320 gtk_widget_ref (window);
321 gtk_widget_ref (menu);
326 case GDK_KEY_RELEASE:
327 handled = gtk_widget_event (menu, event);
333 gtk_widget_unref (window);
334 gtk_widget_unref (menu);
340 gtk_menu_init (GtkMenu *menu)
342 menu->parent_menu_item = NULL;
343 menu->old_active_menu_item = NULL;
344 menu->accel_group = NULL;
345 menu->position_func = NULL;
346 menu->position_func_data = NULL;
347 menu->toggle_size = 0;
349 menu->toplevel = g_object_connect (gtk_widget_new (GTK_TYPE_WINDOW,
350 "type", GTK_WINDOW_POPUP,
353 "signal::event", gtk_menu_window_event, menu,
354 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
356 gtk_window_set_policy (GTK_WINDOW (menu->toplevel),
358 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
360 /* Refloat the menu, so that reference counting for the menu isn't
361 * affected by it being a child of the toplevel
363 GTK_WIDGET_SET_FLAGS (menu, GTK_FLOATING);
364 menu->needs_destruction_ref_count = TRUE;
366 menu->view_window = NULL;
367 menu->bin_window = NULL;
369 menu->scroll_offset = 0;
370 menu->scroll_step = 0;
371 menu->timeout_id = 0;
372 menu->scroll_fast = FALSE;
374 menu->tearoff_window = NULL;
375 menu->tearoff_hbox = NULL;
376 menu->torn_off = FALSE;
377 menu->tearoff_active = FALSE;
378 menu->tearoff_adjustment = NULL;
379 menu->tearoff_scrollbar = NULL;
381 menu->upper_arrow_visible = FALSE;
382 menu->lower_arrow_visible = FALSE;
383 menu->upper_arrow_prelight = FALSE;
384 menu->lower_arrow_prelight = FALSE;
386 MENU_NEEDS_RESIZE (menu) = TRUE;
390 gtk_menu_destroy (GtkObject *object)
393 GtkMenuAttachData *data;
395 g_return_if_fail (GTK_IS_MENU (object));
397 menu = GTK_MENU (object);
399 gtk_menu_stop_scrolling (menu);
401 data = gtk_object_get_data (object, attach_data_key);
403 gtk_menu_detach (menu);
405 gtk_menu_stop_navigating_submenu (menu);
407 if (menu->old_active_menu_item)
409 gtk_widget_unref (menu->old_active_menu_item);
410 menu->old_active_menu_item = NULL;
413 /* Add back the reference count for being a child */
414 if (menu->needs_destruction_ref_count)
416 menu->needs_destruction_ref_count = FALSE;
417 gtk_object_ref (object);
420 if (menu->accel_group)
422 g_object_unref (menu->accel_group);
423 menu->accel_group = NULL;
427 gtk_widget_destroy (menu->toplevel);
428 if (menu->tearoff_window)
429 gtk_widget_destroy (menu->tearoff_window);
431 GTK_OBJECT_CLASS (parent_class)->destroy (object);
435 gtk_menu_finalize (GObject *object)
437 GtkMenu *menu = GTK_MENU (object);
439 g_free (menu->accel_path);
441 G_OBJECT_CLASS (parent_class)->finalize (object);
445 gtk_menu_attach_to_widget (GtkMenu *menu,
446 GtkWidget *attach_widget,
447 GtkMenuDetachFunc detacher)
449 GtkMenuAttachData *data;
451 g_return_if_fail (GTK_IS_MENU (menu));
452 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
453 g_return_if_fail (detacher != NULL);
455 /* keep this function in sync with gtk_widget_set_parent()
458 data = gtk_object_get_data (GTK_OBJECT (menu), attach_data_key);
461 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
462 gtk_type_name (GTK_OBJECT_TYPE (data->attach_widget)));
466 gtk_object_ref (GTK_OBJECT (menu));
467 gtk_object_sink (GTK_OBJECT (menu));
469 data = g_new (GtkMenuAttachData, 1);
470 data->attach_widget = attach_widget;
471 data->detacher = detacher;
472 gtk_object_set_data (GTK_OBJECT (menu), attach_data_key, data);
474 if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
475 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
477 /* we don't need to set the style here, since
478 * we are a toplevel widget.
481 /* Fallback title for menu comes from attach widget */
482 gtk_menu_update_title (menu);
486 gtk_menu_get_attach_widget (GtkMenu *menu)
488 GtkMenuAttachData *data;
490 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
492 data = gtk_object_get_data (GTK_OBJECT (menu), attach_data_key);
494 return data->attach_widget;
499 gtk_menu_detach (GtkMenu *menu)
501 GtkMenuAttachData *data;
503 g_return_if_fail (GTK_IS_MENU (menu));
505 /* keep this function in sync with gtk_widget_unparent()
507 data = gtk_object_get_data (GTK_OBJECT (menu), attach_data_key);
510 g_warning ("gtk_menu_detach(): menu is not attached");
513 gtk_object_remove_data (GTK_OBJECT (menu), attach_data_key);
515 data->detacher (data->attach_widget, menu);
517 if (GTK_WIDGET_REALIZED (menu))
518 gtk_widget_unrealize (GTK_WIDGET (menu));
522 /* Fallback title for menu comes from attach widget */
523 gtk_menu_update_title (menu);
525 gtk_widget_unref (GTK_WIDGET (menu));
529 gtk_menu_remove (GtkContainer *container,
533 g_return_if_fail (GTK_IS_MENU (container));
534 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
536 menu = GTK_MENU (container);
538 /* Clear out old_active_menu_item if it matches the item we are removing
540 if (menu->old_active_menu_item == widget)
542 gtk_widget_unref (menu->old_active_menu_item);
543 menu->old_active_menu_item = NULL;
546 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
553 return GTK_WIDGET (gtk_type_new (gtk_menu_get_type ()));
557 gtk_menu_real_insert (GtkMenuShell *menu_shell,
561 if (GTK_WIDGET_REALIZED (menu_shell))
562 gtk_widget_set_parent_window (child, GTK_MENU (menu_shell)->bin_window);
564 GTK_MENU_SHELL_CLASS (parent_class)->insert (menu_shell, child, position);
568 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
573 widget = GTK_WIDGET (menu);
579 GdkGCValues gc_values;
581 menu->tearoff_active = FALSE;
582 menu->saved_scroll_offset = menu->scroll_offset;
584 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
585 gc = gdk_gc_new_with_values (widget->window,
586 &gc_values, GDK_GC_SUBWINDOW);
588 gdk_window_get_size (menu->tearoff_window->window, &width, &height);
590 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
595 gdk_draw_pixmap (pixmap, gc,
596 menu->tearoff_window->window,
600 gtk_widget_set_usize (menu->tearoff_window,
604 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
605 gdk_pixmap_unref (pixmap);
610 popup_grab_on_window (GdkWindow *window,
611 guint32 activate_time)
613 if ((gdk_pointer_grab (window, TRUE,
614 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
615 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
616 GDK_POINTER_MOTION_MASK,
617 NULL, NULL, activate_time) == 0))
619 if (gdk_keyboard_grab (window, TRUE,
624 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
634 gtk_menu_popup (GtkMenu *menu,
635 GtkWidget *parent_menu_shell,
636 GtkWidget *parent_menu_item,
637 GtkMenuPositionFunc func,
640 guint32 activate_time)
643 GtkWidget *xgrab_shell;
645 GdkEvent *current_event;
646 GtkMenuShell *menu_shell;
647 GtkMenuAttachData *attach_data;
649 g_return_if_fail (GTK_IS_MENU (menu));
651 widget = GTK_WIDGET (menu);
652 menu_shell = GTK_MENU_SHELL (menu);
654 menu_shell->parent_menu_shell = parent_menu_shell;
656 if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
658 /* The screen was not set explicitly, if the menu is
659 * attached to a widget, try to get screen from its
660 * toplevel window else go with the default
662 attach_data = gtk_object_get_data (GTK_OBJECT (menu), attach_data_key);
665 if (!GTK_WIDGET_REALIZED (menu))
666 gtk_window_set_screen (GTK_WINDOW (menu->toplevel),
667 gtk_widget_get_screen (attach_data->attach_widget));
671 /* Find the last viewable ancestor, and make an X grab on it
673 parent = GTK_WIDGET (menu);
677 gboolean viewable = TRUE;
678 GtkWidget *tmp = parent;
682 if (!GTK_WIDGET_MAPPED (tmp))
691 xgrab_shell = parent;
693 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
696 /* We want to receive events generated when we map the menu; unfortunately,
697 * since there is probably already an implicit grab in place from the
698 * button that the user used to pop up the menu, we won't receive then --
699 * in particular, the EnterNotify when the menu pops up under the pointer.
701 * If we are grabbing on a parent menu shell, no problem; just grab on
702 * that menu shell first before popping up the window with owner_events = TRUE.
704 * When grabbing on the menu itself, things get more convuluted - we
705 * we do an explicit grab on a specially created window with
706 * owner_events = TRUE, which we override further down with a grab
707 * on the menu. (We can't grab on the menu until it is mapped; we
708 * probably could just leave the grab on the other window, with a
709 * little reorganization of the code in gtkmenu*).
711 if (xgrab_shell && xgrab_shell != widget)
713 if (popup_grab_on_window (xgrab_shell->window, activate_time))
714 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
718 GdkWindow *transfer_window;
720 xgrab_shell = widget;
721 transfer_window = menu_grab_transfer_window_get (menu);
722 if (popup_grab_on_window (transfer_window, activate_time))
723 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
726 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
728 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
729 * with a stuck up window, we just abort here. Presumably the user will
732 menu_shell->parent_menu_shell = NULL;
733 menu_grab_transfer_window_destroy (menu);
737 menu_shell->active = TRUE;
738 menu_shell->button = button;
740 /* If we are popping up the menu from something other than, a button
741 * press then, as a heuristic, we ignore enter events for the menu
742 * until we get a MOTION_NOTIFY.
745 current_event = gtk_get_current_event ();
748 if ((current_event->type != GDK_BUTTON_PRESS) &&
749 (current_event->type != GDK_ENTER_NOTIFY))
750 menu_shell->ignore_enter = TRUE;
752 gdk_event_free (current_event);
757 gtk_menu_tearoff_bg_copy (menu);
759 gtk_menu_reparent (menu, menu->toplevel, FALSE);
762 menu->parent_menu_item = parent_menu_item;
763 menu->position_func = func;
764 menu->position_func_data = data;
765 menu_shell->activate_time = activate_time;
767 /* We need to show the menu here rather in the init function because
768 * code expects to be able to tell if the menu is onscreen by
769 * looking at the GTK_WIDGET_VISIBLE (menu)
771 gtk_widget_show (GTK_WIDGET (menu));
773 /* Position the menu, possibly changing the size request
775 gtk_menu_position (menu);
777 /* Compute the size of the toplevel and realize it so we
778 * can scroll correctly.
781 GtkRequisition tmp_request;
782 GtkAllocation tmp_allocation = { 0, };
784 gtk_widget_size_request (menu->toplevel, &tmp_request);
786 tmp_allocation.width = tmp_request.width;
787 tmp_allocation.height = tmp_request.height;
789 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
791 gtk_widget_realize (GTK_WIDGET (menu));
794 gtk_menu_scroll_to (menu, menu->scroll_offset);
796 /* Once everything is set up correctly, map the toplevel window on
799 gtk_widget_show (menu->toplevel);
801 if (xgrab_shell == widget)
802 popup_grab_on_window (widget->window, activate_time); /* Should always succeed */
804 gtk_grab_add (GTK_WIDGET (menu));
808 gtk_menu_popdown (GtkMenu *menu)
810 GtkMenuShell *menu_shell;
812 g_return_if_fail (GTK_IS_MENU (menu));
814 menu_shell = GTK_MENU_SHELL (menu);
816 menu_shell->parent_menu_shell = NULL;
817 menu_shell->active = FALSE;
818 menu_shell->ignore_enter = FALSE;
820 gtk_menu_stop_scrolling (menu);
822 gtk_menu_stop_navigating_submenu (menu);
824 if (menu_shell->active_menu_item)
826 if (menu->old_active_menu_item)
827 gtk_widget_unref (menu->old_active_menu_item);
828 menu->old_active_menu_item = menu_shell->active_menu_item;
829 gtk_widget_ref (menu->old_active_menu_item);
832 gtk_menu_shell_deselect (menu_shell);
834 /* The X Grab, if present, will automatically be removed when we hide
836 gtk_widget_hide (menu->toplevel);
841 gdk_window_get_size (menu->tearoff_window->window, &width, &height);
842 gtk_widget_set_usize (menu->tearoff_window,
846 if (GTK_BIN (menu->toplevel)->child)
848 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
852 /* We popped up the menu from the tearoff, so we need to
853 * release the grab - we aren't actually hiding the menu.
855 if (menu_shell->have_xgrab)
857 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
859 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
860 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
864 /* gtk_menu_popdown is called each time a menu item is selected from
865 * a torn off menu. Only scroll back to the saved position if the
866 * non-tearoff menu was popped down.
868 if (!menu->tearoff_active)
869 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
870 menu->tearoff_active = TRUE;
873 gtk_widget_hide (GTK_WIDGET (menu));
875 menu_shell->have_xgrab = FALSE;
876 gtk_grab_remove (GTK_WIDGET (menu));
878 menu_grab_transfer_window_destroy (menu);
882 gtk_menu_get_active (GtkMenu *menu)
887 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
889 if (!menu->old_active_menu_item)
892 children = GTK_MENU_SHELL (menu)->children;
896 child = children->data;
897 children = children->next;
899 if (GTK_BIN (child)->child)
904 menu->old_active_menu_item = child;
905 if (menu->old_active_menu_item)
906 gtk_widget_ref (menu->old_active_menu_item);
909 return menu->old_active_menu_item;
913 gtk_menu_set_active (GtkMenu *menu,
919 g_return_if_fail (GTK_IS_MENU (menu));
921 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
924 child = tmp_list->data;
925 if (GTK_BIN (child)->child)
927 if (menu->old_active_menu_item)
928 gtk_widget_unref (menu->old_active_menu_item);
929 menu->old_active_menu_item = child;
930 gtk_widget_ref (menu->old_active_menu_item);
936 gtk_menu_set_accel_group (GtkMenu *menu,
937 GtkAccelGroup *accel_group)
939 g_return_if_fail (GTK_IS_MENU (menu));
941 if (menu->accel_group != accel_group)
943 if (menu->accel_group)
944 gtk_accel_group_unref (menu->accel_group);
945 menu->accel_group = accel_group;
946 if (menu->accel_group)
947 gtk_accel_group_ref (menu->accel_group);
948 _gtk_menu_refresh_accel_paths (menu, TRUE);
953 gtk_menu_get_accel_group (GtkMenu *menu)
955 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
957 return menu->accel_group;
961 * gtk_menu_set_accel_path
962 * @menu: a valid #GtkMenu
963 * @accel_path: a valid accelerator path
965 * Sets an accelerator path for this menu from which accelerator paths
966 * for its immediate children, its menu items, can be constructed.
967 * The main purpose of this function is to spare the programmer the
968 * inconvenience of having to call gtk_menu_item_set_accel_path() on
969 * each menu item that should support runtime user changable accelerators.
970 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
971 * each menu item of this menu, that contains a label describing its purpose,
972 * automatically gets an accel path assigned. For example, a menu containing
973 * menu items "New" and "Exit", will, after
974 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
975 * has been called, assign its items the accel paths:
976 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
977 * Assigning accel paths to menu items then enables the user to change
978 * their accelerators at runtime. More details about accelerator paths
979 * and their default setups can be found at gtk_accel_map_add_entry().
982 gtk_menu_set_accel_path (GtkMenu *menu,
983 const gchar *accel_path)
985 g_return_if_fail (GTK_IS_MENU (menu));
987 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
989 g_free (menu->accel_path);
990 menu->accel_path = g_strdup (accel_path);
991 if (menu->accel_path)
992 _gtk_menu_refresh_accel_paths (menu, FALSE);
997 gboolean group_changed;
1001 refresh_accel_paths_foreach (GtkWidget *widget,
1004 AccelPropagation *prop = data;
1006 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1007 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1008 prop->menu->accel_path,
1009 prop->menu->accel_group,
1010 prop->group_changed);
1014 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1015 gboolean group_changed)
1017 g_return_if_fail (GTK_IS_MENU (menu));
1019 if (menu->accel_path && menu->accel_group)
1021 AccelPropagation prop;
1024 prop.group_changed = group_changed;
1025 gtk_container_foreach (GTK_CONTAINER (menu),
1026 refresh_accel_paths_foreach,
1032 gtk_menu_reposition (GtkMenu *menu)
1034 g_return_if_fail (GTK_IS_MENU (menu));
1036 if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1037 gtk_menu_position (menu);
1041 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1044 g_return_if_fail (GTK_IS_MENU (menu));
1046 if (adjustment->value != menu->scroll_offset)
1047 gtk_menu_scroll_to (menu, adjustment->value);
1051 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1054 GdkGeometry geometry_hints;
1056 if (!menu->tearoff_window)
1059 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1061 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1062 width += menu->tearoff_scrollbar->requisition.width;
1065 geometry_hints.min_width = width;
1066 geometry_hints.max_width = width;
1068 geometry_hints.min_height = 0;
1069 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1070 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1073 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1077 gtk_menu_update_title (GtkMenu *menu)
1079 if (menu->tearoff_window)
1082 GtkWidget *attach_widget;
1084 title = gtk_menu_get_title (menu);
1087 attach_widget = gtk_menu_get_attach_widget (menu);
1088 if (GTK_IS_MENU_ITEM (attach_widget))
1090 GtkWidget *child = GTK_BIN (attach_widget)->child;
1091 if (GTK_IS_LABEL (child))
1092 title = gtk_label_get_text (GTK_LABEL (child));
1097 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1102 gtk_menu_set_tearoff_state (GtkMenu *menu,
1107 g_return_if_fail (GTK_IS_MENU (menu));
1109 if (menu->torn_off != torn_off)
1111 menu->torn_off = torn_off;
1112 menu->tearoff_active = torn_off;
1116 if (GTK_WIDGET_VISIBLE (menu))
1117 gtk_menu_popdown (menu);
1119 if (!menu->tearoff_window)
1121 menu->tearoff_window = gtk_widget_new (GTK_TYPE_WINDOW,
1122 "type", GTK_WINDOW_TOPLEVEL,
1123 "screen", gtk_widget_get_screen (menu->toplevel),
1124 "app_paintable", TRUE,
1127 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1128 GDK_WINDOW_TYPE_HINT_MENU);
1129 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1130 g_signal_connect (menu->tearoff_window, "destroy",
1131 G_CALLBACK (gtk_widget_destroyed), &menu->tearoff_window);
1132 g_signal_connect (menu->tearoff_window, "event",
1133 G_CALLBACK (gtk_menu_window_event), menu);
1135 gtk_menu_update_title (menu);
1137 gtk_widget_realize (menu->tearoff_window);
1139 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1140 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1142 gdk_window_get_size (GTK_WIDGET (menu)->window, &width, &height);
1143 menu->tearoff_adjustment =
1144 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1146 GTK_WIDGET (menu)->requisition.height,
1150 g_object_connect (GTK_OBJECT (menu->tearoff_adjustment),
1151 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1153 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1155 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1156 menu->tearoff_scrollbar,
1159 if (menu->tearoff_adjustment->upper > height)
1160 gtk_widget_show (menu->tearoff_scrollbar);
1162 gtk_widget_show (menu->tearoff_hbox);
1165 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
1167 gdk_window_get_size (GTK_WIDGET (menu)->window, &width, NULL);
1169 /* Update menu->requisition
1171 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
1173 gtk_menu_set_tearoff_hints (menu, width);
1175 gtk_widget_realize (menu->tearoff_window);
1176 gtk_menu_position (menu);
1178 gtk_widget_show (GTK_WIDGET (menu));
1179 gtk_widget_show (menu->tearoff_window);
1181 gtk_menu_scroll_to (menu, 0);
1186 gtk_widget_hide (menu->tearoff_window);
1187 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1193 * gtk_menu_get_tearoff_state:
1196 * Returns whether the menu is torn off. See
1197 * gtk_menu_set_tearoff_state ().
1199 * Return value: %TRUE if the menu is currently torn off.
1202 gtk_menu_get_tearoff_state (GtkMenu *menu)
1204 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
1206 return menu->torn_off;
1210 * gtk_menu_set_title:
1212 * @title: a string containing the title for the menu.
1214 * Sets the title string for the menu. The title is displayed when the menu
1215 * is shown as a tearoff menu.
1218 gtk_menu_set_title (GtkMenu *menu,
1221 g_return_if_fail (GTK_IS_MENU (menu));
1224 g_object_set_data_full (G_OBJECT (menu), "gtk-menu-title",
1225 g_strdup (title), (GtkDestroyNotify) g_free);
1227 g_object_set_data (G_OBJECT (menu), "gtk-menu-title", NULL);
1229 gtk_menu_update_title (menu);
1230 g_object_notify (G_OBJECT (menu), "tearoff_title");
1234 * gtk_menu_get_title:
1237 * Returns the title of the menu. See gtk_menu_set_title().
1239 * Return value: the title of the menu, or %NULL if the menu has no
1242 G_CONST_RETURN gchar *
1243 gtk_menu_get_title (GtkMenu *menu)
1245 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1247 return gtk_object_get_data (GTK_OBJECT (menu), "gtk-menu-title");
1251 gtk_menu_reorder_child (GtkMenu *menu,
1255 GtkMenuShell *menu_shell;
1256 g_return_if_fail (GTK_IS_MENU (menu));
1257 g_return_if_fail (GTK_IS_MENU_ITEM (child));
1258 menu_shell = GTK_MENU_SHELL (menu);
1259 if (g_list_find (menu_shell->children, child))
1261 menu_shell->children = g_list_remove (menu_shell->children, child);
1262 menu_shell->children = g_list_insert (menu_shell->children, child, position);
1263 if (GTK_WIDGET_VISIBLE (menu_shell))
1264 gtk_widget_queue_resize (GTK_WIDGET (menu_shell));
1269 gtk_menu_style_set (GtkWidget *widget,
1270 GtkStyle *previous_style)
1272 if (GTK_WIDGET_REALIZED (widget))
1274 GtkMenu *menu = GTK_MENU (widget);
1276 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1277 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1278 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1283 gtk_menu_realize (GtkWidget *widget)
1285 GdkWindowAttr attributes;
1286 gint attributes_mask;
1292 g_return_if_fail (GTK_IS_MENU (widget));
1294 menu = GTK_MENU (widget);
1296 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1298 attributes.window_type = GDK_WINDOW_CHILD;
1299 attributes.x = widget->allocation.x;
1300 attributes.y = widget->allocation.y;
1301 attributes.width = widget->allocation.width;
1302 attributes.height = widget->allocation.height;
1303 attributes.wclass = GDK_INPUT_OUTPUT;
1304 attributes.visual = gtk_widget_get_visual (widget);
1305 attributes.colormap = gtk_widget_get_colormap (widget);
1307 attributes.event_mask = gtk_widget_get_events (widget);
1308 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
1309 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
1311 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1312 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1313 gdk_window_set_user_data (widget->window, widget);
1315 border_width = GTK_CONTAINER (widget)->border_width;
1317 attributes.x = border_width + widget->style->xthickness;
1318 attributes.y = border_width + widget->style->ythickness;
1319 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
1320 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
1322 if (menu->upper_arrow_visible)
1324 attributes.y += MENU_SCROLL_ARROW_HEIGHT;
1325 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1327 if (menu->lower_arrow_visible)
1328 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1330 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
1331 gdk_window_set_user_data (menu->view_window, menu);
1335 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness) * 2);
1337 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
1338 gdk_window_set_user_data (menu->bin_window, menu);
1340 children = GTK_MENU_SHELL (menu)->children;
1343 child = children->data;
1344 children = children->next;
1346 gtk_widget_set_parent_window (child, menu->bin_window);
1349 widget->style = gtk_style_attach (widget->style, widget->window);
1350 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1351 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1352 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1354 if (GTK_MENU_SHELL (widget)->active_menu_item)
1355 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
1356 GTK_MENU_SHELL (widget)->active_menu_item);
1358 gdk_window_show (menu->bin_window);
1359 gdk_window_show (menu->view_window);
1362 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
1365 menu_grab_transfer_window_get (GtkMenu *menu)
1367 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1370 GdkWindowAttr attributes;
1371 gint attributes_mask;
1373 attributes.x = -100;
1374 attributes.y = -100;
1375 attributes.width = 10;
1376 attributes.height = 10;
1377 attributes.window_type = GDK_WINDOW_TEMP;
1378 attributes.wclass = GDK_INPUT_ONLY;
1379 attributes.override_redirect = TRUE;
1380 attributes.event_mask = 0;
1382 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
1384 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
1385 &attributes, attributes_mask);
1386 gdk_window_set_user_data (window, menu);
1388 gdk_window_show (window);
1390 g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", window);
1397 menu_grab_transfer_window_destroy (GtkMenu *menu)
1399 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1402 gdk_window_set_user_data (window, NULL);
1403 gdk_window_destroy (window);
1404 g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", NULL);
1409 gtk_menu_unrealize (GtkWidget *widget)
1413 g_return_if_fail (GTK_IS_MENU (widget));
1415 menu = GTK_MENU (widget);
1417 menu_grab_transfer_window_destroy (menu);
1419 gdk_window_set_user_data (menu->view_window, NULL);
1420 gdk_window_destroy (menu->view_window);
1421 menu->view_window = NULL;
1423 gdk_window_set_user_data (menu->bin_window, NULL);
1424 gdk_window_destroy (menu->bin_window);
1425 menu->bin_window = NULL;
1427 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1431 gtk_menu_size_request (GtkWidget *widget,
1432 GtkRequisition *requisition)
1435 GtkMenuShell *menu_shell;
1438 guint max_toggle_size;
1439 guint max_accel_width;
1440 GtkRequisition child_requisition;
1442 g_return_if_fail (GTK_IS_MENU (widget));
1443 g_return_if_fail (requisition != NULL);
1445 menu = GTK_MENU (widget);
1446 menu_shell = GTK_MENU_SHELL (widget);
1448 requisition->width = 0;
1449 requisition->height = 0;
1451 max_toggle_size = 0;
1452 max_accel_width = 0;
1454 children = menu_shell->children;
1457 child = children->data;
1458 children = children->next;
1460 if (GTK_WIDGET_VISIBLE (child))
1464 /* It's important to size_request the child
1465 * before doing the toggle size request, in
1466 * case the toggle size request depends on the size
1467 * request of a child of the child (e.g. for ImageMenuItem)
1470 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
1471 gtk_widget_size_request (child, &child_requisition);
1473 requisition->width = MAX (requisition->width, child_requisition.width);
1474 requisition->height += child_requisition.height;
1476 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
1477 max_toggle_size = MAX (max_toggle_size, toggle_size);
1478 max_accel_width = MAX (max_accel_width, GTK_MENU_ITEM (child)->accelerator_width);
1482 requisition->width += max_toggle_size + max_accel_width;
1483 requisition->width += (GTK_CONTAINER (menu)->border_width +
1484 widget->style->xthickness) * 2;
1485 requisition->height += (GTK_CONTAINER (menu)->border_width +
1486 widget->style->ythickness) * 2;
1488 menu->toggle_size = max_toggle_size;
1490 /* If the requested width was different than the allocated width, we need to change
1491 * the geometry hints for the tear off window so that the window can actually be resized.
1492 * Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
1494 if ((requisition->width != GTK_WIDGET (menu)->allocation.width) && menu->tearoff_active)
1495 gtk_menu_set_tearoff_hints (menu, requisition->width);
1499 gtk_menu_size_allocate (GtkWidget *widget,
1500 GtkAllocation *allocation)
1503 GtkMenuShell *menu_shell;
1505 GtkAllocation child_allocation;
1510 g_return_if_fail (GTK_IS_MENU (widget));
1511 g_return_if_fail (allocation != NULL);
1513 menu = GTK_MENU (widget);
1514 menu_shell = GTK_MENU_SHELL (widget);
1516 widget->allocation = *allocation;
1518 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness;
1519 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness;
1521 width = MAX (1, allocation->width - x * 2);
1522 height = MAX (1, allocation->height - y * 2);
1524 if (menu->upper_arrow_visible && !menu->tearoff_active)
1526 y += MENU_SCROLL_ARROW_HEIGHT;
1527 height -= MENU_SCROLL_ARROW_HEIGHT;
1530 if (menu->lower_arrow_visible && !menu->tearoff_active)
1531 height -= MENU_SCROLL_ARROW_HEIGHT;
1533 if (GTK_WIDGET_REALIZED (widget))
1535 gdk_window_move_resize (widget->window,
1536 allocation->x, allocation->y,
1537 allocation->width, allocation->height);
1539 gdk_window_move_resize (menu->view_window,
1546 if (menu_shell->children)
1548 child_allocation.x = 0;
1549 child_allocation.y = 0;
1550 child_allocation.width = width;
1552 children = menu_shell->children;
1555 child = children->data;
1556 children = children->next;
1558 if (GTK_WIDGET_VISIBLE (child))
1560 GtkRequisition child_requisition;
1561 gtk_widget_get_child_requisition (child, &child_requisition);
1563 child_allocation.height = child_requisition.height;
1565 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
1567 gtk_widget_size_allocate (child, &child_allocation);
1568 gtk_widget_queue_draw (child);
1570 child_allocation.y += child_allocation.height;
1574 /* Resize the item window */
1575 if (GTK_WIDGET_REALIZED (widget))
1577 gdk_window_resize (menu->bin_window,
1578 child_allocation.width,
1579 child_allocation.y);
1583 if (menu->tearoff_active)
1585 if (allocation->height >= widget->requisition.height)
1587 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1589 gtk_widget_hide (menu->tearoff_scrollbar);
1590 gtk_menu_set_tearoff_hints (menu, allocation->width);
1591 gtk_widget_set_usize (menu->tearoff_window, -1, allocation->height);
1593 gtk_menu_scroll_to (menu, 0);
1598 menu->tearoff_adjustment->upper = widget->requisition.height;
1599 menu->tearoff_adjustment->page_size = allocation->height;
1601 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
1602 menu->tearoff_adjustment->upper)
1605 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
1608 gtk_menu_scroll_to (menu, value);
1611 gtk_adjustment_changed (menu->tearoff_adjustment);
1613 if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1615 gtk_widget_show (menu->tearoff_scrollbar);
1616 gtk_menu_set_tearoff_hints (menu, allocation->width);
1617 gtk_widget_set_usize (menu->tearoff_window, -1, allocation->height);
1625 gtk_menu_paint (GtkWidget *widget,
1626 GdkEventExpose *event)
1630 gint border_x, border_y;
1632 g_return_if_fail (GTK_IS_MENU (widget));
1634 menu = GTK_MENU (widget);
1636 border_x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness;
1637 border_y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness;
1638 gdk_window_get_size (widget->window, &width, &height);
1640 if (event->window == widget->window)
1642 gtk_paint_box (widget->style,
1646 NULL, widget, "menu",
1648 if (menu->upper_arrow_visible && !menu->tearoff_active)
1650 gtk_paint_box (widget->style,
1652 menu->upper_arrow_prelight ?
1653 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1655 NULL, widget, "menu",
1659 MENU_SCROLL_ARROW_HEIGHT);
1661 gtk_paint_arrow (widget->style,
1663 menu->upper_arrow_prelight ?
1664 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1666 NULL, widget, "menu",
1669 width / 2 - MENU_SCROLL_ARROW_HEIGHT / 2 + 1,
1671 MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2,
1672 MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2);
1675 if (menu->lower_arrow_visible && !menu->tearoff_active)
1677 gtk_paint_box (widget->style,
1679 menu->lower_arrow_prelight ?
1680 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1682 NULL, widget, "menu",
1684 height - border_y - MENU_SCROLL_ARROW_HEIGHT + 1,
1686 MENU_SCROLL_ARROW_HEIGHT);
1688 gtk_paint_arrow (widget->style,
1690 menu->lower_arrow_prelight ?
1691 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1693 NULL, widget, "menu",
1696 width / 2 - MENU_SCROLL_ARROW_HEIGHT / 2 + 1,
1697 height - MENU_SCROLL_ARROW_HEIGHT + 1,
1698 MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2,
1699 MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2);
1705 gtk_menu_expose (GtkWidget *widget,
1706 GdkEventExpose *event)
1708 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
1709 g_return_val_if_fail (event != NULL, FALSE);
1711 if (GTK_WIDGET_DRAWABLE (widget))
1713 gtk_menu_paint (widget, event);
1715 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
1722 gtk_menu_show (GtkWidget *widget)
1724 GtkMenu *menu = GTK_MENU (widget);
1726 _gtk_menu_refresh_accel_paths (menu, FALSE);
1728 GTK_WIDGET_CLASS (parent_class)->show (widget);
1732 gtk_menu_key_press (GtkWidget *widget,
1735 GtkMenuShell *menu_shell;
1737 gboolean delete = FALSE;
1738 gboolean can_change_accels;
1739 gchar *accel = NULL;
1740 guint accel_key, accel_mods;
1741 GdkModifierType consumed_modifiers;
1742 GdkDisplay *display;
1744 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
1745 g_return_val_if_fail (event != NULL, FALSE);
1747 menu_shell = GTK_MENU_SHELL (widget);
1748 menu = GTK_MENU (widget);
1750 gtk_menu_stop_navigating_submenu (menu);
1752 if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
1755 display = gtk_widget_get_display (widget);
1757 g_object_get (G_OBJECT (gtk_widget_get_settings (widget)),
1758 "gtk-menu-bar-accel", &accel,
1759 "gtk-can-change-accels", &can_change_accels,
1765 GdkModifierType mods = 0;
1766 gboolean handled = FALSE;
1768 gtk_accelerator_parse (accel, &keyval, &mods);
1771 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
1773 /* FIXME this is wrong, needs to be in the global accel resolution
1774 * thing, to properly consider i18n etc., but that probably requires
1775 * AccelGroup changes etc.
1777 if (event->keyval == keyval &&
1778 (mods & event->state) == mods)
1780 gtk_signal_emit_by_name (GTK_OBJECT (menu), "cancel");
1789 switch (event->keyval)
1800 /* Figure out what modifiers went into determining the key symbol */
1801 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
1802 event->hardware_keycode, event->state, event->group,
1803 NULL, NULL, NULL, &consumed_modifiers);
1805 accel_key = gdk_keyval_to_lower (event->keyval);
1806 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
1808 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
1809 * We re-upper case when we match against the keyval, but display and save in caseless form.
1811 if (accel_key != event->keyval)
1812 accel_mods |= GDK_SHIFT_MASK;
1814 /* Modify the accelerators */
1815 if (can_change_accels &&
1816 menu_shell->active_menu_item &&
1817 GTK_BIN (menu_shell->active_menu_item)->child && /* no seperators */
1818 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
1819 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
1821 GtkWidget *menu_item = menu_shell->active_menu_item;
1822 gboolean locked, replace_accels = TRUE;
1825 path = _gtk_widget_get_accel_path (menu_item, &locked);
1826 if (!path || locked)
1828 /* can't change accelerators on menu_items without paths
1829 * (basically, those items are accelerator-locked).
1831 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
1832 gdk_display_beep (display);
1838 /* For the keys that act to delete the current setting, we delete
1839 * the current setting if there is one, otherwise, we set the
1840 * key as the accelerator.
1846 if (gtk_accel_map_lookup_entry (path, &key) &&
1847 (key.accel_key || key.accel_mods))
1853 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
1857 /* we failed, probably because this key is in use and
1860 /* g_print("failed to change\n"); */
1861 gdk_display_beep (display);
1870 gtk_menu_motion_notify (GtkWidget *widget,
1871 GdkEventMotion *event)
1873 GtkWidget *menu_item;
1875 GtkMenuShell *menu_shell;
1877 gboolean need_enter;
1879 if (GTK_IS_MENU (widget))
1880 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
1882 /* We received the event for one of two reasons:
1884 * a) We are the active menu, and did gtk_grab_add()
1885 * b) The widget is a child of ours, and the event was propagated
1887 * Since for computation of navigation regions, we want the menu which
1888 * is the parent of the menu item, for a), we need to find that menu,
1889 * which may be different from 'widget'.
1891 menu_item = gtk_get_event_widget ((GdkEvent*) event);
1892 if (!menu_item || !GTK_IS_MENU_ITEM (menu_item) ||
1893 !_gtk_menu_item_is_selectable (menu_item) ||
1894 !GTK_IS_MENU (menu_item->parent))
1897 menu_shell = GTK_MENU_SHELL (menu_item->parent);
1898 menu = GTK_MENU (menu_shell);
1900 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
1902 /* Check to see if we are within an active submenu's navigation region
1904 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
1909 /* The menu is now sensitive to enter events on its items, but
1910 * was previously sensitive. So we fake an enter event.
1914 menu_shell->ignore_enter = FALSE;
1916 gdk_window_get_size (event->window, &width, &height);
1917 if (event->x >= 0 && event->x < width &&
1918 event->y >= 0 && event->y < height)
1920 GdkEvent send_event;
1922 memset (&send_event, 0, sizeof (send_event));
1923 send_event.crossing.type = GDK_ENTER_NOTIFY;
1924 send_event.crossing.window = event->window;
1925 send_event.crossing.time = event->time;
1926 send_event.crossing.send_event = TRUE;
1927 send_event.crossing.x_root = event->x_root;
1928 send_event.crossing.y_root = event->y_root;
1929 send_event.crossing.x = event->x;
1930 send_event.crossing.y = event->y;
1932 /* We send the event to 'widget', the currently active menu,
1933 * instead of 'menu', the menu that the pointer is in. This
1934 * will ensure that the event will be ignored unless the
1935 * menuitem is a child of the active menu or some parent
1936 * menu of the active menu.
1938 return gtk_widget_event (widget, &send_event);
1946 gtk_menu_scroll_timeout (gpointer data)
1951 gint view_width, view_height;
1953 GDK_THREADS_ENTER ();
1955 menu = GTK_MENU (data);
1956 widget = GTK_WIDGET (menu);
1958 offset = menu->scroll_offset + menu->scroll_step;
1960 /* If we scroll upward and the non-visible top part
1961 * is smaller than the scroll arrow it would be
1962 * pretty stupid to show the arrow and taking more
1963 * screen space than just scrolling to the top.
1965 if ((menu->scroll_step < 0) && (offset < MENU_SCROLL_ARROW_HEIGHT))
1968 /* Don't scroll over the top if we weren't before: */
1969 if ((menu->scroll_offset >= 0) && (offset < 0))
1972 gdk_window_get_size (widget->window, &view_width, &view_height);
1974 /* Don't scroll past the bottom if we weren't before: */
1975 if (menu->scroll_offset > 0)
1976 view_height -= MENU_SCROLL_ARROW_HEIGHT;
1978 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
1979 (offset + view_height > widget->requisition.height))
1980 offset = widget->requisition.height - view_height;
1982 gtk_menu_scroll_to (menu, offset);
1984 GDK_THREADS_LEAVE ();
1990 gtk_menu_handle_scrolling (GtkMenu *menu, gboolean enter)
1992 GtkMenuShell *menu_shell;
1998 gboolean scroll_fast = FALSE;
2000 menu_shell = GTK_MENU_SHELL (menu);
2002 gdk_window_get_pointer (GTK_WIDGET (menu)->window, &x, &y, NULL);
2003 gdk_window_get_size (GTK_WIDGET (menu)->window, &width, &height);
2005 border = GTK_CONTAINER (menu)->border_width + GTK_WIDGET (menu)->style->ythickness;
2007 if (menu->upper_arrow_visible && !menu->tearoff_active)
2012 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2015 if ((x >= rect.x) && (x < rect.x + rect.width) &&
2016 (y >= rect.y) && (y < rect.y + rect.height))
2019 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
2022 if (enter && in_arrow &&
2023 (!menu->upper_arrow_prelight || menu->scroll_fast != scroll_fast))
2025 menu->upper_arrow_prelight = TRUE;
2026 menu->scroll_fast = scroll_fast;
2027 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2029 /* Deselect the active item so that any submenus are poped down */
2030 gtk_menu_shell_deselect (menu_shell);
2032 gtk_menu_stop_scrolling (menu);
2033 menu->scroll_step = -MENU_SCROLL_STEP;
2034 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2035 gtk_menu_scroll_timeout,
2038 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
2040 menu->upper_arrow_prelight = FALSE;
2041 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2043 gtk_menu_stop_scrolling (menu);
2047 if (menu->lower_arrow_visible && !menu->tearoff_active)
2050 rect.y = height - border - MENU_SCROLL_ARROW_HEIGHT;
2052 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2055 if ((x >= rect.x) && (x < rect.x + rect.width) &&
2056 (y >= rect.y) && (y < rect.y + rect.height))
2059 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
2062 if (enter && in_arrow &&
2063 (!menu->lower_arrow_prelight || menu->scroll_fast != scroll_fast))
2065 menu->lower_arrow_prelight = TRUE;
2066 menu->scroll_fast = scroll_fast;
2067 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2069 /* Deselect the active item so that any submenus are poped down */
2070 gtk_menu_shell_deselect (menu_shell);
2072 gtk_menu_stop_scrolling (menu);
2073 menu->scroll_step = MENU_SCROLL_STEP;
2074 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2075 gtk_menu_scroll_timeout,
2078 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
2080 menu->lower_arrow_prelight = FALSE;
2081 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2083 gtk_menu_stop_scrolling (menu);
2089 gtk_menu_enter_notify (GtkWidget *widget,
2090 GdkEventCrossing *event)
2092 GtkWidget *menu_item;
2094 if (widget && GTK_IS_MENU (widget))
2095 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2097 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
2098 * will not correspond to the event widget's parent. Check to see
2099 * if we are in the parent's navigation region.
2101 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2102 if (menu_item && GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
2103 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent), event->x_root, event->y_root))
2106 return GTK_WIDGET_CLASS (parent_class)->enter_notify_event (widget, event);
2110 gtk_menu_leave_notify (GtkWidget *widget,
2111 GdkEventCrossing *event)
2113 GtkMenuShell *menu_shell;
2115 GtkMenuItem *menu_item;
2116 GtkWidget *event_widget;
2118 menu = GTK_MENU (widget);
2119 menu_shell = GTK_MENU_SHELL (widget);
2121 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2124 gtk_menu_handle_scrolling (menu, FALSE);
2126 event_widget = gtk_get_event_widget ((GdkEvent*) event);
2128 if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
2131 menu_item = GTK_MENU_ITEM (event_widget);
2133 /* Here we check to see if we're leaving an active menu item with a submenu,
2134 * in which case we enter submenu navigation mode.
2136 if (menu_shell->active_menu_item != NULL
2137 && menu_item->submenu != NULL
2138 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
2140 if (GTK_MENU_SHELL (menu_item->submenu)->active)
2142 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
2147 return GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event);
2151 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
2153 if (menu->navigation_region)
2155 gdk_region_destroy (menu->navigation_region);
2156 menu->navigation_region = NULL;
2159 if (menu->navigation_timeout)
2161 gtk_timeout_remove (menu->navigation_timeout);
2162 menu->navigation_timeout = 0;
2166 /* When the timeout is elapsed, the navigation region is destroyed
2167 * and the menuitem under the pointer (if any) is selected.
2170 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
2172 GtkMenu *menu = user_data;
2173 GdkWindow *child_window;
2175 GDK_THREADS_ENTER ();
2177 gtk_menu_stop_navigating_submenu (menu);
2179 if (GTK_WIDGET_REALIZED (menu))
2181 child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
2185 GdkEventCrossing send_event;
2187 memset (&send_event, 0, sizeof (send_event));
2188 send_event.window = child_window;
2189 send_event.type = GDK_ENTER_NOTIFY;
2190 send_event.time = GDK_CURRENT_TIME; /* Bogus */
2191 send_event.send_event = TRUE;
2193 GTK_WIDGET_CLASS (parent_class)->enter_notify_event (GTK_WIDGET (menu), &send_event);
2197 GDK_THREADS_LEAVE ();
2203 gtk_menu_navigating_submenu (GtkMenu *menu,
2207 if (menu->navigation_region)
2209 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
2213 gtk_menu_stop_navigating_submenu (menu);
2221 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
2222 GtkMenuItem *menu_item,
2223 GdkEventCrossing *event)
2225 gint submenu_left = 0;
2226 gint submenu_right = 0;
2227 gint submenu_top = 0;
2228 gint submenu_bottom = 0;
2232 GtkWidget *event_widget;
2234 g_return_if_fail (menu_item->submenu != NULL);
2235 g_return_if_fail (event != NULL);
2237 event_widget = gtk_get_event_widget ((GdkEvent*) event);
2239 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
2240 gdk_window_get_size (menu_item->submenu->window, &width, &height);
2241 submenu_right = submenu_left + width;
2242 submenu_bottom = submenu_top + height;
2244 gdk_window_get_size (event_widget->window, &width, &height);
2246 if (event->x >= 0 && event->x < width)
2248 /* Set navigation region */
2249 /* We fudge/give a little padding in case the user
2250 * ``misses the vertex'' of the triangle/is off by a pixel or two.
2252 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
2253 point[0].x = event->x_root - SUBMENU_NAV_REGION_PADDING;
2255 point[0].x = event->x_root + SUBMENU_NAV_REGION_PADDING;
2257 /* Exiting the top or bottom? */
2260 point[0].y = event->y_root + 1;
2261 point[1].y = submenu_top;
2263 if (point[0].y <= point[1].y)
2268 point[0].y = event->y_root;
2269 point[1].y = submenu_bottom;
2271 if (point[0].y >= point[1].y)
2275 /* Submenu is to the left or right? */
2276 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
2277 point[1].x = submenu_left; /* right */
2279 point[1].x = submenu_right; /* left */
2281 point[2].x = point[1].x;
2282 point[2].y = point[0].y;
2284 gtk_menu_stop_navigating_submenu (menu);
2286 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
2288 menu->navigation_timeout = gtk_timeout_add (SUBMENU_NAV_HYSTERESIS_TIMEOUT,
2289 gtk_menu_stop_navigating_submenu_cb, menu);
2294 gtk_menu_deactivate (GtkMenuShell *menu_shell)
2298 g_return_if_fail (GTK_IS_MENU (menu_shell));
2300 parent = menu_shell->parent_menu_shell;
2302 menu_shell->activate_time = 0;
2303 gtk_menu_popdown (GTK_MENU (menu_shell));
2306 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
2310 gtk_menu_position (GtkMenu *menu)
2313 GtkRequisition requisition;
2319 GdkRectangle monitor;
2322 g_return_if_fail (GTK_IS_MENU (menu));
2324 widget = GTK_WIDGET (menu);
2326 gdk_window_get_pointer (gtk_widget_get_root_window (widget),
2329 screen = gtk_widget_get_screen (widget);
2330 monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
2331 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2333 /* We need the requisition to figure out the right place to
2334 * popup the menu. In fact, we always need to ask here, since
2335 * if a size_request was queued while we weren't popped up,
2336 * the requisition won't have been recomputed yet.
2338 gtk_widget_size_request (widget, &requisition);
2342 if (menu->position_func)
2343 (* menu->position_func) (menu, &x, &y, &push_in, menu->position_func_data);
2346 x = CLAMP (x - 2, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
2347 y = CLAMP (y - 2, monitor.y, MAX (monitor.y, monitor.y + monitor.height - requisition.height));
2354 menu_height = GTK_WIDGET (menu)->requisition.height;
2356 if (y + menu_height > monitor.y + monitor.height)
2358 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
2359 y = (monitor.y + monitor.height) - menu_height;
2369 /* FIXME: should this be done in the various position_funcs ? */
2370 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
2372 if (y + requisition.height > monitor.y + monitor.height)
2373 requisition.height = (monitor.y + monitor.height) - y;
2378 requisition.height -= -y;
2382 if (scroll_offset > 0)
2383 scroll_offset += MENU_SCROLL_ARROW_HEIGHT;
2385 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
2387 gtk_widget_set_usize (GTK_MENU_SHELL (menu)->active ?
2388 menu->toplevel : menu->tearoff_hbox,
2389 -1, requisition.height);
2390 menu->scroll_offset = scroll_offset;
2394 gtk_menu_stop_scrolling (GtkMenu *menu)
2396 if (menu->timeout_id)
2398 g_source_remove (menu->timeout_id);
2399 menu->timeout_id = 0;
2405 gtk_menu_scroll_to (GtkMenu *menu,
2410 gint view_width, view_height;
2412 gboolean last_visible;
2415 widget = GTK_WIDGET (menu);
2417 if (menu->tearoff_active &&
2418 menu->tearoff_adjustment &&
2419 (menu->tearoff_adjustment->value != offset))
2421 menu->tearoff_adjustment->value = offset;
2422 gtk_adjustment_value_changed (menu->tearoff_adjustment);
2425 /* Scroll the menu: */
2426 gdk_window_move (menu->bin_window, 0, -offset);
2428 /* Move/resize the viewport according to arrows: */
2429 gdk_window_get_size (widget->window, &view_width, &view_height);
2431 border_width = GTK_CONTAINER (menu)->border_width;
2432 view_width -= (border_width + widget->style->xthickness) * 2;
2433 view_height -= (border_width + widget->style->ythickness) * 2;
2434 menu_height = widget->requisition.height - (border_width + widget->style->ythickness) * 2;
2436 x = border_width + widget->style->xthickness;
2437 y = border_width + widget->style->ythickness;
2439 if (!menu->tearoff_active)
2441 last_visible = menu->upper_arrow_visible;
2442 menu->upper_arrow_visible = (offset > 0);
2444 if (menu->upper_arrow_visible)
2445 view_height -= MENU_SCROLL_ARROW_HEIGHT;
2447 if ( (last_visible != menu->upper_arrow_visible) &&
2448 !menu->upper_arrow_visible)
2450 menu->upper_arrow_prelight = FALSE;
2452 /* If we hid the upper arrow, possibly remove timeout */
2453 if (menu->scroll_step < 0)
2454 gtk_menu_stop_scrolling (menu);
2457 last_visible = menu->lower_arrow_visible;
2458 menu->lower_arrow_visible = (view_height + offset < menu_height);
2460 if (menu->lower_arrow_visible)
2461 view_height -= MENU_SCROLL_ARROW_HEIGHT;
2463 if ( (last_visible != menu->lower_arrow_visible) &&
2464 !menu->lower_arrow_visible)
2466 menu->lower_arrow_prelight = FALSE;
2468 /* If we hid the lower arrow, possibly remove timeout */
2469 if (menu->scroll_step > 0)
2470 gtk_menu_stop_scrolling (menu);
2473 if (menu->upper_arrow_visible)
2474 y += MENU_SCROLL_ARROW_HEIGHT;
2477 gdk_window_move_resize (menu->view_window,
2483 menu->scroll_offset = offset;
2487 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
2488 GtkWidget *menu_item)
2493 GtkRequisition child_requisition;
2494 gint child_offset, child_height;
2498 gboolean last_child = 0;
2500 menu = GTK_MENU (menu_shell);
2502 /* We need to check if the selected item fully visible.
2503 * If not we need to scroll the menu so that it becomes fully
2510 children = menu_shell->children;
2513 child = children->data;
2514 children = children->next;
2516 if (GTK_WIDGET_VISIBLE (child))
2518 gtk_widget_size_request (child, &child_requisition);
2519 child_offset += child_height;
2520 child_height = child_requisition.height;
2523 if (child == menu_item)
2525 last_child = (children == NULL);
2530 if (child == menu_item)
2532 y = menu->scroll_offset;
2533 gdk_window_get_size (GTK_WIDGET (menu)->window, &width, &height);
2535 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness;
2537 if (child_offset + child_height <= y)
2539 /* Ignore the enter event we might get if the pointer is on the menu
2541 menu_shell->ignore_enter = TRUE;
2542 gtk_menu_scroll_to (menu, child_offset);
2547 if (menu->upper_arrow_visible && !menu->tearoff_active)
2548 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2549 if (menu->lower_arrow_visible && !menu->tearoff_active)
2550 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2552 if (child_offset >= y + height - arrow_height)
2555 if (!last_child && !menu->tearoff_active)
2556 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2558 y = child_offset + child_height - height + arrow_height;
2559 if ((y > 0) && !menu->tearoff_active)
2561 /* Need upper arrow */
2562 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2563 y = child_offset + child_height - height + arrow_height;
2565 /* Ignore the enter event we might get if the pointer is on the menu
2567 menu_shell->ignore_enter = TRUE;
2568 gtk_menu_scroll_to (menu, y);
2576 gtk_menu_select_item (GtkMenuShell *menu_shell,
2577 GtkWidget *menu_item)
2579 GtkMenu *menu = GTK_MENU (menu_shell);
2581 if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
2582 gtk_menu_scroll_item_visible (menu_shell, menu_item);
2584 GTK_MENU_SHELL_CLASS (parent_class)->select_item (menu_shell, menu_item);
2588 /* Reparent the menu, taking care of the refcounting
2590 * If unrealize is true we force a unrealize while reparenting the parent.
2591 * This can help eliminate flicker in some cases.
2593 * What happens is that when the menu is unrealized and then re-realized,
2594 * the allocations are as follows:
2596 * parent - 1x1 at (0,0)
2597 * child1 - 100x20 at (0,0)
2598 * child2 - 100x20 at (0,20)
2599 * child3 - 100x20 at (0,40)
2601 * That is, the parent is small but the children are full sized. Then,
2602 * when the queued_resize gets processed, the parent gets resized to
2605 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
2606 * contains the following logic:
2608 * - if a move or resize operation on a window would change the clip
2609 * region on the children, then before the window is resized
2610 * the background for children is temporarily set to None, the
2611 * move/resize done, and the background for the children restored.
2613 * So, at the point where the parent is resized to final size, the
2614 * background for the children is temporarily None, and thus they
2615 * are not cleared to the background color and the previous background
2616 * (the image of the menu) is left in place.
2619 gtk_menu_reparent (GtkMenu *menu,
2620 GtkWidget *new_parent,
2623 GtkObject *object = GTK_OBJECT (menu);
2624 GtkWidget *widget = GTK_WIDGET (menu);
2625 gboolean was_floating = GTK_OBJECT_FLOATING (object);
2627 gtk_object_ref (object);
2628 gtk_object_sink (object);
2632 gtk_object_ref (object);
2633 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
2634 gtk_container_add (GTK_CONTAINER (new_parent), widget);
2635 gtk_object_unref (object);
2638 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
2641 GTK_OBJECT_SET_FLAGS (object, GTK_FLOATING);
2643 gtk_object_unref (object);
2647 gtk_menu_show_all (GtkWidget *widget)
2649 g_return_if_fail (GTK_IS_MENU (widget));
2651 /* Show children, but not self. */
2652 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
2657 gtk_menu_hide_all (GtkWidget *widget)
2659 g_return_if_fail (GTK_IS_MENU (widget));
2661 /* Hide children, but not self. */
2662 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
2666 * gtk_menu_set_screen:
2667 * @menu: a #GtkMenu.
2668 * @screen: a #GtkScreen.
2670 * Sets the #GtkScreen on which the GtkMenu will be displayed.
2671 * This function can only be called before @menu is realized.
2674 gtk_menu_set_screen (GtkMenu *menu,
2677 g_return_if_fail (GTK_IS_MENU (menu));
2678 gtk_window_set_screen (GTK_WINDOW (menu->toplevel),
2680 g_object_set_data (G_OBJECT (menu), "gtk-menu-explicit-screen", screen);