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/.
28 #include <string.h> /* memset */
29 #include "gdk/gdkkeysyms.h"
30 #include "gtkbindings.h"
34 #include "gtkmenuitem.h"
35 #include "gtksignal.h"
36 #include "gtkwindow.h"
38 #include "gtkvscrollbar.h"
39 #include "gtksettings.h"
43 #define MENU_ITEM_CLASS(w) GTK_MENU_ITEM_GET_CLASS (w)
44 #define MENU_NEEDS_RESIZE(m) GTK_MENU_SHELL (m)->menu_flag
46 #define SUBMENU_NAV_REGION_PADDING 2
47 #define SUBMENU_NAV_HYSTERESIS_TIMEOUT 333
49 #define MENU_SCROLL_STEP 10
50 #define MENU_SCROLL_ARROW_HEIGHT 16
51 #define MENU_SCROLL_FAST_ZONE 4
52 #define MENU_SCROLL_TIMEOUT1 150
53 #define MENU_SCROLL_TIMEOUT2 50
55 typedef struct _GtkMenuAttachData GtkMenuAttachData;
57 struct _GtkMenuAttachData
59 GtkWidget *attach_widget;
60 GtkMenuDetachFunc detacher;
68 static void gtk_menu_class_init (GtkMenuClass *klass);
69 static void gtk_menu_init (GtkMenu *menu);
70 static void gtk_menu_set_property (GObject *object,
74 static void gtk_menu_get_property (GObject *object,
78 static void gtk_menu_destroy (GtkObject *object);
79 static void gtk_menu_realize (GtkWidget *widget);
80 static void gtk_menu_unrealize (GtkWidget *widget);
81 static void gtk_menu_size_request (GtkWidget *widget,
82 GtkRequisition *requisition);
83 static void gtk_menu_size_allocate (GtkWidget *widget,
84 GtkAllocation *allocation);
85 static void gtk_menu_paint (GtkWidget *widget);
86 static gboolean gtk_menu_expose (GtkWidget *widget,
87 GdkEventExpose *event);
88 static gboolean gtk_menu_key_press (GtkWidget *widget,
90 static gboolean gtk_menu_motion_notify (GtkWidget *widget,
91 GdkEventMotion *event);
92 static gboolean gtk_menu_enter_notify (GtkWidget *widget,
93 GdkEventCrossing *event);
94 static gboolean gtk_menu_leave_notify (GtkWidget *widget,
95 GdkEventCrossing *event);
96 static void gtk_menu_scroll_to (GtkMenu *menu,
98 static void gtk_menu_stop_scrolling (GtkMenu *menu);
99 static gboolean gtk_menu_scroll_timeout (gpointer data);
100 static void gtk_menu_select_item (GtkMenuShell *menu_shell,
101 GtkWidget *menu_item);
102 static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
105 static void gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
107 static void gtk_menu_handle_scrolling (GtkMenu *menu,
109 static void gtk_menu_set_tearoff_hints (GtkMenu *menu,
112 static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
113 static gboolean gtk_menu_stop_navigating_submenu_cb (gpointer user_data);
114 static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
117 static void gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
118 GtkMenuItem *menu_item,
119 GdkEventCrossing *event);
121 static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
122 static void gtk_menu_show_all (GtkWidget *widget);
123 static void gtk_menu_hide_all (GtkWidget *widget);
124 static void gtk_menu_position (GtkMenu *menu);
125 static void gtk_menu_reparent (GtkMenu *menu,
126 GtkWidget *new_parent,
128 static void gtk_menu_remove (GtkContainer *menu,
131 static GtkMenuShellClass *parent_class = NULL;
132 static const gchar *attach_data_key = "gtk-menu-attach-data";
135 gtk_menu_get_type (void)
137 static GtkType menu_type = 0;
141 static const GtkTypeInfo menu_info =
145 sizeof (GtkMenuClass),
146 (GtkClassInitFunc) gtk_menu_class_init,
147 (GtkObjectInitFunc) gtk_menu_init,
148 /* reserved_1 */ NULL,
149 /* reserved_2 */ NULL,
150 (GtkClassInitFunc) NULL,
153 menu_type = gtk_type_unique (gtk_menu_shell_get_type (), &menu_info);
160 gtk_menu_class_init (GtkMenuClass *class)
162 GObjectClass *gobject_class;
163 GtkObjectClass *object_class;
164 GtkWidgetClass *widget_class;
165 GtkContainerClass *container_class;
166 GtkMenuShellClass *menu_shell_class;
168 GtkBindingSet *binding_set;
170 gobject_class = (GObjectClass*) class;
171 object_class = (GtkObjectClass*) class;
172 widget_class = (GtkWidgetClass*) class;
173 container_class = (GtkContainerClass*) class;
174 menu_shell_class = (GtkMenuShellClass*) class;
175 parent_class = gtk_type_class (gtk_menu_shell_get_type ());
177 gobject_class->set_property = gtk_menu_set_property;
178 gobject_class->get_property = gtk_menu_get_property;
180 g_object_class_install_property (gobject_class,
182 g_param_spec_string ("tearoff-title",
184 _("A title that may be displayed by the window manager when this menu is torn-off."),
186 G_PARAM_READABLE | G_PARAM_WRITABLE));
187 object_class->destroy = gtk_menu_destroy;
189 widget_class->realize = gtk_menu_realize;
190 widget_class->unrealize = gtk_menu_unrealize;
191 widget_class->size_request = gtk_menu_size_request;
192 widget_class->size_allocate = gtk_menu_size_allocate;
193 widget_class->expose_event = gtk_menu_expose;
194 widget_class->key_press_event = gtk_menu_key_press;
195 widget_class->motion_notify_event = gtk_menu_motion_notify;
196 widget_class->show_all = gtk_menu_show_all;
197 widget_class->hide_all = gtk_menu_hide_all;
198 widget_class->enter_notify_event = gtk_menu_enter_notify;
199 widget_class->leave_notify_event = gtk_menu_leave_notify;
201 container_class->remove = gtk_menu_remove;
203 menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
204 menu_shell_class->deactivate = gtk_menu_deactivate;
205 menu_shell_class->select_item = gtk_menu_select_item;
206 menu_shell_class->insert = gtk_menu_real_insert;
208 binding_set = gtk_binding_set_by_class (class);
209 gtk_binding_entry_add_signal (binding_set,
212 GTK_TYPE_MENU_DIRECTION_TYPE,
214 gtk_binding_entry_add_signal (binding_set,
217 GTK_TYPE_MENU_DIRECTION_TYPE,
219 gtk_binding_entry_add_signal (binding_set,
222 GTK_TYPE_MENU_DIRECTION_TYPE,
224 gtk_binding_entry_add_signal (binding_set,
227 GTK_TYPE_MENU_DIRECTION_TYPE,
229 gtk_binding_entry_add_signal (binding_set,
232 GTK_TYPE_MENU_DIRECTION_TYPE,
233 GTK_MENU_DIR_PARENT);
234 gtk_binding_entry_add_signal (binding_set,
237 GTK_TYPE_MENU_DIRECTION_TYPE,
238 GTK_MENU_DIR_PARENT);
239 gtk_binding_entry_add_signal (binding_set,
242 GTK_TYPE_MENU_DIRECTION_TYPE,
244 gtk_binding_entry_add_signal (binding_set,
247 GTK_TYPE_MENU_DIRECTION_TYPE,
253 gtk_menu_set_property (GObject *object,
260 menu = GTK_MENU (object);
264 case PROP_TEAROFF_TITLE:
265 gtk_menu_set_title (menu, g_value_get_string (value));
268 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
274 gtk_menu_get_property (GObject *object,
281 menu = GTK_MENU (object);
285 case PROP_TEAROFF_TITLE:
286 g_value_set_string (value, gtk_object_get_data (GTK_OBJECT (menu),
290 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
296 gtk_menu_window_event (GtkWidget *window,
300 gboolean handled = FALSE;
302 gtk_widget_ref (window);
303 gtk_widget_ref (menu);
308 case GDK_KEY_RELEASE:
309 handled = gtk_widget_event (menu, event);
315 gtk_widget_unref (window);
316 gtk_widget_unref (menu);
322 gtk_menu_init (GtkMenu *menu)
324 menu->parent_menu_item = NULL;
325 menu->old_active_menu_item = NULL;
326 menu->accel_group = NULL;
327 menu->position_func = NULL;
328 menu->position_func_data = NULL;
329 menu->toggle_size = 0;
331 menu->toplevel = g_object_connect (gtk_widget_new (GTK_TYPE_WINDOW,
332 "type", GTK_WINDOW_POPUP,
335 "signal::event", gtk_menu_window_event, menu,
336 "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
338 gtk_window_set_policy (GTK_WINDOW (menu->toplevel),
340 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
342 /* Refloat the menu, so that reference counting for the menu isn't
343 * affected by it being a child of the toplevel
345 GTK_WIDGET_SET_FLAGS (menu, GTK_FLOATING);
346 menu->needs_destruction_ref_count = TRUE;
348 menu->view_window = NULL;
349 menu->bin_window = NULL;
351 menu->scroll_offset = 0;
352 menu->scroll_step = 0;
353 menu->timeout_id = 0;
354 menu->scroll_fast = FALSE;
356 menu->tearoff_window = NULL;
357 menu->tearoff_hbox = NULL;
358 menu->torn_off = FALSE;
359 menu->tearoff_active = FALSE;
360 menu->tearoff_adjustment = NULL;
361 menu->tearoff_scrollbar = NULL;
363 menu->upper_arrow_visible = FALSE;
364 menu->lower_arrow_visible = FALSE;
365 menu->upper_arrow_prelight = FALSE;
366 menu->lower_arrow_prelight = FALSE;
368 MENU_NEEDS_RESIZE (menu) = TRUE;
372 gtk_menu_destroy (GtkObject *object)
375 GtkMenuAttachData *data;
377 g_return_if_fail (GTK_IS_MENU (object));
379 menu = GTK_MENU (object);
381 gtk_menu_stop_scrolling (menu);
383 data = gtk_object_get_data (object, attach_data_key);
385 gtk_menu_detach (menu);
387 gtk_menu_stop_navigating_submenu (menu);
389 gtk_menu_set_accel_group (menu, NULL);
391 if (menu->old_active_menu_item)
393 gtk_widget_unref (menu->old_active_menu_item);
394 menu->old_active_menu_item = NULL;
397 /* Add back the reference count for being a child */
398 if (menu->needs_destruction_ref_count)
400 menu->needs_destruction_ref_count = FALSE;
401 gtk_object_ref (object);
405 gtk_widget_destroy (menu->toplevel);
406 if (menu->tearoff_window)
407 gtk_widget_destroy (menu->tearoff_window);
409 GTK_OBJECT_CLASS (parent_class)->destroy (object);
414 gtk_menu_attach_to_widget (GtkMenu *menu,
415 GtkWidget *attach_widget,
416 GtkMenuDetachFunc detacher)
418 GtkMenuAttachData *data;
420 g_return_if_fail (GTK_IS_MENU (menu));
421 g_return_if_fail (GTK_IS_WIDGET (attach_widget));
422 g_return_if_fail (detacher != NULL);
424 /* keep this function in sync with gtk_widget_set_parent()
427 data = gtk_object_get_data (GTK_OBJECT (menu), attach_data_key);
430 g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
431 gtk_type_name (GTK_OBJECT_TYPE (data->attach_widget)));
435 gtk_object_ref (GTK_OBJECT (menu));
436 gtk_object_sink (GTK_OBJECT (menu));
438 data = g_new (GtkMenuAttachData, 1);
439 data->attach_widget = attach_widget;
440 data->detacher = detacher;
441 gtk_object_set_data (GTK_OBJECT (menu), attach_data_key, data);
443 if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
444 gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
446 /* we don't need to set the style here, since
447 * we are a toplevel widget.
452 gtk_menu_get_attach_widget (GtkMenu *menu)
454 GtkMenuAttachData *data;
456 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
458 data = gtk_object_get_data (GTK_OBJECT (menu), attach_data_key);
460 return data->attach_widget;
465 gtk_menu_detach (GtkMenu *menu)
467 GtkMenuAttachData *data;
469 g_return_if_fail (GTK_IS_MENU (menu));
471 /* keep this function in sync with gtk_widget_unparent()
473 data = gtk_object_get_data (GTK_OBJECT (menu), attach_data_key);
476 g_warning ("gtk_menu_detach(): menu is not attached");
479 gtk_object_remove_data (GTK_OBJECT (menu), attach_data_key);
481 data->detacher (data->attach_widget, menu);
483 if (GTK_WIDGET_REALIZED (menu))
484 gtk_widget_unrealize (GTK_WIDGET (menu));
488 gtk_widget_unref (GTK_WIDGET (menu));
492 gtk_menu_remove (GtkContainer *container,
496 g_return_if_fail (GTK_IS_MENU (container));
497 g_return_if_fail (GTK_IS_MENU_ITEM (widget));
499 menu = GTK_MENU (container);
501 /* Clear out old_active_menu_item if it matches the item we are removing
503 if (menu->old_active_menu_item == widget)
505 gtk_widget_unref (menu->old_active_menu_item);
506 menu->old_active_menu_item = NULL;
509 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
516 return GTK_WIDGET (gtk_type_new (gtk_menu_get_type ()));
520 gtk_menu_real_insert (GtkMenuShell *menu_shell,
524 if (GTK_WIDGET_REALIZED (menu_shell))
525 gtk_widget_set_parent_window (child, GTK_MENU (menu_shell)->bin_window);
527 GTK_MENU_SHELL_CLASS (parent_class)->insert (menu_shell, child, position);
531 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
536 widget = GTK_WIDGET (menu);
542 GdkGCValues gc_values;
544 menu->tearoff_active = FALSE;
545 menu->saved_scroll_offset = menu->scroll_offset;
547 gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
548 gc = gdk_gc_new_with_values (widget->window,
549 &gc_values, GDK_GC_SUBWINDOW);
551 gdk_window_get_size (menu->tearoff_window->window, &width, &height);
553 pixmap = gdk_pixmap_new (menu->tearoff_window->window,
558 gdk_draw_pixmap (pixmap, gc,
559 menu->tearoff_window->window,
563 gtk_widget_set_usize (menu->tearoff_window,
567 gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
568 gdk_pixmap_unref (pixmap);
573 gtk_menu_popup (GtkMenu *menu,
574 GtkWidget *parent_menu_shell,
575 GtkWidget *parent_menu_item,
576 GtkMenuPositionFunc func,
579 guint32 activate_time)
582 GtkWidget *xgrab_shell;
584 GdkEvent *current_event;
585 GtkMenuShell *menu_shell;
587 g_return_if_fail (GTK_IS_MENU (menu));
589 widget = GTK_WIDGET (menu);
590 menu_shell = GTK_MENU_SHELL (menu);
592 menu_shell->parent_menu_shell = parent_menu_shell;
593 menu_shell->active = TRUE;
594 menu_shell->button = button;
596 /* If we are popping up the menu from something other than, a button
597 * press then, as a heuristic, we ignore enter events for the menu
598 * until we get a MOTION_NOTIFY.
601 current_event = gtk_get_current_event ();
604 if ((current_event->type != GDK_BUTTON_PRESS) &&
605 (current_event->type != GDK_ENTER_NOTIFY))
606 menu_shell->ignore_enter = TRUE;
611 gtk_menu_tearoff_bg_copy (menu);
613 gtk_menu_reparent (menu, menu->toplevel, FALSE);
616 menu->parent_menu_item = parent_menu_item;
617 menu->position_func = func;
618 menu->position_func_data = data;
619 menu_shell->activate_time = activate_time;
621 gtk_menu_position (menu);
623 /* We need to show the menu _here_ because code expects to be
624 * able to tell if the menu is onscreen by looking at the
625 * GTK_WIDGET_VISIBLE (menu)
627 gtk_widget_show (GTK_WIDGET (menu));
628 gtk_widget_show (menu->toplevel);
632 /* Also, if we're popping up from a key event, select the first
633 * item in the menu. Bad hack, but no better way to do it
634 * in current menu framework.
636 if (current_event->type == GDK_KEY_PRESS &&
637 GTK_MENU_SHELL (menu)->children)
639 gtk_menu_shell_select_item (GTK_MENU_SHELL (menu),
640 GTK_MENU_SHELL (menu)->children->data);
643 gdk_event_free (current_event);
646 gtk_menu_scroll_to (menu, menu->scroll_offset);
648 /* Find the last viewable ancestor, and make an X grab on it
650 parent = GTK_WIDGET (menu);
654 gboolean viewable = TRUE;
655 GtkWidget *tmp = parent;
659 if (!GTK_WIDGET_MAPPED (tmp))
668 xgrab_shell = parent;
670 parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
673 if (xgrab_shell && (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab))
675 if ((gdk_pointer_grab (xgrab_shell->window, TRUE,
676 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
677 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
678 GDK_POINTER_MOTION_MASK,
679 NULL, NULL, activate_time) == 0))
681 if (gdk_keyboard_grab (xgrab_shell->window, TRUE,
683 GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
685 gdk_pointer_ungrab (activate_time);
689 gtk_grab_add (GTK_WIDGET (menu));
693 gtk_menu_popdown (GtkMenu *menu)
695 GtkMenuShell *menu_shell;
697 g_return_if_fail (GTK_IS_MENU (menu));
699 menu_shell = GTK_MENU_SHELL (menu);
701 menu_shell->parent_menu_shell = NULL;
702 menu_shell->active = FALSE;
703 menu_shell->ignore_enter = FALSE;
705 gtk_menu_stop_scrolling (menu);
707 gtk_menu_stop_navigating_submenu (menu);
709 if (menu_shell->active_menu_item)
711 if (menu->old_active_menu_item)
712 gtk_widget_unref (menu->old_active_menu_item);
713 menu->old_active_menu_item = menu_shell->active_menu_item;
714 gtk_widget_ref (menu->old_active_menu_item);
717 gtk_menu_shell_deselect (menu_shell);
719 /* The X Grab, if present, will automatically be removed when we hide
721 gtk_widget_hide (menu->toplevel);
726 gdk_window_get_size (menu->tearoff_window->window, &width, &height);
727 gtk_widget_set_usize (menu->tearoff_window,
731 if (GTK_BIN (menu->toplevel)->child)
733 gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
737 /* We popped up the menu from the tearoff, so we need to
738 * release the grab - we aren't actually hiding the menu.
740 if (menu_shell->have_xgrab)
742 gdk_pointer_ungrab (GDK_CURRENT_TIME);
743 gdk_keyboard_ungrab (GDK_CURRENT_TIME);
747 /* gtk_menu_popdown is called each time a menu item is selected from
748 * a torn off menu. Only scroll back to the saved position if the
749 * non-tearoff menu was popped down.
751 if (!menu->tearoff_active)
752 gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
753 menu->tearoff_active = TRUE;
756 gtk_widget_hide (GTK_WIDGET (menu));
758 menu_shell->have_xgrab = FALSE;
759 gtk_grab_remove (GTK_WIDGET (menu));
763 gtk_menu_get_active (GtkMenu *menu)
768 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
770 if (!menu->old_active_menu_item)
773 children = GTK_MENU_SHELL (menu)->children;
777 child = children->data;
778 children = children->next;
780 if (GTK_BIN (child)->child)
785 menu->old_active_menu_item = child;
786 if (menu->old_active_menu_item)
787 gtk_widget_ref (menu->old_active_menu_item);
790 return menu->old_active_menu_item;
794 gtk_menu_set_active (GtkMenu *menu,
800 g_return_if_fail (GTK_IS_MENU (menu));
802 tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
805 child = tmp_list->data;
806 if (GTK_BIN (child)->child)
808 if (menu->old_active_menu_item)
809 gtk_widget_unref (menu->old_active_menu_item);
810 menu->old_active_menu_item = child;
811 gtk_widget_ref (menu->old_active_menu_item);
817 gtk_menu_set_accel_group (GtkMenu *menu,
818 GtkAccelGroup *accel_group)
820 g_return_if_fail (GTK_IS_MENU (menu));
822 if (menu->accel_group != accel_group)
824 if (menu->accel_group)
825 gtk_accel_group_unref (menu->accel_group);
826 menu->accel_group = accel_group;
827 if (menu->accel_group)
828 gtk_accel_group_ref (menu->accel_group);
833 gtk_menu_get_accel_group (GtkMenu *menu)
835 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
837 return menu->accel_group;
841 gtk_menu_reposition (GtkMenu *menu)
843 g_return_if_fail (GTK_IS_MENU (menu));
845 if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
846 gtk_menu_position (menu);
850 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
853 g_return_if_fail (GTK_IS_MENU (menu));
855 if (adjustment->value != menu->scroll_offset)
856 gtk_menu_scroll_to (menu, adjustment->value);
860 gtk_menu_set_tearoff_hints (GtkMenu *menu,
863 GdkGeometry geometry_hints;
865 if (!menu->tearoff_window)
868 geometry_hints.min_width = width;
869 geometry_hints.max_width = width;
871 geometry_hints.min_height = 0;
872 geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
873 gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
876 GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
881 gtk_menu_set_tearoff_state (GtkMenu *menu,
886 g_return_if_fail (GTK_IS_MENU (menu));
888 if (menu->torn_off != torn_off)
890 menu->torn_off = torn_off;
891 menu->tearoff_active = torn_off;
895 if (GTK_WIDGET_VISIBLE (menu))
896 gtk_menu_popdown (menu);
898 if (!menu->tearoff_window)
900 GtkWidget *attach_widget;
903 menu->tearoff_window = g_object_connect (gtk_widget_new (GTK_TYPE_WINDOW,
904 "type", GTK_WINDOW_TOPLEVEL,
906 "signal::destroy", gtk_widget_destroyed, &menu->tearoff_window,
908 gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
909 GDK_WINDOW_TYPE_HINT_MENU);
910 gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
911 gtk_widget_set_app_paintable (menu->tearoff_window, TRUE);
912 gtk_signal_connect (GTK_OBJECT (menu->tearoff_window),
914 GTK_SIGNAL_FUNC (gtk_menu_window_event),
916 gtk_widget_realize (menu->tearoff_window);
918 title = gtk_object_get_data (GTK_OBJECT (menu), "gtk-menu-title");
921 attach_widget = gtk_menu_get_attach_widget (menu);
922 if (GTK_IS_MENU_ITEM (attach_widget))
924 GtkWidget *child = GTK_BIN (attach_widget)->child;
925 if (GTK_IS_LABEL (child))
926 gtk_label_get (GTK_LABEL (child), &title);
931 gdk_window_set_title (menu->tearoff_window->window, title);
933 gdk_window_set_decorations (menu->tearoff_window->window,
938 gtk_window_set_policy (GTK_WINDOW (menu->tearoff_window),
941 menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
942 gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
944 gdk_window_get_size (GTK_WIDGET (menu)->window, &width, &height);
945 menu->tearoff_adjustment =
946 GTK_ADJUSTMENT (gtk_adjustment_new (0,
948 GTK_WIDGET (menu)->requisition.height,
952 g_object_connect (GTK_OBJECT (menu->tearoff_adjustment),
953 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
955 menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
957 gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
958 menu->tearoff_scrollbar,
961 if (menu->tearoff_adjustment->upper > height)
962 gtk_widget_show (menu->tearoff_scrollbar);
964 gtk_widget_show (menu->tearoff_hbox);
967 gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
969 gdk_window_get_size (GTK_WIDGET (menu)->window, &width, &height);
970 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
971 width += menu->tearoff_scrollbar->requisition.width;
973 gtk_menu_set_tearoff_hints (menu, width);
975 gtk_widget_realize (menu->tearoff_window);
976 gtk_menu_position (menu);
978 gtk_widget_show (GTK_WIDGET (menu));
979 gtk_widget_show (menu->tearoff_window);
981 gtk_menu_scroll_to (menu, 0);
986 gtk_widget_hide (menu->tearoff_window);
987 gtk_menu_reparent (menu, menu->toplevel, FALSE);
993 * gtk_menu_get_tearoff_state:
996 * Returns whether the menu is torn off. See
997 * gtk_menu_set_tearoff_state ().
999 * Return value: %TRUE if the menu is currently torn off.
1002 gtk_menu_get_tearoff_state (GtkMenu *menu)
1004 g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
1006 return menu->torn_off;
1010 gtk_menu_set_title (GtkMenu *menu,
1013 g_return_if_fail (GTK_IS_MENU (menu));
1015 gtk_object_set_data_full (GTK_OBJECT (menu), "gtk-menu-title",
1016 g_strdup (title), (GtkDestroyNotify) g_free);
1017 g_object_notify (G_OBJECT (menu), "tearoff_title");
1021 * gtk_menu_get_title:
1024 * Returns the title of the menu. See gtk_menu_set_title().
1026 * Return value: the title of the menu, or %NULL if the menu has no
1029 G_CONST_RETURN gchar *
1030 gtk_menu_get_title (GtkMenu *menu)
1032 g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1034 return gtk_object_get_data (GTK_OBJECT (menu), "gtk-menu-title");
1038 gtk_menu_reorder_child (GtkMenu *menu,
1042 GtkMenuShell *menu_shell;
1043 g_return_if_fail (GTK_IS_MENU (menu));
1044 g_return_if_fail (GTK_IS_MENU_ITEM (child));
1045 menu_shell = GTK_MENU_SHELL (menu);
1046 if (g_list_find (menu_shell->children, child))
1048 menu_shell->children = g_list_remove (menu_shell->children, child);
1049 menu_shell->children = g_list_insert (menu_shell->children, child, position);
1050 if (GTK_WIDGET_VISIBLE (menu_shell))
1051 gtk_widget_queue_resize (GTK_WIDGET (menu_shell));
1056 gtk_menu_realize (GtkWidget *widget)
1058 GdkWindowAttr attributes;
1059 gint attributes_mask;
1065 g_return_if_fail (GTK_IS_MENU (widget));
1067 menu = GTK_MENU (widget);
1069 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1071 attributes.window_type = GDK_WINDOW_CHILD;
1072 attributes.x = widget->allocation.x;
1073 attributes.y = widget->allocation.y;
1074 attributes.width = widget->allocation.width;
1075 attributes.height = widget->allocation.height;
1076 attributes.wclass = GDK_INPUT_OUTPUT;
1077 attributes.visual = gtk_widget_get_visual (widget);
1078 attributes.colormap = gtk_widget_get_colormap (widget);
1080 attributes.event_mask = gtk_widget_get_events (widget);
1081 attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
1082 GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
1084 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1085 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1086 gdk_window_set_user_data (widget->window, widget);
1088 border_width = GTK_CONTAINER (widget)->border_width;
1090 attributes.x = border_width + widget->style->xthickness;
1091 attributes.y = border_width + widget->style->ythickness;
1092 attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
1093 attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
1095 if (menu->upper_arrow_visible)
1097 attributes.y += MENU_SCROLL_ARROW_HEIGHT;
1098 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1100 if (menu->lower_arrow_visible)
1101 attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1103 menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
1104 gdk_window_set_user_data (menu->view_window, menu);
1108 attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness) * 2);
1110 menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
1111 gdk_window_set_user_data (menu->bin_window, menu);
1113 children = GTK_MENU_SHELL (menu)->children;
1116 child = children->data;
1117 children = children->next;
1119 gtk_widget_set_parent_window (child, menu->bin_window);
1122 widget->style = gtk_style_attach (widget->style, widget->window);
1123 gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1124 gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1125 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1127 gtk_menu_paint (widget);
1129 gdk_window_show (menu->bin_window);
1130 gdk_window_show (menu->view_window);
1134 gtk_menu_unrealize (GtkWidget *widget)
1138 g_return_if_fail (GTK_IS_MENU (widget));
1140 menu = GTK_MENU (widget);
1142 gdk_window_set_user_data (menu->view_window, NULL);
1143 gdk_window_destroy (menu->view_window);
1144 menu->view_window = NULL;
1146 gdk_window_set_user_data (menu->bin_window, NULL);
1147 gdk_window_destroy (menu->bin_window);
1148 menu->bin_window = NULL;
1150 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1154 gtk_menu_size_request (GtkWidget *widget,
1155 GtkRequisition *requisition)
1158 GtkMenuShell *menu_shell;
1161 guint max_toggle_size;
1162 guint max_accel_width;
1163 GtkRequisition child_requisition;
1166 g_return_if_fail (GTK_IS_MENU (widget));
1167 g_return_if_fail (requisition != NULL);
1169 menu = GTK_MENU (widget);
1170 menu_shell = GTK_MENU_SHELL (widget);
1172 requisition->width = 0;
1173 requisition->height = 0;
1175 max_toggle_size = 0;
1176 max_accel_width = 0;
1178 children = menu_shell->children;
1181 child = children->data;
1182 children = children->next;
1184 if (GTK_WIDGET_VISIBLE (child))
1188 /* It's important to size_request the child
1189 * before doing the toggle size request, in
1190 * case the toggle size request depends on the size
1191 * request of a child of the child (e.g. for ImageMenuItem)
1194 GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
1195 gtk_widget_size_request (child, &child_requisition);
1197 requisition->width = MAX (requisition->width, child_requisition.width);
1198 requisition->height += child_requisition.height;
1200 gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
1201 max_toggle_size = MAX (max_toggle_size, toggle_size);
1202 max_accel_width = MAX (max_accel_width, GTK_MENU_ITEM (child)->accelerator_width);
1206 requisition->width += max_toggle_size + max_accel_width;
1207 requisition->width += (GTK_CONTAINER (menu)->border_width +
1208 widget->style->xthickness) * 2;
1209 requisition->height += (GTK_CONTAINER (menu)->border_width +
1210 widget->style->ythickness) * 2;
1212 menu->toggle_size = max_toggle_size;
1214 /* If the requested width was different than the allocated width, we need to change
1215 * the geometry hints for the tear off window so that the window can actually be resized.
1216 * Don't resize the tearoff if it is not active, beacuse it won't redraw (it is only a background pixmap).
1218 if ((requisition->width != GTK_WIDGET (menu)->allocation.width) && menu->tearoff_active)
1220 width = requisition->width;
1221 if (menu->tearoff_scrollbar && GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1222 width += menu->tearoff_scrollbar->requisition.width;
1223 gtk_menu_set_tearoff_hints (menu, width);
1228 gtk_menu_size_allocate (GtkWidget *widget,
1229 GtkAllocation *allocation)
1232 GtkMenuShell *menu_shell;
1234 GtkAllocation child_allocation;
1239 g_return_if_fail (GTK_IS_MENU (widget));
1240 g_return_if_fail (allocation != NULL);
1242 menu = GTK_MENU (widget);
1243 menu_shell = GTK_MENU_SHELL (widget);
1245 widget->allocation = *allocation;
1247 x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness;
1248 y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness;
1250 width = MAX (1, allocation->width - x * 2);
1251 height = MAX (1, allocation->height - y * 2);
1253 if (menu->upper_arrow_visible && !menu->tearoff_active)
1255 y += MENU_SCROLL_ARROW_HEIGHT;
1256 height -= MENU_SCROLL_ARROW_HEIGHT;
1259 if (menu->lower_arrow_visible && !menu->tearoff_active)
1260 height -= MENU_SCROLL_ARROW_HEIGHT;
1262 if (GTK_WIDGET_REALIZED (widget))
1264 gdk_window_move_resize (widget->window,
1265 allocation->x, allocation->y,
1266 allocation->width, allocation->height);
1268 gdk_window_move_resize (menu->view_window,
1275 if (menu_shell->children)
1277 child_allocation.x = 0;
1278 child_allocation.y = 0;
1279 child_allocation.width = width;
1281 children = menu_shell->children;
1284 child = children->data;
1285 children = children->next;
1287 if (GTK_WIDGET_VISIBLE (child))
1289 GtkRequisition child_requisition;
1290 gtk_widget_get_child_requisition (child, &child_requisition);
1292 child_allocation.height = child_requisition.height;
1294 gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
1296 gtk_widget_size_allocate (child, &child_allocation);
1297 gtk_widget_queue_draw (child);
1299 child_allocation.y += child_allocation.height;
1303 /* Resize the item window */
1304 if (GTK_WIDGET_REALIZED (widget))
1306 gdk_window_resize (menu->bin_window,
1307 child_allocation.width,
1308 child_allocation.y);
1312 if (menu->tearoff_active)
1314 if (allocation->height >= widget->requisition.height)
1316 if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1318 gtk_widget_hide (menu->tearoff_scrollbar);
1319 gtk_menu_set_tearoff_hints (menu, allocation->width);
1320 gtk_widget_set_usize (menu->tearoff_window, -1, allocation->height);
1322 gtk_menu_scroll_to (menu, 0);
1327 menu->tearoff_adjustment->upper = widget->requisition.height;
1328 menu->tearoff_adjustment->page_size = allocation->height;
1330 if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
1331 menu->tearoff_adjustment->upper)
1334 value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
1337 gtk_menu_scroll_to (menu, value);
1340 gtk_adjustment_changed (menu->tearoff_adjustment);
1342 if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1344 gtk_menu_set_tearoff_hints (menu,
1345 allocation->width + menu->tearoff_scrollbar->requisition.width);
1346 gtk_widget_show (menu->tearoff_scrollbar);
1347 gtk_widget_set_usize (menu->tearoff_window, -1, allocation->height);
1355 gtk_menu_paint (GtkWidget *widget)
1364 g_return_if_fail (GTK_IS_MENU (widget));
1366 menu = GTK_MENU (widget);
1368 if (GTK_WIDGET_DRAWABLE (widget))
1370 gtk_paint_box (widget->style,
1374 NULL, widget, "menu",
1377 border_x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness;
1378 border_y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness;
1379 gdk_window_get_size (widget->window, &width, &height);
1381 if (menu->upper_arrow_visible && !menu->tearoff_active)
1383 gtk_paint_box (widget->style,
1385 menu->upper_arrow_prelight ?
1386 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1388 NULL, widget, "menu",
1392 MENU_SCROLL_ARROW_HEIGHT);
1394 gtk_paint_arrow (widget->style,
1396 menu->upper_arrow_prelight ?
1397 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1399 NULL, widget, "menu",
1402 width / 2 - MENU_SCROLL_ARROW_HEIGHT / 2 + 1,
1404 MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2,
1405 MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2);
1408 if (menu->lower_arrow_visible && !menu->tearoff_active)
1410 gtk_paint_box (widget->style,
1412 menu->lower_arrow_prelight ?
1413 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1415 NULL, widget, "menu",
1417 height - border_y - MENU_SCROLL_ARROW_HEIGHT + 1,
1419 MENU_SCROLL_ARROW_HEIGHT);
1421 gtk_paint_arrow (widget->style,
1423 menu->lower_arrow_prelight ?
1424 GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1426 NULL, widget, "menu",
1429 width / 2 - MENU_SCROLL_ARROW_HEIGHT / 2 + 1,
1430 height - MENU_SCROLL_ARROW_HEIGHT + 1,
1431 MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2,
1432 MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2);
1435 if (menu->scroll_offset < 0)
1436 gtk_paint_box (widget->style,
1440 NULL, widget, "menu",
1443 -menu->scroll_offset);
1445 menu_height = widget->requisition.height - 2*border_y;
1446 top_pos = height - 2*border_y - (menu->upper_arrow_visible ? MENU_SCROLL_ARROW_HEIGHT : 0);
1448 if (menu_height - menu->scroll_offset < top_pos)
1449 gtk_paint_box (widget->style,
1453 NULL, widget, "menu",
1455 menu_height - menu->scroll_offset,
1457 top_pos - (menu_height - menu->scroll_offset));
1462 gtk_menu_expose (GtkWidget *widget,
1463 GdkEventExpose *event)
1465 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
1466 g_return_val_if_fail (event != NULL, FALSE);
1468 if (GTK_WIDGET_DRAWABLE (widget))
1470 gtk_menu_paint (widget);
1472 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
1479 gtk_menu_key_press (GtkWidget *widget,
1482 GtkMenuShell *menu_shell;
1484 gboolean delete = FALSE;
1485 gchar *accel = NULL;
1487 g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
1488 g_return_val_if_fail (event != NULL, FALSE);
1490 menu_shell = GTK_MENU_SHELL (widget);
1491 menu = GTK_MENU (widget);
1493 gtk_menu_stop_navigating_submenu (menu);
1495 if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
1498 g_object_get (G_OBJECT (gtk_settings_get_default ()),
1499 "gtk-menu-bar-accel",
1506 GdkModifierType mods = 0;
1507 gboolean handled = FALSE;
1509 gtk_accelerator_parse (accel, &keyval, &mods);
1512 g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
1514 /* FIXME this is wrong, needs to be in the global accel resolution
1515 * thing, to properly consider i18n etc., but that probably requires
1516 * AccelGroup changes etc.
1518 if (event->keyval == keyval &&
1519 (mods & event->state) == mods)
1521 gtk_signal_emit_by_name (GTK_OBJECT (menu), "cancel");
1530 switch (event->keyval)
1541 /* Modify the accelerators */
1542 if (menu_shell->active_menu_item &&
1543 GTK_BIN (menu_shell->active_menu_item)->child &&
1544 GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL &&
1545 !gtk_widget_accelerators_locked (menu_shell->active_menu_item) &&
1547 (gtk_accelerator_valid (event->keyval, event->state) &&
1549 (event->keyval >= GDK_F1 && event->keyval <= GDK_F35)))))
1551 GtkMenuItem *menu_item;
1552 GtkAccelGroup *accel_group;
1554 menu_item = GTK_MENU_ITEM (menu_shell->active_menu_item);
1556 if (!GTK_MENU (widget)->accel_group)
1557 accel_group = gtk_accel_group_get_default ();
1559 accel_group = GTK_MENU (widget)->accel_group;
1561 gtk_widget_remove_accelerators (GTK_WIDGET (menu_item),
1562 gtk_signal_name (menu_item->accelerator_signal),
1566 0 == gtk_widget_accelerator_signal (GTK_WIDGET (menu_item),
1573 slist = gtk_accel_group_entries_from_object (G_OBJECT (menu_item));
1576 GtkAccelEntry *ac_entry;
1578 ac_entry = slist->data;
1580 if (ac_entry->signal_id == menu_item->accelerator_signal)
1583 slist = slist->next;
1587 gtk_widget_add_accelerator (GTK_WIDGET (menu_item),
1588 gtk_signal_name (menu_item->accelerator_signal),
1600 gtk_menu_motion_notify (GtkWidget *widget,
1601 GdkEventMotion *event)
1603 GtkWidget *menu_item;
1605 GtkMenuShell *menu_shell;
1607 gboolean need_enter;
1610 if (GTK_IS_MENU (widget))
1611 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
1613 /* We received the event for one of two reasons:
1615 * a) We are the active menu, and did gtk_grab_add()
1616 * b) The widget is a child of ours, and the event was propagated
1618 * Since for computation of navigation regions, we want the menu which
1619 * is the parent of the menu item, for a), we need to find that menu,
1620 * which may be different from 'widget'.
1622 menu_item = gtk_get_event_widget ((GdkEvent*) event);
1623 if (!menu_item || !GTK_IS_MENU_ITEM (menu_item) || !GTK_WIDGET_IS_SENSITIVE (menu_item) ||
1624 !GTK_IS_MENU (menu_item->parent))
1627 menu_shell = GTK_MENU_SHELL (menu_item->parent);
1628 menu = GTK_MENU (menu_shell);
1630 need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
1632 /* Check to see if we are within an active submenu's navigation region
1634 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
1639 /* The menu is now sensitive to enter events on its items, but
1640 * was previously sensitive. So we fake an enter event.
1644 menu_shell->ignore_enter = FALSE;
1646 gdk_window_get_size (event->window, &width, &height);
1647 if (event->x >= 0 && event->x < width &&
1648 event->y >= 0 && event->y < height)
1650 GdkEvent send_event;
1652 memset (&send_event, 0, sizeof (send_event));
1653 send_event.crossing.type = GDK_ENTER_NOTIFY;
1654 send_event.crossing.window = event->window;
1655 send_event.crossing.time = event->time;
1656 send_event.crossing.send_event = TRUE;
1657 send_event.crossing.x_root = event->x_root;
1658 send_event.crossing.y_root = event->y_root;
1659 send_event.crossing.x = event->x;
1660 send_event.crossing.y = event->y;
1662 /* We send the event to 'widget', the currently active menu,
1663 * instead of 'menu', the menu that the pointer is in. This
1664 * will ensure that the event will be ignored unless the
1665 * menuitem is a child of the active menu or some parent
1666 * menu of the active menu.
1668 return gtk_widget_event (widget, &send_event);
1676 gtk_menu_scroll_timeout (gpointer data)
1681 gint view_width, view_height;
1683 GDK_THREADS_ENTER ();
1685 menu = GTK_MENU (data);
1686 widget = GTK_WIDGET (menu);
1688 offset = menu->scroll_offset + menu->scroll_step;
1690 /* If we scroll upward and the non-visible top part
1691 * is smaller than the scroll arrow it would be
1692 * pretty stupid to show the arrow and taking more
1693 * screen space than just scrolling to the top.
1695 if ((menu->scroll_step < 0) && (offset < MENU_SCROLL_ARROW_HEIGHT))
1698 /* Don't scroll over the top if we weren't before: */
1699 if ((menu->scroll_offset >= 0) && (offset < 0))
1702 gdk_window_get_size (widget->window, &view_width, &view_height);
1704 /* Don't scroll past the bottom if we weren't before: */
1705 if (menu->scroll_offset > 0)
1706 view_height -= MENU_SCROLL_ARROW_HEIGHT;
1708 if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
1709 (offset + view_height > widget->requisition.height))
1710 offset = widget->requisition.height - view_height;
1712 gtk_menu_scroll_to (menu, offset);
1714 GDK_THREADS_LEAVE ();
1720 gtk_menu_handle_scrolling (GtkMenu *menu, gboolean enter)
1722 GtkMenuShell *menu_shell;
1728 gboolean scroll_fast = FALSE;
1730 menu_shell = GTK_MENU_SHELL (menu);
1732 gdk_window_get_pointer (GTK_WIDGET (menu)->window, &x, &y, NULL);
1733 gdk_window_get_size (GTK_WIDGET (menu)->window, &width, &height);
1735 border = GTK_CONTAINER (menu)->border_width + GTK_WIDGET (menu)->style->ythickness;
1737 if (menu->upper_arrow_visible && !menu->tearoff_active)
1742 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
1745 if ((x >= rect.x) && (x < rect.x + rect.width) &&
1746 (y >= rect.y) && (y < rect.y + rect.height))
1749 scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
1752 if (enter && in_arrow &&
1753 (!menu->upper_arrow_prelight || menu->scroll_fast != scroll_fast))
1755 menu->upper_arrow_prelight = TRUE;
1756 menu->scroll_fast = scroll_fast;
1757 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
1759 /* Deselect the active item so that any submenus are poped down */
1760 gtk_menu_shell_deselect (menu_shell);
1762 gtk_menu_stop_scrolling (menu);
1763 menu->scroll_step = -MENU_SCROLL_STEP;
1764 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
1765 gtk_menu_scroll_timeout,
1768 else if (!enter && !in_arrow && menu->upper_arrow_prelight)
1770 menu->upper_arrow_prelight = FALSE;
1771 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
1773 gtk_menu_stop_scrolling (menu);
1777 if (menu->lower_arrow_visible && !menu->tearoff_active)
1780 rect.y = height - border - MENU_SCROLL_ARROW_HEIGHT;
1782 rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
1785 if ((x >= rect.x) && (x < rect.x + rect.width) &&
1786 (y >= rect.y) && (y < rect.y + rect.height))
1789 scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
1792 if (enter && in_arrow &&
1793 (!menu->lower_arrow_prelight || menu->scroll_fast != scroll_fast))
1795 menu->lower_arrow_prelight = TRUE;
1796 menu->scroll_fast = scroll_fast;
1797 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
1799 /* Deselect the active item so that any submenus are poped down */
1800 gtk_menu_shell_deselect (menu_shell);
1802 gtk_menu_stop_scrolling (menu);
1803 menu->scroll_step = MENU_SCROLL_STEP;
1804 menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
1805 gtk_menu_scroll_timeout,
1808 else if (!enter && !in_arrow && menu->lower_arrow_prelight)
1810 menu->lower_arrow_prelight = FALSE;
1811 gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
1813 gtk_menu_stop_scrolling (menu);
1819 gtk_menu_enter_notify (GtkWidget *widget,
1820 GdkEventCrossing *event)
1822 GtkWidget *menu_item;
1824 if (widget && GTK_IS_MENU (widget))
1825 gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
1827 /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
1828 * will not correspond to the event widget's parent. Check to see
1829 * if we are in the parent's navigation region.
1831 menu_item = gtk_get_event_widget ((GdkEvent*) event);
1832 if (menu_item && GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
1833 gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent), event->x_root, event->y_root))
1836 return GTK_WIDGET_CLASS (parent_class)->enter_notify_event (widget, event);
1840 gtk_menu_leave_notify (GtkWidget *widget,
1841 GdkEventCrossing *event)
1843 GtkMenuShell *menu_shell;
1845 GtkMenuItem *menu_item;
1846 GtkWidget *event_widget;
1848 menu = GTK_MENU (widget);
1849 menu_shell = GTK_MENU_SHELL (widget);
1851 if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
1854 gtk_menu_handle_scrolling (menu, FALSE);
1856 event_widget = gtk_get_event_widget ((GdkEvent*) event);
1858 if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
1861 menu_item = GTK_MENU_ITEM (event_widget);
1863 /* Here we check to see if we're leaving an active menu item with a submenu,
1864 * in which case we enter submenu navigation mode.
1866 if (menu_shell->active_menu_item != NULL
1867 && menu_item->submenu != NULL
1868 && menu_item->submenu_placement == GTK_LEFT_RIGHT)
1870 if (menu_item->submenu->window != NULL)
1872 gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
1877 return GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event);
1881 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
1883 if (menu->navigation_region)
1885 gdk_region_destroy (menu->navigation_region);
1886 menu->navigation_region = NULL;
1889 if (menu->navigation_timeout)
1891 gtk_timeout_remove (menu->navigation_timeout);
1892 menu->navigation_timeout = 0;
1896 /* When the timeout is elapsed, the navigation region is destroyed
1897 * and the menuitem under the pointer (if any) is selected.
1900 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
1902 GtkMenu *menu = user_data;
1903 GdkWindow *child_window;
1905 GDK_THREADS_ENTER ();
1907 gtk_menu_stop_navigating_submenu (menu);
1909 if (GTK_WIDGET_REALIZED (menu))
1911 child_window = gdk_window_get_pointer (GTK_WIDGET (menu)->window, NULL, NULL, NULL);
1915 GdkEventCrossing send_event;
1917 memset (&send_event, 0, sizeof (send_event));
1918 send_event.window = child_window;
1919 send_event.type = GDK_ENTER_NOTIFY;
1920 send_event.time = GDK_CURRENT_TIME; /* Bogus */
1921 send_event.send_event = TRUE;
1923 GTK_WIDGET_CLASS (parent_class)->enter_notify_event (GTK_WIDGET (menu), &send_event);
1927 GDK_THREADS_LEAVE ();
1933 gtk_menu_navigating_submenu (GtkMenu *menu,
1937 if (menu->navigation_region)
1939 if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
1943 gtk_menu_stop_navigating_submenu (menu);
1951 gtk_menu_set_submenu_navigation_region (GtkMenu *menu,
1952 GtkMenuItem *menu_item,
1953 GdkEventCrossing *event)
1955 gint submenu_left = 0;
1956 gint submenu_right = 0;
1957 gint submenu_top = 0;
1958 gint submenu_bottom = 0;
1962 GtkWidget *event_widget;
1964 g_return_if_fail (menu_item->submenu != NULL);
1965 g_return_if_fail (event != NULL);
1967 event_widget = gtk_get_event_widget ((GdkEvent*) event);
1969 gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
1970 gdk_window_get_size (menu_item->submenu->window, &width, &height);
1971 submenu_right = submenu_left + width;
1972 submenu_bottom = submenu_top + height;
1974 gdk_window_get_size (event_widget->window, &width, &height);
1976 if (event->x >= 0 && event->x < width)
1978 /* Set navigation region */
1979 /* We fudge/give a little padding in case the user
1980 * ``misses the vertex'' of the triangle/is off by a pixel or two.
1982 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
1983 point[0].x = event->x_root - SUBMENU_NAV_REGION_PADDING;
1985 point[0].x = event->x_root + SUBMENU_NAV_REGION_PADDING;
1987 /* Exiting the top or bottom? */
1990 point[0].y = event->y_root + SUBMENU_NAV_REGION_PADDING;
1991 point[1].y = submenu_top;
1993 if (point[0].y <= point[1].y)
1998 point[0].y = event->y_root - SUBMENU_NAV_REGION_PADDING;
1999 point[1].y = submenu_bottom;
2001 if (point[0].y >= point[1].y)
2005 /* Submenu is to the left or right? */
2006 if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
2007 point[1].x = submenu_left; /* right */
2009 point[1].x = submenu_right; /* left */
2011 point[2].x = point[1].x;
2012 point[2].y = point[0].y;
2014 gtk_menu_stop_navigating_submenu (menu);
2016 menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
2018 menu->navigation_timeout = gtk_timeout_add (SUBMENU_NAV_HYSTERESIS_TIMEOUT,
2019 gtk_menu_stop_navigating_submenu_cb, menu);
2024 gtk_menu_deactivate (GtkMenuShell *menu_shell)
2028 g_return_if_fail (GTK_IS_MENU (menu_shell));
2030 parent = menu_shell->parent_menu_shell;
2032 menu_shell->activate_time = 0;
2033 gtk_menu_popdown (GTK_MENU (menu_shell));
2036 gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
2040 gtk_menu_position (GtkMenu *menu)
2043 GtkRequisition requisition;
2051 g_return_if_fail (GTK_IS_MENU (menu));
2053 widget = GTK_WIDGET (menu);
2055 screen_width = gdk_screen_width ();
2056 screen_height = gdk_screen_height ();
2058 gdk_window_get_pointer (NULL, &x, &y, NULL);
2060 /* We need the requisition to figure out the right place to
2061 * popup the menu. In fact, we always need to ask here, since
2062 * if a size_request was queued while we weren't popped up,
2063 * the requisition won't have been recomputed yet.
2065 gtk_widget_size_request (widget, &requisition);
2069 if (menu->position_func)
2070 (* menu->position_func) (menu, &x, &y, &push_in, menu->position_func_data);
2073 x = CLAMP (x - 2, 0, MAX (0, screen_width - requisition.width));
2074 y = CLAMP (y - 2, 0, MAX (0, screen_height - requisition.height));
2081 menu_height = GTK_WIDGET (menu)->requisition.height;
2083 if (y + menu_height > screen_height)
2085 scroll_offset -= y + menu_height - screen_height;
2086 y = screen_height - menu_height;
2096 if (y + requisition.height > screen_height)
2097 requisition.height = screen_height - y;
2102 requisition.height -= -y;
2106 if (scroll_offset > 0)
2107 scroll_offset += MENU_SCROLL_ARROW_HEIGHT;
2109 /* FIXME: The MAX() here is because gtk_widget_set_uposition
2110 * is broken. Once we provide an alternate interface that
2111 * allows negative values, then we can remove them.
2114 gtk_widget_set_uposition (GTK_MENU_SHELL (menu)->active ?
2115 menu->toplevel : menu->tearoff_window,
2117 gtk_widget_set_usize (GTK_MENU_SHELL (menu)->active ?
2118 menu->toplevel : menu->tearoff_hbox,
2119 -1, requisition.height);
2120 menu->scroll_offset = scroll_offset;
2124 gtk_menu_stop_scrolling (GtkMenu *menu)
2126 if (menu->timeout_id)
2128 g_source_remove (menu->timeout_id);
2129 menu->timeout_id = 0;
2135 gtk_menu_scroll_to (GtkMenu *menu,
2140 gint view_width, view_height;
2142 gboolean last_visible;
2145 widget = GTK_WIDGET (menu);
2147 if (menu->tearoff_active &&
2148 menu->tearoff_adjustment &&
2149 (menu->tearoff_adjustment->value != offset))
2151 menu->tearoff_adjustment->value = offset;
2152 gtk_adjustment_value_changed (menu->tearoff_adjustment);
2155 /* Scroll the menu: */
2156 gdk_window_move (menu->bin_window, 0, -offset);
2158 /* Move/resize the viewport according to arrows: */
2159 gdk_window_get_size (widget->window, &view_width, &view_height);
2161 border_width = GTK_CONTAINER (menu)->border_width;
2162 view_width -= (border_width + widget->style->xthickness) * 2;
2163 view_height -= (border_width + widget->style->ythickness) * 2;
2164 menu_height = widget->requisition.height - (border_width + widget->style->ythickness) * 2;
2166 x = border_width + widget->style->xthickness;
2167 y = border_width + widget->style->ythickness;
2169 if (!menu->tearoff_active)
2171 last_visible = menu->upper_arrow_visible;
2172 menu->upper_arrow_visible = (offset > 0);
2174 if (menu->upper_arrow_visible)
2175 view_height -= MENU_SCROLL_ARROW_HEIGHT;
2177 if ( (last_visible != menu->upper_arrow_visible) &&
2178 !menu->upper_arrow_visible)
2180 menu->upper_arrow_prelight = FALSE;
2182 /* If we hid the upper arrow, possibly remove timeout */
2183 if (menu->scroll_step < 0)
2184 gtk_menu_stop_scrolling (menu);
2187 last_visible = menu->lower_arrow_visible;
2188 menu->lower_arrow_visible = (view_height + offset < menu_height);
2190 if (menu->lower_arrow_visible)
2191 view_height -= MENU_SCROLL_ARROW_HEIGHT;
2193 if ( (last_visible != menu->lower_arrow_visible) &&
2194 !menu->lower_arrow_visible)
2196 menu->lower_arrow_prelight = FALSE;
2198 /* If we hid the lower arrow, possibly remove timeout */
2199 if (menu->scroll_step > 0)
2200 gtk_menu_stop_scrolling (menu);
2203 if (menu->upper_arrow_visible)
2204 y += MENU_SCROLL_ARROW_HEIGHT;
2208 gdk_window_move_resize (menu->view_window,
2214 menu->scroll_offset = offset;
2218 gtk_menu_select_item (GtkMenuShell *menu_shell,
2219 GtkWidget *menu_item)
2224 GtkRequisition child_requisition;
2225 gint child_offset, child_height;
2229 gboolean last_child = 0;
2231 g_return_if_fail (GTK_IS_MENU (menu_shell));
2233 menu = GTK_MENU (menu_shell);
2235 /* We need to check if the selected item fully visible.
2236 * If not we need to scroll the menu so that it becomes fully
2243 children = menu_shell->children;
2246 child = children->data;
2247 children = children->next;
2249 if (GTK_WIDGET_VISIBLE (child))
2251 gtk_widget_size_request (child, &child_requisition);
2252 child_offset += child_height;
2253 child_height = child_requisition.height;
2256 if (child == menu_item)
2258 last_child = (children == NULL);
2263 if (child == menu_item)
2265 y = menu->scroll_offset;
2266 gdk_window_get_size (GTK_WIDGET (menu)->window, &width, &height);
2268 height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness;
2270 if (child_offset + child_height <= y)
2272 /* Ignore the enter event we might get if the pointer is on the menu
2274 menu_shell->ignore_enter = TRUE;
2275 gtk_menu_scroll_to (menu, child_offset);
2280 if (menu->upper_arrow_visible && !menu->tearoff_active)
2281 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2282 if (menu->lower_arrow_visible && !menu->tearoff_active)
2283 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2285 if (child_offset >= y + height - arrow_height)
2288 if (!last_child && !menu->tearoff_active)
2289 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2291 y = child_offset + child_height - height + arrow_height;
2292 if ((y > 0) && !menu->tearoff_active)
2294 /* Need upper arrow */
2295 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2296 y = child_offset + child_height - height + arrow_height;
2298 /* Ignore the enter event we might get if the pointer is on the menu
2300 menu_shell->ignore_enter = TRUE;
2301 gtk_menu_scroll_to (menu, y);
2307 GTK_MENU_SHELL_CLASS (parent_class)->select_item (menu_shell, menu_item);
2311 /* Reparent the menu, taking care of the refcounting
2313 * If unrealize is true we force a unrealize while reparenting the parent.
2314 * This can help eliminate flicker in some cases.
2316 * What happens is that when the menu is unrealized and then re-realized,
2317 * the allocations are as follows:
2319 * parent - 1x1 at (0,0)
2320 * child1 - 100x20 at (0,0)
2321 * child2 - 100x20 at (0,20)
2322 * child3 - 100x20 at (0,40)
2324 * That is, the parent is small but the children are full sized. Then,
2325 * when the queued_resize gets processed, the parent gets resized to
2328 * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
2329 * contains the following logic:
2331 * - if a move or resize operation on a window would change the clip
2332 * region on the children, then before the window is resized
2333 * the background for children is temporarily set to None, the
2334 * move/resize done, and the background for the children restored.
2336 * So, at the point where the parent is resized to final size, the
2337 * background for the children is temporarily None, and thus they
2338 * are not cleared to the background color and the previous background
2339 * (the image of the menu) is left in place.
2342 gtk_menu_reparent (GtkMenu *menu,
2343 GtkWidget *new_parent,
2346 GtkObject *object = GTK_OBJECT (menu);
2347 GtkWidget *widget = GTK_WIDGET (menu);
2348 gboolean was_floating = GTK_OBJECT_FLOATING (object);
2350 gtk_object_ref (object);
2351 gtk_object_sink (object);
2355 gtk_object_ref (object);
2356 gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
2357 gtk_container_add (GTK_CONTAINER (new_parent), widget);
2358 gtk_object_unref (object);
2361 gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
2364 GTK_OBJECT_SET_FLAGS (object, GTK_FLOATING);
2366 gtk_object_unref (object);
2370 gtk_menu_show_all (GtkWidget *widget)
2372 g_return_if_fail (GTK_IS_MENU (widget));
2374 /* Show children, but not self. */
2375 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
2380 gtk_menu_hide_all (GtkWidget *widget)
2382 g_return_if_fail (GTK_IS_MENU (widget));
2384 /* Hide children, but not self. */
2385 gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);