1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gdk/gdkkeysyms.h"
30 #include "gtkaccellabel.h"
31 #include "gtkaccelmap.h"
32 #include "gtkbindings.h"
33 #include "gtkcheckmenuitem.h"
34 #include <gobject/gvaluecollector.h>
36 #include "gtkmarshalers.h"
38 #include "gtkmenuprivate.h"
39 #include "gtktearoffmenuitem.h"
40 #include "gtkwindow.h"
42 #include "gtkvscrollbar.h"
43 #include "gtksettings.h"
44 #include "gtksizerequest.h"
45 #include "gtkprivate.h"
48 #define DEFAULT_POPUP_DELAY 225
49 #define DEFAULT_POPDOWN_DELAY 1000
51 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
52 * extends below the submenu
55 #define MENU_SCROLL_STEP1 8
56 #define MENU_SCROLL_STEP2 15
57 #define MENU_SCROLL_FAST_ZONE 8
58 #define MENU_SCROLL_TIMEOUT1 50
59 #define MENU_SCROLL_TIMEOUT2 20
61 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
62 #define ATTACHED_MENUS "gtk-attached-menus"
64 typedef struct _GtkMenuAttachData GtkMenuAttachData;
65 typedef struct _GtkMenuPrivate GtkMenuPrivate;
66 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
68 struct _GtkMenuAttachData
70 GtkWidget *attach_widget;
71 GtkMenuDetachFunc detacher;
74 struct _GtkMenuPrivate
78 gboolean initially_pushed_in;
80 GDestroyNotify position_func_data_destroy;
82 /* info used for the table */
85 gint requested_height;
89 /* Cached layout information */
98 GtkStateType lower_arrow_state;
99 GtkStateType upper_arrow_state;
101 /* navigation region */
104 int navigation_width;
105 int navigation_height;
107 guint have_layout : 1;
108 guint seen_item_enter : 1;
109 guint have_position : 1;
110 guint ignore_button_release : 1;
111 guint no_toggle_size : 1;
114 struct _GtkMenuPopdownData
126 gint effective_left_attach;
127 gint effective_right_attach;
128 gint effective_top_attach;
129 gint effective_bottom_attach;
146 PROP_RESERVE_TOGGLE_SIZE
151 CHILD_PROP_LEFT_ATTACH,
152 CHILD_PROP_RIGHT_ATTACH,
153 CHILD_PROP_TOP_ATTACH,
154 CHILD_PROP_BOTTOM_ATTACH
157 static void gtk_menu_set_property (GObject *object,
161 static void gtk_menu_get_property (GObject *object,
165 static void gtk_menu_set_child_property(GtkContainer *container,
170 static void gtk_menu_get_child_property(GtkContainer *container,
175 static void gtk_menu_destroy (GtkObject *object);
176 static void gtk_menu_realize (GtkWidget *widget);
177 static void gtk_menu_unrealize (GtkWidget *widget);
178 static void gtk_menu_size_allocate (GtkWidget *widget,
179 GtkAllocation *allocation);
180 static void gtk_menu_paint (GtkWidget *widget,
181 GdkEventExpose *expose);
182 static void gtk_menu_show (GtkWidget *widget);
183 static gboolean gtk_menu_expose (GtkWidget *widget,
184 GdkEventExpose *event);
185 static gboolean gtk_menu_key_press (GtkWidget *widget,
187 static gboolean gtk_menu_scroll (GtkWidget *widget,
188 GdkEventScroll *event);
189 static gboolean gtk_menu_button_press (GtkWidget *widget,
190 GdkEventButton *event);
191 static gboolean gtk_menu_button_release (GtkWidget *widget,
192 GdkEventButton *event);
193 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
194 GdkEventMotion *event);
195 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
196 GdkEventCrossing *event);
197 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
198 GdkEventCrossing *event);
199 static void gtk_menu_scroll_to (GtkMenu *menu,
201 static void gtk_menu_grab_notify (GtkWidget *widget,
202 gboolean was_grabbed);
204 static void gtk_menu_stop_scrolling (GtkMenu *menu);
205 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
206 static gboolean gtk_menu_scroll_timeout (gpointer data);
207 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
208 static void gtk_menu_start_scrolling (GtkMenu *menu);
210 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
211 GtkWidget *menu_item);
212 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
213 GtkWidget *menu_item);
214 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
217 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
219 static void gtk_menu_handle_scrolling (GtkMenu *menu,
224 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
226 static void gtk_menu_style_set (GtkWidget *widget,
227 GtkStyle *previous_style);
228 static gboolean gtk_menu_focus (GtkWidget *widget,
229 GtkDirectionType direction);
230 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
231 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
232 GtkMenuDirectionType direction);
233 static void gtk_menu_real_move_scroll (GtkMenu *menu,
236 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
237 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
238 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
241 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
242 GtkMenuItem *menu_item,
243 GdkEventCrossing *event);
245 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
246 static void gtk_menu_show_all (GtkWidget *widget);
247 static void gtk_menu_hide_all (GtkWidget *widget);
248 static void gtk_menu_position (GtkMenu *menu);
249 static void gtk_menu_reparent (GtkMenu *menu,
250 GtkWidget *new_parent,
252 static void gtk_menu_remove (GtkContainer *menu,
255 static void gtk_menu_update_title (GtkMenu *menu);
257 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
258 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
260 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
262 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
263 gboolean group_changed);
265 static void gtk_menu_size_request_init (GtkSizeRequestIface *iface);
266 static void gtk_menu_get_width (GtkSizeRequest *widget,
269 static void gtk_menu_get_height (GtkSizeRequest *widget,
272 static void gtk_menu_get_height_for_width (GtkSizeRequest *widget,
278 static const gchar attach_data_key[] = "gtk-menu-attach-data";
280 static guint menu_signals[LAST_SIGNAL] = { 0 };
282 static GtkMenuPrivate *
283 gtk_menu_get_private (GtkMenu *menu)
285 return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
288 G_DEFINE_TYPE_WITH_CODE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL,
289 G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
290 gtk_menu_size_request_init))
293 menu_queue_resize (GtkMenu *menu)
295 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
297 priv->have_layout = FALSE;
298 gtk_widget_queue_resize (GTK_WIDGET (menu));
302 attach_info_free (AttachInfo *info)
304 g_slice_free (AttachInfo, info);
308 get_attach_info (GtkWidget *child)
310 GObject *object = G_OBJECT (child);
311 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
315 ai = g_slice_new0 (AttachInfo);
316 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
317 (GDestroyNotify) attach_info_free);
324 is_grid_attached (AttachInfo *ai)
326 return (ai->left_attach >= 0 &&
327 ai->right_attach >= 0 &&
328 ai->top_attach >= 0 &&
329 ai->bottom_attach >= 0);
333 menu_ensure_layout (GtkMenu *menu)
335 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
337 if (!priv->have_layout)
339 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
343 gint max_right_attach;
344 gint max_bottom_attach;
346 /* Find extents of gridded portion
348 max_right_attach = 1;
349 max_bottom_attach = 0;
351 for (l = menu_shell->children; l; l = l->next)
353 GtkWidget *child = l->data;
354 AttachInfo *ai = get_attach_info (child);
356 if (is_grid_attached (ai))
358 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
359 max_right_attach = MAX (max_right_attach, ai->right_attach);
365 row_occupied = g_malloc0 (max_bottom_attach);
367 for (l = menu_shell->children; l; l = l->next)
369 GtkWidget *child = l->data;
370 AttachInfo *ai = get_attach_info (child);
372 if (is_grid_attached (ai))
376 for (i = ai->top_attach; i < ai->bottom_attach; i++)
377 row_occupied[i] = TRUE;
381 /* Lay non-grid-items out in those rows
384 for (l = menu_shell->children; l; l = l->next)
386 GtkWidget *child = l->data;
387 AttachInfo *ai = get_attach_info (child);
389 if (!is_grid_attached (ai))
391 while (current_row < max_bottom_attach && row_occupied[current_row])
394 ai->effective_left_attach = 0;
395 ai->effective_right_attach = max_right_attach;
396 ai->effective_top_attach = current_row;
397 ai->effective_bottom_attach = current_row + 1;
403 ai->effective_left_attach = ai->left_attach;
404 ai->effective_right_attach = ai->right_attach;
405 ai->effective_top_attach = ai->top_attach;
406 ai->effective_bottom_attach = ai->bottom_attach;
410 g_free (row_occupied);
412 priv->n_rows = MAX (current_row, max_bottom_attach);
413 priv->n_columns = max_right_attach;
414 priv->have_layout = TRUE;
420 gtk_menu_get_n_columns (GtkMenu *menu)
422 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
424 menu_ensure_layout (menu);
426 return priv->n_columns;
430 gtk_menu_get_n_rows (GtkMenu *menu)
432 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
434 menu_ensure_layout (menu);
440 get_effective_child_attach (GtkWidget *child,
446 GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
449 menu_ensure_layout (menu);
451 ai = get_attach_info (child);
454 *l = ai->effective_left_attach;
456 *r = ai->effective_right_attach;
458 *t = ai->effective_top_attach;
460 *b = ai->effective_bottom_attach;
465 gtk_menu_class_init (GtkMenuClass *class)
467 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
468 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
469 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
470 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
471 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
472 GtkBindingSet *binding_set;
474 gobject_class->set_property = gtk_menu_set_property;
475 gobject_class->get_property = gtk_menu_get_property;
477 object_class->destroy = gtk_menu_destroy;
479 widget_class->realize = gtk_menu_realize;
480 widget_class->unrealize = gtk_menu_unrealize;
481 widget_class->size_allocate = gtk_menu_size_allocate;
482 widget_class->show = gtk_menu_show;
483 widget_class->expose_event = gtk_menu_expose;
484 widget_class->scroll_event = gtk_menu_scroll;
485 widget_class->key_press_event = gtk_menu_key_press;
486 widget_class->button_press_event = gtk_menu_button_press;
487 widget_class->button_release_event = gtk_menu_button_release;
488 widget_class->motion_notify_event = gtk_menu_motion_notify;
489 widget_class->show_all = gtk_menu_show_all;
490 widget_class->hide_all = gtk_menu_hide_all;
491 widget_class->enter_notify_event = gtk_menu_enter_notify;
492 widget_class->leave_notify_event = gtk_menu_leave_notify;
493 widget_class->style_set = gtk_menu_style_set;
494 widget_class->focus = gtk_menu_focus;
495 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
496 widget_class->grab_notify = gtk_menu_grab_notify;
498 container_class->remove = gtk_menu_remove;
499 container_class->get_child_property = gtk_menu_get_child_property;
500 container_class->set_child_property = gtk_menu_set_child_property;
502 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
503 menu_shell_class->deactivate = gtk_menu_deactivate;
504 menu_shell_class->select_item = gtk_menu_select_item;
505 menu_shell_class->insert = gtk_menu_real_insert;
506 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
507 menu_shell_class->move_current = gtk_menu_move_current;
509 menu_signals[MOVE_SCROLL] =
510 g_signal_new_class_handler (I_("move-scroll"),
511 G_OBJECT_CLASS_TYPE (object_class),
512 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
513 G_CALLBACK (gtk_menu_real_move_scroll),
515 _gtk_marshal_VOID__ENUM,
517 GTK_TYPE_SCROLL_TYPE);
522 * The index of the currently selected menu item, or -1 if no
523 * menu item is selected.
527 g_object_class_install_property (gobject_class,
529 g_param_spec_int ("active",
531 P_("The currently selected menu item"),
533 GTK_PARAM_READWRITE));
536 * GtkMenu:accel-group:
538 * The accel group holding accelerators for the menu.
542 g_object_class_install_property (gobject_class,
544 g_param_spec_object ("accel-group",
546 P_("The accel group holding accelerators for the menu"),
547 GTK_TYPE_ACCEL_GROUP,
548 GTK_PARAM_READWRITE));
551 * GtkMenu:accel-path:
553 * An accel path used to conveniently construct accel paths of child items.
557 g_object_class_install_property (gobject_class,
559 g_param_spec_string ("accel-path",
561 P_("An accel path used to conveniently construct accel paths of child items"),
563 GTK_PARAM_READWRITE));
566 * GtkMenu:attach-widget:
568 * The widget the menu is attached to. Setting this property attaches
569 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
570 * use gtk_menu_attach_to_widget() directly.
574 g_object_class_install_property (gobject_class,
576 g_param_spec_object ("attach-widget",
578 P_("The widget the menu is attached to"),
580 GTK_PARAM_READWRITE));
582 g_object_class_install_property (gobject_class,
584 g_param_spec_string ("tearoff-title",
586 P_("A title that may be displayed by the window manager when this menu is torn-off"),
588 GTK_PARAM_READWRITE));
591 * GtkMenu:tearoff-state:
593 * A boolean that indicates whether the menu is torn-off.
597 g_object_class_install_property (gobject_class,
599 g_param_spec_boolean ("tearoff-state",
601 P_("A boolean that indicates whether the menu is torn-off"),
603 GTK_PARAM_READWRITE));
608 * The monitor the menu will be popped up on.
612 g_object_class_install_property (gobject_class,
614 g_param_spec_int ("monitor",
616 P_("The monitor the menu will be popped up on"),
618 GTK_PARAM_READWRITE));
620 gtk_widget_class_install_style_property (widget_class,
621 g_param_spec_int ("vertical-padding",
622 P_("Vertical Padding"),
623 P_("Extra space at the top and bottom of the menu"),
627 GTK_PARAM_READABLE));
630 * GtkMenu:reserve-toggle-size:
632 * A boolean that indicates whether the menu reserves space for
633 * toggles and icons, regardless of their actual presence.
635 * This property should only be changed from its default value
636 * for special-purposes such as tabular menus. Regular menus that
637 * are connected to a menu bar or context menus should reserve
638 * toggle space for consistency.
642 g_object_class_install_property (gobject_class,
643 PROP_RESERVE_TOGGLE_SIZE,
644 g_param_spec_boolean ("reserve-toggle-size",
645 P_("Reserve Toggle Size"),
646 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
648 GTK_PARAM_READWRITE));
650 gtk_widget_class_install_style_property (widget_class,
651 g_param_spec_int ("horizontal-padding",
652 P_("Horizontal Padding"),
653 P_("Extra space at the left and right edges of the menu"),
657 GTK_PARAM_READABLE));
659 gtk_widget_class_install_style_property (widget_class,
660 g_param_spec_int ("vertical-offset",
661 P_("Vertical Offset"),
662 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
666 GTK_PARAM_READABLE));
668 gtk_widget_class_install_style_property (widget_class,
669 g_param_spec_int ("horizontal-offset",
670 P_("Horizontal Offset"),
671 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
675 GTK_PARAM_READABLE));
677 gtk_widget_class_install_style_property (widget_class,
678 g_param_spec_boolean ("double-arrows",
680 P_("When scrolling, always show both arrows."),
682 GTK_PARAM_READABLE));
685 * GtkMenu:arrow-placement:
687 * Indicates where scroll arrows should be placed.
691 gtk_widget_class_install_style_property (widget_class,
692 g_param_spec_enum ("arrow-placement",
693 P_("Arrow Placement"),
694 P_("Indicates where scroll arrows should be placed"),
695 GTK_TYPE_ARROW_PLACEMENT,
697 GTK_PARAM_READABLE));
699 gtk_container_class_install_child_property (container_class,
700 CHILD_PROP_LEFT_ATTACH,
701 g_param_spec_int ("left-attach",
703 P_("The column number to attach the left side of the child to"),
705 GTK_PARAM_READWRITE));
707 gtk_container_class_install_child_property (container_class,
708 CHILD_PROP_RIGHT_ATTACH,
709 g_param_spec_int ("right-attach",
711 P_("The column number to attach the right side of the child to"),
713 GTK_PARAM_READWRITE));
715 gtk_container_class_install_child_property (container_class,
716 CHILD_PROP_TOP_ATTACH,
717 g_param_spec_int ("top-attach",
719 P_("The row number to attach the top of the child to"),
721 GTK_PARAM_READWRITE));
723 gtk_container_class_install_child_property (container_class,
724 CHILD_PROP_BOTTOM_ATTACH,
725 g_param_spec_int ("bottom-attach",
727 P_("The row number to attach the bottom of the child to"),
729 GTK_PARAM_READWRITE));
732 * GtkMenu::arrow-scaling
734 * Arbitrary constant to scale down the size of the scroll arrow.
738 gtk_widget_class_install_style_property (widget_class,
739 g_param_spec_float ("arrow-scaling",
741 P_("Arbitrary constant to scale down the size of the scroll arrow"),
743 GTK_PARAM_READABLE));
745 binding_set = gtk_binding_set_by_class (class);
746 gtk_binding_entry_add_signal (binding_set,
748 I_("move-current"), 1,
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,
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,
775 GTK_MENU_DIR_PARENT);
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_MENU_DIRECTION_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,
809 GTK_TYPE_SCROLL_TYPE,
811 gtk_binding_entry_add_signal (binding_set,
812 GDK_KEY_KP_Page_Up, 0,
814 GTK_TYPE_SCROLL_TYPE,
816 gtk_binding_entry_add_signal (binding_set,
817 GDK_KEY_Page_Down, 0,
819 GTK_TYPE_SCROLL_TYPE,
820 GTK_SCROLL_PAGE_DOWN);
821 gtk_binding_entry_add_signal (binding_set,
822 GDK_KEY_KP_Page_Down, 0,
824 GTK_TYPE_SCROLL_TYPE,
825 GTK_SCROLL_PAGE_DOWN);
827 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
828 P_("Can change accelerators"),
829 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
831 GTK_PARAM_READWRITE));
833 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
834 P_("Delay before submenus appear"),
835 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
839 GTK_PARAM_READWRITE));
841 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
842 P_("Delay before hiding a submenu"),
843 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
846 DEFAULT_POPDOWN_DELAY,
847 GTK_PARAM_READWRITE));
849 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
854 gtk_menu_set_property (GObject *object,
859 GtkMenu *menu = GTK_MENU (object);
864 gtk_menu_set_active (menu, g_value_get_int (value));
866 case PROP_ACCEL_GROUP:
867 gtk_menu_set_accel_group (menu, g_value_get_object (value));
869 case PROP_ACCEL_PATH:
870 gtk_menu_set_accel_path (menu, g_value_get_string (value));
872 case PROP_ATTACH_WIDGET:
876 widget = gtk_menu_get_attach_widget (menu);
878 gtk_menu_detach (menu);
880 widget = (GtkWidget*) g_value_get_object (value);
882 gtk_menu_attach_to_widget (menu, widget, NULL);
885 case PROP_TEAROFF_STATE:
886 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
888 case PROP_TEAROFF_TITLE:
889 gtk_menu_set_title (menu, g_value_get_string (value));
892 gtk_menu_set_monitor (menu, g_value_get_int (value));
894 case PROP_RESERVE_TOGGLE_SIZE:
895 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
898 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
904 gtk_menu_get_property (GObject *object,
909 GtkMenu *menu = GTK_MENU (object);
914 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->children, gtk_menu_get_active (menu)));
916 case PROP_ACCEL_GROUP:
917 g_value_set_object (value, gtk_menu_get_accel_group (menu));
919 case PROP_ACCEL_PATH:
920 g_value_set_string (value, gtk_menu_get_accel_path (menu));
922 case PROP_ATTACH_WIDGET:
923 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
925 case PROP_TEAROFF_STATE:
926 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
928 case PROP_TEAROFF_TITLE:
929 g_value_set_string (value, gtk_menu_get_title (menu));
932 g_value_set_int (value, gtk_menu_get_monitor (menu));
934 case PROP_RESERVE_TOGGLE_SIZE:
935 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
938 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
944 gtk_menu_set_child_property (GtkContainer *container,
950 GtkMenu *menu = GTK_MENU (container);
951 AttachInfo *ai = get_attach_info (child);
955 case CHILD_PROP_LEFT_ATTACH:
956 ai->left_attach = g_value_get_int (value);
958 case CHILD_PROP_RIGHT_ATTACH:
959 ai->right_attach = g_value_get_int (value);
961 case CHILD_PROP_TOP_ATTACH:
962 ai->top_attach = g_value_get_int (value);
964 case CHILD_PROP_BOTTOM_ATTACH:
965 ai->bottom_attach = g_value_get_int (value);
969 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
973 menu_queue_resize (menu);
977 gtk_menu_get_child_property (GtkContainer *container,
983 AttachInfo *ai = get_attach_info (child);
987 case CHILD_PROP_LEFT_ATTACH:
988 g_value_set_int (value, ai->left_attach);
990 case CHILD_PROP_RIGHT_ATTACH:
991 g_value_set_int (value, ai->right_attach);
993 case CHILD_PROP_TOP_ATTACH:
994 g_value_set_int (value, ai->top_attach);
996 case CHILD_PROP_BOTTOM_ATTACH:
997 g_value_set_int (value, ai->bottom_attach);
1001 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1007 gtk_menu_window_event (GtkWidget *window,
1011 gboolean handled = FALSE;
1013 g_object_ref (window);
1014 g_object_ref (menu);
1016 switch (event->type)
1019 case GDK_KEY_RELEASE:
1020 handled = gtk_widget_event (menu, event);
1026 g_object_unref (window);
1027 g_object_unref (menu);
1033 gtk_menu_init (GtkMenu *menu)
1035 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
1037 menu->parent_menu_item = NULL;
1038 menu->old_active_menu_item = NULL;
1039 menu->accel_group = NULL;
1040 menu->position_func = NULL;
1041 menu->position_func_data = NULL;
1042 menu->toggle_size = 0;
1044 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1045 "type", GTK_WINDOW_POPUP,
1048 "signal::event", gtk_menu_window_event, menu,
1049 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1051 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
1052 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
1054 /* Refloat the menu, so that reference counting for the menu isn't
1055 * affected by it being a child of the toplevel
1057 g_object_force_floating (G_OBJECT (menu));
1058 menu->needs_destruction_ref_count = TRUE;
1060 menu->view_window = NULL;
1061 menu->bin_window = NULL;
1063 menu->scroll_offset = 0;
1064 menu->scroll_step = 0;
1065 menu->timeout_id = 0;
1066 menu->scroll_fast = FALSE;
1068 menu->tearoff_window = NULL;
1069 menu->tearoff_hbox = NULL;
1070 menu->torn_off = FALSE;
1071 menu->tearoff_active = FALSE;
1072 menu->tearoff_adjustment = NULL;
1073 menu->tearoff_scrollbar = NULL;
1075 menu->upper_arrow_visible = FALSE;
1076 menu->lower_arrow_visible = FALSE;
1077 menu->upper_arrow_prelight = FALSE;
1078 menu->lower_arrow_prelight = FALSE;
1080 priv->upper_arrow_state = GTK_STATE_NORMAL;
1081 priv->lower_arrow_state = GTK_STATE_NORMAL;
1083 priv->have_layout = FALSE;
1084 priv->monitor_num = -1;
1088 gtk_menu_destroy (GtkObject *object)
1090 GtkMenu *menu = GTK_MENU (object);
1091 GtkMenuAttachData *data;
1092 GtkMenuPrivate *priv;
1094 gtk_menu_remove_scroll_timeout (menu);
1096 data = g_object_get_data (G_OBJECT (object), attach_data_key);
1098 gtk_menu_detach (menu);
1100 gtk_menu_stop_navigating_submenu (menu);
1102 if (menu->old_active_menu_item)
1104 g_object_unref (menu->old_active_menu_item);
1105 menu->old_active_menu_item = NULL;
1108 /* Add back the reference count for being a child */
1109 if (menu->needs_destruction_ref_count)
1111 menu->needs_destruction_ref_count = FALSE;
1112 g_object_ref (object);
1115 if (menu->accel_group)
1117 g_object_unref (menu->accel_group);
1118 menu->accel_group = NULL;
1122 gtk_widget_destroy (menu->toplevel);
1124 if (menu->tearoff_window)
1125 gtk_widget_destroy (menu->tearoff_window);
1127 priv = gtk_menu_get_private (menu);
1131 g_free (priv->heights);
1132 priv->heights = NULL;
1137 g_free (priv->title);
1141 if (priv->position_func_data_destroy)
1143 priv->position_func_data_destroy (menu->position_func_data);
1144 menu->position_func_data = NULL;
1145 priv->position_func_data_destroy = NULL;
1148 GTK_OBJECT_CLASS (gtk_menu_parent_class)->destroy (object);
1152 menu_change_screen (GtkMenu *menu,
1153 GdkScreen *new_screen)
1155 GtkMenuPrivate *private = gtk_menu_get_private (menu);
1157 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1159 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1165 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1166 gtk_menu_position (menu);
1169 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1170 private->monitor_num = -1;
1174 attach_widget_screen_changed (GtkWidget *attach_widget,
1175 GdkScreen *previous_screen,
1178 if (gtk_widget_has_screen (attach_widget) &&
1179 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1181 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1186 gtk_menu_attach_to_widget (GtkMenu *menu,
1187 GtkWidget *attach_widget,
1188 GtkMenuDetachFunc detacher)
1190 GtkMenuAttachData *data;
1193 g_return_if_fail (GTK_IS_MENU (menu));
1194 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1196 /* keep this function in sync with gtk_widget_set_parent()
1199 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1202 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1203 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1207 g_object_ref_sink (menu);
1209 data = g_slice_new (GtkMenuAttachData);
1210 data->attach_widget = attach_widget;
1212 g_signal_connect (attach_widget, "screen-changed",
1213 G_CALLBACK (attach_widget_screen_changed), menu);
1214 attach_widget_screen_changed (attach_widget, NULL, menu);
1216 data->detacher = detacher;
1217 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1218 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1219 if (!g_list_find (list, menu))
1221 list = g_list_prepend (list, menu);
1223 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1224 (GDestroyNotify) g_list_free);
1226 if (gtk_widget_get_state (GTK_WIDGET (menu)) != GTK_STATE_NORMAL)
1227 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1229 /* we don't need to set the style here, since
1230 * we are a toplevel widget.
1233 /* Fallback title for menu comes from attach widget */
1234 gtk_menu_update_title (menu);
1236 g_object_notify (G_OBJECT (menu), "attach-widget");
1240 gtk_menu_get_attach_widget (GtkMenu *menu)
1242 GtkMenuAttachData *data;
1244 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1246 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1248 return data->attach_widget;
1253 gtk_menu_detach (GtkMenu *menu)
1255 GtkMenuAttachData *data;
1258 g_return_if_fail (GTK_IS_MENU (menu));
1260 /* keep this function in sync with gtk_widget_unparent()
1262 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1265 g_warning ("gtk_menu_detach(): menu is not attached");
1268 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1270 g_signal_handlers_disconnect_by_func (data->attach_widget,
1271 (gpointer) attach_widget_screen_changed,
1275 data->detacher (data->attach_widget, menu);
1276 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1277 list = g_list_remove (list, menu);
1279 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1280 (GDestroyNotify) g_list_free);
1282 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1284 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1285 gtk_widget_unrealize (GTK_WIDGET (menu));
1287 g_slice_free (GtkMenuAttachData, data);
1289 /* Fallback title for menu comes from attach widget */
1290 gtk_menu_update_title (menu);
1292 g_object_unref (menu);
1296 gtk_menu_remove (GtkContainer *container,
1299 GtkMenu *menu = GTK_MENU (container);
1301 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1303 /* Clear out old_active_menu_item if it matches the item we are removing
1305 if (menu->old_active_menu_item == widget)
1307 g_object_unref (menu->old_active_menu_item);
1308 menu->old_active_menu_item = NULL;
1311 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1312 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1314 menu_queue_resize (menu);
1320 return g_object_new (GTK_TYPE_MENU, NULL);
1324 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1328 GtkMenu *menu = GTK_MENU (menu_shell);
1329 AttachInfo *ai = get_attach_info (child);
1331 ai->left_attach = -1;
1332 ai->right_attach = -1;
1333 ai->top_attach = -1;
1334 ai->bottom_attach = -1;
1336 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1337 gtk_widget_set_parent_window (child, menu->bin_window);
1339 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1341 menu_queue_resize (menu);
1345 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1350 widget = GTK_WIDGET (menu);
1358 menu->tearoff_active = FALSE;
1359 menu->saved_scroll_offset = menu->scroll_offset;
1361 window = gtk_widget_get_window (menu->tearoff_window);
1363 gdk_drawable_get_size (window, &width, &height);
1365 pixmap = gdk_pixmap_new (window,
1370 cr = gdk_cairo_create (pixmap);
1371 /* Let's hope that function never notices we're not passing it a pixmap */
1372 gdk_cairo_set_source_pixmap (cr,
1378 gtk_widget_set_size_request (menu->tearoff_window,
1382 gdk_window_set_back_pixmap (window, pixmap, FALSE);
1383 g_object_unref (pixmap);
1388 popup_grab_on_window (GdkWindow *window,
1389 GdkDevice *keyboard,
1391 guint32 activate_time)
1394 gdk_device_grab (keyboard, window,
1395 GDK_OWNERSHIP_WINDOW, TRUE,
1396 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1397 NULL, activate_time) != GDK_GRAB_SUCCESS)
1401 gdk_device_grab (pointer, window,
1402 GDK_OWNERSHIP_WINDOW, TRUE,
1403 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1404 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1405 GDK_POINTER_MOTION_MASK,
1406 NULL, activate_time) != GDK_GRAB_SUCCESS)
1409 gdk_device_ungrab (keyboard, activate_time);
1418 * gtk_menu_popup_for_device:
1419 * @menu: a #GtkMenu.
1420 * @device: (allow-none): a #GdkDevice
1421 * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1422 * menu item, or %NULL
1423 * @parent_menu_item: (allow-none): the menu item whose activation triggered
1424 * the popup, or %NULL
1425 * @func: (allow-none): a user supplied function used to position the menu,
1427 * @data: (allow-none): user supplied data to be passed to @func
1428 * @destroy: (allow-none): destroy notify for @data
1429 * @button: the mouse button which was pressed to initiate the event
1430 * @activate_time: the time at which the activation event occurred
1432 * Displays a menu and makes it available for selection.
1434 * Applications can use this function to display context-sensitive menus,
1435 * and will typically supply %NULL for the @parent_menu_shell,
1436 * @parent_menu_item, @func, @data and @destroy parameters. The default
1437 * menu positioning function will position the menu at the current position
1438 * of @device (or its corresponding pointer).
1440 * The @button parameter should be the mouse button pressed to initiate
1441 * the menu popup. If the menu popup was initiated by something other than
1442 * a mouse button press, such as a mouse button release or a keypress,
1443 * @button should be 0.
1445 * The @activate_time parameter is used to conflict-resolve initiation of
1446 * concurrent requests for mouse/keyboard grab requests. To function
1447 * properly, this needs to be the time stamp of the user event (such as
1448 * a mouse click or key press) that caused the initiation of the popup.
1449 * Only if no such event is available, gtk_get_current_event_time() can
1455 gtk_menu_popup_for_device (GtkMenu *menu,
1457 GtkWidget *parent_menu_shell,
1458 GtkWidget *parent_menu_item,
1459 GtkMenuPositionFunc func,
1461 GDestroyNotify destroy,
1463 guint32 activate_time)
1466 GtkWidget *xgrab_shell;
1468 GdkEvent *current_event;
1469 GtkMenuShell *menu_shell;
1470 gboolean grab_keyboard;
1471 GtkMenuPrivate *priv;
1472 GtkWidget *parent_toplevel;
1473 GdkDevice *keyboard, *pointer;
1475 g_return_if_fail (GTK_IS_MENU (menu));
1476 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1479 device = gtk_get_current_event_device ();
1483 GdkDisplay *display;
1484 GdkDeviceManager *device_manager;
1487 display = gtk_widget_get_display (GTK_WIDGET (menu));
1488 device_manager = gdk_display_get_device_manager (display);
1489 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1491 device = devices->data;
1493 g_list_free (devices);
1496 widget = GTK_WIDGET (menu);
1497 menu_shell = GTK_MENU_SHELL (menu);
1498 priv = gtk_menu_get_private (menu);
1500 if (device->source == GDK_SOURCE_KEYBOARD)
1503 pointer = gdk_device_get_associated_device (device);
1508 keyboard = gdk_device_get_associated_device (device);
1511 menu_shell->parent_menu_shell = parent_menu_shell;
1513 priv->seen_item_enter = FALSE;
1515 /* Find the last viewable ancestor, and make an X grab on it
1517 parent = GTK_WIDGET (menu);
1521 gboolean viewable = TRUE;
1522 GtkWidget *tmp = parent;
1526 if (!gtk_widget_get_mapped (tmp))
1531 tmp = gtk_widget_get_parent (tmp);
1535 xgrab_shell = parent;
1537 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1540 /* We want to receive events generated when we map the menu; unfortunately,
1541 * since there is probably already an implicit grab in place from the
1542 * button that the user used to pop up the menu, we won't receive then --
1543 * in particular, the EnterNotify when the menu pops up under the pointer.
1545 * If we are grabbing on a parent menu shell, no problem; just grab on
1546 * that menu shell first before popping up the window with owner_events = TRUE.
1548 * When grabbing on the menu itself, things get more convuluted - we
1549 * we do an explicit grab on a specially created window with
1550 * owner_events = TRUE, which we override further down with a grab
1551 * on the menu. (We can't grab on the menu until it is mapped; we
1552 * probably could just leave the grab on the other window, with a
1553 * little reorganization of the code in gtkmenu*).
1555 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1556 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1561 if (xgrab_shell && xgrab_shell != widget)
1563 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1565 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1566 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1571 GdkWindow *transfer_window;
1573 xgrab_shell = widget;
1574 transfer_window = menu_grab_transfer_window_get (menu);
1575 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1577 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1578 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1582 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1584 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1585 * with a stuck up window, we just abort here. Presumably the user will
1588 menu_shell->parent_menu_shell = NULL;
1589 menu_grab_transfer_window_destroy (menu);
1593 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1594 menu_shell->active = TRUE;
1595 menu_shell->button = button;
1597 /* If we are popping up the menu from something other than, a button
1598 * press then, as a heuristic, we ignore enter events for the menu
1599 * until we get a MOTION_NOTIFY.
1602 current_event = gtk_get_current_event ();
1605 if ((current_event->type != GDK_BUTTON_PRESS) &&
1606 (current_event->type != GDK_ENTER_NOTIFY))
1607 menu_shell->ignore_enter = TRUE;
1609 gdk_event_free (current_event);
1612 menu_shell->ignore_enter = TRUE;
1616 gtk_menu_tearoff_bg_copy (menu);
1618 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1621 parent_toplevel = NULL;
1622 if (parent_menu_shell)
1623 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1624 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1626 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1628 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1631 /* Set transient for to get the right window group and parent relationship */
1632 if (GTK_IS_WINDOW (parent_toplevel))
1633 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1634 GTK_WINDOW (parent_toplevel));
1636 menu->parent_menu_item = parent_menu_item;
1637 menu->position_func = func;
1638 menu->position_func_data = data;
1639 priv->position_func_data_destroy = destroy;
1640 menu_shell->activate_time = activate_time;
1642 /* We need to show the menu here rather in the init function because
1643 * code expects to be able to tell if the menu is onscreen by
1644 * looking at the gtk_widget_get_visible (menu)
1646 gtk_widget_show (GTK_WIDGET (menu));
1648 /* Position the menu, possibly changing the size request
1650 gtk_menu_position (menu);
1652 /* Compute the size of the toplevel and realize it so we
1653 * can scroll correctly.
1656 GtkRequisition tmp_request;
1657 GtkAllocation tmp_allocation = { 0, };
1659 /* Instead of trusting the menu position function to queue a resize when the
1660 * menu goes out of bounds, invalidate the cached size here. */
1661 gtk_widget_queue_resize (GTK_WIDGET (menu));
1662 gtk_size_request_get_size (GTK_SIZE_REQUEST (menu->toplevel), &tmp_request, NULL);
1664 tmp_allocation.width = tmp_request.width;
1665 tmp_allocation.height = tmp_request.height;
1667 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1669 gtk_widget_realize (GTK_WIDGET (menu));
1672 gtk_menu_scroll_to (menu, menu->scroll_offset);
1674 /* if no item is selected, select the first one */
1675 if (!menu_shell->active_menu_item)
1677 gboolean touchscreen_mode;
1679 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1680 "gtk-touchscreen-mode", &touchscreen_mode,
1683 if (touchscreen_mode)
1684 gtk_menu_shell_select_first (menu_shell, TRUE);
1687 /* Once everything is set up correctly, map the toplevel window on
1690 gtk_widget_show (menu->toplevel);
1692 if (xgrab_shell == widget)
1693 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1695 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1697 if (parent_menu_shell)
1699 gboolean keyboard_mode;
1701 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1702 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1704 else if (menu_shell->button == 0) /* a keynav-activated context menu */
1705 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1707 _gtk_menu_shell_update_mnemonics (menu_shell);
1712 * @menu: a #GtkMenu.
1713 * @parent_menu_shell: (allow-none): the menu shell containing the triggering menu item, or %NULL
1714 * @parent_menu_item: (allow-none): the menu item whose activation triggered the popup, or %NULL
1715 * @func: (allow-none): a user supplied function used to position the menu, or %NULL
1716 * @data: (allow-none): user supplied data to be passed to @func.
1717 * @button: the mouse button which was pressed to initiate the event.
1718 * @activate_time: the time at which the activation event occurred.
1720 * Displays a menu and makes it available for selection. Applications can use
1721 * this function to display context-sensitive menus, and will typically supply
1722 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1723 * parameters. The default menu positioning function will position the menu
1724 * at the current mouse cursor position.
1726 * The @button parameter should be the mouse button pressed to initiate
1727 * the menu popup. If the menu popup was initiated by something other than
1728 * a mouse button press, such as a mouse button release or a keypress,
1729 * @button should be 0.
1731 * The @activate_time parameter is used to conflict-resolve initiation of
1732 * concurrent requests for mouse/keyboard grab requests. To function
1733 * properly, this needs to be the time stamp of the user event (such as
1734 * a mouse click or key press) that caused the initiation of the popup.
1735 * Only if no such event is available, gtk_get_current_event_time() can
1739 gtk_menu_popup (GtkMenu *menu,
1740 GtkWidget *parent_menu_shell,
1741 GtkWidget *parent_menu_item,
1742 GtkMenuPositionFunc func,
1745 guint32 activate_time)
1747 g_return_if_fail (GTK_IS_MENU (menu));
1749 gtk_menu_popup_for_device (menu,
1754 button, activate_time);
1758 gtk_menu_popdown (GtkMenu *menu)
1760 GtkMenuPrivate *private;
1761 GtkMenuShell *menu_shell;
1764 g_return_if_fail (GTK_IS_MENU (menu));
1766 menu_shell = GTK_MENU_SHELL (menu);
1767 private = gtk_menu_get_private (menu);
1769 menu_shell->parent_menu_shell = NULL;
1770 menu_shell->active = FALSE;
1771 menu_shell->ignore_enter = FALSE;
1773 private->have_position = FALSE;
1775 gtk_menu_stop_scrolling (menu);
1777 gtk_menu_stop_navigating_submenu (menu);
1779 if (menu_shell->active_menu_item)
1781 if (menu->old_active_menu_item)
1782 g_object_unref (menu->old_active_menu_item);
1783 menu->old_active_menu_item = menu_shell->active_menu_item;
1784 g_object_ref (menu->old_active_menu_item);
1787 gtk_menu_shell_deselect (menu_shell);
1789 /* The X Grab, if present, will automatically be removed when we hide
1791 gtk_widget_hide (menu->toplevel);
1792 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1794 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1798 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1800 if (gtk_bin_get_child (GTK_BIN (menu->toplevel)))
1802 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1806 /* We popped up the menu from the tearoff, so we need to
1807 * release the grab - we aren't actually hiding the menu.
1809 if (menu_shell->have_xgrab && pointer)
1811 GdkDevice *keyboard;
1813 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1814 keyboard = gdk_device_get_associated_device (pointer);
1817 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1821 /* gtk_menu_popdown is called each time a menu item is selected from
1822 * a torn off menu. Only scroll back to the saved position if the
1823 * non-tearoff menu was popped down.
1825 if (!menu->tearoff_active)
1826 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1827 menu->tearoff_active = TRUE;
1830 gtk_widget_hide (GTK_WIDGET (menu));
1832 menu_shell->have_xgrab = FALSE;
1835 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1837 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1839 menu_grab_transfer_window_destroy (menu);
1843 gtk_menu_get_active (GtkMenu *menu)
1848 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1850 if (!menu->old_active_menu_item)
1853 children = GTK_MENU_SHELL (menu)->children;
1857 child = children->data;
1858 children = children->next;
1860 if (gtk_bin_get_child (GTK_BIN (child)))
1865 menu->old_active_menu_item = child;
1866 if (menu->old_active_menu_item)
1867 g_object_ref (menu->old_active_menu_item);
1870 return menu->old_active_menu_item;
1874 gtk_menu_set_active (GtkMenu *menu,
1880 g_return_if_fail (GTK_IS_MENU (menu));
1882 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1885 child = tmp_list->data;
1886 if (gtk_bin_get_child (GTK_BIN (child)))
1888 if (menu->old_active_menu_item)
1889 g_object_unref (menu->old_active_menu_item);
1890 menu->old_active_menu_item = child;
1891 g_object_ref (menu->old_active_menu_item);
1898 * gtk_menu_set_accel_group:
1899 * @accel_group: (allow-none):
1902 gtk_menu_set_accel_group (GtkMenu *menu,
1903 GtkAccelGroup *accel_group)
1905 g_return_if_fail (GTK_IS_MENU (menu));
1907 if (menu->accel_group != accel_group)
1909 if (menu->accel_group)
1910 g_object_unref (menu->accel_group);
1911 menu->accel_group = accel_group;
1912 if (menu->accel_group)
1913 g_object_ref (menu->accel_group);
1914 _gtk_menu_refresh_accel_paths (menu, TRUE);
1919 gtk_menu_get_accel_group (GtkMenu *menu)
1921 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1923 return menu->accel_group;
1927 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1930 /* Menu items chain here to figure whether they can activate their
1931 * accelerators. Unlike ordinary widgets, menus allow accel
1932 * activation even if invisible since that's the usual case for
1933 * submenus/popup-menus. however, the state of the attach widget
1934 * affects the "activeness" of the menu.
1936 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1939 return gtk_widget_can_activate_accel (awidget, signal_id);
1941 return gtk_widget_is_sensitive (widget);
1945 * gtk_menu_set_accel_path
1946 * @menu: a valid #GtkMenu
1947 * @accel_path: (allow-none): a valid accelerator path
1949 * Sets an accelerator path for this menu from which accelerator paths
1950 * for its immediate children, its menu items, can be constructed.
1951 * The main purpose of this function is to spare the programmer the
1952 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1953 * each menu item that should support runtime user changable accelerators.
1954 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1955 * each menu item of this menu, that contains a label describing its purpose,
1956 * automatically gets an accel path assigned. For example, a menu containing
1957 * menu items "New" and "Exit", will, after
1958 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1959 * has been called, assign its items the accel paths:
1960 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1961 * Assigning accel paths to menu items then enables the user to change
1962 * their accelerators at runtime. More details about accelerator paths
1963 * and their default setups can be found at gtk_accel_map_add_entry().
1965 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1966 * pass a static string, you can save some memory by interning it first with
1967 * g_intern_static_string().
1970 gtk_menu_set_accel_path (GtkMenu *menu,
1971 const gchar *accel_path)
1973 g_return_if_fail (GTK_IS_MENU (menu));
1975 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1977 /* FIXME: accel_path should be defined as const gchar* */
1978 menu->accel_path = (gchar*)g_intern_string (accel_path);
1979 if (menu->accel_path)
1980 _gtk_menu_refresh_accel_paths (menu, FALSE);
1984 * gtk_menu_get_accel_path
1985 * @menu: a valid #GtkMenu
1987 * Retrieves the accelerator path set on the menu.
1989 * Returns: the accelerator path set on the menu.
1994 gtk_menu_get_accel_path (GtkMenu *menu)
1996 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1998 return menu->accel_path;
2003 gboolean group_changed;
2007 refresh_accel_paths_foreach (GtkWidget *widget,
2010 AccelPropagation *prop = data;
2012 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
2013 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2014 prop->menu->accel_path,
2015 prop->menu->accel_group,
2016 prop->group_changed);
2020 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
2021 gboolean group_changed)
2023 g_return_if_fail (GTK_IS_MENU (menu));
2025 if (menu->accel_path && menu->accel_group)
2027 AccelPropagation prop;
2030 prop.group_changed = group_changed;
2031 gtk_container_foreach (GTK_CONTAINER (menu),
2032 refresh_accel_paths_foreach,
2038 gtk_menu_reposition (GtkMenu *menu)
2040 g_return_if_fail (GTK_IS_MENU (menu));
2042 if (!menu->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2043 gtk_menu_position (menu);
2047 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2050 g_return_if_fail (GTK_IS_MENU (menu));
2052 if (adjustment->value != menu->scroll_offset)
2053 gtk_menu_scroll_to (menu, adjustment->value);
2057 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2060 GdkGeometry geometry_hints;
2061 GtkMenuPrivate *priv;
2063 if (!menu->tearoff_window)
2066 priv = gtk_menu_get_private (menu);
2068 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2070 GtkRequisition requisition;
2072 gtk_size_request_get_size (GTK_SIZE_REQUEST (menu->tearoff_scrollbar),
2073 &requisition, NULL);
2074 width += requisition.width;
2077 geometry_hints.min_width = width;
2078 geometry_hints.max_width = width;
2080 geometry_hints.min_height = 0;
2081 geometry_hints.max_height = priv->requested_height;
2083 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
2086 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2090 gtk_menu_update_title (GtkMenu *menu)
2092 if (menu->tearoff_window)
2095 GtkWidget *attach_widget;
2097 title = gtk_menu_get_title (menu);
2100 attach_widget = gtk_menu_get_attach_widget (menu);
2101 if (GTK_IS_MENU_ITEM (attach_widget))
2103 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2104 if (GTK_IS_LABEL (child))
2105 title = gtk_label_get_text (GTK_LABEL (child));
2110 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
2115 gtk_menu_get_toplevel (GtkWidget *menu)
2117 GtkWidget *attach, *toplevel;
2119 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2121 if (GTK_IS_MENU_ITEM (attach))
2122 attach = gtk_widget_get_parent (attach);
2124 if (GTK_IS_MENU (attach))
2125 return gtk_menu_get_toplevel (attach);
2126 else if (GTK_IS_WIDGET (attach))
2128 toplevel = gtk_widget_get_toplevel (attach);
2129 if (gtk_widget_is_toplevel (toplevel))
2137 tearoff_window_destroyed (GtkWidget *widget,
2140 gtk_menu_set_tearoff_state (menu, FALSE);
2144 gtk_menu_set_tearoff_state (GtkMenu *menu,
2148 GtkMenuPrivate *priv;
2150 g_return_if_fail (GTK_IS_MENU (menu));
2152 priv = gtk_menu_get_private (menu);
2154 if (menu->torn_off != torn_off)
2156 menu->torn_off = torn_off;
2157 menu->tearoff_active = torn_off;
2161 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2162 gtk_menu_popdown (menu);
2164 if (!menu->tearoff_window)
2166 GtkWidget *toplevel;
2168 menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2169 "type", GTK_WINDOW_TOPLEVEL,
2170 "screen", gtk_widget_get_screen (menu->toplevel),
2171 "app-paintable", TRUE,
2174 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
2175 GDK_WINDOW_TYPE_HINT_MENU);
2176 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
2177 g_signal_connect (menu->tearoff_window, "destroy",
2178 G_CALLBACK (tearoff_window_destroyed), menu);
2179 g_signal_connect (menu->tearoff_window, "event",
2180 G_CALLBACK (gtk_menu_window_event), menu);
2182 gtk_menu_update_title (menu);
2184 gtk_widget_realize (menu->tearoff_window);
2186 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2187 if (toplevel != NULL)
2188 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
2189 GTK_WINDOW (toplevel));
2191 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
2192 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
2194 gdk_drawable_get_size (gtk_widget_get_window (GTK_WIDGET (menu)),
2196 menu->tearoff_adjustment =
2197 GTK_ADJUSTMENT (gtk_adjustment_new (0,
2199 priv->requested_height,
2203 g_object_connect (menu->tearoff_adjustment,
2204 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2206 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
2208 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
2209 menu->tearoff_scrollbar,
2212 if (menu->tearoff_adjustment->upper > height)
2213 gtk_widget_show (menu->tearoff_scrollbar);
2215 gtk_widget_show (menu->tearoff_hbox);
2218 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
2220 gdk_drawable_get_size (gtk_widget_get_window (GTK_WIDGET (menu)),
2223 /* Update menu->requisition
2225 gtk_size_request_get_size (GTK_SIZE_REQUEST (menu),
2228 gtk_menu_set_tearoff_hints (menu, width);
2230 gtk_widget_realize (menu->tearoff_window);
2231 gtk_menu_position (menu);
2233 gtk_widget_show (GTK_WIDGET (menu));
2234 gtk_widget_show (menu->tearoff_window);
2236 gtk_menu_scroll_to (menu, 0);
2241 gtk_widget_hide (GTK_WIDGET (menu));
2242 gtk_widget_hide (menu->tearoff_window);
2243 if (GTK_IS_CONTAINER (menu->toplevel))
2244 gtk_menu_reparent (menu, menu->toplevel, FALSE);
2245 gtk_widget_destroy (menu->tearoff_window);
2247 menu->tearoff_window = NULL;
2248 menu->tearoff_hbox = NULL;
2249 menu->tearoff_scrollbar = NULL;
2250 menu->tearoff_adjustment = NULL;
2253 g_object_notify (G_OBJECT (menu), "tearoff-state");
2258 * gtk_menu_get_tearoff_state:
2261 * Returns whether the menu is torn off. See
2262 * gtk_menu_set_tearoff_state ().
2264 * Return value: %TRUE if the menu is currently torn off.
2267 gtk_menu_get_tearoff_state (GtkMenu *menu)
2269 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2271 return menu->torn_off;
2275 * gtk_menu_set_title:
2277 * @title: a string containing the title for the menu.
2279 * Sets the title string for the menu. The title is displayed when the menu
2280 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
2281 * attached to a parent menu item, and if so it will try to use the same text as
2282 * that menu item's label.
2285 gtk_menu_set_title (GtkMenu *menu,
2288 GtkMenuPrivate *priv;
2291 g_return_if_fail (GTK_IS_MENU (menu));
2293 priv = gtk_menu_get_private (menu);
2295 old_title = priv->title;
2296 priv->title = g_strdup (title);
2299 gtk_menu_update_title (menu);
2300 g_object_notify (G_OBJECT (menu), "tearoff-title");
2304 * gtk_menu_get_title:
2307 * Returns the title of the menu. See gtk_menu_set_title().
2309 * Return value: the title of the menu, or %NULL if the menu has no
2310 * title set on it. This string is owned by the widget and should
2311 * not be modified or freed.
2313 G_CONST_RETURN gchar *
2314 gtk_menu_get_title (GtkMenu *menu)
2316 GtkMenuPrivate *priv;
2318 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2320 priv = gtk_menu_get_private (menu);
2326 gtk_menu_reorder_child (GtkMenu *menu,
2330 GtkMenuShell *menu_shell;
2332 g_return_if_fail (GTK_IS_MENU (menu));
2333 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2335 menu_shell = GTK_MENU_SHELL (menu);
2337 if (g_list_find (menu_shell->children, child))
2339 menu_shell->children = g_list_remove (menu_shell->children, child);
2340 menu_shell->children = g_list_insert (menu_shell->children, child, position);
2342 menu_queue_resize (menu);
2347 gtk_menu_style_set (GtkWidget *widget,
2348 GtkStyle *previous_style)
2350 if (gtk_widget_get_realized (widget))
2352 GtkMenu *menu = GTK_MENU (widget);
2355 style = gtk_widget_get_style (widget);
2357 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2358 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2359 gtk_style_set_background (style, gtk_widget_get_window (widget), GTK_STATE_NORMAL);
2364 get_arrows_border (GtkMenu *menu,
2367 guint scroll_arrow_height;
2368 GtkArrowPlacement arrow_placement;
2370 gtk_widget_style_get (GTK_WIDGET (menu),
2371 "scroll-arrow-vlength", &scroll_arrow_height,
2372 "arrow_placement", &arrow_placement,
2375 switch (arrow_placement)
2377 case GTK_ARROWS_BOTH:
2378 border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2379 border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2382 case GTK_ARROWS_START:
2383 border->top = (menu->upper_arrow_visible ||
2384 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2388 case GTK_ARROWS_END:
2390 border->bottom = (menu->upper_arrow_visible ||
2391 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2395 border->left = border->right = 0;
2399 gtk_menu_realize (GtkWidget *widget)
2401 GtkAllocation allocation;
2404 GdkWindowAttr attributes;
2405 gint attributes_mask;
2408 GtkMenuPrivate *priv;
2411 guint vertical_padding;
2412 guint horizontal_padding;
2413 GtkBorder arrow_border;
2415 g_return_if_fail (GTK_IS_MENU (widget));
2417 menu = GTK_MENU (widget);
2418 priv = gtk_menu_get_private (menu);
2420 gtk_widget_set_realized (widget, TRUE);
2422 gtk_widget_get_allocation (widget, &allocation);
2424 attributes.window_type = GDK_WINDOW_CHILD;
2425 attributes.x = allocation.x;
2426 attributes.y = allocation.y;
2427 attributes.width = allocation.width;
2428 attributes.height = allocation.height;
2429 attributes.wclass = GDK_INPUT_OUTPUT;
2430 attributes.visual = gtk_widget_get_visual (widget);
2431 attributes.colormap = gtk_widget_get_colormap (widget);
2432 attributes.event_mask = gtk_widget_get_events (widget);
2433 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2434 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2436 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2438 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2439 &attributes, attributes_mask);
2440 gtk_widget_set_window (widget, window);
2441 gdk_window_set_user_data (window, widget);
2443 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2445 style = gtk_widget_get_style (widget);
2446 gtk_widget_style_get (GTK_WIDGET (menu),
2447 "vertical-padding", &vertical_padding,
2448 "horizontal-padding", &horizontal_padding,
2451 gtk_widget_get_allocation (widget, &allocation);
2453 attributes.x = border_width + style->xthickness + horizontal_padding;
2454 attributes.y = border_width + style->ythickness + vertical_padding;
2455 attributes.width = MAX (1, allocation.width - attributes.x * 2);
2456 attributes.height = MAX (1, allocation.height - attributes.y * 2);
2458 get_arrows_border (menu, &arrow_border);
2459 attributes.y += arrow_border.top;
2460 attributes.height -= arrow_border.top;
2461 attributes.height -= arrow_border.bottom;
2463 menu->view_window = gdk_window_new (window,
2464 &attributes, attributes_mask);
2465 gdk_window_set_user_data (menu->view_window, menu);
2467 gtk_widget_get_allocation (widget, &allocation);
2471 attributes.width = MAX (1, allocation.width - (border_width + style->xthickness + horizontal_padding) * 2);
2472 attributes.height = MAX (1, priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2);
2474 menu->bin_window = gdk_window_new (menu->view_window,
2475 &attributes, attributes_mask);
2476 gdk_window_set_user_data (menu->bin_window, menu);
2478 children = GTK_MENU_SHELL (menu)->children;
2481 child = children->data;
2482 children = children->next;
2484 gtk_widget_set_parent_window (child, menu->bin_window);
2487 gtk_widget_style_attach (widget);
2488 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2489 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2490 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
2492 if (GTK_MENU_SHELL (widget)->active_menu_item)
2493 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2494 GTK_MENU_SHELL (widget)->active_menu_item);
2496 gdk_window_show (menu->bin_window);
2497 gdk_window_show (menu->view_window);
2501 gtk_menu_focus (GtkWidget *widget,
2502 GtkDirectionType direction)
2505 * A menu or its menu items cannot have focus
2510 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2513 menu_grab_transfer_window_get (GtkMenu *menu)
2515 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2518 GdkWindowAttr attributes;
2519 gint attributes_mask;
2521 attributes.x = -100;
2522 attributes.y = -100;
2523 attributes.width = 10;
2524 attributes.height = 10;
2525 attributes.window_type = GDK_WINDOW_TEMP;
2526 attributes.wclass = GDK_INPUT_ONLY;
2527 attributes.override_redirect = TRUE;
2528 attributes.event_mask = 0;
2530 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2532 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2533 &attributes, attributes_mask);
2534 gdk_window_set_user_data (window, menu);
2536 gdk_window_show (window);
2538 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2545 menu_grab_transfer_window_destroy (GtkMenu *menu)
2547 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2550 gdk_window_set_user_data (window, NULL);
2551 gdk_window_destroy (window);
2552 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2557 gtk_menu_unrealize (GtkWidget *widget)
2559 GtkMenu *menu = GTK_MENU (widget);
2561 menu_grab_transfer_window_destroy (menu);
2563 gdk_window_set_user_data (menu->view_window, NULL);
2564 gdk_window_destroy (menu->view_window);
2565 menu->view_window = NULL;
2567 gdk_window_set_user_data (menu->bin_window, NULL);
2568 gdk_window_destroy (menu->bin_window);
2569 menu->bin_window = NULL;
2571 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2576 calculate_line_heights (GtkMenu *menu,
2578 guint **ret_min_heights,
2579 guint **ret_nat_heights)
2581 GtkMenuShell *menu_shell;
2582 GtkMenuPrivate *priv;
2583 GtkWidget *child, *widget;
2585 guint horizontal_padding;
2593 widget = GTK_WIDGET (menu);
2594 menu_shell = GTK_MENU_SHELL (widget);
2595 priv = gtk_menu_get_private (menu);
2597 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2598 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2599 n_heights = gtk_menu_get_n_rows (menu);
2600 n_columns = gtk_menu_get_n_columns (menu);
2601 avail_width = for_width - (2 * menu->toggle_size + priv->accel_size) * n_columns;
2603 gtk_widget_style_get (GTK_WIDGET (menu),
2604 "horizontal-padding", &horizontal_padding,
2607 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2608 avail_width -= (border_width + horizontal_padding + gtk_widget_get_style (widget)->xthickness) * 2;
2610 for (children = menu_shell->children; children; children = children->next)
2615 gint child_min, child_nat;
2617 child = children->data;
2619 if (! gtk_widget_get_visible (child))
2622 get_effective_child_attach (child, &l, &r, &t, &b);
2624 part = avail_width / (r - l);
2626 gtk_size_request_get_height_for_width (GTK_SIZE_REQUEST (child), part,
2627 &child_min, &child_nat);
2629 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2631 part = MAX (child_min, toggle_size) / (b - t);
2632 min_heights[t] = MAX (min_heights[t], part);
2634 part = MAX (child_nat, toggle_size) / (b - t);
2635 nat_heights[t] = MAX (nat_heights[t], part);
2638 if (ret_min_heights)
2639 *ret_min_heights = min_heights;
2641 g_free (min_heights);
2643 if (ret_nat_heights)
2644 *ret_nat_heights = nat_heights;
2646 g_free (nat_heights);
2652 gtk_menu_size_allocate (GtkWidget *widget,
2653 GtkAllocation *allocation)
2656 GtkMenuShell *menu_shell;
2658 GtkAllocation child_allocation;
2659 GtkMenuPrivate *priv;
2665 guint vertical_padding;
2666 guint horizontal_padding;
2668 g_return_if_fail (GTK_IS_MENU (widget));
2669 g_return_if_fail (allocation != NULL);
2671 menu = GTK_MENU (widget);
2672 menu_shell = GTK_MENU_SHELL (widget);
2673 priv = gtk_menu_get_private (menu);
2675 gtk_widget_set_allocation (widget, allocation);
2677 style = gtk_widget_get_style (widget);
2679 gtk_widget_style_get (GTK_WIDGET (menu),
2680 "vertical-padding", &vertical_padding,
2681 "horizontal-padding", &horizontal_padding,
2683 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2685 g_free (priv->heights);
2686 priv->heights_length =
2687 calculate_line_heights (menu,
2692 /* refresh our cached height request */
2693 priv->requested_height = (border_width + vertical_padding + style->ythickness) * 2;
2694 for (i = 0; i < priv->heights_length; i++)
2695 priv->requested_height += priv->heights[i];
2697 x = border_width + style->xthickness + horizontal_padding;
2698 y = border_width + style->ythickness + vertical_padding;
2700 width = MAX (1, allocation->width - x * 2);
2701 height = MAX (1, allocation->height - y * 2);
2703 if (menu_shell->active)
2704 gtk_menu_scroll_to (menu, menu->scroll_offset);
2706 if (!menu->tearoff_active)
2708 GtkBorder arrow_border;
2710 get_arrows_border (menu, &arrow_border);
2711 y += arrow_border.top;
2712 height -= arrow_border.top;
2713 height -= arrow_border.bottom;
2716 if (gtk_widget_get_realized (widget))
2718 gdk_window_move_resize (gtk_widget_get_window (widget),
2719 allocation->x, allocation->y,
2720 allocation->width, allocation->height);
2722 gdk_window_move_resize (menu->view_window,
2729 if (menu_shell->children)
2731 gint base_width = width / gtk_menu_get_n_columns (menu);
2733 children = menu_shell->children;
2736 child = children->data;
2737 children = children->next;
2739 if (gtk_widget_get_visible (child))
2744 get_effective_child_attach (child, &l, &r, &t, &b);
2746 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2749 tmp = gtk_menu_get_n_columns (menu) - l;
2750 l = gtk_menu_get_n_columns (menu) - r;
2754 child_allocation.width = (r - l) * base_width;
2755 child_allocation.height = 0;
2756 child_allocation.x = l * base_width;
2757 child_allocation.y = 0;
2759 for (i = 0; i < b; i++)
2762 child_allocation.y += priv->heights[i];
2764 child_allocation.height += priv->heights[i];
2767 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2770 gtk_widget_size_allocate (child, &child_allocation);
2771 gtk_widget_queue_draw (child);
2775 /* Resize the item window */
2776 if (gtk_widget_get_realized (widget))
2782 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2783 height += priv->heights[i];
2785 width = gtk_menu_get_n_columns (menu) * base_width;
2786 gdk_window_resize (menu->bin_window, width, height);
2789 if (menu->tearoff_active)
2791 if (height >= priv->requested_height)
2793 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2795 gtk_widget_hide (menu->tearoff_scrollbar);
2796 gtk_menu_set_tearoff_hints (menu, allocation->width);
2798 gtk_menu_scroll_to (menu, 0);
2803 menu->tearoff_adjustment->upper = priv->requested_height;
2804 menu->tearoff_adjustment->page_size = allocation->height;
2806 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2807 menu->tearoff_adjustment->upper)
2810 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2813 gtk_menu_scroll_to (menu, value);
2816 gtk_adjustment_changed (menu->tearoff_adjustment);
2818 if (!gtk_widget_get_visible (menu->tearoff_scrollbar))
2820 gtk_widget_show (menu->tearoff_scrollbar);
2821 gtk_menu_set_tearoff_hints (menu, allocation->width);
2829 get_arrows_visible_area (GtkMenu *menu,
2830 GdkRectangle *border,
2831 GdkRectangle *upper,
2832 GdkRectangle *lower,
2835 GtkArrowPlacement arrow_placement;
2837 GtkWidget *widget = GTK_WIDGET (menu);
2839 guint vertical_padding;
2840 guint horizontal_padding;
2841 gint scroll_arrow_height;
2843 style = gtk_widget_get_style (widget);
2845 gtk_widget_style_get (widget,
2846 "vertical-padding", &vertical_padding,
2847 "horizontal-padding", &horizontal_padding,
2848 "scroll-arrow-vlength", &scroll_arrow_height,
2849 "arrow-placement", &arrow_placement,
2852 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2853 border->x = border_width + style->xthickness + horizontal_padding;
2854 border->y = border_width + style->ythickness + vertical_padding;
2855 gdk_drawable_get_size (gtk_widget_get_window (widget),
2856 &border->width, &border->height);
2858 switch (arrow_placement)
2860 case GTK_ARROWS_BOTH:
2861 upper->x = border->x;
2862 upper->y = border->y;
2863 upper->width = border->width - 2 * border->x;
2864 upper->height = scroll_arrow_height;
2866 lower->x = border->x;
2867 lower->y = border->height - border->y - scroll_arrow_height;
2868 lower->width = border->width - 2 * border->x;
2869 lower->height = scroll_arrow_height;
2872 case GTK_ARROWS_START:
2873 upper->x = border->x;
2874 upper->y = border->y;
2875 upper->width = (border->width - 2 * border->x) / 2;
2876 upper->height = scroll_arrow_height;
2878 lower->x = border->x + upper->width;
2879 lower->y = border->y;
2880 lower->width = (border->width - 2 * border->x) / 2;
2881 lower->height = scroll_arrow_height;
2884 case GTK_ARROWS_END:
2885 upper->x = border->x;
2886 upper->y = border->height - border->y - scroll_arrow_height;
2887 upper->width = (border->width - 2 * border->x) / 2;
2888 upper->height = scroll_arrow_height;
2890 lower->x = border->x + upper->width;
2891 lower->y = border->height - border->y - scroll_arrow_height;
2892 lower->width = (border->width - 2 * border->x) / 2;
2893 lower->height = scroll_arrow_height;
2897 g_assert_not_reached();
2898 upper->x = upper->y = upper->width = upper->height = 0;
2899 lower->x = lower->y = lower->width = lower->height = 0;
2902 *arrow_space = scroll_arrow_height - 2 * style->ythickness;
2906 gtk_menu_paint (GtkWidget *widget,
2907 GdkEventExpose *event)
2910 GtkMenuPrivate *priv;
2912 GdkRectangle border;
2918 g_return_if_fail (GTK_IS_MENU (widget));
2920 menu = GTK_MENU (widget);
2921 priv = gtk_menu_get_private (menu);
2923 style = gtk_widget_get_style (widget);
2924 window = gtk_widget_get_window (widget);
2926 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2928 if (event->window == window)
2930 gfloat arrow_scaling;
2933 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
2934 arrow_size = arrow_scaling * arrow_space;
2936 gtk_paint_box (style,
2940 &event->area, widget, "menu",
2943 if (menu->upper_arrow_visible && !menu->tearoff_active)
2945 gtk_paint_box (style,
2947 priv->upper_arrow_state,
2949 &event->area, widget, "menu_scroll_arrow_up",
2955 gtk_paint_arrow (style,
2957 priv->upper_arrow_state,
2959 &event->area, widget, "menu_scroll_arrow_up",
2962 upper.x + (upper.width - arrow_size) / 2,
2963 upper.y + style->ythickness + (arrow_space - arrow_size) / 2,
2964 arrow_size, arrow_size);
2967 if (menu->lower_arrow_visible && !menu->tearoff_active)
2969 gtk_paint_box (style,
2971 priv->lower_arrow_state,
2973 &event->area, widget, "menu_scroll_arrow_down",
2979 gtk_paint_arrow (style,
2981 priv->lower_arrow_state,
2983 &event->area, widget, "menu_scroll_arrow_down",
2986 lower.x + (lower.width - arrow_size) / 2,
2987 lower.y + style->ythickness + (arrow_space - arrow_size) / 2,
2988 arrow_size, arrow_size);
2991 else if (event->window == menu->bin_window)
2993 gint y = -border.y + menu->scroll_offset;
2995 if (!menu->tearoff_active)
2997 GtkBorder arrow_border;
2999 get_arrows_border (menu, &arrow_border);
3000 y -= arrow_border.top;
3003 gtk_paint_box (style,
3007 &event->area, widget, "menu",
3009 border.width, border.height);
3014 gtk_menu_expose (GtkWidget *widget,
3015 GdkEventExpose *event)
3017 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3018 g_return_val_if_fail (event != NULL, FALSE);
3020 if (gtk_widget_is_drawable (widget))
3022 gtk_menu_paint (widget, event);
3024 GTK_WIDGET_CLASS (gtk_menu_parent_class)->expose_event (widget, event);
3031 gtk_menu_show (GtkWidget *widget)
3033 GtkMenu *menu = GTK_MENU (widget);
3035 _gtk_menu_refresh_accel_paths (menu, FALSE);
3037 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3043 gtk_menu_size_request_init (GtkSizeRequestIface *iface)
3045 iface->get_width = gtk_menu_get_width;
3046 iface->get_height = gtk_menu_get_height;
3047 iface->get_height_for_width = gtk_menu_get_height_for_width;
3051 gtk_menu_get_width (GtkSizeRequest *widget,
3056 GtkMenuShell *menu_shell;
3057 GtkMenuPrivate *priv;
3061 guint max_toggle_size;
3062 guint max_accel_width;
3063 guint horizontal_padding;
3065 gint child_min, child_nat;
3066 gint min_width, nat_width;
3068 menu = GTK_MENU (widget);
3069 menu_shell = GTK_MENU_SHELL (widget);
3070 priv = gtk_menu_get_private (menu);
3072 style = gtk_widget_get_style (GTK_WIDGET (widget));
3074 min_width = nat_width = 0;
3076 max_toggle_size = 0;
3077 max_accel_width = 0;
3079 children = menu_shell->children;
3086 child = children->data;
3087 children = children->next;
3089 if (! gtk_widget_get_visible (child))
3092 get_effective_child_attach (child, &l, &r, &t, &b);
3094 /* It's important to size_request the child
3095 * before doing the toggle size request, in
3096 * case the toggle size request depends on the size
3097 * request of a child of the child (e.g. for ImageMenuItem)
3100 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
3101 gtk_size_request_get_width (GTK_SIZE_REQUEST (child), &child_min, &child_nat);
3103 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3104 max_toggle_size = MAX (max_toggle_size, toggle_size);
3105 max_accel_width = MAX (max_accel_width,
3106 GTK_MENU_ITEM (child)->accelerator_width);
3108 part = child_min / (r - l);
3109 min_width = MAX (min_width, part);
3111 part = child_nat / (r - l);
3112 nat_width = MAX (nat_width, part);
3115 /* If the menu doesn't include any images or check items
3116 * reserve the space so that all menus are consistent.
3117 * We only do this for 'ordinary' menus, not for combobox
3118 * menus or multi-column menus
3120 if (max_toggle_size == 0 &&
3121 gtk_menu_get_n_columns (menu) == 1 &&
3122 !priv->no_toggle_size)
3124 guint toggle_spacing;
3125 guint indicator_size;
3127 gtk_style_get (style,
3128 GTK_TYPE_CHECK_MENU_ITEM,
3129 "toggle-spacing", &toggle_spacing,
3130 "indicator-size", &indicator_size,
3133 max_toggle_size = indicator_size + toggle_spacing;
3136 min_width += 2 * max_toggle_size + max_accel_width;
3137 min_width *= gtk_menu_get_n_columns (menu);
3139 nat_width += 2 * max_toggle_size + max_accel_width;
3140 nat_width *= gtk_menu_get_n_columns (menu);
3142 gtk_widget_style_get (GTK_WIDGET (menu),
3143 "horizontal-padding", &horizontal_padding,
3146 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3147 min_width += (border_width + horizontal_padding + style->xthickness) * 2;
3148 nat_width += (border_width + horizontal_padding + style->xthickness) * 2;
3150 menu->toggle_size = max_toggle_size;
3151 priv->accel_size = max_accel_width;
3154 *minimum_size = min_width;
3157 *natural_size = nat_width;
3159 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
3161 if (menu->tearoff_active)
3162 gtk_menu_set_tearoff_hints (menu, min_width);
3166 gtk_menu_get_height (GtkSizeRequest *widget,
3172 /* Menus are height-for-width only, just return the height for the minimum width */
3173 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_width (widget, &min_width, NULL);
3174 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_height_for_width (widget, min_width, minimum_size, natural_size);
3178 gtk_menu_get_height_for_width (GtkSizeRequest *widget,
3183 GtkMenu *menu = GTK_MENU (widget);
3184 GtkMenuPrivate *private = gtk_menu_get_private (menu);
3185 guint *min_heights, *nat_heights;
3186 guint vertical_padding, border_width;
3188 gint min_height, nat_height;
3190 gtk_widget_style_get (GTK_WIDGET (menu), "vertical-padding", &vertical_padding, NULL);
3191 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3193 min_height = nat_height = (border_width + vertical_padding + gtk_widget_get_style (GTK_WIDGET (widget))->ythickness) * 2;
3196 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3198 for (i = 0; i < n_heights; i++)
3200 min_height += min_heights[i];
3201 nat_height += nat_heights[i];
3204 if (private->have_position)
3206 GdkScreen *screen = gtk_widget_get_screen (menu->toplevel);
3207 GdkRectangle monitor;
3209 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3211 if (private->y + min_height > monitor.y + monitor.height)
3212 min_height = monitor.y + monitor.height - private->y;
3214 if (private->y + nat_height > monitor.y + monitor.height)
3215 nat_height = monitor.y + monitor.height - private->y;
3217 if (private->y < monitor.y)
3219 min_height -= monitor.y - private->y;
3220 nat_height -= monitor.y - private->y;
3225 *minimum_size = min_height;
3228 *natural_size = nat_height;
3230 g_free (min_heights);
3231 g_free (nat_heights);
3237 gtk_menu_button_scroll (GtkMenu *menu,
3238 GdkEventButton *event)
3240 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
3242 gboolean touchscreen_mode;
3244 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3245 "gtk-touchscreen-mode", &touchscreen_mode,
3248 if (touchscreen_mode)
3249 gtk_menu_handle_scrolling (menu,
3250 event->x_root, event->y_root,
3251 event->type == GDK_BUTTON_PRESS,
3261 pointer_in_menu_window (GtkWidget *widget,
3265 GtkAllocation allocation;
3266 GtkMenu *menu = GTK_MENU (widget);
3268 if (gtk_widget_get_mapped (menu->toplevel))
3270 GtkMenuShell *menu_shell;
3271 gint window_x, window_y;
3273 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3274 &window_x, &window_y);
3276 gtk_widget_get_allocation (widget, &allocation);
3277 if (x_root >= window_x && x_root < window_x + allocation.width &&
3278 y_root >= window_y && y_root < window_y + allocation.height)
3281 menu_shell = GTK_MENU_SHELL (widget);
3283 if (GTK_IS_MENU (menu_shell->parent_menu_shell))
3284 return pointer_in_menu_window (menu_shell->parent_menu_shell,
3292 gtk_menu_button_press (GtkWidget *widget,
3293 GdkEventButton *event)
3295 if (event->type != GDK_BUTTON_PRESS)
3298 /* Don't pass down to menu shell for presses over scroll arrows
3300 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3303 /* Don't pass down to menu shell if a non-menuitem part of the menu
3304 * was clicked. The check for the event_widget being a GtkMenuShell
3305 * works because we have the pointer grabbed on menu_shell->window
3306 * with owner_events=TRUE, so all events that are either outside
3307 * the menu or on its border are delivered relative to
3308 * menu_shell->window.
3310 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3311 pointer_in_menu_window (widget, event->x_root, event->y_root))
3314 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3318 gtk_menu_button_release (GtkWidget *widget,
3319 GdkEventButton *event)
3321 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3323 if (priv->ignore_button_release)
3325 priv->ignore_button_release = FALSE;
3329 if (event->type != GDK_BUTTON_RELEASE)
3332 /* Don't pass down to menu shell for releases over scroll arrows
3334 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3337 /* Don't pass down to menu shell if a non-menuitem part of the menu
3338 * was clicked (see comment in button_press()).
3340 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3341 pointer_in_menu_window (widget, event->x_root, event->y_root))
3343 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3344 * bail out early here so it is in a consistent state for the
3345 * next button_press/button_release in GtkMenuShell.
3348 if (GTK_MENU_SHELL (widget)->active)
3349 GTK_MENU_SHELL (widget)->button = 0;
3354 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3357 static const gchar *
3358 get_accel_path (GtkWidget *menu_item,
3363 GClosure *accel_closure;
3364 GtkAccelGroup *accel_group;
3366 path = _gtk_widget_get_accel_path (menu_item, locked);
3369 path = GTK_MENU_ITEM (menu_item)->accel_path;
3375 label = gtk_bin_get_child (GTK_BIN (menu_item));
3377 if (GTK_IS_ACCEL_LABEL (label))
3379 g_object_get (label,
3380 "accel-closure", &accel_closure,
3384 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3386 *locked = gtk_accel_group_get_is_locked (accel_group);
3396 gtk_menu_key_press (GtkWidget *widget,
3399 GtkMenuShell *menu_shell;
3401 gboolean delete = FALSE;
3402 gboolean can_change_accels;
3403 gchar *accel = NULL;
3404 guint accel_key, accel_mods;
3405 GdkModifierType consumed_modifiers;
3406 GdkDisplay *display;
3408 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3409 g_return_val_if_fail (event != NULL, FALSE);
3411 menu_shell = GTK_MENU_SHELL (widget);
3412 menu = GTK_MENU (widget);
3414 gtk_menu_stop_navigating_submenu (menu);
3416 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3419 display = gtk_widget_get_display (widget);
3421 g_object_get (gtk_widget_get_settings (widget),
3422 "gtk-menu-bar-accel", &accel,
3423 "gtk-can-change-accels", &can_change_accels,
3426 if (accel && *accel)
3429 GdkModifierType mods = 0;
3431 gtk_accelerator_parse (accel, &keyval, &mods);
3434 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3436 /* FIXME this is wrong, needs to be in the global accel resolution
3437 * thing, to properly consider i18n etc., but that probably requires
3438 * AccelGroup changes etc.
3440 if (event->keyval == keyval && (mods & event->state) == mods)
3442 gtk_menu_shell_cancel (menu_shell);
3450 switch (event->keyval)
3452 case GDK_KEY_Delete:
3453 case GDK_KEY_KP_Delete:
3454 case GDK_KEY_BackSpace:
3461 /* Figure out what modifiers went into determining the key symbol */
3462 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3463 event->hardware_keycode, event->state, event->group,
3464 NULL, NULL, NULL, &consumed_modifiers);
3466 accel_key = gdk_keyval_to_lower (event->keyval);
3467 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3469 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
3470 * We re-upper case when we match against the keyval, but display and save in caseless form.
3472 if (accel_key != event->keyval)
3473 accel_mods |= GDK_SHIFT_MASK;
3475 /* Modify the accelerators */
3476 if (can_change_accels &&
3477 menu_shell->active_menu_item &&
3478 gtk_bin_get_child (GTK_BIN (menu_shell->active_menu_item)) && /* no separators */
3479 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
3480 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3482 GtkWidget *menu_item = menu_shell->active_menu_item;
3483 gboolean locked, replace_accels = TRUE;
3486 path = get_accel_path (menu_item, &locked);
3487 if (!path || locked)
3489 /* can't change accelerators on menu_items without paths
3490 * (basically, those items are accelerator-locked).
3492 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
3493 gtk_widget_error_bell (widget);
3499 /* For the keys that act to delete the current setting, we delete
3500 * the current setting if there is one, otherwise, we set the
3501 * key as the accelerator.
3507 if (gtk_accel_map_lookup_entry (path, &key) &&
3508 (key.accel_key || key.accel_mods))
3514 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3518 /* we failed, probably because this key is in use and
3521 /* g_print("failed to change\n"); */
3522 gtk_widget_error_bell (widget);
3531 check_threshold (GtkWidget *widget,
3540 ABS (start_x - x) > THRESHOLD ||
3541 ABS (start_y - y) > THRESHOLD;
3545 definitely_within_item (GtkWidget *widget,
3549 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
3552 gdk_drawable_get_size (window, &w, &h);
3555 check_threshold (widget, 0, 0, x, y) &&
3556 check_threshold (widget, w - 1, 0, x, y) &&
3557 check_threshold (widget, w - 1, h - 1, x, y) &&
3558 check_threshold (widget, 0, h - 1, x, y);
3562 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3564 GtkMenuPrivate *priv;
3566 priv = gtk_menu_get_private (menu);
3568 return priv->navigation_height && priv->navigation_width;
3572 gtk_menu_motion_notify (GtkWidget *widget,
3573 GdkEventMotion *event)
3575 GtkWidget *menu_item;
3577 GtkMenuShell *menu_shell;
3580 gboolean need_enter;
3582 if (GTK_IS_MENU (widget))
3584 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3586 if (priv->ignore_button_release)
3587 priv->ignore_button_release = FALSE;
3589 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3593 /* We received the event for one of two reasons:
3595 * a) We are the active menu, and did gtk_grab_add()
3596 * b) The widget is a child of ours, and the event was propagated
3598 * Since for computation of navigation regions, we want the menu which
3599 * is the parent of the menu item, for a), we need to find that menu,
3600 * which may be different from 'widget'.
3602 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3603 parent = gtk_widget_get_parent (menu_item);
3604 if (!GTK_IS_MENU_ITEM (menu_item) ||
3605 !GTK_IS_MENU (parent))
3608 menu_shell = GTK_MENU_SHELL (parent);
3609 menu = GTK_MENU (menu_shell);
3611 if (definitely_within_item (menu_item, event->x, event->y))
3612 menu_shell->activate_time = 0;
3614 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->ignore_enter);
3616 /* Check to see if we are within an active submenu's navigation region
3618 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3621 /* Make sure we pop down if we enter a non-selectable menu item, so we
3622 * don't show a submenu when the cursor is outside the stay-up triangle.
3624 if (!_gtk_menu_item_is_selectable (menu_item))
3626 /* We really want to deselect, but this gives the menushell code
3627 * a chance to do some bookkeeping about the menuitem.
3629 gtk_menu_shell_select_item (menu_shell, menu_item);
3635 /* The menu is now sensitive to enter events on its items, but
3636 * was previously sensitive. So we fake an enter event.
3640 menu_shell->ignore_enter = FALSE;
3642 gdk_drawable_get_size (event->window, &width, &height);
3643 if (event->x >= 0 && event->x < width &&
3644 event->y >= 0 && event->y < height)
3646 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3649 send_event->crossing.window = g_object_ref (event->window);
3650 send_event->crossing.time = event->time;
3651 send_event->crossing.send_event = TRUE;
3652 send_event->crossing.x_root = event->x_root;
3653 send_event->crossing.y_root = event->y_root;
3654 send_event->crossing.x = event->x;
3655 send_event->crossing.y = event->y;
3656 send_event->crossing.state = event->state;
3657 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3659 /* We send the event to 'widget', the currently active menu,
3660 * instead of 'menu', the menu that the pointer is in. This
3661 * will ensure that the event will be ignored unless the
3662 * menuitem is a child of the active menu or some parent
3663 * menu of the active menu.
3665 result = gtk_widget_event (widget, send_event);
3666 gdk_event_free (send_event);
3676 get_double_arrows (GtkMenu *menu)
3678 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3679 gboolean double_arrows;
3680 GtkArrowPlacement arrow_placement;
3682 gtk_widget_style_get (GTK_WIDGET (menu),
3683 "double-arrows", &double_arrows,
3684 "arrow-placement", &arrow_placement,
3687 if (arrow_placement != GTK_ARROWS_BOTH)
3690 return double_arrows || (priv->initially_pushed_in &&
3691 menu->scroll_offset != 0);
3695 gtk_menu_scroll_by (GtkMenu *menu,
3698 GtkMenuPrivate *priv;
3699 GtkBorder arrow_border;
3702 gint view_width, view_height;
3703 gboolean double_arrows;
3705 widget = GTK_WIDGET (menu);
3706 offset = menu->scroll_offset + step;
3707 priv = gtk_menu_get_private (menu);
3709 get_arrows_border (menu, &arrow_border);
3711 double_arrows = get_double_arrows (menu);
3713 /* If we scroll upward and the non-visible top part
3714 * is smaller than the scroll arrow it would be
3715 * pretty stupid to show the arrow and taking more
3716 * screen space than just scrolling to the top.
3719 if ((step < 0) && (offset < arrow_border.top))
3722 /* Don't scroll over the top if we weren't before: */
3723 if ((menu->scroll_offset >= 0) && (offset < 0))
3726 gdk_drawable_get_size (gtk_widget_get_window (widget),
3727 &view_width, &view_height);
3729 if (menu->scroll_offset == 0 &&
3730 view_height >= priv->requested_height)
3733 /* Don't scroll past the bottom if we weren't before: */
3734 if (menu->scroll_offset > 0)
3735 view_height -= arrow_border.top;
3737 /* When both arrows are always shown, reduce
3738 * view height even more.
3741 view_height -= arrow_border.bottom;
3743 if ((menu->scroll_offset + view_height <= priv->requested_height) &&
3744 (offset + view_height > priv->requested_height))
3745 offset = priv->requested_height - view_height;
3747 if (offset != menu->scroll_offset)
3748 gtk_menu_scroll_to (menu, offset);
3752 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3753 gboolean touchscreen_mode)
3755 gboolean upper_visible;
3756 gboolean lower_visible;
3758 upper_visible = menu->upper_arrow_visible;
3759 lower_visible = menu->lower_arrow_visible;
3761 gtk_menu_scroll_by (menu, menu->scroll_step);
3763 if (touchscreen_mode &&
3764 (upper_visible != menu->upper_arrow_visible ||
3765 lower_visible != menu->lower_arrow_visible))
3767 /* We are about to hide a scroll arrow while the mouse is pressed,
3768 * this would cause the uncovered menu item to be activated on button
3769 * release. Therefore we need to ignore button release here
3771 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3772 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3777 gtk_menu_scroll_timeout (gpointer data)
3780 gboolean touchscreen_mode;
3782 menu = GTK_MENU (data);
3784 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3785 "gtk-touchscreen-mode", &touchscreen_mode,
3788 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3794 gtk_menu_scroll_timeout_initial (gpointer data)
3798 gboolean touchscreen_mode;
3800 menu = GTK_MENU (data);
3802 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3803 "gtk-timeout-repeat", &timeout,
3804 "gtk-touchscreen-mode", &touchscreen_mode,
3807 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3809 gtk_menu_remove_scroll_timeout (menu);
3811 menu->timeout_id = gdk_threads_add_timeout (timeout,
3812 gtk_menu_scroll_timeout,
3819 gtk_menu_start_scrolling (GtkMenu *menu)
3822 gboolean touchscreen_mode;
3824 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3825 "gtk-timeout-repeat", &timeout,
3826 "gtk-touchscreen-mode", &touchscreen_mode,
3829 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3831 menu->timeout_id = gdk_threads_add_timeout (timeout,
3832 gtk_menu_scroll_timeout_initial,
3837 gtk_menu_scroll (GtkWidget *widget,
3838 GdkEventScroll *event)
3840 GtkMenu *menu = GTK_MENU (widget);
3842 switch (event->direction)
3844 case GDK_SCROLL_RIGHT:
3845 case GDK_SCROLL_DOWN:
3846 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3848 case GDK_SCROLL_LEFT:
3850 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3858 get_arrows_sensitive_area (GtkMenu *menu,
3859 GdkRectangle *upper,
3860 GdkRectangle *lower)
3862 GtkArrowPlacement arrow_placement;
3863 GtkWidget *widget = GTK_WIDGET (menu);
3867 guint vertical_padding;
3869 gint scroll_arrow_height;
3871 window = gtk_widget_get_window (widget);
3873 gdk_drawable_get_size (window, &width, &height);
3875 gtk_widget_style_get (widget,
3876 "vertical-padding", &vertical_padding,
3877 "scroll-arrow-vlength", &scroll_arrow_height,
3878 "arrow-placement", &arrow_placement,
3881 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) +
3882 gtk_widget_get_style (widget)->ythickness + vertical_padding;
3884 gdk_window_get_position (window, &win_x, &win_y);
3886 switch (arrow_placement)
3888 case GTK_ARROWS_BOTH:
3893 upper->width = width;
3894 upper->height = scroll_arrow_height + border;
3900 lower->y = win_y + height - border - scroll_arrow_height;
3901 lower->width = width;
3902 lower->height = scroll_arrow_height + border;
3906 case GTK_ARROWS_START:
3911 upper->width = width / 2;
3912 upper->height = scroll_arrow_height + border;
3917 lower->x = win_x + width / 2;
3919 lower->width = width / 2;
3920 lower->height = scroll_arrow_height + border;
3924 case GTK_ARROWS_END:
3928 upper->y = win_y + height - border - scroll_arrow_height;
3929 upper->width = width / 2;
3930 upper->height = scroll_arrow_height + border;
3935 lower->x = win_x + width / 2;
3936 lower->y = win_y + height - border - scroll_arrow_height;
3937 lower->width = width / 2;
3938 lower->height = scroll_arrow_height + border;
3946 gtk_menu_handle_scrolling (GtkMenu *menu,
3952 GtkMenuShell *menu_shell;
3953 GtkMenuPrivate *priv;
3956 gboolean scroll_fast = FALSE;
3958 gboolean touchscreen_mode;
3960 priv = gtk_menu_get_private (menu);
3962 menu_shell = GTK_MENU_SHELL (menu);
3964 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3965 "gtk-touchscreen-mode", &touchscreen_mode,
3968 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3973 /* upper arrow handling */
3975 get_arrows_sensitive_area (menu, &rect, NULL);
3978 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3979 (x >= rect.x) && (x < rect.x + rect.width) &&
3980 (y >= rect.y) && (y < rect.y + rect.height))
3985 if (touchscreen_mode)
3986 menu->upper_arrow_prelight = in_arrow;
3988 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3990 gboolean arrow_pressed = FALSE;
3992 if (menu->upper_arrow_visible && !menu->tearoff_active)
3994 if (touchscreen_mode)
3996 if (enter && menu->upper_arrow_prelight)
3998 if (menu->timeout_id == 0)
4000 /* Deselect the active item so that
4001 * any submenus are popped down
4003 gtk_menu_shell_deselect (menu_shell);
4005 gtk_menu_remove_scroll_timeout (menu);
4006 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
4010 /* Only do stuff on click. */
4011 gtk_menu_start_scrolling (menu);
4012 arrow_pressed = TRUE;
4017 arrow_pressed = TRUE;
4022 gtk_menu_stop_scrolling (menu);
4025 else /* !touchscreen_mode */
4027 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
4029 if (enter && in_arrow &&
4030 (!menu->upper_arrow_prelight ||
4031 menu->scroll_fast != scroll_fast))
4033 menu->upper_arrow_prelight = TRUE;
4034 menu->scroll_fast = scroll_fast;
4036 /* Deselect the active item so that
4037 * any submenus are popped down
4039 gtk_menu_shell_deselect (menu_shell);
4041 gtk_menu_remove_scroll_timeout (menu);
4042 menu->scroll_step = scroll_fast ?
4043 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
4046 gdk_threads_add_timeout (scroll_fast ?
4047 MENU_SCROLL_TIMEOUT2 :
4048 MENU_SCROLL_TIMEOUT1,
4049 gtk_menu_scroll_timeout, menu);
4051 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
4053 gtk_menu_stop_scrolling (menu);
4058 /* gtk_menu_start_scrolling() might have hit the top of the
4059 * menu, so check if the button isn't insensitive before
4060 * changing it to something else.
4062 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
4064 GtkStateType arrow_state = GTK_STATE_NORMAL;
4067 arrow_state = GTK_STATE_ACTIVE;
4068 else if (menu->upper_arrow_prelight)
4069 arrow_state = GTK_STATE_PRELIGHT;
4071 if (arrow_state != priv->upper_arrow_state)
4073 priv->upper_arrow_state = arrow_state;
4075 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4081 /* lower arrow handling */
4083 get_arrows_sensitive_area (menu, NULL, &rect);
4086 if (menu->lower_arrow_visible && !menu->tearoff_active &&
4087 (x >= rect.x) && (x < rect.x + rect.width) &&
4088 (y >= rect.y) && (y < rect.y + rect.height))
4093 if (touchscreen_mode)
4094 menu->lower_arrow_prelight = in_arrow;
4096 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4098 gboolean arrow_pressed = FALSE;
4100 if (menu->lower_arrow_visible && !menu->tearoff_active)
4102 if (touchscreen_mode)
4104 if (enter && menu->lower_arrow_prelight)
4106 if (menu->timeout_id == 0)
4108 /* Deselect the active item so that
4109 * any submenus are popped down
4111 gtk_menu_shell_deselect (menu_shell);
4113 gtk_menu_remove_scroll_timeout (menu);
4114 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4118 /* Only do stuff on click. */
4119 gtk_menu_start_scrolling (menu);
4120 arrow_pressed = TRUE;
4125 arrow_pressed = TRUE;
4130 gtk_menu_stop_scrolling (menu);
4133 else /* !touchscreen_mode */
4135 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4137 if (enter && in_arrow &&
4138 (!menu->lower_arrow_prelight ||
4139 menu->scroll_fast != scroll_fast))
4141 menu->lower_arrow_prelight = TRUE;
4142 menu->scroll_fast = scroll_fast;
4144 /* Deselect the active item so that
4145 * any submenus are popped down
4147 gtk_menu_shell_deselect (menu_shell);
4149 gtk_menu_remove_scroll_timeout (menu);
4150 menu->scroll_step = scroll_fast ?
4151 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
4154 gdk_threads_add_timeout (scroll_fast ?
4155 MENU_SCROLL_TIMEOUT2 :
4156 MENU_SCROLL_TIMEOUT1,
4157 gtk_menu_scroll_timeout, menu);
4159 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
4161 gtk_menu_stop_scrolling (menu);
4166 /* gtk_menu_start_scrolling() might have hit the bottom of the
4167 * menu, so check if the button isn't insensitive before
4168 * changing it to something else.
4170 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4172 GtkStateType arrow_state = GTK_STATE_NORMAL;
4175 arrow_state = GTK_STATE_ACTIVE;
4176 else if (menu->lower_arrow_prelight)
4177 arrow_state = GTK_STATE_PRELIGHT;
4179 if (arrow_state != priv->lower_arrow_state)
4181 priv->lower_arrow_state = arrow_state;
4183 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4191 gtk_menu_enter_notify (GtkWidget *widget,
4192 GdkEventCrossing *event)
4194 GtkWidget *menu_item;
4196 gboolean touchscreen_mode;
4198 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4199 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4200 event->mode == GDK_CROSSING_STATE_CHANGED)
4203 g_object_get (gtk_widget_get_settings (widget),
4204 "gtk-touchscreen-mode", &touchscreen_mode,
4207 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4208 if (GTK_IS_MENU (widget))
4210 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4212 if (!menu_shell->ignore_enter)
4213 gtk_menu_handle_scrolling (GTK_MENU (widget),
4214 event->x_root, event->y_root, TRUE, TRUE);
4217 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4219 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4221 if (GTK_IS_MENU (menu))
4223 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
4224 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4226 if (priv->seen_item_enter)
4228 /* This is the second enter we see for an item
4229 * on this menu. This means a release should always
4232 menu_shell->activate_time = 0;
4234 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4235 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4237 if (definitely_within_item (menu_item, event->x, event->y))
4239 /* This is an actual user-enter (ie. not a pop-under)
4240 * In this case, the user must either have entered
4241 * sufficiently far enough into the item, or he must move
4242 * far enough away from the enter point. (see
4243 * gtk_menu_motion_notify())
4245 menu_shell->activate_time = 0;
4249 priv->seen_item_enter = TRUE;
4253 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4254 * will not correspond to the event widget's parent. Check to see
4255 * if we are in the parent's navigation region.
4257 parent = gtk_widget_get_parent (menu_item);
4258 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4259 gtk_menu_navigating_submenu (GTK_MENU (parent),
4260 event->x_root, event->y_root))
4263 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4267 gtk_menu_leave_notify (GtkWidget *widget,
4268 GdkEventCrossing *event)
4270 GtkMenuShell *menu_shell;
4272 GtkMenuItem *menu_item;
4273 GtkWidget *event_widget;
4275 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4276 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4277 event->mode == GDK_CROSSING_STATE_CHANGED)
4280 menu = GTK_MENU (widget);
4281 menu_shell = GTK_MENU_SHELL (widget);
4283 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4286 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4288 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4290 if (!GTK_IS_MENU_ITEM (event_widget))
4293 menu_item = GTK_MENU_ITEM (event_widget);
4295 /* Here we check to see if we're leaving an active menu item with a submenu,
4296 * in which case we enter submenu navigation mode.
4298 if (menu_shell->active_menu_item != NULL
4299 && menu_item->submenu != NULL
4300 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
4302 if (GTK_MENU_SHELL (menu_item->submenu)->active)
4304 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4307 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
4309 /* We are leaving an active menu item with nonactive submenu.
4310 * Deselect it so we don't surprise the user with by popping
4311 * up a submenu _after_ he left the item.
4313 gtk_menu_shell_deselect (menu_shell);
4318 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4322 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4324 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4326 priv->navigation_x = 0;
4327 priv->navigation_y = 0;
4328 priv->navigation_width = 0;
4329 priv->navigation_height = 0;
4331 if (menu->navigation_timeout)
4333 g_source_remove (menu->navigation_timeout);
4334 menu->navigation_timeout = 0;
4338 /* When the timeout is elapsed, the navigation region is destroyed
4339 * and the menuitem under the pointer (if any) is selected.
4342 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4344 GtkMenuPopdownData *popdown_data = user_data;
4345 GtkMenu *menu = popdown_data->menu;
4346 GdkWindow *child_window;
4348 gtk_menu_stop_navigating_submenu (menu);
4350 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4352 child_window = gdk_window_get_device_position (menu->bin_window,
4353 popdown_data->device,
4358 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4360 send_event->crossing.window = g_object_ref (child_window);
4361 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4362 send_event->crossing.send_event = TRUE;
4363 gdk_event_set_device (send_event, popdown_data->device);
4365 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4367 gdk_event_free (send_event);
4375 gtk_menu_navigating_submenu (GtkMenu *menu,
4379 GtkMenuPrivate *priv;
4382 if (!gtk_menu_has_navigation_triangle (menu))
4385 priv = gtk_menu_get_private (menu);
4386 width = priv->navigation_width;
4387 height = priv->navigation_height;
4389 /* check if x/y are in the triangle spanned by the navigation parameters */
4391 /* 1) Move the coordinates so the triangle starts at 0,0 */
4392 event_x -= priv->navigation_x;
4393 event_y -= priv->navigation_y;
4395 /* 2) Ensure both legs move along the positive axis */
4407 /* 3) Check that the given coordinate is inside the triangle. The formula
4408 * is a transformed form of this formula: x/w + y/h <= 1
4410 if (event_x >= 0 && event_y >= 0 &&
4411 event_x * height + event_y * width <= width * height)
4417 gtk_menu_stop_navigating_submenu (menu);
4423 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4424 GtkMenuItem *menu_item,
4425 GdkEventCrossing *event)
4427 gint submenu_left = 0;
4428 gint submenu_right = 0;
4429 gint submenu_top = 0;
4430 gint submenu_bottom = 0;
4433 GtkWidget *event_widget;
4434 GtkMenuPopdownData *popdown_data;
4435 GtkMenuPrivate *priv;
4438 g_return_if_fail (menu_item->submenu != NULL);
4439 g_return_if_fail (event != NULL);
4441 priv = gtk_menu_get_private (menu);
4443 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4445 window = gtk_widget_get_window (menu_item->submenu);
4446 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4447 gdk_drawable_get_size (window, &width, &height);
4449 submenu_right = submenu_left + width;
4450 submenu_bottom = submenu_top + height;
4452 gdk_drawable_get_size (gtk_widget_get_window (event_widget),
4455 if (event->x >= 0 && event->x < width)
4459 gtk_menu_stop_navigating_submenu (menu);
4461 /* The navigation region is the triangle closest to the x/y
4462 * location of the rectangle. This is why the width or height
4466 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
4469 priv->navigation_x = submenu_left;
4470 priv->navigation_width = event->x_root - submenu_left;
4475 priv->navigation_x = submenu_right;
4476 priv->navigation_width = event->x_root - submenu_right;
4482 priv->navigation_y = event->y_root;
4483 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4485 if (priv->navigation_height >= 0)
4491 priv->navigation_y = event->y_root;
4492 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4494 if (priv->navigation_height <= 0)
4498 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4499 "gtk-menu-popdown-delay", &popdown_delay,
4502 popdown_data = g_new (GtkMenuPopdownData, 1);
4503 popdown_data->menu = menu;
4504 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4506 menu->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4508 gtk_menu_stop_navigating_submenu_cb,
4510 (GDestroyNotify) g_free);
4515 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4519 g_return_if_fail (GTK_IS_MENU (menu_shell));
4521 parent = menu_shell->parent_menu_shell;
4523 menu_shell->activate_time = 0;
4524 gtk_menu_popdown (GTK_MENU (menu_shell));
4527 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4531 gtk_menu_position (GtkMenu *menu)
4534 GtkRequisition requisition;
4535 GtkMenuPrivate *private;
4540 GdkScreen *pointer_screen;
4541 GdkRectangle monitor;
4544 g_return_if_fail (GTK_IS_MENU (menu));
4546 widget = GTK_WIDGET (menu);
4548 screen = gtk_widget_get_screen (widget);
4549 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4550 gdk_display_get_device_state (gdk_screen_get_display (screen),
4551 pointer, &pointer_screen, &x, &y, NULL);
4553 /* Get the minimum height for minimum width to figure out
4554 * the right place to popup the menu.
4556 gtk_size_request_get_size (GTK_SIZE_REQUEST (widget), &requisition, NULL);
4558 if (pointer_screen != screen)
4560 /* Pointer is on a different screen; roughly center the
4561 * menu on the screen. If someone was using multiscreen
4562 * + Xinerama together they'd probably want something
4563 * fancier; but that is likely to be vanishingly rare.
4565 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4566 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4569 private = gtk_menu_get_private (menu);
4570 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4572 private->initially_pushed_in = FALSE;
4574 /* Set the type hint here to allow custom position functions to set a different hint */
4575 if (!gtk_widget_get_visible (menu->toplevel))
4576 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4578 if (menu->position_func)
4580 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
4581 menu->position_func_data);
4583 if (private->monitor_num < 0)
4584 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4586 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4590 GtkStyle *style = gtk_widget_get_style (widget);
4591 gint space_left, space_right, space_above, space_below;
4594 gint xthickness = style->xthickness;
4595 gint ythickness = style->ythickness;
4596 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4598 /* The placement of popup menus horizontally works like this (with
4599 * RTL in parentheses)
4601 * - If there is enough room to the right (left) of the mouse cursor,
4602 * position the menu there.
4604 * - Otherwise, if if there is enough room to the left (right) of the
4605 * mouse cursor, position the menu there.
4607 * - Otherwise if the menu is smaller than the monitor, position it
4608 * on the side of the mouse cursor that has the most space available
4610 * - Otherwise (if there is simply not enough room for the menu on the
4611 * monitor), position it as far left (right) as possible.
4613 * Positioning in the vertical direction is similar: first try below
4614 * mouse cursor, then above.
4616 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4618 space_left = x - monitor.x;
4619 space_right = monitor.x + monitor.width - x - 1;
4620 space_above = y - monitor.y;
4621 space_below = monitor.y + monitor.height - y - 1;
4623 /* position horizontally */
4625 /* the amount of space we need to position the menu. Note the
4626 * menu is offset "xthickness" pixels
4628 needed_width = requisition.width - xthickness;
4630 if (needed_width <= space_left ||
4631 needed_width <= space_right)
4633 if ((rtl && needed_width <= space_left) ||
4634 (!rtl && needed_width > space_right))
4637 x = x + xthickness - requisition.width + 1;
4641 /* position right */
4645 /* x is clamped on-screen further down */
4647 else if (requisition.width <= monitor.width)
4649 /* the menu is too big to fit on either side of the mouse
4650 * cursor, but smaller than the monitor. Position it on
4651 * the side that has the most space
4653 if (space_left > space_right)
4661 x = monitor.x + monitor.width - requisition.width;
4664 else /* menu is simply too big for the monitor */
4669 x = monitor.x + monitor.width - requisition.width;
4678 /* Position vertically. The algorithm is the same as above, but
4679 * simpler because we don't have to take RTL into account.
4681 needed_height = requisition.height - ythickness;
4683 if (needed_height <= space_above ||
4684 needed_height <= space_below)
4686 if (needed_height <= space_below)
4689 y = y + ythickness - requisition.height + 1;
4691 y = CLAMP (y, monitor.y,
4692 monitor.y + monitor.height - requisition.height);
4694 else if (needed_height > space_below && needed_height > space_above)
4696 if (space_below >= space_above)
4697 y = monitor.y + monitor.height - requisition.height;
4709 if (private->initially_pushed_in)
4711 menu_height = requisition.height;
4713 if (y + menu_height > monitor.y + monitor.height)
4715 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4716 y = (monitor.y + monitor.height) - menu_height;
4721 scroll_offset += monitor.y - y;
4726 /* FIXME: should this be done in the various position_funcs ? */
4727 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4729 if (GTK_MENU_SHELL (menu)->active)
4731 private->have_position = TRUE;
4736 if (y + requisition.height > monitor.y + monitor.height)
4737 requisition.height = (monitor.y + monitor.height) - y;
4741 scroll_offset += monitor.y - y;
4742 requisition.height -= monitor.y - y;
4746 if (scroll_offset > 0)
4748 GtkBorder arrow_border;
4750 get_arrows_border (menu, &arrow_border);
4751 scroll_offset += arrow_border.top;
4754 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
4757 if (!GTK_MENU_SHELL (menu)->active)
4759 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4760 requisition.width, requisition.height);
4763 menu->scroll_offset = scroll_offset;
4767 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4769 if (menu->timeout_id)
4771 g_source_remove (menu->timeout_id);
4772 menu->timeout_id = 0;
4777 gtk_menu_stop_scrolling (GtkMenu *menu)
4779 gboolean touchscreen_mode;
4781 gtk_menu_remove_scroll_timeout (menu);
4783 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4784 "gtk-touchscreen-mode", &touchscreen_mode,
4787 if (!touchscreen_mode)
4789 menu->upper_arrow_prelight = FALSE;
4790 menu->lower_arrow_prelight = FALSE;
4795 gtk_menu_scroll_to (GtkMenu *menu,
4798 GtkMenuPrivate *priv;
4799 GtkAllocation allocation;
4800 GtkBorder arrow_border;
4804 gint view_width, view_height;
4807 guint vertical_padding;
4808 guint horizontal_padding;
4809 gboolean double_arrows;
4811 widget = GTK_WIDGET (menu);
4812 priv = gtk_menu_get_private (menu);
4814 if (menu->tearoff_active &&
4815 menu->tearoff_adjustment &&
4816 (menu->tearoff_adjustment->value != offset))
4818 menu->tearoff_adjustment->value =
4820 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4821 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4824 /* Move/resize the viewport according to arrows: */
4825 gtk_widget_get_allocation (widget, &allocation);
4826 view_width = allocation.width;
4827 view_height = allocation.height;
4829 style = gtk_widget_get_style (widget);
4831 gtk_widget_style_get (GTK_WIDGET (menu),
4832 "vertical-padding", &vertical_padding,
4833 "horizontal-padding", &horizontal_padding,
4836 double_arrows = get_double_arrows (menu);
4838 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4840 view_width -= (border_width + style->xthickness + horizontal_padding) * 2;
4841 view_height -= (border_width + style->ythickness + vertical_padding) * 2;
4842 menu_height = priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2;
4844 x = border_width + style->xthickness + horizontal_padding;
4845 y = border_width + style->ythickness + vertical_padding;
4847 if (double_arrows && !menu->tearoff_active)
4849 if (view_height < menu_height ||
4850 (offset > 0 && menu->scroll_offset > 0) ||
4851 (offset < 0 && menu->scroll_offset < 0))
4853 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4854 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4855 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4857 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4858 gtk_widget_queue_draw (GTK_WIDGET (menu));
4860 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4862 get_arrows_border (menu, &arrow_border);
4863 y += arrow_border.top;
4864 view_height -= arrow_border.top;
4865 view_height -= arrow_border.bottom;
4868 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4869 else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4870 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4871 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4873 if (offset >= menu_height - view_height)
4874 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4875 else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4876 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4877 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4879 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4880 (priv->lower_arrow_state != lower_arrow_previous_state))
4881 gtk_widget_queue_draw (GTK_WIDGET (menu));
4883 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4884 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4886 /* At the upper border, possibly remove timeout */
4887 if (menu->scroll_step < 0)
4889 gtk_menu_stop_scrolling (menu);
4890 gtk_widget_queue_draw (GTK_WIDGET (menu));
4894 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4895 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4897 /* At the lower border, possibly remove timeout */
4898 if (menu->scroll_step > 0)
4900 gtk_menu_stop_scrolling (menu);
4901 gtk_widget_queue_draw (GTK_WIDGET (menu));
4905 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4909 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4910 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4912 gtk_menu_stop_scrolling (menu);
4913 gtk_widget_queue_draw (GTK_WIDGET (menu));
4916 else if (!menu->tearoff_active)
4918 gboolean last_visible;
4920 last_visible = menu->upper_arrow_visible;
4921 menu->upper_arrow_visible = offset > 0;
4923 /* upper_arrow_visible may have changed, so requery the border */
4924 get_arrows_border (menu, &arrow_border);
4925 view_height -= arrow_border.top;
4927 if ((last_visible != menu->upper_arrow_visible) &&
4928 !menu->upper_arrow_visible)
4930 menu->upper_arrow_prelight = FALSE;
4932 /* If we hid the upper arrow, possibly remove timeout */
4933 if (menu->scroll_step < 0)
4935 gtk_menu_stop_scrolling (menu);
4936 gtk_widget_queue_draw (GTK_WIDGET (menu));
4940 last_visible = menu->lower_arrow_visible;
4941 menu->lower_arrow_visible = offset < menu_height - view_height;
4943 /* lower_arrow_visible may have changed, so requery the border */
4944 get_arrows_border (menu, &arrow_border);
4945 view_height -= arrow_border.bottom;
4947 if ((last_visible != menu->lower_arrow_visible) &&
4948 !menu->lower_arrow_visible)
4950 menu->lower_arrow_prelight = FALSE;
4952 /* If we hid the lower arrow, possibly remove timeout */
4953 if (menu->scroll_step > 0)
4955 gtk_menu_stop_scrolling (menu);
4956 gtk_widget_queue_draw (GTK_WIDGET (menu));
4960 y += arrow_border.top;
4963 /* Scroll the menu: */
4964 if (gtk_widget_get_realized (widget))
4965 gdk_window_move (menu->bin_window, 0, -offset);
4967 if (gtk_widget_get_realized (widget))
4968 gdk_window_move_resize (menu->view_window,
4974 menu->scroll_offset = offset;
4978 compute_child_offset (GtkMenu *menu,
4979 GtkWidget *menu_item,
4982 gboolean *is_last_child)
4984 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4985 gint item_top_attach;
4986 gint item_bottom_attach;
4987 gint child_offset = 0;
4990 get_effective_child_attach (menu_item, NULL, NULL,
4991 &item_top_attach, &item_bottom_attach);
4993 /* there is a possibility that we get called before _size_request, so
4994 * check the height table for safety.
4996 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4999 /* when we have a row with only invisible children, it's height will
5000 * be zero, so there's no need to check WIDGET_VISIBLE here
5002 for (i = 0; i < item_top_attach; i++)
5003 child_offset += priv->heights[i];
5006 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
5008 *offset = child_offset;
5010 *height = priv->heights[item_top_attach];
5016 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
5017 GtkWidget *menu_item)
5019 GtkMenu *menu = GTK_MENU (menu_shell);
5020 GtkWidget *widget = GTK_WIDGET (menu_shell);
5021 gint child_offset, child_height;
5025 gboolean last_child = 0;
5027 /* We need to check if the selected item fully visible.
5028 * If not we need to scroll the menu so that it becomes fully
5032 if (compute_child_offset (menu, menu_item,
5033 &child_offset, &child_height, &last_child))
5035 guint vertical_padding;
5036 gboolean double_arrows;
5038 y = menu->scroll_offset;
5039 gdk_drawable_get_size (gtk_widget_get_window (widget),
5042 gtk_widget_style_get (widget,
5043 "vertical-padding", &vertical_padding,
5046 double_arrows = get_double_arrows (menu);
5048 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5049 2 * gtk_widget_get_style (widget)->ythickness +
5050 2 * vertical_padding;
5051 if (child_offset < y)
5053 /* Ignore the enter event we might get if the pointer is on the menu
5055 menu_shell->ignore_enter = TRUE;
5056 gtk_menu_scroll_to (menu, child_offset);
5060 GtkBorder arrow_border;
5064 get_arrows_border (menu, &arrow_border);
5065 if (!menu->tearoff_active)
5066 arrow_height = arrow_border.top + arrow_border.bottom;
5068 if (child_offset + child_height > y + height - arrow_height)
5071 if ((!last_child && !menu->tearoff_active) || double_arrows)
5072 arrow_height += arrow_border.bottom;
5074 y = child_offset + child_height - height + arrow_height;
5075 if (((y > 0) && !menu->tearoff_active) || double_arrows)
5077 /* Need upper arrow */
5078 arrow_height += arrow_border.top;
5079 y = child_offset + child_height - height + arrow_height;
5081 /* Ignore the enter event we might get if the pointer is on the menu
5083 menu_shell->ignore_enter = TRUE;
5084 gtk_menu_scroll_to (menu, y);
5092 gtk_menu_select_item (GtkMenuShell *menu_shell,
5093 GtkWidget *menu_item)
5095 GtkMenu *menu = GTK_MENU (menu_shell);
5097 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5098 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5100 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5104 /* Reparent the menu, taking care of the refcounting
5106 * If unrealize is true we force a unrealize while reparenting the parent.
5107 * This can help eliminate flicker in some cases.
5109 * What happens is that when the menu is unrealized and then re-realized,
5110 * the allocations are as follows:
5112 * parent - 1x1 at (0,0)
5113 * child1 - 100x20 at (0,0)
5114 * child2 - 100x20 at (0,20)
5115 * child3 - 100x20 at (0,40)
5117 * That is, the parent is small but the children are full sized. Then,
5118 * when the queued_resize gets processed, the parent gets resized to
5121 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5122 * contains the following logic:
5124 * - if a move or resize operation on a window would change the clip
5125 * region on the children, then before the window is resized
5126 * the background for children is temporarily set to None, the
5127 * move/resize done, and the background for the children restored.
5129 * So, at the point where the parent is resized to final size, the
5130 * background for the children is temporarily None, and thus they
5131 * are not cleared to the background color and the previous background
5132 * (the image of the menu) is left in place.
5135 gtk_menu_reparent (GtkMenu *menu,
5136 GtkWidget *new_parent,
5139 GtkObject *object = GTK_OBJECT (menu);
5140 GtkWidget *widget = GTK_WIDGET (menu);
5141 gboolean was_floating = g_object_is_floating (object);
5143 g_object_ref_sink (object);
5147 g_object_ref (object);
5148 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5149 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5150 g_object_unref (object);
5153 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
5156 g_object_force_floating (G_OBJECT (object));
5158 g_object_unref (object);
5162 gtk_menu_show_all (GtkWidget *widget)
5164 /* Show children, but not self. */
5165 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5170 gtk_menu_hide_all (GtkWidget *widget)
5172 /* Hide children, but not self. */
5173 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
5177 * gtk_menu_set_screen:
5178 * @menu: a #GtkMenu.
5179 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5180 * determined by the widget the menu is attached to.
5182 * Sets the #GdkScreen on which the menu will be displayed.
5187 gtk_menu_set_screen (GtkMenu *menu,
5190 g_return_if_fail (GTK_IS_MENU (menu));
5191 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
5193 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5197 menu_change_screen (menu, screen);
5201 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5203 attach_widget_screen_changed (attach_widget, NULL, menu);
5209 * @menu: a #GtkMenu.
5210 * @child: a #GtkMenuItem.
5211 * @left_attach: The column number to attach the left side of the item to.
5212 * @right_attach: The column number to attach the right side of the item to.
5213 * @top_attach: The row number to attach the top of the item to.
5214 * @bottom_attach: The row number to attach the bottom of the item to.
5216 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5217 * an item will occupy is specified by @left_attach, @right_attach,
5218 * @top_attach and @bottom_attach. These each represent the leftmost,
5219 * rightmost, uppermost and lower column and row numbers of the table.
5220 * (Columns and rows are indexed from zero).
5222 * Note that this function is not related to gtk_menu_detach().
5227 gtk_menu_attach (GtkMenu *menu,
5232 guint bottom_attach)
5234 GtkMenuShell *menu_shell;
5237 g_return_if_fail (GTK_IS_MENU (menu));
5238 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5239 parent = gtk_widget_get_parent (child);
5240 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5241 g_return_if_fail (left_attach < right_attach);
5242 g_return_if_fail (top_attach < bottom_attach);
5244 menu_shell = GTK_MENU_SHELL (menu);
5248 AttachInfo *ai = get_attach_info (child);
5250 ai->left_attach = left_attach;
5251 ai->right_attach = right_attach;
5252 ai->top_attach = top_attach;
5253 ai->bottom_attach = bottom_attach;
5255 menu_shell->children = g_list_append (menu_shell->children, child);
5257 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5259 menu_queue_resize (menu);
5263 gtk_container_child_set (GTK_CONTAINER (parent), child,
5264 "left-attach", left_attach,
5265 "right-attach", right_attach,
5266 "top-attach", top_attach,
5267 "bottom-attach", bottom_attach,
5273 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5277 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5278 "gtk-menu-popup-delay", &popup_delay,
5285 find_child_containing (GtkMenuShell *menu_shell,
5293 /* find a child which includes the area given by
5294 * left, right, top, bottom.
5297 for (list = menu_shell->children; list; list = list->next)
5301 if (!_gtk_menu_item_is_selectable (list->data))
5304 get_effective_child_attach (list->data, &l, &r, &t, &b);
5306 if (l <= left && right <= r
5307 && t <= top && bottom <= b)
5308 return GTK_WIDGET (list->data);
5315 gtk_menu_move_current (GtkMenuShell *menu_shell,
5316 GtkMenuDirectionType direction)
5318 GtkMenu *menu = GTK_MENU (menu_shell);
5321 GtkWidget *match = NULL;
5323 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5327 case GTK_MENU_DIR_CHILD:
5328 direction = GTK_MENU_DIR_PARENT;
5330 case GTK_MENU_DIR_PARENT:
5331 direction = GTK_MENU_DIR_CHILD;
5337 /* use special table menu key bindings */
5338 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5340 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
5342 if (direction == GTK_MENU_DIR_NEXT)
5344 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5346 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5354 for (i = 0; i < t; i++)
5356 match = find_child_containing (menu_shell,
5357 l, l + 1, i, i + 1);
5363 else if (direction == GTK_MENU_DIR_PREV)
5365 for (i = t; i > 0; i--)
5367 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
5375 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5377 match = find_child_containing (menu_shell,
5378 l, l + 1, i - 1, i);
5384 else if (direction == GTK_MENU_DIR_PARENT)
5386 /* we go one left if possible */
5388 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
5392 GtkWidget *parent = menu_shell->parent_menu_shell;
5395 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
5396 match = menu_shell->active_menu_item;
5399 else if (direction == GTK_MENU_DIR_CHILD)
5401 /* we go one right if possible */
5402 if (r < gtk_menu_get_n_columns (menu))
5403 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5407 GtkWidget *parent = menu_shell->parent_menu_shell;
5409 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
5411 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
5412 match = menu_shell->active_menu_item;
5418 gtk_menu_shell_select_item (menu_shell, match);
5423 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5427 get_visible_size (GtkMenu *menu)
5429 GtkAllocation allocation;
5430 GtkWidget *widget = GTK_WIDGET (menu);
5431 GtkContainer *container = GTK_CONTAINER (menu);
5434 gtk_widget_get_allocation (widget, &allocation);
5435 menu_height = (allocation.height
5436 - 2 * (gtk_container_get_border_width (container)
5437 + gtk_widget_get_style (widget)->ythickness));
5439 if (!menu->tearoff_active)
5441 GtkBorder arrow_border;
5443 get_arrows_border (menu, &arrow_border);
5444 menu_height -= arrow_border.top;
5445 menu_height -= arrow_border.bottom;
5451 /* Find the sensitive on-screen child containing @y, or if none,
5452 * the nearest selectable onscreen child. (%NULL if none)
5455 child_at (GtkMenu *menu,
5458 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5459 GtkWidget *child = NULL;
5460 gint child_offset = 0;
5463 gint lower, upper; /* Onscreen bounds */
5465 menu_height = get_visible_size (menu);
5466 lower = menu->scroll_offset;
5467 upper = menu->scroll_offset + menu_height;
5469 for (children = menu_shell->children; children; children = children->next)
5471 if (gtk_widget_get_visible (children->data))
5473 GtkRequisition child_requisition;
5475 gtk_size_request_get_size (GTK_SIZE_REQUEST (children->data),
5476 &child_requisition, NULL);
5478 if (_gtk_menu_item_is_selectable (children->data) &&
5479 child_offset >= lower &&
5480 child_offset + child_requisition.height <= upper)
5482 child = children->data;
5484 if (child_offset + child_requisition.height > y &&
5485 !GTK_IS_TEAROFF_MENU_ITEM (child))
5489 child_offset += child_requisition.height;
5497 get_menu_height (GtkMenu *menu)
5499 GtkAllocation allocation;
5500 GtkWidget *widget = GTK_WIDGET (menu);
5503 gtk_widget_get_allocation (widget, &allocation);
5505 height = allocation.height;
5506 height -= gtk_container_get_border_width (GTK_CONTAINER (widget) + gtk_widget_get_style (widget)->ythickness) * 2;
5508 if (!menu->tearoff_active)
5510 GtkBorder arrow_border;
5512 get_arrows_border (menu, &arrow_border);
5513 height -= arrow_border.top;
5514 height -= arrow_border.bottom;
5521 gtk_menu_real_move_scroll (GtkMenu *menu,
5524 gint page_size = get_visible_size (menu);
5525 gint end_position = get_menu_height (menu);
5526 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5530 case GTK_SCROLL_PAGE_UP:
5531 case GTK_SCROLL_PAGE_DOWN:
5535 gint child_offset = 0;
5536 gboolean old_upper_arrow_visible;
5539 if (type == GTK_SCROLL_PAGE_UP)
5544 if (menu_shell->active_menu_item)
5548 compute_child_offset (menu, menu_shell->active_menu_item,
5549 &child_offset, &child_height, NULL);
5550 child_offset += child_height / 2;
5553 menu_shell->ignore_enter = TRUE;
5554 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5555 old_offset = menu->scroll_offset;
5557 new_offset = menu->scroll_offset + step;
5558 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5560 gtk_menu_scroll_to (menu, new_offset);
5562 if (menu_shell->active_menu_item)
5564 GtkWidget *new_child;
5565 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5566 GtkBorder arrow_border;
5568 get_arrows_border (menu, &arrow_border);
5570 if (menu->scroll_offset != old_offset)
5571 step = menu->scroll_offset - old_offset;
5573 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5575 new_child = child_at (menu, child_offset + step);
5577 gtk_menu_shell_select_item (menu_shell, new_child);
5581 case GTK_SCROLL_START:
5582 /* Ignore the enter event we might get if the pointer is on the menu
5584 menu_shell->ignore_enter = TRUE;
5585 gtk_menu_scroll_to (menu, 0);
5586 gtk_menu_shell_select_first (menu_shell, TRUE);
5588 case GTK_SCROLL_END:
5589 /* Ignore the enter event we might get if the pointer is on the menu
5591 menu_shell->ignore_enter = TRUE;
5592 gtk_menu_scroll_to (menu, end_position - page_size);
5593 _gtk_menu_shell_select_last (menu_shell, TRUE);
5602 * gtk_menu_set_monitor:
5604 * @monitor_num: the number of the monitor on which the menu should
5607 * Informs GTK+ on which monitor a menu should be popped up.
5608 * See gdk_screen_get_monitor_geometry().
5610 * This function should be called from a #GtkMenuPositionFunc if the
5611 * menu should not appear on the same monitor as the pointer. This
5612 * information can't be reliably inferred from the coordinates returned
5613 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
5614 * may extend beyond the monitor boundaries or even the screen boundaries.
5619 gtk_menu_set_monitor (GtkMenu *menu,
5622 GtkMenuPrivate *priv;
5623 g_return_if_fail (GTK_IS_MENU (menu));
5625 priv = gtk_menu_get_private (menu);
5627 priv->monitor_num = monitor_num;
5631 * gtk_menu_get_monitor:
5634 * Retrieves the number of the monitor on which to show the menu.
5636 * Returns: the number of the monitor on which the menu should
5637 * be popped up or -1, if no monitor has been set
5642 gtk_menu_get_monitor (GtkMenu *menu)
5644 GtkMenuPrivate *priv;
5645 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5647 priv = gtk_menu_get_private (menu);
5649 return priv->monitor_num;
5653 * gtk_menu_get_for_attach_widget:
5654 * @widget: a #GtkWidget
5656 * Returns a list of the menus which are attached to this widget.
5657 * This list is owned by GTK+ and must not be modified.
5659 * Return value: (element-type GtkWidget) (transfer none): the list of menus attached to his widget.
5664 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5668 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5670 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5675 gtk_menu_grab_notify (GtkWidget *widget,
5676 gboolean was_grabbed)
5678 GtkWidget *toplevel;
5679 GtkWindowGroup *group;
5683 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5686 !gtk_widget_device_is_shadowed (widget, pointer))
5689 toplevel = gtk_widget_get_toplevel (widget);
5691 if (!GTK_IS_WINDOW (toplevel))
5694 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5695 grab = gtk_window_group_get_current_device_grab (group, pointer);
5697 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
5698 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5702 * gtk_menu_set_reserve_toggle_size:
5704 * @reserve_toggle_size: whether to reserve size for toggles
5706 * Sets whether the menu should reserve space for drawing toggles
5707 * or icons, regardless of their actual presence.
5712 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5713 gboolean reserve_toggle_size)
5715 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5716 gboolean no_toggle_size;
5718 no_toggle_size = !reserve_toggle_size;
5720 if (priv->no_toggle_size != no_toggle_size)
5722 priv->no_toggle_size = no_toggle_size;
5724 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5729 * gtk_menu_get_reserve_toggle_size:
5732 * Returns whether the menu reserves space for toggles and
5733 * icons, regardless of their actual presence.
5735 * Returns: Whether the menu reserves toggle space
5740 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5742 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5744 return !priv->no_toggle_size;