1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 * @Short_description: A menu widget
32 * A #GtkMenu is a #GtkMenuShell that implements a drop down menu
33 * consisting of a list of #GtkMenuItem objects which can be navigated
34 * and activated by the user to perform application functions.
36 * A #GtkMenu is most commonly dropped down by activating a
37 * #GtkMenuItem in a #GtkMenuBar or popped up by activating a
38 * #GtkMenuItem in another #GtkMenu.
40 * A #GtkMenu can also be popped up by activating a #GtkOptionMenu.
41 * Other composite widgets such as the #GtkNotebook can pop up a
44 * Applications can display a #GtkMenu as a popup menu by calling the
45 * gtk_menu_popup() function. The example below shows how an application
46 * can pop up a menu when the 3rd mouse button is pressed.
49 * <title>Connecting the popup signal handler.</title>
51 * /<!---->* connect our handler which will popup the menu *<!---->/
52 * g_signal_connect_swapped (window, "button_press_event",
53 * G_CALLBACK (my_popup_handler), menu);
58 * <title>Signal handler which displays a popup menu.</title>
61 * my_popup_handler (GtkWidget *widget, GdkEvent *event)
64 * GdkEventButton *event_button;
66 * g_return_val_if_fail (widget != NULL, FALSE);
67 * g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
68 * g_return_val_if_fail (event != NULL, FALSE);
70 * /<!---->* The "widget" is the menu that was supplied when
71 * * g_signal_connect_swapped() was called.
73 * menu = GTK_MENU (widget);
75 * if (event->type == GDK_BUTTON_PRESS)
77 * event_button = (GdkEventButton *) event;
78 * if (event_button->button == 3)
80 * gtk_menu_popup (menu, NULL, NULL, NULL, NULL,
81 * event_button->button, event_button->time);
96 #include <gobject/gvaluecollector.h>
98 #include "gtkaccellabel.h"
99 #include "gtkaccelmap.h"
100 #include "gtkbindings.h"
101 #include "gtkcheckmenuitem.h"
103 #include "gtkmarshalers.h"
104 #include "gtkmenuprivate.h"
105 #include "gtkmenuitemprivate.h"
106 #include "gtkmenushellprivate.h"
107 #include "gtktearoffmenuitem.h"
108 #include "gtkwindow.h"
110 #include "gtkscrollbar.h"
111 #include "gtksettings.h"
112 #include "gtkprivate.h"
113 #include "gtkwidgetprivate.h"
115 #include "gtktypebuiltins.h"
117 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
118 * extends below the submenu
121 #define MENU_SCROLL_STEP1 8
122 #define MENU_SCROLL_STEP2 15
123 #define MENU_SCROLL_FAST_ZONE 8
124 #define MENU_SCROLL_TIMEOUT1 50
125 #define MENU_SCROLL_TIMEOUT2 20
127 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
128 #define ATTACHED_MENUS "gtk-attached-menus"
130 typedef struct _GtkMenuAttachData GtkMenuAttachData;
131 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
133 struct _GtkMenuAttachData
135 GtkWidget *attach_widget;
136 GtkMenuDetachFunc detacher;
139 struct _OldGtkMenuPrivate
143 gboolean initially_pushed_in;
145 GDestroyNotify position_func_data_destroy;
147 /* info used for the table */
150 gint requested_height;
154 /* Cached layout information */
163 GtkStateFlags lower_arrow_state;
164 GtkStateFlags upper_arrow_state;
166 /* navigation region */
169 int navigation_width;
170 int navigation_height;
172 guint have_layout : 1;
173 guint seen_item_enter : 1;
174 guint have_position : 1;
175 guint ignore_button_release : 1;
176 guint no_toggle_size : 1;
179 struct _GtkMenuPopdownData
191 gint effective_left_attach;
192 gint effective_right_attach;
193 gint effective_top_attach;
194 gint effective_bottom_attach;
211 PROP_RESERVE_TOGGLE_SIZE
216 CHILD_PROP_LEFT_ATTACH,
217 CHILD_PROP_RIGHT_ATTACH,
218 CHILD_PROP_TOP_ATTACH,
219 CHILD_PROP_BOTTOM_ATTACH
222 static void gtk_menu_set_property (GObject *object,
226 static void gtk_menu_get_property (GObject *object,
230 static void gtk_menu_set_child_property(GtkContainer *container,
235 static void gtk_menu_get_child_property(GtkContainer *container,
240 static void gtk_menu_destroy (GtkWidget *widget);
241 static void gtk_menu_realize (GtkWidget *widget);
242 static void gtk_menu_unrealize (GtkWidget *widget);
243 static void gtk_menu_size_allocate (GtkWidget *widget,
244 GtkAllocation *allocation);
245 static void gtk_menu_show (GtkWidget *widget);
246 static gboolean gtk_menu_draw (GtkWidget *widget,
248 static gboolean gtk_menu_key_press (GtkWidget *widget,
250 static gboolean gtk_menu_scroll (GtkWidget *widget,
251 GdkEventScroll *event);
252 static gboolean gtk_menu_button_press (GtkWidget *widget,
253 GdkEventButton *event);
254 static gboolean gtk_menu_button_release (GtkWidget *widget,
255 GdkEventButton *event);
256 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
257 GdkEventMotion *event);
258 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
259 GdkEventCrossing *event);
260 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
261 GdkEventCrossing *event);
262 static void gtk_menu_scroll_to (GtkMenu *menu,
264 static void gtk_menu_grab_notify (GtkWidget *widget,
265 gboolean was_grabbed);
267 static void gtk_menu_stop_scrolling (GtkMenu *menu);
268 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
269 static gboolean gtk_menu_scroll_timeout (gpointer data);
270 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
271 static void gtk_menu_start_scrolling (GtkMenu *menu);
273 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
274 GtkWidget *menu_item);
275 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
276 GtkWidget *menu_item);
277 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
280 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
282 static void gtk_menu_handle_scrolling (GtkMenu *menu,
287 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
289 static void gtk_menu_style_updated (GtkWidget *widget);
290 static gboolean gtk_menu_focus (GtkWidget *widget,
291 GtkDirectionType direction);
292 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
293 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
294 GtkMenuDirectionType direction);
295 static void gtk_menu_real_move_scroll (GtkMenu *menu,
298 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
299 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
300 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
303 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
304 GtkMenuItem *menu_item,
305 GdkEventCrossing *event);
307 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
308 static void gtk_menu_show_all (GtkWidget *widget);
309 static void gtk_menu_position (GtkMenu *menu,
310 gboolean set_scroll_offset);
311 static void gtk_menu_reparent (GtkMenu *menu,
312 GtkWidget *new_parent,
314 static void gtk_menu_remove (GtkContainer *menu,
317 static void gtk_menu_update_title (GtkMenu *menu);
319 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
320 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
322 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
324 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
325 gboolean group_changed);
327 static void gtk_menu_get_preferred_width (GtkWidget *widget,
330 static void gtk_menu_get_preferred_height (GtkWidget *widget,
333 static void gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
339 static const gchar attach_data_key[] = "gtk-menu-attach-data";
341 static guint menu_signals[LAST_SIGNAL] = { 0 };
343 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
346 menu_queue_resize (GtkMenu *menu)
348 GtkMenuPrivate *priv = menu->priv;
350 priv->have_layout = FALSE;
351 gtk_widget_queue_resize (GTK_WIDGET (menu));
355 attach_info_free (AttachInfo *info)
357 g_slice_free (AttachInfo, info);
361 get_attach_info (GtkWidget *child)
363 GObject *object = G_OBJECT (child);
364 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
368 ai = g_slice_new0 (AttachInfo);
369 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
370 (GDestroyNotify) attach_info_free);
377 is_grid_attached (AttachInfo *ai)
379 return (ai->left_attach >= 0 &&
380 ai->right_attach >= 0 &&
381 ai->top_attach >= 0 &&
382 ai->bottom_attach >= 0);
386 menu_ensure_layout (GtkMenu *menu)
388 GtkMenuPrivate *priv = menu->priv;
390 if (!priv->have_layout)
392 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
396 gint max_right_attach;
397 gint max_bottom_attach;
399 /* Find extents of gridded portion
401 max_right_attach = 1;
402 max_bottom_attach = 0;
404 for (l = menu_shell->priv->children; l; l = l->next)
406 GtkWidget *child = l->data;
407 AttachInfo *ai = get_attach_info (child);
409 if (is_grid_attached (ai))
411 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
412 max_right_attach = MAX (max_right_attach, ai->right_attach);
416 /* Find empty rows */
417 row_occupied = g_malloc0 (max_bottom_attach);
419 for (l = menu_shell->priv->children; l; l = l->next)
421 GtkWidget *child = l->data;
422 AttachInfo *ai = get_attach_info (child);
424 if (is_grid_attached (ai))
428 for (i = ai->top_attach; i < ai->bottom_attach; i++)
429 row_occupied[i] = TRUE;
433 /* Lay non-grid-items out in those rows
436 for (l = menu_shell->priv->children; l; l = l->next)
438 GtkWidget *child = l->data;
439 AttachInfo *ai = get_attach_info (child);
441 if (!is_grid_attached (ai))
443 while (current_row < max_bottom_attach && row_occupied[current_row])
446 ai->effective_left_attach = 0;
447 ai->effective_right_attach = max_right_attach;
448 ai->effective_top_attach = current_row;
449 ai->effective_bottom_attach = current_row + 1;
455 ai->effective_left_attach = ai->left_attach;
456 ai->effective_right_attach = ai->right_attach;
457 ai->effective_top_attach = ai->top_attach;
458 ai->effective_bottom_attach = ai->bottom_attach;
462 g_free (row_occupied);
464 priv->n_rows = MAX (current_row, max_bottom_attach);
465 priv->n_columns = max_right_attach;
466 priv->have_layout = TRUE;
472 gtk_menu_get_n_columns (GtkMenu *menu)
474 GtkMenuPrivate *priv = menu->priv;
476 menu_ensure_layout (menu);
478 return priv->n_columns;
482 gtk_menu_get_n_rows (GtkMenu *menu)
484 GtkMenuPrivate *priv = menu->priv;
486 menu_ensure_layout (menu);
492 get_effective_child_attach (GtkWidget *child,
498 GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
501 menu_ensure_layout (menu);
503 ai = get_attach_info (child);
506 *l = ai->effective_left_attach;
508 *r = ai->effective_right_attach;
510 *t = ai->effective_top_attach;
512 *b = ai->effective_bottom_attach;
517 gtk_menu_class_init (GtkMenuClass *class)
519 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
520 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
521 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
522 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
523 GtkBindingSet *binding_set;
525 gobject_class->set_property = gtk_menu_set_property;
526 gobject_class->get_property = gtk_menu_get_property;
528 widget_class->destroy = gtk_menu_destroy;
529 widget_class->realize = gtk_menu_realize;
530 widget_class->unrealize = gtk_menu_unrealize;
531 widget_class->size_allocate = gtk_menu_size_allocate;
532 widget_class->show = gtk_menu_show;
533 widget_class->draw = gtk_menu_draw;
534 widget_class->scroll_event = gtk_menu_scroll;
535 widget_class->key_press_event = gtk_menu_key_press;
536 widget_class->button_press_event = gtk_menu_button_press;
537 widget_class->button_release_event = gtk_menu_button_release;
538 widget_class->motion_notify_event = gtk_menu_motion_notify;
539 widget_class->show_all = gtk_menu_show_all;
540 widget_class->enter_notify_event = gtk_menu_enter_notify;
541 widget_class->leave_notify_event = gtk_menu_leave_notify;
542 widget_class->style_updated = gtk_menu_style_updated;
543 widget_class->focus = gtk_menu_focus;
544 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
545 widget_class->grab_notify = gtk_menu_grab_notify;
546 widget_class->get_preferred_width = gtk_menu_get_preferred_width;
547 widget_class->get_preferred_height = gtk_menu_get_preferred_height;
548 widget_class->get_preferred_height_for_width = gtk_menu_get_preferred_height_for_width;
550 container_class->remove = gtk_menu_remove;
551 container_class->get_child_property = gtk_menu_get_child_property;
552 container_class->set_child_property = gtk_menu_set_child_property;
554 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
555 menu_shell_class->deactivate = gtk_menu_deactivate;
556 menu_shell_class->select_item = gtk_menu_select_item;
557 menu_shell_class->insert = gtk_menu_real_insert;
558 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
559 menu_shell_class->move_current = gtk_menu_move_current;
562 * GtkMenu::move-scroll:
564 * @scroll_type: a #GtkScrollType
566 menu_signals[MOVE_SCROLL] =
567 g_signal_new_class_handler (I_("move-scroll"),
568 G_OBJECT_CLASS_TYPE (gobject_class),
569 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
570 G_CALLBACK (gtk_menu_real_move_scroll),
572 _gtk_marshal_VOID__ENUM,
574 GTK_TYPE_SCROLL_TYPE);
579 * The index of the currently selected menu item, or -1 if no
580 * menu item is selected.
584 g_object_class_install_property (gobject_class,
586 g_param_spec_int ("active",
588 P_("The currently selected menu item"),
590 GTK_PARAM_READWRITE));
593 * GtkMenu:accel-group:
595 * The accel group holding accelerators for the menu.
599 g_object_class_install_property (gobject_class,
601 g_param_spec_object ("accel-group",
603 P_("The accel group holding accelerators for the menu"),
604 GTK_TYPE_ACCEL_GROUP,
605 GTK_PARAM_READWRITE));
608 * GtkMenu:accel-path:
610 * An accel path used to conveniently construct accel paths of child items.
614 g_object_class_install_property (gobject_class,
616 g_param_spec_string ("accel-path",
618 P_("An accel path used to conveniently construct accel paths of child items"),
620 GTK_PARAM_READWRITE));
623 * GtkMenu:attach-widget:
625 * The widget the menu is attached to. Setting this property attaches
626 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
627 * use gtk_menu_attach_to_widget() directly.
631 g_object_class_install_property (gobject_class,
633 g_param_spec_object ("attach-widget",
635 P_("The widget the menu is attached to"),
637 GTK_PARAM_READWRITE));
639 g_object_class_install_property (gobject_class,
641 g_param_spec_string ("tearoff-title",
643 P_("A title that may be displayed by the window manager when this menu is torn-off"),
645 GTK_PARAM_READWRITE));
648 * GtkMenu:tearoff-state:
650 * A boolean that indicates whether the menu is torn-off.
654 g_object_class_install_property (gobject_class,
656 g_param_spec_boolean ("tearoff-state",
658 P_("A boolean that indicates whether the menu is torn-off"),
660 GTK_PARAM_READWRITE));
665 * The monitor the menu will be popped up on.
669 g_object_class_install_property (gobject_class,
671 g_param_spec_int ("monitor",
673 P_("The monitor the menu will be popped up on"),
675 GTK_PARAM_READWRITE));
677 gtk_widget_class_install_style_property (widget_class,
678 g_param_spec_int ("vertical-padding",
679 P_("Vertical Padding"),
680 P_("Extra space at the top and bottom of the menu"),
684 GTK_PARAM_READABLE));
687 * GtkMenu:reserve-toggle-size:
689 * A boolean that indicates whether the menu reserves space for
690 * toggles and icons, regardless of their actual presence.
692 * This property should only be changed from its default value
693 * for special-purposes such as tabular menus. Regular menus that
694 * are connected to a menu bar or context menus should reserve
695 * toggle space for consistency.
699 g_object_class_install_property (gobject_class,
700 PROP_RESERVE_TOGGLE_SIZE,
701 g_param_spec_boolean ("reserve-toggle-size",
702 P_("Reserve Toggle Size"),
703 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
705 GTK_PARAM_READWRITE));
707 gtk_widget_class_install_style_property (widget_class,
708 g_param_spec_int ("horizontal-padding",
709 P_("Horizontal Padding"),
710 P_("Extra space at the left and right edges of the menu"),
714 GTK_PARAM_READABLE));
716 gtk_widget_class_install_style_property (widget_class,
717 g_param_spec_int ("vertical-offset",
718 P_("Vertical Offset"),
719 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
723 GTK_PARAM_READABLE));
725 gtk_widget_class_install_style_property (widget_class,
726 g_param_spec_int ("horizontal-offset",
727 P_("Horizontal Offset"),
728 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
732 GTK_PARAM_READABLE));
734 gtk_widget_class_install_style_property (widget_class,
735 g_param_spec_boolean ("double-arrows",
737 P_("When scrolling, always show both arrows."),
739 GTK_PARAM_READABLE));
742 * GtkMenu:arrow-placement:
744 * Indicates where scroll arrows should be placed.
748 gtk_widget_class_install_style_property (widget_class,
749 g_param_spec_enum ("arrow-placement",
750 P_("Arrow Placement"),
751 P_("Indicates where scroll arrows should be placed"),
752 GTK_TYPE_ARROW_PLACEMENT,
754 GTK_PARAM_READABLE));
756 gtk_container_class_install_child_property (container_class,
757 CHILD_PROP_LEFT_ATTACH,
758 g_param_spec_int ("left-attach",
760 P_("The column number to attach the left side of the child to"),
762 GTK_PARAM_READWRITE));
764 gtk_container_class_install_child_property (container_class,
765 CHILD_PROP_RIGHT_ATTACH,
766 g_param_spec_int ("right-attach",
768 P_("The column number to attach the right side of the child to"),
770 GTK_PARAM_READWRITE));
772 gtk_container_class_install_child_property (container_class,
773 CHILD_PROP_TOP_ATTACH,
774 g_param_spec_int ("top-attach",
776 P_("The row number to attach the top of the child to"),
778 GTK_PARAM_READWRITE));
780 gtk_container_class_install_child_property (container_class,
781 CHILD_PROP_BOTTOM_ATTACH,
782 g_param_spec_int ("bottom-attach",
784 P_("The row number to attach the bottom of the child to"),
786 GTK_PARAM_READWRITE));
789 * GtkMenu:arrow-scaling
791 * Arbitrary constant to scale down the size of the scroll arrow.
795 gtk_widget_class_install_style_property (widget_class,
796 g_param_spec_float ("arrow-scaling",
798 P_("Arbitrary constant to scale down the size of the scroll arrow"),
800 GTK_PARAM_READABLE));
802 binding_set = gtk_binding_set_by_class (class);
803 gtk_binding_entry_add_signal (binding_set,
805 I_("move-current"), 1,
806 GTK_TYPE_MENU_DIRECTION_TYPE,
808 gtk_binding_entry_add_signal (binding_set,
811 GTK_TYPE_MENU_DIRECTION_TYPE,
813 gtk_binding_entry_add_signal (binding_set,
816 GTK_TYPE_MENU_DIRECTION_TYPE,
818 gtk_binding_entry_add_signal (binding_set,
821 GTK_TYPE_MENU_DIRECTION_TYPE,
823 gtk_binding_entry_add_signal (binding_set,
826 GTK_TYPE_MENU_DIRECTION_TYPE,
827 GTK_MENU_DIR_PARENT);
828 gtk_binding_entry_add_signal (binding_set,
831 GTK_TYPE_MENU_DIRECTION_TYPE,
832 GTK_MENU_DIR_PARENT);
833 gtk_binding_entry_add_signal (binding_set,
836 GTK_TYPE_MENU_DIRECTION_TYPE,
838 gtk_binding_entry_add_signal (binding_set,
841 GTK_TYPE_MENU_DIRECTION_TYPE,
843 gtk_binding_entry_add_signal (binding_set,
846 GTK_TYPE_SCROLL_TYPE,
848 gtk_binding_entry_add_signal (binding_set,
851 GTK_TYPE_SCROLL_TYPE,
853 gtk_binding_entry_add_signal (binding_set,
856 GTK_TYPE_SCROLL_TYPE,
858 gtk_binding_entry_add_signal (binding_set,
861 GTK_TYPE_SCROLL_TYPE,
863 gtk_binding_entry_add_signal (binding_set,
866 GTK_TYPE_SCROLL_TYPE,
868 gtk_binding_entry_add_signal (binding_set,
869 GDK_KEY_KP_Page_Up, 0,
871 GTK_TYPE_SCROLL_TYPE,
873 gtk_binding_entry_add_signal (binding_set,
874 GDK_KEY_Page_Down, 0,
876 GTK_TYPE_SCROLL_TYPE,
877 GTK_SCROLL_PAGE_DOWN);
878 gtk_binding_entry_add_signal (binding_set,
879 GDK_KEY_KP_Page_Down, 0,
881 GTK_TYPE_SCROLL_TYPE,
882 GTK_SCROLL_PAGE_DOWN);
884 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
889 gtk_menu_set_property (GObject *object,
894 GtkMenu *menu = GTK_MENU (object);
899 gtk_menu_set_active (menu, g_value_get_int (value));
901 case PROP_ACCEL_GROUP:
902 gtk_menu_set_accel_group (menu, g_value_get_object (value));
904 case PROP_ACCEL_PATH:
905 gtk_menu_set_accel_path (menu, g_value_get_string (value));
907 case PROP_ATTACH_WIDGET:
911 widget = gtk_menu_get_attach_widget (menu);
913 gtk_menu_detach (menu);
915 widget = (GtkWidget*) g_value_get_object (value);
917 gtk_menu_attach_to_widget (menu, widget, NULL);
920 case PROP_TEAROFF_STATE:
921 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
923 case PROP_TEAROFF_TITLE:
924 gtk_menu_set_title (menu, g_value_get_string (value));
927 gtk_menu_set_monitor (menu, g_value_get_int (value));
929 case PROP_RESERVE_TOGGLE_SIZE:
930 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
933 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
939 gtk_menu_get_property (GObject *object,
944 GtkMenu *menu = GTK_MENU (object);
949 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->priv->children, gtk_menu_get_active (menu)));
951 case PROP_ACCEL_GROUP:
952 g_value_set_object (value, gtk_menu_get_accel_group (menu));
954 case PROP_ACCEL_PATH:
955 g_value_set_string (value, gtk_menu_get_accel_path (menu));
957 case PROP_ATTACH_WIDGET:
958 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
960 case PROP_TEAROFF_STATE:
961 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
963 case PROP_TEAROFF_TITLE:
964 g_value_set_string (value, gtk_menu_get_title (menu));
967 g_value_set_int (value, gtk_menu_get_monitor (menu));
969 case PROP_RESERVE_TOGGLE_SIZE:
970 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
973 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
979 gtk_menu_set_child_property (GtkContainer *container,
985 GtkMenu *menu = GTK_MENU (container);
986 AttachInfo *ai = get_attach_info (child);
990 case CHILD_PROP_LEFT_ATTACH:
991 ai->left_attach = g_value_get_int (value);
993 case CHILD_PROP_RIGHT_ATTACH:
994 ai->right_attach = g_value_get_int (value);
996 case CHILD_PROP_TOP_ATTACH:
997 ai->top_attach = g_value_get_int (value);
999 case CHILD_PROP_BOTTOM_ATTACH:
1000 ai->bottom_attach = g_value_get_int (value);
1004 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1008 menu_queue_resize (menu);
1012 gtk_menu_get_child_property (GtkContainer *container,
1018 AttachInfo *ai = get_attach_info (child);
1020 switch (property_id)
1022 case CHILD_PROP_LEFT_ATTACH:
1023 g_value_set_int (value, ai->left_attach);
1025 case CHILD_PROP_RIGHT_ATTACH:
1026 g_value_set_int (value, ai->right_attach);
1028 case CHILD_PROP_TOP_ATTACH:
1029 g_value_set_int (value, ai->top_attach);
1031 case CHILD_PROP_BOTTOM_ATTACH:
1032 g_value_set_int (value, ai->bottom_attach);
1036 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1042 gtk_menu_window_event (GtkWidget *window,
1046 gboolean handled = FALSE;
1048 g_object_ref (window);
1049 g_object_ref (menu);
1051 switch (event->type)
1054 case GDK_KEY_RELEASE:
1055 handled = gtk_widget_event (menu, event);
1061 g_object_unref (window);
1062 g_object_unref (menu);
1068 gtk_menu_init (GtkMenu *menu)
1070 GtkMenuPrivate *priv;
1071 GtkStyleContext *context;
1073 priv = G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
1077 priv->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1078 "type", GTK_WINDOW_POPUP,
1081 "signal::event", gtk_menu_window_event, menu,
1082 "signal::destroy", gtk_widget_destroyed, &priv->toplevel,
1084 gtk_window_set_resizable (GTK_WINDOW (priv->toplevel), FALSE);
1085 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->toplevel), 0);
1087 /* Refloat the menu, so that reference counting for the menu isn't
1088 * affected by it being a child of the toplevel
1090 g_object_force_floating (G_OBJECT (menu));
1091 priv->needs_destruction_ref = TRUE;
1093 priv->monitor_num = -1;
1095 context = gtk_widget_get_style_context (GTK_WIDGET (menu));
1096 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENU);
1100 gtk_menu_destroy (GtkWidget *widget)
1102 GtkMenu *menu = GTK_MENU (widget);
1103 GtkMenuPrivate *priv = menu->priv;
1104 GtkMenuAttachData *data;
1106 gtk_menu_remove_scroll_timeout (menu);
1108 data = g_object_get_data (G_OBJECT (widget), attach_data_key);
1110 gtk_menu_detach (menu);
1112 gtk_menu_stop_navigating_submenu (menu);
1114 if (priv->old_active_menu_item)
1115 g_clear_object (&priv->old_active_menu_item);
1117 /* Add back the reference count for being a child */
1118 if (priv->needs_destruction_ref)
1120 priv->needs_destruction_ref = FALSE;
1121 g_object_ref (widget);
1124 if (priv->accel_group)
1125 g_clear_object (&priv->accel_group);
1128 gtk_widget_destroy (priv->toplevel);
1130 if (priv->tearoff_window)
1131 gtk_widget_destroy (priv->tearoff_window);
1135 g_free (priv->heights);
1136 priv->heights = NULL;
1141 g_free (priv->title);
1145 if (priv->position_func_data_destroy)
1147 priv->position_func_data_destroy (priv->position_func_data);
1148 priv->position_func_data = NULL;
1149 priv->position_func_data_destroy = NULL;
1152 GTK_WIDGET_CLASS (gtk_menu_parent_class)->destroy (widget);
1156 menu_change_screen (GtkMenu *menu,
1157 GdkScreen *new_screen)
1159 GtkMenuPrivate *priv = menu->priv;
1161 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1163 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1169 gtk_window_set_screen (GTK_WINDOW (priv->tearoff_window), new_screen);
1170 gtk_menu_position (menu, TRUE);
1173 gtk_window_set_screen (GTK_WINDOW (priv->toplevel), new_screen);
1174 priv->monitor_num = -1;
1178 attach_widget_screen_changed (GtkWidget *attach_widget,
1179 GdkScreen *previous_screen,
1182 if (gtk_widget_has_screen (attach_widget) &&
1183 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1184 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1188 * gtk_menu_attach_to_widget:
1190 * @attach_widget: the #GtkWidget that the menu will be attached to
1191 * @detacher: (scope async)(allow-none): the user supplied callback function
1192 * that will be called when the menu calls gtk_menu_detach()
1194 * Attaches the menu to the widget and provides a callback function
1195 * that will be invoked when the menu calls gtk_menu_detach() during
1199 gtk_menu_attach_to_widget (GtkMenu *menu,
1200 GtkWidget *attach_widget,
1201 GtkMenuDetachFunc detacher)
1203 GtkMenuAttachData *data;
1206 g_return_if_fail (GTK_IS_MENU (menu));
1207 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1209 /* keep this function in sync with gtk_widget_set_parent() */
1210 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1213 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1214 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1218 g_object_ref_sink (menu);
1220 data = g_slice_new (GtkMenuAttachData);
1221 data->attach_widget = attach_widget;
1223 g_signal_connect (attach_widget, "screen-changed",
1224 G_CALLBACK (attach_widget_screen_changed), menu);
1225 attach_widget_screen_changed (attach_widget, NULL, menu);
1227 data->detacher = detacher;
1228 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1229 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1230 if (!g_list_find (list, menu))
1231 list = g_list_prepend (list, menu);
1233 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1234 (GDestroyNotify) g_list_free);
1236 if (gtk_widget_get_state_flags (GTK_WIDGET (menu)) != 0)
1237 gtk_widget_set_state_flags (GTK_WIDGET (menu), 0, TRUE);
1239 /* we don't need to set the style here, since
1240 * we are a toplevel widget.
1243 /* Fallback title for menu comes from attach widget */
1244 gtk_menu_update_title (menu);
1246 g_object_notify (G_OBJECT (menu), "attach-widget");
1250 * gtk_menu_get_attach_widget:
1253 * Returns the #GtkWidget that the menu is attached to.
1255 * Returns: (transfer none): the #GtkWidget that the menu is attached to
1258 gtk_menu_get_attach_widget (GtkMenu *menu)
1260 GtkMenuAttachData *data;
1262 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1264 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1266 return data->attach_widget;
1274 * Detaches the menu from the widget to which it had been attached.
1275 * This function will call the callback function, @detacher, provided
1276 * when the gtk_menu_attach_to_widget() function was called.
1279 gtk_menu_detach (GtkMenu *menu)
1281 GtkMenuAttachData *data;
1284 g_return_if_fail (GTK_IS_MENU (menu));
1286 /* keep this function in sync with gtk_widget_unparent() */
1287 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1290 g_warning ("gtk_menu_detach(): menu is not attached");
1293 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1295 g_signal_handlers_disconnect_by_func (data->attach_widget,
1296 (gpointer) attach_widget_screen_changed,
1300 data->detacher (data->attach_widget, menu);
1301 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1302 list = g_list_remove (list, menu);
1304 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1305 (GDestroyNotify) g_list_free);
1307 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1309 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1310 gtk_widget_unrealize (GTK_WIDGET (menu));
1312 g_slice_free (GtkMenuAttachData, data);
1314 /* Fallback title for menu comes from attach widget */
1315 gtk_menu_update_title (menu);
1317 g_object_unref (menu);
1321 gtk_menu_remove (GtkContainer *container,
1324 GtkMenu *menu = GTK_MENU (container);
1325 GtkMenuPrivate *priv = menu->priv;
1327 /* Clear out old_active_menu_item if it matches the item we are removing */
1328 if (priv->old_active_menu_item == widget)
1329 g_clear_object (&priv->old_active_menu_item);
1331 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1333 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1335 menu_queue_resize (menu);
1341 * Creates a new #GtkMenu
1343 * Returns: a new #GtkMenu
1348 return g_object_new (GTK_TYPE_MENU, NULL);
1352 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1356 GtkMenu *menu = GTK_MENU (menu_shell);
1357 GtkMenuPrivate *priv = menu->priv;
1358 AttachInfo *ai = get_attach_info (child);
1360 ai->left_attach = -1;
1361 ai->right_attach = -1;
1362 ai->top_attach = -1;
1363 ai->bottom_attach = -1;
1365 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1366 gtk_widget_set_parent_window (child, priv->bin_window);
1368 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1370 menu_queue_resize (menu);
1374 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1376 GtkMenuPrivate *priv = menu->priv;
1382 cairo_surface_t *surface;
1383 cairo_pattern_t *pattern;
1386 priv->tearoff_active = FALSE;
1387 priv->saved_scroll_offset = priv->scroll_offset;
1389 window = gtk_widget_get_window (priv->tearoff_window);
1390 width = gdk_window_get_width (window);
1391 height = gdk_window_get_height (window);
1393 surface = gdk_window_create_similar_surface (window,
1394 CAIRO_CONTENT_COLOR,
1398 cr = cairo_create (surface);
1399 gdk_cairo_set_source_window (cr, window, 0, 0);
1403 gtk_widget_set_size_request (priv->tearoff_window, width, height);
1405 pattern = cairo_pattern_create_for_surface (surface);
1406 gdk_window_set_background_pattern (window, pattern);
1408 cairo_pattern_destroy (pattern);
1409 cairo_surface_destroy (surface);
1414 popup_grab_on_window (GdkWindow *window,
1415 GdkDevice *keyboard,
1417 guint32 activate_time)
1420 gdk_device_grab (keyboard, window,
1421 GDK_OWNERSHIP_WINDOW, TRUE,
1422 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1423 NULL, activate_time) != GDK_GRAB_SUCCESS)
1427 gdk_device_grab (pointer, window,
1428 GDK_OWNERSHIP_WINDOW, TRUE,
1429 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1430 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1431 GDK_POINTER_MOTION_MASK,
1432 NULL, activate_time) != GDK_GRAB_SUCCESS)
1435 gdk_device_ungrab (keyboard, activate_time);
1444 * gtk_menu_popup_for_device:
1446 * @device: (allow-none): a #GdkDevice
1447 * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1448 * menu item, or %NULL
1449 * @parent_menu_item: (allow-none): the menu item whose activation triggered
1450 * the popup, or %NULL
1451 * @func: (allow-none): a user supplied function used to position the menu,
1453 * @data: (allow-none): user supplied data to be passed to @func
1454 * @destroy: (allow-none): destroy notify for @data
1455 * @button: the mouse button which was pressed to initiate the event
1456 * @activate_time: the time at which the activation event occurred
1458 * Displays a menu and makes it available for selection.
1460 * Applications can use this function to display context-sensitive menus,
1461 * and will typically supply %NULL for the @parent_menu_shell,
1462 * @parent_menu_item, @func, @data and @destroy parameters. The default
1463 * menu positioning function will position the menu at the current position
1464 * of @device (or its corresponding pointer).
1466 * The @button parameter should be the mouse button pressed to initiate
1467 * the menu popup. If the menu popup was initiated by something other than
1468 * a mouse button press, such as a mouse button release or a keypress,
1469 * @button should be 0.
1471 * The @activate_time parameter is used to conflict-resolve initiation of
1472 * concurrent requests for mouse/keyboard grab requests. To function
1473 * properly, this needs to be the time stamp of the user event (such as
1474 * a mouse click or key press) that caused the initiation of the popup.
1475 * Only if no such event is available, gtk_get_current_event_time() can
1481 gtk_menu_popup_for_device (GtkMenu *menu,
1483 GtkWidget *parent_menu_shell,
1484 GtkWidget *parent_menu_item,
1485 GtkMenuPositionFunc func,
1487 GDestroyNotify destroy,
1489 guint32 activate_time)
1491 GtkMenuPrivate *priv = menu->priv;
1493 GtkWidget *xgrab_shell;
1495 GdkEvent *current_event;
1496 GtkMenuShell *menu_shell;
1497 gboolean grab_keyboard;
1498 GtkWidget *parent_toplevel;
1499 GdkDevice *keyboard, *pointer;
1501 g_return_if_fail (GTK_IS_MENU (menu));
1502 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1505 device = gtk_get_current_event_device ();
1509 GdkDisplay *display;
1510 GdkDeviceManager *device_manager;
1513 display = gtk_widget_get_display (GTK_WIDGET (menu));
1514 device_manager = gdk_display_get_device_manager (display);
1515 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1517 device = devices->data;
1519 g_list_free (devices);
1522 widget = GTK_WIDGET (menu);
1523 menu_shell = GTK_MENU_SHELL (menu);
1525 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1528 pointer = gdk_device_get_associated_device (device);
1533 keyboard = gdk_device_get_associated_device (device);
1536 menu_shell->priv->parent_menu_shell = parent_menu_shell;
1538 priv->seen_item_enter = FALSE;
1540 /* Find the last viewable ancestor, and make an X grab on it
1542 parent = GTK_WIDGET (menu);
1546 gboolean viewable = TRUE;
1547 GtkWidget *tmp = parent;
1551 if (!gtk_widget_get_mapped (tmp))
1556 tmp = gtk_widget_get_parent (tmp);
1560 xgrab_shell = parent;
1562 parent = GTK_MENU_SHELL (parent)->priv->parent_menu_shell;
1565 /* We want to receive events generated when we map the menu;
1566 * unfortunately, since there is probably already an implicit
1567 * grab in place from the button that the user used to pop up
1568 * the menu, we won't receive then -- in particular, the EnterNotify
1569 * when the menu pops up under the pointer.
1571 * If we are grabbing on a parent menu shell, no problem; just grab
1572 * on that menu shell first before popping up the window with
1573 * owner_events = TRUE.
1575 * When grabbing on the menu itself, things get more convoluted --
1576 * we do an explicit grab on a specially created window with
1577 * owner_events = TRUE, which we override further down with a
1578 * grab on the menu. (We can't grab on the menu until it is mapped;
1579 * we probably could just leave the grab on the other window,
1580 * with a little reorganization of the code in gtkmenu*).
1582 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1583 gtk_window_set_accept_focus (GTK_WINDOW (priv->toplevel), grab_keyboard);
1588 if (xgrab_shell && xgrab_shell != widget)
1590 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1592 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1593 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1598 GdkWindow *transfer_window;
1600 xgrab_shell = widget;
1601 transfer_window = menu_grab_transfer_window_get (menu);
1602 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1604 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1605 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1609 if (!GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab)
1611 /* We failed to make our pointer/keyboard grab.
1612 * Rather than leaving the user with a stuck up window,
1613 * we just abort here. Presumably the user will try again.
1615 menu_shell->priv->parent_menu_shell = NULL;
1616 menu_grab_transfer_window_destroy (menu);
1620 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1621 menu_shell->priv->active = TRUE;
1622 menu_shell->priv->button = button;
1624 /* If we are popping up the menu from something other than, a button
1625 * press then, as a heuristic, we ignore enter events for the menu
1626 * until we get a MOTION_NOTIFY.
1629 current_event = gtk_get_current_event ();
1632 if ((current_event->type != GDK_BUTTON_PRESS) &&
1633 (current_event->type != GDK_ENTER_NOTIFY))
1634 menu_shell->priv->ignore_enter = TRUE;
1636 gdk_event_free (current_event);
1639 menu_shell->priv->ignore_enter = TRUE;
1643 gtk_menu_tearoff_bg_copy (menu);
1645 gtk_menu_reparent (menu, priv->toplevel, FALSE);
1648 parent_toplevel = NULL;
1649 if (parent_menu_shell)
1650 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1651 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1653 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1655 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1658 /* Set transient for to get the right window group and parent */
1659 if (GTK_IS_WINDOW (parent_toplevel))
1660 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel),
1661 GTK_WINDOW (parent_toplevel));
1663 priv->parent_menu_item = parent_menu_item;
1664 priv->position_func = func;
1665 priv->position_func_data = data;
1666 priv->position_func_data_destroy = destroy;
1667 menu_shell->priv->activate_time = activate_time;
1669 /* We need to show the menu here rather in the init function
1670 * because code expects to be able to tell if the menu is onscreen
1671 * by looking at gtk_widget_get_visible (menu)
1673 gtk_widget_show (GTK_WIDGET (menu));
1675 /* Position the menu, possibly changing the size request
1677 gtk_menu_position (menu, TRUE);
1679 /* Compute the size of the toplevel and realize it so we
1680 * can scroll correctly.
1682 if (!gtk_widget_get_realized (GTK_WIDGET (menu)))
1684 GtkRequisition tmp_request;
1685 GtkAllocation tmp_allocation = { 0, };
1687 /* Instead of trusting the menu position function to queue a
1688 * resize when the menu goes out of bounds, invalidate the cached
1691 gtk_widget_queue_resize (GTK_WIDGET (menu));
1692 gtk_widget_get_preferred_size (priv->toplevel, &tmp_request, NULL);
1694 tmp_allocation.width = tmp_request.width;
1695 tmp_allocation.height = tmp_request.height;
1697 gtk_widget_size_allocate (priv->toplevel, &tmp_allocation);
1699 gtk_widget_realize (priv->toplevel);
1702 gtk_menu_scroll_to (menu, priv->scroll_offset);
1704 /* if no item is selected, select the first one */
1705 if (!menu_shell->priv->active_menu_item)
1707 gboolean touchscreen_mode;
1709 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1710 "gtk-touchscreen-mode", &touchscreen_mode,
1713 if (touchscreen_mode)
1714 gtk_menu_shell_select_first (menu_shell, TRUE);
1717 /* Once everything is set up correctly, map the toplevel */
1718 gtk_widget_show (priv->toplevel);
1720 if (xgrab_shell == widget)
1721 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1723 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1725 if (parent_menu_shell)
1727 gboolean keyboard_mode;
1729 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1730 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1732 else if (menu_shell->priv->button == 0) /* a keynav-activated context menu */
1733 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1735 _gtk_menu_shell_update_mnemonics (menu_shell);
1739 * gtk_menu_popup: (skip)
1741 * @parent_menu_shell: (allow-none): the menu shell containing the
1742 * triggering menu item, or %NULL
1743 * @parent_menu_item: (allow-none): the menu item whose activation
1744 * triggered the popup, or %NULL
1745 * @func: (allow-none): a user supplied function used to position
1746 * the menu, or %NULL
1747 * @data: user supplied data to be passed to @func.
1748 * @button: the mouse button which was pressed to initiate the event.
1749 * @activate_time: the time at which the activation event occurred.
1751 * Displays a menu and makes it available for selection.
1753 * Applications can use this function to display context-sensitive
1754 * menus, and will typically supply %NULL for the @parent_menu_shell,
1755 * @parent_menu_item, @func and @data parameters. The default menu
1756 * positioning function will position the menu at the current mouse
1759 * The @button parameter should be the mouse button pressed to initiate
1760 * the menu popup. If the menu popup was initiated by something other
1761 * than a mouse button press, such as a mouse button release or a keypress,
1762 * @button should be 0.
1764 * The @activate_time parameter is used to conflict-resolve initiation
1765 * of concurrent requests for mouse/keyboard grab requests. To function
1766 * properly, this needs to be the timestamp of the user event (such as
1767 * a mouse click or key press) that caused the initiation of the popup.
1768 * Only if no such event is available, gtk_get_current_event_time() can
1772 gtk_menu_popup (GtkMenu *menu,
1773 GtkWidget *parent_menu_shell,
1774 GtkWidget *parent_menu_item,
1775 GtkMenuPositionFunc func,
1778 guint32 activate_time)
1780 g_return_if_fail (GTK_IS_MENU (menu));
1782 gtk_menu_popup_for_device (menu,
1787 button, activate_time);
1794 * Removes the menu from the screen.
1797 gtk_menu_popdown (GtkMenu *menu)
1799 GtkMenuPrivate *priv;
1800 GtkMenuShell *menu_shell;
1803 g_return_if_fail (GTK_IS_MENU (menu));
1805 menu_shell = GTK_MENU_SHELL (menu);
1808 menu_shell->priv->parent_menu_shell = NULL;
1809 menu_shell->priv->active = FALSE;
1810 menu_shell->priv->ignore_enter = FALSE;
1812 priv->have_position = FALSE;
1814 gtk_menu_stop_scrolling (menu);
1815 gtk_menu_stop_navigating_submenu (menu);
1817 if (menu_shell->priv->active_menu_item)
1819 if (priv->old_active_menu_item)
1820 g_object_unref (priv->old_active_menu_item);
1821 priv->old_active_menu_item = menu_shell->priv->active_menu_item;
1822 g_object_ref (priv->old_active_menu_item);
1825 gtk_menu_shell_deselect (menu_shell);
1827 /* The X Grab, if present, will automatically be removed
1828 * when we hide the window
1832 gtk_widget_hide (priv->toplevel);
1833 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel), NULL);
1836 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1840 gtk_widget_set_size_request (priv->tearoff_window, -1, -1);
1842 if (gtk_bin_get_child (GTK_BIN (priv->toplevel)))
1844 gtk_menu_reparent (menu, priv->tearoff_hbox, TRUE);
1848 /* We popped up the menu from the tearoff, so we need to
1849 * release the grab - we aren't actually hiding the menu.
1851 if (menu_shell->priv->have_xgrab && pointer)
1853 GdkDevice *keyboard;
1855 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1856 keyboard = gdk_device_get_associated_device (pointer);
1859 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1863 /* gtk_menu_popdown is called each time a menu item is selected from
1864 * a torn off menu. Only scroll back to the saved position if the
1865 * non-tearoff menu was popped down.
1867 if (!priv->tearoff_active)
1868 gtk_menu_scroll_to (menu, priv->saved_scroll_offset);
1869 priv->tearoff_active = TRUE;
1872 gtk_widget_hide (GTK_WIDGET (menu));
1874 menu_shell->priv->have_xgrab = FALSE;
1877 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1879 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1881 menu_grab_transfer_window_destroy (menu);
1885 * gtk_menu_get_active:
1888 * Returns the selected menu item from the menu. This is used by the
1891 * Returns: (transfer none): the #GtkMenuItem that was last selected
1892 * in the menu. If a selection has not yet been made, the
1893 * first menu item is selected.
1896 gtk_menu_get_active (GtkMenu *menu)
1898 GtkMenuPrivate *priv = menu->priv;
1902 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1904 if (!priv->old_active_menu_item)
1907 children = GTK_MENU_SHELL (menu)->priv->children;
1911 child = children->data;
1912 children = children->next;
1914 if (gtk_bin_get_child (GTK_BIN (child)))
1919 priv->old_active_menu_item = child;
1920 if (priv->old_active_menu_item)
1921 g_object_ref (priv->old_active_menu_item);
1924 return priv->old_active_menu_item;
1928 * gtk_menu_set_active:
1930 * @index: the index of the menu item to select. Iindex values are
1933 * Selects the specified menu item within the menu. This is used by
1934 * the #GtkOptionMenu and should not be used by anyone else.
1937 gtk_menu_set_active (GtkMenu *menu,
1940 GtkMenuPrivate *priv = menu->priv;
1944 g_return_if_fail (GTK_IS_MENU (menu));
1946 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->priv->children, index);
1949 child = tmp_list->data;
1950 if (gtk_bin_get_child (GTK_BIN (child)))
1952 if (priv->old_active_menu_item)
1953 g_object_unref (priv->old_active_menu_item);
1954 priv->old_active_menu_item = child;
1955 g_object_ref (priv->old_active_menu_item);
1961 * gtk_menu_set_accel_group:
1963 * @accel_group: (allow-none): the #GtkAccelGroup to be associated
1966 * Set the #GtkAccelGroup which holds global accelerators for the
1967 * menu. This accelerator group needs to also be added to all windows
1968 * that this menu is being used in with gtk_window_add_accel_group(),
1969 * in order for those windows to support all the accelerators
1970 * contained in this group.
1973 gtk_menu_set_accel_group (GtkMenu *menu,
1974 GtkAccelGroup *accel_group)
1976 GtkMenuPrivate *priv = menu->priv;
1977 g_return_if_fail (GTK_IS_MENU (menu));
1979 if (priv->accel_group != accel_group)
1981 if (priv->accel_group)
1982 g_object_unref (priv->accel_group);
1983 priv->accel_group = accel_group;
1984 if (priv->accel_group)
1985 g_object_ref (priv->accel_group);
1986 _gtk_menu_refresh_accel_paths (menu, TRUE);
1991 * gtk_menu_get_accel_group:
1994 * Gets the #GtkAccelGroup which holds global accelerators for the
1995 * menu. See gtk_menu_set_accel_group().
1997 * Returns: (transfer none): the #GtkAccelGroup associated with the menu
2000 gtk_menu_get_accel_group (GtkMenu *menu)
2002 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2004 return menu->priv->accel_group;
2008 gtk_menu_real_can_activate_accel (GtkWidget *widget,
2011 /* Menu items chain here to figure whether they can activate their
2012 * accelerators. Unlike ordinary widgets, menus allow accel
2013 * activation even if invisible since that's the usual case for
2014 * submenus/popup-menus. however, the state of the attach widget
2015 * affects the "activeness" of the menu.
2017 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
2020 return gtk_widget_can_activate_accel (awidget, signal_id);
2022 return gtk_widget_is_sensitive (widget);
2026 * gtk_menu_set_accel_path
2027 * @menu: a valid #GtkMenu
2028 * @accel_path: (allow-none): a valid accelerator path
2030 * Sets an accelerator path for this menu from which accelerator paths
2031 * for its immediate children, its menu items, can be constructed.
2032 * The main purpose of this function is to spare the programmer the
2033 * inconvenience of having to call gtk_menu_item_set_accel_path() on
2034 * each menu item that should support runtime user changable accelerators.
2035 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
2036 * each menu item of this menu, that contains a label describing its
2037 * purpose, automatically gets an accel path assigned.
2039 * For example, a menu containing menu items "New" and "Exit", will, after
2040 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
2041 * has been called, assign its items the accel paths:
2042 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
2044 * Assigning accel paths to menu items then enables the user to change
2045 * their accelerators at runtime. More details about accelerator paths
2046 * and their default setups can be found at gtk_accel_map_add_entry().
2048 * Note that @accel_path string will be stored in a #GQuark. Therefore,
2049 * if you pass a static string, you can save some memory by interning
2050 * it first with g_intern_static_string().
2053 gtk_menu_set_accel_path (GtkMenu *menu,
2054 const gchar *accel_path)
2056 GtkMenuPrivate *priv = menu->priv;
2057 g_return_if_fail (GTK_IS_MENU (menu));
2060 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
2062 /* FIXME: accel_path should be defined as const gchar* */
2063 priv->accel_path = (gchar*)g_intern_string (accel_path);
2064 if (priv->accel_path)
2065 _gtk_menu_refresh_accel_paths (menu, FALSE);
2069 * gtk_menu_get_accel_path
2070 * @menu: a valid #GtkMenu
2072 * Retrieves the accelerator path set on the menu.
2074 * Returns: the accelerator path set on the menu.
2079 gtk_menu_get_accel_path (GtkMenu *menu)
2081 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2083 return menu->priv->accel_path;
2088 gboolean group_changed;
2092 refresh_accel_paths_foreach (GtkWidget *widget,
2095 GtkMenuPrivate *priv;
2096 AccelPropagation *prop = data;
2098 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
2100 priv = prop->menu->priv;
2101 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2104 prop->group_changed);
2109 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
2110 gboolean group_changed)
2112 GtkMenuPrivate *priv = menu->priv;
2113 g_return_if_fail (GTK_IS_MENU (menu));
2115 if (priv->accel_path && priv->accel_group)
2117 AccelPropagation prop;
2120 prop.group_changed = group_changed;
2121 gtk_container_foreach (GTK_CONTAINER (menu),
2122 refresh_accel_paths_foreach,
2128 * gtk_menu_reposition:
2131 * Repositions the menu according to its position function.
2134 gtk_menu_reposition (GtkMenu *menu)
2136 g_return_if_fail (GTK_IS_MENU (menu));
2138 if (!menu->priv->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2139 gtk_menu_position (menu, FALSE);
2143 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2148 value = gtk_adjustment_get_value (adjustment);
2149 if (menu->priv->scroll_offset != value)
2150 gtk_menu_scroll_to (menu, value);
2154 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2157 GtkMenuPrivate *priv = menu->priv;
2158 GdkGeometry geometry_hints;
2160 if (!priv->tearoff_window)
2163 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2165 GtkRequisition requisition;
2167 gtk_widget_get_preferred_size (priv->tearoff_scrollbar,
2168 &requisition, NULL);
2169 width += requisition.width;
2172 geometry_hints.min_width = width;
2173 geometry_hints.max_width = width;
2175 geometry_hints.min_height = 0;
2176 geometry_hints.max_height = priv->requested_height;
2178 gtk_window_set_geometry_hints (GTK_WINDOW (priv->tearoff_window),
2181 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2185 gtk_menu_update_title (GtkMenu *menu)
2187 GtkMenuPrivate *priv = menu->priv;
2189 if (priv->tearoff_window)
2192 GtkWidget *attach_widget;
2194 title = gtk_menu_get_title (menu);
2197 attach_widget = gtk_menu_get_attach_widget (menu);
2198 if (GTK_IS_MENU_ITEM (attach_widget))
2200 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2201 if (GTK_IS_LABEL (child))
2202 title = gtk_label_get_text (GTK_LABEL (child));
2207 gtk_window_set_title (GTK_WINDOW (priv->tearoff_window), title);
2212 gtk_menu_get_toplevel (GtkWidget *menu)
2214 GtkWidget *attach, *toplevel;
2216 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2218 if (GTK_IS_MENU_ITEM (attach))
2219 attach = gtk_widget_get_parent (attach);
2221 if (GTK_IS_MENU (attach))
2222 return gtk_menu_get_toplevel (attach);
2223 else if (GTK_IS_WIDGET (attach))
2225 toplevel = gtk_widget_get_toplevel (attach);
2226 if (gtk_widget_is_toplevel (toplevel))
2234 tearoff_window_destroyed (GtkWidget *widget,
2237 gtk_menu_set_tearoff_state (menu, FALSE);
2241 * gtk_menu_set_tearoff_state:
2243 * @torn_off: If %TRUE, menu is displayed as a tearoff menu.
2245 * Changes the tearoff state of the menu. A menu is normally
2246 * displayed as drop down menu which persists as long as the menu is
2247 * active. It can also be displayed as a tearoff menu which persists
2248 * until it is closed or reattached.
2251 gtk_menu_set_tearoff_state (GtkMenu *menu,
2254 GtkMenuPrivate *priv = menu->priv;
2257 g_return_if_fail (GTK_IS_MENU (menu));
2259 if (priv->torn_off != torn_off)
2261 priv->torn_off = torn_off;
2262 priv->tearoff_active = torn_off;
2266 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2267 gtk_menu_popdown (menu);
2269 if (!priv->tearoff_window)
2271 GtkWidget *toplevel;
2273 priv->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2274 "type", GTK_WINDOW_TOPLEVEL,
2275 "screen", gtk_widget_get_screen (priv->toplevel),
2276 "app-paintable", TRUE,
2279 gtk_window_set_type_hint (GTK_WINDOW (priv->tearoff_window),
2280 GDK_WINDOW_TYPE_HINT_MENU);
2281 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->tearoff_window), 0);
2282 g_signal_connect (priv->tearoff_window, "destroy",
2283 G_CALLBACK (tearoff_window_destroyed), menu);
2284 g_signal_connect (priv->tearoff_window, "event",
2285 G_CALLBACK (gtk_menu_window_event), menu);
2287 gtk_menu_update_title (menu);
2289 gtk_widget_realize (priv->tearoff_window);
2291 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2292 if (toplevel != NULL)
2293 gtk_window_set_transient_for (GTK_WINDOW (priv->tearoff_window),
2294 GTK_WINDOW (toplevel));
2296 priv->tearoff_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2297 gtk_container_add (GTK_CONTAINER (priv->tearoff_window),
2298 priv->tearoff_hbox);
2300 height = gdk_window_get_height (gtk_widget_get_window (GTK_WIDGET (menu)));
2301 priv->tearoff_adjustment = gtk_adjustment_new (0,
2302 0, priv->requested_height,
2306 g_object_connect (priv->tearoff_adjustment,
2307 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2309 priv->tearoff_scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, priv->tearoff_adjustment);
2311 gtk_box_pack_end (GTK_BOX (priv->tearoff_hbox),
2312 priv->tearoff_scrollbar,
2315 if (gtk_adjustment_get_upper (priv->tearoff_adjustment) > height)
2316 gtk_widget_show (priv->tearoff_scrollbar);
2318 gtk_widget_show (priv->tearoff_hbox);
2321 gtk_menu_reparent (menu, priv->tearoff_hbox, FALSE);
2323 /* Update menu->requisition */
2324 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, NULL);
2326 gtk_menu_set_tearoff_hints (menu, gdk_window_get_width (gtk_widget_get_window (GTK_WIDGET (menu))));
2328 gtk_widget_realize (priv->tearoff_window);
2329 gtk_menu_position (menu, TRUE);
2331 gtk_widget_show (GTK_WIDGET (menu));
2332 gtk_widget_show (priv->tearoff_window);
2334 gtk_menu_scroll_to (menu, 0);
2339 gtk_widget_hide (GTK_WIDGET (menu));
2340 gtk_widget_hide (priv->tearoff_window);
2341 if (GTK_IS_CONTAINER (priv->toplevel))
2342 gtk_menu_reparent (menu, priv->toplevel, FALSE);
2343 gtk_widget_destroy (priv->tearoff_window);
2345 priv->tearoff_window = NULL;
2346 priv->tearoff_hbox = NULL;
2347 priv->tearoff_scrollbar = NULL;
2348 priv->tearoff_adjustment = NULL;
2351 g_object_notify (G_OBJECT (menu), "tearoff-state");
2356 * gtk_menu_get_tearoff_state:
2359 * Returns whether the menu is torn off.
2360 * See gtk_menu_set_tearoff_state().
2362 * Return value: %TRUE if the menu is currently torn off.
2365 gtk_menu_get_tearoff_state (GtkMenu *menu)
2367 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2369 return menu->priv->torn_off;
2373 * gtk_menu_set_title:
2375 * @title: a string containing the title for the menu
2377 * Sets the title string for the menu.
2379 * The title is displayed when the menu is shown as a tearoff
2380 * menu. If @title is %NULL, the menu will see if it is attached
2381 * to a parent menu item, and if so it will try to use the same
2382 * text as that menu item's label.
2385 gtk_menu_set_title (GtkMenu *menu,
2388 GtkMenuPrivate *priv = menu->priv;
2391 g_return_if_fail (GTK_IS_MENU (menu));
2393 old_title = priv->title;
2394 priv->title = g_strdup (title);
2397 gtk_menu_update_title (menu);
2398 g_object_notify (G_OBJECT (menu), "tearoff-title");
2402 * gtk_menu_get_title:
2405 * Returns the title of the menu. See gtk_menu_set_title().
2407 * Return value: the title of the menu, or %NULL if the menu
2408 * has no title set on it. This string is owned by GTK+
2409 * and should not be modified or freed.
2412 gtk_menu_get_title (GtkMenu *menu)
2414 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2416 return menu->priv->title;
2420 * gtk_menu_reorder_child:
2422 * @child: the #GtkMenuItem to move
2423 * @position: the new position to place @child.
2424 * Positions are numbered from 0 to n - 1
2426 * Moves @child to a new @position in the list of @menu
2430 gtk_menu_reorder_child (GtkMenu *menu,
2434 GtkMenuShell *menu_shell;
2436 g_return_if_fail (GTK_IS_MENU (menu));
2437 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2439 menu_shell = GTK_MENU_SHELL (menu);
2441 if (g_list_find (menu_shell->priv->children, child))
2443 menu_shell->priv->children = g_list_remove (menu_shell->priv->children, child);
2444 menu_shell->priv->children = g_list_insert (menu_shell->priv->children, child, position);
2446 menu_queue_resize (menu);
2451 gtk_menu_style_updated (GtkWidget *widget)
2453 GTK_WIDGET_CLASS (gtk_menu_parent_class)->style_updated (widget);
2455 if (gtk_widget_get_realized (widget))
2457 GtkMenu *menu = GTK_MENU (widget);
2458 GtkMenuPrivate *priv = menu->priv;
2459 GtkStyleContext *context;
2461 context = gtk_widget_get_style_context (widget);
2463 gtk_style_context_set_background (context, priv->bin_window);
2464 gtk_style_context_set_background (context, priv->view_window);
2465 gtk_style_context_set_background (context, gtk_widget_get_window (widget));
2470 get_arrows_border (GtkMenu *menu,
2473 GtkMenuPrivate *priv = menu->priv;
2474 guint scroll_arrow_height;
2475 GtkArrowPlacement arrow_placement;
2477 gtk_widget_style_get (GTK_WIDGET (menu),
2478 "scroll-arrow-vlength", &scroll_arrow_height,
2479 "arrow_placement", &arrow_placement,
2482 switch (arrow_placement)
2484 case GTK_ARROWS_BOTH:
2485 border->top = priv->upper_arrow_visible ? scroll_arrow_height : 0;
2486 border->bottom = priv->lower_arrow_visible ? scroll_arrow_height : 0;
2489 case GTK_ARROWS_START:
2490 border->top = (priv->upper_arrow_visible ||
2491 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2495 case GTK_ARROWS_END:
2497 border->bottom = (priv->upper_arrow_visible ||
2498 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2502 border->left = border->right = 0;
2506 get_menu_padding (GtkWidget *widget,
2509 GtkStyleContext *context;
2510 GtkStateFlags state;
2512 context = gtk_widget_get_style_context (widget);
2513 state = gtk_widget_get_state_flags (widget);
2515 gtk_style_context_get_padding (context, state, padding);
2519 gtk_menu_realize (GtkWidget *widget)
2521 GtkMenu *menu = GTK_MENU (widget);
2522 GtkMenuPrivate *priv = menu->priv;
2523 GtkAllocation allocation;
2524 GtkStyleContext *context;
2526 GdkWindowAttr attributes;
2527 gint attributes_mask;
2531 guint vertical_padding;
2532 guint horizontal_padding;
2533 GtkBorder arrow_border, padding;
2535 g_return_if_fail (GTK_IS_MENU (widget));
2537 gtk_widget_set_realized (widget, TRUE);
2539 gtk_widget_get_allocation (widget, &allocation);
2541 attributes.window_type = GDK_WINDOW_CHILD;
2542 attributes.x = allocation.x;
2543 attributes.y = allocation.y;
2544 attributes.width = allocation.width;
2545 attributes.height = allocation.height;
2546 attributes.wclass = GDK_INPUT_OUTPUT;
2547 attributes.visual = gtk_widget_get_visual (widget);
2548 attributes.event_mask = gtk_widget_get_events (widget);
2549 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2550 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2552 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2554 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2555 &attributes, attributes_mask);
2556 gtk_widget_set_window (widget, window);
2557 gdk_window_set_user_data (window, widget);
2559 get_menu_padding (widget, &padding);
2560 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2561 context = gtk_widget_get_style_context (widget);
2563 gtk_widget_style_get (GTK_WIDGET (menu),
2564 "vertical-padding", &vertical_padding,
2565 "horizontal-padding", &horizontal_padding,
2568 gtk_widget_get_allocation (widget, &allocation);
2570 attributes.x = border_width + padding.left + horizontal_padding;
2571 attributes.y = border_width + padding.top + vertical_padding;
2572 attributes.width = allocation.width -
2573 (2 * (border_width + horizontal_padding)) - padding.left - padding.right;
2574 attributes.height = allocation.height -
2575 (2 * (border_width + vertical_padding)) - padding.top - padding.bottom;
2577 get_arrows_border (menu, &arrow_border);
2578 attributes.y += arrow_border.top;
2579 attributes.height -= arrow_border.top;
2580 attributes.height -= arrow_border.bottom;
2582 attributes.width = MAX (1, attributes.width);
2583 attributes.height = MAX (1, attributes.height);
2585 priv->view_window = gdk_window_new (window,
2586 &attributes, attributes_mask);
2587 gdk_window_set_user_data (priv->view_window, menu);
2589 gtk_widget_get_allocation (widget, &allocation);
2593 attributes.width = allocation.width + (2 * (border_width + horizontal_padding)) +
2594 padding.left + padding.right;
2595 attributes.height = priv->requested_height - (2 * (border_width + vertical_padding)) +
2596 padding.top + padding.bottom;
2598 attributes.width = MAX (1, attributes.width);
2599 attributes.height = MAX (1, attributes.height);
2601 priv->bin_window = gdk_window_new (priv->view_window,
2602 &attributes, attributes_mask);
2603 gdk_window_set_user_data (priv->bin_window, menu);
2605 children = GTK_MENU_SHELL (menu)->priv->children;
2608 child = children->data;
2609 children = children->next;
2611 gtk_widget_set_parent_window (child, priv->bin_window);
2614 gtk_style_context_set_background (context, priv->bin_window);
2615 gtk_style_context_set_background (context, priv->view_window);
2616 gtk_style_context_set_background (context, window);
2618 if (GTK_MENU_SHELL (widget)->priv->active_menu_item)
2619 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2620 GTK_MENU_SHELL (widget)->priv->active_menu_item);
2622 gdk_window_show (priv->bin_window);
2623 gdk_window_show (priv->view_window);
2627 gtk_menu_focus (GtkWidget *widget,
2628 GtkDirectionType direction)
2630 /* A menu or its menu items cannot have focus */
2634 /* See notes in gtk_menu_popup() for information
2635 * about the "grab transfer window"
2638 menu_grab_transfer_window_get (GtkMenu *menu)
2640 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2643 GdkWindowAttr attributes;
2644 gint attributes_mask;
2646 attributes.x = -100;
2647 attributes.y = -100;
2648 attributes.width = 10;
2649 attributes.height = 10;
2650 attributes.window_type = GDK_WINDOW_TEMP;
2651 attributes.wclass = GDK_INPUT_ONLY;
2652 attributes.override_redirect = TRUE;
2653 attributes.event_mask = 0;
2655 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2657 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2658 &attributes, attributes_mask);
2659 gdk_window_set_user_data (window, menu);
2661 gdk_window_show (window);
2663 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2670 menu_grab_transfer_window_destroy (GtkMenu *menu)
2672 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2675 gdk_window_set_user_data (window, NULL);
2676 gdk_window_destroy (window);
2677 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2682 gtk_menu_unrealize (GtkWidget *widget)
2684 GtkMenu *menu = GTK_MENU (widget);
2685 GtkMenuPrivate *priv = menu->priv;
2687 menu_grab_transfer_window_destroy (menu);
2689 gdk_window_set_user_data (priv->view_window, NULL);
2690 gdk_window_destroy (priv->view_window);
2691 priv->view_window = NULL;
2693 gdk_window_set_user_data (priv->bin_window, NULL);
2694 gdk_window_destroy (priv->bin_window);
2695 priv->bin_window = NULL;
2697 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2701 calculate_line_heights (GtkMenu *menu,
2703 guint **ret_min_heights,
2704 guint **ret_nat_heights)
2707 GtkMenuPrivate *priv;
2708 GtkMenuShell *menu_shell;
2709 GtkWidget *child, *widget;
2711 guint horizontal_padding;
2720 widget = GTK_WIDGET (menu);
2721 menu_shell = GTK_MENU_SHELL (widget);
2723 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2724 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2725 n_heights = gtk_menu_get_n_rows (menu);
2726 n_columns = gtk_menu_get_n_columns (menu);
2727 avail_width = for_width - (2 * priv->toggle_size + priv->accel_size) * n_columns;
2729 gtk_widget_style_get (GTK_WIDGET (menu),
2730 "horizontal-padding", &horizontal_padding,
2732 get_menu_padding (widget, &padding);
2734 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2735 avail_width -= (border_width + horizontal_padding) * 2 + padding.left + padding.right;
2737 for (children = menu_shell->priv->children; children; children = children->next)
2742 gint child_min, child_nat;
2744 child = children->data;
2746 if (!gtk_widget_get_visible (child))
2749 get_effective_child_attach (child, &l, &r, &t, &b);
2751 part = avail_width / (r - l);
2753 gtk_widget_get_preferred_height_for_width (child, part,
2754 &child_min, &child_nat);
2756 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2758 part = MAX (child_min, toggle_size) / (b - t);
2759 min_heights[t] = MAX (min_heights[t], part);
2761 part = MAX (child_nat, toggle_size) / (b - t);
2762 nat_heights[t] = MAX (nat_heights[t], part);
2765 if (ret_min_heights)
2766 *ret_min_heights = min_heights;
2768 g_free (min_heights);
2770 if (ret_nat_heights)
2771 *ret_nat_heights = nat_heights;
2773 g_free (nat_heights);
2779 gtk_menu_size_allocate (GtkWidget *widget,
2780 GtkAllocation *allocation)
2783 GtkMenuPrivate *priv;
2784 GtkMenuShell *menu_shell;
2786 GtkAllocation child_allocation;
2791 guint vertical_padding;
2792 guint horizontal_padding;
2795 g_return_if_fail (GTK_IS_MENU (widget));
2796 g_return_if_fail (allocation != NULL);
2798 menu = GTK_MENU (widget);
2799 menu_shell = GTK_MENU_SHELL (widget);
2802 gtk_widget_set_allocation (widget, allocation);
2804 gtk_widget_style_get (GTK_WIDGET (menu),
2805 "vertical-padding", &vertical_padding,
2806 "horizontal-padding", &horizontal_padding,
2809 get_menu_padding (widget, &padding);
2810 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2812 g_free (priv->heights);
2813 priv->heights_length = calculate_line_heights (menu,
2818 /* refresh our cached height request */
2819 priv->requested_height = (2 * (border_width + vertical_padding)) +
2820 padding.top + padding.bottom;
2821 for (i = 0; i < priv->heights_length; i++)
2822 priv->requested_height += priv->heights[i];
2824 x = border_width + padding.left + horizontal_padding;
2825 y = border_width + padding.top + vertical_padding;
2826 width = allocation->width - (2 * (border_width + horizontal_padding)) -
2827 padding.left - padding.right;
2828 height = allocation->height - (2 * (border_width + vertical_padding)) -
2829 padding.top - padding.bottom;
2831 if (menu_shell->priv->active)
2832 gtk_menu_scroll_to (menu, priv->scroll_offset);
2834 if (!priv->tearoff_active)
2836 GtkBorder arrow_border;
2838 get_arrows_border (menu, &arrow_border);
2839 y += arrow_border.top;
2840 height -= arrow_border.top;
2841 height -= arrow_border.bottom;
2844 width = MAX (1, width);
2845 height = MAX (1, height);
2847 if (gtk_widget_get_realized (widget))
2849 gdk_window_move_resize (gtk_widget_get_window (widget),
2850 allocation->x, allocation->y,
2851 allocation->width, allocation->height);
2853 gdk_window_move_resize (priv->view_window, x, y, width, height);
2856 if (menu_shell->priv->children)
2858 gint base_width = width / gtk_menu_get_n_columns (menu);
2860 children = menu_shell->priv->children;
2863 child = children->data;
2864 children = children->next;
2866 if (gtk_widget_get_visible (child))
2871 get_effective_child_attach (child, &l, &r, &t, &b);
2873 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2876 tmp = gtk_menu_get_n_columns (menu) - l;
2877 l = gtk_menu_get_n_columns (menu) - r;
2881 child_allocation.width = (r - l) * base_width;
2882 child_allocation.height = 0;
2883 child_allocation.x = l * base_width;
2884 child_allocation.y = 0;
2886 for (i = 0; i < b; i++)
2889 child_allocation.y += priv->heights[i];
2891 child_allocation.height += priv->heights[i];
2894 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2897 gtk_widget_size_allocate (child, &child_allocation);
2898 gtk_widget_queue_draw (child);
2902 /* Resize the item window */
2903 if (gtk_widget_get_realized (widget))
2909 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2910 height += priv->heights[i];
2912 width = gtk_menu_get_n_columns (menu) * base_width;
2913 gdk_window_resize (priv->bin_window, width, height);
2916 if (priv->tearoff_active)
2918 if (height >= priv->requested_height)
2920 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2922 gtk_widget_hide (priv->tearoff_scrollbar);
2923 gtk_menu_set_tearoff_hints (menu, allocation->width);
2925 gtk_menu_scroll_to (menu, 0);
2930 gtk_adjustment_configure (priv->tearoff_adjustment,
2931 gtk_adjustment_get_value (priv->tearoff_adjustment),
2933 priv->requested_height,
2934 gtk_adjustment_get_step_increment (priv->tearoff_adjustment),
2935 gtk_adjustment_get_page_increment (priv->tearoff_adjustment),
2936 allocation->height);
2938 if (!gtk_widget_get_visible (priv->tearoff_scrollbar))
2940 gtk_widget_show (priv->tearoff_scrollbar);
2941 gtk_menu_set_tearoff_hints (menu, allocation->width);
2949 get_arrows_visible_area (GtkMenu *menu,
2950 GdkRectangle *border,
2951 GdkRectangle *upper,
2952 GdkRectangle *lower,
2955 GtkArrowPlacement arrow_placement;
2956 GtkWidget *widget = GTK_WIDGET (menu);
2958 guint vertical_padding;
2959 guint horizontal_padding;
2960 gint scroll_arrow_height;
2961 GtkBorder menu_padding;
2963 gtk_widget_style_get (widget,
2964 "vertical-padding", &vertical_padding,
2965 "horizontal-padding", &horizontal_padding,
2966 "scroll-arrow-vlength", &scroll_arrow_height,
2967 "arrow-placement", &arrow_placement,
2970 get_menu_padding (widget, &menu_padding);
2971 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2972 border->x = border_width + menu_padding.left + horizontal_padding;
2973 border->y = border_width + menu_padding.top + vertical_padding;
2974 border->width = gdk_window_get_width (gtk_widget_get_window (widget));
2975 border->height = gdk_window_get_height (gtk_widget_get_window (widget));
2977 switch (arrow_placement)
2979 case GTK_ARROWS_BOTH:
2980 upper->x = border->x;
2981 upper->y = border->y;
2982 upper->width = border->width - 2 * border->x;
2983 upper->height = scroll_arrow_height;
2985 lower->x = border->x;
2986 lower->y = border->height - border->y - scroll_arrow_height;
2987 lower->width = border->width - 2 * border->x;
2988 lower->height = scroll_arrow_height;
2991 case GTK_ARROWS_START:
2992 upper->x = border->x;
2993 upper->y = border->y;
2994 upper->width = (border->width - 2 * border->x) / 2;
2995 upper->height = scroll_arrow_height;
2997 lower->x = border->x + upper->width;
2998 lower->y = border->y;
2999 lower->width = (border->width - 2 * border->x) / 2;
3000 lower->height = scroll_arrow_height;
3003 case GTK_ARROWS_END:
3004 upper->x = border->x;
3005 upper->y = border->height - border->y - scroll_arrow_height;
3006 upper->width = (border->width - 2 * border->x) / 2;
3007 upper->height = scroll_arrow_height;
3009 lower->x = border->x + upper->width;
3010 lower->y = border->height - border->y - scroll_arrow_height;
3011 lower->width = (border->width - 2 * border->x) / 2;
3012 lower->height = scroll_arrow_height;
3016 g_assert_not_reached();
3017 upper->x = upper->y = upper->width = upper->height = 0;
3018 lower->x = lower->y = lower->width = lower->height = 0;
3021 *arrow_space = scroll_arrow_height - menu_padding.top - menu_padding.bottom;
3025 gtk_menu_draw (GtkWidget *widget,
3029 GtkMenuPrivate *priv;
3030 GtkStyleContext *context;
3031 GdkRectangle border;
3035 GtkBorder menu_padding;
3037 menu = GTK_MENU (widget);
3039 context = gtk_widget_get_style_context (widget);
3041 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
3042 get_menu_padding (widget, &menu_padding);
3044 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
3046 gfloat arrow_scaling;
3049 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
3050 arrow_size = arrow_scaling * arrow_space;
3052 gtk_render_background (context, cr, 0, 0,
3053 gtk_widget_get_allocated_width (widget),
3054 gtk_widget_get_allocated_height (widget));
3055 gtk_render_frame (context, cr, 0, 0,
3056 gtk_widget_get_allocated_width (widget),
3057 gtk_widget_get_allocated_height (widget));
3059 gtk_style_context_save (context);
3060 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
3062 if (priv->upper_arrow_visible && !priv->tearoff_active)
3064 gtk_style_context_save (context);
3065 gtk_style_context_set_state (context, priv->upper_arrow_state);
3067 gtk_render_background (context, cr,
3069 upper.width, upper.height);
3070 gtk_render_frame (context, cr,
3072 upper.width, upper.height);
3074 gtk_render_arrow (context, cr, 0,
3075 upper.x + (upper.width - arrow_size) / 2,
3076 upper.y + menu_padding.top + (arrow_space - arrow_size) / 2,
3079 gtk_style_context_restore (context);
3082 if (priv->lower_arrow_visible && !priv->tearoff_active)
3084 gtk_style_context_save (context);
3085 gtk_style_context_set_state (context, priv->lower_arrow_state);
3087 gtk_render_background (context, cr,
3089 lower.width, lower.height);
3090 gtk_render_frame (context, cr,
3092 lower.width, lower.height);
3094 gtk_render_arrow (context, cr, G_PI,
3095 lower.x + (lower.width - arrow_size) / 2,
3096 lower.y + menu_padding.bottom + (arrow_space - arrow_size) / 2,
3099 gtk_style_context_restore (context);
3102 gtk_style_context_restore (context);
3105 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
3107 gint y = -border.y + priv->scroll_offset;
3110 gtk_cairo_transform_to_window (cr, widget, priv->bin_window);
3112 if (!priv->tearoff_active)
3114 GtkBorder arrow_border;
3116 get_arrows_border (menu, &arrow_border);
3117 y -= arrow_border.top;
3120 gtk_render_background (context, cr,
3122 border.width, border.height);
3123 gtk_render_frame (context, cr,
3125 border.width, border.height);
3130 GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
3136 gtk_menu_show (GtkWidget *widget)
3138 GtkMenu *menu = GTK_MENU (widget);
3140 _gtk_menu_refresh_accel_paths (menu, FALSE);
3142 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3147 gtk_menu_get_preferred_width (GtkWidget *widget,
3152 GtkMenuShell *menu_shell;
3153 GtkMenuPrivate *priv;
3156 guint max_toggle_size;
3157 guint max_accel_width;
3158 guint horizontal_padding;
3160 gint child_min, child_nat;
3161 gint min_width, nat_width;
3164 menu = GTK_MENU (widget);
3165 menu_shell = GTK_MENU_SHELL (widget);
3168 min_width = nat_width = 0;
3170 max_toggle_size = 0;
3171 max_accel_width = 0;
3173 children = menu_shell->priv->children;
3180 child = children->data;
3181 children = children->next;
3183 if (! gtk_widget_get_visible (child))
3186 get_effective_child_attach (child, &l, &r, &t, &b);
3188 /* It's important to size_request the child
3189 * before doing the toggle size request, in
3190 * case the toggle size request depends on the size
3191 * request of a child of the child (e.g. for ImageMenuItem)
3193 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
3195 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3196 max_toggle_size = MAX (max_toggle_size, toggle_size);
3197 max_accel_width = MAX (max_accel_width,
3198 GTK_MENU_ITEM (child)->priv->accelerator_width);
3200 part = child_min / (r - l);
3201 min_width = MAX (min_width, part);
3203 part = child_nat / (r - l);
3204 nat_width = MAX (nat_width, part);
3207 /* If the menu doesn't include any images or check items
3208 * reserve the space so that all menus are consistent.
3209 * We only do this for 'ordinary' menus, not for combobox
3210 * menus or multi-column menus
3212 if (max_toggle_size == 0 &&
3213 gtk_menu_get_n_columns (menu) == 1 &&
3214 !priv->no_toggle_size)
3216 GtkStyleContext *context;
3217 GtkWidgetPath *menu_path, *check_path;
3218 guint toggle_spacing;
3219 guint indicator_size;
3221 context = gtk_widget_get_style_context (widget);
3222 menu_path = gtk_widget_path_copy (gtk_style_context_get_path (context));
3224 /* Create a GtkCheckMenuItem path, only to query indicator spacing */
3225 check_path = gtk_widget_path_copy (menu_path);
3226 gtk_widget_path_append_type (check_path, GTK_TYPE_CHECK_MENU_ITEM);
3228 gtk_style_context_set_path (context, check_path);
3229 gtk_widget_path_free (check_path);
3231 gtk_style_context_get_style (context,
3232 "toggle-spacing", &toggle_spacing,
3233 "indicator-size", &indicator_size,
3236 max_toggle_size = indicator_size + toggle_spacing;
3238 /* Restore real widget path */
3239 gtk_style_context_set_path (context, menu_path);
3240 gtk_widget_path_free (menu_path);
3243 min_width += 2 * max_toggle_size + max_accel_width;
3244 min_width *= gtk_menu_get_n_columns (menu);
3246 nat_width += 2 * max_toggle_size + max_accel_width;
3247 nat_width *= gtk_menu_get_n_columns (menu);
3249 gtk_widget_style_get (GTK_WIDGET (menu),
3250 "horizontal-padding", &horizontal_padding,
3253 get_menu_padding (widget, &padding);
3254 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3255 min_width += (2 * (border_width + horizontal_padding)) +
3256 padding.left + padding.right;
3257 nat_width += (2 * (border_width + horizontal_padding)) +
3258 padding.left + padding.right;
3260 priv->toggle_size = max_toggle_size;
3261 priv->accel_size = max_accel_width;
3264 *minimum_size = min_width;
3267 *natural_size = nat_width;
3269 /* Don't resize the tearoff if it is not active,
3270 * because it won't redraw (it is only a background pixmap).
3272 if (priv->tearoff_active)
3273 gtk_menu_set_tearoff_hints (menu, min_width);
3277 gtk_menu_get_preferred_height (GtkWidget *widget,
3283 /* Menus are height-for-width only, just return the height
3284 * for the minimum width
3286 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
3287 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
3291 gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
3297 GtkMenu *menu = GTK_MENU (widget);
3298 GtkMenuPrivate *priv = menu->priv;
3299 guint *min_heights, *nat_heights;
3300 guint vertical_padding, border_width;
3302 gint min_height, nat_height;
3304 gtk_widget_style_get (widget, "vertical-padding", &vertical_padding, NULL);
3305 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3306 get_menu_padding (widget, &padding);
3308 min_height = nat_height = (border_width + vertical_padding) * 2 +
3309 padding.top + padding.bottom;
3312 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3314 for (i = 0; i < n_heights; i++)
3316 min_height += min_heights[i];
3317 nat_height += nat_heights[i];
3320 if (priv->have_position)
3322 GdkScreen *screen = gtk_widget_get_screen (priv->toplevel);
3323 GdkRectangle monitor;
3325 gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
3327 if (priv->position_y + min_height > monitor.y + monitor.height)
3328 min_height = monitor.y + monitor.height - priv->position_y;
3330 if (priv->position_y + nat_height > monitor.y + monitor.height)
3331 nat_height = monitor.y + monitor.height - priv->position_y;
3333 if (priv->position_y < monitor.y)
3335 min_height -= monitor.y - priv->position_y;
3336 nat_height -= monitor.y - priv->position_y;
3341 *minimum_size = min_height;
3344 *natural_size = nat_height;
3346 g_free (min_heights);
3347 g_free (nat_heights);
3353 gtk_menu_button_scroll (GtkMenu *menu,
3354 GdkEventButton *event)
3356 GtkMenuPrivate *priv = menu->priv;
3358 if (priv->upper_arrow_prelight || priv->lower_arrow_prelight)
3360 gboolean touchscreen_mode;
3362 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3363 "gtk-touchscreen-mode", &touchscreen_mode,
3366 if (touchscreen_mode)
3367 gtk_menu_handle_scrolling (menu,
3368 event->x_root, event->y_root,
3369 event->type == GDK_BUTTON_PRESS,
3379 pointer_in_menu_window (GtkWidget *widget,
3383 GtkMenu *menu = GTK_MENU (widget);
3384 GtkMenuPrivate *priv = menu->priv;
3385 GtkAllocation allocation;
3387 if (gtk_widget_get_mapped (priv->toplevel))
3389 GtkMenuShell *menu_shell;
3390 gint window_x, window_y;
3392 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
3393 &window_x, &window_y);
3395 gtk_widget_get_allocation (widget, &allocation);
3396 if (x_root >= window_x && x_root < window_x + allocation.width &&
3397 y_root >= window_y && y_root < window_y + allocation.height)
3400 menu_shell = GTK_MENU_SHELL (widget);
3402 if (GTK_IS_MENU (menu_shell->priv->parent_menu_shell))
3403 return pointer_in_menu_window (menu_shell->priv->parent_menu_shell,
3411 gtk_menu_button_press (GtkWidget *widget,
3412 GdkEventButton *event)
3414 if (event->type != GDK_BUTTON_PRESS)
3417 /* Don't pass down to menu shell for presses over scroll arrows
3419 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3422 /* Don't pass down to menu shell if a non-menuitem part of the menu
3423 * was clicked. The check for the event_widget being a GtkMenuShell
3424 * works because we have the pointer grabbed on menu_shell->window
3425 * with owner_events=TRUE, so all events that are either outside
3426 * the menu or on its border are delivered relative to
3427 * menu_shell->window.
3429 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3430 pointer_in_menu_window (widget, event->x_root, event->y_root))
3433 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3437 gtk_menu_button_release (GtkWidget *widget,
3438 GdkEventButton *event)
3440 GtkMenuPrivate *priv = GTK_MENU (widget)->priv;
3442 if (priv->ignore_button_release)
3444 priv->ignore_button_release = FALSE;
3448 if (event->type != GDK_BUTTON_RELEASE)
3451 /* Don't pass down to menu shell for releases over scroll arrows
3453 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3456 /* Don't pass down to menu shell if a non-menuitem part of the menu
3457 * was clicked (see comment in button_press()).
3459 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3460 pointer_in_menu_window (widget, event->x_root, event->y_root))
3462 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3463 * bail out early here so it is in a consistent state for the
3464 * next button_press/button_release in GtkMenuShell.
3467 if (GTK_MENU_SHELL (widget)->priv->active)
3468 GTK_MENU_SHELL (widget)->priv->button = 0;
3473 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3476 static const gchar *
3477 get_accel_path (GtkWidget *menu_item,
3482 GClosure *accel_closure;
3483 GtkAccelGroup *accel_group;
3485 path = _gtk_widget_get_accel_path (menu_item, locked);
3488 path = GTK_MENU_ITEM (menu_item)->priv->accel_path;
3494 label = gtk_bin_get_child (GTK_BIN (menu_item));
3496 if (GTK_IS_ACCEL_LABEL (label))
3498 g_object_get (label,
3499 "accel-closure", &accel_closure,
3503 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3505 *locked = gtk_accel_group_get_is_locked (accel_group);
3515 gtk_menu_key_press (GtkWidget *widget,
3518 GtkMenuShell *menu_shell;
3520 gboolean delete = FALSE;
3521 gboolean can_change_accels;
3522 gchar *accel = NULL;
3523 guint accel_key, accel_mods;
3524 GdkModifierType consumed_modifiers;
3525 GdkDisplay *display;
3527 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3528 g_return_val_if_fail (event != NULL, FALSE);
3530 menu_shell = GTK_MENU_SHELL (widget);
3531 menu = GTK_MENU (widget);
3533 gtk_menu_stop_navigating_submenu (menu);
3535 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3538 display = gtk_widget_get_display (widget);
3540 g_object_get (gtk_widget_get_settings (widget),
3541 "gtk-menu-bar-accel", &accel,
3542 "gtk-can-change-accels", &can_change_accels,
3545 if (accel && *accel)
3548 GdkModifierType mods = 0;
3550 gtk_accelerator_parse (accel, &keyval, &mods);
3553 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3555 /* FIXME this is wrong, needs to be in the global accel resolution
3556 * thing, to properly consider i18n etc., but that probably requires
3557 * AccelGroup changes etc.
3559 if (event->keyval == keyval && (mods & event->state) == mods)
3561 gtk_menu_shell_cancel (menu_shell);
3569 switch (event->keyval)
3571 case GDK_KEY_Delete:
3572 case GDK_KEY_KP_Delete:
3573 case GDK_KEY_BackSpace:
3580 /* Figure out what modifiers went into determining the key symbol */
3581 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3582 event->hardware_keycode,
3583 event->state, event->group,
3584 NULL, NULL, NULL, &consumed_modifiers);
3586 accel_key = gdk_keyval_to_lower (event->keyval);
3587 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3589 /* If lowercasing affects the keysym, then we need to include SHIFT
3590 * in the modifiers, we re-uppercase when we match against the keyval,
3591 * but display and save in caseless form.
3593 if (accel_key != event->keyval)
3594 accel_mods |= GDK_SHIFT_MASK;
3596 /* Modify the accelerators */
3597 if (can_change_accels &&
3598 menu_shell->priv->active_menu_item &&
3599 gtk_bin_get_child (GTK_BIN (menu_shell->priv->active_menu_item)) && /* no separators */
3600 GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu == NULL && /* no submenus */
3601 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3603 GtkWidget *menu_item = menu_shell->priv->active_menu_item;
3604 gboolean locked, replace_accels = TRUE;
3607 path = get_accel_path (menu_item, &locked);
3608 if (!path || locked)
3610 /* Can't change accelerators on menu_items without paths
3611 * (basically, those items are accelerator-locked).
3613 gtk_widget_error_bell (widget);
3619 /* For the keys that act to delete the current setting,
3620 * we delete the current setting if there is one, otherwise,
3621 * we set the key as the accelerator.
3627 if (gtk_accel_map_lookup_entry (path, &key) &&
3628 (key.accel_key || key.accel_mods))
3634 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3638 /* We failed, probably because this key is in use
3639 * and locked already.
3641 gtk_widget_error_bell (widget);
3650 check_threshold (GtkWidget *widget,
3659 ABS (start_x - x) > THRESHOLD ||
3660 ABS (start_y - y) > THRESHOLD;
3664 definitely_within_item (GtkWidget *widget,
3668 GdkWindow *window = GTK_MENU_ITEM (widget)->priv->event_window;
3671 w = gdk_window_get_width (window);
3672 h = gdk_window_get_height (window);
3675 check_threshold (widget, 0, 0, x, y) &&
3676 check_threshold (widget, w - 1, 0, x, y) &&
3677 check_threshold (widget, w - 1, h - 1, x, y) &&
3678 check_threshold (widget, 0, h - 1, x, y);
3682 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3684 GtkMenuPrivate *priv = menu->priv;
3686 return priv->navigation_height && priv->navigation_width;
3690 gtk_menu_motion_notify (GtkWidget *widget,
3691 GdkEventMotion *event)
3693 GtkWidget *menu_item;
3695 GtkMenuShell *menu_shell;
3698 gboolean need_enter;
3700 if (GTK_IS_MENU (widget))
3702 GtkMenuPrivate *priv = GTK_MENU(widget)->priv;
3704 if (priv->ignore_button_release)
3705 priv->ignore_button_release = FALSE;
3707 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3711 /* We received the event for one of two reasons:
3713 * a) We are the active menu, and did gtk_grab_add()
3714 * b) The widget is a child of ours, and the event was propagated
3716 * Since for computation of navigation regions, we want the menu which
3717 * is the parent of the menu item, for a), we need to find that menu,
3718 * which may be different from 'widget'.
3720 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3721 parent = gtk_widget_get_parent (menu_item);
3722 if (!GTK_IS_MENU_ITEM (menu_item) ||
3723 !GTK_IS_MENU (parent))
3726 menu_shell = GTK_MENU_SHELL (parent);
3727 menu = GTK_MENU (menu_shell);
3729 if (definitely_within_item (menu_item, event->x, event->y))
3730 menu_shell->priv->activate_time = 0;
3732 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->priv->ignore_enter);
3734 /* Check to see if we are within an active submenu's navigation region
3736 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3739 /* Make sure we pop down if we enter a non-selectable menu item, so we
3740 * don't show a submenu when the cursor is outside the stay-up triangle.
3742 if (!_gtk_menu_item_is_selectable (menu_item))
3744 /* We really want to deselect, but this gives the menushell code
3745 * a chance to do some bookkeeping about the menuitem.
3747 gtk_menu_shell_select_item (menu_shell, menu_item);
3753 /* The menu is now sensitive to enter events on its items, but
3754 * was previously sensitive. So we fake an enter event.
3756 menu_shell->priv->ignore_enter = FALSE;
3758 if (event->x >= 0 && event->x < gdk_window_get_width (event->window) &&
3759 event->y >= 0 && event->y < gdk_window_get_height (event->window))
3761 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3764 send_event->crossing.window = g_object_ref (event->window);
3765 send_event->crossing.time = event->time;
3766 send_event->crossing.send_event = TRUE;
3767 send_event->crossing.x_root = event->x_root;
3768 send_event->crossing.y_root = event->y_root;
3769 send_event->crossing.x = event->x;
3770 send_event->crossing.y = event->y;
3771 send_event->crossing.state = event->state;
3772 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3774 /* We send the event to 'widget', the currently active menu,
3775 * instead of 'menu', the menu that the pointer is in. This
3776 * will ensure that the event will be ignored unless the
3777 * menuitem is a child of the active menu or some parent
3778 * menu of the active menu.
3780 result = gtk_widget_event (widget, send_event);
3781 gdk_event_free (send_event);
3791 get_double_arrows (GtkMenu *menu)
3793 GtkMenuPrivate *priv = menu->priv;
3794 gboolean double_arrows;
3795 GtkArrowPlacement arrow_placement;
3797 gtk_widget_style_get (GTK_WIDGET (menu),
3798 "double-arrows", &double_arrows,
3799 "arrow-placement", &arrow_placement,
3802 if (arrow_placement != GTK_ARROWS_BOTH)
3805 return double_arrows || (priv->initially_pushed_in &&
3806 priv->scroll_offset != 0);
3810 gtk_menu_scroll_by (GtkMenu *menu,
3813 GtkMenuPrivate *priv = menu->priv;
3814 GtkBorder arrow_border;
3818 gboolean double_arrows;
3820 widget = GTK_WIDGET (menu);
3821 offset = priv->scroll_offset + step;
3823 get_arrows_border (menu, &arrow_border);
3825 double_arrows = get_double_arrows (menu);
3827 /* If we scroll upward and the non-visible top part
3828 * is smaller than the scroll arrow it would be
3829 * pretty stupid to show the arrow and taking more
3830 * screen space than just scrolling to the top.
3833 if ((step < 0) && (offset < arrow_border.top))
3836 /* Don't scroll over the top if we weren't before: */
3837 if ((priv->scroll_offset >= 0) && (offset < 0))
3840 view_height = gdk_window_get_height (gtk_widget_get_window (widget));
3842 if (priv->scroll_offset == 0 &&
3843 view_height >= priv->requested_height)
3846 /* Don't scroll past the bottom if we weren't before: */
3847 if (priv->scroll_offset > 0)
3848 view_height -= arrow_border.top;
3850 /* When both arrows are always shown,
3851 * reduce view height even more.
3854 view_height -= arrow_border.bottom;
3856 if ((priv->scroll_offset + view_height <= priv->requested_height) &&
3857 (offset + view_height > priv->requested_height))
3858 offset = priv->requested_height - view_height;
3860 if (offset != priv->scroll_offset)
3861 gtk_menu_scroll_to (menu, offset);
3865 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3866 gboolean touchscreen_mode)
3868 GtkMenuPrivate *priv = menu->priv;
3869 gboolean upper_visible;
3870 gboolean lower_visible;
3872 upper_visible = priv->upper_arrow_visible;
3873 lower_visible = priv->lower_arrow_visible;
3875 gtk_menu_scroll_by (menu, priv->scroll_step);
3877 if (touchscreen_mode &&
3878 (upper_visible != priv->upper_arrow_visible ||
3879 lower_visible != priv->lower_arrow_visible))
3881 /* We are about to hide a scroll arrow while the mouse is pressed,
3882 * this would cause the uncovered menu item to be activated on button
3883 * release. Therefore we need to ignore button release here
3885 GTK_MENU_SHELL (menu)->priv->ignore_enter = TRUE;
3886 priv->ignore_button_release = TRUE;
3891 gtk_menu_scroll_timeout (gpointer data)
3894 gboolean touchscreen_mode;
3896 menu = GTK_MENU (data);
3898 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3899 "gtk-touchscreen-mode", &touchscreen_mode,
3902 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3908 gtk_menu_scroll_timeout_initial (gpointer data)
3912 gboolean touchscreen_mode;
3914 menu = GTK_MENU (data);
3916 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3917 "gtk-timeout-repeat", &timeout,
3918 "gtk-touchscreen-mode", &touchscreen_mode,
3921 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3923 gtk_menu_remove_scroll_timeout (menu);
3925 menu->priv->scroll_timeout =
3926 gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout, menu);
3932 gtk_menu_start_scrolling (GtkMenu *menu)
3935 gboolean touchscreen_mode;
3937 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3938 "gtk-timeout-repeat", &timeout,
3939 "gtk-touchscreen-mode", &touchscreen_mode,
3942 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3944 menu->priv->scroll_timeout =
3945 gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout_initial, menu);
3949 gtk_menu_scroll (GtkWidget *widget,
3950 GdkEventScroll *event)
3952 GtkMenu *menu = GTK_MENU (widget);
3954 switch (event->direction)
3956 case GDK_SCROLL_RIGHT:
3957 case GDK_SCROLL_DOWN:
3958 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3960 case GDK_SCROLL_LEFT:
3962 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3970 get_arrows_sensitive_area (GtkMenu *menu,
3971 GdkRectangle *upper,
3972 GdkRectangle *lower)
3974 GtkArrowPlacement arrow_placement;
3975 GtkWidget *widget = GTK_WIDGET (menu);
3979 guint vertical_padding;
3981 gint scroll_arrow_height;
3984 window = gtk_widget_get_window (widget);
3985 width = gdk_window_get_width (window);
3986 height = gdk_window_get_height (window);
3988 gtk_widget_style_get (widget,
3989 "vertical-padding", &vertical_padding,
3990 "scroll-arrow-vlength", &scroll_arrow_height,
3991 "arrow-placement", &arrow_placement,
3994 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) + vertical_padding;
3995 get_menu_padding (widget, &padding);
3997 gdk_window_get_position (window, &win_x, &win_y);
3999 switch (arrow_placement)
4001 case GTK_ARROWS_BOTH:
4006 upper->width = width;
4007 upper->height = scroll_arrow_height + border + padding.top;
4013 lower->y = win_y + height - border - padding.bottom - scroll_arrow_height;
4014 lower->width = width;
4015 lower->height = scroll_arrow_height + border + padding.bottom;
4019 case GTK_ARROWS_START:
4024 upper->width = width / 2;
4025 upper->height = scroll_arrow_height + border + padding.top;
4030 lower->x = win_x + width / 2;
4032 lower->width = width / 2;
4033 lower->height = scroll_arrow_height + border + padding.bottom;
4037 case GTK_ARROWS_END:
4041 upper->y = win_y + height - border - scroll_arrow_height;
4042 upper->width = width / 2;
4043 upper->height = scroll_arrow_height + border + padding.top;
4048 lower->x = win_x + width / 2;
4049 lower->y = win_y + height - border - scroll_arrow_height;
4050 lower->width = width / 2;
4051 lower->height = scroll_arrow_height + border + padding.bottom;
4059 gtk_menu_handle_scrolling (GtkMenu *menu,
4065 GtkMenuPrivate *priv = menu->priv;
4066 GtkMenuShell *menu_shell;
4069 gboolean scroll_fast = FALSE;
4071 gboolean touchscreen_mode;
4073 menu_shell = GTK_MENU_SHELL (menu);
4075 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4076 "gtk-touchscreen-mode", &touchscreen_mode,
4079 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
4084 /* upper arrow handling */
4086 get_arrows_sensitive_area (menu, &rect, NULL);
4089 if (priv->upper_arrow_visible && !priv->tearoff_active &&
4090 (x >= rect.x) && (x < rect.x + rect.width) &&
4091 (y >= rect.y) && (y < rect.y + rect.height))
4096 if (touchscreen_mode)
4097 priv->upper_arrow_prelight = in_arrow;
4099 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4101 gboolean arrow_pressed = FALSE;
4103 if (priv->upper_arrow_visible && !priv->tearoff_active)
4105 if (touchscreen_mode)
4107 if (enter && priv->upper_arrow_prelight)
4109 if (priv->scroll_timeout == 0)
4111 /* Deselect the active item so that
4112 * any submenus are popped down
4114 gtk_menu_shell_deselect (menu_shell);
4116 gtk_menu_remove_scroll_timeout (menu);
4117 priv->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
4121 /* Only do stuff on click. */
4122 gtk_menu_start_scrolling (menu);
4123 arrow_pressed = TRUE;
4128 arrow_pressed = TRUE;
4133 gtk_menu_stop_scrolling (menu);
4136 else /* !touchscreen_mode */
4138 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
4140 if (enter && in_arrow &&
4141 (!priv->upper_arrow_prelight ||
4142 priv->scroll_fast != scroll_fast))
4144 priv->upper_arrow_prelight = TRUE;
4145 priv->scroll_fast = scroll_fast;
4147 /* Deselect the active item so that
4148 * any submenus are popped down
4150 gtk_menu_shell_deselect (menu_shell);
4152 gtk_menu_remove_scroll_timeout (menu);
4153 priv->scroll_step = scroll_fast
4154 ? -MENU_SCROLL_STEP2
4155 : -MENU_SCROLL_STEP1;
4157 priv->scroll_timeout =
4158 gdk_threads_add_timeout (scroll_fast
4159 ? MENU_SCROLL_TIMEOUT2
4160 : MENU_SCROLL_TIMEOUT1,
4161 gtk_menu_scroll_timeout, menu);
4163 else if (!enter && !in_arrow && priv->upper_arrow_prelight)
4165 gtk_menu_stop_scrolling (menu);
4170 /* gtk_menu_start_scrolling() might have hit the top of the
4171 * menu, so check if the button isn't insensitive before
4172 * changing it to something else.
4174 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4176 GtkStateFlags arrow_state = 0;
4179 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4181 if (priv->upper_arrow_prelight)
4182 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4184 if (arrow_state != priv->upper_arrow_state)
4186 priv->upper_arrow_state = arrow_state;
4188 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4194 /* lower arrow handling */
4196 get_arrows_sensitive_area (menu, NULL, &rect);
4199 if (priv->lower_arrow_visible && !priv->tearoff_active &&
4200 (x >= rect.x) && (x < rect.x + rect.width) &&
4201 (y >= rect.y) && (y < rect.y + rect.height))
4206 if (touchscreen_mode)
4207 priv->lower_arrow_prelight = in_arrow;
4209 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4211 gboolean arrow_pressed = FALSE;
4213 if (priv->lower_arrow_visible && !priv->tearoff_active)
4215 if (touchscreen_mode)
4217 if (enter && priv->lower_arrow_prelight)
4219 if (priv->scroll_timeout == 0)
4221 /* Deselect the active item so that
4222 * any submenus are popped down
4224 gtk_menu_shell_deselect (menu_shell);
4226 gtk_menu_remove_scroll_timeout (menu);
4227 priv->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4231 /* Only do stuff on click. */
4232 gtk_menu_start_scrolling (menu);
4233 arrow_pressed = TRUE;
4238 arrow_pressed = TRUE;
4243 gtk_menu_stop_scrolling (menu);
4246 else /* !touchscreen_mode */
4248 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4250 if (enter && in_arrow &&
4251 (!priv->lower_arrow_prelight ||
4252 priv->scroll_fast != scroll_fast))
4254 priv->lower_arrow_prelight = TRUE;
4255 priv->scroll_fast = scroll_fast;
4257 /* Deselect the active item so that
4258 * any submenus are popped down
4260 gtk_menu_shell_deselect (menu_shell);
4262 gtk_menu_remove_scroll_timeout (menu);
4263 priv->scroll_step = scroll_fast
4265 : MENU_SCROLL_STEP1;
4267 priv->scroll_timeout =
4268 gdk_threads_add_timeout (scroll_fast
4269 ? MENU_SCROLL_TIMEOUT2
4270 : MENU_SCROLL_TIMEOUT1,
4271 gtk_menu_scroll_timeout, menu);
4273 else if (!enter && !in_arrow && priv->lower_arrow_prelight)
4275 gtk_menu_stop_scrolling (menu);
4280 /* gtk_menu_start_scrolling() might have hit the bottom of the
4281 * menu, so check if the button isn't insensitive before
4282 * changing it to something else.
4284 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4286 GtkStateFlags arrow_state = 0;
4289 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4291 if (priv->lower_arrow_prelight)
4292 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4294 if (arrow_state != priv->lower_arrow_state)
4296 priv->lower_arrow_state = arrow_state;
4298 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4306 gtk_menu_enter_notify (GtkWidget *widget,
4307 GdkEventCrossing *event)
4309 GtkWidget *menu_item;
4311 gboolean touchscreen_mode;
4313 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4314 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4315 event->mode == GDK_CROSSING_STATE_CHANGED)
4318 g_object_get (gtk_widget_get_settings (widget),
4319 "gtk-touchscreen-mode", &touchscreen_mode,
4322 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4323 if (GTK_IS_MENU (widget))
4325 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4327 if (!menu_shell->priv->ignore_enter)
4328 gtk_menu_handle_scrolling (GTK_MENU (widget),
4329 event->x_root, event->y_root, TRUE, TRUE);
4332 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4334 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4336 if (GTK_IS_MENU (menu))
4338 GtkMenuPrivate *priv = (GTK_MENU (menu))->priv;
4339 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4341 if (priv->seen_item_enter)
4343 /* This is the second enter we see for an item
4344 * on this menu. This means a release should always
4347 menu_shell->priv->activate_time = 0;
4349 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4350 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4352 if (definitely_within_item (menu_item, event->x, event->y))
4354 /* This is an actual user-enter (ie. not a pop-under)
4355 * In this case, the user must either have entered
4356 * sufficiently far enough into the item, or he must move
4357 * far enough away from the enter point. (see
4358 * gtk_menu_motion_notify())
4360 menu_shell->priv->activate_time = 0;
4364 priv->seen_item_enter = TRUE;
4368 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4369 * will not correspond to the event widget's parent. Check to see
4370 * if we are in the parent's navigation region.
4372 parent = gtk_widget_get_parent (menu_item);
4373 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4374 gtk_menu_navigating_submenu (GTK_MENU (parent),
4375 event->x_root, event->y_root))
4378 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4382 gtk_menu_leave_notify (GtkWidget *widget,
4383 GdkEventCrossing *event)
4385 GtkMenuShell *menu_shell;
4387 GtkMenuItem *menu_item;
4388 GtkWidget *event_widget;
4390 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4391 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4392 event->mode == GDK_CROSSING_STATE_CHANGED)
4395 menu = GTK_MENU (widget);
4396 menu_shell = GTK_MENU_SHELL (widget);
4398 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4401 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4403 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4405 if (!GTK_IS_MENU_ITEM (event_widget))
4408 menu_item = GTK_MENU_ITEM (event_widget);
4410 /* Here we check to see if we're leaving an active menu item
4411 * with a submenu, in which case we enter submenu navigation mode.
4413 if (menu_shell->priv->active_menu_item != NULL
4414 && menu_item->priv->submenu != NULL
4415 && menu_item->priv->submenu_placement == GTK_LEFT_RIGHT)
4417 if (GTK_MENU_SHELL (menu_item->priv->submenu)->priv->active)
4419 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4422 else if (menu_item == GTK_MENU_ITEM (menu_shell->priv->active_menu_item))
4424 /* We are leaving an active menu item with nonactive submenu.
4425 * Deselect it so we don't surprise the user with by popping
4426 * up a submenu _after_ he left the item.
4428 gtk_menu_shell_deselect (menu_shell);
4433 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4437 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4439 GtkMenuPrivate *priv = menu->priv;
4441 priv->navigation_x = 0;
4442 priv->navigation_y = 0;
4443 priv->navigation_width = 0;
4444 priv->navigation_height = 0;
4446 if (priv->navigation_timeout)
4448 g_source_remove (priv->navigation_timeout);
4449 priv->navigation_timeout = 0;
4453 /* When the timeout is elapsed, the navigation region is destroyed
4454 * and the menuitem under the pointer (if any) is selected.
4457 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4459 GtkMenuPopdownData *popdown_data = user_data;
4460 GtkMenu *menu = popdown_data->menu;
4461 GtkMenuPrivate *priv = menu->priv;
4462 GdkWindow *child_window;
4464 gtk_menu_stop_navigating_submenu (menu);
4466 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4468 child_window = gdk_window_get_device_position (priv->bin_window,
4469 popdown_data->device,
4474 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4476 send_event->crossing.window = g_object_ref (child_window);
4477 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4478 send_event->crossing.send_event = TRUE;
4479 gdk_event_set_device (send_event, popdown_data->device);
4481 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4483 gdk_event_free (send_event);
4491 gtk_menu_navigating_submenu (GtkMenu *menu,
4495 GtkMenuPrivate *priv = menu->priv;
4498 if (!gtk_menu_has_navigation_triangle (menu))
4501 width = priv->navigation_width;
4502 height = priv->navigation_height;
4504 /* Check if x/y are in the triangle spanned by the navigation parameters */
4506 /* 1) Move the coordinates so the triangle starts at 0,0 */
4507 event_x -= priv->navigation_x;
4508 event_y -= priv->navigation_y;
4510 /* 2) Ensure both legs move along the positive axis */
4522 /* 3) Check that the given coordinate is inside the triangle. The formula
4523 * is a transformed form of this formula: x/w + y/h <= 1
4525 if (event_x >= 0 && event_y >= 0 &&
4526 event_x * height + event_y * width <= width * height)
4532 gtk_menu_stop_navigating_submenu (menu);
4538 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4539 GtkMenuItem *menu_item,
4540 GdkEventCrossing *event)
4542 GtkMenuPrivate *priv = menu->priv;
4543 gint submenu_left = 0;
4544 gint submenu_right = 0;
4545 gint submenu_top = 0;
4546 gint submenu_bottom = 0;
4548 GtkWidget *event_widget;
4549 GtkMenuPopdownData *popdown_data;
4552 g_return_if_fail (menu_item->priv->submenu != NULL);
4553 g_return_if_fail (event != NULL);
4555 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4557 window = gtk_widget_get_window (menu_item->priv->submenu);
4558 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4560 submenu_right = submenu_left + gdk_window_get_width (window);
4561 submenu_bottom = submenu_top + gdk_window_get_height (window);
4563 width = gdk_window_get_width (gtk_widget_get_window (event_widget));
4565 if (event->x >= 0 && event->x < width)
4569 gtk_menu_stop_navigating_submenu (menu);
4571 /* The navigation region is the triangle closest to the x/y
4572 * location of the rectangle. This is why the width or height
4575 if (menu_item->priv->submenu_direction == GTK_DIRECTION_RIGHT)
4578 priv->navigation_x = submenu_left;
4579 priv->navigation_width = event->x_root - submenu_left;
4584 priv->navigation_x = submenu_right;
4585 priv->navigation_width = event->x_root - submenu_right;
4591 priv->navigation_y = event->y_root;
4592 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4594 if (priv->navigation_height >= 0)
4600 priv->navigation_y = event->y_root;
4601 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4603 if (priv->navigation_height <= 0)
4607 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4608 "gtk-menu-popdown-delay", &popdown_delay,
4611 popdown_data = g_new (GtkMenuPopdownData, 1);
4612 popdown_data->menu = menu;
4613 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4615 priv->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4617 gtk_menu_stop_navigating_submenu_cb,
4619 (GDestroyNotify) g_free);
4624 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4628 g_return_if_fail (GTK_IS_MENU (menu_shell));
4630 parent = menu_shell->priv->parent_menu_shell;
4632 menu_shell->priv->activate_time = 0;
4633 gtk_menu_popdown (GTK_MENU (menu_shell));
4636 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4640 gtk_menu_position (GtkMenu *menu,
4641 gboolean set_scroll_offset)
4643 GtkMenuPrivate *priv = menu->priv;
4645 GtkRequisition requisition;
4650 GdkScreen *pointer_screen;
4651 GdkRectangle monitor;
4654 widget = GTK_WIDGET (menu);
4656 screen = gtk_widget_get_screen (widget);
4657 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4658 gdk_device_get_position (pointer, &pointer_screen, &x, &y);
4660 /* Realize so we have the proper width and heigh to figure out
4661 * the right place to popup the menu.
4663 gtk_widget_realize (priv->toplevel);
4664 requisition.width = gtk_widget_get_allocated_width (widget);
4665 requisition.height = gtk_widget_get_allocated_height (widget);
4667 if (pointer_screen != screen)
4669 /* Pointer is on a different screen; roughly center the
4670 * menu on the screen. If someone was using multiscreen
4671 * + Xinerama together they'd probably want something
4672 * fancier; but that is likely to be vanishingly rare.
4674 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4675 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4678 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4679 priv->initially_pushed_in = FALSE;
4681 /* Set the type hint here to allow custom position functions
4682 * to set a different hint
4684 if (!gtk_widget_get_visible (priv->toplevel))
4685 gtk_window_set_type_hint (GTK_WINDOW (priv->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4687 if (priv->position_func)
4689 (* priv->position_func) (menu, &x, &y, &priv->initially_pushed_in,
4690 priv->position_func_data);
4692 if (priv->monitor_num < 0)
4693 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4695 gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
4699 gint space_left, space_right, space_above, space_below;
4703 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4705 get_menu_padding (widget, &padding);
4707 /* The placement of popup menus horizontally works like this (with
4708 * RTL in parentheses)
4710 * - If there is enough room to the right (left) of the mouse cursor,
4711 * position the menu there.
4713 * - Otherwise, if if there is enough room to the left (right) of the
4714 * mouse cursor, position the menu there.
4716 * - Otherwise if the menu is smaller than the monitor, position it
4717 * on the side of the mouse cursor that has the most space available
4719 * - Otherwise (if there is simply not enough room for the menu on the
4720 * monitor), position it as far left (right) as possible.
4722 * Positioning in the vertical direction is similar: first try below
4723 * mouse cursor, then above.
4725 gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
4727 space_left = x - monitor.x;
4728 space_right = monitor.x + monitor.width - x - 1;
4729 space_above = y - monitor.y;
4730 space_below = monitor.y + monitor.height - y - 1;
4732 /* Position horizontally. */
4734 /* the amount of space we need to position the menu.
4735 * Note the menu is offset "thickness" pixels
4737 needed_width = requisition.width - padding.left;
4739 if (needed_width <= space_left ||
4740 needed_width <= space_right)
4742 if ((rtl && needed_width <= space_left) ||
4743 (!rtl && needed_width > space_right))
4746 x = x + padding.left - requisition.width + 1;
4750 /* position right */
4751 x = x - padding.right;
4754 /* x is clamped on-screen further down */
4756 else if (requisition.width <= monitor.width)
4758 /* the menu is too big to fit on either side of the mouse
4759 * cursor, but smaller than the monitor. Position it on
4760 * the side that has the most space
4762 if (space_left > space_right)
4770 x = monitor.x + monitor.width - requisition.width;
4773 else /* menu is simply too big for the monitor */
4778 x = monitor.x + monitor.width - requisition.width;
4787 /* Position vertically.
4788 * The algorithm is the same as above, but simpler
4789 * because we don't have to take RTL into account.
4791 needed_height = requisition.height - padding.top;
4793 if (needed_height <= space_above ||
4794 needed_height <= space_below)
4796 if (needed_height <= space_below)
4797 y = y - padding.top;
4799 y = y + padding.bottom - requisition.height + 1;
4801 y = CLAMP (y, monitor.y,
4802 monitor.y + monitor.height - requisition.height);
4804 else if (needed_height > space_below && needed_height > space_above)
4806 if (space_below >= space_above)
4807 y = monitor.y + monitor.height - requisition.height;
4819 if (priv->initially_pushed_in)
4821 menu_height = requisition.height;
4823 if (y + menu_height > monitor.y + monitor.height)
4825 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4826 y = (monitor.y + monitor.height) - menu_height;
4831 scroll_offset += monitor.y - y;
4836 /* FIXME: should this be done in the various position_funcs ? */
4837 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4839 if (GTK_MENU_SHELL (menu)->priv->active)
4841 priv->have_position = TRUE;
4842 priv->position_x = x;
4843 priv->position_y = y;
4846 if (y + requisition.height > monitor.y + monitor.height)
4847 requisition.height = (monitor.y + monitor.height) - y;
4851 scroll_offset += monitor.y - y;
4852 requisition.height -= monitor.y - y;
4856 if (scroll_offset > 0)
4858 GtkBorder arrow_border;
4860 get_arrows_border (menu, &arrow_border);
4861 scroll_offset += arrow_border.top;
4864 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->priv->active ? priv->toplevel : priv->tearoff_window),
4867 if (!GTK_MENU_SHELL (menu)->priv->active)
4869 gtk_window_resize (GTK_WINDOW (priv->tearoff_window),
4870 requisition.width, requisition.height);
4873 if (set_scroll_offset)
4874 priv->scroll_offset = scroll_offset;
4878 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4880 GtkMenuPrivate *priv = menu->priv;
4882 if (priv->scroll_timeout)
4884 g_source_remove (priv->scroll_timeout);
4885 priv->scroll_timeout = 0;
4890 gtk_menu_stop_scrolling (GtkMenu *menu)
4892 GtkMenuPrivate *priv = menu->priv;
4893 gboolean touchscreen_mode;
4895 gtk_menu_remove_scroll_timeout (menu);
4897 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4898 "gtk-touchscreen-mode", &touchscreen_mode,
4901 if (!touchscreen_mode)
4903 priv->upper_arrow_prelight = FALSE;
4904 priv->lower_arrow_prelight = FALSE;
4909 gtk_menu_scroll_to (GtkMenu *menu,
4912 GtkMenuPrivate *priv = menu->priv;
4913 GtkAllocation allocation;
4914 GtkBorder arrow_border, padding;
4917 gint view_width, view_height;
4920 guint vertical_padding;
4921 guint horizontal_padding;
4922 gboolean double_arrows;
4924 widget = GTK_WIDGET (menu);
4926 if (priv->tearoff_active && priv->tearoff_adjustment)
4927 gtk_adjustment_set_value (priv->tearoff_adjustment, offset);
4929 /* Move/resize the viewport according to arrows: */
4930 gtk_widget_get_allocation (widget, &allocation);
4931 view_width = allocation.width;
4932 view_height = allocation.height;
4934 gtk_widget_style_get (GTK_WIDGET (menu),
4935 "vertical-padding", &vertical_padding,
4936 "horizontal-padding", &horizontal_padding,
4939 get_menu_padding (widget, &padding);
4940 double_arrows = get_double_arrows (menu);
4942 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4944 view_width -= (2 * (border_width + horizontal_padding)) + padding.left + padding.right;
4945 view_height -= (2 * (border_width + vertical_padding)) + padding.top + padding.bottom;
4946 menu_height = priv->requested_height - (2 * (border_width + vertical_padding)) -
4947 padding.top - padding.bottom;
4949 x = border_width + padding.left + horizontal_padding;
4950 y = border_width + padding.top + vertical_padding;
4952 if (double_arrows && !priv->tearoff_active)
4954 if (view_height < menu_height ||
4955 (offset > 0 && priv->scroll_offset > 0) ||
4956 (offset < 0 && priv->scroll_offset < 0))
4958 GtkStateFlags upper_arrow_previous_state = priv->upper_arrow_state;
4959 GtkStateFlags lower_arrow_previous_state = priv->lower_arrow_state;
4961 if (!priv->upper_arrow_visible || !priv->lower_arrow_visible)
4962 gtk_widget_queue_draw (GTK_WIDGET (menu));
4964 priv->upper_arrow_visible = priv->lower_arrow_visible = TRUE;
4966 get_arrows_border (menu, &arrow_border);
4967 y += arrow_border.top;
4968 view_height -= arrow_border.top;
4969 view_height -= arrow_border.bottom;
4972 priv->upper_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4975 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4977 if (priv->upper_arrow_prelight)
4978 priv->upper_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4980 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4983 if (offset >= menu_height - view_height)
4984 priv->lower_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4987 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4989 if (priv->lower_arrow_prelight)
4990 priv->lower_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4992 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4995 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4996 (priv->lower_arrow_state != lower_arrow_previous_state))
4997 gtk_widget_queue_draw (GTK_WIDGET (menu));
4999 if ((upper_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
5000 (priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
5002 /* At the upper border, possibly remove timeout */
5003 if (priv->scroll_step < 0)
5005 gtk_menu_stop_scrolling (menu);
5006 gtk_widget_queue_draw (GTK_WIDGET (menu));
5010 if ((lower_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
5011 (priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
5013 /* At the lower border, possibly remove timeout */
5014 if (priv->scroll_step > 0)
5016 gtk_menu_stop_scrolling (menu);
5017 gtk_widget_queue_draw (GTK_WIDGET (menu));
5021 else if (priv->upper_arrow_visible || priv->lower_arrow_visible)
5025 priv->upper_arrow_visible = priv->lower_arrow_visible = FALSE;
5026 priv->upper_arrow_prelight = priv->lower_arrow_prelight = FALSE;
5028 gtk_menu_stop_scrolling (menu);
5029 gtk_widget_queue_draw (GTK_WIDGET (menu));
5032 else if (!priv->tearoff_active)
5034 gboolean last_visible;
5036 last_visible = priv->upper_arrow_visible;
5037 priv->upper_arrow_visible = offset > 0;
5039 /* upper_arrow_visible may have changed, so requery the border */
5040 get_arrows_border (menu, &arrow_border);
5041 view_height -= arrow_border.top;
5043 if ((last_visible != priv->upper_arrow_visible) &&
5044 !priv->upper_arrow_visible)
5046 priv->upper_arrow_prelight = FALSE;
5048 /* If we hide the upper arrow, possibly remove timeout */
5049 if (priv->scroll_step < 0)
5051 gtk_menu_stop_scrolling (menu);
5052 gtk_widget_queue_draw (GTK_WIDGET (menu));
5056 last_visible = priv->lower_arrow_visible;
5057 priv->lower_arrow_visible = offset < menu_height - view_height;
5059 /* lower_arrow_visible may have changed, so requery the border */
5060 get_arrows_border (menu, &arrow_border);
5061 view_height -= arrow_border.bottom;
5063 if ((last_visible != priv->lower_arrow_visible) &&
5064 !priv->lower_arrow_visible)
5066 priv->lower_arrow_prelight = FALSE;
5068 /* If we hide the lower arrow, possibly remove timeout */
5069 if (priv->scroll_step > 0)
5071 gtk_menu_stop_scrolling (menu);
5072 gtk_widget_queue_draw (GTK_WIDGET (menu));
5076 y += arrow_border.top;
5079 /* Scroll the menu: */
5080 if (gtk_widget_get_realized (widget))
5081 gdk_window_move (priv->bin_window, 0, -offset);
5083 if (gtk_widget_get_realized (widget))
5084 gdk_window_move_resize (priv->view_window, x, y, view_width, view_height);
5086 priv->scroll_offset = offset;
5090 compute_child_offset (GtkMenu *menu,
5091 GtkWidget *menu_item,
5094 gboolean *is_last_child)
5096 GtkMenuPrivate *priv = menu->priv;
5097 gint item_top_attach;
5098 gint item_bottom_attach;
5099 gint child_offset = 0;
5102 get_effective_child_attach (menu_item, NULL, NULL,
5103 &item_top_attach, &item_bottom_attach);
5105 /* there is a possibility that we get called before _size_request,
5106 * so check the height table for safety.
5108 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
5111 /* when we have a row with only invisible children, its height will
5112 * be zero, so there's no need to check WIDGET_VISIBLE here
5114 for (i = 0; i < item_top_attach; i++)
5115 child_offset += priv->heights[i];
5118 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
5120 *offset = child_offset;
5122 *height = priv->heights[item_top_attach];
5128 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
5129 GtkWidget *menu_item)
5131 GtkMenu *menu = GTK_MENU (menu_shell);
5132 GtkMenuPrivate *priv = menu->priv;
5133 GtkWidget *widget = GTK_WIDGET (menu_shell);
5134 gint child_offset, child_height;
5138 gboolean last_child = 0;
5140 /* We need to check if the selected item fully visible.
5141 * If not we need to scroll the menu so that it becomes fully
5144 if (compute_child_offset (menu, menu_item,
5145 &child_offset, &child_height, &last_child))
5147 guint vertical_padding;
5148 gboolean double_arrows;
5151 y = priv->scroll_offset;
5152 height = gdk_window_get_height (gtk_widget_get_window (widget));
5154 gtk_widget_style_get (widget,
5155 "vertical-padding", &vertical_padding,
5158 double_arrows = get_double_arrows (menu);
5159 get_menu_padding (widget, &padding);
5161 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5162 padding.top + padding.bottom +
5163 2 * vertical_padding;
5164 if (child_offset < y)
5166 /* Ignore the enter event we might get if the pointer
5169 menu_shell->priv->ignore_enter = TRUE;
5170 gtk_menu_scroll_to (menu, child_offset);
5174 GtkBorder arrow_border;
5178 get_arrows_border (menu, &arrow_border);
5179 if (!priv->tearoff_active)
5180 arrow_height = arrow_border.top + arrow_border.bottom;
5182 if (child_offset + child_height > y + height - arrow_height)
5185 if ((!last_child && !priv->tearoff_active) || double_arrows)
5186 arrow_height += arrow_border.bottom;
5188 y = child_offset + child_height - height + arrow_height;
5189 if (((y > 0) && !priv->tearoff_active) || double_arrows)
5191 /* Need upper arrow */
5192 arrow_height += arrow_border.top;
5193 y = child_offset + child_height - height + arrow_height;
5195 /* Ignore the enter event we might get if the pointer
5198 menu_shell->priv->ignore_enter = TRUE;
5199 gtk_menu_scroll_to (menu, y);
5206 gtk_menu_select_item (GtkMenuShell *menu_shell,
5207 GtkWidget *menu_item)
5209 GtkMenu *menu = GTK_MENU (menu_shell);
5211 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5212 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5214 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5218 /* Reparent the menu, taking care of the refcounting
5220 * If unrealize is true we force a unrealize while reparenting the parent.
5221 * This can help eliminate flicker in some cases.
5223 * What happens is that when the menu is unrealized and then re-realized,
5224 * the allocations are as follows:
5226 * parent - 1x1 at (0,0)
5227 * child1 - 100x20 at (0,0)
5228 * child2 - 100x20 at (0,20)
5229 * child3 - 100x20 at (0,40)
5231 * That is, the parent is small but the children are full sized. Then,
5232 * when the queued_resize gets processed, the parent gets resized to
5235 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5236 * contains the following logic:
5238 * - if a move or resize operation on a window would change the clip
5239 * region on the children, then before the window is resized
5240 * the background for children is temporarily set to None, the
5241 * move/resize done, and the background for the children restored.
5243 * So, at the point where the parent is resized to final size, the
5244 * background for the children is temporarily None, and thus they
5245 * are not cleared to the background color and the previous background
5246 * (the image of the menu) is left in place.
5249 gtk_menu_reparent (GtkMenu *menu,
5250 GtkWidget *new_parent,
5253 GObject *object = G_OBJECT (menu);
5254 GtkWidget *widget = GTK_WIDGET (menu);
5255 gboolean was_floating = g_object_is_floating (object);
5257 g_object_ref_sink (object);
5261 g_object_ref (object);
5262 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5263 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5264 g_object_unref (object);
5267 gtk_widget_reparent (widget, new_parent);
5270 g_object_force_floating (object);
5272 g_object_unref (object);
5276 gtk_menu_show_all (GtkWidget *widget)
5278 /* Show children, but not self. */
5279 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5283 * gtk_menu_set_screen:
5285 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5286 * determined by the widget the menu is attached to
5288 * Sets the #GdkScreen on which the menu will be displayed.
5293 gtk_menu_set_screen (GtkMenu *menu,
5296 g_return_if_fail (GTK_IS_MENU (menu));
5297 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
5299 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5303 menu_change_screen (menu, screen);
5307 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5309 attach_widget_screen_changed (attach_widget, NULL, menu);
5316 * @child: a #GtkMenuItem
5317 * @left_attach: The column number to attach the left side of the item to
5318 * @right_attach: The column number to attach the right side of the item to
5319 * @top_attach: The row number to attach the top of the item to
5320 * @bottom_attach: The row number to attach the bottom of the item to
5322 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5323 * an item will occupy is specified by @left_attach, @right_attach,
5324 * @top_attach and @bottom_attach. These each represent the leftmost,
5325 * rightmost, uppermost and lower column and row numbers of the table.
5326 * (Columns and rows are indexed from zero).
5328 * Note that this function is not related to gtk_menu_detach().
5333 gtk_menu_attach (GtkMenu *menu,
5338 guint bottom_attach)
5340 GtkMenuShell *menu_shell;
5343 g_return_if_fail (GTK_IS_MENU (menu));
5344 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5345 parent = gtk_widget_get_parent (child);
5346 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5347 g_return_if_fail (left_attach < right_attach);
5348 g_return_if_fail (top_attach < bottom_attach);
5350 menu_shell = GTK_MENU_SHELL (menu);
5354 AttachInfo *ai = get_attach_info (child);
5356 ai->left_attach = left_attach;
5357 ai->right_attach = right_attach;
5358 ai->top_attach = top_attach;
5359 ai->bottom_attach = bottom_attach;
5361 menu_shell->priv->children = g_list_append (menu_shell->priv->children, child);
5363 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5365 menu_queue_resize (menu);
5369 gtk_container_child_set (GTK_CONTAINER (parent), child,
5370 "left-attach", left_attach,
5371 "right-attach", right_attach,
5372 "top-attach", top_attach,
5373 "bottom-attach", bottom_attach,
5379 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5383 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5384 "gtk-menu-popup-delay", &popup_delay,
5391 find_child_containing (GtkMenuShell *menu_shell,
5399 /* find a child which includes the area given by
5400 * left, right, top, bottom.
5402 for (list = menu_shell->priv->children; list; list = list->next)
5406 if (!_gtk_menu_item_is_selectable (list->data))
5409 get_effective_child_attach (list->data, &l, &r, &t, &b);
5411 if (l <= left && right <= r && t <= top && bottom <= b)
5412 return GTK_WIDGET (list->data);
5419 gtk_menu_move_current (GtkMenuShell *menu_shell,
5420 GtkMenuDirectionType direction)
5422 GtkMenu *menu = GTK_MENU (menu_shell);
5425 GtkWidget *match = NULL;
5427 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5431 case GTK_MENU_DIR_CHILD:
5432 direction = GTK_MENU_DIR_PARENT;
5434 case GTK_MENU_DIR_PARENT:
5435 direction = GTK_MENU_DIR_CHILD;
5441 /* use special table menu key bindings */
5442 if (menu_shell->priv->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5444 get_effective_child_attach (menu_shell->priv->active_menu_item, &l, &r, &t, &b);
5446 if (direction == GTK_MENU_DIR_NEXT)
5448 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5450 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5458 for (i = 0; i < t; i++)
5460 match = find_child_containing (menu_shell,
5461 l, l + 1, i, i + 1);
5467 else if (direction == GTK_MENU_DIR_PREV)
5469 for (i = t; i > 0; i--)
5471 match = find_child_containing (menu_shell,
5472 l, l + 1, i - 1, i);
5480 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5482 match = find_child_containing (menu_shell,
5483 l, l + 1, i - 1, i);
5489 else if (direction == GTK_MENU_DIR_PARENT)
5491 /* we go one left if possible */
5493 match = find_child_containing (menu_shell,
5494 l - 1, l, t, t + 1);
5498 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5501 || g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1)
5502 match = menu_shell->priv->active_menu_item;
5505 else if (direction == GTK_MENU_DIR_CHILD)
5507 /* we go one right if possible */
5508 if (r < gtk_menu_get_n_columns (menu))
5509 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5513 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5515 if (! GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu &&
5517 g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1))
5518 match = menu_shell->priv->active_menu_item;
5524 gtk_menu_shell_select_item (menu_shell, match);
5529 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5533 get_visible_size (GtkMenu *menu)
5535 GtkMenuPrivate *priv = menu->priv;
5536 GtkAllocation allocation;
5537 GtkWidget *widget = GTK_WIDGET (menu);
5538 GtkContainer *container = GTK_CONTAINER (menu);
5542 gtk_widget_get_allocation (widget, &allocation);
5543 get_menu_padding (widget, &padding);
5545 menu_height = (allocation.height -
5546 (2 * gtk_container_get_border_width (container)) -
5547 padding.top - padding.bottom);
5549 if (!priv->tearoff_active)
5551 GtkBorder arrow_border;
5553 get_arrows_border (menu, &arrow_border);
5554 menu_height -= arrow_border.top;
5555 menu_height -= arrow_border.bottom;
5561 /* Find the sensitive on-screen child containing @y, or if none,
5562 * the nearest selectable onscreen child. (%NULL if none)
5565 child_at (GtkMenu *menu,
5568 GtkMenuPrivate *priv = menu->priv;
5569 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5570 GtkWidget *child = NULL;
5571 gint child_offset = 0;
5574 gint lower, upper; /* Onscreen bounds */
5576 menu_height = get_visible_size (menu);
5577 lower = priv->scroll_offset;
5578 upper = priv->scroll_offset + menu_height;
5580 for (children = menu_shell->priv->children; children; children = children->next)
5582 if (gtk_widget_get_visible (children->data))
5584 GtkRequisition child_requisition;
5586 gtk_widget_get_preferred_size (children->data,
5587 &child_requisition, NULL);
5589 if (_gtk_menu_item_is_selectable (children->data) &&
5590 child_offset >= lower &&
5591 child_offset + child_requisition.height <= upper)
5593 child = children->data;
5595 if (child_offset + child_requisition.height > y &&
5596 !GTK_IS_TEAROFF_MENU_ITEM (child))
5600 child_offset += child_requisition.height;
5608 get_menu_height (GtkMenu *menu)
5610 GtkMenuPrivate *priv = menu->priv;
5611 GtkAllocation allocation;
5612 GtkWidget *widget = GTK_WIDGET (menu);
5616 gtk_widget_get_allocation (widget, &allocation);
5617 get_menu_padding (widget, &padding);
5619 height = allocation.height;
5620 height -= (gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2) +
5621 padding.top + padding.bottom;
5623 if (!priv->tearoff_active)
5625 GtkBorder arrow_border;
5627 get_arrows_border (menu, &arrow_border);
5628 height -= arrow_border.top;
5629 height -= arrow_border.bottom;
5636 gtk_menu_real_move_scroll (GtkMenu *menu,
5639 GtkMenuPrivate *priv = menu->priv;
5640 gint page_size = get_visible_size (menu);
5641 gint end_position = get_menu_height (menu);
5642 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5646 case GTK_SCROLL_PAGE_UP:
5647 case GTK_SCROLL_PAGE_DOWN:
5651 gint child_offset = 0;
5652 gboolean old_upper_arrow_visible;
5655 if (type == GTK_SCROLL_PAGE_UP)
5660 if (menu_shell->priv->active_menu_item)
5664 compute_child_offset (menu, menu_shell->priv->active_menu_item,
5665 &child_offset, &child_height, NULL);
5666 child_offset += child_height / 2;
5669 menu_shell->priv->ignore_enter = TRUE;
5670 old_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5671 old_offset = priv->scroll_offset;
5673 new_offset = priv->scroll_offset + step;
5674 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5676 gtk_menu_scroll_to (menu, new_offset);
5678 if (menu_shell->priv->active_menu_item)
5680 GtkWidget *new_child;
5681 gboolean new_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5682 GtkBorder arrow_border;
5683 get_arrows_border (menu, &arrow_border);
5685 if (priv->scroll_offset != old_offset)
5686 step = priv->scroll_offset - old_offset;
5688 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5690 new_child = child_at (menu, child_offset + step);
5692 gtk_menu_shell_select_item (menu_shell, new_child);
5696 case GTK_SCROLL_START:
5697 /* Ignore the enter event we might get if the pointer is on the menu */
5698 menu_shell->priv->ignore_enter = TRUE;
5699 gtk_menu_scroll_to (menu, 0);
5700 gtk_menu_shell_select_first (menu_shell, TRUE);
5702 case GTK_SCROLL_END:
5703 /* Ignore the enter event we might get if the pointer is on the menu */
5704 menu_shell->priv->ignore_enter = TRUE;
5705 gtk_menu_scroll_to (menu, end_position - page_size);
5706 _gtk_menu_shell_select_last (menu_shell, TRUE);
5715 * gtk_menu_set_monitor:
5717 * @monitor_num: the number of the monitor on which the menu should
5720 * Informs GTK+ on which monitor a menu should be popped up.
5721 * See gdk_screen_get_monitor_geometry().
5723 * This function should be called from a #GtkMenuPositionFunc
5724 * if the menu should not appear on the same monitor as the pointer.
5725 * This information can't be reliably inferred from the coordinates
5726 * returned by a #GtkMenuPositionFunc, since, for very long menus,
5727 * these coordinates may extend beyond the monitor boundaries or even
5728 * the screen boundaries.
5733 gtk_menu_set_monitor (GtkMenu *menu,
5736 GtkMenuPrivate *priv = menu->priv;
5738 g_return_if_fail (GTK_IS_MENU (menu));
5740 priv->monitor_num = monitor_num;
5744 * gtk_menu_get_monitor:
5747 * Retrieves the number of the monitor on which to show the menu.
5749 * Returns: the number of the monitor on which the menu should
5750 * be popped up or -1, if no monitor has been set
5755 gtk_menu_get_monitor (GtkMenu *menu)
5757 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5759 return menu->priv->monitor_num;
5763 * gtk_menu_get_for_attach_widget:
5764 * @widget: a #GtkWidget
5766 * Returns a list of the menus which are attached to this widget.
5767 * This list is owned by GTK+ and must not be modified.
5769 * Return value: (element-type GtkWidget) (transfer none): the list
5770 * of menus attached to his widget.
5775 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5779 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5781 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5787 gtk_menu_grab_notify (GtkWidget *widget,
5788 gboolean was_grabbed)
5790 GtkWidget *toplevel;
5791 GtkWindowGroup *group;
5795 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5798 !gtk_widget_device_is_shadowed (widget, pointer))
5801 toplevel = gtk_widget_get_toplevel (widget);
5803 if (!GTK_IS_WINDOW (toplevel))
5806 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5807 grab = gtk_window_group_get_current_device_grab (group, pointer);
5809 if (GTK_MENU_SHELL (widget)->priv->active && !GTK_IS_MENU_SHELL (grab))
5810 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5814 * gtk_menu_set_reserve_toggle_size:
5816 * @reserve_toggle_size: whether to reserve size for toggles
5818 * Sets whether the menu should reserve space for drawing toggles
5819 * or icons, regardless of their actual presence.
5824 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5825 gboolean reserve_toggle_size)
5827 GtkMenuPrivate *priv = menu->priv;
5828 gboolean no_toggle_size;
5830 g_return_if_fail (GTK_IS_MENU (menu));
5832 no_toggle_size = !reserve_toggle_size;
5834 if (priv->no_toggle_size != no_toggle_size)
5836 priv->no_toggle_size = no_toggle_size;
5838 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5843 * gtk_menu_get_reserve_toggle_size:
5846 * Returns whether the menu reserves space for toggles and
5847 * icons, regardless of their actual presence.
5849 * Returns: Whether the menu reserves toggle space
5854 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5856 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
5858 return !menu->priv->no_toggle_size;