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