]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkmenu.c
Fixes #136082 and #135265, patch by Morten Welinder.
[~andy/gtk] / gtk / gtkmenu.c
index 47db2b26bf9cc00b7b97eee3ea83011206da38ae..80b2768a5b5201e17d74ef61463bbfb3adf0e950 100644 (file)
  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
  */
 
+#define GTK_MENU_INTERNALS
+
+#include <config.h>
 #include <string.h> /* memset */
 #include "gdk/gdkkeysyms.h"
+#include "gtkaccellabel.h"
 #include "gtkaccelmap.h"
 #include "gtkbindings.h"
 #include "gtklabel.h"
 #include "gtkmain.h"
+#include "gtkmarshalers.h"
 #include "gtkmenu.h"
 #include "gtkmenuitem.h"
-#include "gtksignal.h"
+#include "gtktearoffmenuitem.h"
 #include "gtkwindow.h"
 #include "gtkhbox.h"
 #include "gtkvscrollbar.h"
 #define MENU_ITEM_CLASS(w)   GTK_MENU_ITEM_GET_CLASS (w)
 #define        MENU_NEEDS_RESIZE(m) GTK_MENU_SHELL (m)->menu_flag
 
-#define SUBMENU_NAV_REGION_PADDING 2
-#define SUBMENU_NAV_HYSTERESIS_TIMEOUT 333
+#define DEFAULT_POPUP_DELAY    225
+#define DEFAULT_POPDOWN_DELAY  1000
+
+#define NAVIGATION_REGION_OVERSHOOT 50  /* How much the navigation region
+                                        * extends below the submenu
+                                        */
 
-#define MENU_SCROLL_STEP 10
+#define MENU_SCROLL_STEP1 8
+#define MENU_SCROLL_STEP2 15
 #define MENU_SCROLL_ARROW_HEIGHT 16
-#define MENU_SCROLL_FAST_ZONE 4
-#define MENU_SCROLL_TIMEOUT1 150
+#define MENU_SCROLL_FAST_ZONE 8
+#define MENU_SCROLL_TIMEOUT1 50
 #define MENU_SCROLL_TIMEOUT2 50
 
+#define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
+
 typedef struct _GtkMenuAttachData      GtkMenuAttachData;
+typedef struct _GtkMenuPrivate         GtkMenuPrivate;
 
 struct _GtkMenuAttachData
 {
@@ -60,21 +73,70 @@ struct _GtkMenuAttachData
   GtkMenuDetachFunc detacher;
 };
 
+struct _GtkMenuPrivate 
+{
+  gboolean have_position;
+  gint x;
+  gint y;
+
+  /* info used for the table */
+  guint rows;
+  guint columns;
+
+  guint *heights;
+  gint heights_length;
+
+  gint monitor_num;
+};
+
+typedef struct
+{
+  guint left_attach;
+  guint right_attach;
+  guint top_attach;
+  guint bottom_attach;
+}
+AttachInfo;
+
+enum {
+  MOVE_SCROLL,
+  LAST_SIGNAL
+};
+
 enum {
   PROP_0,
   PROP_TEAROFF_TITLE
 };
 
+enum
+{
+  CHILD_PROP_0,
+  CHILD_PROP_LEFT_ATTACH,
+  CHILD_PROP_RIGHT_ATTACH,
+  CHILD_PROP_TOP_ATTACH,
+  CHILD_PROP_BOTTOM_ATTACH
+};
+
 static void     gtk_menu_class_init        (GtkMenuClass     *klass);
 static void     gtk_menu_init              (GtkMenu          *menu);
-static void     gtk_menu_set_property      (GObject      *object,
-                                           guint         prop_id,
-                                           const GValue *value,
-                                           GParamSpec   *pspec);
-static void     gtk_menu_get_property      (GObject     *object,
-                                           guint        prop_id,
-                                           GValue      *value,
-                                           GParamSpec  *pspec);
+static void     gtk_menu_set_property      (GObject          *object,
+                                           guint             prop_id,
+                                           const GValue     *value,
+                                           GParamSpec       *pspec);
+static void     gtk_menu_get_property      (GObject          *object,
+                                           guint             prop_id,
+                                           GValue           *value,
+                                           GParamSpec       *pspec);
+static void     gtk_menu_set_child_property(GtkContainer     *container,
+                                            GtkWidget        *child,
+                                            guint             property_id,
+                                            const GValue     *value,
+                                            GParamSpec       *pspec);
+static void     gtk_menu_get_child_property(GtkContainer     *container,
+                                            GtkWidget        *child,
+                                            guint             property_id,
+                                            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);
@@ -90,6 +152,10 @@ static gboolean gtk_menu_expose            (GtkWidget        *widget,
                                            GdkEventExpose   *event);
 static gboolean gtk_menu_key_press         (GtkWidget        *widget,
                                            GdkEventKey      *event);
+static gboolean gtk_menu_button_press      (GtkWidget        *widget,
+                                           GdkEventButton   *event);
+static gboolean gtk_menu_button_release    (GtkWidget        *widget,
+                                           GdkEventButton   *event);
 static gboolean gtk_menu_motion_notify     (GtkWidget        *widget,
                                            GdkEventMotion   *event);
 static gboolean gtk_menu_enter_notify      (GtkWidget        *widget,
@@ -98,12 +164,18 @@ static gboolean gtk_menu_leave_notify      (GtkWidget        *widget,
                                            GdkEventCrossing *event);
 static void     gtk_menu_scroll_to         (GtkMenu          *menu,
                                            gint              offset);
-static void     gtk_menu_stop_scrolling    (GtkMenu          *menu);
-static gboolean gtk_menu_scroll_timeout    (gpointer          data);
+
+static void     gtk_menu_stop_scrolling        (GtkMenu  *menu);
+static void     gtk_menu_remove_scroll_timeout (GtkMenu  *menu);
+static gboolean gtk_menu_scroll_timeout        (gpointer  data);
+
 static void     gtk_menu_scroll_item_visible (GtkMenuShell    *menu_shell,
                                              GtkWidget       *menu_item);
 static void     gtk_menu_select_item       (GtkMenuShell     *menu_shell,
                                            GtkWidget        *menu_item);
+static void     gtk_menu_do_insert         (GtkMenuShell     *menu_shell,
+                                            GtkWidget        *child,
+                                            gint              position);
 static void     gtk_menu_real_insert       (GtkMenuShell     *menu_shell,
                                            GtkWidget        *child,
                                            gint              position);
@@ -113,6 +185,15 @@ static void     gtk_menu_handle_scrolling  (GtkMenu          *menu,
                                            gboolean         enter);
 static void     gtk_menu_set_tearoff_hints (GtkMenu          *menu,
                                            gint             width);
+static void     gtk_menu_style_set         (GtkWidget        *widget,
+                                           GtkStyle         *previous_style);
+static gboolean gtk_menu_focus             (GtkWidget        *widget,
+                                           GtkDirectionType direction);
+static gint     gtk_menu_get_popup_delay   (GtkMenuShell     *menu_shell);
+static void     gtk_menu_move_current      (GtkMenuShell     *menu_shell,
+                                            GtkMenuDirectionType direction);
+static void     gtk_menu_real_move_scroll  (GtkMenu          *menu,
+                                           GtkScrollType     type);
 
 static void     gtk_menu_stop_navigating_submenu       (GtkMenu          *menu);
 static gboolean gtk_menu_stop_navigating_submenu_cb    (gpointer          user_data);
@@ -138,32 +219,71 @@ static void gtk_menu_update_title   (GtkMenu           *menu);
 static void       menu_grab_transfer_window_destroy (GtkMenu *menu);
 static GdkWindow *menu_grab_transfer_window_get     (GtkMenu *menu);
 
+static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
+                                                  guint      signal_id);
 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
                                           gboolean group_changed);
 
 static GtkMenuShellClass *parent_class = NULL;
 static const gchar      *attach_data_key = "gtk-menu-attach-data";
 
-GtkType
+static guint menu_signals[LAST_SIGNAL] = { 0 };
+
+static void
+gtk_menu_free_private (gpointer data)
+{
+  GtkMenuPrivate *priv = (GtkMenuPrivate *)data;
+
+  g_free (priv->heights);
+
+  g_free (priv);
+}
+
+GtkMenuPrivate *
+gtk_menu_get_private (GtkMenu *menu)
+{
+  GtkMenuPrivate *private;
+  static GQuark private_quark = 0;
+
+  if (!private_quark)
+    private_quark = g_quark_from_static_string ("gtk-menu-private");
+
+  private = g_object_get_qdata (G_OBJECT (menu), private_quark);
+
+  if (!private)
+    {
+      private = g_new0 (GtkMenuPrivate, 1);
+      private->have_position = FALSE;
+      
+      g_object_set_qdata_full (G_OBJECT (menu), private_quark,
+                              private, gtk_menu_free_private);
+    }
+
+  return private;
+}
+
+GType
 gtk_menu_get_type (void)
 {
-  static GtkType menu_type = 0;
+  static GType menu_type = 0;
   
   if (!menu_type)
     {
-      static const GtkTypeInfo menu_info =
+      static const GTypeInfo menu_info =
       {
-       "GtkMenu",
-       sizeof (GtkMenu),
        sizeof (GtkMenuClass),
-       (GtkClassInitFunc) gtk_menu_class_init,
-       (GtkObjectInitFunc) gtk_menu_init,
-       /* reserved_1 */ NULL,
-       /* reserved_2 */ NULL,
-        (GtkClassInitFunc) NULL,
+       NULL,           /* base_init */
+       NULL,           /* base_finalize */
+       (GClassInitFunc) gtk_menu_class_init,
+       NULL,           /* class_finalize */
+       NULL,           /* class_data */
+       sizeof (GtkMenu),
+       0,              /* n_preallocs */
+       (GInstanceInitFunc) gtk_menu_init,
       };
       
-      menu_type = gtk_type_unique (gtk_menu_shell_get_type (), &menu_info);
+      menu_type = g_type_register_static (GTK_TYPE_MENU_SHELL, "GtkMenu",
+                                         &menu_info, 0);
     }
   
   return menu_type;
@@ -185,13 +305,6 @@ gtk_menu_class_init (GtkMenuClass *class)
   gobject_class->set_property = gtk_menu_set_property;
   gobject_class->get_property = gtk_menu_get_property;
 
-  g_object_class_install_property (gobject_class,
-                                   PROP_TEAROFF_TITLE,
-                                   g_param_spec_string ("tearoff-title",
-                                                        _("Tearoff Title"),
-                                                        _("A title that may be displayed by the window manager when this menu is torn-off."),
-                                                        "",
-                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));
   object_class->destroy = gtk_menu_destroy;
   
   widget_class->realize = gtk_menu_realize;
@@ -201,18 +314,106 @@ gtk_menu_class_init (GtkMenuClass *class)
   widget_class->show = gtk_menu_show;
   widget_class->expose_event = gtk_menu_expose;
   widget_class->key_press_event = gtk_menu_key_press;
+  widget_class->button_press_event = gtk_menu_button_press;
+  widget_class->button_release_event = gtk_menu_button_release;
   widget_class->motion_notify_event = gtk_menu_motion_notify;
   widget_class->show_all = gtk_menu_show_all;
   widget_class->hide_all = gtk_menu_hide_all;
   widget_class->enter_notify_event = gtk_menu_enter_notify;
   widget_class->leave_notify_event = gtk_menu_leave_notify;
+  widget_class->motion_notify_event = gtk_menu_motion_notify;
+  widget_class->style_set = gtk_menu_style_set;
+  widget_class->focus = gtk_menu_focus;
+  widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
 
   container_class->remove = gtk_menu_remove;
+  container_class->get_child_property = gtk_menu_get_child_property;
+  container_class->set_child_property = gtk_menu_set_child_property;
   
   menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
   menu_shell_class->deactivate = gtk_menu_deactivate;
   menu_shell_class->select_item = gtk_menu_select_item;
   menu_shell_class->insert = gtk_menu_real_insert;
+  menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
+  menu_shell_class->move_current = gtk_menu_move_current;
+
+  menu_signals[MOVE_SCROLL] =
+    _gtk_binding_signal_new ("move_scroll",
+                            G_OBJECT_CLASS_TYPE (object_class),
+                            G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
+                            G_CALLBACK (gtk_menu_real_move_scroll),
+                            NULL, NULL,
+                            _gtk_marshal_VOID__ENUM,
+                            G_TYPE_NONE, 1,
+                            GTK_TYPE_SCROLL_TYPE);
+  
+  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"),
+                                                        "",
+                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));
+
+  gtk_widget_class_install_style_property (widget_class,
+                                          g_param_spec_int ("vertical-padding",
+                                                            P_("Vertical Padding"),
+                                                            P_("Extra space at the top and bottom of the menu"),
+                                                            0,
+                                                            G_MAXINT,
+                                                            1,
+                                                            G_PARAM_READABLE));
+
+  gtk_widget_class_install_style_property (widget_class,
+                                          g_param_spec_int ("vertical-offset",
+                                                            P_("Vertical Offset"),
+                                                            P_("When the menu is a submenu, position it this number of pixels offset vertically"),
+                                                            G_MININT,
+                                                            G_MAXINT,
+                                                            0,
+                                                            G_PARAM_READABLE));
+
+  gtk_widget_class_install_style_property (widget_class,
+                                          g_param_spec_int ("horizontal-offset",
+                                                            P_("Horizontal Offset"),
+                                                            P_("When the menu is a submenu, position it this number of pixels offset horizontally"),
+                                                            G_MININT,
+                                                            G_MAXINT,
+                                                            -2,
+                                                            G_PARAM_READABLE));
+
+
+ gtk_container_class_install_child_property (container_class,
+                                             CHILD_PROP_LEFT_ATTACH,
+                                             g_param_spec_uint ("left_attach",
+                                                               P_("Left Attach"),
+                                                               P_("The column number to attach the left side of the child to"),
+                                                               0, UINT_MAX, 0,
+                                                               G_PARAM_READWRITE));
+
+ gtk_container_class_install_child_property (container_class,
+                                             CHILD_PROP_RIGHT_ATTACH,
+                                             g_param_spec_uint ("right_attach",
+                                                               P_("Right Attach"),
+                                                               P_("The column number to attach the right side of the child to"),
+                                                               0, UINT_MAX, 0,
+                                                               G_PARAM_READWRITE));
+
+ gtk_container_class_install_child_property (container_class,
+                                             CHILD_PROP_TOP_ATTACH,
+                                             g_param_spec_uint ("top_attach",
+                                                               P_("Top Attach"),
+                                                               P_("The row number to attach the top of the child to"),
+                                                               0, UINT_MAX, 0,
+                                                               G_PARAM_READWRITE));
+
+ gtk_container_class_install_child_property (container_class,
+                                             CHILD_PROP_BOTTOM_ATTACH,
+                                             g_param_spec_uint ("bottom_attach",
+                                                               P_("Bottom Attach"),
+                                                               P_("The row number to attach the bottom of the child to"),
+                                                               0, UINT_MAX, 0,
+                                                               G_PARAM_READWRITE));
 
   binding_set = gtk_binding_set_by_class (class);
   gtk_binding_entry_add_signal (binding_set,
@@ -255,12 +456,69 @@ gtk_menu_class_init (GtkMenuClass *class)
                                "move_current", 1,
                                GTK_TYPE_MENU_DIRECTION_TYPE,
                                GTK_MENU_DIR_CHILD);
