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