]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkmenu.c
Practically everything changed.
[~andy/gtk] / gtk / gtkmenu.c
index 64d1efc0b9823f813a711b221899e3990f265086..d9abe030c0a47480b60fe11307cea29471157cbf 100644 (file)
@@ -1,4 +1,4 @@
-/* GTK - The GIMP Toolkit
+/* GTK - The GTK+ Toolkit
  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  *
  * This library is free software; you can redistribute it and/or
@@ -25,7 +25,7 @@
  */
 
 #define GTK_MENU_INTERNALS
-#include <config.h>
+#include "config.h"
 #include <string.h> /* memset */
 #include "gdk/gdkkeysyms.h"
 #include "gtkaccellabel.h"
@@ -121,8 +121,13 @@ enum {
 
 enum {
   PROP_0,
+  PROP_ACTIVE,
+  PROP_ACCEL_GROUP,
+  PROP_ACCEL_PATH,
+  PROP_ATTACH_WIDGET,
   PROP_TEAROFF_STATE,
-  PROP_TEAROFF_TITLE
+  PROP_TEAROFF_TITLE,
+  PROP_MONITOR
 };
 
 enum {
@@ -152,7 +157,6 @@ static void     gtk_menu_get_child_property(GtkContainer     *container,
                                             GValue           *value,
                                             GParamSpec       *pspec);
 static void     gtk_menu_destroy           (GtkObject        *object);
-static void     gtk_menu_finalize          (GObject          *object);
 static void     gtk_menu_realize           (GtkWidget        *widget);
 static void     gtk_menu_unrealize         (GtkWidget        *widget);
 static void     gtk_menu_size_request      (GtkWidget        *widget,
@@ -265,6 +269,12 @@ menu_queue_resize (GtkMenu *menu)
   gtk_widget_queue_resize (GTK_WIDGET (menu));
 }
 
+static void
+attach_info_free (AttachInfo *info)
+{
+  g_slice_free (AttachInfo, info);
+}
+
 static AttachInfo *
 get_attach_info (GtkWidget *child)
 {
@@ -273,8 +283,9 @@ get_attach_info (GtkWidget *child)
 
   if (!ai)
     {
-      ai = g_new0 (AttachInfo, 1);
-      g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai, g_free);
+      ai = g_slice_new0 (AttachInfo);
+      g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
+                              (GDestroyNotify) attach_info_free);
     }
 
   return ai;
@@ -431,7 +442,6 @@ gtk_menu_class_init (GtkMenuClass *class)
   GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
   GtkBindingSet *binding_set;
   
-  gobject_class->finalize = gtk_menu_finalize;
   gobject_class->set_property = gtk_menu_set_property;
   gobject_class->get_property = gtk_menu_get_property;
 
@@ -477,13 +487,73 @@ gtk_menu_class_init (GtkMenuClass *class)
                             _gtk_marshal_VOID__ENUM,
                             G_TYPE_NONE, 1,
                             GTK_TYPE_SCROLL_TYPE);
