1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gdk/gdkkeysyms.h"
30 #include "gtkaccellabel.h"
31 #include "gtkaccelmap.h"
32 #include "gtkbindings.h"
33 #include "gtkcheckmenuitem.h"
34 #include <gobject/gvaluecollector.h>
36 #include "gtkmarshalers.h"
38 #include "gtkmenuprivate.h"
39 #include "gtktearoffmenuitem.h"
40 #include "gtkwindow.h"
42 #include "gtkvscrollbar.h"
43 #include "gtksettings.h"
44 #include "gtkprivate.h"
47 #define DEFAULT_POPUP_DELAY 225
48 #define DEFAULT_POPDOWN_DELAY 1000
50 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
51 * extends below the submenu
54 #define MENU_SCROLL_STEP1 8
55 #define MENU_SCROLL_STEP2 15
56 #define MENU_SCROLL_FAST_ZONE 8
57 #define MENU_SCROLL_TIMEOUT1 50
58 #define MENU_SCROLL_TIMEOUT2 20
60 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
61 #define ATTACHED_MENUS "gtk-attached-menus"
63 typedef struct _GtkMenuAttachData GtkMenuAttachData;
64 typedef struct _GtkMenuPrivate GtkMenuPrivate;
65 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
67 struct _GtkMenuAttachData
69 GtkWidget *attach_widget;
70 GtkMenuDetachFunc detacher;
73 struct _GtkMenuPrivate
77 gboolean initially_pushed_in;
79 GDestroyNotify position_func_data_destroy;
81 /* info used for the table */
84 gint requested_height;
88 /* Cached layout information */
97 GtkStateType lower_arrow_state;
98 GtkStateType upper_arrow_state;
100 /* navigation region */
103 int navigation_width;
104 int navigation_height;
106 guint have_layout : 1;
107 guint seen_item_enter : 1;
108 guint have_position : 1;
109 guint ignore_button_release : 1;
110 guint no_toggle_size : 1;
113 struct _GtkMenuPopdownData
125 gint effective_left_attach;
126 gint effective_right_attach;
127 gint effective_top_attach;
128 gint effective_bottom_attach;
145 PROP_RESERVE_TOGGLE_SIZE
150 CHILD_PROP_LEFT_ATTACH,
151 CHILD_PROP_RIGHT_ATTACH,
152 CHILD_PROP_TOP_ATTACH,
153 CHILD_PROP_BOTTOM_ATTACH
156 static void gtk_menu_set_property (GObject *object,
160 static void gtk_menu_get_property (GObject *object,
164 static void gtk_menu_set_child_property(GtkContainer *container,
169 static void gtk_menu_get_child_property(GtkContainer *container,
174 static void gtk_menu_destroy (GtkObject *object);
175 static void gtk_menu_realize (GtkWidget *widget);
176 static void gtk_menu_unrealize (GtkWidget *widget);
177 static void gtk_menu_size_allocate (GtkWidget *widget,
178 GtkAllocation *allocation);
179 static void gtk_menu_show (GtkWidget *widget);
180 static gboolean gtk_menu_draw (GtkWidget *widget,
182 static gboolean gtk_menu_key_press (GtkWidget *widget,
184 static gboolean gtk_menu_scroll (GtkWidget *widget,
185 GdkEventScroll *event);
186 static gboolean gtk_menu_button_press (GtkWidget *widget,
187 GdkEventButton *event);
188 static gboolean gtk_menu_button_release (GtkWidget *widget,
189 GdkEventButton *event);
190 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
191 GdkEventMotion *event);
192 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
193 GdkEventCrossing *event);
194 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
195 GdkEventCrossing *event);
196 static void gtk_menu_scroll_to (GtkMenu *menu,
198 static void gtk_menu_grab_notify (GtkWidget *widget,
199 gboolean was_grabbed);
201 static void gtk_menu_stop_scrolling (GtkMenu *menu);
202 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
203 static gboolean gtk_menu_scroll_timeout (gpointer data);
204 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
205 static void gtk_menu_start_scrolling (GtkMenu *menu);
207 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
208 GtkWidget *menu_item);
209 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
210 GtkWidget *menu_item);
211 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
214 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
216 static void gtk_menu_handle_scrolling (GtkMenu *menu,
221 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
223 static void gtk_menu_style_set (GtkWidget *widget,
224 GtkStyle *previous_style);
225 static gboolean gtk_menu_focus (GtkWidget *widget,
226 GtkDirectionType direction);
227 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
228 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
229 GtkMenuDirectionType direction);
230 static void gtk_menu_real_move_scroll (GtkMenu *menu,
233 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
234 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
235 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
238 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
239 GtkMenuItem *menu_item,
240 GdkEventCrossing *event);
242 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
243 static void gtk_menu_show_all (GtkWidget *widget);
244 static void gtk_menu_hide_all (GtkWidget *widget);
245 static void gtk_menu_position (GtkMenu *menu);
246 static void gtk_menu_reparent (GtkMenu *menu,
247 GtkWidget *new_parent,
249 static void gtk_menu_remove (GtkContainer *menu,
252 static void gtk_menu_update_title (GtkMenu *menu);
254 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
255 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
257 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
259 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
260 gboolean group_changed);
262 static void gtk_menu_get_preferred_width (GtkWidget *widget,
265 static void gtk_menu_get_preferred_height (GtkWidget *widget,
268 static void gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
274 static const gchar attach_data_key[] = "gtk-menu-attach-data";
276 static guint menu_signals[LAST_SIGNAL] = { 0 };
278 static GtkMenuPrivate *
279 gtk_menu_get_private (GtkMenu *menu)
281 return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
284 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
287 menu_queue_resize (GtkMenu *menu)
289 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
291 priv->have_layout = FALSE;
292 gtk_widget_queue_resize (GTK_WIDGET (menu));
296 attach_info_free (AttachInfo *info)
298 g_slice_free (AttachInfo, info);
302 get_attach_info (GtkWidget *child)
304 GObject *object = G_OBJECT (child);
305 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
309 ai = g_slice_new0 (AttachInfo);
310 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
311 (GDestroyNotify) attach_info_free);
318 is_grid_attached (AttachInfo *ai)
320 return (ai->left_attach >= 0 &&
321 ai->right_attach >= 0 &&
322 ai->top_attach >= 0 &&
323 ai->bottom_attach >= 0);
327 menu_ensure_layout (GtkMenu *menu)
329 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
331 if (!priv->have_layout)
333 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
337 gint max_right_attach;
338 gint max_bottom_attach;
340 /* Find extents of gridded portion
342 max_right_attach = 1;
343 max_bottom_attach = 0;
345 for (l = menu_shell->children; l; l = l->next)
347 GtkWidget *child = l->data;
348 AttachInfo *ai = get_attach_info (child);
350 if (is_grid_attached (ai))
352 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
353 max_right_attach = MAX (max_right_attach, ai->right_attach);
359 row_occupied = g_malloc0 (max_bottom_attach);
361 for (l = menu_shell->children; l; l = l->next)
363 GtkWidget *child = l->data;
364 AttachInfo *ai = get_attach_info (child);
366 if (is_grid_attached (ai))
370 for (i = ai->top_attach; i < ai->bottom_attach; i++)
371 row_occupied[i] = TRUE;
375 /* Lay non-grid-items out in those rows
378 for (l = menu_shell->children; l; l = l->next)
380 GtkWidget *child = l->data;
381 AttachInfo *ai = get_attach_info (child);
383 if (!is_grid_attached (ai))
385 while (current_row < max_bottom_attach && row_occupied[current_row])
388 ai->effective_left_attach = 0;
389 ai->effective_right_attach = max_right_attach;
390 ai->effective_top_attach = current_row;
391 ai->effective_bottom_attach = current_row + 1;
397 ai->effective_left_attach = ai->left_attach;
398 ai->effective_right_attach = ai->right_attach;
399 ai->effective_top_attach = ai->top_attach;
400 ai->effective_bottom_attach = ai->bottom_attach;
404 g_free (row_occupied);
406 priv->n_rows = MAX (current_row, max_bottom_attach);
407 priv->n_columns = max_right_attach;
408 priv->have_layout = TRUE;
414 gtk_menu_get_n_columns (GtkMenu *menu)
416 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
418 menu_ensure_layout (menu);
420 return priv->n_columns;
424 gtk_menu_get_n_rows (GtkMenu *menu)
426 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
428 menu_ensure_layout (menu);
434 get_effective_child_attach (GtkWidget *child,
440 GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
443 menu_ensure_layout (menu);
445 ai = get_attach_info (child);
448 *l = ai->effective_left_attach;
450 *r = ai->effective_right_attach;
452 *t = ai->effective_top_attach;
454 *b = ai->effective_bottom_attach;
459 gtk_menu_class_init (GtkMenuClass *class)
461 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
462 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
463 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
464 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
465 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
466 GtkBindingSet *binding_set;
468 gobject_class->set_property = gtk_menu_set_property;
469 gobject_class->get_property = gtk_menu_get_property;
471 object_class->destroy = gtk_menu_destroy;
473 widget_class->realize = gtk_menu_realize;
474 widget_class->unrealize = gtk_menu_unrealize;
475 widget_class->size_allocate = gtk_menu_size_allocate;
476 widget_class->show = gtk_menu_show;
477 widget_class->draw = gtk_menu_draw;
478 widget_class->scroll_event = gtk_menu_scroll;
479 widget_class->key_press_event = gtk_menu_key_press;
480 widget_class->button_press_event = gtk_menu_button_press;
481 widget_class->button_release_event = gtk_menu_button_release;
482 widget_class->motion_notify_event = gtk_menu_motion_notify;
483 widget_class->show_all = gtk_menu_show_all;
484 widget_class->hide_all = gtk_menu_hide_all;
485 widget_class->enter_notify_event = gtk_menu_enter_notify;
486 widget_class->leave_notify_event = gtk_menu_leave_notify;
487 widget_class->style_set = gtk_menu_style_set;
488 widget_class->focus = gtk_menu_focus;
489 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
490 widget_class->grab_notify = gtk_menu_grab_notify;
491 widget_class->get_preferred_width = gtk_menu_get_preferred_width;
492 widget_class->get_preferred_height = gtk_menu_get_preferred_height;
493 widget_class->get_preferred_height_for_width = gtk_menu_get_preferred_height_for_width;
495 container_class->remove = gtk_menu_remove;
496 container_class->get_child_property = gtk_menu_get_child_property;
497 container_class->set_child_property = gtk_menu_set_child_property;
499 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
500 menu_shell_class->deactivate = gtk_menu_deactivate;
501 menu_shell_class->select_item = gtk_menu_select_item;
502 menu_shell_class->insert = gtk_menu_real_insert;
503 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
504 menu_shell_class->move_current = gtk_menu_move_current;
506 menu_signals[MOVE_SCROLL] =
507 g_signal_new_class_handler (I_("move-scroll"),
508 G_OBJECT_CLASS_TYPE (object_class),
509 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
510 G_CALLBACK (gtk_menu_real_move_scroll),
512 _gtk_marshal_VOID__ENUM,
514 GTK_TYPE_SCROLL_TYPE);
519 * The index of the currently selected menu item, or -1 if no
520 * menu item is selected.
524 g_object_class_install_property (gobject_class,
526 g_param_spec_int ("active",
528 P_("The currently selected menu item"),
530 GTK_PARAM_READWRITE));
533 * GtkMenu:accel-group:
535 * The accel group holding accelerators for the menu.
539 g_object_class_install_property (gobject_class,
541 g_param_spec_object ("accel-group",
543 P_("The accel group holding accelerators for the menu"),
544 GTK_TYPE_ACCEL_GROUP,
545 GTK_PARAM_READWRITE));
548 * GtkMenu:accel-path:
550 * An accel path used to conveniently construct accel paths of child items.
554 g_object_class_install_property (gobject_class,
556 g_param_spec_string ("accel-path",
558 P_("An accel path used to conveniently construct accel paths of child items"),
560 GTK_PARAM_READWRITE));
563 * GtkMenu:attach-widget:
565 * The widget the menu is attached to. Setting this property attaches
566 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
567 * use gtk_menu_attach_to_widget() directly.
571 g_object_class_install_property (gobject_class,
573 g_param_spec_object ("attach-widget",
575 P_("The widget the menu is attached to"),
577 GTK_PARAM_READWRITE));
579 g_object_class_install_property (gobject_class,
581 g_param_spec_string ("tearoff-title",
583 P_("A title that may be displayed by the window manager when this menu is torn-off"),
585 GTK_PARAM_READWRITE));
588 * GtkMenu:tearoff-state:
590 * A boolean that indicates whether the menu is torn-off.
594 g_object_class_install_property (gobject_class,
596 g_param_spec_boolean ("tearoff-state",
598 P_("A boolean that indicates whether the menu is torn-off"),
600 GTK_PARAM_READWRITE));
605 * The monitor the menu will be popped up on.
609 g_object_class_install_property (gobject_class,
611 g_param_spec_int ("monitor",
613 P_("The monitor the menu will be popped up on"),
615 GTK_PARAM_READWRITE));
617 gtk_widget_class_install_style_property (widget_class,
618 g_param_spec_int ("vertical-padding",
619 P_("Vertical Padding"),
620 P_("Extra space at the top and bottom of the menu"),
624 GTK_PARAM_READABLE));
627 * GtkMenu:reserve-toggle-size:
629 * A boolean that indicates whether the menu reserves space for
630 * toggles and icons, regardless of their actual presence.
632 * This property should only be changed from its default value
633 * for special-purposes such as tabular menus. Regular menus that
634 * are connected to a menu bar or context menus should reserve
635 * toggle space for consistency.
639 g_object_class_install_property (gobject_class,
640 PROP_RESERVE_TOGGLE_SIZE,
641 g_param_spec_boolean ("reserve-toggle-size",
642 P_("Reserve Toggle Size"),
643 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
645 GTK_PARAM_READWRITE));
647 gtk_widget_class_install_style_property (widget_class,
648 g_param_spec_int ("horizontal-padding",
649 P_("Horizontal Padding"),
650 P_("Extra space at the left and right edges of the menu"),
654 GTK_PARAM_READABLE));
656 gtk_widget_class_install_style_property (widget_class,
657 g_param_spec_int ("vertical-offset",
658 P_("Vertical Offset"),
659 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
663 GTK_PARAM_READABLE));
665 gtk_widget_class_install_style_property (widget_class,
666 g_param_spec_int ("horizontal-offset",
667 P_("Horizontal Offset"),
668 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
672 GTK_PARAM_READABLE));
674 gtk_widget_class_install_style_property (widget_class,
675 g_param_spec_boolean ("double-arrows",
677 P_("When scrolling, always show both arrows."),
679 GTK_PARAM_READABLE));
682 * GtkMenu:arrow-placement:
684 * Indicates where scroll arrows should be placed.
688 gtk_widget_class_install_style_property (widget_class,
689 g_param_spec_enum ("arrow-placement",
690 P_("Arrow Placement"),
691 P_("Indicates where scroll arrows should be placed"),
692 GTK_TYPE_ARROW_PLACEMENT,
694 GTK_PARAM_READABLE));
696 gtk_container_class_install_child_property (container_class,
697 CHILD_PROP_LEFT_ATTACH,
698 g_param_spec_int ("left-attach",
700 P_("The column number to attach the left side of the child to"),
702 GTK_PARAM_READWRITE));
704 gtk_container_class_install_child_property (container_class,
705 CHILD_PROP_RIGHT_ATTACH,
706 g_param_spec_int ("right-attach",
708 P_("The column number to attach the right side of the child to"),
710 GTK_PARAM_READWRITE));
712 gtk_container_class_install_child_property (container_class,
713 CHILD_PROP_TOP_ATTACH,
714 g_param_spec_int ("top-attach",
716 P_("The row number to attach the top of the child to"),
718 GTK_PARAM_READWRITE));
720 gtk_container_class_install_child_property (container_class,
721 CHILD_PROP_BOTTOM_ATTACH,
722 g_param_spec_int ("bottom-attach",
724 P_("The row number to attach the bottom of the child to"),
726 GTK_PARAM_READWRITE));
729 * GtkMenu::arrow-scaling
731 * Arbitrary constant to scale down the size of the scroll arrow.
735 gtk_widget_class_install_style_property (widget_class,
736 g_param_spec_float ("arrow-scaling",
738 P_("Arbitrary constant to scale down the size of the scroll arrow"),
740 GTK_PARAM_READABLE));
742 binding_set = gtk_binding_set_by_class (class);
743 gtk_binding_entry_add_signal (binding_set,
745 I_("move-current"), 1,
746 GTK_TYPE_MENU_DIRECTION_TYPE,
748 gtk_binding_entry_add_signal (binding_set,
751 GTK_TYPE_MENU_DIRECTION_TYPE,
753 gtk_binding_entry_add_signal (binding_set,
756 GTK_TYPE_MENU_DIRECTION_TYPE,
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,
767 GTK_MENU_DIR_PARENT);
768 gtk_binding_entry_add_signal (binding_set,
771 GTK_TYPE_MENU_DIRECTION_TYPE,
772 GTK_MENU_DIR_PARENT);
773 gtk_binding_entry_add_signal (binding_set,
776 GTK_TYPE_MENU_DIRECTION_TYPE,
778 gtk_binding_entry_add_signal (binding_set,
781 GTK_TYPE_MENU_DIRECTION_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,
803 gtk_binding_entry_add_signal (binding_set,
806 GTK_TYPE_SCROLL_TYPE,
808 gtk_binding_entry_add_signal (binding_set,
809 GDK_KEY_KP_Page_Up, 0,
811 GTK_TYPE_SCROLL_TYPE,
813 gtk_binding_entry_add_signal (binding_set,
814 GDK_KEY_Page_Down, 0,
816 GTK_TYPE_SCROLL_TYPE,
817 GTK_SCROLL_PAGE_DOWN);
818 gtk_binding_entry_add_signal (binding_set,
819 GDK_KEY_KP_Page_Down, 0,
821 GTK_TYPE_SCROLL_TYPE,
822 GTK_SCROLL_PAGE_DOWN);
824 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
825 P_("Can change accelerators"),
826 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
828 GTK_PARAM_READWRITE));
830 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
831 P_("Delay before submenus appear"),
832 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
836 GTK_PARAM_READWRITE));
838 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
839 P_("Delay before hiding a submenu"),
840 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
843 DEFAULT_POPDOWN_DELAY,
844 GTK_PARAM_READWRITE));
846 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
851 gtk_menu_set_property (GObject *object,
856 GtkMenu *menu = GTK_MENU (object);
861 gtk_menu_set_active (menu, g_value_get_int (value));
863 case PROP_ACCEL_GROUP:
864 gtk_menu_set_accel_group (menu, g_value_get_object (value));
866 case PROP_ACCEL_PATH:
867 gtk_menu_set_accel_path (menu, g_value_get_string (value));
869 case PROP_ATTACH_WIDGET:
873 widget = gtk_menu_get_attach_widget (menu);
875 gtk_menu_detach (menu);
877 widget = (GtkWidget*) g_value_get_object (value);
879 gtk_menu_attach_to_widget (menu, widget, NULL);
882 case PROP_TEAROFF_STATE:
883 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
885 case PROP_TEAROFF_TITLE:
886 gtk_menu_set_title (menu, g_value_get_string (value));
889 gtk_menu_set_monitor (menu, g_value_get_int (value));
891 case PROP_RESERVE_TOGGLE_SIZE:
892 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
895 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
901 gtk_menu_get_property (GObject *object,
906 GtkMenu *menu = GTK_MENU (object);
911 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->children, gtk_menu_get_active (menu)));
913 case PROP_ACCEL_GROUP:
914 g_value_set_object (value, gtk_menu_get_accel_group (menu));
916 case PROP_ACCEL_PATH:
917 g_value_set_string (value, gtk_menu_get_accel_path (menu));
919 case PROP_ATTACH_WIDGET:
920 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
922 case PROP_TEAROFF_STATE:
923 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
925 case PROP_TEAROFF_TITLE:
926 g_value_set_string (value, gtk_menu_get_title (menu));
929 g_value_set_int (value, gtk_menu_get_monitor (menu));
931 case PROP_RESERVE_TOGGLE_SIZE:
932 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
935 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
941 gtk_menu_set_child_property (GtkContainer *container,
947 GtkMenu *menu = GTK_MENU (container);
948 AttachInfo *ai = get_attach_info (child);
952 case CHILD_PROP_LEFT_ATTACH:
953 ai->left_attach = g_value_get_int (value);
955 case CHILD_PROP_RIGHT_ATTACH:
956 ai->right_attach = g_value_get_int (value);
958 case CHILD_PROP_TOP_ATTACH:
959 ai->top_attach = g_value_get_int (value);
961 case CHILD_PROP_BOTTOM_ATTACH:
962 ai->bottom_attach = g_value_get_int (value);
966 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
970 menu_queue_resize (menu);
974 gtk_menu_get_child_property (GtkContainer *container,
980 AttachInfo *ai = get_attach_info (child);
984 case CHILD_PROP_LEFT_ATTACH:
985 g_value_set_int (value, ai->left_attach);
987 case CHILD_PROP_RIGHT_ATTACH:
988 g_value_set_int (value, ai->right_attach);
990 case CHILD_PROP_TOP_ATTACH:
991 g_value_set_int (value, ai->top_attach);
993 case CHILD_PROP_BOTTOM_ATTACH:
994 g_value_set_int (value, ai->bottom_attach);
998 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1004 gtk_menu_window_event (GtkWidget *window,
1008 gboolean handled = FALSE;
1010 g_object_ref (window);
1011 g_object_ref (menu);
1013 switch (event->type)
1016 case GDK_KEY_RELEASE:
1017 handled = gtk_widget_event (menu, event);
1023 g_object_unref (window);
1024 g_object_unref (menu);
1030 gtk_menu_init (GtkMenu *menu)
1032 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
1034 menu->parent_menu_item = NULL;
1035 menu->old_active_menu_item = NULL;
1036 menu->accel_group = NULL;
1037 menu->position_func = NULL;
1038 menu->position_func_data = NULL;
1039 menu->toggle_size = 0;
1041 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1042 "type", GTK_WINDOW_POPUP,
1045 "signal::event", gtk_menu_window_event, menu,
1046 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1048 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
1049 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
1051 /* Refloat the menu, so that reference counting for the menu isn't
1052 * affected by it being a child of the toplevel
1054 g_object_force_floating (G_OBJECT (menu));
1055 menu->needs_destruction_ref_count = TRUE;
1057 menu->view_window = NULL;
1058 menu->bin_window = NULL;
1060 menu->scroll_offset = 0;
1061 menu->scroll_step = 0;
1062 menu->timeout_id = 0;
1063 menu->scroll_fast = FALSE;
1065 menu->tearoff_window = NULL;
1066 menu->tearoff_hbox = NULL;
1067 menu->torn_off = FALSE;
1068 menu->tearoff_active = FALSE;
1069 menu->tearoff_adjustment = NULL;
1070 menu->tearoff_scrollbar = NULL;
1072 menu->upper_arrow_visible = FALSE;
1073 menu->lower_arrow_visible = FALSE;
1074 menu->upper_arrow_prelight = FALSE;
1075 menu->lower_arrow_prelight = FALSE;
1077 priv->upper_arrow_state = GTK_STATE_NORMAL;
1078 priv->lower_arrow_state = GTK_STATE_NORMAL;
1080 priv->have_layout = FALSE;
1081 priv->monitor_num = -1;
1085 gtk_menu_destroy (GtkObject *object)
1087 GtkMenu *menu = GTK_MENU (object);
1088 GtkMenuAttachData *data;
1089 GtkMenuPrivate *priv;
1091 gtk_menu_remove_scroll_timeout (menu);
1093 data = g_object_get_data (G_OBJECT (object), attach_data_key);
1095 gtk_menu_detach (menu);
1097 gtk_menu_stop_navigating_submenu (menu);
1099 if (menu->old_active_menu_item)
1101 g_object_unref (menu->old_active_menu_item);
1102 menu->old_active_menu_item = NULL;
1105 /* Add back the reference count for being a child */
1106 if (menu->needs_destruction_ref_count)
1108 menu->needs_destruction_ref_count = FALSE;
1109 g_object_ref (object);
1112 if (menu->accel_group)
1114 g_object_unref (menu->accel_group);
1115 menu->accel_group = NULL;
1119 gtk_widget_destroy (menu->toplevel);
1121 if (menu->tearoff_window)
1122 gtk_widget_destroy (menu->tearoff_window);
1124 priv = gtk_menu_get_private (menu);
1128 g_free (priv->heights);
1129 priv->heights = NULL;
1134 g_free (priv->title);
1138 if (priv->position_func_data_destroy)
1140 priv->position_func_data_destroy (menu->position_func_data);
1141 menu->position_func_data = NULL;
1142 priv->position_func_data_destroy = NULL;
1145 GTK_OBJECT_CLASS (gtk_menu_parent_class)->destroy (object);
1149 menu_change_screen (GtkMenu *menu,
1150 GdkScreen *new_screen)
1152 GtkMenuPrivate *private = gtk_menu_get_private (menu);
1154 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1156 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1162 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1163 gtk_menu_position (menu);
1166 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1167 private->monitor_num = -1;
1171 attach_widget_screen_changed (GtkWidget *attach_widget,
1172 GdkScreen *previous_screen,
1175 if (gtk_widget_has_screen (attach_widget) &&
1176 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1178 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1183 gtk_menu_attach_to_widget (GtkMenu *menu,
1184 GtkWidget *attach_widget,
1185 GtkMenuDetachFunc detacher)
1187 GtkMenuAttachData *data;
1190 g_return_if_fail (GTK_IS_MENU (menu));
1191 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1193 /* keep this function in sync with gtk_widget_set_parent()
1196 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1199 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1200 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1204 g_object_ref_sink (menu);
1206 data = g_slice_new (GtkMenuAttachData);
1207 data->attach_widget = attach_widget;
1209 g_signal_connect (attach_widget, "screen-changed",
1210 G_CALLBACK (attach_widget_screen_changed), menu);
1211 attach_widget_screen_changed (attach_widget, NULL, menu);
1213 data->detacher = detacher;
1214 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1215 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1216 if (!g_list_find (list, menu))
1218 list = g_list_prepend (list, menu);
1220 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1221 (GDestroyNotify) g_list_free);
1223 if (gtk_widget_get_state (GTK_WIDGET (menu)) != GTK_STATE_NORMAL)
1224 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1226 /* we don't need to set the style here, since
1227 * we are a toplevel widget.
1230 /* Fallback title for menu comes from attach widget */
1231 gtk_menu_update_title (menu);
1233 g_object_notify (G_OBJECT (menu), "attach-widget");
1237 gtk_menu_get_attach_widget (GtkMenu *menu)
1239 GtkMenuAttachData *data;
1241 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1243 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1245 return data->attach_widget;
1250 gtk_menu_detach (GtkMenu *menu)
1252 GtkMenuAttachData *data;
1255 g_return_if_fail (GTK_IS_MENU (menu));
1257 /* keep this function in sync with gtk_widget_unparent()
1259 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1262 g_warning ("gtk_menu_detach(): menu is not attached");
1265 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1267 g_signal_handlers_disconnect_by_func (data->attach_widget,
1268 (gpointer) attach_widget_screen_changed,
1272 data->detacher (data->attach_widget, menu);
1273 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1274 list = g_list_remove (list, menu);
1276 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1277 (GDestroyNotify) g_list_free);
1279 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1281 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1282 gtk_widget_unrealize (GTK_WIDGET (menu));
1284 g_slice_free (GtkMenuAttachData, data);
1286 /* Fallback title for menu comes from attach widget */
1287 gtk_menu_update_title (menu);
1289 g_object_unref (menu);
1293 gtk_menu_remove (GtkContainer *container,
1296 GtkMenu *menu = GTK_MENU (container);
1298 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1300 /* Clear out old_active_menu_item if it matches the item we are removing
1302 if (menu->old_active_menu_item == widget)
1304 g_object_unref (menu->old_active_menu_item);
1305 menu->old_active_menu_item = NULL;
1308 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1309 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1311 menu_queue_resize (menu);
1317 return g_object_new (GTK_TYPE_MENU, NULL);
1321 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1325 GtkMenu *menu = GTK_MENU (menu_shell);
1326 AttachInfo *ai = get_attach_info (child);
1328 ai->left_attach = -1;
1329 ai->right_attach = -1;
1330 ai->top_attach = -1;
1331 ai->bottom_attach = -1;
1333 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1334 gtk_widget_set_parent_window (child, menu->bin_window);
1336 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1338 menu_queue_resize (menu);
1342 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1347 widget = GTK_WIDGET (menu);
1352 cairo_surface_t *surface;
1353 cairo_pattern_t *pattern;
1356 menu->tearoff_active = FALSE;
1357 menu->saved_scroll_offset = menu->scroll_offset;
1359 window = gtk_widget_get_window (menu->tearoff_window);
1360 width = gdk_window_get_width (window);
1361 height = gdk_window_get_height (window);
1363 surface = gdk_window_create_similar_surface (window,
1364 CAIRO_CONTENT_COLOR,
1368 cr = cairo_create (surface);
1369 gdk_cairo_set_source_window (cr,
1375 gtk_widget_set_size_request (menu->tearoff_window,
1379 pattern = cairo_pattern_create_for_surface (surface);
1380 gdk_window_set_background_pattern (window, pattern);
1382 cairo_pattern_destroy (pattern);
1383 cairo_surface_destroy (surface);
1388 popup_grab_on_window (GdkWindow *window,
1389 GdkDevice *keyboard,
1391 guint32 activate_time)
1394 gdk_device_grab (keyboard, window,
1395 GDK_OWNERSHIP_WINDOW, TRUE,
1396 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1397 NULL, activate_time) != GDK_GRAB_SUCCESS)
1401 gdk_device_grab (pointer, window,
1402 GDK_OWNERSHIP_WINDOW, TRUE,
1403 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1404 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1405 GDK_POINTER_MOTION_MASK,
1406 NULL, activate_time) != GDK_GRAB_SUCCESS)
1409 gdk_device_ungrab (keyboard, activate_time);
1418 * gtk_menu_popup_for_device:
1419 * @menu: a #GtkMenu.
1420 * @device: (allow-none): a #GdkDevice
1421 * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1422 * menu item, or %NULL
1423 * @parent_menu_item: (allow-none): the menu item whose activation triggered
1424 * the popup, or %NULL
1425 * @func: (allow-none): a user supplied function used to position the menu,
1427 * @data: (allow-none): user supplied data to be passed to @func
1428 * @destroy: (allow-none): destroy notify for @data
1429 * @button: the mouse button which was pressed to initiate the event
1430 * @activate_time: the time at which the activation event occurred
1432 * Displays a menu and makes it available for selection.
1434 * Applications can use this function to display context-sensitive menus,
1435 * and will typically supply %NULL for the @parent_menu_shell,
1436 * @parent_menu_item, @func, @data and @destroy parameters. The default
1437 * menu positioning function will position the menu at the current position
1438 * of @device (or its corresponding pointer).
1440 * The @button parameter should be the mouse button pressed to initiate
1441 * the menu popup. If the menu popup was initiated by something other than
1442 * a mouse button press, such as a mouse button release or a keypress,
1443 * @button should be 0.
1445 * The @activate_time parameter is used to conflict-resolve initiation of
1446 * concurrent requests for mouse/keyboard grab requests. To function
1447 * properly, this needs to be the time stamp of the user event (such as
1448 * a mouse click or key press) that caused the initiation of the popup.
1449 * Only if no such event is available, gtk_get_current_event_time() can
1455 gtk_menu_popup_for_device (GtkMenu *menu,
1457 GtkWidget *parent_menu_shell,
1458 GtkWidget *parent_menu_item,
1459 GtkMenuPositionFunc func,
1461 GDestroyNotify destroy,
1463 guint32 activate_time)
1466 GtkWidget *xgrab_shell;
1468 GdkEvent *current_event;
1469 GtkMenuShell *menu_shell;
1470 gboolean grab_keyboard;
1471 GtkMenuPrivate *priv;
1472 GtkWidget *parent_toplevel;
1473 GdkDevice *keyboard, *pointer;
1475 g_return_if_fail (GTK_IS_MENU (menu));
1476 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1479 device = gtk_get_current_event_device ();
1483 GdkDisplay *display;
1484 GdkDeviceManager *device_manager;
1487 display = gtk_widget_get_display (GTK_WIDGET (menu));
1488 device_manager = gdk_display_get_device_manager (display);
1489 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1491 device = devices->data;
1493 g_list_free (devices);
1496 widget = GTK_WIDGET (menu);
1497 menu_shell = GTK_MENU_SHELL (menu);
1498 priv = gtk_menu_get_private (menu);
1500 if (device->source == GDK_SOURCE_KEYBOARD)
1503 pointer = gdk_device_get_associated_device (device);
1508 keyboard = gdk_device_get_associated_device (device);
1511 menu_shell->parent_menu_shell = parent_menu_shell;
1513 priv->seen_item_enter = FALSE;
1515 /* Find the last viewable ancestor, and make an X grab on it
1517 parent = GTK_WIDGET (menu);
1521 gboolean viewable = TRUE;
1522 GtkWidget *tmp = parent;
1526 if (!gtk_widget_get_mapped (tmp))
1531 tmp = gtk_widget_get_parent (tmp);
1535 xgrab_shell = parent;
1537 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1540 /* We want to receive events generated when we map the menu; unfortunately,
1541 * since there is probably already an implicit grab in place from the
1542 * button that the user used to pop up the menu, we won't receive then --
1543 * in particular, the EnterNotify when the menu pops up under the pointer.
1545 * If we are grabbing on a parent menu shell, no problem; just grab on
1546 * that menu shell first before popping up the window with owner_events = TRUE.
1548 * When grabbing on the menu itself, things get more convuluted - we
1549 * we do an explicit grab on a specially created window with
1550 * owner_events = TRUE, which we override further down with a grab
1551 * on the menu. (We can't grab on the menu until it is mapped; we
1552 * probably could just leave the grab on the other window, with a
1553 * little reorganization of the code in gtkmenu*).
1555 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1556 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1561 if (xgrab_shell && xgrab_shell != widget)
1563 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1565 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1566 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1571 GdkWindow *transfer_window;
1573 xgrab_shell = widget;
1574 transfer_window = menu_grab_transfer_window_get (menu);
1575 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1577 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1578 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1582 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1584 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1585 * with a stuck up window, we just abort here. Presumably the user will
1588 menu_shell->parent_menu_shell = NULL;
1589 menu_grab_transfer_window_destroy (menu);
1593 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1594 menu_shell->active = TRUE;
1595 menu_shell->button = button;
1597 /* If we are popping up the menu from something other than, a button
1598 * press then, as a heuristic, we ignore enter events for the menu
1599 * until we get a MOTION_NOTIFY.
1602 current_event = gtk_get_current_event ();
1605 if ((current_event->type != GDK_BUTTON_PRESS) &&
1606 (current_event->type != GDK_ENTER_NOTIFY))
1607 menu_shell->ignore_enter = TRUE;
1609 gdk_event_free (current_event);
1612 menu_shell->ignore_enter = TRUE;
1616 gtk_menu_tearoff_bg_copy (menu);
1618 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1621 parent_toplevel = NULL;
1622 if (parent_menu_shell)
1623 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1624 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1626 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1628 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1631 /* Set transient for to get the right window group and parent relationship */
1632 if (GTK_IS_WINDOW (parent_toplevel))
1633 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1634 GTK_WINDOW (parent_toplevel));
1636 menu->parent_menu_item = parent_menu_item;
1637 menu->position_func = func;
1638 menu->position_func_data = data;
1639 priv->position_func_data_destroy = destroy;
1640 menu_shell->activate_time = activate_time;
1642 /* We need to show the menu here rather in the init function because
1643 * code expects to be able to tell if the menu is onscreen by
1644 * looking at the gtk_widget_get_visible (menu)
1646 gtk_widget_show (GTK_WIDGET (menu));
1648 /* Position the menu, possibly changing the size request
1650 gtk_menu_position (menu);
1652 /* Compute the size of the toplevel and realize it so we
1653 * can scroll correctly.
1656 GtkRequisition tmp_request;
1657 GtkAllocation tmp_allocation = { 0, };
1659 /* Instead of trusting the menu position function to queue a resize when the
1660 * menu goes out of bounds, invalidate the cached size here. */
1661 gtk_widget_queue_resize (GTK_WIDGET (menu));
1662 gtk_widget_get_preferred_size (menu->toplevel, &tmp_request, NULL);
1664 tmp_allocation.width = tmp_request.width;
1665 tmp_allocation.height = tmp_request.height;
1667 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1669 gtk_widget_realize (GTK_WIDGET (menu));
1672 gtk_menu_scroll_to (menu, menu->scroll_offset);
1674 /* if no item is selected, select the first one */
1675 if (!menu_shell->active_menu_item)
1677 gboolean touchscreen_mode;
1679 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1680 "gtk-touchscreen-mode", &touchscreen_mode,
1683 if (touchscreen_mode)
1684 gtk_menu_shell_select_first (menu_shell, TRUE);
1687 /* Once everything is set up correctly, map the toplevel window on
1690 gtk_widget_show (menu->toplevel);
1692 if (xgrab_shell == widget)
1693 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1695 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1697 if (parent_menu_shell)
1699 gboolean keyboard_mode;
1701 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1702 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1704 else if (menu_shell->button == 0) /* a keynav-activated context menu */
1705 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1707 _gtk_menu_shell_update_mnemonics (menu_shell);
1712 * @menu: a #GtkMenu.
1713 * @parent_menu_shell: (allow-none): the menu shell containing the triggering menu item, or %NULL
1714 * @parent_menu_item: (allow-none): the menu item whose activation triggered the popup, or %NULL
1715 * @func: (allow-none): a user supplied function used to position the menu, or %NULL
1716 * @data: (allow-none): user supplied data to be passed to @func.
1717 * @button: the mouse button which was pressed to initiate the event.
1718 * @activate_time: the time at which the activation event occurred.
1720 * Displays a menu and makes it available for selection. Applications can use
1721 * this function to display context-sensitive menus, and will typically supply
1722 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1723 * parameters. The default menu positioning function will position the menu
1724 * at the current mouse cursor position.
1726 * The @button parameter should be the mouse button pressed to initiate
1727 * the menu popup. If the menu popup was initiated by something other than
1728 * a mouse button press, such as a mouse button release or a keypress,
1729 * @button should be 0.
1731 * The @activate_time parameter is used to conflict-resolve initiation of
1732 * concurrent requests for mouse/keyboard grab requests. To function
1733 * properly, this needs to be the time stamp of the user event (such as
1734 * a mouse click or key press) that caused the initiation of the popup.
1735 * Only if no such event is available, gtk_get_current_event_time() can
1739 gtk_menu_popup (GtkMenu *menu,
1740 GtkWidget *parent_menu_shell,
1741 GtkWidget *parent_menu_item,
1742 GtkMenuPositionFunc func,
1745 guint32 activate_time)
1747 g_return_if_fail (GTK_IS_MENU (menu));
1749 gtk_menu_popup_for_device (menu,
1754 button, activate_time);
1758 gtk_menu_popdown (GtkMenu *menu)
1760 GtkMenuPrivate *private;
1761 GtkMenuShell *menu_shell;
1764 g_return_if_fail (GTK_IS_MENU (menu));
1766 menu_shell = GTK_MENU_SHELL (menu);
1767 private = gtk_menu_get_private (menu);
1769 menu_shell->parent_menu_shell = NULL;
1770 menu_shell->active = FALSE;
1771 menu_shell->ignore_enter = FALSE;
1773 private->have_position = FALSE;
1775 gtk_menu_stop_scrolling (menu);
1777 gtk_menu_stop_navigating_submenu (menu);
1779 if (menu_shell->active_menu_item)
1781 if (menu->old_active_menu_item)
1782 g_object_unref (menu->old_active_menu_item);
1783 menu->old_active_menu_item = menu_shell->active_menu_item;
1784 g_object_ref (menu->old_active_menu_item);
1787 gtk_menu_shell_deselect (menu_shell);
1789 /* The X Grab, if present, will automatically be removed when we hide
1791 gtk_widget_hide (menu->toplevel);
1792 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1794 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1798 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1800 if (gtk_bin_get_child (GTK_BIN (menu->toplevel)))
1802 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1806 /* We popped up the menu from the tearoff, so we need to
1807 * release the grab - we aren't actually hiding the menu.
1809 if (menu_shell->have_xgrab && pointer)
1811 GdkDevice *keyboard;
1813 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1814 keyboard = gdk_device_get_associated_device (pointer);
1817 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1821 /* gtk_menu_popdown is called each time a menu item is selected from
1822 * a torn off menu. Only scroll back to the saved position if the
1823 * non-tearoff menu was popped down.
1825 if (!menu->tearoff_active)
1826 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1827 menu->tearoff_active = TRUE;
1830 gtk_widget_hide (GTK_WIDGET (menu));
1832 menu_shell->have_xgrab = FALSE;
1835 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1837 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1839 menu_grab_transfer_window_destroy (menu);
1843 gtk_menu_get_active (GtkMenu *menu)
1848 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1850 if (!menu->old_active_menu_item)
1853 children = GTK_MENU_SHELL (menu)->children;
1857 child = children->data;
1858 children = children->next;
1860 if (gtk_bin_get_child (GTK_BIN (child)))
1865 menu->old_active_menu_item = child;
1866 if (menu->old_active_menu_item)
1867 g_object_ref (menu->old_active_menu_item);
1870 return menu->old_active_menu_item;
1874 gtk_menu_set_active (GtkMenu *menu,
1880 g_return_if_fail (GTK_IS_MENU (menu));
1882 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1885 child = tmp_list->data;
1886 if (gtk_bin_get_child (GTK_BIN (child)))
1888 if (menu->old_active_menu_item)
1889 g_object_unref (menu->old_active_menu_item);
1890 menu->old_active_menu_item = child;
1891 g_object_ref (menu->old_active_menu_item);
1898 * gtk_menu_set_accel_group:
1899 * @accel_group: (allow-none):
1902 gtk_menu_set_accel_group (GtkMenu *menu,
1903 GtkAccelGroup *accel_group)
1905 g_return_if_fail (GTK_IS_MENU (menu));
1907 if (menu->accel_group != accel_group)
1909 if (menu->accel_group)
1910 g_object_unref (menu->accel_group);
1911 menu->accel_group = accel_group;
1912 if (menu->accel_group)
1913 g_object_ref (menu->accel_group);
1914 _gtk_menu_refresh_accel_paths (menu, TRUE);
1919 gtk_menu_get_accel_group (GtkMenu *menu)
1921 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1923 return menu->accel_group;
1927 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1930 /* Menu items chain here to figure whether they can activate their
1931 * accelerators. Unlike ordinary widgets, menus allow accel
1932 * activation even if invisible since that's the usual case for
1933 * submenus/popup-menus. however, the state of the attach widget
1934 * affects the "activeness" of the menu.
1936 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1939 return gtk_widget_can_activate_accel (awidget, signal_id);
1941 return gtk_widget_is_sensitive (widget);
1945 * gtk_menu_set_accel_path
1946 * @menu: a valid #GtkMenu
1947 * @accel_path: (allow-none): a valid accelerator path
1949 * Sets an accelerator path for this menu from which accelerator paths
1950 * for its immediate children, its menu items, can be constructed.
1951 * The main purpose of this function is to spare the programmer the
1952 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1953 * each menu item that should support runtime user changable accelerators.
1954 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1955 * each menu item of this menu, that contains a label describing its purpose,
1956 * automatically gets an accel path assigned. For example, a menu containing
1957 * menu items "New" and "Exit", will, after
1958 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1959 * has been called, assign its items the accel paths:
1960 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1961 * Assigning accel paths to menu items then enables the user to change
1962 * their accelerators at runtime. More details about accelerator paths
1963 * and their default setups can be found at gtk_accel_map_add_entry().
1965 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1966 * pass a static string, you can save some memory by interning it first with
1967 * g_intern_static_string().
1970 gtk_menu_set_accel_path (GtkMenu *menu,
1971 const gchar *accel_path)
1973 g_return_if_fail (GTK_IS_MENU (menu));
1975 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1977 /* FIXME: accel_path should be defined as const gchar* */
1978 menu->accel_path = (gchar*)g_intern_string (accel_path);
1979 if (menu->accel_path)
1980 _gtk_menu_refresh_accel_paths (menu, FALSE);
1984 * gtk_menu_get_accel_path
1985 * @menu: a valid #GtkMenu
1987 * Retrieves the accelerator path set on the menu.
1989 * Returns: the accelerator path set on the menu.
1994 gtk_menu_get_accel_path (GtkMenu *menu)
1996 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1998 return menu->accel_path;
2003 gboolean group_changed;
2007 refresh_accel_paths_foreach (GtkWidget *widget,
2010 AccelPropagation *prop = data;
2012 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
2013 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
2014 prop->menu->accel_path,
2015 prop->menu->accel_group,
2016 prop->group_changed);
2020 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
2021 gboolean group_changed)
2023 g_return_if_fail (GTK_IS_MENU (menu));
2025 if (menu->accel_path && menu->accel_group)
2027 AccelPropagation prop;
2030 prop.group_changed = group_changed;
2031 gtk_container_foreach (GTK_CONTAINER (menu),
2032 refresh_accel_paths_foreach,
2038 gtk_menu_reposition (GtkMenu *menu)
2040 g_return_if_fail (GTK_IS_MENU (menu));
2042 if (!menu->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2043 gtk_menu_position (menu);
2047 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2050 g_return_if_fail (GTK_IS_MENU (menu));
2052 if (adjustment->value != menu->scroll_offset)
2053 gtk_menu_scroll_to (menu, adjustment->value);
2057 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2060 GdkGeometry geometry_hints;
2061 GtkMenuPrivate *priv;
2063 if (!menu->tearoff_window)
2066 priv = gtk_menu_get_private (menu);
2068 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2070 GtkRequisition requisition;
2072 gtk_widget_get_preferred_size (menu->tearoff_scrollbar,
2073 &requisition, NULL);
2074 width += requisition.width;
2077 geometry_hints.min_width = width;
2078 geometry_hints.max_width = width;
2080 geometry_hints.min_height = 0;
2081 geometry_hints.max_height = priv->requested_height;
2083 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
2086 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2090 gtk_menu_update_title (GtkMenu *menu)
2092 if (menu->tearoff_window)
2095 GtkWidget *attach_widget;
2097 title = gtk_menu_get_title (menu);
2100 attach_widget = gtk_menu_get_attach_widget (menu);
2101 if (GTK_IS_MENU_ITEM (attach_widget))
2103 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2104 if (GTK_IS_LABEL (child))
2105 title = gtk_label_get_text (GTK_LABEL (child));
2110 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
2115 gtk_menu_get_toplevel (GtkWidget *menu)
2117 GtkWidget *attach, *toplevel;
2119 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2121 if (GTK_IS_MENU_ITEM (attach))
2122 attach = gtk_widget_get_parent (attach);
2124 if (GTK_IS_MENU (attach))
2125 return gtk_menu_get_toplevel (attach);
2126 else if (GTK_IS_WIDGET (attach))
2128 toplevel = gtk_widget_get_toplevel (attach);
2129 if (gtk_widget_is_toplevel (toplevel))
2137 tearoff_window_destroyed (GtkWidget *widget,
2140 gtk_menu_set_tearoff_state (menu, FALSE);
2144 gtk_menu_set_tearoff_state (GtkMenu *menu,
2148 GtkMenuPrivate *priv;
2150 g_return_if_fail (GTK_IS_MENU (menu));
2152 priv = gtk_menu_get_private (menu);
2154 if (menu->torn_off != torn_off)
2156 menu->torn_off = torn_off;
2157 menu->tearoff_active = torn_off;
2161 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2162 gtk_menu_popdown (menu);
2164 if (!menu->tearoff_window)
2166 GtkWidget *toplevel;
2168 menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2169 "type", GTK_WINDOW_TOPLEVEL,
2170 "screen", gtk_widget_get_screen (menu->toplevel),
2171 "app-paintable", TRUE,
2174 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
2175 GDK_WINDOW_TYPE_HINT_MENU);
2176 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
2177 g_signal_connect (menu->tearoff_window, "destroy",
2178 G_CALLBACK (tearoff_window_destroyed), menu);
2179 g_signal_connect (menu->tearoff_window, "event",
2180 G_CALLBACK (gtk_menu_window_event), menu);
2182 gtk_menu_update_title (menu);
2184 gtk_widget_realize (menu->tearoff_window);
2186 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2187 if (toplevel != NULL)
2188 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
2189 GTK_WINDOW (toplevel));
2191 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
2192 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
2194 height = gdk_window_get_height (gtk_widget_get_window (GTK_WIDGET (menu)));
2195 menu->tearoff_adjustment =
2196 GTK_ADJUSTMENT (gtk_adjustment_new (0,
2198 priv->requested_height,
2202 g_object_connect (menu->tearoff_adjustment,
2203 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2205 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
2207 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
2208 menu->tearoff_scrollbar,
2211 if (menu->tearoff_adjustment->upper > height)
2212 gtk_widget_show (menu->tearoff_scrollbar);
2214 gtk_widget_show (menu->tearoff_hbox);
2217 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
2219 /* Update menu->requisition
2221 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
2224 gtk_menu_set_tearoff_hints (menu, gdk_window_get_width (gtk_widget_get_window (GTK_WIDGET (menu))));
2226 gtk_widget_realize (menu->tearoff_window);
2227 gtk_menu_position (menu);
2229 gtk_widget_show (GTK_WIDGET (menu));
2230 gtk_widget_show (menu->tearoff_window);
2232 gtk_menu_scroll_to (menu, 0);
2237 gtk_widget_hide (GTK_WIDGET (menu));
2238 gtk_widget_hide (menu->tearoff_window);
2239 if (GTK_IS_CONTAINER (menu->toplevel))
2240 gtk_menu_reparent (menu, menu->toplevel, FALSE);
2241 gtk_widget_destroy (menu->tearoff_window);
2243 menu->tearoff_window = NULL;
2244 menu->tearoff_hbox = NULL;
2245 menu->tearoff_scrollbar = NULL;
2246 menu->tearoff_adjustment = NULL;
2249 g_object_notify (G_OBJECT (menu), "tearoff-state");
2254 * gtk_menu_get_tearoff_state:
2257 * Returns whether the menu is torn off. See
2258 * gtk_menu_set_tearoff_state ().
2260 * Return value: %TRUE if the menu is currently torn off.
2263 gtk_menu_get_tearoff_state (GtkMenu *menu)
2265 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2267 return menu->torn_off;
2271 * gtk_menu_set_title:
2273 * @title: a string containing the title for the menu.
2275 * Sets the title string for the menu. The title is displayed when the menu
2276 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
2277 * attached to a parent menu item, and if so it will try to use the same text as
2278 * that menu item's label.
2281 gtk_menu_set_title (GtkMenu *menu,
2284 GtkMenuPrivate *priv;
2287 g_return_if_fail (GTK_IS_MENU (menu));
2289 priv = gtk_menu_get_private (menu);
2291 old_title = priv->title;
2292 priv->title = g_strdup (title);
2295 gtk_menu_update_title (menu);
2296 g_object_notify (G_OBJECT (menu), "tearoff-title");
2300 * gtk_menu_get_title:
2303 * Returns the title of the menu. See gtk_menu_set_title().
2305 * Return value: the title of the menu, or %NULL if the menu has no
2306 * title set on it. This string is owned by the widget and should
2307 * not be modified or freed.
2309 G_CONST_RETURN gchar *
2310 gtk_menu_get_title (GtkMenu *menu)
2312 GtkMenuPrivate *priv;
2314 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2316 priv = gtk_menu_get_private (menu);
2322 gtk_menu_reorder_child (GtkMenu *menu,
2326 GtkMenuShell *menu_shell;
2328 g_return_if_fail (GTK_IS_MENU (menu));
2329 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2331 menu_shell = GTK_MENU_SHELL (menu);
2333 if (g_list_find (menu_shell->children, child))
2335 menu_shell->children = g_list_remove (menu_shell->children, child);
2336 menu_shell->children = g_list_insert (menu_shell->children, child, position);
2338 menu_queue_resize (menu);
2343 gtk_menu_style_set (GtkWidget *widget,
2344 GtkStyle *previous_style)
2346 if (gtk_widget_get_realized (widget))
2348 GtkMenu *menu = GTK_MENU (widget);
2351 style = gtk_widget_get_style (widget);
2353 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2354 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2355 gtk_style_set_background (style, gtk_widget_get_window (widget), GTK_STATE_NORMAL);
2360 get_arrows_border (GtkMenu *menu,
2363 guint scroll_arrow_height;
2364 GtkArrowPlacement arrow_placement;
2366 gtk_widget_style_get (GTK_WIDGET (menu),
2367 "scroll-arrow-vlength", &scroll_arrow_height,
2368 "arrow_placement", &arrow_placement,
2371 switch (arrow_placement)
2373 case GTK_ARROWS_BOTH:
2374 border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2375 border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2378 case GTK_ARROWS_START:
2379 border->top = (menu->upper_arrow_visible ||
2380 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2384 case GTK_ARROWS_END:
2386 border->bottom = (menu->upper_arrow_visible ||
2387 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2391 border->left = border->right = 0;
2395 gtk_menu_realize (GtkWidget *widget)
2397 GtkAllocation allocation;
2400 GdkWindowAttr attributes;
2401 gint attributes_mask;
2404 GtkMenuPrivate *priv;
2407 guint vertical_padding;
2408 guint horizontal_padding;
2409 GtkBorder arrow_border;
2411 g_return_if_fail (GTK_IS_MENU (widget));
2413 menu = GTK_MENU (widget);
2414 priv = gtk_menu_get_private (menu);
2416 gtk_widget_set_realized (widget, TRUE);
2418 gtk_widget_get_allocation (widget, &allocation);
2420 attributes.window_type = GDK_WINDOW_CHILD;
2421 attributes.x = allocation.x;
2422 attributes.y = allocation.y;
2423 attributes.width = allocation.width;
2424 attributes.height = allocation.height;
2425 attributes.wclass = GDK_INPUT_OUTPUT;
2426 attributes.visual = gtk_widget_get_visual (widget);
2427 attributes.event_mask = gtk_widget_get_events (widget);
2428 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2429 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2431 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2433 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2434 &attributes, attributes_mask);
2435 gtk_widget_set_window (widget, window);
2436 gdk_window_set_user_data (window, widget);
2438 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2440 style = gtk_widget_get_style (widget);
2441 gtk_widget_style_get (GTK_WIDGET (menu),
2442 "vertical-padding", &vertical_padding,
2443 "horizontal-padding", &horizontal_padding,
2446 gtk_widget_get_allocation (widget, &allocation);
2448 attributes.x = border_width + style->xthickness + horizontal_padding;
2449 attributes.y = border_width + style->ythickness + vertical_padding;
2450 attributes.width = MAX (1, allocation.width - attributes.x * 2);
2451 attributes.height = MAX (1, allocation.height - attributes.y * 2);
2453 get_arrows_border (menu, &arrow_border);
2454 attributes.y += arrow_border.top;
2455 attributes.height -= arrow_border.top;
2456 attributes.height -= arrow_border.bottom;
2458 menu->view_window = gdk_window_new (window,
2459 &attributes, attributes_mask);
2460 gdk_window_set_user_data (menu->view_window, menu);
2462 gtk_widget_get_allocation (widget, &allocation);
2466 attributes.width = MAX (1, allocation.width - (border_width + style->xthickness + horizontal_padding) * 2);
2467 attributes.height = MAX (1, priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2);
2469 menu->bin_window = gdk_window_new (menu->view_window,
2470 &attributes, attributes_mask);
2471 gdk_window_set_user_data (menu->bin_window, menu);
2473 children = GTK_MENU_SHELL (menu)->children;
2476 child = children->data;
2477 children = children->next;
2479 gtk_widget_set_parent_window (child, menu->bin_window);
2482 gtk_widget_style_attach (widget);
2483 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2484 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2485 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
2487 if (GTK_MENU_SHELL (widget)->active_menu_item)
2488 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2489 GTK_MENU_SHELL (widget)->active_menu_item);
2491 gdk_window_show (menu->bin_window);
2492 gdk_window_show (menu->view_window);
2496 gtk_menu_focus (GtkWidget *widget,
2497 GtkDirectionType direction)
2500 * A menu or its menu items cannot have focus
2505 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2508 menu_grab_transfer_window_get (GtkMenu *menu)
2510 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2513 GdkWindowAttr attributes;
2514 gint attributes_mask;
2516 attributes.x = -100;
2517 attributes.y = -100;
2518 attributes.width = 10;
2519 attributes.height = 10;
2520 attributes.window_type = GDK_WINDOW_TEMP;
2521 attributes.wclass = GDK_INPUT_ONLY;
2522 attributes.override_redirect = TRUE;
2523 attributes.event_mask = 0;
2525 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2527 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2528 &attributes, attributes_mask);
2529 gdk_window_set_user_data (window, menu);
2531 gdk_window_show (window);
2533 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2540 menu_grab_transfer_window_destroy (GtkMenu *menu)
2542 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2545 gdk_window_set_user_data (window, NULL);
2546 gdk_window_destroy (window);
2547 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2552 gtk_menu_unrealize (GtkWidget *widget)
2554 GtkMenu *menu = GTK_MENU (widget);
2556 menu_grab_transfer_window_destroy (menu);
2558 gdk_window_set_user_data (menu->view_window, NULL);
2559 gdk_window_destroy (menu->view_window);
2560 menu->view_window = NULL;
2562 gdk_window_set_user_data (menu->bin_window, NULL);
2563 gdk_window_destroy (menu->bin_window);
2564 menu->bin_window = NULL;
2566 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2571 calculate_line_heights (GtkMenu *menu,
2573 guint **ret_min_heights,
2574 guint **ret_nat_heights)
2576 GtkMenuShell *menu_shell;
2577 GtkMenuPrivate *priv;
2578 GtkWidget *child, *widget;
2580 guint horizontal_padding;
2588 widget = GTK_WIDGET (menu);
2589 menu_shell = GTK_MENU_SHELL (widget);
2590 priv = gtk_menu_get_private (menu);
2592 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2593 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2594 n_heights = gtk_menu_get_n_rows (menu);
2595 n_columns = gtk_menu_get_n_columns (menu);
2596 avail_width = for_width - (2 * menu->toggle_size + priv->accel_size) * n_columns;
2598 gtk_widget_style_get (GTK_WIDGET (menu),
2599 "horizontal-padding", &horizontal_padding,
2602 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2603 avail_width -= (border_width + horizontal_padding + gtk_widget_get_style (widget)->xthickness) * 2;
2605 for (children = menu_shell->children; children; children = children->next)
2610 gint child_min, child_nat;
2612 child = children->data;
2614 if (! gtk_widget_get_visible (child))
2617 get_effective_child_attach (child, &l, &r, &t, &b);
2619 part = avail_width / (r - l);
2621 gtk_widget_get_preferred_height_for_width (child, part,
2622 &child_min, &child_nat);
2624 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2626 part = MAX (child_min, toggle_size) / (b - t);
2627 min_heights[t] = MAX (min_heights[t], part);
2629 part = MAX (child_nat, toggle_size) / (b - t);
2630 nat_heights[t] = MAX (nat_heights[t], part);
2633 if (ret_min_heights)
2634 *ret_min_heights = min_heights;
2636 g_free (min_heights);
2638 if (ret_nat_heights)
2639 *ret_nat_heights = nat_heights;
2641 g_free (nat_heights);
2647 gtk_menu_size_allocate (GtkWidget *widget,
2648 GtkAllocation *allocation)
2651 GtkMenuShell *menu_shell;
2653 GtkAllocation child_allocation;
2654 GtkMenuPrivate *priv;
2660 guint vertical_padding;
2661 guint horizontal_padding;
2663 g_return_if_fail (GTK_IS_MENU (widget));
2664 g_return_if_fail (allocation != NULL);
2666 menu = GTK_MENU (widget);
2667 menu_shell = GTK_MENU_SHELL (widget);
2668 priv = gtk_menu_get_private (menu);
2670 gtk_widget_set_allocation (widget, allocation);
2672 style = gtk_widget_get_style (widget);
2674 gtk_widget_style_get (GTK_WIDGET (menu),
2675 "vertical-padding", &vertical_padding,
2676 "horizontal-padding", &horizontal_padding,
2678 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2680 g_free (priv->heights);
2681 priv->heights_length =
2682 calculate_line_heights (menu,
2687 /* refresh our cached height request */
2688 priv->requested_height = (border_width + vertical_padding + style->ythickness) * 2;
2689 for (i = 0; i < priv->heights_length; i++)
2690 priv->requested_height += priv->heights[i];
2692 x = border_width + style->xthickness + horizontal_padding;
2693 y = border_width + style->ythickness + vertical_padding;
2695 width = MAX (1, allocation->width - x * 2);
2696 height = MAX (1, allocation->height - y * 2);
2698 if (menu_shell->active)
2699 gtk_menu_scroll_to (menu, menu->scroll_offset);
2701 if (!menu->tearoff_active)
2703 GtkBorder arrow_border;
2705 get_arrows_border (menu, &arrow_border);
2706 y += arrow_border.top;
2707 height -= arrow_border.top;
2708 height -= arrow_border.bottom;
2711 if (gtk_widget_get_realized (widget))
2713 gdk_window_move_resize (gtk_widget_get_window (widget),
2714 allocation->x, allocation->y,
2715 allocation->width, allocation->height);
2717 gdk_window_move_resize (menu->view_window,
2724 if (menu_shell->children)
2726 gint base_width = width / gtk_menu_get_n_columns (menu);
2728 children = menu_shell->children;
2731 child = children->data;
2732 children = children->next;
2734 if (gtk_widget_get_visible (child))
2739 get_effective_child_attach (child, &l, &r, &t, &b);
2741 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2744 tmp = gtk_menu_get_n_columns (menu) - l;
2745 l = gtk_menu_get_n_columns (menu) - r;
2749 child_allocation.width = (r - l) * base_width;
2750 child_allocation.height = 0;
2751 child_allocation.x = l * base_width;
2752 child_allocation.y = 0;
2754 for (i = 0; i < b; i++)
2757 child_allocation.y += priv->heights[i];
2759 child_allocation.height += priv->heights[i];
2762 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2765 gtk_widget_size_allocate (child, &child_allocation);
2766 gtk_widget_queue_draw (child);
2770 /* Resize the item window */
2771 if (gtk_widget_get_realized (widget))
2777 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2778 height += priv->heights[i];
2780 width = gtk_menu_get_n_columns (menu) * base_width;
2781 gdk_window_resize (menu->bin_window, width, height);
2784 if (menu->tearoff_active)
2786 if (height >= priv->requested_height)
2788 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2790 gtk_widget_hide (menu->tearoff_scrollbar);
2791 gtk_menu_set_tearoff_hints (menu, allocation->width);
2793 gtk_menu_scroll_to (menu, 0);
2798 menu->tearoff_adjustment->upper = priv->requested_height;
2799 menu->tearoff_adjustment->page_size = allocation->height;
2801 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2802 menu->tearoff_adjustment->upper)
2805 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2808 gtk_menu_scroll_to (menu, value);
2811 gtk_adjustment_changed (menu->tearoff_adjustment);
2813 if (!gtk_widget_get_visible (menu->tearoff_scrollbar))
2815 gtk_widget_show (menu->tearoff_scrollbar);
2816 gtk_menu_set_tearoff_hints (menu, allocation->width);
2824 get_arrows_visible_area (GtkMenu *menu,
2825 GdkRectangle *border,
2826 GdkRectangle *upper,
2827 GdkRectangle *lower,
2830 GtkArrowPlacement arrow_placement;
2832 GtkWidget *widget = GTK_WIDGET (menu);
2834 guint vertical_padding;
2835 guint horizontal_padding;
2836 gint scroll_arrow_height;
2838 style = gtk_widget_get_style (widget);
2840 gtk_widget_style_get (widget,
2841 "vertical-padding", &vertical_padding,
2842 "horizontal-padding", &horizontal_padding,
2843 "scroll-arrow-vlength", &scroll_arrow_height,
2844 "arrow-placement", &arrow_placement,
2847 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2848 border->x = border_width + style->xthickness + horizontal_padding;
2849 border->y = border_width + style->ythickness + vertical_padding;
2850 border->width = gdk_window_get_width (gtk_widget_get_window (widget));
2851 border->height = gdk_window_get_height (gtk_widget_get_window (widget));
2853 switch (arrow_placement)
2855 case GTK_ARROWS_BOTH:
2856 upper->x = border->x;
2857 upper->y = border->y;
2858 upper->width = border->width - 2 * border->x;
2859 upper->height = scroll_arrow_height;
2861 lower->x = border->x;
2862 lower->y = border->height - border->y - scroll_arrow_height;
2863 lower->width = border->width - 2 * border->x;
2864 lower->height = scroll_arrow_height;
2867 case GTK_ARROWS_START:
2868 upper->x = border->x;
2869 upper->y = border->y;
2870 upper->width = (border->width - 2 * border->x) / 2;
2871 upper->height = scroll_arrow_height;
2873 lower->x = border->x + upper->width;
2874 lower->y = border->y;
2875 lower->width = (border->width - 2 * border->x) / 2;
2876 lower->height = scroll_arrow_height;
2879 case GTK_ARROWS_END:
2880 upper->x = border->x;
2881 upper->y = border->height - border->y - scroll_arrow_height;
2882 upper->width = (border->width - 2 * border->x) / 2;
2883 upper->height = scroll_arrow_height;
2885 lower->x = border->x + upper->width;
2886 lower->y = border->height - border->y - scroll_arrow_height;
2887 lower->width = (border->width - 2 * border->x) / 2;
2888 lower->height = scroll_arrow_height;
2892 g_assert_not_reached();
2893 upper->x = upper->y = upper->width = upper->height = 0;
2894 lower->x = lower->y = lower->width = lower->height = 0;
2897 *arrow_space = scroll_arrow_height - 2 * style->ythickness;
2901 gtk_menu_draw (GtkWidget *widget,
2905 GtkMenuPrivate *priv;
2907 GdkRectangle border;
2913 menu = GTK_MENU (widget);
2914 priv = gtk_menu_get_private (menu);
2916 style = gtk_widget_get_style (widget);
2917 window = gtk_widget_get_window (widget);
2919 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2921 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
2923 gfloat arrow_scaling;
2926 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
2927 arrow_size = arrow_scaling * arrow_space;
2929 gtk_paint_box (style,
2935 gtk_widget_get_allocated_width (widget),
2936 gtk_widget_get_allocated_height (widget));
2938 if (menu->upper_arrow_visible && !menu->tearoff_active)
2940 gtk_paint_box (style,
2942 priv->upper_arrow_state,
2944 widget, "menu_scroll_arrow_up",
2950 gtk_paint_arrow (style,
2952 priv->upper_arrow_state,
2954 widget, "menu_scroll_arrow_up",
2957 upper.x + (upper.width - arrow_size) / 2,
2958 upper.y + style->ythickness + (arrow_space - arrow_size) / 2,
2959 arrow_size, arrow_size);
2962 if (menu->lower_arrow_visible && !menu->tearoff_active)
2964 gtk_paint_box (style,
2966 priv->lower_arrow_state,
2968 widget, "menu_scroll_arrow_down",
2974 gtk_paint_arrow (style,
2976 priv->lower_arrow_state,
2978 widget, "menu_scroll_arrow_down",
2981 lower.x + (lower.width - arrow_size) / 2,
2982 lower.y + style->ythickness + (arrow_space - arrow_size) / 2,
2983 arrow_size, arrow_size);
2987 if (gtk_cairo_should_draw_window (cr, menu->bin_window))
2990 gint y = -border.y + menu->scroll_offset;
2993 gdk_window_get_position (menu->view_window, &xoff, &yoff);
2994 cairo_translate (cr, xoff, yoff);
2995 gdk_window_get_position (menu->bin_window, &xoff, &yoff);
2996 cairo_translate (cr, xoff, yoff);
2998 if (!menu->tearoff_active)
3000 GtkBorder arrow_border;
3002 get_arrows_border (menu, &arrow_border);
3003 y -= arrow_border.top;
3006 gtk_paint_box (style,
3012 border.width, border.height);
3017 GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
3023 gtk_menu_show (GtkWidget *widget)
3025 GtkMenu *menu = GTK_MENU (widget);
3027 _gtk_menu_refresh_accel_paths (menu, FALSE);
3029 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3034 gtk_menu_get_preferred_width (GtkWidget *widget,
3039 GtkMenuShell *menu_shell;
3040 GtkMenuPrivate *priv;
3044 guint max_toggle_size;
3045 guint max_accel_width;
3046 guint horizontal_padding;
3048 gint child_min, child_nat;
3049 gint min_width, nat_width;
3051 menu = GTK_MENU (widget);
3052 menu_shell = GTK_MENU_SHELL (widget);
3053 priv = gtk_menu_get_private (menu);
3055 style = gtk_widget_get_style (GTK_WIDGET (widget));
3057 min_width = nat_width = 0;
3059 max_toggle_size = 0;
3060 max_accel_width = 0;
3062 children = menu_shell->children;
3069 child = children->data;
3070 children = children->next;
3072 if (! gtk_widget_get_visible (child))
3075 get_effective_child_attach (child, &l, &r, &t, &b);
3077 /* It's important to size_request the child
3078 * before doing the toggle size request, in
3079 * case the toggle size request depends on the size
3080 * request of a child of the child (e.g. for ImageMenuItem)
3083 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
3084 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
3086 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3087 max_toggle_size = MAX (max_toggle_size, toggle_size);
3088 max_accel_width = MAX (max_accel_width,
3089 GTK_MENU_ITEM (child)->accelerator_width);
3091 part = child_min / (r - l);
3092 min_width = MAX (min_width, part);
3094 part = child_nat / (r - l);
3095 nat_width = MAX (nat_width, part);
3098 /* If the menu doesn't include any images or check items
3099 * reserve the space so that all menus are consistent.
3100 * We only do this for 'ordinary' menus, not for combobox
3101 * menus or multi-column menus
3103 if (max_toggle_size == 0 &&
3104 gtk_menu_get_n_columns (menu) == 1 &&
3105 !priv->no_toggle_size)
3107 guint toggle_spacing;
3108 guint indicator_size;
3110 gtk_style_get (style,
3111 GTK_TYPE_CHECK_MENU_ITEM,
3112 "toggle-spacing", &toggle_spacing,
3113 "indicator-size", &indicator_size,
3116 max_toggle_size = indicator_size + toggle_spacing;
3119 min_width += 2 * max_toggle_size + max_accel_width;
3120 min_width *= gtk_menu_get_n_columns (menu);
3122 nat_width += 2 * max_toggle_size + max_accel_width;
3123 nat_width *= gtk_menu_get_n_columns (menu);
3125 gtk_widget_style_get (GTK_WIDGET (menu),
3126 "horizontal-padding", &horizontal_padding,
3129 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3130 min_width += (border_width + horizontal_padding + style->xthickness) * 2;
3131 nat_width += (border_width + horizontal_padding + style->xthickness) * 2;
3133 menu->toggle_size = max_toggle_size;
3134 priv->accel_size = max_accel_width;
3137 *minimum_size = min_width;
3140 *natural_size = nat_width;
3142 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
3144 if (menu->tearoff_active)
3145 gtk_menu_set_tearoff_hints (menu, min_width);
3149 gtk_menu_get_preferred_height (GtkWidget *widget,
3155 /* Menus are height-for-width only, just return the height for the minimum width */
3156 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
3157 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
3161 gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
3166 GtkMenu *menu = GTK_MENU (widget);
3167 GtkMenuPrivate *private = gtk_menu_get_private (menu);
3168 guint *min_heights, *nat_heights;
3169 guint vertical_padding, border_width;
3171 gint min_height, nat_height;
3173 gtk_widget_style_get (GTK_WIDGET (menu), "vertical-padding", &vertical_padding, NULL);
3174 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3176 min_height = nat_height = (border_width + vertical_padding + gtk_widget_get_style (GTK_WIDGET (widget))->ythickness) * 2;
3179 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3181 for (i = 0; i < n_heights; i++)
3183 min_height += min_heights[i];
3184 nat_height += nat_heights[i];
3187 if (private->have_position)
3189 GdkScreen *screen = gtk_widget_get_screen (menu->toplevel);
3190 GdkRectangle monitor;
3192 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3194 if (private->y + min_height > monitor.y + monitor.height)
3195 min_height = monitor.y + monitor.height - private->y;
3197 if (private->y + nat_height > monitor.y + monitor.height)
3198 nat_height = monitor.y + monitor.height - private->y;
3200 if (private->y < monitor.y)
3202 min_height -= monitor.y - private->y;
3203 nat_height -= monitor.y - private->y;
3208 *minimum_size = min_height;
3211 *natural_size = nat_height;
3213 g_free (min_heights);
3214 g_free (nat_heights);
3220 gtk_menu_button_scroll (GtkMenu *menu,
3221 GdkEventButton *event)
3223 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
3225 gboolean touchscreen_mode;
3227 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3228 "gtk-touchscreen-mode", &touchscreen_mode,
3231 if (touchscreen_mode)
3232 gtk_menu_handle_scrolling (menu,
3233 event->x_root, event->y_root,
3234 event->type == GDK_BUTTON_PRESS,
3244 pointer_in_menu_window (GtkWidget *widget,
3248 GtkAllocation allocation;
3249 GtkMenu *menu = GTK_MENU (widget);
3251 if (gtk_widget_get_mapped (menu->toplevel))
3253 GtkMenuShell *menu_shell;
3254 gint window_x, window_y;
3256 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3257 &window_x, &window_y);
3259 gtk_widget_get_allocation (widget, &allocation);
3260 if (x_root >= window_x && x_root < window_x + allocation.width &&
3261 y_root >= window_y && y_root < window_y + allocation.height)
3264 menu_shell = GTK_MENU_SHELL (widget);
3266 if (GTK_IS_MENU (menu_shell->parent_menu_shell))
3267 return pointer_in_menu_window (menu_shell->parent_menu_shell,
3275 gtk_menu_button_press (GtkWidget *widget,
3276 GdkEventButton *event)
3278 if (event->type != GDK_BUTTON_PRESS)
3281 /* Don't pass down to menu shell for presses over scroll arrows
3283 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3286 /* Don't pass down to menu shell if a non-menuitem part of the menu
3287 * was clicked. The check for the event_widget being a GtkMenuShell
3288 * works because we have the pointer grabbed on menu_shell->window
3289 * with owner_events=TRUE, so all events that are either outside
3290 * the menu or on its border are delivered relative to
3291 * menu_shell->window.
3293 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3294 pointer_in_menu_window (widget, event->x_root, event->y_root))
3297 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3301 gtk_menu_button_release (GtkWidget *widget,
3302 GdkEventButton *event)
3304 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3306 if (priv->ignore_button_release)
3308 priv->ignore_button_release = FALSE;
3312 if (event->type != GDK_BUTTON_RELEASE)
3315 /* Don't pass down to menu shell for releases over scroll arrows
3317 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3320 /* Don't pass down to menu shell if a non-menuitem part of the menu
3321 * was clicked (see comment in button_press()).
3323 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3324 pointer_in_menu_window (widget, event->x_root, event->y_root))
3326 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3327 * bail out early here so it is in a consistent state for the
3328 * next button_press/button_release in GtkMenuShell.
3331 if (GTK_MENU_SHELL (widget)->active)
3332 GTK_MENU_SHELL (widget)->button = 0;
3337 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3340 static const gchar *
3341 get_accel_path (GtkWidget *menu_item,
3346 GClosure *accel_closure;
3347 GtkAccelGroup *accel_group;
3349 path = _gtk_widget_get_accel_path (menu_item, locked);
3352 path = GTK_MENU_ITEM (menu_item)->accel_path;
3358 label = gtk_bin_get_child (GTK_BIN (menu_item));
3360 if (GTK_IS_ACCEL_LABEL (label))
3362 g_object_get (label,
3363 "accel-closure", &accel_closure,
3367 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3369 *locked = gtk_accel_group_get_is_locked (accel_group);
3379 gtk_menu_key_press (GtkWidget *widget,
3382 GtkMenuShell *menu_shell;
3384 gboolean delete = FALSE;
3385 gboolean can_change_accels;
3386 gchar *accel = NULL;
3387 guint accel_key, accel_mods;
3388 GdkModifierType consumed_modifiers;
3389 GdkDisplay *display;
3391 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3392 g_return_val_if_fail (event != NULL, FALSE);
3394 menu_shell = GTK_MENU_SHELL (widget);
3395 menu = GTK_MENU (widget);
3397 gtk_menu_stop_navigating_submenu (menu);
3399 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3402 display = gtk_widget_get_display (widget);
3404 g_object_get (gtk_widget_get_settings (widget),
3405 "gtk-menu-bar-accel", &accel,
3406 "gtk-can-change-accels", &can_change_accels,
3409 if (accel && *accel)
3412 GdkModifierType mods = 0;
3414 gtk_accelerator_parse (accel, &keyval, &mods);
3417 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3419 /* FIXME this is wrong, needs to be in the global accel resolution
3420 * thing, to properly consider i18n etc., but that probably requires
3421 * AccelGroup changes etc.
3423 if (event->keyval == keyval && (mods & event->state) == mods)
3425 gtk_menu_shell_cancel (menu_shell);
3433 switch (event->keyval)
3435 case GDK_KEY_Delete:
3436 case GDK_KEY_KP_Delete:
3437 case GDK_KEY_BackSpace:
3444 /* Figure out what modifiers went into determining the key symbol */
3445 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3446 event->hardware_keycode, event->state, event->group,
3447 NULL, NULL, NULL, &consumed_modifiers);
3449 accel_key = gdk_keyval_to_lower (event->keyval);
3450 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3452 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
3453 * We re-upper case when we match against the keyval, but display and save in caseless form.
3455 if (accel_key != event->keyval)
3456 accel_mods |= GDK_SHIFT_MASK;
3458 /* Modify the accelerators */
3459 if (can_change_accels &&
3460 menu_shell->active_menu_item &&
3461 gtk_bin_get_child (GTK_BIN (menu_shell->active_menu_item)) && /* no separators */
3462 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
3463 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3465 GtkWidget *menu_item = menu_shell->active_menu_item;
3466 gboolean locked, replace_accels = TRUE;
3469 path = get_accel_path (menu_item, &locked);
3470 if (!path || locked)
3472 /* can't change accelerators on menu_items without paths
3473 * (basically, those items are accelerator-locked).
3475 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
3476 gtk_widget_error_bell (widget);
3482 /* For the keys that act to delete the current setting, we delete
3483 * the current setting if there is one, otherwise, we set the
3484 * key as the accelerator.
3490 if (gtk_accel_map_lookup_entry (path, &key) &&
3491 (key.accel_key || key.accel_mods))
3497 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3501 /* we failed, probably because this key is in use and
3504 /* g_print("failed to change\n"); */
3505 gtk_widget_error_bell (widget);
3514 check_threshold (GtkWidget *widget,
3523 ABS (start_x - x) > THRESHOLD ||
3524 ABS (start_y - y) > THRESHOLD;
3528 definitely_within_item (GtkWidget *widget,
3532 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
3535 w = gdk_window_get_width (window);
3536 h = gdk_window_get_height (window);
3539 check_threshold (widget, 0, 0, x, y) &&
3540 check_threshold (widget, w - 1, 0, x, y) &&
3541 check_threshold (widget, w - 1, h - 1, x, y) &&
3542 check_threshold (widget, 0, h - 1, x, y);
3546 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3548 GtkMenuPrivate *priv;
3550 priv = gtk_menu_get_private (menu);
3552 return priv->navigation_height && priv->navigation_width;
3556 gtk_menu_motion_notify (GtkWidget *widget,
3557 GdkEventMotion *event)
3559 GtkWidget *menu_item;
3561 GtkMenuShell *menu_shell;
3564 gboolean need_enter;
3566 if (GTK_IS_MENU (widget))
3568 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3570 if (priv->ignore_button_release)
3571 priv->ignore_button_release = FALSE;
3573 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3577 /* We received the event for one of two reasons:
3579 * a) We are the active menu, and did gtk_grab_add()
3580 * b) The widget is a child of ours, and the event was propagated
3582 * Since for computation of navigation regions, we want the menu which
3583 * is the parent of the menu item, for a), we need to find that menu,
3584 * which may be different from 'widget'.
3586 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3587 parent = gtk_widget_get_parent (menu_item);
3588 if (!GTK_IS_MENU_ITEM (menu_item) ||
3589 !GTK_IS_MENU (parent))
3592 menu_shell = GTK_MENU_SHELL (parent);
3593 menu = GTK_MENU (menu_shell);
3595 if (definitely_within_item (menu_item, event->x, event->y))
3596 menu_shell->activate_time = 0;
3598 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->ignore_enter);
3600 /* Check to see if we are within an active submenu's navigation region
3602 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3605 /* Make sure we pop down if we enter a non-selectable menu item, so we
3606 * don't show a submenu when the cursor is outside the stay-up triangle.
3608 if (!_gtk_menu_item_is_selectable (menu_item))
3610 /* We really want to deselect, but this gives the menushell code
3611 * a chance to do some bookkeeping about the menuitem.
3613 gtk_menu_shell_select_item (menu_shell, menu_item);
3619 /* The menu is now sensitive to enter events on its items, but
3620 * was previously sensitive. So we fake an enter event.
3622 menu_shell->ignore_enter = FALSE;
3624 if (event->x >= 0 && event->x < gdk_window_get_width (event->window) &&
3625 event->y >= 0 && event->y < gdk_window_get_height (event->window))
3627 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3630 send_event->crossing.window = g_object_ref (event->window);
3631 send_event->crossing.time = event->time;
3632 send_event->crossing.send_event = TRUE;
3633 send_event->crossing.x_root = event->x_root;
3634 send_event->crossing.y_root = event->y_root;
3635 send_event->crossing.x = event->x;
3636 send_event->crossing.y = event->y;
3637 send_event->crossing.state = event->state;
3638 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3640 /* We send the event to 'widget', the currently active menu,
3641 * instead of 'menu', the menu that the pointer is in. This
3642 * will ensure that the event will be ignored unless the
3643 * menuitem is a child of the active menu or some parent
3644 * menu of the active menu.
3646 result = gtk_widget_event (widget, send_event);
3647 gdk_event_free (send_event);
3657 get_double_arrows (GtkMenu *menu)
3659 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3660 gboolean double_arrows;
3661 GtkArrowPlacement arrow_placement;
3663 gtk_widget_style_get (GTK_WIDGET (menu),
3664 "double-arrows", &double_arrows,
3665 "arrow-placement", &arrow_placement,
3668 if (arrow_placement != GTK_ARROWS_BOTH)
3671 return double_arrows || (priv->initially_pushed_in &&
3672 menu->scroll_offset != 0);
3676 gtk_menu_scroll_by (GtkMenu *menu,
3679 GtkMenuPrivate *priv;
3680 GtkBorder arrow_border;
3684 gboolean double_arrows;
3686 widget = GTK_WIDGET (menu);
3687 offset = menu->scroll_offset + step;
3688 priv = gtk_menu_get_private (menu);
3690 get_arrows_border (menu, &arrow_border);
3692 double_arrows = get_double_arrows (menu);
3694 /* If we scroll upward and the non-visible top part
3695 * is smaller than the scroll arrow it would be
3696 * pretty stupid to show the arrow and taking more
3697 * screen space than just scrolling to the top.
3700 if ((step < 0) && (offset < arrow_border.top))
3703 /* Don't scroll over the top if we weren't before: */
3704 if ((menu->scroll_offset >= 0) && (offset < 0))
3707 view_height = gdk_window_get_height (gtk_widget_get_window (widget));
3709 if (menu->scroll_offset == 0 &&
3710 view_height >= priv->requested_height)
3713 /* Don't scroll past the bottom if we weren't before: */
3714 if (menu->scroll_offset > 0)
3715 view_height -= arrow_border.top;
3717 /* When both arrows are always shown, reduce
3718 * view height even more.
3721 view_height -= arrow_border.bottom;
3723 if ((menu->scroll_offset + view_height <= priv->requested_height) &&
3724 (offset + view_height > priv->requested_height))
3725 offset = priv->requested_height - view_height;
3727 if (offset != menu->scroll_offset)
3728 gtk_menu_scroll_to (menu, offset);
3732 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3733 gboolean touchscreen_mode)
3735 gboolean upper_visible;
3736 gboolean lower_visible;
3738 upper_visible = menu->upper_arrow_visible;
3739 lower_visible = menu->lower_arrow_visible;
3741 gtk_menu_scroll_by (menu, menu->scroll_step);
3743 if (touchscreen_mode &&
3744 (upper_visible != menu->upper_arrow_visible ||
3745 lower_visible != menu->lower_arrow_visible))
3747 /* We are about to hide a scroll arrow while the mouse is pressed,
3748 * this would cause the uncovered menu item to be activated on button
3749 * release. Therefore we need to ignore button release here
3751 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3752 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3757 gtk_menu_scroll_timeout (gpointer data)
3760 gboolean touchscreen_mode;
3762 menu = GTK_MENU (data);
3764 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3765 "gtk-touchscreen-mode", &touchscreen_mode,
3768 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3774 gtk_menu_scroll_timeout_initial (gpointer data)
3778 gboolean touchscreen_mode;
3780 menu = GTK_MENU (data);
3782 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3783 "gtk-timeout-repeat", &timeout,
3784 "gtk-touchscreen-mode", &touchscreen_mode,
3787 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3789 gtk_menu_remove_scroll_timeout (menu);
3791 menu->timeout_id = gdk_threads_add_timeout (timeout,
3792 gtk_menu_scroll_timeout,
3799 gtk_menu_start_scrolling (GtkMenu *menu)
3802 gboolean touchscreen_mode;
3804 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3805 "gtk-timeout-repeat", &timeout,
3806 "gtk-touchscreen-mode", &touchscreen_mode,
3809 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3811 menu->timeout_id = gdk_threads_add_timeout (timeout,
3812 gtk_menu_scroll_timeout_initial,
3817 gtk_menu_scroll (GtkWidget *widget,
3818 GdkEventScroll *event)
3820 GtkMenu *menu = GTK_MENU (widget);
3822 switch (event->direction)
3824 case GDK_SCROLL_RIGHT:
3825 case GDK_SCROLL_DOWN:
3826 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3828 case GDK_SCROLL_LEFT:
3830 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3838 get_arrows_sensitive_area (GtkMenu *menu,
3839 GdkRectangle *upper,
3840 GdkRectangle *lower)
3842 GtkArrowPlacement arrow_placement;
3843 GtkWidget *widget = GTK_WIDGET (menu);
3847 guint vertical_padding;
3849 gint scroll_arrow_height;
3851 window = gtk_widget_get_window (widget);
3852 width = gdk_window_get_width (window);
3853 height = gdk_window_get_height (window);
3855 gtk_widget_style_get (widget,
3856 "vertical-padding", &vertical_padding,
3857 "scroll-arrow-vlength", &scroll_arrow_height,
3858 "arrow-placement", &arrow_placement,
3861 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) +
3862 gtk_widget_get_style (widget)->ythickness + vertical_padding;
3864 gdk_window_get_position (window, &win_x, &win_y);
3866 switch (arrow_placement)
3868 case GTK_ARROWS_BOTH:
3873 upper->width = width;
3874 upper->height = scroll_arrow_height + border;
3880 lower->y = win_y + height - border - scroll_arrow_height;
3881 lower->width = width;
3882 lower->height = scroll_arrow_height + border;
3886 case GTK_ARROWS_START:
3891 upper->width = width / 2;
3892 upper->height = scroll_arrow_height + border;
3897 lower->x = win_x + width / 2;
3899 lower->width = width / 2;
3900 lower->height = scroll_arrow_height + border;
3904 case GTK_ARROWS_END:
3908 upper->y = win_y + height - border - scroll_arrow_height;
3909 upper->width = width / 2;
3910 upper->height = scroll_arrow_height + border;
3915 lower->x = win_x + width / 2;
3916 lower->y = win_y + height - border - scroll_arrow_height;
3917 lower->width = width / 2;
3918 lower->height = scroll_arrow_height + border;
3926 gtk_menu_handle_scrolling (GtkMenu *menu,
3932 GtkMenuShell *menu_shell;
3933 GtkMenuPrivate *priv;
3936 gboolean scroll_fast = FALSE;
3938 gboolean touchscreen_mode;
3940 priv = gtk_menu_get_private (menu);
3942 menu_shell = GTK_MENU_SHELL (menu);
3944 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3945 "gtk-touchscreen-mode", &touchscreen_mode,
3948 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3953 /* upper arrow handling */
3955 get_arrows_sensitive_area (menu, &rect, NULL);
3958 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3959 (x >= rect.x) && (x < rect.x + rect.width) &&
3960 (y >= rect.y) && (y < rect.y + rect.height))
3965 if (touchscreen_mode)
3966 menu->upper_arrow_prelight = in_arrow;
3968 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3970 gboolean arrow_pressed = FALSE;
3972 if (menu->upper_arrow_visible && !menu->tearoff_active)
3974 if (touchscreen_mode)
3976 if (enter && menu->upper_arrow_prelight)
3978 if (menu->timeout_id == 0)
3980 /* Deselect the active item so that
3981 * any submenus are popped down
3983 gtk_menu_shell_deselect (menu_shell);
3985 gtk_menu_remove_scroll_timeout (menu);
3986 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3990 /* Only do stuff on click. */
3991 gtk_menu_start_scrolling (menu);
3992 arrow_pressed = TRUE;
3997 arrow_pressed = TRUE;
4002 gtk_menu_stop_scrolling (menu);
4005 else /* !touchscreen_mode */
4007 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
4009 if (enter && in_arrow &&
4010 (!menu->upper_arrow_prelight ||
4011 menu->scroll_fast != scroll_fast))
4013 menu->upper_arrow_prelight = TRUE;
4014 menu->scroll_fast = scroll_fast;
4016 /* Deselect the active item so that
4017 * any submenus are popped down
4019 gtk_menu_shell_deselect (menu_shell);
4021 gtk_menu_remove_scroll_timeout (menu);
4022 menu->scroll_step = scroll_fast ?
4023 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
4026 gdk_threads_add_timeout (scroll_fast ?
4027 MENU_SCROLL_TIMEOUT2 :
4028 MENU_SCROLL_TIMEOUT1,
4029 gtk_menu_scroll_timeout, menu);
4031 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
4033 gtk_menu_stop_scrolling (menu);
4038 /* gtk_menu_start_scrolling() might have hit the top of the
4039 * menu, so check if the button isn't insensitive before
4040 * changing it to something else.
4042 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
4044 GtkStateType arrow_state = GTK_STATE_NORMAL;
4047 arrow_state = GTK_STATE_ACTIVE;
4048 else if (menu->upper_arrow_prelight)
4049 arrow_state = GTK_STATE_PRELIGHT;
4051 if (arrow_state != priv->upper_arrow_state)
4053 priv->upper_arrow_state = arrow_state;
4055 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4061 /* lower arrow handling */
4063 get_arrows_sensitive_area (menu, NULL, &rect);
4066 if (menu->lower_arrow_visible && !menu->tearoff_active &&
4067 (x >= rect.x) && (x < rect.x + rect.width) &&
4068 (y >= rect.y) && (y < rect.y + rect.height))
4073 if (touchscreen_mode)
4074 menu->lower_arrow_prelight = in_arrow;
4076 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4078 gboolean arrow_pressed = FALSE;
4080 if (menu->lower_arrow_visible && !menu->tearoff_active)
4082 if (touchscreen_mode)
4084 if (enter && menu->lower_arrow_prelight)
4086 if (menu->timeout_id == 0)
4088 /* Deselect the active item so that
4089 * any submenus are popped down
4091 gtk_menu_shell_deselect (menu_shell);
4093 gtk_menu_remove_scroll_timeout (menu);
4094 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4098 /* Only do stuff on click. */
4099 gtk_menu_start_scrolling (menu);
4100 arrow_pressed = TRUE;
4105 arrow_pressed = TRUE;
4110 gtk_menu_stop_scrolling (menu);
4113 else /* !touchscreen_mode */
4115 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4117 if (enter && in_arrow &&
4118 (!menu->lower_arrow_prelight ||
4119 menu->scroll_fast != scroll_fast))
4121 menu->lower_arrow_prelight = TRUE;
4122 menu->scroll_fast = scroll_fast;
4124 /* Deselect the active item so that
4125 * any submenus are popped down
4127 gtk_menu_shell_deselect (menu_shell);
4129 gtk_menu_remove_scroll_timeout (menu);
4130 menu->scroll_step = scroll_fast ?
4131 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
4134 gdk_threads_add_timeout (scroll_fast ?
4135 MENU_SCROLL_TIMEOUT2 :
4136 MENU_SCROLL_TIMEOUT1,
4137 gtk_menu_scroll_timeout, menu);
4139 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
4141 gtk_menu_stop_scrolling (menu);
4146 /* gtk_menu_start_scrolling() might have hit the bottom of the
4147 * menu, so check if the button isn't insensitive before
4148 * changing it to something else.
4150 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4152 GtkStateType arrow_state = GTK_STATE_NORMAL;
4155 arrow_state = GTK_STATE_ACTIVE;
4156 else if (menu->lower_arrow_prelight)
4157 arrow_state = GTK_STATE_PRELIGHT;
4159 if (arrow_state != priv->lower_arrow_state)
4161 priv->lower_arrow_state = arrow_state;
4163 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4171 gtk_menu_enter_notify (GtkWidget *widget,
4172 GdkEventCrossing *event)
4174 GtkWidget *menu_item;
4176 gboolean touchscreen_mode;
4178 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4179 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4180 event->mode == GDK_CROSSING_STATE_CHANGED)
4183 g_object_get (gtk_widget_get_settings (widget),
4184 "gtk-touchscreen-mode", &touchscreen_mode,
4187 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4188 if (GTK_IS_MENU (widget))
4190 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4192 if (!menu_shell->ignore_enter)
4193 gtk_menu_handle_scrolling (GTK_MENU (widget),
4194 event->x_root, event->y_root, TRUE, TRUE);
4197 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4199 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4201 if (GTK_IS_MENU (menu))
4203 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
4204 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4206 if (priv->seen_item_enter)
4208 /* This is the second enter we see for an item
4209 * on this menu. This means a release should always
4212 menu_shell->activate_time = 0;
4214 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4215 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4217 if (definitely_within_item (menu_item, event->x, event->y))
4219 /* This is an actual user-enter (ie. not a pop-under)
4220 * In this case, the user must either have entered
4221 * sufficiently far enough into the item, or he must move
4222 * far enough away from the enter point. (see
4223 * gtk_menu_motion_notify())
4225 menu_shell->activate_time = 0;
4229 priv->seen_item_enter = TRUE;
4233 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4234 * will not correspond to the event widget's parent. Check to see
4235 * if we are in the parent's navigation region.
4237 parent = gtk_widget_get_parent (menu_item);
4238 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4239 gtk_menu_navigating_submenu (GTK_MENU (parent),
4240 event->x_root, event->y_root))
4243 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4247 gtk_menu_leave_notify (GtkWidget *widget,
4248 GdkEventCrossing *event)
4250 GtkMenuShell *menu_shell;
4252 GtkMenuItem *menu_item;
4253 GtkWidget *event_widget;
4255 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4256 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4257 event->mode == GDK_CROSSING_STATE_CHANGED)
4260 menu = GTK_MENU (widget);
4261 menu_shell = GTK_MENU_SHELL (widget);
4263 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4266 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4268 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4270 if (!GTK_IS_MENU_ITEM (event_widget))
4273 menu_item = GTK_MENU_ITEM (event_widget);
4275 /* Here we check to see if we're leaving an active menu item with a submenu,
4276 * in which case we enter submenu navigation mode.
4278 if (menu_shell->active_menu_item != NULL
4279 && menu_item->submenu != NULL
4280 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
4282 if (GTK_MENU_SHELL (menu_item->submenu)->active)
4284 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4287 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
4289 /* We are leaving an active menu item with nonactive submenu.
4290 * Deselect it so we don't surprise the user with by popping
4291 * up a submenu _after_ he left the item.
4293 gtk_menu_shell_deselect (menu_shell);
4298 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4302 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4304 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4306 priv->navigation_x = 0;
4307 priv->navigation_y = 0;
4308 priv->navigation_width = 0;
4309 priv->navigation_height = 0;
4311 if (menu->navigation_timeout)
4313 g_source_remove (menu->navigation_timeout);
4314 menu->navigation_timeout = 0;
4318 /* When the timeout is elapsed, the navigation region is destroyed
4319 * and the menuitem under the pointer (if any) is selected.
4322 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4324 GtkMenuPopdownData *popdown_data = user_data;
4325 GtkMenu *menu = popdown_data->menu;
4326 GdkWindow *child_window;
4328 gtk_menu_stop_navigating_submenu (menu);
4330 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4332 child_window = gdk_window_get_device_position (menu->bin_window,
4333 popdown_data->device,
4338 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4340 send_event->crossing.window = g_object_ref (child_window);
4341 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4342 send_event->crossing.send_event = TRUE;
4343 gdk_event_set_device (send_event, popdown_data->device);
4345 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4347 gdk_event_free (send_event);
4355 gtk_menu_navigating_submenu (GtkMenu *menu,
4359 GtkMenuPrivate *priv;
4362 if (!gtk_menu_has_navigation_triangle (menu))
4365 priv = gtk_menu_get_private (menu);
4366 width = priv->navigation_width;
4367 height = priv->navigation_height;
4369 /* check if x/y are in the triangle spanned by the navigation parameters */
4371 /* 1) Move the coordinates so the triangle starts at 0,0 */
4372 event_x -= priv->navigation_x;
4373 event_y -= priv->navigation_y;
4375 /* 2) Ensure both legs move along the positive axis */
4387 /* 3) Check that the given coordinate is inside the triangle. The formula
4388 * is a transformed form of this formula: x/w + y/h <= 1
4390 if (event_x >= 0 && event_y >= 0 &&
4391 event_x * height + event_y * width <= width * height)
4397 gtk_menu_stop_navigating_submenu (menu);
4403 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4404 GtkMenuItem *menu_item,
4405 GdkEventCrossing *event)
4407 gint submenu_left = 0;
4408 gint submenu_right = 0;
4409 gint submenu_top = 0;
4410 gint submenu_bottom = 0;
4413 GtkWidget *event_widget;
4414 GtkMenuPopdownData *popdown_data;
4415 GtkMenuPrivate *priv;
4418 g_return_if_fail (menu_item->submenu != NULL);
4419 g_return_if_fail (event != NULL);
4421 priv = gtk_menu_get_private (menu);
4423 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4425 window = gtk_widget_get_window (menu_item->submenu);
4426 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4428 submenu_right = submenu_left + gdk_window_get_width (window);
4429 submenu_bottom = submenu_top + gdk_window_get_height (window);
4431 width = gdk_window_get_width (gtk_widget_get_window (event_widget));
4432 height = gdk_window_get_height (gtk_widget_get_window (event_widget));
4434 if (event->x >= 0 && event->x < width)
4438 gtk_menu_stop_navigating_submenu (menu);
4440 /* The navigation region is the triangle closest to the x/y
4441 * location of the rectangle. This is why the width or height
4445 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
4448 priv->navigation_x = submenu_left;
4449 priv->navigation_width = event->x_root - submenu_left;
4454 priv->navigation_x = submenu_right;
4455 priv->navigation_width = event->x_root - submenu_right;
4461 priv->navigation_y = event->y_root;
4462 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4464 if (priv->navigation_height >= 0)
4470 priv->navigation_y = event->y_root;
4471 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4473 if (priv->navigation_height <= 0)
4477 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4478 "gtk-menu-popdown-delay", &popdown_delay,
4481 popdown_data = g_new (GtkMenuPopdownData, 1);
4482 popdown_data->menu = menu;
4483 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4485 menu->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4487 gtk_menu_stop_navigating_submenu_cb,
4489 (GDestroyNotify) g_free);
4494 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4498 g_return_if_fail (GTK_IS_MENU (menu_shell));
4500 parent = menu_shell->parent_menu_shell;
4502 menu_shell->activate_time = 0;
4503 gtk_menu_popdown (GTK_MENU (menu_shell));
4506 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4510 gtk_menu_position (GtkMenu *menu)
4513 GtkRequisition requisition;
4514 GtkMenuPrivate *private;
4519 GdkScreen *pointer_screen;
4520 GdkRectangle monitor;
4523 g_return_if_fail (GTK_IS_MENU (menu));
4525 widget = GTK_WIDGET (menu);
4527 screen = gtk_widget_get_screen (widget);
4528 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4529 gdk_display_get_device_state (gdk_screen_get_display (screen),
4530 pointer, &pointer_screen, &x, &y, NULL);
4532 /* Get the minimum height for minimum width to figure out
4533 * the right place to popup the menu.
4535 gtk_widget_get_preferred_size (widget, &requisition, NULL);
4537 if (pointer_screen != screen)
4539 /* Pointer is on a different screen; roughly center the
4540 * menu on the screen. If someone was using multiscreen
4541 * + Xinerama together they'd probably want something
4542 * fancier; but that is likely to be vanishingly rare.
4544 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4545 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4548 private = gtk_menu_get_private (menu);
4549 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4551 private->initially_pushed_in = FALSE;
4553 /* Set the type hint here to allow custom position functions to set a different hint */
4554 if (!gtk_widget_get_visible (menu->toplevel))
4555 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4557 if (menu->position_func)
4559 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
4560 menu->position_func_data);
4562 if (private->monitor_num < 0)
4563 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4565 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4569 GtkStyle *style = gtk_widget_get_style (widget);
4570 gint space_left, space_right, space_above, space_below;
4573 gint xthickness = style->xthickness;
4574 gint ythickness = style->ythickness;
4575 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4577 /* The placement of popup menus horizontally works like this (with
4578 * RTL in parentheses)
4580 * - If there is enough room to the right (left) of the mouse cursor,
4581 * position the menu there.
4583 * - Otherwise, if if there is enough room to the left (right) of the
4584 * mouse cursor, position the menu there.
4586 * - Otherwise if the menu is smaller than the monitor, position it
4587 * on the side of the mouse cursor that has the most space available
4589 * - Otherwise (if there is simply not enough room for the menu on the
4590 * monitor), position it as far left (right) as possible.
4592 * Positioning in the vertical direction is similar: first try below
4593 * mouse cursor, then above.
4595 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4597 space_left = x - monitor.x;
4598 space_right = monitor.x + monitor.width - x - 1;
4599 space_above = y - monitor.y;
4600 space_below = monitor.y + monitor.height - y - 1;
4602 /* position horizontally */
4604 /* the amount of space we need to position the menu. Note the
4605 * menu is offset "xthickness" pixels
4607 needed_width = requisition.width - xthickness;
4609 if (needed_width <= space_left ||
4610 needed_width <= space_right)
4612 if ((rtl && needed_width <= space_left) ||
4613 (!rtl && needed_width > space_right))
4616 x = x + xthickness - requisition.width + 1;
4620 /* position right */
4624 /* x is clamped on-screen further down */
4626 else if (requisition.width <= monitor.width)
4628 /* the menu is too big to fit on either side of the mouse
4629 * cursor, but smaller than the monitor. Position it on
4630 * the side that has the most space
4632 if (space_left > space_right)
4640 x = monitor.x + monitor.width - requisition.width;
4643 else /* menu is simply too big for the monitor */
4648 x = monitor.x + monitor.width - requisition.width;
4657 /* Position vertically. The algorithm is the same as above, but
4658 * simpler because we don't have to take RTL into account.
4660 needed_height = requisition.height - ythickness;
4662 if (needed_height <= space_above ||
4663 needed_height <= space_below)
4665 if (needed_height <= space_below)
4668 y = y + ythickness - requisition.height + 1;
4670 y = CLAMP (y, monitor.y,
4671 monitor.y + monitor.height - requisition.height);
4673 else if (needed_height > space_below && needed_height > space_above)
4675 if (space_below >= space_above)
4676 y = monitor.y + monitor.height - requisition.height;
4688 if (private->initially_pushed_in)
4690 menu_height = requisition.height;
4692 if (y + menu_height > monitor.y + monitor.height)
4694 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4695 y = (monitor.y + monitor.height) - menu_height;
4700 scroll_offset += monitor.y - y;
4705 /* FIXME: should this be done in the various position_funcs ? */
4706 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4708 if (GTK_MENU_SHELL (menu)->active)
4710 private->have_position = TRUE;
4715 if (y + requisition.height > monitor.y + monitor.height)
4716 requisition.height = (monitor.y + monitor.height) - y;
4720 scroll_offset += monitor.y - y;
4721 requisition.height -= monitor.y - y;
4725 if (scroll_offset > 0)
4727 GtkBorder arrow_border;
4729 get_arrows_border (menu, &arrow_border);
4730 scroll_offset += arrow_border.top;
4733 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
4736 if (!GTK_MENU_SHELL (menu)->active)
4738 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4739 requisition.width, requisition.height);
4742 menu->scroll_offset = scroll_offset;
4746 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4748 if (menu->timeout_id)
4750 g_source_remove (menu->timeout_id);
4751 menu->timeout_id = 0;
4756 gtk_menu_stop_scrolling (GtkMenu *menu)
4758 gboolean touchscreen_mode;
4760 gtk_menu_remove_scroll_timeout (menu);
4762 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4763 "gtk-touchscreen-mode", &touchscreen_mode,
4766 if (!touchscreen_mode)
4768 menu->upper_arrow_prelight = FALSE;
4769 menu->lower_arrow_prelight = FALSE;
4774 gtk_menu_scroll_to (GtkMenu *menu,
4777 GtkMenuPrivate *priv;
4778 GtkAllocation allocation;
4779 GtkBorder arrow_border;
4783 gint view_width, view_height;
4786 guint vertical_padding;
4787 guint horizontal_padding;
4788 gboolean double_arrows;
4790 widget = GTK_WIDGET (menu);
4791 priv = gtk_menu_get_private (menu);
4793 if (menu->tearoff_active &&
4794 menu->tearoff_adjustment &&
4795 (menu->tearoff_adjustment->value != offset))
4797 menu->tearoff_adjustment->value =
4799 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4800 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4803 /* Move/resize the viewport according to arrows: */
4804 gtk_widget_get_allocation (widget, &allocation);
4805 view_width = allocation.width;
4806 view_height = allocation.height;
4808 style = gtk_widget_get_style (widget);
4810 gtk_widget_style_get (GTK_WIDGET (menu),
4811 "vertical-padding", &vertical_padding,
4812 "horizontal-padding", &horizontal_padding,
4815 double_arrows = get_double_arrows (menu);
4817 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4819 view_width -= (border_width + style->xthickness + horizontal_padding) * 2;
4820 view_height -= (border_width + style->ythickness + vertical_padding) * 2;
4821 menu_height = priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2;
4823 x = border_width + style->xthickness + horizontal_padding;
4824 y = border_width + style->ythickness + vertical_padding;
4826 if (double_arrows && !menu->tearoff_active)
4828 if (view_height < menu_height ||
4829 (offset > 0 && menu->scroll_offset > 0) ||
4830 (offset < 0 && menu->scroll_offset < 0))
4832 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4833 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4834 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4836 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4837 gtk_widget_queue_draw (GTK_WIDGET (menu));
4839 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4841 get_arrows_border (menu, &arrow_border);
4842 y += arrow_border.top;
4843 view_height -= arrow_border.top;
4844 view_height -= arrow_border.bottom;
4847 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4848 else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4849 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4850 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4852 if (offset >= menu_height - view_height)
4853 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4854 else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4855 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4856 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4858 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4859 (priv->lower_arrow_state != lower_arrow_previous_state))
4860 gtk_widget_queue_draw (GTK_WIDGET (menu));
4862 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4863 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4865 /* At the upper border, possibly remove timeout */
4866 if (menu->scroll_step < 0)
4868 gtk_menu_stop_scrolling (menu);
4869 gtk_widget_queue_draw (GTK_WIDGET (menu));
4873 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4874 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4876 /* At the lower border, possibly remove timeout */
4877 if (menu->scroll_step > 0)
4879 gtk_menu_stop_scrolling (menu);
4880 gtk_widget_queue_draw (GTK_WIDGET (menu));
4884 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4888 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4889 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4891 gtk_menu_stop_scrolling (menu);
4892 gtk_widget_queue_draw (GTK_WIDGET (menu));
4895 else if (!menu->tearoff_active)
4897 gboolean last_visible;
4899 last_visible = menu->upper_arrow_visible;
4900 menu->upper_arrow_visible = offset > 0;
4902 /* upper_arrow_visible may have changed, so requery the border */
4903 get_arrows_border (menu, &arrow_border);
4904 view_height -= arrow_border.top;
4906 if ((last_visible != menu->upper_arrow_visible) &&
4907 !menu->upper_arrow_visible)
4909 menu->upper_arrow_prelight = FALSE;
4911 /* If we hid the upper arrow, possibly remove timeout */
4912 if (menu->scroll_step < 0)
4914 gtk_menu_stop_scrolling (menu);
4915 gtk_widget_queue_draw (GTK_WIDGET (menu));
4919 last_visible = menu->lower_arrow_visible;
4920 menu->lower_arrow_visible = offset < menu_height - view_height;
4922 /* lower_arrow_visible may have changed, so requery the border */
4923 get_arrows_border (menu, &arrow_border);
4924 view_height -= arrow_border.bottom;
4926 if ((last_visible != menu->lower_arrow_visible) &&
4927 !menu->lower_arrow_visible)
4929 menu->lower_arrow_prelight = FALSE;
4931 /* If we hid the lower arrow, possibly remove timeout */
4932 if (menu->scroll_step > 0)
4934 gtk_menu_stop_scrolling (menu);
4935 gtk_widget_queue_draw (GTK_WIDGET (menu));
4939 y += arrow_border.top;
4942 /* Scroll the menu: */
4943 if (gtk_widget_get_realized (widget))
4944 gdk_window_move (menu->bin_window, 0, -offset);
4946 if (gtk_widget_get_realized (widget))
4947 gdk_window_move_resize (menu->view_window,
4953 menu->scroll_offset = offset;
4957 compute_child_offset (GtkMenu *menu,
4958 GtkWidget *menu_item,
4961 gboolean *is_last_child)
4963 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4964 gint item_top_attach;
4965 gint item_bottom_attach;
4966 gint child_offset = 0;
4969 get_effective_child_attach (menu_item, NULL, NULL,
4970 &item_top_attach, &item_bottom_attach);
4972 /* there is a possibility that we get called before _size_request, so
4973 * check the height table for safety.
4975 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4978 /* when we have a row with only invisible children, it's height will
4979 * be zero, so there's no need to check WIDGET_VISIBLE here
4981 for (i = 0; i < item_top_attach; i++)
4982 child_offset += priv->heights[i];
4985 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4987 *offset = child_offset;
4989 *height = priv->heights[item_top_attach];
4995 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4996 GtkWidget *menu_item)
4998 GtkMenu *menu = GTK_MENU (menu_shell);
4999 GtkWidget *widget = GTK_WIDGET (menu_shell);
5000 gint child_offset, child_height;
5004 gboolean last_child = 0;
5006 /* We need to check if the selected item fully visible.
5007 * If not we need to scroll the menu so that it becomes fully
5011 if (compute_child_offset (menu, menu_item,
5012 &child_offset, &child_height, &last_child))
5014 guint vertical_padding;
5015 gboolean double_arrows;
5017 y = menu->scroll_offset;
5018 width = gdk_window_get_width (gtk_widget_get_window (widget));
5019 height = gdk_window_get_height (gtk_widget_get_window (widget));
5021 gtk_widget_style_get (widget,
5022 "vertical-padding", &vertical_padding,
5025 double_arrows = get_double_arrows (menu);
5027 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5028 2 * gtk_widget_get_style (widget)->ythickness +
5029 2 * vertical_padding;
5030 if (child_offset < y)
5032 /* Ignore the enter event we might get if the pointer is on the menu
5034 menu_shell->ignore_enter = TRUE;
5035 gtk_menu_scroll_to (menu, child_offset);
5039 GtkBorder arrow_border;
5043 get_arrows_border (menu, &arrow_border);
5044 if (!menu->tearoff_active)
5045 arrow_height = arrow_border.top + arrow_border.bottom;
5047 if (child_offset + child_height > y + height - arrow_height)
5050 if ((!last_child && !menu->tearoff_active) || double_arrows)
5051 arrow_height += arrow_border.bottom;
5053 y = child_offset + child_height - height + arrow_height;
5054 if (((y > 0) && !menu->tearoff_active) || double_arrows)
5056 /* Need upper arrow */
5057 arrow_height += arrow_border.top;
5058 y = child_offset + child_height - height + arrow_height;
5060 /* Ignore the enter event we might get if the pointer is on the menu
5062 menu_shell->ignore_enter = TRUE;
5063 gtk_menu_scroll_to (menu, y);
5071 gtk_menu_select_item (GtkMenuShell *menu_shell,
5072 GtkWidget *menu_item)
5074 GtkMenu *menu = GTK_MENU (menu_shell);
5076 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5077 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5079 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5083 /* Reparent the menu, taking care of the refcounting
5085 * If unrealize is true we force a unrealize while reparenting the parent.
5086 * This can help eliminate flicker in some cases.
5088 * What happens is that when the menu is unrealized and then re-realized,
5089 * the allocations are as follows:
5091 * parent - 1x1 at (0,0)
5092 * child1 - 100x20 at (0,0)
5093 * child2 - 100x20 at (0,20)
5094 * child3 - 100x20 at (0,40)
5096 * That is, the parent is small but the children are full sized. Then,
5097 * when the queued_resize gets processed, the parent gets resized to
5100 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5101 * contains the following logic:
5103 * - if a move or resize operation on a window would change the clip
5104 * region on the children, then before the window is resized
5105 * the background for children is temporarily set to None, the
5106 * move/resize done, and the background for the children restored.
5108 * So, at the point where the parent is resized to final size, the
5109 * background for the children is temporarily None, and thus they
5110 * are not cleared to the background color and the previous background
5111 * (the image of the menu) is left in place.
5114 gtk_menu_reparent (GtkMenu *menu,
5115 GtkWidget *new_parent,
5118 GtkObject *object = GTK_OBJECT (menu);
5119 GtkWidget *widget = GTK_WIDGET (menu);
5120 gboolean was_floating = g_object_is_floating (object);
5122 g_object_ref_sink (object);
5126 g_object_ref (object);
5127 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5128 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5129 g_object_unref (object);
5132 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
5135 g_object_force_floating (G_OBJECT (object));
5137 g_object_unref (object);
5141 gtk_menu_show_all (GtkWidget *widget)
5143 /* Show children, but not self. */
5144 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5149 gtk_menu_hide_all (GtkWidget *widget)
5151 /* Hide children, but not self. */
5152 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
5156 * gtk_menu_set_screen:
5157 * @menu: a #GtkMenu.
5158 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5159 * determined by the widget the menu is attached to.
5161 * Sets the #GdkScreen on which the menu will be displayed.
5166 gtk_menu_set_screen (GtkMenu *menu,
5169 g_return_if_fail (GTK_IS_MENU (menu));
5170 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
5172 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5176 menu_change_screen (menu, screen);
5180 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5182 attach_widget_screen_changed (attach_widget, NULL, menu);
5188 * @menu: a #GtkMenu.
5189 * @child: a #GtkMenuItem.
5190 * @left_attach: The column number to attach the left side of the item to.
5191 * @right_attach: The column number to attach the right side of the item to.
5192 * @top_attach: The row number to attach the top of the item to.
5193 * @bottom_attach: The row number to attach the bottom of the item to.
5195 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5196 * an item will occupy is specified by @left_attach, @right_attach,
5197 * @top_attach and @bottom_attach. These each represent the leftmost,
5198 * rightmost, uppermost and lower column and row numbers of the table.
5199 * (Columns and rows are indexed from zero).
5201 * Note that this function is not related to gtk_menu_detach().
5206 gtk_menu_attach (GtkMenu *menu,
5211 guint bottom_attach)
5213 GtkMenuShell *menu_shell;
5216 g_return_if_fail (GTK_IS_MENU (menu));
5217 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5218 parent = gtk_widget_get_parent (child);
5219 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5220 g_return_if_fail (left_attach < right_attach);
5221 g_return_if_fail (top_attach < bottom_attach);
5223 menu_shell = GTK_MENU_SHELL (menu);
5227 AttachInfo *ai = get_attach_info (child);
5229 ai->left_attach = left_attach;
5230 ai->right_attach = right_attach;
5231 ai->top_attach = top_attach;
5232 ai->bottom_attach = bottom_attach;
5234 menu_shell->children = g_list_append (menu_shell->children, child);
5236 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5238 menu_queue_resize (menu);
5242 gtk_container_child_set (GTK_CONTAINER (parent), child,
5243 "left-attach", left_attach,
5244 "right-attach", right_attach,
5245 "top-attach", top_attach,
5246 "bottom-attach", bottom_attach,
5252 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5256 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5257 "gtk-menu-popup-delay", &popup_delay,
5264 find_child_containing (GtkMenuShell *menu_shell,
5272 /* find a child which includes the area given by
5273 * left, right, top, bottom.
5276 for (list = menu_shell->children; list; list = list->next)
5280 if (!_gtk_menu_item_is_selectable (list->data))
5283 get_effective_child_attach (list->data, &l, &r, &t, &b);
5285 if (l <= left && right <= r
5286 && t <= top && bottom <= b)
5287 return GTK_WIDGET (list->data);
5294 gtk_menu_move_current (GtkMenuShell *menu_shell,
5295 GtkMenuDirectionType direction)
5297 GtkMenu *menu = GTK_MENU (menu_shell);
5300 GtkWidget *match = NULL;
5302 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5306 case GTK_MENU_DIR_CHILD:
5307 direction = GTK_MENU_DIR_PARENT;
5309 case GTK_MENU_DIR_PARENT:
5310 direction = GTK_MENU_DIR_CHILD;
5316 /* use special table menu key bindings */
5317 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5319 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
5321 if (direction == GTK_MENU_DIR_NEXT)
5323 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5325 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5333 for (i = 0; i < t; i++)
5335 match = find_child_containing (menu_shell,
5336 l, l + 1, i, i + 1);
5342 else if (direction == GTK_MENU_DIR_PREV)
5344 for (i = t; i > 0; i--)
5346 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
5354 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5356 match = find_child_containing (menu_shell,
5357 l, l + 1, i - 1, i);
5363 else if (direction == GTK_MENU_DIR_PARENT)
5365 /* we go one left if possible */
5367 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
5371 GtkWidget *parent = menu_shell->parent_menu_shell;
5374 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
5375 match = menu_shell->active_menu_item;
5378 else if (direction == GTK_MENU_DIR_CHILD)
5380 /* we go one right if possible */
5381 if (r < gtk_menu_get_n_columns (menu))
5382 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5386 GtkWidget *parent = menu_shell->parent_menu_shell;
5388 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
5390 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
5391 match = menu_shell->active_menu_item;
5397 gtk_menu_shell_select_item (menu_shell, match);
5402 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5406 get_visible_size (GtkMenu *menu)
5408 GtkAllocation allocation;
5409 GtkWidget *widget = GTK_WIDGET (menu);
5410 GtkContainer *container = GTK_CONTAINER (menu);
5413 gtk_widget_get_allocation (widget, &allocation);
5414 menu_height = (allocation.height
5415 - 2 * (gtk_container_get_border_width (container)
5416 + gtk_widget_get_style (widget)->ythickness));
5418 if (!menu->tearoff_active)
5420 GtkBorder arrow_border;
5422 get_arrows_border (menu, &arrow_border);
5423 menu_height -= arrow_border.top;
5424 menu_height -= arrow_border.bottom;
5430 /* Find the sensitive on-screen child containing @y, or if none,
5431 * the nearest selectable onscreen child. (%NULL if none)
5434 child_at (GtkMenu *menu,
5437 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5438 GtkWidget *child = NULL;
5439 gint child_offset = 0;
5442 gint lower, upper; /* Onscreen bounds */
5444 menu_height = get_visible_size (menu);
5445 lower = menu->scroll_offset;
5446 upper = menu->scroll_offset + menu_height;
5448 for (children = menu_shell->children; children; children = children->next)
5450 if (gtk_widget_get_visible (children->data))
5452 GtkRequisition child_requisition;
5454 gtk_widget_get_preferred_size (children->data,
5455 &child_requisition, NULL);
5457 if (_gtk_menu_item_is_selectable (children->data) &&
5458 child_offset >= lower &&
5459 child_offset + child_requisition.height <= upper)
5461 child = children->data;
5463 if (child_offset + child_requisition.height > y &&
5464 !GTK_IS_TEAROFF_MENU_ITEM (child))
5468 child_offset += child_requisition.height;
5476 get_menu_height (GtkMenu *menu)
5478 GtkAllocation allocation;
5479 GtkWidget *widget = GTK_WIDGET (menu);
5482 gtk_widget_get_allocation (widget, &allocation);
5484 height = allocation.height;
5485 height -= gtk_container_get_border_width (GTK_CONTAINER (widget) + gtk_widget_get_style (widget)->ythickness) * 2;
5487 if (!menu->tearoff_active)
5489 GtkBorder arrow_border;
5491 get_arrows_border (menu, &arrow_border);
5492 height -= arrow_border.top;
5493 height -= arrow_border.bottom;
5500 gtk_menu_real_move_scroll (GtkMenu *menu,
5503 gint page_size = get_visible_size (menu);
5504 gint end_position = get_menu_height (menu);
5505 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5509 case GTK_SCROLL_PAGE_UP:
5510 case GTK_SCROLL_PAGE_DOWN:
5514 gint child_offset = 0;
5515 gboolean old_upper_arrow_visible;
5518 if (type == GTK_SCROLL_PAGE_UP)
5523 if (menu_shell->active_menu_item)
5527 compute_child_offset (menu, menu_shell->active_menu_item,
5528 &child_offset, &child_height, NULL);
5529 child_offset += child_height / 2;
5532 menu_shell->ignore_enter = TRUE;
5533 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5534 old_offset = menu->scroll_offset;
5536 new_offset = menu->scroll_offset + step;
5537 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5539 gtk_menu_scroll_to (menu, new_offset);
5541 if (menu_shell->active_menu_item)
5543 GtkWidget *new_child;
5544 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5545 GtkBorder arrow_border;
5547 get_arrows_border (menu, &arrow_border);
5549 if (menu->scroll_offset != old_offset)
5550 step = menu->scroll_offset - old_offset;
5552 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5554 new_child = child_at (menu, child_offset + step);
5556 gtk_menu_shell_select_item (menu_shell, new_child);
5560 case GTK_SCROLL_START:
5561 /* Ignore the enter event we might get if the pointer is on the menu
5563 menu_shell->ignore_enter = TRUE;
5564 gtk_menu_scroll_to (menu, 0);
5565 gtk_menu_shell_select_first (menu_shell, TRUE);
5567 case GTK_SCROLL_END:
5568 /* Ignore the enter event we might get if the pointer is on the menu
5570 menu_shell->ignore_enter = TRUE;
5571 gtk_menu_scroll_to (menu, end_position - page_size);
5572 _gtk_menu_shell_select_last (menu_shell, TRUE);
5581 * gtk_menu_set_monitor:
5583 * @monitor_num: the number of the monitor on which the menu should
5586 * Informs GTK+ on which monitor a menu should be popped up.
5587 * See gdk_screen_get_monitor_geometry().
5589 * This function should be called from a #GtkMenuPositionFunc if the
5590 * menu should not appear on the same monitor as the pointer. This
5591 * information can't be reliably inferred from the coordinates returned
5592 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
5593 * may extend beyond the monitor boundaries or even the screen boundaries.
5598 gtk_menu_set_monitor (GtkMenu *menu,
5601 GtkMenuPrivate *priv;
5602 g_return_if_fail (GTK_IS_MENU (menu));
5604 priv = gtk_menu_get_private (menu);
5606 priv->monitor_num = monitor_num;
5610 * gtk_menu_get_monitor:
5613 * Retrieves the number of the monitor on which to show the menu.
5615 * Returns: the number of the monitor on which the menu should
5616 * be popped up or -1, if no monitor has been set
5621 gtk_menu_get_monitor (GtkMenu *menu)
5623 GtkMenuPrivate *priv;
5624 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5626 priv = gtk_menu_get_private (menu);
5628 return priv->monitor_num;
5632 * gtk_menu_get_for_attach_widget:
5633 * @widget: a #GtkWidget
5635 * Returns a list of the menus which are attached to this widget.
5636 * This list is owned by GTK+ and must not be modified.
5638 * Return value: (element-type GtkWidget) (transfer none): the list of menus attached to his widget.
5643 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5647 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5649 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5654 gtk_menu_grab_notify (GtkWidget *widget,
5655 gboolean was_grabbed)
5657 GtkWidget *toplevel;
5658 GtkWindowGroup *group;
5662 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5665 !gtk_widget_device_is_shadowed (widget, pointer))
5668 toplevel = gtk_widget_get_toplevel (widget);
5670 if (!GTK_IS_WINDOW (toplevel))
5673 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5674 grab = gtk_window_group_get_current_device_grab (group, pointer);
5676 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
5677 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5681 * gtk_menu_set_reserve_toggle_size:
5683 * @reserve_toggle_size: whether to reserve size for toggles
5685 * Sets whether the menu should reserve space for drawing toggles
5686 * or icons, regardless of their actual presence.
5691 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5692 gboolean reserve_toggle_size)
5694 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5695 gboolean no_toggle_size;
5697 no_toggle_size = !reserve_toggle_size;
5699 if (priv->no_toggle_size != no_toggle_size)
5701 priv->no_toggle_size = no_toggle_size;
5703 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5708 * gtk_menu_get_reserve_toggle_size:
5711 * Returns whether the menu reserves space for toggles and
5712 * icons, regardless of their actual presence.
5714 * Returns: Whether the menu reserves toggle space
5719 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5721 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5723 return !priv->no_toggle_size;