]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenu.c
Use canonical names for g_object_notify() as well.
[~andy/gtk] / gtk / gtkmenu.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #define GTK_MENU_INTERNALS
28
29 #include <config.h>
30 #include <string.h> /* memset */
31 #include "gdk/gdkkeysyms.h"
32 #include "gtkaccellabel.h"
33 #include "gtkaccelmap.h"
34 #include "gtkbindings.h"
35 #include "gtklabel.h"
36 #include "gtkmain.h"
37 #include "gtkmarshalers.h"
38 #include "gtkmenu.h"
39 #include "gtkmenuitem.h"
40 #include "gtktearoffmenuitem.h"
41 #include "gtkwindow.h"
42 #include "gtkhbox.h"
43 #include "gtkvscrollbar.h"
44 #include "gtksettings.h"
45 #include "gtkprivate.h"
46 #include "gtkintl.h"
47 #include "gtkalias.h"
48
49
50 #define MENU_ITEM_CLASS(w)   GTK_MENU_ITEM_GET_CLASS (w)
51
52 #define DEFAULT_POPUP_DELAY    225
53 #define DEFAULT_POPDOWN_DELAY  1000
54
55 #define NAVIGATION_REGION_OVERSHOOT 50  /* How much the navigation region
56                                          * extends below the submenu
57                                          */
58
59 #define MENU_SCROLL_STEP1 8
60 #define MENU_SCROLL_STEP2 15
61 #define MENU_SCROLL_ARROW_HEIGHT 16
62 #define MENU_SCROLL_FAST_ZONE 8
63 #define MENU_SCROLL_TIMEOUT1 50
64 #define MENU_SCROLL_TIMEOUT2 20
65
66 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
67 #define ATTACHED_MENUS "gtk-attached-menus"
68
69 typedef struct _GtkMenuAttachData       GtkMenuAttachData;
70 typedef struct _GtkMenuPrivate          GtkMenuPrivate;
71
72 struct _GtkMenuAttachData
73 {
74   GtkWidget *attach_widget;
75   GtkMenuDetachFunc detacher;
76 };
77
78 struct _GtkMenuPrivate 
79 {
80   gboolean seen_item_enter;
81
82   gboolean have_position;
83   gint x;
84   gint y;
85
86   /* info used for the table */
87   guint *heights;
88   gint heights_length;
89
90   gint monitor_num;
91
92   /* Cached layout information */
93   gboolean have_layout;
94   gint n_rows;
95   gint n_columns;
96 };
97
98 typedef struct
99 {
100   gint left_attach;
101   gint right_attach;
102   gint top_attach;
103   gint bottom_attach;
104   gint effective_left_attach;
105   gint effective_right_attach;
106   gint effective_top_attach;
107   gint effective_bottom_attach;
108 } AttachInfo;
109
110 enum {
111   MOVE_SCROLL,
112   LAST_SIGNAL
113 };
114
115 enum {
116   PROP_0,
117   PROP_TEAROFF_STATE,
118   PROP_TEAROFF_TITLE
119 };
120
121 enum {
122   CHILD_PROP_0,
123   CHILD_PROP_LEFT_ATTACH,
124   CHILD_PROP_RIGHT_ATTACH,
125   CHILD_PROP_TOP_ATTACH,
126   CHILD_PROP_BOTTOM_ATTACH
127 };
128
129 static void     gtk_menu_class_init        (GtkMenuClass     *klass);
130 static void     gtk_menu_init              (GtkMenu          *menu);
131 static void     gtk_menu_set_property      (GObject          *object,
132                                             guint             prop_id,
133                                             const GValue     *value,
134                                             GParamSpec       *pspec);
135 static void     gtk_menu_get_property      (GObject          *object,
136                                             guint             prop_id,
137                                             GValue           *value,
138                                             GParamSpec       *pspec);
139 static void     gtk_menu_set_child_property(GtkContainer     *container,
140                                             GtkWidget        *child,
141                                             guint             property_id,
142                                             const GValue     *value,
143                                             GParamSpec       *pspec);
144 static void     gtk_menu_get_child_property(GtkContainer     *container,
145                                             GtkWidget        *child,
146                                             guint             property_id,
147                                             GValue           *value,
148                                             GParamSpec       *pspec);
149 static void     gtk_menu_destroy           (GtkObject        *object);
150 static void     gtk_menu_finalize          (GObject          *object);
151 static void     gtk_menu_realize           (GtkWidget        *widget);
152 static void     gtk_menu_unrealize         (GtkWidget        *widget);
153 static void     gtk_menu_size_request      (GtkWidget        *widget,
154                                             GtkRequisition   *requisition);
155 static void     gtk_menu_size_allocate     (GtkWidget        *widget,
156                                             GtkAllocation    *allocation);
157 static void     gtk_menu_paint             (GtkWidget        *widget,
158                                             GdkEventExpose   *expose);
159 static void     gtk_menu_show              (GtkWidget        *widget);
160 static gboolean gtk_menu_expose            (GtkWidget        *widget,
161                                             GdkEventExpose   *event);
162 static gboolean gtk_menu_key_press         (GtkWidget        *widget,
163                                             GdkEventKey      *event);
164 static gboolean gtk_menu_scroll            (GtkWidget        *widget,
165                                             GdkEventScroll   *event);
166 static gboolean gtk_menu_button_press      (GtkWidget        *widget,
167                                             GdkEventButton   *event);
168 static gboolean gtk_menu_button_release    (GtkWidget        *widget,
169                                             GdkEventButton   *event);
170 static gboolean gtk_menu_motion_notify     (GtkWidget        *widget,
171                                             GdkEventMotion   *event);
172 static gboolean gtk_menu_enter_notify      (GtkWidget        *widget,
173                                             GdkEventCrossing *event);
174 static gboolean gtk_menu_leave_notify      (GtkWidget        *widget,
175                                             GdkEventCrossing *event);
176 static void     gtk_menu_scroll_to         (GtkMenu          *menu,
177                                             gint              offset);
178
179 static void     gtk_menu_stop_scrolling        (GtkMenu  *menu);
180 static void     gtk_menu_remove_scroll_timeout (GtkMenu  *menu);
181 static gboolean gtk_menu_scroll_timeout        (gpointer  data);
182
183 static void     gtk_menu_scroll_item_visible (GtkMenuShell    *menu_shell,
184                                               GtkWidget       *menu_item);
185 static void     gtk_menu_select_item       (GtkMenuShell     *menu_shell,
186                                             GtkWidget        *menu_item);
187 static void     gtk_menu_real_insert       (GtkMenuShell     *menu_shell,
188                                             GtkWidget        *child,
189                                             gint              position);
190 static void     gtk_menu_scrollbar_changed (GtkAdjustment    *adjustment,
191                                             GtkMenu          *menu);
192 static void     gtk_menu_handle_scrolling  (GtkMenu          *menu,
193                                             gint              event_x,
194                                             gint              event_y,
195                                             gboolean          enter);
196 static void     gtk_menu_set_tearoff_hints (GtkMenu          *menu,
197                                             gint             width);
198 static void     gtk_menu_style_set         (GtkWidget        *widget,
199                                             GtkStyle         *previous_style);
200 static gboolean gtk_menu_focus             (GtkWidget        *widget,
201                                             GtkDirectionType direction);
202 static gint     gtk_menu_get_popup_delay   (GtkMenuShell     *menu_shell);
203 static void     gtk_menu_move_current      (GtkMenuShell     *menu_shell,
204                                             GtkMenuDirectionType direction);
205 static void     gtk_menu_real_move_scroll  (GtkMenu          *menu,
206                                             GtkScrollType     type);
207
208 static void     gtk_menu_stop_navigating_submenu       (GtkMenu          *menu);
209 static gboolean gtk_menu_stop_navigating_submenu_cb    (gpointer          user_data);
210 static gboolean gtk_menu_navigating_submenu            (GtkMenu          *menu,
211                                                         gint              event_x,
212                                                         gint              event_y);
213 static void     gtk_menu_set_submenu_navigation_region (GtkMenu          *menu,
214                                                         GtkMenuItem      *menu_item,
215                                                         GdkEventCrossing *event);
216  
217 static void gtk_menu_deactivate     (GtkMenuShell      *menu_shell);
218 static void gtk_menu_show_all       (GtkWidget         *widget);
219 static void gtk_menu_hide_all       (GtkWidget         *widget);
220 static void gtk_menu_position       (GtkMenu           *menu);
221 static void gtk_menu_reparent       (GtkMenu           *menu, 
222                                      GtkWidget         *new_parent, 
223                                      gboolean           unrealize);
224 static void gtk_menu_remove         (GtkContainer      *menu,
225                                      GtkWidget         *widget);
226
227 static void gtk_menu_update_title   (GtkMenu           *menu);
228
229 static void       menu_grab_transfer_window_destroy (GtkMenu *menu);
230 static GdkWindow *menu_grab_transfer_window_get     (GtkMenu *menu);
231
232 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
233                                                   guint      signal_id);
234 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
235                                            gboolean group_changed);
236
237 static GtkMenuShellClass *parent_class = NULL;
238 static const gchar        attach_data_key[] = "gtk-menu-attach-data";
239
240 static guint menu_signals[LAST_SIGNAL] = { 0 };
241
242 static void
243 gtk_menu_free_private (gpointer data)
244 {
245   GtkMenuPrivate *priv = (GtkMenuPrivate *)data;
246
247   g_free (priv->heights);
248
249   g_free (priv);
250 }
251
252 static GtkMenuPrivate *
253 gtk_menu_get_private (GtkMenu *menu)
254 {
255   GtkMenuPrivate *private;
256   static GQuark private_quark = 0;
257
258   if (!private_quark)
259     private_quark = g_quark_from_static_string ("gtk-menu-private");
260
261   private = g_object_get_qdata (G_OBJECT (menu), private_quark);
262
263   if (!private)
264     {
265       private = g_new0 (GtkMenuPrivate, 1);
266       private->have_position = FALSE;
267       
268       g_object_set_qdata_full (G_OBJECT (menu), private_quark,
269                                private, gtk_menu_free_private);
270     }
271
272   return private;
273 }
274
275 GType
276 gtk_menu_get_type (void)
277 {
278   static GType menu_type = 0;
279   
280   if (!menu_type)
281     {
282       static const GTypeInfo menu_info =
283       {
284         sizeof (GtkMenuClass),
285         NULL,           /* base_init */
286         NULL,           /* base_finalize */
287         (GClassInitFunc) gtk_menu_class_init,
288         NULL,           /* class_finalize */
289         NULL,           /* class_data */
290         sizeof (GtkMenu),
291         0,              /* n_preallocs */
292         (GInstanceInitFunc) gtk_menu_init,
293       };
294       
295       menu_type = g_type_register_static (GTK_TYPE_MENU_SHELL, "GtkMenu",
296                                           &menu_info, 0);
297     }
298   
299   return menu_type;
300 }
301
302 static void
303 menu_queue_resize (GtkMenu *menu)
304 {
305   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
306
307   priv->have_layout = FALSE;
308   gtk_widget_queue_resize (GTK_WIDGET (menu));
309 }
310
311 static AttachInfo *
312 get_attach_info (GtkWidget *child)
313 {
314   GObject *object = G_OBJECT (child);
315   AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
316
317   if (!ai)
318     {
319       ai = g_new0 (AttachInfo, 1);
320       g_object_set_data_full (object, ATTACH_INFO_KEY, ai, g_free);
321     }
322
323   return ai;
324 }
325
326 static gboolean
327 is_grid_attached (AttachInfo *ai)
328 {
329   return (ai->left_attach >= 0 &&
330           ai->right_attach >= 0 &&
331           ai->top_attach >= 0 &&
332           ai->bottom_attach >= 0);
333 }
334
335 static void
336 menu_ensure_layout (GtkMenu *menu)
337 {
338   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
339
340   if (!priv->have_layout)
341     {
342       GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
343       GList *l;
344       gchar *row_occupied;
345       gint current_row;
346       gint max_right_attach;      
347       gint max_bottom_attach;
348
349       /* Find extents of gridded portion
350        */
351       max_right_attach = 1;
352       max_bottom_attach = 0;
353
354       for (l = menu_shell->children; l; l = l->next)
355         {
356           GtkWidget *child = l->data;
357           AttachInfo *ai = get_attach_info (child);
358
359           if (is_grid_attached (ai))
360             {
361               max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
362               max_right_attach = MAX (max_right_attach, ai->right_attach);
363             }
364         }
365          
366       /* Find empty rows
367        */
368       row_occupied = g_malloc0 (max_bottom_attach);
369
370       for (l = menu_shell->children; l; l = l->next)
371         {
372           GtkWidget *child = l->data;
373           AttachInfo *ai = get_attach_info (child);
374
375           if (is_grid_attached (ai))
376             {
377               gint i;
378
379               for (i = ai->top_attach; i < ai->bottom_attach; i++)
380                 row_occupied[i] = TRUE;
381             }
382         }
383
384       /* Lay non-grid-items out in those rows
385        */
386       current_row = 0;
387       for (l = menu_shell->children; l; l = l->next)
388         {
389           GtkWidget *child = l->data;
390           AttachInfo *ai = get_attach_info (child);
391
392           if (!is_grid_attached (ai))
393             {
394               while (current_row < max_bottom_attach && row_occupied[current_row])
395                 current_row++;
396                 
397               ai->effective_left_attach = 0;
398               ai->effective_right_attach = max_right_attach;
399               ai->effective_top_attach = current_row;
400               ai->effective_bottom_attach = current_row + 1;
401
402               current_row++;
403             }
404           else
405             {
406               ai->effective_left_attach = ai->left_attach;
407               ai->effective_right_attach = ai->right_attach;
408               ai->effective_top_attach = ai->top_attach;
409               ai->effective_bottom_attach = ai->bottom_attach;
410             }
411         }
412
413       g_free (row_occupied);
414
415       priv->n_rows = MAX (current_row, max_bottom_attach);
416       priv->n_columns = max_right_attach;
417       priv->have_layout = TRUE;
418     }
419 }
420
421
422 static gint
423 gtk_menu_get_n_columns (GtkMenu *menu)
424 {
425   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
426
427   menu_ensure_layout (menu);
428
429   return priv->n_columns;
430 }
431
432 static gint
433 gtk_menu_get_n_rows (GtkMenu *menu)
434 {
435   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
436
437   menu_ensure_layout (menu);
438
439   return priv->n_rows;
440 }
441
442 static void
443 get_effective_child_attach (GtkWidget *child,
444                             int       *l,
445                             int       *r,
446                             int       *t,
447                             int       *b)
448 {
449   GtkMenu *menu = GTK_MENU (child->parent);
450   AttachInfo *ai;
451   
452   menu_ensure_layout (menu);
453
454   ai = get_attach_info (child);
455
456   if (l)
457     *l = ai->effective_left_attach;
458   if (r)
459     *r = ai->effective_right_attach;
460   if (t)
461     *t = ai->effective_top_attach;
462   if (b)
463     *b = ai->effective_bottom_attach;
464
465 }
466
467 static void
468 gtk_menu_class_init (GtkMenuClass *class)
469 {
470   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
471   GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
472   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
473   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
474   GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
475   GtkBindingSet *binding_set;
476   
477   parent_class = g_type_class_peek_parent (class);
478   
479   gobject_class->finalize = gtk_menu_finalize;
480   gobject_class->set_property = gtk_menu_set_property;
481   gobject_class->get_property = gtk_menu_get_property;
482
483   object_class->destroy = gtk_menu_destroy;
484   
485   widget_class->realize = gtk_menu_realize;
486   widget_class->unrealize = gtk_menu_unrealize;
487   widget_class->size_request = gtk_menu_size_request;
488   widget_class->size_allocate = gtk_menu_size_allocate;
489   widget_class->show = gtk_menu_show;
490   widget_class->expose_event = gtk_menu_expose;
491   widget_class->scroll_event = gtk_menu_scroll;
492   widget_class->key_press_event = gtk_menu_key_press;
493   widget_class->button_press_event = gtk_menu_button_press;
494   widget_class->button_release_event = gtk_menu_button_release;
495   widget_class->motion_notify_event = gtk_menu_motion_notify;
496   widget_class->show_all = gtk_menu_show_all;
497   widget_class->hide_all = gtk_menu_hide_all;
498   widget_class->enter_notify_event = gtk_menu_enter_notify;
499   widget_class->leave_notify_event = gtk_menu_leave_notify;
500   widget_class->motion_notify_event = gtk_menu_motion_notify;
501   widget_class->style_set = gtk_menu_style_set;
502   widget_class->focus = gtk_menu_focus;
503   widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
504
505   container_class->remove = gtk_menu_remove;
506   container_class->get_child_property = gtk_menu_get_child_property;
507   container_class->set_child_property = gtk_menu_set_child_property;
508   
509   menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
510   menu_shell_class->deactivate = gtk_menu_deactivate;
511   menu_shell_class->select_item = gtk_menu_select_item;
512   menu_shell_class->insert = gtk_menu_real_insert;
513   menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
514   menu_shell_class->move_current = gtk_menu_move_current;
515
516   menu_signals[MOVE_SCROLL] =
517     _gtk_binding_signal_new ("move_scroll",
518                              G_OBJECT_CLASS_TYPE (object_class),
519                              G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
520                              G_CALLBACK (gtk_menu_real_move_scroll),
521                              NULL, NULL,
522                              _gtk_marshal_VOID__ENUM,
523                              G_TYPE_NONE, 1,
524                              GTK_TYPE_SCROLL_TYPE);
525   
526   g_object_class_install_property (gobject_class,
527                                    PROP_TEAROFF_TITLE,
528                                    g_param_spec_string ("tearoff-title",
529                                                         P_("Tearoff Title"),
530                                                         P_("A title that may be displayed by the window manager when this menu is torn-off"),
531                                                         "",
532                                                         GTK_PARAM_READWRITE));
533
534   /**
535    * GtkMenu:tearoff-state:
536    *
537    * A boolean that indicates whether the menu is torn-off.
538    *
539    * Since: 2.6
540    **/
541   g_object_class_install_property (gobject_class,
542                                    PROP_TEAROFF_STATE,
543                                    g_param_spec_boolean ("tearoff-state",
544                                                          P_("Tearoff State"),
545                                                          P_("A boolean that indicates whether the menu is torn-off"),
546                                                          FALSE,
547                                                          GTK_PARAM_READWRITE));
548
549   gtk_widget_class_install_style_property (widget_class,
550                                            g_param_spec_int ("vertical-padding",
551                                                              P_("Vertical Padding"),
552                                                              P_("Extra space at the top and bottom of the menu"),
553                                                              0,
554                                                              G_MAXINT,
555                                                              1,
556                                                              GTK_PARAM_READABLE));
557
558   gtk_widget_class_install_style_property (widget_class,
559                                            g_param_spec_int ("vertical-offset",
560                                                              P_("Vertical Offset"),
561                                                              P_("When the menu is a submenu, position it this number of pixels offset vertically"),
562                                                              G_MININT,
563                                                              G_MAXINT,
564                                                              0,
565                                                              GTK_PARAM_READABLE));
566
567   gtk_widget_class_install_style_property (widget_class,
568                                            g_param_spec_int ("horizontal-offset",
569                                                              P_("Horizontal Offset"),
570                                                              P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
571                                                              G_MININT,
572                                                              G_MAXINT,
573                                                              -2,
574                                                              GTK_PARAM_READABLE));
575
576
577  gtk_container_class_install_child_property (container_class,
578                                              CHILD_PROP_LEFT_ATTACH,
579                                               g_param_spec_int ("left-attach",
580                                                                P_("Left Attach"),
581                                                                P_("The column number to attach the left side of the child to"),
582                                                                 -1, INT_MAX, -1,
583                                                                GTK_PARAM_READWRITE));
584
585  gtk_container_class_install_child_property (container_class,
586                                              CHILD_PROP_RIGHT_ATTACH,
587                                               g_param_spec_int ("right-attach",
588                                                                P_("Right Attach"),
589                                                                P_("The column number to attach the right side of the child to"),
590                                                                 -1, INT_MAX, -1,
591                                                                GTK_PARAM_READWRITE));
592
593  gtk_container_class_install_child_property (container_class,
594                                              CHILD_PROP_TOP_ATTACH,
595                                               g_param_spec_int ("top-attach",
596                                                                P_("Top Attach"),
597                                                                P_("The row number to attach the top of the child to"),
598                                                                 -1, INT_MAX, -1,
599                                                                GTK_PARAM_READWRITE));
600
601  gtk_container_class_install_child_property (container_class,
602                                              CHILD_PROP_BOTTOM_ATTACH,
603                                               g_param_spec_int ("bottom-attach",
604                                                                P_("Bottom Attach"),
605                                                                P_("The row number to attach the bottom of the child to"),
606                                                                 -1, INT_MAX, -1,
607                                                                GTK_PARAM_READWRITE));
608
609   binding_set = gtk_binding_set_by_class (class);
610   gtk_binding_entry_add_signal (binding_set,
611                                 GDK_Up, 0,
612                                 "move_current", 1,
613                                 GTK_TYPE_MENU_DIRECTION_TYPE,
614                                 GTK_MENU_DIR_PREV);
615   gtk_binding_entry_add_signal (binding_set,
616                                 GDK_KP_Up, 0,
617                                 "move_current", 1,
618                                 GTK_TYPE_MENU_DIRECTION_TYPE,
619                                 GTK_MENU_DIR_PREV);
620   gtk_binding_entry_add_signal (binding_set,
621                                 GDK_Down, 0,
622                                 "move_current", 1,
623                                 GTK_TYPE_MENU_DIRECTION_TYPE,
624                                 GTK_MENU_DIR_NEXT);
625   gtk_binding_entry_add_signal (binding_set,
626                                 GDK_KP_Down, 0,
627                                 "move_current", 1,
628                                 GTK_TYPE_MENU_DIRECTION_TYPE,
629                                 GTK_MENU_DIR_NEXT);
630   gtk_binding_entry_add_signal (binding_set,
631                                 GDK_Left, 0,
632                                 "move_current", 1,
633                                 GTK_TYPE_MENU_DIRECTION_TYPE,
634                                 GTK_MENU_DIR_PARENT);
635   gtk_binding_entry_add_signal (binding_set,
636                                 GDK_KP_Left, 0,
637                                 "move_current", 1,
638                                 GTK_TYPE_MENU_DIRECTION_TYPE,
639                                 GTK_MENU_DIR_PARENT);
640   gtk_binding_entry_add_signal (binding_set,
641                                 GDK_Right, 0,
642                                 "move_current", 1,
643                                 GTK_TYPE_MENU_DIRECTION_TYPE,
644                                 GTK_MENU_DIR_CHILD);
645   gtk_binding_entry_add_signal (binding_set,
646                                 GDK_KP_Right, 0,
647                                 "move_current", 1,
648                                 GTK_TYPE_MENU_DIRECTION_TYPE,
649                                 GTK_MENU_DIR_CHILD);
650   gtk_binding_entry_add_signal (binding_set,
651                                 GDK_Home, 0,
652                                 "move_scroll", 1,
653                                 GTK_TYPE_SCROLL_TYPE,
654                                 GTK_SCROLL_START);
655   gtk_binding_entry_add_signal (binding_set,
656                                 GDK_KP_Home, 0,
657                                 "move_scroll", 1,
658                                 GTK_TYPE_SCROLL_TYPE,
659                                 GTK_SCROLL_START);
660   gtk_binding_entry_add_signal (binding_set,
661                                 GDK_End, 0,
662                                 "move_scroll", 1,
663                                 GTK_TYPE_SCROLL_TYPE,
664                                 GTK_SCROLL_END);
665   gtk_binding_entry_add_signal (binding_set,
666                                 GDK_KP_End, 0,
667                                 "move_scroll", 1,
668                                 GTK_TYPE_SCROLL_TYPE,
669                                 GTK_SCROLL_END);
670   gtk_binding_entry_add_signal (binding_set,
671                                 GDK_Page_Up, 0,
672                                 "move_scroll", 1,
673                                 GTK_TYPE_SCROLL_TYPE,
674                                 GTK_SCROLL_PAGE_UP);
675   gtk_binding_entry_add_signal (binding_set,
676                                 GDK_KP_Page_Up, 0,
677                                 "move_scroll", 1,
678                                 GTK_TYPE_SCROLL_TYPE,
679                                 GTK_SCROLL_PAGE_UP);
680   gtk_binding_entry_add_signal (binding_set,
681                                 GDK_Page_Down, 0,
682                                 "move_scroll", 1,
683                                 GTK_TYPE_SCROLL_TYPE,
684                                 GTK_SCROLL_PAGE_DOWN);
685   gtk_binding_entry_add_signal (binding_set,
686                                 GDK_KP_Page_Down, 0,
687                                 "move_scroll", 1,
688                                 GTK_TYPE_SCROLL_TYPE,
689                                 GTK_SCROLL_PAGE_DOWN);
690
691   gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
692                                                        P_("Can change accelerators"),
693                                                        P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
694                                                        FALSE,
695                                                        GTK_PARAM_READWRITE));
696
697   gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
698                                                    P_("Delay before submenus appear"),
699                                                    P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
700                                                    0,
701                                                    G_MAXINT,
702                                                    DEFAULT_POPUP_DELAY,
703                                                    GTK_PARAM_READWRITE));
704
705   gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
706                                                    P_("Delay before hiding a submenu"),
707                                                    P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
708                                                    0,
709                                                    G_MAXINT,
710                                                    DEFAULT_POPDOWN_DELAY,
711                                                    GTK_PARAM_READWRITE));
712                                                    
713 }
714
715
716 static void 
717 gtk_menu_set_property (GObject      *object,
718                        guint         prop_id,
719                        const GValue *value,
720                        GParamSpec   *pspec)
721 {
722   GtkMenu *menu;
723   
724   menu = GTK_MENU (object);
725   
726   switch (prop_id)
727     {
728     case PROP_TEAROFF_STATE:
729       gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
730       break;
731     case PROP_TEAROFF_TITLE:
732       gtk_menu_set_title (menu, g_value_get_string (value));
733       break;      
734     default:
735       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
736       break;
737     }
738 }
739
740 static void 
741 gtk_menu_get_property (GObject     *object,
742                        guint        prop_id,
743                        GValue      *value,
744                        GParamSpec  *pspec)
745 {
746   GtkMenu *menu;
747   
748   menu = GTK_MENU (object);
749   
750   switch (prop_id)
751     {
752     case PROP_TEAROFF_STATE:
753       g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
754       break;
755     case PROP_TEAROFF_TITLE:
756       g_value_set_string (value, gtk_menu_get_title (menu));
757       break;
758     default:
759       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
760       break;
761     }
762 }
763
764 static void
765 gtk_menu_set_child_property (GtkContainer *container,
766                              GtkWidget    *child,
767                              guint         property_id,
768                              const GValue *value,
769                              GParamSpec   *pspec)
770 {
771   GtkMenu *menu = GTK_MENU (container);
772   AttachInfo *ai = get_attach_info (child);
773
774   switch (property_id)
775     {
776     case CHILD_PROP_LEFT_ATTACH:
777       ai->left_attach = g_value_get_int (value);
778       break;
779     case CHILD_PROP_RIGHT_ATTACH:
780       ai->right_attach = g_value_get_int (value);
781       break;
782     case CHILD_PROP_TOP_ATTACH:
783       ai->top_attach = g_value_get_int (value); 
784       break;
785     case CHILD_PROP_BOTTOM_ATTACH:
786       ai->bottom_attach = g_value_get_int (value);
787       break;
788
789     default:
790       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
791       return;
792     }
793
794   menu_queue_resize (menu);
795 }
796
797 static void
798 gtk_menu_get_child_property (GtkContainer *container,
799                              GtkWidget    *child,
800                              guint         property_id,
801                              GValue       *value,
802                              GParamSpec   *pspec)
803 {
804   AttachInfo *ai = get_attach_info (child);
805
806   switch (property_id)
807     {
808     case CHILD_PROP_LEFT_ATTACH:
809       g_value_set_int (value, ai->left_attach);
810       break;
811     case CHILD_PROP_RIGHT_ATTACH:
812       g_value_set_int (value, ai->right_attach);
813       break;
814     case CHILD_PROP_TOP_ATTACH:
815       g_value_set_int (value, ai->top_attach);
816       break;
817     case CHILD_PROP_BOTTOM_ATTACH:
818       g_value_set_int (value, ai->bottom_attach);
819       break;
820       
821     default:
822       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
823       return;
824     }
825 }
826
827 static gboolean
828 gtk_menu_window_event (GtkWidget *window,
829                        GdkEvent  *event,
830                        GtkWidget *menu)
831 {
832   gboolean handled = FALSE;
833
834   g_object_ref (window);
835   g_object_ref (menu);
836
837   switch (event->type)
838     {
839     case GDK_KEY_PRESS:
840     case GDK_KEY_RELEASE:
841       handled = gtk_widget_event (menu, event);
842       break;
843     default:
844       break;
845     }
846
847   g_object_unref (window);
848   g_object_unref (menu);
849
850   return handled;
851 }
852
853 static void
854 gtk_menu_window_size_request (GtkWidget      *window,
855                               GtkRequisition *requisition,
856                               GtkMenu        *menu)
857 {
858   GtkMenuPrivate *private = gtk_menu_get_private (menu);
859
860   if (private->have_position)
861     {
862       GdkScreen *screen = gtk_widget_get_screen (window);
863       GdkRectangle monitor;
864       
865       gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
866
867       if (private->y + requisition->height > monitor.y + monitor.height)
868         requisition->height = monitor.y + monitor.height - private->y;
869
870       if (private->y < monitor.y)
871         requisition->height -= monitor.y - private->y;
872     }
873 }
874
875 static void
876 gtk_menu_init (GtkMenu *menu)
877 {
878   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
879
880   menu->parent_menu_item = NULL;
881   menu->old_active_menu_item = NULL;
882   menu->accel_group = NULL;
883   menu->position_func = NULL;
884   menu->position_func_data = NULL;
885   menu->toggle_size = 0;
886
887   menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
888                                                    "type", GTK_WINDOW_POPUP,
889                                                    "child", menu,
890                                                    NULL),
891                                      "signal::event", gtk_menu_window_event, menu,
892                                      "signal::size_request", gtk_menu_window_size_request, menu,
893                                      "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
894                                      NULL);
895   gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
896   gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
897
898   /* Refloat the menu, so that reference counting for the menu isn't
899    * affected by it being a child of the toplevel
900    */
901   GTK_WIDGET_SET_FLAGS (menu, GTK_FLOATING);
902   menu->needs_destruction_ref_count = TRUE;
903
904   menu->view_window = NULL;
905   menu->bin_window = NULL;
906
907   menu->scroll_offset = 0;
908   menu->scroll_step  = 0;
909   menu->timeout_id = 0;
910   menu->scroll_fast = FALSE;
911   
912   menu->tearoff_window = NULL;
913   menu->tearoff_hbox = NULL;
914   menu->torn_off = FALSE;
915   menu->tearoff_active = FALSE;
916   menu->tearoff_adjustment = NULL;
917   menu->tearoff_scrollbar = NULL;
918
919   menu->upper_arrow_visible = FALSE;
920   menu->lower_arrow_visible = FALSE;
921   menu->upper_arrow_prelight = FALSE;
922   menu->lower_arrow_prelight = FALSE;
923
924   priv->have_layout = FALSE;
925 }
926
927 static void
928 gtk_menu_destroy (GtkObject *object)
929 {
930   GtkMenu *menu;
931   GtkMenuAttachData *data;
932   GtkMenuPrivate *priv;
933
934   g_return_if_fail (GTK_IS_MENU (object));
935
936   menu = GTK_MENU (object);
937
938   gtk_menu_stop_scrolling (menu);
939   
940   data = g_object_get_data (G_OBJECT (object), attach_data_key);
941   if (data)
942     gtk_menu_detach (menu);
943   
944   gtk_menu_stop_navigating_submenu (menu);
945
946   if (menu->old_active_menu_item)
947     {
948       g_object_unref (menu->old_active_menu_item);
949       menu->old_active_menu_item = NULL;
950     }
951
952   /* Add back the reference count for being a child */
953   if (menu->needs_destruction_ref_count)
954     {
955       menu->needs_destruction_ref_count = FALSE;
956       g_object_ref (object);
957     }
958   
959   if (menu->accel_group)
960     {
961       g_object_unref (menu->accel_group);
962       menu->accel_group = NULL;
963     }
964
965   if (menu->toplevel)
966     gtk_widget_destroy (menu->toplevel);
967   if (menu->tearoff_window)
968     gtk_widget_destroy (menu->tearoff_window);
969
970   priv = gtk_menu_get_private (menu);
971
972   GTK_OBJECT_CLASS (parent_class)->destroy (object);
973 }
974
975 static void
976 gtk_menu_finalize (GObject *object)
977 {
978   GtkMenu *menu = GTK_MENU (object);
979
980   g_free (menu->accel_path);
981   
982   G_OBJECT_CLASS (parent_class)->finalize (object);
983 }
984
985 static void
986 menu_change_screen (GtkMenu   *menu,
987                     GdkScreen *new_screen)
988 {
989   GtkMenuPrivate *private = gtk_menu_get_private (menu);
990
991   if (gtk_widget_has_screen (GTK_WIDGET (menu)))
992     {
993       if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
994         return;
995     }
996
997   if (menu->torn_off)
998     {
999       gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1000       gtk_menu_position (menu);
1001     }
1002
1003   gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1004   private->monitor_num = -1;
1005 }
1006
1007 static void
1008 attach_widget_screen_changed (GtkWidget *attach_widget,
1009                               GdkScreen *previous_screen,
1010                               GtkMenu   *menu)
1011 {
1012   if (gtk_widget_has_screen (attach_widget) &&
1013       !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1014     {
1015       menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1016     }
1017 }
1018
1019 void
1020 gtk_menu_attach_to_widget (GtkMenu             *menu,
1021                            GtkWidget           *attach_widget,
1022                            GtkMenuDetachFunc    detacher)
1023 {
1024   GtkMenuAttachData *data;
1025   GList *list;
1026   
1027   g_return_if_fail (GTK_IS_MENU (menu));
1028   g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1029   g_return_if_fail (detacher != NULL);
1030   
1031   /* keep this function in sync with gtk_widget_set_parent()
1032    */
1033   
1034   data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1035   if (data)
1036     {
1037       g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1038                  g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1039      return;
1040     }
1041   
1042   g_object_ref (menu);
1043   gtk_object_sink (GTK_OBJECT (menu));
1044   
1045   data = g_new (GtkMenuAttachData, 1);
1046   data->attach_widget = attach_widget;
1047   
1048   g_signal_connect (attach_widget, "screen_changed",
1049                     G_CALLBACK (attach_widget_screen_changed), menu);
1050   attach_widget_screen_changed (attach_widget, NULL, menu);
1051   
1052   data->detacher = detacher;
1053   g_object_set_data (G_OBJECT (menu), attach_data_key, data);
1054   list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1055   if (!g_list_find (list, menu))
1056     {
1057       list = g_list_prepend (list, menu);
1058     }
1059   g_object_set_data_full (G_OBJECT (attach_widget), ATTACHED_MENUS, list, (GtkDestroyNotify) g_list_free);
1060   
1061   if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
1062     gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1063   
1064   /* we don't need to set the style here, since
1065    * we are a toplevel widget.
1066    */
1067
1068   /* Fallback title for menu comes from attach widget */
1069   gtk_menu_update_title (menu);
1070 }
1071
1072 GtkWidget*
1073 gtk_menu_get_attach_widget (GtkMenu *menu)
1074 {
1075   GtkMenuAttachData *data;
1076   
1077   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1078   
1079   data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1080   if (data)
1081     return data->attach_widget;
1082   return NULL;
1083 }
1084
1085 void
1086 gtk_menu_detach (GtkMenu *menu)
1087 {
1088   GtkMenuAttachData *data;
1089   GList *list;
1090   
1091   g_return_if_fail (GTK_IS_MENU (menu));
1092   
1093   /* keep this function in sync with gtk_widget_unparent()
1094    */
1095   data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1096   if (!data)
1097     {
1098       g_warning ("gtk_menu_detach(): menu is not attached");
1099       return;
1100     }
1101   g_object_set_data (G_OBJECT (menu), attach_data_key, NULL);
1102   
1103   g_signal_handlers_disconnect_by_func (data->attach_widget,
1104                                         (gpointer) attach_widget_screen_changed,
1105                                         menu);
1106
1107   data->detacher (data->attach_widget, menu);
1108   list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1109   list = g_list_remove (list, menu);
1110   if (list)
1111     g_object_set_data_full (G_OBJECT (data->attach_widget), ATTACHED_MENUS, list, (GtkDestroyNotify) g_list_free);
1112   else
1113     g_object_set_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS, NULL);
1114   
1115   if (GTK_WIDGET_REALIZED (menu))
1116     gtk_widget_unrealize (GTK_WIDGET (menu));
1117   
1118   g_free (data);
1119   
1120   /* Fallback title for menu comes from attach widget */
1121   gtk_menu_update_title (menu);
1122
1123   g_object_unref (menu);
1124 }
1125
1126 static void
1127 gtk_menu_remove (GtkContainer *container,
1128                  GtkWidget    *widget)
1129 {
1130   GtkMenu *menu;
1131   GtkMenuPrivate *priv;
1132
1133   g_return_if_fail (GTK_IS_MENU (container));
1134   g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1135
1136   menu = GTK_MENU (container);
1137   priv = gtk_menu_get_private (menu);
1138
1139   /* Clear out old_active_menu_item if it matches the item we are removing
1140    */
1141   if (menu->old_active_menu_item == widget)
1142     {
1143       g_object_unref (menu->old_active_menu_item);
1144       menu->old_active_menu_item = NULL;
1145     }
1146
1147   GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
1148   g_object_set_data (G_OBJECT (widget), ATTACH_INFO_KEY, NULL);
1149
1150   menu_queue_resize (menu);
1151 }
1152
1153 GtkWidget*
1154 gtk_menu_new (void)
1155 {
1156   return g_object_new (GTK_TYPE_MENU, NULL);
1157 }
1158
1159 static void
1160 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1161                       GtkWidget    *child,
1162                       gint          position)
1163 {
1164   GtkMenu *menu = GTK_MENU (menu_shell);
1165   AttachInfo *ai = get_attach_info (child);
1166
1167   ai->left_attach = -1;
1168   ai->right_attach = -1;
1169   ai->top_attach = -1;
1170   ai->bottom_attach = -1;
1171
1172   if (GTK_WIDGET_REALIZED (menu_shell))
1173     gtk_widget_set_parent_window (child, menu->bin_window);
1174
1175   GTK_MENU_SHELL_CLASS (parent_class)->insert (menu_shell, child, position);
1176
1177   menu_queue_resize (menu);
1178 }
1179
1180 static void
1181 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1182 {
1183   GtkWidget *widget;
1184   gint width, height;
1185
1186   widget = GTK_WIDGET (menu);
1187
1188   if (menu->torn_off)
1189     {
1190       GdkPixmap *pixmap;
1191       GdkGC *gc;
1192       GdkGCValues gc_values;
1193
1194       menu->tearoff_active = FALSE;
1195       menu->saved_scroll_offset = menu->scroll_offset;
1196       
1197       gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
1198       gc = gdk_gc_new_with_values (widget->window,
1199                                    &gc_values, GDK_GC_SUBWINDOW);
1200       
1201       gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
1202       
1203       pixmap = gdk_pixmap_new (menu->tearoff_window->window,
1204                                width,
1205                                height,
1206                                -1);
1207
1208       gdk_draw_drawable (pixmap, gc,
1209                          menu->tearoff_window->window,
1210                          0, 0, 0, 0, -1, -1);
1211       g_object_unref (gc);
1212
1213       gtk_widget_set_size_request (menu->tearoff_window,
1214                                    width,
1215                                    height);
1216
1217       gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
1218       g_object_unref (pixmap);
1219     }
1220 }
1221
1222 static gboolean
1223 popup_grab_on_window (GdkWindow *window,
1224                       guint32    activate_time)
1225 {
1226   if ((gdk_pointer_grab (window, TRUE,
1227                          GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1228                          GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1229                          GDK_POINTER_MOTION_MASK,
1230                          NULL, NULL, activate_time) == 0))
1231     {
1232       if (gdk_keyboard_grab (window, TRUE,
1233                              activate_time) == 0)
1234         return TRUE;
1235       else
1236         {
1237           gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
1238                                       activate_time);
1239           return FALSE;
1240         }
1241     }
1242
1243   return FALSE;
1244 }
1245
1246 /**
1247  * gtk_menu_popup:
1248  * @menu: a #GtkMenu.
1249  * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
1250  * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
1251  * @func: a user supplied function used to position the menu, or %NULL
1252  * @data: user supplied data to be passed to @func.
1253  * @button: the mouse button which was pressed to initiate the event.
1254  * @activate_time: the time at which the activation event occurred.
1255  *
1256  * Displays a menu and makes it available for selection.  Applications can use
1257  * this function to display context-sensitive menus, and will typically supply
1258  * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data 
1259  * parameters. The default menu positioning function will position the menu
1260  * at the current mouse cursor position.
1261  *
1262  * The @button parameter should be the mouse button pressed to initiate
1263  * the menu popup. If the menu popup was initiated by something other than
1264  * a mouse button press, such as a mouse button release or a keypress,
1265  * @button should be 0.
1266  *
1267  * The @activate_time parameter should be the time stamp of the event that
1268  * initiated the popup. If such an event is not available, use
1269  * gtk_get_current_event_time() instead.
1270  *
1271  */
1272 void
1273 gtk_menu_popup (GtkMenu             *menu,
1274                 GtkWidget           *parent_menu_shell,
1275                 GtkWidget           *parent_menu_item,
1276                 GtkMenuPositionFunc  func,
1277                 gpointer             data,
1278                 guint                button,
1279                 guint32              activate_time)
1280 {
1281   GtkWidget *widget;
1282   GtkWidget *xgrab_shell;
1283   GtkWidget *parent;
1284   GdkEvent *current_event;
1285   GtkMenuShell *menu_shell;
1286   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
1287
1288   g_return_if_fail (GTK_IS_MENU (menu));
1289   
1290   widget = GTK_WIDGET (menu);
1291   menu_shell = GTK_MENU_SHELL (menu);
1292   
1293   menu_shell->parent_menu_shell = parent_menu_shell;
1294
1295   priv->seen_item_enter = FALSE;
1296   
1297   /* Find the last viewable ancestor, and make an X grab on it
1298    */
1299   parent = GTK_WIDGET (menu);
1300   xgrab_shell = NULL;
1301   while (parent)
1302     {
1303       gboolean viewable = TRUE;
1304       GtkWidget *tmp = parent;
1305       
1306       while (tmp)
1307         {
1308           if (!GTK_WIDGET_MAPPED (tmp))
1309             {
1310               viewable = FALSE;
1311               break;
1312             }
1313           tmp = tmp->parent;
1314         }
1315       
1316       if (viewable)
1317         xgrab_shell = parent;
1318       
1319       parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1320     }
1321
1322   /* We want to receive events generated when we map the menu; unfortunately,
1323    * since there is probably already an implicit grab in place from the
1324    * button that the user used to pop up the menu, we won't receive then --
1325    * in particular, the EnterNotify when the menu pops up under the pointer.
1326    *
1327    * If we are grabbing on a parent menu shell, no problem; just grab on
1328    * that menu shell first before popping up the window with owner_events = TRUE.
1329    *
1330    * When grabbing on the menu itself, things get more convuluted - we
1331    * we do an explicit grab on a specially created window with
1332    * owner_events = TRUE, which we override further down with a grab
1333    * on the menu. (We can't grab on the menu until it is mapped; we
1334    * probably could just leave the grab on the other window, with a
1335    * little reorganization of the code in gtkmenu*).
1336    */
1337   if (xgrab_shell && xgrab_shell != widget)
1338     {
1339       if (popup_grab_on_window (xgrab_shell->window, activate_time))
1340         GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1341     }
1342   else
1343     {
1344       GdkWindow *transfer_window;
1345
1346       xgrab_shell = widget;
1347       transfer_window = menu_grab_transfer_window_get (menu);
1348       if (popup_grab_on_window (transfer_window, activate_time))
1349         GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1350     }
1351
1352   if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1353     {
1354       /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1355        * with a stuck up window, we just abort here. Presumably the user will
1356        * try again.
1357        */
1358       menu_shell->parent_menu_shell = NULL;
1359       menu_grab_transfer_window_destroy (menu);
1360       return;
1361     }
1362
1363   menu_shell->active = TRUE;
1364   menu_shell->button = button;
1365
1366   /* If we are popping up the menu from something other than, a button
1367    * press then, as a heuristic, we ignore enter events for the menu
1368    * until we get a MOTION_NOTIFY.  
1369    */
1370
1371   current_event = gtk_get_current_event ();
1372   if (current_event)
1373     {
1374       if ((current_event->type != GDK_BUTTON_PRESS) &&
1375           (current_event->type != GDK_ENTER_NOTIFY))
1376         menu_shell->ignore_enter = TRUE;
1377
1378       gdk_event_free (current_event);
1379     }
1380   else
1381     menu_shell->ignore_enter = TRUE;
1382
1383   if (menu->torn_off)
1384     {
1385       gtk_menu_tearoff_bg_copy (menu);
1386
1387       gtk_menu_reparent (menu, menu->toplevel, FALSE);
1388     }
1389  
1390   if (parent_menu_shell) 
1391     {
1392       GtkWidget *toplevel;
1393
1394       toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1395       if (GTK_IS_WINDOW (toplevel))
1396         gtk_window_group_add_window (_gtk_window_get_group (GTK_WINDOW (toplevel)), 
1397                                      GTK_WINDOW (menu->toplevel));
1398     }
1399   
1400   menu->parent_menu_item = parent_menu_item;
1401   menu->position_func = func;
1402   menu->position_func_data = data;
1403   menu_shell->activate_time = activate_time;
1404
1405   /* We need to show the menu here rather in the init function because
1406    * code expects to be able to tell if the menu is onscreen by
1407    * looking at the GTK_WIDGET_VISIBLE (menu)
1408    */
1409   gtk_widget_show (GTK_WIDGET (menu));
1410
1411   /* Position the menu, possibly changing the size request
1412    */
1413   gtk_menu_position (menu);
1414
1415   /* Compute the size of the toplevel and realize it so we
1416    * can scroll correctly.
1417    */
1418   {
1419     GtkRequisition tmp_request;
1420     GtkAllocation tmp_allocation = { 0, };
1421
1422     gtk_widget_size_request (menu->toplevel, &tmp_request);
1423     
1424     tmp_allocation.width = tmp_request.width;
1425     tmp_allocation.height = tmp_request.height;
1426
1427     gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1428     
1429     gtk_widget_realize (GTK_WIDGET (menu));
1430   }
1431
1432   gtk_menu_scroll_to (menu, menu->scroll_offset);
1433
1434   /* Once everything is set up correctly, map the toplevel window on
1435      the screen.
1436    */
1437   gtk_widget_show (menu->toplevel);
1438
1439   if (xgrab_shell == widget)
1440     popup_grab_on_window (widget->window, activate_time); /* Should always succeed */
1441   gtk_grab_add (GTK_WIDGET (menu));
1442 }
1443
1444 void
1445 gtk_menu_popdown (GtkMenu *menu)
1446 {
1447   GtkMenuPrivate *private;
1448   GtkMenuShell *menu_shell;
1449
1450   g_return_if_fail (GTK_IS_MENU (menu));
1451   
1452   menu_shell = GTK_MENU_SHELL (menu);
1453   private = gtk_menu_get_private (menu);
1454   
1455   menu_shell->parent_menu_shell = NULL;
1456   menu_shell->active = FALSE;
1457   menu_shell->ignore_enter = FALSE;
1458
1459   private->have_position = FALSE;
1460
1461   gtk_menu_stop_scrolling (menu);
1462   
1463   gtk_menu_stop_navigating_submenu (menu);
1464   
1465   if (menu_shell->active_menu_item)
1466     {
1467       if (menu->old_active_menu_item)
1468         g_object_unref (menu->old_active_menu_item);
1469       menu->old_active_menu_item = menu_shell->active_menu_item;
1470       g_object_ref (menu->old_active_menu_item);
1471     }
1472
1473   gtk_menu_shell_deselect (menu_shell);
1474   
1475   /* The X Grab, if present, will automatically be removed when we hide
1476    * the window */
1477   gtk_widget_hide (menu->toplevel);
1478   gtk_window_group_add_window (_gtk_window_get_group (NULL), GTK_WINDOW (menu->toplevel));
1479
1480   if (menu->torn_off)
1481     {
1482       gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1483       
1484       if (GTK_BIN (menu->toplevel)->child) 
1485         {
1486           gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1487         } 
1488       else
1489         {
1490           /* We popped up the menu from the tearoff, so we need to 
1491            * release the grab - we aren't actually hiding the menu.
1492            */
1493           if (menu_shell->have_xgrab)
1494             {
1495               GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
1496               
1497               gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
1498               gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
1499             }
1500         }
1501
1502       /* gtk_menu_popdown is called each time a menu item is selected from
1503        * a torn off menu. Only scroll back to the saved position if the
1504        * non-tearoff menu was popped down.
1505        */
1506       if (!menu->tearoff_active)
1507         gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1508       menu->tearoff_active = TRUE;
1509     }
1510   else
1511     gtk_widget_hide (GTK_WIDGET (menu));
1512
1513   menu_shell->have_xgrab = FALSE;
1514   gtk_grab_remove (GTK_WIDGET (menu));
1515
1516   menu_grab_transfer_window_destroy (menu);
1517 }
1518
1519 GtkWidget*
1520 gtk_menu_get_active (GtkMenu *menu)
1521 {
1522   GtkWidget *child;
1523   GList *children;
1524   
1525   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1526   
1527   if (!menu->old_active_menu_item)
1528     {
1529       child = NULL;
1530       children = GTK_MENU_SHELL (menu)->children;
1531       
1532       while (children)
1533         {
1534           child = children->data;
1535           children = children->next;
1536           
1537           if (GTK_BIN (child)->child)
1538             break;
1539           child = NULL;
1540         }
1541       
1542       menu->old_active_menu_item = child;
1543       if (menu->old_active_menu_item)
1544         g_object_ref (menu->old_active_menu_item);
1545     }
1546   
1547   return menu->old_active_menu_item;
1548 }
1549
1550 void
1551 gtk_menu_set_active (GtkMenu *menu,
1552                      guint    index)
1553 {
1554   GtkWidget *child;
1555   GList *tmp_list;
1556   
1557   g_return_if_fail (GTK_IS_MENU (menu));
1558   
1559   tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1560   if (tmp_list)
1561     {
1562       child = tmp_list->data;
1563       if (GTK_BIN (child)->child)
1564         {
1565           if (menu->old_active_menu_item)
1566             g_object_unref (menu->old_active_menu_item);
1567           menu->old_active_menu_item = child;
1568           g_object_ref (menu->old_active_menu_item);
1569         }
1570     }
1571 }
1572
1573 void
1574 gtk_menu_set_accel_group (GtkMenu       *menu,
1575                           GtkAccelGroup *accel_group)
1576 {
1577   g_return_if_fail (GTK_IS_MENU (menu));
1578   
1579   if (menu->accel_group != accel_group)
1580     {
1581       if (menu->accel_group)
1582         g_object_unref (menu->accel_group);
1583       menu->accel_group = accel_group;
1584       if (menu->accel_group)
1585         g_object_ref (menu->accel_group);
1586       _gtk_menu_refresh_accel_paths (menu, TRUE);
1587     }
1588 }
1589
1590 GtkAccelGroup*
1591 gtk_menu_get_accel_group (GtkMenu *menu)
1592 {
1593   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1594
1595   return menu->accel_group;
1596 }
1597
1598 static gboolean
1599 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1600                                   guint      signal_id)
1601 {
1602   /* Menu items chain here to figure whether they can activate their
1603    * accelerators.  Unlike ordinary widgets, menus allow accel
1604    * activation even if invisible since that's the usual case for
1605    * submenus/popup-menus. however, the state of the attach widget
1606    * affects the "activeness" of the menu.
1607    */
1608   GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1609
1610   if (awidget)
1611     return gtk_widget_can_activate_accel (awidget, signal_id);
1612   else
1613     return GTK_WIDGET_IS_SENSITIVE (widget);
1614 }
1615
1616 /**
1617  * gtk_menu_set_accel_path
1618  * @menu:       a valid #GtkMenu
1619  * @accel_path: a valid accelerator path
1620  *
1621  * Sets an accelerator path for this menu from which accelerator paths
1622  * for its immediate children, its menu items, can be constructed.
1623  * The main purpose of this function is to spare the programmer the
1624  * inconvenience of having to call gtk_menu_item_set_accel_path() on
1625  * each menu item that should support runtime user changable accelerators.
1626  * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1627  * each menu item of this menu, that contains a label describing its purpose,
1628  * automatically gets an accel path assigned. For example, a menu containing
1629  * menu items "New" and "Exit", will, after 
1630  * <literal>gtk_menu_set_accel_path (menu, "&lt;Gnumeric-Sheet&gt;/File");</literal>
1631  * has been called, assign its items the accel paths:
1632  * <literal>"&lt;Gnumeric-Sheet&gt;/File/New"</literal> and <literal>"&lt;Gnumeric-Sheet&gt;/File/Exit"</literal>.
1633  * Assigning accel paths to menu items then enables the user to change
1634  * their accelerators at runtime. More details about accelerator paths
1635  * and their default setups can be found at gtk_accel_map_add_entry().
1636  */
1637 void
1638 gtk_menu_set_accel_path (GtkMenu     *menu,
1639                          const gchar *accel_path)
1640 {
1641   g_return_if_fail (GTK_IS_MENU (menu));
1642   if (accel_path)
1643     g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1644
1645   g_free (menu->accel_path);
1646   menu->accel_path = g_strdup (accel_path);
1647   if (menu->accel_path)
1648     _gtk_menu_refresh_accel_paths (menu, FALSE);
1649 }
1650
1651 typedef struct {
1652   GtkMenu *menu;
1653   gboolean group_changed;
1654 } AccelPropagation;
1655
1656 static void
1657 refresh_accel_paths_foreach (GtkWidget *widget,
1658                              gpointer   data)
1659 {
1660   AccelPropagation *prop = data;
1661
1662   if (GTK_IS_MENU_ITEM (widget))        /* should always be true */
1663     _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1664                                        prop->menu->accel_path,
1665                                        prop->menu->accel_group,
1666                                        prop->group_changed);
1667 }
1668
1669 static void
1670 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1671                                gboolean group_changed)
1672 {
1673   g_return_if_fail (GTK_IS_MENU (menu));
1674       
1675   if (menu->accel_path && menu->accel_group)
1676     {
1677       AccelPropagation prop;
1678
1679       prop.menu = menu;
1680       prop.group_changed = group_changed;
1681       gtk_container_foreach (GTK_CONTAINER (menu),
1682                              refresh_accel_paths_foreach,
1683                              &prop);
1684     }
1685 }
1686
1687 void
1688 gtk_menu_reposition (GtkMenu *menu)
1689 {
1690   g_return_if_fail (GTK_IS_MENU (menu));
1691
1692   if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1693     gtk_menu_position (menu);
1694 }
1695
1696 static void
1697 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1698                             GtkMenu       *menu)
1699 {
1700   g_return_if_fail (GTK_IS_MENU (menu));
1701
1702   if (adjustment->value != menu->scroll_offset)
1703     gtk_menu_scroll_to (menu, adjustment->value);
1704 }
1705
1706 static void
1707 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1708                             gint     width)
1709 {
1710   GdkGeometry geometry_hints;
1711   
1712   if (!menu->tearoff_window)
1713     return;
1714
1715   if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1716     {
1717       gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1718       width += menu->tearoff_scrollbar->requisition.width;
1719     }
1720
1721   geometry_hints.min_width = width;
1722   geometry_hints.max_width = width;
1723     
1724   geometry_hints.min_height = 0;
1725   geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1726
1727   gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1728                                  NULL,
1729                                  &geometry_hints,
1730                                  GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1731 }
1732
1733 static void
1734 gtk_menu_update_title (GtkMenu *menu)
1735 {
1736   if (menu->tearoff_window)
1737     {
1738       const gchar *title;
1739       GtkWidget *attach_widget;
1740
1741       title = gtk_menu_get_title (menu);
1742       if (!title)
1743         {
1744           attach_widget = gtk_menu_get_attach_widget (menu);
1745           if (GTK_IS_MENU_ITEM (attach_widget))
1746             {
1747               GtkWidget *child = GTK_BIN (attach_widget)->child;
1748               if (GTK_IS_LABEL (child))
1749                 title = gtk_label_get_text (GTK_LABEL (child));
1750             }
1751         }
1752       
1753       if (title)
1754         gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1755     }
1756 }
1757
1758 static GtkWidget*
1759 gtk_menu_get_toplevel (GtkWidget *menu)
1760 {
1761   GtkWidget *attach, *toplevel;
1762
1763   attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
1764
1765   if (GTK_IS_MENU_ITEM (attach))
1766     attach = attach->parent;
1767
1768   if (GTK_IS_MENU (attach))
1769     return gtk_menu_get_toplevel (attach);
1770   else if (GTK_IS_WIDGET (attach))
1771     {
1772       toplevel = gtk_widget_get_toplevel (attach);
1773       if (GTK_WIDGET_TOPLEVEL (toplevel)) 
1774         return toplevel;
1775     }
1776
1777   return NULL;
1778 }
1779
1780 static void
1781 tearoff_window_destroyed (GtkWidget *widget,
1782                           GtkMenu   *menu)
1783 {
1784   gtk_menu_set_tearoff_state (menu, FALSE);
1785 }
1786
1787 void       
1788 gtk_menu_set_tearoff_state (GtkMenu  *menu,
1789                             gboolean  torn_off)
1790 {
1791   gint width, height;
1792   
1793   g_return_if_fail (GTK_IS_MENU (menu));
1794
1795   if (menu->torn_off != torn_off)
1796     {
1797       menu->torn_off = torn_off;
1798       menu->tearoff_active = torn_off;
1799       
1800       if (menu->torn_off)
1801         {
1802           if (GTK_WIDGET_VISIBLE (menu))
1803             gtk_menu_popdown (menu);
1804
1805           if (!menu->tearoff_window)
1806             {
1807               GtkWidget *toplevel;
1808
1809               menu->tearoff_window = gtk_widget_new (GTK_TYPE_WINDOW,
1810                                                      "type", GTK_WINDOW_TOPLEVEL,
1811                                                      "screen", gtk_widget_get_screen (menu->toplevel),
1812                                                      "app-paintable", TRUE,
1813                                                      NULL);
1814
1815               
1816               gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1817                                         GDK_WINDOW_TYPE_HINT_MENU);
1818               gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1819               g_signal_connect (menu->tearoff_window, "destroy",
1820                                 G_CALLBACK (tearoff_window_destroyed), menu);
1821               g_signal_connect (menu->tearoff_window, "event",
1822                                 G_CALLBACK (gtk_menu_window_event), menu);
1823
1824               gtk_menu_update_title (menu);
1825
1826               gtk_widget_realize (menu->tearoff_window);
1827
1828               toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
1829               if (toplevel != NULL)
1830                 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
1831                                               GTK_WINDOW (toplevel));
1832               
1833               menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1834               gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1835
1836               gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
1837               menu->tearoff_adjustment =
1838                 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1839                                                     0,
1840                                                     GTK_WIDGET (menu)->requisition.height,
1841                                                     MENU_SCROLL_STEP2,
1842                                                     height/2,
1843                                                     height));
1844               g_object_connect (menu->tearoff_adjustment,
1845                                 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1846                                 NULL);
1847               menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1848
1849               gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1850                                 menu->tearoff_scrollbar,
1851                                 FALSE, FALSE, 0);
1852               
1853               if (menu->tearoff_adjustment->upper > height)
1854                 gtk_widget_show (menu->tearoff_scrollbar);
1855               
1856               gtk_widget_show (menu->tearoff_hbox);
1857             }
1858           
1859           gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
1860
1861           gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
1862
1863           /* Update menu->requisition
1864            */
1865           gtk_widget_size_request (GTK_WIDGET (menu), NULL);
1866   
1867           gtk_menu_set_tearoff_hints (menu, width);
1868             
1869           gtk_widget_realize (menu->tearoff_window);
1870           gtk_menu_position (menu);
1871           
1872           gtk_widget_show (GTK_WIDGET (menu));
1873           gtk_widget_show (menu->tearoff_window);
1874
1875           gtk_menu_scroll_to (menu, 0);
1876
1877         }
1878       else
1879         {
1880           gtk_widget_hide (GTK_WIDGET (menu));
1881           gtk_widget_hide (menu->tearoff_window);
1882           if (GTK_IS_CONTAINER (menu->toplevel))
1883             gtk_menu_reparent (menu, menu->toplevel, FALSE);
1884           gtk_widget_destroy (menu->tearoff_window);
1885           
1886           menu->tearoff_window = NULL;
1887           menu->tearoff_hbox = NULL;
1888           menu->tearoff_scrollbar = NULL;
1889           menu->tearoff_adjustment = NULL;
1890         }
1891
1892       g_object_notify (G_OBJECT (menu), "tearoff-state");
1893     }
1894 }
1895
1896 /**
1897  * gtk_menu_get_tearoff_state:
1898  * @menu: a #GtkMenu
1899  *
1900  * Returns whether the menu is torn off. See
1901  * gtk_menu_set_tearoff_state ().
1902  *
1903  * Return value: %TRUE if the menu is currently torn off.
1904  **/
1905 gboolean
1906 gtk_menu_get_tearoff_state (GtkMenu *menu)
1907 {
1908   g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
1909
1910   return menu->torn_off;
1911 }
1912
1913 /**
1914  * gtk_menu_set_title:
1915  * @menu: a #GtkMenu
1916  * @title: a string containing the title for the menu.
1917  * 
1918  * Sets the title string for the menu.  The title is displayed when the menu
1919  * is shown as a tearoff menu.
1920  **/
1921 void       
1922 gtk_menu_set_title (GtkMenu     *menu,
1923                     const gchar *title)
1924 {
1925   g_return_if_fail (GTK_IS_MENU (menu));
1926
1927   if (title)
1928     g_object_set_data_full (G_OBJECT (menu), "gtk-menu-title",
1929                             g_strdup (title), (GtkDestroyNotify) g_free);
1930   else
1931     g_object_set_data (G_OBJECT (menu), "gtk-menu-title", NULL);
1932     
1933   gtk_menu_update_title (menu);
1934   g_object_notify (G_OBJECT (menu), "tearoff-title");
1935 }
1936
1937 /**
1938  * gtk_menu_get_title:
1939  * @menu: a #GtkMenu
1940  *
1941  * Returns the title of the menu. See gtk_menu_set_title().
1942  *
1943  * Return value: the title of the menu, or %NULL if the menu has no
1944  * title set on it. This string is owned by the widget and should
1945  * not be modified or freed.
1946  **/
1947 G_CONST_RETURN gchar *
1948 gtk_menu_get_title (GtkMenu *menu)
1949 {
1950   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1951
1952   return g_object_get_data (G_OBJECT (menu), "gtk-menu-title");
1953 }
1954
1955 void
1956 gtk_menu_reorder_child (GtkMenu   *menu,
1957                         GtkWidget *child,
1958                         gint       position)
1959 {
1960   GtkMenuShell *menu_shell;
1961
1962   g_return_if_fail (GTK_IS_MENU (menu));
1963   g_return_if_fail (GTK_IS_MENU_ITEM (child));
1964
1965   menu_shell = GTK_MENU_SHELL (menu);
1966
1967   if (g_list_find (menu_shell->children, child))
1968     {   
1969       menu_shell->children = g_list_remove (menu_shell->children, child);
1970       menu_shell->children = g_list_insert (menu_shell->children, child, position);
1971
1972       menu_queue_resize (menu);
1973     }   
1974 }
1975
1976 static void
1977 gtk_menu_style_set (GtkWidget *widget,
1978                     GtkStyle  *previous_style)
1979 {
1980   if (GTK_WIDGET_REALIZED (widget))
1981     {
1982       GtkMenu *menu = GTK_MENU (widget);
1983       
1984       gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1985       gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1986       gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1987     }
1988 }
1989
1990 static void
1991 gtk_menu_realize (GtkWidget *widget)
1992 {
1993   GdkWindowAttr attributes;
1994   gint attributes_mask;
1995   gint border_width;
1996   GtkMenu *menu;
1997   GtkWidget *child;
1998   GList *children;
1999   guint vertical_padding;
2000   
2001   g_return_if_fail (GTK_IS_MENU (widget));
2002
2003   menu = GTK_MENU (widget);
2004   
2005   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
2006   
2007   attributes.window_type = GDK_WINDOW_CHILD;
2008   attributes.x = widget->allocation.x;
2009   attributes.y = widget->allocation.y;
2010   attributes.width = widget->allocation.width;
2011   attributes.height = widget->allocation.height;
2012   attributes.wclass = GDK_INPUT_OUTPUT;
2013   attributes.visual = gtk_widget_get_visual (widget);
2014   attributes.colormap = gtk_widget_get_colormap (widget);
2015   
2016   attributes.event_mask = gtk_widget_get_events (widget);
2017
2018   attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2019                             GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2020   
2021   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2022   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
2023   gdk_window_set_user_data (widget->window, widget);
2024   
2025   border_width = GTK_CONTAINER (widget)->border_width;
2026
2027   gtk_widget_style_get (GTK_WIDGET (menu),
2028                         "vertical-padding", &vertical_padding,
2029                         NULL);
2030   
2031   attributes.x = border_width + widget->style->xthickness;
2032   attributes.y = border_width + widget->style->ythickness + vertical_padding;
2033   attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
2034   attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
2035
2036   if (menu->upper_arrow_visible)
2037     {
2038       attributes.y += MENU_SCROLL_ARROW_HEIGHT;
2039       attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
2040     }
2041   if (menu->lower_arrow_visible)
2042     attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
2043
2044   menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
2045   gdk_window_set_user_data (menu->view_window, menu);
2046
2047   attributes.x = 0;
2048   attributes.y = 0;
2049   attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
2050   
2051   menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
2052   gdk_window_set_user_data (menu->bin_window, menu);
2053
2054   children = GTK_MENU_SHELL (menu)->children;
2055   while (children)
2056     {
2057       child = children->data;
2058       children = children->next;
2059           
2060       gtk_widget_set_parent_window (child, menu->bin_window);
2061     }
2062   
2063   widget->style = gtk_style_attach (widget->style, widget->window);
2064   gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
2065   gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
2066   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
2067
2068   if (GTK_MENU_SHELL (widget)->active_menu_item)
2069     gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2070                                   GTK_MENU_SHELL (widget)->active_menu_item);
2071
2072   gdk_window_show (menu->bin_window);
2073   gdk_window_show (menu->view_window);
2074 }
2075
2076 static gboolean 
2077 gtk_menu_focus (GtkWidget       *widget,
2078                 GtkDirectionType direction)
2079 {
2080   /*
2081    * A menu or its menu items cannot have focus
2082    */
2083   return FALSE;
2084 }
2085
2086 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2087  */
2088 static GdkWindow *
2089 menu_grab_transfer_window_get (GtkMenu *menu)
2090 {
2091   GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2092   if (!window)
2093     {
2094       GdkWindowAttr attributes;
2095       gint attributes_mask;
2096       
2097       attributes.x = -100;
2098       attributes.y = -100;
2099       attributes.width = 10;
2100       attributes.height = 10;
2101       attributes.window_type = GDK_WINDOW_TEMP;
2102       attributes.wclass = GDK_INPUT_ONLY;
2103       attributes.override_redirect = TRUE;
2104       attributes.event_mask = 0;
2105
2106       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2107       
2108       window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2109                                &attributes, attributes_mask);
2110       gdk_window_set_user_data (window, menu);
2111
2112       gdk_window_show (window);
2113
2114       g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", window);
2115     }
2116
2117   return window;
2118 }
2119
2120 static void
2121 menu_grab_transfer_window_destroy (GtkMenu *menu)
2122 {
2123   GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2124   if (window)
2125     {
2126       gdk_window_set_user_data (window, NULL);
2127       gdk_window_destroy (window);
2128       g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", NULL);
2129     }
2130 }
2131
2132 static void
2133 gtk_menu_unrealize (GtkWidget *widget)
2134 {
2135   GtkMenu *menu;
2136
2137   g_return_if_fail (GTK_IS_MENU (widget));
2138
2139   menu = GTK_MENU (widget);
2140
2141   menu_grab_transfer_window_destroy (menu);
2142
2143   gdk_window_set_user_data (menu->view_window, NULL);
2144   gdk_window_destroy (menu->view_window);
2145   menu->view_window = NULL;
2146
2147   gdk_window_set_user_data (menu->bin_window, NULL);
2148   gdk_window_destroy (menu->bin_window);
2149   menu->bin_window = NULL;
2150
2151   (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
2152 }
2153
2154 static void
2155 gtk_menu_size_request (GtkWidget      *widget,
2156                        GtkRequisition *requisition)
2157 {
2158   gint i;
2159   GtkMenu *menu;
2160   GtkMenuShell *menu_shell;
2161   GtkWidget *child;
2162   GList *children;
2163   guint max_toggle_size;
2164   guint max_accel_width;
2165   guint vertical_padding;
2166   GtkRequisition child_requisition;
2167   GtkMenuPrivate *priv;
2168   
2169   g_return_if_fail (GTK_IS_MENU (widget));
2170   g_return_if_fail (requisition != NULL);
2171   
2172   menu = GTK_MENU (widget);
2173   menu_shell = GTK_MENU_SHELL (widget);
2174   priv = gtk_menu_get_private (menu);
2175   
2176   requisition->width = 0;
2177   requisition->height = 0;
2178   
2179   max_toggle_size = 0;
2180   max_accel_width = 0;
2181   
2182   g_free (priv->heights);
2183   priv->heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
2184   priv->heights_length = gtk_menu_get_n_rows (menu);
2185
2186   children = menu_shell->children;
2187   while (children)
2188     {
2189       gint part;
2190       gint toggle_size;
2191       guint l, r, t, b;
2192
2193       child = children->data;
2194       children = children->next;
2195       
2196       if (! GTK_WIDGET_VISIBLE (child))
2197         continue;
2198
2199       get_effective_child_attach (child, &l, &r, &t, &b);
2200
2201       /* It's important to size_request the child
2202        * before doing the toggle size request, in
2203        * case the toggle size request depends on the size
2204        * request of a child of the child (e.g. for ImageMenuItem)
2205        */
2206
2207        GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
2208        gtk_widget_size_request (child, &child_requisition);
2209
2210        gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2211        max_toggle_size = MAX (max_toggle_size, toggle_size);
2212        max_accel_width = MAX (max_accel_width,
2213                               GTK_MENU_ITEM (child)->accelerator_width);
2214
2215        part = child_requisition.width / (r - l);
2216        requisition->width = MAX (requisition->width, part);
2217
2218        part = MAX (child_requisition.height, toggle_size) / (b - t);
2219        priv->heights[t] = MAX (priv->heights[t], part);
2220     }
2221
2222   for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2223     requisition->height += priv->heights[i];
2224
2225   requisition->width += max_toggle_size + max_accel_width;
2226   requisition->width *= gtk_menu_get_n_columns (menu);
2227   requisition->width += (GTK_CONTAINER (menu)->border_width +
2228                          widget->style->xthickness) * 2;
2229
2230   gtk_widget_style_get (GTK_WIDGET (menu),
2231                         "vertical-padding", &vertical_padding,
2232                         NULL);
2233   requisition->height += (GTK_CONTAINER (menu)->border_width + vertical_padding +
2234                           widget->style->ythickness) * 2;
2235   
2236   menu->toggle_size = max_toggle_size;
2237
2238   /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
2239    */
2240   if (menu->tearoff_active)
2241     gtk_menu_set_tearoff_hints (menu, requisition->width);
2242 }
2243
2244 static void
2245 gtk_menu_size_allocate (GtkWidget     *widget,
2246                         GtkAllocation *allocation)
2247 {
2248   GtkMenu *menu;
2249   GtkMenuShell *menu_shell;
2250   GtkWidget *child;
2251   GtkAllocation child_allocation;
2252   GtkRequisition child_requisition;
2253   GtkMenuPrivate *priv;
2254   GList *children;
2255   gint x, y;
2256   gint width, height;
2257   guint vertical_padding;
2258
2259   g_return_if_fail (GTK_IS_MENU (widget));
2260   g_return_if_fail (allocation != NULL);
2261   
2262   menu = GTK_MENU (widget);
2263   menu_shell = GTK_MENU_SHELL (widget);
2264   priv = gtk_menu_get_private (menu);
2265
2266   widget->allocation = *allocation;
2267   gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
2268
2269   gtk_widget_style_get (GTK_WIDGET (menu),
2270                         "vertical-padding", &vertical_padding,
2271                         NULL);
2272   
2273   x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness;
2274   y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness + vertical_padding;
2275
2276   width = MAX (1, allocation->width - x * 2);
2277   height = MAX (1, allocation->height - y * 2);
2278
2279   child_requisition.width -= x * 2;
2280   child_requisition.height -= y * 2;
2281
2282   if (menu_shell->active)
2283     gtk_menu_scroll_to (menu, menu->scroll_offset);
2284   
2285   if (menu->upper_arrow_visible && !menu->tearoff_active)
2286     {
2287       y += MENU_SCROLL_ARROW_HEIGHT;
2288       height -= MENU_SCROLL_ARROW_HEIGHT;
2289     }
2290   
2291   if (menu->lower_arrow_visible && !menu->tearoff_active)
2292     height -= MENU_SCROLL_ARROW_HEIGHT;
2293   
2294   if (GTK_WIDGET_REALIZED (widget))
2295     {
2296       gdk_window_move_resize (widget->window,
2297                               allocation->x, allocation->y,
2298                               allocation->width, allocation->height);
2299
2300       gdk_window_move_resize (menu->view_window,
2301                               x,
2302                               y,
2303                               width,
2304                               height);
2305     }
2306
2307   if (menu_shell->children)
2308     {
2309       gint base_width = width / gtk_menu_get_n_columns (menu);
2310
2311       children = menu_shell->children;
2312       while (children)
2313         {
2314           child = children->data;
2315           children = children->next;
2316
2317           if (GTK_WIDGET_VISIBLE (child))
2318             {
2319               gint i;
2320               guint l, r, t, b;
2321
2322               get_effective_child_attach (child, &l, &r, &t, &b);
2323
2324               if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2325                 {
2326                   guint tmp;
2327                   tmp = gtk_menu_get_n_columns (menu) - l;
2328                   l = gtk_menu_get_n_columns (menu) - r;
2329                   r = tmp;
2330                 }
2331
2332               child_allocation.width = (r - l) * base_width;
2333               child_allocation.height = 0;
2334               child_allocation.x = l * base_width;
2335               child_allocation.y = 0;
2336
2337               for (i = 0; i < b; i++)
2338                 {
2339                   if (i < t)
2340                     child_allocation.y += priv->heights[i];
2341                   else
2342                     child_allocation.height += priv->heights[i];
2343                 }
2344
2345               gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2346                                                   menu->toggle_size);
2347
2348               gtk_widget_size_allocate (child, &child_allocation);
2349               gtk_widget_queue_draw (child);
2350             }
2351         }
2352       
2353       /* Resize the item window */
2354       if (GTK_WIDGET_REALIZED (widget))
2355         {
2356           gint i;
2357           gint width, height;
2358
2359           height = 0;
2360           for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2361             height += priv->heights[i];
2362
2363           width = gtk_menu_get_n_columns (menu) * base_width;
2364           gdk_window_resize (menu->bin_window, width, height);
2365         }
2366
2367       if (menu->tearoff_active)
2368         {
2369           if (allocation->height >= widget->requisition.height)
2370             {
2371               if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2372                 {
2373                   gtk_widget_hide (menu->tearoff_scrollbar);
2374                   gtk_menu_set_tearoff_hints (menu, allocation->width);
2375
2376                   gtk_menu_scroll_to (menu, 0);
2377                 }
2378             }
2379           else
2380             {
2381               menu->tearoff_adjustment->upper = widget->requisition.height;
2382               menu->tearoff_adjustment->page_size = allocation->height;
2383               
2384               if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2385                   menu->tearoff_adjustment->upper)
2386                 {
2387                   gint value;
2388                   value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2389                   if (value < 0)
2390                     value = 0;
2391                   gtk_menu_scroll_to (menu, value);
2392                 }
2393               
2394               gtk_adjustment_changed (menu->tearoff_adjustment);
2395               
2396               if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
2397                 {
2398                   gtk_widget_show (menu->tearoff_scrollbar);
2399                   gtk_menu_set_tearoff_hints (menu, allocation->width);
2400                 }
2401             }
2402         }
2403     }
2404 }
2405
2406 static void
2407 gtk_menu_paint (GtkWidget      *widget,
2408                 GdkEventExpose *event)
2409 {
2410   GtkMenu *menu;
2411   gint width, height;
2412   gint border_x, border_y;
2413   guint vertical_padding;
2414   
2415   g_return_if_fail (GTK_IS_MENU (widget));
2416
2417   menu = GTK_MENU (widget);
2418
2419   gtk_widget_style_get (GTK_WIDGET (menu),
2420                         "vertical-padding", &vertical_padding,
2421                         NULL);
2422   
2423   border_x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness;
2424   border_y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
2425   gdk_drawable_get_size (widget->window, &width, &height);
2426
2427   if (event->window == widget->window)
2428     {
2429       gint arrow_space = MENU_SCROLL_ARROW_HEIGHT - 2 * widget->style->ythickness;
2430       gint arrow_size = 0.7 * arrow_space;
2431         
2432       gtk_paint_box (widget->style,
2433                      widget->window,
2434                      GTK_STATE_NORMAL,
2435                      GTK_SHADOW_OUT,
2436                      NULL, widget, "menu",
2437                      0, 0, -1, -1);
2438       if (menu->upper_arrow_visible && !menu->tearoff_active)
2439         {
2440           gtk_paint_box (widget->style,
2441                          widget->window,
2442                          menu->upper_arrow_prelight ?
2443                          GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2444                          GTK_SHADOW_OUT,
2445                          NULL, widget, "menu",
2446                          border_x,
2447                          border_y,
2448                          width - 2 * border_x,
2449                          MENU_SCROLL_ARROW_HEIGHT);
2450           
2451           gtk_paint_arrow (widget->style,
2452                            widget->window,
2453                            menu->upper_arrow_prelight ?
2454                            GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2455                            GTK_SHADOW_OUT,
2456                            NULL, widget, "menu_scroll_arrow_up",
2457                            GTK_ARROW_UP,
2458                            TRUE,
2459                            (width - arrow_size ) / 2,
2460                            border_y + widget->style->ythickness + (arrow_space - arrow_size)/2,
2461                            arrow_size, arrow_size);
2462         }
2463   
2464       if (menu->lower_arrow_visible && !menu->tearoff_active)
2465         {
2466           gtk_paint_box (widget->style,
2467                          widget->window,
2468                          menu->lower_arrow_prelight ?
2469                          GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2470                          GTK_SHADOW_OUT,
2471                          NULL, widget, "menu",
2472                          border_x,
2473                          height - border_y - MENU_SCROLL_ARROW_HEIGHT,
2474                          width - 2*border_x,
2475                          MENU_SCROLL_ARROW_HEIGHT);
2476           
2477           gtk_paint_arrow (widget->style,
2478                            widget->window,
2479                            menu->lower_arrow_prelight ?
2480                            GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
2481                            GTK_SHADOW_OUT,
2482                            NULL, widget, "menu_scroll_arrow_down",
2483                            GTK_ARROW_DOWN,
2484                            TRUE,
2485                            (width - arrow_size) / 2,
2486                            height - border_y - MENU_SCROLL_ARROW_HEIGHT +
2487                               widget->style->ythickness + (arrow_space - arrow_size)/2,
2488                            arrow_size, arrow_size);
2489         }
2490     }
2491 }
2492
2493 static gboolean
2494 gtk_menu_expose (GtkWidget      *widget,
2495                  GdkEventExpose *event)
2496 {
2497   g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2498   g_return_val_if_fail (event != NULL, FALSE);
2499
2500   if (GTK_WIDGET_DRAWABLE (widget))
2501     {
2502       gtk_menu_paint (widget, event);
2503       
2504       (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
2505     }
2506   
2507   return FALSE;
2508 }
2509
2510 static void
2511 gtk_menu_show (GtkWidget *widget)
2512 {
2513   GtkMenu *menu = GTK_MENU (widget);
2514
2515   _gtk_menu_refresh_accel_paths (menu, FALSE);
2516
2517   GTK_WIDGET_CLASS (parent_class)->show (widget);
2518 }
2519
2520 static gboolean
2521 gtk_menu_button_press (GtkWidget      *widget,
2522                          GdkEventButton *event)
2523 {
2524   /* Don't pop down the menu for releases over scroll arrows
2525    */
2526   if (GTK_IS_MENU (widget))
2527     {
2528       GtkMenu *menu = GTK_MENU (widget);
2529
2530       if (menu->upper_arrow_prelight ||  menu->lower_arrow_prelight)
2531         return TRUE;
2532     }
2533
2534   return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
2535 }
2536
2537 static gboolean
2538 gtk_menu_button_release (GtkWidget      *widget,
2539                          GdkEventButton *event)
2540 {
2541   /* Don't pop down the menu for releases over scroll arrows
2542    */
2543   if (GTK_IS_MENU (widget))
2544     {
2545       GtkMenu *menu = GTK_MENU (widget);
2546
2547       if (menu->upper_arrow_prelight ||  menu->lower_arrow_prelight)
2548         return TRUE;
2549     }
2550
2551   return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
2552 }
2553
2554 static const gchar *
2555 get_accel_path (GtkWidget *menu_item,
2556                 gboolean  *locked)
2557 {
2558   const gchar *path;
2559   GtkWidget *label;
2560   GClosure *accel_closure;
2561   GtkAccelGroup *accel_group;    
2562
2563   path = _gtk_widget_get_accel_path (menu_item, locked);
2564   if (!path)
2565     {
2566       path = GTK_MENU_ITEM (menu_item)->accel_path;
2567       
2568       if (locked)
2569         {
2570           *locked = TRUE;
2571
2572           label = GTK_BIN (menu_item)->child;
2573           
2574           if (GTK_IS_ACCEL_LABEL (label))
2575             {
2576               g_object_get (label, 
2577                             "accel-closure", &accel_closure, 
2578                             NULL);
2579               if (accel_closure)
2580                 {
2581                   accel_group = gtk_accel_group_from_accel_closure (accel_closure);
2582                   
2583                   *locked = accel_group->lock_count > 0;
2584                 }
2585             }
2586         }
2587     }
2588
2589   return path;
2590 }
2591
2592 static gboolean
2593 gtk_menu_key_press (GtkWidget   *widget,
2594                     GdkEventKey *event)
2595 {
2596   GtkMenuShell *menu_shell;
2597   GtkMenu *menu;
2598   gboolean delete = FALSE;
2599   gboolean can_change_accels;
2600   gchar *accel = NULL;
2601   guint accel_key, accel_mods;
2602   GdkModifierType consumed_modifiers;
2603   GdkDisplay *display;
2604   
2605   g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2606   g_return_val_if_fail (event != NULL, FALSE);
2607       
2608   menu_shell = GTK_MENU_SHELL (widget);
2609   menu = GTK_MENU (widget);
2610   
2611   gtk_menu_stop_navigating_submenu (menu);
2612
2613   if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
2614     return TRUE;
2615
2616   display = gtk_widget_get_display (widget);
2617     
2618   g_object_get (gtk_widget_get_settings (widget),
2619                 "gtk-menu-bar-accel", &accel,
2620                 "gtk-can-change-accels", &can_change_accels,
2621                 NULL);
2622
2623   if (accel)
2624     {
2625       guint keyval = 0;
2626       GdkModifierType mods = 0;
2627       gboolean handled = FALSE;
2628       
2629       gtk_accelerator_parse (accel, &keyval, &mods);
2630
2631       if (keyval == 0)
2632         g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
2633
2634       /* FIXME this is wrong, needs to be in the global accel resolution
2635        * thing, to properly consider i18n etc., but that probably requires
2636        * AccelGroup changes etc.
2637        */
2638       if (event->keyval == keyval &&
2639           (mods & event->state) == mods)
2640         gtk_menu_shell_cancel (menu_shell);
2641
2642       g_free (accel);
2643
2644       if (handled)
2645         return TRUE;
2646     }
2647   
2648   switch (event->keyval)
2649     {
2650     case GDK_Delete:
2651     case GDK_KP_Delete:
2652     case GDK_BackSpace:
2653       delete = TRUE;
2654       break;
2655     default:
2656       break;
2657     }
2658
2659   /* Figure out what modifiers went into determining the key symbol */
2660   gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
2661                                        event->hardware_keycode, event->state, event->group,
2662                                        NULL, NULL, NULL, &consumed_modifiers);
2663
2664   accel_key = gdk_keyval_to_lower (event->keyval);
2665   accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
2666
2667   /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
2668    * We re-upper case when we match against the keyval, but display and save in caseless form.
2669    */
2670   if (accel_key != event->keyval)
2671     accel_mods |= GDK_SHIFT_MASK;
2672   
2673   /* Modify the accelerators */
2674   if (can_change_accels &&
2675       menu_shell->active_menu_item &&
2676       GTK_BIN (menu_shell->active_menu_item)->child &&                  /* no separators */
2677       GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL &&  /* no submenus */
2678       (delete || gtk_accelerator_valid (accel_key, accel_mods)))
2679     {
2680       GtkWidget *menu_item = menu_shell->active_menu_item;
2681       gboolean locked, replace_accels = TRUE;
2682       const gchar *path;
2683
2684       path = get_accel_path (menu_item, &locked);
2685       if (!path || locked)
2686         {
2687           /* can't change accelerators on menu_items without paths
2688            * (basically, those items are accelerator-locked).
2689            */
2690           /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
2691           gdk_display_beep (display);
2692         }
2693       else
2694         {
2695           gboolean changed;
2696
2697           /* For the keys that act to delete the current setting, we delete
2698            * the current setting if there is one, otherwise, we set the
2699            * key as the accelerator.
2700            */
2701           if (delete)
2702             {
2703               GtkAccelKey key;
2704               
2705               if (gtk_accel_map_lookup_entry (path, &key) &&
2706                   (key.accel_key || key.accel_mods))
2707                 {
2708                   accel_key = 0;
2709                   accel_mods = 0;
2710                 }
2711             }
2712           changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
2713
2714           if (!changed)
2715             {
2716               /* we failed, probably because this key is in use and
2717                * locked already
2718                */
2719               /* g_print("failed to change\n"); */
2720               gdk_display_beep (display);
2721             }
2722         }
2723     }
2724   
2725   return TRUE;
2726 }
2727
2728 static gboolean
2729 check_threshold (GtkWidget *widget,
2730                  int start_x, int start_y,
2731                  int x, int y)
2732 {
2733 #define THRESHOLD 8
2734   
2735   return
2736     ABS (start_x - x) > THRESHOLD  ||
2737     ABS (start_y - y) > THRESHOLD;
2738 }
2739
2740 static gboolean
2741 definitely_within_item (GtkWidget *widget, 
2742                         int x,
2743                         int y)
2744 {
2745   GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
2746   int w, h;
2747
2748   gdk_drawable_get_size (window, &w, &h);
2749   
2750   return
2751     check_threshold (widget, 0, 0, x, y) &&
2752     check_threshold (widget, w - 1, 0, x, y) &&
2753     check_threshold (widget, w - 1, h - 1, x, y) &&
2754     check_threshold (widget, 0, h - 1, x, y);
2755 }
2756
2757 static gboolean
2758 gtk_menu_motion_notify  (GtkWidget         *widget,
2759                          GdkEventMotion    *event)
2760 {
2761   GtkWidget *menu_item;
2762   GtkMenu *menu;
2763   GtkMenuShell *menu_shell;
2764   GtkMenuPrivate *priv;
2765
2766   gboolean need_enter;
2767
2768   if (GTK_IS_MENU (widget))
2769     gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root, TRUE);
2770
2771   /* We received the event for one of two reasons:
2772    *
2773    * a) We are the active menu, and did gtk_grab_add()
2774    * b) The widget is a child of ours, and the event was propagated
2775    *
2776    * Since for computation of navigation regions, we want the menu which
2777    * is the parent of the menu item, for a), we need to find that menu,
2778    * which may be different from 'widget'.
2779    */
2780   menu_item = gtk_get_event_widget ((GdkEvent*) event);
2781   if (!menu_item || !GTK_IS_MENU_ITEM (menu_item) ||
2782       !GTK_IS_MENU (menu_item->parent))
2783     return FALSE;
2784
2785   menu_shell = GTK_MENU_SHELL (menu_item->parent);
2786   menu = GTK_MENU (menu_shell);
2787
2788   priv = gtk_menu_get_private (GTK_MENU (widget));
2789
2790   if (definitely_within_item (menu_item, event->x, event->y))
2791     menu_shell->activate_time = 0;
2792   
2793   need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
2794
2795   /* Check to see if we are within an active submenu's navigation region
2796    */
2797   if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2798     return TRUE; 
2799
2800   /* Make sure we pop down if we enter a non-selectable menu item, so we
2801    * don't show a submenu when the cursor is outside the stay-up triangle.
2802    */
2803   if (!_gtk_menu_item_is_selectable (menu_item))
2804     {
2805       gtk_menu_shell_deselect (menu_shell);
2806       return FALSE;
2807     }
2808
2809   if (need_enter)
2810     {
2811       /* The menu is now sensitive to enter events on its items, but
2812        * was previously sensitive.  So we fake an enter event.
2813        */
2814       gint width, height;
2815       
2816       menu_shell->ignore_enter = FALSE; 
2817       
2818       gdk_drawable_get_size (event->window, &width, &height);
2819       if (event->x >= 0 && event->x < width &&
2820           event->y >= 0 && event->y < height)
2821         {
2822           GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
2823           gboolean result;
2824
2825           send_event->crossing.window = g_object_ref (event->window);
2826           send_event->crossing.time = event->time;
2827           send_event->crossing.send_event = TRUE;
2828           send_event->crossing.x_root = event->x_root;
2829           send_event->crossing.y_root = event->y_root;
2830           send_event->crossing.x = event->x;
2831           send_event->crossing.y = event->y;
2832
2833           /* We send the event to 'widget', the currently active menu,
2834            * instead of 'menu', the menu that the pointer is in. This
2835            * will ensure that the event will be ignored unless the
2836            * menuitem is a child of the active menu or some parent
2837            * menu of the active menu.
2838            */
2839           result = gtk_widget_event (widget, send_event);
2840           gdk_event_free (send_event);
2841
2842           return result;
2843         }
2844     }
2845
2846   return FALSE;
2847 }
2848
2849 static void
2850 gtk_menu_scroll_by (GtkMenu *menu, 
2851                     gint     step)
2852 {
2853   GtkWidget *widget;
2854   gint offset;
2855   gint view_width, view_height;
2856
2857   widget = GTK_WIDGET (menu);
2858   offset = menu->scroll_offset + step;
2859
2860   /* If we scroll upward and the non-visible top part
2861    * is smaller than the scroll arrow it would be
2862    * pretty stupid to show the arrow and taking more
2863    * screen space than just scrolling to the top.
2864    */
2865   if ((step < 0) && (offset < MENU_SCROLL_ARROW_HEIGHT))
2866     offset = 0;
2867
2868   /* Don't scroll over the top if we weren't before: */
2869   if ((menu->scroll_offset >= 0) && (offset < 0))
2870     offset = 0;
2871
2872   gdk_drawable_get_size (widget->window, &view_width, &view_height);
2873
2874   /* Don't scroll past the bottom if we weren't before: */
2875   if (menu->scroll_offset > 0)
2876     view_height -= MENU_SCROLL_ARROW_HEIGHT;
2877   
2878   if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
2879       (offset + view_height > widget->requisition.height))
2880     offset = widget->requisition.height - view_height;
2881
2882   if (offset != menu->scroll_offset)
2883     gtk_menu_scroll_to (menu, offset);
2884 }
2885
2886 static gboolean
2887 gtk_menu_scroll_timeout (gpointer  data)
2888 {
2889   GtkMenu *menu;
2890
2891   GDK_THREADS_ENTER ();
2892
2893   menu = GTK_MENU (data);
2894   gtk_menu_scroll_by (menu, menu->scroll_step);
2895
2896   GDK_THREADS_LEAVE ();
2897
2898   return TRUE;
2899 }
2900
2901 static gboolean
2902 gtk_menu_scroll (GtkWidget      *widget,
2903                  GdkEventScroll *event)
2904 {
2905   GtkMenu *menu = GTK_MENU (widget);
2906
2907   switch (event->direction)
2908     {
2909     case GDK_SCROLL_RIGHT:
2910     case GDK_SCROLL_DOWN:
2911       gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
2912       break;
2913     case GDK_SCROLL_LEFT:
2914     case GDK_SCROLL_UP:
2915       gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
2916       break;
2917     }
2918
2919   return TRUE;
2920 }
2921
2922 static void
2923 gtk_menu_handle_scrolling (GtkMenu *menu,
2924                            gint x,
2925                            gint y,
2926                            gboolean enter)
2927 {
2928   GtkMenuShell *menu_shell;
2929   gint width, height;
2930   gint border;
2931   GdkRectangle rect;
2932   gboolean in_arrow;
2933   gboolean scroll_fast = FALSE;
2934   guint vertical_padding;
2935   gint top_x, top_y;
2936   gint win_x, win_y;
2937
2938   menu_shell = GTK_MENU_SHELL (menu);
2939
2940   gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
2941
2942   gtk_widget_style_get (GTK_WIDGET (menu),
2943                         "vertical-padding", &vertical_padding,
2944                         NULL);
2945   
2946   border = GTK_CONTAINER (menu)->border_width +
2947     GTK_WIDGET (menu)->style->ythickness + vertical_padding;
2948
2949   gdk_window_get_position (menu->toplevel->window, &top_x, &top_y);
2950   gdk_window_get_position (GTK_WIDGET (menu)->window, &win_x, &win_y);
2951   win_x += top_x;
2952   win_y += top_y;
2953   
2954   if (menu->upper_arrow_visible && !menu->tearoff_active)
2955     {
2956       rect.x = win_x;
2957       rect.y = win_y;
2958       rect.width = width;
2959       rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2960       
2961       in_arrow = FALSE;
2962       if ((x >= rect.x) && (x < rect.x + rect.width) &&
2963           (y >= rect.y) && (y < rect.y + rect.height))
2964         {
2965           in_arrow = TRUE;
2966           scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
2967         }
2968         
2969       if (enter && in_arrow &&
2970           (!menu->upper_arrow_prelight || menu->scroll_fast != scroll_fast))
2971         {
2972           menu->upper_arrow_prelight = TRUE;
2973           menu->scroll_fast = scroll_fast;
2974           gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2975           
2976           /* Deselect the active item so that any submenus are poped down */
2977           gtk_menu_shell_deselect (menu_shell);
2978
2979           gtk_menu_remove_scroll_timeout (menu);
2980           menu->scroll_step = (scroll_fast) ? -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
2981           menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2982                                             gtk_menu_scroll_timeout,
2983                                             menu);
2984         }
2985       else if (!enter && !in_arrow && menu->upper_arrow_prelight)
2986         {
2987           gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2988           
2989           gtk_menu_stop_scrolling (menu);
2990         }
2991     }
2992   
2993   if (menu->lower_arrow_visible && !menu->tearoff_active)
2994     {
2995       rect.x = win_x;
2996       rect.y = win_y + height - border - MENU_SCROLL_ARROW_HEIGHT;
2997       rect.width = width;
2998       rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2999
3000       in_arrow = FALSE;
3001       if ((x >= rect.x) && (x < rect.x + rect.width) &&
3002           (y >= rect.y) && (y < rect.y + rect.height))
3003         {
3004           in_arrow = TRUE;
3005           scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
3006         }
3007
3008       if (enter && in_arrow &&
3009           (!menu->lower_arrow_prelight || menu->scroll_fast != scroll_fast))
3010         {
3011           menu->lower_arrow_prelight = TRUE;
3012           menu->scroll_fast = scroll_fast;
3013           gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
3014
3015           /* Deselect the active item so that any submenus are poped down */
3016           gtk_menu_shell_deselect (menu_shell);
3017
3018           gtk_menu_remove_scroll_timeout (menu);
3019           menu->scroll_step = (scroll_fast) ? MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
3020           menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
3021                                             gtk_menu_scroll_timeout,
3022                                             menu);
3023         }
3024       else if (!enter && !in_arrow && menu->lower_arrow_prelight)
3025         {
3026           gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
3027           
3028           gtk_menu_stop_scrolling (menu);
3029         }
3030     }
3031 }
3032
3033 static gboolean
3034 gtk_menu_enter_notify (GtkWidget        *widget,
3035                        GdkEventCrossing *event)
3036 {
3037   GtkWidget *menu_item;
3038
3039   menu_item = gtk_get_event_widget ((GdkEvent*) event);
3040   if (widget && GTK_IS_MENU (widget))
3041     {
3042       GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
3043
3044       if (!menu_shell->ignore_enter)
3045         gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root, TRUE);
3046     }
3047
3048   if (menu_item && GTK_IS_MENU_ITEM (menu_item))
3049     {
3050       GtkWidget *menu = menu_item->parent;
3051       
3052       if (menu && GTK_IS_MENU (menu))
3053         {
3054           GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
3055           GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
3056
3057           if (priv->seen_item_enter)
3058             {
3059               /* This is the second enter we see for an item
3060                * on this menu. This means a release should always
3061                * mean activate.
3062                */
3063               menu_shell->activate_time = 0;
3064             }
3065           else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
3066                     event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
3067             {
3068               if (definitely_within_item (menu_item, event->x, event->y))
3069                 {
3070                   /* This is an actual user-enter (ie. not a pop-under)
3071                    * In this case, the user must either have entered
3072                    * sufficiently far enough into the item, or he must move
3073                    * far enough away from the enter point. (see
3074                    * gtk_menu_motion_notify())
3075                    */
3076                   menu_shell->activate_time = 0;
3077                 }
3078             }
3079             
3080           priv->seen_item_enter = TRUE;
3081         }
3082     }
3083   
3084   /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
3085    * will not correspond to the event widget's parent.  Check to see
3086    * if we are in the parent's navigation region.
3087    */
3088   if (menu_item && GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
3089       gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent), event->x_root, event->y_root))
3090     return TRUE;
3091
3092   return GTK_WIDGET_CLASS (parent_class)->enter_notify_event (widget, event); 
3093 }
3094
3095 static gboolean
3096 gtk_menu_leave_notify (GtkWidget        *widget,
3097                        GdkEventCrossing *event)
3098 {
3099   GtkMenuShell *menu_shell;
3100   GtkMenu *menu;
3101   GtkMenuItem *menu_item;
3102   GtkWidget *event_widget;
3103
3104   menu = GTK_MENU (widget);
3105   menu_shell = GTK_MENU_SHELL (widget); 
3106   
3107   if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3108     return TRUE; 
3109
3110   gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE);
3111   
3112   event_widget = gtk_get_event_widget ((GdkEvent*) event);
3113   
3114   if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
3115     return TRUE;
3116   
3117   menu_item = GTK_MENU_ITEM (event_widget); 
3118   
3119   /* Here we check to see if we're leaving an active menu item with a submenu, 
3120    * in which case we enter submenu navigation mode. 
3121    */
3122   if (menu_shell->active_menu_item != NULL
3123       && menu_item->submenu != NULL
3124       && menu_item->submenu_placement == GTK_LEFT_RIGHT)
3125     {
3126       if (GTK_MENU_SHELL (menu_item->submenu)->active)
3127         {
3128           gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
3129           return TRUE;
3130         }
3131       else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
3132         {
3133           /* We are leaving an active menu item with nonactive submenu.
3134            * Deselect it so we don't surprise the user with by popping
3135            * up a submenu _after_ he left the item.
3136            */
3137           gtk_menu_shell_deselect (menu_shell);
3138           return TRUE;
3139         }
3140     }
3141   
3142   return GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event); 
3143 }
3144
3145 static void 
3146 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
3147 {
3148   if (menu->navigation_region) 
3149     {
3150       gdk_region_destroy (menu->navigation_region);
3151       menu->navigation_region = NULL;
3152     }
3153   
3154   if (menu->navigation_timeout)
3155     {
3156       g_source_remove (menu->navigation_timeout);
3157       menu->navigation_timeout = 0;
3158     }
3159 }
3160
3161 /* When the timeout is elapsed, the navigation region is destroyed
3162  * and the menuitem under the pointer (if any) is selected.
3163  */
3164 static gboolean
3165 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
3166 {
3167   GtkMenu *menu = user_data;
3168   GdkWindow *child_window;
3169
3170   GDK_THREADS_ENTER ();
3171
3172   gtk_menu_stop_navigating_submenu (menu);
3173   
3174   if (GTK_WIDGET_REALIZED (menu))
3175     {
3176       child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
3177
3178       if (child_window)
3179         {
3180           GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3181
3182           send_event->crossing.window = g_object_ref (child_window);
3183           send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
3184           send_event->crossing.send_event = TRUE;
3185
3186           GTK_WIDGET_CLASS (parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
3187
3188           gdk_event_free (send_event);
3189         }
3190     }
3191
3192   GDK_THREADS_LEAVE ();
3193
3194   return FALSE; 
3195 }
3196
3197 static gboolean
3198 gtk_menu_navigating_submenu (GtkMenu *menu,
3199                              gint     event_x,
3200                              gint     event_y)
3201 {
3202   if (menu->navigation_region)
3203     {
3204       if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
3205         return TRUE;
3206       else
3207         {
3208           gtk_menu_stop_navigating_submenu (menu);
3209           return FALSE;
3210         }
3211     }
3212   return FALSE;
3213 }
3214
3215 #undef DRAW_STAY_UP_TRIANGLE
3216
3217 #ifdef DRAW_STAY_UP_TRIANGLE
3218
3219 static void
3220 draw_stay_up_triangle (GdkWindow *window,
3221                        GdkRegion *region)
3222 {
3223   /* Draw ugly color all over the stay-up triangle */
3224   GdkColor ugly_color = { 0, 50000, 10000, 10000 };
3225   GdkGCValues gc_values;
3226   GdkGC *ugly_gc;
3227   GdkRectangle clipbox;
3228
3229   gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
3230   ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
3231   gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
3232   gdk_gc_set_clip_region (ugly_gc, region);
3233
3234   gdk_region_get_clipbox (region, &clipbox);
3235   
3236   gdk_draw_rectangle (window,
3237                      ugly_gc,
3238                      TRUE,
3239                      clipbox.x, clipbox.y,
3240                      clipbox.width, clipbox.height);
3241   
3242   g_object_unref (ugly_gc);
3243 }
3244 #endif
3245
3246 static GdkRegion *
3247 flip_region (GdkRegion *region,
3248              gboolean   flip_x,
3249              gboolean   flip_y)
3250 {
3251   gint n_rectangles;
3252   GdkRectangle *rectangles;
3253   GdkRectangle clipbox;
3254   GdkRegion *new_region;
3255   gint i;
3256
3257   new_region = gdk_region_new ();
3258   
3259   gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
3260   gdk_region_get_clipbox (region, &clipbox);
3261
3262   for (i = 0; i < n_rectangles; ++i)
3263     {
3264       GdkRectangle rect = rectangles[i];
3265
3266       if (flip_y)
3267         rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
3268
3269       if (flip_x)
3270         rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
3271
3272       gdk_region_union_with_rect (new_region, &rect);
3273     }
3274
3275   g_free (rectangles);
3276
3277   return new_region;
3278 }
3279
3280 static void
3281 gtk_menu_set_submenu_navigation_region (GtkMenu          *menu,
3282                                         GtkMenuItem      *menu_item,
3283                                         GdkEventCrossing *event)
3284 {
3285   gint submenu_left = 0;
3286   gint submenu_right = 0;
3287   gint submenu_top = 0;
3288   gint submenu_bottom = 0;
3289   gint width = 0;
3290   gint height = 0;
3291   GdkPoint point[3];
3292   GtkWidget *event_widget;
3293
3294   g_return_if_fail (menu_item->submenu != NULL);
3295   g_return_if_fail (event != NULL);
3296   
3297   event_widget = gtk_get_event_widget ((GdkEvent*) event);
3298   
3299   gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
3300   gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
3301   
3302   submenu_right = submenu_left + width;
3303   submenu_bottom = submenu_top + height;
3304   
3305   gdk_drawable_get_size (event_widget->window, &width, &height);
3306   
3307   if (event->x >= 0 && event->x < width)
3308     {
3309       gint popdown_delay;
3310       gboolean flip_y = FALSE;
3311       gboolean flip_x = FALSE;
3312       
3313       gtk_menu_stop_navigating_submenu (menu);
3314
3315       if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
3316         {
3317           /* right */
3318           point[0].x = event->x_root;
3319           point[1].x = submenu_left;
3320         }
3321       else
3322         {
3323           /* left */
3324           point[0].x = event->x_root + 1;
3325           point[1].x = 2 * (event->x_root + 1) - submenu_right;
3326
3327           flip_x = TRUE;
3328         }
3329
3330       if (event->y < 0)
3331         {
3332           /* top */
3333           point[0].y = event->y_root + 1;
3334           point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
3335
3336           if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
3337             return;
3338
3339           flip_y = TRUE;
3340         }
3341       else
3342         {
3343           /* bottom */
3344           point[0].y = event->y_root;
3345           point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
3346
3347           if (point[0].y >= submenu_bottom)
3348             return;
3349         }
3350
3351       point[2].x = point[1].x;
3352       point[2].y = point[0].y;
3353
3354       menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
3355
3356       if (flip_x || flip_y)
3357         {
3358           GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
3359           gdk_region_destroy (menu->navigation_region);
3360           menu->navigation_region = new_region;
3361         }
3362
3363       g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3364                     "gtk-menu-popdown-delay", &popdown_delay,
3365                     NULL);
3366
3367       menu->navigation_timeout = g_timeout_add (popdown_delay,
3368                                                 gtk_menu_stop_navigating_submenu_cb, menu);
3369
3370 #ifdef DRAW_STAY_UP_TRIANGLE
3371       draw_stay_up_triangle (gdk_get_default_root_window(),
3372                              menu->navigation_region);
3373 #endif
3374     }
3375 }
3376
3377 static void
3378 gtk_menu_deactivate (GtkMenuShell *menu_shell)
3379 {
3380   GtkWidget *parent;
3381   
3382   g_return_if_fail (GTK_IS_MENU (menu_shell));
3383   
3384   parent = menu_shell->parent_menu_shell;
3385   
3386   menu_shell->activate_time = 0;
3387   gtk_menu_popdown (GTK_MENU (menu_shell));
3388   
3389   if (parent)
3390     gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
3391 }
3392
3393 static void
3394 gtk_menu_position (GtkMenu *menu)
3395 {
3396   GtkWidget *widget;
3397   GtkRequisition requisition;
3398   GtkMenuPrivate *private;
3399   gint x, y;
3400   gint scroll_offset;
3401   gint menu_height;
3402   gboolean push_in;
3403   GdkScreen *screen;
3404   GdkScreen *pointer_screen;
3405   GdkRectangle monitor;
3406
3407   g_return_if_fail (GTK_IS_MENU (menu));
3408
3409   widget = GTK_WIDGET (menu);
3410
3411   screen = gtk_widget_get_screen (widget);
3412   gdk_display_get_pointer (gdk_screen_get_display (screen),
3413                            &pointer_screen, &x, &y, NULL);
3414
3415   /* We need the requisition to figure out the right place to
3416    * popup the menu. In fact, we always need to ask here, since
3417    * if a size_request was queued while we weren't popped up,
3418    * the requisition won't have been recomputed yet.
3419    */
3420   gtk_widget_size_request (widget, &requisition);
3421
3422   if (pointer_screen != screen)
3423     {
3424       /* Pointer is on a different screen; roughly center the
3425        * menu on the screen. If someone was using multiscreen
3426        * + Xinerama together they'd probably want something
3427        * fancier; but that is likely to be vanishingly rare.
3428        */
3429       x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
3430       y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
3431     }
3432
3433   private = gtk_menu_get_private (menu);
3434   private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
3435
3436   push_in = FALSE;
3437   
3438   if (menu->position_func)
3439     {
3440       (* menu->position_func) (menu, &x, &y, &push_in, menu->position_func_data);
3441       if (private->monitor_num < 0) 
3442         private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
3443
3444       gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3445     }
3446   else
3447     {
3448       gint space_left, space_right, space_above, space_below;
3449       gint needed_width;
3450       gint needed_height;
3451       gint xthickness = widget->style->xthickness;
3452       gint ythickness = widget->style->ythickness;
3453       gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
3454
3455       /* The placement of popup menus horizontally works like this (with
3456        * RTL in parentheses)
3457        *
3458        * - If there is enough room to the right (left) of the mouse cursor,
3459        *   position the menu there.
3460        * 
3461        * - Otherwise, if if there is enough room to the left (right) of the 
3462        *   mouse cursor, position the menu there.
3463        * 
3464        * - Otherwise if the menu is smaller than the monitor, position it
3465        *   on the side of the mouse cursor that has the most space available
3466        *
3467        * - Otherwise (if there is simply not enough room for the menu on the
3468        *   monitor), position it as far left (right) as possible.
3469        *
3470        * Positioning in the vertical direction is similar: first try below
3471        * mouse cursor, then above.
3472        */
3473       gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3474
3475       space_left = x - monitor.x;
3476       space_right = monitor.x + monitor.width - x - 1;
3477       space_above = y - monitor.y;
3478       space_below = monitor.y + monitor.height - y - 1;
3479
3480       /* position horizontally */
3481
3482       /* the amount of space we need to position the menu. Note the
3483        * menu is offset "xthickness" pixels 
3484        */
3485       needed_width = requisition.width - xthickness;
3486
3487       if (needed_width <= space_left ||
3488           needed_width <= space_right)
3489         {
3490           if ((rtl  && needed_width <= space_left) ||
3491               (!rtl && needed_width >  space_right))
3492             {
3493               /* position left */
3494               x = x + xthickness - requisition.width + 1;
3495             }
3496           else
3497             {
3498               /* position right */
3499               x = x - xthickness;
3500             }
3501
3502           /* x is clamped on-screen further down */
3503         }
3504       else if (requisition.width <= monitor.width)
3505         {
3506           /* the menu is too big to fit on either side of the mouse
3507            * cursor, but smaller than the monitor. Position it on
3508            * the side that has the most space
3509            */
3510           if (space_left > space_right)
3511             {
3512               /* left justify */
3513               x = monitor.x;
3514             }
3515           else
3516             {
3517               /* right justify */
3518               x = monitor.x + monitor.width - requisition.width;
3519             }
3520         }
3521       else /* menu is simply too big for the monitor */
3522         {
3523           if (rtl)
3524             {
3525               /* right justify */
3526               x = monitor.x + monitor.width - requisition.width;
3527             }
3528           else
3529             {
3530               /* left justify */
3531               x = monitor.x;
3532             }
3533         }
3534
3535       /* Position vertically. The algorithm is the same as above, but
3536        * simpler because we don't have to take RTL into account.
3537        */
3538       needed_height = requisition.height - ythickness;
3539
3540       if (needed_height <= space_above ||
3541           needed_height <= space_below)
3542         {
3543           if (needed_height <= space_below)
3544             y = y - ythickness;
3545           else
3546             y = y + ythickness - requisition.height + 1;
3547           
3548           y = CLAMP (y, monitor.y,
3549                      monitor.y + monitor.height - requisition.height);
3550         }
3551       else if (needed_height > space_below && needed_height > space_above)
3552         {
3553           if (space_below >= space_above)
3554             y = monitor.y + monitor.height - requisition.height;
3555           else
3556             y = monitor.y;
3557         }
3558       else
3559         {
3560           y = monitor.y;
3561         }
3562     }
3563
3564   scroll_offset = 0;
3565
3566   if (push_in)
3567     {
3568       menu_height = GTK_WIDGET (menu)->requisition.height;
3569
3570       if (y + menu_height > monitor.y + monitor.height)
3571         {
3572           scroll_offset -= y + menu_height - (monitor.y + monitor.height);
3573           y = (monitor.y + monitor.height) - menu_height;
3574         }
3575   
3576       if (y < monitor.y)
3577         {
3578           scroll_offset += monitor.y - y;
3579           y = monitor.y;
3580         }
3581     }
3582
3583   /* FIXME: should this be done in the various position_funcs ? */
3584   x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
3585  
3586   if (GTK_MENU_SHELL (menu)->active)
3587     {
3588       private->have_position = TRUE;
3589       private->x = x;
3590       private->y = y;
3591     }
3592   
3593   if (y + requisition.height > monitor.y + monitor.height)
3594     requisition.height = (monitor.y + monitor.height) - y;
3595   
3596   if (y < monitor.y)
3597     {
3598       scroll_offset += monitor.y - y;
3599       requisition.height -= monitor.y - y;
3600       y = monitor.y;
3601     }
3602
3603   if (scroll_offset > 0)
3604     scroll_offset += MENU_SCROLL_ARROW_HEIGHT;
3605   
3606   gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window), 
3607                    x, y);
3608
3609   if (!GTK_MENU_SHELL (menu)->active)
3610     {
3611       gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
3612                          requisition.width, requisition.height);
3613     }
3614   
3615   menu->scroll_offset = scroll_offset;
3616 }
3617
3618 static void
3619 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
3620 {
3621   if (menu->timeout_id)
3622     {
3623       g_source_remove (menu->timeout_id);
3624       menu->timeout_id = 0;
3625     }
3626 }
3627
3628 static void
3629 gtk_menu_stop_scrolling (GtkMenu *menu)
3630 {
3631   gtk_menu_remove_scroll_timeout (menu);
3632
3633   menu->upper_arrow_prelight = FALSE;
3634   menu->lower_arrow_prelight = FALSE;
3635 }
3636
3637 static void
3638 gtk_menu_scroll_to (GtkMenu *menu,
3639                     gint    offset)
3640 {
3641   GtkWidget *widget;
3642   gint x, y;
3643   gint view_width, view_height;
3644   gint border_width;
3645   gboolean last_visible;
3646   gint menu_height;
3647   guint vertical_padding;
3648
3649   widget = GTK_WIDGET (menu);
3650
3651   if (menu->tearoff_active &&
3652       menu->tearoff_adjustment &&
3653       (menu->tearoff_adjustment->value != offset))
3654     {
3655       menu->tearoff_adjustment->value =
3656         CLAMP (offset,
3657                0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
3658       gtk_adjustment_value_changed (menu->tearoff_adjustment);
3659     }
3660   
3661   /* Move/resize the viewport according to arrows: */
3662   view_width = widget->allocation.width;
3663   view_height = widget->allocation.height;
3664
3665   gtk_widget_style_get (GTK_WIDGET (menu),
3666                         "vertical-padding", &vertical_padding,
3667                         NULL);
3668   
3669   border_width = GTK_CONTAINER (menu)->border_width;
3670   view_width -= (border_width + widget->style->xthickness) * 2;
3671   view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
3672   menu_height = widget->requisition.height -
3673       (border_width + widget->style->ythickness + vertical_padding) * 2;
3674
3675   x = border_width + widget->style->xthickness;
3676   y = border_width + widget->style->ythickness + vertical_padding;
3677
3678   if (!menu->tearoff_active)
3679     {
3680       last_visible = menu->upper_arrow_visible;
3681       menu->upper_arrow_visible = offset > 0;
3682       
3683       if (menu->upper_arrow_visible)
3684         view_height -= MENU_SCROLL_ARROW_HEIGHT;
3685       
3686       if ( (last_visible != menu->upper_arrow_visible) &&
3687            !menu->upper_arrow_visible)
3688         {
3689           menu->upper_arrow_prelight = FALSE;
3690           
3691           /* If we hid the upper arrow, possibly remove timeout */
3692           if (menu->scroll_step < 0)
3693             {
3694               gtk_menu_stop_scrolling (menu);
3695               gtk_widget_queue_draw (GTK_WIDGET (menu));
3696             }
3697         }
3698
3699       last_visible = menu->lower_arrow_visible;
3700       menu->lower_arrow_visible = offset < menu_height - view_height;
3701       
3702       if (menu->lower_arrow_visible)
3703         view_height -= MENU_SCROLL_ARROW_HEIGHT;
3704       
3705       if ( (last_visible != menu->lower_arrow_visible) &&
3706            !menu->lower_arrow_visible)
3707         {
3708           menu->lower_arrow_prelight = FALSE;
3709           
3710           /* If we hid the lower arrow, possibly remove timeout */
3711           if (menu->scroll_step > 0)
3712             {
3713               gtk_menu_stop_scrolling (menu);
3714               gtk_widget_queue_draw (GTK_WIDGET (menu));
3715             }
3716         }
3717       
3718       if (menu->upper_arrow_visible)
3719         y += MENU_SCROLL_ARROW_HEIGHT;
3720     }
3721
3722   /* Scroll the menu: */
3723   if (GTK_WIDGET_REALIZED (menu))
3724     gdk_window_move (menu->bin_window, 0, -offset);
3725
3726   if (GTK_WIDGET_REALIZED (menu))
3727     gdk_window_move_resize (menu->view_window,
3728                             x,
3729                             y,
3730                             view_width,
3731                             view_height);
3732
3733   menu->scroll_offset = offset;
3734 }
3735
3736 static gboolean
3737 compute_child_offset (GtkMenu   *menu,
3738                       GtkWidget *menu_item,
3739                       gint      *offset,
3740                       gint      *height,
3741                       gboolean  *is_last_child)
3742 {
3743   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
3744   guint item_top_attach;
3745   guint item_bottom_attach;
3746   gint child_offset = 0;
3747   gint i;
3748
3749   get_effective_child_attach (menu_item, NULL, NULL,
3750                               &item_top_attach, &item_bottom_attach);
3751
3752   /* there is a possibility that we get called before _size_request, so
3753    * check the height table for safety.
3754    */
3755   if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
3756     return FALSE;
3757
3758   /* when we have a row with only invisible children, it's height will
3759    * be zero, so there's no need to check WIDGET_VISIBLE here
3760    */
3761   for (i = 0; i < item_top_attach; i++)
3762     child_offset += priv->heights[i];
3763
3764   if (is_last_child)
3765     *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
3766   if (offset)
3767     *offset = child_offset;
3768   if (height)
3769     *height = priv->heights[item_top_attach];
3770
3771   return TRUE;
3772 }
3773
3774 static void
3775 gtk_menu_scroll_item_visible (GtkMenuShell    *menu_shell,
3776                               GtkWidget       *menu_item)
3777 {
3778   GtkMenu *menu;
3779   gint child_offset, child_height;
3780   gint width, height;
3781   gint y;
3782   gint arrow_height;
3783   gboolean last_child = 0;
3784   
3785   menu = GTK_MENU (menu_shell);
3786
3787   /* We need to check if the selected item fully visible.
3788    * If not we need to scroll the menu so that it becomes fully
3789    * visible.
3790    */
3791
3792   if (compute_child_offset (menu, menu_item,
3793                             &child_offset, &child_height, &last_child))
3794     {
3795       guint vertical_padding;
3796       
3797       y = menu->scroll_offset;
3798       gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
3799
3800       gtk_widget_style_get (GTK_WIDGET (menu),
3801                             "vertical-padding", &vertical_padding,
3802                             NULL);
3803                             
3804       height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
3805       
3806       if (child_offset < y)
3807         {
3808           /* Ignore the enter event we might get if the pointer is on the menu
3809            */
3810           menu_shell->ignore_enter = TRUE;
3811           gtk_menu_scroll_to (menu, child_offset);
3812         }
3813       else
3814         {
3815           arrow_height = 0;
3816           if (menu->upper_arrow_visible && !menu->tearoff_active)
3817             arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3818           if (menu->lower_arrow_visible && !menu->tearoff_active)
3819             arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3820           
3821           if (child_offset + child_height > y + height - arrow_height)
3822             {
3823               arrow_height = 0;
3824               if (!last_child && !menu->tearoff_active)
3825                 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3826               
3827               y = child_offset + child_height - height + arrow_height;
3828               if ((y > 0) && !menu->tearoff_active)
3829                 {
3830                   /* Need upper arrow */
3831                   arrow_height += MENU_SCROLL_ARROW_HEIGHT;
3832                   y = child_offset + child_height - height + arrow_height;
3833                 }
3834               /* Ignore the enter event we might get if the pointer is on the menu
3835                */
3836               menu_shell->ignore_enter = TRUE;
3837               gtk_menu_scroll_to (menu, y);
3838             }
3839         }    
3840       
3841     }
3842 }
3843
3844 static void
3845 gtk_menu_select_item (GtkMenuShell  *menu_shell,
3846                       GtkWidget     *menu_item)
3847 {
3848   GtkMenu *menu = GTK_MENU (menu_shell);
3849
3850   if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
3851     gtk_menu_scroll_item_visible (menu_shell, menu_item);
3852
3853   GTK_MENU_SHELL_CLASS (parent_class)->select_item (menu_shell, menu_item);
3854 }
3855
3856
3857 /* Reparent the menu, taking care of the refcounting
3858  *
3859  * If unrealize is true we force a unrealize while reparenting the parent.
3860  * This can help eliminate flicker in some cases.
3861  *
3862  * What happens is that when the menu is unrealized and then re-realized,
3863  * the allocations are as follows:
3864  *
3865  *  parent - 1x1 at (0,0) 
3866  *  child1 - 100x20 at (0,0)
3867  *  child2 - 100x20 at (0,20)
3868  *  child3 - 100x20 at (0,40)
3869  *
3870  * That is, the parent is small but the children are full sized. Then,
3871  * when the queued_resize gets processed, the parent gets resized to
3872  * full size. 
3873  *
3874  * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
3875  * contains the following logic:
3876  * 
3877  * - if a move or resize operation on a window would change the clip 
3878  *   region on the children, then before the window is resized
3879  *   the background for children is temporarily set to None, the
3880  *   move/resize done, and the background for the children restored.
3881  *
3882  * So, at the point where the parent is resized to final size, the
3883  * background for the children is temporarily None, and thus they
3884  * are not cleared to the background color and the previous background
3885  * (the image of the menu) is left in place.
3886  */
3887 static void 
3888 gtk_menu_reparent (GtkMenu      *menu, 
3889                    GtkWidget    *new_parent, 
3890                    gboolean      unrealize)
3891 {
3892   GtkObject *object = GTK_OBJECT (menu);
3893   GtkWidget *widget = GTK_WIDGET (menu);
3894   gboolean was_floating = GTK_OBJECT_FLOATING (object);
3895
3896   g_object_ref (object);
3897   gtk_object_sink (object);
3898
3899   if (unrealize)
3900     {
3901       g_object_ref (object);
3902       gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
3903       gtk_container_add (GTK_CONTAINER (new_parent), widget);
3904       g_object_unref (object);
3905     }
3906   else
3907     gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
3908   
3909   if (was_floating)
3910     GTK_OBJECT_SET_FLAGS (object, GTK_FLOATING);
3911   else
3912     g_object_unref (object);
3913 }
3914
3915 static void
3916 gtk_menu_show_all (GtkWidget *widget)
3917 {
3918   /* Show children, but not self. */
3919   gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
3920 }
3921
3922
3923 static void
3924 gtk_menu_hide_all (GtkWidget *widget)
3925 {
3926   /* Hide children, but not self. */
3927   gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
3928 }
3929
3930 /**
3931  * gtk_menu_set_screen:
3932  * @menu: a #GtkMenu.
3933  * @screen: a #GdkScreen, or %NULL if the screen should be
3934  *          determined by the widget the menu is attached to.
3935  *
3936  * Sets the #GdkScreen on which the menu will be displayed.
3937  * 
3938  * Since: 2.2
3939  **/
3940 void
3941 gtk_menu_set_screen (GtkMenu   *menu, 
3942                      GdkScreen *screen)
3943 {
3944   g_return_if_fail (GTK_IS_MENU (menu));
3945   g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
3946
3947   g_object_set_data (G_OBJECT (menu), "gtk-menu-explicit-screen", screen);
3948
3949   if (screen)
3950     {
3951       menu_change_screen (menu, screen);
3952     }
3953   else
3954     {
3955       GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
3956       if (attach_widget)
3957         attach_widget_screen_changed (attach_widget, NULL, menu);
3958     }
3959 }
3960
3961 /**
3962  * gtk_menu_attach:
3963  * @menu: a #GtkMenu.
3964  * @child: a #GtkMenuItem.
3965  * @left_attach: The column number to attach the left side of the item to.
3966  * @right_attach: The column number to attach the right side of the item to.
3967  * @top_attach: The row number to attach the top of the item to.
3968  * @bottom_attach: The row number to attach the bottom of the item to.
3969  *
3970  * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
3971  * an item will occupy is specified by @left_attach, @right_attach,
3972  * @top_attach and @bottom_attach. These each represent the leftmost,
3973  * rightmost, uppermost and lower column and row numbers of the table.
3974  * (Columns and rows are indexed from zero).
3975  *
3976  * Note that this function is not related to gtk_menu_detach().
3977  *
3978  * Since: 2.4
3979  **/
3980 void
3981 gtk_menu_attach (GtkMenu   *menu,
3982                  GtkWidget *child,
3983                  guint      left_attach,
3984                  guint      right_attach,
3985                  guint      top_attach,
3986                  guint      bottom_attach)
3987 {
3988   GtkMenuShell *menu_shell;
3989   
3990   g_return_if_fail (GTK_IS_MENU (menu));
3991   g_return_if_fail (GTK_IS_MENU_ITEM (child));
3992   g_return_if_fail (child->parent == NULL || 
3993                     child->parent == GTK_WIDGET (menu));
3994   g_return_if_fail (left_attach < right_attach);
3995   g_return_if_fail (top_attach < bottom_attach);
3996
3997   menu_shell = GTK_MENU_SHELL (menu);
3998   
3999   if (!child->parent)
4000     {
4001       AttachInfo *ai = get_attach_info (child);
4002       
4003       ai->left_attach = left_attach;
4004       ai->right_attach = right_attach;
4005       ai->top_attach = top_attach;
4006       ai->bottom_attach = bottom_attach;
4007       
4008       menu_shell->children = g_list_append (menu_shell->children, child);
4009
4010       gtk_widget_set_parent (child, GTK_WIDGET (menu));
4011
4012       menu_queue_resize (menu);
4013     }
4014   else
4015     {
4016       gtk_container_child_set (GTK_CONTAINER (child->parent), child,
4017                                "left-attach",   left_attach,
4018                                "right-attach",  right_attach,
4019                                "top-attach",    top_attach,
4020                                "bottom-attach", bottom_attach,
4021                                NULL);
4022     }
4023 }
4024
4025 static gint
4026 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
4027 {
4028   gint popup_delay;
4029
4030   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
4031                 "gtk-menu-popup-delay", &popup_delay,
4032                 NULL);
4033
4034   return popup_delay;
4035 }
4036
4037 static GtkWidget *
4038 find_child_containing (GtkMenuShell *menu_shell,
4039                        int           left,
4040                        int           right,
4041                        int           top,
4042                        int           bottom)
4043 {
4044   GList *list;
4045
4046   /* find a child which includes the area given by
4047    * left, right, top, bottom.
4048    */
4049
4050   for (list = menu_shell->children; list; list = list->next)
4051     {
4052       guint l, r, t, b;
4053
4054       if (!_gtk_menu_item_is_selectable (list->data))
4055         continue;
4056
4057       get_effective_child_attach (list->data, &l, &r, &t, &b);
4058
4059       if (l <= left && right <= r
4060           && t <= top && bottom <= b)
4061         return GTK_WIDGET (list->data);
4062     }
4063
4064   return NULL;
4065 }
4066
4067 static void
4068 gtk_menu_move_current (GtkMenuShell *menu_shell,
4069                        GtkMenuDirectionType direction)
4070 {
4071   GtkMenu *menu = GTK_MENU (menu_shell);
4072   gint i;
4073   guint l, r, t, b;
4074   GtkWidget *match = NULL;
4075
4076   if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
4077     {
4078       switch (direction)
4079         {
4080         case GTK_MENU_DIR_CHILD:
4081           direction = GTK_MENU_DIR_PARENT;
4082           break;
4083         case GTK_MENU_DIR_PARENT:
4084           direction = GTK_MENU_DIR_CHILD;
4085           break;
4086         default: ;
4087         }
4088     }
4089
4090   /* use special table menu key bindings */
4091   if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
4092     {
4093       get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
4094
4095       if (direction == GTK_MENU_DIR_NEXT)
4096         {
4097           for (i = b; i < gtk_menu_get_n_rows (menu); i++)
4098             {
4099               match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
4100               if (match)
4101                 break;
4102             }
4103
4104           if (!match)
4105             {
4106               /* wrap around */
4107               for (i = 0; i < t; i++)
4108                 {
4109                   match = find_child_containing (menu_shell,
4110                                                  l, l + 1, i, i + 1);
4111                   if (match)
4112                     break;
4113                 }
4114             }
4115         }
4116       else if (direction == GTK_MENU_DIR_PREV)
4117         {
4118           for (i = t; i > 0; i--)
4119             {
4120               match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
4121               if (match)
4122                 break;
4123             }
4124
4125           if (!match)
4126             {
4127               /* wrap around */
4128               for (i = gtk_menu_get_n_rows (menu); i > b; i--)
4129                 {
4130                   match = find_child_containing (menu_shell,
4131                                                  l, l + 1, i - 1, i);
4132                   if (match)
4133                     break;
4134                 }
4135             }
4136         }
4137       else if (direction == GTK_MENU_DIR_PARENT)
4138         {
4139           /* we go one left if possible */
4140           if (l > 0)
4141             match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
4142
4143           if (!match)
4144             {
4145               GtkWidget *parent = menu_shell->parent_menu_shell;
4146
4147               if (!parent
4148                   || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
4149                 match = menu_shell->active_menu_item;
4150             }
4151         }
4152       else if (direction == GTK_MENU_DIR_CHILD)
4153         {
4154           /* we go one right if possible */
4155           if (r < gtk_menu_get_n_columns (menu))
4156             match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
4157
4158           if (!match)
4159             {
4160               GtkWidget *parent = menu_shell->parent_menu_shell;
4161
4162               if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
4163                   (!parent ||
4164                    g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
4165                 match = menu_shell->active_menu_item;
4166             }
4167         }
4168
4169       if (match)
4170         {
4171           gtk_menu_shell_select_item (menu_shell, match);
4172           return;
4173         }
4174     }
4175
4176   GTK_MENU_SHELL_CLASS (parent_class)->move_current (menu_shell, direction);
4177 }
4178
4179 static gint
4180 get_visible_size (GtkMenu *menu)
4181 {
4182   GtkWidget *widget = GTK_WIDGET (menu);
4183   GtkContainer *container = GTK_CONTAINER (menu);
4184   
4185   gint menu_height = (widget->allocation.height
4186                       - 2 * (container->border_width
4187                              + widget->style->ythickness));
4188   
4189   if (menu->upper_arrow_visible && !menu->tearoff_active)
4190     menu_height -= MENU_SCROLL_ARROW_HEIGHT;
4191   if (menu->lower_arrow_visible && !menu->tearoff_active)
4192     menu_height -= MENU_SCROLL_ARROW_HEIGHT;
4193
4194   return menu_height;
4195 }
4196
4197 /* Find the sensitive on-screen child containing @y, or if none,
4198  * the nearest selectable onscreen child. (%NULL if none)
4199  */
4200 static GtkWidget *
4201 child_at (GtkMenu *menu,
4202           gint     y)
4203 {
4204   GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4205   GtkWidget *child = NULL;
4206   gint child_offset = 0;
4207   GList *children;
4208   gint menu_height;
4209   gint lower, upper;            /* Onscreen bounds */
4210
4211   menu_height = get_visible_size (menu);
4212   lower = menu->scroll_offset;
4213   upper = menu->scroll_offset + menu_height;
4214   
4215   for (children = menu_shell->children; children; children = children->next)
4216     {
4217       if (GTK_WIDGET_VISIBLE (children->data))
4218         {
4219           GtkRequisition child_requisition;
4220
4221           gtk_widget_size_request (children->data, &child_requisition);
4222
4223           if (_gtk_menu_item_is_selectable (children->data) &&
4224               child_offset >= lower &&
4225               child_offset + child_requisition.height <= upper)
4226             {
4227               child = children->data;
4228               
4229               if (child_offset + child_requisition.height > y &&
4230                   !GTK_IS_TEAROFF_MENU_ITEM (child))
4231                 return child;
4232             }
4233       
4234           child_offset += child_requisition.height;
4235         }
4236     }
4237
4238   return child;
4239 }
4240
4241 static gint
4242 get_menu_height (GtkMenu *menu)
4243 {
4244   gint height;
4245   GtkWidget *widget = GTK_WIDGET (menu);
4246
4247   height = widget->requisition.height;
4248   height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
4249
4250   if (menu->upper_arrow_visible && !menu->tearoff_active)
4251     height -= MENU_SCROLL_ARROW_HEIGHT;
4252
4253   if (menu->lower_arrow_visible && !menu->tearoff_active)
4254     height -= MENU_SCROLL_ARROW_HEIGHT;
4255
4256   return height;
4257 }
4258
4259 static void
4260 gtk_menu_real_move_scroll (GtkMenu       *menu,
4261                            GtkScrollType  type)
4262 {
4263   gint page_size = get_visible_size (menu);
4264   gint end_position = get_menu_height (menu);
4265   GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4266   
4267   switch (type)
4268     {
4269     case GTK_SCROLL_PAGE_UP:
4270     case GTK_SCROLL_PAGE_DOWN:
4271       {
4272         gint old_offset;
4273         gint new_offset;
4274         gint child_offset = 0;
4275         gboolean old_upper_arrow_visible;
4276         gint step;
4277
4278         if (type == GTK_SCROLL_PAGE_UP)
4279           step = - page_size;
4280         else
4281           step = page_size;
4282
4283         if (menu_shell->active_menu_item)
4284           {
4285             gint child_height;
4286             
4287             compute_child_offset (menu, menu_shell->active_menu_item,
4288                                   &child_offset, &child_height, NULL);
4289             child_offset += child_height / 2;
4290           }
4291
4292         menu_shell->ignore_enter = TRUE;
4293         old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4294         old_offset = menu->scroll_offset;
4295
4296         new_offset = menu->scroll_offset + step;
4297         new_offset = CLAMP (new_offset, 0, end_position - page_size);
4298
4299         gtk_menu_scroll_to (menu, new_offset);
4300         
4301         if (menu_shell->active_menu_item)
4302           {
4303             GtkWidget *new_child;
4304             gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
4305
4306             if (menu->scroll_offset != old_offset)
4307               step = menu->scroll_offset - old_offset;
4308
4309             step -= (new_upper_arrow_visible - old_upper_arrow_visible) * MENU_SCROLL_ARROW_HEIGHT;
4310
4311             new_child = child_at (menu, child_offset + step);
4312             if (new_child)
4313               gtk_menu_shell_select_item (menu_shell, new_child);
4314           }
4315       }
4316       break;
4317     case GTK_SCROLL_START:
4318       /* Ignore the enter event we might get if the pointer is on the menu
4319        */
4320       menu_shell->ignore_enter = TRUE;
4321       gtk_menu_scroll_to (menu, 0);
4322       gtk_menu_shell_select_first (menu_shell, TRUE);
4323       break;
4324     case GTK_SCROLL_END:
4325       /* Ignore the enter event we might get if the pointer is on the menu
4326        */
4327       menu_shell->ignore_enter = TRUE;
4328       gtk_menu_scroll_to (menu, end_position - page_size);
4329       _gtk_menu_shell_select_last (menu_shell, TRUE);
4330       break;
4331     default:
4332       break;
4333     }
4334 }
4335
4336
4337 /**
4338  * gtk_menu_set_monitor:
4339  * @menu: a #GtkMenu
4340  * @monitor_num: the number of the monitor on which the menu should
4341  *    be popped up
4342  * 
4343  * Informs GTK+ on which monitor a menu should be popped up. 
4344  * See gdk_screen_get_monitor_geometry().
4345  *
4346  * This function should be called from a #GtkMenuPositionFunc if the
4347  * menu should not appear on the same monitor as the pointer. This 
4348  * information can't be reliably inferred from the coordinates returned
4349  * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates 
4350  * may extend beyond the monitor boundaries or even the screen boundaries. 
4351  *
4352  * Since: 2.4
4353  **/
4354 void
4355 gtk_menu_set_monitor (GtkMenu *menu,
4356                       gint     monitor_num)
4357 {
4358   GtkMenuPrivate *priv;
4359   g_return_if_fail (GTK_IS_MENU (menu));
4360
4361   priv = gtk_menu_get_private (menu);
4362   
4363   priv->monitor_num = monitor_num;
4364 }
4365
4366 /**
4367  * gtk_menu_get_for_attach_widget:
4368  * @widget: a #GtkWidget
4369  *
4370  * Returns a list of the menus which are attached to this widget.
4371  * This list is owned by GTK+ and must not be modified.
4372  *
4373  * Return value: the list of menus attached to his widget.
4374  *
4375  * Since: 2.6
4376  **/
4377 GList*
4378 gtk_menu_get_for_attach_widget (GtkWidget *widget)
4379 {
4380   GList *list;
4381   
4382   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
4383   
4384   list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
4385   return list;
4386 }
4387
4388 #define __GTK_MENU_C__
4389 #include "gtkaliasdef.c"