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
30 #include <string.h> /* memset */
31 #include "gdk/gdkkeysyms.h"
32 #include "gtkaccellabel.h"
33 #include "gtkaccelmap.h"
34 #include "gtkbindings.h"
37 #include "gtkmarshalers.h"
39 #include "gtkmenuitem.h"
40 #include "gtktearoffmenuitem.h"
41 #include "gtkwindow.h"
43 #include "gtkvscrollbar.h"
44 #include "gtksettings.h"
48 #define MENU_ITEM_CLASS(w) GTK_MENU_ITEM_GET_CLASS (w)
50 #define DEFAULT_POPUP_DELAY 225
51 #define DEFAULT_POPDOWN_DELAY 1000
53 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
54 * extends below the submenu
57 #define MENU_SCROLL_STEP1 8
58 #define MENU_SCROLL_STEP2 15
59 #define MENU_SCROLL_ARROW_HEIGHT 16
60 #define MENU_SCROLL_FAST_ZONE 8
61 #define MENU_SCROLL_TIMEOUT1 50
62 #define MENU_SCROLL_TIMEOUT2 50
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 have_position;
82 /* info used for the table */
88 /* Cached layout information */
100 gint effective_left_attach;
101 gint effective_right_attach;
102 gint effective_top_attach;
103 gint effective_bottom_attach;
118 CHILD_PROP_LEFT_ATTACH,
119 CHILD_PROP_RIGHT_ATTACH,
120 CHILD_PROP_TOP_ATTACH,
121 CHILD_PROP_BOTTOM_ATTACH
124 static void gtk_menu_class_init (GtkMenuClass *klass);
125 static void gtk_menu_init (GtkMenu *menu);
126 static void gtk_menu_set_property (GObject *object,
130 static void gtk_menu_get_property (GObject *object,
134 static void gtk_menu_set_child_property(GtkContainer *container,
139 static void gtk_menu_get_child_property(GtkContainer *container,
144 static void gtk_menu_destroy (GtkObject *object);
145 static void gtk_menu_finalize (GObject *object);
146 static void gtk_menu_realize (GtkWidget *widget);
147 static void gtk_menu_unrealize (GtkWidget *widget);
148 static void gtk_menu_size_request (GtkWidget *widget,
149 GtkRequisition *requisition);
150 static void gtk_menu_size_allocate (GtkWidget *widget,
151 GtkAllocation *allocation);
152 static void gtk_menu_paint (GtkWidget *widget,
153 GdkEventExpose *expose);
154 static void gtk_menu_show (GtkWidget *widget);
155 static gboolean gtk_menu_expose (GtkWidget *widget,
156 GdkEventExpose *event);
157 static gboolean gtk_menu_key_press (GtkWidget *widget,
159 static gboolean gtk_menu_button_press (GtkWidget *widget,
160 GdkEventButton *event);
161 static gboolean gtk_menu_button_release (GtkWidget *widget,
162 GdkEventButton *event);
163 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
164 GdkEventMotion *event);
165 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
166 GdkEventCrossing *event);
167 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
168 GdkEventCrossing *event);
169 static void gtk_menu_scroll_to (GtkMenu *menu,
172 static void gtk_menu_stop_scrolling (GtkMenu *menu);
173 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
174 static gboolean gtk_menu_scroll_timeout (gpointer data);
176 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
177 GtkWidget *menu_item);
178 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
179 GtkWidget *menu_item);
180 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
183 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
185 static void gtk_menu_handle_scrolling (GtkMenu *menu,
187 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
189 static void gtk_menu_style_set (GtkWidget *widget,
190 GtkStyle *previous_style);
191 static gboolean gtk_menu_focus (GtkWidget *widget,
192 GtkDirectionType direction);
193 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
194 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
195 GtkMenuDirectionType direction);
196 static void gtk_menu_real_move_scroll (GtkMenu *menu,
199 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
200 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
201 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
204 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
205 GtkMenuItem *menu_item,
206 GdkEventCrossing *event);
208 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
209 static void gtk_menu_show_all (GtkWidget *widget);
210 static void gtk_menu_hide_all (GtkWidget *widget);
211 static void gtk_menu_position (GtkMenu *menu);
212 static void gtk_menu_reparent (GtkMenu *menu,
213 GtkWidget *new_parent,
215 static void gtk_menu_remove (GtkContainer *menu,
218 static void gtk_menu_update_title (GtkMenu *menu);
220 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
221 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
223 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
225 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
226 gboolean group_changed);
228 static GtkMenuShellClass *parent_class = NULL;
229 static const gchar *attach_data_key = "gtk-menu-attach-data";
231 static guint menu_signals[LAST_SIGNAL] = { 0 };
234 gtk_menu_free_private (gpointer data)
236 GtkMenuPrivate *priv = (GtkMenuPrivate *)data;
238 g_free (priv->heights);
244 gtk_menu_get_private (GtkMenu *menu)
246 GtkMenuPrivate *private;
247 static GQuark private_quark = 0;
250 private_quark = g_quark_from_static_string ("gtk-menu-private");
252 private = g_object_get_qdata (G_OBJECT (menu), private_quark);
256 private = g_new0 (GtkMenuPrivate, 1);
257 private->have_position = FALSE;
259 g_object_set_qdata_full (G_OBJECT (menu), private_quark,
260 private, gtk_menu_free_private);
267 gtk_menu_get_type (void)
269 static GType menu_type = 0;
273 static const GTypeInfo menu_info =
275 sizeof (GtkMenuClass),
276 NULL, /* base_init */
277 NULL, /* base_finalize */
278 (GClassInitFunc) gtk_menu_class_init,
279 NULL, /* class_finalize */
280 NULL, /* class_data */
283 (GInstanceInitFunc) gtk_menu_init,
286 menu_type = g_type_register_static (GTK_TYPE_MENU_SHELL, "GtkMenu",
294 menu_queue_resize (GtkMenu *menu)
296 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
298 priv->have_layout = FALSE;
299 gtk_widget_queue_resize (GTK_WIDGET (menu));
303 get_attach_info (GtkWidget *child)
305 GObject *object = G_OBJECT (child);
306 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
310 ai = g_new0 (AttachInfo, 1);
311 g_object_set_data_full (object, ATTACH_INFO_KEY, ai, g_free);
318 is_grid_attached (AttachInfo *ai)
320 return (ai->left_attach >= 0 &&
321 ai->right_attach >= 0 &&
322 ai->top_attach >= 0 &&
323 ai->bottom_attach >= 0);
327 menu_ensure_layout (GtkMenu *menu)
329 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
331 if (!priv->have_layout)
333 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
337 gint max_right_attach;
338 gint max_bottom_attach;
340 /* Find extents of gridded portion
342 max_right_attach = 1;
343 max_bottom_attach = 0;
345 for (l = menu_shell->children; l; l = l->next)
347 GtkWidget *child = l->data;
348 AttachInfo *ai = get_attach_info (child);
350 if (is_grid_attached (ai))
352 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
353 max_right_attach = MAX (max_right_attach, ai->right_attach);
359 row_occupied = g_malloc0 (max_bottom_attach);
361 for (l = menu_shell->children; l; l = l->next)
363 GtkWidget *child = l->data;
364 AttachInfo *ai = get_attach_info (child);
366 if (is_grid_attached (ai))
370 for (i = ai->top_attach; i < ai->bottom_attach; i++)
371 row_occupied[i] = TRUE;
375 /* Lay non-grid-items out in those rows
378 for (l = menu_shell->children; l; l = l->next)
380 GtkWidget *child = l->data;
381 AttachInfo *ai = get_attach_info (child);
383 if (!is_grid_attached (ai))
385 while (current_row < max_bottom_attach && row_occupied[current_row])
388 ai->effective_left_attach = 0;
389 ai->effective_right_attach = max_right_attach;
390 ai->effective_top_attach = current_row;
391 ai->effective_bottom_attach = current_row + 1;
397 ai->effective_left_attach = ai->left_attach;
398 ai->effective_right_attach = ai->right_attach;
399 ai->effective_top_attach = ai->top_attach;
400 ai->effective_bottom_attach = ai->bottom_attach;
404 g_free (row_occupied);
406 priv->n_rows = MAX (current_row, max_bottom_attach);
407 priv->n_columns = max_right_attach;
408 priv->have_layout = TRUE;
414 gtk_menu_get_n_columns (GtkMenu *menu)
416 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
418 menu_ensure_layout (menu);
420 return priv->n_columns;
424 gtk_menu_get_n_rows (GtkMenu *menu)
426 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
428 menu_ensure_layout (menu);
434 get_effective_child_attach (GtkWidget *child,
440 GtkMenu *menu = GTK_MENU (child->parent);
443 menu_ensure_layout (menu);
445 ai = get_attach_info (child);
448 *l = ai->effective_left_attach;
450 *r = ai->effective_right_attach;
452 *t = ai->effective_top_attach;
454 *b = ai->effective_bottom_attach;
459 gtk_menu_class_init (GtkMenuClass *class)
461 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
462 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
463 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
464 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
465 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
466 GtkBindingSet *binding_set;
468 parent_class = g_type_class_peek_parent (class);
470 gobject_class->finalize = gtk_menu_finalize;
471 gobject_class->set_property = gtk_menu_set_property;
472 gobject_class->get_property = gtk_menu_get_property;
474 object_class->destroy = gtk_menu_destroy;
476 widget_class->realize = gtk_menu_realize;
477 widget_class->unrealize = gtk_menu_unrealize;
478 widget_class->size_request = gtk_menu_size_request;
479 widget_class->size_allocate = gtk_menu_size_allocate;
480 widget_class->show = gtk_menu_show;
481 widget_class->expose_event = gtk_menu_expose;
482 widget_class->key_press_event = gtk_menu_key_press;
483 widget_class->button_press_event = gtk_menu_button_press;
484 widget_class->button_release_event = gtk_menu_button_release;
485 widget_class->motion_notify_event = gtk_menu_motion_notify;
486 widget_class->show_all = gtk_menu_show_all;
487 widget_class->hide_all = gtk_menu_hide_all;
488 widget_class->enter_notify_event = gtk_menu_enter_notify;
489 widget_class->leave_notify_event = gtk_menu_leave_notify;
490 widget_class->motion_notify_event = gtk_menu_motion_notify;
491 widget_class->style_set = gtk_menu_style_set;
492 widget_class->focus = gtk_menu_focus;
493 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
495 container_class->remove = gtk_menu_remove;
496 container_class->get_child_property = gtk_menu_get_child_property;
497 container_class->set_child_property = gtk_menu_set_child_property;
499 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
500 menu_shell_class->deactivate = gtk_menu_deactivate;
501 menu_shell_class->select_item = gtk_menu_select_item;
502 menu_shell_class->insert = gtk_menu_real_insert;
503 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
504 menu_shell_class->move_current = gtk_menu_move_current;
506 menu_signals[MOVE_SCROLL] =
507 _gtk_binding_signal_new ("move_scroll",
508 G_OBJECT_CLASS_TYPE (object_class),
509 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
510 G_CALLBACK (gtk_menu_real_move_scroll),
512 _gtk_marshal_VOID__ENUM,
514 GTK_TYPE_SCROLL_TYPE);
516 g_object_class_install_property (gobject_class,
518 g_param_spec_string ("tearoff-title",
520 P_("A title that may be displayed by the window manager when this menu is torn-off"),
522 G_PARAM_READABLE | G_PARAM_WRITABLE));
524 gtk_widget_class_install_style_property (widget_class,
525 g_param_spec_int ("vertical-padding",
526 P_("Vertical Padding"),
527 P_("Extra space at the top and bottom of the menu"),
533 gtk_widget_class_install_style_property (widget_class,
534 g_param_spec_int ("vertical-offset",
535 P_("Vertical Offset"),
536 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
542 gtk_widget_class_install_style_property (widget_class,
543 g_param_spec_int ("horizontal-offset",
544 P_("Horizontal Offset"),
545 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
552 gtk_container_class_install_child_property (container_class,
553 CHILD_PROP_LEFT_ATTACH,
554 g_param_spec_int ("left_attach",
556 P_("The column number to attach the left side of the child to"),
560 gtk_container_class_install_child_property (container_class,
561 CHILD_PROP_RIGHT_ATTACH,
562 g_param_spec_int ("right_attach",
564 P_("The column number to attach the right side of the child to"),
568 gtk_container_class_install_child_property (container_class,
569 CHILD_PROP_TOP_ATTACH,
570 g_param_spec_int ("top_attach",
572 P_("The row number to attach the top of the child to"),
576 gtk_container_class_install_child_property (container_class,
577 CHILD_PROP_BOTTOM_ATTACH,
578 g_param_spec_int ("bottom_attach",
580 P_("The row number to attach the bottom of the child to"),
584 binding_set = gtk_binding_set_by_class (class);
585 gtk_binding_entry_add_signal (binding_set,
588 GTK_TYPE_MENU_DIRECTION_TYPE,
590 gtk_binding_entry_add_signal (binding_set,
593 GTK_TYPE_MENU_DIRECTION_TYPE,
595 gtk_binding_entry_add_signal (binding_set,
598 GTK_TYPE_MENU_DIRECTION_TYPE,
600 gtk_binding_entry_add_signal (binding_set,
603 GTK_TYPE_MENU_DIRECTION_TYPE,
605 gtk_binding_entry_add_signal (binding_set,
608 GTK_TYPE_MENU_DIRECTION_TYPE,
609 GTK_MENU_DIR_PARENT);
610 gtk_binding_entry_add_signal (binding_set,
613 GTK_TYPE_MENU_DIRECTION_TYPE,
614 GTK_MENU_DIR_PARENT);
615 gtk_binding_entry_add_signal (binding_set,
618 GTK_TYPE_MENU_DIRECTION_TYPE,
620 gtk_binding_entry_add_signal (binding_set,
623 GTK_TYPE_MENU_DIRECTION_TYPE,
625 gtk_binding_entry_add_signal (binding_set,
628 GTK_TYPE_SCROLL_TYPE,
630 gtk_binding_entry_add_signal (binding_set,
633 GTK_TYPE_SCROLL_TYPE,
635 gtk_binding_entry_add_signal (binding_set,
638 GTK_TYPE_SCROLL_TYPE,
640 gtk_binding_entry_add_signal (binding_set,
643 GTK_TYPE_SCROLL_TYPE,
645 gtk_binding_entry_add_signal (binding_set,
648 GTK_TYPE_SCROLL_TYPE,
650 gtk_binding_entry_add_signal (binding_set,
653 GTK_TYPE_SCROLL_TYPE,
655 gtk_binding_entry_add_signal (binding_set,
658 GTK_TYPE_SCROLL_TYPE,
659 GTK_SCROLL_PAGE_DOWN);
660 gtk_binding_entry_add_signal (binding_set,
663 GTK_TYPE_SCROLL_TYPE,
664 GTK_SCROLL_PAGE_DOWN);
666 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
667 P_("Can change accelerators"),
668 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
672 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
673 P_("Delay before submenus appear"),
674 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
680 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
681 P_("Delay before hiding a submenu"),
682 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
685 DEFAULT_POPDOWN_DELAY,
692 gtk_menu_set_property (GObject *object,
699 menu = GTK_MENU (object);
703 case PROP_TEAROFF_TITLE:
704 gtk_menu_set_title (menu, g_value_get_string (value));
707 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
713 gtk_menu_get_property (GObject *object,
720 menu = GTK_MENU (object);
724 case PROP_TEAROFF_TITLE:
725 g_value_set_string (value, gtk_menu_get_title (menu));
728 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
734 gtk_menu_set_child_property (GtkContainer *container,
740 GtkMenu *menu = GTK_MENU (container);
741 AttachInfo *ai = get_attach_info (child);
745 case CHILD_PROP_LEFT_ATTACH:
746 ai->left_attach = g_value_get_int (value);
748 case CHILD_PROP_RIGHT_ATTACH:
749 ai->right_attach = g_value_get_int (value);
751 case CHILD_PROP_TOP_ATTACH:
752 ai->top_attach = g_value_get_int (value);
754 case CHILD_PROP_BOTTOM_ATTACH:
755 ai->bottom_attach = g_value_get_int (value);
759 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
763 menu_queue_resize (menu);
767 gtk_menu_get_child_property (GtkContainer *container,
773 AttachInfo *ai = get_attach_info (child);
777 case CHILD_PROP_LEFT_ATTACH:
778 g_value_set_int (value, ai->left_attach);
780 case CHILD_PROP_RIGHT_ATTACH:
781 g_value_set_int (value, ai->right_attach);
783 case CHILD_PROP_TOP_ATTACH:
784 g_value_set_int (value, ai->top_attach);
786 case CHILD_PROP_BOTTOM_ATTACH:
787 g_value_set_int (value, ai->bottom_attach);
791 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
797 gtk_menu_window_event (GtkWidget *window,
801 gboolean handled = FALSE;
803 g_object_ref (window);
809 case GDK_KEY_RELEASE:
810 handled = gtk_widget_event (menu, event);
816 g_object_unref (window);
817 g_object_unref (menu);
823 gtk_menu_window_size_request (GtkWidget *window,
824 GtkRequisition *requisition,
827 GtkMenuPrivate *private = gtk_menu_get_private (menu);
829 if (private->have_position)
831 GdkScreen *screen = gtk_widget_get_screen (window);
832 GdkRectangle monitor;
834 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
836 if (private->y + requisition->height > monitor.y + monitor.height)
837 requisition->height = monitor.y + monitor.height - private->y;
839 if (private->y < monitor.y)
840 requisition->height -= monitor.y - private->y;
845 gtk_menu_init (GtkMenu *menu)
847 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
849 menu->parent_menu_item = NULL;
850 menu->old_active_menu_item = NULL;
851 menu->accel_group = NULL;
852 menu->position_func = NULL;
853 menu->position_func_data = NULL;
854 menu->toggle_size = 0;
856 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
857 "type", GTK_WINDOW_POPUP,
860 "signal::event", gtk_menu_window_event, menu,
861 "signal::size_request", gtk_menu_window_size_request, menu,
862 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
864 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
865 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
867 /* Refloat the menu, so that reference counting for the menu isn't
868 * affected by it being a child of the toplevel
870 GTK_WIDGET_SET_FLAGS (menu, GTK_FLOATING);
871 menu->needs_destruction_ref_count = TRUE;
873 menu->view_window = NULL;
874 menu->bin_window = NULL;
876 menu->scroll_offset = 0;
877 menu->scroll_step = 0;
878 menu->timeout_id = 0;
879 menu->scroll_fast = FALSE;
881 menu->tearoff_window = NULL;
882 menu->tearoff_hbox = NULL;
883 menu->torn_off = FALSE;
884 menu->tearoff_active = FALSE;
885 menu->tearoff_adjustment = NULL;
886 menu->tearoff_scrollbar = NULL;
888 menu->upper_arrow_visible = FALSE;
889 menu->lower_arrow_visible = FALSE;
890 menu->upper_arrow_prelight = FALSE;
891 menu->lower_arrow_prelight = FALSE;
893 priv->have_layout = FALSE;
897 gtk_menu_destroy (GtkObject *object)
900 GtkMenuAttachData *data;
901 GtkMenuPrivate *priv;
903 g_return_if_fail (GTK_IS_MENU (object));
905 menu = GTK_MENU (object);
907 gtk_menu_stop_scrolling (menu);
909 data = g_object_get_data (G_OBJECT (object), attach_data_key);
911 gtk_menu_detach (menu);
913 gtk_menu_stop_navigating_submenu (menu);
915 if (menu->old_active_menu_item)
917 g_object_unref (menu->old_active_menu_item);
918 menu->old_active_menu_item = NULL;
921 /* Add back the reference count for being a child */
922 if (menu->needs_destruction_ref_count)
924 menu->needs_destruction_ref_count = FALSE;
925 g_object_ref (object);
928 if (menu->accel_group)
930 g_object_unref (menu->accel_group);
931 menu->accel_group = NULL;
935 gtk_widget_destroy (menu->toplevel);
936 if (menu->tearoff_window)
937 gtk_widget_destroy (menu->tearoff_window);
939 priv = gtk_menu_get_private (menu);
941 GTK_OBJECT_CLASS (parent_class)->destroy (object);
945 gtk_menu_finalize (GObject *object)
947 GtkMenu *menu = GTK_MENU (object);
949 g_free (menu->accel_path);
951 G_OBJECT_CLASS (parent_class)->finalize (object);
955 menu_change_screen (GtkMenu *menu,
956 GdkScreen *new_screen)
958 GtkMenuPrivate *private = gtk_menu_get_private (menu);
962 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
963 gtk_menu_position (menu);
966 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
967 private->monitor_num = -1;
971 attach_widget_screen_changed (GtkWidget *attach_widget,
972 GdkScreen *previous_screen,
975 if (gtk_widget_has_screen (attach_widget) &&
976 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
978 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
983 gtk_menu_attach_to_widget (GtkMenu *menu,
984 GtkWidget *attach_widget,
985 GtkMenuDetachFunc detacher)
987 GtkMenuAttachData *data;
990 g_return_if_fail (GTK_IS_MENU (menu));
991 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
992 g_return_if_fail (detacher != NULL);
994 /* keep this function in sync with gtk_widget_set_parent()
997 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1000 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1001 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1005 g_object_ref (menu);
1006 gtk_object_sink (GTK_OBJECT (menu));
1008 data = g_new (GtkMenuAttachData, 1);
1009 data->attach_widget = attach_widget;
1011 g_signal_connect (attach_widget, "screen_changed",
1012 G_CALLBACK (attach_widget_screen_changed), menu);
1013 attach_widget_screen_changed (attach_widget, NULL, menu);
1015 data->detacher = detacher;
1016 g_object_set_data (G_OBJECT (menu), attach_data_key, data);
1017 list = g_object_get_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1018 if (!g_list_find (list, menu))
1020 list = g_list_prepend (list, menu);
1022 g_object_set_data_full (G_OBJECT (attach_widget), ATTACHED_MENUS, list, (GtkDestroyNotify) g_list_free);
1024 if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
1025 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1027 /* we don't need to set the style here, since
1028 * we are a toplevel widget.
1031 /* Fallback title for menu comes from attach widget */
1032 gtk_menu_update_title (menu);
1036 gtk_menu_get_attach_widget (GtkMenu *menu)
1038 GtkMenuAttachData *data;
1040 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1042 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1044 return data->attach_widget;
1049 gtk_menu_detach (GtkMenu *menu)
1051 GtkMenuAttachData *data;
1054 g_return_if_fail (GTK_IS_MENU (menu));
1056 /* keep this function in sync with gtk_widget_unparent()
1058 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1061 g_warning ("gtk_menu_detach(): menu is not attached");
1064 g_object_set_data (G_OBJECT (menu), attach_data_key, NULL);
1066 g_signal_handlers_disconnect_by_func (data->attach_widget,
1067 (gpointer) attach_widget_screen_changed,
1070 data->detacher (data->attach_widget, menu);
1071 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1072 list = g_list_remove (list, menu);
1074 g_object_set_data_full (G_OBJECT (data->attach_widget), ATTACHED_MENUS, list, (GtkDestroyNotify) g_list_free);
1076 g_object_set_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS, NULL);
1078 if (GTK_WIDGET_REALIZED (menu))
1079 gtk_widget_unrealize (GTK_WIDGET (menu));
1083 /* Fallback title for menu comes from attach widget */
1084 gtk_menu_update_title (menu);
1086 g_object_unref (menu);
1090 gtk_menu_remove (GtkContainer *container,
1094 GtkMenuPrivate *priv;
1096 g_return_if_fail (GTK_IS_MENU (container));
1097 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1099 menu = GTK_MENU (container);
1100 priv = gtk_menu_get_private (menu);
1102 /* Clear out old_active_menu_item if it matches the item we are removing
1104 if (menu->old_active_menu_item == widget)
1106 g_object_unref (menu->old_active_menu_item);
1107 menu->old_active_menu_item = NULL;
1110 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
1111 g_object_set_data (G_OBJECT (widget), ATTACH_INFO_KEY, NULL);
1113 menu_queue_resize (menu);
1119 return g_object_new (GTK_TYPE_MENU, NULL);
1123 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1127 GtkMenu *menu = GTK_MENU (menu_shell);
1128 AttachInfo *ai = get_attach_info (child);
1130 ai->left_attach = -1;
1131 ai->right_attach = -1;
1132 ai->top_attach = -1;
1133 ai->bottom_attach = -1;
1135 if (GTK_WIDGET_REALIZED (menu_shell))
1136 gtk_widget_set_parent_window (child, menu->bin_window);
1138 GTK_MENU_SHELL_CLASS (parent_class)->insert (menu_shell, child, position);
1140 menu_queue_resize (menu);
1144 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1149 widget = GTK_WIDGET (menu);
1155 GdkGCValues gc_values;
1157 menu->tearoff_active = FALSE;
1158 menu->saved_scroll_offset = menu->scroll_offset;
1160 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
1161 gc = gdk_gc_new_with_values (widget->window,
1162 &gc_values, GDK_GC_SUBWINDOW);
1164 gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1166 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
1171 gdk_draw_drawable (pixmap, gc,
1172 menu->tearoff_window->window,
1173 0, 0, 0, 0, -1, -1);
1174 g_object_unref (gc);
1176 gtk_widget_set_size_request (menu->tearoff_window,
1180 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
1181 g_object_unref (pixmap);
1186 popup_grab_on_window (GdkWindow *window,
1187 guint32 activate_time)
1189 if ((gdk_pointer_grab (window, TRUE,
1190 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1191 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1192 GDK_POINTER_MOTION_MASK,
1193 NULL, NULL, activate_time) == 0))
1195 if (gdk_keyboard_grab (window, TRUE,
1196 activate_time) == 0)
1200 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1211 * @menu: a #GtkMenu.
1212 * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
1213 * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
1214 * @func: a user supplied function used to position the menu, or %NULL
1215 * @data: user supplied data to be passed to @func.
1216 * @button: the mouse button which was pressed to initiate the event.
1217 * @activate_time: the time at which the activation event occurred.
1219 * Displays a menu and makes it available for selection. Applications can use
1220 * this function to display context-sensitive menus, and will typically supply
1221 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1222 * parameters. The default menu positioning function will position the menu
1223 * at the current mouse cursor position.
1225 * The @button parameter should be the mouse button pressed to initiate
1226 * the menu popup. If the menu popup was initiated by something other than
1227 * a mouse button press, such as a mouse button release or a keypress,
1228 * @button should be 0.
1230 * The @activate_time parameter should be the time stamp of the event that
1231 * initiated the popup. If such an event is not available, use
1232 * gtk_get_current_event_time() instead.
1236 gtk_menu_popup (GtkMenu *menu,
1237 GtkWidget *parent_menu_shell,
1238 GtkWidget *parent_menu_item,
1239 GtkMenuPositionFunc func,
1242 guint32 activate_time)
1245 GtkWidget *xgrab_shell;
1247 GdkEvent *current_event;
1248 GtkMenuShell *menu_shell;
1250 g_return_if_fail (GTK_IS_MENU (menu));
1252 widget = GTK_WIDGET (menu);
1253 menu_shell = GTK_MENU_SHELL (menu);
1255 menu_shell->parent_menu_shell = parent_menu_shell;
1257 /* Find the last viewable ancestor, and make an X grab on it
1259 parent = GTK_WIDGET (menu);
1263 gboolean viewable = TRUE;
1264 GtkWidget *tmp = parent;
1268 if (!GTK_WIDGET_MAPPED (tmp))
1277 xgrab_shell = parent;
1279 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1282 /* We want to receive events generated when we map the menu; unfortunately,
1283 * since there is probably already an implicit grab in place from the
1284 * button that the user used to pop up the menu, we won't receive then --
1285 * in particular, the EnterNotify when the menu pops up under the pointer.
1287 * If we are grabbing on a parent menu shell, no problem; just grab on
1288 * that menu shell first before popping up the window with owner_events = TRUE.
1290 * When grabbing on the menu itself, things get more convuluted - we
1291 * we do an explicit grab on a specially created window with
1292 * owner_events = TRUE, which we override further down with a grab
1293 * on the menu. (We can't grab on the menu until it is mapped; we
1294 * probably could just leave the grab on the other window, with a
1295 * little reorganization of the code in gtkmenu*).
1297 if (xgrab_shell && xgrab_shell != widget)
1299 if (popup_grab_on_window (xgrab_shell->window, activate_time))
1300 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1304 GdkWindow *transfer_window;
1306 xgrab_shell = widget;
1307 transfer_window = menu_grab_transfer_window_get (menu);
1308 if (popup_grab_on_window (transfer_window, activate_time))
1309 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1312 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1314 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1315 * with a stuck up window, we just abort here. Presumably the user will
1318 menu_shell->parent_menu_shell = NULL;
1319 menu_grab_transfer_window_destroy (menu);
1323 menu_shell->active = TRUE;
1324 menu_shell->button = button;
1326 /* If we are popping up the menu from something other than, a button
1327 * press then, as a heuristic, we ignore enter events for the menu
1328 * until we get a MOTION_NOTIFY.
1331 current_event = gtk_get_current_event ();
1334 if ((current_event->type != GDK_BUTTON_PRESS) &&
1335 (current_event->type != GDK_ENTER_NOTIFY))
1336 menu_shell->ignore_enter = TRUE;
1338 gdk_event_free (current_event);
1341 menu_shell->ignore_enter = TRUE;
1345 gtk_menu_tearoff_bg_copy (menu);
1347 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1350 if (parent_menu_shell)
1352 GtkWidget *toplevel;
1354 toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1355 if (GTK_IS_WINDOW (toplevel))
1356 gtk_window_group_add_window (_gtk_window_get_group (GTK_WINDOW (toplevel)),
1357 GTK_WINDOW (menu->toplevel));
1360 menu->parent_menu_item = parent_menu_item;
1361 menu->position_func = func;
1362 menu->position_func_data = data;
1363 menu_shell->activate_time = activate_time;
1365 /* We need to show the menu here rather in the init function because
1366 * code expects to be able to tell if the menu is onscreen by
1367 * looking at the GTK_WIDGET_VISIBLE (menu)
1369 gtk_widget_show (GTK_WIDGET (menu));
1371 /* Position the menu, possibly changing the size request
1373 gtk_menu_position (menu);
1375 /* Compute the size of the toplevel and realize it so we
1376 * can scroll correctly.
1379 GtkRequisition tmp_request;
1380 GtkAllocation tmp_allocation = { 0, };
1382 gtk_widget_size_request (menu->toplevel, &tmp_request);
1384 tmp_allocation.width = tmp_request.width;
1385 tmp_allocation.height = tmp_request.height;
1387 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1389 gtk_widget_realize (GTK_WIDGET (menu));
1392 gtk_menu_scroll_to (menu, menu->scroll_offset);
1394 /* Once everything is set up correctly, map the toplevel window on
1397 gtk_widget_show (menu->toplevel);
1399 if (xgrab_shell == widget)
1400 popup_grab_on_window (widget->window, activate_time); /* Should always succeed */
1401 gtk_grab_add (GTK_WIDGET (menu));
1405 gtk_menu_popdown (GtkMenu *menu)
1407 GtkMenuPrivate *private;
1408 GtkMenuShell *menu_shell;
1410 g_return_if_fail (GTK_IS_MENU (menu));
1412 menu_shell = GTK_MENU_SHELL (menu);
1413 private = gtk_menu_get_private (menu);
1415 menu_shell->parent_menu_shell = NULL;
1416 menu_shell->active = FALSE;
1417 menu_shell->ignore_enter = FALSE;
1419 private->have_position = FALSE;
1421 gtk_menu_stop_scrolling (menu);
1423 gtk_menu_stop_navigating_submenu (menu);
1425 if (menu_shell->active_menu_item)
1427 if (menu->old_active_menu_item)
1428 g_object_unref (menu->old_active_menu_item);
1429 menu->old_active_menu_item = menu_shell->active_menu_item;
1430 g_object_ref (menu->old_active_menu_item);
1433 gtk_menu_shell_deselect (menu_shell);
1435 /* The X Grab, if present, will automatically be removed when we hide
1437 gtk_widget_hide (menu->toplevel);
1438 gtk_window_group_add_window (_gtk_window_get_group (NULL), GTK_WINDOW (menu->toplevel));
1442 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1444 if (GTK_BIN (menu->toplevel)->child)
1446 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1450 /* We popped up the menu from the tearoff, so we need to
1451 * release the grab - we aren't actually hiding the menu.
1453 if (menu_shell->have_xgrab)
1455 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
1457 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1458 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1462 /* gtk_menu_popdown is called each time a menu item is selected from
1463 * a torn off menu. Only scroll back to the saved position if the
1464 * non-tearoff menu was popped down.
1466 if (!menu->tearoff_active)
1467 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1468 menu->tearoff_active = TRUE;
1471 gtk_widget_hide (GTK_WIDGET (menu));
1473 menu_shell->have_xgrab = FALSE;
1474 gtk_grab_remove (GTK_WIDGET (menu));
1476 menu_grab_transfer_window_destroy (menu);
1480 gtk_menu_get_active (GtkMenu *menu)
1485 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1487 if (!menu->old_active_menu_item)
1490 children = GTK_MENU_SHELL (menu)->children;
1494 child = children->data;
1495 children = children->next;
1497 if (GTK_BIN (child)->child)
1502 menu->old_active_menu_item = child;
1503 if (menu->old_active_menu_item)
1504 g_object_ref (menu->old_active_menu_item);
1507 return menu->old_active_menu_item;
1511 gtk_menu_set_active (GtkMenu *menu,
1517 g_return_if_fail (GTK_IS_MENU (menu));
1519 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1522 child = tmp_list->data;
1523 if (GTK_BIN (child)->child)
1525 if (menu->old_active_menu_item)
1526 g_object_unref (menu->old_active_menu_item);
1527 menu->old_active_menu_item = child;
1528 g_object_ref (menu->old_active_menu_item);
1534 gtk_menu_set_accel_group (GtkMenu *menu,
1535 GtkAccelGroup *accel_group)
1537 g_return_if_fail (GTK_IS_MENU (menu));
1539 if (menu->accel_group != accel_group)
1541 if (menu->accel_group)
1542 g_object_unref (menu->accel_group);
1543 menu->accel_group = accel_group;
1544 if (menu->accel_group)
1545 g_object_ref (menu->accel_group);
1546 _gtk_menu_refresh_accel_paths (menu, TRUE);
1551 gtk_menu_get_accel_group (GtkMenu *menu)
1553 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1555 return menu->accel_group;
1559 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1562 /* Menu items chain here to figure whether they can activate their
1563 * accelerators. Unlike ordinary widgets, menus allow accel
1564 * activation even if invisible since that's the usual case for
1565 * submenus/popup-menus. however, the state of the attach widget
1566 * affects the "activeness" of the menu.
1568 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1571 return gtk_widget_can_activate_accel (awidget, signal_id);
1573 return GTK_WIDGET_IS_SENSITIVE (widget);
1577 * gtk_menu_set_accel_path
1578 * @menu: a valid #GtkMenu
1579 * @accel_path: a valid accelerator path
1581 * Sets an accelerator path for this menu from which accelerator paths
1582 * for its immediate children, its menu items, can be constructed.
1583 * The main purpose of this function is to spare the programmer the
1584 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1585 * each menu item that should support runtime user changable accelerators.
1586 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1587 * each menu item of this menu, that contains a label describing its purpose,
1588 * automatically gets an accel path assigned. For example, a menu containing
1589 * menu items "New" and "Exit", will, after
1590 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1591 * has been called, assign its items the accel paths:
1592 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1593 * Assigning accel paths to menu items then enables the user to change
1594 * their accelerators at runtime. More details about accelerator paths
1595 * and their default setups can be found at gtk_accel_map_add_entry().
1598 gtk_menu_set_accel_path (GtkMenu *menu,
1599 const gchar *accel_path)
1601 g_return_if_fail (GTK_IS_MENU (menu));
1603 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1605 g_free (menu->accel_path);
1606 menu->accel_path = g_strdup (accel_path);
1607 if (menu->accel_path)
1608 _gtk_menu_refresh_accel_paths (menu, FALSE);
1613 gboolean group_changed;
1617 refresh_accel_paths_foreach (GtkWidget *widget,
1620 AccelPropagation *prop = data;
1622 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1623 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1624 prop->menu->accel_path,
1625 prop->menu->accel_group,
1626 prop->group_changed);
1630 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1631 gboolean group_changed)
1633 g_return_if_fail (GTK_IS_MENU (menu));
1635 if (menu->accel_path && menu->accel_group)
1637 AccelPropagation prop;
1640 prop.group_changed = group_changed;
1641 gtk_container_foreach (GTK_CONTAINER (menu),
1642 refresh_accel_paths_foreach,
1648 gtk_menu_reposition (GtkMenu *menu)
1650 g_return_if_fail (GTK_IS_MENU (menu));
1652 if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1653 gtk_menu_position (menu);
1657 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1660 g_return_if_fail (GTK_IS_MENU (menu));
1662 if (adjustment->value != menu->scroll_offset)
1663 gtk_menu_scroll_to (menu, adjustment->value);
1667 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1670 GdkGeometry geometry_hints;
1672 if (!menu->tearoff_window)
1675 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1677 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1678 width += menu->tearoff_scrollbar->requisition.width;
1681 geometry_hints.min_width = width;
1682 geometry_hints.max_width = width;
1684 geometry_hints.min_height = 0;
1685 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1687 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1690 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1694 gtk_menu_update_title (GtkMenu *menu)
1696 if (menu->tearoff_window)
1699 GtkWidget *attach_widget;
1701 title = gtk_menu_get_title (menu);
1704 attach_widget = gtk_menu_get_attach_widget (menu);
1705 if (GTK_IS_MENU_ITEM (attach_widget))
1707 GtkWidget *child = GTK_BIN (attach_widget)->child;
1708 if (GTK_IS_LABEL (child))
1709 title = gtk_label_get_text (GTK_LABEL (child));
1714 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1719 gtk_menu_get_toplevel (GtkWidget *menu)
1721 GtkWidget *attach, *toplevel;
1723 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
1725 if (GTK_IS_MENU_ITEM (attach))
1726 attach = attach->parent;
1728 if (GTK_IS_MENU (attach))
1729 return gtk_menu_get_toplevel (attach);
1730 else if (GTK_IS_WIDGET (attach))
1732 toplevel = gtk_widget_get_toplevel (attach);
1733 if (GTK_WIDGET_TOPLEVEL (toplevel))
1741 gtk_menu_set_tearoff_state (GtkMenu *menu,
1746 g_return_if_fail (GTK_IS_MENU (menu));
1748 if (menu->torn_off != torn_off)
1750 menu->torn_off = torn_off;
1751 menu->tearoff_active = torn_off;
1755 if (GTK_WIDGET_VISIBLE (menu))
1756 gtk_menu_popdown (menu);
1758 if (!menu->tearoff_window)
1760 GtkWidget *toplevel;
1762 menu->tearoff_window = gtk_widget_new (GTK_TYPE_WINDOW,
1763 "type", GTK_WINDOW_TOPLEVEL,
1764 "screen", gtk_widget_get_screen (menu->toplevel),
1765 "app_paintable", TRUE,
1769 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1770 GDK_WINDOW_TYPE_HINT_MENU);
1771 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1772 g_signal_connect (menu->tearoff_window, "destroy",
1773 G_CALLBACK (gtk_widget_destroyed), &menu->tearoff_window);
1774 g_signal_connect (menu->tearoff_window, "event",
1775 G_CALLBACK (gtk_menu_window_event), menu);
1777 gtk_menu_update_title (menu);
1779 gtk_widget_realize (menu->tearoff_window);
1781 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
1782 if (toplevel != NULL)
1783 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
1784 GTK_WINDOW (toplevel));
1786 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1787 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1789 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
1790 menu->tearoff_adjustment =
1791 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1793 GTK_WIDGET (menu)->requisition.height,
1797 g_object_connect (menu->tearoff_adjustment,
1798 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1800 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1802 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1803 menu->tearoff_scrollbar,
1806 if (menu->tearoff_adjustment->upper > height)
1807 gtk_widget_show (menu->tearoff_scrollbar);
1809 gtk_widget_show (menu->tearoff_hbox);
1812 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
1814 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
1816 /* Update menu->requisition
1818 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
1820 gtk_menu_set_tearoff_hints (menu, width);
1822 gtk_widget_realize (menu->tearoff_window);
1823 gtk_menu_position (menu);
1825 gtk_widget_show (GTK_WIDGET (menu));
1826 gtk_widget_show (menu->tearoff_window);
1828 gtk_menu_scroll_to (menu, 0);
1833 gtk_widget_hide (GTK_WIDGET (menu));
1834 gtk_widget_hide (menu->tearoff_window);
1835 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1836 gtk_widget_destroy (menu->tearoff_window);
1838 menu->tearoff_window = NULL;
1839 menu->tearoff_hbox = NULL;
1840 menu->tearoff_scrollbar = NULL;
1841 menu->tearoff_adjustment = NULL;
1847 * gtk_menu_get_tearoff_state:
1850 * Returns whether the menu is torn off. See
1851 * gtk_menu_set_tearoff_state ().
1853 * Return value: %TRUE if the menu is currently torn off.
1856 gtk_menu_get_tearoff_state (GtkMenu *menu)
1858 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
1860 return menu->torn_off;
1864 * gtk_menu_set_title:
1866 * @title: a string containing the title for the menu.
1868 * Sets the title string for the menu. The title is displayed when the menu
1869 * is shown as a tearoff menu.
1872 gtk_menu_set_title (GtkMenu *menu,
1875 g_return_if_fail (GTK_IS_MENU (menu));
1878 g_object_set_data_full (G_OBJECT (menu), "gtk-menu-title",
1879 g_strdup (title), (GtkDestroyNotify) g_free);
1881 g_object_set_data (G_OBJECT (menu), "gtk-menu-title", NULL);
1883 gtk_menu_update_title (menu);
1884 g_object_notify (G_OBJECT (menu), "tearoff_title");
1888 * gtk_menu_get_title:
1891 * Returns the title of the menu. See gtk_menu_set_title().
1893 * Return value: the title of the menu, or %NULL if the menu has no
1894 * title set on it. This string is owned by the widget and should
1895 * not be modified or freed.
1897 G_CONST_RETURN gchar *
1898 gtk_menu_get_title (GtkMenu *menu)
1900 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1902 return g_object_get_data (G_OBJECT (menu), "gtk-menu-title");
1906 gtk_menu_reorder_child (GtkMenu *menu,
1910 GtkMenuShell *menu_shell;
1912 g_return_if_fail (GTK_IS_MENU (menu));
1913 g_return_if_fail (GTK_IS_MENU_ITEM (child));
1915 menu_shell = GTK_MENU_SHELL (menu);
1917 if (g_list_find (menu_shell->children, child))
1919 menu_shell->children = g_list_remove (menu_shell->children, child);
1920 menu_shell->children = g_list_insert (menu_shell->children, child, position);
1922 menu_queue_resize (menu);
1927 gtk_menu_style_set (GtkWidget *widget,
1928 GtkStyle *previous_style)
1930 if (GTK_WIDGET_REALIZED (widget))
1932 GtkMenu *menu = GTK_MENU (widget);
1934 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1935 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1936 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1941 gtk_menu_realize (GtkWidget *widget)
1943 GdkWindowAttr attributes;
1944 gint attributes_mask;
1949 guint vertical_padding;
1951 g_return_if_fail (GTK_IS_MENU (widget));
1953 menu = GTK_MENU (widget);
1955 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1957 attributes.window_type = GDK_WINDOW_CHILD;
1958 attributes.x = widget->allocation.x;
1959 attributes.y = widget->allocation.y;
1960 attributes.width = widget->allocation.width;
1961 attributes.height = widget->allocation.height;
1962 attributes.wclass = GDK_INPUT_OUTPUT;
1963 attributes.visual = gtk_widget_get_visual (widget);
1964 attributes.colormap = gtk_widget_get_colormap (widget);
1966 attributes.event_mask = gtk_widget_get_events (widget);
1968 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
1969 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
1971 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1972 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1973 gdk_window_set_user_data (widget->window, widget);
1975 border_width = GTK_CONTAINER (widget)->border_width;
1977 gtk_widget_style_get (GTK_WIDGET (menu),
1978 "vertical-padding", &vertical_padding,
1981 attributes.x = border_width + widget->style->xthickness;
1982 attributes.y = border_width + widget->style->ythickness + vertical_padding;
1983 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
1984 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
1986 if (menu->upper_arrow_visible)
1988 attributes.y += MENU_SCROLL_ARROW_HEIGHT;
1989 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1991 if (menu->lower_arrow_visible)
1992 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1994 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
1995 gdk_window_set_user_data (menu->view_window, menu);
1999 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
2001 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
2002 gdk_window_set_user_data (menu->bin_window, menu);
2004 children = GTK_MENU_SHELL (menu)->children;
2007 child = children->data;
2008 children = children->next;
2010 gtk_widget_set_parent_window (child, menu->bin_window);
2013 widget->style = gtk_style_attach (widget->style, widget->window);
2014 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2015 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2016 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2018 if (GTK_MENU_SHELL (widget)->active_menu_item)
2019 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2020 GTK_MENU_SHELL (widget)->active_menu_item);
2022 gdk_window_show (menu->bin_window);
2023 gdk_window_show (menu->view_window);
2027 gtk_menu_focus (GtkWidget *widget,
2028 GtkDirectionType direction)
2031 * A menu or its menu items cannot have focus
2036 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2039 menu_grab_transfer_window_get (GtkMenu *menu)
2041 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2044 GdkWindowAttr attributes;
2045 gint attributes_mask;
2047 attributes.x = -100;
2048 attributes.y = -100;
2049 attributes.width = 10;
2050 attributes.height = 10;
2051 attributes.window_type = GDK_WINDOW_TEMP;
2052 attributes.wclass = GDK_INPUT_ONLY;
2053 attributes.override_redirect = TRUE;
2054 attributes.event_mask = 0;
2056 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2058 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2059 &attributes, attributes_mask);
2060 gdk_window_set_user_data (window, menu);
2062 gdk_window_show (window);
2064 g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", window);
2071 menu_grab_transfer_window_destroy (GtkMenu *menu)
2073 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2076 gdk_window_set_user_data (window, NULL);
2077 gdk_window_destroy (window);
2078 g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", NULL);
2083 gtk_menu_unrealize (GtkWidget *widget)
2087 g_return_if_fail (GTK_IS_MENU (widget));
2089 menu = GTK_MENU (widget);
2091 menu_grab_transfer_window_destroy (menu);
2093 gdk_window_set_user_data (menu->view_window, NULL);
2094 gdk_window_destroy (menu->view_window);
2095 menu->view_window = NULL;
2097 gdk_window_set_user_data (menu->bin_window, NULL);
2098 gdk_window_destroy (menu->bin_window);
2099 menu->bin_window = NULL;
2101 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
2105 gtk_menu_size_request (GtkWidget *widget,
2106 GtkRequisition *requisition)
2110 GtkMenuShell *menu_shell;
2113 guint max_toggle_size;
2114 guint max_accel_width;
2115 guint vertical_padding;
2116 GtkRequisition child_requisition;
2117 GtkMenuPrivate *priv;
2119 g_return_if_fail (GTK_IS_MENU (widget));
2120 g_return_if_fail (requisition != NULL);
2122 menu = GTK_MENU (widget);
2123 menu_shell = GTK_MENU_SHELL (widget);
2124 priv = gtk_menu_get_private (menu);
2126 requisition->width = 0;
2127 requisition->height = 0;
2129 max_toggle_size = 0;
2130 max_accel_width = 0;
2132 g_free (priv->heights);
2133 priv->heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2134 priv->heights_length = gtk_menu_get_n_rows (menu);
2136 children = menu_shell->children;
2143 child = children->data;
2144 children = children->next;
2146 if (! GTK_WIDGET_VISIBLE (child))
2149 get_effective_child_attach (child, &l, &r, &t, &b);
2151 /* It's important to size_request the child
2152 * before doing the toggle size request, in
2153 * case the toggle size request depends on the size
2154 * request of a child of the child (e.g. for ImageMenuItem)
2157 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
2158 gtk_widget_size_request (child, &child_requisition);
2160 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2161 max_toggle_size = MAX (max_toggle_size, toggle_size);
2162 max_accel_width = MAX (max_accel_width,
2163 GTK_MENU_ITEM (child)->accelerator_width);
2165 part = child_requisition.width / (r - l);
2166 requisition->width = MAX (requisition->width, part);
2168 part = MAX (child_requisition.height, toggle_size) / (b - t);
2169 priv->heights[t] = MAX (priv->heights[t], part);
2172 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2173 requisition->height += priv->heights[i];
2175 requisition->width += max_toggle_size + max_accel_width;
2176 requisition->width *= gtk_menu_get_n_columns (menu);
2177 requisition->width += (GTK_CONTAINER (menu)->border_width +
2178 widget->style->xthickness) * 2;
2180 gtk_widget_style_get (GTK_WIDGET (menu),
2181 "vertical-padding", &vertical_padding,
2183 requisition->height += (GTK_CONTAINER (menu)->border_width + vertical_padding +
2184 widget->style->ythickness) * 2;
2186 menu->toggle_size = max_toggle_size;
2188 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2190 if (menu->tearoff_active)
2191 gtk_menu_set_tearoff_hints (menu, requisition->width);
2195 gtk_menu_size_allocate (GtkWidget *widget,
2196 GtkAllocation *allocation)
2199 GtkMenuShell *menu_shell;
2201 GtkAllocation child_allocation;
2202 GtkRequisition child_requisition;
2203 GtkMenuPrivate *priv;
2207 guint vertical_padding;
2209 g_return_if_fail (GTK_IS_MENU (widget));
2210 g_return_if_fail (allocation != NULL);
2212 menu = GTK_MENU (widget);
2213 menu_shell = GTK_MENU_SHELL (widget);
2214 priv = gtk_menu_get_private (menu);
2216 widget->allocation = *allocation;
2217 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2219 gtk_widget_style_get (GTK_WIDGET (menu),
2220 "vertical-padding", &vertical_padding,
2223 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness;
2224 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness + vertical_padding;
2226 width = MAX (1, allocation->width - x * 2);
2227 height = MAX (1, allocation->height - y * 2);
2229 child_requisition.width -= x * 2;
2230 child_requisition.height -= y * 2;
2232 if (menu_shell->active)
2233 gtk_menu_scroll_to (menu, menu->scroll_offset);
2235 if (menu->upper_arrow_visible && !menu->tearoff_active)
2237 y += MENU_SCROLL_ARROW_HEIGHT;
2238 height -= MENU_SCROLL_ARROW_HEIGHT;
2241 if (menu->lower_arrow_visible && !menu->tearoff_active)
2242 height -= MENU_SCROLL_ARROW_HEIGHT;
2244 if (GTK_WIDGET_REALIZED (widget))
2246 gdk_window_move_resize (widget->window,
2247 allocation->x, allocation->y,
2248 allocation->width, allocation->height);
2250 gdk_window_move_resize (menu->view_window,
2257 if (menu_shell->children)
2259 gint base_width = width / gtk_menu_get_n_columns (menu);
2261 children = menu_shell->children;
2264 child = children->data;
2265 children = children->next;
2267 if (GTK_WIDGET_VISIBLE (child))
2272 get_effective_child_attach (child, &l, &r, &t, &b);
2274 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2277 tmp = gtk_menu_get_n_columns (menu) - l;
2278 l = gtk_menu_get_n_columns (menu) - r;
2282 child_allocation.width = (r - l) * base_width;
2283 child_allocation.height = 0;
2284 child_allocation.x = l * base_width;
2285 child_allocation.y = 0;
2287 for (i = 0; i < b; i++)
2290 child_allocation.y += priv->heights[i];
2292 child_allocation.height += priv->heights[i];
2295 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2298 gtk_widget_size_allocate (child, &child_allocation);
2299 gtk_widget_queue_draw (child);
2303 /* Resize the item window */
2304 if (GTK_WIDGET_REALIZED (widget))
2310 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2311 height += priv->heights[i];
2313 width = gtk_menu_get_n_columns (menu) * base_width;
2314 gdk_window_resize (menu->bin_window, width, height);
2317 if (menu->tearoff_active)
2319 if (allocation->height >= widget->requisition.height)
2321 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2323 gtk_widget_hide (menu->tearoff_scrollbar);
2324 gtk_menu_set_tearoff_hints (menu, allocation->width);
2326 gtk_menu_scroll_to (menu, 0);
2331 menu->tearoff_adjustment->upper = widget->requisition.height;
2332 menu->tearoff_adjustment->page_size = allocation->height;
2334 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2335 menu->tearoff_adjustment->upper)
2338 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2341 gtk_menu_scroll_to (menu, value);
2344 gtk_adjustment_changed (menu->tearoff_adjustment);
2346 if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2348 gtk_widget_show (menu->tearoff_scrollbar);
2349 gtk_menu_set_tearoff_hints (menu, allocation->width);
2357 gtk_menu_paint (GtkWidget *widget,
2358 GdkEventExpose *event)
2362 gint border_x, border_y;
2363 guint vertical_padding;
2365 g_return_if_fail (GTK_IS_MENU (widget));
2367 menu = GTK_MENU (widget);
2369 gtk_widget_style_get (GTK_WIDGET (menu),
2370 "vertical-padding", &vertical_padding,
2373 border_x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness;
2374 border_y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
2375 gdk_drawable_get_size (widget->window, &width, &height);
2377 if (event->window == widget->window)
2379 gint arrow_space = MENU_SCROLL_ARROW_HEIGHT - 2 * widget->style->ythickness;
2380 gint arrow_size = 0.7 * arrow_space;
2382 gtk_paint_box (widget->style,
2386 NULL, widget, "menu",
2388 if (menu->upper_arrow_visible && !menu->tearoff_active)
2390 gtk_paint_box (widget->style,
2392 menu->upper_arrow_prelight ?
2393 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2395 NULL, widget, "menu",
2398 width - 2 * border_x,
2399 MENU_SCROLL_ARROW_HEIGHT);
2401 gtk_paint_arrow (widget->style,
2403 menu->upper_arrow_prelight ?
2404 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2406 NULL, widget, "menu_scroll_arrow_up",
2409 (width - arrow_size ) / 2,
2410 border_y + widget->style->ythickness + (arrow_space - arrow_size)/2,
2411 arrow_size, arrow_size);
2414 if (menu->lower_arrow_visible && !menu->tearoff_active)
2416 gtk_paint_box (widget->style,
2418 menu->lower_arrow_prelight ?
2419 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2421 NULL, widget, "menu",
2423 height - border_y - MENU_SCROLL_ARROW_HEIGHT,
2425 MENU_SCROLL_ARROW_HEIGHT);
2427 gtk_paint_arrow (widget->style,
2429 menu->lower_arrow_prelight ?
2430 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2432 NULL, widget, "menu_scroll_arrow_down",
2435 (width - arrow_size) / 2,
2436 height - border_y - MENU_SCROLL_ARROW_HEIGHT +
2437 widget->style->ythickness + (arrow_space - arrow_size)/2,
2438 arrow_size, arrow_size);
2444 gtk_menu_expose (GtkWidget *widget,
2445 GdkEventExpose *event)
2447 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2448 g_return_val_if_fail (event != NULL, FALSE);
2450 if (GTK_WIDGET_DRAWABLE (widget))
2452 gtk_menu_paint (widget, event);
2454 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
2461 gtk_menu_show (GtkWidget *widget)
2463 GtkMenu *menu = GTK_MENU (widget);
2465 _gtk_menu_refresh_accel_paths (menu, FALSE);
2467 GTK_WIDGET_CLASS (parent_class)->show (widget);
2471 gtk_menu_button_press (GtkWidget *widget,
2472 GdkEventButton *event)
2474 /* Don't pop down the menu for releases over scroll arrows
2476 if (GTK_IS_MENU (widget))
2478 GtkMenu *menu = GTK_MENU (widget);
2480 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2484 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
2488 gtk_menu_button_release (GtkWidget *widget,
2489 GdkEventButton *event)
2491 /* Don't pop down the menu for releases over scroll arrows
2493 if (GTK_IS_MENU (widget))
2495 GtkMenu *menu = GTK_MENU (widget);
2497 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2501 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
2504 static const gchar *
2505 get_accel_path (GtkWidget *menu_item,
2510 GClosure *accel_closure;
2511 GtkAccelGroup *accel_group;
2513 path = _gtk_widget_get_accel_path (menu_item, locked);
2516 path = GTK_MENU_ITEM (menu_item)->accel_path;
2522 label = GTK_BIN (menu_item)->child;
2524 if (GTK_IS_ACCEL_LABEL (label))
2526 g_object_get (label,
2527 "accel_closure", &accel_closure,
2529 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
2531 *locked = accel_group->lock_count > 0;
2540 gtk_menu_key_press (GtkWidget *widget,
2543 GtkMenuShell *menu_shell;
2545 gboolean delete = FALSE;
2546 gboolean can_change_accels;
2547 gchar *accel = NULL;
2548 guint accel_key, accel_mods;
2549 GdkModifierType consumed_modifiers;
2550 GdkDisplay *display;
2552 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2553 g_return_val_if_fail (event != NULL, FALSE);
2555 menu_shell = GTK_MENU_SHELL (widget);
2556 menu = GTK_MENU (widget);
2558 gtk_menu_stop_navigating_submenu (menu);
2560 if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
2563 display = gtk_widget_get_display (widget);
2565 g_object_get (gtk_widget_get_settings (widget),
2566 "gtk-menu-bar-accel", &accel,
2567 "gtk-can-change-accels", &can_change_accels,
2573 GdkModifierType mods = 0;
2574 gboolean handled = FALSE;
2576 gtk_accelerator_parse (accel, &keyval, &mods);
2579 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
2581 /* FIXME this is wrong, needs to be in the global accel resolution
2582 * thing, to properly consider i18n etc., but that probably requires
2583 * AccelGroup changes etc.
2585 if (event->keyval == keyval &&
2586 (mods & event->state) == mods)
2587 gtk_menu_shell_cancel (menu_shell);
2595 switch (event->keyval)
2606 /* Figure out what modifiers went into determining the key symbol */
2607 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
2608 event->hardware_keycode, event->state, event->group,
2609 NULL, NULL, NULL, &consumed_modifiers);
2611 accel_key = gdk_keyval_to_lower (event->keyval);
2612 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
2614 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
2615 * We re-upper case when we match against the keyval, but display and save in caseless form.
2617 if (accel_key != event->keyval)
2618 accel_mods |= GDK_SHIFT_MASK;
2620 /* Modify the accelerators */
2621 if (can_change_accels &&
2622 menu_shell->active_menu_item &&
2623 GTK_BIN (menu_shell->active_menu_item)->child && /* no separators */
2624 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
2625 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
2627 GtkWidget *menu_item = menu_shell->active_menu_item;
2628 gboolean locked, replace_accels = TRUE;
2631 path = get_accel_path (menu_item, &locked);
2632 if (!path || locked)
2634 /* can't change accelerators on menu_items without paths
2635 * (basically, those items are accelerator-locked).
2637 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
2638 gdk_display_beep (display);
2644 /* For the keys that act to delete the current setting, we delete
2645 * the current setting if there is one, otherwise, we set the
2646 * key as the accelerator.
2652 if (gtk_accel_map_lookup_entry (path, &key) &&
2653 (key.accel_key || key.accel_mods))
2659 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
2663 /* we failed, probably because this key is in use and
2666 /* g_print("failed to change\n"); */
2667 gdk_display_beep (display);
2676 gtk_menu_motion_notify (GtkWidget *widget,
2677 GdkEventMotion *event)
2679 GtkWidget *menu_item;
2681 GtkMenuShell *menu_shell;
2683 gboolean need_enter;
2685 if (GTK_IS_MENU (widget))
2686 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2688 /* We received the event for one of two reasons:
2690 * a) We are the active menu, and did gtk_grab_add()
2691 * b) The widget is a child of ours, and the event was propagated
2693 * Since for computation of navigation regions, we want the menu which
2694 * is the parent of the menu item, for a), we need to find that menu,
2695 * which may be different from 'widget'.
2697 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2698 if (!menu_item || !GTK_IS_MENU_ITEM (menu_item) ||
2699 !GTK_IS_MENU (menu_item->parent))
2702 menu_shell = GTK_MENU_SHELL (menu_item->parent);
2703 menu = GTK_MENU (menu_shell);
2705 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
2707 /* Check to see if we are within an active submenu's navigation region
2709 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2712 /* Make sure we pop down if we enter a non-selectable menu item, so we
2713 * don't show a submenu when the cursor is outside the stay-up triangle.
2715 if (!_gtk_menu_item_is_selectable (menu_item))
2717 gtk_menu_shell_deselect (menu_shell);
2723 /* The menu is now sensitive to enter events on its items, but
2724 * was previously sensitive. So we fake an enter event.
2728 menu_shell->ignore_enter = FALSE;
2730 gdk_drawable_get_size (event->window, &width, &height);
2731 if (event->x >= 0 && event->x < width &&
2732 event->y >= 0 && event->y < height)
2734 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2737 send_event->crossing.window = g_object_ref (event->window);
2738 send_event->crossing.time = event->time;
2739 send_event->crossing.send_event = TRUE;
2740 send_event->crossing.x_root = event->x_root;
2741 send_event->crossing.y_root = event->y_root;
2742 send_event->crossing.x = event->x;
2743 send_event->crossing.y = event->y;
2745 /* We send the event to 'widget', the currently active menu,
2746 * instead of 'menu', the menu that the pointer is in. This
2747 * will ensure that the event will be ignored unless the
2748 * menuitem is a child of the active menu or some parent
2749 * menu of the active menu.
2751 result = gtk_widget_event (widget, send_event);
2752 gdk_event_free (send_event);
2762 gtk_menu_scroll_timeout (gpointer data)
2767 gint view_width, view_height;
2769 GDK_THREADS_ENTER ();
2771 menu = GTK_MENU (data);
2772 widget = GTK_WIDGET (menu);
2774 offset = menu->scroll_offset + menu->scroll_step;
2776 /* If we scroll upward and the non-visible top part
2777 * is smaller than the scroll arrow it would be
2778 * pretty stupid to show the arrow and taking more
2779 * screen space than just scrolling to the top.
2781 if ((menu->scroll_step < 0) && (offset < MENU_SCROLL_ARROW_HEIGHT))
2784 /* Don't scroll over the top if we weren't before: */
2785 if ((menu->scroll_offset >= 0) && (offset < 0))
2788 gdk_drawable_get_size (widget->window, &view_width, &view_height);
2790 /* Don't scroll past the bottom if we weren't before: */
2791 if (menu->scroll_offset > 0)
2792 view_height -= MENU_SCROLL_ARROW_HEIGHT;
2794 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
2795 (offset + view_height > widget->requisition.height))
2796 offset = widget->requisition.height - view_height;
2798 gtk_menu_scroll_to (menu, offset);
2800 GDK_THREADS_LEAVE ();
2806 gtk_menu_handle_scrolling (GtkMenu *menu, gboolean enter)
2808 GtkMenuShell *menu_shell;
2814 gboolean scroll_fast = FALSE;
2815 guint vertical_padding;
2817 menu_shell = GTK_MENU_SHELL (menu);
2819 gdk_window_get_pointer (GTK_WIDGET (menu)->window, &x, &y, NULL);
2820 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
2822 gtk_widget_style_get (GTK_WIDGET (menu),
2823 "vertical-padding", &vertical_padding,
2826 border = GTK_CONTAINER (menu)->border_width +
2827 GTK_WIDGET (menu)->style->ythickness + vertical_padding;
2829 if (menu->upper_arrow_visible && !menu->tearoff_active)
2834 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2837 if ((x >= rect.x) && (x < rect.x + rect.width) &&
2838 (y >= rect.y) && (y < rect.y + rect.height))
2841 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
2844 if (enter && in_arrow &&
2845 (!menu->upper_arrow_prelight || menu->scroll_fast != scroll_fast))
2847 menu->upper_arrow_prelight = TRUE;
2848 menu->scroll_fast = scroll_fast;
2849 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2851 /* Deselect the active item so that any submenus are poped down */
2852 gtk_menu_shell_deselect (menu_shell);
2854 gtk_menu_remove_scroll_timeout (menu);
2855 menu->scroll_step = (scroll_fast) ? -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
2856 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2857 gtk_menu_scroll_timeout,
2860 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
2862 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2864 gtk_menu_stop_scrolling (menu);
2868 if (menu->lower_arrow_visible && !menu->tearoff_active)
2871 rect.y = height - border - MENU_SCROLL_ARROW_HEIGHT;
2873 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2876 if ((x >= rect.x) && (x < rect.x + rect.width) &&
2877 (y >= rect.y) && (y < rect.y + rect.height))
2880 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
2883 if (enter && in_arrow &&
2884 (!menu->lower_arrow_prelight || menu->scroll_fast != scroll_fast))
2886 menu->lower_arrow_prelight = TRUE;
2887 menu->scroll_fast = scroll_fast;
2888 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2890 /* Deselect the active item so that any submenus are poped down */
2891 gtk_menu_shell_deselect (menu_shell);
2893 gtk_menu_remove_scroll_timeout (menu);
2894 menu->scroll_step = (scroll_fast) ? MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
2895 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2896 gtk_menu_scroll_timeout,
2899 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
2901 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2903 gtk_menu_stop_scrolling (menu);
2909 gtk_menu_enter_notify (GtkWidget *widget,
2910 GdkEventCrossing *event)
2912 GtkWidget *menu_item;
2914 if (widget && GTK_IS_MENU (widget))
2916 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
2918 if (!menu_shell->ignore_enter)
2919 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2922 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
2923 * will not correspond to the event widget's parent. Check to see
2924 * if we are in the parent's navigation region.
2926 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2927 if (menu_item && GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
2928 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent), event->x_root, event->y_root))
2931 return GTK_WIDGET_CLASS (parent_class)->enter_notify_event (widget, event);
2935 gtk_menu_leave_notify (GtkWidget *widget,
2936 GdkEventCrossing *event)
2938 GtkMenuShell *menu_shell;
2940 GtkMenuItem *menu_item;
2941 GtkWidget *event_widget;
2943 menu = GTK_MENU (widget);
2944 menu_shell = GTK_MENU_SHELL (widget);
2946 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2949 gtk_menu_handle_scrolling (menu, FALSE);
2951 event_widget = gtk_get_event_widget ((GdkEvent*) event);
2953 if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
2956 menu_item = GTK_MENU_ITEM (event_widget);
2958 /* Here we check to see if we're leaving an active menu item with a submenu,
2959 * in which case we enter submenu navigation mode.
2961 if (menu_shell->active_menu_item != NULL
2962 && menu_item->submenu != NULL
2963 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
2965 if (GTK_MENU_SHELL (menu_item->submenu)->active)
2967 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
2970 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
2972 /* We are leaving an active menu item with nonactive submenu.
2973 * Deselect it so we don't surprise the user with by popping
2974 * up a submenu _after_ he left the item.
2976 gtk_menu_shell_deselect (menu_shell);
2981 return GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event);
2985 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
2987 if (menu->navigation_region)
2989 gdk_region_destroy (menu->navigation_region);
2990 menu->navigation_region = NULL;
2993 if (menu->navigation_timeout)
2995 g_source_remove (menu->navigation_timeout);
2996 menu->navigation_timeout = 0;
3000 /* When the timeout is elapsed, the navigation region is destroyed
3001 * and the menuitem under the pointer (if any) is selected.
3004 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
3006 GtkMenu *menu = user_data;
3007 GdkWindow *child_window;
3009 GDK_THREADS_ENTER ();
3011 gtk_menu_stop_navigating_submenu (menu);
3013 if (GTK_WIDGET_REALIZED (menu))
3015 child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
3019 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3021 send_event->crossing.window = g_object_ref (child_window);
3022 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
3023 send_event->crossing.send_event = TRUE;
3025 GTK_WIDGET_CLASS (parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
3027 gdk_event_free (send_event);
3031 GDK_THREADS_LEAVE ();
3037 gtk_menu_navigating_submenu (GtkMenu *menu,
3041 if (menu->navigation_region)
3043 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
3047 gtk_menu_stop_navigating_submenu (menu);
3054 #undef DRAW_STAY_UP_TRIANGLE
3056 #ifdef DRAW_STAY_UP_TRIANGLE
3059 draw_stay_up_triangle (GdkWindow *window,
3062 /* Draw ugly color all over the stay-up triangle */
3063 GdkColor ugly_color = { 0, 50000, 10000, 10000 };
3064 GdkGCValues gc_values;
3066 GdkRectangle clipbox;
3068 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
3069 ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
3070 gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
3071 gdk_gc_set_clip_region (ugly_gc, region);
3073 gdk_region_get_clipbox (region, &clipbox);
3075 gdk_draw_rectangle (window,
3078 clipbox.x, clipbox.y,
3079 clipbox.width, clipbox.height);
3081 g_object_unref (ugly_gc);
3086 flip_region (GdkRegion *region,
3091 GdkRectangle *rectangles;
3092 GdkRectangle clipbox;
3093 GdkRegion *new_region;
3096 new_region = gdk_region_new ();
3098 gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
3099 gdk_region_get_clipbox (region, &clipbox);
3101 for (i = 0; i < n_rectangles; ++i)
3103 GdkRectangle rect = rectangles[i];
3106 rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
3109 rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
3111 gdk_region_union_with_rect (new_region, &rect);
3114 g_free (rectangles);
3120 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
3121 GtkMenuItem *menu_item,
3122 GdkEventCrossing *event)
3124 gint submenu_left = 0;
3125 gint submenu_right = 0;
3126 gint submenu_top = 0;
3127 gint submenu_bottom = 0;
3131 GtkWidget *event_widget;
3133 g_return_if_fail (menu_item->submenu != NULL);
3134 g_return_if_fail (event != NULL);
3136 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3138 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
3139 gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
3141 submenu_right = submenu_left + width;
3142 submenu_bottom = submenu_top + height;
3144 gdk_drawable_get_size (event_widget->window, &width, &height);
3146 if (event->x >= 0 && event->x < width)
3149 gboolean flip_y = FALSE;
3150 gboolean flip_x = FALSE;
3152 gtk_menu_stop_navigating_submenu (menu);
3154 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
3157 point[0].x = event->x_root;
3158 point[1].x = submenu_left;
3163 point[0].x = event->x_root + 1;
3164 point[1].x = 2 * (event->x_root + 1) - submenu_right;
3172 point[0].y = event->y_root + 1;
3173 point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
3175 if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
3183 point[0].y = event->y_root;
3184 point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
3186 if (point[0].y >= submenu_bottom)
3190 point[2].x = point[1].x;
3191 point[2].y = point[0].y;
3193 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
3195 if (flip_x || flip_y)
3197 GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
3198 gdk_region_destroy (menu->navigation_region);
3199 menu->navigation_region = new_region;
3202 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3203 "gtk-menu-popdown-delay", &popdown_delay,
3206 menu->navigation_timeout = g_timeout_add (popdown_delay,
3207 gtk_menu_stop_navigating_submenu_cb, menu);
3209 #ifdef DRAW_STAY_UP_TRIANGLE
3210 draw_stay_up_triangle (gdk_get_default_root_window(),
3211 menu->navigation_region);
3217 gtk_menu_deactivate (GtkMenuShell *menu_shell)
3221 g_return_if_fail (GTK_IS_MENU (menu_shell));
3223 parent = menu_shell->parent_menu_shell;
3225 menu_shell->activate_time = 0;
3226 gtk_menu_popdown (GTK_MENU (menu_shell));
3229 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
3233 gtk_menu_position (GtkMenu *menu)
3236 GtkRequisition requisition;
3237 GtkMenuPrivate *private;
3243 GdkScreen *pointer_screen;
3244 GdkRectangle monitor;
3246 g_return_if_fail (GTK_IS_MENU (menu));
3248 widget = GTK_WIDGET (menu);
3250 screen = gtk_widget_get_screen (widget);
3251 gdk_display_get_pointer (gdk_screen_get_display (screen),
3252 &pointer_screen, &x, &y, NULL);
3254 /* We need the requisition to figure out the right place to
3255 * popup the menu. In fact, we always need to ask here, since
3256 * if a size_request was queued while we weren't popped up,
3257 * the requisition won't have been recomputed yet.
3259 gtk_widget_size_request (widget, &requisition);
3261 if (pointer_screen != screen)
3263 /* Pointer is on a different screen; roughly center the
3264 * menu on the screen. If someone was using multiscreen
3265 * + Xinerama together they'd probably want something
3266 * fancier; but that is likely to be vanishingly rare.
3268 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
3269 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
3272 private = gtk_menu_get_private (menu);
3273 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
3277 if (menu->position_func)
3279 (* menu->position_func) (menu, &x, &y, &push_in, menu->position_func_data);
3280 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3284 gint space_left, space_right, space_above, space_below;
3287 gint xthickness = widget->style->xthickness;
3288 gint ythickness = widget->style->ythickness;
3289 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
3291 /* The placement of popup menus horizontally works like this (with
3292 * RTL in parentheses)
3294 * - If there is enough room to the right (left) of the mouse cursor,
3295 * position the menu there.
3297 * - Otherwise, if if there is enough room to the left (right) of the
3298 * mouse cursor, position the menu there.
3300 * - Otherwise if the menu is smaller than the monitor, position it
3301 * on the side of the mouse cursor that has the most space available
3303 * - Otherwise (if there is simply not enough room for the menu on the
3304 * monitor), position it as far left (right) as possible.
3306 * Positioning in the vertical direction is similar: first try below
3307 * mouse cursor, then above.
3309 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3311 space_left = x - monitor.x;
3312 space_right = monitor.x + monitor.width - x - 1;
3313 space_above = y - monitor.y;
3314 space_below = monitor.y + monitor.height - y - 1;
3316 /* position horizontally */
3318 /* the amount of space we need to position the menu. Note the
3319 * menu is offset "xthickness" pixels
3321 needed_width = requisition.width - xthickness;
3323 if (needed_width <= space_left ||
3324 needed_width <= space_right)
3326 if ((rtl && needed_width <= space_left) ||
3327 (!rtl && needed_width > space_right))
3330 x = x + xthickness - requisition.width + 1;
3334 /* position right */
3338 /* x is clamped on-screen further down */
3340 else if (requisition.width <= monitor.width)
3342 /* the menu is too big to fit on either side of the mouse
3343 * cursor, but smaller than the monitor. Position it on
3344 * the side that has the most space
3346 if (space_left > space_right)
3354 x = monitor.x + monitor.width - requisition.width;
3357 else /* menu is simply too big for the monitor */
3362 x = monitor.x + monitor.width - requisition.width;
3371 /* Position vertically. The algorithm is the same as above, but
3372 * simpler because we don't have to take RTL into account.
3374 needed_height = requisition.height - ythickness;
3376 if (needed_height <= space_above ||
3377 needed_height <= space_below)
3379 if (needed_height <= space_below)
3382 y = y + ythickness - requisition.height + 1;
3384 y = CLAMP (y, monitor.y,
3385 monitor.y + monitor.height - requisition.height);
3387 else if (needed_height > space_below && needed_height > space_above)
3389 if (space_below >= space_above)
3390 y = monitor.y + monitor.height - requisition.height;
3404 menu_height = GTK_WIDGET (menu)->requisition.height;
3406 if (y + menu_height > monitor.y + monitor.height)
3408 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
3409 y = (monitor.y + monitor.height) - menu_height;
3414 scroll_offset += monitor.y - y;
3419 /* FIXME: should this be done in the various position_funcs ? */
3420 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
3422 if (GTK_MENU_SHELL (menu)->active)
3424 private->have_position = TRUE;
3429 if (y + requisition.height > monitor.y + monitor.height)
3430 requisition.height = (monitor.y + monitor.height) - y;
3434 scroll_offset += monitor.y - y;
3435 requisition.height -= monitor.y - y;
3439 if (scroll_offset > 0)
3440 scroll_offset += MENU_SCROLL_ARROW_HEIGHT;
3442 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
3445 if (!GTK_MENU_SHELL (menu)->active)
3447 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
3448 requisition.width, requisition.height);
3451 menu->scroll_offset = scroll_offset;
3455 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
3457 if (menu->timeout_id)
3459 g_source_remove (menu->timeout_id);
3460 menu->timeout_id = 0;
3465 gtk_menu_stop_scrolling (GtkMenu *menu)
3467 gtk_menu_remove_scroll_timeout (menu);
3469 menu->upper_arrow_prelight = FALSE;
3470 menu->lower_arrow_prelight = FALSE;
3474 gtk_menu_scroll_to (GtkMenu *menu,
3479 gint view_width, view_height;
3481 gboolean last_visible;
3483 guint vertical_padding;
3485 widget = GTK_WIDGET (menu);
3487 if (menu->tearoff_active &&
3488 menu->tearoff_adjustment &&
3489 (menu->tearoff_adjustment->value != offset))
3491 menu->tearoff_adjustment->value =
3493 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
3494 gtk_adjustment_value_changed (menu->tearoff_adjustment);
3497 /* Move/resize the viewport according to arrows: */
3498 view_width = widget->allocation.width;
3499 view_height = widget->allocation.height;
3501 gtk_widget_style_get (GTK_WIDGET (menu),
3502 "vertical-padding", &vertical_padding,
3505 border_width = GTK_CONTAINER (menu)->border_width;
3506 view_width -= (border_width + widget->style->xthickness) * 2;
3507 view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
3508 menu_height = widget->requisition.height -
3509 (border_width + widget->style->ythickness + vertical_padding) * 2;
3511 x = border_width + widget->style->xthickness;
3512 y = border_width + widget->style->ythickness + vertical_padding;
3514 if (!menu->tearoff_active)
3516 last_visible = menu->upper_arrow_visible;
3517 menu->upper_arrow_visible = offset > 0;
3519 if (menu->upper_arrow_visible)
3520 view_height -= MENU_SCROLL_ARROW_HEIGHT;
3522 if ( (last_visible != menu->upper_arrow_visible) &&
3523 !menu->upper_arrow_visible)
3525 menu->upper_arrow_prelight = FALSE;
3527 /* If we hid the upper arrow, possibly remove timeout */
3528 if (menu->scroll_step < 0)
3530 gtk_menu_stop_scrolling (menu);
3531 gtk_widget_queue_draw (GTK_WIDGET (menu));
3535 last_visible = menu->lower_arrow_visible;
3536 menu->lower_arrow_visible = offset < menu_height - view_height;
3538 if (menu->lower_arrow_visible)
3539 view_height -= MENU_SCROLL_ARROW_HEIGHT;
3541 if ( (last_visible != menu->lower_arrow_visible) &&
3542 !menu->lower_arrow_visible)
3544 menu->lower_arrow_prelight = FALSE;
3546 /* If we hid the lower arrow, possibly remove timeout */
3547 if (menu->scroll_step > 0)
3549 gtk_menu_stop_scrolling (menu);
3550 gtk_widget_queue_draw (GTK_WIDGET (menu));
3554 if (menu->upper_arrow_visible)
3555 y += MENU_SCROLL_ARROW_HEIGHT;
3558 /* Scroll the menu: */
3559 if (GTK_WIDGET_REALIZED (menu))
3560 gdk_window_move (menu->bin_window, 0, -offset);
3562 if (GTK_WIDGET_REALIZED (menu))
3563 gdk_window_move_resize (menu->view_window,
3569 menu->scroll_offset = offset;
3573 compute_child_offset (GtkMenu *menu,
3574 GtkWidget *menu_item,
3577 gboolean *is_last_child)
3579 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3580 guint item_top_attach;
3581 guint item_bottom_attach;
3582 gint child_offset = 0;
3585 get_effective_child_attach (menu_item, NULL, NULL,
3586 &item_top_attach, &item_bottom_attach);
3588 /* there is a possibility that we get called before _size_request, so
3589 * check the height table for safety.
3591 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
3594 /* when we have a row with only invisible children, it's height will
3595 * be zero, so there's no need to check WIDGET_VISIBLE here
3597 for (i = 0; i < item_top_attach; i++)
3598 child_offset += priv->heights[i];
3601 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
3603 *offset = child_offset;
3605 *height = priv->heights[item_top_attach];
3611 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
3612 GtkWidget *menu_item)
3615 gint child_offset, child_height;
3619 gboolean last_child = 0;
3621 menu = GTK_MENU (menu_shell);
3623 /* We need to check if the selected item fully visible.
3624 * If not we need to scroll the menu so that it becomes fully
3628 if (compute_child_offset (menu, menu_item,
3629 &child_offset, &child_height, &last_child))
3631 guint vertical_padding;
3633 y = menu->scroll_offset;
3634 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3636 gtk_widget_style_get (GTK_WIDGET (menu),
3637 "vertical-padding", &vertical_padding,
3640 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
3642 if (child_offset < y)
3644 /* Ignore the enter event we might get if the pointer is on the menu
3646 menu_shell->ignore_enter = TRUE;
3647 gtk_menu_scroll_to (menu, child_offset);
3652 if (menu->upper_arrow_visible && !menu->tearoff_active)
3653 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3654 if (menu->lower_arrow_visible && !menu->tearoff_active)
3655 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3657 if (child_offset + child_height > y + height - arrow_height)
3660 if (!last_child && !menu->tearoff_active)
3661 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3663 y = child_offset + child_height - height + arrow_height;
3664 if ((y > 0) && !menu->tearoff_active)
3666 /* Need upper arrow */
3667 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3668 y = child_offset + child_height - height + arrow_height;
3670 /* Ignore the enter event we might get if the pointer is on the menu
3672 menu_shell->ignore_enter = TRUE;
3673 gtk_menu_scroll_to (menu, y);
3681 gtk_menu_select_item (GtkMenuShell *menu_shell,
3682 GtkWidget *menu_item)
3684 GtkMenu *menu = GTK_MENU (menu_shell);
3686 if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
3687 gtk_menu_scroll_item_visible (menu_shell, menu_item);
3689 GTK_MENU_SHELL_CLASS (parent_class)->select_item (menu_shell, menu_item);
3693 /* Reparent the menu, taking care of the refcounting
3695 * If unrealize is true we force a unrealize while reparenting the parent.
3696 * This can help eliminate flicker in some cases.
3698 * What happens is that when the menu is unrealized and then re-realized,
3699 * the allocations are as follows:
3701 * parent - 1x1 at (0,0)
3702 * child1 - 100x20 at (0,0)
3703 * child2 - 100x20 at (0,20)
3704 * child3 - 100x20 at (0,40)
3706 * That is, the parent is small but the children are full sized. Then,
3707 * when the queued_resize gets processed, the parent gets resized to
3710 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
3711 * contains the following logic:
3713 * - if a move or resize operation on a window would change the clip
3714 * region on the children, then before the window is resized
3715 * the background for children is temporarily set to None, the
3716 * move/resize done, and the background for the children restored.
3718 * So, at the point where the parent is resized to final size, the
3719 * background for the children is temporarily None, and thus they
3720 * are not cleared to the background color and the previous background
3721 * (the image of the menu) is left in place.
3724 gtk_menu_reparent (GtkMenu *menu,
3725 GtkWidget *new_parent,
3728 GtkObject *object = GTK_OBJECT (menu);
3729 GtkWidget *widget = GTK_WIDGET (menu);
3730 gboolean was_floating = GTK_OBJECT_FLOATING (object);
3732 g_object_ref (object);
3733 gtk_object_sink (object);
3737 g_object_ref (object);
3738 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
3739 gtk_container_add (GTK_CONTAINER (new_parent), widget);
3740 g_object_unref (object);
3743 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
3746 GTK_OBJECT_SET_FLAGS (object, GTK_FLOATING);
3748 g_object_unref (object);
3752 gtk_menu_show_all (GtkWidget *widget)
3754 /* Show children, but not self. */
3755 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
3760 gtk_menu_hide_all (GtkWidget *widget)
3762 /* Hide children, but not self. */
3763 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
3767 * gtk_menu_set_screen:
3768 * @menu: a #GtkMenu.
3769 * @screen: a #GdkScreen, or %NULL if the screen should be
3770 * determined by the widget the menu is attached to.
3772 * Sets the #GdkScreen on which the menu will be displayed.
3777 gtk_menu_set_screen (GtkMenu *menu,
3780 g_return_if_fail (GTK_IS_MENU (menu));
3781 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
3783 g_object_set_data (G_OBJECT (menu), "gtk-menu-explicit-screen", screen);
3787 menu_change_screen (menu, screen);
3791 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
3793 attach_widget_screen_changed (attach_widget, NULL, menu);
3799 * @menu: a #GtkMenu.
3800 * @child: a #GtkMenuItem.
3801 * @left_attach: The column number to attach the left side of the item to.
3802 * @right_attach: The column number to attach the right side of the item to.
3803 * @top_attach: The row number to attach the top of the item to.
3804 * @bottom_attach: The row number to attach the bottom of the item to.
3806 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
3807 * an item will occupy is specified by @left_attach, @right_attach,
3808 * @top_attach and @bottom_attach. These each represent the leftmost,
3809 * rightmost, uppermost and lower column and row numbers of the table.
3810 * (Columns and rows are indexed from zero).
3812 * Note that this function is not related to gtk_menu_detach().
3817 gtk_menu_attach (GtkMenu *menu,
3822 guint bottom_attach)
3824 GtkMenuShell *menu_shell;
3826 g_return_if_fail (GTK_IS_MENU (menu));
3827 g_return_if_fail (GTK_IS_MENU_ITEM (child));
3828 g_return_if_fail (child->parent == NULL ||
3829 child->parent == GTK_WIDGET (menu));
3830 g_return_if_fail (left_attach < right_attach);
3831 g_return_if_fail (top_attach < bottom_attach);
3833 menu_shell = GTK_MENU_SHELL (menu);
3837 AttachInfo *ai = get_attach_info (child);
3839 ai->left_attach = left_attach;
3840 ai->right_attach = right_attach;
3841 ai->top_attach = top_attach;
3842 ai->bottom_attach = bottom_attach;
3844 menu_shell->children = g_list_append (menu_shell->children, child);
3846 gtk_widget_set_parent (child, GTK_WIDGET (menu));
3848 menu_queue_resize (menu);
3852 gtk_container_child_set (GTK_CONTAINER (child->parent), child,
3853 "left_attach", left_attach,
3854 "right_attach", right_attach,
3855 "top_attach", top_attach,
3856 "bottom_attach", bottom_attach,
3862 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
3866 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
3867 "gtk-menu-popup-delay", &popup_delay,
3874 find_child_containing (GtkMenuShell *menu_shell,
3882 /* find a child which includes the area given by
3883 * left, right, top, bottom.
3886 for (list = menu_shell->children; list; list = list->next)
3890 if (!_gtk_menu_item_is_selectable (list->data))
3893 get_effective_child_attach (list->data, &l, &r, &t, &b);
3895 if (l <= left && right <= r
3896 && t <= top && bottom <= b)
3897 return GTK_WIDGET (list->data);
3904 gtk_menu_move_current (GtkMenuShell *menu_shell,
3905 GtkMenuDirectionType direction)
3907 GtkMenu *menu = GTK_MENU (menu_shell);
3909 /* use special table menu key bindings */
3910 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
3914 gboolean rtl = (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL);
3915 GtkWidget *match = NULL;
3917 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
3919 if (direction == GTK_MENU_DIR_NEXT)
3921 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
3923 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
3931 for (i = 0; i < t; i++)
3933 match = find_child_containing (menu_shell,
3934 l, l + 1, i, i + 1);
3940 else if (direction == GTK_MENU_DIR_PREV)
3942 for (i = t; i > 0; i--)
3944 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
3952 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
3954 match = find_child_containing (menu_shell,
3955 l, l + 1, i - 1, i);
3961 else if ((!rtl && direction == GTK_MENU_DIR_PARENT)
3962 || (rtl && direction == GTK_MENU_DIR_CHILD))
3964 /* we go one left if possible */
3966 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
3970 GtkWidget *parent = menu_shell->parent_menu_shell;
3973 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
3974 match = menu_shell->active_menu_item;
3977 else if ((!rtl && direction == GTK_MENU_DIR_CHILD)
3978 || (rtl && direction == GTK_MENU_DIR_PARENT))
3980 /* we go one right if possible */
3981 if (r < gtk_menu_get_n_columns (menu))
3982 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
3986 GtkWidget *parent = menu_shell->parent_menu_shell;
3988 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
3990 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
3991 match = menu_shell->active_menu_item;
3997 gtk_menu_shell_select_item (menu_shell, match);
4002 GTK_MENU_SHELL_CLASS (parent_class)->move_current (menu_shell, direction);
4006 get_visible_size (GtkMenu *menu)
4008 GtkWidget *widget = GTK_WIDGET (menu);
4009 GtkContainer *container = GTK_CONTAINER (menu);
4011 gint menu_height = (widget->allocation.height
4012 - 2 * (container->border_width
4013 + widget->style->ythickness));
4015 if (menu->upper_arrow_visible && !menu->tearoff_active)
4016 menu_height -= MENU_SCROLL_ARROW_HEIGHT;
4017 if (menu->lower_arrow_visible && !menu->tearoff_active)
4018 menu_height -= MENU_SCROLL_ARROW_HEIGHT;
4023 /* Find the sensitive on-screen child containing @y, or if none,
4024 * the nearest selectable onscreen child. (%NULL if none)
4027 child_at (GtkMenu *menu,
4030 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4031 GtkWidget *child = NULL;
4032 gint child_offset = 0;
4035 gint lower, upper; /* Onscreen bounds */
4037 menu_height = get_visible_size (menu);
4038 lower = menu->scroll_offset;
4039 upper = menu->scroll_offset + menu_height;
4041 for (children = menu_shell->children; children; children = children->next)
4043 if (GTK_WIDGET_VISIBLE (children->data))
4045 GtkRequisition child_requisition;
4047 gtk_widget_size_request (children->data, &child_requisition);
4049 if (_gtk_menu_item_is_selectable (children->data) &&
4050 child_offset >= lower &&
4051 child_offset + child_requisition.height <= upper)
4053 child = children->data;
4055 if (child_offset + child_requisition.height > y &&
4056 !GTK_IS_TEAROFF_MENU_ITEM (child))
4060 child_offset += child_requisition.height;
4068 get_menu_height (GtkMenu *menu)
4071 GtkWidget *widget = GTK_WIDGET (menu);
4073 height = widget->requisition.height;
4074 height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
4076 if (menu->upper_arrow_visible && !menu->tearoff_active)
4077 height -= MENU_SCROLL_ARROW_HEIGHT;
4079 if (menu->lower_arrow_visible && !menu->tearoff_active)
4080 height -= MENU_SCROLL_ARROW_HEIGHT;
4086 gtk_menu_real_move_scroll (GtkMenu *menu,
4089 gint page_size = get_visible_size (menu);
4090 gint end_position = get_menu_height (menu);
4091 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4095 case GTK_SCROLL_PAGE_UP:
4096 case GTK_SCROLL_PAGE_DOWN:
4100 gint child_offset = 0;
4101 gboolean old_upper_arrow_visible;
4104 if (type == GTK_SCROLL_PAGE_UP)
4109 if (menu_shell->active_menu_item)
4113 compute_child_offset (menu, menu_shell->active_menu_item,
4114 &child_offset, &child_height, NULL);
4115 child_offset += child_height / 2;
4118 menu_shell->ignore_enter = TRUE;
4119 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4120 old_offset = menu->scroll_offset;
4122 new_offset = menu->scroll_offset + step;
4123 new_offset = CLAMP (new_offset, 0, end_position - page_size);
4125 gtk_menu_scroll_to (menu, new_offset);
4127 if (menu_shell->active_menu_item)
4129 GtkWidget *new_child;
4130 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4132 if (menu->scroll_offset != old_offset)
4133 step = menu->scroll_offset - old_offset;
4135 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * MENU_SCROLL_ARROW_HEIGHT;
4137 new_child = child_at (menu, child_offset + step);
4139 gtk_menu_shell_select_item (menu_shell, new_child);
4143 case GTK_SCROLL_START:
4144 /* Ignore the enter event we might get if the pointer is on the menu
4146 menu_shell->ignore_enter = TRUE;
4147 gtk_menu_scroll_to (menu, 0);
4148 gtk_menu_shell_select_first (menu_shell, TRUE);
4150 case GTK_SCROLL_END:
4151 /* Ignore the enter event we might get if the pointer is on the menu
4153 menu_shell->ignore_enter = TRUE;
4154 gtk_menu_scroll_to (menu, end_position - page_size);
4155 _gtk_menu_shell_select_last (menu_shell, TRUE);
4164 * gtk_menu_set_monitor:
4166 * @monitor_num: the number of the monitor on which the menu should
4169 * Informs GTK+ on which monitor a menu should be popped up.
4170 * See gdk_screen_get_monitor_geometry().
4172 * This function should be called from a #GtkMenuPositionFunc if the
4173 * menu should not appear on the same monitor as the pointer. This
4174 * information can't be reliably inferred from the coordinates returned
4175 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
4176 * may extend beyond the monitor boundaries or even the screen boundaries.
4181 gtk_menu_set_monitor (GtkMenu *menu,
4184 GtkMenuPrivate *priv;
4185 g_return_if_fail (GTK_IS_MENU (menu));
4187 priv = gtk_menu_get_private (menu);
4189 priv->monitor_num = monitor_num;
4193 * gtk_menu_get_for_attach_widget:
4194 * @widget: a #GtkWidget
4196 * Returns a list of the menus which are attached to this widget.
4197 * This list is owned by GTK+ and must not be modified.
4199 * Return value: the list of menus attached to his widget.
4204 gtk_menu_get_for_attach_widget (GtkWidget *widget)
4208 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
4210 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);