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