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 #include "gdk/gdkkeysyms.h"
30 #include "gtkaccellabel.h"
31 #include "gtkaccelmap.h"
32 #include "gtkbindings.h"
33 #include "gtkcheckmenuitem.h"
34 #include <gobject/gvaluecollector.h>
36 #include "gtkmarshalers.h"
38 #include "gtkmenuprivate.h"
39 #include "gtktearoffmenuitem.h"
40 #include "gtkwindow.h"
42 #include "gtkvscrollbar.h"
43 #include "gtksettings.h"
44 #include "gtksizerequest.h"
45 #include "gtkprivate.h"
48 #define DEFAULT_POPUP_DELAY 225
49 #define DEFAULT_POPDOWN_DELAY 1000
51 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
52 * extends below the submenu
55 #define MENU_SCROLL_STEP1 8
56 #define MENU_SCROLL_STEP2 15
57 #define MENU_SCROLL_FAST_ZONE 8
58 #define MENU_SCROLL_TIMEOUT1 50
59 #define MENU_SCROLL_TIMEOUT2 20
61 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
62 #define ATTACHED_MENUS "gtk-attached-menus"
64 typedef struct _GtkMenuAttachData GtkMenuAttachData;
65 typedef struct _GtkMenuPrivate GtkMenuPrivate;
66 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
68 struct _GtkMenuAttachData
70 GtkWidget *attach_widget;
71 GtkMenuDetachFunc detacher;
74 struct _GtkMenuPrivate
78 gboolean initially_pushed_in;
80 GDestroyNotify position_func_data_destroy;
82 /* info used for the table */
85 gint requested_height;
89 /* Cached layout information */
98 GtkStateType lower_arrow_state;
99 GtkStateType upper_arrow_state;
101 /* navigation region */
104 int navigation_width;
105 int navigation_height;
107 guint have_layout : 1;
108 guint seen_item_enter : 1;
109 guint have_position : 1;
110 guint ignore_button_release : 1;
111 guint no_toggle_size : 1;
114 struct _GtkMenuPopdownData
126 gint effective_left_attach;
127 gint effective_right_attach;
128 gint effective_top_attach;
129 gint effective_bottom_attach;
146 PROP_RESERVE_TOGGLE_SIZE
151 CHILD_PROP_LEFT_ATTACH,
152 CHILD_PROP_RIGHT_ATTACH,
153 CHILD_PROP_TOP_ATTACH,
154 CHILD_PROP_BOTTOM_ATTACH
157 static void gtk_menu_set_property (GObject *object,
161 static void gtk_menu_get_property (GObject *object,
165 static void gtk_menu_set_child_property(GtkContainer *container,
170 static void gtk_menu_get_child_property(GtkContainer *container,
175 static void gtk_menu_destroy (GtkObject *object);
176 static void gtk_menu_realize (GtkWidget *widget);
177 static void gtk_menu_unrealize (GtkWidget *widget);
178 static void gtk_menu_size_allocate (GtkWidget *widget,
179 GtkAllocation *allocation);
180 static void gtk_menu_show (GtkWidget *widget);
181 static gboolean gtk_menu_draw (GtkWidget *widget,
183 static gboolean gtk_menu_key_press (GtkWidget *widget,
185 static gboolean gtk_menu_scroll (GtkWidget *widget,
186 GdkEventScroll *event);
187 static gboolean gtk_menu_button_press (GtkWidget *widget,
188 GdkEventButton *event);
189 static gboolean gtk_menu_button_release (GtkWidget *widget,
190 GdkEventButton *event);
191 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
192 GdkEventMotion *event);
193 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
194 GdkEventCrossing *event);
195 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
196 GdkEventCrossing *event);
197 static void gtk_menu_scroll_to (GtkMenu *menu,
199 static void gtk_menu_grab_notify (GtkWidget *widget,
200 gboolean was_grabbed);
202 static void gtk_menu_stop_scrolling (GtkMenu *menu);
203 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
204 static gboolean gtk_menu_scroll_timeout (gpointer data);
205 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
206 static void gtk_menu_start_scrolling (GtkMenu *menu);
208 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
209 GtkWidget *menu_item);
210 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
211 GtkWidget *menu_item);
212 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
215 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
217 static void gtk_menu_handle_scrolling (GtkMenu *menu,
222 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
224 static void gtk_menu_style_set (GtkWidget *widget,
225 GtkStyle *previous_style);
226 static gboolean gtk_menu_focus (GtkWidget *widget,
227 GtkDirectionType direction);
228 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
229 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
230 GtkMenuDirectionType direction);
231 static void gtk_menu_real_move_scroll (GtkMenu *menu,
234 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
235 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
236 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
239 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
240 GtkMenuItem *menu_item,
241 GdkEventCrossing *event);
243 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
244 static void gtk_menu_show_all (GtkWidget *widget);
245 static void gtk_menu_hide_all (GtkWidget *widget);
246 static void gtk_menu_position (GtkMenu *menu);
247 static void gtk_menu_reparent (GtkMenu *menu,
248 GtkWidget *new_parent,
250 static void gtk_menu_remove (GtkContainer *menu,
253 static void gtk_menu_update_title (GtkMenu *menu);
255 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
256 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
258 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
260 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
261 gboolean group_changed);
263 static void gtk_menu_size_request_init (GtkSizeRequestIface *iface);
264 static void gtk_menu_get_width (GtkSizeRequest *widget,
267 static void gtk_menu_get_height (GtkSizeRequest *widget,
270 static void gtk_menu_get_height_for_width (GtkSizeRequest *widget,
276 static const gchar attach_data_key[] = "gtk-menu-attach-data";
278 static guint menu_signals[LAST_SIGNAL] = { 0 };
280 static GtkMenuPrivate *
281 gtk_menu_get_private (GtkMenu *menu)
283 return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
286 G_DEFINE_TYPE_WITH_CODE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL,
287 G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
288 gtk_menu_size_request_init))
291 menu_queue_resize (GtkMenu *menu)
293 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
295 priv->have_layout = FALSE;
296 gtk_widget_queue_resize (GTK_WIDGET (menu));
300 attach_info_free (AttachInfo *info)
302 g_slice_free (AttachInfo, info);
306 get_attach_info (GtkWidget *child)
308 GObject *object = G_OBJECT (child);
309 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
313 ai = g_slice_new0 (AttachInfo);
314 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
315 (GDestroyNotify) attach_info_free);
322 is_grid_attached (AttachInfo *ai)
324 return (ai->left_attach >= 0 &&
325 ai->right_attach >= 0 &&
326 ai->top_attach >= 0 &&
327 ai->bottom_attach >= 0);
331 menu_ensure_layout (GtkMenu *menu)
333 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
335 if (!priv->have_layout)
337 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
341 gint max_right_attach;
342 gint max_bottom_attach;
344 /* Find extents of gridded portion
346 max_right_attach = 1;
347 max_bottom_attach = 0;
349 for (l = menu_shell->children; l; l = l->next)
351 GtkWidget *child = l->data;
352 AttachInfo *ai = get_attach_info (child);
354 if (is_grid_attached (ai))
356 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
357 max_right_attach = MAX (max_right_attach, ai->right_attach);
363 row_occupied = g_malloc0 (max_bottom_attach);
365 for (l = menu_shell->children; l; l = l->next)
367 GtkWidget *child = l->data;
368 AttachInfo *ai = get_attach_info (child);
370 if (is_grid_attached (ai))
374 for (i = ai->top_attach; i < ai->bottom_attach; i++)
375 row_occupied[i] = TRUE;
379 /* Lay non-grid-items out in those rows
382 for (l = menu_shell->children; l; l = l->next)
384 GtkWidget *child = l->data;
385 AttachInfo *ai = get_attach_info (child);
387 if (!is_grid_attached (ai))
389 while (current_row < max_bottom_attach && row_occupied[current_row])
392 ai->effective_left_attach = 0;
393 ai->effective_right_attach = max_right_attach;
394 ai->effective_top_attach = current_row;
395 ai->effective_bottom_attach = current_row + 1;
401 ai->effective_left_attach = ai->left_attach;
402 ai->effective_right_attach = ai->right_attach;
403 ai->effective_top_attach = ai->top_attach;
404 ai->effective_bottom_attach = ai->bottom_attach;
408 g_free (row_occupied);
410 priv->n_rows = MAX (current_row, max_bottom_attach);
411 priv->n_columns = max_right_attach;
412 priv->have_layout = TRUE;
418 gtk_menu_get_n_columns (GtkMenu *menu)
420 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
422 menu_ensure_layout (menu);
424 return priv->n_columns;
428 gtk_menu_get_n_rows (GtkMenu *menu)
430 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
432 menu_ensure_layout (menu);
438 get_effective_child_attach (GtkWidget *child,
444 GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
447 menu_ensure_layout (menu);
449 ai = get_attach_info (child);
452 *l = ai->effective_left_attach;
454 *r = ai->effective_right_attach;
456 *t = ai->effective_top_attach;
458 *b = ai->effective_bottom_attach;
463 gtk_menu_class_init (GtkMenuClass *class)
465 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
466 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
467 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
468 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
469 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
470 GtkBindingSet *binding_set;
472 gobject_class->set_property = gtk_menu_set_property;
473 gobject_class->get_property = gtk_menu_get_property;
475 object_class->destroy = gtk_menu_destroy;
477 widget_class->realize = gtk_menu_realize;
478 widget_class->unrealize = gtk_menu_unrealize;
479 widget_class->size_allocate = gtk_menu_size_allocate;
480 widget_class->show = gtk_menu_show;
481 widget_class->draw = gtk_menu_draw;
482 widget_class->scroll_event = gtk_menu_scroll;
483 widget_class->key_press_event = gtk_menu_key_press;
484 widget_class->button_press_event = gtk_menu_button_press;
485 widget_class->button_release_event = gtk_menu_button_release;
486 widget_class->motion_notify_event = gtk_menu_motion_notify;
487 widget_class->show_all = gtk_menu_show_all;
488 widget_class->hide_all = gtk_menu_hide_all;
489 widget_class->enter_notify_event = gtk_menu_enter_notify;
490 widget_class->leave_notify_event = gtk_menu_leave_notify;
491 widget_class->style_set = gtk_menu_style_set;
492 widget_class->focus = gtk_menu_focus;
493 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
494 widget_class->grab_notify = gtk_menu_grab_notify;
496 container_class->remove = gtk_menu_remove;
497 container_class->get_child_property = gtk_menu_get_child_property;
498 container_class->set_child_property = gtk_menu_set_child_property;
500 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
501 menu_shell_class->deactivate = gtk_menu_deactivate;
502 menu_shell_class->select_item = gtk_menu_select_item;
503 menu_shell_class->insert = gtk_menu_real_insert;
504 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
505 menu_shell_class->move_current = gtk_menu_move_current;
507 menu_signals[MOVE_SCROLL] =
508 g_signal_new_class_handler (I_("move-scroll"),
509 G_OBJECT_CLASS_TYPE (object_class),
510 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
511 G_CALLBACK (gtk_menu_real_move_scroll),
513 _gtk_marshal_VOID__ENUM,
515 GTK_TYPE_SCROLL_TYPE);
520 * The index of the currently selected menu item, or -1 if no
521 * menu item is selected.
525 g_object_class_install_property (gobject_class,
527 g_param_spec_int ("active",
529 P_("The currently selected menu item"),
531 GTK_PARAM_READWRITE));
534 * GtkMenu:accel-group:
536 * The accel group holding accelerators for the menu.
540 g_object_class_install_property (gobject_class,
542 g_param_spec_object ("accel-group",
544 P_("The accel group holding accelerators for the menu"),
545 GTK_TYPE_ACCEL_GROUP,
546 GTK_PARAM_READWRITE));
549 * GtkMenu:accel-path:
551 * An accel path used to conveniently construct accel paths of child items.
555 g_object_class_install_property (gobject_class,
557 g_param_spec_string ("accel-path",
559 P_("An accel path used to conveniently construct accel paths of child items"),
561 GTK_PARAM_READWRITE));
564 * GtkMenu:attach-widget:
566 * The widget the menu is attached to. Setting this property attaches
567 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
568 * use gtk_menu_attach_to_widget() directly.
572 g_object_class_install_property (gobject_class,
574 g_param_spec_object ("attach-widget",
576 P_("The widget the menu is attached to"),
578 GTK_PARAM_READWRITE));
580 g_object_class_install_property (gobject_class,
582 g_param_spec_string ("tearoff-title",
584 P_("A title that may be displayed by the window manager when this menu is torn-off"),
586 GTK_PARAM_READWRITE));
589 * GtkMenu:tearoff-state:
591 * A boolean that indicates whether the menu is torn-off.
595 g_object_class_install_property (gobject_class,
597 g_param_spec_boolean ("tearoff-state",
599 P_("A boolean that indicates whether the menu is torn-off"),
601 GTK_PARAM_READWRITE));
606 * The monitor the menu will be popped up on.
610 g_object_class_install_property (gobject_class,
612 g_param_spec_int ("monitor",
614 P_("The monitor the menu will be popped up on"),
616 GTK_PARAM_READWRITE));
618 gtk_widget_class_install_style_property (widget_class,
619 g_param_spec_int ("vertical-padding",
620 P_("Vertical Padding"),
621 P_("Extra space at the top and bottom of the menu"),
625 GTK_PARAM_READABLE));
628 * GtkMenu:reserve-toggle-size:
630 * A boolean that indicates whether the menu reserves space for
631 * toggles and icons, regardless of their actual presence.
633 * This property should only be changed from its default value
634 * for special-purposes such as tabular menus. Regular menus that
635 * are connected to a menu bar or context menus should reserve
636 * toggle space for consistency.
640 g_object_class_install_property (gobject_class,
641 PROP_RESERVE_TOGGLE_SIZE,
642 g_param_spec_boolean ("reserve-toggle-size",
643 P_("Reserve Toggle Size"),
644 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
646 GTK_PARAM_READWRITE));
648 gtk_widget_class_install_style_property (widget_class,
649 g_param_spec_int ("horizontal-padding",
650 P_("Horizontal Padding"),
651 P_("Extra space at the left and right edges of the menu"),
655 GTK_PARAM_READABLE));
657 gtk_widget_class_install_style_property (widget_class,
658 g_param_spec_int ("vertical-offset",
659 P_("Vertical Offset"),
660 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
664 GTK_PARAM_READABLE));
666 gtk_widget_class_install_style_property (widget_class,
667 g_param_spec_int ("horizontal-offset",
668 P_("Horizontal Offset"),
669 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
673 GTK_PARAM_READABLE));
675 gtk_widget_class_install_style_property (widget_class,
676 g_param_spec_boolean ("double-arrows",
678 P_("When scrolling, always show both arrows."),
680 GTK_PARAM_READABLE));
683 * GtkMenu:arrow-placement:
685 * Indicates where scroll arrows should be placed.
689 gtk_widget_class_install_style_property (widget_class,
690 g_param_spec_enum ("arrow-placement",
691 P_("Arrow Placement"),
692 P_("Indicates where scroll arrows should be placed"),
693 GTK_TYPE_ARROW_PLACEMENT,
695 GTK_PARAM_READABLE));
697 gtk_container_class_install_child_property (container_class,
698 CHILD_PROP_LEFT_ATTACH,
699 g_param_spec_int ("left-attach",
701 P_("The column number to attach the left side of the child to"),
703 GTK_PARAM_READWRITE));
705 gtk_container_class_install_child_property (container_class,
706 CHILD_PROP_RIGHT_ATTACH,
707 g_param_spec_int ("right-attach",
709 P_("The column number to attach the right side of the child to"),
711 GTK_PARAM_READWRITE));
713 gtk_container_class_install_child_property (container_class,
714 CHILD_PROP_TOP_ATTACH,
715 g_param_spec_int ("top-attach",
717 P_("The row number to attach the top of the child to"),
719 GTK_PARAM_READWRITE));
721 gtk_container_class_install_child_property (container_class,
722 CHILD_PROP_BOTTOM_ATTACH,
723 g_param_spec_int ("bottom-attach",
725 P_("The row number to attach the bottom of the child to"),
727 GTK_PARAM_READWRITE));
730 * GtkMenu::arrow-scaling
732 * Arbitrary constant to scale down the size of the scroll arrow.
736 gtk_widget_class_install_style_property (widget_class,
737 g_param_spec_float ("arrow-scaling",
739 P_("Arbitrary constant to scale down the size of the scroll arrow"),
741 GTK_PARAM_READABLE));
743 binding_set = gtk_binding_set_by_class (class);
744 gtk_binding_entry_add_signal (binding_set,
746 I_("move-current"), 1,
747 GTK_TYPE_MENU_DIRECTION_TYPE,
749 gtk_binding_entry_add_signal (binding_set,
752 GTK_TYPE_MENU_DIRECTION_TYPE,
754 gtk_binding_entry_add_signal (binding_set,
757 GTK_TYPE_MENU_DIRECTION_TYPE,
759 gtk_binding_entry_add_signal (binding_set,
762 GTK_TYPE_MENU_DIRECTION_TYPE,
764 gtk_binding_entry_add_signal (binding_set,
767 GTK_TYPE_MENU_DIRECTION_TYPE,
768 GTK_MENU_DIR_PARENT);
769 gtk_binding_entry_add_signal (binding_set,
772 GTK_TYPE_MENU_DIRECTION_TYPE,
773 GTK_MENU_DIR_PARENT);
774 gtk_binding_entry_add_signal (binding_set,
777 GTK_TYPE_MENU_DIRECTION_TYPE,
779 gtk_binding_entry_add_signal (binding_set,
782 GTK_TYPE_MENU_DIRECTION_TYPE,
784 gtk_binding_entry_add_signal (binding_set,
787 GTK_TYPE_SCROLL_TYPE,
789 gtk_binding_entry_add_signal (binding_set,
792 GTK_TYPE_SCROLL_TYPE,
794 gtk_binding_entry_add_signal (binding_set,
797 GTK_TYPE_SCROLL_TYPE,
799 gtk_binding_entry_add_signal (binding_set,
802 GTK_TYPE_SCROLL_TYPE,
804 gtk_binding_entry_add_signal (binding_set,
807 GTK_TYPE_SCROLL_TYPE,
809 gtk_binding_entry_add_signal (binding_set,
810 GDK_KEY_KP_Page_Up, 0,
812 GTK_TYPE_SCROLL_TYPE,
814 gtk_binding_entry_add_signal (binding_set,
815 GDK_KEY_Page_Down, 0,
817 GTK_TYPE_SCROLL_TYPE,
818 GTK_SCROLL_PAGE_DOWN);
819 gtk_binding_entry_add_signal (binding_set,
820 GDK_KEY_KP_Page_Down, 0,
822 GTK_TYPE_SCROLL_TYPE,
823 GTK_SCROLL_PAGE_DOWN);
825 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
826 P_("Can change accelerators"),
827 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
829 GTK_PARAM_READWRITE));
831 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
832 P_("Delay before submenus appear"),
833 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
837 GTK_PARAM_READWRITE));
839 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
840 P_("Delay before hiding a submenu"),
841 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
844 DEFAULT_POPDOWN_DELAY,
845 GTK_PARAM_READWRITE));
847 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
852 gtk_menu_set_property (GObject *object,
857 GtkMenu *menu = GTK_MENU (object);
862 gtk_menu_set_active (menu, g_value_get_int (value));
864 case PROP_ACCEL_GROUP:
865 gtk_menu_set_accel_group (menu, g_value_get_object (value));
867 case PROP_ACCEL_PATH:
868 gtk_menu_set_accel_path (menu, g_value_get_string (value));
870 case PROP_ATTACH_WIDGET:
874 widget = gtk_menu_get_attach_widget (menu);
876 gtk_menu_detach (menu);
878 widget = (GtkWidget*) g_value_get_object (value);
880 gtk_menu_attach_to_widget (menu, widget, NULL);
883 case PROP_TEAROFF_STATE:
884 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
886 case PROP_TEAROFF_TITLE:
887 gtk_menu_set_title (menu, g_value_get_string (value));
890 gtk_menu_set_monitor (menu, g_value_get_int (value));
892 case PROP_RESERVE_TOGGLE_SIZE:
893 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
896 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
902 gtk_menu_get_property (GObject *object,
907 GtkMenu *menu = GTK_MENU (object);
912 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->children, gtk_menu_get_active (menu)));
914 case PROP_ACCEL_GROUP:
915 g_value_set_object (value, gtk_menu_get_accel_group (menu));
917 case PROP_ACCEL_PATH:
918 g_value_set_string (value, gtk_menu_get_accel_path (menu));
920 case PROP_ATTACH_WIDGET:
921 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
923 case PROP_TEAROFF_STATE:
924 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
926 case PROP_TEAROFF_TITLE:
927 g_value_set_string (value, gtk_menu_get_title (menu));
930 g_value_set_int (value, gtk_menu_get_monitor (menu));
932 case PROP_RESERVE_TOGGLE_SIZE:
933 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
936 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
942 gtk_menu_set_child_property (GtkContainer *container,
948 GtkMenu *menu = GTK_MENU (container);
949 AttachInfo *ai = get_attach_info (child);
953 case CHILD_PROP_LEFT_ATTACH:
954 ai->left_attach = g_value_get_int (value);
956 case CHILD_PROP_RIGHT_ATTACH:
957 ai->right_attach = g_value_get_int (value);
959 case CHILD_PROP_TOP_ATTACH:
960 ai->top_attach = g_value_get_int (value);
962 case CHILD_PROP_BOTTOM_ATTACH:
963 ai->bottom_attach = g_value_get_int (value);
967 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
971 menu_queue_resize (menu);
975 gtk_menu_get_child_property (GtkContainer *container,
981 AttachInfo *ai = get_attach_info (child);
985 case CHILD_PROP_LEFT_ATTACH:
986 g_value_set_int (value, ai->left_attach);
988 case CHILD_PROP_RIGHT_ATTACH:
989 g_value_set_int (value, ai->right_attach);
991 case CHILD_PROP_TOP_ATTACH:
992 g_value_set_int (value, ai->top_attach);
994 case CHILD_PROP_BOTTOM_ATTACH:
995 g_value_set_int (value, ai->bottom_attach);
999 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1005 gtk_menu_window_event (GtkWidget *window,
1009 gboolean handled = FALSE;
1011 g_object_ref (window);
1012 g_object_ref (menu);
1014 switch (event->type)
1017 case GDK_KEY_RELEASE:
1018 handled = gtk_widget_event (menu, event);
1024 g_object_unref (window);
1025 g_object_unref (menu);
1031 gtk_menu_init (GtkMenu *menu)
1033 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
1035 menu->parent_menu_item = NULL;
1036 menu->old_active_menu_item = NULL;
1037 menu->accel_group = NULL;
1038 menu->position_func = NULL;
1039 menu->position_func_data = NULL;
1040 menu->toggle_size = 0;
1042 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1043 "type", GTK_WINDOW_POPUP,
1046 "signal::event", gtk_menu_window_event, menu,
1047 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1049 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
1050 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
1052 /* Refloat the menu, so that reference counting for the menu isn't
1053 * affected by it being a child of the toplevel
1055 g_object_force_floating (G_OBJECT (menu));
1056 menu->needs_destruction_ref_count = TRUE;
1058 menu->view_window = NULL;
1059 menu->bin_window = NULL;
1061 menu->scroll_offset = 0;
1062 menu->scroll_step = 0;
1063 menu->timeout_id = 0;
1064 menu->scroll_fast = FALSE;
1066 menu->tearoff_window = NULL;
1067 menu->tearoff_hbox = NULL;
1068 menu->torn_off = FALSE;
1069 menu->tearoff_active = FALSE;
1070 menu->tearoff_adjustment = NULL;
1071 menu->tearoff_scrollbar = NULL;
1073 menu->upper_arrow_visible = FALSE;
1074 menu->lower_arrow_visible = FALSE;
1075 menu->upper_arrow_prelight = FALSE;
1076 menu->lower_arrow_prelight = FALSE;
1078 priv->upper_arrow_state = GTK_STATE_NORMAL;
1079 priv->lower_arrow_state = GTK_STATE_NORMAL;
1081 priv->have_layout = FALSE;
1082 priv->monitor_num = -1;
1086 gtk_menu_destroy (GtkObject *object)
1088 GtkMenu *menu = GTK_MENU (object);
1089 GtkMenuAttachData *data;
1090 GtkMenuPrivate *priv;
1092 gtk_menu_remove_scroll_timeout (menu);
1094 data = g_object_get_data (G_OBJECT (object), attach_data_key);
1096 gtk_menu_detach (menu);
1098 gtk_menu_stop_navigating_submenu (menu);
1100 if (menu->old_active_menu_item)
1102 g_object_unref (menu->old_active_menu_item);
1103 menu->old_active_menu_item = NULL;
1106 /* Add back the reference count for being a child */
1107 if (menu->needs_destruction_ref_count)
1109 menu->needs_destruction_ref_count = FALSE;
1110 g_object_ref (object);
1113 if (menu->accel_group)
1115 g_object_unref (menu->accel_group);
1116 menu->accel_group = NULL;
1120 gtk_widget_destroy (menu->toplevel);
1122 if (menu->tearoff_window)
1123 gtk_widget_destroy (menu->tearoff_window);
1125 priv = gtk_menu_get_private (menu);
1129 g_free (priv->heights);
1130 priv->heights = NULL;
1135 g_free (priv->title);
1139 if (priv->position_func_data_destroy)
1141 priv->position_func_data_destroy (menu->position_func_data);
1142 menu->position_func_data = NULL;
1143 priv->position_func_data_destroy = NULL;
1146 GTK_OBJECT_CLASS (gtk_menu_parent_class)->destroy (object);
1150 menu_change_screen (GtkMenu *menu,
1151 GdkScreen *new_screen)
1153 GtkMenuPrivate *private = gtk_menu_get_private (menu);
1155 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1157 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1163 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1164 gtk_menu_position (menu);
1167 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1168 private->monitor_num = -1;
1172 attach_widget_screen_changed (GtkWidget *attach_widget,
1173 GdkScreen *previous_screen,
1176 if (gtk_widget_has_screen (attach_widget) &&
1177 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1179 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1184 gtk_menu_attach_to_widget (GtkMenu *menu,
1185 GtkWidget *attach_widget,
1186 GtkMenuDetachFunc detacher)
1188 GtkMenuAttachData *data;
1191 g_return_if_fail (GTK_IS_MENU (menu));
1192 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1194 /* keep this function in sync with gtk_widget_set_parent()
1197 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1200 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1201 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1205 g_object_ref_sink (menu);
1207 data = g_slice_new (GtkMenuAttachData);
1208 data->attach_widget = attach_widget;
1210 g_signal_connect (attach_widget, "screen-changed",
1211 G_CALLBACK (attach_widget_screen_changed), menu);
1212 attach_widget_screen_changed (attach_widget, NULL, menu);
1214 data->detacher = detacher;
1215 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1216 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1217 if (!g_list_find (list, menu))
1219 list = g_list_prepend (list, menu);
1221 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1222 (GDestroyNotify) g_list_free);
1224 if (gtk_widget_get_state (GTK_WIDGET (menu)) != GTK_STATE_NORMAL)
1225 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1227 /* we don't need to set the style here, since
1228 * we are a toplevel widget.
1231 /* Fallback title for menu comes from attach widget */
1232 gtk_menu_update_title (menu);
1234 g_object_notify (G_OBJECT (menu), "attach-widget");
1238 gtk_menu_get_attach_widget (GtkMenu *menu)
1240 GtkMenuAttachData *data;
1242 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1244 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1246 return data->attach_widget;
1251 gtk_menu_detach (GtkMenu *menu)
1253 GtkMenuAttachData *data;
1256 g_return_if_fail (GTK_IS_MENU (menu));
1258 /* keep this function in sync with gtk_widget_unparent()
1260 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1263 g_warning ("gtk_menu_detach(): menu is not attached");
1266 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1268 g_signal_handlers_disconnect_by_func (data->attach_widget,
1269 (gpointer) attach_widget_screen_changed,
1273 data->detacher (data->attach_widget, menu);
1274 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1275 list = g_list_remove (list, menu);
1277 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1278 (GDestroyNotify) g_list_free);
1280 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1282 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1283 gtk_widget_unrealize (GTK_WIDGET (menu));
1285 g_slice_free (GtkMenuAttachData, data);
1287 /* Fallback title for menu comes from attach widget */
1288 gtk_menu_update_title (menu);
1290 g_object_unref (menu);
1294 gtk_menu_remove (GtkContainer *container,
1297 GtkMenu *menu = GTK_MENU (container);
1299 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1301 /* Clear out old_active_menu_item if it matches the item we are removing
1303 if (menu->old_active_menu_item == widget)
1305 g_object_unref (menu->old_active_menu_item);
1306 menu->old_active_menu_item = NULL;
1309 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1310 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1312 menu_queue_resize (menu);
1318 return g_object_new (GTK_TYPE_MENU, NULL);
1322 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1326 GtkMenu *menu = GTK_MENU (menu_shell);
1327 AttachInfo *ai = get_attach_info (child);
1329 ai->left_attach = -1;
1330 ai->right_attach = -1;
1331 ai->top_attach = -1;
1332 ai->bottom_attach = -1;
1334 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1335 gtk_widget_set_parent_window (child, menu->bin_window);
1337 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1339 menu_queue_resize (menu);
1343 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1348 widget = GTK_WIDGET (menu);
1353 cairo_surface_t *surface;
1354 cairo_pattern_t *pattern;
1357 menu->tearoff_active = FALSE;
1358 menu->saved_scroll_offset = menu->scroll_offset;
1360 window = gtk_widget_get_window (menu->tearoff_window);
1362 gdk_drawable_get_size (window, &width, &height);
1364 surface = gdk_window_create_similar_surface (window,
1365 CAIRO_CONTENT_COLOR,
1369 cr = cairo_create (surface);
1370 gdk_cairo_set_source_window (cr,
1376 gtk_widget_set_size_request (menu->tearoff_window,
1380 pattern = cairo_pattern_create_for_surface (surface);
1381 gdk_window_set_background_pattern (window, pattern);
1383 cairo_pattern_destroy (pattern);
1384 cairo_surface_destroy (surface);
1389 popup_grab_on_window (GdkWindow *window,
1390 GdkDevice *keyboard,
1392 guint32 activate_time)
1395 gdk_device_grab (keyboard, window,
1396 GDK_OWNERSHIP_WINDOW, TRUE,
1397 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1398 NULL, activate_time) != GDK_GRAB_SUCCESS)
1402 gdk_device_grab (pointer, window,
1403 GDK_OWNERSHIP_WINDOW, TRUE,
1404 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1405 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1406 GDK_POINTER_MOTION_MASK,
1407 NULL, activate_time) != GDK_GRAB_SUCCESS)
1410 gdk_device_ungrab (keyboard, activate_time);
1419 * gtk_menu_popup_for_device:
1420 * @menu: a #GtkMenu.
1421 * @device: (allow-none): a #GdkDevice
1422 * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1423 * menu item, or %NULL
1424 * @parent_menu_item: (allow-none): the menu item whose activation triggered
1425 * the popup, or %NULL
1426 * @func: (allow-none): a user supplied function used to position the menu,
1428 * @data: (allow-none): user supplied data to be passed to @func
1429 * @destroy: (allow-none): destroy notify for @data
1430 * @button: the mouse button which was pressed to initiate the event
1431 * @activate_time: the time at which the activation event occurred
1433 * Displays a menu and makes it available for selection.
1435 * Applications can use this function to display context-sensitive menus,
1436 * and will typically supply %NULL for the @parent_menu_shell,
1437 * @parent_menu_item, @func, @data and @destroy parameters. The default
1438 * menu positioning function will position the menu at the current position
1439 * of @device (or its corresponding pointer).
1441 * The @button parameter should be the mouse button pressed to initiate
1442 * the menu popup. If the menu popup was initiated by something other than
1443 * a mouse button press, such as a mouse button release or a keypress,
1444 * @button should be 0.
1446 * The @activate_time parameter is used to conflict-resolve initiation of
1447 * concurrent requests for mouse/keyboard grab requests. To function
1448 * properly, this needs to be the time stamp of the user event (such as
1449 * a mouse click or key press) that caused the initiation of the popup.
1450 * Only if no such event is available, gtk_get_current_event_time() can
1456 gtk_menu_popup_for_device (GtkMenu *menu,
1458 GtkWidget *parent_menu_shell,
1459 GtkWidget *parent_menu_item,
1460 GtkMenuPositionFunc func,
1462 GDestroyNotify destroy,
1464 guint32 activate_time)
1467 GtkWidget *xgrab_shell;
1469 GdkEvent *current_event;
1470 GtkMenuShell *menu_shell;
1471 gboolean grab_keyboard;
1472 GtkMenuPrivate *priv;
1473 GtkWidget *parent_toplevel;
1474 GdkDevice *keyboard, *pointer;
1476 g_return_if_fail (GTK_IS_MENU (menu));
1477 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1480 device = gtk_get_current_event_device ();
1484 GdkDisplay *display;
1485 GdkDeviceManager *device_manager;
1488 display = gtk_widget_get_display (GTK_WIDGET (menu));
1489 device_manager = gdk_display_get_device_manager (display);
1490 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1492 device = devices->data;
1494 g_list_free (devices);
1497 widget = GTK_WIDGET (menu);
1498 menu_shell = GTK_MENU_SHELL (menu);
1499 priv = gtk_menu_get_private (menu);
1501 if (device->source == GDK_SOURCE_KEYBOARD)
1504 pointer = gdk_device_get_associated_device (device);
1509 keyboard = gdk_device_get_associated_device (device);
1512 menu_shell->parent_menu_shell = parent_menu_shell;
1514 priv->seen_item_enter = FALSE;
1516 /* Find the last viewable ancestor, and make an X grab on it
1518 parent = GTK_WIDGET (menu);
1522 gboolean viewable = TRUE;
1523 GtkWidget *tmp = parent;
1527 if (!gtk_widget_get_mapped (tmp))
1532 tmp = gtk_widget_get_parent (tmp);
1536 xgrab_shell = parent;
1538 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1541 /* We want to receive events generated when we map the menu; unfortunately,
1542 * since there is probably already an implicit grab in place from the
1543 * button that the user used to pop up the menu, we won't receive then --
1544 * in particular, the EnterNotify when the menu pops up under the pointer.
1546 * If we are grabbing on a parent menu shell, no problem; just grab on
1547 * that menu shell first before popping up the window with owner_events = TRUE.
1549 * When grabbing on the menu itself, things get more convuluted - we
1550 * we do an explicit grab on a specially created window with
1551 * owner_events = TRUE, which we override further down with a grab
1552 * on the menu. (We can't grab on the menu until it is mapped; we
1553 * probably could just leave the grab on the other window, with a
1554 * little reorganization of the code in gtkmenu*).
1556 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1557 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1562 if (xgrab_shell && xgrab_shell != widget)
1564 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1566 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1567 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1572 GdkWindow *transfer_window;
1574 xgrab_shell = widget;
1575 transfer_window = menu_grab_transfer_window_get (menu);
1576 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1578 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1579 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1583 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1585 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1586 * with a stuck up window, we just abort here. Presumably the user will
1589 menu_shell->parent_menu_shell = NULL;
1590 menu_grab_transfer_window_destroy (menu);
1594 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1595 menu_shell->active = TRUE;
1596 menu_shell->button = button;
1598 /* If we are popping up the menu from something other than, a button
1599 * press then, as a heuristic, we ignore enter events for the menu
1600 * until we get a MOTION_NOTIFY.
1603 current_event = gtk_get_current_event ();
1606 if ((current_event->type != GDK_BUTTON_PRESS) &&
1607 (current_event->type != GDK_ENTER_NOTIFY))
1608 menu_shell->ignore_enter = TRUE;
1610 gdk_event_free (current_event);
1613 menu_shell->ignore_enter = TRUE;
1617 gtk_menu_tearoff_bg_copy (menu);
1619 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1622 parent_toplevel = NULL;
1623 if (parent_menu_shell)
1624 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1625 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1627 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1629 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1632 /* Set transient for to get the right window group and parent relationship */
1633 if (GTK_IS_WINDOW (parent_toplevel))
1634 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1635 GTK_WINDOW (parent_toplevel));
1637 menu->parent_menu_item = parent_menu_item;
1638 menu->position_func = func;
1639 menu->position_func_data = data;
1640 priv->position_func_data_destroy = destroy;
1641 menu_shell->activate_time = activate_time;
1643 /* We need to show the menu here rather in the init function because
1644 * code expects to be able to tell if the menu is onscreen by
1645 * looking at the gtk_widget_get_visible (menu)
1647 gtk_widget_show (GTK_WIDGET (menu));
1649 /* Position the menu, possibly changing the size request
1651 gtk_menu_position (menu);
1653 /* Compute the size of the toplevel and realize it so we
1654 * can scroll correctly.
1657 GtkRequisition tmp_request;
1658 GtkAllocation tmp_allocation = { 0, };
1660 /* Instead of trusting the menu position function to queue a resize when the
1661 * menu goes out of bounds, invalidate the cached size here. */
1662 gtk_widget_queue_resize (GTK_WIDGET (menu));
1663 gtk_size_request_get_size (GTK_SIZE_REQUEST (menu->toplevel), &tmp_request, NULL);
1665 tmp_allocation.width = tmp_request.width;
1666 tmp_allocation.height = tmp_request.height;
1668 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1670 gtk_widget_realize (GTK_WIDGET (menu));
1673 gtk_menu_scroll_to (menu, menu->scroll_offset);
1675 /* if no item is selected, select the first one */
1676 if (!menu_shell->active_menu_item)
1678 gboolean touchscreen_mode;
1680 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1681 "gtk-touchscreen-mode", &touchscreen_mode,
1684 if (touchscreen_mode)
1685 gtk_menu_shell_select_first (menu_shell, TRUE);
1688 /* Once everything is set up correctly, map the toplevel window on
1691 gtk_widget_show (menu->toplevel);
1693 if (xgrab_shell == widget)
1694 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1696 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1698 if (parent_menu_shell)
1700 gboolean keyboard_mode;
1702 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1703 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1705 else if (menu_shell->button == 0) /* a keynav-activated context menu */
1706 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1708 _gtk_menu_shell_update_mnemonics (menu_shell);
1713 * @menu: a #GtkMenu.
1714 * @parent_menu_shell: (allow-none): the menu shell containing the triggering menu item, or %NULL
1715 * @parent_menu_item: (allow-none): the menu item whose activation triggered the popup, or %NULL
1716 * @func: (allow-none): a user supplied function used to position the menu, or %NULL
1717 * @data: (allow-none): user supplied data to be passed to @func.
1718 * @button: the mouse button which was pressed to initiate the event.
1719 * @activate_time: the time at which the activation event occurred.
1721 * Displays a menu and makes it available for selection. Applications can use
1722 * this function to display context-sensitive menus, and will typically supply
1723 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1724 * parameters. The default menu positioning function will position the menu
1725 * at the current mouse cursor position.
1727 * The @button parameter should be the mouse button pressed to initiate
1728 * the menu popup. If the menu popup was initiated by something other than
1729 * a mouse button press, such as a mouse button release or a keypress,
1730 * @button should be 0.
1732 * The @activate_time parameter is used to conflict-resolve initiation of
1733 * concurrent requests for mouse/keyboard grab requests. To function
1734 * properly, this needs to be the time stamp of the user event (such as
1735 * a mouse click or key press) that caused the initiation of the popup.
1736 * Only if no such event is available, gtk_get_current_event_time() can
1740 gtk_menu_popup (GtkMenu *menu,
1741 GtkWidget *parent_menu_shell,
1742 GtkWidget *parent_menu_item,
1743 GtkMenuPositionFunc func,
1746 guint32 activate_time)
1748 g_return_if_fail (GTK_IS_MENU (menu));
1750 gtk_menu_popup_for_device (menu,
1755 button, activate_time);
1759 gtk_menu_popdown (GtkMenu *menu)
1761 GtkMenuPrivate *private;
1762 GtkMenuShell *menu_shell;
1765 g_return_if_fail (GTK_IS_MENU (menu));
1767 menu_shell = GTK_MENU_SHELL (menu);
1768 private = gtk_menu_get_private (menu);
1770 menu_shell->parent_menu_shell = NULL;
1771 menu_shell->active = FALSE;
1772 menu_shell->ignore_enter = FALSE;
1774 private->have_position = FALSE;
1776 gtk_menu_stop_scrolling (menu);
1778 gtk_menu_stop_navigating_submenu (menu);
1780 if (menu_shell->active_menu_item)
1782 if (menu->old_active_menu_item)
1783 g_object_unref (menu->old_active_menu_item);
1784 menu->old_active_menu_item = menu_shell->active_menu_item;
1785 g_object_ref (menu->old_active_menu_item);
1788 gtk_menu_shell_deselect (menu_shell);
1790 /* The X Grab, if present, will automatically be removed when we hide
1792 gtk_widget_hide (menu->toplevel);
1793 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1795 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1799 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1801 if (gtk_bin_get_child (GTK_BIN (menu->toplevel)))
1803 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1807 /* We popped up the menu from the tearoff, so we need to
1808 * release the grab - we aren't actually hiding the menu.
1810 if (menu_shell->have_xgrab && pointer)
1812 GdkDevice *keyboard;
1814 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1815 keyboard = gdk_device_get_associated_device (pointer);
1818 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1822 /* gtk_menu_popdown is called each time a menu item is selected from
1823 * a torn off menu. Only scroll back to the saved position if the
1824 * non-tearoff menu was popped down.
1826 if (!menu->tearoff_active)
1827 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1828 menu->tearoff_active = TRUE;
1831 gtk_widget_hide (GTK_WIDGET (menu));
1833 menu_shell->have_xgrab = FALSE;
1836 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1838 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1840 menu_grab_transfer_window_destroy (menu);
1844 gtk_menu_get_active (GtkMenu *menu)
1849 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1851 if (!menu->old_active_menu_item)
1854 children = GTK_MENU_SHELL (menu)->children;
1858 child = children->data;
1859 children = children->next;
1861 if (gtk_bin_get_child (GTK_BIN (child)))
1866 menu->old_active_menu_item = child;
1867 if (menu->old_active_menu_item)
1868 g_object_ref (menu->old_active_menu_item);
1871 return menu->old_active_menu_item;
1875 gtk_menu_set_active (GtkMenu *menu,
1881 g_return_if_fail (GTK_IS_MENU (menu));
1883 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1886 child = tmp_list->data;
1887 if (gtk_bin_get_child (GTK_BIN (child)))
1889 if (menu->old_active_menu_item)
1890 g_object_unref (menu->old_active_menu_item);
1891 menu->old_active_menu_item = child;
1892 g_object_ref (menu->old_active_menu_item);
1899 * gtk_menu_set_accel_group:
1900 * @accel_group: (allow-none):
1903 gtk_menu_set_accel_group (GtkMenu *menu,
1904 GtkAccelGroup *accel_group)
1906 g_return_if_fail (GTK_IS_MENU (menu));
1908 if (menu->accel_group != accel_group)
1910 if (menu->accel_group)
1911 g_object_unref (menu->accel_group);
1912 menu->accel_group = accel_group;
1913 if (menu->accel_group)
1914 g_object_ref (menu->accel_group);
1915 _gtk_menu_refresh_accel_paths (menu, TRUE);
1920 gtk_menu_get_accel_group (GtkMenu *menu)
1922 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1924 return menu->accel_group;
1928 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1931 /* Menu items chain here to figure whether they can activate their
1932 * accelerators. Unlike ordinary widgets, menus allow accel
1933 * activation even if invisible since that's the usual case for
1934 * submenus/popup-menus. however, the state of the attach widget
1935 * affects the "activeness" of the menu.
1937 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1940 return gtk_widget_can_activate_accel (awidget, signal_id);
1942 return gtk_widget_is_sensitive (widget);
1946 * gtk_menu_set_accel_path
1947 * @menu: a valid #GtkMenu
1948 * @accel_path: (allow-none): a valid accelerator path
1950 * Sets an accelerator path for this menu from which accelerator paths
1951 * for its immediate children, its menu items, can be constructed.
1952 * The main purpose of this function is to spare the programmer the
1953 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1954 * each menu item that should support runtime user changable accelerators.
1955 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1956 * each menu item of this menu, that contains a label describing its purpose,
1957 * automatically gets an accel path assigned. For example, a menu containing
1958 * menu items "New" and "Exit", will, after
1959 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1960 * has been called, assign its items the accel paths:
1961 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1962 * Assigning accel paths to menu items then enables the user to change
1963 * their accelerators at runtime. More details about accelerator paths
1964 * and their default setups can be found at gtk_accel_map_add_entry().
1966 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1967 * pass a static string, you can save some memory by interning it first with
1968 * g_intern_static_string().
1971 gtk_menu_set_accel_path (GtkMenu *menu,
1972 const gchar *accel_path)
1974 g_return_if_fail (GTK_IS_MENU (menu));
1976 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1978 /* FIXME: accel_path should be defined as const gchar* */
1979 menu->accel_path = (gchar*)g_intern_string (accel_path);
1980 if (menu->accel_path)
1981 _gtk_menu_refresh_accel_paths (menu, FALSE);
1985 * gtk_menu_get_accel_path
1986 * @menu: a valid #GtkMenu
1988 * Retrieves the accelerator path set on the menu.
1990 * Returns: the accelerator path set on the menu.
1995 gtk_menu_get_accel_path (GtkMenu *menu)
1997 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1999 return menu->accel_path;
2004 gboolean group_changed;
2008 refresh_accel_paths_foreach (GtkWidget *widget,
2011 AccelPropagation *prop = data;
2013 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
2014 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2015 prop->menu->accel_path,
2016 prop->menu->accel_group,
2017 prop->group_changed);
2021 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
2022 gboolean group_changed)
2024 g_return_if_fail (GTK_IS_MENU (menu));
2026 if (menu->accel_path && menu->accel_group)
2028 AccelPropagation prop;
2031 prop.group_changed = group_changed;
2032 gtk_container_foreach (GTK_CONTAINER (menu),
2033 refresh_accel_paths_foreach,
2039 gtk_menu_reposition (GtkMenu *menu)
2041 g_return_if_fail (GTK_IS_MENU (menu));
2043 if (!menu->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2044 gtk_menu_position (menu);
2048 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2051 g_return_if_fail (GTK_IS_MENU (menu));
2053 if (adjustment->value != menu->scroll_offset)
2054 gtk_menu_scroll_to (menu, adjustment->value);
2058 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2061 GdkGeometry geometry_hints;
2062 GtkMenuPrivate *priv;
2064 if (!menu->tearoff_window)
2067 priv = gtk_menu_get_private (menu);
2069 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2071 GtkRequisition requisition;
2073 gtk_size_request_get_size (GTK_SIZE_REQUEST (menu->tearoff_scrollbar),
2074 &requisition, NULL);
2075 width += requisition.width;
2078 geometry_hints.min_width = width;
2079 geometry_hints.max_width = width;
2081 geometry_hints.min_height = 0;
2082 geometry_hints.max_height = priv->requested_height;
2084 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
2087 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2091 gtk_menu_update_title (GtkMenu *menu)
2093 if (menu->tearoff_window)
2096 GtkWidget *attach_widget;
2098 title = gtk_menu_get_title (menu);
2101 attach_widget = gtk_menu_get_attach_widget (menu);
2102 if (GTK_IS_MENU_ITEM (attach_widget))
2104 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2105 if (GTK_IS_LABEL (child))
2106 title = gtk_label_get_text (GTK_LABEL (child));
2111 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
2116 gtk_menu_get_toplevel (GtkWidget *menu)
2118 GtkWidget *attach, *toplevel;
2120 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2122 if (GTK_IS_MENU_ITEM (attach))
2123 attach = gtk_widget_get_parent (attach);
2125 if (GTK_IS_MENU (attach))
2126 return gtk_menu_get_toplevel (attach);
2127 else if (GTK_IS_WIDGET (attach))
2129 toplevel = gtk_widget_get_toplevel (attach);
2130 if (gtk_widget_is_toplevel (toplevel))
2138 tearoff_window_destroyed (GtkWidget *widget,
2141 gtk_menu_set_tearoff_state (menu, FALSE);
2145 gtk_menu_set_tearoff_state (GtkMenu *menu,
2149 GtkMenuPrivate *priv;
2151 g_return_if_fail (GTK_IS_MENU (menu));
2153 priv = gtk_menu_get_private (menu);
2155 if (menu->torn_off != torn_off)
2157 menu->torn_off = torn_off;
2158 menu->tearoff_active = torn_off;
2162 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2163 gtk_menu_popdown (menu);
2165 if (!menu->tearoff_window)
2167 GtkWidget *toplevel;
2169 menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2170 "type", GTK_WINDOW_TOPLEVEL,
2171 "screen", gtk_widget_get_screen (menu->toplevel),
2172 "app-paintable", TRUE,
2175 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
2176 GDK_WINDOW_TYPE_HINT_MENU);
2177 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
2178 g_signal_connect (menu->tearoff_window, "destroy",
2179 G_CALLBACK (tearoff_window_destroyed), menu);
2180 g_signal_connect (menu->tearoff_window, "event",
2181 G_CALLBACK (gtk_menu_window_event), menu);
2183 gtk_menu_update_title (menu);
2185 gtk_widget_realize (menu->tearoff_window);
2187 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2188 if (toplevel != NULL)
2189 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
2190 GTK_WINDOW (toplevel));
2192 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
2193 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
2195 gdk_drawable_get_size (gtk_widget_get_window (GTK_WIDGET (menu)),
2197 menu->tearoff_adjustment =
2198 GTK_ADJUSTMENT (gtk_adjustment_new (0,
2200 priv->requested_height,
2204 g_object_connect (menu->tearoff_adjustment,
2205 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2207 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
2209 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
2210 menu->tearoff_scrollbar,
2213 if (menu->tearoff_adjustment->upper > height)
2214 gtk_widget_show (menu->tearoff_scrollbar);
2216 gtk_widget_show (menu->tearoff_hbox);
2219 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
2221 gdk_drawable_get_size (gtk_widget_get_window (GTK_WIDGET (menu)),
2224 /* Update menu->requisition
2226 gtk_size_request_get_size (GTK_SIZE_REQUEST (menu),
2229 gtk_menu_set_tearoff_hints (menu, width);
2231 gtk_widget_realize (menu->tearoff_window);
2232 gtk_menu_position (menu);
2234 gtk_widget_show (GTK_WIDGET (menu));
2235 gtk_widget_show (menu->tearoff_window);
2237 gtk_menu_scroll_to (menu, 0);
2242 gtk_widget_hide (GTK_WIDGET (menu));
2243 gtk_widget_hide (menu->tearoff_window);
2244 if (GTK_IS_CONTAINER (menu->toplevel))
2245 gtk_menu_reparent (menu, menu->toplevel, FALSE);
2246 gtk_widget_destroy (menu->tearoff_window);
2248 menu->tearoff_window = NULL;
2249 menu->tearoff_hbox = NULL;
2250 menu->tearoff_scrollbar = NULL;
2251 menu->tearoff_adjustment = NULL;
2254 g_object_notify (G_OBJECT (menu), "tearoff-state");
2259 * gtk_menu_get_tearoff_state:
2262 * Returns whether the menu is torn off. See
2263 * gtk_menu_set_tearoff_state ().
2265 * Return value: %TRUE if the menu is currently torn off.
2268 gtk_menu_get_tearoff_state (GtkMenu *menu)
2270 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2272 return menu->torn_off;
2276 * gtk_menu_set_title:
2278 * @title: a string containing the title for the menu.
2280 * Sets the title string for the menu. The title is displayed when the menu
2281 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
2282 * attached to a parent menu item, and if so it will try to use the same text as
2283 * that menu item's label.
2286 gtk_menu_set_title (GtkMenu *menu,
2289 GtkMenuPrivate *priv;
2292 g_return_if_fail (GTK_IS_MENU (menu));
2294 priv = gtk_menu_get_private (menu);
2296 old_title = priv->title;
2297 priv->title = g_strdup (title);
2300 gtk_menu_update_title (menu);
2301 g_object_notify (G_OBJECT (menu), "tearoff-title");
2305 * gtk_menu_get_title:
2308 * Returns the title of the menu. See gtk_menu_set_title().
2310 * Return value: the title of the menu, or %NULL if the menu has no
2311 * title set on it. This string is owned by the widget and should
2312 * not be modified or freed.
2314 G_CONST_RETURN gchar *
2315 gtk_menu_get_title (GtkMenu *menu)
2317 GtkMenuPrivate *priv;
2319 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2321 priv = gtk_menu_get_private (menu);
2327 gtk_menu_reorder_child (GtkMenu *menu,
2331 GtkMenuShell *menu_shell;
2333 g_return_if_fail (GTK_IS_MENU (menu));
2334 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2336 menu_shell = GTK_MENU_SHELL (menu);
2338 if (g_list_find (menu_shell->children, child))
2340 menu_shell->children = g_list_remove (menu_shell->children, child);
2341 menu_shell->children = g_list_insert (menu_shell->children, child, position);
2343 menu_queue_resize (menu);
2348 gtk_menu_style_set (GtkWidget *widget,
2349 GtkStyle *previous_style)
2351 if (gtk_widget_get_realized (widget))
2353 GtkMenu *menu = GTK_MENU (widget);
2356 style = gtk_widget_get_style (widget);
2358 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2359 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2360 gtk_style_set_background (style, gtk_widget_get_window (widget), GTK_STATE_NORMAL);
2365 get_arrows_border (GtkMenu *menu,
2368 guint scroll_arrow_height;
2369 GtkArrowPlacement arrow_placement;
2371 gtk_widget_style_get (GTK_WIDGET (menu),
2372 "scroll-arrow-vlength", &scroll_arrow_height,
2373 "arrow_placement", &arrow_placement,
2376 switch (arrow_placement)
2378 case GTK_ARROWS_BOTH:
2379 border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2380 border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2383 case GTK_ARROWS_START:
2384 border->top = (menu->upper_arrow_visible ||
2385 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2389 case GTK_ARROWS_END:
2391 border->bottom = (menu->upper_arrow_visible ||
2392 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2396 border->left = border->right = 0;
2400 gtk_menu_realize (GtkWidget *widget)
2402 GtkAllocation allocation;
2405 GdkWindowAttr attributes;
2406 gint attributes_mask;
2409 GtkMenuPrivate *priv;
2412 guint vertical_padding;
2413 guint horizontal_padding;
2414 GtkBorder arrow_border;
2416 g_return_if_fail (GTK_IS_MENU (widget));
2418 menu = GTK_MENU (widget);
2419 priv = gtk_menu_get_private (menu);
2421 gtk_widget_set_realized (widget, TRUE);
2423 gtk_widget_get_allocation (widget, &allocation);
2425 attributes.window_type = GDK_WINDOW_CHILD;
2426 attributes.x = allocation.x;
2427 attributes.y = allocation.y;
2428 attributes.width = allocation.width;
2429 attributes.height = allocation.height;
2430 attributes.wclass = GDK_INPUT_OUTPUT;
2431 attributes.visual = gtk_widget_get_visual (widget);
2432 attributes.event_mask = gtk_widget_get_events (widget);
2433 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2434 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2436 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2438 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2439 &attributes, attributes_mask);
2440 gtk_widget_set_window (widget, window);
2441 gdk_window_set_user_data (window, widget);
2443 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2445 style = gtk_widget_get_style (widget);
2446 gtk_widget_style_get (GTK_WIDGET (menu),
2447 "vertical-padding", &vertical_padding,
2448 "horizontal-padding", &horizontal_padding,
2451 gtk_widget_get_allocation (widget, &allocation);
2453 attributes.x = border_width + style->xthickness + horizontal_padding;
2454 attributes.y = border_width + style->ythickness + vertical_padding;
2455 attributes.width = MAX (1, allocation.width - attributes.x * 2);
2456 attributes.height = MAX (1, allocation.height - attributes.y * 2);
2458 get_arrows_border (menu, &arrow_border);
2459 attributes.y += arrow_border.top;
2460 attributes.height -= arrow_border.top;
2461 attributes.height -= arrow_border.bottom;
2463 menu->view_window = gdk_window_new (window,
2464 &attributes, attributes_mask);
2465 gdk_window_set_user_data (menu->view_window, menu);
2467 gtk_widget_get_allocation (widget, &allocation);
2471 attributes.width = MAX (1, allocation.width - (border_width + style->xthickness + horizontal_padding) * 2);
2472 attributes.height = MAX (1, priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2);
2474 menu->bin_window = gdk_window_new (menu->view_window,
2475 &attributes, attributes_mask);
2476 gdk_window_set_user_data (menu->bin_window, menu);
2478 children = GTK_MENU_SHELL (menu)->children;
2481 child = children->data;
2482 children = children->next;
2484 gtk_widget_set_parent_window (child, menu->bin_window);
2487 gtk_widget_style_attach (widget);
2488 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2489 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2490 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
2492 if (GTK_MENU_SHELL (widget)->active_menu_item)
2493 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2494 GTK_MENU_SHELL (widget)->active_menu_item);
2496 gdk_window_show (menu->bin_window);
2497 gdk_window_show (menu->view_window);
2501 gtk_menu_focus (GtkWidget *widget,
2502 GtkDirectionType direction)
2505 * A menu or its menu items cannot have focus
2510 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2513 menu_grab_transfer_window_get (GtkMenu *menu)
2515 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2518 GdkWindowAttr attributes;
2519 gint attributes_mask;
2521 attributes.x = -100;
2522 attributes.y = -100;
2523 attributes.width = 10;
2524 attributes.height = 10;
2525 attributes.window_type = GDK_WINDOW_TEMP;
2526 attributes.wclass = GDK_INPUT_ONLY;
2527 attributes.override_redirect = TRUE;
2528 attributes.event_mask = 0;
2530 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2532 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2533 &attributes, attributes_mask);
2534 gdk_window_set_user_data (window, menu);
2536 gdk_window_show (window);
2538 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2545 menu_grab_transfer_window_destroy (GtkMenu *menu)
2547 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2550 gdk_window_set_user_data (window, NULL);
2551 gdk_window_destroy (window);
2552 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2557 gtk_menu_unrealize (GtkWidget *widget)
2559 GtkMenu *menu = GTK_MENU (widget);
2561 menu_grab_transfer_window_destroy (menu);
2563 gdk_window_set_user_data (menu->view_window, NULL);
2564 gdk_window_destroy (menu->view_window);
2565 menu->view_window = NULL;
2567 gdk_window_set_user_data (menu->bin_window, NULL);
2568 gdk_window_destroy (menu->bin_window);
2569 menu->bin_window = NULL;
2571 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2576 calculate_line_heights (GtkMenu *menu,
2578 guint **ret_min_heights,
2579 guint **ret_nat_heights)
2581 GtkMenuShell *menu_shell;
2582 GtkMenuPrivate *priv;
2583 GtkWidget *child, *widget;
2585 guint horizontal_padding;
2593 widget = GTK_WIDGET (menu);
2594 menu_shell = GTK_MENU_SHELL (widget);
2595 priv = gtk_menu_get_private (menu);
2597 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2598 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2599 n_heights = gtk_menu_get_n_rows (menu);
2600 n_columns = gtk_menu_get_n_columns (menu);
2601 avail_width = for_width - (2 * menu->toggle_size + priv->accel_size) * n_columns;
2603 gtk_widget_style_get (GTK_WIDGET (menu),
2604 "horizontal-padding", &horizontal_padding,
2607 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2608 avail_width -= (border_width + horizontal_padding + gtk_widget_get_style (widget)->xthickness) * 2;
2610 for (children = menu_shell->children; children; children = children->next)
2615 gint child_min, child_nat;
2617 child = children->data;
2619 if (! gtk_widget_get_visible (child))
2622 get_effective_child_attach (child, &l, &r, &t, &b);
2624 part = avail_width / (r - l);
2626 gtk_size_request_get_height_for_width (GTK_SIZE_REQUEST (child), part,
2627 &child_min, &child_nat);
2629 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2631 part = MAX (child_min, toggle_size) / (b - t);
2632 min_heights[t] = MAX (min_heights[t], part);
2634 part = MAX (child_nat, toggle_size) / (b - t);
2635 nat_heights[t] = MAX (nat_heights[t], part);
2638 if (ret_min_heights)
2639 *ret_min_heights = min_heights;
2641 g_free (min_heights);
2643 if (ret_nat_heights)
2644 *ret_nat_heights = nat_heights;
2646 g_free (nat_heights);
2652 gtk_menu_size_allocate (GtkWidget *widget,
2653 GtkAllocation *allocation)
2656 GtkMenuShell *menu_shell;
2658 GtkAllocation child_allocation;
2659 GtkMenuPrivate *priv;
2665 guint vertical_padding;
2666 guint horizontal_padding;
2668 g_return_if_fail (GTK_IS_MENU (widget));
2669 g_return_if_fail (allocation != NULL);
2671 menu = GTK_MENU (widget);
2672 menu_shell = GTK_MENU_SHELL (widget);
2673 priv = gtk_menu_get_private (menu);
2675 gtk_widget_set_allocation (widget, allocation);
2677 style = gtk_widget_get_style (widget);
2679 gtk_widget_style_get (GTK_WIDGET (menu),
2680 "vertical-padding", &vertical_padding,
2681 "horizontal-padding", &horizontal_padding,
2683 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2685 g_free (priv->heights);
2686 priv->heights_length =
2687 calculate_line_heights (menu,
2692 /* refresh our cached height request */
2693 priv->requested_height = (border_width + vertical_padding + style->ythickness) * 2;
2694 for (i = 0; i < priv->heights_length; i++)
2695 priv->requested_height += priv->heights[i];
2697 x = border_width + style->xthickness + horizontal_padding;
2698 y = border_width + style->ythickness + vertical_padding;
2700 width = MAX (1, allocation->width - x * 2);
2701 height = MAX (1, allocation->height - y * 2);
2703 if (menu_shell->active)
2704 gtk_menu_scroll_to (menu, menu->scroll_offset);
2706 if (!menu->tearoff_active)
2708 GtkBorder arrow_border;
2710 get_arrows_border (menu, &arrow_border);
2711 y += arrow_border.top;
2712 height -= arrow_border.top;
2713 height -= arrow_border.bottom;
2716 if (gtk_widget_get_realized (widget))
2718 gdk_window_move_resize (gtk_widget_get_window (widget),
2719 allocation->x, allocation->y,
2720 allocation->width, allocation->height);
2722 gdk_window_move_resize (menu->view_window,
2729 if (menu_shell->children)
2731 gint base_width = width / gtk_menu_get_n_columns (menu);
2733 children = menu_shell->children;
2736 child = children->data;
2737 children = children->next;
2739 if (gtk_widget_get_visible (child))
2744 get_effective_child_attach (child, &l, &r, &t, &b);
2746 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2749 tmp = gtk_menu_get_n_columns (menu) - l;
2750 l = gtk_menu_get_n_columns (menu) - r;
2754 child_allocation.width = (r - l) * base_width;
2755 child_allocation.height = 0;
2756 child_allocation.x = l * base_width;
2757 child_allocation.y = 0;
2759 for (i = 0; i < b; i++)
2762 child_allocation.y += priv->heights[i];
2764 child_allocation.height += priv->heights[i];
2767 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2770 gtk_widget_size_allocate (child, &child_allocation);
2771 gtk_widget_queue_draw (child);
2775 /* Resize the item window */
2776 if (gtk_widget_get_realized (widget))
2782 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2783 height += priv->heights[i];
2785 width = gtk_menu_get_n_columns (menu) * base_width;
2786 gdk_window_resize (menu->bin_window, width, height);
2789 if (menu->tearoff_active)
2791 if (height >= priv->requested_height)
2793 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2795 gtk_widget_hide (menu->tearoff_scrollbar);
2796 gtk_menu_set_tearoff_hints (menu, allocation->width);
2798 gtk_menu_scroll_to (menu, 0);
2803 menu->tearoff_adjustment->upper = priv->requested_height;
2804 menu->tearoff_adjustment->page_size = allocation->height;
2806 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2807 menu->tearoff_adjustment->upper)
2810 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2813 gtk_menu_scroll_to (menu, value);
2816 gtk_adjustment_changed (menu->tearoff_adjustment);
2818 if (!gtk_widget_get_visible (menu->tearoff_scrollbar))
2820 gtk_widget_show (menu->tearoff_scrollbar);
2821 gtk_menu_set_tearoff_hints (menu, allocation->width);
2829 get_arrows_visible_area (GtkMenu *menu,
2830 GdkRectangle *border,
2831 GdkRectangle *upper,
2832 GdkRectangle *lower,
2835 GtkArrowPlacement arrow_placement;
2837 GtkWidget *widget = GTK_WIDGET (menu);
2839 guint vertical_padding;
2840 guint horizontal_padding;
2841 gint scroll_arrow_height;
2843 style = gtk_widget_get_style (widget);
2845 gtk_widget_style_get (widget,
2846 "vertical-padding", &vertical_padding,
2847 "horizontal-padding", &horizontal_padding,
2848 "scroll-arrow-vlength", &scroll_arrow_height,
2849 "arrow-placement", &arrow_placement,
2852 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2853 border->x = border_width + style->xthickness + horizontal_padding;
2854 border->y = border_width + style->ythickness + vertical_padding;
2855 gdk_drawable_get_size (gtk_widget_get_window (widget),
2856 &border->width, &border->height);
2858 switch (arrow_placement)
2860 case GTK_ARROWS_BOTH:
2861 upper->x = border->x;
2862 upper->y = border->y;
2863 upper->width = border->width - 2 * border->x;
2864 upper->height = scroll_arrow_height;
2866 lower->x = border->x;
2867 lower->y = border->height - border->y - scroll_arrow_height;
2868 lower->width = border->width - 2 * border->x;
2869 lower->height = scroll_arrow_height;
2872 case GTK_ARROWS_START:
2873 upper->x = border->x;
2874 upper->y = border->y;
2875 upper->width = (border->width - 2 * border->x) / 2;
2876 upper->height = scroll_arrow_height;
2878 lower->x = border->x + upper->width;
2879 lower->y = border->y;
2880 lower->width = (border->width - 2 * border->x) / 2;
2881 lower->height = scroll_arrow_height;
2884 case GTK_ARROWS_END:
2885 upper->x = border->x;
2886 upper->y = border->height - border->y - scroll_arrow_height;
2887 upper->width = (border->width - 2 * border->x) / 2;
2888 upper->height = scroll_arrow_height;
2890 lower->x = border->x + upper->width;
2891 lower->y = border->height - border->y - scroll_arrow_height;
2892 lower->width = (border->width - 2 * border->x) / 2;
2893 lower->height = scroll_arrow_height;
2897 g_assert_not_reached();
2898 upper->x = upper->y = upper->width = upper->height = 0;
2899 lower->x = lower->y = lower->width = lower->height = 0;
2902 *arrow_space = scroll_arrow_height - 2 * style->ythickness;
2906 gtk_menu_draw (GtkWidget *widget,
2910 GtkMenuPrivate *priv;
2912 GdkRectangle border;
2918 menu = GTK_MENU (widget);
2919 priv = gtk_menu_get_private (menu);
2921 style = gtk_widget_get_style (widget);
2922 window = gtk_widget_get_window (widget);
2924 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2926 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
2928 gfloat arrow_scaling;
2931 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
2932 arrow_size = arrow_scaling * arrow_space;
2934 gtk_cairo_paint_box (style,
2940 gtk_widget_get_allocated_width (widget),
2941 gtk_widget_get_allocated_height (widget));
2943 if (menu->upper_arrow_visible && !menu->tearoff_active)
2945 gtk_cairo_paint_box (style,
2947 priv->upper_arrow_state,
2949 widget, "menu_scroll_arrow_up",
2955 gtk_cairo_paint_arrow (style,
2957 priv->upper_arrow_state,
2959 widget, "menu_scroll_arrow_up",
2962 upper.x + (upper.width - arrow_size) / 2,
2963 upper.y + style->ythickness + (arrow_space - arrow_size) / 2,
2964 arrow_size, arrow_size);
2967 if (menu->lower_arrow_visible && !menu->tearoff_active)
2969 gtk_cairo_paint_box (style,
2971 priv->lower_arrow_state,
2973 widget, "menu_scroll_arrow_down",
2979 gtk_cairo_paint_arrow (style,
2981 priv->lower_arrow_state,
2983 widget, "menu_scroll_arrow_down",
2986 lower.x + (lower.width - arrow_size) / 2,
2987 lower.y + style->ythickness + (arrow_space - arrow_size) / 2,
2988 arrow_size, arrow_size);
2992 if (gtk_cairo_should_draw_window (cr, menu->bin_window))
2995 gint y = -border.y + menu->scroll_offset;
2998 gdk_window_get_position (menu->view_window, &xoff, &yoff);
2999 cairo_translate (cr, xoff, yoff);
3000 gdk_window_get_position (menu->bin_window, &xoff, &yoff);
3001 cairo_translate (cr, xoff, yoff);
3003 if (!menu->tearoff_active)
3005 GtkBorder arrow_border;
3007 get_arrows_border (menu, &arrow_border);
3008 y -= arrow_border.top;
3011 gtk_cairo_paint_box (style,
3017 border.width, border.height);
3022 GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
3028 gtk_menu_show (GtkWidget *widget)
3030 GtkMenu *menu = GTK_MENU (widget);
3032 _gtk_menu_refresh_accel_paths (menu, FALSE);
3034 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3040 gtk_menu_size_request_init (GtkSizeRequestIface *iface)
3042 iface->get_width = gtk_menu_get_width;
3043 iface->get_height = gtk_menu_get_height;
3044 iface->get_height_for_width = gtk_menu_get_height_for_width;
3048 gtk_menu_get_width (GtkSizeRequest *widget,
3053 GtkMenuShell *menu_shell;
3054 GtkMenuPrivate *priv;
3058 guint max_toggle_size;
3059 guint max_accel_width;
3060 guint horizontal_padding;
3062 gint child_min, child_nat;
3063 gint min_width, nat_width;
3065 menu = GTK_MENU (widget);
3066 menu_shell = GTK_MENU_SHELL (widget);
3067 priv = gtk_menu_get_private (menu);
3069 style = gtk_widget_get_style (GTK_WIDGET (widget));
3071 min_width = nat_width = 0;
3073 max_toggle_size = 0;
3074 max_accel_width = 0;
3076 children = menu_shell->children;
3083 child = children->data;
3084 children = children->next;
3086 if (! gtk_widget_get_visible (child))
3089 get_effective_child_attach (child, &l, &r, &t, &b);
3091 /* It's important to size_request the child
3092 * before doing the toggle size request, in
3093 * case the toggle size request depends on the size
3094 * request of a child of the child (e.g. for ImageMenuItem)
3097 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
3098 gtk_size_request_get_width (GTK_SIZE_REQUEST (child), &child_min, &child_nat);
3100 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3101 max_toggle_size = MAX (max_toggle_size, toggle_size);
3102 max_accel_width = MAX (max_accel_width,
3103 GTK_MENU_ITEM (child)->accelerator_width);
3105 part = child_min / (r - l);
3106 min_width = MAX (min_width, part);
3108 part = child_nat / (r - l);
3109 nat_width = MAX (nat_width, part);
3112 /* If the menu doesn't include any images or check items
3113 * reserve the space so that all menus are consistent.
3114 * We only do this for 'ordinary' menus, not for combobox
3115 * menus or multi-column menus
3117 if (max_toggle_size == 0 &&
3118 gtk_menu_get_n_columns (menu) == 1 &&
3119 !priv->no_toggle_size)
3121 guint toggle_spacing;
3122 guint indicator_size;
3124 gtk_style_get (style,
3125 GTK_TYPE_CHECK_MENU_ITEM,
3126 "toggle-spacing", &toggle_spacing,
3127 "indicator-size", &indicator_size,
3130 max_toggle_size = indicator_size + toggle_spacing;
3133 min_width += 2 * max_toggle_size + max_accel_width;
3134 min_width *= gtk_menu_get_n_columns (menu);
3136 nat_width += 2 * max_toggle_size + max_accel_width;
3137 nat_width *= gtk_menu_get_n_columns (menu);
3139 gtk_widget_style_get (GTK_WIDGET (menu),
3140 "horizontal-padding", &horizontal_padding,
3143 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3144 min_width += (border_width + horizontal_padding + style->xthickness) * 2;
3145 nat_width += (border_width + horizontal_padding + style->xthickness) * 2;
3147 menu->toggle_size = max_toggle_size;
3148 priv->accel_size = max_accel_width;
3151 *minimum_size = min_width;
3154 *natural_size = nat_width;
3156 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
3158 if (menu->tearoff_active)
3159 gtk_menu_set_tearoff_hints (menu, min_width);
3163 gtk_menu_get_height (GtkSizeRequest *widget,
3169 /* Menus are height-for-width only, just return the height for the minimum width */
3170 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_width (widget, &min_width, NULL);
3171 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_height_for_width (widget, min_width, minimum_size, natural_size);
3175 gtk_menu_get_height_for_width (GtkSizeRequest *widget,
3180 GtkMenu *menu = GTK_MENU (widget);
3181 GtkMenuPrivate *private = gtk_menu_get_private (menu);
3182 guint *min_heights, *nat_heights;
3183 guint vertical_padding, border_width;
3185 gint min_height, nat_height;
3187 gtk_widget_style_get (GTK_WIDGET (menu), "vertical-padding", &vertical_padding, NULL);
3188 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3190 min_height = nat_height = (border_width + vertical_padding + gtk_widget_get_style (GTK_WIDGET (widget))->ythickness) * 2;
3193 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3195 for (i = 0; i < n_heights; i++)
3197 min_height += min_heights[i];
3198 nat_height += nat_heights[i];
3201 if (private->have_position)
3203 GdkScreen *screen = gtk_widget_get_screen (menu->toplevel);
3204 GdkRectangle monitor;
3206 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3208 if (private->y + min_height > monitor.y + monitor.height)
3209 min_height = monitor.y + monitor.height - private->y;
3211 if (private->y + nat_height > monitor.y + monitor.height)
3212 nat_height = monitor.y + monitor.height - private->y;
3214 if (private->y < monitor.y)
3216 min_height -= monitor.y - private->y;
3217 nat_height -= monitor.y - private->y;
3222 *minimum_size = min_height;
3225 *natural_size = nat_height;
3227 g_free (min_heights);
3228 g_free (nat_heights);
3234 gtk_menu_button_scroll (GtkMenu *menu,
3235 GdkEventButton *event)
3237 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
3239 gboolean touchscreen_mode;
3241 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3242 "gtk-touchscreen-mode", &touchscreen_mode,
3245 if (touchscreen_mode)
3246 gtk_menu_handle_scrolling (menu,
3247 event->x_root, event->y_root,
3248 event->type == GDK_BUTTON_PRESS,
3258 pointer_in_menu_window (GtkWidget *widget,
3262 GtkAllocation allocation;
3263 GtkMenu *menu = GTK_MENU (widget);
3265 if (gtk_widget_get_mapped (menu->toplevel))
3267 GtkMenuShell *menu_shell;
3268 gint window_x, window_y;
3270 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3271 &window_x, &window_y);
3273 gtk_widget_get_allocation (widget, &allocation);
3274 if (x_root >= window_x && x_root < window_x + allocation.width &&
3275 y_root >= window_y && y_root < window_y + allocation.height)
3278 menu_shell = GTK_MENU_SHELL (widget);
3280 if (GTK_IS_MENU (menu_shell->parent_menu_shell))
3281 return pointer_in_menu_window (menu_shell->parent_menu_shell,
3289 gtk_menu_button_press (GtkWidget *widget,
3290 GdkEventButton *event)
3292 if (event->type != GDK_BUTTON_PRESS)
3295 /* Don't pass down to menu shell for presses over scroll arrows
3297 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3300 /* Don't pass down to menu shell if a non-menuitem part of the menu
3301 * was clicked. The check for the event_widget being a GtkMenuShell
3302 * works because we have the pointer grabbed on menu_shell->window
3303 * with owner_events=TRUE, so all events that are either outside
3304 * the menu or on its border are delivered relative to
3305 * menu_shell->window.
3307 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3308 pointer_in_menu_window (widget, event->x_root, event->y_root))
3311 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3315 gtk_menu_button_release (GtkWidget *widget,
3316 GdkEventButton *event)
3318 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3320 if (priv->ignore_button_release)
3322 priv->ignore_button_release = FALSE;
3326 if (event->type != GDK_BUTTON_RELEASE)
3329 /* Don't pass down to menu shell for releases over scroll arrows
3331 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3334 /* Don't pass down to menu shell if a non-menuitem part of the menu
3335 * was clicked (see comment in button_press()).
3337 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3338 pointer_in_menu_window (widget, event->x_root, event->y_root))
3340 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3341 * bail out early here so it is in a consistent state for the
3342 * next button_press/button_release in GtkMenuShell.
3345 if (GTK_MENU_SHELL (widget)->active)
3346 GTK_MENU_SHELL (widget)->button = 0;
3351 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3354 static const gchar *
3355 get_accel_path (GtkWidget *menu_item,
3360 GClosure *accel_closure;
3361 GtkAccelGroup *accel_group;
3363 path = _gtk_widget_get_accel_path (menu_item, locked);
3366 path = GTK_MENU_ITEM (menu_item)->accel_path;
3372 label = gtk_bin_get_child (GTK_BIN (menu_item));
3374 if (GTK_IS_ACCEL_LABEL (label))
3376 g_object_get (label,
3377 "accel-closure", &accel_closure,
3381 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3383 *locked = gtk_accel_group_get_is_locked (accel_group);
3393 gtk_menu_key_press (GtkWidget *widget,
3396 GtkMenuShell *menu_shell;
3398 gboolean delete = FALSE;
3399 gboolean can_change_accels;
3400 gchar *accel = NULL;
3401 guint accel_key, accel_mods;
3402 GdkModifierType consumed_modifiers;
3403 GdkDisplay *display;
3405 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3406 g_return_val_if_fail (event != NULL, FALSE);
3408 menu_shell = GTK_MENU_SHELL (widget);
3409 menu = GTK_MENU (widget);
3411 gtk_menu_stop_navigating_submenu (menu);
3413 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3416 display = gtk_widget_get_display (widget);
3418 g_object_get (gtk_widget_get_settings (widget),
3419 "gtk-menu-bar-accel", &accel,
3420 "gtk-can-change-accels", &can_change_accels,
3423 if (accel && *accel)
3426 GdkModifierType mods = 0;
3428 gtk_accelerator_parse (accel, &keyval, &mods);
3431 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3433 /* FIXME this is wrong, needs to be in the global accel resolution
3434 * thing, to properly consider i18n etc., but that probably requires
3435 * AccelGroup changes etc.
3437 if (event->keyval == keyval && (mods & event->state) == mods)
3439 gtk_menu_shell_cancel (menu_shell);
3447 switch (event->keyval)
3449 case GDK_KEY_Delete:
3450 case GDK_KEY_KP_Delete:
3451 case GDK_KEY_BackSpace:
3458 /* Figure out what modifiers went into determining the key symbol */
3459 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3460 event->hardware_keycode, event->state, event->group,
3461 NULL, NULL, NULL, &consumed_modifiers);
3463 accel_key = gdk_keyval_to_lower (event->keyval);
3464 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3466 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
3467 * We re-upper case when we match against the keyval, but display and save in caseless form.
3469 if (accel_key != event->keyval)
3470 accel_mods |= GDK_SHIFT_MASK;
3472 /* Modify the accelerators */
3473 if (can_change_accels &&
3474 menu_shell->active_menu_item &&
3475 gtk_bin_get_child (GTK_BIN (menu_shell->active_menu_item)) && /* no separators */
3476 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
3477 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3479 GtkWidget *menu_item = menu_shell->active_menu_item;
3480 gboolean locked, replace_accels = TRUE;
3483 path = get_accel_path (menu_item, &locked);
3484 if (!path || locked)
3486 /* can't change accelerators on menu_items without paths
3487 * (basically, those items are accelerator-locked).
3489 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
3490 gtk_widget_error_bell (widget);
3496 /* For the keys that act to delete the current setting, we delete
3497 * the current setting if there is one, otherwise, we set the
3498 * key as the accelerator.
3504 if (gtk_accel_map_lookup_entry (path, &key) &&
3505 (key.accel_key || key.accel_mods))
3511 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3515 /* we failed, probably because this key is in use and
3518 /* g_print("failed to change\n"); */
3519 gtk_widget_error_bell (widget);
3528 check_threshold (GtkWidget *widget,
3537 ABS (start_x - x) > THRESHOLD ||
3538 ABS (start_y - y) > THRESHOLD;
3542 definitely_within_item (GtkWidget *widget,
3546 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
3549 gdk_drawable_get_size (window, &w, &h);
3552 check_threshold (widget, 0, 0, x, y) &&
3553 check_threshold (widget, w - 1, 0, x, y) &&
3554 check_threshold (widget, w - 1, h - 1, x, y) &&
3555 check_threshold (widget, 0, h - 1, x, y);
3559 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3561 GtkMenuPrivate *priv;
3563 priv = gtk_menu_get_private (menu);
3565 return priv->navigation_height && priv->navigation_width;
3569 gtk_menu_motion_notify (GtkWidget *widget,
3570 GdkEventMotion *event)
3572 GtkWidget *menu_item;
3574 GtkMenuShell *menu_shell;
3577 gboolean need_enter;
3579 if (GTK_IS_MENU (widget))
3581 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3583 if (priv->ignore_button_release)
3584 priv->ignore_button_release = FALSE;
3586 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3590 /* We received the event for one of two reasons:
3592 * a) We are the active menu, and did gtk_grab_add()
3593 * b) The widget is a child of ours, and the event was propagated
3595 * Since for computation of navigation regions, we want the menu which
3596 * is the parent of the menu item, for a), we need to find that menu,
3597 * which may be different from 'widget'.
3599 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3600 parent = gtk_widget_get_parent (menu_item);
3601 if (!GTK_IS_MENU_ITEM (menu_item) ||
3602 !GTK_IS_MENU (parent))
3605 menu_shell = GTK_MENU_SHELL (parent);
3606 menu = GTK_MENU (menu_shell);
3608 if (definitely_within_item (menu_item, event->x, event->y))
3609 menu_shell->activate_time = 0;
3611 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->ignore_enter);
3613 /* Check to see if we are within an active submenu's navigation region
3615 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3618 /* Make sure we pop down if we enter a non-selectable menu item, so we
3619 * don't show a submenu when the cursor is outside the stay-up triangle.
3621 if (!_gtk_menu_item_is_selectable (menu_item))
3623 /* We really want to deselect, but this gives the menushell code
3624 * a chance to do some bookkeeping about the menuitem.
3626 gtk_menu_shell_select_item (menu_shell, menu_item);
3632 /* The menu is now sensitive to enter events on its items, but
3633 * was previously sensitive. So we fake an enter event.
3637 menu_shell->ignore_enter = FALSE;
3639 gdk_drawable_get_size (event->window, &width, &height);
3640 if (event->x >= 0 && event->x < width &&
3641 event->y >= 0 && event->y < height)
3643 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3646 send_event->crossing.window = g_object_ref (event->window);
3647 send_event->crossing.time = event->time;
3648 send_event->crossing.send_event = TRUE;
3649 send_event->crossing.x_root = event->x_root;
3650 send_event->crossing.y_root = event->y_root;
3651 send_event->crossing.x = event->x;
3652 send_event->crossing.y = event->y;
3653 send_event->crossing.state = event->state;
3654 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3656 /* We send the event to 'widget', the currently active menu,
3657 * instead of 'menu', the menu that the pointer is in. This
3658 * will ensure that the event will be ignored unless the
3659 * menuitem is a child of the active menu or some parent
3660 * menu of the active menu.
3662 result = gtk_widget_event (widget, send_event);
3663 gdk_event_free (send_event);
3673 get_double_arrows (GtkMenu *menu)
3675 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3676 gboolean double_arrows;
3677 GtkArrowPlacement arrow_placement;
3679 gtk_widget_style_get (GTK_WIDGET (menu),
3680 "double-arrows", &double_arrows,
3681 "arrow-placement", &arrow_placement,
3684 if (arrow_placement != GTK_ARROWS_BOTH)
3687 return double_arrows || (priv->initially_pushed_in &&
3688 menu->scroll_offset != 0);
3692 gtk_menu_scroll_by (GtkMenu *menu,
3695 GtkMenuPrivate *priv;
3696 GtkBorder arrow_border;
3699 gint view_width, view_height;
3700 gboolean double_arrows;
3702 widget = GTK_WIDGET (menu);
3703 offset = menu->scroll_offset + step;
3704 priv = gtk_menu_get_private (menu);
3706 get_arrows_border (menu, &arrow_border);
3708 double_arrows = get_double_arrows (menu);
3710 /* If we scroll upward and the non-visible top part
3711 * is smaller than the scroll arrow it would be
3712 * pretty stupid to show the arrow and taking more
3713 * screen space than just scrolling to the top.
3716 if ((step < 0) && (offset < arrow_border.top))
3719 /* Don't scroll over the top if we weren't before: */
3720 if ((menu->scroll_offset >= 0) && (offset < 0))
3723 gdk_drawable_get_size (gtk_widget_get_window (widget),
3724 &view_width, &view_height);
3726 if (menu->scroll_offset == 0 &&
3727 view_height >= priv->requested_height)
3730 /* Don't scroll past the bottom if we weren't before: */
3731 if (menu->scroll_offset > 0)
3732 view_height -= arrow_border.top;
3734 /* When both arrows are always shown, reduce
3735 * view height even more.
3738 view_height -= arrow_border.bottom;
3740 if ((menu->scroll_offset + view_height <= priv->requested_height) &&
3741 (offset + view_height > priv->requested_height))
3742 offset = priv->requested_height - view_height;
3744 if (offset != menu->scroll_offset)
3745 gtk_menu_scroll_to (menu, offset);
3749 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3750 gboolean touchscreen_mode)
3752 gboolean upper_visible;
3753 gboolean lower_visible;
3755 upper_visible = menu->upper_arrow_visible;
3756 lower_visible = menu->lower_arrow_visible;
3758 gtk_menu_scroll_by (menu, menu->scroll_step);
3760 if (touchscreen_mode &&
3761 (upper_visible != menu->upper_arrow_visible ||
3762 lower_visible != menu->lower_arrow_visible))
3764 /* We are about to hide a scroll arrow while the mouse is pressed,
3765 * this would cause the uncovered menu item to be activated on button
3766 * release. Therefore we need to ignore button release here
3768 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3769 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3774 gtk_menu_scroll_timeout (gpointer data)
3777 gboolean touchscreen_mode;
3779 menu = GTK_MENU (data);
3781 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3782 "gtk-touchscreen-mode", &touchscreen_mode,
3785 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3791 gtk_menu_scroll_timeout_initial (gpointer data)
3795 gboolean touchscreen_mode;
3797 menu = GTK_MENU (data);
3799 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3800 "gtk-timeout-repeat", &timeout,
3801 "gtk-touchscreen-mode", &touchscreen_mode,
3804 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3806 gtk_menu_remove_scroll_timeout (menu);
3808 menu->timeout_id = gdk_threads_add_timeout (timeout,
3809 gtk_menu_scroll_timeout,
3816 gtk_menu_start_scrolling (GtkMenu *menu)
3819 gboolean touchscreen_mode;
3821 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3822 "gtk-timeout-repeat", &timeout,
3823 "gtk-touchscreen-mode", &touchscreen_mode,
3826 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3828 menu->timeout_id = gdk_threads_add_timeout (timeout,
3829 gtk_menu_scroll_timeout_initial,
3834 gtk_menu_scroll (GtkWidget *widget,
3835 GdkEventScroll *event)
3837 GtkMenu *menu = GTK_MENU (widget);
3839 switch (event->direction)
3841 case GDK_SCROLL_RIGHT:
3842 case GDK_SCROLL_DOWN:
3843 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3845 case GDK_SCROLL_LEFT:
3847 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3855 get_arrows_sensitive_area (GtkMenu *menu,
3856 GdkRectangle *upper,
3857 GdkRectangle *lower)
3859 GtkArrowPlacement arrow_placement;
3860 GtkWidget *widget = GTK_WIDGET (menu);
3864 guint vertical_padding;
3866 gint scroll_arrow_height;
3868 window = gtk_widget_get_window (widget);
3870 gdk_drawable_get_size (window, &width, &height);
3872 gtk_widget_style_get (widget,
3873 "vertical-padding", &vertical_padding,
3874 "scroll-arrow-vlength", &scroll_arrow_height,
3875 "arrow-placement", &arrow_placement,
3878 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) +
3879 gtk_widget_get_style (widget)->ythickness + vertical_padding;
3881 gdk_window_get_position (window, &win_x, &win_y);
3883 switch (arrow_placement)
3885 case GTK_ARROWS_BOTH:
3890 upper->width = width;
3891 upper->height = scroll_arrow_height + border;
3897 lower->y = win_y + height - border - scroll_arrow_height;
3898 lower->width = width;
3899 lower->height = scroll_arrow_height + border;
3903 case GTK_ARROWS_START:
3908 upper->width = width / 2;
3909 upper->height = scroll_arrow_height + border;
3914 lower->x = win_x + width / 2;
3916 lower->width = width / 2;
3917 lower->height = scroll_arrow_height + border;
3921 case GTK_ARROWS_END:
3925 upper->y = win_y + height - border - scroll_arrow_height;
3926 upper->width = width / 2;
3927 upper->height = scroll_arrow_height + border;
3932 lower->x = win_x + width / 2;
3933 lower->y = win_y + height - border - scroll_arrow_height;
3934 lower->width = width / 2;
3935 lower->height = scroll_arrow_height + border;
3943 gtk_menu_handle_scrolling (GtkMenu *menu,
3949 GtkMenuShell *menu_shell;
3950 GtkMenuPrivate *priv;
3953 gboolean scroll_fast = FALSE;
3955 gboolean touchscreen_mode;
3957 priv = gtk_menu_get_private (menu);
3959 menu_shell = GTK_MENU_SHELL (menu);
3961 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3962 "gtk-touchscreen-mode", &touchscreen_mode,
3965 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3970 /* upper arrow handling */
3972 get_arrows_sensitive_area (menu, &rect, NULL);
3975 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3976 (x >= rect.x) && (x < rect.x + rect.width) &&
3977 (y >= rect.y) && (y < rect.y + rect.height))
3982 if (touchscreen_mode)
3983 menu->upper_arrow_prelight = in_arrow;
3985 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3987 gboolean arrow_pressed = FALSE;
3989 if (menu->upper_arrow_visible && !menu->tearoff_active)
3991 if (touchscreen_mode)
3993 if (enter && menu->upper_arrow_prelight)
3995 if (menu->timeout_id == 0)
3997 /* Deselect the active item so that
3998 * any submenus are popped down
4000 gtk_menu_shell_deselect (menu_shell);
4002 gtk_menu_remove_scroll_timeout (menu);
4003 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
4007 /* Only do stuff on click. */
4008 gtk_menu_start_scrolling (menu);
4009 arrow_pressed = TRUE;
4014 arrow_pressed = TRUE;
4019 gtk_menu_stop_scrolling (menu);
4022 else /* !touchscreen_mode */
4024 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
4026 if (enter && in_arrow &&
4027 (!menu->upper_arrow_prelight ||
4028 menu->scroll_fast != scroll_fast))
4030 menu->upper_arrow_prelight = TRUE;
4031 menu->scroll_fast = scroll_fast;
4033 /* Deselect the active item so that
4034 * any submenus are popped down
4036 gtk_menu_shell_deselect (menu_shell);
4038 gtk_menu_remove_scroll_timeout (menu);
4039 menu->scroll_step = scroll_fast ?
4040 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
4043 gdk_threads_add_timeout (scroll_fast ?
4044 MENU_SCROLL_TIMEOUT2 :
4045 MENU_SCROLL_TIMEOUT1,
4046 gtk_menu_scroll_timeout, menu);
4048 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
4050 gtk_menu_stop_scrolling (menu);
4055 /* gtk_menu_start_scrolling() might have hit the top of the
4056 * menu, so check if the button isn't insensitive before
4057 * changing it to something else.
4059 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
4061 GtkStateType arrow_state = GTK_STATE_NORMAL;
4064 arrow_state = GTK_STATE_ACTIVE;
4065 else if (menu->upper_arrow_prelight)
4066 arrow_state = GTK_STATE_PRELIGHT;
4068 if (arrow_state != priv->upper_arrow_state)
4070 priv->upper_arrow_state = arrow_state;
4072 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4078 /* lower arrow handling */
4080 get_arrows_sensitive_area (menu, NULL, &rect);
4083 if (menu->lower_arrow_visible && !menu->tearoff_active &&
4084 (x >= rect.x) && (x < rect.x + rect.width) &&
4085 (y >= rect.y) && (y < rect.y + rect.height))
4090 if (touchscreen_mode)
4091 menu->lower_arrow_prelight = in_arrow;
4093 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4095 gboolean arrow_pressed = FALSE;
4097 if (menu->lower_arrow_visible && !menu->tearoff_active)
4099 if (touchscreen_mode)
4101 if (enter && menu->lower_arrow_prelight)
4103 if (menu->timeout_id == 0)
4105 /* Deselect the active item so that
4106 * any submenus are popped down
4108 gtk_menu_shell_deselect (menu_shell);
4110 gtk_menu_remove_scroll_timeout (menu);
4111 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4115 /* Only do stuff on click. */
4116 gtk_menu_start_scrolling (menu);
4117 arrow_pressed = TRUE;
4122 arrow_pressed = TRUE;
4127 gtk_menu_stop_scrolling (menu);
4130 else /* !touchscreen_mode */
4132 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4134 if (enter && in_arrow &&
4135 (!menu->lower_arrow_prelight ||
4136 menu->scroll_fast != scroll_fast))
4138 menu->lower_arrow_prelight = TRUE;
4139 menu->scroll_fast = scroll_fast;
4141 /* Deselect the active item so that
4142 * any submenus are popped down
4144 gtk_menu_shell_deselect (menu_shell);
4146 gtk_menu_remove_scroll_timeout (menu);
4147 menu->scroll_step = scroll_fast ?
4148 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
4151 gdk_threads_add_timeout (scroll_fast ?
4152 MENU_SCROLL_TIMEOUT2 :
4153 MENU_SCROLL_TIMEOUT1,
4154 gtk_menu_scroll_timeout, menu);
4156 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
4158 gtk_menu_stop_scrolling (menu);
4163 /* gtk_menu_start_scrolling() might have hit the bottom of the
4164 * menu, so check if the button isn't insensitive before
4165 * changing it to something else.
4167 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4169 GtkStateType arrow_state = GTK_STATE_NORMAL;
4172 arrow_state = GTK_STATE_ACTIVE;
4173 else if (menu->lower_arrow_prelight)
4174 arrow_state = GTK_STATE_PRELIGHT;
4176 if (arrow_state != priv->lower_arrow_state)
4178 priv->lower_arrow_state = arrow_state;
4180 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4188 gtk_menu_enter_notify (GtkWidget *widget,
4189 GdkEventCrossing *event)
4191 GtkWidget *menu_item;
4193 gboolean touchscreen_mode;
4195 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4196 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4197 event->mode == GDK_CROSSING_STATE_CHANGED)
4200 g_object_get (gtk_widget_get_settings (widget),
4201 "gtk-touchscreen-mode", &touchscreen_mode,
4204 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4205 if (GTK_IS_MENU (widget))
4207 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4209 if (!menu_shell->ignore_enter)
4210 gtk_menu_handle_scrolling (GTK_MENU (widget),
4211 event->x_root, event->y_root, TRUE, TRUE);
4214 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4216 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4218 if (GTK_IS_MENU (menu))
4220 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
4221 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4223 if (priv->seen_item_enter)
4225 /* This is the second enter we see for an item
4226 * on this menu. This means a release should always
4229 menu_shell->activate_time = 0;
4231 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4232 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4234 if (definitely_within_item (menu_item, event->x, event->y))
4236 /* This is an actual user-enter (ie. not a pop-under)
4237 * In this case, the user must either have entered
4238 * sufficiently far enough into the item, or he must move
4239 * far enough away from the enter point. (see
4240 * gtk_menu_motion_notify())
4242 menu_shell->activate_time = 0;
4246 priv->seen_item_enter = TRUE;
4250 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4251 * will not correspond to the event widget's parent. Check to see
4252 * if we are in the parent's navigation region.
4254 parent = gtk_widget_get_parent (menu_item);
4255 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4256 gtk_menu_navigating_submenu (GTK_MENU (parent),
4257 event->x_root, event->y_root))
4260 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4264 gtk_menu_leave_notify (GtkWidget *widget,
4265 GdkEventCrossing *event)
4267 GtkMenuShell *menu_shell;
4269 GtkMenuItem *menu_item;
4270 GtkWidget *event_widget;
4272 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4273 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4274 event->mode == GDK_CROSSING_STATE_CHANGED)
4277 menu = GTK_MENU (widget);
4278 menu_shell = GTK_MENU_SHELL (widget);
4280 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4283 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4285 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4287 if (!GTK_IS_MENU_ITEM (event_widget))
4290 menu_item = GTK_MENU_ITEM (event_widget);
4292 /* Here we check to see if we're leaving an active menu item with a submenu,
4293 * in which case we enter submenu navigation mode.
4295 if (menu_shell->active_menu_item != NULL
4296 && menu_item->submenu != NULL
4297 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
4299 if (GTK_MENU_SHELL (menu_item->submenu)->active)
4301 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4304 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
4306 /* We are leaving an active menu item with nonactive submenu.
4307 * Deselect it so we don't surprise the user with by popping
4308 * up a submenu _after_ he left the item.
4310 gtk_menu_shell_deselect (menu_shell);
4315 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4319 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4321 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4323 priv->navigation_x = 0;
4324 priv->navigation_y = 0;
4325 priv->navigation_width = 0;
4326 priv->navigation_height = 0;
4328 if (menu->navigation_timeout)
4330 g_source_remove (menu->navigation_timeout);
4331 menu->navigation_timeout = 0;
4335 /* When the timeout is elapsed, the navigation region is destroyed
4336 * and the menuitem under the pointer (if any) is selected.
4339 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4341 GtkMenuPopdownData *popdown_data = user_data;
4342 GtkMenu *menu = popdown_data->menu;
4343 GdkWindow *child_window;
4345 gtk_menu_stop_navigating_submenu (menu);
4347 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4349 child_window = gdk_window_get_device_position (menu->bin_window,
4350 popdown_data->device,
4355 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4357 send_event->crossing.window = g_object_ref (child_window);
4358 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4359 send_event->crossing.send_event = TRUE;
4360 gdk_event_set_device (send_event, popdown_data->device);
4362 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4364 gdk_event_free (send_event);
4372 gtk_menu_navigating_submenu (GtkMenu *menu,
4376 GtkMenuPrivate *priv;
4379 if (!gtk_menu_has_navigation_triangle (menu))
4382 priv = gtk_menu_get_private (menu);
4383 width = priv->navigation_width;
4384 height = priv->navigation_height;
4386 /* check if x/y are in the triangle spanned by the navigation parameters */
4388 /* 1) Move the coordinates so the triangle starts at 0,0 */
4389 event_x -= priv->navigation_x;
4390 event_y -= priv->navigation_y;
4392 /* 2) Ensure both legs move along the positive axis */
4404 /* 3) Check that the given coordinate is inside the triangle. The formula
4405 * is a transformed form of this formula: x/w + y/h <= 1
4407 if (event_x >= 0 && event_y >= 0 &&
4408 event_x * height + event_y * width <= width * height)
4414 gtk_menu_stop_navigating_submenu (menu);
4420 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4421 GtkMenuItem *menu_item,
4422 GdkEventCrossing *event)
4424 gint submenu_left = 0;
4425 gint submenu_right = 0;
4426 gint submenu_top = 0;
4427 gint submenu_bottom = 0;
4430 GtkWidget *event_widget;
4431 GtkMenuPopdownData *popdown_data;
4432 GtkMenuPrivate *priv;
4435 g_return_if_fail (menu_item->submenu != NULL);
4436 g_return_if_fail (event != NULL);
4438 priv = gtk_menu_get_private (menu);
4440 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4442 window = gtk_widget_get_window (menu_item->submenu);
4443 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4444 gdk_drawable_get_size (window, &width, &height);
4446 submenu_right = submenu_left + width;
4447 submenu_bottom = submenu_top + height;
4449 gdk_drawable_get_size (gtk_widget_get_window (event_widget),
4452 if (event->x >= 0 && event->x < width)
4456 gtk_menu_stop_navigating_submenu (menu);
4458 /* The navigation region is the triangle closest to the x/y
4459 * location of the rectangle. This is why the width or height
4463 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
4466 priv->navigation_x = submenu_left;
4467 priv->navigation_width = event->x_root - submenu_left;
4472 priv->navigation_x = submenu_right;
4473 priv->navigation_width = event->x_root - submenu_right;
4479 priv->navigation_y = event->y_root;
4480 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4482 if (priv->navigation_height >= 0)
4488 priv->navigation_y = event->y_root;
4489 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4491 if (priv->navigation_height <= 0)
4495 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4496 "gtk-menu-popdown-delay", &popdown_delay,
4499 popdown_data = g_new (GtkMenuPopdownData, 1);
4500 popdown_data->menu = menu;
4501 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4503 menu->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4505 gtk_menu_stop_navigating_submenu_cb,
4507 (GDestroyNotify) g_free);
4512 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4516 g_return_if_fail (GTK_IS_MENU (menu_shell));
4518 parent = menu_shell->parent_menu_shell;
4520 menu_shell->activate_time = 0;
4521 gtk_menu_popdown (GTK_MENU (menu_shell));
4524 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4528 gtk_menu_position (GtkMenu *menu)
4531 GtkRequisition requisition;
4532 GtkMenuPrivate *private;
4537 GdkScreen *pointer_screen;
4538 GdkRectangle monitor;
4541 g_return_if_fail (GTK_IS_MENU (menu));
4543 widget = GTK_WIDGET (menu);
4545 screen = gtk_widget_get_screen (widget);
4546 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4547 gdk_display_get_device_state (gdk_screen_get_display (screen),
4548 pointer, &pointer_screen, &x, &y, NULL);
4550 /* Get the minimum height for minimum width to figure out
4551 * the right place to popup the menu.
4553 gtk_size_request_get_size (GTK_SIZE_REQUEST (widget), &requisition, NULL);
4555 if (pointer_screen != screen)
4557 /* Pointer is on a different screen; roughly center the
4558 * menu on the screen. If someone was using multiscreen
4559 * + Xinerama together they'd probably want something
4560 * fancier; but that is likely to be vanishingly rare.
4562 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4563 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4566 private = gtk_menu_get_private (menu);
4567 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4569 private->initially_pushed_in = FALSE;
4571 /* Set the type hint here to allow custom position functions to set a different hint */
4572 if (!gtk_widget_get_visible (menu->toplevel))
4573 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4575 if (menu->position_func)
4577 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
4578 menu->position_func_data);
4580 if (private->monitor_num < 0)
4581 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4583 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4587 GtkStyle *style = gtk_widget_get_style (widget);
4588 gint space_left, space_right, space_above, space_below;
4591 gint xthickness = style->xthickness;
4592 gint ythickness = style->ythickness;
4593 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4595 /* The placement of popup menus horizontally works like this (with
4596 * RTL in parentheses)
4598 * - If there is enough room to the right (left) of the mouse cursor,
4599 * position the menu there.
4601 * - Otherwise, if if there is enough room to the left (right) of the
4602 * mouse cursor, position the menu there.
4604 * - Otherwise if the menu is smaller than the monitor, position it
4605 * on the side of the mouse cursor that has the most space available
4607 * - Otherwise (if there is simply not enough room for the menu on the
4608 * monitor), position it as far left (right) as possible.
4610 * Positioning in the vertical direction is similar: first try below
4611 * mouse cursor, then above.
4613 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4615 space_left = x - monitor.x;
4616 space_right = monitor.x + monitor.width - x - 1;
4617 space_above = y - monitor.y;
4618 space_below = monitor.y + monitor.height - y - 1;
4620 /* position horizontally */
4622 /* the amount of space we need to position the menu. Note the
4623 * menu is offset "xthickness" pixels
4625 needed_width = requisition.width - xthickness;
4627 if (needed_width <= space_left ||
4628 needed_width <= space_right)
4630 if ((rtl && needed_width <= space_left) ||
4631 (!rtl && needed_width > space_right))
4634 x = x + xthickness - requisition.width + 1;
4638 /* position right */
4642 /* x is clamped on-screen further down */
4644 else if (requisition.width <= monitor.width)
4646 /* the menu is too big to fit on either side of the mouse
4647 * cursor, but smaller than the monitor. Position it on
4648 * the side that has the most space
4650 if (space_left > space_right)
4658 x = monitor.x + monitor.width - requisition.width;
4661 else /* menu is simply too big for the monitor */
4666 x = monitor.x + monitor.width - requisition.width;
4675 /* Position vertically. The algorithm is the same as above, but
4676 * simpler because we don't have to take RTL into account.
4678 needed_height = requisition.height - ythickness;
4680 if (needed_height <= space_above ||
4681 needed_height <= space_below)
4683 if (needed_height <= space_below)
4686 y = y + ythickness - requisition.height + 1;
4688 y = CLAMP (y, monitor.y,
4689 monitor.y + monitor.height - requisition.height);
4691 else if (needed_height > space_below && needed_height > space_above)
4693 if (space_below >= space_above)
4694 y = monitor.y + monitor.height - requisition.height;
4706 if (private->initially_pushed_in)
4708 menu_height = requisition.height;
4710 if (y + menu_height > monitor.y + monitor.height)
4712 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4713 y = (monitor.y + monitor.height) - menu_height;
4718 scroll_offset += monitor.y - y;
4723 /* FIXME: should this be done in the various position_funcs ? */
4724 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4726 if (GTK_MENU_SHELL (menu)->active)
4728 private->have_position = TRUE;
4733 if (y + requisition.height > monitor.y + monitor.height)
4734 requisition.height = (monitor.y + monitor.height) - y;
4738 scroll_offset += monitor.y - y;
4739 requisition.height -= monitor.y - y;
4743 if (scroll_offset > 0)
4745 GtkBorder arrow_border;
4747 get_arrows_border (menu, &arrow_border);
4748 scroll_offset += arrow_border.top;
4751 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
4754 if (!GTK_MENU_SHELL (menu)->active)
4756 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4757 requisition.width, requisition.height);
4760 menu->scroll_offset = scroll_offset;
4764 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4766 if (menu->timeout_id)
4768 g_source_remove (menu->timeout_id);
4769 menu->timeout_id = 0;
4774 gtk_menu_stop_scrolling (GtkMenu *menu)
4776 gboolean touchscreen_mode;
4778 gtk_menu_remove_scroll_timeout (menu);
4780 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4781 "gtk-touchscreen-mode", &touchscreen_mode,
4784 if (!touchscreen_mode)
4786 menu->upper_arrow_prelight = FALSE;
4787 menu->lower_arrow_prelight = FALSE;
4792 gtk_menu_scroll_to (GtkMenu *menu,
4795 GtkMenuPrivate *priv;
4796 GtkAllocation allocation;
4797 GtkBorder arrow_border;
4801 gint view_width, view_height;
4804 guint vertical_padding;
4805 guint horizontal_padding;
4806 gboolean double_arrows;
4808 widget = GTK_WIDGET (menu);
4809 priv = gtk_menu_get_private (menu);
4811 if (menu->tearoff_active &&
4812 menu->tearoff_adjustment &&
4813 (menu->tearoff_adjustment->value != offset))
4815 menu->tearoff_adjustment->value =
4817 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4818 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4821 /* Move/resize the viewport according to arrows: */
4822 gtk_widget_get_allocation (widget, &allocation);
4823 view_width = allocation.width;
4824 view_height = allocation.height;
4826 style = gtk_widget_get_style (widget);
4828 gtk_widget_style_get (GTK_WIDGET (menu),
4829 "vertical-padding", &vertical_padding,
4830 "horizontal-padding", &horizontal_padding,
4833 double_arrows = get_double_arrows (menu);
4835 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4837 view_width -= (border_width + style->xthickness + horizontal_padding) * 2;
4838 view_height -= (border_width + style->ythickness + vertical_padding) * 2;
4839 menu_height = priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2;
4841 x = border_width + style->xthickness + horizontal_padding;
4842 y = border_width + style->ythickness + vertical_padding;
4844 if (double_arrows && !menu->tearoff_active)
4846 if (view_height < menu_height ||
4847 (offset > 0 && menu->scroll_offset > 0) ||
4848 (offset < 0 && menu->scroll_offset < 0))
4850 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4851 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4852 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4854 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4855 gtk_widget_queue_draw (GTK_WIDGET (menu));
4857 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4859 get_arrows_border (menu, &arrow_border);
4860 y += arrow_border.top;
4861 view_height -= arrow_border.top;
4862 view_height -= arrow_border.bottom;
4865 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4866 else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4867 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4868 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4870 if (offset >= menu_height - view_height)
4871 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4872 else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4873 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4874 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4876 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4877 (priv->lower_arrow_state != lower_arrow_previous_state))
4878 gtk_widget_queue_draw (GTK_WIDGET (menu));
4880 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4881 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4883 /* At the upper border, possibly remove timeout */
4884 if (menu->scroll_step < 0)
4886 gtk_menu_stop_scrolling (menu);
4887 gtk_widget_queue_draw (GTK_WIDGET (menu));
4891 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4892 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4894 /* At the lower border, possibly remove timeout */
4895 if (menu->scroll_step > 0)
4897 gtk_menu_stop_scrolling (menu);
4898 gtk_widget_queue_draw (GTK_WIDGET (menu));
4902 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4906 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4907 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4909 gtk_menu_stop_scrolling (menu);
4910 gtk_widget_queue_draw (GTK_WIDGET (menu));
4913 else if (!menu->tearoff_active)
4915 gboolean last_visible;
4917 last_visible = menu->upper_arrow_visible;
4918 menu->upper_arrow_visible = offset > 0;
4920 /* upper_arrow_visible may have changed, so requery the border */
4921 get_arrows_border (menu, &arrow_border);
4922 view_height -= arrow_border.top;
4924 if ((last_visible != menu->upper_arrow_visible) &&
4925 !menu->upper_arrow_visible)
4927 menu->upper_arrow_prelight = FALSE;
4929 /* If we hid the upper arrow, possibly remove timeout */
4930 if (menu->scroll_step < 0)
4932 gtk_menu_stop_scrolling (menu);
4933 gtk_widget_queue_draw (GTK_WIDGET (menu));
4937 last_visible = menu->lower_arrow_visible;
4938 menu->lower_arrow_visible = offset < menu_height - view_height;
4940 /* lower_arrow_visible may have changed, so requery the border */
4941 get_arrows_border (menu, &arrow_border);
4942 view_height -= arrow_border.bottom;
4944 if ((last_visible != menu->lower_arrow_visible) &&
4945 !menu->lower_arrow_visible)
4947 menu->lower_arrow_prelight = FALSE;
4949 /* If we hid the lower arrow, possibly remove timeout */
4950 if (menu->scroll_step > 0)
4952 gtk_menu_stop_scrolling (menu);
4953 gtk_widget_queue_draw (GTK_WIDGET (menu));
4957 y += arrow_border.top;
4960 /* Scroll the menu: */
4961 if (gtk_widget_get_realized (widget))
4962 gdk_window_move (menu->bin_window, 0, -offset);
4964 if (gtk_widget_get_realized (widget))
4965 gdk_window_move_resize (menu->view_window,
4971 menu->scroll_offset = offset;
4975 compute_child_offset (GtkMenu *menu,
4976 GtkWidget *menu_item,
4979 gboolean *is_last_child)
4981 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4982 gint item_top_attach;
4983 gint item_bottom_attach;
4984 gint child_offset = 0;
4987 get_effective_child_attach (menu_item, NULL, NULL,
4988 &item_top_attach, &item_bottom_attach);
4990 /* there is a possibility that we get called before _size_request, so
4991 * check the height table for safety.
4993 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4996 /* when we have a row with only invisible children, it's height will
4997 * be zero, so there's no need to check WIDGET_VISIBLE here
4999 for (i = 0; i < item_top_attach; i++)
5000 child_offset += priv->heights[i];
5003 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
5005 *offset = child_offset;
5007 *height = priv->heights[item_top_attach];
5013 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
5014 GtkWidget *menu_item)
5016 GtkMenu *menu = GTK_MENU (menu_shell);
5017 GtkWidget *widget = GTK_WIDGET (menu_shell);
5018 gint child_offset, child_height;
5022 gboolean last_child = 0;
5024 /* We need to check if the selected item fully visible.
5025 * If not we need to scroll the menu so that it becomes fully
5029 if (compute_child_offset (menu, menu_item,
5030 &child_offset, &child_height, &last_child))
5032 guint vertical_padding;
5033 gboolean double_arrows;
5035 y = menu->scroll_offset;
5036 gdk_drawable_get_size (gtk_widget_get_window (widget),
5039 gtk_widget_style_get (widget,
5040 "vertical-padding", &vertical_padding,
5043 double_arrows = get_double_arrows (menu);
5045 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5046 2 * gtk_widget_get_style (widget)->ythickness +
5047 2 * vertical_padding;
5048 if (child_offset < y)
5050 /* Ignore the enter event we might get if the pointer is on the menu
5052 menu_shell->ignore_enter = TRUE;
5053 gtk_menu_scroll_to (menu, child_offset);
5057 GtkBorder arrow_border;
5061 get_arrows_border (menu, &arrow_border);
5062 if (!menu->tearoff_active)
5063 arrow_height = arrow_border.top + arrow_border.bottom;
5065 if (child_offset + child_height > y + height - arrow_height)
5068 if ((!last_child && !menu->tearoff_active) || double_arrows)
5069 arrow_height += arrow_border.bottom;
5071 y = child_offset + child_height - height + arrow_height;
5072 if (((y > 0) && !menu->tearoff_active) || double_arrows)
5074 /* Need upper arrow */
5075 arrow_height += arrow_border.top;
5076 y = child_offset + child_height - height + arrow_height;
5078 /* Ignore the enter event we might get if the pointer is on the menu
5080 menu_shell->ignore_enter = TRUE;
5081 gtk_menu_scroll_to (menu, y);
5089 gtk_menu_select_item (GtkMenuShell *menu_shell,
5090 GtkWidget *menu_item)
5092 GtkMenu *menu = GTK_MENU (menu_shell);
5094 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5095 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5097 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5101 /* Reparent the menu, taking care of the refcounting
5103 * If unrealize is true we force a unrealize while reparenting the parent.
5104 * This can help eliminate flicker in some cases.
5106 * What happens is that when the menu is unrealized and then re-realized,
5107 * the allocations are as follows:
5109 * parent - 1x1 at (0,0)
5110 * child1 - 100x20 at (0,0)
5111 * child2 - 100x20 at (0,20)
5112 * child3 - 100x20 at (0,40)
5114 * That is, the parent is small but the children are full sized. Then,
5115 * when the queued_resize gets processed, the parent gets resized to
5118 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5119 * contains the following logic:
5121 * - if a move or resize operation on a window would change the clip
5122 * region on the children, then before the window is resized
5123 * the background for children is temporarily set to None, the
5124 * move/resize done, and the background for the children restored.
5126 * So, at the point where the parent is resized to final size, the
5127 * background for the children is temporarily None, and thus they
5128 * are not cleared to the background color and the previous background
5129 * (the image of the menu) is left in place.
5132 gtk_menu_reparent (GtkMenu *menu,
5133 GtkWidget *new_parent,
5136 GtkObject *object = GTK_OBJECT (menu);
5137 GtkWidget *widget = GTK_WIDGET (menu);
5138 gboolean was_floating = g_object_is_floating (object);
5140 g_object_ref_sink (object);
5144 g_object_ref (object);
5145 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5146 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5147 g_object_unref (object);
5150 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
5153 g_object_force_floating (G_OBJECT (object));
5155 g_object_unref (object);
5159 gtk_menu_show_all (GtkWidget *widget)
5161 /* Show children, but not self. */
5162 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5167 gtk_menu_hide_all (GtkWidget *widget)
5169 /* Hide children, but not self. */
5170 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
5174 * gtk_menu_set_screen:
5175 * @menu: a #GtkMenu.
5176 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5177 * determined by the widget the menu is attached to.
5179 * Sets the #GdkScreen on which the menu will be displayed.
5184 gtk_menu_set_screen (GtkMenu *menu,
5187 g_return_if_fail (GTK_IS_MENU (menu));
5188 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
5190 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5194 menu_change_screen (menu, screen);
5198 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5200 attach_widget_screen_changed (attach_widget, NULL, menu);
5206 * @menu: a #GtkMenu.
5207 * @child: a #GtkMenuItem.
5208 * @left_attach: The column number to attach the left side of the item to.
5209 * @right_attach: The column number to attach the right side of the item to.
5210 * @top_attach: The row number to attach the top of the item to.
5211 * @bottom_attach: The row number to attach the bottom of the item to.
5213 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5214 * an item will occupy is specified by @left_attach, @right_attach,
5215 * @top_attach and @bottom_attach. These each represent the leftmost,
5216 * rightmost, uppermost and lower column and row numbers of the table.
5217 * (Columns and rows are indexed from zero).
5219 * Note that this function is not related to gtk_menu_detach().
5224 gtk_menu_attach (GtkMenu *menu,
5229 guint bottom_attach)
5231 GtkMenuShell *menu_shell;
5234 g_return_if_fail (GTK_IS_MENU (menu));
5235 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5236 parent = gtk_widget_get_parent (child);
5237 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5238 g_return_if_fail (left_attach < right_attach);
5239 g_return_if_fail (top_attach < bottom_attach);
5241 menu_shell = GTK_MENU_SHELL (menu);
5245 AttachInfo *ai = get_attach_info (child);
5247 ai->left_attach = left_attach;
5248 ai->right_attach = right_attach;
5249 ai->top_attach = top_attach;
5250 ai->bottom_attach = bottom_attach;
5252 menu_shell->children = g_list_append (menu_shell->children, child);
5254 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5256 menu_queue_resize (menu);
5260 gtk_container_child_set (GTK_CONTAINER (parent), child,
5261 "left-attach", left_attach,
5262 "right-attach", right_attach,
5263 "top-attach", top_attach,
5264 "bottom-attach", bottom_attach,
5270 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5274 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5275 "gtk-menu-popup-delay", &popup_delay,
5282 find_child_containing (GtkMenuShell *menu_shell,
5290 /* find a child which includes the area given by
5291 * left, right, top, bottom.
5294 for (list = menu_shell->children; list; list = list->next)
5298 if (!_gtk_menu_item_is_selectable (list->data))
5301 get_effective_child_attach (list->data, &l, &r, &t, &b);
5303 if (l <= left && right <= r
5304 && t <= top && bottom <= b)
5305 return GTK_WIDGET (list->data);
5312 gtk_menu_move_current (GtkMenuShell *menu_shell,
5313 GtkMenuDirectionType direction)
5315 GtkMenu *menu = GTK_MENU (menu_shell);
5318 GtkWidget *match = NULL;
5320 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5324 case GTK_MENU_DIR_CHILD:
5325 direction = GTK_MENU_DIR_PARENT;
5327 case GTK_MENU_DIR_PARENT:
5328 direction = GTK_MENU_DIR_CHILD;
5334 /* use special table menu key bindings */
5335 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5337 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
5339 if (direction == GTK_MENU_DIR_NEXT)
5341 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5343 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5351 for (i = 0; i < t; i++)
5353 match = find_child_containing (menu_shell,
5354 l, l + 1, i, i + 1);
5360 else if (direction == GTK_MENU_DIR_PREV)
5362 for (i = t; i > 0; i--)
5364 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
5372 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5374 match = find_child_containing (menu_shell,
5375 l, l + 1, i - 1, i);
5381 else if (direction == GTK_MENU_DIR_PARENT)
5383 /* we go one left if possible */
5385 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
5389 GtkWidget *parent = menu_shell->parent_menu_shell;
5392 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
5393 match = menu_shell->active_menu_item;
5396 else if (direction == GTK_MENU_DIR_CHILD)
5398 /* we go one right if possible */
5399 if (r < gtk_menu_get_n_columns (menu))
5400 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5404 GtkWidget *parent = menu_shell->parent_menu_shell;
5406 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
5408 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
5409 match = menu_shell->active_menu_item;
5415 gtk_menu_shell_select_item (menu_shell, match);
5420 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5424 get_visible_size (GtkMenu *menu)
5426 GtkAllocation allocation;
5427 GtkWidget *widget = GTK_WIDGET (menu);
5428 GtkContainer *container = GTK_CONTAINER (menu);
5431 gtk_widget_get_allocation (widget, &allocation);
5432 menu_height = (allocation.height
5433 - 2 * (gtk_container_get_border_width (container)
5434 + gtk_widget_get_style (widget)->ythickness));
5436 if (!menu->tearoff_active)
5438 GtkBorder arrow_border;
5440 get_arrows_border (menu, &arrow_border);
5441 menu_height -= arrow_border.top;
5442 menu_height -= arrow_border.bottom;
5448 /* Find the sensitive on-screen child containing @y, or if none,
5449 * the nearest selectable onscreen child. (%NULL if none)
5452 child_at (GtkMenu *menu,
5455 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5456 GtkWidget *child = NULL;
5457 gint child_offset = 0;
5460 gint lower, upper; /* Onscreen bounds */
5462 menu_height = get_visible_size (menu);
5463 lower = menu->scroll_offset;
5464 upper = menu->scroll_offset + menu_height;
5466 for (children = menu_shell->children; children; children = children->next)
5468 if (gtk_widget_get_visible (children->data))
5470 GtkRequisition child_requisition;
5472 gtk_size_request_get_size (GTK_SIZE_REQUEST (children->data),
5473 &child_requisition, NULL);
5475 if (_gtk_menu_item_is_selectable (children->data) &&
5476 child_offset >= lower &&
5477 child_offset + child_requisition.height <= upper)
5479 child = children->data;
5481 if (child_offset + child_requisition.height > y &&
5482 !GTK_IS_TEAROFF_MENU_ITEM (child))
5486 child_offset += child_requisition.height;
5494 get_menu_height (GtkMenu *menu)
5496 GtkAllocation allocation;
5497 GtkWidget *widget = GTK_WIDGET (menu);
5500 gtk_widget_get_allocation (widget, &allocation);
5502 height = allocation.height;
5503 height -= gtk_container_get_border_width (GTK_CONTAINER (widget) + gtk_widget_get_style (widget)->ythickness) * 2;
5505 if (!menu->tearoff_active)
5507 GtkBorder arrow_border;
5509 get_arrows_border (menu, &arrow_border);
5510 height -= arrow_border.top;
5511 height -= arrow_border.bottom;
5518 gtk_menu_real_move_scroll (GtkMenu *menu,
5521 gint page_size = get_visible_size (menu);
5522 gint end_position = get_menu_height (menu);
5523 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5527 case GTK_SCROLL_PAGE_UP:
5528 case GTK_SCROLL_PAGE_DOWN:
5532 gint child_offset = 0;
5533 gboolean old_upper_arrow_visible;
5536 if (type == GTK_SCROLL_PAGE_UP)
5541 if (menu_shell->active_menu_item)
5545 compute_child_offset (menu, menu_shell->active_menu_item,
5546 &child_offset, &child_height, NULL);
5547 child_offset += child_height / 2;
5550 menu_shell->ignore_enter = TRUE;
5551 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5552 old_offset = menu->scroll_offset;
5554 new_offset = menu->scroll_offset + step;
5555 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5557 gtk_menu_scroll_to (menu, new_offset);
5559 if (menu_shell->active_menu_item)
5561 GtkWidget *new_child;
5562 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5563 GtkBorder arrow_border;
5565 get_arrows_border (menu, &arrow_border);
5567 if (menu->scroll_offset != old_offset)
5568 step = menu->scroll_offset - old_offset;
5570 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5572 new_child = child_at (menu, child_offset + step);
5574 gtk_menu_shell_select_item (menu_shell, new_child);
5578 case GTK_SCROLL_START:
5579 /* Ignore the enter event we might get if the pointer is on the menu
5581 menu_shell->ignore_enter = TRUE;
5582 gtk_menu_scroll_to (menu, 0);
5583 gtk_menu_shell_select_first (menu_shell, TRUE);
5585 case GTK_SCROLL_END:
5586 /* Ignore the enter event we might get if the pointer is on the menu
5588 menu_shell->ignore_enter = TRUE;
5589 gtk_menu_scroll_to (menu, end_position - page_size);
5590 _gtk_menu_shell_select_last (menu_shell, TRUE);
5599 * gtk_menu_set_monitor:
5601 * @monitor_num: the number of the monitor on which the menu should
5604 * Informs GTK+ on which monitor a menu should be popped up.
5605 * See gdk_screen_get_monitor_geometry().
5607 * This function should be called from a #GtkMenuPositionFunc if the
5608 * menu should not appear on the same monitor as the pointer. This
5609 * information can't be reliably inferred from the coordinates returned
5610 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
5611 * may extend beyond the monitor boundaries or even the screen boundaries.
5616 gtk_menu_set_monitor (GtkMenu *menu,
5619 GtkMenuPrivate *priv;
5620 g_return_if_fail (GTK_IS_MENU (menu));
5622 priv = gtk_menu_get_private (menu);
5624 priv->monitor_num = monitor_num;
5628 * gtk_menu_get_monitor:
5631 * Retrieves the number of the monitor on which to show the menu.
5633 * Returns: the number of the monitor on which the menu should
5634 * be popped up or -1, if no monitor has been set
5639 gtk_menu_get_monitor (GtkMenu *menu)
5641 GtkMenuPrivate *priv;
5642 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5644 priv = gtk_menu_get_private (menu);
5646 return priv->monitor_num;
5650 * gtk_menu_get_for_attach_widget:
5651 * @widget: a #GtkWidget
5653 * Returns a list of the menus which are attached to this widget.
5654 * This list is owned by GTK+ and must not be modified.
5656 * Return value: (element-type GtkWidget) (transfer none): the list of menus attached to his widget.
5661 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5665 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5667 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5672 gtk_menu_grab_notify (GtkWidget *widget,
5673 gboolean was_grabbed)
5675 GtkWidget *toplevel;
5676 GtkWindowGroup *group;
5680 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5683 !gtk_widget_device_is_shadowed (widget, pointer))
5686 toplevel = gtk_widget_get_toplevel (widget);
5688 if (!GTK_IS_WINDOW (toplevel))
5691 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5692 grab = gtk_window_group_get_current_device_grab (group, pointer);
5694 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
5695 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5699 * gtk_menu_set_reserve_toggle_size:
5701 * @reserve_toggle_size: whether to reserve size for toggles
5703 * Sets whether the menu should reserve space for drawing toggles
5704 * or icons, regardless of their actual presence.
5709 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5710 gboolean reserve_toggle_size)
5712 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5713 gboolean no_toggle_size;
5715 no_toggle_size = !reserve_toggle_size;
5717 if (priv->no_toggle_size != no_toggle_size)
5719 priv->no_toggle_size = no_toggle_size;
5721 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5726 * gtk_menu_get_reserve_toggle_size:
5729 * Returns whether the menu reserves space for toggles and
5730 * icons, regardless of their actual presence.
5732 * Returns: Whether the menu reserves toggle space
5737 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5739 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5741 return !priv->no_toggle_size;