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/.
31 #include <gobject/gvaluecollector.h>
33 #include "gtkaccellabel.h"
34 #include "gtkaccelmap.h"
35 #include "gtkbindings.h"
36 #include "gtkcheckmenuitem.h"
38 #include "gtkmarshalers.h"
39 #include "gtkmenuprivate.h"
40 #include "gtkmenuitemprivate.h"
41 #include "gtkmenushellprivate.h"
42 #include "gtktearoffmenuitem.h"
43 #include "gtkwindow.h"
45 #include "gtkvscrollbar.h"
46 #include "gtksettings.h"
47 #include "gtkprivate.h"
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 _GtkMenuPopdownData GtkMenuPopdownData;
66 struct _GtkMenuAttachData
68 GtkWidget *attach_widget;
69 GtkMenuDetachFunc detacher;
72 struct _OldGtkMenuPrivate
76 gboolean initially_pushed_in;
78 GDestroyNotify position_func_data_destroy;
80 /* info used for the table */
83 gint requested_height;
87 /* Cached layout information */
96 GtkStateFlags lower_arrow_state;
97 GtkStateFlags upper_arrow_state;
99 /* navigation region */
102 int navigation_width;
103 int navigation_height;
105 guint have_layout : 1;
106 guint seen_item_enter : 1;
107 guint have_position : 1;
108 guint ignore_button_release : 1;
109 guint no_toggle_size : 1;
112 struct _GtkMenuPopdownData
124 gint effective_left_attach;
125 gint effective_right_attach;
126 gint effective_top_attach;
127 gint effective_bottom_attach;
144 PROP_RESERVE_TOGGLE_SIZE
149 CHILD_PROP_LEFT_ATTACH,
150 CHILD_PROP_RIGHT_ATTACH,
151 CHILD_PROP_TOP_ATTACH,
152 CHILD_PROP_BOTTOM_ATTACH
155 static void gtk_menu_set_property (GObject *object,
159 static void gtk_menu_get_property (GObject *object,
163 static void gtk_menu_set_child_property(GtkContainer *container,
168 static void gtk_menu_get_child_property(GtkContainer *container,
173 static void gtk_menu_destroy (GtkWidget *widget);
174 static void gtk_menu_realize (GtkWidget *widget);
175 static void gtk_menu_unrealize (GtkWidget *widget);
176 static void gtk_menu_size_allocate (GtkWidget *widget,
177 GtkAllocation *allocation);
178 static void gtk_menu_show (GtkWidget *widget);
179 static gboolean gtk_menu_draw (GtkWidget *widget,
181 static gboolean gtk_menu_key_press (GtkWidget *widget,
183 static gboolean gtk_menu_scroll (GtkWidget *widget,
184 GdkEventScroll *event);
185 static gboolean gtk_menu_button_press (GtkWidget *widget,
186 GdkEventButton *event);
187 static gboolean gtk_menu_button_release (GtkWidget *widget,
188 GdkEventButton *event);
189 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
190 GdkEventMotion *event);
191 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
192 GdkEventCrossing *event);
193 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
194 GdkEventCrossing *event);
195 static void gtk_menu_scroll_to (GtkMenu *menu,
197 static void gtk_menu_grab_notify (GtkWidget *widget,
198 gboolean was_grabbed);
200 static void gtk_menu_stop_scrolling (GtkMenu *menu);
201 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
202 static gboolean gtk_menu_scroll_timeout (gpointer data);
203 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
204 static void gtk_menu_start_scrolling (GtkMenu *menu);
206 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
207 GtkWidget *menu_item);
208 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
209 GtkWidget *menu_item);
210 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
213 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
215 static void gtk_menu_handle_scrolling (GtkMenu *menu,
220 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
222 static void gtk_menu_style_updated (GtkWidget *widget);
223 static gboolean gtk_menu_focus (GtkWidget *widget,
224 GtkDirectionType direction);
225 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
226 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
227 GtkMenuDirectionType direction);
228 static void gtk_menu_real_move_scroll (GtkMenu *menu,
231 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
232 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
233 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
236 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
237 GtkMenuItem *menu_item,
238 GdkEventCrossing *event);
240 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
241 static void gtk_menu_show_all (GtkWidget *widget);
242 static void gtk_menu_position (GtkMenu *menu,
243 gboolean set_scroll_offset);
244 static void gtk_menu_reparent (GtkMenu *menu,
245 GtkWidget *new_parent,
247 static void gtk_menu_remove (GtkContainer *menu,
250 static void gtk_menu_update_title (GtkMenu *menu);
252 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
253 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
255 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
257 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
258 gboolean group_changed);
260 static void gtk_menu_get_preferred_width (GtkWidget *widget,
263 static void gtk_menu_get_preferred_height (GtkWidget *widget,
266 static void gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
272 static const gchar attach_data_key[] = "gtk-menu-attach-data";
274 static guint menu_signals[LAST_SIGNAL] = { 0 };
276 static GtkMenuPrivate *
277 gtk_menu_get_private (GtkMenu *menu)
279 return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
282 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
285 menu_queue_resize (GtkMenu *menu)
287 GtkMenuPrivate *priv = menu->priv;
289 priv->have_layout = FALSE;
290 gtk_widget_queue_resize (GTK_WIDGET (menu));
294 attach_info_free (AttachInfo *info)
296 g_slice_free (AttachInfo, info);
300 get_attach_info (GtkWidget *child)
302 GObject *object = G_OBJECT (child);
303 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
307 ai = g_slice_new0 (AttachInfo);
308 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
309 (GDestroyNotify) attach_info_free);
316 is_grid_attached (AttachInfo *ai)
318 return (ai->left_attach >= 0 &&
319 ai->right_attach >= 0 &&
320 ai->top_attach >= 0 &&
321 ai->bottom_attach >= 0);
325 menu_ensure_layout (GtkMenu *menu)
327 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
329 if (!priv->have_layout)
331 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
335 gint max_right_attach;
336 gint max_bottom_attach;
338 /* Find extents of gridded portion
340 max_right_attach = 1;
341 max_bottom_attach = 0;
343 for (l = menu_shell->priv->children; l; l = l->next)
345 GtkWidget *child = l->data;
346 AttachInfo *ai = get_attach_info (child);
348 if (is_grid_attached (ai))
350 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
351 max_right_attach = MAX (max_right_attach, ai->right_attach);
355 /* Find empty rows */
356 row_occupied = g_malloc0 (max_bottom_attach);
358 for (l = menu_shell->priv->children; l; l = l->next)
360 GtkWidget *child = l->data;
361 AttachInfo *ai = get_attach_info (child);
363 if (is_grid_attached (ai))
367 for (i = ai->top_attach; i < ai->bottom_attach; i++)
368 row_occupied[i] = TRUE;
372 /* Lay non-grid-items out in those rows
375 for (l = menu_shell->priv->children; l; l = l->next)
377 GtkWidget *child = l->data;
378 AttachInfo *ai = get_attach_info (child);
380 if (!is_grid_attached (ai))
382 while (current_row < max_bottom_attach && row_occupied[current_row])
385 ai->effective_left_attach = 0;
386 ai->effective_right_attach = max_right_attach;
387 ai->effective_top_attach = current_row;
388 ai->effective_bottom_attach = current_row + 1;
394 ai->effective_left_attach = ai->left_attach;
395 ai->effective_right_attach = ai->right_attach;
396 ai->effective_top_attach = ai->top_attach;
397 ai->effective_bottom_attach = ai->bottom_attach;
401 g_free (row_occupied);
403 priv->n_rows = MAX (current_row, max_bottom_attach);
404 priv->n_columns = max_right_attach;
405 priv->have_layout = TRUE;
411 gtk_menu_get_n_columns (GtkMenu *menu)
413 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
415 menu_ensure_layout (menu);
417 return priv->n_columns;
421 gtk_menu_get_n_rows (GtkMenu *menu)
423 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
425 menu_ensure_layout (menu);
431 get_effective_child_attach (GtkWidget *child,
437 GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
440 menu_ensure_layout (menu);
442 ai = get_attach_info (child);
445 *l = ai->effective_left_attach;
447 *r = ai->effective_right_attach;
449 *t = ai->effective_top_attach;
451 *b = ai->effective_bottom_attach;
456 gtk_menu_class_init (GtkMenuClass *class)
458 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
459 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
460 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
461 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
462 GtkBindingSet *binding_set;
464 gobject_class->set_property = gtk_menu_set_property;
465 gobject_class->get_property = gtk_menu_get_property;
467 widget_class->destroy = gtk_menu_destroy;
468 widget_class->realize = gtk_menu_realize;
469 widget_class->unrealize = gtk_menu_unrealize;
470 widget_class->size_allocate = gtk_menu_size_allocate;
471 widget_class->show = gtk_menu_show;
472 widget_class->draw = gtk_menu_draw;
473 widget_class->scroll_event = gtk_menu_scroll;
474 widget_class->key_press_event = gtk_menu_key_press;
475 widget_class->button_press_event = gtk_menu_button_press;
476 widget_class->button_release_event = gtk_menu_button_release;
477 widget_class->motion_notify_event = gtk_menu_motion_notify;
478 widget_class->show_all = gtk_menu_show_all;
479 widget_class->enter_notify_event = gtk_menu_enter_notify;
480 widget_class->leave_notify_event = gtk_menu_leave_notify;
481 widget_class->style_updated = gtk_menu_style_updated;
482 widget_class->focus = gtk_menu_focus;
483 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
484 widget_class->grab_notify = gtk_menu_grab_notify;
485 widget_class->get_preferred_width = gtk_menu_get_preferred_width;
486 widget_class->get_preferred_height = gtk_menu_get_preferred_height;
487 widget_class->get_preferred_height_for_width = gtk_menu_get_preferred_height_for_width;
489 container_class->remove = gtk_menu_remove;
490 container_class->get_child_property = gtk_menu_get_child_property;
491 container_class->set_child_property = gtk_menu_set_child_property;
493 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
494 menu_shell_class->deactivate = gtk_menu_deactivate;
495 menu_shell_class->select_item = gtk_menu_select_item;
496 menu_shell_class->insert = gtk_menu_real_insert;
497 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
498 menu_shell_class->move_current = gtk_menu_move_current;
500 menu_signals[MOVE_SCROLL] =
501 g_signal_new_class_handler (I_("move-scroll"),
502 G_OBJECT_CLASS_TYPE (gobject_class),
503 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
504 G_CALLBACK (gtk_menu_real_move_scroll),
506 _gtk_marshal_VOID__ENUM,
508 GTK_TYPE_SCROLL_TYPE);
513 * The index of the currently selected menu item, or -1 if no
514 * menu item is selected.
518 g_object_class_install_property (gobject_class,
520 g_param_spec_int ("active",
522 P_("The currently selected menu item"),
524 GTK_PARAM_READWRITE));
527 * GtkMenu:accel-group:
529 * The accel group holding accelerators for the menu.
533 g_object_class_install_property (gobject_class,
535 g_param_spec_object ("accel-group",
537 P_("The accel group holding accelerators for the menu"),
538 GTK_TYPE_ACCEL_GROUP,
539 GTK_PARAM_READWRITE));
542 * GtkMenu:accel-path:
544 * An accel path used to conveniently construct accel paths of child items.
548 g_object_class_install_property (gobject_class,
550 g_param_spec_string ("accel-path",
552 P_("An accel path used to conveniently construct accel paths of child items"),
554 GTK_PARAM_READWRITE));
557 * GtkMenu:attach-widget:
559 * The widget the menu is attached to. Setting this property attaches
560 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
561 * use gtk_menu_attach_to_widget() directly.
565 g_object_class_install_property (gobject_class,
567 g_param_spec_object ("attach-widget",
569 P_("The widget the menu is attached to"),
571 GTK_PARAM_READWRITE));
573 g_object_class_install_property (gobject_class,
575 g_param_spec_string ("tearoff-title",
577 P_("A title that may be displayed by the window manager when this menu is torn-off"),
579 GTK_PARAM_READWRITE));
582 * GtkMenu:tearoff-state:
584 * A boolean that indicates whether the menu is torn-off.
588 g_object_class_install_property (gobject_class,
590 g_param_spec_boolean ("tearoff-state",
592 P_("A boolean that indicates whether the menu is torn-off"),
594 GTK_PARAM_READWRITE));
599 * The monitor the menu will be popped up on.
603 g_object_class_install_property (gobject_class,
605 g_param_spec_int ("monitor",
607 P_("The monitor the menu will be popped up on"),
609 GTK_PARAM_READWRITE));
611 gtk_widget_class_install_style_property (widget_class,
612 g_param_spec_int ("vertical-padding",
613 P_("Vertical Padding"),
614 P_("Extra space at the top and bottom of the menu"),
618 GTK_PARAM_READABLE));
621 * GtkMenu:reserve-toggle-size:
623 * A boolean that indicates whether the menu reserves space for
624 * toggles and icons, regardless of their actual presence.
626 * This property should only be changed from its default value
627 * for special-purposes such as tabular menus. Regular menus that
628 * are connected to a menu bar or context menus should reserve
629 * toggle space for consistency.
633 g_object_class_install_property (gobject_class,
634 PROP_RESERVE_TOGGLE_SIZE,
635 g_param_spec_boolean ("reserve-toggle-size",
636 P_("Reserve Toggle Size"),
637 P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
639 GTK_PARAM_READWRITE));
641 gtk_widget_class_install_style_property (widget_class,
642 g_param_spec_int ("horizontal-padding",
643 P_("Horizontal Padding"),
644 P_("Extra space at the left and right edges of the menu"),
648 GTK_PARAM_READABLE));
650 gtk_widget_class_install_style_property (widget_class,
651 g_param_spec_int ("vertical-offset",
652 P_("Vertical Offset"),
653 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
657 GTK_PARAM_READABLE));
659 gtk_widget_class_install_style_property (widget_class,
660 g_param_spec_int ("horizontal-offset",
661 P_("Horizontal Offset"),
662 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
666 GTK_PARAM_READABLE));
668 gtk_widget_class_install_style_property (widget_class,
669 g_param_spec_boolean ("double-arrows",
671 P_("When scrolling, always show both arrows."),
673 GTK_PARAM_READABLE));
676 * GtkMenu:arrow-placement:
678 * Indicates where scroll arrows should be placed.
682 gtk_widget_class_install_style_property (widget_class,
683 g_param_spec_enum ("arrow-placement",
684 P_("Arrow Placement"),
685 P_("Indicates where scroll arrows should be placed"),
686 GTK_TYPE_ARROW_PLACEMENT,
688 GTK_PARAM_READABLE));
690 gtk_container_class_install_child_property (container_class,
691 CHILD_PROP_LEFT_ATTACH,
692 g_param_spec_int ("left-attach",
694 P_("The column number to attach the left side of the child to"),
696 GTK_PARAM_READWRITE));
698 gtk_container_class_install_child_property (container_class,
699 CHILD_PROP_RIGHT_ATTACH,
700 g_param_spec_int ("right-attach",
702 P_("The column number to attach the right side of the child to"),
704 GTK_PARAM_READWRITE));
706 gtk_container_class_install_child_property (container_class,
707 CHILD_PROP_TOP_ATTACH,
708 g_param_spec_int ("top-attach",
710 P_("The row number to attach the top of the child to"),
712 GTK_PARAM_READWRITE));
714 gtk_container_class_install_child_property (container_class,
715 CHILD_PROP_BOTTOM_ATTACH,
716 g_param_spec_int ("bottom-attach",
718 P_("The row number to attach the bottom of the child to"),
720 GTK_PARAM_READWRITE));
723 * GtkMenu::arrow-scaling
725 * Arbitrary constant to scale down the size of the scroll arrow.
729 gtk_widget_class_install_style_property (widget_class,
730 g_param_spec_float ("arrow-scaling",
732 P_("Arbitrary constant to scale down the size of the scroll arrow"),
734 GTK_PARAM_READABLE));
736 binding_set = gtk_binding_set_by_class (class);
737 gtk_binding_entry_add_signal (binding_set,
739 I_("move-current"), 1,
740 GTK_TYPE_MENU_DIRECTION_TYPE,
742 gtk_binding_entry_add_signal (binding_set,
745 GTK_TYPE_MENU_DIRECTION_TYPE,
747 gtk_binding_entry_add_signal (binding_set,
750 GTK_TYPE_MENU_DIRECTION_TYPE,
752 gtk_binding_entry_add_signal (binding_set,
755 GTK_TYPE_MENU_DIRECTION_TYPE,
757 gtk_binding_entry_add_signal (binding_set,
760 GTK_TYPE_MENU_DIRECTION_TYPE,
761 GTK_MENU_DIR_PARENT);
762 gtk_binding_entry_add_signal (binding_set,
765 GTK_TYPE_MENU_DIRECTION_TYPE,
766 GTK_MENU_DIR_PARENT);
767 gtk_binding_entry_add_signal (binding_set,
770 GTK_TYPE_MENU_DIRECTION_TYPE,
772 gtk_binding_entry_add_signal (binding_set,
775 GTK_TYPE_MENU_DIRECTION_TYPE,
777 gtk_binding_entry_add_signal (binding_set,
780 GTK_TYPE_SCROLL_TYPE,
782 gtk_binding_entry_add_signal (binding_set,
785 GTK_TYPE_SCROLL_TYPE,
787 gtk_binding_entry_add_signal (binding_set,
790 GTK_TYPE_SCROLL_TYPE,
792 gtk_binding_entry_add_signal (binding_set,
795 GTK_TYPE_SCROLL_TYPE,
797 gtk_binding_entry_add_signal (binding_set,
800 GTK_TYPE_SCROLL_TYPE,
802 gtk_binding_entry_add_signal (binding_set,
803 GDK_KEY_KP_Page_Up, 0,
805 GTK_TYPE_SCROLL_TYPE,
807 gtk_binding_entry_add_signal (binding_set,
808 GDK_KEY_Page_Down, 0,
810 GTK_TYPE_SCROLL_TYPE,
811 GTK_SCROLL_PAGE_DOWN);
812 gtk_binding_entry_add_signal (binding_set,
813 GDK_KEY_KP_Page_Down, 0,
815 GTK_TYPE_SCROLL_TYPE,
816 GTK_SCROLL_PAGE_DOWN);
818 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
823 gtk_menu_set_property (GObject *object,
828 GtkMenu *menu = GTK_MENU (object);
833 gtk_menu_set_active (menu, g_value_get_int (value));
835 case PROP_ACCEL_GROUP:
836 gtk_menu_set_accel_group (menu, g_value_get_object (value));
838 case PROP_ACCEL_PATH:
839 gtk_menu_set_accel_path (menu, g_value_get_string (value));
841 case PROP_ATTACH_WIDGET:
845 widget = gtk_menu_get_attach_widget (menu);
847 gtk_menu_detach (menu);
849 widget = (GtkWidget*) g_value_get_object (value);
851 gtk_menu_attach_to_widget (menu, widget, NULL);
854 case PROP_TEAROFF_STATE:
855 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
857 case PROP_TEAROFF_TITLE:
858 gtk_menu_set_title (menu, g_value_get_string (value));
861 gtk_menu_set_monitor (menu, g_value_get_int (value));
863 case PROP_RESERVE_TOGGLE_SIZE:
864 gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
867 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
873 gtk_menu_get_property (GObject *object,
878 GtkMenu *menu = GTK_MENU (object);
883 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->priv->children, gtk_menu_get_active (menu)));
885 case PROP_ACCEL_GROUP:
886 g_value_set_object (value, gtk_menu_get_accel_group (menu));
888 case PROP_ACCEL_PATH:
889 g_value_set_string (value, gtk_menu_get_accel_path (menu));
891 case PROP_ATTACH_WIDGET:
892 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
894 case PROP_TEAROFF_STATE:
895 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
897 case PROP_TEAROFF_TITLE:
898 g_value_set_string (value, gtk_menu_get_title (menu));
901 g_value_set_int (value, gtk_menu_get_monitor (menu));
903 case PROP_RESERVE_TOGGLE_SIZE:
904 g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
907 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
913 gtk_menu_set_child_property (GtkContainer *container,
919 GtkMenu *menu = GTK_MENU (container);
920 AttachInfo *ai = get_attach_info (child);
924 case CHILD_PROP_LEFT_ATTACH:
925 ai->left_attach = g_value_get_int (value);
927 case CHILD_PROP_RIGHT_ATTACH:
928 ai->right_attach = g_value_get_int (value);
930 case CHILD_PROP_TOP_ATTACH:
931 ai->top_attach = g_value_get_int (value);
933 case CHILD_PROP_BOTTOM_ATTACH:
934 ai->bottom_attach = g_value_get_int (value);
938 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
942 menu_queue_resize (menu);
946 gtk_menu_get_child_property (GtkContainer *container,
952 AttachInfo *ai = get_attach_info (child);
956 case CHILD_PROP_LEFT_ATTACH:
957 g_value_set_int (value, ai->left_attach);
959 case CHILD_PROP_RIGHT_ATTACH:
960 g_value_set_int (value, ai->right_attach);
962 case CHILD_PROP_TOP_ATTACH:
963 g_value_set_int (value, ai->top_attach);
965 case CHILD_PROP_BOTTOM_ATTACH:
966 g_value_set_int (value, ai->bottom_attach);
970 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
976 gtk_menu_window_event (GtkWidget *window,
980 gboolean handled = FALSE;
982 g_object_ref (window);
988 case GDK_KEY_RELEASE:
989 handled = gtk_widget_event (menu, event);
995 g_object_unref (window);
996 g_object_unref (menu);
1002 gtk_menu_init (GtkMenu *menu)
1004 GtkMenuPrivate *priv;
1005 GtkStyleContext *context;
1007 priv = G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
1011 priv->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1012 "type", GTK_WINDOW_POPUP,
1015 "signal::event", gtk_menu_window_event, menu,
1016 "signal::destroy", gtk_widget_destroyed, &priv->toplevel,
1018 gtk_window_set_resizable (GTK_WINDOW (priv->toplevel), FALSE);
1019 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->toplevel), 0);
1021 /* Refloat the menu, so that reference counting for the menu isn't
1022 * affected by it being a child of the toplevel
1024 g_object_force_floating (G_OBJECT (menu));
1025 priv->needs_destruction_ref = TRUE;
1027 priv->monitor_num = -1;
1029 context = gtk_widget_get_style_context (GTK_WIDGET (menu));
1030 gtk_style_context_add_class (context, GTK_STYLE_CLASS_MENU);
1034 gtk_menu_destroy (GtkWidget *widget)
1036 GtkMenu *menu = GTK_MENU (widget);
1037 GtkMenuPrivate *priv = menu->priv;
1038 GtkMenuAttachData *data;
1040 gtk_menu_remove_scroll_timeout (menu);
1042 data = g_object_get_data (G_OBJECT (widget), attach_data_key);
1044 gtk_menu_detach (menu);
1046 gtk_menu_stop_navigating_submenu (menu);
1048 if (priv->old_active_menu_item)
1049 g_clear_object (&priv->old_active_menu_item);
1051 /* Add back the reference count for being a child */
1052 if (priv->needs_destruction_ref)
1054 priv->needs_destruction_ref = FALSE;
1055 g_object_ref (widget);
1058 if (priv->accel_group)
1059 g_clear_object (&priv->accel_group);
1062 gtk_widget_destroy (priv->toplevel);
1064 if (priv->tearoff_window)
1065 gtk_widget_destroy (priv->tearoff_window);
1069 g_free (priv->heights);
1070 priv->heights = NULL;
1075 g_free (priv->title);
1079 if (priv->position_func_data_destroy)
1081 priv->position_func_data_destroy (priv->position_func_data);
1082 priv->position_func_data = NULL;
1083 priv->position_func_data_destroy = NULL;
1086 GTK_WIDGET_CLASS (gtk_menu_parent_class)->destroy (widget);
1090 menu_change_screen (GtkMenu *menu,
1091 GdkScreen *new_screen)
1093 GtkMenuPrivate *priv = menu->priv;
1095 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1097 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1103 gtk_window_set_screen (GTK_WINDOW (priv->tearoff_window), new_screen);
1104 gtk_menu_position (menu, TRUE);
1107 gtk_window_set_screen (GTK_WINDOW (priv->toplevel), new_screen);
1108 priv->monitor_num = -1;
1112 attach_widget_screen_changed (GtkWidget *attach_widget,
1113 GdkScreen *previous_screen,
1116 if (gtk_widget_has_screen (attach_widget) &&
1117 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1118 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1122 gtk_menu_attach_to_widget (GtkMenu *menu,
1123 GtkWidget *attach_widget,
1124 GtkMenuDetachFunc detacher)
1126 GtkMenuAttachData *data;
1129 g_return_if_fail (GTK_IS_MENU (menu));
1130 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1132 /* keep this function in sync with gtk_widget_set_parent() */
1133 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1136 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1137 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1141 g_object_ref_sink (menu);
1143 data = g_slice_new (GtkMenuAttachData);
1144 data->attach_widget = attach_widget;
1146 g_signal_connect (attach_widget, "screen-changed",
1147 G_CALLBACK (attach_widget_screen_changed), menu);
1148 attach_widget_screen_changed (attach_widget, NULL, menu);
1150 data->detacher = detacher;
1151 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1152 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1153 if (!g_list_find (list, menu))
1154 list = g_list_prepend (list, menu);
1156 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1157 (GDestroyNotify) g_list_free);
1159 if (gtk_widget_get_state_flags (GTK_WIDGET (menu)) != 0)
1160 gtk_widget_set_state_flags (GTK_WIDGET (menu), 0, TRUE);
1162 /* we don't need to set the style here, since
1163 * we are a toplevel widget.
1166 /* Fallback title for menu comes from attach widget */
1167 gtk_menu_update_title (menu);
1169 g_object_notify (G_OBJECT (menu), "attach-widget");
1173 gtk_menu_get_attach_widget (GtkMenu *menu)
1175 GtkMenuAttachData *data;
1177 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1179 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1181 return data->attach_widget;
1186 gtk_menu_detach (GtkMenu *menu)
1188 GtkMenuAttachData *data;
1191 g_return_if_fail (GTK_IS_MENU (menu));
1193 /* keep this function in sync with gtk_widget_unparent() */
1194 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1197 g_warning ("gtk_menu_detach(): menu is not attached");
1200 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1202 g_signal_handlers_disconnect_by_func (data->attach_widget,
1203 (gpointer) attach_widget_screen_changed,
1207 data->detacher (data->attach_widget, menu);
1208 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1209 list = g_list_remove (list, menu);
1211 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1212 (GDestroyNotify) g_list_free);
1214 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1216 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1217 gtk_widget_unrealize (GTK_WIDGET (menu));
1219 g_slice_free (GtkMenuAttachData, data);
1221 /* Fallback title for menu comes from attach widget */
1222 gtk_menu_update_title (menu);
1224 g_object_unref (menu);
1228 gtk_menu_remove (GtkContainer *container,
1231 GtkMenu *menu = GTK_MENU (container);
1232 GtkMenuPrivate *priv = menu->priv;
1234 /* Clear out old_active_menu_item if it matches the item we are removing */
1235 if (priv->old_active_menu_item == widget)
1236 g_clear_object (&priv->old_active_menu_item);
1238 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1240 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1242 menu_queue_resize (menu);
1248 return g_object_new (GTK_TYPE_MENU, NULL);
1252 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1256 GtkMenu *menu = GTK_MENU (menu_shell);
1257 GtkMenuPrivate *priv = menu->priv;
1258 AttachInfo *ai = get_attach_info (child);
1260 ai->left_attach = -1;
1261 ai->right_attach = -1;
1262 ai->top_attach = -1;
1263 ai->bottom_attach = -1;
1265 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1266 gtk_widget_set_parent_window (child, priv->bin_window);
1268 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1270 menu_queue_resize (menu);
1274 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1276 GtkMenuPrivate *priv = menu->priv;
1280 widget = GTK_WIDGET (menu);
1285 cairo_surface_t *surface;
1286 cairo_pattern_t *pattern;
1289 priv->tearoff_active = FALSE;
1290 priv->saved_scroll_offset = priv->scroll_offset;
1292 window = gtk_widget_get_window (priv->tearoff_window);
1293 width = gdk_window_get_width (window);
1294 height = gdk_window_get_height (window);
1296 surface = gdk_window_create_similar_surface (window,
1297 CAIRO_CONTENT_COLOR,
1301 cr = cairo_create (surface);
1302 gdk_cairo_set_source_window (cr, window, 0, 0);
1306 gtk_widget_set_size_request (priv->tearoff_window, width, height);
1308 pattern = cairo_pattern_create_for_surface (surface);
1309 gdk_window_set_background_pattern (window, pattern);
1311 cairo_pattern_destroy (pattern);
1312 cairo_surface_destroy (surface);
1317 popup_grab_on_window (GdkWindow *window,
1318 GdkDevice *keyboard,
1320 guint32 activate_time)
1323 gdk_device_grab (keyboard, window,
1324 GDK_OWNERSHIP_WINDOW, TRUE,
1325 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1326 NULL, activate_time) != GDK_GRAB_SUCCESS)
1330 gdk_device_grab (pointer, window,
1331 GDK_OWNERSHIP_WINDOW, TRUE,
1332 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1333 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1334 GDK_POINTER_MOTION_MASK,
1335 NULL, activate_time) != GDK_GRAB_SUCCESS)
1338 gdk_device_ungrab (keyboard, activate_time);
1347 * gtk_menu_popup_for_device:
1348 * @menu: a #GtkMenu.
1349 * @device: (allow-none): a #GdkDevice
1350 * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1351 * menu item, or %NULL
1352 * @parent_menu_item: (allow-none): the menu item whose activation triggered
1353 * the popup, or %NULL
1354 * @func: (allow-none): a user supplied function used to position the menu,
1356 * @data: (allow-none): user supplied data to be passed to @func
1357 * @destroy: (allow-none): destroy notify for @data
1358 * @button: the mouse button which was pressed to initiate the event
1359 * @activate_time: the time at which the activation event occurred
1361 * Displays a menu and makes it available for selection.
1363 * Applications can use this function to display context-sensitive menus,
1364 * and will typically supply %NULL for the @parent_menu_shell,
1365 * @parent_menu_item, @func, @data and @destroy parameters. The default
1366 * menu positioning function will position the menu at the current position
1367 * of @device (or its corresponding pointer).
1369 * The @button parameter should be the mouse button pressed to initiate
1370 * the menu popup. If the menu popup was initiated by something other than
1371 * a mouse button press, such as a mouse button release or a keypress,
1372 * @button should be 0.
1374 * The @activate_time parameter is used to conflict-resolve initiation of
1375 * concurrent requests for mouse/keyboard grab requests. To function
1376 * properly, this needs to be the time stamp of the user event (such as
1377 * a mouse click or key press) that caused the initiation of the popup.
1378 * Only if no such event is available, gtk_get_current_event_time() can
1384 gtk_menu_popup_for_device (GtkMenu *menu,
1386 GtkWidget *parent_menu_shell,
1387 GtkWidget *parent_menu_item,
1388 GtkMenuPositionFunc func,
1390 GDestroyNotify destroy,
1392 guint32 activate_time)
1394 GtkMenuPrivate *priv = menu->priv;
1396 GtkWidget *xgrab_shell;
1398 GdkEvent *current_event;
1399 GtkMenuShell *menu_shell;
1400 gboolean grab_keyboard;
1401 GtkWidget *parent_toplevel;
1402 GdkDevice *keyboard, *pointer;
1404 g_return_if_fail (GTK_IS_MENU (menu));
1405 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1408 device = gtk_get_current_event_device ();
1412 GdkDisplay *display;
1413 GdkDeviceManager *device_manager;
1416 display = gtk_widget_get_display (GTK_WIDGET (menu));
1417 device_manager = gdk_display_get_device_manager (display);
1418 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1420 device = devices->data;
1422 g_list_free (devices);
1425 widget = GTK_WIDGET (menu);
1426 menu_shell = GTK_MENU_SHELL (menu);
1428 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1431 pointer = gdk_device_get_associated_device (device);
1436 keyboard = gdk_device_get_associated_device (device);
1439 menu_shell->priv->parent_menu_shell = parent_menu_shell;
1441 priv->seen_item_enter = FALSE;
1443 /* Find the last viewable ancestor, and make an X grab on it
1445 parent = GTK_WIDGET (menu);
1449 gboolean viewable = TRUE;
1450 GtkWidget *tmp = parent;
1454 if (!gtk_widget_get_mapped (tmp))
1459 tmp = gtk_widget_get_parent (tmp);
1463 xgrab_shell = parent;
1465 parent = GTK_MENU_SHELL (parent)->priv->parent_menu_shell;
1468 /* We want to receive events generated when we map the menu;
1469 * unfortunately, since there is probably already an implicit
1470 * grab in place from the button that the user used to pop up
1471 * the menu, we won't receive then -- in particular, the EnterNotify
1472 * when the menu pops up under the pointer.
1474 * If we are grabbing on a parent menu shell, no problem; just grab
1475 * on that menu shell first before popping up the window with
1476 * owner_events = TRUE.
1478 * When grabbing on the menu itself, things get more convoluted --
1479 * we do an explicit grab on a specially created window with
1480 * owner_events = TRUE, which we override further down with a
1481 * grab on the menu. (We can't grab on the menu until it is mapped;
1482 * we probably could just leave the grab on the other window,
1483 * with a little reorganization of the code in gtkmenu*).
1485 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1486 gtk_window_set_accept_focus (GTK_WINDOW (priv->toplevel), grab_keyboard);
1491 if (xgrab_shell && xgrab_shell != widget)
1493 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1495 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1496 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1501 GdkWindow *transfer_window;
1503 xgrab_shell = widget;
1504 transfer_window = menu_grab_transfer_window_get (menu);
1505 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1507 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1508 GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
1512 if (!GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab)
1514 /* We failed to make our pointer/keyboard grab.
1515 * Rather than leaving the user with a stuck up window,
1516 * we just abort here. Presumably the user will try again.
1518 menu_shell->priv->parent_menu_shell = NULL;
1519 menu_grab_transfer_window_destroy (menu);
1523 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1524 menu_shell->priv->active = TRUE;
1525 menu_shell->priv->button = button;
1527 /* If we are popping up the menu from something other than, a button
1528 * press then, as a heuristic, we ignore enter events for the menu
1529 * until we get a MOTION_NOTIFY.
1532 current_event = gtk_get_current_event ();
1535 if ((current_event->type != GDK_BUTTON_PRESS) &&
1536 (current_event->type != GDK_ENTER_NOTIFY))
1537 menu_shell->priv->ignore_enter = TRUE;
1539 gdk_event_free (current_event);
1542 menu_shell->priv->ignore_enter = TRUE;
1546 gtk_menu_tearoff_bg_copy (menu);
1548 gtk_menu_reparent (menu, priv->toplevel, FALSE);
1551 parent_toplevel = NULL;
1552 if (parent_menu_shell)
1553 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1554 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1556 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1558 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1561 /* Set transient for to get the right window group and parent */
1562 if (GTK_IS_WINDOW (parent_toplevel))
1563 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel),
1564 GTK_WINDOW (parent_toplevel));
1566 priv->parent_menu_item = parent_menu_item;
1567 priv->position_func = func;
1568 priv->position_func_data = data;
1569 priv->position_func_data_destroy = destroy;
1570 menu_shell->priv->activate_time = activate_time;
1572 /* We need to show the menu here rather in the init function
1573 * because code expects to be able to tell if the menu is onscreen
1574 * by looking at gtk_widget_get_visible (menu)
1576 gtk_widget_show (GTK_WIDGET (menu));
1578 /* Position the menu, possibly changing the size request
1580 gtk_menu_position (menu, TRUE);
1582 /* Compute the size of the toplevel and realize it so we
1583 * can scroll correctly.
1586 GtkRequisition tmp_request;
1587 GtkAllocation tmp_allocation = { 0, };
1589 /* Instead of trusting the menu position function to queue a
1590 * resize when the menu goes out of bounds, invalidate the cached
1593 gtk_widget_queue_resize (GTK_WIDGET (menu));
1594 gtk_widget_get_preferred_size (priv->toplevel, &tmp_request, NULL);
1596 tmp_allocation.width = tmp_request.width;
1597 tmp_allocation.height = tmp_request.height;
1599 gtk_widget_size_allocate (priv->toplevel, &tmp_allocation);
1601 gtk_widget_realize (GTK_WIDGET (menu));
1604 gtk_menu_scroll_to (menu, priv->scroll_offset);
1606 /* if no item is selected, select the first one */
1607 if (!menu_shell->priv->active_menu_item)
1609 gboolean touchscreen_mode;
1611 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1612 "gtk-touchscreen-mode", &touchscreen_mode,
1615 if (touchscreen_mode)
1616 gtk_menu_shell_select_first (menu_shell, TRUE);
1619 /* Once everything is set up correctly, map the toplevel */
1620 gtk_widget_show (priv->toplevel);
1622 if (xgrab_shell == widget)
1623 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1625 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1627 if (parent_menu_shell)
1629 gboolean keyboard_mode;
1631 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1632 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1634 else if (menu_shell->priv->button == 0) /* a keynav-activated context menu */
1635 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1637 _gtk_menu_shell_update_mnemonics (menu_shell);
1642 * @menu: a #GtkMenu.
1643 * @parent_menu_shell: (allow-none): the menu shell containing the
1644 * triggering menu item, or %NULL
1645 * @parent_menu_item: (allow-none): the menu item whose activation
1646 * triggered the popup, or %NULL
1647 * @func: (allow-none): a user supplied function used to position
1648 * the menu, or %NULL
1649 * @data: (allow-none): user supplied data to be passed to @func.
1650 * @button: the mouse button which was pressed to initiate the event.
1651 * @activate_time: the time at which the activation event occurred.
1653 * Displays a menu and makes it available for selection.
1655 * Applications can use this function to display context-sensitive
1656 * menus, and will typically supply %NULL for the @parent_menu_shell,
1657 * @parent_menu_item, @func and @data parameters. The default menu
1658 * positioning function will position the menu at the current mouse
1661 * The @button parameter should be the mouse button pressed to initiate
1662 * the menu popup. If the menu popup was initiated by something other
1663 * than a mouse button press, such as a mouse button release or a keypress,
1664 * @button should be 0.
1666 * The @activate_time parameter is used to conflict-resolve initiation
1667 * of concurrent requests for mouse/keyboard grab requests. To function
1668 * properly, this needs to be the timestamp of the user event (such as
1669 * a mouse click or key press) that caused the initiation of the popup.
1670 * Only if no such event is available, gtk_get_current_event_time() can
1674 gtk_menu_popup (GtkMenu *menu,
1675 GtkWidget *parent_menu_shell,
1676 GtkWidget *parent_menu_item,
1677 GtkMenuPositionFunc func,
1680 guint32 activate_time)
1682 g_return_if_fail (GTK_IS_MENU (menu));
1684 gtk_menu_popup_for_device (menu,
1689 button, activate_time);
1693 gtk_menu_popdown (GtkMenu *menu)
1695 GtkMenuPrivate *priv;
1696 GtkMenuShell *menu_shell;
1699 g_return_if_fail (GTK_IS_MENU (menu));
1701 menu_shell = GTK_MENU_SHELL (menu);
1704 menu_shell->priv->parent_menu_shell = NULL;
1705 menu_shell->priv->active = FALSE;
1706 menu_shell->priv->ignore_enter = FALSE;
1708 priv->have_position = FALSE;
1710 gtk_menu_stop_scrolling (menu);
1711 gtk_menu_stop_navigating_submenu (menu);
1713 if (menu_shell->priv->active_menu_item)
1715 if (priv->old_active_menu_item)
1716 g_object_unref (priv->old_active_menu_item);
1717 priv->old_active_menu_item = menu_shell->priv->active_menu_item;
1718 g_object_ref (priv->old_active_menu_item);
1721 gtk_menu_shell_deselect (menu_shell);
1723 /* The X Grab, if present, will automatically be removed
1724 * when we hide the window
1728 gtk_widget_hide (priv->toplevel);
1729 gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel), NULL);
1732 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1736 gtk_widget_set_size_request (priv->tearoff_window, -1, -1);
1738 if (gtk_bin_get_child (GTK_BIN (priv->toplevel)))
1740 gtk_menu_reparent (menu, priv->tearoff_hbox, TRUE);
1744 /* We popped up the menu from the tearoff, so we need to
1745 * release the grab - we aren't actually hiding the menu.
1747 if (menu_shell->priv->have_xgrab && pointer)
1749 GdkDevice *keyboard;
1751 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1752 keyboard = gdk_device_get_associated_device (pointer);
1755 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1759 /* gtk_menu_popdown is called each time a menu item is selected from
1760 * a torn off menu. Only scroll back to the saved position if the
1761 * non-tearoff menu was popped down.
1763 if (!priv->tearoff_active)
1764 gtk_menu_scroll_to (menu, priv->saved_scroll_offset);
1765 priv->tearoff_active = TRUE;
1768 gtk_widget_hide (GTK_WIDGET (menu));
1770 menu_shell->priv->have_xgrab = FALSE;
1773 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1775 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1777 menu_grab_transfer_window_destroy (menu);
1781 gtk_menu_get_active (GtkMenu *menu)
1783 GtkMenuPrivate *priv = menu->priv;
1787 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1789 if (!priv->old_active_menu_item)
1792 children = GTK_MENU_SHELL (menu)->priv->children;
1796 child = children->data;
1797 children = children->next;
1799 if (gtk_bin_get_child (GTK_BIN (child)))
1804 priv->old_active_menu_item = child;
1805 if (priv->old_active_menu_item)
1806 g_object_ref (priv->old_active_menu_item);
1809 return priv->old_active_menu_item;
1813 gtk_menu_set_active (GtkMenu *menu,
1816 GtkMenuPrivate *priv = menu->priv;
1820 g_return_if_fail (GTK_IS_MENU (menu));
1822 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->priv->children, index);
1825 child = tmp_list->data;
1826 if (gtk_bin_get_child (GTK_BIN (child)))
1828 if (priv->old_active_menu_item)
1829 g_object_unref (priv->old_active_menu_item);
1830 priv->old_active_menu_item = child;
1831 g_object_ref (priv->old_active_menu_item);
1837 * gtk_menu_set_accel_group:
1838 * @accel_group: (allow-none):
1841 gtk_menu_set_accel_group (GtkMenu *menu,
1842 GtkAccelGroup *accel_group)
1844 GtkMenuPrivate *priv = menu->priv;
1845 g_return_if_fail (GTK_IS_MENU (menu));
1847 if (priv->accel_group != accel_group)
1849 if (priv->accel_group)
1850 g_object_unref (priv->accel_group);
1851 priv->accel_group = accel_group;
1852 if (priv->accel_group)
1853 g_object_ref (priv->accel_group);
1854 _gtk_menu_refresh_accel_paths (menu, TRUE);
1859 gtk_menu_get_accel_group (GtkMenu *menu)
1861 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1863 return menu->priv->accel_group;
1867 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1870 /* Menu items chain here to figure whether they can activate their
1871 * accelerators. Unlike ordinary widgets, menus allow accel
1872 * activation even if invisible since that's the usual case for
1873 * submenus/popup-menus. however, the state of the attach widget
1874 * affects the "activeness" of the menu.
1876 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1879 return gtk_widget_can_activate_accel (awidget, signal_id);
1881 return gtk_widget_is_sensitive (widget);
1885 * gtk_menu_set_accel_path
1886 * @menu: a valid #GtkMenu
1887 * @accel_path: (allow-none): a valid accelerator path
1889 * Sets an accelerator path for this menu from which accelerator paths
1890 * for its immediate children, its menu items, can be constructed.
1891 * The main purpose of this function is to spare the programmer the
1892 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1893 * each menu item that should support runtime user changable accelerators.
1894 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1895 * each menu item of this menu, that contains a label describing its
1896 * purpose, automatically gets an accel path assigned.
1898 * For example, a menu containing menu items "New" and "Exit", will, after
1899 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1900 * has been called, assign its items the accel paths:
1901 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1903 * Assigning accel paths to menu items then enables the user to change
1904 * their accelerators at runtime. More details about accelerator paths
1905 * and their default setups can be found at gtk_accel_map_add_entry().
1907 * Note that @accel_path string will be stored in a #GQuark. Therefore,
1908 * if you pass a static string, you can save some memory by interning
1909 * it first with g_intern_static_string().
1912 gtk_menu_set_accel_path (GtkMenu *menu,
1913 const gchar *accel_path)
1915 GtkMenuPrivate *priv = menu->priv;
1916 g_return_if_fail (GTK_IS_MENU (menu));
1919 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1921 /* FIXME: accel_path should be defined as const gchar* */
1922 priv->accel_path = (gchar*)g_intern_string (accel_path);
1923 if (priv->accel_path)
1924 _gtk_menu_refresh_accel_paths (menu, FALSE);
1928 * gtk_menu_get_accel_path
1929 * @menu: a valid #GtkMenu
1931 * Retrieves the accelerator path set on the menu.
1933 * Returns: the accelerator path set on the menu.
1938 gtk_menu_get_accel_path (GtkMenu *menu)
1940 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1942 return menu->priv->accel_path;
1947 gboolean group_changed;
1951 refresh_accel_paths_foreach (GtkWidget *widget,
1954 GtkMenuPrivate *priv;
1955 AccelPropagation *prop = data;
1957 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1959 priv = prop->menu->priv;
1960 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1963 prop->group_changed);
1968 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1969 gboolean group_changed)
1971 GtkMenuPrivate *priv = menu->priv;
1972 g_return_if_fail (GTK_IS_MENU (menu));
1974 if (priv->accel_path && priv->accel_group)
1976 AccelPropagation prop;
1979 prop.group_changed = group_changed;
1980 gtk_container_foreach (GTK_CONTAINER (menu),
1981 refresh_accel_paths_foreach,
1987 gtk_menu_reposition (GtkMenu *menu)
1989 g_return_if_fail (GTK_IS_MENU (menu));
1991 if (!menu->priv->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
1992 gtk_menu_position (menu, FALSE);
1996 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1999 g_return_if_fail (GTK_IS_MENU (menu));
2001 if (adjustment->value != menu->priv->scroll_offset)
2002 gtk_menu_scroll_to (menu, adjustment->value);
2006 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2009 GtkMenuPrivate *priv = menu->priv;
2010 GdkGeometry geometry_hints;
2012 if (!priv->tearoff_window)
2015 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2017 GtkRequisition requisition;
2019 gtk_widget_get_preferred_size (priv->tearoff_scrollbar,
2020 &requisition, NULL);
2021 width += requisition.width;
2024 geometry_hints.min_width = width;
2025 geometry_hints.max_width = width;
2027 geometry_hints.min_height = 0;
2028 geometry_hints.max_height = priv->requested_height;
2030 gtk_window_set_geometry_hints (GTK_WINDOW (priv->tearoff_window),
2033 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2037 gtk_menu_update_title (GtkMenu *menu)
2039 GtkMenuPrivate *priv = menu->priv;
2041 if (priv->tearoff_window)
2044 GtkWidget *attach_widget;
2046 title = gtk_menu_get_title (menu);
2049 attach_widget = gtk_menu_get_attach_widget (menu);
2050 if (GTK_IS_MENU_ITEM (attach_widget))
2052 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2053 if (GTK_IS_LABEL (child))
2054 title = gtk_label_get_text (GTK_LABEL (child));
2059 gtk_window_set_title (GTK_WINDOW (priv->tearoff_window), title);
2064 gtk_menu_get_toplevel (GtkWidget *menu)
2066 GtkWidget *attach, *toplevel;
2068 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2070 if (GTK_IS_MENU_ITEM (attach))
2071 attach = gtk_widget_get_parent (attach);
2073 if (GTK_IS_MENU (attach))
2074 return gtk_menu_get_toplevel (attach);
2075 else if (GTK_IS_WIDGET (attach))
2077 toplevel = gtk_widget_get_toplevel (attach);
2078 if (gtk_widget_is_toplevel (toplevel))
2086 tearoff_window_destroyed (GtkWidget *widget,
2089 gtk_menu_set_tearoff_state (menu, FALSE);
2093 gtk_menu_set_tearoff_state (GtkMenu *menu,
2096 GtkMenuPrivate *priv = menu->priv;
2099 g_return_if_fail (GTK_IS_MENU (menu));
2101 if (priv->torn_off != torn_off)
2103 priv->torn_off = torn_off;
2104 priv->tearoff_active = torn_off;
2108 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2109 gtk_menu_popdown (menu);
2111 if (!priv->tearoff_window)
2113 GtkWidget *toplevel;
2115 priv->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2116 "type", GTK_WINDOW_TOPLEVEL,
2117 "screen", gtk_widget_get_screen (priv->toplevel),
2118 "app-paintable", TRUE,
2121 gtk_window_set_type_hint (GTK_WINDOW (priv->tearoff_window),
2122 GDK_WINDOW_TYPE_HINT_MENU);
2123 gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->tearoff_window), 0);
2124 g_signal_connect (priv->tearoff_window, "destroy",
2125 G_CALLBACK (tearoff_window_destroyed), menu);
2126 g_signal_connect (priv->tearoff_window, "event",
2127 G_CALLBACK (gtk_menu_window_event), menu);
2129 gtk_menu_update_title (menu);
2131 gtk_widget_realize (priv->tearoff_window);
2133 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2134 if (toplevel != NULL)
2135 gtk_window_set_transient_for (GTK_WINDOW (priv->tearoff_window),
2136 GTK_WINDOW (toplevel));
2138 priv->tearoff_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2139 gtk_container_add (GTK_CONTAINER (priv->tearoff_window),
2140 priv->tearoff_hbox);
2142 height = gdk_window_get_height (gtk_widget_get_window (GTK_WIDGET (menu)));
2143 priv->tearoff_adjustment = gtk_adjustment_new (0,
2144 0, priv->requested_height,
2148 g_object_connect (priv->tearoff_adjustment,
2149 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2151 priv->tearoff_scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, priv->tearoff_adjustment);
2153 gtk_box_pack_end (GTK_BOX (priv->tearoff_hbox),
2154 priv->tearoff_scrollbar,
2157 if (priv->tearoff_adjustment->upper > height)
2158 gtk_widget_show (priv->tearoff_scrollbar);
2160 gtk_widget_show (priv->tearoff_hbox);
2163 gtk_menu_reparent (menu, priv->tearoff_hbox, FALSE);
2165 /* Update menu->requisition */
2166 gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, NULL);
2168 gtk_menu_set_tearoff_hints (menu, gdk_window_get_width (gtk_widget_get_window (GTK_WIDGET (menu))));
2170 gtk_widget_realize (priv->tearoff_window);
2171 gtk_menu_position (menu, TRUE);
2173 gtk_widget_show (GTK_WIDGET (menu));
2174 gtk_widget_show (priv->tearoff_window);
2176 gtk_menu_scroll_to (menu, 0);
2181 gtk_widget_hide (GTK_WIDGET (menu));
2182 gtk_widget_hide (priv->tearoff_window);
2183 if (GTK_IS_CONTAINER (priv->toplevel))
2184 gtk_menu_reparent (menu, priv->toplevel, FALSE);
2185 gtk_widget_destroy (priv->tearoff_window);
2187 priv->tearoff_window = NULL;
2188 priv->tearoff_hbox = NULL;
2189 priv->tearoff_scrollbar = NULL;
2190 priv->tearoff_adjustment = NULL;
2193 g_object_notify (G_OBJECT (menu), "tearoff-state");
2198 * gtk_menu_get_tearoff_state:
2201 * Returns whether the menu is torn off.
2202 * See gtk_menu_set_tearoff_state().
2204 * Return value: %TRUE if the menu is currently torn off.
2207 gtk_menu_get_tearoff_state (GtkMenu *menu)
2209 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2211 return menu->priv->torn_off;
2215 * gtk_menu_set_title:
2217 * @title: a string containing the title for the menu
2219 * Sets the title string for the menu.
2221 * The title is displayed when the menu is shown as a tearoff
2222 * menu. If @title is %NULL, the menu will see if it is attached
2223 * to a parent menu item, and if so it will try to use the same
2224 * text as that menu item's label.
2227 gtk_menu_set_title (GtkMenu *menu,
2230 GtkMenuPrivate *priv = menu->priv;
2233 g_return_if_fail (GTK_IS_MENU (menu));
2235 old_title = priv->title;
2236 priv->title = g_strdup (title);
2239 gtk_menu_update_title (menu);
2240 g_object_notify (G_OBJECT (menu), "tearoff-title");
2244 * gtk_menu_get_title:
2247 * Returns the title of the menu. See gtk_menu_set_title().
2249 * Return value: the title of the menu, or %NULL if the menu
2250 * has no title set on it. This string is owned by GTK+
2251 * and should not be modified or freed.
2253 G_CONST_RETURN gchar *
2254 gtk_menu_get_title (GtkMenu *menu)
2256 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2258 return menu->priv->title;
2262 gtk_menu_reorder_child (GtkMenu *menu,
2266 GtkMenuShell *menu_shell;
2268 g_return_if_fail (GTK_IS_MENU (menu));
2269 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2271 menu_shell = GTK_MENU_SHELL (menu);
2273 if (g_list_find (menu_shell->priv->children, child))
2275 menu_shell->priv->children = g_list_remove (menu_shell->priv->children, child);
2276 menu_shell->priv->children = g_list_insert (menu_shell->priv->children, child, position);
2278 menu_queue_resize (menu);
2283 gtk_menu_style_updated (GtkWidget *widget)
2285 if (gtk_widget_get_realized (widget))
2287 GtkMenu *menu = GTK_MENU (widget);
2288 GtkMenuPrivate *priv = menu->priv;
2289 GtkStyleContext *context;
2291 context = gtk_widget_get_style_context (widget);
2293 gtk_style_context_set_background (context, priv->bin_window);
2294 gtk_style_context_set_background (context, priv->view_window);
2295 gtk_style_context_set_background (context, gtk_widget_get_window (widget));
2300 get_arrows_border (GtkMenu *menu,
2303 GtkMenuPrivate *priv = menu->priv;
2304 guint scroll_arrow_height;
2305 GtkArrowPlacement arrow_placement;
2307 gtk_widget_style_get (GTK_WIDGET (menu),
2308 "scroll-arrow-vlength", &scroll_arrow_height,
2309 "arrow_placement", &arrow_placement,
2312 switch (arrow_placement)
2314 case GTK_ARROWS_BOTH:
2315 border->top = priv->upper_arrow_visible ? scroll_arrow_height : 0;
2316 border->bottom = priv->lower_arrow_visible ? scroll_arrow_height : 0;
2319 case GTK_ARROWS_START:
2320 border->top = (priv->upper_arrow_visible ||
2321 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2325 case GTK_ARROWS_END:
2327 border->bottom = (priv->upper_arrow_visible ||
2328 priv->lower_arrow_visible) ? scroll_arrow_height : 0;
2332 border->left = border->right = 0;
2336 get_menu_border (GtkWidget *widget,
2339 GtkStyleContext *context;
2340 GtkStateFlags state;
2341 GtkBorder *border_width;
2343 context = gtk_widget_get_style_context (widget);
2344 state = gtk_widget_get_state_flags (widget);
2346 gtk_style_context_get (context, state,
2347 "border-width", &border_width,
2350 *border = *border_width;
2351 gtk_border_free (border_width);
2355 gtk_menu_realize (GtkWidget *widget)
2357 GtkMenu *menu = GTK_MENU (widget);
2358 GtkMenuPrivate *priv = menu->priv;
2359 GtkAllocation allocation;
2360 GtkStyleContext *context;
2362 GdkWindowAttr attributes;
2363 gint attributes_mask;
2367 guint vertical_padding;
2368 guint horizontal_padding;
2369 GtkBorder arrow_border, border;
2371 g_return_if_fail (GTK_IS_MENU (widget));
2373 gtk_widget_set_realized (widget, TRUE);
2375 gtk_widget_get_allocation (widget, &allocation);
2377 attributes.window_type = GDK_WINDOW_CHILD;
2378 attributes.x = allocation.x;
2379 attributes.y = allocation.y;
2380 attributes.width = allocation.width;
2381 attributes.height = allocation.height;
2382 attributes.wclass = GDK_INPUT_OUTPUT;
2383 attributes.visual = gtk_widget_get_visual (widget);
2384 attributes.event_mask = gtk_widget_get_events (widget);
2385 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2386 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2388 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2390 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2391 &attributes, attributes_mask);
2392 gtk_widget_set_window (widget, window);
2393 gdk_window_set_user_data (window, widget);
2395 get_menu_border (widget, &border);
2396 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2397 context = gtk_widget_get_style_context (widget);
2399 gtk_widget_style_get (GTK_WIDGET (menu),
2400 "vertical-padding", &vertical_padding,
2401 "horizontal-padding", &horizontal_padding,
2404 gtk_widget_get_allocation (widget, &allocation);
2406 attributes.x = border_width + border.left + horizontal_padding;
2407 attributes.y = border_width + border.top + vertical_padding;
2408 attributes.width = allocation.width -
2409 (2 * (border_width + horizontal_padding)) - border.left - border.right;
2410 attributes.height = allocation.height -
2411 (2 * (border_width + vertical_padding)) - border.top - border.bottom;
2413 get_arrows_border (menu, &arrow_border);
2414 attributes.y += arrow_border.top;
2415 attributes.height -= arrow_border.top;
2416 attributes.height -= arrow_border.bottom;
2418 attributes.width = MAX (1, attributes.width);
2419 attributes.height = MAX (1, attributes.height);
2421 priv->view_window = gdk_window_new (window,
2422 &attributes, attributes_mask);
2423 gdk_window_set_user_data (priv->view_window, menu);
2425 gtk_widget_get_allocation (widget, &allocation);
2429 attributes.width = allocation.width + (2 * (border_width + horizontal_padding)) +
2430 border.left + border.right;
2431 attributes.height = priv->requested_height - (2 * (border_width + vertical_padding)) +
2432 border.top + border.bottom;
2434 attributes.width = MAX (1, attributes.width);
2435 attributes.height = MAX (1, attributes.height);
2437 priv->bin_window = gdk_window_new (priv->view_window,
2438 &attributes, attributes_mask);
2439 gdk_window_set_user_data (priv->bin_window, menu);
2441 children = GTK_MENU_SHELL (menu)->priv->children;
2444 child = children->data;
2445 children = children->next;
2447 gtk_widget_set_parent_window (child, priv->bin_window);
2450 gtk_style_context_set_background (context, priv->bin_window);
2451 gtk_style_context_set_background (context, priv->view_window);
2452 gtk_style_context_set_background (context, window);
2454 if (GTK_MENU_SHELL (widget)->priv->active_menu_item)
2455 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2456 GTK_MENU_SHELL (widget)->priv->active_menu_item);
2458 gdk_window_show (priv->bin_window);
2459 gdk_window_show (priv->view_window);
2463 gtk_menu_focus (GtkWidget *widget,
2464 GtkDirectionType direction)
2466 /* A menu or its menu items cannot have focus */
2470 /* See notes in gtk_menu_popup() for information
2471 * about the "grab transfer window"
2474 menu_grab_transfer_window_get (GtkMenu *menu)
2476 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2479 GdkWindowAttr attributes;
2480 gint attributes_mask;
2482 attributes.x = -100;
2483 attributes.y = -100;
2484 attributes.width = 10;
2485 attributes.height = 10;
2486 attributes.window_type = GDK_WINDOW_TEMP;
2487 attributes.wclass = GDK_INPUT_ONLY;
2488 attributes.override_redirect = TRUE;
2489 attributes.event_mask = 0;
2491 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2493 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2494 &attributes, attributes_mask);
2495 gdk_window_set_user_data (window, menu);
2497 gdk_window_show (window);
2499 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2506 menu_grab_transfer_window_destroy (GtkMenu *menu)
2508 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2511 gdk_window_set_user_data (window, NULL);
2512 gdk_window_destroy (window);
2513 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2518 gtk_menu_unrealize (GtkWidget *widget)
2520 GtkMenu *menu = GTK_MENU (widget);
2521 GtkMenuPrivate *priv = menu->priv;
2523 menu_grab_transfer_window_destroy (menu);
2525 gdk_window_set_user_data (priv->view_window, NULL);
2526 gdk_window_destroy (priv->view_window);
2527 priv->view_window = NULL;
2529 gdk_window_set_user_data (priv->bin_window, NULL);
2530 gdk_window_destroy (priv->bin_window);
2531 priv->bin_window = NULL;
2533 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2537 calculate_line_heights (GtkMenu *menu,
2539 guint **ret_min_heights,
2540 guint **ret_nat_heights)
2542 GtkMenuPrivate *priv;
2543 GtkMenuShell *menu_shell;
2544 GtkWidget *child, *widget;
2546 guint horizontal_padding;
2555 widget = GTK_WIDGET (menu);
2556 menu_shell = GTK_MENU_SHELL (widget);
2558 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2559 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2560 n_heights = gtk_menu_get_n_rows (menu);
2561 n_columns = gtk_menu_get_n_columns (menu);
2562 avail_width = for_width - (2 * priv->toggle_size + priv->accel_size) * n_columns;
2564 gtk_widget_style_get (GTK_WIDGET (menu),
2565 "horizontal-padding", &horizontal_padding,
2568 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2569 avail_width -= (border_width + horizontal_padding + gtk_widget_get_style (widget)->xthickness) * 2;
2571 for (children = menu_shell->priv->children; children; children = children->next)
2576 gint child_min, child_nat;
2578 child = children->data;
2580 if (!gtk_widget_get_visible (child))
2583 get_effective_child_attach (child, &l, &r, &t, &b);
2585 part = avail_width / (r - l);
2587 gtk_widget_get_preferred_height_for_width (child, part,
2588 &child_min, &child_nat);
2590 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2592 part = MAX (child_min, toggle_size) / (b - t);
2593 min_heights[t] = MAX (min_heights[t], part);
2595 part = MAX (child_nat, toggle_size) / (b - t);
2596 nat_heights[t] = MAX (nat_heights[t], part);
2599 if (ret_min_heights)
2600 *ret_min_heights = min_heights;
2602 g_free (min_heights);
2604 if (ret_nat_heights)
2605 *ret_nat_heights = nat_heights;
2607 g_free (nat_heights);
2613 gtk_menu_size_allocate (GtkWidget *widget,
2614 GtkAllocation *allocation)
2617 GtkMenuPrivate *priv;
2618 GtkMenuShell *menu_shell;
2620 GtkAllocation child_allocation;
2625 guint vertical_padding;
2626 guint horizontal_padding;
2629 g_return_if_fail (GTK_IS_MENU (widget));
2630 g_return_if_fail (allocation != NULL);
2632 menu = GTK_MENU (widget);
2633 menu_shell = GTK_MENU_SHELL (widget);
2636 gtk_widget_set_allocation (widget, allocation);
2638 gtk_widget_style_get (GTK_WIDGET (menu),
2639 "vertical-padding", &vertical_padding,
2640 "horizontal-padding", &horizontal_padding,
2643 get_menu_border (widget, &border);
2644 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2646 g_free (priv->heights);
2647 priv->heights_length = calculate_line_heights (menu,
2652 /* refresh our cached height request */
2653 priv->requested_height = (2 * (border_width + vertical_padding)) +
2654 border.top + border.bottom;
2655 for (i = 0; i < priv->heights_length; i++)
2656 priv->requested_height += priv->heights[i];
2658 x = border_width + border.left + horizontal_padding;
2659 y = border_width + border.top + vertical_padding;
2660 width = allocation->width - (2 * (border_width + horizontal_padding)) -
2661 border.left - border.right;
2662 height = allocation->height - (2 * (border_width + vertical_padding)) -
2663 border.top - border.bottom;
2665 if (menu_shell->priv->active)
2666 gtk_menu_scroll_to (menu, priv->scroll_offset);
2668 if (!priv->tearoff_active)
2670 GtkBorder arrow_border;
2672 get_arrows_border (menu, &arrow_border);
2673 y += arrow_border.top;
2674 height -= arrow_border.top;
2675 height -= arrow_border.bottom;
2678 width = MAX (1, width);
2679 height = MAX (1, height);
2681 if (gtk_widget_get_realized (widget))
2683 gdk_window_move_resize (gtk_widget_get_window (widget),
2684 allocation->x, allocation->y,
2685 allocation->width, allocation->height);
2687 gdk_window_move_resize (priv->view_window, x, y, width, height);
2690 if (menu_shell->priv->children)
2692 gint base_width = width / gtk_menu_get_n_columns (menu);
2694 children = menu_shell->priv->children;
2697 child = children->data;
2698 children = children->next;
2700 if (gtk_widget_get_visible (child))
2705 get_effective_child_attach (child, &l, &r, &t, &b);
2707 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2710 tmp = gtk_menu_get_n_columns (menu) - l;
2711 l = gtk_menu_get_n_columns (menu) - r;
2715 child_allocation.width = (r - l) * base_width;
2716 child_allocation.height = 0;
2717 child_allocation.x = l * base_width;
2718 child_allocation.y = 0;
2720 for (i = 0; i < b; i++)
2723 child_allocation.y += priv->heights[i];
2725 child_allocation.height += priv->heights[i];
2728 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2731 gtk_widget_size_allocate (child, &child_allocation);
2732 gtk_widget_queue_draw (child);
2736 /* Resize the item window */
2737 if (gtk_widget_get_realized (widget))
2743 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2744 height += priv->heights[i];
2746 width = gtk_menu_get_n_columns (menu) * base_width;
2747 gdk_window_resize (priv->bin_window, width, height);
2750 if (priv->tearoff_active)
2752 if (height >= priv->requested_height)
2754 if (gtk_widget_get_visible (priv->tearoff_scrollbar))
2756 gtk_widget_hide (priv->tearoff_scrollbar);
2757 gtk_menu_set_tearoff_hints (menu, allocation->width);
2759 gtk_menu_scroll_to (menu, 0);
2764 priv->tearoff_adjustment->upper = priv->requested_height;
2765 priv->tearoff_adjustment->page_size = allocation->height;
2767 if (priv->tearoff_adjustment->value + priv->tearoff_adjustment->page_size >
2768 priv->tearoff_adjustment->upper)
2771 value = priv->tearoff_adjustment->upper - priv->tearoff_adjustment->page_size;
2774 gtk_menu_scroll_to (menu, value);
2777 gtk_adjustment_changed (priv->tearoff_adjustment);
2779 if (!gtk_widget_get_visible (priv->tearoff_scrollbar))
2781 gtk_widget_show (priv->tearoff_scrollbar);
2782 gtk_menu_set_tearoff_hints (menu, allocation->width);
2790 get_arrows_visible_area (GtkMenu *menu,
2791 GdkRectangle *border,
2792 GdkRectangle *upper,
2793 GdkRectangle *lower,
2796 GtkArrowPlacement arrow_placement;
2797 GtkWidget *widget = GTK_WIDGET (menu);
2799 guint vertical_padding;
2800 guint horizontal_padding;
2801 gint scroll_arrow_height;
2802 GtkBorder menu_border;
2804 gtk_widget_style_get (widget,
2805 "vertical-padding", &vertical_padding,
2806 "horizontal-padding", &horizontal_padding,
2807 "scroll-arrow-vlength", &scroll_arrow_height,
2808 "arrow-placement", &arrow_placement,
2811 get_menu_border (widget, &menu_border);
2812 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2813 border->x = border_width + menu_border.left + horizontal_padding;
2814 border->y = border_width + menu_border.top + vertical_padding;
2815 border->width = gdk_window_get_width (gtk_widget_get_window (widget));
2816 border->height = gdk_window_get_height (gtk_widget_get_window (widget));
2818 switch (arrow_placement)
2820 case GTK_ARROWS_BOTH:
2821 upper->x = border->x;
2822 upper->y = border->y;
2823 upper->width = border->width - 2 * border->x;
2824 upper->height = scroll_arrow_height;
2826 lower->x = border->x;
2827 lower->y = border->height - border->y - scroll_arrow_height;
2828 lower->width = border->width - 2 * border->x;
2829 lower->height = scroll_arrow_height;
2832 case GTK_ARROWS_START:
2833 upper->x = border->x;
2834 upper->y = border->y;
2835 upper->width = (border->width - 2 * border->x) / 2;
2836 upper->height = scroll_arrow_height;
2838 lower->x = border->x + upper->width;
2839 lower->y = border->y;
2840 lower->width = (border->width - 2 * border->x) / 2;
2841 lower->height = scroll_arrow_height;
2844 case GTK_ARROWS_END:
2845 upper->x = border->x;
2846 upper->y = border->height - border->y - scroll_arrow_height;
2847 upper->width = (border->width - 2 * border->x) / 2;
2848 upper->height = scroll_arrow_height;
2850 lower->x = border->x + upper->width;
2851 lower->y = border->height - border->y - scroll_arrow_height;
2852 lower->width = (border->width - 2 * border->x) / 2;
2853 lower->height = scroll_arrow_height;
2857 g_assert_not_reached();
2858 upper->x = upper->y = upper->width = upper->height = 0;
2859 lower->x = lower->y = lower->width = lower->height = 0;
2862 *arrow_space = scroll_arrow_height - menu_border.top - menu_border.bottom;
2866 gtk_menu_draw (GtkWidget *widget,
2870 GtkMenuPrivate *priv;
2871 GtkStyleContext *context;
2872 GdkRectangle border;
2877 GtkStateFlags state;
2878 GtkBorder menu_border;
2880 menu = GTK_MENU (widget);
2881 priv = gtk_menu_get_private (menu);
2882 context = gtk_widget_get_style_context (widget);
2883 window = gtk_widget_get_window (widget);
2884 state = gtk_widget_get_state_flags (widget);
2886 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2887 get_menu_border (widget, &menu_border);
2889 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
2891 gfloat arrow_scaling;
2894 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
2895 arrow_size = arrow_scaling * arrow_space;
2897 gtk_render_background (context, cr, 0, 0,
2898 gtk_widget_get_allocated_width (widget),
2899 gtk_widget_get_allocated_height (widget));
2900 gtk_render_frame (context, cr, 0, 0,
2901 gtk_widget_get_allocated_width (widget),
2902 gtk_widget_get_allocated_height (widget));
2904 gtk_style_context_save (context);
2905 gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
2907 if (priv->upper_arrow_visible && !priv->tearoff_active)
2909 gtk_style_context_save (context);
2910 gtk_style_context_set_state (context, priv->upper_arrow_state);
2912 gtk_render_background (context, cr,
2914 upper.width, upper.height);
2915 gtk_render_frame (context, cr,
2917 upper.width, upper.height);
2919 gtk_render_arrow (context, cr, 0,
2920 upper.x + (upper.width - arrow_size) / 2,
2921 upper.y + menu_border.top + (arrow_space - arrow_size) / 2,
2924 gtk_style_context_restore (context);
2927 if (priv->lower_arrow_visible && !priv->tearoff_active)
2929 gtk_style_context_save (context);
2930 gtk_style_context_set_state (context, priv->lower_arrow_state);
2932 gtk_render_background (context, cr,
2934 lower.width, lower.height);
2935 gtk_render_frame (context, cr,
2937 lower.width, lower.height);
2939 gtk_render_arrow (context, cr, G_PI,
2940 lower.x + (lower.width - arrow_size) / 2,
2941 lower.y + menu_border.top + (arrow_space - arrow_size) / 2,
2944 gtk_style_context_restore (context);
2947 gtk_style_context_restore (context);
2950 if (gtk_cairo_should_draw_window (cr, priv->bin_window))
2952 gint y = -border.y + priv->scroll_offset;
2955 gtk_cairo_transform_to_window (cr, widget, priv->bin_window);
2957 if (!priv->tearoff_active)
2959 GtkBorder arrow_border;
2961 get_arrows_border (menu, &arrow_border);
2962 y -= arrow_border.top;
2965 gtk_render_background (context, cr,
2967 border.width, border.height);
2968 gtk_render_frame (context, cr,
2970 border.width, border.height);
2975 GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
2981 gtk_menu_show (GtkWidget *widget)
2983 GtkMenu *menu = GTK_MENU (widget);
2985 _gtk_menu_refresh_accel_paths (menu, FALSE);
2987 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
2992 gtk_menu_get_preferred_width (GtkWidget *widget,
2997 GtkMenuShell *menu_shell;
2998 GtkMenuPrivate *priv;
3001 guint max_toggle_size;
3002 guint max_accel_width;
3003 guint horizontal_padding;
3005 gint child_min, child_nat;
3006 gint min_width, nat_width;
3009 menu = GTK_MENU (widget);
3010 menu_shell = GTK_MENU_SHELL (widget);
3013 min_width = nat_width = 0;
3015 max_toggle_size = 0;
3016 max_accel_width = 0;
3018 children = menu_shell->priv->children;
3025 child = children->data;
3026 children = children->next;
3028 if (! gtk_widget_get_visible (child))
3031 get_effective_child_attach (child, &l, &r, &t, &b);
3033 /* It's important to size_request the child
3034 * before doing the toggle size request, in
3035 * case the toggle size request depends on the size
3036 * request of a child of the child (e.g. for ImageMenuItem)
3038 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
3040 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3041 max_toggle_size = MAX (max_toggle_size, toggle_size);
3042 max_accel_width = MAX (max_accel_width,
3043 GTK_MENU_ITEM (child)->priv->accelerator_width);
3045 part = child_min / (r - l);
3046 min_width = MAX (min_width, part);
3048 part = child_nat / (r - l);
3049 nat_width = MAX (nat_width, part);
3052 /* If the menu doesn't include any images or check items
3053 * reserve the space so that all menus are consistent.
3054 * We only do this for 'ordinary' menus, not for combobox
3055 * menus or multi-column menus
3057 if (max_toggle_size == 0 &&
3058 gtk_menu_get_n_columns (menu) == 1 &&
3059 !priv->no_toggle_size)
3061 GtkStyleContext *context;
3062 GtkWidgetPath *menu_path, *check_path;
3063 guint toggle_spacing;
3064 guint indicator_size;
3066 context = gtk_widget_get_style_context (widget);
3067 menu_path = gtk_widget_path_copy (gtk_style_context_get_path (context));
3069 /* Create a GtkCheckMenuItem path, only to query indicator spacing */
3070 check_path = gtk_widget_path_copy (menu_path);
3071 gtk_widget_path_append_type (check_path, GTK_TYPE_CHECK_MENU_ITEM);
3073 gtk_style_context_set_path (context, check_path);
3074 gtk_widget_path_free (check_path);
3076 gtk_style_context_get_style (context,
3077 "toggle-spacing", &toggle_spacing,
3078 "indicator-size", &indicator_size,
3081 max_toggle_size = indicator_size + toggle_spacing;
3083 /* Restore real widget path */
3084 gtk_style_context_set_path (context, menu_path);
3085 gtk_widget_path_free (menu_path);
3088 min_width += 2 * max_toggle_size + max_accel_width;
3089 min_width *= gtk_menu_get_n_columns (menu);
3091 nat_width += 2 * max_toggle_size + max_accel_width;
3092 nat_width *= gtk_menu_get_n_columns (menu);
3094 gtk_widget_style_get (GTK_WIDGET (menu),
3095 "horizontal-padding", &horizontal_padding,
3098 get_menu_border (widget, &border);
3099 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3100 min_width += (2 * (border_width + horizontal_padding)) +
3101 border.left + border.right;
3102 nat_width += (2 * (border_width + horizontal_padding)) +
3103 border.top + border.bottom;
3105 priv->toggle_size = max_toggle_size;
3106 priv->accel_size = max_accel_width;
3109 *minimum_size = min_width;
3112 *natural_size = nat_width;
3114 /* Don't resize the tearoff if it is not active,
3115 * because it won't redraw (it is only a background pixmap).
3117 if (priv->tearoff_active)
3118 gtk_menu_set_tearoff_hints (menu, min_width);
3122 gtk_menu_get_preferred_height (GtkWidget *widget,
3128 /* Menus are height-for-width only, just return the height
3129 * for the minimum width
3131 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
3132 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
3136 gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
3141 GtkMenu *menu = GTK_MENU (widget);
3142 GtkMenuPrivate *priv = menu->priv;
3143 guint *min_heights, *nat_heights;
3144 guint vertical_padding, border_width;
3146 gint min_height, nat_height;
3148 gtk_widget_style_get (widget, "vertical-padding", &vertical_padding, NULL);
3149 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3151 min_height = nat_height = (border_width + vertical_padding + gtk_widget_get_style (widget)->ythickness) * 2;
3154 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3156 for (i = 0; i < n_heights; i++)
3158 min_height += min_heights[i];
3159 nat_height += nat_heights[i];
3162 if (priv->have_position)
3164 GdkScreen *screen = gtk_widget_get_screen (priv->toplevel);
3165 GdkRectangle monitor;
3167 gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
3169 if (priv->position_y + min_height > monitor.y + monitor.height)
3170 min_height = monitor.y + monitor.height - priv->position_y;
3172 if (priv->position_y + nat_height > monitor.y + monitor.height)
3173 nat_height = monitor.y + monitor.height - priv->position_y;
3175 if (priv->position_y < monitor.y)
3177 min_height -= monitor.y - priv->position_y;
3178 nat_height -= monitor.y - priv->position_y;
3183 *minimum_size = min_height;
3186 *natural_size = nat_height;
3188 g_free (min_heights);
3189 g_free (nat_heights);
3195 gtk_menu_button_scroll (GtkMenu *menu,
3196 GdkEventButton *event)
3198 GtkMenuPrivate *priv = menu->priv;
3200 if (priv->upper_arrow_prelight || priv->lower_arrow_prelight)
3202 gboolean touchscreen_mode;
3204 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3205 "gtk-touchscreen-mode", &touchscreen_mode,
3208 if (touchscreen_mode)
3209 gtk_menu_handle_scrolling (menu,
3210 event->x_root, event->y_root,
3211 event->type == GDK_BUTTON_PRESS,
3221 pointer_in_menu_window (GtkWidget *widget,
3225 GtkMenu *menu = GTK_MENU (widget);
3226 GtkMenuPrivate *priv = menu->priv;
3227 GtkAllocation allocation;
3229 if (gtk_widget_get_mapped (priv->toplevel))
3231 GtkMenuShell *menu_shell;
3232 gint window_x, window_y;
3234 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
3235 &window_x, &window_y);
3237 gtk_widget_get_allocation (widget, &allocation);
3238 if (x_root >= window_x && x_root < window_x + allocation.width &&
3239 y_root >= window_y && y_root < window_y + allocation.height)
3242 menu_shell = GTK_MENU_SHELL (widget);
3244 if (GTK_IS_MENU (menu_shell->priv->parent_menu_shell))
3245 return pointer_in_menu_window (menu_shell->priv->parent_menu_shell,
3253 gtk_menu_button_press (GtkWidget *widget,
3254 GdkEventButton *event)
3256 if (event->type != GDK_BUTTON_PRESS)
3259 /* Don't pass down to menu shell for presses over scroll arrows
3261 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3264 /* Don't pass down to menu shell if a non-menuitem part of the menu
3265 * was clicked. The check for the event_widget being a GtkMenuShell
3266 * works because we have the pointer grabbed on menu_shell->window
3267 * with owner_events=TRUE, so all events that are either outside
3268 * the menu or on its border are delivered relative to
3269 * menu_shell->window.
3271 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3272 pointer_in_menu_window (widget, event->x_root, event->y_root))
3275 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3279 gtk_menu_button_release (GtkWidget *widget,
3280 GdkEventButton *event)
3282 GtkMenuPrivate *priv = GTK_MENU (widget)->priv;
3284 if (priv->ignore_button_release)
3286 priv->ignore_button_release = FALSE;
3290 if (event->type != GDK_BUTTON_RELEASE)
3293 /* Don't pass down to menu shell for releases over scroll arrows
3295 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3298 /* Don't pass down to menu shell if a non-menuitem part of the menu
3299 * was clicked (see comment in button_press()).
3301 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3302 pointer_in_menu_window (widget, event->x_root, event->y_root))
3304 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3305 * bail out early here so it is in a consistent state for the
3306 * next button_press/button_release in GtkMenuShell.
3309 if (GTK_MENU_SHELL (widget)->priv->active)
3310 GTK_MENU_SHELL (widget)->priv->button = 0;
3315 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3318 static const gchar *
3319 get_accel_path (GtkWidget *menu_item,
3324 GClosure *accel_closure;
3325 GtkAccelGroup *accel_group;
3327 path = _gtk_widget_get_accel_path (menu_item, locked);
3330 path = GTK_MENU_ITEM (menu_item)->priv->accel_path;
3336 label = gtk_bin_get_child (GTK_BIN (menu_item));
3338 if (GTK_IS_ACCEL_LABEL (label))
3340 g_object_get (label,
3341 "accel-closure", &accel_closure,
3345 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3347 *locked = gtk_accel_group_get_is_locked (accel_group);
3357 gtk_menu_key_press (GtkWidget *widget,
3360 GtkMenuShell *menu_shell;
3362 gboolean delete = FALSE;
3363 gboolean can_change_accels;
3364 gchar *accel = NULL;
3365 guint accel_key, accel_mods;
3366 GdkModifierType consumed_modifiers;
3367 GdkDisplay *display;
3369 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3370 g_return_val_if_fail (event != NULL, FALSE);
3372 menu_shell = GTK_MENU_SHELL (widget);
3373 menu = GTK_MENU (widget);
3375 gtk_menu_stop_navigating_submenu (menu);
3377 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3380 display = gtk_widget_get_display (widget);
3382 g_object_get (gtk_widget_get_settings (widget),
3383 "gtk-menu-bar-accel", &accel,
3384 "gtk-can-change-accels", &can_change_accels,
3387 if (accel && *accel)
3390 GdkModifierType mods = 0;
3392 gtk_accelerator_parse (accel, &keyval, &mods);
3395 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3397 /* FIXME this is wrong, needs to be in the global accel resolution
3398 * thing, to properly consider i18n etc., but that probably requires
3399 * AccelGroup changes etc.
3401 if (event->keyval == keyval && (mods & event->state) == mods)
3403 gtk_menu_shell_cancel (menu_shell);
3411 switch (event->keyval)
3413 case GDK_KEY_Delete:
3414 case GDK_KEY_KP_Delete:
3415 case GDK_KEY_BackSpace:
3422 /* Figure out what modifiers went into determining the key symbol */
3423 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3424 event->hardware_keycode,
3425 event->state, event->group,
3426 NULL, NULL, NULL, &consumed_modifiers);
3428 accel_key = gdk_keyval_to_lower (event->keyval);
3429 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3431 /* If lowercasing affects the keysym, then we need to include SHIFT
3432 * in the modifiers, we re-uppercase when we match against the keyval,
3433 * but display and save in caseless form.
3435 if (accel_key != event->keyval)
3436 accel_mods |= GDK_SHIFT_MASK;
3438 /* Modify the accelerators */
3439 if (can_change_accels &&
3440 menu_shell->priv->active_menu_item &&
3441 gtk_bin_get_child (GTK_BIN (menu_shell->priv->active_menu_item)) && /* no separators */
3442 GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu == NULL && /* no submenus */
3443 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3445 GtkWidget *menu_item = menu_shell->priv->active_menu_item;
3446 gboolean locked, replace_accels = TRUE;
3449 path = get_accel_path (menu_item, &locked);
3450 if (!path || locked)
3452 /* Can't change accelerators on menu_items without paths
3453 * (basically, those items are accelerator-locked).
3455 gtk_widget_error_bell (widget);
3461 /* For the keys that act to delete the current setting,
3462 * we delete the current setting if there is one, otherwise,
3463 * we set the key as the accelerator.
3469 if (gtk_accel_map_lookup_entry (path, &key) &&
3470 (key.accel_key || key.accel_mods))
3476 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3480 /* We failed, probably because this key is in use
3481 * and locked already.
3483 gtk_widget_error_bell (widget);
3492 check_threshold (GtkWidget *widget,
3501 ABS (start_x - x) > THRESHOLD ||
3502 ABS (start_y - y) > THRESHOLD;
3506 definitely_within_item (GtkWidget *widget,
3510 GdkWindow *window = GTK_MENU_ITEM (widget)->priv->event_window;
3513 w = gdk_window_get_width (window);
3514 h = gdk_window_get_height (window);
3517 check_threshold (widget, 0, 0, x, y) &&
3518 check_threshold (widget, w - 1, 0, x, y) &&
3519 check_threshold (widget, w - 1, h - 1, x, y) &&
3520 check_threshold (widget, 0, h - 1, x, y);
3524 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3526 GtkMenuPrivate *priv = menu->priv;
3528 return priv->navigation_height && priv->navigation_width;
3532 gtk_menu_motion_notify (GtkWidget *widget,
3533 GdkEventMotion *event)
3535 GtkWidget *menu_item;
3537 GtkMenuShell *menu_shell;
3540 gboolean need_enter;
3542 if (GTK_IS_MENU (widget))
3544 GtkMenuPrivate *priv = GTK_MENU(widget)->priv;
3546 if (priv->ignore_button_release)
3547 priv->ignore_button_release = FALSE;
3549 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3553 /* We received the event for one of two reasons:
3555 * a) We are the active menu, and did gtk_grab_add()
3556 * b) The widget is a child of ours, and the event was propagated
3558 * Since for computation of navigation regions, we want the menu which
3559 * is the parent of the menu item, for a), we need to find that menu,
3560 * which may be different from 'widget'.
3562 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3563 parent = gtk_widget_get_parent (menu_item);
3564 if (!GTK_IS_MENU_ITEM (menu_item) ||
3565 !GTK_IS_MENU (parent))
3568 menu_shell = GTK_MENU_SHELL (parent);
3569 menu = GTK_MENU (menu_shell);
3571 if (definitely_within_item (menu_item, event->x, event->y))
3572 menu_shell->priv->activate_time = 0;
3574 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->priv->ignore_enter);
3576 /* Check to see if we are within an active submenu's navigation region
3578 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3581 /* Make sure we pop down if we enter a non-selectable menu item, so we
3582 * don't show a submenu when the cursor is outside the stay-up triangle.
3584 if (!_gtk_menu_item_is_selectable (menu_item))
3586 /* We really want to deselect, but this gives the menushell code
3587 * a chance to do some bookkeeping about the menuitem.
3589 gtk_menu_shell_select_item (menu_shell, menu_item);
3595 /* The menu is now sensitive to enter events on its items, but
3596 * was previously sensitive. So we fake an enter event.
3598 menu_shell->priv->ignore_enter = FALSE;
3600 if (event->x >= 0 && event->x < gdk_window_get_width (event->window) &&
3601 event->y >= 0 && event->y < gdk_window_get_height (event->window))
3603 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3606 send_event->crossing.window = g_object_ref (event->window);
3607 send_event->crossing.time = event->time;
3608 send_event->crossing.send_event = TRUE;
3609 send_event->crossing.x_root = event->x_root;
3610 send_event->crossing.y_root = event->y_root;
3611 send_event->crossing.x = event->x;
3612 send_event->crossing.y = event->y;
3613 send_event->crossing.state = event->state;
3614 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3616 /* We send the event to 'widget', the currently active menu,
3617 * instead of 'menu', the menu that the pointer is in. This
3618 * will ensure that the event will be ignored unless the
3619 * menuitem is a child of the active menu or some parent
3620 * menu of the active menu.
3622 result = gtk_widget_event (widget, send_event);
3623 gdk_event_free (send_event);
3633 get_double_arrows (GtkMenu *menu)
3635 GtkMenuPrivate *priv = menu->priv;
3636 gboolean double_arrows;
3637 GtkArrowPlacement arrow_placement;
3639 gtk_widget_style_get (GTK_WIDGET (menu),
3640 "double-arrows", &double_arrows,
3641 "arrow-placement", &arrow_placement,
3644 if (arrow_placement != GTK_ARROWS_BOTH)
3647 return double_arrows || (priv->initially_pushed_in &&
3648 priv->scroll_offset != 0);
3652 gtk_menu_scroll_by (GtkMenu *menu,
3655 GtkMenuPrivate *priv = menu->priv;
3656 GtkBorder arrow_border;
3660 gboolean double_arrows;
3662 widget = GTK_WIDGET (menu);
3663 offset = priv->scroll_offset + step;
3665 get_arrows_border (menu, &arrow_border);
3667 double_arrows = get_double_arrows (menu);
3669 /* If we scroll upward and the non-visible top part
3670 * is smaller than the scroll arrow it would be
3671 * pretty stupid to show the arrow and taking more
3672 * screen space than just scrolling to the top.
3675 if ((step < 0) && (offset < arrow_border.top))
3678 /* Don't scroll over the top if we weren't before: */
3679 if ((priv->scroll_offset >= 0) && (offset < 0))
3682 view_height = gdk_window_get_height (gtk_widget_get_window (widget));
3684 if (priv->scroll_offset == 0 &&
3685 view_height >= priv->requested_height)
3688 /* Don't scroll past the bottom if we weren't before: */
3689 if (priv->scroll_offset > 0)
3690 view_height -= arrow_border.top;
3692 /* When both arrows are always shown,
3693 * reduce view height even more.
3696 view_height -= arrow_border.bottom;
3698 if ((priv->scroll_offset + view_height <= priv->requested_height) &&
3699 (offset + view_height > priv->requested_height))
3700 offset = priv->requested_height - view_height;
3702 if (offset != priv->scroll_offset)
3703 gtk_menu_scroll_to (menu, offset);
3707 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3708 gboolean touchscreen_mode)
3710 GtkMenuPrivate *priv = menu->priv;
3711 gboolean upper_visible;
3712 gboolean lower_visible;
3714 upper_visible = priv->upper_arrow_visible;
3715 lower_visible = priv->lower_arrow_visible;
3717 gtk_menu_scroll_by (menu, priv->scroll_step);
3719 if (touchscreen_mode &&
3720 (upper_visible != priv->upper_arrow_visible ||
3721 lower_visible != priv->lower_arrow_visible))
3723 /* We are about to hide a scroll arrow while the mouse is pressed,
3724 * this would cause the uncovered menu item to be activated on button
3725 * release. Therefore we need to ignore button release here
3727 GTK_MENU_SHELL (menu)->priv->ignore_enter = TRUE;
3728 priv->ignore_button_release = TRUE;
3733 gtk_menu_scroll_timeout (gpointer data)
3736 gboolean touchscreen_mode;
3738 menu = GTK_MENU (data);
3740 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3741 "gtk-touchscreen-mode", &touchscreen_mode,
3744 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3750 gtk_menu_scroll_timeout_initial (gpointer data)
3754 gboolean touchscreen_mode;
3756 menu = GTK_MENU (data);
3758 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3759 "gtk-timeout-repeat", &timeout,
3760 "gtk-touchscreen-mode", &touchscreen_mode,
3763 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3765 gtk_menu_remove_scroll_timeout (menu);
3767 menu->priv->scroll_timeout =
3768 gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout, menu);
3774 gtk_menu_start_scrolling (GtkMenu *menu)
3777 gboolean touchscreen_mode;
3779 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3780 "gtk-timeout-repeat", &timeout,
3781 "gtk-touchscreen-mode", &touchscreen_mode,
3784 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3786 menu->priv->scroll_timeout =
3787 gdk_threads_add_timeout (timeout, gtk_menu_scroll_timeout_initial, menu);
3791 gtk_menu_scroll (GtkWidget *widget,
3792 GdkEventScroll *event)
3794 GtkMenu *menu = GTK_MENU (widget);
3796 switch (event->direction)
3798 case GDK_SCROLL_RIGHT:
3799 case GDK_SCROLL_DOWN:
3800 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3802 case GDK_SCROLL_LEFT:
3804 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3812 get_arrows_sensitive_area (GtkMenu *menu,
3813 GdkRectangle *upper,
3814 GdkRectangle *lower)
3816 GtkArrowPlacement arrow_placement;
3817 GtkWidget *widget = GTK_WIDGET (menu);
3821 guint vertical_padding;
3823 gint scroll_arrow_height;
3825 window = gtk_widget_get_window (widget);
3826 width = gdk_window_get_width (window);
3827 height = gdk_window_get_height (window);
3829 gtk_widget_style_get (widget,
3830 "vertical-padding", &vertical_padding,
3831 "scroll-arrow-vlength", &scroll_arrow_height,
3832 "arrow-placement", &arrow_placement,
3835 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) +
3836 gtk_widget_get_style (widget)->ythickness + vertical_padding;
3838 gdk_window_get_position (window, &win_x, &win_y);
3840 switch (arrow_placement)
3842 case GTK_ARROWS_BOTH:
3847 upper->width = width;
3848 upper->height = scroll_arrow_height + border;
3854 lower->y = win_y + height - border - scroll_arrow_height;
3855 lower->width = width;
3856 lower->height = scroll_arrow_height + border;
3860 case GTK_ARROWS_START:
3865 upper->width = width / 2;
3866 upper->height = scroll_arrow_height + border;
3871 lower->x = win_x + width / 2;
3873 lower->width = width / 2;
3874 lower->height = scroll_arrow_height + border;
3878 case GTK_ARROWS_END:
3882 upper->y = win_y + height - border - scroll_arrow_height;
3883 upper->width = width / 2;
3884 upper->height = scroll_arrow_height + border;
3889 lower->x = win_x + width / 2;
3890 lower->y = win_y + height - border - scroll_arrow_height;
3891 lower->width = width / 2;
3892 lower->height = scroll_arrow_height + border;
3900 gtk_menu_handle_scrolling (GtkMenu *menu,
3906 GtkMenuPrivate *priv = menu->priv;
3907 GtkMenuShell *menu_shell;
3910 gboolean scroll_fast = FALSE;
3912 gboolean touchscreen_mode;
3914 menu_shell = GTK_MENU_SHELL (menu);
3916 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3917 "gtk-touchscreen-mode", &touchscreen_mode,
3920 gdk_window_get_position (gtk_widget_get_window (priv->toplevel),
3925 /* upper arrow handling */
3927 get_arrows_sensitive_area (menu, &rect, NULL);
3930 if (priv->upper_arrow_visible && !priv->tearoff_active &&
3931 (x >= rect.x) && (x < rect.x + rect.width) &&
3932 (y >= rect.y) && (y < rect.y + rect.height))
3937 if (touchscreen_mode)
3938 priv->upper_arrow_prelight = in_arrow;
3940 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
3942 gboolean arrow_pressed = FALSE;
3944 if (priv->upper_arrow_visible && !priv->tearoff_active)
3946 if (touchscreen_mode)
3948 if (enter && priv->upper_arrow_prelight)
3950 if (priv->scroll_timeout == 0)
3952 /* Deselect the active item so that
3953 * any submenus are popped down
3955 gtk_menu_shell_deselect (menu_shell);
3957 gtk_menu_remove_scroll_timeout (menu);
3958 priv->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3962 /* Only do stuff on click. */
3963 gtk_menu_start_scrolling (menu);
3964 arrow_pressed = TRUE;
3969 arrow_pressed = TRUE;
3974 gtk_menu_stop_scrolling (menu);
3977 else /* !touchscreen_mode */
3979 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3981 if (enter && in_arrow &&
3982 (!priv->upper_arrow_prelight ||
3983 priv->scroll_fast != scroll_fast))
3985 priv->upper_arrow_prelight = TRUE;
3986 priv->scroll_fast = scroll_fast;
3988 /* Deselect the active item so that
3989 * any submenus are popped down
3991 gtk_menu_shell_deselect (menu_shell);
3993 gtk_menu_remove_scroll_timeout (menu);
3994 priv->scroll_step = scroll_fast
3995 ? -MENU_SCROLL_STEP2
3996 : -MENU_SCROLL_STEP1;
3998 priv->scroll_timeout =
3999 gdk_threads_add_timeout (scroll_fast
4000 ? MENU_SCROLL_TIMEOUT2
4001 : MENU_SCROLL_TIMEOUT1,
4002 gtk_menu_scroll_timeout, menu);
4004 else if (!enter && !in_arrow && priv->upper_arrow_prelight)
4006 gtk_menu_stop_scrolling (menu);
4011 /* gtk_menu_start_scrolling() might have hit the top of the
4012 * menu, so check if the button isn't insensitive before
4013 * changing it to something else.
4015 if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4017 GtkStateFlags arrow_state = 0;
4020 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4022 if (priv->upper_arrow_prelight)
4023 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4025 if (arrow_state != priv->upper_arrow_state)
4027 priv->upper_arrow_state = arrow_state;
4029 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4035 /* lower arrow handling */
4037 get_arrows_sensitive_area (menu, NULL, &rect);
4040 if (priv->lower_arrow_visible && !priv->tearoff_active &&
4041 (x >= rect.x) && (x < rect.x + rect.width) &&
4042 (y >= rect.y) && (y < rect.y + rect.height))
4047 if (touchscreen_mode)
4048 priv->lower_arrow_prelight = in_arrow;
4050 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4052 gboolean arrow_pressed = FALSE;
4054 if (priv->lower_arrow_visible && !priv->tearoff_active)
4056 if (touchscreen_mode)
4058 if (enter && priv->lower_arrow_prelight)
4060 if (priv->scroll_timeout == 0)
4062 /* Deselect the active item so that
4063 * any submenus are popped down
4065 gtk_menu_shell_deselect (menu_shell);
4067 gtk_menu_remove_scroll_timeout (menu);
4068 priv->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4072 /* Only do stuff on click. */
4073 gtk_menu_start_scrolling (menu);
4074 arrow_pressed = TRUE;
4079 arrow_pressed = TRUE;
4084 gtk_menu_stop_scrolling (menu);
4087 else /* !touchscreen_mode */
4089 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4091 if (enter && in_arrow &&
4092 (!priv->lower_arrow_prelight ||
4093 priv->scroll_fast != scroll_fast))
4095 priv->lower_arrow_prelight = TRUE;
4096 priv->scroll_fast = scroll_fast;
4098 /* Deselect the active item so that
4099 * any submenus are popped down
4101 gtk_menu_shell_deselect (menu_shell);
4103 gtk_menu_remove_scroll_timeout (menu);
4104 priv->scroll_step = scroll_fast
4106 : MENU_SCROLL_STEP1;
4108 priv->scroll_timeout =
4109 gdk_threads_add_timeout (scroll_fast
4110 ? MENU_SCROLL_TIMEOUT2
4111 : MENU_SCROLL_TIMEOUT1,
4112 gtk_menu_scroll_timeout, menu);
4114 else if (!enter && !in_arrow && priv->lower_arrow_prelight)
4116 gtk_menu_stop_scrolling (menu);
4121 /* gtk_menu_start_scrolling() might have hit the bottom of the
4122 * menu, so check if the button isn't insensitive before
4123 * changing it to something else.
4125 if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
4127 GtkStateFlags arrow_state = 0;
4130 arrow_state |= GTK_STATE_FLAG_ACTIVE;
4132 if (priv->lower_arrow_prelight)
4133 arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4135 if (arrow_state != priv->lower_arrow_state)
4137 priv->lower_arrow_state = arrow_state;
4139 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4147 gtk_menu_enter_notify (GtkWidget *widget,
4148 GdkEventCrossing *event)
4150 GtkWidget *menu_item;
4152 gboolean touchscreen_mode;
4154 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4155 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4156 event->mode == GDK_CROSSING_STATE_CHANGED)
4159 g_object_get (gtk_widget_get_settings (widget),
4160 "gtk-touchscreen-mode", &touchscreen_mode,
4163 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4164 if (GTK_IS_MENU (widget))
4166 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4168 if (!menu_shell->priv->ignore_enter)
4169 gtk_menu_handle_scrolling (GTK_MENU (widget),
4170 event->x_root, event->y_root, TRUE, TRUE);
4173 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4175 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4177 if (GTK_IS_MENU (menu))
4179 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
4180 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4182 if (priv->seen_item_enter)
4184 /* This is the second enter we see for an item
4185 * on this menu. This means a release should always
4188 menu_shell->priv->activate_time = 0;
4190 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4191 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4193 if (definitely_within_item (menu_item, event->x, event->y))
4195 /* This is an actual user-enter (ie. not a pop-under)
4196 * In this case, the user must either have entered
4197 * sufficiently far enough into the item, or he must move
4198 * far enough away from the enter point. (see
4199 * gtk_menu_motion_notify())
4201 menu_shell->priv->activate_time = 0;
4205 priv->seen_item_enter = TRUE;
4209 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4210 * will not correspond to the event widget's parent. Check to see
4211 * if we are in the parent's navigation region.
4213 parent = gtk_widget_get_parent (menu_item);
4214 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4215 gtk_menu_navigating_submenu (GTK_MENU (parent),
4216 event->x_root, event->y_root))
4219 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4223 gtk_menu_leave_notify (GtkWidget *widget,
4224 GdkEventCrossing *event)
4226 GtkMenuShell *menu_shell;
4228 GtkMenuItem *menu_item;
4229 GtkWidget *event_widget;
4231 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4232 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4233 event->mode == GDK_CROSSING_STATE_CHANGED)
4236 menu = GTK_MENU (widget);
4237 menu_shell = GTK_MENU_SHELL (widget);
4239 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4242 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4244 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4246 if (!GTK_IS_MENU_ITEM (event_widget))
4249 menu_item = GTK_MENU_ITEM (event_widget);
4251 /* Here we check to see if we're leaving an active menu item
4252 * with a submenu, in which case we enter submenu navigation mode.
4254 if (menu_shell->priv->active_menu_item != NULL
4255 && menu_item->priv->submenu != NULL
4256 && menu_item->priv->submenu_placement == GTK_LEFT_RIGHT)
4258 if (GTK_MENU_SHELL (menu_item->priv->submenu)->priv->active)
4260 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4263 else if (menu_item == GTK_MENU_ITEM (menu_shell->priv->active_menu_item))
4265 /* We are leaving an active menu item with nonactive submenu.
4266 * Deselect it so we don't surprise the user with by popping
4267 * up a submenu _after_ he left the item.
4269 gtk_menu_shell_deselect (menu_shell);
4274 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4278 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4280 GtkMenuPrivate *priv = menu->priv;
4282 priv->navigation_x = 0;
4283 priv->navigation_y = 0;
4284 priv->navigation_width = 0;
4285 priv->navigation_height = 0;
4287 if (priv->navigation_timeout)
4289 g_source_remove (priv->navigation_timeout);
4290 priv->navigation_timeout = 0;
4294 /* When the timeout is elapsed, the navigation region is destroyed
4295 * and the menuitem under the pointer (if any) is selected.
4298 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4300 GtkMenuPopdownData *popdown_data = user_data;
4301 GtkMenu *menu = popdown_data->menu;
4302 GtkMenuPrivate *priv = menu->priv;
4303 GdkWindow *child_window;
4305 gtk_menu_stop_navigating_submenu (menu);
4307 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4309 child_window = gdk_window_get_device_position (priv->bin_window,
4310 popdown_data->device,
4315 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4317 send_event->crossing.window = g_object_ref (child_window);
4318 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4319 send_event->crossing.send_event = TRUE;
4320 gdk_event_set_device (send_event, popdown_data->device);
4322 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4324 gdk_event_free (send_event);
4332 gtk_menu_navigating_submenu (GtkMenu *menu,
4336 GtkMenuPrivate *priv = menu->priv;
4339 if (!gtk_menu_has_navigation_triangle (menu))
4342 width = priv->navigation_width;
4343 height = priv->navigation_height;
4345 /* Check if x/y are in the triangle spanned by the navigation parameters */
4347 /* 1) Move the coordinates so the triangle starts at 0,0 */
4348 event_x -= priv->navigation_x;
4349 event_y -= priv->navigation_y;
4351 /* 2) Ensure both legs move along the positive axis */
4363 /* 3) Check that the given coordinate is inside the triangle. The formula
4364 * is a transformed form of this formula: x/w + y/h <= 1
4366 if (event_x >= 0 && event_y >= 0 &&
4367 event_x * height + event_y * width <= width * height)
4373 gtk_menu_stop_navigating_submenu (menu);
4379 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4380 GtkMenuItem *menu_item,
4381 GdkEventCrossing *event)
4383 GtkMenuPrivate *priv = menu->priv;
4384 gint submenu_left = 0;
4385 gint submenu_right = 0;
4386 gint submenu_top = 0;
4387 gint submenu_bottom = 0;
4390 GtkWidget *event_widget;
4391 GtkMenuPopdownData *popdown_data;
4394 g_return_if_fail (menu_item->priv->submenu != NULL);
4395 g_return_if_fail (event != NULL);
4397 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4399 window = gtk_widget_get_window (menu_item->priv->submenu);
4400 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4402 submenu_right = submenu_left + gdk_window_get_width (window);
4403 submenu_bottom = submenu_top + gdk_window_get_height (window);
4405 width = gdk_window_get_width (gtk_widget_get_window (event_widget));
4406 height = gdk_window_get_height (gtk_widget_get_window (event_widget));
4408 if (event->x >= 0 && event->x < width)
4412 gtk_menu_stop_navigating_submenu (menu);
4414 /* The navigation region is the triangle closest to the x/y
4415 * location of the rectangle. This is why the width or height
4418 if (menu_item->priv->submenu_direction == GTK_DIRECTION_RIGHT)
4421 priv->navigation_x = submenu_left;
4422 priv->navigation_width = event->x_root - submenu_left;
4427 priv->navigation_x = submenu_right;
4428 priv->navigation_width = event->x_root - submenu_right;
4434 priv->navigation_y = event->y_root;
4435 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4437 if (priv->navigation_height >= 0)
4443 priv->navigation_y = event->y_root;
4444 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4446 if (priv->navigation_height <= 0)
4450 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4451 "gtk-menu-popdown-delay", &popdown_delay,
4454 popdown_data = g_new (GtkMenuPopdownData, 1);
4455 popdown_data->menu = menu;
4456 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4458 priv->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4460 gtk_menu_stop_navigating_submenu_cb,
4462 (GDestroyNotify) g_free);
4467 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4471 g_return_if_fail (GTK_IS_MENU (menu_shell));
4473 parent = menu_shell->priv->parent_menu_shell;
4475 menu_shell->priv->activate_time = 0;
4476 gtk_menu_popdown (GTK_MENU (menu_shell));
4479 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4483 gtk_menu_position (GtkMenu *menu,
4484 gboolean set_scroll_offset)
4486 GtkMenuPrivate *priv = menu->priv;
4488 GtkRequisition requisition;
4493 GdkScreen *pointer_screen;
4494 GdkRectangle monitor;
4497 widget = GTK_WIDGET (menu);
4499 screen = gtk_widget_get_screen (widget);
4500 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4501 gdk_device_get_position (pointer, &pointer_screen, &x, &y);
4503 /* Get the minimum height for minimum width to figure out
4504 * the right place to popup the menu.
4506 gtk_widget_get_preferred_size (widget, &requisition, NULL);
4508 if (pointer_screen != screen)
4510 /* Pointer is on a different screen; roughly center the
4511 * menu on the screen. If someone was using multiscreen
4512 * + Xinerama together they'd probably want something
4513 * fancier; but that is likely to be vanishingly rare.
4515 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4516 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4519 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4520 priv->initially_pushed_in = FALSE;
4522 /* Set the type hint here to allow custom position functions
4523 * to set a different hint
4525 if (!gtk_widget_get_visible (priv->toplevel))
4526 gtk_window_set_type_hint (GTK_WINDOW (priv->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4528 if (priv->position_func)
4530 (* priv->position_func) (menu, &x, &y, &priv->initially_pushed_in,
4531 priv->position_func_data);
4533 if (priv->monitor_num < 0)
4534 priv->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4536 gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
4540 gint space_left, space_right, space_above, space_below;
4544 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4546 get_menu_border (widget, &border);
4548 /* The placement of popup menus horizontally works like this (with
4549 * RTL in parentheses)
4551 * - If there is enough room to the right (left) of the mouse cursor,
4552 * position the menu there.
4554 * - Otherwise, if if there is enough room to the left (right) of the
4555 * mouse cursor, position the menu there.
4557 * - Otherwise if the menu is smaller than the monitor, position it
4558 * on the side of the mouse cursor that has the most space available
4560 * - Otherwise (if there is simply not enough room for the menu on the
4561 * monitor), position it as far left (right) as possible.
4563 * Positioning in the vertical direction is similar: first try below
4564 * mouse cursor, then above.
4566 gdk_screen_get_monitor_geometry (screen, priv->monitor_num, &monitor);
4568 space_left = x - monitor.x;
4569 space_right = monitor.x + monitor.width - x - 1;
4570 space_above = y - monitor.y;
4571 space_below = monitor.y + monitor.height - y - 1;
4573 /* Position horizontally. */
4575 /* the amount of space we need to position the menu.
4576 * Note the menu is offset "thickness" pixels
4578 needed_width = requisition.width - border.left;
4580 if (needed_width <= space_left ||
4581 needed_width <= space_right)
4583 if ((rtl && needed_width <= space_left) ||
4584 (!rtl && needed_width > space_right))
4587 x = x + border.left - requisition.width + 1;
4591 /* position right */
4592 x = x - border.right;
4595 /* x is clamped on-screen further down */
4597 else if (requisition.width <= monitor.width)
4599 /* the menu is too big to fit on either side of the mouse
4600 * cursor, but smaller than the monitor. Position it on
4601 * the side that has the most space
4603 if (space_left > space_right)
4611 x = monitor.x + monitor.width - requisition.width;
4614 else /* menu is simply too big for the monitor */
4619 x = monitor.x + monitor.width - requisition.width;
4628 /* Position vertically.
4629 * The algorithm is the same as above, but simpler
4630 * because we don't have to take RTL into account.
4632 needed_height = requisition.height - border.top;
4634 if (needed_height <= space_above ||
4635 needed_height <= space_below)
4637 if (needed_height <= space_below)
4640 y = y + border.bottom - requisition.height + 1;
4642 y = CLAMP (y, monitor.y,
4643 monitor.y + monitor.height - requisition.height);
4645 else if (needed_height > space_below && needed_height > space_above)
4647 if (space_below >= space_above)
4648 y = monitor.y + monitor.height - requisition.height;
4660 if (priv->initially_pushed_in)
4662 menu_height = requisition.height;
4664 if (y + menu_height > monitor.y + monitor.height)
4666 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4667 y = (monitor.y + monitor.height) - menu_height;
4672 scroll_offset += monitor.y - y;
4677 /* FIXME: should this be done in the various position_funcs ? */
4678 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4680 if (GTK_MENU_SHELL (menu)->priv->active)
4682 priv->have_position = TRUE;
4683 priv->position_x = x;
4684 priv->position_y = y;
4687 if (y + requisition.height > monitor.y + monitor.height)
4688 requisition.height = (monitor.y + monitor.height) - y;
4692 scroll_offset += monitor.y - y;
4693 requisition.height -= monitor.y - y;
4697 if (scroll_offset > 0)
4699 GtkBorder arrow_border;
4701 get_arrows_border (menu, &arrow_border);
4702 scroll_offset += arrow_border.top;
4705 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->priv->active ? priv->toplevel : priv->tearoff_window),
4708 if (!GTK_MENU_SHELL (menu)->priv->active)
4710 gtk_window_resize (GTK_WINDOW (priv->tearoff_window),
4711 requisition.width, requisition.height);
4714 if (set_scroll_offset)
4715 priv->scroll_offset = scroll_offset;
4719 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4721 GtkMenuPrivate *priv = menu->priv;
4723 if (priv->scroll_timeout)
4725 g_source_remove (priv->scroll_timeout);
4726 priv->scroll_timeout = 0;
4731 gtk_menu_stop_scrolling (GtkMenu *menu)
4733 GtkMenuPrivate *priv = menu->priv;
4734 gboolean touchscreen_mode;
4736 gtk_menu_remove_scroll_timeout (menu);
4738 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4739 "gtk-touchscreen-mode", &touchscreen_mode,
4742 if (!touchscreen_mode)
4744 priv->upper_arrow_prelight = FALSE;
4745 priv->lower_arrow_prelight = FALSE;
4750 gtk_menu_scroll_to (GtkMenu *menu,
4753 GtkMenuPrivate *priv = menu->priv;
4754 GtkAllocation allocation;
4755 GtkBorder arrow_border, border;
4758 gint view_width, view_height;
4761 guint vertical_padding;
4762 guint horizontal_padding;
4763 gboolean double_arrows;
4765 widget = GTK_WIDGET (menu);
4767 if (priv->tearoff_active &&
4768 priv->tearoff_adjustment &&
4769 (priv->tearoff_adjustment->value != offset))
4771 priv->tearoff_adjustment->value =
4773 0, priv->tearoff_adjustment->upper - priv->tearoff_adjustment->page_size);
4774 gtk_adjustment_value_changed (priv->tearoff_adjustment);
4777 /* Move/resize the viewport according to arrows: */
4778 gtk_widget_get_allocation (widget, &allocation);
4779 view_width = allocation.width;
4780 view_height = allocation.height;
4782 gtk_widget_style_get (GTK_WIDGET (menu),
4783 "vertical-padding", &vertical_padding,
4784 "horizontal-padding", &horizontal_padding,
4787 get_menu_border (widget, &border);
4788 double_arrows = get_double_arrows (menu);
4790 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4792 view_width -= (2 * (border_width + horizontal_padding)) + border.left + border.right;
4793 view_height -= (2 * (border_width + vertical_padding)) + border.top + border.bottom;
4794 menu_height = priv->requested_height - (2 * (border_width + vertical_padding)) -
4795 border.top - border.bottom;
4797 x = border_width + border.left + horizontal_padding;
4798 y = border_width + border.top + vertical_padding;
4800 if (double_arrows && !priv->tearoff_active)
4802 if (view_height < menu_height ||
4803 (offset > 0 && priv->scroll_offset > 0) ||
4804 (offset < 0 && priv->scroll_offset < 0))
4806 GtkStateFlags upper_arrow_previous_state = priv->upper_arrow_state;
4807 GtkStateFlags lower_arrow_previous_state = priv->lower_arrow_state;
4809 if (!priv->upper_arrow_visible || !priv->lower_arrow_visible)
4810 gtk_widget_queue_draw (GTK_WIDGET (menu));
4812 priv->upper_arrow_visible = priv->lower_arrow_visible = TRUE;
4814 get_arrows_border (menu, &arrow_border);
4815 y += arrow_border.top;
4816 view_height -= arrow_border.top;
4817 view_height -= arrow_border.bottom;
4820 priv->upper_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4823 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4825 if (priv->upper_arrow_prelight)
4826 priv->upper_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4828 priv->upper_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4831 if (offset >= menu_height - view_height)
4832 priv->lower_arrow_state |= GTK_STATE_FLAG_INSENSITIVE;
4835 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_INSENSITIVE);
4837 if (priv->lower_arrow_prelight)
4838 priv->lower_arrow_state |= GTK_STATE_FLAG_PRELIGHT;
4840 priv->lower_arrow_state &= ~(GTK_STATE_FLAG_PRELIGHT);
4843 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4844 (priv->lower_arrow_state != lower_arrow_previous_state))
4845 gtk_widget_queue_draw (GTK_WIDGET (menu));
4847 if ((upper_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
4848 (priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
4850 /* At the upper border, possibly remove timeout */
4851 if (priv->scroll_step < 0)
4853 gtk_menu_stop_scrolling (menu);
4854 gtk_widget_queue_draw (GTK_WIDGET (menu));
4858 if ((lower_arrow_previous_state & GTK_STATE_FLAG_INSENSITIVE) == 0 &&
4859 (priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) != 0)
4861 /* At the lower border, possibly remove timeout */
4862 if (priv->scroll_step > 0)
4864 gtk_menu_stop_scrolling (menu);
4865 gtk_widget_queue_draw (GTK_WIDGET (menu));
4869 else if (priv->upper_arrow_visible || priv->lower_arrow_visible)
4873 priv->upper_arrow_visible = priv->lower_arrow_visible = FALSE;
4874 priv->upper_arrow_prelight = priv->lower_arrow_prelight = FALSE;
4876 gtk_menu_stop_scrolling (menu);
4877 gtk_widget_queue_draw (GTK_WIDGET (menu));
4880 else if (!priv->tearoff_active)
4882 gboolean last_visible;
4884 last_visible = priv->upper_arrow_visible;
4885 priv->upper_arrow_visible = offset > 0;
4887 /* upper_arrow_visible may have changed, so requery the border */
4888 get_arrows_border (menu, &arrow_border);
4889 view_height -= arrow_border.top;
4891 if ((last_visible != priv->upper_arrow_visible) &&
4892 !priv->upper_arrow_visible)
4894 priv->upper_arrow_prelight = FALSE;
4896 /* If we hide the upper arrow, possibly remove timeout */
4897 if (priv->scroll_step < 0)
4899 gtk_menu_stop_scrolling (menu);
4900 gtk_widget_queue_draw (GTK_WIDGET (menu));
4904 last_visible = priv->lower_arrow_visible;
4905 priv->lower_arrow_visible = offset < menu_height - view_height;
4907 /* lower_arrow_visible may have changed, so requery the border */
4908 get_arrows_border (menu, &arrow_border);
4909 view_height -= arrow_border.bottom;
4911 if ((last_visible != priv->lower_arrow_visible) &&
4912 !priv->lower_arrow_visible)
4914 priv->lower_arrow_prelight = FALSE;
4916 /* If we hide the lower arrow, possibly remove timeout */
4917 if (priv->scroll_step > 0)
4919 gtk_menu_stop_scrolling (menu);
4920 gtk_widget_queue_draw (GTK_WIDGET (menu));
4924 y += arrow_border.top;
4927 /* Scroll the menu: */
4928 if (gtk_widget_get_realized (widget))
4929 gdk_window_move (priv->bin_window, 0, -offset);
4931 if (gtk_widget_get_realized (widget))
4932 gdk_window_move_resize (priv->view_window, x, y, view_width, view_height);
4934 priv->scroll_offset = offset;
4938 compute_child_offset (GtkMenu *menu,
4939 GtkWidget *menu_item,
4942 gboolean *is_last_child)
4944 GtkMenuPrivate *priv = menu->priv;
4945 gint item_top_attach;
4946 gint item_bottom_attach;
4947 gint child_offset = 0;
4950 get_effective_child_attach (menu_item, NULL, NULL,
4951 &item_top_attach, &item_bottom_attach);
4953 /* there is a possibility that we get called before _size_request,
4954 * so check the height table for safety.
4956 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4959 /* when we have a row with only invisible children, it's height will
4960 * be zero, so there's no need to check WIDGET_VISIBLE here
4962 for (i = 0; i < item_top_attach; i++)
4963 child_offset += priv->heights[i];
4966 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4968 *offset = child_offset;
4970 *height = priv->heights[item_top_attach];
4976 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4977 GtkWidget *menu_item)
4979 GtkMenu *menu = GTK_MENU (menu_shell);
4980 GtkMenuPrivate *priv = menu->priv;
4981 GtkWidget *widget = GTK_WIDGET (menu_shell);
4982 gint child_offset, child_height;
4986 gboolean last_child = 0;
4988 /* We need to check if the selected item fully visible.
4989 * If not we need to scroll the menu so that it becomes fully
4993 if (compute_child_offset (menu, menu_item,
4994 &child_offset, &child_height, &last_child))
4996 guint vertical_padding;
4997 gboolean double_arrows;
4999 y = priv->scroll_offset;
5000 width = gdk_window_get_width (gtk_widget_get_window (widget));
5001 height = gdk_window_get_height (gtk_widget_get_window (widget));
5003 gtk_widget_style_get (widget,
5004 "vertical-padding", &vertical_padding,
5007 double_arrows = get_double_arrows (menu);
5009 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5010 2 * gtk_widget_get_style (widget)->ythickness +
5011 2 * vertical_padding;
5012 if (child_offset < y)
5014 /* Ignore the enter event we might get if the pointer
5017 menu_shell->priv->ignore_enter = TRUE;
5018 gtk_menu_scroll_to (menu, child_offset);
5022 GtkBorder arrow_border;
5026 get_arrows_border (menu, &arrow_border);
5027 if (!priv->tearoff_active)
5028 arrow_height = arrow_border.top + arrow_border.bottom;
5030 if (child_offset + child_height > y + height - arrow_height)
5033 if ((!last_child && !priv->tearoff_active) || double_arrows)
5034 arrow_height += arrow_border.bottom;
5036 y = child_offset + child_height - height + arrow_height;
5037 if (((y > 0) && !priv->tearoff_active) || double_arrows)
5039 /* Need upper arrow */
5040 arrow_height += arrow_border.top;
5041 y = child_offset + child_height - height + arrow_height;
5043 /* Ignore the enter event we might get if the pointer
5046 menu_shell->priv->ignore_enter = TRUE;
5047 gtk_menu_scroll_to (menu, y);
5054 gtk_menu_select_item (GtkMenuShell *menu_shell,
5055 GtkWidget *menu_item)
5057 GtkMenu *menu = GTK_MENU (menu_shell);
5059 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5060 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5062 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5066 /* Reparent the menu, taking care of the refcounting
5068 * If unrealize is true we force a unrealize while reparenting the parent.
5069 * This can help eliminate flicker in some cases.
5071 * What happens is that when the menu is unrealized and then re-realized,
5072 * the allocations are as follows:
5074 * parent - 1x1 at (0,0)
5075 * child1 - 100x20 at (0,0)
5076 * child2 - 100x20 at (0,20)
5077 * child3 - 100x20 at (0,40)
5079 * That is, the parent is small but the children are full sized. Then,
5080 * when the queued_resize gets processed, the parent gets resized to
5083 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5084 * contains the following logic:
5086 * - if a move or resize operation on a window would change the clip
5087 * region on the children, then before the window is resized
5088 * the background for children is temporarily set to None, the
5089 * move/resize done, and the background for the children restored.
5091 * So, at the point where the parent is resized to final size, the
5092 * background for the children is temporarily None, and thus they
5093 * are not cleared to the background color and the previous background
5094 * (the image of the menu) is left in place.
5097 gtk_menu_reparent (GtkMenu *menu,
5098 GtkWidget *new_parent,
5101 GObject *object = G_OBJECT (menu);
5102 GtkWidget *widget = GTK_WIDGET (menu);
5103 gboolean was_floating = g_object_is_floating (object);
5105 g_object_ref_sink (object);
5109 g_object_ref (object);
5110 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5111 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5112 g_object_unref (object);
5115 gtk_widget_reparent (widget, new_parent);
5118 g_object_force_floating (object);
5120 g_object_unref (object);
5124 gtk_menu_show_all (GtkWidget *widget)
5126 /* Show children, but not self. */
5127 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5131 * gtk_menu_set_screen:
5133 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5134 * determined by the widget the menu is attached to
5136 * Sets the #GdkScreen on which the menu will be displayed.
5141 gtk_menu_set_screen (GtkMenu *menu,
5144 g_return_if_fail (GTK_IS_MENU (menu));
5145 g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));
5147 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5151 menu_change_screen (menu, screen);
5155 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5157 attach_widget_screen_changed (attach_widget, NULL, menu);
5164 * @child: a #GtkMenuItem
5165 * @left_attach: The column number to attach the left side of the item to
5166 * @right_attach: The column number to attach the right side of the item to
5167 * @top_attach: The row number to attach the top of the item to
5168 * @bottom_attach: The row number to attach the bottom of the item to
5170 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5171 * an item will occupy is specified by @left_attach, @right_attach,
5172 * @top_attach and @bottom_attach. These each represent the leftmost,
5173 * rightmost, uppermost and lower column and row numbers of the table.
5174 * (Columns and rows are indexed from zero).
5176 * Note that this function is not related to gtk_menu_detach().
5181 gtk_menu_attach (GtkMenu *menu,
5186 guint bottom_attach)
5188 GtkMenuShell *menu_shell;
5191 g_return_if_fail (GTK_IS_MENU (menu));
5192 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5193 parent = gtk_widget_get_parent (child);
5194 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5195 g_return_if_fail (left_attach < right_attach);
5196 g_return_if_fail (top_attach < bottom_attach);
5198 menu_shell = GTK_MENU_SHELL (menu);
5202 AttachInfo *ai = get_attach_info (child);
5204 ai->left_attach = left_attach;
5205 ai->right_attach = right_attach;
5206 ai->top_attach = top_attach;
5207 ai->bottom_attach = bottom_attach;
5209 menu_shell->priv->children = g_list_append (menu_shell->priv->children, child);
5211 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5213 menu_queue_resize (menu);
5217 gtk_container_child_set (GTK_CONTAINER (parent), child,
5218 "left-attach", left_attach,
5219 "right-attach", right_attach,
5220 "top-attach", top_attach,
5221 "bottom-attach", bottom_attach,
5227 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5231 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5232 "gtk-menu-popup-delay", &popup_delay,
5239 find_child_containing (GtkMenuShell *menu_shell,
5247 /* find a child which includes the area given by
5248 * left, right, top, bottom.
5250 for (list = menu_shell->priv->children; list; list = list->next)
5254 if (!_gtk_menu_item_is_selectable (list->data))
5257 get_effective_child_attach (list->data, &l, &r, &t, &b);
5259 if (l <= left && right <= r && t <= top && bottom <= b)
5260 return GTK_WIDGET (list->data);
5267 gtk_menu_move_current (GtkMenuShell *menu_shell,
5268 GtkMenuDirectionType direction)
5270 GtkMenu *menu = GTK_MENU (menu_shell);
5273 GtkWidget *match = NULL;
5275 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5279 case GTK_MENU_DIR_CHILD:
5280 direction = GTK_MENU_DIR_PARENT;
5282 case GTK_MENU_DIR_PARENT:
5283 direction = GTK_MENU_DIR_CHILD;
5289 /* use special table menu key bindings */
5290 if (menu_shell->priv->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5292 get_effective_child_attach (menu_shell->priv->active_menu_item, &l, &r, &t, &b);
5294 if (direction == GTK_MENU_DIR_NEXT)
5296 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5298 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5306 for (i = 0; i < t; i++)
5308 match = find_child_containing (menu_shell,
5309 l, l + 1, i, i + 1);
5315 else if (direction == GTK_MENU_DIR_PREV)
5317 for (i = t; i > 0; i--)
5319 match = find_child_containing (menu_shell,
5320 l, l + 1, i - 1, i);
5328 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5330 match = find_child_containing (menu_shell,
5331 l, l + 1, i - 1, i);
5337 else if (direction == GTK_MENU_DIR_PARENT)
5339 /* we go one left if possible */
5341 match = find_child_containing (menu_shell,
5342 l - 1, l, t, t + 1);
5346 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5349 || g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1)
5350 match = menu_shell->priv->active_menu_item;
5353 else if (direction == GTK_MENU_DIR_CHILD)
5355 /* we go one right if possible */
5356 if (r < gtk_menu_get_n_columns (menu))
5357 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5361 GtkWidget *parent = menu_shell->priv->parent_menu_shell;
5363 if (! GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu &&
5365 g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1))
5366 match = menu_shell->priv->active_menu_item;
5372 gtk_menu_shell_select_item (menu_shell, match);
5377 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5381 get_visible_size (GtkMenu *menu)
5383 GtkMenuPrivate *priv = menu->priv;
5384 GtkAllocation allocation;
5385 GtkWidget *widget = GTK_WIDGET (menu);
5386 GtkContainer *container = GTK_CONTAINER (menu);
5389 gtk_widget_get_allocation (widget, &allocation);
5390 menu_height = (allocation.height
5391 - 2 * (gtk_container_get_border_width (container)
5392 + gtk_widget_get_style (widget)->ythickness));
5394 if (!priv->tearoff_active)
5396 GtkBorder arrow_border;
5398 get_arrows_border (menu, &arrow_border);
5399 menu_height -= arrow_border.top;
5400 menu_height -= arrow_border.bottom;
5406 /* Find the sensitive on-screen child containing @y, or if none,
5407 * the nearest selectable onscreen child. (%NULL if none)
5410 child_at (GtkMenu *menu,
5413 GtkMenuPrivate *priv = menu->priv;
5414 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5415 GtkWidget *child = NULL;
5416 gint child_offset = 0;
5419 gint lower, upper; /* Onscreen bounds */
5421 menu_height = get_visible_size (menu);
5422 lower = priv->scroll_offset;
5423 upper = priv->scroll_offset + menu_height;
5425 for (children = menu_shell->priv->children; children; children = children->next)
5427 if (gtk_widget_get_visible (children->data))
5429 GtkRequisition child_requisition;
5431 gtk_widget_get_preferred_size (children->data,
5432 &child_requisition, NULL);
5434 if (_gtk_menu_item_is_selectable (children->data) &&
5435 child_offset >= lower &&
5436 child_offset + child_requisition.height <= upper)
5438 child = children->data;
5440 if (child_offset + child_requisition.height > y &&
5441 !GTK_IS_TEAROFF_MENU_ITEM (child))
5445 child_offset += child_requisition.height;
5453 get_menu_height (GtkMenu *menu)
5455 GtkMenuPrivate *priv = menu->priv;
5456 GtkAllocation allocation;
5457 GtkWidget *widget = GTK_WIDGET (menu);
5460 gtk_widget_get_allocation (widget, &allocation);
5462 height = allocation.height;
5463 height -= (gtk_container_get_border_width (GTK_CONTAINER (widget)) + gtk_widget_get_style (widget)->ythickness) * 2;
5465 if (!priv->tearoff_active)
5467 GtkBorder arrow_border;
5469 get_arrows_border (menu, &arrow_border);
5470 height -= arrow_border.top;
5471 height -= arrow_border.bottom;
5478 gtk_menu_real_move_scroll (GtkMenu *menu,
5481 GtkMenuPrivate *priv = menu->priv;
5482 gint page_size = get_visible_size (menu);
5483 gint end_position = get_menu_height (menu);
5484 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5488 case GTK_SCROLL_PAGE_UP:
5489 case GTK_SCROLL_PAGE_DOWN:
5493 gint child_offset = 0;
5494 gboolean old_upper_arrow_visible;
5497 if (type == GTK_SCROLL_PAGE_UP)
5502 if (menu_shell->priv->active_menu_item)
5506 compute_child_offset (menu, menu_shell->priv->active_menu_item,
5507 &child_offset, &child_height, NULL);
5508 child_offset += child_height / 2;
5511 menu_shell->priv->ignore_enter = TRUE;
5512 old_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5513 old_offset = priv->scroll_offset;
5515 new_offset = priv->scroll_offset + step;
5516 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5518 gtk_menu_scroll_to (menu, new_offset);
5520 if (menu_shell->priv->active_menu_item)
5522 GtkWidget *new_child;
5523 gboolean new_upper_arrow_visible = priv->upper_arrow_visible && !priv->tearoff_active;
5524 GtkBorder arrow_border;
5525 get_arrows_border (menu, &arrow_border);
5527 if (priv->scroll_offset != old_offset)
5528 step = priv->scroll_offset - old_offset;
5530 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5532 new_child = child_at (menu, child_offset + step);
5534 gtk_menu_shell_select_item (menu_shell, new_child);
5538 case GTK_SCROLL_START:
5539 /* Ignore the enter event we might get if the pointer is on the menu */
5540 menu_shell->priv->ignore_enter = TRUE;
5541 gtk_menu_scroll_to (menu, 0);
5542 gtk_menu_shell_select_first (menu_shell, TRUE);
5544 case GTK_SCROLL_END:
5545 /* Ignore the enter event we might get if the pointer is on the menu */
5546 menu_shell->priv->ignore_enter = TRUE;
5547 gtk_menu_scroll_to (menu, end_position - page_size);
5548 _gtk_menu_shell_select_last (menu_shell, TRUE);
5557 * gtk_menu_set_monitor:
5559 * @monitor_num: the number of the monitor on which the menu should
5562 * Informs GTK+ on which monitor a menu should be popped up.
5563 * See gdk_screen_get_monitor_geometry().
5565 * This function should be called from a #GtkMenuPositionFunc
5566 * if the menu should not appear on the same monitor as the pointer.
5567 * This information can't be reliably inferred from the coordinates
5568 * returned by a #GtkMenuPositionFunc, since, for very long menus,
5569 * these coordinates may extend beyond the monitor boundaries or even
5570 * the screen boundaries.
5575 gtk_menu_set_monitor (GtkMenu *menu,
5578 GtkMenuPrivate *priv = menu->priv;
5580 g_return_if_fail (GTK_IS_MENU (menu));
5582 priv->monitor_num = monitor_num;
5586 * gtk_menu_get_monitor:
5589 * Retrieves the number of the monitor on which to show the menu.
5591 * Returns: the number of the monitor on which the menu should
5592 * be popped up or -1, if no monitor has been set
5597 gtk_menu_get_monitor (GtkMenu *menu)
5599 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5601 return menu->priv->monitor_num;
5605 * gtk_menu_get_for_attach_widget:
5606 * @widget: a #GtkWidget
5608 * Returns a list of the menus which are attached to this widget.
5609 * This list is owned by GTK+ and must not be modified.
5611 * Return value: (element-type GtkWidget) (transfer none): the list
5612 * of menus attached to his widget.
5617 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5621 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5623 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5629 gtk_menu_grab_notify (GtkWidget *widget,
5630 gboolean was_grabbed)
5632 GtkWidget *toplevel;
5633 GtkWindowGroup *group;
5637 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5640 !gtk_widget_device_is_shadowed (widget, pointer))
5643 toplevel = gtk_widget_get_toplevel (widget);
5645 if (!GTK_IS_WINDOW (toplevel))
5648 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5649 grab = gtk_window_group_get_current_device_grab (group, pointer);
5651 if (GTK_MENU_SHELL (widget)->priv->active && !GTK_IS_MENU_SHELL (grab))
5652 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5656 * gtk_menu_set_reserve_toggle_size:
5658 * @reserve_toggle_size: whether to reserve size for toggles
5660 * Sets whether the menu should reserve space for drawing toggles
5661 * or icons, regardless of their actual presence.
5666 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5667 gboolean reserve_toggle_size)
5669 GtkMenuPrivate *priv = menu->priv;
5670 gboolean no_toggle_size;
5672 g_return_if_fail (GTK_IS_MENU (menu));
5674 no_toggle_size = !reserve_toggle_size;
5676 if (priv->no_toggle_size != no_toggle_size)
5678 priv->no_toggle_size = no_toggle_size;
5680 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5685 * gtk_menu_get_reserve_toggle_size:
5688 * Returns whether the menu reserves space for toggles and
5689 * icons, regardless of their actual presence.
5691 * Returns: Whether the menu reserves toggle space
5696 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5698 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
5700 return !menu->priv->no_toggle_size;