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