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/.
27 #define GTK_MENU_INTERNALS
30 #include "gdk/gdkkeysyms.h"
31 #include "gtkaccellabel.h"
32 #include "gtkaccelmap.h"
33 #include "gtkbindings.h"
34 #include "gtkcheckmenuitem.h"
35 #include <gobject/gvaluecollector.h>
37 #include "gtkmarshalers.h"
39 #include "gtktearoffmenuitem.h"
40 #include "gtkwindow.h"
42 #include "gtkvscrollbar.h"
43 #include "gtksettings.h"
44 #include "gtkprivate.h"
47 #define DEFAULT_POPUP_DELAY 225
48 #define DEFAULT_POPDOWN_DELAY 1000
50 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
51 * extends below the submenu
54 #define MENU_SCROLL_STEP1 8
55 #define MENU_SCROLL_STEP2 15
56 #define MENU_SCROLL_FAST_ZONE 8
57 #define MENU_SCROLL_TIMEOUT1 50
58 #define MENU_SCROLL_TIMEOUT2 20
60 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
61 #define ATTACHED_MENUS "gtk-attached-menus"
63 typedef struct _GtkMenuAttachData GtkMenuAttachData;
64 typedef struct _GtkMenuPrivate GtkMenuPrivate;
65 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
67 struct _GtkMenuAttachData
69 GtkWidget *attach_widget;
70 GtkMenuDetachFunc detacher;
73 struct _GtkMenuPrivate
77 gboolean initially_pushed_in;
79 /* info used for the table */
85 /* Cached layout information */
92 GtkStateType lower_arrow_state;
93 GtkStateType upper_arrow_state;
95 /* navigation region */
99 int navigation_height;
101 guint have_layout : 1;
102 guint seen_item_enter : 1;
103 guint have_position : 1;
104 guint ignore_button_release : 1;
105 guint no_toggle_size : 1;
108 struct _GtkMenuPopdownData
120 gint effective_left_attach;
121 gint effective_right_attach;
122 gint effective_top_attach;
123 gint effective_bottom_attach;
140 PROP_RESERVE_TOGGLE_SIZE
145 CHILD_PROP_LEFT_ATTACH,
146 CHILD_PROP_RIGHT_ATTACH,
147 CHILD_PROP_TOP_ATTACH,
148 CHILD_PROP_BOTTOM_ATTACH
151 static void gtk_menu_set_property (GObject *object,
155 static void gtk_menu_get_property (GObject *object,
159 static void gtk_menu_set_child_property(GtkContainer *container,
164 static void gtk_menu_get_child_property(GtkContainer *container,
169 static void gtk_menu_destroy (GtkObject *object);
170 static void gtk_menu_realize (GtkWidget *widget);
171 static void gtk_menu_unrealize (GtkWidget *widget);
172 static void gtk_menu_size_request (GtkWidget *widget,
173 GtkRequisition *requisition);
174 static void gtk_menu_size_allocate (GtkWidget *widget,
175 GtkAllocation *allocation);
176 static void gtk_menu_paint (GtkWidget *widget,
177 GdkEventExpose *expose);
178 static void gtk_menu_show (GtkWidget *widget);
179 static gboolean gtk_menu_expose (GtkWidget *widget,
180 GdkEventExpose *event);
181 static gboolean gtk_menu_key_press (GtkWidget *widget,
183 static gboolean gtk_menu_scroll (GtkWidget *widget,
184 GdkEventScroll *event);
185 static gboolean gtk_menu_button_press (GtkWidget *widget,
186 GdkEventButton *event);
187 static gboolean gtk_menu_button_release (GtkWidget *widget,
188 GdkEventButton *event);
189 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
190 GdkEventMotion *event);
191 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
192 GdkEventCrossing *event);
193 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
194 GdkEventCrossing *event);
195 static void gtk_menu_scroll_to (GtkMenu *menu,
197 static void gtk_menu_grab_notify (GtkWidget *widget,
198 gboolean was_grabbed);
200 static void gtk_menu_stop_scrolling (GtkMenu *menu);
201 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
202 static gboolean gtk_menu_scroll_timeout (gpointer data);
203 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
204 static void gtk_menu_start_scrolling (GtkMenu *menu);
206 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
207 GtkWidget *menu_item);
208 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
209 GtkWidget *menu_item);
210 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
213 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
215 static void gtk_menu_handle_scrolling (GtkMenu *menu,
220 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
222 static void gtk_menu_style_set (GtkWidget *widget,
223 GtkStyle *previous_style);
224 static gboolean gtk_menu_focus (GtkWidget *widget,
225 GtkDirectionType direction);
226 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
227 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
228 GtkMenuDirectionType direction);
229 static void gtk_menu_real_move_scroll (GtkMenu *menu,
232 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
233 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
234 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
237 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
238 GtkMenuItem *menu_item,
239 GdkEventCrossing *event);
241 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
242 static void gtk_menu_show_all (GtkWidget *widget);
243 static void gtk_menu_hide_all (GtkWidget *widget);
244 static void gtk_menu_position (GtkMenu *menu);
245 static void gtk_menu_reparent (GtkMenu *menu,
246 GtkWidget *new_parent,
248 static void gtk_menu_remove (GtkContainer *menu,
251 static void gtk_menu_update_title (GtkMenu *menu);
253 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
254 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
256 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
258 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
259 gboolean group_changed);
261 static const gchar attach_data_key[] = "gtk-menu-attach-data";
263 static guint menu_signals[LAST_SIGNAL] = { 0 };
265 static GtkMenuPrivate *
266 gtk_menu_get_private (GtkMenu *menu)
268 return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
271 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
274 menu_queue_resize (GtkMenu *menu)
276 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
278 priv->have_layout = FALSE;
279 gtk_widget_queue_resize (GTK_WIDGET (menu));
283 attach_info_free (AttachInfo *info)
285 g_slice_free (AttachInfo, info);
289 get_attach_info (GtkWidget *child)
291 GObject *object = G_OBJECT (child);
292 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
296 ai = g_slice_new0 (AttachInfo);
297 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
298 (GDestroyNotify) attach_info_free);
305 is_grid_attached (AttachInfo *ai)
307 return (ai->left_attach >= 0 &&
308 ai->right_attach >= 0 &&
309 ai->top_attach >= 0 &&
310 ai->bottom_attach >= 0);
314 menu_ensure_layout (GtkMenu *menu)
316 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
318 if (!priv->have_layout)
320 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
324 gint max_right_attach;
325 gint max_bottom_attach;
327 /* Find extents of gridded portion
329 max_right_attach = 1;
330 max_bottom_attach = 0;
332 for (l = menu_shell->children; l; l = l->next)
334 GtkWidget *child = l->data;
335 AttachInfo *ai = get_attach_info (child);
337 if (is_grid_attached (ai))
339 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
340 max_right_attach = MAX (max_right_attach, ai->right_attach);
346 row_occupied = g_malloc0 (max_bottom_attach);
348 for (l = menu_shell->children; l; l = l->next)
350 GtkWidget *child = l->data;
351 AttachInfo *ai = get_attach_info (child);
353 if (is_grid_attached (ai))
357 for (i = ai->top_attach; i < ai->bottom_attach; i++)
358 row_occupied[i] = TRUE;
362 /* Lay non-grid-items out in those rows
365 for (l = menu_shell->children; l; l = l->next)
367 GtkWidget *child = l->data;
368 AttachInfo *ai = get_attach_info (child);
370 if (!is_grid_attached (ai))
372 while (current_row < max_bottom_attach && row_occupied[current_row])
375 ai->effective_left_attach = 0;
376 ai->effective_right_attach = max_right_attach;
377 ai->effective_top_attach = current_row;
378 ai->effective_bottom_attach = current_row + 1;
384 ai->effective_left_attach = ai->left_attach;
385 ai->effective_right_attach = ai->right_attach;
386 ai->effective_top_attach = ai->top_attach;
387 ai->effective_bottom_attach = ai->bottom_attach;
391 g_free (row_occupied);
393 priv->n_rows = MAX (current_row, max_bottom_attach);
394 priv->n_columns = max_right_attach;
395 priv->have_layout = TRUE;
401 gtk_menu_get_n_columns (GtkMenu *menu)
403 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
405 menu_ensure_layout (menu);
407 return priv->n_columns;
411 gtk_menu_get_n_rows (GtkMenu *menu)
413 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
415 menu_ensure_layout (menu);
421 get_effective_child_attach (GtkWidget *child,
427 GtkMenu *menu = GTK_MENU (child->parent);
430 menu_ensure_layout (menu);
432 ai = get_attach_info (child);
435 *l = ai->effective_left_attach;
437 *r = ai->effective_right_attach;
439 *t = ai->effective_top_attach;
441 *b = ai->effective_bottom_attach;
446 gtk_menu_class_init (GtkMenuClass *class)
448 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
449 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
450 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
451 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
452 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
453 GtkBindingSet *binding_set;
455 gobject_class->set_property = gtk_menu_set_property;
456 gobject_class->get_property = gtk_menu_get_property;
458 object_class->destroy = gtk_menu_destroy;
460 widget_class->realize = gtk_menu_realize;
461 widget_class->unrealize = gtk_menu_unrealize;
462 widget_class->size_request = gtk_menu_size_request;
463 widget_class->size_allocate = gtk_menu_size_allocate;
464 widget_class->show = gtk_menu_show;
465 widget_class->expose_event = gtk_menu_expose;
466 widget_class->scroll_event = gtk_menu_scroll;
467 widget_class->key_press_event = gtk_menu_key_press;
468 widget_class->button_press_event = gtk_menu_button_press;
469 widget_class->button_release_event = gtk_menu_button_release;
470 widget_class->motion_notify_event = gtk_menu_motion_notify;
471 widget_class->show_all = gtk_menu_show_all;
472 widget_class->hide_all = gtk_menu_hide_all;
473 widget_class->enter_notify_event = gtk_menu_enter_notify;
474 widget_class->leave_notify_event = gtk_menu_leave_notify;
475 widget_class->style_set = gtk_menu_style_set;
476 widget_class->focus = gtk_menu_focus;
477 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
478 widget_class->grab_notify = gtk_menu_grab_notify;
480 container_class->remove = gtk_menu_remove;
481 container_class->get_child_property = gtk_menu_get_child_property;
482 container_class->set_child_property = gtk_menu_set_child_property;
484 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
485 menu_shell_class->deactivate = gtk_menu_deactivate;
486 menu_shell_class->select_item = gtk_menu_select_item;
487 menu_shell_class->insert = gtk_menu_real_insert;
488 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
489 menu_shell_class->move_current = gtk_menu_move_current;
491 menu_signals[MOVE_SCROLL] =
492 g_signal_new_class_handler (I_("move-scroll"),
493 G_OBJECT_CLASS_TYPE (object_class),
494 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
495 G_CALLBACK (gtk_menu_real_move_scroll),
497 _gtk_marshal_VOID__ENUM,
499 GTK_TYPE_SCROLL_TYPE);
504 * The index of the currently selected menu item, or -1 if no
505 * menu item is selected.
509 g_object_class_install_property (gobject_class,
511 g_param_spec_int ("active",
513 P_("The currently selected menu item"),
515 GTK_PARAM_READWRITE));
518 * GtkMenu:accel-group:
520 * The accel group holding accelerators for the menu.
524 g_object_class_install_property (gobject_class,
526 g_param_spec_object ("accel-group",
528 P_("The accel group holding accelerators for the menu"),
529 GTK_TYPE_ACCEL_GROUP,
530 GTK_PARAM_READWRITE));
533 * GtkMenu:accel-path:
535 * An accel path used to conveniently construct accel paths of child items.
539 g_object_class_install_property (gobject_class,
541 g_param_spec_string ("accel-path",
543 P_("An accel path used to conveniently construct accel paths of child items"),
545 GTK_PARAM_READWRITE));
548 * GtkMenu:attach-widget:
550 * The widget the menu is attached to. Setting this property attaches
551 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
552 * use gtk_menu_attach_to_widget() directly.
556 g_object_class_install_property (gobject_class,
558 g_param_spec_object ("attach-widget",
560 P_("The widget the menu is attached to"),
562 GTK_PARAM_READWRITE));
564 g_object_class_install_property (gobject_class,
566 g_param_spec_string ("tearoff-title",
568 P_("A title that may be displayed by the window manager when this menu is torn-off"),
570 GTK_PARAM_READWRITE));
573 * GtkMenu:tearoff-state:
575 * A boolean that indicates whether the menu is torn-off.
579 g_object_class_install_property (gobject_class,
581 g_param_spec_boolean ("tearoff-state",
583 P_("A boolean that indicates whether the menu is torn-off"),
585 GTK_PARAM_READWRITE));
590 * The monitor the menu will be popped up on.
594 g_object_class_install_property (gobject_class,
596 g_param_spec_int ("monitor",
598 P_("The monitor the menu will be popped up on"),
600 GTK_PARAM_READWRITE));
602 gtk_widget_class_install_style_property (widget_class,
603 g_param_spec_int ("vertical-padding",
604 P_("Vertical Padding"),
605 P_("Extra space at the top and bottom of the menu"),
609 GTK_PARAM_READABLE));
612 * GtkMenu:reserve-toggle-size:
614 * A boolean that indicates whether the menu reserves space for
615 * toggles and icons, regardless of their actual presence.
617 * This property should only be changed from its default value
618 * for special-purposes such as tabular menus. Regular menus that
619 * are connected to a menu bar or context menus should reserve
620 * toggle space for consistency.
624 g_object_class_install_property (gobject_class,
625 PROP_RESERVE_TOGGLE_SIZE,
626 g_param_spec_boolean ("reserve-toggle-size",
627 P_("Reserve Toggle Size"),
628 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
630 GTK_PARAM_READWRITE));
632 gtk_widget_class_install_style_property (widget_class,
633 g_param_spec_int ("horizontal-padding",
634 P_("Horizontal Padding"),
635 P_("Extra space at the left and right edges of the menu"),
639 GTK_PARAM_READABLE));
641 gtk_widget_class_install_style_property (widget_class,
642 g_param_spec_int ("vertical-offset",
643 P_("Vertical Offset"),
644 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
648 GTK_PARAM_READABLE));
650 gtk_widget_class_install_style_property (widget_class,
651 g_param_spec_int ("horizontal-offset",
652 P_("Horizontal Offset"),
653 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
657 GTK_PARAM_READABLE));
659 gtk_widget_class_install_style_property (widget_class,
660 g_param_spec_boolean ("double-arrows",
662 P_("When scrolling, always show both arrows."),
664 GTK_PARAM_READABLE));
667 * GtkMenu:arrow-placement:
669 * Indicates where scroll arrows should be placed.
673 gtk_widget_class_install_style_property (widget_class,
674 g_param_spec_enum ("arrow-placement",
675 P_("Arrow Placement"),
676 P_("Indicates where scroll arrows should be placed"),
677 GTK_TYPE_ARROW_PLACEMENT,
679 GTK_PARAM_READABLE));
681 gtk_container_class_install_child_property (container_class,
682 CHILD_PROP_LEFT_ATTACH,
683 g_param_spec_int ("left-attach",
685 P_("The column number to attach the left side of the child to"),
687 GTK_PARAM_READWRITE));
689 gtk_container_class_install_child_property (container_class,
690 CHILD_PROP_RIGHT_ATTACH,
691 g_param_spec_int ("right-attach",
693 P_("The column number to attach the right side of the child to"),
695 GTK_PARAM_READWRITE));
697 gtk_container_class_install_child_property (container_class,
698 CHILD_PROP_TOP_ATTACH,
699 g_param_spec_int ("top-attach",
701 P_("The row number to attach the top of the child to"),
703 GTK_PARAM_READWRITE));
705 gtk_container_class_install_child_property (container_class,
706 CHILD_PROP_BOTTOM_ATTACH,
707 g_param_spec_int ("bottom-attach",
709 P_("The row number to attach the bottom of the child to"),
711 GTK_PARAM_READWRITE));
714 * GtkMenu::arrow-scaling
716 * Arbitrary constant to scale down the size of the scroll arrow.
720 gtk_widget_class_install_style_property (widget_class,
721 g_param_spec_float ("arrow-scaling",
723 P_("Arbitrary constant to scale down the size of the scroll arrow"),
725 GTK_PARAM_READABLE));
727 binding_set = gtk_binding_set_by_class (class);
728 gtk_binding_entry_add_signal (binding_set,
730 I_("move-current"), 1,
731 GTK_TYPE_MENU_DIRECTION_TYPE,
733 gtk_binding_entry_add_signal (binding_set,
736 GTK_TYPE_MENU_DIRECTION_TYPE,
738 gtk_binding_entry_add_signal (binding_set,
741 GTK_TYPE_MENU_DIRECTION_TYPE,
743 gtk_binding_entry_add_signal (binding_set,
746 GTK_TYPE_MENU_DIRECTION_TYPE,
748 gtk_binding_entry_add_signal (binding_set,
751 GTK_TYPE_MENU_DIRECTION_TYPE,
752 GTK_MENU_DIR_PARENT);
753 gtk_binding_entry_add_signal (binding_set,
756 GTK_TYPE_MENU_DIRECTION_TYPE,
757 GTK_MENU_DIR_PARENT);
758 gtk_binding_entry_add_signal (binding_set,
761 GTK_TYPE_MENU_DIRECTION_TYPE,
763 gtk_binding_entry_add_signal (binding_set,
766 GTK_TYPE_MENU_DIRECTION_TYPE,
768 gtk_binding_entry_add_signal (binding_set,
771 GTK_TYPE_SCROLL_TYPE,
773 gtk_binding_entry_add_signal (binding_set,
776 GTK_TYPE_SCROLL_TYPE,
778 gtk_binding_entry_add_signal (binding_set,
781 GTK_TYPE_SCROLL_TYPE,
783 gtk_binding_entry_add_signal (binding_set,
786 GTK_TYPE_SCROLL_TYPE,
788 gtk_binding_entry_add_signal (binding_set,
791 GTK_TYPE_SCROLL_TYPE,
793 gtk_binding_entry_add_signal (binding_set,
796 GTK_TYPE_SCROLL_TYPE,
798 gtk_binding_entry_add_signal (binding_set,
801 GTK_TYPE_SCROLL_TYPE,
802 GTK_SCROLL_PAGE_DOWN);
803 gtk_binding_entry_add_signal (binding_set,
806 GTK_TYPE_SCROLL_TYPE,
807 GTK_SCROLL_PAGE_DOWN);
809 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
810 P_("Can change accelerators"),
811 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
813 GTK_PARAM_READWRITE));
815 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
816 P_("Delay before submenus appear"),
817 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
821 GTK_PARAM_READWRITE));
823 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
824 P_("Delay before hiding a submenu"),
825 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
828 DEFAULT_POPDOWN_DELAY,
829 GTK_PARAM_READWRITE));
831 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
836 gtk_menu_set_property (GObject *object,
841 GtkMenu *menu = GTK_MENU (object);
846 gtk_menu_set_active (menu, g_value_get_int (value));
848 case PROP_ACCEL_GROUP:
849 gtk_menu_set_accel_group (menu, g_value_get_object (value));
851 case PROP_ACCEL_PATH:
852 gtk_menu_set_accel_path (menu, g_value_get_string (value));
854 case PROP_ATTACH_WIDGET:
858 widget = gtk_menu_get_attach_widget (menu);
860 gtk_menu_detach (menu);
862 widget = (GtkWidget*) g_value_get_object (value);
864 gtk_menu_attach_to_widget (menu, widget, NULL);
867 case PROP_TEAROFF_STATE:
868 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
870 case PROP_TEAROFF_TITLE:
871 gtk_menu_set_title (menu, g_value_get_string (value));
874 gtk_menu_set_monitor (menu, g_value_get_int (value));
876 case PROP_RESERVE_TOGGLE_SIZE:
877 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
880 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
886 gtk_menu_get_property (GObject *object,
891 GtkMenu *menu = GTK_MENU (object);
896 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->children, gtk_menu_get_active (menu)));
898 case PROP_ACCEL_GROUP:
899 g_value_set_object (value, gtk_menu_get_accel_group (menu));
901 case PROP_ACCEL_PATH:
902 g_value_set_string (value, gtk_menu_get_accel_path (menu));
904 case PROP_ATTACH_WIDGET:
905 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
907 case PROP_TEAROFF_STATE:
908 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
910 case PROP_TEAROFF_TITLE:
911 g_value_set_string (value, gtk_menu_get_title (menu));
914 g_value_set_int (value, gtk_menu_get_monitor (menu));
916 case PROP_RESERVE_TOGGLE_SIZE:
917 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
920 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
926 gtk_menu_set_child_property (GtkContainer *container,
932 GtkMenu *menu = GTK_MENU (container);
933 AttachInfo *ai = get_attach_info (child);
937 case CHILD_PROP_LEFT_ATTACH:
938 ai->left_attach = g_value_get_int (value);
940 case CHILD_PROP_RIGHT_ATTACH:
941 ai->right_attach = g_value_get_int (value);
943 case CHILD_PROP_TOP_ATTACH:
944 ai->top_attach = g_value_get_int (value);
946 case CHILD_PROP_BOTTOM_ATTACH:
947 ai->bottom_attach = g_value_get_int (value);
951 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
955 menu_queue_resize (menu);
959 gtk_menu_get_child_property (GtkContainer *container,
965 AttachInfo *ai = get_attach_info (child);
969 case CHILD_PROP_LEFT_ATTACH:
970 g_value_set_int (value, ai->left_attach);
972 case CHILD_PROP_RIGHT_ATTACH:
973 g_value_set_int (value, ai->right_attach);
975 case CHILD_PROP_TOP_ATTACH:
976 g_value_set_int (value, ai->top_attach);
978 case CHILD_PROP_BOTTOM_ATTACH:
979 g_value_set_int (value, ai->bottom_attach);
983 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
989 gtk_menu_window_event (GtkWidget *window,
993 gboolean handled = FALSE;
995 g_object_ref (window);
1001 case GDK_KEY_RELEASE:
1002 handled = gtk_widget_event (menu, event);
1008 g_object_unref (window);
1009 g_object_unref (menu);
1015 gtk_menu_window_size_request (GtkWidget *window,
1016 GtkRequisition *requisition,
1019 GtkMenuPrivate *private = gtk_menu_get_private (menu);
1021 if (private->have_position)
1023 GdkScreen *screen = gtk_widget_get_screen (window);
1024 GdkRectangle monitor;
1026 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
1028 if (private->y + requisition->height > monitor.y + monitor.height)
1029 requisition->height = monitor.y + monitor.height - private->y;
1031 if (private->y < monitor.y)
1032 requisition->height -= monitor.y - private->y;
1037 gtk_menu_init (GtkMenu *menu)
1039 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
1041 menu->parent_menu_item = NULL;
1042 menu->old_active_menu_item = NULL;
1043 menu->accel_group = NULL;
1044 menu->position_func = NULL;
1045 menu->position_func_data = NULL;
1046 menu->toggle_size = 0;
1048 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1049 "type", GTK_WINDOW_POPUP,
1052 "signal::event", gtk_menu_window_event, menu,
1053 "signal::size-request", gtk_menu_window_size_request, menu,
1054 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1056 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
1057 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
1059 /* Refloat the menu, so that reference counting for the menu isn't
1060 * affected by it being a child of the toplevel
1062 g_object_force_floating (G_OBJECT (menu));
1063 menu->needs_destruction_ref_count = TRUE;
1065 menu->view_window = NULL;
1066 menu->bin_window = NULL;
1068 menu->scroll_offset = 0;
1069 menu->scroll_step = 0;
1070 menu->timeout_id = 0;
1071 menu->scroll_fast = FALSE;
1073 menu->tearoff_window = NULL;
1074 menu->tearoff_hbox = NULL;
1075 menu->torn_off = FALSE;
1076 menu->tearoff_active = FALSE;
1077 menu->tearoff_adjustment = NULL;
1078 menu->tearoff_scrollbar = NULL;
1080 menu->upper_arrow_visible = FALSE;
1081 menu->lower_arrow_visible = FALSE;
1082 menu->upper_arrow_prelight = FALSE;
1083 menu->lower_arrow_prelight = FALSE;
1085 priv->upper_arrow_state = GTK_STATE_NORMAL;
1086 priv->lower_arrow_state = GTK_STATE_NORMAL;
1088 priv->have_layout = FALSE;
1089 priv->monitor_num = -1;
1093 gtk_menu_destroy (GtkObject *object)
1095 GtkMenu *menu = GTK_MENU (object);
1096 GtkMenuAttachData *data;
1097 GtkMenuPrivate *priv;
1099 gtk_menu_remove_scroll_timeout (menu);
1101 data = g_object_get_data (G_OBJECT (object), attach_data_key);
1103 gtk_menu_detach (menu);
1105 gtk_menu_stop_navigating_submenu (menu);
1107 if (menu->old_active_menu_item)
1109 g_object_unref (menu->old_active_menu_item);
1110 menu->old_active_menu_item = NULL;
1113 /* Add back the reference count for being a child */
1114 if (menu->needs_destruction_ref_count)
1116 menu->needs_destruction_ref_count = FALSE;
1117 g_object_ref (object);
1120 if (menu->accel_group)
1122 g_object_unref (menu->accel_group);
1123 menu->accel_group = NULL;
1127 gtk_widget_destroy (menu->toplevel);
1129 if (menu->tearoff_window)
1130 gtk_widget_destroy (menu->tearoff_window);
1132 priv = gtk_menu_get_private (menu);
1136 g_free (priv->heights);
1137 priv->heights = NULL;
1142 g_free (priv->title);
1146 GTK_OBJECT_CLASS (gtk_menu_parent_class)->destroy (object);
1150 menu_change_screen (GtkMenu *menu,
1151 GdkScreen *new_screen)
1153 GtkMenuPrivate *private = gtk_menu_get_private (menu);
1155 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1157 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1163 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1164 gtk_menu_position (menu);
1167 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1168 private->monitor_num = -1;
1172 attach_widget_screen_changed (GtkWidget *attach_widget,
1173 GdkScreen *previous_screen,
1176 if (gtk_widget_has_screen (attach_widget) &&
1177 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1179 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1184 gtk_menu_attach_to_widget (GtkMenu *menu,
1185 GtkWidget *attach_widget,
1186 GtkMenuDetachFunc detacher)
1188 GtkMenuAttachData *data;
1191 g_return_if_fail (GTK_IS_MENU (menu));
1192 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1194 /* keep this function in sync with gtk_widget_set_parent()
1197 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1200 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1201 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1205 g_object_ref_sink (menu);
1207 data = g_slice_new (GtkMenuAttachData);
1208 data->attach_widget = attach_widget;
1210 g_signal_connect (attach_widget, "screen-changed",
1211 G_CALLBACK (attach_widget_screen_changed), menu);
1212 attach_widget_screen_changed (attach_widget, NULL, menu);
1214 data->detacher = detacher;
1215 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1216 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1217 if (!g_list_find (list, menu))
1219 list = g_list_prepend (list, menu);
1221 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1222 (GDestroyNotify) g_list_free);
1224 if (gtk_widget_get_state (GTK_WIDGET (menu)) != GTK_STATE_NORMAL)
1225 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1227 /* we don't need to set the style here, since
1228 * we are a toplevel widget.
1231 /* Fallback title for menu comes from attach widget */
1232 gtk_menu_update_title (menu);
1234 g_object_notify (G_OBJECT (menu), "attach-widget");
1238 gtk_menu_get_attach_widget (GtkMenu *menu)
1240 GtkMenuAttachData *data;
1242 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1244 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1246 return data->attach_widget;
1251 gtk_menu_detach (GtkMenu *menu)
1253 GtkMenuAttachData *data;
1256 g_return_if_fail (GTK_IS_MENU (menu));
1258 /* keep this function in sync with gtk_widget_unparent()
1260 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1263 g_warning ("gtk_menu_detach(): menu is not attached");
1266 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1268 g_signal_handlers_disconnect_by_func (data->attach_widget,
1269 (gpointer) attach_widget_screen_changed,
1273 data->detacher (data->attach_widget, menu);
1274 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1275 list = g_list_remove (list, menu);
1277 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1278 (GDestroyNotify) g_list_free);
1280 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1282 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1283 gtk_widget_unrealize (GTK_WIDGET (menu));
1285 g_slice_free (GtkMenuAttachData, data);
1287 /* Fallback title for menu comes from attach widget */
1288 gtk_menu_update_title (menu);
1290 g_object_unref (menu);
1294 gtk_menu_remove (GtkContainer *container,
1297 GtkMenu *menu = GTK_MENU (container);
1299 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1301 /* Clear out old_active_menu_item if it matches the item we are removing
1303 if (menu->old_active_menu_item == widget)
1305 g_object_unref (menu->old_active_menu_item);
1306 menu->old_active_menu_item = NULL;
1309 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1310 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1312 menu_queue_resize (menu);
1318 return g_object_new (GTK_TYPE_MENU, NULL);
1322 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1326 GtkMenu *menu = GTK_MENU (menu_shell);
1327 AttachInfo *ai = get_attach_info (child);
1329 ai->left_attach = -1;
1330 ai->right_attach = -1;
1331 ai->top_attach = -1;
1332 ai->bottom_attach = -1;
1334 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1335 gtk_widget_set_parent_window (child, menu->bin_window);
1337 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1339 menu_queue_resize (menu);
1343 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1348 widget = GTK_WIDGET (menu);
1355 menu->tearoff_active = FALSE;
1356 menu->saved_scroll_offset = menu->scroll_offset;
1358 gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1360 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
1365 cr = gdk_cairo_create (pixmap);
1366 /* Let's hope that function never notices we're not passing it a pixmap */
1367 gdk_cairo_set_source_pixmap (cr,
1368 menu->tearoff_window->window,
1373 gtk_widget_set_size_request (menu->tearoff_window,
1377 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
1378 g_object_unref (pixmap);
1383 popup_grab_on_window (GdkWindow *window,
1384 GdkDevice *keyboard,
1386 guint32 activate_time)
1389 gdk_device_grab (keyboard, window,
1390 GDK_OWNERSHIP_WINDOW, TRUE,
1391 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1392 NULL, activate_time) != GDK_GRAB_SUCCESS)
1396 gdk_device_grab (pointer, window,
1397 GDK_OWNERSHIP_WINDOW, TRUE,
1398 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1399 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1400 GDK_POINTER_MOTION_MASK,
1401 NULL, activate_time) != GDK_GRAB_SUCCESS)
1404 gdk_device_ungrab (keyboard, activate_time);
1413 * gtk_menu_popup_for_device:
1414 * @menu: a #GtkMenu.
1415 * @device: (allow-none): a #GdkDevice
1416 * @parent_menu_shell: (allow-none): the menu shell containing the triggering menu item, or %NULL
1417 * @parent_menu_item: (allow-none): the menu item whose activation triggered the popup, or %NULL
1418 * @func: (allow-none): a user supplied function used to position the menu, or %NULL
1419 * @data: (allow-none): user supplied data to be passed to @func.
1420 * @button: the mouse button which was pressed to initiate the event.
1421 * @activate_time: the time at which the activation event occurred.
1423 * Displays a menu and makes it available for selection. Applications can use
1424 * this function to display context-sensitive menus, and will typically supply
1425 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1426 * parameters. The default menu positioning function will position the menu
1427 * at the current position of @device (or its corresponding pointer).
1429 * The @button parameter should be the mouse button pressed to initiate
1430 * the menu popup. If the menu popup was initiated by something other than
1431 * a mouse button press, such as a mouse button release or a keypress,
1432 * @button should be 0.
1434 * The @activate_time parameter is used to conflict-resolve initiation of
1435 * concurrent requests for mouse/keyboard grab requests. To function
1436 * properly, this needs to be the time stamp of the user event (such as
1437 * a mouse click or key press) that caused the initiation of the popup.
1438 * Only if no such event is available, gtk_get_current_event_time() can
1444 gtk_menu_popup_for_device (GtkMenu *menu,
1446 GtkWidget *parent_menu_shell,
1447 GtkWidget *parent_menu_item,
1448 GtkMenuPositionFunc func,
1451 guint32 activate_time)
1454 GtkWidget *xgrab_shell;
1456 GdkEvent *current_event;
1457 GtkMenuShell *menu_shell;
1458 gboolean grab_keyboard;
1459 GtkMenuPrivate *priv;
1460 GtkWidget *parent_toplevel;
1461 GdkDevice *keyboard, *pointer;
1463 g_return_if_fail (GTK_IS_MENU (menu));
1464 g_return_if_fail (GDK_IS_DEVICE (device));
1466 widget = GTK_WIDGET (menu);
1467 menu_shell = GTK_MENU_SHELL (menu);
1468 priv = gtk_menu_get_private (menu);
1470 if (device->source == GDK_SOURCE_KEYBOARD)
1473 pointer = gdk_device_get_associated_device (device);
1478 keyboard = gdk_device_get_associated_device (device);
1481 menu_shell->parent_menu_shell = parent_menu_shell;
1483 priv->seen_item_enter = FALSE;
1485 /* Find the last viewable ancestor, and make an X grab on it
1487 parent = GTK_WIDGET (menu);
1491 gboolean viewable = TRUE;
1492 GtkWidget *tmp = parent;
1496 if (!gtk_widget_get_mapped (tmp))
1505 xgrab_shell = parent;
1507 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1510 /* We want to receive events generated when we map the menu; unfortunately,
1511 * since there is probably already an implicit grab in place from the
1512 * button that the user used to pop up the menu, we won't receive then --
1513 * in particular, the EnterNotify when the menu pops up under the pointer.
1515 * If we are grabbing on a parent menu shell, no problem; just grab on
1516 * that menu shell first before popping up the window with owner_events = TRUE.
1518 * When grabbing on the menu itself, things get more convuluted - we
1519 * we do an explicit grab on a specially created window with
1520 * owner_events = TRUE, which we override further down with a grab
1521 * on the menu. (We can't grab on the menu until it is mapped; we
1522 * probably could just leave the grab on the other window, with a
1523 * little reorganization of the code in gtkmenu*).
1525 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1526 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1531 if (xgrab_shell && xgrab_shell != widget)
1533 if (popup_grab_on_window (xgrab_shell->window, keyboard, pointer, activate_time))
1535 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1536 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1541 GdkWindow *transfer_window;
1543 xgrab_shell = widget;
1544 transfer_window = menu_grab_transfer_window_get (menu);
1545 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1547 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1548 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1552 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1554 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1555 * with a stuck up window, we just abort here. Presumably the user will
1558 menu_shell->parent_menu_shell = NULL;
1559 menu_grab_transfer_window_destroy (menu);
1563 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1564 menu_shell->active = TRUE;
1565 menu_shell->button = button;
1567 /* If we are popping up the menu from something other than, a button
1568 * press then, as a heuristic, we ignore enter events for the menu
1569 * until we get a MOTION_NOTIFY.
1572 current_event = gtk_get_current_event ();
1575 if ((current_event->type != GDK_BUTTON_PRESS) &&
1576 (current_event->type != GDK_ENTER_NOTIFY))
1577 menu_shell->ignore_enter = TRUE;
1579 gdk_event_free (current_event);
1582 menu_shell->ignore_enter = TRUE;
1586 gtk_menu_tearoff_bg_copy (menu);
1588 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1591 parent_toplevel = NULL;
1592 if (parent_menu_shell)
1593 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1594 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1596 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1598 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1601 /* Set transient for to get the right window group and parent relationship */
1602 if (GTK_IS_WINDOW (parent_toplevel))
1603 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1604 GTK_WINDOW (parent_toplevel));
1606 menu->parent_menu_item = parent_menu_item;
1607 menu->position_func = func;
1608 menu->position_func_data = data;
1609 menu_shell->activate_time = activate_time;
1611 /* We need to show the menu here rather in the init function because
1612 * code expects to be able to tell if the menu is onscreen by
1613 * looking at the gtk_widget_get_visible (menu)
1615 gtk_widget_show (GTK_WIDGET (menu));
1617 /* Position the menu, possibly changing the size request
1619 gtk_menu_position (menu);
1621 /* Compute the size of the toplevel and realize it so we
1622 * can scroll correctly.
1625 GtkRequisition tmp_request;
1626 GtkAllocation tmp_allocation = { 0, };
1628 gtk_widget_size_request (menu->toplevel, &tmp_request);
1630 tmp_allocation.width = tmp_request.width;
1631 tmp_allocation.height = tmp_request.height;
1633 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1635 gtk_widget_realize (GTK_WIDGET (menu));
1638 gtk_menu_scroll_to (menu, menu->scroll_offset);
1640 /* if no item is selected, select the first one */
1641 if (!menu_shell->active_menu_item)
1643 gboolean touchscreen_mode;
1645 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1646 "gtk-touchscreen-mode", &touchscreen_mode,
1649 if (touchscreen_mode)
1650 gtk_menu_shell_select_first (menu_shell, TRUE);
1653 /* Once everything is set up correctly, map the toplevel window on
1656 gtk_widget_show (menu->toplevel);
1658 if (xgrab_shell == widget)
1659 popup_grab_on_window (widget->window, keyboard, pointer, activate_time); /* Should always succeed */
1661 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1663 if (parent_menu_shell)
1665 gboolean keyboard_mode;
1667 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1668 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1670 else if (menu_shell->button == 0) /* a keynav-activated context menu */
1671 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1673 _gtk_menu_shell_update_mnemonics (menu_shell);
1678 * @menu: a #GtkMenu.
1679 * @parent_menu_shell: (allow-none): the menu shell containing the triggering menu item, or %NULL
1680 * @parent_menu_item: (allow-none): the menu item whose activation triggered the popup, or %NULL
1681 * @func: (allow-none): a user supplied function used to position the menu, or %NULL
1682 * @data: (allow-none): user supplied data to be passed to @func.
1683 * @button: the mouse button which was pressed to initiate the event.
1684 * @activate_time: the time at which the activation event occurred.
1686 * Displays a menu and makes it available for selection. Applications can use
1687 * this function to display context-sensitive menus, and will typically supply
1688 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1689 * parameters. The default menu positioning function will position the menu
1690 * at the current mouse cursor position.
1692 * The @button parameter should be the mouse button pressed to initiate
1693 * the menu popup. If the menu popup was initiated by something other than
1694 * a mouse button press, such as a mouse button release or a keypress,
1695 * @button should be 0.
1697 * The @activate_time parameter is used to conflict-resolve initiation of
1698 * concurrent requests for mouse/keyboard grab requests. To function
1699 * properly, this needs to be the time stamp of the user event (such as
1700 * a mouse click or key press) that caused the initiation of the popup.
1701 * Only if no such event is available, gtk_get_current_event_time() can
1705 gtk_menu_popup (GtkMenu *menu,
1706 GtkWidget *parent_menu_shell,
1707 GtkWidget *parent_menu_item,
1708 GtkMenuPositionFunc func,
1711 guint32 activate_time)
1715 g_return_if_fail (GTK_IS_MENU (menu));
1717 device = gtk_get_current_event_device ();
1721 GdkDisplay *display;
1722 GdkDeviceManager *device_manager;
1725 display = gtk_widget_get_display (GTK_WIDGET (menu));
1726 device_manager = gdk_display_get_device_manager (display);
1727 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1729 device = devices->data;
1731 g_list_free (devices);
1734 gtk_menu_popup_for_device (menu, device,
1738 button, activate_time);
1742 gtk_menu_popdown (GtkMenu *menu)
1744 GtkMenuPrivate *private;
1745 GtkMenuShell *menu_shell;
1748 g_return_if_fail (GTK_IS_MENU (menu));
1750 menu_shell = GTK_MENU_SHELL (menu);
1751 private = gtk_menu_get_private (menu);
1753 menu_shell->parent_menu_shell = NULL;
1754 menu_shell->active = FALSE;
1755 menu_shell->ignore_enter = FALSE;
1757 private->have_position = FALSE;
1759 gtk_menu_stop_scrolling (menu);
1761 gtk_menu_stop_navigating_submenu (menu);
1763 if (menu_shell->active_menu_item)
1765 if (menu->old_active_menu_item)
1766 g_object_unref (menu->old_active_menu_item);
1767 menu->old_active_menu_item = menu_shell->active_menu_item;
1768 g_object_ref (menu->old_active_menu_item);
1771 gtk_menu_shell_deselect (menu_shell);
1773 /* The X Grab, if present, will automatically be removed when we hide
1775 gtk_widget_hide (menu->toplevel);
1776 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1778 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1782 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1784 if (gtk_bin_get_child (GTK_BIN (menu->toplevel)))
1786 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1790 /* We popped up the menu from the tearoff, so we need to
1791 * release the grab - we aren't actually hiding the menu.
1793 if (menu_shell->have_xgrab && pointer)
1795 GdkDevice *keyboard;
1797 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1798 keyboard = gdk_device_get_associated_device (pointer);
1801 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1805 /* gtk_menu_popdown is called each time a menu item is selected from
1806 * a torn off menu. Only scroll back to the saved position if the
1807 * non-tearoff menu was popped down.
1809 if (!menu->tearoff_active)
1810 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1811 menu->tearoff_active = TRUE;
1814 gtk_widget_hide (GTK_WIDGET (menu));
1816 menu_shell->have_xgrab = FALSE;
1819 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1821 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1823 menu_grab_transfer_window_destroy (menu);
1827 gtk_menu_get_active (GtkMenu *menu)
1832 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1834 if (!menu->old_active_menu_item)
1837 children = GTK_MENU_SHELL (menu)->children;
1841 child = children->data;
1842 children = children->next;
1844 if (gtk_bin_get_child (GTK_BIN (child)))
1849 menu->old_active_menu_item = child;
1850 if (menu->old_active_menu_item)
1851 g_object_ref (menu->old_active_menu_item);
1854 return menu->old_active_menu_item;
1858 gtk_menu_set_active (GtkMenu *menu,
1864 g_return_if_fail (GTK_IS_MENU (menu));
1866 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1869 child = tmp_list->data;
1870 if (gtk_bin_get_child (GTK_BIN (child)))
1872 if (menu->old_active_menu_item)
1873 g_object_unref (menu->old_active_menu_item);
1874 menu->old_active_menu_item = child;
1875 g_object_ref (menu->old_active_menu_item);
1882 * gtk_menu_set_accel_group:
1883 * @accel_group: (allow-none):
1886 gtk_menu_set_accel_group (GtkMenu *menu,
1887 GtkAccelGroup *accel_group)
1889 g_return_if_fail (GTK_IS_MENU (menu));
1891 if (menu->accel_group != accel_group)
1893 if (menu->accel_group)
1894 g_object_unref (menu->accel_group);
1895 menu->accel_group = accel_group;
1896 if (menu->accel_group)
1897 g_object_ref (menu->accel_group);
1898 _gtk_menu_refresh_accel_paths (menu, TRUE);
1903 gtk_menu_get_accel_group (GtkMenu *menu)
1905 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1907 return menu->accel_group;
1911 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1914 /* Menu items chain here to figure whether they can activate their
1915 * accelerators. Unlike ordinary widgets, menus allow accel
1916 * activation even if invisible since that's the usual case for
1917 * submenus/popup-menus. however, the state of the attach widget
1918 * affects the "activeness" of the menu.
1920 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1923 return gtk_widget_can_activate_accel (awidget, signal_id);
1925 return gtk_widget_is_sensitive (widget);
1929 * gtk_menu_set_accel_path
1930 * @menu: a valid #GtkMenu
1931 * @accel_path: (allow-none): a valid accelerator path
1933 * Sets an accelerator path for this menu from which accelerator paths
1934 * for its immediate children, its menu items, can be constructed.
1935 * The main purpose of this function is to spare the programmer the
1936 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1937 * each menu item that should support runtime user changable accelerators.
1938 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1939 * each menu item of this menu, that contains a label describing its purpose,
1940 * automatically gets an accel path assigned. For example, a menu containing
1941 * menu items "New" and "Exit", will, after
1942 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1943 * has been called, assign its items the accel paths:
1944 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1945 * Assigning accel paths to menu items then enables the user to change
1946 * their accelerators at runtime. More details about accelerator paths
1947 * and their default setups can be found at gtk_accel_map_add_entry().
1949 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1950 * pass a static string, you can save some memory by interning it first with
1951 * g_intern_static_string().
1954 gtk_menu_set_accel_path (GtkMenu *menu,
1955 const gchar *accel_path)
1957 g_return_if_fail (GTK_IS_MENU (menu));
1959 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1961 /* FIXME: accel_path should be defined as const gchar* */
1962 menu->accel_path = (gchar*)g_intern_string (accel_path);
1963 if (menu->accel_path)
1964 _gtk_menu_refresh_accel_paths (menu, FALSE);
1968 * gtk_menu_get_accel_path
1969 * @menu: a valid #GtkMenu
1971 * Retrieves the accelerator path set on the menu.
1973 * Returns: the accelerator path set on the menu.
1978 gtk_menu_get_accel_path (GtkMenu *menu)
1980 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1982 return menu->accel_path;
1987 gboolean group_changed;
1991 refresh_accel_paths_foreach (GtkWidget *widget,
1994 AccelPropagation *prop = data;
1996 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1997 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1998 prop->menu->accel_path,
1999 prop->menu->accel_group,
2000 prop->group_changed);
2004 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
2005 gboolean group_changed)
2007 g_return_if_fail (GTK_IS_MENU (menu));
2009 if (menu->accel_path && menu->accel_group)
2011 AccelPropagation prop;
2014 prop.group_changed = group_changed;
2015 gtk_container_foreach (GTK_CONTAINER (menu),
2016 refresh_accel_paths_foreach,
2022 gtk_menu_reposition (GtkMenu *menu)
2024 g_return_if_fail (GTK_IS_MENU (menu));
2026 if (!menu->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2027 gtk_menu_position (menu);
2031 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2034 g_return_if_fail (GTK_IS_MENU (menu));
2036 if (adjustment->value != menu->scroll_offset)
2037 gtk_menu_scroll_to (menu, adjustment->value);
2041 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2044 GdkGeometry geometry_hints;
2046 if (!menu->tearoff_window)
2049 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2051 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
2052 width += menu->tearoff_scrollbar->requisition.width;
2055 geometry_hints.min_width = width;
2056 geometry_hints.max_width = width;
2058 geometry_hints.min_height = 0;
2059 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
2061 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
2064 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2068 gtk_menu_update_title (GtkMenu *menu)
2070 if (menu->tearoff_window)
2073 GtkWidget *attach_widget;
2075 title = gtk_menu_get_title (menu);
2078 attach_widget = gtk_menu_get_attach_widget (menu);
2079 if (GTK_IS_MENU_ITEM (attach_widget))
2081 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2082 if (GTK_IS_LABEL (child))
2083 title = gtk_label_get_text (GTK_LABEL (child));
2088 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
2093 gtk_menu_get_toplevel (GtkWidget *menu)
2095 GtkWidget *attach, *toplevel;
2097 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2099 if (GTK_IS_MENU_ITEM (attach))
2100 attach = attach->parent;
2102 if (GTK_IS_MENU (attach))
2103 return gtk_menu_get_toplevel (attach);
2104 else if (GTK_IS_WIDGET (attach))
2106 toplevel = gtk_widget_get_toplevel (attach);
2107 if (gtk_widget_is_toplevel (toplevel))
2115 tearoff_window_destroyed (GtkWidget *widget,
2118 gtk_menu_set_tearoff_state (menu, FALSE);
2122 gtk_menu_set_tearoff_state (GtkMenu *menu,
2127 g_return_if_fail (GTK_IS_MENU (menu));
2129 if (menu->torn_off != torn_off)
2131 menu->torn_off = torn_off;
2132 menu->tearoff_active = torn_off;
2136 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2137 gtk_menu_popdown (menu);
2139 if (!menu->tearoff_window)
2141 GtkWidget *toplevel;
2143 menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2144 "type", GTK_WINDOW_TOPLEVEL,
2145 "screen", gtk_widget_get_screen (menu->toplevel),
2146 "app-paintable", TRUE,
2149 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
2150 GDK_WINDOW_TYPE_HINT_MENU);
2151 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
2152 g_signal_connect (menu->tearoff_window, "destroy",
2153 G_CALLBACK (tearoff_window_destroyed), menu);
2154 g_signal_connect (menu->tearoff_window, "event",
2155 G_CALLBACK (gtk_menu_window_event), menu);
2157 gtk_menu_update_title (menu);
2159 gtk_widget_realize (menu->tearoff_window);
2161 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2162 if (toplevel != NULL)
2163 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
2164 GTK_WINDOW (toplevel));
2166 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
2167 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
2169 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
2170 menu->tearoff_adjustment =
2171 GTK_ADJUSTMENT (gtk_adjustment_new (0,
2173 GTK_WIDGET (menu)->requisition.height,
2177 g_object_connect (menu->tearoff_adjustment,
2178 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2180 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
2182 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
2183 menu->tearoff_scrollbar,
2186 if (menu->tearoff_adjustment->upper > height)
2187 gtk_widget_show (menu->tearoff_scrollbar);
2189 gtk_widget_show (menu->tearoff_hbox);
2192 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
2194 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
2196 /* Update menu->requisition
2198 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
2200 gtk_menu_set_tearoff_hints (menu, width);
2202 gtk_widget_realize (menu->tearoff_window);
2203 gtk_menu_position (menu);
2205 gtk_widget_show (GTK_WIDGET (menu));
2206 gtk_widget_show (menu->tearoff_window);
2208 gtk_menu_scroll_to (menu, 0);
2213 gtk_widget_hide (GTK_WIDGET (menu));
2214 gtk_widget_hide (menu->tearoff_window);
2215 if (GTK_IS_CONTAINER (menu->toplevel))
2216 gtk_menu_reparent (menu, menu->toplevel, FALSE);
2217 gtk_widget_destroy (menu->tearoff_window);
2219 menu->tearoff_window = NULL;
2220 menu->tearoff_hbox = NULL;
2221 menu->tearoff_scrollbar = NULL;
2222 menu->tearoff_adjustment = NULL;
2225 g_object_notify (G_OBJECT (menu), "tearoff-state");
2230 * gtk_menu_get_tearoff_state:
2233 * Returns whether the menu is torn off. See
2234 * gtk_menu_set_tearoff_state ().
2236 * Return value: %TRUE if the menu is currently torn off.
2239 gtk_menu_get_tearoff_state (GtkMenu *menu)
2241 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2243 return menu->torn_off;
2247 * gtk_menu_set_title:
2249 * @title: a string containing the title for the menu.
2251 * Sets the title string for the menu. The title is displayed when the menu
2252 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
2253 * attached to a parent menu item, and if so it will try to use the same text as
2254 * that menu item's label.
2257 gtk_menu_set_title (GtkMenu *menu,
2260 GtkMenuPrivate *priv;
2263 g_return_if_fail (GTK_IS_MENU (menu));
2265 priv = gtk_menu_get_private (menu);
2267 old_title = priv->title;
2268 priv->title = g_strdup (title);
2271 gtk_menu_update_title (menu);
2272 g_object_notify (G_OBJECT (menu), "tearoff-title");
2276 * gtk_menu_get_title:
2279 * Returns the title of the menu. See gtk_menu_set_title().
2281 * Return value: the title of the menu, or %NULL if the menu has no
2282 * title set on it. This string is owned by the widget and should
2283 * not be modified or freed.
2285 G_CONST_RETURN gchar *
2286 gtk_menu_get_title (GtkMenu *menu)
2288 GtkMenuPrivate *priv;
2290 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2292 priv = gtk_menu_get_private (menu);
2298 gtk_menu_reorder_child (GtkMenu *menu,
2302 GtkMenuShell *menu_shell;
2304 g_return_if_fail (GTK_IS_MENU (menu));
2305 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2307 menu_shell = GTK_MENU_SHELL (menu);
2309 if (g_list_find (menu_shell->children, child))
2311 menu_shell->children = g_list_remove (menu_shell->children, child);
2312 menu_shell->children = g_list_insert (menu_shell->children, child, position);
2314 menu_queue_resize (menu);
2319 gtk_menu_style_set (GtkWidget *widget,
2320 GtkStyle *previous_style)
2322 if (gtk_widget_get_realized (widget))
2324 GtkMenu *menu = GTK_MENU (widget);
2326 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2327 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2328 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2333 get_arrows_border (GtkMenu *menu,
2336 guint scroll_arrow_height;
2337 GtkArrowPlacement arrow_placement;
2339 gtk_widget_style_get (GTK_WIDGET (menu),
2340 "scroll-arrow-vlength", &scroll_arrow_height,
2341 "arrow_placement", &arrow_placement,
2344 switch (arrow_placement)
2346 case GTK_ARROWS_BOTH:
2347 border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2348 border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2351 case GTK_ARROWS_START:
2352 border->top = (menu->upper_arrow_visible ||
2353 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2357 case GTK_ARROWS_END:
2359 border->bottom = (menu->upper_arrow_visible ||
2360 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2364 border->left = border->right = 0;
2368 gtk_menu_realize (GtkWidget *widget)
2370 GdkWindowAttr attributes;
2371 gint attributes_mask;
2376 guint vertical_padding;
2377 guint horizontal_padding;
2378 GtkBorder arrow_border;
2380 g_return_if_fail (GTK_IS_MENU (widget));
2382 menu = GTK_MENU (widget);
2384 gtk_widget_set_realized (widget, TRUE);
2386 attributes.window_type = GDK_WINDOW_CHILD;
2387 attributes.x = widget->allocation.x;
2388 attributes.y = widget->allocation.y;
2389 attributes.width = widget->allocation.width;
2390 attributes.height = widget->allocation.height;
2391 attributes.wclass = GDK_INPUT_OUTPUT;
2392 attributes.visual = gtk_widget_get_visual (widget);
2393 attributes.colormap = gtk_widget_get_colormap (widget);
2395 attributes.event_mask = gtk_widget_get_events (widget);
2397 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2398 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2400 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2401 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
2402 gdk_window_set_user_data (widget->window, widget);
2404 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2406 gtk_widget_style_get (GTK_WIDGET (menu),
2407 "vertical-padding", &vertical_padding,
2408 "horizontal-padding", &horizontal_padding,
2411 attributes.x = border_width + widget->style->xthickness + horizontal_padding;
2412 attributes.y = border_width + widget->style->ythickness + vertical_padding;
2413 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
2414 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
2416 get_arrows_border (menu, &arrow_border);
2417 attributes.y += arrow_border.top;
2418 attributes.height -= arrow_border.top;
2419 attributes.height -= arrow_border.bottom;
2421 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
2422 gdk_window_set_user_data (menu->view_window, menu);
2426 attributes.width = MAX (1, widget->allocation.width - (border_width + widget->style->xthickness + horizontal_padding) * 2);
2427 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
2429 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
2430 gdk_window_set_user_data (menu->bin_window, menu);
2432 children = GTK_MENU_SHELL (menu)->children;
2435 child = children->data;
2436 children = children->next;
2438 gtk_widget_set_parent_window (child, menu->bin_window);
2441 widget->style = gtk_style_attach (widget->style, widget->window);
2442 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2443 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2444 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2446 if (GTK_MENU_SHELL (widget)->active_menu_item)
2447 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2448 GTK_MENU_SHELL (widget)->active_menu_item);
2450 gdk_window_show (menu->bin_window);
2451 gdk_window_show (menu->view_window);
2455 gtk_menu_focus (GtkWidget *widget,
2456 GtkDirectionType direction)
2459 * A menu or its menu items cannot have focus
2464 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2467 menu_grab_transfer_window_get (GtkMenu *menu)
2469 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2472 GdkWindowAttr attributes;
2473 gint attributes_mask;
2475 attributes.x = -100;
2476 attributes.y = -100;
2477 attributes.width = 10;
2478 attributes.height = 10;
2479 attributes.window_type = GDK_WINDOW_TEMP;
2480 attributes.wclass = GDK_INPUT_ONLY;
2481 attributes.override_redirect = TRUE;
2482 attributes.event_mask = 0;
2484 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2486 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2487 &attributes, attributes_mask);
2488 gdk_window_set_user_data (window, menu);
2490 gdk_window_show (window);
2492 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2499 menu_grab_transfer_window_destroy (GtkMenu *menu)
2501 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2504 gdk_window_set_user_data (window, NULL);
2505 gdk_window_destroy (window);
2506 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2511 gtk_menu_unrealize (GtkWidget *widget)
2513 GtkMenu *menu = GTK_MENU (widget);
2515 menu_grab_transfer_window_destroy (menu);
2517 gdk_window_set_user_data (menu->view_window, NULL);
2518 gdk_window_destroy (menu->view_window);
2519 menu->view_window = NULL;
2521 gdk_window_set_user_data (menu->bin_window, NULL);
2522 gdk_window_destroy (menu->bin_window);
2523 menu->bin_window = NULL;
2525 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2529 gtk_menu_size_request (GtkWidget *widget,
2530 GtkRequisition *requisition)
2534 GtkMenuShell *menu_shell;
2537 guint max_toggle_size;
2538 guint max_accel_width;
2539 guint vertical_padding;
2540 guint horizontal_padding;
2542 GtkRequisition child_requisition;
2543 GtkMenuPrivate *priv;
2545 g_return_if_fail (GTK_IS_MENU (widget));
2546 g_return_if_fail (requisition != NULL);
2548 menu = GTK_MENU (widget);
2549 menu_shell = GTK_MENU_SHELL (widget);
2550 priv = gtk_menu_get_private (menu);
2552 requisition->width = 0;
2553 requisition->height = 0;
2555 max_toggle_size = 0;
2556 max_accel_width = 0;
2558 g_free (priv->heights);
2559 priv->heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2560 priv->heights_length = gtk_menu_get_n_rows (menu);
2562 children = menu_shell->children;
2569 child = children->data;
2570 children = children->next;
2572 if (! gtk_widget_get_visible (child))
2575 get_effective_child_attach (child, &l, &r, &t, &b);
2577 /* It's important to size_request the child
2578 * before doing the toggle size request, in
2579 * case the toggle size request depends on the size
2580 * request of a child of the child (e.g. for ImageMenuItem)
2583 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
2584 gtk_widget_size_request (child, &child_requisition);
2586 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2587 max_toggle_size = MAX (max_toggle_size, toggle_size);
2588 max_accel_width = MAX (max_accel_width,
2589 GTK_MENU_ITEM (child)->accelerator_width);
2591 part = child_requisition.width / (r - l);
2592 requisition->width = MAX (requisition->width, part);
2594 part = MAX (child_requisition.height, toggle_size) / (b - t);
2595 priv->heights[t] = MAX (priv->heights[t], part);
2598 /* If the menu doesn't include any images or check items
2599 * reserve the space so that all menus are consistent.
2600 * We only do this for 'ordinary' menus, not for combobox
2601 * menus or multi-column menus
2603 if (max_toggle_size == 0 &&
2604 gtk_menu_get_n_columns (menu) == 1 &&
2605 !priv->no_toggle_size)
2607 guint toggle_spacing;
2608 guint indicator_size;
2610 gtk_style_get (widget->style,
2611 GTK_TYPE_CHECK_MENU_ITEM,
2612 "toggle-spacing", &toggle_spacing,
2613 "indicator-size", &indicator_size,
2616 max_toggle_size = indicator_size + toggle_spacing;
2619 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2620 requisition->height += priv->heights[i];
2622 requisition->width += 2 * max_toggle_size + max_accel_width;
2623 requisition->width *= gtk_menu_get_n_columns (menu);
2625 gtk_widget_style_get (GTK_WIDGET (menu),
2626 "vertical-padding", &vertical_padding,
2627 "horizontal-padding", &horizontal_padding,
2630 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2631 requisition->width += (border_width + horizontal_padding + widget->style->xthickness) * 2;
2632 requisition->height += (border_width + vertical_padding + widget->style->ythickness) * 2;
2634 menu->toggle_size = max_toggle_size;
2636 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2638 if (menu->tearoff_active)
2639 gtk_menu_set_tearoff_hints (menu, requisition->width);
2643 gtk_menu_size_allocate (GtkWidget *widget,
2644 GtkAllocation *allocation)
2647 GtkMenuShell *menu_shell;
2649 GtkAllocation child_allocation;
2650 GtkRequisition child_requisition;
2651 GtkMenuPrivate *priv;
2656 guint vertical_padding;
2657 guint horizontal_padding;
2659 g_return_if_fail (GTK_IS_MENU (widget));
2660 g_return_if_fail (allocation != NULL);
2662 menu = GTK_MENU (widget);
2663 menu_shell = GTK_MENU_SHELL (widget);
2664 priv = gtk_menu_get_private (menu);
2666 widget->allocation = *allocation;
2667 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2669 gtk_widget_style_get (GTK_WIDGET (menu),
2670 "vertical-padding", &vertical_padding,
2671 "horizontal-padding", &horizontal_padding,
2674 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2675 x = border_width + widget->style->xthickness + horizontal_padding;
2676 y = border_width + widget->style->ythickness + vertical_padding;
2678 width = MAX (1, allocation->width - x * 2);
2679 height = MAX (1, allocation->height - y * 2);
2681 child_requisition.width -= x * 2;
2682 child_requisition.height -= y * 2;
2684 if (menu_shell->active)
2685 gtk_menu_scroll_to (menu, menu->scroll_offset);
2687 if (!menu->tearoff_active)
2689 GtkBorder arrow_border;
2691 get_arrows_border (menu, &arrow_border);
2692 y += arrow_border.top;
2693 height -= arrow_border.top;
2694 height -= arrow_border.bottom;
2697 if (gtk_widget_get_realized (widget))
2699 gdk_window_move_resize (widget->window,
2700 allocation->x, allocation->y,
2701 allocation->width, allocation->height);
2703 gdk_window_move_resize (menu->view_window,
2710 if (menu_shell->children)
2712 gint base_width = width / gtk_menu_get_n_columns (menu);
2714 children = menu_shell->children;
2717 child = children->data;
2718 children = children->next;
2720 if (gtk_widget_get_visible (child))
2725 get_effective_child_attach (child, &l, &r, &t, &b);
2727 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2730 tmp = gtk_menu_get_n_columns (menu) - l;
2731 l = gtk_menu_get_n_columns (menu) - r;
2735 child_allocation.width = (r - l) * base_width;
2736 child_allocation.height = 0;
2737 child_allocation.x = l * base_width;
2738 child_allocation.y = 0;
2740 for (i = 0; i < b; i++)
2743 child_allocation.y += priv->heights[i];
2745 child_allocation.height += priv->heights[i];
2748 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2751 gtk_widget_size_allocate (child, &child_allocation);
2752 gtk_widget_queue_draw (child);
2756 /* Resize the item window */
2757 if (gtk_widget_get_realized (widget))
2763 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2764 height += priv->heights[i];
2766 width = gtk_menu_get_n_columns (menu) * base_width;
2767 gdk_window_resize (menu->bin_window, width, height);
2770 if (menu->tearoff_active)
2772 if (allocation->height >= widget->requisition.height)
2774 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2776 gtk_widget_hide (menu->tearoff_scrollbar);
2777 gtk_menu_set_tearoff_hints (menu, allocation->width);
2779 gtk_menu_scroll_to (menu, 0);
2784 menu->tearoff_adjustment->upper = widget->requisition.height;
2785 menu->tearoff_adjustment->page_size = allocation->height;
2787 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2788 menu->tearoff_adjustment->upper)
2791 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2794 gtk_menu_scroll_to (menu, value);
2797 gtk_adjustment_changed (menu->tearoff_adjustment);
2799 if (!gtk_widget_get_visible (menu->tearoff_scrollbar))
2801 gtk_widget_show (menu->tearoff_scrollbar);
2802 gtk_menu_set_tearoff_hints (menu, allocation->width);
2810 get_arrows_visible_area (GtkMenu *menu,
2811 GdkRectangle *border,
2812 GdkRectangle *upper,
2813 GdkRectangle *lower,
2816 GtkWidget *widget = GTK_WIDGET (menu);
2818 guint vertical_padding;
2819 guint horizontal_padding;
2820 gint scroll_arrow_height;
2821 GtkArrowPlacement arrow_placement;
2823 gtk_widget_style_get (widget,
2824 "vertical-padding", &vertical_padding,
2825 "horizontal-padding", &horizontal_padding,
2826 "scroll-arrow-vlength", &scroll_arrow_height,
2827 "arrow-placement", &arrow_placement,
2830 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2831 border->x = border_width + widget->style->xthickness + horizontal_padding;
2832 border->y = border_width + widget->style->ythickness + vertical_padding;
2833 gdk_drawable_get_size (widget->window, &border->width, &border->height);
2835 switch (arrow_placement)
2837 case GTK_ARROWS_BOTH:
2838 upper->x = border->x;
2839 upper->y = border->y;
2840 upper->width = border->width - 2 * border->x;
2841 upper->height = scroll_arrow_height;
2843 lower->x = border->x;
2844 lower->y = border->height - border->y - scroll_arrow_height;
2845 lower->width = border->width - 2 * border->x;
2846 lower->height = scroll_arrow_height;
2849 case GTK_ARROWS_START:
2850 upper->x = border->x;
2851 upper->y = border->y;
2852 upper->width = (border->width - 2 * border->x) / 2;
2853 upper->height = scroll_arrow_height;
2855 lower->x = border->x + upper->width;
2856 lower->y = border->y;
2857 lower->width = (border->width - 2 * border->x) / 2;
2858 lower->height = scroll_arrow_height;
2861 case GTK_ARROWS_END:
2862 upper->x = border->x;
2863 upper->y = border->height - border->y - scroll_arrow_height;
2864 upper->width = (border->width - 2 * border->x) / 2;
2865 upper->height = scroll_arrow_height;
2867 lower->x = border->x + upper->width;
2868 lower->y = border->height - border->y - scroll_arrow_height;
2869 lower->width = (border->width - 2 * border->x) / 2;
2870 lower->height = scroll_arrow_height;
2874 g_assert_not_reached();
2875 upper->x = upper->y = upper->width = upper->height = 0;
2876 lower->x = lower->y = lower->width = lower->height = 0;
2879 *arrow_space = scroll_arrow_height - 2 * widget->style->ythickness;
2883 gtk_menu_paint (GtkWidget *widget,
2884 GdkEventExpose *event)
2887 GtkMenuPrivate *priv;
2888 GdkRectangle border;
2893 g_return_if_fail (GTK_IS_MENU (widget));
2895 menu = GTK_MENU (widget);
2896 priv = gtk_menu_get_private (menu);
2898 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2900 if (event->window == widget->window)
2902 gfloat arrow_scaling;
2905 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
2906 arrow_size = arrow_scaling * arrow_space;
2908 gtk_paint_box (widget->style,
2912 &event->area, widget, "menu",
2915 if (menu->upper_arrow_visible && !menu->tearoff_active)
2917 gtk_paint_box (widget->style,
2919 priv->upper_arrow_state,
2921 &event->area, widget, "menu_scroll_arrow_up",
2927 gtk_paint_arrow (widget->style,
2929 priv->upper_arrow_state,
2931 &event->area, widget, "menu_scroll_arrow_up",
2934 upper.x + (upper.width - arrow_size) / 2,
2935 upper.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
2936 arrow_size, arrow_size);
2939 if (menu->lower_arrow_visible && !menu->tearoff_active)
2941 gtk_paint_box (widget->style,
2943 priv->lower_arrow_state,
2945 &event->area, widget, "menu_scroll_arrow_down",
2951 gtk_paint_arrow (widget->style,
2953 priv->lower_arrow_state,
2955 &event->area, widget, "menu_scroll_arrow_down",
2958 lower.x + (lower.width - arrow_size) / 2,
2959 lower.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
2960 arrow_size, arrow_size);
2963 else if (event->window == menu->bin_window)
2965 gint y = -border.y + menu->scroll_offset;
2967 if (!menu->tearoff_active)
2969 GtkBorder arrow_border;
2971 get_arrows_border (menu, &arrow_border);
2972 y -= arrow_border.top;
2975 gtk_paint_box (widget->style,
2979 &event->area, widget, "menu",
2981 border.width, border.height);
2986 gtk_menu_expose (GtkWidget *widget,
2987 GdkEventExpose *event)
2989 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2990 g_return_val_if_fail (event != NULL, FALSE);
2992 if (gtk_widget_is_drawable (widget))
2994 gtk_menu_paint (widget, event);
2996 GTK_WIDGET_CLASS (gtk_menu_parent_class)->expose_event (widget, event);
3003 gtk_menu_show (GtkWidget *widget)
3005 GtkMenu *menu = GTK_MENU (widget);
3007 _gtk_menu_refresh_accel_paths (menu, FALSE);
3009 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3013 gtk_menu_button_scroll (GtkMenu *menu,
3014 GdkEventButton *event)
3016 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
3018 gboolean touchscreen_mode;
3020 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3021 "gtk-touchscreen-mode", &touchscreen_mode,
3024 if (touchscreen_mode)
3025 gtk_menu_handle_scrolling (menu,
3026 event->x_root, event->y_root,
3027 event->type == GDK_BUTTON_PRESS,
3037 pointer_in_menu_window (GtkWidget *widget,
3041 GtkMenu *menu = GTK_MENU (widget);
3043 if (gtk_widget_get_mapped (menu->toplevel))
3045 GtkMenuShell *menu_shell;
3046 gint window_x, window_y;
3048 gdk_window_get_position (menu->toplevel->window, &window_x, &window_y);
3050 if (x_root >= window_x && x_root < window_x + widget->allocation.width &&
3051 y_root >= window_y && y_root < window_y + widget->allocation.height)
3054 menu_shell = GTK_MENU_SHELL (widget);
3056 if (GTK_IS_MENU (menu_shell->parent_menu_shell))
3057 return pointer_in_menu_window (menu_shell->parent_menu_shell,
3065 gtk_menu_button_press (GtkWidget *widget,
3066 GdkEventButton *event)
3068 if (event->type != GDK_BUTTON_PRESS)
3071 /* Don't pass down to menu shell for presses over scroll arrows
3073 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3076 /* Don't pass down to menu shell if a non-menuitem part of the menu
3077 * was clicked. The check for the event_widget being a GtkMenuShell
3078 * works because we have the pointer grabbed on menu_shell->window
3079 * with owner_events=TRUE, so all events that are either outside
3080 * the menu or on its border are delivered relative to
3081 * menu_shell->window.
3083 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3084 pointer_in_menu_window (widget, event->x_root, event->y_root))
3087 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3091 gtk_menu_button_release (GtkWidget *widget,
3092 GdkEventButton *event)
3094 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3096 if (priv->ignore_button_release)
3098 priv->ignore_button_release = FALSE;
3102 if (event->type != GDK_BUTTON_RELEASE)
3105 /* Don't pass down to menu shell for releases over scroll arrows
3107 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3110 /* Don't pass down to menu shell if a non-menuitem part of the menu
3111 * was clicked (see comment in button_press()).
3113 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3114 pointer_in_menu_window (widget, event->x_root, event->y_root))
3116 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3117 * bail out early here so it is in a consistent state for the
3118 * next button_press/button_release in GtkMenuShell.
3121 if (GTK_MENU_SHELL (widget)->active)
3122 GTK_MENU_SHELL (widget)->button = 0;
3127 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3130 static const gchar *
3131 get_accel_path (GtkWidget *menu_item,
3136 GClosure *accel_closure;
3137 GtkAccelGroup *accel_group;
3139 path = _gtk_widget_get_accel_path (menu_item, locked);
3142 path = GTK_MENU_ITEM (menu_item)->accel_path;
3148 label = gtk_bin_get_child (GTK_BIN (menu_item));
3150 if (GTK_IS_ACCEL_LABEL (label))
3152 g_object_get (label,
3153 "accel-closure", &accel_closure,
3157 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3159 *locked = gtk_accel_group_get_is_locked (accel_group);
3169 gtk_menu_key_press (GtkWidget *widget,
3172 GtkMenuShell *menu_shell;
3174 gboolean delete = FALSE;
3175 gboolean can_change_accels;
3176 gchar *accel = NULL;
3177 guint accel_key, accel_mods;
3178 GdkModifierType consumed_modifiers;
3179 GdkDisplay *display;
3181 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3182 g_return_val_if_fail (event != NULL, FALSE);
3184 menu_shell = GTK_MENU_SHELL (widget);
3185 menu = GTK_MENU (widget);
3187 gtk_menu_stop_navigating_submenu (menu);
3189 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3192 display = gtk_widget_get_display (widget);
3194 g_object_get (gtk_widget_get_settings (widget),
3195 "gtk-menu-bar-accel", &accel,
3196 "gtk-can-change-accels", &can_change_accels,
3199 if (accel && *accel)
3202 GdkModifierType mods = 0;
3204 gtk_accelerator_parse (accel, &keyval, &mods);
3207 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3209 /* FIXME this is wrong, needs to be in the global accel resolution
3210 * thing, to properly consider i18n etc., but that probably requires
3211 * AccelGroup changes etc.
3213 if (event->keyval == keyval && (mods & event->state) == mods)
3215 gtk_menu_shell_cancel (menu_shell);
3223 switch (event->keyval)
3234 /* Figure out what modifiers went into determining the key symbol */
3235 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3236 event->hardware_keycode, event->state, event->group,
3237 NULL, NULL, NULL, &consumed_modifiers);
3239 accel_key = gdk_keyval_to_lower (event->keyval);
3240 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3242 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
3243 * We re-upper case when we match against the keyval, but display and save in caseless form.
3245 if (accel_key != event->keyval)
3246 accel_mods |= GDK_SHIFT_MASK;
3248 /* Modify the accelerators */
3249 if (can_change_accels &&
3250 menu_shell->active_menu_item &&
3251 gtk_bin_get_child (GTK_BIN (menu_shell->active_menu_item)) && /* no separators */
3252 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
3253 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3255 GtkWidget *menu_item = menu_shell->active_menu_item;
3256 gboolean locked, replace_accels = TRUE;
3259 path = get_accel_path (menu_item, &locked);
3260 if (!path || locked)
3262 /* can't change accelerators on menu_items without paths
3263 * (basically, those items are accelerator-locked).
3265 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
3266 gtk_widget_error_bell (widget);
3272 /* For the keys that act to delete the current setting, we delete
3273 * the current setting if there is one, otherwise, we set the
3274 * key as the accelerator.
3280 if (gtk_accel_map_lookup_entry (path, &key) &&
3281 (key.accel_key || key.accel_mods))
3287 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3291 /* we failed, probably because this key is in use and
3294 /* g_print("failed to change\n"); */
3295 gtk_widget_error_bell (widget);
3304 check_threshold (GtkWidget *widget,
3313 ABS (start_x - x) > THRESHOLD ||
3314 ABS (start_y - y) > THRESHOLD;
3318 definitely_within_item (GtkWidget *widget,
3322 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
3325 gdk_drawable_get_size (window, &w, &h);
3328 check_threshold (widget, 0, 0, x, y) &&
3329 check_threshold (widget, w - 1, 0, x, y) &&
3330 check_threshold (widget, w - 1, h - 1, x, y) &&
3331 check_threshold (widget, 0, h - 1, x, y);
3335 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3337 GtkMenuPrivate *priv;
3339 priv = gtk_menu_get_private (menu);
3341 return priv->navigation_height && priv->navigation_width;
3345 gtk_menu_motion_notify (GtkWidget *widget,
3346 GdkEventMotion *event)
3348 GtkWidget *menu_item;
3350 GtkMenuShell *menu_shell;
3352 gboolean need_enter;
3354 if (GTK_IS_MENU (widget))
3356 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3358 if (priv->ignore_button_release)
3359 priv->ignore_button_release = FALSE;
3361 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3365 /* We received the event for one of two reasons:
3367 * a) We are the active menu, and did gtk_grab_add()
3368 * b) The widget is a child of ours, and the event was propagated
3370 * Since for computation of navigation regions, we want the menu which
3371 * is the parent of the menu item, for a), we need to find that menu,
3372 * which may be different from 'widget'.
3374 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3375 if (!GTK_IS_MENU_ITEM (menu_item) ||
3376 !GTK_IS_MENU (menu_item->parent))
3379 menu_shell = GTK_MENU_SHELL (menu_item->parent);
3380 menu = GTK_MENU (menu_shell);
3382 if (definitely_within_item (menu_item, event->x, event->y))
3383 menu_shell->activate_time = 0;
3385 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->ignore_enter);
3387 /* Check to see if we are within an active submenu's navigation region
3389 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3392 /* Make sure we pop down if we enter a non-selectable menu item, so we
3393 * don't show a submenu when the cursor is outside the stay-up triangle.
3395 if (!_gtk_menu_item_is_selectable (menu_item))
3397 /* We really want to deselect, but this gives the menushell code
3398 * a chance to do some bookkeeping about the menuitem.
3400 gtk_menu_shell_select_item (menu_shell, menu_item);
3406 /* The menu is now sensitive to enter events on its items, but
3407 * was previously sensitive. So we fake an enter event.
3411 menu_shell->ignore_enter = FALSE;
3413 gdk_drawable_get_size (event->window, &width, &height);
3414 if (event->x >= 0 && event->x < width &&
3415 event->y >= 0 && event->y < height)
3417 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3420 send_event->crossing.window = g_object_ref (event->window);
3421 send_event->crossing.time = event->time;
3422 send_event->crossing.send_event = TRUE;
3423 send_event->crossing.x_root = event->x_root;
3424 send_event->crossing.y_root = event->y_root;
3425 send_event->crossing.x = event->x;
3426 send_event->crossing.y = event->y;
3427 send_event->crossing.state = event->state;
3428 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3430 /* We send the event to 'widget', the currently active menu,
3431 * instead of 'menu', the menu that the pointer is in. This
3432 * will ensure that the event will be ignored unless the
3433 * menuitem is a child of the active menu or some parent
3434 * menu of the active menu.
3436 result = gtk_widget_event (widget, send_event);
3437 gdk_event_free (send_event);
3447 get_double_arrows (GtkMenu *menu)
3449 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3450 gboolean double_arrows;
3451 GtkArrowPlacement arrow_placement;
3453 gtk_widget_style_get (GTK_WIDGET (menu),
3454 "double-arrows", &double_arrows,
3455 "arrow-placement", &arrow_placement,
3458 if (arrow_placement != GTK_ARROWS_BOTH)
3461 return double_arrows || (priv->initially_pushed_in &&
3462 menu->scroll_offset != 0);
3466 gtk_menu_scroll_by (GtkMenu *menu,
3471 gint view_width, view_height;
3472 gboolean double_arrows;
3473 GtkBorder arrow_border;
3475 widget = GTK_WIDGET (menu);
3476 offset = menu->scroll_offset + step;
3478 get_arrows_border (menu, &arrow_border);
3480 double_arrows = get_double_arrows (menu);
3482 /* If we scroll upward and the non-visible top part
3483 * is smaller than the scroll arrow it would be
3484 * pretty stupid to show the arrow and taking more
3485 * screen space than just scrolling to the top.
3488 if ((step < 0) && (offset < arrow_border.top))
3491 /* Don't scroll over the top if we weren't before: */
3492 if ((menu->scroll_offset >= 0) && (offset < 0))
3495 gdk_drawable_get_size (widget->window, &view_width, &view_height);
3497 if (menu->scroll_offset == 0 &&
3498 view_height >= widget->requisition.height)
3501 /* Don't scroll past the bottom if we weren't before: */
3502 if (menu->scroll_offset > 0)
3503 view_height -= arrow_border.top;
3505 /* When both arrows are always shown, reduce
3506 * view height even more.
3509 view_height -= arrow_border.bottom;
3511 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
3512 (offset + view_height > widget->requisition.height))
3513 offset = widget->requisition.height - view_height;
3515 if (offset != menu->scroll_offset)
3516 gtk_menu_scroll_to (menu, offset);
3520 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3521 gboolean touchscreen_mode)
3523 gboolean upper_visible;
3524 gboolean lower_visible;
3526 upper_visible = menu->upper_arrow_visible;
3527 lower_visible = menu->lower_arrow_visible;
3529 gtk_menu_scroll_by (menu, menu->scroll_step);
3531 if (touchscreen_mode &&
3532 (upper_visible != menu->upper_arrow_visible ||
3533 lower_visible != menu->lower_arrow_visible))
3535 /* We are about to hide a scroll arrow while the mouse is pressed,
3536 * this would cause the uncovered menu item to be activated on button
3537 * release. Therefore we need to ignore button release here
3539 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3540 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3545 gtk_menu_scroll_timeout (gpointer data)
3548 gboolean touchscreen_mode;
3550 menu = GTK_MENU (data);
3552 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3553 "gtk-touchscreen-mode", &touchscreen_mode,
3556 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3562 gtk_menu_scroll_timeout_initial (gpointer data)
3566 gboolean touchscreen_mode;
3568 menu = GTK_MENU (data);
3570 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3571 "gtk-timeout-repeat", &timeout,
3572 "gtk-touchscreen-mode", &touchscreen_mode,
3575 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3577 gtk_menu_remove_scroll_timeout (menu);
3579 menu->timeout_id = gdk_threads_add_timeout (timeout,
3580 gtk_menu_scroll_timeout,
3587 gtk_menu_start_scrolling (GtkMenu *menu)
3590 gboolean touchscreen_mode;
3592 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3593 "gtk-timeout-repeat", &timeout,
3594 "gtk-touchscreen-mode", &touchscreen_mode,
3597 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3599 menu->timeout_id = gdk_threads_add_timeout (timeout,
3600 gtk_menu_scroll_timeout_initial,
3605 gtk_menu_scroll (GtkWidget *widget,
3606 GdkEventScroll *event)
3608 GtkMenu *menu = GTK_MENU (widget);
3610 switch (event->direction)
3612 case GDK_SCROLL_RIGHT:
3613 case GDK_SCROLL_DOWN:
3614 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3616 case GDK_SCROLL_LEFT:
3618 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3626 get_arrows_sensitive_area (GtkMenu *menu,
3627 GdkRectangle *upper,
3628 GdkRectangle *lower)
3632 guint vertical_padding;
3634 gint scroll_arrow_height;
3635 GtkArrowPlacement arrow_placement;
3637 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3639 gtk_widget_style_get (GTK_WIDGET (menu),
3640 "vertical-padding", &vertical_padding,
3641 "scroll-arrow-vlength", &scroll_arrow_height,
3642 "arrow-placement", &arrow_placement,
3645 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) +
3646 GTK_WIDGET (menu)->style->ythickness + vertical_padding;
3648 gdk_window_get_position (GTK_WIDGET (menu)->window, &win_x, &win_y);
3650 switch (arrow_placement)
3652 case GTK_ARROWS_BOTH:
3657 upper->width = width;
3658 upper->height = scroll_arrow_height + border;
3664 lower->y = win_y + height - border - scroll_arrow_height;
3665 lower->width = width;
3666 lower->height = scroll_arrow_height + border;
3670 case GTK_ARROWS_START:
3675 upper->width = width / 2;
3676 upper->height = scroll_arrow_height + border;
3681 lower->x = win_x + width / 2;
3683 lower->width = width / 2;
3684 lower->height = scroll_arrow_height + border;
3688 case GTK_ARROWS_END:
3692 upper->y = win_y + height - border - scroll_arrow_height;
3693 upper->width = width / 2;
3694 upper->height = scroll_arrow_height + border;
3699 lower->x = win_x + width / 2;
3700 lower->y = win_y + height - border - scroll_arrow_height;
3701 lower->width = width / 2;
3702 lower->height = scroll_arrow_height + border;
3710 gtk_menu_handle_scrolling (GtkMenu *menu,
3716 GtkMenuShell *menu_shell;
3717 GtkMenuPrivate *priv;
3720 gboolean scroll_fast = FALSE;
3722 gboolean touchscreen_mode;
3724 priv = gtk_menu_get_private (menu);
3726 menu_shell = GTK_MENU_SHELL (menu);
3728 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3729 "gtk-touchscreen-mode", &touchscreen_mode,
3732 gdk_window_get_position (menu->toplevel->window, &top_x, &top_y);
3736 /* upper arrow handling */
3738 get_arrows_sensitive_area (menu, &rect, NULL);
3741 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3742 (x >= rect.x) && (x < rect.x + rect.width) &&
3743 (y >= rect.y) && (y < rect.y + rect.height))
3748 if (touchscreen_mode)
3749 menu->upper_arrow_prelight = in_arrow;
3751 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3753 gboolean arrow_pressed = FALSE;
3755 if (menu->upper_arrow_visible && !menu->tearoff_active)
3757 if (touchscreen_mode)
3759 if (enter && menu->upper_arrow_prelight)
3761 if (menu->timeout_id == 0)
3763 /* Deselect the active item so that
3764 * any submenus are popped down
3766 gtk_menu_shell_deselect (menu_shell);
3768 gtk_menu_remove_scroll_timeout (menu);
3769 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3773 /* Only do stuff on click. */
3774 gtk_menu_start_scrolling (menu);
3775 arrow_pressed = TRUE;
3780 arrow_pressed = TRUE;
3785 gtk_menu_stop_scrolling (menu);
3788 else /* !touchscreen_mode */
3790 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3792 if (enter && in_arrow &&
3793 (!menu->upper_arrow_prelight ||
3794 menu->scroll_fast != scroll_fast))
3796 menu->upper_arrow_prelight = TRUE;
3797 menu->scroll_fast = scroll_fast;
3799 /* Deselect the active item so that
3800 * any submenus are popped down
3802 gtk_menu_shell_deselect (menu_shell);
3804 gtk_menu_remove_scroll_timeout (menu);
3805 menu->scroll_step = scroll_fast ?
3806 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
3809 gdk_threads_add_timeout (scroll_fast ?
3810 MENU_SCROLL_TIMEOUT2 :
3811 MENU_SCROLL_TIMEOUT1,
3812 gtk_menu_scroll_timeout, menu);
3814 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
3816 gtk_menu_stop_scrolling (menu);
3821 /* gtk_menu_start_scrolling() might have hit the top of the
3822 * menu, so check if the button isn't insensitive before
3823 * changing it to something else.
3825 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3827 GtkStateType arrow_state = GTK_STATE_NORMAL;
3830 arrow_state = GTK_STATE_ACTIVE;
3831 else if (menu->upper_arrow_prelight)
3832 arrow_state = GTK_STATE_PRELIGHT;
3834 if (arrow_state != priv->upper_arrow_state)
3836 priv->upper_arrow_state = arrow_state;
3838 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3844 /* lower arrow handling */
3846 get_arrows_sensitive_area (menu, NULL, &rect);
3849 if (menu->lower_arrow_visible && !menu->tearoff_active &&
3850 (x >= rect.x) && (x < rect.x + rect.width) &&
3851 (y >= rect.y) && (y < rect.y + rect.height))
3856 if (touchscreen_mode)
3857 menu->lower_arrow_prelight = in_arrow;
3859 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3861 gboolean arrow_pressed = FALSE;
3863 if (menu->lower_arrow_visible && !menu->tearoff_active)
3865 if (touchscreen_mode)
3867 if (enter && menu->lower_arrow_prelight)
3869 if (menu->timeout_id == 0)
3871 /* Deselect the active item so that
3872 * any submenus are popped down
3874 gtk_menu_shell_deselect (menu_shell);
3876 gtk_menu_remove_scroll_timeout (menu);
3877 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
3881 /* Only do stuff on click. */
3882 gtk_menu_start_scrolling (menu);
3883 arrow_pressed = TRUE;
3888 arrow_pressed = TRUE;
3893 gtk_menu_stop_scrolling (menu);
3896 else /* !touchscreen_mode */
3898 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
3900 if (enter && in_arrow &&
3901 (!menu->lower_arrow_prelight ||
3902 menu->scroll_fast != scroll_fast))
3904 menu->lower_arrow_prelight = TRUE;
3905 menu->scroll_fast = scroll_fast;
3907 /* Deselect the active item so that
3908 * any submenus are popped down
3910 gtk_menu_shell_deselect (menu_shell);
3912 gtk_menu_remove_scroll_timeout (menu);
3913 menu->scroll_step = scroll_fast ?
3914 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
3917 gdk_threads_add_timeout (scroll_fast ?
3918 MENU_SCROLL_TIMEOUT2 :
3919 MENU_SCROLL_TIMEOUT1,
3920 gtk_menu_scroll_timeout, menu);
3922 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
3924 gtk_menu_stop_scrolling (menu);
3929 /* gtk_menu_start_scrolling() might have hit the bottom of the
3930 * menu, so check if the button isn't insensitive before
3931 * changing it to something else.
3933 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3935 GtkStateType arrow_state = GTK_STATE_NORMAL;
3938 arrow_state = GTK_STATE_ACTIVE;
3939 else if (menu->lower_arrow_prelight)
3940 arrow_state = GTK_STATE_PRELIGHT;
3942 if (arrow_state != priv->lower_arrow_state)
3944 priv->lower_arrow_state = arrow_state;
3946 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3954 gtk_menu_enter_notify (GtkWidget *widget,
3955 GdkEventCrossing *event)
3957 GtkWidget *menu_item;
3958 gboolean touchscreen_mode;
3960 if (event->mode == GDK_CROSSING_GTK_GRAB ||
3961 event->mode == GDK_CROSSING_GTK_UNGRAB ||
3962 event->mode == GDK_CROSSING_STATE_CHANGED)
3965 g_object_get (gtk_widget_get_settings (widget),
3966 "gtk-touchscreen-mode", &touchscreen_mode,
3969 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3970 if (GTK_IS_MENU (widget))
3972 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
3974 if (!menu_shell->ignore_enter)
3975 gtk_menu_handle_scrolling (GTK_MENU (widget),
3976 event->x_root, event->y_root, TRUE, TRUE);
3979 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
3981 GtkWidget *menu = menu_item->parent;
3983 if (GTK_IS_MENU (menu))
3985 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
3986 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3988 if (priv->seen_item_enter)
3990 /* This is the second enter we see for an item
3991 * on this menu. This means a release should always
3994 menu_shell->activate_time = 0;
3996 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
3997 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
3999 if (definitely_within_item (menu_item, event->x, event->y))
4001 /* This is an actual user-enter (ie. not a pop-under)
4002 * In this case, the user must either have entered
4003 * sufficiently far enough into the item, or he must move
4004 * far enough away from the enter point. (see
4005 * gtk_menu_motion_notify())
4007 menu_shell->activate_time = 0;
4011 priv->seen_item_enter = TRUE;
4015 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4016 * will not correspond to the event widget's parent. Check to see
4017 * if we are in the parent's navigation region.
4019 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
4020 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent),
4021 event->x_root, event->y_root))
4024 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4028 gtk_menu_leave_notify (GtkWidget *widget,
4029 GdkEventCrossing *event)
4031 GtkMenuShell *menu_shell;
4033 GtkMenuItem *menu_item;
4034 GtkWidget *event_widget;
4036 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4037 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4038 event->mode == GDK_CROSSING_STATE_CHANGED)
4041 menu = GTK_MENU (widget);
4042 menu_shell = GTK_MENU_SHELL (widget);
4044 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4047 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4049 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4051 if (!GTK_IS_MENU_ITEM (event_widget))
4054 menu_item = GTK_MENU_ITEM (event_widget);
4056 /* Here we check to see if we're leaving an active menu item with a submenu,
4057 * in which case we enter submenu navigation mode.
4059 if (menu_shell->active_menu_item != NULL
4060 && menu_item->submenu != NULL
4061 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
4063 if (GTK_MENU_SHELL (menu_item->submenu)->active)
4065 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4068 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
4070 /* We are leaving an active menu item with nonactive submenu.
4071 * Deselect it so we don't surprise the user with by popping
4072 * up a submenu _after_ he left the item.
4074 gtk_menu_shell_deselect (menu_shell);
4079 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4083 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4085 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4087 priv->navigation_x = 0;
4088 priv->navigation_y = 0;
4089 priv->navigation_width = 0;
4090 priv->navigation_height = 0;
4092 if (menu->navigation_timeout)
4094 g_source_remove (menu->navigation_timeout);
4095 menu->navigation_timeout = 0;
4099 /* When the timeout is elapsed, the navigation region is destroyed
4100 * and the menuitem under the pointer (if any) is selected.
4103 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4105 GtkMenuPopdownData *popdown_data = user_data;
4106 GtkMenu *menu = popdown_data->menu;
4107 GdkWindow *child_window;
4109 gtk_menu_stop_navigating_submenu (menu);
4111 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4113 child_window = gdk_window_get_device_position (menu->bin_window,
4114 popdown_data->device,
4119 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4121 send_event->crossing.window = g_object_ref (child_window);
4122 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4123 send_event->crossing.send_event = TRUE;
4124 gdk_event_set_device (send_event, popdown_data->device);
4126 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4128 gdk_event_free (send_event);
4136 gtk_menu_navigating_submenu (GtkMenu *menu,
4140 GtkMenuPrivate *priv;
4143 if (!gtk_menu_has_navigation_triangle (menu))
4146 priv = gtk_menu_get_private (menu);
4147 width = priv->navigation_width;
4148 height = priv->navigation_height;
4150 /* check if x/y are in the triangle spanned by the navigation parameters */
4152 /* 1) Move the coordinates so the triangle starts at 0,0 */
4153 event_x -= priv->navigation_x;
4154 event_y -= priv->navigation_y;
4156 /* 2) Ensure both legs move along the positive axis */
4168 /* 3) Check that the given coordinate is inside the triangle. The formula
4169 * is a transformed form of this formula: x/w + y/h <= 1
4171 if (event_x >= 0 && event_y >= 0 &&
4172 event_x * height + event_y * width <= width * height)
4178 gtk_menu_stop_navigating_submenu (menu);
4184 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4185 GtkMenuItem *menu_item,
4186 GdkEventCrossing *event)
4188 gint submenu_left = 0;
4189 gint submenu_right = 0;
4190 gint submenu_top = 0;
4191 gint submenu_bottom = 0;
4194 GtkWidget *event_widget;
4195 GtkMenuPopdownData *popdown_data;
4196 GtkMenuPrivate *priv;
4198 g_return_if_fail (menu_item->submenu != NULL);
4199 g_return_if_fail (event != NULL);
4201 priv = gtk_menu_get_private (menu);
4203 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4205 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
4206 gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
4208 submenu_right = submenu_left + width;
4209 submenu_bottom = submenu_top + height;
4211 gdk_drawable_get_size (event_widget->window, &width, &height);
4213 if (event->x >= 0 && event->x < width)
4217 gtk_menu_stop_navigating_submenu (menu);
4219 /* The navigation region is the triangle closest to the x/y
4220 * location of the rectangle. This is why the width or height
4224 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
4227 priv->navigation_x = submenu_left;
4228 priv->navigation_width = event->x_root - submenu_left;
4233 priv->navigation_x = submenu_right;
4234 priv->navigation_width = event->x_root - submenu_right;
4240 priv->navigation_y = event->y_root;
4241 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4243 if (priv->navigation_height >= 0)
4249 priv->navigation_y = event->y_root;
4250 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4252 if (priv->navigation_height <= 0)
4256 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4257 "gtk-menu-popdown-delay", &popdown_delay,
4260 popdown_data = g_new (GtkMenuPopdownData, 1);
4261 popdown_data->menu = menu;
4262 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4264 menu->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4266 gtk_menu_stop_navigating_submenu_cb,
4268 (GDestroyNotify) g_free);
4273 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4277 g_return_if_fail (GTK_IS_MENU (menu_shell));
4279 parent = menu_shell->parent_menu_shell;
4281 menu_shell->activate_time = 0;
4282 gtk_menu_popdown (GTK_MENU (menu_shell));
4285 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4289 gtk_menu_position (GtkMenu *menu)
4292 GtkRequisition requisition;
4293 GtkMenuPrivate *private;
4298 GdkScreen *pointer_screen;
4299 GdkRectangle monitor;
4302 g_return_if_fail (GTK_IS_MENU (menu));
4304 widget = GTK_WIDGET (menu);
4306 screen = gtk_widget_get_screen (widget);
4307 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4308 gdk_display_get_device_state (gdk_screen_get_display (screen),
4309 pointer, &pointer_screen, &x, &y, NULL);
4311 /* We need the requisition to figure out the right place to
4312 * popup the menu. In fact, we always need to ask here, since
4313 * if a size_request was queued while we weren't popped up,
4314 * the requisition won't have been recomputed yet.
4316 gtk_widget_size_request (widget, &requisition);
4318 if (pointer_screen != screen)
4320 /* Pointer is on a different screen; roughly center the
4321 * menu on the screen. If someone was using multiscreen
4322 * + Xinerama together they'd probably want something
4323 * fancier; but that is likely to be vanishingly rare.
4325 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4326 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4329 private = gtk_menu_get_private (menu);
4330 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4332 private->initially_pushed_in = FALSE;
4334 /* Set the type hint here to allow custom position functions to set a different hint */
4335 if (!gtk_widget_get_visible (menu->toplevel))
4336 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4338 if (menu->position_func)
4340 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
4341 menu->position_func_data);
4343 if (private->monitor_num < 0)
4344 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4346 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4350 gint space_left, space_right, space_above, space_below;
4353 gint xthickness = widget->style->xthickness;
4354 gint ythickness = widget->style->ythickness;
4355 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4357 /* The placement of popup menus horizontally works like this (with
4358 * RTL in parentheses)
4360 * - If there is enough room to the right (left) of the mouse cursor,
4361 * position the menu there.
4363 * - Otherwise, if if there is enough room to the left (right) of the
4364 * mouse cursor, position the menu there.
4366 * - Otherwise if the menu is smaller than the monitor, position it
4367 * on the side of the mouse cursor that has the most space available
4369 * - Otherwise (if there is simply not enough room for the menu on the
4370 * monitor), position it as far left (right) as possible.
4372 * Positioning in the vertical direction is similar: first try below
4373 * mouse cursor, then above.
4375 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4377 space_left = x - monitor.x;
4378 space_right = monitor.x + monitor.width - x - 1;
4379 space_above = y - monitor.y;
4380 space_below = monitor.y + monitor.height - y - 1;
4382 /* position horizontally */
4384 /* the amount of space we need to position the menu. Note the
4385 * menu is offset "xthickness" pixels
4387 needed_width = requisition.width - xthickness;
4389 if (needed_width <= space_left ||
4390 needed_width <= space_right)
4392 if ((rtl && needed_width <= space_left) ||
4393 (!rtl && needed_width > space_right))
4396 x = x + xthickness - requisition.width + 1;
4400 /* position right */
4404 /* x is clamped on-screen further down */
4406 else if (requisition.width <= monitor.width)
4408 /* the menu is too big to fit on either side of the mouse
4409 * cursor, but smaller than the monitor. Position it on
4410 * the side that has the most space
4412 if (space_left > space_right)
4420 x = monitor.x + monitor.width - requisition.width;
4423 else /* menu is simply too big for the monitor */
4428 x = monitor.x + monitor.width - requisition.width;
4437 /* Position vertically. The algorithm is the same as above, but
4438 * simpler because we don't have to take RTL into account.
4440 needed_height = requisition.height - ythickness;
4442 if (needed_height <= space_above ||
4443 needed_height <= space_below)
4445 if (needed_height <= space_below)
4448 y = y + ythickness - requisition.height + 1;
4450 y = CLAMP (y, monitor.y,
4451 monitor.y + monitor.height - requisition.height);
4453 else if (needed_height > space_below && needed_height > space_above)
4455 if (space_below >= space_above)
4456 y = monitor.y + monitor.height - requisition.height;
4468 if (private->initially_pushed_in)
4470 menu_height = GTK_WIDGET (menu)->requisition.height;
4472 if (y + menu_height > monitor.y + monitor.height)
4474 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4475 y = (monitor.y + monitor.height) - menu_height;
4480 scroll_offset += monitor.y - y;
4485 /* FIXME: should this be done in the various position_funcs ? */
4486 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4488 if (GTK_MENU_SHELL (menu)->active)
4490 private->have_position = TRUE;
4495 if (y + requisition.height > monitor.y + monitor.height)
4496 requisition.height = (monitor.y + monitor.height) - y;
4500 scroll_offset += monitor.y - y;
4501 requisition.height -= monitor.y - y;
4505 if (scroll_offset > 0)
4507 GtkBorder arrow_border;
4509 get_arrows_border (menu, &arrow_border);
4510 scroll_offset += arrow_border.top;
4513 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
4516 if (!GTK_MENU_SHELL (menu)->active)
4518 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4519 requisition.width, requisition.height);
4522 menu->scroll_offset = scroll_offset;
4526 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4528 if (menu->timeout_id)
4530 g_source_remove (menu->timeout_id);
4531 menu->timeout_id = 0;
4536 gtk_menu_stop_scrolling (GtkMenu *menu)
4538 gboolean touchscreen_mode;
4540 gtk_menu_remove_scroll_timeout (menu);
4542 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4543 "gtk-touchscreen-mode", &touchscreen_mode,
4546 if (!touchscreen_mode)
4548 menu->upper_arrow_prelight = FALSE;
4549 menu->lower_arrow_prelight = FALSE;
4554 gtk_menu_scroll_to (GtkMenu *menu,
4559 gint view_width, view_height;
4562 guint vertical_padding;
4563 guint horizontal_padding;
4564 gboolean double_arrows;
4565 GtkBorder arrow_border;
4567 widget = GTK_WIDGET (menu);
4569 if (menu->tearoff_active &&
4570 menu->tearoff_adjustment &&
4571 (menu->tearoff_adjustment->value != offset))
4573 menu->tearoff_adjustment->value =
4575 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4576 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4579 /* Move/resize the viewport according to arrows: */
4580 view_width = widget->allocation.width;
4581 view_height = widget->allocation.height;
4583 gtk_widget_style_get (GTK_WIDGET (menu),
4584 "vertical-padding", &vertical_padding,
4585 "horizontal-padding", &horizontal_padding,
4588 double_arrows = get_double_arrows (menu);
4590 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4591 view_width -= (border_width + widget->style->xthickness + horizontal_padding) * 2;
4592 view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
4593 menu_height = widget->requisition.height -
4594 (border_width + widget->style->ythickness + vertical_padding) * 2;
4596 x = border_width + widget->style->xthickness + horizontal_padding;
4597 y = border_width + widget->style->ythickness + vertical_padding;
4599 if (double_arrows && !menu->tearoff_active)
4601 if (view_height < menu_height ||
4602 (offset > 0 && menu->scroll_offset > 0) ||
4603 (offset < 0 && menu->scroll_offset < 0))
4605 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4606 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4607 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4609 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4610 gtk_widget_queue_draw (GTK_WIDGET (menu));
4612 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4614 get_arrows_border (menu, &arrow_border);
4615 y += arrow_border.top;
4616 view_height -= arrow_border.top;
4617 view_height -= arrow_border.bottom;
4620 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4621 else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4622 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4623 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4625 if (offset >= menu_height - view_height)
4626 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4627 else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4628 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4629 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4631 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4632 (priv->lower_arrow_state != lower_arrow_previous_state))
4633 gtk_widget_queue_draw (GTK_WIDGET (menu));
4635 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4636 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4638 /* At the upper border, possibly remove timeout */
4639 if (menu->scroll_step < 0)
4641 gtk_menu_stop_scrolling (menu);
4642 gtk_widget_queue_draw (GTK_WIDGET (menu));
4646 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4647 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4649 /* At the lower border, possibly remove timeout */
4650 if (menu->scroll_step > 0)
4652 gtk_menu_stop_scrolling (menu);
4653 gtk_widget_queue_draw (GTK_WIDGET (menu));
4657 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4661 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4662 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4664 gtk_menu_stop_scrolling (menu);
4665 gtk_widget_queue_draw (GTK_WIDGET (menu));
4668 else if (!menu->tearoff_active)
4670 gboolean last_visible;
4672 last_visible = menu->upper_arrow_visible;
4673 menu->upper_arrow_visible = offset > 0;
4675 /* upper_arrow_visible may have changed, so requery the border */
4676 get_arrows_border (menu, &arrow_border);
4677 view_height -= arrow_border.top;
4679 if ((last_visible != menu->upper_arrow_visible) &&
4680 !menu->upper_arrow_visible)
4682 menu->upper_arrow_prelight = FALSE;
4684 /* If we hid the upper arrow, possibly remove timeout */
4685 if (menu->scroll_step < 0)
4687 gtk_menu_stop_scrolling (menu);
4688 gtk_widget_queue_draw (GTK_WIDGET (menu));
4692 last_visible = menu->lower_arrow_visible;
4693 menu->lower_arrow_visible = offset < menu_height - view_height;
4695 /* lower_arrow_visible may have changed, so requery the border */
4696 get_arrows_border (menu, &arrow_border);
4697 view_height -= arrow_border.bottom;
4699 if ((last_visible != menu->lower_arrow_visible) &&
4700 !menu->lower_arrow_visible)
4702 menu->lower_arrow_prelight = FALSE;
4704 /* If we hid the lower arrow, possibly remove timeout */
4705 if (menu->scroll_step > 0)
4707 gtk_menu_stop_scrolling (menu);
4708 gtk_widget_queue_draw (GTK_WIDGET (menu));
4712 y += arrow_border.top;
4715 /* Scroll the menu: */
4716 if (gtk_widget_get_realized (widget))
4717 gdk_window_move (menu->bin_window, 0, -offset);
4719 if (gtk_widget_get_realized (widget))
4720 gdk_window_move_resize (menu->view_window,
4726 menu->scroll_offset = offset;
4730 compute_child_offset (GtkMenu *menu,
4731 GtkWidget *menu_item,
4734 gboolean *is_last_child)
4736 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4737 gint item_top_attach;
4738 gint item_bottom_attach;
4739 gint child_offset = 0;
4742 get_effective_child_attach (menu_item, NULL, NULL,
4743 &item_top_attach, &item_bottom_attach);
4745 /* there is a possibility that we get called before _size_request, so
4746 * check the height table for safety.
4748 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4751 /* when we have a row with only invisible children, it's height will
4752 * be zero, so there's no need to check WIDGET_VISIBLE here
4754 for (i = 0; i < item_top_attach; i++)
4755 child_offset += priv->heights[i];
4758 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4760 *offset = child_offset;
4762 *height = priv->heights[item_top_attach];
4768 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4769 GtkWidget *menu_item)
4772 gint child_offset, child_height;
4776 gboolean last_child = 0;
4778 menu = GTK_MENU (menu_shell);
4780 /* We need to check if the selected item fully visible.
4781 * If not we need to scroll the menu so that it becomes fully
4785 if (compute_child_offset (menu, menu_item,
4786 &child_offset, &child_height, &last_child))
4788 guint vertical_padding;
4789 gboolean double_arrows;
4791 y = menu->scroll_offset;
4792 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
4794 gtk_widget_style_get (GTK_WIDGET (menu),
4795 "vertical-padding", &vertical_padding,
4798 double_arrows = get_double_arrows (menu);
4800 height -= 2*gtk_container_get_border_width (GTK_CONTAINER (menu)) + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
4802 if (child_offset < y)
4804 /* Ignore the enter event we might get if the pointer is on the menu
4806 menu_shell->ignore_enter = TRUE;
4807 gtk_menu_scroll_to (menu, child_offset);
4811 GtkBorder arrow_border;
4815 get_arrows_border (menu, &arrow_border);
4816 if (!menu->tearoff_active)
4817 arrow_height = arrow_border.top + arrow_border.bottom;
4819 if (child_offset + child_height > y + height - arrow_height)
4822 if ((!last_child && !menu->tearoff_active) || double_arrows)
4823 arrow_height += arrow_border.bottom;
4825 y = child_offset + child_height - height + arrow_height;
4826 if (((y > 0) && !menu->tearoff_active) || double_arrows)
4828 /* Need upper arrow */
4829 arrow_height += arrow_border.top;
4830 y = child_offset + child_height - height + arrow_height;
4832 /* Ignore the enter event we might get if the pointer is on the menu
4834 menu_shell->ignore_enter = TRUE;
4835 gtk_menu_scroll_to (menu, y);
4843 gtk_menu_select_item (GtkMenuShell *menu_shell,
4844 GtkWidget *menu_item)
4846 GtkMenu *menu = GTK_MENU (menu_shell);
4848 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4849 gtk_menu_scroll_item_visible (menu_shell, menu_item);
4851 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
4855 /* Reparent the menu, taking care of the refcounting
4857 * If unrealize is true we force a unrealize while reparenting the parent.
4858 * This can help eliminate flicker in some cases.
4860 * What happens is that when the menu is unrealized and then re-realized,
4861 * the allocations are as follows:
4863 * parent - 1x1 at (0,0)
4864 * child1 - 100x20 at (0,0)
4865 * child2 - 100x20 at (0,20)
4866 * child3 - 100x20 at (0,40)
4868 * That is, the parent is small but the children are full sized. Then,
4869 * when the queued_resize gets processed, the parent gets resized to
4872 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
4873 * contains the following logic:
4875 * - if a move or resize operation on a window would change the clip
4876 * region on the children, then before the window is resized
4877 * the background for children is temporarily set to None, the
4878 * move/resize done, and the background for the children restored.
4880 * So, at the point where the parent is resized to final size, the
4881 * background for the children is temporarily None, and thus they
4882 * are not cleared to the background color and the previous background
4883 * (the image of the menu) is left in place.
4886 gtk_menu_reparent (GtkMenu *menu,
4887 GtkWidget *new_parent,
4890 GtkObject *object = GTK_OBJECT (menu);
4891 GtkWidget *widget = GTK_WIDGET (menu);
4892 gboolean was_floating = g_object_is_floating (object);
4894 g_object_ref_sink (object);
4898 g_object_ref (object);
4899 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
4900 gtk_container_add (GTK_CONTAINER (new_parent), widget);
4901 g_object_unref (object);
4904 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
4907 g_object_force_floating (G_OBJECT (object));
4909 g_object_unref (object);
4913 gtk_menu_show_all (GtkWidget *widget)
4915 /* Show children, but not self. */
4916 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
4921 gtk_menu_hide_all (GtkWidget *widget)
4923 /* Hide children, but not self. */
4924 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
4928 * gtk_menu_set_screen:
4929 * @menu: a #GtkMenu.
4930 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
4931 * determined by the widget the menu is attached to.
4933 * Sets the #GdkScreen on which the menu will be displayed.
4938 gtk_menu_set_screen (GtkMenu *menu,
4941 g_return_if_fail (GTK_IS_MENU (menu));
4942 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
4944 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
4948 menu_change_screen (menu, screen);
4952 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
4954 attach_widget_screen_changed (attach_widget, NULL, menu);
4960 * @menu: a #GtkMenu.
4961 * @child: a #GtkMenuItem.
4962 * @left_attach: The column number to attach the left side of the item to.
4963 * @right_attach: The column number to attach the right side of the item to.
4964 * @top_attach: The row number to attach the top of the item to.
4965 * @bottom_attach: The row number to attach the bottom of the item to.
4967 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
4968 * an item will occupy is specified by @left_attach, @right_attach,
4969 * @top_attach and @bottom_attach. These each represent the leftmost,
4970 * rightmost, uppermost and lower column and row numbers of the table.
4971 * (Columns and rows are indexed from zero).
4973 * Note that this function is not related to gtk_menu_detach().
4978 gtk_menu_attach (GtkMenu *menu,
4983 guint bottom_attach)
4985 GtkMenuShell *menu_shell;
4987 g_return_if_fail (GTK_IS_MENU (menu));
4988 g_return_if_fail (GTK_IS_MENU_ITEM (child));
4989 g_return_if_fail (child->parent == NULL ||
4990 child->parent == GTK_WIDGET (menu));
4991 g_return_if_fail (left_attach < right_attach);
4992 g_return_if_fail (top_attach < bottom_attach);
4994 menu_shell = GTK_MENU_SHELL (menu);
4998 AttachInfo *ai = get_attach_info (child);
5000 ai->left_attach = left_attach;
5001 ai->right_attach = right_attach;
5002 ai->top_attach = top_attach;
5003 ai->bottom_attach = bottom_attach;
5005 menu_shell->children = g_list_append (menu_shell->children, child);
5007 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5009 menu_queue_resize (menu);
5013 gtk_container_child_set (GTK_CONTAINER (child->parent), child,
5014 "left-attach", left_attach,
5015 "right-attach", right_attach,
5016 "top-attach", top_attach,
5017 "bottom-attach", bottom_attach,
5023 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5027 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5028 "gtk-menu-popup-delay", &popup_delay,
5035 find_child_containing (GtkMenuShell *menu_shell,
5043 /* find a child which includes the area given by
5044 * left, right, top, bottom.
5047 for (list = menu_shell->children; list; list = list->next)
5051 if (!_gtk_menu_item_is_selectable (list->data))
5054 get_effective_child_attach (list->data, &l, &r, &t, &b);
5056 if (l <= left && right <= r
5057 && t <= top && bottom <= b)
5058 return GTK_WIDGET (list->data);
5065 gtk_menu_move_current (GtkMenuShell *menu_shell,
5066 GtkMenuDirectionType direction)
5068 GtkMenu *menu = GTK_MENU (menu_shell);
5071 GtkWidget *match = NULL;
5073 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5077 case GTK_MENU_DIR_CHILD:
5078 direction = GTK_MENU_DIR_PARENT;
5080 case GTK_MENU_DIR_PARENT:
5081 direction = GTK_MENU_DIR_CHILD;
5087 /* use special table menu key bindings */
5088 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5090 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
5092 if (direction == GTK_MENU_DIR_NEXT)
5094 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5096 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5104 for (i = 0; i < t; i++)
5106 match = find_child_containing (menu_shell,
5107 l, l + 1, i, i + 1);
5113 else if (direction == GTK_MENU_DIR_PREV)
5115 for (i = t; i > 0; i--)
5117 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
5125 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5127 match = find_child_containing (menu_shell,
5128 l, l + 1, i - 1, i);
5134 else if (direction == GTK_MENU_DIR_PARENT)
5136 /* we go one left if possible */
5138 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
5142 GtkWidget *parent = menu_shell->parent_menu_shell;
5145 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
5146 match = menu_shell->active_menu_item;
5149 else if (direction == GTK_MENU_DIR_CHILD)
5151 /* we go one right if possible */
5152 if (r < gtk_menu_get_n_columns (menu))
5153 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5157 GtkWidget *parent = menu_shell->parent_menu_shell;
5159 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
5161 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
5162 match = menu_shell->active_menu_item;
5168 gtk_menu_shell_select_item (menu_shell, match);
5173 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5177 get_visible_size (GtkMenu *menu)
5179 GtkWidget *widget = GTK_WIDGET (menu);
5180 GtkContainer *container = GTK_CONTAINER (menu);
5182 gint menu_height = (widget->allocation.height
5183 - 2 * (gtk_container_get_border_width (container)
5184 + widget->style->ythickness));
5186 if (!menu->tearoff_active)
5188 GtkBorder arrow_border;
5190 get_arrows_border (menu, &arrow_border);
5191 menu_height -= arrow_border.top;
5192 menu_height -= arrow_border.bottom;
5198 /* Find the sensitive on-screen child containing @y, or if none,
5199 * the nearest selectable onscreen child. (%NULL if none)
5202 child_at (GtkMenu *menu,
5205 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5206 GtkWidget *child = NULL;
5207 gint child_offset = 0;
5210 gint lower, upper; /* Onscreen bounds */
5212 menu_height = get_visible_size (menu);
5213 lower = menu->scroll_offset;
5214 upper = menu->scroll_offset + menu_height;
5216 for (children = menu_shell->children; children; children = children->next)
5218 if (gtk_widget_get_visible (children->data))
5220 GtkRequisition child_requisition;
5222 gtk_widget_size_request (children->data, &child_requisition);
5224 if (_gtk_menu_item_is_selectable (children->data) &&
5225 child_offset >= lower &&
5226 child_offset + child_requisition.height <= upper)
5228 child = children->data;
5230 if (child_offset + child_requisition.height > y &&
5231 !GTK_IS_TEAROFF_MENU_ITEM (child))
5235 child_offset += child_requisition.height;
5243 get_menu_height (GtkMenu *menu)
5246 GtkWidget *widget = GTK_WIDGET (menu);
5248 height = widget->requisition.height;
5249 height -= gtk_container_get_border_width (GTK_CONTAINER (widget) + widget->style->ythickness) * 2;
5251 if (!menu->tearoff_active)
5253 GtkBorder arrow_border;
5255 get_arrows_border (menu, &arrow_border);
5256 height -= arrow_border.top;
5257 height -= arrow_border.bottom;
5264 gtk_menu_real_move_scroll (GtkMenu *menu,
5267 gint page_size = get_visible_size (menu);
5268 gint end_position = get_menu_height (menu);
5269 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5273 case GTK_SCROLL_PAGE_UP:
5274 case GTK_SCROLL_PAGE_DOWN:
5278 gint child_offset = 0;
5279 gboolean old_upper_arrow_visible;
5282 if (type == GTK_SCROLL_PAGE_UP)
5287 if (menu_shell->active_menu_item)
5291 compute_child_offset (menu, menu_shell->active_menu_item,
5292 &child_offset, &child_height, NULL);
5293 child_offset += child_height / 2;
5296 menu_shell->ignore_enter = TRUE;
5297 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5298 old_offset = menu->scroll_offset;
5300 new_offset = menu->scroll_offset + step;
5301 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5303 gtk_menu_scroll_to (menu, new_offset);
5305 if (menu_shell->active_menu_item)
5307 GtkWidget *new_child;
5308 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5309 GtkBorder arrow_border;
5311 get_arrows_border (menu, &arrow_border);
5313 if (menu->scroll_offset != old_offset)
5314 step = menu->scroll_offset - old_offset;
5316 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5318 new_child = child_at (menu, child_offset + step);
5320 gtk_menu_shell_select_item (menu_shell, new_child);
5324 case GTK_SCROLL_START:
5325 /* Ignore the enter event we might get if the pointer is on the menu
5327 menu_shell->ignore_enter = TRUE;
5328 gtk_menu_scroll_to (menu, 0);
5329 gtk_menu_shell_select_first (menu_shell, TRUE);
5331 case GTK_SCROLL_END:
5332 /* Ignore the enter event we might get if the pointer is on the menu
5334 menu_shell->ignore_enter = TRUE;
5335 gtk_menu_scroll_to (menu, end_position - page_size);
5336 _gtk_menu_shell_select_last (menu_shell, TRUE);
5345 * gtk_menu_set_monitor:
5347 * @monitor_num: the number of the monitor on which the menu should
5350 * Informs GTK+ on which monitor a menu should be popped up.
5351 * See gdk_screen_get_monitor_geometry().
5353 * This function should be called from a #GtkMenuPositionFunc if the
5354 * menu should not appear on the same monitor as the pointer. This
5355 * information can't be reliably inferred from the coordinates returned
5356 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
5357 * may extend beyond the monitor boundaries or even the screen boundaries.
5362 gtk_menu_set_monitor (GtkMenu *menu,
5365 GtkMenuPrivate *priv;
5366 g_return_if_fail (GTK_IS_MENU (menu));
5368 priv = gtk_menu_get_private (menu);
5370 priv->monitor_num = monitor_num;
5374 * gtk_menu_get_monitor:
5377 * Retrieves the number of the monitor on which to show the menu.
5379 * Returns: the number of the monitor on which the menu should
5380 * be popped up or -1, if no monitor has been set
5385 gtk_menu_get_monitor (GtkMenu *menu)
5387 GtkMenuPrivate *priv;
5388 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5390 priv = gtk_menu_get_private (menu);
5392 return priv->monitor_num;
5396 * gtk_menu_get_for_attach_widget:
5397 * @widget: a #GtkWidget
5399 * Returns a list of the menus which are attached to this widget.
5400 * This list is owned by GTK+ and must not be modified.
5402 * Return value: (element-type GtkWidget) (transfer none): the list of menus attached to his widget.
5407 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5411 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5413 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5418 gtk_menu_grab_notify (GtkWidget *widget,
5419 gboolean was_grabbed)
5421 GtkWidget *toplevel;
5422 GtkWindowGroup *group;
5426 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5429 !gtk_widget_device_is_shadowed (widget, pointer))
5432 toplevel = gtk_widget_get_toplevel (widget);
5434 if (!GTK_IS_WINDOW (toplevel))
5437 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5438 grab = gtk_window_group_get_current_device_grab (group, pointer);
5440 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
5441 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5445 * gtk_menu_set_reserve_toggle_size:
5447 * @reserve_toggle_size: whether to reserve size for toggles
5449 * Sets whether the menu should reserve space for drawing toggles
5450 * or icons, regardless of their actual presence.
5455 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5456 gboolean reserve_toggle_size)
5458 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5459 gboolean no_toggle_size;
5461 no_toggle_size = !reserve_toggle_size;
5463 if (priv->no_toggle_size != no_toggle_size)
5465 priv->no_toggle_size = no_toggle_size;
5467 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5472 * gtk_menu_get_reserve_toggle_size:
5475 * Returns whether the menu reserves space for toggles and
5476 * icons, regardless of their actual presence.
5478 * Returns: Whether the menu reserves toggle space
5483 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5485 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5487 return !priv->no_toggle_size;