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 "gtkwindow.h"
109 #include "gtkscrollbar.h"
110 #include "gtksettings.h"
111 #include "gtkprivate.h"
112 #include "gtkwidgetprivate.h"
114 #include "gtktypebuiltins.h"
116 #include "deprecated/gtktearoffmenuitem.h"
119 #include "a11y/gtkmenuaccessible.h"
121 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
122 * extends below the submenu
125 #define MENU_SCROLL_STEP1 8
126 #define MENU_SCROLL_STEP2 15
127 #define MENU_SCROLL_FAST_ZONE 8
128 #define MENU_SCROLL_TIMEOUT1 50
129 #define MENU_SCROLL_TIMEOUT2 20
131 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
132 #define ATTACHED_MENUS "gtk-attached-menus"
134 typedef struct _GtkMenuAttachData GtkMenuAttachData;
135 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
137 struct _GtkMenuAttachData
139 GtkWidget *attach_widget;
140 GtkMenuDetachFunc detacher;
143 struct _GtkMenuPopdownData
155 gint effective_left_attach;
156 gint effective_right_attach;
157 gint effective_top_attach;
158 gint effective_bottom_attach;
175 PROP_RESERVE_TOGGLE_SIZE
180 CHILD_PROP_LEFT_ATTACH,
181 CHILD_PROP_RIGHT_ATTACH,
182 CHILD_PROP_TOP_ATTACH,
183 CHILD_PROP_BOTTOM_ATTACH
186 static void gtk_menu_set_property (GObject *object,
190 static void gtk_menu_get_property (GObject *object,
194 static void gtk_menu_set_child_property(GtkContainer *container,
199 static void gtk_menu_get_child_property(GtkContainer *container,
204 static void gtk_menu_destroy (GtkWidget *widget);
205 static void gtk_menu_realize (GtkWidget *widget);
206 static void gtk_menu_unrealize (GtkWidget *widget);
207 static void gtk_menu_size_allocate (GtkWidget *widget,
208 GtkAllocation *allocation);
209 static void gtk_menu_show (GtkWidget *widget);
210 static gboolean gtk_menu_draw (GtkWidget *widget,
212 static gboolean gtk_menu_key_press (GtkWidget *widget,
214 static gboolean gtk_menu_scroll (GtkWidget *widget,
215 GdkEventScroll *event);
216 static gboolean gtk_menu_button_press (GtkWidget *widget,
217 GdkEventButton *event);
218 static gboolean gtk_menu_button_release (GtkWidget *widget,
219 GdkEventButton *event);
220 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
221 GdkEventMotion *event);
222 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
223 GdkEventCrossing *event);
224 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
225 GdkEventCrossing *event);
226 static void gtk_menu_scroll_to (GtkMenu *menu,
228 static void gtk_menu_grab_notify (GtkWidget *widget,
229 gboolean was_grabbed);
231 static void gtk_menu_stop_scrolling (GtkMenu *menu);
232 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
233 static gboolean gtk_menu_scroll_timeout (gpointer data);
234 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
235 static void gtk_menu_start_scrolling (GtkMenu *menu);
237 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
238 GtkWidget *menu_item);
239 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
240 GtkWidget *menu_item);
241 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
244 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
246 static void gtk_menu_handle_scrolling (GtkMenu *menu,
251 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
253 static void gtk_menu_style_updated (GtkWidget *widget);
254 static gboolean gtk_menu_focus (GtkWidget *widget,
255 GtkDirectionType direction);
256 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
257 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
258 GtkMenuDirectionType direction);
259 static void gtk_menu_real_move_scroll (GtkMenu *menu,
262 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
263 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
264 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
267 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
268 GtkMenuItem *menu_item,
269 GdkEventCrossing *event);
271 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
272 static void gtk_menu_show_all (GtkWidget *widget);
273 static void gtk_menu_position (GtkMenu *menu,
274 gboolean set_scroll_offset);
275 static void gtk_menu_reparent (GtkMenu *menu,
276 GtkWidget *new_parent,
278 static void gtk_menu_remove (GtkContainer *menu,
281 static void gtk_menu_update_title (GtkMenu *menu);
283 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
284 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
286 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
288 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
289 gboolean group_changed);
291 static void gtk_menu_get_preferred_width (GtkWidget *widget,
294 static void gtk_menu_get_preferred_height (GtkWidget *widget,
297 static void gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
303 static const gchar attach_data_key[] = "gtk-menu-attach-data";
305 static guint menu_signals[LAST_SIGNAL] = { 0 };
307 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
310 menu_queue_resize (GtkMenu *menu)
312 GtkMenuPrivate *priv = menu->priv;
314 priv->have_layout = FALSE;
315 gtk_widget_queue_resize (GTK_WIDGET (menu));
319 attach_info_free (AttachInfo *info)
321 g_slice_free (AttachInfo, info);
325 get_attach_info (GtkWidget *child)
327 GObject *object = G_OBJECT (child);
328 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
332 ai = g_slice_new0 (AttachInfo);
333 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
334 (GDestroyNotify) attach_info_free);
341 is_grid_attached (AttachInfo *ai)
343 return (ai->left_attach >= 0 &&
344 ai->right_attach >= 0 &&
345 ai->top_attach >= 0 &&
346 ai->bottom_attach >= 0);
350 menu_ensure_layout (GtkMenu *menu)
352 GtkMenuPrivate *priv = menu->priv;
354 if (!priv->have_layout)
356 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
360 gint max_right_attach;
361 gint max_bottom_attach;
363 /* Find extents of gridded portion
365 max_right_attach = 1;
366 max_bottom_attach = 0;
368 for (l = menu_shell->priv->children; l; l = l->next)
370 GtkWidget *child = l->data;
371 AttachInfo *ai = get_attach_info (child);
373 if (is_grid_attached (ai))
375 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
376 max_right_attach = MAX (max_right_attach, ai->right_attach);
380 /* Find empty rows */
381 row_occupied = g_malloc0 (max_bottom_attach);
383 for (l = menu_shell->priv->children; l; l = l->next)
385 GtkWidget *child = l->data;
386 AttachInfo *ai = get_attach_info (child);
388 if (is_grid_attached (ai))
392 for (i = ai->top_attach; i < ai->bottom_attach; i++)
393 row_occupied[i] = TRUE;
397 /* Lay non-grid-items out in those rows
400 for (l = menu_shell->priv->children; l; l = l->next)
402 GtkWidget *child = l->data;
403 AttachInfo *ai = get_attach_info (child);
405 if (!is_grid_attached (ai))
407 while (current_row < max_bottom_attach && row_occupied[current_row])
410 ai->effective_left_attach = 0;
411 ai->effective_right_attach = max_right_attach;
412 ai->effective_top_attach = current_row;
413 ai->effective_bottom_attach = current_row + 1;
419 ai->effective_left_attach = ai->left_attach;
420 ai->effective_right_attach = ai->right_attach;
421 ai->effective_top_attach = ai->top_attach;
422 ai->effective_bottom_attach = ai->bottom_attach;
426 g_free (row_occupied);
428 priv->n_rows = MAX (current_row, max_bottom_attach);
429 priv->n_columns = max_right_attach;
430 priv->have_layout = TRUE;
436 gtk_menu_get_n_columns (GtkMenu *menu)
438 GtkMenuPrivate *priv = menu->priv;
440 menu_ensure_layout (menu);
442 return priv->n_columns;
446 gtk_menu_get_n_rows (GtkMenu *menu)
448 GtkMenuPrivate *priv = menu->priv;
450 menu_ensure_layout (menu);
456 get_effective_child_attach (GtkWidget *child,
462 GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
465 menu_ensure_layout (menu);
467 ai = get_attach_info (child);
470 *l = ai->effective_left_attach;
472 *r = ai->effective_right_attach;
474 *t = ai->effective_top_attach;
476 *b = ai->effective_bottom_attach;
481 gtk_menu_class_init (GtkMenuClass *class)
483 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
484 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
485 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
486 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
487 GtkBindingSet *binding_set;
489 gobject_class->set_property = gtk_menu_set_property;
490 gobject_class->get_property = gtk_menu_get_property;
492 widget_class->destroy = gtk_menu_destroy;
493 widget_class->realize = gtk_menu_realize;
494 widget_class->unrealize = gtk_menu_unrealize;
495 widget_class->size_allocate = gtk_menu_size_allocate;
496 widget_class->show = gtk_menu_show;
497 widget_class->draw = gtk_menu_draw;
498 widget_class->scroll_event = gtk_menu_scroll;
499 widget_class->key_press_event = gtk_menu_key_press;
500 widget_class->button_press_event = gtk_menu_button_press;
501 widget_class->button_release_event = gtk_menu_button_release;
502 widget_class->motion_notify_event = gtk_menu_motion_notify;
503 widget_class->show_all = gtk_menu_show_all;
504 widget_class->enter_notify_event = gtk_menu_enter_notify;
505 widget_class->leave_notify_event = gtk_menu_leave_notify;
506 widget_class->style_updated = gtk_menu_style_updated;
507 widget_class->focus = gtk_menu_focus;
508 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
509 widget_class->grab_notify = gtk_menu_grab_notify;
510 widget_class->get_preferred_width = gtk_menu_get_preferred_width;
511 widget_class->get_preferred_height = gtk_menu_get_preferred_height;
512 widget_class->get_preferred_height_for_width = gtk_menu_get_preferred_height_for_width;
514 container_class->remove = gtk_menu_remove;
515 container_class->get_child_property = gtk_menu_get_child_property;
516 container_class->set_child_property = gtk_menu_set_child_property;
518 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
519 menu_shell_class->deactivate = gtk_menu_deactivate;
520 menu_shell_class->select_item = gtk_menu_select_item;
521 menu_shell_class->insert = gtk_menu_real_insert;
522 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
523 menu_shell_class->move_current = gtk_menu_move_current;
526 * GtkMenu::move-scroll:
528 * @scroll_type: a #GtkScrollType
530 menu_signals[MOVE_SCROLL] =
531 g_signal_new_class_handler (I_("move-scroll"),
532 G_OBJECT_CLASS_TYPE (gobject_class),
533 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
534 G_CALLBACK (gtk_menu_real_move_scroll),
536 _gtk_marshal_VOID__ENUM,
538 GTK_TYPE_SCROLL_TYPE);
543 * The index of the currently selected menu item, or -1 if no
544 * menu item is selected.
548 g_object_class_install_property (gobject_class,
550 g_param_spec_int ("active",
552 P_("The currently selected menu item"),
554 GTK_PARAM_READWRITE));
557 * GtkMenu:accel-group:
559 * The accel group holding accelerators for the menu.
563 g_object_class_install_property (gobject_class,
565 g_param_spec_object ("accel-group",
567 P_("The accel group holding accelerators for the menu"),
568 GTK_TYPE_ACCEL_GROUP,
569 GTK_PARAM_READWRITE));
572 * GtkMenu:accel-path:
574 * An accel path used to conveniently construct accel paths of child items.
578 g_object_class_install_property (gobject_class,
580 g_param_spec_string ("accel-path",
582 P_("An accel path used to conveniently construct accel paths of child items"),
584 GTK_PARAM_READWRITE));
587 * GtkMenu:attach-widget:
589 * The widget the menu is attached to. Setting this property attaches
590 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
591 * use gtk_menu_attach_to_widget() directly.
595 g_object_class_install_property (gobject_class,
597 g_param_spec_object ("attach-widget",
599 P_("The widget the menu is attached to"),
601 GTK_PARAM_READWRITE));
603 g_object_class_install_property (gobject_class,
605 g_param_spec_string ("tearoff-title",
607 P_("A title that may be displayed by the window manager when this menu is torn-off"),
609 GTK_PARAM_READWRITE));
612 * GtkMenu:tearoff-state:
614 * A boolean that indicates whether the menu is torn-off.
618 g_object_class_install_property (gobject_class,
620 g_param_spec_boolean ("tearoff-state",
622 P_("A boolean that indicates whether the menu is torn-off"),
624 GTK_PARAM_READWRITE));
629 * The monitor the menu will be popped up on.
633 g_object_class_install_property (gobject_class,
635 g_param_spec_int ("monitor",
637 P_("The monitor the menu will be popped up on"),
639 GTK_PARAM_READWRITE));
641 gtk_widget_class_install_style_property (widget_class,
642 g_param_spec_int ("vertical-padding",
643 P_("Vertical Padding"),
644 P_("Extra space at the top and bottom of the menu"),
648 GTK_PARAM_READABLE));
651 * GtkMenu:reserve-toggle-size:
653 * A boolean that indicates whether the menu reserves space for
654 * toggles and icons, regardless of their actual presence.
656 * This property should only be changed from its default value
657 * for special-purposes such as tabular menus. Regular menus that
658 * are connected to a menu bar or context menus should reserve
659 * toggle space for consistency.
663 g_object_class_install_property (gobject_class,
664 PROP_RESERVE_TOGGLE_SIZE,
665 g_param_spec_boolean ("reserve-toggle-size",
666 P_("Reserve Toggle Size"),
667 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
669 GTK_PARAM_READWRITE));
671 gtk_widget_class_install_style_property (widget_class,
672 g_param_spec_int ("horizontal-padding",
673 P_("Horizontal Padding"),
674 P_("Extra space at the left and right edges of the menu"),
678 GTK_PARAM_READABLE));
680 gtk_widget_class_install_style_property (widget_class,
681 g_param_spec_int ("vertical-offset",
682 P_("Vertical Offset"),
683 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
687 GTK_PARAM_READABLE));
689 gtk_widget_class_install_style_property (widget_class,
690 g_param_spec_int ("horizontal-offset",
691 P_("Horizontal Offset"),
692 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
696 GTK_PARAM_READABLE));
698 gtk_widget_class_install_style_property (widget_class,
699 g_param_spec_boolean ("double-arrows",
701 P_("When scrolling, always show both arrows."),
703 GTK_PARAM_READABLE));
706 * GtkMenu:arrow-placement:
708 * Indicates where scroll arrows should be placed.
712 gtk_widget_class_install_style_property (widget_class,
713 g_param_spec_enum ("arrow-placement",
714 P_("Arrow Placement"),
715 P_("Indicates where scroll arrows should be placed"),
716 GTK_TYPE_ARROW_PLACEMENT,
718 GTK_PARAM_READABLE));
720 gtk_container_class_install_child_property (container_class,
721 CHILD_PROP_LEFT_ATTACH,
722 g_param_spec_int ("left-attach",
724 P_("The column number to attach the left side of the child to"),
726 GTK_PARAM_READWRITE));
728 gtk_container_class_install_child_property (container_class,
729 CHILD_PROP_RIGHT_ATTACH,
730 g_param_spec_int ("right-attach",
732 P_("The column number to attach the right side of the child to"),
734 GTK_PARAM_READWRITE));
736 gtk_container_class_install_child_property (container_class,
737 CHILD_PROP_TOP_ATTACH,
738 g_param_spec_int ("top-attach",
740 P_("The row number to attach the top of the child to"),
742 GTK_PARAM_READWRITE));
744 gtk_container_class_install_child_property (container_class,
745 CHILD_PROP_BOTTOM_ATTACH,
746 g_param_spec_int ("bottom-attach",
748 P_("The row number to attach the bottom of the child to"),
750 GTK_PARAM_READWRITE));
753 * GtkMenu:arrow-scaling
755 * Arbitrary constant to scale down the size of the scroll arrow.
759 gtk_widget_class_install_style_property (widget_class,
760 g_param_spec_float ("arrow-scaling",
762 P_("Arbitrary constant to scale down the size of the scroll arrow"),
764 GTK_PARAM_READABLE));
766 binding_set = gtk_binding_set_by_class (class);
767 gtk_binding_entry_add_signal (binding_set,
769 I_("move-current"), 1,
770 GTK_TYPE_MENU_DIRECTION_TYPE,
772 gtk_binding_entry_add_signal (binding_set,
775 GTK_TYPE_MENU_DIRECTION_TYPE,
777 gtk_binding_entry_add_signal (binding_set,
780 GTK_TYPE_MENU_DIRECTION_TYPE,
782 gtk_binding_entry_add_signal (binding_set,
785 GTK_TYPE_MENU_DIRECTION_TYPE,
787 gtk_binding_entry_add_signal (binding_set,
790 GTK_TYPE_MENU_DIRECTION_TYPE,
791 GTK_MENU_DIR_PARENT);
792 gtk_binding_entry_add_signal (binding_set,
795 GTK_TYPE_MENU_DIRECTION_TYPE,
796 GTK_MENU_DIR_PARENT);
797 gtk_binding_entry_add_signal (binding_set,
800 GTK_TYPE_MENU_DIRECTION_TYPE,
802 gtk_binding_entry_add_signal (binding_set,
805 GTK_TYPE_MENU_DIRECTION_TYPE,
807 gtk_binding_entry_add_signal (binding_set,
810 GTK_TYPE_SCROLL_TYPE,
812 gtk_binding_entry_add_signal (binding_set,
815 GTK_TYPE_SCROLL_TYPE,
817 gtk_binding_entry_add_signal (binding_set,
820 GTK_TYPE_SCROLL_TYPE,
822 gtk_binding_entry_add_signal (binding_set,
825 GTK_TYPE_SCROLL_TYPE,
827 gtk_binding_entry_add_signal (binding_set,
830 GTK_TYPE_SCROLL_TYPE,
832 gtk_binding_entry_add_signal (binding_set,
833 GDK_KEY_KP_Page_Up, 0,
835 GTK_TYPE_SCROLL_TYPE,
837 gtk_binding_entry_add_signal (binding_set,
838 GDK_KEY_Page_Down, 0,
840 GTK_TYPE_SCROLL_TYPE,
841 GTK_SCROLL_PAGE_DOWN);
842 gtk_binding_entry_add_signal (binding_set,
843 GDK_KEY_KP_Page_Down, 0,
845 GTK_TYPE_SCROLL_TYPE,
846 GTK_SCROLL_PAGE_DOWN);
848 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
850 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_MENU_ACCESSIBLE);
855 gtk_menu_set_property (GObject *object,
860 GtkMenu *menu = GTK_MENU (object);
865 gtk_menu_set_active (menu, g_value_get_int (value));
867 case PROP_ACCEL_GROUP:
868 gtk_menu_set_accel_group (menu, g_value_get_object (value));
870 case PROP_ACCEL_PATH:
871 gtk_menu_set_accel_path (menu, g_value_get_string (value));
873 case PROP_ATTACH_WIDGET:
877 widget = gtk_menu_get_attach_widget (menu);
879 gtk_menu_detach (menu);
881 widget = (GtkWidget*) g_value_get_object (value);
883 gtk_menu_attach_to_widget (menu, widget, NULL);
886 case PROP_TEAROFF_STATE:
887 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
889 case PROP_TEAROFF_TITLE:
890 gtk_menu_set_title (menu, g_value_get_string (value));
893 gtk_menu_set_monitor (menu, g_value_get_int (value));
895 case PROP_RESERVE_TOGGLE_SIZE:
896 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
899 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
905 gtk_menu_get_property (GObject *object,
910 GtkMenu *menu = GTK_MENU (object);
915 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->priv->children, gtk_menu_get_active (menu)));
917 case PROP_ACCEL_GROUP:
918 g_value_set_object (value, gtk_menu_get_accel_group (menu));
920 case PROP_ACCEL_PATH:
921 g_value_set_string (value, gtk_menu_get_accel_path (menu));
923 case PROP_ATTACH_WIDGET:
924 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
926 case PROP_TEAROFF_STATE:
927 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
929 case PROP_TEAROFF_TITLE:
930 g_value_set_string (value, gtk_menu_get_title (menu));
933 g_value_set_int (value, gtk_menu_get_monitor (menu));
935 case PROP_RESERVE_TOGGLE_SIZE:
936 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
939 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
945 gtk_menu_set_child_property (GtkContainer *container,
951 GtkMenu *menu = GTK_MENU (container);
952 AttachInfo *ai = get_attach_info (child);
956 case CHILD_PROP_LEFT_ATTACH:
957 ai->left_attach = g_value_get_int (value);
959 case CHILD_PROP_RIGHT_ATTACH:
960 ai->right_attach = g_value_get_int (value);
962 case CHILD_PROP_TOP_ATTACH:
963 ai->top_attach = g_value_get_int (value);
965 case CHILD_PROP_BOTTOM_ATTACH:
966 ai->bottom_attach = g_value_get_int (value);
970 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
974 menu_queue_resize (menu);
978 gtk_menu_get_child_property (GtkContainer *container,
984 AttachInfo *ai = get_attach_info (child);
988 case CHILD_PROP_LEFT_ATTACH:
989 g_value_set_int (value, ai->left_attach);
991 case CHILD_PROP_RIGHT_ATTACH:
992 g_value_set_int (value, ai->right_attach);
994 case CHILD_PROP_TOP_ATTACH:
995 g_value_set_int (value, ai->top_attach);
997 case CHILD_PROP_BOTTOM_ATTACH:
998 g_value_set_int (value, ai->bottom_attach);
1002 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1008 gtk_menu_window_event (GtkWidget *window,
1012 gboolean handled = FALSE;
1014 g_object_ref (window);
1015 g_object_ref (menu);
1017 switch (event->type)
1020 case GDK_KEY_RELEASE:
1021 handled = gtk_widget_event (menu, event);
1027 g_object_unref (window);
1028 g_object_unref (menu);
1034 gtk_menu_init (GtkMenu *menu)
1036 GtkMenuPrivate *priv;
1037 GtkStyleContext *context;
1039 priv = G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
1043 priv->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1044 "type", GTK_WINDOW_POPUP,
1047 "signal::event", gtk_menu_window_event, menu,
1048 "signal::destroy", gtk_widget_destroyed, &priv->toplevel,
1050 gtk_window_set_resizable (GTK_WINDOW (priv->toplevel), FALSE);
1051 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->toplevel), 0);
1053 /* Refloat the menu, so that reference counting for the menu isn't
1054 * affected by it being a child of the toplevel
1056 g_object_force_floating (G_OBJECT (menu));
1057 priv->needs_destruction_ref = TRUE;
1059 priv->monitor_num = -1;
1061 context = gtk_widget_get_style_context (GTK_WIDGET (menu));
1062 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENU);
1066 gtk_menu_destroy (GtkWidget *widget)
1068 GtkMenu *menu = GTK_MENU (widget);
1069 GtkMenuPrivate *priv = menu->priv;
1070 GtkMenuAttachData *data;
1072 gtk_menu_remove_scroll_timeout (menu);
1074 data = g_object_get_data (G_OBJECT (widget), attach_data_key);
1076 gtk_menu_detach (menu);
1078 gtk_menu_stop_navigating_submenu (menu);
1080 if (priv->old_active_menu_item)
1081 g_clear_object (&priv->old_active_menu_item);
1083 /* Add back the reference count for being a child */
1084 if (priv->needs_destruction_ref)
1086 priv->needs_destruction_ref = FALSE;
1087 g_object_ref (widget);
1090 if (priv->accel_group)
1091 g_clear_object (&priv->accel_group);
1094 gtk_widget_destroy (priv->toplevel);
1096 if (priv->tearoff_window)
1097 gtk_widget_destroy (priv->tearoff_window);
1101 g_free (priv->heights);
1102 priv->heights = NULL;
1107 g_free (priv->title);
1111 if (priv->position_func_data_destroy)
1113 priv->position_func_data_destroy (priv->position_func_data);
1114 priv->position_func_data = NULL;
1115 priv->position_func_data_destroy = NULL;
1118 GTK_WIDGET_CLASS (gtk_menu_parent_class)->destroy (widget);
1122 menu_change_screen (GtkMenu *menu,
1123 GdkScreen *new_screen)
1125 GtkMenuPrivate *priv = menu->priv;
1127 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1129 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1135 gtk_window_set_screen (GTK_WINDOW (priv->tearoff_window), new_screen);
1136 gtk_menu_position (menu, TRUE);
1139 gtk_window_set_screen (GTK_WINDOW (priv->toplevel), new_screen);
1140 priv->monitor_num = -1;
1144 attach_widget_screen_changed (GtkWidget *attach_widget,
1145 GdkScreen *previous_screen,
1148 if (gtk_widget_has_screen (attach_widget) &&
1149 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1150 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1154 * gtk_menu_attach_to_widget:
1156 * @attach_widget: the #GtkWidget that the menu will be attached to
1157 * @detacher: (scope async)(allow-none): the user supplied callback function
1158 * that will be called when the menu calls gtk_menu_detach()
1160 * Attaches the menu to the widget and provides a callback function
1161 * that will be invoked when the menu calls gtk_menu_detach() during
1165 gtk_menu_attach_to_widget (GtkMenu *menu,
1166 GtkWidget *attach_widget,
1167 GtkMenuDetachFunc detacher)
1169 GtkMenuAttachData *data;
1172 g_return_if_fail (GTK_IS_MENU (menu));
1173 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1175 /* keep this function in sync with gtk_widget_set_parent() */
1176 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1179 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1180 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1184 g_object_ref_sink (menu);
1186 data = g_slice_new (GtkMenuAttachData);
1187 data->attach_widget = attach_widget;
1189 g_signal_connect (attach_widget, "screen-changed",
1190 G_CALLBACK (attach_widget_screen_changed), menu);
1191 attach_widget_screen_changed (attach_widget, NULL, menu);
1193 data->detacher = detacher;
1194 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1195 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1196 if (!g_list_find (list, menu))
1197 list = g_list_prepend (list, menu);
1199 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1200 (GDestroyNotify) g_list_free);
1202 if (gtk_widget_get_state_flags (GTK_WIDGET (menu)) != 0)
1203 gtk_widget_set_state_flags (GTK_WIDGET (menu), 0, TRUE);
1205 /* we don't need to set the style here, since
1206 * we are a toplevel widget.
1209 /* Fallback title for menu comes from attach widget */
1210 gtk_menu_update_title (menu);
1212 g_object_notify (G_OBJECT (menu), "attach-widget");
1216 * gtk_menu_get_attach_widget:
1219 * Returns the #GtkWidget that the menu is attached to.
1221 * Returns: (transfer none): the #GtkWidget that the menu is attached to
1224 gtk_menu_get_attach_widget (GtkMenu *menu)
1226 GtkMenuAttachData *data;
1228 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1230 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1232 return data->attach_widget;
1240 * Detaches the menu from the widget to which it had been attached.
1241 * This function will call the callback function, @detacher, provided
1242 * when the gtk_menu_attach_to_widget() function was called.
1245 gtk_menu_detach (GtkMenu *menu)
1247 GtkMenuAttachData *data;
1250 g_return_if_fail (GTK_IS_MENU (menu));
1252 /* keep this function in sync with gtk_widget_unparent() */
1253 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1256 g_warning ("gtk_menu_detach(): menu is not attached");
1259 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1261 g_signal_handlers_disconnect_by_func (data->attach_widget,
1262 (gpointer) attach_widget_screen_changed,
1266 data->detacher (data->attach_widget, menu);
1267 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1268 list = g_list_remove (list, menu);
1270 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1271 (GDestroyNotify) g_list_free);
1273 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1275 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1276 gtk_widget_unrealize (GTK_WIDGET (menu));
1278 g_slice_free (GtkMenuAttachData, data);
1280 /* Fallback title for menu comes from attach widget */
1281 gtk_menu_update_title (menu);
1283 g_object_unref (menu);
1287 gtk_menu_remove (GtkContainer *container,
1290 GtkMenu *menu = GTK_MENU (container);
1291 GtkMenuPrivate *priv = menu->priv;
1293 /* Clear out old_active_menu_item if it matches the item we are removing */
1294 if (priv->old_active_menu_item == widget)
1295 g_clear_object (&priv->old_active_menu_item);
1297 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1299 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1301 menu_queue_resize (menu);
1307 * Creates a new #GtkMenu
1309 * Returns: a new #GtkMenu
1314 return g_object_new (GTK_TYPE_MENU, NULL);
1318 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1322 GtkMenu *menu = GTK_MENU (menu_shell);
1323 GtkMenuPrivate *priv = menu->priv;
1324 AttachInfo *ai = get_attach_info (child);
1326 ai->left_attach = -1;
1327 ai->right_attach = -1;
1328 ai->top_attach = -1;
1329 ai->bottom_attach = -1;
1331 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1332 gtk_widget_set_parent_window (child, priv->bin_window);
1334 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1336 menu_queue_resize (menu);
1340 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1342 GtkMenuPrivate *priv = menu->priv;
1348 cairo_surface_t *surface;
1349 cairo_pattern_t *pattern;
1352 priv->tearoff_active = FALSE;
1353 priv->saved_scroll_offset = priv->scroll_offset;
1355 window = gtk_widget_get_window (priv->tearoff_window);
1356 width = gdk_window_get_width (window);
1357 height = gdk_window_get_height (window);
1359 surface = gdk_window_create_similar_surface (window,
1360 CAIRO_CONTENT_COLOR,
1364 cr = cairo_create (surface);
1365 gdk_cairo_set_source_window (cr, window, 0, 0);
1369 gtk_widget_set_size_request (priv->tearoff_window, width, height);
1371 pattern = cairo_pattern_create_for_surface (surface);
1372 gdk_window_set_background_pattern (window, pattern);
1374 cairo_pattern_destroy (pattern);
1375 cairo_surface_destroy (surface);
1380 popup_grab_on_window (GdkWindow *window,
1381 GdkDevice *keyboard,
1383 guint32 activate_time)
1386 gdk_device_grab (keyboard, window,
1387 GDK_OWNERSHIP_WINDOW, TRUE,
1388 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1389 NULL, activate_time) != GDK_GRAB_SUCCESS)
1393 gdk_device_grab (pointer, window,
1394 GDK_OWNERSHIP_WINDOW, TRUE,
1395 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1396 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1397 GDK_POINTER_MOTION_MASK,
1398 NULL, activate_time) != GDK_GRAB_SUCCESS)
1401 gdk_device_ungrab (keyboard, activate_time);
1410 * gtk_menu_popup_for_device:
1412 * @device: (allow-none): a #GdkDevice
1413 * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1414 * menu item, or %NULL
1415 * @parent_menu_item: (allow-none): the menu item whose activation triggered
1416 * the popup, or %NULL
1417 * @func: (allow-none): a user supplied function used to position the menu,
1419 * @data: (allow-none): user supplied data to be passed to @func
1420 * @destroy: (allow-none): destroy notify for @data
1421 * @button: the mouse button which was pressed to initiate the event
1422 * @activate_time: the time at which the activation event occurred
1424 * Displays a menu and makes it available for selection.
1426 * Applications can use this function to display context-sensitive menus,
1427 * and will typically supply %NULL for the @parent_menu_shell,
1428 * @parent_menu_item, @func, @data and @destroy parameters. The default
1429 * menu positioning function will position the menu at the current position
1430 * of @device (or its corresponding pointer).
1432 * The @button parameter should be the mouse button pressed to initiate
1433 * the menu popup. If the menu popup was initiated by something other than
1434 * a mouse button press, such as a mouse button release or a keypress,
1435 * @button should be 0.
1437 * The @activate_time parameter is used to conflict-resolve initiation of
1438 * concurrent requests for mouse/keyboard grab requests. To function
1439 * properly, this needs to be the time stamp of the user event (such as
1440 * a mouse click or key press) that caused the initiation of the popup.
1441 * Only if no such event is available, gtk_get_current_event_time() can
1447 gtk_menu_popup_for_device (GtkMenu *menu,
1449 GtkWidget *parent_menu_shell,
1450 GtkWidget *parent_menu_item,
1451 GtkMenuPositionFunc func,
1453 GDestroyNotify destroy,
1455 guint32 activate_time)
1457 GtkMenuPrivate *priv = menu->priv;
1459 GtkWidget *xgrab_shell;
1461 GdkEvent *current_event;
1462 GtkMenuShell *menu_shell;
1463 gboolean grab_keyboard;
1464 GtkWidget *parent_toplevel;
1465 GdkDevice *keyboard, *pointer;
1467 g_return_if_fail (GTK_IS_MENU (menu));
1468 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1471 device = gtk_get_current_event_device ();
1475 GdkDisplay *display;
1476 GdkDeviceManager *device_manager;
1479 display = gtk_widget_get_display (GTK_WIDGET (menu));
1480 device_manager = gdk_display_get_device_manager (display);
1481 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1483 device = devices->data;
1485 g_list_free (devices);
1488 widget = GTK_WIDGET (menu);
1489 menu_shell = GTK_MENU_SHELL (menu);
1491 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1494 pointer = gdk_device_get_associated_device (device);
1499 keyboard = gdk_device_get_associated_device (device);
1502 menu_shell->priv->parent_menu_shell = parent_menu_shell;
1504 priv->seen_item_enter = FALSE;
1506 /* Find the last viewable ancestor, and make an X grab on it
1508 parent = GTK_WIDGET (menu);
1512 gboolean viewable = TRUE;
1513 GtkWidget *tmp = parent;
1517 if (!gtk_widget_get_mapped (tmp))
1522 tmp = gtk_widget_get_parent (tmp);
1526 xgrab_shell = parent;
1528 parent = GTK_MENU_SHELL (parent)->priv->parent_menu_shell;
1531 /* We want to receive events generated when we map the menu;
1532 * unfortunately, since there is probably already an implicit
1533 * grab in place from the button that the user used to pop up
1534 * the menu, we won't receive then -- in particular, the EnterNotify
1535 * when the menu pops up under the pointer.
1537 * If we are grabbing on a parent menu shell, no problem; just grab
1538 * on that menu shell first before popping up the window with
1539 * owner_events = TRUE.
1541 * When grabbing on the menu itself, things get more convoluted --
1542 * we do an explicit grab on a specially created window with
1543 * owner_events = TRUE, which we override further down with a
1544 * grab on the menu. (We can't grab on the menu until it is mapped;
1545 * we probably could just leave the grab on the other window,
1546 * with a little reorganization of the code in gtkmenu*).
1548 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1549 gtk_window_set_accept_focus (GTK_WINDOW (priv->toplevel), grab_keyboard);
1554 if (xgrab_shell && xgrab_shell != widget)
1556 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1558 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1559 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1564 GdkWindow *transfer_window;
1566 xgrab_shell = widget;
1567 transfer_window = menu_grab_transfer_window_get (menu);
1568 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1570 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1571 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1575 if (!GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab)
1577 /* We failed to make our pointer/keyboard grab.
1578 * Rather than leaving the user with a stuck up window,
1579 * we just abort here. Presumably the user will try again.
1581 menu_shell->priv->parent_menu_shell = NULL;
1582 menu_grab_transfer_window_destroy (menu);
1586 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1587 menu_shell->priv->active = TRUE;
1588 menu_shell->priv->button = button;
1590 /* If we are popping up the menu from something other than, a button
1591 * press then, as a heuristic, we ignore enter events for the menu
1592 * until we get a MOTION_NOTIFY.
1595 current_event = gtk_get_current_event ();
1598 if ((current_event->type != GDK_BUTTON_PRESS) &&
1599 (current_event->type != GDK_ENTER_NOTIFY))
1600 menu_shell->priv->ignore_enter = TRUE;
1602 gdk_event_free (current_event);
1605 menu_shell->priv->ignore_enter = TRUE;
1609 gtk_menu_tearoff_bg_copy (menu);
1611 gtk_menu_reparent (menu, priv->toplevel, FALSE);
1614 parent_toplevel = NULL;
1615 if (parent_menu_shell)
1616 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1617 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1619 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1621 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1624 /* Set transient for to get the right window group and parent */
1625 if (GTK_IS_WINDOW (parent_toplevel))
1626 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel),
1627 GTK_WINDOW (parent_toplevel));
1629 priv->parent_menu_item = parent_menu_item;
1630 priv->position_func = func;
1631 priv->position_func_data = data;
1632 priv->position_func_data_destroy = destroy;
1633 menu_shell->priv->activate_time = activate_time;
1635 /* We need to show the menu here rather in the init function
1636 * because code expects to be able to tell if the menu is onscreen
1637 * by looking at gtk_widget_get_visible (menu)
1639 gtk_widget_show (GTK_WIDGET (menu));
1641 /* Position the menu, possibly changing the size request
1643 gtk_menu_position (menu, TRUE);
1645 /* Compute the size of the toplevel and realize it so we
1646 * can scroll correctly.
1648 if (!gtk_widget_get_realized (GTK_WIDGET (menu)))
1650 GtkRequisition tmp_request;
1651 GtkAllocation tmp_allocation = { 0, };
1653 /* Instead of trusting the menu position function to queue a
1654 * resize when the menu goes out of bounds, invalidate the cached
1657 gtk_widget_queue_resize (GTK_WIDGET (menu));
1658 gtk_widget_get_preferred_size (priv->toplevel, &tmp_request, NULL);
1660 tmp_allocation.width = tmp_request.width;
1661 tmp_allocation.height = tmp_request.height;
1663 gtk_widget_size_allocate (priv->toplevel, &tmp_allocation);
1665 gtk_widget_realize (priv->toplevel);
1668 gtk_menu_scroll_to (menu, priv->scroll_offset);
1670 /* if no item is selected, select the first one */
1671 if (!menu_shell->priv->active_menu_item)
1673 gboolean touchscreen_mode;
1675 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1676 "gtk-touchscreen-mode", &touchscreen_mode,
1679 if (touchscreen_mode)
1680 gtk_menu_shell_select_first (menu_shell, TRUE);
1683 /* Once everything is set up correctly, map the toplevel */
1684 gtk_widget_show (priv->toplevel);
1686 if (xgrab_shell == widget)
1687 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1689 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1691 if (parent_menu_shell)
1693 gboolean keyboard_mode;
1695 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1696 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1698 else if (menu_shell->priv->button == 0) /* a keynav-activated context menu */
1699 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1701 _gtk_menu_shell_update_mnemonics (menu_shell);
1705 * gtk_menu_popup: (skip)
1707 * @parent_menu_shell: (allow-none): the menu shell containing the
1708 * triggering menu item, or %NULL
1709 * @parent_menu_item: (allow-none): the menu item whose activation
1710 * triggered the popup, or %NULL
1711 * @func: (allow-none): a user supplied function used to position
1712 * the menu, or %NULL
1713 * @data: user supplied data to be passed to @func.
1714 * @button: the mouse button which was pressed to initiate the event.
1715 * @activate_time: the time at which the activation event occurred.
1717 * Displays a menu and makes it available for selection.
1719 * Applications can use this function to display context-sensitive
1720 * menus, and will typically supply %NULL for the @parent_menu_shell,
1721 * @parent_menu_item, @func and @data parameters. The default menu
1722 * positioning function will position the menu at the current mouse
1725 * The @button parameter should be the mouse button pressed to initiate
1726 * the menu popup. If the menu popup was initiated by something other
1727 * than a mouse button press, such as a mouse button release or a keypress,
1728 * @button should be 0.
1730 * The @activate_time parameter is used to conflict-resolve initiation
1731 * of concurrent requests for mouse/keyboard grab requests. To function
1732 * properly, this needs to be the timestamp of the user event (such as
1733 * a mouse click or key press) that caused the initiation of the popup.
1734 * Only if no such event is available, gtk_get_current_event_time() can
1738 gtk_menu_popup (GtkMenu *menu,
1739 GtkWidget *parent_menu_shell,
1740 GtkWidget *parent_menu_item,
1741 GtkMenuPositionFunc func,
1744 guint32 activate_time)
1746 g_return_if_fail (GTK_IS_MENU (menu));
1748 gtk_menu_popup_for_device (menu,
1753 button, activate_time);
1760 * Removes the menu from the screen.
1763 gtk_menu_popdown (GtkMenu *menu)
1765 GtkMenuPrivate *priv;
1766 GtkMenuShell *menu_shell;
1769 g_return_if_fail (GTK_IS_MENU (menu));
1771 menu_shell = GTK_MENU_SHELL (menu);
1774 menu_shell->priv->parent_menu_shell = NULL;
1775 menu_shell->priv->active = FALSE;
1776 menu_shell->priv->ignore_enter = FALSE;
1778 priv->have_position = FALSE;
1780 gtk_menu_stop_scrolling (menu);
1781 gtk_menu_stop_navigating_submenu (menu);
1783 if (menu_shell->priv->active_menu_item)
1785 if (priv->old_active_menu_item)
1786 g_object_unref (priv->old_active_menu_item);
1787 priv->old_active_menu_item = menu_shell->priv->active_menu_item;
1788 g_object_ref (priv->old_active_menu_item);
1791 gtk_menu_shell_deselect (menu_shell);
1793 /* The X Grab, if present, will automatically be removed
1794 * when we hide the window
1798 gtk_widget_hide (priv->toplevel);
1799 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel), NULL);
1802 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1806 gtk_widget_set_size_request (priv->tearoff_window, -1, -1);
1808 if (gtk_bin_get_child (GTK_BIN (priv->toplevel)))
1810 gtk_menu_reparent (menu, priv->tearoff_hbox, TRUE);
1814 /* We popped up the menu from the tearoff, so we need to
1815 * release the grab - we aren't actually hiding the menu.
1817 if (menu_shell->priv->have_xgrab && pointer)
1819 GdkDevice *keyboard;
1821 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1822 keyboard = gdk_device_get_associated_device (pointer);
1825 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1829 /* gtk_menu_popdown is called each time a menu item is selected from
1830 * a torn off menu. Only scroll back to the saved position if the
1831 * non-tearoff menu was popped down.
1833 if (!priv->tearoff_active)
1834 gtk_menu_scroll_to (menu, priv->saved_scroll_offset);
1835 priv->tearoff_active = TRUE;
1838 gtk_widget_hide (GTK_WIDGET (menu));
1840 menu_shell->priv->have_xgrab = FALSE;
1843 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1845 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1847 menu_grab_transfer_window_destroy (menu);
1851 * gtk_menu_get_active:
1854 * Returns the selected menu item from the menu. This is used by the
1857 * Returns: (transfer none): the #GtkMenuItem that was last selected
1858 * in the menu. If a selection has not yet been made, the
1859 * first menu item is selected.
1862 gtk_menu_get_active (GtkMenu *menu)
1864 GtkMenuPrivate *priv = menu->priv;
1868 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1870 if (!priv->old_active_menu_item)
1873 children = GTK_MENU_SHELL (menu)->priv->children;
1877 child = children->data;
1878 children = children->next;
1880 if (gtk_bin_get_child (GTK_BIN (child)))
1885 priv->old_active_menu_item = child;
1886 if (priv->old_active_menu_item)
1887 g_object_ref (priv->old_active_menu_item);
1890 return priv->old_active_menu_item;
1894 * gtk_menu_set_active:
1896 * @index: the index of the menu item to select. Iindex values are
1899 * Selects the specified menu item within the menu. This is used by
1900 * the #GtkOptionMenu and should not be used by anyone else.
1903 gtk_menu_set_active (GtkMenu *menu,
1906 GtkMenuPrivate *priv = menu->priv;
1910 g_return_if_fail (GTK_IS_MENU (menu));
1912 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->priv->children, index);
1915 child = tmp_list->data;
1916 if (gtk_bin_get_child (GTK_BIN (child)))
1918 if (priv->old_active_menu_item)
1919 g_object_unref (priv->old_active_menu_item);
1920 priv->old_active_menu_item = child;
1921 g_object_ref (priv->old_active_menu_item);
1927 * gtk_menu_set_accel_group:
1929 * @accel_group: (allow-none): the #GtkAccelGroup to be associated
1932 * Set the #GtkAccelGroup which holds global accelerators for the
1933 * menu. This accelerator group needs to also be added to all windows
1934 * that this menu is being used in with gtk_window_add_accel_group(),
1935 * in order for those windows to support all the accelerators
1936 * contained in this group.
1939 gtk_menu_set_accel_group (GtkMenu *menu,
1940 GtkAccelGroup *accel_group)
1942 GtkMenuPrivate *priv = menu->priv;
1943 g_return_if_fail (GTK_IS_MENU (menu));
1945 if (priv->accel_group != accel_group)
1947 if (priv->accel_group)
1948 g_object_unref (priv->accel_group);
1949 priv->accel_group = accel_group;
1950 if (priv->accel_group)
1951 g_object_ref (priv->accel_group);
1952 _gtk_menu_refresh_accel_paths (menu, TRUE);
1957 * gtk_menu_get_accel_group:
1960 * Gets the #GtkAccelGroup which holds global accelerators for the
1961 * menu. See gtk_menu_set_accel_group().
1963 * Returns: (transfer none): the #GtkAccelGroup associated with the menu
1966 gtk_menu_get_accel_group (GtkMenu *menu)
1968 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1970 return menu->priv->accel_group;
1974 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1977 /* Menu items chain here to figure whether they can activate their
1978 * accelerators. Unlike ordinary widgets, menus allow accel
1979 * activation even if invisible since that's the usual case for
1980 * submenus/popup-menus. however, the state of the attach widget
1981 * affects the "activeness" of the menu.
1983 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1986 return gtk_widget_can_activate_accel (awidget, signal_id);
1988 return gtk_widget_is_sensitive (widget);
1992 * gtk_menu_set_accel_path
1993 * @menu: a valid #GtkMenu
1994 * @accel_path: (allow-none): a valid accelerator path
1996 * Sets an accelerator path for this menu from which accelerator paths
1997 * for its immediate children, its menu items, can be constructed.
1998 * The main purpose of this function is to spare the programmer the
1999 * inconvenience of having to call gtk_menu_item_set_accel_path() on
2000 * each menu item that should support runtime user changable accelerators.
2001 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
2002 * each menu item of this menu, that contains a label describing its
2003 * purpose, automatically gets an accel path assigned.
2005 * For example, a menu containing menu items "New" and "Exit", will, after
2006 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
2007 * has been called, assign its items the accel paths:
2008 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
2010 * Assigning accel paths to menu items then enables the user to change
2011 * their accelerators at runtime. More details about accelerator paths
2012 * and their default setups can be found at gtk_accel_map_add_entry().
2014 * Note that @accel_path string will be stored in a #GQuark. Therefore,
2015 * if you pass a static string, you can save some memory by interning
2016 * it first with g_intern_static_string().
2019 gtk_menu_set_accel_path (GtkMenu *menu,
2020 const gchar *accel_path)
2022 GtkMenuPrivate *priv = menu->priv;
2023 g_return_if_fail (GTK_IS_MENU (menu));
2026 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
2028 /* FIXME: accel_path should be defined as const gchar* */
2029 priv->accel_path = (gchar*)g_intern_string (accel_path);
2030 if (priv->accel_path)
2031 _gtk_menu_refresh_accel_paths (menu, FALSE);
2035 * gtk_menu_get_accel_path
2036 * @menu: a valid #GtkMenu
2038 * Retrieves the accelerator path set on the menu.
2040 * Returns: the accelerator path set on the menu.
2045 gtk_menu_get_accel_path (GtkMenu *menu)
2047 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2049 return menu->priv->accel_path;
2054 gboolean group_changed;
2058 refresh_accel_paths_foreach (GtkWidget *widget,
2061 GtkMenuPrivate *priv;
2062 AccelPropagation *prop = data;
2064 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
2066 priv = prop->menu->priv;
2067 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2070 prop->group_changed);
2075 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
2076 gboolean group_changed)
2078 GtkMenuPrivate *priv = menu->priv;
2079 g_return_if_fail (GTK_IS_MENU (menu));
2081 if (priv->accel_path && priv->accel_group)
2083 AccelPropagation prop;
2086 prop.group_changed = group_changed;
2087 gtk_container_foreach (GTK_CONTAINER (menu),
2088 refresh_accel_paths_foreach,
2094 * gtk_menu_reposition:
2097 * Repositions the menu according to its position function.
2100 gtk_menu_reposition (GtkMenu *menu)
2102 g_return_if_fail (GTK_IS_MENU (menu));
2104 if (!menu->priv->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2105 gtk_menu_position (menu, FALSE);
2109 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2114 value = gtk_adjustment_get_value (adjustment);
2115 if (menu->priv->scroll_offset != value)
2116 gtk_menu_scroll_to (menu, value);
2120 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2123 GtkMenuPrivate *priv = menu->priv;
2124 GdkGeometry geometry_hints;
2126 if (!priv->tearoff_window)
2129 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2131 GtkRequisition requisition;
2133 gtk_widget_get_preferred_size (priv->tearoff_scrollbar,
2134 &requisition, NULL);
2135 width += requisition.width;
2138 geometry_hints.min_width = width;
2139 geometry_hints.max_width = width;
2141 geometry_hints.min_height = 0;
2142 geometry_hints.max_height = priv->requested_height;
2144 gtk_window_set_geometry_hints (GTK_WINDOW (priv->tearoff_window),
2147 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2151 gtk_menu_update_title (GtkMenu *menu)
2153 GtkMenuPrivate *priv = menu->priv;
2155 if (priv->tearoff_window)
2158 GtkWidget *attach_widget;
2160 title = gtk_menu_get_title (menu);
2163 attach_widget = gtk_menu_get_attach_widget (menu);
2164 if (GTK_IS_MENU_ITEM (attach_widget))
2166 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2167 if (GTK_IS_LABEL (child))
2168 title = gtk_label_get_text (GTK_LABEL (child));
2173 gtk_window_set_title (GTK_WINDOW (priv->tearoff_window), title);
2178 gtk_menu_get_toplevel (GtkWidget *menu)
2180 GtkWidget *attach, *toplevel;
2182 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2184 if (GTK_IS_MENU_ITEM (attach))
2185 attach = gtk_widget_get_parent (attach);
2187 if (GTK_IS_MENU (attach))
2188 return gtk_menu_get_toplevel (attach);
2189 else if (GTK_IS_WIDGET (attach))
2191 toplevel = gtk_widget_get_toplevel (attach);
2192 if (gtk_widget_is_toplevel (toplevel))
2200 tearoff_window_destroyed (GtkWidget *widget,
2203 gtk_menu_set_tearoff_state (menu, FALSE);
2207 * gtk_menu_set_tearoff_state:
2209 * @torn_off: If %TRUE, menu is displayed as a tearoff menu.
2211 * Changes the tearoff state of the menu. A menu is normally
2212 * displayed as drop down menu which persists as long as the menu is
2213 * active. It can also be displayed as a tearoff menu which persists
2214 * until it is closed or reattached.
2217 gtk_menu_set_tearoff_state (GtkMenu *menu,
2220 GtkMenuPrivate *priv = menu->priv;
2223 g_return_if_fail (GTK_IS_MENU (menu));
2225 if (priv->torn_off != torn_off)
2227 priv->torn_off = torn_off;
2228 priv->tearoff_active = torn_off;
2232 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2233 gtk_menu_popdown (menu);
2235 if (!priv->tearoff_window)
2237 GtkWidget *toplevel;
2239 priv->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2240 "type", GTK_WINDOW_TOPLEVEL,
2241 "screen", gtk_widget_get_screen (priv->toplevel),
2242 "app-paintable", TRUE,
2245 gtk_window_set_type_hint (GTK_WINDOW (priv->tearoff_window),
2246 GDK_WINDOW_TYPE_HINT_MENU);
2247 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->tearoff_window), 0);
2248 g_signal_connect (priv->tearoff_window, "destroy",
2249 G_CALLBACK (tearoff_window_destroyed), menu);
2250 g_signal_connect (priv->tearoff_window, "event",
2251 G_CALLBACK (gtk_menu_window_event), menu);
2253 gtk_menu_update_title (menu);
2255 gtk_widget_realize (priv->tearoff_window);
2257 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2258 if (toplevel != NULL)
2259 gtk_window_set_transient_for (GTK_WINDOW (priv->tearoff_window),
2260 GTK_WINDOW (toplevel));
2262 priv->tearoff_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2263 gtk_container_add (GTK_CONTAINER (priv->tearoff_window),
2264 priv->tearoff_hbox);
2266 height = gdk_window_get_height (gtk_widget_get_window (GTK_WIDGET (menu)));
2267 priv->tearoff_adjustment = gtk_adjustment_new (0,
2268 0, priv->requested_height,
2272 g_object_connect (priv->tearoff_adjustment,
2273 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2275 priv->tearoff_scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, priv->tearoff_adjustment);
2277 gtk_box_pack_end (GTK_BOX (priv->tearoff_hbox),
2278 priv->tearoff_scrollbar,
2281 if (gtk_adjustment_get_upper (priv->tearoff_adjustment) > height)
2282 gtk_widget_show (priv->tearoff_scrollbar);
2284 gtk_widget_show (priv->tearoff_hbox);
2287 gtk_menu_reparent (menu, priv->tearoff_hbox, FALSE);
2289 /* Update menu->requisition */
2290 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, NULL);
2292 gtk_menu_set_tearoff_hints (menu, gdk_window_get_width (gtk_widget_get_window (GTK_WIDGET (menu))));
2294 gtk_widget_realize (priv->tearoff_window);
2295 gtk_menu_position (menu, TRUE);
2297 gtk_widget_show (GTK_WIDGET (menu));
2298 gtk_widget_show (priv->tearoff_window);
2300 gtk_menu_scroll_to (menu, 0);
2305 gtk_widget_hide (GTK_WIDGET (menu));
2306 gtk_widget_hide (priv->tearoff_window);
2307 if (GTK_IS_CONTAINER (priv->toplevel))
2308 gtk_menu_reparent (menu, priv->toplevel, FALSE);
2309 gtk_widget_destroy (priv->tearoff_window);
2311 priv->tearoff_window = NULL;
2312 priv->tearoff_hbox = NULL;
2313 priv->tearoff_scrollbar = NULL;
2314 priv->tearoff_adjustment = NULL;
2317 g_object_notify (G_OBJECT (menu), "tearoff-state");
2322 * gtk_menu_get_tearoff_state:
2325 * Returns whether the menu is torn off.
2326 * See gtk_menu_set_tearoff_state().
2328 * Return value: %TRUE if the menu is currently torn off.
2331 gtk_menu_get_tearoff_state (GtkMenu *menu)
2333 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2335 return menu->priv->torn_off;
2339 * gtk_menu_set_title:
2341 * @title: a string containing the title for the menu
2343 * Sets the title string for the menu.
2345 * The title is displayed when the menu is shown as a tearoff
2346 * menu. If @title is %NULL, the menu will see if it is attached
2347 * to a parent menu item, and if so it will try to use the same
2348 * text as that menu item's label.
2351 gtk_menu_set_title (GtkMenu *menu,
2354 GtkMenuPrivate *priv = menu->priv;
2357 g_return_if_fail (GTK_IS_MENU (menu));
2359 old_title = priv->title;
2360 priv->title = g_strdup (title);
2363 gtk_menu_update_title (menu);
2364 g_object_notify (G_OBJECT (menu), "tearoff-title");
2368 * gtk_menu_get_title:
2371 * Returns the title of the menu. See gtk_menu_set_title().
2373 * Return value: the title of the menu, or %NULL if the menu
2374 * has no title set on it. This string is owned by GTK+
2375 * and should not be modified or freed.
2378 gtk_menu_get_title (GtkMenu *menu)
2380 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2382 return menu->priv->title;
2386 * gtk_menu_reorder_child:
2388 * @child: the #GtkMenuItem to move
2389 * @position: the new position to place @child.
2390 * Positions are numbered from 0 to n - 1
2392 * Moves @child to a new @position in the list of @menu
2396 gtk_menu_reorder_child (GtkMenu *menu,
2400 GtkMenuShell *menu_shell;
2402 g_return_if_fail (GTK_IS_MENU (menu));
2403 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2405 menu_shell = GTK_MENU_SHELL (menu);
2407 if (g_list_find (menu_shell->priv->children, child))
2409 menu_shell->priv->children = g_list_remove (menu_shell->priv->children, child);
2410 menu_shell->priv->children = g_list_insert (menu_shell->priv->children, child, position);
2412 menu_queue_resize (menu);
2417 gtk_menu_style_updated (GtkWidget *widget)
2419 GTK_WIDGET_CLASS (gtk_menu_parent_class)->style_updated (widget);
2421 if (gtk_widget_get_realized (widget))
2423 GtkMenu *menu = GTK_MENU (widget);
2424 GtkMenuPrivate *priv = menu->priv;
2425 GtkStyleContext *context;
2427 context = gtk_widget_get_style_context (widget);
2429 gtk_style_context_set_background (context, priv->bin_window);
2430 gtk_style_context_set_background (context, priv->view_window);
2431 gtk_style_context_set_background (context, gtk_widget_get_window (widget));
2436 get_arrows_border (GtkMenu *menu,
2439 GtkMenuPrivate *priv = menu->priv;
2440 guint scroll_arrow_height;
2441 GtkArrowPlacement arrow_placement;
2443 gtk_widget_style_get (GTK_WIDGET (menu),
2444 "scroll-arrow-vlength", &scroll_arrow_height,
2445 "arrow_placement", &arrow_placement,
2448 switch (arrow_placement)
2450 case GTK_ARROWS_BOTH:
2451 border->top = priv->upper_arrow_visible ? scroll_arrow_height : 0;
2452 border->bottom = priv->lower_arrow_visible ? scroll_arrow_height : 0;
2455 case GTK_ARROWS_START:
2456 border->top = (priv->upper_arrow_visible ||
2457 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2461 case GTK_ARROWS_END:
2463 border->bottom = (priv->upper_arrow_visible ||
2464 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2468 border->left = border->right = 0;
2472 get_menu_padding (GtkWidget *widget,
2475 GtkStyleContext *context;
2476 GtkStateFlags state;
2478 context = gtk_widget_get_style_context (widget);
2479 state = gtk_widget_get_state_flags (widget);
2481 gtk_style_context_get_padding (context, state, padding);
2485 gtk_menu_realize (GtkWidget *widget)
2487 GtkMenu *menu = GTK_MENU (widget);
2488 GtkMenuPrivate *priv = menu->priv;
2489 GtkAllocation allocation;
2490 GtkStyleContext *context;
2492 GdkWindowAttr attributes;
2493 gint attributes_mask;
2497 guint vertical_padding;
2498 guint horizontal_padding;
2499 GtkBorder arrow_border, padding;
2501 g_return_if_fail (GTK_IS_MENU (widget));
2503 gtk_widget_set_realized (widget, TRUE);
2505 gtk_widget_get_allocation (widget, &allocation);
2507 attributes.window_type = GDK_WINDOW_CHILD;
2508 attributes.x = allocation.x;
2509 attributes.y = allocation.y;
2510 attributes.width = allocation.width;
2511 attributes.height = allocation.height;
2512 attributes.wclass = GDK_INPUT_OUTPUT;
2513 attributes.visual = gtk_widget_get_visual (widget);
2514 attributes.event_mask = gtk_widget_get_events (widget);
2515 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2516 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2518 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2520 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2521 &attributes, attributes_mask);
2522 gtk_widget_set_window (widget, window);
2523 gdk_window_set_user_data (window, widget);
2525 get_menu_padding (widget, &padding);
2526 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2527 context = gtk_widget_get_style_context (widget);
2529 gtk_widget_style_get (GTK_WIDGET (menu),
2530 "vertical-padding", &vertical_padding,
2531 "horizontal-padding", &horizontal_padding,
2534 gtk_widget_get_allocation (widget, &allocation);
2536 attributes.x = border_width + padding.left + horizontal_padding;
2537 attributes.y = border_width + padding.top + vertical_padding;
2538 attributes.width = allocation.width -
2539 (2 * (border_width + horizontal_padding)) - padding.left - padding.right;
2540 attributes.height = allocation.height -
2541 (2 * (border_width + vertical_padding)) - padding.top - padding.bottom;
2543 get_arrows_border (menu, &arrow_border);
2544 attributes.y += arrow_border.top;
2545 attributes.height -= arrow_border.top;
2546 attributes.height -= arrow_border.bottom;
2548 attributes.width = MAX (1, attributes.width);
2549 attributes.height = MAX (1, attributes.height);
2551 priv->view_window = gdk_window_new (window,
2552 &attributes, attributes_mask);
2553 gdk_window_set_user_data (priv->view_window, menu);
2555 gtk_widget_get_allocation (widget, &allocation);
2559 attributes.width = allocation.width + (2 * (border_width + horizontal_padding)) +
2560 padding.left + padding.right;
2561 attributes.height = priv->requested_height - (2 * (border_width + vertical_padding)) +
2562 padding.top + padding.bottom;
2564 attributes.width = MAX (1, attributes.width);
2565 attributes.height = MAX (1, attributes.height);
2567 priv->bin_window = gdk_window_new (priv->view_window,
2568 &attributes, attributes_mask);
2569 gdk_window_set_user_data (priv->bin_window, menu);
2571 children = GTK_MENU_SHELL (menu)->priv->children;
2574 child = children->data;
2575 children = children->next;
2577 gtk_widget_set_parent_window (child, priv->bin_window);
2580 gtk_style_context_set_background (context, priv->bin_window);
2581 gtk_style_context_set_background (context, priv->view_window);
2582 gtk_style_context_set_background (context, window);
2584 if (GTK_MENU_SHELL (widget)->priv->active_menu_item)
2585 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2586 GTK_MENU_SHELL (widget)->priv->active_menu_item);
2588 gdk_window_show (priv->bin_window);
2589 gdk_window_show (priv->view_window);
2593 gtk_menu_focus (GtkWidget *widget,
2594 GtkDirectionType direction)
2596 /* A menu or its menu items cannot have focus */
2600 /* See notes in gtk_menu_popup() for information
2601 * about the "grab transfer window"
2604 menu_grab_transfer_window_get (GtkMenu *menu)
2606 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2609 GdkWindowAttr attributes;
2610 gint attributes_mask;
2612 attributes.x = -100;
2613 attributes.y = -100;
2614 attributes.width = 10;
2615 attributes.height = 10;
2616 attributes.window_type = GDK_WINDOW_TEMP;
2617 attributes.wclass = GDK_INPUT_ONLY;
2618 attributes.override_redirect = TRUE;
2619 attributes.event_mask = 0;
2621 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2623 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2624 &attributes, attributes_mask);
2625 gdk_window_set_user_data (window, menu);
2627 gdk_window_show (window);
2629 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2636 menu_grab_transfer_window_destroy (GtkMenu *menu)
2638 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2641 gdk_window_set_user_data (window, NULL);
2642 gdk_window_destroy (window);
2643 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2648 gtk_menu_unrealize (GtkWidget *widget)
2650 GtkMenu *menu = GTK_MENU (widget);
2651 GtkMenuPrivate *priv = menu->priv;
2653 menu_grab_transfer_window_destroy (menu);
2655 gdk_window_set_user_data (priv->view_window, NULL);
2656 gdk_window_destroy (priv->view_window);
2657 priv->view_window = NULL;
2659 gdk_window_set_user_data (priv->bin_window, NULL);
2660 gdk_window_destroy (priv->bin_window);
2661 priv->bin_window = NULL;
2663 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2667 calculate_line_heights (GtkMenu *menu,
2669 guint **ret_min_heights,
2670 guint **ret_nat_heights)
2673 GtkMenuPrivate *priv;
2674 GtkMenuShell *menu_shell;
2675 GtkWidget *child, *widget;
2677 guint horizontal_padding;
2686 widget = GTK_WIDGET (menu);
2687 menu_shell = GTK_MENU_SHELL (widget);
2689 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2690 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2691 n_heights = gtk_menu_get_n_rows (menu);
2692 n_columns = gtk_menu_get_n_columns (menu);
2693 avail_width = for_width - (2 * priv->toggle_size + priv->accel_size) * n_columns;
2695 gtk_widget_style_get (GTK_WIDGET (menu),
2696 "horizontal-padding", &horizontal_padding,
2698 get_menu_padding (widget, &padding);
2700 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2701 avail_width -= (border_width + horizontal_padding) * 2 + padding.left + padding.right;
2703 for (children = menu_shell->priv->children; children; children = children->next)
2708 gint child_min, child_nat;
2710 child = children->data;
2712 if (!gtk_widget_get_visible (child))
2715 get_effective_child_attach (child, &l, &r, &t, &b);
2717 part = avail_width / (r - l);
2719 gtk_widget_get_preferred_height_for_width (child, part,
2720 &child_min, &child_nat);
2722 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2724 part = MAX (child_min, toggle_size) / (b - t);
2725 min_heights[t] = MAX (min_heights[t], part);
2727 part = MAX (child_nat, toggle_size) / (b - t);
2728 nat_heights[t] = MAX (nat_heights[t], part);
2731 if (ret_min_heights)
2732 *ret_min_heights = min_heights;
2734 g_free (min_heights);
2736 if (ret_nat_heights)
2737 *ret_nat_heights = nat_heights;
2739 g_free (nat_heights);
2745 gtk_menu_size_allocate (GtkWidget *widget,
2746 GtkAllocation *allocation)
2749 GtkMenuPrivate *priv;
2750 GtkMenuShell *menu_shell;
2752 GtkAllocation child_allocation;
2757 guint vertical_padding;
2758 guint horizontal_padding;
2761 g_return_if_fail (GTK_IS_MENU (widget));
2762 g_return_if_fail (allocation != NULL);
2764 menu = GTK_MENU (widget);
2765 menu_shell = GTK_MENU_SHELL (widget);
2768 gtk_widget_set_allocation (widget, allocation);
2770 gtk_widget_style_get (GTK_WIDGET (menu),
2771 "vertical-padding", &vertical_padding,
2772 "horizontal-padding", &horizontal_padding,
2775 get_menu_padding (widget, &padding);
2776 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2778 g_free (priv->heights);
2779 priv->heights_length = calculate_line_heights (menu,
2784 /* refresh our cached height request */
2785 priv->requested_height = (2 * (border_width + vertical_padding)) +
2786 padding.top + padding.bottom;
2787 for (i = 0; i < priv->heights_length; i++)
2788 priv->requested_height += priv->heights[i];
2790 x = border_width + padding.left + horizontal_padding;
2791 y = border_width + padding.top + vertical_padding;
2792 width = allocation->width - (2 * (border_width + horizontal_padding)) -
2793 padding.left - padding.right;
2794 height = allocation->height - (2 * (border_width + vertical_padding)) -
2795 padding.top - padding.bottom;
2797 if (menu_shell->priv->active)
2798 gtk_menu_scroll_to (menu, priv->scroll_offset);
2800 if (!priv->tearoff_active)
2802 GtkBorder arrow_border;
2804 get_arrows_border (menu, &arrow_border);
2805 y += arrow_border.top;
2806 height -= arrow_border.top;
2807 height -= arrow_border.bottom;
2810 width = MAX (1, width);
2811 height = MAX (1, height);
2813 if (gtk_widget_get_realized (widget))
2815 gdk_window_move_resize (gtk_widget_get_window (widget),
2816 allocation->x, allocation->y,
2817 allocation->width, allocation->height);
2819 gdk_window_move_resize (priv->view_window, x, y, width, height);
2822 if (menu_shell->priv->children)
2824 gint base_width = width / gtk_menu_get_n_columns (menu);
2826 children = menu_shell->priv->children;
2829 child = children->data;
2830 children = children->next;
2832 if (gtk_widget_get_visible (child))
2837 get_effective_child_attach (child, &l, &r, &t, &b);
2839 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2842 tmp = gtk_menu_get_n_columns (menu) - l;
2843 l = gtk_menu_get_n_columns (menu) - r;
2847 child_allocation.width = (r - l) * base_width;
2848 child_allocation.height = 0;
2849 child_allocation.x = l * base_width;
2850 child_allocation.y = 0;
2852 for (i = 0; i < b; i++)
2855 child_allocation.y += priv->heights[i];
2857 child_allocation.height += priv->heights[i];
2860 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2863 gtk_widget_size_allocate (child, &child_allocation);
2864 gtk_widget_queue_draw (child);
2868 /* Resize the item window */
2869 if (gtk_widget_get_realized (widget))
2875 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2876 height += priv->heights[i];
2878 width = gtk_menu_get_n_columns (menu) * base_width;
2879 gdk_window_resize (priv->bin_window, width, height);
2882 if (priv->tearoff_active)
2884 if (height >= priv->requested_height)
2886 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2888 gtk_widget_hide (priv->tearoff_scrollbar);
2889 gtk_menu_set_tearoff_hints (menu, allocation->width);
2891 gtk_menu_scroll_to (menu, 0);
2896 gtk_adjustment_configure (priv->tearoff_adjustment,
2897 gtk_adjustment_get_value (priv->tearoff_adjustment),
2899 priv->requested_height,
2900 gtk_adjustment_get_step_increment (priv->tearoff_adjustment),
2901 gtk_adjustment_get_page_increment (priv->tearoff_adjustment),
2902 allocation->height);
2904 if (!gtk_widget_get_visible (priv->tearoff_scrollbar))
2906 gtk_widget_show (priv->tearoff_scrollbar);
2907 gtk_menu_set_tearoff_hints (menu, allocation->width);
2915 get_arrows_visible_area (GtkMenu *menu,
2916 GdkRectangle *border,
2917 GdkRectangle *upper,
2918 GdkRectangle *lower,
2921 GtkArrowPlacement arrow_placement;
2922 GtkWidget *widget = GTK_WIDGET (menu);
2924 guint vertical_padding;
2925 guint horizontal_padding;
2926 gint scroll_arrow_height;
2927 GtkBorder menu_padding;
2929 gtk_widget_style_get (widget,
2930 "vertical-padding", &vertical_padding,
2931 "horizontal-padding", &horizontal_padding,
2932 "scroll-arrow-vlength", &scroll_arrow_height,
2933 "arrow-placement", &arrow_placement,
2936 get_menu_padding (widget, &menu_padding);
2937 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2938 border->x = border_width + menu_padding.left + horizontal_padding;
2939 border->y = border_width + menu_padding.top + vertical_padding;
2940 border->width = gdk_window_get_width (gtk_widget_get_window (widget));
2941 border->height = gdk_window_get_height (gtk_widget_get_window (widget));
2943 switch (arrow_placement)
2945 case GTK_ARROWS_BOTH:
2946 upper->x = border->x;
2947 upper->y = border->y;
2948 upper->width = border->width - 2 * border->x;
2949 upper->height = scroll_arrow_height;
2951 lower->x = border->x;
2952 lower->y = border->height - border->y - scroll_arrow_height;
2953 lower->width = border->width - 2 * border->x;
2954 lower->height = scroll_arrow_height;
2957 case GTK_ARROWS_START:
2958 upper->x = border->x;
2959 upper->y = border->y;
2960 upper->width = (border->width - 2 * border->x) / 2;
2961 upper->height = scroll_arrow_height;
2963 lower->x = border->x + upper->width;
2964 lower->y = border->y;
2965 lower->width = (border->width - 2 * border->x) / 2;
2966 lower->height = scroll_arrow_height;
2969 case GTK_ARROWS_END:
2970 upper->x = border->x;
2971 upper->y = border->height - border->y - scroll_arrow_height;
2972 upper->width = (border->width - 2 * border->x) / 2;
2973 upper->height = scroll_arrow_height;
2975 lower->x = border->x + upper->width;
2976 lower->y = border->height - border->y - scroll_arrow_height;
2977 lower->width = (border->width - 2 * border->x) / 2;
2978 lower->height = scroll_arrow_height;
2982 g_assert_not_reached();
2983 upper->x = upper->y = upper->width = upper->height = 0;
2984 lower->x = lower->y = lower->width = lower->height = 0;
2987 *arrow_space = scroll_arrow_height - menu_padding.top - menu_padding.bottom;
2991 gtk_menu_draw (GtkWidget *widget,
2995 GtkMenuPrivate *priv;
2996 GtkStyleContext *context;
2997 GdkRectangle border;
3001 GtkBorder menu_padding;
3003 menu = GTK_MENU (widget);
3005 context = gtk_widget_get_style_context (widget);
3007 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
3008 get_menu_padding (widget, &menu_padding);
3010 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
3012 gfloat arrow_scaling;
3015 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
3016 arrow_size = arrow_scaling * arrow_space;
3018 gtk_render_background (context, cr, 0, 0,
3019 gtk_widget_get_allocated_width (widget),
3020 gtk_widget_get_allocated_height (widget));
3021 gtk_render_frame (context, cr, 0, 0,
3022 gtk_widget_get_allocated_width (widget),
3023 gtk_widget_get_allocated_height (widget));
3025 gtk_style_context_save (context);
3026 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
3028 if (priv->upper_arrow_visible && !priv->tearoff_active)
3030 gtk_style_context_save (context);
3031 gtk_style_context_set_state (context, priv->upper_arrow_state);
3033 gtk_render_background (context, cr,
3035 upper.width, upper.height);
3036 gtk_render_frame (context, cr,
3038 upper.width, upper.height);
3040 gtk_render_arrow (context, cr, 0,
3041 upper.x + (upper.width - arrow_size) / 2,
3042 upper.y + menu_padding.top + (arrow_space - arrow_size) / 2,
3045 gtk_style_context_restore (context);
3048 if (priv->lower_arrow_visible && !priv->tearoff_active)
3050 gtk_style_context_save (context);
3051 gtk_style_context_set_state (context, priv->lower_arrow_state);
3053 gtk_render_background (context, cr,
3055 lower.width, lower.height);
3056 gtk_render_frame (context, cr,
3058 lower.width, lower.height);
3060 gtk_render_arrow (context, cr, G_PI,
3061 lower.x + (lower.width - arrow_size) / 2,
3062 lower.y + menu_padding.bottom + (arrow_space - arrow_size) / 2,
3065 gtk_style_context_restore (context);
3068 gtk_style_context_restore (context);
3071 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
3073 gint y = -border.y + priv->scroll_offset;
3076 gtk_cairo_transform_to_window (cr, widget, priv->bin_window);
3078 if (!priv->tearoff_active)
3080 GtkBorder arrow_border;
3082 get_arrows_border (menu, &arrow_border);
3083 y -= arrow_border.top;
3086 gtk_render_background (context, cr,
3088 border.width, border.height);
3089 gtk_render_frame (context, cr,
3091 border.width, border.height);
3096 GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
3102 gtk_menu_show (GtkWidget *widget)
3104 GtkMenu *menu = GTK_MENU (widget);
3106 _gtk_menu_refresh_accel_paths (menu, FALSE);
3108 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3113 gtk_menu_get_preferred_width (GtkWidget *widget,
3118 GtkMenuShell *menu_shell;
3119 GtkMenuPrivate *priv;
3122 guint max_toggle_size;
3123 guint max_accel_width;
3124 guint horizontal_padding;
3126 gint child_min, child_nat;
3127 gint min_width, nat_width;
3130 menu = GTK_MENU (widget);
3131 menu_shell = GTK_MENU_SHELL (widget);
3134 min_width = nat_width = 0;
3136 max_toggle_size = 0;
3137 max_accel_width = 0;
3139 children = menu_shell->priv->children;
3146 child = children->data;
3147 children = children->next;
3149 if (! gtk_widget_get_visible (child))
3152 get_effective_child_attach (child, &l, &r, &t, &b);
3154 /* It's important to size_request the child
3155 * before doing the toggle size request, in
3156 * case the toggle size request depends on the size
3157 * request of a child of the child (e.g. for ImageMenuItem)
3159 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
3161 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3162 max_toggle_size = MAX (max_toggle_size, toggle_size);
3163 max_accel_width = MAX (max_accel_width,
3164 GTK_MENU_ITEM (child)->priv->accelerator_width);
3166 part = child_min / (r - l);
3167 min_width = MAX (min_width, part);
3169 part = child_nat / (r - l);
3170 nat_width = MAX (nat_width, part);
3173 /* If the menu doesn't include any images or check items
3174 * reserve the space so that all menus are consistent.
3175 * We only do this for 'ordinary' menus, not for combobox
3176 * menus or multi-column menus
3178 if (max_toggle_size == 0 &&
3179 gtk_menu_get_n_columns (menu) == 1 &&
3180 !priv->no_toggle_size)
3182 GtkStyleContext *context;
3183 GtkWidgetPath *menu_path, *check_path;
3184 guint toggle_spacing;
3185 guint indicator_size;
3187 context = gtk_widget_get_style_context (widget);
3188 menu_path = gtk_widget_path_copy (gtk_style_context_get_path (context));
3190 /* Create a GtkCheckMenuItem path, only to query indicator spacing */
3191 check_path = gtk_widget_path_copy (menu_path);
3192 gtk_widget_path_append_type (check_path, GTK_TYPE_CHECK_MENU_ITEM);
3194 gtk_style_context_set_path (context, check_path);
3195 gtk_widget_path_free (check_path);
3197 gtk_style_context_get_style (context,
3198 "toggle-spacing", &toggle_spacing,
3199 "indicator-size", &indicator_size,
3202 max_toggle_size = indicator_size + toggle_spacing;
3204 /* Restore real widget path */
3205 gtk_style_context_set_path (context, menu_path);
3206 gtk_widget_path_free (menu_path);
3209 min_width += 2 * max_toggle_size + max_accel_width;
3210 min_width *= gtk_menu_get_n_columns (menu);
3212 nat_width += 2 * max_toggle_size + max_accel_width;
3213 nat_width *= gtk_menu_get_n_columns (menu);
3215 gtk_widget_style_get (GTK_WIDGET (menu),
3216 "horizontal-padding", &horizontal_padding,
3219 get_menu_padding (widget, &padding);
3220 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3221 min_width += (2 * (border_width + horizontal_padding)) +
3222 padding.left + padding.right;
3223 nat_width += (2 * (border_width + horizontal_padding)) +
3224 padding.left + padding.right;
3226 priv->toggle_size = max_toggle_size;
3227 priv->accel_size = max_accel_width;
3230 *minimum_size = min_width;
3233 *natural_size = nat_width;
3235 /* Don't resize the tearoff if it is not active,
3236 * because it won't redraw (it is only a background pixmap).
3238 if (priv->tearoff_active)
3239 gtk_menu_set_tearoff_hints (menu, min_width);
3243 gtk_menu_get_preferred_height (GtkWidget *widget,
3249 /* Menus are height-for-width only, just return the height
3250 * for the minimum width
3252 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
3253 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
3257 gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
3263 GtkMenu *menu = GTK_MENU (widget);
3264 GtkMenuPrivate *priv = menu->priv;
3265 guint *min_heights, *nat_heights;
3266 guint vertical_padding, border_width;
3268 gint min_height, nat_height;
3270 gtk_widget_style_get (widget, "vertical-padding", &vertical_padding, NULL);
3271 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3272 get_menu_padding (widget, &padding);
3274 min_height = nat_height = (border_width + vertical_padding) * 2 +
3275 padding.top + padding.bottom;
3278 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3280 for (i = 0; i < n_heights; i++)
3282 min_height += min_heights[i];
3283 nat_height += nat_heights[i];
3286 if (priv->have_position)
3288 GdkScreen *screen = gtk_widget_get_screen (priv->toplevel);
3289 GdkRectangle monitor;
3291 gdk_screen_get_monitor_workarea (screen, priv->monitor_num, &monitor);
3293 if (priv->position_y + min_height > monitor.y + monitor.height)
3294 min_height = monitor.y + monitor.height - priv->position_y;
3296 if (priv->position_y + nat_height > monitor.y + monitor.height)
3297 nat_height = monitor.y + monitor.height - priv->position_y;
3299 if (priv->position_y < monitor.y)
3301 min_height -= monitor.y - priv->position_y;
3302 nat_height -= monitor.y - priv->position_y;
3307 *minimum_size = min_height;
3310 *natural_size = nat_height;
3312 g_free (min_heights);
3313 g_free (nat_heights);
3319 gtk_menu_button_scroll (GtkMenu *menu,
3320 GdkEventButton *event)
3322 GtkMenuPrivate *priv = menu->priv;
3324 if (priv->upper_arrow_prelight || priv->lower_arrow_prelight)
3326 gboolean touchscreen_mode;
3328 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3329 "gtk-touchscreen-mode", &touchscreen_mode,
3332 if (touchscreen_mode)
3333 gtk_menu_handle_scrolling (menu,
3334 event->x_root, event->y_root,
3335 event->type == GDK_BUTTON_PRESS,
3345 pointer_in_menu_window (GtkWidget *widget,
3349 GtkMenu *menu = GTK_MENU (widget);
3350 GtkMenuPrivate *priv = menu->priv;
3351 GtkAllocation allocation;
3353 if (gtk_widget_get_mapped (priv->toplevel))
3355 GtkMenuShell *menu_shell;
3356 gint window_x, window_y;
3358 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
3359 &window_x, &window_y);
3361 gtk_widget_get_allocation (widget, &allocation);
3362 if (x_root >= window_x && x_root < window_x + allocation.width &&
3363 y_root >= window_y && y_root < window_y + allocation.height)
3366 menu_shell = GTK_MENU_SHELL (widget);
3368 if (GTK_IS_MENU (menu_shell->priv->parent_menu_shell))
3369 return pointer_in_menu_window (menu_shell->priv->parent_menu_shell,
3377 gtk_menu_button_press (GtkWidget *widget,
3378 GdkEventButton *event)
3380 if (event->type != GDK_BUTTON_PRESS)
3383 /* Don't pass down to menu shell for presses over scroll arrows
3385 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3388 /* Don't pass down to menu shell if a non-menuitem part of the menu
3389 * was clicked. The check for the event_widget being a GtkMenuShell
3390 * works because we have the pointer grabbed on menu_shell->window
3391 * with owner_events=TRUE, so all events that are either outside
3392 * the menu or on its border are delivered relative to
3393 * menu_shell->window.
3395 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3396 pointer_in_menu_window (widget, event->x_root, event->y_root))
3399 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3403 gtk_menu_button_release (GtkWidget *widget,
3404 GdkEventButton *event)
3406 GtkMenuPrivate *priv = GTK_MENU (widget)->priv;
3408 if (priv->ignore_button_release)
3410 priv->ignore_button_release = FALSE;
3414 if (event->type != GDK_BUTTON_RELEASE)
3417 /* Don't pass down to menu shell for releases 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 (see comment in button_press()).
3425 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3426 pointer_in_menu_window (widget, event->x_root, event->y_root))
3428 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3429 * bail out early here so it is in a consistent state for the
3430 * next button_press/button_release in GtkMenuShell.
3433 if (GTK_MENU_SHELL (widget)->priv->active)
3434 GTK_MENU_SHELL (widget)->priv->button = 0;
3439 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3442 static const gchar *
3443 get_accel_path (GtkWidget *menu_item,
3448 GClosure *accel_closure;
3449 GtkAccelGroup *accel_group;
3451 path = _gtk_widget_get_accel_path (menu_item, locked);
3454 path = GTK_MENU_ITEM (menu_item)->priv->accel_path;
3460 label = gtk_bin_get_child (GTK_BIN (menu_item));
3462 if (GTK_IS_ACCEL_LABEL (label))
3464 g_object_get (label,
3465 "accel-closure", &accel_closure,
3469 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3471 *locked = gtk_accel_group_get_is_locked (accel_group);
3481 gtk_menu_key_press (GtkWidget *widget,
3484 GtkMenuShell *menu_shell;
3486 gboolean delete = FALSE;
3487 gboolean can_change_accels;
3488 gchar *accel = NULL;
3489 guint accel_key, accel_mods;
3490 GdkModifierType consumed_modifiers;
3491 GdkDisplay *display;
3493 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3494 g_return_val_if_fail (event != NULL, FALSE);
3496 menu_shell = GTK_MENU_SHELL (widget);
3497 menu = GTK_MENU (widget);
3499 gtk_menu_stop_navigating_submenu (menu);
3501 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3504 display = gtk_widget_get_display (widget);
3506 g_object_get (gtk_widget_get_settings (widget),
3507 "gtk-menu-bar-accel", &accel,
3508 "gtk-can-change-accels", &can_change_accels,
3511 if (accel && *accel)
3514 GdkModifierType mods = 0;
3516 gtk_accelerator_parse (accel, &keyval, &mods);
3519 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3521 /* FIXME this is wrong, needs to be in the global accel resolution
3522 * thing, to properly consider i18n etc., but that probably requires
3523 * AccelGroup changes etc.
3525 if (event->keyval == keyval && (mods & event->state) == mods)
3527 gtk_menu_shell_cancel (menu_shell);
3535 switch (event->keyval)
3537 case GDK_KEY_Delete:
3538 case GDK_KEY_KP_Delete:
3539 case GDK_KEY_BackSpace:
3546 /* Figure out what modifiers went into determining the key symbol */
3547 _gtk_translate_keyboard_accel_state (gdk_keymap_get_for_display (display),
3548 event->hardware_keycode,
3550 gtk_accelerator_get_default_mod_mask (),
3552 &accel_key, NULL, NULL, &consumed_modifiers);
3554 accel_key = gdk_keyval_to_lower (accel_key);
3555 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3557 /* If lowercasing affects the keysym, then we need to include SHIFT
3558 * in the modifiers, We re-upper case when we match against the
3559 * keyval, but display and save in caseless form.
3561 if (accel_key != event->keyval)
3562 accel_mods |= GDK_SHIFT_MASK;
3564 /* Modify the accelerators */
3565 if (can_change_accels &&
3566 menu_shell->priv->active_menu_item &&
3567 gtk_bin_get_child (GTK_BIN (menu_shell->priv->active_menu_item)) && /* no separators */
3568 GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu == NULL && /* no submenus */
3569 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3571 GtkWidget *menu_item = menu_shell->priv->active_menu_item;
3572 gboolean locked, replace_accels = TRUE;
3575 path = get_accel_path (menu_item, &locked);
3576 if (!path || locked)
3578 /* Can't change accelerators on menu_items without paths
3579 * (basically, those items are accelerator-locked).
3581 gtk_widget_error_bell (widget);
3587 /* For the keys that act to delete the current setting,
3588 * we delete the current setting if there is one, otherwise,
3589 * we set the key as the accelerator.
3595 if (gtk_accel_map_lookup_entry (path, &key) &&
3596 (key.accel_key || key.accel_mods))
3602 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3606 /* We failed, probably because this key is in use
3607 * and locked already.
3609 gtk_widget_error_bell (widget);
3618 check_threshold (GtkWidget *widget,
3627 ABS (start_x - x) > THRESHOLD ||
3628 ABS (start_y - y) > THRESHOLD;
3632 definitely_within_item (GtkWidget *widget,
3636 GdkWindow *window = GTK_MENU_ITEM (widget)->priv->event_window;
3639 w = gdk_window_get_width (window);
3640 h = gdk_window_get_height (window);
3643 check_threshold (widget, 0, 0, x, y) &&
3644 check_threshold (widget, w - 1, 0, x, y) &&
3645 check_threshold (widget, w - 1, h - 1, x, y) &&
3646 check_threshold (widget, 0, h - 1, x, y);
3650 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3652 GtkMenuPrivate *priv = menu->priv;
3654 return priv->navigation_height && priv->navigation_width;
3658 gtk_menu_motion_notify (GtkWidget *widget,
3659 GdkEventMotion *event)
3661 GtkWidget *menu_item;
3663 GtkMenuShell *menu_shell;
3666 gboolean need_enter;
3668 if (GTK_IS_MENU (widget))
3670 GtkMenuPrivate *priv = GTK_MENU(widget)->priv;
3672 if (priv->ignore_button_release)
3673 priv->ignore_button_release = FALSE;
3675 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3679 /* We received the event for one of two reasons:
3681 * a) We are the active menu, and did gtk_grab_add()
3682 * b) The widget is a child of ours, and the event was propagated
3684 * Since for computation of navigation regions, we want the menu which
3685 * is the parent of the menu item, for a), we need to find that menu,
3686 * which may be different from 'widget'.
3688 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3689 parent = gtk_widget_get_parent (menu_item);
3690 if (!GTK_IS_MENU_ITEM (menu_item) ||
3691 !GTK_IS_MENU (parent))
3694 menu_shell = GTK_MENU_SHELL (parent);
3695 menu = GTK_MENU (menu_shell);
3697 if (definitely_within_item (menu_item, event->x, event->y))
3698 menu_shell->priv->activate_time = 0;
3700 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->priv->ignore_enter);
3702 /* Check to see if we are within an active submenu's navigation region
3704 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3707 /* Make sure we pop down if we enter a non-selectable menu item, so we
3708 * don't show a submenu when the cursor is outside the stay-up triangle.
3710 if (!_gtk_menu_item_is_selectable (menu_item))
3712 /* We really want to deselect, but this gives the menushell code
3713 * a chance to do some bookkeeping about the menuitem.
3715 gtk_menu_shell_select_item (menu_shell, menu_item);
3721 /* The menu is now sensitive to enter events on its items, but
3722 * was previously sensitive. So we fake an enter event.
3724 menu_shell->priv->ignore_enter = FALSE;
3726 if (event->x >= 0 && event->x < gdk_window_get_width (event->window) &&
3727 event->y >= 0 && event->y < gdk_window_get_height (event->window))
3729 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3732 send_event->crossing.window = g_object_ref (event->window);
3733 send_event->crossing.time = event->time;
3734 send_event->crossing.send_event = TRUE;
3735 send_event->crossing.x_root = event->x_root;
3736 send_event->crossing.y_root = event->y_root;
3737 send_event->crossing.x = event->x;
3738 send_event->crossing.y = event->y;
3739 send_event->crossing.state = event->state;
3740 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3742 /* We send the event to 'widget', the currently active menu,
3743 * instead of 'menu', the menu that the pointer is in. This
3744 * will ensure that the event will be ignored unless the
3745 * menuitem is a child of the active menu or some parent
3746 * menu of the active menu.
3748 result = gtk_widget_event (widget, send_event);
3749 gdk_event_free (send_event);
3759 get_double_arrows (GtkMenu *menu)
3761 GtkMenuPrivate *priv = menu->priv;
3762 gboolean double_arrows;
3763 GtkArrowPlacement arrow_placement;
3765 gtk_widget_style_get (GTK_WIDGET (menu),
3766 "double-arrows", &double_arrows,
3767 "arrow-placement", &arrow_placement,
3770 if (arrow_placement != GTK_ARROWS_BOTH)
3773 return double_arrows || (priv->initially_pushed_in &&
3774 priv->scroll_offset != 0);
3778 gtk_menu_scroll_by (GtkMenu *menu,
3781 GtkMenuPrivate *priv = menu->priv;
3782 GtkBorder arrow_border;
3786 gboolean double_arrows;
3788 widget = GTK_WIDGET (menu);
3789 offset = priv->scroll_offset + step;
3791 get_arrows_border (menu, &arrow_border);
3793 double_arrows = get_double_arrows (menu);
3795 /* If we scroll upward and the non-visible top part
3796 * is smaller than the scroll arrow it would be
3797 * pretty stupid to show the arrow and taking more
3798 * screen space than just scrolling to the top.
3801 if ((step < 0) && (offset < arrow_border.top))
3804 /* Don't scroll over the top if we weren't before: */
3805 if ((priv->scroll_offset >= 0) && (offset < 0))
3808 view_height = gdk_window_get_height (gtk_widget_get_window (widget));
3810 if (priv->scroll_offset == 0 &&
3811 view_height >= priv->requested_height)
3814 /* Don't scroll past the bottom if we weren't before: */
3815 if (priv->scroll_offset > 0)
3816 view_height -= arrow_border.top;
3818 /* When both arrows are always shown,
3819 * reduce view height even more.
3822 view_height -= arrow_border.bottom;
3824 if ((priv->scroll_offset + view_height <= priv->requested_height) &&
3825 (offset + view_height > priv->requested_height))
3826 offset = priv->requested_height - view_height;
3828 if (offset != priv->scroll_offset)
3829 gtk_menu_scroll_to (menu, offset);
3833 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3834 gboolean touchscreen_mode)
3836 GtkMenuPrivate *priv = menu->priv;
3837 gboolean upper_visible;
3838 gboolean lower_visible;
3840 upper_visible = priv->upper_arrow_visible;
3841 lower_visible = priv->lower_arrow_visible;
3843 gtk_menu_scroll_by (menu, priv->scroll_step);
3845 if (touchscreen_mode &&
3846 (upper_visible != priv->upper_arrow_visible ||
3847 lower_visible != priv->lower_arrow_visible))
3849 /* We are about to hide a scroll arrow while the mouse is pressed,
3850 * this would cause the uncovered menu item to be activated on button
3851 * release. Therefore we need to ignore button release here
3853 GTK_MENU_SHELL (menu)->priv->ignore_enter = TRUE;
3854 priv->ignore_button_release = TRUE;
3859 gtk_menu_scroll_timeout (gpointer data)
3862 gboolean touchscreen_mode;
3864 menu = GTK_MENU (data);
3866 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3867 "gtk-touchscreen-mode", &touchscreen_mode,
3870 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3876 gtk_menu_scroll_timeout_initial (gpointer data)
3880 gboolean touchscreen_mode;
3882 menu = GTK_MENU (data);
3884 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3885 "gtk-timeout-repeat", &timeout,
3886 "gtk-touchscreen-mode", &touchscreen_mode,
3889 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3891 gtk_menu_remove_scroll_timeout (menu);
3893 menu->priv->scroll_timeout =
3894 gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout, menu);
3900 gtk_menu_start_scrolling (GtkMenu *menu)
3903 gboolean touchscreen_mode;
3905 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3906 "gtk-timeout-repeat", &timeout,
3907 "gtk-touchscreen-mode", &touchscreen_mode,
3910 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3912 menu->priv->scroll_timeout =
3913 gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout_initial, menu);
3917 gtk_menu_scroll (GtkWidget *widget,
3918 GdkEventScroll *event)
3920 GtkMenu *menu = GTK_MENU (widget);
3922 switch (event->direction)
3924 case GDK_SCROLL_RIGHT:
3925 case GDK_SCROLL_DOWN:
3926 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3928 case GDK_SCROLL_LEFT:
3930 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3938 get_arrows_sensitive_area (GtkMenu *menu,
3939 GdkRectangle *upper,
3940 GdkRectangle *lower)
3942 GtkArrowPlacement arrow_placement;
3943 GtkWidget *widget = GTK_WIDGET (menu);
3947 guint vertical_padding;
3949 gint scroll_arrow_height;
3952 window = gtk_widget_get_window (widget);
3953 width = gdk_window_get_width (window);
3954 height = gdk_window_get_height (window);
3956 gtk_widget_style_get (widget,
3957 "vertical-padding", &vertical_padding,
3958 "scroll-arrow-vlength", &scroll_arrow_height,
3959 "arrow-placement", &arrow_placement,
3962 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) + vertical_padding;
3963 get_menu_padding (widget, &padding);
3965 gdk_window_get_position (window, &win_x, &win_y);
3967 switch (arrow_placement)
3969 case GTK_ARROWS_BOTH:
3974 upper->width = width;
3975 upper->height = scroll_arrow_height + border + padding.top;
3981 lower->y = win_y + height - border - padding.bottom - scroll_arrow_height;
3982 lower->width = width;
3983 lower->height = scroll_arrow_height + border + padding.bottom;
3987 case GTK_ARROWS_START:
3992 upper->width = width / 2;
3993 upper->height = scroll_arrow_height + border + padding.top;
3998 lower->x = win_x + width / 2;
4000 lower->width = width / 2;
4001 lower->height = scroll_arrow_height + border + padding.bottom;
4005 case GTK_ARROWS_END:
4009 upper->y = win_y + height - border - scroll_arrow_height;
4010 upper->width = width / 2;
4011 upper->height = scroll_arrow_height + border + padding.top;
4016 lower->x = win_x + width / 2;
4017 lower->y = win_y + height - border - scroll_arrow_height;
4018 lower->width = width / 2;
4019 lower->height = scroll_arrow_height + border + padding.bottom;
4027 gtk_menu_handle_scrolling (GtkMenu *menu,
4033 GtkMenuPrivate *priv = menu->priv;
4034 GtkMenuShell *menu_shell;
4037 gboolean scroll_fast = FALSE;
4039 gboolean touchscreen_mode;
4041 menu_shell = GTK_MENU_SHELL (menu);
4043 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4044 "gtk-touchscreen-mode", &touchscreen_mode,
4047 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
4052 /* upper arrow handling */
4054 get_arrows_sensitive_area (menu, &rect, NULL);
4057 if (priv->upper_arrow_visible && !priv->tearoff_active &&
4058 (x >= rect.x) && (x < rect.x + rect.width) &&
4059 (y >= rect.y) && (y < rect.y + rect.height))
4064 if (touchscreen_mode)
4065 priv->upper_arrow_prelight = in_arrow;
4067 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4069 gboolean arrow_pressed = FALSE;
4071 if (priv->upper_arrow_visible && !priv->tearoff_active)
4073 if (touchscreen_mode)
4075 if (enter && priv->upper_arrow_prelight)
4077 if (priv->scroll_timeout == 0)
4079 /* Deselect the active item so that
4080 * any submenus are popped down
4082 gtk_menu_shell_deselect (menu_shell);
4084 gtk_menu_remove_scroll_timeout (menu);
4085 priv->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
4089 /* Only do stuff on click. */
4090 gtk_menu_start_scrolling (menu);
4091 arrow_pressed = TRUE;
4096 arrow_pressed = TRUE;
4101 gtk_menu_stop_scrolling (menu);
4104 else /* !touchscreen_mode */
4106 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
4108 if (enter && in_arrow &&
4109 (!priv->upper_arrow_prelight ||
4110 priv->scroll_fast != scroll_fast))
4112 priv->upper_arrow_prelight = TRUE;
4113 priv->scroll_fast = scroll_fast;
4115 /* Deselect the active item so that
4116 * any submenus are popped down
4118 gtk_menu_shell_deselect (menu_shell);
4120 gtk_menu_remove_scroll_timeout (menu);
4121 priv->scroll_step = scroll_fast
4122 ? -MENU_SCROLL_STEP2
4123 : -MENU_SCROLL_STEP1;
4125 priv->scroll_timeout =
4126 gdk_threads_add_timeout (scroll_fast
4127 ? MENU_SCROLL_TIMEOUT2
4128 : MENU_SCROLL_TIMEOUT1,
4129 gtk_menu_scroll_timeout, menu);
4131 else if (!enter && !in_arrow && priv->upper_arrow_prelight)
4133 gtk_menu_stop_scrolling (menu);
4138 /* gtk_menu_start_scrolling() might have hit the top of the
4139 * menu, so check if the button isn't insensitive before
4140 * changing it to something else.
4142 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4144 GtkStateFlags arrow_state = 0;
4147 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4149 if (priv->upper_arrow_prelight)
4150 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4152 if (arrow_state != priv->upper_arrow_state)
4154 priv->upper_arrow_state = arrow_state;
4156 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4162 /* lower arrow handling */
4164 get_arrows_sensitive_area (menu, NULL, &rect);
4167 if (priv->lower_arrow_visible && !priv->tearoff_active &&
4168 (x >= rect.x) && (x < rect.x + rect.width) &&
4169 (y >= rect.y) && (y < rect.y + rect.height))
4174 if (touchscreen_mode)
4175 priv->lower_arrow_prelight = in_arrow;
4177 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4179 gboolean arrow_pressed = FALSE;
4181 if (priv->lower_arrow_visible && !priv->tearoff_active)
4183 if (touchscreen_mode)
4185 if (enter && priv->lower_arrow_prelight)
4187 if (priv->scroll_timeout == 0)
4189 /* Deselect the active item so that
4190 * any submenus are popped down
4192 gtk_menu_shell_deselect (menu_shell);
4194 gtk_menu_remove_scroll_timeout (menu);
4195 priv->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4199 /* Only do stuff on click. */
4200 gtk_menu_start_scrolling (menu);
4201 arrow_pressed = TRUE;
4206 arrow_pressed = TRUE;
4211 gtk_menu_stop_scrolling (menu);
4214 else /* !touchscreen_mode */
4216 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4218 if (enter && in_arrow &&
4219 (!priv->lower_arrow_prelight ||
4220 priv->scroll_fast != scroll_fast))
4222 priv->lower_arrow_prelight = TRUE;
4223 priv->scroll_fast = scroll_fast;
4225 /* Deselect the active item so that
4226 * any submenus are popped down
4228 gtk_menu_shell_deselect (menu_shell);
4230 gtk_menu_remove_scroll_timeout (menu);
4231 priv->scroll_step = scroll_fast
4233 : MENU_SCROLL_STEP1;
4235 priv->scroll_timeout =
4236 gdk_threads_add_timeout (scroll_fast
4237 ? MENU_SCROLL_TIMEOUT2
4238 : MENU_SCROLL_TIMEOUT1,
4239 gtk_menu_scroll_timeout, menu);
4241 else if (!enter && !in_arrow && priv->lower_arrow_prelight)
4243 gtk_menu_stop_scrolling (menu);
4248 /* gtk_menu_start_scrolling() might have hit the bottom of the
4249 * menu, so check if the button isn't insensitive before
4250 * changing it to something else.
4252 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4254 GtkStateFlags arrow_state = 0;
4257 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4259 if (priv->lower_arrow_prelight)
4260 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4262 if (arrow_state != priv->lower_arrow_state)
4264 priv->lower_arrow_state = arrow_state;
4266 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4274 gtk_menu_enter_notify (GtkWidget *widget,
4275 GdkEventCrossing *event)
4277 GtkWidget *menu_item;
4279 gboolean touchscreen_mode;
4281 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4282 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4283 event->mode == GDK_CROSSING_STATE_CHANGED)
4286 g_object_get (gtk_widget_get_settings (widget),
4287 "gtk-touchscreen-mode", &touchscreen_mode,
4290 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4291 if (GTK_IS_MENU (widget))
4293 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4295 if (!menu_shell->priv->ignore_enter)
4296 gtk_menu_handle_scrolling (GTK_MENU (widget),
4297 event->x_root, event->y_root, TRUE, TRUE);
4300 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4302 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4304 if (GTK_IS_MENU (menu))
4306 GtkMenuPrivate *priv = (GTK_MENU (menu))->priv;
4307 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4309 if (priv->seen_item_enter)
4311 /* This is the second enter we see for an item
4312 * on this menu. This means a release should always
4315 menu_shell->priv->activate_time = 0;
4317 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4318 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4320 if (definitely_within_item (menu_item, event->x, event->y))
4322 /* This is an actual user-enter (ie. not a pop-under)
4323 * In this case, the user must either have entered
4324 * sufficiently far enough into the item, or he must move
4325 * far enough away from the enter point. (see
4326 * gtk_menu_motion_notify())
4328 menu_shell->priv->activate_time = 0;
4332 priv->seen_item_enter = TRUE;
4336 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4337 * will not correspond to the event widget's parent. Check to see
4338 * if we are in the parent's navigation region.
4340 parent = gtk_widget_get_parent (menu_item);
4341 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4342 gtk_menu_navigating_submenu (GTK_MENU (parent),
4343 event->x_root, event->y_root))
4346 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4350 gtk_menu_leave_notify (GtkWidget *widget,
4351 GdkEventCrossing *event)
4353 GtkMenuShell *menu_shell;
4355 GtkMenuItem *menu_item;
4356 GtkWidget *event_widget;
4358 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4359 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4360 event->mode == GDK_CROSSING_STATE_CHANGED)
4363 menu = GTK_MENU (widget);
4364 menu_shell = GTK_MENU_SHELL (widget);
4366 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4369 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4371 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4373 if (!GTK_IS_MENU_ITEM (event_widget))
4376 menu_item = GTK_MENU_ITEM (event_widget);
4378 /* Here we check to see if we're leaving an active menu item
4379 * with a submenu, in which case we enter submenu navigation mode.
4381 if (menu_shell->priv->active_menu_item != NULL
4382 && menu_item->priv->submenu != NULL
4383 && menu_item->priv->submenu_placement == GTK_LEFT_RIGHT)
4385 if (GTK_MENU_SHELL (menu_item->priv->submenu)->priv->active)
4387 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4390 else if (menu_item == GTK_MENU_ITEM (menu_shell->priv->active_menu_item))
4392 /* We are leaving an active menu item with nonactive submenu.
4393 * Deselect it so we don't surprise the user with by popping
4394 * up a submenu _after_ he left the item.
4396 gtk_menu_shell_deselect (menu_shell);
4401 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4405 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4407 GtkMenuPrivate *priv = menu->priv;
4409 priv->navigation_x = 0;
4410 priv->navigation_y = 0;
4411 priv->navigation_width = 0;
4412 priv->navigation_height = 0;
4414 if (priv->navigation_timeout)
4416 g_source_remove (priv->navigation_timeout);
4417 priv->navigation_timeout = 0;
4421 /* When the timeout is elapsed, the navigation region is destroyed
4422 * and the menuitem under the pointer (if any) is selected.
4425 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4427 GtkMenuPopdownData *popdown_data = user_data;
4428 GtkMenu *menu = popdown_data->menu;
4429 GtkMenuPrivate *priv = menu->priv;
4430 GdkWindow *child_window;
4432 gtk_menu_stop_navigating_submenu (menu);
4434 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4436 child_window = gdk_window_get_device_position (priv->bin_window,
4437 popdown_data->device,
4442 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4444 send_event->crossing.window = g_object_ref (child_window);
4445 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4446 send_event->crossing.send_event = TRUE;
4447 gdk_event_set_device (send_event, popdown_data->device);
4449 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4451 gdk_event_free (send_event);
4459 gtk_menu_navigating_submenu (GtkMenu *menu,
4463 GtkMenuPrivate *priv = menu->priv;
4466 if (!gtk_menu_has_navigation_triangle (menu))
4469 width = priv->navigation_width;
4470 height = priv->navigation_height;
4472 /* Check if x/y are in the triangle spanned by the navigation parameters */
4474 /* 1) Move the coordinates so the triangle starts at 0,0 */
4475 event_x -= priv->navigation_x;
4476 event_y -= priv->navigation_y;
4478 /* 2) Ensure both legs move along the positive axis */
4490 /* 3) Check that the given coordinate is inside the triangle. The formula
4491 * is a transformed form of this formula: x/w + y/h <= 1
4493 if (event_x >= 0 && event_y >= 0 &&
4494 event_x * height + event_y * width <= width * height)
4500 gtk_menu_stop_navigating_submenu (menu);
4506 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4507 GtkMenuItem *menu_item,
4508 GdkEventCrossing *event)
4510 GtkMenuPrivate *priv = menu->priv;
4511 gint submenu_left = 0;
4512 gint submenu_right = 0;
4513 gint submenu_top = 0;
4514 gint submenu_bottom = 0;
4516 GtkWidget *event_widget;
4517 GtkMenuPopdownData *popdown_data;
4520 g_return_if_fail (menu_item->priv->submenu != NULL);
4521 g_return_if_fail (event != NULL);
4523 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4525 window = gtk_widget_get_window (menu_item->priv->submenu);
4526 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4528 submenu_right = submenu_left + gdk_window_get_width (window);
4529 submenu_bottom = submenu_top + gdk_window_get_height (window);
4531 width = gdk_window_get_width (gtk_widget_get_window (event_widget));
4533 if (event->x >= 0 && event->x < width)
4537 gtk_menu_stop_navigating_submenu (menu);
4539 /* The navigation region is the triangle closest to the x/y
4540 * location of the rectangle. This is why the width or height
4543 if (menu_item->priv->submenu_direction == GTK_DIRECTION_RIGHT)
4546 priv->navigation_x = submenu_left;
4547 priv->navigation_width = event->x_root - submenu_left;
4552 priv->navigation_x = submenu_right;
4553 priv->navigation_width = event->x_root - submenu_right;
4559 priv->navigation_y = event->y_root;
4560 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4562 if (priv->navigation_height >= 0)
4568 priv->navigation_y = event->y_root;
4569 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4571 if (priv->navigation_height <= 0)
4575 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4576 "gtk-menu-popdown-delay", &popdown_delay,
4579 popdown_data = g_new (GtkMenuPopdownData, 1);
4580 popdown_data->menu = menu;
4581 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4583 priv->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4585 gtk_menu_stop_navigating_submenu_cb,
4587 (GDestroyNotify) g_free);
4592 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4596 g_return_if_fail (GTK_IS_MENU (menu_shell));
4598 parent = menu_shell->priv->parent_menu_shell;
4600 menu_shell->priv->activate_time = 0;
4601 gtk_menu_popdown (GTK_MENU (menu_shell));
4604 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4608 gtk_menu_position (GtkMenu *menu,
4609 gboolean set_scroll_offset)
4611 GtkMenuPrivate *priv = menu->priv;
4613 GtkRequisition requisition;
4617 GdkScreen *pointer_screen;
4618 GdkRectangle monitor;
4621 widget = GTK_WIDGET (menu);
4623 screen = gtk_widget_get_screen (widget);
4624 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4625 gdk_device_get_position (pointer, &pointer_screen, &x, &y);
4627 /* Realize so we have the proper width and heigh to figure out
4628 * the right place to popup the menu.
4630 gtk_widget_realize (priv->toplevel);
4631 requisition.width = gtk_widget_get_allocated_width (widget);
4632 requisition.height = gtk_widget_get_allocated_height (widget);
4634 if (pointer_screen != screen)
4636 /* Pointer is on a different screen; roughly center the
4637 * menu on the screen. If someone was using multiscreen
4638 * + Xinerama together they'd probably want something
4639 * fancier; but that is likely to be vanishingly rare.
4641 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4642 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4645 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4646 priv->initially_pushed_in = FALSE;
4648 /* Set the type hint here to allow custom position functions
4649 * to set a different hint
4651 if (!gtk_widget_get_visible (priv->toplevel))
4652 gtk_window_set_type_hint (GTK_WINDOW (priv->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4654 if (priv->position_func)
4656 (* priv->position_func) (menu, &x, &y, &priv->initially_pushed_in,
4657 priv->position_func_data);
4659 if (priv->monitor_num < 0)
4660 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4662 gdk_screen_get_monitor_workarea (screen, priv->monitor_num, &monitor);
4666 gint space_left, space_right, space_above, space_below;
4670 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4672 get_menu_padding (widget, &padding);
4674 /* The placement of popup menus horizontally works like this (with
4675 * RTL in parentheses)
4677 * - If there is enough room to the right (left) of the mouse cursor,
4678 * position the menu there.
4680 * - Otherwise, if if there is enough room to the left (right) of the
4681 * mouse cursor, position the menu there.
4683 * - Otherwise if the menu is smaller than the monitor, position it
4684 * on the side of the mouse cursor that has the most space available
4686 * - Otherwise (if there is simply not enough room for the menu on the
4687 * monitor), position it as far left (right) as possible.
4689 * Positioning in the vertical direction is similar: first try below
4690 * mouse cursor, then above.
4692 gdk_screen_get_monitor_workarea (screen, priv->monitor_num, &monitor);
4694 space_left = x - monitor.x;
4695 space_right = monitor.x + monitor.width - x - 1;
4696 space_above = y - monitor.y;
4697 space_below = monitor.y + monitor.height - y - 1;
4699 /* Position horizontally. */
4701 /* the amount of space we need to position the menu.
4702 * Note the menu is offset "thickness" pixels
4704 needed_width = requisition.width - padding.left;
4706 if (needed_width <= space_left ||
4707 needed_width <= space_right)
4709 if ((rtl && needed_width <= space_left) ||
4710 (!rtl && needed_width > space_right))
4713 x = x + padding.left - requisition.width + 1;
4717 /* position right */
4718 x = x - padding.right;
4721 /* x is clamped on-screen further down */
4723 else if (requisition.width <= monitor.width)
4725 /* the menu is too big to fit on either side of the mouse
4726 * cursor, but smaller than the monitor. Position it on
4727 * the side that has the most space
4729 if (space_left > space_right)
4737 x = monitor.x + monitor.width - requisition.width;
4740 else /* menu is simply too big for the monitor */
4745 x = monitor.x + monitor.width - requisition.width;
4754 /* Position vertically.
4755 * The algorithm is the same as above, but simpler
4756 * because we don't have to take RTL into account.
4758 needed_height = requisition.height - padding.top;
4760 if (needed_height <= space_above ||
4761 needed_height <= space_below)
4763 if (needed_height <= space_below)
4764 y = y - padding.top;
4766 y = y + padding.bottom - requisition.height + 1;
4768 y = CLAMP (y, monitor.y,
4769 monitor.y + monitor.height - requisition.height);
4771 else if (needed_height > space_below && needed_height > space_above)
4773 if (space_below >= space_above)
4774 y = monitor.y + monitor.height - requisition.height;
4786 if (priv->initially_pushed_in)
4788 if (y + requisition.height > monitor.y + monitor.height)
4790 scroll_offset -= y + requisition.height - (monitor.y + monitor.height);
4791 y = (monitor.y + monitor.height) - requisition.height;
4796 scroll_offset += monitor.y - y;
4800 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4803 if (GTK_MENU_SHELL (menu)->priv->active)
4805 priv->have_position = TRUE;
4806 priv->position_x = x;
4807 priv->position_y = y;
4810 if (scroll_offset > 0)
4812 GtkBorder arrow_border;
4814 get_arrows_border (menu, &arrow_border);
4815 scroll_offset += arrow_border.top;
4818 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->priv->active ? priv->toplevel : priv->tearoff_window),
4821 if (!GTK_MENU_SHELL (menu)->priv->active)
4823 gtk_window_resize (GTK_WINDOW (priv->tearoff_window),
4824 requisition.width, requisition.height);
4827 if (set_scroll_offset)
4828 priv->scroll_offset = scroll_offset;
4832 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4834 GtkMenuPrivate *priv = menu->priv;
4836 if (priv->scroll_timeout)
4838 g_source_remove (priv->scroll_timeout);
4839 priv->scroll_timeout = 0;
4844 gtk_menu_stop_scrolling (GtkMenu *menu)
4846 GtkMenuPrivate *priv = menu->priv;
4847 gboolean touchscreen_mode;
4849 gtk_menu_remove_scroll_timeout (menu);
4851 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4852 "gtk-touchscreen-mode", &touchscreen_mode,
4855 if (!touchscreen_mode)
4857 priv->upper_arrow_prelight = FALSE;
4858 priv->lower_arrow_prelight = FALSE;
4863 gtk_menu_scroll_to (GtkMenu *menu,
4866 GtkMenuPrivate *priv = menu->priv;
4867 GtkBorder arrow_border, padding;
4870 gint view_width, view_height;
4873 guint vertical_padding;
4874 guint horizontal_padding;
4875 gboolean double_arrows;
4877 widget = GTK_WIDGET (menu);
4879 if (priv->tearoff_active && priv->tearoff_adjustment)
4880 gtk_adjustment_set_value (priv->tearoff_adjustment, offset);
4882 /* Move/resize the viewport according to arrows: */
4883 view_width = gtk_widget_get_allocated_width (widget);
4884 view_height = gtk_widget_get_allocated_height (widget);
4886 gtk_widget_style_get (GTK_WIDGET (menu),
4887 "vertical-padding", &vertical_padding,
4888 "horizontal-padding", &horizontal_padding,
4891 get_menu_padding (widget, &padding);
4892 double_arrows = get_double_arrows (menu);
4894 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4896 view_width -= (2 * (border_width + horizontal_padding)) + padding.left + padding.right;
4897 view_height -= (2 * (border_width + vertical_padding)) + padding.top + padding.bottom;
4898 menu_height = priv->requested_height - (2 * (border_width + vertical_padding)) -
4899 padding.top - padding.bottom;
4901 x = border_width + padding.left + horizontal_padding;
4902 y = border_width + padding.top + vertical_padding;
4904 if (double_arrows && !priv->tearoff_active)
4906 if (view_height < menu_height ||
4907 (offset > 0 && priv->scroll_offset > 0) ||
4908 (offset < 0 && priv->scroll_offset < 0))
4910 GtkStateFlags upper_arrow_previous_state = priv->upper_arrow_state;
4911 GtkStateFlags lower_arrow_previous_state = priv->lower_arrow_state;
4913 if (!priv->upper_arrow_visible || !priv->lower_arrow_visible)
4914 gtk_widget_queue_draw (GTK_WIDGET (menu));
4916 priv->upper_arrow_visible = priv->lower_arrow_visible = TRUE;
4918 get_arrows_border (menu, &arrow_border);
4919 y += arrow_border.top;
4920 view_height -= arrow_border.top;
4921 view_height -= arrow_border.bottom;
4924 priv->upper_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4927 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4929 if (priv->upper_arrow_prelight)
4930 priv->upper_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4932 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4935 if (offset >= menu_height - view_height)
4936 priv->lower_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4939 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4941 if (priv->lower_arrow_prelight)
4942 priv->lower_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4944 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4947 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4948 (priv->lower_arrow_state != lower_arrow_previous_state))
4949 gtk_widget_queue_draw (GTK_WIDGET (menu));
4951 if ((upper_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
4952 (priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
4954 /* At the upper border, possibly remove timeout */
4955 if (priv->scroll_step < 0)
4957 gtk_menu_stop_scrolling (menu);
4958 gtk_widget_queue_draw (GTK_WIDGET (menu));
4962 if ((lower_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
4963 (priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
4965 /* At the lower border, possibly remove timeout */
4966 if (priv->scroll_step > 0)
4968 gtk_menu_stop_scrolling (menu);
4969 gtk_widget_queue_draw (GTK_WIDGET (menu));
4973 else if (priv->upper_arrow_visible || priv->lower_arrow_visible)
4977 priv->upper_arrow_visible = priv->lower_arrow_visible = FALSE;
4978 priv->upper_arrow_prelight = priv->lower_arrow_prelight = FALSE;
4980 gtk_menu_stop_scrolling (menu);
4981 gtk_widget_queue_draw (GTK_WIDGET (menu));
4984 else if (!priv->tearoff_active)
4986 gboolean last_visible;
4988 last_visible = priv->upper_arrow_visible;
4989 priv->upper_arrow_visible = offset > 0;
4991 /* upper_arrow_visible may have changed, so requery the border */
4992 get_arrows_border (menu, &arrow_border);
4993 view_height -= arrow_border.top;
4995 if ((last_visible != priv->upper_arrow_visible) &&
4996 !priv->upper_arrow_visible)
4998 priv->upper_arrow_prelight = FALSE;
5000 /* If we hide the upper arrow, possibly remove timeout */
5001 if (priv->scroll_step < 0)
5003 gtk_menu_stop_scrolling (menu);
5004 gtk_widget_queue_draw (GTK_WIDGET (menu));
5008 last_visible = priv->lower_arrow_visible;
5009 priv->lower_arrow_visible = offset < menu_height - view_height;
5011 /* lower_arrow_visible may have changed, so requery the border */
5012 get_arrows_border (menu, &arrow_border);
5013 view_height -= arrow_border.bottom;
5015 if ((last_visible != priv->lower_arrow_visible) &&
5016 !priv->lower_arrow_visible)
5018 priv->lower_arrow_prelight = FALSE;
5020 /* If we hide the lower arrow, possibly remove timeout */
5021 if (priv->scroll_step > 0)
5023 gtk_menu_stop_scrolling (menu);
5024 gtk_widget_queue_draw (GTK_WIDGET (menu));
5028 y += arrow_border.top;
5031 /* Scroll the menu: */
5032 if (gtk_widget_get_realized (widget))
5033 gdk_window_move (priv->bin_window, 0, -offset);
5035 if (gtk_widget_get_realized (widget))
5036 gdk_window_move_resize (priv->view_window, x, y, view_width, view_height);
5038 priv->scroll_offset = offset;
5042 compute_child_offset (GtkMenu *menu,
5043 GtkWidget *menu_item,
5046 gboolean *is_last_child)
5048 GtkMenuPrivate *priv = menu->priv;
5049 gint item_top_attach;
5050 gint item_bottom_attach;
5051 gint child_offset = 0;
5054 get_effective_child_attach (menu_item, NULL, NULL,
5055 &item_top_attach, &item_bottom_attach);
5057 /* there is a possibility that we get called before _size_request,
5058 * so check the height table for safety.
5060 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
5063 /* when we have a row with only invisible children, its height will
5064 * be zero, so there's no need to check WIDGET_VISIBLE here
5066 for (i = 0; i < item_top_attach; i++)
5067 child_offset += priv->heights[i];
5070 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
5072 *offset = child_offset;
5074 *height = priv->heights[item_top_attach];
5080 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
5081 GtkWidget *menu_item)
5083 GtkMenu *menu = GTK_MENU (menu_shell);
5084 GtkMenuPrivate *priv = menu->priv;
5085 GtkWidget *widget = GTK_WIDGET (menu_shell);
5086 gint child_offset, child_height;
5090 gboolean last_child = 0;
5092 /* We need to check if the selected item fully visible.
5093 * If not we need to scroll the menu so that it becomes fully
5096 if (compute_child_offset (menu, menu_item,
5097 &child_offset, &child_height, &last_child))
5099 guint vertical_padding;
5100 gboolean double_arrows;
5103 y = priv->scroll_offset;
5104 height = gdk_window_get_height (gtk_widget_get_window (widget));
5106 gtk_widget_style_get (widget,
5107 "vertical-padding", &vertical_padding,
5110 double_arrows = get_double_arrows (menu);
5111 get_menu_padding (widget, &padding);
5113 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5114 padding.top + padding.bottom +
5115 2 * vertical_padding;
5116 if (child_offset < y)
5118 /* Ignore the enter event we might get if the pointer
5121 menu_shell->priv->ignore_enter = TRUE;
5122 gtk_menu_scroll_to (menu, child_offset);
5126 GtkBorder arrow_border;
5130 get_arrows_border (menu, &arrow_border);
5131 if (!priv->tearoff_active)
5132 arrow_height = arrow_border.top + arrow_border.bottom;
5134 if (child_offset + child_height > y + height - arrow_height)
5137 if ((!last_child && !priv->tearoff_active) || double_arrows)
5138 arrow_height += arrow_border.bottom;
5140 y = child_offset + child_height - height + arrow_height;
5141 if (((y > 0) && !priv->tearoff_active) || double_arrows)
5143 /* Need upper arrow */
5144 arrow_height += arrow_border.top;
5145 y = child_offset + child_height - height + arrow_height;
5147 /* Ignore the enter event we might get if the pointer
5150 menu_shell->priv->ignore_enter = TRUE;
5151 gtk_menu_scroll_to (menu, y);
5158 gtk_menu_select_item (GtkMenuShell *menu_shell,
5159 GtkWidget *menu_item)
5161 GtkMenu *menu = GTK_MENU (menu_shell);
5163 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5164 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5166 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5170 /* Reparent the menu, taking care of the refcounting
5172 * If unrealize is true we force a unrealize while reparenting the parent.
5173 * This can help eliminate flicker in some cases.
5175 * What happens is that when the menu is unrealized and then re-realized,
5176 * the allocations are as follows:
5178 * parent - 1x1 at (0,0)
5179 * child1 - 100x20 at (0,0)
5180 * child2 - 100x20 at (0,20)
5181 * child3 - 100x20 at (0,40)
5183 * That is, the parent is small but the children are full sized. Then,
5184 * when the queued_resize gets processed, the parent gets resized to
5187 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5188 * contains the following logic:
5190 * - if a move or resize operation on a window would change the clip
5191 * region on the children, then before the window is resized
5192 * the background for children is temporarily set to None, the
5193 * move/resize done, and the background for the children restored.
5195 * So, at the point where the parent is resized to final size, the
5196 * background for the children is temporarily None, and thus they
5197 * are not cleared to the background color and the previous background
5198 * (the image of the menu) is left in place.
5201 gtk_menu_reparent (GtkMenu *menu,
5202 GtkWidget *new_parent,
5205 GObject *object = G_OBJECT (menu);
5206 GtkWidget *widget = GTK_WIDGET (menu);
5207 gboolean was_floating = g_object_is_floating (object);
5209 g_object_ref_sink (object);
5213 g_object_ref (object);
5214 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5215 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5216 g_object_unref (object);
5219 gtk_widget_reparent (widget, new_parent);
5222 g_object_force_floating (object);
5224 g_object_unref (object);
5228 gtk_menu_show_all (GtkWidget *widget)
5230 /* Show children, but not self. */
5231 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5235 * gtk_menu_set_screen:
5237 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5238 * determined by the widget the menu is attached to
5240 * Sets the #GdkScreen on which the menu will be displayed.
5245 gtk_menu_set_screen (GtkMenu *menu,
5248 g_return_if_fail (GTK_IS_MENU (menu));
5249 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
5251 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5255 menu_change_screen (menu, screen);
5259 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5261 attach_widget_screen_changed (attach_widget, NULL, menu);
5268 * @child: a #GtkMenuItem
5269 * @left_attach: The column number to attach the left side of the item to
5270 * @right_attach: The column number to attach the right side of the item to
5271 * @top_attach: The row number to attach the top of the item to
5272 * @bottom_attach: The row number to attach the bottom of the item to
5274 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5275 * an item will occupy is specified by @left_attach, @right_attach,
5276 * @top_attach and @bottom_attach. These each represent the leftmost,
5277 * rightmost, uppermost and lower column and row numbers of the table.
5278 * (Columns and rows are indexed from zero).
5280 * Note that this function is not related to gtk_menu_detach().
5285 gtk_menu_attach (GtkMenu *menu,
5290 guint bottom_attach)
5292 GtkMenuShell *menu_shell;
5295 g_return_if_fail (GTK_IS_MENU (menu));
5296 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5297 parent = gtk_widget_get_parent (child);
5298 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5299 g_return_if_fail (left_attach < right_attach);
5300 g_return_if_fail (top_attach < bottom_attach);
5302 menu_shell = GTK_MENU_SHELL (menu);
5306 AttachInfo *ai = get_attach_info (child);
5308 ai->left_attach = left_attach;
5309 ai->right_attach = right_attach;
5310 ai->top_attach = top_attach;
5311 ai->bottom_attach = bottom_attach;
5313 menu_shell->priv->children = g_list_append (menu_shell->priv->children, child);
5315 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5317 menu_queue_resize (menu);
5321 gtk_container_child_set (GTK_CONTAINER (parent), child,
5322 "left-attach", left_attach,
5323 "right-attach", right_attach,
5324 "top-attach", top_attach,
5325 "bottom-attach", bottom_attach,
5331 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5335 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5336 "gtk-menu-popup-delay", &popup_delay,
5343 find_child_containing (GtkMenuShell *menu_shell,
5351 /* find a child which includes the area given by
5352 * left, right, top, bottom.
5354 for (list = menu_shell->priv->children; list; list = list->next)
5358 if (!_gtk_menu_item_is_selectable (list->data))
5361 get_effective_child_attach (list->data, &l, &r, &t, &b);
5363 if (l <= left && right <= r && t <= top && bottom <= b)
5364 return GTK_WIDGET (list->data);
5371 gtk_menu_move_current (GtkMenuShell *menu_shell,
5372 GtkMenuDirectionType direction)
5374 GtkMenu *menu = GTK_MENU (menu_shell);
5377 GtkWidget *match = NULL;
5379 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5383 case GTK_MENU_DIR_CHILD:
5384 direction = GTK_MENU_DIR_PARENT;
5386 case GTK_MENU_DIR_PARENT:
5387 direction = GTK_MENU_DIR_CHILD;
5393 /* use special table menu key bindings */
5394 if (menu_shell->priv->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5396 get_effective_child_attach (menu_shell->priv->active_menu_item, &l, &r, &t, &b);
5398 if (direction == GTK_MENU_DIR_NEXT)
5400 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5402 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5410 for (i = 0; i < t; i++)
5412 match = find_child_containing (menu_shell,
5413 l, l + 1, i, i + 1);
5419 else if (direction == GTK_MENU_DIR_PREV)
5421 for (i = t; i > 0; i--)
5423 match = find_child_containing (menu_shell,
5424 l, l + 1, i - 1, i);
5432 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5434 match = find_child_containing (menu_shell,
5435 l, l + 1, i - 1, i);
5441 else if (direction == GTK_MENU_DIR_PARENT)
5443 /* we go one left if possible */
5445 match = find_child_containing (menu_shell,
5446 l - 1, l, t, t + 1);
5450 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5453 || g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1)
5454 match = menu_shell->priv->active_menu_item;
5457 else if (direction == GTK_MENU_DIR_CHILD)
5459 /* we go one right if possible */
5460 if (r < gtk_menu_get_n_columns (menu))
5461 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5465 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5467 if (! GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu &&
5469 g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1))
5470 match = menu_shell->priv->active_menu_item;
5476 gtk_menu_shell_select_item (menu_shell, match);
5481 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5485 get_visible_size (GtkMenu *menu)
5487 GtkMenuPrivate *priv = menu->priv;
5488 GtkAllocation allocation;
5489 GtkWidget *widget = GTK_WIDGET (menu);
5490 GtkContainer *container = GTK_CONTAINER (menu);
5494 gtk_widget_get_allocation (widget, &allocation);
5495 get_menu_padding (widget, &padding);
5497 menu_height = (allocation.height -
5498 (2 * gtk_container_get_border_width (container)) -
5499 padding.top - padding.bottom);
5501 if (!priv->tearoff_active)
5503 GtkBorder arrow_border;
5505 get_arrows_border (menu, &arrow_border);
5506 menu_height -= arrow_border.top;
5507 menu_height -= arrow_border.bottom;
5513 /* Find the sensitive on-screen child containing @y, or if none,
5514 * the nearest selectable onscreen child. (%NULL if none)
5517 child_at (GtkMenu *menu,
5520 GtkMenuPrivate *priv = menu->priv;
5521 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5522 GtkWidget *child = NULL;
5523 gint child_offset = 0;
5526 gint lower, upper; /* Onscreen bounds */
5528 menu_height = get_visible_size (menu);
5529 lower = priv->scroll_offset;
5530 upper = priv->scroll_offset + menu_height;
5532 for (children = menu_shell->priv->children; children; children = children->next)
5534 if (gtk_widget_get_visible (children->data))
5536 GtkRequisition child_requisition;
5538 gtk_widget_get_preferred_size (children->data,
5539 &child_requisition, NULL);
5541 if (_gtk_menu_item_is_selectable (children->data) &&
5542 child_offset >= lower &&
5543 child_offset + child_requisition.height <= upper)
5545 child = children->data;
5547 if (child_offset + child_requisition.height > y &&
5548 !GTK_IS_TEAROFF_MENU_ITEM (child))
5552 child_offset += child_requisition.height;
5560 get_menu_height (GtkMenu *menu)
5562 GtkMenuPrivate *priv = menu->priv;
5563 GtkAllocation allocation;
5564 GtkWidget *widget = GTK_WIDGET (menu);
5568 gtk_widget_get_allocation (widget, &allocation);
5569 get_menu_padding (widget, &padding);
5571 height = allocation.height;
5572 height -= (gtk_container_get_border_width (GTK_CONTAINER (widget)) * 2) +
5573 padding.top + padding.bottom;
5575 if (!priv->tearoff_active)
5577 GtkBorder arrow_border;
5579 get_arrows_border (menu, &arrow_border);
5580 height -= arrow_border.top;
5581 height -= arrow_border.bottom;
5588 gtk_menu_real_move_scroll (GtkMenu *menu,
5591 GtkMenuPrivate *priv = menu->priv;
5592 gint page_size = get_visible_size (menu);
5593 gint end_position = get_menu_height (menu);
5594 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5598 case GTK_SCROLL_PAGE_UP:
5599 case GTK_SCROLL_PAGE_DOWN:
5603 gint child_offset = 0;
5604 gboolean old_upper_arrow_visible;
5607 if (type == GTK_SCROLL_PAGE_UP)
5612 if (menu_shell->priv->active_menu_item)
5616 compute_child_offset (menu, menu_shell->priv->active_menu_item,
5617 &child_offset, &child_height, NULL);
5618 child_offset += child_height / 2;
5621 menu_shell->priv->ignore_enter = TRUE;
5622 old_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5623 old_offset = priv->scroll_offset;
5625 new_offset = priv->scroll_offset + step;
5626 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5628 gtk_menu_scroll_to (menu, new_offset);
5630 if (menu_shell->priv->active_menu_item)
5632 GtkWidget *new_child;
5633 gboolean new_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5634 GtkBorder arrow_border;
5635 get_arrows_border (menu, &arrow_border);
5637 if (priv->scroll_offset != old_offset)
5638 step = priv->scroll_offset - old_offset;
5640 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5642 new_child = child_at (menu, child_offset + step);
5644 gtk_menu_shell_select_item (menu_shell, new_child);
5648 case GTK_SCROLL_START:
5649 /* Ignore the enter event we might get if the pointer is on the menu */
5650 menu_shell->priv->ignore_enter = TRUE;
5651 gtk_menu_scroll_to (menu, 0);
5652 gtk_menu_shell_select_first (menu_shell, TRUE);
5654 case GTK_SCROLL_END:
5655 /* Ignore the enter event we might get if the pointer is on the menu */
5656 menu_shell->priv->ignore_enter = TRUE;
5657 gtk_menu_scroll_to (menu, end_position - page_size);
5658 _gtk_menu_shell_select_last (menu_shell, TRUE);
5667 * gtk_menu_set_monitor:
5669 * @monitor_num: the number of the monitor on which the menu should
5672 * Informs GTK+ on which monitor a menu should be popped up.
5673 * See gdk_screen_get_monitor_geometry().
5675 * This function should be called from a #GtkMenuPositionFunc
5676 * if the menu should not appear on the same monitor as the pointer.
5677 * This information can't be reliably inferred from the coordinates
5678 * returned by a #GtkMenuPositionFunc, since, for very long menus,
5679 * these coordinates may extend beyond the monitor boundaries or even
5680 * the screen boundaries.
5685 gtk_menu_set_monitor (GtkMenu *menu,
5688 GtkMenuPrivate *priv = menu->priv;
5690 g_return_if_fail (GTK_IS_MENU (menu));
5692 priv->monitor_num = monitor_num;
5696 * gtk_menu_get_monitor:
5699 * Retrieves the number of the monitor on which to show the menu.
5701 * Returns: the number of the monitor on which the menu should
5702 * be popped up or -1, if no monitor has been set
5707 gtk_menu_get_monitor (GtkMenu *menu)
5709 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5711 return menu->priv->monitor_num;
5715 * gtk_menu_get_for_attach_widget:
5716 * @widget: a #GtkWidget
5718 * Returns a list of the menus which are attached to this widget.
5719 * This list is owned by GTK+ and must not be modified.
5721 * Return value: (element-type GtkWidget) (transfer none): the list
5722 * of menus attached to his widget.
5727 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5731 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5733 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5739 gtk_menu_grab_notify (GtkWidget *widget,
5740 gboolean was_grabbed)
5742 GtkWidget *toplevel;
5743 GtkWindowGroup *group;
5747 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5750 !gtk_widget_device_is_shadowed (widget, pointer))
5753 toplevel = gtk_widget_get_toplevel (widget);
5755 if (!GTK_IS_WINDOW (toplevel))
5758 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5759 grab = gtk_window_group_get_current_device_grab (group, pointer);
5761 if (GTK_MENU_SHELL (widget)->priv->active && !GTK_IS_MENU_SHELL (grab))
5762 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5766 * gtk_menu_set_reserve_toggle_size:
5768 * @reserve_toggle_size: whether to reserve size for toggles
5770 * Sets whether the menu should reserve space for drawing toggles
5771 * or icons, regardless of their actual presence.
5776 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5777 gboolean reserve_toggle_size)
5779 GtkMenuPrivate *priv = menu->priv;
5780 gboolean no_toggle_size;
5782 g_return_if_fail (GTK_IS_MENU (menu));
5784 no_toggle_size = !reserve_toggle_size;
5786 if (priv->no_toggle_size != no_toggle_size)
5788 priv->no_toggle_size = no_toggle_size;
5790 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5795 * gtk_menu_get_reserve_toggle_size:
5798 * Returns whether the menu reserves space for toggles and
5799 * icons, regardless of their actual presence.
5801 * Returns: Whether the menu reserves toggle space
5806 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5808 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
5810 return !menu->priv->no_toggle_size;