]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenu.c
Fix more SizeRequest implementations to avoid recursive calls to wrapper API
[~andy/gtk] / gtk / gtkmenu.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
25  */
26
27 #include "config.h"
28 #include <string.h>
29 #include "gdk/gdkkeysyms.h"
30 #include "gtkaccellabel.h"
31 #include "gtkaccelmap.h"
32 #include "gtkbindings.h"
33 #include "gtkcheckmenuitem.h"
34 #include  <gobject/gvaluecollector.h>
35 #include "gtkmain.h"
36 #include "gtkmarshalers.h"
37 #include "gtkmenu.h"
38 #include "gtkmenuprivate.h"
39 #include "gtktearoffmenuitem.h"
40 #include "gtkwindow.h"
41 #include "gtkhbox.h"
42 #include "gtkvscrollbar.h"
43 #include "gtksettings.h"
44 #include "gtksizerequest.h"
45 #include "gtkprivate.h"
46 #include "gtkintl.h"
47
48 #define DEFAULT_POPUP_DELAY     225
49 #define DEFAULT_POPDOWN_DELAY  1000
50
51 #define NAVIGATION_REGION_OVERSHOOT 50  /* How much the navigation region
52                                          * extends below the submenu
53                                          */
54
55 #define MENU_SCROLL_STEP1      8
56 #define MENU_SCROLL_STEP2     15
57 #define MENU_SCROLL_FAST_ZONE  8
58 #define MENU_SCROLL_TIMEOUT1  50
59 #define MENU_SCROLL_TIMEOUT2  20
60
61 #define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
62 #define ATTACHED_MENUS "gtk-attached-menus"
63
64 typedef struct _GtkMenuAttachData       GtkMenuAttachData;
65 typedef struct _GtkMenuPrivate          GtkMenuPrivate;
66 typedef struct _GtkMenuPopdownData      GtkMenuPopdownData;
67
68 struct _GtkMenuAttachData
69 {
70   GtkWidget *attach_widget;
71   GtkMenuDetachFunc detacher;
72 };
73
74 struct _GtkMenuPrivate 
75 {
76   gint x;
77   gint y;
78   gboolean initially_pushed_in;
79
80   /* info used for the table */
81   guint *heights;
82   gint heights_length;
83   gint requested_height;
84
85   gint monitor_num;
86
87   /* Cached layout information */
88   gint n_rows;
89   gint n_columns;
90
91   guint accel_size;
92
93   gchar *title;
94
95   /* Arrow states */
96   GtkStateType lower_arrow_state;
97   GtkStateType upper_arrow_state;
98
99   /* navigation region */
100   int navigation_x;
101   int navigation_y;
102   int navigation_width;
103   int navigation_height;
104
105   guint have_layout           : 1;
106   guint seen_item_enter       : 1;
107   guint have_position         : 1;
108   guint ignore_button_release : 1;
109   guint no_toggle_size        : 1;
110 };
111
112 struct _GtkMenuPopdownData
113 {
114   GtkMenu *menu;
115   GdkDevice *device;
116 };
117
118 typedef struct
119 {
120   gint left_attach;
121   gint right_attach;
122   gint top_attach;
123   gint bottom_attach;
124   gint effective_left_attach;
125   gint effective_right_attach;
126   gint effective_top_attach;
127   gint effective_bottom_attach;
128 } AttachInfo;
129
130 enum {
131   MOVE_SCROLL,
132   LAST_SIGNAL
133 };
134
135 enum {
136   PROP_0,
137   PROP_ACTIVE,
138   PROP_ACCEL_GROUP,
139   PROP_ACCEL_PATH,
140   PROP_ATTACH_WIDGET,
141   PROP_TEAROFF_STATE,
142   PROP_TEAROFF_TITLE,
143   PROP_MONITOR,
144   PROP_RESERVE_TOGGLE_SIZE
145 };
146
147 enum {
148   CHILD_PROP_0,
149   CHILD_PROP_LEFT_ATTACH,
150   CHILD_PROP_RIGHT_ATTACH,
151   CHILD_PROP_TOP_ATTACH,
152   CHILD_PROP_BOTTOM_ATTACH
153 };
154
155 static void     gtk_menu_set_property      (GObject          *object,
156                                             guint             prop_id,
157                                             const GValue     *value,
158                                             GParamSpec       *pspec);
159 static void     gtk_menu_get_property      (GObject          *object,
160                                             guint             prop_id,
161                                             GValue           *value,
162                                             GParamSpec       *pspec);
163 static void     gtk_menu_set_child_property(GtkContainer     *container,
164                                             GtkWidget        *child,
165                                             guint             property_id,
166                                             const GValue     *value,
167                                             GParamSpec       *pspec);
168 static void     gtk_menu_get_child_property(GtkContainer     *container,
169                                             GtkWidget        *child,
170                                             guint             property_id,
171                                             GValue           *value,
172                                             GParamSpec       *pspec);
173 static void     gtk_menu_destroy           (GtkObject        *object);
174 static void     gtk_menu_realize           (GtkWidget        *widget);
175 static void     gtk_menu_unrealize         (GtkWidget        *widget);
176 static void     gtk_menu_size_allocate     (GtkWidget        *widget,
177                                             GtkAllocation    *allocation);
178 static void     gtk_menu_paint             (GtkWidget        *widget,
179                                             GdkEventExpose   *expose);
180 static void     gtk_menu_show              (GtkWidget        *widget);
181 static gboolean gtk_menu_expose            (GtkWidget        *widget,
182                                             GdkEventExpose   *event);
183 static gboolean gtk_menu_key_press         (GtkWidget        *widget,
184                                             GdkEventKey      *event);
185 static gboolean gtk_menu_scroll            (GtkWidget        *widget,
186                                             GdkEventScroll   *event);
187 static gboolean gtk_menu_button_press      (GtkWidget        *widget,
188                                             GdkEventButton   *event);
189 static gboolean gtk_menu_button_release    (GtkWidget        *widget,
190                                             GdkEventButton   *event);
191 static gboolean gtk_menu_motion_notify     (GtkWidget        *widget,
192                                             GdkEventMotion   *event);
193 static gboolean gtk_menu_enter_notify      (GtkWidget        *widget,
194                                             GdkEventCrossing *event);
195 static gboolean gtk_menu_leave_notify      (GtkWidget        *widget,
196                                             GdkEventCrossing *event);
197 static void     gtk_menu_scroll_to         (GtkMenu          *menu,
198                                             gint              offset);
199 static void     gtk_menu_grab_notify       (GtkWidget        *widget,
200                                             gboolean          was_grabbed);
201
202 static void     gtk_menu_stop_scrolling         (GtkMenu  *menu);
203 static void     gtk_menu_remove_scroll_timeout  (GtkMenu  *menu);
204 static gboolean gtk_menu_scroll_timeout         (gpointer  data);
205 static gboolean gtk_menu_scroll_timeout_initial (gpointer  data);
206 static void     gtk_menu_start_scrolling        (GtkMenu  *menu);
207
208 static void     gtk_menu_scroll_item_visible (GtkMenuShell    *menu_shell,
209                                               GtkWidget       *menu_item);
210 static void     gtk_menu_select_item       (GtkMenuShell     *menu_shell,
211                                             GtkWidget        *menu_item);
212 static void     gtk_menu_real_insert       (GtkMenuShell     *menu_shell,
213                                             GtkWidget        *child,
214                                             gint              position);
215 static void     gtk_menu_scrollbar_changed (GtkAdjustment    *adjustment,
216                                             GtkMenu          *menu);
217 static void     gtk_menu_handle_scrolling  (GtkMenu          *menu,
218                                             gint              event_x,
219                                             gint              event_y,
220                                             gboolean          enter,
221                                             gboolean          motion);
222 static void     gtk_menu_set_tearoff_hints (GtkMenu          *menu,
223                                             gint             width);
224 static void     gtk_menu_style_set         (GtkWidget        *widget,
225                                             GtkStyle         *previous_style);
226 static gboolean gtk_menu_focus             (GtkWidget        *widget,
227                                             GtkDirectionType direction);
228 static gint     gtk_menu_get_popup_delay   (GtkMenuShell     *menu_shell);
229 static void     gtk_menu_move_current      (GtkMenuShell     *menu_shell,
230                                             GtkMenuDirectionType direction);
231 static void     gtk_menu_real_move_scroll  (GtkMenu          *menu,
232                                             GtkScrollType     type);
233
234 static void     gtk_menu_stop_navigating_submenu       (GtkMenu          *menu);
235 static gboolean gtk_menu_stop_navigating_submenu_cb    (gpointer          user_data);
236 static gboolean gtk_menu_navigating_submenu            (GtkMenu          *menu,
237                                                         gint              event_x,
238                                                         gint              event_y);
239 static void     gtk_menu_set_submenu_navigation_region (GtkMenu          *menu,
240                                                         GtkMenuItem      *menu_item,
241                                                         GdkEventCrossing *event);
242  
243 static void gtk_menu_deactivate     (GtkMenuShell      *menu_shell);
244 static void gtk_menu_show_all       (GtkWidget         *widget);
245 static void gtk_menu_hide_all       (GtkWidget         *widget);
246 static void gtk_menu_position       (GtkMenu           *menu);
247 static void gtk_menu_reparent       (GtkMenu           *menu, 
248                                      GtkWidget         *new_parent, 
249                                      gboolean           unrealize);
250 static void gtk_menu_remove         (GtkContainer      *menu,
251                                      GtkWidget         *widget);
252
253 static void gtk_menu_update_title   (GtkMenu           *menu);
254
255 static void       menu_grab_transfer_window_destroy (GtkMenu *menu);
256 static GdkWindow *menu_grab_transfer_window_get     (GtkMenu *menu);
257
258 static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
259                                                   guint      signal_id);
260 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
261                                            gboolean group_changed);
262
263 static void gtk_menu_size_request_init          (GtkSizeRequestIface *iface);
264 static void gtk_menu_get_width                  (GtkSizeRequest      *widget,
265                                                  gint                *minimum_size,
266                                                  gint                *natural_size);
267 static void gtk_menu_get_height                 (GtkSizeRequest      *widget,
268                                                  gint                *minimum_size,
269                                                  gint                *natural_size);
270 static void gtk_menu_get_height_for_width       (GtkSizeRequest      *widget,
271                                                  gint                 for_size,
272                                                  gint                *minimum_size,
273                                                  gint                *natural_size);
274
275
276 static const gchar attach_data_key[] = "gtk-menu-attach-data";
277
278 static guint menu_signals[LAST_SIGNAL] = { 0 };
279
280 static GtkMenuPrivate *
281 gtk_menu_get_private (GtkMenu *menu)
282 {
283   return G_TYPE_INSTANCE_GET_PRIVATE (menu, GTK_TYPE_MENU, GtkMenuPrivate);
284 }
285
286 G_DEFINE_TYPE_WITH_CODE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL,
287                          G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
288                                                 gtk_menu_size_request_init))
289
290 static void
291 menu_queue_resize (GtkMenu *menu)
292 {
293   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
294
295   priv->have_layout = FALSE;
296   gtk_widget_queue_resize (GTK_WIDGET (menu));
297 }
298
299 static void
300 attach_info_free (AttachInfo *info)
301 {
302   g_slice_free (AttachInfo, info);
303 }
304
305 static AttachInfo *
306 get_attach_info (GtkWidget *child)
307 {
308   GObject *object = G_OBJECT (child);
309   AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
310
311   if (!ai)
312     {
313       ai = g_slice_new0 (AttachInfo);
314       g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
315                               (GDestroyNotify) attach_info_free);
316     }
317
318   return ai;
319 }
320
321 static gboolean
322 is_grid_attached (AttachInfo *ai)
323 {
324   return (ai->left_attach >= 0 &&
325           ai->right_attach >= 0 &&
326           ai->top_attach >= 0 &&
327           ai->bottom_attach >= 0);
328 }
329
330 static void
331 menu_ensure_layout (GtkMenu *menu)
332 {
333   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
334
335   if (!priv->have_layout)
336     {
337       GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
338       GList *l;
339       gchar *row_occupied;
340       gint current_row;
341       gint max_right_attach;      
342       gint max_bottom_attach;
343
344       /* Find extents of gridded portion
345        */
346       max_right_attach = 1;
347       max_bottom_attach = 0;
348
349       for (l = menu_shell->children; l; l = l->next)
350         {
351           GtkWidget *child = l->data;
352           AttachInfo *ai = get_attach_info (child);
353
354           if (is_grid_attached (ai))
355             {
356               max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
357               max_right_attach = MAX (max_right_attach, ai->right_attach);
358             }
359         }
360          
361       /* Find empty rows
362        */
363       row_occupied = g_malloc0 (max_bottom_attach);
364
365       for (l = menu_shell->children; l; l = l->next)
366         {
367           GtkWidget *child = l->data;
368           AttachInfo *ai = get_attach_info (child);
369
370           if (is_grid_attached (ai))
371             {
372               gint i;
373
374               for (i = ai->top_attach; i < ai->bottom_attach; i++)
375                 row_occupied[i] = TRUE;
376             }
377         }
378
379       /* Lay non-grid-items out in those rows
380        */
381       current_row = 0;
382       for (l = menu_shell->children; l; l = l->next)
383         {
384           GtkWidget *child = l->data;
385           AttachInfo *ai = get_attach_info (child);
386
387           if (!is_grid_attached (ai))
388             {
389               while (current_row < max_bottom_attach && row_occupied[current_row])
390                 current_row++;
391                 
392               ai->effective_left_attach = 0;
393               ai->effective_right_attach = max_right_attach;
394               ai->effective_top_attach = current_row;
395               ai->effective_bottom_attach = current_row + 1;
396
397               current_row++;
398             }
399           else
400             {
401               ai->effective_left_attach = ai->left_attach;
402               ai->effective_right_attach = ai->right_attach;
403               ai->effective_top_attach = ai->top_attach;
404               ai->effective_bottom_attach = ai->bottom_attach;
405             }
406         }
407
408       g_free (row_occupied);
409
410       priv->n_rows = MAX (current_row, max_bottom_attach);
411       priv->n_columns = max_right_attach;
412       priv->have_layout = TRUE;
413     }
414 }
415
416
417 static gint
418 gtk_menu_get_n_columns (GtkMenu *menu)
419 {
420   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
421
422   menu_ensure_layout (menu);
423
424   return priv->n_columns;
425 }
426
427 static gint
428 gtk_menu_get_n_rows (GtkMenu *menu)
429 {
430   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
431
432   menu_ensure_layout (menu);
433
434   return priv->n_rows;
435 }
436
437 static void
438 get_effective_child_attach (GtkWidget *child,
439                             int       *l,
440                             int       *r,
441                             int       *t,
442                             int       *b)
443 {
444   GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
445   AttachInfo *ai;
446   
447   menu_ensure_layout (menu);
448
449   ai = get_attach_info (child);
450
451   if (l)
452     *l = ai->effective_left_attach;
453   if (r)
454     *r = ai->effective_right_attach;
455   if (t)
456     *t = ai->effective_top_attach;
457   if (b)
458     *b = ai->effective_bottom_attach;
459
460 }
461
462 static void
463 gtk_menu_class_init (GtkMenuClass *class)
464 {
465   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
466   GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
467   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
468   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
469   GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
470   GtkBindingSet *binding_set;
471   
472   gobject_class->set_property = gtk_menu_set_property;
473   gobject_class->get_property = gtk_menu_get_property;
474
475   object_class->destroy = gtk_menu_destroy;
476   
477   widget_class->realize = gtk_menu_realize;
478   widget_class->unrealize = gtk_menu_unrealize;
479   widget_class->size_allocate = gtk_menu_size_allocate;
480   widget_class->show = gtk_menu_show;
481   widget_class->expose_event = gtk_menu_expose;
482   widget_class->scroll_event = gtk_menu_scroll;
483   widget_class->key_press_event = gtk_menu_key_press;
484   widget_class->button_press_event = gtk_menu_button_press;
485   widget_class->button_release_event = gtk_menu_button_release;
486   widget_class->motion_notify_event = gtk_menu_motion_notify;
487   widget_class->show_all = gtk_menu_show_all;
488   widget_class->hide_all = gtk_menu_hide_all;
489   widget_class->enter_notify_event = gtk_menu_enter_notify;
490   widget_class->leave_notify_event = gtk_menu_leave_notify;
491   widget_class->style_set = gtk_menu_style_set;
492   widget_class->focus = gtk_menu_focus;
493   widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
494   widget_class->grab_notify = gtk_menu_grab_notify;
495
496   container_class->remove = gtk_menu_remove;
497   container_class->get_child_property = gtk_menu_get_child_property;
498   container_class->set_child_property = gtk_menu_set_child_property;
499   
500   menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
501   menu_shell_class->deactivate = gtk_menu_deactivate;
502   menu_shell_class->select_item = gtk_menu_select_item;
503   menu_shell_class->insert = gtk_menu_real_insert;
504   menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
505   menu_shell_class->move_current = gtk_menu_move_current;
506
507   menu_signals[MOVE_SCROLL] =
508     g_signal_new_class_handler (I_("move-scroll"),
509                                 G_OBJECT_CLASS_TYPE (object_class),
510                                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
511                                 G_CALLBACK (gtk_menu_real_move_scroll),
512                                 NULL, NULL,
513                                 _gtk_marshal_VOID__ENUM,
514                                 G_TYPE_NONE, 1,
515                                 GTK_TYPE_SCROLL_TYPE);
516
517   /**
518    * GtkMenu:active:
519    *
520    * The index of the currently selected menu item, or -1 if no
521    * menu item is selected.
522    *
523    * Since: 2.14
524    **/
525   g_object_class_install_property (gobject_class,
526                                    PROP_ACTIVE,
527                                    g_param_spec_int ("active",
528                                                      P_("Active"),
529                                                      P_("The currently selected menu item"),
530                                                      -1, G_MAXINT, -1,
531                                                      GTK_PARAM_READWRITE));
532
533   /**
534    * GtkMenu:accel-group:
535    *
536    * The accel group holding accelerators for the menu.
537    *
538    * Since: 2.14
539    **/
540   g_object_class_install_property (gobject_class,
541                                    PROP_ACCEL_GROUP,
542                                    g_param_spec_object ("accel-group",
543                                                         P_("Accel Group"),
544                                                         P_("The accel group holding accelerators for the menu"),
545                                                         GTK_TYPE_ACCEL_GROUP,
546                                                         GTK_PARAM_READWRITE));
547
548   /**
549    * GtkMenu:accel-path:
550    *
551    * An accel path used to conveniently construct accel paths of child items.
552    *
553    * Since: 2.14
554    **/
555   g_object_class_install_property (gobject_class,
556                                    PROP_ACCEL_PATH,
557                                    g_param_spec_string ("accel-path",
558                                                         P_("Accel Path"),
559                                                         P_("An accel path used to conveniently construct accel paths of child items"),
560                                                         NULL,
561                                                         GTK_PARAM_READWRITE));
562
563   /**
564    * GtkMenu:attach-widget:
565    *
566    * The widget the menu is attached to. Setting this property attaches
567    * the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
568    * use gtk_menu_attach_to_widget() directly.
569    *
570    * Since: 2.14
571    **/
572   g_object_class_install_property (gobject_class,
573                                    PROP_ATTACH_WIDGET,
574                                    g_param_spec_object ("attach-widget",
575                                                         P_("Attach Widget"),
576                                                         P_("The widget the menu is attached to"),
577                                                         GTK_TYPE_WIDGET,
578                                                         GTK_PARAM_READWRITE));
579
580   g_object_class_install_property (gobject_class,
581                                    PROP_TEAROFF_TITLE,
582                                    g_param_spec_string ("tearoff-title",
583                                                         P_("Tearoff Title"),
584                                                         P_("A title that may be displayed by the window manager when this menu is torn-off"),
585                                                         NULL,
586                                                         GTK_PARAM_READWRITE));
587
588   /**
589    * GtkMenu:tearoff-state:
590    *
591    * A boolean that indicates whether the menu is torn-off.
592    *
593    * Since: 2.6
594    **/
595   g_object_class_install_property (gobject_class,
596                                    PROP_TEAROFF_STATE,
597                                    g_param_spec_boolean ("tearoff-state",
598                                                          P_("Tearoff State"),
599                                                          P_("A boolean that indicates whether the menu is torn-off"),
600                                                          FALSE,
601                                                          GTK_PARAM_READWRITE));
602
603   /**
604    * GtkMenu:monitor:
605    *
606    * The monitor the menu will be popped up on.
607    *
608    * Since: 2.14
609    **/
610   g_object_class_install_property (gobject_class,
611                                    PROP_MONITOR,
612                                    g_param_spec_int ("monitor",
613                                                      P_("Monitor"),
614                                                      P_("The monitor the menu will be popped up on"),
615                                                      -1, G_MAXINT, -1,
616                                                      GTK_PARAM_READWRITE));
617
618   gtk_widget_class_install_style_property (widget_class,
619                                            g_param_spec_int ("vertical-padding",
620                                                              P_("Vertical Padding"),
621                                                              P_("Extra space at the top and bottom of the menu"),
622                                                              0,
623                                                              G_MAXINT,
624                                                              1,
625                                                              GTK_PARAM_READABLE));
626
627   /**
628    * GtkMenu:reserve-toggle-size:
629    *
630    * A boolean that indicates whether the menu reserves space for
631    * toggles and icons, regardless of their actual presence.
632    *
633    * This property should only be changed from its default value
634    * for special-purposes such as tabular menus. Regular menus that
635    * are connected to a menu bar or context menus should reserve
636    * toggle space for consistency.
637    *
638    * Since: 2.18
639    */
640   g_object_class_install_property (gobject_class,
641                                    PROP_RESERVE_TOGGLE_SIZE,
642                                    g_param_spec_boolean ("reserve-toggle-size",
643                                                          P_("Reserve Toggle Size"),
644                                                          P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
645                                                          TRUE,
646                                                          GTK_PARAM_READWRITE));
647
648   gtk_widget_class_install_style_property (widget_class,
649                                            g_param_spec_int ("horizontal-padding",
650                                                              P_("Horizontal Padding"),
651                                                              P_("Extra space at the left and right edges of the menu"),
652                                                              0,
653                                                              G_MAXINT,
654                                                              0,
655                                                              GTK_PARAM_READABLE));
656
657   gtk_widget_class_install_style_property (widget_class,
658                                            g_param_spec_int ("vertical-offset",
659                                                              P_("Vertical Offset"),
660                                                              P_("When the menu is a submenu, position it this number of pixels offset vertically"),
661                                                              G_MININT,
662                                                              G_MAXINT,
663                                                              0,
664                                                              GTK_PARAM_READABLE));
665
666   gtk_widget_class_install_style_property (widget_class,
667                                            g_param_spec_int ("horizontal-offset",
668                                                              P_("Horizontal Offset"),
669                                                              P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
670                                                              G_MININT,
671                                                              G_MAXINT,
672                                                              -2,
673                                                              GTK_PARAM_READABLE));
674
675   gtk_widget_class_install_style_property (widget_class,
676                                            g_param_spec_boolean ("double-arrows",
677                                                                  P_("Double Arrows"),
678                                                                  P_("When scrolling, always show both arrows."),
679                                                                  TRUE,
680                                                                  GTK_PARAM_READABLE));
681
682   /**
683    * GtkMenu:arrow-placement:
684    *
685    * Indicates where scroll arrows should be placed.
686    *
687    * Since: 2.16
688    **/
689   gtk_widget_class_install_style_property (widget_class,
690                                            g_param_spec_enum ("arrow-placement",
691                                                               P_("Arrow Placement"),
692                                                               P_("Indicates where scroll arrows should be placed"),
693                                                               GTK_TYPE_ARROW_PLACEMENT,
694                                                               GTK_ARROWS_BOTH,
695                                                               GTK_PARAM_READABLE));
696
697  gtk_container_class_install_child_property (container_class,
698                                              CHILD_PROP_LEFT_ATTACH,
699                                               g_param_spec_int ("left-attach",
700                                                                P_("Left Attach"),
701                                                                P_("The column number to attach the left side of the child to"),
702                                                                 -1, INT_MAX, -1,
703                                                                GTK_PARAM_READWRITE));
704
705  gtk_container_class_install_child_property (container_class,
706                                              CHILD_PROP_RIGHT_ATTACH,
707                                               g_param_spec_int ("right-attach",
708                                                                P_("Right Attach"),
709                                                                P_("The column number to attach the right side of the child to"),
710                                                                 -1, INT_MAX, -1,
711                                                                GTK_PARAM_READWRITE));
712
713  gtk_container_class_install_child_property (container_class,
714                                              CHILD_PROP_TOP_ATTACH,
715                                               g_param_spec_int ("top-attach",
716                                                                P_("Top Attach"),
717                                                                P_("The row number to attach the top of the child to"),
718                                                                 -1, INT_MAX, -1,
719                                                                GTK_PARAM_READWRITE));
720
721  gtk_container_class_install_child_property (container_class,
722                                              CHILD_PROP_BOTTOM_ATTACH,
723                                               g_param_spec_int ("bottom-attach",
724                                                                P_("Bottom Attach"),
725                                                                P_("The row number to attach the bottom of the child to"),
726                                                                 -1, INT_MAX, -1,
727                                                                GTK_PARAM_READWRITE));
728
729  /**
730   * GtkMenu::arrow-scaling
731   *
732   * Arbitrary constant to scale down the size of the scroll arrow.
733   *
734   * Since: 2.16
735   */
736   gtk_widget_class_install_style_property (widget_class,
737                                            g_param_spec_float ("arrow-scaling",
738                                                                P_("Arrow Scaling"),
739                                                                P_("Arbitrary constant to scale down the size of the scroll arrow"),
740                                                                0.0, 1.0, 0.7,
741                                                                GTK_PARAM_READABLE));
742
743   binding_set = gtk_binding_set_by_class (class);
744   gtk_binding_entry_add_signal (binding_set,
745                                 GDK_KEY_Up, 0,
746                                 I_("move-current"), 1,
747                                 GTK_TYPE_MENU_DIRECTION_TYPE,
748                                 GTK_MENU_DIR_PREV);
749   gtk_binding_entry_add_signal (binding_set,
750                                 GDK_KEY_KP_Up, 0,
751                                 "move-current", 1,
752                                 GTK_TYPE_MENU_DIRECTION_TYPE,
753                                 GTK_MENU_DIR_PREV);
754   gtk_binding_entry_add_signal (binding_set,
755                                 GDK_KEY_Down, 0,
756                                 "move-current", 1,
757                                 GTK_TYPE_MENU_DIRECTION_TYPE,
758                                 GTK_MENU_DIR_NEXT);
759   gtk_binding_entry_add_signal (binding_set,
760                                 GDK_KEY_KP_Down, 0,
761                                 "move-current", 1,
762                                 GTK_TYPE_MENU_DIRECTION_TYPE,
763                                 GTK_MENU_DIR_NEXT);
764   gtk_binding_entry_add_signal (binding_set,
765                                 GDK_KEY_Left, 0,
766                                 "move-current", 1,
767                                 GTK_TYPE_MENU_DIRECTION_TYPE,
768                                 GTK_MENU_DIR_PARENT);
769   gtk_binding_entry_add_signal (binding_set,
770                                 GDK_KEY_KP_Left, 0,
771                                 "move-current", 1,
772                                 GTK_TYPE_MENU_DIRECTION_TYPE,
773                                 GTK_MENU_DIR_PARENT);
774   gtk_binding_entry_add_signal (binding_set,
775                                 GDK_KEY_Right, 0,
776                                 "move-current", 1,
777                                 GTK_TYPE_MENU_DIRECTION_TYPE,
778                                 GTK_MENU_DIR_CHILD);
779   gtk_binding_entry_add_signal (binding_set,
780                                 GDK_KEY_KP_Right, 0,
781                                 "move-current", 1,
782                                 GTK_TYPE_MENU_DIRECTION_TYPE,
783                                 GTK_MENU_DIR_CHILD);
784   gtk_binding_entry_add_signal (binding_set,
785                                 GDK_KEY_Home, 0,
786                                 "move-scroll", 1,
787                                 GTK_TYPE_SCROLL_TYPE,
788                                 GTK_SCROLL_START);
789   gtk_binding_entry_add_signal (binding_set,
790                                 GDK_KEY_KP_Home, 0,
791                                 "move-scroll", 1,
792                                 GTK_TYPE_SCROLL_TYPE,
793                                 GTK_SCROLL_START);
794   gtk_binding_entry_add_signal (binding_set,
795                                 GDK_KEY_End, 0,
796                                 "move-scroll", 1,
797                                 GTK_TYPE_SCROLL_TYPE,
798                                 GTK_SCROLL_END);
799   gtk_binding_entry_add_signal (binding_set,
800                                 GDK_KEY_KP_End, 0,
801                                 "move-scroll", 1,
802                                 GTK_TYPE_SCROLL_TYPE,
803                                 GTK_SCROLL_END);
804   gtk_binding_entry_add_signal (binding_set,
805                                 GDK_KEY_Page_Up, 0,
806                                 "move-scroll", 1,
807                                 GTK_TYPE_SCROLL_TYPE,
808                                 GTK_SCROLL_PAGE_UP);
809   gtk_binding_entry_add_signal (binding_set,
810                                 GDK_KEY_KP_Page_Up, 0,
811                                 "move-scroll", 1,
812                                 GTK_TYPE_SCROLL_TYPE,
813                                 GTK_SCROLL_PAGE_UP);
814   gtk_binding_entry_add_signal (binding_set,
815                                 GDK_KEY_Page_Down, 0,
816                                 "move-scroll", 1,
817                                 GTK_TYPE_SCROLL_TYPE,
818                                 GTK_SCROLL_PAGE_DOWN);
819   gtk_binding_entry_add_signal (binding_set,
820                                 GDK_KEY_KP_Page_Down, 0,
821                                 "move-scroll", 1,
822                                 GTK_TYPE_SCROLL_TYPE,
823                                 GTK_SCROLL_PAGE_DOWN);
824
825   gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
826                                                        P_("Can change accelerators"),
827                                                        P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
828                                                        FALSE,
829                                                        GTK_PARAM_READWRITE));
830
831   gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
832                                                    P_("Delay before submenus appear"),
833                                                    P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
834                                                    0,
835                                                    G_MAXINT,
836                                                    DEFAULT_POPUP_DELAY,
837                                                    GTK_PARAM_READWRITE));
838
839   gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
840                                                    P_("Delay before hiding a submenu"),
841                                                    P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
842                                                    0,
843                                                    G_MAXINT,
844                                                    DEFAULT_POPDOWN_DELAY,
845                                                    GTK_PARAM_READWRITE));
846
847   g_type_class_add_private (gobject_class, sizeof (GtkMenuPrivate));
848 }
849
850
851 static void
852 gtk_menu_set_property (GObject      *object,
853                        guint         prop_id,
854                        const GValue *value,
855                        GParamSpec   *pspec)
856 {
857   GtkMenu *menu = GTK_MENU (object);
858
859   switch (prop_id)
860     {
861     case PROP_ACTIVE:
862       gtk_menu_set_active (menu, g_value_get_int (value));
863       break;
864     case PROP_ACCEL_GROUP:
865       gtk_menu_set_accel_group (menu, g_value_get_object (value));
866       break;
867     case PROP_ACCEL_PATH:
868       gtk_menu_set_accel_path (menu, g_value_get_string (value));
869       break;
870     case PROP_ATTACH_WIDGET:
871       {
872         GtkWidget *widget;
873
874         widget = gtk_menu_get_attach_widget (menu);
875         if (widget)
876           gtk_menu_detach (menu);
877
878         widget = (GtkWidget*) g_value_get_object (value); 
879         if (widget)
880           gtk_menu_attach_to_widget (menu, widget, NULL);
881       }
882       break;
883     case PROP_TEAROFF_STATE:
884       gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
885       break;
886     case PROP_TEAROFF_TITLE:
887       gtk_menu_set_title (menu, g_value_get_string (value));
888       break;
889     case PROP_MONITOR:
890       gtk_menu_set_monitor (menu, g_value_get_int (value));
891       break;
892     case PROP_RESERVE_TOGGLE_SIZE:
893       gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
894       break;
895     default:
896       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
897       break;
898     }
899 }
900
901 static void
902 gtk_menu_get_property (GObject     *object,
903                        guint        prop_id,
904                        GValue      *value,
905                        GParamSpec  *pspec)
906 {
907   GtkMenu *menu = GTK_MENU (object);
908
909   switch (prop_id)
910     {
911     case PROP_ACTIVE:
912       g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->children, gtk_menu_get_active (menu)));
913       break;
914     case PROP_ACCEL_GROUP:
915       g_value_set_object (value, gtk_menu_get_accel_group (menu));
916       break;
917     case PROP_ACCEL_PATH:
918       g_value_set_string (value, gtk_menu_get_accel_path (menu));
919       break;
920     case PROP_ATTACH_WIDGET:
921       g_value_set_object (value, gtk_menu_get_attach_widget (menu));
922       break;
923     case PROP_TEAROFF_STATE:
924       g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
925       break;
926     case PROP_TEAROFF_TITLE:
927       g_value_set_string (value, gtk_menu_get_title (menu));
928       break;
929     case PROP_MONITOR:
930       g_value_set_int (value, gtk_menu_get_monitor (menu));
931       break;
932     case PROP_RESERVE_TOGGLE_SIZE:
933       g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
934       break;
935     default:
936       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
937       break;
938     }
939 }
940
941 static void
942 gtk_menu_set_child_property (GtkContainer *container,
943                              GtkWidget    *child,
944                              guint         property_id,
945                              const GValue *value,
946                              GParamSpec   *pspec)
947 {
948   GtkMenu *menu = GTK_MENU (container);
949   AttachInfo *ai = get_attach_info (child);
950
951   switch (property_id)
952     {
953     case CHILD_PROP_LEFT_ATTACH:
954       ai->left_attach = g_value_get_int (value);
955       break;
956     case CHILD_PROP_RIGHT_ATTACH:
957       ai->right_attach = g_value_get_int (value);
958       break;
959     case CHILD_PROP_TOP_ATTACH:
960       ai->top_attach = g_value_get_int (value); 
961       break;
962     case CHILD_PROP_BOTTOM_ATTACH:
963       ai->bottom_attach = g_value_get_int (value);
964       break;
965
966     default:
967       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
968       return;
969     }
970
971   menu_queue_resize (menu);
972 }
973
974 static void
975 gtk_menu_get_child_property (GtkContainer *container,
976                              GtkWidget    *child,
977                              guint         property_id,
978                              GValue       *value,
979                              GParamSpec   *pspec)
980 {
981   AttachInfo *ai = get_attach_info (child);
982
983   switch (property_id)
984     {
985     case CHILD_PROP_LEFT_ATTACH:
986       g_value_set_int (value, ai->left_attach);
987       break;
988     case CHILD_PROP_RIGHT_ATTACH:
989       g_value_set_int (value, ai->right_attach);
990       break;
991     case CHILD_PROP_TOP_ATTACH:
992       g_value_set_int (value, ai->top_attach);
993       break;
994     case CHILD_PROP_BOTTOM_ATTACH:
995       g_value_set_int (value, ai->bottom_attach);
996       break;
997       
998     default:
999       GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
1000       return;
1001     }
1002 }
1003
1004 static gboolean
1005 gtk_menu_window_event (GtkWidget *window,
1006                        GdkEvent  *event,
1007                        GtkWidget *menu)
1008 {
1009   gboolean handled = FALSE;
1010
1011   g_object_ref (window);
1012   g_object_ref (menu);
1013
1014   switch (event->type)
1015     {
1016     case GDK_KEY_PRESS:
1017     case GDK_KEY_RELEASE:
1018       handled = gtk_widget_event (menu, event);
1019       break;
1020     default:
1021       break;
1022     }
1023
1024   g_object_unref (window);
1025   g_object_unref (menu);
1026
1027   return handled;
1028 }
1029
1030 static void
1031 gtk_menu_init (GtkMenu *menu)
1032 {
1033   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
1034
1035   menu->parent_menu_item = NULL;
1036   menu->old_active_menu_item = NULL;
1037   menu->accel_group = NULL;
1038   menu->position_func = NULL;
1039   menu->position_func_data = NULL;
1040   menu->toggle_size = 0;
1041
1042   menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
1043                                                    "type", GTK_WINDOW_POPUP,
1044                                                    "child", menu,
1045                                                    NULL),
1046                                      "signal::event", gtk_menu_window_event, menu,
1047                                      "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
1048                                      NULL);
1049   gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
1050   gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
1051
1052   /* Refloat the menu, so that reference counting for the menu isn't
1053    * affected by it being a child of the toplevel
1054    */
1055   g_object_force_floating (G_OBJECT (menu));
1056   menu->needs_destruction_ref_count = TRUE;
1057
1058   menu->view_window = NULL;
1059   menu->bin_window = NULL;
1060
1061   menu->scroll_offset = 0;
1062   menu->scroll_step  = 0;
1063   menu->timeout_id = 0;
1064   menu->scroll_fast = FALSE;
1065   
1066   menu->tearoff_window = NULL;
1067   menu->tearoff_hbox = NULL;
1068   menu->torn_off = FALSE;
1069   menu->tearoff_active = FALSE;
1070   menu->tearoff_adjustment = NULL;
1071   menu->tearoff_scrollbar = NULL;
1072
1073   menu->upper_arrow_visible = FALSE;
1074   menu->lower_arrow_visible = FALSE;
1075   menu->upper_arrow_prelight = FALSE;
1076   menu->lower_arrow_prelight = FALSE;
1077
1078   priv->upper_arrow_state = GTK_STATE_NORMAL;
1079   priv->lower_arrow_state = GTK_STATE_NORMAL;
1080
1081   priv->have_layout = FALSE;
1082   priv->monitor_num = -1;
1083 }
1084
1085 static void
1086 gtk_menu_destroy (GtkObject *object)
1087 {
1088   GtkMenu *menu = GTK_MENU (object);
1089   GtkMenuAttachData *data;
1090   GtkMenuPrivate *priv; 
1091
1092   gtk_menu_remove_scroll_timeout (menu);
1093   
1094   data = g_object_get_data (G_OBJECT (object), attach_data_key);
1095   if (data)
1096     gtk_menu_detach (menu);
1097   
1098   gtk_menu_stop_navigating_submenu (menu);
1099
1100   if (menu->old_active_menu_item)
1101     {
1102       g_object_unref (menu->old_active_menu_item);
1103       menu->old_active_menu_item = NULL;
1104     }
1105
1106   /* Add back the reference count for being a child */
1107   if (menu->needs_destruction_ref_count)
1108     {
1109       menu->needs_destruction_ref_count = FALSE;
1110       g_object_ref (object);
1111     }
1112   
1113   if (menu->accel_group)
1114     {
1115       g_object_unref (menu->accel_group);
1116       menu->accel_group = NULL;
1117     }
1118
1119   if (menu->toplevel)
1120     gtk_widget_destroy (menu->toplevel);
1121
1122   if (menu->tearoff_window)
1123     gtk_widget_destroy (menu->tearoff_window);
1124
1125   priv = gtk_menu_get_private (menu);
1126
1127   if (priv->heights)
1128     {
1129       g_free (priv->heights);
1130       priv->heights = NULL;
1131     }
1132
1133   if (priv->title)
1134     {
1135       g_free (priv->title);
1136       priv->title = NULL;
1137     }
1138
1139   GTK_OBJECT_CLASS (gtk_menu_parent_class)->destroy (object);
1140 }
1141
1142 static void
1143 menu_change_screen (GtkMenu   *menu,
1144                     GdkScreen *new_screen)
1145 {
1146   GtkMenuPrivate *private = gtk_menu_get_private (menu);
1147
1148   if (gtk_widget_has_screen (GTK_WIDGET (menu)))
1149     {
1150       if (new_screen == gtk_widget_get_screen (GTK_WIDGET (menu)))
1151         return;
1152     }
1153
1154   if (menu->torn_off)
1155     {
1156       gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
1157       gtk_menu_position (menu);
1158     }
1159
1160   gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
1161   private->monitor_num = -1;
1162 }
1163
1164 static void
1165 attach_widget_screen_changed (GtkWidget *attach_widget,
1166                               GdkScreen *previous_screen,
1167                               GtkMenu   *menu)
1168 {
1169   if (gtk_widget_has_screen (attach_widget) &&
1170       !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1171     {
1172       menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
1173     }
1174 }
1175
1176 void
1177 gtk_menu_attach_to_widget (GtkMenu             *menu,
1178                            GtkWidget           *attach_widget,
1179                            GtkMenuDetachFunc    detacher)
1180 {
1181   GtkMenuAttachData *data;
1182   GList *list;
1183   
1184   g_return_if_fail (GTK_IS_MENU (menu));
1185   g_return_if_fail (GTK_IS_WIDGET (attach_widget));
1186   
1187   /* keep this function in sync with gtk_widget_set_parent()
1188    */
1189   
1190   data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1191   if (data)
1192     {
1193       g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
1194                  g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
1195      return;
1196     }
1197   
1198   g_object_ref_sink (menu);
1199   
1200   data = g_slice_new (GtkMenuAttachData);
1201   data->attach_widget = attach_widget;
1202   
1203   g_signal_connect (attach_widget, "screen-changed",
1204                     G_CALLBACK (attach_widget_screen_changed), menu);
1205   attach_widget_screen_changed (attach_widget, NULL, menu);
1206   
1207   data->detacher = detacher;
1208   g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
1209   list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
1210   if (!g_list_find (list, menu))
1211     {
1212       list = g_list_prepend (list, menu);
1213     }
1214   g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
1215                           (GDestroyNotify) g_list_free);
1216
1217   if (gtk_widget_get_state (GTK_WIDGET (menu)) != GTK_STATE_NORMAL)
1218     gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
1219   
1220   /* we don't need to set the style here, since
1221    * we are a toplevel widget.
1222    */
1223
1224   /* Fallback title for menu comes from attach widget */
1225   gtk_menu_update_title (menu);
1226
1227   g_object_notify (G_OBJECT (menu), "attach-widget");
1228 }
1229
1230 GtkWidget*
1231 gtk_menu_get_attach_widget (GtkMenu *menu)
1232 {
1233   GtkMenuAttachData *data;
1234   
1235   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1236   
1237   data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1238   if (data)
1239     return data->attach_widget;
1240   return NULL;
1241 }
1242
1243 void
1244 gtk_menu_detach (GtkMenu *menu)
1245 {
1246   GtkMenuAttachData *data;
1247   GList *list;
1248   
1249   g_return_if_fail (GTK_IS_MENU (menu));
1250   
1251   /* keep this function in sync with gtk_widget_unparent()
1252    */
1253   data = g_object_get_data (G_OBJECT (menu), attach_data_key);
1254   if (!data)
1255     {
1256       g_warning ("gtk_menu_detach(): menu is not attached");
1257       return;
1258     }
1259   g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
1260   
1261   g_signal_handlers_disconnect_by_func (data->attach_widget,
1262                                         (gpointer) attach_widget_screen_changed,
1263                                         menu);
1264
1265   if (data->detacher)
1266     data->detacher (data->attach_widget, menu);
1267   list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
1268   list = g_list_remove (list, menu);
1269   if (list)
1270     g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
1271                             (GDestroyNotify) g_list_free);
1272   else
1273     g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
1274   
1275   if (gtk_widget_get_realized (GTK_WIDGET (menu)))
1276     gtk_widget_unrealize (GTK_WIDGET (menu));
1277   
1278   g_slice_free (GtkMenuAttachData, data);
1279   
1280   /* Fallback title for menu comes from attach widget */
1281   gtk_menu_update_title (menu);
1282
1283   g_object_unref (menu);
1284 }
1285
1286 static void
1287 gtk_menu_remove (GtkContainer *container,
1288                  GtkWidget    *widget)
1289 {
1290   GtkMenu *menu = GTK_MENU (container);
1291
1292   g_return_if_fail (GTK_IS_MENU_ITEM (widget));
1293
1294   /* Clear out old_active_menu_item if it matches the item we are removing
1295    */
1296   if (menu->old_active_menu_item == widget)
1297     {
1298       g_object_unref (menu->old_active_menu_item);
1299       menu->old_active_menu_item = NULL;
1300     }
1301
1302   GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
1303   g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
1304
1305   menu_queue_resize (menu);
1306 }
1307
1308 GtkWidget*
1309 gtk_menu_new (void)
1310 {
1311   return g_object_new (GTK_TYPE_MENU, NULL);
1312 }
1313
1314 static void
1315 gtk_menu_real_insert (GtkMenuShell *menu_shell,
1316                       GtkWidget    *child,
1317                       gint          position)
1318 {
1319   GtkMenu *menu = GTK_MENU (menu_shell);
1320   AttachInfo *ai = get_attach_info (child);
1321
1322   ai->left_attach = -1;
1323   ai->right_attach = -1;
1324   ai->top_attach = -1;
1325   ai->bottom_attach = -1;
1326
1327   if (gtk_widget_get_realized (GTK_WIDGET (menu_shell)))
1328     gtk_widget_set_parent_window (child, menu->bin_window);
1329
1330   GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
1331
1332   menu_queue_resize (menu);
1333 }
1334
1335 static void
1336 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
1337 {
1338   GtkWidget *widget;
1339   gint width, height;
1340
1341   widget = GTK_WIDGET (menu);
1342
1343   if (menu->torn_off)
1344     {
1345       GdkPixmap *pixmap;
1346       GdkWindow *window;
1347       cairo_t *cr;
1348
1349       menu->tearoff_active = FALSE;
1350       menu->saved_scroll_offset = menu->scroll_offset;
1351
1352       window = gtk_widget_get_window (menu->tearoff_window);
1353
1354       gdk_drawable_get_size (window, &width, &height);
1355
1356       pixmap = gdk_pixmap_new (window,
1357                                width,
1358                                height,
1359                                -1);
1360
1361       cr = gdk_cairo_create (pixmap);
1362       /* Let's hope that function never notices we're not passing it a pixmap */
1363       gdk_cairo_set_source_pixmap (cr,
1364                                    window,
1365                                    0, 0);
1366       cairo_paint (cr);
1367       cairo_destroy (cr);
1368
1369       gtk_widget_set_size_request (menu->tearoff_window,
1370                                    width,
1371                                    height);
1372
1373       gdk_window_set_back_pixmap (window, pixmap, FALSE);
1374       g_object_unref (pixmap);
1375     }
1376 }
1377
1378 static gboolean
1379 popup_grab_on_window (GdkWindow *window,
1380                       GdkDevice *keyboard,
1381                       GdkDevice *pointer,
1382                       guint32    activate_time)
1383 {
1384   if (keyboard &&
1385       gdk_device_grab (keyboard, window,
1386                        GDK_OWNERSHIP_WINDOW, TRUE,
1387                        GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK,
1388                        NULL, activate_time) != GDK_GRAB_SUCCESS)
1389     return FALSE;
1390
1391   if (pointer &&
1392       gdk_device_grab (pointer, window,
1393                        GDK_OWNERSHIP_WINDOW, TRUE,
1394                        GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1395                        GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
1396                        GDK_POINTER_MOTION_MASK,
1397                        NULL, activate_time) != GDK_GRAB_SUCCESS)
1398     {
1399       if (keyboard)
1400         gdk_device_ungrab (keyboard, activate_time);
1401
1402       return FALSE;
1403     }
1404
1405   return TRUE;
1406 }
1407
1408 /**
1409  * gtk_menu_popup_for_device:
1410  * @menu: a #GtkMenu.
1411  * @device: (allow-none): a #GdkDevice
1412  * @parent_menu_shell: (allow-none): the menu shell containing the triggering menu item, or %NULL
1413  * @parent_menu_item: (allow-none): the menu item whose activation triggered the popup, or %NULL
1414  * @func: (allow-none): a user supplied function used to position the menu, or %NULL
1415  * @data: (allow-none): user supplied data to be passed to @func.
1416  * @button: the mouse button which was pressed to initiate the event.
1417  * @activate_time: the time at which the activation event occurred.
1418  *
1419  * Displays a menu and makes it available for selection.  Applications can use
1420  * this function to display context-sensitive menus, and will typically supply
1421  * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data
1422  * parameters. The default menu positioning function will position the menu
1423  * at the current position of @device (or its corresponding pointer).
1424  *
1425  * The @button parameter should be the mouse button pressed to initiate
1426  * the menu popup. If the menu popup was initiated by something other than
1427  * a mouse button press, such as a mouse button release or a keypress,
1428  * @button should be 0.
1429  *
1430  * The @activate_time parameter is used to conflict-resolve initiation of
1431  * concurrent requests for mouse/keyboard grab requests. To function
1432  * properly, this needs to be the time stamp of the user event (such as
1433  * a mouse click or key press) that caused the initiation of the popup.
1434  * Only if no such event is available, gtk_get_current_event_time() can
1435  * be used instead.
1436  *
1437  * Since: 3.0
1438  */
1439 void
1440 gtk_menu_popup_for_device (GtkMenu             *menu,
1441                            GdkDevice           *device,
1442                            GtkWidget           *parent_menu_shell,
1443                            GtkWidget           *parent_menu_item,
1444                            GtkMenuPositionFunc  func,
1445                            gpointer             data,
1446                            guint                button,
1447                            guint32              activate_time)
1448 {
1449   GtkWidget *widget;
1450   GtkWidget *xgrab_shell;
1451   GtkWidget *parent;
1452   GdkEvent *current_event;
1453   GtkMenuShell *menu_shell;
1454   gboolean grab_keyboard;
1455   GtkMenuPrivate *priv;
1456   GtkWidget *parent_toplevel;
1457   GdkDevice *keyboard, *pointer;
1458
1459   g_return_if_fail (GTK_IS_MENU (menu));
1460   g_return_if_fail (GDK_IS_DEVICE (device));
1461
1462   widget = GTK_WIDGET (menu);
1463   menu_shell = GTK_MENU_SHELL (menu);
1464   priv = gtk_menu_get_private (menu);
1465
1466   if (device->source == GDK_SOURCE_KEYBOARD)
1467     {
1468       keyboard = device;
1469       pointer = gdk_device_get_associated_device (device);
1470     }
1471   else
1472     {
1473       pointer = device;
1474       keyboard = gdk_device_get_associated_device (device);
1475     }
1476
1477   menu_shell->parent_menu_shell = parent_menu_shell;
1478
1479   priv->seen_item_enter = FALSE;
1480   
1481   /* Find the last viewable ancestor, and make an X grab on it
1482    */
1483   parent = GTK_WIDGET (menu);
1484   xgrab_shell = NULL;
1485   while (parent)
1486     {
1487       gboolean viewable = TRUE;
1488       GtkWidget *tmp = parent;
1489       
1490       while (tmp)
1491         {
1492           if (!gtk_widget_get_mapped (tmp))
1493             {
1494               viewable = FALSE;
1495               break;
1496             }
1497           tmp = gtk_widget_get_parent (tmp);
1498         }
1499       
1500       if (viewable)
1501         xgrab_shell = parent;
1502       
1503       parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
1504     }
1505
1506   /* We want to receive events generated when we map the menu; unfortunately,
1507    * since there is probably already an implicit grab in place from the
1508    * button that the user used to pop up the menu, we won't receive then --
1509    * in particular, the EnterNotify when the menu pops up under the pointer.
1510    *
1511    * If we are grabbing on a parent menu shell, no problem; just grab on
1512    * that menu shell first before popping up the window with owner_events = TRUE.
1513    *
1514    * When grabbing on the menu itself, things get more convuluted - we
1515    * we do an explicit grab on a specially created window with
1516    * owner_events = TRUE, which we override further down with a grab
1517    * on the menu. (We can't grab on the menu until it is mapped; we
1518    * probably could just leave the grab on the other window, with a
1519    * little reorganization of the code in gtkmenu*).
1520    */
1521   grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
1522   gtk_window_set_accept_focus (GTK_WINDOW (menu->toplevel), grab_keyboard);
1523
1524   if (!grab_keyboard)
1525     keyboard = NULL;
1526
1527   if (xgrab_shell && xgrab_shell != widget)
1528     {
1529       if (popup_grab_on_window (gtk_widget_get_window (xgrab_shell), keyboard, pointer, activate_time))
1530         {
1531           _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1532           GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1533         }
1534     }
1535   else
1536     {
1537       GdkWindow *transfer_window;
1538
1539       xgrab_shell = widget;
1540       transfer_window = menu_grab_transfer_window_get (menu);
1541       if (popup_grab_on_window (transfer_window, keyboard, pointer, activate_time))
1542         {
1543           _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
1544           GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
1545         }
1546     }
1547
1548   if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
1549     {
1550       /* We failed to make our pointer/keyboard grab. Rather than leaving the user
1551        * with a stuck up window, we just abort here. Presumably the user will
1552        * try again.
1553        */
1554       menu_shell->parent_menu_shell = NULL;
1555       menu_grab_transfer_window_destroy (menu);
1556       return;
1557     }
1558
1559   _gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
1560   menu_shell->active = TRUE;
1561   menu_shell->button = button;
1562
1563   /* If we are popping up the menu from something other than, a button
1564    * press then, as a heuristic, we ignore enter events for the menu
1565    * until we get a MOTION_NOTIFY.  
1566    */
1567
1568   current_event = gtk_get_current_event ();
1569   if (current_event)
1570     {
1571       if ((current_event->type != GDK_BUTTON_PRESS) &&
1572           (current_event->type != GDK_ENTER_NOTIFY))
1573         menu_shell->ignore_enter = TRUE;
1574
1575       gdk_event_free (current_event);
1576     }
1577   else
1578     menu_shell->ignore_enter = TRUE;
1579
1580   if (menu->torn_off)
1581     {
1582       gtk_menu_tearoff_bg_copy (menu);
1583
1584       gtk_menu_reparent (menu, menu->toplevel, FALSE);
1585     }
1586
1587   parent_toplevel = NULL;
1588   if (parent_menu_shell) 
1589     parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
1590   else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
1591     {
1592       GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
1593       if (attach_widget)
1594         parent_toplevel = gtk_widget_get_toplevel (attach_widget);
1595     }
1596
1597   /* Set transient for to get the right window group and parent relationship */
1598   if (GTK_IS_WINDOW (parent_toplevel))
1599     gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
1600                                   GTK_WINDOW (parent_toplevel));
1601   
1602   menu->parent_menu_item = parent_menu_item;
1603   menu->position_func = func;
1604   menu->position_func_data = data;
1605   menu_shell->activate_time = activate_time;
1606
1607   /* We need to show the menu here rather in the init function because
1608    * code expects to be able to tell if the menu is onscreen by
1609    * looking at the gtk_widget_get_visible (menu)
1610    */
1611   gtk_widget_show (GTK_WIDGET (menu));
1612
1613   /* Position the menu, possibly changing the size request
1614    */
1615   gtk_menu_position (menu);
1616
1617   /* Compute the size of the toplevel and realize it so we
1618    * can scroll correctly.
1619    */
1620   {
1621     GtkRequisition tmp_request;
1622     GtkAllocation tmp_allocation = { 0, };
1623
1624     /* Instead of trusting the menu position function to queue a resize when the
1625      * menu goes out of bounds, invalidate the cached size here. */
1626     gtk_widget_queue_resize (GTK_WIDGET (menu));
1627     gtk_size_request_get_size (GTK_SIZE_REQUEST (menu->toplevel), &tmp_request, NULL);
1628     
1629     tmp_allocation.width = tmp_request.width;
1630     tmp_allocation.height = tmp_request.height;
1631
1632     gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
1633     
1634     gtk_widget_realize (GTK_WIDGET (menu));
1635   }
1636
1637   gtk_menu_scroll_to (menu, menu->scroll_offset);
1638
1639   /* if no item is selected, select the first one */
1640   if (!menu_shell->active_menu_item)
1641     {
1642       gboolean touchscreen_mode;
1643
1644       g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
1645                     "gtk-touchscreen-mode", &touchscreen_mode,
1646                     NULL);
1647
1648       if (touchscreen_mode)
1649         gtk_menu_shell_select_first (menu_shell, TRUE);
1650     }
1651
1652   /* Once everything is set up correctly, map the toplevel window on
1653      the screen.
1654    */
1655   gtk_widget_show (menu->toplevel);
1656
1657   if (xgrab_shell == widget)
1658     popup_grab_on_window (gtk_widget_get_window (widget), keyboard, pointer, activate_time); /* Should always succeed */
1659
1660   gtk_device_grab_add (GTK_WIDGET (menu), pointer, TRUE);
1661
1662   if (parent_menu_shell)
1663     {
1664       gboolean keyboard_mode;
1665
1666       keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
1667       _gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
1668     }
1669   else if (menu_shell->button == 0) /* a keynav-activated context menu */
1670     _gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
1671
1672   _gtk_menu_shell_update_mnemonics (menu_shell);
1673 }
1674
1675 /**
1676  * gtk_menu_popup:
1677  * @menu: a #GtkMenu.
1678  * @parent_menu_shell: (allow-none): the menu shell containing the triggering menu item, or %NULL
1679  * @parent_menu_item: (allow-none): the menu item whose activation triggered the popup, or %NULL
1680  * @func: (allow-none): a user supplied function used to position the menu, or %NULL
1681  * @data: (allow-none): user supplied data to be passed to @func.
1682  * @button: the mouse button which was pressed to initiate the event.
1683  * @activate_time: the time at which the activation event occurred.
1684  *
1685  * Displays a menu and makes it available for selection.  Applications can use
1686  * this function to display context-sensitive menus, and will typically supply
1687  * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data 
1688  * parameters. The default menu positioning function will position the menu
1689  * at the current mouse cursor position.
1690  *
1691  * The @button parameter should be the mouse button pressed to initiate
1692  * the menu popup. If the menu popup was initiated by something other than
1693  * a mouse button press, such as a mouse button release or a keypress,
1694  * @button should be 0.
1695  *
1696  * The @activate_time parameter is used to conflict-resolve initiation of
1697  * concurrent requests for mouse/keyboard grab requests. To function
1698  * properly, this needs to be the time stamp of the user event (such as
1699  * a mouse click or key press) that caused the initiation of the popup.
1700  * Only if no such event is available, gtk_get_current_event_time() can
1701  * be used instead.
1702  */
1703 void
1704 gtk_menu_popup (GtkMenu             *menu,
1705                 GtkWidget           *parent_menu_shell,
1706                 GtkWidget           *parent_menu_item,
1707                 GtkMenuPositionFunc  func,
1708                 gpointer             data,
1709                 guint                button,
1710                 guint32              activate_time)
1711 {
1712   GdkDevice *device;
1713
1714   g_return_if_fail (GTK_IS_MENU (menu));
1715
1716   device = gtk_get_current_event_device ();
1717
1718   if (!device)
1719     {
1720       GdkDisplay *display;
1721       GdkDeviceManager *device_manager;
1722       GList *devices;
1723
1724       display = gtk_widget_get_display (GTK_WIDGET (menu));
1725       device_manager = gdk_display_get_device_manager (display);
1726       devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
1727
1728       device = devices->data;
1729
1730       g_list_free (devices);
1731     }
1732
1733   gtk_menu_popup_for_device (menu, device,
1734                              parent_menu_shell,
1735                              parent_menu_item,
1736                              func, data,
1737                              button, activate_time);
1738 }
1739
1740 void
1741 gtk_menu_popdown (GtkMenu *menu)
1742 {
1743   GtkMenuPrivate *private;
1744   GtkMenuShell *menu_shell;
1745   GdkDevice *pointer;
1746
1747   g_return_if_fail (GTK_IS_MENU (menu));
1748   
1749   menu_shell = GTK_MENU_SHELL (menu);
1750   private = gtk_menu_get_private (menu);
1751
1752   menu_shell->parent_menu_shell = NULL;
1753   menu_shell->active = FALSE;
1754   menu_shell->ignore_enter = FALSE;
1755
1756   private->have_position = FALSE;
1757
1758   gtk_menu_stop_scrolling (menu);
1759   
1760   gtk_menu_stop_navigating_submenu (menu);
1761   
1762   if (menu_shell->active_menu_item)
1763     {
1764       if (menu->old_active_menu_item)
1765         g_object_unref (menu->old_active_menu_item);
1766       menu->old_active_menu_item = menu_shell->active_menu_item;
1767       g_object_ref (menu->old_active_menu_item);
1768     }
1769
1770   gtk_menu_shell_deselect (menu_shell);
1771   
1772   /* The X Grab, if present, will automatically be removed when we hide
1773    * the window */
1774   gtk_widget_hide (menu->toplevel);
1775   gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel), NULL);
1776
1777   pointer = _gtk_menu_shell_get_grab_device (menu_shell);
1778
1779   if (menu->torn_off)
1780     {
1781       gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
1782       
1783       if (gtk_bin_get_child (GTK_BIN (menu->toplevel))) 
1784         {
1785           gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
1786         } 
1787       else
1788         {
1789           /* We popped up the menu from the tearoff, so we need to
1790            * release the grab - we aren't actually hiding the menu.
1791            */
1792           if (menu_shell->have_xgrab && pointer)
1793             {
1794               GdkDevice *keyboard;
1795
1796               gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
1797               keyboard = gdk_device_get_associated_device (pointer);
1798
1799               if (keyboard)
1800                 gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
1801             }
1802         }
1803
1804       /* gtk_menu_popdown is called each time a menu item is selected from
1805        * a torn off menu. Only scroll back to the saved position if the
1806        * non-tearoff menu was popped down.
1807        */
1808       if (!menu->tearoff_active)
1809         gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
1810       menu->tearoff_active = TRUE;
1811     }
1812   else
1813     gtk_widget_hide (GTK_WIDGET (menu));
1814
1815   menu_shell->have_xgrab = FALSE;
1816
1817   if (pointer)
1818     gtk_device_grab_remove (GTK_WIDGET (menu), pointer);
1819
1820   _gtk_menu_shell_set_grab_device (menu_shell, NULL);
1821
1822   menu_grab_transfer_window_destroy (menu);
1823 }
1824
1825 GtkWidget*
1826 gtk_menu_get_active (GtkMenu *menu)
1827 {
1828   GtkWidget *child;
1829   GList *children;
1830   
1831   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1832   
1833   if (!menu->old_active_menu_item)
1834     {
1835       child = NULL;
1836       children = GTK_MENU_SHELL (menu)->children;
1837       
1838       while (children)
1839         {
1840           child = children->data;
1841           children = children->next;
1842           
1843           if (gtk_bin_get_child (GTK_BIN (child)))
1844             break;
1845           child = NULL;
1846         }
1847       
1848       menu->old_active_menu_item = child;
1849       if (menu->old_active_menu_item)
1850         g_object_ref (menu->old_active_menu_item);
1851     }
1852   
1853   return menu->old_active_menu_item;
1854 }
1855
1856 void
1857 gtk_menu_set_active (GtkMenu *menu,
1858                      guint    index)
1859 {
1860   GtkWidget *child;
1861   GList *tmp_list;
1862   
1863   g_return_if_fail (GTK_IS_MENU (menu));
1864   
1865   tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
1866   if (tmp_list)
1867     {
1868       child = tmp_list->data;
1869       if (gtk_bin_get_child (GTK_BIN (child)))
1870         {
1871           if (menu->old_active_menu_item)
1872             g_object_unref (menu->old_active_menu_item);
1873           menu->old_active_menu_item = child;
1874           g_object_ref (menu->old_active_menu_item);
1875         }
1876     }
1877 }
1878
1879
1880 /**
1881  * gtk_menu_set_accel_group:
1882  * @accel_group: (allow-none):
1883  */
1884 void
1885 gtk_menu_set_accel_group (GtkMenu       *menu,
1886                           GtkAccelGroup *accel_group)
1887 {
1888   g_return_if_fail (GTK_IS_MENU (menu));
1889   
1890   if (menu->accel_group != accel_group)
1891     {
1892       if (menu->accel_group)
1893         g_object_unref (menu->accel_group);
1894       menu->accel_group = accel_group;
1895       if (menu->accel_group)
1896         g_object_ref (menu->accel_group);
1897       _gtk_menu_refresh_accel_paths (menu, TRUE);
1898     }
1899 }
1900
1901 GtkAccelGroup*
1902 gtk_menu_get_accel_group (GtkMenu *menu)
1903 {
1904   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1905
1906   return menu->accel_group;
1907 }
1908
1909 static gboolean
1910 gtk_menu_real_can_activate_accel (GtkWidget *widget,
1911                                   guint      signal_id)
1912 {
1913   /* Menu items chain here to figure whether they can activate their
1914    * accelerators.  Unlike ordinary widgets, menus allow accel
1915    * activation even if invisible since that's the usual case for
1916    * submenus/popup-menus. however, the state of the attach widget
1917    * affects the "activeness" of the menu.
1918    */
1919   GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
1920
1921   if (awidget)
1922     return gtk_widget_can_activate_accel (awidget, signal_id);
1923   else
1924     return gtk_widget_is_sensitive (widget);
1925 }
1926
1927 /**
1928  * gtk_menu_set_accel_path
1929  * @menu:       a valid #GtkMenu
1930  * @accel_path: (allow-none): a valid accelerator path
1931  *
1932  * Sets an accelerator path for this menu from which accelerator paths
1933  * for its immediate children, its menu items, can be constructed.
1934  * The main purpose of this function is to spare the programmer the
1935  * inconvenience of having to call gtk_menu_item_set_accel_path() on
1936  * each menu item that should support runtime user changable accelerators.
1937  * Instead, by just calling gtk_menu_set_accel_path() on their parent,
1938  * each menu item of this menu, that contains a label describing its purpose,
1939  * automatically gets an accel path assigned. For example, a menu containing
1940  * menu items "New" and "Exit", will, after 
1941  * <literal>gtk_menu_set_accel_path (menu, "&lt;Gnumeric-Sheet&gt;/File");</literal>
1942  * has been called, assign its items the accel paths:
1943  * <literal>"&lt;Gnumeric-Sheet&gt;/File/New"</literal> and <literal>"&lt;Gnumeric-Sheet&gt;/File/Exit"</literal>.
1944  * Assigning accel paths to menu items then enables the user to change
1945  * their accelerators at runtime. More details about accelerator paths
1946  * and their default setups can be found at gtk_accel_map_add_entry().
1947  * 
1948  * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
1949  * pass a static string, you can save some memory by interning it first with 
1950  * g_intern_static_string().
1951  */
1952 void
1953 gtk_menu_set_accel_path (GtkMenu     *menu,
1954                          const gchar *accel_path)
1955 {
1956   g_return_if_fail (GTK_IS_MENU (menu));
1957   if (accel_path)
1958     g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
1959
1960   /* FIXME: accel_path should be defined as const gchar* */
1961   menu->accel_path = (gchar*)g_intern_string (accel_path);
1962   if (menu->accel_path)
1963     _gtk_menu_refresh_accel_paths (menu, FALSE);
1964 }
1965
1966 /**
1967  * gtk_menu_get_accel_path
1968  * @menu: a valid #GtkMenu
1969  *
1970  * Retrieves the accelerator path set on the menu.
1971  *
1972  * Returns: the accelerator path set on the menu.
1973  *
1974  * Since: 2.14
1975  */
1976 const gchar*
1977 gtk_menu_get_accel_path (GtkMenu *menu)
1978 {
1979   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1980
1981   return menu->accel_path;
1982 }
1983
1984 typedef struct {
1985   GtkMenu *menu;
1986   gboolean group_changed;
1987 } AccelPropagation;
1988
1989 static void
1990 refresh_accel_paths_foreach (GtkWidget *widget,
1991                              gpointer   data)
1992 {
1993   AccelPropagation *prop = data;
1994
1995   if (GTK_IS_MENU_ITEM (widget))        /* should always be true */
1996     _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1997                                        prop->menu->accel_path,
1998                                        prop->menu->accel_group,
1999                                        prop->group_changed);
2000 }
2001
2002 static void
2003 _gtk_menu_refresh_accel_paths (GtkMenu  *menu,
2004                                gboolean  group_changed)
2005 {
2006   g_return_if_fail (GTK_IS_MENU (menu));
2007
2008   if (menu->accel_path && menu->accel_group)
2009     {
2010       AccelPropagation prop;
2011
2012       prop.menu = menu;
2013       prop.group_changed = group_changed;
2014       gtk_container_foreach (GTK_CONTAINER (menu),
2015                              refresh_accel_paths_foreach,
2016                              &prop);
2017     }
2018 }
2019
2020 void
2021 gtk_menu_reposition (GtkMenu *menu)
2022 {
2023   g_return_if_fail (GTK_IS_MENU (menu));
2024
2025   if (!menu->torn_off && gtk_widget_is_drawable (GTK_WIDGET (menu)))
2026     gtk_menu_position (menu);
2027 }
2028
2029 static void
2030 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
2031                             GtkMenu       *menu)
2032 {
2033   g_return_if_fail (GTK_IS_MENU (menu));
2034
2035   if (adjustment->value != menu->scroll_offset)
2036     gtk_menu_scroll_to (menu, adjustment->value);
2037 }
2038
2039 static void
2040 gtk_menu_set_tearoff_hints (GtkMenu *menu,
2041                             gint     width)
2042 {
2043   GdkGeometry geometry_hints;
2044   GtkMenuPrivate *priv;
2045
2046   if (!menu->tearoff_window)
2047     return;
2048
2049   priv = gtk_menu_get_private (menu);
2050
2051   if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2052     {
2053       GtkRequisition requisition;
2054       gtk_widget_size_request (menu->tearoff_scrollbar, &requisition);
2055       width += requisition.width;
2056     }
2057
2058   geometry_hints.min_width = width;
2059   geometry_hints.max_width = width;
2060     
2061   geometry_hints.min_height = 0;
2062   geometry_hints.max_height = priv->requested_height;
2063
2064   gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
2065                                  NULL,
2066                                  &geometry_hints,
2067                                  GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
2068 }
2069
2070 static void
2071 gtk_menu_update_title (GtkMenu *menu)
2072 {
2073   if (menu->tearoff_window)
2074     {
2075       const gchar *title;
2076       GtkWidget *attach_widget;
2077
2078       title = gtk_menu_get_title (menu);
2079       if (!title)
2080         {
2081           attach_widget = gtk_menu_get_attach_widget (menu);
2082           if (GTK_IS_MENU_ITEM (attach_widget))
2083             {
2084               GtkWidget *child = gtk_bin_get_child (GTK_BIN (attach_widget));
2085               if (GTK_IS_LABEL (child))
2086                 title = gtk_label_get_text (GTK_LABEL (child));
2087             }
2088         }
2089       
2090       if (title)
2091         gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
2092     }
2093 }
2094
2095 static GtkWidget*
2096 gtk_menu_get_toplevel (GtkWidget *menu)
2097 {
2098   GtkWidget *attach, *toplevel;
2099
2100   attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
2101
2102   if (GTK_IS_MENU_ITEM (attach))
2103     attach = gtk_widget_get_parent (attach);
2104
2105   if (GTK_IS_MENU (attach))
2106     return gtk_menu_get_toplevel (attach);
2107   else if (GTK_IS_WIDGET (attach))
2108     {
2109       toplevel = gtk_widget_get_toplevel (attach);
2110       if (gtk_widget_is_toplevel (toplevel)) 
2111         return toplevel;
2112     }
2113
2114   return NULL;
2115 }
2116
2117 static void
2118 tearoff_window_destroyed (GtkWidget *widget,
2119                           GtkMenu   *menu)
2120 {
2121   gtk_menu_set_tearoff_state (menu, FALSE);
2122 }
2123
2124 void       
2125 gtk_menu_set_tearoff_state (GtkMenu  *menu,
2126                             gboolean  torn_off)
2127 {
2128   gint            width, height;
2129   GtkMenuPrivate *priv;
2130   
2131   g_return_if_fail (GTK_IS_MENU (menu));
2132
2133   priv = gtk_menu_get_private (menu);
2134
2135   if (menu->torn_off != torn_off)
2136     {
2137       menu->torn_off = torn_off;
2138       menu->tearoff_active = torn_off;
2139       
2140       if (menu->torn_off)
2141         {
2142           if (gtk_widget_get_visible (GTK_WIDGET (menu)))
2143             gtk_menu_popdown (menu);
2144
2145           if (!menu->tearoff_window)
2146             {
2147               GtkWidget *toplevel;
2148
2149               menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
2150                                                      "type", GTK_WINDOW_TOPLEVEL,
2151                                                      "screen", gtk_widget_get_screen (menu->toplevel),
2152                                                      "app-paintable", TRUE,
2153                                                      NULL);
2154
2155               gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
2156                                         GDK_WINDOW_TYPE_HINT_MENU);
2157               gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
2158               g_signal_connect (menu->tearoff_window, "destroy",
2159                                 G_CALLBACK (tearoff_window_destroyed), menu);
2160               g_signal_connect (menu->tearoff_window, "event",
2161                                 G_CALLBACK (gtk_menu_window_event), menu);
2162
2163               gtk_menu_update_title (menu);
2164
2165               gtk_widget_realize (menu->tearoff_window);
2166
2167               toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
2168               if (toplevel != NULL)
2169                 gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
2170                                               GTK_WINDOW (toplevel));
2171               
2172               menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
2173               gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
2174
2175               gdk_drawable_get_size (gtk_widget_get_window (GTK_WIDGET (menu)),
2176                                      &width, &height);
2177               menu->tearoff_adjustment =
2178                 GTK_ADJUSTMENT (gtk_adjustment_new (0,
2179                                                     0,
2180                                                     priv->requested_height,
2181                                                     MENU_SCROLL_STEP2,
2182                                                     height/2,
2183                                                     height));
2184               g_object_connect (menu->tearoff_adjustment,
2185                                 "signal::value-changed", gtk_menu_scrollbar_changed, menu,
2186                                 NULL);
2187               menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
2188
2189               gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
2190                                 menu->tearoff_scrollbar,
2191                                 FALSE, FALSE, 0);
2192               
2193               if (menu->tearoff_adjustment->upper > height)
2194                 gtk_widget_show (menu->tearoff_scrollbar);
2195               
2196               gtk_widget_show (menu->tearoff_hbox);
2197             }
2198           
2199           gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
2200
2201           gdk_drawable_get_size (gtk_widget_get_window (GTK_WIDGET (menu)),
2202                                  &width, NULL);
2203
2204           /* Update menu->requisition
2205            */
2206           gtk_widget_size_request (GTK_WIDGET (menu), NULL);
2207   
2208           gtk_menu_set_tearoff_hints (menu, width);
2209             
2210           gtk_widget_realize (menu->tearoff_window);
2211           gtk_menu_position (menu);
2212           
2213           gtk_widget_show (GTK_WIDGET (menu));
2214           gtk_widget_show (menu->tearoff_window);
2215
2216           gtk_menu_scroll_to (menu, 0);
2217
2218         }
2219       else
2220         {
2221           gtk_widget_hide (GTK_WIDGET (menu));
2222           gtk_widget_hide (menu->tearoff_window);
2223           if (GTK_IS_CONTAINER (menu->toplevel))
2224             gtk_menu_reparent (menu, menu->toplevel, FALSE);
2225           gtk_widget_destroy (menu->tearoff_window);
2226           
2227           menu->tearoff_window = NULL;
2228           menu->tearoff_hbox = NULL;
2229           menu->tearoff_scrollbar = NULL;
2230           menu->tearoff_adjustment = NULL;
2231         }
2232
2233       g_object_notify (G_OBJECT (menu), "tearoff-state");
2234     }
2235 }
2236
2237 /**
2238  * gtk_menu_get_tearoff_state:
2239  * @menu: a #GtkMenu
2240  *
2241  * Returns whether the menu is torn off. See
2242  * gtk_menu_set_tearoff_state ().
2243  *
2244  * Return value: %TRUE if the menu is currently torn off.
2245  **/
2246 gboolean
2247 gtk_menu_get_tearoff_state (GtkMenu *menu)
2248 {
2249   g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
2250
2251   return menu->torn_off;
2252 }
2253
2254 /**
2255  * gtk_menu_set_title:
2256  * @menu: a #GtkMenu
2257  * @title: a string containing the title for the menu.
2258  * 
2259  * Sets the title string for the menu.  The title is displayed when the menu
2260  * is shown as a tearoff menu.  If @title is %NULL, the menu will see if it is
2261  * attached to a parent menu item, and if so it will try to use the same text as
2262  * that menu item's label.
2263  **/
2264 void
2265 gtk_menu_set_title (GtkMenu     *menu,
2266                     const gchar *title)
2267 {
2268   GtkMenuPrivate *priv;
2269   char *old_title;
2270
2271   g_return_if_fail (GTK_IS_MENU (menu));
2272
2273   priv = gtk_menu_get_private (menu);
2274
2275   old_title = priv->title;
2276   priv->title = g_strdup (title);
2277   g_free (old_title);
2278        
2279   gtk_menu_update_title (menu);
2280   g_object_notify (G_OBJECT (menu), "tearoff-title");
2281 }
2282
2283 /**
2284  * gtk_menu_get_title:
2285  * @menu: a #GtkMenu
2286  *
2287  * Returns the title of the menu. See gtk_menu_set_title().
2288  *
2289  * Return value: the title of the menu, or %NULL if the menu has no
2290  * title set on it. This string is owned by the widget and should
2291  * not be modified or freed.
2292  **/
2293 G_CONST_RETURN gchar *
2294 gtk_menu_get_title (GtkMenu *menu)
2295 {
2296   GtkMenuPrivate *priv;
2297
2298   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
2299
2300   priv = gtk_menu_get_private (menu);
2301
2302   return priv->title;
2303 }
2304
2305 void
2306 gtk_menu_reorder_child (GtkMenu   *menu,
2307                         GtkWidget *child,
2308                         gint       position)
2309 {
2310   GtkMenuShell *menu_shell;
2311
2312   g_return_if_fail (GTK_IS_MENU (menu));
2313   g_return_if_fail (GTK_IS_MENU_ITEM (child));
2314
2315   menu_shell = GTK_MENU_SHELL (menu);
2316
2317   if (g_list_find (menu_shell->children, child))
2318     {   
2319       menu_shell->children = g_list_remove (menu_shell->children, child);
2320       menu_shell->children = g_list_insert (menu_shell->children, child, position);
2321
2322       menu_queue_resize (menu);
2323     }   
2324 }
2325
2326 static void
2327 gtk_menu_style_set (GtkWidget *widget,
2328                     GtkStyle  *previous_style)
2329 {
2330   if (gtk_widget_get_realized (widget))
2331     {
2332       GtkMenu *menu = GTK_MENU (widget);
2333       GtkStyle *style;
2334
2335       style = gtk_widget_get_style (widget);
2336
2337       gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2338       gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2339       gtk_style_set_background (style, gtk_widget_get_window (widget), GTK_STATE_NORMAL);
2340     }
2341 }
2342
2343 static void
2344 get_arrows_border (GtkMenu   *menu,
2345                    GtkBorder *border)
2346 {
2347   guint scroll_arrow_height;
2348   GtkArrowPlacement arrow_placement;
2349
2350   gtk_widget_style_get (GTK_WIDGET (menu),
2351                         "scroll-arrow-vlength", &scroll_arrow_height,
2352                         "arrow_placement", &arrow_placement,
2353                         NULL);
2354
2355   switch (arrow_placement)
2356     {
2357     case GTK_ARROWS_BOTH:
2358       border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
2359       border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
2360       break;
2361
2362     case GTK_ARROWS_START:
2363       border->top = (menu->upper_arrow_visible ||
2364                      menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2365       border->bottom = 0;
2366       break;
2367
2368     case GTK_ARROWS_END:
2369       border->top = 0;
2370       border->bottom = (menu->upper_arrow_visible ||
2371                         menu->lower_arrow_visible) ? scroll_arrow_height : 0;
2372       break;
2373     }
2374
2375   border->left = border->right = 0;
2376 }
2377
2378 static void
2379 gtk_menu_realize (GtkWidget *widget)
2380 {
2381   GtkAllocation allocation;
2382   GtkStyle *style;
2383   GdkWindow *window;
2384   GdkWindowAttr attributes;
2385   gint attributes_mask;
2386   gint border_width;
2387   GtkMenu *menu;
2388   GtkMenuPrivate *priv;
2389   GtkWidget *child;
2390   GList *children;
2391   guint vertical_padding;
2392   guint horizontal_padding;
2393   GtkBorder arrow_border;
2394
2395   g_return_if_fail (GTK_IS_MENU (widget));
2396
2397   menu = GTK_MENU (widget);
2398   priv = gtk_menu_get_private (menu);
2399
2400   gtk_widget_set_realized (widget, TRUE);
2401
2402   gtk_widget_get_allocation (widget, &allocation);
2403
2404   attributes.window_type = GDK_WINDOW_CHILD;
2405   attributes.x = allocation.x;
2406   attributes.y = allocation.y;
2407   attributes.width = allocation.width;
2408   attributes.height = allocation.height;
2409   attributes.wclass = GDK_INPUT_OUTPUT;
2410   attributes.visual = gtk_widget_get_visual (widget);
2411   attributes.colormap = gtk_widget_get_colormap (widget);
2412   attributes.event_mask = gtk_widget_get_events (widget);
2413   attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
2414                             GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
2415
2416   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
2417
2418   window = gdk_window_new (gtk_widget_get_parent_window (widget),
2419                            &attributes, attributes_mask);
2420   gtk_widget_set_window (widget, window);
2421   gdk_window_set_user_data (window, widget);
2422
2423   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2424
2425   style = gtk_widget_get_style (widget);
2426   gtk_widget_style_get (GTK_WIDGET (menu),
2427                         "vertical-padding", &vertical_padding,
2428                         "horizontal-padding", &horizontal_padding,
2429                         NULL);
2430
2431   gtk_widget_get_allocation (widget, &allocation);
2432
2433   attributes.x = border_width + style->xthickness + horizontal_padding;
2434   attributes.y = border_width + style->ythickness + vertical_padding;
2435   attributes.width = MAX (1, allocation.width - attributes.x * 2);
2436   attributes.height = MAX (1, allocation.height - attributes.y * 2);
2437
2438   get_arrows_border (menu, &arrow_border);
2439   attributes.y += arrow_border.top;
2440   attributes.height -= arrow_border.top;
2441   attributes.height -= arrow_border.bottom;
2442
2443   menu->view_window = gdk_window_new (window,
2444                                       &attributes, attributes_mask);
2445   gdk_window_set_user_data (menu->view_window, menu);
2446
2447   gtk_widget_get_allocation (widget, &allocation);
2448
2449   attributes.x = 0;
2450   attributes.y = 0;
2451   attributes.width = MAX (1, allocation.width - (border_width + style->xthickness + horizontal_padding) * 2);
2452   attributes.height = MAX (1, priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2);
2453
2454   menu->bin_window = gdk_window_new (menu->view_window,
2455                                      &attributes, attributes_mask);
2456   gdk_window_set_user_data (menu->bin_window, menu);
2457
2458   children = GTK_MENU_SHELL (menu)->children;
2459   while (children)
2460     {
2461       child = children->data;
2462       children = children->next;
2463           
2464       gtk_widget_set_parent_window (child, menu->bin_window);
2465     }
2466
2467   gtk_widget_style_attach (widget);
2468   gtk_style_set_background (style, menu->bin_window, GTK_STATE_NORMAL);
2469   gtk_style_set_background (style, menu->view_window, GTK_STATE_NORMAL);
2470   gtk_style_set_background (style, window, GTK_STATE_NORMAL);
2471
2472   if (GTK_MENU_SHELL (widget)->active_menu_item)
2473     gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
2474                                   GTK_MENU_SHELL (widget)->active_menu_item);
2475
2476   gdk_window_show (menu->bin_window);
2477   gdk_window_show (menu->view_window);
2478 }
2479
2480 static gboolean 
2481 gtk_menu_focus (GtkWidget       *widget,
2482                 GtkDirectionType direction)
2483 {
2484   /*
2485    * A menu or its menu items cannot have focus
2486    */
2487   return FALSE;
2488 }
2489
2490 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
2491  */
2492 static GdkWindow *
2493 menu_grab_transfer_window_get (GtkMenu *menu)
2494 {
2495   GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2496   if (!window)
2497     {
2498       GdkWindowAttr attributes;
2499       gint attributes_mask;
2500       
2501       attributes.x = -100;
2502       attributes.y = -100;
2503       attributes.width = 10;
2504       attributes.height = 10;
2505       attributes.window_type = GDK_WINDOW_TEMP;
2506       attributes.wclass = GDK_INPUT_ONLY;
2507       attributes.override_redirect = TRUE;
2508       attributes.event_mask = 0;
2509
2510       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
2511       
2512       window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
2513                                &attributes, attributes_mask);
2514       gdk_window_set_user_data (window, menu);
2515
2516       gdk_window_show (window);
2517
2518       g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), window);
2519     }
2520
2521   return window;
2522 }
2523
2524 static void
2525 menu_grab_transfer_window_destroy (GtkMenu *menu)
2526 {
2527   GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
2528   if (window)
2529     {
2530       gdk_window_set_user_data (window, NULL);
2531       gdk_window_destroy (window);
2532       g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-window"), NULL);
2533     }
2534 }
2535
2536 static void
2537 gtk_menu_unrealize (GtkWidget *widget)
2538 {
2539   GtkMenu *menu = GTK_MENU (widget);
2540
2541   menu_grab_transfer_window_destroy (menu);
2542
2543   gdk_window_set_user_data (menu->view_window, NULL);
2544   gdk_window_destroy (menu->view_window);
2545   menu->view_window = NULL;
2546
2547   gdk_window_set_user_data (menu->bin_window, NULL);
2548   gdk_window_destroy (menu->bin_window);
2549   menu->bin_window = NULL;
2550
2551   GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
2552 }
2553
2554
2555 static gint
2556 calculate_line_heights (GtkMenu *menu,
2557                         gint     for_width,
2558                         guint  **ret_min_heights,
2559                         guint  **ret_nat_heights)
2560 {
2561   GtkMenuShell   *menu_shell;
2562   GtkMenuPrivate *priv;
2563   GtkWidget      *child, *widget;
2564   GList          *children;
2565   guint           horizontal_padding;
2566   guint           border_width;
2567   guint           n_columns;
2568   gint            n_heights;
2569   guint          *min_heights;
2570   guint          *nat_heights;
2571   gint            avail_width;
2572     
2573   widget       = GTK_WIDGET (menu);
2574   menu_shell   = GTK_MENU_SHELL (widget);
2575   priv         = gtk_menu_get_private (menu);
2576   
2577   min_heights  = g_new0 (guint, gtk_menu_get_n_rows (menu));
2578   nat_heights  = g_new0 (guint, gtk_menu_get_n_rows (menu));
2579   n_heights    = gtk_menu_get_n_rows (menu);
2580   n_columns    = gtk_menu_get_n_columns (menu);
2581   avail_width  = for_width - (2 * menu->toggle_size + priv->accel_size) * n_columns;
2582
2583   gtk_widget_style_get (GTK_WIDGET (menu),
2584                         "horizontal-padding", &horizontal_padding,
2585                         NULL);
2586
2587   border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2588   avail_width -= (border_width + horizontal_padding + gtk_widget_get_style (widget)->xthickness) * 2;
2589
2590   for (children = menu_shell->children; children; children = children->next)
2591     {
2592       gint part;
2593       gint toggle_size;
2594       gint l, r, t, b;
2595       gint child_min, child_nat;
2596
2597       child = children->data;
2598       
2599       if (! gtk_widget_get_visible (child))
2600         continue;
2601
2602       get_effective_child_attach (child, &l, &r, &t, &b);
2603
2604       part = avail_width / (r - l);
2605
2606       gtk_size_request_get_height_for_width (GTK_SIZE_REQUEST (child), part,
2607                                              &child_min, &child_nat);
2608
2609       gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
2610       
2611       part = MAX (child_min, toggle_size) / (b - t);
2612       min_heights[t] = MAX (min_heights[t], part);
2613
2614       part = MAX (child_nat, toggle_size) / (b - t);
2615       nat_heights[t] = MAX (nat_heights[t], part);
2616     }
2617
2618   if (ret_min_heights)
2619     *ret_min_heights = min_heights;
2620   else
2621     g_free (min_heights);
2622
2623   if (ret_nat_heights)
2624     *ret_nat_heights = nat_heights;
2625   else
2626     g_free (nat_heights);
2627   
2628   return n_heights;
2629 }
2630
2631 static void
2632 gtk_menu_size_allocate (GtkWidget     *widget,
2633                         GtkAllocation *allocation)
2634 {
2635   GtkMenu *menu;
2636   GtkMenuShell *menu_shell;
2637   GtkWidget *child;
2638   GtkAllocation child_allocation;
2639   GtkMenuPrivate *priv;
2640   GtkStyle *style;
2641   GList *children;
2642   gint x, y, i;
2643   gint width, height;
2644   guint border_width;
2645   guint vertical_padding;
2646   guint horizontal_padding;
2647   
2648   g_return_if_fail (GTK_IS_MENU (widget));
2649   g_return_if_fail (allocation != NULL);
2650   
2651   menu = GTK_MENU (widget);
2652   menu_shell = GTK_MENU_SHELL (widget);
2653   priv = gtk_menu_get_private (menu);
2654
2655   gtk_widget_set_allocation (widget, allocation);
2656
2657   style = gtk_widget_get_style (widget);
2658
2659   gtk_widget_style_get (GTK_WIDGET (menu),
2660                         "vertical-padding", &vertical_padding,
2661                         "horizontal-padding", &horizontal_padding,
2662                         NULL);
2663   border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
2664
2665   g_free (priv->heights);
2666   priv->heights_length = 
2667     calculate_line_heights (menu,
2668                             allocation->width,
2669                             &priv->heights,
2670                             NULL);
2671
2672   /* refresh our cached height request */
2673   priv->requested_height = (border_width + vertical_padding + style->ythickness) * 2;
2674   for (i = 0; i < priv->heights_length; i++)
2675     priv->requested_height += priv->heights[i];
2676
2677   x = border_width + style->xthickness + horizontal_padding;
2678   y = border_width + style->ythickness + vertical_padding;
2679
2680   width = MAX (1, allocation->width - x * 2);
2681   height = MAX (1, allocation->height - y * 2);
2682
2683   if (menu_shell->active)
2684     gtk_menu_scroll_to (menu, menu->scroll_offset);
2685
2686   if (!menu->tearoff_active)
2687     {
2688       GtkBorder arrow_border;
2689
2690       get_arrows_border (menu, &arrow_border);
2691       y += arrow_border.top;
2692       height -= arrow_border.top;
2693       height -= arrow_border.bottom;
2694     }
2695
2696   if (gtk_widget_get_realized (widget))
2697     {
2698       gdk_window_move_resize (gtk_widget_get_window (widget),
2699                               allocation->x, allocation->y,
2700                               allocation->width, allocation->height);
2701
2702       gdk_window_move_resize (menu->view_window,
2703                               x,
2704                               y,
2705                               width,
2706                               height);
2707     }
2708
2709   if (menu_shell->children)
2710     {
2711       gint base_width = width / gtk_menu_get_n_columns (menu);
2712
2713       children = menu_shell->children;
2714       while (children)
2715         {
2716           child = children->data;
2717           children = children->next;
2718
2719           if (gtk_widget_get_visible (child))
2720             {
2721               gint i;
2722               gint l, r, t, b;
2723
2724               get_effective_child_attach (child, &l, &r, &t, &b);
2725
2726               if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
2727                 {
2728                   guint tmp;
2729                   tmp = gtk_menu_get_n_columns (menu) - l;
2730                   l = gtk_menu_get_n_columns (menu) - r;
2731                   r = tmp;
2732                 }
2733
2734               child_allocation.width = (r - l) * base_width;
2735               child_allocation.height = 0;
2736               child_allocation.x = l * base_width;
2737               child_allocation.y = 0;
2738
2739               for (i = 0; i < b; i++)
2740                 {
2741                   if (i < t)
2742                     child_allocation.y += priv->heights[i];
2743                   else
2744                     child_allocation.height += priv->heights[i];
2745                 }
2746
2747               gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
2748                                                   menu->toggle_size);
2749
2750               gtk_widget_size_allocate (child, &child_allocation);
2751               gtk_widget_queue_draw (child);
2752             }
2753         }
2754       
2755       /* Resize the item window */
2756       if (gtk_widget_get_realized (widget))
2757         {
2758           gint i;
2759           gint width, height;
2760
2761           height = 0;
2762           for (i = 0; i < gtk_menu_get_n_rows (menu); i++)
2763             height += priv->heights[i];
2764
2765           width = gtk_menu_get_n_columns (menu) * base_width;
2766           gdk_window_resize (menu->bin_window, width, height);
2767         }
2768
2769       if (menu->tearoff_active)
2770         {
2771           if (height >= priv->requested_height)
2772             {
2773               if (gtk_widget_get_visible (menu->tearoff_scrollbar))
2774                 {
2775                   gtk_widget_hide (menu->tearoff_scrollbar);
2776                   gtk_menu_set_tearoff_hints (menu, allocation->width);
2777
2778                   gtk_menu_scroll_to (menu, 0);
2779                 }
2780             }
2781           else
2782             {
2783               menu->tearoff_adjustment->upper = priv->requested_height;
2784               menu->tearoff_adjustment->page_size = allocation->height;
2785               
2786               if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
2787                   menu->tearoff_adjustment->upper)
2788                 {
2789                   gint value;
2790                   value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
2791                   if (value < 0)
2792                     value = 0;
2793                   gtk_menu_scroll_to (menu, value);
2794                 }
2795               
2796               gtk_adjustment_changed (menu->tearoff_adjustment);
2797               
2798               if (!gtk_widget_get_visible (menu->tearoff_scrollbar))
2799                 {
2800                   gtk_widget_show (menu->tearoff_scrollbar);
2801                   gtk_menu_set_tearoff_hints (menu, allocation->width);
2802                 }
2803             }
2804         }
2805     }
2806 }
2807
2808 static void
2809 get_arrows_visible_area (GtkMenu      *menu,
2810                          GdkRectangle *border,
2811                          GdkRectangle *upper,
2812                          GdkRectangle *lower,
2813                          gint         *arrow_space)
2814 {
2815   GtkArrowPlacement arrow_placement;
2816   GtkStyle *style;
2817   GtkWidget *widget = GTK_WIDGET (menu);
2818   guint border_width;
2819   guint vertical_padding;
2820   guint horizontal_padding;
2821   gint scroll_arrow_height;
2822
2823   style = gtk_widget_get_style (widget);
2824
2825   gtk_widget_style_get (widget,
2826                         "vertical-padding", &vertical_padding,
2827                         "horizontal-padding", &horizontal_padding,
2828                         "scroll-arrow-vlength", &scroll_arrow_height,
2829                         "arrow-placement", &arrow_placement,
2830                         NULL);
2831
2832   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
2833   border->x = border_width + style->xthickness + horizontal_padding;
2834   border->y = border_width + style->ythickness + vertical_padding;
2835   gdk_drawable_get_size (gtk_widget_get_window (widget),
2836                          &border->width, &border->height);
2837
2838   switch (arrow_placement)
2839     {
2840     case GTK_ARROWS_BOTH:
2841       upper->x = border->x;
2842       upper->y = border->y;
2843       upper->width = border->width - 2 * border->x;
2844       upper->height = scroll_arrow_height;
2845
2846       lower->x = border->x;
2847       lower->y = border->height - border->y - scroll_arrow_height;
2848       lower->width = border->width - 2 * border->x;
2849       lower->height = scroll_arrow_height;
2850       break;
2851
2852     case GTK_ARROWS_START:
2853       upper->x = border->x;
2854       upper->y = border->y;
2855       upper->width = (border->width - 2 * border->x) / 2;
2856       upper->height = scroll_arrow_height;
2857
2858       lower->x = border->x + upper->width;
2859       lower->y = border->y;
2860       lower->width = (border->width - 2 * border->x) / 2;
2861       lower->height = scroll_arrow_height;
2862       break;
2863
2864     case GTK_ARROWS_END:
2865       upper->x = border->x;
2866       upper->y = border->height - border->y - scroll_arrow_height;
2867       upper->width = (border->width - 2 * border->x) / 2;
2868       upper->height = scroll_arrow_height;
2869
2870       lower->x = border->x + upper->width;
2871       lower->y = border->height - border->y - scroll_arrow_height;
2872       lower->width = (border->width - 2 * border->x) / 2;
2873       lower->height = scroll_arrow_height;
2874       break;
2875
2876     default:
2877        g_assert_not_reached();
2878        upper->x = upper->y = upper->width = upper->height = 0;
2879        lower->x = lower->y = lower->width = lower->height = 0;
2880     }
2881
2882   *arrow_space = scroll_arrow_height - 2 * style->ythickness;
2883 }
2884
2885 static void
2886 gtk_menu_paint (GtkWidget      *widget,
2887                 GdkEventExpose *event)
2888 {
2889   GtkMenu *menu;
2890   GtkMenuPrivate *priv;
2891   GtkStyle *style;
2892   GdkRectangle border;
2893   GdkRectangle upper;
2894   GdkRectangle lower;
2895   GdkWindow *window;
2896   gint arrow_space;
2897   
2898   g_return_if_fail (GTK_IS_MENU (widget));
2899
2900   menu = GTK_MENU (widget);
2901   priv = gtk_menu_get_private (menu);
2902
2903   style = gtk_widget_get_style (widget);
2904   window = gtk_widget_get_window (widget);
2905
2906   get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
2907
2908   if (event->window == window)
2909     {
2910       gfloat arrow_scaling;
2911       gint arrow_size;
2912
2913       gtk_widget_style_get (widget, "arrow-scaling", &arrow_scaling, NULL);
2914       arrow_size = arrow_scaling * arrow_space;
2915
2916       gtk_paint_box (style,
2917                      window,
2918                      GTK_STATE_NORMAL,
2919                      GTK_SHADOW_OUT,
2920                      &event->area, widget, "menu",
2921                      0, 0, -1, -1);
2922
2923       if (menu->upper_arrow_visible && !menu->tearoff_active)
2924         {
2925           gtk_paint_box (style,
2926                          window,
2927                          priv->upper_arrow_state,
2928                          GTK_SHADOW_OUT,
2929                          &event->area, widget, "menu_scroll_arrow_up",
2930                          upper.x,
2931                          upper.y,
2932                          upper.width,
2933                          upper.height);
2934
2935           gtk_paint_arrow (style,
2936                            window,
2937                            priv->upper_arrow_state,
2938                            GTK_SHADOW_OUT,
2939                            &event->area, widget, "menu_scroll_arrow_up",
2940                            GTK_ARROW_UP,
2941                            TRUE,
2942                            upper.x + (upper.width - arrow_size) / 2,
2943                            upper.y + style->ythickness + (arrow_space - arrow_size) / 2,
2944                            arrow_size, arrow_size);
2945         }
2946
2947       if (menu->lower_arrow_visible && !menu->tearoff_active)
2948         {
2949           gtk_paint_box (style,
2950                          window,
2951                          priv->lower_arrow_state,
2952                          GTK_SHADOW_OUT,
2953                          &event->area, widget, "menu_scroll_arrow_down",
2954                          lower.x,
2955                          lower.y,
2956                          lower.width,
2957                          lower.height);
2958
2959           gtk_paint_arrow (style,
2960                            window,
2961                            priv->lower_arrow_state,
2962                            GTK_SHADOW_OUT,
2963                            &event->area, widget, "menu_scroll_arrow_down",
2964                            GTK_ARROW_DOWN,
2965                            TRUE,
2966                            lower.x + (lower.width - arrow_size) / 2,
2967                            lower.y + style->ythickness + (arrow_space - arrow_size) / 2,
2968                            arrow_size, arrow_size);
2969         }
2970     }
2971   else if (event->window == menu->bin_window)
2972     {
2973       gint y = -border.y + menu->scroll_offset;
2974
2975       if (!menu->tearoff_active)
2976         {
2977           GtkBorder arrow_border;
2978
2979           get_arrows_border (menu, &arrow_border);
2980           y -= arrow_border.top;
2981         }
2982
2983       gtk_paint_box (style,
2984                      menu->bin_window,
2985                      GTK_STATE_NORMAL,
2986                      GTK_SHADOW_OUT,
2987                      &event->area, widget, "menu",
2988                      - border.x, y,
2989                      border.width, border.height);
2990     }
2991 }
2992
2993 static gboolean
2994 gtk_menu_expose (GtkWidget      *widget,
2995                  GdkEventExpose *event)
2996 {
2997   g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
2998   g_return_val_if_fail (event != NULL, FALSE);
2999
3000   if (gtk_widget_is_drawable (widget))
3001     {
3002       gtk_menu_paint (widget, event);
3003
3004       GTK_WIDGET_CLASS (gtk_menu_parent_class)->expose_event (widget, event);
3005     }
3006   
3007   return FALSE;
3008 }
3009
3010 static void
3011 gtk_menu_show (GtkWidget *widget)
3012 {
3013   GtkMenu *menu = GTK_MENU (widget);
3014
3015   _gtk_menu_refresh_accel_paths (menu, FALSE);
3016
3017   GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
3018 }
3019
3020
3021
3022 static void 
3023 gtk_menu_size_request_init (GtkSizeRequestIface *iface)
3024 {
3025   iface->get_width            = gtk_menu_get_width;
3026   iface->get_height           = gtk_menu_get_height;
3027   iface->get_height_for_width = gtk_menu_get_height_for_width;
3028 }
3029
3030 static void 
3031 gtk_menu_get_width (GtkSizeRequest      *widget,
3032                     gint                *minimum_size,
3033                     gint                *natural_size)
3034 {
3035   GtkMenu        *menu;
3036   GtkMenuShell   *menu_shell;
3037   GtkMenuPrivate *priv;
3038   GtkStyle       *style;
3039   GtkWidget      *child;
3040   GList          *children;
3041   guint           max_toggle_size;
3042   guint           max_accel_width;
3043   guint           horizontal_padding;
3044   guint           border_width;
3045   gint            child_min, child_nat;
3046   gint            min_width, nat_width;
3047
3048   menu       = GTK_MENU (widget);
3049   menu_shell = GTK_MENU_SHELL (widget);
3050   priv       = gtk_menu_get_private (menu);
3051
3052   style = gtk_widget_get_style (GTK_WIDGET (widget));
3053
3054   min_width = nat_width = 0;
3055   
3056   max_toggle_size = 0;
3057   max_accel_width = 0;
3058   
3059   children = menu_shell->children;
3060   while (children)
3061     {
3062       gint part;
3063       gint toggle_size;
3064       gint l, r, t, b;
3065
3066       child = children->data;
3067       children = children->next;
3068       
3069       if (! gtk_widget_get_visible (child))
3070         continue;
3071
3072       get_effective_child_attach (child, &l, &r, &t, &b);
3073
3074       /* It's important to size_request the child
3075        * before doing the toggle size request, in
3076        * case the toggle size request depends on the size
3077        * request of a child of the child (e.g. for ImageMenuItem)
3078        */
3079
3080        GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
3081        gtk_size_request_get_width (GTK_SIZE_REQUEST (child), &child_min, &child_nat);
3082
3083        gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
3084        max_toggle_size = MAX (max_toggle_size, toggle_size);
3085        max_accel_width = MAX (max_accel_width,
3086                               GTK_MENU_ITEM (child)->accelerator_width);
3087
3088        part = child_min / (r - l);
3089        min_width = MAX (min_width, part);
3090
3091        part = child_nat / (r - l);
3092        nat_width = MAX (nat_width, part);
3093     }
3094
3095   /* If the menu doesn't include any images or check items
3096    * reserve the space so that all menus are consistent.
3097    * We only do this for 'ordinary' menus, not for combobox
3098    * menus or multi-column menus
3099    */
3100   if (max_toggle_size == 0 && 
3101       gtk_menu_get_n_columns (menu) == 1 &&
3102       !priv->no_toggle_size)
3103     {
3104       guint toggle_spacing;
3105       guint indicator_size;
3106
3107       gtk_style_get (style,
3108                      GTK_TYPE_CHECK_MENU_ITEM,
3109                      "toggle-spacing", &toggle_spacing,
3110                      "indicator-size", &indicator_size,
3111                      NULL);
3112
3113       max_toggle_size = indicator_size + toggle_spacing;
3114     }
3115
3116   min_width += 2 * max_toggle_size + max_accel_width;
3117   min_width *= gtk_menu_get_n_columns (menu);
3118
3119   nat_width += 2 * max_toggle_size + max_accel_width;
3120   nat_width *= gtk_menu_get_n_columns (menu);
3121
3122   gtk_widget_style_get (GTK_WIDGET (menu),
3123                         "horizontal-padding", &horizontal_padding,
3124                         NULL);
3125
3126   border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3127   min_width   += (border_width + horizontal_padding + style->xthickness) * 2;
3128   nat_width   += (border_width + horizontal_padding + style->xthickness) * 2;
3129   
3130   menu->toggle_size = max_toggle_size;
3131   priv->accel_size  = max_accel_width;
3132
3133   if (minimum_size)
3134     *minimum_size = min_width;
3135
3136   if (natural_size)
3137     *natural_size = nat_width;
3138
3139   /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
3140    */
3141   if (menu->tearoff_active)
3142     gtk_menu_set_tearoff_hints (menu, min_width);
3143 }
3144
3145 static void 
3146 gtk_menu_get_height (GtkSizeRequest      *widget,
3147                      gint                *minimum_size,
3148                      gint                *natural_size)
3149 {
3150   gint min_width;
3151
3152   /* Menus are height-for-width only, just return the height for the minimum width */
3153   GTK_SIZE_REQUEST_GET_IFACE (widget)->get_width (widget, &min_width, NULL);
3154   GTK_SIZE_REQUEST_GET_IFACE (widget)->get_height_for_width (widget, min_width, minimum_size, natural_size);
3155 }
3156
3157 static void
3158 gtk_menu_get_height_for_width (GtkSizeRequest      *widget,
3159                                gint                 for_size,
3160                                gint                *minimum_size,
3161                                gint                *natural_size)
3162 {
3163   GtkMenu        *menu = GTK_MENU (widget);
3164   GtkMenuPrivate *private = gtk_menu_get_private (menu);
3165   guint          *min_heights, *nat_heights;
3166   guint           vertical_padding, border_width;
3167   gint            n_heights, i;
3168   gint            min_height, nat_height;
3169
3170   gtk_widget_style_get (GTK_WIDGET (menu), "vertical-padding", &vertical_padding, NULL);
3171   border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
3172
3173   min_height = nat_height = (border_width + vertical_padding + gtk_widget_get_style (GTK_WIDGET (widget))->ythickness) * 2;
3174
3175   n_heights = 
3176     calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
3177
3178   for (i = 0; i < n_heights; i++)
3179     {
3180       min_height += min_heights[i];
3181       nat_height += nat_heights[i];
3182     }
3183
3184   if (private->have_position)
3185     {
3186       GdkScreen *screen = gtk_widget_get_screen (menu->toplevel);
3187       GdkRectangle monitor;
3188       
3189       gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
3190
3191       if (private->y + min_height > monitor.y + monitor.height)
3192         min_height = monitor.y + monitor.height - private->y;
3193
3194       if (private->y + nat_height > monitor.y + monitor.height)
3195         nat_height = monitor.y + monitor.height - private->y;
3196
3197       if (private->y < monitor.y)
3198         {
3199           min_height -= monitor.y - private->y;
3200           nat_height -= monitor.y - private->y;
3201         }
3202     }
3203
3204   if (minimum_size)
3205     *minimum_size = min_height;
3206
3207   if (natural_size)
3208     *natural_size = nat_height;
3209
3210   g_free (min_heights);
3211   g_free (nat_heights);
3212 }
3213
3214
3215
3216 static gboolean
3217 gtk_menu_button_scroll (GtkMenu        *menu,
3218                         GdkEventButton *event)
3219 {
3220   if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
3221     {
3222       gboolean touchscreen_mode;
3223
3224       g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3225                     "gtk-touchscreen-mode", &touchscreen_mode,
3226                     NULL);
3227
3228       if (touchscreen_mode)
3229         gtk_menu_handle_scrolling (menu,
3230                                    event->x_root, event->y_root,
3231                                    event->type == GDK_BUTTON_PRESS,
3232                                    FALSE);
3233
3234       return TRUE;
3235     }
3236
3237   return FALSE;
3238 }
3239
3240 static gboolean
3241 pointer_in_menu_window (GtkWidget *widget,
3242                         gdouble    x_root,
3243                         gdouble    y_root)
3244 {
3245   GtkAllocation allocation;
3246   GtkMenu *menu = GTK_MENU (widget);
3247
3248   if (gtk_widget_get_mapped (menu->toplevel))
3249     {
3250       GtkMenuShell *menu_shell;
3251       gint          window_x, window_y;
3252
3253       gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3254                                &window_x, &window_y);
3255
3256       gtk_widget_get_allocation (widget, &allocation);
3257       if (x_root >= window_x && x_root < window_x + allocation.width &&
3258           y_root >= window_y && y_root < window_y + allocation.height)
3259         return TRUE;
3260
3261       menu_shell = GTK_MENU_SHELL (widget);
3262
3263       if (GTK_IS_MENU (menu_shell->parent_menu_shell))
3264         return pointer_in_menu_window (menu_shell->parent_menu_shell,
3265                                        x_root, y_root);
3266     }
3267
3268   return FALSE;
3269 }
3270
3271 static gboolean
3272 gtk_menu_button_press (GtkWidget      *widget,
3273                        GdkEventButton *event)
3274 {
3275   if (event->type != GDK_BUTTON_PRESS)
3276     return FALSE;
3277
3278   /* Don't pass down to menu shell for presses over scroll arrows
3279    */
3280   if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3281     return TRUE;
3282
3283   /*  Don't pass down to menu shell if a non-menuitem part of the menu
3284    *  was clicked. The check for the event_widget being a GtkMenuShell
3285    *  works because we have the pointer grabbed on menu_shell->window
3286    *  with owner_events=TRUE, so all events that are either outside
3287    *  the menu or on its border are delivered relative to
3288    *  menu_shell->window.
3289    */
3290   if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3291       pointer_in_menu_window (widget, event->x_root, event->y_root))
3292     return TRUE;
3293
3294   return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
3295 }
3296
3297 static gboolean
3298 gtk_menu_button_release (GtkWidget      *widget,
3299                          GdkEventButton *event)
3300 {
3301   GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3302
3303   if (priv->ignore_button_release)
3304     {
3305       priv->ignore_button_release = FALSE;
3306       return FALSE;
3307     }
3308
3309   if (event->type != GDK_BUTTON_RELEASE)
3310     return FALSE;
3311
3312   /* Don't pass down to menu shell for releases over scroll arrows
3313    */
3314   if (gtk_menu_button_scroll (GTK_MENU (widget), event))
3315     return TRUE;
3316
3317   /*  Don't pass down to menu shell if a non-menuitem part of the menu
3318    *  was clicked (see comment in button_press()).
3319    */
3320   if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
3321       pointer_in_menu_window (widget, event->x_root, event->y_root))
3322     {
3323       /*  Ugly: make sure menu_shell->button gets reset to 0 when we
3324        *  bail out early here so it is in a consistent state for the
3325        *  next button_press/button_release in GtkMenuShell.
3326        *  See bug #449371.
3327        */
3328       if (GTK_MENU_SHELL (widget)->active)
3329         GTK_MENU_SHELL (widget)->button = 0;
3330
3331       return TRUE;
3332     }
3333
3334   return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
3335 }
3336
3337 static const gchar *
3338 get_accel_path (GtkWidget *menu_item,
3339                 gboolean  *locked)
3340 {
3341   const gchar *path;
3342   GtkWidget *label;
3343   GClosure *accel_closure;
3344   GtkAccelGroup *accel_group;    
3345
3346   path = _gtk_widget_get_accel_path (menu_item, locked);
3347   if (!path)
3348     {
3349       path = GTK_MENU_ITEM (menu_item)->accel_path;
3350       
3351       if (locked)
3352         {
3353           *locked = TRUE;
3354
3355           label = gtk_bin_get_child (GTK_BIN (menu_item));
3356           
3357           if (GTK_IS_ACCEL_LABEL (label))
3358             {
3359               g_object_get (label, 
3360                             "accel-closure", &accel_closure, 
3361                             NULL);
3362               if (accel_closure)
3363                 {
3364                   accel_group = gtk_accel_group_from_accel_closure (accel_closure);
3365                   
3366                   *locked = gtk_accel_group_get_is_locked (accel_group);
3367                 }
3368             }
3369         }
3370     }
3371
3372   return path;
3373 }
3374
3375 static gboolean
3376 gtk_menu_key_press (GtkWidget   *widget,
3377                     GdkEventKey *event)
3378 {
3379   GtkMenuShell *menu_shell;
3380   GtkMenu *menu;
3381   gboolean delete = FALSE;
3382   gboolean can_change_accels;
3383   gchar *accel = NULL;
3384   guint accel_key, accel_mods;
3385   GdkModifierType consumed_modifiers;
3386   GdkDisplay *display;
3387   
3388   g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
3389   g_return_val_if_fail (event != NULL, FALSE);
3390       
3391   menu_shell = GTK_MENU_SHELL (widget);
3392   menu = GTK_MENU (widget);
3393   
3394   gtk_menu_stop_navigating_submenu (menu);
3395
3396   if (GTK_WIDGET_CLASS (gtk_menu_parent_class)->key_press_event (widget, event))
3397     return TRUE;
3398
3399   display = gtk_widget_get_display (widget);
3400     
3401   g_object_get (gtk_widget_get_settings (widget),
3402                 "gtk-menu-bar-accel", &accel,
3403                 "gtk-can-change-accels", &can_change_accels,
3404                 NULL);
3405
3406   if (accel && *accel)
3407     {
3408       guint keyval = 0;
3409       GdkModifierType mods = 0;
3410       
3411       gtk_accelerator_parse (accel, &keyval, &mods);
3412
3413       if (keyval == 0)
3414         g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
3415
3416       /* FIXME this is wrong, needs to be in the global accel resolution
3417        * thing, to properly consider i18n etc., but that probably requires
3418        * AccelGroup changes etc.
3419        */
3420       if (event->keyval == keyval && (mods & event->state) == mods)
3421         {
3422           gtk_menu_shell_cancel (menu_shell);
3423           g_free (accel);
3424           return TRUE;
3425         }
3426     }
3427
3428   g_free (accel);
3429   
3430   switch (event->keyval)
3431     {
3432     case GDK_KEY_Delete:
3433     case GDK_KEY_KP_Delete:
3434     case GDK_KEY_BackSpace:
3435       delete = TRUE;
3436       break;
3437     default:
3438       break;
3439     }
3440
3441   /* Figure out what modifiers went into determining the key symbol */
3442   gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
3443                                        event->hardware_keycode, event->state, event->group,
3444                                        NULL, NULL, NULL, &consumed_modifiers);
3445
3446   accel_key = gdk_keyval_to_lower (event->keyval);
3447   accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
3448
3449   /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
3450    * We re-upper case when we match against the keyval, but display and save in caseless form.
3451    */
3452   if (accel_key != event->keyval)
3453     accel_mods |= GDK_SHIFT_MASK;
3454   
3455   /* Modify the accelerators */
3456   if (can_change_accels &&
3457       menu_shell->active_menu_item &&
3458       gtk_bin_get_child (GTK_BIN (menu_shell->active_menu_item)) && /* no separators */
3459       GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL &&  /* no submenus */
3460       (delete || gtk_accelerator_valid (accel_key, accel_mods)))
3461     {
3462       GtkWidget *menu_item = menu_shell->active_menu_item;
3463       gboolean locked, replace_accels = TRUE;
3464       const gchar *path;
3465
3466       path = get_accel_path (menu_item, &locked);
3467       if (!path || locked)
3468         {
3469           /* can't change accelerators on menu_items without paths
3470            * (basically, those items are accelerator-locked).
3471            */
3472           /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
3473           gtk_widget_error_bell (widget);
3474         }
3475       else
3476         {
3477           gboolean changed;
3478
3479           /* For the keys that act to delete the current setting, we delete
3480            * the current setting if there is one, otherwise, we set the
3481            * key as the accelerator.
3482            */
3483           if (delete)
3484             {
3485               GtkAccelKey key;
3486               
3487               if (gtk_accel_map_lookup_entry (path, &key) &&
3488                   (key.accel_key || key.accel_mods))
3489                 {
3490                   accel_key = 0;
3491                   accel_mods = 0;
3492                 }
3493             }
3494           changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
3495
3496           if (!changed)
3497             {
3498               /* we failed, probably because this key is in use and
3499                * locked already
3500                */
3501               /* g_print("failed to change\n"); */
3502               gtk_widget_error_bell (widget);
3503             }
3504         }
3505     }
3506   
3507   return TRUE;
3508 }
3509
3510 static gboolean
3511 check_threshold (GtkWidget *widget,
3512                  gint       start_x,
3513                  gint       start_y,
3514                  gint       x,
3515                  gint       y)
3516 {
3517 #define THRESHOLD 8
3518   
3519   return
3520     ABS (start_x - x) > THRESHOLD  ||
3521     ABS (start_y - y) > THRESHOLD;
3522 }
3523
3524 static gboolean
3525 definitely_within_item (GtkWidget *widget,
3526                         gint       x,
3527                         gint       y)
3528 {
3529   GdkWindow *window = GTK_MENU_ITEM (widget)->event_window;
3530   int w, h;
3531
3532   gdk_drawable_get_size (window, &w, &h);
3533   
3534   return
3535     check_threshold (widget, 0, 0, x, y) &&
3536     check_threshold (widget, w - 1, 0, x, y) &&
3537     check_threshold (widget, w - 1, h - 1, x, y) &&
3538     check_threshold (widget, 0, h - 1, x, y);
3539 }
3540
3541 static gboolean
3542 gtk_menu_has_navigation_triangle (GtkMenu *menu)
3543 {
3544   GtkMenuPrivate *priv;
3545
3546   priv = gtk_menu_get_private (menu);
3547
3548   return priv->navigation_height && priv->navigation_width;
3549 }
3550
3551 static gboolean
3552 gtk_menu_motion_notify (GtkWidget      *widget,
3553                         GdkEventMotion *event)
3554 {
3555   GtkWidget *menu_item;
3556   GtkMenu *menu;
3557   GtkMenuShell *menu_shell;
3558   GtkWidget *parent;
3559
3560   gboolean need_enter;
3561
3562   if (GTK_IS_MENU (widget))
3563     {
3564       GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
3565
3566       if (priv->ignore_button_release)
3567         priv->ignore_button_release = FALSE;
3568
3569       gtk_menu_handle_scrolling (GTK_MENU (widget), event->x_root, event->y_root,
3570                                  TRUE, TRUE);
3571     }
3572
3573   /* We received the event for one of two reasons:
3574    *
3575    * a) We are the active menu, and did gtk_grab_add()
3576    * b) The widget is a child of ours, and the event was propagated
3577    *
3578    * Since for computation of navigation regions, we want the menu which
3579    * is the parent of the menu item, for a), we need to find that menu,
3580    * which may be different from 'widget'.
3581    */
3582   menu_item = gtk_get_event_widget ((GdkEvent*) event);
3583   parent = gtk_widget_get_parent (menu_item);
3584   if (!GTK_IS_MENU_ITEM (menu_item) ||
3585       !GTK_IS_MENU (parent))
3586     return FALSE;
3587
3588   menu_shell = GTK_MENU_SHELL (parent);
3589   menu = GTK_MENU (menu_shell);
3590
3591   if (definitely_within_item (menu_item, event->x, event->y))
3592     menu_shell->activate_time = 0;
3593
3594   need_enter = (gtk_menu_has_navigation_triangle (menu) || menu_shell->ignore_enter);
3595
3596   /* Check to see if we are within an active submenu's navigation region
3597    */
3598   if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
3599     return TRUE; 
3600
3601   /* Make sure we pop down if we enter a non-selectable menu item, so we
3602    * don't show a submenu when the cursor is outside the stay-up triangle.
3603    */
3604   if (!_gtk_menu_item_is_selectable (menu_item))
3605     {
3606       /* We really want to deselect, but this gives the menushell code
3607        * a chance to do some bookkeeping about the menuitem.
3608        */
3609       gtk_menu_shell_select_item (menu_shell, menu_item);
3610       return FALSE;
3611     }
3612
3613   if (need_enter)
3614     {
3615       /* The menu is now sensitive to enter events on its items, but
3616        * was previously sensitive.  So we fake an enter event.
3617        */
3618       gint width, height;
3619       
3620       menu_shell->ignore_enter = FALSE; 
3621       
3622       gdk_drawable_get_size (event->window, &width, &height);
3623       if (event->x >= 0 && event->x < width &&
3624           event->y >= 0 && event->y < height)
3625         {
3626           GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
3627           gboolean result;
3628
3629           send_event->crossing.window = g_object_ref (event->window);
3630           send_event->crossing.time = event->time;
3631           send_event->crossing.send_event = TRUE;
3632           send_event->crossing.x_root = event->x_root;
3633           send_event->crossing.y_root = event->y_root;
3634           send_event->crossing.x = event->x;
3635           send_event->crossing.y = event->y;
3636           send_event->crossing.state = event->state;
3637           gdk_event_set_device (send_event, gdk_event_get_device ((GdkEvent *) event));
3638
3639           /* We send the event to 'widget', the currently active menu,
3640            * instead of 'menu', the menu that the pointer is in. This
3641            * will ensure that the event will be ignored unless the
3642            * menuitem is a child of the active menu or some parent
3643            * menu of the active menu.
3644            */
3645           result = gtk_widget_event (widget, send_event);
3646           gdk_event_free (send_event);
3647
3648           return result;
3649         }
3650     }
3651
3652   return FALSE;
3653 }
3654
3655 static gboolean
3656 get_double_arrows (GtkMenu *menu)
3657 {
3658   GtkMenuPrivate   *priv = gtk_menu_get_private (menu);
3659   gboolean          double_arrows;
3660   GtkArrowPlacement arrow_placement;
3661
3662   gtk_widget_style_get (GTK_WIDGET (menu),
3663                         "double-arrows", &double_arrows,
3664                         "arrow-placement", &arrow_placement,
3665                         NULL);
3666
3667   if (arrow_placement != GTK_ARROWS_BOTH)
3668     return TRUE;
3669
3670   return double_arrows || (priv->initially_pushed_in &&
3671                            menu->scroll_offset != 0);
3672 }
3673
3674 static void
3675 gtk_menu_scroll_by (GtkMenu *menu, 
3676                     gint     step)
3677 {
3678   GtkMenuPrivate *priv;
3679   GtkBorder arrow_border;
3680   GtkWidget *widget;
3681   gint offset;
3682   gint view_width, view_height;
3683   gboolean double_arrows;
3684
3685   widget = GTK_WIDGET (menu);
3686   offset = menu->scroll_offset + step;
3687   priv   = gtk_menu_get_private (menu);
3688
3689   get_arrows_border (menu, &arrow_border);
3690
3691   double_arrows = get_double_arrows (menu);
3692
3693   /* If we scroll upward and the non-visible top part
3694    * is smaller than the scroll arrow it would be
3695    * pretty stupid to show the arrow and taking more
3696    * screen space than just scrolling to the top.
3697    */
3698   if (!double_arrows)
3699     if ((step < 0) && (offset < arrow_border.top))
3700       offset = 0;
3701
3702   /* Don't scroll over the top if we weren't before: */
3703   if ((menu->scroll_offset >= 0) && (offset < 0))
3704     offset = 0;
3705
3706   gdk_drawable_get_size (gtk_widget_get_window (widget),
3707                          &view_width, &view_height);
3708
3709   if (menu->scroll_offset == 0 &&
3710       view_height >= priv->requested_height)
3711     return;
3712
3713   /* Don't scroll past the bottom if we weren't before: */
3714   if (menu->scroll_offset > 0)
3715     view_height -= arrow_border.top;
3716
3717   /* When both arrows are always shown, reduce
3718    * view height even more.
3719    */
3720   if (double_arrows)
3721     view_height -= arrow_border.bottom;
3722
3723   if ((menu->scroll_offset + view_height <= priv->requested_height) &&
3724       (offset + view_height > priv->requested_height))
3725     offset = priv->requested_height - view_height;
3726
3727   if (offset != menu->scroll_offset)
3728     gtk_menu_scroll_to (menu, offset);
3729 }
3730
3731 static void
3732 gtk_menu_do_timeout_scroll (GtkMenu  *menu,
3733                             gboolean  touchscreen_mode)
3734 {
3735   gboolean upper_visible;
3736   gboolean lower_visible;
3737
3738   upper_visible = menu->upper_arrow_visible;
3739   lower_visible = menu->lower_arrow_visible;
3740
3741   gtk_menu_scroll_by (menu, menu->scroll_step);
3742
3743   if (touchscreen_mode &&
3744       (upper_visible != menu->upper_arrow_visible ||
3745        lower_visible != menu->lower_arrow_visible))
3746     {
3747       /* We are about to hide a scroll arrow while the mouse is pressed,
3748        * this would cause the uncovered menu item to be activated on button
3749        * release. Therefore we need to ignore button release here
3750        */
3751       GTK_MENU_SHELL (menu)->ignore_enter = TRUE;
3752       gtk_menu_get_private (menu)->ignore_button_release = TRUE;
3753     }
3754 }
3755
3756 static gboolean
3757 gtk_menu_scroll_timeout (gpointer data)
3758 {
3759   GtkMenu  *menu;
3760   gboolean  touchscreen_mode;
3761
3762   menu = GTK_MENU (data);
3763
3764   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3765                 "gtk-touchscreen-mode", &touchscreen_mode,
3766                 NULL);
3767
3768   gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3769
3770   return TRUE;
3771 }
3772
3773 static gboolean
3774 gtk_menu_scroll_timeout_initial (gpointer data)
3775 {
3776   GtkMenu  *menu;
3777   guint     timeout;
3778   gboolean  touchscreen_mode;
3779
3780   menu = GTK_MENU (data);
3781
3782   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3783                 "gtk-timeout-repeat", &timeout,
3784                 "gtk-touchscreen-mode", &touchscreen_mode,
3785                 NULL);
3786
3787   gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3788
3789   gtk_menu_remove_scroll_timeout (menu);
3790
3791   menu->timeout_id = gdk_threads_add_timeout (timeout,
3792                                               gtk_menu_scroll_timeout,
3793                                               menu);
3794
3795   return FALSE;
3796 }
3797
3798 static void
3799 gtk_menu_start_scrolling (GtkMenu *menu)
3800 {
3801   guint    timeout;
3802   gboolean touchscreen_mode;
3803
3804   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3805                 "gtk-timeout-repeat", &timeout,
3806                 "gtk-touchscreen-mode", &touchscreen_mode,
3807                 NULL);
3808
3809   gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
3810
3811   menu->timeout_id = gdk_threads_add_timeout (timeout,
3812                                               gtk_menu_scroll_timeout_initial,
3813                                               menu);
3814 }
3815
3816 static gboolean
3817 gtk_menu_scroll (GtkWidget      *widget,
3818                  GdkEventScroll *event)
3819 {
3820   GtkMenu *menu = GTK_MENU (widget);
3821
3822   switch (event->direction)
3823     {
3824     case GDK_SCROLL_RIGHT:
3825     case GDK_SCROLL_DOWN:
3826       gtk_menu_scroll_by (menu, MENU_SCROLL_STEP2);
3827       break;
3828     case GDK_SCROLL_LEFT:
3829     case GDK_SCROLL_UP:
3830       gtk_menu_scroll_by (menu, - MENU_SCROLL_STEP2);
3831       break;
3832     }
3833
3834   return TRUE;
3835 }
3836
3837 static void
3838 get_arrows_sensitive_area (GtkMenu      *menu,
3839                            GdkRectangle *upper,
3840                            GdkRectangle *lower)
3841 {
3842   GtkArrowPlacement arrow_placement;
3843   GtkWidget *widget = GTK_WIDGET (menu);
3844   GdkWindow *window;
3845   gint width, height;
3846   guint border;
3847   guint vertical_padding;
3848   gint win_x, win_y;
3849   gint scroll_arrow_height;
3850
3851   window = gtk_widget_get_window (widget);
3852
3853   gdk_drawable_get_size (window, &width, &height);
3854
3855   gtk_widget_style_get (widget,
3856                         "vertical-padding", &vertical_padding,
3857                         "scroll-arrow-vlength", &scroll_arrow_height,
3858                         "arrow-placement", &arrow_placement,
3859                         NULL);
3860
3861   border = gtk_container_get_border_width (GTK_CONTAINER (menu)) +
3862            gtk_widget_get_style (widget)->ythickness + vertical_padding;
3863
3864   gdk_window_get_position (window, &win_x, &win_y);
3865
3866   switch (arrow_placement)
3867     {
3868     case GTK_ARROWS_BOTH:
3869       if (upper)
3870         {
3871           upper->x = win_x;
3872           upper->y = win_y;
3873           upper->width = width;
3874           upper->height = scroll_arrow_height + border;
3875         }
3876
3877       if (lower)
3878         {
3879           lower->x = win_x;
3880           lower->y = win_y + height - border - scroll_arrow_height;
3881           lower->width = width;
3882           lower->height = scroll_arrow_height + border;
3883         }
3884       break;
3885
3886     case GTK_ARROWS_START:
3887       if (upper)
3888         {
3889           upper->x = win_x;
3890           upper->y = win_y;
3891           upper->width = width / 2;
3892           upper->height = scroll_arrow_height + border;
3893         }
3894
3895       if (lower)
3896         {
3897           lower->x = win_x + width / 2;
3898           lower->y = win_y;
3899           lower->width = width / 2;
3900           lower->height = scroll_arrow_height + border;
3901         }
3902       break;
3903
3904     case GTK_ARROWS_END:
3905       if (upper)
3906         {
3907           upper->x = win_x;
3908           upper->y = win_y + height - border - scroll_arrow_height;
3909           upper->width = width / 2;
3910           upper->height = scroll_arrow_height + border;
3911         }
3912
3913       if (lower)
3914         {
3915           lower->x = win_x + width / 2;
3916           lower->y = win_y + height - border - scroll_arrow_height;
3917           lower->width = width / 2;
3918           lower->height = scroll_arrow_height + border;
3919         }
3920       break;
3921     }
3922 }
3923
3924
3925 static void
3926 gtk_menu_handle_scrolling (GtkMenu *menu,
3927                            gint     x,
3928                            gint     y,
3929                            gboolean enter,
3930                            gboolean motion)
3931 {
3932   GtkMenuShell *menu_shell;
3933   GtkMenuPrivate *priv;
3934   GdkRectangle rect;
3935   gboolean in_arrow;
3936   gboolean scroll_fast = FALSE;
3937   gint top_x, top_y;
3938   gboolean touchscreen_mode;
3939
3940   priv = gtk_menu_get_private (menu);
3941
3942   menu_shell = GTK_MENU_SHELL (menu);
3943
3944   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
3945                 "gtk-touchscreen-mode", &touchscreen_mode,
3946                 NULL);
3947
3948   gdk_window_get_position (gtk_widget_get_window (menu->toplevel),
3949                            &top_x, &top_y);
3950   x -= top_x;
3951   y -= top_y;
3952
3953   /*  upper arrow handling  */
3954
3955   get_arrows_sensitive_area (menu, &rect, NULL);
3956
3957   in_arrow = FALSE;
3958   if (menu->upper_arrow_visible && !menu->tearoff_active &&
3959       (x >= rect.x) && (x < rect.x + rect.width) &&
3960       (y >= rect.y) && (y < rect.y + rect.height))
3961     {
3962       in_arrow = TRUE;
3963     }
3964
3965   if (touchscreen_mode)
3966     menu->upper_arrow_prelight = in_arrow;
3967
3968   if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
3969     {
3970       gboolean arrow_pressed = FALSE;
3971
3972       if (menu->upper_arrow_visible && !menu->tearoff_active)
3973         {
3974           if (touchscreen_mode)
3975             {
3976               if (enter && menu->upper_arrow_prelight)
3977                 {
3978                   if (menu->timeout_id == 0)
3979                     {
3980                       /* Deselect the active item so that
3981                        * any submenus are popped down
3982                        */
3983                       gtk_menu_shell_deselect (menu_shell);
3984
3985                       gtk_menu_remove_scroll_timeout (menu);
3986                       menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
3987
3988                       if (!motion)
3989                         {
3990                           /* Only do stuff on click. */
3991                           gtk_menu_start_scrolling (menu);
3992                           arrow_pressed = TRUE;
3993                         }
3994                     }
3995                   else
3996                     {
3997                       arrow_pressed = TRUE;
3998                     }
3999                 }
4000               else if (!enter)
4001                 {
4002                   gtk_menu_stop_scrolling (menu);
4003                 }
4004             }
4005           else /* !touchscreen_mode */
4006             {
4007               scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
4008
4009               if (enter && in_arrow &&
4010                   (!menu->upper_arrow_prelight ||
4011                    menu->scroll_fast != scroll_fast))
4012                 {
4013                   menu->upper_arrow_prelight = TRUE;
4014                   menu->scroll_fast = scroll_fast;
4015
4016                   /* Deselect the active item so that
4017                    * any submenus are popped down
4018                    */
4019                   gtk_menu_shell_deselect (menu_shell);
4020
4021                   gtk_menu_remove_scroll_timeout (menu);
4022                   menu->scroll_step = scroll_fast ?
4023                     -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
4024
4025                   menu->timeout_id =
4026                     gdk_threads_add_timeout (scroll_fast ?
4027                                              MENU_SCROLL_TIMEOUT2 :
4028                                              MENU_SCROLL_TIMEOUT1,
4029                                              gtk_menu_scroll_timeout, menu);
4030                 }
4031               else if (!enter && !in_arrow && menu->upper_arrow_prelight)
4032                 {
4033                   gtk_menu_stop_scrolling (menu);
4034                 }
4035             }
4036         }
4037
4038       /*  gtk_menu_start_scrolling() might have hit the top of the
4039        *  menu, so check if the button isn't insensitive before
4040        *  changing it to something else.
4041        */
4042       if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
4043         {
4044           GtkStateType arrow_state = GTK_STATE_NORMAL;
4045
4046           if (arrow_pressed)
4047             arrow_state = GTK_STATE_ACTIVE;
4048           else if (menu->upper_arrow_prelight)
4049             arrow_state = GTK_STATE_PRELIGHT;
4050
4051           if (arrow_state != priv->upper_arrow_state)
4052             {
4053               priv->upper_arrow_state = arrow_state;
4054
4055               gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4056                                           &rect, FALSE);
4057             }
4058         }
4059     }
4060
4061   /*  lower arrow handling  */
4062
4063   get_arrows_sensitive_area (menu, NULL, &rect);
4064
4065   in_arrow = FALSE;
4066   if (menu->lower_arrow_visible && !menu->tearoff_active &&
4067       (x >= rect.x) && (x < rect.x + rect.width) &&
4068       (y >= rect.y) && (y < rect.y + rect.height))
4069     {
4070       in_arrow = TRUE;
4071     }
4072
4073   if (touchscreen_mode)
4074     menu->lower_arrow_prelight = in_arrow;
4075
4076   if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4077     {
4078       gboolean arrow_pressed = FALSE;
4079
4080       if (menu->lower_arrow_visible && !menu->tearoff_active)
4081         {
4082           if (touchscreen_mode)
4083             {
4084               if (enter && menu->lower_arrow_prelight)
4085                 {
4086                   if (menu->timeout_id == 0)
4087                     {
4088                       /* Deselect the active item so that
4089                        * any submenus are popped down
4090                        */
4091                       gtk_menu_shell_deselect (menu_shell);
4092
4093                       gtk_menu_remove_scroll_timeout (menu);
4094                       menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
4095
4096                       if (!motion)
4097                         {
4098                           /* Only do stuff on click. */
4099                           gtk_menu_start_scrolling (menu);
4100                           arrow_pressed = TRUE;
4101                         }
4102                     }
4103                   else
4104                     {
4105                       arrow_pressed = TRUE;
4106                     }
4107                 }
4108               else if (!enter)
4109                 {
4110                   gtk_menu_stop_scrolling (menu);
4111                 }
4112             }
4113           else /* !touchscreen_mode */
4114             {
4115               scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
4116
4117               if (enter && in_arrow &&
4118                   (!menu->lower_arrow_prelight ||
4119                    menu->scroll_fast != scroll_fast))
4120                 {
4121                   menu->lower_arrow_prelight = TRUE;
4122                   menu->scroll_fast = scroll_fast;
4123
4124                   /* Deselect the active item so that
4125                    * any submenus are popped down
4126                    */
4127                   gtk_menu_shell_deselect (menu_shell);
4128
4129                   gtk_menu_remove_scroll_timeout (menu);
4130                   menu->scroll_step = scroll_fast ?
4131                     MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
4132
4133                   menu->timeout_id =
4134                     gdk_threads_add_timeout (scroll_fast ?
4135                                              MENU_SCROLL_TIMEOUT2 :
4136                                              MENU_SCROLL_TIMEOUT1,
4137                                              gtk_menu_scroll_timeout, menu);
4138                 }
4139               else if (!enter && !in_arrow && menu->lower_arrow_prelight)
4140                 {
4141                   gtk_menu_stop_scrolling (menu);
4142                 }
4143             }
4144         }
4145
4146       /*  gtk_menu_start_scrolling() might have hit the bottom of the
4147        *  menu, so check if the button isn't insensitive before
4148        *  changing it to something else.
4149        */
4150       if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
4151         {
4152           GtkStateType arrow_state = GTK_STATE_NORMAL;
4153
4154           if (arrow_pressed)
4155             arrow_state = GTK_STATE_ACTIVE;
4156           else if (menu->lower_arrow_prelight)
4157             arrow_state = GTK_STATE_PRELIGHT;
4158
4159           if (arrow_state != priv->lower_arrow_state)
4160             {
4161               priv->lower_arrow_state = arrow_state;
4162
4163               gdk_window_invalidate_rect (gtk_widget_get_window (GTK_WIDGET (menu)),
4164                                           &rect, FALSE);
4165             }
4166         }
4167     }
4168 }
4169
4170 static gboolean
4171 gtk_menu_enter_notify (GtkWidget        *widget,
4172                        GdkEventCrossing *event)
4173 {
4174   GtkWidget *menu_item;
4175   GtkWidget *parent;
4176   gboolean   touchscreen_mode;
4177
4178   if (event->mode == GDK_CROSSING_GTK_GRAB ||
4179       event->mode == GDK_CROSSING_GTK_UNGRAB ||
4180       event->mode == GDK_CROSSING_STATE_CHANGED)
4181     return TRUE;
4182
4183   g_object_get (gtk_widget_get_settings (widget),
4184                 "gtk-touchscreen-mode", &touchscreen_mode,
4185                 NULL);
4186
4187   menu_item = gtk_get_event_widget ((GdkEvent*) event);
4188   if (GTK_IS_MENU (widget))
4189     {
4190       GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
4191
4192       if (!menu_shell->ignore_enter)
4193         gtk_menu_handle_scrolling (GTK_MENU (widget),
4194                                    event->x_root, event->y_root, TRUE, TRUE);
4195     }
4196
4197   if (!touchscreen_mode && GTK_IS_MENU_ITEM (menu_item))
4198     {
4199       GtkWidget *menu = gtk_widget_get_parent (menu_item);
4200       
4201       if (GTK_IS_MENU (menu))
4202         {
4203           GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu));
4204           GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
4205
4206           if (priv->seen_item_enter)
4207             {
4208               /* This is the second enter we see for an item
4209                * on this menu. This means a release should always
4210                * mean activate.
4211                */
4212               menu_shell->activate_time = 0;
4213             }
4214           else if ((event->detail != GDK_NOTIFY_NONLINEAR &&
4215                     event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL))
4216             {
4217               if (definitely_within_item (menu_item, event->x, event->y))
4218                 {
4219                   /* This is an actual user-enter (ie. not a pop-under)
4220                    * In this case, the user must either have entered
4221                    * sufficiently far enough into the item, or he must move
4222                    * far enough away from the enter point. (see
4223                    * gtk_menu_motion_notify())
4224                    */
4225                   menu_shell->activate_time = 0;
4226                 }
4227             }
4228             
4229           priv->seen_item_enter = TRUE;
4230         }
4231     }
4232   
4233   /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
4234    * will not correspond to the event widget's parent.  Check to see
4235    * if we are in the parent's navigation region.
4236    */
4237   parent = gtk_widget_get_parent (menu_item);
4238   if (GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (parent) &&
4239       gtk_menu_navigating_submenu (GTK_MENU (parent),
4240                                    event->x_root, event->y_root))
4241     return TRUE;
4242
4243   return GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (widget, event); 
4244 }
4245
4246 static gboolean
4247 gtk_menu_leave_notify (GtkWidget        *widget,
4248                        GdkEventCrossing *event)
4249 {
4250   GtkMenuShell *menu_shell;
4251   GtkMenu *menu;
4252   GtkMenuItem *menu_item;
4253   GtkWidget *event_widget;
4254
4255   if (event->mode == GDK_CROSSING_GTK_GRAB ||
4256       event->mode == GDK_CROSSING_GTK_UNGRAB ||
4257       event->mode == GDK_CROSSING_STATE_CHANGED)
4258     return TRUE;
4259
4260   menu = GTK_MENU (widget);
4261   menu_shell = GTK_MENU_SHELL (widget); 
4262   
4263   if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
4264     return TRUE; 
4265
4266   gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
4267
4268   event_widget = gtk_get_event_widget ((GdkEvent*) event);
4269   
4270   if (!GTK_IS_MENU_ITEM (event_widget))
4271     return TRUE;
4272   
4273   menu_item = GTK_MENU_ITEM (event_widget); 
4274
4275   /* Here we check to see if we're leaving an active menu item with a submenu, 
4276    * in which case we enter submenu navigation mode. 
4277    */
4278   if (menu_shell->active_menu_item != NULL
4279       && menu_item->submenu != NULL
4280       && menu_item->submenu_placement == GTK_LEFT_RIGHT)
4281     {
4282       if (GTK_MENU_SHELL (menu_item->submenu)->active)
4283         {
4284           gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
4285           return TRUE;
4286         }
4287       else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
4288         {
4289           /* We are leaving an active menu item with nonactive submenu.
4290            * Deselect it so we don't surprise the user with by popping
4291            * up a submenu _after_ he left the item.
4292            */
4293           gtk_menu_shell_deselect (menu_shell);
4294           return TRUE;
4295         }
4296     }
4297   
4298   return GTK_WIDGET_CLASS (gtk_menu_parent_class)->leave_notify_event (widget, event); 
4299 }
4300
4301 static void 
4302 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
4303 {
4304   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4305
4306   priv->navigation_x = 0;
4307   priv->navigation_y = 0;
4308   priv->navigation_width = 0;
4309   priv->navigation_height = 0;
4310
4311   if (menu->navigation_timeout)
4312     {
4313       g_source_remove (menu->navigation_timeout);
4314       menu->navigation_timeout = 0;
4315     }
4316 }
4317
4318 /* When the timeout is elapsed, the navigation region is destroyed
4319  * and the menuitem under the pointer (if any) is selected.
4320  */
4321 static gboolean
4322 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
4323 {
4324   GtkMenuPopdownData *popdown_data = user_data;
4325   GtkMenu *menu = popdown_data->menu;
4326   GdkWindow *child_window;
4327
4328   gtk_menu_stop_navigating_submenu (menu);
4329   
4330   if (gtk_widget_get_realized (GTK_WIDGET (menu)))
4331     {
4332       child_window = gdk_window_get_device_position (menu->bin_window,
4333                                                      popdown_data->device,
4334                                                      NULL, NULL, NULL);
4335
4336       if (child_window)
4337         {
4338           GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
4339
4340           send_event->crossing.window = g_object_ref (child_window);
4341           send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
4342           send_event->crossing.send_event = TRUE;
4343           gdk_event_set_device (send_event, popdown_data->device);
4344
4345           GTK_WIDGET_CLASS (gtk_menu_parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
4346
4347           gdk_event_free (send_event);
4348         }
4349     }
4350
4351   return FALSE; 
4352 }
4353
4354 static gboolean
4355 gtk_menu_navigating_submenu (GtkMenu *menu,
4356                              gint     event_x,
4357                              gint     event_y)
4358 {
4359   GtkMenuPrivate *priv;
4360   int width, height;
4361
4362   if (!gtk_menu_has_navigation_triangle (menu))
4363     return FALSE;
4364
4365   priv = gtk_menu_get_private (menu);
4366   width = priv->navigation_width;
4367   height = priv->navigation_height;
4368
4369   /* check if x/y are in the triangle spanned by the navigation parameters */
4370
4371   /* 1) Move the coordinates so the triangle starts at 0,0 */
4372   event_x -= priv->navigation_x;
4373   event_y -= priv->navigation_y;
4374
4375   /* 2) Ensure both legs move along the positive axis */
4376   if (width < 0)
4377     {
4378       event_x = -event_x;
4379       width = -width;
4380     }
4381   if (height < 0)
4382     {
4383       event_y = -event_y;
4384       height = -height;
4385     }
4386
4387   /* 3) Check that the given coordinate is inside the triangle. The formula
4388    * is a transformed form of this formula: x/w + y/h <= 1
4389    */
4390   if (event_x >= 0 && event_y >= 0 &&
4391       event_x * height + event_y * width <= width * height)
4392     {
4393       return TRUE;
4394     }
4395   else
4396     {
4397       gtk_menu_stop_navigating_submenu (menu);
4398       return FALSE;
4399     }
4400 }
4401
4402 static void
4403 gtk_menu_set_submenu_navigation_region (GtkMenu          *menu,
4404                                         GtkMenuItem      *menu_item,
4405                                         GdkEventCrossing *event)
4406 {
4407   gint submenu_left = 0;
4408   gint submenu_right = 0;
4409   gint submenu_top = 0;
4410   gint submenu_bottom = 0;
4411   gint width = 0;
4412   gint height = 0;
4413   GtkWidget *event_widget;
4414   GtkMenuPopdownData *popdown_data;
4415   GtkMenuPrivate *priv;
4416   GdkWindow *window;
4417
4418   g_return_if_fail (menu_item->submenu != NULL);
4419   g_return_if_fail (event != NULL);
4420   
4421   priv = gtk_menu_get_private (menu);
4422
4423   event_widget = gtk_get_event_widget ((GdkEvent*) event);
4424
4425   window = gtk_widget_get_window (menu_item->submenu);
4426   gdk_window_get_origin (window, &submenu_left, &submenu_top);
4427   gdk_drawable_get_size (window, &width, &height);
4428
4429   submenu_right = submenu_left + width;
4430   submenu_bottom = submenu_top + height;
4431
4432   gdk_drawable_get_size (gtk_widget_get_window (event_widget),
4433                          &width, &height);
4434
4435   if (event->x >= 0 && event->x < width)
4436     {
4437       gint popdown_delay;
4438       
4439       gtk_menu_stop_navigating_submenu (menu);
4440
4441       /* The navigation region is the triangle closest to the x/y
4442        * location of the rectangle. This is why the width or height
4443        * can be negative.
4444        */
4445
4446       if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
4447         {
4448           /* right */
4449           priv->navigation_x = submenu_left;
4450           priv->navigation_width = event->x_root - submenu_left;
4451         }
4452       else
4453         {
4454           /* left */
4455           priv->navigation_x = submenu_right;
4456           priv->navigation_width = event->x_root - submenu_right;
4457         }
4458
4459       if (event->y < 0)
4460         {
4461           /* top */
4462           priv->navigation_y = event->y_root;
4463           priv->navigation_height = submenu_top - event->y_root - NAVIGATION_REGION_OVERSHOOT;
4464
4465           if (priv->navigation_height >= 0)
4466             return;
4467         }
4468       else
4469         {
4470           /* bottom */
4471           priv->navigation_y = event->y_root;
4472           priv->navigation_height = submenu_bottom - event->y_root + NAVIGATION_REGION_OVERSHOOT;
4473
4474           if (priv->navigation_height <= 0)
4475             return;
4476         }
4477
4478       g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4479                     "gtk-menu-popdown-delay", &popdown_delay,
4480                     NULL);
4481
4482       popdown_data = g_new (GtkMenuPopdownData, 1);
4483       popdown_data->menu = menu;
4484       popdown_data->device = gdk_event_get_device ((GdkEvent *) event);
4485
4486       menu->navigation_timeout = gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
4487                                                                popdown_delay,
4488                                                                gtk_menu_stop_navigating_submenu_cb,
4489                                                                popdown_data,
4490                                                                (GDestroyNotify) g_free);
4491     }
4492 }
4493
4494 static void
4495 gtk_menu_deactivate (GtkMenuShell *menu_shell)
4496 {
4497   GtkWidget *parent;
4498   
4499   g_return_if_fail (GTK_IS_MENU (menu_shell));
4500   
4501   parent = menu_shell->parent_menu_shell;
4502   
4503   menu_shell->activate_time = 0;
4504   gtk_menu_popdown (GTK_MENU (menu_shell));
4505   
4506   if (parent)
4507     gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
4508 }
4509
4510 static void
4511 gtk_menu_position (GtkMenu *menu)
4512 {
4513   GtkWidget *widget;
4514   GtkRequisition requisition;
4515   GtkMenuPrivate *private;
4516   gint x, y;
4517   gint scroll_offset;
4518   gint menu_height;
4519   GdkScreen *screen;
4520   GdkScreen *pointer_screen;
4521   GdkRectangle monitor;
4522   GdkDevice *pointer;
4523
4524   g_return_if_fail (GTK_IS_MENU (menu));
4525
4526   widget = GTK_WIDGET (menu);
4527
4528   screen = gtk_widget_get_screen (widget);
4529   pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
4530   gdk_display_get_device_state (gdk_screen_get_display (screen),
4531                                 pointer, &pointer_screen, &x, &y, NULL);
4532
4533   /* Get the minimum height for minimum width to figure out 
4534    * the right place to popup the menu. 
4535    */
4536   gtk_size_request_get_size (GTK_SIZE_REQUEST (widget), &requisition, NULL);
4537
4538   if (pointer_screen != screen)
4539     {
4540       /* Pointer is on a different screen; roughly center the
4541        * menu on the screen. If someone was using multiscreen
4542        * + Xinerama together they'd probably want something
4543        * fancier; but that is likely to be vanishingly rare.
4544        */
4545       x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
4546       y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
4547     }
4548
4549   private = gtk_menu_get_private (menu);
4550   private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4551
4552   private->initially_pushed_in = FALSE;
4553
4554   /* Set the type hint here to allow custom position functions to set a different hint */
4555   if (!gtk_widget_get_visible (menu->toplevel))
4556     gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), GDK_WINDOW_TYPE_HINT_POPUP_MENU);
4557   
4558   if (menu->position_func)
4559     {
4560       (* menu->position_func) (menu, &x, &y, &private->initially_pushed_in,
4561                                menu->position_func_data);
4562
4563       if (private->monitor_num < 0) 
4564         private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
4565
4566       gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4567     }
4568   else
4569     {
4570       GtkStyle *style = gtk_widget_get_style (widget);
4571       gint space_left, space_right, space_above, space_below;
4572       gint needed_width;
4573       gint needed_height;
4574       gint xthickness = style->xthickness;
4575       gint ythickness = style->ythickness;
4576       gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
4577
4578       /* The placement of popup menus horizontally works like this (with
4579        * RTL in parentheses)
4580        *
4581        * - If there is enough room to the right (left) of the mouse cursor,
4582        *   position the menu there.
4583        * 
4584        * - Otherwise, if if there is enough room to the left (right) of the 
4585        *   mouse cursor, position the menu there.
4586        * 
4587        * - Otherwise if the menu is smaller than the monitor, position it
4588        *   on the side of the mouse cursor that has the most space available
4589        *
4590        * - Otherwise (if there is simply not enough room for the menu on the
4591        *   monitor), position it as far left (right) as possible.
4592        *
4593        * Positioning in the vertical direction is similar: first try below
4594        * mouse cursor, then above.
4595        */
4596       gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
4597
4598       space_left = x - monitor.x;
4599       space_right = monitor.x + monitor.width - x - 1;
4600       space_above = y - monitor.y;
4601       space_below = monitor.y + monitor.height - y - 1;
4602
4603       /* position horizontally */
4604
4605       /* the amount of space we need to position the menu. Note the
4606        * menu is offset "xthickness" pixels 
4607        */
4608       needed_width = requisition.width - xthickness;
4609
4610       if (needed_width <= space_left ||
4611           needed_width <= space_right)
4612         {
4613           if ((rtl  && needed_width <= space_left) ||
4614               (!rtl && needed_width >  space_right))
4615             {
4616               /* position left */
4617               x = x + xthickness - requisition.width + 1;
4618             }
4619           else
4620             {
4621               /* position right */
4622               x = x - xthickness;
4623             }
4624
4625           /* x is clamped on-screen further down */
4626         }
4627       else if (requisition.width <= monitor.width)
4628         {
4629           /* the menu is too big to fit on either side of the mouse
4630            * cursor, but smaller than the monitor. Position it on
4631            * the side that has the most space
4632            */
4633           if (space_left > space_right)
4634             {
4635               /* left justify */
4636               x = monitor.x;
4637             }
4638           else
4639             {
4640               /* right justify */
4641               x = monitor.x + monitor.width - requisition.width;
4642             }
4643         }
4644       else /* menu is simply too big for the monitor */
4645         {
4646           if (rtl)
4647             {
4648               /* right justify */
4649               x = monitor.x + monitor.width - requisition.width;
4650             }
4651           else
4652             {
4653               /* left justify */
4654               x = monitor.x;
4655             }
4656         }
4657
4658       /* Position vertically. The algorithm is the same as above, but
4659        * simpler because we don't have to take RTL into account.
4660        */
4661       needed_height = requisition.height - ythickness;
4662
4663       if (needed_height <= space_above ||
4664           needed_height <= space_below)
4665         {
4666           if (needed_height <= space_below)
4667             y = y - ythickness;
4668           else
4669             y = y + ythickness - requisition.height + 1;
4670           
4671           y = CLAMP (y, monitor.y,
4672                      monitor.y + monitor.height - requisition.height);
4673         }
4674       else if (needed_height > space_below && needed_height > space_above)
4675         {
4676           if (space_below >= space_above)
4677             y = monitor.y + monitor.height - requisition.height;
4678           else
4679             y = monitor.y;
4680         }
4681       else
4682         {
4683           y = monitor.y;
4684         }
4685     }
4686
4687   scroll_offset = 0;
4688
4689   if (private->initially_pushed_in)
4690     {
4691       menu_height = requisition.height;
4692
4693       if (y + menu_height > monitor.y + monitor.height)
4694         {
4695           scroll_offset -= y + menu_height - (monitor.y + monitor.height);
4696           y = (monitor.y + monitor.height) - menu_height;
4697         }
4698   
4699       if (y < monitor.y)
4700         {
4701           scroll_offset += monitor.y - y;
4702           y = monitor.y;
4703         }
4704     }
4705
4706   /* FIXME: should this be done in the various position_funcs ? */
4707   x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
4708  
4709   if (GTK_MENU_SHELL (menu)->active)
4710     {
4711       private->have_position = TRUE;
4712       private->x = x;
4713       private->y = y;
4714     }
4715   
4716   if (y + requisition.height > monitor.y + monitor.height)
4717     requisition.height = (monitor.y + monitor.height) - y;
4718   
4719   if (y < monitor.y)
4720     {
4721       scroll_offset += monitor.y - y;
4722       requisition.height -= monitor.y - y;
4723       y = monitor.y;
4724     }
4725
4726   if (scroll_offset > 0)
4727     {
4728       GtkBorder arrow_border;
4729
4730       get_arrows_border (menu, &arrow_border);
4731       scroll_offset += arrow_border.top;
4732     }
4733   
4734   gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window), 
4735                    x, y);
4736
4737   if (!GTK_MENU_SHELL (menu)->active)
4738     {
4739       gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
4740                          requisition.width, requisition.height);
4741     }
4742
4743   menu->scroll_offset = scroll_offset;
4744 }
4745
4746 static void
4747 gtk_menu_remove_scroll_timeout (GtkMenu *menu)
4748 {
4749   if (menu->timeout_id)
4750     {
4751       g_source_remove (menu->timeout_id);
4752       menu->timeout_id = 0;
4753     }
4754 }
4755
4756 static void
4757 gtk_menu_stop_scrolling (GtkMenu *menu)
4758 {
4759   gboolean touchscreen_mode;
4760
4761   gtk_menu_remove_scroll_timeout (menu);
4762
4763   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
4764                 "gtk-touchscreen-mode", &touchscreen_mode,
4765                 NULL);
4766
4767   if (!touchscreen_mode)
4768     {
4769       menu->upper_arrow_prelight = FALSE;
4770       menu->lower_arrow_prelight = FALSE;
4771     }
4772 }
4773
4774 static void
4775 gtk_menu_scroll_to (GtkMenu *menu,
4776                     gint    offset)
4777 {
4778   GtkMenuPrivate *priv;
4779   GtkAllocation allocation;
4780   GtkBorder arrow_border;
4781   GtkStyle *style;
4782   GtkWidget *widget;
4783   gint x, y;
4784   gint view_width, view_height;
4785   gint border_width;
4786   gint menu_height;
4787   guint vertical_padding;
4788   guint horizontal_padding;
4789   gboolean double_arrows;
4790
4791   widget = GTK_WIDGET (menu);
4792   priv   = gtk_menu_get_private (menu);
4793
4794   if (menu->tearoff_active &&
4795       menu->tearoff_adjustment &&
4796       (menu->tearoff_adjustment->value != offset))
4797     {
4798       menu->tearoff_adjustment->value =
4799         CLAMP (offset,
4800                0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
4801       gtk_adjustment_value_changed (menu->tearoff_adjustment);
4802     }
4803   
4804   /* Move/resize the viewport according to arrows: */
4805   gtk_widget_get_allocation (widget, &allocation);
4806   view_width = allocation.width;
4807   view_height = allocation.height;
4808
4809   style = gtk_widget_get_style (widget);
4810
4811   gtk_widget_style_get (GTK_WIDGET (menu),
4812                         "vertical-padding", &vertical_padding,
4813                         "horizontal-padding", &horizontal_padding,
4814                         NULL);
4815
4816   double_arrows = get_double_arrows (menu);
4817
4818   border_width = gtk_container_get_border_width (GTK_CONTAINER (menu));
4819
4820   view_width -= (border_width + style->xthickness + horizontal_padding) * 2;
4821   view_height -= (border_width + style->ythickness + vertical_padding) * 2;
4822   menu_height = priv->requested_height - (border_width + style->ythickness + vertical_padding) * 2;
4823
4824   x = border_width + style->xthickness + horizontal_padding;
4825   y = border_width + style->ythickness + vertical_padding;
4826
4827   if (double_arrows && !menu->tearoff_active)
4828     {
4829       if (view_height < menu_height               ||
4830           (offset > 0 && menu->scroll_offset > 0) ||
4831           (offset < 0 && menu->scroll_offset < 0))
4832         {
4833           GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4834           GtkStateType    upper_arrow_previous_state = priv->upper_arrow_state;
4835           GtkStateType    lower_arrow_previous_state = priv->lower_arrow_state;
4836
4837           if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
4838             gtk_widget_queue_draw (GTK_WIDGET (menu));
4839
4840           menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
4841
4842           get_arrows_border (menu, &arrow_border);
4843           y += arrow_border.top;
4844           view_height -= arrow_border.top;
4845           view_height -= arrow_border.bottom;
4846
4847           if (offset <= 0)
4848             priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
4849           else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4850             priv->upper_arrow_state = menu->upper_arrow_prelight ?
4851               GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4852
4853           if (offset >= menu_height - view_height)
4854             priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
4855           else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4856             priv->lower_arrow_state = menu->lower_arrow_prelight ?
4857               GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
4858
4859           if ((priv->upper_arrow_state != upper_arrow_previous_state) ||
4860               (priv->lower_arrow_state != lower_arrow_previous_state))
4861             gtk_widget_queue_draw (GTK_WIDGET (menu));
4862
4863           if (upper_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4864               priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
4865             {
4866               /* At the upper border, possibly remove timeout */
4867               if (menu->scroll_step < 0)
4868                 {
4869                   gtk_menu_stop_scrolling (menu);
4870                   gtk_widget_queue_draw (GTK_WIDGET (menu));
4871                 }
4872             }
4873
4874           if (lower_arrow_previous_state != GTK_STATE_INSENSITIVE &&
4875               priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
4876             {
4877               /* At the lower border, possibly remove timeout */
4878               if (menu->scroll_step > 0)
4879                 {
4880                   gtk_menu_stop_scrolling (menu);
4881                   gtk_widget_queue_draw (GTK_WIDGET (menu));
4882                 }
4883             }
4884         }
4885       else if (menu->upper_arrow_visible || menu->lower_arrow_visible)
4886         {
4887           offset = 0;
4888
4889           menu->upper_arrow_visible = menu->lower_arrow_visible = FALSE;
4890           menu->upper_arrow_prelight = menu->lower_arrow_prelight = FALSE;
4891
4892           gtk_menu_stop_scrolling (menu);
4893           gtk_widget_queue_draw (GTK_WIDGET (menu));
4894         }
4895     }
4896   else if (!menu->tearoff_active)
4897     {
4898       gboolean last_visible;
4899
4900       last_visible = menu->upper_arrow_visible;
4901       menu->upper_arrow_visible = offset > 0;
4902       
4903       /* upper_arrow_visible may have changed, so requery the border */
4904       get_arrows_border (menu, &arrow_border);
4905       view_height -= arrow_border.top;
4906       
4907       if ((last_visible != menu->upper_arrow_visible) &&
4908           !menu->upper_arrow_visible)
4909         {
4910           menu->upper_arrow_prelight = FALSE;
4911
4912           /* If we hid the upper arrow, possibly remove timeout */
4913           if (menu->scroll_step < 0)
4914             {
4915               gtk_menu_stop_scrolling (menu);
4916               gtk_widget_queue_draw (GTK_WIDGET (menu));
4917             }
4918         }
4919
4920       last_visible = menu->lower_arrow_visible;
4921       menu->lower_arrow_visible = offset < menu_height - view_height;
4922       
4923       /* lower_arrow_visible may have changed, so requery the border */
4924       get_arrows_border (menu, &arrow_border);
4925       view_height -= arrow_border.bottom;
4926       
4927       if ((last_visible != menu->lower_arrow_visible) &&
4928            !menu->lower_arrow_visible)
4929         {
4930           menu->lower_arrow_prelight = FALSE;
4931
4932           /* If we hid the lower arrow, possibly remove timeout */
4933           if (menu->scroll_step > 0)
4934             {
4935               gtk_menu_stop_scrolling (menu);
4936               gtk_widget_queue_draw (GTK_WIDGET (menu));
4937             }
4938         }
4939       
4940       y += arrow_border.top;
4941     }
4942
4943   /* Scroll the menu: */
4944   if (gtk_widget_get_realized (widget))
4945     gdk_window_move (menu->bin_window, 0, -offset);
4946
4947   if (gtk_widget_get_realized (widget))
4948     gdk_window_move_resize (menu->view_window,
4949                             x,
4950                             y,
4951                             view_width,
4952                             view_height);
4953
4954   menu->scroll_offset = offset;
4955 }
4956
4957 static gboolean
4958 compute_child_offset (GtkMenu   *menu,
4959                       GtkWidget *menu_item,
4960                       gint      *offset,
4961                       gint      *height,
4962                       gboolean  *is_last_child)
4963 {
4964   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
4965   gint item_top_attach;
4966   gint item_bottom_attach;
4967   gint child_offset = 0;
4968   gint i;
4969
4970   get_effective_child_attach (menu_item, NULL, NULL,
4971                               &item_top_attach, &item_bottom_attach);
4972
4973   /* there is a possibility that we get called before _size_request, so
4974    * check the height table for safety.
4975    */
4976   if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
4977     return FALSE;
4978
4979   /* when we have a row with only invisible children, it's height will
4980    * be zero, so there's no need to check WIDGET_VISIBLE here
4981    */
4982   for (i = 0; i < item_top_attach; i++)
4983     child_offset += priv->heights[i];
4984
4985   if (is_last_child)
4986     *is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
4987   if (offset)
4988     *offset = child_offset;
4989   if (height)
4990     *height = priv->heights[item_top_attach];
4991
4992   return TRUE;
4993 }
4994
4995 static void
4996 gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
4997                               GtkWidget    *menu_item)
4998 {
4999   GtkMenu *menu = GTK_MENU (menu_shell);
5000   GtkWidget *widget = GTK_WIDGET (menu_shell);
5001   gint child_offset, child_height;
5002   gint width, height;
5003   gint y;
5004   gint arrow_height;
5005   gboolean last_child = 0;
5006
5007   /* We need to check if the selected item fully visible.
5008    * If not we need to scroll the menu so that it becomes fully
5009    * visible.
5010    */
5011
5012   if (compute_child_offset (menu, menu_item,
5013                             &child_offset, &child_height, &last_child))
5014     {
5015       guint vertical_padding;
5016       gboolean double_arrows;
5017       
5018       y = menu->scroll_offset;
5019       gdk_drawable_get_size (gtk_widget_get_window (widget),
5020                              &width, &height);
5021
5022       gtk_widget_style_get (widget,
5023                             "vertical-padding", &vertical_padding,
5024                             NULL);
5025
5026       double_arrows = get_double_arrows (menu);
5027
5028       height -= 2 * gtk_container_get_border_width (GTK_CONTAINER (menu)) +
5029                 2 * gtk_widget_get_style (widget)->ythickness +
5030                 2 * vertical_padding;
5031       if (child_offset < y)
5032         {
5033           /* Ignore the enter event we might get if the pointer is on the menu
5034            */
5035           menu_shell->ignore_enter = TRUE;
5036           gtk_menu_scroll_to (menu, child_offset);
5037         }
5038       else
5039         {
5040           GtkBorder arrow_border;
5041
5042           arrow_height = 0;
5043
5044           get_arrows_border (menu, &arrow_border);
5045           if (!menu->tearoff_active)
5046             arrow_height = arrow_border.top + arrow_border.bottom;
5047           
5048           if (child_offset + child_height > y + height - arrow_height)
5049             {
5050               arrow_height = 0;
5051               if ((!last_child && !menu->tearoff_active) || double_arrows)
5052                 arrow_height += arrow_border.bottom;
5053
5054               y = child_offset + child_height - height + arrow_height;
5055               if (((y > 0) && !menu->tearoff_active) || double_arrows)
5056                 {
5057                   /* Need upper arrow */
5058                   arrow_height += arrow_border.top;
5059                   y = child_offset + child_height - height + arrow_height;
5060                 }
5061               /* Ignore the enter event we might get if the pointer is on the menu
5062                */
5063               menu_shell->ignore_enter = TRUE;
5064               gtk_menu_scroll_to (menu, y);
5065             }
5066         }    
5067       
5068     }
5069 }
5070
5071 static void
5072 gtk_menu_select_item (GtkMenuShell *menu_shell,
5073                       GtkWidget    *menu_item)
5074 {
5075   GtkMenu *menu = GTK_MENU (menu_shell);
5076
5077   if (gtk_widget_get_realized (GTK_WIDGET (menu)))
5078     gtk_menu_scroll_item_visible (menu_shell, menu_item);
5079
5080   GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
5081 }
5082
5083
5084 /* Reparent the menu, taking care of the refcounting
5085  *
5086  * If unrealize is true we force a unrealize while reparenting the parent.
5087  * This can help eliminate flicker in some cases.
5088  *
5089  * What happens is that when the menu is unrealized and then re-realized,
5090  * the allocations are as follows:
5091  *
5092  *  parent - 1x1 at (0,0) 
5093  *  child1 - 100x20 at (0,0)
5094  *  child2 - 100x20 at (0,20)
5095  *  child3 - 100x20 at (0,40)
5096  *
5097  * That is, the parent is small but the children are full sized. Then,
5098  * when the queued_resize gets processed, the parent gets resized to
5099  * full size. 
5100  *
5101  * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
5102  * contains the following logic:
5103  * 
5104  * - if a move or resize operation on a window would change the clip 
5105  *   region on the children, then before the window is resized
5106  *   the background for children is temporarily set to None, the
5107  *   move/resize done, and the background for the children restored.
5108  *
5109  * So, at the point where the parent is resized to final size, the
5110  * background for the children is temporarily None, and thus they
5111  * are not cleared to the background color and the previous background
5112  * (the image of the menu) is left in place.
5113  */
5114 static void 
5115 gtk_menu_reparent (GtkMenu   *menu,
5116                    GtkWidget *new_parent,
5117                    gboolean   unrealize)
5118 {
5119   GtkObject *object = GTK_OBJECT (menu);
5120   GtkWidget *widget = GTK_WIDGET (menu);
5121   gboolean was_floating = g_object_is_floating (object);
5122
5123   g_object_ref_sink (object);
5124
5125   if (unrealize)
5126     {
5127       g_object_ref (object);
5128       gtk_container_remove (GTK_CONTAINER (gtk_widget_get_parent (widget)), widget);
5129       gtk_container_add (GTK_CONTAINER (new_parent), widget);
5130       g_object_unref (object);
5131     }
5132   else
5133     gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
5134   
5135   if (was_floating)
5136     g_object_force_floating (G_OBJECT (object));
5137   else
5138     g_object_unref (object);
5139 }
5140
5141 static void
5142 gtk_menu_show_all (GtkWidget *widget)
5143 {
5144   /* Show children, but not self. */
5145   gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
5146 }
5147
5148
5149 static void
5150 gtk_menu_hide_all (GtkWidget *widget)
5151 {
5152   /* Hide children, but not self. */
5153   gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
5154 }
5155
5156 /**
5157  * gtk_menu_set_screen:
5158  * @menu: a #GtkMenu.
5159  * @screen: (allow-none): a #GdkScreen, or %NULL if the screen should be
5160  *          determined by the widget the menu is attached to.
5161  *
5162  * Sets the #GdkScreen on which the menu will be displayed.
5163  *
5164  * Since: 2.2
5165  **/
5166 void
5167 gtk_menu_set_screen (GtkMenu   *menu, 
5168                      GdkScreen *screen)
5169 {
5170   g_return_if_fail (GTK_IS_MENU (menu));
5171   g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
5172
5173   g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-screen"), screen);
5174
5175   if (screen)
5176     {
5177       menu_change_screen (menu, screen);
5178     }
5179   else
5180     {
5181       GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
5182       if (attach_widget)
5183         attach_widget_screen_changed (attach_widget, NULL, menu);
5184     }
5185 }
5186
5187 /**
5188  * gtk_menu_attach:
5189  * @menu: a #GtkMenu.
5190  * @child: a #GtkMenuItem.
5191  * @left_attach: The column number to attach the left side of the item to.
5192  * @right_attach: The column number to attach the right side of the item to.
5193  * @top_attach: The row number to attach the top of the item to.
5194  * @bottom_attach: The row number to attach the bottom of the item to.
5195  *
5196  * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
5197  * an item will occupy is specified by @left_attach, @right_attach,
5198  * @top_attach and @bottom_attach. These each represent the leftmost,
5199  * rightmost, uppermost and lower column and row numbers of the table.
5200  * (Columns and rows are indexed from zero).
5201  *
5202  * Note that this function is not related to gtk_menu_detach().
5203  *
5204  * Since: 2.4
5205  **/
5206 void
5207 gtk_menu_attach (GtkMenu   *menu,
5208                  GtkWidget *child,
5209                  guint      left_attach,
5210                  guint      right_attach,
5211                  guint      top_attach,
5212                  guint      bottom_attach)
5213 {
5214   GtkMenuShell *menu_shell;
5215   GtkWidget *parent;
5216
5217   g_return_if_fail (GTK_IS_MENU (menu));
5218   g_return_if_fail (GTK_IS_MENU_ITEM (child));
5219   parent = gtk_widget_get_parent (child);
5220   g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
5221   g_return_if_fail (left_attach < right_attach);
5222   g_return_if_fail (top_attach < bottom_attach);
5223
5224   menu_shell = GTK_MENU_SHELL (menu);
5225
5226   if (!parent)
5227     {
5228       AttachInfo *ai = get_attach_info (child);
5229       
5230       ai->left_attach = left_attach;
5231       ai->right_attach = right_attach;
5232       ai->top_attach = top_attach;
5233       ai->bottom_attach = bottom_attach;
5234       
5235       menu_shell->children = g_list_append (menu_shell->children, child);
5236
5237       gtk_widget_set_parent (child, GTK_WIDGET (menu));
5238
5239       menu_queue_resize (menu);
5240     }
5241   else
5242     {
5243       gtk_container_child_set (GTK_CONTAINER (parent), child,
5244                                "left-attach",   left_attach,
5245                                "right-attach",  right_attach,
5246                                "top-attach",    top_attach,
5247                                "bottom-attach", bottom_attach,
5248                                NULL);
5249     }
5250 }
5251
5252 static gint
5253 gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
5254 {
5255   gint popup_delay;
5256
5257   g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
5258                 "gtk-menu-popup-delay", &popup_delay,
5259                 NULL);
5260
5261   return popup_delay;
5262 }
5263
5264 static GtkWidget *
5265 find_child_containing (GtkMenuShell *menu_shell,
5266                        int           left,
5267                        int           right,
5268                        int           top,
5269                        int           bottom)
5270 {
5271   GList *list;
5272
5273   /* find a child which includes the area given by
5274    * left, right, top, bottom.
5275    */
5276
5277   for (list = menu_shell->children; list; list = list->next)
5278     {
5279       gint l, r, t, b;
5280
5281       if (!_gtk_menu_item_is_selectable (list->data))
5282         continue;
5283
5284       get_effective_child_attach (list->data, &l, &r, &t, &b);
5285
5286       if (l <= left && right <= r
5287           && t <= top && bottom <= b)
5288         return GTK_WIDGET (list->data);
5289     }
5290
5291   return NULL;
5292 }
5293
5294 static void
5295 gtk_menu_move_current (GtkMenuShell         *menu_shell,
5296                        GtkMenuDirectionType  direction)
5297 {
5298   GtkMenu *menu = GTK_MENU (menu_shell);
5299   gint i;
5300   gint l, r, t, b;
5301   GtkWidget *match = NULL;
5302
5303   if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
5304     {
5305       switch (direction)
5306         {
5307         case GTK_MENU_DIR_CHILD:
5308           direction = GTK_MENU_DIR_PARENT;
5309           break;
5310         case GTK_MENU_DIR_PARENT:
5311           direction = GTK_MENU_DIR_CHILD;
5312           break;
5313         default: ;
5314         }
5315     }
5316
5317   /* use special table menu key bindings */
5318   if (menu_shell->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
5319     {
5320       get_effective_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
5321
5322       if (direction == GTK_MENU_DIR_NEXT)
5323         {
5324           for (i = b; i < gtk_menu_get_n_rows (menu); i++)
5325             {
5326               match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
5327               if (match)
5328                 break;
5329             }
5330
5331           if (!match)
5332             {
5333               /* wrap around */
5334               for (i = 0; i < t; i++)
5335                 {
5336                   match = find_child_containing (menu_shell,
5337                                                  l, l + 1, i, i + 1);
5338                   if (match)
5339                     break;
5340                 }
5341             }
5342         }
5343       else if (direction == GTK_MENU_DIR_PREV)
5344         {
5345           for (i = t; i > 0; i--)
5346             {
5347               match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
5348               if (match)
5349                 break;
5350             }
5351
5352           if (!match)
5353             {
5354               /* wrap around */
5355               for (i = gtk_menu_get_n_rows (menu); i > b; i--)
5356                 {
5357                   match = find_child_containing (menu_shell,
5358                                                  l, l + 1, i - 1, i);
5359                   if (match)
5360                     break;
5361                 }
5362             }
5363         }
5364       else if (direction == GTK_MENU_DIR_PARENT)
5365         {
5366           /* we go one left if possible */
5367           if (l > 0)
5368             match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
5369
5370           if (!match)
5371             {
5372               GtkWidget *parent = menu_shell->parent_menu_shell;
5373
5374               if (!parent
5375                   || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
5376                 match = menu_shell->active_menu_item;
5377             }
5378         }
5379       else if (direction == GTK_MENU_DIR_CHILD)
5380         {
5381           /* we go one right if possible */
5382           if (r < gtk_menu_get_n_columns (menu))
5383             match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
5384
5385           if (!match)
5386             {
5387               GtkWidget *parent = menu_shell->parent_menu_shell;
5388
5389               if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
5390                   (!parent ||
5391                    g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
5392                 match = menu_shell->active_menu_item;
5393             }
5394         }
5395
5396       if (match)
5397         {
5398           gtk_menu_shell_select_item (menu_shell, match);
5399           return;
5400         }
5401     }
5402
5403   GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
5404 }
5405
5406 static gint
5407 get_visible_size (GtkMenu *menu)
5408 {
5409   GtkAllocation allocation;
5410   GtkWidget *widget = GTK_WIDGET (menu);
5411   GtkContainer *container = GTK_CONTAINER (menu);
5412   gint menu_height;
5413
5414   gtk_widget_get_allocation (widget, &allocation);
5415   menu_height = (allocation.height
5416                  - 2 * (gtk_container_get_border_width (container)
5417                         + gtk_widget_get_style (widget)->ythickness));
5418
5419   if (!menu->tearoff_active)
5420     {
5421       GtkBorder arrow_border;
5422
5423       get_arrows_border (menu, &arrow_border);
5424       menu_height -= arrow_border.top;
5425       menu_height -= arrow_border.bottom;
5426     }
5427   
5428   return menu_height;
5429 }
5430
5431 /* Find the sensitive on-screen child containing @y, or if none,
5432  * the nearest selectable onscreen child. (%NULL if none)
5433  */
5434 static GtkWidget *
5435 child_at (GtkMenu *menu,
5436           gint     y)
5437 {
5438   GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5439   GtkWidget *child = NULL;
5440   gint child_offset = 0;
5441   GList *children;
5442   gint menu_height;
5443   gint lower, upper;            /* Onscreen bounds */
5444
5445   menu_height = get_visible_size (menu);
5446   lower = menu->scroll_offset;
5447   upper = menu->scroll_offset + menu_height;
5448   
5449   for (children = menu_shell->children; children; children = children->next)
5450     {
5451       if (gtk_widget_get_visible (children->data))
5452         {
5453           GtkRequisition child_requisition;
5454
5455           gtk_widget_size_request (children->data, &child_requisition);
5456
5457           if (_gtk_menu_item_is_selectable (children->data) &&
5458               child_offset >= lower &&
5459               child_offset + child_requisition.height <= upper)
5460             {
5461               child = children->data;
5462               
5463               if (child_offset + child_requisition.height > y &&
5464                   !GTK_IS_TEAROFF_MENU_ITEM (child))
5465                 return child;
5466             }
5467       
5468           child_offset += child_requisition.height;
5469         }
5470     }
5471
5472   return child;
5473 }
5474
5475 static gint
5476 get_menu_height (GtkMenu *menu)
5477 {
5478   GtkAllocation allocation;
5479   GtkWidget *widget = GTK_WIDGET (menu);
5480   gint height;
5481
5482   gtk_widget_get_allocation (widget, &allocation);
5483
5484   height = allocation.height;
5485   height -= gtk_container_get_border_width (GTK_CONTAINER (widget) + gtk_widget_get_style (widget)->ythickness) * 2;
5486
5487   if (!menu->tearoff_active)
5488     {
5489       GtkBorder arrow_border;
5490
5491       get_arrows_border (menu, &arrow_border);
5492       height -= arrow_border.top;
5493       height -= arrow_border.bottom;
5494     }
5495
5496   return height;
5497 }
5498
5499 static void
5500 gtk_menu_real_move_scroll (GtkMenu       *menu,
5501                            GtkScrollType  type)
5502 {
5503   gint page_size = get_visible_size (menu);
5504   gint end_position = get_menu_height (menu);
5505   GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
5506   
5507   switch (type)
5508     {
5509     case GTK_SCROLL_PAGE_UP:
5510     case GTK_SCROLL_PAGE_DOWN:
5511       {
5512         gint old_offset;
5513         gint new_offset;
5514         gint child_offset = 0;
5515         gboolean old_upper_arrow_visible;
5516         gint step;
5517
5518         if (type == GTK_SCROLL_PAGE_UP)
5519           step = - page_size;
5520         else
5521           step = page_size;
5522
5523         if (menu_shell->active_menu_item)
5524           {
5525             gint child_height;
5526             
5527             compute_child_offset (menu, menu_shell->active_menu_item,
5528                                   &child_offset, &child_height, NULL);
5529             child_offset += child_height / 2;
5530           }
5531
5532         menu_shell->ignore_enter = TRUE;
5533         old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5534         old_offset = menu->scroll_offset;
5535
5536         new_offset = menu->scroll_offset + step;
5537         new_offset = CLAMP (new_offset, 0, end_position - page_size);
5538
5539         gtk_menu_scroll_to (menu, new_offset);
5540         
5541         if (menu_shell->active_menu_item)
5542           {
5543             GtkWidget *new_child;
5544             gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
5545             GtkBorder arrow_border;
5546
5547             get_arrows_border (menu, &arrow_border);
5548
5549             if (menu->scroll_offset != old_offset)
5550               step = menu->scroll_offset - old_offset;
5551
5552             step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
5553
5554             new_child = child_at (menu, child_offset + step);
5555             if (new_child)
5556               gtk_menu_shell_select_item (menu_shell, new_child);
5557           }
5558       }
5559       break;
5560     case GTK_SCROLL_START:
5561       /* Ignore the enter event we might get if the pointer is on the menu
5562        */
5563       menu_shell->ignore_enter = TRUE;
5564       gtk_menu_scroll_to (menu, 0);
5565       gtk_menu_shell_select_first (menu_shell, TRUE);
5566       break;
5567     case GTK_SCROLL_END:
5568       /* Ignore the enter event we might get if the pointer is on the menu
5569        */
5570       menu_shell->ignore_enter = TRUE;
5571       gtk_menu_scroll_to (menu, end_position - page_size);
5572       _gtk_menu_shell_select_last (menu_shell, TRUE);
5573       break;
5574     default:
5575       break;
5576     }
5577 }
5578
5579
5580 /**
5581  * gtk_menu_set_monitor:
5582  * @menu: a #GtkMenu
5583  * @monitor_num: the number of the monitor on which the menu should
5584  *    be popped up
5585  * 
5586  * Informs GTK+ on which monitor a menu should be popped up. 
5587  * See gdk_screen_get_monitor_geometry().
5588  *
5589  * This function should be called from a #GtkMenuPositionFunc if the
5590  * menu should not appear on the same monitor as the pointer. This 
5591  * information can't be reliably inferred from the coordinates returned
5592  * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates 
5593  * may extend beyond the monitor boundaries or even the screen boundaries. 
5594  *
5595  * Since: 2.4
5596  **/
5597 void
5598 gtk_menu_set_monitor (GtkMenu *menu,
5599                       gint     monitor_num)
5600 {
5601   GtkMenuPrivate *priv;
5602   g_return_if_fail (GTK_IS_MENU (menu));
5603
5604   priv = gtk_menu_get_private (menu);
5605   
5606   priv->monitor_num = monitor_num;
5607 }
5608
5609 /**
5610  * gtk_menu_get_monitor:
5611  * @menu: a #GtkMenu
5612  *
5613  * Retrieves the number of the monitor on which to show the menu.
5614  *
5615  * Returns: the number of the monitor on which the menu should
5616  *    be popped up or -1, if no monitor has been set
5617  *
5618  * Since: 2.14
5619  **/
5620 gint
5621 gtk_menu_get_monitor (GtkMenu *menu)
5622 {
5623   GtkMenuPrivate *priv;
5624   g_return_val_if_fail (GTK_IS_MENU (menu), -1);
5625
5626   priv = gtk_menu_get_private (menu);
5627   
5628   return priv->monitor_num;
5629 }
5630
5631 /**
5632  * gtk_menu_get_for_attach_widget:
5633  * @widget: a #GtkWidget
5634  *
5635  * Returns a list of the menus which are attached to this widget.
5636  * This list is owned by GTK+ and must not be modified.
5637  *
5638  * Return value: (element-type GtkWidget) (transfer none): the list of menus attached to his widget.
5639  *
5640  * Since: 2.6
5641  **/
5642 GList*
5643 gtk_menu_get_for_attach_widget (GtkWidget *widget)
5644 {
5645   GList *list;
5646   
5647   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5648   
5649   list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
5650   return list;
5651 }
5652
5653 static void
5654 gtk_menu_grab_notify (GtkWidget *widget,
5655                       gboolean   was_grabbed)
5656 {
5657   GtkWidget *toplevel;
5658   GtkWindowGroup *group;
5659   GtkWidget *grab;
5660   GdkDevice *pointer;
5661
5662   pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
5663
5664   if (!pointer ||
5665       !gtk_widget_device_is_shadowed (widget, pointer))
5666     return;
5667
5668   toplevel = gtk_widget_get_toplevel (widget);
5669
5670   if (!GTK_IS_WINDOW (toplevel))
5671     return;
5672
5673   group = gtk_window_get_group (GTK_WINDOW (toplevel));
5674   grab = gtk_window_group_get_current_device_grab (group, pointer);
5675
5676   if (GTK_MENU_SHELL (widget)->active && !GTK_IS_MENU_SHELL (grab))
5677     gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
5678 }
5679
5680 /**
5681  * gtk_menu_set_reserve_toggle_size:
5682  * @menu: a #GtkMenu
5683  * @reserve_toggle_size: whether to reserve size for toggles
5684  *
5685  * Sets whether the menu should reserve space for drawing toggles 
5686  * or icons, regardless of their actual presence.
5687  *
5688  * Since: 2.18
5689  */
5690 void
5691 gtk_menu_set_reserve_toggle_size (GtkMenu  *menu,
5692                                   gboolean  reserve_toggle_size)
5693 {
5694   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5695   gboolean no_toggle_size;
5696   
5697   no_toggle_size = !reserve_toggle_size;
5698
5699   if (priv->no_toggle_size != no_toggle_size)
5700     {
5701       priv->no_toggle_size = no_toggle_size;
5702
5703       g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
5704     }
5705 }
5706
5707 /**
5708  * gtk_menu_get_reserve_toggle_size:
5709  * @menu: a #GtkMenu
5710  *
5711  * Returns whether the menu reserves space for toggles and
5712  * icons, regardless of their actual presence.
5713  *
5714  * Returns: Whether the menu reserves toggle space
5715  *
5716  * Since: 2.18
5717  */
5718 gboolean
5719 gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
5720 {
5721   GtkMenuPrivate *priv = gtk_menu_get_private (menu);
5722
5723   return !priv->no_toggle_size;
5724 }