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