1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 #define GTK_MENU_INTERNALS
29 #include <string.h> /* memset */
30 #include "gdk/gdkkeysyms.h"
31 #include "gtkaccellabel.h"
32 #include "gtkaccelmap.h"
33 #include "gtkbindings.h"
36 #include "gtkmarshalers.h"
38 #include "gtkmenuitem.h"
39 #include "gtktearoffmenuitem.h"
40 #include "gtkwindow.h"
42 #include "gtkvscrollbar.h"
43 #include "gtksettings.h"
44 #include "gtkprivate.h"
49 #define MENU_ITEM_CLASS(w) GTK_MENU_ITEM_GET_CLASS (w)
51 #define DEFAULT_POPUP_DELAY 225
52 #define DEFAULT_POPDOWN_DELAY 1000
54 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
55 * extends below the submenu
58 #define MENU_SCROLL_STEP1 8
59 #define MENU_SCROLL_STEP2 15
60 #define MENU_SCROLL_FAST_ZONE 8
61 #define MENU_SCROLL_TIMEOUT1 50
62 #define MENU_SCROLL_TIMEOUT2 20
64 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
65 #define ATTACHED_MENUS "gtk-attached-menus"
67 typedef struct _GtkMenuAttachData GtkMenuAttachData;
68 typedef struct _GtkMenuPrivate GtkMenuPrivate;
70 struct _GtkMenuAttachData
72 GtkWidget *attach_widget;
73 GtkMenuDetachFunc detacher;
76 struct _GtkMenuPrivate
78 gboolean seen_item_enter;
80 gboolean have_position;
84 /* info used for the table */
90 /* Cached layout information */
98 GtkStateType lower_arrow_state;
99 GtkStateType upper_arrow_state;
101 gboolean ignore_button_release;
102 gboolean initially_pushed_in;
111 gint effective_left_attach;
112 gint effective_right_attach;
113 gint effective_top_attach;
114 gint effective_bottom_attach;
135 CHILD_PROP_LEFT_ATTACH,
136 CHILD_PROP_RIGHT_ATTACH,
137 CHILD_PROP_TOP_ATTACH,
138 CHILD_PROP_BOTTOM_ATTACH
141 static void gtk_menu_set_property (GObject *object,
145 static void gtk_menu_get_property (GObject *object,
149 static void gtk_menu_set_child_property(GtkContainer *container,
154 static void gtk_menu_get_child_property(GtkContainer *container,
159 static void gtk_menu_destroy (GtkObject *object);
160 static void gtk_menu_realize (GtkWidget *widget);
161 static void gtk_menu_unrealize (GtkWidget *widget);
162 static void gtk_menu_size_request (GtkWidget *widget,
163 GtkRequisition *requisition);
164 static void gtk_menu_size_allocate (GtkWidget *widget,
165 GtkAllocation *allocation);
166 static void gtk_menu_paint (GtkWidget *widget,
167 GdkEventExpose *expose);
168 static void gtk_menu_show (GtkWidget *widget);
169 static gboolean gtk_menu_expose (GtkWidget *widget,
170 GdkEventExpose *event);
171 static gboolean gtk_menu_key_press (GtkWidget *widget,
173 static gboolean gtk_menu_scroll (GtkWidget *widget,
174 GdkEventScroll *event);
175 static gboolean gtk_menu_button_press (GtkWidget *widget,
176 GdkEventButton *event);
177 static gboolean gtk_menu_button_release (GtkWidget *widget,
178 GdkEventButton *event);
179 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
180 GdkEventMotion *event);
181 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
182 GdkEventCrossing *event);
183 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
184 GdkEventCrossing *event);
185 static void gtk_menu_scroll_to (GtkMenu *menu,
187 static void gtk_menu_grab_notify (GtkWidget *widget,
188 gboolean was_grabbed);
190 static void gtk_menu_stop_scrolling (GtkMenu *menu);
191 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
192 static gboolean gtk_menu_scroll_timeout (gpointer data);
193 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
194 static void gtk_menu_start_scrolling (GtkMenu *menu);
196 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
197 GtkWidget *menu_item);
198 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
199 GtkWidget *menu_item);
200 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
203 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
205 static void gtk_menu_handle_scrolling (GtkMenu *menu,
210 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
212 static void gtk_menu_style_set (GtkWidget *widget,
213 GtkStyle *previous_style);
214 static gboolean gtk_menu_focus (GtkWidget *widget,
215 GtkDirectionType direction);
216 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
217 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
218 GtkMenuDirectionType direction);
219 static void gtk_menu_real_move_scroll (GtkMenu *menu,
222 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
223 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
224 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
227 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
228 GtkMenuItem *menu_item,
229 GdkEventCrossing *event);
231 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
232 static void gtk_menu_show_all (GtkWidget *widget);
233 static void gtk_menu_hide_all (GtkWidget *widget);
234 static void gtk_menu_position (GtkMenu *menu);
235 static void gtk_menu_reparent (GtkMenu *menu,
236 GtkWidget *new_parent,
238 static void gtk_menu_remove (GtkContainer *menu,
241 static void gtk_menu_update_title (GtkMenu *menu);
243 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
244 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
246 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
248 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
249 gboolean group_changed);
251 static const gchar attach_data_key[] = "gtk-menu-attach-data";
253 static guint menu_signals[LAST_SIGNAL] = { 0 };
255 static GtkMenuPrivate *
256 gtk_menu_get_private (GtkMenu *menu)
258 return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
261 G_DEFINE_TYPE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
264 menu_queue_resize (GtkMenu *menu)
266 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
268 priv->have_layout = FALSE;
269 gtk_widget_queue_resize (GTK_WIDGET (menu));
273 attach_info_free (AttachInfo *info)
275 g_slice_free (AttachInfo, info);
279 get_attach_info (GtkWidget *child)
281 GObject *object = G_OBJECT (child);
282 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
286 ai = g_slice_new0 (AttachInfo);
287 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
288 (GDestroyNotify) attach_info_free);
295 is_grid_attached (AttachInfo *ai)
297 return (ai->left_attach >= 0 &&
298 ai->right_attach >= 0 &&
299 ai->top_attach >= 0 &&
300 ai->bottom_attach >= 0);
304 menu_ensure_layout (GtkMenu *menu)
306 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
308 if (!priv->have_layout)
310 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
314 gint max_right_attach;
315 gint max_bottom_attach;
317 /* Find extents of gridded portion
319 max_right_attach = 1;
320 max_bottom_attach = 0;
322 for (l = menu_shell->children; l; l = l->next)
324 GtkWidget *child = l->data;
325 AttachInfo *ai = get_attach_info (child);
327 if (is_grid_attached (ai))
329 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
330 max_right_attach = MAX (max_right_attach, ai->right_attach);
336 row_occupied = g_malloc0 (max_bottom_attach);
338 for (l = menu_shell->children; l; l = l->next)
340 GtkWidget *child = l->data;
341 AttachInfo *ai = get_attach_info (child);
343 if (is_grid_attached (ai))
347 for (i = ai->top_attach; i < ai->bottom_attach; i++)
348 row_occupied[i] = TRUE;
352 /* Lay non-grid-items out in those rows
355 for (l = menu_shell->children; l; l = l->next)
357 GtkWidget *child = l->data;
358 AttachInfo *ai = get_attach_info (child);
360 if (!is_grid_attached (ai))
362 while (current_row < max_bottom_attach && row_occupied[current_row])
365 ai->effective_left_attach = 0;
366 ai->effective_right_attach = max_right_attach;
367 ai->effective_top_attach = current_row;
368 ai->effective_bottom_attach = current_row + 1;
374 ai->effective_left_attach = ai->left_attach;
375 ai->effective_right_attach = ai->right_attach;
376 ai->effective_top_attach = ai->top_attach;
377 ai->effective_bottom_attach = ai->bottom_attach;
381 g_free (row_occupied);
383 priv->n_rows = MAX (current_row, max_bottom_attach);
384 priv->n_columns = max_right_attach;
385 priv->have_layout = TRUE;
391 gtk_menu_get_n_columns (GtkMenu *menu)
393 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
395 menu_ensure_layout (menu);
397 return priv->n_columns;
401 gtk_menu_get_n_rows (GtkMenu *menu)
403 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
405 menu_ensure_layout (menu);
411 get_effective_child_attach (GtkWidget *child,
417 GtkMenu *menu = GTK_MENU (child->parent);
420 menu_ensure_layout (menu);
422 ai = get_attach_info (child);
425 *l = ai->effective_left_attach;
427 *r = ai->effective_right_attach;
429 *t = ai->effective_top_attach;
431 *b = ai->effective_bottom_attach;
436 gtk_menu_class_init (GtkMenuClass *class)
438 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
439 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
440 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
441 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
442 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
443 GtkBindingSet *binding_set;
445 gobject_class->set_property = gtk_menu_set_property;
446 gobject_class->get_property = gtk_menu_get_property;
448 object_class->destroy = gtk_menu_destroy;
450 widget_class->realize = gtk_menu_realize;
451 widget_class->unrealize = gtk_menu_unrealize;
452 widget_class->size_request = gtk_menu_size_request;
453 widget_class->size_allocate = gtk_menu_size_allocate;
454 widget_class->show = gtk_menu_show;
455 widget_class->expose_event = gtk_menu_expose;
456 widget_class->scroll_event = gtk_menu_scroll;
457 widget_class->key_press_event = gtk_menu_key_press;
458 widget_class->button_press_event = gtk_menu_button_press;
459 widget_class->button_release_event = gtk_menu_button_release;
460 widget_class->motion_notify_event = gtk_menu_motion_notify;
461 widget_class->show_all = gtk_menu_show_all;
462 widget_class->hide_all = gtk_menu_hide_all;
463 widget_class->enter_notify_event = gtk_menu_enter_notify;
464 widget_class->leave_notify_event = gtk_menu_leave_notify;
465 widget_class->style_set = gtk_menu_style_set;
466 widget_class->focus = gtk_menu_focus;
467 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
468 widget_class->grab_notify = gtk_menu_grab_notify;
470 container_class->remove = gtk_menu_remove;
471 container_class->get_child_property = gtk_menu_get_child_property;
472 container_class->set_child_property = gtk_menu_set_child_property;
474 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
475 menu_shell_class->deactivate = gtk_menu_deactivate;
476 menu_shell_class->select_item = gtk_menu_select_item;
477 menu_shell_class->insert = gtk_menu_real_insert;
478 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
479 menu_shell_class->move_current = gtk_menu_move_current;
481 menu_signals[MOVE_SCROLL] =
482 _gtk_binding_signal_new (I_("move_scroll"),
483 G_OBJECT_CLASS_TYPE (object_class),
484 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
485 G_CALLBACK (gtk_menu_real_move_scroll),
487 _gtk_marshal_VOID__ENUM,
489 GTK_TYPE_SCROLL_TYPE);
494 * The index of the currently selected menu item, or -1 if no
495 * menu item is selected.
499 g_object_class_install_property (gobject_class,
501 g_param_spec_int ("active",
503 P_("The currently selected menu item"),
505 GTK_PARAM_READWRITE));
508 * GtkMenu:accel-group:
510 * The accel group holding accelerators for the menu.
514 g_object_class_install_property (gobject_class,
516 g_param_spec_object ("accel-group",
518 P_("The accel group holding accelerators for the menu"),
519 GTK_TYPE_ACCEL_GROUP,
520 GTK_PARAM_READWRITE));
523 * GtkMenu:accel-path:
525 * An accel path used to conveniently construct accel paths of child items.
529 g_object_class_install_property (gobject_class,
531 g_param_spec_string ("accel-path",
533 P_("An accel path used to conveniently construct accel paths of child items"),
535 GTK_PARAM_READWRITE));
538 * GtkMenu:attach-widget:
540 * The widget the menu is attached to. Setting this property attaches
541 * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
542 * use gtk_menu_attach_to_widget() directly.
546 g_object_class_install_property (gobject_class,
548 g_param_spec_object ("attach-widget",
550 P_("The widget the menu is attached to"),
552 GTK_PARAM_READWRITE));
554 g_object_class_install_property (gobject_class,
556 g_param_spec_string ("tearoff-title",
558 P_("A title that may be displayed by the window manager when this menu is torn-off"),
560 GTK_PARAM_READWRITE));
563 * GtkMenu:tearoff-state:
565 * A boolean that indicates whether the menu is torn-off.
569 g_object_class_install_property (gobject_class,
571 g_param_spec_boolean ("tearoff-state",
573 P_("A boolean that indicates whether the menu is torn-off"),
575 GTK_PARAM_READWRITE));
580 * The monitor the menu will be popped up on.
584 g_object_class_install_property (gobject_class,
586 g_param_spec_int ("monitor",
588 P_("The monitor the menu will be popped up on"),
590 GTK_PARAM_READWRITE));
592 gtk_widget_class_install_style_property (widget_class,
593 g_param_spec_int ("vertical-padding",
594 P_("Vertical Padding"),
595 P_("Extra space at the top and bottom of the menu"),
599 GTK_PARAM_READABLE));
601 gtk_widget_class_install_style_property (widget_class,
602 g_param_spec_int ("horizontal-padding",
603 P_("Horizontal Padding"),
604 P_("Extra space at the left and right edges of the menu"),
608 GTK_PARAM_READABLE));
610 gtk_widget_class_install_style_property (widget_class,
611 g_param_spec_int ("vertical-offset",
612 P_("Vertical Offset"),
613 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
617 GTK_PARAM_READABLE));
619 gtk_widget_class_install_style_property (widget_class,
620 g_param_spec_int ("horizontal-offset",
621 P_("Horizontal Offset"),
622 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
626 GTK_PARAM_READABLE));
628 gtk_widget_class_install_style_property (widget_class,
629 g_param_spec_boolean ("double-arrows",
631 P_("When scrolling, always show both arrows."),
633 GTK_PARAM_READABLE));
636 gtk_container_class_install_child_property (container_class,
637 CHILD_PROP_LEFT_ATTACH,
638 g_param_spec_int ("left-attach",
640 P_("The column number to attach the left side of the child to"),
642 GTK_PARAM_READWRITE));
644 gtk_container_class_install_child_property (container_class,
645 CHILD_PROP_RIGHT_ATTACH,
646 g_param_spec_int ("right-attach",
648 P_("The column number to attach the right side of the child to"),
650 GTK_PARAM_READWRITE));
652 gtk_container_class_install_child_property (container_class,
653 CHILD_PROP_TOP_ATTACH,
654 g_param_spec_int ("top-attach",
656 P_("The row number to attach the top of the child to"),
658 GTK_PARAM_READWRITE));
660 gtk_container_class_install_child_property (container_class,
661 CHILD_PROP_BOTTOM_ATTACH,
662 g_param_spec_int ("bottom-attach",
664 P_("The row number to attach the bottom of the child to"),
666 GTK_PARAM_READWRITE));
668 binding_set = gtk_binding_set_by_class (class);
669 gtk_binding_entry_add_signal (binding_set,
671 I_("move_current"), 1,
672 GTK_TYPE_MENU_DIRECTION_TYPE,
674 gtk_binding_entry_add_signal (binding_set,
677 GTK_TYPE_MENU_DIRECTION_TYPE,
679 gtk_binding_entry_add_signal (binding_set,
682 GTK_TYPE_MENU_DIRECTION_TYPE,
684 gtk_binding_entry_add_signal (binding_set,
687 GTK_TYPE_MENU_DIRECTION_TYPE,
689 gtk_binding_entry_add_signal (binding_set,
692 GTK_TYPE_MENU_DIRECTION_TYPE,
693 GTK_MENU_DIR_PARENT);
694 gtk_binding_entry_add_signal (binding_set,
697 GTK_TYPE_MENU_DIRECTION_TYPE,
698 GTK_MENU_DIR_PARENT);
699 gtk_binding_entry_add_signal (binding_set,
702 GTK_TYPE_MENU_DIRECTION_TYPE,
704 gtk_binding_entry_add_signal (binding_set,
707 GTK_TYPE_MENU_DIRECTION_TYPE,
709 gtk_binding_entry_add_signal (binding_set,
712 GTK_TYPE_SCROLL_TYPE,
714 gtk_binding_entry_add_signal (binding_set,
717 GTK_TYPE_SCROLL_TYPE,
719 gtk_binding_entry_add_signal (binding_set,
722 GTK_TYPE_SCROLL_TYPE,
724 gtk_binding_entry_add_signal (binding_set,
727 GTK_TYPE_SCROLL_TYPE,
729 gtk_binding_entry_add_signal (binding_set,
732 GTK_TYPE_SCROLL_TYPE,
734 gtk_binding_entry_add_signal (binding_set,
737 GTK_TYPE_SCROLL_TYPE,
739 gtk_binding_entry_add_signal (binding_set,
742 GTK_TYPE_SCROLL_TYPE,
743 GTK_SCROLL_PAGE_DOWN);
744 gtk_binding_entry_add_signal (binding_set,
747 GTK_TYPE_SCROLL_TYPE,
748 GTK_SCROLL_PAGE_DOWN);
750 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
751 P_("Can change accelerators"),
752 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
754 GTK_PARAM_READWRITE));
756 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
757 P_("Delay before submenus appear"),
758 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
762 GTK_PARAM_READWRITE));
764 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
765 P_("Delay before hiding a submenu"),
766 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
769 DEFAULT_POPDOWN_DELAY,
770 GTK_PARAM_READWRITE));
772 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
777 gtk_menu_set_property (GObject *object,
784 menu = GTK_MENU (object);
789 gtk_menu_set_active (menu, g_value_get_int (value));
791 case PROP_ACCEL_GROUP:
792 gtk_menu_set_accel_group (menu, g_value_get_object (value));
794 case PROP_ACCEL_PATH:
795 gtk_menu_set_accel_path (menu, g_value_get_string (value));
797 case PROP_ATTACH_WIDGET:
801 widget = gtk_menu_get_attach_widget (menu);
803 gtk_menu_detach (menu);
805 widget = (GtkWidget*) g_value_get_object (value);
807 gtk_menu_attach_to_widget (menu, widget, NULL);
810 case PROP_TEAROFF_STATE:
811 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
813 case PROP_TEAROFF_TITLE:
814 gtk_menu_set_title (menu, g_value_get_string (value));
817 gtk_menu_set_monitor (menu, g_value_get_int (value));
820 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
826 gtk_menu_get_property (GObject *object,
833 menu = GTK_MENU (object);
838 g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->children, gtk_menu_get_active (menu)));
840 case PROP_ACCEL_GROUP:
841 g_value_set_object (value, gtk_menu_get_accel_group (menu));
843 case PROP_ACCEL_PATH:
844 g_value_set_string (value, gtk_menu_get_accel_path (menu));
846 case PROP_ATTACH_WIDGET:
847 g_value_set_object (value, gtk_menu_get_attach_widget (menu));
849 case PROP_TEAROFF_STATE:
850 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
852 case PROP_TEAROFF_TITLE:
853 g_value_set_string (value, gtk_menu_get_title (menu));
856 g_value_set_int (value, gtk_menu_get_monitor (menu));
859 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
865 gtk_menu_set_child_property (GtkContainer *container,
871 GtkMenu *menu = GTK_MENU (container);
872 AttachInfo *ai = get_attach_info (child);
876 case CHILD_PROP_LEFT_ATTACH:
877 ai->left_attach = g_value_get_int (value);
879 case CHILD_PROP_RIGHT_ATTACH:
880 ai->right_attach = g_value_get_int (value);
882 case CHILD_PROP_TOP_ATTACH:
883 ai->top_attach = g_value_get_int (value);
885 case CHILD_PROP_BOTTOM_ATTACH:
886 ai->bottom_attach = g_value_get_int (value);
890 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
894 menu_queue_resize (menu);
898 gtk_menu_get_child_property (GtkContainer *container,
904 AttachInfo *ai = get_attach_info (child);
908 case CHILD_PROP_LEFT_ATTACH:
909 g_value_set_int (value, ai->left_attach);
911 case CHILD_PROP_RIGHT_ATTACH:
912 g_value_set_int (value, ai->right_attach);
914 case CHILD_PROP_TOP_ATTACH:
915 g_value_set_int (value, ai->top_attach);
917 case CHILD_PROP_BOTTOM_ATTACH:
918 g_value_set_int (value, ai->bottom_attach);
922 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
928 gtk_menu_window_event (GtkWidget *window,
932 gboolean handled = FALSE;
934 g_object_ref (window);
940 case GDK_KEY_RELEASE:
941 handled = gtk_widget_event (menu, event);
947 g_object_unref (window);
948 g_object_unref (menu);
954 gtk_menu_window_size_request (GtkWidget *window,
955 GtkRequisition *requisition,
958 GtkMenuPrivate *private = gtk_menu_get_private (menu);
960 if (private->have_position)
962 GdkScreen *screen = gtk_widget_get_screen (window);
963 GdkRectangle monitor;
965 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
967 if (private->y + requisition->height > monitor.y + monitor.height)
968 requisition->height = monitor.y + monitor.height - private->y;
970 if (private->y < monitor.y)
971 requisition->height -= monitor.y - private->y;
976 gtk_menu_init (GtkMenu *menu)
978 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
980 menu->parent_menu_item = NULL;
981 menu->old_active_menu_item = NULL;
982 menu->accel_group = NULL;
983 menu->position_func = NULL;
984 menu->position_func_data = NULL;
985 menu->toggle_size = 0;
987 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
988 "type", GTK_WINDOW_POPUP,
991 "signal::event", gtk_menu_window_event, menu,
992 "signal::size_request", gtk_menu_window_size_request, menu,
993 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
995 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
996 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
998 /* Refloat the menu, so that reference counting for the menu isn't
999 * affected by it being a child of the toplevel
1001 g_object_force_floating (G_OBJECT (menu));
1002 menu->needs_destruction_ref_count = TRUE;
1004 menu->view_window = NULL;
1005 menu->bin_window = NULL;
1007 menu->scroll_offset = 0;
1008 menu->scroll_step = 0;
1009 menu->timeout_id = 0;
1010 menu->scroll_fast = FALSE;
1012 menu->tearoff_window = NULL;
1013 menu->tearoff_hbox = NULL;
1014 menu->torn_off = FALSE;
1015 menu->tearoff_active = FALSE;
1016 menu->tearoff_adjustment = NULL;
1017 menu->tearoff_scrollbar = NULL;
1019 menu->upper_arrow_visible = FALSE;
1020 menu->lower_arrow_visible = FALSE;
1021 menu->upper_arrow_prelight = FALSE;
1022 menu->lower_arrow_prelight = FALSE;
1024 priv->upper_arrow_state = GTK_STATE_NORMAL;
1025 priv->lower_arrow_state = GTK_STATE_NORMAL;
1027 priv->have_layout = FALSE;
1028 priv->monitor_num = -1;
1032 gtk_menu_destroy (GtkObject *object)
1034 GtkMenu *menu = GTK_MENU (object);
1035 GtkMenuAttachData *data;
1036 GtkMenuPrivate *priv;
1038 gtk_menu_remove_scroll_timeout (menu);
1040 data = g_object_get_data (G_OBJECT (object), attach_data_key);
1042 gtk_menu_detach (menu);
1044 gtk_menu_stop_navigating_submenu (menu);
1046 if (menu->old_active_menu_item)
1048 g_object_unref (menu->old_active_menu_item);
1049 menu->old_active_menu_item = NULL;
1052 /* Add back the reference count for being a child */
1053 if (menu->needs_destruction_ref_count)
1055 menu->needs_destruction_ref_count = FALSE;
1056 g_object_ref (object);
1059 if (menu->accel_group)
1061 g_object_unref (menu->accel_group);
1062 menu->accel_group = NULL;
1066 gtk_widget_destroy (menu->toplevel);
1068 if (menu->tearoff_window)
1069 gtk_widget_destroy (menu->tearoff_window);
1071 priv = gtk_menu_get_private (menu);
1075 g_free (priv->heights);
1076 priv->heights = NULL;
1081 g_free (priv->title);
1085 GTK_OBJECT_CLASS (gtk_menu_parent_class)->destroy (object);
1089 menu_change_screen (GtkMenu *menu,
1090 GdkScreen *new_screen)
1092 GtkMenuPrivate *private = gtk_menu_get_private (menu);
1094 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1096 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1102 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1103 gtk_menu_position (menu);
1106 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1107 private->monitor_num = -1;
1111 attach_widget_screen_changed (GtkWidget *attach_widget,
1112 GdkScreen *previous_screen,
1115 if (gtk_widget_has_screen (attach_widget) &&
1116 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1118 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1123 gtk_menu_attach_to_widget (GtkMenu *menu,
1124 GtkWidget *attach_widget,
1125 GtkMenuDetachFunc detacher)
1127 GtkMenuAttachData *data;
1130 g_return_if_fail (GTK_IS_MENU (menu));
1131 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1133 /* keep this function in sync with gtk_widget_set_parent()
1136 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1139 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1140 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1144 g_object_ref_sink (menu);
1146 data = g_slice_new (GtkMenuAttachData);
1147 data->attach_widget = attach_widget;
1149 g_signal_connect (attach_widget, "screen_changed",
1150 G_CALLBACK (attach_widget_screen_changed), menu);
1151 attach_widget_screen_changed (attach_widget, NULL, menu);
1153 data->detacher = detacher;
1154 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1155 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1156 if (!g_list_find (list, menu))
1158 list = g_list_prepend (list, menu);
1160 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1161 (GDestroyNotify) g_list_free);
1163 if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
1164 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1166 /* we don't need to set the style here, since
1167 * we are a toplevel widget.
1170 /* Fallback title for menu comes from attach widget */
1171 gtk_menu_update_title (menu);
1175 gtk_menu_get_attach_widget (GtkMenu *menu)
1177 GtkMenuAttachData *data;
1179 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1181 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1183 return data->attach_widget;
1188 gtk_menu_detach (GtkMenu *menu)
1190 GtkMenuAttachData *data;
1193 g_return_if_fail (GTK_IS_MENU (menu));
1195 /* keep this function in sync with gtk_widget_unparent()
1197 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1200 g_warning ("gtk_menu_detach(): menu is not attached");
1203 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1205 g_signal_handlers_disconnect_by_func (data->attach_widget,
1206 (gpointer) attach_widget_screen_changed,
1210 data->detacher (data->attach_widget, menu);
1211 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1212 list = g_list_remove (list, menu);
1214 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1215 (GDestroyNotify) g_list_free);
1217 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1219 if (GTK_WIDGET_REALIZED (menu))
1220 gtk_widget_unrealize (GTK_WIDGET (menu));
1222 g_slice_free (GtkMenuAttachData, data);
1224 /* Fallback title for menu comes from attach widget */
1225 gtk_menu_update_title (menu);
1227 g_object_unref (menu);
1231 gtk_menu_remove (GtkContainer *container,
1234 GtkMenu *menu = GTK_MENU (container);
1236 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1238 /* Clear out old_active_menu_item if it matches the item we are removing
1240 if (menu->old_active_menu_item == widget)
1242 g_object_unref (menu->old_active_menu_item);
1243 menu->old_active_menu_item = NULL;
1246 GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1247 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1249 menu_queue_resize (menu);
1255 return g_object_new (GTK_TYPE_MENU, NULL);
1259 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1263 GtkMenu *menu = GTK_MENU (menu_shell);
1264 AttachInfo *ai = get_attach_info (child);
1266 ai->left_attach = -1;
1267 ai->right_attach = -1;
1268 ai->top_attach = -1;
1269 ai->bottom_attach = -1;
1271 if (GTK_WIDGET_REALIZED (menu_shell))
1272 gtk_widget_set_parent_window (child, menu->bin_window);
1274 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1276 menu_queue_resize (menu);
1280 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1285 widget = GTK_WIDGET (menu);
1291 GdkGCValues gc_values;
1293 menu->tearoff_active = FALSE;
1294 menu->saved_scroll_offset = menu->scroll_offset;
1296 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
1297 gc = gdk_gc_new_with_values (widget->window,
1298 &gc_values, GDK_GC_SUBWINDOW);
1300 gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1302 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
1307 gdk_draw_drawable (pixmap, gc,
1308 menu->tearoff_window->window,
1309 0, 0, 0, 0, -1, -1);
1310 g_object_unref (gc);
1312 gtk_widget_set_size_request (menu->tearoff_window,
1316 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
1317 g_object_unref (pixmap);
1322 popup_grab_on_window (GdkWindow *window,
1323 guint32 activate_time,
1324 gboolean grab_keyboard)
1326 if ((gdk_pointer_grab (window, TRUE,
1327 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1328 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1329 GDK_POINTER_MOTION_MASK,
1330 NULL, NULL, activate_time) == 0))
1332 if (!grab_keyboard ||
1333 gdk_keyboard_grab (window, TRUE,
1334 activate_time) == 0)
1338 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1349 * @menu: a #GtkMenu.
1350 * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
1351 * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
1352 * @func: a user supplied function used to position the menu, or %NULL
1353 * @data: user supplied data to be passed to @func.
1354 * @button: the mouse button which was pressed to initiate the event.
1355 * @activate_time: the time at which the activation event occurred.
1357 * Displays a menu and makes it available for selection. Applications can use
1358 * this function to display context-sensitive menus, and will typically supply
1359 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1360 * parameters. The default menu positioning function will position the menu
1361 * at the current mouse cursor position.
1363 * The @button parameter should be the mouse button pressed to initiate
1364 * the menu popup. If the menu popup was initiated by something other than
1365 * a mouse button press, such as a mouse button release or a keypress,
1366 * @button should be 0.
1368 * The @activate_time parameter is used to conflict-resolve initiation of
1369 * concurrent requests for mouse/keyboard grab requests. To function
1370 * properly, this needs to be the time stamp of the user event (such as
1371 * a mouse click or key press) that caused the initiation of the popup.
1372 * Only if no such event is available, gtk_get_current_event_time() can
1376 gtk_menu_popup (GtkMenu *menu,
1377 GtkWidget *parent_menu_shell,
1378 GtkWidget *parent_menu_item,
1379 GtkMenuPositionFunc func,
1382 guint32 activate_time)
1385 GtkWidget *xgrab_shell;
1387 GdkEvent *current_event;
1388 GtkMenuShell *menu_shell;
1389 gboolean grab_keyboard;
1390 GtkMenuPrivate *priv;
1391 GtkWidget *parent_toplevel;
1393 g_return_if_fail (GTK_IS_MENU (menu));
1395 widget = GTK_WIDGET (menu);
1396 menu_shell = GTK_MENU_SHELL (menu);
1397 priv = gtk_menu_get_private (menu);
1399 menu_shell->parent_menu_shell = parent_menu_shell;
1401 priv->seen_item_enter = FALSE;
1403 /* Find the last viewable ancestor, and make an X grab on it
1405 parent = GTK_WIDGET (menu);
1409 gboolean viewable = TRUE;
1410 GtkWidget *tmp = parent;
1414 if (!GTK_WIDGET_MAPPED (tmp))
1423 xgrab_shell = parent;
1425 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1428 /* We want to receive events generated when we map the menu; unfortunately,
1429 * since there is probably already an implicit grab in place from the
1430 * button that the user used to pop up the menu, we won't receive then --
1431 * in particular, the EnterNotify when the menu pops up under the pointer.
1433 * If we are grabbing on a parent menu shell, no problem; just grab on
1434 * that menu shell first before popping up the window with owner_events = TRUE.
1436 * When grabbing on the menu itself, things get more convuluted - we
1437 * we do an explicit grab on a specially created window with
1438 * owner_events = TRUE, which we override further down with a grab
1439 * on the menu. (We can't grab on the menu until it is mapped; we
1440 * probably could just leave the grab on the other window, with a
1441 * little reorganization of the code in gtkmenu*).
1443 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1444 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1446 if (xgrab_shell && xgrab_shell != widget)
1448 if (popup_grab_on_window (xgrab_shell->window, activate_time, grab_keyboard))
1449 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1453 GdkWindow *transfer_window;
1455 xgrab_shell = widget;
1456 transfer_window = menu_grab_transfer_window_get (menu);
1457 if (popup_grab_on_window (transfer_window, activate_time, grab_keyboard))
1458 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1461 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1463 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1464 * with a stuck up window, we just abort here. Presumably the user will
1467 menu_shell->parent_menu_shell = NULL;
1468 menu_grab_transfer_window_destroy (menu);
1472 menu_shell->active = TRUE;
1473 menu_shell->button = button;
1475 /* If we are popping up the menu from something other than, a button
1476 * press then, as a heuristic, we ignore enter events for the menu
1477 * until we get a MOTION_NOTIFY.
1480 current_event = gtk_get_current_event ();
1483 if ((current_event->type != GDK_BUTTON_PRESS) &&
1484 (current_event->type != GDK_ENTER_NOTIFY))
1485 menu_shell->ignore_enter = TRUE;
1487 gdk_event_free (current_event);
1490 menu_shell->ignore_enter = TRUE;
1494 gtk_menu_tearoff_bg_copy (menu);
1496 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1499 parent_toplevel = NULL;
1500 if (parent_menu_shell)
1501 parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1502 else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1504 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1506 parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1509 /* Set transient for to get the right window group and parent relationship */
1510 if (GTK_IS_WINDOW (parent_toplevel))
1511 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1512 GTK_WINDOW (parent_toplevel));
1514 menu->parent_menu_item = parent_menu_item;
1515 menu->position_func = func;
1516 menu->position_func_data = data;
1517 menu_shell->activate_time = activate_time;
1519 /* We need to show the menu here rather in the init function because
1520 * code expects to be able to tell if the menu is onscreen by
1521 * looking at the GTK_WIDGET_VISIBLE (menu)
1523 gtk_widget_show (GTK_WIDGET (menu));
1525 /* Position the menu, possibly changing the size request
1527 gtk_menu_position (menu);
1529 /* Compute the size of the toplevel and realize it so we
1530 * can scroll correctly.
1533 GtkRequisition tmp_request;
1534 GtkAllocation tmp_allocation = { 0, };
1536 gtk_widget_size_request (menu->toplevel, &tmp_request);
1538 tmp_allocation.width = tmp_request.width;
1539 tmp_allocation.height = tmp_request.height;
1541 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1543 gtk_widget_realize (GTK_WIDGET (menu));
1546 gtk_menu_scroll_to (menu, menu->scroll_offset);
1548 /* if no item is selected, select the first one */
1549 if (!menu_shell->active_menu_item)
1551 gboolean touchscreen_mode;
1553 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1554 "gtk-touchscreen-mode", &touchscreen_mode,
1557 if (touchscreen_mode)
1558 gtk_menu_shell_select_first (menu_shell, TRUE);
1561 /* Once everything is set up correctly, map the toplevel window on
1564 gtk_widget_show (menu->toplevel);
1566 if (xgrab_shell == widget)
1567 popup_grab_on_window (widget->window, activate_time, grab_keyboard); /* Should always succeed */
1568 gtk_grab_add (GTK_WIDGET (menu));
1572 gtk_menu_popdown (GtkMenu *menu)
1574 GtkMenuPrivate *private;
1575 GtkMenuShell *menu_shell;
1577 g_return_if_fail (GTK_IS_MENU (menu));
1579 menu_shell = GTK_MENU_SHELL (menu);
1580 private = gtk_menu_get_private (menu);
1582 menu_shell->parent_menu_shell = NULL;
1583 menu_shell->active = FALSE;
1584 menu_shell->ignore_enter = FALSE;
1586 private->have_position = FALSE;
1588 gtk_menu_stop_scrolling (menu);
1590 gtk_menu_stop_navigating_submenu (menu);
1592 if (menu_shell->active_menu_item)
1594 if (menu->old_active_menu_item)
1595 g_object_unref (menu->old_active_menu_item);
1596 menu->old_active_menu_item = menu_shell->active_menu_item;
1597 g_object_ref (menu->old_active_menu_item);
1600 gtk_menu_shell_deselect (menu_shell);
1602 /* The X Grab, if present, will automatically be removed when we hide
1604 gtk_widget_hide (menu->toplevel);
1605 gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1609 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1611 if (GTK_BIN (menu->toplevel)->child)
1613 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1617 /* We popped up the menu from the tearoff, so we need to
1618 * release the grab - we aren't actually hiding the menu.
1620 if (menu_shell->have_xgrab)
1622 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
1624 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1625 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1629 /* gtk_menu_popdown is called each time a menu item is selected from
1630 * a torn off menu. Only scroll back to the saved position if the
1631 * non-tearoff menu was popped down.
1633 if (!menu->tearoff_active)
1634 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1635 menu->tearoff_active = TRUE;
1638 gtk_widget_hide (GTK_WIDGET (menu));
1640 menu_shell->have_xgrab = FALSE;
1641 gtk_grab_remove (GTK_WIDGET (menu));
1643 menu_grab_transfer_window_destroy (menu);
1647 gtk_menu_get_active (GtkMenu *menu)
1652 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1654 if (!menu->old_active_menu_item)
1657 children = GTK_MENU_SHELL (menu)->children;
1661 child = children->data;
1662 children = children->next;
1664 if (GTK_BIN (child)->child)
1669 menu->old_active_menu_item = child;
1670 if (menu->old_active_menu_item)
1671 g_object_ref (menu->old_active_menu_item);
1674 return menu->old_active_menu_item;
1678 gtk_menu_set_active (GtkMenu *menu,
1684 g_return_if_fail (GTK_IS_MENU (menu));
1686 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1689 child = tmp_list->data;
1690 if (GTK_BIN (child)->child)
1692 if (menu->old_active_menu_item)
1693 g_object_unref (menu->old_active_menu_item);
1694 menu->old_active_menu_item = child;
1695 g_object_ref (menu->old_active_menu_item);
1701 gtk_menu_set_accel_group (GtkMenu *menu,
1702 GtkAccelGroup *accel_group)
1704 g_return_if_fail (GTK_IS_MENU (menu));
1706 if (menu->accel_group != accel_group)
1708 if (menu->accel_group)
1709 g_object_unref (menu->accel_group);
1710 menu->accel_group = accel_group;
1711 if (menu->accel_group)
1712 g_object_ref (menu->accel_group);
1713 _gtk_menu_refresh_accel_paths (menu, TRUE);
1718 gtk_menu_get_accel_group (GtkMenu *menu)
1720 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1722 return menu->accel_group;
1726 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1729 /* Menu items chain here to figure whether they can activate their
1730 * accelerators. Unlike ordinary widgets, menus allow accel
1731 * activation even if invisible since that's the usual case for
1732 * submenus/popup-menus. however, the state of the attach widget
1733 * affects the "activeness" of the menu.
1735 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1738 return gtk_widget_can_activate_accel (awidget, signal_id);
1740 return GTK_WIDGET_IS_SENSITIVE (widget);
1744 * gtk_menu_set_accel_path
1745 * @menu: a valid #GtkMenu
1746 * @accel_path: a valid accelerator path
1748 * Sets an accelerator path for this menu from which accelerator paths
1749 * for its immediate children, its menu items, can be constructed.
1750 * The main purpose of this function is to spare the programmer the
1751 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1752 * each menu item that should support runtime user changable accelerators.
1753 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1754 * each menu item of this menu, that contains a label describing its purpose,
1755 * automatically gets an accel path assigned. For example, a menu containing
1756 * menu items "New" and "Exit", will, after
1757 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1758 * has been called, assign its items the accel paths:
1759 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1760 * Assigning accel paths to menu items then enables the user to change
1761 * their accelerators at runtime. More details about accelerator paths
1762 * and their default setups can be found at gtk_accel_map_add_entry().
1764 * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1765 * pass a static string, you can save some memory by interning it first with
1766 * g_intern_static_string().
1769 gtk_menu_set_accel_path (GtkMenu *menu,
1770 const gchar *accel_path)
1772 g_return_if_fail (GTK_IS_MENU (menu));
1774 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1776 /* FIXME: accel_path should be defined as const gchar* */
1777 menu->accel_path = (gchar*)g_intern_string (accel_path);
1778 if (menu->accel_path)
1779 _gtk_menu_refresh_accel_paths (menu, FALSE);
1783 * gtk_menu_get_accel_path
1784 * @menu: a valid #GtkMenu
1786 * Retrieves the accelerator path set on the menu.
1791 gtk_menu_get_accel_path (GtkMenu *menu)
1793 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1795 return menu->accel_path;
1800 gboolean group_changed;
1804 refresh_accel_paths_foreach (GtkWidget *widget,
1807 AccelPropagation *prop = data;
1809 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1810 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1811 prop->menu->accel_path,
1812 prop->menu->accel_group,
1813 prop->group_changed);
1817 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1818 gboolean group_changed)
1820 g_return_if_fail (GTK_IS_MENU (menu));
1822 if (menu->accel_path && menu->accel_group)
1824 AccelPropagation prop;
1827 prop.group_changed = group_changed;
1828 gtk_container_foreach (GTK_CONTAINER (menu),
1829 refresh_accel_paths_foreach,
1835 gtk_menu_reposition (GtkMenu *menu)
1837 g_return_if_fail (GTK_IS_MENU (menu));
1839 if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1840 gtk_menu_position (menu);
1844 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1847 g_return_if_fail (GTK_IS_MENU (menu));
1849 if (adjustment->value != menu->scroll_offset)
1850 gtk_menu_scroll_to (menu, adjustment->value);
1854 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1857 GdkGeometry geometry_hints;
1859 if (!menu->tearoff_window)
1862 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1864 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1865 width += menu->tearoff_scrollbar->requisition.width;
1868 geometry_hints.min_width = width;
1869 geometry_hints.max_width = width;
1871 geometry_hints.min_height = 0;
1872 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1874 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1877 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1881 gtk_menu_update_title (GtkMenu *menu)
1883 if (menu->tearoff_window)
1886 GtkWidget *attach_widget;
1888 title = gtk_menu_get_title (menu);
1891 attach_widget = gtk_menu_get_attach_widget (menu);
1892 if (GTK_IS_MENU_ITEM (attach_widget))
1894 GtkWidget *child = GTK_BIN (attach_widget)->child;
1895 if (GTK_IS_LABEL (child))
1896 title = gtk_label_get_text (GTK_LABEL (child));
1901 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1906 gtk_menu_get_toplevel (GtkWidget *menu)
1908 GtkWidget *attach, *toplevel;
1910 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
1912 if (GTK_IS_MENU_ITEM (attach))
1913 attach = attach->parent;
1915 if (GTK_IS_MENU (attach))
1916 return gtk_menu_get_toplevel (attach);
1917 else if (GTK_IS_WIDGET (attach))
1919 toplevel = gtk_widget_get_toplevel (attach);
1920 if (GTK_WIDGET_TOPLEVEL (toplevel))
1928 tearoff_window_destroyed (GtkWidget *widget,
1931 gtk_menu_set_tearoff_state (menu, FALSE);
1935 gtk_menu_set_tearoff_state (GtkMenu *menu,
1940 g_return_if_fail (GTK_IS_MENU (menu));
1942 if (menu->torn_off != torn_off)
1944 menu->torn_off = torn_off;
1945 menu->tearoff_active = torn_off;
1949 if (GTK_WIDGET_VISIBLE (menu))
1950 gtk_menu_popdown (menu);
1952 if (!menu->tearoff_window)
1954 GtkWidget *toplevel;
1956 menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
1957 "type", GTK_WINDOW_TOPLEVEL,
1958 "screen", gtk_widget_get_screen (menu->toplevel),
1959 "app-paintable", TRUE,
1962 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1963 GDK_WINDOW_TYPE_HINT_MENU);
1964 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1965 g_signal_connect (menu->tearoff_window, "destroy",
1966 G_CALLBACK (tearoff_window_destroyed), menu);
1967 g_signal_connect (menu->tearoff_window, "event",
1968 G_CALLBACK (gtk_menu_window_event), menu);
1970 gtk_menu_update_title (menu);
1972 gtk_widget_realize (menu->tearoff_window);
1974 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
1975 if (toplevel != NULL)
1976 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
1977 GTK_WINDOW (toplevel));
1979 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1980 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1982 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
1983 menu->tearoff_adjustment =
1984 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1986 GTK_WIDGET (menu)->requisition.height,
1990 g_object_connect (menu->tearoff_adjustment,
1991 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1993 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1995 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1996 menu->tearoff_scrollbar,
1999 if (menu->tearoff_adjustment->upper > height)
2000 gtk_widget_show (menu->tearoff_scrollbar);
2002 gtk_widget_show (menu->tearoff_hbox);
2005 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
2007 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
2009 /* Update menu->requisition
2011 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
2013 gtk_menu_set_tearoff_hints (menu, width);
2015 gtk_widget_realize (menu->tearoff_window);
2016 gtk_menu_position (menu);
2018 gtk_widget_show (GTK_WIDGET (menu));
2019 gtk_widget_show (menu->tearoff_window);
2021 gtk_menu_scroll_to (menu, 0);
2026 gtk_widget_hide (GTK_WIDGET (menu));
2027 gtk_widget_hide (menu->tearoff_window);
2028 if (GTK_IS_CONTAINER (menu->toplevel))
2029 gtk_menu_reparent (menu, menu->toplevel, FALSE);
2030 gtk_widget_destroy (menu->tearoff_window);
2032 menu->tearoff_window = NULL;
2033 menu->tearoff_hbox = NULL;
2034 menu->tearoff_scrollbar = NULL;
2035 menu->tearoff_adjustment = NULL;
2038 g_object_notify (G_OBJECT (menu), "tearoff-state");
2043 * gtk_menu_get_tearoff_state:
2046 * Returns whether the menu is torn off. See
2047 * gtk_menu_set_tearoff_state ().
2049 * Return value: %TRUE if the menu is currently torn off.
2052 gtk_menu_get_tearoff_state (GtkMenu *menu)
2054 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2056 return menu->torn_off;
2060 * gtk_menu_set_title:
2062 * @title: a string containing the title for the menu.
2064 * Sets the title string for the menu. The title is displayed when the menu
2065 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
2066 * attached to a parent menu item, and if so it will try to use the same text as
2067 * that menu item's label.
2070 gtk_menu_set_title (GtkMenu *menu,
2073 GtkMenuPrivate *priv;
2076 g_return_if_fail (GTK_IS_MENU (menu));
2078 priv = gtk_menu_get_private (menu);
2080 old_title = priv->title;
2081 priv->title = g_strdup (title);
2084 gtk_menu_update_title (menu);
2085 g_object_notify (G_OBJECT (menu), "tearoff-title");
2089 * gtk_menu_get_title:
2092 * Returns the title of the menu. See gtk_menu_set_title().
2094 * Return value: the title of the menu, or %NULL if the menu has no
2095 * title set on it. This string is owned by the widget and should
2096 * not be modified or freed.
2098 G_CONST_RETURN gchar *
2099 gtk_menu_get_title (GtkMenu *menu)
2101 GtkMenuPrivate *priv;
2103 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2105 priv = gtk_menu_get_private (menu);
2111 gtk_menu_reorder_child (GtkMenu *menu,
2115 GtkMenuShell *menu_shell;
2117 g_return_if_fail (GTK_IS_MENU (menu));
2118 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2120 menu_shell = GTK_MENU_SHELL (menu);
2122 if (g_list_find (menu_shell->children, child))
2124 menu_shell->children = g_list_remove (menu_shell->children, child);
2125 menu_shell->children = g_list_insert (menu_shell->children, child, position);
2127 menu_queue_resize (menu);
2132 gtk_menu_style_set (GtkWidget *widget,
2133 GtkStyle *previous_style)
2135 if (GTK_WIDGET_REALIZED (widget))
2137 GtkMenu *menu = GTK_MENU (widget);
2139 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2140 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2141 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2146 get_arrows_border (GtkMenu *menu, GtkBorder *border)
2148 guint scroll_arrow_height;
2150 gtk_widget_style_get (GTK_WIDGET (menu),
2151 "scroll-arrow-vlength", &scroll_arrow_height,
2154 border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2155 border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2157 border->left = border->right = 0;
2161 gtk_menu_realize (GtkWidget *widget)
2163 GdkWindowAttr attributes;
2164 gint attributes_mask;
2169 guint vertical_padding;
2170 guint horizontal_padding;
2171 GtkBorder arrow_border;
2173 g_return_if_fail (GTK_IS_MENU (widget));
2175 menu = GTK_MENU (widget);
2177 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
2179 attributes.window_type = GDK_WINDOW_CHILD;
2180 attributes.x = widget->allocation.x;
2181 attributes.y = widget->allocation.y;
2182 attributes.width = widget->allocation.width;
2183 attributes.height = widget->allocation.height;
2184 attributes.wclass = GDK_INPUT_OUTPUT;
2185 attributes.visual = gtk_widget_get_visual (widget);
2186 attributes.colormap = gtk_widget_get_colormap (widget);
2188 attributes.event_mask = gtk_widget_get_events (widget);
2190 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2191 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2193 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2194 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
2195 gdk_window_set_user_data (widget->window, widget);
2197 border_width = GTK_CONTAINER (widget)->border_width;
2199 gtk_widget_style_get (GTK_WIDGET (menu),
2200 "vertical-padding", &vertical_padding,
2201 "horizontal-padding", &horizontal_padding,
2204 attributes.x = border_width + widget->style->xthickness + horizontal_padding;
2205 attributes.y = border_width + widget->style->ythickness + vertical_padding;
2206 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
2207 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
2209 get_arrows_border (menu, &arrow_border);
2210 attributes.y += arrow_border.top;
2211 attributes.height -= arrow_border.top;
2212 attributes.height -= arrow_border.bottom;
2214 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
2215 gdk_window_set_user_data (menu->view_window, menu);
2219 attributes.width = MAX (1, widget->allocation.width - (border_width + widget->style->xthickness + horizontal_padding) * 2);
2220 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
2222 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
2223 gdk_window_set_user_data (menu->bin_window, menu);
2225 children = GTK_MENU_SHELL (menu)->children;
2228 child = children->data;
2229 children = children->next;
2231 gtk_widget_set_parent_window (child, menu->bin_window);
2234 widget->style = gtk_style_attach (widget->style, widget->window);
2235 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2236 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2237 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2239 if (GTK_MENU_SHELL (widget)->active_menu_item)
2240 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2241 GTK_MENU_SHELL (widget)->active_menu_item);
2243 gdk_window_show (menu->bin_window);
2244 gdk_window_show (menu->view_window);
2248 gtk_menu_focus (GtkWidget *widget,
2249 GtkDirectionType direction)
2252 * A menu or its menu items cannot have focus
2257 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2260 menu_grab_transfer_window_get (GtkMenu *menu)
2262 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2265 GdkWindowAttr attributes;
2266 gint attributes_mask;
2268 attributes.x = -100;
2269 attributes.y = -100;
2270 attributes.width = 10;
2271 attributes.height = 10;
2272 attributes.window_type = GDK_WINDOW_TEMP;
2273 attributes.wclass = GDK_INPUT_ONLY;
2274 attributes.override_redirect = TRUE;
2275 attributes.event_mask = 0;
2277 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2279 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2280 &attributes, attributes_mask);
2281 gdk_window_set_user_data (window, menu);
2283 gdk_window_show (window);
2285 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2292 menu_grab_transfer_window_destroy (GtkMenu *menu)
2294 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2297 gdk_window_set_user_data (window, NULL);
2298 gdk_window_destroy (window);
2299 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2304 gtk_menu_unrealize (GtkWidget *widget)
2306 GtkMenu *menu = GTK_MENU (widget);
2308 menu_grab_transfer_window_destroy (menu);
2310 gdk_window_set_user_data (menu->view_window, NULL);
2311 gdk_window_destroy (menu->view_window);
2312 menu->view_window = NULL;
2314 gdk_window_set_user_data (menu->bin_window, NULL);
2315 gdk_window_destroy (menu->bin_window);
2316 menu->bin_window = NULL;
2318 (* GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize) (widget);
2322 gtk_menu_size_request (GtkWidget *widget,
2323 GtkRequisition *requisition)
2327 GtkMenuShell *menu_shell;
2330 guint max_toggle_size;
2331 guint max_accel_width;
2332 guint vertical_padding;
2333 guint horizontal_padding;
2334 GtkRequisition child_requisition;
2335 GtkMenuPrivate *priv;
2337 g_return_if_fail (GTK_IS_MENU (widget));
2338 g_return_if_fail (requisition != NULL);
2340 menu = GTK_MENU (widget);
2341 menu_shell = GTK_MENU_SHELL (widget);
2342 priv = gtk_menu_get_private (menu);
2344 requisition->width = 0;
2345 requisition->height = 0;
2347 max_toggle_size = 0;
2348 max_accel_width = 0;
2350 g_free (priv->heights);
2351 priv->heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2352 priv->heights_length = gtk_menu_get_n_rows (menu);
2354 children = menu_shell->children;
2361 child = children->data;
2362 children = children->next;
2364 if (! GTK_WIDGET_VISIBLE (child))
2367 get_effective_child_attach (child, &l, &r, &t, &b);
2369 /* It's important to size_request the child
2370 * before doing the toggle size request, in
2371 * case the toggle size request depends on the size
2372 * request of a child of the child (e.g. for ImageMenuItem)
2375 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
2376 gtk_widget_size_request (child, &child_requisition);
2378 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2379 max_toggle_size = MAX (max_toggle_size, toggle_size);
2380 max_accel_width = MAX (max_accel_width,
2381 GTK_MENU_ITEM (child)->accelerator_width);
2383 part = child_requisition.width / (r - l);
2384 requisition->width = MAX (requisition->width, part);
2386 part = MAX (child_requisition.height, toggle_size) / (b - t);
2387 priv->heights[t] = MAX (priv->heights[t], part);
2390 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2391 requisition->height += priv->heights[i];
2393 requisition->width += max_toggle_size + max_accel_width;
2394 requisition->width *= gtk_menu_get_n_columns (menu);
2396 gtk_widget_style_get (GTK_WIDGET (menu),
2397 "vertical-padding", &vertical_padding,
2398 "horizontal-padding", &horizontal_padding,
2401 requisition->width += (GTK_CONTAINER (menu)->border_width + horizontal_padding +
2402 widget->style->xthickness) * 2;
2403 requisition->height += (GTK_CONTAINER (menu)->border_width + vertical_padding +
2404 widget->style->ythickness) * 2;
2406 menu->toggle_size = max_toggle_size;
2408 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2410 if (menu->tearoff_active)
2411 gtk_menu_set_tearoff_hints (menu, requisition->width);
2415 gtk_menu_size_allocate (GtkWidget *widget,
2416 GtkAllocation *allocation)
2419 GtkMenuShell *menu_shell;
2421 GtkAllocation child_allocation;
2422 GtkRequisition child_requisition;
2423 GtkMenuPrivate *priv;
2427 guint vertical_padding;
2428 guint horizontal_padding;
2430 g_return_if_fail (GTK_IS_MENU (widget));
2431 g_return_if_fail (allocation != NULL);
2433 menu = GTK_MENU (widget);
2434 menu_shell = GTK_MENU_SHELL (widget);
2435 priv = gtk_menu_get_private (menu);
2437 widget->allocation = *allocation;
2438 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2440 gtk_widget_style_get (GTK_WIDGET (menu),
2441 "vertical-padding", &vertical_padding,
2442 "horizontal-padding", &horizontal_padding,
2445 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness + horizontal_padding;
2446 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness + vertical_padding;
2448 width = MAX (1, allocation->width - x * 2);
2449 height = MAX (1, allocation->height - y * 2);
2451 child_requisition.width -= x * 2;
2452 child_requisition.height -= y * 2;
2454 if (menu_shell->active)
2455 gtk_menu_scroll_to (menu, menu->scroll_offset);
2457 if (!menu->tearoff_active)
2459 GtkBorder arrow_border;
2461 get_arrows_border (menu, &arrow_border);
2462 y += arrow_border.top;
2463 height -= arrow_border.top;
2464 height -= arrow_border.bottom;
2467 if (GTK_WIDGET_REALIZED (widget))
2469 gdk_window_move_resize (widget->window,
2470 allocation->x, allocation->y,
2471 allocation->width, allocation->height);
2473 gdk_window_move_resize (menu->view_window,
2480 if (menu_shell->children)
2482 gint base_width = width / gtk_menu_get_n_columns (menu);
2484 children = menu_shell->children;
2487 child = children->data;
2488 children = children->next;
2490 if (GTK_WIDGET_VISIBLE (child))
2495 get_effective_child_attach (child, &l, &r, &t, &b);
2497 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2500 tmp = gtk_menu_get_n_columns (menu) - l;
2501 l = gtk_menu_get_n_columns (menu) - r;
2505 child_allocation.width = (r - l) * base_width;
2506 child_allocation.height = 0;
2507 child_allocation.x = l * base_width;
2508 child_allocation.y = 0;
2510 for (i = 0; i < b; i++)
2513 child_allocation.y += priv->heights[i];
2515 child_allocation.height += priv->heights[i];
2518 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2521 gtk_widget_size_allocate (child, &child_allocation);
2522 gtk_widget_queue_draw (child);
2526 /* Resize the item window */
2527 if (GTK_WIDGET_REALIZED (widget))
2533 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2534 height += priv->heights[i];
2536 width = gtk_menu_get_n_columns (menu) * base_width;
2537 gdk_window_resize (menu->bin_window, width, height);
2540 if (menu->tearoff_active)
2542 if (allocation->height >= widget->requisition.height)
2544 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2546 gtk_widget_hide (menu->tearoff_scrollbar);
2547 gtk_menu_set_tearoff_hints (menu, allocation->width);
2549 gtk_menu_scroll_to (menu, 0);
2554 menu->tearoff_adjustment->upper = widget->requisition.height;
2555 menu->tearoff_adjustment->page_size = allocation->height;
2557 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2558 menu->tearoff_adjustment->upper)
2561 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2564 gtk_menu_scroll_to (menu, value);
2567 gtk_adjustment_changed (menu->tearoff_adjustment);
2569 if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2571 gtk_widget_show (menu->tearoff_scrollbar);
2572 gtk_menu_set_tearoff_hints (menu, allocation->width);
2580 get_arrows_visible_area (GtkMenu *menu,
2581 GdkRectangle *border,
2582 GdkRectangle *upper,
2583 GdkRectangle *lower,
2586 GtkWidget *widget = GTK_WIDGET (menu);
2587 guint vertical_padding;
2588 guint horizontal_padding;
2589 gint scroll_arrow_height;
2591 gtk_widget_style_get (widget,
2592 "vertical-padding", &vertical_padding,
2593 "horizontal-padding", &horizontal_padding,
2594 "scroll-arrow-vlength", &scroll_arrow_height,
2597 border->x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness + horizontal_padding;
2598 border->y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
2599 gdk_drawable_get_size (widget->window, &border->width, &border->height);
2601 upper->x = border->x;
2602 upper->y = border->y;
2603 upper->width = border->width - 2 * border->x;
2604 upper->height = scroll_arrow_height;
2606 lower->x = border->x;
2607 lower->y = border->height - border->y - scroll_arrow_height;
2608 lower->width = border->width - 2 * border->x;
2609 lower->height = scroll_arrow_height;
2611 *arrow_space = scroll_arrow_height - 2 * widget->style->ythickness;
2615 gtk_menu_paint (GtkWidget *widget,
2616 GdkEventExpose *event)
2619 GtkMenuPrivate *priv;
2620 GdkRectangle border;
2625 g_return_if_fail (GTK_IS_MENU (widget));
2627 menu = GTK_MENU (widget);
2628 priv = gtk_menu_get_private (menu);
2630 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2632 if (event->window == widget->window)
2634 gint arrow_size = 0.7 * arrow_space;
2636 gtk_paint_box (widget->style,
2640 &event->area, widget, "menu",
2643 if (menu->upper_arrow_visible && !menu->tearoff_active)
2645 gtk_paint_box (widget->style,
2647 priv->upper_arrow_state,
2649 &event->area, widget, "menu_scroll_arrow_up",
2655 gtk_paint_arrow (widget->style,
2657 priv->upper_arrow_state,
2659 &event->area, widget, "menu_scroll_arrow_up",
2662 upper.x + (upper.width - arrow_size) / 2,
2663 upper.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
2664 arrow_size, arrow_size);
2667 if (menu->lower_arrow_visible && !menu->tearoff_active)
2669 gtk_paint_box (widget->style,
2671 priv->lower_arrow_state,
2673 &event->area, widget, "menu_scroll_arrow_down",
2679 gtk_paint_arrow (widget->style,
2681 priv->lower_arrow_state,
2683 &event->area, widget, "menu_scroll_arrow_down",
2686 lower.x + (lower.width - arrow_size) / 2,
2687 lower.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
2688 arrow_size, arrow_size);
2691 else if (event->window == menu->bin_window)
2693 gint y = -border.y + menu->scroll_offset;
2695 if (!menu->tearoff_active)
2697 GtkBorder arrow_border;
2699 get_arrows_border (menu, &arrow_border);
2700 y -= arrow_border.top;
2703 gtk_paint_box (widget->style,
2707 &event->area, widget, "menu",
2709 border.width, border.height);
2714 gtk_menu_expose (GtkWidget *widget,
2715 GdkEventExpose *event)
2717 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2718 g_return_val_if_fail (event != NULL, FALSE);
2720 if (GTK_WIDGET_DRAWABLE (widget))
2722 gtk_menu_paint (widget, event);
2724 (* GTK_WIDGET_CLASS (gtk_menu_parent_class)->expose_event) (widget, event);
2731 gtk_menu_show (GtkWidget *widget)
2733 GtkMenu *menu = GTK_MENU (widget);
2735 _gtk_menu_refresh_accel_paths (menu, FALSE);
2737 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
2741 gtk_menu_button_scroll (GtkMenu *menu,
2742 GdkEventButton *event)
2744 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2746 gboolean touchscreen_mode;
2748 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
2749 "gtk-touchscreen-mode", &touchscreen_mode,
2752 if (touchscreen_mode)
2753 gtk_menu_handle_scrolling (menu,
2754 event->x_root, event->y_root,
2755 event->type == GDK_BUTTON_PRESS,
2765 pointer_in_menu_window (GtkWidget *widget,
2769 GtkMenu *menu = GTK_MENU (widget);
2771 if (GTK_WIDGET_MAPPED (menu->toplevel))
2773 GtkMenuShell *menu_shell;
2774 gint window_x, window_y;
2776 gdk_window_get_position (menu->toplevel->window, &window_x, &window_y);
2778 if (x_root >= window_x && x_root < window_x + widget->allocation.width &&
2779 y_root >= window_y && y_root < window_y + widget->allocation.height)
2782 menu_shell = GTK_MENU_SHELL (widget);
2784 if (GTK_IS_MENU (menu_shell->parent_menu_shell))
2785 return pointer_in_menu_window (menu_shell->parent_menu_shell,
2793 gtk_menu_button_press (GtkWidget *widget,
2794 GdkEventButton *event)
2796 if (event->type != GDK_BUTTON_PRESS)
2799 /* Don't pass down to menu shell for presses over scroll arrows
2801 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
2804 /* Don't pass down to menu shell if a non-menuitem part of the menu
2805 * was clicked. The check for the event_widget being a GtkMenuShell
2806 * works because we have the pointer grabbed on menu_shell->window
2807 * with owner_events=TRUE, so all events that are either outside
2808 * the menu or on its border are delivered relative to
2809 * menu_shell->window.
2811 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
2812 pointer_in_menu_window (widget, event->x_root, event->y_root))
2815 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
2819 gtk_menu_button_release (GtkWidget *widget,
2820 GdkEventButton *event)
2822 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
2824 if (priv->ignore_button_release)
2826 priv->ignore_button_release = FALSE;
2830 if (event->type != GDK_BUTTON_RELEASE)
2833 /* Don't pass down to menu shell for releases over scroll arrows
2835 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
2838 /* Don't pass down to menu shell if a non-menuitem part of the menu
2839 * was clicked (see comment in button_press()).
2841 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
2842 pointer_in_menu_window (widget, event->x_root, event->y_root))
2844 /* Ugly: make sure menu_shell->button gets reset to 0 when we
2845 * bail out early here so it is in a consistent state for the
2846 * next button_press/button_release in GtkMenuShell.
2849 if (GTK_MENU_SHELL (widget)->active)
2850 GTK_MENU_SHELL (widget)->button = 0;
2855 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
2858 static const gchar *
2859 get_accel_path (GtkWidget *menu_item,
2864 GClosure *accel_closure;
2865 GtkAccelGroup *accel_group;
2867 path = _gtk_widget_get_accel_path (menu_item, locked);
2870 path = GTK_MENU_ITEM (menu_item)->accel_path;
2876 label = GTK_BIN (menu_item)->child;
2878 if (GTK_IS_ACCEL_LABEL (label))
2880 g_object_get (label,
2881 "accel-closure", &accel_closure,
2885 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
2887 *locked = accel_group->lock_count > 0;
2897 gtk_menu_key_press (GtkWidget *widget,
2900 GtkMenuShell *menu_shell;
2902 gboolean delete = FALSE;
2903 gboolean can_change_accels;
2904 gchar *accel = NULL;
2905 guint accel_key, accel_mods;
2906 GdkModifierType consumed_modifiers;
2907 GdkDisplay *display;
2909 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2910 g_return_val_if_fail (event != NULL, FALSE);
2912 menu_shell = GTK_MENU_SHELL (widget);
2913 menu = GTK_MENU (widget);
2915 gtk_menu_stop_navigating_submenu (menu);
2917 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
2920 display = gtk_widget_get_display (widget);
2922 g_object_get (gtk_widget_get_settings (widget),
2923 "gtk-menu-bar-accel", &accel,
2924 "gtk-can-change-accels", &can_change_accels,
2927 if (accel && *accel)
2930 GdkModifierType mods = 0;
2932 gtk_accelerator_parse (accel, &keyval, &mods);
2935 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
2937 /* FIXME this is wrong, needs to be in the global accel resolution
2938 * thing, to properly consider i18n etc., but that probably requires
2939 * AccelGroup changes etc.
2941 if (event->keyval == keyval && (mods & event->state) == mods)
2943 gtk_menu_shell_cancel (menu_shell);
2951 switch (event->keyval)
2962 /* Figure out what modifiers went into determining the key symbol */
2963 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
2964 event->hardware_keycode, event->state, event->group,
2965 NULL, NULL, NULL, &consumed_modifiers);
2967 accel_key = gdk_keyval_to_lower (event->keyval);
2968 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
2970 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
2971 * We re-upper case when we match against the keyval, but display and save in caseless form.
2973 if (accel_key != event->keyval)
2974 accel_mods |= GDK_SHIFT_MASK;
2976 /* Modify the accelerators */
2977 if (can_change_accels &&
2978 menu_shell->active_menu_item &&
2979 GTK_BIN (menu_shell->active_menu_item)->child && /* no separators */
2980 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
2981 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
2983 GtkWidget *menu_item = menu_shell->active_menu_item;
2984 gboolean locked, replace_accels = TRUE;
2987 path = get_accel_path (menu_item, &locked);
2988 if (!path || locked)
2990 /* can't change accelerators on menu_items without paths
2991 * (basically, those items are accelerator-locked).
2993 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
2994 gtk_widget_error_bell (widget);
3000 /* For the keys that act to delete the current setting, we delete
3001 * the current setting if there is one, otherwise, we set the
3002 * key as the accelerator.
3008 if (gtk_accel_map_lookup_entry (path, &key) &&
3009 (key.accel_key || key.accel_mods))
3015 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3019 /* we failed, probably because this key is in use and
3022 /* g_print("failed to change\n"); */
3023 gtk_widget_error_bell (widget);
3032 check_threshold (GtkWidget *widget,
3033 int start_x, int start_y,
3039 ABS (start_x - x) > THRESHOLD ||
3040 ABS (start_y - y) > THRESHOLD;
3044 definitely_within_item (GtkWidget *widget,
3048 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
3051 gdk_drawable_get_size (window, &w, &h);
3054 check_threshold (widget, 0, 0, x, y) &&
3055 check_threshold (widget, w - 1, 0, x, y) &&
3056 check_threshold (widget, w - 1, h - 1, x, y) &&
3057 check_threshold (widget, 0, h - 1, x, y);
3061 gtk_menu_motion_notify (GtkWidget *widget,
3062 GdkEventMotion *event)
3064 GtkWidget *menu_item;
3066 GtkMenuShell *menu_shell;
3068 gboolean need_enter;
3070 if (GTK_IS_MENU (widget))
3072 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3074 if (priv->ignore_button_release)
3075 priv->ignore_button_release = FALSE;
3077 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3081 /* We received the event for one of two reasons:
3083 * a) We are the active menu, and did gtk_grab_add()
3084 * b) The widget is a child of ours, and the event was propagated
3086 * Since for computation of navigation regions, we want the menu which
3087 * is the parent of the menu item, for a), we need to find that menu,
3088 * which may be different from 'widget'.
3090 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3091 if (!GTK_IS_MENU_ITEM (menu_item) ||
3092 !GTK_IS_MENU (menu_item->parent))
3095 menu_shell = GTK_MENU_SHELL (menu_item->parent);
3096 menu = GTK_MENU (menu_shell);
3098 if (definitely_within_item (menu_item, event->x, event->y))
3099 menu_shell->activate_time = 0;
3101 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
3103 /* Check to see if we are within an active submenu's navigation region
3105 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3108 /* Make sure we pop down if we enter a non-selectable menu item, so we
3109 * don't show a submenu when the cursor is outside the stay-up triangle.
3111 if (!_gtk_menu_item_is_selectable (menu_item))
3113 gtk_menu_shell_deselect (menu_shell);
3119 /* The menu is now sensitive to enter events on its items, but
3120 * was previously sensitive. So we fake an enter event.
3124 menu_shell->ignore_enter = FALSE;
3126 gdk_drawable_get_size (event->window, &width, &height);
3127 if (event->x >= 0 && event->x < width &&
3128 event->y >= 0 && event->y < height)
3130 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3133 send_event->crossing.window = g_object_ref (event->window);
3134 send_event->crossing.time = event->time;
3135 send_event->crossing.send_event = TRUE;
3136 send_event->crossing.x_root = event->x_root;
3137 send_event->crossing.y_root = event->y_root;
3138 send_event->crossing.x = event->x;
3139 send_event->crossing.y = event->y;
3140 send_event->crossing.state = event->state;
3142 /* We send the event to 'widget', the currently active menu,
3143 * instead of 'menu', the menu that the pointer is in. This
3144 * will ensure that the event will be ignored unless the
3145 * menuitem is a child of the active menu or some parent
3146 * menu of the active menu.
3148 result = gtk_widget_event (widget, send_event);
3149 gdk_event_free (send_event);
3159 get_double_arrows (GtkMenu *menu)
3161 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3162 gboolean double_arrows;
3164 gtk_widget_style_get (GTK_WIDGET (menu),
3165 "double-arrows", &double_arrows,
3168 return double_arrows || (priv->initially_pushed_in &&
3169 menu->scroll_offset != 0);
3173 gtk_menu_scroll_by (GtkMenu *menu,
3178 gint view_width, view_height;
3179 gboolean double_arrows;
3180 GtkBorder arrow_border;
3182 widget = GTK_WIDGET (menu);
3183 offset = menu->scroll_offset + step;
3185 get_arrows_border (menu, &arrow_border);
3187 double_arrows = get_double_arrows (menu);
3189 /* If we scroll upward and the non-visible top part
3190 * is smaller than the scroll arrow it would be
3191 * pretty stupid to show the arrow and taking more
3192 * screen space than just scrolling to the top.
3195 if ((step < 0) && (offset < arrow_border.top))
3198 /* Don't scroll over the top if we weren't before: */
3199 if ((menu->scroll_offset >= 0) && (offset < 0))
3202 gdk_drawable_get_size (widget->window, &view_width, &view_height);
3204 if (menu->scroll_offset == 0 &&
3205 view_height >= widget->requisition.height)
3208 /* Don't scroll past the bottom if we weren't before: */
3209 if (menu->scroll_offset > 0)
3210 view_height -= arrow_border.top;
3212 /* When both arrows are always shown, reduce
3213 * view height even more.
3216 view_height -= arrow_border.bottom;
3218 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
3219 (offset + view_height > widget->requisition.height))
3220 offset = widget->requisition.height - view_height;
3222 if (offset != menu->scroll_offset)
3223 gtk_menu_scroll_to (menu, offset);
3227 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3228 gboolean touchscreen_mode)
3230 gboolean upper_visible;
3231 gboolean lower_visible;
3233 upper_visible = menu->upper_arrow_visible;
3234 lower_visible = menu->lower_arrow_visible;
3236 gtk_menu_scroll_by (menu, menu->scroll_step);
3238 if (touchscreen_mode &&
3239 (upper_visible != menu->upper_arrow_visible ||
3240 lower_visible != menu->lower_arrow_visible))
3242 /* We are about to hide a scroll arrow while the mouse is pressed,
3243 * this would cause the uncovered menu item to be activated on button
3244 * release. Therefore we need to ignore button release here
3246 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3247 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3252 gtk_menu_scroll_timeout (gpointer data)
3255 gboolean touchscreen_mode;
3257 menu = GTK_MENU (data);
3259 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3260 "gtk-touchscreen-mode", &touchscreen_mode,
3263 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3269 gtk_menu_scroll_timeout_initial (gpointer data)
3273 gboolean touchscreen_mode;
3275 menu = GTK_MENU (data);
3277 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3278 "gtk-timeout-repeat", &timeout,
3279 "gtk-touchscreen-mode", &touchscreen_mode,
3282 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3284 gtk_menu_remove_scroll_timeout (menu);
3286 menu->timeout_id = gdk_threads_add_timeout (timeout,
3287 gtk_menu_scroll_timeout,
3294 gtk_menu_start_scrolling (GtkMenu *menu)
3297 gboolean touchscreen_mode;
3299 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3300 "gtk-timeout-repeat", &timeout,
3301 "gtk-touchscreen-mode", &touchscreen_mode,
3304 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3306 menu->timeout_id = gdk_threads_add_timeout (timeout,
3307 gtk_menu_scroll_timeout_initial,
3312 gtk_menu_scroll (GtkWidget *widget,
3313 GdkEventScroll *event)
3315 GtkMenu *menu = GTK_MENU (widget);
3317 switch (event->direction)
3319 case GDK_SCROLL_RIGHT:
3320 case GDK_SCROLL_DOWN:
3321 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3323 case GDK_SCROLL_LEFT:
3325 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3333 get_arrows_sensitive_area (GtkMenu *menu,
3334 GdkRectangle *upper,
3335 GdkRectangle *lower)
3339 guint vertical_padding;
3341 gint scroll_arrow_height;
3343 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3345 gtk_widget_style_get (GTK_WIDGET (menu),
3346 "vertical-padding", &vertical_padding,
3347 "scroll-arrow-vlength", &scroll_arrow_height,
3350 border = GTK_CONTAINER (menu)->border_width +
3351 GTK_WIDGET (menu)->style->ythickness + vertical_padding;
3353 gdk_window_get_position (GTK_WIDGET (menu)->window, &win_x, &win_y);
3359 upper->width = width;
3360 upper->height = scroll_arrow_height + border;
3366 lower->y = win_y + height - border - scroll_arrow_height;
3367 lower->width = width;
3368 lower->height = scroll_arrow_height + border;
3374 gtk_menu_handle_scrolling (GtkMenu *menu,
3380 GtkMenuShell *menu_shell;
3381 GtkMenuPrivate *priv;
3384 gboolean scroll_fast = FALSE;
3386 gboolean touchscreen_mode;
3388 priv = gtk_menu_get_private (menu);
3390 menu_shell = GTK_MENU_SHELL (menu);
3392 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3393 "gtk-touchscreen-mode", &touchscreen_mode,
3396 gdk_window_get_position (menu->toplevel->window, &top_x, &top_y);
3400 /* upper arrow handling */
3402 get_arrows_sensitive_area (menu, &rect, NULL);
3405 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3406 (x >= rect.x) && (x < rect.x + rect.width) &&
3407 (y >= rect.y) && (y < rect.y + rect.height))
3412 if (touchscreen_mode)
3413 menu->upper_arrow_prelight = in_arrow;
3415 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3417 gboolean arrow_pressed = FALSE;
3419 if (menu->upper_arrow_visible && !menu->tearoff_active)
3421 if (touchscreen_mode)
3423 if (enter && menu->upper_arrow_prelight)
3425 if (menu->timeout_id == 0)
3427 /* Deselect the active item so that
3428 * any submenus are popped down
3430 gtk_menu_shell_deselect (menu_shell);
3432 gtk_menu_remove_scroll_timeout (menu);
3433 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3437 /* Only do stuff on click. */
3438 gtk_menu_start_scrolling (menu);
3439 arrow_pressed = TRUE;
3444 arrow_pressed = TRUE;
3449 gtk_menu_stop_scrolling (menu);
3452 else /* !touchscreen_mode */
3454 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3456 if (enter && in_arrow &&
3457 (!menu->upper_arrow_prelight ||
3458 menu->scroll_fast != scroll_fast))
3460 menu->upper_arrow_prelight = TRUE;
3461 menu->scroll_fast = scroll_fast;
3463 /* Deselect the active item so that
3464 * any submenus are popped down
3466 gtk_menu_shell_deselect (menu_shell);
3468 gtk_menu_remove_scroll_timeout (menu);
3469 menu->scroll_step = scroll_fast ?
3470 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
3473 gdk_threads_add_timeout (scroll_fast ?
3474 MENU_SCROLL_TIMEOUT2 :
3475 MENU_SCROLL_TIMEOUT1,
3476 gtk_menu_scroll_timeout, menu);
3478 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
3480 gtk_menu_stop_scrolling (menu);
3485 /* gtk_menu_start_scrolling() might have hit the top of the
3486 * menu, so check if the button isn't insensitive before
3487 * changing it to something else.
3489 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3491 GtkStateType arrow_state = GTK_STATE_NORMAL;
3494 arrow_state = GTK_STATE_ACTIVE;
3495 else if (menu->upper_arrow_prelight)
3496 arrow_state = GTK_STATE_PRELIGHT;
3498 if (arrow_state != priv->upper_arrow_state)
3500 priv->upper_arrow_state = arrow_state;
3502 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3508 /* lower arrow handling */
3510 get_arrows_sensitive_area (menu, NULL, &rect);
3513 if (menu->lower_arrow_visible && !menu->tearoff_active &&
3514 (x >= rect.x) && (x < rect.x + rect.width) &&
3515 (y >= rect.y) && (y < rect.y + rect.height))
3520 if (touchscreen_mode)
3521 menu->lower_arrow_prelight = in_arrow;
3523 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3525 gboolean arrow_pressed = FALSE;
3527 if (menu->lower_arrow_visible && !menu->tearoff_active)
3529 if (touchscreen_mode)
3531 if (enter && menu->lower_arrow_prelight)
3533 if (menu->timeout_id == 0)
3535 /* Deselect the active item so that
3536 * any submenus are popped down
3538 gtk_menu_shell_deselect (menu_shell);
3540 gtk_menu_remove_scroll_timeout (menu);
3541 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
3545 /* Only do stuff on click. */
3546 gtk_menu_start_scrolling (menu);
3547 arrow_pressed = TRUE;
3552 arrow_pressed = TRUE;
3557 gtk_menu_stop_scrolling (menu);
3560 else /* !touchscreen_mode */
3562 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
3564 if (enter && in_arrow &&
3565 (!menu->lower_arrow_prelight ||
3566 menu->scroll_fast != scroll_fast))
3568 menu->lower_arrow_prelight = TRUE;
3569 menu->scroll_fast = scroll_fast;
3571 /* Deselect the active item so that
3572 * any submenus are popped down
3574 gtk_menu_shell_deselect (menu_shell);
3576 gtk_menu_remove_scroll_timeout (menu);
3577 menu->scroll_step = scroll_fast ?
3578 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
3581 gdk_threads_add_timeout (scroll_fast ?
3582 MENU_SCROLL_TIMEOUT2 :
3583 MENU_SCROLL_TIMEOUT1,
3584 gtk_menu_scroll_timeout, menu);
3586 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
3588 gtk_menu_stop_scrolling (menu);
3593 /* gtk_menu_start_scrolling() might have hit the bottom of the
3594 * menu, so check if the button isn't insensitive before
3595 * changing it to something else.
3597 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3599 GtkStateType arrow_state = GTK_STATE_NORMAL;
3602 arrow_state = GTK_STATE_ACTIVE;
3603 else if (menu->lower_arrow_prelight)
3604 arrow_state = GTK_STATE_PRELIGHT;
3606 if (arrow_state != priv->lower_arrow_state)
3608 priv->lower_arrow_state = arrow_state;
3610 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3618 gtk_menu_enter_notify (GtkWidget *widget,
3619 GdkEventCrossing *event)
3621 GtkWidget *menu_item;
3622 gboolean touchscreen_mode;
3624 g_object_get (gtk_widget_get_settings (widget),
3625 "gtk-touchscreen-mode", &touchscreen_mode,
3628 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3629 if (GTK_IS_MENU (widget))
3631 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
3633 if (!menu_shell->ignore_enter)
3634 gtk_menu_handle_scrolling (GTK_MENU (widget),
3635 event->x_root, event->y_root, TRUE, TRUE);
3638 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
3640 GtkWidget *menu = menu_item->parent;
3642 if (GTK_IS_MENU (menu))
3644 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
3645 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3647 if (priv->seen_item_enter)
3649 /* This is the second enter we see for an item
3650 * on this menu. This means a release should always
3653 menu_shell->activate_time = 0;
3655 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
3656 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
3658 if (definitely_within_item (menu_item, event->x, event->y))
3660 /* This is an actual user-enter (ie. not a pop-under)
3661 * In this case, the user must either have entered
3662 * sufficiently far enough into the item, or he must move
3663 * far enough away from the enter point. (see
3664 * gtk_menu_motion_notify())
3666 menu_shell->activate_time = 0;
3670 priv->seen_item_enter = TRUE;
3674 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
3675 * will not correspond to the event widget's parent. Check to see
3676 * if we are in the parent's navigation region.
3678 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
3679 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent),
3680 event->x_root, event->y_root))
3683 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
3687 gtk_menu_leave_notify (GtkWidget *widget,
3688 GdkEventCrossing *event)
3690 GtkMenuShell *menu_shell;
3692 GtkMenuItem *menu_item;
3693 GtkWidget *event_widget;
3695 menu = GTK_MENU (widget);
3696 menu_shell = GTK_MENU_SHELL (widget);
3698 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3701 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
3703 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3705 if (!GTK_IS_MENU_ITEM (event_widget))
3708 menu_item = GTK_MENU_ITEM (event_widget);
3710 /* Here we check to see if we're leaving an active menu item with a submenu,
3711 * in which case we enter submenu navigation mode.
3713 if (menu_shell->active_menu_item != NULL
3714 && menu_item->submenu != NULL
3715 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
3717 if (GTK_MENU_SHELL (menu_item->submenu)->active)
3719 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
3722 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
3724 /* We are leaving an active menu item with nonactive submenu.
3725 * Deselect it so we don't surprise the user with by popping
3726 * up a submenu _after_ he left the item.
3728 gtk_menu_shell_deselect (menu_shell);
3733 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
3737 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
3739 if (menu->navigation_region)
3741 gdk_region_destroy (menu->navigation_region);
3742 menu->navigation_region = NULL;
3744 if (menu->navigation_timeout)
3746 g_source_remove (menu->navigation_timeout);
3747 menu->navigation_timeout = 0;
3751 /* When the timeout is elapsed, the navigation region is destroyed
3752 * and the menuitem under the pointer (if any) is selected.
3755 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
3757 GtkMenu *menu = user_data;
3758 GdkWindow *child_window;
3760 gtk_menu_stop_navigating_submenu (menu);
3762 if (GTK_WIDGET_REALIZED (menu))
3764 child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
3768 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3770 send_event->crossing.window = g_object_ref (child_window);
3771 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
3772 send_event->crossing.send_event = TRUE;
3774 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
3776 gdk_event_free (send_event);
3784 gtk_menu_navigating_submenu (GtkMenu *menu,
3788 if (menu->navigation_region)
3790 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
3794 gtk_menu_stop_navigating_submenu (menu);
3801 #undef DRAW_STAY_UP_TRIANGLE
3803 #ifdef DRAW_STAY_UP_TRIANGLE
3806 draw_stay_up_triangle (GdkWindow *window,
3809 /* Draw ugly color all over the stay-up triangle */
3810 GdkColor ugly_color = { 0, 50000, 10000, 10000 };
3811 GdkGCValues gc_values;
3813 GdkRectangle clipbox;
3815 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
3816 ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
3817 gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
3818 gdk_gc_set_clip_region (ugly_gc, region);
3820 gdk_region_get_clipbox (region, &clipbox);
3822 gdk_draw_rectangle (window,
3825 clipbox.x, clipbox.y,
3826 clipbox.width, clipbox.height);
3828 g_object_unref (ugly_gc);
3833 flip_region (GdkRegion *region,
3838 GdkRectangle *rectangles;
3839 GdkRectangle clipbox;
3840 GdkRegion *new_region;
3843 new_region = gdk_region_new ();
3845 gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
3846 gdk_region_get_clipbox (region, &clipbox);
3848 for (i = 0; i < n_rectangles; ++i)
3850 GdkRectangle rect = rectangles[i];
3853 rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
3856 rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
3858 gdk_region_union_with_rect (new_region, &rect);
3861 g_free (rectangles);
3867 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
3868 GtkMenuItem *menu_item,
3869 GdkEventCrossing *event)
3871 gint submenu_left = 0;
3872 gint submenu_right = 0;
3873 gint submenu_top = 0;
3874 gint submenu_bottom = 0;
3878 GtkWidget *event_widget;
3880 g_return_if_fail (menu_item->submenu != NULL);
3881 g_return_if_fail (event != NULL);
3883 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3885 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
3886 gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
3888 submenu_right = submenu_left + width;
3889 submenu_bottom = submenu_top + height;
3891 gdk_drawable_get_size (event_widget->window, &width, &height);
3893 if (event->x >= 0 && event->x < width)
3896 gboolean flip_y = FALSE;
3897 gboolean flip_x = FALSE;
3899 gtk_menu_stop_navigating_submenu (menu);
3901 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
3904 point[0].x = event->x_root;
3905 point[1].x = submenu_left;
3910 point[0].x = event->x_root + 1;
3911 point[1].x = 2 * (event->x_root + 1) - submenu_right;
3919 point[0].y = event->y_root + 1;
3920 point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
3922 if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
3930 point[0].y = event->y_root;
3931 point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
3933 if (point[0].y >= submenu_bottom)
3937 point[2].x = point[1].x;
3938 point[2].y = point[0].y;
3940 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
3942 if (flip_x || flip_y)
3944 GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
3945 gdk_region_destroy (menu->navigation_region);
3946 menu->navigation_region = new_region;
3949 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3950 "gtk-menu-popdown-delay", &popdown_delay,
3953 menu->navigation_timeout = gdk_threads_add_timeout (popdown_delay,
3954 gtk_menu_stop_navigating_submenu_cb,
3957 #ifdef DRAW_STAY_UP_TRIANGLE
3958 draw_stay_up_triangle (gdk_get_default_root_window(),
3959 menu->navigation_region);
3965 gtk_menu_deactivate (GtkMenuShell *menu_shell)
3969 g_return_if_fail (GTK_IS_MENU (menu_shell));
3971 parent = menu_shell->parent_menu_shell;
3973 menu_shell->activate_time = 0;
3974 gtk_menu_popdown (GTK_MENU (menu_shell));
3977 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
3981 gtk_menu_position (GtkMenu *menu)
3984 GtkRequisition requisition;
3985 GtkMenuPrivate *private;
3990 GdkScreen *pointer_screen;
3991 GdkRectangle monitor;
3993 g_return_if_fail (GTK_IS_MENU (menu));
3995 widget = GTK_WIDGET (menu);
3997 screen = gtk_widget_get_screen (widget);
3998 gdk_display_get_pointer (gdk_screen_get_display (screen),
3999 &pointer_screen, &x, &y, NULL);
4001 /* We need the requisition to figure out the right place to
4002 * popup the menu. In fact, we always need to ask here, since
4003 * if a size_request was queued while we weren't popped up,
4004 * the requisition won't have been recomputed yet.
4006 gtk_widget_size_request (widget, &requisition);
4008 if (pointer_screen != screen)
4010 /* Pointer is on a different screen; roughly center the
4011 * menu on the screen. If someone was using multiscreen
4012 * + Xinerama together they'd probably want something
4013 * fancier; but that is likely to be vanishingly rare.
4015 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4016 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4019 private = gtk_menu_get_private (menu);
4020 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4022 private->initially_pushed_in = FALSE;
4024 /* Set the type hint here to allow custom position functions to set a different hint */
4025 if (!GTK_WIDGET_VISIBLE (menu->toplevel))
4026 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4028 if (menu->position_func)
4030 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
4031 menu->position_func_data);
4032 if (private->monitor_num < 0)
4033 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4035 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4039 gint space_left, space_right, space_above, space_below;
4042 gint xthickness = widget->style->xthickness;
4043 gint ythickness = widget->style->ythickness;
4044 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4046 /* The placement of popup menus horizontally works like this (with
4047 * RTL in parentheses)
4049 * - If there is enough room to the right (left) of the mouse cursor,
4050 * position the menu there.
4052 * - Otherwise, if if there is enough room to the left (right) of the
4053 * mouse cursor, position the menu there.
4055 * - Otherwise if the menu is smaller than the monitor, position it
4056 * on the side of the mouse cursor that has the most space available
4058 * - Otherwise (if there is simply not enough room for the menu on the
4059 * monitor), position it as far left (right) as possible.
4061 * Positioning in the vertical direction is similar: first try below
4062 * mouse cursor, then above.
4064 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4066 space_left = x - monitor.x;
4067 space_right = monitor.x + monitor.width - x - 1;
4068 space_above = y - monitor.y;
4069 space_below = monitor.y + monitor.height - y - 1;
4071 /* position horizontally */
4073 /* the amount of space we need to position the menu. Note the
4074 * menu is offset "xthickness" pixels
4076 needed_width = requisition.width - xthickness;
4078 if (needed_width <= space_left ||
4079 needed_width <= space_right)
4081 if ((rtl && needed_width <= space_left) ||
4082 (!rtl && needed_width > space_right))
4085 x = x + xthickness - requisition.width + 1;
4089 /* position right */
4093 /* x is clamped on-screen further down */
4095 else if (requisition.width <= monitor.width)
4097 /* the menu is too big to fit on either side of the mouse
4098 * cursor, but smaller than the monitor. Position it on
4099 * the side that has the most space
4101 if (space_left > space_right)
4109 x = monitor.x + monitor.width - requisition.width;
4112 else /* menu is simply too big for the monitor */
4117 x = monitor.x + monitor.width - requisition.width;
4126 /* Position vertically. The algorithm is the same as above, but
4127 * simpler because we don't have to take RTL into account.
4129 needed_height = requisition.height - ythickness;
4131 if (needed_height <= space_above ||
4132 needed_height <= space_below)
4134 if (needed_height <= space_below)
4137 y = y + ythickness - requisition.height + 1;
4139 y = CLAMP (y, monitor.y,
4140 monitor.y + monitor.height - requisition.height);
4142 else if (needed_height > space_below && needed_height > space_above)
4144 if (space_below >= space_above)
4145 y = monitor.y + monitor.height - requisition.height;
4157 if (private->initially_pushed_in)
4159 menu_height = GTK_WIDGET (menu)->requisition.height;
4161 if (y + menu_height > monitor.y + monitor.height)
4163 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4164 y = (monitor.y + monitor.height) - menu_height;
4169 scroll_offset += monitor.y - y;
4174 /* FIXME: should this be done in the various position_funcs ? */
4175 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4177 if (GTK_MENU_SHELL (menu)->active)
4179 private->have_position = TRUE;
4184 if (y + requisition.height > monitor.y + monitor.height)
4185 requisition.height = (monitor.y + monitor.height) - y;
4189 scroll_offset += monitor.y - y;
4190 requisition.height -= monitor.y - y;
4194 if (scroll_offset > 0)
4196 GtkBorder arrow_border;
4198 get_arrows_border (menu, &arrow_border);
4199 scroll_offset += arrow_border.top;
4202 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
4205 if (!GTK_MENU_SHELL (menu)->active)
4207 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4208 requisition.width, requisition.height);
4211 menu->scroll_offset = scroll_offset;
4215 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4217 if (menu->timeout_id)
4219 g_source_remove (menu->timeout_id);
4220 menu->timeout_id = 0;
4225 gtk_menu_stop_scrolling (GtkMenu *menu)
4227 gboolean touchscreen_mode;
4229 gtk_menu_remove_scroll_timeout (menu);
4231 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4232 "gtk-touchscreen-mode", &touchscreen_mode,
4235 if (!touchscreen_mode)
4237 menu->upper_arrow_prelight = FALSE;
4238 menu->lower_arrow_prelight = FALSE;
4243 gtk_menu_scroll_to (GtkMenu *menu,
4248 gint view_width, view_height;
4251 guint vertical_padding;
4252 guint horizontal_padding;
4253 gboolean double_arrows;
4254 GtkBorder arrow_border;
4256 widget = GTK_WIDGET (menu);
4258 if (menu->tearoff_active &&
4259 menu->tearoff_adjustment &&
4260 (menu->tearoff_adjustment->value != offset))
4262 menu->tearoff_adjustment->value =
4264 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4265 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4268 /* Move/resize the viewport according to arrows: */
4269 view_width = widget->allocation.width;
4270 view_height = widget->allocation.height;
4272 gtk_widget_style_get (GTK_WIDGET (menu),
4273 "vertical-padding", &vertical_padding,
4274 "horizontal-padding", &horizontal_padding,
4277 double_arrows = get_double_arrows (menu);
4279 border_width = GTK_CONTAINER (menu)->border_width;
4280 view_width -= (border_width + widget->style->xthickness + horizontal_padding) * 2;
4281 view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
4282 menu_height = widget->requisition.height -
4283 (border_width + widget->style->ythickness + vertical_padding) * 2;
4285 x = border_width + widget->style->xthickness + horizontal_padding;
4286 y = border_width + widget->style->ythickness + vertical_padding;
4288 if (double_arrows && !menu->tearoff_active)
4290 if (view_height < menu_height ||
4291 (offset > 0 && menu->scroll_offset > 0) ||
4292 (offset < 0 && menu->scroll_offset < 0))
4294 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4295 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4296 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4298 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4299 gtk_widget_queue_draw (GTK_WIDGET (menu));
4301 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4303 get_arrows_border (menu, &arrow_border);
4304 y += arrow_border.top;
4305 view_height -= arrow_border.top;
4306 view_height -= arrow_border.bottom;
4309 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4310 else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4311 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4312 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4314 if (offset >= menu_height - view_height)
4315 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4316 else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4317 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4318 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4320 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4321 (priv->lower_arrow_state != lower_arrow_previous_state))
4322 gtk_widget_queue_draw (GTK_WIDGET (menu));
4324 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4325 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4327 /* At the upper border, possibly remove timeout */
4328 if (menu->scroll_step < 0)
4330 gtk_menu_stop_scrolling (menu);
4331 gtk_widget_queue_draw (GTK_WIDGET (menu));
4335 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4336 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4338 /* At the lower border, possibly remove timeout */
4339 if (menu->scroll_step > 0)
4341 gtk_menu_stop_scrolling (menu);
4342 gtk_widget_queue_draw (GTK_WIDGET (menu));
4346 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4350 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4351 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4353 gtk_menu_stop_scrolling (menu);
4354 gtk_widget_queue_draw (GTK_WIDGET (menu));
4357 else if (!menu->tearoff_active)
4359 gboolean last_visible;
4361 last_visible = menu->upper_arrow_visible;
4362 menu->upper_arrow_visible = offset > 0;
4364 /* upper_arrow_visible may have changed, so requery the border */
4365 get_arrows_border (menu, &arrow_border);
4366 view_height -= arrow_border.top;
4368 if ((last_visible != menu->upper_arrow_visible) &&
4369 !menu->upper_arrow_visible)
4371 menu->upper_arrow_prelight = FALSE;
4373 /* If we hid the upper arrow, possibly remove timeout */
4374 if (menu->scroll_step < 0)
4376 gtk_menu_stop_scrolling (menu);
4377 gtk_widget_queue_draw (GTK_WIDGET (menu));
4381 last_visible = menu->lower_arrow_visible;
4382 menu->lower_arrow_visible = offset < menu_height - view_height;
4384 /* lower_arrow_visible may have changed, so requery the border */
4385 get_arrows_border (menu, &arrow_border);
4386 view_height -= arrow_border.bottom;
4388 if ((last_visible != menu->lower_arrow_visible) &&
4389 !menu->lower_arrow_visible)
4391 menu->lower_arrow_prelight = FALSE;
4393 /* If we hid the lower arrow, possibly remove timeout */
4394 if (menu->scroll_step > 0)
4396 gtk_menu_stop_scrolling (menu);
4397 gtk_widget_queue_draw (GTK_WIDGET (menu));
4401 y += arrow_border.top;
4404 /* Scroll the menu: */
4405 if (GTK_WIDGET_REALIZED (menu))
4406 gdk_window_move (menu->bin_window, 0, -offset);
4408 if (GTK_WIDGET_REALIZED (menu))
4409 gdk_window_move_resize (menu->view_window,
4415 menu->scroll_offset = offset;
4419 compute_child_offset (GtkMenu *menu,
4420 GtkWidget *menu_item,
4423 gboolean *is_last_child)
4425 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4426 gint item_top_attach;
4427 gint item_bottom_attach;
4428 gint child_offset = 0;
4431 get_effective_child_attach (menu_item, NULL, NULL,
4432 &item_top_attach, &item_bottom_attach);
4434 /* there is a possibility that we get called before _size_request, so
4435 * check the height table for safety.
4437 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4440 /* when we have a row with only invisible children, it's height will
4441 * be zero, so there's no need to check WIDGET_VISIBLE here
4443 for (i = 0; i < item_top_attach; i++)
4444 child_offset += priv->heights[i];
4447 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4449 *offset = child_offset;
4451 *height = priv->heights[item_top_attach];
4457 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4458 GtkWidget *menu_item)
4461 gint child_offset, child_height;
4465 gboolean last_child = 0;
4467 menu = GTK_MENU (menu_shell);
4469 /* We need to check if the selected item fully visible.
4470 * If not we need to scroll the menu so that it becomes fully
4474 if (compute_child_offset (menu, menu_item,
4475 &child_offset, &child_height, &last_child))
4477 guint vertical_padding;
4478 gboolean double_arrows;
4480 y = menu->scroll_offset;
4481 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
4483 gtk_widget_style_get (GTK_WIDGET (menu),
4484 "vertical-padding", &vertical_padding,
4487 double_arrows = get_double_arrows (menu);
4489 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
4491 if (child_offset < y)
4493 /* Ignore the enter event we might get if the pointer is on the menu
4495 menu_shell->ignore_enter = TRUE;
4496 gtk_menu_scroll_to (menu, child_offset);
4500 GtkBorder arrow_border;
4504 get_arrows_border (menu, &arrow_border);
4505 if (!menu->tearoff_active)
4506 arrow_height = arrow_border.top + arrow_border.bottom;
4508 if (child_offset + child_height > y + height - arrow_height)
4511 if ((!last_child && !menu->tearoff_active) || double_arrows)
4512 arrow_height += arrow_border.bottom;
4514 y = child_offset + child_height - height + arrow_height;
4515 if (((y > 0) && !menu->tearoff_active) || double_arrows)
4517 /* Need upper arrow */
4518 arrow_height += arrow_border.top;
4519 y = child_offset + child_height - height + arrow_height;
4521 /* Ignore the enter event we might get if the pointer is on the menu
4523 menu_shell->ignore_enter = TRUE;
4524 gtk_menu_scroll_to (menu, y);
4532 gtk_menu_select_item (GtkMenuShell *menu_shell,
4533 GtkWidget *menu_item)
4535 GtkMenu *menu = GTK_MENU (menu_shell);
4537 if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
4538 gtk_menu_scroll_item_visible (menu_shell, menu_item);
4540 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
4544 /* Reparent the menu, taking care of the refcounting
4546 * If unrealize is true we force a unrealize while reparenting the parent.
4547 * This can help eliminate flicker in some cases.
4549 * What happens is that when the menu is unrealized and then re-realized,
4550 * the allocations are as follows:
4552 * parent - 1x1 at (0,0)
4553 * child1 - 100x20 at (0,0)
4554 * child2 - 100x20 at (0,20)
4555 * child3 - 100x20 at (0,40)
4557 * That is, the parent is small but the children are full sized. Then,
4558 * when the queued_resize gets processed, the parent gets resized to
4561 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
4562 * contains the following logic:
4564 * - if a move or resize operation on a window would change the clip
4565 * region on the children, then before the window is resized
4566 * the background for children is temporarily set to None, the
4567 * move/resize done, and the background for the children restored.
4569 * So, at the point where the parent is resized to final size, the
4570 * background for the children is temporarily None, and thus they
4571 * are not cleared to the background color and the previous background
4572 * (the image of the menu) is left in place.
4575 gtk_menu_reparent (GtkMenu *menu,
4576 GtkWidget *new_parent,
4579 GtkObject *object = GTK_OBJECT (menu);
4580 GtkWidget *widget = GTK_WIDGET (menu);
4581 gboolean was_floating = g_object_is_floating (object);
4583 g_object_ref_sink (object);
4587 g_object_ref (object);
4588 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
4589 gtk_container_add (GTK_CONTAINER (new_parent), widget);
4590 g_object_unref (object);
4593 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
4596 g_object_force_floating (G_OBJECT (object));
4598 g_object_unref (object);
4602 gtk_menu_show_all (GtkWidget *widget)
4604 /* Show children, but not self. */
4605 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
4610 gtk_menu_hide_all (GtkWidget *widget)
4612 /* Hide children, but not self. */
4613 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
4617 * gtk_menu_set_screen:
4618 * @menu: a #GtkMenu.
4619 * @screen: a #GdkScreen, or %NULL if the screen should be
4620 * determined by the widget the menu is attached to.
4622 * Sets the #GdkScreen on which the menu will be displayed.
4627 gtk_menu_set_screen (GtkMenu *menu,
4630 g_return_if_fail (GTK_IS_MENU (menu));
4631 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
4633 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
4637 menu_change_screen (menu, screen);
4641 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
4643 attach_widget_screen_changed (attach_widget, NULL, menu);
4649 * @menu: a #GtkMenu.
4650 * @child: a #GtkMenuItem.
4651 * @left_attach: The column number to attach the left side of the item to.
4652 * @right_attach: The column number to attach the right side of the item to.
4653 * @top_attach: The row number to attach the top of the item to.
4654 * @bottom_attach: The row number to attach the bottom of the item to.
4656 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
4657 * an item will occupy is specified by @left_attach, @right_attach,
4658 * @top_attach and @bottom_attach. These each represent the leftmost,
4659 * rightmost, uppermost and lower column and row numbers of the table.
4660 * (Columns and rows are indexed from zero).
4662 * Note that this function is not related to gtk_menu_detach().
4667 gtk_menu_attach (GtkMenu *menu,
4672 guint bottom_attach)
4674 GtkMenuShell *menu_shell;
4676 g_return_if_fail (GTK_IS_MENU (menu));
4677 g_return_if_fail (GTK_IS_MENU_ITEM (child));
4678 g_return_if_fail (child->parent == NULL ||
4679 child->parent == GTK_WIDGET (menu));
4680 g_return_if_fail (left_attach < right_attach);
4681 g_return_if_fail (top_attach < bottom_attach);
4683 menu_shell = GTK_MENU_SHELL (menu);
4687 AttachInfo *ai = get_attach_info (child);
4689 ai->left_attach = left_attach;
4690 ai->right_attach = right_attach;
4691 ai->top_attach = top_attach;
4692 ai->bottom_attach = bottom_attach;
4694 menu_shell->children = g_list_append (menu_shell->children, child);
4696 gtk_widget_set_parent (child, GTK_WIDGET (menu));
4698 menu_queue_resize (menu);
4702 gtk_container_child_set (GTK_CONTAINER (child->parent), child,
4703 "left-attach", left_attach,
4704 "right-attach", right_attach,
4705 "top-attach", top_attach,
4706 "bottom-attach", bottom_attach,
4712 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
4716 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
4717 "gtk-menu-popup-delay", &popup_delay,
4724 find_child_containing (GtkMenuShell *menu_shell,
4732 /* find a child which includes the area given by
4733 * left, right, top, bottom.
4736 for (list = menu_shell->children; list; list = list->next)
4740 if (!_gtk_menu_item_is_selectable (list->data))
4743 get_effective_child_attach (list->data, &l, &r, &t, &b);
4745 if (l <= left && right <= r
4746 && t <= top && bottom <= b)
4747 return GTK_WIDGET (list->data);
4754 gtk_menu_move_current (GtkMenuShell *menu_shell,
4755 GtkMenuDirectionType direction)
4757 GtkMenu *menu = GTK_MENU (menu_shell);
4760 GtkWidget *match = NULL;
4762 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
4766 case GTK_MENU_DIR_CHILD:
4767 direction = GTK_MENU_DIR_PARENT;
4769 case GTK_MENU_DIR_PARENT:
4770 direction = GTK_MENU_DIR_CHILD;
4776 /* use special table menu key bindings */
4777 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
4779 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
4781 if (direction == GTK_MENU_DIR_NEXT)
4783 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
4785 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
4793 for (i = 0; i < t; i++)
4795 match = find_child_containing (menu_shell,
4796 l, l + 1, i, i + 1);
4802 else if (direction == GTK_MENU_DIR_PREV)
4804 for (i = t; i > 0; i--)
4806 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
4814 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
4816 match = find_child_containing (menu_shell,
4817 l, l + 1, i - 1, i);
4823 else if (direction == GTK_MENU_DIR_PARENT)
4825 /* we go one left if possible */
4827 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
4831 GtkWidget *parent = menu_shell->parent_menu_shell;
4834 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
4835 match = menu_shell->active_menu_item;
4838 else if (direction == GTK_MENU_DIR_CHILD)
4840 /* we go one right if possible */
4841 if (r < gtk_menu_get_n_columns (menu))
4842 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
4846 GtkWidget *parent = menu_shell->parent_menu_shell;
4848 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
4850 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
4851 match = menu_shell->active_menu_item;
4857 gtk_menu_shell_select_item (menu_shell, match);
4862 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
4866 get_visible_size (GtkMenu *menu)
4868 GtkWidget *widget = GTK_WIDGET (menu);
4869 GtkContainer *container = GTK_CONTAINER (menu);
4871 gint menu_height = (widget->allocation.height
4872 - 2 * (container->border_width
4873 + widget->style->ythickness));
4875 if (!menu->tearoff_active)
4877 GtkBorder arrow_border;
4879 get_arrows_border (menu, &arrow_border);
4880 menu_height -= arrow_border.top;
4881 menu_height -= arrow_border.bottom;
4887 /* Find the sensitive on-screen child containing @y, or if none,
4888 * the nearest selectable onscreen child. (%NULL if none)
4891 child_at (GtkMenu *menu,
4894 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4895 GtkWidget *child = NULL;
4896 gint child_offset = 0;
4899 gint lower, upper; /* Onscreen bounds */
4901 menu_height = get_visible_size (menu);
4902 lower = menu->scroll_offset;
4903 upper = menu->scroll_offset + menu_height;
4905 for (children = menu_shell->children; children; children = children->next)
4907 if (GTK_WIDGET_VISIBLE (children->data))
4909 GtkRequisition child_requisition;
4911 gtk_widget_size_request (children->data, &child_requisition);
4913 if (_gtk_menu_item_is_selectable (children->data) &&
4914 child_offset >= lower &&
4915 child_offset + child_requisition.height <= upper)
4917 child = children->data;
4919 if (child_offset + child_requisition.height > y &&
4920 !GTK_IS_TEAROFF_MENU_ITEM (child))
4924 child_offset += child_requisition.height;
4932 get_menu_height (GtkMenu *menu)
4935 GtkWidget *widget = GTK_WIDGET (menu);
4937 height = widget->requisition.height;
4938 height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
4940 if (!menu->tearoff_active)
4942 GtkBorder arrow_border;
4944 get_arrows_border (menu, &arrow_border);
4945 height -= arrow_border.top;
4946 height -= arrow_border.bottom;
4953 gtk_menu_real_move_scroll (GtkMenu *menu,
4956 gint page_size = get_visible_size (menu);
4957 gint end_position = get_menu_height (menu);
4958 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4962 case GTK_SCROLL_PAGE_UP:
4963 case GTK_SCROLL_PAGE_DOWN:
4967 gint child_offset = 0;
4968 gboolean old_upper_arrow_visible;
4971 if (type == GTK_SCROLL_PAGE_UP)
4976 if (menu_shell->active_menu_item)
4980 compute_child_offset (menu, menu_shell->active_menu_item,
4981 &child_offset, &child_height, NULL);
4982 child_offset += child_height / 2;
4985 menu_shell->ignore_enter = TRUE;
4986 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4987 old_offset = menu->scroll_offset;
4989 new_offset = menu->scroll_offset + step;
4990 new_offset = CLAMP (new_offset, 0, end_position - page_size);
4992 gtk_menu_scroll_to (menu, new_offset);
4994 if (menu_shell->active_menu_item)
4996 GtkWidget *new_child;
4997 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4998 GtkBorder arrow_border;
5000 get_arrows_border (menu, &arrow_border);
5002 if (menu->scroll_offset != old_offset)
5003 step = menu->scroll_offset - old_offset;
5005 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5007 new_child = child_at (menu, child_offset + step);
5009 gtk_menu_shell_select_item (menu_shell, new_child);
5013 case GTK_SCROLL_START:
5014 /* Ignore the enter event we might get if the pointer is on the menu
5016 menu_shell->ignore_enter = TRUE;
5017 gtk_menu_scroll_to (menu, 0);
5018 gtk_menu_shell_select_first (menu_shell, TRUE);
5020 case GTK_SCROLL_END:
5021 /* Ignore the enter event we might get if the pointer is on the menu
5023 menu_shell->ignore_enter = TRUE;
5024 gtk_menu_scroll_to (menu, end_position - page_size);
5025 _gtk_menu_shell_select_last (menu_shell, TRUE);
5034 * gtk_menu_set_monitor:
5036 * @monitor_num: the number of the monitor on which the menu should
5039 * Informs GTK+ on which monitor a menu should be popped up.
5040 * See gdk_screen_get_monitor_geometry().
5042 * This function should be called from a #GtkMenuPositionFunc if the
5043 * menu should not appear on the same monitor as the pointer. This
5044 * information can't be reliably inferred from the coordinates returned
5045 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
5046 * may extend beyond the monitor boundaries or even the screen boundaries.
5051 gtk_menu_set_monitor (GtkMenu *menu,
5054 GtkMenuPrivate *priv;
5055 g_return_if_fail (GTK_IS_MENU (menu));
5057 priv = gtk_menu_get_private (menu);
5059 priv->monitor_num = monitor_num;
5063 * gtk_menu_get_monitor:
5066 * Retrieves the number of the monitor on which to show the menu.
5068 * Returns: the number of the monitor on which the menu should
5069 * be popped up or -1, if no monitor has been set
5074 gtk_menu_get_monitor (GtkMenu *menu)
5076 GtkMenuPrivate *priv;
5077 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5079 priv = gtk_menu_get_private (menu);
5081 return priv->monitor_num;
5085 * gtk_menu_get_for_attach_widget:
5086 * @widget: a #GtkWidget
5088 * Returns a list of the menus which are attached to this widget.
5089 * This list is owned by GTK+ and must not be modified.
5091 * Return value: the list of menus attached to his widget.
5096 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5100 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5102 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5107 gtk_menu_grab_notify (GtkWidget *widget,
5108 gboolean was_grabbed)
5110 GtkWidget *toplevel;
5111 GtkWindowGroup *group;
5114 toplevel = gtk_widget_get_toplevel (widget);
5115 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5116 grab = _gtk_window_group_get_current_grab (group);
5120 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
5121 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5125 #define __GTK_MENU_C__
5126 #include "gtkaliasdef.c"