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 #GtkComboBox.
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_notify (G_OBJECT (menu), "attach-widget");
1301 g_object_unref (menu);
1305 gtk_menu_remove (GtkContainer *container,
1308 GtkMenu *menu = GTK_MENU (container);
1309 GtkMenuPrivate *priv = menu->priv;
1311 /* Clear out old_active_menu_item if it matches the item we are removing */
1312 if (priv->old_active_menu_item == widget)
1313 g_clear_object (&priv->old_active_menu_item);
1315 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1317 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1319 menu_queue_resize (menu);
1325 * Creates a new #GtkMenu
1327 * Returns: a new #GtkMenu
1332 return g_object_new (GTK_TYPE_MENU, NULL);
1336 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1340 GtkMenu *menu = GTK_MENU (menu_shell);
1341 GtkMenuPrivate *priv = menu->priv;
1342 AttachInfo *ai = get_attach_info (child);
1344 ai->left_attach = -1;
1345 ai->right_attach = -1;
1346 ai->top_attach = -1;
1347 ai->bottom_attach = -1;
1349 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1350 gtk_widget_set_parent_window (child, priv->bin_window);
1352 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1354 menu_queue_resize (menu);
1358 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1360 GtkMenuPrivate *priv = menu->priv;
1366 cairo_surface_t *surface;
1367 cairo_pattern_t *pattern;
1370 priv->tearoff_active = FALSE;
1371 priv->saved_scroll_offset = priv->scroll_offset;
1373 window = gtk_widget_get_window (priv->tearoff_window);
1374 width = gdk_window_get_width (window);
1375 height = gdk_window_get_height (window);
1377 surface = gdk_window_create_similar_surface (window,
1378 CAIRO_CONTENT_COLOR,
1382 cr = cairo_create (surface);
1383 gdk_cairo_set_source_window (cr, window, 0, 0);
1387 gtk_widget_set_size_request (priv->tearoff_window, width, height);
1389 pattern = cairo_pattern_create_for_surface (surface);
1390 gdk_window_set_background_pattern (window, pattern);
1392 cairo_pattern_destroy (pattern);
1393 cairo_surface_destroy (surface);
1398 popup_grab_on_window (GdkWindow *window,
1399 GdkDevice *keyboard,
1401 guint32 activate_time)
1404 gdk_device_grab (keyboard, window,
1405 GDK_OWNERSHIP_WINDOW, TRUE,
1406 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1407 NULL, activate_time) != GDK_GRAB_SUCCESS)
1411 gdk_device_grab (pointer, window,
1412 GDK_OWNERSHIP_WINDOW, TRUE,
1413 GDK_SMOOTH_SCROLL_MASK |
1414 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1415 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1416 GDK_POINTER_MOTION_MASK,
1417 NULL, activate_time) != GDK_GRAB_SUCCESS)
1420 gdk_device_ungrab (keyboard, activate_time);
1429 * gtk_menu_popup_for_device:
1431 * @device: (allow-none): a #GdkDevice
1432 * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1433 * menu item, or %NULL
1434 * @parent_menu_item: (allow-none): the menu item whose activation triggered
1435 * the popup, or %NULL
1436 * @func: (allow-none): a user supplied function used to position the menu,
1438 * @data: (allow-none): user supplied data to be passed to @func
1439 * @destroy: (allow-none): destroy notify for @data
1440 * @button: the mouse button which was pressed to initiate the event
1441 * @activate_time: the time at which the activation event occurred
1443 * Displays a menu and makes it available for selection.
1445 * Applications can use this function to display context-sensitive menus,
1446 * and will typically supply %NULL for the @parent_menu_shell,
1447 * @parent_menu_item, @func, @data and @destroy parameters. The default
1448 * menu positioning function will position the menu at the current position
1449 * of @device (or its corresponding pointer).
1451 * The @button parameter should be the mouse button pressed to initiate
1452 * the menu popup. If the menu popup was initiated by something other than
1453 * a mouse button press, such as a mouse button release or a keypress,
1454 * @button should be 0.
1456 * The @activate_time parameter is used to conflict-resolve initiation of
1457 * concurrent requests for mouse/keyboard grab requests. To function
1458 * properly, this needs to be the time stamp of the user event (such as
1459 * a mouse click or key press) that caused the initiation of the popup.
1460 * Only if no such event is available, gtk_get_current_event_time() can
1466 gtk_menu_popup_for_device (GtkMenu *menu,
1468 GtkWidget *parent_menu_shell,
1469 GtkWidget *parent_menu_item,
1470 GtkMenuPositionFunc func,
1472 GDestroyNotify destroy,
1474 guint32 activate_time)
1476 GtkMenuPrivate *priv = menu->priv;
1478 GtkWidget *xgrab_shell;
1480 GdkEvent *current_event;
1481 GtkMenuShell *menu_shell;
1482 gboolean grab_keyboard;
1483 GtkWidget *parent_toplevel;
1484 GdkDevice *keyboard, *pointer, *source_device = NULL;
1486 g_return_if_fail (GTK_IS_MENU (menu));
1487 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1490 device = gtk_get_current_event_device ();
1494 GdkDisplay *display;
1495 GdkDeviceManager *device_manager;
1498 display = gtk_widget_get_display (GTK_WIDGET (menu));
1499 device_manager = gdk_display_get_device_manager (display);
1500 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1502 device = devices->data;
1504 g_list_free (devices);
1507 widget = GTK_WIDGET (menu);
1508 menu_shell = GTK_MENU_SHELL (menu);
1510 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1513 pointer = gdk_device_get_associated_device (device);
1518 keyboard = gdk_device_get_associated_device (device);
1521 menu_shell->priv->parent_menu_shell = parent_menu_shell;
1523 priv->seen_item_enter = FALSE;
1525 /* Find the last viewable ancestor, and make an X grab on it
1527 parent = GTK_WIDGET (menu);
1531 gboolean viewable = TRUE;
1532 GtkWidget *tmp = parent;
1536 if (!gtk_widget_get_mapped (tmp))
1541 tmp = gtk_widget_get_parent (tmp);
1545 xgrab_shell = parent;
1547 parent = GTK_MENU_SHELL (parent)->priv->parent_menu_shell;
1550 /* We want to receive events generated when we map the menu;
1551 * unfortunately, since there is probably already an implicit
1552 * grab in place from the button that the user used to pop up
1553 * the menu, we won't receive then -- in particular, the EnterNotify
1554 * when the menu pops up under the pointer.
1556 * If we are grabbing on a parent menu shell, no problem; just grab
1557 * on that menu shell first before popping up the window with
1558 * owner_events = TRUE.
1560 * When grabbing on the menu itself, things get more convoluted --
1561 * we do an explicit grab on a specially created window with
1562 * owner_events = TRUE, which we override further down with a
1563 * grab on the menu. (We can't grab on the menu until it is mapped;
1564 * we probably could just leave the grab on the other window,
1565 * with a little reorganization of the code in gtkmenu*).
1567 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1568 gtk_window_set_accept_focus (GTK_WINDOW (priv->toplevel), grab_keyboard);
1573 if (xgrab_shell && xgrab_shell != widget)
1575 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1577 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1578 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1583 GdkWindow *transfer_window;
1585 xgrab_shell = widget;
1586 transfer_window = menu_grab_transfer_window_get (menu);
1587 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1589 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1590 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1594 if (!GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab)
1596 /* We failed to make our pointer/keyboard grab.
1597 * Rather than leaving the user with a stuck up window,
1598 * we just abort here. Presumably the user will try again.
1600 menu_shell->priv->parent_menu_shell = NULL;
1601 menu_grab_transfer_window_destroy (menu);
1605 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1606 menu_shell->priv->active = TRUE;
1607 menu_shell->priv->button = button;
1609 /* If we are popping up the menu from something other than, a button
1610 * press then, as a heuristic, we ignore enter events for the menu
1611 * until we get a MOTION_NOTIFY.
1614 current_event = gtk_get_current_event ();
1617 if ((current_event->type != GDK_BUTTON_PRESS) &&
1618 (current_event->type != GDK_ENTER_NOTIFY))
1619 menu_shell->priv->ignore_enter = TRUE;
1621 source_device = gdk_event_get_source_device (current_event);
1622 gdk_event_free (current_event);
1625 menu_shell->priv->ignore_enter = TRUE;
1629 gtk_menu_tearoff_bg_copy (menu);
1631 gtk_menu_reparent (menu, priv->toplevel, FALSE);
1634 parent_toplevel = NULL;
1635 if (parent_menu_shell)
1636 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1637 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1639 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1641 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1644 /* Set transient for to get the right window group and parent */
1645 if (GTK_IS_WINDOW (parent_toplevel))
1646 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel),
1647 GTK_WINDOW (parent_toplevel));
1649 priv->parent_menu_item = parent_menu_item;
1650 priv->position_func = func;
1651 priv->position_func_data = data;
1652 priv->position_func_data_destroy = destroy;
1653 menu_shell->priv->activate_time = activate_time;
1655 /* We need to show the menu here rather in the init function
1656 * because code expects to be able to tell if the menu is onscreen
1657 * by looking at gtk_widget_get_visible (menu)
1659 gtk_widget_show (GTK_WIDGET (menu));
1661 /* Position the menu, possibly changing the size request
1663 gtk_menu_position (menu, TRUE);
1665 /* Compute the size of the toplevel and realize it so we
1666 * can scroll correctly.
1668 if (!gtk_widget_get_realized (GTK_WIDGET (menu)))
1670 GtkRequisition tmp_request;
1671 GtkAllocation tmp_allocation = { 0, };
1673 /* Instead of trusting the menu position function to queue a
1674 * resize when the menu goes out of bounds, invalidate the cached
1677 gtk_widget_queue_resize (GTK_WIDGET (menu));
1678 gtk_widget_get_preferred_size (priv->toplevel, &tmp_request, NULL);
1680 tmp_allocation.width = tmp_request.width;
1681 tmp_allocation.height = tmp_request.height;
1683 gtk_widget_size_allocate (priv->toplevel, &tmp_allocation);
1685 gtk_widget_realize (priv->toplevel);
1688 gtk_menu_scroll_to (menu, priv->scroll_offset);
1690 /* if no item is selected, select the first one */
1691 if (!menu_shell->priv->active_menu_item &&
1692 source_device && gdk_device_get_source (source_device) == GDK_SOURCE_TOUCHSCREEN)
1693 gtk_menu_shell_select_first (menu_shell, TRUE);
1695 /* Once everything is set up correctly, map the toplevel */
1696 gtk_widget_show (priv->toplevel);
1698 if (xgrab_shell == widget)
1699 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1701 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1703 if (parent_menu_shell)
1705 gboolean keyboard_mode;
1707 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1708 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1710 else if (menu_shell->priv->button == 0) /* a keynav-activated context menu */
1711 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1713 _gtk_menu_shell_update_mnemonics (menu_shell);
1719 * @parent_menu_shell: (allow-none): the menu shell containing the
1720 * triggering menu item, or %NULL
1721 * @parent_menu_item: (allow-none): the menu item whose activation
1722 * triggered the popup, or %NULL
1723 * @func: (scope async) (allow-none): a user supplied function used to position
1724 * the menu, or %NULL
1725 * @data: user supplied data to be passed to @func.
1726 * @button: the mouse button which was pressed to initiate the event.
1727 * @activate_time: the time at which the activation event occurred.
1729 * Displays a menu and makes it available for selection.
1731 * Applications can use this function to display context-sensitive
1732 * menus, and will typically supply %NULL for the @parent_menu_shell,
1733 * @parent_menu_item, @func and @data parameters. The default menu
1734 * positioning function will position the menu at the current mouse
1737 * The @button parameter should be the mouse button pressed to initiate
1738 * the menu popup. If the menu popup was initiated by something other
1739 * than a mouse button press, such as a mouse button release or a keypress,
1740 * @button should be 0.
1742 * The @activate_time parameter is used to conflict-resolve initiation
1743 * of concurrent requests for mouse/keyboard grab requests. To function
1744 * properly, this needs to be the timestamp of the user event (such as
1745 * a mouse click or key press) that caused the initiation of the popup.
1746 * Only if no such event is available, gtk_get_current_event_time() can
1750 gtk_menu_popup (GtkMenu *menu,
1751 GtkWidget *parent_menu_shell,
1752 GtkWidget *parent_menu_item,
1753 GtkMenuPositionFunc func,
1756 guint32 activate_time)
1758 g_return_if_fail (GTK_IS_MENU (menu));
1760 gtk_menu_popup_for_device (menu,
1765 button, activate_time);
1772 * Removes the menu from the screen.
1775 gtk_menu_popdown (GtkMenu *menu)
1777 GtkMenuPrivate *priv;
1778 GtkMenuShell *menu_shell;
1781 g_return_if_fail (GTK_IS_MENU (menu));
1783 menu_shell = GTK_MENU_SHELL (menu);
1786 menu_shell->priv->parent_menu_shell = NULL;
1787 menu_shell->priv->active = FALSE;
1788 menu_shell->priv->ignore_enter = FALSE;
1790 priv->have_position = FALSE;
1792 gtk_menu_stop_scrolling (menu);
1793 gtk_menu_stop_navigating_submenu (menu);
1795 if (menu_shell->priv->active_menu_item)
1797 if (priv->old_active_menu_item)
1798 g_object_unref (priv->old_active_menu_item);
1799 priv->old_active_menu_item = menu_shell->priv->active_menu_item;
1800 g_object_ref (priv->old_active_menu_item);
1803 gtk_menu_shell_deselect (menu_shell);
1805 /* The X Grab, if present, will automatically be removed
1806 * when we hide the window
1810 gtk_widget_hide (priv->toplevel);
1811 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel), NULL);
1814 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1818 gtk_widget_set_size_request (priv->tearoff_window, -1, -1);
1820 if (gtk_bin_get_child (GTK_BIN (priv->toplevel)))
1822 gtk_menu_reparent (menu, priv->tearoff_hbox, TRUE);
1826 /* We popped up the menu from the tearoff, so we need to
1827 * release the grab - we aren't actually hiding the menu.
1829 if (menu_shell->priv->have_xgrab && pointer)
1831 GdkDevice *keyboard;
1833 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1834 keyboard = gdk_device_get_associated_device (pointer);
1837 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1841 /* gtk_menu_popdown is called each time a menu item is selected from
1842 * a torn off menu. Only scroll back to the saved position if the
1843 * non-tearoff menu was popped down.
1845 if (!priv->tearoff_active)
1846 gtk_menu_scroll_to (menu, priv->saved_scroll_offset);
1847 priv->tearoff_active = TRUE;
1850 gtk_widget_hide (GTK_WIDGET (menu));
1852 menu_shell->priv->have_xgrab = FALSE;
1855 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1857 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1859 menu_grab_transfer_window_destroy (menu);
1863 * gtk_menu_get_active:
1866 * Returns the selected menu item from the menu. This is used by the
1869 * Returns: (transfer none): the #GtkMenuItem that was last selected
1870 * in the menu. If a selection has not yet been made, the
1871 * first menu item is selected.
1874 gtk_menu_get_active (GtkMenu *menu)
1876 GtkMenuPrivate *priv = menu->priv;
1880 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1882 if (!priv->old_active_menu_item)
1885 children = GTK_MENU_SHELL (menu)->priv->children;
1889 child = children->data;
1890 children = children->next;
1892 if (gtk_bin_get_child (GTK_BIN (child)))
1897 priv->old_active_menu_item = child;
1898 if (priv->old_active_menu_item)
1899 g_object_ref (priv->old_active_menu_item);
1902 return priv->old_active_menu_item;
1906 * gtk_menu_set_active:
1908 * @index: the index of the menu item to select. Iindex values are
1911 * Selects the specified menu item within the menu. This is used by
1912 * the #GtkComboBox and should not be used by anyone else.
1915 gtk_menu_set_active (GtkMenu *menu,
1918 GtkMenuPrivate *priv = menu->priv;
1922 g_return_if_fail (GTK_IS_MENU (menu));
1924 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->priv->children, index);
1927 child = tmp_list->data;
1928 if (gtk_bin_get_child (GTK_BIN (child)))
1930 if (priv->old_active_menu_item)
1931 g_object_unref (priv->old_active_menu_item);
1932 priv->old_active_menu_item = child;
1933 g_object_ref (priv->old_active_menu_item);
1939 * gtk_menu_set_accel_group:
1941 * @accel_group: (allow-none): the #GtkAccelGroup to be associated
1944 * Set the #GtkAccelGroup which holds global accelerators for the
1945 * menu. This accelerator group needs to also be added to all windows
1946 * that this menu is being used in with gtk_window_add_accel_group(),
1947 * in order for those windows to support all the accelerators
1948 * contained in this group.
1951 gtk_menu_set_accel_group (GtkMenu *menu,
1952 GtkAccelGroup *accel_group)
1954 GtkMenuPrivate *priv = menu->priv;
1955 g_return_if_fail (GTK_IS_MENU (menu));
1957 if (priv->accel_group != accel_group)
1959 if (priv->accel_group)
1960 g_object_unref (priv->accel_group);
1961 priv->accel_group = accel_group;
1962 if (priv->accel_group)
1963 g_object_ref (priv->accel_group);
1964 _gtk_menu_refresh_accel_paths (menu, TRUE);
1969 * gtk_menu_get_accel_group:
1972 * Gets the #GtkAccelGroup which holds global accelerators for the
1973 * menu. See gtk_menu_set_accel_group().
1975 * Returns: (transfer none): the #GtkAccelGroup associated with the menu
1978 gtk_menu_get_accel_group (GtkMenu *menu)
1980 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1982 return menu->priv->accel_group;
1986 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1989 /* Menu items chain here to figure whether they can activate their
1990 * accelerators. Unlike ordinary widgets, menus allow accel
1991 * activation even if invisible since that's the usual case for
1992 * submenus/popup-menus. however, the state of the attach widget
1993 * affects the "activeness" of the menu.
1995 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1998 return gtk_widget_can_activate_accel (awidget, signal_id);
2000 return gtk_widget_is_sensitive (widget);
2004 * gtk_menu_set_accel_path:
2005 * @menu: a valid #GtkMenu
2006 * @accel_path: (allow-none): a valid accelerator path
2008 * Sets an accelerator path for this menu from which accelerator paths
2009 * for its immediate children, its menu items, can be constructed.
2010 * The main purpose of this function is to spare the programmer the
2011 * inconvenience of having to call gtk_menu_item_set_accel_path() on
2012 * each menu item that should support runtime user changable accelerators.
2013 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
2014 * each menu item of this menu, that contains a label describing its
2015 * purpose, automatically gets an accel path assigned.
2017 * For example, a menu containing menu items "New" and "Exit", will, after
2018 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
2019 * has been called, assign its items the accel paths:
2020 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
2022 * Assigning accel paths to menu items then enables the user to change
2023 * their accelerators at runtime. More details about accelerator paths
2024 * and their default setups can be found at gtk_accel_map_add_entry().
2026 * Note that @accel_path string will be stored in a #GQuark. Therefore,
2027 * if you pass a static string, you can save some memory by interning
2028 * it first with g_intern_static_string().
2031 gtk_menu_set_accel_path (GtkMenu *menu,
2032 const gchar *accel_path)
2034 GtkMenuPrivate *priv = menu->priv;
2035 g_return_if_fail (GTK_IS_MENU (menu));
2038 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
2040 /* FIXME: accel_path should be defined as const gchar* */
2041 priv->accel_path = (gchar*)g_intern_string (accel_path);
2042 if (priv->accel_path)
2043 _gtk_menu_refresh_accel_paths (menu, FALSE);
2047 * gtk_menu_get_accel_path:
2048 * @menu: a valid #GtkMenu
2050 * Retrieves the accelerator path set on the menu.
2052 * Returns: the accelerator path set on the menu.
2057 gtk_menu_get_accel_path (GtkMenu *menu)
2059 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2061 return menu->priv->accel_path;
2066 gboolean group_changed;
2070 refresh_accel_paths_foreach (GtkWidget *widget,
2073 GtkMenuPrivate *priv;
2074 AccelPropagation *prop = data;
2076 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
2078 priv = prop->menu->priv;
2079 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2082 prop->group_changed);
2087 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
2088 gboolean group_changed)
2090 GtkMenuPrivate *priv = menu->priv;
2091 g_return_if_fail (GTK_IS_MENU (menu));
2093 if (priv->accel_path && priv->accel_group)
2095 AccelPropagation prop;
2098 prop.group_changed = group_changed;
2099 gtk_container_foreach (GTK_CONTAINER (menu),
2100 refresh_accel_paths_foreach,
2106 * gtk_menu_reposition:
2109 * Repositions the menu according to its position function.
2112 gtk_menu_reposition (GtkMenu *menu)
2114 g_return_if_fail (GTK_IS_MENU (menu));
2116 if (!menu->priv->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2117 gtk_menu_position (menu, FALSE);
2121 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2126 value = gtk_adjustment_get_value (adjustment);
2127 if (menu->priv->scroll_offset != value)
2128 gtk_menu_scroll_to (menu, value);
2132 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2135 GtkMenuPrivate *priv = menu->priv;
2136 GdkGeometry geometry_hints;
2138 if (!priv->tearoff_window)
2141 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2143 GtkRequisition requisition;
2145 gtk_widget_get_preferred_size (priv->tearoff_scrollbar,
2146 &requisition, NULL);
2147 width += requisition.width;
2150 geometry_hints.min_width = width;
2151 geometry_hints.max_width = width;
2153 geometry_hints.min_height = 0;
2154 geometry_hints.max_height = priv->requested_height;
2156 gtk_window_set_geometry_hints (GTK_WINDOW (priv->tearoff_window),
2159 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2163 gtk_menu_update_title (GtkMenu *menu)
2165 GtkMenuPrivate *priv = menu->priv;
2167 if (priv->tearoff_window)
2170 GtkWidget *attach_widget;
2172 title = gtk_menu_get_title (menu);
2175 attach_widget = gtk_menu_get_attach_widget (menu);
2176 if (GTK_IS_MENU_ITEM (attach_widget))
2178 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2179 if (GTK_IS_LABEL (child))
2180 title = gtk_label_get_text (GTK_LABEL (child));
2185 gtk_window_set_title (GTK_WINDOW (priv->tearoff_window), title);
2190 gtk_menu_get_toplevel (GtkWidget *menu)
2192 GtkWidget *attach, *toplevel;
2194 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2196 if (GTK_IS_MENU_ITEM (attach))
2197 attach = gtk_widget_get_parent (attach);
2199 if (GTK_IS_MENU (attach))
2200 return gtk_menu_get_toplevel (attach);
2201 else if (GTK_IS_WIDGET (attach))
2203 toplevel = gtk_widget_get_toplevel (attach);
2204 if (gtk_widget_is_toplevel (toplevel))
2212 tearoff_window_destroyed (GtkWidget *widget,
2215 gtk_menu_set_tearoff_state (menu, FALSE);
2219 * gtk_menu_set_tearoff_state:
2221 * @torn_off: If %TRUE, menu is displayed as a tearoff menu.
2223 * Changes the tearoff state of the menu. A menu is normally
2224 * displayed as drop down menu which persists as long as the menu is
2225 * active. It can also be displayed as a tearoff menu which persists
2226 * until it is closed or reattached.
2229 gtk_menu_set_tearoff_state (GtkMenu *menu,
2232 GtkMenuPrivate *priv = menu->priv;
2235 g_return_if_fail (GTK_IS_MENU (menu));
2237 if (priv->torn_off != torn_off)
2239 priv->torn_off = torn_off;
2240 priv->tearoff_active = torn_off;
2244 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2245 gtk_menu_popdown (menu);
2247 if (!priv->tearoff_window)
2249 GtkWidget *toplevel;
2251 priv->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2252 "type", GTK_WINDOW_TOPLEVEL,
2253 "screen", gtk_widget_get_screen (priv->toplevel),
2254 "app-paintable", TRUE,
2257 gtk_window_set_type_hint (GTK_WINDOW (priv->tearoff_window),
2258 GDK_WINDOW_TYPE_HINT_MENU);
2259 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->tearoff_window), 0);
2260 g_signal_connect (priv->tearoff_window, "destroy",
2261 G_CALLBACK (tearoff_window_destroyed), menu);
2262 g_signal_connect (priv->tearoff_window, "event",
2263 G_CALLBACK (gtk_menu_window_event), menu);
2265 gtk_menu_update_title (menu);
2267 gtk_widget_realize (priv->tearoff_window);
2269 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2270 if (toplevel != NULL)
2271 gtk_window_set_transient_for (GTK_WINDOW (priv->tearoff_window),
2272 GTK_WINDOW (toplevel));
2274 priv->tearoff_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2275 gtk_container_add (GTK_CONTAINER (priv->tearoff_window),
2276 priv->tearoff_hbox);
2278 height = gdk_window_get_height (gtk_widget_get_window (GTK_WIDGET (menu)));
2279 priv->tearoff_adjustment = gtk_adjustment_new (0,
2280 0, priv->requested_height,
2284 g_object_connect (priv->tearoff_adjustment,
2285 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2287 priv->tearoff_scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, priv->tearoff_adjustment);
2289 gtk_box_pack_end (GTK_BOX (priv->tearoff_hbox),
2290 priv->tearoff_scrollbar,
2293 if (gtk_adjustment_get_upper (priv->tearoff_adjustment) > height)
2294 gtk_widget_show (priv->tearoff_scrollbar);
2296 gtk_widget_show (priv->tearoff_hbox);
2299 gtk_menu_reparent (menu, priv->tearoff_hbox, FALSE);
2301 /* Update menu->requisition */
2302 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, NULL);
2304 gtk_menu_set_tearoff_hints (menu, gdk_window_get_width (gtk_widget_get_window (GTK_WIDGET (menu))));
2306 gtk_widget_realize (priv->tearoff_window);
2307 gtk_menu_position (menu, TRUE);
2309 gtk_widget_show (GTK_WIDGET (menu));
2310 gtk_widget_show (priv->tearoff_window);
2312 gtk_menu_scroll_to (menu, 0);
2317 gtk_widget_hide (GTK_WIDGET (menu));
2318 gtk_widget_hide (priv->tearoff_window);
2319 if (GTK_IS_CONTAINER (priv->toplevel))
2320 gtk_menu_reparent (menu, priv->toplevel, FALSE);
2321 gtk_widget_destroy (priv->tearoff_window);
2323 priv->tearoff_window = NULL;
2324 priv->tearoff_hbox = NULL;
2325 priv->tearoff_scrollbar = NULL;
2326 priv->tearoff_adjustment = NULL;
2329 g_object_notify (G_OBJECT (menu), "tearoff-state");
2334 * gtk_menu_get_tearoff_state:
2337 * Returns whether the menu is torn off.
2338 * See gtk_menu_set_tearoff_state().
2340 * Return value: %TRUE if the menu is currently torn off.
2343 gtk_menu_get_tearoff_state (GtkMenu *menu)
2345 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2347 return menu->priv->torn_off;
2351 * gtk_menu_set_title:
2353 * @title: a string containing the title for the menu
2355 * Sets the title string for the menu.
2357 * The title is displayed when the menu is shown as a tearoff
2358 * menu. If @title is %NULL, the menu will see if it is attached
2359 * to a parent menu item, and if so it will try to use the same
2360 * text as that menu item's label.
2363 gtk_menu_set_title (GtkMenu *menu,
2366 GtkMenuPrivate *priv = menu->priv;
2369 g_return_if_fail (GTK_IS_MENU (menu));
2371 old_title = priv->title;
2372 priv->title = g_strdup (title);
2375 gtk_menu_update_title (menu);
2376 g_object_notify (G_OBJECT (menu), "tearoff-title");
2380 * gtk_menu_get_title:
2383 * Returns the title of the menu. See gtk_menu_set_title().
2385 * Return value: the title of the menu, or %NULL if the menu
2386 * has no title set on it. This string is owned by GTK+
2387 * and should not be modified or freed.
2390 gtk_menu_get_title (GtkMenu *menu)
2392 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2394 return menu->priv->title;
2398 * gtk_menu_reorder_child:
2400 * @child: the #GtkMenuItem to move
2401 * @position: the new position to place @child.
2402 * Positions are numbered from 0 to n - 1
2404 * Moves @child to a new @position in the list of @menu
2408 gtk_menu_reorder_child (GtkMenu *menu,
2412 GtkMenuShell *menu_shell;
2414 g_return_if_fail (GTK_IS_MENU (menu));
2415 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2417 menu_shell = GTK_MENU_SHELL (menu);
2419 if (g_list_find (menu_shell->priv->children, child))
2421 menu_shell->priv->children = g_list_remove (menu_shell->priv->children, child);
2422 menu_shell->priv->children = g_list_insert (menu_shell->priv->children, child, position);
2424 menu_queue_resize (menu);
2429 gtk_menu_style_updated (GtkWidget *widget)
2431 GTK_WIDGET_CLASS (gtk_menu_parent_class)->style_updated (widget);
2433 if (gtk_widget_get_realized (widget))
2435 GtkMenu *menu = GTK_MENU (widget);
2436 GtkMenuPrivate *priv = menu->priv;
2437 GtkStyleContext *context;
2439 context = gtk_widget_get_style_context (widget);
2441 gtk_style_context_set_background (context, priv->bin_window);
2442 gtk_style_context_set_background (context, priv->view_window);
2443 gtk_style_context_set_background (context, gtk_widget_get_window (widget));
2448 get_arrows_border (GtkMenu *menu,
2451 GtkMenuPrivate *priv = menu->priv;
2452 guint scroll_arrow_height;
2453 GtkArrowPlacement arrow_placement;
2455 gtk_widget_style_get (GTK_WIDGET (menu),
2456 "scroll-arrow-vlength", &scroll_arrow_height,
2457 "arrow_placement", &arrow_placement,
2460 switch (arrow_placement)
2462 case GTK_ARROWS_BOTH:
2463 border->top = priv->upper_arrow_visible ? scroll_arrow_height : 0;
2464 border->bottom = priv->lower_arrow_visible ? scroll_arrow_height : 0;
2467 case GTK_ARROWS_START:
2468 border->top = (priv->upper_arrow_visible ||
2469 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2473 case GTK_ARROWS_END:
2475 border->bottom = (priv->upper_arrow_visible ||
2476 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2480 border->left = border->right = 0;
2484 get_menu_padding (GtkWidget *widget,
2487 GtkStyleContext *context;
2488 GtkStateFlags state;
2490 context = gtk_widget_get_style_context (widget);
2491 state = gtk_widget_get_state_flags (widget);
2493 gtk_style_context_get_padding (context, state, padding);
2497 gtk_menu_realize (GtkWidget *widget)
2499 GtkMenu *menu = GTK_MENU (widget);
2500 GtkMenuPrivate *priv = menu->priv;
2501 GtkAllocation allocation;
2502 GtkStyleContext *context;
2504 GdkWindowAttr attributes;
2505 gint attributes_mask;
2509 guint vertical_padding;
2510 guint horizontal_padding;
2511 GtkBorder arrow_border, padding;
2513 g_return_if_fail (GTK_IS_MENU (widget));
2515 gtk_widget_set_realized (widget, TRUE);
2517 gtk_widget_get_allocation (widget, &allocation);
2519 attributes.window_type = GDK_WINDOW_CHILD;
2520 attributes.x = allocation.x;
2521 attributes.y = allocation.y;
2522 attributes.width = allocation.width;
2523 attributes.height = allocation.height;
2524 attributes.wclass = GDK_INPUT_OUTPUT;
2525 attributes.visual = gtk_widget_get_visual (widget);
2526 attributes.event_mask = gtk_widget_get_events (widget);
2527 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2528 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2530 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2532 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2533 &attributes, attributes_mask);
2534 gtk_widget_set_window (widget, window);
2535 gdk_window_set_user_data (window, widget);
2537 get_menu_padding (widget, &padding);
2538 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2539 context = gtk_widget_get_style_context (widget);
2541 gtk_widget_style_get (GTK_WIDGET (menu),
2542 "vertical-padding", &vertical_padding,
2543 "horizontal-padding", &horizontal_padding,
2546 gtk_widget_get_allocation (widget, &allocation);
2548 attributes.x = border_width + padding.left + horizontal_padding;
2549 attributes.y = border_width + padding.top + vertical_padding;
2550 attributes.width = allocation.width -
2551 (2 * (border_width + horizontal_padding)) - padding.left - padding.right;
2552 attributes.height = allocation.height -
2553 (2 * (border_width + vertical_padding)) - padding.top - padding.bottom;
2555 get_arrows_border (menu, &arrow_border);
2556 attributes.y += arrow_border.top;
2557 attributes.height -= arrow_border.top;
2558 attributes.height -= arrow_border.bottom;
2560 attributes.width = MAX (1, attributes.width);
2561 attributes.height = MAX (1, attributes.height);
2563 priv->view_window = gdk_window_new (window,
2564 &attributes, attributes_mask);
2565 gdk_window_set_user_data (priv->view_window, menu);
2567 gtk_widget_get_allocation (widget, &allocation);
2571 attributes.width = allocation.width + (2 * (border_width + horizontal_padding)) +
2572 padding.left + padding.right;
2573 attributes.height = priv->requested_height - (2 * (border_width + vertical_padding)) +
2574 padding.top + padding.bottom;
2576 attributes.width = MAX (1, attributes.width);
2577 attributes.height = MAX (1, attributes.height);
2579 priv->bin_window = gdk_window_new (priv->view_window,
2580 &attributes, attributes_mask);
2581 gdk_window_set_user_data (priv->bin_window, menu);
2583 children = GTK_MENU_SHELL (menu)->priv->children;
2586 child = children->data;
2587 children = children->next;
2589 gtk_widget_set_parent_window (child, priv->bin_window);
2592 gtk_style_context_set_background (context, priv->bin_window);
2593 gtk_style_context_set_background (context, priv->view_window);
2594 gtk_style_context_set_background (context, window);
2596 if (GTK_MENU_SHELL (widget)->priv->active_menu_item)
2597 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2598 GTK_MENU_SHELL (widget)->priv->active_menu_item);
2600 gdk_window_show (priv->bin_window);
2601 gdk_window_show (priv->view_window);
2605 gtk_menu_focus (GtkWidget *widget,
2606 GtkDirectionType direction)
2608 /* A menu or its menu items cannot have focus */
2612 /* See notes in gtk_menu_popup() for information
2613 * about the "grab transfer window"
2616 menu_grab_transfer_window_get (GtkMenu *menu)
2618 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2621 GdkWindowAttr attributes;
2622 gint attributes_mask;
2624 attributes.x = -100;
2625 attributes.y = -100;
2626 attributes.width = 10;
2627 attributes.height = 10;
2628 attributes.window_type = GDK_WINDOW_TEMP;
2629 attributes.wclass = GDK_INPUT_ONLY;
2630 attributes.override_redirect = TRUE;
2631 attributes.event_mask = 0;
2633 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2635 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2636 &attributes, attributes_mask);
2637 gdk_window_set_user_data (window, menu);
2639 gdk_window_show (window);
2641 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2648 menu_grab_transfer_window_destroy (GtkMenu *menu)
2650 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2653 gdk_window_set_user_data (window, NULL);
2654 gdk_window_destroy (window);
2655 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2660 gtk_menu_unrealize (GtkWidget *widget)
2662 GtkMenu *menu = GTK_MENU (widget);
2663 GtkMenuPrivate *priv = menu->priv;
2665 menu_grab_transfer_window_destroy (menu);
2667 gdk_window_set_user_data (priv->view_window, NULL);
2668 gdk_window_destroy (priv->view_window);
2669 priv->view_window = NULL;
2671 gdk_window_set_user_data (priv->bin_window, NULL);
2672 gdk_window_destroy (priv->bin_window);
2673 priv->bin_window = NULL;
2675 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2679 calculate_line_heights (GtkMenu *menu,
2681 guint **ret_min_heights,
2682 guint **ret_nat_heights)
2685 GtkMenuPrivate *priv;
2686 GtkMenuShell *menu_shell;
2687 GtkWidget *child, *widget;
2689 guint horizontal_padding;
2698 widget = GTK_WIDGET (menu);
2699 menu_shell = GTK_MENU_SHELL (widget);
2701 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2702 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2703 n_heights = gtk_menu_get_n_rows (menu);
2704 n_columns = gtk_menu_get_n_columns (menu);
2705 avail_width = for_width - (2 * priv->toggle_size + priv->accel_size) * n_columns;
2707 gtk_widget_style_get (GTK_WIDGET (menu),
2708 "horizontal-padding", &horizontal_padding,
2710 get_menu_padding (widget, &padding);
2712 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2713 avail_width -= (border_width + horizontal_padding) * 2 + padding.left + padding.right;
2715 for (children = menu_shell->priv->children; children; children = children->next)
2720 gint child_min, child_nat;
2722 child = children->data;
2724 if (!gtk_widget_get_visible (child))
2727 get_effective_child_attach (child, &l, &r, &t, &b);
2729 part = avail_width / (r - l);
2731 gtk_widget_get_preferred_height_for_width (child, part,
2732 &child_min, &child_nat);
2734 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2736 part = MAX (child_min, toggle_size) / (b - t);
2737 min_heights[t] = MAX (min_heights[t], part);
2739 part = MAX (child_nat, toggle_size) / (b - t);
2740 nat_heights[t] = MAX (nat_heights[t], part);
2743 if (ret_min_heights)
2744 *ret_min_heights = min_heights;
2746 g_free (min_heights);
2748 if (ret_nat_heights)
2749 *ret_nat_heights = nat_heights;
2751 g_free (nat_heights);
2757 gtk_menu_size_allocate (GtkWidget *widget,
2758 GtkAllocation *allocation)
2761 GtkMenuPrivate *priv;
2762 GtkMenuShell *menu_shell;
2764 GtkAllocation child_allocation;
2769 guint vertical_padding;
2770 guint horizontal_padding;
2773 g_return_if_fail (GTK_IS_MENU (widget));
2774 g_return_if_fail (allocation != NULL);
2776 menu = GTK_MENU (widget);
2777 menu_shell = GTK_MENU_SHELL (widget);
2780 gtk_widget_set_allocation (widget, allocation);
2782 gtk_widget_style_get (GTK_WIDGET (menu),
2783 "vertical-padding", &vertical_padding,
2784 "horizontal-padding", &horizontal_padding,
2787 get_menu_padding (widget, &padding);
2788 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2790 g_free (priv->heights);
2791 priv->heights_length = calculate_line_heights (menu,
2796 /* refresh our cached height request */
2797 priv->requested_height = (2 * (border_width + vertical_padding)) +
2798 padding.top + padding.bottom;
2799 for (i = 0; i < priv->heights_length; i++)
2800 priv->requested_height += priv->heights[i];
2802 x = border_width + padding.left + horizontal_padding;
2803 y = border_width + padding.top + vertical_padding;
2804 width = allocation->width - (2 * (border_width + horizontal_padding)) -
2805 padding.left - padding.right;
2806 height = allocation->height - (2 * (border_width + vertical_padding)) -
2807 padding.top - padding.bottom;
2809 if (menu_shell->priv->active)
2810 gtk_menu_scroll_to (menu, priv->scroll_offset);
2812 if (!priv->tearoff_active)
2814 GtkBorder arrow_border;
2816 get_arrows_border (menu, &arrow_border);
2817 y += arrow_border.top;
2818 height -= arrow_border.top;
2819 height -= arrow_border.bottom;
2822 width = MAX (1, width);
2823 height = MAX (1, height);
2825 if (gtk_widget_get_realized (widget))
2827 gdk_window_move_resize (gtk_widget_get_window (widget),
2828 allocation->x, allocation->y,
2829 allocation->width, allocation->height);
2831 gdk_window_move_resize (priv->view_window, x, y, width, height);
2834 if (menu_shell->priv->children)
2836 gint base_width = width / gtk_menu_get_n_columns (menu);
2838 children = menu_shell->priv->children;
2841 child = children->data;
2842 children = children->next;
2844 if (gtk_widget_get_visible (child))
2849 get_effective_child_attach (child, &l, &r, &t, &b);
2851 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2854 tmp = gtk_menu_get_n_columns (menu) - l;
2855 l = gtk_menu_get_n_columns (menu) - r;
2859 child_allocation.width = (r - l) * base_width;
2860 child_allocation.height = 0;
2861 child_allocation.x = l * base_width;
2862 child_allocation.y = 0;
2864 for (i = 0; i < b; i++)
2867 child_allocation.y += priv->heights[i];
2869 child_allocation.height += priv->heights[i];
2872 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2875 gtk_widget_size_allocate (child, &child_allocation);
2876 gtk_widget_queue_draw (child);
2880 /* Resize the item window */
2881 if (gtk_widget_get_realized (widget))
2887 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2888 height += priv->heights[i];
2890 width = gtk_menu_get_n_columns (menu) * base_width;
2891 gdk_window_resize (priv->bin_window, width, height);
2894 if (priv->tearoff_active)
2896 if (height >= priv->requested_height)
2898 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2900 gtk_widget_hide (priv->tearoff_scrollbar);
2901 gtk_menu_set_tearoff_hints (menu, allocation->width);
2903 gtk_menu_scroll_to (menu, 0);
2908 gtk_adjustment_configure (priv->tearoff_adjustment,
2909 gtk_adjustment_get_value (priv->tearoff_adjustment),
2911 priv->requested_height,
2912 gtk_adjustment_get_step_increment (priv->tearoff_adjustment),
2913 gtk_adjustment_get_page_increment (priv->tearoff_adjustment),
2914 allocation->height);
2916 if (!gtk_widget_get_visible (priv->tearoff_scrollbar))
2918 gtk_widget_show (priv->tearoff_scrollbar);
2919 gtk_menu_set_tearoff_hints (menu, allocation->width);
2927 get_arrows_visible_area (GtkMenu *menu,
2928 GdkRectangle *border,
2929 GdkRectangle *upper,
2930 GdkRectangle *lower,
2933 GtkArrowPlacement arrow_placement;
2934 GtkWidget *widget = GTK_WIDGET (menu);
2936 guint vertical_padding;
2937 guint horizontal_padding;
2938 gint scroll_arrow_height;
2939 GtkBorder menu_padding;
2941 gtk_widget_style_get (widget,
2942 "vertical-padding", &vertical_padding,
2943 "horizontal-padding", &horizontal_padding,
2944 "scroll-arrow-vlength", &scroll_arrow_height,
2945 "arrow-placement", &arrow_placement,
2948 get_menu_padding (widget, &menu_padding);
2949 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2950 border->x = border_width + menu_padding.left + horizontal_padding;
2951 border->y = border_width + menu_padding.top + vertical_padding;
2952 border->width = gdk_window_get_width (gtk_widget_get_window (widget));
2953 border->height = gdk_window_get_height (gtk_widget_get_window (widget));
2955 switch (arrow_placement)
2957 case GTK_ARROWS_BOTH:
2958 upper->x = border->x;
2959 upper->y = border->y;
2960 upper->width = border->width - 2 * border->x;
2961 upper->height = scroll_arrow_height;
2963 lower->x = border->x;
2964 lower->y = border->height - border->y - scroll_arrow_height;
2965 lower->width = border->width - 2 * border->x;
2966 lower->height = scroll_arrow_height;
2969 case GTK_ARROWS_START:
2970 upper->x = border->x;
2971 upper->y = border->y;
2972 upper->width = (border->width - 2 * border->x) / 2;
2973 upper->height = scroll_arrow_height;
2975 lower->x = border->x + upper->width;
2976 lower->y = border->y;
2977 lower->width = (border->width - 2 * border->x) / 2;
2978 lower->height = scroll_arrow_height;
2981 case GTK_ARROWS_END:
2982 upper->x = border->x;
2983 upper->y = border->height - border->y - scroll_arrow_height;
2984 upper->width = (border->width - 2 * border->x) / 2;
2985 upper->height = scroll_arrow_height;
2987 lower->x = border->x + upper->width;
2988 lower->y = border->height - border->y - scroll_arrow_height;
2989 lower->width = (border->width - 2 * border->x) / 2;
2990 lower->height = scroll_arrow_height;
2994 g_assert_not_reached();
2995 upper->x = upper->y = upper->width = upper->height = 0;
2996 lower->x = lower->y = lower->width = lower->height = 0;
2999 *arrow_space = scroll_arrow_height - menu_padding.top - menu_padding.bottom;
3003 gtk_menu_draw (GtkWidget *widget,
3007 GtkMenuPrivate *priv;
3008 GtkStyleContext *context;
3009 GdkRectangle border;
3013 GtkBorder menu_padding;
3015 menu = GTK_MENU (widget);
3017 context = gtk_widget_get_style_context (widget);
3019 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
3020 get_menu_padding (widget, &menu_padding);
3022 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
3024 gfloat arrow_scaling;
3027 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
3028 arrow_size = arrow_scaling * arrow_space;
3030 gtk_render_background (context, cr, 0, 0,
3031 gtk_widget_get_allocated_width (widget),
3032 gtk_widget_get_allocated_height (widget));
3033 gtk_render_frame (context, cr, 0, 0,
3034 gtk_widget_get_allocated_width (widget),
3035 gtk_widget_get_allocated_height (widget));
3037 gtk_style_context_save (context);
3038 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
3040 if (priv->upper_arrow_visible && !priv->tearoff_active)
3042 gtk_style_context_save (context);
3043 gtk_style_context_set_state (context, priv->upper_arrow_state);
3045 gtk_render_background (context, cr,
3047 upper.width, upper.height);
3048 gtk_render_frame (context, cr,
3050 upper.width, upper.height);
3052 gtk_render_arrow (context, cr, 0,
3053 upper.x + (upper.width - arrow_size) / 2,
3054 upper.y + menu_padding.top + (arrow_space - arrow_size) / 2,
3057 gtk_style_context_restore (context);
3060 if (priv->lower_arrow_visible && !priv->tearoff_active)
3062 gtk_style_context_save (context);
3063 gtk_style_context_set_state (context, priv->lower_arrow_state);
3065 gtk_render_background (context, cr,
3067 lower.width, lower.height);
3068 gtk_render_frame (context, cr,
3070 lower.width, lower.height);
3072 gtk_render_arrow (context, cr, G_PI,
3073 lower.x + (lower.width - arrow_size) / 2,
3074 lower.y + menu_padding.bottom + (arrow_space - arrow_size) / 2,
3077 gtk_style_context_restore (context);
3080 gtk_style_context_restore (context);
3083 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
3085 gint y = -border.y + priv->scroll_offset;
3088 gtk_cairo_transform_to_window (cr, widget, priv->bin_window);
3090 if (!priv->tearoff_active)
3092 GtkBorder arrow_border;
3094 get_arrows_border (menu, &arrow_border);
3095 y -= arrow_border.top;
3098 gtk_render_background (context, cr,
3100 border.width, border.height);
3101 gtk_render_frame (context, cr,
3103 border.width, border.height);
3108 GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
3114 gtk_menu_show (GtkWidget *widget)
3116 GtkMenu *menu = GTK_MENU (widget);
3118 _gtk_menu_refresh_accel_paths (menu, FALSE);
3120 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3125 gtk_menu_get_preferred_width (GtkWidget *widget,
3130 GtkMenuShell *menu_shell;
3131 GtkMenuPrivate *priv;
3134 guint max_toggle_size;
3135 guint max_accel_width;
3136 guint horizontal_padding;
3138 gint child_min, child_nat;
3139 gint min_width, nat_width;
3142 menu = GTK_MENU (widget);
3143 menu_shell = GTK_MENU_SHELL (widget);
3146 min_width = nat_width = 0;
3148 max_toggle_size = 0;
3149 max_accel_width = 0;
3151 children = menu_shell->priv->children;
3158 child = children->data;
3159 children = children->next;
3161 if (! gtk_widget_get_visible (child))
3164 get_effective_child_attach (child, &l, &r, &t, &b);
3166 /* It's important to size_request the child
3167 * before doing the toggle size request, in
3168 * case the toggle size request depends on the size
3169 * request of a child of the child (e.g. for ImageMenuItem)
3171 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
3173 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3174 max_toggle_size = MAX (max_toggle_size, toggle_size);
3175 max_accel_width = MAX (max_accel_width,
3176 GTK_MENU_ITEM (child)->priv->accelerator_width);
3178 part = child_min / (r - l);
3179 min_width = MAX (min_width, part);
3181 part = child_nat / (r - l);
3182 nat_width = MAX (nat_width, part);
3185 /* If the menu doesn't include any images or check items
3186 * reserve the space so that all menus are consistent.
3187 * We only do this for 'ordinary' menus, not for combobox
3188 * menus or multi-column menus
3190 if (max_toggle_size == 0 &&
3191 gtk_menu_get_n_columns (menu) == 1 &&
3192 !priv->no_toggle_size)
3194 GtkStyleContext *context;
3195 GtkWidgetPath *check_path;
3196 guint toggle_spacing;
3197 guint indicator_size;
3199 context = gtk_style_context_new ();
3201 /* Create a GtkCheckMenuItem path, only to query indicator spacing */
3202 check_path = _gtk_widget_create_path (widget);
3203 gtk_widget_path_append_type (check_path, GTK_TYPE_CHECK_MENU_ITEM);
3205 gtk_style_context_set_path (context, check_path);
3206 gtk_widget_path_free (check_path);
3207 gtk_style_context_set_screen (context, gtk_widget_get_screen (widget));
3209 gtk_style_context_get_style (context,
3210 "toggle-spacing", &toggle_spacing,
3211 "indicator-size", &indicator_size,
3214 max_toggle_size = indicator_size + toggle_spacing;
3216 g_object_unref (context);
3219 min_width += 2 * max_toggle_size + max_accel_width;
3220 min_width *= gtk_menu_get_n_columns (menu);
3222 nat_width += 2 * max_toggle_size + max_accel_width;
3223 nat_width *= gtk_menu_get_n_columns (menu);
3225 gtk_widget_style_get (GTK_WIDGET (menu),
3226 "horizontal-padding", &horizontal_padding,
3229 get_menu_padding (widget, &padding);
3230 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3231 min_width += (2 * (border_width + horizontal_padding)) +
3232 padding.left + padding.right;
3233 nat_width += (2 * (border_width + horizontal_padding)) +
3234 padding.left + padding.right;
3236 priv->toggle_size = max_toggle_size;
3237 priv->accel_size = max_accel_width;
3240 *minimum_size = min_width;
3243 *natural_size = nat_width;
3245 /* Don't resize the tearoff if it is not active,
3246 * because it won't redraw (it is only a background pixmap).
3248 if (priv->tearoff_active)
3249 gtk_menu_set_tearoff_hints (menu, min_width);
3253 gtk_menu_get_preferred_height (GtkWidget *widget,
3259 /* Menus are height-for-width only, just return the height
3260 * for the minimum width
3262 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
3263 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
3267 gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
3273 GtkMenu *menu = GTK_MENU (widget);
3274 GtkMenuPrivate *priv = menu->priv;
3275 guint *min_heights, *nat_heights;
3276 guint vertical_padding, border_width;
3278 gint min_height, nat_height;
3280 gtk_widget_style_get (widget, "vertical-padding", &vertical_padding, NULL);
3281 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3282 get_menu_padding (widget, &padding);
3284 min_height = nat_height = (border_width + vertical_padding) * 2 +
3285 padding.top + padding.bottom;
3288 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3290 for (i = 0; i < n_heights; i++)
3292 min_height += min_heights[i];
3293 nat_height += nat_heights[i];
3296 if (priv->have_position)
3298 GdkScreen *screen = gtk_widget_get_screen (priv->toplevel);
3299 GdkRectangle monitor;
3301 gdk_screen_get_monitor_workarea (screen, priv->monitor_num, &monitor);
3303 if (priv->position_y + min_height > monitor.y + monitor.height)
3304 min_height = monitor.y + monitor.height - priv->position_y;
3306 if (priv->position_y + nat_height > monitor.y + monitor.height)
3307 nat_height = monitor.y + monitor.height - priv->position_y;
3309 if (priv->position_y < monitor.y)
3311 min_height -= monitor.y - priv->position_y;
3312 nat_height -= monitor.y - priv->position_y;
3317 *minimum_size = min_height;
3320 *natural_size = nat_height;
3322 g_free (min_heights);
3323 g_free (nat_heights);
3327 pointer_in_menu_window (GtkWidget *widget,
3331 GtkMenu *menu = GTK_MENU (widget);
3332 GtkMenuPrivate *priv = menu->priv;
3333 GtkAllocation allocation;
3335 if (gtk_widget_get_mapped (priv->toplevel))
3337 GtkMenuShell *menu_shell;
3338 gint window_x, window_y;
3340 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
3341 &window_x, &window_y);
3343 gtk_widget_get_allocation (widget, &allocation);
3344 if (x_root >= window_x && x_root < window_x + allocation.width &&
3345 y_root >= window_y && y_root < window_y + allocation.height)
3348 menu_shell = GTK_MENU_SHELL (widget);
3350 if (GTK_IS_MENU (menu_shell->priv->parent_menu_shell))
3351 return pointer_in_menu_window (menu_shell->priv->parent_menu_shell,
3359 gtk_menu_button_press (GtkWidget *widget,
3360 GdkEventButton *event)
3362 GdkDevice *source_device;
3363 GtkWidget *event_widget;
3366 if (event->type != GDK_BUTTON_PRESS)
3369 source_device = gdk_event_get_source_device ((GdkEvent *) event);
3370 event_widget = gtk_get_event_widget ((GdkEvent *) event);
3371 menu = GTK_MENU (widget);
3373 /* Don't pass down to menu shell if a non-menuitem part of the menu
3374 * was clicked. The check for the event_widget being a GtkMenuShell
3375 * works because we have the pointer grabbed on menu_shell->window
3376 * with owner_events=TRUE, so all events that are either outside
3377 * the menu or on its border are delivered relative to
3378 * menu_shell->window.
3380 if (GTK_IS_MENU_SHELL (event_widget) &&
3381 pointer_in_menu_window (widget, event->x_root, event->y_root))
3384 if (GTK_IS_MENU_ITEM (event_widget) &&
3385 gdk_device_get_source (source_device) == GDK_SOURCE_TOUCHSCREEN &&
3386 GTK_MENU_ITEM (event_widget)->priv->submenu != NULL &&
3387 !gtk_widget_is_drawable (GTK_MENU_ITEM (event_widget)->priv->submenu))
3388 menu->priv->ignore_button_release = TRUE;
3390 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3394 gtk_menu_button_release (GtkWidget *widget,
3395 GdkEventButton *event)
3397 GtkMenuPrivate *priv = GTK_MENU (widget)->priv;
3399 if (priv->ignore_button_release)
3401 priv->ignore_button_release = FALSE;
3405 if (event->type != GDK_BUTTON_RELEASE)
3408 /* Don't pass down to menu shell if a non-menuitem part of the menu
3409 * was clicked (see comment in button_press()).
3411 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3412 pointer_in_menu_window (widget, event->x_root, event->y_root))
3414 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3415 * bail out early here so it is in a consistent state for the
3416 * next button_press/button_release in GtkMenuShell.
3419 if (GTK_MENU_SHELL (widget)->priv->active)
3420 GTK_MENU_SHELL (widget)->priv->button = 0;
3425 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3428 static const gchar *
3429 get_accel_path (GtkWidget *menu_item,
3434 GClosure *accel_closure;
3435 GtkAccelGroup *accel_group;
3437 path = _gtk_widget_get_accel_path (menu_item, locked);
3440 path = GTK_MENU_ITEM (menu_item)->priv->accel_path;
3446 label = gtk_bin_get_child (GTK_BIN (menu_item));
3448 if (GTK_IS_ACCEL_LABEL (label))
3450 g_object_get (label,
3451 "accel-closure", &accel_closure,
3455 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3457 *locked = gtk_accel_group_get_is_locked (accel_group);
3467 gtk_menu_key_press (GtkWidget *widget,
3470 GtkMenuShell *menu_shell;
3472 gboolean delete = FALSE;
3473 gboolean can_change_accels;
3474 gchar *accel = NULL;
3475 guint accel_key, accel_mods;
3476 GdkModifierType consumed_modifiers;
3477 GdkDisplay *display;
3479 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3480 g_return_val_if_fail (event != NULL, FALSE);
3482 menu_shell = GTK_MENU_SHELL (widget);
3483 menu = GTK_MENU (widget);
3485 gtk_menu_stop_navigating_submenu (menu);
3487 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3490 display = gtk_widget_get_display (widget);
3492 g_object_get (gtk_widget_get_settings (widget),
3493 "gtk-menu-bar-accel", &accel,
3494 "gtk-can-change-accels", &can_change_accels,
3497 if (accel && *accel)
3500 GdkModifierType mods = 0;
3502 gtk_accelerator_parse (accel, &keyval, &mods);
3505 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3507 /* FIXME this is wrong, needs to be in the global accel resolution
3508 * thing, to properly consider i18n etc., but that probably requires
3509 * AccelGroup changes etc.
3511 if (event->keyval == keyval && (mods & event->state) == mods)
3513 gtk_menu_shell_cancel (menu_shell);
3521 switch (event->keyval)
3523 case GDK_KEY_Delete:
3524 case GDK_KEY_KP_Delete:
3525 case GDK_KEY_BackSpace:
3532 /* Figure out what modifiers went into determining the key symbol */
3533 _gtk_translate_keyboard_accel_state (gdk_keymap_get_for_display (display),
3534 event->hardware_keycode,
3536 gtk_accelerator_get_default_mod_mask (),
3538 &accel_key, NULL, NULL, &consumed_modifiers);
3540 accel_key = gdk_keyval_to_lower (accel_key);
3541 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3543 /* If lowercasing affects the keysym, then we need to include SHIFT
3544 * in the modifiers, We re-upper case when we match against the
3545 * keyval, but display and save in caseless form.
3547 if (accel_key != event->keyval)
3548 accel_mods |= GDK_SHIFT_MASK;
3550 /* Modify the accelerators */
3551 if (can_change_accels &&
3552 menu_shell->priv->active_menu_item &&
3553 gtk_bin_get_child (GTK_BIN (menu_shell->priv->active_menu_item)) && /* no separators */
3554 GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu == NULL && /* no submenus */
3555 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3557 GtkWidget *menu_item = menu_shell->priv->active_menu_item;
3558 gboolean locked, replace_accels = TRUE;
3561 path = get_accel_path (menu_item, &locked);
3562 if (!path || locked)
3564 /* Can't change accelerators on menu_items without paths
3565 * (basically, those items are accelerator-locked).
3567 gtk_widget_error_bell (widget);
3573 /* For the keys that act to delete the current setting,
3574 * we delete the current setting if there is one, otherwise,
3575 * we set the key as the accelerator.
3581 if (gtk_accel_map_lookup_entry (path, &key) &&
3582 (key.accel_key || key.accel_mods))
3588 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3592 /* We failed, probably because this key is in use
3593 * and locked already.
3595 gtk_widget_error_bell (widget);
3604 check_threshold (GtkWidget *widget,
3613 ABS (start_x - x) > THRESHOLD ||
3614 ABS (start_y - y) > THRESHOLD;
3618 definitely_within_item (GtkWidget *widget,
3622 GdkWindow *window = GTK_MENU_ITEM (widget)->priv->event_window;
3625 w = gdk_window_get_width (window);
3626 h = gdk_window_get_height (window);
3629 check_threshold (widget, 0, 0, x, y) &&
3630 check_threshold (widget, w - 1, 0, x, y) &&
3631 check_threshold (widget, w - 1, h - 1, x, y) &&
3632 check_threshold (widget, 0, h - 1, x, y);
3636 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3638 GtkMenuPrivate *priv = menu->priv;
3640 return priv->navigation_height && priv->navigation_width;
3644 gtk_menu_motion_notify (GtkWidget *widget,
3645 GdkEventMotion *event)
3647 GtkWidget *menu_item;
3649 GtkMenuShell *menu_shell;
3651 GdkDevice *source_device;
3653 gboolean need_enter;
3655 source_device = gdk_event_get_source_device ((GdkEvent *) event);
3657 if (GTK_IS_MENU (widget) &&
3658 gdk_device_get_source (source_device) != GDK_SOURCE_TOUCHSCREEN)
3660 GtkMenuPrivate *priv = GTK_MENU(widget)->priv;
3662 if (priv->ignore_button_release)
3663 priv->ignore_button_release = FALSE;
3665 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3669 /* We received the event for one of two reasons:
3671 * a) We are the active menu, and did gtk_grab_add()
3672 * b) The widget is a child of ours, and the event was propagated
3674 * Since for computation of navigation regions, we want the menu which
3675 * is the parent of the menu item, for a), we need to find that menu,
3676 * which may be different from 'widget'.
3678 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3679 parent = gtk_widget_get_parent (menu_item);
3680 if (!GTK_IS_MENU_ITEM (menu_item) ||
3681 !GTK_IS_MENU (parent))
3684 menu_shell = GTK_MENU_SHELL (parent);
3685 menu = GTK_MENU (menu_shell);
3687 if (definitely_within_item (menu_item, event->x, event->y))
3688 menu_shell->priv->activate_time = 0;
3690 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->priv->ignore_enter);
3692 /* Check to see if we are within an active submenu's navigation region
3694 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3697 /* Make sure we pop down if we enter a non-selectable menu item, so we
3698 * don't show a submenu when the cursor is outside the stay-up triangle.
3700 if (!_gtk_menu_item_is_selectable (menu_item))
3702 /* We really want to deselect, but this gives the menushell code
3703 * a chance to do some bookkeeping about the menuitem.
3705 gtk_menu_shell_select_item (menu_shell, menu_item);
3711 /* The menu is now sensitive to enter events on its items, but
3712 * was previously sensitive. So we fake an enter event.
3714 menu_shell->priv->ignore_enter = FALSE;
3716 if (event->x >= 0 && event->x < gdk_window_get_width (event->window) &&
3717 event->y >= 0 && event->y < gdk_window_get_height (event->window))
3719 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3722 send_event->crossing.window = g_object_ref (event->window);
3723 send_event->crossing.time = event->time;
3724 send_event->crossing.send_event = TRUE;
3725 send_event->crossing.x_root = event->x_root;
3726 send_event->crossing.y_root = event->y_root;
3727 send_event->crossing.x = event->x;
3728 send_event->crossing.y = event->y;
3729 send_event->crossing.state = event->state;
3730 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3732 /* We send the event to 'widget', the currently active menu,
3733 * instead of 'menu', the menu that the pointer is in. This
3734 * will ensure that the event will be ignored unless the
3735 * menuitem is a child of the active menu or some parent
3736 * menu of the active menu.
3738 result = gtk_widget_event (widget, send_event);
3739 gdk_event_free (send_event);
3749 get_double_arrows (GtkMenu *menu)
3751 GtkMenuPrivate *priv = menu->priv;
3752 gboolean double_arrows;
3753 GtkArrowPlacement arrow_placement;
3755 gtk_widget_style_get (GTK_WIDGET (menu),
3756 "double-arrows", &double_arrows,
3757 "arrow-placement", &arrow_placement,
3760 if (arrow_placement != GTK_ARROWS_BOTH)
3763 return double_arrows || (priv->initially_pushed_in &&
3764 priv->scroll_offset != 0);
3768 gtk_menu_scroll_by (GtkMenu *menu,
3771 GtkMenuPrivate *priv = menu->priv;
3772 GtkBorder arrow_border;
3776 gboolean double_arrows;
3778 widget = GTK_WIDGET (menu);
3779 offset = priv->scroll_offset + step;
3781 get_arrows_border (menu, &arrow_border);
3783 double_arrows = get_double_arrows (menu);
3785 /* If we scroll upward and the non-visible top part
3786 * is smaller than the scroll arrow it would be
3787 * pretty stupid to show the arrow and taking more
3788 * screen space than just scrolling to the top.
3791 if ((step < 0) && (offset < arrow_border.top))
3794 /* Don't scroll over the top if we weren't before: */
3795 if ((priv->scroll_offset >= 0) && (offset < 0))
3798 view_height = gdk_window_get_height (gtk_widget_get_window (widget));
3800 if (priv->scroll_offset == 0 &&
3801 view_height >= priv->requested_height)
3804 /* Don't scroll past the bottom if we weren't before: */
3805 if (priv->scroll_offset > 0)
3806 view_height -= arrow_border.top;
3808 /* When both arrows are always shown,
3809 * reduce view height even more.
3812 view_height -= arrow_border.bottom;
3814 if ((priv->scroll_offset + view_height <= priv->requested_height) &&
3815 (offset + view_height > priv->requested_height))
3816 offset = priv->requested_height - view_height;
3818 if (offset != priv->scroll_offset)
3819 gtk_menu_scroll_to (menu, offset);
3823 gtk_menu_scroll_timeout (gpointer data)
3827 menu = GTK_MENU (data);
3828 gtk_menu_scroll_by (menu, menu->priv->scroll_step);
3834 gtk_menu_scroll (GtkWidget *widget,
3835 GdkEventScroll *event)
3837 GtkMenu *menu = GTK_MENU (widget);
3839 switch (event->direction)
3841 case GDK_SCROLL_RIGHT:
3842 case GDK_SCROLL_DOWN:
3843 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3845 case GDK_SCROLL_LEFT:
3847 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3849 case GDK_SCROLL_SMOOTH:
3850 gtk_menu_scroll_by (menu, event->delta_y);
3858 get_arrows_sensitive_area (GtkMenu *menu,
3859 GdkRectangle *upper,
3860 GdkRectangle *lower)
3862 GtkArrowPlacement arrow_placement;
3863 GtkWidget *widget = GTK_WIDGET (menu);
3867 guint vertical_padding;
3869 gint scroll_arrow_height;
3872 window = gtk_widget_get_window (widget);
3873 width = gdk_window_get_width (window);
3874 height = gdk_window_get_height (window);
3876 gtk_widget_style_get (widget,
3877 "vertical-padding", &vertical_padding,
3878 "scroll-arrow-vlength", &scroll_arrow_height,
3879 "arrow-placement", &arrow_placement,
3882 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) + vertical_padding;
3883 get_menu_padding (widget, &padding);
3885 gdk_window_get_position (window, &win_x, &win_y);
3887 switch (arrow_placement)
3889 case GTK_ARROWS_BOTH:
3894 upper->width = width;
3895 upper->height = scroll_arrow_height + border + padding.top;
3901 lower->y = win_y + height - border - padding.bottom - scroll_arrow_height;
3902 lower->width = width;
3903 lower->height = scroll_arrow_height + border + padding.bottom;
3907 case GTK_ARROWS_START:
3912 upper->width = width / 2;
3913 upper->height = scroll_arrow_height + border + padding.top;
3918 lower->x = win_x + width / 2;
3920 lower->width = width / 2;
3921 lower->height = scroll_arrow_height + border + padding.bottom;
3925 case GTK_ARROWS_END:
3929 upper->y = win_y + height - border - scroll_arrow_height;
3930 upper->width = width / 2;
3931 upper->height = scroll_arrow_height + border + padding.top;
3936 lower->x = win_x + width / 2;
3937 lower->y = win_y + height - border - scroll_arrow_height;
3938 lower->width = width / 2;
3939 lower->height = scroll_arrow_height + border + padding.bottom;
3947 gtk_menu_handle_scrolling (GtkMenu *menu,
3953 GtkMenuPrivate *priv = menu->priv;
3954 GtkMenuShell *menu_shell;
3957 gboolean scroll_fast = FALSE;
3960 menu_shell = GTK_MENU_SHELL (menu);
3962 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
3967 /* upper arrow handling */
3969 get_arrows_sensitive_area (menu, &rect, NULL);
3972 if (priv->upper_arrow_visible && !priv->tearoff_active &&
3973 (x >= rect.x) && (x < rect.x + rect.width) &&
3974 (y >= rect.y) && (y < rect.y + rect.height))
3979 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
3981 gboolean arrow_pressed = FALSE;
3983 if (priv->upper_arrow_visible && !priv->tearoff_active)
3985 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3987 if (enter && in_arrow &&
3988 (!priv->upper_arrow_prelight ||
3989 priv->scroll_fast != scroll_fast))
3991 priv->upper_arrow_prelight = TRUE;
3992 priv->scroll_fast = scroll_fast;
3994 /* Deselect the active item so that
3995 * any submenus are popped down
3997 gtk_menu_shell_deselect (menu_shell);
3999 gtk_menu_remove_scroll_timeout (menu);
4000 priv->scroll_step = scroll_fast
4001 ? -MENU_SCROLL_STEP2
4002 : -MENU_SCROLL_STEP1;
4004 priv->scroll_timeout =
4005 gdk_threads_add_timeout (scroll_fast
4006 ? MENU_SCROLL_TIMEOUT2
4007 : MENU_SCROLL_TIMEOUT1,
4008 gtk_menu_scroll_timeout, menu);
4010 else if (!enter && !in_arrow && priv->upper_arrow_prelight)
4012 gtk_menu_stop_scrolling (menu);
4016 /* check if the button isn't insensitive before
4017 * changing it to something else.
4019 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4021 GtkStateFlags arrow_state = 0;
4024 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4026 if (priv->upper_arrow_prelight)
4027 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4029 if (arrow_state != priv->upper_arrow_state)
4031 priv->upper_arrow_state = arrow_state;
4033 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4039 /* lower arrow handling */
4041 get_arrows_sensitive_area (menu, NULL, &rect);
4044 if (priv->lower_arrow_visible && !priv->tearoff_active &&
4045 (x >= rect.x) && (x < rect.x + rect.width) &&
4046 (y >= rect.y) && (y < rect.y + rect.height))
4051 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4053 gboolean arrow_pressed = FALSE;
4055 if (priv->lower_arrow_visible && !priv->tearoff_active)
4057 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4059 if (enter && in_arrow &&
4060 (!priv->lower_arrow_prelight ||
4061 priv->scroll_fast != scroll_fast))
4063 priv->lower_arrow_prelight = TRUE;
4064 priv->scroll_fast = scroll_fast;
4066 /* Deselect the active item so that
4067 * any submenus are popped down
4069 gtk_menu_shell_deselect (menu_shell);
4071 gtk_menu_remove_scroll_timeout (menu);
4072 priv->scroll_step = scroll_fast
4074 : MENU_SCROLL_STEP1;
4076 priv->scroll_timeout =
4077 gdk_threads_add_timeout (scroll_fast
4078 ? MENU_SCROLL_TIMEOUT2
4079 : MENU_SCROLL_TIMEOUT1,
4080 gtk_menu_scroll_timeout, menu);
4082 else if (!enter && !in_arrow && priv->lower_arrow_prelight)
4084 gtk_menu_stop_scrolling (menu);
4088 /* check if the button isn't insensitive before
4089 * changing it to something else.
4091 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4093 GtkStateFlags arrow_state = 0;
4096 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4098 if (priv->lower_arrow_prelight)
4099 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4101 if (arrow_state != priv->lower_arrow_state)
4103 priv->lower_arrow_state = arrow_state;
4105 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4113 gtk_menu_enter_notify (GtkWidget *widget,
4114 GdkEventCrossing *event)
4116 GtkWidget *menu_item;
4118 GdkDevice *source_device;
4120 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4121 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4122 event->mode == GDK_CROSSING_STATE_CHANGED)
4125 source_device = gdk_event_get_source_device ((GdkEvent *) event);
4126 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4128 if (GTK_IS_MENU (widget) &&
4129 gdk_device_get_source (source_device) != GDK_SOURCE_TOUCHSCREEN)
4131 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4133 if (!menu_shell->priv->ignore_enter)
4134 gtk_menu_handle_scrolling (GTK_MENU (widget),
4135 event->x_root, event->y_root, TRUE, TRUE);
4138 if (gdk_device_get_source (source_device) != GDK_SOURCE_TOUCHSCREEN &&
4139 GTK_IS_MENU_ITEM (menu_item))
4141 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4143 if (GTK_IS_MENU (menu))
4145 GtkMenuPrivate *priv = (GTK_MENU (menu))->priv;
4146 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4148 if (priv->seen_item_enter)
4150 /* This is the second enter we see for an item
4151 * on this menu. This means a release should always
4154 menu_shell->priv->activate_time = 0;
4156 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4157 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4159 if (definitely_within_item (menu_item, event->x, event->y))
4161 /* This is an actual user-enter (ie. not a pop-under)
4162 * In this case, the user must either have entered
4163 * sufficiently far enough into the item, or he must move
4164 * far enough away from the enter point. (see
4165 * gtk_menu_motion_notify())
4167 menu_shell->priv->activate_time = 0;
4171 priv->seen_item_enter = TRUE;
4175 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4176 * will not correspond to the event widget's parent. Check to see
4177 * if we are in the parent's navigation region.
4179 parent = gtk_widget_get_parent (menu_item);
4180 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4181 gtk_menu_navigating_submenu (GTK_MENU (parent),
4182 event->x_root, event->y_root))
4185 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4189 gtk_menu_leave_notify (GtkWidget *widget,
4190 GdkEventCrossing *event)
4192 GtkMenuShell *menu_shell;
4194 GtkMenuItem *menu_item;
4195 GtkWidget *event_widget;
4196 GdkDevice *source_device;
4198 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4199 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4200 event->mode == GDK_CROSSING_STATE_CHANGED)
4203 menu = GTK_MENU (widget);
4204 menu_shell = GTK_MENU_SHELL (widget);
4206 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4209 source_device = gdk_event_get_source_device ((GdkEvent *) event);
4211 if (gdk_device_get_source (source_device) != GDK_SOURCE_TOUCHSCREEN)
4212 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4214 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4216 if (!GTK_IS_MENU_ITEM (event_widget))
4219 menu_item = GTK_MENU_ITEM (event_widget);
4221 /* Here we check to see if we're leaving an active menu item
4222 * with a submenu, in which case we enter submenu navigation mode.
4224 if (menu_shell->priv->active_menu_item != NULL
4225 && menu_item->priv->submenu != NULL
4226 && menu_item->priv->submenu_placement == GTK_LEFT_RIGHT)
4228 if (GTK_MENU_SHELL (menu_item->priv->submenu)->priv->active)
4230 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4233 else if (menu_item == GTK_MENU_ITEM (menu_shell->priv->active_menu_item))
4235 /* We are leaving an active menu item with nonactive submenu.
4236 * Deselect it so we don't surprise the user with by popping
4237 * up a submenu _after_ he left the item.
4239 gtk_menu_shell_deselect (menu_shell);
4244 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4248 pointer_on_menu_widget (GtkMenu *menu,
4252 GtkMenuPrivate *priv = menu->priv;
4253 GtkAllocation allocation;
4254 gint window_x, window_y;
4256 gtk_widget_get_allocation (GTK_WIDGET (menu), &allocation);
4257 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
4258 &window_x, &window_y);
4260 if (x_root >= window_x && x_root < window_x + allocation.width &&
4261 y_root >= window_y && y_root < window_y + allocation.height)
4268 gtk_menu_captured_event (GtkWidget *widget,
4271 GdkDevice *source_device;
4272 gboolean retval = FALSE;
4273 GtkMenuPrivate *priv;
4275 gdouble x_root, y_root;
4277 GdkModifierType state;
4279 menu = GTK_MENU (widget);
4282 if (!priv->upper_arrow_visible && !priv->lower_arrow_visible && priv->drag_start_y < 0)
4285 source_device = gdk_event_get_source_device (event);
4286 gdk_event_get_root_coords (event, &x_root, &y_root);
4288 switch (event->type)
4290 case GDK_TOUCH_BEGIN:
4291 case GDK_BUTTON_PRESS:
4292 if ((!gdk_event_get_button (event, &button) || button == 1) &&
4293 gdk_device_get_source (source_device) == GDK_SOURCE_TOUCHSCREEN &&
4294 pointer_on_menu_widget (menu, x_root, y_root))
4296 priv->drag_start_y = event->button.y_root;
4297 priv->initial_drag_offset = priv->scroll_offset;
4298 priv->drag_scroll_started = FALSE;
4301 priv->drag_start_y = -1;
4303 priv->drag_already_pressed = TRUE;
4306 case GDK_BUTTON_RELEASE:
4307 if (priv->drag_scroll_started)
4309 priv->drag_scroll_started = FALSE;
4310 priv->drag_start_y = -1;
4311 priv->drag_already_pressed = FALSE;
4315 case GDK_TOUCH_UPDATE:
4316 case GDK_MOTION_NOTIFY:
4317 if ((!gdk_event_get_state (event, &state) || (state & GDK_BUTTON1_MASK) != 0) &&
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,
4356 MIN (priv->scroll_offset, 0),
4357 MAX (priv->scroll_offset, priv->requested_height - view_height));
4359 gtk_menu_scroll_to (menu, offset);
4363 else if (gtk_drag_check_threshold (widget,
4364 0, priv->drag_start_y,
4367 priv->drag_scroll_started = TRUE;
4368 gtk_menu_shell_deselect (GTK_MENU_SHELL (menu));
4373 case GDK_ENTER_NOTIFY:
4374 case GDK_LEAVE_NOTIFY:
4375 if (priv->drag_scroll_started)
4386 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4388 GtkMenuPrivate *priv = menu->priv;
4390 priv->navigation_x = 0;
4391 priv->navigation_y = 0;
4392 priv->navigation_width = 0;
4393 priv->navigation_height = 0;
4395 if (priv->navigation_timeout)
4397 g_source_remove (priv->navigation_timeout);
4398 priv->navigation_timeout = 0;
4402 /* When the timeout is elapsed, the navigation region is destroyed
4403 * and the menuitem under the pointer (if any) is selected.
4406 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4408 GtkMenuPopdownData *popdown_data = user_data;
4409 GtkMenu *menu = popdown_data->menu;
4410 GtkMenuPrivate *priv = menu->priv;
4411 GdkWindow *child_window;
4413 gtk_menu_stop_navigating_submenu (menu);
4415 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4417 child_window = gdk_window_get_device_position (priv->bin_window,
4418 popdown_data->device,
4423 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4425 send_event->crossing.window = g_object_ref (child_window);
4426 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4427 send_event->crossing.send_event = TRUE;
4428 gdk_event_set_device (send_event, popdown_data->device);
4430 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4432 gdk_event_free (send_event);
4440 gtk_menu_navigating_submenu (GtkMenu *menu,
4444 GtkMenuPrivate *priv = menu->priv;
4447 if (!gtk_menu_has_navigation_triangle (menu))
4450 width = priv->navigation_width;
4451 height = priv->navigation_height;
4453 /* Check if x/y are in the triangle spanned by the navigation parameters */
4455 /* 1) Move the coordinates so the triangle starts at 0,0 */
4456 event_x -= priv->navigation_x;
4457 event_y -= priv->navigation_y;
4459 /* 2) Ensure both legs move along the positive axis */
4471 /* 3) Check that the given coordinate is inside the triangle. The formula
4472 * is a transformed form of this formula: x/w + y/h <= 1
4474 if (event_x >= 0 && event_y >= 0 &&
4475 event_x * height + event_y * width <= width * height)
4481 gtk_menu_stop_navigating_submenu (menu);
4487 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4488 GtkMenuItem *menu_item,
4489 GdkEventCrossing *event)
4491 GtkMenuPrivate *priv = menu->priv;
4492 gint submenu_left = 0;
4493 gint submenu_right = 0;
4494 gint submenu_top = 0;
4495 gint submenu_bottom = 0;
4497 GtkWidget *event_widget;
4498 GtkMenuPopdownData *popdown_data;
4501 g_return_if_fail (menu_item->priv->submenu != NULL);
4502 g_return_if_fail (event != NULL);
4504 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4506 window = gtk_widget_get_window (menu_item->priv->submenu);
4507 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4509 submenu_right = submenu_left + gdk_window_get_width (window);
4510 submenu_bottom = submenu_top + gdk_window_get_height (window);
4512 width = gdk_window_get_width (gtk_widget_get_window (event_widget));
4514 if (event->x >= 0 && event->x < width)
4518 gtk_menu_stop_navigating_submenu (menu);
4520 /* The navigation region is the triangle closest to the x/y
4521 * location of the rectangle. This is why the width or height
4524 if (menu_item->priv->submenu_direction == GTK_DIRECTION_RIGHT)
4527 priv->navigation_x = submenu_left;
4528 priv->navigation_width = event->x_root - submenu_left;
4533 priv->navigation_x = submenu_right;
4534 priv->navigation_width = event->x_root - submenu_right;
4540 priv->navigation_y = event->y_root;
4541 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4543 if (priv->navigation_height >= 0)
4549 priv->navigation_y = event->y_root;
4550 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4552 if (priv->navigation_height <= 0)
4556 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4557 "gtk-menu-popdown-delay", &popdown_delay,
4560 popdown_data = g_new (GtkMenuPopdownData, 1);
4561 popdown_data->menu = menu;
4562 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4564 priv->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4566 gtk_menu_stop_navigating_submenu_cb,
4568 (GDestroyNotify) g_free);
4573 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4577 g_return_if_fail (GTK_IS_MENU (menu_shell));
4579 parent = menu_shell->priv->parent_menu_shell;
4581 menu_shell->priv->activate_time = 0;
4582 gtk_menu_popdown (GTK_MENU (menu_shell));
4585 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4589 gtk_menu_position (GtkMenu *menu,
4590 gboolean set_scroll_offset)
4592 GtkMenuPrivate *priv = menu->priv;
4594 GtkRequisition requisition;
4598 GdkScreen *pointer_screen;
4599 GdkRectangle monitor;
4602 widget = GTK_WIDGET (menu);
4604 screen = gtk_widget_get_screen (widget);
4605 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4606 gdk_device_get_position (pointer, &pointer_screen, &x, &y);
4608 /* Realize so we have the proper width and heigh to figure out
4609 * the right place to popup the menu.
4611 gtk_widget_realize (priv->toplevel);
4612 requisition.width = gtk_widget_get_allocated_width (widget);
4613 requisition.height = gtk_widget_get_allocated_height (widget);
4615 if (pointer_screen != screen)
4617 /* Pointer is on a different screen; roughly center the
4618 * menu on the screen. If someone was using multiscreen
4619 * + Xinerama together they'd probably want something
4620 * fancier; but that is likely to be vanishingly rare.
4622 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4623 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4626 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4627 priv->initially_pushed_in = FALSE;
4629 /* Set the type hint here to allow custom position functions
4630 * to set a different hint
4632 if (!gtk_widget_get_visible (priv->toplevel))
4633 gtk_window_set_type_hint (GTK_WINDOW (priv->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4635 if (priv->position_func)
4637 (* priv->position_func) (menu, &x, &y, &priv->initially_pushed_in,
4638 priv->position_func_data);
4640 if (priv->monitor_num < 0)
4641 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4643 gdk_screen_get_monitor_workarea (screen, priv->monitor_num, &monitor);
4647 gint space_left, space_right, space_above, space_below;
4651 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4653 get_menu_padding (widget, &padding);
4655 /* The placement of popup menus horizontally works like this (with
4656 * RTL in parentheses)
4658 * - If there is enough room to the right (left) of the mouse cursor,
4659 * position the menu there.
4661 * - Otherwise, if if there is enough room to the left (right) of the
4662 * mouse cursor, position the menu there.
4664 * - Otherwise if the menu is smaller than the monitor, position it
4665 * on the side of the mouse cursor that has the most space available
4667 * - Otherwise (if there is simply not enough room for the menu on the
4668 * monitor), position it as far left (right) as possible.
4670 * Positioning in the vertical direction is similar: first try below
4671 * mouse cursor, then above.
4673 gdk_screen_get_monitor_workarea (screen, priv->monitor_num, &monitor);
4675 space_left = x - monitor.x;
4676 space_right = monitor.x + monitor.width - x - 1;
4677 space_above = y - monitor.y;
4678 space_below = monitor.y + monitor.height - y - 1;
4680 /* Position horizontally. */
4682 /* the amount of space we need to position the menu.
4683 * Note the menu is offset "thickness" pixels
4685 needed_width = requisition.width - padding.left;
4687 if (needed_width <= space_left ||
4688 needed_width <= space_right)
4690 if ((rtl && needed_width <= space_left) ||
4691 (!rtl && needed_width > space_right))
4694 x = x + padding.left - requisition.width + 1;
4698 /* position right */
4699 x = x - padding.right;
4702 /* x is clamped on-screen further down */
4704 else if (requisition.width <= monitor.width)
4706 /* the menu is too big to fit on either side of the mouse
4707 * cursor, but smaller than the monitor. Position it on
4708 * the side that has the most space
4710 if (space_left > space_right)
4718 x = monitor.x + monitor.width - requisition.width;
4721 else /* menu is simply too big for the monitor */
4726 x = monitor.x + monitor.width - requisition.width;
4735 /* Position vertically.
4736 * The algorithm is the same as above, but simpler
4737 * because we don't have to take RTL into account.
4739 needed_height = requisition.height - padding.top;
4741 if (needed_height <= space_above ||
4742 needed_height <= space_below)
4744 if (needed_height <= space_below)
4745 y = y - padding.top;
4747 y = y + padding.bottom - requisition.height + 1;
4749 y = CLAMP (y, monitor.y,
4750 monitor.y + monitor.height - requisition.height);
4752 else if (needed_height > space_below && needed_height > space_above)
4754 if (space_below >= space_above)
4755 y = monitor.y + monitor.height - requisition.height;
4767 if (y + requisition.height > monitor.y + monitor.height)
4769 if (priv->initially_pushed_in)
4770 scroll_offset += (monitor.y + monitor.height) - requisition.height - y;
4771 y = (monitor.y + monitor.height) - requisition.height;
4776 if (priv->initially_pushed_in)
4777 scroll_offset += monitor.y - y;
4781 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4783 if (GTK_MENU_SHELL (menu)->priv->active)
4785 priv->have_position = TRUE;
4786 priv->position_x = x;
4787 priv->position_y = y;
4790 if (scroll_offset != 0)
4792 GtkBorder arrow_border;
4794 get_arrows_border (menu, &arrow_border);
4795 scroll_offset += arrow_border.top;
4798 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->priv->active ? priv->toplevel : priv->tearoff_window),
4801 if (!GTK_MENU_SHELL (menu)->priv->active)
4803 gtk_window_resize (GTK_WINDOW (priv->tearoff_window),
4804 requisition.width, requisition.height);
4807 if (set_scroll_offset)
4808 priv->scroll_offset = scroll_offset;
4812 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4814 GtkMenuPrivate *priv = menu->priv;
4816 if (priv->scroll_timeout)
4818 g_source_remove (priv->scroll_timeout);
4819 priv->scroll_timeout = 0;
4824 gtk_menu_stop_scrolling (GtkMenu *menu)
4826 GtkMenuPrivate *priv = menu->priv;
4828 gtk_menu_remove_scroll_timeout (menu);
4829 priv->upper_arrow_prelight = FALSE;
4830 priv->lower_arrow_prelight = FALSE;
4834 gtk_menu_scroll_to (GtkMenu *menu,
4837 GtkMenuPrivate *priv = menu->priv;
4838 GtkBorder arrow_border, padding;
4841 gint view_width, view_height;
4844 guint vertical_padding;
4845 guint horizontal_padding;
4846 gboolean double_arrows;
4848 widget = GTK_WIDGET (menu);
4850 if (priv->tearoff_active && priv->tearoff_adjustment)
4851 gtk_adjustment_set_value (priv->tearoff_adjustment, offset);
4853 /* Move/resize the viewport according to arrows: */
4854 view_width = gtk_widget_get_allocated_width (widget);
4855 view_height = gtk_widget_get_allocated_height (widget);
4857 gtk_widget_style_get (GTK_WIDGET (menu),
4858 "vertical-padding", &vertical_padding,
4859 "horizontal-padding", &horizontal_padding,
4862 get_menu_padding (widget, &padding);
4863 double_arrows = get_double_arrows (menu);
4865 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4867 view_width -= (2 * (border_width + horizontal_padding)) + padding.left + padding.right;
4868 view_height -= (2 * (border_width + vertical_padding)) + padding.top + padding.bottom;
4869 menu_height = priv->requested_height - (2 * (border_width + vertical_padding)) -
4870 padding.top - padding.bottom;
4872 x = border_width + padding.left + horizontal_padding;
4873 y = border_width + padding.top + vertical_padding;
4875 if (double_arrows && !priv->tearoff_active)
4877 if (view_height < menu_height ||
4878 (offset > 0 && priv->scroll_offset > 0) ||
4879 (offset < 0 && priv->scroll_offset < 0))
4881 GtkStateFlags upper_arrow_previous_state = priv->upper_arrow_state;
4882 GtkStateFlags lower_arrow_previous_state = priv->lower_arrow_state;
4884 if (!priv->upper_arrow_visible || !priv->lower_arrow_visible)
4885 gtk_widget_queue_draw (GTK_WIDGET (menu));
4887 priv->upper_arrow_visible = priv->lower_arrow_visible = TRUE;
4889 get_arrows_border (menu, &arrow_border);
4890 y += arrow_border.top;
4891 view_height -= arrow_border.top;
4892 view_height -= arrow_border.bottom;
4895 priv->upper_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4898 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4900 if (priv->upper_arrow_prelight)
4901 priv->upper_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4903 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4906 if (offset >= menu_height - view_height)
4907 priv->lower_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4910 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4912 if (priv->lower_arrow_prelight)
4913 priv->lower_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4915 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4918 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4919 (priv->lower_arrow_state != lower_arrow_previous_state))
4920 gtk_widget_queue_draw (GTK_WIDGET (menu));
4922 if ((upper_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
4923 (priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
4925 /* At the upper border, possibly remove timeout */
4926 if (priv->scroll_step < 0)
4928 gtk_menu_stop_scrolling (menu);
4929 gtk_widget_queue_draw (GTK_WIDGET (menu));
4933 if ((lower_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
4934 (priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
4936 /* At the lower border, possibly remove timeout */
4937 if (priv->scroll_step > 0)
4939 gtk_menu_stop_scrolling (menu);
4940 gtk_widget_queue_draw (GTK_WIDGET (menu));
4944 else if (priv->upper_arrow_visible || priv->lower_arrow_visible)
4948 priv->upper_arrow_visible = priv->lower_arrow_visible = FALSE;
4949 priv->upper_arrow_prelight = priv->lower_arrow_prelight = FALSE;
4951 gtk_menu_stop_scrolling (menu);
4952 gtk_widget_queue_draw (GTK_WIDGET (menu));
4955 else if (!priv->tearoff_active)
4957 gboolean last_visible;
4959 last_visible = priv->upper_arrow_visible;
4960 priv->upper_arrow_visible = offset > 0;
4962 /* upper_arrow_visible may have changed, so requery the border */
4963 get_arrows_border (menu, &arrow_border);
4964 view_height -= arrow_border.top;
4966 if ((last_visible != priv->upper_arrow_visible) &&
4967 !priv->upper_arrow_visible)
4969 priv->upper_arrow_prelight = FALSE;
4971 /* If we hide the upper arrow, possibly remove timeout */
4972 if (priv->scroll_step < 0)
4974 gtk_menu_stop_scrolling (menu);
4975 gtk_widget_queue_draw (GTK_WIDGET (menu));
4979 last_visible = priv->lower_arrow_visible;
4980 priv->lower_arrow_visible = offset < menu_height - view_height;
4982 /* lower_arrow_visible may have changed, so requery the border */
4983 get_arrows_border (menu, &arrow_border);
4984 view_height -= arrow_border.bottom;
4986 if ((last_visible != priv->lower_arrow_visible) &&
4987 !priv->lower_arrow_visible)
4989 priv->lower_arrow_prelight = FALSE;
4991 /* If we hide the lower arrow, possibly remove timeout */
4992 if (priv->scroll_step > 0)
4994 gtk_menu_stop_scrolling (menu);
4995 gtk_widget_queue_draw (GTK_WIDGET (menu));
4999 y += arrow_border.top;
5002 /* Scroll the menu: */
5003 if (gtk_widget_get_realized (widget))
5005 gdk_window_move (priv->bin_window, 0, -offset);
5006 gdk_window_move_resize (priv->view_window, x, y, view_width, view_height);
5009 priv->scroll_offset = offset;
5013 compute_child_offset (GtkMenu *menu,
5014 GtkWidget *menu_item,
5017 gboolean *is_last_child)
5019 GtkMenuPrivate *priv = menu->priv;
5020 gint item_top_attach;
5021 gint item_bottom_attach;
5022 gint child_offset = 0;
5025 get_effective_child_attach (menu_item, NULL, NULL,
5026 &item_top_attach, &item_bottom_attach);
5028 /* there is a possibility that we get called before _size_request,
5029 * so check the height table for safety.
5031 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
5034 /* when we have a row with only invisible children, its height will
5035 * be zero, so there's no need to check WIDGET_VISIBLE here
5037 for (i = 0; i < item_top_attach; i++)
5038 child_offset += priv->heights[i];
5041 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
5043 *offset = child_offset;
5045 *height = priv->heights[item_top_attach];
5051 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
5052 GtkWidget *menu_item)
5054 GtkMenu *menu = GTK_MENU (menu_shell);
5055 GtkMenuPrivate *priv = menu->priv;
5056 GtkWidget *widget = GTK_WIDGET (menu_shell);
5057 gint child_offset, child_height;
5061 gboolean last_child = 0;
5063 /* We need to check if the selected item fully visible.
5064 * If not we need to scroll the menu so that it becomes fully
5067 if (compute_child_offset (menu, menu_item,
5068 &child_offset, &child_height, &last_child))
5070 guint vertical_padding;
5071 gboolean double_arrows;
5074 y = priv->scroll_offset;
5075 height = gdk_window_get_height (gtk_widget_get_window (widget));
5077 gtk_widget_style_get (widget,
5078 "vertical-padding", &vertical_padding,
5081 double_arrows = get_double_arrows (menu);
5082 get_menu_padding (widget, &padding);
5084 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5085 padding.top + padding.bottom +
5086 2 * vertical_padding;
5087 if (child_offset < y)
5089 /* Ignore the enter event we might get if the pointer
5092 menu_shell->priv->ignore_enter = TRUE;
5093 gtk_menu_scroll_to (menu, child_offset);
5097 GtkBorder arrow_border;
5101 get_arrows_border (menu, &arrow_border);
5102 if (!priv->tearoff_active)
5103 arrow_height = arrow_border.top + arrow_border.bottom;
5105 if (child_offset + child_height > y + height - arrow_height)
5108 if ((!last_child && !priv->tearoff_active) || double_arrows)
5109 arrow_height += arrow_border.bottom;
5111 y = child_offset + child_height - height + arrow_height;
5112 if (((y > 0) && !priv->tearoff_active) || double_arrows)
5114 /* Need upper arrow */
5115 arrow_height += arrow_border.top;
5116 y = child_offset + child_height - height + arrow_height;
5118 /* Ignore the enter event we might get if the pointer
5121 menu_shell->priv->ignore_enter = TRUE;
5122 gtk_menu_scroll_to (menu, y);
5129 gtk_menu_select_item (GtkMenuShell *menu_shell,
5130 GtkWidget *menu_item)
5132 GtkMenu *menu = GTK_MENU (menu_shell);
5134 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5135 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5137 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5141 /* Reparent the menu, taking care of the refcounting
5143 * If unrealize is true we force a unrealize while reparenting the parent.
5144 * This can help eliminate flicker in some cases.
5146 * What happens is that when the menu is unrealized and then re-realized,
5147 * the allocations are as follows:
5149 * parent - 1x1 at (0,0)
5150 * child1 - 100x20 at (0,0)
5151 * child2 - 100x20 at (0,20)
5152 * child3 - 100x20 at (0,40)
5154 * That is, the parent is small but the children are full sized. Then,
5155 * when the queued_resize gets processed, the parent gets resized to
5158 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5159 * contains the following logic:
5161 * - if a move or resize operation on a window would change the clip
5162 * region on the children, then before the window is resized
5163 * the background for children is temporarily set to None, the
5164 * move/resize done, and the background for the children restored.
5166 * So, at the point where the parent is resized to final size, the
5167 * background for the children is temporarily None, and thus they
5168 * are not cleared to the background color and the previous background
5169 * (the image of the menu) is left in place.
5172 gtk_menu_reparent (GtkMenu *menu,
5173 GtkWidget *new_parent,
5176 GObject *object = G_OBJECT (menu);
5177 GtkWidget *widget = GTK_WIDGET (menu);
5178 gboolean was_floating = g_object_is_floating (object);
5180 g_object_ref_sink (object);
5184 g_object_ref (object);
5185 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5186 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5187 g_object_unref (object);
5190 gtk_widget_reparent (widget, new_parent);
5193 g_object_force_floating (object);
5195 g_object_unref (object);
5199 gtk_menu_show_all (GtkWidget *widget)
5201 /* Show children, but not self. */
5202 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5206 * gtk_menu_set_screen:
5208 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5209 * determined by the widget the menu is attached to
5211 * Sets the #GdkScreen on which the menu will be displayed.
5216 gtk_menu_set_screen (GtkMenu *menu,
5219 g_return_if_fail (GTK_IS_MENU (menu));
5220 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
5222 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5226 menu_change_screen (menu, screen);
5230 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5232 attach_widget_screen_changed (attach_widget, NULL, menu);
5239 * @child: a #GtkMenuItem
5240 * @left_attach: The column number to attach the left side of the item to
5241 * @right_attach: The column number to attach the right side of the item to
5242 * @top_attach: The row number to attach the top of the item to
5243 * @bottom_attach: The row number to attach the bottom of the item to
5245 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5246 * an item will occupy is specified by @left_attach, @right_attach,
5247 * @top_attach and @bottom_attach. These each represent the leftmost,
5248 * rightmost, uppermost and lower column and row numbers of the table.
5249 * (Columns and rows are indexed from zero).
5251 * Note that this function is not related to gtk_menu_detach().
5256 gtk_menu_attach (GtkMenu *menu,
5261 guint bottom_attach)
5263 GtkMenuShell *menu_shell;
5266 g_return_if_fail (GTK_IS_MENU (menu));
5267 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5268 parent = gtk_widget_get_parent (child);
5269 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5270 g_return_if_fail (left_attach < right_attach);
5271 g_return_if_fail (top_attach < bottom_attach);
5273 menu_shell = GTK_MENU_SHELL (menu);
5277 AttachInfo *ai = get_attach_info (child);
5279 ai->left_attach = left_attach;
5280 ai->right_attach = right_attach;
5281 ai->top_attach = top_attach;
5282 ai->bottom_attach = bottom_attach;
5284 menu_shell->priv->children = g_list_append (menu_shell->priv->children, child);
5286 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5288 menu_queue_resize (menu);
5292 gtk_container_child_set (GTK_CONTAINER (parent), child,
5293 "left-attach", left_attach,
5294 "right-attach", right_attach,
5295 "top-attach", top_attach,
5296 "bottom-attach", bottom_attach,
5302 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5306 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5307 "gtk-menu-popup-delay", &popup_delay,
5314 find_child_containing (GtkMenuShell *menu_shell,
5322 /* find a child which includes the area given by
5323 * left, right, top, bottom.
5325 for (list = menu_shell->priv->children; list; list = list->next)
5329 if (!_gtk_menu_item_is_selectable (list->data))
5332 get_effective_child_attach (list->data, &l, &r, &t, &b);
5334 if (l <= left && right <= r && t <= top && bottom <= b)
5335 return GTK_WIDGET (list->data);
5342 gtk_menu_move_current (GtkMenuShell *menu_shell,
5343 GtkMenuDirectionType direction)
5345 GtkMenu *menu = GTK_MENU (menu_shell);
5348 GtkWidget *match = NULL;
5350 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5354 case GTK_MENU_DIR_CHILD:
5355 direction = GTK_MENU_DIR_PARENT;
5357 case GTK_MENU_DIR_PARENT:
5358 direction = GTK_MENU_DIR_CHILD;
5364 /* use special table menu key bindings */
5365 if (menu_shell->priv->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5367 get_effective_child_attach (menu_shell->priv->active_menu_item, &l, &r, &t, &b);
5369 if (direction == GTK_MENU_DIR_NEXT)
5371 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5373 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5381 for (i = 0; i < t; i++)
5383 match = find_child_containing (menu_shell,
5384 l, l + 1, i, i + 1);
5390 else if (direction == GTK_MENU_DIR_PREV)
5392 for (i = t; i > 0; i--)
5394 match = find_child_containing (menu_shell,
5395 l, l + 1, i - 1, i);
5403 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5405 match = find_child_containing (menu_shell,
5406 l, l + 1, i - 1, i);
5412 else if (direction == GTK_MENU_DIR_PARENT)
5414 /* we go one left if possible */
5416 match = find_child_containing (menu_shell,
5417 l - 1, l, t, t + 1);
5421 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5424 || g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1)
5425 match = menu_shell->priv->active_menu_item;
5428 else if (direction == GTK_MENU_DIR_CHILD)
5430 /* we go one right if possible */
5431 if (r < gtk_menu_get_n_columns (menu))
5432 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5436 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5438 if (! GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu &&
5440 g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1))
5441 match = menu_shell->priv->active_menu_item;
5447 gtk_menu_shell_select_item (menu_shell, match);
5452 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5456 get_visible_size (GtkMenu *menu)
5458 GtkMenuPrivate *priv = menu->priv;
5459 GtkAllocation allocation;
5460 GtkWidget *widget = GTK_WIDGET (menu);
5461 GtkContainer *container = GTK_CONTAINER (menu);
5465 gtk_widget_get_allocation (widget, &allocation);
5466 get_menu_padding (widget, &padding);
5468 menu_height = (allocation.height -
5469 (2 * gtk_container_get_border_width (container)) -
5470 padding.top - padding.bottom);
5472 if (!priv->tearoff_active)
5474 GtkBorder arrow_border;
5476 get_arrows_border (menu, &arrow_border);
5477 menu_height -= arrow_border.top;
5478 menu_height -= arrow_border.bottom;
5484 /* Find the sensitive on-screen child containing @y, or if none,
5485 * the nearest selectable onscreen child. (%NULL if none)
5488 child_at (GtkMenu *menu,
5491 GtkMenuPrivate *priv = menu->priv;
5492 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5493 GtkWidget *child = NULL;
5494 gint child_offset = 0;
5497 gint lower, upper; /* Onscreen bounds */
5499 menu_height = get_visible_size (menu);
5500 lower = priv->scroll_offset;
5501 upper = priv->scroll_offset + menu_height;
5503 for (children = menu_shell->priv->children; children; children = children->next)
5505 if (gtk_widget_get_visible (children->data))
5507 GtkRequisition child_requisition;
5509 gtk_widget_get_preferred_size (children->data,
5510 &child_requisition, NULL);
5512 if (_gtk_menu_item_is_selectable (children->data) &&
5513 child_offset >= lower &&
5514 child_offset + child_requisition.height <= upper)
5516 child = children->data;
5518 if (child_offset + child_requisition.height > y &&
5519 !GTK_IS_TEAROFF_MENU_ITEM (child))
5523 child_offset += child_requisition.height;
5531 get_menu_height (GtkMenu *menu)
5533 GtkMenuPrivate *priv = menu->priv;
5534 GtkWidget *widget = GTK_WIDGET (menu);
5538 get_menu_padding (widget, &padding);
5540 height = priv->requested_height;
5541 height -= (gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2) +
5542 padding.top + padding.bottom;
5544 if (!priv->tearoff_active)
5546 GtkBorder arrow_border;
5548 get_arrows_border (menu, &arrow_border);
5549 height -= arrow_border.top;
5550 height -= arrow_border.bottom;
5557 gtk_menu_real_move_scroll (GtkMenu *menu,
5560 GtkMenuPrivate *priv = menu->priv;
5561 gint page_size = get_visible_size (menu);
5562 gint end_position = get_menu_height (menu);
5563 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5567 case GTK_SCROLL_PAGE_UP:
5568 case GTK_SCROLL_PAGE_DOWN:
5572 gint child_offset = 0;
5573 gboolean old_upper_arrow_visible;
5576 if (type == GTK_SCROLL_PAGE_UP)
5581 if (menu_shell->priv->active_menu_item)
5585 compute_child_offset (menu, menu_shell->priv->active_menu_item,
5586 &child_offset, &child_height, NULL);
5587 child_offset += child_height / 2;
5590 menu_shell->priv->ignore_enter = TRUE;
5591 old_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5592 old_offset = priv->scroll_offset;
5594 new_offset = priv->scroll_offset + step;
5595 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5597 gtk_menu_scroll_to (menu, new_offset);
5599 if (menu_shell->priv->active_menu_item)
5601 GtkWidget *new_child;
5602 gboolean new_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5603 GtkBorder arrow_border;
5605 get_arrows_border (menu, &arrow_border);
5607 if (priv->scroll_offset != old_offset)
5608 step = priv->scroll_offset - old_offset;
5610 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5612 new_child = child_at (menu, child_offset + step);
5614 gtk_menu_shell_select_item (menu_shell, new_child);
5618 case GTK_SCROLL_START:
5619 /* Ignore the enter event we might get if the pointer is on the menu */
5620 menu_shell->priv->ignore_enter = TRUE;
5621 gtk_menu_shell_select_first (menu_shell, TRUE);
5623 case GTK_SCROLL_END:
5624 /* Ignore the enter event we might get if the pointer is on the menu */
5625 menu_shell->priv->ignore_enter = TRUE;
5626 _gtk_menu_shell_select_last (menu_shell, TRUE);
5634 * gtk_menu_set_monitor:
5636 * @monitor_num: the number of the monitor on which the menu should
5639 * Informs GTK+ on which monitor a menu should be popped up.
5640 * See gdk_screen_get_monitor_geometry().
5642 * This function should be called from a #GtkMenuPositionFunc
5643 * if the menu should not appear on the same monitor as the pointer.
5644 * This information can't be reliably inferred from the coordinates
5645 * returned by a #GtkMenuPositionFunc, since, for very long menus,
5646 * these coordinates may extend beyond the monitor boundaries or even
5647 * the screen boundaries.
5652 gtk_menu_set_monitor (GtkMenu *menu,
5655 GtkMenuPrivate *priv = menu->priv;
5657 g_return_if_fail (GTK_IS_MENU (menu));
5659 priv->monitor_num = monitor_num;
5663 * gtk_menu_get_monitor:
5666 * Retrieves the number of the monitor on which to show the menu.
5668 * Returns: the number of the monitor on which the menu should
5669 * be popped up or -1, if no monitor has been set
5674 gtk_menu_get_monitor (GtkMenu *menu)
5676 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5678 return menu->priv->monitor_num;
5682 * gtk_menu_get_for_attach_widget:
5683 * @widget: a #GtkWidget
5685 * Returns a list of the menus which are attached to this widget.
5686 * This list is owned by GTK+ and must not be modified.
5688 * Return value: (element-type GtkWidget) (transfer none): the list
5689 * of menus attached to his widget.
5694 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5698 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5700 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5706 gtk_menu_grab_notify (GtkWidget *widget,
5707 gboolean was_grabbed)
5710 GtkWidget *toplevel;
5711 GtkWindowGroup *group;
5715 menu = GTK_MENU (widget);
5716 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5719 !gtk_widget_device_is_shadowed (widget, pointer))
5722 toplevel = gtk_widget_get_toplevel (widget);
5724 if (!GTK_IS_WINDOW (toplevel))
5727 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5728 grab = gtk_window_group_get_current_device_grab (group, pointer);
5730 if (GTK_MENU_SHELL (widget)->priv->active && !GTK_IS_MENU_SHELL (grab))
5731 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5733 menu->priv->drag_scroll_started = FALSE;
5737 * gtk_menu_set_reserve_toggle_size:
5739 * @reserve_toggle_size: whether to reserve size for toggles
5741 * Sets whether the menu should reserve space for drawing toggles
5742 * or icons, regardless of their actual presence.
5747 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5748 gboolean reserve_toggle_size)
5750 GtkMenuPrivate *priv = menu->priv;
5751 gboolean no_toggle_size;
5753 g_return_if_fail (GTK_IS_MENU (menu));
5755 no_toggle_size = !reserve_toggle_size;
5757 if (priv->no_toggle_size != no_toggle_size)
5759 priv->no_toggle_size = no_toggle_size;
5761 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5766 * gtk_menu_get_reserve_toggle_size:
5769 * Returns whether the menu reserves space for toggles and
5770 * icons, regardless of their actual presence.
5772 * Returns: Whether the menu reserves toggle space
5777 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5779 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
5781 return !menu->priv->no_toggle_size;