+  gtk_binding_entry_add_signal (binding_set,
+                               GDK_Home, 0,
+                               "move_scroll", 1,
+                               GTK_TYPE_SCROLL_TYPE,
+                               GTK_SCROLL_START);
+  gtk_binding_entry_add_signal (binding_set,
+                               GDK_KP_Home, 0,
+                               "move_scroll", 1,
+                               GTK_TYPE_SCROLL_TYPE,
+                               GTK_SCROLL_START);
+  gtk_binding_entry_add_signal (binding_set,
+                               GDK_End, 0,
+                               "move_scroll", 1,
+                               GTK_TYPE_SCROLL_TYPE,
+                               GTK_SCROLL_END);
+  gtk_binding_entry_add_signal (binding_set,
+                               GDK_KP_End, 0,
+                               "move_scroll", 1,
+                               GTK_TYPE_SCROLL_TYPE,
+                               GTK_SCROLL_END);
+  gtk_binding_entry_add_signal (binding_set,
+                               GDK_Page_Up, 0,
+                               "move_scroll", 1,
+                               GTK_TYPE_SCROLL_TYPE,
+                               GTK_SCROLL_PAGE_UP);
+  gtk_binding_entry_add_signal (binding_set,
+                               GDK_KP_Page_Up, 0,
+                               "move_scroll", 1,
+                               GTK_TYPE_SCROLL_TYPE,
+                               GTK_SCROLL_PAGE_UP);
+  gtk_binding_entry_add_signal (binding_set,
+                               GDK_Page_Down, 0,
+                               "move_scroll", 1,
+                               GTK_TYPE_SCROLL_TYPE,
+                               GTK_SCROLL_PAGE_DOWN);
+  gtk_binding_entry_add_signal (binding_set,
+                               GDK_KP_Page_Down, 0,
+                               "move_scroll", 1,
+                               GTK_TYPE_SCROLL_TYPE,
+                               GTK_SCROLL_PAGE_DOWN);
 
   gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
-                                                      _("Can change accelerators"),
-                                                      _("Whether menu accelerators can be changed by pressing a key over the menu item."),
+                                                      P_("Can change accelerators"),
+                                                      P_("Whether menu accelerators can be changed by pressing a key over the menu item"),
                                                       FALSE,
                                                       G_PARAM_READWRITE));
+
+  gtk_settings_install_property (g_param_spec_int ("gtk-menu-popup-delay",
+                                                  P_("Delay before submenus appear"),
+                                                  P_("Minimum time the pointer must stay over a menu item before the submenu appear"),
+                                                  0,
+                                                  G_MAXINT,
+                                                  DEFAULT_POPUP_DELAY,
+                                                  G_PARAM_READWRITE));
+
+  gtk_settings_install_property (g_param_spec_int ("gtk-menu-popdown-delay",
+                                                  P_("Delay before hiding a submenu"),
+                                                  P_("The time before hiding a submenu when the pointer is moving towards the submenu"),
+                                                  0,
+                                                  G_MAXINT,
+                                                  DEFAULT_POPDOWN_DELAY,
+                                                  G_PARAM_READWRITE));
+                                                  
 }
 
 
@@ -306,6 +564,102 @@ gtk_menu_get_property (GObject     *object,
     }
 }
 
