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_do_insert (GtkMenuShell *menu_shell,
178 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
181 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
183 static void gtk_menu_handle_scrolling (GtkMenu *menu,
185 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
187 static void gtk_menu_style_set (GtkWidget *widget,
188 GtkStyle *previous_style);
189 static gboolean gtk_menu_focus (GtkWidget *widget,
190 GtkDirectionType direction);
191 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
192 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
193 GtkMenuDirectionType direction);
194 static void gtk_menu_real_move_scroll (GtkMenu *menu,
197 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
198 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
199 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
202 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
203 GtkMenuItem *menu_item,
204 GdkEventCrossing *event);
206 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
207 static void gtk_menu_show_all (GtkWidget *widget);
208 static void gtk_menu_hide_all (GtkWidget *widget);
209 static void gtk_menu_position (GtkMenu *menu);
210 static void gtk_menu_reparent (GtkMenu *menu,
211 GtkWidget *new_parent,
213 static void gtk_menu_remove (GtkContainer *menu,
216 static void gtk_menu_update_title (GtkMenu *menu);
218 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
219 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
221 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
223 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
224 gboolean group_changed);
226 static GtkMenuShellClass *parent_class = NULL;
227 static const gchar *attach_data_key = "gtk-menu-attach-data";
229 static guint menu_signals[LAST_SIGNAL] = { 0 };
232 gtk_menu_free_private (gpointer data)
234 GtkMenuPrivate *priv = (GtkMenuPrivate *)data;
236 g_free (priv->heights);
242 gtk_menu_get_private (GtkMenu *menu)
244 GtkMenuPrivate *private;
245 static GQuark private_quark = 0;
248 private_quark = g_quark_from_static_string ("gtk-menu-private");
250 private = g_object_get_qdata (G_OBJECT (menu), private_quark);
254 private = g_new0 (GtkMenuPrivate, 1);
255 private->have_position = FALSE;
257 g_object_set_qdata_full (G_OBJECT (menu), private_quark,
258 private, gtk_menu_free_private);
265 gtk_menu_get_type (void)
267 static GType menu_type = 0;
271 static const GTypeInfo menu_info =
273 sizeof (GtkMenuClass),
274 NULL, /* base_init */
275 NULL, /* base_finalize */
276 (GClassInitFunc) gtk_menu_class_init,
277 NULL, /* class_finalize */
278 NULL, /* class_data */
281 (GInstanceInitFunc) gtk_menu_init,
284 menu_type = g_type_register_static (GTK_TYPE_MENU_SHELL, "GtkMenu",
292 gtk_menu_class_init (GtkMenuClass *class)
294 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
295 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
296 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
297 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
298 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
299 GtkBindingSet *binding_set;
301 parent_class = g_type_class_peek_parent (class);
303 gobject_class->finalize = gtk_menu_finalize;
304 gobject_class->set_property = gtk_menu_set_property;
305 gobject_class->get_property = gtk_menu_get_property;
307 object_class->destroy = gtk_menu_destroy;
309 widget_class->realize = gtk_menu_realize;
310 widget_class->unrealize = gtk_menu_unrealize;
311 widget_class->size_request = gtk_menu_size_request;
312 widget_class->size_allocate = gtk_menu_size_allocate;
313 widget_class->show = gtk_menu_show;
314 widget_class->expose_event = gtk_menu_expose;
315 widget_class->key_press_event = gtk_menu_key_press;
316 widget_class->button_press_event = gtk_menu_button_press;
317 widget_class->button_release_event = gtk_menu_button_release;
318 widget_class->motion_notify_event = gtk_menu_motion_notify;
319 widget_class->show_all = gtk_menu_show_all;
320 widget_class->hide_all = gtk_menu_hide_all;
321 widget_class->enter_notify_event = gtk_menu_enter_notify;
322 widget_class->leave_notify_event = gtk_menu_leave_notify;
323 widget_class->motion_notify_event = gtk_menu_motion_notify;
324 widget_class->style_set = gtk_menu_style_set;
325 widget_class->focus = gtk_menu_focus;
326 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
328 container_class->remove = gtk_menu_remove;
329 container_class->get_child_property = gtk_menu_get_child_property;
330 container_class->set_child_property = gtk_menu_set_child_property;
332 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
333 menu_shell_class->deactivate = gtk_menu_deactivate;
334 menu_shell_class->select_item = gtk_menu_select_item;
335 menu_shell_class->insert = gtk_menu_real_insert;
336 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
337 menu_shell_class->move_current = gtk_menu_move_current;
339 menu_signals[MOVE_SCROLL] =
340 _gtk_binding_signal_new ("move_scroll",
341 G_OBJECT_CLASS_TYPE (object_class),
342 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
343 G_CALLBACK (gtk_menu_real_move_scroll),
345 _gtk_marshal_VOID__ENUM,
347 GTK_TYPE_SCROLL_TYPE);
349 g_object_class_install_property (gobject_class,
351 g_param_spec_string ("tearoff-title",
353 P_("A title that may be displayed by the window manager when this menu is torn-off"),
355 G_PARAM_READABLE | G_PARAM_WRITABLE));
357 gtk_widget_class_install_style_property (widget_class,
358 g_param_spec_int ("vertical-padding",
359 P_("Vertical Padding"),
360 P_("Extra space at the top and bottom of the menu"),
366 gtk_widget_class_install_style_property (widget_class,
367 g_param_spec_int ("vertical-offset",
368 P_("Vertical Offset"),
369 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
375 gtk_widget_class_install_style_property (widget_class,
376 g_param_spec_int ("horizontal-offset",
377 P_("Horizontal Offset"),
378 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
385 gtk_container_class_install_child_property (container_class,
386 CHILD_PROP_LEFT_ATTACH,
387 g_param_spec_uint ("left_attach",
389 P_("The column number to attach the left side of the child to"),
393 gtk_container_class_install_child_property (container_class,
394 CHILD_PROP_RIGHT_ATTACH,
395 g_param_spec_uint ("right_attach",
397 P_("The column number to attach the right side of the child to"),
401 gtk_container_class_install_child_property (container_class,
402 CHILD_PROP_TOP_ATTACH,
403 g_param_spec_uint ("top_attach",
405 P_("The row number to attach the top of the child to"),
409 gtk_container_class_install_child_property (container_class,
410 CHILD_PROP_BOTTOM_ATTACH,
411 g_param_spec_uint ("bottom_attach",
413 P_("The row number to attach the bottom of the child to"),
417 binding_set = gtk_binding_set_by_class (class);
418 gtk_binding_entry_add_signal (binding_set,
421 GTK_TYPE_MENU_DIRECTION_TYPE,
423 gtk_binding_entry_add_signal (binding_set,
426 GTK_TYPE_MENU_DIRECTION_TYPE,
428 gtk_binding_entry_add_signal (binding_set,
431 GTK_TYPE_MENU_DIRECTION_TYPE,
433 gtk_binding_entry_add_signal (binding_set,
436 GTK_TYPE_MENU_DIRECTION_TYPE,
438 gtk_binding_entry_add_signal (binding_set,
441 GTK_TYPE_MENU_DIRECTION_TYPE,
442 GTK_MENU_DIR_PARENT);
443 gtk_binding_entry_add_signal (binding_set,
446 GTK_TYPE_MENU_DIRECTION_TYPE,
447 GTK_MENU_DIR_PARENT);
448 gtk_binding_entry_add_signal (binding_set,
451 GTK_TYPE_MENU_DIRECTION_TYPE,
453 gtk_binding_entry_add_signal (binding_set,
456 GTK_TYPE_MENU_DIRECTION_TYPE,
458 gtk_binding_entry_add_signal (binding_set,
461 GTK_TYPE_SCROLL_TYPE,
463 gtk_binding_entry_add_signal (binding_set,
466 GTK_TYPE_SCROLL_TYPE,
468 gtk_binding_entry_add_signal (binding_set,
471 GTK_TYPE_SCROLL_TYPE,
473 gtk_binding_entry_add_signal (binding_set,
476 GTK_TYPE_SCROLL_TYPE,
478 gtk_binding_entry_add_signal (binding_set,
481 GTK_TYPE_SCROLL_TYPE,
483 gtk_binding_entry_add_signal (binding_set,
486 GTK_TYPE_SCROLL_TYPE,
488 gtk_binding_entry_add_signal (binding_set,
491 GTK_TYPE_SCROLL_TYPE,
492 GTK_SCROLL_PAGE_DOWN);
493 gtk_binding_entry_add_signal (binding_set,
496 GTK_TYPE_SCROLL_TYPE,
497 GTK_SCROLL_PAGE_DOWN);
499 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
500 P_("Can change accelerators"),
501 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
505 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
506 P_("Delay before submenus appear"),
507 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
513 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
514 P_("Delay before hiding a submenu"),
515 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
518 DEFAULT_POPDOWN_DELAY,
525 gtk_menu_set_property (GObject *object,
532 menu = GTK_MENU (object);
536 case PROP_TEAROFF_TITLE:
537 gtk_menu_set_title (menu, g_value_get_string (value));
540 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
546 gtk_menu_get_property (GObject *object,
553 menu = GTK_MENU (object);
557 case PROP_TEAROFF_TITLE:
558 g_value_set_string (value, gtk_menu_get_title (menu));
561 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
567 get_attach_info (GObject *child)
569 AttachInfo *ai = g_object_get_data (child, ATTACH_INFO_KEY);
573 ai = g_new0 (AttachInfo, 1);
574 g_object_set_data_full (child, ATTACH_INFO_KEY, ai, g_free);
581 gtk_menu_set_child_property (GtkContainer *container,
587 GtkMenu *menu = GTK_MENU (container);
588 GtkMenuPrivate *priv;
589 AttachInfo *ai = get_attach_info (G_OBJECT (child));
591 priv = gtk_menu_get_private (menu);
595 case CHILD_PROP_LEFT_ATTACH:
596 ai->left_attach = g_value_get_uint (value);
598 case CHILD_PROP_RIGHT_ATTACH:
599 ai->right_attach = g_value_get_uint (value);
601 case CHILD_PROP_TOP_ATTACH:
602 ai->top_attach = g_value_get_uint (value);
604 case CHILD_PROP_BOTTOM_ATTACH:
605 ai->bottom_attach = g_value_get_uint (value);
609 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
613 priv->columns = MAX (priv->columns, ai->right_attach);
614 priv->rows = MAX (priv->rows, ai->bottom_attach);
616 gtk_widget_queue_resize (GTK_WIDGET (menu));
620 gtk_menu_get_child_property (GtkContainer *container,
626 AttachInfo *ai = get_attach_info (G_OBJECT (child));
630 case CHILD_PROP_LEFT_ATTACH:
631 g_value_set_uint (value, ai->left_attach);
633 case CHILD_PROP_RIGHT_ATTACH:
634 g_value_set_uint (value, ai->right_attach);
636 case CHILD_PROP_TOP_ATTACH:
637 g_value_set_uint (value, ai->top_attach);
639 case CHILD_PROP_BOTTOM_ATTACH:
640 g_value_set_uint (value, ai->bottom_attach);
644 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
650 get_child_attach (GtkWidget *child,
656 gtk_container_child_get (GTK_CONTAINER (child->parent), child,
665 gtk_menu_window_event (GtkWidget *window,
669 gboolean handled = FALSE;
671 g_object_ref (window);
677 case GDK_KEY_RELEASE:
678 handled = gtk_widget_event (menu, event);
684 g_object_unref (window);
685 g_object_unref (menu);
691 gtk_menu_window_size_request (GtkWidget *window,
692 GtkRequisition *requisition,
695 GtkMenuPrivate *private = gtk_menu_get_private (menu);
697 if (private->have_position)
699 GdkScreen *screen = gtk_widget_get_screen (window);
700 GdkRectangle monitor;
703 monitor_num = gdk_screen_get_monitor_at_point (screen,
704 private->x, private->y);
705 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
707 if (private->y + requisition->height > monitor.y + monitor.height)
708 requisition->height = monitor.y + monitor.height - private->y;
710 if (private->y < monitor.y)
711 requisition->height -= monitor.y - private->y;
716 gtk_menu_init (GtkMenu *menu)
718 menu->parent_menu_item = NULL;
719 menu->old_active_menu_item = NULL;
720 menu->accel_group = NULL;
721 menu->position_func = NULL;
722 menu->position_func_data = NULL;
723 menu->toggle_size = 0;
725 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
726 "type", GTK_WINDOW_POPUP,
729 "signal::event", gtk_menu_window_event, menu,
730 "signal::size_request", gtk_menu_window_size_request, menu,
731 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
733 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
734 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
736 /* Refloat the menu, so that reference counting for the menu isn't
737 * affected by it being a child of the toplevel
739 GTK_WIDGET_SET_FLAGS (menu, GTK_FLOATING);
740 menu->needs_destruction_ref_count = TRUE;
742 menu->view_window = NULL;
743 menu->bin_window = NULL;
745 menu->scroll_offset = 0;
746 menu->scroll_step = 0;
747 menu->timeout_id = 0;
748 menu->scroll_fast = FALSE;
750 menu->tearoff_window = NULL;
751 menu->tearoff_hbox = NULL;
752 menu->torn_off = FALSE;
753 menu->tearoff_active = FALSE;
754 menu->tearoff_adjustment = NULL;
755 menu->tearoff_scrollbar = NULL;
757 menu->upper_arrow_visible = FALSE;
758 menu->lower_arrow_visible = FALSE;
759 menu->upper_arrow_prelight = FALSE;
760 menu->lower_arrow_prelight = FALSE;
762 MENU_NEEDS_RESIZE (menu) = TRUE;
766 gtk_menu_destroy (GtkObject *object)
769 GtkMenuAttachData *data;
771 g_return_if_fail (GTK_IS_MENU (object));
773 menu = GTK_MENU (object);
775 gtk_menu_stop_scrolling (menu);
777 data = g_object_get_data (G_OBJECT (object), attach_data_key);
779 gtk_menu_detach (menu);
781 gtk_menu_stop_navigating_submenu (menu);
783 if (menu->old_active_menu_item)
785 g_object_unref (menu->old_active_menu_item);
786 menu->old_active_menu_item = NULL;
789 /* Add back the reference count for being a child */
790 if (menu->needs_destruction_ref_count)
792 menu->needs_destruction_ref_count = FALSE;
793 g_object_ref (object);
796 if (menu->accel_group)
798 g_object_unref (menu->accel_group);
799 menu->accel_group = NULL;
803 gtk_widget_destroy (menu->toplevel);
804 if (menu->tearoff_window)
805 gtk_widget_destroy (menu->tearoff_window);
807 GTK_OBJECT_CLASS (parent_class)->destroy (object);
811 gtk_menu_finalize (GObject *object)
813 GtkMenu *menu = GTK_MENU (object);
815 g_free (menu->accel_path);
817 G_OBJECT_CLASS (parent_class)->finalize (object);
821 menu_change_screen (GtkMenu *menu,
822 GdkScreen *new_screen)
826 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
827 gtk_menu_position (menu);
830 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
834 attach_widget_screen_changed (GtkWidget *attach_widget,
835 GdkScreen *previous_screen,
838 if (gtk_widget_has_screen (attach_widget) &&
839 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
841 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
846 gtk_menu_attach_to_widget (GtkMenu *menu,
847 GtkWidget *attach_widget,
848 GtkMenuDetachFunc detacher)
850 GtkMenuAttachData *data;
852 g_return_if_fail (GTK_IS_MENU (menu));
853 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
854 g_return_if_fail (detacher != NULL);
856 /* keep this function in sync with gtk_widget_set_parent()
859 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
862 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
863 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
868 gtk_object_sink (GTK_OBJECT (menu));
870 data = g_new (GtkMenuAttachData, 1);
871 data->attach_widget = attach_widget;
873 g_signal_connect (attach_widget, "screen_changed",
874 G_CALLBACK (attach_widget_screen_changed), menu);
875 attach_widget_screen_changed (attach_widget, NULL, menu);
877 data->detacher = detacher;
878 g_object_set_data (G_OBJECT (menu), attach_data_key, data);
880 if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
881 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
883 /* we don't need to set the style here, since
884 * we are a toplevel widget.
887 /* Fallback title for menu comes from attach widget */
888 gtk_menu_update_title (menu);
892 gtk_menu_get_attach_widget (GtkMenu *menu)
894 GtkMenuAttachData *data;
896 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
898 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
900 return data->attach_widget;
905 gtk_menu_detach (GtkMenu *menu)
907 GtkMenuAttachData *data;
909 g_return_if_fail (GTK_IS_MENU (menu));
911 /* keep this function in sync with gtk_widget_unparent()
913 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
916 g_warning ("gtk_menu_detach(): menu is not attached");
919 g_object_set_data (G_OBJECT (menu), attach_data_key, NULL);
921 g_signal_handlers_disconnect_by_func (data->attach_widget,
922 (gpointer) attach_widget_screen_changed,
925 data->detacher (data->attach_widget, menu);
927 if (GTK_WIDGET_REALIZED (menu))
928 gtk_widget_unrealize (GTK_WIDGET (menu));
932 /* Fallback title for menu comes from attach widget */
933 gtk_menu_update_title (menu);
935 g_object_unref (menu);
939 gtk_menu_remove (GtkContainer *container,
944 g_return_if_fail (GTK_IS_MENU (container));
945 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
947 menu = GTK_MENU (container);
949 /* Clear out old_active_menu_item if it matches the item we are removing
951 if (menu->old_active_menu_item == widget)
953 g_object_unref (menu->old_active_menu_item);
954 menu->old_active_menu_item = NULL;
957 g_object_set_data (G_OBJECT (widget), ATTACH_INFO_KEY, NULL);
959 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
966 return g_object_new (GTK_TYPE_MENU, NULL);
970 gtk_menu_do_insert (GtkMenuShell *menu_shell,
976 GtkMenuPrivate *priv;
978 priv = gtk_menu_get_private (GTK_MENU (menu_shell));
982 /* attach after the last row */
983 i = g_list_length (menu_shell->children) - 1;
984 gtk_menu_attach (GTK_MENU (menu_shell), child,
985 0, priv->columns ? priv->columns : 1,
991 /* we need to make space for this new item; move all items with
992 * top >= position one down
994 for (children = menu_shell->children; children; children = children->next)
998 gtk_container_child_get (GTK_CONTAINER (menu_shell), children->data,
1000 "bottom_attach", &bottom,
1003 if (top >= position)
1004 gtk_container_child_set (GTK_CONTAINER (menu_shell), children->data,
1005 "top_attach", top + 1,
1006 "bottom_attach", bottom + 1,
1010 /* attach the new item */
1011 gtk_menu_attach (GTK_MENU (menu_shell), child,
1012 0, priv->columns ? priv->columns : 1,
1013 position, position + 1);
1017 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1021 if (GTK_WIDGET_REALIZED (menu_shell))
1022 gtk_widget_set_parent_window (child, GTK_MENU (menu_shell)->bin_window);
1024 GTK_MENU_SHELL_CLASS (parent_class)->insert (menu_shell, child, position);
1026 gtk_menu_do_insert (menu_shell, child, position);
1030 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1035 widget = GTK_WIDGET (menu);
1041 GdkGCValues gc_values;
1043 menu->tearoff_active = FALSE;
1044 menu->saved_scroll_offset = menu->scroll_offset;
1046 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
1047 gc = gdk_gc_new_with_values (widget->window,
1048 &gc_values, GDK_GC_SUBWINDOW);
1050 gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1052 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
1057 gdk_draw_drawable (pixmap, gc,
1058 menu->tearoff_window->window,
1059 0, 0, 0, 0, -1, -1);
1060 g_object_unref (gc);
1062 gtk_widget_set_size_request (menu->tearoff_window,
1066 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
1067 g_object_unref (pixmap);
1072 popup_grab_on_window (GdkWindow *window,
1073 guint32 activate_time)
1075 if ((gdk_pointer_grab (window, TRUE,
1076 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1077 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1078 GDK_POINTER_MOTION_MASK,
1079 NULL, NULL, activate_time) == 0))
1081 if (gdk_keyboard_grab (window, TRUE,
1082 activate_time) == 0)
1086 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1097 * @menu: a #GtkMenu.
1098 * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
1099 * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
1100 * @func: a user supplied function used to position the menu, or %NULL
1101 * @data: user supplied data to be passed to @func.
1102 * @button: the mouse button which was pressed to initiate the event.
1103 * @activate_time: the time at which the activation event occurred.
1105 * Displays a menu and makes it available for selection. Applications can use
1106 * this function to display context-sensitive menus, and will typically supply
1107 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1108 * parameters. The default menu positioning function will position the menu
1109 * at the current mouse cursor position.
1111 * The @button parameter should be the mouse button pressed to initiate
1112 * the menu popup. If the menu popup was initiated by something other than
1113 * a mouse button press, such as a mouse button release or a keypress,
1114 * @button should be 0.
1116 * The @activate_time parameter should be the time stamp of the event that
1117 * initiated the popup. If such an event is not available, use
1118 * gtk_get_current_event_time() instead.
1122 gtk_menu_popup (GtkMenu *menu,
1123 GtkWidget *parent_menu_shell,
1124 GtkWidget *parent_menu_item,
1125 GtkMenuPositionFunc func,
1128 guint32 activate_time)
1131 GtkWidget *xgrab_shell;
1133 GdkEvent *current_event;
1134 GtkMenuShell *menu_shell;
1136 g_return_if_fail (GTK_IS_MENU (menu));
1138 widget = GTK_WIDGET (menu);
1139 menu_shell = GTK_MENU_SHELL (menu);
1141 menu_shell->parent_menu_shell = parent_menu_shell;
1143 /* Find the last viewable ancestor, and make an X grab on it
1145 parent = GTK_WIDGET (menu);
1149 gboolean viewable = TRUE;
1150 GtkWidget *tmp = parent;
1154 if (!GTK_WIDGET_MAPPED (tmp))
1163 xgrab_shell = parent;
1165 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1168 /* We want to receive events generated when we map the menu; unfortunately,
1169 * since there is probably already an implicit grab in place from the
1170 * button that the user used to pop up the menu, we won't receive then --
1171 * in particular, the EnterNotify when the menu pops up under the pointer.
1173 * If we are grabbing on a parent menu shell, no problem; just grab on
1174 * that menu shell first before popping up the window with owner_events = TRUE.
1176 * When grabbing on the menu itself, things get more convuluted - we
1177 * we do an explicit grab on a specially created window with
1178 * owner_events = TRUE, which we override further down with a grab
1179 * on the menu. (We can't grab on the menu until it is mapped; we
1180 * probably could just leave the grab on the other window, with a
1181 * little reorganization of the code in gtkmenu*).
1183 if (xgrab_shell && xgrab_shell != widget)
1185 if (popup_grab_on_window (xgrab_shell->window, activate_time))
1186 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1190 GdkWindow *transfer_window;
1192 xgrab_shell = widget;
1193 transfer_window = menu_grab_transfer_window_get (menu);
1194 if (popup_grab_on_window (transfer_window, activate_time))
1195 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1198 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1200 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1201 * with a stuck up window, we just abort here. Presumably the user will
1204 menu_shell->parent_menu_shell = NULL;
1205 menu_grab_transfer_window_destroy (menu);
1209 menu_shell->active = TRUE;
1210 menu_shell->button = button;
1212 /* If we are popping up the menu from something other than, a button
1213 * press then, as a heuristic, we ignore enter events for the menu
1214 * until we get a MOTION_NOTIFY.
1217 current_event = gtk_get_current_event ();
1220 if ((current_event->type != GDK_BUTTON_PRESS) &&
1221 (current_event->type != GDK_ENTER_NOTIFY))
1222 menu_shell->ignore_enter = TRUE;
1224 gdk_event_free (current_event);
1227 menu_shell->ignore_enter = TRUE;
1231 gtk_menu_tearoff_bg_copy (menu);
1233 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1236 menu->parent_menu_item = parent_menu_item;
1237 menu->position_func = func;
1238 menu->position_func_data = data;
1239 menu_shell->activate_time = activate_time;
1241 /* We need to show the menu here rather in the init function because
1242 * code expects to be able to tell if the menu is onscreen by
1243 * looking at the GTK_WIDGET_VISIBLE (menu)
1245 gtk_widget_show (GTK_WIDGET (menu));
1247 /* Position the menu, possibly changing the size request
1249 gtk_menu_position (menu);
1251 /* Compute the size of the toplevel and realize it so we
1252 * can scroll correctly.
1255 GtkRequisition tmp_request;
1256 GtkAllocation tmp_allocation = { 0, };
1258 gtk_widget_size_request (menu->toplevel, &tmp_request);
1260 tmp_allocation.width = tmp_request.width;
1261 tmp_allocation.height = tmp_request.height;
1263 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1265 gtk_widget_realize (GTK_WIDGET (menu));
1268 gtk_menu_scroll_to (menu, menu->scroll_offset);
1270 /* Once everything is set up correctly, map the toplevel window on
1273 gtk_widget_show (menu->toplevel);
1275 if (xgrab_shell == widget)
1276 popup_grab_on_window (widget->window, activate_time); /* Should always succeed */
1277 gtk_grab_add (GTK_WIDGET (menu));
1281 gtk_menu_popdown (GtkMenu *menu)
1283 GtkMenuPrivate *private;
1284 GtkMenuShell *menu_shell;
1286 g_return_if_fail (GTK_IS_MENU (menu));
1288 menu_shell = GTK_MENU_SHELL (menu);
1289 private = gtk_menu_get_private (menu);
1291 menu_shell->parent_menu_shell = NULL;
1292 menu_shell->active = FALSE;
1293 menu_shell->ignore_enter = FALSE;
1295 private->have_position = FALSE;
1297 gtk_menu_stop_scrolling (menu);
1299 gtk_menu_stop_navigating_submenu (menu);
1301 if (menu_shell->active_menu_item)
1303 if (menu->old_active_menu_item)
1304 g_object_unref (menu->old_active_menu_item);
1305 menu->old_active_menu_item = menu_shell->active_menu_item;
1306 g_object_ref (menu->old_active_menu_item);
1309 gtk_menu_shell_deselect (menu_shell);
1311 /* The X Grab, if present, will automatically be removed when we hide
1313 gtk_widget_hide (menu->toplevel);
1317 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1319 if (GTK_BIN (menu->toplevel)->child)
1321 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1325 /* We popped up the menu from the tearoff, so we need to
1326 * release the grab - we aren't actually hiding the menu.
1328 if (menu_shell->have_xgrab)
1330 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
1332 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1333 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1337 /* gtk_menu_popdown is called each time a menu item is selected from
1338 * a torn off menu. Only scroll back to the saved position if the
1339 * non-tearoff menu was popped down.
1341 if (!menu->tearoff_active)
1342 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1343 menu->tearoff_active = TRUE;
1346 gtk_widget_hide (GTK_WIDGET (menu));
1348 menu_shell->have_xgrab = FALSE;
1349 gtk_grab_remove (GTK_WIDGET (menu));
1351 menu_grab_transfer_window_destroy (menu);
1355 gtk_menu_get_active (GtkMenu *menu)
1360 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1362 if (!menu->old_active_menu_item)
1365 children = GTK_MENU_SHELL (menu)->children;
1369 child = children->data;
1370 children = children->next;
1372 if (GTK_BIN (child)->child)
1377 menu->old_active_menu_item = child;
1378 if (menu->old_active_menu_item)
1379 g_object_ref (menu->old_active_menu_item);
1382 return menu->old_active_menu_item;
1386 gtk_menu_set_active (GtkMenu *menu,
1392 g_return_if_fail (GTK_IS_MENU (menu));
1394 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1397 child = tmp_list->data;
1398 if (GTK_BIN (child)->child)
1400 if (menu->old_active_menu_item)
1401 g_object_unref (menu->old_active_menu_item);
1402 menu->old_active_menu_item = child;
1403 g_object_ref (menu->old_active_menu_item);
1409 gtk_menu_set_accel_group (GtkMenu *menu,
1410 GtkAccelGroup *accel_group)
1412 g_return_if_fail (GTK_IS_MENU (menu));
1414 if (menu->accel_group != accel_group)
1416 if (menu->accel_group)
1417 g_object_unref (menu->accel_group);
1418 menu->accel_group = accel_group;
1419 if (menu->accel_group)
1420 g_object_ref (menu->accel_group);
1421 _gtk_menu_refresh_accel_paths (menu, TRUE);
1426 gtk_menu_get_accel_group (GtkMenu *menu)
1428 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1430 return menu->accel_group;
1434 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1437 /* Menu items chain here to figure whether they can activate their
1438 * accelerators. Unlike ordinary widgets, menus allow accel
1439 * activation even if invisible since that's the usual case for
1440 * submenus/popup-menus. however, the state of the attach widget
1441 * affects the "activeness" of the menu.
1443 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1446 return gtk_widget_can_activate_accel (awidget, signal_id);
1448 return GTK_WIDGET_IS_SENSITIVE (widget);
1452 * gtk_menu_set_accel_path
1453 * @menu: a valid #GtkMenu
1454 * @accel_path: a valid accelerator path
1456 * Sets an accelerator path for this menu from which accelerator paths
1457 * for its immediate children, its menu items, can be constructed.
1458 * The main purpose of this function is to spare the programmer the
1459 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1460 * each menu item that should support runtime user changable accelerators.
1461 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1462 * each menu item of this menu, that contains a label describing its purpose,
1463 * automatically gets an accel path assigned. For example, a menu containing
1464 * menu items "New" and "Exit", will, after
1465 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1466 * has been called, assign its items the accel paths:
1467 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1468 * Assigning accel paths to menu items then enables the user to change
1469 * their accelerators at runtime. More details about accelerator paths
1470 * and their default setups can be found at gtk_accel_map_add_entry().
1473 gtk_menu_set_accel_path (GtkMenu *menu,
1474 const gchar *accel_path)
1476 g_return_if_fail (GTK_IS_MENU (menu));
1478 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1480 g_free (menu->accel_path);
1481 menu->accel_path = g_strdup (accel_path);
1482 if (menu->accel_path)
1483 _gtk_menu_refresh_accel_paths (menu, FALSE);
1488 gboolean group_changed;
1492 refresh_accel_paths_foreach (GtkWidget *widget,
1495 AccelPropagation *prop = data;
1497 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1498 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1499 prop->menu->accel_path,
1500 prop->menu->accel_group,
1501 prop->group_changed);
1505 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1506 gboolean group_changed)
1508 g_return_if_fail (GTK_IS_MENU (menu));
1510 if (menu->accel_path && menu->accel_group)
1512 AccelPropagation prop;
1515 prop.group_changed = group_changed;
1516 gtk_container_foreach (GTK_CONTAINER (menu),
1517 refresh_accel_paths_foreach,
1523 gtk_menu_reposition (GtkMenu *menu)
1525 g_return_if_fail (GTK_IS_MENU (menu));
1527 if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1528 gtk_menu_position (menu);
1532 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1535 g_return_if_fail (GTK_IS_MENU (menu));
1537 if (adjustment->value != menu->scroll_offset)
1538 gtk_menu_scroll_to (menu, adjustment->value);
1542 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1545 GdkGeometry geometry_hints;
1547 if (!menu->tearoff_window)
1550 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1552 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1553 width += menu->tearoff_scrollbar->requisition.width;
1556 geometry_hints.min_width = width;
1557 geometry_hints.max_width = width;
1559 geometry_hints.min_height = 0;
1560 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1562 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1565 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1569 gtk_menu_update_title (GtkMenu *menu)
1571 if (menu->tearoff_window)
1574 GtkWidget *attach_widget;
1576 title = gtk_menu_get_title (menu);
1579 attach_widget = gtk_menu_get_attach_widget (menu);
1580 if (GTK_IS_MENU_ITEM (attach_widget))
1582 GtkWidget *child = GTK_BIN (attach_widget)->child;
1583 if (GTK_IS_LABEL (child))
1584 title = gtk_label_get_text (GTK_LABEL (child));
1589 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1594 gtk_menu_get_toplevel (GtkWidget *menu)
1596 GtkWidget *attach, *toplevel;
1598 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
1600 if (GTK_IS_MENU_ITEM (attach))
1601 attach = attach->parent;
1603 if (GTK_IS_MENU (attach))
1604 return gtk_menu_get_toplevel (attach->parent);
1605 else if (GTK_IS_WIDGET (attach))
1607 toplevel = gtk_widget_get_toplevel (attach->parent);
1608 if (GTK_WIDGET_TOPLEVEL (toplevel))
1616 gtk_menu_set_tearoff_state (GtkMenu *menu,
1621 g_return_if_fail (GTK_IS_MENU (menu));
1623 if (menu->torn_off != torn_off)
1625 menu->torn_off = torn_off;
1626 menu->tearoff_active = torn_off;
1630 if (GTK_WIDGET_VISIBLE (menu))
1631 gtk_menu_popdown (menu);
1633 if (!menu->tearoff_window)
1635 GtkWidget *toplevel;
1637 menu->tearoff_window = gtk_widget_new (GTK_TYPE_WINDOW,
1638 "type", GTK_WINDOW_TOPLEVEL,
1639 "screen", gtk_widget_get_screen (menu->toplevel),
1640 "app_paintable", TRUE,
1644 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1645 GDK_WINDOW_TYPE_HINT_MENU);
1646 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1647 g_signal_connect (menu->tearoff_window, "destroy",
1648 G_CALLBACK (gtk_widget_destroyed), &menu->tearoff_window);
1649 g_signal_connect (menu->tearoff_window, "event",
1650 G_CALLBACK (gtk_menu_window_event), menu);
1652 gtk_menu_update_title (menu);
1654 gtk_widget_realize (menu->tearoff_window);
1656 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
1657 if (toplevel != NULL)
1658 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
1659 GTK_WINDOW (toplevel));
1661 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1662 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1664 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
1665 menu->tearoff_adjustment =
1666 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1668 GTK_WIDGET (menu)->requisition.height,
1672 g_object_connect (menu->tearoff_adjustment,
1673 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1675 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1677 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1678 menu->tearoff_scrollbar,
1681 if (menu->tearoff_adjustment->upper > height)
1682 gtk_widget_show (menu->tearoff_scrollbar);
1684 gtk_widget_show (menu->tearoff_hbox);
1687 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
1689 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
1691 /* Update menu->requisition
1693 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
1695 gtk_menu_set_tearoff_hints (menu, width);
1697 gtk_widget_realize (menu->tearoff_window);
1698 gtk_menu_position (menu);
1700 gtk_widget_show (GTK_WIDGET (menu));
1701 gtk_widget_show (menu->tearoff_window);
1703 gtk_menu_scroll_to (menu, 0);
1708 gtk_widget_hide (GTK_WIDGET (menu));
1709 gtk_widget_hide (menu->tearoff_window);
1710 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1711 gtk_widget_destroy (menu->tearoff_window);
1713 menu->tearoff_window = NULL;
1714 menu->tearoff_hbox = NULL;
1715 menu->tearoff_scrollbar = NULL;
1716 menu->tearoff_adjustment = NULL;
1722 * gtk_menu_get_tearoff_state:
1725 * Returns whether the menu is torn off. See
1726 * gtk_menu_set_tearoff_state ().
1728 * Return value: %TRUE if the menu is currently torn off.
1731 gtk_menu_get_tearoff_state (GtkMenu *menu)
1733 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
1735 return menu->torn_off;
1739 * gtk_menu_set_title:
1741 * @title: a string containing the title for the menu.
1743 * Sets the title string for the menu. The title is displayed when the menu
1744 * is shown as a tearoff menu.
1747 gtk_menu_set_title (GtkMenu *menu,
1750 g_return_if_fail (GTK_IS_MENU (menu));
1753 g_object_set_data_full (G_OBJECT (menu), "gtk-menu-title",
1754 g_strdup (title), (GtkDestroyNotify) g_free);
1756 g_object_set_data (G_OBJECT (menu), "gtk-menu-title", NULL);
1758 gtk_menu_update_title (menu);
1759 g_object_notify (G_OBJECT (menu), "tearoff_title");
1763 * gtk_menu_get_title:
1766 * Returns the title of the menu. See gtk_menu_set_title().
1768 * Return value: the title of the menu, or %NULL if the menu has no
1769 * title set on it. This string is owned by the widget and should
1770 * not be modified or freed.
1772 G_CONST_RETURN gchar *
1773 gtk_menu_get_title (GtkMenu *menu)
1775 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1777 return g_object_get_data (G_OBJECT (menu), "gtk-menu-title");
1781 gtk_menu_reorder_child (GtkMenu *menu,
1785 GtkMenuShell *menu_shell;
1787 g_return_if_fail (GTK_IS_MENU (menu));
1788 g_return_if_fail (GTK_IS_MENU_ITEM (child));
1790 menu_shell = GTK_MENU_SHELL (menu);
1792 if (g_list_find (menu_shell->children, child))
1794 menu_shell->children = g_list_remove (menu_shell->children, child);
1795 menu_shell->children = g_list_insert (menu_shell->children, child, position);
1796 gtk_menu_do_insert (menu_shell, child, position);
1798 if (GTK_WIDGET_VISIBLE (menu_shell))
1799 gtk_widget_queue_resize (GTK_WIDGET (menu_shell));
1804 gtk_menu_style_set (GtkWidget *widget,
1805 GtkStyle *previous_style)
1807 if (GTK_WIDGET_REALIZED (widget))
1809 GtkMenu *menu = GTK_MENU (widget);
1811 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1812 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1813 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1818 gtk_menu_realize (GtkWidget *widget)
1820 GdkWindowAttr attributes;
1821 gint attributes_mask;
1826 guint vertical_padding;
1828 g_return_if_fail (GTK_IS_MENU (widget));
1830 menu = GTK_MENU (widget);
1832 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1834 attributes.window_type = GDK_WINDOW_CHILD;
1835 attributes.x = widget->allocation.x;
1836 attributes.y = widget->allocation.y;
1837 attributes.width = widget->allocation.width;
1838 attributes.height = widget->allocation.height;
1839 attributes.wclass = GDK_INPUT_OUTPUT;
1840 attributes.visual = gtk_widget_get_visual (widget);
1841 attributes.colormap = gtk_widget_get_colormap (widget);
1843 attributes.event_mask = gtk_widget_get_events (widget);
1845 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
1846 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
1848 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1849 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1850 gdk_window_set_user_data (widget->window, widget);
1852 border_width = GTK_CONTAINER (widget)->border_width;
1854 gtk_widget_style_get (GTK_WIDGET (menu),
1855 "vertical-padding", &vertical_padding,
1858 attributes.x = border_width + widget->style->xthickness;
1859 attributes.y = border_width + widget->style->ythickness + vertical_padding;
1860 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
1861 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
1863 if (menu->upper_arrow_visible)
1865 attributes.y += MENU_SCROLL_ARROW_HEIGHT;
1866 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1868 if (menu->lower_arrow_visible)
1869 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1871 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
1872 gdk_window_set_user_data (menu->view_window, menu);
1876 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
1878 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
1879 gdk_window_set_user_data (menu->bin_window, menu);
1881 children = GTK_MENU_SHELL (menu)->children;
1884 child = children->data;
1885 children = children->next;
1887 gtk_widget_set_parent_window (child, menu->bin_window);
1890 widget->style = gtk_style_attach (widget->style, widget->window);
1891 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1892 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1893 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1895 if (GTK_MENU_SHELL (widget)->active_menu_item)
1896 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
1897 GTK_MENU_SHELL (widget)->active_menu_item);
1899 gdk_window_show (menu->bin_window);
1900 gdk_window_show (menu->view_window);
1904 gtk_menu_focus (GtkWidget *widget,
1905 GtkDirectionType direction)
1908 * A menu or its menu items cannot have focus
1913 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
1916 menu_grab_transfer_window_get (GtkMenu *menu)
1918 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1921 GdkWindowAttr attributes;
1922 gint attributes_mask;
1924 attributes.x = -100;
1925 attributes.y = -100;
1926 attributes.width = 10;
1927 attributes.height = 10;
1928 attributes.window_type = GDK_WINDOW_TEMP;
1929 attributes.wclass = GDK_INPUT_ONLY;
1930 attributes.override_redirect = TRUE;
1931 attributes.event_mask = 0;
1933 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
1935 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
1936 &attributes, attributes_mask);
1937 gdk_window_set_user_data (window, menu);
1939 gdk_window_show (window);
1941 g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", window);
1948 menu_grab_transfer_window_destroy (GtkMenu *menu)
1950 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1953 gdk_window_set_user_data (window, NULL);
1954 gdk_window_destroy (window);
1955 g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", NULL);
1960 gtk_menu_unrealize (GtkWidget *widget)
1964 g_return_if_fail (GTK_IS_MENU (widget));
1966 menu = GTK_MENU (widget);
1968 menu_grab_transfer_window_destroy (menu);
1970 gdk_window_set_user_data (menu->view_window, NULL);
1971 gdk_window_destroy (menu->view_window);
1972 menu->view_window = NULL;
1974 gdk_window_set_user_data (menu->bin_window, NULL);
1975 gdk_window_destroy (menu->bin_window);
1976 menu->bin_window = NULL;
1978 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1982 gtk_menu_size_request (GtkWidget *widget,
1983 GtkRequisition *requisition)
1987 GtkMenuShell *menu_shell;
1990 guint max_toggle_size;
1991 guint max_accel_width;
1992 guint vertical_padding;
1993 GtkRequisition child_requisition;
1994 GtkMenuPrivate *priv;
1996 g_return_if_fail (GTK_IS_MENU (widget));
1997 g_return_if_fail (requisition != NULL);
1999 menu = GTK_MENU (widget);
2000 menu_shell = GTK_MENU_SHELL (widget);
2001 priv = gtk_menu_get_private (menu);
2003 requisition->width = 0;
2004 requisition->height = 0;
2006 max_toggle_size = 0;
2007 max_accel_width = 0;
2009 g_free (priv->heights);
2010 priv->heights = g_new0 (guint, priv->rows);
2011 priv->heights_length = priv->rows;
2013 children = menu_shell->children;
2020 child = children->data;
2021 children = children->next;
2023 if (! GTK_WIDGET_VISIBLE (child))
2026 get_child_attach (child, &l, &r, &t, &b);
2028 /* It's important to size_request the child
2029 * before doing the toggle size request, in
2030 * case the toggle size request depends on the size
2031 * request of a child of the child (e.g. for ImageMenuItem)
2034 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
2035 gtk_widget_size_request (child, &child_requisition);
2037 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2038 max_toggle_size = MAX (max_toggle_size, toggle_size);
2039 max_accel_width = MAX (max_accel_width,
2040 GTK_MENU_ITEM (child)->accelerator_width);
2042 part = child_requisition.width / (r - l);
2043 requisition->width = MAX (requisition->width, part);
2045 part = child_requisition.height / (b - t);
2046 priv->heights[t] = MAX (priv->heights[t], part);
2049 for (i = 0; i < priv->rows; i++)
2050 requisition->height += priv->heights[i];
2052 requisition->width += max_toggle_size + max_accel_width;
2053 requisition->width *= priv->columns;
2054 requisition->width += (GTK_CONTAINER (menu)->border_width +
2055 widget->style->xthickness) * 2;
2057 gtk_widget_style_get (GTK_WIDGET (menu),
2058 "vertical-padding", &vertical_padding,
2060 requisition->height += (GTK_CONTAINER (menu)->border_width + vertical_padding +
2061 widget->style->ythickness) * 2;
2063 menu->toggle_size = max_toggle_size;
2065 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2067 if (menu->tearoff_active)
2068 gtk_menu_set_tearoff_hints (menu, requisition->width);
2072 gtk_menu_size_allocate (GtkWidget *widget,
2073 GtkAllocation *allocation)
2076 GtkMenuShell *menu_shell;
2078 GtkAllocation child_allocation;
2079 GtkRequisition child_requisition;
2080 GtkMenuPrivate *priv;
2084 guint vertical_padding;
2086 g_return_if_fail (GTK_IS_MENU (widget));
2087 g_return_if_fail (allocation != NULL);
2089 menu = GTK_MENU (widget);
2090 menu_shell = GTK_MENU_SHELL (widget);
2091 priv = gtk_menu_get_private (menu);
2093 widget->allocation = *allocation;
2094 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2096 gtk_widget_style_get (GTK_WIDGET (menu),
2097 "vertical-padding", &vertical_padding,
2100 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness;
2101 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness + vertical_padding;
2103 width = MAX (1, allocation->width - x * 2);
2104 height = MAX (1, allocation->height - y * 2);
2106 child_requisition.width -= x * 2;
2107 child_requisition.height -= y * 2;
2109 if (menu_shell->active)
2110 gtk_menu_scroll_to (menu, menu->scroll_offset);
2112 if (menu->upper_arrow_visible && !menu->tearoff_active)
2114 y += MENU_SCROLL_ARROW_HEIGHT;
2115 height -= MENU_SCROLL_ARROW_HEIGHT;
2118 if (menu->lower_arrow_visible && !menu->tearoff_active)
2119 height -= MENU_SCROLL_ARROW_HEIGHT;
2121 if (GTK_WIDGET_REALIZED (widget))
2123 gdk_window_move_resize (widget->window,
2124 allocation->x, allocation->y,
2125 allocation->width, allocation->height);
2127 gdk_window_move_resize (menu->view_window,
2134 if (menu_shell->children)
2136 gint base_width = width / priv->columns;
2138 children = menu_shell->children;
2141 child = children->data;
2142 children = children->next;
2144 if (GTK_WIDGET_VISIBLE (child))
2149 get_child_attach (child, &l, &r, &t, &b);
2151 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2154 tmp = priv->columns - l;
2155 l = priv->columns - r;
2159 child_allocation.width = (r - l) * base_width;
2160 child_allocation.height = 0;
2161 child_allocation.x = l * base_width;
2162 child_allocation.y = 0;
2164 for (i = 0; i < b; i++)
2167 child_allocation.y += priv->heights[i];
2169 child_allocation.height += priv->heights[i];
2172 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2175 gtk_widget_size_allocate (child, &child_allocation);
2176 gtk_widget_queue_draw (child);
2180 /* Resize the item window */
2181 if (GTK_WIDGET_REALIZED (widget))
2187 for (i = 0; i < priv->rows; i++)
2188 height += priv->heights[i];
2190 width = priv->columns * base_width;
2191 gdk_window_resize (menu->bin_window, width, height);
2194 if (menu->tearoff_active)
2196 if (allocation->height >= widget->requisition.height)
2198 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2200 gtk_widget_hide (menu->tearoff_scrollbar);
2201 gtk_menu_set_tearoff_hints (menu, allocation->width);
2203 gtk_menu_scroll_to (menu, 0);
2208 menu->tearoff_adjustment->upper = widget->requisition.height;
2209 menu->tearoff_adjustment->page_size = allocation->height;
2211 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2212 menu->tearoff_adjustment->upper)
2215 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2218 gtk_menu_scroll_to (menu, value);
2221 gtk_adjustment_changed (menu->tearoff_adjustment);
2223 if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2225 gtk_widget_show (menu->tearoff_scrollbar);
2226 gtk_menu_set_tearoff_hints (menu, allocation->width);
2234 gtk_menu_paint (GtkWidget *widget,
2235 GdkEventExpose *event)
2239 gint border_x, border_y;
2240 guint vertical_padding;
2242 g_return_if_fail (GTK_IS_MENU (widget));
2244 menu = GTK_MENU (widget);
2246 gtk_widget_style_get (GTK_WIDGET (menu),
2247 "vertical-padding", &vertical_padding,
2250 border_x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness;
2251 border_y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
2252 gdk_drawable_get_size (widget->window, &width, &height);
2254 if (event->window == widget->window)
2256 gint arrow_space = MENU_SCROLL_ARROW_HEIGHT - 2 * widget->style->ythickness;
2257 gint arrow_size = 0.7 * arrow_space;
2259 gtk_paint_box (widget->style,
2263 NULL, widget, "menu",
2265 if (menu->upper_arrow_visible && !menu->tearoff_active)
2267 gtk_paint_box (widget->style,
2269 menu->upper_arrow_prelight ?
2270 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2272 NULL, widget, "menu",
2275 width - 2 * border_x,
2276 MENU_SCROLL_ARROW_HEIGHT);
2278 gtk_paint_arrow (widget->style,
2280 menu->upper_arrow_prelight ?
2281 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2283 NULL, widget, "menu_scroll_arrow_up",
2286 (width - arrow_size ) / 2,
2287 border_y + widget->style->ythickness + (arrow_space - arrow_size)/2,
2288 arrow_size, arrow_size);
2291 if (menu->lower_arrow_visible && !menu->tearoff_active)
2293 gtk_paint_box (widget->style,
2295 menu->lower_arrow_prelight ?
2296 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2298 NULL, widget, "menu",
2300 height - border_y - MENU_SCROLL_ARROW_HEIGHT,
2302 MENU_SCROLL_ARROW_HEIGHT);
2304 gtk_paint_arrow (widget->style,
2306 menu->lower_arrow_prelight ?
2307 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2309 NULL, widget, "menu_scroll_arrow_down",
2312 (width - arrow_size) / 2,
2313 height - border_y - MENU_SCROLL_ARROW_HEIGHT +
2314 widget->style->ythickness + (arrow_space - arrow_size)/2,
2315 arrow_size, arrow_size);
2321 gtk_menu_expose (GtkWidget *widget,
2322 GdkEventExpose *event)
2324 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2325 g_return_val_if_fail (event != NULL, FALSE);
2327 if (GTK_WIDGET_DRAWABLE (widget))
2329 gtk_menu_paint (widget, event);
2331 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
2338 gtk_menu_show (GtkWidget *widget)
2340 GtkMenu *menu = GTK_MENU (widget);
2342 _gtk_menu_refresh_accel_paths (menu, FALSE);
2344 GTK_WIDGET_CLASS (parent_class)->show (widget);
2348 gtk_menu_button_press (GtkWidget *widget,
2349 GdkEventButton *event)
2351 /* Don't pop down the menu for releases over scroll arrows
2353 if (GTK_IS_MENU (widget))
2355 GtkMenu *menu = GTK_MENU (widget);
2357 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2361 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
2365 gtk_menu_button_release (GtkWidget *widget,
2366 GdkEventButton *event)
2368 /* Don't pop down the menu for releases over scroll arrows
2370 if (GTK_IS_MENU (widget))
2372 GtkMenu *menu = GTK_MENU (widget);
2374 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2378 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
2381 static const gchar *
2382 get_accel_path (GtkWidget *menu_item,
2387 GClosure *accel_closure;
2388 GtkAccelGroup *accel_group;
2390 path = _gtk_widget_get_accel_path (menu_item, locked);
2393 path = GTK_MENU_ITEM (menu_item)->accel_path;
2399 label = GTK_BIN (menu_item)->child;
2401 if (GTK_IS_ACCEL_LABEL (label))
2403 g_object_get (label,
2404 "accel_closure", &accel_closure,
2406 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
2408 *locked = accel_group->lock_count > 0;
2417 gtk_menu_key_press (GtkWidget *widget,
2420 GtkMenuShell *menu_shell;
2422 gboolean delete = FALSE;
2423 gboolean can_change_accels;
2424 gchar *accel = NULL;
2425 guint accel_key, accel_mods;
2426 GdkModifierType consumed_modifiers;
2427 GdkDisplay *display;
2429 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2430 g_return_val_if_fail (event != NULL, FALSE);
2432 menu_shell = GTK_MENU_SHELL (widget);
2433 menu = GTK_MENU (widget);
2435 gtk_menu_stop_navigating_submenu (menu);
2437 if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
2440 display = gtk_widget_get_display (widget);
2442 g_object_get (gtk_widget_get_settings (widget),
2443 "gtk-menu-bar-accel", &accel,
2444 "gtk-can-change-accels", &can_change_accels,
2450 GdkModifierType mods = 0;
2451 gboolean handled = FALSE;
2453 gtk_accelerator_parse (accel, &keyval, &mods);
2456 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
2458 /* FIXME this is wrong, needs to be in the global accel resolution
2459 * thing, to properly consider i18n etc., but that probably requires
2460 * AccelGroup changes etc.
2462 if (event->keyval == keyval &&
2463 (mods & event->state) == mods)
2464 gtk_menu_shell_cancel (menu_shell);
2472 switch (event->keyval)
2483 /* Figure out what modifiers went into determining the key symbol */
2484 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
2485 event->hardware_keycode, event->state, event->group,
2486 NULL, NULL, NULL, &consumed_modifiers);
2488 accel_key = gdk_keyval_to_lower (event->keyval);
2489 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
2491 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
2492 * We re-upper case when we match against the keyval, but display and save in caseless form.
2494 if (accel_key != event->keyval)
2495 accel_mods |= GDK_SHIFT_MASK;
2497 /* Modify the accelerators */
2498 if (can_change_accels &&
2499 menu_shell->active_menu_item &&
2500 GTK_BIN (menu_shell->active_menu_item)->child && /* no separators */
2501 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
2502 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
2504 GtkWidget *menu_item = menu_shell->active_menu_item;
2505 gboolean locked, replace_accels = TRUE;
2508 path = get_accel_path (menu_item, &locked);
2509 if (!path || locked)
2511 /* can't change accelerators on menu_items without paths
2512 * (basically, those items are accelerator-locked).
2514 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
2515 gdk_display_beep (display);
2521 /* For the keys that act to delete the current setting, we delete
2522 * the current setting if there is one, otherwise, we set the
2523 * key as the accelerator.
2529 if (gtk_accel_map_lookup_entry (path, &key) &&
2530 (key.accel_key || key.accel_mods))
2536 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
2540 /* we failed, probably because this key is in use and
2543 /* g_print("failed to change\n"); */
2544 gdk_display_beep (display);
2553 gtk_menu_motion_notify (GtkWidget *widget,
2554 GdkEventMotion *event)
2556 GtkWidget *menu_item;
2558 GtkMenuShell *menu_shell;
2560 gboolean need_enter;
2562 if (GTK_IS_MENU (widget))
2563 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2565 /* We received the event for one of two reasons:
2567 * a) We are the active menu, and did gtk_grab_add()
2568 * b) The widget is a child of ours, and the event was propagated
2570 * Since for computation of navigation regions, we want the menu which
2571 * is the parent of the menu item, for a), we need to find that menu,
2572 * which may be different from 'widget'.
2574 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2575 if (!menu_item || !GTK_IS_MENU_ITEM (menu_item) ||
2576 !_gtk_menu_item_is_selectable (menu_item) ||
2577 !GTK_IS_MENU (menu_item->parent))
2580 menu_shell = GTK_MENU_SHELL (menu_item->parent);
2581 menu = GTK_MENU (menu_shell);
2583 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
2585 /* Check to see if we are within an active submenu's navigation region
2587 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2592 /* The menu is now sensitive to enter events on its items, but
2593 * was previously sensitive. So we fake an enter event.
2597 menu_shell->ignore_enter = FALSE;
2599 gdk_drawable_get_size (event->window, &width, &height);
2600 if (event->x >= 0 && event->x < width &&
2601 event->y >= 0 && event->y < height)
2603 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2606 send_event->crossing.window = g_object_ref (event->window);
2607 send_event->crossing.time = event->time;
2608 send_event->crossing.send_event = TRUE;
2609 send_event->crossing.x_root = event->x_root;
2610 send_event->crossing.y_root = event->y_root;
2611 send_event->crossing.x = event->x;
2612 send_event->crossing.y = event->y;
2614 /* We send the event to 'widget', the currently active menu,
2615 * instead of 'menu', the menu that the pointer is in. This
2616 * will ensure that the event will be ignored unless the
2617 * menuitem is a child of the active menu or some parent
2618 * menu of the active menu.
2620 result = gtk_widget_event (widget, send_event);
2621 gdk_event_free (send_event);
2631 gtk_menu_scroll_timeout (gpointer data)
2636 gint view_width, view_height;
2638 GDK_THREADS_ENTER ();
2640 menu = GTK_MENU (data);
2641 widget = GTK_WIDGET (menu);
2643 offset = menu->scroll_offset + menu->scroll_step;
2645 /* If we scroll upward and the non-visible top part
2646 * is smaller than the scroll arrow it would be
2647 * pretty stupid to show the arrow and taking more
2648 * screen space than just scrolling to the top.
2650 if ((menu->scroll_step < 0) && (offset < MENU_SCROLL_ARROW_HEIGHT))
2653 /* Don't scroll over the top if we weren't before: */
2654 if ((menu->scroll_offset >= 0) && (offset < 0))
2657 gdk_drawable_get_size (widget->window, &view_width, &view_height);
2659 /* Don't scroll past the bottom if we weren't before: */
2660 if (menu->scroll_offset > 0)
2661 view_height -= MENU_SCROLL_ARROW_HEIGHT;
2663 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
2664 (offset + view_height > widget->requisition.height))
2665 offset = widget->requisition.height - view_height;
2667 gtk_menu_scroll_to (menu, offset);
2669 GDK_THREADS_LEAVE ();
2675 gtk_menu_handle_scrolling (GtkMenu *menu, gboolean enter)
2677 GtkMenuShell *menu_shell;
2683 gboolean scroll_fast = FALSE;
2684 guint vertical_padding;
2686 menu_shell = GTK_MENU_SHELL (menu);
2688 gdk_window_get_pointer (GTK_WIDGET (menu)->window, &x, &y, NULL);
2689 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
2691 gtk_widget_style_get (GTK_WIDGET (menu),
2692 "vertical-padding", &vertical_padding,
2695 border = GTK_CONTAINER (menu)->border_width +
2696 GTK_WIDGET (menu)->style->ythickness + vertical_padding;
2698 if (menu->upper_arrow_visible && !menu->tearoff_active)
2703 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2706 if ((x >= rect.x) && (x < rect.x + rect.width) &&
2707 (y >= rect.y) && (y < rect.y + rect.height))
2710 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
2713 if (enter && in_arrow &&
2714 (!menu->upper_arrow_prelight || menu->scroll_fast != scroll_fast))
2716 menu->upper_arrow_prelight = TRUE;
2717 menu->scroll_fast = scroll_fast;
2718 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2720 /* Deselect the active item so that any submenus are poped down */
2721 gtk_menu_shell_deselect (menu_shell);
2723 gtk_menu_remove_scroll_timeout (menu);
2724 menu->scroll_step = (scroll_fast) ? -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
2725 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2726 gtk_menu_scroll_timeout,
2729 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
2731 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2733 gtk_menu_stop_scrolling (menu);
2737 if (menu->lower_arrow_visible && !menu->tearoff_active)
2740 rect.y = height - border - MENU_SCROLL_ARROW_HEIGHT;
2742 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2745 if ((x >= rect.x) && (x < rect.x + rect.width) &&
2746 (y >= rect.y) && (y < rect.y + rect.height))
2749 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
2752 if (enter && in_arrow &&
2753 (!menu->lower_arrow_prelight || menu->scroll_fast != scroll_fast))
2755 menu->lower_arrow_prelight = TRUE;
2756 menu->scroll_fast = scroll_fast;
2757 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2759 /* Deselect the active item so that any submenus are poped down */
2760 gtk_menu_shell_deselect (menu_shell);
2762 gtk_menu_remove_scroll_timeout (menu);
2763 menu->scroll_step = (scroll_fast) ? MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
2764 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2765 gtk_menu_scroll_timeout,
2768 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
2770 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2772 gtk_menu_stop_scrolling (menu);
2778 gtk_menu_enter_notify (GtkWidget *widget,
2779 GdkEventCrossing *event)
2781 GtkWidget *menu_item;
2783 if (widget && GTK_IS_MENU (widget))
2785 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
2787 if (!menu_shell->ignore_enter)
2788 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2791 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
2792 * will not correspond to the event widget's parent. Check to see
2793 * if we are in the parent's navigation region.
2795 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2796 if (menu_item && GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
2797 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent), event->x_root, event->y_root))
2800 return GTK_WIDGET_CLASS (parent_class)->enter_notify_event (widget, event);
2804 gtk_menu_leave_notify (GtkWidget *widget,
2805 GdkEventCrossing *event)
2807 GtkMenuShell *menu_shell;
2809 GtkMenuItem *menu_item;
2810 GtkWidget *event_widget;
2812 menu = GTK_MENU (widget);
2813 menu_shell = GTK_MENU_SHELL (widget);
2815 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2818 gtk_menu_handle_scrolling (menu, FALSE);
2820 event_widget = gtk_get_event_widget ((GdkEvent*) event);
2822 if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
2825 menu_item = GTK_MENU_ITEM (event_widget);
2827 /* Here we check to see if we're leaving an active menu item with a submenu,
2828 * in which case we enter submenu navigation mode.
2830 if (menu_shell->active_menu_item != NULL
2831 && menu_item->submenu != NULL
2832 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
2834 if (GTK_MENU_SHELL (menu_item->submenu)->active)
2836 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
2841 return GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event);
2845 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
2847 if (menu->navigation_region)
2849 gdk_region_destroy (menu->navigation_region);
2850 menu->navigation_region = NULL;
2853 if (menu->navigation_timeout)
2855 g_source_remove (menu->navigation_timeout);
2856 menu->navigation_timeout = 0;
2860 /* When the timeout is elapsed, the navigation region is destroyed
2861 * and the menuitem under the pointer (if any) is selected.
2864 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
2866 GtkMenu *menu = user_data;
2867 GdkWindow *child_window;
2869 GDK_THREADS_ENTER ();
2871 gtk_menu_stop_navigating_submenu (menu);
2873 if (GTK_WIDGET_REALIZED (menu))
2875 child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
2879 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2881 send_event->crossing.window = g_object_ref (child_window);
2882 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
2883 send_event->crossing.send_event = TRUE;
2885 GTK_WIDGET_CLASS (parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
2887 gdk_event_free (send_event);
2891 GDK_THREADS_LEAVE ();
2897 gtk_menu_navigating_submenu (GtkMenu *menu,
2901 if (menu->navigation_region)
2903 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
2907 gtk_menu_stop_navigating_submenu (menu);
2914 #undef DRAW_STAY_UP_TRIANGLE
2916 #ifdef DRAW_STAY_UP_TRIANGLE
2919 draw_stay_up_triangle (GdkWindow *window,
2922 /* Draw ugly color all over the stay-up triangle */
2923 GdkColor ugly_color = { 0, 50000, 10000, 10000 };
2924 GdkGCValues gc_values;
2926 GdkRectangle clipbox;
2928 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
2929 ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
2930 gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
2931 gdk_gc_set_clip_region (ugly_gc, region);
2933 gdk_region_get_clipbox (region, &clipbox);
2935 gdk_draw_rectangle (window,
2938 clipbox.x, clipbox.y,
2939 clipbox.width, clipbox.height);
2941 g_object_unref (ugly_gc);
2946 flip_region (GdkRegion *region,
2951 GdkRectangle *rectangles;
2952 GdkRectangle clipbox;
2953 GdkRegion *new_region;
2956 new_region = gdk_region_new ();
2958 gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
2959 gdk_region_get_clipbox (region, &clipbox);
2961 for (i = 0; i < n_rectangles; ++i)
2963 GdkRectangle rect = rectangles[i];
2966 rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
2969 rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
2971 gdk_region_union_with_rect (new_region, &rect);
2974 g_free (rectangles);
2980 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
2981 GtkMenuItem *menu_item,
2982 GdkEventCrossing *event)
2984 gint submenu_left = 0;
2985 gint submenu_right = 0;
2986 gint submenu_top = 0;
2987 gint submenu_bottom = 0;
2991 GtkWidget *event_widget;
2993 g_return_if_fail (menu_item->submenu != NULL);
2994 g_return_if_fail (event != NULL);
2996 event_widget = gtk_get_event_widget ((GdkEvent*) event);
2998 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
2999 gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
3001 submenu_right = submenu_left + width;
3002 submenu_bottom = submenu_top + height;
3004 gdk_drawable_get_size (event_widget->window, &width, &height);
3006 if (event->x >= 0 && event->x < width)
3009 gboolean flip_y = FALSE;
3010 gboolean flip_x = FALSE;
3012 gtk_menu_stop_navigating_submenu (menu);
3014 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
3017 point[0].x = event->x_root;
3018 point[1].x = submenu_left;
3023 point[0].x = event->x_root + 1;
3024 point[1].x = 2 * (event->x_root + 1) - submenu_right;
3032 point[0].y = event->y_root + 1;
3033 point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
3035 if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
3043 point[0].y = event->y_root;
3044 point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
3046 if (point[0].y >= submenu_bottom)
3050 point[2].x = point[1].x;
3051 point[2].y = point[0].y;
3053 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
3055 if (flip_x || flip_y)
3057 GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
3058 gdk_region_destroy (menu->navigation_region);
3059 menu->navigation_region = new_region;
3062 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3063 "gtk-menu-popdown-delay", &popdown_delay,
3066 menu->navigation_timeout = g_timeout_add (popdown_delay,
3067 gtk_menu_stop_navigating_submenu_cb, menu);
3069 #ifdef DRAW_STAY_UP_TRIANGLE
3070 draw_stay_up_triangle (gdk_get_default_root_window(),
3071 menu->navigation_region);
3077 gtk_menu_deactivate (GtkMenuShell *menu_shell)
3081 g_return_if_fail (GTK_IS_MENU (menu_shell));
3083 parent = menu_shell->parent_menu_shell;
3085 menu_shell->activate_time = 0;
3086 gtk_menu_popdown (GTK_MENU (menu_shell));
3089 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
3093 gtk_menu_position (GtkMenu *menu)
3096 GtkRequisition requisition;
3097 GtkMenuPrivate *private;
3103 GdkScreen *pointer_screen;
3104 GdkRectangle monitor;
3106 g_return_if_fail (GTK_IS_MENU (menu));
3108 widget = GTK_WIDGET (menu);
3110 screen = gtk_widget_get_screen (widget);
3111 gdk_display_get_pointer (gdk_screen_get_display (screen),
3112 &pointer_screen, &x, &y, NULL);
3114 /* We need the requisition to figure out the right place to
3115 * popup the menu. In fact, we always need to ask here, since
3116 * if a size_request was queued while we weren't popped up,
3117 * the requisition won't have been recomputed yet.
3119 gtk_widget_size_request (widget, &requisition);
3121 if (pointer_screen != screen)
3123 /* Pointer is on a different screen; roughly center the
3124 * menu on the screen. If someone was using multiscreen
3125 * + Xinerama together they'd probably want something
3126 * fancier; but that is likely to be vanishingly rare.
3128 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
3129 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
3132 private = gtk_menu_get_private (menu);
3133 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
3137 if (menu->position_func)
3139 (* menu->position_func) (menu, &x, &y, &push_in, menu->position_func_data);
3140 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3144 gint space_left, space_right, space_above, space_below;
3147 gint xthickness = widget->style->xthickness;
3148 gint ythickness = widget->style->ythickness;
3149 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
3151 /* The placement of popup menus horizontally works like this (with
3152 * RTL in parentheses)
3154 * - If there is enough room to the right (left) of the mouse cursor,
3155 * position the menu there.
3157 * - Otherwise, if if there is enough room to the left (right) of the
3158 * mouse cursor, position the menu there.
3160 * - Otherwise if the menu is smaller than the monitor, position it
3161 * on the side of the mouse cursor that has the most space available
3163 * - Otherwise (if there is simply not enough room for the menu on the
3164 * monitor), position it as far left (right) as possible.
3166 * Positioning in the vertical direction is similar: first try below
3167 * mouse cursor, then above.
3169 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3171 space_left = x - monitor.x;
3172 space_right = monitor.x + monitor.width - x - 1;
3173 space_above = y - monitor.y;
3174 space_below = monitor.y + monitor.height - y - 1;
3176 /* position horizontally */
3178 /* the amount of space we need to position the menu. Note the
3179 * menu is offset "xthickness" pixels
3181 needed_width = requisition.width - xthickness;
3183 if (needed_width <= space_left ||
3184 needed_width <= space_right)
3186 if ((rtl && needed_width <= space_left) ||
3187 (!rtl && needed_width > space_right))
3190 x = x + xthickness - requisition.width + 1;
3194 /* position right */
3198 /* x is clamped on-screen further down */
3200 else if (requisition.width <= monitor.width)
3202 /* the menu is too big to fit on either side of the mouse
3203 * cursor, but smaller than the monitor. Position it on
3204 * the side that has the most space
3206 if (space_left > space_right)
3214 x = monitor.x + monitor.width - requisition.width;
3217 else /* menu is simply too big for the monitor */
3222 x = monitor.x + monitor.width - requisition.width;
3231 /* Position vertically. The algorithm is the same as above, but
3232 * simpler because we don't have to take RTL into account.
3234 needed_height = requisition.height - ythickness;
3236 if (needed_height <= space_above ||
3237 needed_height <= space_below)
3239 if (needed_height <= space_below)
3242 y = y + ythickness - requisition.height + 1;
3244 y = CLAMP (y, monitor.y,
3245 monitor.y + monitor.height - requisition.height);
3247 else if (needed_height > space_below && needed_height > space_above)
3249 if (space_below >= space_above)
3250 y = monitor.y + monitor.height - requisition.height;
3264 menu_height = GTK_WIDGET (menu)->requisition.height;
3266 if (y + menu_height > monitor.y + monitor.height)
3268 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
3269 y = (monitor.y + monitor.height) - menu_height;
3274 scroll_offset += monitor.y - y;
3279 /* FIXME: should this be done in the various position_funcs ? */
3280 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
3282 if (GTK_MENU_SHELL (menu)->active)
3284 private->have_position = TRUE;
3289 if (y + requisition.height > monitor.y + monitor.height)
3290 requisition.height = (monitor.y + monitor.height) - y;
3294 scroll_offset += monitor.y - y;
3295 requisition.height -= monitor.y - y;
3299 if (scroll_offset > 0)
3300 scroll_offset += MENU_SCROLL_ARROW_HEIGHT;
3302 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
3305 if (!GTK_MENU_SHELL (menu)->active)
3307 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
3308 requisition.width, requisition.height);
3311 menu->scroll_offset = scroll_offset;
3315 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
3317 if (menu->timeout_id)
3319 g_source_remove (menu->timeout_id);
3320 menu->timeout_id = 0;
3325 gtk_menu_stop_scrolling (GtkMenu *menu)
3327 gtk_menu_remove_scroll_timeout (menu);
3329 menu->upper_arrow_prelight = FALSE;
3330 menu->lower_arrow_prelight = FALSE;
3334 gtk_menu_scroll_to (GtkMenu *menu,
3339 gint view_width, view_height;
3341 gboolean last_visible;
3343 guint vertical_padding;
3345 widget = GTK_WIDGET (menu);
3347 if (menu->tearoff_active &&
3348 menu->tearoff_adjustment &&
3349 (menu->tearoff_adjustment->value != offset))
3351 menu->tearoff_adjustment->value =
3353 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
3354 gtk_adjustment_value_changed (menu->tearoff_adjustment);
3357 /* Move/resize the viewport according to arrows: */
3358 view_width = widget->allocation.width;
3359 view_height = widget->allocation.height;
3361 gtk_widget_style_get (GTK_WIDGET (menu),
3362 "vertical-padding", &vertical_padding,
3365 border_width = GTK_CONTAINER (menu)->border_width;
3366 view_width -= (border_width + widget->style->xthickness) * 2;
3367 view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
3368 menu_height = widget->requisition.height -
3369 (border_width + widget->style->ythickness + vertical_padding) * 2;
3371 x = border_width + widget->style->xthickness;
3372 y = border_width + widget->style->ythickness + vertical_padding;
3374 if (!menu->tearoff_active)
3376 last_visible = menu->upper_arrow_visible;
3377 menu->upper_arrow_visible = offset > 0;
3379 if (menu->upper_arrow_visible)
3380 view_height -= MENU_SCROLL_ARROW_HEIGHT;
3382 if ( (last_visible != menu->upper_arrow_visible) &&
3383 !menu->upper_arrow_visible)
3385 menu->upper_arrow_prelight = FALSE;
3387 /* If we hid the upper arrow, possibly remove timeout */
3388 if (menu->scroll_step < 0)
3390 gtk_menu_stop_scrolling (menu);
3391 gtk_widget_queue_draw (GTK_WIDGET (menu));
3395 last_visible = menu->lower_arrow_visible;
3396 menu->lower_arrow_visible = offset < menu_height - view_height;
3398 if (menu->lower_arrow_visible)
3399 view_height -= MENU_SCROLL_ARROW_HEIGHT;
3401 if ( (last_visible != menu->lower_arrow_visible) &&
3402 !menu->lower_arrow_visible)
3404 menu->lower_arrow_prelight = FALSE;
3406 /* If we hid the lower arrow, possibly remove timeout */
3407 if (menu->scroll_step > 0)
3409 gtk_menu_stop_scrolling (menu);
3410 gtk_widget_queue_draw (GTK_WIDGET (menu));
3414 if (menu->upper_arrow_visible)
3415 y += MENU_SCROLL_ARROW_HEIGHT;
3418 /* Scroll the menu: */
3419 if (GTK_WIDGET_REALIZED (menu))
3420 gdk_window_move (menu->bin_window, 0, -offset);
3422 if (GTK_WIDGET_REALIZED (menu))
3423 gdk_window_move_resize (menu->view_window,
3429 menu->scroll_offset = offset;
3433 compute_child_offset (GtkMenu *menu,
3434 GtkWidget *menu_item,
3437 gboolean *is_last_child)
3439 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3440 guint item_top_attach;
3441 guint item_bottom_attach;
3442 gint child_offset = 0;
3445 gtk_container_child_get (GTK_CONTAINER (menu), menu_item,
3446 "top_attach", &item_top_attach,
3447 "bottom_attach", &item_bottom_attach,
3450 /* there is a possibility that we get called before _size_request, so
3451 * check the height table for safety.
3453 if (!priv->heights || priv->heights_length < priv->rows)
3456 /* when we have a row with only invisible children, it's height will
3457 * be zero, so there's no need to check WIDGET_VISIBLE here
3459 for (i = 0; i < item_top_attach; i++)
3460 child_offset += priv->heights[i];
3463 *is_last_child = (item_bottom_attach == priv->rows);
3465 *offset = child_offset;
3467 *height = priv->heights[item_top_attach];
3473 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
3474 GtkWidget *menu_item)
3477 gint child_offset, child_height;
3481 gboolean last_child = 0;
3483 menu = GTK_MENU (menu_shell);
3485 /* We need to check if the selected item fully visible.
3486 * If not we need to scroll the menu so that it becomes fully
3490 if (compute_child_offset (menu, menu_item,
3491 &child_offset, &child_height, &last_child))
3493 guint vertical_padding;
3495 y = menu->scroll_offset;
3496 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3498 gtk_widget_style_get (GTK_WIDGET (menu),
3499 "vertical-padding", &vertical_padding,
3502 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
3504 if (child_offset < y)
3506 /* Ignore the enter event we might get if the pointer is on the menu
3508 menu_shell->ignore_enter = TRUE;
3509 gtk_menu_scroll_to (menu, child_offset);
3514 if (menu->upper_arrow_visible && !menu->tearoff_active)
3515 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3516 if (menu->lower_arrow_visible && !menu->tearoff_active)
3517 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3519 if (child_offset + child_height > y + height - arrow_height)
3522 if (!last_child && !menu->tearoff_active)
3523 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3525 y = child_offset + child_height - height + arrow_height;
3526 if ((y > 0) && !menu->tearoff_active)
3528 /* Need upper arrow */
3529 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3530 y = child_offset + child_height - height + arrow_height;
3532 /* Ignore the enter event we might get if the pointer is on the menu
3534 menu_shell->ignore_enter = TRUE;
3535 gtk_menu_scroll_to (menu, y);
3543 gtk_menu_select_item (GtkMenuShell *menu_shell,
3544 GtkWidget *menu_item)
3546 GtkMenu *menu = GTK_MENU (menu_shell);
3548 if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
3549 gtk_menu_scroll_item_visible (menu_shell, menu_item);
3551 GTK_MENU_SHELL_CLASS (parent_class)->select_item (menu_shell, menu_item);
3555 /* Reparent the menu, taking care of the refcounting
3557 * If unrealize is true we force a unrealize while reparenting the parent.
3558 * This can help eliminate flicker in some cases.
3560 * What happens is that when the menu is unrealized and then re-realized,
3561 * the allocations are as follows:
3563 * parent - 1x1 at (0,0)
3564 * child1 - 100x20 at (0,0)
3565 * child2 - 100x20 at (0,20)
3566 * child3 - 100x20 at (0,40)
3568 * That is, the parent is small but the children are full sized. Then,
3569 * when the queued_resize gets processed, the parent gets resized to
3572 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
3573 * contains the following logic:
3575 * - if a move or resize operation on a window would change the clip
3576 * region on the children, then before the window is resized
3577 * the background for children is temporarily set to None, the
3578 * move/resize done, and the background for the children restored.
3580 * So, at the point where the parent is resized to final size, the
3581 * background for the children is temporarily None, and thus they
3582 * are not cleared to the background color and the previous background
3583 * (the image of the menu) is left in place.
3586 gtk_menu_reparent (GtkMenu *menu,
3587 GtkWidget *new_parent,
3590 GtkObject *object = GTK_OBJECT (menu);
3591 GtkWidget *widget = GTK_WIDGET (menu);
3592 gboolean was_floating = GTK_OBJECT_FLOATING (object);
3594 g_object_ref (object);
3595 gtk_object_sink (object);
3599 g_object_ref (object);
3600 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
3601 gtk_container_add (GTK_CONTAINER (new_parent), widget);
3602 g_object_unref (object);
3605 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
3608 GTK_OBJECT_SET_FLAGS (object, GTK_FLOATING);
3610 g_object_unref (object);
3614 gtk_menu_show_all (GtkWidget *widget)
3616 /* Show children, but not self. */
3617 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
3622 gtk_menu_hide_all (GtkWidget *widget)
3624 /* Hide children, but not self. */
3625 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
3629 * gtk_menu_set_screen:
3630 * @menu: a #GtkMenu.
3631 * @screen: a #GdkScreen, or %NULL if the screen should be
3632 * determined by the widget the menu is attached to.
3634 * Sets the #GdkScreen on which the menu will be displayed.
3639 gtk_menu_set_screen (GtkMenu *menu,
3642 g_return_if_fail (GTK_IS_MENU (menu));
3643 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
3645 g_object_set_data (G_OBJECT (menu), "gtk-menu-explicit-screen", screen);
3649 menu_change_screen (menu, screen);
3653 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
3655 attach_widget_screen_changed (attach_widget, NULL, menu);
3661 * @menu: a #GtkMenu.
3662 * @child: a #GtkMenuItem.
3663 * @left_attach: The column number to attach the left side of the item to.
3664 * @right_attach: The column number to attach the right side of the item to.
3665 * @top_attach: The row number to attach the top of the item to.
3666 * @bottom_attach: The row number to attach the bottom of the item to.
3668 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
3669 * an item will occupy is specified by @left_attach, @right_attach,
3670 * @top_attach and @bottom_attach. These each represent the leftmost,
3671 * rightmost, uppermost and lower column and row numbers of the table.
3672 * (Columns and rows are indexed from zero).
3677 gtk_menu_attach (GtkMenu *menu,
3682 guint bottom_attach)
3684 g_return_if_fail (GTK_IS_MENU (menu));
3685 g_return_if_fail (GTK_IS_MENU_ITEM (child));
3687 g_return_if_fail (left_attach < right_attach);
3688 g_return_if_fail (top_attach < bottom_attach);
3691 gtk_menu_shell_append (GTK_MENU_SHELL (menu), child);
3693 gtk_container_child_set (GTK_CONTAINER (menu), child,
3694 "left_attach", left_attach,
3695 "right_attach", right_attach,
3696 "top_attach", top_attach,
3697 "bottom_attach", bottom_attach,
3702 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
3706 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
3707 "gtk-menu-popup-delay", &popup_delay,
3714 find_child_containing (GtkMenuShell *menu_shell,
3722 /* find a child which includes the area given by
3723 * left, right, top, bottom.
3726 for (list = menu_shell->children; list; list = list->next)
3730 if (!_gtk_menu_item_is_selectable (list->data))
3733 get_child_attach (list->data, &l, &r, &t, &b);
3735 if (l <= left && right <= r
3736 && t <= top && bottom <= b)
3737 return GTK_WIDGET (list->data);
3744 gtk_menu_move_current (GtkMenuShell *menu_shell,
3745 GtkMenuDirectionType direction)
3747 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu_shell));
3749 /* use special table menu key bindings */
3750 if (menu_shell->active_menu_item && priv->columns > 1)
3754 gboolean rtl = (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL);
3755 GtkWidget *match = NULL;
3757 get_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
3759 if (direction == GTK_MENU_DIR_NEXT)
3761 for (i = b; i < priv->rows; i++)
3763 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
3771 for (i = 0; i < t; i++)
3773 match = find_child_containing (menu_shell,
3774 l, l + 1, i, i + 1);
3780 else if (direction == GTK_MENU_DIR_PREV)
3782 for (i = t; i > 0; i--)
3784 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
3792 for (i = priv->rows; i > b; i--)
3794 match = find_child_containing (menu_shell,
3795 l, l + 1, i - 1, i);
3801 else if ((!rtl && direction == GTK_MENU_DIR_PARENT)
3802 || (rtl && direction == GTK_MENU_DIR_CHILD))
3804 /* we go one left if possible */
3806 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
3810 GtkWidget *parent = menu_shell->parent_menu_shell;
3813 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
3814 match = menu_shell->active_menu_item;
3817 else if ((!rtl && direction == GTK_MENU_DIR_CHILD)
3818 || (rtl && direction == GTK_MENU_DIR_PARENT))
3820 /* we go one right if possible */
3821 if (r < priv->columns)
3822 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
3826 GtkWidget *parent = menu_shell->parent_menu_shell;
3828 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
3830 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
3831 match = menu_shell->active_menu_item;
3837 gtk_menu_shell_select_item (menu_shell, match);
3842 GTK_MENU_SHELL_CLASS (parent_class)->move_current (menu_shell, direction);
3846 get_visible_size (GtkMenu *menu)
3848 GtkWidget *widget = GTK_WIDGET (menu);
3849 GtkContainer *container = GTK_CONTAINER (menu);
3851 gint menu_height = (widget->allocation.height
3852 - 2 * (container->border_width
3853 + widget->style->ythickness));
3855 if (menu->upper_arrow_visible && !menu->tearoff_active)
3856 menu_height -= MENU_SCROLL_ARROW_HEIGHT;
3857 if (menu->lower_arrow_visible && !menu->tearoff_active)
3858 menu_height -= MENU_SCROLL_ARROW_HEIGHT;
3863 /* Find the sensitive on-screen child containing @y, or if none,
3864 * the nearest selectable onscreen child. (%NULL if none)
3867 child_at (GtkMenu *menu,
3870 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3871 GtkWidget *child = NULL;
3872 gint child_offset = 0;
3875 gint lower, upper; /* Onscreen bounds */
3877 menu_height = get_visible_size (menu);
3878 lower = menu->scroll_offset;
3879 upper = menu->scroll_offset + menu_height;
3881 for (children = menu_shell->children; children; children = children->next)
3883 if (GTK_WIDGET_VISIBLE (children->data))
3885 GtkRequisition child_requisition;
3887 gtk_widget_size_request (children->data, &child_requisition);
3889 if (_gtk_menu_item_is_selectable (children->data) &&
3890 child_offset >= lower &&
3891 child_offset + child_requisition.height <= upper)
3893 child = children->data;
3895 if (child_offset + child_requisition.height > y &&
3896 !GTK_IS_TEAROFF_MENU_ITEM (child))
3900 child_offset += child_requisition.height;
3908 get_menu_height (GtkMenu *menu)
3911 GtkWidget *widget = GTK_WIDGET (menu);
3913 height = widget->requisition.height;
3914 height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
3916 if (menu->upper_arrow_visible && !menu->tearoff_active)
3917 height -= MENU_SCROLL_ARROW_HEIGHT;
3919 if (menu->lower_arrow_visible && !menu->tearoff_active)
3920 height -= MENU_SCROLL_ARROW_HEIGHT;
3926 gtk_menu_real_move_scroll (GtkMenu *menu,
3929 gint page_size = get_visible_size (menu);
3930 gint end_position = get_menu_height (menu);
3931 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3935 case GTK_SCROLL_PAGE_UP:
3936 case GTK_SCROLL_PAGE_DOWN:
3940 gint child_offset = 0;
3941 gboolean old_upper_arrow_visible;
3944 if (type == GTK_SCROLL_PAGE_UP)
3949 if (menu_shell->active_menu_item)
3953 compute_child_offset (menu, menu_shell->active_menu_item,
3954 &child_offset, &child_height, NULL);
3955 child_offset += child_height / 2;
3958 menu_shell->ignore_enter = TRUE;
3959 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
3960 old_offset = menu->scroll_offset;
3962 new_offset = menu->scroll_offset + step;
3963 new_offset = CLAMP (new_offset, 0, end_position - page_size);
3965 gtk_menu_scroll_to (menu, new_offset);
3967 if (menu_shell->active_menu_item)
3969 GtkWidget *new_child;
3970 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
3972 if (menu->scroll_offset != old_offset)
3973 step = menu->scroll_offset - old_offset;
3975 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * MENU_SCROLL_ARROW_HEIGHT;
3977 new_child = child_at (menu, child_offset + step);
3979 gtk_menu_shell_select_item (menu_shell, new_child);
3983 case GTK_SCROLL_START:
3984 /* Ignore the enter event we might get if the pointer is on the menu
3986 menu_shell->ignore_enter = TRUE;
3987 gtk_menu_scroll_to (menu, 0);
3988 gtk_menu_shell_select_first (menu_shell, TRUE);
3990 case GTK_SCROLL_END:
3991 /* Ignore the enter event we might get if the pointer is on the menu
3993 menu_shell->ignore_enter = TRUE;
3994 gtk_menu_scroll_to (menu, end_position - page_size);
3995 _gtk_menu_shell_select_last (menu_shell, TRUE);
4004 * gtk_menu_set_monitor:
4006 * @monitor_num: the number of the monitor on which the menu should
4009 * Informs GTK+ on which monitor a menu should be popped up.
4010 * See gdk_screen_get_monitor_geometry().
4012 * This function should be called from a #GtkMenuPositionFunc if the
4013 * menu should not appear on the same monitor as the pointer. This
4014 * information can't be reliably inferred from the coordinates returned
4015 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
4016 * may extend beyond the monitor boundaries or even the screen boundaries.
4020 void gtk_menu_set_monitor (GtkMenu *menu,
4023 GtkMenuPrivate *priv;
4024 g_return_if_fail (GTK_IS_MENU (menu));
4026 priv = gtk_menu_get_private (menu);
4028 priv->monitor_num = monitor_num;