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