+static AttachInfo *
+get_attach_info (GObject *child)
+{
+  AttachInfo *ai = g_object_get_data (child, ATTACH_INFO_KEY);
+
+  if (!ai)
+    {
+      ai = g_new0 (AttachInfo, 1);
+      g_object_set_data_full (child, ATTACH_INFO_KEY, ai, g_free);
+    }
+
+  return ai;
+}
+
+static void
+gtk_menu_set_child_property (GtkContainer *container,
+                             GtkWidget    *child,
+                             guint         property_id,
+                             const GValue *value,
+                             GParamSpec   *pspec)
+{
+  GtkMenu *menu = GTK_MENU (container);
+  GtkMenuPrivate *priv;
+  AttachInfo *ai = get_attach_info (G_OBJECT (child));
+
+  priv = gtk_menu_get_private (menu);
+
+  switch (property_id)
+    {
+      case CHILD_PROP_LEFT_ATTACH:
+        ai->left_attach = g_value_get_uint (value);
+        break;
+      case CHILD_PROP_RIGHT_ATTACH:
+        ai->right_attach = g_value_get_uint (value);
+       priv->columns = MAX (priv->columns, ai->right_attach);
+        break;
+      case CHILD_PROP_TOP_ATTACH:
+        ai->top_attach = g_value_get_uint (value);
+        break;
+      case CHILD_PROP_BOTTOM_ATTACH:
+        ai->bottom_attach = g_value_get_uint (value);
+       priv->rows = MAX (priv->rows, ai->bottom_attach);
+        break;
+      default:
+        GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
+        return;
+    }
+
+  gtk_widget_queue_resize (GTK_WIDGET (menu));
+}
+
+static void
+gtk_menu_get_child_property (GtkContainer *container,
+                             GtkWidget    *child,
+                             guint         property_id,
+                             GValue       *value,
+                             GParamSpec   *pspec)
+{
+  AttachInfo *ai = get_attach_info (G_OBJECT (child));
+
+  switch (property_id)
+    {
+      case CHILD_PROP_LEFT_ATTACH:
+        g_value_set_uint (value, ai->left_attach);
+        break;
+      case CHILD_PROP_RIGHT_ATTACH:
+        g_value_set_uint (value, ai->right_attach);
+        break;
+      case CHILD_PROP_TOP_ATTACH:
+        g_value_set_uint (value, ai->top_attach);
+        break;
+      case CHILD_PROP_BOTTOM_ATTACH:
+        g_value_set_uint (value, ai->bottom_attach);
+        break;
+
+      default:
+        GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
+        return;
+    }
+}
+
+static void
+get_child_attach (GtkWidget *child,
+                  gint      *l,
+                  gint      *r,
+                  gint      *t,
+                  gint      *b)
+{
+  gtk_container_child_get (GTK_CONTAINER (child->parent), child,
+                           "left_attach", l,
+                           "right_attach", r,
+                           "top_attach", t,
+                           "bottom_attach", b,
+                           NULL);
+}
+
 static gboolean
 gtk_menu_window_event (GtkWidget *window,
                       GdkEvent  *event,
@@ -313,8 +667,8 @@ gtk_menu_window_event (GtkWidget *window,
 {
   gboolean handled = FALSE;
 
-  gtk_widget_ref (window);
-  gtk_widget_ref (menu);
+  g_object_ref (window);
+  g_object_ref (menu);
 
   switch (event->type)
     {
@@ -326,15 +680,39 @@ gtk_menu_window_event (GtkWidget *window,
       break;
     }
 
-  gtk_widget_unref (window);
-  gtk_widget_unref (menu);
+  g_object_unref (window);
+  g_object_unref (menu);
 
   return handled;
 }
 
+static void
+gtk_menu_window_size_request (GtkWidget      *window,
+                             GtkRequisition *requisition,
+                             GtkMenu        *menu)
+{
+  GtkMenuPrivate *private = gtk_menu_get_private (menu);
+
+  if (private->have_position)
+    {
+      GdkScreen *screen = gtk_widget_get_screen (window);
+      GdkRectangle monitor;
+      
+      gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
+
+      if (private->y + requisition->height > monitor.y + monitor.height)
+       requisition->height = monitor.y + monitor.height - private->y;
+
+      if (private->y < monitor.y)
+       requisition->height -= monitor.y - private->y;
+    }
+}
+
 static void
 gtk_menu_init (GtkMenu *menu)
 {
+  GtkMenuPrivate *priv = gtk_menu_get_private (menu);
+
   menu->parent_menu_item = NULL;
   menu->old_active_menu_item = NULL;
   menu->accel_group = NULL;
@@ -342,15 +720,15 @@ gtk_menu_init (GtkMenu *menu)
   menu->position_func_data = NULL;
   menu->toggle_size = 0;
 
-  menu->toplevel = g_object_connect (gtk_widget_new (GTK_TYPE_WINDOW,
-                                                    "type", GTK_WINDOW_POPUP,
-                                                    "child", menu,
-                                                    NULL),
+  menu->toplevel = g_object_connect (g_object_new (GTK_TYPE_WINDOW,
+                                                  "type", GTK_WINDOW_POPUP,
+                                                  "child", menu,
+                                                  NULL),
                                     "signal::event", gtk_menu_window_event, menu,
+                                    "signal::size_request", gtk_menu_window_size_request, menu,
                                     "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
                                     NULL);
-  gtk_window_set_policy (GTK_WINDOW (menu->toplevel),
-                        FALSE, FALSE, TRUE);
+  gtk_window_set_resizable (GTK_WINDOW (menu->toplevel), FALSE);
   gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
 
   /* Refloat the menu, so that reference counting for the menu isn't
@@ -380,6 +758,8 @@ gtk_menu_init (GtkMenu *menu)
   menu->lower_arrow_prelight = FALSE;
   
   MENU_NEEDS_RESIZE (menu) = TRUE;
+
+  priv->columns = 1;
 }
 
 static void
@@ -394,7 +774,7 @@ gtk_menu_destroy (GtkObject *object)
 
   gtk_menu_stop_scrolling (menu);
   
-  data = gtk_object_get_data (object, attach_data_key);
+  data = g_object_get_data (G_OBJECT (object), attach_data_key);
   if (data)
     gtk_menu_detach (menu);
   
@@ -402,7 +782,7 @@ gtk_menu_destroy (GtkObject *object)
 
   if (menu->old_active_menu_item)
     {
-      gtk_widget_unref (menu->old_active_menu_item);
+      g_object_unref (menu->old_active_menu_item);
       menu->old_active_menu_item = NULL;
     }
 
@@ -410,7 +790,7 @@ gtk_menu_destroy (GtkObject *object)
   if (menu->needs_destruction_ref_count)
     {
       menu->needs_destruction_ref_count = FALSE;
-      gtk_object_ref (object);
+      g_object_ref (object);
     }
   
   if (menu->accel_group)
@@ -437,6 +817,34 @@ gtk_menu_finalize (GObject *object)
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
+static void
+menu_change_screen (GtkMenu   *menu,
+                   GdkScreen *new_screen)
+{
+  GtkMenuPrivate *private = gtk_menu_get_private (menu);
+
+  if (menu->torn_off)
+    {
+      gtk_window_set_screen (GTK_WINDOW (menu->tearoff_window), new_screen);
+      gtk_menu_position (menu);
+    }
+
+  gtk_window_set_screen (GTK_WINDOW (menu->toplevel), new_screen);
+  private->monitor_num = -1;
+}
+
+static void
+attach_widget_screen_changed (GtkWidget *attach_widget,
+                             GdkScreen *previous_screen,
+                             GtkMenu   *menu)
+{
+  if (gtk_widget_has_screen (attach_widget) &&
+      !g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
+    {
+      menu_change_screen (menu, gtk_widget_get_screen (attach_widget));
+    }
+}
+
 void
 gtk_menu_attach_to_widget (GtkMenu            *menu,
                           GtkWidget           *attach_widget,
@@ -451,21 +859,26 @@ gtk_menu_attach_to_widget (GtkMenu               *menu,
   /* keep this function in sync with gtk_widget_set_parent()
    */
   
-  data = gtk_object_get_data (GTK_OBJECT (menu), attach_data_key);
+  data = g_object_get_data (G_OBJECT (menu), attach_data_key);
   if (data)
     {
       g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
-                gtk_type_name (GTK_OBJECT_TYPE (data->attach_widget)));
-      return;
+                g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
+     return;
     }
   
-  gtk_object_ref (GTK_OBJECT (menu));
+  g_object_ref (menu);
   gtk_object_sink (GTK_OBJECT (menu));
   
   data = g_new (GtkMenuAttachData, 1);
   data->attach_widget = attach_widget;
+  
+  g_signal_connect (attach_widget, "screen_changed",
+                   G_CALLBACK (attach_widget_screen_changed), menu);
+  attach_widget_screen_changed (attach_widget, NULL, menu);
+  
   data->detacher = detacher;
-  gtk_object_set_data (GTK_OBJECT (menu), attach_data_key, data);
+  g_object_set_data (G_OBJECT (menu), attach_data_key, data);
   
   if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
     gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
@@ -485,7 +898,7 @@ gtk_menu_get_attach_widget (GtkMenu *menu)
   
   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
   
-  data = gtk_object_get_data (GTK_OBJECT (menu), attach_data_key);
+  data = g_object_get_data (G_OBJECT (menu), attach_data_key);
   if (data)
     return data->attach_widget;
   return NULL;
@@ -500,14 +913,18 @@ gtk_menu_detach (GtkMenu *menu)
   
   /* keep this function in sync with gtk_widget_unparent()
    */
-  data = gtk_object_get_data (GTK_OBJECT (menu), attach_data_key);
+  data = g_object_get_data (G_OBJECT (menu), attach_data_key);
   if (!data)
     {
       g_warning ("gtk_menu_detach(): menu is not attached");
       return;
     }
-  gtk_object_remove_data (GTK_OBJECT (menu), attach_data_key);
+  g_object_set_data (G_OBJECT (menu), attach_data_key, NULL);
   
+  g_signal_handlers_disconnect_by_func (data->attach_widget,
+                                       (gpointer) attach_widget_screen_changed,
+                                       menu);
+
   data->detacher (data->attach_widget, menu);
   
   if (GTK_WIDGET_REALIZED (menu))
@@ -518,7 +935,46 @@ gtk_menu_detach (GtkMenu *menu)
   /* Fallback title for menu comes from attach widget */
   gtk_menu_update_title (menu);
 
-  gtk_widget_unref (GTK_WIDGET (menu));
+  g_object_unref (menu);
+}
+
+static void
+gtk_menu_do_remove (GtkMenuShell *menu_shell,
+                   GtkWidget    *child)
+{
+  AttachInfo *ai;
+  GList *children;
+  GtkMenuPrivate *priv;
+  gint delta;
+  gboolean single_column;
+
+  ai = get_attach_info (G_OBJECT (child));
+  priv = gtk_menu_get_private (GTK_MENU (menu_shell));
+  delta = ai->bottom_attach - ai->top_attach;
+  single_column = priv->columns == 1;
+
+  /* Recalculate these, assuming the child has already been removed. 
+   * Note that an empty menu is assumed to have one column
+   */
+  priv->rows = 0;
+  priv->columns = 1;
+
+  for (children = menu_shell->children; children; children = children->next)
+    {
+      AttachInfo *child_ai = get_attach_info (children->data);
+      
+      /* Do not move items in table menus */
+      if (single_column && child_ai->bottom_attach > ai->bottom_attach)
+        gtk_container_child_set (GTK_CONTAINER (menu_shell), children->data,
+                                 "top_attach", child_ai->top_attach - delta,
+                                 "bottom_attach", child_ai->bottom_attach - delta,
+                                 NULL);
+      else
+       {
+         priv->columns = MAX (priv->columns, child_ai->right_attach);
+         priv->rows = MAX (priv->rows, child_ai->bottom_attach);
+       }
+    }
 }
 
 static void 
@@ -526,6 +982,7 @@ gtk_menu_remove (GtkContainer *container,
                 GtkWidget    *widget)
 {
   GtkMenu *menu;
+
   g_return_if_fail (GTK_IS_MENU (container));
   g_return_if_fail (GTK_IS_MENU_ITEM (widget));
 
@@ -535,18 +992,62 @@ gtk_menu_remove (GtkContainer *container,
    */
   if (menu->old_active_menu_item == widget)
     {
-      gtk_widget_unref (menu->old_active_menu_item);
+      g_object_unref (menu->old_active_menu_item);
       menu->old_active_menu_item = NULL;
     }
 
   GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
+  gtk_menu_do_remove (GTK_MENU_SHELL (container), widget);
+  g_object_set_data (G_OBJECT (widget), ATTACH_INFO_KEY, NULL);
 }
 
 
 GtkWidget*
 gtk_menu_new (void)
 {
-  return GTK_WIDGET (gtk_type_new (gtk_menu_get_type ()));
+  return g_object_new (GTK_TYPE_MENU, NULL);
+}
+
+static void
+gtk_menu_do_insert (GtkMenuShell *menu_shell,
+                    GtkWidget    *child,
+                    gint          position)
+{
+  GList *children;
+  GtkMenuPrivate *priv;
+
+  priv = gtk_menu_get_private (GTK_MENU (menu_shell));
+
+  if (position < 0 || position >= priv->rows)
+    {
+      /* attach after the last row */
+      gtk_menu_attach (GTK_MENU (menu_shell), child,
+                       0, priv->columns,
+                       priv->rows, priv->rows + 1);
+
+      return;
+    }
+
+  /* We need to make space for this new item; move all items with
+   * top >= position one down. 
+   * Note that this does not prevent overlaps when the menu contains 
+   * vertically spanning items.
+   */
+  for (children = menu_shell->children; children; children = children->next)
+    {
+      AttachInfo *child_ai = get_attach_info (children->data);
+
+      if (child_ai->top_attach >= position)
+        gtk_container_child_set (GTK_CONTAINER (menu_shell), children->data,
+                                 "top_attach", child_ai->top_attach + 1,
+                                 "bottom_attach", child_ai->bottom_attach + 1,
+                                 NULL);
+    }
+
+  /* attach the new item */
+  gtk_menu_attach (GTK_MENU (menu_shell), child,
+                   0, priv->columns,
+                   position, position + 1);
 }
 
 static void
@@ -556,8 +1057,10 @@ gtk_menu_real_insert (GtkMenuShell     *menu_shell,
 {
   if (GTK_WIDGET_REALIZED (menu_shell))
     gtk_widget_set_parent_window (child, GTK_MENU (menu_shell)->bin_window);
-  
+
   GTK_MENU_SHELL_CLASS (parent_class)->insert (menu_shell, child, position);
+
+  gtk_menu_do_insert (menu_shell, child, position);
 }
 
 static void
@@ -581,24 +1084,24 @@ gtk_menu_tearoff_bg_copy (GtkMenu *menu)
       gc = gdk_gc_new_with_values (widget->window,
                                   &gc_values, GDK_GC_SUBWINDOW);
       
-      gdk_window_get_size (menu->tearoff_window->window, &width, &height);
+      gdk_drawable_get_size (menu->tearoff_window->window, &width, &height);
       
       pixmap = gdk_pixmap_new (menu->tearoff_window->window,
                               width,
                               height,
                               -1);
 
-      gdk_draw_pixmap (pixmap, gc,
-                      menu->tearoff_window->window,
-                      0, 0, 0, 0, -1, -1);
-      gdk_gc_unref (gc);
+      gdk_draw_drawable (pixmap, gc,
+                        menu->tearoff_window->window,
+                        0, 0, 0, 0, -1, -1);
+      g_object_unref (gc);
 
-      gtk_widget_set_usize (menu->tearoff_window,
-                           width,
-                           height);
+      gtk_widget_set_size_request (menu->tearoff_window,
+                                  width,
+                                  height);
 
       gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
-      gdk_pixmap_unref (pixmap);
+      g_object_unref (pixmap);
     }
 }
 
@@ -617,7 +1120,8 @@ popup_grab_on_window (GdkWindow *window,
        return TRUE;
       else
        {
-         gdk_pointer_ungrab (activate_time);
+         gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
+                                     activate_time);
          return FALSE;
        }
     }
@@ -625,6 +1129,32 @@ popup_grab_on_window (GdkWindow *window,
   return FALSE;
 }
 
+/**
+ * gtk_menu_popup:
+ * @menu: a #GtkMenu.
+ * @parent_menu_shell: the menu shell containing the triggering menu item, or %NULL
+ * @parent_menu_item: the menu item whose activation triggered the popup, or %NULL
+ * @func: a user supplied function used to position the menu, or %NULL
+ * @data: user supplied data to be passed to @func.
+ * @button: the mouse button which was pressed to initiate the event.
+ * @activate_time: the time at which the activation event occurred.
+ *
+ * Displays a menu and makes it available for selection.  Applications can use
+ * this function to display context-sensitive menus, and will typically supply
+ * %NULL for the @parent_menu_shell, @parent_menu_item, @func and @data 
+ * parameters. The default menu positioning function will position the menu
+ * at the current mouse cursor position.
+ *
+ * The @button parameter should be the mouse button pressed to initiate
+ * the menu popup. If the menu popup was initiated by something other than
+ * 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.
+ *
+ */
 void
 gtk_menu_popup (GtkMenu                    *menu,
                GtkWidget           *parent_menu_shell,
@@ -646,7 +1176,7 @@ gtk_menu_popup (GtkMenu                *menu,
   menu_shell = GTK_MENU_SHELL (menu);
   
   menu_shell->parent_menu_shell = parent_menu_shell;
-
+  
   /* Find the last viewable ancestor, and make an X grab on it
    */
   parent = GTK_WIDGET (menu);
@@ -709,6 +1239,7 @@ gtk_menu_popup (GtkMenu                *menu,
        * try again.
        */
       menu_shell->parent_menu_shell = NULL;
+      menu_grab_transfer_window_destroy (menu);
       return;
     }
 
@@ -729,6 +1260,8 @@ gtk_menu_popup (GtkMenu                *menu,
 
       gdk_event_free (current_event);
     }
+  else
+    menu_shell->ignore_enter = TRUE;
 
   if (menu->torn_off)
     {
@@ -736,6 +1269,16 @@ gtk_menu_popup (GtkMenu               *menu,
 
       gtk_menu_reparent (menu, menu->toplevel, FALSE);
     }
+  if (parent_menu_shell) 
+    {
+      GtkWidget *toplevel;
+
+      toplevel = gtk_widget_get_toplevel (parent_menu_shell);
+      if (GTK_IS_WINDOW (toplevel))
+       gtk_window_group_add_window (_gtk_window_get_group (GTK_WINDOW (toplevel)), 
+                                    GTK_WINDOW (menu->toplevel));
+    }
   
   menu->parent_menu_item = parent_menu_item;
   menu->position_func = func;
@@ -748,8 +1291,12 @@ gtk_menu_popup (GtkMenu               *menu,
    */
   gtk_widget_show (GTK_WIDGET (menu));
 
+  /* Position the menu, possibly changing the size request
+   */
+  gtk_menu_position (menu);
+
   /* Compute the size of the toplevel and realize it so we
-   * can position and scroll correctly.
+   * can scroll correctly.
    */
   {
     GtkRequisition tmp_request;
@@ -765,8 +1312,6 @@ gtk_menu_popup (GtkMenu                *menu,
     gtk_widget_realize (GTK_WIDGET (menu));
   }
 
-  gtk_menu_position (menu);
-
   gtk_menu_scroll_to (menu, menu->scroll_offset);
 
   /* Once everything is set up correctly, map the toplevel window on
@@ -776,23 +1321,26 @@ gtk_menu_popup (GtkMenu              *menu,
 
   if (xgrab_shell == widget)
     popup_grab_on_window (widget->window, activate_time); /* Should always succeed */
-
   gtk_grab_add (GTK_WIDGET (menu));
 }
 
 void
 gtk_menu_popdown (GtkMenu *menu)
 {
+  GtkMenuPrivate *private;
   GtkMenuShell *menu_shell;
 
   g_return_if_fail (GTK_IS_MENU (menu));
   
   menu_shell = GTK_MENU_SHELL (menu);
+  private = gtk_menu_get_private (menu);
   
   menu_shell->parent_menu_shell = NULL;
   menu_shell->active = FALSE;
   menu_shell->ignore_enter = FALSE;
 
+  private->have_position = FALSE;
+
   gtk_menu_stop_scrolling (menu);
   
   gtk_menu_stop_navigating_submenu (menu);
@@ -800,9 +1348,9 @@ gtk_menu_popdown (GtkMenu *menu)
   if (menu_shell->active_menu_item)
     {
       if (menu->old_active_menu_item)
-       gtk_widget_unref (menu->old_active_menu_item);
+       g_object_unref (menu->old_active_menu_item);
       menu->old_active_menu_item = menu_shell->active_menu_item;
-      gtk_widget_ref (menu->old_active_menu_item);
+      g_object_ref (menu->old_active_menu_item);
     }
 
   gtk_menu_shell_deselect (menu_shell);
@@ -810,14 +1358,11 @@ gtk_menu_popdown (GtkMenu *menu)
   /* The X Grab, if present, will automatically be removed when we hide
    * the window */
   gtk_widget_hide (menu->toplevel);
+  gtk_window_group_add_window (_gtk_window_get_group (NULL), GTK_WINDOW (menu->toplevel));
 
   if (menu->torn_off)
     {
-      gint width, height;
-      gdk_window_get_size (menu->tearoff_window->window, &width, &height);
-      gtk_widget_set_usize (menu->tearoff_window,
-                           -1,
-                           height);
+      gtk_widget_set_size_request (menu->tearoff_window, -1, -1);
       
       if (GTK_BIN (menu->toplevel)->child) 
        {
@@ -830,8 +1375,10 @@ gtk_menu_popdown (GtkMenu *menu)
           */
          if (menu_shell->have_xgrab)
            {
-             gdk_pointer_ungrab (GDK_CURRENT_TIME);
-             gdk_keyboard_ungrab (GDK_CURRENT_TIME);
+             GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
+             
+             gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
+             gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
            }
        }
 
@@ -877,7 +1424,7 @@ gtk_menu_get_active (GtkMenu *menu)
       
       menu->old_active_menu_item = child;
       if (menu->old_active_menu_item)
-       gtk_widget_ref (menu->old_active_menu_item);
+       g_object_ref (menu->old_active_menu_item);
     }
   
   return menu->old_active_menu_item;
@@ -899,9 +1446,9 @@ gtk_menu_set_active (GtkMenu *menu,
       if (GTK_BIN (child)->child)
        {
          if (menu->old_active_menu_item)
-           gtk_widget_unref (menu->old_active_menu_item);
+           g_object_unref (menu->old_active_menu_item);
          menu->old_active_menu_item = child;
-         gtk_widget_ref (menu->old_active_menu_item);
+         g_object_ref (menu->old_active_menu_item);
        }
     }
 }
@@ -915,10 +1462,10 @@ gtk_menu_set_accel_group (GtkMenu        *menu,
   if (menu->accel_group != accel_group)
     {
       if (menu->accel_group)
-       gtk_accel_group_unref (menu->accel_group);
+       g_object_unref (menu->accel_group);
       menu->accel_group = accel_group;
       if (menu->accel_group)
-       gtk_accel_group_ref (menu->accel_group);
+       g_object_ref (menu->accel_group);
       _gtk_menu_refresh_accel_paths (menu, TRUE);
     }
 }
@@ -931,6 +1478,24 @@ gtk_menu_get_accel_group (GtkMenu *menu)
   return menu->accel_group;
 }
 
+static gboolean
+gtk_menu_real_can_activate_accel (GtkWidget *widget,
+                                  guint      signal_id)
+{
+  /* Menu items chain here to figure whether they can activate their
+   * accelerators.  Unlike ordinary widgets, menus allow accel
+   * activation even if invisible since that's the usual case for
+   * submenus/popup-menus. however, the state of the attach widget
+   * affects the "activeness" of the menu.
+   */
+  GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
+
+  if (awidget)
+    return gtk_widget_can_activate_accel (awidget, signal_id);
+  else
+    return GTK_WIDGET_IS_SENSITIVE (widget);
+}
+
 /**
  * gtk_menu_set_accel_path
  * @menu:       a valid #GtkMenu
@@ -1041,6 +1606,7 @@ gtk_menu_set_tearoff_hints (GtkMenu *menu,
     
   geometry_hints.min_height = 0;
   geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
+
   gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
                                 NULL,
                                 &geometry_hints,
@@ -1072,6 +1638,28 @@ gtk_menu_update_title (GtkMenu *menu)
     }
 }
 
+static GtkWidget*
+gtk_menu_get_toplevel (GtkWidget *menu)
+{
+  GtkWidget *attach, *toplevel;
+
+  attach = gtk_menu_get_attach_widget (GTK_MENU (menu));
+
+  if (GTK_IS_MENU_ITEM (attach))
+    attach = attach->parent;
+
+  if (GTK_IS_MENU (attach))
+    return gtk_menu_get_toplevel (attach->parent);
+  else if (GTK_IS_WIDGET (attach))
+    {
+      toplevel = gtk_widget_get_toplevel (attach->parent);
+      if (GTK_WIDGET_TOPLEVEL (toplevel)) 
+       return toplevel;
+    }
+
+  return NULL;
+}
+
 void       
 gtk_menu_set_tearoff_state (GtkMenu  *menu,
                            gboolean  torn_off)
@@ -1092,43 +1680,44 @@ gtk_menu_set_tearoff_state (GtkMenu  *menu,
 
          if (!menu->tearoff_window)
            {
-             menu->tearoff_window = g_object_connect (gtk_widget_new (GTK_TYPE_WINDOW,
-                                                                      "type", GTK_WINDOW_TOPLEVEL,
-                                                                      NULL),
-                                                      "signal::destroy", gtk_widget_destroyed, &menu->tearoff_window,
-                                                      NULL);
+             GtkWidget *toplevel;
+
+             menu->tearoff_window = gtk_widget_new (GTK_TYPE_WINDOW,
+                                                    "type", GTK_WINDOW_TOPLEVEL,
+                                                    "screen", gtk_widget_get_screen (menu->toplevel),
+                                                    "app_paintable", TRUE,
+                                                    NULL);
+
+             
              gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
                                        GDK_WINDOW_TYPE_HINT_MENU);
              gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
-             gtk_widget_set_app_paintable (menu->tearoff_window, TRUE);
-             gtk_signal_connect (GTK_OBJECT (menu->tearoff_window),  
-                                 "event",
-                                 GTK_SIGNAL_FUNC (gtk_menu_window_event), 
-                                 GTK_OBJECT (menu));
+             g_signal_connect (menu->tearoff_window, "destroy",
+                               G_CALLBACK (gtk_widget_destroyed), &menu->tearoff_window);
+             g_signal_connect (menu->tearoff_window, "event",
+                               G_CALLBACK (gtk_menu_window_event), menu);
 
              gtk_menu_update_title (menu);
 
              gtk_widget_realize (menu->tearoff_window);
-             
-             gdk_window_set_decorations (menu->tearoff_window->window, 
-                                         GDK_DECOR_ALL |
-                                         GDK_DECOR_RESIZEH |
-                                         GDK_DECOR_MINIMIZE |
-                                         GDK_DECOR_MAXIMIZE);
-             gtk_window_set_resizable (GTK_WINDOW (menu->tearoff_window), FALSE);
 
+             toplevel = gtk_menu_get_toplevel (GTK_WIDGET (menu));
+             if (toplevel != NULL)
+               gtk_window_set_transient_for (GTK_WINDOW (menu->tearoff_window),
+                                             GTK_WINDOW (toplevel));
+             
              menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
              gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
 
-             gdk_window_get_size (GTK_WIDGET (menu)->window, &width, &height);
+             gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
              menu->tearoff_adjustment =
                GTK_ADJUSTMENT (gtk_adjustment_new (0,
                                                    0,
                                                    GTK_WIDGET (menu)->requisition.height,
-                                                   MENU_SCROLL_STEP,
+                                                   MENU_SCROLL_STEP2,
                                                    height/2,
                                                    height));
-             g_object_connect (GTK_OBJECT (menu->tearoff_adjustment),
+             g_object_connect (menu->tearoff_adjustment,
                                "signal::value_changed", gtk_menu_scrollbar_changed, menu,
                                NULL);
              menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
@@ -1145,7 +1734,7 @@ gtk_menu_set_tearoff_state (GtkMenu  *menu,
          
          gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
 
-         gdk_window_get_size (GTK_WIDGET (menu)->window, &width, NULL);
+         gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, NULL);
 
          /* Update menu->requisition
           */
@@ -1164,8 +1753,15 @@ gtk_menu_set_tearoff_state (GtkMenu  *menu,
        }
       else
        {
+         gtk_widget_hide (GTK_WIDGET (menu));
          gtk_widget_hide (menu->tearoff_window);
          gtk_menu_reparent (menu, menu->toplevel, FALSE);
+         gtk_widget_destroy (menu->tearoff_window);
+         
+         menu->tearoff_window = NULL;
+         menu->tearoff_hbox = NULL;
+         menu->tearoff_scrollbar = NULL;
+         menu->tearoff_adjustment = NULL;
        }
     }
 }
@@ -1218,14 +1814,15 @@ gtk_menu_set_title (GtkMenu     *menu,
  * Returns the title of the menu. See gtk_menu_set_title().
  *
  * Return value: the title of the menu, or %NULL if the menu has no
- *               title set on it.
+ * title set on it. This string is owned by the widget and should
+ * not be modified or freed.
  **/
 G_CONST_RETURN gchar *
 gtk_menu_get_title (GtkMenu *menu)
 {
   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
 
-  return gtk_object_get_data (GTK_OBJECT (menu), "gtk-menu-title");
+  return g_object_get_data (G_OBJECT (menu), "gtk-menu-title");
 }
 
 void
@@ -1234,18 +1831,37 @@ gtk_menu_reorder_child (GtkMenu   *menu,
                         gint       position)
 {
   GtkMenuShell *menu_shell;
+
   g_return_if_fail (GTK_IS_MENU (menu));
   g_return_if_fail (GTK_IS_MENU_ITEM (child));
+
   menu_shell = GTK_MENU_SHELL (menu);
+
   if (g_list_find (menu_shell->children, child))
     {   
       menu_shell->children = g_list_remove (menu_shell->children, child);
-      menu_shell->children = g_list_insert (menu_shell->children, child, position);   
+      menu_shell->children = g_list_insert (menu_shell->children, child, position);
+      gtk_menu_do_insert (menu_shell, child, position);
+
       if (GTK_WIDGET_VISIBLE (menu_shell))
         gtk_widget_queue_resize (GTK_WIDGET (menu_shell));
     }   
 }
 
+static void
+gtk_menu_style_set (GtkWidget *widget,
+                   GtkStyle  *previous_style)
+{
+  if (GTK_WIDGET_REALIZED (widget))
+    {
+      GtkMenu *menu = GTK_MENU (widget);
+      
+      gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
+      gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
+      gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
+    }
+}
+
 static void
 gtk_menu_realize (GtkWidget *widget)
 {
@@ -1255,7 +1871,8 @@ gtk_menu_realize (GtkWidget *widget)
   GtkMenu *menu;
   GtkWidget *child;
   GList *children;
-
+  guint vertical_padding;
+  
   g_return_if_fail (GTK_IS_MENU (widget));
 
   menu = GTK_MENU (widget);
@@ -1272,6 +1889,7 @@ gtk_menu_realize (GtkWidget *widget)
   attributes.colormap = gtk_widget_get_colormap (widget);
   
   attributes.event_mask = gtk_widget_get_events (widget);
+
   attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
                            GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
   
@@ -1280,9 +1898,13 @@ gtk_menu_realize (GtkWidget *widget)
   gdk_window_set_user_data (widget->window, widget);
   
   border_width = GTK_CONTAINER (widget)->border_width;
+
+  gtk_widget_style_get (GTK_WIDGET (menu),
+                       "vertical-padding", &vertical_padding,
+                       NULL);
   
   attributes.x = border_width + widget->style->xthickness;
-  attributes.y = border_width + widget->style->ythickness;
+  attributes.y = border_width + widget->style->ythickness + vertical_padding;
   attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
   attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
 
@@ -1299,7 +1921,7 @@ gtk_menu_realize (GtkWidget *widget)
 
   attributes.x = 0;
   attributes.y = 0;
-  attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness) * 2);
+  attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness + vertical_padding) * 2);
   
   menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
   gdk_window_set_user_data (menu->bin_window, menu);
@@ -1326,6 +1948,16 @@ gtk_menu_realize (GtkWidget *widget)
   gdk_window_show (menu->view_window);
 }
 
