1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
29 #include "gdk/gdkkeysyms.h"
30 #include "gtkaccellabel.h"
31 #include "gtkaccelmap.h"
32 #include "gtkbindings.h"
33 #include "gtkcheckmenuitem.h"
34 #include <gobject/gvaluecollector.h>
36 #include "gtkmarshalers.h"
38 #include "gtkmenuprivate.h"
39 #include "gtktearoffmenuitem.h"
40 #include "gtkwindow.h"
42 #include "gtkvscrollbar.h"
43 #include "gtksettings.h"
44 #include "gtkprivate.h"
47 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
48 * extends below the submenu
51 #define MENU_SCROLL_STEP1 8
52 #define MENU_SCROLL_STEP2 15
53 #define MENU_SCROLL_FAST_ZONE 8
54 #define MENU_SCROLL_TIMEOUT1 50
55 #define MENU_SCROLL_TIMEOUT2 20
57 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
58 #define ATTACHED_MENUS "gtk-attached-menus"
60 typedef struct _GtkMenuAttachData GtkMenuAttachData;
61 typedef struct _GtkMenuPrivate GtkMenuPrivate;
62 typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
64 struct _GtkMenuAttachData
66 GtkWidget *attach_widget;
67 GtkMenuDetachFunc detacher;
70 struct _GtkMenuPrivate
74 gboolean initially_pushed_in;
76 GDestroyNotify position_func_data_destroy;
78 /* info used for the table */
81 gint requested_height;
85 /* Cached layout information */
94 GtkStateType lower_arrow_state;
95 GtkStateType upper_arrow_state;
97 /* navigation region */
100 int navigation_width;
101 int navigation_height;
103 guint have_layout : 1;
104 guint seen_item_enter : 1;
105 guint have_position : 1;
106 guint ignore_button_release : 1;
107 guint no_toggle_size : 1;
110 struct _GtkMenuPopdownData
122 gint effective_left_attach;
123 gint effective_right_attach;
124 gint effective_top_attach;
125 gint effective_bottom_attach;
142 PROP_RESERVE_TOGGLE_SIZE
147 CHILD_PROP_LEFT_ATTACH,
148 CHILD_PROP_RIGHT_ATTACH,
149 CHILD_PROP_TOP_ATTACH,
150 CHILD_PROP_BOTTOM_ATTACH
153 static void gtk_menu_set_property (GObject *object,
157 static void gtk_menu_get_property (GObject *object,
161 static void gtk_menu_set_child_property(GtkContainer *container,
166 static void gtk_menu_get_child_property(GtkContainer *container,
171 static void gtk_menu_destroy (GtkWidget *widget);
172 static void gtk_menu_realize (GtkWidget *widget);
173 static void gtk_menu_unrealize (GtkWidget *widget);
174 static void gtk_menu_size_allocate (GtkWidget *widget,
175 GtkAllocation *allocation);
176 static void gtk_menu_show (GtkWidget *widget);
177 static gboolean gtk_menu_draw (GtkWidget *widget,
179 static gboolean gtk_menu_key_press (GtkWidget *widget,
181 static gboolean gtk_menu_scroll (GtkWidget *widget,
182 GdkEventScroll *event);
183 static gboolean gtk_menu_button_press (GtkWidget *widget,
184 GdkEventButton *event);
185 static gboolean gtk_menu_button_release (GtkWidget *widget,
186 GdkEventButton *event);
187 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
188 GdkEventMotion *event);
189 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
190 GdkEventCrossing *event);
191 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
192 GdkEventCrossing *event);
193 static void gtk_menu_scroll_to (GtkMenu *menu,
195 static void gtk_menu_grab_notify (GtkWidget *widget,
196 gboolean was_grabbed);
198 static void gtk_menu_stop_scrolling (GtkMenu *menu);
199 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
200 static gboolean gtk_menu_scroll_timeout (gpointer data);
201 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
202 static void gtk_menu_start_scrolling (GtkMenu *menu);
204 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
205 GtkWidget *menu_item);
206 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
207 GtkWidget *menu_item);
208 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
211 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
213 static void gtk_menu_handle_scrolling (GtkMenu *menu,
218 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
220 static void gtk_menu_style_set (GtkWidget *widget,
221 GtkStyle *previous_style);
222 static gboolean gtk_menu_focus (GtkWidget *widget,
223 GtkDirectionType direction);
224 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
225 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
226 GtkMenuDirectionType direction);
227 static void gtk_menu_real_move_scroll (GtkMenu *menu,
230 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
231 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
232 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
235 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
236 GtkMenuItem *menu_item,
237 GdkEventCrossing *event);
239 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
240 static void gtk_menu_show_all (GtkWidget *widget);
241 static void gtk_menu_position (GtkMenu *menu,
242 gboolean set_scroll_offset);
243 static void gtk_menu_reparent (GtkMenu *menu,
244 GtkWidget *new_parent,
246 static void gtk_menu_remove (GtkContainer *menu,
249 static void gtk_menu_update_title (GtkMenu *menu);
251 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
252 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
254 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
256 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
257 gboolean group_changed);
259 static void gtk_menu_get_preferred_width (GtkWidget *widget,
262 static void gtk_menu_get_preferred_height (GtkWidget *widget,
265 static void gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
271 static const gchar attach_data_key[] = "gtk-menu-attach-data";
273 static guint menu_signals[LAST_SIGNAL] = { 0 };
275 static GtkMenuPrivate *
276 gtk_menu_get_private (GtkMenu *menu)
278 return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
281 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
284 menu_queue_resize (GtkMenu *menu)
286 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
288 priv->have_layout = FALSE;
289 gtk_widget_queue_resize (GTK_WIDGET (menu));
293 attach_info_free (AttachInfo *info)
295 g_slice_free (AttachInfo, info);
299 get_attach_info (GtkWidget *child)
301 GObject *object = G_OBJECT (child);
302 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
306 ai = g_slice_new0 (AttachInfo);
307 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
308 (GDestroyNotify) attach_info_free);
315 is_grid_attached (AttachInfo *ai)
317 return (ai->left_attach >= 0 &&
318 ai->right_attach >= 0 &&
319 ai->top_attach >= 0 &&
320 ai->bottom_attach >= 0);
324 menu_ensure_layout (GtkMenu *menu)
326 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
328 if (!priv->have_layout)
330 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
334 gint max_right_attach;
335 gint max_bottom_attach;
337 /* Find extents of gridded portion
339 max_right_attach = 1;
340 max_bottom_attach = 0;
342 for (l = menu_shell->children; l; l = l->next)
344 GtkWidget *child = l->data;
345 AttachInfo *ai = get_attach_info (child);
347 if (is_grid_attached (ai))
349 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
350 max_right_attach = MAX (max_right_attach, ai->right_attach);
356 row_occupied = g_malloc0 (max_bottom_attach);
358 for (l = menu_shell->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->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_set = gtk_menu_style_set;
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)->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 = gtk_menu_get_private (menu);
1006 menu->parent_menu_item = NULL;
1007 menu->old_active_menu_item = NULL;
1008 menu->accel_group = NULL;
1009 menu->position_func = NULL;
1010 menu->position_func_data = NULL;
1011 menu->toggle_size = 0;
1013 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1014 "type", GTK_WINDOW_POPUP,
1017 "signal::event", gtk_menu_window_event, menu,
1018 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1020 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
1021 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
1023 /* Refloat the menu, so that reference counting for the menu isn't
1024 * affected by it being a child of the toplevel
1026 g_object_force_floating (G_OBJECT (menu));
1027 menu->needs_destruction_ref_count = TRUE;
1029 menu->view_window = NULL;
1030 menu->bin_window = NULL;
1032 menu->scroll_offset = 0;
1033 menu->scroll_step = 0;
1034 menu->timeout_id = 0;
1035 menu->scroll_fast = FALSE;
1037 menu->tearoff_window = NULL;
1038 menu->tearoff_hbox = NULL;
1039 menu->torn_off = FALSE;
1040 menu->tearoff_active = FALSE;
1041 menu->tearoff_adjustment = NULL;
1042 menu->tearoff_scrollbar = NULL;
1044 menu->upper_arrow_visible = FALSE;
1045 menu->lower_arrow_visible = FALSE;
1046 menu->upper_arrow_prelight = FALSE;
1047 menu->lower_arrow_prelight = FALSE;
1049 priv->upper_arrow_state = GTK_STATE_NORMAL;
1050 priv->lower_arrow_state = GTK_STATE_NORMAL;
1052 priv->have_layout = FALSE;
1053 priv->monitor_num = -1;
1057 gtk_menu_destroy (GtkWidget *widget)
1059 GtkMenu *menu = GTK_MENU (widget);
1060 GtkMenuAttachData *data;
1061 GtkMenuPrivate *priv;
1063 gtk_menu_remove_scroll_timeout (menu);
1065 data = g_object_get_data (G_OBJECT (widget), attach_data_key);
1067 gtk_menu_detach (menu);
1069 gtk_menu_stop_navigating_submenu (menu);
1071 if (menu->old_active_menu_item)
1073 g_object_unref (menu->old_active_menu_item);
1074 menu->old_active_menu_item = NULL;
1077 /* Add back the reference count for being a child */
1078 if (menu->needs_destruction_ref_count)
1080 menu->needs_destruction_ref_count = FALSE;
1081 g_object_ref (widget);
1084 if (menu->accel_group)
1086 g_object_unref (menu->accel_group);
1087 menu->accel_group = NULL;
1091 gtk_widget_destroy (menu->toplevel);
1093 if (menu->tearoff_window)
1094 gtk_widget_destroy (menu->tearoff_window);
1096 priv = gtk_menu_get_private (menu);
1100 g_free (priv->heights);
1101 priv->heights = NULL;
1106 g_free (priv->title);
1110 if (priv->position_func_data_destroy)
1112 priv->position_func_data_destroy (menu->position_func_data);
1113 menu->position_func_data = NULL;
1114 priv->position_func_data_destroy = NULL;
1117 GTK_WIDGET_CLASS (gtk_menu_parent_class)->destroy (widget);
1121 menu_change_screen (GtkMenu *menu,
1122 GdkScreen *new_screen)
1124 GtkMenuPrivate *private = gtk_menu_get_private (menu);
1126 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1128 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1134 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1135 gtk_menu_position (menu, TRUE);
1138 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1139 private->monitor_num = -1;
1143 attach_widget_screen_changed (GtkWidget *attach_widget,
1144 GdkScreen *previous_screen,
1147 if (gtk_widget_has_screen (attach_widget) &&
1148 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1150 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1155 gtk_menu_attach_to_widget (GtkMenu *menu,
1156 GtkWidget *attach_widget,
1157 GtkMenuDetachFunc detacher)
1159 GtkMenuAttachData *data;
1162 g_return_if_fail (GTK_IS_MENU (menu));
1163 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1165 /* keep this function in sync with gtk_widget_set_parent()
1168 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1171 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1172 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1176 g_object_ref_sink (menu);
1178 data = g_slice_new (GtkMenuAttachData);
1179 data->attach_widget = attach_widget;
1181 g_signal_connect (attach_widget, "screen-changed",
1182 G_CALLBACK (attach_widget_screen_changed), menu);
1183 attach_widget_screen_changed (attach_widget, NULL, menu);
1185 data->detacher = detacher;
1186 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1187 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1188 if (!g_list_find (list, menu))
1190 list = g_list_prepend (list, menu);
1192 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1193 (GDestroyNotify) g_list_free);
1195 if (gtk_widget_get_state (GTK_WIDGET (menu)) != GTK_STATE_NORMAL)
1196 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1198 /* we don't need to set the style here, since
1199 * we are a toplevel widget.
1202 /* Fallback title for menu comes from attach widget */
1203 gtk_menu_update_title (menu);
1205 g_object_notify (G_OBJECT (menu), "attach-widget");
1209 gtk_menu_get_attach_widget (GtkMenu *menu)
1211 GtkMenuAttachData *data;
1213 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1215 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1217 return data->attach_widget;
1222 gtk_menu_detach (GtkMenu *menu)
1224 GtkMenuAttachData *data;
1227 g_return_if_fail (GTK_IS_MENU (menu));
1229 /* keep this function in sync with gtk_widget_unparent()
1231 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1234 g_warning ("gtk_menu_detach(): menu is not attached");
1237 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1239 g_signal_handlers_disconnect_by_func (data->attach_widget,
1240 (gpointer) attach_widget_screen_changed,
1244 data->detacher (data->attach_widget, menu);
1245 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1246 list = g_list_remove (list, menu);
1248 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1249 (GDestroyNotify) g_list_free);
1251 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1253 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1254 gtk_widget_unrealize (GTK_WIDGET (menu));
1256 g_slice_free (GtkMenuAttachData, data);
1258 /* Fallback title for menu comes from attach widget */
1259 gtk_menu_update_title (menu);
1261 g_object_unref (menu);
1265 gtk_menu_remove (GtkContainer *container,
1268 GtkMenu *menu = GTK_MENU (container);
1270 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1272 /* Clear out old_active_menu_item if it matches the item we are removing
1274 if (menu->old_active_menu_item == widget)
1276 g_object_unref (menu->old_active_menu_item);
1277 menu->old_active_menu_item = NULL;
1280 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1281 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1283 menu_queue_resize (menu);
1289 return g_object_new (GTK_TYPE_MENU, NULL);
1293 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1297 GtkMenu *menu = GTK_MENU (menu_shell);
1298 AttachInfo *ai = get_attach_info (child);
1300 ai->left_attach = -1;
1301 ai->right_attach = -1;
1302 ai->top_attach = -1;
1303 ai->bottom_attach = -1;
1305 if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1306 gtk_widget_set_parent_window (child, menu->bin_window);
1308 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1310 menu_queue_resize (menu);
1314 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1319 widget = GTK_WIDGET (menu);
1324 cairo_surface_t *surface;
1325 cairo_pattern_t *pattern;
1328 menu->tearoff_active = FALSE;
1329 menu->saved_scroll_offset = menu->scroll_offset;
1331 window = gtk_widget_get_window (menu->tearoff_window);
1332 width = gdk_window_get_width (window);
1333 height = gdk_window_get_height (window);
1335 surface = gdk_window_create_similar_surface (window,
1336 CAIRO_CONTENT_COLOR,
1340 cr = cairo_create (surface);
1341 gdk_cairo_set_source_window (cr,
1347 gtk_widget_set_size_request (menu->tearoff_window,
1351 pattern = cairo_pattern_create_for_surface (surface);
1352 gdk_window_set_background_pattern (window, pattern);
1354 cairo_pattern_destroy (pattern);
1355 cairo_surface_destroy (surface);
1360 popup_grab_on_window (GdkWindow *window,
1361 GdkDevice *keyboard,
1363 guint32 activate_time)
1366 gdk_device_grab (keyboard, window,
1367 GDK_OWNERSHIP_WINDOW, TRUE,
1368 GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1369 NULL, activate_time) != GDK_GRAB_SUCCESS)
1373 gdk_device_grab (pointer, window,
1374 GDK_OWNERSHIP_WINDOW, TRUE,
1375 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1376 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1377 GDK_POINTER_MOTION_MASK,
1378 NULL, activate_time) != GDK_GRAB_SUCCESS)
1381 gdk_device_ungrab (keyboard, activate_time);
1390 * gtk_menu_popup_for_device:
1391 * @menu: a #GtkMenu.
1392 * @device: (allow-none): a #GdkDevice
1393 * @parent_menu_shell: (allow-none): the menu shell containing the triggering
1394 * menu item, or %NULL
1395 * @parent_menu_item: (allow-none): the menu item whose activation triggered
1396 * the popup, or %NULL
1397 * @func: (allow-none): a user supplied function used to position the menu,
1399 * @data: (allow-none): user supplied data to be passed to @func
1400 * @destroy: (allow-none): destroy notify for @data
1401 * @button: the mouse button which was pressed to initiate the event
1402 * @activate_time: the time at which the activation event occurred
1404 * Displays a menu and makes it available for selection.
1406 * Applications can use this function to display context-sensitive menus,
1407 * and will typically supply %NULL for the @parent_menu_shell,
1408 * @parent_menu_item, @func, @data and @destroy parameters. The default
1409 * menu positioning function will position the menu at the current position
1410 * of @device (or its corresponding pointer).
1412 * The @button parameter should be the mouse button pressed to initiate
1413 * the menu popup. If the menu popup was initiated by something other than
1414 * a mouse button press, such as a mouse button release or a keypress,
1415 * @button should be 0.
1417 * The @activate_time parameter is used to conflict-resolve initiation of
1418 * concurrent requests for mouse/keyboard grab requests. To function
1419 * properly, this needs to be the time stamp of the user event (such as
1420 * a mouse click or key press) that caused the initiation of the popup.
1421 * Only if no such event is available, gtk_get_current_event_time() can
1427 gtk_menu_popup_for_device (GtkMenu *menu,
1429 GtkWidget *parent_menu_shell,
1430 GtkWidget *parent_menu_item,
1431 GtkMenuPositionFunc func,
1433 GDestroyNotify destroy,
1435 guint32 activate_time)
1438 GtkWidget *xgrab_shell;
1440 GdkEvent *current_event;
1441 GtkMenuShell *menu_shell;
1442 gboolean grab_keyboard;
1443 GtkMenuPrivate *priv;
1444 GtkWidget *parent_toplevel;
1445 GdkDevice *keyboard, *pointer;
1447 g_return_if_fail (GTK_IS_MENU (menu));
1448 g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
1451 device = gtk_get_current_event_device ();
1455 GdkDisplay *display;
1456 GdkDeviceManager *device_manager;
1459 display = gtk_widget_get_display (GTK_WIDGET (menu));
1460 device_manager = gdk_display_get_device_manager (display);
1461 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1463 device = devices->data;
1465 g_list_free (devices);
1468 widget = GTK_WIDGET (menu);
1469 menu_shell = GTK_MENU_SHELL (menu);
1470 priv = gtk_menu_get_private (menu);
1472 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
1475 pointer = gdk_device_get_associated_device (device);
1480 keyboard = gdk_device_get_associated_device (device);
1483 menu_shell->parent_menu_shell = parent_menu_shell;
1485 priv->seen_item_enter = FALSE;
1487 /* Find the last viewable ancestor, and make an X grab on it
1489 parent = GTK_WIDGET (menu);
1493 gboolean viewable = TRUE;
1494 GtkWidget *tmp = parent;
1498 if (!gtk_widget_get_mapped (tmp))
1503 tmp = gtk_widget_get_parent (tmp);
1507 xgrab_shell = parent;
1509 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1512 /* We want to receive events generated when we map the menu; unfortunately,
1513 * since there is probably already an implicit grab in place from the
1514 * button that the user used to pop up the menu, we won't receive then --
1515 * in particular, the EnterNotify when the menu pops up under the pointer.
1517 * If we are grabbing on a parent menu shell, no problem; just grab on
1518 * that menu shell first before popping up the window with owner_events = TRUE.
1520 * When grabbing on the menu itself, things get more convuluted - we
1521 * we do an explicit grab on a specially created window with
1522 * owner_events = TRUE, which we override further down with a grab
1523 * on the menu. (We can't grab on the menu until it is mapped; we
1524 * probably could just leave the grab on the other window, with a
1525 * little reorganization of the code in gtkmenu*).
1527 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1528 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1533 if (xgrab_shell && xgrab_shell != widget)
1535 if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1537 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1538 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1543 GdkWindow *transfer_window;
1545 xgrab_shell = widget;
1546 transfer_window = menu_grab_transfer_window_get (menu);
1547 if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1549 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1550 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1554 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1556 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1557 * with a stuck up window, we just abort here. Presumably the user will
1560 menu_shell->parent_menu_shell = NULL;
1561 menu_grab_transfer_window_destroy (menu);
1565 _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1566 menu_shell->active = TRUE;
1567 menu_shell->button = button;
1569 /* If we are popping up the menu from something other than, a button
1570 * press then, as a heuristic, we ignore enter events for the menu
1571 * until we get a MOTION_NOTIFY.
1574 current_event = gtk_get_current_event ();
1577 if ((current_event->type != GDK_BUTTON_PRESS) &&
1578 (current_event->type != GDK_ENTER_NOTIFY))
1579 menu_shell->ignore_enter = TRUE;
1581 gdk_event_free (current_event);
1584 menu_shell->ignore_enter = TRUE;
1588 gtk_menu_tearoff_bg_copy (menu);
1590 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1593 parent_toplevel = NULL;
1594 if (parent_menu_shell)
1595 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1596 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1598 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1600 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1603 /* Set transient for to get the right window group and parent relationship */
1604 if (GTK_IS_WINDOW (parent_toplevel))
1605 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1606 GTK_WINDOW (parent_toplevel));
1608 menu->parent_menu_item = parent_menu_item;
1609 menu->position_func = func;
1610 menu->position_func_data = data;
1611 priv->position_func_data_destroy = destroy;
1612 menu_shell->activate_time = activate_time;
1614 /* We need to show the menu here rather in the init function because
1615 * code expects to be able to tell if the menu is onscreen by
1616 * looking at the gtk_widget_get_visible (menu)
1618 gtk_widget_show (GTK_WIDGET (menu));
1620 /* Position the menu, possibly changing the size request
1622 gtk_menu_position (menu, TRUE);
1624 /* Compute the size of the toplevel and realize it so we
1625 * can scroll correctly.
1628 GtkRequisition tmp_request;
1629 GtkAllocation tmp_allocation = { 0, };
1631 /* Instead of trusting the menu position function to queue a resize when the
1632 * menu goes out of bounds, invalidate the cached size here. */
1633 gtk_widget_queue_resize (GTK_WIDGET (menu));
1634 gtk_widget_get_preferred_size (menu->toplevel, &tmp_request, NULL);
1636 tmp_allocation.width = tmp_request.width;
1637 tmp_allocation.height = tmp_request.height;
1639 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1641 gtk_widget_realize (GTK_WIDGET (menu));
1644 gtk_menu_scroll_to (menu, menu->scroll_offset);
1646 /* if no item is selected, select the first one */
1647 if (!menu_shell->active_menu_item)
1649 gboolean touchscreen_mode;
1651 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1652 "gtk-touchscreen-mode", &touchscreen_mode,
1655 if (touchscreen_mode)
1656 gtk_menu_shell_select_first (menu_shell, TRUE);
1659 /* Once everything is set up correctly, map the toplevel window on
1662 gtk_widget_show (menu->toplevel);
1664 if (xgrab_shell == widget)
1665 popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1667 gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1669 if (parent_menu_shell)
1671 gboolean keyboard_mode;
1673 keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1674 _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1676 else if (menu_shell->button == 0) /* a keynav-activated context menu */
1677 _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1679 _gtk_menu_shell_update_mnemonics (menu_shell);
1684 * @menu: a #GtkMenu.
1685 * @parent_menu_shell: (allow-none): the menu shell containing the triggering menu item, or %NULL
1686 * @parent_menu_item: (allow-none): the menu item whose activation triggered the popup, or %NULL
1687 * @func: (allow-none): a user supplied function used to position the menu, or %NULL
1688 * @data: (allow-none): user supplied data to be passed to @func.
1689 * @button: the mouse button which was pressed to initiate the event.
1690 * @activate_time: the time at which the activation event occurred.
1692 * Displays a menu and makes it available for selection. Applications can use
1693 * this function to display context-sensitive menus, and will typically supply
1694 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1695 * parameters. The default menu positioning function will position the menu
1696 * at the current mouse cursor position.
1698 * The @button parameter should be the mouse button pressed to initiate
1699 * the menu popup. If the menu popup was initiated by something other than
1700 * a mouse button press, such as a mouse button release or a keypress,
1701 * @button should be 0.
1703 * The @activate_time parameter is used to conflict-resolve initiation of
1704 * concurrent requests for mouse/keyboard grab requests. To function
1705 * properly, this needs to be the time stamp of the user event (such as
1706 * a mouse click or key press) that caused the initiation of the popup.
1707 * Only if no such event is available, gtk_get_current_event_time() can
1711 gtk_menu_popup (GtkMenu *menu,
1712 GtkWidget *parent_menu_shell,
1713 GtkWidget *parent_menu_item,
1714 GtkMenuPositionFunc func,
1717 guint32 activate_time)
1719 g_return_if_fail (GTK_IS_MENU (menu));
1721 gtk_menu_popup_for_device (menu,
1726 button, activate_time);
1730 gtk_menu_popdown (GtkMenu *menu)
1732 GtkMenuPrivate *private;
1733 GtkMenuShell *menu_shell;
1736 g_return_if_fail (GTK_IS_MENU (menu));
1738 menu_shell = GTK_MENU_SHELL (menu);
1739 private = gtk_menu_get_private (menu);
1741 menu_shell->parent_menu_shell = NULL;
1742 menu_shell->active = FALSE;
1743 menu_shell->ignore_enter = FALSE;
1745 private->have_position = FALSE;
1747 gtk_menu_stop_scrolling (menu);
1749 gtk_menu_stop_navigating_submenu (menu);
1751 if (menu_shell->active_menu_item)
1753 if (menu->old_active_menu_item)
1754 g_object_unref (menu->old_active_menu_item);
1755 menu->old_active_menu_item = menu_shell->active_menu_item;
1756 g_object_ref (menu->old_active_menu_item);
1759 gtk_menu_shell_deselect (menu_shell);
1761 /* The X Grab, if present, will automatically be removed when we hide
1765 gtk_widget_hide (menu->toplevel);
1766 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1769 pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1773 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1775 if (gtk_bin_get_child (GTK_BIN (menu->toplevel)))
1777 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1781 /* We popped up the menu from the tearoff, so we need to
1782 * release the grab - we aren't actually hiding the menu.
1784 if (menu_shell->have_xgrab && pointer)
1786 GdkDevice *keyboard;
1788 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1789 keyboard = gdk_device_get_associated_device (pointer);
1792 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1796 /* gtk_menu_popdown is called each time a menu item is selected from
1797 * a torn off menu. Only scroll back to the saved position if the
1798 * non-tearoff menu was popped down.
1800 if (!menu->tearoff_active)
1801 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1802 menu->tearoff_active = TRUE;
1805 gtk_widget_hide (GTK_WIDGET (menu));
1807 menu_shell->have_xgrab = FALSE;
1810 gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1812 _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1814 menu_grab_transfer_window_destroy (menu);
1818 gtk_menu_get_active (GtkMenu *menu)
1823 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1825 if (!menu->old_active_menu_item)
1828 children = GTK_MENU_SHELL (menu)->children;
1832 child = children->data;
1833 children = children->next;
1835 if (gtk_bin_get_child (GTK_BIN (child)))
1840 menu->old_active_menu_item = child;
1841 if (menu->old_active_menu_item)
1842 g_object_ref (menu->old_active_menu_item);
1845 return menu->old_active_menu_item;
1849 gtk_menu_set_active (GtkMenu *menu,
1855 g_return_if_fail (GTK_IS_MENU (menu));
1857 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1860 child = tmp_list->data;
1861 if (gtk_bin_get_child (GTK_BIN (child)))
1863 if (menu->old_active_menu_item)
1864 g_object_unref (menu->old_active_menu_item);
1865 menu->old_active_menu_item = child;
1866 g_object_ref (menu->old_active_menu_item);
1873 * gtk_menu_set_accel_group:
1874 * @accel_group: (allow-none):
1877 gtk_menu_set_accel_group (GtkMenu *menu,
1878 GtkAccelGroup *accel_group)
1880 g_return_if_fail (GTK_IS_MENU (menu));
1882 if (menu->accel_group != accel_group)
1884 if (menu->accel_group)
1885 g_object_unref (menu->accel_group);
1886 menu->accel_group = accel_group;
1887 if (menu->accel_group)
1888 g_object_ref (menu->accel_group);
1889 _gtk_menu_refresh_accel_paths (menu, TRUE);
1894 gtk_menu_get_accel_group (GtkMenu *menu)
1896 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1898 return menu->accel_group;
1902 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1905 /* Menu items chain here to figure whether they can activate their
1906 * accelerators. Unlike ordinary widgets, menus allow accel
1907 * activation even if invisible since that's the usual case for
1908 * submenus/popup-menus. however, the state of the attach widget
1909 * affects the "activeness" of the menu.
1911 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1914 return gtk_widget_can_activate_accel (awidget, signal_id);
1916 return gtk_widget_is_sensitive (widget);
1920 * gtk_menu_set_accel_path
1921 * @menu: a valid #GtkMenu
1922 * @accel_path: (allow-none): a valid accelerator path
1924 * Sets an accelerator path for this menu from which accelerator paths
1925 * for its immediate children, its menu items, can be constructed.
1926 * The main purpose of this function is to spare the programmer the
1927 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1928 * each menu item that should support runtime user changable accelerators.
1929 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1930 * each menu item of this menu, that contains a label describing its purpose,
1931 * automatically gets an accel path assigned. For example, a menu containing
1932 * menu items "New" and "Exit", will, after
1933 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1934 * has been called, assign its items the accel paths:
1935 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1936 * Assigning accel paths to menu items then enables the user to change
1937 * their accelerators at runtime. More details about accelerator paths
1938 * and their default setups can be found at gtk_accel_map_add_entry().
1940 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1941 * pass a static string, you can save some memory by interning it first with
1942 * g_intern_static_string().
1945 gtk_menu_set_accel_path (GtkMenu *menu,
1946 const gchar *accel_path)
1948 g_return_if_fail (GTK_IS_MENU (menu));
1950 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1952 /* FIXME: accel_path should be defined as const gchar* */
1953 menu->accel_path = (gchar*)g_intern_string (accel_path);
1954 if (menu->accel_path)
1955 _gtk_menu_refresh_accel_paths (menu, FALSE);
1959 * gtk_menu_get_accel_path
1960 * @menu: a valid #GtkMenu
1962 * Retrieves the accelerator path set on the menu.
1964 * Returns: the accelerator path set on the menu.
1969 gtk_menu_get_accel_path (GtkMenu *menu)
1971 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1973 return menu->accel_path;
1978 gboolean group_changed;
1982 refresh_accel_paths_foreach (GtkWidget *widget,
1985 AccelPropagation *prop = data;
1987 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1988 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1989 prop->menu->accel_path,
1990 prop->menu->accel_group,
1991 prop->group_changed);
1995 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1996 gboolean group_changed)
1998 g_return_if_fail (GTK_IS_MENU (menu));
2000 if (menu->accel_path && menu->accel_group)
2002 AccelPropagation prop;
2005 prop.group_changed = group_changed;
2006 gtk_container_foreach (GTK_CONTAINER (menu),
2007 refresh_accel_paths_foreach,
2013 gtk_menu_reposition (GtkMenu *menu)
2015 g_return_if_fail (GTK_IS_MENU (menu));
2017 if (!menu->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2018 gtk_menu_position (menu, FALSE);
2022 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2025 g_return_if_fail (GTK_IS_MENU (menu));
2027 if (adjustment->value != menu->scroll_offset)
2028 gtk_menu_scroll_to (menu, adjustment->value);
2032 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2035 GdkGeometry geometry_hints;
2036 GtkMenuPrivate *priv;
2038 if (!menu->tearoff_window)
2041 priv = gtk_menu_get_private (menu);
2043 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2045 GtkRequisition requisition;
2047 gtk_widget_get_preferred_size (menu->tearoff_scrollbar,
2048 &requisition, NULL);
2049 width += requisition.width;
2052 geometry_hints.min_width = width;
2053 geometry_hints.max_width = width;
2055 geometry_hints.min_height = 0;
2056 geometry_hints.max_height = priv->requested_height;
2058 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
2061 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2065 gtk_menu_update_title (GtkMenu *menu)
2067 if (menu->tearoff_window)
2070 GtkWidget *attach_widget;
2072 title = gtk_menu_get_title (menu);
2075 attach_widget = gtk_menu_get_attach_widget (menu);
2076 if (GTK_IS_MENU_ITEM (attach_widget))
2078 GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2079 if (GTK_IS_LABEL (child))
2080 title = gtk_label_get_text (GTK_LABEL (child));
2085 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
2090 gtk_menu_get_toplevel (GtkWidget *menu)
2092 GtkWidget *attach, *toplevel;
2094 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2096 if (GTK_IS_MENU_ITEM (attach))
2097 attach = gtk_widget_get_parent (attach);
2099 if (GTK_IS_MENU (attach))
2100 return gtk_menu_get_toplevel (attach);
2101 else if (GTK_IS_WIDGET (attach))
2103 toplevel = gtk_widget_get_toplevel (attach);
2104 if (gtk_widget_is_toplevel (toplevel))
2112 tearoff_window_destroyed (GtkWidget *widget,
2115 gtk_menu_set_tearoff_state (menu, FALSE);
2119 gtk_menu_set_tearoff_state (GtkMenu *menu,
2123 GtkMenuPrivate *priv;
2125 g_return_if_fail (GTK_IS_MENU (menu));
2127 priv = gtk_menu_get_private (menu);
2129 if (menu->torn_off != torn_off)
2131 menu->torn_off = torn_off;
2132 menu->tearoff_active = torn_off;
2136 if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2137 gtk_menu_popdown (menu);
2139 if (!menu->tearoff_window)
2141 GtkWidget *toplevel;
2143 menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2144 "type", GTK_WINDOW_TOPLEVEL,
2145 "screen", gtk_widget_get_screen (menu->toplevel),
2146 "app-paintable", TRUE,
2149 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
2150 GDK_WINDOW_TYPE_HINT_MENU);
2151 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
2152 g_signal_connect (menu->tearoff_window, "destroy",
2153 G_CALLBACK (tearoff_window_destroyed), menu);
2154 g_signal_connect (menu->tearoff_window, "event",
2155 G_CALLBACK (gtk_menu_window_event), menu);
2157 gtk_menu_update_title (menu);
2159 gtk_widget_realize (menu->tearoff_window);
2161 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2162 if (toplevel != NULL)
2163 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
2164 GTK_WINDOW (toplevel));
2166 menu->tearoff_hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
2167 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
2169 height = gdk_window_get_height (gtk_widget_get_window (GTK_WIDGET (menu)));
2170 menu->tearoff_adjustment = gtk_adjustment_new (0,
2171 0, priv->requested_height,
2175 g_object_connect (menu->tearoff_adjustment,
2176 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2178 menu->tearoff_scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, menu->tearoff_adjustment);
2180 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
2181 menu->tearoff_scrollbar,
2184 if (menu->tearoff_adjustment->upper > height)
2185 gtk_widget_show (menu->tearoff_scrollbar);
2187 gtk_widget_show (menu->tearoff_hbox);
2190 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
2192 /* Update menu->requisition
2194 gtk_widget_get_preferred_size (GTK_WIDGET (menu),
2197 gtk_menu_set_tearoff_hints (menu, gdk_window_get_width (gtk_widget_get_window (GTK_WIDGET (menu))));
2199 gtk_widget_realize (menu->tearoff_window);
2200 gtk_menu_position (menu, TRUE);
2202 gtk_widget_show (GTK_WIDGET (menu));
2203 gtk_widget_show (menu->tearoff_window);
2205 gtk_menu_scroll_to (menu, 0);
2210 gtk_widget_hide (GTK_WIDGET (menu));
2211 gtk_widget_hide (menu->tearoff_window);
2212 if (GTK_IS_CONTAINER (menu->toplevel))
2213 gtk_menu_reparent (menu, menu->toplevel, FALSE);
2214 gtk_widget_destroy (menu->tearoff_window);
2216 menu->tearoff_window = NULL;
2217 menu->tearoff_hbox = NULL;
2218 menu->tearoff_scrollbar = NULL;
2219 menu->tearoff_adjustment = NULL;
2222 g_object_notify (G_OBJECT (menu), "tearoff-state");
2227 * gtk_menu_get_tearoff_state:
2230 * Returns whether the menu is torn off. See
2231 * gtk_menu_set_tearoff_state ().
2233 * Return value: %TRUE if the menu is currently torn off.
2236 gtk_menu_get_tearoff_state (GtkMenu *menu)
2238 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2240 return menu->torn_off;
2244 * gtk_menu_set_title:
2246 * @title: a string containing the title for the menu.
2248 * Sets the title string for the menu. The title is displayed when the menu
2249 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
2250 * attached to a parent menu item, and if so it will try to use the same text as
2251 * that menu item's label.
2254 gtk_menu_set_title (GtkMenu *menu,
2257 GtkMenuPrivate *priv;
2260 g_return_if_fail (GTK_IS_MENU (menu));
2262 priv = gtk_menu_get_private (menu);
2264 old_title = priv->title;
2265 priv->title = g_strdup (title);
2268 gtk_menu_update_title (menu);
2269 g_object_notify (G_OBJECT (menu), "tearoff-title");
2273 * gtk_menu_get_title:
2276 * Returns the title of the menu. See gtk_menu_set_title().
2278 * Return value: the title of the menu, or %NULL if the menu has no
2279 * title set on it. This string is owned by the widget and should
2280 * not be modified or freed.
2282 G_CONST_RETURN gchar *
2283 gtk_menu_get_title (GtkMenu *menu)
2285 GtkMenuPrivate *priv;
2287 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2289 priv = gtk_menu_get_private (menu);
2295 gtk_menu_reorder_child (GtkMenu *menu,
2299 GtkMenuShell *menu_shell;
2301 g_return_if_fail (GTK_IS_MENU (menu));
2302 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2304 menu_shell = GTK_MENU_SHELL (menu);
2306 if (g_list_find (menu_shell->children, child))
2308 menu_shell->children = g_list_remove (menu_shell->children, child);
2309 menu_shell->children = g_list_insert (menu_shell->children, child, position);
2311 menu_queue_resize (menu);
2316 gtk_menu_style_set (GtkWidget *widget,
2317 GtkStyle *previous_style)
2319 if (gtk_widget_get_realized (widget))
2321 GtkMenu *menu = GTK_MENU (widget);
2324 style = gtk_widget_get_style (widget);
2326 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2327 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2328 gtk_style_set_background (style, gtk_widget_get_window (widget), GTK_STATE_NORMAL);
2333 get_arrows_border (GtkMenu *menu,
2336 guint scroll_arrow_height;
2337 GtkArrowPlacement arrow_placement;
2339 gtk_widget_style_get (GTK_WIDGET (menu),
2340 "scroll-arrow-vlength", &scroll_arrow_height,
2341 "arrow_placement", &arrow_placement,
2344 switch (arrow_placement)
2346 case GTK_ARROWS_BOTH:
2347 border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2348 border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2351 case GTK_ARROWS_START:
2352 border->top = (menu->upper_arrow_visible ||
2353 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2357 case GTK_ARROWS_END:
2359 border->bottom = (menu->upper_arrow_visible ||
2360 menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2364 border->left = border->right = 0;
2368 gtk_menu_realize (GtkWidget *widget)
2370 GtkAllocation allocation;
2373 GdkWindowAttr attributes;
2374 gint attributes_mask;
2377 GtkMenuPrivate *priv;
2380 guint vertical_padding;
2381 guint horizontal_padding;
2382 GtkBorder arrow_border;
2384 g_return_if_fail (GTK_IS_MENU (widget));
2386 menu = GTK_MENU (widget);
2387 priv = gtk_menu_get_private (menu);
2389 gtk_widget_set_realized (widget, TRUE);
2391 gtk_widget_get_allocation (widget, &allocation);
2393 attributes.window_type = GDK_WINDOW_CHILD;
2394 attributes.x = allocation.x;
2395 attributes.y = allocation.y;
2396 attributes.width = allocation.width;
2397 attributes.height = allocation.height;
2398 attributes.wclass = GDK_INPUT_OUTPUT;
2399 attributes.visual = gtk_widget_get_visual (widget);
2400 attributes.event_mask = gtk_widget_get_events (widget);
2401 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2402 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2404 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
2406 window = gdk_window_new (gtk_widget_get_parent_window (widget),
2407 &attributes, attributes_mask);
2408 gtk_widget_set_window (widget, window);
2409 gdk_window_set_user_data (window, widget);
2411 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2413 style = gtk_widget_get_style (widget);
2414 gtk_widget_style_get (GTK_WIDGET (menu),
2415 "vertical-padding", &vertical_padding,
2416 "horizontal-padding", &horizontal_padding,
2419 gtk_widget_get_allocation (widget, &allocation);
2421 attributes.x = border_width + style->xthickness + horizontal_padding;
2422 attributes.y = border_width + style->ythickness + vertical_padding;
2423 attributes.width = MAX (1, allocation.width - attributes.x * 2);
2424 attributes.height = MAX (1, allocation.height - attributes.y * 2);
2426 get_arrows_border (menu, &arrow_border);
2427 attributes.y += arrow_border.top;
2428 attributes.height -= arrow_border.top;
2429 attributes.height -= arrow_border.bottom;
2431 menu->view_window = gdk_window_new (window,
2432 &attributes, attributes_mask);
2433 gdk_window_set_user_data (menu->view_window, menu);
2435 gtk_widget_get_allocation (widget, &allocation);
2439 attributes.width = MAX (1, allocation.width - (border_width + style->xthickness + horizontal_padding) * 2);
2440 attributes.height = MAX (1, priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2);
2442 menu->bin_window = gdk_window_new (menu->view_window,
2443 &attributes, attributes_mask);
2444 gdk_window_set_user_data (menu->bin_window, menu);
2446 children = GTK_MENU_SHELL (menu)->children;
2449 child = children->data;
2450 children = children->next;
2452 gtk_widget_set_parent_window (child, menu->bin_window);
2455 gtk_widget_style_attach (widget);
2456 gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2457 gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2458 gtk_style_set_background (style, window, GTK_STATE_NORMAL);
2460 if (GTK_MENU_SHELL (widget)->active_menu_item)
2461 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2462 GTK_MENU_SHELL (widget)->active_menu_item);
2464 gdk_window_show (menu->bin_window);
2465 gdk_window_show (menu->view_window);
2469 gtk_menu_focus (GtkWidget *widget,
2470 GtkDirectionType direction)
2473 * A menu or its menu items cannot have focus
2478 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2481 menu_grab_transfer_window_get (GtkMenu *menu)
2483 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2486 GdkWindowAttr attributes;
2487 gint attributes_mask;
2489 attributes.x = -100;
2490 attributes.y = -100;
2491 attributes.width = 10;
2492 attributes.height = 10;
2493 attributes.window_type = GDK_WINDOW_TEMP;
2494 attributes.wclass = GDK_INPUT_ONLY;
2495 attributes.override_redirect = TRUE;
2496 attributes.event_mask = 0;
2498 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2500 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2501 &attributes, attributes_mask);
2502 gdk_window_set_user_data (window, menu);
2504 gdk_window_show (window);
2506 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2513 menu_grab_transfer_window_destroy (GtkMenu *menu)
2515 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2518 gdk_window_set_user_data (window, NULL);
2519 gdk_window_destroy (window);
2520 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2525 gtk_menu_unrealize (GtkWidget *widget)
2527 GtkMenu *menu = GTK_MENU (widget);
2529 menu_grab_transfer_window_destroy (menu);
2531 gdk_window_set_user_data (menu->view_window, NULL);
2532 gdk_window_destroy (menu->view_window);
2533 menu->view_window = NULL;
2535 gdk_window_set_user_data (menu->bin_window, NULL);
2536 gdk_window_destroy (menu->bin_window);
2537 menu->bin_window = NULL;
2539 GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2544 calculate_line_heights (GtkMenu *menu,
2546 guint **ret_min_heights,
2547 guint **ret_nat_heights)
2549 GtkMenuShell *menu_shell;
2550 GtkMenuPrivate *priv;
2551 GtkWidget *child, *widget;
2553 guint horizontal_padding;
2561 widget = GTK_WIDGET (menu);
2562 menu_shell = GTK_MENU_SHELL (widget);
2563 priv = gtk_menu_get_private (menu);
2565 min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2566 nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2567 n_heights = gtk_menu_get_n_rows (menu);
2568 n_columns = gtk_menu_get_n_columns (menu);
2569 avail_width = for_width - (2 * menu->toggle_size + priv->accel_size) * n_columns;
2571 gtk_widget_style_get (GTK_WIDGET (menu),
2572 "horizontal-padding", &horizontal_padding,
2575 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2576 avail_width -= (border_width + horizontal_padding + gtk_widget_get_style (widget)->xthickness) * 2;
2578 for (children = menu_shell->children; children; children = children->next)
2583 gint child_min, child_nat;
2585 child = children->data;
2587 if (! gtk_widget_get_visible (child))
2590 get_effective_child_attach (child, &l, &r, &t, &b);
2592 part = avail_width / (r - l);
2594 gtk_widget_get_preferred_height_for_width (child, part,
2595 &child_min, &child_nat);
2597 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2599 part = MAX (child_min, toggle_size) / (b - t);
2600 min_heights[t] = MAX (min_heights[t], part);
2602 part = MAX (child_nat, toggle_size) / (b - t);
2603 nat_heights[t] = MAX (nat_heights[t], part);
2606 if (ret_min_heights)
2607 *ret_min_heights = min_heights;
2609 g_free (min_heights);
2611 if (ret_nat_heights)
2612 *ret_nat_heights = nat_heights;
2614 g_free (nat_heights);
2620 gtk_menu_size_allocate (GtkWidget *widget,
2621 GtkAllocation *allocation)
2624 GtkMenuShell *menu_shell;
2626 GtkAllocation child_allocation;
2627 GtkMenuPrivate *priv;
2633 guint vertical_padding;
2634 guint horizontal_padding;
2636 g_return_if_fail (GTK_IS_MENU (widget));
2637 g_return_if_fail (allocation != NULL);
2639 menu = GTK_MENU (widget);
2640 menu_shell = GTK_MENU_SHELL (widget);
2641 priv = gtk_menu_get_private (menu);
2643 gtk_widget_set_allocation (widget, allocation);
2645 style = gtk_widget_get_style (widget);
2647 gtk_widget_style_get (GTK_WIDGET (menu),
2648 "vertical-padding", &vertical_padding,
2649 "horizontal-padding", &horizontal_padding,
2651 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2653 g_free (priv->heights);
2654 priv->heights_length =
2655 calculate_line_heights (menu,
2660 /* refresh our cached height request */
2661 priv->requested_height = (border_width + vertical_padding + style->ythickness) * 2;
2662 for (i = 0; i < priv->heights_length; i++)
2663 priv->requested_height += priv->heights[i];
2665 x = border_width + style->xthickness + horizontal_padding;
2666 y = border_width + style->ythickness + vertical_padding;
2668 width = MAX (1, allocation->width - x * 2);
2669 height = MAX (1, allocation->height - y * 2);
2671 if (menu_shell->active)
2672 gtk_menu_scroll_to (menu, menu->scroll_offset);
2674 if (!menu->tearoff_active)
2676 GtkBorder arrow_border;
2678 get_arrows_border (menu, &arrow_border);
2679 y += arrow_border.top;
2680 height -= arrow_border.top;
2681 height -= arrow_border.bottom;
2684 if (gtk_widget_get_realized (widget))
2686 gdk_window_move_resize (gtk_widget_get_window (widget),
2687 allocation->x, allocation->y,
2688 allocation->width, allocation->height);
2690 gdk_window_move_resize (menu->view_window,
2697 if (menu_shell->children)
2699 gint base_width = width / gtk_menu_get_n_columns (menu);
2701 children = menu_shell->children;
2704 child = children->data;
2705 children = children->next;
2707 if (gtk_widget_get_visible (child))
2712 get_effective_child_attach (child, &l, &r, &t, &b);
2714 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2717 tmp = gtk_menu_get_n_columns (menu) - l;
2718 l = gtk_menu_get_n_columns (menu) - r;
2722 child_allocation.width = (r - l) * base_width;
2723 child_allocation.height = 0;
2724 child_allocation.x = l * base_width;
2725 child_allocation.y = 0;
2727 for (i = 0; i < b; i++)
2730 child_allocation.y += priv->heights[i];
2732 child_allocation.height += priv->heights[i];
2735 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2738 gtk_widget_size_allocate (child, &child_allocation);
2739 gtk_widget_queue_draw (child);
2743 /* Resize the item window */
2744 if (gtk_widget_get_realized (widget))
2750 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2751 height += priv->heights[i];
2753 width = gtk_menu_get_n_columns (menu) * base_width;
2754 gdk_window_resize (menu->bin_window, width, height);
2757 if (menu->tearoff_active)
2759 if (height >= priv->requested_height)
2761 if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2763 gtk_widget_hide (menu->tearoff_scrollbar);
2764 gtk_menu_set_tearoff_hints (menu, allocation->width);
2766 gtk_menu_scroll_to (menu, 0);
2771 menu->tearoff_adjustment->upper = priv->requested_height;
2772 menu->tearoff_adjustment->page_size = allocation->height;
2774 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2775 menu->tearoff_adjustment->upper)
2778 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2781 gtk_menu_scroll_to (menu, value);
2784 gtk_adjustment_changed (menu->tearoff_adjustment);
2786 if (!gtk_widget_get_visible (menu->tearoff_scrollbar))
2788 gtk_widget_show (menu->tearoff_scrollbar);
2789 gtk_menu_set_tearoff_hints (menu, allocation->width);
2797 get_arrows_visible_area (GtkMenu *menu,
2798 GdkRectangle *border,
2799 GdkRectangle *upper,
2800 GdkRectangle *lower,
2803 GtkArrowPlacement arrow_placement;
2805 GtkWidget *widget = GTK_WIDGET (menu);
2807 guint vertical_padding;
2808 guint horizontal_padding;
2809 gint scroll_arrow_height;
2811 style = gtk_widget_get_style (widget);
2813 gtk_widget_style_get (widget,
2814 "vertical-padding", &vertical_padding,
2815 "horizontal-padding", &horizontal_padding,
2816 "scroll-arrow-vlength", &scroll_arrow_height,
2817 "arrow-placement", &arrow_placement,
2820 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2821 border->x = border_width + style->xthickness + horizontal_padding;
2822 border->y = border_width + style->ythickness + vertical_padding;
2823 border->width = gdk_window_get_width (gtk_widget_get_window (widget));
2824 border->height = gdk_window_get_height (gtk_widget_get_window (widget));
2826 switch (arrow_placement)
2828 case GTK_ARROWS_BOTH:
2829 upper->x = border->x;
2830 upper->y = border->y;
2831 upper->width = border->width - 2 * border->x;
2832 upper->height = scroll_arrow_height;
2834 lower->x = border->x;
2835 lower->y = border->height - border->y - scroll_arrow_height;
2836 lower->width = border->width - 2 * border->x;
2837 lower->height = scroll_arrow_height;
2840 case GTK_ARROWS_START:
2841 upper->x = border->x;
2842 upper->y = border->y;
2843 upper->width = (border->width - 2 * border->x) / 2;
2844 upper->height = scroll_arrow_height;
2846 lower->x = border->x + upper->width;
2847 lower->y = border->y;
2848 lower->width = (border->width - 2 * border->x) / 2;
2849 lower->height = scroll_arrow_height;
2852 case GTK_ARROWS_END:
2853 upper->x = border->x;
2854 upper->y = border->height - border->y - scroll_arrow_height;
2855 upper->width = (border->width - 2 * border->x) / 2;
2856 upper->height = scroll_arrow_height;
2858 lower->x = border->x + upper->width;
2859 lower->y = border->height - border->y - scroll_arrow_height;
2860 lower->width = (border->width - 2 * border->x) / 2;
2861 lower->height = scroll_arrow_height;
2865 g_assert_not_reached();
2866 upper->x = upper->y = upper->width = upper->height = 0;
2867 lower->x = lower->y = lower->width = lower->height = 0;
2870 *arrow_space = scroll_arrow_height - 2 * style->ythickness;
2874 gtk_menu_draw (GtkWidget *widget,
2878 GtkMenuPrivate *priv;
2880 GdkRectangle border;
2886 menu = GTK_MENU (widget);
2887 priv = gtk_menu_get_private (menu);
2889 style = gtk_widget_get_style (widget);
2890 window = gtk_widget_get_window (widget);
2892 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2894 if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
2896 gfloat arrow_scaling;
2899 gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
2900 arrow_size = arrow_scaling * arrow_space;
2902 gtk_paint_box (style,
2908 gtk_widget_get_allocated_width (widget),
2909 gtk_widget_get_allocated_height (widget));
2911 if (menu->upper_arrow_visible && !menu->tearoff_active)
2913 gtk_paint_box (style,
2915 priv->upper_arrow_state,
2917 widget, "menu_scroll_arrow_up",
2923 gtk_paint_arrow (style,
2925 priv->upper_arrow_state,
2927 widget, "menu_scroll_arrow_up",
2930 upper.x + (upper.width - arrow_size) / 2,
2931 upper.y + style->ythickness + (arrow_space - arrow_size) / 2,
2932 arrow_size, arrow_size);
2935 if (menu->lower_arrow_visible && !menu->tearoff_active)
2937 gtk_paint_box (style,
2939 priv->lower_arrow_state,
2941 widget, "menu_scroll_arrow_down",
2947 gtk_paint_arrow (style,
2949 priv->lower_arrow_state,
2951 widget, "menu_scroll_arrow_down",
2954 lower.x + (lower.width - arrow_size) / 2,
2955 lower.y + style->ythickness + (arrow_space - arrow_size) / 2,
2956 arrow_size, arrow_size);
2960 if (gtk_cairo_should_draw_window (cr, menu->bin_window))
2962 gint y = -border.y + menu->scroll_offset;
2965 gtk_cairo_transform_to_window (cr, widget, menu->bin_window);
2967 if (!menu->tearoff_active)
2969 GtkBorder arrow_border;
2971 get_arrows_border (menu, &arrow_border);
2972 y -= arrow_border.top;
2975 gtk_paint_box (style,
2981 border.width, border.height);
2986 GTK_WIDGET_CLASS (gtk_menu_parent_class)->draw (widget, cr);
2992 gtk_menu_show (GtkWidget *widget)
2994 GtkMenu *menu = GTK_MENU (widget);
2996 _gtk_menu_refresh_accel_paths (menu, FALSE);
2998 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3003 gtk_menu_get_preferred_width (GtkWidget *widget,
3008 GtkMenuShell *menu_shell;
3009 GtkMenuPrivate *priv;
3013 guint max_toggle_size;
3014 guint max_accel_width;
3015 guint horizontal_padding;
3017 gint child_min, child_nat;
3018 gint min_width, nat_width;
3020 menu = GTK_MENU (widget);
3021 menu_shell = GTK_MENU_SHELL (widget);
3022 priv = gtk_menu_get_private (menu);
3024 style = gtk_widget_get_style (GTK_WIDGET (widget));
3026 min_width = nat_width = 0;
3028 max_toggle_size = 0;
3029 max_accel_width = 0;
3031 children = menu_shell->children;
3038 child = children->data;
3039 children = children->next;
3041 if (! gtk_widget_get_visible (child))
3044 get_effective_child_attach (child, &l, &r, &t, &b);
3046 /* It's important to size_request the child
3047 * before doing the toggle size request, in
3048 * case the toggle size request depends on the size
3049 * request of a child of the child (e.g. for ImageMenuItem)
3052 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
3053 gtk_widget_get_preferred_width (child, &child_min, &child_nat);
3055 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3056 max_toggle_size = MAX (max_toggle_size, toggle_size);
3057 max_accel_width = MAX (max_accel_width,
3058 GTK_MENU_ITEM (child)->accelerator_width);
3060 part = child_min / (r - l);
3061 min_width = MAX (min_width, part);
3063 part = child_nat / (r - l);
3064 nat_width = MAX (nat_width, part);
3067 /* If the menu doesn't include any images or check items
3068 * reserve the space so that all menus are consistent.
3069 * We only do this for 'ordinary' menus, not for combobox
3070 * menus or multi-column menus
3072 if (max_toggle_size == 0 &&
3073 gtk_menu_get_n_columns (menu) == 1 &&
3074 !priv->no_toggle_size)
3076 guint toggle_spacing;
3077 guint indicator_size;
3079 gtk_style_get (style,
3080 GTK_TYPE_CHECK_MENU_ITEM,
3081 "toggle-spacing", &toggle_spacing,
3082 "indicator-size", &indicator_size,
3085 max_toggle_size = indicator_size + toggle_spacing;
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 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3099 min_width += (border_width + horizontal_padding + style->xthickness) * 2;
3100 nat_width += (border_width + horizontal_padding + style->xthickness) * 2;
3102 menu->toggle_size = max_toggle_size;
3103 priv->accel_size = max_accel_width;
3106 *minimum_size = min_width;
3109 *natural_size = nat_width;
3111 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
3113 if (menu->tearoff_active)
3114 gtk_menu_set_tearoff_hints (menu, min_width);
3118 gtk_menu_get_preferred_height (GtkWidget *widget,
3124 /* Menus are height-for-width only, just return the height for the minimum width */
3125 GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
3126 GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width, minimum_size, natural_size);
3130 gtk_menu_get_preferred_height_for_width (GtkWidget *widget,
3135 GtkMenu *menu = GTK_MENU (widget);
3136 GtkMenuPrivate *private = gtk_menu_get_private (menu);
3137 guint *min_heights, *nat_heights;
3138 guint vertical_padding, border_width;
3140 gint min_height, nat_height;
3142 gtk_widget_style_get (GTK_WIDGET (menu), "vertical-padding", &vertical_padding, NULL);
3143 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3145 min_height = nat_height = (border_width + vertical_padding + gtk_widget_get_style (GTK_WIDGET (widget))->ythickness) * 2;
3148 calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3150 for (i = 0; i < n_heights; i++)
3152 min_height += min_heights[i];
3153 nat_height += nat_heights[i];
3156 if (private->have_position)
3158 GdkScreen *screen = gtk_widget_get_screen (menu->toplevel);
3159 GdkRectangle monitor;
3161 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3163 if (private->y + min_height > monitor.y + monitor.height)
3164 min_height = monitor.y + monitor.height - private->y;
3166 if (private->y + nat_height > monitor.y + monitor.height)
3167 nat_height = monitor.y + monitor.height - private->y;
3169 if (private->y < monitor.y)
3171 min_height -= monitor.y - private->y;
3172 nat_height -= monitor.y - private->y;
3177 *minimum_size = min_height;
3180 *natural_size = nat_height;
3182 g_free (min_heights);
3183 g_free (nat_heights);
3189 gtk_menu_button_scroll (GtkMenu *menu,
3190 GdkEventButton *event)
3192 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
3194 gboolean touchscreen_mode;
3196 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3197 "gtk-touchscreen-mode", &touchscreen_mode,
3200 if (touchscreen_mode)
3201 gtk_menu_handle_scrolling (menu,
3202 event->x_root, event->y_root,
3203 event->type == GDK_BUTTON_PRESS,
3213 pointer_in_menu_window (GtkWidget *widget,
3217 GtkAllocation allocation;
3218 GtkMenu *menu = GTK_MENU (widget);
3220 if (gtk_widget_get_mapped (menu->toplevel))
3222 GtkMenuShell *menu_shell;
3223 gint window_x, window_y;
3225 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3226 &window_x, &window_y);
3228 gtk_widget_get_allocation (widget, &allocation);
3229 if (x_root >= window_x && x_root < window_x + allocation.width &&
3230 y_root >= window_y && y_root < window_y + allocation.height)
3233 menu_shell = GTK_MENU_SHELL (widget);
3235 if (GTK_IS_MENU (menu_shell->parent_menu_shell))
3236 return pointer_in_menu_window (menu_shell->parent_menu_shell,
3244 gtk_menu_button_press (GtkWidget *widget,
3245 GdkEventButton *event)
3247 if (event->type != GDK_BUTTON_PRESS)
3250 /* Don't pass down to menu shell for presses over scroll arrows
3252 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3255 /* Don't pass down to menu shell if a non-menuitem part of the menu
3256 * was clicked. The check for the event_widget being a GtkMenuShell
3257 * works because we have the pointer grabbed on menu_shell->window
3258 * with owner_events=TRUE, so all events that are either outside
3259 * the menu or on its border are delivered relative to
3260 * menu_shell->window.
3262 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3263 pointer_in_menu_window (widget, event->x_root, event->y_root))
3266 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3270 gtk_menu_button_release (GtkWidget *widget,
3271 GdkEventButton *event)
3273 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3275 if (priv->ignore_button_release)
3277 priv->ignore_button_release = FALSE;
3281 if (event->type != GDK_BUTTON_RELEASE)
3284 /* Don't pass down to menu shell for releases over scroll arrows
3286 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3289 /* Don't pass down to menu shell if a non-menuitem part of the menu
3290 * was clicked (see comment in button_press()).
3292 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3293 pointer_in_menu_window (widget, event->x_root, event->y_root))
3295 /* Ugly: make sure menu_shell->button gets reset to 0 when we
3296 * bail out early here so it is in a consistent state for the
3297 * next button_press/button_release in GtkMenuShell.
3300 if (GTK_MENU_SHELL (widget)->active)
3301 GTK_MENU_SHELL (widget)->button = 0;
3306 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3309 static const gchar *
3310 get_accel_path (GtkWidget *menu_item,
3315 GClosure *accel_closure;
3316 GtkAccelGroup *accel_group;
3318 path = _gtk_widget_get_accel_path (menu_item, locked);
3321 path = GTK_MENU_ITEM (menu_item)->accel_path;
3327 label = gtk_bin_get_child (GTK_BIN (menu_item));
3329 if (GTK_IS_ACCEL_LABEL (label))
3331 g_object_get (label,
3332 "accel-closure", &accel_closure,
3336 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3338 *locked = gtk_accel_group_get_is_locked (accel_group);
3348 gtk_menu_key_press (GtkWidget *widget,
3351 GtkMenuShell *menu_shell;
3353 gboolean delete = FALSE;
3354 gboolean can_change_accels;
3355 gchar *accel = NULL;
3356 guint accel_key, accel_mods;
3357 GdkModifierType consumed_modifiers;
3358 GdkDisplay *display;
3360 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3361 g_return_val_if_fail (event != NULL, FALSE);
3363 menu_shell = GTK_MENU_SHELL (widget);
3364 menu = GTK_MENU (widget);
3366 gtk_menu_stop_navigating_submenu (menu);
3368 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3371 display = gtk_widget_get_display (widget);
3373 g_object_get (gtk_widget_get_settings (widget),
3374 "gtk-menu-bar-accel", &accel,
3375 "gtk-can-change-accels", &can_change_accels,
3378 if (accel && *accel)
3381 GdkModifierType mods = 0;
3383 gtk_accelerator_parse (accel, &keyval, &mods);
3386 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3388 /* FIXME this is wrong, needs to be in the global accel resolution
3389 * thing, to properly consider i18n etc., but that probably requires
3390 * AccelGroup changes etc.
3392 if (event->keyval == keyval && (mods & event->state) == mods)
3394 gtk_menu_shell_cancel (menu_shell);
3402 switch (event->keyval)
3404 case GDK_KEY_Delete:
3405 case GDK_KEY_KP_Delete:
3406 case GDK_KEY_BackSpace:
3413 /* Figure out what modifiers went into determining the key symbol */
3414 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3415 event->hardware_keycode, event->state, event->group,
3416 NULL, NULL, NULL, &consumed_modifiers);
3418 accel_key = gdk_keyval_to_lower (event->keyval);
3419 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3421 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
3422 * We re-upper case when we match against the keyval, but display and save in caseless form.
3424 if (accel_key != event->keyval)
3425 accel_mods |= GDK_SHIFT_MASK;
3427 /* Modify the accelerators */
3428 if (can_change_accels &&
3429 menu_shell->active_menu_item &&
3430 gtk_bin_get_child (GTK_BIN (menu_shell->active_menu_item)) && /* no separators */
3431 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
3432 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3434 GtkWidget *menu_item = menu_shell->active_menu_item;
3435 gboolean locked, replace_accels = TRUE;
3438 path = get_accel_path (menu_item, &locked);
3439 if (!path || locked)
3441 /* can't change accelerators on menu_items without paths
3442 * (basically, those items are accelerator-locked).
3444 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
3445 gtk_widget_error_bell (widget);
3451 /* For the keys that act to delete the current setting, we delete
3452 * the current setting if there is one, otherwise, we set the
3453 * key as the accelerator.
3459 if (gtk_accel_map_lookup_entry (path, &key) &&
3460 (key.accel_key || key.accel_mods))
3466 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3470 /* we failed, probably because this key is in use and
3473 /* g_print("failed to change\n"); */
3474 gtk_widget_error_bell (widget);
3483 check_threshold (GtkWidget *widget,
3492 ABS (start_x - x) > THRESHOLD ||
3493 ABS (start_y - y) > THRESHOLD;
3497 definitely_within_item (GtkWidget *widget,
3501 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
3504 w = gdk_window_get_width (window);
3505 h = gdk_window_get_height (window);
3508 check_threshold (widget, 0, 0, x, y) &&
3509 check_threshold (widget, w - 1, 0, x, y) &&
3510 check_threshold (widget, w - 1, h - 1, x, y) &&
3511 check_threshold (widget, 0, h - 1, x, y);
3515 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3517 GtkMenuPrivate *priv;
3519 priv = gtk_menu_get_private (menu);
3521 return priv->navigation_height && priv->navigation_width;
3525 gtk_menu_motion_notify (GtkWidget *widget,
3526 GdkEventMotion *event)
3528 GtkWidget *menu_item;
3530 GtkMenuShell *menu_shell;
3533 gboolean need_enter;
3535 if (GTK_IS_MENU (widget))
3537 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3539 if (priv->ignore_button_release)
3540 priv->ignore_button_release = FALSE;
3542 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3546 /* We received the event for one of two reasons:
3548 * a) We are the active menu, and did gtk_grab_add()
3549 * b) The widget is a child of ours, and the event was propagated
3551 * Since for computation of navigation regions, we want the menu which
3552 * is the parent of the menu item, for a), we need to find that menu,
3553 * which may be different from 'widget'.
3555 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3556 parent = gtk_widget_get_parent (menu_item);
3557 if (!GTK_IS_MENU_ITEM (menu_item) ||
3558 !GTK_IS_MENU (parent))
3561 menu_shell = GTK_MENU_SHELL (parent);
3562 menu = GTK_MENU (menu_shell);
3564 if (definitely_within_item (menu_item, event->x, event->y))
3565 menu_shell->activate_time = 0;
3567 need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->ignore_enter);
3569 /* Check to see if we are within an active submenu's navigation region
3571 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3574 /* Make sure we pop down if we enter a non-selectable menu item, so we
3575 * don't show a submenu when the cursor is outside the stay-up triangle.
3577 if (!_gtk_menu_item_is_selectable (menu_item))
3579 /* We really want to deselect, but this gives the menushell code
3580 * a chance to do some bookkeeping about the menuitem.
3582 gtk_menu_shell_select_item (menu_shell, menu_item);
3588 /* The menu is now sensitive to enter events on its items, but
3589 * was previously sensitive. So we fake an enter event.
3591 menu_shell->ignore_enter = FALSE;
3593 if (event->x >= 0 && event->x < gdk_window_get_width (event->window) &&
3594 event->y >= 0 && event->y < gdk_window_get_height (event->window))
3596 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3599 send_event->crossing.window = g_object_ref (event->window);
3600 send_event->crossing.time = event->time;
3601 send_event->crossing.send_event = TRUE;
3602 send_event->crossing.x_root = event->x_root;
3603 send_event->crossing.y_root = event->y_root;
3604 send_event->crossing.x = event->x;
3605 send_event->crossing.y = event->y;
3606 send_event->crossing.state = event->state;
3607 gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3609 /* We send the event to 'widget', the currently active menu,
3610 * instead of 'menu', the menu that the pointer is in. This
3611 * will ensure that the event will be ignored unless the
3612 * menuitem is a child of the active menu or some parent
3613 * menu of the active menu.
3615 result = gtk_widget_event (widget, send_event);
3616 gdk_event_free (send_event);
3626 get_double_arrows (GtkMenu *menu)
3628 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3629 gboolean double_arrows;
3630 GtkArrowPlacement arrow_placement;
3632 gtk_widget_style_get (GTK_WIDGET (menu),
3633 "double-arrows", &double_arrows,
3634 "arrow-placement", &arrow_placement,
3637 if (arrow_placement != GTK_ARROWS_BOTH)
3640 return double_arrows || (priv->initially_pushed_in &&
3641 menu->scroll_offset != 0);
3645 gtk_menu_scroll_by (GtkMenu *menu,
3648 GtkMenuPrivate *priv;
3649 GtkBorder arrow_border;
3653 gboolean double_arrows;
3655 widget = GTK_WIDGET (menu);
3656 offset = menu->scroll_offset + step;
3657 priv = gtk_menu_get_private (menu);
3659 get_arrows_border (menu, &arrow_border);
3661 double_arrows = get_double_arrows (menu);
3663 /* If we scroll upward and the non-visible top part
3664 * is smaller than the scroll arrow it would be
3665 * pretty stupid to show the arrow and taking more
3666 * screen space than just scrolling to the top.
3669 if ((step < 0) && (offset < arrow_border.top))
3672 /* Don't scroll over the top if we weren't before: */
3673 if ((menu->scroll_offset >= 0) && (offset < 0))
3676 view_height = gdk_window_get_height (gtk_widget_get_window (widget));
3678 if (menu->scroll_offset == 0 &&
3679 view_height >= priv->requested_height)
3682 /* Don't scroll past the bottom if we weren't before: */
3683 if (menu->scroll_offset > 0)
3684 view_height -= arrow_border.top;
3686 /* When both arrows are always shown, reduce
3687 * view height even more.
3690 view_height -= arrow_border.bottom;
3692 if ((menu->scroll_offset + view_height <= priv->requested_height) &&
3693 (offset + view_height > priv->requested_height))
3694 offset = priv->requested_height - view_height;
3696 if (offset != menu->scroll_offset)
3697 gtk_menu_scroll_to (menu, offset);
3701 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3702 gboolean touchscreen_mode)
3704 gboolean upper_visible;
3705 gboolean lower_visible;
3707 upper_visible = menu->upper_arrow_visible;
3708 lower_visible = menu->lower_arrow_visible;
3710 gtk_menu_scroll_by (menu, menu->scroll_step);
3712 if (touchscreen_mode &&
3713 (upper_visible != menu->upper_arrow_visible ||
3714 lower_visible != menu->lower_arrow_visible))
3716 /* We are about to hide a scroll arrow while the mouse is pressed,
3717 * this would cause the uncovered menu item to be activated on button
3718 * release. Therefore we need to ignore button release here
3720 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3721 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3726 gtk_menu_scroll_timeout (gpointer data)
3729 gboolean touchscreen_mode;
3731 menu = GTK_MENU (data);
3733 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3734 "gtk-touchscreen-mode", &touchscreen_mode,
3737 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3743 gtk_menu_scroll_timeout_initial (gpointer data)
3747 gboolean touchscreen_mode;
3749 menu = GTK_MENU (data);
3751 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3752 "gtk-timeout-repeat", &timeout,
3753 "gtk-touchscreen-mode", &touchscreen_mode,
3756 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3758 gtk_menu_remove_scroll_timeout (menu);
3760 menu->timeout_id = gdk_threads_add_timeout (timeout,
3761 gtk_menu_scroll_timeout,
3768 gtk_menu_start_scrolling (GtkMenu *menu)
3771 gboolean touchscreen_mode;
3773 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3774 "gtk-timeout-repeat", &timeout,
3775 "gtk-touchscreen-mode", &touchscreen_mode,
3778 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3780 menu->timeout_id = gdk_threads_add_timeout (timeout,
3781 gtk_menu_scroll_timeout_initial,
3786 gtk_menu_scroll (GtkWidget *widget,
3787 GdkEventScroll *event)
3789 GtkMenu *menu = GTK_MENU (widget);
3791 switch (event->direction)
3793 case GDK_SCROLL_RIGHT:
3794 case GDK_SCROLL_DOWN:
3795 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3797 case GDK_SCROLL_LEFT:
3799 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3807 get_arrows_sensitive_area (GtkMenu *menu,
3808 GdkRectangle *upper,
3809 GdkRectangle *lower)
3811 GtkArrowPlacement arrow_placement;
3812 GtkWidget *widget = GTK_WIDGET (menu);
3816 guint vertical_padding;
3818 gint scroll_arrow_height;
3820 window = gtk_widget_get_window (widget);
3821 width = gdk_window_get_width (window);
3822 height = gdk_window_get_height (window);
3824 gtk_widget_style_get (widget,
3825 "vertical-padding", &vertical_padding,
3826 "scroll-arrow-vlength", &scroll_arrow_height,
3827 "arrow-placement", &arrow_placement,
3830 border = gtk_container_get_border_width (GTK_CONTAINER (menu)) +
3831 gtk_widget_get_style (widget)->ythickness + vertical_padding;
3833 gdk_window_get_position (window, &win_x, &win_y);
3835 switch (arrow_placement)
3837 case GTK_ARROWS_BOTH:
3842 upper->width = width;
3843 upper->height = scroll_arrow_height + border;
3849 lower->y = win_y + height - border - scroll_arrow_height;
3850 lower->width = width;
3851 lower->height = scroll_arrow_height + border;
3855 case GTK_ARROWS_START:
3860 upper->width = width / 2;
3861 upper->height = scroll_arrow_height + border;
3866 lower->x = win_x + width / 2;
3868 lower->width = width / 2;
3869 lower->height = scroll_arrow_height + border;
3873 case GTK_ARROWS_END:
3877 upper->y = win_y + height - border - scroll_arrow_height;
3878 upper->width = width / 2;
3879 upper->height = scroll_arrow_height + border;
3884 lower->x = win_x + width / 2;
3885 lower->y = win_y + height - border - scroll_arrow_height;
3886 lower->width = width / 2;
3887 lower->height = scroll_arrow_height + border;
3895 gtk_menu_handle_scrolling (GtkMenu *menu,
3901 GtkMenuShell *menu_shell;
3902 GtkMenuPrivate *priv;
3905 gboolean scroll_fast = FALSE;
3907 gboolean touchscreen_mode;
3909 priv = gtk_menu_get_private (menu);
3911 menu_shell = GTK_MENU_SHELL (menu);
3913 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3914 "gtk-touchscreen-mode", &touchscreen_mode,
3917 gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3922 /* upper arrow handling */
3924 get_arrows_sensitive_area (menu, &rect, NULL);
3927 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3928 (x >= rect.x) && (x < rect.x + rect.width) &&
3929 (y >= rect.y) && (y < rect.y + rect.height))
3934 if (touchscreen_mode)
3935 menu->upper_arrow_prelight = in_arrow;
3937 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3939 gboolean arrow_pressed = FALSE;
3941 if (menu->upper_arrow_visible && !menu->tearoff_active)
3943 if (touchscreen_mode)
3945 if (enter && menu->upper_arrow_prelight)
3947 if (menu->timeout_id == 0)
3949 /* Deselect the active item so that
3950 * any submenus are popped down
3952 gtk_menu_shell_deselect (menu_shell);
3954 gtk_menu_remove_scroll_timeout (menu);
3955 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3959 /* Only do stuff on click. */
3960 gtk_menu_start_scrolling (menu);
3961 arrow_pressed = TRUE;
3966 arrow_pressed = TRUE;
3971 gtk_menu_stop_scrolling (menu);
3974 else /* !touchscreen_mode */
3976 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3978 if (enter && in_arrow &&
3979 (!menu->upper_arrow_prelight ||
3980 menu->scroll_fast != scroll_fast))
3982 menu->upper_arrow_prelight = TRUE;
3983 menu->scroll_fast = scroll_fast;
3985 /* Deselect the active item so that
3986 * any submenus are popped down
3988 gtk_menu_shell_deselect (menu_shell);
3990 gtk_menu_remove_scroll_timeout (menu);
3991 menu->scroll_step = scroll_fast ?
3992 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
3995 gdk_threads_add_timeout (scroll_fast ?
3996 MENU_SCROLL_TIMEOUT2 :
3997 MENU_SCROLL_TIMEOUT1,
3998 gtk_menu_scroll_timeout, menu);
4000 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
4002 gtk_menu_stop_scrolling (menu);
4007 /* gtk_menu_start_scrolling() might have hit the top of the
4008 * menu, so check if the button isn't insensitive before
4009 * changing it to something else.
4011 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
4013 GtkStateType arrow_state = GTK_STATE_NORMAL;
4016 arrow_state = GTK_STATE_ACTIVE;
4017 else if (menu->upper_arrow_prelight)
4018 arrow_state = GTK_STATE_PRELIGHT;
4020 if (arrow_state != priv->upper_arrow_state)
4022 priv->upper_arrow_state = arrow_state;
4024 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4030 /* lower arrow handling */
4032 get_arrows_sensitive_area (menu, NULL, &rect);
4035 if (menu->lower_arrow_visible && !menu->tearoff_active &&
4036 (x >= rect.x) && (x < rect.x + rect.width) &&
4037 (y >= rect.y) && (y < rect.y + rect.height))
4042 if (touchscreen_mode)
4043 menu->lower_arrow_prelight = in_arrow;
4045 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4047 gboolean arrow_pressed = FALSE;
4049 if (menu->lower_arrow_visible && !menu->tearoff_active)
4051 if (touchscreen_mode)
4053 if (enter && menu->lower_arrow_prelight)
4055 if (menu->timeout_id == 0)
4057 /* Deselect the active item so that
4058 * any submenus are popped down
4060 gtk_menu_shell_deselect (menu_shell);
4062 gtk_menu_remove_scroll_timeout (menu);
4063 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4067 /* Only do stuff on click. */
4068 gtk_menu_start_scrolling (menu);
4069 arrow_pressed = TRUE;
4074 arrow_pressed = TRUE;
4079 gtk_menu_stop_scrolling (menu);
4082 else /* !touchscreen_mode */
4084 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4086 if (enter && in_arrow &&
4087 (!menu->lower_arrow_prelight ||
4088 menu->scroll_fast != scroll_fast))
4090 menu->lower_arrow_prelight = TRUE;
4091 menu->scroll_fast = scroll_fast;
4093 /* Deselect the active item so that
4094 * any submenus are popped down
4096 gtk_menu_shell_deselect (menu_shell);
4098 gtk_menu_remove_scroll_timeout (menu);
4099 menu->scroll_step = scroll_fast ?
4100 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
4103 gdk_threads_add_timeout (scroll_fast ?
4104 MENU_SCROLL_TIMEOUT2 :
4105 MENU_SCROLL_TIMEOUT1,
4106 gtk_menu_scroll_timeout, menu);
4108 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
4110 gtk_menu_stop_scrolling (menu);
4115 /* gtk_menu_start_scrolling() might have hit the bottom of the
4116 * menu, so check if the button isn't insensitive before
4117 * changing it to something else.
4119 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4121 GtkStateType arrow_state = GTK_STATE_NORMAL;
4124 arrow_state = GTK_STATE_ACTIVE;
4125 else if (menu->lower_arrow_prelight)
4126 arrow_state = GTK_STATE_PRELIGHT;
4128 if (arrow_state != priv->lower_arrow_state)
4130 priv->lower_arrow_state = arrow_state;
4132 gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4140 gtk_menu_enter_notify (GtkWidget *widget,
4141 GdkEventCrossing *event)
4143 GtkWidget *menu_item;
4145 gboolean touchscreen_mode;
4147 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4148 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4149 event->mode == GDK_CROSSING_STATE_CHANGED)
4152 g_object_get (gtk_widget_get_settings (widget),
4153 "gtk-touchscreen-mode", &touchscreen_mode,
4156 menu_item = gtk_get_event_widget ((GdkEvent*) event);
4157 if (GTK_IS_MENU (widget))
4159 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4161 if (!menu_shell->ignore_enter)
4162 gtk_menu_handle_scrolling (GTK_MENU (widget),
4163 event->x_root, event->y_root, TRUE, TRUE);
4166 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4168 GtkWidget *menu = gtk_widget_get_parent (menu_item);
4170 if (GTK_IS_MENU (menu))
4172 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
4173 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4175 if (priv->seen_item_enter)
4177 /* This is the second enter we see for an item
4178 * on this menu. This means a release should always
4181 menu_shell->activate_time = 0;
4183 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4184 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4186 if (definitely_within_item (menu_item, event->x, event->y))
4188 /* This is an actual user-enter (ie. not a pop-under)
4189 * In this case, the user must either have entered
4190 * sufficiently far enough into the item, or he must move
4191 * far enough away from the enter point. (see
4192 * gtk_menu_motion_notify())
4194 menu_shell->activate_time = 0;
4198 priv->seen_item_enter = TRUE;
4202 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4203 * will not correspond to the event widget's parent. Check to see
4204 * if we are in the parent's navigation region.
4206 parent = gtk_widget_get_parent (menu_item);
4207 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4208 gtk_menu_navigating_submenu (GTK_MENU (parent),
4209 event->x_root, event->y_root))
4212 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
4216 gtk_menu_leave_notify (GtkWidget *widget,
4217 GdkEventCrossing *event)
4219 GtkMenuShell *menu_shell;
4221 GtkMenuItem *menu_item;
4222 GtkWidget *event_widget;
4224 if (event->mode == GDK_CROSSING_GTK_GRAB ||
4225 event->mode == GDK_CROSSING_GTK_UNGRAB ||
4226 event->mode == GDK_CROSSING_STATE_CHANGED)
4229 menu = GTK_MENU (widget);
4230 menu_shell = GTK_MENU_SHELL (widget);
4232 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4235 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4237 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4239 if (!GTK_IS_MENU_ITEM (event_widget))
4242 menu_item = GTK_MENU_ITEM (event_widget);
4244 /* Here we check to see if we're leaving an active menu item with a submenu,
4245 * in which case we enter submenu navigation mode.
4247 if (menu_shell->active_menu_item != NULL
4248 && menu_item->submenu != NULL
4249 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
4251 if (GTK_MENU_SHELL (menu_item->submenu)->active)
4253 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4256 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
4258 /* We are leaving an active menu item with nonactive submenu.
4259 * Deselect it so we don't surprise the user with by popping
4260 * up a submenu _after_ he left the item.
4262 gtk_menu_shell_deselect (menu_shell);
4267 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
4271 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4273 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4275 priv->navigation_x = 0;
4276 priv->navigation_y = 0;
4277 priv->navigation_width = 0;
4278 priv->navigation_height = 0;
4280 if (menu->navigation_timeout)
4282 g_source_remove (menu->navigation_timeout);
4283 menu->navigation_timeout = 0;
4287 /* When the timeout is elapsed, the navigation region is destroyed
4288 * and the menuitem under the pointer (if any) is selected.
4291 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4293 GtkMenuPopdownData *popdown_data = user_data;
4294 GtkMenu *menu = popdown_data->menu;
4295 GdkWindow *child_window;
4297 gtk_menu_stop_navigating_submenu (menu);
4299 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4301 child_window = gdk_window_get_device_position (menu->bin_window,
4302 popdown_data->device,
4307 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4309 send_event->crossing.window = g_object_ref (child_window);
4310 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4311 send_event->crossing.send_event = TRUE;
4312 gdk_event_set_device (send_event, popdown_data->device);
4314 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4316 gdk_event_free (send_event);
4324 gtk_menu_navigating_submenu (GtkMenu *menu,
4328 GtkMenuPrivate *priv;
4331 if (!gtk_menu_has_navigation_triangle (menu))
4334 priv = gtk_menu_get_private (menu);
4335 width = priv->navigation_width;
4336 height = priv->navigation_height;
4338 /* check if x/y are in the triangle spanned by the navigation parameters */
4340 /* 1) Move the coordinates so the triangle starts at 0,0 */
4341 event_x -= priv->navigation_x;
4342 event_y -= priv->navigation_y;
4344 /* 2) Ensure both legs move along the positive axis */
4356 /* 3) Check that the given coordinate is inside the triangle. The formula
4357 * is a transformed form of this formula: x/w + y/h <= 1
4359 if (event_x >= 0 && event_y >= 0 &&
4360 event_x * height + event_y * width <= width * height)
4366 gtk_menu_stop_navigating_submenu (menu);
4372 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
4373 GtkMenuItem *menu_item,
4374 GdkEventCrossing *event)
4376 gint submenu_left = 0;
4377 gint submenu_right = 0;
4378 gint submenu_top = 0;
4379 gint submenu_bottom = 0;
4382 GtkWidget *event_widget;
4383 GtkMenuPopdownData *popdown_data;
4384 GtkMenuPrivate *priv;
4387 g_return_if_fail (menu_item->submenu != NULL);
4388 g_return_if_fail (event != NULL);
4390 priv = gtk_menu_get_private (menu);
4392 event_widget = gtk_get_event_widget ((GdkEvent*) event);
4394 window = gtk_widget_get_window (menu_item->submenu);
4395 gdk_window_get_origin (window, &submenu_left, &submenu_top);
4397 submenu_right = submenu_left + gdk_window_get_width (window);
4398 submenu_bottom = submenu_top + gdk_window_get_height (window);
4400 width = gdk_window_get_width (gtk_widget_get_window (event_widget));
4401 height = gdk_window_get_height (gtk_widget_get_window (event_widget));
4403 if (event->x >= 0 && event->x < width)
4407 gtk_menu_stop_navigating_submenu (menu);
4409 /* The navigation region is the triangle closest to the x/y
4410 * location of the rectangle. This is why the width or height
4414 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
4417 priv->navigation_x = submenu_left;
4418 priv->navigation_width = event->x_root - submenu_left;
4423 priv->navigation_x = submenu_right;
4424 priv->navigation_width = event->x_root - submenu_right;
4430 priv->navigation_y = event->y_root;
4431 priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4433 if (priv->navigation_height >= 0)
4439 priv->navigation_y = event->y_root;
4440 priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4442 if (priv->navigation_height <= 0)
4446 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4447 "gtk-menu-popdown-delay", &popdown_delay,
4450 popdown_data = g_new (GtkMenuPopdownData, 1);
4451 popdown_data->menu = menu;
4452 popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4454 menu->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4456 gtk_menu_stop_navigating_submenu_cb,
4458 (GDestroyNotify) g_free);
4463 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4467 g_return_if_fail (GTK_IS_MENU (menu_shell));
4469 parent = menu_shell->parent_menu_shell;
4471 menu_shell->activate_time = 0;
4472 gtk_menu_popdown (GTK_MENU (menu_shell));
4475 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4479 gtk_menu_position (GtkMenu *menu,
4480 gboolean set_scroll_offset)
4483 GtkRequisition requisition;
4484 GtkMenuPrivate *private;
4489 GdkScreen *pointer_screen;
4490 GdkRectangle monitor;
4493 g_return_if_fail (GTK_IS_MENU (menu));
4495 widget = GTK_WIDGET (menu);
4497 screen = gtk_widget_get_screen (widget);
4498 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4499 gdk_display_get_device_state (gdk_screen_get_display (screen),
4500 pointer, &pointer_screen, &x, &y, NULL);
4502 /* Get the minimum height for minimum width to figure out
4503 * the right place to popup the menu.
4505 gtk_widget_get_preferred_size (widget, &requisition, NULL);
4507 if (pointer_screen != screen)
4509 /* Pointer is on a different screen; roughly center the
4510 * menu on the screen. If someone was using multiscreen
4511 * + Xinerama together they'd probably want something
4512 * fancier; but that is likely to be vanishingly rare.
4514 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4515 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4518 private = gtk_menu_get_private (menu);
4519 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4521 private->initially_pushed_in = FALSE;
4523 /* Set the type hint here to allow custom position functions to set a different hint */
4524 if (!gtk_widget_get_visible (menu->toplevel))
4525 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4527 if (menu->position_func)
4529 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
4530 menu->position_func_data);
4532 if (private->monitor_num < 0)
4533 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4535 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4539 GtkStyle *style = gtk_widget_get_style (widget);
4540 gint space_left, space_right, space_above, space_below;
4543 gint xthickness = style->xthickness;
4544 gint ythickness = style->ythickness;
4545 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4547 /* The placement of popup menus horizontally works like this (with
4548 * RTL in parentheses)
4550 * - If there is enough room to the right (left) of the mouse cursor,
4551 * position the menu there.
4553 * - Otherwise, if if there is enough room to the left (right) of the
4554 * mouse cursor, position the menu there.
4556 * - Otherwise if the menu is smaller than the monitor, position it
4557 * on the side of the mouse cursor that has the most space available
4559 * - Otherwise (if there is simply not enough room for the menu on the
4560 * monitor), position it as far left (right) as possible.
4562 * Positioning in the vertical direction is similar: first try below
4563 * mouse cursor, then above.
4565 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4567 space_left = x - monitor.x;
4568 space_right = monitor.x + monitor.width - x - 1;
4569 space_above = y - monitor.y;
4570 space_below = monitor.y + monitor.height - y - 1;
4572 /* position horizontally */
4574 /* the amount of space we need to position the menu. Note the
4575 * menu is offset "xthickness" pixels
4577 needed_width = requisition.width - xthickness;
4579 if (needed_width <= space_left ||
4580 needed_width <= space_right)
4582 if ((rtl && needed_width <= space_left) ||
4583 (!rtl && needed_width > space_right))
4586 x = x + xthickness - requisition.width + 1;
4590 /* position right */
4594 /* x is clamped on-screen further down */
4596 else if (requisition.width <= monitor.width)
4598 /* the menu is too big to fit on either side of the mouse
4599 * cursor, but smaller than the monitor. Position it on
4600 * the side that has the most space
4602 if (space_left > space_right)
4610 x = monitor.x + monitor.width - requisition.width;
4613 else /* menu is simply too big for the monitor */
4618 x = monitor.x + monitor.width - requisition.width;
4627 /* Position vertically. The algorithm is the same as above, but
4628 * simpler because we don't have to take RTL into account.
4630 needed_height = requisition.height - ythickness;
4632 if (needed_height <= space_above ||
4633 needed_height <= space_below)
4635 if (needed_height <= space_below)
4638 y = y + ythickness - requisition.height + 1;
4640 y = CLAMP (y, monitor.y,
4641 monitor.y + monitor.height - requisition.height);
4643 else if (needed_height > space_below && needed_height > space_above)
4645 if (space_below >= space_above)
4646 y = monitor.y + monitor.height - requisition.height;
4658 if (private->initially_pushed_in)
4660 menu_height = requisition.height;
4662 if (y + menu_height > monitor.y + monitor.height)
4664 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4665 y = (monitor.y + monitor.height) - menu_height;
4670 scroll_offset += monitor.y - y;
4675 /* FIXME: should this be done in the various position_funcs ? */
4676 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4678 if (GTK_MENU_SHELL (menu)->active)
4680 private->have_position = TRUE;
4685 if (y + requisition.height > monitor.y + monitor.height)
4686 requisition.height = (monitor.y + monitor.height) - y;
4690 scroll_offset += monitor.y - y;
4691 requisition.height -= monitor.y - y;
4695 if (scroll_offset > 0)
4697 GtkBorder arrow_border;
4699 get_arrows_border (menu, &arrow_border);
4700 scroll_offset += arrow_border.top;
4703 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
4706 if (!GTK_MENU_SHELL (menu)->active)
4708 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4709 requisition.width, requisition.height);
4712 if (set_scroll_offset)
4713 menu->scroll_offset = scroll_offset;
4717 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4719 if (menu->timeout_id)
4721 g_source_remove (menu->timeout_id);
4722 menu->timeout_id = 0;
4727 gtk_menu_stop_scrolling (GtkMenu *menu)
4729 gboolean touchscreen_mode;
4731 gtk_menu_remove_scroll_timeout (menu);
4733 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4734 "gtk-touchscreen-mode", &touchscreen_mode,
4737 if (!touchscreen_mode)
4739 menu->upper_arrow_prelight = FALSE;
4740 menu->lower_arrow_prelight = FALSE;
4745 gtk_menu_scroll_to (GtkMenu *menu,
4748 GtkMenuPrivate *priv;
4749 GtkAllocation allocation;
4750 GtkBorder arrow_border;
4754 gint view_width, view_height;
4757 guint vertical_padding;
4758 guint horizontal_padding;
4759 gboolean double_arrows;
4761 widget = GTK_WIDGET (menu);
4762 priv = gtk_menu_get_private (menu);
4764 if (menu->tearoff_active &&
4765 menu->tearoff_adjustment &&
4766 (menu->tearoff_adjustment->value != offset))
4768 menu->tearoff_adjustment->value =
4770 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4771 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4774 /* Move/resize the viewport according to arrows: */
4775 gtk_widget_get_allocation (widget, &allocation);
4776 view_width = allocation.width;
4777 view_height = allocation.height;
4779 style = gtk_widget_get_style (widget);
4781 gtk_widget_style_get (GTK_WIDGET (menu),
4782 "vertical-padding", &vertical_padding,
4783 "horizontal-padding", &horizontal_padding,
4786 double_arrows = get_double_arrows (menu);
4788 border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4790 view_width -= (border_width + style->xthickness + horizontal_padding) * 2;
4791 view_height -= (border_width + style->ythickness + vertical_padding) * 2;
4792 menu_height = priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2;
4794 x = border_width + style->xthickness + horizontal_padding;
4795 y = border_width + style->ythickness + vertical_padding;
4797 if (double_arrows && !menu->tearoff_active)
4799 if (view_height < menu_height ||
4800 (offset > 0 && menu->scroll_offset > 0) ||
4801 (offset < 0 && menu->scroll_offset < 0))
4803 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4804 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4805 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4807 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4808 gtk_widget_queue_draw (GTK_WIDGET (menu));
4810 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4812 get_arrows_border (menu, &arrow_border);
4813 y += arrow_border.top;
4814 view_height -= arrow_border.top;
4815 view_height -= arrow_border.bottom;
4818 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4819 else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4820 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4821 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4823 if (offset >= menu_height - view_height)
4824 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4825 else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4826 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4827 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4829 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4830 (priv->lower_arrow_state != lower_arrow_previous_state))
4831 gtk_widget_queue_draw (GTK_WIDGET (menu));
4833 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4834 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4836 /* At the upper border, possibly remove timeout */
4837 if (menu->scroll_step < 0)
4839 gtk_menu_stop_scrolling (menu);
4840 gtk_widget_queue_draw (GTK_WIDGET (menu));
4844 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4845 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4847 /* At the lower border, possibly remove timeout */
4848 if (menu->scroll_step > 0)
4850 gtk_menu_stop_scrolling (menu);
4851 gtk_widget_queue_draw (GTK_WIDGET (menu));
4855 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4859 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4860 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4862 gtk_menu_stop_scrolling (menu);
4863 gtk_widget_queue_draw (GTK_WIDGET (menu));
4866 else if (!menu->tearoff_active)
4868 gboolean last_visible;
4870 last_visible = menu->upper_arrow_visible;
4871 menu->upper_arrow_visible = offset > 0;
4873 /* upper_arrow_visible may have changed, so requery the border */
4874 get_arrows_border (menu, &arrow_border);
4875 view_height -= arrow_border.top;
4877 if ((last_visible != menu->upper_arrow_visible) &&
4878 !menu->upper_arrow_visible)
4880 menu->upper_arrow_prelight = FALSE;
4882 /* If we hid the upper arrow, possibly remove timeout */
4883 if (menu->scroll_step < 0)
4885 gtk_menu_stop_scrolling (menu);
4886 gtk_widget_queue_draw (GTK_WIDGET (menu));
4890 last_visible = menu->lower_arrow_visible;
4891 menu->lower_arrow_visible = offset < menu_height - view_height;
4893 /* lower_arrow_visible may have changed, so requery the border */
4894 get_arrows_border (menu, &arrow_border);
4895 view_height -= arrow_border.bottom;
4897 if ((last_visible != menu->lower_arrow_visible) &&
4898 !menu->lower_arrow_visible)
4900 menu->lower_arrow_prelight = FALSE;
4902 /* If we hid the lower arrow, possibly remove timeout */
4903 if (menu->scroll_step > 0)
4905 gtk_menu_stop_scrolling (menu);
4906 gtk_widget_queue_draw (GTK_WIDGET (menu));
4910 y += arrow_border.top;
4913 /* Scroll the menu: */
4914 if (gtk_widget_get_realized (widget))
4915 gdk_window_move (menu->bin_window, 0, -offset);
4917 if (gtk_widget_get_realized (widget))
4918 gdk_window_move_resize (menu->view_window,
4924 menu->scroll_offset = offset;
4928 compute_child_offset (GtkMenu *menu,
4929 GtkWidget *menu_item,
4932 gboolean *is_last_child)
4934 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4935 gint item_top_attach;
4936 gint item_bottom_attach;
4937 gint child_offset = 0;
4940 get_effective_child_attach (menu_item, NULL, NULL,
4941 &item_top_attach, &item_bottom_attach);
4943 /* there is a possibility that we get called before _size_request, so
4944 * check the height table for safety.
4946 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4949 /* when we have a row with only invisible children, it's height will
4950 * be zero, so there's no need to check WIDGET_VISIBLE here
4952 for (i = 0; i < item_top_attach; i++)
4953 child_offset += priv->heights[i];
4956 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4958 *offset = child_offset;
4960 *height = priv->heights[item_top_attach];
4966 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4967 GtkWidget *menu_item)
4969 GtkMenu *menu = GTK_MENU (menu_shell);
4970 GtkWidget *widget = GTK_WIDGET (menu_shell);
4971 gint child_offset, child_height;
4975 gboolean last_child = 0;
4977 /* We need to check if the selected item fully visible.
4978 * If not we need to scroll the menu so that it becomes fully
4982 if (compute_child_offset (menu, menu_item,
4983 &child_offset, &child_height, &last_child))
4985 guint vertical_padding;
4986 gboolean double_arrows;
4988 y = menu->scroll_offset;
4989 width = gdk_window_get_width (gtk_widget_get_window (widget));
4990 height = gdk_window_get_height (gtk_widget_get_window (widget));
4992 gtk_widget_style_get (widget,
4993 "vertical-padding", &vertical_padding,
4996 double_arrows = get_double_arrows (menu);
4998 height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
4999 2 * gtk_widget_get_style (widget)->ythickness +
5000 2 * vertical_padding;
5001 if (child_offset < y)
5003 /* Ignore the enter event we might get if the pointer is on the menu
5005 menu_shell->ignore_enter = TRUE;
5006 gtk_menu_scroll_to (menu, child_offset);
5010 GtkBorder arrow_border;
5014 get_arrows_border (menu, &arrow_border);
5015 if (!menu->tearoff_active)
5016 arrow_height = arrow_border.top + arrow_border.bottom;
5018 if (child_offset + child_height > y + height - arrow_height)
5021 if ((!last_child && !menu->tearoff_active) || double_arrows)
5022 arrow_height += arrow_border.bottom;
5024 y = child_offset + child_height - height + arrow_height;
5025 if (((y > 0) && !menu->tearoff_active) || double_arrows)
5027 /* Need upper arrow */
5028 arrow_height += arrow_border.top;
5029 y = child_offset + child_height - height + arrow_height;
5031 /* Ignore the enter event we might get if the pointer is on the menu
5033 menu_shell->ignore_enter = TRUE;
5034 gtk_menu_scroll_to (menu, y);
5042 gtk_menu_select_item (GtkMenuShell *menu_shell,
5043 GtkWidget *menu_item)
5045 GtkMenu *menu = GTK_MENU (menu_shell);
5047 if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5048 gtk_menu_scroll_item_visible (menu_shell, menu_item);
5050 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5054 /* Reparent the menu, taking care of the refcounting
5056 * If unrealize is true we force a unrealize while reparenting the parent.
5057 * This can help eliminate flicker in some cases.
5059 * What happens is that when the menu is unrealized and then re-realized,
5060 * the allocations are as follows:
5062 * parent - 1x1 at (0,0)
5063 * child1 - 100x20 at (0,0)
5064 * child2 - 100x20 at (0,20)
5065 * child3 - 100x20 at (0,40)
5067 * That is, the parent is small but the children are full sized. Then,
5068 * when the queued_resize gets processed, the parent gets resized to
5071 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5072 * contains the following logic:
5074 * - if a move or resize operation on a window would change the clip
5075 * region on the children, then before the window is resized
5076 * the background for children is temporarily set to None, the
5077 * move/resize done, and the background for the children restored.
5079 * So, at the point where the parent is resized to final size, the
5080 * background for the children is temporarily None, and thus they
5081 * are not cleared to the background color and the previous background
5082 * (the image of the menu) is left in place.
5085 gtk_menu_reparent (GtkMenu *menu,
5086 GtkWidget *new_parent,
5089 GObject *object = G_OBJECT (menu);
5090 GtkWidget *widget = GTK_WIDGET (menu);
5091 gboolean was_floating = g_object_is_floating (object);
5093 g_object_ref_sink (object);
5097 g_object_ref (object);
5098 gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5099 gtk_container_add (GTK_CONTAINER (new_parent), widget);
5100 g_object_unref (object);
5103 gtk_widget_reparent (widget, new_parent);
5106 g_object_force_floating (object);
5108 g_object_unref (object);
5112 gtk_menu_show_all (GtkWidget *widget)
5114 /* Show children, but not self. */
5115 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5119 * gtk_menu_set_screen:
5120 * @menu: a #GtkMenu.
5121 * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5122 * determined by the widget the menu is attached to.
5124 * Sets the #GdkScreen on which the menu will be displayed.
5129 gtk_menu_set_screen (GtkMenu *menu,
5132 g_return_if_fail (GTK_IS_MENU (menu));
5133 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
5135 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5139 menu_change_screen (menu, screen);
5143 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5145 attach_widget_screen_changed (attach_widget, NULL, menu);
5151 * @menu: a #GtkMenu.
5152 * @child: a #GtkMenuItem.
5153 * @left_attach: The column number to attach the left side of the item to.
5154 * @right_attach: The column number to attach the right side of the item to.
5155 * @top_attach: The row number to attach the top of the item to.
5156 * @bottom_attach: The row number to attach the bottom of the item to.
5158 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5159 * an item will occupy is specified by @left_attach, @right_attach,
5160 * @top_attach and @bottom_attach. These each represent the leftmost,
5161 * rightmost, uppermost and lower column and row numbers of the table.
5162 * (Columns and rows are indexed from zero).
5164 * Note that this function is not related to gtk_menu_detach().
5169 gtk_menu_attach (GtkMenu *menu,
5174 guint bottom_attach)
5176 GtkMenuShell *menu_shell;
5179 g_return_if_fail (GTK_IS_MENU (menu));
5180 g_return_if_fail (GTK_IS_MENU_ITEM (child));
5181 parent = gtk_widget_get_parent (child);
5182 g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5183 g_return_if_fail (left_attach < right_attach);
5184 g_return_if_fail (top_attach < bottom_attach);
5186 menu_shell = GTK_MENU_SHELL (menu);
5190 AttachInfo *ai = get_attach_info (child);
5192 ai->left_attach = left_attach;
5193 ai->right_attach = right_attach;
5194 ai->top_attach = top_attach;
5195 ai->bottom_attach = bottom_attach;
5197 menu_shell->children = g_list_append (menu_shell->children, child);
5199 gtk_widget_set_parent (child, GTK_WIDGET (menu));
5201 menu_queue_resize (menu);
5205 gtk_container_child_set (GTK_CONTAINER (parent), child,
5206 "left-attach", left_attach,
5207 "right-attach", right_attach,
5208 "top-attach", top_attach,
5209 "bottom-attach", bottom_attach,
5215 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5219 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5220 "gtk-menu-popup-delay", &popup_delay,
5227 find_child_containing (GtkMenuShell *menu_shell,
5235 /* find a child which includes the area given by
5236 * left, right, top, bottom.
5239 for (list = menu_shell->children; list; list = list->next)
5243 if (!_gtk_menu_item_is_selectable (list->data))
5246 get_effective_child_attach (list->data, &l, &r, &t, &b);
5248 if (l <= left && right <= r
5249 && t <= top && bottom <= b)
5250 return GTK_WIDGET (list->data);
5257 gtk_menu_move_current (GtkMenuShell *menu_shell,
5258 GtkMenuDirectionType direction)
5260 GtkMenu *menu = GTK_MENU (menu_shell);
5263 GtkWidget *match = NULL;
5265 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5269 case GTK_MENU_DIR_CHILD:
5270 direction = GTK_MENU_DIR_PARENT;
5272 case GTK_MENU_DIR_PARENT:
5273 direction = GTK_MENU_DIR_CHILD;
5279 /* use special table menu key bindings */
5280 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5282 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
5284 if (direction == GTK_MENU_DIR_NEXT)
5286 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5288 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5296 for (i = 0; i < t; i++)
5298 match = find_child_containing (menu_shell,
5299 l, l + 1, i, i + 1);
5305 else if (direction == GTK_MENU_DIR_PREV)
5307 for (i = t; i > 0; i--)
5309 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
5317 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5319 match = find_child_containing (menu_shell,
5320 l, l + 1, i - 1, i);
5326 else if (direction == GTK_MENU_DIR_PARENT)
5328 /* we go one left if possible */
5330 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
5334 GtkWidget *parent = menu_shell->parent_menu_shell;
5337 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
5338 match = menu_shell->active_menu_item;
5341 else if (direction == GTK_MENU_DIR_CHILD)
5343 /* we go one right if possible */
5344 if (r < gtk_menu_get_n_columns (menu))
5345 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5349 GtkWidget *parent = menu_shell->parent_menu_shell;
5351 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
5353 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
5354 match = menu_shell->active_menu_item;
5360 gtk_menu_shell_select_item (menu_shell, match);
5365 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5369 get_visible_size (GtkMenu *menu)
5371 GtkAllocation allocation;
5372 GtkWidget *widget = GTK_WIDGET (menu);
5373 GtkContainer *container = GTK_CONTAINER (menu);
5376 gtk_widget_get_allocation (widget, &allocation);
5377 menu_height = (allocation.height
5378 - 2 * (gtk_container_get_border_width (container)
5379 + gtk_widget_get_style (widget)->ythickness));
5381 if (!menu->tearoff_active)
5383 GtkBorder arrow_border;
5385 get_arrows_border (menu, &arrow_border);
5386 menu_height -= arrow_border.top;
5387 menu_height -= arrow_border.bottom;
5393 /* Find the sensitive on-screen child containing @y, or if none,
5394 * the nearest selectable onscreen child. (%NULL if none)
5397 child_at (GtkMenu *menu,
5400 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5401 GtkWidget *child = NULL;
5402 gint child_offset = 0;
5405 gint lower, upper; /* Onscreen bounds */
5407 menu_height = get_visible_size (menu);
5408 lower = menu->scroll_offset;
5409 upper = menu->scroll_offset + menu_height;
5411 for (children = menu_shell->children; children; children = children->next)
5413 if (gtk_widget_get_visible (children->data))
5415 GtkRequisition child_requisition;
5417 gtk_widget_get_preferred_size (children->data,
5418 &child_requisition, NULL);
5420 if (_gtk_menu_item_is_selectable (children->data) &&
5421 child_offset >= lower &&
5422 child_offset + child_requisition.height <= upper)
5424 child = children->data;
5426 if (child_offset + child_requisition.height > y &&
5427 !GTK_IS_TEAROFF_MENU_ITEM (child))
5431 child_offset += child_requisition.height;
5439 get_menu_height (GtkMenu *menu)
5441 GtkAllocation allocation;
5442 GtkWidget *widget = GTK_WIDGET (menu);
5445 gtk_widget_get_allocation (widget, &allocation);
5447 height = allocation.height;
5448 height -= (gtk_container_get_border_width (GTK_CONTAINER (widget)) + gtk_widget_get_style (widget)->ythickness) * 2;
5450 if (!menu->tearoff_active)
5452 GtkBorder arrow_border;
5454 get_arrows_border (menu, &arrow_border);
5455 height -= arrow_border.top;
5456 height -= arrow_border.bottom;
5463 gtk_menu_real_move_scroll (GtkMenu *menu,
5466 gint page_size = get_visible_size (menu);
5467 gint end_position = get_menu_height (menu);
5468 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5472 case GTK_SCROLL_PAGE_UP:
5473 case GTK_SCROLL_PAGE_DOWN:
5477 gint child_offset = 0;
5478 gboolean old_upper_arrow_visible;
5481 if (type == GTK_SCROLL_PAGE_UP)
5486 if (menu_shell->active_menu_item)
5490 compute_child_offset (menu, menu_shell->active_menu_item,
5491 &child_offset, &child_height, NULL);
5492 child_offset += child_height / 2;
5495 menu_shell->ignore_enter = TRUE;
5496 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5497 old_offset = menu->scroll_offset;
5499 new_offset = menu->scroll_offset + step;
5500 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5502 gtk_menu_scroll_to (menu, new_offset);
5504 if (menu_shell->active_menu_item)
5506 GtkWidget *new_child;
5507 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5508 GtkBorder arrow_border;
5510 get_arrows_border (menu, &arrow_border);
5512 if (menu->scroll_offset != old_offset)
5513 step = menu->scroll_offset - old_offset;
5515 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5517 new_child = child_at (menu, child_offset + step);
5519 gtk_menu_shell_select_item (menu_shell, new_child);
5523 case GTK_SCROLL_START:
5524 /* Ignore the enter event we might get if the pointer is on the menu
5526 menu_shell->ignore_enter = TRUE;
5527 gtk_menu_scroll_to (menu, 0);
5528 gtk_menu_shell_select_first (menu_shell, TRUE);
5530 case GTK_SCROLL_END:
5531 /* Ignore the enter event we might get if the pointer is on the menu
5533 menu_shell->ignore_enter = TRUE;
5534 gtk_menu_scroll_to (menu, end_position - page_size);
5535 _gtk_menu_shell_select_last (menu_shell, TRUE);
5544 * gtk_menu_set_monitor:
5546 * @monitor_num: the number of the monitor on which the menu should
5549 * Informs GTK+ on which monitor a menu should be popped up.
5550 * See gdk_screen_get_monitor_geometry().
5552 * This function should be called from a #GtkMenuPositionFunc if the
5553 * menu should not appear on the same monitor as the pointer. This
5554 * information can't be reliably inferred from the coordinates returned
5555 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
5556 * may extend beyond the monitor boundaries or even the screen boundaries.
5561 gtk_menu_set_monitor (GtkMenu *menu,
5564 GtkMenuPrivate *priv;
5565 g_return_if_fail (GTK_IS_MENU (menu));
5567 priv = gtk_menu_get_private (menu);
5569 priv->monitor_num = monitor_num;
5573 * gtk_menu_get_monitor:
5576 * Retrieves the number of the monitor on which to show the menu.
5578 * Returns: the number of the monitor on which the menu should
5579 * be popped up or -1, if no monitor has been set
5584 gtk_menu_get_monitor (GtkMenu *menu)
5586 GtkMenuPrivate *priv;
5587 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5589 priv = gtk_menu_get_private (menu);
5591 return priv->monitor_num;
5595 * gtk_menu_get_for_attach_widget:
5596 * @widget: a #GtkWidget
5598 * Returns a list of the menus which are attached to this widget.
5599 * This list is owned by GTK+ and must not be modified.
5601 * Return value: (element-type GtkWidget) (transfer none): the list of menus attached to his widget.
5606 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5610 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5612 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5617 gtk_menu_grab_notify (GtkWidget *widget,
5618 gboolean was_grabbed)
5620 GtkWidget *toplevel;
5621 GtkWindowGroup *group;
5625 pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5628 !gtk_widget_device_is_shadowed (widget, pointer))
5631 toplevel = gtk_widget_get_toplevel (widget);
5633 if (!GTK_IS_WINDOW (toplevel))
5636 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5637 grab = gtk_window_group_get_current_device_grab (group, pointer);
5639 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
5640 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5644 * gtk_menu_set_reserve_toggle_size:
5646 * @reserve_toggle_size: whether to reserve size for toggles
5648 * Sets whether the menu should reserve space for drawing toggles
5649 * or icons, regardless of their actual presence.
5654 gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
5655 gboolean reserve_toggle_size)
5657 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5658 gboolean no_toggle_size;
5660 no_toggle_size = !reserve_toggle_size;
5662 if (priv->no_toggle_size != no_toggle_size)
5664 priv->no_toggle_size = no_toggle_size;
5666 g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5671 * gtk_menu_get_reserve_toggle_size:
5674 * Returns whether the menu reserves space for toggles and
5675 * icons, regardless of their actual presence.
5677 * Returns: Whether the menu reserves toggle space
5682 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5684 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5686 return !priv->no_toggle_size;