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_hide_all (GtkWidget *widget);
245 static void gtk_menu_position (GtkMenu *menu);
246 static void gtk_menu_reparent (GtkMenu *menu,
247 GtkWidget *new_parent,
249 static void gtk_menu_remove (GtkContainer *menu,
252 static void gtk_menu_update_title (GtkMenu *menu);
254 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
255 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
257 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
259 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
260 gboolean group_changed);
262 static void gtk_menu_get_preferred_width (GtkWidget *widget,
265 static void gtk_menu_get_preferred_height (GtkWidget *widget,
268 static void gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
274 static const gchar attach_data_key[] = "gtk-menu-attach-data";
276 static guint menu_signals[LAST_SIGNAL] = { 0 };
278 static GtkMenuPrivate *
279 gtk_menu_get_private (GtkMenu *menu)
281 return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
284 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
287 menu_queue_resize (GtkMenu *menu)
289 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
291 priv->have_layout = FALSE;
292 gtk_widget_queue_resize (GTK_WIDGET (menu));
296 attach_info_free (AttachInfo *info)
298 g_slice_free (AttachInfo, info);
302 get_attach_info (GtkWidget *child)
304 GObject *object = G_OBJECT (child);
305 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
309 ai = g_slice_new0 (AttachInfo);
310 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
311 (GDestroyNotify) attach_info_free);
318 is_grid_attached (AttachInfo *ai)
320 return (ai->left_attach >= 0 &&
321 ai->right_attach >= 0 &&
322 ai->top_attach >= 0 &&
323 ai->bottom_attach >= 0);
327 menu_ensure_layout (GtkMenu *menu)
329 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
331 if (!priv->have_layout)
333 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
337 gint max_right_attach;
338 gint max_bottom_attach;
340 /* Find extents of gridded portion
342 max_right_attach = 1;
343 max_bottom_attach = 0;
345 for (l = menu_shell->children; l; l = l->next)
347 GtkWidget *child = l->data;
348 AttachInfo *ai = get_attach_info (child);
350 if (is_grid_attached (ai))
352 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
353 max_right_attach = MAX (max_right_attach, ai->right_attach);
359 row_occupied = g_malloc0 (max_bottom_attach);
361 for (l = menu_shell->children; l; l = l->next)
363 GtkWidget *child = l->data;
364 AttachInfo *ai = get_attach_info (child);
366 if (is_grid_attached (ai))
370 for (i = ai->top_attach; i < ai->bottom_attach; i++)
371 row_occupied[i] = TRUE;
375 /* Lay non-grid-items out in those rows
378 for (l = menu_shell->children; l; l = l->next)
380 GtkWidget *child = l->data;
381 AttachInfo *ai = get_attach_info (child);
383 if (!is_grid_attached (ai))
385 while (current_row < max_bottom_attach && row_occupied[current_row])
388 ai->effective_left_attach = 0;
389 ai->effective_right_attach = max_right_attach;
390 ai->effective_top_attach = current_row;
391 ai->effective_bottom_attach = current_row + 1;
397 ai->effective_left_attach = ai->left_attach;
398 ai->effective_right_attach = ai->right_attach;
399 ai->effective_top_attach = ai->top_attach;
400 ai->effective_bottom_attach = ai->bottom_attach;
404 g_free (row_occupied);
406 priv->n_rows = MAX (current_row, max_bottom_attach);
407 priv->n_columns = max_right_attach;
408 priv->have_layout = TRUE;
414 gtk_menu_get_n_columns (GtkMenu *menu)
416 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
418 menu_ensure_layout (menu);
420 return priv->n_columns;
424 gtk_menu_get_n_rows (GtkMenu *menu)
426 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
428 menu_ensure_layout (menu);
434 get_effective_child_attach (GtkWidget *child,
440 GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
443 menu_ensure_layout (menu);
445 ai = get_attach_info (child);
448 *l = ai->effective_left_attach;
450 *r = ai->effective_right_attach;
452 *t = ai->effective_top_attach;
454 *b = ai->effective_bottom_attach;
459 gtk_menu_class_init (GtkMenuClass *class)
461 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
462 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
463 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
464 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
465 GtkBindingSet *binding_set;
467 gobject_class->set_property = gtk_menu_set_property;
468 gobject_class->get_property = gtk_menu_get_property;
470 widget_class->destroy = gtk_menu_destroy;
471 widget_class->realize = gtk_menu_realize;
472 widget_class->unrealize = gtk_menu_unrealize;
473 widget_class->size_allocate = gtk_menu_size_allocate;
474 widget_class->show = gtk_menu_show;
475 widget_class->draw = gtk_menu_draw;
476 widget_class->scroll_event = gtk_menu_scroll;
477 widget_class->key_press_event = gtk_menu_key_press;
478 widget_class->button_press_event = gtk_menu_button_press;
479 widget_class->button_release_event = gtk_menu_button_release;
480 widget_class->motion_notify_event = gtk_menu_motion_notify;
481 widget_class->show_all = gtk_menu_show_all;
482 widget_class->hide_all = gtk_menu_hide_all;
483 widget_class->enter_notify_event = gtk_menu_enter_notify;
484 widget_class->leave_notify_event = gtk_menu_leave_notify;
485 widget_class->style_set = gtk_menu_style_set;
486 widget_class->focus = gtk_menu_focus;
487 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
488 widget_class->grab_notify = gtk_menu_grab_notify;
489 widget_class->get_preferred_width = gtk_menu_get_preferred_width;
490 widget_class->get_preferred_height = gtk_menu_get_preferred_height;
491 widget_class->get_preferred_height_for_width = gtk_menu_get_preferred_height_for_width;
493 container_class->remove = gtk_menu_remove;
494 container_class->get_child_property = gtk_menu_get_child_property;
495 container_class->set_child_property = gtk_menu_set_child_property;
497 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
498 menu_shell_class->deactivate = gtk_menu_deactivate;
499 menu_shell_class->select_item = gtk_menu_select_item;
500 menu_shell_class->insert = gtk_menu_real_insert;
501 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
502 menu_shell_class->move_current = gtk_menu_move_current;
504 menu_signals[MOVE_SCROLL] =
505 g_signal_new_class_handler (I_("move-scroll"),
506 G_OBJECT_CLASS_TYPE (gobject_class),
507 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
508 G_CALLBACK (gtk_menu_real_move_scroll),
510 _gtk_marshal_VOID__ENUM,
512 GTK_TYPE_SCROLL_TYPE);
517 * The index of the currently selected menu item, or -1 if no
518 * menu item is selected.
522 g_object_class_install_property (gobject_class,
524 g_param_spec_int ("active",
526 P_("The currently selected menu item"),
528 GTK_PARAM_READWRITE));
531 * GtkMenu:accel-group:
533 * The accel group holding accelerators for the menu.
537 g_object_class_install_property (gobject_class,
539 g_param_spec_object ("accel-group",
541 P_("The accel group holding accelerators for the menu"),
542 GTK_TYPE_ACCEL_GROUP,
543 GTK_PARAM_READWRITE));
546 * GtkMenu:accel-path:
548 * An accel path used to conveniently construct accel paths of child items.
552 g_object_class_install_property (gobject_class,
554 g_param_spec_string ("accel-path",
556 P_("An accel path used to conveniently construct accel paths of child items"),
558 GTK_PARAM_READWRITE));
561 * GtkMenu:attach-widget:
563 * The widget the menu is attached to. Setting this property attaches
564 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
565 * use gtk_menu_attach_to_widget() directly.
569 g_object_class_install_property (gobject_class,
571 g_param_spec_object ("attach-widget",
573 P_("The widget the menu is attached to"),
575 GTK_PARAM_READWRITE));
577 g_object_class_install_property (gobject_class,
579 g_param_spec_string ("tearoff-title",
581 P_("A title that may be displayed by the window manager when this menu is torn-off"),
583 GTK_PARAM_READWRITE));
586 * GtkMenu:tearoff-state:
588 * A boolean that indicates whether the menu is torn-off.
592 g_object_class_install_property (gobject_class,
594 g_param_spec_boolean ("tearoff-state",
596 P_("A boolean that indicates whether the menu is torn-off"),
598 GTK_PARAM_READWRITE));
603 * The monitor the menu will be popped up on.
607 g_object_class_install_property (gobject_class,
609 g_param_spec_int ("monitor",
611 P_("The monitor the menu will be popped up on"),
613 GTK_PARAM_READWRITE));
615 gtk_widget_class_install_style_property (widget_class,
616 g_param_spec_int ("vertical-padding",
617 P_("Vertical Padding"),
618 P_("Extra space at the top and bottom of the menu"),
622 GTK_PARAM_READABLE));
625 * GtkMenu:reserve-toggle-size:
627 * A boolean that indicates whether the menu reserves space for
628 * toggles and icons, regardless of their actual presence.
630 * This property should only be changed from its default value
631 * for special-purposes such as tabular menus. Regular menus that
632 * are connected to a menu bar or context menus should reserve
633 * toggle space for consistency.
637 g_object_class_install_property (gobject_class,
638 PROP_RESERVE_TOGGLE_SIZE,
639 g_param_spec_boolean ("reserve-toggle-size",
640 P_("Reserve Toggle Size"),
641 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
643 GTK_PARAM_READWRITE));
645 gtk_widget_class_install_style_property (widget_class,
646 g_param_spec_int ("horizontal-padding",
647 P_("Horizontal Padding"),
648 P_("Extra space at the left and right edges of the menu"),
652 GTK_PARAM_READABLE));
654 gtk_widget_class_install_style_property (widget_class,
655 g_param_spec_int ("vertical-offset",
656 P_("Vertical Offset"),
657 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
661 GTK_PARAM_READABLE));
663 gtk_widget_class_install_style_property (widget_class,
664 g_param_spec_int ("horizontal-offset",
665 P_("Horizontal Offset"),
666 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
670 GTK_PARAM_READABLE));
672 gtk_widget_class_install_style_property (widget_class,
673 g_param_spec_boolean ("double-arrows",
675 P_("When scrolling, always show both arrows."),
677 GTK_PARAM_READABLE));
680 * GtkMenu:arrow-placement:
682 * Indicates where scroll arrows should be placed.
686 gtk_widget_class_install_style_property (widget_class,
687 g_param_spec_enum ("arrow-placement",
688 P_("Arrow Placement"),
689 P_("Indicates where scroll arrows should be placed"),
690 GTK_TYPE_ARROW_PLACEMENT,
692 GTK_PARAM_READABLE));
694 gtk_container_class_install_child_property (container_class,
695 CHILD_PROP_LEFT_ATTACH,
696 g_param_spec_int ("left-attach",
698 P_("The column number to attach the left side of the child to"),
700 GTK_PARAM_READWRITE));
702 gtk_container_class_install_child_property (container_class,
703 CHILD_PROP_RIGHT_ATTACH,
704 g_param_spec_int ("right-attach",
706 P_("The column number to attach the right side of the child to"),
708 GTK_PARAM_READWRITE));
710 gtk_container_class_install_child_property (container_class,
711 CHILD_PROP_TOP_ATTACH,
712 g_param_spec_int ("top-attach",
714 P_("The row number to attach the top of the child to"),
716 GTK_PARAM_READWRITE));
718 gtk_container_class_install_child_property (container_class,
719 CHILD_PROP_BOTTOM_ATTACH,
720 g_param_spec_int ("bottom-attach",
722 P_("The row number to attach the bottom of the child to"),
724 GTK_PARAM_READWRITE));
727 * GtkMenu::arrow-scaling
729 * Arbitrary constant to scale down the size of the scroll arrow.
733 gtk_widget_class_install_style_property (widget_class,
734 g_param_spec_float ("arrow-scaling",
736 P_("Arbitrary constant to scale down the size of the scroll arrow"),
738 GTK_PARAM_READABLE));
740 binding_set = gtk_binding_set_by_class (class);
741 gtk_binding_entry_add_signal (binding_set,
743 I_("move-current"), 1,
744 GTK_TYPE_MENU_DIRECTION_TYPE,
746 gtk_binding_entry_add_signal (binding_set,
749 GTK_TYPE_MENU_DIRECTION_TYPE,
751 gtk_binding_entry_add_signal (binding_set,
754 GTK_TYPE_MENU_DIRECTION_TYPE,
756 gtk_binding_entry_add_signal (binding_set,
759 GTK_TYPE_MENU_DIRECTION_TYPE,
761 gtk_binding_entry_add_signal (binding_set,
764 GTK_TYPE_MENU_DIRECTION_TYPE,
765 GTK_MENU_DIR_PARENT);
766 gtk_binding_entry_add_signal (binding_set,
769 GTK_TYPE_MENU_DIRECTION_TYPE,
770 GTK_MENU_DIR_PARENT);
771 gtk_binding_entry_add_signal (binding_set,
774 GTK_TYPE_MENU_DIRECTION_TYPE,
776 gtk_binding_entry_add_signal (binding_set,
779 GTK_TYPE_MENU_DIRECTION_TYPE,
781 gtk_binding_entry_add_signal (binding_set,
784 GTK_TYPE_SCROLL_TYPE,
786 gtk_binding_entry_add_signal (binding_set,
789 GTK_TYPE_SCROLL_TYPE,
791 gtk_binding_entry_add_signal (binding_set,
794 GTK_TYPE_SCROLL_TYPE,
796 gtk_binding_entry_add_signal (binding_set,
799 GTK_TYPE_SCROLL_TYPE,
801 gtk_binding_entry_add_signal (binding_set,
804 GTK_TYPE_SCROLL_TYPE,
806 gtk_binding_entry_add_signal (binding_set,
807 GDK_KEY_KP_Page_Up, 0,
809 GTK_TYPE_SCROLL_TYPE,
811 gtk_binding_entry_add_signal (binding_set,
812 GDK_KEY_Page_Down, 0,
814 GTK_TYPE_SCROLL_TYPE,
815 GTK_SCROLL_PAGE_DOWN);
816 gtk_binding_entry_add_signal (binding_set,
817 GDK_KEY_KP_Page_Down, 0,
819 GTK_TYPE_SCROLL_TYPE,
820 GTK_SCROLL_PAGE_DOWN);
822 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
823 P_("Can change accelerators"),
824 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
826 GTK_PARAM_READWRITE));
828 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
829 P_("Delay before submenus appear"),
830 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
834 GTK_PARAM_READWRITE));
836 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
837 P_("Delay before hiding a submenu"),
838 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
841 DEFAULT_POPDOWN_DELAY,
842 GTK_PARAM_READWRITE));
844 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
849 gtk_menu_set_property (GObject *object,
854 GtkMenu *menu = GTK_MENU (object);
859 gtk_menu_set_active (menu, g_value_get_int (value));
861 case PROP_ACCEL_GROUP:
862 gtk_menu_set_accel_group (menu, g_value_get_object (value));
864 case PROP_ACCEL_PATH:
865 gtk_menu_set_accel_path (menu, g_value_get_string (value));
867 case PROP_ATTACH_WIDGET:
871 widget = gtk_menu_get_attach_widget (menu);
873 gtk_menu_detach (menu);
875 widget = (GtkWidget*) g_value_get_object (value);
877 gtk_menu_attach_to_widget (menu, widget, NULL);
880 case PROP_TEAROFF_STATE:
881 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
883 case PROP_TEAROFF_TITLE:
884 gtk_menu_set_title (menu, g_value_get_string (value));
887 gtk_menu_set_monitor (menu, g_value_get_int (value));
889 case PROP_RESERVE_TOGGLE_SIZE:
890 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
893 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
899 gtk_menu_get_property (GObject *object,
904 GtkMenu *menu = GTK_MENU (object);
909 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->children, gtk_menu_get_active (menu)));
911 case PROP_ACCEL_GROUP:
912 g_value_set_object (value, gtk_menu_get_accel_group (menu));
914 case PROP_ACCEL_PATH:
915 g_value_set_string (value, gtk_menu_get_accel_path (menu));
917 case PROP_ATTACH_WIDGET:
918 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
920 case PROP_TEAROFF_STATE:
921 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
923 case PROP_TEAROFF_TITLE:
924 g_value_set_string (value, gtk_menu_get_title (menu));
927 g_value_set_int (value, gtk_menu_get_monitor (menu));
929 case PROP_RESERVE_TOGGLE_SIZE:
930 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
933 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
939 gtk_menu_set_child_property (GtkContainer *container,
945 GtkMenu *menu = GTK_MENU (container);
946 AttachInfo *ai = get_attach_info (child);
950 case CHILD_PROP_LEFT_ATTACH:
951 ai->left_attach = g_value_get_int (value);
953 case CHILD_PROP_RIGHT_ATTACH:
954 ai->right_attach = g_value_get_int (value);
956 case CHILD_PROP_TOP_ATTACH:
957 ai->top_attach = g_value_get_int (value);
959 case CHILD_PROP_BOTTOM_ATTACH:
960 ai->bottom_attach = g_value_get_int (value);
964 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
968 menu_queue_resize (menu);
972 gtk_menu_get_child_property (GtkContainer *container,
978 AttachInfo *ai = get_attach_info (child);
982 case CHILD_PROP_LEFT_ATTACH:
983 g_value_set_int (value, ai->left_attach);
985 case CHILD_PROP_RIGHT_ATTACH:
986 g_value_set_int (value, ai->right_attach);
988 case CHILD_PROP_TOP_ATTACH:
989 g_value_set_int (value, ai->top_attach);
991 case CHILD_PROP_BOTTOM_ATTACH:
992 g_value_set_int (value, ai->bottom_attach);
996 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1002 gtk_menu_window_event (GtkWidget *window,
1006 gboolean handled = FALSE;
1008 g_object_ref (window);
1009 g_object_ref (menu);
1011 switch (event->type)
1014 case GDK_KEY_RELEASE:
1015 handled = gtk_widget_event (menu, event);
1021 g_object_unref (window);
1022 g_object_unref (menu);
1028 gtk_menu_init (GtkMenu *menu)
1030 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
1032 menu->parent_menu_item = NULL;
1033 menu->old_active_menu_item = NULL;
1034 menu->accel_group = NULL;
1035 menu->position_func = NULL;
1036 menu->position_func_data = NULL;
1037 menu->toggle_size = 0;
1039 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1040 "type", GTK_WINDOW_POPUP,
1043 "signal::event", gtk_menu_window_event, menu,
1044 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1046 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
1047 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
1049 /* Refloat the menu, so that reference counting for the menu isn't
1050 * affected by it being a child of the toplevel
1052 g_object_force_floating (G_OBJECT (menu));
1053 menu->needs_destruction_ref_count = TRUE;
1055 menu->view_window = NULL;
1056 menu->bin_window = NULL;
1058 menu->scroll_offset = 0;
1059 menu->scroll_step = 0;
1060 menu->timeout_id = 0;
1061 menu->scroll_fast = FALSE;
1063 menu->tearoff_window = NULL;
1064 menu->tearoff_hbox = NULL;
1065 menu->torn_off = FALSE;
1066 menu->tearoff_active = FALSE;
1067 menu->tearoff_adjustment = NULL;
1068 menu->tearoff_scrollbar = NULL;
1070 menu->upper_arrow_visible = FALSE;
1071 menu->lower_arrow_visible = FALSE;
1072 menu->upper_arrow_prelight = FALSE;
1073 menu->lower_arrow_prelight = FALSE;
1075 priv->upper_arrow_state = GTK_STATE_NORMAL;
1076 priv->lower_arrow_state = GTK_STATE_NORMAL;
1078 priv->have_layout = FALSE;
1079 priv->monitor_num = -1;
1083 gtk_menu_destroy (GtkWidget *widget)
1085 GtkMenu *menu = GTK_MENU (widget);
1086 GtkMenuAttachData *data;
1087 GtkMenuPrivate *priv;
1089 gtk_menu_remove_scroll_timeout (menu);
1091 data = g_object_get_data (G_OBJECT (widget), attach_data_key);
1093 gtk_menu_detach (menu);
1095 gtk_menu_stop_navigating_submenu (menu);
1097 if (menu->old_active_menu_item)
1099 g_object_unref (menu->old_active_menu_item);
1100 menu->old_active_menu_item = NULL;
1103 /* Add back the reference count for being a child */
1104 if (menu->needs_destruction_ref_count)
1106 menu->needs_destruction_ref_count = FALSE;
1107 g_object_ref (widget);
1110 if (menu->accel_group)
1112 g_object_unref (menu->accel_group);
1113 menu->accel_group = NULL;
1117 gtk_widget_destroy (menu->toplevel);
1119 if (menu->tearoff_window)
1120 gtk_widget_destroy (menu->tearoff_window);
1122 priv = gtk_menu_get_private (menu);
1126 g_free (priv->heights);
1127 priv->heights = NULL;
1132 g_free (priv->title);
1136 if (priv->position_func_data_destroy)
1138 priv->position_func_data_destroy (menu->position_func_data);
1139 menu->position_func_data = NULL;
1140 priv->position_func_data_destroy = NULL;
1143 GTK_WIDGET_CLASS (gtk_menu_parent_class)->destroy (widget);
1147 menu_change_screen (GtkMenu *menu,
1148 GdkScreen *new_screen)
1150 GtkMenuPrivate *private = gtk_menu_get_private (menu);
1152 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1154 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1160 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1161 gtk_menu_position (menu);
1164 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1165 private->monitor_num = -1;
1169 attach_widget_screen_changed (GtkWidget *attach_widget,
1170 GdkScreen *previous_screen,
1173 if (gtk_widget_has_screen (attach_widget) &&
1174 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1176 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1181 gtk_menu_attach_to_widget (GtkMenu *menu,
1182 GtkWidget *attach_widget,
1183 GtkMenuDetachFunc detacher)
1185 GtkMenuAttachData *data;
1188 g_return_if_fail (GTK_IS_MENU (menu));
1189 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1191 /* keep this function in sync with gtk_widget_set_parent()
1194 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1197 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1198 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1202 g_object_ref_sink (menu);
1204 data = g_slice_new (GtkMenuAttachData);
1205 data->attach_widget = attach_widget;
1207 g_signal_connect (attach_widget, "screen-changed",
1208 G_CALLBACK (attach_widget_screen_changed), menu);
1209 attach_widget_screen_changed (attach_widget, NULL, menu);
1211 data->detacher = detacher;
1212 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1213 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1214 if (!g_list_find (list, menu))
1216 list = g_list_prepend (list, menu);
1218 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1219 (GDestroyNotify) g_list_free);
1221 if (gtk_widget_get_state (GTK_WIDGET (menu)) != GTK_STATE_NORMAL)
1222 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1224 /* we don't need to set the style here, since
1225 * we are a toplevel widget.
1228 /* Fallback title for menu comes from attach widget */
1229 gtk_menu_update_title (menu);
1231 g_object_notify (G_OBJECT (menu), "attach-widget");
1235 gtk_menu_get_attach_widget (GtkMenu *menu)
1237 GtkMenuAttachData *data;
1239 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1241 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1243 return data->attach_widget;
1248 gtk_menu_detach (GtkMenu *menu)
1250 GtkMenuAttachData *data;
1253 g_return_if_fail (GTK_IS_MENU (menu));
1255 /* keep this function in sync with gtk_widget_unparent()
1257 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1260 g_warning ("gtk_menu_detach(): menu is not attached");
1263 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1265 g_signal_handlers_disconnect_by_func (data->attach_widget,
1266 (gpointer) attach_widget_screen_changed,
1270 data->detacher (data->attach_widget, menu);
1271 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1272 list = g_list_remove (list, menu);
1274 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1275 (GDestroyNotify) g_list_free);
1277 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1279 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1280 gtk_widget_unrealize (GTK_WIDGET (menu));
1282 g_slice_free (GtkMenuAttachData, data);
1284 /* Fallback title for menu comes from attach widget */
1285 gtk_menu_update_title (menu);
1287 g_object_unref (menu);
1291 gtk_menu_remove (GtkContainer *container,
1294 GtkMenu *menu = GTK_MENU (container);
1296 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1298 /* Clear out old_active_menu_item if it matches the item we are removing
1300 if (menu->old_active_menu_item == widget)
1302 g_object_unref (menu->old_active_menu_item);
1303 menu->old_active_menu_item = NULL;
1306 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1307 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1309 menu_queue_resize (menu);
1315 return g_object_new (GTK_TYPE_MENU, NULL);
1319 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1323 GtkMenu *menu = GTK_MENU (menu_shell);
1324 AttachInfo *ai = get_attach_info (child);
1326 ai->left_attach = -1;
1327 ai->right_attach = -1;
1328 ai->top_attach = -1;
1329 ai->bottom_attach = -1;
1331 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1332 gtk_widget_set_parent_window (child, menu->bin_window);
1334 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1336 menu_queue_resize (menu);
1340 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1345 widget = GTK_WIDGET (menu);
1350 cairo_surface_t *surface;
1351 cairo_pattern_t *pattern;
1354 menu->tearoff_active = FALSE;
1355 menu->saved_scroll_offset = menu->scroll_offset;
1357 window = gtk_widget_get_window (menu->tearoff_window);
1358 width = gdk_window_get_width (window);
1359 height = gdk_window_get_height (window);
1361 surface = gdk_window_create_similar_surface (window,
1362 CAIRO_CONTENT_COLOR,
1366 cr = cairo_create (surface);
1367 gdk_cairo_set_source_window (cr,
1373 gtk_widget_set_size_request (menu->tearoff_window,
1377 pattern = cairo_pattern_create_for_surface (surface);
1378 gdk_window_set_background_pattern (window, pattern);
1380 cairo_pattern_destroy (pattern);
1381 cairo_surface_destroy (surface);
1386 popup_grab_on_window (GdkWindow *window,
1387 GdkDevice *keyboard,
1389 guint32 activate_time)
1392 gdk_device_grab (keyboard, window,
1393 GDK_OWNERSHIP_WINDOW, TRUE,
1394 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1395 NULL, activate_time) != GDK_GRAB_SUCCESS)
1399 gdk_device_grab (pointer, window,
1400 GDK_OWNERSHIP_WINDOW, TRUE,
1401 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1402 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1403 GDK_POINTER_MOTION_MASK,
1404 NULL, activate_time) != GDK_GRAB_SUCCESS)
1407 gdk_device_ungrab (keyboard, activate_time);
1416 * gtk_menu_popup_for_device:
1417 * @menu: a #GtkMenu.
1418 * @device: (allow-none): a #GdkDevice
1419 * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1420 * menu item, or %NULL
1421 * @parent_menu_item: (allow-none): the menu item whose activation triggered
1422 * the popup, or %NULL
1423 * @func: (allow-none): a user supplied function used to position the menu,
1425 * @data: (allow-none): user supplied data to be passed to @func
1426 * @destroy: (allow-none): destroy notify for @data
1427 * @button: the mouse button which was pressed to initiate the event
1428 * @activate_time: the time at which the activation event occurred
1430 * Displays a menu and makes it available for selection.
1432 * Applications can use this function to display context-sensitive menus,
1433 * and will typically supply %NULL for the @parent_menu_shell,
1434 * @parent_menu_item, @func, @data and @destroy parameters. The default
1435 * menu positioning function will position the menu at the current position
1436 * of @device (or its corresponding pointer).
1438 * The @button parameter should be the mouse button pressed to initiate
1439 * the menu popup. If the menu popup was initiated by something other than
1440 * a mouse button press, such as a mouse button release or a keypress,
1441 * @button should be 0.
1443 * The @activate_time parameter is used to conflict-resolve initiation of
1444 * concurrent requests for mouse/keyboard grab requests. To function
1445 * properly, this needs to be the time stamp of the user event (such as
1446 * a mouse click or key press) that caused the initiation of the popup.
1447 * Only if no such event is available, gtk_get_current_event_time() can
1453 gtk_menu_popup_for_device (GtkMenu *menu,
1455 GtkWidget *parent_menu_shell,
1456 GtkWidget *parent_menu_item,
1457 GtkMenuPositionFunc func,
1459 GDestroyNotify destroy,
1461 guint32 activate_time)
1464 GtkWidget *xgrab_shell;
1466 GdkEvent *current_event;
1467 GtkMenuShell *menu_shell;
1468 gboolean grab_keyboard;
1469 GtkMenuPrivate *priv;
1470 GtkWidget *parent_toplevel;
1471 GdkDevice *keyboard, *pointer;
1473 g_return_if_fail (GTK_IS_MENU (menu));
1474 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1477 device = gtk_get_current_event_device ();
1481 GdkDisplay *display;
1482 GdkDeviceManager *device_manager;
1485 display = gtk_widget_get_display (GTK_WIDGET (menu));
1486 device_manager = gdk_display_get_device_manager (display);
1487 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1489 device = devices->data;
1491 g_list_free (devices);
1494 widget = GTK_WIDGET (menu);
1495 menu_shell = GTK_MENU_SHELL (menu);
1496 priv = gtk_menu_get_private (menu);
1498 if (device->source == GDK_SOURCE_KEYBOARD)
1501 pointer = gdk_device_get_associated_device (device);
1506 keyboard = gdk_device_get_associated_device (device);
1509 menu_shell->parent_menu_shell = parent_menu_shell;
1511 priv->seen_item_enter = FALSE;
1513 /* Find the last viewable ancestor, and make an X grab on it
1515 parent = GTK_WIDGET (menu);
1519 gboolean viewable = TRUE;
1520 GtkWidget *tmp = parent;
1524 if (!gtk_widget_get_mapped (tmp))
1529 tmp = gtk_widget_get_parent (tmp);
1533 xgrab_shell = parent;
1535 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1538 /* We want to receive events generated when we map the menu; unfortunately,
1539 * since there is probably already an implicit grab in place from the
1540 * button that the user used to pop up the menu, we won't receive then --
1541 * in particular, the EnterNotify when the menu pops up under the pointer.
1543 * If we are grabbing on a parent menu shell, no problem; just grab on
1544 * that menu shell first before popping up the window with owner_events = TRUE.
1546 * When grabbing on the menu itself, things get more convuluted - we
1547 * we do an explicit grab on a specially created window with
1548 * owner_events = TRUE, which we override further down with a grab
1549 * on the menu. (We can't grab on the menu until it is mapped; we
1550 * probably could just leave the grab on the other window, with a
1551 * little reorganization of the code in gtkmenu*).
1553 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1554 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1559 if (xgrab_shell && xgrab_shell != widget)
1561 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1563 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1564 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1569 GdkWindow *transfer_window;
1571 xgrab_shell = widget;
1572 transfer_window = menu_grab_transfer_window_get (menu);
1573 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1575 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1576 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1580 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1582 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1583 * with a stuck up window, we just abort here. Presumably the user will
1586 menu_shell->parent_menu_shell = NULL;
1587 menu_grab_transfer_window_destroy (menu);
1591 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1592 menu_shell->active = TRUE;
1593 menu_shell->button = button;
1595 /* If we are popping up the menu from something other than, a button
1596 * press then, as a heuristic, we ignore enter events for the menu
1597 * until we get a MOTION_NOTIFY.
1600 current_event = gtk_get_current_event ();
1603 if ((current_event->type != GDK_BUTTON_PRESS) &&
1604 (current_event->type != GDK_ENTER_NOTIFY))
1605 menu_shell->ignore_enter = TRUE;
1607 gdk_event_free (current_event);
1610 menu_shell->ignore_enter = TRUE;
1614 gtk_menu_tearoff_bg_copy (menu);
1616 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1619 parent_toplevel = NULL;
1620 if (parent_menu_shell)
1621 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1622 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1624 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1626 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1629 /* Set transient for to get the right window group and parent relationship */
1630 if (GTK_IS_WINDOW (parent_toplevel))
1631 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1632 GTK_WINDOW (parent_toplevel));
1634 menu->parent_menu_item = parent_menu_item;
1635 menu->position_func = func;
1636 menu->position_func_data = data;
1637 priv->position_func_data_destroy = destroy;
1638 menu_shell->activate_time = activate_time;
1640 /* We need to show the menu here rather in the init function because
1641 * code expects to be able to tell if the menu is onscreen by
1642 * looking at the gtk_widget_get_visible (menu)
1644 gtk_widget_show (GTK_WIDGET (menu));
1646 /* Position the menu, possibly changing the size request
1648 gtk_menu_position (menu);
1650 /* Compute the size of the toplevel and realize it so we
1651 * can scroll correctly.
1654 GtkRequisition tmp_request;
1655 GtkAllocation tmp_allocation = { 0, };
1657 /* Instead of trusting the menu position function to queue a resize when the
1658 * menu goes out of bounds, invalidate the cached size here. */
1659 gtk_widget_queue_resize (GTK_WIDGET (menu));
1660 gtk_widget_get_preferred_size (menu->toplevel, &tmp_request, NULL);
1662 tmp_allocation.width = tmp_request.width;
1663 tmp_allocation.height = tmp_request.height;
1665 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1667 gtk_widget_realize (GTK_WIDGET (menu));
1670 gtk_menu_scroll_to (menu, menu->scroll_offset);
1672 /* if no item is selected, select the first one */
1673 if (!menu_shell->active_menu_item)
1675 gboolean touchscreen_mode;
1677 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1678 "gtk-touchscreen-mode", &touchscreen_mode,
1681 if (touchscreen_mode)
1682 gtk_menu_shell_select_first (menu_shell, TRUE);
1685 /* Once everything is set up correctly, map the toplevel window on
1688 gtk_widget_show (menu->toplevel);
1690 if (xgrab_shell == widget)
1691 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1693 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1695 if (parent_menu_shell)
1697 gboolean keyboard_mode;
1699 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1700 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1702 else if (menu_shell->button == 0) /* a keynav-activated context menu */
1703 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1705 _gtk_menu_shell_update_mnemonics (menu_shell);
1710 * @menu: a #GtkMenu.
1711 * @parent_menu_shell: (allow-none): the menu shell containing the triggering menu item, or %NULL
1712 * @parent_menu_item: (allow-none): the menu item whose activation triggered the popup, or %NULL
1713 * @func: (allow-none): a user supplied function used to position the menu, or %NULL
1714 * @data: (allow-none): user supplied data to be passed to @func.
1715 * @button: the mouse button which was pressed to initiate the event.
1716 * @activate_time: the time at which the activation event occurred.
1718 * Displays a menu and makes it available for selection. Applications can use
1719 * this function to display context-sensitive menus, and will typically supply
1720 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1721 * parameters. The default menu positioning function will position the menu
1722 * at the current mouse cursor position.
1724 * The @button parameter should be the mouse button pressed to initiate
1725 * the menu popup. If the menu popup was initiated by something other than
1726 * a mouse button press, such as a mouse button release or a keypress,
1727 * @button should be 0.
1729 * The @activate_time parameter is used to conflict-resolve initiation of
1730 * concurrent requests for mouse/keyboard grab requests. To function
1731 * properly, this needs to be the time stamp of the user event (such as
1732 * a mouse click or key press) that caused the initiation of the popup.
1733 * Only if no such event is available, gtk_get_current_event_time() can
1737 gtk_menu_popup (GtkMenu *menu,
1738 GtkWidget *parent_menu_shell,
1739 GtkWidget *parent_menu_item,
1740 GtkMenuPositionFunc func,
1743 guint32 activate_time)
1745 g_return_if_fail (GTK_IS_MENU (menu));
1747 gtk_menu_popup_for_device (menu,
1752 button, activate_time);
1756 gtk_menu_popdown (GtkMenu *menu)
1758 GtkMenuPrivate *private;
1759 GtkMenuShell *menu_shell;
1762 g_return_if_fail (GTK_IS_MENU (menu));
1764 menu_shell = GTK_MENU_SHELL (menu);
1765 private = gtk_menu_get_private (menu);
1767 menu_shell->parent_menu_shell = NULL;
1768 menu_shell->active = FALSE;
1769 menu_shell->ignore_enter = FALSE;
1771 private->have_position = FALSE;
1773 gtk_menu_stop_scrolling (menu);
1775 gtk_menu_stop_navigating_submenu (menu);
1777 if (menu_shell->active_menu_item)
1779 if (menu->old_active_menu_item)
1780 g_object_unref (menu->old_active_menu_item);
1781 menu->old_active_menu_item = menu_shell->active_menu_item;
1782 g_object_ref (menu->old_active_menu_item);
1785 gtk_menu_shell_deselect (menu_shell);
1787 /* The X Grab, if present, will automatically be removed when we hide
1789 gtk_widget_hide (menu->toplevel);
1790 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1792 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1796 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1798 if (gtk_bin_get_child (GTK_BIN (menu->toplevel)))
1800 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1804 /* We popped up the menu from the tearoff, so we need to
1805 * release the grab - we aren't actually hiding the menu.
1807 if (menu_shell->have_xgrab && pointer)
1809 GdkDevice *keyboard;
1811 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1812 keyboard = gdk_device_get_associated_device (pointer);
1815 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1819 /* gtk_menu_popdown is called each time a menu item is selected from
1820 * a torn off menu. Only scroll back to the saved position if the
1821 * non-tearoff menu was popped down.
1823 if (!menu->tearoff_active)
1824 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1825 menu->tearoff_active = TRUE;
1828 gtk_widget_hide (GTK_WIDGET (menu));
1830 menu_shell->have_xgrab = FALSE;
1833 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1835 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1837 menu_grab_transfer_window_destroy (menu);
1841 gtk_menu_get_active (GtkMenu *menu)
1846 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1848 if (!menu->old_active_menu_item)
1851 children = GTK_MENU_SHELL (menu)->children;
1855 child = children->data;
1856 children = children->next;
1858 if (gtk_bin_get_child (GTK_BIN (child)))
1863 menu->old_active_menu_item = child;
1864 if (menu->old_active_menu_item)
1865 g_object_ref (menu->old_active_menu_item);
1868 return menu->old_active_menu_item;
1872 gtk_menu_set_active (GtkMenu *menu,
1878 g_return_if_fail (GTK_IS_MENU (menu));
1880 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1883 child = tmp_list->data;
1884 if (gtk_bin_get_child (GTK_BIN (child)))
1886 if (menu->old_active_menu_item)
1887 g_object_unref (menu->old_active_menu_item);
1888 menu->old_active_menu_item = child;
1889 g_object_ref (menu->old_active_menu_item);
1896 * gtk_menu_set_accel_group:
1897 * @accel_group: (allow-none):
1900 gtk_menu_set_accel_group (GtkMenu *menu,
1901 GtkAccelGroup *accel_group)
1903 g_return_if_fail (GTK_IS_MENU (menu));
1905 if (menu->accel_group != accel_group)
1907 if (menu->accel_group)
1908 g_object_unref (menu->accel_group);
1909 menu->accel_group = accel_group;
1910 if (menu->accel_group)
1911 g_object_ref (menu->accel_group);
1912 _gtk_menu_refresh_accel_paths (menu, TRUE);
1917 gtk_menu_get_accel_group (GtkMenu *menu)
1919 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1921 return menu->accel_group;
1925 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1928 /* Menu items chain here to figure whether they can activate their
1929 * accelerators. Unlike ordinary widgets, menus allow accel
1930 * activation even if invisible since that's the usual case for
1931 * submenus/popup-menus. however, the state of the attach widget
1932 * affects the "activeness" of the menu.
1934 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1937 return gtk_widget_can_activate_accel (awidget, signal_id);
1939 return gtk_widget_is_sensitive (widget);
1943 * gtk_menu_set_accel_path
1944 * @menu: a valid #GtkMenu
1945 * @accel_path: (allow-none): a valid accelerator path
1947 * Sets an accelerator path for this menu from which accelerator paths
1948 * for its immediate children, its menu items, can be constructed.
1949 * The main purpose of this function is to spare the programmer the
1950 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1951 * each menu item that should support runtime user changable accelerators.
1952 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1953 * each menu item of this menu, that contains a label describing its purpose,
1954 * automatically gets an accel path assigned. For example, a menu containing
1955 * menu items "New" and "Exit", will, after
1956 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1957 * has been called, assign its items the accel paths:
1958 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1959 * Assigning accel paths to menu items then enables the user to change
1960 * their accelerators at runtime. More details about accelerator paths
1961 * and their default setups can be found at gtk_accel_map_add_entry().
1963 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1964 * pass a static string, you can save some memory by interning it first with
1965 * g_intern_static_string().
1968 gtk_menu_set_accel_path (GtkMenu *menu,
1969 const gchar *accel_path)
1971 g_return_if_fail (GTK_IS_MENU (menu));
1973 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1975 /* FIXME: accel_path should be defined as const gchar* */
1976 menu->accel_path = (gchar*)g_intern_string (accel_path);
1977 if (menu->accel_path)
1978 _gtk_menu_refresh_accel_paths (menu, FALSE);
1982 * gtk_menu_get_accel_path
1983 * @menu: a valid #GtkMenu
1985 * Retrieves the accelerator path set on the menu.
1987 * Returns: the accelerator path set on the menu.
1992 gtk_menu_get_accel_path (GtkMenu *menu)
1994 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1996 return menu->accel_path;
2001 gboolean group_changed;
2005 refresh_accel_paths_foreach (GtkWidget *widget,
2008 AccelPropagation *prop = data;
2010 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
2011 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2012 prop->menu->accel_path,
2013 prop->menu->accel_group,
2014 prop->group_changed);
2018 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
2019 gboolean group_changed)
2021 g_return_if_fail (GTK_IS_MENU (menu));
2023 if (menu->accel_path && menu->accel_group)
2025 AccelPropagation prop;
2028 prop.group_changed = group_changed;
2029 gtk_container_foreach (GTK_CONTAINER (menu),
2030 refresh_accel_paths_foreach,
2036 gtk_menu_reposition (GtkMenu *menu)
2038 g_return_if_fail (GTK_IS_MENU (menu));
2040 if (!menu->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2041 gtk_menu_position (menu);
2045 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2048 g_return_if_fail (GTK_IS_MENU (menu));
2050 if (adjustment->value != menu->scroll_offset)
2051 gtk_menu_scroll_to (menu, adjustment->value);
2055 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2058 GdkGeometry geometry_hints;
2059 GtkMenuPrivate *priv;
2061 if (!menu->tearoff_window)
2064 priv = gtk_menu_get_private (menu);
2066 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2068 GtkRequisition requisition;
2070 gtk_widget_get_preferred_size (menu->tearoff_scrollbar,
2071 &requisition, NULL);
2072 width += requisition.width;
2075 geometry_hints.min_width = width;
2076 geometry_hints.max_width = width;
2078 geometry_hints.min_height = 0;
2079 geometry_hints.max_height = priv->requested_height;
2081 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
2084 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2088 gtk_menu_update_title (GtkMenu *menu)
2090 if (menu->tearoff_window)
2093 GtkWidget *attach_widget;
2095 title = gtk_menu_get_title (menu);
2098 attach_widget = gtk_menu_get_attach_widget (menu);
2099 if (GTK_IS_MENU_ITEM (attach_widget))
2101 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2102 if (GTK_IS_LABEL (child))
2103 title = gtk_label_get_text (GTK_LABEL (child));
2108 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
2113 gtk_menu_get_toplevel (GtkWidget *menu)
2115 GtkWidget *attach, *toplevel;
2117 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2119 if (GTK_IS_MENU_ITEM (attach))
2120 attach = gtk_widget_get_parent (attach);
2122 if (GTK_IS_MENU (attach))
2123 return gtk_menu_get_toplevel (attach);
2124 else if (GTK_IS_WIDGET (attach))
2126 toplevel = gtk_widget_get_toplevel (attach);
2127 if (gtk_widget_is_toplevel (toplevel))
2135 tearoff_window_destroyed (GtkWidget *widget,
2138 gtk_menu_set_tearoff_state (menu, FALSE);
2142 gtk_menu_set_tearoff_state (GtkMenu *menu,
2146 GtkMenuPrivate *priv;
2148 g_return_if_fail (GTK_IS_MENU (menu));
2150 priv = gtk_menu_get_private (menu);
2152 if (menu->torn_off != torn_off)
2154 menu->torn_off = torn_off;
2155 menu->tearoff_active = torn_off;
2159 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2160 gtk_menu_popdown (menu);
2162 if (!menu->tearoff_window)
2164 GtkWidget *toplevel;
2166 menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2167 "type", GTK_WINDOW_TOPLEVEL,
2168 "screen", gtk_widget_get_screen (menu->toplevel),
2169 "app-paintable", TRUE,
2172 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
2173 GDK_WINDOW_TYPE_HINT_MENU);
2174 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
2175 g_signal_connect (menu->tearoff_window, "destroy",
2176 G_CALLBACK (tearoff_window_destroyed), menu);
2177 g_signal_connect (menu->tearoff_window, "event",
2178 G_CALLBACK (gtk_menu_window_event), menu);
2180 gtk_menu_update_title (menu);
2182 gtk_widget_realize (menu->tearoff_window);
2184 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2185 if (toplevel != NULL)
2186 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
2187 GTK_WINDOW (toplevel));
2189 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
2190 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
2192 height = gdk_window_get_height (gtk_widget_get_window (GTK_WIDGET (menu)));
2193 menu->tearoff_adjustment = gtk_adjustment_new (0,
2194 0, priv->requested_height,
2198 g_object_connect (menu->tearoff_adjustment,
2199 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2201 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
2203 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
2204 menu->tearoff_scrollbar,
2207 if (menu->tearoff_adjustment->upper > height)
2208 gtk_widget_show (menu->tearoff_scrollbar);
2210 gtk_widget_show (menu->tearoff_hbox);
2213 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
2215 /* Update menu->requisition
2217 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
2220 gtk_menu_set_tearoff_hints (menu, gdk_window_get_width (gtk_widget_get_window (GTK_WIDGET (menu))));
2222 gtk_widget_realize (menu->tearoff_window);
2223 gtk_menu_position (menu);
2225 gtk_widget_show (GTK_WIDGET (menu));
2226 gtk_widget_show (menu->tearoff_window);
2228 gtk_menu_scroll_to (menu, 0);
2233 gtk_widget_hide (GTK_WIDGET (menu));
2234 gtk_widget_hide (menu->tearoff_window);
2235 if (GTK_IS_CONTAINER (menu->toplevel))
2236 gtk_menu_reparent (menu, menu->toplevel, FALSE);
2237 gtk_widget_destroy (menu->tearoff_window);
2239 menu->tearoff_window = NULL;
2240 menu->tearoff_hbox = NULL;
2241 menu->tearoff_scrollbar = NULL;
2242 menu->tearoff_adjustment = NULL;
2245 g_object_notify (G_OBJECT (menu), "tearoff-state");
2250 * gtk_menu_get_tearoff_state:
2253 * Returns whether the menu is torn off. See
2254 * gtk_menu_set_tearoff_state ().
2256 * Return value: %TRUE if the menu is currently torn off.
2259 gtk_menu_get_tearoff_state (GtkMenu *menu)
2261 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2263 return menu->torn_off;
2267 * gtk_menu_set_title:
2269 * @title: a string containing the title for the menu.
2271 * Sets the title string for the menu. The title is displayed when the menu
2272 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
2273 * attached to a parent menu item, and if so it will try to use the same text as
2274 * that menu item's label.
2277 gtk_menu_set_title (GtkMenu *menu,
2280 GtkMenuPrivate *priv;
2283 g_return_if_fail (GTK_IS_MENU (menu));
2285 priv = gtk_menu_get_private (menu);
2287 old_title = priv->title;
2288 priv->title = g_strdup (title);
2291 gtk_menu_update_title (menu);
2292 g_object_notify (G_OBJECT (menu), "tearoff-title");
2296 * gtk_menu_get_title:
2299 * Returns the title of the menu. See gtk_menu_set_title().
2301 * Return value: the title of the menu, or %NULL if the menu has no
2302 * title set on it. This string is owned by the widget and should
2303 * not be modified or freed.
2305 G_CONST_RETURN gchar *
2306 gtk_menu_get_title (GtkMenu *menu)
2308 GtkMenuPrivate *priv;
2310 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2312 priv = gtk_menu_get_private (menu);
2318 gtk_menu_reorder_child (GtkMenu *menu,
2322 GtkMenuShell *menu_shell;
2324 g_return_if_fail (GTK_IS_MENU (menu));
2325 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2327 menu_shell = GTK_MENU_SHELL (menu);
2329 if (g_list_find (menu_shell->children, child))
2331 menu_shell->children = g_list_remove (menu_shell->children, child);
2332 menu_shell->children = g_list_insert (menu_shell->children, child, position);
2334 menu_queue_resize (menu);
2339 gtk_menu_style_set (GtkWidget *widget,
2340 GtkStyle *previous_style)
2342 if (gtk_widget_get_realized (widget))
2344 GtkMenu *menu = GTK_MENU (widget);
2347 style = gtk_widget_get_style (widget);
2349 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2350 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2351 gtk_style_set_background (style, gtk_widget_get_window (widget), GTK_STATE_NORMAL);
2356 get_arrows_border (GtkMenu *menu,
2359 guint scroll_arrow_height;
2360 GtkArrowPlacement arrow_placement;
2362 gtk_widget_style_get (GTK_WIDGET (menu),
2363 "scroll-arrow-vlength", &scroll_arrow_height,
2364 "arrow_placement", &arrow_placement,
2367 switch (arrow_placement)
2369 case GTK_ARROWS_BOTH:
2370 border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2371 border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2374 case GTK_ARROWS_START:
2375 border->top = (menu->upper_arrow_visible ||
2376 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2380 case GTK_ARROWS_END:
2382 border->bottom = (menu->upper_arrow_visible ||
2383 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2387 border->left = border->right = 0;
2391 gtk_menu_realize (GtkWidget *widget)
2393 GtkAllocation allocation;
2396 GdkWindowAttr attributes;
2397 gint attributes_mask;
2400 GtkMenuPrivate *priv;
2403 guint vertical_padding;
2404 guint horizontal_padding;
2405 GtkBorder arrow_border;
2407 g_return_if_fail (GTK_IS_MENU (widget));
2409 menu = GTK_MENU (widget);
2410 priv = gtk_menu_get_private (menu);
2412 gtk_widget_set_realized (widget, TRUE);
2414 gtk_widget_get_allocation (widget, &allocation);
2416 attributes.window_type = GDK_WINDOW_CHILD;
2417 attributes.x = allocation.x;
2418 attributes.y = allocation.y;
2419 attributes.width = allocation.width;
2420 attributes.height = allocation.height;
2421 attributes.wclass = GDK_INPUT_OUTPUT;
2422 attributes.visual = gtk_widget_get_visual (widget);
2423 attributes.event_mask = gtk_widget_get_events (widget);
2424 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2425 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2427 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2429 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2430 &attributes, attributes_mask);
2431 gtk_widget_set_window (widget, window);
2432 gdk_window_set_user_data (window, widget);
2434 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2436 style = gtk_widget_get_style (widget);
2437 gtk_widget_style_get (GTK_WIDGET (menu),
2438 "vertical-padding", &vertical_padding,
2439 "horizontal-padding", &horizontal_padding,
2442 gtk_widget_get_allocation (widget, &allocation);
2444 attributes.x = border_width + style->xthickness + horizontal_padding;
2445 attributes.y = border_width + style->ythickness + vertical_padding;
2446 attributes.width = MAX (1, allocation.width - attributes.x * 2);
2447 attributes.height = MAX (1, allocation.height - attributes.y * 2);
2449 get_arrows_border (menu, &arrow_border);
2450 attributes.y += arrow_border.top;
2451 attributes.height -= arrow_border.top;
2452 attributes.height -= arrow_border.bottom;
2454 menu->view_window = gdk_window_new (window,
2455 &attributes, attributes_mask);
2456 gdk_window_set_user_data (menu->view_window, menu);
2458 gtk_widget_get_allocation (widget, &allocation);
2462 attributes.width = MAX (1, allocation.width - (border_width + style->xthickness + horizontal_padding) * 2);
2463 attributes.height = MAX (1, priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2);
2465 menu->bin_window = gdk_window_new (menu->view_window,
2466 &attributes, attributes_mask);
2467 gdk_window_set_user_data (menu->bin_window, menu);
2469 children = GTK_MENU_SHELL (menu)->children;
2472 child = children->data;
2473 children = children->next;
2475 gtk_widget_set_parent_window (child, menu->bin_window);
2478 gtk_widget_style_attach (widget);
2479 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2480 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2481 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
2483 if (GTK_MENU_SHELL (widget)->active_menu_item)
2484 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2485 GTK_MENU_SHELL (widget)->active_menu_item);
2487 gdk_window_show (menu->bin_window);
2488 gdk_window_show (menu->view_window);
2492 gtk_menu_focus (GtkWidget *widget,
2493 GtkDirectionType direction)
2496 * A menu or its menu items cannot have focus
2501 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2504 menu_grab_transfer_window_get (GtkMenu *menu)
2506 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2509 GdkWindowAttr attributes;
2510 gint attributes_mask;
2512 attributes.x = -100;
2513 attributes.y = -100;
2514 attributes.width = 10;
2515 attributes.height = 10;
2516 attributes.window_type = GDK_WINDOW_TEMP;
2517 attributes.wclass = GDK_INPUT_ONLY;
2518 attributes.override_redirect = TRUE;
2519 attributes.event_mask = 0;
2521 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2523 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2524 &attributes, attributes_mask);
2525 gdk_window_set_user_data (window, menu);
2527 gdk_window_show (window);
2529 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2536 menu_grab_transfer_window_destroy (GtkMenu *menu)
2538 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2541 gdk_window_set_user_data (window, NULL);
2542 gdk_window_destroy (window);
2543 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2548 gtk_menu_unrealize (GtkWidget *widget)
2550 GtkMenu *menu = GTK_MENU (widget);
2552 menu_grab_transfer_window_destroy (menu);
2554 gdk_window_set_user_data (menu->view_window, NULL);
2555 gdk_window_destroy (menu->view_window);
2556 menu->view_window = NULL;
2558 gdk_window_set_user_data (menu->bin_window, NULL);
2559 gdk_window_destroy (menu->bin_window);
2560 menu->bin_window = NULL;
2562 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2567 calculate_line_heights (GtkMenu *menu,
2569 guint **ret_min_heights,
2570 guint **ret_nat_heights)
2572 GtkMenuShell *menu_shell;
2573 GtkMenuPrivate *priv;
2574 GtkWidget *child, *widget;
2576 guint horizontal_padding;
2584 widget = GTK_WIDGET (menu);
2585 menu_shell = GTK_MENU_SHELL (widget);
2586 priv = gtk_menu_get_private (menu);
2588 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2589 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2590 n_heights = gtk_menu_get_n_rows (menu);
2591 n_columns = gtk_menu_get_n_columns (menu);
2592 avail_width = for_width - (2 * menu->toggle_size + priv->accel_size) * n_columns;
2594 gtk_widget_style_get (GTK_WIDGET (menu),
2595 "horizontal-padding", &horizontal_padding,
2598 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2599 avail_width -= (border_width + horizontal_padding + gtk_widget_get_style (widget)->xthickness) * 2;
2601 for (children = menu_shell->children; children; children = children->next)
2606 gint child_min, child_nat;
2608 child = children->data;
2610 if (! gtk_widget_get_visible (child))
2613 get_effective_child_attach (child, &l, &r, &t, &b);
2615 part = avail_width / (r - l);
2617 gtk_widget_get_preferred_height_for_width (child, part,
2618 &child_min, &child_nat);
2620 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2622 part = MAX (child_min, toggle_size) / (b - t);
2623 min_heights[t] = MAX (min_heights[t], part);
2625 part = MAX (child_nat, toggle_size) / (b - t);
2626 nat_heights[t] = MAX (nat_heights[t], part);
2629 if (ret_min_heights)
2630 *ret_min_heights = min_heights;
2632 g_free (min_heights);
2634 if (ret_nat_heights)
2635 *ret_nat_heights = nat_heights;
2637 g_free (nat_heights);
2643 gtk_menu_size_allocate (GtkWidget *widget,
2644 GtkAllocation *allocation)
2647 GtkMenuShell *menu_shell;
2649 GtkAllocation child_allocation;
2650 GtkMenuPrivate *priv;
2656 guint vertical_padding;
2657 guint horizontal_padding;
2659 g_return_if_fail (GTK_IS_MENU (widget));
2660 g_return_if_fail (allocation != NULL);
2662 menu = GTK_MENU (widget);
2663 menu_shell = GTK_MENU_SHELL (widget);
2664 priv = gtk_menu_get_private (menu);
2666 gtk_widget_set_allocation (widget, allocation);
2668 style = gtk_widget_get_style (widget);
2670 gtk_widget_style_get (GTK_WIDGET (menu),
2671 "vertical-padding", &vertical_padding,
2672 "horizontal-padding", &horizontal_padding,
2674 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2676 g_free (priv->heights);
2677 priv->heights_length =
2678 calculate_line_heights (menu,
2683 /* refresh our cached height request */
2684 priv->requested_height = (border_width + vertical_padding + style->ythickness) * 2;
2685 for (i = 0; i < priv->heights_length; i++)
2686 priv->requested_height += priv->heights[i];
2688 x = border_width + style->xthickness + horizontal_padding;
2689 y = border_width + style->ythickness + vertical_padding;
2691 width = MAX (1, allocation->width - x * 2);
2692 height = MAX (1, allocation->height - y * 2);
2694 if (menu_shell->active)
2695 gtk_menu_scroll_to (menu, menu->scroll_offset);
2697 if (!menu->tearoff_active)
2699 GtkBorder arrow_border;
2701 get_arrows_border (menu, &arrow_border);
2702 y += arrow_border.top;
2703 height -= arrow_border.top;
2704 height -= arrow_border.bottom;
2707 if (gtk_widget_get_realized (widget))
2709 gdk_window_move_resize (gtk_widget_get_window (widget),
2710 allocation->x, allocation->y,
2711 allocation->width, allocation->height);
2713 gdk_window_move_resize (menu->view_window,
2720 if (menu_shell->children)
2722 gint base_width = width / gtk_menu_get_n_columns (menu);
2724 children = menu_shell->children;
2727 child = children->data;
2728 children = children->next;
2730 if (gtk_widget_get_visible (child))
2735 get_effective_child_attach (child, &l, &r, &t, &b);
2737 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2740 tmp = gtk_menu_get_n_columns (menu) - l;
2741 l = gtk_menu_get_n_columns (menu) - r;
2745 child_allocation.width = (r - l) * base_width;
2746 child_allocation.height = 0;
2747 child_allocation.x = l * base_width;
2748 child_allocation.y = 0;
2750 for (i = 0; i < b; i++)
2753 child_allocation.y += priv->heights[i];
2755 child_allocation.height += priv->heights[i];
2758 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2761 gtk_widget_size_allocate (child, &child_allocation);
2762 gtk_widget_queue_draw (child);
2766 /* Resize the item window */
2767 if (gtk_widget_get_realized (widget))
2773 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2774 height += priv->heights[i];
2776 width = gtk_menu_get_n_columns (menu) * base_width;
2777 gdk_window_resize (menu->bin_window, width, height);
2780 if (menu->tearoff_active)
2782 if (height >= priv->requested_height)
2784 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2786 gtk_widget_hide (menu->tearoff_scrollbar);
2787 gtk_menu_set_tearoff_hints (menu, allocation->width);
2789 gtk_menu_scroll_to (menu, 0);
2794 menu->tearoff_adjustment->upper = priv->requested_height;
2795 menu->tearoff_adjustment->page_size = allocation->height;
2797 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2798 menu->tearoff_adjustment->upper)
2801 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2804 gtk_menu_scroll_to (menu, value);
2807 gtk_adjustment_changed (menu->tearoff_adjustment);
2809 if (!gtk_widget_get_visible (menu->tearoff_scrollbar))
2811 gtk_widget_show (menu->tearoff_scrollbar);
2812 gtk_menu_set_tearoff_hints (menu, allocation->width);
2820 get_arrows_visible_area (GtkMenu *menu,
2821 GdkRectangle *border,
2822 GdkRectangle *upper,
2823 GdkRectangle *lower,
2826 GtkArrowPlacement arrow_placement;
2828 GtkWidget *widget = GTK_WIDGET (menu);
2830 guint vertical_padding;
2831 guint horizontal_padding;
2832 gint scroll_arrow_height;
2834 style = gtk_widget_get_style (widget);
2836 gtk_widget_style_get (widget,
2837 "vertical-padding", &vertical_padding,
2838 "horizontal-padding", &horizontal_padding,
2839 "scroll-arrow-vlength", &scroll_arrow_height,
2840 "arrow-placement", &arrow_placement,
2843 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2844 border->x = border_width + style->xthickness + horizontal_padding;
2845 border->y = border_width + style->ythickness + vertical_padding;
2846 border->width = gdk_window_get_width (gtk_widget_get_window (widget));
2847 border->height = gdk_window_get_height (gtk_widget_get_window (widget));
2849 switch (arrow_placement)
2851 case GTK_ARROWS_BOTH:
2852 upper->x = border->x;
2853 upper->y = border->y;
2854 upper->width = border->width - 2 * border->x;
2855 upper->height = scroll_arrow_height;
2857 lower->x = border->x;
2858 lower->y = border->height - border->y - scroll_arrow_height;
2859 lower->width = border->width - 2 * border->x;
2860 lower->height = scroll_arrow_height;
2863 case GTK_ARROWS_START:
2864 upper->x = border->x;
2865 upper->y = border->y;
2866 upper->width = (border->width - 2 * border->x) / 2;
2867 upper->height = scroll_arrow_height;
2869 lower->x = border->x + upper->width;
2870 lower->y = border->y;
2871 lower->width = (border->width - 2 * border->x) / 2;
2872 lower->height = scroll_arrow_height;
2875 case GTK_ARROWS_END:
2876 upper->x = border->x;
2877 upper->y = border->height - border->y - scroll_arrow_height;
2878 upper->width = (border->width - 2 * border->x) / 2;
2879 upper->height = scroll_arrow_height;
2881 lower->x = border->x + upper->width;
2882 lower->y = border->height - border->y - scroll_arrow_height;
2883 lower->width = (border->width - 2 * border->x) / 2;
2884 lower->height = scroll_arrow_height;
2888 g_assert_not_reached();
2889 upper->x = upper->y = upper->width = upper->height = 0;
2890 lower->x = lower->y = lower->width = lower->height = 0;
2893 *arrow_space = scroll_arrow_height - 2 * style->ythickness;
2897 gtk_menu_draw (GtkWidget *widget,
2901 GtkMenuPrivate *priv;
2903 GdkRectangle border;
2909 menu = GTK_MENU (widget);
2910 priv = gtk_menu_get_private (menu);
2912 style = gtk_widget_get_style (widget);
2913 window = gtk_widget_get_window (widget);
2915 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2917 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
2919 gfloat arrow_scaling;
2922 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
2923 arrow_size = arrow_scaling * arrow_space;
2925 gtk_paint_box (style,
2931 gtk_widget_get_allocated_width (widget),
2932 gtk_widget_get_allocated_height (widget));
2934 if (menu->upper_arrow_visible && !menu->tearoff_active)
2936 gtk_paint_box (style,
2938 priv->upper_arrow_state,
2940 widget, "menu_scroll_arrow_up",
2946 gtk_paint_arrow (style,
2948 priv->upper_arrow_state,
2950 widget, "menu_scroll_arrow_up",
2953 upper.x + (upper.width - arrow_size) / 2,
2954 upper.y + style->ythickness + (arrow_space - arrow_size) / 2,
2955 arrow_size, arrow_size);
2958 if (menu->lower_arrow_visible && !menu->tearoff_active)
2960 gtk_paint_box (style,
2962 priv->lower_arrow_state,
2964 widget, "menu_scroll_arrow_down",
2970 gtk_paint_arrow (style,
2972 priv->lower_arrow_state,
2974 widget, "menu_scroll_arrow_down",
2977 lower.x + (lower.width - arrow_size) / 2,
2978 lower.y + style->ythickness + (arrow_space - arrow_size) / 2,
2979 arrow_size, arrow_size);
2983 if (gtk_cairo_should_draw_window (cr, menu->bin_window))
2985 gint y = -border.y + menu->scroll_offset;
2988 gtk_cairo_transform_to_window (cr, widget, menu->bin_window);
2990 if (!menu->tearoff_active)
2992 GtkBorder arrow_border;
2994 get_arrows_border (menu, &arrow_border);
2995 y -= arrow_border.top;
2998 gtk_paint_box (style,
3004 border.width, border.height);
3009 GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
3015 gtk_menu_show (GtkWidget *widget)
3017 GtkMenu *menu = GTK_MENU (widget);
3019 _gtk_menu_refresh_accel_paths (menu, FALSE);
3021 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3026 gtk_menu_get_preferred_width (GtkWidget *widget,
3031 GtkMenuShell *menu_shell;
3032 GtkMenuPrivate *priv;
3036 guint max_toggle_size;
3037 guint max_accel_width;
3038 guint horizontal_padding;
3040 gint child_min, child_nat;
3041 gint min_width, nat_width;
3043 menu = GTK_MENU (widget);
3044 menu_shell = GTK_MENU_SHELL (widget);
3045 priv = gtk_menu_get_private (menu);
3047 style = gtk_widget_get_style (GTK_WIDGET (widget));
3049 min_width = nat_width = 0;
3051 max_toggle_size = 0;
3052 max_accel_width = 0;
3054 children = menu_shell->children;
3061 child = children->data;
3062 children = children->next;
3064 if (! gtk_widget_get_visible (child))
3067 get_effective_child_attach (child, &l, &r, &t, &b);
3069 /* It's important to size_request the child
3070 * before doing the toggle size request, in
3071 * case the toggle size request depends on the size
3072 * request of a child of the child (e.g. for ImageMenuItem)
3075 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
3076 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
3078 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3079 max_toggle_size = MAX (max_toggle_size, toggle_size);
3080 max_accel_width = MAX (max_accel_width,
3081 GTK_MENU_ITEM (child)->accelerator_width);
3083 part = child_min / (r - l);
3084 min_width = MAX (min_width, part);
3086 part = child_nat / (r - l);
3087 nat_width = MAX (nat_width, part);
3090 /* If the menu doesn't include any images or check items
3091 * reserve the space so that all menus are consistent.
3092 * We only do this for 'ordinary' menus, not for combobox
3093 * menus or multi-column menus
3095 if (max_toggle_size == 0 &&
3096 gtk_menu_get_n_columns (menu) == 1 &&
3097 !priv->no_toggle_size)
3099 guint toggle_spacing;
3100 guint indicator_size;
3102 gtk_style_get (style,
3103 GTK_TYPE_CHECK_MENU_ITEM,
3104 "toggle-spacing", &toggle_spacing,
3105 "indicator-size", &indicator_size,
3108 max_toggle_size = indicator_size + toggle_spacing;
3111 min_width += 2 * max_toggle_size + max_accel_width;
3112 min_width *= gtk_menu_get_n_columns (menu);
3114 nat_width += 2 * max_toggle_size + max_accel_width;
3115 nat_width *= gtk_menu_get_n_columns (menu);
3117 gtk_widget_style_get (GTK_WIDGET (menu),
3118 "horizontal-padding", &horizontal_padding,
3121 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3122 min_width += (border_width + horizontal_padding + style->xthickness) * 2;
3123 nat_width += (border_width + horizontal_padding + style->xthickness) * 2;
3125 menu->toggle_size = max_toggle_size;
3126 priv->accel_size = max_accel_width;
3129 *minimum_size = min_width;
3132 *natural_size = nat_width;
3134 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
3136 if (menu->tearoff_active)
3137 gtk_menu_set_tearoff_hints (menu, min_width);
3141 gtk_menu_get_preferred_height (GtkWidget *widget,
3147 /* Menus are height-for-width only, just return the height for the minimum width */
3148 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
3149 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
3153 gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
3158 GtkMenu *menu = GTK_MENU (widget);
3159 GtkMenuPrivate *private = gtk_menu_get_private (menu);
3160 guint *min_heights, *nat_heights;
3161 guint vertical_padding, border_width;
3163 gint min_height, nat_height;
3165 gtk_widget_style_get (GTK_WIDGET (menu), "vertical-padding", &vertical_padding, NULL);
3166 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3168 min_height = nat_height = (border_width + vertical_padding + gtk_widget_get_style (GTK_WIDGET (widget))->ythickness) * 2;
3171 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3173 for (i = 0; i < n_heights; i++)
3175 min_height += min_heights[i];
3176 nat_height += nat_heights[i];
3179 if (private->have_position)
3181 GdkScreen *screen = gtk_widget_get_screen (menu->toplevel);
3182 GdkRectangle monitor;
3184 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3186 if (private->y + min_height > monitor.y + monitor.height)
3187 min_height = monitor.y + monitor.height - private->y;
3189 if (private->y + nat_height > monitor.y + monitor.height)
3190 nat_height = monitor.y + monitor.height - private->y;
3192 if (private->y < monitor.y)
3194 min_height -= monitor.y - private->y;
3195 nat_height -= monitor.y - private->y;
3200 *minimum_size = min_height;
3203 *natural_size = nat_height;
3205 g_free (min_heights);
3206 g_free (nat_heights);
3212 gtk_menu_button_scroll (GtkMenu *menu,
3213 GdkEventButton *event)
3215 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
3217 gboolean touchscreen_mode;
3219 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3220 "gtk-touchscreen-mode", &touchscreen_mode,
3223 if (touchscreen_mode)
3224 gtk_menu_handle_scrolling (menu,
3225 event->x_root, event->y_root,
3226 event->type == GDK_BUTTON_PRESS,
3236 pointer_in_menu_window (GtkWidget *widget,
3240 GtkAllocation allocation;
3241 GtkMenu *menu = GTK_MENU (widget);
3243 if (gtk_widget_get_mapped (menu->toplevel))
3245 GtkMenuShell *menu_shell;
3246 gint window_x, window_y;
3248 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3249 &window_x, &window_y);
3251 gtk_widget_get_allocation (widget, &allocation);
3252 if (x_root >= window_x && x_root < window_x + allocation.width &&
3253 y_root >= window_y && y_root < window_y + allocation.height)
3256 menu_shell = GTK_MENU_SHELL (widget);
3258 if (GTK_IS_MENU (menu_shell->parent_menu_shell))
3259 return pointer_in_menu_window (menu_shell->parent_menu_shell,
3267 gtk_menu_button_press (GtkWidget *widget,
3268 GdkEventButton *event)
3270 if (event->type != GDK_BUTTON_PRESS)
3273 /* Don't pass down to menu shell for presses over scroll arrows
3275 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3278 /* Don't pass down to menu shell if a non-menuitem part of the menu
3279 * was clicked. The check for the event_widget being a GtkMenuShell
3280 * works because we have the pointer grabbed on menu_shell->window
3281 * with owner_events=TRUE, so all events that are either outside
3282 * the menu or on its border are delivered relative to
3283 * menu_shell->window.
3285 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3286 pointer_in_menu_window (widget, event->x_root, event->y_root))
3289 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3293 gtk_menu_button_release (GtkWidget *widget,
3294 GdkEventButton *event)
3296 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3298 if (priv->ignore_button_release)
3300 priv->ignore_button_release = FALSE;
3304 if (event->type != GDK_BUTTON_RELEASE)
3307 /* Don't pass down to menu shell for releases over scroll arrows
3309 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3312 /* Don't pass down to menu shell if a non-menuitem part of the menu
3313 * was clicked (see comment in button_press()).
3315 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3316 pointer_in_menu_window (widget, event->x_root, event->y_root))
3318 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3319 * bail out early here so it is in a consistent state for the
3320 * next button_press/button_release in GtkMenuShell.
3323 if (GTK_MENU_SHELL (widget)->active)
3324 GTK_MENU_SHELL (widget)->button = 0;
3329 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3332 static const gchar *
3333 get_accel_path (GtkWidget *menu_item,
3338 GClosure *accel_closure;
3339 GtkAccelGroup *accel_group;
3341 path = _gtk_widget_get_accel_path (menu_item, locked);
3344 path = GTK_MENU_ITEM (menu_item)->accel_path;
3350 label = gtk_bin_get_child (GTK_BIN (menu_item));
3352 if (GTK_IS_ACCEL_LABEL (label))
3354 g_object_get (label,
3355 "accel-closure", &accel_closure,
3359 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3361 *locked = gtk_accel_group_get_is_locked (accel_group);
3371 gtk_menu_key_press (GtkWidget *widget,
3374 GtkMenuShell *menu_shell;
3376 gboolean delete = FALSE;
3377 gboolean can_change_accels;
3378 gchar *accel = NULL;
3379 guint accel_key, accel_mods;
3380 GdkModifierType consumed_modifiers;
3381 GdkDisplay *display;
3383 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3384 g_return_val_if_fail (event != NULL, FALSE);
3386 menu_shell = GTK_MENU_SHELL (widget);
3387 menu = GTK_MENU (widget);
3389 gtk_menu_stop_navigating_submenu (menu);
3391 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3394 display = gtk_widget_get_display (widget);
3396 g_object_get (gtk_widget_get_settings (widget),
3397 "gtk-menu-bar-accel", &accel,
3398 "gtk-can-change-accels", &can_change_accels,
3401 if (accel && *accel)
3404 GdkModifierType mods = 0;
3406 gtk_accelerator_parse (accel, &keyval, &mods);
3409 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3411 /* FIXME this is wrong, needs to be in the global accel resolution
3412 * thing, to properly consider i18n etc., but that probably requires
3413 * AccelGroup changes etc.
3415 if (event->keyval == keyval && (mods & event->state) == mods)
3417 gtk_menu_shell_cancel (menu_shell);
3425 switch (event->keyval)
3427 case GDK_KEY_Delete:
3428 case GDK_KEY_KP_Delete:
3429 case GDK_KEY_BackSpace:
3436 /* Figure out what modifiers went into determining the key symbol */
3437 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3438 event->hardware_keycode, event->state, event->group,
3439 NULL, NULL, NULL, &consumed_modifiers);
3441 accel_key = gdk_keyval_to_lower (event->keyval);
3442 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3444 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
3445 * We re-upper case when we match against the keyval, but display and save in caseless form.
3447 if (accel_key != event->keyval)
3448 accel_mods |= GDK_SHIFT_MASK;
3450 /* Modify the accelerators */
3451 if (can_change_accels &&
3452 menu_shell->active_menu_item &&
3453 gtk_bin_get_child (GTK_BIN (menu_shell->active_menu_item)) && /* no separators */
3454 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
3455 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3457 GtkWidget *menu_item = menu_shell->active_menu_item;
3458 gboolean locked, replace_accels = TRUE;
3461 path = get_accel_path (menu_item, &locked);
3462 if (!path || locked)
3464 /* can't change accelerators on menu_items without paths
3465 * (basically, those items are accelerator-locked).
3467 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
3468 gtk_widget_error_bell (widget);
3474 /* For the keys that act to delete the current setting, we delete
3475 * the current setting if there is one, otherwise, we set the
3476 * key as the accelerator.
3482 if (gtk_accel_map_lookup_entry (path, &key) &&
3483 (key.accel_key || key.accel_mods))
3489 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3493 /* we failed, probably because this key is in use and
3496 /* g_print("failed to change\n"); */
3497 gtk_widget_error_bell (widget);
3506 check_threshold (GtkWidget *widget,
3515 ABS (start_x - x) > THRESHOLD ||
3516 ABS (start_y - y) > THRESHOLD;
3520 definitely_within_item (GtkWidget *widget,
3524 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
3527 w = gdk_window_get_width (window);
3528 h = gdk_window_get_height (window);
3531 check_threshold (widget, 0, 0, x, y) &&
3532 check_threshold (widget, w - 1, 0, x, y) &&
3533 check_threshold (widget, w - 1, h - 1, x, y) &&
3534 check_threshold (widget, 0, h - 1, x, y);
3538 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3540 GtkMenuPrivate *priv;
3542 priv = gtk_menu_get_private (menu);
3544 return priv->navigation_height && priv->navigation_width;
3548 gtk_menu_motion_notify (GtkWidget *widget,
3549 GdkEventMotion *event)
3551 GtkWidget *menu_item;
3553 GtkMenuShell *menu_shell;
3556 gboolean need_enter;
3558 if (GTK_IS_MENU (widget))
3560 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3562 if (priv->ignore_button_release)
3563 priv->ignore_button_release = FALSE;
3565 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3569 /* We received the event for one of two reasons:
3571 * a) We are the active menu, and did gtk_grab_add()
3572 * b) The widget is a child of ours, and the event was propagated
3574 * Since for computation of navigation regions, we want the menu which
3575 * is the parent of the menu item, for a), we need to find that menu,
3576 * which may be different from 'widget'.
3578 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3579 parent = gtk_widget_get_parent (menu_item);
3580 if (!GTK_IS_MENU_ITEM (menu_item) ||
3581 !GTK_IS_MENU (parent))
3584 menu_shell = GTK_MENU_SHELL (parent);
3585 menu = GTK_MENU (menu_shell);
3587 if (definitely_within_item (menu_item, event->x, event->y))
3588 menu_shell->activate_time = 0;
3590 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->ignore_enter);
3592 /* Check to see if we are within an active submenu's navigation region
3594 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3597 /* Make sure we pop down if we enter a non-selectable menu item, so we
3598 * don't show a submenu when the cursor is outside the stay-up triangle.
3600 if (!_gtk_menu_item_is_selectable (menu_item))
3602 /* We really want to deselect, but this gives the menushell code
3603 * a chance to do some bookkeeping about the menuitem.
3605 gtk_menu_shell_select_item (menu_shell, menu_item);
3611 /* The menu is now sensitive to enter events on its items, but
3612 * was previously sensitive. So we fake an enter event.
3614 menu_shell->ignore_enter = FALSE;
3616 if (event->x >= 0 && event->x < gdk_window_get_width (event->window) &&
3617 event->y >= 0 && event->y < gdk_window_get_height (event->window))
3619 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3622 send_event->crossing.window = g_object_ref (event->window);
3623 send_event->crossing.time = event->time;
3624 send_event->crossing.send_event = TRUE;
3625 send_event->crossing.x_root = event->x_root;
3626 send_event->crossing.y_root = event->y_root;
3627 send_event->crossing.x = event->x;
3628 send_event->crossing.y = event->y;
3629 send_event->crossing.state = event->state;
3630 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3632 /* We send the event to 'widget', the currently active menu,
3633 * instead of 'menu', the menu that the pointer is in. This
3634 * will ensure that the event will be ignored unless the
3635 * menuitem is a child of the active menu or some parent
3636 * menu of the active menu.
3638 result = gtk_widget_event (widget, send_event);
3639 gdk_event_free (send_event);
3649 get_double_arrows (GtkMenu *menu)
3651 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3652 gboolean double_arrows;
3653 GtkArrowPlacement arrow_placement;
3655 gtk_widget_style_get (GTK_WIDGET (menu),
3656 "double-arrows", &double_arrows,
3657 "arrow-placement", &arrow_placement,
3660 if (arrow_placement != GTK_ARROWS_BOTH)
3663 return double_arrows || (priv->initially_pushed_in &&
3664 menu->scroll_offset != 0);
3668 gtk_menu_scroll_by (GtkMenu *menu,
3671 GtkMenuPrivate *priv;
3672 GtkBorder arrow_border;
3676 gboolean double_arrows;
3678 widget = GTK_WIDGET (menu);
3679 offset = menu->scroll_offset + step;
3680 priv = gtk_menu_get_private (menu);
3682 get_arrows_border (menu, &arrow_border);
3684 double_arrows = get_double_arrows (menu);
3686 /* If we scroll upward and the non-visible top part
3687 * is smaller than the scroll arrow it would be
3688 * pretty stupid to show the arrow and taking more
3689 * screen space than just scrolling to the top.
3692 if ((step < 0) && (offset < arrow_border.top))
3695 /* Don't scroll over the top if we weren't before: */
3696 if ((menu->scroll_offset >= 0) && (offset < 0))
3699 view_height = gdk_window_get_height (gtk_widget_get_window (widget));
3701 if (menu->scroll_offset == 0 &&
3702 view_height >= priv->requested_height)
3705 /* Don't scroll past the bottom if we weren't before: */
3706 if (menu->scroll_offset > 0)
3707 view_height -= arrow_border.top;
3709 /* When both arrows are always shown, reduce
3710 * view height even more.
3713 view_height -= arrow_border.bottom;
3715 if ((menu->scroll_offset + view_height <= priv->requested_height) &&
3716 (offset + view_height > priv->requested_height))
3717 offset = priv->requested_height - view_height;
3719 if (offset != menu->scroll_offset)
3720 gtk_menu_scroll_to (menu, offset);
3724 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3725 gboolean touchscreen_mode)
3727 gboolean upper_visible;
3728 gboolean lower_visible;
3730 upper_visible = menu->upper_arrow_visible;
3731 lower_visible = menu->lower_arrow_visible;
3733 gtk_menu_scroll_by (menu, menu->scroll_step);
3735 if (touchscreen_mode &&
3736 (upper_visible != menu->upper_arrow_visible ||
3737 lower_visible != menu->lower_arrow_visible))
3739 /* We are about to hide a scroll arrow while the mouse is pressed,
3740 * this would cause the uncovered menu item to be activated on button
3741 * release. Therefore we need to ignore button release here
3743 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3744 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3749 gtk_menu_scroll_timeout (gpointer data)
3752 gboolean touchscreen_mode;
3754 menu = GTK_MENU (data);
3756 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3757 "gtk-touchscreen-mode", &touchscreen_mode,
3760 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3766 gtk_menu_scroll_timeout_initial (gpointer data)
3770 gboolean touchscreen_mode;
3772 menu = GTK_MENU (data);
3774 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3775 "gtk-timeout-repeat", &timeout,
3776 "gtk-touchscreen-mode", &touchscreen_mode,
3779 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3781 gtk_menu_remove_scroll_timeout (menu);
3783 menu->timeout_id = gdk_threads_add_timeout (timeout,
3784 gtk_menu_scroll_timeout,
3791 gtk_menu_start_scrolling (GtkMenu *menu)
3794 gboolean touchscreen_mode;
3796 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3797 "gtk-timeout-repeat", &timeout,
3798 "gtk-touchscreen-mode", &touchscreen_mode,
3801 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3803 menu->timeout_id = gdk_threads_add_timeout (timeout,
3804 gtk_menu_scroll_timeout_initial,
3809 gtk_menu_scroll (GtkWidget *widget,
3810 GdkEventScroll *event)
3812 GtkMenu *menu = GTK_MENU (widget);
3814 switch (event->direction)
3816 case GDK_SCROLL_RIGHT:
3817 case GDK_SCROLL_DOWN:
3818 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3820 case GDK_SCROLL_LEFT:
3822 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3830 get_arrows_sensitive_area (GtkMenu *menu,
3831 GdkRectangle *upper,
3832 GdkRectangle *lower)
3834 GtkArrowPlacement arrow_placement;
3835 GtkWidget *widget = GTK_WIDGET (menu);
3839 guint vertical_padding;
3841 gint scroll_arrow_height;
3843 window = gtk_widget_get_window (widget);
3844 width = gdk_window_get_width (window);
3845 height = gdk_window_get_height (window);
3847 gtk_widget_style_get (widget,
3848 "vertical-padding", &vertical_padding,
3849 "scroll-arrow-vlength", &scroll_arrow_height,
3850 "arrow-placement", &arrow_placement,
3853 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) +
3854 gtk_widget_get_style (widget)->ythickness + vertical_padding;
3856 gdk_window_get_position (window, &win_x, &win_y);
3858 switch (arrow_placement)
3860 case GTK_ARROWS_BOTH:
3865 upper->width = width;
3866 upper->height = scroll_arrow_height + border;
3872 lower->y = win_y + height - border - scroll_arrow_height;
3873 lower->width = width;
3874 lower->height = scroll_arrow_height + border;
3878 case GTK_ARROWS_START:
3883 upper->width = width / 2;
3884 upper->height = scroll_arrow_height + border;
3889 lower->x = win_x + width / 2;
3891 lower->width = width / 2;
3892 lower->height = scroll_arrow_height + border;
3896 case GTK_ARROWS_END:
3900 upper->y = win_y + height - border - scroll_arrow_height;
3901 upper->width = width / 2;
3902 upper->height = scroll_arrow_height + border;
3907 lower->x = win_x + width / 2;
3908 lower->y = win_y + height - border - scroll_arrow_height;
3909 lower->width = width / 2;
3910 lower->height = scroll_arrow_height + border;
3918 gtk_menu_handle_scrolling (GtkMenu *menu,
3924 GtkMenuShell *menu_shell;
3925 GtkMenuPrivate *priv;
3928 gboolean scroll_fast = FALSE;
3930 gboolean touchscreen_mode;
3932 priv = gtk_menu_get_private (menu);
3934 menu_shell = GTK_MENU_SHELL (menu);
3936 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3937 "gtk-touchscreen-mode", &touchscreen_mode,
3940 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3945 /* upper arrow handling */
3947 get_arrows_sensitive_area (menu, &rect, NULL);
3950 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3951 (x >= rect.x) && (x < rect.x + rect.width) &&
3952 (y >= rect.y) && (y < rect.y + rect.height))
3957 if (touchscreen_mode)
3958 menu->upper_arrow_prelight = in_arrow;
3960 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3962 gboolean arrow_pressed = FALSE;
3964 if (menu->upper_arrow_visible && !menu->tearoff_active)
3966 if (touchscreen_mode)
3968 if (enter && menu->upper_arrow_prelight)
3970 if (menu->timeout_id == 0)
3972 /* Deselect the active item so that
3973 * any submenus are popped down
3975 gtk_menu_shell_deselect (menu_shell);
3977 gtk_menu_remove_scroll_timeout (menu);
3978 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3982 /* Only do stuff on click. */
3983 gtk_menu_start_scrolling (menu);
3984 arrow_pressed = TRUE;
3989 arrow_pressed = TRUE;
3994 gtk_menu_stop_scrolling (menu);
3997 else /* !touchscreen_mode */
3999 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
4001 if (enter && in_arrow &&
4002 (!menu->upper_arrow_prelight ||
4003 menu->scroll_fast != scroll_fast))
4005 menu->upper_arrow_prelight = TRUE;
4006 menu->scroll_fast = scroll_fast;
4008 /* Deselect the active item so that
4009 * any submenus are popped down
4011 gtk_menu_shell_deselect (menu_shell);
4013 gtk_menu_remove_scroll_timeout (menu);
4014 menu->scroll_step = scroll_fast ?
4015 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
4018 gdk_threads_add_timeout (scroll_fast ?
4019 MENU_SCROLL_TIMEOUT2 :
4020 MENU_SCROLL_TIMEOUT1,
4021 gtk_menu_scroll_timeout, menu);
4023 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
4025 gtk_menu_stop_scrolling (menu);
4030 /* gtk_menu_start_scrolling() might have hit the top of the
4031 * menu, so check if the button isn't insensitive before
4032 * changing it to something else.
4034 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
4036 GtkStateType arrow_state = GTK_STATE_NORMAL;
4039 arrow_state = GTK_STATE_ACTIVE;
4040 else if (menu->upper_arrow_prelight)
4041 arrow_state = GTK_STATE_PRELIGHT;
4043 if (arrow_state != priv->upper_arrow_state)
4045 priv->upper_arrow_state = arrow_state;
4047 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4053 /* lower arrow handling */
4055 get_arrows_sensitive_area (menu, NULL, &rect);
4058 if (menu->lower_arrow_visible && !menu->tearoff_active &&
4059 (x >= rect.x) && (x < rect.x + rect.width) &&
4060 (y >= rect.y) && (y < rect.y + rect.height))
4065 if (touchscreen_mode)
4066 menu->lower_arrow_prelight = in_arrow;
4068 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4070 gboolean arrow_pressed = FALSE;
4072 if (menu->lower_arrow_visible && !menu->tearoff_active)
4074 if (touchscreen_mode)
4076 if (enter && menu->lower_arrow_prelight)
4078 if (menu->timeout_id == 0)
4080 /* Deselect the active item so that
4081 * any submenus are popped down
4083 gtk_menu_shell_deselect (menu_shell);
4085 gtk_menu_remove_scroll_timeout (menu);
4086 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4090 /* Only do stuff on click. */
4091 gtk_menu_start_scrolling (menu);
4092 arrow_pressed = TRUE;
4097 arrow_pressed = TRUE;
4102 gtk_menu_stop_scrolling (menu);
4105 else /* !touchscreen_mode */
4107 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4109 if (enter && in_arrow &&
4110 (!menu->lower_arrow_prelight ||
4111 menu->scroll_fast != scroll_fast))
4113 menu->lower_arrow_prelight = TRUE;
4114 menu->scroll_fast = scroll_fast;
4116 /* Deselect the active item so that
4117 * any submenus are popped down
4119 gtk_menu_shell_deselect (menu_shell);
4121 gtk_menu_remove_scroll_timeout (menu);
4122 menu->scroll_step = scroll_fast ?
4123 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
4126 gdk_threads_add_timeout (scroll_fast ?
4127 MENU_SCROLL_TIMEOUT2 :
4128 MENU_SCROLL_TIMEOUT1,
4129 gtk_menu_scroll_timeout, menu);
4131 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
4133 gtk_menu_stop_scrolling (menu);
4138 /* gtk_menu_start_scrolling() might have hit the bottom of the
4139 * menu, so check if the button isn't insensitive before
4140 * changing it to something else.
4142 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4144 GtkStateType arrow_state = GTK_STATE_NORMAL;
4147 arrow_state = GTK_STATE_ACTIVE;
4148 else if (menu->lower_arrow_prelight)
4149 arrow_state = GTK_STATE_PRELIGHT;
4151 if (arrow_state != priv->lower_arrow_state)
4153 priv->lower_arrow_state = arrow_state;
4155 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4163 gtk_menu_enter_notify (GtkWidget *widget,
4164 GdkEventCrossing *event)
4166 GtkWidget *menu_item;
4168 gboolean touchscreen_mode;
4170 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4171 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4172 event->mode == GDK_CROSSING_STATE_CHANGED)
4175 g_object_get (gtk_widget_get_settings (widget),
4176 "gtk-touchscreen-mode", &touchscreen_mode,
4179 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4180 if (GTK_IS_MENU (widget))
4182 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4184 if (!menu_shell->ignore_enter)
4185 gtk_menu_handle_scrolling (GTK_MENU (widget),
4186 event->x_root, event->y_root, TRUE, TRUE);
4189 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4191 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4193 if (GTK_IS_MENU (menu))
4195 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
4196 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4198 if (priv->seen_item_enter)
4200 /* This is the second enter we see for an item
4201 * on this menu. This means a release should always
4204 menu_shell->activate_time = 0;
4206 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4207 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4209 if (definitely_within_item (menu_item, event->x, event->y))
4211 /* This is an actual user-enter (ie. not a pop-under)
4212 * In this case, the user must either have entered
4213 * sufficiently far enough into the item, or he must move
4214 * far enough away from the enter point. (see
4215 * gtk_menu_motion_notify())
4217 menu_shell->activate_time = 0;
4221 priv->seen_item_enter = TRUE;
4225 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4226 * will not correspond to the event widget's parent. Check to see
4227 * if we are in the parent's navigation region.
4229 parent = gtk_widget_get_parent (menu_item);
4230 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4231 gtk_menu_navigating_submenu (GTK_MENU (parent),
4232 event->x_root, event->y_root))
4235 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4239 gtk_menu_leave_notify (GtkWidget *widget,
4240 GdkEventCrossing *event)
4242 GtkMenuShell *menu_shell;
4244 GtkMenuItem *menu_item;
4245 GtkWidget *event_widget;
4247 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4248 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4249 event->mode == GDK_CROSSING_STATE_CHANGED)
4252 menu = GTK_MENU (widget);
4253 menu_shell = GTK_MENU_SHELL (widget);
4255 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4258 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4260 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4262 if (!GTK_IS_MENU_ITEM (event_widget))
4265 menu_item = GTK_MENU_ITEM (event_widget);
4267 /* Here we check to see if we're leaving an active menu item with a submenu,
4268 * in which case we enter submenu navigation mode.
4270 if (menu_shell->active_menu_item != NULL
4271 && menu_item->submenu != NULL
4272 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
4274 if (GTK_MENU_SHELL (menu_item->submenu)->active)
4276 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4279 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
4281 /* We are leaving an active menu item with nonactive submenu.
4282 * Deselect it so we don't surprise the user with by popping
4283 * up a submenu _after_ he left the item.
4285 gtk_menu_shell_deselect (menu_shell);
4290 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4294 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4296 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4298 priv->navigation_x = 0;
4299 priv->navigation_y = 0;
4300 priv->navigation_width = 0;
4301 priv->navigation_height = 0;
4303 if (menu->navigation_timeout)
4305 g_source_remove (menu->navigation_timeout);
4306 menu->navigation_timeout = 0;
4310 /* When the timeout is elapsed, the navigation region is destroyed
4311 * and the menuitem under the pointer (if any) is selected.
4314 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4316 GtkMenuPopdownData *popdown_data = user_data;
4317 GtkMenu *menu = popdown_data->menu;
4318 GdkWindow *child_window;
4320 gtk_menu_stop_navigating_submenu (menu);
4322 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4324 child_window = gdk_window_get_device_position (menu->bin_window,
4325 popdown_data->device,
4330 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4332 send_event->crossing.window = g_object_ref (child_window);
4333 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4334 send_event->crossing.send_event = TRUE;
4335 gdk_event_set_device (send_event, popdown_data->device);
4337 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4339 gdk_event_free (send_event);
4347 gtk_menu_navigating_submenu (GtkMenu *menu,
4351 GtkMenuPrivate *priv;
4354 if (!gtk_menu_has_navigation_triangle (menu))
4357 priv = gtk_menu_get_private (menu);
4358 width = priv->navigation_width;
4359 height = priv->navigation_height;
4361 /* check if x/y are in the triangle spanned by the navigation parameters */
4363 /* 1) Move the coordinates so the triangle starts at 0,0 */
4364 event_x -= priv->navigation_x;
4365 event_y -= priv->navigation_y;
4367 /* 2) Ensure both legs move along the positive axis */
4379 /* 3) Check that the given coordinate is inside the triangle. The formula
4380 * is a transformed form of this formula: x/w + y/h <= 1
4382 if (event_x >= 0 && event_y >= 0 &&
4383 event_x * height + event_y * width <= width * height)
4389 gtk_menu_stop_navigating_submenu (menu);
4395 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4396 GtkMenuItem *menu_item,
4397 GdkEventCrossing *event)
4399 gint submenu_left = 0;
4400 gint submenu_right = 0;
4401 gint submenu_top = 0;
4402 gint submenu_bottom = 0;
4405 GtkWidget *event_widget;
4406 GtkMenuPopdownData *popdown_data;
4407 GtkMenuPrivate *priv;
4410 g_return_if_fail (menu_item->submenu != NULL);
4411 g_return_if_fail (event != NULL);
4413 priv = gtk_menu_get_private (menu);
4415 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4417 window = gtk_widget_get_window (menu_item->submenu);
4418 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4420 submenu_right = submenu_left + gdk_window_get_width (window);
4421 submenu_bottom = submenu_top + gdk_window_get_height (window);
4423 width = gdk_window_get_width (gtk_widget_get_window (event_widget));
4424 height = gdk_window_get_height (gtk_widget_get_window (event_widget));
4426 if (event->x >= 0 && event->x < width)
4430 gtk_menu_stop_navigating_submenu (menu);
4432 /* The navigation region is the triangle closest to the x/y
4433 * location of the rectangle. This is why the width or height
4437 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
4440 priv->navigation_x = submenu_left;
4441 priv->navigation_width = event->x_root - submenu_left;
4446 priv->navigation_x = submenu_right;
4447 priv->navigation_width = event->x_root - submenu_right;
4453 priv->navigation_y = event->y_root;
4454 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4456 if (priv->navigation_height >= 0)
4462 priv->navigation_y = event->y_root;
4463 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4465 if (priv->navigation_height <= 0)
4469 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4470 "gtk-menu-popdown-delay", &popdown_delay,
4473 popdown_data = g_new (GtkMenuPopdownData, 1);
4474 popdown_data->menu = menu;
4475 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4477 menu->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4479 gtk_menu_stop_navigating_submenu_cb,
4481 (GDestroyNotify) g_free);
4486 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4490 g_return_if_fail (GTK_IS_MENU (menu_shell));
4492 parent = menu_shell->parent_menu_shell;
4494 menu_shell->activate_time = 0;
4495 gtk_menu_popdown (GTK_MENU (menu_shell));
4498 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4502 gtk_menu_position (GtkMenu *menu)
4505 GtkRequisition requisition;
4506 GtkMenuPrivate *private;
4511 GdkScreen *pointer_screen;
4512 GdkRectangle monitor;
4515 g_return_if_fail (GTK_IS_MENU (menu));
4517 widget = GTK_WIDGET (menu);
4519 screen = gtk_widget_get_screen (widget);
4520 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4521 gdk_display_get_device_state (gdk_screen_get_display (screen),
4522 pointer, &pointer_screen, &x, &y, NULL);
4524 /* Get the minimum height for minimum width to figure out
4525 * the right place to popup the menu.
4527 gtk_widget_get_preferred_size (widget, &requisition, NULL);
4529 if (pointer_screen != screen)
4531 /* Pointer is on a different screen; roughly center the
4532 * menu on the screen. If someone was using multiscreen
4533 * + Xinerama together they'd probably want something
4534 * fancier; but that is likely to be vanishingly rare.
4536 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4537 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4540 private = gtk_menu_get_private (menu);
4541 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4543 private->initially_pushed_in = FALSE;
4545 /* Set the type hint here to allow custom position functions to set a different hint */
4546 if (!gtk_widget_get_visible (menu->toplevel))
4547 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4549 if (menu->position_func)
4551 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
4552 menu->position_func_data);
4554 if (private->monitor_num < 0)
4555 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4557 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4561 GtkStyle *style = gtk_widget_get_style (widget);
4562 gint space_left, space_right, space_above, space_below;
4565 gint xthickness = style->xthickness;
4566 gint ythickness = style->ythickness;
4567 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4569 /* The placement of popup menus horizontally works like this (with
4570 * RTL in parentheses)
4572 * - If there is enough room to the right (left) of the mouse cursor,
4573 * position the menu there.
4575 * - Otherwise, if if there is enough room to the left (right) of the
4576 * mouse cursor, position the menu there.
4578 * - Otherwise if the menu is smaller than the monitor, position it
4579 * on the side of the mouse cursor that has the most space available
4581 * - Otherwise (if there is simply not enough room for the menu on the
4582 * monitor), position it as far left (right) as possible.
4584 * Positioning in the vertical direction is similar: first try below
4585 * mouse cursor, then above.
4587 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4589 space_left = x - monitor.x;
4590 space_right = monitor.x + monitor.width - x - 1;
4591 space_above = y - monitor.y;
4592 space_below = monitor.y + monitor.height - y - 1;
4594 /* position horizontally */
4596 /* the amount of space we need to position the menu. Note the
4597 * menu is offset "xthickness" pixels
4599 needed_width = requisition.width - xthickness;
4601 if (needed_width <= space_left ||
4602 needed_width <= space_right)
4604 if ((rtl && needed_width <= space_left) ||
4605 (!rtl && needed_width > space_right))
4608 x = x + xthickness - requisition.width + 1;
4612 /* position right */
4616 /* x is clamped on-screen further down */
4618 else if (requisition.width <= monitor.width)
4620 /* the menu is too big to fit on either side of the mouse
4621 * cursor, but smaller than the monitor. Position it on
4622 * the side that has the most space
4624 if (space_left > space_right)
4632 x = monitor.x + monitor.width - requisition.width;
4635 else /* menu is simply too big for the monitor */
4640 x = monitor.x + monitor.width - requisition.width;
4649 /* Position vertically. The algorithm is the same as above, but
4650 * simpler because we don't have to take RTL into account.
4652 needed_height = requisition.height - ythickness;
4654 if (needed_height <= space_above ||
4655 needed_height <= space_below)
4657 if (needed_height <= space_below)
4660 y = y + ythickness - requisition.height + 1;
4662 y = CLAMP (y, monitor.y,
4663 monitor.y + monitor.height - requisition.height);
4665 else if (needed_height > space_below && needed_height > space_above)
4667 if (space_below >= space_above)
4668 y = monitor.y + monitor.height - requisition.height;
4680 if (private->initially_pushed_in)
4682 menu_height = requisition.height;
4684 if (y + menu_height > monitor.y + monitor.height)
4686 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4687 y = (monitor.y + monitor.height) - menu_height;
4692 scroll_offset += monitor.y - y;
4697 /* FIXME: should this be done in the various position_funcs ? */
4698 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4700 if (GTK_MENU_SHELL (menu)->active)
4702 private->have_position = TRUE;
4707 if (y + requisition.height > monitor.y + monitor.height)
4708 requisition.height = (monitor.y + monitor.height) - y;
4712 scroll_offset += monitor.y - y;
4713 requisition.height -= monitor.y - y;
4717 if (scroll_offset > 0)
4719 GtkBorder arrow_border;
4721 get_arrows_border (menu, &arrow_border);
4722 scroll_offset += arrow_border.top;
4725 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
4728 if (!GTK_MENU_SHELL (menu)->active)
4730 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4731 requisition.width, requisition.height);
4734 menu->scroll_offset = scroll_offset;
4738 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4740 if (menu->timeout_id)
4742 g_source_remove (menu->timeout_id);
4743 menu->timeout_id = 0;
4748 gtk_menu_stop_scrolling (GtkMenu *menu)
4750 gboolean touchscreen_mode;
4752 gtk_menu_remove_scroll_timeout (menu);
4754 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4755 "gtk-touchscreen-mode", &touchscreen_mode,
4758 if (!touchscreen_mode)
4760 menu->upper_arrow_prelight = FALSE;
4761 menu->lower_arrow_prelight = FALSE;
4766 gtk_menu_scroll_to (GtkMenu *menu,
4769 GtkMenuPrivate *priv;
4770 GtkAllocation allocation;
4771 GtkBorder arrow_border;
4775 gint view_width, view_height;
4778 guint vertical_padding;
4779 guint horizontal_padding;
4780 gboolean double_arrows;
4782 widget = GTK_WIDGET (menu);
4783 priv = gtk_menu_get_private (menu);
4785 if (menu->tearoff_active &&
4786 menu->tearoff_adjustment &&
4787 (menu->tearoff_adjustment->value != offset))
4789 menu->tearoff_adjustment->value =
4791 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4792 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4795 /* Move/resize the viewport according to arrows: */
4796 gtk_widget_get_allocation (widget, &allocation);
4797 view_width = allocation.width;
4798 view_height = allocation.height;
4800 style = gtk_widget_get_style (widget);
4802 gtk_widget_style_get (GTK_WIDGET (menu),
4803 "vertical-padding", &vertical_padding,
4804 "horizontal-padding", &horizontal_padding,
4807 double_arrows = get_double_arrows (menu);
4809 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4811 view_width -= (border_width + style->xthickness + horizontal_padding) * 2;
4812 view_height -= (border_width + style->ythickness + vertical_padding) * 2;
4813 menu_height = priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2;
4815 x = border_width + style->xthickness + horizontal_padding;
4816 y = border_width + style->ythickness + vertical_padding;
4818 if (double_arrows && !menu->tearoff_active)
4820 if (view_height < menu_height ||
4821 (offset > 0 && menu->scroll_offset > 0) ||
4822 (offset < 0 && menu->scroll_offset < 0))
4824 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4825 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4826 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4828 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4829 gtk_widget_queue_draw (GTK_WIDGET (menu));
4831 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4833 get_arrows_border (menu, &arrow_border);
4834 y += arrow_border.top;
4835 view_height -= arrow_border.top;
4836 view_height -= arrow_border.bottom;
4839 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4840 else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4841 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4842 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4844 if (offset >= menu_height - view_height)
4845 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4846 else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4847 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4848 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4850 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4851 (priv->lower_arrow_state != lower_arrow_previous_state))
4852 gtk_widget_queue_draw (GTK_WIDGET (menu));
4854 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4855 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4857 /* At the upper border, possibly remove timeout */
4858 if (menu->scroll_step < 0)
4860 gtk_menu_stop_scrolling (menu);
4861 gtk_widget_queue_draw (GTK_WIDGET (menu));
4865 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4866 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4868 /* At the lower border, possibly remove timeout */
4869 if (menu->scroll_step > 0)
4871 gtk_menu_stop_scrolling (menu);
4872 gtk_widget_queue_draw (GTK_WIDGET (menu));
4876 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4880 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4881 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4883 gtk_menu_stop_scrolling (menu);
4884 gtk_widget_queue_draw (GTK_WIDGET (menu));
4887 else if (!menu->tearoff_active)
4889 gboolean last_visible;
4891 last_visible = menu->upper_arrow_visible;
4892 menu->upper_arrow_visible = offset > 0;
4894 /* upper_arrow_visible may have changed, so requery the border */
4895 get_arrows_border (menu, &arrow_border);
4896 view_height -= arrow_border.top;
4898 if ((last_visible != menu->upper_arrow_visible) &&
4899 !menu->upper_arrow_visible)
4901 menu->upper_arrow_prelight = FALSE;
4903 /* If we hid the upper arrow, possibly remove timeout */
4904 if (menu->scroll_step < 0)
4906 gtk_menu_stop_scrolling (menu);
4907 gtk_widget_queue_draw (GTK_WIDGET (menu));
4911 last_visible = menu->lower_arrow_visible;
4912 menu->lower_arrow_visible = offset < menu_height - view_height;
4914 /* lower_arrow_visible may have changed, so requery the border */
4915 get_arrows_border (menu, &arrow_border);
4916 view_height -= arrow_border.bottom;
4918 if ((last_visible != menu->lower_arrow_visible) &&
4919 !menu->lower_arrow_visible)
4921 menu->lower_arrow_prelight = FALSE;
4923 /* If we hid the lower arrow, possibly remove timeout */
4924 if (menu->scroll_step > 0)
4926 gtk_menu_stop_scrolling (menu);
4927 gtk_widget_queue_draw (GTK_WIDGET (menu));
4931 y += arrow_border.top;
4934 /* Scroll the menu: */
4935 if (gtk_widget_get_realized (widget))
4936 gdk_window_move (menu->bin_window, 0, -offset);
4938 if (gtk_widget_get_realized (widget))
4939 gdk_window_move_resize (menu->view_window,
4945 menu->scroll_offset = offset;
4949 compute_child_offset (GtkMenu *menu,
4950 GtkWidget *menu_item,
4953 gboolean *is_last_child)
4955 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4956 gint item_top_attach;
4957 gint item_bottom_attach;
4958 gint child_offset = 0;
4961 get_effective_child_attach (menu_item, NULL, NULL,
4962 &item_top_attach, &item_bottom_attach);
4964 /* there is a possibility that we get called before _size_request, so
4965 * check the height table for safety.
4967 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4970 /* when we have a row with only invisible children, it's height will
4971 * be zero, so there's no need to check WIDGET_VISIBLE here
4973 for (i = 0; i < item_top_attach; i++)
4974 child_offset += priv->heights[i];
4977 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4979 *offset = child_offset;
4981 *height = priv->heights[item_top_attach];
4987 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4988 GtkWidget *menu_item)
4990 GtkMenu *menu = GTK_MENU (menu_shell);
4991 GtkWidget *widget = GTK_WIDGET (menu_shell);
4992 gint child_offset, child_height;
4996 gboolean last_child = 0;
4998 /* We need to check if the selected item fully visible.
4999 * If not we need to scroll the menu so that it becomes fully
5003 if (compute_child_offset (menu, menu_item,
5004 &child_offset, &child_height, &last_child))
5006 guint vertical_padding;
5007 gboolean double_arrows;
5009 y = menu->scroll_offset;
5010 width = gdk_window_get_width (gtk_widget_get_window (widget));
5011 height = gdk_window_get_height (gtk_widget_get_window (widget));
5013 gtk_widget_style_get (widget,
5014 "vertical-padding", &vertical_padding,
5017 double_arrows = get_double_arrows (menu);
5019 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5020 2 * gtk_widget_get_style (widget)->ythickness +
5021 2 * vertical_padding;
5022 if (child_offset < y)
5024 /* Ignore the enter event we might get if the pointer is on the menu
5026 menu_shell->ignore_enter = TRUE;
5027 gtk_menu_scroll_to (menu, child_offset);
5031 GtkBorder arrow_border;
5035 get_arrows_border (menu, &arrow_border);
5036 if (!menu->tearoff_active)
5037 arrow_height = arrow_border.top + arrow_border.bottom;
5039 if (child_offset + child_height > y + height - arrow_height)
5042 if ((!last_child && !menu->tearoff_active) || double_arrows)
5043 arrow_height += arrow_border.bottom;
5045 y = child_offset + child_height - height + arrow_height;
5046 if (((y > 0) && !menu->tearoff_active) || double_arrows)
5048 /* Need upper arrow */
5049 arrow_height += arrow_border.top;
5050 y = child_offset + child_height - height + arrow_height;
5052 /* Ignore the enter event we might get if the pointer is on the menu
5054 menu_shell->ignore_enter = TRUE;
5055 gtk_menu_scroll_to (menu, y);
5063 gtk_menu_select_item (GtkMenuShell *menu_shell,
5064 GtkWidget *menu_item)
5066 GtkMenu *menu = GTK_MENU (menu_shell);
5068 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5069 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5071 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5075 /* Reparent the menu, taking care of the refcounting
5077 * If unrealize is true we force a unrealize while reparenting the parent.
5078 * This can help eliminate flicker in some cases.
5080 * What happens is that when the menu is unrealized and then re-realized,
5081 * the allocations are as follows:
5083 * parent - 1x1 at (0,0)
5084 * child1 - 100x20 at (0,0)
5085 * child2 - 100x20 at (0,20)
5086 * child3 - 100x20 at (0,40)
5088 * That is, the parent is small but the children are full sized. Then,
5089 * when the queued_resize gets processed, the parent gets resized to
5092 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5093 * contains the following logic:
5095 * - if a move or resize operation on a window would change the clip
5096 * region on the children, then before the window is resized
5097 * the background for children is temporarily set to None, the
5098 * move/resize done, and the background for the children restored.
5100 * So, at the point where the parent is resized to final size, the
5101 * background for the children is temporarily None, and thus they
5102 * are not cleared to the background color and the previous background
5103 * (the image of the menu) is left in place.
5106 gtk_menu_reparent (GtkMenu *menu,
5107 GtkWidget *new_parent,
5110 GObject *object = G_OBJECT (menu);
5111 GtkWidget *widget = GTK_WIDGET (menu);
5112 gboolean was_floating = g_object_is_floating (object);
5114 g_object_ref_sink (object);
5118 g_object_ref (object);
5119 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5120 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5121 g_object_unref (object);
5124 gtk_widget_reparent (widget, new_parent);
5127 g_object_force_floating (object);
5129 g_object_unref (object);
5133 gtk_menu_show_all (GtkWidget *widget)
5135 /* Show children, but not self. */
5136 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5141 gtk_menu_hide_all (GtkWidget *widget)
5143 /* Hide children, but not self. */
5144 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
5148 * gtk_menu_set_screen:
5149 * @menu: a #GtkMenu.
5150 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5151 * determined by the widget the menu is attached to.
5153 * Sets the #GdkScreen on which the menu will be displayed.
5158 gtk_menu_set_screen (GtkMenu *menu,
5161 g_return_if_fail (GTK_IS_MENU (menu));
5162 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
5164 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5168 menu_change_screen (menu, screen);
5172 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5174 attach_widget_screen_changed (attach_widget, NULL, menu);
5180 * @menu: a #GtkMenu.
5181 * @child: a #GtkMenuItem.
5182 * @left_attach: The column number to attach the left side of the item to.
5183 * @right_attach: The column number to attach the right side of the item to.
5184 * @top_attach: The row number to attach the top of the item to.
5185 * @bottom_attach: The row number to attach the bottom of the item to.
5187 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5188 * an item will occupy is specified by @left_attach, @right_attach,
5189 * @top_attach and @bottom_attach. These each represent the leftmost,
5190 * rightmost, uppermost and lower column and row numbers of the table.
5191 * (Columns and rows are indexed from zero).
5193 * Note that this function is not related to gtk_menu_detach().
5198 gtk_menu_attach (GtkMenu *menu,
5203 guint bottom_attach)
5205 GtkMenuShell *menu_shell;
5208 g_return_if_fail (GTK_IS_MENU (menu));
5209 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5210 parent = gtk_widget_get_parent (child);
5211 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5212 g_return_if_fail (left_attach < right_attach);
5213 g_return_if_fail (top_attach < bottom_attach);
5215 menu_shell = GTK_MENU_SHELL (menu);
5219 AttachInfo *ai = get_attach_info (child);
5221 ai->left_attach = left_attach;
5222 ai->right_attach = right_attach;
5223 ai->top_attach = top_attach;
5224 ai->bottom_attach = bottom_attach;
5226 menu_shell->children = g_list_append (menu_shell->children, child);
5228 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5230 menu_queue_resize (menu);
5234 gtk_container_child_set (GTK_CONTAINER (parent), child,
5235 "left-attach", left_attach,
5236 "right-attach", right_attach,
5237 "top-attach", top_attach,
5238 "bottom-attach", bottom_attach,
5244 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5248 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5249 "gtk-menu-popup-delay", &popup_delay,
5256 find_child_containing (GtkMenuShell *menu_shell,
5264 /* find a child which includes the area given by
5265 * left, right, top, bottom.
5268 for (list = menu_shell->children; list; list = list->next)
5272 if (!_gtk_menu_item_is_selectable (list->data))
5275 get_effective_child_attach (list->data, &l, &r, &t, &b);
5277 if (l <= left && right <= r
5278 && t <= top && bottom <= b)
5279 return GTK_WIDGET (list->data);
5286 gtk_menu_move_current (GtkMenuShell *menu_shell,
5287 GtkMenuDirectionType direction)
5289 GtkMenu *menu = GTK_MENU (menu_shell);
5292 GtkWidget *match = NULL;
5294 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5298 case GTK_MENU_DIR_CHILD:
5299 direction = GTK_MENU_DIR_PARENT;
5301 case GTK_MENU_DIR_PARENT:
5302 direction = GTK_MENU_DIR_CHILD;
5308 /* use special table menu key bindings */
5309 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5311 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
5313 if (direction == GTK_MENU_DIR_NEXT)
5315 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5317 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5325 for (i = 0; i < t; i++)
5327 match = find_child_containing (menu_shell,
5328 l, l + 1, i, i + 1);
5334 else if (direction == GTK_MENU_DIR_PREV)
5336 for (i = t; i > 0; i--)
5338 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
5346 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5348 match = find_child_containing (menu_shell,
5349 l, l + 1, i - 1, i);
5355 else if (direction == GTK_MENU_DIR_PARENT)
5357 /* we go one left if possible */
5359 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
5363 GtkWidget *parent = menu_shell->parent_menu_shell;
5366 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
5367 match = menu_shell->active_menu_item;
5370 else if (direction == GTK_MENU_DIR_CHILD)
5372 /* we go one right if possible */
5373 if (r < gtk_menu_get_n_columns (menu))
5374 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5378 GtkWidget *parent = menu_shell->parent_menu_shell;
5380 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
5382 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
5383 match = menu_shell->active_menu_item;
5389 gtk_menu_shell_select_item (menu_shell, match);
5394 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5398 get_visible_size (GtkMenu *menu)
5400 GtkAllocation allocation;
5401 GtkWidget *widget = GTK_WIDGET (menu);
5402 GtkContainer *container = GTK_CONTAINER (menu);
5405 gtk_widget_get_allocation (widget, &allocation);
5406 menu_height = (allocation.height
5407 - 2 * (gtk_container_get_border_width (container)
5408 + gtk_widget_get_style (widget)->ythickness));
5410 if (!menu->tearoff_active)
5412 GtkBorder arrow_border;
5414 get_arrows_border (menu, &arrow_border);
5415 menu_height -= arrow_border.top;
5416 menu_height -= arrow_border.bottom;
5422 /* Find the sensitive on-screen child containing @y, or if none,
5423 * the nearest selectable onscreen child. (%NULL if none)
5426 child_at (GtkMenu *menu,
5429 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5430 GtkWidget *child = NULL;
5431 gint child_offset = 0;
5434 gint lower, upper; /* Onscreen bounds */
5436 menu_height = get_visible_size (menu);
5437 lower = menu->scroll_offset;
5438 upper = menu->scroll_offset + menu_height;
5440 for (children = menu_shell->children; children; children = children->next)
5442 if (gtk_widget_get_visible (children->data))
5444 GtkRequisition child_requisition;
5446 gtk_widget_get_preferred_size (children->data,
5447 &child_requisition, NULL);
5449 if (_gtk_menu_item_is_selectable (children->data) &&
5450 child_offset >= lower &&
5451 child_offset + child_requisition.height <= upper)
5453 child = children->data;
5455 if (child_offset + child_requisition.height > y &&
5456 !GTK_IS_TEAROFF_MENU_ITEM (child))
5460 child_offset += child_requisition.height;
5468 get_menu_height (GtkMenu *menu)
5470 GtkAllocation allocation;
5471 GtkWidget *widget = GTK_WIDGET (menu);
5474 gtk_widget_get_allocation (widget, &allocation);
5476 height = allocation.height;
5477 height -= gtk_container_get_border_width (GTK_CONTAINER (widget)) + gtk_widget_get_style (widget)->ythickness * 2;
5479 if (!menu->tearoff_active)
5481 GtkBorder arrow_border;
5483 get_arrows_border (menu, &arrow_border);
5484 height -= arrow_border.top;
5485 height -= arrow_border.bottom;
5492 gtk_menu_real_move_scroll (GtkMenu *menu,
5495 gint page_size = get_visible_size (menu);
5496 gint end_position = get_menu_height (menu);
5497 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5501 case GTK_SCROLL_PAGE_UP:
5502 case GTK_SCROLL_PAGE_DOWN:
5506 gint child_offset = 0;
5507 gboolean old_upper_arrow_visible;
5510 if (type == GTK_SCROLL_PAGE_UP)
5515 if (menu_shell->active_menu_item)
5519 compute_child_offset (menu, menu_shell->active_menu_item,
5520 &child_offset, &child_height, NULL);
5521 child_offset += child_height / 2;
5524 menu_shell->ignore_enter = TRUE;
5525 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5526 old_offset = menu->scroll_offset;
5528 new_offset = menu->scroll_offset + step;
5529 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5531 gtk_menu_scroll_to (menu, new_offset);
5533 if (menu_shell->active_menu_item)
5535 GtkWidget *new_child;
5536 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5537 GtkBorder arrow_border;
5539 get_arrows_border (menu, &arrow_border);
5541 if (menu->scroll_offset != old_offset)
5542 step = menu->scroll_offset - old_offset;
5544 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5546 new_child = child_at (menu, child_offset + step);
5548 gtk_menu_shell_select_item (menu_shell, new_child);
5552 case GTK_SCROLL_START:
5553 /* Ignore the enter event we might get if the pointer is on the menu
5555 menu_shell->ignore_enter = TRUE;
5556 gtk_menu_scroll_to (menu, 0);
5557 gtk_menu_shell_select_first (menu_shell, TRUE);
5559 case GTK_SCROLL_END:
5560 /* Ignore the enter event we might get if the pointer is on the menu
5562 menu_shell->ignore_enter = TRUE;
5563 gtk_menu_scroll_to (menu, end_position - page_size);
5564 _gtk_menu_shell_select_last (menu_shell, TRUE);
5573 * gtk_menu_set_monitor:
5575 * @monitor_num: the number of the monitor on which the menu should
5578 * Informs GTK+ on which monitor a menu should be popped up.
5579 * See gdk_screen_get_monitor_geometry().
5581 * This function should be called from a #GtkMenuPositionFunc if the
5582 * menu should not appear on the same monitor as the pointer. This
5583 * information can't be reliably inferred from the coordinates returned
5584 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
5585 * may extend beyond the monitor boundaries or even the screen boundaries.
5590 gtk_menu_set_monitor (GtkMenu *menu,
5593 GtkMenuPrivate *priv;
5594 g_return_if_fail (GTK_IS_MENU (menu));
5596 priv = gtk_menu_get_private (menu);
5598 priv->monitor_num = monitor_num;
5602 * gtk_menu_get_monitor:
5605 * Retrieves the number of the monitor on which to show the menu.
5607 * Returns: the number of the monitor on which the menu should
5608 * be popped up or -1, if no monitor has been set
5613 gtk_menu_get_monitor (GtkMenu *menu)
5615 GtkMenuPrivate *priv;
5616 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5618 priv = gtk_menu_get_private (menu);
5620 return priv->monitor_num;
5624 * gtk_menu_get_for_attach_widget:
5625 * @widget: a #GtkWidget
5627 * Returns a list of the menus which are attached to this widget.
5628 * This list is owned by GTK+ and must not be modified.
5630 * Return value: (element-type GtkWidget) (transfer none): the list of menus attached to his widget.
5635 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5639 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5641 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5646 gtk_menu_grab_notify (GtkWidget *widget,
5647 gboolean was_grabbed)
5649 GtkWidget *toplevel;
5650 GtkWindowGroup *group;
5654 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5657 !gtk_widget_device_is_shadowed (widget, pointer))
5660 toplevel = gtk_widget_get_toplevel (widget);
5662 if (!GTK_IS_WINDOW (toplevel))
5665 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5666 grab = gtk_window_group_get_current_device_grab (group, pointer);
5668 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
5669 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5673 * gtk_menu_set_reserve_toggle_size:
5675 * @reserve_toggle_size: whether to reserve size for toggles
5677 * Sets whether the menu should reserve space for drawing toggles
5678 * or icons, regardless of their actual presence.
5683 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5684 gboolean reserve_toggle_size)
5686 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5687 gboolean no_toggle_size;
5689 no_toggle_size = !reserve_toggle_size;
5691 if (priv->no_toggle_size != no_toggle_size)
5693 priv->no_toggle_size = no_toggle_size;
5695 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5700 * gtk_menu_get_reserve_toggle_size:
5703 * Returns whether the menu reserves space for toggles and
5704 * icons, regardless of their actual presence.
5706 * Returns: Whether the menu reserves toggle space
5711 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5713 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5715 return !priv->no_toggle_size;