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"
45 #include "gtkprivate.h"
50 #define MENU_ITEM_CLASS(w) GTK_MENU_ITEM_GET_CLASS (w)
52 #define DEFAULT_POPUP_DELAY 225
53 #define DEFAULT_POPDOWN_DELAY 1000
55 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
56 * extends below the submenu
59 #define MENU_SCROLL_STEP1 8
60 #define MENU_SCROLL_STEP2 15
61 #define MENU_SCROLL_ARROW_HEIGHT 16
62 #define MENU_SCROLL_FAST_ZONE 8
63 #define MENU_SCROLL_TIMEOUT1 50
64 #define MENU_SCROLL_TIMEOUT2 20
66 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
67 #define ATTACHED_MENUS "gtk-attached-menus"
69 typedef struct _GtkMenuAttachData GtkMenuAttachData;
70 typedef struct _GtkMenuPrivate GtkMenuPrivate;
72 struct _GtkMenuAttachData
74 GtkWidget *attach_widget;
75 GtkMenuDetachFunc detacher;
78 struct _GtkMenuPrivate
80 gboolean seen_item_enter;
82 gboolean have_position;
86 /* info used for the table */
92 /* Cached layout information */
106 gint effective_left_attach;
107 gint effective_right_attach;
108 gint effective_top_attach;
109 gint effective_bottom_attach;
125 CHILD_PROP_LEFT_ATTACH,
126 CHILD_PROP_RIGHT_ATTACH,
127 CHILD_PROP_TOP_ATTACH,
128 CHILD_PROP_BOTTOM_ATTACH
131 static void gtk_menu_class_init (GtkMenuClass *klass);
132 static void gtk_menu_init (GtkMenu *menu);
133 static void gtk_menu_set_property (GObject *object,
137 static void gtk_menu_get_property (GObject *object,
141 static void gtk_menu_set_child_property(GtkContainer *container,
146 static void gtk_menu_get_child_property(GtkContainer *container,
151 static void gtk_menu_destroy (GtkObject *object);
152 static void gtk_menu_finalize (GObject *object);
153 static void gtk_menu_realize (GtkWidget *widget);
154 static void gtk_menu_unrealize (GtkWidget *widget);
155 static void gtk_menu_size_request (GtkWidget *widget,
156 GtkRequisition *requisition);
157 static void gtk_menu_size_allocate (GtkWidget *widget,
158 GtkAllocation *allocation);
159 static void gtk_menu_paint (GtkWidget *widget,
160 GdkEventExpose *expose);
161 static void gtk_menu_show (GtkWidget *widget);
162 static gboolean gtk_menu_expose (GtkWidget *widget,
163 GdkEventExpose *event);
164 static gboolean gtk_menu_key_press (GtkWidget *widget,
166 static gboolean gtk_menu_scroll (GtkWidget *widget,
167 GdkEventScroll *event);
168 static gboolean gtk_menu_button_press (GtkWidget *widget,
169 GdkEventButton *event);
170 static gboolean gtk_menu_button_release (GtkWidget *widget,
171 GdkEventButton *event);
172 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
173 GdkEventMotion *event);
174 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
175 GdkEventCrossing *event);
176 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
177 GdkEventCrossing *event);
178 static void gtk_menu_scroll_to (GtkMenu *menu,
180 static void gtk_menu_grab_notify (GtkWidget *widget,
181 gboolean was_grabbed);
183 static void gtk_menu_stop_scrolling (GtkMenu *menu);
184 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
185 static gboolean gtk_menu_scroll_timeout (gpointer data);
187 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
188 GtkWidget *menu_item);
189 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
190 GtkWidget *menu_item);
191 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
194 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
196 static void gtk_menu_handle_scrolling (GtkMenu *menu,
200 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
202 static void gtk_menu_style_set (GtkWidget *widget,
203 GtkStyle *previous_style);
204 static gboolean gtk_menu_focus (GtkWidget *widget,
205 GtkDirectionType direction);
206 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
207 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
208 GtkMenuDirectionType direction);
209 static void gtk_menu_real_move_scroll (GtkMenu *menu,
212 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
213 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
214 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
217 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
218 GtkMenuItem *menu_item,
219 GdkEventCrossing *event);
221 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
222 static void gtk_menu_show_all (GtkWidget *widget);
223 static void gtk_menu_hide_all (GtkWidget *widget);
224 static void gtk_menu_position (GtkMenu *menu);
225 static void gtk_menu_reparent (GtkMenu *menu,
226 GtkWidget *new_parent,
228 static void gtk_menu_remove (GtkContainer *menu,
231 static void gtk_menu_update_title (GtkMenu *menu);
233 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
234 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
236 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
238 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
239 gboolean group_changed);
241 static GtkMenuShellClass *parent_class = NULL;
242 static const gchar attach_data_key[] = "gtk-menu-attach-data";
244 static guint menu_signals[LAST_SIGNAL] = { 0 };
246 static GtkMenuPrivate *
247 gtk_menu_get_private (GtkMenu *menu)
249 return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
253 gtk_menu_get_type (void)
255 static GType menu_type = 0;
259 static const GTypeInfo menu_info =
261 sizeof (GtkMenuClass),
262 NULL, /* base_init */
263 NULL, /* base_finalize */
264 (GClassInitFunc) gtk_menu_class_init,
265 NULL, /* class_finalize */
266 NULL, /* class_data */
269 (GInstanceInitFunc) gtk_menu_init,
272 menu_type = g_type_register_static (GTK_TYPE_MENU_SHELL, I_("GtkMenu"),
280 menu_queue_resize (GtkMenu *menu)
282 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
284 priv->have_layout = FALSE;
285 gtk_widget_queue_resize (GTK_WIDGET (menu));
289 get_attach_info (GtkWidget *child)
291 GObject *object = G_OBJECT (child);
292 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
296 ai = g_new0 (AttachInfo, 1);
297 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai, g_free);
304 is_grid_attached (AttachInfo *ai)
306 return (ai->left_attach >= 0 &&
307 ai->right_attach >= 0 &&
308 ai->top_attach >= 0 &&
309 ai->bottom_attach >= 0);
313 menu_ensure_layout (GtkMenu *menu)
315 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
317 if (!priv->have_layout)
319 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
323 gint max_right_attach;
324 gint max_bottom_attach;
326 /* Find extents of gridded portion
328 max_right_attach = 1;
329 max_bottom_attach = 0;
331 for (l = menu_shell->children; l; l = l->next)
333 GtkWidget *child = l->data;
334 AttachInfo *ai = get_attach_info (child);
336 if (is_grid_attached (ai))
338 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
339 max_right_attach = MAX (max_right_attach, ai->right_attach);
345 row_occupied = g_malloc0 (max_bottom_attach);
347 for (l = menu_shell->children; l; l = l->next)
349 GtkWidget *child = l->data;
350 AttachInfo *ai = get_attach_info (child);
352 if (is_grid_attached (ai))
356 for (i = ai->top_attach; i < ai->bottom_attach; i++)
357 row_occupied[i] = TRUE;
361 /* Lay non-grid-items out in those rows
364 for (l = menu_shell->children; l; l = l->next)
366 GtkWidget *child = l->data;
367 AttachInfo *ai = get_attach_info (child);
369 if (!is_grid_attached (ai))
371 while (current_row < max_bottom_attach && row_occupied[current_row])
374 ai->effective_left_attach = 0;
375 ai->effective_right_attach = max_right_attach;
376 ai->effective_top_attach = current_row;
377 ai->effective_bottom_attach = current_row + 1;
383 ai->effective_left_attach = ai->left_attach;
384 ai->effective_right_attach = ai->right_attach;
385 ai->effective_top_attach = ai->top_attach;
386 ai->effective_bottom_attach = ai->bottom_attach;
390 g_free (row_occupied);
392 priv->n_rows = MAX (current_row, max_bottom_attach);
393 priv->n_columns = max_right_attach;
394 priv->have_layout = TRUE;
400 gtk_menu_get_n_columns (GtkMenu *menu)
402 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
404 menu_ensure_layout (menu);
406 return priv->n_columns;
410 gtk_menu_get_n_rows (GtkMenu *menu)
412 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
414 menu_ensure_layout (menu);
420 get_effective_child_attach (GtkWidget *child,
426 GtkMenu *menu = GTK_MENU (child->parent);
429 menu_ensure_layout (menu);
431 ai = get_attach_info (child);
434 *l = ai->effective_left_attach;
436 *r = ai->effective_right_attach;
438 *t = ai->effective_top_attach;
440 *b = ai->effective_bottom_attach;
445 gtk_menu_class_init (GtkMenuClass *class)
447 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
448 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
449 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
450 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
451 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
452 GtkBindingSet *binding_set;
454 parent_class = g_type_class_peek_parent (class);
456 gobject_class->finalize = gtk_menu_finalize;
457 gobject_class->set_property = gtk_menu_set_property;
458 gobject_class->get_property = gtk_menu_get_property;
460 object_class->destroy = gtk_menu_destroy;
462 widget_class->realize = gtk_menu_realize;
463 widget_class->unrealize = gtk_menu_unrealize;
464 widget_class->size_request = gtk_menu_size_request;
465 widget_class->size_allocate = gtk_menu_size_allocate;
466 widget_class->show = gtk_menu_show;
467 widget_class->expose_event = gtk_menu_expose;
468 widget_class->scroll_event = gtk_menu_scroll;
469 widget_class->key_press_event = gtk_menu_key_press;
470 widget_class->button_press_event = gtk_menu_button_press;
471 widget_class->button_release_event = gtk_menu_button_release;
472 widget_class->motion_notify_event = gtk_menu_motion_notify;
473 widget_class->show_all = gtk_menu_show_all;
474 widget_class->hide_all = gtk_menu_hide_all;
475 widget_class->enter_notify_event = gtk_menu_enter_notify;
476 widget_class->leave_notify_event = gtk_menu_leave_notify;
477 widget_class->style_set = gtk_menu_style_set;
478 widget_class->focus = gtk_menu_focus;
479 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
480 widget_class->grab_notify = gtk_menu_grab_notify;
482 container_class->remove = gtk_menu_remove;
483 container_class->get_child_property = gtk_menu_get_child_property;
484 container_class->set_child_property = gtk_menu_set_child_property;
486 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
487 menu_shell_class->deactivate = gtk_menu_deactivate;
488 menu_shell_class->select_item = gtk_menu_select_item;
489 menu_shell_class->insert = gtk_menu_real_insert;
490 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
491 menu_shell_class->move_current = gtk_menu_move_current;
493 menu_signals[MOVE_SCROLL] =
494 _gtk_binding_signal_new (I_("move_scroll"),
495 G_OBJECT_CLASS_TYPE (object_class),
496 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
497 G_CALLBACK (gtk_menu_real_move_scroll),
499 _gtk_marshal_VOID__ENUM,
501 GTK_TYPE_SCROLL_TYPE);
503 g_object_class_install_property (gobject_class,
505 g_param_spec_string ("tearoff-title",
507 P_("A title that may be displayed by the window manager when this menu is torn-off"),
509 GTK_PARAM_READWRITE));
512 * GtkMenu:tearoff-state:
514 * A boolean that indicates whether the menu is torn-off.
518 g_object_class_install_property (gobject_class,
520 g_param_spec_boolean ("tearoff-state",
522 P_("A boolean that indicates whether the menu is torn-off"),
524 GTK_PARAM_READWRITE));
526 gtk_widget_class_install_style_property (widget_class,
527 g_param_spec_int ("vertical-padding",
528 P_("Vertical Padding"),
529 P_("Extra space at the top and bottom of the menu"),
533 GTK_PARAM_READABLE));
535 gtk_widget_class_install_style_property (widget_class,
536 g_param_spec_int ("horizontal-padding",
537 P_("Horizontal Padding"),
538 P_("Extra space at the left and right edges of the menu"),
542 GTK_PARAM_READABLE));
544 gtk_widget_class_install_style_property (widget_class,
545 g_param_spec_int ("vertical-offset",
546 P_("Vertical Offset"),
547 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
551 GTK_PARAM_READABLE));
553 gtk_widget_class_install_style_property (widget_class,
554 g_param_spec_int ("horizontal-offset",
555 P_("Horizontal Offset"),
556 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
560 GTK_PARAM_READABLE));
563 gtk_container_class_install_child_property (container_class,
564 CHILD_PROP_LEFT_ATTACH,
565 g_param_spec_int ("left-attach",
567 P_("The column number to attach the left side of the child to"),
569 GTK_PARAM_READWRITE));
571 gtk_container_class_install_child_property (container_class,
572 CHILD_PROP_RIGHT_ATTACH,
573 g_param_spec_int ("right-attach",
575 P_("The column number to attach the right side of the child to"),
577 GTK_PARAM_READWRITE));
579 gtk_container_class_install_child_property (container_class,
580 CHILD_PROP_TOP_ATTACH,
581 g_param_spec_int ("top-attach",
583 P_("The row number to attach the top of the child to"),
585 GTK_PARAM_READWRITE));
587 gtk_container_class_install_child_property (container_class,
588 CHILD_PROP_BOTTOM_ATTACH,
589 g_param_spec_int ("bottom-attach",
591 P_("The row number to attach the bottom of the child to"),
593 GTK_PARAM_READWRITE));
595 binding_set = gtk_binding_set_by_class (class);
596 gtk_binding_entry_add_signal (binding_set,
598 I_("move_current"), 1,
599 GTK_TYPE_MENU_DIRECTION_TYPE,
601 gtk_binding_entry_add_signal (binding_set,
604 GTK_TYPE_MENU_DIRECTION_TYPE,
606 gtk_binding_entry_add_signal (binding_set,
609 GTK_TYPE_MENU_DIRECTION_TYPE,
611 gtk_binding_entry_add_signal (binding_set,
614 GTK_TYPE_MENU_DIRECTION_TYPE,
616 gtk_binding_entry_add_signal (binding_set,
619 GTK_TYPE_MENU_DIRECTION_TYPE,
620 GTK_MENU_DIR_PARENT);
621 gtk_binding_entry_add_signal (binding_set,
624 GTK_TYPE_MENU_DIRECTION_TYPE,
625 GTK_MENU_DIR_PARENT);
626 gtk_binding_entry_add_signal (binding_set,
629 GTK_TYPE_MENU_DIRECTION_TYPE,
631 gtk_binding_entry_add_signal (binding_set,
634 GTK_TYPE_MENU_DIRECTION_TYPE,
636 gtk_binding_entry_add_signal (binding_set,
639 GTK_TYPE_SCROLL_TYPE,
641 gtk_binding_entry_add_signal (binding_set,
644 GTK_TYPE_SCROLL_TYPE,
646 gtk_binding_entry_add_signal (binding_set,
649 GTK_TYPE_SCROLL_TYPE,
651 gtk_binding_entry_add_signal (binding_set,
654 GTK_TYPE_SCROLL_TYPE,
656 gtk_binding_entry_add_signal (binding_set,
659 GTK_TYPE_SCROLL_TYPE,
661 gtk_binding_entry_add_signal (binding_set,
664 GTK_TYPE_SCROLL_TYPE,
666 gtk_binding_entry_add_signal (binding_set,
669 GTK_TYPE_SCROLL_TYPE,
670 GTK_SCROLL_PAGE_DOWN);
671 gtk_binding_entry_add_signal (binding_set,
674 GTK_TYPE_SCROLL_TYPE,
675 GTK_SCROLL_PAGE_DOWN);
677 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
678 P_("Can change accelerators"),
679 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
681 GTK_PARAM_READWRITE));
683 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
684 P_("Delay before submenus appear"),
685 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
689 GTK_PARAM_READWRITE));
691 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
692 P_("Delay before hiding a submenu"),
693 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
696 DEFAULT_POPDOWN_DELAY,
697 GTK_PARAM_READWRITE));
699 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
704 gtk_menu_set_property (GObject *object,
711 menu = GTK_MENU (object);
715 case PROP_TEAROFF_STATE:
716 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
718 case PROP_TEAROFF_TITLE:
719 gtk_menu_set_title (menu, g_value_get_string (value));
722 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
728 gtk_menu_get_property (GObject *object,
735 menu = GTK_MENU (object);
739 case PROP_TEAROFF_STATE:
740 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
742 case PROP_TEAROFF_TITLE:
743 g_value_set_string (value, gtk_menu_get_title (menu));
746 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
752 gtk_menu_set_child_property (GtkContainer *container,
758 GtkMenu *menu = GTK_MENU (container);
759 AttachInfo *ai = get_attach_info (child);
763 case CHILD_PROP_LEFT_ATTACH:
764 ai->left_attach = g_value_get_int (value);
766 case CHILD_PROP_RIGHT_ATTACH:
767 ai->right_attach = g_value_get_int (value);
769 case CHILD_PROP_TOP_ATTACH:
770 ai->top_attach = g_value_get_int (value);
772 case CHILD_PROP_BOTTOM_ATTACH:
773 ai->bottom_attach = g_value_get_int (value);
777 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
781 menu_queue_resize (menu);
785 gtk_menu_get_child_property (GtkContainer *container,
791 AttachInfo *ai = get_attach_info (child);
795 case CHILD_PROP_LEFT_ATTACH:
796 g_value_set_int (value, ai->left_attach);
798 case CHILD_PROP_RIGHT_ATTACH:
799 g_value_set_int (value, ai->right_attach);
801 case CHILD_PROP_TOP_ATTACH:
802 g_value_set_int (value, ai->top_attach);
804 case CHILD_PROP_BOTTOM_ATTACH:
805 g_value_set_int (value, ai->bottom_attach);
809 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
815 gtk_menu_window_event (GtkWidget *window,
819 gboolean handled = FALSE;
821 g_object_ref (window);
827 case GDK_KEY_RELEASE:
828 handled = gtk_widget_event (menu, event);
834 g_object_unref (window);
835 g_object_unref (menu);
841 gtk_menu_window_size_request (GtkWidget *window,
842 GtkRequisition *requisition,
845 GtkMenuPrivate *private = gtk_menu_get_private (menu);
847 if (private->have_position)
849 GdkScreen *screen = gtk_widget_get_screen (window);
850 GdkRectangle monitor;
852 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
854 if (private->y + requisition->height > monitor.y + monitor.height)
855 requisition->height = monitor.y + monitor.height - private->y;
857 if (private->y < monitor.y)
858 requisition->height -= monitor.y - private->y;
863 gtk_menu_init (GtkMenu *menu)
865 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
867 menu->parent_menu_item = NULL;
868 menu->old_active_menu_item = NULL;
869 menu->accel_group = NULL;
870 menu->position_func = NULL;
871 menu->position_func_data = NULL;
872 menu->toggle_size = 0;
874 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
875 "type", GTK_WINDOW_POPUP,
878 "signal::event", gtk_menu_window_event, menu,
879 "signal::size_request", gtk_menu_window_size_request, menu,
880 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
882 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
883 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
885 /* Refloat the menu, so that reference counting for the menu isn't
886 * affected by it being a child of the toplevel
888 g_object_force_floating (G_OBJECT (menu));
889 menu->needs_destruction_ref_count = TRUE;
891 menu->view_window = NULL;
892 menu->bin_window = NULL;
894 menu->scroll_offset = 0;
895 menu->scroll_step = 0;
896 menu->timeout_id = 0;
897 menu->scroll_fast = FALSE;
899 menu->tearoff_window = NULL;
900 menu->tearoff_hbox = NULL;
901 menu->torn_off = FALSE;
902 menu->tearoff_active = FALSE;
903 menu->tearoff_adjustment = NULL;
904 menu->tearoff_scrollbar = NULL;
906 menu->upper_arrow_visible = FALSE;
907 menu->lower_arrow_visible = FALSE;
908 menu->upper_arrow_prelight = FALSE;
909 menu->lower_arrow_prelight = FALSE;
911 priv->have_layout = FALSE;
915 gtk_menu_destroy (GtkObject *object)
918 GtkMenuAttachData *data;
919 GtkMenuPrivate *priv;
921 g_return_if_fail (GTK_IS_MENU (object));
923 menu = GTK_MENU (object);
925 gtk_menu_stop_scrolling (menu);
927 data = g_object_get_data (G_OBJECT (object), attach_data_key);
929 gtk_menu_detach (menu);
931 gtk_menu_stop_navigating_submenu (menu);
933 if (menu->old_active_menu_item)
935 g_object_unref (menu->old_active_menu_item);
936 menu->old_active_menu_item = NULL;
939 /* Add back the reference count for being a child */
940 if (menu->needs_destruction_ref_count)
942 menu->needs_destruction_ref_count = FALSE;
943 g_object_ref (object);
946 if (menu->accel_group)
948 g_object_unref (menu->accel_group);
949 menu->accel_group = NULL;
953 gtk_widget_destroy (menu->toplevel);
955 if (menu->tearoff_window)
956 gtk_widget_destroy (menu->tearoff_window);
958 priv = gtk_menu_get_private (menu);
962 g_free (priv->heights);
963 priv->heights = NULL;
968 g_free (priv->title);
972 GTK_OBJECT_CLASS (parent_class)->destroy (object);
976 gtk_menu_finalize (GObject *object)
978 GtkMenu *menu = GTK_MENU (object);
980 g_free (menu->accel_path);
982 G_OBJECT_CLASS (parent_class)->finalize (object);
986 menu_change_screen (GtkMenu *menu,
987 GdkScreen *new_screen)
989 GtkMenuPrivate *private = gtk_menu_get_private (menu);
991 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
993 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
999 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1000 gtk_menu_position (menu);
1003 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1004 private->monitor_num = -1;
1008 attach_widget_screen_changed (GtkWidget *attach_widget,
1009 GdkScreen *previous_screen,
1012 if (gtk_widget_has_screen (attach_widget) &&
1013 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1015 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1020 gtk_menu_attach_to_widget (GtkMenu *menu,
1021 GtkWidget *attach_widget,
1022 GtkMenuDetachFunc detacher)
1024 GtkMenuAttachData *data;
1027 g_return_if_fail (GTK_IS_MENU (menu));
1028 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1030 /* keep this function in sync with gtk_widget_set_parent()
1033 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1036 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1037 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1041 g_object_ref_sink (menu);
1043 data = g_new (GtkMenuAttachData, 1);
1044 data->attach_widget = attach_widget;
1046 g_signal_connect (attach_widget, "screen_changed",
1047 G_CALLBACK (attach_widget_screen_changed), menu);
1048 attach_widget_screen_changed (attach_widget, NULL, menu);
1050 data->detacher = detacher;
1051 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1052 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1053 if (!g_list_find (list, menu))
1055 list = g_list_prepend (list, menu);
1057 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list, (GtkDestroyNotify) g_list_free);
1059 if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
1060 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1062 /* we don't need to set the style here, since
1063 * we are a toplevel widget.
1066 /* Fallback title for menu comes from attach widget */
1067 gtk_menu_update_title (menu);
1071 gtk_menu_get_attach_widget (GtkMenu *menu)
1073 GtkMenuAttachData *data;
1075 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1077 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1079 return data->attach_widget;
1084 gtk_menu_detach (GtkMenu *menu)
1086 GtkMenuAttachData *data;
1089 g_return_if_fail (GTK_IS_MENU (menu));
1091 /* keep this function in sync with gtk_widget_unparent()
1093 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1096 g_warning ("gtk_menu_detach(): menu is not attached");
1099 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1101 g_signal_handlers_disconnect_by_func (data->attach_widget,
1102 (gpointer) attach_widget_screen_changed,
1106 data->detacher (data->attach_widget, menu);
1107 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1108 list = g_list_remove (list, menu);
1110 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list, (GtkDestroyNotify) g_list_free);
1112 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1114 if (GTK_WIDGET_REALIZED (menu))
1115 gtk_widget_unrealize (GTK_WIDGET (menu));
1119 /* Fallback title for menu comes from attach widget */
1120 gtk_menu_update_title (menu);
1122 g_object_unref (menu);
1126 gtk_menu_remove (GtkContainer *container,
1131 g_return_if_fail (GTK_IS_MENU (container));
1132 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1134 menu = GTK_MENU (container);
1136 /* Clear out old_active_menu_item if it matches the item we are removing
1138 if (menu->old_active_menu_item == widget)
1140 g_object_unref (menu->old_active_menu_item);
1141 menu->old_active_menu_item = NULL;
1144 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
1145 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1147 menu_queue_resize (menu);
1153 return g_object_new (GTK_TYPE_MENU, NULL);
1157 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1161 GtkMenu *menu = GTK_MENU (menu_shell);
1162 AttachInfo *ai = get_attach_info (child);
1164 ai->left_attach = -1;
1165 ai->right_attach = -1;
1166 ai->top_attach = -1;
1167 ai->bottom_attach = -1;
1169 if (GTK_WIDGET_REALIZED (menu_shell))
1170 gtk_widget_set_parent_window (child, menu->bin_window);
1172 GTK_MENU_SHELL_CLASS (parent_class)->insert (menu_shell, child, position);
1174 menu_queue_resize (menu);
1178 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1183 widget = GTK_WIDGET (menu);
1189 GdkGCValues gc_values;
1191 menu->tearoff_active = FALSE;
1192 menu->saved_scroll_offset = menu->scroll_offset;
1194 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
1195 gc = gdk_gc_new_with_values (widget->window,
1196 &gc_values, GDK_GC_SUBWINDOW);
1198 gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1200 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
1205 gdk_draw_drawable (pixmap, gc,
1206 menu->tearoff_window->window,
1207 0, 0, 0, 0, -1, -1);
1208 g_object_unref (gc);
1210 gtk_widget_set_size_request (menu->tearoff_window,
1214 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
1215 g_object_unref (pixmap);
1220 popup_grab_on_window (GdkWindow *window,
1221 guint32 activate_time,
1222 gboolean grab_keyboard)
1224 if ((gdk_pointer_grab (window, TRUE,
1225 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1226 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1227 GDK_POINTER_MOTION_MASK,
1228 NULL, NULL, activate_time) == 0))
1230 if (!grab_keyboard ||
1231 gdk_keyboard_grab (window, TRUE,
1232 activate_time) == 0)
1236 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1247 * @menu: a #GtkMenu.
1248 * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
1249 * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
1250 * @func: a user supplied function used to position the menu, or %NULL
1251 * @data: user supplied data to be passed to @func.
1252 * @button: the mouse button which was pressed to initiate the event.
1253 * @activate_time: the time at which the activation event occurred.
1255 * Displays a menu and makes it available for selection. Applications can use
1256 * this function to display context-sensitive menus, and will typically supply
1257 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1258 * parameters. The default menu positioning function will position the menu
1259 * at the current mouse cursor position.
1261 * The @button parameter should be the mouse button pressed to initiate
1262 * the menu popup. If the menu popup was initiated by something other than
1263 * a mouse button press, such as a mouse button release or a keypress,
1264 * @button should be 0.
1266 * The @activate_time parameter should be the time stamp of the event that
1267 * initiated the popup. If such an event is not available, use
1268 * gtk_get_current_event_time() instead.
1272 gtk_menu_popup (GtkMenu *menu,
1273 GtkWidget *parent_menu_shell,
1274 GtkWidget *parent_menu_item,
1275 GtkMenuPositionFunc func,
1278 guint32 activate_time)
1281 GtkWidget *xgrab_shell;
1283 GdkEvent *current_event;
1284 GtkMenuShell *menu_shell;
1285 gboolean grab_keyboard;
1286 GtkMenuPrivate *priv;
1288 g_return_if_fail (GTK_IS_MENU (menu));
1290 widget = GTK_WIDGET (menu);
1291 menu_shell = GTK_MENU_SHELL (menu);
1292 priv = gtk_menu_get_private (menu);
1294 menu_shell->parent_menu_shell = parent_menu_shell;
1296 priv->seen_item_enter = FALSE;
1298 /* Find the last viewable ancestor, and make an X grab on it
1300 parent = GTK_WIDGET (menu);
1304 gboolean viewable = TRUE;
1305 GtkWidget *tmp = parent;
1309 if (!GTK_WIDGET_MAPPED (tmp))
1318 xgrab_shell = parent;
1320 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1323 /* We want to receive events generated when we map the menu; unfortunately,
1324 * since there is probably already an implicit grab in place from the
1325 * button that the user used to pop up the menu, we won't receive then --
1326 * in particular, the EnterNotify when the menu pops up under the pointer.
1328 * If we are grabbing on a parent menu shell, no problem; just grab on
1329 * that menu shell first before popping up the window with owner_events = TRUE.
1331 * When grabbing on the menu itself, things get more convuluted - we
1332 * we do an explicit grab on a specially created window with
1333 * owner_events = TRUE, which we override further down with a grab
1334 * on the menu. (We can't grab on the menu until it is mapped; we
1335 * probably could just leave the grab on the other window, with a
1336 * little reorganization of the code in gtkmenu*).
1338 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1339 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1341 if (xgrab_shell && xgrab_shell != widget)
1343 if (popup_grab_on_window (xgrab_shell->window, activate_time, grab_keyboard))
1344 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1348 GdkWindow *transfer_window;
1350 xgrab_shell = widget;
1351 transfer_window = menu_grab_transfer_window_get (menu);
1352 if (popup_grab_on_window (transfer_window, activate_time, grab_keyboard))
1353 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1356 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1358 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1359 * with a stuck up window, we just abort here. Presumably the user will
1362 menu_shell->parent_menu_shell = NULL;
1363 menu_grab_transfer_window_destroy (menu);
1367 menu_shell->active = TRUE;
1368 menu_shell->button = button;
1370 /* If we are popping up the menu from something other than, a button
1371 * press then, as a heuristic, we ignore enter events for the menu
1372 * until we get a MOTION_NOTIFY.
1375 current_event = gtk_get_current_event ();
1378 if ((current_event->type != GDK_BUTTON_PRESS) &&
1379 (current_event->type != GDK_ENTER_NOTIFY))
1380 menu_shell->ignore_enter = TRUE;
1382 gdk_event_free (current_event);
1385 menu_shell->ignore_enter = TRUE;
1389 gtk_menu_tearoff_bg_copy (menu);
1391 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1394 if (parent_menu_shell)
1396 GtkWidget *toplevel;
1398 toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1399 if (GTK_IS_WINDOW (toplevel))
1400 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1401 GTK_WINDOW (menu->toplevel));
1404 menu->parent_menu_item = parent_menu_item;
1405 menu->position_func = func;
1406 menu->position_func_data = data;
1407 menu_shell->activate_time = activate_time;
1409 /* We need to show the menu here rather in the init function because
1410 * code expects to be able to tell if the menu is onscreen by
1411 * looking at the GTK_WIDGET_VISIBLE (menu)
1413 gtk_widget_show (GTK_WIDGET (menu));
1415 /* Position the menu, possibly changing the size request
1417 gtk_menu_position (menu);
1419 /* Compute the size of the toplevel and realize it so we
1420 * can scroll correctly.
1423 GtkRequisition tmp_request;
1424 GtkAllocation tmp_allocation = { 0, };
1426 gtk_widget_size_request (menu->toplevel, &tmp_request);
1428 tmp_allocation.width = tmp_request.width;
1429 tmp_allocation.height = tmp_request.height;
1431 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1433 gtk_widget_realize (GTK_WIDGET (menu));
1436 gtk_menu_scroll_to (menu, menu->scroll_offset);
1438 /* Once everything is set up correctly, map the toplevel window on
1441 gtk_widget_show (menu->toplevel);
1443 if (xgrab_shell == widget)
1444 popup_grab_on_window (widget->window, activate_time, grab_keyboard); /* Should always succeed */
1445 gtk_grab_add (GTK_WIDGET (menu));
1449 gtk_menu_popdown (GtkMenu *menu)
1451 GtkMenuPrivate *private;
1452 GtkMenuShell *menu_shell;
1454 g_return_if_fail (GTK_IS_MENU (menu));
1456 menu_shell = GTK_MENU_SHELL (menu);
1457 private = gtk_menu_get_private (menu);
1459 menu_shell->parent_menu_shell = NULL;
1460 menu_shell->active = FALSE;
1461 menu_shell->ignore_enter = FALSE;
1463 private->have_position = FALSE;
1465 gtk_menu_stop_scrolling (menu);
1467 gtk_menu_stop_navigating_submenu (menu);
1469 if (menu_shell->active_menu_item)
1471 if (menu->old_active_menu_item)
1472 g_object_unref (menu->old_active_menu_item);
1473 menu->old_active_menu_item = menu_shell->active_menu_item;
1474 g_object_ref (menu->old_active_menu_item);
1477 gtk_menu_shell_deselect (menu_shell);
1479 /* The X Grab, if present, will automatically be removed when we hide
1481 gtk_widget_hide (menu->toplevel);
1482 gtk_window_group_add_window (gtk_window_get_group (NULL), GTK_WINDOW (menu->toplevel));
1486 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1488 if (GTK_BIN (menu->toplevel)->child)
1490 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1494 /* We popped up the menu from the tearoff, so we need to
1495 * release the grab - we aren't actually hiding the menu.
1497 if (menu_shell->have_xgrab)
1499 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
1501 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1502 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1506 /* gtk_menu_popdown is called each time a menu item is selected from
1507 * a torn off menu. Only scroll back to the saved position if the
1508 * non-tearoff menu was popped down.
1510 if (!menu->tearoff_active)
1511 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1512 menu->tearoff_active = TRUE;
1515 gtk_widget_hide (GTK_WIDGET (menu));
1517 menu_shell->have_xgrab = FALSE;
1518 gtk_grab_remove (GTK_WIDGET (menu));
1520 menu_grab_transfer_window_destroy (menu);
1524 gtk_menu_get_active (GtkMenu *menu)
1529 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1531 if (!menu->old_active_menu_item)
1534 children = GTK_MENU_SHELL (menu)->children;
1538 child = children->data;
1539 children = children->next;
1541 if (GTK_BIN (child)->child)
1546 menu->old_active_menu_item = child;
1547 if (menu->old_active_menu_item)
1548 g_object_ref (menu->old_active_menu_item);
1551 return menu->old_active_menu_item;
1555 gtk_menu_set_active (GtkMenu *menu,
1561 g_return_if_fail (GTK_IS_MENU (menu));
1563 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1566 child = tmp_list->data;
1567 if (GTK_BIN (child)->child)
1569 if (menu->old_active_menu_item)
1570 g_object_unref (menu->old_active_menu_item);
1571 menu->old_active_menu_item = child;
1572 g_object_ref (menu->old_active_menu_item);
1578 gtk_menu_set_accel_group (GtkMenu *menu,
1579 GtkAccelGroup *accel_group)
1581 g_return_if_fail (GTK_IS_MENU (menu));
1583 if (menu->accel_group != accel_group)
1585 if (menu->accel_group)
1586 g_object_unref (menu->accel_group);
1587 menu->accel_group = accel_group;
1588 if (menu->accel_group)
1589 g_object_ref (menu->accel_group);
1590 _gtk_menu_refresh_accel_paths (menu, TRUE);
1595 gtk_menu_get_accel_group (GtkMenu *menu)
1597 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1599 return menu->accel_group;
1603 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1606 /* Menu items chain here to figure whether they can activate their
1607 * accelerators. Unlike ordinary widgets, menus allow accel
1608 * activation even if invisible since that's the usual case for
1609 * submenus/popup-menus. however, the state of the attach widget
1610 * affects the "activeness" of the menu.
1612 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1615 return gtk_widget_can_activate_accel (awidget, signal_id);
1617 return GTK_WIDGET_IS_SENSITIVE (widget);
1621 * gtk_menu_set_accel_path
1622 * @menu: a valid #GtkMenu
1623 * @accel_path: a valid accelerator path
1625 * Sets an accelerator path for this menu from which accelerator paths
1626 * for its immediate children, its menu items, can be constructed.
1627 * The main purpose of this function is to spare the programmer the
1628 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1629 * each menu item that should support runtime user changable accelerators.
1630 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1631 * each menu item of this menu, that contains a label describing its purpose,
1632 * automatically gets an accel path assigned. For example, a menu containing
1633 * menu items "New" and "Exit", will, after
1634 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1635 * has been called, assign its items the accel paths:
1636 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1637 * Assigning accel paths to menu items then enables the user to change
1638 * their accelerators at runtime. More details about accelerator paths
1639 * and their default setups can be found at gtk_accel_map_add_entry().
1642 gtk_menu_set_accel_path (GtkMenu *menu,
1643 const gchar *accel_path)
1645 g_return_if_fail (GTK_IS_MENU (menu));
1647 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1649 g_free (menu->accel_path);
1650 menu->accel_path = g_strdup (accel_path);
1651 if (menu->accel_path)
1652 _gtk_menu_refresh_accel_paths (menu, FALSE);
1657 gboolean group_changed;
1661 refresh_accel_paths_foreach (GtkWidget *widget,
1664 AccelPropagation *prop = data;
1666 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1667 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1668 prop->menu->accel_path,
1669 prop->menu->accel_group,
1670 prop->group_changed);
1674 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1675 gboolean group_changed)
1677 g_return_if_fail (GTK_IS_MENU (menu));
1679 if (menu->accel_path && menu->accel_group)
1681 AccelPropagation prop;
1684 prop.group_changed = group_changed;
1685 gtk_container_foreach (GTK_CONTAINER (menu),
1686 refresh_accel_paths_foreach,
1692 gtk_menu_reposition (GtkMenu *menu)
1694 g_return_if_fail (GTK_IS_MENU (menu));
1696 if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1697 gtk_menu_position (menu);
1701 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1704 g_return_if_fail (GTK_IS_MENU (menu));
1706 if (adjustment->value != menu->scroll_offset)
1707 gtk_menu_scroll_to (menu, adjustment->value);
1711 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1714 GdkGeometry geometry_hints;
1716 if (!menu->tearoff_window)
1719 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1721 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1722 width += menu->tearoff_scrollbar->requisition.width;
1725 geometry_hints.min_width = width;
1726 geometry_hints.max_width = width;
1728 geometry_hints.min_height = 0;
1729 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1731 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1734 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1738 gtk_menu_update_title (GtkMenu *menu)
1740 if (menu->tearoff_window)
1743 GtkWidget *attach_widget;
1745 title = gtk_menu_get_title (menu);
1748 attach_widget = gtk_menu_get_attach_widget (menu);
1749 if (GTK_IS_MENU_ITEM (attach_widget))
1751 GtkWidget *child = GTK_BIN (attach_widget)->child;
1752 if (GTK_IS_LABEL (child))
1753 title = gtk_label_get_text (GTK_LABEL (child));
1758 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1763 gtk_menu_get_toplevel (GtkWidget *menu)
1765 GtkWidget *attach, *toplevel;
1767 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
1769 if (GTK_IS_MENU_ITEM (attach))
1770 attach = attach->parent;
1772 if (GTK_IS_MENU (attach))
1773 return gtk_menu_get_toplevel (attach);
1774 else if (GTK_IS_WIDGET (attach))
1776 toplevel = gtk_widget_get_toplevel (attach);
1777 if (GTK_WIDGET_TOPLEVEL (toplevel))
1785 tearoff_window_destroyed (GtkWidget *widget,
1788 gtk_menu_set_tearoff_state (menu, FALSE);
1792 gtk_menu_set_tearoff_state (GtkMenu *menu,
1797 g_return_if_fail (GTK_IS_MENU (menu));
1799 if (menu->torn_off != torn_off)
1801 menu->torn_off = torn_off;
1802 menu->tearoff_active = torn_off;
1806 if (GTK_WIDGET_VISIBLE (menu))
1807 gtk_menu_popdown (menu);
1809 if (!menu->tearoff_window)
1811 GtkWidget *toplevel;
1813 menu->tearoff_window = gtk_widget_new (GTK_TYPE_WINDOW,
1814 "type", GTK_WINDOW_TOPLEVEL,
1815 "screen", gtk_widget_get_screen (menu->toplevel),
1816 "app-paintable", TRUE,
1820 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1821 GDK_WINDOW_TYPE_HINT_MENU);
1822 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1823 g_signal_connect (menu->tearoff_window, "destroy",
1824 G_CALLBACK (tearoff_window_destroyed), menu);
1825 g_signal_connect (menu->tearoff_window, "event",
1826 G_CALLBACK (gtk_menu_window_event), menu);
1828 gtk_menu_update_title (menu);
1830 gtk_widget_realize (menu->tearoff_window);
1832 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
1833 if (toplevel != NULL)
1834 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
1835 GTK_WINDOW (toplevel));
1837 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1838 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1840 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
1841 menu->tearoff_adjustment =
1842 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1844 GTK_WIDGET (menu)->requisition.height,
1848 g_object_connect (menu->tearoff_adjustment,
1849 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1851 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1853 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1854 menu->tearoff_scrollbar,
1857 if (menu->tearoff_adjustment->upper > height)
1858 gtk_widget_show (menu->tearoff_scrollbar);
1860 gtk_widget_show (menu->tearoff_hbox);
1863 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
1865 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
1867 /* Update menu->requisition
1869 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
1871 gtk_menu_set_tearoff_hints (menu, width);
1873 gtk_widget_realize (menu->tearoff_window);
1874 gtk_menu_position (menu);
1876 gtk_widget_show (GTK_WIDGET (menu));
1877 gtk_widget_show (menu->tearoff_window);
1879 gtk_menu_scroll_to (menu, 0);
1884 gtk_widget_hide (GTK_WIDGET (menu));
1885 gtk_widget_hide (menu->tearoff_window);
1886 if (GTK_IS_CONTAINER (menu->toplevel))
1887 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1888 gtk_widget_destroy (menu->tearoff_window);
1890 menu->tearoff_window = NULL;
1891 menu->tearoff_hbox = NULL;
1892 menu->tearoff_scrollbar = NULL;
1893 menu->tearoff_adjustment = NULL;
1896 g_object_notify (G_OBJECT (menu), "tearoff-state");
1901 * gtk_menu_get_tearoff_state:
1904 * Returns whether the menu is torn off. See
1905 * gtk_menu_set_tearoff_state ().
1907 * Return value: %TRUE if the menu is currently torn off.
1910 gtk_menu_get_tearoff_state (GtkMenu *menu)
1912 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
1914 return menu->torn_off;
1918 * gtk_menu_set_title:
1920 * @title: a string containing the title for the menu.
1922 * Sets the title string for the menu. The title is displayed when the menu
1923 * is shown as a tearoff menu.
1926 gtk_menu_set_title (GtkMenu *menu,
1929 GtkMenuPrivate *priv;
1931 g_return_if_fail (GTK_IS_MENU (menu));
1933 priv = gtk_menu_get_private (menu);
1935 if (strcmp (title ? title : "", priv->title ? priv->title : "") != 0)
1937 g_free (priv->title);
1938 priv->title = g_strdup (title);
1940 gtk_menu_update_title (menu);
1941 g_object_notify (G_OBJECT (menu), "tearoff-title");
1946 * gtk_menu_get_title:
1949 * Returns the title of the menu. See gtk_menu_set_title().
1951 * Return value: the title of the menu, or %NULL if the menu has no
1952 * title set on it. This string is owned by the widget and should
1953 * not be modified or freed.
1955 G_CONST_RETURN gchar *
1956 gtk_menu_get_title (GtkMenu *menu)
1958 GtkMenuPrivate *priv;
1960 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1962 priv = gtk_menu_get_private (menu);
1968 gtk_menu_reorder_child (GtkMenu *menu,
1972 GtkMenuShell *menu_shell;
1974 g_return_if_fail (GTK_IS_MENU (menu));
1975 g_return_if_fail (GTK_IS_MENU_ITEM (child));
1977 menu_shell = GTK_MENU_SHELL (menu);
1979 if (g_list_find (menu_shell->children, child))
1981 menu_shell->children = g_list_remove (menu_shell->children, child);
1982 menu_shell->children = g_list_insert (menu_shell->children, child, position);
1984 menu_queue_resize (menu);
1989 gtk_menu_style_set (GtkWidget *widget,
1990 GtkStyle *previous_style)
1992 if (GTK_WIDGET_REALIZED (widget))
1994 GtkMenu *menu = GTK_MENU (widget);
1996 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1997 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1998 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2003 gtk_menu_realize (GtkWidget *widget)
2005 GdkWindowAttr attributes;
2006 gint attributes_mask;
2011 guint vertical_padding;
2012 guint horizontal_padding;
2014 g_return_if_fail (GTK_IS_MENU (widget));
2016 menu = GTK_MENU (widget);
2018 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
2020 attributes.window_type = GDK_WINDOW_CHILD;
2021 attributes.x = widget->allocation.x;
2022 attributes.y = widget->allocation.y;
2023 attributes.width = widget->allocation.width;
2024 attributes.height = widget->allocation.height;
2025 attributes.wclass = GDK_INPUT_OUTPUT;
2026 attributes.visual = gtk_widget_get_visual (widget);
2027 attributes.colormap = gtk_widget_get_colormap (widget);
2029 attributes.event_mask = gtk_widget_get_events (widget);
2031 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2032 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2034 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2035 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
2036 gdk_window_set_user_data (widget->window, widget);
2038 border_width = GTK_CONTAINER (widget)->border_width;
2040 gtk_widget_style_get (GTK_WIDGET (menu),
2041 "vertical-padding", &vertical_padding,
2042 "horizontal-padding", &horizontal_padding,
2045 attributes.x = border_width + widget->style->xthickness + horizontal_padding;
2046 attributes.y = border_width + widget->style->ythickness + vertical_padding;
2047 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
2048 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
2050 if (menu->upper_arrow_visible)
2052 attributes.y += MENU_SCROLL_ARROW_HEIGHT;
2053 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
2055 if (menu->lower_arrow_visible)
2056 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
2058 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
2059 gdk_window_set_user_data (menu->view_window, menu);
2063 attributes.width = MAX (1, widget->allocation.width - (border_width + widget->style->xthickness + horizontal_padding) * 2);
2064 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
2066 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
2067 gdk_window_set_user_data (menu->bin_window, menu);
2069 children = GTK_MENU_SHELL (menu)->children;
2072 child = children->data;
2073 children = children->next;
2075 gtk_widget_set_parent_window (child, menu->bin_window);
2078 widget->style = gtk_style_attach (widget->style, widget->window);
2079 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2080 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2081 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2083 if (GTK_MENU_SHELL (widget)->active_menu_item)
2084 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2085 GTK_MENU_SHELL (widget)->active_menu_item);
2087 gdk_window_show (menu->bin_window);
2088 gdk_window_show (menu->view_window);
2092 gtk_menu_focus (GtkWidget *widget,
2093 GtkDirectionType direction)
2096 * A menu or its menu items cannot have focus
2101 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2104 menu_grab_transfer_window_get (GtkMenu *menu)
2106 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2109 GdkWindowAttr attributes;
2110 gint attributes_mask;
2112 attributes.x = -100;
2113 attributes.y = -100;
2114 attributes.width = 10;
2115 attributes.height = 10;
2116 attributes.window_type = GDK_WINDOW_TEMP;
2117 attributes.wclass = GDK_INPUT_ONLY;
2118 attributes.override_redirect = TRUE;
2119 attributes.event_mask = 0;
2121 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2123 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2124 &attributes, attributes_mask);
2125 gdk_window_set_user_data (window, menu);
2127 gdk_window_show (window);
2129 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2136 menu_grab_transfer_window_destroy (GtkMenu *menu)
2138 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2141 gdk_window_set_user_data (window, NULL);
2142 gdk_window_destroy (window);
2143 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2148 gtk_menu_unrealize (GtkWidget *widget)
2152 g_return_if_fail (GTK_IS_MENU (widget));
2154 menu = GTK_MENU (widget);
2156 menu_grab_transfer_window_destroy (menu);
2158 gdk_window_set_user_data (menu->view_window, NULL);
2159 gdk_window_destroy (menu->view_window);
2160 menu->view_window = NULL;
2162 gdk_window_set_user_data (menu->bin_window, NULL);
2163 gdk_window_destroy (menu->bin_window);
2164 menu->bin_window = NULL;
2166 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
2170 gtk_menu_size_request (GtkWidget *widget,
2171 GtkRequisition *requisition)
2175 GtkMenuShell *menu_shell;
2178 guint max_toggle_size;
2179 guint max_accel_width;
2180 guint vertical_padding;
2181 guint horizontal_padding;
2182 GtkRequisition child_requisition;
2183 GtkMenuPrivate *priv;
2185 g_return_if_fail (GTK_IS_MENU (widget));
2186 g_return_if_fail (requisition != NULL);
2188 menu = GTK_MENU (widget);
2189 menu_shell = GTK_MENU_SHELL (widget);
2190 priv = gtk_menu_get_private (menu);
2192 requisition->width = 0;
2193 requisition->height = 0;
2195 max_toggle_size = 0;
2196 max_accel_width = 0;
2198 g_free (priv->heights);
2199 priv->heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2200 priv->heights_length = gtk_menu_get_n_rows (menu);
2202 children = menu_shell->children;
2209 child = children->data;
2210 children = children->next;
2212 if (! GTK_WIDGET_VISIBLE (child))
2215 get_effective_child_attach (child, &l, &r, &t, &b);
2217 /* It's important to size_request the child
2218 * before doing the toggle size request, in
2219 * case the toggle size request depends on the size
2220 * request of a child of the child (e.g. for ImageMenuItem)
2223 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
2224 gtk_widget_size_request (child, &child_requisition);
2226 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2227 max_toggle_size = MAX (max_toggle_size, toggle_size);
2228 max_accel_width = MAX (max_accel_width,
2229 GTK_MENU_ITEM (child)->accelerator_width);
2231 part = child_requisition.width / (r - l);
2232 requisition->width = MAX (requisition->width, part);
2234 part = MAX (child_requisition.height, toggle_size) / (b - t);
2235 priv->heights[t] = MAX (priv->heights[t], part);
2238 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2239 requisition->height += priv->heights[i];
2241 requisition->width += max_toggle_size + max_accel_width;
2242 requisition->width *= gtk_menu_get_n_columns (menu);
2244 gtk_widget_style_get (GTK_WIDGET (menu),
2245 "vertical-padding", &vertical_padding,
2246 "horizontal-padding", &horizontal_padding,
2249 requisition->width += (GTK_CONTAINER (menu)->border_width + horizontal_padding +
2250 widget->style->xthickness) * 2;
2251 requisition->height += (GTK_CONTAINER (menu)->border_width + vertical_padding +
2252 widget->style->ythickness) * 2;
2254 menu->toggle_size = max_toggle_size;
2256 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2258 if (menu->tearoff_active)
2259 gtk_menu_set_tearoff_hints (menu, requisition->width);
2263 gtk_menu_size_allocate (GtkWidget *widget,
2264 GtkAllocation *allocation)
2267 GtkMenuShell *menu_shell;
2269 GtkAllocation child_allocation;
2270 GtkRequisition child_requisition;
2271 GtkMenuPrivate *priv;
2275 guint vertical_padding;
2276 guint horizontal_padding;
2278 g_return_if_fail (GTK_IS_MENU (widget));
2279 g_return_if_fail (allocation != NULL);
2281 menu = GTK_MENU (widget);
2282 menu_shell = GTK_MENU_SHELL (widget);
2283 priv = gtk_menu_get_private (menu);
2285 widget->allocation = *allocation;
2286 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2288 gtk_widget_style_get (GTK_WIDGET (menu),
2289 "vertical-padding", &vertical_padding,
2290 "horizontal-padding", &horizontal_padding,
2293 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness + horizontal_padding;
2294 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness + vertical_padding;
2296 width = MAX (1, allocation->width - x * 2);
2297 height = MAX (1, allocation->height - y * 2);
2299 child_requisition.width -= x * 2;
2300 child_requisition.height -= y * 2;
2302 if (menu_shell->active)
2303 gtk_menu_scroll_to (menu, menu->scroll_offset);
2305 if (menu->upper_arrow_visible && !menu->tearoff_active)
2307 y += MENU_SCROLL_ARROW_HEIGHT;
2308 height -= MENU_SCROLL_ARROW_HEIGHT;
2311 if (menu->lower_arrow_visible && !menu->tearoff_active)
2312 height -= MENU_SCROLL_ARROW_HEIGHT;
2314 if (GTK_WIDGET_REALIZED (widget))
2316 gdk_window_move_resize (widget->window,
2317 allocation->x, allocation->y,
2318 allocation->width, allocation->height);
2320 gdk_window_move_resize (menu->view_window,
2327 if (menu_shell->children)
2329 gint base_width = width / gtk_menu_get_n_columns (menu);
2331 children = menu_shell->children;
2334 child = children->data;
2335 children = children->next;
2337 if (GTK_WIDGET_VISIBLE (child))
2342 get_effective_child_attach (child, &l, &r, &t, &b);
2344 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2347 tmp = gtk_menu_get_n_columns (menu) - l;
2348 l = gtk_menu_get_n_columns (menu) - r;
2352 child_allocation.width = (r - l) * base_width;
2353 child_allocation.height = 0;
2354 child_allocation.x = l * base_width;
2355 child_allocation.y = 0;
2357 for (i = 0; i < b; i++)
2360 child_allocation.y += priv->heights[i];
2362 child_allocation.height += priv->heights[i];
2365 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2368 gtk_widget_size_allocate (child, &child_allocation);
2369 gtk_widget_queue_draw (child);
2373 /* Resize the item window */
2374 if (GTK_WIDGET_REALIZED (widget))
2380 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2381 height += priv->heights[i];
2383 width = gtk_menu_get_n_columns (menu) * base_width;
2384 gdk_window_resize (menu->bin_window, width, height);
2387 if (menu->tearoff_active)
2389 if (allocation->height >= widget->requisition.height)
2391 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2393 gtk_widget_hide (menu->tearoff_scrollbar);
2394 gtk_menu_set_tearoff_hints (menu, allocation->width);
2396 gtk_menu_scroll_to (menu, 0);
2401 menu->tearoff_adjustment->upper = widget->requisition.height;
2402 menu->tearoff_adjustment->page_size = allocation->height;
2404 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2405 menu->tearoff_adjustment->upper)
2408 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2411 gtk_menu_scroll_to (menu, value);
2414 gtk_adjustment_changed (menu->tearoff_adjustment);
2416 if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2418 gtk_widget_show (menu->tearoff_scrollbar);
2419 gtk_menu_set_tearoff_hints (menu, allocation->width);
2427 gtk_menu_paint (GtkWidget *widget,
2428 GdkEventExpose *event)
2432 gint border_x, border_y;
2433 guint vertical_padding;
2434 guint horizontal_padding;
2436 g_return_if_fail (GTK_IS_MENU (widget));
2438 menu = GTK_MENU (widget);
2440 gtk_widget_style_get (GTK_WIDGET (menu),
2441 "vertical-padding", &vertical_padding,
2442 "horizontal-padding", &horizontal_padding,
2445 border_x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness + horizontal_padding;
2446 border_y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
2447 gdk_drawable_get_size (widget->window, &width, &height);
2449 if (event->window == widget->window)
2451 gint arrow_space = MENU_SCROLL_ARROW_HEIGHT - 2 * widget->style->ythickness;
2452 gint arrow_size = 0.7 * arrow_space;
2454 gtk_paint_box (widget->style,
2458 NULL, widget, "menu",
2460 if (menu->upper_arrow_visible && !menu->tearoff_active)
2462 gtk_paint_box (widget->style,
2464 menu->upper_arrow_prelight ?
2465 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2467 NULL, widget, "menu",
2470 width - 2 * border_x,
2471 MENU_SCROLL_ARROW_HEIGHT);
2473 gtk_paint_arrow (widget->style,
2475 menu->upper_arrow_prelight ?
2476 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2478 NULL, widget, "menu_scroll_arrow_up",
2481 (width - arrow_size ) / 2,
2482 border_y + widget->style->ythickness + (arrow_space - arrow_size)/2,
2483 arrow_size, arrow_size);
2486 if (menu->lower_arrow_visible && !menu->tearoff_active)
2488 gtk_paint_box (widget->style,
2490 menu->lower_arrow_prelight ?
2491 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2493 NULL, widget, "menu",
2495 height - border_y - MENU_SCROLL_ARROW_HEIGHT,
2497 MENU_SCROLL_ARROW_HEIGHT);
2499 gtk_paint_arrow (widget->style,
2501 menu->lower_arrow_prelight ?
2502 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2504 NULL, widget, "menu_scroll_arrow_down",
2507 (width - arrow_size) / 2,
2508 height - border_y - MENU_SCROLL_ARROW_HEIGHT +
2509 widget->style->ythickness + (arrow_space - arrow_size)/2,
2510 arrow_size, arrow_size);
2513 else if (event->window == menu->bin_window)
2515 gtk_paint_box (widget->style,
2519 NULL, widget, "menu",
2520 - border_x, menu->scroll_offset - border_y,
2526 gtk_menu_expose (GtkWidget *widget,
2527 GdkEventExpose *event)
2529 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2530 g_return_val_if_fail (event != NULL, FALSE);
2532 if (GTK_WIDGET_DRAWABLE (widget))
2534 gtk_menu_paint (widget, event);
2536 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
2543 gtk_menu_show (GtkWidget *widget)
2545 GtkMenu *menu = GTK_MENU (widget);
2547 _gtk_menu_refresh_accel_paths (menu, FALSE);
2549 GTK_WIDGET_CLASS (parent_class)->show (widget);
2553 gtk_menu_button_press (GtkWidget *widget,
2554 GdkEventButton *event)
2556 /* Don't pop down the menu for releases over scroll arrows
2558 if (GTK_IS_MENU (widget))
2560 GtkMenu *menu = GTK_MENU (widget);
2562 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2566 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
2570 gtk_menu_button_release (GtkWidget *widget,
2571 GdkEventButton *event)
2573 /* Don't pop down the menu for releases over scroll arrows
2575 if (GTK_IS_MENU (widget))
2577 GtkMenu *menu = GTK_MENU (widget);
2579 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2583 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
2586 static const gchar *
2587 get_accel_path (GtkWidget *menu_item,
2592 GClosure *accel_closure;
2593 GtkAccelGroup *accel_group;
2595 path = _gtk_widget_get_accel_path (menu_item, locked);
2598 path = GTK_MENU_ITEM (menu_item)->accel_path;
2604 label = GTK_BIN (menu_item)->child;
2606 if (GTK_IS_ACCEL_LABEL (label))
2608 g_object_get (label,
2609 "accel-closure", &accel_closure,
2613 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
2615 *locked = accel_group->lock_count > 0;
2625 gtk_menu_key_press (GtkWidget *widget,
2628 GtkMenuShell *menu_shell;
2630 gboolean delete = FALSE;
2631 gboolean can_change_accels;
2632 gchar *accel = NULL;
2633 guint accel_key, accel_mods;
2634 GdkModifierType consumed_modifiers;
2635 GdkDisplay *display;
2637 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2638 g_return_val_if_fail (event != NULL, FALSE);
2640 menu_shell = GTK_MENU_SHELL (widget);
2641 menu = GTK_MENU (widget);
2643 gtk_menu_stop_navigating_submenu (menu);
2645 if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
2648 display = gtk_widget_get_display (widget);
2650 g_object_get (gtk_widget_get_settings (widget),
2651 "gtk-menu-bar-accel", &accel,
2652 "gtk-can-change-accels", &can_change_accels,
2658 GdkModifierType mods = 0;
2659 gboolean handled = FALSE;
2661 gtk_accelerator_parse (accel, &keyval, &mods);
2664 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
2666 /* FIXME this is wrong, needs to be in the global accel resolution
2667 * thing, to properly consider i18n etc., but that probably requires
2668 * AccelGroup changes etc.
2670 if (event->keyval == keyval &&
2671 (mods & event->state) == mods)
2672 gtk_menu_shell_cancel (menu_shell);
2680 switch (event->keyval)
2691 /* Figure out what modifiers went into determining the key symbol */
2692 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
2693 event->hardware_keycode, event->state, event->group,
2694 NULL, NULL, NULL, &consumed_modifiers);
2696 accel_key = gdk_keyval_to_lower (event->keyval);
2697 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
2699 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
2700 * We re-upper case when we match against the keyval, but display and save in caseless form.
2702 if (accel_key != event->keyval)
2703 accel_mods |= GDK_SHIFT_MASK;
2705 /* Modify the accelerators */
2706 if (can_change_accels &&
2707 menu_shell->active_menu_item &&
2708 GTK_BIN (menu_shell->active_menu_item)->child && /* no separators */
2709 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
2710 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
2712 GtkWidget *menu_item = menu_shell->active_menu_item;
2713 gboolean locked, replace_accels = TRUE;
2716 path = get_accel_path (menu_item, &locked);
2717 if (!path || locked)
2719 /* can't change accelerators on menu_items without paths
2720 * (basically, those items are accelerator-locked).
2722 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
2723 gdk_display_beep (display);
2729 /* For the keys that act to delete the current setting, we delete
2730 * the current setting if there is one, otherwise, we set the
2731 * key as the accelerator.
2737 if (gtk_accel_map_lookup_entry (path, &key) &&
2738 (key.accel_key || key.accel_mods))
2744 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
2748 /* we failed, probably because this key is in use and
2751 /* g_print("failed to change\n"); */
2752 gdk_display_beep (display);
2761 check_threshold (GtkWidget *widget,
2762 int start_x, int start_y,
2768 ABS (start_x - x) > THRESHOLD ||
2769 ABS (start_y - y) > THRESHOLD;
2773 definitely_within_item (GtkWidget *widget,
2777 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
2780 gdk_drawable_get_size (window, &w, &h);
2783 check_threshold (widget, 0, 0, x, y) &&
2784 check_threshold (widget, w - 1, 0, x, y) &&
2785 check_threshold (widget, w - 1, h - 1, x, y) &&
2786 check_threshold (widget, 0, h - 1, x, y);
2790 gtk_menu_motion_notify (GtkWidget *widget,
2791 GdkEventMotion *event)
2793 GtkWidget *menu_item;
2795 GtkMenuShell *menu_shell;
2797 gboolean need_enter;
2799 if (GTK_IS_MENU (widget))
2800 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root, TRUE);
2802 /* We received the event for one of two reasons:
2804 * a) We are the active menu, and did gtk_grab_add()
2805 * b) The widget is a child of ours, and the event was propagated
2807 * Since for computation of navigation regions, we want the menu which
2808 * is the parent of the menu item, for a), we need to find that menu,
2809 * which may be different from 'widget'.
2811 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2812 if (!menu_item || !GTK_IS_MENU_ITEM (menu_item) ||
2813 !GTK_IS_MENU (menu_item->parent))
2816 menu_shell = GTK_MENU_SHELL (menu_item->parent);
2817 menu = GTK_MENU (menu_shell);
2819 if (definitely_within_item (menu_item, event->x, event->y))
2820 menu_shell->activate_time = 0;
2822 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
2824 /* Check to see if we are within an active submenu's navigation region
2826 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2829 /* Make sure we pop down if we enter a non-selectable menu item, so we
2830 * don't show a submenu when the cursor is outside the stay-up triangle.
2832 if (!_gtk_menu_item_is_selectable (menu_item))
2834 gtk_menu_shell_deselect (menu_shell);
2840 /* The menu is now sensitive to enter events on its items, but
2841 * was previously sensitive. So we fake an enter event.
2845 menu_shell->ignore_enter = FALSE;
2847 gdk_drawable_get_size (event->window, &width, &height);
2848 if (event->x >= 0 && event->x < width &&
2849 event->y >= 0 && event->y < height)
2851 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2854 send_event->crossing.window = g_object_ref (event->window);
2855 send_event->crossing.time = event->time;
2856 send_event->crossing.send_event = TRUE;
2857 send_event->crossing.x_root = event->x_root;
2858 send_event->crossing.y_root = event->y_root;
2859 send_event->crossing.x = event->x;
2860 send_event->crossing.y = event->y;
2862 /* We send the event to 'widget', the currently active menu,
2863 * instead of 'menu', the menu that the pointer is in. This
2864 * will ensure that the event will be ignored unless the
2865 * menuitem is a child of the active menu or some parent
2866 * menu of the active menu.
2868 result = gtk_widget_event (widget, send_event);
2869 gdk_event_free (send_event);
2879 gtk_menu_scroll_by (GtkMenu *menu,
2884 gint view_width, view_height;
2886 widget = GTK_WIDGET (menu);
2887 offset = menu->scroll_offset + step;
2889 /* If we scroll upward and the non-visible top part
2890 * is smaller than the scroll arrow it would be
2891 * pretty stupid to show the arrow and taking more
2892 * screen space than just scrolling to the top.
2894 if ((step < 0) && (offset < MENU_SCROLL_ARROW_HEIGHT))
2897 /* Don't scroll over the top if we weren't before: */
2898 if ((menu->scroll_offset >= 0) && (offset < 0))
2901 gdk_drawable_get_size (widget->window, &view_width, &view_height);
2903 /* Don't scroll past the bottom if we weren't before: */
2904 if (menu->scroll_offset > 0)
2905 view_height -= MENU_SCROLL_ARROW_HEIGHT;
2907 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
2908 (offset + view_height > widget->requisition.height))
2909 offset = widget->requisition.height - view_height;
2911 if (offset != menu->scroll_offset)
2912 gtk_menu_scroll_to (menu, offset);
2916 gtk_menu_scroll_timeout (gpointer data)
2920 GDK_THREADS_ENTER ();
2922 menu = GTK_MENU (data);
2923 gtk_menu_scroll_by (menu, menu->scroll_step);
2925 GDK_THREADS_LEAVE ();
2931 gtk_menu_scroll (GtkWidget *widget,
2932 GdkEventScroll *event)
2934 GtkMenu *menu = GTK_MENU (widget);
2936 switch (event->direction)
2938 case GDK_SCROLL_RIGHT:
2939 case GDK_SCROLL_DOWN:
2940 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
2942 case GDK_SCROLL_LEFT:
2944 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
2952 gtk_menu_handle_scrolling (GtkMenu *menu,
2957 GtkMenuShell *menu_shell;
2962 gboolean scroll_fast = FALSE;
2963 guint vertical_padding;
2967 menu_shell = GTK_MENU_SHELL (menu);
2969 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
2971 gtk_widget_style_get (GTK_WIDGET (menu),
2972 "vertical-padding", &vertical_padding,
2975 border = GTK_CONTAINER (menu)->border_width +
2976 GTK_WIDGET (menu)->style->ythickness + vertical_padding;
2978 gdk_window_get_position (menu->toplevel->window, &top_x, &top_y);
2982 gdk_window_get_position (GTK_WIDGET (menu)->window, &win_x, &win_y);
2984 if (menu->upper_arrow_visible && !menu->tearoff_active)
2989 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2992 if ((x >= rect.x) && (x < rect.x + rect.width) &&
2993 (y >= rect.y) && (y < rect.y + rect.height))
2996 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
2999 if (enter && in_arrow &&
3000 (!menu->upper_arrow_prelight || menu->scroll_fast != scroll_fast))
3002 menu->upper_arrow_prelight = TRUE;
3003 menu->scroll_fast = scroll_fast;
3004 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
3006 /* Deselect the active item so that any submenus are poped down */
3007 gtk_menu_shell_deselect (menu_shell);
3009 gtk_menu_remove_scroll_timeout (menu);
3010 menu->scroll_step = (scroll_fast) ? -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
3011 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
3012 gtk_menu_scroll_timeout,
3015 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
3017 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
3019 gtk_menu_stop_scrolling (menu);
3023 if (menu->lower_arrow_visible && !menu->tearoff_active)
3026 rect.y = win_y + height - border - MENU_SCROLL_ARROW_HEIGHT;
3028 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
3031 if ((x >= rect.x) && (x < rect.x + rect.width) &&
3032 (y >= rect.y) && (y < rect.y + rect.height))
3035 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
3038 if (enter && in_arrow &&
3039 (!menu->lower_arrow_prelight || menu->scroll_fast != scroll_fast))
3041 menu->lower_arrow_prelight = TRUE;
3042 menu->scroll_fast = scroll_fast;
3043 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
3045 /* Deselect the active item so that any submenus are poped down */
3046 gtk_menu_shell_deselect (menu_shell);
3048 gtk_menu_remove_scroll_timeout (menu);
3049 menu->scroll_step = (scroll_fast) ? MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
3050 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
3051 gtk_menu_scroll_timeout,
3054 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
3056 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
3058 gtk_menu_stop_scrolling (menu);
3064 gtk_menu_enter_notify (GtkWidget *widget,
3065 GdkEventCrossing *event)
3067 GtkWidget *menu_item;
3069 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3070 if (widget && GTK_IS_MENU (widget))
3072 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
3074 if (!menu_shell->ignore_enter)
3075 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root, TRUE);
3078 if (menu_item && GTK_IS_MENU_ITEM (menu_item))
3080 GtkWidget *menu = menu_item->parent;
3082 if (menu && GTK_IS_MENU (menu))
3084 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
3085 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3087 if (priv->seen_item_enter)
3089 /* This is the second enter we see for an item
3090 * on this menu. This means a release should always
3093 menu_shell->activate_time = 0;
3095 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
3096 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
3098 if (definitely_within_item (menu_item, event->x, event->y))
3100 /* This is an actual user-enter (ie. not a pop-under)
3101 * In this case, the user must either have entered
3102 * sufficiently far enough into the item, or he must move
3103 * far enough away from the enter point. (see
3104 * gtk_menu_motion_notify())
3106 menu_shell->activate_time = 0;
3110 priv->seen_item_enter = TRUE;
3114 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
3115 * will not correspond to the event widget's parent. Check to see
3116 * if we are in the parent's navigation region.
3118 if (menu_item && GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
3119 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent), event->x_root, event->y_root))
3122 return GTK_WIDGET_CLASS (parent_class)->enter_notify_event (widget, event);
3126 gtk_menu_leave_notify (GtkWidget *widget,
3127 GdkEventCrossing *event)
3129 GtkMenuShell *menu_shell;
3131 GtkMenuItem *menu_item;
3132 GtkWidget *event_widget;
3134 menu = GTK_MENU (widget);
3135 menu_shell = GTK_MENU_SHELL (widget);
3137 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3140 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE);
3142 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3144 if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
3147 menu_item = GTK_MENU_ITEM (event_widget);
3149 /* Here we check to see if we're leaving an active menu item with a submenu,
3150 * in which case we enter submenu navigation mode.
3152 if (menu_shell->active_menu_item != NULL
3153 && menu_item->submenu != NULL
3154 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
3156 if (GTK_MENU_SHELL (menu_item->submenu)->active)
3158 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
3161 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
3163 /* We are leaving an active menu item with nonactive submenu.
3164 * Deselect it so we don't surprise the user with by popping
3165 * up a submenu _after_ he left the item.
3167 gtk_menu_shell_deselect (menu_shell);
3172 return GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event);
3176 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
3178 if (menu->navigation_region)
3180 gdk_region_destroy (menu->navigation_region);
3181 menu->navigation_region = NULL;
3184 if (menu->navigation_timeout)
3186 g_source_remove (menu->navigation_timeout);
3187 menu->navigation_timeout = 0;
3191 /* When the timeout is elapsed, the navigation region is destroyed
3192 * and the menuitem under the pointer (if any) is selected.
3195 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
3197 GtkMenu *menu = user_data;
3198 GdkWindow *child_window;
3200 GDK_THREADS_ENTER ();
3202 gtk_menu_stop_navigating_submenu (menu);
3204 if (GTK_WIDGET_REALIZED (menu))
3206 child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
3210 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3212 send_event->crossing.window = g_object_ref (child_window);
3213 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
3214 send_event->crossing.send_event = TRUE;
3216 GTK_WIDGET_CLASS (parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
3218 gdk_event_free (send_event);
3222 GDK_THREADS_LEAVE ();
3228 gtk_menu_navigating_submenu (GtkMenu *menu,
3232 if (menu->navigation_region)
3234 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
3238 gtk_menu_stop_navigating_submenu (menu);
3245 #undef DRAW_STAY_UP_TRIANGLE
3247 #ifdef DRAW_STAY_UP_TRIANGLE
3250 draw_stay_up_triangle (GdkWindow *window,
3253 /* Draw ugly color all over the stay-up triangle */
3254 GdkColor ugly_color = { 0, 50000, 10000, 10000 };
3255 GdkGCValues gc_values;
3257 GdkRectangle clipbox;
3259 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
3260 ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
3261 gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
3262 gdk_gc_set_clip_region (ugly_gc, region);
3264 gdk_region_get_clipbox (region, &clipbox);
3266 gdk_draw_rectangle (window,
3269 clipbox.x, clipbox.y,
3270 clipbox.width, clipbox.height);
3272 g_object_unref (ugly_gc);
3277 flip_region (GdkRegion *region,
3282 GdkRectangle *rectangles;
3283 GdkRectangle clipbox;
3284 GdkRegion *new_region;
3287 new_region = gdk_region_new ();
3289 gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
3290 gdk_region_get_clipbox (region, &clipbox);
3292 for (i = 0; i < n_rectangles; ++i)
3294 GdkRectangle rect = rectangles[i];
3297 rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
3300 rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
3302 gdk_region_union_with_rect (new_region, &rect);
3305 g_free (rectangles);
3311 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
3312 GtkMenuItem *menu_item,
3313 GdkEventCrossing *event)
3315 gint submenu_left = 0;
3316 gint submenu_right = 0;
3317 gint submenu_top = 0;
3318 gint submenu_bottom = 0;
3322 GtkWidget *event_widget;
3324 g_return_if_fail (menu_item->submenu != NULL);
3325 g_return_if_fail (event != NULL);
3327 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3329 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
3330 gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
3332 submenu_right = submenu_left + width;
3333 submenu_bottom = submenu_top + height;
3335 gdk_drawable_get_size (event_widget->window, &width, &height);
3337 if (event->x >= 0 && event->x < width)
3340 gboolean flip_y = FALSE;
3341 gboolean flip_x = FALSE;
3343 gtk_menu_stop_navigating_submenu (menu);
3345 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
3348 point[0].x = event->x_root;
3349 point[1].x = submenu_left;
3354 point[0].x = event->x_root + 1;
3355 point[1].x = 2 * (event->x_root + 1) - submenu_right;
3363 point[0].y = event->y_root + 1;
3364 point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
3366 if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
3374 point[0].y = event->y_root;
3375 point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
3377 if (point[0].y >= submenu_bottom)
3381 point[2].x = point[1].x;
3382 point[2].y = point[0].y;
3384 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
3386 if (flip_x || flip_y)
3388 GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
3389 gdk_region_destroy (menu->navigation_region);
3390 menu->navigation_region = new_region;
3393 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3394 "gtk-menu-popdown-delay", &popdown_delay,
3397 menu->navigation_timeout = g_timeout_add (popdown_delay,
3398 gtk_menu_stop_navigating_submenu_cb, menu);
3400 #ifdef DRAW_STAY_UP_TRIANGLE
3401 draw_stay_up_triangle (gdk_get_default_root_window(),
3402 menu->navigation_region);
3408 gtk_menu_deactivate (GtkMenuShell *menu_shell)
3412 g_return_if_fail (GTK_IS_MENU (menu_shell));
3414 parent = menu_shell->parent_menu_shell;
3416 menu_shell->activate_time = 0;
3417 gtk_menu_popdown (GTK_MENU (menu_shell));
3420 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
3424 gtk_menu_position (GtkMenu *menu)
3427 GtkRequisition requisition;
3428 GtkMenuPrivate *private;
3434 GdkScreen *pointer_screen;
3435 GdkRectangle monitor;
3437 g_return_if_fail (GTK_IS_MENU (menu));
3439 widget = GTK_WIDGET (menu);
3441 screen = gtk_widget_get_screen (widget);
3442 gdk_display_get_pointer (gdk_screen_get_display (screen),
3443 &pointer_screen, &x, &y, NULL);
3445 /* We need the requisition to figure out the right place to
3446 * popup the menu. In fact, we always need to ask here, since
3447 * if a size_request was queued while we weren't popped up,
3448 * the requisition won't have been recomputed yet.
3450 gtk_widget_size_request (widget, &requisition);
3452 if (pointer_screen != screen)
3454 /* Pointer is on a different screen; roughly center the
3455 * menu on the screen. If someone was using multiscreen
3456 * + Xinerama together they'd probably want something
3457 * fancier; but that is likely to be vanishingly rare.
3459 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
3460 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
3463 private = gtk_menu_get_private (menu);
3464 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
3468 if (menu->position_func)
3470 (* menu->position_func) (menu, &x, &y, &push_in, menu->position_func_data);
3471 if (private->monitor_num < 0)
3472 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
3474 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3478 gint space_left, space_right, space_above, space_below;
3481 gint xthickness = widget->style->xthickness;
3482 gint ythickness = widget->style->ythickness;
3483 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
3485 /* The placement of popup menus horizontally works like this (with
3486 * RTL in parentheses)
3488 * - If there is enough room to the right (left) of the mouse cursor,
3489 * position the menu there.
3491 * - Otherwise, if if there is enough room to the left (right) of the
3492 * mouse cursor, position the menu there.
3494 * - Otherwise if the menu is smaller than the monitor, position it
3495 * on the side of the mouse cursor that has the most space available
3497 * - Otherwise (if there is simply not enough room for the menu on the
3498 * monitor), position it as far left (right) as possible.
3500 * Positioning in the vertical direction is similar: first try below
3501 * mouse cursor, then above.
3503 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3505 space_left = x - monitor.x;
3506 space_right = monitor.x + monitor.width - x - 1;
3507 space_above = y - monitor.y;
3508 space_below = monitor.y + monitor.height - y - 1;
3510 /* position horizontally */
3512 /* the amount of space we need to position the menu. Note the
3513 * menu is offset "xthickness" pixels
3515 needed_width = requisition.width - xthickness;
3517 if (needed_width <= space_left ||
3518 needed_width <= space_right)
3520 if ((rtl && needed_width <= space_left) ||
3521 (!rtl && needed_width > space_right))
3524 x = x + xthickness - requisition.width + 1;
3528 /* position right */
3532 /* x is clamped on-screen further down */
3534 else if (requisition.width <= monitor.width)
3536 /* the menu is too big to fit on either side of the mouse
3537 * cursor, but smaller than the monitor. Position it on
3538 * the side that has the most space
3540 if (space_left > space_right)
3548 x = monitor.x + monitor.width - requisition.width;
3551 else /* menu is simply too big for the monitor */
3556 x = monitor.x + monitor.width - requisition.width;
3565 /* Position vertically. The algorithm is the same as above, but
3566 * simpler because we don't have to take RTL into account.
3568 needed_height = requisition.height - ythickness;
3570 if (needed_height <= space_above ||
3571 needed_height <= space_below)
3573 if (needed_height <= space_below)
3576 y = y + ythickness - requisition.height + 1;
3578 y = CLAMP (y, monitor.y,
3579 monitor.y + monitor.height - requisition.height);
3581 else if (needed_height > space_below && needed_height > space_above)
3583 if (space_below >= space_above)
3584 y = monitor.y + monitor.height - requisition.height;
3598 menu_height = GTK_WIDGET (menu)->requisition.height;
3600 if (y + menu_height > monitor.y + monitor.height)
3602 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
3603 y = (monitor.y + monitor.height) - menu_height;
3608 scroll_offset += monitor.y - y;
3613 /* FIXME: should this be done in the various position_funcs ? */
3614 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
3616 if (GTK_MENU_SHELL (menu)->active)
3618 private->have_position = TRUE;
3623 if (y + requisition.height > monitor.y + monitor.height)
3624 requisition.height = (monitor.y + monitor.height) - y;
3628 scroll_offset += monitor.y - y;
3629 requisition.height -= monitor.y - y;
3633 if (scroll_offset > 0)
3634 scroll_offset += MENU_SCROLL_ARROW_HEIGHT;
3636 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
3639 if (!GTK_MENU_SHELL (menu)->active)
3641 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
3642 requisition.width, requisition.height);
3645 menu->scroll_offset = scroll_offset;
3649 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
3651 if (menu->timeout_id)
3653 g_source_remove (menu->timeout_id);
3654 menu->timeout_id = 0;
3659 gtk_menu_stop_scrolling (GtkMenu *menu)
3661 gtk_menu_remove_scroll_timeout (menu);
3663 menu->upper_arrow_prelight = FALSE;
3664 menu->lower_arrow_prelight = FALSE;
3668 gtk_menu_scroll_to (GtkMenu *menu,
3673 gint view_width, view_height;
3675 gboolean last_visible;
3677 guint vertical_padding;
3678 guint horizontal_padding;
3680 widget = GTK_WIDGET (menu);
3682 if (menu->tearoff_active &&
3683 menu->tearoff_adjustment &&
3684 (menu->tearoff_adjustment->value != offset))
3686 menu->tearoff_adjustment->value =
3688 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
3689 gtk_adjustment_value_changed (menu->tearoff_adjustment);
3692 /* Move/resize the viewport according to arrows: */
3693 view_width = widget->allocation.width;
3694 view_height = widget->allocation.height;
3696 gtk_widget_style_get (GTK_WIDGET (menu),
3697 "vertical-padding", &vertical_padding,
3698 "horizontal-padding", &horizontal_padding,
3701 border_width = GTK_CONTAINER (menu)->border_width;
3702 view_width -= (border_width + widget->style->xthickness + horizontal_padding) * 2;
3703 view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
3704 menu_height = widget->requisition.height -
3705 (border_width + widget->style->ythickness + vertical_padding) * 2;
3707 x = border_width + widget->style->xthickness + horizontal_padding;
3708 y = border_width + widget->style->ythickness + vertical_padding;
3710 if (!menu->tearoff_active)
3712 last_visible = menu->upper_arrow_visible;
3713 menu->upper_arrow_visible = offset > 0;
3715 if (menu->upper_arrow_visible)
3716 view_height -= MENU_SCROLL_ARROW_HEIGHT;
3718 if ( (last_visible != menu->upper_arrow_visible) &&
3719 !menu->upper_arrow_visible)
3721 menu->upper_arrow_prelight = FALSE;
3723 /* If we hid the upper arrow, possibly remove timeout */
3724 if (menu->scroll_step < 0)
3726 gtk_menu_stop_scrolling (menu);
3727 gtk_widget_queue_draw (GTK_WIDGET (menu));
3731 last_visible = menu->lower_arrow_visible;
3732 menu->lower_arrow_visible = offset < menu_height - view_height;
3734 if (menu->lower_arrow_visible)
3735 view_height -= MENU_SCROLL_ARROW_HEIGHT;
3737 if ( (last_visible != menu->lower_arrow_visible) &&
3738 !menu->lower_arrow_visible)
3740 menu->lower_arrow_prelight = FALSE;
3742 /* If we hid the lower arrow, possibly remove timeout */
3743 if (menu->scroll_step > 0)
3745 gtk_menu_stop_scrolling (menu);
3746 gtk_widget_queue_draw (GTK_WIDGET (menu));
3750 if (menu->upper_arrow_visible)
3751 y += MENU_SCROLL_ARROW_HEIGHT;
3754 /* Scroll the menu: */
3755 if (GTK_WIDGET_REALIZED (menu))
3756 gdk_window_move (menu->bin_window, 0, -offset);
3758 if (GTK_WIDGET_REALIZED (menu))
3759 gdk_window_move_resize (menu->view_window,
3765 menu->scroll_offset = offset;
3769 compute_child_offset (GtkMenu *menu,
3770 GtkWidget *menu_item,
3773 gboolean *is_last_child)
3775 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3776 gint item_top_attach;
3777 gint item_bottom_attach;
3778 gint child_offset = 0;
3781 get_effective_child_attach (menu_item, NULL, NULL,
3782 &item_top_attach, &item_bottom_attach);
3784 /* there is a possibility that we get called before _size_request, so
3785 * check the height table for safety.
3787 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
3790 /* when we have a row with only invisible children, it's height will
3791 * be zero, so there's no need to check WIDGET_VISIBLE here
3793 for (i = 0; i < item_top_attach; i++)
3794 child_offset += priv->heights[i];
3797 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
3799 *offset = child_offset;
3801 *height = priv->heights[item_top_attach];
3807 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
3808 GtkWidget *menu_item)
3811 gint child_offset, child_height;
3815 gboolean last_child = 0;
3817 menu = GTK_MENU (menu_shell);
3819 /* We need to check if the selected item fully visible.
3820 * If not we need to scroll the menu so that it becomes fully
3824 if (compute_child_offset (menu, menu_item,
3825 &child_offset, &child_height, &last_child))
3827 guint vertical_padding;
3829 y = menu->scroll_offset;
3830 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3832 gtk_widget_style_get (GTK_WIDGET (menu),
3833 "vertical-padding", &vertical_padding,
3836 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
3838 if (child_offset < y)
3840 /* Ignore the enter event we might get if the pointer is on the menu
3842 menu_shell->ignore_enter = TRUE;
3843 gtk_menu_scroll_to (menu, child_offset);
3848 if (menu->upper_arrow_visible && !menu->tearoff_active)
3849 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3850 if (menu->lower_arrow_visible && !menu->tearoff_active)
3851 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3853 if (child_offset + child_height > y + height - arrow_height)
3856 if (!last_child && !menu->tearoff_active)
3857 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3859 y = child_offset + child_height - height + arrow_height;
3860 if ((y > 0) && !menu->tearoff_active)
3862 /* Need upper arrow */
3863 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3864 y = child_offset + child_height - height + arrow_height;
3866 /* Ignore the enter event we might get if the pointer is on the menu
3868 menu_shell->ignore_enter = TRUE;
3869 gtk_menu_scroll_to (menu, y);
3877 gtk_menu_select_item (GtkMenuShell *menu_shell,
3878 GtkWidget *menu_item)
3880 GtkMenu *menu = GTK_MENU (menu_shell);
3882 if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
3883 gtk_menu_scroll_item_visible (menu_shell, menu_item);
3885 GTK_MENU_SHELL_CLASS (parent_class)->select_item (menu_shell, menu_item);
3889 /* Reparent the menu, taking care of the refcounting
3891 * If unrealize is true we force a unrealize while reparenting the parent.
3892 * This can help eliminate flicker in some cases.
3894 * What happens is that when the menu is unrealized and then re-realized,
3895 * the allocations are as follows:
3897 * parent - 1x1 at (0,0)
3898 * child1 - 100x20 at (0,0)
3899 * child2 - 100x20 at (0,20)
3900 * child3 - 100x20 at (0,40)
3902 * That is, the parent is small but the children are full sized. Then,
3903 * when the queued_resize gets processed, the parent gets resized to
3906 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
3907 * contains the following logic:
3909 * - if a move or resize operation on a window would change the clip
3910 * region on the children, then before the window is resized
3911 * the background for children is temporarily set to None, the
3912 * move/resize done, and the background for the children restored.
3914 * So, at the point where the parent is resized to final size, the
3915 * background for the children is temporarily None, and thus they
3916 * are not cleared to the background color and the previous background
3917 * (the image of the menu) is left in place.
3920 gtk_menu_reparent (GtkMenu *menu,
3921 GtkWidget *new_parent,
3924 GtkObject *object = GTK_OBJECT (menu);
3925 GtkWidget *widget = GTK_WIDGET (menu);
3926 gboolean was_floating = g_object_is_floating (object);
3928 g_object_ref_sink (object);
3932 g_object_ref (object);
3933 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
3934 gtk_container_add (GTK_CONTAINER (new_parent), widget);
3935 g_object_unref (object);
3938 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
3941 g_object_force_floating (G_OBJECT (object));
3943 g_object_unref (object);
3947 gtk_menu_show_all (GtkWidget *widget)
3949 /* Show children, but not self. */
3950 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
3955 gtk_menu_hide_all (GtkWidget *widget)
3957 /* Hide children, but not self. */
3958 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
3962 * gtk_menu_set_screen:
3963 * @menu: a #GtkMenu.
3964 * @screen: a #GdkScreen, or %NULL if the screen should be
3965 * determined by the widget the menu is attached to.
3967 * Sets the #GdkScreen on which the menu will be displayed.
3972 gtk_menu_set_screen (GtkMenu *menu,
3975 g_return_if_fail (GTK_IS_MENU (menu));
3976 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
3978 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
3982 menu_change_screen (menu, screen);
3986 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
3988 attach_widget_screen_changed (attach_widget, NULL, menu);
3994 * @menu: a #GtkMenu.
3995 * @child: a #GtkMenuItem.
3996 * @left_attach: The column number to attach the left side of the item to.
3997 * @right_attach: The column number to attach the right side of the item to.
3998 * @top_attach: The row number to attach the top of the item to.
3999 * @bottom_attach: The row number to attach the bottom of the item to.
4001 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
4002 * an item will occupy is specified by @left_attach, @right_attach,
4003 * @top_attach and @bottom_attach. These each represent the leftmost,
4004 * rightmost, uppermost and lower column and row numbers of the table.
4005 * (Columns and rows are indexed from zero).
4007 * Note that this function is not related to gtk_menu_detach().
4012 gtk_menu_attach (GtkMenu *menu,
4017 guint bottom_attach)
4019 GtkMenuShell *menu_shell;
4021 g_return_if_fail (GTK_IS_MENU (menu));
4022 g_return_if_fail (GTK_IS_MENU_ITEM (child));
4023 g_return_if_fail (child->parent == NULL ||
4024 child->parent == GTK_WIDGET (menu));
4025 g_return_if_fail (left_attach < right_attach);
4026 g_return_if_fail (top_attach < bottom_attach);
4028 menu_shell = GTK_MENU_SHELL (menu);
4032 AttachInfo *ai = get_attach_info (child);
4034 ai->left_attach = left_attach;
4035 ai->right_attach = right_attach;
4036 ai->top_attach = top_attach;
4037 ai->bottom_attach = bottom_attach;
4039 menu_shell->children = g_list_append (menu_shell->children, child);
4041 gtk_widget_set_parent (child, GTK_WIDGET (menu));
4043 menu_queue_resize (menu);
4047 gtk_container_child_set (GTK_CONTAINER (child->parent), child,
4048 "left-attach", left_attach,
4049 "right-attach", right_attach,
4050 "top-attach", top_attach,
4051 "bottom-attach", bottom_attach,
4057 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
4061 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
4062 "gtk-menu-popup-delay", &popup_delay,
4069 find_child_containing (GtkMenuShell *menu_shell,
4077 /* find a child which includes the area given by
4078 * left, right, top, bottom.
4081 for (list = menu_shell->children; list; list = list->next)
4085 if (!_gtk_menu_item_is_selectable (list->data))
4088 get_effective_child_attach (list->data, &l, &r, &t, &b);
4090 if (l <= left && right <= r
4091 && t <= top && bottom <= b)
4092 return GTK_WIDGET (list->data);
4099 gtk_menu_move_current (GtkMenuShell *menu_shell,
4100 GtkMenuDirectionType direction)
4102 GtkMenu *menu = GTK_MENU (menu_shell);
4105 GtkWidget *match = NULL;
4107 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
4111 case GTK_MENU_DIR_CHILD:
4112 direction = GTK_MENU_DIR_PARENT;
4114 case GTK_MENU_DIR_PARENT:
4115 direction = GTK_MENU_DIR_CHILD;
4121 /* use special table menu key bindings */
4122 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
4124 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
4126 if (direction == GTK_MENU_DIR_NEXT)
4128 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
4130 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
4138 for (i = 0; i < t; i++)
4140 match = find_child_containing (menu_shell,
4141 l, l + 1, i, i + 1);
4147 else if (direction == GTK_MENU_DIR_PREV)
4149 for (i = t; i > 0; i--)
4151 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
4159 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
4161 match = find_child_containing (menu_shell,
4162 l, l + 1, i - 1, i);
4168 else if (direction == GTK_MENU_DIR_PARENT)
4170 /* we go one left if possible */
4172 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
4176 GtkWidget *parent = menu_shell->parent_menu_shell;
4179 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
4180 match = menu_shell->active_menu_item;
4183 else if (direction == GTK_MENU_DIR_CHILD)
4185 /* we go one right if possible */
4186 if (r < gtk_menu_get_n_columns (menu))
4187 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
4191 GtkWidget *parent = menu_shell->parent_menu_shell;
4193 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
4195 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
4196 match = menu_shell->active_menu_item;
4202 gtk_menu_shell_select_item (menu_shell, match);
4207 GTK_MENU_SHELL_CLASS (parent_class)->move_current (menu_shell, direction);
4211 get_visible_size (GtkMenu *menu)
4213 GtkWidget *widget = GTK_WIDGET (menu);
4214 GtkContainer *container = GTK_CONTAINER (menu);
4216 gint menu_height = (widget->allocation.height
4217 - 2 * (container->border_width
4218 + widget->style->ythickness));
4220 if (menu->upper_arrow_visible && !menu->tearoff_active)
4221 menu_height -= MENU_SCROLL_ARROW_HEIGHT;
4222 if (menu->lower_arrow_visible && !menu->tearoff_active)
4223 menu_height -= MENU_SCROLL_ARROW_HEIGHT;
4228 /* Find the sensitive on-screen child containing @y, or if none,
4229 * the nearest selectable onscreen child. (%NULL if none)
4232 child_at (GtkMenu *menu,
4235 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4236 GtkWidget *child = NULL;
4237 gint child_offset = 0;
4240 gint lower, upper; /* Onscreen bounds */
4242 menu_height = get_visible_size (menu);
4243 lower = menu->scroll_offset;
4244 upper = menu->scroll_offset + menu_height;
4246 for (children = menu_shell->children; children; children = children->next)
4248 if (GTK_WIDGET_VISIBLE (children->data))
4250 GtkRequisition child_requisition;
4252 gtk_widget_size_request (children->data, &child_requisition);
4254 if (_gtk_menu_item_is_selectable (children->data) &&
4255 child_offset >= lower &&
4256 child_offset + child_requisition.height <= upper)
4258 child = children->data;
4260 if (child_offset + child_requisition.height > y &&
4261 !GTK_IS_TEAROFF_MENU_ITEM (child))
4265 child_offset += child_requisition.height;
4273 get_menu_height (GtkMenu *menu)
4276 GtkWidget *widget = GTK_WIDGET (menu);
4278 height = widget->requisition.height;
4279 height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
4281 if (menu->upper_arrow_visible && !menu->tearoff_active)
4282 height -= MENU_SCROLL_ARROW_HEIGHT;
4284 if (menu->lower_arrow_visible && !menu->tearoff_active)
4285 height -= MENU_SCROLL_ARROW_HEIGHT;
4291 gtk_menu_real_move_scroll (GtkMenu *menu,
4294 gint page_size = get_visible_size (menu);
4295 gint end_position = get_menu_height (menu);
4296 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4300 case GTK_SCROLL_PAGE_UP:
4301 case GTK_SCROLL_PAGE_DOWN:
4305 gint child_offset = 0;
4306 gboolean old_upper_arrow_visible;
4309 if (type == GTK_SCROLL_PAGE_UP)
4314 if (menu_shell->active_menu_item)
4318 compute_child_offset (menu, menu_shell->active_menu_item,
4319 &child_offset, &child_height, NULL);
4320 child_offset += child_height / 2;
4323 menu_shell->ignore_enter = TRUE;
4324 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4325 old_offset = menu->scroll_offset;
4327 new_offset = menu->scroll_offset + step;
4328 new_offset = CLAMP (new_offset, 0, end_position - page_size);
4330 gtk_menu_scroll_to (menu, new_offset);
4332 if (menu_shell->active_menu_item)
4334 GtkWidget *new_child;
4335 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4337 if (menu->scroll_offset != old_offset)
4338 step = menu->scroll_offset - old_offset;
4340 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * MENU_SCROLL_ARROW_HEIGHT;
4342 new_child = child_at (menu, child_offset + step);
4344 gtk_menu_shell_select_item (menu_shell, new_child);
4348 case GTK_SCROLL_START:
4349 /* Ignore the enter event we might get if the pointer is on the menu
4351 menu_shell->ignore_enter = TRUE;
4352 gtk_menu_scroll_to (menu, 0);
4353 gtk_menu_shell_select_first (menu_shell, TRUE);
4355 case GTK_SCROLL_END:
4356 /* Ignore the enter event we might get if the pointer is on the menu
4358 menu_shell->ignore_enter = TRUE;
4359 gtk_menu_scroll_to (menu, end_position - page_size);
4360 _gtk_menu_shell_select_last (menu_shell, TRUE);
4369 * gtk_menu_set_monitor:
4371 * @monitor_num: the number of the monitor on which the menu should
4374 * Informs GTK+ on which monitor a menu should be popped up.
4375 * See gdk_screen_get_monitor_geometry().
4377 * This function should be called from a #GtkMenuPositionFunc if the
4378 * menu should not appear on the same monitor as the pointer. This
4379 * information can't be reliably inferred from the coordinates returned
4380 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
4381 * may extend beyond the monitor boundaries or even the screen boundaries.
4386 gtk_menu_set_monitor (GtkMenu *menu,
4389 GtkMenuPrivate *priv;
4390 g_return_if_fail (GTK_IS_MENU (menu));
4392 priv = gtk_menu_get_private (menu);
4394 priv->monitor_num = monitor_num;
4398 * gtk_menu_get_for_attach_widget:
4399 * @widget: a #GtkWidget
4400 * * Returns a list of the menus which are attached to this widget.
4401 * This list is owned by GTK+ and must not be modified.
4403 * Return value: the list of menus attached to his widget.
4408 gtk_menu_get_for_attach_widget (GtkWidget *widget)
4412 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
4414 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
4419 gtk_menu_grab_notify (GtkWidget *widget,
4420 gboolean was_grabbed)
4422 GtkWidget *toplevel;
4423 GtkWindowGroup *group;
4426 toplevel = gtk_widget_get_toplevel (widget);
4427 group = gtk_window_get_group (GTK_WINDOW (toplevel));
4428 grab = _gtk_window_group_get_current_grab (group);
4432 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
4433 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
4437 #define __GTK_MENU_C__
4438 #include "gtkaliasdef.c"