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 "gtkprivate.h"
47 #define DEFAULT_POPUP_DELAY 225
48 #define DEFAULT_POPDOWN_DELAY 1000
50 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
51 * extends below the submenu
54 #define MENU_SCROLL_STEP1 8
55 #define MENU_SCROLL_STEP2 15
56 #define MENU_SCROLL_FAST_ZONE 8
57 #define MENU_SCROLL_TIMEOUT1 50
58 #define MENU_SCROLL_TIMEOUT2 20
60 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
61 #define ATTACHED_MENUS "gtk-attached-menus"
63 typedef struct _GtkMenuAttachData GtkMenuAttachData;
64 typedef struct _GtkMenuPrivate GtkMenuPrivate;
65 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
67 struct _GtkMenuAttachData
69 GtkWidget *attach_widget;
70 GtkMenuDetachFunc detacher;
73 struct _GtkMenuPrivate
77 gboolean initially_pushed_in;
79 GDestroyNotify position_func_data_destroy;
81 /* info used for the table */
84 gint requested_height;
88 /* Cached layout information */
97 GtkStateType lower_arrow_state;
98 GtkStateType upper_arrow_state;
100 /* navigation region */
103 int navigation_width;
104 int navigation_height;
106 guint have_layout : 1;
107 guint seen_item_enter : 1;
108 guint have_position : 1;
109 guint ignore_button_release : 1;
110 guint no_toggle_size : 1;
113 struct _GtkMenuPopdownData
125 gint effective_left_attach;
126 gint effective_right_attach;
127 gint effective_top_attach;
128 gint effective_bottom_attach;
145 PROP_RESERVE_TOGGLE_SIZE
150 CHILD_PROP_LEFT_ATTACH,
151 CHILD_PROP_RIGHT_ATTACH,
152 CHILD_PROP_TOP_ATTACH,
153 CHILD_PROP_BOTTOM_ATTACH
156 static void gtk_menu_set_property (GObject *object,
160 static void gtk_menu_get_property (GObject *object,
164 static void gtk_menu_set_child_property(GtkContainer *container,
169 static void gtk_menu_get_child_property(GtkContainer *container,
174 static void gtk_menu_destroy (GtkWidget *widget);
175 static void gtk_menu_realize (GtkWidget *widget);
176 static void gtk_menu_unrealize (GtkWidget *widget);
177 static void gtk_menu_size_allocate (GtkWidget *widget,
178 GtkAllocation *allocation);
179 static void gtk_menu_show (GtkWidget *widget);
180 static gboolean gtk_menu_draw (GtkWidget *widget,
182 static gboolean gtk_menu_key_press (GtkWidget *widget,
184 static gboolean gtk_menu_scroll (GtkWidget *widget,
185 GdkEventScroll *event);
186 static gboolean gtk_menu_button_press (GtkWidget *widget,
187 GdkEventButton *event);
188 static gboolean gtk_menu_button_release (GtkWidget *widget,
189 GdkEventButton *event);
190 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
191 GdkEventMotion *event);
192 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
193 GdkEventCrossing *event);
194 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
195 GdkEventCrossing *event);
196 static void gtk_menu_scroll_to (GtkMenu *menu,
198 static void gtk_menu_grab_notify (GtkWidget *widget,
199 gboolean was_grabbed);
201 static void gtk_menu_stop_scrolling (GtkMenu *menu);
202 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
203 static gboolean gtk_menu_scroll_timeout (gpointer data);
204 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
205 static void gtk_menu_start_scrolling (GtkMenu *menu);
207 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
208 GtkWidget *menu_item);
209 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
210 GtkWidget *menu_item);
211 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
214 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
216 static void gtk_menu_handle_scrolling (GtkMenu *menu,
221 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
223 static void gtk_menu_style_set (GtkWidget *widget,
224 GtkStyle *previous_style);
225 static gboolean gtk_menu_focus (GtkWidget *widget,
226 GtkDirectionType direction);
227 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
228 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
229 GtkMenuDirectionType direction);
230 static void gtk_menu_real_move_scroll (GtkMenu *menu,
233 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
234 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
235 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
238 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
239 GtkMenuItem *menu_item,
240 GdkEventCrossing *event);
242 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
243 static void gtk_menu_show_all (GtkWidget *widget);
244 static void gtk_menu_position (GtkMenu *menu);
245 static void gtk_menu_reparent (GtkMenu *menu,
246 GtkWidget *new_parent,
248 static void gtk_menu_remove (GtkContainer *menu,
251 static void gtk_menu_update_title (GtkMenu *menu);
253 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
254 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
256 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
258 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
259 gboolean group_changed);
261 static void gtk_menu_get_preferred_width (GtkWidget *widget,
264 static void gtk_menu_get_preferred_height (GtkWidget *widget,
267 static void gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
273 static const gchar attach_data_key[] = "gtk-menu-attach-data";
275 static guint menu_signals[LAST_SIGNAL] = { 0 };
277 static GtkMenuPrivate *
278 gtk_menu_get_private (GtkMenu *menu)
280 return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
283 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
286 menu_queue_resize (GtkMenu *menu)
288 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
290 priv->have_layout = FALSE;
291 gtk_widget_queue_resize (GTK_WIDGET (menu));
295 attach_info_free (AttachInfo *info)
297 g_slice_free (AttachInfo, info);
301 get_attach_info (GtkWidget *child)
303 GObject *object = G_OBJECT (child);
304 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
308 ai = g_slice_new0 (AttachInfo);
309 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
310 (GDestroyNotify) attach_info_free);
317 is_grid_attached (AttachInfo *ai)
319 return (ai->left_attach >= 0 &&
320 ai->right_attach >= 0 &&
321 ai->top_attach >= 0 &&
322 ai->bottom_attach >= 0);
326 menu_ensure_layout (GtkMenu *menu)
328 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
330 if (!priv->have_layout)
332 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
336 gint max_right_attach;
337 gint max_bottom_attach;
339 /* Find extents of gridded portion
341 max_right_attach = 1;
342 max_bottom_attach = 0;
344 for (l = menu_shell->children; l; l = l->next)
346 GtkWidget *child = l->data;
347 AttachInfo *ai = get_attach_info (child);
349 if (is_grid_attached (ai))
351 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
352 max_right_attach = MAX (max_right_attach, ai->right_attach);
358 row_occupied = g_malloc0 (max_bottom_attach);
360 for (l = menu_shell->children; l; l = l->next)
362 GtkWidget *child = l->data;
363 AttachInfo *ai = get_attach_info (child);
365 if (is_grid_attached (ai))
369 for (i = ai->top_attach; i < ai->bottom_attach; i++)
370 row_occupied[i] = TRUE;
374 /* Lay non-grid-items out in those rows
377 for (l = menu_shell->children; l; l = l->next)
379 GtkWidget *child = l->data;
380 AttachInfo *ai = get_attach_info (child);
382 if (!is_grid_attached (ai))
384 while (current_row < max_bottom_attach && row_occupied[current_row])
387 ai->effective_left_attach = 0;
388 ai->effective_right_attach = max_right_attach;
389 ai->effective_top_attach = current_row;
390 ai->effective_bottom_attach = current_row + 1;
396 ai->effective_left_attach = ai->left_attach;
397 ai->effective_right_attach = ai->right_attach;
398 ai->effective_top_attach = ai->top_attach;
399 ai->effective_bottom_attach = ai->bottom_attach;
403 g_free (row_occupied);
405 priv->n_rows = MAX (current_row, max_bottom_attach);
406 priv->n_columns = max_right_attach;
407 priv->have_layout = TRUE;
413 gtk_menu_get_n_columns (GtkMenu *menu)
415 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
417 menu_ensure_layout (menu);
419 return priv->n_columns;
423 gtk_menu_get_n_rows (GtkMenu *menu)
425 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
427 menu_ensure_layout (menu);
433 get_effective_child_attach (GtkWidget *child,
439 GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
442 menu_ensure_layout (menu);
444 ai = get_attach_info (child);
447 *l = ai->effective_left_attach;
449 *r = ai->effective_right_attach;
451 *t = ai->effective_top_attach;
453 *b = ai->effective_bottom_attach;
458 gtk_menu_class_init (GtkMenuClass *class)
460 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
461 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
462 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
463 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
464 GtkBindingSet *binding_set;
466 gobject_class->set_property = gtk_menu_set_property;
467 gobject_class->get_property = gtk_menu_get_property;
469 widget_class->destroy = gtk_menu_destroy;
470 widget_class->realize = gtk_menu_realize;
471 widget_class->unrealize = gtk_menu_unrealize;
472 widget_class->size_allocate = gtk_menu_size_allocate;
473 widget_class->show = gtk_menu_show;
474 widget_class->draw = gtk_menu_draw;
475 widget_class->scroll_event = gtk_menu_scroll;
476 widget_class->key_press_event = gtk_menu_key_press;
477 widget_class->button_press_event = gtk_menu_button_press;
478 widget_class->button_release_event = gtk_menu_button_release;
479 widget_class->motion_notify_event = gtk_menu_motion_notify;
480 widget_class->show_all = gtk_menu_show_all;
481 widget_class->enter_notify_event = gtk_menu_enter_notify;
482 widget_class->leave_notify_event = gtk_menu_leave_notify;
483 widget_class->style_set = gtk_menu_style_set;
484 widget_class->focus = gtk_menu_focus;
485 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
486 widget_class->grab_notify = gtk_menu_grab_notify;
487 widget_class->get_preferred_width = gtk_menu_get_preferred_width;
488 widget_class->get_preferred_height = gtk_menu_get_preferred_height;
489 widget_class->get_preferred_height_for_width = gtk_menu_get_preferred_height_for_width;
491 container_class->remove = gtk_menu_remove;
492 container_class->get_child_property = gtk_menu_get_child_property;
493 container_class->set_child_property = gtk_menu_set_child_property;
495 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
496 menu_shell_class->deactivate = gtk_menu_deactivate;
497 menu_shell_class->select_item = gtk_menu_select_item;
498 menu_shell_class->insert = gtk_menu_real_insert;
499 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
500 menu_shell_class->move_current = gtk_menu_move_current;
502 menu_signals[MOVE_SCROLL] =
503 g_signal_new_class_handler (I_("move-scroll"),
504 G_OBJECT_CLASS_TYPE (gobject_class),
505 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
506 G_CALLBACK (gtk_menu_real_move_scroll),
508 _gtk_marshal_VOID__ENUM,
510 GTK_TYPE_SCROLL_TYPE);
515 * The index of the currently selected menu item, or -1 if no
516 * menu item is selected.
520 g_object_class_install_property (gobject_class,
522 g_param_spec_int ("active",
524 P_("The currently selected menu item"),
526 GTK_PARAM_READWRITE));
529 * GtkMenu:accel-group:
531 * The accel group holding accelerators for the menu.
535 g_object_class_install_property (gobject_class,
537 g_param_spec_object ("accel-group",
539 P_("The accel group holding accelerators for the menu"),
540 GTK_TYPE_ACCEL_GROUP,
541 GTK_PARAM_READWRITE));
544 * GtkMenu:accel-path:
546 * An accel path used to conveniently construct accel paths of child items.
550 g_object_class_install_property (gobject_class,
552 g_param_spec_string ("accel-path",
554 P_("An accel path used to conveniently construct accel paths of child items"),
556 GTK_PARAM_READWRITE));
559 * GtkMenu:attach-widget:
561 * The widget the menu is attached to. Setting this property attaches
562 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
563 * use gtk_menu_attach_to_widget() directly.
567 g_object_class_install_property (gobject_class,
569 g_param_spec_object ("attach-widget",
571 P_("The widget the menu is attached to"),
573 GTK_PARAM_READWRITE));
575 g_object_class_install_property (gobject_class,
577 g_param_spec_string ("tearoff-title",
579 P_("A title that may be displayed by the window manager when this menu is torn-off"),
581 GTK_PARAM_READWRITE));
584 * GtkMenu:tearoff-state:
586 * A boolean that indicates whether the menu is torn-off.
590 g_object_class_install_property (gobject_class,
592 g_param_spec_boolean ("tearoff-state",
594 P_("A boolean that indicates whether the menu is torn-off"),
596 GTK_PARAM_READWRITE));
601 * The monitor the menu will be popped up on.
605 g_object_class_install_property (gobject_class,
607 g_param_spec_int ("monitor",
609 P_("The monitor the menu will be popped up on"),
611 GTK_PARAM_READWRITE));
613 gtk_widget_class_install_style_property (widget_class,
614 g_param_spec_int ("vertical-padding",
615 P_("Vertical Padding"),
616 P_("Extra space at the top and bottom of the menu"),
620 GTK_PARAM_READABLE));
623 * GtkMenu:reserve-toggle-size:
625 * A boolean that indicates whether the menu reserves space for
626 * toggles and icons, regardless of their actual presence.
628 * This property should only be changed from its default value
629 * for special-purposes such as tabular menus. Regular menus that
630 * are connected to a menu bar or context menus should reserve
631 * toggle space for consistency.
635 g_object_class_install_property (gobject_class,
636 PROP_RESERVE_TOGGLE_SIZE,
637 g_param_spec_boolean ("reserve-toggle-size",
638 P_("Reserve Toggle Size"),
639 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
641 GTK_PARAM_READWRITE));
643 gtk_widget_class_install_style_property (widget_class,
644 g_param_spec_int ("horizontal-padding",
645 P_("Horizontal Padding"),
646 P_("Extra space at the left and right edges of the menu"),
650 GTK_PARAM_READABLE));
652 gtk_widget_class_install_style_property (widget_class,
653 g_param_spec_int ("vertical-offset",
654 P_("Vertical Offset"),
655 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
659 GTK_PARAM_READABLE));
661 gtk_widget_class_install_style_property (widget_class,
662 g_param_spec_int ("horizontal-offset",
663 P_("Horizontal Offset"),
664 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
668 GTK_PARAM_READABLE));
670 gtk_widget_class_install_style_property (widget_class,
671 g_param_spec_boolean ("double-arrows",
673 P_("When scrolling, always show both arrows."),
675 GTK_PARAM_READABLE));
678 * GtkMenu:arrow-placement:
680 * Indicates where scroll arrows should be placed.
684 gtk_widget_class_install_style_property (widget_class,
685 g_param_spec_enum ("arrow-placement",
686 P_("Arrow Placement"),
687 P_("Indicates where scroll arrows should be placed"),
688 GTK_TYPE_ARROW_PLACEMENT,
690 GTK_PARAM_READABLE));
692 gtk_container_class_install_child_property (container_class,
693 CHILD_PROP_LEFT_ATTACH,
694 g_param_spec_int ("left-attach",
696 P_("The column number to attach the left side of the child to"),
698 GTK_PARAM_READWRITE));
700 gtk_container_class_install_child_property (container_class,
701 CHILD_PROP_RIGHT_ATTACH,
702 g_param_spec_int ("right-attach",
704 P_("The column number to attach the right side of the child to"),
706 GTK_PARAM_READWRITE));
708 gtk_container_class_install_child_property (container_class,
709 CHILD_PROP_TOP_ATTACH,
710 g_param_spec_int ("top-attach",
712 P_("The row number to attach the top of the child to"),
714 GTK_PARAM_READWRITE));
716 gtk_container_class_install_child_property (container_class,
717 CHILD_PROP_BOTTOM_ATTACH,
718 g_param_spec_int ("bottom-attach",
720 P_("The row number to attach the bottom of the child to"),
722 GTK_PARAM_READWRITE));
725 * GtkMenu::arrow-scaling
727 * Arbitrary constant to scale down the size of the scroll arrow.
731 gtk_widget_class_install_style_property (widget_class,
732 g_param_spec_float ("arrow-scaling",
734 P_("Arbitrary constant to scale down the size of the scroll arrow"),
736 GTK_PARAM_READABLE));
738 binding_set = gtk_binding_set_by_class (class);
739 gtk_binding_entry_add_signal (binding_set,
741 I_("move-current"), 1,
742 GTK_TYPE_MENU_DIRECTION_TYPE,
744 gtk_binding_entry_add_signal (binding_set,
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,
763 GTK_MENU_DIR_PARENT);
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,
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_SCROLL_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,
805 GDK_KEY_KP_Page_Up, 0,
807 GTK_TYPE_SCROLL_TYPE,
809 gtk_binding_entry_add_signal (binding_set,
810 GDK_KEY_Page_Down, 0,
812 GTK_TYPE_SCROLL_TYPE,
813 GTK_SCROLL_PAGE_DOWN);
814 gtk_binding_entry_add_signal (binding_set,
815 GDK_KEY_KP_Page_Down, 0,
817 GTK_TYPE_SCROLL_TYPE,
818 GTK_SCROLL_PAGE_DOWN);
820 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
821 P_("Can change accelerators"),
822 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
824 GTK_PARAM_READWRITE));
826 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
827 P_("Delay before submenus appear"),
828 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
832 GTK_PARAM_READWRITE));
834 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
835 P_("Delay before hiding a submenu"),
836 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
839 DEFAULT_POPDOWN_DELAY,
840 GTK_PARAM_READWRITE));
842 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
847 gtk_menu_set_property (GObject *object,
852 GtkMenu *menu = GTK_MENU (object);
857 gtk_menu_set_active (menu, g_value_get_int (value));
859 case PROP_ACCEL_GROUP:
860 gtk_menu_set_accel_group (menu, g_value_get_object (value));
862 case PROP_ACCEL_PATH:
863 gtk_menu_set_accel_path (menu, g_value_get_string (value));
865 case PROP_ATTACH_WIDGET:
869 widget = gtk_menu_get_attach_widget (menu);
871 gtk_menu_detach (menu);
873 widget = (GtkWidget*) g_value_get_object (value);
875 gtk_menu_attach_to_widget (menu, widget, NULL);
878 case PROP_TEAROFF_STATE:
879 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
881 case PROP_TEAROFF_TITLE:
882 gtk_menu_set_title (menu, g_value_get_string (value));
885 gtk_menu_set_monitor (menu, g_value_get_int (value));
887 case PROP_RESERVE_TOGGLE_SIZE:
888 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
891 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
897 gtk_menu_get_property (GObject *object,
902 GtkMenu *menu = GTK_MENU (object);
907 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->children, gtk_menu_get_active (menu)));
909 case PROP_ACCEL_GROUP:
910 g_value_set_object (value, gtk_menu_get_accel_group (menu));
912 case PROP_ACCEL_PATH:
913 g_value_set_string (value, gtk_menu_get_accel_path (menu));
915 case PROP_ATTACH_WIDGET:
916 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
918 case PROP_TEAROFF_STATE:
919 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
921 case PROP_TEAROFF_TITLE:
922 g_value_set_string (value, gtk_menu_get_title (menu));
925 g_value_set_int (value, gtk_menu_get_monitor (menu));
927 case PROP_RESERVE_TOGGLE_SIZE:
928 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
931 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
937 gtk_menu_set_child_property (GtkContainer *container,
943 GtkMenu *menu = GTK_MENU (container);
944 AttachInfo *ai = get_attach_info (child);
948 case CHILD_PROP_LEFT_ATTACH:
949 ai->left_attach = g_value_get_int (value);
951 case CHILD_PROP_RIGHT_ATTACH:
952 ai->right_attach = g_value_get_int (value);
954 case CHILD_PROP_TOP_ATTACH:
955 ai->top_attach = g_value_get_int (value);
957 case CHILD_PROP_BOTTOM_ATTACH:
958 ai->bottom_attach = g_value_get_int (value);
962 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
966 menu_queue_resize (menu);
970 gtk_menu_get_child_property (GtkContainer *container,
976 AttachInfo *ai = get_attach_info (child);
980 case CHILD_PROP_LEFT_ATTACH:
981 g_value_set_int (value, ai->left_attach);
983 case CHILD_PROP_RIGHT_ATTACH:
984 g_value_set_int (value, ai->right_attach);
986 case CHILD_PROP_TOP_ATTACH:
987 g_value_set_int (value, ai->top_attach);
989 case CHILD_PROP_BOTTOM_ATTACH:
990 g_value_set_int (value, ai->bottom_attach);
994 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1000 gtk_menu_window_event (GtkWidget *window,
1004 gboolean handled = FALSE;
1006 g_object_ref (window);
1007 g_object_ref (menu);
1009 switch (event->type)
1012 case GDK_KEY_RELEASE:
1013 handled = gtk_widget_event (menu, event);
1019 g_object_unref (window);
1020 g_object_unref (menu);
1026 gtk_menu_init (GtkMenu *menu)
1028 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
1030 menu->parent_menu_item = NULL;
1031 menu->old_active_menu_item = NULL;
1032 menu->accel_group = NULL;
1033 menu->position_func = NULL;
1034 menu->position_func_data = NULL;
1035 menu->toggle_size = 0;
1037 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1038 "type", GTK_WINDOW_POPUP,
1041 "signal::event", gtk_menu_window_event, menu,
1042 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1044 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
1045 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
1047 /* Refloat the menu, so that reference counting for the menu isn't
1048 * affected by it being a child of the toplevel
1050 g_object_force_floating (G_OBJECT (menu));
1051 menu->needs_destruction_ref_count = TRUE;
1053 menu->view_window = NULL;
1054 menu->bin_window = NULL;
1056 menu->scroll_offset = 0;
1057 menu->scroll_step = 0;
1058 menu->timeout_id = 0;
1059 menu->scroll_fast = FALSE;
1061 menu->tearoff_window = NULL;
1062 menu->tearoff_hbox = NULL;
1063 menu->torn_off = FALSE;
1064 menu->tearoff_active = FALSE;
1065 menu->tearoff_adjustment = NULL;
1066 menu->tearoff_scrollbar = NULL;
1068 menu->upper_arrow_visible = FALSE;
1069 menu->lower_arrow_visible = FALSE;
1070 menu->upper_arrow_prelight = FALSE;
1071 menu->lower_arrow_prelight = FALSE;
1073 priv->upper_arrow_state = GTK_STATE_NORMAL;
1074 priv->lower_arrow_state = GTK_STATE_NORMAL;
1076 priv->have_layout = FALSE;
1077 priv->monitor_num = -1;
1081 gtk_menu_destroy (GtkWidget *widget)
1083 GtkMenu *menu = GTK_MENU (widget);
1084 GtkMenuAttachData *data;
1085 GtkMenuPrivate *priv;
1087 gtk_menu_remove_scroll_timeout (menu);
1089 data = g_object_get_data (G_OBJECT (widget), attach_data_key);
1091 gtk_menu_detach (menu);
1093 gtk_menu_stop_navigating_submenu (menu);
1095 if (menu->old_active_menu_item)
1097 g_object_unref (menu->old_active_menu_item);
1098 menu->old_active_menu_item = NULL;
1101 /* Add back the reference count for being a child */
1102 if (menu->needs_destruction_ref_count)
1104 menu->needs_destruction_ref_count = FALSE;
1105 g_object_ref (widget);
1108 if (menu->accel_group)
1110 g_object_unref (menu->accel_group);
1111 menu->accel_group = NULL;
1115 gtk_widget_destroy (menu->toplevel);
1117 if (menu->tearoff_window)
1118 gtk_widget_destroy (menu->tearoff_window);
1120 priv = gtk_menu_get_private (menu);
1124 g_free (priv->heights);
1125 priv->heights = NULL;
1130 g_free (priv->title);
1134 if (priv->position_func_data_destroy)
1136 priv->position_func_data_destroy (menu->position_func_data);
1137 menu->position_func_data = NULL;
1138 priv->position_func_data_destroy = NULL;
1141 GTK_WIDGET_CLASS (gtk_menu_parent_class)->destroy (widget);
1145 menu_change_screen (GtkMenu *menu,
1146 GdkScreen *new_screen)
1148 GtkMenuPrivate *private = gtk_menu_get_private (menu);
1150 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1152 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1158 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1159 gtk_menu_position (menu);
1162 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1163 private->monitor_num = -1;
1167 attach_widget_screen_changed (GtkWidget *attach_widget,
1168 GdkScreen *previous_screen,
1171 if (gtk_widget_has_screen (attach_widget) &&
1172 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1174 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1179 gtk_menu_attach_to_widget (GtkMenu *menu,
1180 GtkWidget *attach_widget,
1181 GtkMenuDetachFunc detacher)
1183 GtkMenuAttachData *data;
1186 g_return_if_fail (GTK_IS_MENU (menu));
1187 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1189 /* keep this function in sync with gtk_widget_set_parent()
1192 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1195 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1196 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1200 g_object_ref_sink (menu);
1202 data = g_slice_new (GtkMenuAttachData);
1203 data->attach_widget = attach_widget;
1205 g_signal_connect (attach_widget, "screen-changed",
1206 G_CALLBACK (attach_widget_screen_changed), menu);
1207 attach_widget_screen_changed (attach_widget, NULL, menu);
1209 data->detacher = detacher;
1210 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1211 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1212 if (!g_list_find (list, menu))
1214 list = g_list_prepend (list, menu);
1216 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1217 (GDestroyNotify) g_list_free);
1219 if (gtk_widget_get_state (GTK_WIDGET (menu)) != GTK_STATE_NORMAL)
1220 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1222 /* we don't need to set the style here, since
1223 * we are a toplevel widget.
1226 /* Fallback title for menu comes from attach widget */
1227 gtk_menu_update_title (menu);
1229 g_object_notify (G_OBJECT (menu), "attach-widget");
1233 gtk_menu_get_attach_widget (GtkMenu *menu)
1235 GtkMenuAttachData *data;
1237 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1239 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1241 return data->attach_widget;
1246 gtk_menu_detach (GtkMenu *menu)
1248 GtkMenuAttachData *data;
1251 g_return_if_fail (GTK_IS_MENU (menu));
1253 /* keep this function in sync with gtk_widget_unparent()
1255 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1258 g_warning ("gtk_menu_detach(): menu is not attached");
1261 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1263 g_signal_handlers_disconnect_by_func (data->attach_widget,
1264 (gpointer) attach_widget_screen_changed,
1268 data->detacher (data->attach_widget, menu);
1269 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1270 list = g_list_remove (list, menu);
1272 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1273 (GDestroyNotify) g_list_free);
1275 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1277 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1278 gtk_widget_unrealize (GTK_WIDGET (menu));
1280 g_slice_free (GtkMenuAttachData, data);
1282 /* Fallback title for menu comes from attach widget */
1283 gtk_menu_update_title (menu);
1285 g_object_unref (menu);
1289 gtk_menu_remove (GtkContainer *container,
1292 GtkMenu *menu = GTK_MENU (container);
1294 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1296 /* Clear out old_active_menu_item if it matches the item we are removing
1298 if (menu->old_active_menu_item == widget)
1300 g_object_unref (menu->old_active_menu_item);
1301 menu->old_active_menu_item = NULL;
1304 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1305 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1307 menu_queue_resize (menu);
1313 return g_object_new (GTK_TYPE_MENU, NULL);
1317 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1321 GtkMenu *menu = GTK_MENU (menu_shell);
1322 AttachInfo *ai = get_attach_info (child);
1324 ai->left_attach = -1;
1325 ai->right_attach = -1;
1326 ai->top_attach = -1;
1327 ai->bottom_attach = -1;
1329 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1330 gtk_widget_set_parent_window (child, menu->bin_window);
1332 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1334 menu_queue_resize (menu);
1338 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1343 widget = GTK_WIDGET (menu);
1348 cairo_surface_t *surface;
1349 cairo_pattern_t *pattern;
1352 menu->tearoff_active = FALSE;
1353 menu->saved_scroll_offset = menu->scroll_offset;
1355 window = gtk_widget_get_window (menu->tearoff_window);
1356 width = gdk_window_get_width (window);
1357 height = gdk_window_get_height (window);
1359 surface = gdk_window_create_similar_surface (window,
1360 CAIRO_CONTENT_COLOR,
1364 cr = cairo_create (surface);
1365 gdk_cairo_set_source_window (cr,
1371 gtk_widget_set_size_request (menu->tearoff_window,
1375 pattern = cairo_pattern_create_for_surface (surface);
1376 gdk_window_set_background_pattern (window, pattern);
1378 cairo_pattern_destroy (pattern);
1379 cairo_surface_destroy (surface);
1384 popup_grab_on_window (GdkWindow *window,
1385 GdkDevice *keyboard,
1387 guint32 activate_time)
1390 gdk_device_grab (keyboard, window,
1391 GDK_OWNERSHIP_WINDOW, TRUE,
1392 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1393 NULL, activate_time) != GDK_GRAB_SUCCESS)
1397 gdk_device_grab (pointer, window,
1398 GDK_OWNERSHIP_WINDOW, TRUE,
1399 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1400 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1401 GDK_POINTER_MOTION_MASK,
1402 NULL, activate_time) != GDK_GRAB_SUCCESS)
1405 gdk_device_ungrab (keyboard, activate_time);
1414 * gtk_menu_popup_for_device:
1415 * @menu: a #GtkMenu.
1416 * @device: (allow-none): a #GdkDevice
1417 * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1418 * menu item, or %NULL
1419 * @parent_menu_item: (allow-none): the menu item whose activation triggered
1420 * the popup, or %NULL
1421 * @func: (allow-none): a user supplied function used to position the menu,
1423 * @data: (allow-none): user supplied data to be passed to @func
1424 * @destroy: (allow-none): destroy notify for @data
1425 * @button: the mouse button which was pressed to initiate the event
1426 * @activate_time: the time at which the activation event occurred
1428 * Displays a menu and makes it available for selection.
1430 * Applications can use this function to display context-sensitive menus,
1431 * and will typically supply %NULL for the @parent_menu_shell,
1432 * @parent_menu_item, @func, @data and @destroy parameters. The default
1433 * menu positioning function will position the menu at the current position
1434 * of @device (or its corresponding pointer).
1436 * The @button parameter should be the mouse button pressed to initiate
1437 * the menu popup. If the menu popup was initiated by something other than
1438 * a mouse button press, such as a mouse button release or a keypress,
1439 * @button should be 0.
1441 * The @activate_time parameter is used to conflict-resolve initiation of
1442 * concurrent requests for mouse/keyboard grab requests. To function
1443 * properly, this needs to be the time stamp of the user event (such as
1444 * a mouse click or key press) that caused the initiation of the popup.
1445 * Only if no such event is available, gtk_get_current_event_time() can
1451 gtk_menu_popup_for_device (GtkMenu *menu,
1453 GtkWidget *parent_menu_shell,
1454 GtkWidget *parent_menu_item,
1455 GtkMenuPositionFunc func,
1457 GDestroyNotify destroy,
1459 guint32 activate_time)
1462 GtkWidget *xgrab_shell;
1464 GdkEvent *current_event;
1465 GtkMenuShell *menu_shell;
1466 gboolean grab_keyboard;
1467 GtkMenuPrivate *priv;
1468 GtkWidget *parent_toplevel;
1469 GdkDevice *keyboard, *pointer;
1471 g_return_if_fail (GTK_IS_MENU (menu));
1472 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1475 device = gtk_get_current_event_device ();
1479 GdkDisplay *display;
1480 GdkDeviceManager *device_manager;
1483 display = gtk_widget_get_display (GTK_WIDGET (menu));
1484 device_manager = gdk_display_get_device_manager (display);
1485 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1487 device = devices->data;
1489 g_list_free (devices);
1492 widget = GTK_WIDGET (menu);
1493 menu_shell = GTK_MENU_SHELL (menu);
1494 priv = gtk_menu_get_private (menu);
1496 if (device->source == GDK_SOURCE_KEYBOARD)
1499 pointer = gdk_device_get_associated_device (device);
1504 keyboard = gdk_device_get_associated_device (device);
1507 menu_shell->parent_menu_shell = parent_menu_shell;
1509 priv->seen_item_enter = FALSE;
1511 /* Find the last viewable ancestor, and make an X grab on it
1513 parent = GTK_WIDGET (menu);
1517 gboolean viewable = TRUE;
1518 GtkWidget *tmp = parent;
1522 if (!gtk_widget_get_mapped (tmp))
1527 tmp = gtk_widget_get_parent (tmp);
1531 xgrab_shell = parent;
1533 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1536 /* We want to receive events generated when we map the menu; unfortunately,
1537 * since there is probably already an implicit grab in place from the
1538 * button that the user used to pop up the menu, we won't receive then --
1539 * in particular, the EnterNotify when the menu pops up under the pointer.
1541 * If we are grabbing on a parent menu shell, no problem; just grab on
1542 * that menu shell first before popping up the window with owner_events = TRUE.
1544 * When grabbing on the menu itself, things get more convuluted - we
1545 * we do an explicit grab on a specially created window with
1546 * owner_events = TRUE, which we override further down with a grab
1547 * on the menu. (We can't grab on the menu until it is mapped; we
1548 * probably could just leave the grab on the other window, with a
1549 * little reorganization of the code in gtkmenu*).
1551 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1552 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1557 if (xgrab_shell && xgrab_shell != widget)
1559 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1561 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1562 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1567 GdkWindow *transfer_window;
1569 xgrab_shell = widget;
1570 transfer_window = menu_grab_transfer_window_get (menu);
1571 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1573 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1574 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1578 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1580 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1581 * with a stuck up window, we just abort here. Presumably the user will
1584 menu_shell->parent_menu_shell = NULL;
1585 menu_grab_transfer_window_destroy (menu);
1589 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1590 menu_shell->active = TRUE;
1591 menu_shell->button = button;
1593 /* If we are popping up the menu from something other than, a button
1594 * press then, as a heuristic, we ignore enter events for the menu
1595 * until we get a MOTION_NOTIFY.
1598 current_event = gtk_get_current_event ();
1601 if ((current_event->type != GDK_BUTTON_PRESS) &&
1602 (current_event->type != GDK_ENTER_NOTIFY))
1603 menu_shell->ignore_enter = TRUE;
1605 gdk_event_free (current_event);
1608 menu_shell->ignore_enter = TRUE;
1612 gtk_menu_tearoff_bg_copy (menu);
1614 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1617 parent_toplevel = NULL;
1618 if (parent_menu_shell)
1619 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1620 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1622 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1624 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1627 /* Set transient for to get the right window group and parent relationship */
1628 if (GTK_IS_WINDOW (parent_toplevel))
1629 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1630 GTK_WINDOW (parent_toplevel));
1632 menu->parent_menu_item = parent_menu_item;
1633 menu->position_func = func;
1634 menu->position_func_data = data;
1635 priv->position_func_data_destroy = destroy;
1636 menu_shell->activate_time = activate_time;
1638 /* We need to show the menu here rather in the init function because
1639 * code expects to be able to tell if the menu is onscreen by
1640 * looking at the gtk_widget_get_visible (menu)
1642 gtk_widget_show (GTK_WIDGET (menu));
1644 /* Position the menu, possibly changing the size request
1646 gtk_menu_position (menu);
1648 /* Compute the size of the toplevel and realize it so we
1649 * can scroll correctly.
1652 GtkRequisition tmp_request;
1653 GtkAllocation tmp_allocation = { 0, };
1655 /* Instead of trusting the menu position function to queue a resize when the
1656 * menu goes out of bounds, invalidate the cached size here. */
1657 gtk_widget_queue_resize (GTK_WIDGET (menu));
1658 gtk_widget_get_preferred_size (menu->toplevel, &tmp_request, NULL);
1660 tmp_allocation.width = tmp_request.width;
1661 tmp_allocation.height = tmp_request.height;
1663 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1665 gtk_widget_realize (GTK_WIDGET (menu));
1668 gtk_menu_scroll_to (menu, menu->scroll_offset);
1670 /* if no item is selected, select the first one */
1671 if (!menu_shell->active_menu_item)
1673 gboolean touchscreen_mode;
1675 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1676 "gtk-touchscreen-mode", &touchscreen_mode,
1679 if (touchscreen_mode)
1680 gtk_menu_shell_select_first (menu_shell, TRUE);
1683 /* Once everything is set up correctly, map the toplevel window on
1686 gtk_widget_show (menu->toplevel);
1688 if (xgrab_shell == widget)
1689 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1691 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1693 if (parent_menu_shell)
1695 gboolean keyboard_mode;
1697 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1698 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1700 else if (menu_shell->button == 0) /* a keynav-activated context menu */
1701 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1703 _gtk_menu_shell_update_mnemonics (menu_shell);
1708 * @menu: a #GtkMenu.
1709 * @parent_menu_shell: (allow-none): the menu shell containing the triggering menu item, or %NULL
1710 * @parent_menu_item: (allow-none): the menu item whose activation triggered the popup, or %NULL
1711 * @func: (allow-none): a user supplied function used to position the menu, or %NULL
1712 * @data: (allow-none): user supplied data to be passed to @func.
1713 * @button: the mouse button which was pressed to initiate the event.
1714 * @activate_time: the time at which the activation event occurred.
1716 * Displays a menu and makes it available for selection. Applications can use
1717 * this function to display context-sensitive menus, and will typically supply
1718 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1719 * parameters. The default menu positioning function will position the menu
1720 * at the current mouse cursor position.
1722 * The @button parameter should be the mouse button pressed to initiate
1723 * the menu popup. If the menu popup was initiated by something other than
1724 * a mouse button press, such as a mouse button release or a keypress,
1725 * @button should be 0.
1727 * The @activate_time parameter is used to conflict-resolve initiation of
1728 * concurrent requests for mouse/keyboard grab requests. To function
1729 * properly, this needs to be the time stamp of the user event (such as
1730 * a mouse click or key press) that caused the initiation of the popup.
1731 * Only if no such event is available, gtk_get_current_event_time() can
1735 gtk_menu_popup (GtkMenu *menu,
1736 GtkWidget *parent_menu_shell,
1737 GtkWidget *parent_menu_item,
1738 GtkMenuPositionFunc func,
1741 guint32 activate_time)
1743 g_return_if_fail (GTK_IS_MENU (menu));
1745 gtk_menu_popup_for_device (menu,
1750 button, activate_time);
1754 gtk_menu_popdown (GtkMenu *menu)
1756 GtkMenuPrivate *private;
1757 GtkMenuShell *menu_shell;
1760 g_return_if_fail (GTK_IS_MENU (menu));
1762 menu_shell = GTK_MENU_SHELL (menu);
1763 private = gtk_menu_get_private (menu);
1765 menu_shell->parent_menu_shell = NULL;
1766 menu_shell->active = FALSE;
1767 menu_shell->ignore_enter = FALSE;
1769 private->have_position = FALSE;
1771 gtk_menu_stop_scrolling (menu);
1773 gtk_menu_stop_navigating_submenu (menu);
1775 if (menu_shell->active_menu_item)
1777 if (menu->old_active_menu_item)
1778 g_object_unref (menu->old_active_menu_item);
1779 menu->old_active_menu_item = menu_shell->active_menu_item;
1780 g_object_ref (menu->old_active_menu_item);
1783 gtk_menu_shell_deselect (menu_shell);
1785 /* The X Grab, if present, will automatically be removed when we hide
1787 gtk_widget_hide (menu->toplevel);
1788 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1790 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1794 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1796 if (gtk_bin_get_child (GTK_BIN (menu->toplevel)))
1798 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1802 /* We popped up the menu from the tearoff, so we need to
1803 * release the grab - we aren't actually hiding the menu.
1805 if (menu_shell->have_xgrab && pointer)
1807 GdkDevice *keyboard;
1809 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1810 keyboard = gdk_device_get_associated_device (pointer);
1813 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1817 /* gtk_menu_popdown is called each time a menu item is selected from
1818 * a torn off menu. Only scroll back to the saved position if the
1819 * non-tearoff menu was popped down.
1821 if (!menu->tearoff_active)
1822 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1823 menu->tearoff_active = TRUE;
1826 gtk_widget_hide (GTK_WIDGET (menu));
1828 menu_shell->have_xgrab = FALSE;
1831 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1833 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1835 menu_grab_transfer_window_destroy (menu);
1839 gtk_menu_get_active (GtkMenu *menu)
1844 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1846 if (!menu->old_active_menu_item)
1849 children = GTK_MENU_SHELL (menu)->children;
1853 child = children->data;
1854 children = children->next;
1856 if (gtk_bin_get_child (GTK_BIN (child)))
1861 menu->old_active_menu_item = child;
1862 if (menu->old_active_menu_item)
1863 g_object_ref (menu->old_active_menu_item);
1866 return menu->old_active_menu_item;
1870 gtk_menu_set_active (GtkMenu *menu,
1876 g_return_if_fail (GTK_IS_MENU (menu));
1878 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1881 child = tmp_list->data;
1882 if (gtk_bin_get_child (GTK_BIN (child)))
1884 if (menu->old_active_menu_item)
1885 g_object_unref (menu->old_active_menu_item);
1886 menu->old_active_menu_item = child;
1887 g_object_ref (menu->old_active_menu_item);
1894 * gtk_menu_set_accel_group:
1895 * @accel_group: (allow-none):
1898 gtk_menu_set_accel_group (GtkMenu *menu,
1899 GtkAccelGroup *accel_group)
1901 g_return_if_fail (GTK_IS_MENU (menu));
1903 if (menu->accel_group != accel_group)
1905 if (menu->accel_group)
1906 g_object_unref (menu->accel_group);
1907 menu->accel_group = accel_group;
1908 if (menu->accel_group)
1909 g_object_ref (menu->accel_group);
1910 _gtk_menu_refresh_accel_paths (menu, TRUE);
1915 gtk_menu_get_accel_group (GtkMenu *menu)
1917 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1919 return menu->accel_group;
1923 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1926 /* Menu items chain here to figure whether they can activate their
1927 * accelerators. Unlike ordinary widgets, menus allow accel
1928 * activation even if invisible since that's the usual case for
1929 * submenus/popup-menus. however, the state of the attach widget
1930 * affects the "activeness" of the menu.
1932 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1935 return gtk_widget_can_activate_accel (awidget, signal_id);
1937 return gtk_widget_is_sensitive (widget);
1941 * gtk_menu_set_accel_path
1942 * @menu: a valid #GtkMenu
1943 * @accel_path: (allow-none): a valid accelerator path
1945 * Sets an accelerator path for this menu from which accelerator paths
1946 * for its immediate children, its menu items, can be constructed.
1947 * The main purpose of this function is to spare the programmer the
1948 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1949 * each menu item that should support runtime user changable accelerators.
1950 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1951 * each menu item of this menu, that contains a label describing its purpose,
1952 * automatically gets an accel path assigned. For example, a menu containing
1953 * menu items "New" and "Exit", will, after
1954 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1955 * has been called, assign its items the accel paths:
1956 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1957 * Assigning accel paths to menu items then enables the user to change
1958 * their accelerators at runtime. More details about accelerator paths
1959 * and their default setups can be found at gtk_accel_map_add_entry().
1961 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1962 * pass a static string, you can save some memory by interning it first with
1963 * g_intern_static_string().
1966 gtk_menu_set_accel_path (GtkMenu *menu,
1967 const gchar *accel_path)
1969 g_return_if_fail (GTK_IS_MENU (menu));
1971 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1973 /* FIXME: accel_path should be defined as const gchar* */
1974 menu->accel_path = (gchar*)g_intern_string (accel_path);
1975 if (menu->accel_path)
1976 _gtk_menu_refresh_accel_paths (menu, FALSE);
1980 * gtk_menu_get_accel_path
1981 * @menu: a valid #GtkMenu
1983 * Retrieves the accelerator path set on the menu.
1985 * Returns: the accelerator path set on the menu.
1990 gtk_menu_get_accel_path (GtkMenu *menu)
1992 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1994 return menu->accel_path;
1999 gboolean group_changed;
2003 refresh_accel_paths_foreach (GtkWidget *widget,
2006 AccelPropagation *prop = data;
2008 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
2009 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2010 prop->menu->accel_path,
2011 prop->menu->accel_group,
2012 prop->group_changed);
2016 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
2017 gboolean group_changed)
2019 g_return_if_fail (GTK_IS_MENU (menu));
2021 if (menu->accel_path && menu->accel_group)
2023 AccelPropagation prop;
2026 prop.group_changed = group_changed;
2027 gtk_container_foreach (GTK_CONTAINER (menu),
2028 refresh_accel_paths_foreach,
2034 gtk_menu_reposition (GtkMenu *menu)
2036 g_return_if_fail (GTK_IS_MENU (menu));
2038 if (!menu->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2039 gtk_menu_position (menu);
2043 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2046 g_return_if_fail (GTK_IS_MENU (menu));
2048 if (adjustment->value != menu->scroll_offset)
2049 gtk_menu_scroll_to (menu, adjustment->value);
2053 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2056 GdkGeometry geometry_hints;
2057 GtkMenuPrivate *priv;
2059 if (!menu->tearoff_window)
2062 priv = gtk_menu_get_private (menu);
2064 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2066 GtkRequisition requisition;
2068 gtk_widget_get_preferred_size (menu->tearoff_scrollbar,
2069 &requisition, NULL);
2070 width += requisition.width;
2073 geometry_hints.min_width = width;
2074 geometry_hints.max_width = width;
2076 geometry_hints.min_height = 0;
2077 geometry_hints.max_height = priv->requested_height;
2079 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
2082 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2086 gtk_menu_update_title (GtkMenu *menu)
2088 if (menu->tearoff_window)
2091 GtkWidget *attach_widget;
2093 title = gtk_menu_get_title (menu);
2096 attach_widget = gtk_menu_get_attach_widget (menu);
2097 if (GTK_IS_MENU_ITEM (attach_widget))
2099 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2100 if (GTK_IS_LABEL (child))
2101 title = gtk_label_get_text (GTK_LABEL (child));
2106 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
2111 gtk_menu_get_toplevel (GtkWidget *menu)
2113 GtkWidget *attach, *toplevel;
2115 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2117 if (GTK_IS_MENU_ITEM (attach))
2118 attach = gtk_widget_get_parent (attach);
2120 if (GTK_IS_MENU (attach))
2121 return gtk_menu_get_toplevel (attach);
2122 else if (GTK_IS_WIDGET (attach))
2124 toplevel = gtk_widget_get_toplevel (attach);
2125 if (gtk_widget_is_toplevel (toplevel))
2133 tearoff_window_destroyed (GtkWidget *widget,
2136 gtk_menu_set_tearoff_state (menu, FALSE);
2140 gtk_menu_set_tearoff_state (GtkMenu *menu,
2144 GtkMenuPrivate *priv;
2146 g_return_if_fail (GTK_IS_MENU (menu));
2148 priv = gtk_menu_get_private (menu);
2150 if (menu->torn_off != torn_off)
2152 menu->torn_off = torn_off;
2153 menu->tearoff_active = torn_off;
2157 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2158 gtk_menu_popdown (menu);
2160 if (!menu->tearoff_window)
2162 GtkWidget *toplevel;
2164 menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2165 "type", GTK_WINDOW_TOPLEVEL,
2166 "screen", gtk_widget_get_screen (menu->toplevel),
2167 "app-paintable", TRUE,
2170 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
2171 GDK_WINDOW_TYPE_HINT_MENU);
2172 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
2173 g_signal_connect (menu->tearoff_window, "destroy",
2174 G_CALLBACK (tearoff_window_destroyed), menu);
2175 g_signal_connect (menu->tearoff_window, "event",
2176 G_CALLBACK (gtk_menu_window_event), menu);
2178 gtk_menu_update_title (menu);
2180 gtk_widget_realize (menu->tearoff_window);
2182 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2183 if (toplevel != NULL)
2184 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
2185 GTK_WINDOW (toplevel));
2187 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
2188 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
2190 height = gdk_window_get_height (gtk_widget_get_window (GTK_WIDGET (menu)));
2191 menu->tearoff_adjustment = gtk_adjustment_new (0,
2192 0, priv->requested_height,
2196 g_object_connect (menu->tearoff_adjustment,
2197 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2199 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
2201 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
2202 menu->tearoff_scrollbar,
2205 if (menu->tearoff_adjustment->upper > height)
2206 gtk_widget_show (menu->tearoff_scrollbar);
2208 gtk_widget_show (menu->tearoff_hbox);
2211 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
2213 /* Update menu->requisition
2215 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
2218 gtk_menu_set_tearoff_hints (menu, gdk_window_get_width (gtk_widget_get_window (GTK_WIDGET (menu))));
2220 gtk_widget_realize (menu->tearoff_window);
2221 gtk_menu_position (menu);
2223 gtk_widget_show (GTK_WIDGET (menu));
2224 gtk_widget_show (menu->tearoff_window);
2226 gtk_menu_scroll_to (menu, 0);
2231 gtk_widget_hide (GTK_WIDGET (menu));
2232 gtk_widget_hide (menu->tearoff_window);
2233 if (GTK_IS_CONTAINER (menu->toplevel))
2234 gtk_menu_reparent (menu, menu->toplevel, FALSE);
2235 gtk_widget_destroy (menu->tearoff_window);
2237 menu->tearoff_window = NULL;
2238 menu->tearoff_hbox = NULL;
2239 menu->tearoff_scrollbar = NULL;
2240 menu->tearoff_adjustment = NULL;
2243 g_object_notify (G_OBJECT (menu), "tearoff-state");
2248 * gtk_menu_get_tearoff_state:
2251 * Returns whether the menu is torn off. See
2252 * gtk_menu_set_tearoff_state ().
2254 * Return value: %TRUE if the menu is currently torn off.
2257 gtk_menu_get_tearoff_state (GtkMenu *menu)
2259 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2261 return menu->torn_off;
2265 * gtk_menu_set_title:
2267 * @title: a string containing the title for the menu.
2269 * Sets the title string for the menu. The title is displayed when the menu
2270 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
2271 * attached to a parent menu item, and if so it will try to use the same text as
2272 * that menu item's label.
2275 gtk_menu_set_title (GtkMenu *menu,
2278 GtkMenuPrivate *priv;
2281 g_return_if_fail (GTK_IS_MENU (menu));
2283 priv = gtk_menu_get_private (menu);
2285 old_title = priv->title;
2286 priv->title = g_strdup (title);
2289 gtk_menu_update_title (menu);
2290 g_object_notify (G_OBJECT (menu), "tearoff-title");
2294 * gtk_menu_get_title:
2297 * Returns the title of the menu. See gtk_menu_set_title().
2299 * Return value: the title of the menu, or %NULL if the menu has no
2300 * title set on it. This string is owned by the widget and should
2301 * not be modified or freed.
2303 G_CONST_RETURN gchar *
2304 gtk_menu_get_title (GtkMenu *menu)
2306 GtkMenuPrivate *priv;
2308 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2310 priv = gtk_menu_get_private (menu);
2316 gtk_menu_reorder_child (GtkMenu *menu,
2320 GtkMenuShell *menu_shell;
2322 g_return_if_fail (GTK_IS_MENU (menu));
2323 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2325 menu_shell = GTK_MENU_SHELL (menu);
2327 if (g_list_find (menu_shell->children, child))
2329 menu_shell->children = g_list_remove (menu_shell->children, child);
2330 menu_shell->children = g_list_insert (menu_shell->children, child, position);
2332 menu_queue_resize (menu);
2337 gtk_menu_style_set (GtkWidget *widget,
2338 GtkStyle *previous_style)
2340 if (gtk_widget_get_realized (widget))
2342 GtkMenu *menu = GTK_MENU (widget);
2345 style = gtk_widget_get_style (widget);
2347 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2348 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2349 gtk_style_set_background (style, gtk_widget_get_window (widget), GTK_STATE_NORMAL);
2354 get_arrows_border (GtkMenu *menu,
2357 guint scroll_arrow_height;
2358 GtkArrowPlacement arrow_placement;
2360 gtk_widget_style_get (GTK_WIDGET (menu),
2361 "scroll-arrow-vlength", &scroll_arrow_height,
2362 "arrow_placement", &arrow_placement,
2365 switch (arrow_placement)
2367 case GTK_ARROWS_BOTH:
2368 border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2369 border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2372 case GTK_ARROWS_START:
2373 border->top = (menu->upper_arrow_visible ||
2374 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2378 case GTK_ARROWS_END:
2380 border->bottom = (menu->upper_arrow_visible ||
2381 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2385 border->left = border->right = 0;
2389 gtk_menu_realize (GtkWidget *widget)
2391 GtkAllocation allocation;
2394 GdkWindowAttr attributes;
2395 gint attributes_mask;
2398 GtkMenuPrivate *priv;
2401 guint vertical_padding;
2402 guint horizontal_padding;
2403 GtkBorder arrow_border;
2405 g_return_if_fail (GTK_IS_MENU (widget));
2407 menu = GTK_MENU (widget);
2408 priv = gtk_menu_get_private (menu);
2410 gtk_widget_set_realized (widget, TRUE);
2412 gtk_widget_get_allocation (widget, &allocation);
2414 attributes.window_type = GDK_WINDOW_CHILD;
2415 attributes.x = allocation.x;
2416 attributes.y = allocation.y;
2417 attributes.width = allocation.width;
2418 attributes.height = allocation.height;
2419 attributes.wclass = GDK_INPUT_OUTPUT;
2420 attributes.visual = gtk_widget_get_visual (widget);
2421 attributes.event_mask = gtk_widget_get_events (widget);
2422 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2423 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2425 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2427 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2428 &attributes, attributes_mask);
2429 gtk_widget_set_window (widget, window);
2430 gdk_window_set_user_data (window, widget);
2432 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2434 style = gtk_widget_get_style (widget);
2435 gtk_widget_style_get (GTK_WIDGET (menu),
2436 "vertical-padding", &vertical_padding,
2437 "horizontal-padding", &horizontal_padding,
2440 gtk_widget_get_allocation (widget, &allocation);
2442 attributes.x = border_width + style->xthickness + horizontal_padding;
2443 attributes.y = border_width + style->ythickness + vertical_padding;
2444 attributes.width = MAX (1, allocation.width - attributes.x * 2);
2445 attributes.height = MAX (1, allocation.height - attributes.y * 2);
2447 get_arrows_border (menu, &arrow_border);
2448 attributes.y += arrow_border.top;
2449 attributes.height -= arrow_border.top;
2450 attributes.height -= arrow_border.bottom;
2452 menu->view_window = gdk_window_new (window,
2453 &attributes, attributes_mask);
2454 gdk_window_set_user_data (menu->view_window, menu);
2456 gtk_widget_get_allocation (widget, &allocation);
2460 attributes.width = MAX (1, allocation.width - (border_width + style->xthickness + horizontal_padding) * 2);
2461 attributes.height = MAX (1, priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2);
2463 menu->bin_window = gdk_window_new (menu->view_window,
2464 &attributes, attributes_mask);
2465 gdk_window_set_user_data (menu->bin_window, menu);
2467 children = GTK_MENU_SHELL (menu)->children;
2470 child = children->data;
2471 children = children->next;
2473 gtk_widget_set_parent_window (child, menu->bin_window);
2476 gtk_widget_style_attach (widget);
2477 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2478 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2479 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
2481 if (GTK_MENU_SHELL (widget)->active_menu_item)
2482 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2483 GTK_MENU_SHELL (widget)->active_menu_item);
2485 gdk_window_show (menu->bin_window);
2486 gdk_window_show (menu->view_window);
2490 gtk_menu_focus (GtkWidget *widget,
2491 GtkDirectionType direction)
2494 * A menu or its menu items cannot have focus
2499 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2502 menu_grab_transfer_window_get (GtkMenu *menu)
2504 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2507 GdkWindowAttr attributes;
2508 gint attributes_mask;
2510 attributes.x = -100;
2511 attributes.y = -100;
2512 attributes.width = 10;
2513 attributes.height = 10;
2514 attributes.window_type = GDK_WINDOW_TEMP;
2515 attributes.wclass = GDK_INPUT_ONLY;
2516 attributes.override_redirect = TRUE;
2517 attributes.event_mask = 0;
2519 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2521 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2522 &attributes, attributes_mask);
2523 gdk_window_set_user_data (window, menu);
2525 gdk_window_show (window);
2527 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2534 menu_grab_transfer_window_destroy (GtkMenu *menu)
2536 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2539 gdk_window_set_user_data (window, NULL);
2540 gdk_window_destroy (window);
2541 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2546 gtk_menu_unrealize (GtkWidget *widget)
2548 GtkMenu *menu = GTK_MENU (widget);
2550 menu_grab_transfer_window_destroy (menu);
2552 gdk_window_set_user_data (menu->view_window, NULL);
2553 gdk_window_destroy (menu->view_window);
2554 menu->view_window = NULL;
2556 gdk_window_set_user_data (menu->bin_window, NULL);
2557 gdk_window_destroy (menu->bin_window);
2558 menu->bin_window = NULL;
2560 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2565 calculate_line_heights (GtkMenu *menu,
2567 guint **ret_min_heights,
2568 guint **ret_nat_heights)
2570 GtkMenuShell *menu_shell;
2571 GtkMenuPrivate *priv;
2572 GtkWidget *child, *widget;
2574 guint horizontal_padding;
2582 widget = GTK_WIDGET (menu);
2583 menu_shell = GTK_MENU_SHELL (widget);
2584 priv = gtk_menu_get_private (menu);
2586 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2587 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2588 n_heights = gtk_menu_get_n_rows (menu);
2589 n_columns = gtk_menu_get_n_columns (menu);
2590 avail_width = for_width - (2 * menu->toggle_size + priv->accel_size) * n_columns;
2592 gtk_widget_style_get (GTK_WIDGET (menu),
2593 "horizontal-padding", &horizontal_padding,
2596 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2597 avail_width -= (border_width + horizontal_padding + gtk_widget_get_style (widget)->xthickness) * 2;
2599 for (children = menu_shell->children; children; children = children->next)
2604 gint child_min, child_nat;
2606 child = children->data;
2608 if (! gtk_widget_get_visible (child))
2611 get_effective_child_attach (child, &l, &r, &t, &b);
2613 part = avail_width / (r - l);
2615 gtk_widget_get_preferred_height_for_width (child, part,
2616 &child_min, &child_nat);
2618 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2620 part = MAX (child_min, toggle_size) / (b - t);
2621 min_heights[t] = MAX (min_heights[t], part);
2623 part = MAX (child_nat, toggle_size) / (b - t);
2624 nat_heights[t] = MAX (nat_heights[t], part);
2627 if (ret_min_heights)
2628 *ret_min_heights = min_heights;
2630 g_free (min_heights);
2632 if (ret_nat_heights)
2633 *ret_nat_heights = nat_heights;
2635 g_free (nat_heights);
2641 gtk_menu_size_allocate (GtkWidget *widget,
2642 GtkAllocation *allocation)
2645 GtkMenuShell *menu_shell;
2647 GtkAllocation child_allocation;
2648 GtkMenuPrivate *priv;
2654 guint vertical_padding;
2655 guint horizontal_padding;
2657 g_return_if_fail (GTK_IS_MENU (widget));
2658 g_return_if_fail (allocation != NULL);
2660 menu = GTK_MENU (widget);
2661 menu_shell = GTK_MENU_SHELL (widget);
2662 priv = gtk_menu_get_private (menu);
2664 gtk_widget_set_allocation (widget, allocation);
2666 style = gtk_widget_get_style (widget);
2668 gtk_widget_style_get (GTK_WIDGET (menu),
2669 "vertical-padding", &vertical_padding,
2670 "horizontal-padding", &horizontal_padding,
2672 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2674 g_free (priv->heights);
2675 priv->heights_length =
2676 calculate_line_heights (menu,
2681 /* refresh our cached height request */
2682 priv->requested_height = (border_width + vertical_padding + style->ythickness) * 2;
2683 for (i = 0; i < priv->heights_length; i++)
2684 priv->requested_height += priv->heights[i];
2686 x = border_width + style->xthickness + horizontal_padding;
2687 y = border_width + style->ythickness + vertical_padding;
2689 width = MAX (1, allocation->width - x * 2);
2690 height = MAX (1, allocation->height - y * 2);
2692 if (menu_shell->active)
2693 gtk_menu_scroll_to (menu, menu->scroll_offset);
2695 if (!menu->tearoff_active)
2697 GtkBorder arrow_border;
2699 get_arrows_border (menu, &arrow_border);
2700 y += arrow_border.top;
2701 height -= arrow_border.top;
2702 height -= arrow_border.bottom;
2705 if (gtk_widget_get_realized (widget))
2707 gdk_window_move_resize (gtk_widget_get_window (widget),
2708 allocation->x, allocation->y,
2709 allocation->width, allocation->height);
2711 gdk_window_move_resize (menu->view_window,
2718 if (menu_shell->children)
2720 gint base_width = width / gtk_menu_get_n_columns (menu);
2722 children = menu_shell->children;
2725 child = children->data;
2726 children = children->next;
2728 if (gtk_widget_get_visible (child))
2733 get_effective_child_attach (child, &l, &r, &t, &b);
2735 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2738 tmp = gtk_menu_get_n_columns (menu) - l;
2739 l = gtk_menu_get_n_columns (menu) - r;
2743 child_allocation.width = (r - l) * base_width;
2744 child_allocation.height = 0;
2745 child_allocation.x = l * base_width;
2746 child_allocation.y = 0;
2748 for (i = 0; i < b; i++)
2751 child_allocation.y += priv->heights[i];
2753 child_allocation.height += priv->heights[i];
2756 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2759 gtk_widget_size_allocate (child, &child_allocation);
2760 gtk_widget_queue_draw (child);
2764 /* Resize the item window */
2765 if (gtk_widget_get_realized (widget))
2771 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2772 height += priv->heights[i];
2774 width = gtk_menu_get_n_columns (menu) * base_width;
2775 gdk_window_resize (menu->bin_window, width, height);
2778 if (menu->tearoff_active)
2780 if (height >= priv->requested_height)
2782 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2784 gtk_widget_hide (menu->tearoff_scrollbar);
2785 gtk_menu_set_tearoff_hints (menu, allocation->width);
2787 gtk_menu_scroll_to (menu, 0);
2792 menu->tearoff_adjustment->upper = priv->requested_height;
2793 menu->tearoff_adjustment->page_size = allocation->height;
2795 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2796 menu->tearoff_adjustment->upper)
2799 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2802 gtk_menu_scroll_to (menu, value);
2805 gtk_adjustment_changed (menu->tearoff_adjustment);
2807 if (!gtk_widget_get_visible (menu->tearoff_scrollbar))
2809 gtk_widget_show (menu->tearoff_scrollbar);
2810 gtk_menu_set_tearoff_hints (menu, allocation->width);
2818 get_arrows_visible_area (GtkMenu *menu,
2819 GdkRectangle *border,
2820 GdkRectangle *upper,
2821 GdkRectangle *lower,
2824 GtkArrowPlacement arrow_placement;
2826 GtkWidget *widget = GTK_WIDGET (menu);
2828 guint vertical_padding;
2829 guint horizontal_padding;
2830 gint scroll_arrow_height;
2832 style = gtk_widget_get_style (widget);
2834 gtk_widget_style_get (widget,
2835 "vertical-padding", &vertical_padding,
2836 "horizontal-padding", &horizontal_padding,
2837 "scroll-arrow-vlength", &scroll_arrow_height,
2838 "arrow-placement", &arrow_placement,
2841 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2842 border->x = border_width + style->xthickness + horizontal_padding;
2843 border->y = border_width + style->ythickness + vertical_padding;
2844 border->width = gdk_window_get_width (gtk_widget_get_window (widget));
2845 border->height = gdk_window_get_height (gtk_widget_get_window (widget));
2847 switch (arrow_placement)
2849 case GTK_ARROWS_BOTH:
2850 upper->x = border->x;
2851 upper->y = border->y;
2852 upper->width = border->width - 2 * border->x;
2853 upper->height = scroll_arrow_height;
2855 lower->x = border->x;
2856 lower->y = border->height - border->y - scroll_arrow_height;
2857 lower->width = border->width - 2 * border->x;
2858 lower->height = scroll_arrow_height;
2861 case GTK_ARROWS_START:
2862 upper->x = border->x;
2863 upper->y = border->y;
2864 upper->width = (border->width - 2 * border->x) / 2;
2865 upper->height = scroll_arrow_height;
2867 lower->x = border->x + upper->width;
2868 lower->y = border->y;
2869 lower->width = (border->width - 2 * border->x) / 2;
2870 lower->height = scroll_arrow_height;
2873 case GTK_ARROWS_END:
2874 upper->x = border->x;
2875 upper->y = border->height - border->y - scroll_arrow_height;
2876 upper->width = (border->width - 2 * border->x) / 2;
2877 upper->height = scroll_arrow_height;
2879 lower->x = border->x + upper->width;
2880 lower->y = border->height - border->y - scroll_arrow_height;
2881 lower->width = (border->width - 2 * border->x) / 2;
2882 lower->height = scroll_arrow_height;
2886 g_assert_not_reached();
2887 upper->x = upper->y = upper->width = upper->height = 0;
2888 lower->x = lower->y = lower->width = lower->height = 0;
2891 *arrow_space = scroll_arrow_height - 2 * style->ythickness;
2895 gtk_menu_draw (GtkWidget *widget,
2899 GtkMenuPrivate *priv;
2901 GdkRectangle border;
2907 menu = GTK_MENU (widget);
2908 priv = gtk_menu_get_private (menu);
2910 style = gtk_widget_get_style (widget);
2911 window = gtk_widget_get_window (widget);
2913 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2915 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
2917 gfloat arrow_scaling;
2920 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
2921 arrow_size = arrow_scaling * arrow_space;
2923 gtk_paint_box (style,
2929 gtk_widget_get_allocated_width (widget),
2930 gtk_widget_get_allocated_height (widget));
2932 if (menu->upper_arrow_visible && !menu->tearoff_active)
2934 gtk_paint_box (style,
2936 priv->upper_arrow_state,
2938 widget, "menu_scroll_arrow_up",
2944 gtk_paint_arrow (style,
2946 priv->upper_arrow_state,
2948 widget, "menu_scroll_arrow_up",
2951 upper.x + (upper.width - arrow_size) / 2,
2952 upper.y + style->ythickness + (arrow_space - arrow_size) / 2,
2953 arrow_size, arrow_size);
2956 if (menu->lower_arrow_visible && !menu->tearoff_active)
2958 gtk_paint_box (style,
2960 priv->lower_arrow_state,
2962 widget, "menu_scroll_arrow_down",
2968 gtk_paint_arrow (style,
2970 priv->lower_arrow_state,
2972 widget, "menu_scroll_arrow_down",
2975 lower.x + (lower.width - arrow_size) / 2,
2976 lower.y + style->ythickness + (arrow_space - arrow_size) / 2,
2977 arrow_size, arrow_size);
2981 if (gtk_cairo_should_draw_window (cr, menu->bin_window))
2983 gint y = -border.y + menu->scroll_offset;
2986 gtk_cairo_transform_to_window (cr, widget, menu->bin_window);
2988 if (!menu->tearoff_active)
2990 GtkBorder arrow_border;
2992 get_arrows_border (menu, &arrow_border);
2993 y -= arrow_border.top;
2996 gtk_paint_box (style,
3002 border.width, border.height);
3007 GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
3013 gtk_menu_show (GtkWidget *widget)
3015 GtkMenu *menu = GTK_MENU (widget);
3017 _gtk_menu_refresh_accel_paths (menu, FALSE);
3019 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3024 gtk_menu_get_preferred_width (GtkWidget *widget,
3029 GtkMenuShell *menu_shell;
3030 GtkMenuPrivate *priv;
3034 guint max_toggle_size;
3035 guint max_accel_width;
3036 guint horizontal_padding;
3038 gint child_min, child_nat;
3039 gint min_width, nat_width;
3041 menu = GTK_MENU (widget);
3042 menu_shell = GTK_MENU_SHELL (widget);
3043 priv = gtk_menu_get_private (menu);
3045 style = gtk_widget_get_style (GTK_WIDGET (widget));
3047 min_width = nat_width = 0;
3049 max_toggle_size = 0;
3050 max_accel_width = 0;
3052 children = menu_shell->children;
3059 child = children->data;
3060 children = children->next;
3062 if (! gtk_widget_get_visible (child))
3065 get_effective_child_attach (child, &l, &r, &t, &b);
3067 /* It's important to size_request the child
3068 * before doing the toggle size request, in
3069 * case the toggle size request depends on the size
3070 * request of a child of the child (e.g. for ImageMenuItem)
3073 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
3074 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
3076 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3077 max_toggle_size = MAX (max_toggle_size, toggle_size);
3078 max_accel_width = MAX (max_accel_width,
3079 GTK_MENU_ITEM (child)->accelerator_width);
3081 part = child_min / (r - l);
3082 min_width = MAX (min_width, part);
3084 part = child_nat / (r - l);
3085 nat_width = MAX (nat_width, part);
3088 /* If the menu doesn't include any images or check items
3089 * reserve the space so that all menus are consistent.
3090 * We only do this for 'ordinary' menus, not for combobox
3091 * menus or multi-column menus
3093 if (max_toggle_size == 0 &&
3094 gtk_menu_get_n_columns (menu) == 1 &&
3095 !priv->no_toggle_size)
3097 guint toggle_spacing;
3098 guint indicator_size;
3100 gtk_style_get (style,
3101 GTK_TYPE_CHECK_MENU_ITEM,
3102 "toggle-spacing", &toggle_spacing,
3103 "indicator-size", &indicator_size,
3106 max_toggle_size = indicator_size + toggle_spacing;
3109 min_width += 2 * max_toggle_size + max_accel_width;
3110 min_width *= gtk_menu_get_n_columns (menu);
3112 nat_width += 2 * max_toggle_size + max_accel_width;
3113 nat_width *= gtk_menu_get_n_columns (menu);
3115 gtk_widget_style_get (GTK_WIDGET (menu),
3116 "horizontal-padding", &horizontal_padding,
3119 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3120 min_width += (border_width + horizontal_padding + style->xthickness) * 2;
3121 nat_width += (border_width + horizontal_padding + style->xthickness) * 2;
3123 menu->toggle_size = max_toggle_size;
3124 priv->accel_size = max_accel_width;
3127 *minimum_size = min_width;
3130 *natural_size = nat_width;
3132 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
3134 if (menu->tearoff_active)
3135 gtk_menu_set_tearoff_hints (menu, min_width);
3139 gtk_menu_get_preferred_height (GtkWidget *widget,
3145 /* Menus are height-for-width only, just return the height for the minimum width */
3146 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
3147 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
3151 gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
3156 GtkMenu *menu = GTK_MENU (widget);
3157 GtkMenuPrivate *private = gtk_menu_get_private (menu);
3158 guint *min_heights, *nat_heights;
3159 guint vertical_padding, border_width;
3161 gint min_height, nat_height;
3163 gtk_widget_style_get (GTK_WIDGET (menu), "vertical-padding", &vertical_padding, NULL);
3164 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3166 min_height = nat_height = (border_width + vertical_padding + gtk_widget_get_style (GTK_WIDGET (widget))->ythickness) * 2;
3169 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3171 for (i = 0; i < n_heights; i++)
3173 min_height += min_heights[i];
3174 nat_height += nat_heights[i];
3177 if (private->have_position)
3179 GdkScreen *screen = gtk_widget_get_screen (menu->toplevel);
3180 GdkRectangle monitor;
3182 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3184 if (private->y + min_height > monitor.y + monitor.height)
3185 min_height = monitor.y + monitor.height - private->y;
3187 if (private->y + nat_height > monitor.y + monitor.height)
3188 nat_height = monitor.y + monitor.height - private->y;
3190 if (private->y < monitor.y)
3192 min_height -= monitor.y - private->y;
3193 nat_height -= monitor.y - private->y;
3198 *minimum_size = min_height;
3201 *natural_size = nat_height;
3203 g_free (min_heights);
3204 g_free (nat_heights);
3210 gtk_menu_button_scroll (GtkMenu *menu,
3211 GdkEventButton *event)
3213 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
3215 gboolean touchscreen_mode;
3217 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3218 "gtk-touchscreen-mode", &touchscreen_mode,
3221 if (touchscreen_mode)
3222 gtk_menu_handle_scrolling (menu,
3223 event->x_root, event->y_root,
3224 event->type == GDK_BUTTON_PRESS,
3234 pointer_in_menu_window (GtkWidget *widget,
3238 GtkAllocation allocation;
3239 GtkMenu *menu = GTK_MENU (widget);
3241 if (gtk_widget_get_mapped (menu->toplevel))
3243 GtkMenuShell *menu_shell;
3244 gint window_x, window_y;
3246 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3247 &window_x, &window_y);
3249 gtk_widget_get_allocation (widget, &allocation);
3250 if (x_root >= window_x && x_root < window_x + allocation.width &&
3251 y_root >= window_y && y_root < window_y + allocation.height)
3254 menu_shell = GTK_MENU_SHELL (widget);
3256 if (GTK_IS_MENU (menu_shell->parent_menu_shell))
3257 return pointer_in_menu_window (menu_shell->parent_menu_shell,
3265 gtk_menu_button_press (GtkWidget *widget,
3266 GdkEventButton *event)
3268 if (event->type != GDK_BUTTON_PRESS)
3271 /* Don't pass down to menu shell for presses over scroll arrows
3273 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3276 /* Don't pass down to menu shell if a non-menuitem part of the menu
3277 * was clicked. The check for the event_widget being a GtkMenuShell
3278 * works because we have the pointer grabbed on menu_shell->window
3279 * with owner_events=TRUE, so all events that are either outside
3280 * the menu or on its border are delivered relative to
3281 * menu_shell->window.
3283 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3284 pointer_in_menu_window (widget, event->x_root, event->y_root))
3287 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3291 gtk_menu_button_release (GtkWidget *widget,
3292 GdkEventButton *event)
3294 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3296 if (priv->ignore_button_release)
3298 priv->ignore_button_release = FALSE;
3302 if (event->type != GDK_BUTTON_RELEASE)
3305 /* Don't pass down to menu shell for releases over scroll arrows
3307 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3310 /* Don't pass down to menu shell if a non-menuitem part of the menu
3311 * was clicked (see comment in button_press()).
3313 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3314 pointer_in_menu_window (widget, event->x_root, event->y_root))
3316 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3317 * bail out early here so it is in a consistent state for the
3318 * next button_press/button_release in GtkMenuShell.
3321 if (GTK_MENU_SHELL (widget)->active)
3322 GTK_MENU_SHELL (widget)->button = 0;
3327 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3330 static const gchar *
3331 get_accel_path (GtkWidget *menu_item,
3336 GClosure *accel_closure;
3337 GtkAccelGroup *accel_group;
3339 path = _gtk_widget_get_accel_path (menu_item, locked);
3342 path = GTK_MENU_ITEM (menu_item)->accel_path;
3348 label = gtk_bin_get_child (GTK_BIN (menu_item));
3350 if (GTK_IS_ACCEL_LABEL (label))
3352 g_object_get (label,
3353 "accel-closure", &accel_closure,
3357 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3359 *locked = gtk_accel_group_get_is_locked (accel_group);
3369 gtk_menu_key_press (GtkWidget *widget,
3372 GtkMenuShell *menu_shell;
3374 gboolean delete = FALSE;
3375 gboolean can_change_accels;
3376 gchar *accel = NULL;
3377 guint accel_key, accel_mods;
3378 GdkModifierType consumed_modifiers;
3379 GdkDisplay *display;
3381 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3382 g_return_val_if_fail (event != NULL, FALSE);
3384 menu_shell = GTK_MENU_SHELL (widget);
3385 menu = GTK_MENU (widget);
3387 gtk_menu_stop_navigating_submenu (menu);
3389 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3392 display = gtk_widget_get_display (widget);
3394 g_object_get (gtk_widget_get_settings (widget),
3395 "gtk-menu-bar-accel", &accel,
3396 "gtk-can-change-accels", &can_change_accels,
3399 if (accel && *accel)
3402 GdkModifierType mods = 0;
3404 gtk_accelerator_parse (accel, &keyval, &mods);
3407 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3409 /* FIXME this is wrong, needs to be in the global accel resolution
3410 * thing, to properly consider i18n etc., but that probably requires
3411 * AccelGroup changes etc.
3413 if (event->keyval == keyval && (mods & event->state) == mods)
3415 gtk_menu_shell_cancel (menu_shell);
3423 switch (event->keyval)
3425 case GDK_KEY_Delete:
3426 case GDK_KEY_KP_Delete:
3427 case GDK_KEY_BackSpace:
3434 /* Figure out what modifiers went into determining the key symbol */
3435 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3436 event->hardware_keycode, event->state, event->group,
3437 NULL, NULL, NULL, &consumed_modifiers);
3439 accel_key = gdk_keyval_to_lower (event->keyval);
3440 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3442 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
3443 * We re-upper case when we match against the keyval, but display and save in caseless form.
3445 if (accel_key != event->keyval)
3446 accel_mods |= GDK_SHIFT_MASK;
3448 /* Modify the accelerators */
3449 if (can_change_accels &&
3450 menu_shell->active_menu_item &&
3451 gtk_bin_get_child (GTK_BIN (menu_shell->active_menu_item)) && /* no separators */
3452 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
3453 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3455 GtkWidget *menu_item = menu_shell->active_menu_item;
3456 gboolean locked, replace_accels = TRUE;
3459 path = get_accel_path (menu_item, &locked);
3460 if (!path || locked)
3462 /* can't change accelerators on menu_items without paths
3463 * (basically, those items are accelerator-locked).
3465 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
3466 gtk_widget_error_bell (widget);
3472 /* For the keys that act to delete the current setting, we delete
3473 * the current setting if there is one, otherwise, we set the
3474 * key as the accelerator.
3480 if (gtk_accel_map_lookup_entry (path, &key) &&
3481 (key.accel_key || key.accel_mods))
3487 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3491 /* we failed, probably because this key is in use and
3494 /* g_print("failed to change\n"); */
3495 gtk_widget_error_bell (widget);
3504 check_threshold (GtkWidget *widget,
3513 ABS (start_x - x) > THRESHOLD ||
3514 ABS (start_y - y) > THRESHOLD;
3518 definitely_within_item (GtkWidget *widget,
3522 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
3525 w = gdk_window_get_width (window);
3526 h = gdk_window_get_height (window);
3529 check_threshold (widget, 0, 0, x, y) &&
3530 check_threshold (widget, w - 1, 0, x, y) &&
3531 check_threshold (widget, w - 1, h - 1, x, y) &&
3532 check_threshold (widget, 0, h - 1, x, y);
3536 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3538 GtkMenuPrivate *priv;
3540 priv = gtk_menu_get_private (menu);
3542 return priv->navigation_height && priv->navigation_width;
3546 gtk_menu_motion_notify (GtkWidget *widget,
3547 GdkEventMotion *event)
3549 GtkWidget *menu_item;
3551 GtkMenuShell *menu_shell;
3554 gboolean need_enter;
3556 if (GTK_IS_MENU (widget))
3558 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3560 if (priv->ignore_button_release)
3561 priv->ignore_button_release = FALSE;
3563 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3567 /* We received the event for one of two reasons:
3569 * a) We are the active menu, and did gtk_grab_add()
3570 * b) The widget is a child of ours, and the event was propagated
3572 * Since for computation of navigation regions, we want the menu which
3573 * is the parent of the menu item, for a), we need to find that menu,
3574 * which may be different from 'widget'.
3576 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3577 parent = gtk_widget_get_parent (menu_item);
3578 if (!GTK_IS_MENU_ITEM (menu_item) ||
3579 !GTK_IS_MENU (parent))
3582 menu_shell = GTK_MENU_SHELL (parent);
3583 menu = GTK_MENU (menu_shell);
3585 if (definitely_within_item (menu_item, event->x, event->y))
3586 menu_shell->activate_time = 0;
3588 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->ignore_enter);
3590 /* Check to see if we are within an active submenu's navigation region
3592 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3595 /* Make sure we pop down if we enter a non-selectable menu item, so we
3596 * don't show a submenu when the cursor is outside the stay-up triangle.
3598 if (!_gtk_menu_item_is_selectable (menu_item))
3600 /* We really want to deselect, but this gives the menushell code
3601 * a chance to do some bookkeeping about the menuitem.
3603 gtk_menu_shell_select_item (menu_shell, menu_item);
3609 /* The menu is now sensitive to enter events on its items, but
3610 * was previously sensitive. So we fake an enter event.
3612 menu_shell->ignore_enter = FALSE;
3614 if (event->x >= 0 && event->x < gdk_window_get_width (event->window) &&
3615 event->y >= 0 && event->y < gdk_window_get_height (event->window))
3617 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3620 send_event->crossing.window = g_object_ref (event->window);
3621 send_event->crossing.time = event->time;
3622 send_event->crossing.send_event = TRUE;
3623 send_event->crossing.x_root = event->x_root;
3624 send_event->crossing.y_root = event->y_root;
3625 send_event->crossing.x = event->x;
3626 send_event->crossing.y = event->y;
3627 send_event->crossing.state = event->state;
3628 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3630 /* We send the event to 'widget', the currently active menu,
3631 * instead of 'menu', the menu that the pointer is in. This
3632 * will ensure that the event will be ignored unless the
3633 * menuitem is a child of the active menu or some parent
3634 * menu of the active menu.
3636 result = gtk_widget_event (widget, send_event);
3637 gdk_event_free (send_event);
3647 get_double_arrows (GtkMenu *menu)
3649 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3650 gboolean double_arrows;
3651 GtkArrowPlacement arrow_placement;
3653 gtk_widget_style_get (GTK_WIDGET (menu),
3654 "double-arrows", &double_arrows,
3655 "arrow-placement", &arrow_placement,
3658 if (arrow_placement != GTK_ARROWS_BOTH)
3661 return double_arrows || (priv->initially_pushed_in &&
3662 menu->scroll_offset != 0);
3666 gtk_menu_scroll_by (GtkMenu *menu,
3669 GtkMenuPrivate *priv;
3670 GtkBorder arrow_border;
3674 gboolean double_arrows;
3676 widget = GTK_WIDGET (menu);
3677 offset = menu->scroll_offset + step;
3678 priv = gtk_menu_get_private (menu);
3680 get_arrows_border (menu, &arrow_border);
3682 double_arrows = get_double_arrows (menu);
3684 /* If we scroll upward and the non-visible top part
3685 * is smaller than the scroll arrow it would be
3686 * pretty stupid to show the arrow and taking more
3687 * screen space than just scrolling to the top.
3690 if ((step < 0) && (offset < arrow_border.top))
3693 /* Don't scroll over the top if we weren't before: */
3694 if ((menu->scroll_offset >= 0) && (offset < 0))
3697 view_height = gdk_window_get_height (gtk_widget_get_window (widget));
3699 if (menu->scroll_offset == 0 &&
3700 view_height >= priv->requested_height)
3703 /* Don't scroll past the bottom if we weren't before: */
3704 if (menu->scroll_offset > 0)
3705 view_height -= arrow_border.top;
3707 /* When both arrows are always shown, reduce
3708 * view height even more.
3711 view_height -= arrow_border.bottom;
3713 if ((menu->scroll_offset + view_height <= priv->requested_height) &&
3714 (offset + view_height > priv->requested_height))
3715 offset = priv->requested_height - view_height;
3717 if (offset != menu->scroll_offset)
3718 gtk_menu_scroll_to (menu, offset);
3722 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3723 gboolean touchscreen_mode)
3725 gboolean upper_visible;
3726 gboolean lower_visible;
3728 upper_visible = menu->upper_arrow_visible;
3729 lower_visible = menu->lower_arrow_visible;
3731 gtk_menu_scroll_by (menu, menu->scroll_step);
3733 if (touchscreen_mode &&
3734 (upper_visible != menu->upper_arrow_visible ||
3735 lower_visible != menu->lower_arrow_visible))
3737 /* We are about to hide a scroll arrow while the mouse is pressed,
3738 * this would cause the uncovered menu item to be activated on button
3739 * release. Therefore we need to ignore button release here
3741 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3742 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3747 gtk_menu_scroll_timeout (gpointer data)
3750 gboolean touchscreen_mode;
3752 menu = GTK_MENU (data);
3754 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3755 "gtk-touchscreen-mode", &touchscreen_mode,
3758 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3764 gtk_menu_scroll_timeout_initial (gpointer data)
3768 gboolean touchscreen_mode;
3770 menu = GTK_MENU (data);
3772 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3773 "gtk-timeout-repeat", &timeout,
3774 "gtk-touchscreen-mode", &touchscreen_mode,
3777 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3779 gtk_menu_remove_scroll_timeout (menu);
3781 menu->timeout_id = gdk_threads_add_timeout (timeout,
3782 gtk_menu_scroll_timeout,
3789 gtk_menu_start_scrolling (GtkMenu *menu)
3792 gboolean touchscreen_mode;
3794 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3795 "gtk-timeout-repeat", &timeout,
3796 "gtk-touchscreen-mode", &touchscreen_mode,
3799 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3801 menu->timeout_id = gdk_threads_add_timeout (timeout,
3802 gtk_menu_scroll_timeout_initial,
3807 gtk_menu_scroll (GtkWidget *widget,
3808 GdkEventScroll *event)
3810 GtkMenu *menu = GTK_MENU (widget);
3812 switch (event->direction)
3814 case GDK_SCROLL_RIGHT:
3815 case GDK_SCROLL_DOWN:
3816 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3818 case GDK_SCROLL_LEFT:
3820 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3828 get_arrows_sensitive_area (GtkMenu *menu,
3829 GdkRectangle *upper,
3830 GdkRectangle *lower)
3832 GtkArrowPlacement arrow_placement;
3833 GtkWidget *widget = GTK_WIDGET (menu);
3837 guint vertical_padding;
3839 gint scroll_arrow_height;
3841 window = gtk_widget_get_window (widget);
3842 width = gdk_window_get_width (window);
3843 height = gdk_window_get_height (window);
3845 gtk_widget_style_get (widget,
3846 "vertical-padding", &vertical_padding,
3847 "scroll-arrow-vlength", &scroll_arrow_height,
3848 "arrow-placement", &arrow_placement,
3851 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) +
3852 gtk_widget_get_style (widget)->ythickness + vertical_padding;
3854 gdk_window_get_position (window, &win_x, &win_y);
3856 switch (arrow_placement)
3858 case GTK_ARROWS_BOTH:
3863 upper->width = width;
3864 upper->height = scroll_arrow_height + border;
3870 lower->y = win_y + height - border - scroll_arrow_height;
3871 lower->width = width;
3872 lower->height = scroll_arrow_height + border;
3876 case GTK_ARROWS_START:
3881 upper->width = width / 2;
3882 upper->height = scroll_arrow_height + border;
3887 lower->x = win_x + width / 2;
3889 lower->width = width / 2;
3890 lower->height = scroll_arrow_height + border;
3894 case GTK_ARROWS_END:
3898 upper->y = win_y + height - border - scroll_arrow_height;
3899 upper->width = width / 2;
3900 upper->height = scroll_arrow_height + border;
3905 lower->x = win_x + width / 2;
3906 lower->y = win_y + height - border - scroll_arrow_height;
3907 lower->width = width / 2;
3908 lower->height = scroll_arrow_height + border;
3916 gtk_menu_handle_scrolling (GtkMenu *menu,
3922 GtkMenuShell *menu_shell;
3923 GtkMenuPrivate *priv;
3926 gboolean scroll_fast = FALSE;
3928 gboolean touchscreen_mode;
3930 priv = gtk_menu_get_private (menu);
3932 menu_shell = GTK_MENU_SHELL (menu);
3934 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3935 "gtk-touchscreen-mode", &touchscreen_mode,
3938 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3943 /* upper arrow handling */
3945 get_arrows_sensitive_area (menu, &rect, NULL);
3948 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3949 (x >= rect.x) && (x < rect.x + rect.width) &&
3950 (y >= rect.y) && (y < rect.y + rect.height))
3955 if (touchscreen_mode)
3956 menu->upper_arrow_prelight = in_arrow;
3958 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3960 gboolean arrow_pressed = FALSE;
3962 if (menu->upper_arrow_visible && !menu->tearoff_active)
3964 if (touchscreen_mode)
3966 if (enter && menu->upper_arrow_prelight)
3968 if (menu->timeout_id == 0)
3970 /* Deselect the active item so that
3971 * any submenus are popped down
3973 gtk_menu_shell_deselect (menu_shell);
3975 gtk_menu_remove_scroll_timeout (menu);
3976 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3980 /* Only do stuff on click. */
3981 gtk_menu_start_scrolling (menu);
3982 arrow_pressed = TRUE;
3987 arrow_pressed = TRUE;
3992 gtk_menu_stop_scrolling (menu);
3995 else /* !touchscreen_mode */
3997 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3999 if (enter && in_arrow &&
4000 (!menu->upper_arrow_prelight ||
4001 menu->scroll_fast != scroll_fast))
4003 menu->upper_arrow_prelight = TRUE;
4004 menu->scroll_fast = scroll_fast;
4006 /* Deselect the active item so that
4007 * any submenus are popped down
4009 gtk_menu_shell_deselect (menu_shell);
4011 gtk_menu_remove_scroll_timeout (menu);
4012 menu->scroll_step = scroll_fast ?
4013 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
4016 gdk_threads_add_timeout (scroll_fast ?
4017 MENU_SCROLL_TIMEOUT2 :
4018 MENU_SCROLL_TIMEOUT1,
4019 gtk_menu_scroll_timeout, menu);
4021 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
4023 gtk_menu_stop_scrolling (menu);
4028 /* gtk_menu_start_scrolling() might have hit the top of the
4029 * menu, so check if the button isn't insensitive before
4030 * changing it to something else.
4032 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
4034 GtkStateType arrow_state = GTK_STATE_NORMAL;
4037 arrow_state = GTK_STATE_ACTIVE;
4038 else if (menu->upper_arrow_prelight)
4039 arrow_state = GTK_STATE_PRELIGHT;
4041 if (arrow_state != priv->upper_arrow_state)
4043 priv->upper_arrow_state = arrow_state;
4045 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4051 /* lower arrow handling */
4053 get_arrows_sensitive_area (menu, NULL, &rect);
4056 if (menu->lower_arrow_visible && !menu->tearoff_active &&
4057 (x >= rect.x) && (x < rect.x + rect.width) &&
4058 (y >= rect.y) && (y < rect.y + rect.height))
4063 if (touchscreen_mode)
4064 menu->lower_arrow_prelight = in_arrow;
4066 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4068 gboolean arrow_pressed = FALSE;
4070 if (menu->lower_arrow_visible && !menu->tearoff_active)
4072 if (touchscreen_mode)
4074 if (enter && menu->lower_arrow_prelight)
4076 if (menu->timeout_id == 0)
4078 /* Deselect the active item so that
4079 * any submenus are popped down
4081 gtk_menu_shell_deselect (menu_shell);
4083 gtk_menu_remove_scroll_timeout (menu);
4084 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4088 /* Only do stuff on click. */
4089 gtk_menu_start_scrolling (menu);
4090 arrow_pressed = TRUE;
4095 arrow_pressed = TRUE;
4100 gtk_menu_stop_scrolling (menu);
4103 else /* !touchscreen_mode */
4105 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4107 if (enter && in_arrow &&
4108 (!menu->lower_arrow_prelight ||
4109 menu->scroll_fast != scroll_fast))
4111 menu->lower_arrow_prelight = TRUE;
4112 menu->scroll_fast = scroll_fast;
4114 /* Deselect the active item so that
4115 * any submenus are popped down
4117 gtk_menu_shell_deselect (menu_shell);
4119 gtk_menu_remove_scroll_timeout (menu);
4120 menu->scroll_step = scroll_fast ?
4121 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
4124 gdk_threads_add_timeout (scroll_fast ?
4125 MENU_SCROLL_TIMEOUT2 :
4126 MENU_SCROLL_TIMEOUT1,
4127 gtk_menu_scroll_timeout, menu);
4129 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
4131 gtk_menu_stop_scrolling (menu);
4136 /* gtk_menu_start_scrolling() might have hit the bottom of the
4137 * menu, so check if the button isn't insensitive before
4138 * changing it to something else.
4140 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4142 GtkStateType arrow_state = GTK_STATE_NORMAL;
4145 arrow_state = GTK_STATE_ACTIVE;
4146 else if (menu->lower_arrow_prelight)
4147 arrow_state = GTK_STATE_PRELIGHT;
4149 if (arrow_state != priv->lower_arrow_state)
4151 priv->lower_arrow_state = arrow_state;
4153 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4161 gtk_menu_enter_notify (GtkWidget *widget,
4162 GdkEventCrossing *event)
4164 GtkWidget *menu_item;
4166 gboolean touchscreen_mode;
4168 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4169 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4170 event->mode == GDK_CROSSING_STATE_CHANGED)
4173 g_object_get (gtk_widget_get_settings (widget),
4174 "gtk-touchscreen-mode", &touchscreen_mode,
4177 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4178 if (GTK_IS_MENU (widget))
4180 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4182 if (!menu_shell->ignore_enter)
4183 gtk_menu_handle_scrolling (GTK_MENU (widget),
4184 event->x_root, event->y_root, TRUE, TRUE);
4187 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4189 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4191 if (GTK_IS_MENU (menu))
4193 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
4194 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4196 if (priv->seen_item_enter)
4198 /* This is the second enter we see for an item
4199 * on this menu. This means a release should always
4202 menu_shell->activate_time = 0;
4204 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4205 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4207 if (definitely_within_item (menu_item, event->x, event->y))
4209 /* This is an actual user-enter (ie. not a pop-under)
4210 * In this case, the user must either have entered
4211 * sufficiently far enough into the item, or he must move
4212 * far enough away from the enter point. (see
4213 * gtk_menu_motion_notify())
4215 menu_shell->activate_time = 0;
4219 priv->seen_item_enter = TRUE;
4223 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4224 * will not correspond to the event widget's parent. Check to see
4225 * if we are in the parent's navigation region.
4227 parent = gtk_widget_get_parent (menu_item);
4228 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4229 gtk_menu_navigating_submenu (GTK_MENU (parent),
4230 event->x_root, event->y_root))
4233 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4237 gtk_menu_leave_notify (GtkWidget *widget,
4238 GdkEventCrossing *event)
4240 GtkMenuShell *menu_shell;
4242 GtkMenuItem *menu_item;
4243 GtkWidget *event_widget;
4245 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4246 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4247 event->mode == GDK_CROSSING_STATE_CHANGED)
4250 menu = GTK_MENU (widget);
4251 menu_shell = GTK_MENU_SHELL (widget);
4253 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4256 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4258 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4260 if (!GTK_IS_MENU_ITEM (event_widget))
4263 menu_item = GTK_MENU_ITEM (event_widget);
4265 /* Here we check to see if we're leaving an active menu item with a submenu,
4266 * in which case we enter submenu navigation mode.
4268 if (menu_shell->active_menu_item != NULL
4269 && menu_item->submenu != NULL
4270 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
4272 if (GTK_MENU_SHELL (menu_item->submenu)->active)
4274 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4277 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
4279 /* We are leaving an active menu item with nonactive submenu.
4280 * Deselect it so we don't surprise the user with by popping
4281 * up a submenu _after_ he left the item.
4283 gtk_menu_shell_deselect (menu_shell);
4288 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4292 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4294 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4296 priv->navigation_x = 0;
4297 priv->navigation_y = 0;
4298 priv->navigation_width = 0;
4299 priv->navigation_height = 0;
4301 if (menu->navigation_timeout)
4303 g_source_remove (menu->navigation_timeout);
4304 menu->navigation_timeout = 0;
4308 /* When the timeout is elapsed, the navigation region is destroyed
4309 * and the menuitem under the pointer (if any) is selected.
4312 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4314 GtkMenuPopdownData *popdown_data = user_data;
4315 GtkMenu *menu = popdown_data->menu;
4316 GdkWindow *child_window;
4318 gtk_menu_stop_navigating_submenu (menu);
4320 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4322 child_window = gdk_window_get_device_position (menu->bin_window,
4323 popdown_data->device,
4328 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4330 send_event->crossing.window = g_object_ref (child_window);
4331 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4332 send_event->crossing.send_event = TRUE;
4333 gdk_event_set_device (send_event, popdown_data->device);
4335 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4337 gdk_event_free (send_event);
4345 gtk_menu_navigating_submenu (GtkMenu *menu,
4349 GtkMenuPrivate *priv;
4352 if (!gtk_menu_has_navigation_triangle (menu))
4355 priv = gtk_menu_get_private (menu);
4356 width = priv->navigation_width;
4357 height = priv->navigation_height;
4359 /* check if x/y are in the triangle spanned by the navigation parameters */
4361 /* 1) Move the coordinates so the triangle starts at 0,0 */
4362 event_x -= priv->navigation_x;
4363 event_y -= priv->navigation_y;
4365 /* 2) Ensure both legs move along the positive axis */
4377 /* 3) Check that the given coordinate is inside the triangle. The formula
4378 * is a transformed form of this formula: x/w + y/h <= 1
4380 if (event_x >= 0 && event_y >= 0 &&
4381 event_x * height + event_y * width <= width * height)
4387 gtk_menu_stop_navigating_submenu (menu);
4393 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4394 GtkMenuItem *menu_item,
4395 GdkEventCrossing *event)
4397 gint submenu_left = 0;
4398 gint submenu_right = 0;
4399 gint submenu_top = 0;
4400 gint submenu_bottom = 0;
4403 GtkWidget *event_widget;
4404 GtkMenuPopdownData *popdown_data;
4405 GtkMenuPrivate *priv;
4408 g_return_if_fail (menu_item->submenu != NULL);
4409 g_return_if_fail (event != NULL);
4411 priv = gtk_menu_get_private (menu);
4413 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4415 window = gtk_widget_get_window (menu_item->submenu);
4416 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4418 submenu_right = submenu_left + gdk_window_get_width (window);
4419 submenu_bottom = submenu_top + gdk_window_get_height (window);
4421 width = gdk_window_get_width (gtk_widget_get_window (event_widget));
4422 height = gdk_window_get_height (gtk_widget_get_window (event_widget));
4424 if (event->x >= 0 && event->x < width)
4428 gtk_menu_stop_navigating_submenu (menu);
4430 /* The navigation region is the triangle closest to the x/y
4431 * location of the rectangle. This is why the width or height
4435 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
4438 priv->navigation_x = submenu_left;
4439 priv->navigation_width = event->x_root - submenu_left;
4444 priv->navigation_x = submenu_right;
4445 priv->navigation_width = event->x_root - submenu_right;
4451 priv->navigation_y = event->y_root;
4452 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4454 if (priv->navigation_height >= 0)
4460 priv->navigation_y = event->y_root;
4461 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4463 if (priv->navigation_height <= 0)
4467 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4468 "gtk-menu-popdown-delay", &popdown_delay,
4471 popdown_data = g_new (GtkMenuPopdownData, 1);
4472 popdown_data->menu = menu;
4473 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4475 menu->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4477 gtk_menu_stop_navigating_submenu_cb,
4479 (GDestroyNotify) g_free);
4484 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4488 g_return_if_fail (GTK_IS_MENU (menu_shell));
4490 parent = menu_shell->parent_menu_shell;
4492 menu_shell->activate_time = 0;
4493 gtk_menu_popdown (GTK_MENU (menu_shell));
4496 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4500 gtk_menu_position (GtkMenu *menu)
4503 GtkRequisition requisition;
4504 GtkMenuPrivate *private;
4509 GdkScreen *pointer_screen;
4510 GdkRectangle monitor;
4513 g_return_if_fail (GTK_IS_MENU (menu));
4515 widget = GTK_WIDGET (menu);
4517 screen = gtk_widget_get_screen (widget);
4518 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4519 gdk_display_get_device_state (gdk_screen_get_display (screen),
4520 pointer, &pointer_screen, &x, &y, NULL);
4522 /* Get the minimum height for minimum width to figure out
4523 * the right place to popup the menu.
4525 gtk_widget_get_preferred_size (widget, &requisition, NULL);
4527 if (pointer_screen != screen)
4529 /* Pointer is on a different screen; roughly center the
4530 * menu on the screen. If someone was using multiscreen
4531 * + Xinerama together they'd probably want something
4532 * fancier; but that is likely to be vanishingly rare.
4534 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4535 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4538 private = gtk_menu_get_private (menu);
4539 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4541 private->initially_pushed_in = FALSE;
4543 /* Set the type hint here to allow custom position functions to set a different hint */
4544 if (!gtk_widget_get_visible (menu->toplevel))
4545 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4547 if (menu->position_func)
4549 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
4550 menu->position_func_data);
4552 if (private->monitor_num < 0)
4553 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4555 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4559 GtkStyle *style = gtk_widget_get_style (widget);
4560 gint space_left, space_right, space_above, space_below;
4563 gint xthickness = style->xthickness;
4564 gint ythickness = style->ythickness;
4565 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4567 /* The placement of popup menus horizontally works like this (with
4568 * RTL in parentheses)
4570 * - If there is enough room to the right (left) of the mouse cursor,
4571 * position the menu there.
4573 * - Otherwise, if if there is enough room to the left (right) of the
4574 * mouse cursor, position the menu there.
4576 * - Otherwise if the menu is smaller than the monitor, position it
4577 * on the side of the mouse cursor that has the most space available
4579 * - Otherwise (if there is simply not enough room for the menu on the
4580 * monitor), position it as far left (right) as possible.
4582 * Positioning in the vertical direction is similar: first try below
4583 * mouse cursor, then above.
4585 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4587 space_left = x - monitor.x;
4588 space_right = monitor.x + monitor.width - x - 1;
4589 space_above = y - monitor.y;
4590 space_below = monitor.y + monitor.height - y - 1;
4592 /* position horizontally */
4594 /* the amount of space we need to position the menu. Note the
4595 * menu is offset "xthickness" pixels
4597 needed_width = requisition.width - xthickness;
4599 if (needed_width <= space_left ||
4600 needed_width <= space_right)
4602 if ((rtl && needed_width <= space_left) ||
4603 (!rtl && needed_width > space_right))
4606 x = x + xthickness - requisition.width + 1;
4610 /* position right */
4614 /* x is clamped on-screen further down */
4616 else if (requisition.width <= monitor.width)
4618 /* the menu is too big to fit on either side of the mouse
4619 * cursor, but smaller than the monitor. Position it on
4620 * the side that has the most space
4622 if (space_left > space_right)
4630 x = monitor.x + monitor.width - requisition.width;
4633 else /* menu is simply too big for the monitor */
4638 x = monitor.x + monitor.width - requisition.width;
4647 /* Position vertically. The algorithm is the same as above, but
4648 * simpler because we don't have to take RTL into account.
4650 needed_height = requisition.height - ythickness;
4652 if (needed_height <= space_above ||
4653 needed_height <= space_below)
4655 if (needed_height <= space_below)
4658 y = y + ythickness - requisition.height + 1;
4660 y = CLAMP (y, monitor.y,
4661 monitor.y + monitor.height - requisition.height);
4663 else if (needed_height > space_below && needed_height > space_above)
4665 if (space_below >= space_above)
4666 y = monitor.y + monitor.height - requisition.height;
4678 if (private->initially_pushed_in)
4680 menu_height = requisition.height;
4682 if (y + menu_height > monitor.y + monitor.height)
4684 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4685 y = (monitor.y + monitor.height) - menu_height;
4690 scroll_offset += monitor.y - y;
4695 /* FIXME: should this be done in the various position_funcs ? */
4696 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4698 if (GTK_MENU_SHELL (menu)->active)
4700 private->have_position = TRUE;
4705 if (y + requisition.height > monitor.y + monitor.height)
4706 requisition.height = (monitor.y + monitor.height) - y;
4710 scroll_offset += monitor.y - y;
4711 requisition.height -= monitor.y - y;
4715 if (scroll_offset > 0)
4717 GtkBorder arrow_border;
4719 get_arrows_border (menu, &arrow_border);
4720 scroll_offset += arrow_border.top;
4723 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
4726 if (!GTK_MENU_SHELL (menu)->active)
4728 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4729 requisition.width, requisition.height);
4732 menu->scroll_offset = scroll_offset;
4736 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4738 if (menu->timeout_id)
4740 g_source_remove (menu->timeout_id);
4741 menu->timeout_id = 0;
4746 gtk_menu_stop_scrolling (GtkMenu *menu)
4748 gboolean touchscreen_mode;
4750 gtk_menu_remove_scroll_timeout (menu);
4752 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4753 "gtk-touchscreen-mode", &touchscreen_mode,
4756 if (!touchscreen_mode)
4758 menu->upper_arrow_prelight = FALSE;
4759 menu->lower_arrow_prelight = FALSE;
4764 gtk_menu_scroll_to (GtkMenu *menu,
4767 GtkMenuPrivate *priv;
4768 GtkAllocation allocation;
4769 GtkBorder arrow_border;
4773 gint view_width, view_height;
4776 guint vertical_padding;
4777 guint horizontal_padding;
4778 gboolean double_arrows;
4780 widget = GTK_WIDGET (menu);
4781 priv = gtk_menu_get_private (menu);
4783 if (menu->tearoff_active &&
4784 menu->tearoff_adjustment &&
4785 (menu->tearoff_adjustment->value != offset))
4787 menu->tearoff_adjustment->value =
4789 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4790 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4793 /* Move/resize the viewport according to arrows: */
4794 gtk_widget_get_allocation (widget, &allocation);
4795 view_width = allocation.width;
4796 view_height = allocation.height;
4798 style = gtk_widget_get_style (widget);
4800 gtk_widget_style_get (GTK_WIDGET (menu),
4801 "vertical-padding", &vertical_padding,
4802 "horizontal-padding", &horizontal_padding,
4805 double_arrows = get_double_arrows (menu);
4807 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4809 view_width -= (border_width + style->xthickness + horizontal_padding) * 2;
4810 view_height -= (border_width + style->ythickness + vertical_padding) * 2;
4811 menu_height = priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2;
4813 x = border_width + style->xthickness + horizontal_padding;
4814 y = border_width + style->ythickness + vertical_padding;
4816 if (double_arrows && !menu->tearoff_active)
4818 if (view_height < menu_height ||
4819 (offset > 0 && menu->scroll_offset > 0) ||
4820 (offset < 0 && menu->scroll_offset < 0))
4822 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4823 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4824 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4826 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4827 gtk_widget_queue_draw (GTK_WIDGET (menu));
4829 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4831 get_arrows_border (menu, &arrow_border);
4832 y += arrow_border.top;
4833 view_height -= arrow_border.top;
4834 view_height -= arrow_border.bottom;
4837 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4838 else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4839 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4840 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4842 if (offset >= menu_height - view_height)
4843 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4844 else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4845 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4846 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4848 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4849 (priv->lower_arrow_state != lower_arrow_previous_state))
4850 gtk_widget_queue_draw (GTK_WIDGET (menu));
4852 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4853 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4855 /* At the upper border, possibly remove timeout */
4856 if (menu->scroll_step < 0)
4858 gtk_menu_stop_scrolling (menu);
4859 gtk_widget_queue_draw (GTK_WIDGET (menu));
4863 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4864 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4866 /* At the lower border, possibly remove timeout */
4867 if (menu->scroll_step > 0)
4869 gtk_menu_stop_scrolling (menu);
4870 gtk_widget_queue_draw (GTK_WIDGET (menu));
4874 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4878 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4879 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4881 gtk_menu_stop_scrolling (menu);
4882 gtk_widget_queue_draw (GTK_WIDGET (menu));
4885 else if (!menu->tearoff_active)
4887 gboolean last_visible;
4889 last_visible = menu->upper_arrow_visible;
4890 menu->upper_arrow_visible = offset > 0;
4892 /* upper_arrow_visible may have changed, so requery the border */
4893 get_arrows_border (menu, &arrow_border);
4894 view_height -= arrow_border.top;
4896 if ((last_visible != menu->upper_arrow_visible) &&
4897 !menu->upper_arrow_visible)
4899 menu->upper_arrow_prelight = FALSE;
4901 /* If we hid the upper arrow, possibly remove timeout */
4902 if (menu->scroll_step < 0)
4904 gtk_menu_stop_scrolling (menu);
4905 gtk_widget_queue_draw (GTK_WIDGET (menu));
4909 last_visible = menu->lower_arrow_visible;
4910 menu->lower_arrow_visible = offset < menu_height - view_height;
4912 /* lower_arrow_visible may have changed, so requery the border */
4913 get_arrows_border (menu, &arrow_border);
4914 view_height -= arrow_border.bottom;
4916 if ((last_visible != menu->lower_arrow_visible) &&
4917 !menu->lower_arrow_visible)
4919 menu->lower_arrow_prelight = FALSE;
4921 /* If we hid the lower arrow, possibly remove timeout */
4922 if (menu->scroll_step > 0)
4924 gtk_menu_stop_scrolling (menu);
4925 gtk_widget_queue_draw (GTK_WIDGET (menu));
4929 y += arrow_border.top;
4932 /* Scroll the menu: */
4933 if (gtk_widget_get_realized (widget))
4934 gdk_window_move (menu->bin_window, 0, -offset);
4936 if (gtk_widget_get_realized (widget))
4937 gdk_window_move_resize (menu->view_window,
4943 menu->scroll_offset = offset;
4947 compute_child_offset (GtkMenu *menu,
4948 GtkWidget *menu_item,
4951 gboolean *is_last_child)
4953 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4954 gint item_top_attach;
4955 gint item_bottom_attach;
4956 gint child_offset = 0;
4959 get_effective_child_attach (menu_item, NULL, NULL,
4960 &item_top_attach, &item_bottom_attach);
4962 /* there is a possibility that we get called before _size_request, so
4963 * check the height table for safety.
4965 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4968 /* when we have a row with only invisible children, it's height will
4969 * be zero, so there's no need to check WIDGET_VISIBLE here
4971 for (i = 0; i < item_top_attach; i++)
4972 child_offset += priv->heights[i];
4975 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4977 *offset = child_offset;
4979 *height = priv->heights[item_top_attach];
4985 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4986 GtkWidget *menu_item)
4988 GtkMenu *menu = GTK_MENU (menu_shell);
4989 GtkWidget *widget = GTK_WIDGET (menu_shell);
4990 gint child_offset, child_height;
4994 gboolean last_child = 0;
4996 /* We need to check if the selected item fully visible.
4997 * If not we need to scroll the menu so that it becomes fully
5001 if (compute_child_offset (menu, menu_item,
5002 &child_offset, &child_height, &last_child))
5004 guint vertical_padding;
5005 gboolean double_arrows;
5007 y = menu->scroll_offset;
5008 width = gdk_window_get_width (gtk_widget_get_window (widget));
5009 height = gdk_window_get_height (gtk_widget_get_window (widget));
5011 gtk_widget_style_get (widget,
5012 "vertical-padding", &vertical_padding,
5015 double_arrows = get_double_arrows (menu);
5017 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5018 2 * gtk_widget_get_style (widget)->ythickness +
5019 2 * vertical_padding;
5020 if (child_offset < y)
5022 /* Ignore the enter event we might get if the pointer is on the menu
5024 menu_shell->ignore_enter = TRUE;
5025 gtk_menu_scroll_to (menu, child_offset);
5029 GtkBorder arrow_border;
5033 get_arrows_border (menu, &arrow_border);
5034 if (!menu->tearoff_active)
5035 arrow_height = arrow_border.top + arrow_border.bottom;
5037 if (child_offset + child_height > y + height - arrow_height)
5040 if ((!last_child && !menu->tearoff_active) || double_arrows)
5041 arrow_height += arrow_border.bottom;
5043 y = child_offset + child_height - height + arrow_height;
5044 if (((y > 0) && !menu->tearoff_active) || double_arrows)
5046 /* Need upper arrow */
5047 arrow_height += arrow_border.top;
5048 y = child_offset + child_height - height + arrow_height;
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, y);
5061 gtk_menu_select_item (GtkMenuShell *menu_shell,
5062 GtkWidget *menu_item)
5064 GtkMenu *menu = GTK_MENU (menu_shell);
5066 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5067 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5069 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5073 /* Reparent the menu, taking care of the refcounting
5075 * If unrealize is true we force a unrealize while reparenting the parent.
5076 * This can help eliminate flicker in some cases.
5078 * What happens is that when the menu is unrealized and then re-realized,
5079 * the allocations are as follows:
5081 * parent - 1x1 at (0,0)
5082 * child1 - 100x20 at (0,0)
5083 * child2 - 100x20 at (0,20)
5084 * child3 - 100x20 at (0,40)
5086 * That is, the parent is small but the children are full sized. Then,
5087 * when the queued_resize gets processed, the parent gets resized to
5090 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5091 * contains the following logic:
5093 * - if a move or resize operation on a window would change the clip
5094 * region on the children, then before the window is resized
5095 * the background for children is temporarily set to None, the
5096 * move/resize done, and the background for the children restored.
5098 * So, at the point where the parent is resized to final size, the
5099 * background for the children is temporarily None, and thus they
5100 * are not cleared to the background color and the previous background
5101 * (the image of the menu) is left in place.
5104 gtk_menu_reparent (GtkMenu *menu,
5105 GtkWidget *new_parent,
5108 GObject *object = G_OBJECT (menu);
5109 GtkWidget *widget = GTK_WIDGET (menu);
5110 gboolean was_floating = g_object_is_floating (object);
5112 g_object_ref_sink (object);
5116 g_object_ref (object);
5117 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5118 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5119 g_object_unref (object);
5122 gtk_widget_reparent (widget, new_parent);
5125 g_object_force_floating (object);
5127 g_object_unref (object);
5131 gtk_menu_show_all (GtkWidget *widget)
5133 /* Show children, but not self. */
5134 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5138 * gtk_menu_set_screen:
5139 * @menu: a #GtkMenu.
5140 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5141 * determined by the widget the menu is attached to.
5143 * Sets the #GdkScreen on which the menu will be displayed.
5148 gtk_menu_set_screen (GtkMenu *menu,
5151 g_return_if_fail (GTK_IS_MENU (menu));
5152 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
5154 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5158 menu_change_screen (menu, screen);
5162 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5164 attach_widget_screen_changed (attach_widget, NULL, menu);
5170 * @menu: a #GtkMenu.
5171 * @child: a #GtkMenuItem.
5172 * @left_attach: The column number to attach the left side of the item to.
5173 * @right_attach: The column number to attach the right side of the item to.
5174 * @top_attach: The row number to attach the top of the item to.
5175 * @bottom_attach: The row number to attach the bottom of the item to.
5177 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5178 * an item will occupy is specified by @left_attach, @right_attach,
5179 * @top_attach and @bottom_attach. These each represent the leftmost,
5180 * rightmost, uppermost and lower column and row numbers of the table.
5181 * (Columns and rows are indexed from zero).
5183 * Note that this function is not related to gtk_menu_detach().
5188 gtk_menu_attach (GtkMenu *menu,
5193 guint bottom_attach)
5195 GtkMenuShell *menu_shell;
5198 g_return_if_fail (GTK_IS_MENU (menu));
5199 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5200 parent = gtk_widget_get_parent (child);
5201 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5202 g_return_if_fail (left_attach < right_attach);
5203 g_return_if_fail (top_attach < bottom_attach);
5205 menu_shell = GTK_MENU_SHELL (menu);
5209 AttachInfo *ai = get_attach_info (child);
5211 ai->left_attach = left_attach;
5212 ai->right_attach = right_attach;
5213 ai->top_attach = top_attach;
5214 ai->bottom_attach = bottom_attach;
5216 menu_shell->children = g_list_append (menu_shell->children, child);
5218 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5220 menu_queue_resize (menu);
5224 gtk_container_child_set (GTK_CONTAINER (parent), child,
5225 "left-attach", left_attach,
5226 "right-attach", right_attach,
5227 "top-attach", top_attach,
5228 "bottom-attach", bottom_attach,
5234 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5238 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5239 "gtk-menu-popup-delay", &popup_delay,
5246 find_child_containing (GtkMenuShell *menu_shell,
5254 /* find a child which includes the area given by
5255 * left, right, top, bottom.
5258 for (list = menu_shell->children; list; list = list->next)
5262 if (!_gtk_menu_item_is_selectable (list->data))
5265 get_effective_child_attach (list->data, &l, &r, &t, &b);
5267 if (l <= left && right <= r
5268 && t <= top && bottom <= b)
5269 return GTK_WIDGET (list->data);
5276 gtk_menu_move_current (GtkMenuShell *menu_shell,
5277 GtkMenuDirectionType direction)
5279 GtkMenu *menu = GTK_MENU (menu_shell);
5282 GtkWidget *match = NULL;
5284 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5288 case GTK_MENU_DIR_CHILD:
5289 direction = GTK_MENU_DIR_PARENT;
5291 case GTK_MENU_DIR_PARENT:
5292 direction = GTK_MENU_DIR_CHILD;
5298 /* use special table menu key bindings */
5299 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5301 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
5303 if (direction == GTK_MENU_DIR_NEXT)
5305 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5307 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5315 for (i = 0; i < t; i++)
5317 match = find_child_containing (menu_shell,
5318 l, l + 1, i, i + 1);
5324 else if (direction == GTK_MENU_DIR_PREV)
5326 for (i = t; i > 0; i--)
5328 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
5336 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5338 match = find_child_containing (menu_shell,
5339 l, l + 1, i - 1, i);
5345 else if (direction == GTK_MENU_DIR_PARENT)
5347 /* we go one left if possible */
5349 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
5353 GtkWidget *parent = menu_shell->parent_menu_shell;
5356 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
5357 match = menu_shell->active_menu_item;
5360 else if (direction == GTK_MENU_DIR_CHILD)
5362 /* we go one right if possible */
5363 if (r < gtk_menu_get_n_columns (menu))
5364 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5368 GtkWidget *parent = menu_shell->parent_menu_shell;
5370 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
5372 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
5373 match = menu_shell->active_menu_item;
5379 gtk_menu_shell_select_item (menu_shell, match);
5384 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5388 get_visible_size (GtkMenu *menu)
5390 GtkAllocation allocation;
5391 GtkWidget *widget = GTK_WIDGET (menu);
5392 GtkContainer *container = GTK_CONTAINER (menu);
5395 gtk_widget_get_allocation (widget, &allocation);
5396 menu_height = (allocation.height
5397 - 2 * (gtk_container_get_border_width (container)
5398 + gtk_widget_get_style (widget)->ythickness));
5400 if (!menu->tearoff_active)
5402 GtkBorder arrow_border;
5404 get_arrows_border (menu, &arrow_border);
5405 menu_height -= arrow_border.top;
5406 menu_height -= arrow_border.bottom;
5412 /* Find the sensitive on-screen child containing @y, or if none,
5413 * the nearest selectable onscreen child. (%NULL if none)
5416 child_at (GtkMenu *menu,
5419 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5420 GtkWidget *child = NULL;
5421 gint child_offset = 0;
5424 gint lower, upper; /* Onscreen bounds */
5426 menu_height = get_visible_size (menu);
5427 lower = menu->scroll_offset;
5428 upper = menu->scroll_offset + menu_height;
5430 for (children = menu_shell->children; children; children = children->next)
5432 if (gtk_widget_get_visible (children->data))
5434 GtkRequisition child_requisition;
5436 gtk_widget_get_preferred_size (children->data,
5437 &child_requisition, NULL);
5439 if (_gtk_menu_item_is_selectable (children->data) &&
5440 child_offset >= lower &&
5441 child_offset + child_requisition.height <= upper)
5443 child = children->data;
5445 if (child_offset + child_requisition.height > y &&
5446 !GTK_IS_TEAROFF_MENU_ITEM (child))
5450 child_offset += child_requisition.height;
5458 get_menu_height (GtkMenu *menu)
5460 GtkAllocation allocation;
5461 GtkWidget *widget = GTK_WIDGET (menu);
5464 gtk_widget_get_allocation (widget, &allocation);
5466 height = allocation.height;
5467 height -= (gtk_container_get_border_width (GTK_CONTAINER (widget)) + gtk_widget_get_style (widget)->ythickness) * 2;
5469 if (!menu->tearoff_active)
5471 GtkBorder arrow_border;
5473 get_arrows_border (menu, &arrow_border);
5474 height -= arrow_border.top;
5475 height -= arrow_border.bottom;
5482 gtk_menu_real_move_scroll (GtkMenu *menu,
5485 gint page_size = get_visible_size (menu);
5486 gint end_position = get_menu_height (menu);
5487 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5491 case GTK_SCROLL_PAGE_UP:
5492 case GTK_SCROLL_PAGE_DOWN:
5496 gint child_offset = 0;
5497 gboolean old_upper_arrow_visible;
5500 if (type == GTK_SCROLL_PAGE_UP)
5505 if (menu_shell->active_menu_item)
5509 compute_child_offset (menu, menu_shell->active_menu_item,
5510 &child_offset, &child_height, NULL);
5511 child_offset += child_height / 2;
5514 menu_shell->ignore_enter = TRUE;
5515 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5516 old_offset = menu->scroll_offset;
5518 new_offset = menu->scroll_offset + step;
5519 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5521 gtk_menu_scroll_to (menu, new_offset);
5523 if (menu_shell->active_menu_item)
5525 GtkWidget *new_child;
5526 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5527 GtkBorder arrow_border;
5529 get_arrows_border (menu, &arrow_border);
5531 if (menu->scroll_offset != old_offset)
5532 step = menu->scroll_offset - old_offset;
5534 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5536 new_child = child_at (menu, child_offset + step);
5538 gtk_menu_shell_select_item (menu_shell, new_child);
5542 case GTK_SCROLL_START:
5543 /* Ignore the enter event we might get if the pointer is on the menu
5545 menu_shell->ignore_enter = TRUE;
5546 gtk_menu_scroll_to (menu, 0);
5547 gtk_menu_shell_select_first (menu_shell, TRUE);
5549 case GTK_SCROLL_END:
5550 /* Ignore the enter event we might get if the pointer is on the menu
5552 menu_shell->ignore_enter = TRUE;
5553 gtk_menu_scroll_to (menu, end_position - page_size);
5554 _gtk_menu_shell_select_last (menu_shell, TRUE);
5563 * gtk_menu_set_monitor:
5565 * @monitor_num: the number of the monitor on which the menu should
5568 * Informs GTK+ on which monitor a menu should be popped up.
5569 * See gdk_screen_get_monitor_geometry().
5571 * This function should be called from a #GtkMenuPositionFunc if the
5572 * menu should not appear on the same monitor as the pointer. This
5573 * information can't be reliably inferred from the coordinates returned
5574 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
5575 * may extend beyond the monitor boundaries or even the screen boundaries.
5580 gtk_menu_set_monitor (GtkMenu *menu,
5583 GtkMenuPrivate *priv;
5584 g_return_if_fail (GTK_IS_MENU (menu));
5586 priv = gtk_menu_get_private (menu);
5588 priv->monitor_num = monitor_num;
5592 * gtk_menu_get_monitor:
5595 * Retrieves the number of the monitor on which to show the menu.
5597 * Returns: the number of the monitor on which the menu should
5598 * be popped up or -1, if no monitor has been set
5603 gtk_menu_get_monitor (GtkMenu *menu)
5605 GtkMenuPrivate *priv;
5606 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5608 priv = gtk_menu_get_private (menu);
5610 return priv->monitor_num;
5614 * gtk_menu_get_for_attach_widget:
5615 * @widget: a #GtkWidget
5617 * Returns a list of the menus which are attached to this widget.
5618 * This list is owned by GTK+ and must not be modified.
5620 * Return value: (element-type GtkWidget) (transfer none): the list of menus attached to his widget.
5625 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5629 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5631 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5636 gtk_menu_grab_notify (GtkWidget *widget,
5637 gboolean was_grabbed)
5639 GtkWidget *toplevel;
5640 GtkWindowGroup *group;
5644 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5647 !gtk_widget_device_is_shadowed (widget, pointer))
5650 toplevel = gtk_widget_get_toplevel (widget);
5652 if (!GTK_IS_WINDOW (toplevel))
5655 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5656 grab = gtk_window_group_get_current_device_grab (group, pointer);
5658 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
5659 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5663 * gtk_menu_set_reserve_toggle_size:
5665 * @reserve_toggle_size: whether to reserve size for toggles
5667 * Sets whether the menu should reserve space for drawing toggles
5668 * or icons, regardless of their actual presence.
5673 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5674 gboolean reserve_toggle_size)
5676 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5677 gboolean no_toggle_size;
5679 no_toggle_size = !reserve_toggle_size;
5681 if (priv->no_toggle_size != no_toggle_size)
5683 priv->no_toggle_size = no_toggle_size;
5685 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5690 * gtk_menu_get_reserve_toggle_size:
5693 * Returns whether the menu reserves space for toggles and
5694 * icons, regardless of their actual presence.
5696 * Returns: Whether the menu reserves toggle space
5701 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5703 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5705 return !priv->no_toggle_size;