]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenu.c
Use the correct screen for getting the height. (Fix from Stephen Browne,
[~andy/gtk] / gtk / gtkmenu.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #define GTK_MENU_INTERNALS
28
29 #include <string.h> /* memset */
30 #include "gdk/gdkkeysyms.h"
31 #include "gtkaccelmap.h"
32 #include "gtkbindings.h"
33 #include "gtklabel.h"
34 #include "gtkmain.h"
35 #include "gtkmenu.h"
36 #include "gtkmenuitem.h"
37 #include "gtkwindow.h"
38 #include "gtkhbox.h"
39 #include "gtkvscrollbar.h"
40 #include "gtksettings.h"
41 #include "gtkintl.h"
42
43
44 #define MENU_ITEM_CLASS(w)   GTK_MENU_ITEM_GET_CLASS (w)
45 #define MENU_NEEDS_RESIZE(m) GTK_MENU_SHELL (m)->menu_flag
46
47 #define DEFAULT_POPUP_DELAY    225
48 #define DEFAULT_POPDOWN_DELAY  1000
49
50 #define NAVIGATION_REGION_OVERSHOOT 50  /* How much the navigation region
51                                          * extends below the submenu
52                                          */
53
54 #define MENU_SCROLL_STEP1 8
55 #define MENU_SCROLL_STEP2 15
56 #define MENU_SCROLL_ARROW_HEIGHT 16
57 #define MENU_SCROLL_FAST_ZONE 8
58 #define MENU_SCROLL_TIMEOUT1 50
59 #define MENU_SCROLL_TIMEOUT2 50
60
61 typedef struct _GtkMenuAttachData       GtkMenuAttachData;
62 typedef struct _GtkMenuPrivate          GtkMenuPrivate;
63
64 struct _GtkMenuAttachData
65 {
66   GtkWidget *attach_widget;
67   GtkMenuDetachFunc detacher;
68 };
69
70 struct _GtkMenuPrivate 
71 {
72   gboolean have_position;
73   gint x;
74   gint y;
75 };
76
77 enum {
78   PROP_0,
79   PROP_TEAROFF_TITLE
80 };
81
82 static void     gtk_menu_class_init        (GtkMenuClass     *klass);
83 static void     gtk_menu_init              (GtkMenu          *menu);
84 static void     gtk_menu_set_property      (GObject      *object,
85                                             guint         prop_id,
86                                             const GValue *value,
87                                             GParamSpec   *pspec);
88 static void     gtk_menu_get_property      (GObject     *object,
89                                             guint        prop_id,
90                                             GValue      *value,
91                                             GParamSpec  *pspec);
92 static void     gtk_menu_destroy           (GtkObject        *object);
93 static void     gtk_menu_finalize          (GObject          *object);
94 static void     gtk_menu_realize           (GtkWidget        *widget);
95 static void     gtk_menu_unrealize         (GtkWidget        *widget);
96 static void     gtk_menu_size_request      (GtkWidget        *widget,
97                                             GtkRequisition   *requisition);
98 static void     gtk_menu_size_allocate     (GtkWidget        *widget,
99                                             GtkAllocation    *allocation);
100 static void     gtk_menu_paint             (GtkWidget        *widget,
101                                             GdkEventExpose   *expose);
102 static void     gtk_menu_show              (GtkWidget        *widget);
103 static gboolean gtk_menu_expose            (GtkWidget        *widget,
104                                             GdkEventExpose   *event);
105 static gboolean gtk_menu_key_press         (GtkWidget        *widget,
106                                             GdkEventKey      *event);
107 static gboolean gtk_menu_button_press      (GtkWidget        *widget,
108                                             GdkEventButton   *event);
109 static gboolean gtk_menu_button_release    (GtkWidget        *widget,
110                                             GdkEventButton   *event);
111 static gboolean gtk_menu_motion_notify     (GtkWidget        *widget,
112                                             GdkEventMotion   *event);
113 static gboolean gtk_menu_enter_notify      (GtkWidget        *widget,
114                                             GdkEventCrossing *event);
115 static gboolean gtk_menu_leave_notify      (GtkWidget        *widget,
116                                             GdkEventCrossing *event);
117 static void     gtk_menu_scroll_to         (GtkMenu          *menu,
118                                             gint              offset);
119
120 static void     gtk_menu_stop_scrolling        (GtkMenu  *menu);
121 static void     gtk_menu_remove_scroll_timeout (GtkMenu  *menu);
122 static gboolean gtk_menu_scroll_timeout        (gpointer  data);
123
124 static void     gtk_menu_scroll_item_visible (GtkMenuShell    *menu_shell,
125                                               GtkWidget       *menu_item);
126 static void     gtk_menu_select_item       (GtkMenuShell     *menu_shell,
127                                             GtkWidget        *menu_item);
128 static void     gtk_menu_real_insert       (GtkMenuShell     *menu_shell,
129                                             GtkWidget        *child,
130                                             gint              position);
131 static void     gtk_menu_scrollbar_changed (GtkAdjustment    *adjustment,
132                                             GtkMenu          *menu);
133 static void     gtk_menu_handle_scrolling  (GtkMenu          *menu,
134                                             gboolean         enter);
135 static void     gtk_menu_set_tearoff_hints (GtkMenu          *menu,
136                                             gint             width);
137 static void     gtk_menu_style_set         (GtkWidget        *widget,
138                                             GtkStyle         *previous_style);
139 static gboolean gtk_menu_focus             (GtkWidget        *widget,
140                                             GtkDirectionType direction);
141 static gint     gtk_menu_get_popup_delay   (GtkMenuShell    *menu_shell);
142
143
144 static void     gtk_menu_stop_navigating_submenu       (GtkMenu          *menu);
145 static gboolean gtk_menu_stop_navigating_submenu_cb    (gpointer          user_data);
146 static gboolean gtk_menu_navigating_submenu            (GtkMenu          *menu,
147                                                         gint              event_x,
148                                                         gint              event_y);
149 static void     gtk_menu_set_submenu_navigation_region (GtkMenu          *menu,
150                                                         GtkMenuItem      *menu_item,
151                                                         GdkEventCrossing *event);
152  
153 static void gtk_menu_deactivate     (GtkMenuShell      *menu_shell);
154 static void gtk_menu_show_all       (GtkWidget         *widget);
155 static void gtk_menu_hide_all       (GtkWidget         *widget);
156 static void gtk_menu_position       (GtkMenu           *menu);
157 static void gtk_menu_reparent       (GtkMenu           *menu, 
158                                      GtkWidget         *new_parent, 
159                                      gboolean           unrealize);
160 static void gtk_menu_remove         (GtkContainer      *menu,
161                                      GtkWidget         *widget);
162
163 static void gtk_menu_update_title   (GtkMenu           *menu);
164
165 static void       menu_grab_transfer_window_destroy (GtkMenu *menu);
166 static GdkWindow *menu_grab_transfer_window_get     (GtkMenu *menu);
167
168 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
169                                            gboolean group_changed);
170
171 static GtkMenuShellClass *parent_class = NULL;
172 static const gchar       *attach_data_key = "gtk-menu-attach-data";
173
174 GtkMenuPrivate *
175 gtk_menu_get_private (GtkMenu *menu)
176 {
177   GtkMenuPrivate *private;
178   static GQuark private_quark = 0;
179
180   if (!private_quark)
181     private_quark = g_quark_from_static_string ("gtk-menu-private");
182
183   private = g_object_get_qdata (G_OBJECT (menu), private_quark);
184
185   if (!private)
186     {
187       private = g_new0 (GtkMenuPrivate, 1);
188       private->have_position = FALSE;
189       
190       g_object_set_qdata_full (G_OBJECT (menu), private_quark,
191                                private, g_free);
192     }
193
194   return private;
195 }
196
197 GType
198 gtk_menu_get_type (void)
199 {
200   static GType menu_type = 0;
201   
202   if (!menu_type)
203     {
204       static const GTypeInfo menu_info =
205       {
206         sizeof (GtkMenuClass),
207         NULL,           /* base_init */
208         NULL,           /* base_finalize */
209         (GClassInitFunc) gtk_menu_class_init,
210         NULL,           /* class_finalize */
211         NULL,           /* class_data */
212         sizeof (GtkMenu),
213         0,              /* n_preallocs */
214         (GInstanceInitFunc) gtk_menu_init,
215       };
216       
217       menu_type = g_type_register_static (GTK_TYPE_MENU_SHELL, "GtkMenu",
218                                           &menu_info, 0);
219     }
220   
221   return menu_type;
222 }
223
224 static void
225 gtk_menu_class_init (GtkMenuClass *class)
226 {
227   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
228   GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
229   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
230   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
231   GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
232   GtkBindingSet *binding_set;
233   
234   parent_class = g_type_class_peek_parent (class);
235   
236   gobject_class->finalize = gtk_menu_finalize;
237   gobject_class->set_property = gtk_menu_set_property;
238   gobject_class->get_property = gtk_menu_get_property;
239
240   g_object_class_install_property (gobject_class,
241                                    PROP_TEAROFF_TITLE,
242                                    g_param_spec_string ("tearoff-title",
243                                                         _("Tearoff Title"),
244                                                         _("A title that may be displayed by the window manager when this menu is torn-off"),
245                                                         "",
246                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
247
248   object_class->destroy = gtk_menu_destroy;
249   
250   widget_class->realize = gtk_menu_realize;
251   widget_class->unrealize = gtk_menu_unrealize;
252   widget_class->size_request = gtk_menu_size_request;
253   widget_class->size_allocate = gtk_menu_size_allocate;
254   widget_class->show = gtk_menu_show;
255   widget_class->expose_event = gtk_menu_expose;
256   widget_class->key_press_event = gtk_menu_key_press;
257   widget_class->button_press_event = gtk_menu_button_press;
258   widget_class->button_release_event = gtk_menu_button_release;
259   widget_class->motion_notify_event = gtk_menu_motion_notify;
260   widget_class->show_all = gtk_menu_show_all;
261   widget_class->hide_all = gtk_menu_hide_all;
262   widget_class->enter_notify_event = gtk_menu_enter_notify;
263   widget_class->leave_notify_event = gtk_menu_leave_notify;
264   widget_class->motion_notify_event = gtk_menu_motion_notify;
265   widget_class->style_set = gtk_menu_style_set;
266   widget_class->focus = gtk_menu_focus;
267
268   container_class->remove = gtk_menu_remove;
269   
270   menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
271   menu_shell_class->deactivate = gtk_menu_deactivate;
272   menu_shell_class->select_item = gtk_menu_select_item;
273   menu_shell_class->insert = gtk_menu_real_insert;
274   menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
275
276   binding_set = gtk_binding_set_by_class (class);
277   gtk_binding_entry_add_signal (binding_set,
278                                 GDK_Up, 0,
279                                 "move_current", 1,
280                                 GTK_TYPE_MENU_DIRECTION_TYPE,
281                                 GTK_MENU_DIR_PREV);
282   gtk_binding_entry_add_signal (binding_set,
283                                 GDK_KP_Up, 0,
284                                 "move_current", 1,
285                                 GTK_TYPE_MENU_DIRECTION_TYPE,
286                                 GTK_MENU_DIR_PREV);
287   gtk_binding_entry_add_signal (binding_set,
288                                 GDK_Down, 0,
289                                 "move_current", 1,
290                                 GTK_TYPE_MENU_DIRECTION_TYPE,
291                                 GTK_MENU_DIR_NEXT);
292   gtk_binding_entry_add_signal (binding_set,
293                                 GDK_KP_Down, 0,
294                                 "move_current", 1,
295                                 GTK_TYPE_MENU_DIRECTION_TYPE,
296                                 GTK_MENU_DIR_NEXT);
297   gtk_binding_entry_add_signal (binding_set,
298                                 GDK_Left, 0,
299                                 "move_current", 1,
300                                 GTK_TYPE_MENU_DIRECTION_TYPE,
301                                 GTK_MENU_DIR_PARENT);
302   gtk_binding_entry_add_signal (binding_set,
303                                 GDK_KP_Left, 0,
304                                 "move_current", 1,
305                                 GTK_TYPE_MENU_DIRECTION_TYPE,
306                                 GTK_MENU_DIR_PARENT);
307   gtk_binding_entry_add_signal (binding_set,
308                                 GDK_Right, 0,
309                                 "move_current", 1,
310                                 GTK_TYPE_MENU_DIRECTION_TYPE,
311                                 GTK_MENU_DIR_CHILD);
312   gtk_binding_entry_add_signal (binding_set,
313                                 GDK_KP_Right, 0,
314                                 "move_current", 1,
315                                 GTK_TYPE_MENU_DIRECTION_TYPE,
316                                 GTK_MENU_DIR_CHILD);
317
318   gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
319                                                        _("Can change accelerators"),
320                                                        _("Whether menu accelerators can be changed by pressing a key over the menu item"),
321                                                        FALSE,
322                                                        G_PARAM_READWRITE));
323
324   gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
325                                                    _("Delay before submenus appear"),
326                                                    _("Minimum time the pointer must stay over a menu item before the submenu appear"),
327                                                    0,
328                                                    G_MAXINT,
329                                                    DEFAULT_POPUP_DELAY,
330                                                    G_PARAM_READWRITE));
331
332   gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
333                                                    _("Delay before hiding a submenu"),
334                                                    _("The time before hiding a submenu when the pointer is moving towards the submenu"),
335                                                    0,
336                                                    G_MAXINT,
337                                                    DEFAULT_POPDOWN_DELAY,
338                                                    G_PARAM_READWRITE));
339                                                    
340 }
341
342
343 static void 
344 gtk_menu_set_property (GObject      *object,
345                        guint         prop_id,
346                        const GValue *value,
347                        GParamSpec   *pspec)
348 {
349   GtkMenu *menu;
350   
351   menu = GTK_MENU (object);
352   
353   switch (prop_id)
354     {
355     case PROP_TEAROFF_TITLE:
356       gtk_menu_set_title (menu, g_value_get_string (value));
357       break;      
358     default:
359       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
360       break;
361     }
362 }
363
364 static void 
365 gtk_menu_get_property (GObject     *object,
366                        guint        prop_id,
367                        GValue      *value,
368                        GParamSpec  *pspec)
369 {
370   GtkMenu *menu;
371   
372   menu = GTK_MENU (object);
373   
374   switch (prop_id)
375     {
376     case PROP_TEAROFF_TITLE:
377       g_value_set_string (value, gtk_menu_get_title (menu));
378       break;
379     default:
380       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
381       break;
382     }
383 }
384
385 static gboolean
386 gtk_menu_window_event (GtkWidget *window,
387                        GdkEvent  *event,
388                        GtkWidget *menu)
389 {
390   gboolean handled = FALSE;
391
392   g_object_ref (window);
393   g_object_ref (menu);
394
395   switch (event->type)
396     {
397     case GDK_KEY_PRESS:
398     case GDK_KEY_RELEASE:
399       handled = gtk_widget_event (menu, event);
400       break;
401     default:
402       break;
403     }
404
405   g_object_unref (window);
406   g_object_unref (menu);
407
408   return handled;
409 }
410
411 static void
412 gtk_menu_window_size_request (GtkWidget      *window,
413                               GtkRequisition *requisition,
414                               GtkMenu        *menu)
415 {
416   GtkMenuPrivate *private = gtk_menu_get_private (menu);
417
418   if (private->have_position)
419     {
420       GdkScreen *screen = gtk_widget_get_screen (window);
421       gint screen_height = gdk_screen_get_height (screen);
422
423       if (private->y + requisition->height > screen_height)
424         requisition->height = screen_height - private->y;
425     }
426 }
427
428 static void
429 gtk_menu_init (GtkMenu *menu)
430 {
431   menu->parent_menu_item = NULL;
432   menu->old_active_menu_item = NULL;
433   menu->accel_group = NULL;
434   menu->position_func = NULL;
435   menu->position_func_data = NULL;
436   menu->toggle_size = 0;
437
438   menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
439                                                    "type", GTK_WINDOW_POPUP,
440                                                    "child", menu,
441                                                    NULL),
442                                      "signal::event", gtk_menu_window_event, menu,
443                                      "signal::size_request", gtk_menu_window_size_request, menu,
444                                      "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
445                                      NULL);
446   gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
447   gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
448
449   /* Refloat the menu, so that reference counting for the menu isn't
450    * affected by it being a child of the toplevel
451    */
452   GTK_WIDGET_SET_FLAGS (menu, GTK_FLOATING);
453   menu->needs_destruction_ref_count = TRUE;
454
455   menu->view_window = NULL;
456   menu->bin_window = NULL;
457
458   menu->scroll_offset = 0;
459   menu->scroll_step  = 0;
460   menu->timeout_id = 0;
461   menu->scroll_fast = FALSE;
462   
463   menu->tearoff_window = NULL;
464   menu->tearoff_hbox = NULL;
465   menu->torn_off = FALSE;
466   menu->tearoff_active = FALSE;
467   menu->tearoff_adjustment = NULL;
468   menu->tearoff_scrollbar = NULL;
469
470   menu->upper_arrow_visible = FALSE;
471   menu->lower_arrow_visible = FALSE;
472   menu->upper_arrow_prelight = FALSE;
473   menu->lower_arrow_prelight = FALSE;
474   
475   MENU_NEEDS_RESIZE (menu) = TRUE;
476 }
477
478 static void
479 gtk_menu_destroy (GtkObject *object)
480 {
481   GtkMenu *menu;
482   GtkMenuAttachData *data;
483
484   g_return_if_fail (GTK_IS_MENU (object));
485
486   menu = GTK_MENU (object);
487
488   gtk_menu_stop_scrolling (menu);
489   
490   data = g_object_get_data (G_OBJECT (object), attach_data_key);
491   if (data)
492     gtk_menu_detach (menu);
493   
494   gtk_menu_stop_navigating_submenu (menu);
495
496   if (menu->old_active_menu_item)
497     {
498       g_object_unref (menu->old_active_menu_item);
499       menu->old_active_menu_item = NULL;
500     }
501
502   /* Add back the reference count for being a child */
503   if (menu->needs_destruction_ref_count)
504     {
505       menu->needs_destruction_ref_count = FALSE;
506       g_object_ref (object);
507     }
508   
509   if (menu->accel_group)
510     {
511       g_object_unref (menu->accel_group);
512       menu->accel_group = NULL;
513     }
514
515   if (menu->toplevel)
516     gtk_widget_destroy (menu->toplevel);
517   if (menu->tearoff_window)
518     gtk_widget_destroy (menu->tearoff_window);
519
520   GTK_OBJECT_CLASS (parent_class)->destroy (object);
521 }
522
523 static void
524 gtk_menu_finalize (GObject *object)
525 {
526   GtkMenu *menu = GTK_MENU (object);
527
528   g_free (menu->accel_path);
529   
530   G_OBJECT_CLASS (parent_class)->finalize (object);
531 }
532
533 static void
534 attach_widget_screen_changed (GtkWidget *attach_widget,
535                               GdkScreen *previous_screen,
536                               GtkMenu   *menu)
537 {
538   if (gtk_widget_has_screen (attach_widget) &&
539       !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
540     {
541       gtk_window_set_screen (GTK_WINDOW (menu->toplevel),
542                              gtk_widget_get_screen (attach_widget));
543     }
544 }
545
546 void
547 gtk_menu_attach_to_widget (GtkMenu             *menu,
548                            GtkWidget           *attach_widget,
549                            GtkMenuDetachFunc    detacher)
550 {
551   GtkMenuAttachData *data;
552   
553   g_return_if_fail (GTK_IS_MENU (menu));
554   g_return_if_fail (GTK_IS_WIDGET (attach_widget));
555   g_return_if_fail (detacher != NULL);
556   
557   /* keep this function in sync with gtk_widget_set_parent()
558    */
559   
560   data = g_object_get_data (G_OBJECT (menu), attach_data_key);
561   if (data)
562     {
563       g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
564                  g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
565      return;
566     }
567   
568   g_object_ref (menu);
569   gtk_object_sink (GTK_OBJECT (menu));
570   
571   data = g_new (GtkMenuAttachData, 1);
572   data->attach_widget = attach_widget;
573   
574   g_signal_connect (attach_widget, "screen_changed",
575                     G_CALLBACK (attach_widget_screen_changed), menu);
576   attach_widget_screen_changed (attach_widget, NULL, menu);
577   
578   data->detacher = detacher;
579   g_object_set_data (G_OBJECT (menu), attach_data_key, data);
580   
581   if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
582     gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
583   
584   /* we don't need to set the style here, since
585    * we are a toplevel widget.
586    */
587
588   /* Fallback title for menu comes from attach widget */
589   gtk_menu_update_title (menu);
590 }
591
592 GtkWidget*
593 gtk_menu_get_attach_widget (GtkMenu *menu)
594 {
595   GtkMenuAttachData *data;
596   
597   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
598   
599   data = g_object_get_data (G_OBJECT (menu), attach_data_key);
600   if (data)
601     return data->attach_widget;
602   return NULL;
603 }
604
605 void
606 gtk_menu_detach (GtkMenu *menu)
607 {
608   GtkMenuAttachData *data;
609   
610   g_return_if_fail (GTK_IS_MENU (menu));
611   
612   /* keep this function in sync with gtk_widget_unparent()
613    */
614   data = g_object_get_data (G_OBJECT (menu), attach_data_key);
615   if (!data)
616     {
617       g_warning ("gtk_menu_detach(): menu is not attached");
618       return;
619     }
620   g_object_set_data (G_OBJECT (menu), attach_data_key, NULL);
621   
622   g_signal_handlers_disconnect_by_func (data->attach_widget,
623                                         (gpointer) attach_widget_screen_changed,
624                                         menu);
625
626   data->detacher (data->attach_widget, menu);
627   
628   if (GTK_WIDGET_REALIZED (menu))
629     gtk_widget_unrealize (GTK_WIDGET (menu));
630   
631   g_free (data);
632   
633   /* Fallback title for menu comes from attach widget */
634   gtk_menu_update_title (menu);
635
636   g_object_unref (menu);
637 }
638
639 static void 
640 gtk_menu_remove (GtkContainer *container,
641                  GtkWidget    *widget)
642 {
643   GtkMenu *menu;
644   g_return_if_fail (GTK_IS_MENU (container));
645   g_return_if_fail (GTK_IS_MENU_ITEM (widget));
646
647   menu = GTK_MENU (container);
648
649   /* Clear out old_active_menu_item if it matches the item we are removing
650    */
651   if (menu->old_active_menu_item == widget)
652     {
653       g_object_unref (menu->old_active_menu_item);
654       menu->old_active_menu_item = NULL;
655     }
656
657   GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
658 }
659
660
661 GtkWidget*
662 gtk_menu_new (void)
663 {
664   return g_object_new (GTK_TYPE_MENU, NULL);
665 }
666
667 static void
668 gtk_menu_real_insert (GtkMenuShell     *menu_shell,
669                       GtkWidget        *child,
670                       gint              position)
671 {
672   if (GTK_WIDGET_REALIZED (menu_shell))
673     gtk_widget_set_parent_window (child, GTK_MENU (menu_shell)->bin_window);
674
675   GTK_MENU_SHELL_CLASS (parent_class)->insert (menu_shell, child, position);
676 }
677
678 static void
679 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
680 {
681   GtkWidget *widget;
682   gint width, height;
683
684   widget = GTK_WIDGET (menu);
685
686   if (menu->torn_off)
687     {
688       GdkPixmap *pixmap;
689       GdkGC *gc;
690       GdkGCValues gc_values;
691
692       menu->tearoff_active = FALSE;
693       menu->saved_scroll_offset = menu->scroll_offset;
694       
695       gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
696       gc = gdk_gc_new_with_values (widget->window,
697                                    &gc_values, GDK_GC_SUBWINDOW);
698       
699       gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
700       
701       pixmap = gdk_pixmap_new (menu->tearoff_window->window,
702                                width,
703                                height,
704                                -1);
705
706       gdk_draw_drawable (pixmap, gc,
707                          menu->tearoff_window->window,
708                          0, 0, 0, 0, -1, -1);
709       g_object_unref (gc);
710
711       gtk_widget_set_size_request (menu->tearoff_window,
712                                    width,
713                                    height);
714
715       gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
716       g_object_unref (pixmap);
717     }
718 }
719
720 static gboolean
721 popup_grab_on_window (GdkWindow *window,
722                       guint32    activate_time)
723 {
724   if ((gdk_pointer_grab (window, TRUE,
725                          GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
726                          GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
727                          GDK_POINTER_MOTION_MASK,
728                          NULL, NULL, activate_time) == 0))
729     {
730       if (gdk_keyboard_grab (window, TRUE,
731                              activate_time) == 0)
732         return TRUE;
733       else
734         {
735           gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
736                                       activate_time);
737           return FALSE;
738         }
739     }
740
741   return FALSE;
742 }
743
744 /**
745  * gtk_menu_popup:
746  * @menu: a #GtkMenu.
747  * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
748  * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
749  * @func: a user supplied function used to position the menu, or %NULL
750  * @data: user supplied data to be passed to @func.
751  * @button: the mouse button which was pressed to initiate the event.
752  * @activate_time: the time at which the activation event occurred.
753  *
754  * Displays a menu and makes it available for selection.  Applications can use
755  * this function to display context-sensitive menus, and will typically supply
756  * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data 
757  * parameters. The default menu positioning function will position the menu
758  * at the current mouse cursor position.
759  *
760  * The @button parameter should be the mouse button pressed to initiate
761  * the menu popup. If the menu popup was initiated by something other than
762  * a mouse button press, such as a mouse button release or a keypress,
763  * @button should be 0.
764  *
765  * The @activate_time parameter should be the time stamp of the event that
766  * initiated the popup. If such an event is not available, use
767  * gtk_get_current_event_time() instead.
768  *
769  */
770 void
771 gtk_menu_popup (GtkMenu             *menu,
772                 GtkWidget           *parent_menu_shell,
773                 GtkWidget           *parent_menu_item,
774                 GtkMenuPositionFunc  func,
775                 gpointer             data,
776                 guint                button,
777                 guint32              activate_time)
778 {
779   GtkWidget *widget;
780   GtkWidget *xgrab_shell;
781   GtkWidget *parent;
782   GdkEvent *current_event;
783   GtkMenuShell *menu_shell;
784
785   g_return_if_fail (GTK_IS_MENU (menu));
786   
787   widget = GTK_WIDGET (menu);
788   menu_shell = GTK_MENU_SHELL (menu);
789   
790   menu_shell->parent_menu_shell = parent_menu_shell;
791   
792   /* Find the last viewable ancestor, and make an X grab on it
793    */
794   parent = GTK_WIDGET (menu);
795   xgrab_shell = NULL;
796   while (parent)
797     {
798       gboolean viewable = TRUE;
799       GtkWidget *tmp = parent;
800       
801       while (tmp)
802         {
803           if (!GTK_WIDGET_MAPPED (tmp))
804             {
805               viewable = FALSE;
806               break;
807             }
808           tmp = tmp->parent;
809         }
810       
811       if (viewable)
812         xgrab_shell = parent;
813       
814       parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
815     }
816
817   /* We want to receive events generated when we map the menu; unfortunately,
818    * since there is probably already an implicit grab in place from the
819    * button that the user used to pop up the menu, we won't receive then --
820    * in particular, the EnterNotify when the menu pops up under the pointer.
821    *
822    * If we are grabbing on a parent menu shell, no problem; just grab on
823    * that menu shell first before popping up the window with owner_events = TRUE.
824    *
825    * When grabbing on the menu itself, things get more convuluted - we
826    * we do an explicit grab on a specially created window with
827    * owner_events = TRUE, which we override further down with a grab
828    * on the menu. (We can't grab on the menu until it is mapped; we
829    * probably could just leave the grab on the other window, with a
830    * little reorganization of the code in gtkmenu*).
831    */
832   if (xgrab_shell && xgrab_shell != widget)
833     {
834       if (popup_grab_on_window (xgrab_shell->window, activate_time))
835         GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
836     }
837   else
838     {
839       GdkWindow *transfer_window;
840
841       xgrab_shell = widget;
842       transfer_window = menu_grab_transfer_window_get (menu);
843       if (popup_grab_on_window (transfer_window, activate_time))
844         GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
845     }
846
847   if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
848     {
849       /* We failed to make our pointer/keyboard grab. Rather than leaving the user
850        * with a stuck up window, we just abort here. Presumably the user will
851        * try again.
852        */
853       menu_shell->parent_menu_shell = NULL;
854       menu_grab_transfer_window_destroy (menu);
855       return;
856     }
857
858   menu_shell->active = TRUE;
859   menu_shell->button = button;
860
861   /* If we are popping up the menu from something other than, a button
862    * press then, as a heuristic, we ignore enter events for the menu
863    * until we get a MOTION_NOTIFY.  
864    */
865
866   current_event = gtk_get_current_event ();
867   if (current_event)
868     {
869       if ((current_event->type != GDK_BUTTON_PRESS) &&
870           (current_event->type != GDK_ENTER_NOTIFY))
871         menu_shell->ignore_enter = TRUE;
872
873       gdk_event_free (current_event);
874     }
875
876   if (menu->torn_off)
877     {
878       gtk_menu_tearoff_bg_copy (menu);
879
880       gtk_menu_reparent (menu, menu->toplevel, FALSE);
881     }
882   
883   menu->parent_menu_item = parent_menu_item;
884   menu->position_func = func;
885   menu->position_func_data = data;
886   menu_shell->activate_time = activate_time;
887
888   /* We need to show the menu here rather in the init function because
889    * code expects to be able to tell if the menu is onscreen by
890    * looking at the GTK_WIDGET_VISIBLE (menu)
891    */
892   gtk_widget_show (GTK_WIDGET (menu));
893
894   /* Position the menu, possibly changing the size request
895    */
896   gtk_menu_position (menu);
897
898   /* Compute the size of the toplevel and realize it so we
899    * can scroll correctly.
900    */
901   {
902     GtkRequisition tmp_request;
903     GtkAllocation tmp_allocation = { 0, };
904
905     gtk_widget_size_request (menu->toplevel, &tmp_request);
906     
907     tmp_allocation.width = tmp_request.width;
908     tmp_allocation.height = tmp_request.height;
909
910     gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
911     
912     gtk_widget_realize (GTK_WIDGET (menu));
913   }
914
915   gtk_menu_scroll_to (menu, menu->scroll_offset);
916
917   /* Once everything is set up correctly, map the toplevel window on
918      the screen.
919    */
920   gtk_widget_show (menu->toplevel);
921
922   if (xgrab_shell == widget)
923     popup_grab_on_window (widget->window, activate_time); /* Should always succeed */
924
925   gtk_grab_add (GTK_WIDGET (menu));
926 }
927
928 void
929 gtk_menu_popdown (GtkMenu *menu)
930 {
931   GtkMenuPrivate *private;
932   GtkMenuShell *menu_shell;
933
934   g_return_if_fail (GTK_IS_MENU (menu));
935   
936   menu_shell = GTK_MENU_SHELL (menu);
937   private = gtk_menu_get_private (menu);
938   
939   menu_shell->parent_menu_shell = NULL;
940   menu_shell->active = FALSE;
941   menu_shell->ignore_enter = FALSE;
942
943   private->have_position = FALSE;
944
945   gtk_menu_stop_scrolling (menu);
946   
947   gtk_menu_stop_navigating_submenu (menu);
948   
949   if (menu_shell->active_menu_item)
950     {
951       if (menu->old_active_menu_item)
952         g_object_unref (menu->old_active_menu_item);
953       menu->old_active_menu_item = menu_shell->active_menu_item;
954       g_object_ref (menu->old_active_menu_item);
955     }
956
957   gtk_menu_shell_deselect (menu_shell);
958   
959   /* The X Grab, if present, will automatically be removed when we hide
960    * the window */
961   gtk_widget_hide (menu->toplevel);
962
963   if (menu->torn_off)
964     {
965       gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
966       
967       if (GTK_BIN (menu->toplevel)->child) 
968         {
969           gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
970         } 
971       else
972         {
973           /* We popped up the menu from the tearoff, so we need to 
974            * release the grab - we aren't actually hiding the menu.
975            */
976           if (menu_shell->have_xgrab)
977             {
978               GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
979               
980               gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
981               gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
982             }
983         }
984
985       /* gtk_menu_popdown is called each time a menu item is selected from
986        * a torn off menu. Only scroll back to the saved position if the
987        * non-tearoff menu was popped down.
988        */
989       if (!menu->tearoff_active)
990         gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
991       menu->tearoff_active = TRUE;
992     }
993   else
994     gtk_widget_hide (GTK_WIDGET (menu));
995
996   menu_shell->have_xgrab = FALSE;
997   gtk_grab_remove (GTK_WIDGET (menu));
998
999   menu_grab_transfer_window_destroy (menu);
1000 }
1001
1002 GtkWidget*
1003 gtk_menu_get_active (GtkMenu *menu)
1004 {
1005   GtkWidget *child;
1006   GList *children;
1007   
1008   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1009   
1010   if (!menu->old_active_menu_item)
1011     {
1012       child = NULL;
1013       children = GTK_MENU_SHELL (menu)->children;
1014       
1015       while (children)
1016         {
1017           child = children->data;
1018           children = children->next;
1019           
1020           if (GTK_BIN (child)->child)
1021             break;
1022           child = NULL;
1023         }
1024       
1025       menu->old_active_menu_item = child;
1026       if (menu->old_active_menu_item)
1027         g_object_ref (menu->old_active_menu_item);
1028     }
1029   
1030   return menu->old_active_menu_item;
1031 }
1032
1033 void
1034 gtk_menu_set_active (GtkMenu *menu,
1035                      guint    index)
1036 {
1037   GtkWidget *child;
1038   GList *tmp_list;
1039   
1040   g_return_if_fail (GTK_IS_MENU (menu));
1041   
1042   tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1043   if (tmp_list)
1044     {
1045       child = tmp_list->data;
1046       if (GTK_BIN (child)->child)
1047         {
1048           if (menu->old_active_menu_item)
1049             g_object_unref (menu->old_active_menu_item);
1050           menu->old_active_menu_item = child;
1051           g_object_ref (menu->old_active_menu_item);
1052         }
1053     }
1054 }
1055
1056 void
1057 gtk_menu_set_accel_group (GtkMenu       *menu,
1058                           GtkAccelGroup *accel_group)
1059 {
1060   g_return_if_fail (GTK_IS_MENU (menu));
1061   
1062   if (menu->accel_group != accel_group)
1063     {
1064       if (menu->accel_group)
1065         g_object_unref (menu->accel_group);
1066       menu->accel_group = accel_group;
1067       if (menu->accel_group)
1068         g_object_ref (menu->accel_group);
1069       _gtk_menu_refresh_accel_paths (menu, TRUE);
1070     }
1071 }
1072
1073 GtkAccelGroup*
1074 gtk_menu_get_accel_group (GtkMenu *menu)
1075 {
1076   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1077
1078   return menu->accel_group;
1079 }
1080
1081 /**
1082  * gtk_menu_set_accel_path
1083  * @menu:       a valid #GtkMenu
1084  * @accel_path: a valid accelerator path
1085  *
1086  * Sets an accelerator path for this menu from which accelerator paths
1087  * for its immediate children, its menu items, can be constructed.
1088  * The main purpose of this function is to spare the programmer the
1089  * inconvenience of having to call gtk_menu_item_set_accel_path() on
1090  * each menu item that should support runtime user changable accelerators.
1091  * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1092  * each menu item of this menu, that contains a label describing its purpose,
1093  * automatically gets an accel path assigned. For example, a menu containing
1094  * menu items "New" and "Exit", will, after 
1095  * <literal>gtk_menu_set_accel_path (menu, "&lt;Gnumeric-Sheet&gt;/File");</literal>
1096  * has been called, assign its items the accel paths:
1097  * <literal>"&lt;Gnumeric-Sheet&gt;/File/New"</literal> and <literal>"&lt;Gnumeric-Sheet&gt;/File/Exit"</literal>.
1098  * Assigning accel paths to menu items then enables the user to change
1099  * their accelerators at runtime. More details about accelerator paths
1100  * and their default setups can be found at gtk_accel_map_add_entry().
1101  */
1102 void
1103 gtk_menu_set_accel_path (GtkMenu     *menu,
1104                          const gchar *accel_path)
1105 {
1106   g_return_if_fail (GTK_IS_MENU (menu));
1107   if (accel_path)
1108     g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1109
1110   g_free (menu->accel_path);
1111   menu->accel_path = g_strdup (accel_path);
1112   if (menu->accel_path)
1113     _gtk_menu_refresh_accel_paths (menu, FALSE);
1114 }
1115
1116 typedef struct {
1117   GtkMenu *menu;
1118   gboolean group_changed;
1119 } AccelPropagation;
1120
1121 static void
1122 refresh_accel_paths_foreach (GtkWidget *widget,
1123                              gpointer   data)
1124 {
1125   AccelPropagation *prop = data;
1126
1127   if (GTK_IS_MENU_ITEM (widget))        /* should always be true */
1128     _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1129                                        prop->menu->accel_path,
1130                                        prop->menu->accel_group,
1131                                        prop->group_changed);
1132 }
1133
1134 static void
1135 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1136                                gboolean group_changed)
1137 {
1138   g_return_if_fail (GTK_IS_MENU (menu));
1139       
1140   if (menu->accel_path && menu->accel_group)
1141     {
1142       AccelPropagation prop;
1143
1144       prop.menu = menu;
1145       prop.group_changed = group_changed;
1146       gtk_container_foreach (GTK_CONTAINER (menu),
1147                              refresh_accel_paths_foreach,
1148                              &prop);
1149     }
1150 }
1151
1152 void
1153 gtk_menu_reposition (GtkMenu *menu)
1154 {
1155   g_return_if_fail (GTK_IS_MENU (menu));
1156
1157   if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1158     gtk_menu_position (menu);
1159 }
1160
1161 static void
1162 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1163                             GtkMenu       *menu)
1164 {
1165   g_return_if_fail (GTK_IS_MENU (menu));
1166
1167   if (adjustment->value != menu->scroll_offset)
1168     gtk_menu_scroll_to (menu, adjustment->value);
1169 }
1170
1171 static void
1172 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1173                             gint     width)
1174 {
1175   GdkGeometry geometry_hints;
1176   
1177   if (!menu->tearoff_window)
1178     return;
1179
1180   if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1181     {
1182       gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1183       width += menu->tearoff_scrollbar->requisition.width;
1184     }
1185
1186   geometry_hints.min_width = width;
1187   geometry_hints.max_width = width;
1188     
1189   geometry_hints.min_height = 0;
1190   geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1191
1192   gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1193                                  NULL,
1194                                  &geometry_hints,
1195                                  GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1196 }
1197
1198 static void
1199 gtk_menu_update_title (GtkMenu *menu)
1200 {
1201   if (menu->tearoff_window)
1202     {
1203       const gchar *title;
1204       GtkWidget *attach_widget;
1205
1206       title = gtk_menu_get_title (menu);
1207       if (!title)
1208         {
1209           attach_widget = gtk_menu_get_attach_widget (menu);
1210           if (GTK_IS_MENU_ITEM (attach_widget))
1211             {
1212               GtkWidget *child = GTK_BIN (attach_widget)->child;
1213               if (GTK_IS_LABEL (child))
1214                 title = gtk_label_get_text (GTK_LABEL (child));
1215             }
1216         }
1217       
1218       if (title)
1219         gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1220     }
1221 }
1222
1223 void       
1224 gtk_menu_set_tearoff_state (GtkMenu  *menu,
1225                             gboolean  torn_off)
1226 {
1227   gint width, height;
1228   
1229   g_return_if_fail (GTK_IS_MENU (menu));
1230
1231   if (menu->torn_off != torn_off)
1232     {
1233       menu->torn_off = torn_off;
1234       menu->tearoff_active = torn_off;
1235       
1236       if (menu->torn_off)
1237         {
1238           if (GTK_WIDGET_VISIBLE (menu))
1239             gtk_menu_popdown (menu);
1240
1241           if (!menu->tearoff_window)
1242             {
1243               menu->tearoff_window = gtk_widget_new (GTK_TYPE_WINDOW,
1244                                                      "type", GTK_WINDOW_TOPLEVEL,
1245                                                      "screen", gtk_widget_get_screen (menu->toplevel),
1246                                                      "app_paintable", TRUE,
1247                                                      NULL);
1248
1249               gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1250                                         GDK_WINDOW_TYPE_HINT_MENU);
1251               gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1252               g_signal_connect (menu->tearoff_window, "destroy",
1253                                 G_CALLBACK (gtk_widget_destroyed), &menu->tearoff_window);
1254               g_signal_connect (menu->tearoff_window, "event",
1255                                 G_CALLBACK (gtk_menu_window_event), menu);
1256
1257               gtk_menu_update_title (menu);
1258
1259               gtk_widget_realize (menu->tearoff_window);
1260               
1261               menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1262               gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1263
1264               gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
1265               menu->tearoff_adjustment =
1266                 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1267                                                     0,
1268                                                     GTK_WIDGET (menu)->requisition.height,
1269                                                     MENU_SCROLL_STEP2,
1270                                                     height/2,
1271                                                     height));
1272               g_object_connect (menu->tearoff_adjustment,
1273                                 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1274                                 NULL);
1275               menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1276
1277               gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1278                                 menu->tearoff_scrollbar,
1279                                 FALSE, FALSE, 0);
1280               
1281               if (menu->tearoff_adjustment->upper > height)
1282                 gtk_widget_show (menu->tearoff_scrollbar);
1283               
1284               gtk_widget_show (menu->tearoff_hbox);
1285             }
1286           
1287           gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
1288
1289           gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
1290
1291           /* Update menu->requisition
1292            */
1293           gtk_widget_size_request (GTK_WIDGET (menu), NULL);
1294   
1295           gtk_menu_set_tearoff_hints (menu, width);
1296             
1297           gtk_widget_realize (menu->tearoff_window);
1298           gtk_menu_position (menu);
1299           
1300           gtk_widget_show (GTK_WIDGET (menu));
1301           gtk_widget_show (menu->tearoff_window);
1302
1303           gtk_menu_scroll_to (menu, 0);
1304
1305         }
1306       else
1307         {
1308           gtk_widget_hide (menu->tearoff_window);
1309           gtk_menu_reparent (menu, menu->toplevel, FALSE);
1310         }
1311     }
1312 }
1313
1314 /**
1315  * gtk_menu_get_tearoff_state:
1316  * @menu: a #GtkMenu
1317  *
1318  * Returns whether the menu is torn off. See
1319  * gtk_menu_set_tearoff_state ().
1320  *
1321  * Return value: %TRUE if the menu is currently torn off.
1322  **/
1323 gboolean
1324 gtk_menu_get_tearoff_state (GtkMenu *menu)
1325 {
1326   g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
1327
1328   return menu->torn_off;
1329 }
1330
1331 /**
1332  * gtk_menu_set_title:
1333  * @menu: a #GtkMenu
1334  * @title: a string containing the title for the menu.
1335  * 
1336  * Sets the title string for the menu.  The title is displayed when the menu
1337  * is shown as a tearoff menu.
1338  **/
1339 void       
1340 gtk_menu_set_title (GtkMenu     *menu,
1341                     const gchar *title)
1342 {
1343   g_return_if_fail (GTK_IS_MENU (menu));
1344
1345   if (title)
1346     g_object_set_data_full (G_OBJECT (menu), "gtk-menu-title",
1347                             g_strdup (title), (GtkDestroyNotify) g_free);
1348   else
1349     g_object_set_data (G_OBJECT (menu), "gtk-menu-title", NULL);
1350     
1351   gtk_menu_update_title (menu);
1352   g_object_notify (G_OBJECT (menu), "tearoff_title");
1353 }
1354
1355 /**
1356  * gtk_menu_get_title:
1357  * @menu: a #GtkMenu
1358  *
1359  * Returns the title of the menu. See gtk_menu_set_title().
1360  *
1361  * Return value: the title of the menu, or %NULL if the menu has no
1362  * title set on it. This string is owned by the widget and should
1363  * not be modified or freed.
1364  **/
1365 G_CONST_RETURN gchar *
1366 gtk_menu_get_title (GtkMenu *menu)
1367 {
1368   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1369
1370   return g_object_get_data (G_OBJECT (menu), "gtk-menu-title");
1371 }
1372
1373 void
1374 gtk_menu_reorder_child (GtkMenu   *menu,
1375                         GtkWidget *child,
1376                         gint       position)
1377 {
1378   GtkMenuShell *menu_shell;
1379   g_return_if_fail (GTK_IS_MENU (menu));
1380   g_return_if_fail (GTK_IS_MENU_ITEM (child));
1381   menu_shell = GTK_MENU_SHELL (menu);
1382   if (g_list_find (menu_shell->children, child))
1383     {   
1384       menu_shell->children = g_list_remove (menu_shell->children, child);
1385       menu_shell->children = g_list_insert (menu_shell->children, child, position);   
1386       if (GTK_WIDGET_VISIBLE (menu_shell))
1387         gtk_widget_queue_resize (GTK_WIDGET (menu_shell));
1388     }   
1389 }
1390
1391 static void
1392 gtk_menu_style_set (GtkWidget *widget,
1393                     GtkStyle  *previous_style)
1394 {
1395   if (GTK_WIDGET_REALIZED (widget))
1396     {
1397       GtkMenu *menu = GTK_MENU (widget);
1398       
1399       gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1400       gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1401       gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1402     }
1403 }
1404
1405 static void
1406 gtk_menu_realize (GtkWidget *widget)
1407 {
1408   GdkWindowAttr attributes;
1409   gint attributes_mask;
1410   gint border_width;
1411   GtkMenu *menu;
1412   GtkWidget *child;
1413   GList *children;
1414
1415   g_return_if_fail (GTK_IS_MENU (widget));
1416
1417   menu = GTK_MENU (widget);
1418   
1419   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1420   
1421   attributes.window_type = GDK_WINDOW_CHILD;
1422   attributes.x = widget->allocation.x;
1423   attributes.y = widget->allocation.y;
1424   attributes.width = widget->allocation.width;
1425   attributes.height = widget->allocation.height;
1426   attributes.wclass = GDK_INPUT_OUTPUT;
1427   attributes.visual = gtk_widget_get_visual (widget);
1428   attributes.colormap = gtk_widget_get_colormap (widget);
1429   
1430   attributes.event_mask = gtk_widget_get_events (widget);
1431   attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
1432                             GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
1433   
1434   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1435   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1436   gdk_window_set_user_data (widget->window, widget);
1437   
1438   border_width = GTK_CONTAINER (widget)->border_width;
1439   
1440   attributes.x = border_width + widget->style->xthickness;
1441   attributes.y = border_width + widget->style->ythickness;
1442   attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
1443   attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
1444
1445   if (menu->upper_arrow_visible)
1446     {
1447       attributes.y += MENU_SCROLL_ARROW_HEIGHT;
1448       attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1449     }
1450   if (menu->lower_arrow_visible)
1451     attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1452
1453   menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
1454   gdk_window_set_user_data (menu->view_window, menu);
1455
1456   attributes.x = 0;
1457   attributes.y = 0;
1458   attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness) * 2);
1459   
1460   menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
1461   gdk_window_set_user_data (menu->bin_window, menu);
1462
1463   children = GTK_MENU_SHELL (menu)->children;
1464   while (children)
1465     {
1466       child = children->data;
1467       children = children->next;
1468           
1469       gtk_widget_set_parent_window (child, menu->bin_window);
1470     }
1471   
1472   widget->style = gtk_style_attach (widget->style, widget->window);
1473   gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1474   gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1475   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1476
1477   if (GTK_MENU_SHELL (widget)->active_menu_item)
1478     gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
1479                                   GTK_MENU_SHELL (widget)->active_menu_item);
1480
1481   gdk_window_show (menu->bin_window);
1482   gdk_window_show (menu->view_window);
1483 }
1484
1485 static gboolean 
1486 gtk_menu_focus (GtkWidget       *widget,
1487                 GtkDirectionType direction)
1488 {
1489   /*
1490    * A menu or its menu items cannot have focus
1491    */
1492   return FALSE;
1493 }
1494
1495 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
1496  */
1497 static GdkWindow *
1498 menu_grab_transfer_window_get (GtkMenu *menu)
1499 {
1500   GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1501   if (!window)
1502     {
1503       GdkWindowAttr attributes;
1504       gint attributes_mask;
1505       
1506       attributes.x = -100;
1507       attributes.y = -100;
1508       attributes.width = 10;
1509       attributes.height = 10;
1510       attributes.window_type = GDK_WINDOW_TEMP;
1511       attributes.wclass = GDK_INPUT_ONLY;
1512       attributes.override_redirect = TRUE;
1513       attributes.event_mask = 0;
1514
1515       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
1516       
1517       window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
1518                                &attributes, attributes_mask);
1519       gdk_window_set_user_data (window, menu);
1520
1521       gdk_window_show (window);
1522
1523       g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", window);
1524     }
1525
1526   return window;
1527 }
1528
1529 static void
1530 menu_grab_transfer_window_destroy (GtkMenu *menu)
1531 {
1532   GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1533   if (window)
1534     {
1535       gdk_window_set_user_data (window, NULL);
1536       gdk_window_destroy (window);
1537       g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", NULL);
1538     }
1539 }
1540
1541 static void
1542 gtk_menu_unrealize (GtkWidget *widget)
1543 {
1544   GtkMenu *menu;
1545
1546   g_return_if_fail (GTK_IS_MENU (widget));
1547
1548   menu = GTK_MENU (widget);
1549
1550   menu_grab_transfer_window_destroy (menu);
1551
1552   gdk_window_set_user_data (menu->view_window, NULL);
1553   gdk_window_destroy (menu->view_window);
1554   menu->view_window = NULL;
1555
1556   gdk_window_set_user_data (menu->bin_window, NULL);
1557   gdk_window_destroy (menu->bin_window);
1558   menu->bin_window = NULL;
1559
1560   (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1561 }
1562
1563 static void
1564 gtk_menu_size_request (GtkWidget      *widget,
1565                        GtkRequisition *requisition)
1566 {
1567   GtkMenu *menu;
1568   GtkMenuShell *menu_shell;
1569   GtkWidget *child;
1570   GList *children;
1571   guint max_toggle_size;
1572   guint max_accel_width;
1573   GtkRequisition child_requisition;
1574   
1575   g_return_if_fail (GTK_IS_MENU (widget));
1576   g_return_if_fail (requisition != NULL);
1577   
1578   menu = GTK_MENU (widget);
1579   menu_shell = GTK_MENU_SHELL (widget);
1580   
1581   requisition->width = 0;
1582   requisition->height = 0;
1583   
1584   max_toggle_size = 0;
1585   max_accel_width = 0;
1586   
1587   children = menu_shell->children;
1588   while (children)
1589     {
1590       child = children->data;
1591       children = children->next;
1592       
1593       if (GTK_WIDGET_VISIBLE (child))
1594         {
1595           gint toggle_size;
1596
1597           /* It's important to size_request the child
1598            * before doing the toggle size request, in
1599            * case the toggle size request depends on the size
1600            * request of a child of the child (e.g. for ImageMenuItem)
1601            */
1602           
1603           GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
1604           gtk_widget_size_request (child, &child_requisition);
1605           
1606           requisition->width = MAX (requisition->width, child_requisition.width);
1607           requisition->height += child_requisition.height;
1608
1609           gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
1610           max_toggle_size = MAX (max_toggle_size, toggle_size);
1611           max_accel_width = MAX (max_accel_width, GTK_MENU_ITEM (child)->accelerator_width);
1612         }
1613     }
1614   
1615   requisition->width += max_toggle_size + max_accel_width;
1616   requisition->width += (GTK_CONTAINER (menu)->border_width +
1617                          widget->style->xthickness) * 2;
1618   requisition->height += (GTK_CONTAINER (menu)->border_width +
1619                           widget->style->ythickness) * 2;
1620   
1621   menu->toggle_size = max_toggle_size;
1622
1623   /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
1624    */
1625   if (menu->tearoff_active)
1626     gtk_menu_set_tearoff_hints (menu, requisition->width);
1627 }
1628
1629 static void
1630 gtk_menu_size_allocate (GtkWidget     *widget,
1631                         GtkAllocation *allocation)
1632 {
1633   GtkMenu *menu;
1634   GtkMenuShell *menu_shell;
1635   GtkWidget *child;
1636   GtkAllocation child_allocation;
1637   GList *children;
1638   gint x, y;
1639   gint width, height;
1640
1641   g_return_if_fail (GTK_IS_MENU (widget));
1642   g_return_if_fail (allocation != NULL);
1643   
1644   menu = GTK_MENU (widget);
1645   menu_shell = GTK_MENU_SHELL (widget);
1646
1647   widget->allocation = *allocation;
1648
1649   x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness;
1650   y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness;
1651   
1652   width = MAX (1, allocation->width - x * 2);
1653   height = MAX (1, allocation->height - y * 2);
1654
1655   if (menu_shell->active)
1656     gtk_menu_scroll_to (menu, menu->scroll_offset);
1657   
1658   if (menu->upper_arrow_visible && !menu->tearoff_active)
1659     {
1660       y += MENU_SCROLL_ARROW_HEIGHT;
1661       height -= MENU_SCROLL_ARROW_HEIGHT;
1662     }
1663   
1664   if (menu->lower_arrow_visible && !menu->tearoff_active)
1665     height -= MENU_SCROLL_ARROW_HEIGHT;
1666   
1667   if (GTK_WIDGET_REALIZED (widget))
1668     {
1669       gdk_window_move_resize (widget->window,
1670                               allocation->x, allocation->y,
1671                               allocation->width, allocation->height);
1672
1673       gdk_window_move_resize (menu->view_window,
1674                               x,
1675                               y,
1676                               width,
1677                               height);
1678     }
1679
1680   if (menu_shell->children)
1681     {
1682       child_allocation.x = 0;
1683       child_allocation.y = 0;
1684       child_allocation.width = width;
1685       
1686       children = menu_shell->children;
1687       while (children)
1688         {
1689           child = children->data;
1690           children = children->next;
1691           
1692           if (GTK_WIDGET_VISIBLE (child))
1693             {
1694               GtkRequisition child_requisition;
1695               gtk_widget_get_child_requisition (child, &child_requisition);
1696               
1697               child_allocation.height = child_requisition.height;
1698
1699               gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
1700                                                   menu->toggle_size);
1701               gtk_widget_size_allocate (child, &child_allocation);
1702               gtk_widget_queue_draw (child);
1703               
1704               child_allocation.y += child_allocation.height;
1705             }
1706         }
1707       
1708       /* Resize the item window */
1709       if (GTK_WIDGET_REALIZED (widget))
1710         {
1711           gdk_window_resize (menu->bin_window,
1712                              child_allocation.width,
1713                              child_allocation.y);
1714         }
1715
1716
1717       if (menu->tearoff_active)
1718         {
1719           if (allocation->height >= widget->requisition.height)
1720             {
1721               if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1722                 {
1723                   gtk_widget_hide (menu->tearoff_scrollbar);
1724                   gtk_menu_set_tearoff_hints (menu, allocation->width);
1725
1726                   gtk_menu_scroll_to (menu, 0);
1727                 }
1728             }
1729           else
1730             {
1731               menu->tearoff_adjustment->upper = widget->requisition.height;
1732               menu->tearoff_adjustment->page_size = allocation->height;
1733               
1734               if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
1735                   menu->tearoff_adjustment->upper)
1736                 {
1737                   gint value;
1738                   value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
1739                   if (value < 0)
1740                     value = 0;
1741                   gtk_menu_scroll_to (menu, value);
1742                 }
1743               
1744               gtk_adjustment_changed (menu->tearoff_adjustment);
1745               
1746               if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1747                 {
1748                   gtk_widget_show (menu->tearoff_scrollbar);
1749                   gtk_menu_set_tearoff_hints (menu, allocation->width);
1750                 }
1751             }
1752         }
1753     }
1754 }
1755
1756 static void
1757 gtk_menu_paint (GtkWidget      *widget,
1758                 GdkEventExpose *event)
1759 {
1760   GtkMenu *menu;
1761   gint width, height;
1762   gint border_x, border_y;
1763   
1764   g_return_if_fail (GTK_IS_MENU (widget));
1765
1766   menu = GTK_MENU (widget);
1767   
1768   border_x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness;
1769   border_y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness;
1770   gdk_drawable_get_size (widget->window, &width, &height);
1771
1772   if (event->window == widget->window)
1773     {
1774       gtk_paint_box (widget->style,
1775                      widget->window,
1776                      GTK_STATE_NORMAL,
1777                      GTK_SHADOW_OUT,
1778                      NULL, widget, "menu",
1779                      0, 0, -1, -1);
1780       if (menu->upper_arrow_visible && !menu->tearoff_active)
1781         {
1782           gtk_paint_box (widget->style,
1783                          widget->window,
1784                          menu->upper_arrow_prelight ?
1785                          GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1786                          GTK_SHADOW_OUT,
1787                          NULL, widget, "menu",
1788                          border_x,
1789                          border_y,
1790                          width - 2*border_x,
1791                          MENU_SCROLL_ARROW_HEIGHT);
1792           
1793           gtk_paint_arrow (widget->style,
1794                            widget->window,
1795                            menu->upper_arrow_prelight ?
1796                            GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1797                            GTK_SHADOW_OUT,
1798                            NULL, widget, "menu",
1799                            GTK_ARROW_UP,
1800                            TRUE,
1801                            width / 2 - MENU_SCROLL_ARROW_HEIGHT / 2 + 1,
1802                            2 * border_y + 1,
1803                            MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2,
1804                            MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2);
1805         }
1806   
1807       if (menu->lower_arrow_visible && !menu->tearoff_active)
1808         {
1809           gtk_paint_box (widget->style,
1810                          widget->window,
1811                          menu->lower_arrow_prelight ?
1812                          GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1813                          GTK_SHADOW_OUT,
1814                          NULL, widget, "menu",
1815                          border_x,
1816                          height - border_y - MENU_SCROLL_ARROW_HEIGHT + 1,
1817                          width - 2*border_x,
1818                          MENU_SCROLL_ARROW_HEIGHT);
1819           
1820           gtk_paint_arrow (widget->style,
1821                            widget->window,
1822                            menu->lower_arrow_prelight ?
1823                            GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1824                            GTK_SHADOW_OUT,
1825                            NULL, widget, "menu",
1826                            GTK_ARROW_DOWN,
1827                            TRUE,
1828                            width / 2 - MENU_SCROLL_ARROW_HEIGHT / 2 + 1,
1829                            height - MENU_SCROLL_ARROW_HEIGHT + 1,
1830                            MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2,
1831                            MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2);
1832         }
1833     }
1834 }
1835
1836 static gboolean
1837 gtk_menu_expose (GtkWidget      *widget,
1838                  GdkEventExpose *event)
1839 {
1840   g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
1841   g_return_val_if_fail (event != NULL, FALSE);
1842
1843   if (GTK_WIDGET_DRAWABLE (widget))
1844     {
1845       gtk_menu_paint (widget, event);
1846       
1847       (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
1848     }
1849   
1850   return FALSE;
1851 }
1852
1853 static void
1854 gtk_menu_show (GtkWidget *widget)
1855 {
1856   GtkMenu *menu = GTK_MENU (widget);
1857
1858   _gtk_menu_refresh_accel_paths (menu, FALSE);
1859
1860   GTK_WIDGET_CLASS (parent_class)->show (widget);
1861 }
1862
1863 static gboolean
1864 gtk_menu_button_press (GtkWidget      *widget,
1865                          GdkEventButton *event)
1866 {
1867   /* Don't pop down the menu for releases over scroll arrows
1868    */
1869   if (GTK_IS_MENU (widget))
1870     {
1871       GtkMenu *menu = GTK_MENU (widget);
1872
1873       if (menu->upper_arrow_prelight ||  menu->lower_arrow_prelight)
1874         return TRUE;
1875     }
1876
1877   return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
1878 }
1879
1880 static gboolean
1881 gtk_menu_button_release (GtkWidget      *widget,
1882                          GdkEventButton *event)
1883 {
1884   /* Don't pop down the menu for releases over scroll arrows
1885    */
1886   if (GTK_IS_MENU (widget))
1887     {
1888       GtkMenu *menu = GTK_MENU (widget);
1889
1890       if (menu->upper_arrow_prelight ||  menu->lower_arrow_prelight)
1891         return TRUE;
1892     }
1893
1894   return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
1895 }
1896
1897 static gboolean
1898 gtk_menu_key_press (GtkWidget   *widget,
1899                     GdkEventKey *event)
1900 {
1901   GtkMenuShell *menu_shell;
1902   GtkMenu *menu;
1903   gboolean delete = FALSE;
1904   gboolean can_change_accels;
1905   gchar *accel = NULL;
1906   guint accel_key, accel_mods;
1907   GdkModifierType consumed_modifiers;
1908   GdkDisplay *display;
1909   
1910   g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
1911   g_return_val_if_fail (event != NULL, FALSE);
1912       
1913   menu_shell = GTK_MENU_SHELL (widget);
1914   menu = GTK_MENU (widget);
1915   
1916   gtk_menu_stop_navigating_submenu (menu);
1917
1918   if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
1919     return TRUE;
1920
1921   display = gtk_widget_get_display (widget);
1922     
1923   g_object_get (G_OBJECT (gtk_widget_get_settings (widget)),
1924                 "gtk-menu-bar-accel", &accel,
1925                 "gtk-can-change-accels", &can_change_accels,
1926                 NULL);
1927
1928   if (accel)
1929     {
1930       guint keyval = 0;
1931       GdkModifierType mods = 0;
1932       gboolean handled = FALSE;
1933       
1934       gtk_accelerator_parse (accel, &keyval, &mods);
1935
1936       if (keyval == 0)
1937         g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
1938
1939       /* FIXME this is wrong, needs to be in the global accel resolution
1940        * thing, to properly consider i18n etc., but that probably requires
1941        * AccelGroup changes etc.
1942        */
1943       if (event->keyval == keyval &&
1944           (mods & event->state) == mods)
1945         {
1946           g_signal_emit_by_name (menu, "cancel", 0);
1947         }
1948
1949       g_free (accel);
1950
1951       if (handled)
1952         return TRUE;
1953     }
1954   
1955   switch (event->keyval)
1956     {
1957     case GDK_Delete:
1958     case GDK_KP_Delete:
1959     case GDK_BackSpace:
1960       delete = TRUE;
1961       break;
1962     default:
1963       break;
1964     }
1965
1966   /* Figure out what modifiers went into determining the key symbol */
1967   gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
1968                                        event->hardware_keycode, event->state, event->group,
1969                                        NULL, NULL, NULL, &consumed_modifiers);
1970
1971   accel_key = gdk_keyval_to_lower (event->keyval);
1972   accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
1973
1974   /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
1975    * We re-upper case when we match against the keyval, but display and save in caseless form.
1976    */
1977   if (accel_key != event->keyval)
1978     accel_mods |= GDK_SHIFT_MASK;
1979   
1980   /* Modify the accelerators */
1981   if (can_change_accels &&
1982       menu_shell->active_menu_item &&
1983       GTK_BIN (menu_shell->active_menu_item)->child &&                  /* no seperators */
1984       GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL &&  /* no submenus */
1985       (delete || gtk_accelerator_valid (accel_key, accel_mods)))
1986     {
1987       GtkWidget *menu_item = menu_shell->active_menu_item;
1988       gboolean locked, replace_accels = TRUE;
1989       const gchar *path;
1990
1991       path = _gtk_widget_get_accel_path (menu_item, &locked);
1992       if (!path || locked)
1993         {
1994           /* can't change accelerators on menu_items without paths
1995            * (basically, those items are accelerator-locked).
1996            */
1997           /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
1998           gdk_display_beep (display);
1999         }
2000       else
2001         {
2002           gboolean changed;
2003
2004           /* For the keys that act to delete the current setting, we delete
2005            * the current setting if there is one, otherwise, we set the
2006            * key as the accelerator.
2007            */
2008           if (delete)
2009             {
2010               GtkAccelKey key;
2011               
2012               if (gtk_accel_map_lookup_entry (path, &key) &&
2013                   (key.accel_key || key.accel_mods))
2014                 {
2015                   accel_key = 0;
2016                   accel_mods = 0;
2017                 }
2018             }
2019           changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
2020
2021           if (!changed)
2022             {
2023               /* we failed, probably because this key is in use and
2024                * locked already
2025                */
2026               /* g_print("failed to change\n"); */
2027               gdk_display_beep (display);
2028             }
2029         }
2030     }
2031   
2032   return TRUE;
2033 }
2034
2035 static gboolean
2036 gtk_menu_motion_notify  (GtkWidget         *widget,
2037                          GdkEventMotion    *event)
2038 {
2039   GtkWidget *menu_item;
2040   GtkMenu *menu;
2041   GtkMenuShell *menu_shell;
2042
2043   gboolean need_enter;
2044
2045   if (GTK_IS_MENU (widget))
2046     gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2047
2048   /* We received the event for one of two reasons:
2049    *
2050    * a) We are the active menu, and did gtk_grab_add()
2051    * b) The widget is a child of ours, and the event was propagated
2052    *
2053    * Since for computation of navigation regions, we want the menu which
2054    * is the parent of the menu item, for a), we need to find that menu,
2055    * which may be different from 'widget'.
2056    */
2057   menu_item = gtk_get_event_widget ((GdkEvent*) event);
2058   if (!menu_item || !GTK_IS_MENU_ITEM (menu_item) ||
2059       !_gtk_menu_item_is_selectable (menu_item) ||
2060       !GTK_IS_MENU (menu_item->parent))
2061     return FALSE;
2062
2063   menu_shell = GTK_MENU_SHELL (menu_item->parent);
2064   menu = GTK_MENU (menu_shell);
2065   
2066   need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
2067
2068   /* Check to see if we are within an active submenu's navigation region
2069    */
2070   if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2071     return TRUE; 
2072
2073   if (need_enter)
2074     {
2075       /* The menu is now sensitive to enter events on its items, but
2076        * was previously sensitive.  So we fake an enter event.
2077        */
2078       gint width, height;
2079       
2080       menu_shell->ignore_enter = FALSE; 
2081       
2082       gdk_drawable_get_size (event->window, &width, &height);
2083       if (event->x >= 0 && event->x < width &&
2084           event->y >= 0 && event->y < height)
2085         {
2086           GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2087           gboolean result;
2088
2089           send_event->crossing.window = g_object_ref (event->window);
2090           send_event->crossing.time = event->time;
2091           send_event->crossing.send_event = TRUE;
2092           send_event->crossing.x_root = event->x_root;
2093           send_event->crossing.y_root = event->y_root;
2094           send_event->crossing.x = event->x;
2095           send_event->crossing.y = event->y;
2096
2097           /* We send the event to 'widget', the currently active menu,
2098            * instead of 'menu', the menu that the pointer is in. This
2099            * will ensure that the event will be ignored unless the
2100            * menuitem is a child of the active menu or some parent
2101            * menu of the active menu.
2102            */
2103           result = gtk_widget_event (widget, send_event);
2104           gdk_event_free (send_event);
2105
2106           return result;
2107         }
2108     }
2109
2110   return FALSE;
2111 }
2112
2113 static gboolean
2114 gtk_menu_scroll_timeout (gpointer  data)
2115 {
2116   GtkMenu *menu;
2117   GtkWidget *widget;
2118   gint offset;
2119   gint view_width, view_height;
2120
2121   GDK_THREADS_ENTER ();
2122
2123   menu = GTK_MENU (data);
2124   widget = GTK_WIDGET (menu);
2125
2126   offset = menu->scroll_offset + menu->scroll_step;
2127
2128   /* If we scroll upward and the non-visible top part
2129    * is smaller than the scroll arrow it would be
2130    * pretty stupid to show the arrow and taking more
2131    * screen space than just scrolling to the top.
2132    */
2133   if ((menu->scroll_step < 0) && (offset < MENU_SCROLL_ARROW_HEIGHT))
2134     offset = 0;
2135
2136   /* Don't scroll over the top if we weren't before: */
2137   if ((menu->scroll_offset >= 0) && (offset < 0))
2138     offset = 0;
2139
2140   gdk_drawable_get_size (widget->window, &view_width, &view_height);
2141
2142   /* Don't scroll past the bottom if we weren't before: */
2143   if (menu->scroll_offset > 0)
2144     view_height -= MENU_SCROLL_ARROW_HEIGHT;
2145   
2146   if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
2147       (offset + view_height > widget->requisition.height))
2148     offset = widget->requisition.height - view_height;
2149
2150   gtk_menu_scroll_to (menu, offset);
2151
2152   GDK_THREADS_LEAVE ();
2153
2154   return TRUE;
2155 }
2156
2157 static void
2158 gtk_menu_handle_scrolling (GtkMenu *menu, gboolean enter)
2159 {
2160   GtkMenuShell *menu_shell;
2161   gint width, height;
2162   gint x, y;
2163   gint border;
2164   GdkRectangle rect;
2165   gboolean in_arrow;
2166   gboolean scroll_fast = FALSE;
2167
2168   menu_shell = GTK_MENU_SHELL (menu);
2169
2170   gdk_window_get_pointer (GTK_WIDGET (menu)->window, &x, &y, NULL);
2171   gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
2172
2173   border = GTK_CONTAINER (menu)->border_width + GTK_WIDGET (menu)->style->ythickness;
2174
2175   if (menu->upper_arrow_visible && !menu->tearoff_active)
2176     {
2177       rect.x = 0;
2178       rect.y = 0;
2179       rect.width = width;
2180       rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2181       
2182       in_arrow = FALSE;
2183       if ((x >= rect.x) && (x < rect.x + rect.width) &&
2184           (y >= rect.y) && (y < rect.y + rect.height))
2185         {
2186           in_arrow = TRUE;
2187           scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
2188         }
2189         
2190       if (enter && in_arrow &&
2191           (!menu->upper_arrow_prelight || menu->scroll_fast != scroll_fast))
2192         {
2193           menu->upper_arrow_prelight = TRUE;
2194           menu->scroll_fast = scroll_fast;
2195           gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2196           
2197           /* Deselect the active item so that any submenus are poped down */
2198           gtk_menu_shell_deselect (menu_shell);
2199
2200           gtk_menu_remove_scroll_timeout (menu);
2201           menu->scroll_step = (scroll_fast) ? -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
2202           menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2203                                             gtk_menu_scroll_timeout,
2204                                             menu);
2205         }
2206       else if (!enter && !in_arrow && menu->upper_arrow_prelight)
2207         {
2208           gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2209           
2210           gtk_menu_stop_scrolling (menu);
2211         }
2212     }
2213   
2214   if (menu->lower_arrow_visible && !menu->tearoff_active)
2215     {
2216       rect.x = 0;
2217       rect.y = height - border - MENU_SCROLL_ARROW_HEIGHT;
2218       rect.width = width;
2219       rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2220
2221       in_arrow = FALSE;
2222       if ((x >= rect.x) && (x < rect.x + rect.width) &&
2223           (y >= rect.y) && (y < rect.y + rect.height))
2224         {
2225           in_arrow = TRUE;
2226           scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
2227         }
2228
2229       if (enter && in_arrow &&
2230           (!menu->lower_arrow_prelight || menu->scroll_fast != scroll_fast))
2231         {
2232           menu->lower_arrow_prelight = TRUE;
2233           menu->scroll_fast = scroll_fast;
2234           gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2235
2236           /* Deselect the active item so that any submenus are poped down */
2237           gtk_menu_shell_deselect (menu_shell);
2238
2239           gtk_menu_remove_scroll_timeout (menu);
2240           menu->scroll_step = (scroll_fast) ? MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
2241           menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2242                                             gtk_menu_scroll_timeout,
2243                                             menu);
2244         }
2245       else if (!enter && !in_arrow && menu->lower_arrow_prelight)
2246         {
2247           gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2248           
2249           gtk_menu_stop_scrolling (menu);
2250         }
2251     }
2252 }
2253
2254 static gboolean
2255 gtk_menu_enter_notify (GtkWidget        *widget,
2256                        GdkEventCrossing *event)
2257 {
2258   GtkWidget *menu_item;
2259
2260   if (widget && GTK_IS_MENU (widget))
2261     {
2262       GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
2263
2264       if (!menu_shell->ignore_enter)
2265         gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2266     }
2267   
2268   /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
2269    * will not correspond to the event widget's parent.  Check to see
2270    * if we are in the parent's navigation region.
2271    */
2272   menu_item = gtk_get_event_widget ((GdkEvent*) event);
2273   if (menu_item && GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
2274       gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent), event->x_root, event->y_root))
2275     return TRUE;
2276
2277   return GTK_WIDGET_CLASS (parent_class)->enter_notify_event (widget, event); 
2278 }
2279
2280 static gboolean
2281 gtk_menu_leave_notify (GtkWidget        *widget,
2282                        GdkEventCrossing *event)
2283 {
2284   GtkMenuShell *menu_shell;
2285   GtkMenu *menu;
2286   GtkMenuItem *menu_item;
2287   GtkWidget *event_widget;
2288
2289   menu = GTK_MENU (widget);
2290   menu_shell = GTK_MENU_SHELL (widget); 
2291   
2292   if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2293     return TRUE; 
2294
2295   gtk_menu_handle_scrolling (menu, FALSE);
2296   
2297   event_widget = gtk_get_event_widget ((GdkEvent*) event);
2298   
2299   if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
2300     return TRUE;
2301   
2302   menu_item = GTK_MENU_ITEM (event_widget); 
2303   
2304   /* Here we check to see if we're leaving an active menu item with a submenu, 
2305    * in which case we enter submenu navigation mode. 
2306    */
2307   if (menu_shell->active_menu_item != NULL
2308       && menu_item->submenu != NULL
2309       && menu_item->submenu_placement == GTK_LEFT_RIGHT)
2310     {
2311       if (GTK_MENU_SHELL (menu_item->submenu)->active)
2312         {
2313           gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
2314           return TRUE;
2315         }
2316     }
2317   
2318   return GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event); 
2319 }
2320
2321 static void 
2322 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
2323 {
2324   if (menu->navigation_region) 
2325     {
2326       gdk_region_destroy (menu->navigation_region);
2327       menu->navigation_region = NULL;
2328     }
2329   
2330   if (menu->navigation_timeout)
2331     {
2332       gtk_timeout_remove (menu->navigation_timeout);
2333       menu->navigation_timeout = 0;
2334     }
2335 }
2336
2337 /* When the timeout is elapsed, the navigation region is destroyed
2338  * and the menuitem under the pointer (if any) is selected.
2339  */
2340 static gboolean
2341 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
2342 {
2343   GtkMenu *menu = user_data;
2344   GdkWindow *child_window;
2345
2346   GDK_THREADS_ENTER ();
2347
2348   gtk_menu_stop_navigating_submenu (menu);
2349   
2350   if (GTK_WIDGET_REALIZED (menu))
2351     {
2352       child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
2353
2354       if (child_window)
2355         {
2356           GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2357
2358           send_event->crossing.window = g_object_ref (child_window);
2359           send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
2360           send_event->crossing.send_event = TRUE;
2361
2362           GTK_WIDGET_CLASS (parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
2363
2364           gdk_event_free (send_event);
2365         }
2366     }
2367
2368   GDK_THREADS_LEAVE ();
2369
2370   return FALSE; 
2371 }
2372
2373 static gboolean
2374 gtk_menu_navigating_submenu (GtkMenu *menu,
2375                              gint     event_x,
2376                              gint     event_y)
2377 {
2378   if (menu->navigation_region)
2379     {
2380       if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
2381         return TRUE;
2382       else
2383         {
2384           gtk_menu_stop_navigating_submenu (menu);
2385           return FALSE;
2386         }
2387     }
2388   return FALSE;
2389 }
2390
2391 #undef DRAW_STAY_UP_TRIANGLE
2392
2393 #ifdef DRAW_STAY_UP_TRIANGLE
2394
2395 static void
2396 draw_stay_up_triangle (GdkWindow *window,
2397                        GdkRegion *region)
2398 {
2399   /* Draw ugly color all over the stay-up triangle */
2400   GdkColor ugly_color = { 0, 50000, 10000, 10000 };
2401   GdkGCValues gc_values;
2402   GdkGC *ugly_gc;
2403   GdkRectangle clipbox;
2404
2405   gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
2406   ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
2407   gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
2408   gdk_gc_set_clip_region (ugly_gc, region);
2409
2410   gdk_region_get_clipbox (region, &clipbox);
2411   
2412   gdk_draw_rectangle (window,
2413                      ugly_gc,
2414                      TRUE,
2415                      clipbox.x, clipbox.y,
2416                      clipbox.width, clipbox.height);
2417   
2418   g_object_unref (G_OBJECT (ugly_gc));
2419 }
2420 #endif
2421
2422 static GdkRegion *
2423 flip_region (GdkRegion *region,
2424              gboolean   flip_x,
2425              gboolean   flip_y)
2426 {
2427   gint n_rectangles;
2428   GdkRectangle *rectangles;
2429   GdkRectangle clipbox;
2430   GdkRegion *new_region;
2431   gint i;
2432
2433   new_region = gdk_region_new ();
2434   
2435   gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
2436   gdk_region_get_clipbox (region, &clipbox);
2437
2438   for (i = 0; i < n_rectangles; ++i)
2439     {
2440       GdkRectangle rect = rectangles[i];
2441
2442       if (flip_y)
2443         rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
2444
2445       if (flip_x)
2446         rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
2447
2448       gdk_region_union_with_rect (new_region, &rect);
2449     }
2450
2451   g_free (rectangles);
2452
2453   return new_region;
2454 }
2455
2456 static void
2457 gtk_menu_set_submenu_navigation_region (GtkMenu          *menu,
2458                                         GtkMenuItem      *menu_item,
2459                                         GdkEventCrossing *event)
2460 {
2461   gint submenu_left = 0;
2462   gint submenu_right = 0;
2463   gint submenu_top = 0;
2464   gint submenu_bottom = 0;
2465   gint width = 0;
2466   gint height = 0;
2467   GdkPoint point[3];
2468   GtkWidget *event_widget;
2469
2470   g_return_if_fail (menu_item->submenu != NULL);
2471   g_return_if_fail (event != NULL);
2472   
2473   event_widget = gtk_get_event_widget ((GdkEvent*) event);
2474   
2475   gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
2476   gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
2477   
2478   submenu_right = submenu_left + width;
2479   submenu_bottom = submenu_top + height;
2480   
2481   gdk_drawable_get_size (event_widget->window, &width, &height);
2482   
2483   if (event->x >= 0 && event->x < width)
2484     {
2485       gint popdown_delay;
2486       gboolean flip_y = FALSE;
2487       gboolean flip_x = FALSE;
2488       
2489       gtk_menu_stop_navigating_submenu (menu);
2490
2491       if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
2492         {
2493           /* right */
2494           point[0].x = event->x_root;
2495           point[1].x = submenu_left;
2496         }
2497       else
2498         {
2499           /* left */
2500           point[0].x = event->x_root + 1;
2501           point[1].x = 2 * (event->x_root + 1) - submenu_right;
2502
2503           flip_x = TRUE;
2504         }
2505
2506       if (event->y < 0)
2507         {
2508           /* top */
2509           point[0].y = event->y_root + 1;
2510           point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
2511
2512           if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
2513             return;
2514
2515           flip_y = TRUE;
2516         }
2517       else
2518         {
2519           /* bottom */
2520           point[0].y = event->y_root;
2521           point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
2522
2523           if (point[0].y >= submenu_bottom)
2524             return;
2525         }
2526
2527       point[2].x = point[1].x;
2528       point[2].y = point[0].y;
2529
2530       menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
2531
2532       if (flip_x || flip_y)
2533         {
2534           GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
2535           gdk_region_destroy (menu->navigation_region);
2536           menu->navigation_region = new_region;
2537         }
2538
2539       g_object_get (G_OBJECT (gtk_widget_get_settings (GTK_WIDGET (menu))),
2540                     "gtk-menu-popdown-delay", &popdown_delay,
2541                     NULL);
2542
2543       menu->navigation_timeout = gtk_timeout_add (popdown_delay,
2544                                                   gtk_menu_stop_navigating_submenu_cb, menu);
2545
2546 #ifdef DRAW_STAY_UP_TRIANGLE
2547       draw_stay_up_triangle (gdk_get_default_root_window(),
2548                              menu->navigation_region);
2549 #endif
2550     }
2551 }
2552
2553 static void
2554 gtk_menu_deactivate (GtkMenuShell *menu_shell)
2555 {
2556   GtkWidget *parent;
2557   
2558   g_return_if_fail (GTK_IS_MENU (menu_shell));
2559   
2560   parent = menu_shell->parent_menu_shell;
2561   
2562   menu_shell->activate_time = 0;
2563   gtk_menu_popdown (GTK_MENU (menu_shell));
2564   
2565   if (parent)
2566     gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
2567 }
2568
2569 static void
2570 gtk_menu_position (GtkMenu *menu)
2571 {
2572   GtkWidget *widget;
2573   GtkRequisition requisition;
2574   GtkMenuPrivate *private;
2575   gint x, y;
2576   gint scroll_offset;
2577   gint menu_height;
2578   gboolean push_in;
2579   GdkScreen *screen;
2580   GdkRectangle monitor;
2581   gint monitor_num;
2582
2583   g_return_if_fail (GTK_IS_MENU (menu));
2584
2585   widget = GTK_WIDGET (menu);
2586
2587   gdk_window_get_pointer (gtk_widget_get_root_window (widget),
2588                           &x, &y, NULL);
2589
2590   screen = gtk_widget_get_screen (widget);
2591   monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
2592   if (monitor_num < 0)
2593     monitor_num = 0;
2594   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2595
2596   /* We need the requisition to figure out the right place to
2597    * popup the menu. In fact, we always need to ask here, since
2598    * if a size_request was queued while we weren't popped up,
2599    * the requisition won't have been recomputed yet.
2600    */
2601   gtk_widget_size_request (widget, &requisition);
2602
2603   push_in = FALSE;
2604   
2605   if (menu->position_func)
2606     (* menu->position_func) (menu, &x, &y, &push_in, menu->position_func_data);
2607   else
2608     {
2609       x = CLAMP (x - 2, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
2610       y = CLAMP (y - 2, monitor.y, MAX (monitor.y, monitor.y + monitor.height - requisition.height));      
2611     }
2612
2613   scroll_offset = 0;
2614
2615   if (push_in)
2616     {
2617       menu_height = GTK_WIDGET (menu)->requisition.height;
2618
2619       if (y + menu_height > monitor.y + monitor.height)
2620         {
2621           scroll_offset -= y + menu_height - (monitor.y + monitor.height);
2622           y = (monitor.y + monitor.height) - menu_height;
2623         }
2624   
2625       if (y < monitor.y)
2626         {
2627           scroll_offset -= y;
2628           y = monitor.y;
2629         }
2630     }
2631
2632   /* FIXME: should this be done in the various position_funcs ? */
2633   x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
2634  
2635   if (y + requisition.height > monitor.y + monitor.height)
2636     requisition.height = (monitor.y + monitor.height) - y;
2637   
2638   if (y < monitor.y)
2639     {
2640       scroll_offset -= y;
2641       requisition.height -= -y;
2642       y = monitor.y;
2643     }
2644
2645   if (scroll_offset > 0)
2646     scroll_offset += MENU_SCROLL_ARROW_HEIGHT;
2647   
2648   gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window), 
2649                    x, y);
2650
2651   if (GTK_MENU_SHELL (menu)->active)
2652     {
2653       private = gtk_menu_get_private (menu);
2654       private->have_position = TRUE;
2655       private->x = x;
2656       private->y = y;
2657
2658       gtk_widget_queue_resize (menu->toplevel);
2659     }
2660   else
2661     {
2662       gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
2663                          requisition.width, requisition.height);
2664     }
2665   
2666   menu->scroll_offset = scroll_offset;
2667 }
2668
2669 static void
2670 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
2671 {
2672   if (menu->timeout_id)
2673     {
2674       g_source_remove (menu->timeout_id);
2675       menu->timeout_id = 0;
2676     }
2677 }
2678
2679 static void
2680 gtk_menu_stop_scrolling (GtkMenu *menu)
2681 {
2682   gtk_menu_remove_scroll_timeout (menu);
2683
2684   menu->upper_arrow_prelight = FALSE;
2685   menu->lower_arrow_prelight = FALSE;
2686 }
2687
2688 static void
2689 gtk_menu_scroll_to (GtkMenu *menu,
2690                     gint    offset)
2691 {
2692   GtkWidget *widget;
2693   gint x, y;
2694   gint view_width, view_height;
2695   gint border_width;
2696   gboolean last_visible;
2697   gint menu_height;
2698
2699   widget = GTK_WIDGET (menu);
2700
2701   if (menu->tearoff_active &&
2702       menu->tearoff_adjustment &&
2703       (menu->tearoff_adjustment->value != offset))
2704     {
2705       menu->tearoff_adjustment->value = offset;
2706       gtk_adjustment_value_changed (menu->tearoff_adjustment);
2707     }
2708   
2709   /* Move/resize the viewport according to arrows: */
2710   view_width = widget->allocation.width;
2711   view_height = widget->allocation.height;
2712
2713   border_width = GTK_CONTAINER (menu)->border_width;
2714   view_width -= (border_width + widget->style->xthickness) * 2;
2715   view_height -= (border_width + widget->style->ythickness) * 2;
2716   menu_height = widget->requisition.height - (border_width + widget->style->ythickness) * 2;
2717
2718   x = border_width + widget->style->xthickness;
2719   y = border_width + widget->style->ythickness;
2720   
2721   if (!menu->tearoff_active)
2722     {
2723       last_visible = menu->upper_arrow_visible;
2724       menu->upper_arrow_visible = (offset > 0);
2725       
2726       if (menu->upper_arrow_visible)
2727         view_height -= MENU_SCROLL_ARROW_HEIGHT;
2728       
2729       if ( (last_visible != menu->upper_arrow_visible) &&
2730            !menu->upper_arrow_visible)
2731         {
2732           menu->upper_arrow_prelight = FALSE;
2733           
2734           /* If we hid the upper arrow, possibly remove timeout */
2735           if (menu->scroll_step < 0)
2736             gtk_menu_stop_scrolling (menu);
2737         }
2738       
2739       last_visible = menu->lower_arrow_visible;
2740       menu->lower_arrow_visible = (view_height + offset < menu_height);
2741       
2742       if (menu->lower_arrow_visible)
2743         view_height -= MENU_SCROLL_ARROW_HEIGHT;
2744       
2745       if ( (last_visible != menu->lower_arrow_visible) &&
2746            !menu->lower_arrow_visible)
2747         {
2748           menu->lower_arrow_prelight = FALSE;
2749           
2750           /* If we hid the lower arrow, possibly remove timeout */
2751           if (menu->scroll_step > 0)
2752             gtk_menu_stop_scrolling (menu);
2753         }
2754       
2755       if (menu->upper_arrow_visible)
2756         y += MENU_SCROLL_ARROW_HEIGHT;
2757     }
2758
2759   offset = CLAMP (offset, 0, menu_height - view_height);
2760
2761   /* Scroll the menu: */
2762   if (GTK_WIDGET_REALIZED (menu))
2763     gdk_window_move (menu->bin_window, 0, -offset);
2764
2765   if (GTK_WIDGET_REALIZED (menu))
2766     gdk_window_move_resize (menu->view_window,
2767                             x,
2768                             y,
2769                             view_width,
2770                             view_height);
2771
2772   menu->scroll_offset = offset;
2773 }
2774
2775 static void
2776 gtk_menu_scroll_item_visible (GtkMenuShell    *menu_shell,
2777                               GtkWidget       *menu_item)
2778 {
2779   GtkMenu *menu;
2780   GtkWidget *child;
2781   GList *children;
2782   GtkRequisition child_requisition;
2783   gint child_offset, child_height;
2784   gint width, height;
2785   gint y;
2786   gint arrow_height;
2787   gboolean last_child = 0;
2788   
2789   menu = GTK_MENU (menu_shell);
2790
2791   /* We need to check if the selected item fully visible.
2792    * If not we need to scroll the menu so that it becomes fully
2793    * visible.
2794    */
2795
2796   child = NULL;
2797   child_offset = 0;
2798   child_height = 0;
2799   children = menu_shell->children;
2800   while (children)
2801     {
2802       child = children->data;
2803       children = children->next;
2804       
2805       if (GTK_WIDGET_VISIBLE (child))
2806         {
2807           gtk_widget_size_request (child, &child_requisition);
2808           child_offset += child_height;
2809           child_height = child_requisition.height;
2810         }
2811       
2812       if (child == menu_item)
2813         {
2814           last_child = (children == NULL);
2815           break;
2816         }
2817     }
2818
2819   if (child == menu_item)
2820     {
2821       y = menu->scroll_offset;
2822       gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
2823
2824       height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness;
2825       
2826       if (child_offset + child_height <= y)
2827         {
2828           /* Ignore the enter event we might get if the pointer is on the menu
2829            */
2830           menu_shell->ignore_enter = TRUE;
2831           gtk_menu_scroll_to (menu, child_offset);
2832         }
2833       else
2834         {
2835           arrow_height = 0;
2836           if (menu->upper_arrow_visible && !menu->tearoff_active)
2837             arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2838           if (menu->lower_arrow_visible && !menu->tearoff_active)
2839             arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2840           
2841           if (child_offset >= y + height - arrow_height)
2842             {
2843               arrow_height = 0;
2844               if (!last_child && !menu->tearoff_active)
2845                 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2846               
2847               y = child_offset + child_height - height + arrow_height;
2848               if ((y > 0) && !menu->tearoff_active)
2849                 {
2850                   /* Need upper arrow */
2851                   arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2852                   y = child_offset + child_height - height + arrow_height;
2853                 }
2854               /* Ignore the enter event we might get if the pointer is on the menu
2855                */
2856               menu_shell->ignore_enter = TRUE;
2857               gtk_menu_scroll_to (menu, y);
2858             }
2859         }    
2860       
2861     }
2862 }
2863
2864 static void
2865 gtk_menu_select_item (GtkMenuShell  *menu_shell,
2866                       GtkWidget     *menu_item)
2867 {
2868   GtkMenu *menu = GTK_MENU (menu_shell);
2869
2870   if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
2871     gtk_menu_scroll_item_visible (menu_shell, menu_item);
2872
2873   GTK_MENU_SHELL_CLASS (parent_class)->select_item (menu_shell, menu_item);
2874 }
2875
2876
2877 /* Reparent the menu, taking care of the refcounting
2878  *
2879  * If unrealize is true we force a unrealize while reparenting the parent.
2880  * This can help eliminate flicker in some cases.
2881  *
2882  * What happens is that when the menu is unrealized and then re-realized,
2883  * the allocations are as follows:
2884  *
2885  *  parent - 1x1 at (0,0) 
2886  *  child1 - 100x20 at (0,0)
2887  *  child2 - 100x20 at (0,20)
2888  *  child3 - 100x20 at (0,40)
2889  *
2890  * That is, the parent is small but the children are full sized. Then,
2891  * when the queued_resize gets processed, the parent gets resized to
2892  * full size. 
2893  *
2894  * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
2895  * contains the following logic:
2896  * 
2897  * - if a move or resize operation on a window would change the clip 
2898  *   region on the children, then before the window is resized
2899  *   the background for children is temporarily set to None, the
2900  *   move/resize done, and the background for the children restored.
2901  *
2902  * So, at the point where the parent is resized to final size, the
2903  * background for the children is temporarily None, and thus they
2904  * are not cleared to the background color and the previous background
2905  * (the image of the menu) is left in place.
2906  */
2907 static void 
2908 gtk_menu_reparent (GtkMenu      *menu, 
2909                    GtkWidget    *new_parent, 
2910                    gboolean      unrealize)
2911 {
2912   GtkObject *object = GTK_OBJECT (menu);
2913   GtkWidget *widget = GTK_WIDGET (menu);
2914   gboolean was_floating = GTK_OBJECT_FLOATING (object);
2915
2916   g_object_ref (object);
2917   gtk_object_sink (object);
2918
2919   if (unrealize)
2920     {
2921       g_object_ref (object);
2922       gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
2923       gtk_container_add (GTK_CONTAINER (new_parent), widget);
2924       g_object_unref (object);
2925     }
2926   else
2927     gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
2928   
2929   if (was_floating)
2930     GTK_OBJECT_SET_FLAGS (object, GTK_FLOATING);
2931   else
2932     g_object_unref (object);
2933 }
2934
2935 static void
2936 gtk_menu_show_all (GtkWidget *widget)
2937 {
2938   g_return_if_fail (GTK_IS_MENU (widget));
2939
2940   /* Show children, but not self. */
2941   gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
2942 }
2943
2944
2945 static void
2946 gtk_menu_hide_all (GtkWidget *widget)
2947 {
2948   g_return_if_fail (GTK_IS_MENU (widget));
2949
2950   /* Hide children, but not self. */
2951   gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
2952 }
2953
2954 /**
2955  * gtk_menu_set_screen:
2956  * @menu: a #GtkMenu.
2957  * @screen: a #GdkScreen, or %NULL if the screen should be
2958  *          determined by the widget the menu is attached to.
2959  *
2960  * Sets the #GdkScreen on which the menu will be displayed.
2961  * 
2962  * Since: 2.2
2963  **/
2964 void
2965 gtk_menu_set_screen (GtkMenu   *menu, 
2966                      GdkScreen *screen)
2967 {
2968   g_return_if_fail (GTK_IS_MENU (menu));
2969   g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
2970
2971   g_object_set_data (G_OBJECT (menu), "gtk-menu-explicit-screen", screen);
2972
2973   if (screen)
2974     {
2975       gtk_window_set_screen (GTK_WINDOW (menu->toplevel), screen);
2976     }
2977   else
2978     {
2979       GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
2980       if (attach_widget)
2981         attach_widget_screen_changed (attach_widget, NULL, menu);
2982     }
2983 }
2984
2985
2986 static gint
2987 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
2988 {
2989   gint popup_delay;
2990
2991   g_object_get (G_OBJECT (gtk_widget_get_settings (GTK_WIDGET (menu_shell))),
2992                 "gtk-menu-popup-delay", &popup_delay,
2993                 NULL);
2994
2995   return popup_delay;
2996 }