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