-  
+
+  /**
+   * GtkMenu:active:
+   *
+   * The currently selected menu item.
+   *
+   * Since: 2.14
+   **/
+  g_object_class_install_property (gobject_class,
+                                   PROP_ACTIVE,
+                                   g_param_spec_uint ("active",
+                                                     P_("Active"),
+                                                     P_("The currently selected menu item"),
+                                                     0, G_MAXUINT, 0,
+                                                     GTK_PARAM_READWRITE));
+
+  /**
+   * GtkMenu:accel-group:
+   *
+   * The accel group holding accelerators for the menu.
+   *
+   * Since: 2.14
+   **/
+  g_object_class_install_property (gobject_class,
+                                   PROP_ACCEL_GROUP,
+                                   g_param_spec_object ("accel-group",
+                                                       P_("Accel Group"),
+                                                       P_("The accel group holding accelerators for the menu"),
+                                                       GTK_TYPE_ACCEL_GROUP,
+                                                       GTK_PARAM_READWRITE));
+
+  /**
+   * GtkMenu:accel-path:
+   *
+   * An accel path used to conveniently construct accel paths of child items.
+   *
+   * Since: 2.14
+   **/
+  g_object_class_install_property (gobject_class,
+                                   PROP_ACCEL_PATH,
+                                   g_param_spec_string ("accel-path",
+                                                       P_("Accel Path"),
+                                                       P_("An accel path used to conveniently construct accel paths of child items"),
+                                                       NULL,
+                                                       GTK_PARAM_READWRITE));
+
+  /**
+   * GtkMenu:attach-widget:
+   *
+   * The widget the menu is attached to.
+   *
+   * Since: 2.14
+   **/
+  g_object_class_install_property (gobject_class,
+                                   PROP_ACCEL_PATH,
+                                   g_param_spec_string ("attach-widget",
+                                                       P_("Attach Widget"),
+                                                       P_("The widget the menu is attached to"),
+                                                       NULL,
+                                                       GTK_PARAM_READWRITE));
+
   g_object_class_install_property (gobject_class,
                                    PROP_TEAROFF_TITLE,
                                    g_param_spec_string ("tearoff-title",
                                                         P_("Tearoff Title"),
                                                         P_("A title that may be displayed by the window manager when this menu is torn-off"),
-                                                        "",
+                                                        NULL,
                                                         GTK_PARAM_READWRITE));
 
   /**
@@ -501,6 +571,21 @@ gtk_menu_class_init (GtkMenuClass *class)
                                                         FALSE,
                                                         GTK_PARAM_READWRITE));
 
+  /**
+   * GtkMenu:monitor:
+   *
+   * The monitor the menu will be popped up on.
+   *
+   * Since: 2.14
+   **/
+  g_object_class_install_property (gobject_class,
+                                   PROP_MONITOR,
+                                   g_param_spec_int ("monitor",
+                                                    P_("Monitor"),
+                                                    P_("The monitor the menu will be popped up on"),
+                                                    -1, G_MAXINT, -1,
+                                                    GTK_PARAM_READWRITE));
+
   gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_int ("vertical-padding",
                                                             P_("Vertical Padding"),
@@ -697,12 +782,27 @@ gtk_menu_set_property (GObject      *object,
   
   switch (prop_id)
     {
+    case PROP_ACTIVE:
+      gtk_menu_set_active (menu, g_value_get_uint (value));
+      break;
+    case PROP_ACCEL_GROUP:
+      gtk_menu_set_accel_group (menu, g_value_get_object (value));
+      break;
+    case PROP_ACCEL_PATH:
+      gtk_menu_set_accel_path (menu, g_value_get_string (value));
+      break;
+    case PROP_ATTACH_WIDGET:
+      gtk_menu_attach (menu, g_value_get_object (value), 0, 0, 0, 0);
+      break;
     case PROP_TEAROFF_STATE:
       gtk_menu_set_tearoff_state (menu, g_value_get_boolean (value));
       break;
     case PROP_TEAROFF_TITLE:
       gtk_menu_set_title (menu, g_value_get_string (value));
-      break;     
+      break;
+    case PROP_MONITOR:
+      gtk_menu_set_monitor (menu, g_value_get_int (value));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -721,12 +821,27 @@ gtk_menu_get_property (GObject     *object,
   
   switch (prop_id)
     {
+    case PROP_ACTIVE:
+      g_value_set_uint (value, g_list_index (GTK_MENU_SHELL (menu)->children, gtk_menu_get_active (menu)));
+      break;
+    case PROP_ACCEL_GROUP:
+      g_value_set_object (value, gtk_menu_get_accel_group (menu));
+      break;
+    case PROP_ACCEL_PATH:
+      g_value_set_string (value, gtk_menu_get_accel_path (menu));
+      break;
+    case PROP_ATTACH_WIDGET:
+      g_value_set_object (value, gtk_menu_get_attach_widget (menu));
+      break;
     case PROP_TEAROFF_STATE:
       g_value_set_boolean (value, gtk_menu_get_tearoff_state (menu));
       break;
     case PROP_TEAROFF_TITLE:
       g_value_set_string (value, gtk_menu_get_title (menu));
       break;
+    case PROP_MONITOR:
+      g_value_set_int (value, gtk_menu_get_monitor (menu));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
       break;
@@ -902,15 +1017,11 @@ gtk_menu_init (GtkMenu *menu)
 static void
 gtk_menu_destroy (GtkObject *object)
 {
-  GtkMenu *menu;
+  GtkMenu *menu = GTK_MENU (object);
   GtkMenuAttachData *data;
   GtkMenuPrivate *priv; 
 
-  g_return_if_fail (GTK_IS_MENU (object));
-
-  menu = GTK_MENU (object);
-
-  gtk_menu_stop_scrolling (menu);
+  gtk_menu_remove_scroll_timeout (menu);
   
   data = g_object_get_data (G_OBJECT (object), attach_data_key);
   if (data)
@@ -960,16 +1071,6 @@ gtk_menu_destroy (GtkObject *object)
   GTK_OBJECT_CLASS (gtk_menu_parent_class)->destroy (object);
 }
 
-static void
-gtk_menu_finalize (GObject *object)
-{
-  GtkMenu *menu = GTK_MENU (object);
-
-  g_free (menu->accel_path);
-  
-  G_OBJECT_CLASS (gtk_menu_parent_class)->finalize (object);
-}
-
 static void
 menu_change_screen (GtkMenu   *menu,
                    GdkScreen *new_screen)
@@ -1028,7 +1129,7 @@ gtk_menu_attach_to_widget (GtkMenu               *menu,
   
   g_object_ref_sink (menu);
   
-  data = g_new (GtkMenuAttachData, 1);
+  data = g_slice_new (GtkMenuAttachData);
   data->attach_widget = attach_widget;
   
   g_signal_connect (attach_widget, "screen_changed",
@@ -1042,8 +1143,9 @@ gtk_menu_attach_to_widget (GtkMenu               *menu,
     {
       list = g_list_prepend (list, menu);
     }
-  g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list, (GtkDestroyNotify) g_list_free);
-  
+  g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
+                          (GDestroyNotify) g_list_free);
+
   if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
     gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
   
@@ -1095,14 +1197,15 @@ gtk_menu_detach (GtkMenu *menu)
   list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
   list = g_list_remove (list, menu);
   if (list)
-    g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list, (GtkDestroyNotify) g_list_free);
+    g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
+                            (GDestroyNotify) g_list_free);
   else
     g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
   
   if (GTK_WIDGET_REALIZED (menu))
     gtk_widget_unrealize (GTK_WIDGET (menu));
   
-  g_free (data);
+  g_slice_free (GtkMenuAttachData, data);
   
   /* Fallback title for menu comes from attach widget */
   gtk_menu_update_title (menu);
@@ -1114,13 +1217,10 @@ static void
 gtk_menu_remove (GtkContainer *container,
                 GtkWidget    *widget)
 {
-  GtkMenu *menu;
+  GtkMenu *menu = GTK_MENU (container);
 
-  g_return_if_fail (GTK_IS_MENU (container));
   g_return_if_fail (GTK_IS_MENU_ITEM (widget));
 
-  menu = GTK_MENU (container);
-
   /* Clear out old_active_menu_item if it matches the item we are removing
    */
   if (menu->old_active_menu_item == widget)
@@ -1251,10 +1351,12 @@ popup_grab_on_window (GdkWindow *window,
  * a mouse button press, such as a mouse button release or a keypress,
  * @button should be 0.
  *
- * The @activate_time parameter should be the time stamp of the event that
- * initiated the popup. If such an event is not available, use
- * gtk_get_current_event_time() instead.
- *
+ * The @activate_time parameter is used to conflict-resolve initiation of
+ * concurrent requests for mouse/keyboard grab requests. To function
+ * properly, this needs to be the time stamp of the user event (such as
+ * a mouse click or key press) that caused the initiation of the popup.
+ * Only if no such event is available, gtk_get_current_event_time() can
+ * be used instead.
  */
 void
 gtk_menu_popup (GtkMenu                    *menu,
@@ -1391,7 +1493,7 @@ gtk_menu_popup (GtkMenu               *menu,
     }
 
   /* Set transient for to get the right window group and parent relationship */
-  if (parent_toplevel && GTK_IS_WINDOW (parent_toplevel))
+  if (GTK_IS_WINDOW (parent_toplevel))
     gtk_window_set_transient_for (GTK_WINDOW (menu->toplevel),
                                  GTK_WINDOW (parent_toplevel));
   
@@ -1429,6 +1531,19 @@ gtk_menu_popup (GtkMenu              *menu,
 
   gtk_menu_scroll_to (menu, menu->scroll_offset);
 
+  /* if no item is selected, select the first one */
+  if (!menu_shell->active_menu_item)
+    {
+      gboolean touchscreen_mode;
+
+      g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
+                    "gtk-touchscreen-mode", &touchscreen_mode,
+                    NULL);
+
+      if (touchscreen_mode)
+        gtk_menu_shell_select_first (menu_shell, TRUE);
+    }
+
   /* Once everything is set up correctly, map the toplevel window on
      the screen.
    */
@@ -1631,6 +1746,10 @@ gtk_menu_real_can_activate_accel (GtkWidget *widget,
  * Assigning accel paths to menu items then enables the user to change
  * their accelerators at runtime. More details about accelerator paths
  * and their default setups can be found at gtk_accel_map_add_entry().
+ * 
+ * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
+ * pass a static string, you can save some memory by interning it first with 
+ * g_intern_static_string().
  */
 void
 gtk_menu_set_accel_path (GtkMenu     *menu,
@@ -1640,12 +1759,28 @@ gtk_menu_set_accel_path (GtkMenu     *menu,
   if (accel_path)
     g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
 
-  g_free (menu->accel_path);
-  menu->accel_path = g_strdup (accel_path);
+  /* FIXME: accel_path should be defined as const gchar* */
+  menu->accel_path = (gchar*)g_intern_string (accel_path);
   if (menu->accel_path)
     _gtk_menu_refresh_accel_paths (menu, FALSE);
 }
 
+/**
+ * gtk_menu_get_accel_path
+ * @menu: a valid #GtkMenu
+ *
+ * Retrieves the accelerator path set on the menu.
+ *
+ * Since: 2.14
+ */
+const gchar*
+gtk_menu_get_accel_path (GtkMenu     *menu)
+{
+  g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
+
+  return menu->accel_path;
+}
+
 typedef struct {
   GtkMenu *menu;
   gboolean group_changed;
@@ -1804,7 +1939,7 @@ gtk_menu_set_tearoff_state (GtkMenu  *menu,
            {
              GtkWidget *toplevel;
 
-             menu->tearoff_window = gtk_widget_new (GTK_TYPE_WINDOW,
+             menu->tearoff_window = g_object_new (GTK_TYPE_WINDOW,
                                                     "type", GTK_WINDOW_TOPLEVEL,
                                                     "screen", gtk_widget_get_screen (menu->toplevel),
                                                     "app-paintable", TRUE,
@@ -1913,26 +2048,27 @@ gtk_menu_get_tearoff_state (GtkMenu *menu)
  * @title: a string containing the title for the menu.
  * 
  * Sets the title string for the menu.  The title is displayed when the menu
- * is shown as a tearoff menu.
+ * is shown as a tearoff menu.  If @title is %NULL, the menu will see if it is
+ * attached to a parent menu item, and if so it will try to use the same text as
+ * that menu item's label.
  **/
-void       
+void
 gtk_menu_set_title (GtkMenu     *menu,
                    const gchar *title)
 {
   GtkMenuPrivate *priv;
+  char *old_title;
 
   g_return_if_fail (GTK_IS_MENU (menu));
 
   priv = gtk_menu_get_private (menu);
 
-  if (strcmp (title ? title : "", priv->title ? priv->title : "") != 0)
-    {
-      g_free (priv->title);
-      priv->title = g_strdup (title);
+  old_title = priv->title;
+  priv->title = g_strdup (title);
+  g_free (old_title);
        
-      gtk_menu_update_title (menu);
-      g_object_notify (G_OBJECT (menu), "tearoff-title");
-    }
+  gtk_menu_update_title (menu);
+  g_object_notify (G_OBJECT (menu), "tearoff-title");
 }
 
 /**
@@ -1992,6 +2128,21 @@ gtk_menu_style_set (GtkWidget *widget,
     }
 }
 
+static void
+get_arrows_border (GtkMenu *menu, GtkBorder *border)
+{
+  guint scroll_arrow_height;
+
+  gtk_widget_style_get (GTK_WIDGET (menu),
+                        "scroll-arrow-vlength", &scroll_arrow_height,
+                        NULL);
+
+  border->top = menu->upper_arrow_visible ? scroll_arrow_height : 0;
+  border->bottom = menu->lower_arrow_visible ? scroll_arrow_height : 0;
+
+  border->left = border->right = 0;
+}
+
 static void
 gtk_menu_realize (GtkWidget *widget)
 {
@@ -2003,7 +2154,7 @@ gtk_menu_realize (GtkWidget *widget)
   GList *children;
   guint vertical_padding;
   guint horizontal_padding;
-  guint scroll_arrow_height;
+  GtkBorder arrow_border;
 
   g_return_if_fail (GTK_IS_MENU (widget));
 
@@ -2034,7 +2185,6 @@ gtk_menu_realize (GtkWidget *widget)
   gtk_widget_style_get (GTK_WIDGET (menu),
                        "vertical-padding", &vertical_padding,
                         "horizontal-padding", &horizontal_padding,
-                        "scroll-arrow-vlength", &scroll_arrow_height,
                        NULL);
 
   attributes.x = border_width + widget->style->xthickness + horizontal_padding;
@@ -2042,14 +2192,10 @@ gtk_menu_realize (GtkWidget *widget)
   attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
   attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
 
-  if (menu->upper_arrow_visible)
-    {
-      attributes.y += scroll_arrow_height;
-      attributes.height -= scroll_arrow_height;
-    }
-
-  if (menu->lower_arrow_visible)
-    attributes.height -= scroll_arrow_height;
+  get_arrows_border (menu, &arrow_border);
+  attributes.y += arrow_border.top;
+  attributes.height -= arrow_border.top;
+  attributes.height -= arrow_border.bottom;
 
   menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
   gdk_window_set_user_data (menu->view_window, menu);
@@ -2143,11 +2289,7 @@ menu_grab_transfer_window_destroy (GtkMenu *menu)
 static void
 gtk_menu_unrealize (GtkWidget *widget)
 {
-  GtkMenu *menu;
-
-  g_return_if_fail (GTK_IS_MENU (widget));
-
-  menu = GTK_MENU (widget);
+  GtkMenu *menu = GTK_MENU (widget);
 
   menu_grab_transfer_window_destroy (menu);
 
@@ -2270,7 +2412,6 @@ gtk_menu_size_allocate (GtkWidget     *widget,
   gint width, height;
   guint vertical_padding;
   guint horizontal_padding;
-  gint scroll_arrow_height;
   
   g_return_if_fail (GTK_IS_MENU (widget));
   g_return_if_fail (allocation != NULL);
@@ -2285,7 +2426,6 @@ gtk_menu_size_allocate (GtkWidget     *widget,
   gtk_widget_style_get (GTK_WIDGET (menu),
                        "vertical-padding", &vertical_padding,
                         "horizontal-padding", &horizontal_padding,
-                        "scroll-arrow-vlength", &scroll_arrow_height,
                        NULL);
 
   x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness + horizontal_padding;
@@ -2300,14 +2440,15 @@ gtk_menu_size_allocate (GtkWidget     *widget,
   if (menu_shell->active)
     gtk_menu_scroll_to (menu, menu->scroll_offset);
 
-  if (menu->upper_arrow_visible && !menu->tearoff_active)
+  if (!menu->tearoff_active)
     {
-      y += scroll_arrow_height;
-      height -= scroll_arrow_height;
-    }
+      GtkBorder arrow_border;
 
-  if (menu->lower_arrow_visible && !menu->tearoff_active)
-    height -= scroll_arrow_height;
+      get_arrows_border (menu, &arrow_border);
+      y += arrow_border.top;
+      height -= arrow_border.top;
+      height -= arrow_border.bottom;
+    }
 
   if (GTK_WIDGET_REALIZED (widget))
     {
@@ -2421,43 +2562,68 @@ gtk_menu_size_allocate (GtkWidget     *widget,
     }
 }
 
+static void
+get_arrows_visible_area (GtkMenu *menu,
+                         GdkRectangle *border,
+                         GdkRectangle *upper,
+                         GdkRectangle *lower,
+                         gint *arrow_space)
+{
+  GtkWidget *widget = GTK_WIDGET (menu);
+  guint vertical_padding;
+  guint horizontal_padding;
+  gint scroll_arrow_height;
+  
+  gtk_widget_style_get (widget,
+                        "vertical-padding", &vertical_padding,
+                        "horizontal-padding", &horizontal_padding,
+                        "scroll-arrow-vlength", &scroll_arrow_height,
+                        NULL);
+
+  border->x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness + horizontal_padding;
+  border->y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
+  gdk_drawable_get_size (widget->window, &border->width, &border->height);
+
+  upper->x = border->x;
+  upper->y = border->y;
+  upper->width = border->width - 2 * border->x;
+  upper->height = scroll_arrow_height;
+
+  lower->x = border->x;
+  lower->y = border->height - border->y - scroll_arrow_height;
+  lower->width = border->width - 2 * border->x;
+  lower->height = scroll_arrow_height;
+
+  *arrow_space = scroll_arrow_height - 2 * widget->style->ythickness;
+}
+
 static void
 gtk_menu_paint (GtkWidget      *widget,
                GdkEventExpose *event)
 {
   GtkMenu *menu;
   GtkMenuPrivate *priv;
-  gint width, height;
-  gint border_x, border_y;
-  guint vertical_padding;
-  guint horizontal_padding;
-  gint scroll_arrow_height;
+  GdkRectangle border;
+  GdkRectangle upper;
+  GdkRectangle lower;
+  gint arrow_space;
   
   g_return_if_fail (GTK_IS_MENU (widget));
 
   menu = GTK_MENU (widget);
   priv = gtk_menu_get_private (menu);
 
-  gtk_widget_style_get (GTK_WIDGET (menu),
-                       "vertical-padding", &vertical_padding,
-                        "horizontal-padding", &horizontal_padding,
-                        "scroll-arrow-vlength", &scroll_arrow_height,
-                        NULL);
-
-  border_x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness + horizontal_padding;
-  border_y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
-  gdk_drawable_get_size (widget->window, &width, &height);
+  get_arrows_visible_area (menu, &border, &upper, &lower, &arrow_space);
 
   if (event->window == widget->window)
     {
-      gint arrow_space = scroll_arrow_height - 2 * widget->style->ythickness;
       gint arrow_size = 0.7 * arrow_space;
 
       gtk_paint_box (widget->style,
                     widget->window,
                     GTK_STATE_NORMAL,
                     GTK_SHADOW_OUT,
-                    NULL, widget, "menu",
+                    &event->area, widget, "menu",
                     0, 0, -1, -1);
 
       if (menu->upper_arrow_visible && !menu->tearoff_active)
@@ -2465,22 +2631,22 @@ gtk_menu_paint (GtkWidget      *widget,
          gtk_paint_box (widget->style,
                         widget->window,
                         priv->upper_arrow_state,
-                        GTK_SHADOW_OUT,
-                        NULL, widget, "menu",
-                        border_x,
-                        border_y,
-                        width - 2 * border_x,
-                        scroll_arrow_height);
+                         GTK_SHADOW_OUT,
+                        &event->area, widget, "menu_scroll_arrow_up",
+                         upper.x,
+                         upper.y,
+                         upper.width,
+                         upper.height);
 
          gtk_paint_arrow (widget->style,
                           widget->window,
                           priv->upper_arrow_state,
                           GTK_SHADOW_OUT,
-                          NULL, widget, "menu_scroll_arrow_up",
+                          &event->area, widget, "menu_scroll_arrow_up",
                           GTK_ARROW_UP,
                           TRUE,
-                          (width - arrow_size ) / 2,
-                          border_y + widget->style->ythickness + (arrow_space - arrow_size)/2,
+                           upper.x + (upper.width - arrow_size) / 2,
+                           upper.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
                           arrow_size, arrow_size);
        }
 
@@ -2489,35 +2655,44 @@ gtk_menu_paint (GtkWidget      *widget,
          gtk_paint_box (widget->style,
                         widget->window,
                         priv->lower_arrow_state,
-                        GTK_SHADOW_OUT,
-                        NULL, widget, "menu",
-                        border_x,
-                        height - border_y - scroll_arrow_height,
-                        width - 2*border_x,
-                        scroll_arrow_height);
+                         GTK_SHADOW_OUT,
+                        &event->area, widget, "menu_scroll_arrow_down",
+                         lower.x,
+                         lower.y,
+                         lower.width,
+                         lower.height);
 
          gtk_paint_arrow (widget->style,
                           widget->window,
                           priv->lower_arrow_state,
                           GTK_SHADOW_OUT,
-                          NULL, widget, "menu_scroll_arrow_down",
+                          &event->area, widget, "menu_scroll_arrow_down",
                           GTK_ARROW_DOWN,
                           TRUE,
-                          (width - arrow_size) / 2,
-                          height - border_y - scroll_arrow_height +
-                             widget->style->ythickness + (arrow_space - arrow_size)/2,
+                           lower.x + (lower.width - arrow_size) / 2,
+                          lower.y + widget->style->ythickness + (arrow_space - arrow_size) / 2,
                           arrow_size, arrow_size);
        }
     }
   else if (event->window == menu->bin_window)
     {
+      gint y = -border.y + menu->scroll_offset;
+
+      if (!menu->tearoff_active)
+        {
+          GtkBorder arrow_border;
+
+          get_arrows_border (menu, &arrow_border);
+          y -= arrow_border.top;
+        }
+
       gtk_paint_box (widget->style,
                     menu->bin_window,
                     GTK_STATE_NORMAL,
                     GTK_SHADOW_OUT,
-                    NULL, widget, "menu",
-                    - border_x, menu->scroll_offset - border_y, 
-                    width, height);
+                    &event->area, widget, "menu",
+                    - border.x, y,
+                    border.width, border.height);
     }
 }
 
@@ -2549,30 +2724,52 @@ gtk_menu_show (GtkWidget *widget)
 }
 
 static gboolean
-gtk_menu_button_scroll (GtkWidget      *widget,
+gtk_menu_button_scroll (GtkMenu        *menu,
                         GdkEventButton *event)
 {
-  if (GTK_IS_MENU (widget))
+  if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
     {
-      GtkMenu *menu = GTK_MENU (widget);
+      gboolean touchscreen_mode;
 
-      if (menu->upper_arrow_prelight || menu->lower_arrow_prelight)
-        {
-          GtkSettings *settings = gtk_widget_get_settings (widget);
-          gboolean     touchscreen_mode;
+      g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
+                    "gtk-touchscreen-mode", &touchscreen_mode,
+                    NULL);
 
-          g_object_get (G_OBJECT (settings),
-                        "gtk-touchscreen-mode", &touchscreen_mode,
-                        NULL);
+      if (touchscreen_mode)
+        gtk_menu_handle_scrolling (menu,
+                                   event->x_root, event->y_root,
+                                   event->type == GDK_BUTTON_PRESS,
+                                   FALSE);
 
-          if (touchscreen_mode)
-            gtk_menu_handle_scrolling (menu,
-                                       event->x_root, event->y_root,
-                                       event->type == GDK_BUTTON_PRESS,
-                                       FALSE);
+      return TRUE;
+    }
 
-          return TRUE;
-        }
+  return FALSE;
+}
+
+static gboolean
+pointer_in_menu_window (GtkWidget *widget,
+                        gdouble    x_root,
+                        gdouble    y_root)
+{
+  GtkMenu *menu = GTK_MENU (widget);
+
+  if (GTK_WIDGET_MAPPED (menu->toplevel))
+    {
+      GtkMenuShell *menu_shell;
+      gint          window_x, window_y;
+
+      gdk_window_get_position (menu->toplevel->window, &window_x, &window_y);
+
+      if (x_root >= window_x && x_root < window_x + widget->allocation.width &&
+          y_root >= window_y && y_root < window_y + widget->allocation.height)
+        return TRUE;
+
+      menu_shell = GTK_MENU_SHELL (widget);
+
+      if (GTK_IS_MENU (menu_shell->parent_menu_shell))
+        return pointer_in_menu_window (menu_shell->parent_menu_shell,
+                                       x_root, y_root);
     }
 
   return FALSE;
@@ -2585,9 +2782,20 @@ gtk_menu_button_press (GtkWidget      *widget,
   if (event->type != GDK_BUTTON_PRESS)
     return FALSE;
 
-  /* Don't pop down the menu for presses over scroll arrows
+  /* Don't pass down to menu shell for presses over scroll arrows
    */
-  if (gtk_menu_button_scroll (widget, event))
+  if (gtk_menu_button_scroll (GTK_MENU (widget), event))
+    return TRUE;
+
+  /*  Don't pass down to menu shell if a non-menuitem part of the menu
+   *  was clicked. The check for the event_widget being a GtkMenuShell
+   *  works because we have the pointer grabbed on menu_shell->window
+   *  with owner_events=TRUE, so all events that are either outside
+   *  the menu or on its border are delivered relative to
+   *  menu_shell->window.
+   */
+  if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
+      pointer_in_menu_window (widget, event->x_root, event->y_root))
     return TRUE;
 
   return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_press_event (widget, event);
@@ -2597,25 +2805,39 @@ static gboolean
 gtk_menu_button_release (GtkWidget      *widget,
                         GdkEventButton *event)
 {
-  if (GTK_IS_MENU (widget))
-    {
-      GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
+  GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (widget));
 
-      if (priv->ignore_button_release)
-        {
-          priv->ignore_button_release = FALSE;
-          return FALSE;
-        }
+  if (priv->ignore_button_release)
+    {
+      priv->ignore_button_release = FALSE;
+      return FALSE;
     }
 
   if (event->type != GDK_BUTTON_RELEASE)
     return FALSE;
 
-  /* Don't pop down the menu for releases over scroll arrows
+  /* Don't pass down to menu shell for releases over scroll arrows
    */
-  if (gtk_menu_button_scroll (widget, event))
+  if (gtk_menu_button_scroll (GTK_MENU (widget), event))
     return TRUE;
 
+  /*  Don't pass down to menu shell if a non-menuitem part of the menu
+   *  was clicked (see comment in button_press()).
+   */
+  if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
+      pointer_in_menu_window (widget, event->x_root, event->y_root))
+    {
+      /*  Ugly: make sure menu_shell->button gets reset to 0 when we
+       *  bail out early here so it is in a consistent state for the
+       *  next button_press/button_release in GtkMenuShell.
+       *  See bug #449371.
+       */
+      if (GTK_MENU_SHELL (widget)->active)
+        GTK_MENU_SHELL (widget)->button = 0;
+
+      return TRUE;
+    }
+
   return GTK_WIDGET_CLASS (gtk_menu_parent_class)->button_release_event (widget, event);
 }
 
@@ -2688,11 +2910,10 @@ gtk_menu_key_press (GtkWidget   *widget,
                "gtk-can-change-accels", &can_change_accels,
                 NULL);
 
-  if (accel)
+  if (accel && *accel)
     {
       guint keyval = 0;
       GdkModifierType mods = 0;
-      gboolean handled = FALSE;
       
       gtk_accelerator_parse (accel, &keyval, &mods);
 
@@ -2703,15 +2924,15 @@ gtk_menu_key_press (GtkWidget   *widget,
        * thing, to properly consider i18n etc., but that probably requires
        * AccelGroup changes etc.
        */
-      if (event->keyval == keyval &&
-          (mods & event->state) == mods)
-       gtk_menu_shell_cancel (menu_shell);
-
-      g_free (accel);
-
-      if (handled)
-        return TRUE;
+      if (event->keyval == keyval && (mods & event->state) == mods)
+        {
+         gtk_menu_shell_cancel (menu_shell);
+          g_free (accel);
+          return TRUE;
+        }
     }
+
+  g_free (accel);
   
   switch (event->keyval)
     {
@@ -2756,7 +2977,7 @@ gtk_menu_key_press (GtkWidget     *widget,
           * (basically, those items are accelerator-locked).
           */
          /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
-         gdk_display_beep (display);
+         gtk_widget_error_bell (widget);
        }
       else
        {
@@ -2785,7 +3006,7 @@ gtk_menu_key_press (GtkWidget     *widget,
               * locked already
               */
              /* g_print("failed to change\n"); */
-             gdk_display_beep (display);
+             gtk_widget_error_bell (widget);
            }
        }
     }
@@ -2862,7 +3083,7 @@ gtk_menu_motion_notify  (GtkWidget           *widget,
 
   if (definitely_within_item (menu_item, event->x, event->y))
     menu_shell->activate_time = 0;
-  
+
   need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
 
   /* Check to see if we are within an active submenu's navigation region
@@ -2902,6 +3123,7 @@ gtk_menu_motion_notify  (GtkWidget           *widget,
          send_event->crossing.y_root = event->y_root;
          send_event->crossing.x = event->x;
          send_event->crossing.y = event->y;
+          send_event->crossing.state = event->state;
 
          /* We send the event to 'widget', the currently active menu,
           * instead of 'menu', the menu that the pointer is in. This
@@ -2941,14 +3163,12 @@ gtk_menu_scroll_by (GtkMenu *menu,
   gint offset;
   gint view_width, view_height;
   gboolean double_arrows;
-  gint scroll_arrow_height;
+  GtkBorder arrow_border;
   
   widget = GTK_WIDGET (menu);
   offset = menu->scroll_offset + step;
 
-  gtk_widget_style_get (GTK_WIDGET (menu),
-                        "scroll-arrow-vlength", &scroll_arrow_height,
-                        NULL);
+  get_arrows_border (menu, &arrow_border);
 
   double_arrows = get_double_arrows (menu);
 
@@ -2958,7 +3178,7 @@ gtk_menu_scroll_by (GtkMenu *menu,
    * screen space than just scrolling to the top.
    */
   if (!double_arrows)
-    if ((step < 0) && (offset < scroll_arrow_height))
+    if ((step < 0) && (offset < arrow_border.top))
       offset = 0;
 
   /* Don't scroll over the top if we weren't before: */
@@ -2973,13 +3193,13 @@ gtk_menu_scroll_by (GtkMenu *menu,
 
   /* Don't scroll past the bottom if we weren't before: */
   if (menu->scroll_offset > 0)
-    view_height -= scroll_arrow_height;
+    view_height -= arrow_border.top;
 
   /* When both arrows are always shown, reduce
    * view height even more.
    */
   if (double_arrows)
-    view_height -= scroll_arrow_height;
+    view_height -= arrow_border.bottom;
 
   if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
       (offset + view_height > widget->requisition.height))
@@ -3015,42 +3235,32 @@ gtk_menu_do_timeout_scroll (GtkMenu  *menu,
 }
 
 static gboolean
-gtk_menu_scroll_timeout (gpointer  data)
+gtk_menu_scroll_timeout (gpointer data)
 {
-  GtkMenu     *menu;
-  GtkSettings *settings;
-  gboolean     touchscreen_mode;
-
-  GDK_THREADS_ENTER ();
+  GtkMenu  *menu;
+  gboolean  touchscreen_mode;
 
   menu = GTK_MENU (data);
 
-  settings = gtk_widget_get_settings (GTK_WIDGET (menu));
-  g_object_get (settings,
+  g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
                 "gtk-touchscreen-mode", &touchscreen_mode,
                 NULL);
 
   gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
 
-  GDK_THREADS_LEAVE ();
-
   return TRUE;
 }
 
 static gboolean
 gtk_menu_scroll_timeout_initial (gpointer data)
 {
-  GtkMenu     *menu;
-  GtkSettings *settings;
-  guint        timeout;
-  gboolean     touchscreen_mode;
-
-  GDK_THREADS_ENTER ();
+  GtkMenu  *menu;
+  guint     timeout;
+  gboolean  touchscreen_mode;
 
   menu = GTK_MENU (data);
 
-  settings = gtk_widget_get_settings (GTK_WIDGET (menu));
-  g_object_get (settings,
+  g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
                 "gtk-timeout-repeat", &timeout,
                 "gtk-touchscreen-mode", &touchscreen_mode,
                 NULL);
@@ -3059,9 +3269,9 @@ gtk_menu_scroll_timeout_initial (gpointer data)
 
   gtk_menu_remove_scroll_timeout (menu);
 
-  menu->timeout_id = g_timeout_add (timeout, gtk_menu_scroll_timeout, menu);
-
-  GDK_THREADS_LEAVE ();
+  menu->timeout_id = gdk_threads_add_timeout (timeout,
+                                              gtk_menu_scroll_timeout,
+                                              menu);
 
   return FALSE;
 }
@@ -3069,20 +3279,19 @@ gtk_menu_scroll_timeout_initial (gpointer data)
 static void
 gtk_menu_start_scrolling (GtkMenu *menu)
 {
-  GtkSettings *settings;
-  guint        timeout;
-  gboolean     touchscreen_mode;
+  guint    timeout;
+  gboolean touchscreen_mode;
 
-  settings = gtk_widget_get_settings (GTK_WIDGET (menu));
-  g_object_get (settings,
+  g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
                 "gtk-timeout-repeat", &timeout,
                 "gtk-touchscreen-mode", &touchscreen_mode,
                 NULL);
 
   gtk_menu_do_timeout_scroll (menu, touchscreen_mode);
 
-  menu->timeout_id = g_timeout_add (timeout, gtk_menu_scroll_timeout_initial,
-                                    menu);
+  menu->timeout_id = gdk_threads_add_timeout (timeout,
+                                              gtk_menu_scroll_timeout_initial,
+                                              menu);
 }
 
 static gboolean
@@ -3106,6 +3315,47 @@ gtk_menu_scroll (GtkWidget       *widget,
   return TRUE;
 }
 
+static void
+get_arrows_sensitive_area (GtkMenu *menu,
+                           GdkRectangle *upper,
+                           GdkRectangle *lower)
+{
+  gint width, height;
+  gint border;
+  guint vertical_padding;
+  gint win_x, win_y;
+  gint scroll_arrow_height;
+
+  gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
+
+  gtk_widget_style_get (GTK_WIDGET (menu),
+                        "vertical-padding", &vertical_padding,
+                        "scroll-arrow-vlength", &scroll_arrow_height,
+                        NULL);
+
+  border = GTK_CONTAINER (menu)->border_width +
+    GTK_WIDGET (menu)->style->ythickness + vertical_padding;
+
+  gdk_window_get_position (GTK_WIDGET (menu)->window, &win_x, &win_y);
+
+  if (upper)
+    {
+      upper->x = win_x;
+      upper->y = win_y;
+      upper->width = width;
+      upper->height = scroll_arrow_height + border;
+    }
+
+  if (lower)
+    {
+      lower->x = win_x;
+      lower->y = win_y + height - border - scroll_arrow_height;
+      lower->width = width;
+      lower->height = scroll_arrow_height + border;
+    }
+}
+
+
 static void
 gtk_menu_handle_scrolling (GtkMenu *menu,
                           gint     x,
@@ -3115,48 +3365,27 @@ gtk_menu_handle_scrolling (GtkMenu *menu,
 {
   GtkMenuShell *menu_shell;
   GtkMenuPrivate *priv;
-  gint width, height;
-  gint border;
   GdkRectangle rect;
   gboolean in_arrow;
   gboolean scroll_fast = FALSE;
-  guint vertical_padding;
   gint top_x, top_y;
-  gint win_x, win_y;
-  GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (menu));
   gboolean touchscreen_mode;
-  gint scroll_arrow_height;
-  
+
   priv = gtk_menu_get_private (menu);
 
   menu_shell = GTK_MENU_SHELL (menu);
 
-  gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
-
-  g_object_get (G_OBJECT (settings),
+  g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
                 "gtk-touchscreen-mode", &touchscreen_mode,
                 NULL);
 
-  gtk_widget_style_get (GTK_WIDGET (menu),
-                       "vertical-padding", &vertical_padding,
-                        "scroll-arrow-vlength", &scroll_arrow_height,
-                       NULL);
-
-  border = GTK_CONTAINER (menu)->border_width +
-    GTK_WIDGET (menu)->style->ythickness + vertical_padding;
-
   gdk_window_get_position (menu->toplevel->window, &top_x, &top_y);
   x -= top_x;
   y -= top_y;
 
-  gdk_window_get_position (GTK_WIDGET (menu)->window, &win_x, &win_y);
-
   /*  upper arrow handling  */
 
-  rect.x = win_x;
-  rect.y = win_y;
-  rect.width = width;
-  rect.height = scroll_arrow_height + border;
+  get_arrows_sensitive_area (menu, &rect, NULL);
 
   in_arrow = FALSE;
   if (menu->upper_arrow_visible && !menu->tearoff_active &&
@@ -3171,36 +3400,38 @@ gtk_menu_handle_scrolling (GtkMenu *menu,
 
   if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
     {
+      gboolean arrow_pressed = FALSE;
+
       if (menu->upper_arrow_visible && !menu->tearoff_active)
         {
           if (touchscreen_mode)
             {
-              if (enter && menu->upper_arrow_prelight &&
-                  menu->timeout_id == 0)
+              if (enter && menu->upper_arrow_prelight)
                 {
-                  /* Deselect the active item so that
-                   * any submenus are popped down
-                   */
-                  gtk_menu_shell_deselect (menu_shell);
-
-                  gtk_menu_remove_scroll_timeout (menu);
-                  menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
-
-                  if (!motion)
+                  if (menu->timeout_id == 0)
                     {
-                      /* Only do stuff on click. */
-                      gtk_menu_start_scrolling (menu);
-                      priv->upper_arrow_state = GTK_STATE_ACTIVE;
+                      /* Deselect the active item so that
+                       * any submenus are popped down
+                       */
+                      gtk_menu_shell_deselect (menu_shell);
+
+                      gtk_menu_remove_scroll_timeout (menu);
+                      menu->scroll_step = -MENU_SCROLL_STEP2; /* always fast */
+
+                      if (!motion)
+                        {
+                          /* Only do stuff on click. */
+                          gtk_menu_start_scrolling (menu);
+                          arrow_pressed = TRUE;
+                        }
+                    }
+                  else
+                    {
+                      arrow_pressed = TRUE;
                     }
-
-                  gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
-                                              &rect, FALSE);
                 }
               else if (!enter)
                 {
-                  gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
-                                              &rect, FALSE);
-
                   gtk_menu_stop_scrolling (menu);
                 }
             }
@@ -3214,8 +3445,6 @@ gtk_menu_handle_scrolling (GtkMenu *menu,
                 {
                   menu->upper_arrow_prelight = TRUE;
                   menu->scroll_fast = scroll_fast;
-                  gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
-                                              &rect, FALSE);
 
                   /* Deselect the active item so that
                    * any submenus are popped down
@@ -3227,30 +3456,44 @@ gtk_menu_handle_scrolling (GtkMenu *menu,
                     -MENU_SCROLL_STEP2 : -MENU_SCROLL_STEP1;
 
                   menu->timeout_id =
-                    g_timeout_add (scroll_fast ?
-                                   MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
-                                   gtk_menu_scroll_timeout, menu);
+                    gdk_threads_add_timeout (scroll_fast ?
+                                             MENU_SCROLL_TIMEOUT2 :
+                                             MENU_SCROLL_TIMEOUT1,
+                                             gtk_menu_scroll_timeout, menu);
                 }
               else if (!enter && !in_arrow && menu->upper_arrow_prelight)
                 {
-                  gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
-                                              &rect, FALSE);
-
                   gtk_menu_stop_scrolling (menu);
                 }
             }
         }
 
-      priv->upper_arrow_state = menu->upper_arrow_prelight ?
-        GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
+      /*  gtk_menu_start_scrolling() might have hit the top of the
+       *  menu, so check if the button isn't insensitive before
+       *  changing it to something else.
+       */
+      if (priv->upper_arrow_state != GTK_STATE_INSENSITIVE)
+        {
+          GtkStateType arrow_state = GTK_STATE_NORMAL;
+
+          if (arrow_pressed)
+            arrow_state = GTK_STATE_ACTIVE;
+          else if (menu->upper_arrow_prelight)
+            arrow_state = GTK_STATE_PRELIGHT;
+
+          if (arrow_state != priv->upper_arrow_state)
+            {
+              priv->upper_arrow_state = arrow_state;
+
+              gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
+                                          &rect, FALSE);
+            }
+        }
     }
 
   /*  lower arrow handling  */
 
-  rect.x = win_x;
-  rect.y = win_y + height - border - scroll_arrow_height;
-  rect.width = width;
-  rect.height = scroll_arrow_height + border;
+  get_arrows_sensitive_area (menu, NULL, &rect);
 
   in_arrow = FALSE;
   if (menu->lower_arrow_visible && !menu->tearoff_active &&
@@ -3265,36 +3508,38 @@ gtk_menu_handle_scrolling (GtkMenu *menu,
 
   if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
     {
+      gboolean arrow_pressed = FALSE;
+
       if (menu->lower_arrow_visible && !menu->tearoff_active)
         {
           if (touchscreen_mode)
             {
-              if (enter && menu->lower_arrow_prelight &&
-                  menu->timeout_id == 0)
+              if (enter && menu->lower_arrow_prelight)
                 {
-                  /* Deselect the active item so that
-                   * any submenus are popped down
-                   */
-                  gtk_menu_shell_deselect (menu_shell);
-
-                  gtk_menu_remove_scroll_timeout (menu);
-                  menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
-
-                  if (!motion)
+                  if (menu->timeout_id == 0)
                     {
-                      /* Only do stuff on click. */
-                      gtk_menu_start_scrolling (menu);
-                      priv->lower_arrow_state = GTK_STATE_ACTIVE;
+                      /* Deselect the active item so that
+                       * any submenus are popped down
+                       */
+                      gtk_menu_shell_deselect (menu_shell);
+
+                      gtk_menu_remove_scroll_timeout (menu);
+                      menu->scroll_step = MENU_SCROLL_STEP2; /* always fast */
+
+                      if (!motion)
+                        {
+                          /* Only do stuff on click. */
+                          gtk_menu_start_scrolling (menu);
+                          arrow_pressed = TRUE;
+                        }
+                    }
+                  else
+                    {
+                      arrow_pressed = TRUE;
                     }
-
-                  gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
-                                              &rect, FALSE);
                 }
               else if (!enter)
                 {
-                  gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
-                                              &rect, FALSE);
-
                   gtk_menu_stop_scrolling (menu);
                 }
             }
@@ -3308,8 +3553,6 @@ gtk_menu_handle_scrolling (GtkMenu *menu,
                 {
                   menu->lower_arrow_prelight = TRUE;
                   menu->scroll_fast = scroll_fast;
-                  gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
-                                              &rect, FALSE);
 
                   /* Deselect the active item so that
                    * any submenus are popped down
@@ -3321,22 +3564,39 @@ gtk_menu_handle_scrolling (GtkMenu *menu,
                     MENU_SCROLL_STEP2 : MENU_SCROLL_STEP1;
 
                   menu->timeout_id =
-                    g_timeout_add (scroll_fast ?
-                                   MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
-                                   gtk_menu_scroll_timeout, menu);
+                    gdk_threads_add_timeout (scroll_fast ?
+                                             MENU_SCROLL_TIMEOUT2 :
+                                             MENU_SCROLL_TIMEOUT1,
+                                             gtk_menu_scroll_timeout, menu);
                 }
               else if (!enter && !in_arrow && menu->lower_arrow_prelight)
                 {
-                  gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
-                                              &rect, FALSE);
-
                   gtk_menu_stop_scrolling (menu);
                 }
             }
         }
 
-      priv->lower_arrow_state = menu->lower_arrow_prelight ?
-        GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
+      /*  gtk_menu_start_scrolling() might have hit the bottom of the
+       *  menu, so check if the button isn't insensitive before
+       *  changing it to something else.
+       */
+      if (priv->lower_arrow_state != GTK_STATE_INSENSITIVE)
+        {
+          GtkStateType arrow_state = GTK_STATE_NORMAL;
+
+          if (arrow_pressed)
+            arrow_state = GTK_STATE_ACTIVE;
+          else if (menu->lower_arrow_prelight)
+            arrow_state = GTK_STATE_PRELIGHT;
+
+          if (arrow_state != priv->lower_arrow_state)
+            {
+              priv->lower_arrow_state = arrow_state;
+
+              gdk_window_invalidate_rect (GTK_WIDGET (menu)->window,
+                                          &rect, FALSE);
+            }
+        }
     }
 }
 
@@ -3344,11 +3604,10 @@ static gboolean
 gtk_menu_enter_notify (GtkWidget        *widget,
                       GdkEventCrossing *event)
 {
-  GtkSettings *settings = gtk_widget_get_settings (widget);
   GtkWidget *menu_item;
-  gboolean touchscreen_mode;
+  gboolean   touchscreen_mode;
 
-  g_object_get (G_OBJECT (settings),
+  g_object_get (gtk_widget_get_settings (widget),
                 "gtk-touchscreen-mode", &touchscreen_mode,
                 NULL);
 
@@ -3484,8 +3743,6 @@ gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
   GtkMenu *menu = user_data;
   GdkWindow *child_window;
 
-  GDK_THREADS_ENTER ();
-
   gtk_menu_stop_navigating_submenu (menu);
   
   if (GTK_WIDGET_REALIZED (menu))
@@ -3506,8 +3763,6 @@ gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
        }
     }
 
-  GDK_THREADS_LEAVE ();
-
   return FALSE; 
 }
 
@@ -3681,8 +3936,9 @@ gtk_menu_set_submenu_navigation_region (GtkMenu          *menu,
                    "gtk-menu-popdown-delay", &popdown_delay,
                    NULL);
 
-      menu->navigation_timeout = g_timeout_add (popdown_delay,
-                                               gtk_menu_stop_navigating_submenu_cb, menu);
+      menu->navigation_timeout = gdk_threads_add_timeout (popdown_delay,
+                                                          gtk_menu_stop_navigating_submenu_cb,
+                                                          menu);
 
 #ifdef DRAW_STAY_UP_TRIANGLE
       draw_stay_up_triangle (gdk_get_default_root_window(),
@@ -3719,7 +3975,6 @@ gtk_menu_position (GtkMenu *menu)
   GdkScreen *screen;
   GdkScreen *pointer_screen;
   GdkRectangle monitor;
-  gint scroll_arrow_height;
   
   g_return_if_fail (GTK_IS_MENU (menu));
 
@@ -3728,10 +3983,6 @@ gtk_menu_position (GtkMenu *menu)
   screen = gtk_widget_get_screen (widget);
   gdk_display_get_pointer (gdk_screen_get_display (screen),
                           &pointer_screen, &x, &y, NULL);
-
-  gtk_widget_style_get (GTK_WIDGET (menu),
-                        "scroll-arrow-vlength", &scroll_arrow_height,
-                        NULL);
   
   /* We need the requisition to figure out the right place to
    * popup the menu. In fact, we always need to ask here, since
@@ -3927,7 +4178,12 @@ gtk_menu_position (GtkMenu *menu)
     }
 
   if (scroll_offset > 0)
-    scroll_offset += scroll_arrow_height;
+    {
+      GtkBorder arrow_border;
+
+      get_arrows_border (menu, &arrow_border);
+      scroll_offset += arrow_border.top;
+    }
   
   gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window), 
                   x, y);
@@ -3954,14 +4210,13 @@ gtk_menu_remove_scroll_timeout (GtkMenu *menu)
 static void
 gtk_menu_stop_scrolling (GtkMenu *menu)
 {
-  GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (menu));
   gboolean touchscreen_mode;
 
   gtk_menu_remove_scroll_timeout (menu);
 
-  g_object_get (G_OBJECT (settings),
-                "gtk-touchscreen-mode", &touchscreen_mode,
-                NULL);
+  g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
+               "gtk-touchscreen-mode", &touchscreen_mode,
+               NULL);
 
   if (!touchscreen_mode)
     {
@@ -3982,7 +4237,7 @@ gtk_menu_scroll_to (GtkMenu *menu,
   guint vertical_padding;
   guint horizontal_padding;
   gboolean double_arrows;
-  gint scroll_arrow_height;
+  GtkBorder arrow_border;
   
   widget = GTK_WIDGET (menu);
 
@@ -4003,7 +4258,6 @@ gtk_menu_scroll_to (GtkMenu *menu,
   gtk_widget_style_get (GTK_WIDGET (menu),
                         "vertical-padding", &vertical_padding,
                         "horizontal-padding", &horizontal_padding,
-                        "scroll-arrow-vlength", &scroll_arrow_height,
                         NULL);
 
   double_arrows = get_double_arrows (menu);
@@ -4030,20 +4284,22 @@ gtk_menu_scroll_to (GtkMenu *menu,
           if (!menu->upper_arrow_visible || !menu->lower_arrow_visible)
             gtk_widget_queue_draw (GTK_WIDGET (menu));
 
-          view_height -= 2 * scroll_arrow_height;
-          y += scroll_arrow_height;
-
           menu->upper_arrow_visible = menu->lower_arrow_visible = TRUE;
 
+         get_arrows_border (menu, &arrow_border);
+         y += arrow_border.top;
+         view_height -= arrow_border.top;
+         view_height -= arrow_border.bottom;
+
           if (offset <= 0)
             priv->upper_arrow_state = GTK_STATE_INSENSITIVE;
-          else
+          else if (priv->upper_arrow_state == GTK_STATE_INSENSITIVE)
             priv->upper_arrow_state = menu->upper_arrow_prelight ?
               GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
 
           if (offset >= menu_height - view_height)
             priv->lower_arrow_state = GTK_STATE_INSENSITIVE;
-          else
+          else if (priv->lower_arrow_state == GTK_STATE_INSENSITIVE)
             priv->lower_arrow_state = menu->lower_arrow_prelight ?
               GTK_STATE_PRELIGHT : GTK_STATE_NORMAL;
 
@@ -4091,8 +4347,9 @@ gtk_menu_scroll_to (GtkMenu *menu,
       last_visible = menu->upper_arrow_visible;
       menu->upper_arrow_visible = offset > 0;
       
-      if (menu->upper_arrow_visible)
-       view_height -= scroll_arrow_height;
+      /* upper_arrow_visible may have changed, so requery the border */
+      get_arrows_border (menu, &arrow_border);
+      view_height -= arrow_border.top;
       
       if ((last_visible != menu->upper_arrow_visible) &&
           !menu->upper_arrow_visible)
@@ -4110,8 +4367,9 @@ gtk_menu_scroll_to (GtkMenu *menu,
       last_visible = menu->lower_arrow_visible;
       menu->lower_arrow_visible = offset < menu_height - view_height;
       
-      if (menu->lower_arrow_visible)
-       view_height -= scroll_arrow_height;
+      /* lower_arrow_visible may have changed, so requery the border */
+      get_arrows_border (menu, &arrow_border);
+      view_height -= arrow_border.bottom;
       
       if ((last_visible != menu->lower_arrow_visible) &&
           !menu->lower_arrow_visible)
@@ -4126,8 +4384,7 @@ gtk_menu_scroll_to (GtkMenu *menu,
            }
        }
       
-      if (menu->upper_arrow_visible)
-       y += scroll_arrow_height;
+      y += arrow_border.top;
     }
 
   /* Scroll the menu: */
@@ -4205,14 +4462,12 @@ gtk_menu_scroll_item_visible (GtkMenuShell    *menu_shell,
     {
       guint vertical_padding;
       gboolean double_arrows;
-      gint scroll_arrow_height;
       
       y = menu->scroll_offset;
       gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
 
       gtk_widget_style_get (GTK_WIDGET (menu),
                            "vertical-padding", &vertical_padding,
-                            "scroll-arrow-vlength", &scroll_arrow_height,
                             NULL);
 
       double_arrows = get_double_arrows (menu);
@@ -4228,23 +4483,25 @@ gtk_menu_scroll_item_visible (GtkMenuShell    *menu_shell,
        }
       else
        {
-         arrow_height = 0;
-         if (menu->upper_arrow_visible && !menu->tearoff_active)
-           arrow_height += scroll_arrow_height;
-         if (menu->lower_arrow_visible && !menu->tearoff_active)
-           arrow_height += scroll_arrow_height;
+          GtkBorder arrow_border;
+
+          arrow_height = 0;
+
+          get_arrows_border (menu, &arrow_border);
+          if (!menu->tearoff_active)
+            arrow_height = arrow_border.top + arrow_border.bottom;
          
          if (child_offset + child_height > y + height - arrow_height)
            {
              arrow_height = 0;
              if ((!last_child && !menu->tearoff_active) || double_arrows)
-               arrow_height += scroll_arrow_height;
+               arrow_height += arrow_border.bottom;
 
              y = child_offset + child_height - height + arrow_height;
              if (((y > 0) && !menu->tearoff_active) || double_arrows)
                {
                  /* Need upper arrow */
-                 arrow_height += scroll_arrow_height;
+                 arrow_height += arrow_border.top;
                  y = child_offset + child_height - height + arrow_height;
                }
              /* Ignore the enter event we might get if the pointer is on the menu
@@ -4596,21 +4853,20 @@ get_visible_size (GtkMenu *menu)
 {
   GtkWidget *widget = GTK_WIDGET (menu);
   GtkContainer *container = GTK_CONTAINER (menu);
-  gint scroll_arrow_height;
   
   gint menu_height = (widget->allocation.height
                      - 2 * (container->border_width
                             + widget->style->ythickness));
-  
-  gtk_widget_style_get (GTK_WIDGET (menu),
-                        "scroll-arrow-vlength", &scroll_arrow_height,
-                        NULL);
 
-  if (menu->upper_arrow_visible && !menu->tearoff_active)
-    menu_height -= scroll_arrow_height;
-  if (menu->lower_arrow_visible && !menu->tearoff_active)
-    menu_height -= scroll_arrow_height;
+  if (!menu->tearoff_active)
+    {
+      GtkBorder arrow_border;
 
+      get_arrows_border (menu, &arrow_border);
+      menu_height -= arrow_border.top;
+      menu_height -= arrow_border.bottom;
+    }
+  
   return menu_height;
 }
 
@@ -4663,20 +4919,18 @@ get_menu_height (GtkMenu *menu)
 {
   gint height;
   GtkWidget *widget = GTK_WIDGET (menu);
-  gint scroll_arrow_height;
 
-  gtk_widget_style_get (GTK_WIDGET (menu),
-                        "scroll-arrow-vlength", &scroll_arrow_height,
-                        NULL);
-  
   height = widget->requisition.height;
   height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
 
-  if (menu->upper_arrow_visible && !menu->tearoff_active)
-    height -= scroll_arrow_height;
+  if (!menu->tearoff_active)
+    {
+      GtkBorder arrow_border;
 
-  if (menu->lower_arrow_visible && !menu->tearoff_active)
-    height -= scroll_arrow_height;
+      get_arrows_border (menu, &arrow_border);
+      height -= arrow_border.top;
+      height -= arrow_border.bottom;
+    }
 
   return height;
 }
@@ -4688,11 +4942,6 @@ gtk_menu_real_move_scroll (GtkMenu       *menu,
   gint page_size = get_visible_size (menu);
   gint end_position = get_menu_height (menu);
   GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
-  gint scroll_arrow_height;
-
-  gtk_widget_style_get (GTK_WIDGET (menu),
-                        "scroll-arrow-vlength", &scroll_arrow_height,
-                        NULL);
   
   switch (type)
     {
@@ -4732,11 +4981,14 @@ gtk_menu_real_move_scroll (GtkMenu       *menu,
          {
            GtkWidget *new_child;
            gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
+            GtkBorder arrow_border;
+
+           get_arrows_border (menu, &arrow_border);
 
            if (menu->scroll_offset != old_offset)
              step = menu->scroll_offset - old_offset;
 
-           step -= (new_upper_arrow_visible - old_upper_arrow_visible) * scroll_arrow_height;
+           step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
 
            new_child = child_at (menu, child_offset + step);
            if (new_child)
@@ -4793,6 +5045,28 @@ gtk_menu_set_monitor (GtkMenu *menu,
   priv->monitor_num = monitor_num;
 }
 
+/**
+ * gtk_menu_get_monitor:
+ * @menu: a #GtkMenu
+ *
+ * Retrieves the number of the monitor on which to show the menu.
+ *
+ * Returns: the number of the monitor on which the menu should
+ *    be popped up or -1
+ *
+ * Since: 2.14
+ **/
+gint
+gtk_menu_get_monitor (GtkMenu *menu)
+{
+  GtkMenuPrivate *priv;
+  g_return_val_if_fail (GTK_IS_MENU (menu), -1);
+
+  priv = gtk_menu_get_private (menu);
+  
+  return priv->monitor_num;
+}
+
 /**
  * gtk_menu_get_for_attach_widget:
  * @widget: a #GtkWidget