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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 * @Short_description: A menu widget
30 * A #GtkMenu is a #GtkMenuShell that implements a drop down menu
31 * consisting of a list of #GtkMenuItem objects which can be navigated
32 * and activated by the user to perform application functions.
34 * A #GtkMenu is most commonly dropped down by activating a
35 * #GtkMenuItem in a #GtkMenuBar or popped up by activating a
36 * #GtkMenuItem in another #GtkMenu.
38 * A #GtkMenu can also be popped up by activating a #GtkOptionMenu.
39 * Other composite widgets such as the #GtkNotebook can pop up a
42 * Applications can display a #GtkMenu as a popup menu by calling the
43 * gtk_menu_popup() function. The example below shows how an application
44 * can pop up a menu when the 3rd mouse button is pressed.
47 * <title>Connecting the popup signal handler.</title>
49 * /<!---->* connect our handler which will popup the menu *<!---->/
50 * g_signal_connect_swapped (window, "button_press_event",
51 * G_CALLBACK (my_popup_handler), menu);
56 * <title>Signal handler which displays a popup menu.</title>
59 * my_popup_handler (GtkWidget *widget, GdkEvent *event)
62 * GdkEventButton *event_button;
64 * g_return_val_if_fail (widget != NULL, FALSE);
65 * g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
66 * g_return_val_if_fail (event != NULL, FALSE);
68 * /<!---->* The "widget" is the menu that was supplied when
69 * * g_signal_connect_swapped() was called.
71 * menu = GTK_MENU (widget);
73 * if (event->type == GDK_BUTTON_PRESS)
75 * event_button = (GdkEventButton *) event;
76 * if (event_button->button == GDK_BUTTON_SECONDARY)
78 * gtk_menu_popup (menu, NULL, NULL, NULL, NULL,
79 * event_button->button, event_button->time);
94 #include <gobject/gvaluecollector.h>
96 #include "gtkaccellabel.h"
97 #include "gtkaccelmap.h"
98 #include "gtkadjustment.h"
99 #include "gtkbindings.h"
100 #include "gtkcheckmenuitem.h"
102 #include "gtkmarshalers.h"
103 #include "gtkmenuprivate.h"
104 #include "gtkmenuitemprivate.h"
105 #include "gtkmenushellprivate.h"
106 #include "gtkwindow.h"
108 #include "gtkscrollbar.h"
109 #include "gtksettings.h"
110 #include "gtkprivate.h"
111 #include "gtkwidgetpath.h"
112 #include "gtkwidgetprivate.h"
115 #include "gtktypebuiltins.h"
116 #include "gtkwidgetprivate.h"
118 #include "deprecated/gtktearoffmenuitem.h"
121 #include "a11y/gtkmenuaccessible.h"
123 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
124 * extends below the submenu
127 #define MENU_SCROLL_STEP1 8
128 #define MENU_SCROLL_STEP2 15
129 #define MENU_SCROLL_FAST_ZONE 8
130 #define MENU_SCROLL_TIMEOUT1 50
131 #define MENU_SCROLL_TIMEOUT2 20
133 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
134 #define ATTACHED_MENUS "gtk-attached-menus"
136 typedef struct _GtkMenuAttachData GtkMenuAttachData;
137 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
139 struct _GtkMenuAttachData
141 GtkWidget *attach_widget;
142 GtkMenuDetachFunc detacher;
145 struct _GtkMenuPopdownData
157 gint effective_left_attach;
158 gint effective_right_attach;
159 gint effective_top_attach;
160 gint effective_bottom_attach;
177 PROP_RESERVE_TOGGLE_SIZE
182 CHILD_PROP_LEFT_ATTACH,
183 CHILD_PROP_RIGHT_ATTACH,
184 CHILD_PROP_TOP_ATTACH,
185 CHILD_PROP_BOTTOM_ATTACH
188 static void gtk_menu_set_property (GObject *object,
192 static void gtk_menu_get_property (GObject *object,
196 static void gtk_menu_set_child_property(GtkContainer *container,
201 static void gtk_menu_get_child_property(GtkContainer *container,
206 static void gtk_menu_destroy (GtkWidget *widget);
207 static void gtk_menu_realize (GtkWidget *widget);
208 static void gtk_menu_unrealize (GtkWidget *widget);
209 static void gtk_menu_size_allocate (GtkWidget *widget,
210 GtkAllocation *allocation);
211 static void gtk_menu_show (GtkWidget *widget);
212 static gboolean gtk_menu_draw (GtkWidget *widget,
214 static gboolean gtk_menu_key_press (GtkWidget *widget,
216 static gboolean gtk_menu_scroll (GtkWidget *widget,
217 GdkEventScroll *event);
218 static gboolean gtk_menu_button_press (GtkWidget *widget,
219 GdkEventButton *event);
220 static gboolean gtk_menu_button_release (GtkWidget *widget,
221 GdkEventButton *event);
222 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
223 GdkEventMotion *event);
224 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
225 GdkEventCrossing *event);
226 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
227 GdkEventCrossing *event);
228 static void gtk_menu_scroll_to (GtkMenu *menu,
230 static void gtk_menu_grab_notify (GtkWidget *widget,
231 gboolean was_grabbed);
232 static gboolean gtk_menu_captured_event (GtkWidget *widget,
236 static void gtk_menu_stop_scrolling (GtkMenu *menu);
237 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
238 static gboolean gtk_menu_scroll_timeout (gpointer data);
240 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
241 GtkWidget *menu_item);
242 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
243 GtkWidget *menu_item);
244 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
247 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
249 static void gtk_menu_handle_scrolling (GtkMenu *menu,
254 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
256 static void gtk_menu_style_updated (GtkWidget *widget);
257 static gboolean gtk_menu_focus (GtkWidget *widget,
258 GtkDirectionType direction);
259 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
260 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
261 GtkMenuDirectionType direction);
262 static void gtk_menu_real_move_scroll (GtkMenu *menu,
265 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
266 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
267 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
270 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
271 GtkMenuItem *menu_item,
272 GdkEventCrossing *event);
274 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
275 static void gtk_menu_show_all (GtkWidget *widget);
276 static void gtk_menu_position (GtkMenu *menu,
277 gboolean set_scroll_offset);
278 static void gtk_menu_reparent (GtkMenu *menu,
279 GtkWidget *new_parent,
281 static void gtk_menu_remove (GtkContainer *menu,
284 static void gtk_menu_update_title (GtkMenu *menu);
286 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
287 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
289 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
291 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
292 gboolean group_changed);
294 static void gtk_menu_get_preferred_width (GtkWidget *widget,
297 static void gtk_menu_get_preferred_height (GtkWidget *widget,
300 static void gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
306 static const gchar attach_data_key[] = "gtk-menu-attach-data";
308 static guint menu_signals[LAST_SIGNAL] = { 0 };
310 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
313 menu_queue_resize (GtkMenu *menu)
315 GtkMenuPrivate *priv = menu->priv;
317 priv->have_layout = FALSE;
318 gtk_widget_queue_resize (GTK_WIDGET (menu));
322 attach_info_free (AttachInfo *info)
324 g_slice_free (AttachInfo, info);
328 get_attach_info (GtkWidget *child)
330 GObject *object = G_OBJECT (child);
331 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
335 ai = g_slice_new0 (AttachInfo);
336 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
337 (GDestroyNotify) attach_info_free);
344 is_grid_attached (AttachInfo *ai)
346 return (ai->left_attach >= 0 &&
347 ai->right_attach >= 0 &&
348 ai->top_attach >= 0 &&
349 ai->bottom_attach >= 0);
353 menu_ensure_layout (GtkMenu *menu)
355 GtkMenuPrivate *priv = menu->priv;
357 if (!priv->have_layout)
359 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
363 gint max_right_attach;
364 gint max_bottom_attach;
366 /* Find extents of gridded portion
368 max_right_attach = 1;
369 max_bottom_attach = 0;
371 for (l = menu_shell->priv->children; l; l = l->next)
373 GtkWidget *child = l->data;
374 AttachInfo *ai = get_attach_info (child);
376 if (is_grid_attached (ai))
378 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
379 max_right_attach = MAX (max_right_attach, ai->right_attach);
383 /* Find empty rows */
384 row_occupied = g_malloc0 (max_bottom_attach);
386 for (l = menu_shell->priv->children; l; l = l->next)
388 GtkWidget *child = l->data;
389 AttachInfo *ai = get_attach_info (child);
391 if (is_grid_attached (ai))
395 for (i = ai->top_attach; i < ai->bottom_attach; i++)
396 row_occupied[i] = TRUE;
400 /* Lay non-grid-items out in those rows
403 for (l = menu_shell->priv->children; l; l = l->next)
405 GtkWidget *child = l->data;
406 AttachInfo *ai = get_attach_info (child);
408 if (!is_grid_attached (ai))
410 while (current_row < max_bottom_attach && row_occupied[current_row])
413 ai->effective_left_attach = 0;
414 ai->effective_right_attach = max_right_attach;
415 ai->effective_top_attach = current_row;
416 ai->effective_bottom_attach = current_row + 1;
422 ai->effective_left_attach = ai->left_attach;
423 ai->effective_right_attach = ai->right_attach;
424 ai->effective_top_attach = ai->top_attach;
425 ai->effective_bottom_attach = ai->bottom_attach;
429 g_free (row_occupied);
431 priv->n_rows = MAX (current_row, max_bottom_attach);
432 priv->n_columns = max_right_attach;
433 priv->have_layout = TRUE;
439 gtk_menu_get_n_columns (GtkMenu *menu)
441 GtkMenuPrivate *priv = menu->priv;
443 menu_ensure_layout (menu);
445 return priv->n_columns;
449 gtk_menu_get_n_rows (GtkMenu *menu)
451 GtkMenuPrivate *priv = menu->priv;
453 menu_ensure_layout (menu);
459 get_effective_child_attach (GtkWidget *child,
465 GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
468 menu_ensure_layout (menu);
470 ai = get_attach_info (child);
473 *l = ai->effective_left_attach;
475 *r = ai->effective_right_attach;
477 *t = ai->effective_top_attach;
479 *b = ai->effective_bottom_attach;
484 gtk_menu_class_init (GtkMenuClass *class)
486 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
487 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
488 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
489 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
490 GtkBindingSet *binding_set;
492 gobject_class->set_property = gtk_menu_set_property;
493 gobject_class->get_property = gtk_menu_get_property;
495 widget_class->destroy = gtk_menu_destroy;
496 widget_class->realize = gtk_menu_realize;
497 widget_class->unrealize = gtk_menu_unrealize;
498 widget_class->size_allocate = gtk_menu_size_allocate;
499 widget_class->show = gtk_menu_show;
500 widget_class->draw = gtk_menu_draw;
501 widget_class->scroll_event = gtk_menu_scroll;
502 widget_class->key_press_event = gtk_menu_key_press;
503 widget_class->button_press_event = gtk_menu_button_press;
504 widget_class->button_release_event = gtk_menu_button_release;
505 widget_class->motion_notify_event = gtk_menu_motion_notify;
506 widget_class->show_all = gtk_menu_show_all;
507 widget_class->enter_notify_event = gtk_menu_enter_notify;
508 widget_class->leave_notify_event = gtk_menu_leave_notify;
509 widget_class->style_updated = gtk_menu_style_updated;
510 widget_class->focus = gtk_menu_focus;
511 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
512 widget_class->grab_notify = gtk_menu_grab_notify;
513 widget_class->get_preferred_width = gtk_menu_get_preferred_width;
514 widget_class->get_preferred_height = gtk_menu_get_preferred_height;
515 widget_class->get_preferred_height_for_width = gtk_menu_get_preferred_height_for_width;
517 container_class->remove = gtk_menu_remove;
518 container_class->get_child_property = gtk_menu_get_child_property;
519 container_class->set_child_property = gtk_menu_set_child_property;
521 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
522 menu_shell_class->deactivate = gtk_menu_deactivate;
523 menu_shell_class->select_item = gtk_menu_select_item;
524 menu_shell_class->insert = gtk_menu_real_insert;
525 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
526 menu_shell_class->move_current = gtk_menu_move_current;
529 * GtkMenu::move-scroll:
531 * @scroll_type: a #GtkScrollType
533 menu_signals[MOVE_SCROLL] =
534 g_signal_new_class_handler (I_("move-scroll"),
535 G_OBJECT_CLASS_TYPE (gobject_class),
536 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
537 G_CALLBACK (gtk_menu_real_move_scroll),
539 _gtk_marshal_VOID__ENUM,
541 GTK_TYPE_SCROLL_TYPE);
546 * The index of the currently selected menu item, or -1 if no
547 * menu item is selected.
551 g_object_class_install_property (gobject_class,
553 g_param_spec_int ("active",
555 P_("The currently selected menu item"),
557 GTK_PARAM_READWRITE));
560 * GtkMenu:accel-group:
562 * The accel group holding accelerators for the menu.
566 g_object_class_install_property (gobject_class,
568 g_param_spec_object ("accel-group",
570 P_("The accel group holding accelerators for the menu"),
571 GTK_TYPE_ACCEL_GROUP,
572 GTK_PARAM_READWRITE));
575 * GtkMenu:accel-path:
577 * An accel path used to conveniently construct accel paths of child items.
581 g_object_class_install_property (gobject_class,
583 g_param_spec_string ("accel-path",
585 P_("An accel path used to conveniently construct accel paths of child items"),
587 GTK_PARAM_READWRITE));
590 * GtkMenu:attach-widget:
592 * The widget the menu is attached to. Setting this property attaches
593 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
594 * use gtk_menu_attach_to_widget() directly.
598 g_object_class_install_property (gobject_class,
600 g_param_spec_object ("attach-widget",
602 P_("The widget the menu is attached to"),
604 GTK_PARAM_READWRITE));
606 g_object_class_install_property (gobject_class,
608 g_param_spec_string ("tearoff-title",
610 P_("A title that may be displayed by the window manager when this menu is torn-off"),
612 GTK_PARAM_READWRITE));
615 * GtkMenu:tearoff-state:
617 * A boolean that indicates whether the menu is torn-off.
621 g_object_class_install_property (gobject_class,
623 g_param_spec_boolean ("tearoff-state",
625 P_("A boolean that indicates whether the menu is torn-off"),
627 GTK_PARAM_READWRITE));
632 * The monitor the menu will be popped up on.
636 g_object_class_install_property (gobject_class,
638 g_param_spec_int ("monitor",
640 P_("The monitor the menu will be popped up on"),
642 GTK_PARAM_READWRITE));
644 gtk_widget_class_install_style_property (widget_class,
645 g_param_spec_int ("vertical-padding",
646 P_("Vertical Padding"),
647 P_("Extra space at the top and bottom of the menu"),
651 GTK_PARAM_READABLE));
654 * GtkMenu:reserve-toggle-size:
656 * A boolean that indicates whether the menu reserves space for
657 * toggles and icons, regardless of their actual presence.
659 * This property should only be changed from its default value
660 * for special-purposes such as tabular menus. Regular menus that
661 * are connected to a menu bar or context menus should reserve
662 * toggle space for consistency.
666 g_object_class_install_property (gobject_class,
667 PROP_RESERVE_TOGGLE_SIZE,
668 g_param_spec_boolean ("reserve-toggle-size",
669 P_("Reserve Toggle Size"),
670 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
672 GTK_PARAM_READWRITE));
674 gtk_widget_class_install_style_property (widget_class,
675 g_param_spec_int ("horizontal-padding",
676 P_("Horizontal Padding"),
677 P_("Extra space at the left and right edges of the menu"),
681 GTK_PARAM_READABLE));
683 gtk_widget_class_install_style_property (widget_class,
684 g_param_spec_int ("vertical-offset",
685 P_("Vertical Offset"),
686 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
690 GTK_PARAM_READABLE));
692 gtk_widget_class_install_style_property (widget_class,
693 g_param_spec_int ("horizontal-offset",
694 P_("Horizontal Offset"),
695 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
699 GTK_PARAM_READABLE));
701 gtk_widget_class_install_style_property (widget_class,
702 g_param_spec_boolean ("double-arrows",
704 P_("When scrolling, always show both arrows."),
706 GTK_PARAM_READABLE));
709 * GtkMenu:arrow-placement:
711 * Indicates where scroll arrows should be placed.
715 gtk_widget_class_install_style_property (widget_class,
716 g_param_spec_enum ("arrow-placement",
717 P_("Arrow Placement"),
718 P_("Indicates where scroll arrows should be placed"),
719 GTK_TYPE_ARROW_PLACEMENT,
721 GTK_PARAM_READABLE));
723 gtk_container_class_install_child_property (container_class,
724 CHILD_PROP_LEFT_ATTACH,
725 g_param_spec_int ("left-attach",
727 P_("The column number to attach the left side of the child to"),
729 GTK_PARAM_READWRITE));
731 gtk_container_class_install_child_property (container_class,
732 CHILD_PROP_RIGHT_ATTACH,
733 g_param_spec_int ("right-attach",
735 P_("The column number to attach the right side of the child to"),
737 GTK_PARAM_READWRITE));
739 gtk_container_class_install_child_property (container_class,
740 CHILD_PROP_TOP_ATTACH,
741 g_param_spec_int ("top-attach",
743 P_("The row number to attach the top of the child to"),
745 GTK_PARAM_READWRITE));
747 gtk_container_class_install_child_property (container_class,
748 CHILD_PROP_BOTTOM_ATTACH,
749 g_param_spec_int ("bottom-attach",
751 P_("The row number to attach the bottom of the child to"),
753 GTK_PARAM_READWRITE));
756 * GtkMenu:arrow-scaling:
758 * Arbitrary constant to scale down the size of the scroll arrow.
762 gtk_widget_class_install_style_property (widget_class,
763 g_param_spec_float ("arrow-scaling",
765 P_("Arbitrary constant to scale down the size of the scroll arrow"),
767 GTK_PARAM_READABLE));
769 binding_set = gtk_binding_set_by_class (class);
770 gtk_binding_entry_add_signal (binding_set,
772 I_("move-current"), 1,
773 GTK_TYPE_MENU_DIRECTION_TYPE,
775 gtk_binding_entry_add_signal (binding_set,
778 GTK_TYPE_MENU_DIRECTION_TYPE,
780 gtk_binding_entry_add_signal (binding_set,
783 GTK_TYPE_MENU_DIRECTION_TYPE,
785 gtk_binding_entry_add_signal (binding_set,
788 GTK_TYPE_MENU_DIRECTION_TYPE,
790 gtk_binding_entry_add_signal (binding_set,
793 GTK_TYPE_MENU_DIRECTION_TYPE,
794 GTK_MENU_DIR_PARENT);
795 gtk_binding_entry_add_signal (binding_set,
798 GTK_TYPE_MENU_DIRECTION_TYPE,
799 GTK_MENU_DIR_PARENT);
800 gtk_binding_entry_add_signal (binding_set,
803 GTK_TYPE_MENU_DIRECTION_TYPE,
805 gtk_binding_entry_add_signal (binding_set,
808 GTK_TYPE_MENU_DIRECTION_TYPE,
810 gtk_binding_entry_add_signal (binding_set,
813 GTK_TYPE_SCROLL_TYPE,
815 gtk_binding_entry_add_signal (binding_set,
818 GTK_TYPE_SCROLL_TYPE,
820 gtk_binding_entry_add_signal (binding_set,
823 GTK_TYPE_SCROLL_TYPE,
825 gtk_binding_entry_add_signal (binding_set,
828 GTK_TYPE_SCROLL_TYPE,
830 gtk_binding_entry_add_signal (binding_set,
833 GTK_TYPE_SCROLL_TYPE,
835 gtk_binding_entry_add_signal (binding_set,
836 GDK_KEY_KP_Page_Up, 0,
838 GTK_TYPE_SCROLL_TYPE,
840 gtk_binding_entry_add_signal (binding_set,
841 GDK_KEY_Page_Down, 0,
843 GTK_TYPE_SCROLL_TYPE,
844 GTK_SCROLL_PAGE_DOWN);
845 gtk_binding_entry_add_signal (binding_set,
846 GDK_KEY_KP_Page_Down, 0,
848 GTK_TYPE_SCROLL_TYPE,
849 GTK_SCROLL_PAGE_DOWN);
851 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
853 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_MENU_ACCESSIBLE);
858 gtk_menu_set_property (GObject *object,
863 GtkMenu *menu = GTK_MENU (object);
868 gtk_menu_set_active (menu, g_value_get_int (value));
870 case PROP_ACCEL_GROUP:
871 gtk_menu_set_accel_group (menu, g_value_get_object (value));
873 case PROP_ACCEL_PATH:
874 gtk_menu_set_accel_path (menu, g_value_get_string (value));
876 case PROP_ATTACH_WIDGET:
880 widget = gtk_menu_get_attach_widget (menu);
882 gtk_menu_detach (menu);
884 widget = (GtkWidget*) g_value_get_object (value);
886 gtk_menu_attach_to_widget (menu, widget, NULL);
889 case PROP_TEAROFF_STATE:
890 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
892 case PROP_TEAROFF_TITLE:
893 gtk_menu_set_title (menu, g_value_get_string (value));
896 gtk_menu_set_monitor (menu, g_value_get_int (value));
898 case PROP_RESERVE_TOGGLE_SIZE:
899 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
902 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
908 gtk_menu_get_property (GObject *object,
913 GtkMenu *menu = GTK_MENU (object);
918 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->priv->children, gtk_menu_get_active (menu)));
920 case PROP_ACCEL_GROUP:
921 g_value_set_object (value, gtk_menu_get_accel_group (menu));
923 case PROP_ACCEL_PATH:
924 g_value_set_string (value, gtk_menu_get_accel_path (menu));
926 case PROP_ATTACH_WIDGET:
927 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
929 case PROP_TEAROFF_STATE:
930 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
932 case PROP_TEAROFF_TITLE:
933 g_value_set_string (value, gtk_menu_get_title (menu));
936 g_value_set_int (value, gtk_menu_get_monitor (menu));
938 case PROP_RESERVE_TOGGLE_SIZE:
939 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
942 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
948 gtk_menu_set_child_property (GtkContainer *container,
954 GtkMenu *menu = GTK_MENU (container);
955 AttachInfo *ai = get_attach_info (child);
959 case CHILD_PROP_LEFT_ATTACH:
960 ai->left_attach = g_value_get_int (value);
962 case CHILD_PROP_RIGHT_ATTACH:
963 ai->right_attach = g_value_get_int (value);
965 case CHILD_PROP_TOP_ATTACH:
966 ai->top_attach = g_value_get_int (value);
968 case CHILD_PROP_BOTTOM_ATTACH:
969 ai->bottom_attach = g_value_get_int (value);
973 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
977 menu_queue_resize (menu);
981 gtk_menu_get_child_property (GtkContainer *container,
987 AttachInfo *ai = get_attach_info (child);
991 case CHILD_PROP_LEFT_ATTACH:
992 g_value_set_int (value, ai->left_attach);
994 case CHILD_PROP_RIGHT_ATTACH:
995 g_value_set_int (value, ai->right_attach);
997 case CHILD_PROP_TOP_ATTACH:
998 g_value_set_int (value, ai->top_attach);
1000 case CHILD_PROP_BOTTOM_ATTACH:
1001 g_value_set_int (value, ai->bottom_attach);
1005 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1011 gtk_menu_window_event (GtkWidget *window,
1015 gboolean handled = FALSE;
1017 g_object_ref (window);
1018 g_object_ref (menu);
1020 switch (event->type)
1023 case GDK_KEY_RELEASE:
1024 handled = gtk_widget_event (menu, event);
1026 case GDK_WINDOW_STATE:
1027 /* Window for the menu has been closed by the display server or by GDK.
1028 * Update the internal state as if the user had clicked outside the
1031 if (event->window_state.new_window_state & GDK_WINDOW_STATE_WITHDRAWN &&
1032 event->window_state.changed_mask & GDK_WINDOW_STATE_WITHDRAWN)
1033 gtk_menu_shell_deactivate (GTK_MENU_SHELL(menu));
1039 g_object_unref (window);
1040 g_object_unref (menu);
1046 gtk_menu_init (GtkMenu *menu)
1048 GtkMenuPrivate *priv;
1049 GtkStyleContext *context;
1051 priv = G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
1055 priv->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1056 "type", GTK_WINDOW_POPUP,
1059 "signal::event", gtk_menu_window_event, menu,
1060 "signal::destroy", gtk_widget_destroyed, &priv->toplevel,
1062 gtk_window_set_resizable (GTK_WINDOW (priv->toplevel), FALSE);
1063 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->toplevel), 0);
1065 /* Refloat the menu, so that reference counting for the menu isn't
1066 * affected by it being a child of the toplevel
1068 g_object_force_floating (G_OBJECT (menu));
1069 priv->needs_destruction_ref = TRUE;
1071 priv->monitor_num = -1;
1072 priv->drag_start_y = -1;
1074 context = gtk_widget_get_style_context (GTK_WIDGET (menu));
1075 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENU);
1077 _gtk_widget_set_captured_event_handler (GTK_WIDGET (menu), gtk_menu_captured_event);
1081 gtk_menu_destroy (GtkWidget *widget)
1083 GtkMenu *menu = GTK_MENU (widget);
1084 GtkMenuPrivate *priv = menu->priv;
1085 GtkMenuAttachData *data;
1087 gtk_menu_remove_scroll_timeout (menu);
1089 data = g_object_get_data (G_OBJECT (widget), attach_data_key);
1091 gtk_menu_detach (menu);
1093 gtk_menu_stop_navigating_submenu (menu);
1095 if (priv->old_active_menu_item)
1096 g_clear_object (&priv->old_active_menu_item);
1098 /* Add back the reference count for being a child */
1099 if (priv->needs_destruction_ref)
1101 priv->needs_destruction_ref = FALSE;
1102 g_object_ref (widget);
1105 if (priv->accel_group)
1106 g_clear_object (&priv->accel_group);
1109 gtk_widget_destroy (priv->toplevel);
1111 if (priv->tearoff_window)
1112 gtk_widget_destroy (priv->tearoff_window);
1116 g_free (priv->heights);
1117 priv->heights = NULL;
1122 g_free (priv->title);
1126 if (priv->position_func_data_destroy)
1128 priv->position_func_data_destroy (priv->position_func_data);
1129 priv->position_func_data = NULL;
1130 priv->position_func_data_destroy = NULL;
1133 GTK_WIDGET_CLASS (gtk_menu_parent_class)->destroy (widget);
1137 menu_change_screen (GtkMenu *menu,
1138 GdkScreen *new_screen)
1140 GtkMenuPrivate *priv = menu->priv;
1142 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1144 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1150 gtk_window_set_screen (GTK_WINDOW (priv->tearoff_window), new_screen);
1151 gtk_menu_position (menu, TRUE);
1154 gtk_window_set_screen (GTK_WINDOW (priv->toplevel), new_screen);
1155 priv->monitor_num = -1;
1159 attach_widget_screen_changed (GtkWidget *attach_widget,
1160 GdkScreen *previous_screen,
1163 if (gtk_widget_has_screen (attach_widget) &&
1164 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1165 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1169 * gtk_menu_attach_to_widget:
1171 * @attach_widget: the #GtkWidget that the menu will be attached to
1172 * @detacher: (scope async)(allow-none): the user supplied callback function
1173 * that will be called when the menu calls gtk_menu_detach()
1175 * Attaches the menu to the widget and provides a callback function
1176 * that will be invoked when the menu calls gtk_menu_detach() during
1180 gtk_menu_attach_to_widget (GtkMenu *menu,
1181 GtkWidget *attach_widget,
1182 GtkMenuDetachFunc detacher)
1184 GtkMenuAttachData *data;
1187 g_return_if_fail (GTK_IS_MENU (menu));
1188 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1190 /* keep this function in sync with gtk_widget_set_parent() */
1191 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1194 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1195 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1199 g_object_ref_sink (menu);
1201 data = g_slice_new (GtkMenuAttachData);
1202 data->attach_widget = attach_widget;
1204 g_signal_connect (attach_widget, "screen-changed",
1205 G_CALLBACK (attach_widget_screen_changed), menu);
1206 attach_widget_screen_changed (attach_widget, NULL, menu);
1208 data->detacher = detacher;
1209 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1210 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1211 if (!g_list_find (list, menu))
1212 list = g_list_prepend (list, menu);
1214 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1215 (GDestroyNotify) g_list_free);
1217 if (gtk_widget_get_state_flags (GTK_WIDGET (menu)) != 0)
1218 gtk_widget_set_state_flags (GTK_WIDGET (menu), 0, TRUE);
1220 /* Attach the widget to the toplevel window. */
1221 gtk_window_set_attached_to (GTK_WINDOW (menu->priv->toplevel), attach_widget);
1223 /* Fallback title for menu comes from attach widget */
1224 gtk_menu_update_title (menu);
1226 g_object_notify (G_OBJECT (menu), "attach-widget");
1230 * gtk_menu_get_attach_widget:
1233 * Returns the #GtkWidget that the menu is attached to.
1235 * Returns: (transfer none): the #GtkWidget that the menu is attached to
1238 gtk_menu_get_attach_widget (GtkMenu *menu)
1240 GtkMenuAttachData *data;
1242 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1244 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1246 return data->attach_widget;
1254 * Detaches the menu from the widget to which it had been attached.
1255 * This function will call the callback function, @detacher, provided
1256 * when the gtk_menu_attach_to_widget() function was called.
1259 gtk_menu_detach (GtkMenu *menu)
1261 GtkMenuAttachData *data;
1264 g_return_if_fail (GTK_IS_MENU (menu));
1266 /* keep this function in sync with gtk_widget_unparent() */
1267 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1270 g_warning ("gtk_menu_detach(): menu is not attached");
1273 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1275 /* Detach the toplevel window. */
1276 gtk_window_set_attached_to (GTK_WINDOW (menu->priv->toplevel), NULL);
1278 g_signal_handlers_disconnect_by_func (data->attach_widget,
1279 (gpointer) attach_widget_screen_changed,
1283 data->detacher (data->attach_widget, menu);
1284 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1285 list = g_list_remove (list, menu);
1287 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1288 (GDestroyNotify) g_list_free);
1290 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1292 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1293 gtk_widget_unrealize (GTK_WIDGET (menu));
1295 g_slice_free (GtkMenuAttachData, data);
1297 /* Fallback title for menu comes from attach widget */
1298 gtk_menu_update_title (menu);
1300 g_object_unref (menu);
1304 gtk_menu_remove (GtkContainer *container,
1307 GtkMenu *menu = GTK_MENU (container);
1308 GtkMenuPrivate *priv = menu->priv;
1310 /* Clear out old_active_menu_item if it matches the item we are removing */
1311 if (priv->old_active_menu_item == widget)
1312 g_clear_object (&priv->old_active_menu_item);
1314 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1316 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1318 menu_queue_resize (menu);
1324 * Creates a new #GtkMenu
1326 * Returns: a new #GtkMenu
1331 return g_object_new (GTK_TYPE_MENU, NULL);
1335 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1339 GtkMenu *menu = GTK_MENU (menu_shell);
1340 GtkMenuPrivate *priv = menu->priv;
1341 AttachInfo *ai = get_attach_info (child);
1343 ai->left_attach = -1;
1344 ai->right_attach = -1;
1345 ai->top_attach = -1;
1346 ai->bottom_attach = -1;
1348 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1349 gtk_widget_set_parent_window (child, priv->bin_window);
1351 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1353 menu_queue_resize (menu);
1357 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1359 GtkMenuPrivate *priv = menu->priv;
1365 cairo_surface_t *surface;
1366 cairo_pattern_t *pattern;
1369 priv->tearoff_active = FALSE;
1370 priv->saved_scroll_offset = priv->scroll_offset;
1372 window = gtk_widget_get_window (priv->tearoff_window);
1373 width = gdk_window_get_width (window);
1374 height = gdk_window_get_height (window);
1376 surface = gdk_window_create_similar_surface (window,
1377 CAIRO_CONTENT_COLOR,
1381 cr = cairo_create (surface);
1382 gdk_cairo_set_source_window (cr, window, 0, 0);
1386 gtk_widget_set_size_request (priv->tearoff_window, width, height);
1388 pattern = cairo_pattern_create_for_surface (surface);
1389 gdk_window_set_background_pattern (window, pattern);
1391 cairo_pattern_destroy (pattern);
1392 cairo_surface_destroy (surface);
1397 popup_grab_on_window (GdkWindow *window,
1398 GdkDevice *keyboard,
1400 guint32 activate_time)
1403 gdk_device_grab (keyboard, window,
1404 GDK_OWNERSHIP_WINDOW, TRUE,
1405 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1406 NULL, activate_time) != GDK_GRAB_SUCCESS)
1410 gdk_device_grab (pointer, window,
1411 GDK_OWNERSHIP_WINDOW, TRUE,
1412 GDK_SMOOTH_SCROLL_MASK |
1413 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1414 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1415 GDK_POINTER_MOTION_MASK,
1416 NULL, activate_time) != GDK_GRAB_SUCCESS)
1419 gdk_device_ungrab (keyboard, activate_time);
1428 * gtk_menu_popup_for_device:
1430 * @device: (allow-none): a #GdkDevice
1431 * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1432 * menu item, or %NULL
1433 * @parent_menu_item: (allow-none): the menu item whose activation triggered
1434 * the popup, or %NULL
1435 * @func: (allow-none): a user supplied function used to position the menu,
1437 * @data: (allow-none): user supplied data to be passed to @func
1438 * @destroy: (allow-none): destroy notify for @data
1439 * @button: the mouse button which was pressed to initiate the event
1440 * @activate_time: the time at which the activation event occurred
1442 * Displays a menu and makes it available for selection.
1444 * Applications can use this function to display context-sensitive menus,
1445 * and will typically supply %NULL for the @parent_menu_shell,
1446 * @parent_menu_item, @func, @data and @destroy parameters. The default
1447 * menu positioning function will position the menu at the current position
1448 * of @device (or its corresponding pointer).
1450 * The @button parameter should be the mouse button pressed to initiate
1451 * the menu popup. If the menu popup was initiated by something other than
1452 * a mouse button press, such as a mouse button release or a keypress,
1453 * @button should be 0.
1455 * The @activate_time parameter is used to conflict-resolve initiation of
1456 * concurrent requests for mouse/keyboard grab requests. To function
1457 * properly, this needs to be the time stamp of the user event (such as
1458 * a mouse click or key press) that caused the initiation of the popup.
1459 * Only if no such event is available, gtk_get_current_event_time() can
1465 gtk_menu_popup_for_device (GtkMenu *menu,
1467 GtkWidget *parent_menu_shell,
1468 GtkWidget *parent_menu_item,
1469 GtkMenuPositionFunc func,
1471 GDestroyNotify destroy,
1473 guint32 activate_time)
1475 GtkMenuPrivate *priv = menu->priv;
1477 GtkWidget *xgrab_shell;
1479 GdkEvent *current_event;
1480 GtkMenuShell *menu_shell;
1481 gboolean grab_keyboard;
1482 GtkWidget *parent_toplevel;
1483 GdkDevice *keyboard, *pointer, *source_device = NULL;
1485 g_return_if_fail (GTK_IS_MENU (menu));
1486 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1489 device = gtk_get_current_event_device ();
1493 GdkDisplay *display;
1494 GdkDeviceManager *device_manager;
1497 display = gtk_widget_get_display (GTK_WIDGET (menu));
1498 device_manager = gdk_display_get_device_manager (display);
1499 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1501 device = devices->data;
1503 g_list_free (devices);
1506 widget = GTK_WIDGET (menu);
1507 menu_shell = GTK_MENU_SHELL (menu);
1509 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1512 pointer = gdk_device_get_associated_device (device);
1517 keyboard = gdk_device_get_associated_device (device);
1520 menu_shell->priv->parent_menu_shell = parent_menu_shell;
1522 priv->seen_item_enter = FALSE;
1524 /* Find the last viewable ancestor, and make an X grab on it
1526 parent = GTK_WIDGET (menu);
1530 gboolean viewable = TRUE;
1531 GtkWidget *tmp = parent;
1535 if (!gtk_widget_get_mapped (tmp))
1540 tmp = gtk_widget_get_parent (tmp);
1544 xgrab_shell = parent;
1546 parent = GTK_MENU_SHELL (parent)->priv->parent_menu_shell;
1549 /* We want to receive events generated when we map the menu;
1550 * unfortunately, since there is probably already an implicit
1551 * grab in place from the button that the user used to pop up
1552 * the menu, we won't receive then -- in particular, the EnterNotify
1553 * when the menu pops up under the pointer.
1555 * If we are grabbing on a parent menu shell, no problem; just grab
1556 * on that menu shell first before popping up the window with
1557 * owner_events = TRUE.
1559 * When grabbing on the menu itself, things get more convoluted --
1560 * we do an explicit grab on a specially created window with
1561 * owner_events = TRUE, which we override further down with a
1562 * grab on the menu. (We can't grab on the menu until it is mapped;
1563 * we probably could just leave the grab on the other window,
1564 * with a little reorganization of the code in gtkmenu*).
1566 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1567 gtk_window_set_accept_focus (GTK_WINDOW (priv->toplevel), grab_keyboard);
1572 if (xgrab_shell && xgrab_shell != widget)
1574 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1576 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1577 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1582 GdkWindow *transfer_window;
1584 xgrab_shell = widget;
1585 transfer_window = menu_grab_transfer_window_get (menu);
1586 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1588 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1589 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1593 if (!GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab)
1595 /* We failed to make our pointer/keyboard grab.
1596 * Rather than leaving the user with a stuck up window,
1597 * we just abort here. Presumably the user will try again.
1599 menu_shell->priv->parent_menu_shell = NULL;
1600 menu_grab_transfer_window_destroy (menu);
1604 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1605 menu_shell->priv->active = TRUE;
1606 menu_shell->priv->button = button;
1608 /* If we are popping up the menu from something other than, a button
1609 * press then, as a heuristic, we ignore enter events for the menu
1610 * until we get a MOTION_NOTIFY.
1613 current_event = gtk_get_current_event ();
1616 if ((current_event->type != GDK_BUTTON_PRESS) &&
1617 (current_event->type != GDK_ENTER_NOTIFY))
1618 menu_shell->priv->ignore_enter = TRUE;
1620 source_device = gdk_event_get_source_device (current_event);
1621 gdk_event_free (current_event);
1624 menu_shell->priv->ignore_enter = TRUE;
1628 gtk_menu_tearoff_bg_copy (menu);
1630 gtk_menu_reparent (menu, priv->toplevel, FALSE);
1633 parent_toplevel = NULL;
1634 if (parent_menu_shell)
1635 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1636 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1638 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1640 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1643 /* Set transient for to get the right window group and parent */
1644 if (GTK_IS_WINDOW (parent_toplevel))
1645 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel),
1646 GTK_WINDOW (parent_toplevel));
1648 priv->parent_menu_item = parent_menu_item;
1649 priv->position_func = func;
1650 priv->position_func_data = data;
1651 priv->position_func_data_destroy = destroy;
1652 menu_shell->priv->activate_time = activate_time;
1654 /* We need to show the menu here rather in the init function
1655 * because code expects to be able to tell if the menu is onscreen
1656 * by looking at gtk_widget_get_visible (menu)
1658 gtk_widget_show (GTK_WIDGET (menu));
1660 /* Position the menu, possibly changing the size request
1662 gtk_menu_position (menu, TRUE);
1664 /* Compute the size of the toplevel and realize it so we
1665 * can scroll correctly.
1667 if (!gtk_widget_get_realized (GTK_WIDGET (menu)))
1669 GtkRequisition tmp_request;
1670 GtkAllocation tmp_allocation = { 0, };
1672 /* Instead of trusting the menu position function to queue a
1673 * resize when the menu goes out of bounds, invalidate the cached
1676 gtk_widget_queue_resize (GTK_WIDGET (menu));
1677 gtk_widget_get_preferred_size (priv->toplevel, &tmp_request, NULL);
1679 tmp_allocation.width = tmp_request.width;
1680 tmp_allocation.height = tmp_request.height;
1682 gtk_widget_size_allocate (priv->toplevel, &tmp_allocation);
1684 gtk_widget_realize (priv->toplevel);
1687 gtk_menu_scroll_to (menu, priv->scroll_offset);
1689 /* if no item is selected, select the first one */
1690 if (!menu_shell->priv->active_menu_item &&
1691 source_device && gdk_device_get_source (source_device) == GDK_SOURCE_TOUCHSCREEN)
1692 gtk_menu_shell_select_first (menu_shell, TRUE);
1694 /* Once everything is set up correctly, map the toplevel */
1695 gtk_widget_show (priv->toplevel);
1697 if (xgrab_shell == widget)
1698 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1700 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1702 if (parent_menu_shell)
1704 gboolean keyboard_mode;
1706 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1707 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1709 else if (menu_shell->priv->button == 0) /* a keynav-activated context menu */
1710 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1712 _gtk_menu_shell_update_mnemonics (menu_shell);
1718 * @parent_menu_shell: (allow-none): the menu shell containing the
1719 * triggering menu item, or %NULL
1720 * @parent_menu_item: (allow-none): the menu item whose activation
1721 * triggered the popup, or %NULL
1722 * @func: (scope async) (allow-none): a user supplied function used to position
1723 * the menu, or %NULL
1724 * @data: user supplied data to be passed to @func.
1725 * @button: the mouse button which was pressed to initiate the event.
1726 * @activate_time: the time at which the activation event occurred.
1728 * Displays a menu and makes it available for selection.
1730 * Applications can use this function to display context-sensitive
1731 * menus, and will typically supply %NULL for the @parent_menu_shell,
1732 * @parent_menu_item, @func and @data parameters. The default menu
1733 * positioning function will position the menu at the current mouse
1736 * The @button parameter should be the mouse button pressed to initiate
1737 * the menu popup. If the menu popup was initiated by something other
1738 * than a mouse button press, such as a mouse button release or a keypress,
1739 * @button should be 0.
1741 * The @activate_time parameter is used to conflict-resolve initiation
1742 * of concurrent requests for mouse/keyboard grab requests. To function
1743 * properly, this needs to be the timestamp of the user event (such as
1744 * a mouse click or key press) that caused the initiation of the popup.
1745 * Only if no such event is available, gtk_get_current_event_time() can
1749 gtk_menu_popup (GtkMenu *menu,
1750 GtkWidget *parent_menu_shell,
1751 GtkWidget *parent_menu_item,
1752 GtkMenuPositionFunc func,
1755 guint32 activate_time)
1757 g_return_if_fail (GTK_IS_MENU (menu));
1759 gtk_menu_popup_for_device (menu,
1764 button, activate_time);
1771 * Removes the menu from the screen.
1774 gtk_menu_popdown (GtkMenu *menu)
1776 GtkMenuPrivate *priv;
1777 GtkMenuShell *menu_shell;
1780 g_return_if_fail (GTK_IS_MENU (menu));
1782 menu_shell = GTK_MENU_SHELL (menu);
1785 menu_shell->priv->parent_menu_shell = NULL;
1786 menu_shell->priv->active = FALSE;
1787 menu_shell->priv->ignore_enter = FALSE;
1789 priv->have_position = FALSE;
1791 gtk_menu_stop_scrolling (menu);
1792 gtk_menu_stop_navigating_submenu (menu);
1794 if (menu_shell->priv->active_menu_item)
1796 if (priv->old_active_menu_item)
1797 g_object_unref (priv->old_active_menu_item);
1798 priv->old_active_menu_item = menu_shell->priv->active_menu_item;
1799 g_object_ref (priv->old_active_menu_item);
1802 gtk_menu_shell_deselect (menu_shell);
1804 /* The X Grab, if present, will automatically be removed
1805 * when we hide the window
1809 gtk_widget_hide (priv->toplevel);
1810 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel), NULL);
1813 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1817 gtk_widget_set_size_request (priv->tearoff_window, -1, -1);
1819 if (gtk_bin_get_child (GTK_BIN (priv->toplevel)))
1821 gtk_menu_reparent (menu, priv->tearoff_hbox, TRUE);
1825 /* We popped up the menu from the tearoff, so we need to
1826 * release the grab - we aren't actually hiding the menu.
1828 if (menu_shell->priv->have_xgrab && pointer)
1830 GdkDevice *keyboard;
1832 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1833 keyboard = gdk_device_get_associated_device (pointer);
1836 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1840 /* gtk_menu_popdown is called each time a menu item is selected from
1841 * a torn off menu. Only scroll back to the saved position if the
1842 * non-tearoff menu was popped down.
1844 if (!priv->tearoff_active)
1845 gtk_menu_scroll_to (menu, priv->saved_scroll_offset);
1846 priv->tearoff_active = TRUE;
1849 gtk_widget_hide (GTK_WIDGET (menu));
1851 menu_shell->priv->have_xgrab = FALSE;
1854 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1856 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1858 menu_grab_transfer_window_destroy (menu);
1862 * gtk_menu_get_active:
1865 * Returns the selected menu item from the menu. This is used by the
1868 * Returns: (transfer none): the #GtkMenuItem that was last selected
1869 * in the menu. If a selection has not yet been made, the
1870 * first menu item is selected.
1873 gtk_menu_get_active (GtkMenu *menu)
1875 GtkMenuPrivate *priv = menu->priv;
1879 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1881 if (!priv->old_active_menu_item)
1884 children = GTK_MENU_SHELL (menu)->priv->children;
1888 child = children->data;
1889 children = children->next;
1891 if (gtk_bin_get_child (GTK_BIN (child)))
1896 priv->old_active_menu_item = child;
1897 if (priv->old_active_menu_item)
1898 g_object_ref (priv->old_active_menu_item);
1901 return priv->old_active_menu_item;
1905 * gtk_menu_set_active:
1907 * @index: the index of the menu item to select. Iindex values are
1910 * Selects the specified menu item within the menu. This is used by
1911 * the #GtkOptionMenu and should not be used by anyone else.
1914 gtk_menu_set_active (GtkMenu *menu,
1917 GtkMenuPrivate *priv = menu->priv;
1921 g_return_if_fail (GTK_IS_MENU (menu));
1923 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->priv->children, index);
1926 child = tmp_list->data;
1927 if (gtk_bin_get_child (GTK_BIN (child)))
1929 if (priv->old_active_menu_item)
1930 g_object_unref (priv->old_active_menu_item);
1931 priv->old_active_menu_item = child;
1932 g_object_ref (priv->old_active_menu_item);
1938 * gtk_menu_set_accel_group:
1940 * @accel_group: (allow-none): the #GtkAccelGroup to be associated
1943 * Set the #GtkAccelGroup which holds global accelerators for the
1944 * menu. This accelerator group needs to also be added to all windows
1945 * that this menu is being used in with gtk_window_add_accel_group(),
1946 * in order for those windows to support all the accelerators
1947 * contained in this group.
1950 gtk_menu_set_accel_group (GtkMenu *menu,
1951 GtkAccelGroup *accel_group)
1953 GtkMenuPrivate *priv = menu->priv;
1954 g_return_if_fail (GTK_IS_MENU (menu));
1956 if (priv->accel_group != accel_group)
1958 if (priv->accel_group)
1959 g_object_unref (priv->accel_group);
1960 priv->accel_group = accel_group;
1961 if (priv->accel_group)
1962 g_object_ref (priv->accel_group);
1963 _gtk_menu_refresh_accel_paths (menu, TRUE);
1968 * gtk_menu_get_accel_group:
1971 * Gets the #GtkAccelGroup which holds global accelerators for the
1972 * menu. See gtk_menu_set_accel_group().
1974 * Returns: (transfer none): the #GtkAccelGroup associated with the menu
1977 gtk_menu_get_accel_group (GtkMenu *menu)
1979 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1981 return menu->priv->accel_group;
1985 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1988 /* Menu items chain here to figure whether they can activate their
1989 * accelerators. Unlike ordinary widgets, menus allow accel
1990 * activation even if invisible since that's the usual case for
1991 * submenus/popup-menus. however, the state of the attach widget
1992 * affects the "activeness" of the menu.
1994 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1997 return gtk_widget_can_activate_accel (awidget, signal_id);
1999 return gtk_widget_is_sensitive (widget);
2003 * gtk_menu_set_accel_path:
2004 * @menu: a valid #GtkMenu
2005 * @accel_path: (allow-none): a valid accelerator path
2007 * Sets an accelerator path for this menu from which accelerator paths
2008 * for its immediate children, its menu items, can be constructed.
2009 * The main purpose of this function is to spare the programmer the
2010 * inconvenience of having to call gtk_menu_item_set_accel_path() on
2011 * each menu item that should support runtime user changable accelerators.
2012 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
2013 * each menu item of this menu, that contains a label describing its
2014 * purpose, automatically gets an accel path assigned.
2016 * For example, a menu containing menu items "New" and "Exit", will, after
2017 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
2018 * has been called, assign its items the accel paths:
2019 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
2021 * Assigning accel paths to menu items then enables the user to change
2022 * their accelerators at runtime. More details about accelerator paths
2023 * and their default setups can be found at gtk_accel_map_add_entry().
2025 * Note that @accel_path string will be stored in a #GQuark. Therefore,
2026 * if you pass a static string, you can save some memory by interning
2027 * it first with g_intern_static_string().
2030 gtk_menu_set_accel_path (GtkMenu *menu,
2031 const gchar *accel_path)
2033 GtkMenuPrivate *priv = menu->priv;
2034 g_return_if_fail (GTK_IS_MENU (menu));
2037 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
2039 /* FIXME: accel_path should be defined as const gchar* */
2040 priv->accel_path = (gchar*)g_intern_string (accel_path);
2041 if (priv->accel_path)
2042 _gtk_menu_refresh_accel_paths (menu, FALSE);
2046 * gtk_menu_get_accel_path:
2047 * @menu: a valid #GtkMenu
2049 * Retrieves the accelerator path set on the menu.
2051 * Returns: the accelerator path set on the menu.
2056 gtk_menu_get_accel_path (GtkMenu *menu)
2058 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2060 return menu->priv->accel_path;
2065 gboolean group_changed;
2069 refresh_accel_paths_foreach (GtkWidget *widget,
2072 GtkMenuPrivate *priv;
2073 AccelPropagation *prop = data;
2075 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
2077 priv = prop->menu->priv;
2078 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2081 prop->group_changed);
2086 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
2087 gboolean group_changed)
2089 GtkMenuPrivate *priv = menu->priv;
2090 g_return_if_fail (GTK_IS_MENU (menu));
2092 if (priv->accel_path && priv->accel_group)
2094 AccelPropagation prop;
2097 prop.group_changed = group_changed;
2098 gtk_container_foreach (GTK_CONTAINER (menu),
2099 refresh_accel_paths_foreach,
2105 * gtk_menu_reposition:
2108 * Repositions the menu according to its position function.
2111 gtk_menu_reposition (GtkMenu *menu)
2113 g_return_if_fail (GTK_IS_MENU (menu));
2115 if (!menu->priv->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2116 gtk_menu_position (menu, FALSE);
2120 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2125 value = gtk_adjustment_get_value (adjustment);
2126 if (menu->priv->scroll_offset != value)
2127 gtk_menu_scroll_to (menu, value);
2131 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2134 GtkMenuPrivate *priv = menu->priv;
2135 GdkGeometry geometry_hints;
2137 if (!priv->tearoff_window)
2140 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2142 GtkRequisition requisition;
2144 gtk_widget_get_preferred_size (priv->tearoff_scrollbar,
2145 &requisition, NULL);
2146 width += requisition.width;
2149 geometry_hints.min_width = width;
2150 geometry_hints.max_width = width;
2152 geometry_hints.min_height = 0;
2153 geometry_hints.max_height = priv->requested_height;
2155 gtk_window_set_geometry_hints (GTK_WINDOW (priv->tearoff_window),
2158 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2162 gtk_menu_update_title (GtkMenu *menu)
2164 GtkMenuPrivate *priv = menu->priv;
2166 if (priv->tearoff_window)
2169 GtkWidget *attach_widget;
2171 title = gtk_menu_get_title (menu);
2174 attach_widget = gtk_menu_get_attach_widget (menu);
2175 if (GTK_IS_MENU_ITEM (attach_widget))
2177 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2178 if (GTK_IS_LABEL (child))
2179 title = gtk_label_get_text (GTK_LABEL (child));
2184 gtk_window_set_title (GTK_WINDOW (priv->tearoff_window), title);
2189 gtk_menu_get_toplevel (GtkWidget *menu)
2191 GtkWidget *attach, *toplevel;
2193 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2195 if (GTK_IS_MENU_ITEM (attach))
2196 attach = gtk_widget_get_parent (attach);
2198 if (GTK_IS_MENU (attach))
2199 return gtk_menu_get_toplevel (attach);
2200 else if (GTK_IS_WIDGET (attach))
2202 toplevel = gtk_widget_get_toplevel (attach);
2203 if (gtk_widget_is_toplevel (toplevel))
2211 tearoff_window_destroyed (GtkWidget *widget,
2214 gtk_menu_set_tearoff_state (menu, FALSE);
2218 * gtk_menu_set_tearoff_state:
2220 * @torn_off: If %TRUE, menu is displayed as a tearoff menu.
2222 * Changes the tearoff state of the menu. A menu is normally
2223 * displayed as drop down menu which persists as long as the menu is
2224 * active. It can also be displayed as a tearoff menu which persists
2225 * until it is closed or reattached.
2228 gtk_menu_set_tearoff_state (GtkMenu *menu,
2231 GtkMenuPrivate *priv = menu->priv;
2234 g_return_if_fail (GTK_IS_MENU (menu));
2236 if (priv->torn_off != torn_off)
2238 priv->torn_off = torn_off;
2239 priv->tearoff_active = torn_off;
2243 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2244 gtk_menu_popdown (menu);
2246 if (!priv->tearoff_window)
2248 GtkWidget *toplevel;
2250 priv->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2251 "type", GTK_WINDOW_TOPLEVEL,
2252 "screen", gtk_widget_get_screen (priv->toplevel),
2253 "app-paintable", TRUE,
2256 gtk_window_set_type_hint (GTK_WINDOW (priv->tearoff_window),
2257 GDK_WINDOW_TYPE_HINT_MENU);
2258 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->tearoff_window), 0);
2259 g_signal_connect (priv->tearoff_window, "destroy",
2260 G_CALLBACK (tearoff_window_destroyed), menu);
2261 g_signal_connect (priv->tearoff_window, "event",
2262 G_CALLBACK (gtk_menu_window_event), menu);
2264 gtk_menu_update_title (menu);
2266 gtk_widget_realize (priv->tearoff_window);
2268 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2269 if (toplevel != NULL)
2270 gtk_window_set_transient_for (GTK_WINDOW (priv->tearoff_window),
2271 GTK_WINDOW (toplevel));
2273 priv->tearoff_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2274 gtk_container_add (GTK_CONTAINER (priv->tearoff_window),
2275 priv->tearoff_hbox);
2277 height = gdk_window_get_height (gtk_widget_get_window (GTK_WIDGET (menu)));
2278 priv->tearoff_adjustment = gtk_adjustment_new (0,
2279 0, priv->requested_height,
2283 g_object_connect (priv->tearoff_adjustment,
2284 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2286 priv->tearoff_scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, priv->tearoff_adjustment);
2288 gtk_box_pack_end (GTK_BOX (priv->tearoff_hbox),
2289 priv->tearoff_scrollbar,
2292 if (gtk_adjustment_get_upper (priv->tearoff_adjustment) > height)
2293 gtk_widget_show (priv->tearoff_scrollbar);
2295 gtk_widget_show (priv->tearoff_hbox);
2298 gtk_menu_reparent (menu, priv->tearoff_hbox, FALSE);
2300 /* Update menu->requisition */
2301 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, NULL);
2303 gtk_menu_set_tearoff_hints (menu, gdk_window_get_width (gtk_widget_get_window (GTK_WIDGET (menu))));
2305 gtk_widget_realize (priv->tearoff_window);
2306 gtk_menu_position (menu, TRUE);
2308 gtk_widget_show (GTK_WIDGET (menu));
2309 gtk_widget_show (priv->tearoff_window);
2311 gtk_menu_scroll_to (menu, 0);
2316 gtk_widget_hide (GTK_WIDGET (menu));
2317 gtk_widget_hide (priv->tearoff_window);
2318 if (GTK_IS_CONTAINER (priv->toplevel))
2319 gtk_menu_reparent (menu, priv->toplevel, FALSE);
2320 gtk_widget_destroy (priv->tearoff_window);
2322 priv->tearoff_window = NULL;
2323 priv->tearoff_hbox = NULL;
2324 priv->tearoff_scrollbar = NULL;
2325 priv->tearoff_adjustment = NULL;
2328 g_object_notify (G_OBJECT (menu), "tearoff-state");
2333 * gtk_menu_get_tearoff_state:
2336 * Returns whether the menu is torn off.
2337 * See gtk_menu_set_tearoff_state().
2339 * Return value: %TRUE if the menu is currently torn off.
2342 gtk_menu_get_tearoff_state (GtkMenu *menu)
2344 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2346 return menu->priv->torn_off;
2350 * gtk_menu_set_title:
2352 * @title: a string containing the title for the menu
2354 * Sets the title string for the menu.
2356 * The title is displayed when the menu is shown as a tearoff
2357 * menu. If @title is %NULL, the menu will see if it is attached
2358 * to a parent menu item, and if so it will try to use the same
2359 * text as that menu item's label.
2362 gtk_menu_set_title (GtkMenu *menu,
2365 GtkMenuPrivate *priv = menu->priv;
2368 g_return_if_fail (GTK_IS_MENU (menu));
2370 old_title = priv->title;
2371 priv->title = g_strdup (title);
2374 gtk_menu_update_title (menu);
2375 g_object_notify (G_OBJECT (menu), "tearoff-title");
2379 * gtk_menu_get_title:
2382 * Returns the title of the menu. See gtk_menu_set_title().
2384 * Return value: the title of the menu, or %NULL if the menu
2385 * has no title set on it. This string is owned by GTK+
2386 * and should not be modified or freed.
2389 gtk_menu_get_title (GtkMenu *menu)
2391 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2393 return menu->priv->title;
2397 * gtk_menu_reorder_child:
2399 * @child: the #GtkMenuItem to move
2400 * @position: the new position to place @child.
2401 * Positions are numbered from 0 to n - 1
2403 * Moves @child to a new @position in the list of @menu
2407 gtk_menu_reorder_child (GtkMenu *menu,
2411 GtkMenuShell *menu_shell;
2413 g_return_if_fail (GTK_IS_MENU (menu));
2414 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2416 menu_shell = GTK_MENU_SHELL (menu);
2418 if (g_list_find (menu_shell->priv->children, child))
2420 menu_shell->priv->children = g_list_remove (menu_shell->priv->children, child);
2421 menu_shell->priv->children = g_list_insert (menu_shell->priv->children, child, position);
2423 menu_queue_resize (menu);
2428 gtk_menu_style_updated (GtkWidget *widget)
2430 GTK_WIDGET_CLASS (gtk_menu_parent_class)->style_updated (widget);
2432 if (gtk_widget_get_realized (widget))
2434 GtkMenu *menu = GTK_MENU (widget);
2435 GtkMenuPrivate *priv = menu->priv;
2436 GtkStyleContext *context;
2438 context = gtk_widget_get_style_context (widget);
2440 gtk_style_context_set_background (context, priv->bin_window);
2441 gtk_style_context_set_background (context, priv->view_window);
2442 gtk_style_context_set_background (context, gtk_widget_get_window (widget));
2447 get_arrows_border (GtkMenu *menu,
2450 GtkMenuPrivate *priv = menu->priv;
2451 guint scroll_arrow_height;
2452 GtkArrowPlacement arrow_placement;
2454 gtk_widget_style_get (GTK_WIDGET (menu),
2455 "scroll-arrow-vlength", &scroll_arrow_height,
2456 "arrow_placement", &arrow_placement,
2459 switch (arrow_placement)
2461 case GTK_ARROWS_BOTH:
2462 border->top = priv->upper_arrow_visible ? scroll_arrow_height : 0;
2463 border->bottom = priv->lower_arrow_visible ? scroll_arrow_height : 0;
2466 case GTK_ARROWS_START:
2467 border->top = (priv->upper_arrow_visible ||
2468 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2472 case GTK_ARROWS_END:
2474 border->bottom = (priv->upper_arrow_visible ||
2475 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2479 border->left = border->right = 0;
2483 get_menu_padding (GtkWidget *widget,
2486 GtkStyleContext *context;
2487 GtkStateFlags state;
2489 context = gtk_widget_get_style_context (widget);
2490 state = gtk_widget_get_state_flags (widget);
2492 gtk_style_context_get_padding (context, state, padding);
2496 gtk_menu_realize (GtkWidget *widget)
2498 GtkMenu *menu = GTK_MENU (widget);
2499 GtkMenuPrivate *priv = menu->priv;
2500 GtkAllocation allocation;
2501 GtkStyleContext *context;
2503 GdkWindowAttr attributes;
2504 gint attributes_mask;
2508 guint vertical_padding;
2509 guint horizontal_padding;
2510 GtkBorder arrow_border, padding;
2512 g_return_if_fail (GTK_IS_MENU (widget));
2514 gtk_widget_set_realized (widget, TRUE);
2516 gtk_widget_get_allocation (widget, &allocation);
2518 attributes.window_type = GDK_WINDOW_CHILD;
2519 attributes.x = allocation.x;
2520 attributes.y = allocation.y;
2521 attributes.width = allocation.width;
2522 attributes.height = allocation.height;
2523 attributes.wclass = GDK_INPUT_OUTPUT;
2524 attributes.visual = gtk_widget_get_visual (widget);
2525 attributes.event_mask = gtk_widget_get_events (widget);
2526 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2527 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2529 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2531 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2532 &attributes, attributes_mask);
2533 gtk_widget_set_window (widget, window);
2534 gdk_window_set_user_data (window, widget);
2536 get_menu_padding (widget, &padding);
2537 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2538 context = gtk_widget_get_style_context (widget);
2540 gtk_widget_style_get (GTK_WIDGET (menu),
2541 "vertical-padding", &vertical_padding,
2542 "horizontal-padding", &horizontal_padding,
2545 gtk_widget_get_allocation (widget, &allocation);
2547 attributes.x = border_width + padding.left + horizontal_padding;
2548 attributes.y = border_width + padding.top + vertical_padding;
2549 attributes.width = allocation.width -
2550 (2 * (border_width + horizontal_padding)) - padding.left - padding.right;
2551 attributes.height = allocation.height -
2552 (2 * (border_width + vertical_padding)) - padding.top - padding.bottom;
2554 get_arrows_border (menu, &arrow_border);
2555 attributes.y += arrow_border.top;
2556 attributes.height -= arrow_border.top;
2557 attributes.height -= arrow_border.bottom;
2559 attributes.width = MAX (1, attributes.width);
2560 attributes.height = MAX (1, attributes.height);
2562 priv->view_window = gdk_window_new (window,
2563 &attributes, attributes_mask);
2564 gdk_window_set_user_data (priv->view_window, menu);
2566 gtk_widget_get_allocation (widget, &allocation);
2570 attributes.width = allocation.width + (2 * (border_width + horizontal_padding)) +
2571 padding.left + padding.right;
2572 attributes.height = priv->requested_height - (2 * (border_width + vertical_padding)) +
2573 padding.top + padding.bottom;
2575 attributes.width = MAX (1, attributes.width);
2576 attributes.height = MAX (1, attributes.height);
2578 priv->bin_window = gdk_window_new (priv->view_window,
2579 &attributes, attributes_mask);
2580 gdk_window_set_user_data (priv->bin_window, menu);
2582 children = GTK_MENU_SHELL (menu)->priv->children;
2585 child = children->data;
2586 children = children->next;
2588 gtk_widget_set_parent_window (child, priv->bin_window);
2591 gtk_style_context_set_background (context, priv->bin_window);
2592 gtk_style_context_set_background (context, priv->view_window);
2593 gtk_style_context_set_background (context, window);
2595 if (GTK_MENU_SHELL (widget)->priv->active_menu_item)
2596 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2597 GTK_MENU_SHELL (widget)->priv->active_menu_item);
2599 gdk_window_show (priv->bin_window);
2600 gdk_window_show (priv->view_window);
2604 gtk_menu_focus (GtkWidget *widget,
2605 GtkDirectionType direction)
2607 /* A menu or its menu items cannot have focus */
2611 /* See notes in gtk_menu_popup() for information
2612 * about the "grab transfer window"
2615 menu_grab_transfer_window_get (GtkMenu *menu)
2617 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2620 GdkWindowAttr attributes;
2621 gint attributes_mask;
2623 attributes.x = -100;
2624 attributes.y = -100;
2625 attributes.width = 10;
2626 attributes.height = 10;
2627 attributes.window_type = GDK_WINDOW_TEMP;
2628 attributes.wclass = GDK_INPUT_ONLY;
2629 attributes.override_redirect = TRUE;
2630 attributes.event_mask = 0;
2632 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2634 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2635 &attributes, attributes_mask);
2636 gdk_window_set_user_data (window, menu);
2638 gdk_window_show (window);
2640 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2647 menu_grab_transfer_window_destroy (GtkMenu *menu)
2649 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2652 gdk_window_set_user_data (window, NULL);
2653 gdk_window_destroy (window);
2654 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2659 gtk_menu_unrealize (GtkWidget *widget)
2661 GtkMenu *menu = GTK_MENU (widget);
2662 GtkMenuPrivate *priv = menu->priv;
2664 menu_grab_transfer_window_destroy (menu);
2666 gdk_window_set_user_data (priv->view_window, NULL);
2667 gdk_window_destroy (priv->view_window);
2668 priv->view_window = NULL;
2670 gdk_window_set_user_data (priv->bin_window, NULL);
2671 gdk_window_destroy (priv->bin_window);
2672 priv->bin_window = NULL;
2674 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2678 calculate_line_heights (GtkMenu *menu,
2680 guint **ret_min_heights,
2681 guint **ret_nat_heights)
2684 GtkMenuPrivate *priv;
2685 GtkMenuShell *menu_shell;
2686 GtkWidget *child, *widget;
2688 guint horizontal_padding;
2697 widget = GTK_WIDGET (menu);
2698 menu_shell = GTK_MENU_SHELL (widget);
2700 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2701 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2702 n_heights = gtk_menu_get_n_rows (menu);
2703 n_columns = gtk_menu_get_n_columns (menu);
2704 avail_width = for_width - (2 * priv->toggle_size + priv->accel_size) * n_columns;
2706 gtk_widget_style_get (GTK_WIDGET (menu),
2707 "horizontal-padding", &horizontal_padding,
2709 get_menu_padding (widget, &padding);
2711 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2712 avail_width -= (border_width + horizontal_padding) * 2 + padding.left + padding.right;
2714 for (children = menu_shell->priv->children; children; children = children->next)
2719 gint child_min, child_nat;
2721 child = children->data;
2723 if (!gtk_widget_get_visible (child))
2726 get_effective_child_attach (child, &l, &r, &t, &b);
2728 part = avail_width / (r - l);
2730 gtk_widget_get_preferred_height_for_width (child, part,
2731 &child_min, &child_nat);
2733 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2735 part = MAX (child_min, toggle_size) / (b - t);
2736 min_heights[t] = MAX (min_heights[t], part);
2738 part = MAX (child_nat, toggle_size) / (b - t);
2739 nat_heights[t] = MAX (nat_heights[t], part);
2742 if (ret_min_heights)
2743 *ret_min_heights = min_heights;
2745 g_free (min_heights);
2747 if (ret_nat_heights)
2748 *ret_nat_heights = nat_heights;
2750 g_free (nat_heights);
2756 gtk_menu_size_allocate (GtkWidget *widget,
2757 GtkAllocation *allocation)
2760 GtkMenuPrivate *priv;
2761 GtkMenuShell *menu_shell;
2763 GtkAllocation child_allocation;
2768 guint vertical_padding;
2769 guint horizontal_padding;
2772 g_return_if_fail (GTK_IS_MENU (widget));
2773 g_return_if_fail (allocation != NULL);
2775 menu = GTK_MENU (widget);
2776 menu_shell = GTK_MENU_SHELL (widget);
2779 gtk_widget_set_allocation (widget, allocation);
2781 gtk_widget_style_get (GTK_WIDGET (menu),
2782 "vertical-padding", &vertical_padding,
2783 "horizontal-padding", &horizontal_padding,
2786 get_menu_padding (widget, &padding);
2787 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2789 g_free (priv->heights);
2790 priv->heights_length = calculate_line_heights (menu,
2795 /* refresh our cached height request */
2796 priv->requested_height = (2 * (border_width + vertical_padding)) +
2797 padding.top + padding.bottom;
2798 for (i = 0; i < priv->heights_length; i++)
2799 priv->requested_height += priv->heights[i];
2801 x = border_width + padding.left + horizontal_padding;
2802 y = border_width + padding.top + vertical_padding;
2803 width = allocation->width - (2 * (border_width + horizontal_padding)) -
2804 padding.left - padding.right;
2805 height = allocation->height - (2 * (border_width + vertical_padding)) -
2806 padding.top - padding.bottom;
2808 if (menu_shell->priv->active)
2809 gtk_menu_scroll_to (menu, priv->scroll_offset);
2811 if (!priv->tearoff_active)
2813 GtkBorder arrow_border;
2815 get_arrows_border (menu, &arrow_border);
2816 y += arrow_border.top;
2817 height -= arrow_border.top;
2818 height -= arrow_border.bottom;
2821 width = MAX (1, width);
2822 height = MAX (1, height);
2824 if (gtk_widget_get_realized (widget))
2826 gdk_window_move_resize (gtk_widget_get_window (widget),
2827 allocation->x, allocation->y,
2828 allocation->width, allocation->height);
2830 gdk_window_move_resize (priv->view_window, x, y, width, height);
2833 if (menu_shell->priv->children)
2835 gint base_width = width / gtk_menu_get_n_columns (menu);
2837 children = menu_shell->priv->children;
2840 child = children->data;
2841 children = children->next;
2843 if (gtk_widget_get_visible (child))
2848 get_effective_child_attach (child, &l, &r, &t, &b);
2850 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2853 tmp = gtk_menu_get_n_columns (menu) - l;
2854 l = gtk_menu_get_n_columns (menu) - r;
2858 child_allocation.width = (r - l) * base_width;
2859 child_allocation.height = 0;
2860 child_allocation.x = l * base_width;
2861 child_allocation.y = 0;
2863 for (i = 0; i < b; i++)
2866 child_allocation.y += priv->heights[i];
2868 child_allocation.height += priv->heights[i];
2871 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2874 gtk_widget_size_allocate (child, &child_allocation);
2875 gtk_widget_queue_draw (child);
2879 /* Resize the item window */
2880 if (gtk_widget_get_realized (widget))
2886 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2887 height += priv->heights[i];
2889 width = gtk_menu_get_n_columns (menu) * base_width;
2890 gdk_window_resize (priv->bin_window, width, height);
2893 if (priv->tearoff_active)
2895 if (height >= priv->requested_height)
2897 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2899 gtk_widget_hide (priv->tearoff_scrollbar);
2900 gtk_menu_set_tearoff_hints (menu, allocation->width);
2902 gtk_menu_scroll_to (menu, 0);
2907 gtk_adjustment_configure (priv->tearoff_adjustment,
2908 gtk_adjustment_get_value (priv->tearoff_adjustment),
2910 priv->requested_height,
2911 gtk_adjustment_get_step_increment (priv->tearoff_adjustment),
2912 gtk_adjustment_get_page_increment (priv->tearoff_adjustment),
2913 allocation->height);
2915 if (!gtk_widget_get_visible (priv->tearoff_scrollbar))
2917 gtk_widget_show (priv->tearoff_scrollbar);
2918 gtk_menu_set_tearoff_hints (menu, allocation->width);
2926 get_arrows_visible_area (GtkMenu *menu,
2927 GdkRectangle *border,
2928 GdkRectangle *upper,
2929 GdkRectangle *lower,
2932 GtkArrowPlacement arrow_placement;
2933 GtkWidget *widget = GTK_WIDGET (menu);
2935 guint vertical_padding;
2936 guint horizontal_padding;
2937 gint scroll_arrow_height;
2938 GtkBorder menu_padding;
2940 gtk_widget_style_get (widget,
2941 "vertical-padding", &vertical_padding,
2942 "horizontal-padding", &horizontal_padding,
2943 "scroll-arrow-vlength", &scroll_arrow_height,
2944 "arrow-placement", &arrow_placement,
2947 get_menu_padding (widget, &menu_padding);
2948 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2949 border->x = border_width + menu_padding.left + horizontal_padding;
2950 border->y = border_width + menu_padding.top + vertical_padding;
2951 border->width = gdk_window_get_width (gtk_widget_get_window (widget));
2952 border->height = gdk_window_get_height (gtk_widget_get_window (widget));
2954 switch (arrow_placement)
2956 case GTK_ARROWS_BOTH:
2957 upper->x = border->x;
2958 upper->y = border->y;
2959 upper->width = border->width - 2 * border->x;
2960 upper->height = scroll_arrow_height;
2962 lower->x = border->x;
2963 lower->y = border->height - border->y - scroll_arrow_height;
2964 lower->width = border->width - 2 * border->x;
2965 lower->height = scroll_arrow_height;
2968 case GTK_ARROWS_START:
2969 upper->x = border->x;
2970 upper->y = border->y;
2971 upper->width = (border->width - 2 * border->x) / 2;
2972 upper->height = scroll_arrow_height;
2974 lower->x = border->x + upper->width;
2975 lower->y = border->y;
2976 lower->width = (border->width - 2 * border->x) / 2;
2977 lower->height = scroll_arrow_height;
2980 case GTK_ARROWS_END:
2981 upper->x = border->x;
2982 upper->y = border->height - border->y - scroll_arrow_height;
2983 upper->width = (border->width - 2 * border->x) / 2;
2984 upper->height = scroll_arrow_height;
2986 lower->x = border->x + upper->width;
2987 lower->y = border->height - border->y - scroll_arrow_height;
2988 lower->width = (border->width - 2 * border->x) / 2;
2989 lower->height = scroll_arrow_height;
2993 g_assert_not_reached();
2994 upper->x = upper->y = upper->width = upper->height = 0;
2995 lower->x = lower->y = lower->width = lower->height = 0;
2998 *arrow_space = scroll_arrow_height - menu_padding.top - menu_padding.bottom;
3002 gtk_menu_draw (GtkWidget *widget,
3006 GtkMenuPrivate *priv;
3007 GtkStyleContext *context;
3008 GdkRectangle border;
3012 GtkBorder menu_padding;
3014 menu = GTK_MENU (widget);
3016 context = gtk_widget_get_style_context (widget);
3018 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
3019 get_menu_padding (widget, &menu_padding);
3021 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
3023 gfloat arrow_scaling;
3026 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
3027 arrow_size = arrow_scaling * arrow_space;
3029 gtk_render_background (context, cr, 0, 0,
3030 gtk_widget_get_allocated_width (widget),
3031 gtk_widget_get_allocated_height (widget));
3032 gtk_render_frame (context, cr, 0, 0,
3033 gtk_widget_get_allocated_width (widget),
3034 gtk_widget_get_allocated_height (widget));
3036 gtk_style_context_save (context);
3037 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
3039 if (priv->upper_arrow_visible && !priv->tearoff_active)
3041 gtk_style_context_save (context);
3042 gtk_style_context_set_state (context, priv->upper_arrow_state);
3044 gtk_render_background (context, cr,
3046 upper.width, upper.height);
3047 gtk_render_frame (context, cr,
3049 upper.width, upper.height);
3051 gtk_render_arrow (context, cr, 0,
3052 upper.x + (upper.width - arrow_size) / 2,
3053 upper.y + menu_padding.top + (arrow_space - arrow_size) / 2,
3056 gtk_style_context_restore (context);
3059 if (priv->lower_arrow_visible && !priv->tearoff_active)
3061 gtk_style_context_save (context);
3062 gtk_style_context_set_state (context, priv->lower_arrow_state);
3064 gtk_render_background (context, cr,
3066 lower.width, lower.height);
3067 gtk_render_frame (context, cr,
3069 lower.width, lower.height);
3071 gtk_render_arrow (context, cr, G_PI,
3072 lower.x + (lower.width - arrow_size) / 2,
3073 lower.y + menu_padding.bottom + (arrow_space - arrow_size) / 2,
3076 gtk_style_context_restore (context);
3079 gtk_style_context_restore (context);
3082 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
3084 gint y = -border.y + priv->scroll_offset;
3087 gtk_cairo_transform_to_window (cr, widget, priv->bin_window);
3089 if (!priv->tearoff_active)
3091 GtkBorder arrow_border;
3093 get_arrows_border (menu, &arrow_border);
3094 y -= arrow_border.top;
3097 gtk_render_background (context, cr,
3099 border.width, border.height);
3100 gtk_render_frame (context, cr,
3102 border.width, border.height);
3107 GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
3113 gtk_menu_show (GtkWidget *widget)
3115 GtkMenu *menu = GTK_MENU (widget);
3117 _gtk_menu_refresh_accel_paths (menu, FALSE);
3119 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3124 gtk_menu_get_preferred_width (GtkWidget *widget,
3129 GtkMenuShell *menu_shell;
3130 GtkMenuPrivate *priv;
3133 guint max_toggle_size;
3134 guint max_accel_width;
3135 guint horizontal_padding;
3137 gint child_min, child_nat;
3138 gint min_width, nat_width;
3141 menu = GTK_MENU (widget);
3142 menu_shell = GTK_MENU_SHELL (widget);
3145 min_width = nat_width = 0;
3147 max_toggle_size = 0;
3148 max_accel_width = 0;
3150 children = menu_shell->priv->children;
3157 child = children->data;
3158 children = children->next;
3160 if (! gtk_widget_get_visible (child))
3163 get_effective_child_attach (child, &l, &r, &t, &b);
3165 /* It's important to size_request the child
3166 * before doing the toggle size request, in
3167 * case the toggle size request depends on the size
3168 * request of a child of the child (e.g. for ImageMenuItem)
3170 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
3172 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3173 max_toggle_size = MAX (max_toggle_size, toggle_size);
3174 max_accel_width = MAX (max_accel_width,
3175 GTK_MENU_ITEM (child)->priv->accelerator_width);
3177 part = child_min / (r - l);
3178 min_width = MAX (min_width, part);
3180 part = child_nat / (r - l);
3181 nat_width = MAX (nat_width, part);
3184 /* If the menu doesn't include any images or check items
3185 * reserve the space so that all menus are consistent.
3186 * We only do this for 'ordinary' menus, not for combobox
3187 * menus or multi-column menus
3189 if (max_toggle_size == 0 &&
3190 gtk_menu_get_n_columns (menu) == 1 &&
3191 !priv->no_toggle_size)
3193 GtkStyleContext *context;
3194 GtkWidgetPath *check_path;
3195 guint toggle_spacing;
3196 guint indicator_size;
3198 context = gtk_style_context_new ();
3200 /* Create a GtkCheckMenuItem path, only to query indicator spacing */
3201 check_path = _gtk_widget_create_path (widget);
3202 gtk_widget_path_append_type (check_path, GTK_TYPE_CHECK_MENU_ITEM);
3204 gtk_style_context_set_path (context, check_path);
3205 gtk_widget_path_free (check_path);
3206 gtk_style_context_set_screen (context, gtk_widget_get_screen (widget));
3208 gtk_style_context_get_style (context,
3209 "toggle-spacing", &toggle_spacing,
3210 "indicator-size", &indicator_size,
3213 max_toggle_size = indicator_size + toggle_spacing;
3215 g_object_unref (context);
3218 min_width += 2 * max_toggle_size + max_accel_width;
3219 min_width *= gtk_menu_get_n_columns (menu);
3221 nat_width += 2 * max_toggle_size + max_accel_width;
3222 nat_width *= gtk_menu_get_n_columns (menu);
3224 gtk_widget_style_get (GTK_WIDGET (menu),
3225 "horizontal-padding", &horizontal_padding,
3228 get_menu_padding (widget, &padding);
3229 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3230 min_width += (2 * (border_width + horizontal_padding)) +
3231 padding.left + padding.right;
3232 nat_width += (2 * (border_width + horizontal_padding)) +
3233 padding.left + padding.right;
3235 priv->toggle_size = max_toggle_size;
3236 priv->accel_size = max_accel_width;
3239 *minimum_size = min_width;
3242 *natural_size = nat_width;
3244 /* Don't resize the tearoff if it is not active,
3245 * because it won't redraw (it is only a background pixmap).
3247 if (priv->tearoff_active)
3248 gtk_menu_set_tearoff_hints (menu, min_width);
3252 gtk_menu_get_preferred_height (GtkWidget *widget,
3258 /* Menus are height-for-width only, just return the height
3259 * for the minimum width
3261 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
3262 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
3266 gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
3272 GtkMenu *menu = GTK_MENU (widget);
3273 GtkMenuPrivate *priv = menu->priv;
3274 guint *min_heights, *nat_heights;
3275 guint vertical_padding, border_width;
3277 gint min_height, nat_height;
3279 gtk_widget_style_get (widget, "vertical-padding", &vertical_padding, NULL);
3280 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3281 get_menu_padding (widget, &padding);
3283 min_height = nat_height = (border_width + vertical_padding) * 2 +
3284 padding.top + padding.bottom;
3287 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3289 for (i = 0; i < n_heights; i++)
3291 min_height += min_heights[i];
3292 nat_height += nat_heights[i];
3295 if (priv->have_position)
3297 GdkScreen *screen = gtk_widget_get_screen (priv->toplevel);
3298 GdkRectangle monitor;
3300 gdk_screen_get_monitor_workarea (screen, priv->monitor_num, &monitor);
3302 if (priv->position_y + min_height > monitor.y + monitor.height)
3303 min_height = monitor.y + monitor.height - priv->position_y;
3305 if (priv->position_y + nat_height > monitor.y + monitor.height)
3306 nat_height = monitor.y + monitor.height - priv->position_y;
3308 if (priv->position_y < monitor.y)
3310 min_height -= monitor.y - priv->position_y;
3311 nat_height -= monitor.y - priv->position_y;
3316 *minimum_size = min_height;
3319 *natural_size = nat_height;
3321 g_free (min_heights);
3322 g_free (nat_heights);
3326 pointer_in_menu_window (GtkWidget *widget,
3330 GtkMenu *menu = GTK_MENU (widget);
3331 GtkMenuPrivate *priv = menu->priv;
3332 GtkAllocation allocation;
3334 if (gtk_widget_get_mapped (priv->toplevel))
3336 GtkMenuShell *menu_shell;
3337 gint window_x, window_y;
3339 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
3340 &window_x, &window_y);
3342 gtk_widget_get_allocation (widget, &allocation);
3343 if (x_root >= window_x && x_root < window_x + allocation.width &&
3344 y_root >= window_y && y_root < window_y + allocation.height)
3347 menu_shell = GTK_MENU_SHELL (widget);
3349 if (GTK_IS_MENU (menu_shell->priv->parent_menu_shell))
3350 return pointer_in_menu_window (menu_shell->priv->parent_menu_shell,
3358 gtk_menu_button_press (GtkWidget *widget,
3359 GdkEventButton *event)
3361 GdkDevice *source_device;
3362 GtkWidget *event_widget;
3365 if (event->type != GDK_BUTTON_PRESS)
3368 source_device = gdk_event_get_source_device ((GdkEvent *) event);
3369 event_widget = gtk_get_event_widget ((GdkEvent *) event);
3370 menu = GTK_MENU (widget);
3372 /* Don't pass down to menu shell if a non-menuitem part of the menu
3373 * was clicked. The check for the event_widget being a GtkMenuShell
3374 * works because we have the pointer grabbed on menu_shell->window
3375 * with owner_events=TRUE, so all events that are either outside
3376 * the menu or on its border are delivered relative to
3377 * menu_shell->window.
3379 if (GTK_IS_MENU_SHELL (event_widget) &&
3380 pointer_in_menu_window (widget, event->x_root, event->y_root))
3383 if (GTK_IS_MENU_ITEM (event_widget) &&
3384 gdk_device_get_source (source_device) == GDK_SOURCE_TOUCHSCREEN &&
3385 GTK_MENU_ITEM (event_widget)->priv->submenu != NULL &&
3386 !gtk_widget_is_drawable (GTK_MENU_ITEM (event_widget)->priv->submenu))
3387 menu->priv->ignore_button_release = TRUE;
3389 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3393 gtk_menu_button_release (GtkWidget *widget,
3394 GdkEventButton *event)
3396 GtkMenuPrivate *priv = GTK_MENU (widget)->priv;
3398 if (priv->ignore_button_release)
3400 priv->ignore_button_release = FALSE;
3404 if (event->type != GDK_BUTTON_RELEASE)
3407 /* Don't pass down to menu shell if a non-menuitem part of the menu
3408 * was clicked (see comment in button_press()).
3410 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3411 pointer_in_menu_window (widget, event->x_root, event->y_root))
3413 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3414 * bail out early here so it is in a consistent state for the
3415 * next button_press/button_release in GtkMenuShell.
3418 if (GTK_MENU_SHELL (widget)->priv->active)
3419 GTK_MENU_SHELL (widget)->priv->button = 0;
3424 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3427 static const gchar *
3428 get_accel_path (GtkWidget *menu_item,
3433 GClosure *accel_closure;
3434 GtkAccelGroup *accel_group;
3436 path = _gtk_widget_get_accel_path (menu_item, locked);
3439 path = GTK_MENU_ITEM (menu_item)->priv->accel_path;
3445 label = gtk_bin_get_child (GTK_BIN (menu_item));
3447 if (GTK_IS_ACCEL_LABEL (label))
3449 g_object_get (label,
3450 "accel-closure", &accel_closure,
3454 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3456 *locked = gtk_accel_group_get_is_locked (accel_group);
3466 gtk_menu_key_press (GtkWidget *widget,
3469 GtkMenuShell *menu_shell;
3471 gboolean delete = FALSE;
3472 gboolean can_change_accels;
3473 gchar *accel = NULL;
3474 guint accel_key, accel_mods;
3475 GdkModifierType consumed_modifiers;
3476 GdkDisplay *display;
3478 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3479 g_return_val_if_fail (event != NULL, FALSE);
3481 menu_shell = GTK_MENU_SHELL (widget);
3482 menu = GTK_MENU (widget);
3484 gtk_menu_stop_navigating_submenu (menu);
3486 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3489 display = gtk_widget_get_display (widget);
3491 g_object_get (gtk_widget_get_settings (widget),
3492 "gtk-menu-bar-accel", &accel,
3493 "gtk-can-change-accels", &can_change_accels,
3496 if (accel && *accel)
3499 GdkModifierType mods = 0;
3501 gtk_accelerator_parse (accel, &keyval, &mods);
3504 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3506 /* FIXME this is wrong, needs to be in the global accel resolution
3507 * thing, to properly consider i18n etc., but that probably requires
3508 * AccelGroup changes etc.
3510 if (event->keyval == keyval && (mods & event->state) == mods)
3512 gtk_menu_shell_cancel (menu_shell);
3520 switch (event->keyval)
3522 case GDK_KEY_Delete:
3523 case GDK_KEY_KP_Delete:
3524 case GDK_KEY_BackSpace:
3531 /* Figure out what modifiers went into determining the key symbol */
3532 _gtk_translate_keyboard_accel_state (gdk_keymap_get_for_display (display),
3533 event->hardware_keycode,
3535 gtk_accelerator_get_default_mod_mask (),
3537 &accel_key, NULL, NULL, &consumed_modifiers);
3539 accel_key = gdk_keyval_to_lower (accel_key);
3540 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3542 /* If lowercasing affects the keysym, then we need to include SHIFT
3543 * in the modifiers, We re-upper case when we match against the
3544 * keyval, but display and save in caseless form.
3546 if (accel_key != event->keyval)
3547 accel_mods |= GDK_SHIFT_MASK;
3549 /* Modify the accelerators */
3550 if (can_change_accels &&
3551 menu_shell->priv->active_menu_item &&
3552 gtk_bin_get_child (GTK_BIN (menu_shell->priv->active_menu_item)) && /* no separators */
3553 GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu == NULL && /* no submenus */
3554 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3556 GtkWidget *menu_item = menu_shell->priv->active_menu_item;
3557 gboolean locked, replace_accels = TRUE;
3560 path = get_accel_path (menu_item, &locked);
3561 if (!path || locked)
3563 /* Can't change accelerators on menu_items without paths
3564 * (basically, those items are accelerator-locked).
3566 gtk_widget_error_bell (widget);
3572 /* For the keys that act to delete the current setting,
3573 * we delete the current setting if there is one, otherwise,
3574 * we set the key as the accelerator.
3580 if (gtk_accel_map_lookup_entry (path, &key) &&
3581 (key.accel_key || key.accel_mods))
3587 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3591 /* We failed, probably because this key is in use
3592 * and locked already.
3594 gtk_widget_error_bell (widget);
3603 check_threshold (GtkWidget *widget,
3612 ABS (start_x - x) > THRESHOLD ||
3613 ABS (start_y - y) > THRESHOLD;
3617 definitely_within_item (GtkWidget *widget,
3621 GdkWindow *window = GTK_MENU_ITEM (widget)->priv->event_window;
3624 w = gdk_window_get_width (window);
3625 h = gdk_window_get_height (window);
3628 check_threshold (widget, 0, 0, x, y) &&
3629 check_threshold (widget, w - 1, 0, x, y) &&
3630 check_threshold (widget, w - 1, h - 1, x, y) &&
3631 check_threshold (widget, 0, h - 1, x, y);
3635 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3637 GtkMenuPrivate *priv = menu->priv;
3639 return priv->navigation_height && priv->navigation_width;
3643 gtk_menu_motion_notify (GtkWidget *widget,
3644 GdkEventMotion *event)
3646 GtkWidget *menu_item;
3648 GtkMenuShell *menu_shell;
3650 GdkDevice *source_device;
3652 gboolean need_enter;
3654 source_device = gdk_event_get_source_device ((GdkEvent *) event);
3656 if (GTK_IS_MENU (widget) &&
3657 gdk_device_get_source (source_device) != GDK_SOURCE_TOUCHSCREEN)
3659 GtkMenuPrivate *priv = GTK_MENU(widget)->priv;
3661 if (priv->ignore_button_release)
3662 priv->ignore_button_release = FALSE;
3664 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3668 /* We received the event for one of two reasons:
3670 * a) We are the active menu, and did gtk_grab_add()
3671 * b) The widget is a child of ours, and the event was propagated
3673 * Since for computation of navigation regions, we want the menu which
3674 * is the parent of the menu item, for a), we need to find that menu,
3675 * which may be different from 'widget'.
3677 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3678 parent = gtk_widget_get_parent (menu_item);
3679 if (!GTK_IS_MENU_ITEM (menu_item) ||
3680 !GTK_IS_MENU (parent))
3683 menu_shell = GTK_MENU_SHELL (parent);
3684 menu = GTK_MENU (menu_shell);
3686 if (definitely_within_item (menu_item, event->x, event->y))
3687 menu_shell->priv->activate_time = 0;
3689 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->priv->ignore_enter);
3691 /* Check to see if we are within an active submenu's navigation region
3693 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3696 /* Make sure we pop down if we enter a non-selectable menu item, so we
3697 * don't show a submenu when the cursor is outside the stay-up triangle.
3699 if (!_gtk_menu_item_is_selectable (menu_item))
3701 /* We really want to deselect, but this gives the menushell code
3702 * a chance to do some bookkeeping about the menuitem.
3704 gtk_menu_shell_select_item (menu_shell, menu_item);
3710 /* The menu is now sensitive to enter events on its items, but
3711 * was previously sensitive. So we fake an enter event.
3713 menu_shell->priv->ignore_enter = FALSE;
3715 if (event->x >= 0 && event->x < gdk_window_get_width (event->window) &&
3716 event->y >= 0 && event->y < gdk_window_get_height (event->window))
3718 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3721 send_event->crossing.window = g_object_ref (event->window);
3722 send_event->crossing.time = event->time;
3723 send_event->crossing.send_event = TRUE;
3724 send_event->crossing.x_root = event->x_root;
3725 send_event->crossing.y_root = event->y_root;
3726 send_event->crossing.x = event->x;
3727 send_event->crossing.y = event->y;
3728 send_event->crossing.state = event->state;
3729 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3731 /* We send the event to 'widget', the currently active menu,
3732 * instead of 'menu', the menu that the pointer is in. This
3733 * will ensure that the event will be ignored unless the
3734 * menuitem is a child of the active menu or some parent
3735 * menu of the active menu.
3737 result = gtk_widget_event (widget, send_event);
3738 gdk_event_free (send_event);
3748 get_double_arrows (GtkMenu *menu)
3750 GtkMenuPrivate *priv = menu->priv;
3751 gboolean double_arrows;
3752 GtkArrowPlacement arrow_placement;
3754 gtk_widget_style_get (GTK_WIDGET (menu),
3755 "double-arrows", &double_arrows,
3756 "arrow-placement", &arrow_placement,
3759 if (arrow_placement != GTK_ARROWS_BOTH)
3762 return double_arrows || (priv->initially_pushed_in &&
3763 priv->scroll_offset != 0);
3767 gtk_menu_scroll_by (GtkMenu *menu,
3770 GtkMenuPrivate *priv = menu->priv;
3771 GtkBorder arrow_border;
3775 gboolean double_arrows;
3777 widget = GTK_WIDGET (menu);
3778 offset = priv->scroll_offset + step;
3780 get_arrows_border (menu, &arrow_border);
3782 double_arrows = get_double_arrows (menu);
3784 /* If we scroll upward and the non-visible top part
3785 * is smaller than the scroll arrow it would be
3786 * pretty stupid to show the arrow and taking more
3787 * screen space than just scrolling to the top.
3790 if ((step < 0) && (offset < arrow_border.top))
3793 /* Don't scroll over the top if we weren't before: */
3794 if ((priv->scroll_offset >= 0) && (offset < 0))
3797 view_height = gdk_window_get_height (gtk_widget_get_window (widget));
3799 if (priv->scroll_offset == 0 &&
3800 view_height >= priv->requested_height)
3803 /* Don't scroll past the bottom if we weren't before: */
3804 if (priv->scroll_offset > 0)
3805 view_height -= arrow_border.top;
3807 /* When both arrows are always shown,
3808 * reduce view height even more.
3811 view_height -= arrow_border.bottom;
3813 if ((priv->scroll_offset + view_height <= priv->requested_height) &&
3814 (offset + view_height > priv->requested_height))
3815 offset = priv->requested_height - view_height;
3817 if (offset != priv->scroll_offset)
3818 gtk_menu_scroll_to (menu, offset);
3822 gtk_menu_scroll_timeout (gpointer data)
3826 menu = GTK_MENU (data);
3827 gtk_menu_scroll_by (menu, menu->priv->scroll_step);
3833 gtk_menu_scroll (GtkWidget *widget,
3834 GdkEventScroll *event)
3836 GtkMenu *menu = GTK_MENU (widget);
3838 switch (event->direction)
3840 case GDK_SCROLL_RIGHT:
3841 case GDK_SCROLL_DOWN:
3842 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3844 case GDK_SCROLL_LEFT:
3846 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3848 case GDK_SCROLL_SMOOTH:
3849 gtk_menu_scroll_by (menu, event->delta_y);
3857 get_arrows_sensitive_area (GtkMenu *menu,
3858 GdkRectangle *upper,
3859 GdkRectangle *lower)
3861 GtkArrowPlacement arrow_placement;
3862 GtkWidget *widget = GTK_WIDGET (menu);
3866 guint vertical_padding;
3868 gint scroll_arrow_height;
3871 window = gtk_widget_get_window (widget);
3872 width = gdk_window_get_width (window);
3873 height = gdk_window_get_height (window);
3875 gtk_widget_style_get (widget,
3876 "vertical-padding", &vertical_padding,
3877 "scroll-arrow-vlength", &scroll_arrow_height,
3878 "arrow-placement", &arrow_placement,
3881 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) + vertical_padding;
3882 get_menu_padding (widget, &padding);
3884 gdk_window_get_position (window, &win_x, &win_y);
3886 switch (arrow_placement)
3888 case GTK_ARROWS_BOTH:
3893 upper->width = width;
3894 upper->height = scroll_arrow_height + border + padding.top;
3900 lower->y = win_y + height - border - padding.bottom - scroll_arrow_height;
3901 lower->width = width;
3902 lower->height = scroll_arrow_height + border + padding.bottom;
3906 case GTK_ARROWS_START:
3911 upper->width = width / 2;
3912 upper->height = scroll_arrow_height + border + padding.top;
3917 lower->x = win_x + width / 2;
3919 lower->width = width / 2;
3920 lower->height = scroll_arrow_height + border + padding.bottom;
3924 case GTK_ARROWS_END:
3928 upper->y = win_y + height - border - scroll_arrow_height;
3929 upper->width = width / 2;
3930 upper->height = scroll_arrow_height + border + padding.top;
3935 lower->x = win_x + width / 2;
3936 lower->y = win_y + height - border - scroll_arrow_height;
3937 lower->width = width / 2;
3938 lower->height = scroll_arrow_height + border + padding.bottom;
3946 gtk_menu_handle_scrolling (GtkMenu *menu,
3952 GtkMenuPrivate *priv = menu->priv;
3953 GtkMenuShell *menu_shell;
3956 gboolean scroll_fast = FALSE;
3959 menu_shell = GTK_MENU_SHELL (menu);
3961 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
3966 /* upper arrow handling */
3968 get_arrows_sensitive_area (menu, &rect, NULL);
3971 if (priv->upper_arrow_visible && !priv->tearoff_active &&
3972 (x >= rect.x) && (x < rect.x + rect.width) &&
3973 (y >= rect.y) && (y < rect.y + rect.height))
3978 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
3980 gboolean arrow_pressed = FALSE;
3982 if (priv->upper_arrow_visible && !priv->tearoff_active)
3984 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3986 if (enter && in_arrow &&
3987 (!priv->upper_arrow_prelight ||
3988 priv->scroll_fast != scroll_fast))
3990 priv->upper_arrow_prelight = TRUE;
3991 priv->scroll_fast = scroll_fast;
3993 /* Deselect the active item so that
3994 * any submenus are popped down
3996 gtk_menu_shell_deselect (menu_shell);
3998 gtk_menu_remove_scroll_timeout (menu);
3999 priv->scroll_step = scroll_fast
4000 ? -MENU_SCROLL_STEP2
4001 : -MENU_SCROLL_STEP1;
4003 priv->scroll_timeout =
4004 gdk_threads_add_timeout (scroll_fast
4005 ? MENU_SCROLL_TIMEOUT2
4006 : MENU_SCROLL_TIMEOUT1,
4007 gtk_menu_scroll_timeout, menu);
4009 else if (!enter && !in_arrow && priv->upper_arrow_prelight)
4011 gtk_menu_stop_scrolling (menu);
4015 /* check if the button isn't insensitive before
4016 * changing it to something else.
4018 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4020 GtkStateFlags arrow_state = 0;
4023 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4025 if (priv->upper_arrow_prelight)
4026 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4028 if (arrow_state != priv->upper_arrow_state)
4030 priv->upper_arrow_state = arrow_state;
4032 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4038 /* lower arrow handling */
4040 get_arrows_sensitive_area (menu, NULL, &rect);
4043 if (priv->lower_arrow_visible && !priv->tearoff_active &&
4044 (x >= rect.x) && (x < rect.x + rect.width) &&
4045 (y >= rect.y) && (y < rect.y + rect.height))
4050 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4052 gboolean arrow_pressed = FALSE;
4054 if (priv->lower_arrow_visible && !priv->tearoff_active)
4056 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4058 if (enter && in_arrow &&
4059 (!priv->lower_arrow_prelight ||
4060 priv->scroll_fast != scroll_fast))
4062 priv->lower_arrow_prelight = TRUE;
4063 priv->scroll_fast = scroll_fast;
4065 /* Deselect the active item so that
4066 * any submenus are popped down
4068 gtk_menu_shell_deselect (menu_shell);
4070 gtk_menu_remove_scroll_timeout (menu);
4071 priv->scroll_step = scroll_fast
4073 : MENU_SCROLL_STEP1;
4075 priv->scroll_timeout =
4076 gdk_threads_add_timeout (scroll_fast
4077 ? MENU_SCROLL_TIMEOUT2
4078 : MENU_SCROLL_TIMEOUT1,
4079 gtk_menu_scroll_timeout, menu);
4081 else if (!enter && !in_arrow && priv->lower_arrow_prelight)
4083 gtk_menu_stop_scrolling (menu);
4087 /* check if the button isn't insensitive before
4088 * changing it to something else.
4090 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4092 GtkStateFlags arrow_state = 0;
4095 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4097 if (priv->lower_arrow_prelight)
4098 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4100 if (arrow_state != priv->lower_arrow_state)
4102 priv->lower_arrow_state = arrow_state;
4104 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4112 gtk_menu_enter_notify (GtkWidget *widget,
4113 GdkEventCrossing *event)
4115 GtkWidget *menu_item;
4117 GdkDevice *source_device;
4119 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4120 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4121 event->mode == GDK_CROSSING_STATE_CHANGED)
4124 source_device = gdk_event_get_source_device ((GdkEvent *) event);
4125 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4127 if (GTK_IS_MENU (widget) &&
4128 gdk_device_get_source (source_device) != GDK_SOURCE_TOUCHSCREEN)
4130 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4132 if (!menu_shell->priv->ignore_enter)
4133 gtk_menu_handle_scrolling (GTK_MENU (widget),
4134 event->x_root, event->y_root, TRUE, TRUE);
4137 if (gdk_device_get_source (source_device) != GDK_SOURCE_TOUCHSCREEN &&
4138 GTK_IS_MENU_ITEM (menu_item))
4140 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4142 if (GTK_IS_MENU (menu))
4144 GtkMenuPrivate *priv = (GTK_MENU (menu))->priv;
4145 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4147 if (priv->seen_item_enter)
4149 /* This is the second enter we see for an item
4150 * on this menu. This means a release should always
4153 menu_shell->priv->activate_time = 0;
4155 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4156 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4158 if (definitely_within_item (menu_item, event->x, event->y))
4160 /* This is an actual user-enter (ie. not a pop-under)
4161 * In this case, the user must either have entered
4162 * sufficiently far enough into the item, or he must move
4163 * far enough away from the enter point. (see
4164 * gtk_menu_motion_notify())
4166 menu_shell->priv->activate_time = 0;
4170 priv->seen_item_enter = TRUE;
4174 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4175 * will not correspond to the event widget's parent. Check to see
4176 * if we are in the parent's navigation region.
4178 parent = gtk_widget_get_parent (menu_item);
4179 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4180 gtk_menu_navigating_submenu (GTK_MENU (parent),
4181 event->x_root, event->y_root))
4184 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4188 gtk_menu_leave_notify (GtkWidget *widget,
4189 GdkEventCrossing *event)
4191 GtkMenuShell *menu_shell;
4193 GtkMenuItem *menu_item;
4194 GtkWidget *event_widget;
4195 GdkDevice *source_device;
4197 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4198 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4199 event->mode == GDK_CROSSING_STATE_CHANGED)
4202 menu = GTK_MENU (widget);
4203 menu_shell = GTK_MENU_SHELL (widget);
4205 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4208 source_device = gdk_event_get_source_device ((GdkEvent *) event);
4210 if (gdk_device_get_source (source_device) != GDK_SOURCE_TOUCHSCREEN)
4211 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4213 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4215 if (!GTK_IS_MENU_ITEM (event_widget))
4218 menu_item = GTK_MENU_ITEM (event_widget);
4220 /* Here we check to see if we're leaving an active menu item
4221 * with a submenu, in which case we enter submenu navigation mode.
4223 if (menu_shell->priv->active_menu_item != NULL
4224 && menu_item->priv->submenu != NULL
4225 && menu_item->priv->submenu_placement == GTK_LEFT_RIGHT)
4227 if (GTK_MENU_SHELL (menu_item->priv->submenu)->priv->active)
4229 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4232 else if (menu_item == GTK_MENU_ITEM (menu_shell->priv->active_menu_item))
4234 /* We are leaving an active menu item with nonactive submenu.
4235 * Deselect it so we don't surprise the user with by popping
4236 * up a submenu _after_ he left the item.
4238 gtk_menu_shell_deselect (menu_shell);
4243 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4247 pointer_on_menu_widget (GtkMenu *menu,
4251 GtkMenuPrivate *priv = menu->priv;
4252 GtkAllocation allocation;
4253 gint window_x, window_y;
4255 gtk_widget_get_allocation (GTK_WIDGET (menu), &allocation);
4256 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
4257 &window_x, &window_y);
4259 if (x_root >= window_x && x_root < window_x + allocation.width &&
4260 y_root >= window_y && y_root < window_y + allocation.height)
4267 gtk_menu_captured_event (GtkWidget *widget,
4270 GdkDevice *source_device;
4271 gboolean retval = FALSE;
4272 GtkMenuPrivate *priv;
4274 gdouble x_root, y_root;
4276 GdkModifierType state;
4278 menu = GTK_MENU (widget);
4281 if (!priv->upper_arrow_visible && !priv->lower_arrow_visible)
4284 source_device = gdk_event_get_source_device (event);
4285 gdk_event_get_root_coords (event, &x_root, &y_root);
4287 switch (event->type)
4289 case GDK_TOUCH_BEGIN:
4290 case GDK_BUTTON_PRESS:
4291 if ((!gdk_event_get_button (event, &button) || button == 1) &&
4292 gdk_device_get_source (source_device) == GDK_SOURCE_TOUCHSCREEN &&
4293 pointer_on_menu_widget (menu, x_root, y_root))
4295 priv->drag_start_y = event->button.y_root;
4296 priv->initial_drag_offset = priv->scroll_offset;
4297 priv->drag_scroll_started = FALSE;
4300 priv->drag_start_y = -1;
4302 priv->drag_already_pressed = TRUE;
4305 case GDK_BUTTON_RELEASE:
4306 if (priv->drag_scroll_started)
4308 priv->drag_scroll_started = FALSE;
4309 priv->drag_start_y = -1;
4310 priv->drag_already_pressed = FALSE;
4314 case GDK_TOUCH_UPDATE:
4315 case GDK_MOTION_NOTIFY:
4316 if ((!gdk_event_get_state (event, &state) || (state & GDK_BUTTON1_MASK)
4318 gdk_device_get_source (source_device) == GDK_SOURCE_TOUCHSCREEN)
4320 if (!priv->drag_already_pressed)
4322 if (pointer_on_menu_widget (menu, x_root, y_root))
4324 priv->drag_start_y = y_root;
4325 priv->initial_drag_offset = priv->scroll_offset;
4326 priv->drag_scroll_started = FALSE;
4329 priv->drag_start_y = -1;
4331 priv->drag_already_pressed = TRUE;
4334 if (priv->drag_start_y < 0 && !priv->drag_scroll_started)
4337 if (priv->drag_scroll_started)
4339 gint offset, view_height;
4340 GtkBorder arrow_border;
4343 y_diff = y_root - priv->drag_start_y;
4344 offset = priv->initial_drag_offset - y_diff;
4346 view_height = gdk_window_get_height (gtk_widget_get_window (widget));
4347 get_arrows_border (menu, &arrow_border);
4349 if (priv->upper_arrow_visible)
4350 view_height -= arrow_border.top;
4352 if (priv->lower_arrow_visible)
4353 view_height -= arrow_border.bottom;
4355 offset = CLAMP (offset, 0, priv->requested_height - view_height);
4356 gtk_menu_scroll_to (menu, offset);
4360 else if (gtk_drag_check_threshold (widget,
4361 0, priv->drag_start_y,
4364 priv->drag_scroll_started = TRUE;
4365 gtk_menu_shell_deselect (GTK_MENU_SHELL (menu));
4370 case GDK_ENTER_NOTIFY:
4371 case GDK_LEAVE_NOTIFY:
4372 if (priv->drag_scroll_started)
4383 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4385 GtkMenuPrivate *priv = menu->priv;
4387 priv->navigation_x = 0;
4388 priv->navigation_y = 0;
4389 priv->navigation_width = 0;
4390 priv->navigation_height = 0;
4392 if (priv->navigation_timeout)
4394 g_source_remove (priv->navigation_timeout);
4395 priv->navigation_timeout = 0;
4399 /* When the timeout is elapsed, the navigation region is destroyed
4400 * and the menuitem under the pointer (if any) is selected.
4403 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4405 GtkMenuPopdownData *popdown_data = user_data;
4406 GtkMenu *menu = popdown_data->menu;
4407 GtkMenuPrivate *priv = menu->priv;
4408 GdkWindow *child_window;
4410 gtk_menu_stop_navigating_submenu (menu);
4412 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4414 child_window = gdk_window_get_device_position (priv->bin_window,
4415 popdown_data->device,
4420 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4422 send_event->crossing.window = g_object_ref (child_window);
4423 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4424 send_event->crossing.send_event = TRUE;
4425 gdk_event_set_device (send_event, popdown_data->device);
4427 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4429 gdk_event_free (send_event);
4437 gtk_menu_navigating_submenu (GtkMenu *menu,
4441 GtkMenuPrivate *priv = menu->priv;
4444 if (!gtk_menu_has_navigation_triangle (menu))
4447 width = priv->navigation_width;
4448 height = priv->navigation_height;
4450 /* Check if x/y are in the triangle spanned by the navigation parameters */
4452 /* 1) Move the coordinates so the triangle starts at 0,0 */
4453 event_x -= priv->navigation_x;
4454 event_y -= priv->navigation_y;
4456 /* 2) Ensure both legs move along the positive axis */
4468 /* 3) Check that the given coordinate is inside the triangle. The formula
4469 * is a transformed form of this formula: x/w + y/h <= 1
4471 if (event_x >= 0 && event_y >= 0 &&
4472 event_x * height + event_y * width <= width * height)
4478 gtk_menu_stop_navigating_submenu (menu);
4484 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4485 GtkMenuItem *menu_item,
4486 GdkEventCrossing *event)
4488 GtkMenuPrivate *priv = menu->priv;
4489 gint submenu_left = 0;
4490 gint submenu_right = 0;
4491 gint submenu_top = 0;
4492 gint submenu_bottom = 0;
4494 GtkWidget *event_widget;
4495 GtkMenuPopdownData *popdown_data;
4498 g_return_if_fail (menu_item->priv->submenu != NULL);
4499 g_return_if_fail (event != NULL);
4501 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4503 window = gtk_widget_get_window (menu_item->priv->submenu);
4504 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4506 submenu_right = submenu_left + gdk_window_get_width (window);
4507 submenu_bottom = submenu_top + gdk_window_get_height (window);
4509 width = gdk_window_get_width (gtk_widget_get_window (event_widget));
4511 if (event->x >= 0 && event->x < width)
4515 gtk_menu_stop_navigating_submenu (menu);
4517 /* The navigation region is the triangle closest to the x/y
4518 * location of the rectangle. This is why the width or height
4521 if (menu_item->priv->submenu_direction == GTK_DIRECTION_RIGHT)
4524 priv->navigation_x = submenu_left;
4525 priv->navigation_width = event->x_root - submenu_left;
4530 priv->navigation_x = submenu_right;
4531 priv->navigation_width = event->x_root - submenu_right;
4537 priv->navigation_y = event->y_root;
4538 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4540 if (priv->navigation_height >= 0)
4546 priv->navigation_y = event->y_root;
4547 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4549 if (priv->navigation_height <= 0)
4553 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4554 "gtk-menu-popdown-delay", &popdown_delay,
4557 popdown_data = g_new (GtkMenuPopdownData, 1);
4558 popdown_data->menu = menu;
4559 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4561 priv->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4563 gtk_menu_stop_navigating_submenu_cb,
4565 (GDestroyNotify) g_free);
4570 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4574 g_return_if_fail (GTK_IS_MENU (menu_shell));
4576 parent = menu_shell->priv->parent_menu_shell;
4578 menu_shell->priv->activate_time = 0;
4579 gtk_menu_popdown (GTK_MENU (menu_shell));
4582 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4586 gtk_menu_position (GtkMenu *menu,
4587 gboolean set_scroll_offset)
4589 GtkMenuPrivate *priv = menu->priv;
4591 GtkRequisition requisition;
4595 GdkScreen *pointer_screen;
4596 GdkRectangle monitor;
4599 widget = GTK_WIDGET (menu);
4601 screen = gtk_widget_get_screen (widget);
4602 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4603 gdk_device_get_position (pointer, &pointer_screen, &x, &y);
4605 /* Realize so we have the proper width and heigh to figure out
4606 * the right place to popup the menu.
4608 gtk_widget_realize (priv->toplevel);
4609 requisition.width = gtk_widget_get_allocated_width (widget);
4610 requisition.height = gtk_widget_get_allocated_height (widget);
4612 if (pointer_screen != screen)
4614 /* Pointer is on a different screen; roughly center the
4615 * menu on the screen. If someone was using multiscreen
4616 * + Xinerama together they'd probably want something
4617 * fancier; but that is likely to be vanishingly rare.
4619 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4620 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4623 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4624 priv->initially_pushed_in = FALSE;
4626 /* Set the type hint here to allow custom position functions
4627 * to set a different hint
4629 if (!gtk_widget_get_visible (priv->toplevel))
4630 gtk_window_set_type_hint (GTK_WINDOW (priv->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4632 if (priv->position_func)
4634 (* priv->position_func) (menu, &x, &y, &priv->initially_pushed_in,
4635 priv->position_func_data);
4637 if (priv->monitor_num < 0)
4638 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4640 gdk_screen_get_monitor_workarea (screen, priv->monitor_num, &monitor);
4644 gint space_left, space_right, space_above, space_below;
4648 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4650 get_menu_padding (widget, &padding);
4652 /* The placement of popup menus horizontally works like this (with
4653 * RTL in parentheses)
4655 * - If there is enough room to the right (left) of the mouse cursor,
4656 * position the menu there.
4658 * - Otherwise, if if there is enough room to the left (right) of the
4659 * mouse cursor, position the menu there.
4661 * - Otherwise if the menu is smaller than the monitor, position it
4662 * on the side of the mouse cursor that has the most space available
4664 * - Otherwise (if there is simply not enough room for the menu on the
4665 * monitor), position it as far left (right) as possible.
4667 * Positioning in the vertical direction is similar: first try below
4668 * mouse cursor, then above.
4670 gdk_screen_get_monitor_workarea (screen, priv->monitor_num, &monitor);
4672 space_left = x - monitor.x;
4673 space_right = monitor.x + monitor.width - x - 1;
4674 space_above = y - monitor.y;
4675 space_below = monitor.y + monitor.height - y - 1;
4677 /* Position horizontally. */
4679 /* the amount of space we need to position the menu.
4680 * Note the menu is offset "thickness" pixels
4682 needed_width = requisition.width - padding.left;
4684 if (needed_width <= space_left ||
4685 needed_width <= space_right)
4687 if ((rtl && needed_width <= space_left) ||
4688 (!rtl && needed_width > space_right))
4691 x = x + padding.left - requisition.width + 1;
4695 /* position right */
4696 x = x - padding.right;
4699 /* x is clamped on-screen further down */
4701 else if (requisition.width <= monitor.width)
4703 /* the menu is too big to fit on either side of the mouse
4704 * cursor, but smaller than the monitor. Position it on
4705 * the side that has the most space
4707 if (space_left > space_right)
4715 x = monitor.x + monitor.width - requisition.width;
4718 else /* menu is simply too big for the monitor */
4723 x = monitor.x + monitor.width - requisition.width;
4732 /* Position vertically.
4733 * The algorithm is the same as above, but simpler
4734 * because we don't have to take RTL into account.
4736 needed_height = requisition.height - padding.top;
4738 if (needed_height <= space_above ||
4739 needed_height <= space_below)
4741 if (needed_height <= space_below)
4742 y = y - padding.top;
4744 y = y + padding.bottom - requisition.height + 1;
4746 y = CLAMP (y, monitor.y,
4747 monitor.y + monitor.height - requisition.height);
4749 else if (needed_height > space_below && needed_height > space_above)
4751 if (space_below >= space_above)
4752 y = monitor.y + monitor.height - requisition.height;
4764 if (y + requisition.height > monitor.y + monitor.height)
4766 if (priv->initially_pushed_in)
4767 scroll_offset += (monitor.y + monitor.height) - requisition.height - y;
4768 y = (monitor.y + monitor.height) - requisition.height;
4773 if (priv->initially_pushed_in)
4774 scroll_offset += monitor.y - y;
4778 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4780 if (GTK_MENU_SHELL (menu)->priv->active)
4782 priv->have_position = TRUE;
4783 priv->position_x = x;
4784 priv->position_y = y;
4787 if (scroll_offset != 0)
4789 GtkBorder arrow_border;
4791 get_arrows_border (menu, &arrow_border);
4792 scroll_offset += arrow_border.top;
4795 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->priv->active ? priv->toplevel : priv->tearoff_window),
4798 if (!GTK_MENU_SHELL (menu)->priv->active)
4800 gtk_window_resize (GTK_WINDOW (priv->tearoff_window),
4801 requisition.width, requisition.height);
4804 if (set_scroll_offset)
4805 priv->scroll_offset = scroll_offset;
4809 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4811 GtkMenuPrivate *priv = menu->priv;
4813 if (priv->scroll_timeout)
4815 g_source_remove (priv->scroll_timeout);
4816 priv->scroll_timeout = 0;
4821 gtk_menu_stop_scrolling (GtkMenu *menu)
4823 GtkMenuPrivate *priv = menu->priv;
4825 gtk_menu_remove_scroll_timeout (menu);
4826 priv->upper_arrow_prelight = FALSE;
4827 priv->lower_arrow_prelight = FALSE;
4831 gtk_menu_scroll_to (GtkMenu *menu,
4834 GtkMenuPrivate *priv = menu->priv;
4835 GtkBorder arrow_border, padding;
4838 gint view_width, view_height;
4841 guint vertical_padding;
4842 guint horizontal_padding;
4843 gboolean double_arrows;
4845 widget = GTK_WIDGET (menu);
4847 if (priv->tearoff_active && priv->tearoff_adjustment)
4848 gtk_adjustment_set_value (priv->tearoff_adjustment, offset);
4850 /* Move/resize the viewport according to arrows: */
4851 view_width = gtk_widget_get_allocated_width (widget);
4852 view_height = gtk_widget_get_allocated_height (widget);
4854 gtk_widget_style_get (GTK_WIDGET (menu),
4855 "vertical-padding", &vertical_padding,
4856 "horizontal-padding", &horizontal_padding,
4859 get_menu_padding (widget, &padding);
4860 double_arrows = get_double_arrows (menu);
4862 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4864 view_width -= (2 * (border_width + horizontal_padding)) + padding.left + padding.right;
4865 view_height -= (2 * (border_width + vertical_padding)) + padding.top + padding.bottom;
4866 menu_height = priv->requested_height - (2 * (border_width + vertical_padding)) -
4867 padding.top - padding.bottom;
4869 x = border_width + padding.left + horizontal_padding;
4870 y = border_width + padding.top + vertical_padding;
4872 if (double_arrows && !priv->tearoff_active)
4874 if (view_height < menu_height ||
4875 (offset > 0 && priv->scroll_offset > 0) ||
4876 (offset < 0 && priv->scroll_offset < 0))
4878 GtkStateFlags upper_arrow_previous_state = priv->upper_arrow_state;
4879 GtkStateFlags lower_arrow_previous_state = priv->lower_arrow_state;
4881 if (!priv->upper_arrow_visible || !priv->lower_arrow_visible)
4882 gtk_widget_queue_draw (GTK_WIDGET (menu));
4884 priv->upper_arrow_visible = priv->lower_arrow_visible = TRUE;
4886 get_arrows_border (menu, &arrow_border);
4887 y += arrow_border.top;
4888 view_height -= arrow_border.top;
4889 view_height -= arrow_border.bottom;
4892 priv->upper_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4895 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4897 if (priv->upper_arrow_prelight)
4898 priv->upper_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4900 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4903 if (offset >= menu_height - view_height)
4904 priv->lower_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4907 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4909 if (priv->lower_arrow_prelight)
4910 priv->lower_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4912 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4915 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4916 (priv->lower_arrow_state != lower_arrow_previous_state))
4917 gtk_widget_queue_draw (GTK_WIDGET (menu));
4919 if ((upper_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
4920 (priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
4922 /* At the upper border, possibly remove timeout */
4923 if (priv->scroll_step < 0)
4925 gtk_menu_stop_scrolling (menu);
4926 gtk_widget_queue_draw (GTK_WIDGET (menu));
4930 if ((lower_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
4931 (priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
4933 /* At the lower border, possibly remove timeout */
4934 if (priv->scroll_step > 0)
4936 gtk_menu_stop_scrolling (menu);
4937 gtk_widget_queue_draw (GTK_WIDGET (menu));
4941 else if (priv->upper_arrow_visible || priv->lower_arrow_visible)
4945 priv->upper_arrow_visible = priv->lower_arrow_visible = FALSE;
4946 priv->upper_arrow_prelight = priv->lower_arrow_prelight = FALSE;
4948 gtk_menu_stop_scrolling (menu);
4949 gtk_widget_queue_draw (GTK_WIDGET (menu));
4952 else if (!priv->tearoff_active)
4954 gboolean last_visible;
4956 last_visible = priv->upper_arrow_visible;
4957 priv->upper_arrow_visible = offset > 0;
4959 /* upper_arrow_visible may have changed, so requery the border */
4960 get_arrows_border (menu, &arrow_border);
4961 view_height -= arrow_border.top;
4963 if ((last_visible != priv->upper_arrow_visible) &&
4964 !priv->upper_arrow_visible)
4966 priv->upper_arrow_prelight = FALSE;
4968 /* If we hide the upper arrow, possibly remove timeout */
4969 if (priv->scroll_step < 0)
4971 gtk_menu_stop_scrolling (menu);
4972 gtk_widget_queue_draw (GTK_WIDGET (menu));
4976 last_visible = priv->lower_arrow_visible;
4977 priv->lower_arrow_visible = offset < menu_height - view_height;
4979 /* lower_arrow_visible may have changed, so requery the border */
4980 get_arrows_border (menu, &arrow_border);
4981 view_height -= arrow_border.bottom;
4983 if ((last_visible != priv->lower_arrow_visible) &&
4984 !priv->lower_arrow_visible)
4986 priv->lower_arrow_prelight = FALSE;
4988 /* If we hide the lower arrow, possibly remove timeout */
4989 if (priv->scroll_step > 0)
4991 gtk_menu_stop_scrolling (menu);
4992 gtk_widget_queue_draw (GTK_WIDGET (menu));
4996 y += arrow_border.top;
4999 /* Scroll the menu: */
5000 if (gtk_widget_get_realized (widget))
5001 gdk_window_move (priv->bin_window, 0, -offset);
5003 if (gtk_widget_get_realized (widget))
5004 gdk_window_move_resize (priv->view_window, x, y, view_width, view_height);
5006 priv->scroll_offset = offset;
5010 compute_child_offset (GtkMenu *menu,
5011 GtkWidget *menu_item,
5014 gboolean *is_last_child)
5016 GtkMenuPrivate *priv = menu->priv;
5017 gint item_top_attach;
5018 gint item_bottom_attach;
5019 gint child_offset = 0;
5022 get_effective_child_attach (menu_item, NULL, NULL,
5023 &item_top_attach, &item_bottom_attach);
5025 /* there is a possibility that we get called before _size_request,
5026 * so check the height table for safety.
5028 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
5031 /* when we have a row with only invisible children, its height will
5032 * be zero, so there's no need to check WIDGET_VISIBLE here
5034 for (i = 0; i < item_top_attach; i++)
5035 child_offset += priv->heights[i];
5038 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
5040 *offset = child_offset;
5042 *height = priv->heights[item_top_attach];
5048 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
5049 GtkWidget *menu_item)
5051 GtkMenu *menu = GTK_MENU (menu_shell);
5052 GtkMenuPrivate *priv = menu->priv;
5053 GtkWidget *widget = GTK_WIDGET (menu_shell);
5054 gint child_offset, child_height;
5058 gboolean last_child = 0;
5060 /* We need to check if the selected item fully visible.
5061 * If not we need to scroll the menu so that it becomes fully
5064 if (compute_child_offset (menu, menu_item,
5065 &child_offset, &child_height, &last_child))
5067 guint vertical_padding;
5068 gboolean double_arrows;
5071 y = priv->scroll_offset;
5072 height = gdk_window_get_height (gtk_widget_get_window (widget));
5074 gtk_widget_style_get (widget,
5075 "vertical-padding", &vertical_padding,
5078 double_arrows = get_double_arrows (menu);
5079 get_menu_padding (widget, &padding);
5081 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5082 padding.top + padding.bottom +
5083 2 * vertical_padding;
5084 if (child_offset < y)
5086 /* Ignore the enter event we might get if the pointer
5089 menu_shell->priv->ignore_enter = TRUE;
5090 gtk_menu_scroll_to (menu, child_offset);
5094 GtkBorder arrow_border;
5098 get_arrows_border (menu, &arrow_border);
5099 if (!priv->tearoff_active)
5100 arrow_height = arrow_border.top + arrow_border.bottom;
5102 if (child_offset + child_height > y + height - arrow_height)
5105 if ((!last_child && !priv->tearoff_active) || double_arrows)
5106 arrow_height += arrow_border.bottom;
5108 y = child_offset + child_height - height + arrow_height;
5109 if (((y > 0) && !priv->tearoff_active) || double_arrows)
5111 /* Need upper arrow */
5112 arrow_height += arrow_border.top;
5113 y = child_offset + child_height - height + arrow_height;
5115 /* Ignore the enter event we might get if the pointer
5118 menu_shell->priv->ignore_enter = TRUE;
5119 gtk_menu_scroll_to (menu, y);
5126 gtk_menu_select_item (GtkMenuShell *menu_shell,
5127 GtkWidget *menu_item)
5129 GtkMenu *menu = GTK_MENU (menu_shell);
5131 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5132 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5134 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5138 /* Reparent the menu, taking care of the refcounting
5140 * If unrealize is true we force a unrealize while reparenting the parent.
5141 * This can help eliminate flicker in some cases.
5143 * What happens is that when the menu is unrealized and then re-realized,
5144 * the allocations are as follows:
5146 * parent - 1x1 at (0,0)
5147 * child1 - 100x20 at (0,0)
5148 * child2 - 100x20 at (0,20)
5149 * child3 - 100x20 at (0,40)
5151 * That is, the parent is small but the children are full sized. Then,
5152 * when the queued_resize gets processed, the parent gets resized to
5155 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5156 * contains the following logic:
5158 * - if a move or resize operation on a window would change the clip
5159 * region on the children, then before the window is resized
5160 * the background for children is temporarily set to None, the
5161 * move/resize done, and the background for the children restored.
5163 * So, at the point where the parent is resized to final size, the
5164 * background for the children is temporarily None, and thus they
5165 * are not cleared to the background color and the previous background
5166 * (the image of the menu) is left in place.
5169 gtk_menu_reparent (GtkMenu *menu,
5170 GtkWidget *new_parent,
5173 GObject *object = G_OBJECT (menu);
5174 GtkWidget *widget = GTK_WIDGET (menu);
5175 gboolean was_floating = g_object_is_floating (object);
5177 g_object_ref_sink (object);
5181 g_object_ref (object);
5182 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5183 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5184 g_object_unref (object);
5187 gtk_widget_reparent (widget, new_parent);
5190 g_object_force_floating (object);
5192 g_object_unref (object);
5196 gtk_menu_show_all (GtkWidget *widget)
5198 /* Show children, but not self. */
5199 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5203 * gtk_menu_set_screen:
5205 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5206 * determined by the widget the menu is attached to
5208 * Sets the #GdkScreen on which the menu will be displayed.
5213 gtk_menu_set_screen (GtkMenu *menu,
5216 g_return_if_fail (GTK_IS_MENU (menu));
5217 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
5219 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5223 menu_change_screen (menu, screen);
5227 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5229 attach_widget_screen_changed (attach_widget, NULL, menu);
5236 * @child: a #GtkMenuItem
5237 * @left_attach: The column number to attach the left side of the item to
5238 * @right_attach: The column number to attach the right side of the item to
5239 * @top_attach: The row number to attach the top of the item to
5240 * @bottom_attach: The row number to attach the bottom of the item to
5242 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5243 * an item will occupy is specified by @left_attach, @right_attach,
5244 * @top_attach and @bottom_attach. These each represent the leftmost,
5245 * rightmost, uppermost and lower column and row numbers of the table.
5246 * (Columns and rows are indexed from zero).
5248 * Note that this function is not related to gtk_menu_detach().
5253 gtk_menu_attach (GtkMenu *menu,
5258 guint bottom_attach)
5260 GtkMenuShell *menu_shell;
5263 g_return_if_fail (GTK_IS_MENU (menu));
5264 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5265 parent = gtk_widget_get_parent (child);
5266 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5267 g_return_if_fail (left_attach < right_attach);
5268 g_return_if_fail (top_attach < bottom_attach);
5270 menu_shell = GTK_MENU_SHELL (menu);
5274 AttachInfo *ai = get_attach_info (child);
5276 ai->left_attach = left_attach;
5277 ai->right_attach = right_attach;
5278 ai->top_attach = top_attach;
5279 ai->bottom_attach = bottom_attach;
5281 menu_shell->priv->children = g_list_append (menu_shell->priv->children, child);
5283 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5285 menu_queue_resize (menu);
5289 gtk_container_child_set (GTK_CONTAINER (parent), child,
5290 "left-attach", left_attach,
5291 "right-attach", right_attach,
5292 "top-attach", top_attach,
5293 "bottom-attach", bottom_attach,
5299 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5303 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5304 "gtk-menu-popup-delay", &popup_delay,
5311 find_child_containing (GtkMenuShell *menu_shell,
5319 /* find a child which includes the area given by
5320 * left, right, top, bottom.
5322 for (list = menu_shell->priv->children; list; list = list->next)
5326 if (!_gtk_menu_item_is_selectable (list->data))
5329 get_effective_child_attach (list->data, &l, &r, &t, &b);
5331 if (l <= left && right <= r && t <= top && bottom <= b)
5332 return GTK_WIDGET (list->data);
5339 gtk_menu_move_current (GtkMenuShell *menu_shell,
5340 GtkMenuDirectionType direction)
5342 GtkMenu *menu = GTK_MENU (menu_shell);
5345 GtkWidget *match = NULL;
5347 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5351 case GTK_MENU_DIR_CHILD:
5352 direction = GTK_MENU_DIR_PARENT;
5354 case GTK_MENU_DIR_PARENT:
5355 direction = GTK_MENU_DIR_CHILD;
5361 /* use special table menu key bindings */
5362 if (menu_shell->priv->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5364 get_effective_child_attach (menu_shell->priv->active_menu_item, &l, &r, &t, &b);
5366 if (direction == GTK_MENU_DIR_NEXT)
5368 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5370 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5378 for (i = 0; i < t; i++)
5380 match = find_child_containing (menu_shell,
5381 l, l + 1, i, i + 1);
5387 else if (direction == GTK_MENU_DIR_PREV)
5389 for (i = t; i > 0; i--)
5391 match = find_child_containing (menu_shell,
5392 l, l + 1, i - 1, i);
5400 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5402 match = find_child_containing (menu_shell,
5403 l, l + 1, i - 1, i);
5409 else if (direction == GTK_MENU_DIR_PARENT)
5411 /* we go one left if possible */
5413 match = find_child_containing (menu_shell,
5414 l - 1, l, t, t + 1);
5418 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5421 || g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1)
5422 match = menu_shell->priv->active_menu_item;
5425 else if (direction == GTK_MENU_DIR_CHILD)
5427 /* we go one right if possible */
5428 if (r < gtk_menu_get_n_columns (menu))
5429 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5433 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5435 if (! GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu &&
5437 g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1))
5438 match = menu_shell->priv->active_menu_item;
5444 gtk_menu_shell_select_item (menu_shell, match);
5449 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5453 get_visible_size (GtkMenu *menu)
5455 GtkMenuPrivate *priv = menu->priv;
5456 GtkAllocation allocation;
5457 GtkWidget *widget = GTK_WIDGET (menu);
5458 GtkContainer *container = GTK_CONTAINER (menu);
5462 gtk_widget_get_allocation (widget, &allocation);
5463 get_menu_padding (widget, &padding);
5465 menu_height = (allocation.height -
5466 (2 * gtk_container_get_border_width (container)) -
5467 padding.top - padding.bottom);
5469 if (!priv->tearoff_active)
5471 GtkBorder arrow_border;
5473 get_arrows_border (menu, &arrow_border);
5474 menu_height -= arrow_border.top;
5475 menu_height -= arrow_border.bottom;
5481 /* Find the sensitive on-screen child containing @y, or if none,
5482 * the nearest selectable onscreen child. (%NULL if none)
5485 child_at (GtkMenu *menu,
5488 GtkMenuPrivate *priv = menu->priv;
5489 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5490 GtkWidget *child = NULL;
5491 gint child_offset = 0;
5494 gint lower, upper; /* Onscreen bounds */
5496 menu_height = get_visible_size (menu);
5497 lower = priv->scroll_offset;
5498 upper = priv->scroll_offset + menu_height;
5500 for (children = menu_shell->priv->children; children; children = children->next)
5502 if (gtk_widget_get_visible (children->data))
5504 GtkRequisition child_requisition;
5506 gtk_widget_get_preferred_size (children->data,
5507 &child_requisition, NULL);
5509 if (_gtk_menu_item_is_selectable (children->data) &&
5510 child_offset >= lower &&
5511 child_offset + child_requisition.height <= upper)
5513 child = children->data;
5515 if (child_offset + child_requisition.height > y &&
5516 !GTK_IS_TEAROFF_MENU_ITEM (child))
5520 child_offset += child_requisition.height;
5528 get_menu_height (GtkMenu *menu)
5530 GtkMenuPrivate *priv = menu->priv;
5531 GtkWidget *widget = GTK_WIDGET (menu);
5535 get_menu_padding (widget, &padding);
5537 height = priv->requested_height;
5538 height -= (gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2) +
5539 padding.top + padding.bottom;
5541 if (!priv->tearoff_active)
5543 GtkBorder arrow_border;
5545 get_arrows_border (menu, &arrow_border);
5546 height -= arrow_border.top;
5547 height -= arrow_border.bottom;
5554 gtk_menu_real_move_scroll (GtkMenu *menu,
5557 GtkMenuPrivate *priv = menu->priv;
5558 gint page_size = get_visible_size (menu);
5559 gint end_position = get_menu_height (menu);
5560 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5564 case GTK_SCROLL_PAGE_UP:
5565 case GTK_SCROLL_PAGE_DOWN:
5569 gint child_offset = 0;
5570 gboolean old_upper_arrow_visible;
5573 if (type == GTK_SCROLL_PAGE_UP)
5578 if (menu_shell->priv->active_menu_item)
5582 compute_child_offset (menu, menu_shell->priv->active_menu_item,
5583 &child_offset, &child_height, NULL);
5584 child_offset += child_height / 2;
5587 menu_shell->priv->ignore_enter = TRUE;
5588 old_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5589 old_offset = priv->scroll_offset;
5591 new_offset = priv->scroll_offset + step;
5592 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5594 gtk_menu_scroll_to (menu, new_offset);
5596 if (menu_shell->priv->active_menu_item)
5598 GtkWidget *new_child;
5599 gboolean new_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5600 GtkBorder arrow_border;
5602 get_arrows_border (menu, &arrow_border);
5604 if (priv->scroll_offset != old_offset)
5605 step = priv->scroll_offset - old_offset;
5607 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5609 new_child = child_at (menu, child_offset + step);
5611 gtk_menu_shell_select_item (menu_shell, new_child);
5615 case GTK_SCROLL_START:
5616 /* Ignore the enter event we might get if the pointer is on the menu */
5617 menu_shell->priv->ignore_enter = TRUE;
5618 gtk_menu_shell_select_first (menu_shell, TRUE);
5620 case GTK_SCROLL_END:
5621 /* Ignore the enter event we might get if the pointer is on the menu */
5622 menu_shell->priv->ignore_enter = TRUE;
5623 _gtk_menu_shell_select_last (menu_shell, TRUE);
5631 * gtk_menu_set_monitor:
5633 * @monitor_num: the number of the monitor on which the menu should
5636 * Informs GTK+ on which monitor a menu should be popped up.
5637 * See gdk_screen_get_monitor_geometry().
5639 * This function should be called from a #GtkMenuPositionFunc
5640 * if the menu should not appear on the same monitor as the pointer.
5641 * This information can't be reliably inferred from the coordinates
5642 * returned by a #GtkMenuPositionFunc, since, for very long menus,
5643 * these coordinates may extend beyond the monitor boundaries or even
5644 * the screen boundaries.
5649 gtk_menu_set_monitor (GtkMenu *menu,
5652 GtkMenuPrivate *priv = menu->priv;
5654 g_return_if_fail (GTK_IS_MENU (menu));
5656 priv->monitor_num = monitor_num;
5660 * gtk_menu_get_monitor:
5663 * Retrieves the number of the monitor on which to show the menu.
5665 * Returns: the number of the monitor on which the menu should
5666 * be popped up or -1, if no monitor has been set
5671 gtk_menu_get_monitor (GtkMenu *menu)
5673 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5675 return menu->priv->monitor_num;
5679 * gtk_menu_get_for_attach_widget:
5680 * @widget: a #GtkWidget
5682 * Returns a list of the menus which are attached to this widget.
5683 * This list is owned by GTK+ and must not be modified.
5685 * Return value: (element-type GtkWidget) (transfer none): the list
5686 * of menus attached to his widget.
5691 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5695 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5697 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5703 gtk_menu_grab_notify (GtkWidget *widget,
5704 gboolean was_grabbed)
5707 GtkWidget *toplevel;
5708 GtkWindowGroup *group;
5712 menu = GTK_MENU (widget);
5713 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5716 !gtk_widget_device_is_shadowed (widget, pointer))
5719 toplevel = gtk_widget_get_toplevel (widget);
5721 if (!GTK_IS_WINDOW (toplevel))
5724 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5725 grab = gtk_window_group_get_current_device_grab (group, pointer);
5727 if (GTK_MENU_SHELL (widget)->priv->active && !GTK_IS_MENU_SHELL (grab))
5728 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5730 menu->priv->drag_scroll_started = FALSE;
5734 * gtk_menu_set_reserve_toggle_size:
5736 * @reserve_toggle_size: whether to reserve size for toggles
5738 * Sets whether the menu should reserve space for drawing toggles
5739 * or icons, regardless of their actual presence.
5744 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5745 gboolean reserve_toggle_size)
5747 GtkMenuPrivate *priv = menu->priv;
5748 gboolean no_toggle_size;
5750 g_return_if_fail (GTK_IS_MENU (menu));
5752 no_toggle_size = !reserve_toggle_size;
5754 if (priv->no_toggle_size != no_toggle_size)
5756 priv->no_toggle_size = no_toggle_size;
5758 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5763 * gtk_menu_get_reserve_toggle_size:
5766 * Returns whether the menu reserves space for toggles and
5767 * icons, regardless of their actual presence.
5769 * Returns: Whether the menu reserves toggle space
5774 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5776 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
5778 return !menu->priv->no_toggle_size;