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 225
51 #define DEFAULT_POPDOWN_DELAY 1000
53 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
54 * extends below the submenu
57 #define MENU_SCROLL_STEP1 8
58 #define MENU_SCROLL_STEP2 15
59 #define MENU_SCROLL_ARROW_HEIGHT 16
60 #define MENU_SCROLL_FAST_ZONE 8
61 #define MENU_SCROLL_TIMEOUT1 50
62 #define MENU_SCROLL_TIMEOUT2 50
64 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
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);
600 priv->columns = MAX (priv->columns, ai->right_attach);
602 case CHILD_PROP_TOP_ATTACH:
603 ai->top_attach = g_value_get_uint (value);
605 case CHILD_PROP_BOTTOM_ATTACH:
606 ai->bottom_attach = g_value_get_uint (value);
607 priv->rows = MAX (priv->rows, ai->bottom_attach);
610 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
614 gtk_widget_queue_resize (GTK_WIDGET (menu));
618 gtk_menu_get_child_property (GtkContainer *container,
624 AttachInfo *ai = get_attach_info (G_OBJECT (child));
628 case CHILD_PROP_LEFT_ATTACH:
629 g_value_set_uint (value, ai->left_attach);
631 case CHILD_PROP_RIGHT_ATTACH:
632 g_value_set_uint (value, ai->right_attach);
634 case CHILD_PROP_TOP_ATTACH:
635 g_value_set_uint (value, ai->top_attach);
637 case CHILD_PROP_BOTTOM_ATTACH:
638 g_value_set_uint (value, ai->bottom_attach);
642 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
648 get_child_attach (GtkWidget *child,
654 gtk_container_child_get (GTK_CONTAINER (child->parent), child,
663 gtk_menu_window_event (GtkWidget *window,
667 gboolean handled = FALSE;
669 g_object_ref (window);
675 case GDK_KEY_RELEASE:
676 handled = gtk_widget_event (menu, event);
682 g_object_unref (window);
683 g_object_unref (menu);
689 gtk_menu_window_size_request (GtkWidget *window,
690 GtkRequisition *requisition,
693 GtkMenuPrivate *private = gtk_menu_get_private (menu);
695 if (private->have_position)
697 GdkScreen *screen = gtk_widget_get_screen (window);
698 GdkRectangle monitor;
700 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
702 if (private->y + requisition->height > monitor.y + monitor.height)
703 requisition->height = monitor.y + monitor.height - private->y;
705 if (private->y < monitor.y)
706 requisition->height -= monitor.y - private->y;
711 gtk_menu_init (GtkMenu *menu)
713 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
715 menu->parent_menu_item = NULL;
716 menu->old_active_menu_item = NULL;
717 menu->accel_group = NULL;
718 menu->position_func = NULL;
719 menu->position_func_data = NULL;
720 menu->toggle_size = 0;
722 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
723 "type", GTK_WINDOW_POPUP,
726 "signal::event", gtk_menu_window_event, menu,
727 "signal::size_request", gtk_menu_window_size_request, menu,
728 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
730 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
731 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
733 /* Refloat the menu, so that reference counting for the menu isn't
734 * affected by it being a child of the toplevel
736 GTK_WIDGET_SET_FLAGS (menu, GTK_FLOATING);
737 menu->needs_destruction_ref_count = TRUE;
739 menu->view_window = NULL;
740 menu->bin_window = NULL;
742 menu->scroll_offset = 0;
743 menu->scroll_step = 0;
744 menu->timeout_id = 0;
745 menu->scroll_fast = FALSE;
747 menu->tearoff_window = NULL;
748 menu->tearoff_hbox = NULL;
749 menu->torn_off = FALSE;
750 menu->tearoff_active = FALSE;
751 menu->tearoff_adjustment = NULL;
752 menu->tearoff_scrollbar = NULL;
754 menu->upper_arrow_visible = FALSE;
755 menu->lower_arrow_visible = FALSE;
756 menu->upper_arrow_prelight = FALSE;
757 menu->lower_arrow_prelight = FALSE;
759 MENU_NEEDS_RESIZE (menu) = TRUE;
765 gtk_menu_destroy (GtkObject *object)
768 GtkMenuAttachData *data;
770 g_return_if_fail (GTK_IS_MENU (object));
772 menu = GTK_MENU (object);
774 gtk_menu_stop_scrolling (menu);
776 data = g_object_get_data (G_OBJECT (object), attach_data_key);
778 gtk_menu_detach (menu);
780 gtk_menu_stop_navigating_submenu (menu);
782 if (menu->old_active_menu_item)
784 g_object_unref (menu->old_active_menu_item);
785 menu->old_active_menu_item = NULL;
788 /* Add back the reference count for being a child */
789 if (menu->needs_destruction_ref_count)
791 menu->needs_destruction_ref_count = FALSE;
792 g_object_ref (object);
795 if (menu->accel_group)
797 g_object_unref (menu->accel_group);
798 menu->accel_group = NULL;
802 gtk_widget_destroy (menu->toplevel);
803 if (menu->tearoff_window)
804 gtk_widget_destroy (menu->tearoff_window);
806 GTK_OBJECT_CLASS (parent_class)->destroy (object);
810 gtk_menu_finalize (GObject *object)
812 GtkMenu *menu = GTK_MENU (object);
814 g_free (menu->accel_path);
816 G_OBJECT_CLASS (parent_class)->finalize (object);
820 menu_change_screen (GtkMenu *menu,
821 GdkScreen *new_screen)
823 GtkMenuPrivate *private = gtk_menu_get_private (menu);
827 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
828 gtk_menu_position (menu);
831 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
832 private->monitor_num = -1;
836 attach_widget_screen_changed (GtkWidget *attach_widget,
837 GdkScreen *previous_screen,
840 if (gtk_widget_has_screen (attach_widget) &&
841 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
843 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
848 gtk_menu_attach_to_widget (GtkMenu *menu,
849 GtkWidget *attach_widget,
850 GtkMenuDetachFunc detacher)
852 GtkMenuAttachData *data;
854 g_return_if_fail (GTK_IS_MENU (menu));
855 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
856 g_return_if_fail (detacher != NULL);
858 /* keep this function in sync with gtk_widget_set_parent()
861 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
864 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
865 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
870 gtk_object_sink (GTK_OBJECT (menu));
872 data = g_new (GtkMenuAttachData, 1);
873 data->attach_widget = attach_widget;
875 g_signal_connect (attach_widget, "screen_changed",
876 G_CALLBACK (attach_widget_screen_changed), menu);
877 attach_widget_screen_changed (attach_widget, NULL, menu);
879 data->detacher = detacher;
880 g_object_set_data (G_OBJECT (menu), attach_data_key, data);
882 if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
883 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
885 /* we don't need to set the style here, since
886 * we are a toplevel widget.
889 /* Fallback title for menu comes from attach widget */
890 gtk_menu_update_title (menu);
894 gtk_menu_get_attach_widget (GtkMenu *menu)
896 GtkMenuAttachData *data;
898 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
900 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
902 return data->attach_widget;
907 gtk_menu_detach (GtkMenu *menu)
909 GtkMenuAttachData *data;
911 g_return_if_fail (GTK_IS_MENU (menu));
913 /* keep this function in sync with gtk_widget_unparent()
915 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
918 g_warning ("gtk_menu_detach(): menu is not attached");
921 g_object_set_data (G_OBJECT (menu), attach_data_key, NULL);
923 g_signal_handlers_disconnect_by_func (data->attach_widget,
924 (gpointer) attach_widget_screen_changed,
927 data->detacher (data->attach_widget, menu);
929 if (GTK_WIDGET_REALIZED (menu))
930 gtk_widget_unrealize (GTK_WIDGET (menu));
934 /* Fallback title for menu comes from attach widget */
935 gtk_menu_update_title (menu);
937 g_object_unref (menu);
941 gtk_menu_do_remove (GtkMenuShell *menu_shell,
946 GtkMenuPrivate *priv;
948 gboolean single_column;
950 ai = get_attach_info (G_OBJECT (child));
951 priv = gtk_menu_get_private (GTK_MENU (menu_shell));
952 delta = ai->bottom_attach - ai->top_attach;
953 single_column = priv->columns == 1;
955 /* Recalculate these, assuming the child has already been removed.
956 * Note that an empty menu is assumed to have one column
961 for (children = menu_shell->children; children; children = children->next)
963 AttachInfo *child_ai = get_attach_info (children->data);
965 /* Do not move items in table menus */
966 if (single_column && child_ai->bottom_attach > ai->bottom_attach)
967 gtk_container_child_set (GTK_CONTAINER (menu_shell), children->data,
968 "top_attach", child_ai->top_attach - delta,
969 "bottom_attach", child_ai->bottom_attach - delta,
973 priv->columns = MAX (priv->columns, child_ai->right_attach);
974 priv->rows = MAX (priv->rows, child_ai->bottom_attach);
980 gtk_menu_remove (GtkContainer *container,
985 g_return_if_fail (GTK_IS_MENU (container));
986 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
988 menu = GTK_MENU (container);
990 /* Clear out old_active_menu_item if it matches the item we are removing
992 if (menu->old_active_menu_item == widget)
994 g_object_unref (menu->old_active_menu_item);
995 menu->old_active_menu_item = NULL;
998 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
999 gtk_menu_do_remove (GTK_MENU_SHELL (container), widget);
1000 g_object_set_data (G_OBJECT (widget), ATTACH_INFO_KEY, NULL);
1007 return g_object_new (GTK_TYPE_MENU, NULL);
1011 gtk_menu_do_insert (GtkMenuShell *menu_shell,
1016 GtkMenuPrivate *priv;
1018 priv = gtk_menu_get_private (GTK_MENU (menu_shell));
1020 if (position < 0 || position >= priv->rows)
1022 /* attach after the last row */
1023 gtk_menu_attach (GTK_MENU (menu_shell), child,
1025 priv->rows, priv->rows + 1);
1030 /* We need to make space for this new item; move all items with
1031 * top >= position one down.
1032 * Note that this does not prevent overlaps when the menu contains
1033 * vertically spanning items.
1035 for (children = menu_shell->children; children; children = children->next)
1037 AttachInfo *child_ai = get_attach_info (children->data);
1039 if (child_ai->top_attach >= position)
1040 gtk_container_child_set (GTK_CONTAINER (menu_shell), children->data,
1041 "top_attach", child_ai->top_attach + 1,
1042 "bottom_attach", child_ai->bottom_attach + 1,
1046 /* attach the new item */
1047 gtk_menu_attach (GTK_MENU (menu_shell), child,
1049 position, position + 1);
1053 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1057 if (GTK_WIDGET_REALIZED (menu_shell))
1058 gtk_widget_set_parent_window (child, GTK_MENU (menu_shell)->bin_window);
1060 GTK_MENU_SHELL_CLASS (parent_class)->insert (menu_shell, child, position);
1062 gtk_menu_do_insert (menu_shell, child, position);
1066 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1071 widget = GTK_WIDGET (menu);
1077 GdkGCValues gc_values;
1079 menu->tearoff_active = FALSE;
1080 menu->saved_scroll_offset = menu->scroll_offset;
1082 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
1083 gc = gdk_gc_new_with_values (widget->window,
1084 &gc_values, GDK_GC_SUBWINDOW);
1086 gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1088 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
1093 gdk_draw_drawable (pixmap, gc,
1094 menu->tearoff_window->window,
1095 0, 0, 0, 0, -1, -1);
1096 g_object_unref (gc);
1098 gtk_widget_set_size_request (menu->tearoff_window,
1102 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
1103 g_object_unref (pixmap);
1108 popup_grab_on_window (GdkWindow *window,
1109 guint32 activate_time)
1111 if ((gdk_pointer_grab (window, TRUE,
1112 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1113 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1114 GDK_POINTER_MOTION_MASK,
1115 NULL, NULL, activate_time) == 0))
1117 if (gdk_keyboard_grab (window, TRUE,
1118 activate_time) == 0)
1122 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1133 * @menu: a #GtkMenu.
1134 * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
1135 * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
1136 * @func: a user supplied function used to position the menu, or %NULL
1137 * @data: user supplied data to be passed to @func.
1138 * @button: the mouse button which was pressed to initiate the event.
1139 * @activate_time: the time at which the activation event occurred.
1141 * Displays a menu and makes it available for selection. Applications can use
1142 * this function to display context-sensitive menus, and will typically supply
1143 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1144 * parameters. The default menu positioning function will position the menu
1145 * at the current mouse cursor position.
1147 * The @button parameter should be the mouse button pressed to initiate
1148 * the menu popup. If the menu popup was initiated by something other than
1149 * a mouse button press, such as a mouse button release or a keypress,
1150 * @button should be 0.
1152 * The @activate_time parameter should be the time stamp of the event that
1153 * initiated the popup. If such an event is not available, use
1154 * gtk_get_current_event_time() instead.
1158 gtk_menu_popup (GtkMenu *menu,
1159 GtkWidget *parent_menu_shell,
1160 GtkWidget *parent_menu_item,
1161 GtkMenuPositionFunc func,
1164 guint32 activate_time)
1167 GtkWidget *xgrab_shell;
1169 GdkEvent *current_event;
1170 GtkMenuShell *menu_shell;
1172 g_return_if_fail (GTK_IS_MENU (menu));
1174 widget = GTK_WIDGET (menu);
1175 menu_shell = GTK_MENU_SHELL (menu);
1177 menu_shell->parent_menu_shell = parent_menu_shell;
1179 /* Find the last viewable ancestor, and make an X grab on it
1181 parent = GTK_WIDGET (menu);
1185 gboolean viewable = TRUE;
1186 GtkWidget *tmp = parent;
1190 if (!GTK_WIDGET_MAPPED (tmp))
1199 xgrab_shell = parent;
1201 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1204 /* We want to receive events generated when we map the menu; unfortunately,
1205 * since there is probably already an implicit grab in place from the
1206 * button that the user used to pop up the menu, we won't receive then --
1207 * in particular, the EnterNotify when the menu pops up under the pointer.
1209 * If we are grabbing on a parent menu shell, no problem; just grab on
1210 * that menu shell first before popping up the window with owner_events = TRUE.
1212 * When grabbing on the menu itself, things get more convuluted - we
1213 * we do an explicit grab on a specially created window with
1214 * owner_events = TRUE, which we override further down with a grab
1215 * on the menu. (We can't grab on the menu until it is mapped; we
1216 * probably could just leave the grab on the other window, with a
1217 * little reorganization of the code in gtkmenu*).
1219 if (xgrab_shell && xgrab_shell != widget)
1221 if (popup_grab_on_window (xgrab_shell->window, activate_time))
1222 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1226 GdkWindow *transfer_window;
1228 xgrab_shell = widget;
1229 transfer_window = menu_grab_transfer_window_get (menu);
1230 if (popup_grab_on_window (transfer_window, activate_time))
1231 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1234 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1236 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1237 * with a stuck up window, we just abort here. Presumably the user will
1240 menu_shell->parent_menu_shell = NULL;
1241 menu_grab_transfer_window_destroy (menu);
1245 menu_shell->active = TRUE;
1246 menu_shell->button = button;
1248 /* If we are popping up the menu from something other than, a button
1249 * press then, as a heuristic, we ignore enter events for the menu
1250 * until we get a MOTION_NOTIFY.
1253 current_event = gtk_get_current_event ();
1256 if ((current_event->type != GDK_BUTTON_PRESS) &&
1257 (current_event->type != GDK_ENTER_NOTIFY))
1258 menu_shell->ignore_enter = TRUE;
1260 gdk_event_free (current_event);
1263 menu_shell->ignore_enter = TRUE;
1267 gtk_menu_tearoff_bg_copy (menu);
1269 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1272 menu->parent_menu_item = parent_menu_item;
1273 menu->position_func = func;
1274 menu->position_func_data = data;
1275 menu_shell->activate_time = activate_time;
1277 /* We need to show the menu here rather in the init function because
1278 * code expects to be able to tell if the menu is onscreen by
1279 * looking at the GTK_WIDGET_VISIBLE (menu)
1281 gtk_widget_show (GTK_WIDGET (menu));
1283 /* Position the menu, possibly changing the size request
1285 gtk_menu_position (menu);
1287 /* Compute the size of the toplevel and realize it so we
1288 * can scroll correctly.
1291 GtkRequisition tmp_request;
1292 GtkAllocation tmp_allocation = { 0, };
1294 gtk_widget_size_request (menu->toplevel, &tmp_request);
1296 tmp_allocation.width = tmp_request.width;
1297 tmp_allocation.height = tmp_request.height;
1299 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1301 gtk_widget_realize (GTK_WIDGET (menu));
1304 gtk_menu_scroll_to (menu, menu->scroll_offset);
1306 /* Once everything is set up correctly, map the toplevel window on
1309 gtk_widget_show (menu->toplevel);
1311 if (xgrab_shell == widget)
1312 popup_grab_on_window (widget->window, activate_time); /* Should always succeed */
1313 gtk_grab_add (GTK_WIDGET (menu));
1317 gtk_menu_popdown (GtkMenu *menu)
1319 GtkMenuPrivate *private;
1320 GtkMenuShell *menu_shell;
1322 g_return_if_fail (GTK_IS_MENU (menu));
1324 menu_shell = GTK_MENU_SHELL (menu);
1325 private = gtk_menu_get_private (menu);
1327 menu_shell->parent_menu_shell = NULL;
1328 menu_shell->active = FALSE;
1329 menu_shell->ignore_enter = FALSE;
1331 private->have_position = FALSE;
1333 gtk_menu_stop_scrolling (menu);
1335 gtk_menu_stop_navigating_submenu (menu);
1337 if (menu_shell->active_menu_item)
1339 if (menu->old_active_menu_item)
1340 g_object_unref (menu->old_active_menu_item);
1341 menu->old_active_menu_item = menu_shell->active_menu_item;
1342 g_object_ref (menu->old_active_menu_item);
1345 gtk_menu_shell_deselect (menu_shell);
1347 /* The X Grab, if present, will automatically be removed when we hide
1349 gtk_widget_hide (menu->toplevel);
1353 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1355 if (GTK_BIN (menu->toplevel)->child)
1357 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1361 /* We popped up the menu from the tearoff, so we need to
1362 * release the grab - we aren't actually hiding the menu.
1364 if (menu_shell->have_xgrab)
1366 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
1368 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1369 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1373 /* gtk_menu_popdown is called each time a menu item is selected from
1374 * a torn off menu. Only scroll back to the saved position if the
1375 * non-tearoff menu was popped down.
1377 if (!menu->tearoff_active)
1378 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1379 menu->tearoff_active = TRUE;
1382 gtk_widget_hide (GTK_WIDGET (menu));
1384 menu_shell->have_xgrab = FALSE;
1385 gtk_grab_remove (GTK_WIDGET (menu));
1387 menu_grab_transfer_window_destroy (menu);
1391 gtk_menu_get_active (GtkMenu *menu)
1396 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1398 if (!menu->old_active_menu_item)
1401 children = GTK_MENU_SHELL (menu)->children;
1405 child = children->data;
1406 children = children->next;
1408 if (GTK_BIN (child)->child)
1413 menu->old_active_menu_item = child;
1414 if (menu->old_active_menu_item)
1415 g_object_ref (menu->old_active_menu_item);
1418 return menu->old_active_menu_item;
1422 gtk_menu_set_active (GtkMenu *menu,
1428 g_return_if_fail (GTK_IS_MENU (menu));
1430 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1433 child = tmp_list->data;
1434 if (GTK_BIN (child)->child)
1436 if (menu->old_active_menu_item)
1437 g_object_unref (menu->old_active_menu_item);
1438 menu->old_active_menu_item = child;
1439 g_object_ref (menu->old_active_menu_item);
1445 gtk_menu_set_accel_group (GtkMenu *menu,
1446 GtkAccelGroup *accel_group)
1448 g_return_if_fail (GTK_IS_MENU (menu));
1450 if (menu->accel_group != accel_group)
1452 if (menu->accel_group)
1453 g_object_unref (menu->accel_group);
1454 menu->accel_group = accel_group;
1455 if (menu->accel_group)
1456 g_object_ref (menu->accel_group);
1457 _gtk_menu_refresh_accel_paths (menu, TRUE);
1462 gtk_menu_get_accel_group (GtkMenu *menu)
1464 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1466 return menu->accel_group;
1470 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1473 /* Menu items chain here to figure whether they can activate their
1474 * accelerators. Unlike ordinary widgets, menus allow accel
1475 * activation even if invisible since that's the usual case for
1476 * submenus/popup-menus. however, the state of the attach widget
1477 * affects the "activeness" of the menu.
1479 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1482 return gtk_widget_can_activate_accel (awidget, signal_id);
1484 return GTK_WIDGET_IS_SENSITIVE (widget);
1488 * gtk_menu_set_accel_path
1489 * @menu: a valid #GtkMenu
1490 * @accel_path: a valid accelerator path
1492 * Sets an accelerator path for this menu from which accelerator paths
1493 * for its immediate children, its menu items, can be constructed.
1494 * The main purpose of this function is to spare the programmer the
1495 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1496 * each menu item that should support runtime user changable accelerators.
1497 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1498 * each menu item of this menu, that contains a label describing its purpose,
1499 * automatically gets an accel path assigned. For example, a menu containing
1500 * menu items "New" and "Exit", will, after
1501 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1502 * has been called, assign its items the accel paths:
1503 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1504 * Assigning accel paths to menu items then enables the user to change
1505 * their accelerators at runtime. More details about accelerator paths
1506 * and their default setups can be found at gtk_accel_map_add_entry().
1509 gtk_menu_set_accel_path (GtkMenu *menu,
1510 const gchar *accel_path)
1512 g_return_if_fail (GTK_IS_MENU (menu));
1514 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1516 g_free (menu->accel_path);
1517 menu->accel_path = g_strdup (accel_path);
1518 if (menu->accel_path)
1519 _gtk_menu_refresh_accel_paths (menu, FALSE);
1524 gboolean group_changed;
1528 refresh_accel_paths_foreach (GtkWidget *widget,
1531 AccelPropagation *prop = data;
1533 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1534 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1535 prop->menu->accel_path,
1536 prop->menu->accel_group,
1537 prop->group_changed);
1541 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1542 gboolean group_changed)
1544 g_return_if_fail (GTK_IS_MENU (menu));
1546 if (menu->accel_path && menu->accel_group)
1548 AccelPropagation prop;
1551 prop.group_changed = group_changed;
1552 gtk_container_foreach (GTK_CONTAINER (menu),
1553 refresh_accel_paths_foreach,
1559 gtk_menu_reposition (GtkMenu *menu)
1561 g_return_if_fail (GTK_IS_MENU (menu));
1563 if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1564 gtk_menu_position (menu);
1568 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1571 g_return_if_fail (GTK_IS_MENU (menu));
1573 if (adjustment->value != menu->scroll_offset)
1574 gtk_menu_scroll_to (menu, adjustment->value);
1578 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1581 GdkGeometry geometry_hints;
1583 if (!menu->tearoff_window)
1586 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1588 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1589 width += menu->tearoff_scrollbar->requisition.width;
1592 geometry_hints.min_width = width;
1593 geometry_hints.max_width = width;
1595 geometry_hints.min_height = 0;
1596 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1598 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1601 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1605 gtk_menu_update_title (GtkMenu *menu)
1607 if (menu->tearoff_window)
1610 GtkWidget *attach_widget;
1612 title = gtk_menu_get_title (menu);
1615 attach_widget = gtk_menu_get_attach_widget (menu);
1616 if (GTK_IS_MENU_ITEM (attach_widget))
1618 GtkWidget *child = GTK_BIN (attach_widget)->child;
1619 if (GTK_IS_LABEL (child))
1620 title = gtk_label_get_text (GTK_LABEL (child));
1625 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1630 gtk_menu_get_toplevel (GtkWidget *menu)
1632 GtkWidget *attach, *toplevel;
1634 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
1636 if (GTK_IS_MENU_ITEM (attach))
1637 attach = attach->parent;
1639 if (GTK_IS_MENU (attach))
1640 return gtk_menu_get_toplevel (attach->parent);
1641 else if (GTK_IS_WIDGET (attach))
1643 toplevel = gtk_widget_get_toplevel (attach->parent);
1644 if (GTK_WIDGET_TOPLEVEL (toplevel))
1652 gtk_menu_set_tearoff_state (GtkMenu *menu,
1657 g_return_if_fail (GTK_IS_MENU (menu));
1659 if (menu->torn_off != torn_off)
1661 menu->torn_off = torn_off;
1662 menu->tearoff_active = torn_off;
1666 if (GTK_WIDGET_VISIBLE (menu))
1667 gtk_menu_popdown (menu);
1669 if (!menu->tearoff_window)
1671 GtkWidget *toplevel;
1673 menu->tearoff_window = gtk_widget_new (GTK_TYPE_WINDOW,
1674 "type", GTK_WINDOW_TOPLEVEL,
1675 "screen", gtk_widget_get_screen (menu->toplevel),
1676 "app_paintable", TRUE,
1680 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1681 GDK_WINDOW_TYPE_HINT_MENU);
1682 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1683 g_signal_connect (menu->tearoff_window, "destroy",
1684 G_CALLBACK (gtk_widget_destroyed), &menu->tearoff_window);
1685 g_signal_connect (menu->tearoff_window, "event",
1686 G_CALLBACK (gtk_menu_window_event), menu);
1688 gtk_menu_update_title (menu);
1690 gtk_widget_realize (menu->tearoff_window);
1692 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
1693 if (toplevel != NULL)
1694 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
1695 GTK_WINDOW (toplevel));
1697 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1698 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1700 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
1701 menu->tearoff_adjustment =
1702 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1704 GTK_WIDGET (menu)->requisition.height,
1708 g_object_connect (menu->tearoff_adjustment,
1709 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1711 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1713 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1714 menu->tearoff_scrollbar,
1717 if (menu->tearoff_adjustment->upper > height)
1718 gtk_widget_show (menu->tearoff_scrollbar);
1720 gtk_widget_show (menu->tearoff_hbox);
1723 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
1725 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
1727 /* Update menu->requisition
1729 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
1731 gtk_menu_set_tearoff_hints (menu, width);
1733 gtk_widget_realize (menu->tearoff_window);
1734 gtk_menu_position (menu);
1736 gtk_widget_show (GTK_WIDGET (menu));
1737 gtk_widget_show (menu->tearoff_window);
1739 gtk_menu_scroll_to (menu, 0);
1744 gtk_widget_hide (GTK_WIDGET (menu));
1745 gtk_widget_hide (menu->tearoff_window);
1746 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1747 gtk_widget_destroy (menu->tearoff_window);
1749 menu->tearoff_window = NULL;
1750 menu->tearoff_hbox = NULL;
1751 menu->tearoff_scrollbar = NULL;
1752 menu->tearoff_adjustment = NULL;
1758 * gtk_menu_get_tearoff_state:
1761 * Returns whether the menu is torn off. See
1762 * gtk_menu_set_tearoff_state ().
1764 * Return value: %TRUE if the menu is currently torn off.
1767 gtk_menu_get_tearoff_state (GtkMenu *menu)
1769 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
1771 return menu->torn_off;
1775 * gtk_menu_set_title:
1777 * @title: a string containing the title for the menu.
1779 * Sets the title string for the menu. The title is displayed when the menu
1780 * is shown as a tearoff menu.
1783 gtk_menu_set_title (GtkMenu *menu,
1786 g_return_if_fail (GTK_IS_MENU (menu));
1789 g_object_set_data_full (G_OBJECT (menu), "gtk-menu-title",
1790 g_strdup (title), (GtkDestroyNotify) g_free);
1792 g_object_set_data (G_OBJECT (menu), "gtk-menu-title", NULL);
1794 gtk_menu_update_title (menu);
1795 g_object_notify (G_OBJECT (menu), "tearoff_title");
1799 * gtk_menu_get_title:
1802 * Returns the title of the menu. See gtk_menu_set_title().
1804 * Return value: the title of the menu, or %NULL if the menu has no
1805 * title set on it. This string is owned by the widget and should
1806 * not be modified or freed.
1808 G_CONST_RETURN gchar *
1809 gtk_menu_get_title (GtkMenu *menu)
1811 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1813 return g_object_get_data (G_OBJECT (menu), "gtk-menu-title");
1817 gtk_menu_reorder_child (GtkMenu *menu,
1821 GtkMenuShell *menu_shell;
1823 g_return_if_fail (GTK_IS_MENU (menu));
1824 g_return_if_fail (GTK_IS_MENU_ITEM (child));
1826 menu_shell = GTK_MENU_SHELL (menu);
1828 if (g_list_find (menu_shell->children, child))
1830 menu_shell->children = g_list_remove (menu_shell->children, child);
1831 menu_shell->children = g_list_insert (menu_shell->children, child, position);
1832 gtk_menu_do_insert (menu_shell, child, position);
1834 if (GTK_WIDGET_VISIBLE (menu_shell))
1835 gtk_widget_queue_resize (GTK_WIDGET (menu_shell));
1840 gtk_menu_style_set (GtkWidget *widget,
1841 GtkStyle *previous_style)
1843 if (GTK_WIDGET_REALIZED (widget))
1845 GtkMenu *menu = GTK_MENU (widget);
1847 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1848 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1849 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1854 gtk_menu_realize (GtkWidget *widget)
1856 GdkWindowAttr attributes;
1857 gint attributes_mask;
1862 guint vertical_padding;
1864 g_return_if_fail (GTK_IS_MENU (widget));
1866 menu = GTK_MENU (widget);
1868 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1870 attributes.window_type = GDK_WINDOW_CHILD;
1871 attributes.x = widget->allocation.x;
1872 attributes.y = widget->allocation.y;
1873 attributes.width = widget->allocation.width;
1874 attributes.height = widget->allocation.height;
1875 attributes.wclass = GDK_INPUT_OUTPUT;
1876 attributes.visual = gtk_widget_get_visual (widget);
1877 attributes.colormap = gtk_widget_get_colormap (widget);
1879 attributes.event_mask = gtk_widget_get_events (widget);
1881 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
1882 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
1884 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1885 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1886 gdk_window_set_user_data (widget->window, widget);
1888 border_width = GTK_CONTAINER (widget)->border_width;
1890 gtk_widget_style_get (GTK_WIDGET (menu),
1891 "vertical-padding", &vertical_padding,
1894 attributes.x = border_width + widget->style->xthickness;
1895 attributes.y = border_width + widget->style->ythickness + vertical_padding;
1896 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
1897 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
1899 if (menu->upper_arrow_visible)
1901 attributes.y += MENU_SCROLL_ARROW_HEIGHT;
1902 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1904 if (menu->lower_arrow_visible)
1905 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1907 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
1908 gdk_window_set_user_data (menu->view_window, menu);
1912 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
1914 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
1915 gdk_window_set_user_data (menu->bin_window, menu);
1917 children = GTK_MENU_SHELL (menu)->children;
1920 child = children->data;
1921 children = children->next;
1923 gtk_widget_set_parent_window (child, menu->bin_window);
1926 widget->style = gtk_style_attach (widget->style, widget->window);
1927 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1928 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1929 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1931 if (GTK_MENU_SHELL (widget)->active_menu_item)
1932 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
1933 GTK_MENU_SHELL (widget)->active_menu_item);
1935 gdk_window_show (menu->bin_window);
1936 gdk_window_show (menu->view_window);
1940 gtk_menu_focus (GtkWidget *widget,
1941 GtkDirectionType direction)
1944 * A menu or its menu items cannot have focus
1949 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
1952 menu_grab_transfer_window_get (GtkMenu *menu)
1954 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1957 GdkWindowAttr attributes;
1958 gint attributes_mask;
1960 attributes.x = -100;
1961 attributes.y = -100;
1962 attributes.width = 10;
1963 attributes.height = 10;
1964 attributes.window_type = GDK_WINDOW_TEMP;
1965 attributes.wclass = GDK_INPUT_ONLY;
1966 attributes.override_redirect = TRUE;
1967 attributes.event_mask = 0;
1969 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
1971 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
1972 &attributes, attributes_mask);
1973 gdk_window_set_user_data (window, menu);
1975 gdk_window_show (window);
1977 g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", window);
1984 menu_grab_transfer_window_destroy (GtkMenu *menu)
1986 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1989 gdk_window_set_user_data (window, NULL);
1990 gdk_window_destroy (window);
1991 g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", NULL);
1996 gtk_menu_unrealize (GtkWidget *widget)
2000 g_return_if_fail (GTK_IS_MENU (widget));
2002 menu = GTK_MENU (widget);
2004 menu_grab_transfer_window_destroy (menu);
2006 gdk_window_set_user_data (menu->view_window, NULL);
2007 gdk_window_destroy (menu->view_window);
2008 menu->view_window = NULL;
2010 gdk_window_set_user_data (menu->bin_window, NULL);
2011 gdk_window_destroy (menu->bin_window);
2012 menu->bin_window = NULL;
2014 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
2018 gtk_menu_size_request (GtkWidget *widget,
2019 GtkRequisition *requisition)
2023 GtkMenuShell *menu_shell;
2026 guint max_toggle_size;
2027 guint max_accel_width;
2028 guint vertical_padding;
2029 GtkRequisition child_requisition;
2030 GtkMenuPrivate *priv;
2032 g_return_if_fail (GTK_IS_MENU (widget));
2033 g_return_if_fail (requisition != NULL);
2035 menu = GTK_MENU (widget);
2036 menu_shell = GTK_MENU_SHELL (widget);
2037 priv = gtk_menu_get_private (menu);
2039 requisition->width = 0;
2040 requisition->height = 0;
2042 max_toggle_size = 0;
2043 max_accel_width = 0;
2045 g_free (priv->heights);
2046 priv->heights = g_new0 (guint, priv->rows);
2047 priv->heights_length = priv->rows;
2049 children = menu_shell->children;
2056 child = children->data;
2057 children = children->next;
2059 if (! GTK_WIDGET_VISIBLE (child))
2062 get_child_attach (child, &l, &r, &t, &b);
2064 /* It's important to size_request the child
2065 * before doing the toggle size request, in
2066 * case the toggle size request depends on the size
2067 * request of a child of the child (e.g. for ImageMenuItem)
2070 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
2071 gtk_widget_size_request (child, &child_requisition);
2073 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2074 max_toggle_size = MAX (max_toggle_size, toggle_size);
2075 max_accel_width = MAX (max_accel_width,
2076 GTK_MENU_ITEM (child)->accelerator_width);
2078 part = child_requisition.width / (r - l);
2079 requisition->width = MAX (requisition->width, part);
2081 part = MAX (child_requisition.height, toggle_size) / (b - t);
2082 priv->heights[t] = MAX (priv->heights[t], part);
2085 for (i = 0; i < priv->rows; i++)
2086 requisition->height += priv->heights[i];
2088 requisition->width += max_toggle_size + max_accel_width;
2089 requisition->width *= priv->columns;
2090 requisition->width += (GTK_CONTAINER (menu)->border_width +
2091 widget->style->xthickness) * 2;
2093 gtk_widget_style_get (GTK_WIDGET (menu),
2094 "vertical-padding", &vertical_padding,
2096 requisition->height += (GTK_CONTAINER (menu)->border_width + vertical_padding +
2097 widget->style->ythickness) * 2;
2099 menu->toggle_size = max_toggle_size;
2101 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2103 if (menu->tearoff_active)
2104 gtk_menu_set_tearoff_hints (menu, requisition->width);
2108 gtk_menu_size_allocate (GtkWidget *widget,
2109 GtkAllocation *allocation)
2112 GtkMenuShell *menu_shell;
2114 GtkAllocation child_allocation;
2115 GtkRequisition child_requisition;
2116 GtkMenuPrivate *priv;
2120 guint vertical_padding;
2122 g_return_if_fail (GTK_IS_MENU (widget));
2123 g_return_if_fail (allocation != NULL);
2125 menu = GTK_MENU (widget);
2126 menu_shell = GTK_MENU_SHELL (widget);
2127 priv = gtk_menu_get_private (menu);
2129 widget->allocation = *allocation;
2130 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2132 gtk_widget_style_get (GTK_WIDGET (menu),
2133 "vertical-padding", &vertical_padding,
2136 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness;
2137 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness + vertical_padding;
2139 width = MAX (1, allocation->width - x * 2);
2140 height = MAX (1, allocation->height - y * 2);
2142 child_requisition.width -= x * 2;
2143 child_requisition.height -= y * 2;
2145 if (menu_shell->active)
2146 gtk_menu_scroll_to (menu, menu->scroll_offset);
2148 if (menu->upper_arrow_visible && !menu->tearoff_active)
2150 y += MENU_SCROLL_ARROW_HEIGHT;
2151 height -= MENU_SCROLL_ARROW_HEIGHT;
2154 if (menu->lower_arrow_visible && !menu->tearoff_active)
2155 height -= MENU_SCROLL_ARROW_HEIGHT;
2157 if (GTK_WIDGET_REALIZED (widget))
2159 gdk_window_move_resize (widget->window,
2160 allocation->x, allocation->y,
2161 allocation->width, allocation->height);
2163 gdk_window_move_resize (menu->view_window,
2170 if (menu_shell->children)
2172 gint base_width = width / priv->columns;
2174 children = menu_shell->children;
2177 child = children->data;
2178 children = children->next;
2180 if (GTK_WIDGET_VISIBLE (child))
2185 get_child_attach (child, &l, &r, &t, &b);
2187 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2190 tmp = priv->columns - l;
2191 l = priv->columns - r;
2195 child_allocation.width = (r - l) * base_width;
2196 child_allocation.height = 0;
2197 child_allocation.x = l * base_width;
2198 child_allocation.y = 0;
2200 for (i = 0; i < b; i++)
2203 child_allocation.y += priv->heights[i];
2205 child_allocation.height += priv->heights[i];
2208 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2211 gtk_widget_size_allocate (child, &child_allocation);
2212 gtk_widget_queue_draw (child);
2216 /* Resize the item window */
2217 if (GTK_WIDGET_REALIZED (widget))
2223 for (i = 0; i < priv->rows; i++)
2224 height += priv->heights[i];
2226 width = priv->columns * base_width;
2227 gdk_window_resize (menu->bin_window, width, height);
2230 if (menu->tearoff_active)
2232 if (allocation->height >= widget->requisition.height)
2234 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2236 gtk_widget_hide (menu->tearoff_scrollbar);
2237 gtk_menu_set_tearoff_hints (menu, allocation->width);
2239 gtk_menu_scroll_to (menu, 0);
2244 menu->tearoff_adjustment->upper = widget->requisition.height;
2245 menu->tearoff_adjustment->page_size = allocation->height;
2247 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2248 menu->tearoff_adjustment->upper)
2251 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2254 gtk_menu_scroll_to (menu, value);
2257 gtk_adjustment_changed (menu->tearoff_adjustment);
2259 if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2261 gtk_widget_show (menu->tearoff_scrollbar);
2262 gtk_menu_set_tearoff_hints (menu, allocation->width);
2270 gtk_menu_paint (GtkWidget *widget,
2271 GdkEventExpose *event)
2275 gint border_x, border_y;
2276 guint vertical_padding;
2278 g_return_if_fail (GTK_IS_MENU (widget));
2280 menu = GTK_MENU (widget);
2282 gtk_widget_style_get (GTK_WIDGET (menu),
2283 "vertical-padding", &vertical_padding,
2286 border_x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness;
2287 border_y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
2288 gdk_drawable_get_size (widget->window, &width, &height);
2290 if (event->window == widget->window)
2292 gint arrow_space = MENU_SCROLL_ARROW_HEIGHT - 2 * widget->style->ythickness;
2293 gint arrow_size = 0.7 * arrow_space;
2295 gtk_paint_box (widget->style,
2299 NULL, widget, "menu",
2301 if (menu->upper_arrow_visible && !menu->tearoff_active)
2303 gtk_paint_box (widget->style,
2305 menu->upper_arrow_prelight ?
2306 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2308 NULL, widget, "menu",
2311 width - 2 * border_x,
2312 MENU_SCROLL_ARROW_HEIGHT);
2314 gtk_paint_arrow (widget->style,
2316 menu->upper_arrow_prelight ?
2317 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2319 NULL, widget, "menu_scroll_arrow_up",
2322 (width - arrow_size ) / 2,
2323 border_y + widget->style->ythickness + (arrow_space - arrow_size)/2,
2324 arrow_size, arrow_size);
2327 if (menu->lower_arrow_visible && !menu->tearoff_active)
2329 gtk_paint_box (widget->style,
2331 menu->lower_arrow_prelight ?
2332 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2334 NULL, widget, "menu",
2336 height - border_y - MENU_SCROLL_ARROW_HEIGHT,
2338 MENU_SCROLL_ARROW_HEIGHT);
2340 gtk_paint_arrow (widget->style,
2342 menu->lower_arrow_prelight ?
2343 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2345 NULL, widget, "menu_scroll_arrow_down",
2348 (width - arrow_size) / 2,
2349 height - border_y - MENU_SCROLL_ARROW_HEIGHT +
2350 widget->style->ythickness + (arrow_space - arrow_size)/2,
2351 arrow_size, arrow_size);
2357 gtk_menu_expose (GtkWidget *widget,
2358 GdkEventExpose *event)
2360 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2361 g_return_val_if_fail (event != NULL, FALSE);
2363 if (GTK_WIDGET_DRAWABLE (widget))
2365 gtk_menu_paint (widget, event);
2367 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
2374 gtk_menu_show (GtkWidget *widget)
2376 GtkMenu *menu = GTK_MENU (widget);
2378 _gtk_menu_refresh_accel_paths (menu, FALSE);
2380 GTK_WIDGET_CLASS (parent_class)->show (widget);
2384 gtk_menu_button_press (GtkWidget *widget,
2385 GdkEventButton *event)
2387 /* Don't pop down the menu for releases over scroll arrows
2389 if (GTK_IS_MENU (widget))
2391 GtkMenu *menu = GTK_MENU (widget);
2393 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2397 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
2401 gtk_menu_button_release (GtkWidget *widget,
2402 GdkEventButton *event)
2404 /* Don't pop down the menu for releases over scroll arrows
2406 if (GTK_IS_MENU (widget))
2408 GtkMenu *menu = GTK_MENU (widget);
2410 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2414 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
2417 static const gchar *
2418 get_accel_path (GtkWidget *menu_item,
2423 GClosure *accel_closure;
2424 GtkAccelGroup *accel_group;
2426 path = _gtk_widget_get_accel_path (menu_item, locked);
2429 path = GTK_MENU_ITEM (menu_item)->accel_path;
2435 label = GTK_BIN (menu_item)->child;
2437 if (GTK_IS_ACCEL_LABEL (label))
2439 g_object_get (label,
2440 "accel_closure", &accel_closure,
2442 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
2444 *locked = accel_group->lock_count > 0;
2453 gtk_menu_key_press (GtkWidget *widget,
2456 GtkMenuShell *menu_shell;
2458 gboolean delete = FALSE;
2459 gboolean can_change_accels;
2460 gchar *accel = NULL;
2461 guint accel_key, accel_mods;
2462 GdkModifierType consumed_modifiers;
2463 GdkDisplay *display;
2465 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2466 g_return_val_if_fail (event != NULL, FALSE);
2468 menu_shell = GTK_MENU_SHELL (widget);
2469 menu = GTK_MENU (widget);
2471 gtk_menu_stop_navigating_submenu (menu);
2473 if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
2476 display = gtk_widget_get_display (widget);
2478 g_object_get (gtk_widget_get_settings (widget),
2479 "gtk-menu-bar-accel", &accel,
2480 "gtk-can-change-accels", &can_change_accels,
2486 GdkModifierType mods = 0;
2487 gboolean handled = FALSE;
2489 gtk_accelerator_parse (accel, &keyval, &mods);
2492 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
2494 /* FIXME this is wrong, needs to be in the global accel resolution
2495 * thing, to properly consider i18n etc., but that probably requires
2496 * AccelGroup changes etc.
2498 if (event->keyval == keyval &&
2499 (mods & event->state) == mods)
2500 gtk_menu_shell_cancel (menu_shell);
2508 switch (event->keyval)
2519 /* Figure out what modifiers went into determining the key symbol */
2520 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
2521 event->hardware_keycode, event->state, event->group,
2522 NULL, NULL, NULL, &consumed_modifiers);
2524 accel_key = gdk_keyval_to_lower (event->keyval);
2525 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
2527 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
2528 * We re-upper case when we match against the keyval, but display and save in caseless form.
2530 if (accel_key != event->keyval)
2531 accel_mods |= GDK_SHIFT_MASK;
2533 /* Modify the accelerators */
2534 if (can_change_accels &&
2535 menu_shell->active_menu_item &&
2536 GTK_BIN (menu_shell->active_menu_item)->child && /* no separators */
2537 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
2538 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
2540 GtkWidget *menu_item = menu_shell->active_menu_item;
2541 gboolean locked, replace_accels = TRUE;
2544 path = get_accel_path (menu_item, &locked);
2545 if (!path || locked)
2547 /* can't change accelerators on menu_items without paths
2548 * (basically, those items are accelerator-locked).
2550 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
2551 gdk_display_beep (display);
2557 /* For the keys that act to delete the current setting, we delete
2558 * the current setting if there is one, otherwise, we set the
2559 * key as the accelerator.
2565 if (gtk_accel_map_lookup_entry (path, &key) &&
2566 (key.accel_key || key.accel_mods))
2572 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
2576 /* we failed, probably because this key is in use and
2579 /* g_print("failed to change\n"); */
2580 gdk_display_beep (display);
2589 gtk_menu_motion_notify (GtkWidget *widget,
2590 GdkEventMotion *event)
2592 GtkWidget *menu_item;
2594 GtkMenuShell *menu_shell;
2596 gboolean need_enter;
2598 if (GTK_IS_MENU (widget))
2599 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2601 /* We received the event for one of two reasons:
2603 * a) We are the active menu, and did gtk_grab_add()
2604 * b) The widget is a child of ours, and the event was propagated
2606 * Since for computation of navigation regions, we want the menu which
2607 * is the parent of the menu item, for a), we need to find that menu,
2608 * which may be different from 'widget'.
2610 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2611 if (!menu_item || !GTK_IS_MENU_ITEM (menu_item) ||
2612 !GTK_IS_MENU (menu_item->parent))
2615 menu_shell = GTK_MENU_SHELL (menu_item->parent);
2616 menu = GTK_MENU (menu_shell);
2618 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
2620 /* Check to see if we are within an active submenu's navigation region
2622 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2625 /* Make sure we pop down if we enter a non-selectable menu item, so we
2626 * don't show a submenu when the cursor is outside the stay-up triangle.
2628 if (!_gtk_menu_item_is_selectable (menu_item))
2630 gtk_menu_shell_deselect (menu_shell);
2636 /* The menu is now sensitive to enter events on its items, but
2637 * was previously sensitive. So we fake an enter event.
2641 menu_shell->ignore_enter = FALSE;
2643 gdk_drawable_get_size (event->window, &width, &height);
2644 if (event->x >= 0 && event->x < width &&
2645 event->y >= 0 && event->y < height)
2647 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2650 send_event->crossing.window = g_object_ref (event->window);
2651 send_event->crossing.time = event->time;
2652 send_event->crossing.send_event = TRUE;
2653 send_event->crossing.x_root = event->x_root;
2654 send_event->crossing.y_root = event->y_root;
2655 send_event->crossing.x = event->x;
2656 send_event->crossing.y = event->y;
2658 /* We send the event to 'widget', the currently active menu,
2659 * instead of 'menu', the menu that the pointer is in. This
2660 * will ensure that the event will be ignored unless the
2661 * menuitem is a child of the active menu or some parent
2662 * menu of the active menu.
2664 result = gtk_widget_event (widget, send_event);
2665 gdk_event_free (send_event);
2675 gtk_menu_scroll_timeout (gpointer data)
2680 gint view_width, view_height;
2682 GDK_THREADS_ENTER ();
2684 menu = GTK_MENU (data);
2685 widget = GTK_WIDGET (menu);
2687 offset = menu->scroll_offset + menu->scroll_step;
2689 /* If we scroll upward and the non-visible top part
2690 * is smaller than the scroll arrow it would be
2691 * pretty stupid to show the arrow and taking more
2692 * screen space than just scrolling to the top.
2694 if ((menu->scroll_step < 0) && (offset < MENU_SCROLL_ARROW_HEIGHT))
2697 /* Don't scroll over the top if we weren't before: */
2698 if ((menu->scroll_offset >= 0) && (offset < 0))
2701 gdk_drawable_get_size (widget->window, &view_width, &view_height);
2703 /* Don't scroll past the bottom if we weren't before: */
2704 if (menu->scroll_offset > 0)
2705 view_height -= MENU_SCROLL_ARROW_HEIGHT;
2707 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
2708 (offset + view_height > widget->requisition.height))
2709 offset = widget->requisition.height - view_height;
2711 gtk_menu_scroll_to (menu, offset);
2713 GDK_THREADS_LEAVE ();
2719 gtk_menu_handle_scrolling (GtkMenu *menu, gboolean enter)
2721 GtkMenuShell *menu_shell;
2727 gboolean scroll_fast = FALSE;
2728 guint vertical_padding;
2730 menu_shell = GTK_MENU_SHELL (menu);
2732 gdk_window_get_pointer (GTK_WIDGET (menu)->window, &x, &y, NULL);
2733 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
2735 gtk_widget_style_get (GTK_WIDGET (menu),
2736 "vertical-padding", &vertical_padding,
2739 border = GTK_CONTAINER (menu)->border_width +
2740 GTK_WIDGET (menu)->style->ythickness + vertical_padding;
2742 if (menu->upper_arrow_visible && !menu->tearoff_active)
2747 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2750 if ((x >= rect.x) && (x < rect.x + rect.width) &&
2751 (y >= rect.y) && (y < rect.y + rect.height))
2754 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
2757 if (enter && in_arrow &&
2758 (!menu->upper_arrow_prelight || menu->scroll_fast != scroll_fast))
2760 menu->upper_arrow_prelight = TRUE;
2761 menu->scroll_fast = scroll_fast;
2762 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2764 /* Deselect the active item so that any submenus are poped down */
2765 gtk_menu_shell_deselect (menu_shell);
2767 gtk_menu_remove_scroll_timeout (menu);
2768 menu->scroll_step = (scroll_fast) ? -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
2769 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2770 gtk_menu_scroll_timeout,
2773 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
2775 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2777 gtk_menu_stop_scrolling (menu);
2781 if (menu->lower_arrow_visible && !menu->tearoff_active)
2784 rect.y = height - border - MENU_SCROLL_ARROW_HEIGHT;
2786 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2789 if ((x >= rect.x) && (x < rect.x + rect.width) &&
2790 (y >= rect.y) && (y < rect.y + rect.height))
2793 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
2796 if (enter && in_arrow &&
2797 (!menu->lower_arrow_prelight || menu->scroll_fast != scroll_fast))
2799 menu->lower_arrow_prelight = TRUE;
2800 menu->scroll_fast = scroll_fast;
2801 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2803 /* Deselect the active item so that any submenus are poped down */
2804 gtk_menu_shell_deselect (menu_shell);
2806 gtk_menu_remove_scroll_timeout (menu);
2807 menu->scroll_step = (scroll_fast) ? MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
2808 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2809 gtk_menu_scroll_timeout,
2812 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
2814 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2816 gtk_menu_stop_scrolling (menu);
2822 gtk_menu_enter_notify (GtkWidget *widget,
2823 GdkEventCrossing *event)
2825 GtkWidget *menu_item;
2827 if (widget && GTK_IS_MENU (widget))
2829 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
2831 if (!menu_shell->ignore_enter)
2832 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2835 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
2836 * will not correspond to the event widget's parent. Check to see
2837 * if we are in the parent's navigation region.
2839 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2840 if (menu_item && GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
2841 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent), event->x_root, event->y_root))
2844 return GTK_WIDGET_CLASS (parent_class)->enter_notify_event (widget, event);
2848 gtk_menu_leave_notify (GtkWidget *widget,
2849 GdkEventCrossing *event)
2851 GtkMenuShell *menu_shell;
2853 GtkMenuItem *menu_item;
2854 GtkWidget *event_widget;
2856 menu = GTK_MENU (widget);
2857 menu_shell = GTK_MENU_SHELL (widget);
2859 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2862 gtk_menu_handle_scrolling (menu, FALSE);
2864 event_widget = gtk_get_event_widget ((GdkEvent*) event);
2866 if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
2869 menu_item = GTK_MENU_ITEM (event_widget);
2871 /* Here we check to see if we're leaving an active menu item with a submenu,
2872 * in which case we enter submenu navigation mode.
2874 if (menu_shell->active_menu_item != NULL
2875 && menu_item->submenu != NULL
2876 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
2878 if (GTK_MENU_SHELL (menu_item->submenu)->active)
2880 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
2883 else if (menu_item == menu_shell->active_menu_item)
2885 /* We are leaving an active menu item with nonactive submenu.
2886 * Deselect it so we don't surprise the user with by popping
2887 * up a submenu _after_ he left the item.
2889 gtk_menu_shell_deselect (menu_shell);
2894 return GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event);
2898 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
2900 if (menu->navigation_region)
2902 gdk_region_destroy (menu->navigation_region);
2903 menu->navigation_region = NULL;
2906 if (menu->navigation_timeout)
2908 g_source_remove (menu->navigation_timeout);
2909 menu->navigation_timeout = 0;
2913 /* When the timeout is elapsed, the navigation region is destroyed
2914 * and the menuitem under the pointer (if any) is selected.
2917 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
2919 GtkMenu *menu = user_data;
2920 GdkWindow *child_window;
2922 GDK_THREADS_ENTER ();
2924 gtk_menu_stop_navigating_submenu (menu);
2926 if (GTK_WIDGET_REALIZED (menu))
2928 child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
2932 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2934 send_event->crossing.window = g_object_ref (child_window);
2935 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
2936 send_event->crossing.send_event = TRUE;
2938 GTK_WIDGET_CLASS (parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
2940 gdk_event_free (send_event);
2944 GDK_THREADS_LEAVE ();
2950 gtk_menu_navigating_submenu (GtkMenu *menu,
2954 if (menu->navigation_region)
2956 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
2960 gtk_menu_stop_navigating_submenu (menu);
2967 #undef DRAW_STAY_UP_TRIANGLE
2969 #ifdef DRAW_STAY_UP_TRIANGLE
2972 draw_stay_up_triangle (GdkWindow *window,
2975 /* Draw ugly color all over the stay-up triangle */
2976 GdkColor ugly_color = { 0, 50000, 10000, 10000 };
2977 GdkGCValues gc_values;
2979 GdkRectangle clipbox;
2981 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
2982 ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
2983 gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
2984 gdk_gc_set_clip_region (ugly_gc, region);
2986 gdk_region_get_clipbox (region, &clipbox);
2988 gdk_draw_rectangle (window,
2991 clipbox.x, clipbox.y,
2992 clipbox.width, clipbox.height);
2994 g_object_unref (ugly_gc);
2999 flip_region (GdkRegion *region,
3004 GdkRectangle *rectangles;
3005 GdkRectangle clipbox;
3006 GdkRegion *new_region;
3009 new_region = gdk_region_new ();
3011 gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
3012 gdk_region_get_clipbox (region, &clipbox);
3014 for (i = 0; i < n_rectangles; ++i)
3016 GdkRectangle rect = rectangles[i];
3019 rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
3022 rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
3024 gdk_region_union_with_rect (new_region, &rect);
3027 g_free (rectangles);
3033 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
3034 GtkMenuItem *menu_item,
3035 GdkEventCrossing *event)
3037 gint submenu_left = 0;
3038 gint submenu_right = 0;
3039 gint submenu_top = 0;
3040 gint submenu_bottom = 0;
3044 GtkWidget *event_widget;
3046 g_return_if_fail (menu_item->submenu != NULL);
3047 g_return_if_fail (event != NULL);
3049 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3051 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
3052 gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
3054 submenu_right = submenu_left + width;
3055 submenu_bottom = submenu_top + height;
3057 gdk_drawable_get_size (event_widget->window, &width, &height);
3059 if (event->x >= 0 && event->x < width)
3062 gboolean flip_y = FALSE;
3063 gboolean flip_x = FALSE;
3065 gtk_menu_stop_navigating_submenu (menu);
3067 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
3070 point[0].x = event->x_root;
3071 point[1].x = submenu_left;
3076 point[0].x = event->x_root + 1;
3077 point[1].x = 2 * (event->x_root + 1) - submenu_right;
3085 point[0].y = event->y_root + 1;
3086 point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
3088 if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
3096 point[0].y = event->y_root;
3097 point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
3099 if (point[0].y >= submenu_bottom)
3103 point[2].x = point[1].x;
3104 point[2].y = point[0].y;
3106 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
3108 if (flip_x || flip_y)
3110 GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
3111 gdk_region_destroy (menu->navigation_region);
3112 menu->navigation_region = new_region;
3115 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3116 "gtk-menu-popdown-delay", &popdown_delay,
3119 menu->navigation_timeout = g_timeout_add (popdown_delay,
3120 gtk_menu_stop_navigating_submenu_cb, menu);
3122 #ifdef DRAW_STAY_UP_TRIANGLE
3123 draw_stay_up_triangle (gdk_get_default_root_window(),
3124 menu->navigation_region);
3130 gtk_menu_deactivate (GtkMenuShell *menu_shell)
3134 g_return_if_fail (GTK_IS_MENU (menu_shell));
3136 parent = menu_shell->parent_menu_shell;
3138 menu_shell->activate_time = 0;
3139 gtk_menu_popdown (GTK_MENU (menu_shell));
3142 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
3146 gtk_menu_position (GtkMenu *menu)
3149 GtkRequisition requisition;
3150 GtkMenuPrivate *private;
3156 GdkScreen *pointer_screen;
3157 GdkRectangle monitor;
3159 g_return_if_fail (GTK_IS_MENU (menu));
3161 widget = GTK_WIDGET (menu);
3163 screen = gtk_widget_get_screen (widget);
3164 gdk_display_get_pointer (gdk_screen_get_display (screen),
3165 &pointer_screen, &x, &y, NULL);
3167 /* We need the requisition to figure out the right place to
3168 * popup the menu. In fact, we always need to ask here, since
3169 * if a size_request was queued while we weren't popped up,
3170 * the requisition won't have been recomputed yet.
3172 gtk_widget_size_request (widget, &requisition);
3174 if (pointer_screen != screen)
3176 /* Pointer is on a different screen; roughly center the
3177 * menu on the screen. If someone was using multiscreen
3178 * + Xinerama together they'd probably want something
3179 * fancier; but that is likely to be vanishingly rare.
3181 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
3182 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
3185 private = gtk_menu_get_private (menu);
3186 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
3190 if (menu->position_func)
3192 (* menu->position_func) (menu, &x, &y, &push_in, menu->position_func_data);
3193 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3197 gint space_left, space_right, space_above, space_below;
3200 gint xthickness = widget->style->xthickness;
3201 gint ythickness = widget->style->ythickness;
3202 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
3204 /* The placement of popup menus horizontally works like this (with
3205 * RTL in parentheses)
3207 * - If there is enough room to the right (left) of the mouse cursor,
3208 * position the menu there.
3210 * - Otherwise, if if there is enough room to the left (right) of the
3211 * mouse cursor, position the menu there.
3213 * - Otherwise if the menu is smaller than the monitor, position it
3214 * on the side of the mouse cursor that has the most space available
3216 * - Otherwise (if there is simply not enough room for the menu on the
3217 * monitor), position it as far left (right) as possible.
3219 * Positioning in the vertical direction is similar: first try below
3220 * mouse cursor, then above.
3222 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3224 space_left = x - monitor.x;
3225 space_right = monitor.x + monitor.width - x - 1;
3226 space_above = y - monitor.y;
3227 space_below = monitor.y + monitor.height - y - 1;
3229 /* position horizontally */
3231 /* the amount of space we need to position the menu. Note the
3232 * menu is offset "xthickness" pixels
3234 needed_width = requisition.width - xthickness;
3236 if (needed_width <= space_left ||
3237 needed_width <= space_right)
3239 if ((rtl && needed_width <= space_left) ||
3240 (!rtl && needed_width > space_right))
3243 x = x + xthickness - requisition.width + 1;
3247 /* position right */
3251 /* x is clamped on-screen further down */
3253 else if (requisition.width <= monitor.width)
3255 /* the menu is too big to fit on either side of the mouse
3256 * cursor, but smaller than the monitor. Position it on
3257 * the side that has the most space
3259 if (space_left > space_right)
3267 x = monitor.x + monitor.width - requisition.width;
3270 else /* menu is simply too big for the monitor */
3275 x = monitor.x + monitor.width - requisition.width;
3284 /* Position vertically. The algorithm is the same as above, but
3285 * simpler because we don't have to take RTL into account.
3287 needed_height = requisition.height - ythickness;
3289 if (needed_height <= space_above ||
3290 needed_height <= space_below)
3292 if (needed_height <= space_below)
3295 y = y + ythickness - requisition.height + 1;
3297 y = CLAMP (y, monitor.y,
3298 monitor.y + monitor.height - requisition.height);
3300 else if (needed_height > space_below && needed_height > space_above)
3302 if (space_below >= space_above)
3303 y = monitor.y + monitor.height - requisition.height;
3317 menu_height = GTK_WIDGET (menu)->requisition.height;
3319 if (y + menu_height > monitor.y + monitor.height)
3321 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
3322 y = (monitor.y + monitor.height) - menu_height;
3327 scroll_offset += monitor.y - y;
3332 /* FIXME: should this be done in the various position_funcs ? */
3333 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
3335 if (GTK_MENU_SHELL (menu)->active)
3337 private->have_position = TRUE;
3342 if (y + requisition.height > monitor.y + monitor.height)
3343 requisition.height = (monitor.y + monitor.height) - y;
3347 scroll_offset += monitor.y - y;
3348 requisition.height -= monitor.y - y;
3352 if (scroll_offset > 0)
3353 scroll_offset += MENU_SCROLL_ARROW_HEIGHT;
3355 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
3358 if (!GTK_MENU_SHELL (menu)->active)
3360 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
3361 requisition.width, requisition.height);
3364 menu->scroll_offset = scroll_offset;
3368 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
3370 if (menu->timeout_id)
3372 g_source_remove (menu->timeout_id);
3373 menu->timeout_id = 0;
3378 gtk_menu_stop_scrolling (GtkMenu *menu)
3380 gtk_menu_remove_scroll_timeout (menu);
3382 menu->upper_arrow_prelight = FALSE;
3383 menu->lower_arrow_prelight = FALSE;
3387 gtk_menu_scroll_to (GtkMenu *menu,
3392 gint view_width, view_height;
3394 gboolean last_visible;
3396 guint vertical_padding;
3398 widget = GTK_WIDGET (menu);
3400 if (menu->tearoff_active &&
3401 menu->tearoff_adjustment &&
3402 (menu->tearoff_adjustment->value != offset))
3404 menu->tearoff_adjustment->value =
3406 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
3407 gtk_adjustment_value_changed (menu->tearoff_adjustment);
3410 /* Move/resize the viewport according to arrows: */
3411 view_width = widget->allocation.width;
3412 view_height = widget->allocation.height;
3414 gtk_widget_style_get (GTK_WIDGET (menu),
3415 "vertical-padding", &vertical_padding,
3418 border_width = GTK_CONTAINER (menu)->border_width;
3419 view_width -= (border_width + widget->style->xthickness) * 2;
3420 view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
3421 menu_height = widget->requisition.height -
3422 (border_width + widget->style->ythickness + vertical_padding) * 2;
3424 x = border_width + widget->style->xthickness;
3425 y = border_width + widget->style->ythickness + vertical_padding;
3427 if (!menu->tearoff_active)
3429 last_visible = menu->upper_arrow_visible;
3430 menu->upper_arrow_visible = offset > 0;
3432 if (menu->upper_arrow_visible)
3433 view_height -= MENU_SCROLL_ARROW_HEIGHT;
3435 if ( (last_visible != menu->upper_arrow_visible) &&
3436 !menu->upper_arrow_visible)
3438 menu->upper_arrow_prelight = FALSE;
3440 /* If we hid the upper arrow, possibly remove timeout */
3441 if (menu->scroll_step < 0)
3443 gtk_menu_stop_scrolling (menu);
3444 gtk_widget_queue_draw (GTK_WIDGET (menu));
3448 last_visible = menu->lower_arrow_visible;
3449 menu->lower_arrow_visible = offset < menu_height - view_height;
3451 if (menu->lower_arrow_visible)
3452 view_height -= MENU_SCROLL_ARROW_HEIGHT;
3454 if ( (last_visible != menu->lower_arrow_visible) &&
3455 !menu->lower_arrow_visible)
3457 menu->lower_arrow_prelight = FALSE;
3459 /* If we hid the lower arrow, possibly remove timeout */
3460 if (menu->scroll_step > 0)
3462 gtk_menu_stop_scrolling (menu);
3463 gtk_widget_queue_draw (GTK_WIDGET (menu));
3467 if (menu->upper_arrow_visible)
3468 y += MENU_SCROLL_ARROW_HEIGHT;
3471 /* Scroll the menu: */
3472 if (GTK_WIDGET_REALIZED (menu))
3473 gdk_window_move (menu->bin_window, 0, -offset);
3475 if (GTK_WIDGET_REALIZED (menu))
3476 gdk_window_move_resize (menu->view_window,
3482 menu->scroll_offset = offset;
3486 compute_child_offset (GtkMenu *menu,
3487 GtkWidget *menu_item,
3490 gboolean *is_last_child)
3492 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3493 guint item_top_attach;
3494 guint item_bottom_attach;
3495 gint child_offset = 0;
3498 gtk_container_child_get (GTK_CONTAINER (menu), menu_item,
3499 "top_attach", &item_top_attach,
3500 "bottom_attach", &item_bottom_attach,
3503 /* there is a possibility that we get called before _size_request, so
3504 * check the height table for safety.
3506 if (!priv->heights || priv->heights_length < priv->rows)
3509 /* when we have a row with only invisible children, it's height will
3510 * be zero, so there's no need to check WIDGET_VISIBLE here
3512 for (i = 0; i < item_top_attach; i++)
3513 child_offset += priv->heights[i];
3516 *is_last_child = (item_bottom_attach == priv->rows);
3518 *offset = child_offset;
3520 *height = priv->heights[item_top_attach];
3526 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
3527 GtkWidget *menu_item)
3530 gint child_offset, child_height;
3534 gboolean last_child = 0;
3536 menu = GTK_MENU (menu_shell);
3538 /* We need to check if the selected item fully visible.
3539 * If not we need to scroll the menu so that it becomes fully
3543 if (compute_child_offset (menu, menu_item,
3544 &child_offset, &child_height, &last_child))
3546 guint vertical_padding;
3548 y = menu->scroll_offset;
3549 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3551 gtk_widget_style_get (GTK_WIDGET (menu),
3552 "vertical-padding", &vertical_padding,
3555 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
3557 if (child_offset < y)
3559 /* Ignore the enter event we might get if the pointer is on the menu
3561 menu_shell->ignore_enter = TRUE;
3562 gtk_menu_scroll_to (menu, child_offset);
3567 if (menu->upper_arrow_visible && !menu->tearoff_active)
3568 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3569 if (menu->lower_arrow_visible && !menu->tearoff_active)
3570 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3572 if (child_offset + child_height > y + height - arrow_height)
3575 if (!last_child && !menu->tearoff_active)
3576 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3578 y = child_offset + child_height - height + arrow_height;
3579 if ((y > 0) && !menu->tearoff_active)
3581 /* Need upper arrow */
3582 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3583 y = child_offset + child_height - height + arrow_height;
3585 /* Ignore the enter event we might get if the pointer is on the menu
3587 menu_shell->ignore_enter = TRUE;
3588 gtk_menu_scroll_to (menu, y);
3596 gtk_menu_select_item (GtkMenuShell *menu_shell,
3597 GtkWidget *menu_item)
3599 GtkMenu *menu = GTK_MENU (menu_shell);
3601 if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
3602 gtk_menu_scroll_item_visible (menu_shell, menu_item);
3604 GTK_MENU_SHELL_CLASS (parent_class)->select_item (menu_shell, menu_item);
3608 /* Reparent the menu, taking care of the refcounting
3610 * If unrealize is true we force a unrealize while reparenting the parent.
3611 * This can help eliminate flicker in some cases.
3613 * What happens is that when the menu is unrealized and then re-realized,
3614 * the allocations are as follows:
3616 * parent - 1x1 at (0,0)
3617 * child1 - 100x20 at (0,0)
3618 * child2 - 100x20 at (0,20)
3619 * child3 - 100x20 at (0,40)
3621 * That is, the parent is small but the children are full sized. Then,
3622 * when the queued_resize gets processed, the parent gets resized to
3625 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
3626 * contains the following logic:
3628 * - if a move or resize operation on a window would change the clip
3629 * region on the children, then before the window is resized
3630 * the background for children is temporarily set to None, the
3631 * move/resize done, and the background for the children restored.
3633 * So, at the point where the parent is resized to final size, the
3634 * background for the children is temporarily None, and thus they
3635 * are not cleared to the background color and the previous background
3636 * (the image of the menu) is left in place.
3639 gtk_menu_reparent (GtkMenu *menu,
3640 GtkWidget *new_parent,
3643 GtkObject *object = GTK_OBJECT (menu);
3644 GtkWidget *widget = GTK_WIDGET (menu);
3645 gboolean was_floating = GTK_OBJECT_FLOATING (object);
3647 g_object_ref (object);
3648 gtk_object_sink (object);
3652 g_object_ref (object);
3653 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
3654 gtk_container_add (GTK_CONTAINER (new_parent), widget);
3655 g_object_unref (object);
3658 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
3661 GTK_OBJECT_SET_FLAGS (object, GTK_FLOATING);
3663 g_object_unref (object);
3667 gtk_menu_show_all (GtkWidget *widget)
3669 /* Show children, but not self. */
3670 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
3675 gtk_menu_hide_all (GtkWidget *widget)
3677 /* Hide children, but not self. */
3678 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
3682 * gtk_menu_set_screen:
3683 * @menu: a #GtkMenu.
3684 * @screen: a #GdkScreen, or %NULL if the screen should be
3685 * determined by the widget the menu is attached to.
3687 * Sets the #GdkScreen on which the menu will be displayed.
3692 gtk_menu_set_screen (GtkMenu *menu,
3695 g_return_if_fail (GTK_IS_MENU (menu));
3696 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
3698 g_object_set_data (G_OBJECT (menu), "gtk-menu-explicit-screen", screen);
3702 menu_change_screen (menu, screen);
3706 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
3708 attach_widget_screen_changed (attach_widget, NULL, menu);
3714 * @menu: a #GtkMenu.
3715 * @child: a #GtkMenuItem.
3716 * @left_attach: The column number to attach the left side of the item to.
3717 * @right_attach: The column number to attach the right side of the item to.
3718 * @top_attach: The row number to attach the top of the item to.
3719 * @bottom_attach: The row number to attach the bottom of the item to.
3721 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
3722 * an item will occupy is specified by @left_attach, @right_attach,
3723 * @top_attach and @bottom_attach. These each represent the leftmost,
3724 * rightmost, uppermost and lower column and row numbers of the table.
3725 * (Columns and rows are indexed from zero).
3727 * Note that this function is not related to gtk_menu_detach().
3732 gtk_menu_attach (GtkMenu *menu,
3737 guint bottom_attach)
3739 g_return_if_fail (GTK_IS_MENU (menu));
3740 g_return_if_fail (GTK_IS_MENU_ITEM (child));
3741 g_return_if_fail (child->parent == NULL ||
3742 child->parent == GTK_WIDGET (menu));
3743 g_return_if_fail (left_attach < right_attach);
3744 g_return_if_fail (top_attach < bottom_attach);
3748 GTK_MENU_SHELL (menu)->children =
3749 g_list_append (GTK_MENU_SHELL (menu)->children, child);
3751 gtk_widget_set_parent (child, GTK_WIDGET (menu));
3754 gtk_container_child_set (GTK_CONTAINER (menu), child,
3755 "left_attach", left_attach,
3756 "right_attach", right_attach,
3757 "top_attach", top_attach,
3758 "bottom_attach", bottom_attach,
3763 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
3767 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
3768 "gtk-menu-popup-delay", &popup_delay,
3775 find_child_containing (GtkMenuShell *menu_shell,
3783 /* find a child which includes the area given by
3784 * left, right, top, bottom.
3787 for (list = menu_shell->children; list; list = list->next)
3791 if (!_gtk_menu_item_is_selectable (list->data))
3794 get_child_attach (list->data, &l, &r, &t, &b);
3796 if (l <= left && right <= r
3797 && t <= top && bottom <= b)
3798 return GTK_WIDGET (list->data);
3805 gtk_menu_move_current (GtkMenuShell *menu_shell,
3806 GtkMenuDirectionType direction)
3808 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu_shell));
3810 /* use special table menu key bindings */
3811 if (menu_shell->active_menu_item && priv->columns > 1)
3815 gboolean rtl = (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL);
3816 GtkWidget *match = NULL;
3818 get_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
3820 if (direction == GTK_MENU_DIR_NEXT)
3822 for (i = b; i < priv->rows; i++)
3824 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
3832 for (i = 0; i < t; i++)
3834 match = find_child_containing (menu_shell,
3835 l, l + 1, i, i + 1);
3841 else if (direction == GTK_MENU_DIR_PREV)
3843 for (i = t; i > 0; i--)
3845 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
3853 for (i = priv->rows; i > b; i--)
3855 match = find_child_containing (menu_shell,
3856 l, l + 1, i - 1, i);
3862 else if ((!rtl && direction == GTK_MENU_DIR_PARENT)
3863 || (rtl && direction == GTK_MENU_DIR_CHILD))
3865 /* we go one left if possible */
3867 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
3871 GtkWidget *parent = menu_shell->parent_menu_shell;
3874 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
3875 match = menu_shell->active_menu_item;
3878 else if ((!rtl && direction == GTK_MENU_DIR_CHILD)
3879 || (rtl && direction == GTK_MENU_DIR_PARENT))
3881 /* we go one right if possible */
3882 if (r < priv->columns)
3883 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
3887 GtkWidget *parent = menu_shell->parent_menu_shell;
3889 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
3891 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
3892 match = menu_shell->active_menu_item;
3898 gtk_menu_shell_select_item (menu_shell, match);
3903 GTK_MENU_SHELL_CLASS (parent_class)->move_current (menu_shell, direction);
3907 get_visible_size (GtkMenu *menu)
3909 GtkWidget *widget = GTK_WIDGET (menu);
3910 GtkContainer *container = GTK_CONTAINER (menu);
3912 gint menu_height = (widget->allocation.height
3913 - 2 * (container->border_width
3914 + widget->style->ythickness));
3916 if (menu->upper_arrow_visible && !menu->tearoff_active)
3917 menu_height -= MENU_SCROLL_ARROW_HEIGHT;
3918 if (menu->lower_arrow_visible && !menu->tearoff_active)
3919 menu_height -= MENU_SCROLL_ARROW_HEIGHT;
3924 /* Find the sensitive on-screen child containing @y, or if none,
3925 * the nearest selectable onscreen child. (%NULL if none)
3928 child_at (GtkMenu *menu,
3931 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3932 GtkWidget *child = NULL;
3933 gint child_offset = 0;
3936 gint lower, upper; /* Onscreen bounds */
3938 menu_height = get_visible_size (menu);
3939 lower = menu->scroll_offset;
3940 upper = menu->scroll_offset + menu_height;
3942 for (children = menu_shell->children; children; children = children->next)
3944 if (GTK_WIDGET_VISIBLE (children->data))
3946 GtkRequisition child_requisition;
3948 gtk_widget_size_request (children->data, &child_requisition);
3950 if (_gtk_menu_item_is_selectable (children->data) &&
3951 child_offset >= lower &&
3952 child_offset + child_requisition.height <= upper)
3954 child = children->data;
3956 if (child_offset + child_requisition.height > y &&
3957 !GTK_IS_TEAROFF_MENU_ITEM (child))
3961 child_offset += child_requisition.height;
3969 get_menu_height (GtkMenu *menu)
3972 GtkWidget *widget = GTK_WIDGET (menu);
3974 height = widget->requisition.height;
3975 height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
3977 if (menu->upper_arrow_visible && !menu->tearoff_active)
3978 height -= MENU_SCROLL_ARROW_HEIGHT;
3980 if (menu->lower_arrow_visible && !menu->tearoff_active)
3981 height -= MENU_SCROLL_ARROW_HEIGHT;
3987 gtk_menu_real_move_scroll (GtkMenu *menu,
3990 gint page_size = get_visible_size (menu);
3991 gint end_position = get_menu_height (menu);
3992 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3996 case GTK_SCROLL_PAGE_UP:
3997 case GTK_SCROLL_PAGE_DOWN:
4001 gint child_offset = 0;
4002 gboolean old_upper_arrow_visible;
4005 if (type == GTK_SCROLL_PAGE_UP)
4010 if (menu_shell->active_menu_item)
4014 compute_child_offset (menu, menu_shell->active_menu_item,
4015 &child_offset, &child_height, NULL);
4016 child_offset += child_height / 2;
4019 menu_shell->ignore_enter = TRUE;
4020 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4021 old_offset = menu->scroll_offset;
4023 new_offset = menu->scroll_offset + step;
4024 new_offset = CLAMP (new_offset, 0, end_position - page_size);
4026 gtk_menu_scroll_to (menu, new_offset);
4028 if (menu_shell->active_menu_item)
4030 GtkWidget *new_child;
4031 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4033 if (menu->scroll_offset != old_offset)
4034 step = menu->scroll_offset - old_offset;
4036 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * MENU_SCROLL_ARROW_HEIGHT;
4038 new_child = child_at (menu, child_offset + step);
4040 gtk_menu_shell_select_item (menu_shell, new_child);
4044 case GTK_SCROLL_START:
4045 /* Ignore the enter event we might get if the pointer is on the menu
4047 menu_shell->ignore_enter = TRUE;
4048 gtk_menu_scroll_to (menu, 0);
4049 gtk_menu_shell_select_first (menu_shell, TRUE);
4051 case GTK_SCROLL_END:
4052 /* Ignore the enter event we might get if the pointer is on the menu
4054 menu_shell->ignore_enter = TRUE;
4055 gtk_menu_scroll_to (menu, end_position - page_size);
4056 _gtk_menu_shell_select_last (menu_shell, TRUE);
4065 * gtk_menu_set_monitor:
4067 * @monitor_num: the number of the monitor on which the menu should
4070 * Informs GTK+ on which monitor a menu should be popped up.
4071 * See gdk_screen_get_monitor_geometry().
4073 * This function should be called from a #GtkMenuPositionFunc if the
4074 * menu should not appear on the same monitor as the pointer. This
4075 * information can't be reliably inferred from the coordinates returned
4076 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
4077 * may extend beyond the monitor boundaries or even the screen boundaries.
4081 void gtk_menu_set_monitor (GtkMenu *menu,
4084 GtkMenuPrivate *priv;
4085 g_return_if_fail (GTK_IS_MENU (menu));
4087 priv = gtk_menu_get_private (menu);
4089 priv->monitor_num = monitor_num;