+static gboolean 
+gtk_menu_focus (GtkWidget       *widget,
+                GtkDirectionType direction)
+{
+  /*
+   * A menu or its menu items cannot have focus
+   */
+  return FALSE;
+}
+
 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
  */
 static GdkWindow *
@@ -1348,7 +1980,8 @@ menu_grab_transfer_window_get (GtkMenu *menu)
 
       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
       
-      window = gdk_window_new (NULL, &attributes, attributes_mask);
+      window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
+                              &attributes, attributes_mask);
       gdk_window_set_user_data (window, menu);
 
       gdk_window_show (window);
@@ -1397,19 +2030,23 @@ static void
 gtk_menu_size_request (GtkWidget      *widget,
                       GtkRequisition *requisition)
 {
+  gint i;
   GtkMenu *menu;
   GtkMenuShell *menu_shell;
   GtkWidget *child;
   GList *children;
   guint max_toggle_size;
   guint max_accel_width;
+  guint vertical_padding;
   GtkRequisition child_requisition;
+  GtkMenuPrivate *priv;
   
   g_return_if_fail (GTK_IS_MENU (widget));
   g_return_if_fail (requisition != NULL);
   
   menu = GTK_MENU (widget);
   menu_shell = GTK_MENU_SHELL (widget);
+  priv = gtk_menu_get_private (menu);
   
   requisition->width = 0;
   requisition->height = 0;
@@ -1417,47 +2054,65 @@ gtk_menu_size_request (GtkWidget      *widget,
   max_toggle_size = 0;
   max_accel_width = 0;
   
+  g_free (priv->heights);
+  priv->heights = g_new0 (guint, priv->rows);
+  priv->heights_length = priv->rows;
+
   children = menu_shell->children;
   while (children)
     {
+      gint part;
+      gint toggle_size;
+      guint l, r, t, b;
+
       child = children->data;
       children = children->next;
       
-      if (GTK_WIDGET_VISIBLE (child))
-       {
-         gint toggle_size;
-
-          /* It's important to size_request the child
-           * before doing the toggle size request, in
-           * case the toggle size request depends on the size
-           * request of a child of the child (e.g. for ImageMenuItem)
-           */
-          
-         GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
-         gtk_widget_size_request (child, &child_requisition);
-         
-         requisition->width = MAX (requisition->width, child_requisition.width);
-         requisition->height += child_requisition.height;
+      if (! GTK_WIDGET_VISIBLE (child))
+        continue;
 
-         gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
-         max_toggle_size = MAX (max_toggle_size, toggle_size);
-         max_accel_width = MAX (max_accel_width, GTK_MENU_ITEM (child)->accelerator_width);
-       }
+      get_child_attach (child, &l, &r, &t, &b);
+
+      /* It's important to size_request the child
+       * before doing the toggle size request, in
+       * case the toggle size request depends on the size
+       * request of a child of the child (e.g. for ImageMenuItem)
+       */
+
+       GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
+       gtk_widget_size_request (child, &child_requisition);
+
+       gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
+       max_toggle_size = MAX (max_toggle_size, toggle_size);
+       max_accel_width = MAX (max_accel_width,
+                              GTK_MENU_ITEM (child)->accelerator_width);
+
+       part = child_requisition.width / (r - l);
+       requisition->width = MAX (requisition->width, part);
+
+       part = MAX (child_requisition.height, toggle_size) / (b - t);
+       priv->heights[t] = MAX (priv->heights[t], part);
     }
-  
+
+  for (i = 0; i < priv->rows; i++)
+    requisition->height += priv->heights[i];
+
   requisition->width += max_toggle_size + max_accel_width;
+  requisition->width *= priv->columns;
   requisition->width += (GTK_CONTAINER (menu)->border_width +
                         widget->style->xthickness) * 2;
-  requisition->height += (GTK_CONTAINER (menu)->border_width +
+
+  gtk_widget_style_get (GTK_WIDGET (menu),
+                       "vertical-padding", &vertical_padding,
+                       NULL);
+  requisition->height += (GTK_CONTAINER (menu)->border_width + vertical_padding +
                          widget->style->ythickness) * 2;
   
   menu->toggle_size = max_toggle_size;
 
-  /* If the requested width was different than the allocated width, we need to change
-   * the geometry hints for the tear off window so that the window can actually be resized.
-   * Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
+  /* Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
    */
-  if ((requisition->width != GTK_WIDGET (menu)->allocation.width) && menu->tearoff_active)
+  if (menu->tearoff_active)
     gtk_menu_set_tearoff_hints (menu, requisition->width);
 }
 
@@ -1469,23 +2124,38 @@ gtk_menu_size_allocate (GtkWidget     *widget,
   GtkMenuShell *menu_shell;
   GtkWidget *child;
   GtkAllocation child_allocation;
+  GtkRequisition child_requisition;
+  GtkMenuPrivate *priv;
   GList *children;
   gint x, y;
   gint width, height;
+  guint vertical_padding;
 
   g_return_if_fail (GTK_IS_MENU (widget));
   g_return_if_fail (allocation != NULL);
   
   menu = GTK_MENU (widget);
   menu_shell = GTK_MENU_SHELL (widget);
+  priv = gtk_menu_get_private (menu);
 
   widget->allocation = *allocation;
+  gtk_widget_get_child_requisition (GTK_WIDGET (menu), &child_requisition);
 
-  x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness;
-  y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness;
+  gtk_widget_style_get (GTK_WIDGET (menu),
+                       "vertical-padding", &vertical_padding,
+                       NULL);
   
+  x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness;
+  y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness + vertical_padding;
+
   width = MAX (1, allocation->width - x * 2);
   height = MAX (1, allocation->height - y * 2);
+
+  child_requisition.width -= x * 2;
+  child_requisition.height -= y * 2;
+
+  if (menu_shell->active)
+    gtk_menu_scroll_to (menu, menu->scroll_offset);
   
   if (menu->upper_arrow_visible && !menu->tearoff_active)
     {
@@ -1511,40 +2181,63 @@ gtk_menu_size_allocate (GtkWidget     *widget,
 
   if (menu_shell->children)
     {
-      child_allocation.x = 0;
-      child_allocation.y = 0;
-      child_allocation.width = width;
-      
+      gint base_width = width / priv->columns;
+
       children = menu_shell->children;
       while (children)
        {
          child = children->data;
          children = children->next;
-         
+
          if (GTK_WIDGET_VISIBLE (child))
            {
-             GtkRequisition child_requisition;
-             gtk_widget_get_child_requisition (child, &child_requisition);
-             
-             child_allocation.height = child_requisition.height;
+              gint i;
+             guint l, r, t, b;
+
+              get_child_attach (child, &l, &r, &t, &b);
+
+              if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
+                {
+                  guint tmp;
+                  tmp = priv->columns - l;
+                  l = priv->columns - r;
+                  r = tmp;
+                }
+
+              child_allocation.width = (r - l) * base_width;
+              child_allocation.height = 0;
+              child_allocation.x = l * base_width;
+              child_allocation.y = 0;
+
+              for (i = 0; i < b; i++)
+                {
+                  if (i < t)
+                    child_allocation.y += priv->heights[i];
+                  else
+                    child_allocation.height += priv->heights[i];
+                }
 
              gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
                                                  menu->toggle_size);
+
              gtk_widget_size_allocate (child, &child_allocation);
              gtk_widget_queue_draw (child);
-             
-             child_allocation.y += child_allocation.height;
            }
        }
       
       /* Resize the item window */
       if (GTK_WIDGET_REALIZED (widget))
        {
-         gdk_window_resize (menu->bin_window,
-                            child_allocation.width,
-                            child_allocation.y);
-       }
+          gint i;
+          gint width, height;
 
+          height = 0;
+          for (i = 0; i < priv->rows; i++)
+            height += priv->heights[i];
+
+          width = priv->columns * base_width;
+         gdk_window_resize (menu->bin_window, width, height);
+       }
 
       if (menu->tearoff_active)
        {
@@ -1554,7 +2247,6 @@ gtk_menu_size_allocate (GtkWidget     *widget,
                {
                  gtk_widget_hide (menu->tearoff_scrollbar);
                  gtk_menu_set_tearoff_hints (menu, allocation->width);
-                 gtk_widget_set_usize (menu->tearoff_window, -1, allocation->height);
 
                  gtk_menu_scroll_to (menu, 0);
                }
@@ -1580,7 +2272,6 @@ gtk_menu_size_allocate (GtkWidget     *widget,
                {
                  gtk_widget_show (menu->tearoff_scrollbar);
                  gtk_menu_set_tearoff_hints (menu, allocation->width);
-                 gtk_widget_set_usize (menu->tearoff_window, -1, allocation->height);
                }
            }
        }
@@ -1594,17 +2285,25 @@ gtk_menu_paint (GtkWidget      *widget,
   GtkMenu *menu;
   gint width, height;
   gint border_x, border_y;
+  guint vertical_padding;
   
   g_return_if_fail (GTK_IS_MENU (widget));
 
   menu = GTK_MENU (widget);
+
+  gtk_widget_style_get (GTK_WIDGET (menu),
+                       "vertical-padding", &vertical_padding,
+                       NULL);
   
   border_x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness;
-  border_y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness;
-  gdk_window_get_size (widget->window, &width, &height);
+  border_y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness + vertical_padding;
+  gdk_drawable_get_size (widget->window, &width, &height);
 
   if (event->window == widget->window)
     {
+      gint arrow_space = MENU_SCROLL_ARROW_HEIGHT - 2 * widget->style->ythickness;
+      gint arrow_size = 0.7 * arrow_space;
+       
       gtk_paint_box (widget->style,
                     widget->window,
                     GTK_STATE_NORMAL,
@@ -1621,7 +2320,7 @@ gtk_menu_paint (GtkWidget      *widget,
                         NULL, widget, "menu",
                         border_x,
                         border_y,
-                        width - 2*border_x,
+                        width - 2 * border_x,
                         MENU_SCROLL_ARROW_HEIGHT);
          
          gtk_paint_arrow (widget->style,
@@ -1629,13 +2328,12 @@ gtk_menu_paint (GtkWidget      *widget,
                           menu->upper_arrow_prelight ?
                           GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
                           GTK_SHADOW_OUT,
-                          NULL, widget, "menu",
+                          NULL, widget, "menu_scroll_arrow_up",
                           GTK_ARROW_UP,
                           TRUE,
-                          width / 2 - MENU_SCROLL_ARROW_HEIGHT / 2 + 1,
-                          2 * border_y + 1,
-                          MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2,
-                          MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2);
+                          (width - arrow_size ) / 2,
+                          border_y + widget->style->ythickness + (arrow_space - arrow_size)/2,
+                          arrow_size, arrow_size);
        }
   
       if (menu->lower_arrow_visible && !menu->tearoff_active)
@@ -1647,7 +2345,7 @@ gtk_menu_paint (GtkWidget      *widget,
                         GTK_SHADOW_OUT,
                         NULL, widget, "menu",
                         border_x,
-                        height - border_y - MENU_SCROLL_ARROW_HEIGHT + 1,
+                        height - border_y - MENU_SCROLL_ARROW_HEIGHT,
                         width - 2*border_x,
                         MENU_SCROLL_ARROW_HEIGHT);
          
@@ -1656,44 +2354,15 @@ gtk_menu_paint (GtkWidget      *widget,
                           menu->lower_arrow_prelight ?
                           GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
                           GTK_SHADOW_OUT,
-                          NULL, widget, "menu",
+                          NULL, widget, "menu_scroll_arrow_down",
                           GTK_ARROW_DOWN,
                           TRUE,
-                          width / 2 - MENU_SCROLL_ARROW_HEIGHT / 2 + 1,
-                          height - MENU_SCROLL_ARROW_HEIGHT + 1,
-                          MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2,
-                          MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2);
+                          (width - arrow_size) / 2,
+                          height - border_y - MENU_SCROLL_ARROW_HEIGHT +
+                             widget->style->ythickness + (arrow_space - arrow_size)/2,
+                          arrow_size, arrow_size);
        }
     }
-  else if (event->window == menu->view_window)
-    {
-      gint menu_height;
-      gint top_pos;
-      
-      if (menu->scroll_offset < 0)
-       gtk_paint_box (widget->style,
-                      menu->view_window,
-                      GTK_STATE_ACTIVE,
-                      GTK_SHADOW_IN,
-                      NULL, widget, "menu",
-                      0, 0,
-                      -1,
-                      -menu->scroll_offset);
-
-      menu_height = widget->requisition.height - 2*border_y;
-      top_pos = height - 2*border_y - (menu->upper_arrow_visible ? MENU_SCROLL_ARROW_HEIGHT : 0);
-
-      if (menu_height - menu->scroll_offset < top_pos)
-       gtk_paint_box (widget->style,
-                      menu->view_window,
-                      GTK_STATE_ACTIVE,
-                      GTK_SHADOW_IN,
-                      NULL, widget, "menu",
-                      0,
-                      menu_height - menu->scroll_offset,
-                      -1,
-                      top_pos - (menu_height - menu->scroll_offset));
-    }
 }
 
 static gboolean
@@ -1723,6 +2392,75 @@ gtk_menu_show (GtkWidget *widget)
   GTK_WIDGET_CLASS (parent_class)->show (widget);
 }
 
+static gboolean
+gtk_menu_button_press (GtkWidget      *widget,
+                        GdkEventButton *event)
+{
+  /* Don't pop down the menu for releases over scroll arrows
+   */
+  if (GTK_IS_MENU (widget))
+    {
+      GtkMenu *menu = GTK_MENU (widget);
+
+      if (menu->upper_arrow_prelight ||  menu->lower_arrow_prelight)
+       return TRUE;
+    }
+
+  return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
+}
+
+static gboolean
+gtk_menu_button_release (GtkWidget      *widget,
+                        GdkEventButton *event)
+{
+  /* Don't pop down the menu for releases over scroll arrows
+   */
+  if (GTK_IS_MENU (widget))
+    {
+      GtkMenu *menu = GTK_MENU (widget);
+
+      if (menu->upper_arrow_prelight ||  menu->lower_arrow_prelight)
+       return TRUE;
+    }
+
+  return GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
+}
+
+static const gchar *
+get_accel_path (GtkWidget *menu_item,
+               gboolean  *locked)
+{
+  const gchar *path;
+  GtkWidget *label;
+  GClosure *accel_closure;
+  GtkAccelGroup *accel_group;    
+
+  path = _gtk_widget_get_accel_path (menu_item, locked);
+  if (!path)
+    {
+      path = GTK_MENU_ITEM (menu_item)->accel_path;
+      
+      if (locked)
+       {
+         *locked = TRUE;
+
+         label = GTK_BIN (menu_item)->child;
+         
+         if (GTK_IS_ACCEL_LABEL (label))
+           {
+             g_object_get (label, 
+                           "accel_closure", &accel_closure, 
+                           NULL);
+             accel_group = gtk_accel_group_from_accel_closure (accel_closure);
+             
+             *locked = accel_group->lock_count > 0;
+           }
+       }
+    }
+
+  return path;
+}
+
 static gboolean
 gtk_menu_key_press (GtkWidget  *widget,
                    GdkEventKey *event)
@@ -1734,6 +2472,7 @@ gtk_menu_key_press (GtkWidget     *widget,
   gchar *accel = NULL;
   guint accel_key, accel_mods;
   GdkModifierType consumed_modifiers;
+  GdkDisplay *display;
   
   g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
   g_return_val_if_fail (event != NULL, FALSE);
@@ -1745,10 +2484,12 @@ gtk_menu_key_press (GtkWidget   *widget,
 
   if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
     return TRUE;
+
+  display = gtk_widget_get_display (widget);
     
-  g_object_get (G_OBJECT (gtk_settings_get_default ()),
-                "gtk-menu-bar-accel",
-                &accel,
+  g_object_get (gtk_widget_get_settings (widget),
+                "gtk-menu-bar-accel", &accel,
+               "gtk-can-change-accels", &can_change_accels,
                 NULL);
 
   if (accel)
@@ -1768,9 +2509,7 @@ gtk_menu_key_press (GtkWidget     *widget,
        */
       if (event->keyval == keyval &&
           (mods & event->state) == mods)
-        {
-          gtk_signal_emit_by_name (GTK_OBJECT (menu), "cancel");
-        }
+       gtk_menu_shell_cancel (menu_shell);
 
       g_free (accel);
 
@@ -1789,12 +2528,8 @@ gtk_menu_key_press (GtkWidget    *widget,
       break;
     }
 
-  g_object_get (G_OBJECT (gtk_settings_get_default ()),
-               "gtk-can-change-accels", &can_change_accels,
-               NULL);
-
   /* Figure out what modifiers went into determining the key symbol */
-  gdk_keymap_translate_keyboard_state (gdk_keymap_get_default (),
+  gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
                                       event->hardware_keycode, event->state, event->group,
                                       NULL, NULL, NULL, &consumed_modifiers);
 
@@ -1810,22 +2545,22 @@ gtk_menu_key_press (GtkWidget   *widget,
   /* Modify the accelerators */
   if (can_change_accels &&
       menu_shell->active_menu_item &&
-      GTK_BIN (menu_shell->active_menu_item)->child &&                 /* no seperators */
+      GTK_BIN (menu_shell->active_menu_item)->child &&                 /* no separators */
       GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL && /* no submenus */
-      (delete || gtk_accelerator_valid (event->keyval, event->state)))
+      (delete || gtk_accelerator_valid (accel_key, accel_mods)))
     {
       GtkWidget *menu_item = menu_shell->active_menu_item;
-      gboolean replace_accels = TRUE;
+      gboolean locked, replace_accels = TRUE;
       const gchar *path;
 
-      path = _gtk_widget_get_accel_path (menu_item);
-      if (!path)
+      path = get_accel_path (menu_item, &locked);
+      if (!path || locked)
        {
          /* can't change accelerators on menu_items without paths
           * (basically, those items are accelerator-locked).
           */
-         /* g_print("item has no path, menu prefix: %s\n", menu->accel_path); */
-         gdk_beep ();
+         /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
+         gdk_display_beep (display);
        }
       else
        {
@@ -1854,7 +2589,7 @@ gtk_menu_key_press (GtkWidget     *widget,
               * locked already
               */
              /* g_print("failed to change\n"); */
-             gdk_beep ();
+             gdk_display_beep (display);
            }
        }
     }
@@ -1874,7 +2609,7 @@ gtk_menu_motion_notify  (GtkWidget           *widget,
 
   if (GTK_IS_MENU (widget))
     gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
-  
+
   /* We received the event for one of two reasons:
    *
    * a) We are the active menu, and did gtk_grab_add()
@@ -1886,7 +2621,6 @@ gtk_menu_motion_notify  (GtkWidget           *widget,
    */
   menu_item = gtk_get_event_widget ((GdkEvent*) event);
   if (!menu_item || !GTK_IS_MENU_ITEM (menu_item) ||
-      !_gtk_menu_item_is_selectable (menu_item) ||
       !GTK_IS_MENU (menu_item->parent))
     return FALSE;
 
@@ -1900,6 +2634,15 @@ gtk_menu_motion_notify  (GtkWidget          *widget,
   if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
     return TRUE; 
 
+  /* Make sure we pop down if we enter a non-selectable menu item, so we
+   * don't show a submenu when the cursor is outside the stay-up triangle.
+   */
+  if (!_gtk_menu_item_is_selectable (menu_item))
+    {
+      gtk_menu_shell_deselect (menu_shell);
+      return FALSE;
+    }
+
   if (need_enter)
     {
       /* The menu is now sensitive to enter events on its items, but
@@ -1909,21 +2652,20 @@ gtk_menu_motion_notify  (GtkWidget         *widget,
       
       menu_shell->ignore_enter = FALSE; 
       
-      gdk_window_get_size (event->window, &width, &height);
+      gdk_drawable_get_size (event->window, &width, &height);
       if (event->x >= 0 && event->x < width &&
          event->y >= 0 && event->y < height)
        {
-         GdkEvent send_event;
-
-         memset (&send_event, 0, sizeof (send_event));
-         send_event.crossing.type = GDK_ENTER_NOTIFY;
-         send_event.crossing.window = event->window;
-         send_event.crossing.time = event->time;
-         send_event.crossing.send_event = TRUE;
-         send_event.crossing.x_root = event->x_root;
-         send_event.crossing.y_root = event->y_root;
-         send_event.crossing.x = event->x;
-         send_event.crossing.y = event->y;
+         GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
+         gboolean result;
+
+         send_event->crossing.window = g_object_ref (event->window);
+         send_event->crossing.time = event->time;
+         send_event->crossing.send_event = TRUE;
+         send_event->crossing.x_root = event->x_root;
+         send_event->crossing.y_root = event->y_root;
+         send_event->crossing.x = event->x;
+         send_event->crossing.y = event->y;
 
          /* We send the event to 'widget', the currently active menu,
           * instead of 'menu', the menu that the pointer is in. This
@@ -1931,7 +2673,10 @@ gtk_menu_motion_notify  (GtkWidget          *widget,
           * menuitem is a child of the active menu or some parent
           * menu of the active menu.
           */
-         return gtk_widget_event (widget, &send_event);
+         result = gtk_widget_event (widget, send_event);
+         gdk_event_free (send_event);
+
+         return result;
        }
     }
 
@@ -1965,7 +2710,7 @@ gtk_menu_scroll_timeout (gpointer  data)
   if ((menu->scroll_offset >= 0) && (offset < 0))
     offset = 0;
 
-  gdk_window_get_size (widget->window, &view_width, &view_height);
+  gdk_drawable_get_size (widget->window, &view_width, &view_height);
 
   /* Don't scroll past the bottom if we weren't before: */
   if (menu->scroll_offset > 0)
@@ -1992,13 +2737,19 @@ gtk_menu_handle_scrolling (GtkMenu *menu, gboolean enter)
   GdkRectangle rect;
   gboolean in_arrow;
   gboolean scroll_fast = FALSE;
+  guint vertical_padding;
 
   menu_shell = GTK_MENU_SHELL (menu);
 
   gdk_window_get_pointer (GTK_WIDGET (menu)->window, &x, &y, NULL);
-  gdk_window_get_size (GTK_WIDGET (menu)->window, &width, &height);
+  gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
 
-  border = GTK_CONTAINER (menu)->border_width + GTK_WIDGET (menu)->style->ythickness;
+  gtk_widget_style_get (GTK_WIDGET (menu),
+                       "vertical-padding", &vertical_padding,
+                       NULL);
+  
+  border = GTK_CONTAINER (menu)->border_width +
+    GTK_WIDGET (menu)->style->ythickness + vertical_padding;
 
   if (menu->upper_arrow_visible && !menu->tearoff_active)
     {
@@ -2025,15 +2776,14 @@ gtk_menu_handle_scrolling (GtkMenu *menu, gboolean enter)
          /* Deselect the active item so that any submenus are poped down */
          gtk_menu_shell_deselect (menu_shell);
 
-         gtk_menu_stop_scrolling (menu);
-         menu->scroll_step = -MENU_SCROLL_STEP;
+         gtk_menu_remove_scroll_timeout (menu);
+         menu->scroll_step = (scroll_fast) ? -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);
        }
       else if (!enter && !in_arrow && menu->upper_arrow_prelight)
        {
-         menu->upper_arrow_prelight = FALSE;
          gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
          
          gtk_menu_stop_scrolling (menu);
@@ -2065,15 +2815,14 @@ gtk_menu_handle_scrolling (GtkMenu *menu, gboolean enter)
          /* Deselect the active item so that any submenus are poped down */
          gtk_menu_shell_deselect (menu_shell);
 
-         gtk_menu_stop_scrolling (menu);
-         menu->scroll_step = MENU_SCROLL_STEP;
+         gtk_menu_remove_scroll_timeout (menu);
+         menu->scroll_step = (scroll_fast) ? 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);
        }
       else if (!enter && !in_arrow && menu->lower_arrow_prelight)
        {
-         menu->lower_arrow_prelight = FALSE;
          gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
          
          gtk_menu_stop_scrolling (menu);
@@ -2088,8 +2837,13 @@ gtk_menu_enter_notify (GtkWidget        *widget,
   GtkWidget *menu_item;
 
   if (widget && GTK_IS_MENU (widget))
-    gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
-      
+    {
+      GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
+
+      if (!menu_shell->ignore_enter)
+       gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
+    }
+  
   /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
    * will not correspond to the event widget's parent.  Check to see
    * if we are in the parent's navigation region.
@@ -2138,6 +2892,15 @@ gtk_menu_leave_notify (GtkWidget        *widget,
          gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
          return TRUE;
        }
+      else if (menu_item == GTK_MENU_ITEM (menu_shell->active_menu_item))
+       {
+         /* We are leaving an active menu item with nonactive submenu.
+          * Deselect it so we don't surprise the user with by popping
+          * up a submenu _after_ he left the item.
+          */
+         gtk_menu_shell_deselect (menu_shell);
+         return TRUE;
+       }
     }
   
   return GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event); 
@@ -2154,7 +2917,7 @@ gtk_menu_stop_navigating_submenu (GtkMenu *menu)
   
   if (menu->navigation_timeout)
     {
-      gtk_timeout_remove (menu->navigation_timeout);
+      g_source_remove (menu->navigation_timeout);
       menu->navigation_timeout = 0;
     }
 }
@@ -2178,15 +2941,15 @@ gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
 
       if (child_window)
        {
-         GdkEventCrossing send_event;
+         GdkEvent *send_event = gdk_event_new (GDK_ENTER_NOTIFY);
+
+         send_event->crossing.window = g_object_ref (child_window);
+         send_event->crossing.time = GDK_CURRENT_TIME; /* Bogus */
+         send_event->crossing.send_event = TRUE;
 
-         memset (&send_event, 0, sizeof (send_event));
-         send_event.window = child_window;
-         send_event.type = GDK_ENTER_NOTIFY;
-         send_event.time = GDK_CURRENT_TIME; /* Bogus */
-         send_event.send_event = TRUE;
+         GTK_WIDGET_CLASS (parent_class)->enter_notify_event (GTK_WIDGET (menu), (GdkEventCrossing *)send_event);
 
-         GTK_WIDGET_CLASS (parent_class)->enter_notify_event (GTK_WIDGET (menu), &send_event); 
+         gdk_event_free (send_event);
        }
     }
 
@@ -2213,6 +2976,71 @@ gtk_menu_navigating_submenu (GtkMenu *menu,
   return FALSE;
 }
 
+#undef DRAW_STAY_UP_TRIANGLE
+
+#ifdef DRAW_STAY_UP_TRIANGLE
+
+static void
+draw_stay_up_triangle (GdkWindow *window,
+                      GdkRegion *region)
+{
+  /* Draw ugly color all over the stay-up triangle */
+  GdkColor ugly_color = { 0, 50000, 10000, 10000 };
+  GdkGCValues gc_values;
+  GdkGC *ugly_gc;
+  GdkRectangle clipbox;
+
+  gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
+  ugly_gc = gdk_gc_new_with_values (window, &gc_values, 0 | GDK_GC_SUBWINDOW);
+  gdk_gc_set_rgb_fg_color (ugly_gc, &ugly_color);
+  gdk_gc_set_clip_region (ugly_gc, region);
+
+  gdk_region_get_clipbox (region, &clipbox);
+  
+  gdk_draw_rectangle (window,
+                     ugly_gc,
+                     TRUE,
+                     clipbox.x, clipbox.y,
+                     clipbox.width, clipbox.height);
+  
+  g_object_unref (ugly_gc);
+}
+#endif
+
+static GdkRegion *
+flip_region (GdkRegion *region,
+            gboolean   flip_x,
+            gboolean   flip_y)
+{
+  gint n_rectangles;
+  GdkRectangle *rectangles;
+  GdkRectangle clipbox;
+  GdkRegion *new_region;
+  gint i;
+
+  new_region = gdk_region_new ();
+  
+  gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
+  gdk_region_get_clipbox (region, &clipbox);
+
+  for (i = 0; i < n_rectangles; ++i)
+    {
+      GdkRectangle rect = rectangles[i];
+
+      if (flip_y)
+       rect.y -= 2 * (rect.y - clipbox.y) + rect.height;
+
+      if (flip_x)
+       rect.x -= 2 * (rect.x - clipbox.x) + rect.width;
+
+      gdk_region_union_with_rect (new_region, &rect);
+    }
+
+  g_free (rectangles);
+
+  return new_region;
+}
+
 static void
 gtk_menu_set_submenu_navigation_region (GtkMenu          *menu,
                                        GtkMenuItem      *menu_item,
@@ -2233,56 +3061,80 @@ gtk_menu_set_submenu_navigation_region (GtkMenu          *menu,
   event_widget = gtk_get_event_widget ((GdkEvent*) event);
   
   gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
-  gdk_window_get_size (menu_item->submenu->window, &width, &height);
+  gdk_drawable_get_size (menu_item->submenu->window, &width, &height);
+  
   submenu_right = submenu_left + width;
   submenu_bottom = submenu_top + height;
   
-  gdk_window_get_size (event_widget->window, &width, &height);
+  gdk_drawable_get_size (event_widget->window, &width, &height);
   
   if (event->x >= 0 && event->x < width)
     {
-      /* Set navigation region */
-      /* We fudge/give a little padding in case the user
-       * ``misses the vertex'' of the triangle/is off by a pixel or two.
-       */ 
-      if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
-       point[0].x = event->x_root - SUBMENU_NAV_REGION_PADDING; 
-      else                             
-       point[0].x = event->x_root + SUBMENU_NAV_REGION_PADDING;  
+      gint popdown_delay;
+      gboolean flip_y = FALSE;
+      gboolean flip_x = FALSE;
       
-      /* Exiting the top or bottom? */ 
+      gtk_menu_stop_navigating_submenu (menu);
+
+      if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
+       {
+         /* right */
+         point[0].x = event->x_root;
+         point[1].x = submenu_left;
+       }
+      else
+       {
+         /* left */
+         point[0].x = event->x_root + 1;
+         point[1].x = 2 * (event->x_root + 1) - submenu_right;
+
+         flip_x = TRUE;
+       }
+
       if (event->y < 0)
-        { /* top */
+       {
+         /* top */
          point[0].y = event->y_root + 1;
-         point[1].y = submenu_top;
+         point[1].y = 2 * (event->y_root + 1) - submenu_top + NAVIGATION_REGION_OVERSHOOT;
 
-         if (point[0].y <= point[1].y)
+         if (point[0].y >= point[1].y - NAVIGATION_REGION_OVERSHOOT)
            return;
+
+         flip_y = TRUE;
        }
       else
-        { /* bottom */
+       {
+         /* bottom */
          point[0].y = event->y_root;
-         point[1].y = submenu_bottom;
+         point[1].y = submenu_bottom + NAVIGATION_REGION_OVERSHOOT;
 
-         if (point[0].y >= point[1].y)
+         if (point[0].y >= submenu_bottom)
            return;
        }
-      
-      /* Submenu is to the left or right? */ 
-      if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
-       point[1].x = submenu_left;  /* right */
-      else
-       point[1].x = submenu_right; /* left */
-      
+
       point[2].x = point[1].x;
       point[2].y = point[0].y;
 
-      gtk_menu_stop_navigating_submenu (menu);
-      
       menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
 
-      menu->navigation_timeout = gtk_timeout_add (SUBMENU_NAV_HYSTERESIS_TIMEOUT, 
-                                                 gtk_menu_stop_navigating_submenu_cb, menu);
+      if (flip_x || flip_y)
+       {
+         GdkRegion *new_region = flip_region (menu->navigation_region, flip_x, flip_y);
+         gdk_region_destroy (menu->navigation_region);
+         menu->navigation_region = new_region;
+       }
+
+      g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu)),
+                   "gtk-menu-popdown-delay", &popdown_delay,
+                   NULL);
+
+      menu->navigation_timeout = g_timeout_add (popdown_delay,
+                                               gtk_menu_stop_navigating_submenu_cb, menu);
+
+#ifdef DRAW_STAY_UP_TRIANGLE
+      draw_stay_up_triangle (gdk_get_default_root_window(),
+                            menu->navigation_region);
+#endif
     }
 }
 
@@ -2307,21 +3159,22 @@ gtk_menu_position (GtkMenu *menu)
 {
   GtkWidget *widget;
   GtkRequisition requisition;
+  GtkMenuPrivate *private;
   gint x, y;
-  gint screen_width;
-  gint screen_height;
   gint scroll_offset;
   gint menu_height;
   gboolean push_in;
-  
+  GdkScreen *screen;
+  GdkScreen *pointer_screen;
+  GdkRectangle monitor;
+
   g_return_if_fail (GTK_IS_MENU (menu));
 
   widget = GTK_WIDGET (menu);
 
-  screen_width = gdk_screen_width ();
-  screen_height = gdk_screen_height ();
-
-  gdk_window_get_pointer (NULL, &x, &y, NULL);
+  screen = gtk_widget_get_screen (widget);
+  gdk_display_get_pointer (gdk_screen_get_display (screen),
+                          &pointer_screen, &x, &y, NULL);
 
   /* We need the requisition to figure out the right place to
    * popup the menu. In fact, we always need to ask here, since
@@ -2330,14 +3183,143 @@ gtk_menu_position (GtkMenu *menu)
    */
   gtk_widget_size_request (widget, &requisition);
 
+  if (pointer_screen != screen)
+    {
+      /* Pointer is on a different screen; roughly center the
+       * menu on the screen. If someone was using multiscreen
+       * + Xinerama together they'd probably want something
+       * fancier; but that is likely to be vanishingly rare.
+       */
+      x = MAX (0, (gdk_screen_get_width (screen) - requisition.width) / 2);
+      y = MAX (0, (gdk_screen_get_height (screen) - requisition.height) / 2);
+    }
+
+  private = gtk_menu_get_private (menu);
+  private->monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
+
   push_in = FALSE;
   
   if (menu->position_func)
-    (* menu->position_func) (menu, &x, &y, &push_in, menu->position_func_data);
+    {
+      (* menu->position_func) (menu, &x, &y, &push_in, menu->position_func_data);
+      gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
+    }
   else
     {
-      x = CLAMP (x - 2, 0, MAX (0, screen_width - requisition.width));
-      y = CLAMP (y - 2, 0, MAX (0, screen_height - requisition.height));
+      gint space_left, space_right, space_above, space_below;
+      gint needed_width;
+      gint needed_height;
+      gint xthickness = widget->style->xthickness;
+      gint ythickness = widget->style->ythickness;
+      gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
+
+      /* The placement of popup menus horizontally works like this (with
+       * RTL in parentheses)
+       *
+       * - If there is enough room to the right (left) of the mouse cursor,
+       *   position the menu there.
+       * 
+       * - Otherwise, if if there is enough room to the left (right) of the 
+       *   mouse cursor, position the menu there.
+       * 
+       * - Otherwise if the menu is smaller than the monitor, position it
+       *   on the side of the mouse cursor that has the most space available
+       *
+       * - Otherwise (if there is simply not enough room for the menu on the
+       *   monitor), position it as far left (right) as possible.
+       *
+       * Positioning in the vertical direction is similar: first try below
+       * mouse cursor, then above.
+       */
+      gdk_screen_get_monitor_geometry (screen, private->monitor_num, &monitor);
+
+      space_left = x - monitor.x;
+      space_right = monitor.x + monitor.width - x - 1;
+      space_above = y - monitor.y;
+      space_below = monitor.y + monitor.height - y - 1;
+
+      /* position horizontally */
+
+      /* the amount of space we need to position the menu. Note the
+       * menu is offset "xthickness" pixels 
+       */
+      needed_width = requisition.width - xthickness;
+
+      if (needed_width <= space_left ||
+         needed_width <= space_right)
+       {
+         if ((rtl  && needed_width <= space_left) ||
+             (!rtl && needed_width >  space_right))
+           {
+             /* position left */
+             x = x + xthickness - requisition.width + 1;
+           }
+         else
+           {
+             /* position right */
+             x = x - xthickness;
+           }
+
+         /* x is clamped on-screen further down */
+       }
+      else if (requisition.width <= monitor.width)
+       {
+         /* the menu is too big to fit on either side of the mouse
+          * cursor, but smaller than the monitor. Position it on
+          * the side that has the most space
+          */
+         if (space_left > space_right)
+           {
+             /* left justify */
+             x = monitor.x;
+           }
+         else
+           {
+             /* right justify */
+             x = monitor.x + monitor.width - requisition.width;
+           }
+       }
+      else /* menu is simply too big for the monitor */
+       {
+         if (rtl)
+           {
+             /* right justify */
+             x = monitor.x + monitor.width - requisition.width;
+           }
+         else
+           {
+             /* left justify */
+             x = monitor.x;
+           }
+       }
+
+      /* Position vertically. The algorithm is the same as above, but
+       * simpler because we don't have to take RTL into account.
+       */
+      needed_height = requisition.height - ythickness;
+
+      if (needed_height <= space_above ||
+         needed_height <= space_below)
+       {
+         if (needed_height <= space_below)
+           y = y - ythickness;
+         else
+           y = y + ythickness - requisition.height + 1;
+         
+         y = CLAMP (y, monitor.y,
+                    monitor.y + monitor.height - requisition.height);
+       }
+      else if (needed_height > space_below && needed_height > space_above)
+       {
+         if (space_below >= space_above)
+           y = monitor.y + monitor.height - requisition.height;
+         else
+           y = monitor.y;
+       }
+      else
+       {
+         y = monitor.y;
+       }
     }
 
   scroll_offset = 0;
@@ -2346,27 +3328,37 @@ gtk_menu_position (GtkMenu *menu)
     {
       menu_height = GTK_WIDGET (menu)->requisition.height;
 
-      if (y + menu_height > screen_height)
+      if (y + menu_height > monitor.y + monitor.height)
        {
-         scroll_offset -= y + menu_height - screen_height;
-         y = screen_height - menu_height;
+         scroll_offset -= y + menu_height - (monitor.y + monitor.height);
+         y = (monitor.y + monitor.height) - menu_height;
        }
   
-      if (y < 0)
+      if (y < monitor.y)
        {
-         scroll_offset -= y;
-         y = 0;
+         scroll_offset += monitor.y - y;
+         y = monitor.y;
        }
     }
 
-  if (y + requisition.height > screen_height)
-    requisition.height = screen_height - y;
+  /* FIXME: should this be done in the various position_funcs ? */
+  x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
+  if (GTK_MENU_SHELL (menu)->active)
+    {
+      private->have_position = TRUE;
+      private->x = x;
+      private->y = y;
+    }
+  
+  if (y + requisition.height > monitor.y + monitor.height)
+    requisition.height = (monitor.y + monitor.height) - y;
   
-  if (y < 0)
+  if (y < monitor.y)
     {
-      scroll_offset -= y;
-      requisition.height -= -y;
-      y = 0;
+      scroll_offset += monitor.y - y;
+      requisition.height -= monitor.y - y;
+      y = monitor.y;
     }
 
   if (scroll_offset > 0)
@@ -2374,14 +3366,18 @@ gtk_menu_position (GtkMenu *menu)
   
   gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window), 
                   x, y);
-  gtk_widget_set_usize (GTK_MENU_SHELL (menu)->active ?
-                       menu->toplevel : menu->tearoff_hbox,
-                       -1, requisition.height);
+
+  if (!GTK_MENU_SHELL (menu)->active)
+    {
+      gtk_window_resize (GTK_WINDOW (menu->tearoff_window),
+                        requisition.width, requisition.height);
+    }
+  
   menu->scroll_offset = scroll_offset;
 }
 
 static void
-gtk_menu_stop_scrolling (GtkMenu *menu)
+gtk_menu_remove_scroll_timeout (GtkMenu *menu)
 {
   if (menu->timeout_id)
     {
@@ -2390,6 +3386,14 @@ gtk_menu_stop_scrolling (GtkMenu *menu)
     }
 }
 
+static void
+gtk_menu_stop_scrolling (GtkMenu *menu)
+{
+  gtk_menu_remove_scroll_timeout (menu);
+
+  menu->upper_arrow_prelight = FALSE;
+  menu->lower_arrow_prelight = FALSE;
+}
 
 static void
 gtk_menu_scroll_to (GtkMenu *menu,
@@ -2401,6 +3405,7 @@ gtk_menu_scroll_to (GtkMenu *menu,
   gint border_width;
   gboolean last_visible;
   gint menu_height;
+  guint vertical_padding;
 
   widget = GTK_WIDGET (menu);
 
@@ -2408,29 +3413,34 @@ gtk_menu_scroll_to (GtkMenu *menu,
       menu->tearoff_adjustment &&
       (menu->tearoff_adjustment->value != offset))
     {
-      menu->tearoff_adjustment->value = offset;
+      menu->tearoff_adjustment->value =
+       CLAMP (offset,
+              0, menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size);
       gtk_adjustment_value_changed (menu->tearoff_adjustment);
     }
   
-  /* Scroll the menu: */
-  gdk_window_move (menu->bin_window, 0, -offset);
-
   /* Move/resize the viewport according to arrows: */
-  gdk_window_get_size (widget->window, &view_width, &view_height);
+  view_width = widget->allocation.width;
+  view_height = widget->allocation.height;
 
+  gtk_widget_style_get (GTK_WIDGET (menu),
+                       "vertical-padding", &vertical_padding,
+                       NULL);
+  
   border_width = GTK_CONTAINER (menu)->border_width;
   view_width -= (border_width + widget->style->xthickness) * 2;
-  view_height -= (border_width + widget->style->ythickness) * 2;
-  menu_height = widget->requisition.height - (border_width + widget->style->ythickness) * 2;
+  view_height -= (border_width + widget->style->ythickness + vertical_padding) * 2;
+  menu_height = widget->requisition.height -
+      (border_width + widget->style->ythickness + vertical_padding) * 2;
 
   x = border_width + widget->style->xthickness;
-  y = border_width + widget->style->ythickness;
-  
+  y = border_width + widget->style->ythickness + vertical_padding;
+
   if (!menu->tearoff_active)
     {
       last_visible = menu->upper_arrow_visible;
-      menu->upper_arrow_visible = (offset > 0);
-
+      menu->upper_arrow_visible = offset > 0;
+      
       if (menu->upper_arrow_visible)
        view_height -= MENU_SCROLL_ARROW_HEIGHT;
       
@@ -2441,11 +3451,14 @@ gtk_menu_scroll_to (GtkMenu *menu,
          
          /* If we hid the upper arrow, possibly remove timeout */
          if (menu->scroll_step < 0)
-           gtk_menu_stop_scrolling (menu);
+           {
+             gtk_menu_stop_scrolling (menu);
+             gtk_widget_queue_draw (GTK_WIDGET (menu));
+           }
        }
-      
+
       last_visible = menu->lower_arrow_visible;
-      menu->lower_arrow_visible = (view_height + offset < menu_height);
+      menu->lower_arrow_visible = offset < menu_height - view_height;
       
       if (menu->lower_arrow_visible)
        view_height -= MENU_SCROLL_ARROW_HEIGHT;
@@ -2457,30 +3470,75 @@ gtk_menu_scroll_to (GtkMenu *menu,
          
          /* If we hid the lower arrow, possibly remove timeout */
          if (menu->scroll_step > 0)
-           gtk_menu_stop_scrolling (menu);
+           {
+             gtk_menu_stop_scrolling (menu);
+             gtk_widget_queue_draw (GTK_WIDGET (menu));
+           }
        }
       
       if (menu->upper_arrow_visible)
        y += MENU_SCROLL_ARROW_HEIGHT;
     }
-  
-  gdk_window_move_resize (menu->view_window,
-                         x,
-                         y,
-                         view_width,
-                         view_height);
+
+  /* Scroll the menu: */
+  if (GTK_WIDGET_REALIZED (menu))
+    gdk_window_move (menu->bin_window, 0, -offset);
+
+  if (GTK_WIDGET_REALIZED (menu))
+    gdk_window_move_resize (menu->view_window,
+                           x,
+                           y,
+                           view_width,
+                           view_height);
 
   menu->scroll_offset = offset;
 }
 
+static gboolean
+compute_child_offset (GtkMenu   *menu,
+                     GtkWidget *menu_item,
+                     gint      *offset,
+                     gint      *height,
+                     gboolean  *is_last_child)
+{
+  GtkMenuPrivate *priv = gtk_menu_get_private (menu);
+  guint item_top_attach;
+  guint item_bottom_attach;
+  gint child_offset = 0;
+  gint i;
+
+  gtk_container_child_get (GTK_CONTAINER (menu), menu_item,
+                           "top_attach", &item_top_attach,
+                           "bottom_attach", &item_bottom_attach,
+                           NULL);
+
+  /* there is a possibility that we get called before _size_request, so
+   * check the height table for safety.
+   */
+  if (!priv->heights || priv->heights_length < priv->rows)
+    return FALSE;
+
+  /* when we have a row with only invisible children, it's height will
+   * be zero, so there's no need to check WIDGET_VISIBLE here
+   */
+  for (i = 0; i < item_top_attach; i++)
+    child_offset += priv->heights[i];
+
+  if (is_last_child)
+    *is_last_child = (item_bottom_attach == priv->rows);
+  if (offset)
+    *offset = child_offset;
+  if (height)
+    *height = priv->heights[item_top_attach];
+
+  return TRUE;
+}
+
 static void
 gtk_menu_scroll_item_visible (GtkMenuShell    *menu_shell,
                              GtkWidget       *menu_item)
 {
   GtkMenu *menu;
-  GtkWidget *child;
-  GList *children;
-  GtkRequisition child_requisition;
   gint child_offset, child_height;
   gint width, height;
   gint y;
@@ -2494,37 +3552,21 @@ gtk_menu_scroll_item_visible (GtkMenuShell    *menu_shell,
    * visible.
    */
 
-  child = NULL;
-  child_offset = 0;
-  child_height = 0;
-  children = menu_shell->children;
-  while (children)
+  if (compute_child_offset (menu, menu_item,
+                           &child_offset, &child_height, &last_child))
     {
-      child = children->data;
-      children = children->next;
-      
-      if (GTK_WIDGET_VISIBLE (child))
-       {
-         gtk_widget_size_request (child, &child_requisition);
-         child_offset += child_height;
-         child_height = child_requisition.height;
-       }
+      guint vertical_padding;
       
-      if (child == menu_item)
-       {
-         last_child = (children == NULL);
-         break;
-       }
-    }
-
-  if (child == menu_item)
-    {
       y = menu->scroll_offset;
-      gdk_window_get_size (GTK_WIDGET (menu)->window, &width, &height);
+      gdk_drawable_get_size (GTK_WIDGET (menu)->window, &width, &height);
 
-      height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness;
+      gtk_widget_style_get (GTK_WIDGET (menu),
+                           "vertical-padding", &vertical_padding,
+                           NULL);
+                           
+      height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness + 2*vertical_padding;
       
-      if (child_offset + child_height <= y)
+      if (child_offset < y)
        {
          /* Ignore the enter event we might get if the pointer is on the menu
           */
@@ -2539,7 +3581,7 @@ gtk_menu_scroll_item_visible (GtkMenuShell    *menu_shell,
          if (menu->lower_arrow_visible && !menu->tearoff_active)
            arrow_height += MENU_SCROLL_ARROW_HEIGHT;
          
-         if (child_offset >= y + height - arrow_height)
+         if (child_offset + child_height > y + height - arrow_height)
            {
              arrow_height = 0;
              if (!last_child && !menu->tearoff_active)
@@ -2614,15 +3656,15 @@ gtk_menu_reparent (GtkMenu      *menu,
   GtkWidget *widget = GTK_WIDGET (menu);
   gboolean was_floating = GTK_OBJECT_FLOATING (object);
 
-  gtk_object_ref (object);
+  g_object_ref (object);
   gtk_object_sink (object);
 
   if (unrealize)
     {
-      gtk_object_ref (object);
+      g_object_ref (object);
       gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
       gtk_container_add (GTK_CONTAINER (new_parent), widget);
-      gtk_object_unref (object);
+      g_object_unref (object);
     }
   else
     gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
@@ -2630,14 +3672,12 @@ gtk_menu_reparent (GtkMenu      *menu,
   if (was_floating)
     GTK_OBJECT_SET_FLAGS (object, GTK_FLOATING);
   else
-    gtk_object_unref (object);
+    g_object_unref (object);
 }
 
 static void
 gtk_menu_show_all (GtkWidget *widget)
 {
-  g_return_if_fail (GTK_IS_MENU (widget));
-
   /* Show children, but not self. */
   gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
 }
@@ -2646,10 +3686,417 @@ gtk_menu_show_all (GtkWidget *widget)
 static void
 gtk_menu_hide_all (GtkWidget *widget)
 {
-  g_return_if_fail (GTK_IS_MENU (widget));
-
   /* Hide children, but not self. */
   gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
 }
 
+/**
+ * gtk_menu_set_screen:
+ * @menu: a #GtkMenu.
+ * @screen: a #GdkScreen, or %NULL if the screen should be
+ *          determined by the widget the menu is attached to.
+ *
+ * Sets the #GdkScreen on which the menu will be displayed.
+ * 
+ * Since: 2.2
+ **/
+void
+gtk_menu_set_screen (GtkMenu   *menu, 
+                    GdkScreen *screen)
+{
+  g_return_if_fail (GTK_IS_MENU (menu));
+  g_return_if_fail (!screen || GDK_IS_SCREEN (screen));
+
+  g_object_set_data (G_OBJECT (menu), "gtk-menu-explicit-screen", screen);
+
+  if (screen)
+    {
+      menu_change_screen (menu, screen);
+    }
+  else
+    {
+      GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
+      if (attach_widget)
+       attach_widget_screen_changed (attach_widget, NULL, menu);
+    }
+}
+
+/**
+ * gtk_menu_attach:
+ * @menu: a #GtkMenu.
+ * @child: a #GtkMenuItem.
+ * @left_attach: The column number to attach the left side of the item to.
+ * @right_attach: The column number to attach the right side of the item to.
+ * @top_attach: The row number to attach the top of the item to.
+ * @bottom_attach: The row number to attach the bottom of the item to.
+ *
+ * Adds a new #GtkMenuItem to a (table) menu. The number of 'cells' that
+ * an item will occupy is specified by @left_attach, @right_attach,
+ * @top_attach and @bottom_attach. These each represent the leftmost,
+ * rightmost, uppermost and lower column and row numbers of the table.
+ * (Columns and rows are indexed from zero).
+ *
+ * Note that this function is not related to gtk_menu_detach().
+ *
+ * Since: 2.4
+ **/
+void
+gtk_menu_attach (GtkMenu   *menu,
+                 GtkWidget *child,
+                 guint      left_attach,
+                 guint      right_attach,
+                 guint      top_attach,
+                 guint      bottom_attach)
+{
+  g_return_if_fail (GTK_IS_MENU (menu));
+  g_return_if_fail (GTK_IS_MENU_ITEM (child));
+  g_return_if_fail (child->parent == NULL || 
+                   child->parent == GTK_WIDGET (menu));
+  g_return_if_fail (left_attach < right_attach);
+  g_return_if_fail (top_attach < bottom_attach);
+
+  if (!child->parent)
+    {
+      GTK_MENU_SHELL (menu)->children = 
+       g_list_append (GTK_MENU_SHELL (menu)->children, child);
+
+      gtk_widget_set_parent (child, GTK_WIDGET (menu));
+    }
+
+  gtk_container_child_set (GTK_CONTAINER (menu), child,
+                           "left_attach", left_attach,
+                           "right_attach", right_attach,
+                           "top_attach", top_attach,
+                           "bottom_attach", bottom_attach,
+                           NULL);
+}
+
+static gint
+gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
+{
+  gint popup_delay;
+
+  g_object_get (gtk_widget_get_settings (GTK_WIDGET (menu_shell)),
+               "gtk-menu-popup-delay", &popup_delay,
+               NULL);
+
+  return popup_delay;
+}
+
+static GtkWidget *
+find_child_containing (GtkMenuShell *menu_shell,
+                       int           left,
+                       int           right,
+                       int           top,
+                       int           bottom)
+{
+  GList *list;
+
+  /* find a child which includes the area given by
+   * left, right, top, bottom.
+   */
+
+  for (list = menu_shell->children; list; list = list->next)
+    {
+      guint l, r, t, b;
+
+      if (!_gtk_menu_item_is_selectable (list->data))
+        continue;
+
+      get_child_attach (list->data, &l, &r, &t, &b);
+
+      if (l <= left && right <= r
+          && t <= top && bottom <= b)
+        return GTK_WIDGET (list->data);
+    }
+
+  return NULL;
+}
+
+static void
+gtk_menu_move_current (GtkMenuShell *menu_shell,
+                       GtkMenuDirectionType direction)
+{
+  GtkMenuPrivate *priv = gtk_menu_get_private (GTK_MENU (menu_shell));
+
+  /* use special table menu key bindings */
+  if (menu_shell->active_menu_item && priv->columns > 1)
+    {
+      int i;
+      guint l, r, t, b;
+      gboolean rtl = (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL);
+      GtkWidget *match = NULL;
+
+      get_child_attach (menu_shell->active_menu_item, &l, &r, &t, &b);
 
+      if (direction == GTK_MENU_DIR_NEXT)
+        {
+          for (i = b; i < priv->rows; i++)
+            {
+              match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
+              if (match)
+                break;
+            }
+
+         if (!match)
+           {
+             /* wrap around */
+             for (i = 0; i < t; i++)
+               {
+                  match = find_child_containing (menu_shell,
+                                                 l, l + 1, i, i + 1);
+                  if (match)
+                    break;
+               }
+           }
+       }
+      else if (direction == GTK_MENU_DIR_PREV)
+        {
+          for (i = t; i > 0; i--)
+            {
+              match = find_child_containing (menu_shell, l, l + 1, i - 1, i);
+              if (match)
+                break;
+            }
+
+         if (!match)
+           {
+             /* wrap around */
+             for (i = priv->rows; i > b; i--)
+               {
+                  match = find_child_containing (menu_shell,
+                                                 l, l + 1, i - 1, i);
+                  if (match)
+                   break;
+               }
+           }
+       }
+      else if ((!rtl && direction == GTK_MENU_DIR_PARENT)
+               || (rtl && direction == GTK_MENU_DIR_CHILD))
+        {
+          /* we go one left if possible */
+          if (l > 0)
+            match = find_child_containing (menu_shell, l - 1, l, t, t + 1);
+
+          if (!match)
+            {
+              GtkWidget *parent = menu_shell->parent_menu_shell;
+
+              if (!parent
+                  || g_list_length (GTK_MENU_SHELL (parent)->children) <= 1)
+                match = menu_shell->active_menu_item;
+            }
+        }
+      else if ((!rtl && direction == GTK_MENU_DIR_CHILD)
+               || (rtl && direction == GTK_MENU_DIR_PARENT))
+        {
+          /* we go one right if possible */
+          if (r < priv->columns)
+            match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
+
+          if (!match)
+            {
+              GtkWidget *parent = menu_shell->parent_menu_shell;
+
+              if (! GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu &&
+                  (!parent ||
+                   g_list_length (GTK_MENU_SHELL (parent)->children) <= 1))
+                match = menu_shell->active_menu_item;
+            }
+        }
+
+      if (match)
+        {
+         gtk_menu_shell_select_item (menu_shell, match);
+          return;
+        }
+    }
+
+  GTK_MENU_SHELL_CLASS (parent_class)->move_current (menu_shell, direction);
+}
+
+static gint
+get_visible_size (GtkMenu *menu)
+{
+  GtkWidget *widget = GTK_WIDGET (menu);
+  GtkContainer *container = GTK_CONTAINER (menu);
+  
+  gint menu_height = (widget->allocation.height
+                     - 2 * (container->border_width
+                            + widget->style->ythickness));
+  
+  if (menu->upper_arrow_visible && !menu->tearoff_active)
+    menu_height -= MENU_SCROLL_ARROW_HEIGHT;
+  if (menu->lower_arrow_visible && !menu->tearoff_active)
+    menu_height -= MENU_SCROLL_ARROW_HEIGHT;
+
+  return menu_height;
+}
+
+/* Find the sensitive on-screen child containing @y, or if none,
+ * the nearest selectable onscreen child. (%NULL if none)
+ */
+static GtkWidget *
+child_at (GtkMenu *menu,
+         gint     y)
+{
+  GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
+  GtkWidget *child = NULL;
+  gint child_offset = 0;
+  GList *children;
+  gint menu_height;
+  gint lower, upper;           /* Onscreen bounds */
+
+  menu_height = get_visible_size (menu);
+  lower = menu->scroll_offset;
+  upper = menu->scroll_offset + menu_height;
+  
+  for (children = menu_shell->children; children; children = children->next)
+    {
+      if (GTK_WIDGET_VISIBLE (children->data))
+       {
+         GtkRequisition child_requisition;
+
+         gtk_widget_size_request (children->data, &child_requisition);
+
+         if (_gtk_menu_item_is_selectable (children->data) &&
+             child_offset >= lower &&
+             child_offset + child_requisition.height <= upper)
+           {
+             child = children->data;
+             
+             if (child_offset + child_requisition.height > y &&
+                 !GTK_IS_TEAROFF_MENU_ITEM (child))
+               return child;
+           }
+      
+         child_offset += child_requisition.height;
+       }
+    }
+
+  return child;
+}
+
+static gint
+get_menu_height (GtkMenu *menu)
+{
+  gint height;
+  GtkWidget *widget = GTK_WIDGET (menu);
+
+  height = widget->requisition.height;
+  height -= (GTK_CONTAINER (widget)->border_width + widget->style->ythickness) * 2;
+
+  if (menu->upper_arrow_visible && !menu->tearoff_active)
+    height -= MENU_SCROLL_ARROW_HEIGHT;
+
+  if (menu->lower_arrow_visible && !menu->tearoff_active)
+    height -= MENU_SCROLL_ARROW_HEIGHT;
+
+  return height;
+}
+
+static void
+gtk_menu_real_move_scroll (GtkMenu       *menu,
+                          GtkScrollType  type)
+{
+  gint page_size = get_visible_size (menu);
+  gint end_position = get_menu_height (menu);
+  GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
+  
+  switch (type)
+    {
+    case GTK_SCROLL_PAGE_UP:
+    case GTK_SCROLL_PAGE_DOWN:
+      {
+       gint old_offset;
+        gint new_offset;
+       gint child_offset = 0;
+       gboolean old_upper_arrow_visible;
+       gint step;
+
+       if (type == GTK_SCROLL_PAGE_UP)
+         step = - page_size;
+       else
+         step = page_size;
+
+       if (menu_shell->active_menu_item)
+         {
+           gint child_height;
+           
+           compute_child_offset (menu, menu_shell->active_menu_item,
+                                 &child_offset, &child_height, NULL);
+           child_offset += child_height / 2;
+         }
+
+       menu_shell->ignore_enter = TRUE;
+       old_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
+       old_offset = menu->scroll_offset;
+
+        new_offset = menu->scroll_offset + step;
+        new_offset = CLAMP (new_offset, 0, end_position - page_size);
+
+        gtk_menu_scroll_to (menu, new_offset);
+       
+       if (menu_shell->active_menu_item)
+         {
+           GtkWidget *new_child;
+           gboolean new_upper_arrow_visible = menu->upper_arrow_visible && !menu->tearoff_active;
+
+           if (menu->scroll_offset != old_offset)
+             step = menu->scroll_offset - old_offset;
+
+           step -= (new_upper_arrow_visible - old_upper_arrow_visible) * MENU_SCROLL_ARROW_HEIGHT;
+
+           new_child = child_at (menu, child_offset + step);
+           if (new_child)
+             gtk_menu_shell_select_item (menu_shell, new_child);
+         }
+      }
+      break;
+    case GTK_SCROLL_START:
+      /* Ignore the enter event we might get if the pointer is on the menu
+       */
+      menu_shell->ignore_enter = TRUE;
+      gtk_menu_scroll_to (menu, 0);
+      gtk_menu_shell_select_first (menu_shell, TRUE);
+      break;
+    case GTK_SCROLL_END:
+      /* Ignore the enter event we might get if the pointer is on the menu
+       */
+      menu_shell->ignore_enter = TRUE;
+      gtk_menu_scroll_to (menu, end_position - page_size);
+      _gtk_menu_shell_select_last (menu_shell, TRUE);
+      break;
+    default:
+      break;
+    }
+}
+
+
+/**
+ * gtk_menu_set_monitor:
+ * @menu: a #GtkMenu
+ * @monitor_num: the number of the monitor on which the menu should
+ *    be popped up
+ * 
+ * Informs GTK+ on which monitor a menu should be popped up. 
+ * See gdk_screen_get_monitor_geometry().
+ *
+ * This function should be called from a #GtkMenuPositionFunc if the
+ * menu should not appear on the same monitor as the pointer. This 
+ * information can't be reliably inferred from the coordinates returned
+ * by a #GtkMenuPositionFunc, since, for very long menus, these coordinates 
+ * may extend beyond the monitor boundaries or even the screen boundaries. 
+ *
+ * Since: 2.4
+ **/
+void gtk_menu_set_monitor (GtkMenu *menu,
+                          gint     monitor_num)
+{
+  GtkMenuPrivate *priv;
+  g_return_if_fail (GTK_IS_MENU (menu));
+
+  priv = gtk_menu_get_private (menu);
+  
+  priv->monitor_num = monitor_num;
+}