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 #define GTK_MENU_INTERNALS
30 #include "gdk/gdkkeysyms.h"
31 #include "gtkaccellabel.h"
32 #include "gtkaccelmap.h"
33 #include "gtkbindings.h"
34 #include "gtkcheckmenuitem.h"
35 #include <gobject/gvaluecollector.h>
37 #include "gtkmarshalers.h"
39 #include "gtktearoffmenuitem.h"
40 #include "gtkwindow.h"
42 #include "gtkvscrollbar.h"
43 #include "gtksettings.h"
44 #include "gtkprivate.h"
49 #define DEFAULT_POPUP_DELAY 225
50 #define DEFAULT_POPDOWN_DELAY 1000
52 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
53 * extends below the submenu
56 #define MENU_SCROLL_STEP1 8
57 #define MENU_SCROLL_STEP2 15
58 #define MENU_SCROLL_FAST_ZONE 8
59 #define MENU_SCROLL_TIMEOUT1 50
60 #define MENU_SCROLL_TIMEOUT2 20
62 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
63 #define ATTACHED_MENUS "gtk-attached-menus"
65 typedef struct _GtkMenuAttachData GtkMenuAttachData;
66 typedef struct _GtkMenuPrivate GtkMenuPrivate;
68 struct _GtkMenuAttachData
70 GtkWidget *attach_widget;
71 GtkMenuDetachFunc detacher;
74 struct _GtkMenuPrivate
76 gboolean seen_item_enter;
78 gboolean have_position;
82 /* info used for the table */
88 /* Cached layout information */
96 GtkStateType lower_arrow_state;
97 GtkStateType upper_arrow_state;
99 gboolean ignore_button_release;
100 gboolean initially_pushed_in;
109 gint effective_left_attach;
110 gint effective_right_attach;
111 gint effective_top_attach;
112 gint effective_bottom_attach;
133 CHILD_PROP_LEFT_ATTACH,
134 CHILD_PROP_RIGHT_ATTACH,
135 CHILD_PROP_TOP_ATTACH,
136 CHILD_PROP_BOTTOM_ATTACH
139 static void gtk_menu_set_property (GObject *object,
143 static void gtk_menu_get_property (GObject *object,
147 static void gtk_menu_set_child_property(GtkContainer *container,
152 static void gtk_menu_get_child_property(GtkContainer *container,
157 static void gtk_menu_destroy (GtkObject *object);
158 static void gtk_menu_realize (GtkWidget *widget);
159 static void gtk_menu_unrealize (GtkWidget *widget);
160 static void gtk_menu_size_request (GtkWidget *widget,
161 GtkRequisition *requisition);
162 static void gtk_menu_size_allocate (GtkWidget *widget,
163 GtkAllocation *allocation);
164 static void gtk_menu_paint (GtkWidget *widget,
165 GdkEventExpose *expose);
166 static void gtk_menu_show (GtkWidget *widget);
167 static gboolean gtk_menu_expose (GtkWidget *widget,
168 GdkEventExpose *event);
169 static gboolean gtk_menu_key_press (GtkWidget *widget,
171 static gboolean gtk_menu_scroll (GtkWidget *widget,
172 GdkEventScroll *event);
173 static gboolean gtk_menu_button_press (GtkWidget *widget,
174 GdkEventButton *event);
175 static gboolean gtk_menu_button_release (GtkWidget *widget,
176 GdkEventButton *event);
177 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
178 GdkEventMotion *event);
179 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
180 GdkEventCrossing *event);
181 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
182 GdkEventCrossing *event);
183 static void gtk_menu_scroll_to (GtkMenu *menu,
185 static void gtk_menu_grab_notify (GtkWidget *widget,
186 gboolean was_grabbed);
188 static void gtk_menu_stop_scrolling (GtkMenu *menu);
189 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
190 static gboolean gtk_menu_scroll_timeout (gpointer data);
191 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
192 static void gtk_menu_start_scrolling (GtkMenu *menu);
194 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
195 GtkWidget *menu_item);
196 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
197 GtkWidget *menu_item);
198 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
201 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
203 static void gtk_menu_handle_scrolling (GtkMenu *menu,
208 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
210 static void gtk_menu_style_set (GtkWidget *widget,
211 GtkStyle *previous_style);
212 static gboolean gtk_menu_focus (GtkWidget *widget,
213 GtkDirectionType direction);
214 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
215 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
216 GtkMenuDirectionType direction);
217 static void gtk_menu_real_move_scroll (GtkMenu *menu,
220 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
221 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
222 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
225 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
226 GtkMenuItem *menu_item,
227 GdkEventCrossing *event);
229 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
230 static void gtk_menu_show_all (GtkWidget *widget);
231 static void gtk_menu_hide_all (GtkWidget *widget);
232 static void gtk_menu_position (GtkMenu *menu);
233 static void gtk_menu_reparent (GtkMenu *menu,
234 GtkWidget *new_parent,
236 static void gtk_menu_remove (GtkContainer *menu,
239 static void gtk_menu_update_title (GtkMenu *menu);
241 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
242 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
244 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
246 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
247 gboolean group_changed);
249 static const gchar attach_data_key[] = "gtk-menu-attach-data";
251 static guint menu_signals[LAST_SIGNAL] = { 0 };
253 static GtkMenuPrivate *
254 gtk_menu_get_private (GtkMenu *menu)
256 return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
259 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
262 menu_queue_resize (GtkMenu *menu)
264 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
266 priv->have_layout = FALSE;
267 gtk_widget_queue_resize (GTK_WIDGET (menu));
271 attach_info_free (AttachInfo *info)
273 g_slice_free (AttachInfo, info);
277 get_attach_info (GtkWidget *child)
279 GObject *object = G_OBJECT (child);
280 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
284 ai = g_slice_new0 (AttachInfo);
285 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
286 (GDestroyNotify) attach_info_free);
293 is_grid_attached (AttachInfo *ai)
295 return (ai->left_attach >= 0 &&
296 ai->right_attach >= 0 &&
297 ai->top_attach >= 0 &&
298 ai->bottom_attach >= 0);
302 menu_ensure_layout (GtkMenu *menu)
304 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
306 if (!priv->have_layout)
308 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
312 gint max_right_attach;
313 gint max_bottom_attach;
315 /* Find extents of gridded portion
317 max_right_attach = 1;
318 max_bottom_attach = 0;
320 for (l = menu_shell->children; l; l = l->next)
322 GtkWidget *child = l->data;
323 AttachInfo *ai = get_attach_info (child);
325 if (is_grid_attached (ai))
327 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
328 max_right_attach = MAX (max_right_attach, ai->right_attach);
334 row_occupied = g_malloc0 (max_bottom_attach);
336 for (l = menu_shell->children; l; l = l->next)
338 GtkWidget *child = l->data;
339 AttachInfo *ai = get_attach_info (child);
341 if (is_grid_attached (ai))
345 for (i = ai->top_attach; i < ai->bottom_attach; i++)
346 row_occupied[i] = TRUE;
350 /* Lay non-grid-items out in those rows
353 for (l = menu_shell->children; l; l = l->next)
355 GtkWidget *child = l->data;
356 AttachInfo *ai = get_attach_info (child);
358 if (!is_grid_attached (ai))
360 while (current_row < max_bottom_attach && row_occupied[current_row])
363 ai->effective_left_attach = 0;
364 ai->effective_right_attach = max_right_attach;
365 ai->effective_top_attach = current_row;
366 ai->effective_bottom_attach = current_row + 1;
372 ai->effective_left_attach = ai->left_attach;
373 ai->effective_right_attach = ai->right_attach;
374 ai->effective_top_attach = ai->top_attach;
375 ai->effective_bottom_attach = ai->bottom_attach;
379 g_free (row_occupied);
381 priv->n_rows = MAX (current_row, max_bottom_attach);
382 priv->n_columns = max_right_attach;
383 priv->have_layout = TRUE;
389 gtk_menu_get_n_columns (GtkMenu *menu)
391 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
393 menu_ensure_layout (menu);
395 return priv->n_columns;
399 gtk_menu_get_n_rows (GtkMenu *menu)
401 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
403 menu_ensure_layout (menu);
409 get_effective_child_attach (GtkWidget *child,
415 GtkMenu *menu = GTK_MENU (child->parent);
418 menu_ensure_layout (menu);
420 ai = get_attach_info (child);
423 *l = ai->effective_left_attach;
425 *r = ai->effective_right_attach;
427 *t = ai->effective_top_attach;
429 *b = ai->effective_bottom_attach;
434 gtk_menu_class_init (GtkMenuClass *class)
436 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
437 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
438 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
439 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
440 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
441 GtkBindingSet *binding_set;
443 gobject_class->set_property = gtk_menu_set_property;
444 gobject_class->get_property = gtk_menu_get_property;
446 object_class->destroy = gtk_menu_destroy;
448 widget_class->realize = gtk_menu_realize;
449 widget_class->unrealize = gtk_menu_unrealize;
450 widget_class->size_request = gtk_menu_size_request;
451 widget_class->size_allocate = gtk_menu_size_allocate;
452 widget_class->show = gtk_menu_show;
453 widget_class->expose_event = gtk_menu_expose;
454 widget_class->scroll_event = gtk_menu_scroll;
455 widget_class->key_press_event = gtk_menu_key_press;
456 widget_class->button_press_event = gtk_menu_button_press;
457 widget_class->button_release_event = gtk_menu_button_release;
458 widget_class->motion_notify_event = gtk_menu_motion_notify;
459 widget_class->show_all = gtk_menu_show_all;
460 widget_class->hide_all = gtk_menu_hide_all;
461 widget_class->enter_notify_event = gtk_menu_enter_notify;
462 widget_class->leave_notify_event = gtk_menu_leave_notify;
463 widget_class->style_set = gtk_menu_style_set;
464 widget_class->focus = gtk_menu_focus;
465 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
466 widget_class->grab_notify = gtk_menu_grab_notify;
468 container_class->remove = gtk_menu_remove;
469 container_class->get_child_property = gtk_menu_get_child_property;
470 container_class->set_child_property = gtk_menu_set_child_property;
472 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
473 menu_shell_class->deactivate = gtk_menu_deactivate;
474 menu_shell_class->select_item = gtk_menu_select_item;
475 menu_shell_class->insert = gtk_menu_real_insert;
476 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
477 menu_shell_class->move_current = gtk_menu_move_current;
479 menu_signals[MOVE_SCROLL] =
480 g_signal_new_class_handler (I_("move-scroll"),
481 G_OBJECT_CLASS_TYPE (object_class),
482 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
483 G_CALLBACK (gtk_menu_real_move_scroll),
485 _gtk_marshal_VOID__ENUM,
487 GTK_TYPE_SCROLL_TYPE);
492 * The index of the currently selected menu item, or -1 if no
493 * menu item is selected.
497 g_object_class_install_property (gobject_class,
499 g_param_spec_int ("active",
501 P_("The currently selected menu item"),
503 GTK_PARAM_READWRITE));
506 * GtkMenu:accel-group:
508 * The accel group holding accelerators for the menu.
512 g_object_class_install_property (gobject_class,
514 g_param_spec_object ("accel-group",
516 P_("The accel group holding accelerators for the menu"),
517 GTK_TYPE_ACCEL_GROUP,
518 GTK_PARAM_READWRITE));
521 * GtkMenu:accel-path:
523 * An accel path used to conveniently construct accel paths of child items.
527 g_object_class_install_property (gobject_class,
529 g_param_spec_string ("accel-path",
531 P_("An accel path used to conveniently construct accel paths of child items"),
533 GTK_PARAM_READWRITE));
536 * GtkMenu:attach-widget:
538 * The widget the menu is attached to. Setting this property attaches
539 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
540 * use gtk_menu_attach_to_widget() directly.
544 g_object_class_install_property (gobject_class,
546 g_param_spec_object ("attach-widget",
548 P_("The widget the menu is attached to"),
550 GTK_PARAM_READWRITE));
552 g_object_class_install_property (gobject_class,
554 g_param_spec_string ("tearoff-title",
556 P_("A title that may be displayed by the window manager when this menu is torn-off"),
558 GTK_PARAM_READWRITE));
561 * GtkMenu:tearoff-state:
563 * A boolean that indicates whether the menu is torn-off.
567 g_object_class_install_property (gobject_class,
569 g_param_spec_boolean ("tearoff-state",
571 P_("A boolean that indicates whether the menu is torn-off"),
573 GTK_PARAM_READWRITE));
578 * The monitor the menu will be popped up on.
582 g_object_class_install_property (gobject_class,
584 g_param_spec_int ("monitor",
586 P_("The monitor the menu will be popped up on"),
588 GTK_PARAM_READWRITE));
590 gtk_widget_class_install_style_property (widget_class,
591 g_param_spec_int ("vertical-padding",
592 P_("Vertical Padding"),
593 P_("Extra space at the top and bottom of the menu"),
597 GTK_PARAM_READABLE));
599 gtk_widget_class_install_style_property (widget_class,
600 g_param_spec_int ("horizontal-padding",
601 P_("Horizontal Padding"),
602 P_("Extra space at the left and right edges of the menu"),
606 GTK_PARAM_READABLE));
608 gtk_widget_class_install_style_property (widget_class,
609 g_param_spec_int ("vertical-offset",
610 P_("Vertical Offset"),
611 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
615 GTK_PARAM_READABLE));
617 gtk_widget_class_install_style_property (widget_class,
618 g_param_spec_int ("horizontal-offset",
619 P_("Horizontal Offset"),
620 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
624 GTK_PARAM_READABLE));
626 gtk_widget_class_install_style_property (widget_class,
627 g_param_spec_boolean ("double-arrows",
629 P_("When scrolling, always show both arrows."),
631 GTK_PARAM_READABLE));
634 * GtkMenu:arrow-placement:
636 * Indicates where scroll arrows should be placed.
640 gtk_widget_class_install_style_property (widget_class,
641 g_param_spec_enum ("arrow-placement",
642 P_("Arrow Placement"),
643 P_("Indicates where scroll arrows should be placed"),
644 GTK_TYPE_ARROW_PLACEMENT,
646 GTK_PARAM_READABLE));
648 gtk_container_class_install_child_property (container_class,
649 CHILD_PROP_LEFT_ATTACH,
650 g_param_spec_int ("left-attach",
652 P_("The column number to attach the left side of the child to"),
654 GTK_PARAM_READWRITE));
656 gtk_container_class_install_child_property (container_class,
657 CHILD_PROP_RIGHT_ATTACH,
658 g_param_spec_int ("right-attach",
660 P_("The column number to attach the right side of the child to"),
662 GTK_PARAM_READWRITE));
664 gtk_container_class_install_child_property (container_class,
665 CHILD_PROP_TOP_ATTACH,
666 g_param_spec_int ("top-attach",
668 P_("The row number to attach the top of the child to"),
670 GTK_PARAM_READWRITE));
672 gtk_container_class_install_child_property (container_class,
673 CHILD_PROP_BOTTOM_ATTACH,
674 g_param_spec_int ("bottom-attach",
676 P_("The row number to attach the bottom of the child to"),
678 GTK_PARAM_READWRITE));
681 * GtkMenu::arrow-scaling
683 * Arbitrary constant to scale down the size of the scroll arrow.
687 gtk_widget_class_install_style_property (widget_class,
688 g_param_spec_float ("arrow-scaling",
690 P_("Arbitrary constant to scale down the size of the scroll arrow"),
692 GTK_PARAM_READABLE));
694 binding_set = gtk_binding_set_by_class (class);
695 gtk_binding_entry_add_signal (binding_set,
697 I_("move-current"), 1,
698 GTK_TYPE_MENU_DIRECTION_TYPE,
700 gtk_binding_entry_add_signal (binding_set,
703 GTK_TYPE_MENU_DIRECTION_TYPE,
705 gtk_binding_entry_add_signal (binding_set,
708 GTK_TYPE_MENU_DIRECTION_TYPE,
710 gtk_binding_entry_add_signal (binding_set,
713 GTK_TYPE_MENU_DIRECTION_TYPE,
715 gtk_binding_entry_add_signal (binding_set,
718 GTK_TYPE_MENU_DIRECTION_TYPE,
719 GTK_MENU_DIR_PARENT);
720 gtk_binding_entry_add_signal (binding_set,
723 GTK_TYPE_MENU_DIRECTION_TYPE,
724 GTK_MENU_DIR_PARENT);
725 gtk_binding_entry_add_signal (binding_set,
728 GTK_TYPE_MENU_DIRECTION_TYPE,
730 gtk_binding_entry_add_signal (binding_set,
733 GTK_TYPE_MENU_DIRECTION_TYPE,
735 gtk_binding_entry_add_signal (binding_set,
738 GTK_TYPE_SCROLL_TYPE,
740 gtk_binding_entry_add_signal (binding_set,
743 GTK_TYPE_SCROLL_TYPE,
745 gtk_binding_entry_add_signal (binding_set,
748 GTK_TYPE_SCROLL_TYPE,
750 gtk_binding_entry_add_signal (binding_set,
753 GTK_TYPE_SCROLL_TYPE,
755 gtk_binding_entry_add_signal (binding_set,
758 GTK_TYPE_SCROLL_TYPE,
760 gtk_binding_entry_add_signal (binding_set,
763 GTK_TYPE_SCROLL_TYPE,
765 gtk_binding_entry_add_signal (binding_set,
768 GTK_TYPE_SCROLL_TYPE,
769 GTK_SCROLL_PAGE_DOWN);
770 gtk_binding_entry_add_signal (binding_set,
773 GTK_TYPE_SCROLL_TYPE,
774 GTK_SCROLL_PAGE_DOWN);
776 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
777 P_("Can change accelerators"),
778 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
780 GTK_PARAM_READWRITE));
782 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
783 P_("Delay before submenus appear"),
784 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
788 GTK_PARAM_READWRITE));
790 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
791 P_("Delay before hiding a submenu"),
792 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
795 DEFAULT_POPDOWN_DELAY,
796 GTK_PARAM_READWRITE));
798 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
803 gtk_menu_set_property (GObject *object,
808 GtkMenu *menu = GTK_MENU (object);
813 gtk_menu_set_active (menu, g_value_get_int (value));
815 case PROP_ACCEL_GROUP:
816 gtk_menu_set_accel_group (menu, g_value_get_object (value));
818 case PROP_ACCEL_PATH:
819 gtk_menu_set_accel_path (menu, g_value_get_string (value));
821 case PROP_ATTACH_WIDGET:
825 widget = gtk_menu_get_attach_widget (menu);
827 gtk_menu_detach (menu);
829 widget = (GtkWidget*) g_value_get_object (value);
831 gtk_menu_attach_to_widget (menu, widget, NULL);
834 case PROP_TEAROFF_STATE:
835 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
837 case PROP_TEAROFF_TITLE:
838 gtk_menu_set_title (menu, g_value_get_string (value));
841 gtk_menu_set_monitor (menu, g_value_get_int (value));
844 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
850 gtk_menu_get_property (GObject *object,
855 GtkMenu *menu = GTK_MENU (object);
860 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->children, gtk_menu_get_active (menu)));
862 case PROP_ACCEL_GROUP:
863 g_value_set_object (value, gtk_menu_get_accel_group (menu));
865 case PROP_ACCEL_PATH:
866 g_value_set_string (value, gtk_menu_get_accel_path (menu));
868 case PROP_ATTACH_WIDGET:
869 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
871 case PROP_TEAROFF_STATE:
872 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
874 case PROP_TEAROFF_TITLE:
875 g_value_set_string (value, gtk_menu_get_title (menu));
878 g_value_set_int (value, gtk_menu_get_monitor (menu));
881 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
887 gtk_menu_set_child_property (GtkContainer *container,
893 GtkMenu *menu = GTK_MENU (container);
894 AttachInfo *ai = get_attach_info (child);
898 case CHILD_PROP_LEFT_ATTACH:
899 ai->left_attach = g_value_get_int (value);
901 case CHILD_PROP_RIGHT_ATTACH:
902 ai->right_attach = g_value_get_int (value);
904 case CHILD_PROP_TOP_ATTACH:
905 ai->top_attach = g_value_get_int (value);
907 case CHILD_PROP_BOTTOM_ATTACH:
908 ai->bottom_attach = g_value_get_int (value);
912 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
916 menu_queue_resize (menu);
920 gtk_menu_get_child_property (GtkContainer *container,
926 AttachInfo *ai = get_attach_info (child);
930 case CHILD_PROP_LEFT_ATTACH:
931 g_value_set_int (value, ai->left_attach);
933 case CHILD_PROP_RIGHT_ATTACH:
934 g_value_set_int (value, ai->right_attach);
936 case CHILD_PROP_TOP_ATTACH:
937 g_value_set_int (value, ai->top_attach);
939 case CHILD_PROP_BOTTOM_ATTACH:
940 g_value_set_int (value, ai->bottom_attach);
944 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
950 gtk_menu_window_event (GtkWidget *window,
954 gboolean handled = FALSE;
956 g_object_ref (window);
962 case GDK_KEY_RELEASE:
963 handled = gtk_widget_event (menu, event);
969 g_object_unref (window);
970 g_object_unref (menu);
976 gtk_menu_window_size_request (GtkWidget *window,
977 GtkRequisition *requisition,
980 GtkMenuPrivate *private = gtk_menu_get_private (menu);
982 if (private->have_position)
984 GdkScreen *screen = gtk_widget_get_screen (window);
985 GdkRectangle monitor;
987 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
989 if (private->y + requisition->height > monitor.y + monitor.height)
990 requisition->height = monitor.y + monitor.height - private->y;
992 if (private->y < monitor.y)
993 requisition->height -= monitor.y - private->y;
998 gtk_menu_init (GtkMenu *menu)
1000 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
1002 menu->parent_menu_item = NULL;
1003 menu->old_active_menu_item = NULL;
1004 menu->accel_group = NULL;
1005 menu->position_func = NULL;
1006 menu->position_func_data = NULL;
1007 menu->toggle_size = 0;
1009 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1010 "type", GTK_WINDOW_POPUP,
1013 "signal::event", gtk_menu_window_event, menu,
1014 "signal::size-request", gtk_menu_window_size_request, menu,
1015 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1017 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
1018 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
1020 /* Refloat the menu, so that reference counting for the menu isn't
1021 * affected by it being a child of the toplevel
1023 g_object_force_floating (G_OBJECT (menu));
1024 menu->needs_destruction_ref_count = TRUE;
1026 menu->view_window = NULL;
1027 menu->bin_window = NULL;
1029 menu->scroll_offset = 0;
1030 menu->scroll_step = 0;
1031 menu->timeout_id = 0;
1032 menu->scroll_fast = FALSE;
1034 menu->tearoff_window = NULL;
1035 menu->tearoff_hbox = NULL;
1036 menu->torn_off = FALSE;
1037 menu->tearoff_active = FALSE;
1038 menu->tearoff_adjustment = NULL;
1039 menu->tearoff_scrollbar = NULL;
1041 menu->upper_arrow_visible = FALSE;
1042 menu->lower_arrow_visible = FALSE;
1043 menu->upper_arrow_prelight = FALSE;
1044 menu->lower_arrow_prelight = FALSE;
1046 priv->upper_arrow_state = GTK_STATE_NORMAL;
1047 priv->lower_arrow_state = GTK_STATE_NORMAL;
1049 priv->have_layout = FALSE;
1050 priv->monitor_num = -1;
1054 gtk_menu_destroy (GtkObject *object)
1056 GtkMenu *menu = GTK_MENU (object);
1057 GtkMenuAttachData *data;
1058 GtkMenuPrivate *priv;
1060 gtk_menu_remove_scroll_timeout (menu);
1062 data = g_object_get_data (G_OBJECT (object), attach_data_key);
1064 gtk_menu_detach (menu);
1066 gtk_menu_stop_navigating_submenu (menu);
1068 if (menu->old_active_menu_item)
1070 g_object_unref (menu->old_active_menu_item);
1071 menu->old_active_menu_item = NULL;
1074 /* Add back the reference count for being a child */
1075 if (menu->needs_destruction_ref_count)
1077 menu->needs_destruction_ref_count = FALSE;
1078 g_object_ref (object);
1081 if (menu->accel_group)
1083 g_object_unref (menu->accel_group);
1084 menu->accel_group = NULL;
1088 gtk_widget_destroy (menu->toplevel);
1090 if (menu->tearoff_window)
1091 gtk_widget_destroy (menu->tearoff_window);
1093 priv = gtk_menu_get_private (menu);
1097 g_free (priv->heights);
1098 priv->heights = NULL;
1103 g_free (priv->title);
1107 GTK_OBJECT_CLASS (gtk_menu_parent_class)->destroy (object);
1111 menu_change_screen (GtkMenu *menu,
1112 GdkScreen *new_screen)
1114 GtkMenuPrivate *private = gtk_menu_get_private (menu);
1116 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1118 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1124 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1125 gtk_menu_position (menu);
1128 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1129 private->monitor_num = -1;
1133 attach_widget_screen_changed (GtkWidget *attach_widget,
1134 GdkScreen *previous_screen,
1137 if (gtk_widget_has_screen (attach_widget) &&
1138 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1140 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1145 gtk_menu_attach_to_widget (GtkMenu *menu,
1146 GtkWidget *attach_widget,
1147 GtkMenuDetachFunc detacher)
1149 GtkMenuAttachData *data;
1152 g_return_if_fail (GTK_IS_MENU (menu));
1153 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1155 /* keep this function in sync with gtk_widget_set_parent()
1158 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1161 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1162 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1166 g_object_ref_sink (menu);
1168 data = g_slice_new (GtkMenuAttachData);
1169 data->attach_widget = attach_widget;
1171 g_signal_connect (attach_widget, "screen-changed",
1172 G_CALLBACK (attach_widget_screen_changed), menu);
1173 attach_widget_screen_changed (attach_widget, NULL, menu);
1175 data->detacher = detacher;
1176 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1177 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1178 if (!g_list_find (list, menu))
1180 list = g_list_prepend (list, menu);
1182 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1183 (GDestroyNotify) g_list_free);
1185 if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
1186 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1188 /* we don't need to set the style here, since
1189 * we are a toplevel widget.
1192 /* Fallback title for menu comes from attach widget */
1193 gtk_menu_update_title (menu);
1197 gtk_menu_get_attach_widget (GtkMenu *menu)
1199 GtkMenuAttachData *data;
1201 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1203 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1205 return data->attach_widget;
1210 gtk_menu_detach (GtkMenu *menu)
1212 GtkMenuAttachData *data;
1215 g_return_if_fail (GTK_IS_MENU (menu));
1217 /* keep this function in sync with gtk_widget_unparent()
1219 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1222 g_warning ("gtk_menu_detach(): menu is not attached");
1225 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1227 g_signal_handlers_disconnect_by_func (data->attach_widget,
1228 (gpointer) attach_widget_screen_changed,
1232 data->detacher (data->attach_widget, menu);
1233 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1234 list = g_list_remove (list, menu);
1236 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1237 (GDestroyNotify) g_list_free);
1239 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1241 if (GTK_WIDGET_REALIZED (menu))
1242 gtk_widget_unrealize (GTK_WIDGET (menu));
1244 g_slice_free (GtkMenuAttachData, data);
1246 /* Fallback title for menu comes from attach widget */
1247 gtk_menu_update_title (menu);
1249 g_object_unref (menu);
1253 gtk_menu_remove (GtkContainer *container,
1256 GtkMenu *menu = GTK_MENU (container);
1258 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1260 /* Clear out old_active_menu_item if it matches the item we are removing
1262 if (menu->old_active_menu_item == widget)
1264 g_object_unref (menu->old_active_menu_item);
1265 menu->old_active_menu_item = NULL;
1268 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1269 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1271 menu_queue_resize (menu);
1277 return g_object_new (GTK_TYPE_MENU, NULL);
1281 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1285 GtkMenu *menu = GTK_MENU (menu_shell);
1286 AttachInfo *ai = get_attach_info (child);
1288 ai->left_attach = -1;
1289 ai->right_attach = -1;
1290 ai->top_attach = -1;
1291 ai->bottom_attach = -1;
1293 if (GTK_WIDGET_REALIZED (menu_shell))
1294 gtk_widget_set_parent_window (child, menu->bin_window);
1296 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1298 menu_queue_resize (menu);
1302 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1307 widget = GTK_WIDGET (menu);
1313 GdkGCValues gc_values;
1315 menu->tearoff_active = FALSE;
1316 menu->saved_scroll_offset = menu->scroll_offset;
1318 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
1319 gc = gdk_gc_new_with_values (widget->window,
1320 &gc_values, GDK_GC_SUBWINDOW);
1322 gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1324 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
1329 gdk_draw_drawable (pixmap, gc,
1330 menu->tearoff_window->window,
1331 0, 0, 0, 0, -1, -1);
1332 g_object_unref (gc);
1334 gtk_widget_set_size_request (menu->tearoff_window,
1338 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
1339 g_object_unref (pixmap);
1344 popup_grab_on_window (GdkWindow *window,
1345 guint32 activate_time,
1346 gboolean grab_keyboard)
1348 if ((gdk_pointer_grab (window, TRUE,
1349 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1350 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1351 GDK_POINTER_MOTION_MASK,
1352 NULL, NULL, activate_time) == 0))
1354 if (!grab_keyboard ||
1355 gdk_keyboard_grab (window, TRUE,
1356 activate_time) == 0)
1360 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1371 * @menu: a #GtkMenu.
1372 * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
1373 * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
1374 * @func: a user supplied function used to position the menu, or %NULL
1375 * @data: user supplied data to be passed to @func.
1376 * @button: the mouse button which was pressed to initiate the event.
1377 * @activate_time: the time at which the activation event occurred.
1379 * Displays a menu and makes it available for selection. Applications can use
1380 * this function to display context-sensitive menus, and will typically supply
1381 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1382 * parameters. The default menu positioning function will position the menu
1383 * at the current mouse cursor position.
1385 * The @button parameter should be the mouse button pressed to initiate
1386 * the menu popup. If the menu popup was initiated by something other than
1387 * a mouse button press, such as a mouse button release or a keypress,
1388 * @button should be 0.
1390 * The @activate_time parameter is used to conflict-resolve initiation of
1391 * concurrent requests for mouse/keyboard grab requests. To function
1392 * properly, this needs to be the time stamp of the user event (such as
1393 * a mouse click or key press) that caused the initiation of the popup.
1394 * Only if no such event is available, gtk_get_current_event_time() can
1398 gtk_menu_popup (GtkMenu *menu,
1399 GtkWidget *parent_menu_shell,
1400 GtkWidget *parent_menu_item,
1401 GtkMenuPositionFunc func,
1404 guint32 activate_time)
1407 GtkWidget *xgrab_shell;
1409 GdkEvent *current_event;
1410 GtkMenuShell *menu_shell;
1411 gboolean grab_keyboard;
1412 GtkMenuPrivate *priv;
1413 GtkWidget *parent_toplevel;
1415 g_return_if_fail (GTK_IS_MENU (menu));
1417 widget = GTK_WIDGET (menu);
1418 menu_shell = GTK_MENU_SHELL (menu);
1419 priv = gtk_menu_get_private (menu);
1421 menu_shell->parent_menu_shell = parent_menu_shell;
1423 priv->seen_item_enter = FALSE;
1425 /* Find the last viewable ancestor, and make an X grab on it
1427 parent = GTK_WIDGET (menu);
1431 gboolean viewable = TRUE;
1432 GtkWidget *tmp = parent;
1436 if (!GTK_WIDGET_MAPPED (tmp))
1445 xgrab_shell = parent;
1447 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1450 /* We want to receive events generated when we map the menu; unfortunately,
1451 * since there is probably already an implicit grab in place from the
1452 * button that the user used to pop up the menu, we won't receive then --
1453 * in particular, the EnterNotify when the menu pops up under the pointer.
1455 * If we are grabbing on a parent menu shell, no problem; just grab on
1456 * that menu shell first before popping up the window with owner_events = TRUE.
1458 * When grabbing on the menu itself, things get more convuluted - we
1459 * we do an explicit grab on a specially created window with
1460 * owner_events = TRUE, which we override further down with a grab
1461 * on the menu. (We can't grab on the menu until it is mapped; we
1462 * probably could just leave the grab on the other window, with a
1463 * little reorganization of the code in gtkmenu*).
1465 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1466 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1468 if (xgrab_shell && xgrab_shell != widget)
1470 if (popup_grab_on_window (xgrab_shell->window, activate_time, grab_keyboard))
1471 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1475 GdkWindow *transfer_window;
1477 xgrab_shell = widget;
1478 transfer_window = menu_grab_transfer_window_get (menu);
1479 if (popup_grab_on_window (transfer_window, activate_time, grab_keyboard))
1480 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1483 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1485 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1486 * with a stuck up window, we just abort here. Presumably the user will
1489 menu_shell->parent_menu_shell = NULL;
1490 menu_grab_transfer_window_destroy (menu);
1494 menu_shell->active = TRUE;
1495 menu_shell->button = button;
1497 /* If we are popping up the menu from something other than, a button
1498 * press then, as a heuristic, we ignore enter events for the menu
1499 * until we get a MOTION_NOTIFY.
1502 current_event = gtk_get_current_event ();
1505 if ((current_event->type != GDK_BUTTON_PRESS) &&
1506 (current_event->type != GDK_ENTER_NOTIFY))
1507 menu_shell->ignore_enter = TRUE;
1509 gdk_event_free (current_event);
1512 menu_shell->ignore_enter = TRUE;
1516 gtk_menu_tearoff_bg_copy (menu);
1518 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1521 parent_toplevel = NULL;
1522 if (parent_menu_shell)
1523 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1524 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1526 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1528 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1531 /* Set transient for to get the right window group and parent relationship */
1532 if (GTK_IS_WINDOW (parent_toplevel))
1533 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1534 GTK_WINDOW (parent_toplevel));
1536 menu->parent_menu_item = parent_menu_item;
1537 menu->position_func = func;
1538 menu->position_func_data = data;
1539 menu_shell->activate_time = activate_time;
1541 /* We need to show the menu here rather in the init function because
1542 * code expects to be able to tell if the menu is onscreen by
1543 * looking at the GTK_WIDGET_VISIBLE (menu)
1545 gtk_widget_show (GTK_WIDGET (menu));
1547 /* Position the menu, possibly changing the size request
1549 gtk_menu_position (menu);
1551 /* Compute the size of the toplevel and realize it so we
1552 * can scroll correctly.
1555 GtkRequisition tmp_request;
1556 GtkAllocation tmp_allocation = { 0, };
1558 gtk_widget_size_request (menu->toplevel, &tmp_request);
1560 tmp_allocation.width = tmp_request.width;
1561 tmp_allocation.height = tmp_request.height;
1563 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1565 gtk_widget_realize (GTK_WIDGET (menu));
1568 gtk_menu_scroll_to (menu, menu->scroll_offset);
1570 /* if no item is selected, select the first one */
1571 if (!menu_shell->active_menu_item)
1573 gboolean touchscreen_mode;
1575 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1576 "gtk-touchscreen-mode", &touchscreen_mode,
1579 if (touchscreen_mode)
1580 gtk_menu_shell_select_first (menu_shell, TRUE);
1583 /* Once everything is set up correctly, map the toplevel window on
1586 gtk_widget_show (menu->toplevel);
1588 if (xgrab_shell == widget)
1589 popup_grab_on_window (widget->window, activate_time, grab_keyboard); /* Should always succeed */
1590 gtk_grab_add (GTK_WIDGET (menu));
1594 gtk_menu_popdown (GtkMenu *menu)
1596 GtkMenuPrivate *private;
1597 GtkMenuShell *menu_shell;
1599 g_return_if_fail (GTK_IS_MENU (menu));
1601 menu_shell = GTK_MENU_SHELL (menu);
1602 private = gtk_menu_get_private (menu);
1604 menu_shell->parent_menu_shell = NULL;
1605 menu_shell->active = FALSE;
1606 menu_shell->ignore_enter = FALSE;
1608 private->have_position = FALSE;
1610 gtk_menu_stop_scrolling (menu);
1612 gtk_menu_stop_navigating_submenu (menu);
1614 if (menu_shell->active_menu_item)
1616 if (menu->old_active_menu_item)
1617 g_object_unref (menu->old_active_menu_item);
1618 menu->old_active_menu_item = menu_shell->active_menu_item;
1619 g_object_ref (menu->old_active_menu_item);
1622 gtk_menu_shell_deselect (menu_shell);
1624 /* The X Grab, if present, will automatically be removed when we hide
1626 gtk_widget_hide (menu->toplevel);
1627 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1631 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1633 if (GTK_BIN (menu->toplevel)->child)
1635 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1639 /* We popped up the menu from the tearoff, so we need to
1640 * release the grab - we aren't actually hiding the menu.
1642 if (menu_shell->have_xgrab)
1644 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
1646 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1647 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1651 /* gtk_menu_popdown is called each time a menu item is selected from
1652 * a torn off menu. Only scroll back to the saved position if the
1653 * non-tearoff menu was popped down.
1655 if (!menu->tearoff_active)
1656 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1657 menu->tearoff_active = TRUE;
1660 gtk_widget_hide (GTK_WIDGET (menu));
1662 menu_shell->have_xgrab = FALSE;
1663 gtk_grab_remove (GTK_WIDGET (menu));
1665 menu_grab_transfer_window_destroy (menu);
1669 gtk_menu_get_active (GtkMenu *menu)
1674 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1676 if (!menu->old_active_menu_item)
1679 children = GTK_MENU_SHELL (menu)->children;
1683 child = children->data;
1684 children = children->next;
1686 if (GTK_BIN (child)->child)
1691 menu->old_active_menu_item = child;
1692 if (menu->old_active_menu_item)
1693 g_object_ref (menu->old_active_menu_item);
1696 return menu->old_active_menu_item;
1700 gtk_menu_set_active (GtkMenu *menu,
1706 g_return_if_fail (GTK_IS_MENU (menu));
1708 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1711 child = tmp_list->data;
1712 if (GTK_BIN (child)->child)
1714 if (menu->old_active_menu_item)
1715 g_object_unref (menu->old_active_menu_item);
1716 menu->old_active_menu_item = child;
1717 g_object_ref (menu->old_active_menu_item);
1723 gtk_menu_set_accel_group (GtkMenu *menu,
1724 GtkAccelGroup *accel_group)
1726 g_return_if_fail (GTK_IS_MENU (menu));
1728 if (menu->accel_group != accel_group)
1730 if (menu->accel_group)
1731 g_object_unref (menu->accel_group);
1732 menu->accel_group = accel_group;
1733 if (menu->accel_group)
1734 g_object_ref (menu->accel_group);
1735 _gtk_menu_refresh_accel_paths (menu, TRUE);
1740 gtk_menu_get_accel_group (GtkMenu *menu)
1742 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1744 return menu->accel_group;
1748 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1751 /* Menu items chain here to figure whether they can activate their
1752 * accelerators. Unlike ordinary widgets, menus allow accel
1753 * activation even if invisible since that's the usual case for
1754 * submenus/popup-menus. however, the state of the attach widget
1755 * affects the "activeness" of the menu.
1757 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1760 return gtk_widget_can_activate_accel (awidget, signal_id);
1762 return GTK_WIDGET_IS_SENSITIVE (widget);
1766 * gtk_menu_set_accel_path
1767 * @menu: a valid #GtkMenu
1768 * @accel_path: a valid accelerator path
1770 * Sets an accelerator path for this menu from which accelerator paths
1771 * for its immediate children, its menu items, can be constructed.
1772 * The main purpose of this function is to spare the programmer the
1773 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1774 * each menu item that should support runtime user changable accelerators.
1775 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1776 * each menu item of this menu, that contains a label describing its purpose,
1777 * automatically gets an accel path assigned. For example, a menu containing
1778 * menu items "New" and "Exit", will, after
1779 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1780 * has been called, assign its items the accel paths:
1781 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1782 * Assigning accel paths to menu items then enables the user to change
1783 * their accelerators at runtime. More details about accelerator paths
1784 * and their default setups can be found at gtk_accel_map_add_entry().
1786 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1787 * pass a static string, you can save some memory by interning it first with
1788 * g_intern_static_string().
1791 gtk_menu_set_accel_path (GtkMenu *menu,
1792 const gchar *accel_path)
1794 g_return_if_fail (GTK_IS_MENU (menu));
1796 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1798 /* FIXME: accel_path should be defined as const gchar* */
1799 menu->accel_path = (gchar*)g_intern_string (accel_path);
1800 if (menu->accel_path)
1801 _gtk_menu_refresh_accel_paths (menu, FALSE);
1805 * gtk_menu_get_accel_path
1806 * @menu: a valid #GtkMenu
1808 * Retrieves the accelerator path set on the menu.
1810 * Returns: the accelerator path set on the menu.
1815 gtk_menu_get_accel_path (GtkMenu *menu)
1817 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1819 return menu->accel_path;
1824 gboolean group_changed;
1828 refresh_accel_paths_foreach (GtkWidget *widget,
1831 AccelPropagation *prop = data;
1833 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1834 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1835 prop->menu->accel_path,
1836 prop->menu->accel_group,
1837 prop->group_changed);
1841 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1842 gboolean group_changed)
1844 g_return_if_fail (GTK_IS_MENU (menu));
1846 if (menu->accel_path && menu->accel_group)
1848 AccelPropagation prop;
1851 prop.group_changed = group_changed;
1852 gtk_container_foreach (GTK_CONTAINER (menu),
1853 refresh_accel_paths_foreach,
1859 gtk_menu_reposition (GtkMenu *menu)
1861 g_return_if_fail (GTK_IS_MENU (menu));
1863 if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1864 gtk_menu_position (menu);
1868 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1871 g_return_if_fail (GTK_IS_MENU (menu));
1873 if (adjustment->value != menu->scroll_offset)
1874 gtk_menu_scroll_to (menu, adjustment->value);
1878 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1881 GdkGeometry geometry_hints;
1883 if (!menu->tearoff_window)
1886 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1888 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1889 width += menu->tearoff_scrollbar->requisition.width;
1892 geometry_hints.min_width = width;
1893 geometry_hints.max_width = width;
1895 geometry_hints.min_height = 0;
1896 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1898 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1901 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1905 gtk_menu_update_title (GtkMenu *menu)
1907 if (menu->tearoff_window)
1910 GtkWidget *attach_widget;
1912 title = gtk_menu_get_title (menu);
1915 attach_widget = gtk_menu_get_attach_widget (menu);
1916 if (GTK_IS_MENU_ITEM (attach_widget))
1918 GtkWidget *child = GTK_BIN (attach_widget)->child;
1919 if (GTK_IS_LABEL (child))
1920 title = gtk_label_get_text (GTK_LABEL (child));
1925 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1930 gtk_menu_get_toplevel (GtkWidget *menu)
1932 GtkWidget *attach, *toplevel;
1934 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
1936 if (GTK_IS_MENU_ITEM (attach))
1937 attach = attach->parent;
1939 if (GTK_IS_MENU (attach))
1940 return gtk_menu_get_toplevel (attach);
1941 else if (GTK_IS_WIDGET (attach))
1943 toplevel = gtk_widget_get_toplevel (attach);
1944 if (GTK_WIDGET_TOPLEVEL (toplevel))
1952 tearoff_window_destroyed (GtkWidget *widget,
1955 gtk_menu_set_tearoff_state (menu, FALSE);
1959 gtk_menu_set_tearoff_state (GtkMenu *menu,
1964 g_return_if_fail (GTK_IS_MENU (menu));
1966 if (menu->torn_off != torn_off)
1968 menu->torn_off = torn_off;
1969 menu->tearoff_active = torn_off;
1973 if (GTK_WIDGET_VISIBLE (menu))
1974 gtk_menu_popdown (menu);
1976 if (!menu->tearoff_window)
1978 GtkWidget *toplevel;
1980 menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
1981 "type", GTK_WINDOW_TOPLEVEL,
1982 "screen", gtk_widget_get_screen (menu->toplevel),
1983 "app-paintable", TRUE,
1986 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1987 GDK_WINDOW_TYPE_HINT_MENU);
1988 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1989 g_signal_connect (menu->tearoff_window, "destroy",
1990 G_CALLBACK (tearoff_window_destroyed), menu);
1991 g_signal_connect (menu->tearoff_window, "event",
1992 G_CALLBACK (gtk_menu_window_event), menu);
1994 gtk_menu_update_title (menu);
1996 gtk_widget_realize (menu->tearoff_window);
1998 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
1999 if (toplevel != NULL)
2000 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
2001 GTK_WINDOW (toplevel));
2003 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
2004 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
2006 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
2007 menu->tearoff_adjustment =
2008 GTK_ADJUSTMENT (gtk_adjustment_new (0,
2010 GTK_WIDGET (menu)->requisition.height,
2014 g_object_connect (menu->tearoff_adjustment,
2015 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2017 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
2019 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
2020 menu->tearoff_scrollbar,
2023 if (menu->tearoff_adjustment->upper > height)
2024 gtk_widget_show (menu->tearoff_scrollbar);
2026 gtk_widget_show (menu->tearoff_hbox);
2029 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
2031 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
2033 /* Update menu->requisition
2035 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
2037 gtk_menu_set_tearoff_hints (menu, width);
2039 gtk_widget_realize (menu->tearoff_window);
2040 gtk_menu_position (menu);
2042 gtk_widget_show (GTK_WIDGET (menu));
2043 gtk_widget_show (menu->tearoff_window);
2045 gtk_menu_scroll_to (menu, 0);
2050 gtk_widget_hide (GTK_WIDGET (menu));
2051 gtk_widget_hide (menu->tearoff_window);
2052 if (GTK_IS_CONTAINER (menu->toplevel))
2053 gtk_menu_reparent (menu, menu->toplevel, FALSE);
2054 gtk_widget_destroy (menu->tearoff_window);
2056 menu->tearoff_window = NULL;
2057 menu->tearoff_hbox = NULL;
2058 menu->tearoff_scrollbar = NULL;
2059 menu->tearoff_adjustment = NULL;
2062 g_object_notify (G_OBJECT (menu), "tearoff-state");
2067 * gtk_menu_get_tearoff_state:
2070 * Returns whether the menu is torn off. See
2071 * gtk_menu_set_tearoff_state ().
2073 * Return value: %TRUE if the menu is currently torn off.
2076 gtk_menu_get_tearoff_state (GtkMenu *menu)
2078 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2080 return menu->torn_off;
2084 * gtk_menu_set_title:
2086 * @title: a string containing the title for the menu.
2088 * Sets the title string for the menu. The title is displayed when the menu
2089 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
2090 * attached to a parent menu item, and if so it will try to use the same text as
2091 * that menu item's label.
2094 gtk_menu_set_title (GtkMenu *menu,
2097 GtkMenuPrivate *priv;
2100 g_return_if_fail (GTK_IS_MENU (menu));
2102 priv = gtk_menu_get_private (menu);
2104 old_title = priv->title;
2105 priv->title = g_strdup (title);
2108 gtk_menu_update_title (menu);
2109 g_object_notify (G_OBJECT (menu), "tearoff-title");
2113 * gtk_menu_get_title:
2116 * Returns the title of the menu. See gtk_menu_set_title().
2118 * Return value: the title of the menu, or %NULL if the menu has no
2119 * title set on it. This string is owned by the widget and should
2120 * not be modified or freed.
2122 G_CONST_RETURN gchar *
2123 gtk_menu_get_title (GtkMenu *menu)
2125 GtkMenuPrivate *priv;
2127 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2129 priv = gtk_menu_get_private (menu);
2135 gtk_menu_reorder_child (GtkMenu *menu,
2139 GtkMenuShell *menu_shell;
2141 g_return_if_fail (GTK_IS_MENU (menu));
2142 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2144 menu_shell = GTK_MENU_SHELL (menu);
2146 if (g_list_find (menu_shell->children, child))
2148 menu_shell->children = g_list_remove (menu_shell->children, child);
2149 menu_shell->children = g_list_insert (menu_shell->children, child, position);
2151 menu_queue_resize (menu);
2156 gtk_menu_style_set (GtkWidget *widget,
2157 GtkStyle *previous_style)
2159 if (GTK_WIDGET_REALIZED (widget))
2161 GtkMenu *menu = GTK_MENU (widget);
2163 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2164 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2165 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2170 get_arrows_border (GtkMenu *menu,
2173 guint scroll_arrow_height;
2174 GtkArrowPlacement arrow_placement;
2176 gtk_widget_style_get (GTK_WIDGET (menu),
2177 "scroll-arrow-vlength", &scroll_arrow_height,
2178 "arrow_placement", &arrow_placement,
2181 switch (arrow_placement)
2183 case GTK_ARROWS_BOTH:
2184 border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2185 border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2188 case GTK_ARROWS_START:
2189 border->top = (menu->upper_arrow_visible ||
2190 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2194 case GTK_ARROWS_END:
2196 border->bottom = (menu->upper_arrow_visible ||
2197 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2201 border->left = border->right = 0;
2205 gtk_menu_realize (GtkWidget *widget)
2207 GdkWindowAttr attributes;
2208 gint attributes_mask;
2213 guint vertical_padding;
2214 guint horizontal_padding;
2215 GtkBorder arrow_border;
2217 g_return_if_fail (GTK_IS_MENU (widget));
2219 menu = GTK_MENU (widget);
2221 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
2223 attributes.window_type = GDK_WINDOW_CHILD;
2224 attributes.x = widget->allocation.x;
2225 attributes.y = widget->allocation.y;
2226 attributes.width = widget->allocation.width;
2227 attributes.height = widget->allocation.height;
2228 attributes.wclass = GDK_INPUT_OUTPUT;
2229 attributes.visual = gtk_widget_get_visual (widget);
2230 attributes.colormap = gtk_widget_get_colormap (widget);
2232 attributes.event_mask = gtk_widget_get_events (widget);
2234 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2235 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2237 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2238 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
2239 gdk_window_set_user_data (widget->window, widget);
2241 border_width = GTK_CONTAINER (widget)->border_width;
2243 gtk_widget_style_get (GTK_WIDGET (menu),
2244 "vertical-padding", &vertical_padding,
2245 "horizontal-padding", &horizontal_padding,
2248 attributes.x = border_width + widget->style->xthickness + horizontal_padding;
2249 attributes.y = border_width + widget->style->ythickness + vertical_padding;
2250 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
2251 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
2253 get_arrows_border (menu, &arrow_border);
2254 attributes.y += arrow_border.top;
2255 attributes.height -= arrow_border.top;
2256 attributes.height -= arrow_border.bottom;
2258 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
2259 gdk_window_set_user_data (menu->view_window, menu);
2263 attributes.width = MAX (1, widget->allocation.width - (border_width + widget->style->xthickness + horizontal_padding) * 2);
2264 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
2266 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
2267 gdk_window_set_user_data (menu->bin_window, menu);
2269 children = GTK_MENU_SHELL (menu)->children;
2272 child = children->data;
2273 children = children->next;
2275 gtk_widget_set_parent_window (child, menu->bin_window);
2278 widget->style = gtk_style_attach (widget->style, widget->window);
2279 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2280 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2281 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2283 if (GTK_MENU_SHELL (widget)->active_menu_item)
2284 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2285 GTK_MENU_SHELL (widget)->active_menu_item);
2287 gdk_window_show (menu->bin_window);
2288 gdk_window_show (menu->view_window);
2292 gtk_menu_focus (GtkWidget *widget,
2293 GtkDirectionType direction)
2296 * A menu or its menu items cannot have focus
2301 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2304 menu_grab_transfer_window_get (GtkMenu *menu)
2306 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2309 GdkWindowAttr attributes;
2310 gint attributes_mask;
2312 attributes.x = -100;
2313 attributes.y = -100;
2314 attributes.width = 10;
2315 attributes.height = 10;
2316 attributes.window_type = GDK_WINDOW_TEMP;
2317 attributes.wclass = GDK_INPUT_ONLY;
2318 attributes.override_redirect = TRUE;
2319 attributes.event_mask = 0;
2321 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2323 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2324 &attributes, attributes_mask);
2325 gdk_window_set_user_data (window, menu);
2327 gdk_window_show (window);
2329 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2336 menu_grab_transfer_window_destroy (GtkMenu *menu)
2338 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2341 gdk_window_set_user_data (window, NULL);
2342 gdk_window_destroy (window);
2343 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2348 gtk_menu_unrealize (GtkWidget *widget)
2350 GtkMenu *menu = GTK_MENU (widget);
2352 menu_grab_transfer_window_destroy (menu);
2354 gdk_window_set_user_data (menu->view_window, NULL);
2355 gdk_window_destroy (menu->view_window);
2356 menu->view_window = NULL;
2358 gdk_window_set_user_data (menu->bin_window, NULL);
2359 gdk_window_destroy (menu->bin_window);
2360 menu->bin_window = NULL;
2362 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2366 gtk_menu_size_request (GtkWidget *widget,
2367 GtkRequisition *requisition)
2371 GtkMenuShell *menu_shell;
2374 guint max_toggle_size;
2375 guint max_accel_width;
2376 guint vertical_padding;
2377 guint horizontal_padding;
2378 GtkRequisition child_requisition;
2379 GtkMenuPrivate *priv;
2381 g_return_if_fail (GTK_IS_MENU (widget));
2382 g_return_if_fail (requisition != NULL);
2384 menu = GTK_MENU (widget);
2385 menu_shell = GTK_MENU_SHELL (widget);
2386 priv = gtk_menu_get_private (menu);
2388 requisition->width = 0;
2389 requisition->height = 0;
2391 max_toggle_size = 0;
2392 max_accel_width = 0;
2394 g_free (priv->heights);
2395 priv->heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2396 priv->heights_length = gtk_menu_get_n_rows (menu);
2398 children = menu_shell->children;
2405 child = children->data;
2406 children = children->next;
2408 if (! GTK_WIDGET_VISIBLE (child))
2411 get_effective_child_attach (child, &l, &r, &t, &b);
2413 /* It's important to size_request the child
2414 * before doing the toggle size request, in
2415 * case the toggle size request depends on the size
2416 * request of a child of the child (e.g. for ImageMenuItem)
2419 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
2420 gtk_widget_size_request (child, &child_requisition);
2422 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2423 max_toggle_size = MAX (max_toggle_size, toggle_size);
2424 max_accel_width = MAX (max_accel_width,
2425 GTK_MENU_ITEM (child)->accelerator_width);
2427 part = child_requisition.width / (r - l);
2428 requisition->width = MAX (requisition->width, part);
2430 part = MAX (child_requisition.height, toggle_size) / (b - t);
2431 priv->heights[t] = MAX (priv->heights[t], part);
2434 /* if the menu doesn't include any images or check items
2435 * reserve the space so that all menus are consistent */
2436 if (max_toggle_size == 0)
2438 guint toggle_spacing;
2439 guint indicator_size;
2441 gtk_style_get (widget->style,
2442 GTK_TYPE_CHECK_MENU_ITEM,
2443 "toggle-spacing", &toggle_spacing,
2444 "indicator-size", &indicator_size,
2447 max_toggle_size = indicator_size + toggle_spacing;
2450 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2451 requisition->height += priv->heights[i];
2453 requisition->width += 2 * max_toggle_size + max_accel_width;
2454 requisition->width *= gtk_menu_get_n_columns (menu);
2456 gtk_widget_style_get (GTK_WIDGET (menu),
2457 "vertical-padding", &vertical_padding,
2458 "horizontal-padding", &horizontal_padding,
2461 requisition->width += (GTK_CONTAINER (menu)->border_width + horizontal_padding +
2462 widget->style->xthickness) * 2;
2463 requisition->height += (GTK_CONTAINER (menu)->border_width + vertical_padding +
2464 widget->style->ythickness) * 2;
2466 menu->toggle_size = max_toggle_size;
2468 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2470 if (menu->tearoff_active)
2471 gtk_menu_set_tearoff_hints (menu, requisition->width);
2475 gtk_menu_size_allocate (GtkWidget *widget,
2476 GtkAllocation *allocation)
2479 GtkMenuShell *menu_shell;
2481 GtkAllocation child_allocation;
2482 GtkRequisition child_requisition;
2483 GtkMenuPrivate *priv;
2487 guint vertical_padding;
2488 guint horizontal_padding;
2490 g_return_if_fail (GTK_IS_MENU (widget));
2491 g_return_if_fail (allocation != NULL);
2493 menu = GTK_MENU (widget);
2494 menu_shell = GTK_MENU_SHELL (widget);
2495 priv = gtk_menu_get_private (menu);
2497 widget->allocation = *allocation;
2498 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2500 gtk_widget_style_get (GTK_WIDGET (menu),
2501 "vertical-padding", &vertical_padding,
2502 "horizontal-padding", &horizontal_padding,
2505 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness + horizontal_padding;
2506 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness + vertical_padding;
2508 width = MAX (1, allocation->width - x * 2);
2509 height = MAX (1, allocation->height - y * 2);
2511 child_requisition.width -= x * 2;
2512 child_requisition.height -= y * 2;
2514 if (menu_shell->active)
2515 gtk_menu_scroll_to (menu, menu->scroll_offset);
2517 if (!menu->tearoff_active)
2519 GtkBorder arrow_border;
2521 get_arrows_border (menu, &arrow_border);
2522 y += arrow_border.top;
2523 height -= arrow_border.top;
2524 height -= arrow_border.bottom;
2527 if (GTK_WIDGET_REALIZED (widget))
2529 gdk_window_move_resize (widget->window,
2530 allocation->x, allocation->y,
2531 allocation->width, allocation->height);
2533 gdk_window_move_resize (menu->view_window,
2540 if (menu_shell->children)
2542 gint base_width = width / gtk_menu_get_n_columns (menu);
2544 children = menu_shell->children;
2547 child = children->data;
2548 children = children->next;
2550 if (GTK_WIDGET_VISIBLE (child))
2555 get_effective_child_attach (child, &l, &r, &t, &b);
2557 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2560 tmp = gtk_menu_get_n_columns (menu) - l;
2561 l = gtk_menu_get_n_columns (menu) - r;
2565 child_allocation.width = (r - l) * base_width;
2566 child_allocation.height = 0;
2567 child_allocation.x = l * base_width;
2568 child_allocation.y = 0;
2570 for (i = 0; i < b; i++)
2573 child_allocation.y += priv->heights[i];
2575 child_allocation.height += priv->heights[i];
2578 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2581 gtk_widget_size_allocate (child, &child_allocation);
2582 gtk_widget_queue_draw (child);
2586 /* Resize the item window */
2587 if (GTK_WIDGET_REALIZED (widget))
2593 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2594 height += priv->heights[i];
2596 width = gtk_menu_get_n_columns (menu) * base_width;
2597 gdk_window_resize (menu->bin_window, width, height);
2600 if (menu->tearoff_active)
2602 if (allocation->height >= widget->requisition.height)
2604 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2606 gtk_widget_hide (menu->tearoff_scrollbar);
2607 gtk_menu_set_tearoff_hints (menu, allocation->width);
2609 gtk_menu_scroll_to (menu, 0);
2614 menu->tearoff_adjustment->upper = widget->requisition.height;
2615 menu->tearoff_adjustment->page_size = allocation->height;
2617 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2618 menu->tearoff_adjustment->upper)
2621 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2624 gtk_menu_scroll_to (menu, value);
2627 gtk_adjustment_changed (menu->tearoff_adjustment);
2629 if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2631 gtk_widget_show (menu->tearoff_scrollbar);
2632 gtk_menu_set_tearoff_hints (menu, allocation->width);
2640 get_arrows_visible_area (GtkMenu *menu,
2641 GdkRectangle *border,
2642 GdkRectangle *upper,
2643 GdkRectangle *lower,
2646 GtkWidget *widget = GTK_WIDGET (menu);
2647 guint vertical_padding;
2648 guint horizontal_padding;
2649 gint scroll_arrow_height;
2650 GtkArrowPlacement arrow_placement;
2652 gtk_widget_style_get (widget,
2653 "vertical-padding", &vertical_padding,
2654 "horizontal-padding", &horizontal_padding,
2655 "scroll-arrow-vlength", &scroll_arrow_height,
2656 "arrow-placement", &arrow_placement,
2659 border->x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness + horizontal_padding;
2660 border->y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
2661 gdk_drawable_get_size (widget->window, &border->width, &border->height);
2663 switch (arrow_placement)
2665 case GTK_ARROWS_BOTH:
2666 upper->x = border->x;
2667 upper->y = border->y;
2668 upper->width = border->width - 2 * border->x;
2669 upper->height = scroll_arrow_height;
2671 lower->x = border->x;
2672 lower->y = border->height - border->y - scroll_arrow_height;
2673 lower->width = border->width - 2 * border->x;
2674 lower->height = scroll_arrow_height;
2677 case GTK_ARROWS_START:
2678 upper->x = border->x;
2679 upper->y = border->y;
2680 upper->width = (border->width - 2 * border->x) / 2;
2681 upper->height = scroll_arrow_height;
2683 lower->x = border->x + upper->width;
2684 lower->y = border->y;
2685 lower->width = (border->width - 2 * border->x) / 2;
2686 lower->height = scroll_arrow_height;
2689 case GTK_ARROWS_END:
2690 upper->x = border->x;
2691 upper->y = border->height - border->y - scroll_arrow_height;
2692 upper->width = (border->width - 2 * border->x) / 2;
2693 upper->height = scroll_arrow_height;
2695 lower->x = border->x + upper->width;
2696 lower->y = border->height - border->y - scroll_arrow_height;
2697 lower->width = (border->width - 2 * border->x) / 2;
2698 lower->height = scroll_arrow_height;
2702 *arrow_space = scroll_arrow_height - 2 * widget->style->ythickness;
2706 gtk_menu_paint (GtkWidget *widget,
2707 GdkEventExpose *event)
2710 GtkMenuPrivate *priv;
2711 GdkRectangle border;
2716 g_return_if_fail (GTK_IS_MENU (widget));
2718 menu = GTK_MENU (widget);
2719 priv = gtk_menu_get_private (menu);
2721 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2723 if (event->window == widget->window)
2725 gfloat arrow_scaling;
2728 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
2729 arrow_size = arrow_scaling * arrow_space;
2731 gtk_paint_box (widget->style,
2735 &event->area, widget, "menu",
2738 if (menu->upper_arrow_visible && !menu->tearoff_active)
2740 gtk_paint_box (widget->style,
2742 priv->upper_arrow_state,
2744 &event->area, widget, "menu_scroll_arrow_up",
2750 gtk_paint_arrow (widget->style,
2752 priv->upper_arrow_state,
2754 &event->area, widget, "menu_scroll_arrow_up",
2757 upper.x + (upper.width - arrow_size) / 2,
2758 upper.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
2759 arrow_size, arrow_size);
2762 if (menu->lower_arrow_visible && !menu->tearoff_active)
2764 gtk_paint_box (widget->style,
2766 priv->lower_arrow_state,
2768 &event->area, widget, "menu_scroll_arrow_down",
2774 gtk_paint_arrow (widget->style,
2776 priv->lower_arrow_state,
2778 &event->area, widget, "menu_scroll_arrow_down",
2781 lower.x + (lower.width - arrow_size) / 2,
2782 lower.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
2783 arrow_size, arrow_size);
2786 else if (event->window == menu->bin_window)
2788 gint y = -border.y + menu->scroll_offset;
2790 if (!menu->tearoff_active)
2792 GtkBorder arrow_border;
2794 get_arrows_border (menu, &arrow_border);
2795 y -= arrow_border.top;
2798 gtk_paint_box (widget->style,
2802 &event->area, widget, "menu",
2804 border.width, border.height);
2809 gtk_menu_expose (GtkWidget *widget,
2810 GdkEventExpose *event)
2812 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2813 g_return_val_if_fail (event != NULL, FALSE);
2815 if (GTK_WIDGET_DRAWABLE (widget))
2817 gtk_menu_paint (widget, event);
2819 GTK_WIDGET_CLASS (gtk_menu_parent_class)->expose_event (widget, event);
2826 gtk_menu_show (GtkWidget *widget)
2828 GtkMenu *menu = GTK_MENU (widget);
2830 _gtk_menu_refresh_accel_paths (menu, FALSE);
2832 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
2836 gtk_menu_button_scroll (GtkMenu *menu,
2837 GdkEventButton *event)
2839 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2841 gboolean touchscreen_mode;
2843 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
2844 "gtk-touchscreen-mode", &touchscreen_mode,
2847 if (touchscreen_mode)
2848 gtk_menu_handle_scrolling (menu,
2849 event->x_root, event->y_root,
2850 event->type == GDK_BUTTON_PRESS,
2860 pointer_in_menu_window (GtkWidget *widget,
2864 GtkMenu *menu = GTK_MENU (widget);
2866 if (GTK_WIDGET_MAPPED (menu->toplevel))
2868 GtkMenuShell *menu_shell;
2869 gint window_x, window_y;
2871 gdk_window_get_position (menu->toplevel->window, &window_x, &window_y);
2873 if (x_root >= window_x && x_root < window_x + widget->allocation.width &&
2874 y_root >= window_y && y_root < window_y + widget->allocation.height)
2877 menu_shell = GTK_MENU_SHELL (widget);
2879 if (GTK_IS_MENU (menu_shell->parent_menu_shell))
2880 return pointer_in_menu_window (menu_shell->parent_menu_shell,
2888 gtk_menu_button_press (GtkWidget *widget,
2889 GdkEventButton *event)
2891 if (event->type != GDK_BUTTON_PRESS)
2894 /* Don't pass down to menu shell for presses over scroll arrows
2896 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
2899 /* Don't pass down to menu shell if a non-menuitem part of the menu
2900 * was clicked. The check for the event_widget being a GtkMenuShell
2901 * works because we have the pointer grabbed on menu_shell->window
2902 * with owner_events=TRUE, so all events that are either outside
2903 * the menu or on its border are delivered relative to
2904 * menu_shell->window.
2906 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
2907 pointer_in_menu_window (widget, event->x_root, event->y_root))
2910 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
2914 gtk_menu_button_release (GtkWidget *widget,
2915 GdkEventButton *event)
2917 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
2919 if (priv->ignore_button_release)
2921 priv->ignore_button_release = FALSE;
2925 if (event->type != GDK_BUTTON_RELEASE)
2928 /* Don't pass down to menu shell for releases over scroll arrows
2930 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
2933 /* Don't pass down to menu shell if a non-menuitem part of the menu
2934 * was clicked (see comment in button_press()).
2936 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
2937 pointer_in_menu_window (widget, event->x_root, event->y_root))
2939 /* Ugly: make sure menu_shell->button gets reset to 0 when we
2940 * bail out early here so it is in a consistent state for the
2941 * next button_press/button_release in GtkMenuShell.
2944 if (GTK_MENU_SHELL (widget)->active)
2945 GTK_MENU_SHELL (widget)->button = 0;
2950 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
2953 static const gchar *
2954 get_accel_path (GtkWidget *menu_item,
2959 GClosure *accel_closure;
2960 GtkAccelGroup *accel_group;
2962 path = _gtk_widget_get_accel_path (menu_item, locked);
2965 path = GTK_MENU_ITEM (menu_item)->accel_path;
2971 label = GTK_BIN (menu_item)->child;
2973 if (GTK_IS_ACCEL_LABEL (label))
2975 g_object_get (label,
2976 "accel-closure", &accel_closure,
2980 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
2982 *locked = accel_group->lock_count > 0;
2992 gtk_menu_key_press (GtkWidget *widget,
2995 GtkMenuShell *menu_shell;
2997 gboolean delete = FALSE;
2998 gboolean can_change_accels;
2999 gchar *accel = NULL;
3000 guint accel_key, accel_mods;
3001 GdkModifierType consumed_modifiers;
3002 GdkDisplay *display;
3004 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3005 g_return_val_if_fail (event != NULL, FALSE);
3007 menu_shell = GTK_MENU_SHELL (widget);
3008 menu = GTK_MENU (widget);
3010 gtk_menu_stop_navigating_submenu (menu);
3012 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3015 display = gtk_widget_get_display (widget);
3017 g_object_get (gtk_widget_get_settings (widget),
3018 "gtk-menu-bar-accel", &accel,
3019 "gtk-can-change-accels", &can_change_accels,
3022 if (accel && *accel)
3025 GdkModifierType mods = 0;
3027 gtk_accelerator_parse (accel, &keyval, &mods);
3030 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3032 /* FIXME this is wrong, needs to be in the global accel resolution
3033 * thing, to properly consider i18n etc., but that probably requires
3034 * AccelGroup changes etc.
3036 if (event->keyval == keyval && (mods & event->state) == mods)
3038 gtk_menu_shell_cancel (menu_shell);
3046 switch (event->keyval)
3057 /* Figure out what modifiers went into determining the key symbol */
3058 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3059 event->hardware_keycode, event->state, event->group,
3060 NULL, NULL, NULL, &consumed_modifiers);
3062 accel_key = gdk_keyval_to_lower (event->keyval);
3063 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3065 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
3066 * We re-upper case when we match against the keyval, but display and save in caseless form.
3068 if (accel_key != event->keyval)
3069 accel_mods |= GDK_SHIFT_MASK;
3071 /* Modify the accelerators */
3072 if (can_change_accels &&
3073 menu_shell->active_menu_item &&
3074 GTK_BIN (menu_shell->active_menu_item)->child && /* no separators */
3075 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
3076 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3078 GtkWidget *menu_item = menu_shell->active_menu_item;
3079 gboolean locked, replace_accels = TRUE;
3082 path = get_accel_path (menu_item, &locked);
3083 if (!path || locked)
3085 /* can't change accelerators on menu_items without paths
3086 * (basically, those items are accelerator-locked).
3088 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
3089 gtk_widget_error_bell (widget);
3095 /* For the keys that act to delete the current setting, we delete
3096 * the current setting if there is one, otherwise, we set the
3097 * key as the accelerator.
3103 if (gtk_accel_map_lookup_entry (path, &key) &&
3104 (key.accel_key || key.accel_mods))
3110 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3114 /* we failed, probably because this key is in use and
3117 /* g_print("failed to change\n"); */
3118 gtk_widget_error_bell (widget);
3127 check_threshold (GtkWidget *widget,
3136 ABS (start_x - x) > THRESHOLD ||
3137 ABS (start_y - y) > THRESHOLD;
3141 definitely_within_item (GtkWidget *widget,
3145 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
3148 gdk_drawable_get_size (window, &w, &h);
3151 check_threshold (widget, 0, 0, x, y) &&
3152 check_threshold (widget, w - 1, 0, x, y) &&
3153 check_threshold (widget, w - 1, h - 1, x, y) &&
3154 check_threshold (widget, 0, h - 1, x, y);
3158 gtk_menu_motion_notify (GtkWidget *widget,
3159 GdkEventMotion *event)
3161 GtkWidget *menu_item;
3163 GtkMenuShell *menu_shell;
3165 gboolean need_enter;
3167 if (GTK_IS_MENU (widget))
3169 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3171 if (priv->ignore_button_release)
3172 priv->ignore_button_release = FALSE;
3174 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3178 /* We received the event for one of two reasons:
3180 * a) We are the active menu, and did gtk_grab_add()
3181 * b) The widget is a child of ours, and the event was propagated
3183 * Since for computation of navigation regions, we want the menu which
3184 * is the parent of the menu item, for a), we need to find that menu,
3185 * which may be different from 'widget'.
3187 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3188 if (!GTK_IS_MENU_ITEM (menu_item) ||
3189 !GTK_IS_MENU (menu_item->parent))
3192 menu_shell = GTK_MENU_SHELL (menu_item->parent);
3193 menu = GTK_MENU (menu_shell);
3195 if (definitely_within_item (menu_item, event->x, event->y))
3196 menu_shell->activate_time = 0;
3198 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
3200 /* Check to see if we are within an active submenu's navigation region
3202 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3205 /* Make sure we pop down if we enter a non-selectable menu item, so we
3206 * don't show a submenu when the cursor is outside the stay-up triangle.
3208 if (!_gtk_menu_item_is_selectable (menu_item))
3210 gtk_menu_shell_deselect (menu_shell);
3216 /* The menu is now sensitive to enter events on its items, but
3217 * was previously sensitive. So we fake an enter event.
3221 menu_shell->ignore_enter = FALSE;
3223 gdk_drawable_get_size (event->window, &width, &height);
3224 if (event->x >= 0 && event->x < width &&
3225 event->y >= 0 && event->y < height)
3227 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3230 send_event->crossing.window = g_object_ref (event->window);
3231 send_event->crossing.time = event->time;
3232 send_event->crossing.send_event = TRUE;
3233 send_event->crossing.x_root = event->x_root;
3234 send_event->crossing.y_root = event->y_root;
3235 send_event->crossing.x = event->x;
3236 send_event->crossing.y = event->y;
3237 send_event->crossing.state = event->state;
3239 /* We send the event to 'widget', the currently active menu,
3240 * instead of 'menu', the menu that the pointer is in. This
3241 * will ensure that the event will be ignored unless the
3242 * menuitem is a child of the active menu or some parent
3243 * menu of the active menu.
3245 result = gtk_widget_event (widget, send_event);
3246 gdk_event_free (send_event);
3256 get_double_arrows (GtkMenu *menu)
3258 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3259 gboolean double_arrows;
3260 GtkArrowPlacement arrow_placement;
3262 gtk_widget_style_get (GTK_WIDGET (menu),
3263 "double-arrows", &double_arrows,
3264 "arrow-placement", &arrow_placement,
3267 if (arrow_placement != GTK_ARROWS_BOTH)
3270 return double_arrows || (priv->initially_pushed_in &&
3271 menu->scroll_offset != 0);
3275 gtk_menu_scroll_by (GtkMenu *menu,
3280 gint view_width, view_height;
3281 gboolean double_arrows;
3282 GtkBorder arrow_border;
3284 widget = GTK_WIDGET (menu);
3285 offset = menu->scroll_offset + step;
3287 get_arrows_border (menu, &arrow_border);
3289 double_arrows = get_double_arrows (menu);
3291 /* If we scroll upward and the non-visible top part
3292 * is smaller than the scroll arrow it would be
3293 * pretty stupid to show the arrow and taking more
3294 * screen space than just scrolling to the top.
3297 if ((step < 0) && (offset < arrow_border.top))
3300 /* Don't scroll over the top if we weren't before: */
3301 if ((menu->scroll_offset >= 0) && (offset < 0))
3304 gdk_drawable_get_size (widget->window, &view_width, &view_height);
3306 if (menu->scroll_offset == 0 &&
3307 view_height >= widget->requisition.height)
3310 /* Don't scroll past the bottom if we weren't before: */
3311 if (menu->scroll_offset > 0)
3312 view_height -= arrow_border.top;
3314 /* When both arrows are always shown, reduce
3315 * view height even more.
3318 view_height -= arrow_border.bottom;
3320 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
3321 (offset + view_height > widget->requisition.height))
3322 offset = widget->requisition.height - view_height;
3324 if (offset != menu->scroll_offset)
3325 gtk_menu_scroll_to (menu, offset);
3329 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3330 gboolean touchscreen_mode)
3332 gboolean upper_visible;
3333 gboolean lower_visible;
3335 upper_visible = menu->upper_arrow_visible;
3336 lower_visible = menu->lower_arrow_visible;
3338 gtk_menu_scroll_by (menu, menu->scroll_step);
3340 if (touchscreen_mode &&
3341 (upper_visible != menu->upper_arrow_visible ||
3342 lower_visible != menu->lower_arrow_visible))
3344 /* We are about to hide a scroll arrow while the mouse is pressed,
3345 * this would cause the uncovered menu item to be activated on button
3346 * release. Therefore we need to ignore button release here
3348 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3349 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3354 gtk_menu_scroll_timeout (gpointer data)
3357 gboolean touchscreen_mode;
3359 menu = GTK_MENU (data);
3361 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3362 "gtk-touchscreen-mode", &touchscreen_mode,
3365 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3371 gtk_menu_scroll_timeout_initial (gpointer data)
3375 gboolean touchscreen_mode;
3377 menu = GTK_MENU (data);
3379 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3380 "gtk-timeout-repeat", &timeout,
3381 "gtk-touchscreen-mode", &touchscreen_mode,
3384 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3386 gtk_menu_remove_scroll_timeout (menu);
3388 menu->timeout_id = gdk_threads_add_timeout (timeout,
3389 gtk_menu_scroll_timeout,
3396 gtk_menu_start_scrolling (GtkMenu *menu)
3399 gboolean touchscreen_mode;
3401 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3402 "gtk-timeout-repeat", &timeout,
3403 "gtk-touchscreen-mode", &touchscreen_mode,
3406 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3408 menu->timeout_id = gdk_threads_add_timeout (timeout,
3409 gtk_menu_scroll_timeout_initial,
3414 gtk_menu_scroll (GtkWidget *widget,
3415 GdkEventScroll *event)
3417 GtkMenu *menu = GTK_MENU (widget);
3419 switch (event->direction)
3421 case GDK_SCROLL_RIGHT:
3422 case GDK_SCROLL_DOWN:
3423 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3425 case GDK_SCROLL_LEFT:
3427 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3435 get_arrows_sensitive_area (GtkMenu *menu,
3436 GdkRectangle *upper,
3437 GdkRectangle *lower)
3441 guint vertical_padding;
3443 gint scroll_arrow_height;
3444 GtkArrowPlacement arrow_placement;
3446 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3448 gtk_widget_style_get (GTK_WIDGET (menu),
3449 "vertical-padding", &vertical_padding,
3450 "scroll-arrow-vlength", &scroll_arrow_height,
3451 "arrow-placement", &arrow_placement,
3454 border = GTK_CONTAINER (menu)->border_width +
3455 GTK_WIDGET (menu)->style->ythickness + vertical_padding;
3457 gdk_window_get_position (GTK_WIDGET (menu)->window, &win_x, &win_y);
3459 switch (arrow_placement)
3461 case GTK_ARROWS_BOTH:
3466 upper->width = width;
3467 upper->height = scroll_arrow_height + border;
3473 lower->y = win_y + height - border - scroll_arrow_height;
3474 lower->width = width;
3475 lower->height = scroll_arrow_height + border;
3479 case GTK_ARROWS_START:
3484 upper->width = width / 2;
3485 upper->height = scroll_arrow_height + border;
3490 lower->x = win_x + width / 2;
3492 lower->width = width / 2;
3493 lower->height = scroll_arrow_height + border;
3497 case GTK_ARROWS_END:
3501 upper->y = win_y + height - border - scroll_arrow_height;
3502 upper->width = width / 2;
3503 upper->height = scroll_arrow_height + border;
3508 lower->x = win_x + width / 2;
3509 lower->y = win_y + height - border - scroll_arrow_height;
3510 lower->width = width / 2;
3511 lower->height = scroll_arrow_height + border;
3519 gtk_menu_handle_scrolling (GtkMenu *menu,
3525 GtkMenuShell *menu_shell;
3526 GtkMenuPrivate *priv;
3529 gboolean scroll_fast = FALSE;
3531 gboolean touchscreen_mode;
3533 priv = gtk_menu_get_private (menu);
3535 menu_shell = GTK_MENU_SHELL (menu);
3537 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3538 "gtk-touchscreen-mode", &touchscreen_mode,
3541 gdk_window_get_position (menu->toplevel->window, &top_x, &top_y);
3545 /* upper arrow handling */
3547 get_arrows_sensitive_area (menu, &rect, NULL);
3550 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3551 (x >= rect.x) && (x < rect.x + rect.width) &&
3552 (y >= rect.y) && (y < rect.y + rect.height))
3557 if (touchscreen_mode)
3558 menu->upper_arrow_prelight = in_arrow;
3560 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3562 gboolean arrow_pressed = FALSE;
3564 if (menu->upper_arrow_visible && !menu->tearoff_active)
3566 if (touchscreen_mode)
3568 if (enter && menu->upper_arrow_prelight)
3570 if (menu->timeout_id == 0)
3572 /* Deselect the active item so that
3573 * any submenus are popped down
3575 gtk_menu_shell_deselect (menu_shell);
3577 gtk_menu_remove_scroll_timeout (menu);
3578 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3582 /* Only do stuff on click. */
3583 gtk_menu_start_scrolling (menu);
3584 arrow_pressed = TRUE;
3589 arrow_pressed = TRUE;
3594 gtk_menu_stop_scrolling (menu);
3597 else /* !touchscreen_mode */
3599 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3601 if (enter && in_arrow &&
3602 (!menu->upper_arrow_prelight ||
3603 menu->scroll_fast != scroll_fast))
3605 menu->upper_arrow_prelight = TRUE;
3606 menu->scroll_fast = scroll_fast;
3608 /* Deselect the active item so that
3609 * any submenus are popped down
3611 gtk_menu_shell_deselect (menu_shell);
3613 gtk_menu_remove_scroll_timeout (menu);
3614 menu->scroll_step = scroll_fast ?
3615 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
3618 gdk_threads_add_timeout (scroll_fast ?
3619 MENU_SCROLL_TIMEOUT2 :
3620 MENU_SCROLL_TIMEOUT1,
3621 gtk_menu_scroll_timeout, menu);
3623 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
3625 gtk_menu_stop_scrolling (menu);
3630 /* gtk_menu_start_scrolling() might have hit the top of the
3631 * menu, so check if the button isn't insensitive before
3632 * changing it to something else.
3634 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3636 GtkStateType arrow_state = GTK_STATE_NORMAL;
3639 arrow_state = GTK_STATE_ACTIVE;
3640 else if (menu->upper_arrow_prelight)
3641 arrow_state = GTK_STATE_PRELIGHT;
3643 if (arrow_state != priv->upper_arrow_state)
3645 priv->upper_arrow_state = arrow_state;
3647 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3653 /* lower arrow handling */
3655 get_arrows_sensitive_area (menu, NULL, &rect);
3658 if (menu->lower_arrow_visible && !menu->tearoff_active &&
3659 (x >= rect.x) && (x < rect.x + rect.width) &&
3660 (y >= rect.y) && (y < rect.y + rect.height))
3665 if (touchscreen_mode)
3666 menu->lower_arrow_prelight = in_arrow;
3668 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3670 gboolean arrow_pressed = FALSE;
3672 if (menu->lower_arrow_visible && !menu->tearoff_active)
3674 if (touchscreen_mode)
3676 if (enter && menu->lower_arrow_prelight)
3678 if (menu->timeout_id == 0)
3680 /* Deselect the active item so that
3681 * any submenus are popped down
3683 gtk_menu_shell_deselect (menu_shell);
3685 gtk_menu_remove_scroll_timeout (menu);
3686 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
3690 /* Only do stuff on click. */
3691 gtk_menu_start_scrolling (menu);
3692 arrow_pressed = TRUE;
3697 arrow_pressed = TRUE;
3702 gtk_menu_stop_scrolling (menu);
3705 else /* !touchscreen_mode */
3707 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
3709 if (enter && in_arrow &&
3710 (!menu->lower_arrow_prelight ||
3711 menu->scroll_fast != scroll_fast))
3713 menu->lower_arrow_prelight = TRUE;
3714 menu->scroll_fast = scroll_fast;
3716 /* Deselect the active item so that
3717 * any submenus are popped down
3719 gtk_menu_shell_deselect (menu_shell);
3721 gtk_menu_remove_scroll_timeout (menu);
3722 menu->scroll_step = scroll_fast ?
3723 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
3726 gdk_threads_add_timeout (scroll_fast ?
3727 MENU_SCROLL_TIMEOUT2 :
3728 MENU_SCROLL_TIMEOUT1,
3729 gtk_menu_scroll_timeout, menu);
3731 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
3733 gtk_menu_stop_scrolling (menu);
3738 /* gtk_menu_start_scrolling() might have hit the bottom of the
3739 * menu, so check if the button isn't insensitive before
3740 * changing it to something else.
3742 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3744 GtkStateType arrow_state = GTK_STATE_NORMAL;
3747 arrow_state = GTK_STATE_ACTIVE;
3748 else if (menu->lower_arrow_prelight)
3749 arrow_state = GTK_STATE_PRELIGHT;
3751 if (arrow_state != priv->lower_arrow_state)
3753 priv->lower_arrow_state = arrow_state;
3755 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3763 gtk_menu_enter_notify (GtkWidget *widget,
3764 GdkEventCrossing *event)
3766 GtkWidget *menu_item;
3767 gboolean touchscreen_mode;
3769 if (event->mode == GDK_CROSSING_GTK_GRAB ||
3770 event->mode == GDK_CROSSING_GTK_UNGRAB ||
3771 event->mode == GDK_CROSSING_STATE_CHANGED)
3774 g_object_get (gtk_widget_get_settings (widget),
3775 "gtk-touchscreen-mode", &touchscreen_mode,
3778 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3779 if (GTK_IS_MENU (widget))
3781 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
3783 if (!menu_shell->ignore_enter)
3784 gtk_menu_handle_scrolling (GTK_MENU (widget),
3785 event->x_root, event->y_root, TRUE, TRUE);
3788 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
3790 GtkWidget *menu = menu_item->parent;
3792 if (GTK_IS_MENU (menu))
3794 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
3795 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3797 if (priv->seen_item_enter)
3799 /* This is the second enter we see for an item
3800 * on this menu. This means a release should always
3803 menu_shell->activate_time = 0;
3805 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
3806 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
3808 if (definitely_within_item (menu_item, event->x, event->y))
3810 /* This is an actual user-enter (ie. not a pop-under)
3811 * In this case, the user must either have entered
3812 * sufficiently far enough into the item, or he must move
3813 * far enough away from the enter point. (see
3814 * gtk_menu_motion_notify())
3816 menu_shell->activate_time = 0;
3820 priv->seen_item_enter = TRUE;
3824 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
3825 * will not correspond to the event widget's parent. Check to see
3826 * if we are in the parent's navigation region.
3828 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
3829 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent),
3830 event->x_root, event->y_root))
3833 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
3837 gtk_menu_leave_notify (GtkWidget *widget,
3838 GdkEventCrossing *event)
3840 GtkMenuShell *menu_shell;
3842 GtkMenuItem *menu_item;
3843 GtkWidget *event_widget;
3845 if (event->mode == GDK_CROSSING_GTK_GRAB ||
3846 event->mode == GDK_CROSSING_GTK_UNGRAB ||
3847 event->mode == GDK_CROSSING_STATE_CHANGED)
3850 menu = GTK_MENU (widget);
3851 menu_shell = GTK_MENU_SHELL (widget);
3853 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3856 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
3858 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3860 if (!GTK_IS_MENU_ITEM (event_widget))
3863 menu_item = GTK_MENU_ITEM (event_widget);
3865 /* Here we check to see if we're leaving an active menu item with a submenu,
3866 * in which case we enter submenu navigation mode.
3868 if (menu_shell->active_menu_item != NULL
3869 && menu_item->submenu != NULL
3870 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
3872 if (GTK_MENU_SHELL (menu_item->submenu)->active)
3874 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
3877 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
3879 /* We are leaving an active menu item with nonactive submenu.
3880 * Deselect it so we don't surprise the user with by popping
3881 * up a submenu _after_ he left the item.
3883 gtk_menu_shell_deselect (menu_shell);
3888 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
3892 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
3894 if (menu->navigation_region)
3896 gdk_region_destroy (menu->navigation_region);
3897 menu->navigation_region = NULL;
3899 if (menu->navigation_timeout)
3901 g_source_remove (menu->navigation_timeout);
3902 menu->navigation_timeout = 0;
3906 /* When the timeout is elapsed, the navigation region is destroyed
3907 * and the menuitem under the pointer (if any) is selected.
3910 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
3912 GtkMenu *menu = user_data;
3913 GdkWindow *child_window;
3915 gtk_menu_stop_navigating_submenu (menu);
3917 if (GTK_WIDGET_REALIZED (menu))
3919 child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
3923 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3925 send_event->crossing.window = g_object_ref (child_window);
3926 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
3927 send_event->crossing.send_event = TRUE;
3929 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
3931 gdk_event_free (send_event);
3939 gtk_menu_navigating_submenu (GtkMenu *menu,
3943 if (menu->navigation_region)
3945 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
3949 gtk_menu_stop_navigating_submenu (menu);
3956 #undef DRAW_STAY_UP_TRIANGLE
3958 #ifdef DRAW_STAY_UP_TRIANGLE
3961 draw_stay_up_triangle (GdkWindow *window,
3964 /* Draw ugly color all over the stay-up triangle */
3965 GdkColor ugly_color = { 0, 50000, 10000, 10000 };
3966 GdkGCValues gc_values;
3968 GdkRectangle clipbox;
3970 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
3971 ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
3972 gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
3973 gdk_gc_set_clip_region (ugly_gc, region);
3975 gdk_region_get_clipbox (region, &clipbox);
3977 gdk_draw_rectangle (window,
3980 clipbox.x, clipbox.y,
3981 clipbox.width, clipbox.height);
3983 g_object_unref (ugly_gc);
3988 flip_region (GdkRegion *region,
3993 GdkRectangle *rectangles;
3994 GdkRectangle clipbox;
3995 GdkRegion *new_region;
3998 new_region = gdk_region_new ();
4000 gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
4001 gdk_region_get_clipbox (region, &clipbox);
4003 for (i = 0; i < n_rectangles; ++i)
4005 GdkRectangle rect = rectangles[i];
4008 rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
4011 rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
4013 gdk_region_union_with_rect (new_region, &rect);
4016 g_free (rectangles);
4022 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4023 GtkMenuItem *menu_item,
4024 GdkEventCrossing *event)
4026 gint submenu_left = 0;
4027 gint submenu_right = 0;
4028 gint submenu_top = 0;
4029 gint submenu_bottom = 0;
4033 GtkWidget *event_widget;
4035 g_return_if_fail (menu_item->submenu != NULL);
4036 g_return_if_fail (event != NULL);
4038 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4040 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
4041 gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
4043 submenu_right = submenu_left + width;
4044 submenu_bottom = submenu_top + height;
4046 gdk_drawable_get_size (event_widget->window, &width, &height);
4048 if (event->x >= 0 && event->x < width)
4051 gboolean flip_y = FALSE;
4052 gboolean flip_x = FALSE;
4054 gtk_menu_stop_navigating_submenu (menu);
4056 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
4059 point[0].x = event->x_root;
4060 point[1].x = submenu_left;
4065 point[0].x = event->x_root + 1;
4066 point[1].x = 2 * (event->x_root + 1) - submenu_right;
4074 point[0].y = event->y_root + 1;
4075 point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
4077 if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
4085 point[0].y = event->y_root;
4086 point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
4088 if (point[0].y >= submenu_bottom)
4092 point[2].x = point[1].x;
4093 point[2].y = point[0].y;
4095 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
4097 if (flip_x || flip_y)
4099 GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
4100 gdk_region_destroy (menu->navigation_region);
4101 menu->navigation_region = new_region;
4104 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4105 "gtk-menu-popdown-delay", &popdown_delay,
4108 menu->navigation_timeout = gdk_threads_add_timeout (popdown_delay,
4109 gtk_menu_stop_navigating_submenu_cb,
4112 #ifdef DRAW_STAY_UP_TRIANGLE
4113 draw_stay_up_triangle (gdk_get_default_root_window(),
4114 menu->navigation_region);
4120 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4124 g_return_if_fail (GTK_IS_MENU (menu_shell));
4126 parent = menu_shell->parent_menu_shell;
4128 menu_shell->activate_time = 0;
4129 gtk_menu_popdown (GTK_MENU (menu_shell));
4132 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4136 gtk_menu_position (GtkMenu *menu)
4139 GtkRequisition requisition;
4140 GtkMenuPrivate *private;
4145 GdkScreen *pointer_screen;
4146 GdkRectangle monitor;
4148 g_return_if_fail (GTK_IS_MENU (menu));
4150 widget = GTK_WIDGET (menu);
4152 screen = gtk_widget_get_screen (widget);
4153 gdk_display_get_pointer (gdk_screen_get_display (screen),
4154 &pointer_screen, &x, &y, NULL);
4156 /* We need the requisition to figure out the right place to
4157 * popup the menu. In fact, we always need to ask here, since
4158 * if a size_request was queued while we weren't popped up,
4159 * the requisition won't have been recomputed yet.
4161 gtk_widget_size_request (widget, &requisition);
4163 if (pointer_screen != screen)
4165 /* Pointer is on a different screen; roughly center the
4166 * menu on the screen. If someone was using multiscreen
4167 * + Xinerama together they'd probably want something
4168 * fancier; but that is likely to be vanishingly rare.
4170 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4171 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4174 private = gtk_menu_get_private (menu);
4175 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4177 private->initially_pushed_in = FALSE;
4179 /* Set the type hint here to allow custom position functions to set a different hint */
4180 if (!GTK_WIDGET_VISIBLE (menu->toplevel))
4181 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4183 if (menu->position_func)
4185 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
4186 menu->position_func_data);
4187 if (private->monitor_num < 0)
4188 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4190 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4194 gint space_left, space_right, space_above, space_below;
4197 gint xthickness = widget->style->xthickness;
4198 gint ythickness = widget->style->ythickness;
4199 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4201 /* The placement of popup menus horizontally works like this (with
4202 * RTL in parentheses)
4204 * - If there is enough room to the right (left) of the mouse cursor,
4205 * position the menu there.
4207 * - Otherwise, if if there is enough room to the left (right) of the
4208 * mouse cursor, position the menu there.
4210 * - Otherwise if the menu is smaller than the monitor, position it
4211 * on the side of the mouse cursor that has the most space available
4213 * - Otherwise (if there is simply not enough room for the menu on the
4214 * monitor), position it as far left (right) as possible.
4216 * Positioning in the vertical direction is similar: first try below
4217 * mouse cursor, then above.
4219 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4221 space_left = x - monitor.x;
4222 space_right = monitor.x + monitor.width - x - 1;
4223 space_above = y - monitor.y;
4224 space_below = monitor.y + monitor.height - y - 1;
4226 /* position horizontally */
4228 /* the amount of space we need to position the menu. Note the
4229 * menu is offset "xthickness" pixels
4231 needed_width = requisition.width - xthickness;
4233 if (needed_width <= space_left ||
4234 needed_width <= space_right)
4236 if ((rtl && needed_width <= space_left) ||
4237 (!rtl && needed_width > space_right))
4240 x = x + xthickness - requisition.width + 1;
4244 /* position right */
4248 /* x is clamped on-screen further down */
4250 else if (requisition.width <= monitor.width)
4252 /* the menu is too big to fit on either side of the mouse
4253 * cursor, but smaller than the monitor. Position it on
4254 * the side that has the most space
4256 if (space_left > space_right)
4264 x = monitor.x + monitor.width - requisition.width;
4267 else /* menu is simply too big for the monitor */
4272 x = monitor.x + monitor.width - requisition.width;
4281 /* Position vertically. The algorithm is the same as above, but
4282 * simpler because we don't have to take RTL into account.
4284 needed_height = requisition.height - ythickness;
4286 if (needed_height <= space_above ||
4287 needed_height <= space_below)
4289 if (needed_height <= space_below)
4292 y = y + ythickness - requisition.height + 1;
4294 y = CLAMP (y, monitor.y,
4295 monitor.y + monitor.height - requisition.height);
4297 else if (needed_height > space_below && needed_height > space_above)
4299 if (space_below >= space_above)
4300 y = monitor.y + monitor.height - requisition.height;
4312 if (private->initially_pushed_in)
4314 menu_height = GTK_WIDGET (menu)->requisition.height;
4316 if (y + menu_height > monitor.y + monitor.height)
4318 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4319 y = (monitor.y + monitor.height) - menu_height;
4324 scroll_offset += monitor.y - y;
4329 /* FIXME: should this be done in the various position_funcs ? */
4330 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4332 if (GTK_MENU_SHELL (menu)->active)
4334 private->have_position = TRUE;
4339 if (y + requisition.height > monitor.y + monitor.height)
4340 requisition.height = (monitor.y + monitor.height) - y;
4344 scroll_offset += monitor.y - y;
4345 requisition.height -= monitor.y - y;
4349 if (scroll_offset > 0)
4351 GtkBorder arrow_border;
4353 get_arrows_border (menu, &arrow_border);
4354 scroll_offset += arrow_border.top;
4357 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
4360 if (!GTK_MENU_SHELL (menu)->active)
4362 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4363 requisition.width, requisition.height);
4366 menu->scroll_offset = scroll_offset;
4370 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4372 if (menu->timeout_id)
4374 g_source_remove (menu->timeout_id);
4375 menu->timeout_id = 0;
4380 gtk_menu_stop_scrolling (GtkMenu *menu)
4382 gboolean touchscreen_mode;
4384 gtk_menu_remove_scroll_timeout (menu);
4386 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4387 "gtk-touchscreen-mode", &touchscreen_mode,
4390 if (!touchscreen_mode)
4392 menu->upper_arrow_prelight = FALSE;
4393 menu->lower_arrow_prelight = FALSE;
4398 gtk_menu_scroll_to (GtkMenu *menu,
4403 gint view_width, view_height;
4406 guint vertical_padding;
4407 guint horizontal_padding;
4408 gboolean double_arrows;
4409 GtkBorder arrow_border;
4411 widget = GTK_WIDGET (menu);
4413 if (menu->tearoff_active &&
4414 menu->tearoff_adjustment &&
4415 (menu->tearoff_adjustment->value != offset))
4417 menu->tearoff_adjustment->value =
4419 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4420 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4423 /* Move/resize the viewport according to arrows: */
4424 view_width = widget->allocation.width;
4425 view_height = widget->allocation.height;
4427 gtk_widget_style_get (GTK_WIDGET (menu),
4428 "vertical-padding", &vertical_padding,
4429 "horizontal-padding", &horizontal_padding,
4432 double_arrows = get_double_arrows (menu);
4434 border_width = GTK_CONTAINER (menu)->border_width;
4435 view_width -= (border_width + widget->style->xthickness + horizontal_padding) * 2;
4436 view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
4437 menu_height = widget->requisition.height -
4438 (border_width + widget->style->ythickness + vertical_padding) * 2;
4440 x = border_width + widget->style->xthickness + horizontal_padding;
4441 y = border_width + widget->style->ythickness + vertical_padding;
4443 if (double_arrows && !menu->tearoff_active)
4445 if (view_height < menu_height ||
4446 (offset > 0 && menu->scroll_offset > 0) ||
4447 (offset < 0 && menu->scroll_offset < 0))
4449 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4450 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4451 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4453 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4454 gtk_widget_queue_draw (GTK_WIDGET (menu));
4456 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4458 get_arrows_border (menu, &arrow_border);
4459 y += arrow_border.top;
4460 view_height -= arrow_border.top;
4461 view_height -= arrow_border.bottom;
4464 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4465 else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4466 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4467 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4469 if (offset >= menu_height - view_height)
4470 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4471 else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4472 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4473 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4475 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4476 (priv->lower_arrow_state != lower_arrow_previous_state))
4477 gtk_widget_queue_draw (GTK_WIDGET (menu));
4479 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4480 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4482 /* At the upper border, possibly remove timeout */
4483 if (menu->scroll_step < 0)
4485 gtk_menu_stop_scrolling (menu);
4486 gtk_widget_queue_draw (GTK_WIDGET (menu));
4490 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4491 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4493 /* At the lower border, possibly remove timeout */
4494 if (menu->scroll_step > 0)
4496 gtk_menu_stop_scrolling (menu);
4497 gtk_widget_queue_draw (GTK_WIDGET (menu));
4501 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4505 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4506 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4508 gtk_menu_stop_scrolling (menu);
4509 gtk_widget_queue_draw (GTK_WIDGET (menu));
4512 else if (!menu->tearoff_active)
4514 gboolean last_visible;
4516 last_visible = menu->upper_arrow_visible;
4517 menu->upper_arrow_visible = offset > 0;
4519 /* upper_arrow_visible may have changed, so requery the border */
4520 get_arrows_border (menu, &arrow_border);
4521 view_height -= arrow_border.top;
4523 if ((last_visible != menu->upper_arrow_visible) &&
4524 !menu->upper_arrow_visible)
4526 menu->upper_arrow_prelight = FALSE;
4528 /* If we hid the upper arrow, possibly remove timeout */
4529 if (menu->scroll_step < 0)
4531 gtk_menu_stop_scrolling (menu);
4532 gtk_widget_queue_draw (GTK_WIDGET (menu));
4536 last_visible = menu->lower_arrow_visible;
4537 menu->lower_arrow_visible = offset < menu_height - view_height;
4539 /* lower_arrow_visible may have changed, so requery the border */
4540 get_arrows_border (menu, &arrow_border);
4541 view_height -= arrow_border.bottom;
4543 if ((last_visible != menu->lower_arrow_visible) &&
4544 !menu->lower_arrow_visible)
4546 menu->lower_arrow_prelight = FALSE;
4548 /* If we hid the lower arrow, possibly remove timeout */
4549 if (menu->scroll_step > 0)
4551 gtk_menu_stop_scrolling (menu);
4552 gtk_widget_queue_draw (GTK_WIDGET (menu));
4556 y += arrow_border.top;
4559 /* Scroll the menu: */
4560 if (GTK_WIDGET_REALIZED (menu))
4561 gdk_window_move (menu->bin_window, 0, -offset);
4563 if (GTK_WIDGET_REALIZED (menu))
4564 gdk_window_move_resize (menu->view_window,
4570 menu->scroll_offset = offset;
4574 compute_child_offset (GtkMenu *menu,
4575 GtkWidget *menu_item,
4578 gboolean *is_last_child)
4580 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4581 gint item_top_attach;
4582 gint item_bottom_attach;
4583 gint child_offset = 0;
4586 get_effective_child_attach (menu_item, NULL, NULL,
4587 &item_top_attach, &item_bottom_attach);
4589 /* there is a possibility that we get called before _size_request, so
4590 * check the height table for safety.
4592 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4595 /* when we have a row with only invisible children, it's height will
4596 * be zero, so there's no need to check WIDGET_VISIBLE here
4598 for (i = 0; i < item_top_attach; i++)
4599 child_offset += priv->heights[i];
4602 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4604 *offset = child_offset;
4606 *height = priv->heights[item_top_attach];
4612 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4613 GtkWidget *menu_item)
4616 gint child_offset, child_height;
4620 gboolean last_child = 0;
4622 menu = GTK_MENU (menu_shell);
4624 /* We need to check if the selected item fully visible.
4625 * If not we need to scroll the menu so that it becomes fully
4629 if (compute_child_offset (menu, menu_item,
4630 &child_offset, &child_height, &last_child))
4632 guint vertical_padding;
4633 gboolean double_arrows;
4635 y = menu->scroll_offset;
4636 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
4638 gtk_widget_style_get (GTK_WIDGET (menu),
4639 "vertical-padding", &vertical_padding,
4642 double_arrows = get_double_arrows (menu);
4644 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
4646 if (child_offset < y)
4648 /* Ignore the enter event we might get if the pointer is on the menu
4650 menu_shell->ignore_enter = TRUE;
4651 gtk_menu_scroll_to (menu, child_offset);
4655 GtkBorder arrow_border;
4659 get_arrows_border (menu, &arrow_border);
4660 if (!menu->tearoff_active)
4661 arrow_height = arrow_border.top + arrow_border.bottom;
4663 if (child_offset + child_height > y + height - arrow_height)
4666 if ((!last_child && !menu->tearoff_active) || double_arrows)
4667 arrow_height += arrow_border.bottom;
4669 y = child_offset + child_height - height + arrow_height;
4670 if (((y > 0) && !menu->tearoff_active) || double_arrows)
4672 /* Need upper arrow */
4673 arrow_height += arrow_border.top;
4674 y = child_offset + child_height - height + arrow_height;
4676 /* Ignore the enter event we might get if the pointer is on the menu
4678 menu_shell->ignore_enter = TRUE;
4679 gtk_menu_scroll_to (menu, y);
4687 gtk_menu_select_item (GtkMenuShell *menu_shell,
4688 GtkWidget *menu_item)
4690 GtkMenu *menu = GTK_MENU (menu_shell);
4692 if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
4693 gtk_menu_scroll_item_visible (menu_shell, menu_item);
4695 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
4699 /* Reparent the menu, taking care of the refcounting
4701 * If unrealize is true we force a unrealize while reparenting the parent.
4702 * This can help eliminate flicker in some cases.
4704 * What happens is that when the menu is unrealized and then re-realized,
4705 * the allocations are as follows:
4707 * parent - 1x1 at (0,0)
4708 * child1 - 100x20 at (0,0)
4709 * child2 - 100x20 at (0,20)
4710 * child3 - 100x20 at (0,40)
4712 * That is, the parent is small but the children are full sized. Then,
4713 * when the queued_resize gets processed, the parent gets resized to
4716 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
4717 * contains the following logic:
4719 * - if a move or resize operation on a window would change the clip
4720 * region on the children, then before the window is resized
4721 * the background for children is temporarily set to None, the
4722 * move/resize done, and the background for the children restored.
4724 * So, at the point where the parent is resized to final size, the
4725 * background for the children is temporarily None, and thus they
4726 * are not cleared to the background color and the previous background
4727 * (the image of the menu) is left in place.
4730 gtk_menu_reparent (GtkMenu *menu,
4731 GtkWidget *new_parent,
4734 GtkObject *object = GTK_OBJECT (menu);
4735 GtkWidget *widget = GTK_WIDGET (menu);
4736 gboolean was_floating = g_object_is_floating (object);
4738 g_object_ref_sink (object);
4742 g_object_ref (object);
4743 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
4744 gtk_container_add (GTK_CONTAINER (new_parent), widget);
4745 g_object_unref (object);
4748 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
4751 g_object_force_floating (G_OBJECT (object));
4753 g_object_unref (object);
4757 gtk_menu_show_all (GtkWidget *widget)
4759 /* Show children, but not self. */
4760 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
4765 gtk_menu_hide_all (GtkWidget *widget)
4767 /* Hide children, but not self. */
4768 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
4772 * gtk_menu_set_screen:
4773 * @menu: a #GtkMenu.
4774 * @screen: a #GdkScreen, or %NULL if the screen should be
4775 * determined by the widget the menu is attached to.
4777 * Sets the #GdkScreen on which the menu will be displayed.
4782 gtk_menu_set_screen (GtkMenu *menu,
4785 g_return_if_fail (GTK_IS_MENU (menu));
4786 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
4788 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
4792 menu_change_screen (menu, screen);
4796 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
4798 attach_widget_screen_changed (attach_widget, NULL, menu);
4804 * @menu: a #GtkMenu.
4805 * @child: a #GtkMenuItem.
4806 * @left_attach: The column number to attach the left side of the item to.
4807 * @right_attach: The column number to attach the right side of the item to.
4808 * @top_attach: The row number to attach the top of the item to.
4809 * @bottom_attach: The row number to attach the bottom of the item to.
4811 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
4812 * an item will occupy is specified by @left_attach, @right_attach,
4813 * @top_attach and @bottom_attach. These each represent the leftmost,
4814 * rightmost, uppermost and lower column and row numbers of the table.
4815 * (Columns and rows are indexed from zero).
4817 * Note that this function is not related to gtk_menu_detach().
4822 gtk_menu_attach (GtkMenu *menu,
4827 guint bottom_attach)
4829 GtkMenuShell *menu_shell;
4831 g_return_if_fail (GTK_IS_MENU (menu));
4832 g_return_if_fail (GTK_IS_MENU_ITEM (child));
4833 g_return_if_fail (child->parent == NULL ||
4834 child->parent == GTK_WIDGET (menu));
4835 g_return_if_fail (left_attach < right_attach);
4836 g_return_if_fail (top_attach < bottom_attach);
4838 menu_shell = GTK_MENU_SHELL (menu);
4842 AttachInfo *ai = get_attach_info (child);
4844 ai->left_attach = left_attach;
4845 ai->right_attach = right_attach;
4846 ai->top_attach = top_attach;
4847 ai->bottom_attach = bottom_attach;
4849 menu_shell->children = g_list_append (menu_shell->children, child);
4851 gtk_widget_set_parent (child, GTK_WIDGET (menu));
4853 menu_queue_resize (menu);
4857 gtk_container_child_set (GTK_CONTAINER (child->parent), child,
4858 "left-attach", left_attach,
4859 "right-attach", right_attach,
4860 "top-attach", top_attach,
4861 "bottom-attach", bottom_attach,
4867 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
4871 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
4872 "gtk-menu-popup-delay", &popup_delay,
4879 find_child_containing (GtkMenuShell *menu_shell,
4887 /* find a child which includes the area given by
4888 * left, right, top, bottom.
4891 for (list = menu_shell->children; list; list = list->next)
4895 if (!_gtk_menu_item_is_selectable (list->data))
4898 get_effective_child_attach (list->data, &l, &r, &t, &b);
4900 if (l <= left && right <= r
4901 && t <= top && bottom <= b)
4902 return GTK_WIDGET (list->data);
4909 gtk_menu_move_current (GtkMenuShell *menu_shell,
4910 GtkMenuDirectionType direction)
4912 GtkMenu *menu = GTK_MENU (menu_shell);
4915 GtkWidget *match = NULL;
4917 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
4921 case GTK_MENU_DIR_CHILD:
4922 direction = GTK_MENU_DIR_PARENT;
4924 case GTK_MENU_DIR_PARENT:
4925 direction = GTK_MENU_DIR_CHILD;
4931 /* use special table menu key bindings */
4932 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
4934 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
4936 if (direction == GTK_MENU_DIR_NEXT)
4938 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
4940 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
4948 for (i = 0; i < t; i++)
4950 match = find_child_containing (menu_shell,
4951 l, l + 1, i, i + 1);
4957 else if (direction == GTK_MENU_DIR_PREV)
4959 for (i = t; i > 0; i--)
4961 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
4969 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
4971 match = find_child_containing (menu_shell,
4972 l, l + 1, i - 1, i);
4978 else if (direction == GTK_MENU_DIR_PARENT)
4980 /* we go one left if possible */
4982 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
4986 GtkWidget *parent = menu_shell->parent_menu_shell;
4989 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
4990 match = menu_shell->active_menu_item;
4993 else if (direction == GTK_MENU_DIR_CHILD)
4995 /* we go one right if possible */
4996 if (r < gtk_menu_get_n_columns (menu))
4997 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5001 GtkWidget *parent = menu_shell->parent_menu_shell;
5003 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
5005 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
5006 match = menu_shell->active_menu_item;
5012 gtk_menu_shell_select_item (menu_shell, match);
5017 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5021 get_visible_size (GtkMenu *menu)
5023 GtkWidget *widget = GTK_WIDGET (menu);
5024 GtkContainer *container = GTK_CONTAINER (menu);
5026 gint menu_height = (widget->allocation.height
5027 - 2 * (container->border_width
5028 + widget->style->ythickness));
5030 if (!menu->tearoff_active)
5032 GtkBorder arrow_border;
5034 get_arrows_border (menu, &arrow_border);
5035 menu_height -= arrow_border.top;
5036 menu_height -= arrow_border.bottom;
5042 /* Find the sensitive on-screen child containing @y, or if none,
5043 * the nearest selectable onscreen child. (%NULL if none)
5046 child_at (GtkMenu *menu,
5049 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5050 GtkWidget *child = NULL;
5051 gint child_offset = 0;
5054 gint lower, upper; /* Onscreen bounds */
5056 menu_height = get_visible_size (menu);
5057 lower = menu->scroll_offset;
5058 upper = menu->scroll_offset + menu_height;
5060 for (children = menu_shell->children; children; children = children->next)
5062 if (GTK_WIDGET_VISIBLE (children->data))
5064 GtkRequisition child_requisition;
5066 gtk_widget_size_request (children->data, &child_requisition);
5068 if (_gtk_menu_item_is_selectable (children->data) &&
5069 child_offset >= lower &&
5070 child_offset + child_requisition.height <= upper)
5072 child = children->data;
5074 if (child_offset + child_requisition.height > y &&
5075 !GTK_IS_TEAROFF_MENU_ITEM (child))
5079 child_offset += child_requisition.height;
5087 get_menu_height (GtkMenu *menu)
5090 GtkWidget *widget = GTK_WIDGET (menu);
5092 height = widget->requisition.height;
5093 height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
5095 if (!menu->tearoff_active)
5097 GtkBorder arrow_border;
5099 get_arrows_border (menu, &arrow_border);
5100 height -= arrow_border.top;
5101 height -= arrow_border.bottom;
5108 gtk_menu_real_move_scroll (GtkMenu *menu,
5111 gint page_size = get_visible_size (menu);
5112 gint end_position = get_menu_height (menu);
5113 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5117 case GTK_SCROLL_PAGE_UP:
5118 case GTK_SCROLL_PAGE_DOWN:
5122 gint child_offset = 0;
5123 gboolean old_upper_arrow_visible;
5126 if (type == GTK_SCROLL_PAGE_UP)
5131 if (menu_shell->active_menu_item)
5135 compute_child_offset (menu, menu_shell->active_menu_item,
5136 &child_offset, &child_height, NULL);
5137 child_offset += child_height / 2;
5140 menu_shell->ignore_enter = TRUE;
5141 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5142 old_offset = menu->scroll_offset;
5144 new_offset = menu->scroll_offset + step;
5145 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5147 gtk_menu_scroll_to (menu, new_offset);
5149 if (menu_shell->active_menu_item)
5151 GtkWidget *new_child;
5152 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5153 GtkBorder arrow_border;
5155 get_arrows_border (menu, &arrow_border);
5157 if (menu->scroll_offset != old_offset)
5158 step = menu->scroll_offset - old_offset;
5160 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5162 new_child = child_at (menu, child_offset + step);
5164 gtk_menu_shell_select_item (menu_shell, new_child);
5168 case GTK_SCROLL_START:
5169 /* Ignore the enter event we might get if the pointer is on the menu
5171 menu_shell->ignore_enter = TRUE;
5172 gtk_menu_scroll_to (menu, 0);
5173 gtk_menu_shell_select_first (menu_shell, TRUE);
5175 case GTK_SCROLL_END:
5176 /* Ignore the enter event we might get if the pointer is on the menu
5178 menu_shell->ignore_enter = TRUE;
5179 gtk_menu_scroll_to (menu, end_position - page_size);
5180 _gtk_menu_shell_select_last (menu_shell, TRUE);
5189 * gtk_menu_set_monitor:
5191 * @monitor_num: the number of the monitor on which the menu should
5194 * Informs GTK+ on which monitor a menu should be popped up.
5195 * See gdk_screen_get_monitor_geometry().
5197 * This function should be called from a #GtkMenuPositionFunc if the
5198 * menu should not appear on the same monitor as the pointer. This
5199 * information can't be reliably inferred from the coordinates returned
5200 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
5201 * may extend beyond the monitor boundaries or even the screen boundaries.
5206 gtk_menu_set_monitor (GtkMenu *menu,
5209 GtkMenuPrivate *priv;
5210 g_return_if_fail (GTK_IS_MENU (menu));
5212 priv = gtk_menu_get_private (menu);
5214 priv->monitor_num = monitor_num;
5218 * gtk_menu_get_monitor:
5221 * Retrieves the number of the monitor on which to show the menu.
5223 * Returns: the number of the monitor on which the menu should
5224 * be popped up or -1, if no monitor has been set
5229 gtk_menu_get_monitor (GtkMenu *menu)
5231 GtkMenuPrivate *priv;
5232 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5234 priv = gtk_menu_get_private (menu);
5236 return priv->monitor_num;
5240 * gtk_menu_get_for_attach_widget:
5241 * @widget: a #GtkWidget
5243 * Returns a list of the menus which are attached to this widget.
5244 * This list is owned by GTK+ and must not be modified.
5246 * Return value: the list of menus attached to his widget.
5251 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5255 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5257 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5262 gtk_menu_grab_notify (GtkWidget *widget,
5263 gboolean was_grabbed)
5265 GtkWidget *toplevel;
5266 GtkWindowGroup *group;
5269 toplevel = gtk_widget_get_toplevel (widget);
5270 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5271 grab = _gtk_window_group_get_current_grab (group);
5275 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
5276 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5280 #define __GTK_MENU_C__
5281 #include "gtkaliasdef.c"