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