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)
1749 g_return_if_fail (GTK_IS_MENU (menu));
1751 gtk_menu_popup_for_device (menu,
1756 button, activate_time);
1760 gtk_menu_popdown (GtkMenu *menu)
1762 GtkMenuPrivate *private;
1763 GtkMenuShell *menu_shell;
1766 g_return_if_fail (GTK_IS_MENU (menu));
1768 menu_shell = GTK_MENU_SHELL (menu);
1769 private = gtk_menu_get_private (menu);
1771 menu_shell->parent_menu_shell = NULL;
1772 menu_shell->active = FALSE;
1773 menu_shell->ignore_enter = FALSE;
1775 private->have_position = FALSE;
1777 gtk_menu_stop_scrolling (menu);
1779 gtk_menu_stop_navigating_submenu (menu);
1781 if (menu_shell->active_menu_item)
1783 if (menu->old_active_menu_item)
1784 g_object_unref (menu->old_active_menu_item);
1785 menu->old_active_menu_item = menu_shell->active_menu_item;
1786 g_object_ref (menu->old_active_menu_item);
1789 gtk_menu_shell_deselect (menu_shell);
1791 /* The X Grab, if present, will automatically be removed when we hide
1793 gtk_widget_hide (menu->toplevel);
1794 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1796 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1800 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1802 if (gtk_bin_get_child (GTK_BIN (menu->toplevel)))
1804 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1808 /* We popped up the menu from the tearoff, so we need to
1809 * release the grab - we aren't actually hiding the menu.
1811 if (menu_shell->have_xgrab && pointer)
1813 GdkDevice *keyboard;
1815 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1816 keyboard = gdk_device_get_associated_device (pointer);
1819 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1823 /* gtk_menu_popdown is called each time a menu item is selected from
1824 * a torn off menu. Only scroll back to the saved position if the
1825 * non-tearoff menu was popped down.
1827 if (!menu->tearoff_active)
1828 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1829 menu->tearoff_active = TRUE;
1832 gtk_widget_hide (GTK_WIDGET (menu));
1834 menu_shell->have_xgrab = FALSE;
1837 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1839 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1841 menu_grab_transfer_window_destroy (menu);
1845 gtk_menu_get_active (GtkMenu *menu)
1850 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1852 if (!menu->old_active_menu_item)
1855 children = GTK_MENU_SHELL (menu)->children;
1859 child = children->data;
1860 children = children->next;
1862 if (gtk_bin_get_child (GTK_BIN (child)))
1867 menu->old_active_menu_item = child;
1868 if (menu->old_active_menu_item)
1869 g_object_ref (menu->old_active_menu_item);
1872 return menu->old_active_menu_item;
1876 gtk_menu_set_active (GtkMenu *menu,
1882 g_return_if_fail (GTK_IS_MENU (menu));
1884 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1887 child = tmp_list->data;
1888 if (gtk_bin_get_child (GTK_BIN (child)))
1890 if (menu->old_active_menu_item)
1891 g_object_unref (menu->old_active_menu_item);
1892 menu->old_active_menu_item = child;
1893 g_object_ref (menu->old_active_menu_item);
1900 * gtk_menu_set_accel_group:
1901 * @accel_group: (allow-none):
1904 gtk_menu_set_accel_group (GtkMenu *menu,
1905 GtkAccelGroup *accel_group)
1907 g_return_if_fail (GTK_IS_MENU (menu));
1909 if (menu->accel_group != accel_group)
1911 if (menu->accel_group)
1912 g_object_unref (menu->accel_group);
1913 menu->accel_group = accel_group;
1914 if (menu->accel_group)
1915 g_object_ref (menu->accel_group);
1916 _gtk_menu_refresh_accel_paths (menu, TRUE);
1921 gtk_menu_get_accel_group (GtkMenu *menu)
1923 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1925 return menu->accel_group;
1929 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1932 /* Menu items chain here to figure whether they can activate their
1933 * accelerators. Unlike ordinary widgets, menus allow accel
1934 * activation even if invisible since that's the usual case for
1935 * submenus/popup-menus. however, the state of the attach widget
1936 * affects the "activeness" of the menu.
1938 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1941 return gtk_widget_can_activate_accel (awidget, signal_id);
1943 return gtk_widget_is_sensitive (widget);
1947 * gtk_menu_set_accel_path
1948 * @menu: a valid #GtkMenu
1949 * @accel_path: (allow-none): a valid accelerator path
1951 * Sets an accelerator path for this menu from which accelerator paths
1952 * for its immediate children, its menu items, can be constructed.
1953 * The main purpose of this function is to spare the programmer the
1954 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1955 * each menu item that should support runtime user changable accelerators.
1956 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1957 * each menu item of this menu, that contains a label describing its purpose,
1958 * automatically gets an accel path assigned. For example, a menu containing
1959 * menu items "New" and "Exit", will, after
1960 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1961 * has been called, assign its items the accel paths:
1962 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1963 * Assigning accel paths to menu items then enables the user to change
1964 * their accelerators at runtime. More details about accelerator paths
1965 * and their default setups can be found at gtk_accel_map_add_entry().
1967 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1968 * pass a static string, you can save some memory by interning it first with
1969 * g_intern_static_string().
1972 gtk_menu_set_accel_path (GtkMenu *menu,
1973 const gchar *accel_path)
1975 g_return_if_fail (GTK_IS_MENU (menu));
1977 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1979 /* FIXME: accel_path should be defined as const gchar* */
1980 menu->accel_path = (gchar*)g_intern_string (accel_path);
1981 if (menu->accel_path)
1982 _gtk_menu_refresh_accel_paths (menu, FALSE);
1986 * gtk_menu_get_accel_path
1987 * @menu: a valid #GtkMenu
1989 * Retrieves the accelerator path set on the menu.
1991 * Returns: the accelerator path set on the menu.
1996 gtk_menu_get_accel_path (GtkMenu *menu)
1998 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2000 return menu->accel_path;
2005 gboolean group_changed;
2009 refresh_accel_paths_foreach (GtkWidget *widget,
2012 AccelPropagation *prop = data;
2014 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
2015 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2016 prop->menu->accel_path,
2017 prop->menu->accel_group,
2018 prop->group_changed);
2022 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
2023 gboolean group_changed)
2025 g_return_if_fail (GTK_IS_MENU (menu));
2027 if (menu->accel_path && menu->accel_group)
2029 AccelPropagation prop;
2032 prop.group_changed = group_changed;
2033 gtk_container_foreach (GTK_CONTAINER (menu),
2034 refresh_accel_paths_foreach,
2040 gtk_menu_reposition (GtkMenu *menu)
2042 g_return_if_fail (GTK_IS_MENU (menu));
2044 if (!menu->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2045 gtk_menu_position (menu);
2049 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2052 g_return_if_fail (GTK_IS_MENU (menu));
2054 if (adjustment->value != menu->scroll_offset)
2055 gtk_menu_scroll_to (menu, adjustment->value);
2059 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2062 GdkGeometry geometry_hints;
2063 GtkMenuPrivate *priv;
2065 if (!menu->tearoff_window)
2068 priv = gtk_menu_get_private (menu);
2070 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2072 GtkRequisition requisition;
2074 gtk_size_request_get_size (GTK_SIZE_REQUEST (menu->tearoff_scrollbar),
2075 &requisition, NULL);
2076 width += requisition.width;
2079 geometry_hints.min_width = width;
2080 geometry_hints.max_width = width;
2082 geometry_hints.min_height = 0;
2083 geometry_hints.max_height = priv->requested_height;
2085 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
2088 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2092 gtk_menu_update_title (GtkMenu *menu)
2094 if (menu->tearoff_window)
2097 GtkWidget *attach_widget;
2099 title = gtk_menu_get_title (menu);
2102 attach_widget = gtk_menu_get_attach_widget (menu);
2103 if (GTK_IS_MENU_ITEM (attach_widget))
2105 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2106 if (GTK_IS_LABEL (child))
2107 title = gtk_label_get_text (GTK_LABEL (child));
2112 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
2117 gtk_menu_get_toplevel (GtkWidget *menu)
2119 GtkWidget *attach, *toplevel;
2121 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2123 if (GTK_IS_MENU_ITEM (attach))
2124 attach = gtk_widget_get_parent (attach);
2126 if (GTK_IS_MENU (attach))
2127 return gtk_menu_get_toplevel (attach);
2128 else if (GTK_IS_WIDGET (attach))
2130 toplevel = gtk_widget_get_toplevel (attach);
2131 if (gtk_widget_is_toplevel (toplevel))
2139 tearoff_window_destroyed (GtkWidget *widget,
2142 gtk_menu_set_tearoff_state (menu, FALSE);
2146 gtk_menu_set_tearoff_state (GtkMenu *menu,
2150 GtkMenuPrivate *priv;
2152 g_return_if_fail (GTK_IS_MENU (menu));
2154 priv = gtk_menu_get_private (menu);
2156 if (menu->torn_off != torn_off)
2158 menu->torn_off = torn_off;
2159 menu->tearoff_active = torn_off;
2163 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2164 gtk_menu_popdown (menu);
2166 if (!menu->tearoff_window)
2168 GtkWidget *toplevel;
2170 menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2171 "type", GTK_WINDOW_TOPLEVEL,
2172 "screen", gtk_widget_get_screen (menu->toplevel),
2173 "app-paintable", TRUE,
2176 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
2177 GDK_WINDOW_TYPE_HINT_MENU);
2178 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
2179 g_signal_connect (menu->tearoff_window, "destroy",
2180 G_CALLBACK (tearoff_window_destroyed), menu);
2181 g_signal_connect (menu->tearoff_window, "event",
2182 G_CALLBACK (gtk_menu_window_event), menu);
2184 gtk_menu_update_title (menu);
2186 gtk_widget_realize (menu->tearoff_window);
2188 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2189 if (toplevel != NULL)
2190 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
2191 GTK_WINDOW (toplevel));
2193 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
2194 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
2196 gdk_drawable_get_size (gtk_widget_get_window (GTK_WIDGET (menu)),
2198 menu->tearoff_adjustment =
2199 GTK_ADJUSTMENT (gtk_adjustment_new (0,
2201 priv->requested_height,
2205 g_object_connect (menu->tearoff_adjustment,
2206 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2208 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
2210 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
2211 menu->tearoff_scrollbar,
2214 if (menu->tearoff_adjustment->upper > height)
2215 gtk_widget_show (menu->tearoff_scrollbar);
2217 gtk_widget_show (menu->tearoff_hbox);
2220 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
2222 gdk_drawable_get_size (gtk_widget_get_window (GTK_WIDGET (menu)),
2225 /* Update menu->requisition
2227 gtk_size_request_get_size (GTK_SIZE_REQUEST (menu),
2230 gtk_menu_set_tearoff_hints (menu, width);
2232 gtk_widget_realize (menu->tearoff_window);
2233 gtk_menu_position (menu);
2235 gtk_widget_show (GTK_WIDGET (menu));
2236 gtk_widget_show (menu->tearoff_window);
2238 gtk_menu_scroll_to (menu, 0);
2243 gtk_widget_hide (GTK_WIDGET (menu));
2244 gtk_widget_hide (menu->tearoff_window);
2245 if (GTK_IS_CONTAINER (menu->toplevel))
2246 gtk_menu_reparent (menu, menu->toplevel, FALSE);
2247 gtk_widget_destroy (menu->tearoff_window);
2249 menu->tearoff_window = NULL;
2250 menu->tearoff_hbox = NULL;
2251 menu->tearoff_scrollbar = NULL;
2252 menu->tearoff_adjustment = NULL;
2255 g_object_notify (G_OBJECT (menu), "tearoff-state");
2260 * gtk_menu_get_tearoff_state:
2263 * Returns whether the menu is torn off. See
2264 * gtk_menu_set_tearoff_state ().
2266 * Return value: %TRUE if the menu is currently torn off.
2269 gtk_menu_get_tearoff_state (GtkMenu *menu)
2271 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2273 return menu->torn_off;
2277 * gtk_menu_set_title:
2279 * @title: a string containing the title for the menu.
2281 * Sets the title string for the menu. The title is displayed when the menu
2282 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
2283 * attached to a parent menu item, and if so it will try to use the same text as
2284 * that menu item's label.
2287 gtk_menu_set_title (GtkMenu *menu,
2290 GtkMenuPrivate *priv;
2293 g_return_if_fail (GTK_IS_MENU (menu));
2295 priv = gtk_menu_get_private (menu);
2297 old_title = priv->title;
2298 priv->title = g_strdup (title);
2301 gtk_menu_update_title (menu);
2302 g_object_notify (G_OBJECT (menu), "tearoff-title");
2306 * gtk_menu_get_title:
2309 * Returns the title of the menu. See gtk_menu_set_title().
2311 * Return value: the title of the menu, or %NULL if the menu has no
2312 * title set on it. This string is owned by the widget and should
2313 * not be modified or freed.
2315 G_CONST_RETURN gchar *
2316 gtk_menu_get_title (GtkMenu *menu)
2318 GtkMenuPrivate *priv;
2320 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2322 priv = gtk_menu_get_private (menu);
2328 gtk_menu_reorder_child (GtkMenu *menu,
2332 GtkMenuShell *menu_shell;
2334 g_return_if_fail (GTK_IS_MENU (menu));
2335 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2337 menu_shell = GTK_MENU_SHELL (menu);
2339 if (g_list_find (menu_shell->children, child))
2341 menu_shell->children = g_list_remove (menu_shell->children, child);
2342 menu_shell->children = g_list_insert (menu_shell->children, child, position);
2344 menu_queue_resize (menu);
2349 gtk_menu_style_set (GtkWidget *widget,
2350 GtkStyle *previous_style)
2352 if (gtk_widget_get_realized (widget))
2354 GtkMenu *menu = GTK_MENU (widget);
2357 style = gtk_widget_get_style (widget);
2359 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2360 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2361 gtk_style_set_background (style, gtk_widget_get_window (widget), GTK_STATE_NORMAL);
2366 get_arrows_border (GtkMenu *menu,
2369 guint scroll_arrow_height;
2370 GtkArrowPlacement arrow_placement;
2372 gtk_widget_style_get (GTK_WIDGET (menu),
2373 "scroll-arrow-vlength", &scroll_arrow_height,
2374 "arrow_placement", &arrow_placement,
2377 switch (arrow_placement)
2379 case GTK_ARROWS_BOTH:
2380 border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2381 border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2384 case GTK_ARROWS_START:
2385 border->top = (menu->upper_arrow_visible ||
2386 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2390 case GTK_ARROWS_END:
2392 border->bottom = (menu->upper_arrow_visible ||
2393 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2397 border->left = border->right = 0;
2401 gtk_menu_realize (GtkWidget *widget)
2403 GtkAllocation allocation;
2406 GdkWindowAttr attributes;
2407 gint attributes_mask;
2410 GtkMenuPrivate *priv;
2413 guint vertical_padding;
2414 guint horizontal_padding;
2415 GtkBorder arrow_border;
2417 g_return_if_fail (GTK_IS_MENU (widget));
2419 menu = GTK_MENU (widget);
2420 priv = gtk_menu_get_private (menu);
2422 gtk_widget_set_realized (widget, TRUE);
2424 gtk_widget_get_allocation (widget, &allocation);
2426 attributes.window_type = GDK_WINDOW_CHILD;
2427 attributes.x = allocation.x;
2428 attributes.y = allocation.y;
2429 attributes.width = allocation.width;
2430 attributes.height = allocation.height;
2431 attributes.wclass = GDK_INPUT_OUTPUT;
2432 attributes.visual = gtk_widget_get_visual (widget);
2433 attributes.colormap = gtk_widget_get_colormap (widget);
2434 attributes.event_mask = gtk_widget_get_events (widget);
2435 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2436 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2438 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2440 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2441 &attributes, attributes_mask);
2442 gtk_widget_set_window (widget, window);
2443 gdk_window_set_user_data (window, widget);
2445 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2447 style = gtk_widget_get_style (widget);
2448 gtk_widget_style_get (GTK_WIDGET (menu),
2449 "vertical-padding", &vertical_padding,
2450 "horizontal-padding", &horizontal_padding,
2453 gtk_widget_get_allocation (widget, &allocation);
2455 attributes.x = border_width + style->xthickness + horizontal_padding;
2456 attributes.y = border_width + style->ythickness + vertical_padding;
2457 attributes.width = MAX (1, allocation.width - attributes.x * 2);
2458 attributes.height = MAX (1, allocation.height - attributes.y * 2);
2460 get_arrows_border (menu, &arrow_border);
2461 attributes.y += arrow_border.top;
2462 attributes.height -= arrow_border.top;
2463 attributes.height -= arrow_border.bottom;
2465 menu->view_window = gdk_window_new (window,
2466 &attributes, attributes_mask);
2467 gdk_window_set_user_data (menu->view_window, menu);
2469 gtk_widget_get_allocation (widget, &allocation);
2473 attributes.width = MAX (1, allocation.width - (border_width + style->xthickness + horizontal_padding) * 2);
2474 attributes.height = MAX (1, priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2);
2476 menu->bin_window = gdk_window_new (menu->view_window,
2477 &attributes, attributes_mask);
2478 gdk_window_set_user_data (menu->bin_window, menu);
2480 children = GTK_MENU_SHELL (menu)->children;
2483 child = children->data;
2484 children = children->next;
2486 gtk_widget_set_parent_window (child, menu->bin_window);
2489 gtk_widget_style_attach (widget);
2490 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2491 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2492 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
2494 if (GTK_MENU_SHELL (widget)->active_menu_item)
2495 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2496 GTK_MENU_SHELL (widget)->active_menu_item);
2498 gdk_window_show (menu->bin_window);
2499 gdk_window_show (menu->view_window);
2503 gtk_menu_focus (GtkWidget *widget,
2504 GtkDirectionType direction)
2507 * A menu or its menu items cannot have focus
2512 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2515 menu_grab_transfer_window_get (GtkMenu *menu)
2517 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2520 GdkWindowAttr attributes;
2521 gint attributes_mask;
2523 attributes.x = -100;
2524 attributes.y = -100;
2525 attributes.width = 10;
2526 attributes.height = 10;
2527 attributes.window_type = GDK_WINDOW_TEMP;
2528 attributes.wclass = GDK_INPUT_ONLY;
2529 attributes.override_redirect = TRUE;
2530 attributes.event_mask = 0;
2532 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2534 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2535 &attributes, attributes_mask);
2536 gdk_window_set_user_data (window, menu);
2538 gdk_window_show (window);
2540 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2547 menu_grab_transfer_window_destroy (GtkMenu *menu)
2549 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2552 gdk_window_set_user_data (window, NULL);
2553 gdk_window_destroy (window);
2554 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2559 gtk_menu_unrealize (GtkWidget *widget)
2561 GtkMenu *menu = GTK_MENU (widget);
2563 menu_grab_transfer_window_destroy (menu);
2565 gdk_window_set_user_data (menu->view_window, NULL);
2566 gdk_window_destroy (menu->view_window);
2567 menu->view_window = NULL;
2569 gdk_window_set_user_data (menu->bin_window, NULL);
2570 gdk_window_destroy (menu->bin_window);
2571 menu->bin_window = NULL;
2573 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2578 calculate_line_heights (GtkMenu *menu,
2580 guint **ret_min_heights,
2581 guint **ret_nat_heights)
2583 GtkMenuShell *menu_shell;
2584 GtkMenuPrivate *priv;
2585 GtkWidget *child, *widget;
2587 guint horizontal_padding;
2595 widget = GTK_WIDGET (menu);
2596 menu_shell = GTK_MENU_SHELL (widget);
2597 priv = gtk_menu_get_private (menu);
2599 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2600 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2601 n_heights = gtk_menu_get_n_rows (menu);
2602 n_columns = gtk_menu_get_n_columns (menu);
2603 avail_width = for_width - (2 * menu->toggle_size + priv->accel_size) * n_columns;
2605 gtk_widget_style_get (GTK_WIDGET (menu),
2606 "horizontal-padding", &horizontal_padding,
2609 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2610 avail_width -= (border_width + horizontal_padding + gtk_widget_get_style (widget)->xthickness) * 2;
2612 for (children = menu_shell->children; children; children = children->next)
2617 gint child_min, child_nat;
2619 child = children->data;
2621 if (! gtk_widget_get_visible (child))
2624 get_effective_child_attach (child, &l, &r, &t, &b);
2626 part = avail_width / (r - l);
2628 gtk_size_request_get_height_for_width (GTK_SIZE_REQUEST (child), part,
2629 &child_min, &child_nat);
2631 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2633 part = MAX (child_min, toggle_size) / (b - t);
2634 min_heights[t] = MAX (min_heights[t], part);
2636 part = MAX (child_nat, toggle_size) / (b - t);
2637 nat_heights[t] = MAX (nat_heights[t], part);
2640 if (ret_min_heights)
2641 *ret_min_heights = min_heights;
2643 g_free (min_heights);
2645 if (ret_nat_heights)
2646 *ret_nat_heights = nat_heights;
2648 g_free (nat_heights);
2654 gtk_menu_size_allocate (GtkWidget *widget,
2655 GtkAllocation *allocation)
2658 GtkMenuShell *menu_shell;
2660 GtkAllocation child_allocation;
2661 GtkMenuPrivate *priv;
2667 guint vertical_padding;
2668 guint horizontal_padding;
2670 g_return_if_fail (GTK_IS_MENU (widget));
2671 g_return_if_fail (allocation != NULL);
2673 menu = GTK_MENU (widget);
2674 menu_shell = GTK_MENU_SHELL (widget);
2675 priv = gtk_menu_get_private (menu);
2677 gtk_widget_set_allocation (widget, allocation);
2679 style = gtk_widget_get_style (widget);
2681 gtk_widget_style_get (GTK_WIDGET (menu),
2682 "vertical-padding", &vertical_padding,
2683 "horizontal-padding", &horizontal_padding,
2685 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2687 g_free (priv->heights);
2688 priv->heights_length =
2689 calculate_line_heights (menu,
2694 /* refresh our cached height request */
2695 priv->requested_height = (border_width + vertical_padding + style->ythickness) * 2;
2696 for (i = 0; i < priv->heights_length; i++)
2697 priv->requested_height += priv->heights[i];
2699 x = border_width + style->xthickness + horizontal_padding;
2700 y = border_width + style->ythickness + vertical_padding;
2702 width = MAX (1, allocation->width - x * 2);
2703 height = MAX (1, allocation->height - y * 2);
2705 if (menu_shell->active)
2706 gtk_menu_scroll_to (menu, menu->scroll_offset);
2708 if (!menu->tearoff_active)
2710 GtkBorder arrow_border;
2712 get_arrows_border (menu, &arrow_border);
2713 y += arrow_border.top;
2714 height -= arrow_border.top;
2715 height -= arrow_border.bottom;
2718 if (gtk_widget_get_realized (widget))
2720 gdk_window_move_resize (gtk_widget_get_window (widget),
2721 allocation->x, allocation->y,
2722 allocation->width, allocation->height);
2724 gdk_window_move_resize (menu->view_window,
2731 if (menu_shell->children)
2733 gint base_width = width / gtk_menu_get_n_columns (menu);
2735 children = menu_shell->children;
2738 child = children->data;
2739 children = children->next;
2741 if (gtk_widget_get_visible (child))
2746 get_effective_child_attach (child, &l, &r, &t, &b);
2748 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2751 tmp = gtk_menu_get_n_columns (menu) - l;
2752 l = gtk_menu_get_n_columns (menu) - r;
2756 child_allocation.width = (r - l) * base_width;
2757 child_allocation.height = 0;
2758 child_allocation.x = l * base_width;
2759 child_allocation.y = 0;
2761 for (i = 0; i < b; i++)
2764 child_allocation.y += priv->heights[i];
2766 child_allocation.height += priv->heights[i];
2769 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2772 gtk_widget_size_allocate (child, &child_allocation);
2773 gtk_widget_queue_draw (child);
2777 /* Resize the item window */
2778 if (gtk_widget_get_realized (widget))
2784 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2785 height += priv->heights[i];
2787 width = gtk_menu_get_n_columns (menu) * base_width;
2788 gdk_window_resize (menu->bin_window, width, height);
2791 if (menu->tearoff_active)
2793 if (height >= priv->requested_height)
2795 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2797 gtk_widget_hide (menu->tearoff_scrollbar);
2798 gtk_menu_set_tearoff_hints (menu, allocation->width);
2800 gtk_menu_scroll_to (menu, 0);
2805 menu->tearoff_adjustment->upper = priv->requested_height;
2806 menu->tearoff_adjustment->page_size = allocation->height;
2808 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2809 menu->tearoff_adjustment->upper)
2812 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2815 gtk_menu_scroll_to (menu, value);
2818 gtk_adjustment_changed (menu->tearoff_adjustment);
2820 if (!gtk_widget_get_visible (menu->tearoff_scrollbar))
2822 gtk_widget_show (menu->tearoff_scrollbar);
2823 gtk_menu_set_tearoff_hints (menu, allocation->width);
2831 get_arrows_visible_area (GtkMenu *menu,
2832 GdkRectangle *border,
2833 GdkRectangle *upper,
2834 GdkRectangle *lower,
2837 GtkArrowPlacement arrow_placement;
2839 GtkWidget *widget = GTK_WIDGET (menu);
2841 guint vertical_padding;
2842 guint horizontal_padding;
2843 gint scroll_arrow_height;
2845 style = gtk_widget_get_style (widget);
2847 gtk_widget_style_get (widget,
2848 "vertical-padding", &vertical_padding,
2849 "horizontal-padding", &horizontal_padding,
2850 "scroll-arrow-vlength", &scroll_arrow_height,
2851 "arrow-placement", &arrow_placement,
2854 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2855 border->x = border_width + style->xthickness + horizontal_padding;
2856 border->y = border_width + style->ythickness + vertical_padding;
2857 gdk_drawable_get_size (gtk_widget_get_window (widget),
2858 &border->width, &border->height);
2860 switch (arrow_placement)
2862 case GTK_ARROWS_BOTH:
2863 upper->x = border->x;
2864 upper->y = border->y;
2865 upper->width = border->width - 2 * border->x;
2866 upper->height = scroll_arrow_height;
2868 lower->x = border->x;
2869 lower->y = border->height - border->y - scroll_arrow_height;
2870 lower->width = border->width - 2 * border->x;
2871 lower->height = scroll_arrow_height;
2874 case GTK_ARROWS_START:
2875 upper->x = border->x;
2876 upper->y = border->y;
2877 upper->width = (border->width - 2 * border->x) / 2;
2878 upper->height = scroll_arrow_height;
2880 lower->x = border->x + upper->width;
2881 lower->y = border->y;
2882 lower->width = (border->width - 2 * border->x) / 2;
2883 lower->height = scroll_arrow_height;
2886 case GTK_ARROWS_END:
2887 upper->x = border->x;
2888 upper->y = border->height - border->y - scroll_arrow_height;
2889 upper->width = (border->width - 2 * border->x) / 2;
2890 upper->height = scroll_arrow_height;
2892 lower->x = border->x + upper->width;
2893 lower->y = border->height - border->y - scroll_arrow_height;
2894 lower->width = (border->width - 2 * border->x) / 2;
2895 lower->height = scroll_arrow_height;
2899 g_assert_not_reached();
2900 upper->x = upper->y = upper->width = upper->height = 0;
2901 lower->x = lower->y = lower->width = lower->height = 0;
2904 *arrow_space = scroll_arrow_height - 2 * style->ythickness;
2908 gtk_menu_paint (GtkWidget *widget,
2909 GdkEventExpose *event)
2912 GtkMenuPrivate *priv;
2914 GdkRectangle border;
2920 g_return_if_fail (GTK_IS_MENU (widget));
2922 menu = GTK_MENU (widget);
2923 priv = gtk_menu_get_private (menu);
2925 style = gtk_widget_get_style (widget);
2926 window = gtk_widget_get_window (widget);
2928 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2930 if (event->window == window)
2932 gfloat arrow_scaling;
2935 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
2936 arrow_size = arrow_scaling * arrow_space;
2938 gtk_paint_box (style,
2942 &event->area, widget, "menu",
2945 if (menu->upper_arrow_visible && !menu->tearoff_active)
2947 gtk_paint_box (style,
2949 priv->upper_arrow_state,
2951 &event->area, widget, "menu_scroll_arrow_up",
2957 gtk_paint_arrow (style,
2959 priv->upper_arrow_state,
2961 &event->area, widget, "menu_scroll_arrow_up",
2964 upper.x + (upper.width - arrow_size) / 2,
2965 upper.y + style->ythickness + (arrow_space - arrow_size) / 2,
2966 arrow_size, arrow_size);
2969 if (menu->lower_arrow_visible && !menu->tearoff_active)
2971 gtk_paint_box (style,
2973 priv->lower_arrow_state,
2975 &event->area, widget, "menu_scroll_arrow_down",
2981 gtk_paint_arrow (style,
2983 priv->lower_arrow_state,
2985 &event->area, widget, "menu_scroll_arrow_down",
2988 lower.x + (lower.width - arrow_size) / 2,
2989 lower.y + style->ythickness + (arrow_space - arrow_size) / 2,
2990 arrow_size, arrow_size);
2993 else if (event->window == menu->bin_window)
2995 gint y = -border.y + menu->scroll_offset;
2997 if (!menu->tearoff_active)
2999 GtkBorder arrow_border;
3001 get_arrows_border (menu, &arrow_border);
3002 y -= arrow_border.top;
3005 gtk_paint_box (style,
3009 &event->area, widget, "menu",
3011 border.width, border.height);
3016 gtk_menu_expose (GtkWidget *widget,
3017 GdkEventExpose *event)
3019 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3020 g_return_val_if_fail (event != NULL, FALSE);
3022 if (gtk_widget_is_drawable (widget))
3024 gtk_menu_paint (widget, event);
3026 GTK_WIDGET_CLASS (gtk_menu_parent_class)->expose_event (widget, event);
3033 gtk_menu_show (GtkWidget *widget)
3035 GtkMenu *menu = GTK_MENU (widget);
3037 _gtk_menu_refresh_accel_paths (menu, FALSE);
3039 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3045 gtk_menu_size_request_init (GtkSizeRequestIface *iface)
3047 iface->get_width = gtk_menu_get_width;
3048 iface->get_height = gtk_menu_get_height;
3049 iface->get_height_for_width = gtk_menu_get_height_for_width;
3053 gtk_menu_get_width (GtkSizeRequest *widget,
3058 GtkMenuShell *menu_shell;
3059 GtkMenuPrivate *priv;
3063 guint max_toggle_size;
3064 guint max_accel_width;
3065 guint horizontal_padding;
3067 gint child_min, child_nat;
3068 gint min_width, nat_width;
3070 menu = GTK_MENU (widget);
3071 menu_shell = GTK_MENU_SHELL (widget);
3072 priv = gtk_menu_get_private (menu);
3074 style = gtk_widget_get_style (GTK_WIDGET (widget));
3076 min_width = nat_width = 0;
3078 max_toggle_size = 0;
3079 max_accel_width = 0;
3081 children = menu_shell->children;
3088 child = children->data;
3089 children = children->next;
3091 if (! gtk_widget_get_visible (child))
3094 get_effective_child_attach (child, &l, &r, &t, &b);
3096 /* It's important to size_request the child
3097 * before doing the toggle size request, in
3098 * case the toggle size request depends on the size
3099 * request of a child of the child (e.g. for ImageMenuItem)
3102 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
3103 gtk_size_request_get_width (GTK_SIZE_REQUEST (child), &child_min, &child_nat);
3105 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3106 max_toggle_size = MAX (max_toggle_size, toggle_size);
3107 max_accel_width = MAX (max_accel_width,
3108 GTK_MENU_ITEM (child)->accelerator_width);
3110 part = child_min / (r - l);
3111 min_width = MAX (min_width, part);
3113 part = child_nat / (r - l);
3114 nat_width = MAX (nat_width, part);
3117 /* If the menu doesn't include any images or check items
3118 * reserve the space so that all menus are consistent.
3119 * We only do this for 'ordinary' menus, not for combobox
3120 * menus or multi-column menus
3122 if (max_toggle_size == 0 &&
3123 gtk_menu_get_n_columns (menu) == 1 &&
3124 !priv->no_toggle_size)
3126 guint toggle_spacing;
3127 guint indicator_size;
3129 gtk_style_get (style,
3130 GTK_TYPE_CHECK_MENU_ITEM,
3131 "toggle-spacing", &toggle_spacing,
3132 "indicator-size", &indicator_size,
3135 max_toggle_size = indicator_size + toggle_spacing;
3138 min_width += 2 * max_toggle_size + max_accel_width;
3139 min_width *= gtk_menu_get_n_columns (menu);
3141 nat_width += 2 * max_toggle_size + max_accel_width;
3142 nat_width *= gtk_menu_get_n_columns (menu);
3144 gtk_widget_style_get (GTK_WIDGET (menu),
3145 "horizontal-padding", &horizontal_padding,
3148 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3149 min_width += (border_width + horizontal_padding + style->xthickness) * 2;
3150 nat_width += (border_width + horizontal_padding + style->xthickness) * 2;
3152 menu->toggle_size = max_toggle_size;
3153 priv->accel_size = max_accel_width;
3156 *minimum_size = min_width;
3159 *natural_size = nat_width;
3161 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
3163 if (menu->tearoff_active)
3164 gtk_menu_set_tearoff_hints (menu, min_width);
3168 gtk_menu_get_height (GtkSizeRequest *widget,
3174 /* Menus are height-for-width only, just return the height for the minimum width */
3175 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_width (widget, &min_width, NULL);
3176 GTK_SIZE_REQUEST_GET_IFACE (widget)->get_height_for_width (widget, min_width, minimum_size, natural_size);
3180 gtk_menu_get_height_for_width (GtkSizeRequest *widget,
3185 GtkMenu *menu = GTK_MENU (widget);
3186 GtkMenuPrivate *private = gtk_menu_get_private (menu);
3187 guint *min_heights, *nat_heights;
3188 guint vertical_padding, border_width;
3190 gint min_height, nat_height;
3192 gtk_widget_style_get (GTK_WIDGET (menu), "vertical-padding", &vertical_padding, NULL);
3193 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3195 min_height = nat_height = (border_width + vertical_padding + gtk_widget_get_style (GTK_WIDGET (widget))->ythickness) * 2;
3198 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3200 for (i = 0; i < n_heights; i++)
3202 min_height += min_heights[i];
3203 nat_height += nat_heights[i];
3206 if (private->have_position)
3208 GdkScreen *screen = gtk_widget_get_screen (menu->toplevel);
3209 GdkRectangle monitor;
3211 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3213 if (private->y + min_height > monitor.y + monitor.height)
3214 min_height = monitor.y + monitor.height - private->y;
3216 if (private->y + nat_height > monitor.y + monitor.height)
3217 nat_height = monitor.y + monitor.height - private->y;
3219 if (private->y < monitor.y)
3221 min_height -= monitor.y - private->y;
3222 nat_height -= monitor.y - private->y;
3227 *minimum_size = min_height;
3230 *natural_size = nat_height;
3232 g_free (min_heights);
3233 g_free (nat_heights);
3239 gtk_menu_button_scroll (GtkMenu *menu,
3240 GdkEventButton *event)
3242 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
3244 gboolean touchscreen_mode;
3246 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3247 "gtk-touchscreen-mode", &touchscreen_mode,
3250 if (touchscreen_mode)
3251 gtk_menu_handle_scrolling (menu,
3252 event->x_root, event->y_root,
3253 event->type == GDK_BUTTON_PRESS,
3263 pointer_in_menu_window (GtkWidget *widget,
3267 GtkAllocation allocation;
3268 GtkMenu *menu = GTK_MENU (widget);
3270 if (gtk_widget_get_mapped (menu->toplevel))
3272 GtkMenuShell *menu_shell;
3273 gint window_x, window_y;
3275 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3276 &window_x, &window_y);
3278 gtk_widget_get_allocation (widget, &allocation);
3279 if (x_root >= window_x && x_root < window_x + allocation.width &&
3280 y_root >= window_y && y_root < window_y + allocation.height)
3283 menu_shell = GTK_MENU_SHELL (widget);
3285 if (GTK_IS_MENU (menu_shell->parent_menu_shell))
3286 return pointer_in_menu_window (menu_shell->parent_menu_shell,
3294 gtk_menu_button_press (GtkWidget *widget,
3295 GdkEventButton *event)
3297 if (event->type != GDK_BUTTON_PRESS)
3300 /* Don't pass down to menu shell for presses over scroll arrows
3302 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3305 /* Don't pass down to menu shell if a non-menuitem part of the menu
3306 * was clicked. The check for the event_widget being a GtkMenuShell
3307 * works because we have the pointer grabbed on menu_shell->window
3308 * with owner_events=TRUE, so all events that are either outside
3309 * the menu or on its border are delivered relative to
3310 * menu_shell->window.
3312 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3313 pointer_in_menu_window (widget, event->x_root, event->y_root))
3316 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3320 gtk_menu_button_release (GtkWidget *widget,
3321 GdkEventButton *event)
3323 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3325 if (priv->ignore_button_release)
3327 priv->ignore_button_release = FALSE;
3331 if (event->type != GDK_BUTTON_RELEASE)
3334 /* Don't pass down to menu shell for releases over scroll arrows
3336 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3339 /* Don't pass down to menu shell if a non-menuitem part of the menu
3340 * was clicked (see comment in button_press()).
3342 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3343 pointer_in_menu_window (widget, event->x_root, event->y_root))
3345 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3346 * bail out early here so it is in a consistent state for the
3347 * next button_press/button_release in GtkMenuShell.
3350 if (GTK_MENU_SHELL (widget)->active)
3351 GTK_MENU_SHELL (widget)->button = 0;
3356 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3359 static const gchar *
3360 get_accel_path (GtkWidget *menu_item,
3365 GClosure *accel_closure;
3366 GtkAccelGroup *accel_group;
3368 path = _gtk_widget_get_accel_path (menu_item, locked);
3371 path = GTK_MENU_ITEM (menu_item)->accel_path;
3377 label = gtk_bin_get_child (GTK_BIN (menu_item));
3379 if (GTK_IS_ACCEL_LABEL (label))
3381 g_object_get (label,
3382 "accel-closure", &accel_closure,
3386 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3388 *locked = gtk_accel_group_get_is_locked (accel_group);
3398 gtk_menu_key_press (GtkWidget *widget,
3401 GtkMenuShell *menu_shell;
3403 gboolean delete = FALSE;
3404 gboolean can_change_accels;
3405 gchar *accel = NULL;
3406 guint accel_key, accel_mods;
3407 GdkModifierType consumed_modifiers;
3408 GdkDisplay *display;
3410 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3411 g_return_val_if_fail (event != NULL, FALSE);
3413 menu_shell = GTK_MENU_SHELL (widget);
3414 menu = GTK_MENU (widget);
3416 gtk_menu_stop_navigating_submenu (menu);
3418 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3421 display = gtk_widget_get_display (widget);
3423 g_object_get (gtk_widget_get_settings (widget),
3424 "gtk-menu-bar-accel", &accel,
3425 "gtk-can-change-accels", &can_change_accels,
3428 if (accel && *accel)
3431 GdkModifierType mods = 0;
3433 gtk_accelerator_parse (accel, &keyval, &mods);
3436 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3438 /* FIXME this is wrong, needs to be in the global accel resolution
3439 * thing, to properly consider i18n etc., but that probably requires
3440 * AccelGroup changes etc.
3442 if (event->keyval == keyval && (mods & event->state) == mods)
3444 gtk_menu_shell_cancel (menu_shell);
3452 switch (event->keyval)
3454 case GDK_KEY_Delete:
3455 case GDK_KEY_KP_Delete:
3456 case GDK_KEY_BackSpace:
3463 /* Figure out what modifiers went into determining the key symbol */
3464 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3465 event->hardware_keycode, event->state, event->group,
3466 NULL, NULL, NULL, &consumed_modifiers);
3468 accel_key = gdk_keyval_to_lower (event->keyval);
3469 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3471 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
3472 * We re-upper case when we match against the keyval, but display and save in caseless form.
3474 if (accel_key != event->keyval)
3475 accel_mods |= GDK_SHIFT_MASK;
3477 /* Modify the accelerators */
3478 if (can_change_accels &&
3479 menu_shell->active_menu_item &&
3480 gtk_bin_get_child (GTK_BIN (menu_shell->active_menu_item)) && /* no separators */
3481 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
3482 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3484 GtkWidget *menu_item = menu_shell->active_menu_item;
3485 gboolean locked, replace_accels = TRUE;
3488 path = get_accel_path (menu_item, &locked);
3489 if (!path || locked)
3491 /* can't change accelerators on menu_items without paths
3492 * (basically, those items are accelerator-locked).
3494 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
3495 gtk_widget_error_bell (widget);
3501 /* For the keys that act to delete the current setting, we delete
3502 * the current setting if there is one, otherwise, we set the
3503 * key as the accelerator.
3509 if (gtk_accel_map_lookup_entry (path, &key) &&
3510 (key.accel_key || key.accel_mods))
3516 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3520 /* we failed, probably because this key is in use and
3523 /* g_print("failed to change\n"); */
3524 gtk_widget_error_bell (widget);
3533 check_threshold (GtkWidget *widget,
3542 ABS (start_x - x) > THRESHOLD ||
3543 ABS (start_y - y) > THRESHOLD;
3547 definitely_within_item (GtkWidget *widget,
3551 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
3554 gdk_drawable_get_size (window, &w, &h);
3557 check_threshold (widget, 0, 0, x, y) &&
3558 check_threshold (widget, w - 1, 0, x, y) &&
3559 check_threshold (widget, w - 1, h - 1, x, y) &&
3560 check_threshold (widget, 0, h - 1, x, y);
3564 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3566 GtkMenuPrivate *priv;
3568 priv = gtk_menu_get_private (menu);
3570 return priv->navigation_height && priv->navigation_width;
3574 gtk_menu_motion_notify (GtkWidget *widget,
3575 GdkEventMotion *event)
3577 GtkWidget *menu_item;
3579 GtkMenuShell *menu_shell;
3582 gboolean need_enter;
3584 if (GTK_IS_MENU (widget))
3586 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3588 if (priv->ignore_button_release)
3589 priv->ignore_button_release = FALSE;
3591 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3595 /* We received the event for one of two reasons:
3597 * a) We are the active menu, and did gtk_grab_add()
3598 * b) The widget is a child of ours, and the event was propagated
3600 * Since for computation of navigation regions, we want the menu which
3601 * is the parent of the menu item, for a), we need to find that menu,
3602 * which may be different from 'widget'.
3604 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3605 parent = gtk_widget_get_parent (menu_item);
3606 if (!GTK_IS_MENU_ITEM (menu_item) ||
3607 !GTK_IS_MENU (parent))
3610 menu_shell = GTK_MENU_SHELL (parent);
3611 menu = GTK_MENU (menu_shell);
3613 if (definitely_within_item (menu_item, event->x, event->y))
3614 menu_shell->activate_time = 0;
3616 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->ignore_enter);
3618 /* Check to see if we are within an active submenu's navigation region
3620 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3623 /* Make sure we pop down if we enter a non-selectable menu item, so we
3624 * don't show a submenu when the cursor is outside the stay-up triangle.
3626 if (!_gtk_menu_item_is_selectable (menu_item))
3628 /* We really want to deselect, but this gives the menushell code
3629 * a chance to do some bookkeeping about the menuitem.
3631 gtk_menu_shell_select_item (menu_shell, menu_item);
3637 /* The menu is now sensitive to enter events on its items, but
3638 * was previously sensitive. So we fake an enter event.
3642 menu_shell->ignore_enter = FALSE;
3644 gdk_drawable_get_size (event->window, &width, &height);
3645 if (event->x >= 0 && event->x < width &&
3646 event->y >= 0 && event->y < height)
3648 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3651 send_event->crossing.window = g_object_ref (event->window);
3652 send_event->crossing.time = event->time;
3653 send_event->crossing.send_event = TRUE;
3654 send_event->crossing.x_root = event->x_root;
3655 send_event->crossing.y_root = event->y_root;
3656 send_event->crossing.x = event->x;
3657 send_event->crossing.y = event->y;
3658 send_event->crossing.state = event->state;
3659 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3661 /* We send the event to 'widget', the currently active menu,
3662 * instead of 'menu', the menu that the pointer is in. This
3663 * will ensure that the event will be ignored unless the
3664 * menuitem is a child of the active menu or some parent
3665 * menu of the active menu.
3667 result = gtk_widget_event (widget, send_event);
3668 gdk_event_free (send_event);
3678 get_double_arrows (GtkMenu *menu)
3680 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3681 gboolean double_arrows;
3682 GtkArrowPlacement arrow_placement;
3684 gtk_widget_style_get (GTK_WIDGET (menu),
3685 "double-arrows", &double_arrows,
3686 "arrow-placement", &arrow_placement,
3689 if (arrow_placement != GTK_ARROWS_BOTH)
3692 return double_arrows || (priv->initially_pushed_in &&
3693 menu->scroll_offset != 0);
3697 gtk_menu_scroll_by (GtkMenu *menu,
3700 GtkMenuPrivate *priv;
3701 GtkBorder arrow_border;
3704 gint view_width, view_height;
3705 gboolean double_arrows;
3707 widget = GTK_WIDGET (menu);
3708 offset = menu->scroll_offset + step;
3709 priv = gtk_menu_get_private (menu);
3711 get_arrows_border (menu, &arrow_border);
3713 double_arrows = get_double_arrows (menu);
3715 /* If we scroll upward and the non-visible top part
3716 * is smaller than the scroll arrow it would be
3717 * pretty stupid to show the arrow and taking more
3718 * screen space than just scrolling to the top.
3721 if ((step < 0) && (offset < arrow_border.top))
3724 /* Don't scroll over the top if we weren't before: */
3725 if ((menu->scroll_offset >= 0) && (offset < 0))
3728 gdk_drawable_get_size (gtk_widget_get_window (widget),
3729 &view_width, &view_height);
3731 if (menu->scroll_offset == 0 &&
3732 view_height >= priv->requested_height)
3735 /* Don't scroll past the bottom if we weren't before: */
3736 if (menu->scroll_offset > 0)
3737 view_height -= arrow_border.top;
3739 /* When both arrows are always shown, reduce
3740 * view height even more.
3743 view_height -= arrow_border.bottom;
3745 if ((menu->scroll_offset + view_height <= priv->requested_height) &&
3746 (offset + view_height > priv->requested_height))
3747 offset = priv->requested_height - view_height;
3749 if (offset != menu->scroll_offset)
3750 gtk_menu_scroll_to (menu, offset);
3754 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3755 gboolean touchscreen_mode)
3757 gboolean upper_visible;
3758 gboolean lower_visible;
3760 upper_visible = menu->upper_arrow_visible;
3761 lower_visible = menu->lower_arrow_visible;
3763 gtk_menu_scroll_by (menu, menu->scroll_step);
3765 if (touchscreen_mode &&
3766 (upper_visible != menu->upper_arrow_visible ||
3767 lower_visible != menu->lower_arrow_visible))
3769 /* We are about to hide a scroll arrow while the mouse is pressed,
3770 * this would cause the uncovered menu item to be activated on button
3771 * release. Therefore we need to ignore button release here
3773 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3774 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3779 gtk_menu_scroll_timeout (gpointer data)
3782 gboolean touchscreen_mode;
3784 menu = GTK_MENU (data);
3786 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3787 "gtk-touchscreen-mode", &touchscreen_mode,
3790 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3796 gtk_menu_scroll_timeout_initial (gpointer data)
3800 gboolean touchscreen_mode;
3802 menu = GTK_MENU (data);
3804 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3805 "gtk-timeout-repeat", &timeout,
3806 "gtk-touchscreen-mode", &touchscreen_mode,
3809 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3811 gtk_menu_remove_scroll_timeout (menu);
3813 menu->timeout_id = gdk_threads_add_timeout (timeout,
3814 gtk_menu_scroll_timeout,
3821 gtk_menu_start_scrolling (GtkMenu *menu)
3824 gboolean touchscreen_mode;
3826 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3827 "gtk-timeout-repeat", &timeout,
3828 "gtk-touchscreen-mode", &touchscreen_mode,
3831 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3833 menu->timeout_id = gdk_threads_add_timeout (timeout,
3834 gtk_menu_scroll_timeout_initial,
3839 gtk_menu_scroll (GtkWidget *widget,
3840 GdkEventScroll *event)
3842 GtkMenu *menu = GTK_MENU (widget);
3844 switch (event->direction)
3846 case GDK_SCROLL_RIGHT:
3847 case GDK_SCROLL_DOWN:
3848 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3850 case GDK_SCROLL_LEFT:
3852 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3860 get_arrows_sensitive_area (GtkMenu *menu,
3861 GdkRectangle *upper,
3862 GdkRectangle *lower)
3864 GtkArrowPlacement arrow_placement;
3865 GtkWidget *widget = GTK_WIDGET (menu);
3869 guint vertical_padding;
3871 gint scroll_arrow_height;
3873 window = gtk_widget_get_window (widget);
3875 gdk_drawable_get_size (window, &width, &height);
3877 gtk_widget_style_get (widget,
3878 "vertical-padding", &vertical_padding,
3879 "scroll-arrow-vlength", &scroll_arrow_height,
3880 "arrow-placement", &arrow_placement,
3883 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) +
3884 gtk_widget_get_style (widget)->ythickness + vertical_padding;
3886 gdk_window_get_position (window, &win_x, &win_y);
3888 switch (arrow_placement)
3890 case GTK_ARROWS_BOTH:
3895 upper->width = width;
3896 upper->height = scroll_arrow_height + border;
3902 lower->y = win_y + height - border - scroll_arrow_height;
3903 lower->width = width;
3904 lower->height = scroll_arrow_height + border;
3908 case GTK_ARROWS_START:
3913 upper->width = width / 2;
3914 upper->height = scroll_arrow_height + border;
3919 lower->x = win_x + width / 2;
3921 lower->width = width / 2;
3922 lower->height = scroll_arrow_height + border;
3926 case GTK_ARROWS_END:
3930 upper->y = win_y + height - border - scroll_arrow_height;
3931 upper->width = width / 2;
3932 upper->height = scroll_arrow_height + border;
3937 lower->x = win_x + width / 2;
3938 lower->y = win_y + height - border - scroll_arrow_height;
3939 lower->width = width / 2;
3940 lower->height = scroll_arrow_height + border;
3948 gtk_menu_handle_scrolling (GtkMenu *menu,
3954 GtkMenuShell *menu_shell;
3955 GtkMenuPrivate *priv;
3958 gboolean scroll_fast = FALSE;
3960 gboolean touchscreen_mode;
3962 priv = gtk_menu_get_private (menu);
3964 menu_shell = GTK_MENU_SHELL (menu);
3966 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3967 "gtk-touchscreen-mode", &touchscreen_mode,
3970 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3975 /* upper arrow handling */
3977 get_arrows_sensitive_area (menu, &rect, NULL);
3980 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3981 (x >= rect.x) && (x < rect.x + rect.width) &&
3982 (y >= rect.y) && (y < rect.y + rect.height))
3987 if (touchscreen_mode)
3988 menu->upper_arrow_prelight = in_arrow;
3990 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3992 gboolean arrow_pressed = FALSE;
3994 if (menu->upper_arrow_visible && !menu->tearoff_active)
3996 if (touchscreen_mode)
3998 if (enter && menu->upper_arrow_prelight)
4000 if (menu->timeout_id == 0)
4002 /* Deselect the active item so that
4003 * any submenus are popped down
4005 gtk_menu_shell_deselect (menu_shell);
4007 gtk_menu_remove_scroll_timeout (menu);
4008 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
4012 /* Only do stuff on click. */
4013 gtk_menu_start_scrolling (menu);
4014 arrow_pressed = TRUE;
4019 arrow_pressed = TRUE;
4024 gtk_menu_stop_scrolling (menu);
4027 else /* !touchscreen_mode */
4029 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
4031 if (enter && in_arrow &&
4032 (!menu->upper_arrow_prelight ||
4033 menu->scroll_fast != scroll_fast))
4035 menu->upper_arrow_prelight = TRUE;
4036 menu->scroll_fast = scroll_fast;
4038 /* Deselect the active item so that
4039 * any submenus are popped down
4041 gtk_menu_shell_deselect (menu_shell);
4043 gtk_menu_remove_scroll_timeout (menu);
4044 menu->scroll_step = scroll_fast ?
4045 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
4048 gdk_threads_add_timeout (scroll_fast ?
4049 MENU_SCROLL_TIMEOUT2 :
4050 MENU_SCROLL_TIMEOUT1,
4051 gtk_menu_scroll_timeout, menu);
4053 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
4055 gtk_menu_stop_scrolling (menu);
4060 /* gtk_menu_start_scrolling() might have hit the top of the
4061 * menu, so check if the button isn't insensitive before
4062 * changing it to something else.
4064 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
4066 GtkStateType arrow_state = GTK_STATE_NORMAL;
4069 arrow_state = GTK_STATE_ACTIVE;
4070 else if (menu->upper_arrow_prelight)
4071 arrow_state = GTK_STATE_PRELIGHT;
4073 if (arrow_state != priv->upper_arrow_state)
4075 priv->upper_arrow_state = arrow_state;
4077 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4083 /* lower arrow handling */
4085 get_arrows_sensitive_area (menu, NULL, &rect);
4088 if (menu->lower_arrow_visible && !menu->tearoff_active &&
4089 (x >= rect.x) && (x < rect.x + rect.width) &&
4090 (y >= rect.y) && (y < rect.y + rect.height))
4095 if (touchscreen_mode)
4096 menu->lower_arrow_prelight = in_arrow;
4098 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4100 gboolean arrow_pressed = FALSE;
4102 if (menu->lower_arrow_visible && !menu->tearoff_active)
4104 if (touchscreen_mode)
4106 if (enter && menu->lower_arrow_prelight)
4108 if (menu->timeout_id == 0)
4110 /* Deselect the active item so that
4111 * any submenus are popped down
4113 gtk_menu_shell_deselect (menu_shell);
4115 gtk_menu_remove_scroll_timeout (menu);
4116 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4120 /* Only do stuff on click. */
4121 gtk_menu_start_scrolling (menu);
4122 arrow_pressed = TRUE;
4127 arrow_pressed = TRUE;
4132 gtk_menu_stop_scrolling (menu);
4135 else /* !touchscreen_mode */
4137 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4139 if (enter && in_arrow &&
4140 (!menu->lower_arrow_prelight ||
4141 menu->scroll_fast != scroll_fast))
4143 menu->lower_arrow_prelight = TRUE;
4144 menu->scroll_fast = scroll_fast;
4146 /* Deselect the active item so that
4147 * any submenus are popped down
4149 gtk_menu_shell_deselect (menu_shell);
4151 gtk_menu_remove_scroll_timeout (menu);
4152 menu->scroll_step = scroll_fast ?
4153 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
4156 gdk_threads_add_timeout (scroll_fast ?
4157 MENU_SCROLL_TIMEOUT2 :
4158 MENU_SCROLL_TIMEOUT1,
4159 gtk_menu_scroll_timeout, menu);
4161 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
4163 gtk_menu_stop_scrolling (menu);
4168 /* gtk_menu_start_scrolling() might have hit the bottom of the
4169 * menu, so check if the button isn't insensitive before
4170 * changing it to something else.
4172 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4174 GtkStateType arrow_state = GTK_STATE_NORMAL;
4177 arrow_state = GTK_STATE_ACTIVE;
4178 else if (menu->lower_arrow_prelight)
4179 arrow_state = GTK_STATE_PRELIGHT;
4181 if (arrow_state != priv->lower_arrow_state)
4183 priv->lower_arrow_state = arrow_state;
4185 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4193 gtk_menu_enter_notify (GtkWidget *widget,
4194 GdkEventCrossing *event)
4196 GtkWidget *menu_item;
4198 gboolean touchscreen_mode;
4200 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4201 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4202 event->mode == GDK_CROSSING_STATE_CHANGED)
4205 g_object_get (gtk_widget_get_settings (widget),
4206 "gtk-touchscreen-mode", &touchscreen_mode,
4209 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4210 if (GTK_IS_MENU (widget))
4212 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4214 if (!menu_shell->ignore_enter)
4215 gtk_menu_handle_scrolling (GTK_MENU (widget),
4216 event->x_root, event->y_root, TRUE, TRUE);
4219 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4221 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4223 if (GTK_IS_MENU (menu))
4225 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
4226 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4228 if (priv->seen_item_enter)
4230 /* This is the second enter we see for an item
4231 * on this menu. This means a release should always
4234 menu_shell->activate_time = 0;
4236 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4237 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4239 if (definitely_within_item (menu_item, event->x, event->y))
4241 /* This is an actual user-enter (ie. not a pop-under)
4242 * In this case, the user must either have entered
4243 * sufficiently far enough into the item, or he must move
4244 * far enough away from the enter point. (see
4245 * gtk_menu_motion_notify())
4247 menu_shell->activate_time = 0;
4251 priv->seen_item_enter = TRUE;
4255 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4256 * will not correspond to the event widget's parent. Check to see
4257 * if we are in the parent's navigation region.
4259 parent = gtk_widget_get_parent (menu_item);
4260 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4261 gtk_menu_navigating_submenu (GTK_MENU (parent),
4262 event->x_root, event->y_root))
4265 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4269 gtk_menu_leave_notify (GtkWidget *widget,
4270 GdkEventCrossing *event)
4272 GtkMenuShell *menu_shell;
4274 GtkMenuItem *menu_item;
4275 GtkWidget *event_widget;
4277 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4278 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4279 event->mode == GDK_CROSSING_STATE_CHANGED)
4282 menu = GTK_MENU (widget);
4283 menu_shell = GTK_MENU_SHELL (widget);
4285 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4288 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4290 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4292 if (!GTK_IS_MENU_ITEM (event_widget))
4295 menu_item = GTK_MENU_ITEM (event_widget);
4297 /* Here we check to see if we're leaving an active menu item with a submenu,
4298 * in which case we enter submenu navigation mode.
4300 if (menu_shell->active_menu_item != NULL
4301 && menu_item->submenu != NULL
4302 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
4304 if (GTK_MENU_SHELL (menu_item->submenu)->active)
4306 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4309 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
4311 /* We are leaving an active menu item with nonactive submenu.
4312 * Deselect it so we don't surprise the user with by popping
4313 * up a submenu _after_ he left the item.
4315 gtk_menu_shell_deselect (menu_shell);
4320 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4324 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4326 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4328 priv->navigation_x = 0;
4329 priv->navigation_y = 0;
4330 priv->navigation_width = 0;
4331 priv->navigation_height = 0;
4333 if (menu->navigation_timeout)
4335 g_source_remove (menu->navigation_timeout);
4336 menu->navigation_timeout = 0;
4340 /* When the timeout is elapsed, the navigation region is destroyed
4341 * and the menuitem under the pointer (if any) is selected.
4344 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4346 GtkMenuPopdownData *popdown_data = user_data;
4347 GtkMenu *menu = popdown_data->menu;
4348 GdkWindow *child_window;
4350 gtk_menu_stop_navigating_submenu (menu);
4352 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4354 child_window = gdk_window_get_device_position (menu->bin_window,
4355 popdown_data->device,
4360 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4362 send_event->crossing.window = g_object_ref (child_window);
4363 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4364 send_event->crossing.send_event = TRUE;
4365 gdk_event_set_device (send_event, popdown_data->device);
4367 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4369 gdk_event_free (send_event);
4377 gtk_menu_navigating_submenu (GtkMenu *menu,
4381 GtkMenuPrivate *priv;
4384 if (!gtk_menu_has_navigation_triangle (menu))
4387 priv = gtk_menu_get_private (menu);
4388 width = priv->navigation_width;
4389 height = priv->navigation_height;
4391 /* check if x/y are in the triangle spanned by the navigation parameters */
4393 /* 1) Move the coordinates so the triangle starts at 0,0 */
4394 event_x -= priv->navigation_x;
4395 event_y -= priv->navigation_y;
4397 /* 2) Ensure both legs move along the positive axis */
4409 /* 3) Check that the given coordinate is inside the triangle. The formula
4410 * is a transformed form of this formula: x/w + y/h <= 1
4412 if (event_x >= 0 && event_y >= 0 &&
4413 event_x * height + event_y * width <= width * height)
4419 gtk_menu_stop_navigating_submenu (menu);
4425 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4426 GtkMenuItem *menu_item,
4427 GdkEventCrossing *event)
4429 gint submenu_left = 0;
4430 gint submenu_right = 0;
4431 gint submenu_top = 0;
4432 gint submenu_bottom = 0;
4435 GtkWidget *event_widget;
4436 GtkMenuPopdownData *popdown_data;
4437 GtkMenuPrivate *priv;
4440 g_return_if_fail (menu_item->submenu != NULL);
4441 g_return_if_fail (event != NULL);
4443 priv = gtk_menu_get_private (menu);
4445 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4447 window = gtk_widget_get_window (menu_item->submenu);
4448 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4449 gdk_drawable_get_size (window, &width, &height);
4451 submenu_right = submenu_left + width;
4452 submenu_bottom = submenu_top + height;
4454 gdk_drawable_get_size (gtk_widget_get_window (event_widget),
4457 if (event->x >= 0 && event->x < width)
4461 gtk_menu_stop_navigating_submenu (menu);
4463 /* The navigation region is the triangle closest to the x/y
4464 * location of the rectangle. This is why the width or height
4468 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
4471 priv->navigation_x = submenu_left;
4472 priv->navigation_width = event->x_root - submenu_left;
4477 priv->navigation_x = submenu_right;
4478 priv->navigation_width = event->x_root - submenu_right;
4484 priv->navigation_y = event->y_root;
4485 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4487 if (priv->navigation_height >= 0)
4493 priv->navigation_y = event->y_root;
4494 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4496 if (priv->navigation_height <= 0)
4500 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4501 "gtk-menu-popdown-delay", &popdown_delay,
4504 popdown_data = g_new (GtkMenuPopdownData, 1);
4505 popdown_data->menu = menu;
4506 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4508 menu->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4510 gtk_menu_stop_navigating_submenu_cb,
4512 (GDestroyNotify) g_free);
4517 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4521 g_return_if_fail (GTK_IS_MENU (menu_shell));
4523 parent = menu_shell->parent_menu_shell;
4525 menu_shell->activate_time = 0;
4526 gtk_menu_popdown (GTK_MENU (menu_shell));
4529 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4533 gtk_menu_position (GtkMenu *menu)
4536 GtkRequisition requisition;
4537 GtkMenuPrivate *private;
4542 GdkScreen *pointer_screen;
4543 GdkRectangle monitor;
4546 g_return_if_fail (GTK_IS_MENU (menu));
4548 widget = GTK_WIDGET (menu);
4550 screen = gtk_widget_get_screen (widget);
4551 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4552 gdk_display_get_device_state (gdk_screen_get_display (screen),
4553 pointer, &pointer_screen, &x, &y, NULL);
4555 /* Get the minimum height for minimum width to figure out
4556 * the right place to popup the menu.
4558 gtk_size_request_get_size (GTK_SIZE_REQUEST (widget), &requisition, NULL);
4560 if (pointer_screen != screen)
4562 /* Pointer is on a different screen; roughly center the
4563 * menu on the screen. If someone was using multiscreen
4564 * + Xinerama together they'd probably want something
4565 * fancier; but that is likely to be vanishingly rare.
4567 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4568 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4571 private = gtk_menu_get_private (menu);
4572 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4574 private->initially_pushed_in = FALSE;
4576 /* Set the type hint here to allow custom position functions to set a different hint */
4577 if (!gtk_widget_get_visible (menu->toplevel))
4578 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4580 if (menu->position_func)
4582 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
4583 menu->position_func_data);
4585 if (private->monitor_num < 0)
4586 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4588 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4592 GtkStyle *style = gtk_widget_get_style (widget);
4593 gint space_left, space_right, space_above, space_below;
4596 gint xthickness = style->xthickness;
4597 gint ythickness = style->ythickness;
4598 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4600 /* The placement of popup menus horizontally works like this (with
4601 * RTL in parentheses)
4603 * - If there is enough room to the right (left) of the mouse cursor,
4604 * position the menu there.
4606 * - Otherwise, if if there is enough room to the left (right) of the
4607 * mouse cursor, position the menu there.
4609 * - Otherwise if the menu is smaller than the monitor, position it
4610 * on the side of the mouse cursor that has the most space available
4612 * - Otherwise (if there is simply not enough room for the menu on the
4613 * monitor), position it as far left (right) as possible.
4615 * Positioning in the vertical direction is similar: first try below
4616 * mouse cursor, then above.
4618 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4620 space_left = x - monitor.x;
4621 space_right = monitor.x + monitor.width - x - 1;
4622 space_above = y - monitor.y;
4623 space_below = monitor.y + monitor.height - y - 1;
4625 /* position horizontally */
4627 /* the amount of space we need to position the menu. Note the
4628 * menu is offset "xthickness" pixels
4630 needed_width = requisition.width - xthickness;
4632 if (needed_width <= space_left ||
4633 needed_width <= space_right)
4635 if ((rtl && needed_width <= space_left) ||
4636 (!rtl && needed_width > space_right))
4639 x = x + xthickness - requisition.width + 1;
4643 /* position right */
4647 /* x is clamped on-screen further down */
4649 else if (requisition.width <= monitor.width)
4651 /* the menu is too big to fit on either side of the mouse
4652 * cursor, but smaller than the monitor. Position it on
4653 * the side that has the most space
4655 if (space_left > space_right)
4663 x = monitor.x + monitor.width - requisition.width;
4666 else /* menu is simply too big for the monitor */
4671 x = monitor.x + monitor.width - requisition.width;
4680 /* Position vertically. The algorithm is the same as above, but
4681 * simpler because we don't have to take RTL into account.
4683 needed_height = requisition.height - ythickness;
4685 if (needed_height <= space_above ||
4686 needed_height <= space_below)
4688 if (needed_height <= space_below)
4691 y = y + ythickness - requisition.height + 1;
4693 y = CLAMP (y, monitor.y,
4694 monitor.y + monitor.height - requisition.height);
4696 else if (needed_height > space_below && needed_height > space_above)
4698 if (space_below >= space_above)
4699 y = monitor.y + monitor.height - requisition.height;
4711 if (private->initially_pushed_in)
4713 menu_height = requisition.height;
4715 if (y + menu_height > monitor.y + monitor.height)
4717 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4718 y = (monitor.y + monitor.height) - menu_height;
4723 scroll_offset += monitor.y - y;
4728 /* FIXME: should this be done in the various position_funcs ? */
4729 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4731 if (GTK_MENU_SHELL (menu)->active)
4733 private->have_position = TRUE;
4738 if (y + requisition.height > monitor.y + monitor.height)
4739 requisition.height = (monitor.y + monitor.height) - y;
4743 scroll_offset += monitor.y - y;
4744 requisition.height -= monitor.y - y;
4748 if (scroll_offset > 0)
4750 GtkBorder arrow_border;
4752 get_arrows_border (menu, &arrow_border);
4753 scroll_offset += arrow_border.top;
4756 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
4759 if (!GTK_MENU_SHELL (menu)->active)
4761 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4762 requisition.width, requisition.height);
4765 menu->scroll_offset = scroll_offset;
4769 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4771 if (menu->timeout_id)
4773 g_source_remove (menu->timeout_id);
4774 menu->timeout_id = 0;
4779 gtk_menu_stop_scrolling (GtkMenu *menu)
4781 gboolean touchscreen_mode;
4783 gtk_menu_remove_scroll_timeout (menu);
4785 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4786 "gtk-touchscreen-mode", &touchscreen_mode,
4789 if (!touchscreen_mode)
4791 menu->upper_arrow_prelight = FALSE;
4792 menu->lower_arrow_prelight = FALSE;
4797 gtk_menu_scroll_to (GtkMenu *menu,
4800 GtkMenuPrivate *priv;
4801 GtkAllocation allocation;
4802 GtkBorder arrow_border;
4806 gint view_width, view_height;
4809 guint vertical_padding;
4810 guint horizontal_padding;
4811 gboolean double_arrows;
4813 widget = GTK_WIDGET (menu);
4814 priv = gtk_menu_get_private (menu);
4816 if (menu->tearoff_active &&
4817 menu->tearoff_adjustment &&
4818 (menu->tearoff_adjustment->value != offset))
4820 menu->tearoff_adjustment->value =
4822 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4823 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4826 /* Move/resize the viewport according to arrows: */
4827 gtk_widget_get_allocation (widget, &allocation);
4828 view_width = allocation.width;
4829 view_height = allocation.height;
4831 style = gtk_widget_get_style (widget);
4833 gtk_widget_style_get (GTK_WIDGET (menu),
4834 "vertical-padding", &vertical_padding,
4835 "horizontal-padding", &horizontal_padding,
4838 double_arrows = get_double_arrows (menu);
4840 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4842 view_width -= (border_width + style->xthickness + horizontal_padding) * 2;
4843 view_height -= (border_width + style->ythickness + vertical_padding) * 2;
4844 menu_height = priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2;
4846 x = border_width + style->xthickness + horizontal_padding;
4847 y = border_width + style->ythickness + vertical_padding;
4849 if (double_arrows && !menu->tearoff_active)
4851 if (view_height < menu_height ||
4852 (offset > 0 && menu->scroll_offset > 0) ||
4853 (offset < 0 && menu->scroll_offset < 0))
4855 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4856 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4857 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4859 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4860 gtk_widget_queue_draw (GTK_WIDGET (menu));
4862 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4864 get_arrows_border (menu, &arrow_border);
4865 y += arrow_border.top;
4866 view_height -= arrow_border.top;
4867 view_height -= arrow_border.bottom;
4870 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4871 else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4872 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4873 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4875 if (offset >= menu_height - view_height)
4876 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4877 else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4878 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4879 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4881 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4882 (priv->lower_arrow_state != lower_arrow_previous_state))
4883 gtk_widget_queue_draw (GTK_WIDGET (menu));
4885 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4886 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4888 /* At the upper border, possibly remove timeout */
4889 if (menu->scroll_step < 0)
4891 gtk_menu_stop_scrolling (menu);
4892 gtk_widget_queue_draw (GTK_WIDGET (menu));
4896 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4897 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4899 /* At the lower border, possibly remove timeout */
4900 if (menu->scroll_step > 0)
4902 gtk_menu_stop_scrolling (menu);
4903 gtk_widget_queue_draw (GTK_WIDGET (menu));
4907 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4911 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4912 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4914 gtk_menu_stop_scrolling (menu);
4915 gtk_widget_queue_draw (GTK_WIDGET (menu));
4918 else if (!menu->tearoff_active)
4920 gboolean last_visible;
4922 last_visible = menu->upper_arrow_visible;
4923 menu->upper_arrow_visible = offset > 0;
4925 /* upper_arrow_visible may have changed, so requery the border */
4926 get_arrows_border (menu, &arrow_border);
4927 view_height -= arrow_border.top;
4929 if ((last_visible != menu->upper_arrow_visible) &&
4930 !menu->upper_arrow_visible)
4932 menu->upper_arrow_prelight = FALSE;
4934 /* If we hid the upper arrow, possibly remove timeout */
4935 if (menu->scroll_step < 0)
4937 gtk_menu_stop_scrolling (menu);
4938 gtk_widget_queue_draw (GTK_WIDGET (menu));
4942 last_visible = menu->lower_arrow_visible;
4943 menu->lower_arrow_visible = offset < menu_height - view_height;
4945 /* lower_arrow_visible may have changed, so requery the border */
4946 get_arrows_border (menu, &arrow_border);
4947 view_height -= arrow_border.bottom;
4949 if ((last_visible != menu->lower_arrow_visible) &&
4950 !menu->lower_arrow_visible)
4952 menu->lower_arrow_prelight = FALSE;
4954 /* If we hid the lower arrow, possibly remove timeout */
4955 if (menu->scroll_step > 0)
4957 gtk_menu_stop_scrolling (menu);
4958 gtk_widget_queue_draw (GTK_WIDGET (menu));
4962 y += arrow_border.top;
4965 /* Scroll the menu: */
4966 if (gtk_widget_get_realized (widget))
4967 gdk_window_move (menu->bin_window, 0, -offset);
4969 if (gtk_widget_get_realized (widget))
4970 gdk_window_move_resize (menu->view_window,
4976 menu->scroll_offset = offset;
4980 compute_child_offset (GtkMenu *menu,
4981 GtkWidget *menu_item,
4984 gboolean *is_last_child)
4986 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4987 gint item_top_attach;
4988 gint item_bottom_attach;
4989 gint child_offset = 0;
4992 get_effective_child_attach (menu_item, NULL, NULL,
4993 &item_top_attach, &item_bottom_attach);
4995 /* there is a possibility that we get called before _size_request, so
4996 * check the height table for safety.
4998 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
5001 /* when we have a row with only invisible children, it's height will
5002 * be zero, so there's no need to check WIDGET_VISIBLE here
5004 for (i = 0; i < item_top_attach; i++)
5005 child_offset += priv->heights[i];
5008 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
5010 *offset = child_offset;
5012 *height = priv->heights[item_top_attach];
5018 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
5019 GtkWidget *menu_item)
5021 GtkMenu *menu = GTK_MENU (menu_shell);
5022 GtkWidget *widget = GTK_WIDGET (menu_shell);
5023 gint child_offset, child_height;
5027 gboolean last_child = 0;
5029 /* We need to check if the selected item fully visible.
5030 * If not we need to scroll the menu so that it becomes fully
5034 if (compute_child_offset (menu, menu_item,
5035 &child_offset, &child_height, &last_child))
5037 guint vertical_padding;
5038 gboolean double_arrows;
5040 y = menu->scroll_offset;
5041 gdk_drawable_get_size (gtk_widget_get_window (widget),
5044 gtk_widget_style_get (widget,
5045 "vertical-padding", &vertical_padding,
5048 double_arrows = get_double_arrows (menu);
5050 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5051 2 * gtk_widget_get_style (widget)->ythickness +
5052 2 * vertical_padding;
5053 if (child_offset < y)
5055 /* Ignore the enter event we might get if the pointer is on the menu
5057 menu_shell->ignore_enter = TRUE;
5058 gtk_menu_scroll_to (menu, child_offset);
5062 GtkBorder arrow_border;
5066 get_arrows_border (menu, &arrow_border);
5067 if (!menu->tearoff_active)
5068 arrow_height = arrow_border.top + arrow_border.bottom;
5070 if (child_offset + child_height > y + height - arrow_height)
5073 if ((!last_child && !menu->tearoff_active) || double_arrows)
5074 arrow_height += arrow_border.bottom;
5076 y = child_offset + child_height - height + arrow_height;
5077 if (((y > 0) && !menu->tearoff_active) || double_arrows)
5079 /* Need upper arrow */
5080 arrow_height += arrow_border.top;
5081 y = child_offset + child_height - height + arrow_height;
5083 /* Ignore the enter event we might get if the pointer is on the menu
5085 menu_shell->ignore_enter = TRUE;
5086 gtk_menu_scroll_to (menu, y);
5094 gtk_menu_select_item (GtkMenuShell *menu_shell,
5095 GtkWidget *menu_item)
5097 GtkMenu *menu = GTK_MENU (menu_shell);
5099 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5100 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5102 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5106 /* Reparent the menu, taking care of the refcounting
5108 * If unrealize is true we force a unrealize while reparenting the parent.
5109 * This can help eliminate flicker in some cases.
5111 * What happens is that when the menu is unrealized and then re-realized,
5112 * the allocations are as follows:
5114 * parent - 1x1 at (0,0)
5115 * child1 - 100x20 at (0,0)
5116 * child2 - 100x20 at (0,20)
5117 * child3 - 100x20 at (0,40)
5119 * That is, the parent is small but the children are full sized. Then,
5120 * when the queued_resize gets processed, the parent gets resized to
5123 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5124 * contains the following logic:
5126 * - if a move or resize operation on a window would change the clip
5127 * region on the children, then before the window is resized
5128 * the background for children is temporarily set to None, the
5129 * move/resize done, and the background for the children restored.
5131 * So, at the point where the parent is resized to final size, the
5132 * background for the children is temporarily None, and thus they
5133 * are not cleared to the background color and the previous background
5134 * (the image of the menu) is left in place.
5137 gtk_menu_reparent (GtkMenu *menu,
5138 GtkWidget *new_parent,
5141 GtkObject *object = GTK_OBJECT (menu);
5142 GtkWidget *widget = GTK_WIDGET (menu);
5143 gboolean was_floating = g_object_is_floating (object);
5145 g_object_ref_sink (object);
5149 g_object_ref (object);
5150 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5151 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5152 g_object_unref (object);
5155 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
5158 g_object_force_floating (G_OBJECT (object));
5160 g_object_unref (object);
5164 gtk_menu_show_all (GtkWidget *widget)
5166 /* Show children, but not self. */
5167 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5172 gtk_menu_hide_all (GtkWidget *widget)
5174 /* Hide children, but not self. */
5175 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
5179 * gtk_menu_set_screen:
5180 * @menu: a #GtkMenu.
5181 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5182 * determined by the widget the menu is attached to.
5184 * Sets the #GdkScreen on which the menu will be displayed.
5189 gtk_menu_set_screen (GtkMenu *menu,
5192 g_return_if_fail (GTK_IS_MENU (menu));
5193 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
5195 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5199 menu_change_screen (menu, screen);
5203 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5205 attach_widget_screen_changed (attach_widget, NULL, menu);
5211 * @menu: a #GtkMenu.
5212 * @child: a #GtkMenuItem.
5213 * @left_attach: The column number to attach the left side of the item to.
5214 * @right_attach: The column number to attach the right side of the item to.
5215 * @top_attach: The row number to attach the top of the item to.
5216 * @bottom_attach: The row number to attach the bottom of the item to.
5218 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5219 * an item will occupy is specified by @left_attach, @right_attach,
5220 * @top_attach and @bottom_attach. These each represent the leftmost,
5221 * rightmost, uppermost and lower column and row numbers of the table.
5222 * (Columns and rows are indexed from zero).
5224 * Note that this function is not related to gtk_menu_detach().
5229 gtk_menu_attach (GtkMenu *menu,
5234 guint bottom_attach)
5236 GtkMenuShell *menu_shell;
5239 g_return_if_fail (GTK_IS_MENU (menu));
5240 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5241 parent = gtk_widget_get_parent (child);
5242 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5243 g_return_if_fail (left_attach < right_attach);
5244 g_return_if_fail (top_attach < bottom_attach);
5246 menu_shell = GTK_MENU_SHELL (menu);
5250 AttachInfo *ai = get_attach_info (child);
5252 ai->left_attach = left_attach;
5253 ai->right_attach = right_attach;
5254 ai->top_attach = top_attach;
5255 ai->bottom_attach = bottom_attach;
5257 menu_shell->children = g_list_append (menu_shell->children, child);
5259 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5261 menu_queue_resize (menu);
5265 gtk_container_child_set (GTK_CONTAINER (parent), child,
5266 "left-attach", left_attach,
5267 "right-attach", right_attach,
5268 "top-attach", top_attach,
5269 "bottom-attach", bottom_attach,
5275 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5279 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5280 "gtk-menu-popup-delay", &popup_delay,
5287 find_child_containing (GtkMenuShell *menu_shell,
5295 /* find a child which includes the area given by
5296 * left, right, top, bottom.
5299 for (list = menu_shell->children; list; list = list->next)
5303 if (!_gtk_menu_item_is_selectable (list->data))
5306 get_effective_child_attach (list->data, &l, &r, &t, &b);
5308 if (l <= left && right <= r
5309 && t <= top && bottom <= b)
5310 return GTK_WIDGET (list->data);
5317 gtk_menu_move_current (GtkMenuShell *menu_shell,
5318 GtkMenuDirectionType direction)
5320 GtkMenu *menu = GTK_MENU (menu_shell);
5323 GtkWidget *match = NULL;
5325 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5329 case GTK_MENU_DIR_CHILD:
5330 direction = GTK_MENU_DIR_PARENT;
5332 case GTK_MENU_DIR_PARENT:
5333 direction = GTK_MENU_DIR_CHILD;
5339 /* use special table menu key bindings */
5340 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5342 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
5344 if (direction == GTK_MENU_DIR_NEXT)
5346 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5348 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5356 for (i = 0; i < t; i++)
5358 match = find_child_containing (menu_shell,
5359 l, l + 1, i, i + 1);
5365 else if (direction == GTK_MENU_DIR_PREV)
5367 for (i = t; i > 0; i--)
5369 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
5377 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5379 match = find_child_containing (menu_shell,
5380 l, l + 1, i - 1, i);
5386 else if (direction == GTK_MENU_DIR_PARENT)
5388 /* we go one left if possible */
5390 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
5394 GtkWidget *parent = menu_shell->parent_menu_shell;
5397 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
5398 match = menu_shell->active_menu_item;
5401 else if (direction == GTK_MENU_DIR_CHILD)
5403 /* we go one right if possible */
5404 if (r < gtk_menu_get_n_columns (menu))
5405 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5409 GtkWidget *parent = menu_shell->parent_menu_shell;
5411 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
5413 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
5414 match = menu_shell->active_menu_item;
5420 gtk_menu_shell_select_item (menu_shell, match);
5425 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5429 get_visible_size (GtkMenu *menu)
5431 GtkAllocation allocation;
5432 GtkWidget *widget = GTK_WIDGET (menu);
5433 GtkContainer *container = GTK_CONTAINER (menu);
5436 gtk_widget_get_allocation (widget, &allocation);
5437 menu_height = (allocation.height
5438 - 2 * (gtk_container_get_border_width (container)
5439 + gtk_widget_get_style (widget)->ythickness));
5441 if (!menu->tearoff_active)
5443 GtkBorder arrow_border;
5445 get_arrows_border (menu, &arrow_border);
5446 menu_height -= arrow_border.top;
5447 menu_height -= arrow_border.bottom;
5453 /* Find the sensitive on-screen child containing @y, or if none,
5454 * the nearest selectable onscreen child. (%NULL if none)
5457 child_at (GtkMenu *menu,
5460 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5461 GtkWidget *child = NULL;
5462 gint child_offset = 0;
5465 gint lower, upper; /* Onscreen bounds */
5467 menu_height = get_visible_size (menu);
5468 lower = menu->scroll_offset;
5469 upper = menu->scroll_offset + menu_height;
5471 for (children = menu_shell->children; children; children = children->next)
5473 if (gtk_widget_get_visible (children->data))
5475 GtkRequisition child_requisition;
5477 gtk_size_request_get_size (GTK_SIZE_REQUEST (children->data),
5478 &child_requisition, NULL);
5480 if (_gtk_menu_item_is_selectable (children->data) &&
5481 child_offset >= lower &&
5482 child_offset + child_requisition.height <= upper)
5484 child = children->data;
5486 if (child_offset + child_requisition.height > y &&
5487 !GTK_IS_TEAROFF_MENU_ITEM (child))
5491 child_offset += child_requisition.height;
5499 get_menu_height (GtkMenu *menu)
5501 GtkAllocation allocation;
5502 GtkWidget *widget = GTK_WIDGET (menu);
5505 gtk_widget_get_allocation (widget, &allocation);
5507 height = allocation.height;
5508 height -= gtk_container_get_border_width (GTK_CONTAINER (widget) + gtk_widget_get_style (widget)->ythickness) * 2;
5510 if (!menu->tearoff_active)
5512 GtkBorder arrow_border;
5514 get_arrows_border (menu, &arrow_border);
5515 height -= arrow_border.top;
5516 height -= arrow_border.bottom;
5523 gtk_menu_real_move_scroll (GtkMenu *menu,
5526 gint page_size = get_visible_size (menu);
5527 gint end_position = get_menu_height (menu);
5528 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5532 case GTK_SCROLL_PAGE_UP:
5533 case GTK_SCROLL_PAGE_DOWN:
5537 gint child_offset = 0;
5538 gboolean old_upper_arrow_visible;
5541 if (type == GTK_SCROLL_PAGE_UP)
5546 if (menu_shell->active_menu_item)
5550 compute_child_offset (menu, menu_shell->active_menu_item,
5551 &child_offset, &child_height, NULL);
5552 child_offset += child_height / 2;
5555 menu_shell->ignore_enter = TRUE;
5556 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5557 old_offset = menu->scroll_offset;
5559 new_offset = menu->scroll_offset + step;
5560 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5562 gtk_menu_scroll_to (menu, new_offset);
5564 if (menu_shell->active_menu_item)
5566 GtkWidget *new_child;
5567 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5568 GtkBorder arrow_border;
5570 get_arrows_border (menu, &arrow_border);
5572 if (menu->scroll_offset != old_offset)
5573 step = menu->scroll_offset - old_offset;
5575 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5577 new_child = child_at (menu, child_offset + step);
5579 gtk_menu_shell_select_item (menu_shell, new_child);
5583 case GTK_SCROLL_START:
5584 /* Ignore the enter event we might get if the pointer is on the menu
5586 menu_shell->ignore_enter = TRUE;
5587 gtk_menu_scroll_to (menu, 0);
5588 gtk_menu_shell_select_first (menu_shell, TRUE);
5590 case GTK_SCROLL_END:
5591 /* Ignore the enter event we might get if the pointer is on the menu
5593 menu_shell->ignore_enter = TRUE;
5594 gtk_menu_scroll_to (menu, end_position - page_size);
5595 _gtk_menu_shell_select_last (menu_shell, TRUE);
5604 * gtk_menu_set_monitor:
5606 * @monitor_num: the number of the monitor on which the menu should
5609 * Informs GTK+ on which monitor a menu should be popped up.
5610 * See gdk_screen_get_monitor_geometry().
5612 * This function should be called from a #GtkMenuPositionFunc if the
5613 * menu should not appear on the same monitor as the pointer. This
5614 * information can't be reliably inferred from the coordinates returned
5615 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
5616 * may extend beyond the monitor boundaries or even the screen boundaries.
5621 gtk_menu_set_monitor (GtkMenu *menu,
5624 GtkMenuPrivate *priv;
5625 g_return_if_fail (GTK_IS_MENU (menu));
5627 priv = gtk_menu_get_private (menu);
5629 priv->monitor_num = monitor_num;
5633 * gtk_menu_get_monitor:
5636 * Retrieves the number of the monitor on which to show the menu.
5638 * Returns: the number of the monitor on which the menu should
5639 * be popped up or -1, if no monitor has been set
5644 gtk_menu_get_monitor (GtkMenu *menu)
5646 GtkMenuPrivate *priv;
5647 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5649 priv = gtk_menu_get_private (menu);
5651 return priv->monitor_num;
5655 * gtk_menu_get_for_attach_widget:
5656 * @widget: a #GtkWidget
5658 * Returns a list of the menus which are attached to this widget.
5659 * This list is owned by GTK+ and must not be modified.
5661 * Return value: (element-type GtkWidget) (transfer none): the list of menus attached to his widget.
5666 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5670 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5672 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5677 gtk_menu_grab_notify (GtkWidget *widget,
5678 gboolean was_grabbed)
5680 GtkWidget *toplevel;
5681 GtkWindowGroup *group;
5685 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5688 !gtk_widget_device_is_shadowed (widget, pointer))
5691 toplevel = gtk_widget_get_toplevel (widget);
5693 if (!GTK_IS_WINDOW (toplevel))
5696 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5697 grab = gtk_window_group_get_current_device_grab (group, pointer);
5699 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
5700 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5704 * gtk_menu_set_reserve_toggle_size:
5706 * @reserve_toggle_size: whether to reserve size for toggles
5708 * Sets whether the menu should reserve space for drawing toggles
5709 * or icons, regardless of their actual presence.
5714 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5715 gboolean reserve_toggle_size)
5717 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5718 gboolean no_toggle_size;
5720 no_toggle_size = !reserve_toggle_size;
5722 if (priv->no_toggle_size != no_toggle_size)
5724 priv->no_toggle_size = no_toggle_size;
5726 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5731 * gtk_menu_get_reserve_toggle_size:
5734 * Returns whether the menu reserves space for toggles and
5735 * icons, regardless of their actual presence.
5737 * Returns: Whether the menu reserves toggle space
5742 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5744 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5746 return !priv->no_toggle_size;