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