1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 #define GTK_MENU_INTERNALS
30 #include <string.h> /* memset */
31 #include "gdk/gdkkeysyms.h"
32 #include "gtkaccellabel.h"
33 #include "gtkaccelmap.h"
34 #include "gtkbindings.h"
37 #include "gtkmarshalers.h"
39 #include "gtkmenuitem.h"
40 #include "gtktearoffmenuitem.h"
41 #include "gtkwindow.h"
43 #include "gtkvscrollbar.h"
44 #include "gtksettings.h"
45 #include "gtkprivate.h"
50 #define MENU_ITEM_CLASS(w) GTK_MENU_ITEM_GET_CLASS (w)
52 #define DEFAULT_POPUP_DELAY 225
53 #define DEFAULT_POPDOWN_DELAY 1000
55 #define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
56 * extends below the submenu
59 #define MENU_SCROLL_STEP1 8
60 #define MENU_SCROLL_STEP2 15
61 #define MENU_SCROLL_ARROW_HEIGHT 16
62 #define MENU_SCROLL_FAST_ZONE 8
63 #define MENU_SCROLL_TIMEOUT1 50
64 #define MENU_SCROLL_TIMEOUT2 20
66 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
67 #define ATTACHED_MENUS "gtk-attached-menus"
69 typedef struct _GtkMenuAttachData GtkMenuAttachData;
70 typedef struct _GtkMenuPrivate GtkMenuPrivate;
72 struct _GtkMenuAttachData
74 GtkWidget *attach_widget;
75 GtkMenuDetachFunc detacher;
78 struct _GtkMenuPrivate
80 gboolean seen_item_enter;
82 gboolean have_position;
86 /* info used for the table */
92 /* Cached layout information */
100 GtkStateType lower_arrow_state;
101 GtkStateType upper_arrow_state;
103 gboolean ignore_button_release;
104 gboolean initially_pushed_in;
113 gint effective_left_attach;
114 gint effective_right_attach;
115 gint effective_top_attach;
116 gint effective_bottom_attach;
132 CHILD_PROP_LEFT_ATTACH,
133 CHILD_PROP_RIGHT_ATTACH,
134 CHILD_PROP_TOP_ATTACH,
135 CHILD_PROP_BOTTOM_ATTACH
138 static void gtk_menu_class_init (GtkMenuClass *klass);
139 static void gtk_menu_init (GtkMenu *menu);
140 static void gtk_menu_set_property (GObject *object,
144 static void gtk_menu_get_property (GObject *object,
148 static void gtk_menu_set_child_property(GtkContainer *container,
153 static void gtk_menu_get_child_property(GtkContainer *container,
158 static void gtk_menu_destroy (GtkObject *object);
159 static void gtk_menu_finalize (GObject *object);
160 static void gtk_menu_realize (GtkWidget *widget);
161 static void gtk_menu_unrealize (GtkWidget *widget);
162 static void gtk_menu_size_request (GtkWidget *widget,
163 GtkRequisition *requisition);
164 static void gtk_menu_size_allocate (GtkWidget *widget,
165 GtkAllocation *allocation);
166 static void gtk_menu_paint (GtkWidget *widget,
167 GdkEventExpose *expose);
168 static void gtk_menu_show (GtkWidget *widget);
169 static gboolean gtk_menu_expose (GtkWidget *widget,
170 GdkEventExpose *event);
171 static gboolean gtk_menu_key_press (GtkWidget *widget,
173 static gboolean gtk_menu_scroll (GtkWidget *widget,
174 GdkEventScroll *event);
175 static gboolean gtk_menu_button_press (GtkWidget *widget,
176 GdkEventButton *event);
177 static gboolean gtk_menu_button_release (GtkWidget *widget,
178 GdkEventButton *event);
179 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
180 GdkEventMotion *event);
181 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
182 GdkEventCrossing *event);
183 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
184 GdkEventCrossing *event);
185 static void gtk_menu_scroll_to (GtkMenu *menu,
187 static void gtk_menu_grab_notify (GtkWidget *widget,
188 gboolean was_grabbed);
190 static void gtk_menu_stop_scrolling (GtkMenu *menu);
191 static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
192 static gboolean gtk_menu_scroll_timeout (gpointer data);
193 static gboolean gtk_menu_scroll_timeout_initial (gpointer data);
194 static void gtk_menu_start_scrolling (GtkMenu *menu);
196 static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
197 GtkWidget *menu_item);
198 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
199 GtkWidget *menu_item);
200 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
203 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
205 static void gtk_menu_handle_scrolling (GtkMenu *menu,
210 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
212 static void gtk_menu_style_set (GtkWidget *widget,
213 GtkStyle *previous_style);
214 static gboolean gtk_menu_focus (GtkWidget *widget,
215 GtkDirectionType direction);
216 static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
217 static void gtk_menu_move_current (GtkMenuShell *menu_shell,
218 GtkMenuDirectionType direction);
219 static void gtk_menu_real_move_scroll (GtkMenu *menu,
222 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
223 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
224 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
227 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
228 GtkMenuItem *menu_item,
229 GdkEventCrossing *event);
231 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
232 static void gtk_menu_show_all (GtkWidget *widget);
233 static void gtk_menu_hide_all (GtkWidget *widget);
234 static void gtk_menu_position (GtkMenu *menu);
235 static void gtk_menu_reparent (GtkMenu *menu,
236 GtkWidget *new_parent,
238 static void gtk_menu_remove (GtkContainer *menu,
241 static void gtk_menu_update_title (GtkMenu *menu);
243 static void menu_grab_transfer_window_destroy (GtkMenu *menu);
244 static GdkWindow *menu_grab_transfer_window_get (GtkMenu *menu);
246 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
248 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
249 gboolean group_changed);
251 static GtkMenuShellClass *parent_class = NULL;
252 static const gchar attach_data_key[] = "gtk-menu-attach-data";
254 static guint menu_signals[LAST_SIGNAL] = { 0 };
256 static GtkMenuPrivate *
257 gtk_menu_get_private (GtkMenu *menu)
259 return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
263 gtk_menu_get_type (void)
265 static GType menu_type = 0;
269 static const GTypeInfo menu_info =
271 sizeof (GtkMenuClass),
272 NULL, /* base_init */
273 NULL, /* base_finalize */
274 (GClassInitFunc) gtk_menu_class_init,
275 NULL, /* class_finalize */
276 NULL, /* class_data */
279 (GInstanceInitFunc) gtk_menu_init,
282 menu_type = g_type_register_static (GTK_TYPE_MENU_SHELL, I_("GtkMenu"),
290 menu_queue_resize (GtkMenu *menu)
292 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
294 priv->have_layout = FALSE;
295 gtk_widget_queue_resize (GTK_WIDGET (menu));
299 get_attach_info (GtkWidget *child)
301 GObject *object = G_OBJECT (child);
302 AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
306 ai = g_new0 (AttachInfo, 1);
307 g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai, g_free);
314 is_grid_attached (AttachInfo *ai)
316 return (ai->left_attach >= 0 &&
317 ai->right_attach >= 0 &&
318 ai->top_attach >= 0 &&
319 ai->bottom_attach >= 0);
323 menu_ensure_layout (GtkMenu *menu)
325 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
327 if (!priv->have_layout)
329 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
333 gint max_right_attach;
334 gint max_bottom_attach;
336 /* Find extents of gridded portion
338 max_right_attach = 1;
339 max_bottom_attach = 0;
341 for (l = menu_shell->children; l; l = l->next)
343 GtkWidget *child = l->data;
344 AttachInfo *ai = get_attach_info (child);
346 if (is_grid_attached (ai))
348 max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
349 max_right_attach = MAX (max_right_attach, ai->right_attach);
355 row_occupied = g_malloc0 (max_bottom_attach);
357 for (l = menu_shell->children; l; l = l->next)
359 GtkWidget *child = l->data;
360 AttachInfo *ai = get_attach_info (child);
362 if (is_grid_attached (ai))
366 for (i = ai->top_attach; i < ai->bottom_attach; i++)
367 row_occupied[i] = TRUE;
371 /* Lay non-grid-items out in those rows
374 for (l = menu_shell->children; l; l = l->next)
376 GtkWidget *child = l->data;
377 AttachInfo *ai = get_attach_info (child);
379 if (!is_grid_attached (ai))
381 while (current_row < max_bottom_attach && row_occupied[current_row])
384 ai->effective_left_attach = 0;
385 ai->effective_right_attach = max_right_attach;
386 ai->effective_top_attach = current_row;
387 ai->effective_bottom_attach = current_row + 1;
393 ai->effective_left_attach = ai->left_attach;
394 ai->effective_right_attach = ai->right_attach;
395 ai->effective_top_attach = ai->top_attach;
396 ai->effective_bottom_attach = ai->bottom_attach;
400 g_free (row_occupied);
402 priv->n_rows = MAX (current_row, max_bottom_attach);
403 priv->n_columns = max_right_attach;
404 priv->have_layout = TRUE;
410 gtk_menu_get_n_columns (GtkMenu *menu)
412 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
414 menu_ensure_layout (menu);
416 return priv->n_columns;
420 gtk_menu_get_n_rows (GtkMenu *menu)
422 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
424 menu_ensure_layout (menu);
430 get_effective_child_attach (GtkWidget *child,
436 GtkMenu *menu = GTK_MENU (child->parent);
439 menu_ensure_layout (menu);
441 ai = get_attach_info (child);
444 *l = ai->effective_left_attach;
446 *r = ai->effective_right_attach;
448 *t = ai->effective_top_attach;
450 *b = ai->effective_bottom_attach;
455 gtk_menu_class_init (GtkMenuClass *class)
457 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
458 GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
459 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
460 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
461 GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
462 GtkBindingSet *binding_set;
464 parent_class = g_type_class_peek_parent (class);
466 gobject_class->finalize = gtk_menu_finalize;
467 gobject_class->set_property = gtk_menu_set_property;
468 gobject_class->get_property = gtk_menu_get_property;
470 object_class->destroy = gtk_menu_destroy;
472 widget_class->realize = gtk_menu_realize;
473 widget_class->unrealize = gtk_menu_unrealize;
474 widget_class->size_request = gtk_menu_size_request;
475 widget_class->size_allocate = gtk_menu_size_allocate;
476 widget_class->show = gtk_menu_show;
477 widget_class->expose_event = gtk_menu_expose;
478 widget_class->scroll_event = gtk_menu_scroll;
479 widget_class->key_press_event = gtk_menu_key_press;
480 widget_class->button_press_event = gtk_menu_button_press;
481 widget_class->button_release_event = gtk_menu_button_release;
482 widget_class->motion_notify_event = gtk_menu_motion_notify;
483 widget_class->show_all = gtk_menu_show_all;
484 widget_class->hide_all = gtk_menu_hide_all;
485 widget_class->enter_notify_event = gtk_menu_enter_notify;
486 widget_class->leave_notify_event = gtk_menu_leave_notify;
487 widget_class->style_set = gtk_menu_style_set;
488 widget_class->focus = gtk_menu_focus;
489 widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
490 widget_class->grab_notify = gtk_menu_grab_notify;
492 container_class->remove = gtk_menu_remove;
493 container_class->get_child_property = gtk_menu_get_child_property;
494 container_class->set_child_property = gtk_menu_set_child_property;
496 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
497 menu_shell_class->deactivate = gtk_menu_deactivate;
498 menu_shell_class->select_item = gtk_menu_select_item;
499 menu_shell_class->insert = gtk_menu_real_insert;
500 menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
501 menu_shell_class->move_current = gtk_menu_move_current;
503 menu_signals[MOVE_SCROLL] =
504 _gtk_binding_signal_new (I_("move_scroll"),
505 G_OBJECT_CLASS_TYPE (object_class),
506 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
507 G_CALLBACK (gtk_menu_real_move_scroll),
509 _gtk_marshal_VOID__ENUM,
511 GTK_TYPE_SCROLL_TYPE);
513 g_object_class_install_property (gobject_class,
515 g_param_spec_string ("tearoff-title",
517 P_("A title that may be displayed by the window manager when this menu is torn-off"),
519 GTK_PARAM_READWRITE));
522 * GtkMenu:tearoff-state:
524 * A boolean that indicates whether the menu is torn-off.
528 g_object_class_install_property (gobject_class,
530 g_param_spec_boolean ("tearoff-state",
532 P_("A boolean that indicates whether the menu is torn-off"),
534 GTK_PARAM_READWRITE));
536 gtk_widget_class_install_style_property (widget_class,
537 g_param_spec_int ("vertical-padding",
538 P_("Vertical Padding"),
539 P_("Extra space at the top and bottom of the menu"),
543 GTK_PARAM_READABLE));
545 gtk_widget_class_install_style_property (widget_class,
546 g_param_spec_int ("horizontal-padding",
547 P_("Horizontal Padding"),
548 P_("Extra space at the left and right edges of the menu"),
552 GTK_PARAM_READABLE));
554 gtk_widget_class_install_style_property (widget_class,
555 g_param_spec_int ("vertical-offset",
556 P_("Vertical Offset"),
557 P_("When the menu is a submenu, position it this number of pixels offset vertically"),
561 GTK_PARAM_READABLE));
563 gtk_widget_class_install_style_property (widget_class,
564 g_param_spec_int ("horizontal-offset",
565 P_("Horizontal Offset"),
566 P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
570 GTK_PARAM_READABLE));
572 gtk_widget_class_install_style_property (widget_class,
573 g_param_spec_boolean ("double-arrows",
575 P_("When scrolling, always show both arrows."),
577 GTK_PARAM_READABLE));
580 gtk_container_class_install_child_property (container_class,
581 CHILD_PROP_LEFT_ATTACH,
582 g_param_spec_int ("left-attach",
584 P_("The column number to attach the left side of the child to"),
586 GTK_PARAM_READWRITE));
588 gtk_container_class_install_child_property (container_class,
589 CHILD_PROP_RIGHT_ATTACH,
590 g_param_spec_int ("right-attach",
592 P_("The column number to attach the right side of the child to"),
594 GTK_PARAM_READWRITE));
596 gtk_container_class_install_child_property (container_class,
597 CHILD_PROP_TOP_ATTACH,
598 g_param_spec_int ("top-attach",
600 P_("The row number to attach the top of the child to"),
602 GTK_PARAM_READWRITE));
604 gtk_container_class_install_child_property (container_class,
605 CHILD_PROP_BOTTOM_ATTACH,
606 g_param_spec_int ("bottom-attach",
608 P_("The row number to attach the bottom of the child to"),
610 GTK_PARAM_READWRITE));
612 binding_set = gtk_binding_set_by_class (class);
613 gtk_binding_entry_add_signal (binding_set,
615 I_("move_current"), 1,
616 GTK_TYPE_MENU_DIRECTION_TYPE,
618 gtk_binding_entry_add_signal (binding_set,
621 GTK_TYPE_MENU_DIRECTION_TYPE,
623 gtk_binding_entry_add_signal (binding_set,
626 GTK_TYPE_MENU_DIRECTION_TYPE,
628 gtk_binding_entry_add_signal (binding_set,
631 GTK_TYPE_MENU_DIRECTION_TYPE,
633 gtk_binding_entry_add_signal (binding_set,
636 GTK_TYPE_MENU_DIRECTION_TYPE,
637 GTK_MENU_DIR_PARENT);
638 gtk_binding_entry_add_signal (binding_set,
641 GTK_TYPE_MENU_DIRECTION_TYPE,
642 GTK_MENU_DIR_PARENT);
643 gtk_binding_entry_add_signal (binding_set,
646 GTK_TYPE_MENU_DIRECTION_TYPE,
648 gtk_binding_entry_add_signal (binding_set,
651 GTK_TYPE_MENU_DIRECTION_TYPE,
653 gtk_binding_entry_add_signal (binding_set,
656 GTK_TYPE_SCROLL_TYPE,
658 gtk_binding_entry_add_signal (binding_set,
661 GTK_TYPE_SCROLL_TYPE,
663 gtk_binding_entry_add_signal (binding_set,
666 GTK_TYPE_SCROLL_TYPE,
668 gtk_binding_entry_add_signal (binding_set,
671 GTK_TYPE_SCROLL_TYPE,
673 gtk_binding_entry_add_signal (binding_set,
676 GTK_TYPE_SCROLL_TYPE,
678 gtk_binding_entry_add_signal (binding_set,
681 GTK_TYPE_SCROLL_TYPE,
683 gtk_binding_entry_add_signal (binding_set,
686 GTK_TYPE_SCROLL_TYPE,
687 GTK_SCROLL_PAGE_DOWN);
688 gtk_binding_entry_add_signal (binding_set,
691 GTK_TYPE_SCROLL_TYPE,
692 GTK_SCROLL_PAGE_DOWN);
694 gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
695 P_("Can change accelerators"),
696 P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
698 GTK_PARAM_READWRITE));
700 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
701 P_("Delay before submenus appear"),
702 P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
706 GTK_PARAM_READWRITE));
708 gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
709 P_("Delay before hiding a submenu"),
710 P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
713 DEFAULT_POPDOWN_DELAY,
714 GTK_PARAM_READWRITE));
716 g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
721 gtk_menu_set_property (GObject *object,
728 menu = GTK_MENU (object);
732 case PROP_TEAROFF_STATE:
733 gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
735 case PROP_TEAROFF_TITLE:
736 gtk_menu_set_title (menu, g_value_get_string (value));
739 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
745 gtk_menu_get_property (GObject *object,
752 menu = GTK_MENU (object);
756 case PROP_TEAROFF_STATE:
757 g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
759 case PROP_TEAROFF_TITLE:
760 g_value_set_string (value, gtk_menu_get_title (menu));
763 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
769 gtk_menu_set_child_property (GtkContainer *container,
775 GtkMenu *menu = GTK_MENU (container);
776 AttachInfo *ai = get_attach_info (child);
780 case CHILD_PROP_LEFT_ATTACH:
781 ai->left_attach = g_value_get_int (value);
783 case CHILD_PROP_RIGHT_ATTACH:
784 ai->right_attach = g_value_get_int (value);
786 case CHILD_PROP_TOP_ATTACH:
787 ai->top_attach = g_value_get_int (value);
789 case CHILD_PROP_BOTTOM_ATTACH:
790 ai->bottom_attach = g_value_get_int (value);
794 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
798 menu_queue_resize (menu);
802 gtk_menu_get_child_property (GtkContainer *container,
808 AttachInfo *ai = get_attach_info (child);
812 case CHILD_PROP_LEFT_ATTACH:
813 g_value_set_int (value, ai->left_attach);
815 case CHILD_PROP_RIGHT_ATTACH:
816 g_value_set_int (value, ai->right_attach);
818 case CHILD_PROP_TOP_ATTACH:
819 g_value_set_int (value, ai->top_attach);
821 case CHILD_PROP_BOTTOM_ATTACH:
822 g_value_set_int (value, ai->bottom_attach);
826 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
832 gtk_menu_window_event (GtkWidget *window,
836 gboolean handled = FALSE;
838 g_object_ref (window);
844 case GDK_KEY_RELEASE:
845 handled = gtk_widget_event (menu, event);
851 g_object_unref (window);
852 g_object_unref (menu);
858 gtk_menu_window_size_request (GtkWidget *window,
859 GtkRequisition *requisition,
862 GtkMenuPrivate *private = gtk_menu_get_private (menu);
864 if (private->have_position)
866 GdkScreen *screen = gtk_widget_get_screen (window);
867 GdkRectangle monitor;
869 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
871 if (private->y + requisition->height > monitor.y + monitor.height)
872 requisition->height = monitor.y + monitor.height - private->y;
874 if (private->y < monitor.y)
875 requisition->height -= monitor.y - private->y;
880 gtk_menu_init (GtkMenu *menu)
882 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
884 menu->parent_menu_item = NULL;
885 menu->old_active_menu_item = NULL;
886 menu->accel_group = NULL;
887 menu->position_func = NULL;
888 menu->position_func_data = NULL;
889 menu->toggle_size = 0;
891 menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
892 "type", GTK_WINDOW_POPUP,
895 "signal::event", gtk_menu_window_event, menu,
896 "signal::size_request", gtk_menu_window_size_request, menu,
897 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
899 gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
900 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
902 /* Refloat the menu, so that reference counting for the menu isn't
903 * affected by it being a child of the toplevel
905 g_object_force_floating (G_OBJECT (menu));
906 menu->needs_destruction_ref_count = TRUE;
908 menu->view_window = NULL;
909 menu->bin_window = NULL;
911 menu->scroll_offset = 0;
912 menu->scroll_step = 0;
913 menu->timeout_id = 0;
914 menu->scroll_fast = FALSE;
916 menu->tearoff_window = NULL;
917 menu->tearoff_hbox = NULL;
918 menu->torn_off = FALSE;
919 menu->tearoff_active = FALSE;
920 menu->tearoff_adjustment = NULL;
921 menu->tearoff_scrollbar = NULL;
923 menu->upper_arrow_visible = FALSE;
924 menu->lower_arrow_visible = FALSE;
925 menu->upper_arrow_prelight = FALSE;
926 menu->lower_arrow_prelight = FALSE;
928 priv->upper_arrow_state = GTK_STATE_NORMAL;
929 priv->lower_arrow_state = GTK_STATE_NORMAL;
931 priv->have_layout = FALSE;
935 gtk_menu_destroy (GtkObject *object)
938 GtkMenuAttachData *data;
939 GtkMenuPrivate *priv;
941 g_return_if_fail (GTK_IS_MENU (object));
943 menu = GTK_MENU (object);
945 gtk_menu_stop_scrolling (menu);
947 data = g_object_get_data (G_OBJECT (object), attach_data_key);
949 gtk_menu_detach (menu);
951 gtk_menu_stop_navigating_submenu (menu);
953 if (menu->old_active_menu_item)
955 g_object_unref (menu->old_active_menu_item);
956 menu->old_active_menu_item = NULL;
959 /* Add back the reference count for being a child */
960 if (menu->needs_destruction_ref_count)
962 menu->needs_destruction_ref_count = FALSE;
963 g_object_ref (object);
966 if (menu->accel_group)
968 g_object_unref (menu->accel_group);
969 menu->accel_group = NULL;
973 gtk_widget_destroy (menu->toplevel);
975 if (menu->tearoff_window)
976 gtk_widget_destroy (menu->tearoff_window);
978 priv = gtk_menu_get_private (menu);
982 g_free (priv->heights);
983 priv->heights = NULL;
988 g_free (priv->title);
992 GTK_OBJECT_CLASS (parent_class)->destroy (object);
996 gtk_menu_finalize (GObject *object)
998 GtkMenu *menu = GTK_MENU (object);
1000 g_free (menu->accel_path);
1002 G_OBJECT_CLASS (parent_class)->finalize (object);
1006 menu_change_screen (GtkMenu *menu,
1007 GdkScreen *new_screen)
1009 GtkMenuPrivate *private = gtk_menu_get_private (menu);
1011 if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1013 if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1019 gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1020 gtk_menu_position (menu);
1023 gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1024 private->monitor_num = -1;
1028 attach_widget_screen_changed (GtkWidget *attach_widget,
1029 GdkScreen *previous_screen,
1032 if (gtk_widget_has_screen (attach_widget) &&
1033 !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1035 menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1040 gtk_menu_attach_to_widget (GtkMenu *menu,
1041 GtkWidget *attach_widget,
1042 GtkMenuDetachFunc detacher)
1044 GtkMenuAttachData *data;
1047 g_return_if_fail (GTK_IS_MENU (menu));
1048 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1050 /* keep this function in sync with gtk_widget_set_parent()
1053 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1056 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1057 g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1061 g_object_ref_sink (menu);
1063 data = g_new (GtkMenuAttachData, 1);
1064 data->attach_widget = attach_widget;
1066 g_signal_connect (attach_widget, "screen_changed",
1067 G_CALLBACK (attach_widget_screen_changed), menu);
1068 attach_widget_screen_changed (attach_widget, NULL, menu);
1070 data->detacher = detacher;
1071 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1072 list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1073 if (!g_list_find (list, menu))
1075 list = g_list_prepend (list, menu);
1077 g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list, (GtkDestroyNotify) g_list_free);
1079 if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
1080 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1082 /* we don't need to set the style here, since
1083 * we are a toplevel widget.
1086 /* Fallback title for menu comes from attach widget */
1087 gtk_menu_update_title (menu);
1091 gtk_menu_get_attach_widget (GtkMenu *menu)
1093 GtkMenuAttachData *data;
1095 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1097 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1099 return data->attach_widget;
1104 gtk_menu_detach (GtkMenu *menu)
1106 GtkMenuAttachData *data;
1109 g_return_if_fail (GTK_IS_MENU (menu));
1111 /* keep this function in sync with gtk_widget_unparent()
1113 data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1116 g_warning ("gtk_menu_detach(): menu is not attached");
1119 g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1121 g_signal_handlers_disconnect_by_func (data->attach_widget,
1122 (gpointer) attach_widget_screen_changed,
1126 data->detacher (data->attach_widget, menu);
1127 list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1128 list = g_list_remove (list, menu);
1130 g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list, (GtkDestroyNotify) g_list_free);
1132 g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1134 if (GTK_WIDGET_REALIZED (menu))
1135 gtk_widget_unrealize (GTK_WIDGET (menu));
1139 /* Fallback title for menu comes from attach widget */
1140 gtk_menu_update_title (menu);
1142 g_object_unref (menu);
1146 gtk_menu_remove (GtkContainer *container,
1151 g_return_if_fail (GTK_IS_MENU (container));
1152 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1154 menu = GTK_MENU (container);
1156 /* Clear out old_active_menu_item if it matches the item we are removing
1158 if (menu->old_active_menu_item == widget)
1160 g_object_unref (menu->old_active_menu_item);
1161 menu->old_active_menu_item = NULL;
1164 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
1165 g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1167 menu_queue_resize (menu);
1173 return g_object_new (GTK_TYPE_MENU, NULL);
1177 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1181 GtkMenu *menu = GTK_MENU (menu_shell);
1182 AttachInfo *ai = get_attach_info (child);
1184 ai->left_attach = -1;
1185 ai->right_attach = -1;
1186 ai->top_attach = -1;
1187 ai->bottom_attach = -1;
1189 if (GTK_WIDGET_REALIZED (menu_shell))
1190 gtk_widget_set_parent_window (child, menu->bin_window);
1192 GTK_MENU_SHELL_CLASS (parent_class)->insert (menu_shell, child, position);
1194 menu_queue_resize (menu);
1198 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1203 widget = GTK_WIDGET (menu);
1209 GdkGCValues gc_values;
1211 menu->tearoff_active = FALSE;
1212 menu->saved_scroll_offset = menu->scroll_offset;
1214 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
1215 gc = gdk_gc_new_with_values (widget->window,
1216 &gc_values, GDK_GC_SUBWINDOW);
1218 gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1220 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
1225 gdk_draw_drawable (pixmap, gc,
1226 menu->tearoff_window->window,
1227 0, 0, 0, 0, -1, -1);
1228 g_object_unref (gc);
1230 gtk_widget_set_size_request (menu->tearoff_window,
1234 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
1235 g_object_unref (pixmap);
1240 popup_grab_on_window (GdkWindow *window,
1241 guint32 activate_time,
1242 gboolean grab_keyboard)
1244 if ((gdk_pointer_grab (window, TRUE,
1245 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1246 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1247 GDK_POINTER_MOTION_MASK,
1248 NULL, NULL, activate_time) == 0))
1250 if (!grab_keyboard ||
1251 gdk_keyboard_grab (window, TRUE,
1252 activate_time) == 0)
1256 gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1267 * @menu: a #GtkMenu.
1268 * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
1269 * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
1270 * @func: a user supplied function used to position the menu, or %NULL
1271 * @data: user supplied data to be passed to @func.
1272 * @button: the mouse button which was pressed to initiate the event.
1273 * @activate_time: the time at which the activation event occurred.
1275 * Displays a menu and makes it available for selection. Applications can use
1276 * this function to display context-sensitive menus, and will typically supply
1277 * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1278 * parameters. The default menu positioning function will position the menu
1279 * at the current mouse cursor position.
1281 * The @button parameter should be the mouse button pressed to initiate
1282 * the menu popup. If the menu popup was initiated by something other than
1283 * a mouse button press, such as a mouse button release or a keypress,
1284 * @button should be 0.
1286 * The @activate_time parameter should be the time stamp of the event that
1287 * initiated the popup. If such an event is not available, use
1288 * gtk_get_current_event_time() instead.
1292 gtk_menu_popup (GtkMenu *menu,
1293 GtkWidget *parent_menu_shell,
1294 GtkWidget *parent_menu_item,
1295 GtkMenuPositionFunc func,
1298 guint32 activate_time)
1301 GtkWidget *xgrab_shell;
1303 GdkEvent *current_event;
1304 GtkMenuShell *menu_shell;
1305 gboolean grab_keyboard;
1306 GtkMenuPrivate *priv;
1308 g_return_if_fail (GTK_IS_MENU (menu));
1310 widget = GTK_WIDGET (menu);
1311 menu_shell = GTK_MENU_SHELL (menu);
1312 priv = gtk_menu_get_private (menu);
1314 menu_shell->parent_menu_shell = parent_menu_shell;
1316 priv->seen_item_enter = FALSE;
1318 /* Find the last viewable ancestor, and make an X grab on it
1320 parent = GTK_WIDGET (menu);
1324 gboolean viewable = TRUE;
1325 GtkWidget *tmp = parent;
1329 if (!GTK_WIDGET_MAPPED (tmp))
1338 xgrab_shell = parent;
1340 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1343 /* We want to receive events generated when we map the menu; unfortunately,
1344 * since there is probably already an implicit grab in place from the
1345 * button that the user used to pop up the menu, we won't receive then --
1346 * in particular, the EnterNotify when the menu pops up under the pointer.
1348 * If we are grabbing on a parent menu shell, no problem; just grab on
1349 * that menu shell first before popping up the window with owner_events = TRUE.
1351 * When grabbing on the menu itself, things get more convuluted - we
1352 * we do an explicit grab on a specially created window with
1353 * owner_events = TRUE, which we override further down with a grab
1354 * on the menu. (We can't grab on the menu until it is mapped; we
1355 * probably could just leave the grab on the other window, with a
1356 * little reorganization of the code in gtkmenu*).
1358 grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1359 gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1361 if (xgrab_shell && xgrab_shell != widget)
1363 if (popup_grab_on_window (xgrab_shell->window, activate_time, grab_keyboard))
1364 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1368 GdkWindow *transfer_window;
1370 xgrab_shell = widget;
1371 transfer_window = menu_grab_transfer_window_get (menu);
1372 if (popup_grab_on_window (transfer_window, activate_time, grab_keyboard))
1373 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1376 if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1378 /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1379 * with a stuck up window, we just abort here. Presumably the user will
1382 menu_shell->parent_menu_shell = NULL;
1383 menu_grab_transfer_window_destroy (menu);
1387 menu_shell->active = TRUE;
1388 menu_shell->button = button;
1390 /* If we are popping up the menu from something other than, a button
1391 * press then, as a heuristic, we ignore enter events for the menu
1392 * until we get a MOTION_NOTIFY.
1395 current_event = gtk_get_current_event ();
1398 if ((current_event->type != GDK_BUTTON_PRESS) &&
1399 (current_event->type != GDK_ENTER_NOTIFY))
1400 menu_shell->ignore_enter = TRUE;
1402 gdk_event_free (current_event);
1405 menu_shell->ignore_enter = TRUE;
1409 gtk_menu_tearoff_bg_copy (menu);
1411 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1414 if (parent_menu_shell)
1416 GtkWidget *toplevel;
1418 toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1419 if (GTK_IS_WINDOW (toplevel))
1420 gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)),
1421 GTK_WINDOW (menu->toplevel));
1424 menu->parent_menu_item = parent_menu_item;
1425 menu->position_func = func;
1426 menu->position_func_data = data;
1427 menu_shell->activate_time = activate_time;
1429 /* We need to show the menu here rather in the init function because
1430 * code expects to be able to tell if the menu is onscreen by
1431 * looking at the GTK_WIDGET_VISIBLE (menu)
1433 gtk_widget_show (GTK_WIDGET (menu));
1435 /* Position the menu, possibly changing the size request
1437 gtk_menu_position (menu);
1439 /* Compute the size of the toplevel and realize it so we
1440 * can scroll correctly.
1443 GtkRequisition tmp_request;
1444 GtkAllocation tmp_allocation = { 0, };
1446 gtk_widget_size_request (menu->toplevel, &tmp_request);
1448 tmp_allocation.width = tmp_request.width;
1449 tmp_allocation.height = tmp_request.height;
1451 gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1453 gtk_widget_realize (GTK_WIDGET (menu));
1456 gtk_menu_scroll_to (menu, menu->scroll_offset);
1458 /* Once everything is set up correctly, map the toplevel window on
1461 gtk_widget_show (menu->toplevel);
1463 if (xgrab_shell == widget)
1464 popup_grab_on_window (widget->window, activate_time, grab_keyboard); /* Should always succeed */
1465 gtk_grab_add (GTK_WIDGET (menu));
1469 gtk_menu_popdown (GtkMenu *menu)
1471 GtkMenuPrivate *private;
1472 GtkMenuShell *menu_shell;
1474 g_return_if_fail (GTK_IS_MENU (menu));
1476 menu_shell = GTK_MENU_SHELL (menu);
1477 private = gtk_menu_get_private (menu);
1479 menu_shell->parent_menu_shell = NULL;
1480 menu_shell->active = FALSE;
1481 menu_shell->ignore_enter = FALSE;
1483 private->have_position = FALSE;
1485 gtk_menu_stop_scrolling (menu);
1487 gtk_menu_stop_navigating_submenu (menu);
1489 if (menu_shell->active_menu_item)
1491 if (menu->old_active_menu_item)
1492 g_object_unref (menu->old_active_menu_item);
1493 menu->old_active_menu_item = menu_shell->active_menu_item;
1494 g_object_ref (menu->old_active_menu_item);
1497 gtk_menu_shell_deselect (menu_shell);
1499 /* The X Grab, if present, will automatically be removed when we hide
1501 gtk_widget_hide (menu->toplevel);
1502 gtk_window_group_add_window (gtk_window_get_group (NULL), GTK_WINDOW (menu->toplevel));
1506 gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1508 if (GTK_BIN (menu->toplevel)->child)
1510 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1514 /* We popped up the menu from the tearoff, so we need to
1515 * release the grab - we aren't actually hiding the menu.
1517 if (menu_shell->have_xgrab)
1519 GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
1521 gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1522 gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1526 /* gtk_menu_popdown is called each time a menu item is selected from
1527 * a torn off menu. Only scroll back to the saved position if the
1528 * non-tearoff menu was popped down.
1530 if (!menu->tearoff_active)
1531 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1532 menu->tearoff_active = TRUE;
1535 gtk_widget_hide (GTK_WIDGET (menu));
1537 menu_shell->have_xgrab = FALSE;
1538 gtk_grab_remove (GTK_WIDGET (menu));
1540 menu_grab_transfer_window_destroy (menu);
1544 gtk_menu_get_active (GtkMenu *menu)
1549 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1551 if (!menu->old_active_menu_item)
1554 children = GTK_MENU_SHELL (menu)->children;
1558 child = children->data;
1559 children = children->next;
1561 if (GTK_BIN (child)->child)
1566 menu->old_active_menu_item = child;
1567 if (menu->old_active_menu_item)
1568 g_object_ref (menu->old_active_menu_item);
1571 return menu->old_active_menu_item;
1575 gtk_menu_set_active (GtkMenu *menu,
1581 g_return_if_fail (GTK_IS_MENU (menu));
1583 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1586 child = tmp_list->data;
1587 if (GTK_BIN (child)->child)
1589 if (menu->old_active_menu_item)
1590 g_object_unref (menu->old_active_menu_item);
1591 menu->old_active_menu_item = child;
1592 g_object_ref (menu->old_active_menu_item);
1598 gtk_menu_set_accel_group (GtkMenu *menu,
1599 GtkAccelGroup *accel_group)
1601 g_return_if_fail (GTK_IS_MENU (menu));
1603 if (menu->accel_group != accel_group)
1605 if (menu->accel_group)
1606 g_object_unref (menu->accel_group);
1607 menu->accel_group = accel_group;
1608 if (menu->accel_group)
1609 g_object_ref (menu->accel_group);
1610 _gtk_menu_refresh_accel_paths (menu, TRUE);
1615 gtk_menu_get_accel_group (GtkMenu *menu)
1617 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1619 return menu->accel_group;
1623 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1626 /* Menu items chain here to figure whether they can activate their
1627 * accelerators. Unlike ordinary widgets, menus allow accel
1628 * activation even if invisible since that's the usual case for
1629 * submenus/popup-menus. however, the state of the attach widget
1630 * affects the "activeness" of the menu.
1632 GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1635 return gtk_widget_can_activate_accel (awidget, signal_id);
1637 return GTK_WIDGET_IS_SENSITIVE (widget);
1641 * gtk_menu_set_accel_path
1642 * @menu: a valid #GtkMenu
1643 * @accel_path: a valid accelerator path
1645 * Sets an accelerator path for this menu from which accelerator paths
1646 * for its immediate children, its menu items, can be constructed.
1647 * The main purpose of this function is to spare the programmer the
1648 * inconvenience of having to call gtk_menu_item_set_accel_path() on
1649 * each menu item that should support runtime user changable accelerators.
1650 * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1651 * each menu item of this menu, that contains a label describing its purpose,
1652 * automatically gets an accel path assigned. For example, a menu containing
1653 * menu items "New" and "Exit", will, after
1654 * <literal>gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");</literal>
1655 * has been called, assign its items the accel paths:
1656 * <literal>"<Gnumeric-Sheet>/File/New"</literal> and <literal>"<Gnumeric-Sheet>/File/Exit"</literal>.
1657 * Assigning accel paths to menu items then enables the user to change
1658 * their accelerators at runtime. More details about accelerator paths
1659 * and their default setups can be found at gtk_accel_map_add_entry().
1662 gtk_menu_set_accel_path (GtkMenu *menu,
1663 const gchar *accel_path)
1665 g_return_if_fail (GTK_IS_MENU (menu));
1667 g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1669 g_free (menu->accel_path);
1670 menu->accel_path = g_strdup (accel_path);
1671 if (menu->accel_path)
1672 _gtk_menu_refresh_accel_paths (menu, FALSE);
1677 gboolean group_changed;
1681 refresh_accel_paths_foreach (GtkWidget *widget,
1684 AccelPropagation *prop = data;
1686 if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
1687 _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1688 prop->menu->accel_path,
1689 prop->menu->accel_group,
1690 prop->group_changed);
1694 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1695 gboolean group_changed)
1697 g_return_if_fail (GTK_IS_MENU (menu));
1699 if (menu->accel_path && menu->accel_group)
1701 AccelPropagation prop;
1704 prop.group_changed = group_changed;
1705 gtk_container_foreach (GTK_CONTAINER (menu),
1706 refresh_accel_paths_foreach,
1712 gtk_menu_reposition (GtkMenu *menu)
1714 g_return_if_fail (GTK_IS_MENU (menu));
1716 if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1717 gtk_menu_position (menu);
1721 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1724 g_return_if_fail (GTK_IS_MENU (menu));
1726 if (adjustment->value != menu->scroll_offset)
1727 gtk_menu_scroll_to (menu, adjustment->value);
1731 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1734 GdkGeometry geometry_hints;
1736 if (!menu->tearoff_window)
1739 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1741 gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1742 width += menu->tearoff_scrollbar->requisition.width;
1745 geometry_hints.min_width = width;
1746 geometry_hints.max_width = width;
1748 geometry_hints.min_height = 0;
1749 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1751 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1754 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1758 gtk_menu_update_title (GtkMenu *menu)
1760 if (menu->tearoff_window)
1763 GtkWidget *attach_widget;
1765 title = gtk_menu_get_title (menu);
1768 attach_widget = gtk_menu_get_attach_widget (menu);
1769 if (GTK_IS_MENU_ITEM (attach_widget))
1771 GtkWidget *child = GTK_BIN (attach_widget)->child;
1772 if (GTK_IS_LABEL (child))
1773 title = gtk_label_get_text (GTK_LABEL (child));
1778 gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1783 gtk_menu_get_toplevel (GtkWidget *menu)
1785 GtkWidget *attach, *toplevel;
1787 attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
1789 if (GTK_IS_MENU_ITEM (attach))
1790 attach = attach->parent;
1792 if (GTK_IS_MENU (attach))
1793 return gtk_menu_get_toplevel (attach);
1794 else if (GTK_IS_WIDGET (attach))
1796 toplevel = gtk_widget_get_toplevel (attach);
1797 if (GTK_WIDGET_TOPLEVEL (toplevel))
1805 tearoff_window_destroyed (GtkWidget *widget,
1808 gtk_menu_set_tearoff_state (menu, FALSE);
1812 gtk_menu_set_tearoff_state (GtkMenu *menu,
1817 g_return_if_fail (GTK_IS_MENU (menu));
1819 if (menu->torn_off != torn_off)
1821 menu->torn_off = torn_off;
1822 menu->tearoff_active = torn_off;
1826 if (GTK_WIDGET_VISIBLE (menu))
1827 gtk_menu_popdown (menu);
1829 if (!menu->tearoff_window)
1831 GtkWidget *toplevel;
1833 menu->tearoff_window = gtk_widget_new (GTK_TYPE_WINDOW,
1834 "type", GTK_WINDOW_TOPLEVEL,
1835 "screen", gtk_widget_get_screen (menu->toplevel),
1836 "app-paintable", TRUE,
1840 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1841 GDK_WINDOW_TYPE_HINT_MENU);
1842 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1843 g_signal_connect (menu->tearoff_window, "destroy",
1844 G_CALLBACK (tearoff_window_destroyed), menu);
1845 g_signal_connect (menu->tearoff_window, "event",
1846 G_CALLBACK (gtk_menu_window_event), menu);
1848 gtk_menu_update_title (menu);
1850 gtk_widget_realize (menu->tearoff_window);
1852 toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
1853 if (toplevel != NULL)
1854 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
1855 GTK_WINDOW (toplevel));
1857 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1858 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1860 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
1861 menu->tearoff_adjustment =
1862 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1864 GTK_WIDGET (menu)->requisition.height,
1868 g_object_connect (menu->tearoff_adjustment,
1869 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1871 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1873 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1874 menu->tearoff_scrollbar,
1877 if (menu->tearoff_adjustment->upper > height)
1878 gtk_widget_show (menu->tearoff_scrollbar);
1880 gtk_widget_show (menu->tearoff_hbox);
1883 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
1885 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
1887 /* Update menu->requisition
1889 gtk_widget_size_request (GTK_WIDGET (menu), NULL);
1891 gtk_menu_set_tearoff_hints (menu, width);
1893 gtk_widget_realize (menu->tearoff_window);
1894 gtk_menu_position (menu);
1896 gtk_widget_show (GTK_WIDGET (menu));
1897 gtk_widget_show (menu->tearoff_window);
1899 gtk_menu_scroll_to (menu, 0);
1904 gtk_widget_hide (GTK_WIDGET (menu));
1905 gtk_widget_hide (menu->tearoff_window);
1906 if (GTK_IS_CONTAINER (menu->toplevel))
1907 gtk_menu_reparent (menu, menu->toplevel, FALSE);
1908 gtk_widget_destroy (menu->tearoff_window);
1910 menu->tearoff_window = NULL;
1911 menu->tearoff_hbox = NULL;
1912 menu->tearoff_scrollbar = NULL;
1913 menu->tearoff_adjustment = NULL;
1916 g_object_notify (G_OBJECT (menu), "tearoff-state");
1921 * gtk_menu_get_tearoff_state:
1924 * Returns whether the menu is torn off. See
1925 * gtk_menu_set_tearoff_state ().
1927 * Return value: %TRUE if the menu is currently torn off.
1930 gtk_menu_get_tearoff_state (GtkMenu *menu)
1932 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
1934 return menu->torn_off;
1938 * gtk_menu_set_title:
1940 * @title: a string containing the title for the menu.
1942 * Sets the title string for the menu. The title is displayed when the menu
1943 * is shown as a tearoff menu.
1946 gtk_menu_set_title (GtkMenu *menu,
1949 GtkMenuPrivate *priv;
1951 g_return_if_fail (GTK_IS_MENU (menu));
1953 priv = gtk_menu_get_private (menu);
1955 if (strcmp (title ? title : "", priv->title ? priv->title : "") != 0)
1957 g_free (priv->title);
1958 priv->title = g_strdup (title);
1960 gtk_menu_update_title (menu);
1961 g_object_notify (G_OBJECT (menu), "tearoff-title");
1966 * gtk_menu_get_title:
1969 * Returns the title of the menu. See gtk_menu_set_title().
1971 * Return value: the title of the menu, or %NULL if the menu has no
1972 * title set on it. This string is owned by the widget and should
1973 * not be modified or freed.
1975 G_CONST_RETURN gchar *
1976 gtk_menu_get_title (GtkMenu *menu)
1978 GtkMenuPrivate *priv;
1980 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1982 priv = gtk_menu_get_private (menu);
1988 gtk_menu_reorder_child (GtkMenu *menu,
1992 GtkMenuShell *menu_shell;
1994 g_return_if_fail (GTK_IS_MENU (menu));
1995 g_return_if_fail (GTK_IS_MENU_ITEM (child));
1997 menu_shell = GTK_MENU_SHELL (menu);
1999 if (g_list_find (menu_shell->children, child))
2001 menu_shell->children = g_list_remove (menu_shell->children, child);
2002 menu_shell->children = g_list_insert (menu_shell->children, child, position);
2004 menu_queue_resize (menu);
2009 gtk_menu_style_set (GtkWidget *widget,
2010 GtkStyle *previous_style)
2012 if (GTK_WIDGET_REALIZED (widget))
2014 GtkMenu *menu = GTK_MENU (widget);
2016 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2017 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2018 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2023 gtk_menu_realize (GtkWidget *widget)
2025 GdkWindowAttr attributes;
2026 gint attributes_mask;
2031 guint vertical_padding;
2032 guint horizontal_padding;
2034 g_return_if_fail (GTK_IS_MENU (widget));
2036 menu = GTK_MENU (widget);
2038 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
2040 attributes.window_type = GDK_WINDOW_CHILD;
2041 attributes.x = widget->allocation.x;
2042 attributes.y = widget->allocation.y;
2043 attributes.width = widget->allocation.width;
2044 attributes.height = widget->allocation.height;
2045 attributes.wclass = GDK_INPUT_OUTPUT;
2046 attributes.visual = gtk_widget_get_visual (widget);
2047 attributes.colormap = gtk_widget_get_colormap (widget);
2049 attributes.event_mask = gtk_widget_get_events (widget);
2051 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2052 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2054 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2055 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
2056 gdk_window_set_user_data (widget->window, widget);
2058 border_width = GTK_CONTAINER (widget)->border_width;
2060 gtk_widget_style_get (GTK_WIDGET (menu),
2061 "vertical-padding", &vertical_padding,
2062 "horizontal-padding", &horizontal_padding,
2065 attributes.x = border_width + widget->style->xthickness + horizontal_padding;
2066 attributes.y = border_width + widget->style->ythickness + vertical_padding;
2067 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
2068 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
2070 if (menu->upper_arrow_visible)
2072 attributes.y += MENU_SCROLL_ARROW_HEIGHT;
2073 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
2076 if (menu->lower_arrow_visible)
2077 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
2079 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
2080 gdk_window_set_user_data (menu->view_window, menu);
2084 attributes.width = MAX (1, widget->allocation.width - (border_width + widget->style->xthickness + horizontal_padding) * 2);
2085 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
2087 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
2088 gdk_window_set_user_data (menu->bin_window, menu);
2090 children = GTK_MENU_SHELL (menu)->children;
2093 child = children->data;
2094 children = children->next;
2096 gtk_widget_set_parent_window (child, menu->bin_window);
2099 widget->style = gtk_style_attach (widget->style, widget->window);
2100 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2101 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2102 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2104 if (GTK_MENU_SHELL (widget)->active_menu_item)
2105 gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2106 GTK_MENU_SHELL (widget)->active_menu_item);
2108 gdk_window_show (menu->bin_window);
2109 gdk_window_show (menu->view_window);
2113 gtk_menu_focus (GtkWidget *widget,
2114 GtkDirectionType direction)
2117 * A menu or its menu items cannot have focus
2122 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2125 menu_grab_transfer_window_get (GtkMenu *menu)
2127 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2130 GdkWindowAttr attributes;
2131 gint attributes_mask;
2133 attributes.x = -100;
2134 attributes.y = -100;
2135 attributes.width = 10;
2136 attributes.height = 10;
2137 attributes.window_type = GDK_WINDOW_TEMP;
2138 attributes.wclass = GDK_INPUT_ONLY;
2139 attributes.override_redirect = TRUE;
2140 attributes.event_mask = 0;
2142 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2144 window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2145 &attributes, attributes_mask);
2146 gdk_window_set_user_data (window, menu);
2148 gdk_window_show (window);
2150 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2157 menu_grab_transfer_window_destroy (GtkMenu *menu)
2159 GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2162 gdk_window_set_user_data (window, NULL);
2163 gdk_window_destroy (window);
2164 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2169 gtk_menu_unrealize (GtkWidget *widget)
2173 g_return_if_fail (GTK_IS_MENU (widget));
2175 menu = GTK_MENU (widget);
2177 menu_grab_transfer_window_destroy (menu);
2179 gdk_window_set_user_data (menu->view_window, NULL);
2180 gdk_window_destroy (menu->view_window);
2181 menu->view_window = NULL;
2183 gdk_window_set_user_data (menu->bin_window, NULL);
2184 gdk_window_destroy (menu->bin_window);
2185 menu->bin_window = NULL;
2187 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
2191 gtk_menu_size_request (GtkWidget *widget,
2192 GtkRequisition *requisition)
2196 GtkMenuShell *menu_shell;
2199 guint max_toggle_size;
2200 guint max_accel_width;
2201 guint vertical_padding;
2202 guint horizontal_padding;
2203 GtkRequisition child_requisition;
2204 GtkMenuPrivate *priv;
2206 g_return_if_fail (GTK_IS_MENU (widget));
2207 g_return_if_fail (requisition != NULL);
2209 menu = GTK_MENU (widget);
2210 menu_shell = GTK_MENU_SHELL (widget);
2211 priv = gtk_menu_get_private (menu);
2213 requisition->width = 0;
2214 requisition->height = 0;
2216 max_toggle_size = 0;
2217 max_accel_width = 0;
2219 g_free (priv->heights);
2220 priv->heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2221 priv->heights_length = gtk_menu_get_n_rows (menu);
2223 children = menu_shell->children;
2230 child = children->data;
2231 children = children->next;
2233 if (! GTK_WIDGET_VISIBLE (child))
2236 get_effective_child_attach (child, &l, &r, &t, &b);
2238 /* It's important to size_request the child
2239 * before doing the toggle size request, in
2240 * case the toggle size request depends on the size
2241 * request of a child of the child (e.g. for ImageMenuItem)
2244 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
2245 gtk_widget_size_request (child, &child_requisition);
2247 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2248 max_toggle_size = MAX (max_toggle_size, toggle_size);
2249 max_accel_width = MAX (max_accel_width,
2250 GTK_MENU_ITEM (child)->accelerator_width);
2252 part = child_requisition.width / (r - l);
2253 requisition->width = MAX (requisition->width, part);
2255 part = MAX (child_requisition.height, toggle_size) / (b - t);
2256 priv->heights[t] = MAX (priv->heights[t], part);
2259 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2260 requisition->height += priv->heights[i];
2262 requisition->width += max_toggle_size + max_accel_width;
2263 requisition->width *= gtk_menu_get_n_columns (menu);
2265 gtk_widget_style_get (GTK_WIDGET (menu),
2266 "vertical-padding", &vertical_padding,
2267 "horizontal-padding", &horizontal_padding,
2270 requisition->width += (GTK_CONTAINER (menu)->border_width + horizontal_padding +
2271 widget->style->xthickness) * 2;
2272 requisition->height += (GTK_CONTAINER (menu)->border_width + vertical_padding +
2273 widget->style->ythickness) * 2;
2275 menu->toggle_size = max_toggle_size;
2277 /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2279 if (menu->tearoff_active)
2280 gtk_menu_set_tearoff_hints (menu, requisition->width);
2284 gtk_menu_size_allocate (GtkWidget *widget,
2285 GtkAllocation *allocation)
2288 GtkMenuShell *menu_shell;
2290 GtkAllocation child_allocation;
2291 GtkRequisition child_requisition;
2292 GtkMenuPrivate *priv;
2296 guint vertical_padding;
2297 guint horizontal_padding;
2299 g_return_if_fail (GTK_IS_MENU (widget));
2300 g_return_if_fail (allocation != NULL);
2302 menu = GTK_MENU (widget);
2303 menu_shell = GTK_MENU_SHELL (widget);
2304 priv = gtk_menu_get_private (menu);
2306 widget->allocation = *allocation;
2307 gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2309 gtk_widget_style_get (GTK_WIDGET (menu),
2310 "vertical-padding", &vertical_padding,
2311 "horizontal-padding", &horizontal_padding,
2314 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness + horizontal_padding;
2315 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness + vertical_padding;
2317 width = MAX (1, allocation->width - x * 2);
2318 height = MAX (1, allocation->height - y * 2);
2320 child_requisition.width -= x * 2;
2321 child_requisition.height -= y * 2;
2323 if (menu_shell->active)
2324 gtk_menu_scroll_to (menu, menu->scroll_offset);
2326 if (menu->upper_arrow_visible && !menu->tearoff_active)
2328 y += MENU_SCROLL_ARROW_HEIGHT;
2329 height -= MENU_SCROLL_ARROW_HEIGHT;
2332 if (menu->lower_arrow_visible && !menu->tearoff_active)
2333 height -= MENU_SCROLL_ARROW_HEIGHT;
2335 if (GTK_WIDGET_REALIZED (widget))
2337 gdk_window_move_resize (widget->window,
2338 allocation->x, allocation->y,
2339 allocation->width, allocation->height);
2341 gdk_window_move_resize (menu->view_window,
2348 if (menu_shell->children)
2350 gint base_width = width / gtk_menu_get_n_columns (menu);
2352 children = menu_shell->children;
2355 child = children->data;
2356 children = children->next;
2358 if (GTK_WIDGET_VISIBLE (child))
2363 get_effective_child_attach (child, &l, &r, &t, &b);
2365 if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2368 tmp = gtk_menu_get_n_columns (menu) - l;
2369 l = gtk_menu_get_n_columns (menu) - r;
2373 child_allocation.width = (r - l) * base_width;
2374 child_allocation.height = 0;
2375 child_allocation.x = l * base_width;
2376 child_allocation.y = 0;
2378 for (i = 0; i < b; i++)
2381 child_allocation.y += priv->heights[i];
2383 child_allocation.height += priv->heights[i];
2386 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2389 gtk_widget_size_allocate (child, &child_allocation);
2390 gtk_widget_queue_draw (child);
2394 /* Resize the item window */
2395 if (GTK_WIDGET_REALIZED (widget))
2401 for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2402 height += priv->heights[i];
2404 width = gtk_menu_get_n_columns (menu) * base_width;
2405 gdk_window_resize (menu->bin_window, width, height);
2408 if (menu->tearoff_active)
2410 if (allocation->height >= widget->requisition.height)
2412 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2414 gtk_widget_hide (menu->tearoff_scrollbar);
2415 gtk_menu_set_tearoff_hints (menu, allocation->width);
2417 gtk_menu_scroll_to (menu, 0);
2422 menu->tearoff_adjustment->upper = widget->requisition.height;
2423 menu->tearoff_adjustment->page_size = allocation->height;
2425 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2426 menu->tearoff_adjustment->upper)
2429 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2432 gtk_menu_scroll_to (menu, value);
2435 gtk_adjustment_changed (menu->tearoff_adjustment);
2437 if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2439 gtk_widget_show (menu->tearoff_scrollbar);
2440 gtk_menu_set_tearoff_hints (menu, allocation->width);
2448 gtk_menu_paint (GtkWidget *widget,
2449 GdkEventExpose *event)
2452 GtkMenuPrivate *priv;
2454 gint border_x, border_y;
2455 guint vertical_padding;
2456 guint horizontal_padding;
2458 g_return_if_fail (GTK_IS_MENU (widget));
2460 menu = GTK_MENU (widget);
2461 priv = gtk_menu_get_private (menu);
2463 gtk_widget_style_get (GTK_WIDGET (menu),
2464 "vertical-padding", &vertical_padding,
2465 "horizontal-padding", &horizontal_padding,
2468 border_x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness + horizontal_padding;
2469 border_y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
2470 gdk_drawable_get_size (widget->window, &width, &height);
2472 if (event->window == widget->window)
2474 gint arrow_space = MENU_SCROLL_ARROW_HEIGHT - 2 * widget->style->ythickness;
2475 gint arrow_size = 0.7 * arrow_space;
2477 gtk_paint_box (widget->style,
2481 NULL, widget, "menu",
2484 if (menu->upper_arrow_visible && !menu->tearoff_active)
2486 gtk_paint_box (widget->style,
2488 priv->upper_arrow_state,
2490 NULL, widget, "menu",
2493 width - 2 * border_x,
2494 MENU_SCROLL_ARROW_HEIGHT);
2496 gtk_paint_arrow (widget->style,
2498 priv->upper_arrow_state,
2500 NULL, widget, "menu_scroll_arrow_up",
2503 (width - arrow_size ) / 2,
2504 border_y + widget->style->ythickness + (arrow_space - arrow_size)/2,
2505 arrow_size, arrow_size);
2508 if (menu->lower_arrow_visible && !menu->tearoff_active)
2510 gtk_paint_box (widget->style,
2512 priv->lower_arrow_state,
2514 NULL, widget, "menu",
2516 height - border_y - MENU_SCROLL_ARROW_HEIGHT,
2518 MENU_SCROLL_ARROW_HEIGHT);
2520 gtk_paint_arrow (widget->style,
2522 priv->lower_arrow_state,
2524 NULL, widget, "menu_scroll_arrow_down",
2527 (width - arrow_size) / 2,
2528 height - border_y - MENU_SCROLL_ARROW_HEIGHT +
2529 widget->style->ythickness + (arrow_space - arrow_size)/2,
2530 arrow_size, arrow_size);
2533 else if (event->window == menu->bin_window)
2535 gtk_paint_box (widget->style,
2539 NULL, widget, "menu",
2540 - border_x, menu->scroll_offset - border_y,
2546 gtk_menu_expose (GtkWidget *widget,
2547 GdkEventExpose *event)
2549 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2550 g_return_val_if_fail (event != NULL, FALSE);
2552 if (GTK_WIDGET_DRAWABLE (widget))
2554 gtk_menu_paint (widget, event);
2556 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
2563 gtk_menu_show (GtkWidget *widget)
2565 GtkMenu *menu = GTK_MENU (widget);
2567 _gtk_menu_refresh_accel_paths (menu, FALSE);
2569 GTK_WIDGET_CLASS (parent_class)->show (widget);
2573 gtk_menu_button_scroll (GtkWidget *widget,
2574 GdkEventButton *event)
2576 if (GTK_IS_MENU (widget))
2578 GtkMenu *menu = GTK_MENU (widget);
2580 if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
2582 GtkSettings *settings = gtk_widget_get_settings (widget);
2583 gboolean touchscreen_mode;
2585 g_object_get (G_OBJECT (settings),
2586 "gtk-touchscreen-mode", &touchscreen_mode,
2589 if (touchscreen_mode)
2590 gtk_menu_handle_scrolling (menu,
2591 event->x_root, event->y_root,
2592 event->type == GDK_BUTTON_PRESS,
2603 gtk_menu_button_press (GtkWidget *widget,
2604 GdkEventButton *event)
2606 if (event->type != GDK_BUTTON_PRESS)
2609 /* Don't pop down the menu for presses over scroll arrows
2611 if (gtk_menu_button_scroll (widget, event))
2614 return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
2618 gtk_menu_button_release (GtkWidget *widget,
2619 GdkEventButton *event)
2621 if (GTK_IS_MENU (widget))
2623 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
2625 if (priv->ignore_button_release)
2627 priv->ignore_button_release = FALSE;
2632 if (event->type != GDK_BUTTON_RELEASE)
2635 /* Don't pop down the menu for releases over scroll arrows
2637 if (gtk_menu_button_scroll (widget, event))
2640 return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
2643 static const gchar *
2644 get_accel_path (GtkWidget *menu_item,
2649 GClosure *accel_closure;
2650 GtkAccelGroup *accel_group;
2652 path = _gtk_widget_get_accel_path (menu_item, locked);
2655 path = GTK_MENU_ITEM (menu_item)->accel_path;
2661 label = GTK_BIN (menu_item)->child;
2663 if (GTK_IS_ACCEL_LABEL (label))
2665 g_object_get (label,
2666 "accel-closure", &accel_closure,
2670 accel_group = gtk_accel_group_from_accel_closure (accel_closure);
2672 *locked = accel_group->lock_count > 0;
2682 gtk_menu_key_press (GtkWidget *widget,
2685 GtkMenuShell *menu_shell;
2687 gboolean delete = FALSE;
2688 gboolean can_change_accels;
2689 gchar *accel = NULL;
2690 guint accel_key, accel_mods;
2691 GdkModifierType consumed_modifiers;
2692 GdkDisplay *display;
2694 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2695 g_return_val_if_fail (event != NULL, FALSE);
2697 menu_shell = GTK_MENU_SHELL (widget);
2698 menu = GTK_MENU (widget);
2700 gtk_menu_stop_navigating_submenu (menu);
2702 if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
2705 display = gtk_widget_get_display (widget);
2707 g_object_get (gtk_widget_get_settings (widget),
2708 "gtk-menu-bar-accel", &accel,
2709 "gtk-can-change-accels", &can_change_accels,
2715 GdkModifierType mods = 0;
2716 gboolean handled = FALSE;
2718 gtk_accelerator_parse (accel, &keyval, &mods);
2721 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
2723 /* FIXME this is wrong, needs to be in the global accel resolution
2724 * thing, to properly consider i18n etc., but that probably requires
2725 * AccelGroup changes etc.
2727 if (event->keyval == keyval &&
2728 (mods & event->state) == mods)
2729 gtk_menu_shell_cancel (menu_shell);
2737 switch (event->keyval)
2748 /* Figure out what modifiers went into determining the key symbol */
2749 gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
2750 event->hardware_keycode, event->state, event->group,
2751 NULL, NULL, NULL, &consumed_modifiers);
2753 accel_key = gdk_keyval_to_lower (event->keyval);
2754 accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
2756 /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
2757 * We re-upper case when we match against the keyval, but display and save in caseless form.
2759 if (accel_key != event->keyval)
2760 accel_mods |= GDK_SHIFT_MASK;
2762 /* Modify the accelerators */
2763 if (can_change_accels &&
2764 menu_shell->active_menu_item &&
2765 GTK_BIN (menu_shell->active_menu_item)->child && /* no separators */
2766 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
2767 (delete || gtk_accelerator_valid (accel_key, accel_mods)))
2769 GtkWidget *menu_item = menu_shell->active_menu_item;
2770 gboolean locked, replace_accels = TRUE;
2773 path = get_accel_path (menu_item, &locked);
2774 if (!path || locked)
2776 /* can't change accelerators on menu_items without paths
2777 * (basically, those items are accelerator-locked).
2779 /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
2780 gdk_display_beep (display);
2786 /* For the keys that act to delete the current setting, we delete
2787 * the current setting if there is one, otherwise, we set the
2788 * key as the accelerator.
2794 if (gtk_accel_map_lookup_entry (path, &key) &&
2795 (key.accel_key || key.accel_mods))
2801 changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
2805 /* we failed, probably because this key is in use and
2808 /* g_print("failed to change\n"); */
2809 gdk_display_beep (display);
2818 check_threshold (GtkWidget *widget,
2819 int start_x, int start_y,
2825 ABS (start_x - x) > THRESHOLD ||
2826 ABS (start_y - y) > THRESHOLD;
2830 definitely_within_item (GtkWidget *widget,
2834 GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
2837 gdk_drawable_get_size (window, &w, &h);
2840 check_threshold (widget, 0, 0, x, y) &&
2841 check_threshold (widget, w - 1, 0, x, y) &&
2842 check_threshold (widget, w - 1, h - 1, x, y) &&
2843 check_threshold (widget, 0, h - 1, x, y);
2847 gtk_menu_motion_notify (GtkWidget *widget,
2848 GdkEventMotion *event)
2850 GtkWidget *menu_item;
2852 GtkMenuShell *menu_shell;
2854 gboolean need_enter;
2856 if (GTK_IS_MENU (widget))
2858 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
2860 if (priv->ignore_button_release)
2861 priv->ignore_button_release = FALSE;
2863 gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
2867 /* We received the event for one of two reasons:
2869 * a) We are the active menu, and did gtk_grab_add()
2870 * b) The widget is a child of ours, and the event was propagated
2872 * Since for computation of navigation regions, we want the menu which
2873 * is the parent of the menu item, for a), we need to find that menu,
2874 * which may be different from 'widget'.
2876 menu_item = gtk_get_event_widget ((GdkEvent*) event);
2877 if (!GTK_IS_MENU_ITEM (menu_item) ||
2878 !GTK_IS_MENU (menu_item->parent))
2881 menu_shell = GTK_MENU_SHELL (menu_item->parent);
2882 menu = GTK_MENU (menu_shell);
2884 if (definitely_within_item (menu_item, event->x, event->y))
2885 menu_shell->activate_time = 0;
2887 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
2889 /* Check to see if we are within an active submenu's navigation region
2891 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2894 /* Make sure we pop down if we enter a non-selectable menu item, so we
2895 * don't show a submenu when the cursor is outside the stay-up triangle.
2897 if (!_gtk_menu_item_is_selectable (menu_item))
2899 gtk_menu_shell_deselect (menu_shell);
2905 /* The menu is now sensitive to enter events on its items, but
2906 * was previously sensitive. So we fake an enter event.
2910 menu_shell->ignore_enter = FALSE;
2912 gdk_drawable_get_size (event->window, &width, &height);
2913 if (event->x >= 0 && event->x < width &&
2914 event->y >= 0 && event->y < height)
2916 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2919 send_event->crossing.window = g_object_ref (event->window);
2920 send_event->crossing.time = event->time;
2921 send_event->crossing.send_event = TRUE;
2922 send_event->crossing.x_root = event->x_root;
2923 send_event->crossing.y_root = event->y_root;
2924 send_event->crossing.x = event->x;
2925 send_event->crossing.y = event->y;
2927 /* We send the event to 'widget', the currently active menu,
2928 * instead of 'menu', the menu that the pointer is in. This
2929 * will ensure that the event will be ignored unless the
2930 * menuitem is a child of the active menu or some parent
2931 * menu of the active menu.
2933 result = gtk_widget_event (widget, send_event);
2934 gdk_event_free (send_event);
2944 get_double_arrows (GtkMenu *menu)
2946 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
2947 gboolean double_arrows;
2949 gtk_widget_style_get (GTK_WIDGET (menu),
2950 "double-arrows", &double_arrows,
2953 return double_arrows || (priv->initially_pushed_in &&
2954 menu->scroll_offset != 0);
2958 gtk_menu_scroll_by (GtkMenu *menu,
2963 gint view_width, view_height;
2964 gboolean double_arrows;
2966 widget = GTK_WIDGET (menu);
2967 offset = menu->scroll_offset + step;
2969 double_arrows = get_double_arrows (menu);
2971 /* If we scroll upward and the non-visible top part
2972 * is smaller than the scroll arrow it would be
2973 * pretty stupid to show the arrow and taking more
2974 * screen space than just scrolling to the top.
2977 if ((step < 0) && (offset < MENU_SCROLL_ARROW_HEIGHT))
2980 /* Don't scroll over the top if we weren't before: */
2981 if ((menu->scroll_offset >= 0) && (offset < 0))
2984 gdk_drawable_get_size (widget->window, &view_width, &view_height);
2986 /* Don't scroll past the bottom if we weren't before: */
2987 if (menu->scroll_offset > 0)
2988 view_height -= MENU_SCROLL_ARROW_HEIGHT;
2990 /* When both arrows are always shown, reduce
2991 * view height even more.
2994 view_height -= MENU_SCROLL_ARROW_HEIGHT;
2996 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
2997 (offset + view_height > widget->requisition.height))
2998 offset = widget->requisition.height - view_height;
3000 if (offset != menu->scroll_offset)
3001 gtk_menu_scroll_to (menu, offset);
3005 gtk_menu_do_timeout_scroll (GtkMenu *menu,
3006 gboolean touchscreen_mode)
3008 gboolean upper_visible;
3009 gboolean lower_visible;
3011 upper_visible = menu->upper_arrow_visible;
3012 lower_visible = menu->lower_arrow_visible;
3014 gtk_menu_scroll_by (menu, menu->scroll_step);
3016 if (touchscreen_mode &&
3017 (upper_visible != menu->upper_arrow_visible ||
3018 lower_visible != menu->lower_arrow_visible))
3020 /* We are about to hide a scroll arrow while the mouse is pressed,
3021 * this would cause the uncovered menu item to be activated on button
3022 * release. Therefore we need to ignore button release here
3024 GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3025 gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3030 gtk_menu_scroll_timeout (gpointer data)
3033 GtkSettings *settings;
3034 gboolean touchscreen_mode;
3036 GDK_THREADS_ENTER ();
3038 menu = GTK_MENU (data);
3040 settings = gtk_widget_get_settings (GTK_WIDGET (menu));
3041 g_object_get (settings,
3042 "gtk-touchscreen-mode", &touchscreen_mode,
3045 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3047 GDK_THREADS_LEAVE ();
3053 gtk_menu_scroll_timeout_initial (gpointer data)
3056 GtkSettings *settings;
3058 gboolean touchscreen_mode;
3060 GDK_THREADS_ENTER ();
3062 menu = GTK_MENU (data);
3064 settings = gtk_widget_get_settings (GTK_WIDGET (menu));
3065 g_object_get (settings,
3066 "gtk-timeout-repeat", &timeout,
3067 "gtk-touchscreen-mode", &touchscreen_mode,
3070 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3072 gtk_menu_remove_scroll_timeout (menu);
3074 menu->timeout_id = g_timeout_add (timeout, gtk_menu_scroll_timeout, menu);
3076 GDK_THREADS_LEAVE ();
3082 gtk_menu_start_scrolling (GtkMenu *menu)
3084 GtkSettings *settings;
3086 gboolean touchscreen_mode;
3088 settings = gtk_widget_get_settings (GTK_WIDGET (menu));
3089 g_object_get (settings,
3090 "gtk-timeout-repeat", &timeout,
3091 "gtk-touchscreen-mode", &touchscreen_mode,
3094 gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3096 menu->timeout_id = g_timeout_add (timeout, gtk_menu_scroll_timeout_initial,
3101 gtk_menu_scroll (GtkWidget *widget,
3102 GdkEventScroll *event)
3104 GtkMenu *menu = GTK_MENU (widget);
3106 switch (event->direction)
3108 case GDK_SCROLL_RIGHT:
3109 case GDK_SCROLL_DOWN:
3110 gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3112 case GDK_SCROLL_LEFT:
3114 gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3122 gtk_menu_handle_scrolling (GtkMenu *menu,
3128 GtkMenuShell *menu_shell;
3129 GtkMenuPrivate *priv;
3134 gboolean scroll_fast = FALSE;
3135 guint vertical_padding;
3138 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (menu));
3139 gboolean touchscreen_mode;
3141 priv = gtk_menu_get_private (menu);
3143 menu_shell = GTK_MENU_SHELL (menu);
3145 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3147 g_object_get (G_OBJECT (settings),
3148 "gtk-touchscreen-mode", &touchscreen_mode,
3151 gtk_widget_style_get (GTK_WIDGET (menu),
3152 "vertical-padding", &vertical_padding,
3155 border = GTK_CONTAINER (menu)->border_width +
3156 GTK_WIDGET (menu)->style->ythickness + vertical_padding;
3158 gdk_window_get_position (menu->toplevel->window, &top_x, &top_y);
3162 gdk_window_get_position (GTK_WIDGET (menu)->window, &win_x, &win_y);
3164 /* upper arrow handling */
3169 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
3172 if (menu->upper_arrow_visible && !menu->tearoff_active &&
3173 (x >= rect.x) && (x < rect.x + rect.width) &&
3174 (y >= rect.y) && (y < rect.y + rect.height))
3179 if (touchscreen_mode)
3180 menu->upper_arrow_prelight = in_arrow;
3182 if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3184 if (menu->upper_arrow_visible && !menu->tearoff_active)
3186 if (touchscreen_mode)
3188 if (enter && menu->upper_arrow_prelight &&
3189 menu->timeout_id == 0)
3191 /* Deselect the active item so that
3192 * any submenus are popped down
3194 gtk_menu_shell_deselect (menu_shell);
3196 gtk_menu_remove_scroll_timeout (menu);
3197 menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3201 /* Only do stuff on click. */
3202 gtk_menu_start_scrolling (menu);
3203 priv->upper_arrow_state = GTK_STATE_ACTIVE;
3206 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3211 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3214 gtk_menu_stop_scrolling (menu);
3217 else /* !touchscreen_mode */
3219 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
3221 if (enter && in_arrow &&
3222 (!menu->upper_arrow_prelight ||
3223 menu->scroll_fast != scroll_fast))
3225 menu->upper_arrow_prelight = TRUE;
3226 menu->scroll_fast = scroll_fast;
3227 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3230 /* Deselect the active item so that
3231 * any submenus are popped down
3233 gtk_menu_shell_deselect (menu_shell);
3235 gtk_menu_remove_scroll_timeout (menu);
3236 menu->scroll_step = scroll_fast ?
3237 -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
3240 g_timeout_add (scroll_fast ?
3241 MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
3242 gtk_menu_scroll_timeout, menu);
3244 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
3246 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3249 gtk_menu_stop_scrolling (menu);
3254 priv->upper_arrow_state = menu->upper_arrow_prelight ?
3255 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
3258 /* lower arrow handling */
3261 rect.y = win_y + height - border - MENU_SCROLL_ARROW_HEIGHT;
3263 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
3266 if (menu->lower_arrow_visible && !menu->tearoff_active &&
3267 (x >= rect.x) && (x < rect.x + rect.width) &&
3268 (y >= rect.y) && (y < rect.y + rect.height))
3273 if (touchscreen_mode)
3274 menu->lower_arrow_prelight = in_arrow;
3276 if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
3278 if (menu->lower_arrow_visible && !menu->tearoff_active)
3280 if (touchscreen_mode)
3282 if (enter && menu->lower_arrow_prelight &&
3283 menu->timeout_id == 0)
3285 /* Deselect the active item so that
3286 * any submenus are popped down
3288 gtk_menu_shell_deselect (menu_shell);
3290 gtk_menu_remove_scroll_timeout (menu);
3291 menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
3295 /* Only do stuff on click. */
3296 gtk_menu_start_scrolling (menu);
3297 priv->lower_arrow_state = GTK_STATE_ACTIVE;
3300 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3305 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3308 gtk_menu_stop_scrolling (menu);
3311 else /* !touchscreen_mode */
3313 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
3315 if (enter && in_arrow &&
3316 (!menu->lower_arrow_prelight ||
3317 menu->scroll_fast != scroll_fast))
3319 menu->lower_arrow_prelight = TRUE;
3320 menu->scroll_fast = scroll_fast;
3321 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3324 /* Deselect the active item so that
3325 * any submenus are popped down
3327 gtk_menu_shell_deselect (menu_shell);
3329 gtk_menu_remove_scroll_timeout (menu);
3330 menu->scroll_step = scroll_fast ?
3331 MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
3334 g_timeout_add (scroll_fast ?
3335 MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
3336 gtk_menu_scroll_timeout, menu);
3338 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
3340 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
3343 gtk_menu_stop_scrolling (menu);
3348 priv->lower_arrow_state = menu->lower_arrow_prelight ?
3349 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
3354 gtk_menu_enter_notify (GtkWidget *widget,
3355 GdkEventCrossing *event)
3357 GtkSettings *settings = gtk_widget_get_settings (widget);
3358 GtkWidget *menu_item;
3359 gboolean touchscreen_mode;
3361 g_object_get (G_OBJECT (settings),
3362 "gtk-touchscreen-mode", &touchscreen_mode,
3365 menu_item = gtk_get_event_widget ((GdkEvent*) event);
3366 if (GTK_IS_MENU (widget))
3368 GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
3370 if (!menu_shell->ignore_enter)
3371 gtk_menu_handle_scrolling (GTK_MENU (widget),
3372 event->x_root, event->y_root, TRUE, TRUE);
3375 if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
3377 GtkWidget *menu = menu_item->parent;
3379 if (GTK_IS_MENU (menu))
3381 GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
3382 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3384 if (priv->seen_item_enter)
3386 /* This is the second enter we see for an item
3387 * on this menu. This means a release should always
3390 menu_shell->activate_time = 0;
3392 else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
3393 event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
3395 if (definitely_within_item (menu_item, event->x, event->y))
3397 /* This is an actual user-enter (ie. not a pop-under)
3398 * In this case, the user must either have entered
3399 * sufficiently far enough into the item, or he must move
3400 * far enough away from the enter point. (see
3401 * gtk_menu_motion_notify())
3403 menu_shell->activate_time = 0;
3407 priv->seen_item_enter = TRUE;
3411 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
3412 * will not correspond to the event widget's parent. Check to see
3413 * if we are in the parent's navigation region.
3415 if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
3416 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent),
3417 event->x_root, event->y_root))
3420 return GTK_WIDGET_CLASS (parent_class)->enter_notify_event (widget, event);
3424 gtk_menu_leave_notify (GtkWidget *widget,
3425 GdkEventCrossing *event)
3427 GtkMenuShell *menu_shell;
3429 GtkMenuItem *menu_item;
3430 GtkWidget *event_widget;
3432 menu = GTK_MENU (widget);
3433 menu_shell = GTK_MENU_SHELL (widget);
3435 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3438 gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
3440 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3442 if (!GTK_IS_MENU_ITEM (event_widget))
3445 menu_item = GTK_MENU_ITEM (event_widget);
3447 /* Here we check to see if we're leaving an active menu item with a submenu,
3448 * in which case we enter submenu navigation mode.
3450 if (menu_shell->active_menu_item != NULL
3451 && menu_item->submenu != NULL
3452 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
3454 if (GTK_MENU_SHELL (menu_item->submenu)->active)
3456 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
3459 else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
3461 /* We are leaving an active menu item with nonactive submenu.
3462 * Deselect it so we don't surprise the user with by popping
3463 * up a submenu _after_ he left the item.
3465 gtk_menu_shell_deselect (menu_shell);
3470 return GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event);
3474 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
3476 if (menu->navigation_region)
3478 gdk_region_destroy (menu->navigation_region);
3479 menu->navigation_region = NULL;
3482 if (menu->navigation_timeout)
3484 g_source_remove (menu->navigation_timeout);
3485 menu->navigation_timeout = 0;
3489 /* When the timeout is elapsed, the navigation region is destroyed
3490 * and the menuitem under the pointer (if any) is selected.
3493 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
3495 GtkMenu *menu = user_data;
3496 GdkWindow *child_window;
3498 GDK_THREADS_ENTER ();
3500 gtk_menu_stop_navigating_submenu (menu);
3502 if (GTK_WIDGET_REALIZED (menu))
3504 child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
3508 GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3510 send_event->crossing.window = g_object_ref (child_window);
3511 send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
3512 send_event->crossing.send_event = TRUE;
3514 GTK_WIDGET_CLASS (parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
3516 gdk_event_free (send_event);
3520 GDK_THREADS_LEAVE ();
3526 gtk_menu_navigating_submenu (GtkMenu *menu,
3530 if (menu->navigation_region)
3532 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
3536 gtk_menu_stop_navigating_submenu (menu);
3543 #undef DRAW_STAY_UP_TRIANGLE
3545 #ifdef DRAW_STAY_UP_TRIANGLE
3548 draw_stay_up_triangle (GdkWindow *window,
3551 /* Draw ugly color all over the stay-up triangle */
3552 GdkColor ugly_color = { 0, 50000, 10000, 10000 };
3553 GdkGCValues gc_values;
3555 GdkRectangle clipbox;
3557 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
3558 ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
3559 gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
3560 gdk_gc_set_clip_region (ugly_gc, region);
3562 gdk_region_get_clipbox (region, &clipbox);
3564 gdk_draw_rectangle (window,
3567 clipbox.x, clipbox.y,
3568 clipbox.width, clipbox.height);
3570 g_object_unref (ugly_gc);
3575 flip_region (GdkRegion *region,
3580 GdkRectangle *rectangles;
3581 GdkRectangle clipbox;
3582 GdkRegion *new_region;
3585 new_region = gdk_region_new ();
3587 gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
3588 gdk_region_get_clipbox (region, &clipbox);
3590 for (i = 0; i < n_rectangles; ++i)
3592 GdkRectangle rect = rectangles[i];
3595 rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
3598 rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
3600 gdk_region_union_with_rect (new_region, &rect);
3603 g_free (rectangles);
3609 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
3610 GtkMenuItem *menu_item,
3611 GdkEventCrossing *event)
3613 gint submenu_left = 0;
3614 gint submenu_right = 0;
3615 gint submenu_top = 0;
3616 gint submenu_bottom = 0;
3620 GtkWidget *event_widget;
3622 g_return_if_fail (menu_item->submenu != NULL);
3623 g_return_if_fail (event != NULL);
3625 event_widget = gtk_get_event_widget ((GdkEvent*) event);
3627 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
3628 gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
3630 submenu_right = submenu_left + width;
3631 submenu_bottom = submenu_top + height;
3633 gdk_drawable_get_size (event_widget->window, &width, &height);
3635 if (event->x >= 0 && event->x < width)
3638 gboolean flip_y = FALSE;
3639 gboolean flip_x = FALSE;
3641 gtk_menu_stop_navigating_submenu (menu);
3643 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
3646 point[0].x = event->x_root;
3647 point[1].x = submenu_left;
3652 point[0].x = event->x_root + 1;
3653 point[1].x = 2 * (event->x_root + 1) - submenu_right;
3661 point[0].y = event->y_root + 1;
3662 point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
3664 if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
3672 point[0].y = event->y_root;
3673 point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
3675 if (point[0].y >= submenu_bottom)
3679 point[2].x = point[1].x;
3680 point[2].y = point[0].y;
3682 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
3684 if (flip_x || flip_y)
3686 GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
3687 gdk_region_destroy (menu->navigation_region);
3688 menu->navigation_region = new_region;
3691 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3692 "gtk-menu-popdown-delay", &popdown_delay,
3695 menu->navigation_timeout = g_timeout_add (popdown_delay,
3696 gtk_menu_stop_navigating_submenu_cb, menu);
3698 #ifdef DRAW_STAY_UP_TRIANGLE
3699 draw_stay_up_triangle (gdk_get_default_root_window(),
3700 menu->navigation_region);
3706 gtk_menu_deactivate (GtkMenuShell *menu_shell)
3710 g_return_if_fail (GTK_IS_MENU (menu_shell));
3712 parent = menu_shell->parent_menu_shell;
3714 menu_shell->activate_time = 0;
3715 gtk_menu_popdown (GTK_MENU (menu_shell));
3718 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
3722 gtk_menu_position (GtkMenu *menu)
3725 GtkRequisition requisition;
3726 GtkMenuPrivate *private;
3731 GdkScreen *pointer_screen;
3732 GdkRectangle monitor;
3734 g_return_if_fail (GTK_IS_MENU (menu));
3736 widget = GTK_WIDGET (menu);
3738 screen = gtk_widget_get_screen (widget);
3739 gdk_display_get_pointer (gdk_screen_get_display (screen),
3740 &pointer_screen, &x, &y, NULL);
3742 /* We need the requisition to figure out the right place to
3743 * popup the menu. In fact, we always need to ask here, since
3744 * if a size_request was queued while we weren't popped up,
3745 * the requisition won't have been recomputed yet.
3747 gtk_widget_size_request (widget, &requisition);
3749 if (pointer_screen != screen)
3751 /* Pointer is on a different screen; roughly center the
3752 * menu on the screen. If someone was using multiscreen
3753 * + Xinerama together they'd probably want something
3754 * fancier; but that is likely to be vanishingly rare.
3756 x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
3757 y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
3760 private = gtk_menu_get_private (menu);
3761 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
3763 private->initially_pushed_in = FALSE;
3765 if (menu->position_func)
3767 (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
3768 menu->position_func_data);
3769 if (private->monitor_num < 0)
3770 private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
3772 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3776 gint space_left, space_right, space_above, space_below;
3779 gint xthickness = widget->style->xthickness;
3780 gint ythickness = widget->style->ythickness;
3781 gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
3783 /* The placement of popup menus horizontally works like this (with
3784 * RTL in parentheses)
3786 * - If there is enough room to the right (left) of the mouse cursor,
3787 * position the menu there.
3789 * - Otherwise, if if there is enough room to the left (right) of the
3790 * mouse cursor, position the menu there.
3792 * - Otherwise if the menu is smaller than the monitor, position it
3793 * on the side of the mouse cursor that has the most space available
3795 * - Otherwise (if there is simply not enough room for the menu on the
3796 * monitor), position it as far left (right) as possible.
3798 * Positioning in the vertical direction is similar: first try below
3799 * mouse cursor, then above.
3801 gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3803 space_left = x - monitor.x;
3804 space_right = monitor.x + monitor.width - x - 1;
3805 space_above = y - monitor.y;
3806 space_below = monitor.y + monitor.height - y - 1;
3808 /* position horizontally */
3810 /* the amount of space we need to position the menu. Note the
3811 * menu is offset "xthickness" pixels
3813 needed_width = requisition.width - xthickness;
3815 if (needed_width <= space_left ||
3816 needed_width <= space_right)
3818 if ((rtl && needed_width <= space_left) ||
3819 (!rtl && needed_width > space_right))
3822 x = x + xthickness - requisition.width + 1;
3826 /* position right */
3830 /* x is clamped on-screen further down */
3832 else if (requisition.width <= monitor.width)
3834 /* the menu is too big to fit on either side of the mouse
3835 * cursor, but smaller than the monitor. Position it on
3836 * the side that has the most space
3838 if (space_left > space_right)
3846 x = monitor.x + monitor.width - requisition.width;
3849 else /* menu is simply too big for the monitor */
3854 x = monitor.x + monitor.width - requisition.width;
3863 /* Position vertically. The algorithm is the same as above, but
3864 * simpler because we don't have to take RTL into account.
3866 needed_height = requisition.height - ythickness;
3868 if (needed_height <= space_above ||
3869 needed_height <= space_below)
3871 if (needed_height <= space_below)
3874 y = y + ythickness - requisition.height + 1;
3876 y = CLAMP (y, monitor.y,
3877 monitor.y + monitor.height - requisition.height);
3879 else if (needed_height > space_below && needed_height > space_above)
3881 if (space_below >= space_above)
3882 y = monitor.y + monitor.height - requisition.height;
3894 if (private->initially_pushed_in)
3896 menu_height = GTK_WIDGET (menu)->requisition.height;
3898 if (y + menu_height > monitor.y + monitor.height)
3900 scroll_offset -= y + menu_height - (monitor.y + monitor.height);
3901 y = (monitor.y + monitor.height) - menu_height;
3906 scroll_offset += monitor.y - y;
3911 /* FIXME: should this be done in the various position_funcs ? */
3912 x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
3914 if (GTK_MENU_SHELL (menu)->active)
3916 private->have_position = TRUE;
3921 if (y + requisition.height > monitor.y + monitor.height)
3922 requisition.height = (monitor.y + monitor.height) - y;
3926 scroll_offset += monitor.y - y;
3927 requisition.height -= monitor.y - y;
3931 if (scroll_offset > 0)
3932 scroll_offset += MENU_SCROLL_ARROW_HEIGHT;
3934 gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window),
3937 if (!GTK_MENU_SHELL (menu)->active)
3939 gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
3940 requisition.width, requisition.height);
3943 menu->scroll_offset = scroll_offset;
3947 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
3949 if (menu->timeout_id)
3951 g_source_remove (menu->timeout_id);
3952 menu->timeout_id = 0;
3957 gtk_menu_stop_scrolling (GtkMenu *menu)
3959 GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (menu));
3960 gboolean touchscreen_mode;
3962 gtk_menu_remove_scroll_timeout (menu);
3964 g_object_get (G_OBJECT (settings),
3965 "gtk-touchscreen-mode", &touchscreen_mode,
3968 if (!touchscreen_mode)
3970 menu->upper_arrow_prelight = FALSE;
3971 menu->lower_arrow_prelight = FALSE;
3976 gtk_menu_scroll_to (GtkMenu *menu,
3981 gint view_width, view_height;
3984 guint vertical_padding;
3985 guint horizontal_padding;
3986 gboolean double_arrows;
3988 widget = GTK_WIDGET (menu);
3990 if (menu->tearoff_active &&
3991 menu->tearoff_adjustment &&
3992 (menu->tearoff_adjustment->value != offset))
3994 menu->tearoff_adjustment->value =
3996 0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
3997 gtk_adjustment_value_changed (menu->tearoff_adjustment);
4000 /* Move/resize the viewport according to arrows: */
4001 view_width = widget->allocation.width;
4002 view_height = widget->allocation.height;
4004 gtk_widget_style_get (GTK_WIDGET (menu),
4005 "vertical-padding", &vertical_padding,
4006 "horizontal-padding", &horizontal_padding,
4009 double_arrows = get_double_arrows (menu);
4011 border_width = GTK_CONTAINER (menu)->border_width;
4012 view_width -= (border_width + widget->style->xthickness + horizontal_padding) * 2;
4013 view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
4014 menu_height = widget->requisition.height -
4015 (border_width + widget->style->ythickness + vertical_padding) * 2;
4017 x = border_width + widget->style->xthickness + horizontal_padding;
4018 y = border_width + widget->style->ythickness + vertical_padding;
4020 if (double_arrows && !menu->tearoff_active)
4022 if (view_height < menu_height ||
4023 (offset > 0 && menu->scroll_offset > 0) ||
4024 (offset < 0 && menu->scroll_offset < 0))
4026 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4027 GtkStateType upper_arrow_previous_state = priv->upper_arrow_state;
4028 GtkStateType lower_arrow_previous_state = priv->lower_arrow_state;
4030 if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4031 gtk_widget_queue_draw (GTK_WIDGET (menu));
4033 view_height -= 2 * MENU_SCROLL_ARROW_HEIGHT;
4034 y += MENU_SCROLL_ARROW_HEIGHT;
4036 menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4039 priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4041 priv->upper_arrow_state = menu->upper_arrow_prelight ?
4042 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4044 if (offset >= menu_height - view_height)
4045 priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4047 priv->lower_arrow_state = menu->lower_arrow_prelight ?
4048 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4050 if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4051 (priv->lower_arrow_state != lower_arrow_previous_state))
4052 gtk_widget_queue_draw (GTK_WIDGET (menu));
4054 if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4055 priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4057 /* At the upper border, possibly remove timeout */
4058 if (menu->scroll_step < 0)
4060 gtk_menu_stop_scrolling (menu);
4061 gtk_widget_queue_draw (GTK_WIDGET (menu));
4065 if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4066 priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4068 /* At the lower border, possibly remove timeout */
4069 if (menu->scroll_step > 0)
4071 gtk_menu_stop_scrolling (menu);
4072 gtk_widget_queue_draw (GTK_WIDGET (menu));
4076 else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4080 menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4081 menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4083 gtk_menu_stop_scrolling (menu);
4084 gtk_widget_queue_draw (GTK_WIDGET (menu));
4087 else if (!menu->tearoff_active)
4089 gboolean last_visible;
4091 last_visible = menu->upper_arrow_visible;
4092 menu->upper_arrow_visible = offset > 0;
4094 if (menu->upper_arrow_visible)
4095 view_height -= MENU_SCROLL_ARROW_HEIGHT;
4097 if ((last_visible != menu->upper_arrow_visible) &&
4098 !menu->upper_arrow_visible)
4100 menu->upper_arrow_prelight = FALSE;
4102 /* If we hid the upper arrow, possibly remove timeout */
4103 if (menu->scroll_step < 0)
4105 gtk_menu_stop_scrolling (menu);
4106 gtk_widget_queue_draw (GTK_WIDGET (menu));
4110 last_visible = menu->lower_arrow_visible;
4111 menu->lower_arrow_visible = offset < menu_height - view_height;
4113 if (menu->lower_arrow_visible)
4114 view_height -= MENU_SCROLL_ARROW_HEIGHT;
4116 if ((last_visible != menu->lower_arrow_visible) &&
4117 !menu->lower_arrow_visible)
4119 menu->lower_arrow_prelight = FALSE;
4121 /* If we hid the lower arrow, possibly remove timeout */
4122 if (menu->scroll_step > 0)
4124 gtk_menu_stop_scrolling (menu);
4125 gtk_widget_queue_draw (GTK_WIDGET (menu));
4129 if (menu->upper_arrow_visible)
4130 y += MENU_SCROLL_ARROW_HEIGHT;
4133 /* Scroll the menu: */
4134 if (GTK_WIDGET_REALIZED (menu))
4135 gdk_window_move (menu->bin_window, 0, -offset);
4137 if (GTK_WIDGET_REALIZED (menu))
4138 gdk_window_move_resize (menu->view_window,
4144 menu->scroll_offset = offset;
4148 compute_child_offset (GtkMenu *menu,
4149 GtkWidget *menu_item,
4152 gboolean *is_last_child)
4154 GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4155 gint item_top_attach;
4156 gint item_bottom_attach;
4157 gint child_offset = 0;
4160 get_effective_child_attach (menu_item, NULL, NULL,
4161 &item_top_attach, &item_bottom_attach);
4163 /* there is a possibility that we get called before _size_request, so
4164 * check the height table for safety.
4166 if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4169 /* when we have a row with only invisible children, it's height will
4170 * be zero, so there's no need to check WIDGET_VISIBLE here
4172 for (i = 0; i < item_top_attach; i++)
4173 child_offset += priv->heights[i];
4176 *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4178 *offset = child_offset;
4180 *height = priv->heights[item_top_attach];
4186 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4187 GtkWidget *menu_item)
4190 gint child_offset, child_height;
4194 gboolean last_child = 0;
4196 menu = GTK_MENU (menu_shell);
4198 /* We need to check if the selected item fully visible.
4199 * If not we need to scroll the menu so that it becomes fully
4203 if (compute_child_offset (menu, menu_item,
4204 &child_offset, &child_height, &last_child))
4206 guint vertical_padding;
4207 gboolean double_arrows;
4209 y = menu->scroll_offset;
4210 gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
4212 gtk_widget_style_get (GTK_WIDGET (menu),
4213 "vertical-padding", &vertical_padding,
4216 double_arrows = get_double_arrows (menu);
4218 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
4220 if (child_offset < y)
4222 /* Ignore the enter event we might get if the pointer is on the menu
4224 menu_shell->ignore_enter = TRUE;
4225 gtk_menu_scroll_to (menu, child_offset);
4230 if (menu->upper_arrow_visible && !menu->tearoff_active)
4231 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
4232 if (menu->lower_arrow_visible && !menu->tearoff_active)
4233 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
4235 if (child_offset + child_height > y + height - arrow_height)
4238 if ((!last_child && !menu->tearoff_active) || double_arrows)
4239 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
4241 y = child_offset + child_height - height + arrow_height;
4242 if (((y > 0) && !menu->tearoff_active) || double_arrows)
4244 /* Need upper arrow */
4245 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
4246 y = child_offset + child_height - height + arrow_height;
4248 /* Ignore the enter event we might get if the pointer is on the menu
4250 menu_shell->ignore_enter = TRUE;
4251 gtk_menu_scroll_to (menu, y);
4259 gtk_menu_select_item (GtkMenuShell *menu_shell,
4260 GtkWidget *menu_item)
4262 GtkMenu *menu = GTK_MENU (menu_shell);
4264 if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
4265 gtk_menu_scroll_item_visible (menu_shell, menu_item);
4267 GTK_MENU_SHELL_CLASS (parent_class)->select_item (menu_shell, menu_item);
4271 /* Reparent the menu, taking care of the refcounting
4273 * If unrealize is true we force a unrealize while reparenting the parent.
4274 * This can help eliminate flicker in some cases.
4276 * What happens is that when the menu is unrealized and then re-realized,
4277 * the allocations are as follows:
4279 * parent - 1x1 at (0,0)
4280 * child1 - 100x20 at (0,0)
4281 * child2 - 100x20 at (0,20)
4282 * child3 - 100x20 at (0,40)
4284 * That is, the parent is small but the children are full sized. Then,
4285 * when the queued_resize gets processed, the parent gets resized to
4288 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
4289 * contains the following logic:
4291 * - if a move or resize operation on a window would change the clip
4292 * region on the children, then before the window is resized
4293 * the background for children is temporarily set to None, the
4294 * move/resize done, and the background for the children restored.
4296 * So, at the point where the parent is resized to final size, the
4297 * background for the children is temporarily None, and thus they
4298 * are not cleared to the background color and the previous background
4299 * (the image of the menu) is left in place.
4302 gtk_menu_reparent (GtkMenu *menu,
4303 GtkWidget *new_parent,
4306 GtkObject *object = GTK_OBJECT (menu);
4307 GtkWidget *widget = GTK_WIDGET (menu);
4308 gboolean was_floating = g_object_is_floating (object);
4310 g_object_ref_sink (object);
4314 g_object_ref (object);
4315 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
4316 gtk_container_add (GTK_CONTAINER (new_parent), widget);
4317 g_object_unref (object);
4320 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
4323 g_object_force_floating (G_OBJECT (object));
4325 g_object_unref (object);
4329 gtk_menu_show_all (GtkWidget *widget)
4331 /* Show children, but not self. */
4332 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
4337 gtk_menu_hide_all (GtkWidget *widget)
4339 /* Hide children, but not self. */
4340 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
4344 * gtk_menu_set_screen:
4345 * @menu: a #GtkMenu.
4346 * @screen: a #GdkScreen, or %NULL if the screen should be
4347 * determined by the widget the menu is attached to.
4349 * Sets the #GdkScreen on which the menu will be displayed.
4354 gtk_menu_set_screen (GtkMenu *menu,
4357 g_return_if_fail (GTK_IS_MENU (menu));
4358 g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
4360 g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
4364 menu_change_screen (menu, screen);
4368 GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
4370 attach_widget_screen_changed (attach_widget, NULL, menu);
4376 * @menu: a #GtkMenu.
4377 * @child: a #GtkMenuItem.
4378 * @left_attach: The column number to attach the left side of the item to.
4379 * @right_attach: The column number to attach the right side of the item to.
4380 * @top_attach: The row number to attach the top of the item to.
4381 * @bottom_attach: The row number to attach the bottom of the item to.
4383 * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
4384 * an item will occupy is specified by @left_attach, @right_attach,
4385 * @top_attach and @bottom_attach. These each represent the leftmost,
4386 * rightmost, uppermost and lower column and row numbers of the table.
4387 * (Columns and rows are indexed from zero).
4389 * Note that this function is not related to gtk_menu_detach().
4394 gtk_menu_attach (GtkMenu *menu,
4399 guint bottom_attach)
4401 GtkMenuShell *menu_shell;
4403 g_return_if_fail (GTK_IS_MENU (menu));
4404 g_return_if_fail (GTK_IS_MENU_ITEM (child));
4405 g_return_if_fail (child->parent == NULL ||
4406 child->parent == GTK_WIDGET (menu));
4407 g_return_if_fail (left_attach < right_attach);
4408 g_return_if_fail (top_attach < bottom_attach);
4410 menu_shell = GTK_MENU_SHELL (menu);
4414 AttachInfo *ai = get_attach_info (child);
4416 ai->left_attach = left_attach;
4417 ai->right_attach = right_attach;
4418 ai->top_attach = top_attach;
4419 ai->bottom_attach = bottom_attach;
4421 menu_shell->children = g_list_append (menu_shell->children, child);
4423 gtk_widget_set_parent (child, GTK_WIDGET (menu));
4425 menu_queue_resize (menu);
4429 gtk_container_child_set (GTK_CONTAINER (child->parent), child,
4430 "left-attach", left_attach,
4431 "right-attach", right_attach,
4432 "top-attach", top_attach,
4433 "bottom-attach", bottom_attach,
4439 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
4443 g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
4444 "gtk-menu-popup-delay", &popup_delay,
4451 find_child_containing (GtkMenuShell *menu_shell,
4459 /* find a child which includes the area given by
4460 * left, right, top, bottom.
4463 for (list = menu_shell->children; list; list = list->next)
4467 if (!_gtk_menu_item_is_selectable (list->data))
4470 get_effective_child_attach (list->data, &l, &r, &t, &b);
4472 if (l <= left && right <= r
4473 && t <= top && bottom <= b)
4474 return GTK_WIDGET (list->data);
4481 gtk_menu_move_current (GtkMenuShell *menu_shell,
4482 GtkMenuDirectionType direction)
4484 GtkMenu *menu = GTK_MENU (menu_shell);
4487 GtkWidget *match = NULL;
4489 if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
4493 case GTK_MENU_DIR_CHILD:
4494 direction = GTK_MENU_DIR_PARENT;
4496 case GTK_MENU_DIR_PARENT:
4497 direction = GTK_MENU_DIR_CHILD;
4503 /* use special table menu key bindings */
4504 if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
4506 get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
4508 if (direction == GTK_MENU_DIR_NEXT)
4510 for (i = b; i < gtk_menu_get_n_rows (menu); i++)
4512 match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
4520 for (i = 0; i < t; i++)
4522 match = find_child_containing (menu_shell,
4523 l, l + 1, i, i + 1);
4529 else if (direction == GTK_MENU_DIR_PREV)
4531 for (i = t; i > 0; i--)
4533 match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
4541 for (i = gtk_menu_get_n_rows (menu); i > b; i--)
4543 match = find_child_containing (menu_shell,
4544 l, l + 1, i - 1, i);
4550 else if (direction == GTK_MENU_DIR_PARENT)
4552 /* we go one left if possible */
4554 match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
4558 GtkWidget *parent = menu_shell->parent_menu_shell;
4561 || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
4562 match = menu_shell->active_menu_item;
4565 else if (direction == GTK_MENU_DIR_CHILD)
4567 /* we go one right if possible */
4568 if (r < gtk_menu_get_n_columns (menu))
4569 match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
4573 GtkWidget *parent = menu_shell->parent_menu_shell;
4575 if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
4577 g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
4578 match = menu_shell->active_menu_item;
4584 gtk_menu_shell_select_item (menu_shell, match);
4589 GTK_MENU_SHELL_CLASS (parent_class)->move_current (menu_shell, direction);
4593 get_visible_size (GtkMenu *menu)
4595 GtkWidget *widget = GTK_WIDGET (menu);
4596 GtkContainer *container = GTK_CONTAINER (menu);
4598 gint menu_height = (widget->allocation.height
4599 - 2 * (container->border_width
4600 + widget->style->ythickness));
4602 if (menu->upper_arrow_visible && !menu->tearoff_active)
4603 menu_height -= MENU_SCROLL_ARROW_HEIGHT;
4604 if (menu->lower_arrow_visible && !menu->tearoff_active)
4605 menu_height -= MENU_SCROLL_ARROW_HEIGHT;
4610 /* Find the sensitive on-screen child containing @y, or if none,
4611 * the nearest selectable onscreen child. (%NULL if none)
4614 child_at (GtkMenu *menu,
4617 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4618 GtkWidget *child = NULL;
4619 gint child_offset = 0;
4622 gint lower, upper; /* Onscreen bounds */
4624 menu_height = get_visible_size (menu);
4625 lower = menu->scroll_offset;
4626 upper = menu->scroll_offset + menu_height;
4628 for (children = menu_shell->children; children; children = children->next)
4630 if (GTK_WIDGET_VISIBLE (children->data))
4632 GtkRequisition child_requisition;
4634 gtk_widget_size_request (children->data, &child_requisition);
4636 if (_gtk_menu_item_is_selectable (children->data) &&
4637 child_offset >= lower &&
4638 child_offset + child_requisition.height <= upper)
4640 child = children->data;
4642 if (child_offset + child_requisition.height > y &&
4643 !GTK_IS_TEAROFF_MENU_ITEM (child))
4647 child_offset += child_requisition.height;
4655 get_menu_height (GtkMenu *menu)
4658 GtkWidget *widget = GTK_WIDGET (menu);
4660 height = widget->requisition.height;
4661 height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
4663 if (menu->upper_arrow_visible && !menu->tearoff_active)
4664 height -= MENU_SCROLL_ARROW_HEIGHT;
4666 if (menu->lower_arrow_visible && !menu->tearoff_active)
4667 height -= MENU_SCROLL_ARROW_HEIGHT;
4673 gtk_menu_real_move_scroll (GtkMenu *menu,
4676 gint page_size = get_visible_size (menu);
4677 gint end_position = get_menu_height (menu);
4678 GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4682 case GTK_SCROLL_PAGE_UP:
4683 case GTK_SCROLL_PAGE_DOWN:
4687 gint child_offset = 0;
4688 gboolean old_upper_arrow_visible;
4691 if (type == GTK_SCROLL_PAGE_UP)
4696 if (menu_shell->active_menu_item)
4700 compute_child_offset (menu, menu_shell->active_menu_item,
4701 &child_offset, &child_height, NULL);
4702 child_offset += child_height / 2;
4705 menu_shell->ignore_enter = TRUE;
4706 old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4707 old_offset = menu->scroll_offset;
4709 new_offset = menu->scroll_offset + step;
4710 new_offset = CLAMP (new_offset, 0, end_position - page_size);
4712 gtk_menu_scroll_to (menu, new_offset);
4714 if (menu_shell->active_menu_item)
4716 GtkWidget *new_child;
4717 gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4719 if (menu->scroll_offset != old_offset)
4720 step = menu->scroll_offset - old_offset;
4722 step -= (new_upper_arrow_visible - old_upper_arrow_visible) * MENU_SCROLL_ARROW_HEIGHT;
4724 new_child = child_at (menu, child_offset + step);
4726 gtk_menu_shell_select_item (menu_shell, new_child);
4730 case GTK_SCROLL_START:
4731 /* Ignore the enter event we might get if the pointer is on the menu
4733 menu_shell->ignore_enter = TRUE;
4734 gtk_menu_scroll_to (menu, 0);
4735 gtk_menu_shell_select_first (menu_shell, TRUE);
4737 case GTK_SCROLL_END:
4738 /* Ignore the enter event we might get if the pointer is on the menu
4740 menu_shell->ignore_enter = TRUE;
4741 gtk_menu_scroll_to (menu, end_position - page_size);
4742 _gtk_menu_shell_select_last (menu_shell, TRUE);
4751 * gtk_menu_set_monitor:
4753 * @monitor_num: the number of the monitor on which the menu should
4756 * Informs GTK+ on which monitor a menu should be popped up.
4757 * See gdk_screen_get_monitor_geometry().
4759 * This function should be called from a #GtkMenuPositionFunc if the
4760 * menu should not appear on the same monitor as the pointer. This
4761 * information can't be reliably inferred from the coordinates returned
4762 * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates
4763 * may extend beyond the monitor boundaries or even the screen boundaries.
4768 gtk_menu_set_monitor (GtkMenu *menu,
4771 GtkMenuPrivate *priv;
4772 g_return_if_fail (GTK_IS_MENU (menu));
4774 priv = gtk_menu_get_private (menu);
4776 priv->monitor_num = monitor_num;
4780 * gtk_menu_get_for_attach_widget:
4781 * @widget: a #GtkWidget
4782 * * Returns a list of the menus which are attached to this widget.
4783 * This list is owned by GTK+ and must not be modified.
4785 * Return value: the list of menus attached to his widget.
4790 gtk_menu_get_for_attach_widget (GtkWidget *widget)
4794 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
4796 list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
4801 gtk_menu_grab_notify (GtkWidget *widget,
4802 gboolean was_grabbed)
4804 GtkWidget *toplevel;
4805 GtkWindowGroup *group;
4808 toplevel = gtk_widget_get_toplevel (widget);
4809 group = gtk_window_get_group (GTK_WINDOW (toplevel));
4810 grab = _gtk_window_group_get_current_grab (group);
4814 if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
4815 gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
4819 #define __GTK_MENU_C__
4820 #include "gtkaliasdef.c"