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 g_signal_new_class_handler (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.
1788 * Returns: the accelerator path set on the menu.
1793 gtk_menu_get_accel_path (GtkMenu *menu)
1795 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1797 return menu->accel_path;
1802 gboolean group_changed;
1806 refresh_accel_paths_foreach (GtkWidget *widget,
1809 AccelPropagation *prop = data;
1811 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1812 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1813 prop->menu->accel_path,
1814 prop->menu->accel_group,
1815 prop->group_changed);
1819 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1820 gboolean group_changed)
1822 g_return_if_fail (GTK_IS_MENU (menu));
1824 if (menu->accel_path && menu->accel_group)
1826 AccelPropagation prop;
1829 prop.group_changed = group_changed;
1830 gtk_container_foreach (GTK_CONTAINER (menu),
1831 refresh_accel_paths_foreach,
1837 gtk_menu_reposition (GtkMenu *menu)
1839 g_return_if_fail (GTK_IS_MENU (menu));
1841 if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1842 gtk_menu_position (menu);
1846 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1849 g_return_if_fail (GTK_IS_MENU (menu));
1851 if (adjustment->value != menu->scroll_offset)
1852 gtk_menu_scroll_to (menu, adjustment->value);
1856 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1859 GdkGeometry geometry_hints;
1861 if (!menu->tearoff_window)
1864 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1866 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1867 width += menu->tearoff_scrollbar->requisition.width;
1870 geometry_hints.min_width = width;
1871 geometry_hints.max_width = width;
1873 geometry_hints.min_height = 0;
1874 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1876 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1879 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1883 gtk_menu_update_title (GtkMenu *menu)
1885 if (menu->tearoff_window)
1888 GtkWidget *attach_widget;
1890 title = gtk_menu_get_title (menu);
1893 attach_widget = gtk_menu_get_attach_widget (menu);
1894 if (GTK_IS_MENU_ITEM (attach_widget))
1896 GtkWidget *child = GTK_BIN (attach_widget)->child;
1897 if (GTK_IS_LABEL (child))
1898 title = gtk_label_get_text (GTK_LABEL (child));
1903 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1908 gtk_menu_get_toplevel (GtkWidget *menu)
1910 GtkWidget *attach, *toplevel;
1912 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
1914 if (GTK_IS_MENU_ITEM (attach))
1915 attach = attach->parent;
1917 if (GTK_IS_MENU (attach))
1918 return gtk_menu_get_toplevel (attach);
1919 else if (GTK_IS_WIDGET (attach))
1921 toplevel = gtk_widget_get_toplevel (attach);
1922 if (GTK_WIDGET_TOPLEVEL (toplevel))
1930 tearoff_window_destroyed (GtkWidget *widget,
1933 gtk_menu_set_tearoff_state (menu, FALSE);
1937 gtk_menu_set_tearoff_state (GtkMenu *menu,
1942 g_return_if_fail (GTK_IS_MENU (menu));
1944 if (menu->torn_off != torn_off)
1946 menu->torn_off = torn_off;
1947 menu->tearoff_active = torn_off;
1951 if (GTK_WIDGET_VISIBLE (menu))
1952 gtk_menu_popdown (menu);
1954 if (!menu->tearoff_window)
1956 GtkWidget *toplevel;
1958 menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
1959 "type", GTK_WINDOW_TOPLEVEL,
1960 "screen", gtk_widget_get_screen (menu->toplevel),
1961 "app-paintable", TRUE,
1964 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1965 GDK_WINDOW_TYPE_HINT_MENU);
1966 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1967 g_signal_connect (menu->tearoff_window, "destroy",
1968 G_CALLBACK (tearoff_window_destroyed), menu);
1969 g_signal_connect (menu->tearoff_window, "event",
1970 G_CALLBACK (gtk_menu_window_event), menu);
1972 gtk_menu_update_title (menu);
1974 gtk_widget_realize (menu->tearoff_window);
1976 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
1977 if (toplevel != NULL)
1978 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
1979 GTK_WINDOW (toplevel));
1981 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1982 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1984 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
1985 menu->tearoff_adjustment =
1986 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1988 GTK_WIDGET (menu)->requisition.height,
1992 g_object_connect (menu->tearoff_adjustment,
1993 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1995 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1997 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1998 menu->tearoff_scrollbar,
2001 if (menu->tearoff_adjustment->upper > height)
2002 gtk_widget_show (menu->tearoff_scrollbar);
2004 gtk_widget_show (menu->tearoff_hbox);
2007 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
2009 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
2011 /* Update menu->requisition
2013 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
2015 gtk_menu_set_tearoff_hints (menu, width);
2017 gtk_widget_realize (menu->tearoff_window);
2018 gtk_menu_position (menu);
2020 gtk_widget_show (GTK_WIDGET (menu));
2021 gtk_widget_show (menu->tearoff_window);
2023 gtk_menu_scroll_to (menu, 0);
2028 gtk_widget_hide (GTK_WIDGET (menu));
2029 gtk_widget_hide (menu->tearoff_window);
2030 if (GTK_IS_CONTAINER (menu->toplevel))
2031 gtk_menu_reparent (menu, menu->toplevel, FALSE);
2032 gtk_widget_destroy (menu->tearoff_window);
2034 menu->tearoff_window = NULL;
2035 menu->tearoff_hbox = NULL;
2036 menu->tearoff_scrollbar = NULL;
2037 menu->tearoff_adjustment = NULL;
2040 g_object_notify (G_OBJECT (menu), "tearoff-state");
2045 * gtk_menu_get_tearoff_state:
2048 * Returns whether the menu is torn off. See
2049 * gtk_menu_set_tearoff_state ().
2051 * Return value: %TRUE if the menu is currently torn off.
2054 gtk_menu_get_tearoff_state (GtkMenu *menu)
2056 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2058 return menu->torn_off;
2062 * gtk_menu_set_title:
2064 * @title: a string containing the title for the menu.
2066 * Sets the title string for the menu. The title is displayed when the menu
2067 * is shown as a tearoff menu. If @title is %NULL, the menu will see if it is
2068 * attached to a parent menu item, and if so it will try to use the same text as
2069 * that menu item's label.
2072 gtk_menu_set_title (GtkMenu *menu,
2075 GtkMenuPrivate *priv;
2078 g_return_if_fail (GTK_IS_MENU (menu));
2080 priv = gtk_menu_get_private (menu);
2082 old_title = priv->title;
2083 priv->title = g_strdup (title);
2086 gtk_menu_update_title (menu);
2087 g_object_notify (G_OBJECT (menu), "tearoff-title");
2091 * gtk_menu_get_title:
2094 * Returns the title of the menu. See gtk_menu_set_title().
2096 * Return value: the title of the menu, or %NULL if the menu has no
2097 * title set on it. This string is owned by the widget and should
2098 * not be modified or freed.
2100 G_CONST_RETURN gchar *
2101 gtk_menu_get_title (GtkMenu *menu)
2103 GtkMenuPrivate *priv;
2105 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2107 priv = gtk_menu_get_private (menu);
2113 gtk_menu_reorder_child (GtkMenu *menu,
2117 GtkMenuShell *menu_shell;
2119 g_return_if_fail (GTK_IS_MENU (menu));
2120 g_return_if_fail (GTK_IS_MENU_ITEM (child));
2122 menu_shell = GTK_MENU_SHELL (menu);
2124 if (g_list_find (menu_shell->children, child))
2126 menu_shell->children = g_list_remove (menu_shell->children, child);
2127 menu_shell->children = g_list_insert (menu_shell->children, child, position);
2129 menu_queue_resize (menu);
2134 gtk_menu_style_set (GtkWidget *widget,
2135 GtkStyle *previous_style)
2137 if (GTK_WIDGET_REALIZED (widget))
2139 GtkMenu *menu = GTK_MENU (widget);
2141 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2142 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2143 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2148 get_arrows_border (GtkMenu *menu, GtkBorder *border)
2150 guint scroll_arrow_height;
2152 gtk_widget_style_get (GTK_WIDGET (menu),
2153 "scroll-arrow-vlength", &scroll_arrow_height,
2156 border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2157 border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2159 border->left = border->right = 0;
2163 gtk_menu_realize (GtkWidget *widget)
2165 GdkWindowAttr attributes;
2166 gint attributes_mask;
2171 guint vertical_padding;
2172 guint horizontal_padding;
2173 GtkBorder arrow_border;
2175 g_return_if_fail (GTK_IS_MENU (widget));
2177 menu = GTK_MENU (widget);
2179 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
2181 attributes.window_type = GDK_WINDOW_CHILD;
2182 attributes.x = widget->allocation.x;
2183 attributes.y = widget->allocation.y;
2184 attributes.width = widget->allocation.width;
2185 attributes.height = widget->allocation.height;
2186 attributes.wclass = GDK_INPUT_OUTPUT;
2187 attributes.visual = gtk_widget_get_visual (widget);
2188 attributes.colormap = gtk_widget_get_colormap (widget);
2190 attributes.event_mask = gtk_widget_get_events (widget);
2192 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2193 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2195 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2196 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
2197 gdk_window_set_user_data (widget->window, widget);
2199 border_width = GTK_CONTAINER (widget)->border_width;
2201 gtk_widget_style_get (GTK_WIDGET (menu),
2202 "vertical-padding", &vertical_padding,
2203 "horizontal-padding", &horizontal_padding,
2206 attributes.x = border_width + widget->style->xthickness + horizontal_padding;
2207 attributes.y = border_width + widget->style->ythickness + vertical_padding;
2208 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
2209 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
2211 get_arrows_border (menu, &arrow_border);
2212 attributes.y += arrow_border.top;
2213 attributes.height -= arrow_border.top;
2214 attributes.height -= arrow_border.bottom;
2216 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
2217 gdk_window_set_user_data (menu->view_window, menu);
2221 attributes.width = MAX (1, widget->allocation.width - (border_width + widget->style->xthickness + horizontal_padding) * 2);
2222 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
2224 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
2225 gdk_window_set_user_data (menu->bin_window, menu);
2227 children = GTK_MENU_SHELL (menu)->children;
2230 child = children->data;
2231 children = children->next;
2233 gtk_widget_set_parent_window (child, menu->bin_window);
2236 widget->style = gtk_style_attach (widget->style, widget->window);
2237 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2238 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2239 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2241 if (GTK_MENU_SHELL (widget)->active_menu_item)
2242 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2243 GTK_MENU_SHELL (widget)->active_menu_item);
2245 gdk_window_show (menu->bin_window);
2246 gdk_window_show (menu->view_window);
2250 gtk_menu_focus (GtkWidget *widget,
2251 GtkDirectionType direction)
2254 * A menu or its menu items cannot have focus
2259 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2262 menu_grab_transfer_window_get (GtkMenu *menu)
2264 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2267 GdkWindowAttr attributes;
2268 gint attributes_mask;
2270 attributes.x = -100;
2271 attributes.y = -100;
2272 attributes.width = 10;
2273 attributes.height = 10;
2274 attributes.window_type = GDK_WINDOW_TEMP;
2275 attributes.wclass = GDK_INPUT_ONLY;
2276 attributes.override_redirect = TRUE;
2277 attributes.event_mask = 0;
2279 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2281 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2282 &attributes, attributes_mask);
2283 gdk_window_set_user_data (window, menu);
2285 gdk_window_show (window);
2287 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2294 menu_grab_transfer_window_destroy (GtkMenu *menu)
2296 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2299 gdk_window_set_user_data (window, NULL);
2300 gdk_window_destroy (window);
2301 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2306 gtk_menu_unrealize (GtkWidget *widget)
2308 GtkMenu *menu = GTK_MENU (widget);
2310 menu_grab_transfer_window_destroy (menu);
2312 gdk_window_set_user_data (menu->view_window, NULL);
2313 gdk_window_destroy (menu->view_window);
2314 menu->view_window = NULL;
2316 gdk_window_set_user_data (menu->bin_window, NULL);
2317 gdk_window_destroy (menu->bin_window);
2318 menu->bin_window = NULL;
2320 (* GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize) (widget);
2324 gtk_menu_size_request (GtkWidget *widget,
2325 GtkRequisition *requisition)
2329 GtkMenuShell *menu_shell;
2332 guint max_toggle_size;
2333 guint max_accel_width;
2334 guint vertical_padding;
2335 guint horizontal_padding;
2336 GtkRequisition child_requisition;
2337 GtkMenuPrivate *priv;
2339 g_return_if_fail (GTK_IS_MENU (widget));
2340 g_return_if_fail (requisition != NULL);
2342 menu = GTK_MENU (widget);
2343 menu_shell = GTK_MENU_SHELL (widget);
2344 priv = gtk_menu_get_private (menu);
2346 requisition->width = 0;
2347 requisition->height = 0;
2349 max_toggle_size = 0;
2350 max_accel_width = 0;
2352 g_free (priv->heights);
2353 priv->heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2354 priv->heights_length = gtk_menu_get_n_rows (menu);
2356 children = menu_shell->children;
2363 child = children->data;
2364 children = children->next;
2366 if (! GTK_WIDGET_VISIBLE (child))
2369 get_effective_child_attach (child, &l, &r, &t, &b);
2371 /* It's important to size_request the child
2372 * before doing the toggle size request, in
2373 * case the toggle size request depends on the size
2374 * request of a child of the child (e.g. for ImageMenuItem)
2377 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
2378 gtk_widget_size_request (child, &child_requisition);
2380 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2381 max_toggle_size = MAX (max_toggle_size, toggle_size);
2382 max_accel_width = MAX (max_accel_width,
2383 GTK_MENU_ITEM (child)->accelerator_width);
2385 part = child_requisition.width / (r - l);
2386 requisition->width = MAX (requisition->width, part);
2388 part = MAX (child_requisition.height, toggle_size) / (b - t);
2389 priv->heights[t] = MAX (priv->heights[t], part);
2392 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2393 requisition->height += priv->heights[i];
2395 requisition->width += max_toggle_size + max_accel_width;
2396 requisition->width *= gtk_menu_get_n_columns (menu);
2398 gtk_widget_style_get (GTK_WIDGET (menu),
2399 "vertical-padding", &vertical_padding,
2400 "horizontal-padding", &horizontal_padding,
2403 requisition->width += (GTK_CONTAINER (menu)->border_width + horizontal_padding +
2404 widget->style->xthickness) * 2;
2405 requisition->height += (GTK_CONTAINER (menu)->border_width + vertical_padding +
2406 widget->style->ythickness) * 2;
2408 menu->toggle_size = max_toggle_size;
2410 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2412 if (menu->tearoff_active)
2413 gtk_menu_set_tearoff_hints (menu, requisition->width);
2417 gtk_menu_size_allocate (GtkWidget *widget,
2418 GtkAllocation *allocation)
2421 GtkMenuShell *menu_shell;
2423 GtkAllocation child_allocation;
2424 GtkRequisition child_requisition;
2425 GtkMenuPrivate *priv;
2429 guint vertical_padding;
2430 guint horizontal_padding;
2432 g_return_if_fail (GTK_IS_MENU (widget));
2433 g_return_if_fail (allocation != NULL);
2435 menu = GTK_MENU (widget);
2436 menu_shell = GTK_MENU_SHELL (widget);
2437 priv = gtk_menu_get_private (menu);
2439 widget->allocation = *allocation;
2440 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2442 gtk_widget_style_get (GTK_WIDGET (menu),
2443 "vertical-padding", &vertical_padding,
2444 "horizontal-padding", &horizontal_padding,
2447 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness + horizontal_padding;
2448 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness + vertical_padding;
2450 width = MAX (1, allocation->width - x * 2);
2451 height = MAX (1, allocation->height - y * 2);
2453 child_requisition.width -= x * 2;
2454 child_requisition.height -= y * 2;
2456 if (menu_shell->active)
2457 gtk_menu_scroll_to (menu, menu->scroll_offset);
2459 if (!menu->tearoff_active)
2461 GtkBorder arrow_border;
2463 get_arrows_border (menu, &arrow_border);
2464 y += arrow_border.top;
2465 height -= arrow_border.top;
2466 height -= arrow_border.bottom;
2469 if (GTK_WIDGET_REALIZED (widget))
2471 gdk_window_move_resize (widget->window,
2472 allocation->x, allocation->y,
2473 allocation->width, allocation->height);
2475 gdk_window_move_resize (menu->view_window,
2482 if (menu_shell->children)
2484 gint base_width = width / gtk_menu_get_n_columns (menu);
2486 children = menu_shell->children;
2489 child = children->data;
2490 children = children->next;
2492 if (GTK_WIDGET_VISIBLE (child))
2497 get_effective_child_attach (child, &l, &r, &t, &b);
2499 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2502 tmp = gtk_menu_get_n_columns (menu) - l;
2503 l = gtk_menu_get_n_columns (menu) - r;
2507 child_allocation.width = (r - l) * base_width;
2508 child_allocation.height = 0;
2509 child_allocation.x = l * base_width;
2510 child_allocation.y = 0;
2512 for (i = 0; i < b; i++)
2515 child_allocation.y += priv->heights[i];
2517 child_allocation.height += priv->heights[i];
2520 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2523 gtk_widget_size_allocate (child, &child_allocation);
2524 gtk_widget_queue_draw (child);
2528 /* Resize the item window */
2529 if (GTK_WIDGET_REALIZED (widget))
2535 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2536 height += priv->heights[i];
2538 width = gtk_menu_get_n_columns (menu) * base_width;
2539 gdk_window_resize (menu->bin_window, width, height);
2542 if (menu->tearoff_active)
2544 if (allocation->height >= widget->requisition.height)
2546 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2548 gtk_widget_hide (menu->tearoff_scrollbar);
2549 gtk_menu_set_tearoff_hints (menu, allocation->width);
2551 gtk_menu_scroll_to (menu, 0);
2556 menu->tearoff_adjustment->upper = widget->requisition.height;
2557 menu->tearoff_adjustment->page_size = allocation->height;
2559 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2560 menu->tearoff_adjustment->upper)
2563 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2566 gtk_menu_scroll_to (menu, value);
2569 gtk_adjustment_changed (menu->tearoff_adjustment);
2571 if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2573 gtk_widget_show (menu->tearoff_scrollbar);
2574 gtk_menu_set_tearoff_hints (menu, allocation->width);
2582 get_arrows_visible_area (GtkMenu *menu,
2583 GdkRectangle *border,
2584 GdkRectangle *upper,
2585 GdkRectangle *lower,
2588 GtkWidget *widget = GTK_WIDGET (menu);
2589 guint vertical_padding;
2590 guint horizontal_padding;
2591 gint scroll_arrow_height;
2593 gtk_widget_style_get (widget,
2594 "vertical-padding", &vertical_padding,
2595 "horizontal-padding", &horizontal_padding,
2596 "scroll-arrow-vlength", &scroll_arrow_height,
2599 border->x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness + horizontal_padding;
2600 border->y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
2601 gdk_drawable_get_size (widget->window, &border->width, &border->height);
2603 upper->x = border->x;
2604 upper->y = border->y;
2605 upper->width = border->width - 2 * border->x;
2606 upper->height = scroll_arrow_height;
2608 lower->x = border->x;
2609 lower->y = border->height - border->y - scroll_arrow_height;
2610 lower->width = border->width - 2 * border->x;
2611 lower->height = scroll_arrow_height;
2613 *arrow_space = scroll_arrow_height - 2 * widget->style->ythickness;
2617 gtk_menu_paint (GtkWidget *widget,
2618 GdkEventExpose *event)
2621 GtkMenuPrivate *priv;
2622 GdkRectangle border;
2627 g_return_if_fail (GTK_IS_MENU (widget));
2629 menu = GTK_MENU (widget);
2630 priv = gtk_menu_get_private (menu);
2632 get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2634 if (event->window == widget->window)
2636 gint arrow_size = 0.7 * arrow_space;
2638 gtk_paint_box (widget->style,
2642 &event->area, widget, "menu",
2645 if (menu->upper_arrow_visible && !menu->tearoff_active)
2647 gtk_paint_box (widget->style,
2649 priv->upper_arrow_state,
2651 &event->area, widget, "menu_scroll_arrow_up",
2657 gtk_paint_arrow (widget->style,
2659 priv->upper_arrow_state,
2661 &event->area, widget, "menu_scroll_arrow_up",
2664 upper.x + (upper.width - arrow_size) / 2,
2665 upper.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
2666 arrow_size, arrow_size);
2669 if (menu->lower_arrow_visible && !menu->tearoff_active)
2671 gtk_paint_box (widget->style,
2673 priv->lower_arrow_state,
2675 &event->area, widget, "menu_scroll_arrow_down",
2681 gtk_paint_arrow (widget->style,
2683 priv->lower_arrow_state,
2685 &event->area, widget, "menu_scroll_arrow_down",
2688 lower.x + (lower.width - arrow_size) / 2,
2689 lower.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
2690 arrow_size, arrow_size);
2693 else if (event->window == menu->bin_window)
2695 gint y = -border.y + menu->scroll_offset;
2697 if (!menu->tearoff_active)
2699 GtkBorder arrow_border;
2701 get_arrows_border (menu, &arrow_border);
2702 y -= arrow_border.top;
2705 gtk_paint_box (widget->style,
2709 &event->area, widget, "menu",
2711 border.width, border.height);
2716 gtk_menu_expose (GtkWidget *widget,
2717 GdkEventExpose *event)
2719 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2720 g_return_val_if_fail (event != NULL, FALSE);
2722 if (GTK_WIDGET_DRAWABLE (widget))
2724 gtk_menu_paint (widget, event);
2726 (* GTK_WIDGET_CLASS (gtk_menu_parent_class)->expose_event) (widget, event);
2733 gtk_menu_show (GtkWidget *widget)
2735 GtkMenu *menu = GTK_MENU (widget);
2737 _gtk_menu_refresh_accel_paths (menu, FALSE);
2739 GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
2743 gtk_menu_button_scroll (GtkMenu *menu,
2744 GdkEventButton *event)
2746 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2748 gboolean touchscreen_mode;
2750 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
2751 "gtk-touchscreen-mode", &touchscreen_mode,
2754 if (touchscreen_mode)
2755 gtk_menu_handle_scrolling (menu,
2756 event->x_root, event->y_root,
2757 event->type == GDK_BUTTON_PRESS,
2767 pointer_in_menu_window (GtkWidget *widget,
2771 GtkMenu *menu = GTK_MENU (widget);
2773 if (GTK_WIDGET_MAPPED (menu->toplevel))
2775 GtkMenuShell *menu_shell;
2776 gint window_x, window_y;
2778 gdk_window_get_position (menu->toplevel->window, &window_x, &window_y);
2780 if (x_root >= window_x && x_root < window_x + widget->allocation.width &&
2781 y_root >= window_y && y_root < window_y + widget->allocation.height)
2784 menu_shell = GTK_MENU_SHELL (widget);
2786 if (GTK_IS_MENU (menu_shell->parent_menu_shell))
2787 return pointer_in_menu_window (menu_shell->parent_menu_shell,
2795 gtk_menu_button_press (GtkWidget *widget,
2796 GdkEventButton *event)
2798 if (event->type != GDK_BUTTON_PRESS)
2801 /* Don't pass down to menu shell for presses over scroll arrows
2803 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
2806 /* Don't pass down to menu shell if a non-menuitem part of the menu
2807 * was clicked. The check for the event_widget being a GtkMenuShell
2808 * works because we have the pointer grabbed on menu_shell->window
2809 * with owner_events=TRUE, so all events that are either outside
2810 * the menu or on its border are delivered relative to
2811 * menu_shell->window.
2813 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
2814 pointer_in_menu_window (widget, event->x_root, event->y_root))
2817 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
2821 gtk_menu_button_release (GtkWidget *widget,
2822 GdkEventButton *event)
2824 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
2826 if (priv->ignore_button_release)
2828 priv->ignore_button_release = FALSE;
2832 if (event->type != GDK_BUTTON_RELEASE)
2835 /* Don't pass down to menu shell for releases over scroll arrows
2837 if (gtk_menu_button_scroll (GTK_MENU (widget), event))
2840 /* Don't pass down to menu shell if a non-menuitem part of the menu
2841 * was clicked (see comment in button_press()).
2843 if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
2844 pointer_in_menu_window (widget, event->x_root, event->y_root))
2846 /* Ugly: make sure menu_shell->button gets reset to 0 when we
2847 * bail out early here so it is in a consistent state for the
2848 * next button_press/button_release in GtkMenuShell.
2851 if (GTK_MENU_SHELL (widget)->active)
2852 GTK_MENU_SHELL (widget)->button = 0;
2857 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
2860 static const gchar *
2861 get_accel_path (GtkWidget *menu_item,
2866 GClosure *accel_closure;
2867 GtkAccelGroup *accel_group;
2869 path = _gtk_widget_get_accel_path (menu_item, locked);
2872 path = GTK_MENU_ITEM (menu_item)->accel_path;
2878 label = GTK_BIN (menu_item)->child;
2880 if (GTK_IS_ACCEL_LABEL (label))
2882 g_object_get (label,
2883 "accel-closure", &accel_closure,
2887 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
2889 *locked = accel_group->lock_count > 0;
2899 gtk_menu_key_press (GtkWidget *widget,
2902 GtkMenuShell *menu_shell;
2904 gboolean delete = FALSE;
2905 gboolean can_change_accels;
2906 gchar *accel = NULL;
2907 guint accel_key, accel_mods;
2908 GdkModifierType consumed_modifiers;
2909 GdkDisplay *display;
2911 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2912 g_return_val_if_fail (event != NULL, FALSE);
2914 menu_shell = GTK_MENU_SHELL (widget);
2915 menu = GTK_MENU (widget);
2917 gtk_menu_stop_navigating_submenu (menu);
2919 if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
2922 display = gtk_widget_get_display (widget);
2924 g_object_get (gtk_widget_get_settings (widget),
2925 "gtk-menu-bar-accel", &accel,
2926 "gtk-can-change-accels", &can_change_accels,
2929 if (accel && *accel)
2932 GdkModifierType mods = 0;
2934 gtk_accelerator_parse (accel, &keyval, &mods);
2937 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
2939 /* FIXME this is wrong, needs to be in the global accel resolution
2940 * thing, to properly consider i18n etc., but that probably requires
2941 * AccelGroup changes etc.
2943 if (event->keyval == keyval && (mods & event->state) == mods)
2945 gtk_menu_shell_cancel (menu_shell);
2953 switch (event->keyval)
2964 /* Figure out what modifiers went into determining the key symbol */
2965 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
2966 event->hardware_keycode, event->state, event->group,
2967 NULL, NULL, NULL, &consumed_modifiers);
2969 accel_key = gdk_keyval_to_lower (event->keyval);
2970 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
2972 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
2973 * We re-upper case when we match against the keyval, but display and save in caseless form.
2975 if (accel_key != event->keyval)
2976 accel_mods |= GDK_SHIFT_MASK;
2978 /* Modify the accelerators */
2979 if (can_change_accels &&
2980 menu_shell->active_menu_item &&
2981 GTK_BIN (menu_shell->active_menu_item)->child && /* no separators */
2982 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
2983 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
2985 GtkWidget *menu_item = menu_shell->active_menu_item;
2986 gboolean locked, replace_accels = TRUE;
2989 path = get_accel_path (menu_item, &locked);
2990 if (!path || locked)
2992 /* can't change accelerators on menu_items without paths
2993 * (basically, those items are accelerator-locked).
2995 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
2996 gtk_widget_error_bell (widget);
3002 /* For the keys that act to delete the current setting, we delete
3003 * the current setting if there is one, otherwise, we set the
3004 * key as the accelerator.
3010 if (gtk_accel_map_lookup_entry (path, &key) &&
3011 (key.accel_key || key.accel_mods))
3017 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3021 /* we failed, probably because this key is in use and
3024 /* g_print("failed to change\n"); */
3025 gtk_widget_error_bell (widget);
3034 check_threshold (GtkWidget *widget,
3035 int start_x, int start_y,
3041 ABS (start_x - x) > THRESHOLD ||
3042 ABS (start_y - y) > THRESHOLD;
3046 definitely_within_item (GtkWidget *widget,
3050 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
3053 gdk_drawable_get_size (window, &w, &h);
3056 check_threshold (widget, 0, 0, x, y) &&
3057 check_threshold (widget, w - 1, 0, x, y) &&
3058 check_threshold (widget, w - 1, h - 1, x, y) &&
3059 check_threshold (widget, 0, h - 1, x, y);
3063 gtk_menu_motion_notify (GtkWidget *widget,
3064 GdkEventMotion *event)
3066 GtkWidget *menu_item;
3068 GtkMenuShell *menu_shell;
3070 gboolean need_enter;
3072 if (GTK_IS_MENU (widget))
3074 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3076 if (priv->ignore_button_release)
3077 priv->ignore_button_release = FALSE;
3079 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3083 /* We received the event for one of two reasons:
3085 * a) We are the active menu, and did gtk_grab_add()
3086 * b) The widget is a child of ours, and the event was propagated
3088 * Since for computation of navigation regions, we want the menu which
3089 * is the parent of the menu item, for a), we need to find that menu,
3090 * which may be different from 'widget'.
3092 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3093 if (!GTK_IS_MENU_ITEM (menu_item) ||
3094 !GTK_IS_MENU (menu_item->parent))
3097 menu_shell = GTK_MENU_SHELL (menu_item->parent);
3098 menu = GTK_MENU (menu_shell);
3100 if (definitely_within_item (menu_item, event->x, event->y))
3101 menu_shell->activate_time = 0;
3103 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
3105 /* Check to see if we are within an active submenu's navigation region
3107 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3110 /* Make sure we pop down if we enter a non-selectable menu item, so we
3111 * don't show a submenu when the cursor is outside the stay-up triangle.
3113 if (!_gtk_menu_item_is_selectable (menu_item))
3115 gtk_menu_shell_deselect (menu_shell);
3121 /* The menu is now sensitive to enter events on its items, but
3122 * was previously sensitive. So we fake an enter event.
3126 menu_shell->ignore_enter = FALSE;
3128 gdk_drawable_get_size (event->window, &width, &height);
3129 if (event->x >= 0 && event->x < width &&
3130 event->y >= 0 && event->y < height)
3132 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3135 send_event->crossing.window = g_object_ref (event->window);
3136 send_event->crossing.time = event->time;
3137 send_event->crossing.send_event = TRUE;
3138 send_event->crossing.x_root = event->x_root;
3139 send_event->crossing.y_root = event->y_root;
3140 send_event->crossing.x = event->x;
3141 send_event->crossing.y = event->y;
3142 send_event->crossing.state = event->state;
3144 /* We send the event to 'widget', the currently active menu,
3145 * instead of 'menu', the menu that the pointer is in. This
3146 * will ensure that the event will be ignored unless the
3147 * menuitem is a child of the active menu or some parent
3148 * menu of the active menu.
3150 result = gtk_widget_event (widget, send_event);
3151 gdk_event_free (send_event);
3161 get_double_arrows (GtkMenu *menu)
3163 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3164 gboolean double_arrows;
3166 gtk_widget_style_get (GTK_WIDGET (menu),
3167 "double-arrows", &double_arrows,
3170 return double_arrows || (priv->initially_pushed_in &&
3171 menu->scroll_offset != 0);
3175 gtk_menu_scroll_by (GtkMenu *menu,
3180 gint view_width, view_height;
3181 gboolean double_arrows;
3182 GtkBorder arrow_border;
3184 widget = GTK_WIDGET (menu);
3185 offset = menu->scroll_offset + step;
3187 get_arrows_border (menu, &arrow_border);
3189 double_arrows = get_double_arrows (menu);
3191 /* If we scroll upward and the non-visible top part
3192 * is smaller than the scroll arrow it would be
3193 * pretty stupid to show the arrow and taking more
3194 * screen space than just scrolling to the top.
3197 if ((step < 0) && (offset < arrow_border.top))
3200 /* Don't scroll over the top if we weren't before: */
3201 if ((menu->scroll_offset >= 0) && (offset < 0))
3204 gdk_drawable_get_size (widget->window, &view_width, &view_height);
3206 if (menu->scroll_offset == 0 &&
3207 view_height >= widget->requisition.height)
3210 /* Don't scroll past the bottom if we weren't before: */
3211 if (menu->scroll_offset > 0)
3212 view_height -= arrow_border.top;
3214 /* When both arrows are always shown, reduce
3215 * view height even more.
3218 view_height -= arrow_border.bottom;
3220 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
3221 (offset + view_height > widget->requisition.height))
3222 offset = widget->requisition.height - view_height;
3224 if (offset != menu->scroll_offset)
3225 gtk_menu_scroll_to (menu, offset);
3229 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3230 gboolean touchscreen_mode)
3232 gboolean upper_visible;
3233 gboolean lower_visible;
3235 upper_visible = menu->upper_arrow_visible;
3236 lower_visible = menu->lower_arrow_visible;
3238 gtk_menu_scroll_by (menu, menu->scroll_step);
3240 if (touchscreen_mode &&
3241 (upper_visible != menu->upper_arrow_visible ||
3242 lower_visible != menu->lower_arrow_visible))
3244 /* We are about to hide a scroll arrow while the mouse is pressed,
3245 * this would cause the uncovered menu item to be activated on button
3246 * release. Therefore we need to ignore button release here
3248 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3249 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3254 gtk_menu_scroll_timeout (gpointer data)
3257 gboolean touchscreen_mode;
3259 menu = GTK_MENU (data);
3261 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3262 "gtk-touchscreen-mode", &touchscreen_mode,
3265 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3271 gtk_menu_scroll_timeout_initial (gpointer data)
3275 gboolean touchscreen_mode;
3277 menu = GTK_MENU (data);
3279 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3280 "gtk-timeout-repeat", &timeout,
3281 "gtk-touchscreen-mode", &touchscreen_mode,
3284 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3286 gtk_menu_remove_scroll_timeout (menu);
3288 menu->timeout_id = gdk_threads_add_timeout (timeout,
3289 gtk_menu_scroll_timeout,
3296 gtk_menu_start_scrolling (GtkMenu *menu)
3299 gboolean touchscreen_mode;
3301 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3302 "gtk-timeout-repeat", &timeout,
3303 "gtk-touchscreen-mode", &touchscreen_mode,
3306 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3308 menu->timeout_id = gdk_threads_add_timeout (timeout,
3309 gtk_menu_scroll_timeout_initial,
3314 gtk_menu_scroll (GtkWidget *widget,
3315 GdkEventScroll *event)
3317 GtkMenu *menu = GTK_MENU (widget);
3319 switch (event->direction)
3321 case GDK_SCROLL_RIGHT:
3322 case GDK_SCROLL_DOWN:
3323 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3325 case GDK_SCROLL_LEFT:
3327 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3335 get_arrows_sensitive_area (GtkMenu *menu,
3336 GdkRectangle *upper,
3337 GdkRectangle *lower)
3341 guint vertical_padding;
3343 gint scroll_arrow_height;
3345 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3347 gtk_widget_style_get (GTK_WIDGET (menu),
3348 "vertical-padding", &vertical_padding,
3349 "scroll-arrow-vlength", &scroll_arrow_height,
3352 border = GTK_CONTAINER (menu)->border_width +
3353 GTK_WIDGET (menu)->style->ythickness + vertical_padding;
3355 gdk_window_get_position (GTK_WIDGET (menu)->window, &win_x, &win_y);
3361 upper->width = width;
3362 upper->height = scroll_arrow_height + border;
3368 lower->y = win_y + height - border - scroll_arrow_height;
3369 lower->width = width;
3370 lower->height = scroll_arrow_height + border;
3376 gtk_menu_handle_scrolling (GtkMenu *menu,
3382 GtkMenuShell *menu_shell;
3383 GtkMenuPrivate *priv;
3386 gboolean scroll_fast = FALSE;
3388 gboolean touchscreen_mode;
3390 priv = gtk_menu_get_private (menu);
3392 menu_shell = GTK_MENU_SHELL (menu);
3394 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3395 "gtk-touchscreen-mode", &touchscreen_mode,
3398 gdk_window_get_position (menu->toplevel->window, &top_x, &top_y);
3402 /* upper arrow handling */
3404 get_arrows_sensitive_area (menu, &rect, NULL);
3407 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3408 (x >= rect.x) && (x < rect.x + rect.width) &&
3409 (y >= rect.y) && (y < rect.y + rect.height))
3414 if (touchscreen_mode)
3415 menu->upper_arrow_prelight = in_arrow;
3417 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3419 gboolean arrow_pressed = FALSE;
3421 if (menu->upper_arrow_visible && !menu->tearoff_active)
3423 if (touchscreen_mode)
3425 if (enter && menu->upper_arrow_prelight)
3427 if (menu->timeout_id == 0)
3429 /* Deselect the active item so that
3430 * any submenus are popped down
3432 gtk_menu_shell_deselect (menu_shell);
3434 gtk_menu_remove_scroll_timeout (menu);
3435 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3439 /* Only do stuff on click. */
3440 gtk_menu_start_scrolling (menu);
3441 arrow_pressed = TRUE;
3446 arrow_pressed = TRUE;
3451 gtk_menu_stop_scrolling (menu);
3454 else /* !touchscreen_mode */
3456 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3458 if (enter && in_arrow &&
3459 (!menu->upper_arrow_prelight ||
3460 menu->scroll_fast != scroll_fast))
3462 menu->upper_arrow_prelight = TRUE;
3463 menu->scroll_fast = scroll_fast;
3465 /* Deselect the active item so that
3466 * any submenus are popped down
3468 gtk_menu_shell_deselect (menu_shell);
3470 gtk_menu_remove_scroll_timeout (menu);
3471 menu->scroll_step = scroll_fast ?
3472 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
3475 gdk_threads_add_timeout (scroll_fast ?
3476 MENU_SCROLL_TIMEOUT2 :
3477 MENU_SCROLL_TIMEOUT1,
3478 gtk_menu_scroll_timeout, menu);
3480 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
3482 gtk_menu_stop_scrolling (menu);
3487 /* gtk_menu_start_scrolling() might have hit the top of the
3488 * menu, so check if the button isn't insensitive before
3489 * changing it to something else.
3491 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3493 GtkStateType arrow_state = GTK_STATE_NORMAL;
3496 arrow_state = GTK_STATE_ACTIVE;
3497 else if (menu->upper_arrow_prelight)
3498 arrow_state = GTK_STATE_PRELIGHT;
3500 if (arrow_state != priv->upper_arrow_state)
3502 priv->upper_arrow_state = arrow_state;
3504 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3510 /* lower arrow handling */
3512 get_arrows_sensitive_area (menu, NULL, &rect);
3515 if (menu->lower_arrow_visible && !menu->tearoff_active &&
3516 (x >= rect.x) && (x < rect.x + rect.width) &&
3517 (y >= rect.y) && (y < rect.y + rect.height))
3522 if (touchscreen_mode)
3523 menu->lower_arrow_prelight = in_arrow;
3525 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3527 gboolean arrow_pressed = FALSE;
3529 if (menu->lower_arrow_visible && !menu->tearoff_active)
3531 if (touchscreen_mode)
3533 if (enter && menu->lower_arrow_prelight)
3535 if (menu->timeout_id == 0)
3537 /* Deselect the active item so that
3538 * any submenus are popped down
3540 gtk_menu_shell_deselect (menu_shell);
3542 gtk_menu_remove_scroll_timeout (menu);
3543 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
3547 /* Only do stuff on click. */
3548 gtk_menu_start_scrolling (menu);
3549 arrow_pressed = TRUE;
3554 arrow_pressed = TRUE;
3559 gtk_menu_stop_scrolling (menu);
3562 else /* !touchscreen_mode */
3564 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
3566 if (enter && in_arrow &&
3567 (!menu->lower_arrow_prelight ||
3568 menu->scroll_fast != scroll_fast))
3570 menu->lower_arrow_prelight = TRUE;
3571 menu->scroll_fast = scroll_fast;
3573 /* Deselect the active item so that
3574 * any submenus are popped down
3576 gtk_menu_shell_deselect (menu_shell);
3578 gtk_menu_remove_scroll_timeout (menu);
3579 menu->scroll_step = scroll_fast ?
3580 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
3583 gdk_threads_add_timeout (scroll_fast ?
3584 MENU_SCROLL_TIMEOUT2 :
3585 MENU_SCROLL_TIMEOUT1,
3586 gtk_menu_scroll_timeout, menu);
3588 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
3590 gtk_menu_stop_scrolling (menu);
3595 /* gtk_menu_start_scrolling() might have hit the bottom of the
3596 * menu, so check if the button isn't insensitive before
3597 * changing it to something else.
3599 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3601 GtkStateType arrow_state = GTK_STATE_NORMAL;
3604 arrow_state = GTK_STATE_ACTIVE;
3605 else if (menu->lower_arrow_prelight)
3606 arrow_state = GTK_STATE_PRELIGHT;
3608 if (arrow_state != priv->lower_arrow_state)
3610 priv->lower_arrow_state = arrow_state;
3612 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3620 gtk_menu_enter_notify (GtkWidget *widget,
3621 GdkEventCrossing *event)
3623 GtkWidget *menu_item;
3624 gboolean touchscreen_mode;
3626 if (event->mode == GDK_CROSSING_GTK_GRAB ||
3627 event->mode == GDK_CROSSING_GTK_UNGRAB ||
3628 event->mode == GDK_CROSSING_STATE_CHANGED)
3631 g_object_get (gtk_widget_get_settings (widget),
3632 "gtk-touchscreen-mode", &touchscreen_mode,
3635 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3636 if (GTK_IS_MENU (widget))
3638 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
3640 if (!menu_shell->ignore_enter)
3641 gtk_menu_handle_scrolling (GTK_MENU (widget),
3642 event->x_root, event->y_root, TRUE, TRUE);
3645 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
3647 GtkWidget *menu = menu_item->parent;
3649 if (GTK_IS_MENU (menu))
3651 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
3652 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3654 if (priv->seen_item_enter)
3656 /* This is the second enter we see for an item
3657 * on this menu. This means a release should always
3660 menu_shell->activate_time = 0;
3662 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
3663 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
3665 if (definitely_within_item (menu_item, event->x, event->y))
3667 /* This is an actual user-enter (ie. not a pop-under)
3668 * In this case, the user must either have entered
3669 * sufficiently far enough into the item, or he must move
3670 * far enough away from the enter point. (see
3671 * gtk_menu_motion_notify())
3673 menu_shell->activate_time = 0;
3677 priv->seen_item_enter = TRUE;
3681 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
3682 * will not correspond to the event widget's parent. Check to see
3683 * if we are in the parent's navigation region.
3685 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
3686 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent),
3687 event->x_root, event->y_root))
3690 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event);
3694 gtk_menu_leave_notify (GtkWidget *widget,
3695 GdkEventCrossing *event)
3697 GtkMenuShell *menu_shell;
3699 GtkMenuItem *menu_item;
3700 GtkWidget *event_widget;
3702 if (event->mode == GDK_CROSSING_GTK_GRAB ||
3703 event->mode == GDK_CROSSING_GTK_UNGRAB ||
3704 event->mode == GDK_CROSSING_STATE_CHANGED)
3707 menu = GTK_MENU (widget);
3708 menu_shell = GTK_MENU_SHELL (widget);
3710 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3713 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
3715 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3717 if (!GTK_IS_MENU_ITEM (event_widget))
3720 menu_item = GTK_MENU_ITEM (event_widget);
3722 /* Here we check to see if we're leaving an active menu item with a submenu,
3723 * in which case we enter submenu navigation mode.
3725 if (menu_shell->active_menu_item != NULL
3726 && menu_item->submenu != NULL
3727 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
3729 if (GTK_MENU_SHELL (menu_item->submenu)->active)
3731 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
3734 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
3736 /* We are leaving an active menu item with nonactive submenu.
3737 * Deselect it so we don't surprise the user with by popping
3738 * up a submenu _after_ he left the item.
3740 gtk_menu_shell_deselect (menu_shell);
3745 return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event);
3749 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
3751 if (menu->navigation_region)
3753 gdk_region_destroy (menu->navigation_region);
3754 menu->navigation_region = NULL;
3756 if (menu->navigation_timeout)
3758 g_source_remove (menu->navigation_timeout);
3759 menu->navigation_timeout = 0;
3763 /* When the timeout is elapsed, the navigation region is destroyed
3764 * and the menuitem under the pointer (if any) is selected.
3767 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
3769 GtkMenu *menu = user_data;
3770 GdkWindow *child_window;
3772 gtk_menu_stop_navigating_submenu (menu);
3774 if (GTK_WIDGET_REALIZED (menu))
3776 child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
3780 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3782 send_event->crossing.window = g_object_ref (child_window);
3783 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
3784 send_event->crossing.send_event = TRUE;
3786 GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
3788 gdk_event_free (send_event);
3796 gtk_menu_navigating_submenu (GtkMenu *menu,
3800 if (menu->navigation_region)
3802 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
3806 gtk_menu_stop_navigating_submenu (menu);
3813 #undef DRAW_STAY_UP_TRIANGLE
3815 #ifdef DRAW_STAY_UP_TRIANGLE
3818 draw_stay_up_triangle (GdkWindow *window,
3821 /* Draw ugly color all over the stay-up triangle */
3822 GdkColor ugly_color = { 0, 50000, 10000, 10000 };
3823 GdkGCValues gc_values;
3825 GdkRectangle clipbox;
3827 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
3828 ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
3829 gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
3830 gdk_gc_set_clip_region (ugly_gc, region);
3832 gdk_region_get_clipbox (region, &clipbox);
3834 gdk_draw_rectangle (window,
3837 clipbox.x, clipbox.y,
3838 clipbox.width, clipbox.height);
3840 g_object_unref (ugly_gc);
3845 flip_region (GdkRegion *region,
3850 GdkRectangle *rectangles;
3851 GdkRectangle clipbox;
3852 GdkRegion *new_region;
3855 new_region = gdk_region_new ();
3857 gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
3858 gdk_region_get_clipbox (region, &clipbox);
3860 for (i = 0; i < n_rectangles; ++i)
3862 GdkRectangle rect = rectangles[i];
3865 rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
3868 rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
3870 gdk_region_union_with_rect (new_region, &rect);
3873 g_free (rectangles);
3879 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
3880 GtkMenuItem *menu_item,
3881 GdkEventCrossing *event)
3883 gint submenu_left = 0;
3884 gint submenu_right = 0;
3885 gint submenu_top = 0;
3886 gint submenu_bottom = 0;
3890 GtkWidget *event_widget;
3892 g_return_if_fail (menu_item->submenu != NULL);
3893 g_return_if_fail (event != NULL);
3895 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3897 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
3898 gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
3900 submenu_right = submenu_left + width;
3901 submenu_bottom = submenu_top + height;
3903 gdk_drawable_get_size (event_widget->window, &width, &height);
3905 if (event->x >= 0 && event->x < width)
3908 gboolean flip_y = FALSE;
3909 gboolean flip_x = FALSE;
3911 gtk_menu_stop_navigating_submenu (menu);
3913 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
3916 point[0].x = event->x_root;
3917 point[1].x = submenu_left;
3922 point[0].x = event->x_root + 1;
3923 point[1].x = 2 * (event->x_root + 1) - submenu_right;
3931 point[0].y = event->y_root + 1;
3932 point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
3934 if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
3942 point[0].y = event->y_root;
3943 point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
3945 if (point[0].y >= submenu_bottom)
3949 point[2].x = point[1].x;
3950 point[2].y = point[0].y;
3952 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
3954 if (flip_x || flip_y)
3956 GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
3957 gdk_region_destroy (menu->navigation_region);
3958 menu->navigation_region = new_region;
3961 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3962 "gtk-menu-popdown-delay", &popdown_delay,
3965 menu->navigation_timeout = gdk_threads_add_timeout (popdown_delay,
3966 gtk_menu_stop_navigating_submenu_cb,
3969 #ifdef DRAW_STAY_UP_TRIANGLE
3970 draw_stay_up_triangle (gdk_get_default_root_window(),
3971 menu->navigation_region);
3977 gtk_menu_deactivate (GtkMenuShell *menu_shell)
3981 g_return_if_fail (GTK_IS_MENU (menu_shell));
3983 parent = menu_shell->parent_menu_shell;
3985 menu_shell->activate_time = 0;
3986 gtk_menu_popdown (GTK_MENU (menu_shell));
3989 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
3993 gtk_menu_position (GtkMenu *menu)
3996 GtkRequisition requisition;
3997 GtkMenuPrivate *private;
4002 GdkScreen *pointer_screen;
4003 GdkRectangle monitor;
4005 g_return_if_fail (GTK_IS_MENU (menu));
4007 widget = GTK_WIDGET (menu);
4009 screen = gtk_widget_get_screen (widget);
4010 gdk_display_get_pointer (gdk_screen_get_display (screen),
4011 &pointer_screen, &x, &y, NULL);
4013 /* We need the requisition to figure out the right place to
4014 * popup the menu. In fact, we always need to ask here, since
4015 * if a size_request was queued while we weren't popped up,
4016 * the requisition won't have been recomputed yet.
4018 gtk_widget_size_request (widget, &requisition);
4020 if (pointer_screen != screen)
4022 /* Pointer is on a different screen; roughly center the
4023 * menu on the screen. If someone was using multiscreen
4024 * + Xinerama together they'd probably want something
4025 * fancier; but that is likely to be vanishingly rare.
4027 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4028 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4031 private = gtk_menu_get_private (menu);
4032 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4034 private->initially_pushed_in = FALSE;
4036 /* Set the type hint here to allow custom position functions to set a different hint */
4037 if (!GTK_WIDGET_VISIBLE (menu->toplevel))
4038 gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4040 if (menu->position_func)
4042 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
4043 menu->position_func_data);
4044 if (private->monitor_num < 0)
4045 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4047 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4051 gint space_left, space_right, space_above, space_below;
4054 gint xthickness = widget->style->xthickness;
4055 gint ythickness = widget->style->ythickness;
4056 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4058 /* The placement of popup menus horizontally works like this (with
4059 * RTL in parentheses)
4061 * - If there is enough room to the right (left) of the mouse cursor,
4062 * position the menu there.
4064 * - Otherwise, if if there is enough room to the left (right) of the
4065 * mouse cursor, position the menu there.
4067 * - Otherwise if the menu is smaller than the monitor, position it
4068 * on the side of the mouse cursor that has the most space available
4070 * - Otherwise (if there is simply not enough room for the menu on the
4071 * monitor), position it as far left (right) as possible.
4073 * Positioning in the vertical direction is similar: first try below
4074 * mouse cursor, then above.
4076 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4078 space_left = x - monitor.x;
4079 space_right = monitor.x + monitor.width - x - 1;
4080 space_above = y - monitor.y;
4081 space_below = monitor.y + monitor.height - y - 1;
4083 /* position horizontally */
4085 /* the amount of space we need to position the menu. Note the
4086 * menu is offset "xthickness" pixels
4088 needed_width = requisition.width - xthickness;
4090 if (needed_width <= space_left ||
4091 needed_width <= space_right)
4093 if ((rtl && needed_width <= space_left) ||
4094 (!rtl && needed_width > space_right))
4097 x = x + xthickness - requisition.width + 1;
4101 /* position right */
4105 /* x is clamped on-screen further down */
4107 else if (requisition.width <= monitor.width)
4109 /* the menu is too big to fit on either side of the mouse
4110 * cursor, but smaller than the monitor. Position it on
4111 * the side that has the most space
4113 if (space_left > space_right)
4121 x = monitor.x + monitor.width - requisition.width;
4124 else /* menu is simply too big for the monitor */
4129 x = monitor.x + monitor.width - requisition.width;
4138 /* Position vertically. The algorithm is the same as above, but
4139 * simpler because we don't have to take RTL into account.
4141 needed_height = requisition.height - ythickness;
4143 if (needed_height <= space_above ||
4144 needed_height <= space_below)
4146 if (needed_height <= space_below)
4149 y = y + ythickness - requisition.height + 1;
4151 y = CLAMP (y, monitor.y,
4152 monitor.y + monitor.height - requisition.height);
4154 else if (needed_height > space_below && needed_height > space_above)
4156 if (space_below >= space_above)
4157 y = monitor.y + monitor.height - requisition.height;
4169 if (private->initially_pushed_in)
4171 menu_height = GTK_WIDGET (menu)->requisition.height;
4173 if (y + menu_height > monitor.y + monitor.height)
4175 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4176 y = (monitor.y + monitor.height) - menu_height;
4181 scroll_offset += monitor.y - y;
4186 /* FIXME: should this be done in the various position_funcs ? */
4187 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4189 if (GTK_MENU_SHELL (menu)->active)
4191 private->have_position = TRUE;
4196 if (y + requisition.height > monitor.y + monitor.height)
4197 requisition.height = (monitor.y + monitor.height) - y;
4201 scroll_offset += monitor.y - y;
4202 requisition.height -= monitor.y - y;
4206 if (scroll_offset > 0)
4208 GtkBorder arrow_border;
4210 get_arrows_border (menu, &arrow_border);
4211 scroll_offset += arrow_border.top;
4214 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
4217 if (!GTK_MENU_SHELL (menu)->active)
4219 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4220 requisition.width, requisition.height);
4223 menu->scroll_offset = scroll_offset;
4227 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4229 if (menu->timeout_id)
4231 g_source_remove (menu->timeout_id);
4232 menu->timeout_id = 0;
4237 gtk_menu_stop_scrolling (GtkMenu *menu)
4239 gboolean touchscreen_mode;
4241 gtk_menu_remove_scroll_timeout (menu);
4243 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4244 "gtk-touchscreen-mode", &touchscreen_mode,
4247 if (!touchscreen_mode)
4249 menu->upper_arrow_prelight = FALSE;
4250 menu->lower_arrow_prelight = FALSE;
4255 gtk_menu_scroll_to (GtkMenu *menu,
4260 gint view_width, view_height;
4263 guint vertical_padding;
4264 guint horizontal_padding;
4265 gboolean double_arrows;
4266 GtkBorder arrow_border;
4268 widget = GTK_WIDGET (menu);
4270 if (menu->tearoff_active &&
4271 menu->tearoff_adjustment &&
4272 (menu->tearoff_adjustment->value != offset))
4274 menu->tearoff_adjustment->value =
4276 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4277 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4280 /* Move/resize the viewport according to arrows: */
4281 view_width = widget->allocation.width;
4282 view_height = widget->allocation.height;
4284 gtk_widget_style_get (GTK_WIDGET (menu),
4285 "vertical-padding", &vertical_padding,
4286 "horizontal-padding", &horizontal_padding,
4289 double_arrows = get_double_arrows (menu);
4291 border_width = GTK_CONTAINER (menu)->border_width;
4292 view_width -= (border_width + widget->style->xthickness + horizontal_padding) * 2;
4293 view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
4294 menu_height = widget->requisition.height -
4295 (border_width + widget->style->ythickness + vertical_padding) * 2;
4297 x = border_width + widget->style->xthickness + horizontal_padding;
4298 y = border_width + widget->style->ythickness + vertical_padding;
4300 if (double_arrows && !menu->tearoff_active)
4302 if (view_height < menu_height ||
4303 (offset > 0 && menu->scroll_offset > 0) ||
4304 (offset < 0 && menu->scroll_offset < 0))
4306 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4307 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4308 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4310 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4311 gtk_widget_queue_draw (GTK_WIDGET (menu));
4313 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4315 get_arrows_border (menu, &arrow_border);
4316 y += arrow_border.top;
4317 view_height -= arrow_border.top;
4318 view_height -= arrow_border.bottom;
4321 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4322 else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4323 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4324 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4326 if (offset >= menu_height - view_height)
4327 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4328 else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4329 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4330 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4332 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4333 (priv->lower_arrow_state != lower_arrow_previous_state))
4334 gtk_widget_queue_draw (GTK_WIDGET (menu));
4336 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4337 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4339 /* At the upper border, possibly remove timeout */
4340 if (menu->scroll_step < 0)
4342 gtk_menu_stop_scrolling (menu);
4343 gtk_widget_queue_draw (GTK_WIDGET (menu));
4347 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4348 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4350 /* At the lower border, possibly remove timeout */
4351 if (menu->scroll_step > 0)
4353 gtk_menu_stop_scrolling (menu);
4354 gtk_widget_queue_draw (GTK_WIDGET (menu));
4358 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4362 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4363 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4365 gtk_menu_stop_scrolling (menu);
4366 gtk_widget_queue_draw (GTK_WIDGET (menu));
4369 else if (!menu->tearoff_active)
4371 gboolean last_visible;
4373 last_visible = menu->upper_arrow_visible;
4374 menu->upper_arrow_visible = offset > 0;
4376 /* upper_arrow_visible may have changed, so requery the border */
4377 get_arrows_border (menu, &arrow_border);
4378 view_height -= arrow_border.top;
4380 if ((last_visible != menu->upper_arrow_visible) &&
4381 !menu->upper_arrow_visible)
4383 menu->upper_arrow_prelight = FALSE;
4385 /* If we hid the upper arrow, possibly remove timeout */
4386 if (menu->scroll_step < 0)
4388 gtk_menu_stop_scrolling (menu);
4389 gtk_widget_queue_draw (GTK_WIDGET (menu));
4393 last_visible = menu->lower_arrow_visible;
4394 menu->lower_arrow_visible = offset < menu_height - view_height;
4396 /* lower_arrow_visible may have changed, so requery the border */
4397 get_arrows_border (menu, &arrow_border);
4398 view_height -= arrow_border.bottom;
4400 if ((last_visible != menu->lower_arrow_visible) &&
4401 !menu->lower_arrow_visible)
4403 menu->lower_arrow_prelight = FALSE;
4405 /* If we hid the lower arrow, possibly remove timeout */
4406 if (menu->scroll_step > 0)
4408 gtk_menu_stop_scrolling (menu);
4409 gtk_widget_queue_draw (GTK_WIDGET (menu));
4413 y += arrow_border.top;
4416 /* Scroll the menu: */
4417 if (GTK_WIDGET_REALIZED (menu))
4418 gdk_window_move (menu->bin_window, 0, -offset);
4420 if (GTK_WIDGET_REALIZED (menu))
4421 gdk_window_move_resize (menu->view_window,
4427 menu->scroll_offset = offset;
4431 compute_child_offset (GtkMenu *menu,
4432 GtkWidget *menu_item,
4435 gboolean *is_last_child)
4437 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4438 gint item_top_attach;
4439 gint item_bottom_attach;
4440 gint child_offset = 0;
4443 get_effective_child_attach (menu_item, NULL, NULL,
4444 &item_top_attach, &item_bottom_attach);
4446 /* there is a possibility that we get called before _size_request, so
4447 * check the height table for safety.
4449 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4452 /* when we have a row with only invisible children, it's height will
4453 * be zero, so there's no need to check WIDGET_VISIBLE here
4455 for (i = 0; i < item_top_attach; i++)
4456 child_offset += priv->heights[i];
4459 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4461 *offset = child_offset;
4463 *height = priv->heights[item_top_attach];
4469 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4470 GtkWidget *menu_item)
4473 gint child_offset, child_height;
4477 gboolean last_child = 0;
4479 menu = GTK_MENU (menu_shell);
4481 /* We need to check if the selected item fully visible.
4482 * If not we need to scroll the menu so that it becomes fully
4486 if (compute_child_offset (menu, menu_item,
4487 &child_offset, &child_height, &last_child))
4489 guint vertical_padding;
4490 gboolean double_arrows;
4492 y = menu->scroll_offset;
4493 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
4495 gtk_widget_style_get (GTK_WIDGET (menu),
4496 "vertical-padding", &vertical_padding,
4499 double_arrows = get_double_arrows (menu);
4501 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
4503 if (child_offset < y)
4505 /* Ignore the enter event we might get if the pointer is on the menu
4507 menu_shell->ignore_enter = TRUE;
4508 gtk_menu_scroll_to (menu, child_offset);
4512 GtkBorder arrow_border;
4516 get_arrows_border (menu, &arrow_border);
4517 if (!menu->tearoff_active)
4518 arrow_height = arrow_border.top + arrow_border.bottom;
4520 if (child_offset + child_height > y + height - arrow_height)
4523 if ((!last_child && !menu->tearoff_active) || double_arrows)
4524 arrow_height += arrow_border.bottom;
4526 y = child_offset + child_height - height + arrow_height;
4527 if (((y > 0) && !menu->tearoff_active) || double_arrows)
4529 /* Need upper arrow */
4530 arrow_height += arrow_border.top;
4531 y = child_offset + child_height - height + arrow_height;
4533 /* Ignore the enter event we might get if the pointer is on the menu
4535 menu_shell->ignore_enter = TRUE;
4536 gtk_menu_scroll_to (menu, y);
4544 gtk_menu_select_item (GtkMenuShell *menu_shell,
4545 GtkWidget *menu_item)
4547 GtkMenu *menu = GTK_MENU (menu_shell);
4549 if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
4550 gtk_menu_scroll_item_visible (menu_shell, menu_item);
4552 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
4556 /* Reparent the menu, taking care of the refcounting
4558 * If unrealize is true we force a unrealize while reparenting the parent.
4559 * This can help eliminate flicker in some cases.
4561 * What happens is that when the menu is unrealized and then re-realized,
4562 * the allocations are as follows:
4564 * parent - 1x1 at (0,0)
4565 * child1 - 100x20 at (0,0)
4566 * child2 - 100x20 at (0,20)
4567 * child3 - 100x20 at (0,40)
4569 * That is, the parent is small but the children are full sized. Then,
4570 * when the queued_resize gets processed, the parent gets resized to
4573 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
4574 * contains the following logic:
4576 * - if a move or resize operation on a window would change the clip
4577 * region on the children, then before the window is resized
4578 * the background for children is temporarily set to None, the
4579 * move/resize done, and the background for the children restored.
4581 * So, at the point where the parent is resized to final size, the
4582 * background for the children is temporarily None, and thus they
4583 * are not cleared to the background color and the previous background
4584 * (the image of the menu) is left in place.
4587 gtk_menu_reparent (GtkMenu *menu,
4588 GtkWidget *new_parent,
4591 GtkObject *object = GTK_OBJECT (menu);
4592 GtkWidget *widget = GTK_WIDGET (menu);
4593 gboolean was_floating = g_object_is_floating (object);
4595 g_object_ref_sink (object);
4599 g_object_ref (object);
4600 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
4601 gtk_container_add (GTK_CONTAINER (new_parent), widget);
4602 g_object_unref (object);
4605 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
4608 g_object_force_floating (G_OBJECT (object));
4610 g_object_unref (object);
4614 gtk_menu_show_all (GtkWidget *widget)
4616 /* Show children, but not self. */
4617 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
4622 gtk_menu_hide_all (GtkWidget *widget)
4624 /* Hide children, but not self. */
4625 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
4629 * gtk_menu_set_screen:
4630 * @menu: a #GtkMenu.
4631 * @screen: a #GdkScreen, or %NULL if the screen should be
4632 * determined by the widget the menu is attached to.
4634 * Sets the #GdkScreen on which the menu will be displayed.
4639 gtk_menu_set_screen (GtkMenu *menu,
4642 g_return_if_fail (GTK_IS_MENU (menu));
4643 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
4645 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
4649 menu_change_screen (menu, screen);
4653 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
4655 attach_widget_screen_changed (attach_widget, NULL, menu);
4661 * @menu: a #GtkMenu.
4662 * @child: a #GtkMenuItem.
4663 * @left_attach: The column number to attach the left side of the item to.
4664 * @right_attach: The column number to attach the right side of the item to.
4665 * @top_attach: The row number to attach the top of the item to.
4666 * @bottom_attach: The row number to attach the bottom of the item to.
4668 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
4669 * an item will occupy is specified by @left_attach, @right_attach,
4670 * @top_attach and @bottom_attach. These each represent the leftmost,
4671 * rightmost, uppermost and lower column and row numbers of the table.
4672 * (Columns and rows are indexed from zero).
4674 * Note that this function is not related to gtk_menu_detach().
4679 gtk_menu_attach (GtkMenu *menu,
4684 guint bottom_attach)
4686 GtkMenuShell *menu_shell;
4688 g_return_if_fail (GTK_IS_MENU (menu));
4689 g_return_if_fail (GTK_IS_MENU_ITEM (child));
4690 g_return_if_fail (child->parent == NULL ||
4691 child->parent == GTK_WIDGET (menu));
4692 g_return_if_fail (left_attach < right_attach);
4693 g_return_if_fail (top_attach < bottom_attach);
4695 menu_shell = GTK_MENU_SHELL (menu);
4699 AttachInfo *ai = get_attach_info (child);
4701 ai->left_attach = left_attach;
4702 ai->right_attach = right_attach;
4703 ai->top_attach = top_attach;
4704 ai->bottom_attach = bottom_attach;
4706 menu_shell->children = g_list_append (menu_shell->children, child);
4708 gtk_widget_set_parent (child, GTK_WIDGET (menu));
4710 menu_queue_resize (menu);
4714 gtk_container_child_set (GTK_CONTAINER (child->parent), child,
4715 "left-attach", left_attach,
4716 "right-attach", right_attach,
4717 "top-attach", top_attach,
4718 "bottom-attach", bottom_attach,
4724 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
4728 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
4729 "gtk-menu-popup-delay", &popup_delay,
4736 find_child_containing (GtkMenuShell *menu_shell,
4744 /* find a child which includes the area given by
4745 * left, right, top, bottom.
4748 for (list = menu_shell->children; list; list = list->next)
4752 if (!_gtk_menu_item_is_selectable (list->data))
4755 get_effective_child_attach (list->data, &l, &r, &t, &b);
4757 if (l <= left && right <= r
4758 && t <= top && bottom <= b)
4759 return GTK_WIDGET (list->data);
4766 gtk_menu_move_current (GtkMenuShell *menu_shell,
4767 GtkMenuDirectionType direction)
4769 GtkMenu *menu = GTK_MENU (menu_shell);
4772 GtkWidget *match = NULL;
4774 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
4778 case GTK_MENU_DIR_CHILD:
4779 direction = GTK_MENU_DIR_PARENT;
4781 case GTK_MENU_DIR_PARENT:
4782 direction = GTK_MENU_DIR_CHILD;
4788 /* use special table menu key bindings */
4789 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
4791 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
4793 if (direction == GTK_MENU_DIR_NEXT)
4795 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
4797 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
4805 for (i = 0; i < t; i++)
4807 match = find_child_containing (menu_shell,
4808 l, l + 1, i, i + 1);
4814 else if (direction == GTK_MENU_DIR_PREV)
4816 for (i = t; i > 0; i--)
4818 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
4826 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
4828 match = find_child_containing (menu_shell,
4829 l, l + 1, i - 1, i);
4835 else if (direction == GTK_MENU_DIR_PARENT)
4837 /* we go one left if possible */
4839 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
4843 GtkWidget *parent = menu_shell->parent_menu_shell;
4846 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
4847 match = menu_shell->active_menu_item;
4850 else if (direction == GTK_MENU_DIR_CHILD)
4852 /* we go one right if possible */
4853 if (r < gtk_menu_get_n_columns (menu))
4854 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
4858 GtkWidget *parent = menu_shell->parent_menu_shell;
4860 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
4862 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
4863 match = menu_shell->active_menu_item;
4869 gtk_menu_shell_select_item (menu_shell, match);
4874 GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
4878 get_visible_size (GtkMenu *menu)
4880 GtkWidget *widget = GTK_WIDGET (menu);
4881 GtkContainer *container = GTK_CONTAINER (menu);
4883 gint menu_height = (widget->allocation.height
4884 - 2 * (container->border_width
4885 + widget->style->ythickness));
4887 if (!menu->tearoff_active)
4889 GtkBorder arrow_border;
4891 get_arrows_border (menu, &arrow_border);
4892 menu_height -= arrow_border.top;
4893 menu_height -= arrow_border.bottom;
4899 /* Find the sensitive on-screen child containing @y, or if none,
4900 * the nearest selectable onscreen child. (%NULL if none)
4903 child_at (GtkMenu *menu,
4906 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4907 GtkWidget *child = NULL;
4908 gint child_offset = 0;
4911 gint lower, upper; /* Onscreen bounds */
4913 menu_height = get_visible_size (menu);
4914 lower = menu->scroll_offset;
4915 upper = menu->scroll_offset + menu_height;
4917 for (children = menu_shell->children; children; children = children->next)
4919 if (GTK_WIDGET_VISIBLE (children->data))
4921 GtkRequisition child_requisition;
4923 gtk_widget_size_request (children->data, &child_requisition);
4925 if (_gtk_menu_item_is_selectable (children->data) &&
4926 child_offset >= lower &&
4927 child_offset + child_requisition.height <= upper)
4929 child = children->data;
4931 if (child_offset + child_requisition.height > y &&
4932 !GTK_IS_TEAROFF_MENU_ITEM (child))
4936 child_offset += child_requisition.height;
4944 get_menu_height (GtkMenu *menu)
4947 GtkWidget *widget = GTK_WIDGET (menu);
4949 height = widget->requisition.height;
4950 height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
4952 if (!menu->tearoff_active)
4954 GtkBorder arrow_border;
4956 get_arrows_border (menu, &arrow_border);
4957 height -= arrow_border.top;
4958 height -= arrow_border.bottom;
4965 gtk_menu_real_move_scroll (GtkMenu *menu,
4968 gint page_size = get_visible_size (menu);
4969 gint end_position = get_menu_height (menu);
4970 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4974 case GTK_SCROLL_PAGE_UP:
4975 case GTK_SCROLL_PAGE_DOWN:
4979 gint child_offset = 0;
4980 gboolean old_upper_arrow_visible;
4983 if (type == GTK_SCROLL_PAGE_UP)
4988 if (menu_shell->active_menu_item)
4992 compute_child_offset (menu, menu_shell->active_menu_item,
4993 &child_offset, &child_height, NULL);
4994 child_offset += child_height / 2;
4997 menu_shell->ignore_enter = TRUE;
4998 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4999 old_offset = menu->scroll_offset;
5001 new_offset = menu->scroll_offset + step;
5002 new_offset = CLAMP (new_offset, 0, end_position - page_size);
5004 gtk_menu_scroll_to (menu, new_offset);
5006 if (menu_shell->active_menu_item)
5008 GtkWidget *new_child;
5009 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5010 GtkBorder arrow_border;
5012 get_arrows_border (menu, &arrow_border);
5014 if (menu->scroll_offset != old_offset)
5015 step = menu->scroll_offset - old_offset;
5017 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5019 new_child = child_at (menu, child_offset + step);
5021 gtk_menu_shell_select_item (menu_shell, new_child);
5025 case GTK_SCROLL_START:
5026 /* Ignore the enter event we might get if the pointer is on the menu
5028 menu_shell->ignore_enter = TRUE;
5029 gtk_menu_scroll_to (menu, 0);
5030 gtk_menu_shell_select_first (menu_shell, TRUE);
5032 case GTK_SCROLL_END:
5033 /* Ignore the enter event we might get if the pointer is on the menu
5035 menu_shell->ignore_enter = TRUE;
5036 gtk_menu_scroll_to (menu, end_position - page_size);
5037 _gtk_menu_shell_select_last (menu_shell, TRUE);
5046 * gtk_menu_set_monitor:
5048 * @monitor_num: the number of the monitor on which the menu should
5051 * Informs GTK+ on which monitor a menu should be popped up.
5052 * See gdk_screen_get_monitor_geometry().
5054 * This function should be called from a #GtkMenuPositionFunc if the
5055 * menu should not appear on the same monitor as the pointer. This
5056 * information can't be reliably inferred from the coordinates returned
5057 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
5058 * may extend beyond the monitor boundaries or even the screen boundaries.
5063 gtk_menu_set_monitor (GtkMenu *menu,
5066 GtkMenuPrivate *priv;
5067 g_return_if_fail (GTK_IS_MENU (menu));
5069 priv = gtk_menu_get_private (menu);
5071 priv->monitor_num = monitor_num;
5075 * gtk_menu_get_monitor:
5078 * Retrieves the number of the monitor on which to show the menu.
5080 * Returns: the number of the monitor on which the menu should
5081 * be popped up or -1, if no monitor has been set
5086 gtk_menu_get_monitor (GtkMenu *menu)
5088 GtkMenuPrivate *priv;
5089 g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5091 priv = gtk_menu_get_private (menu);
5093 return priv->monitor_num;
5097 * gtk_menu_get_for_attach_widget:
5098 * @widget: a #GtkWidget
5100 * Returns a list of the menus which are attached to this widget.
5101 * This list is owned by GTK+ and must not be modified.
5103 * Return value: the list of menus attached to his widget.
5108 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5112 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5114 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5119 gtk_menu_grab_notify (GtkWidget *widget,
5120 gboolean was_grabbed)
5122 GtkWidget *toplevel;
5123 GtkWindowGroup *group;
5126 toplevel = gtk_widget_get_toplevel (widget);
5127 group = gtk_window_get_group (GTK_WINDOW (toplevel));
5128 grab = _gtk_window_group_get_current_grab (group);
5132 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
5133 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5137 #define __GTK_MENU_C__
5138 #include "gtkaliasdef.c"