1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 #define GTK_MENU_INTERNALS
29 #include <string.h> /* memset */
30 #include "gdk/gdkkeysyms.h"
31 #include "gtkaccellabel.h"
32 #include "gtkaccelmap.h"
33 #include "gtkbindings.h"
36 #include "gtkmarshalers.h"
38 #include "gtkmenuitem.h"
39 #include "gtktearoffmenuitem.h"
40 #include "gtkwindow.h"
42 #include "gtkvscrollbar.h"
43 #include "gtksettings.h"
47 #define MENU_ITEM_CLASS(w) GTK_MENU_ITEM_GET_CLASS (w)
48 #define MENU_NEEDS_RESIZE(m) GTK_MENU_SHELL (m)->menu_flag
50 #define DEFAULT_POPUP_DELAY 200
51 #define DEFAULT_POPDOWN_DELAY 1000
53 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
54 * extends below the submenu
57 #define MENU_SCROLL_STEP1 8
58 #define MENU_SCROLL_STEP2 15
59 #define MENU_SCROLL_ARROW_HEIGHT 16
60 #define MENU_SCROLL_FAST_ZONE 8
61 #define MENU_SCROLL_TIMEOUT1 50
62 #define MENU_SCROLL_TIMEOUT2 50
64 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
66 typedef struct _GtkMenuAttachData GtkMenuAttachData;
67 typedef struct _GtkMenuPrivate GtkMenuPrivate;
69 struct _GtkMenuAttachData
71 GtkWidget *attach_widget;
72 GtkMenuDetachFunc detacher;
75 struct _GtkMenuPrivate
77 gboolean have_position;
81 /* info used for the table */
113 CHILD_PROP_LEFT_ATTACH,
114 CHILD_PROP_RIGHT_ATTACH,
115 CHILD_PROP_TOP_ATTACH,
116 CHILD_PROP_BOTTOM_ATTACH
119 static void gtk_menu_class_init (GtkMenuClass *klass);
120 static void gtk_menu_init (GtkMenu *menu);
121 static void gtk_menu_set_property (GObject *object,
125 static void gtk_menu_get_property (GObject *object,
129 static void gtk_menu_set_child_property(GtkContainer *container,
134 static void gtk_menu_get_child_property(GtkContainer *container,
139 static void gtk_menu_destroy (GtkObject *object);
140 static void gtk_menu_finalize (GObject *object);
141 static void gtk_menu_realize (GtkWidget *widget);
142 static void gtk_menu_unrealize (GtkWidget *widget);
143 static void gtk_menu_size_request (GtkWidget *widget,
144 GtkRequisition *requisition);
145 static void gtk_menu_size_allocate (GtkWidget *widget,
146 GtkAllocation *allocation);
147 static void gtk_menu_paint (GtkWidget *widget,
148 GdkEventExpose *expose);
149 static void gtk_menu_show (GtkWidget *widget);
150 static gboolean gtk_menu_expose (GtkWidget *widget,
151 GdkEventExpose *event);
152 static gboolean gtk_menu_key_press (GtkWidget *widget,
154 static gboolean gtk_menu_button_press (GtkWidget *widget,
155 GdkEventButton *event);
156 static gboolean gtk_menu_button_release (GtkWidget *widget,
157 GdkEventButton *event);
158 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
159 GdkEventMotion *event);
160 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
161 GdkEventCrossing *event);
162 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
163 GdkEventCrossing *event);
164 static void gtk_menu_scroll_to (GtkMenu *menu,
167 static void gtk_menu_stop_scrolling (GtkMenu *menu);
168 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
169 static gboolean gtk_menu_scroll_timeout (gpointer data);
171 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
172 GtkWidget *menu_item);
173 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
174 GtkWidget *menu_item);
175 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
178 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
180 static void gtk_menu_handle_scrolling (GtkMenu *menu,
182 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
184 static void gtk_menu_style_set (GtkWidget *widget,
185 GtkStyle *previous_style);
186 static gboolean gtk_menu_focus (GtkWidget *widget,
187 GtkDirectionType direction);
188 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
189 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
190 GtkMenuDirectionType direction);
191 static void gtk_menu_real_move_scroll (GtkMenu *menu,
194 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
195 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
196 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
199 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
200 GtkMenuItem *menu_item,
201 GdkEventCrossing *event);
203 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
204 static void gtk_menu_show_all (GtkWidget *widget);
205 static void gtk_menu_hide_all (GtkWidget *widget);
206 static void gtk_menu_position (GtkMenu *menu);
207 static void gtk_menu_reparent (GtkMenu *menu,
208 GtkWidget *new_parent,
210 static void gtk_menu_remove (GtkContainer *menu,
213 static void gtk_menu_update_title (GtkMenu *menu);
215 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
216 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
218 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
220 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
221 gboolean group_changed);
223 static GtkMenuShellClass *parent_class = NULL;
224 static const gchar *attach_data_key = "gtk-menu-attach-data";
226 static guint menu_signals[LAST_SIGNAL] = { 0 };
229 gtk_menu_free_private (gpointer data)
231 GtkMenuPrivate *priv = (GtkMenuPrivate *)data;
233 g_free (priv->heights);
239 gtk_menu_get_private (GtkMenu *menu)
241 GtkMenuPrivate *private;
242 static GQuark private_quark = 0;
245 private_quark = g_quark_from_static_string ("gtk-menu-private");
247 private = g_object_get_qdata (G_OBJECT (menu), private_quark);
251 private = g_new0 (GtkMenuPrivate, 1);
252 private->have_position = FALSE;
254 g_object_set_qdata_full (G_OBJECT (menu), private_quark,
255 private, gtk_menu_free_private);
262 gtk_menu_get_type (void)
264 static GType menu_type = 0;
268 static const GTypeInfo menu_info =
270 sizeof (GtkMenuClass),
271 NULL, /* base_init */
272 NULL, /* base_finalize */
273 (GClassInitFunc) gtk_menu_class_init,
274 NULL, /* class_finalize */
275 NULL, /* class_data */
278 (GInstanceInitFunc) gtk_menu_init,
281 menu_type = g_type_register_static (GTK_TYPE_MENU_SHELL, "GtkMenu",
289 gtk_menu_class_init (GtkMenuClass *class)
291 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
292 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
293 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
294 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
295 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
296 GtkBindingSet *binding_set;
298 parent_class = g_type_class_peek_parent (class);
300 gobject_class->finalize = gtk_menu_finalize;
301 gobject_class->set_property = gtk_menu_set_property;
302 gobject_class->get_property = gtk_menu_get_property;
304 object_class->destroy = gtk_menu_destroy;
306 widget_class->realize = gtk_menu_realize;
307 widget_class->unrealize = gtk_menu_unrealize;
308 widget_class->size_request = gtk_menu_size_request;
309 widget_class->size_allocate = gtk_menu_size_allocate;
310 widget_class->show = gtk_menu_show;
311 widget_class->expose_event = gtk_menu_expose;
312 widget_class->key_press_event = gtk_menu_key_press;
313 widget_class->button_press_event = gtk_menu_button_press;
314 widget_class->button_release_event = gtk_menu_button_release;
315 widget_class->motion_notify_event = gtk_menu_motion_notify;
316 widget_class->show_all = gtk_menu_show_all;
317 widget_class->hide_all = gtk_menu_hide_all;
318 widget_class->enter_notify_event = gtk_menu_enter_notify;
319 widget_class->leave_notify_event = gtk_menu_leave_notify;
320 widget_class->motion_notify_event = gtk_menu_motion_notify;
321 widget_class->style_set = gtk_menu_style_set;
322 widget_class->focus = gtk_menu_focus;
323 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
325 container_class->remove = gtk_menu_remove;
326 container_class->get_child_property = gtk_menu_get_child_property;
327 container_class->set_child_property = gtk_menu_set_child_property;
329 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
330 menu_shell_class->deactivate = gtk_menu_deactivate;
331 menu_shell_class->select_item = gtk_menu_select_item;
332 menu_shell_class->insert = gtk_menu_real_insert;
333 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
334 menu_shell_class->move_current = gtk_menu_move_current;
336 menu_signals[MOVE_SCROLL] =
337 _gtk_binding_signal_new ("move_scroll",
338 G_OBJECT_CLASS_TYPE (object_class),
339 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
340 G_CALLBACK (gtk_menu_real_move_scroll),
342 _gtk_marshal_VOID__ENUM,
344 GTK_TYPE_SCROLL_TYPE);
346 g_object_class_install_property (gobject_class,
348 g_param_spec_string ("tearoff-title",
350 _("A title that may be displayed by the window manager when this menu is torn-off"),
352 G_PARAM_READABLE | G_PARAM_WRITABLE));
354 gtk_widget_class_install_style_property (widget_class,
355 g_param_spec_int ("vertical-padding",
356 _("Vertical Padding"),
357 _("Extra space at the top and bottom of the menu"),
363 gtk_widget_class_install_style_property (widget_class,
364 g_param_spec_int ("vertical-offset",
365 _("Vertical Offset"),
366 _("When the menu is a submenu, position it this number of pixels offset vertically"),
372 gtk_widget_class_install_style_property (widget_class,
373 g_param_spec_int ("horizontal-offset",
374 _("Horizontal Offset"),
375 _("When the menu is a submenu, position it this number of pixels offset horizontally"),
382 gtk_container_class_install_child_property (container_class,
383 CHILD_PROP_LEFT_ATTACH,
384 g_param_spec_uint ("left_attach",
386 _("The column number to attach the left side of the child to"),
390 gtk_container_class_install_child_property (container_class,
391 CHILD_PROP_RIGHT_ATTACH,
392 g_param_spec_uint ("right_attach",
394 _("The column number to attach the right side of the child to"),
398 gtk_container_class_install_child_property (container_class,
399 CHILD_PROP_TOP_ATTACH,
400 g_param_spec_uint ("top_attach",
402 _("The row number to attach the top of the child to"),
406 gtk_container_class_install_child_property (container_class,
407 CHILD_PROP_BOTTOM_ATTACH,
408 g_param_spec_uint ("bottom_attach",
410 _("The row number to attach the bottom of the child to"),
414 binding_set = gtk_binding_set_by_class (class);
415 gtk_binding_entry_add_signal (binding_set,
418 GTK_TYPE_MENU_DIRECTION_TYPE,
420 gtk_binding_entry_add_signal (binding_set,
423 GTK_TYPE_MENU_DIRECTION_TYPE,
425 gtk_binding_entry_add_signal (binding_set,
428 GTK_TYPE_MENU_DIRECTION_TYPE,
430 gtk_binding_entry_add_signal (binding_set,
433 GTK_TYPE_MENU_DIRECTION_TYPE,
435 gtk_binding_entry_add_signal (binding_set,
438 GTK_TYPE_MENU_DIRECTION_TYPE,
439 GTK_MENU_DIR_PARENT);
440 gtk_binding_entry_add_signal (binding_set,
443 GTK_TYPE_MENU_DIRECTION_TYPE,
444 GTK_MENU_DIR_PARENT);
445 gtk_binding_entry_add_signal (binding_set,
448 GTK_TYPE_MENU_DIRECTION_TYPE,
450 gtk_binding_entry_add_signal (binding_set,
453 GTK_TYPE_MENU_DIRECTION_TYPE,
455 gtk_binding_entry_add_signal (binding_set,
458 GTK_TYPE_SCROLL_TYPE,
460 gtk_binding_entry_add_signal (binding_set,
463 GTK_TYPE_SCROLL_TYPE,
465 gtk_binding_entry_add_signal (binding_set,
468 GTK_TYPE_SCROLL_TYPE,
470 gtk_binding_entry_add_signal (binding_set,
473 GTK_TYPE_SCROLL_TYPE,
475 gtk_binding_entry_add_signal (binding_set,
478 GTK_TYPE_SCROLL_TYPE,
480 gtk_binding_entry_add_signal (binding_set,
483 GTK_TYPE_SCROLL_TYPE,
485 gtk_binding_entry_add_signal (binding_set,
488 GTK_TYPE_SCROLL_TYPE,
489 GTK_SCROLL_PAGE_DOWN);
490 gtk_binding_entry_add_signal (binding_set,
493 GTK_TYPE_SCROLL_TYPE,
494 GTK_SCROLL_PAGE_DOWN);
496 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
497 _("Can change accelerators"),
498 _("Whether menu accelerators can be changed by pressing a key over the menu item"),
502 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
503 _("Delay before submenus appear"),
504 _("Minimum time the pointer must stay over a menu item before the submenu appear"),
510 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
511 _("Delay before hiding a submenu"),
512 _("The time before hiding a submenu when the pointer is moving towards the submenu"),
515 DEFAULT_POPDOWN_DELAY,
522 gtk_menu_set_property (GObject *object,
529 menu = GTK_MENU (object);
533 case PROP_TEAROFF_TITLE:
534 gtk_menu_set_title (menu, g_value_get_string (value));
537 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
543 gtk_menu_get_property (GObject *object,
550 menu = GTK_MENU (object);
554 case PROP_TEAROFF_TITLE:
555 g_value_set_string (value, gtk_menu_get_title (menu));
558 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
564 get_attach_info (GObject *child)
566 AttachInfo *ai = g_object_get_data (child, ATTACH_INFO_KEY);
570 ai = g_new0 (AttachInfo, 1);
571 g_object_set_data_full (child, ATTACH_INFO_KEY, ai, g_free);
578 gtk_menu_set_child_property (GtkContainer *container,
584 GtkMenu *menu = GTK_MENU (container);
585 GtkMenuPrivate *priv;
586 AttachInfo *ai = get_attach_info (G_OBJECT (child));
588 priv = gtk_menu_get_private (menu);
592 case CHILD_PROP_LEFT_ATTACH:
593 ai->left_attach = g_value_get_uint (value);
595 case CHILD_PROP_RIGHT_ATTACH:
596 ai->right_attach = g_value_get_uint (value);
598 case CHILD_PROP_TOP_ATTACH:
599 ai->top_attach = g_value_get_uint (value);
601 case CHILD_PROP_BOTTOM_ATTACH:
602 ai->bottom_attach = g_value_get_uint (value);
606 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
610 priv->columns = MAX (priv->columns, ai->right_attach);
611 priv->rows = MAX (priv->rows, ai->bottom_attach);
613 gtk_widget_queue_resize (GTK_WIDGET (menu));
617 gtk_menu_get_child_property (GtkContainer *container,
623 AttachInfo *ai = get_attach_info (G_OBJECT (child));
627 case CHILD_PROP_LEFT_ATTACH:
628 g_value_set_uint (value, ai->left_attach);
630 case CHILD_PROP_RIGHT_ATTACH:
631 g_value_set_uint (value, ai->right_attach);
633 case CHILD_PROP_TOP_ATTACH:
634 g_value_set_uint (value, ai->top_attach);
636 case CHILD_PROP_BOTTOM_ATTACH:
637 g_value_set_uint (value, ai->bottom_attach);
641 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
647 get_child_attach (GtkWidget *child,
653 gtk_container_child_get (GTK_CONTAINER (child->parent), child,
662 gtk_menu_window_event (GtkWidget *window,
666 gboolean handled = FALSE;
668 g_object_ref (window);
674 case GDK_KEY_RELEASE:
675 handled = gtk_widget_event (menu, event);
681 g_object_unref (window);
682 g_object_unref (menu);
688 gtk_menu_window_size_request (GtkWidget *window,
689 GtkRequisition *requisition,
692 GtkMenuPrivate *private = gtk_menu_get_private (menu);
694 if (private->have_position)
696 GdkScreen *screen = gtk_widget_get_screen (window);
697 GdkRectangle monitor;
700 monitor_num = gdk_screen_get_monitor_at_point (screen,
701 private->x, private->y);
702 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
704 if (private->y + requisition->height > monitor.y + monitor.height)
705 requisition->height = monitor.y + monitor.height - private->y;
707 if (private->y < monitor.y)
708 requisition->height -= monitor.y - private->y;
713 gtk_menu_init (GtkMenu *menu)
715 menu->parent_menu_item = NULL;
716 menu->old_active_menu_item = NULL;
717 menu->accel_group = NULL;
718 menu->position_func = NULL;
719 menu->position_func_data = NULL;
720 menu->toggle_size = 0;
722 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
723 "type", GTK_WINDOW_POPUP,
726 "signal::event", gtk_menu_window_event, menu,
727 "signal::size_request", gtk_menu_window_size_request, menu,
728 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
730 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
731 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
733 /* Refloat the menu, so that reference counting for the menu isn't
734 * affected by it being a child of the toplevel
736 GTK_WIDGET_SET_FLAGS (menu, GTK_FLOATING);
737 menu->needs_destruction_ref_count = TRUE;
739 menu->view_window = NULL;
740 menu->bin_window = NULL;
742 menu->scroll_offset = 0;
743 menu->scroll_step = 0;
744 menu->timeout_id = 0;
745 menu->scroll_fast = FALSE;
747 menu->tearoff_window = NULL;
748 menu->tearoff_hbox = NULL;
749 menu->torn_off = FALSE;
750 menu->tearoff_active = FALSE;
751 menu->tearoff_adjustment = NULL;
752 menu->tearoff_scrollbar = NULL;
754 menu->upper_arrow_visible = FALSE;
755 menu->lower_arrow_visible = FALSE;
756 menu->upper_arrow_prelight = FALSE;
757 menu->lower_arrow_prelight = FALSE;
759 MENU_NEEDS_RESIZE (menu) = TRUE;
763 gtk_menu_destroy (GtkObject *object)
766 GtkMenuAttachData *data;
768 g_return_if_fail (GTK_IS_MENU (object));
770 menu = GTK_MENU (object);
772 gtk_menu_stop_scrolling (menu);
774 data = g_object_get_data (G_OBJECT (object), attach_data_key);
776 gtk_menu_detach (menu);
778 gtk_menu_stop_navigating_submenu (menu);
780 if (menu->old_active_menu_item)
782 g_object_unref (menu->old_active_menu_item);
783 menu->old_active_menu_item = NULL;
786 /* Add back the reference count for being a child */
787 if (menu->needs_destruction_ref_count)
789 menu->needs_destruction_ref_count = FALSE;
790 g_object_ref (object);
793 if (menu->accel_group)
795 g_object_unref (menu->accel_group);
796 menu->accel_group = NULL;
800 gtk_widget_destroy (menu->toplevel);
801 if (menu->tearoff_window)
802 gtk_widget_destroy (menu->tearoff_window);
804 GTK_OBJECT_CLASS (parent_class)->destroy (object);
808 gtk_menu_finalize (GObject *object)
810 GtkMenu *menu = GTK_MENU (object);
812 g_free (menu->accel_path);
814 G_OBJECT_CLASS (parent_class)->finalize (object);
818 menu_change_screen (GtkMenu *menu,
819 GdkScreen *new_screen)
823 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
824 gtk_menu_position (menu);
827 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
831 attach_widget_screen_changed (GtkWidget *attach_widget,
832 GdkScreen *previous_screen,
835 if (gtk_widget_has_screen (attach_widget) &&
836 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
838 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
843 gtk_menu_attach_to_widget (GtkMenu *menu,
844 GtkWidget *attach_widget,
845 GtkMenuDetachFunc detacher)
847 GtkMenuAttachData *data;
849 g_return_if_fail (GTK_IS_MENU (menu));
850 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
851 g_return_if_fail (detacher != NULL);
853 /* keep this function in sync with gtk_widget_set_parent()
856 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
859 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
860 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
865 gtk_object_sink (GTK_OBJECT (menu));
867 data = g_new (GtkMenuAttachData, 1);
868 data->attach_widget = attach_widget;
870 g_signal_connect (attach_widget, "screen_changed",
871 G_CALLBACK (attach_widget_screen_changed), menu);
872 attach_widget_screen_changed (attach_widget, NULL, menu);
874 data->detacher = detacher;
875 g_object_set_data (G_OBJECT (menu), attach_data_key, data);
877 if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
878 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
880 /* we don't need to set the style here, since
881 * we are a toplevel widget.
884 /* Fallback title for menu comes from attach widget */
885 gtk_menu_update_title (menu);
889 gtk_menu_get_attach_widget (GtkMenu *menu)
891 GtkMenuAttachData *data;
893 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
895 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
897 return data->attach_widget;
902 gtk_menu_detach (GtkMenu *menu)
904 GtkMenuAttachData *data;
906 g_return_if_fail (GTK_IS_MENU (menu));
908 /* keep this function in sync with gtk_widget_unparent()
910 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
913 g_warning ("gtk_menu_detach(): menu is not attached");
916 g_object_set_data (G_OBJECT (menu), attach_data_key, NULL);
918 g_signal_handlers_disconnect_by_func (data->attach_widget,
919 (gpointer) attach_widget_screen_changed,
922 data->detacher (data->attach_widget, menu);
924 if (GTK_WIDGET_REALIZED (menu))
925 gtk_widget_unrealize (GTK_WIDGET (menu));
929 /* Fallback title for menu comes from attach widget */
930 gtk_menu_update_title (menu);
932 g_object_unref (menu);
936 gtk_menu_remove (GtkContainer *container,
941 g_return_if_fail (GTK_IS_MENU (container));
942 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
944 menu = GTK_MENU (container);
946 /* Clear out old_active_menu_item if it matches the item we are removing
948 if (menu->old_active_menu_item == widget)
950 g_object_unref (menu->old_active_menu_item);
951 menu->old_active_menu_item = NULL;
954 g_object_set_data (G_OBJECT (widget), ATTACH_INFO_KEY, NULL);
956 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
963 return g_object_new (GTK_TYPE_MENU, NULL);
967 gtk_menu_real_insert (GtkMenuShell *menu_shell,
973 GtkMenuPrivate *priv;
975 if (GTK_WIDGET_REALIZED (menu_shell))
976 gtk_widget_set_parent_window (child, GTK_MENU (menu_shell)->bin_window);
978 GTK_MENU_SHELL_CLASS (parent_class)->insert (menu_shell, child, position);
980 priv = gtk_menu_get_private (GTK_MENU (menu_shell));
984 /* attach after the last row */
985 i = g_list_length (menu_shell->children) - 1;
986 gtk_menu_attach (GTK_MENU (menu_shell), child,
987 0, priv->columns ? priv->columns : 1,
993 /* we need to make space for this new item; move all items with
994 * top >= position one down
996 for (children = menu_shell->children; children; children = children->next)
1000 gtk_container_child_get (GTK_CONTAINER (menu_shell), children->data,
1002 "bottom_attach", &bottom,
1005 if (top >= position)
1006 gtk_container_child_set (GTK_CONTAINER (menu_shell), children->data,
1007 "top_attach", top + 1,
1008 "bottom_attach", bottom + 1,
1012 /* attach the new item */
1013 gtk_menu_attach (GTK_MENU (menu_shell), child,
1014 0, priv->columns ? priv->columns : 1,
1015 position, position + 1);
1019 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1024 widget = GTK_WIDGET (menu);
1030 GdkGCValues gc_values;
1032 menu->tearoff_active = FALSE;
1033 menu->saved_scroll_offset = menu->scroll_offset;
1035 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
1036 gc = gdk_gc_new_with_values (widget->window,
1037 &gc_values, GDK_GC_SUBWINDOW);
1039 gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1041 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
1046 gdk_draw_drawable (pixmap, gc,
1047 menu->tearoff_window->window,
1048 0, 0, 0, 0, -1, -1);
1049 g_object_unref (gc);
1051 gtk_widget_set_size_request (menu->tearoff_window,
1055 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
1056 g_object_unref (pixmap);
1061 popup_grab_on_window (GdkWindow *window,
1062 guint32 activate_time)
1064 if ((gdk_pointer_grab (window, TRUE,
1065 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1066 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1067 GDK_POINTER_MOTION_MASK,
1068 NULL, NULL, activate_time) == 0))
1070 if (gdk_keyboard_grab (window, TRUE,
1071 activate_time) == 0)
1075 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1086 * @menu: a #GtkMenu.
1087 * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
1088 * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
1089 * @func: a user supplied function used to position the menu, or %NULL
1090 * @data: user supplied data to be passed to @func.
1091 * @button: the mouse button which was pressed to initiate the event.
1092 * @activate_time: the time at which the activation event occurred.
1094 * Displays a menu and makes it available for selection. Applications can use
1095 * this function to display context-sensitive menus, and will typically supply
1096 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1097 * parameters. The default menu positioning function will position the menu
1098 * at the current mouse cursor position.
1100 * The @button parameter should be the mouse button pressed to initiate
1101 * the menu popup. If the menu popup was initiated by something other than
1102 * a mouse button press, such as a mouse button release or a keypress,
1103 * @button should be 0.
1105 * The @activate_time parameter should be the time stamp of the event that
1106 * initiated the popup. If such an event is not available, use
1107 * gtk_get_current_event_time() instead.
1111 gtk_menu_popup (GtkMenu *menu,
1112 GtkWidget *parent_menu_shell,
1113 GtkWidget *parent_menu_item,
1114 GtkMenuPositionFunc func,
1117 guint32 activate_time)
1120 GtkWidget *xgrab_shell;
1122 GdkEvent *current_event;
1123 GtkMenuShell *menu_shell;
1125 g_return_if_fail (GTK_IS_MENU (menu));
1127 widget = GTK_WIDGET (menu);
1128 menu_shell = GTK_MENU_SHELL (menu);
1130 menu_shell->parent_menu_shell = parent_menu_shell;
1132 /* Find the last viewable ancestor, and make an X grab on it
1134 parent = GTK_WIDGET (menu);
1138 gboolean viewable = TRUE;
1139 GtkWidget *tmp = parent;
1143 if (!GTK_WIDGET_MAPPED (tmp))
1152 xgrab_shell = parent;
1154 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1157 /* We want to receive events generated when we map the menu; unfortunately,
1158 * since there is probably already an implicit grab in place from the
1159 * button that the user used to pop up the menu, we won't receive then --
1160 * in particular, the EnterNotify when the menu pops up under the pointer.
1162 * If we are grabbing on a parent menu shell, no problem; just grab on
1163 * that menu shell first before popping up the window with owner_events = TRUE.
1165 * When grabbing on the menu itself, things get more convuluted - we
1166 * we do an explicit grab on a specially created window with
1167 * owner_events = TRUE, which we override further down with a grab
1168 * on the menu. (We can't grab on the menu until it is mapped; we
1169 * probably could just leave the grab on the other window, with a
1170 * little reorganization of the code in gtkmenu*).
1172 if (xgrab_shell && xgrab_shell != widget)
1174 if (popup_grab_on_window (xgrab_shell->window, activate_time))
1175 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1179 GdkWindow *transfer_window;
1181 xgrab_shell = widget;
1182 transfer_window = menu_grab_transfer_window_get (menu);
1183 if (popup_grab_on_window (transfer_window, activate_time))
1184 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1187 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1189 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1190 * with a stuck up window, we just abort here. Presumably the user will
1193 menu_shell->parent_menu_shell = NULL;
1194 menu_grab_transfer_window_destroy (menu);
1198 menu_shell->active = TRUE;
1199 menu_shell->button = button;
1201 /* If we are popping up the menu from something other than, a button
1202 * press then, as a heuristic, we ignore enter events for the menu
1203 * until we get a MOTION_NOTIFY.
1206 current_event = gtk_get_current_event ();
1209 if ((current_event->type != GDK_BUTTON_PRESS) &&
1210 (current_event->type != GDK_ENTER_NOTIFY))
1211 menu_shell->ignore_enter = TRUE;
1213 gdk_event_free (current_event);
1216 menu_shell->ignore_enter = TRUE;
1220 gtk_menu_tearoff_bg_copy (menu);
1222 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1225 menu->parent_menu_item = parent_menu_item;
1226 menu->position_func = func;
1227 menu->position_func_data = data;
1228 menu_shell->activate_time = activate_time;
1230 /* We need to show the menu here rather in the init function because
1231 * code expects to be able to tell if the menu is onscreen by
1232 * looking at the GTK_WIDGET_VISIBLE (menu)
1234 gtk_widget_show (GTK_WIDGET (menu));
1236 /* Position the menu, possibly changing the size request
1238 gtk_menu_position (menu);
1240 /* Compute the size of the toplevel and realize it so we
1241 * can scroll correctly.
1244 GtkRequisition tmp_request;
1245 GtkAllocation tmp_allocation = { 0, };
1247 gtk_widget_size_request (menu->toplevel, &tmp_request);
1249 tmp_allocation.width = tmp_request.width;
1250 tmp_allocation.height = tmp_request.height;
1252 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1254 gtk_widget_realize (GTK_WIDGET (menu));
1257 gtk_menu_scroll_to (menu, menu->scroll_offset);
1259 /* Once everything is set up correctly, map the toplevel window on
1262 gtk_widget_show (menu->toplevel);
1264 if (xgrab_shell == widget)
1265 popup_grab_on_window (widget->window, activate_time); /* Should always succeed */
1266 gtk_grab_add (GTK_WIDGET (menu));
1270 gtk_menu_popdown (GtkMenu *menu)
1272 GtkMenuPrivate *private;
1273 GtkMenuShell *menu_shell;
1275 g_return_if_fail (GTK_IS_MENU (menu));
1277 menu_shell = GTK_MENU_SHELL (menu);
1278 private = gtk_menu_get_private (menu);
1280 menu_shell->parent_menu_shell = NULL;
1281 menu_shell->active = FALSE;
1282 menu_shell->ignore_enter = FALSE;
1284 private->have_position = FALSE;
1286 gtk_menu_stop_scrolling (menu);
1288 gtk_menu_stop_navigating_submenu (menu);
1290 if (menu_shell->active_menu_item)
1292 if (menu->old_active_menu_item)
1293 g_object_unref (menu->old_active_menu_item);
1294 menu->old_active_menu_item = menu_shell->active_menu_item;
1295 g_object_ref (menu->old_active_menu_item);
1298 gtk_menu_shell_deselect (menu_shell);
1300 /* The X Grab, if present, will automatically be removed when we hide
1302 gtk_widget_hide (menu->toplevel);
1306 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1308 if (GTK_BIN (menu->toplevel)->child)
1310 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1314 /* We popped up the menu from the tearoff, so we need to
1315 * release the grab - we aren't actually hiding the menu.
1317 if (menu_shell->have_xgrab)
1319 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
1321 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1322 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1326 /* gtk_menu_popdown is called each time a menu item is selected from
1327 * a torn off menu. Only scroll back to the saved position if the
1328 * non-tearoff menu was popped down.
1330 if (!menu->tearoff_active)
1331 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1332 menu->tearoff_active = TRUE;
1335 gtk_widget_hide (GTK_WIDGET (menu));
1337 menu_shell->have_xgrab = FALSE;
1338 gtk_grab_remove (GTK_WIDGET (menu));
1340 menu_grab_transfer_window_destroy (menu);
1344 gtk_menu_get_active (GtkMenu *menu)
1349 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1351 if (!menu->old_active_menu_item)
1354 children = GTK_MENU_SHELL (menu)->children;
1358 child = children->data;
1359 children = children->next;
1361 if (GTK_BIN (child)->child)
1366 menu->old_active_menu_item = child;
1367 if (menu->old_active_menu_item)
1368 g_object_ref (menu->old_active_menu_item);
1371 return menu->old_active_menu_item;
1375 gtk_menu_set_active (GtkMenu *menu,
1381 g_return_if_fail (GTK_IS_MENU (menu));
1383 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1386 child = tmp_list->data;
1387 if (GTK_BIN (child)->child)
1389 if (menu->old_active_menu_item)
1390 g_object_unref (menu->old_active_menu_item);
1391 menu->old_active_menu_item = child;
1392 g_object_ref (menu->old_active_menu_item);
1398 gtk_menu_set_accel_group (GtkMenu *menu,
1399 GtkAccelGroup *accel_group)
1401 g_return_if_fail (GTK_IS_MENU (menu));
1403 if (menu->accel_group != accel_group)
1405 if (menu->accel_group)
1406 g_object_unref (menu->accel_group);
1407 menu->accel_group = accel_group;
1408 if (menu->accel_group)
1409 g_object_ref (menu->accel_group);
1410 _gtk_menu_refresh_accel_paths (menu, TRUE);
1415 gtk_menu_get_accel_group (GtkMenu *menu)
1417 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1419 return menu->accel_group;
1423 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1426 /* Menu items chain here to figure whether they can activate their
1427 * accelerators. Unlike ordinary widgets, menus allow accel
1428 * activation even if invisible since that's the usual case for
1429 * submenus/popup-menus. however, the state of the attach widget
1430 * affects the "activeness" of the menu.
1432 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1435 return gtk_widget_can_activate_accel (awidget, signal_id);
1437 return GTK_WIDGET_IS_SENSITIVE (widget);
1441 * gtk_menu_set_accel_path
1442 * @menu: a valid #GtkMenu
1443 * @accel_path: a valid accelerator path
1445 * Sets an accelerator path for this menu from which accelerator paths
1446 * for its immediate children, its menu items, can be constructed.
1447 * The main purpose of this function is to spare the programmer the
1448 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1449 * each menu item that should support runtime user changable accelerators.
1450 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1451 * each menu item of this menu, that contains a label describing its purpose,
1452 * automatically gets an accel path assigned. For example, a menu containing
1453 * menu items "New" and "Exit", will, after
1454 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1455 * has been called, assign its items the accel paths:
1456 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1457 * Assigning accel paths to menu items then enables the user to change
1458 * their accelerators at runtime. More details about accelerator paths
1459 * and their default setups can be found at gtk_accel_map_add_entry().
1462 gtk_menu_set_accel_path (GtkMenu *menu,
1463 const gchar *accel_path)
1465 g_return_if_fail (GTK_IS_MENU (menu));
1467 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1469 g_free (menu->accel_path);
1470 menu->accel_path = g_strdup (accel_path);
1471 if (menu->accel_path)
1472 _gtk_menu_refresh_accel_paths (menu, FALSE);
1477 gboolean group_changed;
1481 refresh_accel_paths_foreach (GtkWidget *widget,
1484 AccelPropagation *prop = data;
1486 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1487 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1488 prop->menu->accel_path,
1489 prop->menu->accel_group,
1490 prop->group_changed);
1494 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1495 gboolean group_changed)
1497 g_return_if_fail (GTK_IS_MENU (menu));
1499 if (menu->accel_path && menu->accel_group)
1501 AccelPropagation prop;
1504 prop.group_changed = group_changed;
1505 gtk_container_foreach (GTK_CONTAINER (menu),
1506 refresh_accel_paths_foreach,
1512 gtk_menu_reposition (GtkMenu *menu)
1514 g_return_if_fail (GTK_IS_MENU (menu));
1516 if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1517 gtk_menu_position (menu);
1521 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1524 g_return_if_fail (GTK_IS_MENU (menu));
1526 if (adjustment->value != menu->scroll_offset)
1527 gtk_menu_scroll_to (menu, adjustment->value);
1531 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1534 GdkGeometry geometry_hints;
1536 if (!menu->tearoff_window)
1539 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1541 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1542 width += menu->tearoff_scrollbar->requisition.width;
1545 geometry_hints.min_width = width;
1546 geometry_hints.max_width = width;
1548 geometry_hints.min_height = 0;
1549 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1551 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1554 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1558 gtk_menu_update_title (GtkMenu *menu)
1560 if (menu->tearoff_window)
1563 GtkWidget *attach_widget;
1565 title = gtk_menu_get_title (menu);
1568 attach_widget = gtk_menu_get_attach_widget (menu);
1569 if (GTK_IS_MENU_ITEM (attach_widget))
1571 GtkWidget *child = GTK_BIN (attach_widget)->child;
1572 if (GTK_IS_LABEL (child))
1573 title = gtk_label_get_text (GTK_LABEL (child));
1578 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1583 gtk_menu_get_toplevel (GtkWidget *menu)
1585 GtkWidget *attach, *toplevel;
1587 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
1589 if (GTK_IS_MENU_ITEM (attach))
1590 attach = attach->parent;
1592 if (GTK_IS_MENU (attach))
1593 return gtk_menu_get_toplevel (attach->parent);
1594 else if (GTK_IS_WIDGET (attach))
1596 toplevel = gtk_widget_get_toplevel (attach->parent);
1597 if (GTK_WIDGET_TOPLEVEL (toplevel))
1605 gtk_menu_set_tearoff_state (GtkMenu *menu,
1610 g_return_if_fail (GTK_IS_MENU (menu));
1612 if (menu->torn_off != torn_off)
1614 menu->torn_off = torn_off;
1615 menu->tearoff_active = torn_off;
1619 if (GTK_WIDGET_VISIBLE (menu))
1620 gtk_menu_popdown (menu);
1622 if (!menu->tearoff_window)
1624 GtkWidget *toplevel;
1626 menu->tearoff_window = gtk_widget_new (GTK_TYPE_WINDOW,
1627 "type", GTK_WINDOW_TOPLEVEL,
1628 "screen", gtk_widget_get_screen (menu->toplevel),
1629 "app_paintable", TRUE,
1633 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1634 GDK_WINDOW_TYPE_HINT_MENU);
1635 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1636 g_signal_connect (menu->tearoff_window, "destroy",
1637 G_CALLBACK (gtk_widget_destroyed), &menu->tearoff_window);
1638 g_signal_connect (menu->tearoff_window, "event",
1639 G_CALLBACK (gtk_menu_window_event), menu);
1641 gtk_menu_update_title (menu);
1643 gtk_widget_realize (menu->tearoff_window);
1645 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
1646 if (toplevel != NULL)
1647 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
1648 GTK_WINDOW (toplevel));
1650 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1651 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1653 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
1654 menu->tearoff_adjustment =
1655 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1657 GTK_WIDGET (menu)->requisition.height,
1661 g_object_connect (menu->tearoff_adjustment,
1662 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1664 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1666 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1667 menu->tearoff_scrollbar,
1670 if (menu->tearoff_adjustment->upper > height)
1671 gtk_widget_show (menu->tearoff_scrollbar);
1673 gtk_widget_show (menu->tearoff_hbox);
1676 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
1678 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
1680 /* Update menu->requisition
1682 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
1684 gtk_menu_set_tearoff_hints (menu, width);
1686 gtk_widget_realize (menu->tearoff_window);
1687 gtk_menu_position (menu);
1689 gtk_widget_show (GTK_WIDGET (menu));
1690 gtk_widget_show (menu->tearoff_window);
1692 gtk_menu_scroll_to (menu, 0);
1697 gtk_widget_hide (GTK_WIDGET (menu));
1698 gtk_widget_hide (menu->tearoff_window);
1699 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1700 gtk_widget_destroy (menu->tearoff_window);
1702 menu->tearoff_window = NULL;
1703 menu->tearoff_hbox = NULL;
1704 menu->tearoff_scrollbar = NULL;
1705 menu->tearoff_adjustment = NULL;
1711 * gtk_menu_get_tearoff_state:
1714 * Returns whether the menu is torn off. See
1715 * gtk_menu_set_tearoff_state ().
1717 * Return value: %TRUE if the menu is currently torn off.
1720 gtk_menu_get_tearoff_state (GtkMenu *menu)
1722 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
1724 return menu->torn_off;
1728 * gtk_menu_set_title:
1730 * @title: a string containing the title for the menu.
1732 * Sets the title string for the menu. The title is displayed when the menu
1733 * is shown as a tearoff menu.
1736 gtk_menu_set_title (GtkMenu *menu,
1739 g_return_if_fail (GTK_IS_MENU (menu));
1742 g_object_set_data_full (G_OBJECT (menu), "gtk-menu-title",
1743 g_strdup (title), (GtkDestroyNotify) g_free);
1745 g_object_set_data (G_OBJECT (menu), "gtk-menu-title", NULL);
1747 gtk_menu_update_title (menu);
1748 g_object_notify (G_OBJECT (menu), "tearoff_title");
1752 * gtk_menu_get_title:
1755 * Returns the title of the menu. See gtk_menu_set_title().
1757 * Return value: the title of the menu, or %NULL if the menu has no
1758 * title set on it. This string is owned by the widget and should
1759 * not be modified or freed.
1761 G_CONST_RETURN gchar *
1762 gtk_menu_get_title (GtkMenu *menu)
1764 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1766 return g_object_get_data (G_OBJECT (menu), "gtk-menu-title");
1770 gtk_menu_reorder_child (GtkMenu *menu,
1774 GtkMenuShell *menu_shell;
1775 g_return_if_fail (GTK_IS_MENU (menu));
1776 g_return_if_fail (GTK_IS_MENU_ITEM (child));
1777 menu_shell = GTK_MENU_SHELL (menu);
1778 if (g_list_find (menu_shell->children, child))
1780 menu_shell->children = g_list_remove (menu_shell->children, child);
1781 menu_shell->children = g_list_insert (menu_shell->children, child, position);
1782 if (GTK_WIDGET_VISIBLE (menu_shell))
1783 gtk_widget_queue_resize (GTK_WIDGET (menu_shell));
1788 gtk_menu_style_set (GtkWidget *widget,
1789 GtkStyle *previous_style)
1791 if (GTK_WIDGET_REALIZED (widget))
1793 GtkMenu *menu = GTK_MENU (widget);
1795 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1796 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1797 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1802 gtk_menu_realize (GtkWidget *widget)
1804 GdkWindowAttr attributes;
1805 gint attributes_mask;
1810 guint vertical_padding;
1812 g_return_if_fail (GTK_IS_MENU (widget));
1814 menu = GTK_MENU (widget);
1816 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1818 attributes.window_type = GDK_WINDOW_CHILD;
1819 attributes.x = widget->allocation.x;
1820 attributes.y = widget->allocation.y;
1821 attributes.width = widget->allocation.width;
1822 attributes.height = widget->allocation.height;
1823 attributes.wclass = GDK_INPUT_OUTPUT;
1824 attributes.visual = gtk_widget_get_visual (widget);
1825 attributes.colormap = gtk_widget_get_colormap (widget);
1827 attributes.event_mask = gtk_widget_get_events (widget);
1829 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
1830 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
1832 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1833 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1834 gdk_window_set_user_data (widget->window, widget);
1836 border_width = GTK_CONTAINER (widget)->border_width;
1838 gtk_widget_style_get (GTK_WIDGET (menu),
1839 "vertical-padding", &vertical_padding,
1842 attributes.x = border_width + widget->style->xthickness;
1843 attributes.y = border_width + widget->style->ythickness + vertical_padding;
1844 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
1845 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
1847 if (menu->upper_arrow_visible)
1849 attributes.y += MENU_SCROLL_ARROW_HEIGHT;
1850 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1852 if (menu->lower_arrow_visible)
1853 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1855 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
1856 gdk_window_set_user_data (menu->view_window, menu);
1860 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
1862 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
1863 gdk_window_set_user_data (menu->bin_window, menu);
1865 children = GTK_MENU_SHELL (menu)->children;
1868 child = children->data;
1869 children = children->next;
1871 gtk_widget_set_parent_window (child, menu->bin_window);
1874 widget->style = gtk_style_attach (widget->style, widget->window);
1875 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1876 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1877 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1879 if (GTK_MENU_SHELL (widget)->active_menu_item)
1880 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
1881 GTK_MENU_SHELL (widget)->active_menu_item);
1883 gdk_window_show (menu->bin_window);
1884 gdk_window_show (menu->view_window);
1888 gtk_menu_focus (GtkWidget *widget,
1889 GtkDirectionType direction)
1892 * A menu or its menu items cannot have focus
1897 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
1900 menu_grab_transfer_window_get (GtkMenu *menu)
1902 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1905 GdkWindowAttr attributes;
1906 gint attributes_mask;
1908 attributes.x = -100;
1909 attributes.y = -100;
1910 attributes.width = 10;
1911 attributes.height = 10;
1912 attributes.window_type = GDK_WINDOW_TEMP;
1913 attributes.wclass = GDK_INPUT_ONLY;
1914 attributes.override_redirect = TRUE;
1915 attributes.event_mask = 0;
1917 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
1919 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
1920 &attributes, attributes_mask);
1921 gdk_window_set_user_data (window, menu);
1923 gdk_window_show (window);
1925 g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", window);
1932 menu_grab_transfer_window_destroy (GtkMenu *menu)
1934 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1937 gdk_window_set_user_data (window, NULL);
1938 gdk_window_destroy (window);
1939 g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", NULL);
1944 gtk_menu_unrealize (GtkWidget *widget)
1948 g_return_if_fail (GTK_IS_MENU (widget));
1950 menu = GTK_MENU (widget);
1952 menu_grab_transfer_window_destroy (menu);
1954 gdk_window_set_user_data (menu->view_window, NULL);
1955 gdk_window_destroy (menu->view_window);
1956 menu->view_window = NULL;
1958 gdk_window_set_user_data (menu->bin_window, NULL);
1959 gdk_window_destroy (menu->bin_window);
1960 menu->bin_window = NULL;
1962 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1966 gtk_menu_size_request (GtkWidget *widget,
1967 GtkRequisition *requisition)
1971 GtkMenuShell *menu_shell;
1974 guint max_toggle_size;
1975 guint max_accel_width;
1976 guint vertical_padding;
1977 GtkRequisition child_requisition;
1978 GtkMenuPrivate *priv;
1980 g_return_if_fail (GTK_IS_MENU (widget));
1981 g_return_if_fail (requisition != NULL);
1983 menu = GTK_MENU (widget);
1984 menu_shell = GTK_MENU_SHELL (widget);
1985 priv = gtk_menu_get_private (menu);
1987 requisition->width = 0;
1988 requisition->height = 0;
1990 max_toggle_size = 0;
1991 max_accel_width = 0;
1993 g_free (priv->heights);
1994 priv->heights = g_new0 (guint, priv->rows);
1995 priv->heights_length = priv->rows;
1997 children = menu_shell->children;
2004 child = children->data;
2005 children = children->next;
2007 if (! GTK_WIDGET_VISIBLE (child))
2010 get_child_attach (child, &l, &r, &t, &b);
2012 /* It's important to size_request the child
2013 * before doing the toggle size request, in
2014 * case the toggle size request depends on the size
2015 * request of a child of the child (e.g. for ImageMenuItem)
2018 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
2019 gtk_widget_size_request (child, &child_requisition);
2021 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2022 max_toggle_size = MAX (max_toggle_size, toggle_size);
2023 max_accel_width = MAX (max_accel_width,
2024 GTK_MENU_ITEM (child)->accelerator_width);
2026 part = child_requisition.width / (r - l);
2027 requisition->width = MAX (requisition->width, part);
2029 part = child_requisition.height / (b - t);
2030 priv->heights[t] = MAX (priv->heights[t], part);
2033 for (i = 0; i < priv->rows; i++)
2034 requisition->height += priv->heights[i];
2036 requisition->width += max_toggle_size + max_accel_width;
2037 requisition->width *= priv->columns;
2038 requisition->width += (GTK_CONTAINER (menu)->border_width +
2039 widget->style->xthickness) * 2;
2041 gtk_widget_style_get (GTK_WIDGET (menu),
2042 "vertical-padding", &vertical_padding,
2044 requisition->height += (GTK_CONTAINER (menu)->border_width + vertical_padding +
2045 widget->style->ythickness) * 2;
2047 menu->toggle_size = max_toggle_size;
2049 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2051 if (menu->tearoff_active)
2052 gtk_menu_set_tearoff_hints (menu, requisition->width);
2056 gtk_menu_size_allocate (GtkWidget *widget,
2057 GtkAllocation *allocation)
2060 GtkMenuShell *menu_shell;
2062 GtkAllocation child_allocation;
2063 GtkRequisition child_requisition;
2064 GtkMenuPrivate *priv;
2068 guint vertical_padding;
2070 g_return_if_fail (GTK_IS_MENU (widget));
2071 g_return_if_fail (allocation != NULL);
2073 menu = GTK_MENU (widget);
2074 menu_shell = GTK_MENU_SHELL (widget);
2075 priv = gtk_menu_get_private (menu);
2077 widget->allocation = *allocation;
2078 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2080 gtk_widget_style_get (GTK_WIDGET (menu),
2081 "vertical-padding", &vertical_padding,
2084 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness;
2085 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness + vertical_padding;
2087 width = MAX (1, allocation->width - x * 2);
2088 height = MAX (1, allocation->height - y * 2);
2090 child_requisition.width -= x * 2;
2091 child_requisition.height -= y * 2;
2093 if (menu_shell->active)
2094 gtk_menu_scroll_to (menu, menu->scroll_offset);
2096 if (menu->upper_arrow_visible && !menu->tearoff_active)
2098 y += MENU_SCROLL_ARROW_HEIGHT;
2099 height -= MENU_SCROLL_ARROW_HEIGHT;
2102 if (menu->lower_arrow_visible && !menu->tearoff_active)
2103 height -= MENU_SCROLL_ARROW_HEIGHT;
2105 if (GTK_WIDGET_REALIZED (widget))
2107 gdk_window_move_resize (widget->window,
2108 allocation->x, allocation->y,
2109 allocation->width, allocation->height);
2111 gdk_window_move_resize (menu->view_window,
2118 if (menu_shell->children)
2120 gint base_width = width / priv->columns;
2122 children = menu_shell->children;
2125 child = children->data;
2126 children = children->next;
2128 if (GTK_WIDGET_VISIBLE (child))
2133 get_child_attach (child, &l, &r, &t, &b);
2135 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2138 tmp = priv->columns - l;
2139 l = priv->columns - r;
2143 child_allocation.width = (r - l) * base_width;
2144 child_allocation.height = 0;
2145 child_allocation.x = l * base_width;
2146 child_allocation.y = 0;
2148 for (i = 0; i < b; i++)
2151 child_allocation.y += priv->heights[i];
2153 child_allocation.height += priv->heights[i];
2156 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2159 gtk_widget_size_allocate (child, &child_allocation);
2160 gtk_widget_queue_draw (child);
2164 /* Resize the item window */
2165 if (GTK_WIDGET_REALIZED (widget))
2171 for (i = 0; i < priv->rows; i++)
2172 height += priv->heights[i];
2174 width = priv->columns * base_width;
2175 gdk_window_resize (menu->bin_window, width, height);
2178 if (menu->tearoff_active)
2180 if (allocation->height >= widget->requisition.height)
2182 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2184 gtk_widget_hide (menu->tearoff_scrollbar);
2185 gtk_menu_set_tearoff_hints (menu, allocation->width);
2187 gtk_menu_scroll_to (menu, 0);
2192 menu->tearoff_adjustment->upper = widget->requisition.height;
2193 menu->tearoff_adjustment->page_size = allocation->height;
2195 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2196 menu->tearoff_adjustment->upper)
2199 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2202 gtk_menu_scroll_to (menu, value);
2205 gtk_adjustment_changed (menu->tearoff_adjustment);
2207 if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2209 gtk_widget_show (menu->tearoff_scrollbar);
2210 gtk_menu_set_tearoff_hints (menu, allocation->width);
2218 gtk_menu_paint (GtkWidget *widget,
2219 GdkEventExpose *event)
2223 gint border_x, border_y;
2224 guint vertical_padding;
2226 g_return_if_fail (GTK_IS_MENU (widget));
2228 menu = GTK_MENU (widget);
2230 gtk_widget_style_get (GTK_WIDGET (menu),
2231 "vertical-padding", &vertical_padding,
2234 border_x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness;
2235 border_y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
2236 gdk_drawable_get_size (widget->window, &width, &height);
2238 if (event->window == widget->window)
2240 gint arrow_space = MENU_SCROLL_ARROW_HEIGHT - 2 * widget->style->ythickness;
2241 gint arrow_size = 0.7 * arrow_space;
2243 gtk_paint_box (widget->style,
2247 NULL, widget, "menu",
2249 if (menu->upper_arrow_visible && !menu->tearoff_active)
2251 gtk_paint_box (widget->style,
2253 menu->upper_arrow_prelight ?
2254 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2256 NULL, widget, "menu",
2259 width - 2 * border_x,
2260 MENU_SCROLL_ARROW_HEIGHT);
2262 gtk_paint_arrow (widget->style,
2264 menu->upper_arrow_prelight ?
2265 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2267 NULL, widget, "menu_scroll_arrow_up",
2270 (width - arrow_size ) / 2,
2271 border_y + widget->style->ythickness + (arrow_space - arrow_size)/2,
2272 arrow_size, arrow_size);
2275 if (menu->lower_arrow_visible && !menu->tearoff_active)
2277 gtk_paint_box (widget->style,
2279 menu->lower_arrow_prelight ?
2280 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2282 NULL, widget, "menu",
2284 height - border_y - MENU_SCROLL_ARROW_HEIGHT,
2286 MENU_SCROLL_ARROW_HEIGHT);
2288 gtk_paint_arrow (widget->style,
2290 menu->lower_arrow_prelight ?
2291 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2293 NULL, widget, "menu_scroll_arrow_down",
2296 (width - arrow_size) / 2,
2297 height - border_y - MENU_SCROLL_ARROW_HEIGHT +
2298 widget->style->ythickness + (arrow_space - arrow_size)/2,
2299 arrow_size, arrow_size);
2305 gtk_menu_expose (GtkWidget *widget,
2306 GdkEventExpose *event)
2308 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2309 g_return_val_if_fail (event != NULL, FALSE);
2311 if (GTK_WIDGET_DRAWABLE (widget))
2313 gtk_menu_paint (widget, event);
2315 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
2322 gtk_menu_show (GtkWidget *widget)
2324 GtkMenu *menu = GTK_MENU (widget);
2326 _gtk_menu_refresh_accel_paths (menu, FALSE);
2328 GTK_WIDGET_CLASS (parent_class)->show (widget);
2332 gtk_menu_button_press (GtkWidget *widget,
2333 GdkEventButton *event)
2335 /* Don't pop down the menu for releases over scroll arrows
2337 if (GTK_IS_MENU (widget))
2339 GtkMenu *menu = GTK_MENU (widget);
2341 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2345 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
2349 gtk_menu_button_release (GtkWidget *widget,
2350 GdkEventButton *event)
2352 /* Don't pop down the menu for releases over scroll arrows
2354 if (GTK_IS_MENU (widget))
2356 GtkMenu *menu = GTK_MENU (widget);
2358 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2362 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
2365 static const gchar *
2366 get_accel_path (GtkWidget *menu_item,
2371 GClosure *accel_closure;
2372 GtkAccelGroup *accel_group;
2374 path = _gtk_widget_get_accel_path (menu_item, locked);
2377 path = GTK_MENU_ITEM (menu_item)->accel_path;
2383 label = GTK_BIN (menu_item)->child;
2385 if (GTK_IS_ACCEL_LABEL (label))
2387 g_object_get (label,
2388 "accel_closure", &accel_closure,
2390 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
2392 *locked = accel_group->lock_count > 0;
2401 gtk_menu_key_press (GtkWidget *widget,
2404 GtkMenuShell *menu_shell;
2406 gboolean delete = FALSE;
2407 gboolean can_change_accels;
2408 gchar *accel = NULL;
2409 guint accel_key, accel_mods;
2410 GdkModifierType consumed_modifiers;
2411 GdkDisplay *display;
2413 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2414 g_return_val_if_fail (event != NULL, FALSE);
2416 menu_shell = GTK_MENU_SHELL (widget);
2417 menu = GTK_MENU (widget);
2419 gtk_menu_stop_navigating_submenu (menu);
2421 if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
2424 display = gtk_widget_get_display (widget);
2426 g_object_get (gtk_widget_get_settings (widget),
2427 "gtk-menu-bar-accel", &accel,
2428 "gtk-can-change-accels", &can_change_accels,
2434 GdkModifierType mods = 0;
2435 gboolean handled = FALSE;
2437 gtk_accelerator_parse (accel, &keyval, &mods);
2440 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
2442 /* FIXME this is wrong, needs to be in the global accel resolution
2443 * thing, to properly consider i18n etc., but that probably requires
2444 * AccelGroup changes etc.
2446 if (event->keyval == keyval &&
2447 (mods & event->state) == mods)
2449 g_signal_emit_by_name (menu, "cancel", 0);
2458 switch (event->keyval)
2469 /* Figure out what modifiers went into determining the key symbol */
2470 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
2471 event->hardware_keycode, event->state, event->group,
2472 NULL, NULL, NULL, &consumed_modifiers);
2474 accel_key = gdk_keyval_to_lower (event->keyval);
2475 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
2477 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
2478 * We re-upper case when we match against the keyval, but display and save in caseless form.
2480 if (accel_key != event->keyval)
2481 accel_mods |= GDK_SHIFT_MASK;
2483 /* Modify the accelerators */
2484 if (can_change_accels &&
2485 menu_shell->active_menu_item &&
2486 GTK_BIN (menu_shell->active_menu_item)->child && /* no separators */
2487 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
2488 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
2490 GtkWidget *menu_item = menu_shell->active_menu_item;
2491 gboolean locked, replace_accels = TRUE;
2494 path = get_accel_path (menu_item, &locked);
2495 if (!path || locked)
2497 /* can't change accelerators on menu_items without paths
2498 * (basically, those items are accelerator-locked).
2500 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
2501 gdk_display_beep (display);
2507 /* For the keys that act to delete the current setting, we delete
2508 * the current setting if there is one, otherwise, we set the
2509 * key as the accelerator.
2515 if (gtk_accel_map_lookup_entry (path, &key) &&
2516 (key.accel_key || key.accel_mods))
2522 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
2526 /* we failed, probably because this key is in use and
2529 /* g_print("failed to change\n"); */
2530 gdk_display_beep (display);
2539 gtk_menu_motion_notify (GtkWidget *widget,
2540 GdkEventMotion *event)
2542 GtkWidget *menu_item;
2544 GtkMenuShell *menu_shell;
2546 gboolean need_enter;
2548 if (GTK_IS_MENU (widget))
2549 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2551 /* We received the event for one of two reasons:
2553 * a) We are the active menu, and did gtk_grab_add()
2554 * b) The widget is a child of ours, and the event was propagated
2556 * Since for computation of navigation regions, we want the menu which
2557 * is the parent of the menu item, for a), we need to find that menu,
2558 * which may be different from 'widget'.
2560 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2561 if (!menu_item || !GTK_IS_MENU_ITEM (menu_item) ||
2562 !_gtk_menu_item_is_selectable (menu_item) ||
2563 !GTK_IS_MENU (menu_item->parent))
2566 menu_shell = GTK_MENU_SHELL (menu_item->parent);
2567 menu = GTK_MENU (menu_shell);
2569 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
2571 /* Check to see if we are within an active submenu's navigation region
2573 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2578 /* The menu is now sensitive to enter events on its items, but
2579 * was previously sensitive. So we fake an enter event.
2583 menu_shell->ignore_enter = FALSE;
2585 gdk_drawable_get_size (event->window, &width, &height);
2586 if (event->x >= 0 && event->x < width &&
2587 event->y >= 0 && event->y < height)
2589 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2592 send_event->crossing.window = g_object_ref (event->window);
2593 send_event->crossing.time = event->time;
2594 send_event->crossing.send_event = TRUE;
2595 send_event->crossing.x_root = event->x_root;
2596 send_event->crossing.y_root = event->y_root;
2597 send_event->crossing.x = event->x;
2598 send_event->crossing.y = event->y;
2600 /* We send the event to 'widget', the currently active menu,
2601 * instead of 'menu', the menu that the pointer is in. This
2602 * will ensure that the event will be ignored unless the
2603 * menuitem is a child of the active menu or some parent
2604 * menu of the active menu.
2606 result = gtk_widget_event (widget, send_event);
2607 gdk_event_free (send_event);
2617 gtk_menu_scroll_timeout (gpointer data)
2622 gint view_width, view_height;
2624 GDK_THREADS_ENTER ();
2626 menu = GTK_MENU (data);
2627 widget = GTK_WIDGET (menu);
2629 offset = menu->scroll_offset + menu->scroll_step;
2631 /* If we scroll upward and the non-visible top part
2632 * is smaller than the scroll arrow it would be
2633 * pretty stupid to show the arrow and taking more
2634 * screen space than just scrolling to the top.
2636 if ((menu->scroll_step < 0) && (offset < MENU_SCROLL_ARROW_HEIGHT))
2639 /* Don't scroll over the top if we weren't before: */
2640 if ((menu->scroll_offset >= 0) && (offset < 0))
2643 gdk_drawable_get_size (widget->window, &view_width, &view_height);
2645 /* Don't scroll past the bottom if we weren't before: */
2646 if (menu->scroll_offset > 0)
2647 view_height -= MENU_SCROLL_ARROW_HEIGHT;
2649 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
2650 (offset + view_height > widget->requisition.height))
2651 offset = widget->requisition.height - view_height;
2653 gtk_menu_scroll_to (menu, offset);
2655 GDK_THREADS_LEAVE ();
2661 gtk_menu_handle_scrolling (GtkMenu *menu, gboolean enter)
2663 GtkMenuShell *menu_shell;
2669 gboolean scroll_fast = FALSE;
2670 guint vertical_padding;
2672 menu_shell = GTK_MENU_SHELL (menu);
2674 gdk_window_get_pointer (GTK_WIDGET (menu)->window, &x, &y, NULL);
2675 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
2677 gtk_widget_style_get (GTK_WIDGET (menu),
2678 "vertical-padding", &vertical_padding,
2681 border = GTK_CONTAINER (menu)->border_width +
2682 GTK_WIDGET (menu)->style->ythickness + vertical_padding;
2684 if (menu->upper_arrow_visible && !menu->tearoff_active)
2689 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2692 if ((x >= rect.x) && (x < rect.x + rect.width) &&
2693 (y >= rect.y) && (y < rect.y + rect.height))
2696 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
2699 if (enter && in_arrow &&
2700 (!menu->upper_arrow_prelight || menu->scroll_fast != scroll_fast))
2702 menu->upper_arrow_prelight = TRUE;
2703 menu->scroll_fast = scroll_fast;
2704 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2706 /* Deselect the active item so that any submenus are poped down */
2707 gtk_menu_shell_deselect (menu_shell);
2709 gtk_menu_remove_scroll_timeout (menu);
2710 menu->scroll_step = (scroll_fast) ? -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
2711 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2712 gtk_menu_scroll_timeout,
2715 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
2717 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2719 gtk_menu_stop_scrolling (menu);
2723 if (menu->lower_arrow_visible && !menu->tearoff_active)
2726 rect.y = height - border - MENU_SCROLL_ARROW_HEIGHT;
2728 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2731 if ((x >= rect.x) && (x < rect.x + rect.width) &&
2732 (y >= rect.y) && (y < rect.y + rect.height))
2735 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
2738 if (enter && in_arrow &&
2739 (!menu->lower_arrow_prelight || menu->scroll_fast != scroll_fast))
2741 menu->lower_arrow_prelight = TRUE;
2742 menu->scroll_fast = scroll_fast;
2743 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2745 /* Deselect the active item so that any submenus are poped down */
2746 gtk_menu_shell_deselect (menu_shell);
2748 gtk_menu_remove_scroll_timeout (menu);
2749 menu->scroll_step = (scroll_fast) ? MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
2750 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2751 gtk_menu_scroll_timeout,
2754 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
2756 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2758 gtk_menu_stop_scrolling (menu);
2764 gtk_menu_enter_notify (GtkWidget *widget,
2765 GdkEventCrossing *event)
2767 GtkWidget *menu_item;
2769 if (widget && GTK_IS_MENU (widget))
2771 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
2773 if (!menu_shell->ignore_enter)
2774 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2777 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
2778 * will not correspond to the event widget's parent. Check to see
2779 * if we are in the parent's navigation region.
2781 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2782 if (menu_item && GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
2783 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent), event->x_root, event->y_root))
2786 return GTK_WIDGET_CLASS (parent_class)->enter_notify_event (widget, event);
2790 gtk_menu_leave_notify (GtkWidget *widget,
2791 GdkEventCrossing *event)
2793 GtkMenuShell *menu_shell;
2795 GtkMenuItem *menu_item;
2796 GtkWidget *event_widget;
2798 menu = GTK_MENU (widget);
2799 menu_shell = GTK_MENU_SHELL (widget);
2801 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2804 gtk_menu_handle_scrolling (menu, FALSE);
2806 event_widget = gtk_get_event_widget ((GdkEvent*) event);
2808 if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
2811 menu_item = GTK_MENU_ITEM (event_widget);
2813 /* Here we check to see if we're leaving an active menu item with a submenu,
2814 * in which case we enter submenu navigation mode.
2816 if (menu_shell->active_menu_item != NULL
2817 && menu_item->submenu != NULL
2818 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
2820 if (GTK_MENU_SHELL (menu_item->submenu)->active)
2822 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
2827 return GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event);
2831 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
2833 if (menu->navigation_region)
2835 gdk_region_destroy (menu->navigation_region);
2836 menu->navigation_region = NULL;
2839 if (menu->navigation_timeout)
2841 g_source_remove (menu->navigation_timeout);
2842 menu->navigation_timeout = 0;
2846 /* When the timeout is elapsed, the navigation region is destroyed
2847 * and the menuitem under the pointer (if any) is selected.
2850 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
2852 GtkMenu *menu = user_data;
2853 GdkWindow *child_window;
2855 GDK_THREADS_ENTER ();
2857 gtk_menu_stop_navigating_submenu (menu);
2859 if (GTK_WIDGET_REALIZED (menu))
2861 child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
2865 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2867 send_event->crossing.window = g_object_ref (child_window);
2868 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
2869 send_event->crossing.send_event = TRUE;
2871 GTK_WIDGET_CLASS (parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
2873 gdk_event_free (send_event);
2877 GDK_THREADS_LEAVE ();
2883 gtk_menu_navigating_submenu (GtkMenu *menu,
2887 if (menu->navigation_region)
2889 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
2893 gtk_menu_stop_navigating_submenu (menu);
2900 #undef DRAW_STAY_UP_TRIANGLE
2902 #ifdef DRAW_STAY_UP_TRIANGLE
2905 draw_stay_up_triangle (GdkWindow *window,
2908 /* Draw ugly color all over the stay-up triangle */
2909 GdkColor ugly_color = { 0, 50000, 10000, 10000 };
2910 GdkGCValues gc_values;
2912 GdkRectangle clipbox;
2914 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
2915 ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
2916 gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
2917 gdk_gc_set_clip_region (ugly_gc, region);
2919 gdk_region_get_clipbox (region, &clipbox);
2921 gdk_draw_rectangle (window,
2924 clipbox.x, clipbox.y,
2925 clipbox.width, clipbox.height);
2927 g_object_unref (ugly_gc);
2932 flip_region (GdkRegion *region,
2937 GdkRectangle *rectangles;
2938 GdkRectangle clipbox;
2939 GdkRegion *new_region;
2942 new_region = gdk_region_new ();
2944 gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
2945 gdk_region_get_clipbox (region, &clipbox);
2947 for (i = 0; i < n_rectangles; ++i)
2949 GdkRectangle rect = rectangles[i];
2952 rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
2955 rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
2957 gdk_region_union_with_rect (new_region, &rect);
2960 g_free (rectangles);
2966 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
2967 GtkMenuItem *menu_item,
2968 GdkEventCrossing *event)
2970 gint submenu_left = 0;
2971 gint submenu_right = 0;
2972 gint submenu_top = 0;
2973 gint submenu_bottom = 0;
2977 GtkWidget *event_widget;
2979 g_return_if_fail (menu_item->submenu != NULL);
2980 g_return_if_fail (event != NULL);
2982 event_widget = gtk_get_event_widget ((GdkEvent*) event);
2984 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
2985 gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
2987 submenu_right = submenu_left + width;
2988 submenu_bottom = submenu_top + height;
2990 gdk_drawable_get_size (event_widget->window, &width, &height);
2992 if (event->x >= 0 && event->x < width)
2995 gboolean flip_y = FALSE;
2996 gboolean flip_x = FALSE;
2998 gtk_menu_stop_navigating_submenu (menu);
3000 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
3003 point[0].x = event->x_root;
3004 point[1].x = submenu_left;
3009 point[0].x = event->x_root + 1;
3010 point[1].x = 2 * (event->x_root + 1) - submenu_right;
3018 point[0].y = event->y_root + 1;
3019 point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
3021 if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
3029 point[0].y = event->y_root;
3030 point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
3032 if (point[0].y >= submenu_bottom)
3036 point[2].x = point[1].x;
3037 point[2].y = point[0].y;
3039 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
3041 if (flip_x || flip_y)
3043 GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
3044 gdk_region_destroy (menu->navigation_region);
3045 menu->navigation_region = new_region;
3048 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3049 "gtk-menu-popdown-delay", &popdown_delay,
3052 menu->navigation_timeout = g_timeout_add (popdown_delay,
3053 gtk_menu_stop_navigating_submenu_cb, menu);
3055 #ifdef DRAW_STAY_UP_TRIANGLE
3056 draw_stay_up_triangle (gdk_get_default_root_window(),
3057 menu->navigation_region);
3063 gtk_menu_deactivate (GtkMenuShell *menu_shell)
3067 g_return_if_fail (GTK_IS_MENU (menu_shell));
3069 parent = menu_shell->parent_menu_shell;
3071 menu_shell->activate_time = 0;
3072 gtk_menu_popdown (GTK_MENU (menu_shell));
3075 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
3079 gtk_menu_position (GtkMenu *menu)
3082 GtkRequisition requisition;
3083 GtkMenuPrivate *private;
3089 GdkScreen *pointer_screen;
3090 GdkRectangle monitor;
3092 g_return_if_fail (GTK_IS_MENU (menu));
3094 widget = GTK_WIDGET (menu);
3096 screen = gtk_widget_get_screen (widget);
3097 gdk_display_get_pointer (gdk_screen_get_display (screen),
3098 &pointer_screen, &x, &y, NULL);
3100 /* We need the requisition to figure out the right place to
3101 * popup the menu. In fact, we always need to ask here, since
3102 * if a size_request was queued while we weren't popped up,
3103 * the requisition won't have been recomputed yet.
3105 gtk_widget_size_request (widget, &requisition);
3107 if (pointer_screen != screen)
3109 /* Pointer is on a different screen; roughly center the
3110 * menu on the screen. If someone was using multiscreen
3111 * + Xinerama together they'd probably want something
3112 * fancier; but that is likely to be vanishingly rare.
3114 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
3115 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
3118 private = gtk_menu_get_private (menu);
3119 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
3123 if (menu->position_func)
3125 (* menu->position_func) (menu, &x, &y, &push_in, menu->position_func_data);
3126 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3130 gint space_left, space_right, space_above, space_below;
3133 gint xthickness = widget->style->xthickness;
3134 gint ythickness = widget->style->ythickness;
3135 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
3137 /* The placement of popup menus horizontally works like this (with
3138 * RTL in parentheses)
3140 * - If there is enough room to the right (left) of the mouse cursor,
3141 * position the menu there.
3143 * - Otherwise, if if there is enough room to the left (right) of the
3144 * mouse cursor, position the menu there.
3146 * - Otherwise if the menu is smaller than the monitor, position it
3147 * on the side of the mouse cursor that has the most space available
3149 * - Otherwise (if there is simply not enough room for the menu on the
3150 * monitor), position it as far left (right) as possible.
3152 * Positioning in the vertical direction is similar: first try below
3153 * mouse cursor, then above.
3155 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3157 space_left = x - monitor.x;
3158 space_right = monitor.x + monitor.width - x - 1;
3159 space_above = y - monitor.y;
3160 space_below = monitor.y + monitor.height - y - 1;
3162 /* position horizontally */
3164 /* the amount of space we need to position the menu. Note the
3165 * menu is offset "xthickness" pixels
3167 needed_width = requisition.width - xthickness;
3169 if (needed_width <= space_left ||
3170 needed_width <= space_right)
3172 if ((rtl && needed_width <= space_left) ||
3173 (!rtl && needed_width > space_right))
3176 x = x + xthickness - requisition.width + 1;
3180 /* position right */
3184 /* x is clamped on-screen further down */
3186 else if (requisition.width <= monitor.width)
3188 /* the menu is too big to fit on either side of the mouse
3189 * cursor, but smaller than the monitor. Position it on
3190 * the side that has the most space
3192 if (space_left > space_right)
3200 x = monitor.x + monitor.width - requisition.width;
3203 else /* menu is simply too big for the monitor */
3208 x = monitor.x + monitor.width - requisition.width;
3217 /* Position vertically. The algorithm is the same as above, but
3218 * simpler because we don't have to take RTL into account.
3220 needed_height = requisition.height - ythickness;
3222 if (needed_height <= space_above ||
3223 needed_height <= space_below)
3225 if (needed_height <= space_below)
3228 y = y + ythickness - requisition.height + 1;
3230 y = CLAMP (y, monitor.y,
3231 monitor.y + monitor.height - requisition.height);
3233 else if (needed_height > space_below && needed_height > space_above)
3235 if (space_below >= space_above)
3236 y = monitor.y + monitor.height - requisition.height;
3250 menu_height = GTK_WIDGET (menu)->requisition.height;
3252 if (y + menu_height > monitor.y + monitor.height)
3254 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
3255 y = (monitor.y + monitor.height) - menu_height;
3260 scroll_offset += monitor.y - y;
3265 /* FIXME: should this be done in the various position_funcs ? */
3266 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
3268 if (GTK_MENU_SHELL (menu)->active)
3270 private->have_position = TRUE;
3275 if (y + requisition.height > monitor.y + monitor.height)
3276 requisition.height = (monitor.y + monitor.height) - y;
3280 scroll_offset += monitor.y - y;
3281 requisition.height -= monitor.y - y;
3285 if (scroll_offset > 0)
3286 scroll_offset += MENU_SCROLL_ARROW_HEIGHT;
3288 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
3291 if (!GTK_MENU_SHELL (menu)->active)
3293 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
3294 requisition.width, requisition.height);
3297 menu->scroll_offset = scroll_offset;
3301 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
3303 if (menu->timeout_id)
3305 g_source_remove (menu->timeout_id);
3306 menu->timeout_id = 0;
3311 gtk_menu_stop_scrolling (GtkMenu *menu)
3313 gtk_menu_remove_scroll_timeout (menu);
3315 menu->upper_arrow_prelight = FALSE;
3316 menu->lower_arrow_prelight = FALSE;
3320 gtk_menu_scroll_to (GtkMenu *menu,
3325 gint view_width, view_height;
3327 gboolean last_visible;
3329 guint vertical_padding;
3331 widget = GTK_WIDGET (menu);
3333 if (menu->tearoff_active &&
3334 menu->tearoff_adjustment &&
3335 (menu->tearoff_adjustment->value != offset))
3337 menu->tearoff_adjustment->value =
3339 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
3340 gtk_adjustment_value_changed (menu->tearoff_adjustment);
3343 /* Move/resize the viewport according to arrows: */
3344 view_width = widget->allocation.width;
3345 view_height = widget->allocation.height;
3347 gtk_widget_style_get (GTK_WIDGET (menu),
3348 "vertical-padding", &vertical_padding,
3351 border_width = GTK_CONTAINER (menu)->border_width;
3352 view_width -= (border_width + widget->style->xthickness) * 2;
3353 view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
3354 menu_height = widget->requisition.height -
3355 (border_width + widget->style->ythickness + vertical_padding) * 2;
3357 x = border_width + widget->style->xthickness;
3358 y = border_width + widget->style->ythickness + vertical_padding;
3360 if (!menu->tearoff_active)
3362 last_visible = menu->upper_arrow_visible;
3363 menu->upper_arrow_visible = offset > 0;
3365 if (menu->upper_arrow_visible)
3366 view_height -= MENU_SCROLL_ARROW_HEIGHT;
3368 if ( (last_visible != menu->upper_arrow_visible) &&
3369 !menu->upper_arrow_visible)
3371 menu->upper_arrow_prelight = FALSE;
3373 /* If we hid the upper arrow, possibly remove timeout */
3374 if (menu->scroll_step < 0)
3376 gtk_menu_stop_scrolling (menu);
3377 gtk_widget_queue_draw (GTK_WIDGET (menu));
3381 last_visible = menu->lower_arrow_visible;
3382 menu->lower_arrow_visible = offset < menu_height - view_height;
3384 if (menu->lower_arrow_visible)
3385 view_height -= MENU_SCROLL_ARROW_HEIGHT;
3387 if ( (last_visible != menu->lower_arrow_visible) &&
3388 !menu->lower_arrow_visible)
3390 menu->lower_arrow_prelight = FALSE;
3392 /* If we hid the lower arrow, possibly remove timeout */
3393 if (menu->scroll_step > 0)
3395 gtk_menu_stop_scrolling (menu);
3396 gtk_widget_queue_draw (GTK_WIDGET (menu));
3400 if (menu->upper_arrow_visible)
3401 y += MENU_SCROLL_ARROW_HEIGHT;
3404 /* Scroll the menu: */
3405 if (GTK_WIDGET_REALIZED (menu))
3406 gdk_window_move (menu->bin_window, 0, -offset);
3408 if (GTK_WIDGET_REALIZED (menu))
3409 gdk_window_move_resize (menu->view_window,
3415 menu->scroll_offset = offset;
3419 compute_child_offset (GtkMenu *menu,
3420 GtkWidget *menu_item,
3423 gboolean *is_last_child)
3425 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3426 guint item_top_attach;
3427 guint item_bottom_attach;
3428 gint child_offset = 0;
3431 gtk_container_child_get (GTK_CONTAINER (menu), menu_item,
3432 "top_attach", &item_top_attach,
3433 "bottom_attach", &item_bottom_attach,
3436 /* there is a possibility that we get called before _size_request, so
3437 * check the height table for safety.
3439 if (!priv->heights || priv->heights_length < priv->rows)
3442 /* when we have a row with only invisible children, it's height will
3443 * be zero, so there's no need to check WIDGET_VISIBLE here
3445 for (i = 0; i < item_top_attach; i++)
3446 child_offset += priv->heights[i];
3449 *is_last_child = (item_bottom_attach == priv->rows);
3451 *offset = child_offset;
3453 *height = priv->heights[item_top_attach];
3459 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
3460 GtkWidget *menu_item)
3463 gint child_offset, child_height;
3467 gboolean last_child = 0;
3469 menu = GTK_MENU (menu_shell);
3471 /* We need to check if the selected item fully visible.
3472 * If not we need to scroll the menu so that it becomes fully
3476 if (compute_child_offset (menu, menu_item,
3477 &child_offset, &child_height, &last_child))
3479 guint vertical_padding;
3481 y = menu->scroll_offset;
3482 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3484 gtk_widget_style_get (GTK_WIDGET (menu),
3485 "vertical-padding", &vertical_padding,
3488 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
3490 if (child_offset < y)
3492 /* Ignore the enter event we might get if the pointer is on the menu
3494 menu_shell->ignore_enter = TRUE;
3495 gtk_menu_scroll_to (menu, child_offset);
3500 if (menu->upper_arrow_visible && !menu->tearoff_active)
3501 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3502 if (menu->lower_arrow_visible && !menu->tearoff_active)
3503 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3505 if (child_offset + child_height > y + height - arrow_height)
3508 if (!last_child && !menu->tearoff_active)
3509 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3511 y = child_offset + child_height - height + arrow_height;
3512 if ((y > 0) && !menu->tearoff_active)
3514 /* Need upper arrow */
3515 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3516 y = child_offset + child_height - height + arrow_height;
3518 /* Ignore the enter event we might get if the pointer is on the menu
3520 menu_shell->ignore_enter = TRUE;
3521 gtk_menu_scroll_to (menu, y);
3529 gtk_menu_select_item (GtkMenuShell *menu_shell,
3530 GtkWidget *menu_item)
3532 GtkMenu *menu = GTK_MENU (menu_shell);
3534 if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
3535 gtk_menu_scroll_item_visible (menu_shell, menu_item);
3537 GTK_MENU_SHELL_CLASS (parent_class)->select_item (menu_shell, menu_item);
3541 /* Reparent the menu, taking care of the refcounting
3543 * If unrealize is true we force a unrealize while reparenting the parent.
3544 * This can help eliminate flicker in some cases.
3546 * What happens is that when the menu is unrealized and then re-realized,
3547 * the allocations are as follows:
3549 * parent - 1x1 at (0,0)
3550 * child1 - 100x20 at (0,0)
3551 * child2 - 100x20 at (0,20)
3552 * child3 - 100x20 at (0,40)
3554 * That is, the parent is small but the children are full sized. Then,
3555 * when the queued_resize gets processed, the parent gets resized to
3558 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
3559 * contains the following logic:
3561 * - if a move or resize operation on a window would change the clip
3562 * region on the children, then before the window is resized
3563 * the background for children is temporarily set to None, the
3564 * move/resize done, and the background for the children restored.
3566 * So, at the point where the parent is resized to final size, the
3567 * background for the children is temporarily None, and thus they
3568 * are not cleared to the background color and the previous background
3569 * (the image of the menu) is left in place.
3572 gtk_menu_reparent (GtkMenu *menu,
3573 GtkWidget *new_parent,
3576 GtkObject *object = GTK_OBJECT (menu);
3577 GtkWidget *widget = GTK_WIDGET (menu);
3578 gboolean was_floating = GTK_OBJECT_FLOATING (object);
3580 g_object_ref (object);
3581 gtk_object_sink (object);
3585 g_object_ref (object);
3586 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
3587 gtk_container_add (GTK_CONTAINER (new_parent), widget);
3588 g_object_unref (object);
3591 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
3594 GTK_OBJECT_SET_FLAGS (object, GTK_FLOATING);
3596 g_object_unref (object);
3600 gtk_menu_show_all (GtkWidget *widget)
3602 /* Show children, but not self. */
3603 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
3608 gtk_menu_hide_all (GtkWidget *widget)
3610 /* Hide children, but not self. */
3611 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
3615 * gtk_menu_set_screen:
3616 * @menu: a #GtkMenu.
3617 * @screen: a #GdkScreen, or %NULL if the screen should be
3618 * determined by the widget the menu is attached to.
3620 * Sets the #GdkScreen on which the menu will be displayed.
3625 gtk_menu_set_screen (GtkMenu *menu,
3628 g_return_if_fail (GTK_IS_MENU (menu));
3629 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
3631 g_object_set_data (G_OBJECT (menu), "gtk-menu-explicit-screen", screen);
3635 menu_change_screen (menu, screen);
3639 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
3641 attach_widget_screen_changed (attach_widget, NULL, menu);
3647 * @menu: a #GtkMenu.
3648 * @child: a #GtkMenuItem.
3649 * @left_attach: The column number to attach the left side of the item to.
3650 * @right_attach: The column number to attach the right side of the item to.
3651 * @top_attach: The row number to attach the top of the item to.
3652 * @bottom_attach: The row number to attach the bottom of the item to.
3654 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
3655 * an item will occupy is specified by @left_attach, @right_attach,
3656 * @top_attach and @bottom_attach. These each represent the leftmost,
3657 * rightmost, uppermost and lower column and row numbers of the table.
3658 * (Columns and rows are indexed from zero).
3663 gtk_menu_attach (GtkMenu *menu,
3668 guint bottom_attach)
3670 g_return_if_fail (GTK_IS_MENU (menu));
3671 g_return_if_fail (GTK_IS_MENU_ITEM (child));
3673 g_return_if_fail (left_attach < right_attach);
3674 g_return_if_fail (top_attach < bottom_attach);
3677 gtk_menu_shell_append (GTK_MENU_SHELL (menu), child);
3679 gtk_container_child_set (GTK_CONTAINER (menu), child,
3680 "left_attach", left_attach,
3681 "right_attach", right_attach,
3682 "top_attach", top_attach,
3683 "bottom_attach", bottom_attach,
3688 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
3692 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
3693 "gtk-menu-popup-delay", &popup_delay,
3700 find_child_containing (GtkMenuShell *menu_shell,
3708 /* find a child which includes the area given by
3709 * left, right, top, bottom.
3712 for (list = menu_shell->children; list; list = list->next)
3716 if (!_gtk_menu_item_is_selectable (list->data))
3719 get_child_attach (list->data, &l, &r, &t, &b);
3721 if (l <= left && right <= r
3722 && t <= top && bottom <= b)
3723 return GTK_WIDGET (list->data);
3730 gtk_menu_move_current (GtkMenuShell *menu_shell,
3731 GtkMenuDirectionType direction)
3733 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu_shell));
3735 /* use special table menu key bindings */
3736 if (menu_shell->active_menu_item && priv->columns > 1)
3740 gboolean rtl = (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL);
3741 GtkWidget *match = NULL;
3743 get_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
3745 if (direction == GTK_MENU_DIR_NEXT)
3747 for (i = b; i < priv->rows; i++)
3749 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
3757 for (i = 0; i < t; i++)
3759 match = find_child_containing (menu_shell,
3760 l, l + 1, i, i + 1);
3766 else if (direction == GTK_MENU_DIR_PREV)
3768 for (i = t; i > 0; i--)
3770 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
3778 for (i = priv->rows; i > b; i--)
3780 match = find_child_containing (menu_shell,
3781 l, l + 1, i - 1, i);
3787 else if ((!rtl && direction == GTK_MENU_DIR_PARENT)
3788 || (rtl && direction == GTK_MENU_DIR_CHILD))
3790 /* we go one left if possible */
3792 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
3796 GtkWidget *parent = menu_shell->parent_menu_shell;
3799 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
3800 match = menu_shell->active_menu_item;
3803 else if ((!rtl && direction == GTK_MENU_DIR_CHILD)
3804 || (rtl && direction == GTK_MENU_DIR_PARENT))
3806 /* we go one right if possible */
3807 if (r < priv->columns)
3808 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
3812 GtkWidget *parent = menu_shell->parent_menu_shell;
3814 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
3816 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
3817 match = menu_shell->active_menu_item;
3823 gtk_menu_shell_select_item (menu_shell, match);
3828 GTK_MENU_SHELL_CLASS (parent_class)->move_current (menu_shell, direction);
3832 get_visible_size (GtkMenu *menu)
3834 GtkWidget *widget = GTK_WIDGET (menu);
3835 GtkContainer *container = GTK_CONTAINER (menu);
3837 gint menu_height = (widget->allocation.height
3838 - 2 * (container->border_width
3839 + widget->style->ythickness));
3841 if (menu->upper_arrow_visible && !menu->tearoff_active)
3842 menu_height -= MENU_SCROLL_ARROW_HEIGHT;
3843 if (menu->lower_arrow_visible && !menu->tearoff_active)
3844 menu_height -= MENU_SCROLL_ARROW_HEIGHT;
3849 /* Find the sensitive on-screen child containing @y, or if none,
3850 * the nearest selectable onscreen child. (%NULL if none)
3853 child_at (GtkMenu *menu,
3856 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3857 GtkWidget *child = NULL;
3858 gint child_offset = 0;
3861 gint lower, upper; /* Onscreen bounds */
3863 menu_height = get_visible_size (menu);
3864 lower = menu->scroll_offset;
3865 upper = menu->scroll_offset + menu_height;
3867 for (children = menu_shell->children; children; children = children->next)
3869 if (GTK_WIDGET_VISIBLE (children->data))
3871 GtkRequisition child_requisition;
3873 gtk_widget_size_request (children->data, &child_requisition);
3875 if (_gtk_menu_item_is_selectable (children->data) &&
3876 child_offset >= lower &&
3877 child_offset + child_requisition.height <= upper)
3879 child = children->data;
3881 if (child_offset + child_requisition.height > y &&
3882 !GTK_IS_TEAROFF_MENU_ITEM (child))
3886 child_offset += child_requisition.height;
3894 get_menu_height (GtkMenu *menu)
3897 GtkWidget *widget = GTK_WIDGET (menu);
3899 height = widget->requisition.height;
3900 height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
3902 if (menu->upper_arrow_visible && !menu->tearoff_active)
3903 height -= MENU_SCROLL_ARROW_HEIGHT;
3905 if (menu->lower_arrow_visible && !menu->tearoff_active)
3906 height -= MENU_SCROLL_ARROW_HEIGHT;
3912 gtk_menu_real_move_scroll (GtkMenu *menu,
3915 gint page_size = get_visible_size (menu);
3916 gint end_position = get_menu_height (menu);
3917 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3921 case GTK_SCROLL_PAGE_UP:
3922 case GTK_SCROLL_PAGE_DOWN:
3926 gint child_offset = 0;
3927 gboolean old_upper_arrow_visible;
3930 if (type == GTK_SCROLL_PAGE_UP)
3935 if (menu_shell->active_menu_item)
3939 compute_child_offset (menu, menu_shell->active_menu_item,
3940 &child_offset, &child_height, NULL);
3941 child_offset += child_height / 2;
3944 menu_shell->ignore_enter = TRUE;
3945 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
3946 old_offset = menu->scroll_offset;
3948 new_offset = menu->scroll_offset + step;
3949 new_offset = CLAMP (new_offset, 0, end_position - page_size);
3951 gtk_menu_scroll_to (menu, new_offset);
3953 if (menu_shell->active_menu_item)
3955 GtkWidget *new_child;
3956 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
3958 if (menu->scroll_offset != old_offset)
3959 step = menu->scroll_offset - old_offset;
3961 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * MENU_SCROLL_ARROW_HEIGHT;
3963 new_child = child_at (menu, child_offset + step);
3965 gtk_menu_shell_select_item (menu_shell, new_child);
3969 case GTK_SCROLL_START:
3970 /* Ignore the enter event we might get if the pointer is on the menu
3972 menu_shell->ignore_enter = TRUE;
3973 gtk_menu_scroll_to (menu, 0);
3974 gtk_menu_shell_select_first (menu_shell, TRUE);
3976 case GTK_SCROLL_END:
3977 /* Ignore the enter event we might get if the pointer is on the menu
3979 menu_shell->ignore_enter = TRUE;
3980 gtk_menu_scroll_to (menu, end_position - page_size);
3981 _gtk_menu_shell_select_last (menu_shell, TRUE);
3990 * gtk_menu_set_monitor:
3992 * @monitor_num: the number of the monitor on which the menu should
3995 * Informs GTK+ on which monitor a menu should be popped up.
3996 * See gdk_screen_get_monitor_geometry().
3998 * This function should be called from a #GtkMenuPositionFunc if the
3999 * menu should not appear on the same monitor as the pointer. This
4000 * information can't be reliably inferred from the coordinates returned
4001 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
4002 * may extend beyond the monitor boundaries or even the screen boundaries.
4006 void gtk_menu_set_monitor (GtkMenu *menu,
4009 GtkMenuPrivate *priv;
4010 g_return_if_fail (GTK_IS_MENU (menu));
4012 priv = gtk_menu_get_private (menu);
4014 priv->monitor_num = monitor_num;