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