[gtk+/wip/attente/popup-at: 2/2] port to new gtk_menu_popup_at_* () functions



commit 46b715cf36f48881ec80b970c931c0d0e493bf11
Author: William Hua <william hua canonical com>
Date:   Wed Jun 15 11:03:49 2016 -0400

    port to new gtk_menu_popup_at_* () functions

 gtk/gtkentry.c           |   90 +++---------
 gtk/gtklabel.c           |   55 +------
 gtk/gtklinkbutton.c      |   69 ++--------
 gtk/gtkmenuitem.c        |  360 +++++++++++++++++++---------------------------
 gtk/gtkmenuitemprivate.h |    1 -
 gtk/gtknotebook.c        |   83 +++++------
 gtk/gtktextview.c        |  149 ++++++--------------
 gtk/gtktoolbar.c         |   94 +++++--------
 gtk/gtkwindow.c          |   21 +---
 9 files changed, 304 insertions(+), 618 deletions(-)
---
diff --git a/gtk/gtkentry.c b/gtk/gtkentry.c
index dd792eb..841d6ae 100644
--- a/gtk/gtkentry.c
+++ b/gtk/gtkentry.c
@@ -9430,56 +9430,10 @@ popup_menu_detach (GtkWidget *attach_widget,
   priv_attach->popup_menu = NULL;
 }
 
-static void
-popup_position_func (GtkMenu   *menu,
-                     gint      *x,
-                     gint      *y,
-                     gboolean  *push_in,
-                     gpointer  user_data)
-{
-  GtkEntry *entry = GTK_ENTRY (user_data);
-  GtkEntryPrivate *priv = entry->priv;
-  GtkWidget *widget = GTK_WIDGET (entry);
-  GdkDisplay *display;
-  GtkRequisition menu_req;
-  GdkMonitor *monitor;
-  GdkRectangle area;
-  gint strong_x, height;
-
-  g_return_if_fail (gtk_widget_get_realized (widget));
-
-  gdk_window_get_origin (priv->text_area, x, y);
-
-  display = gtk_widget_get_display (widget);
-  monitor = gdk_display_get_monitor_at_window (display, priv->text_area);
-  gtk_menu_place_on_monitor (menu, monitor);
-  gdk_monitor_get_workarea (monitor, &area);
-  gtk_widget_get_preferred_size (priv->popup_menu, &menu_req, NULL);
-  height = gdk_window_get_height (priv->text_area);
-  gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &strong_x, NULL);
-
-  *x += 0 + strong_x - priv->scroll_offset;
-  if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
-    *x -= menu_req.width;
-
-  if ((*y + height + menu_req.height) <= area.y + area.height)
-    *y += height;
-  else if ((*y - menu_req.height) >= area.y)
-    *y -= menu_req.height;
-  else if (area.y + area.height - (*y + height) > *y)
-    *y += height;
-  else
-    *y -= menu_req.height;
-
-  *push_in = FALSE;
-}
-
 typedef struct
 {
   GtkEntry *entry;
-  guint button;
-  guint time;
-  GdkDevice *device;
+  GdkEvent *trigger_event;
 } PopupInfo;
 
 static void
@@ -9490,6 +9444,7 @@ popup_targets_received (GtkClipboard     *clipboard,
   PopupInfo *info = user_data;
   GtkEntry *entry = info->entry;
   GtkEntryPrivate *info_entry_priv = entry->priv;
+  GdkRectangle rect = { 0, 0, 1, 0 };
 
   if (gtk_widget_get_realized (GTK_WIDGET (entry)))
     {
@@ -9540,19 +9495,28 @@ popup_targets_received (GtkClipboard     *clipboard,
 
       g_signal_emit (entry, signals[POPULATE_POPUP], 0, menu);
 
-      if (info->device)
-       gtk_menu_popup_for_device (GTK_MENU (menu),
-                                   info->device, NULL, NULL, NULL, NULL, NULL,
-                                   info->button, info->time);
+      if (info->trigger_event && gdk_event_triggers_context_menu (info->trigger_event))
+        gtk_menu_popup_at_pointer (GTK_MENU (menu), info->trigger_event);
       else
-       {
-          gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
-                          popup_position_func, entry,
-                          0, gtk_get_current_event_time ());
+        {
+          gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &rect.x, NULL);
+          rect.x -= info_entry_priv->scroll_offset;
+          rect.height = gdk_window_get_height (info_entry_priv->text_area);
+
+          gtk_menu_popup_at_rect (GTK_MENU (menu),
+                                  info_entry_priv->text_area,
+                                  &rect,
+                                  GDK_GRAVITY_SOUTH_EAST,
+                                  GDK_GRAVITY_NORTH_WEST,
+                                  NULL);
+
           gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
-       }
+        }
     }
 
+  if (info->trigger_event)
+    gdk_event_free (info->trigger_event);
+
   g_object_unref (entry);
   g_slice_free (PopupInfo, info);
 }
@@ -9568,19 +9532,7 @@ gtk_entry_do_popup (GtkEntry       *entry,
    * we get them, then we actually pop up the menu.
    */
   info->entry = g_object_ref (entry);
-  
-  if (event)
-    {
-      gdk_event_get_button (event, &info->button);
-      info->time = gdk_event_get_time (event);
-      info->device = gdk_event_get_device (event);
-    }
-  else
-    {
-      info->button = 0;
-      info->time = gtk_get_current_event_time ();
-      info->device = NULL;
-    }
+  info->trigger_event = event ? gdk_event_copy (event) : gtk_get_current_event ();
 
   gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (entry), GDK_SELECTION_CLIPBOARD),
                                  gdk_atom_intern_static_string ("TARGETS"),
diff --git a/gtk/gtklabel.c b/gtk/gtklabel.c
index dd2bbbf..4851b3a 100644
--- a/gtk/gtklabel.c
+++ b/gtk/gtklabel.c
@@ -6545,48 +6545,6 @@ popup_menu_detach (GtkWidget *attach_widget,
 }
 
 static void
-popup_position_func (GtkMenu   *menu,
-                     gint      *x,
-                     gint      *y,
-                     gboolean  *push_in,
-                     gpointer  user_data)
-{
-  GtkLabel *label;
-  GtkWidget *widget;
-  GtkAllocation allocation;
-  GtkRequisition req;
-  GdkDisplay *display;
-  GdkMonitor *monitor;
-  GdkRectangle workarea;
-
-  label = GTK_LABEL (user_data);
-  widget = GTK_WIDGET (label);
-
-  g_return_if_fail (gtk_widget_get_realized (widget));
-
-  display = gtk_widget_get_display (widget);
-  monitor = gdk_display_get_monitor_at_window (display,
-                                               gtk_widget_get_window (widget));
-  gdk_monitor_get_workarea (monitor, &workarea);
-
-  gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
-  gtk_widget_get_allocation (widget, &allocation);
-
-  *x += allocation.x;
-  *y += allocation.y;
-
-  gtk_widget_get_preferred_size (GTK_WIDGET (menu), &req, NULL);
-
-  gtk_widget_get_allocation (widget, &allocation);
-
-  *x += allocation.width / 2;
-  *y += allocation.height;
-
-  *x = CLAMP (*x, 0, MAX (0, workarea.width - req.width));
-  *y = CLAMP (*y, 0, MAX (0, workarea.height - req.height));
-}
-
-static void
 open_link_activate_cb (GtkMenuItem *menuitem,
                        GtkLabel    *label)
 {
@@ -6696,14 +6654,15 @@ gtk_label_do_popup (GtkLabel       *label,
   g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
 
   if (event)
-    gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
-                    NULL, NULL,
-                    event->button, event->time);
+    gtk_menu_popup_at_pointer (GTK_MENU (menu), (GdkEvent *) event);
   else
     {
-      gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
-                      popup_position_func, label,
-                      0, gtk_get_current_event_time ());
+      gtk_menu_popup_at_widget (GTK_MENU (menu),
+                                GTK_WIDGET (label),
+                                GDK_GRAVITY_SOUTH,
+                                GDK_GRAVITY_NORTH_WEST,
+                                NULL);
+
       gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
     }
 }
diff --git a/gtk/gtklinkbutton.c b/gtk/gtklinkbutton.c
index fe46867..c19b36c 100644
--- a/gtk/gtklinkbutton.c
+++ b/gtk/gtklinkbutton.c
@@ -336,43 +336,6 @@ popup_menu_detach (GtkWidget *attach_widget,
 }
 
 static void
-popup_position_func (GtkMenu  *menu,
-                    gint     *x,
-                    gint     *y,
-                    gboolean *push_in,
-                    gpointer  user_data)
-{
-  GtkLinkButton *link_button = GTK_LINK_BUTTON (user_data);
-  GtkLinkButtonPrivate *priv = link_button->priv;
-  GtkAllocation allocation;
-  GtkWidget *widget = GTK_WIDGET (link_button);
-  GdkDisplay *display;
-  GdkMonitor *monitor;
-  GtkRequisition req;
-  GdkRectangle area;
-
-  g_return_if_fail (gtk_widget_get_realized (widget));
-
-  gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
-
-  gtk_widget_get_preferred_size (priv->popup_menu, &req, NULL);
-
-  gtk_widget_get_allocation (widget, &allocation);
-  *x += allocation.width / 2;
-  *y += allocation.height;
-
-  display = gtk_widget_get_display (widget);
-  monitor = gdk_display_get_monitor_at_point (display, *x, *y);
-  gtk_menu_place_on_monitor (menu, monitor);
-  gdk_monitor_get_workarea (monitor, &area);
-
-  *x = CLAMP (*x, area.x, area.x + MAX (0, area.width - req.width));
-  *y = CLAMP (*y, area.y, area.y + MAX (0, area.height - req.height));
-
-  *push_in = FALSE;
-}
-
-static void
 copy_activate_cb (GtkWidget     *widget,
                  GtkLinkButton *link_button)
 {
@@ -388,19 +351,6 @@ gtk_link_button_do_popup (GtkLinkButton  *link_button,
                          GdkEventButton *event)
 {
   GtkLinkButtonPrivate *priv = link_button->priv;
-  gint button;
-  guint time;
-  
-  if (event)
-    {
-      button = event->button;
-      time = event->time;
-    }
-  else
-    {
-      button = 0;
-      time = gtk_get_current_event_time ();
-    }
 
   if (gtk_widget_get_realized (GTK_WIDGET (link_button)))
     {
@@ -423,17 +373,18 @@ gtk_link_button_do_popup (GtkLinkButton  *link_button,
       gtk_widget_show (menu_item);
       gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menu_item);
 
-      if (button)
-        gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
-                       NULL, NULL,
-                       button, time);
+      if (event)
+        gtk_menu_popup_at_pointer (GTK_MENU (priv->popup_menu), (GdkEvent *) event);
       else
         {
-          gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
-                         popup_position_func, link_button,
-                         button, time);
-         gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->popup_menu), FALSE);
-       }
+          gtk_menu_popup_at_widget (GTK_MENU (priv->popup_menu),
+                                    GTK_WIDGET (link_button),
+                                    GDK_GRAVITY_SOUTH,
+                                    GDK_GRAVITY_NORTH_WEST,
+                                    NULL);
+
+          gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->popup_menu), FALSE);
+        }
     }
 }
 
diff --git a/gtk/gtkmenuitem.c b/gtk/gtkmenuitem.c
index 7910806..5655604 100644
--- a/gtk/gtkmenuitem.c
+++ b/gtk/gtkmenuitem.c
@@ -171,11 +171,6 @@ static gboolean gtk_menu_item_mnemonic_activate     (GtkWidget   *widget,
 
 static void gtk_menu_item_ensure_label   (GtkMenuItem      *menu_item);
 static gint gtk_menu_item_popup_timeout  (gpointer          data);
-static void gtk_menu_item_position_menu  (GtkMenu          *menu,
-                                          gint             *x,
-                                          gint             *y,
-                                          gboolean         *push_in,
-                                          gpointer          user_data);
 static void gtk_menu_item_show_all       (GtkWidget        *widget);
 
 static void gtk_menu_item_forall         (GtkContainer    *container,
@@ -1909,19 +1904,61 @@ free_timeval (GTimeVal *val)
 }
 
 static void
-gtk_menu_item_real_popup_submenu (GtkWidget *widget,
-                                  gboolean   remember_exact_time)
+popped_up_cb (GtkMenu            *menu,
+              const GdkRectangle *flipped_rect,
+              const GdkRectangle *real_rect,
+              gboolean            flipped_x,
+              gboolean            flipped_y,
+              GtkMenuItem        *menu_item)
+{
+  GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (menu_item));
+  GtkMenu *parent_menu = GTK_IS_MENU (parent) ? GTK_MENU (parent) : NULL;
+
+  if (parent_menu && GTK_IS_MENU_ITEM (parent_menu->priv->parent_menu_item))
+    menu_item->priv->submenu_direction = GTK_MENU_ITEM 
(parent_menu->priv->parent_menu_item)->priv->submenu_direction;
+  else
+    {
+      /* this case is stateful, do it at most once */
+      g_signal_handlers_disconnect_by_func (menu, popped_up_cb, menu_item);
+    }
+
+  if (flipped_x)
+    {
+      switch (menu_item->priv->submenu_direction)
+        {
+        case GTK_DIRECTION_LEFT:
+          menu_item->priv->submenu_direction = GTK_DIRECTION_RIGHT;
+          break;
+
+        case GTK_DIRECTION_RIGHT:
+          menu_item->priv->submenu_direction = GTK_DIRECTION_LEFT;
+          break;
+        }
+    }
+}
+
+static void
+gtk_menu_item_real_popup_submenu (GtkWidget      *widget,
+                                  const GdkEvent *trigger_event,
+                                  gboolean        remember_exact_time)
 {
   GtkMenuItem *menu_item = GTK_MENU_ITEM (widget);
   GtkMenuItemPrivate *priv = menu_item->priv;
+  GtkSubmenuDirection submenu_direction;
+  GtkStyleContext *context;
+  GtkBorder parent_padding;
+  GtkBorder menu_padding;
+  gint horizontal_offset;
+  gint vertical_offset;
   GtkWidget *parent;
+  GtkMenu *parent_menu;
 
   parent = gtk_widget_get_parent (widget);
+  parent_menu = GTK_IS_MENU (parent) ? GTK_MENU (parent) : NULL;
 
   if (gtk_widget_is_sensitive (priv->submenu) && parent)
     {
       gboolean take_focus;
-      GtkMenuPositionFunc menu_position_func;
 
       take_focus = gtk_menu_shell_get_take_focus (GTK_MENU_SHELL (parent));
       gtk_menu_shell_set_take_focus (GTK_MENU_SHELL (priv->submenu), take_focus);
@@ -1942,24 +1979,91 @@ gtk_menu_item_real_popup_submenu (GtkWidget *widget,
                              "gtk-menu-exact-popup-time", NULL);
         }
 
-      /* gtk_menu_item_position_menu positions the submenu from the
-       * menuitems position. If the menuitem doesn't have a window,
-       * that doesn't work. In that case we use the default
-       * positioning function instead which places the submenu at the
-       * mouse cursor.
+      /* Position the submenu at the menu item if it is mapped.
+       * Otherwise, position the submenu at the pointer device.
        */
       if (gtk_widget_get_window (widget))
-        menu_position_func = gtk_menu_item_position_menu;
+        {
+          switch (priv->submenu_placement)
+            {
+            case GTK_TOP_BOTTOM:
+              g_object_set (priv->submenu,
+                            "anchor-hints", GDK_ANCHOR_FLIP_Y |
+                                            GDK_ANCHOR_SLIDE |
+                                            GDK_ANCHOR_RESIZE,
+                            "menu-type-hint", priv->from_menubar ?
+                                              GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU :
+                                              GDK_WINDOW_TYPE_HINT_POPUP_MENU,
+                            NULL);
+
+              gtk_menu_popup_at_widget (GTK_MENU (priv->submenu),
+                                        widget,
+                                        GDK_GRAVITY_SOUTH_WEST,
+                                        GDK_GRAVITY_NORTH_WEST,
+                                        trigger_event);
+
+              break;
+
+            case GTK_LEFT_RIGHT:
+              if (parent_menu && GTK_IS_MENU_ITEM (parent_menu->priv->parent_menu_item))
+                submenu_direction = GTK_MENU_ITEM 
(parent_menu->priv->parent_menu_item)->priv->submenu_direction;
+              else
+                submenu_direction = priv->submenu_direction;
+
+              g_signal_handlers_disconnect_by_func (priv->submenu, popped_up_cb, menu_item);
+              g_signal_connect (priv->submenu, "popped-up", G_CALLBACK (popped_up_cb), menu_item);
+
+              gtk_widget_style_get (priv->submenu,
+                                    "horizontal-offset", &horizontal_offset,
+                                    "vertical-offset", &vertical_offset,
+                                    NULL);
+
+              context = gtk_widget_get_style_context (parent);
+              gtk_style_context_get_padding (context, gtk_style_context_get_state (context), 
&parent_padding);
+              context = gtk_widget_get_style_context (priv->submenu);
+              gtk_style_context_get_padding (context, gtk_style_context_get_state (context), &menu_padding);
+
+              g_object_set (priv->submenu,
+                            "anchor-hints", GDK_ANCHOR_FLIP_X |
+                                            GDK_ANCHOR_SLIDE |
+                                            GDK_ANCHOR_RESIZE,
+                            "rect-anchor-dy", vertical_offset - menu_padding.top,
+                            NULL);
+
+              switch (submenu_direction)
+                {
+                case GTK_DIRECTION_RIGHT:
+                  g_object_set (priv->submenu,
+                                "rect-anchor-dx", horizontal_offset + parent_padding.right + 
menu_padding.left,
+                                NULL);
+
+                  gtk_menu_popup_at_widget (GTK_MENU (priv->submenu),
+                                            widget,
+                                            GDK_GRAVITY_NORTH_EAST,
+                                            GDK_GRAVITY_NORTH_WEST,
+                                            trigger_event);
+
+                  break;
+
+                case GTK_DIRECTION_LEFT:
+                  g_object_set (priv->submenu,
+                                "rect-anchor-dx", -(horizontal_offset + parent_padding.left + 
menu_padding.right),
+                                NULL);
+
+                  gtk_menu_popup_at_widget (GTK_MENU (priv->submenu),
+                                            widget,
+                                            GDK_GRAVITY_NORTH_WEST,
+                                            GDK_GRAVITY_NORTH_EAST,
+                                            trigger_event);
+
+                  break;
+                }
+
+              break;
+            }
+        }
       else
-        menu_position_func = NULL;
-
-      gtk_menu_popup (GTK_MENU (priv->submenu),
-                      parent,
-                      widget,
-                      menu_position_func,
-                      menu_item,
-                      GTK_MENU_SHELL (parent)->priv->button,
-                      0);
+        gtk_menu_popup_at_pointer (GTK_MENU (priv->submenu), trigger_event);
     }
 
   /* Enable themeing of the parent menu item depending on whether
@@ -1968,10 +2072,17 @@ gtk_menu_item_real_popup_submenu (GtkWidget *widget,
   gtk_widget_queue_draw (widget);
 }
 
+typedef struct
+{
+  GtkMenuItem *menu_item;
+  GdkEvent    *trigger_event;
+} PopupInfo;
+
 static gint
 gtk_menu_item_popup_timeout (gpointer data)
 {
-  GtkMenuItem *menu_item = GTK_MENU_ITEM (data);
+  PopupInfo *info = data;
+  GtkMenuItem *menu_item = info->menu_item;
   GtkMenuItemPrivate *priv = menu_item->priv;
   GtkWidget *parent;
 
@@ -1980,13 +2091,21 @@ gtk_menu_item_popup_timeout (gpointer data)
   if ((GTK_IS_MENU_SHELL (parent) && GTK_MENU_SHELL (parent)->priv->active) ||
       (GTK_IS_MENU (parent) && GTK_MENU (parent)->priv->torn_off))
     {
-      gtk_menu_item_real_popup_submenu (GTK_WIDGET (menu_item), TRUE);
-      if (priv->timer_from_keypress && priv->submenu)
+      gtk_menu_item_real_popup_submenu (GTK_WIDGET (menu_item), info->trigger_event, TRUE);
+      if (info->trigger_event &&
+          info->trigger_event->type != GDK_BUTTON_PRESS &&
+          info->trigger_event->type != GDK_ENTER_NOTIFY &&
+          priv->submenu)
         GTK_MENU_SHELL (priv->submenu)->priv->ignore_enter = TRUE;
     }
 
   priv->timer = 0;
 
+  if (info->trigger_event)
+    gdk_event_free (info->trigger_event);
+
+  g_slice_free (PopupInfo, info);
+
   return FALSE;
 }
 
@@ -2022,28 +2141,21 @@ _gtk_menu_item_popup_submenu (GtkWidget *widget,
 
       if (popup_delay > 0)
         {
-          GdkEvent *event = gtk_get_current_event ();
+          PopupInfo *info = g_slice_new (PopupInfo);
+
+          info->menu_item = menu_item;
+          info->trigger_event = gtk_get_current_event ();
 
           priv->timer = gdk_threads_add_timeout (popup_delay,
                                                  gtk_menu_item_popup_timeout,
-                                                 menu_item);
+                                                 info);
           g_source_set_name_by_id (priv->timer, "[gtk+] gtk_menu_item_popup_timeout");
 
-          if (event &&
-              event->type != GDK_BUTTON_PRESS &&
-              event->type != GDK_ENTER_NOTIFY)
-            priv->timer_from_keypress = TRUE;
-          else
-            priv->timer_from_keypress = FALSE;
-
-          if (event)
-            gdk_event_free (event);
-
           return;
         }
     }
 
-  gtk_menu_item_real_popup_submenu (widget, FALSE);
+  gtk_menu_item_real_popup_submenu (widget, NULL, FALSE);
 }
 
 void
@@ -2069,176 +2181,6 @@ _gtk_menu_item_popdown_submenu (GtkWidget *widget)
     }
 }
 
-static void
-get_offsets (GtkMenu *menu,
-             gint    *horizontal_offset,
-             gint    *vertical_offset)
-{
-  GtkStyleContext *context;
-  GtkBorder padding;
-
-  gtk_widget_style_get (GTK_WIDGET (menu),
-                        "horizontal-offset", horizontal_offset,
-                        "vertical-offset", vertical_offset,
-                        NULL);
-
-  context = gtk_widget_get_style_context (GTK_WIDGET (menu));
-  gtk_style_context_get_padding (context, gtk_style_context_get_state (context), &padding);
-
-  *vertical_offset -= padding.top;
-  *horizontal_offset += padding.left;
-}
-
-static void
-gtk_menu_item_position_menu (GtkMenu  *menu,
-                             gint     *x,
-                             gint     *y,
-                             gboolean *push_in,
-                             gpointer  user_data)
-{
-  GtkMenuItem *menu_item = GTK_MENU_ITEM (user_data);
-  GtkMenuItemPrivate *priv = menu_item->priv;
-  GtkAllocation allocation;
-  GtkWidget *widget;
-  GtkMenuItem *parent_menu_item;
-  GtkWidget *parent;
-  GdkDisplay *display;
-  gint twidth, theight;
-  gint tx, ty;
-  GtkTextDirection direction;
-  GdkMonitor *monitor;
-  GdkRectangle workarea;
-  gint horizontal_offset;
-  gint vertical_offset;
-  gint available_left, available_right;
-  GtkStyleContext *context;
-  GtkBorder parent_padding;
-
-  g_return_if_fail (menu != NULL);
-  g_return_if_fail (x != NULL);
-  g_return_if_fail (y != NULL);
-
-  widget = GTK_WIDGET (user_data);
-
-  if (push_in)
-    *push_in = FALSE;
-
-  direction = gtk_widget_get_direction (widget);
-
-  twidth = gtk_widget_get_allocated_width (GTK_WIDGET (menu));
-  theight = gtk_widget_get_allocated_height (GTK_WIDGET (menu));
-
-  display = gtk_widget_get_display (GTK_WIDGET (menu));
-  monitor = gdk_display_get_monitor_at_window (display, priv->event_window);
-  gdk_monitor_get_workarea (monitor, &workarea);
-
-  if (!gdk_window_get_origin (gtk_widget_get_window (widget), &tx, &ty))
-    {
-      g_warning ("Menu not on screen");
-      return;
-    }
-
-  gtk_widget_get_allocation (widget, &allocation);
-
-  tx += allocation.x;
-  ty += allocation.y;
-
-  get_offsets (menu, &horizontal_offset, &vertical_offset);
-
-  available_left = tx - workarea.x;
-  available_right = workarea.x + workarea.width - (tx + allocation.width);
-
-  parent = gtk_widget_get_parent (widget);
-  priv->from_menubar = GTK_IS_MENU_BAR (parent);
-
-  switch (priv->submenu_placement)
-    {
-    case GTK_TOP_BOTTOM:
-      if (direction == GTK_TEXT_DIR_LTR)
-        priv->submenu_direction = GTK_DIRECTION_RIGHT;
-      else
-        {
-          priv->submenu_direction = GTK_DIRECTION_LEFT;
-          tx += allocation.width - twidth;
-        }
-      if ((ty + allocation.height + theight) <= workarea.y + workarea.height)
-        ty += allocation.height;
-      else if ((ty - theight) >= workarea.y)
-        ty -= theight;
-      else if (workarea.y + workarea.height - (ty + allocation.height) > ty)
-        ty += allocation.height;
-      else
-        ty -= theight;
-      break;
-
-    case GTK_LEFT_RIGHT:
-      if (GTK_IS_MENU (parent))
-        parent_menu_item = GTK_MENU_ITEM (GTK_MENU (parent)->priv->parent_menu_item);
-      else
-        parent_menu_item = NULL;
-
-      context = gtk_widget_get_style_context (parent);
-      gtk_style_context_get_padding (context, gtk_style_context_get_state (context), &parent_padding);
-
-      if (parent_menu_item && !GTK_MENU (parent)->priv->torn_off)
-        {
-          priv->submenu_direction = parent_menu_item->priv->submenu_direction;
-        }
-      else
-        {
-          if (direction == GTK_TEXT_DIR_LTR)
-            priv->submenu_direction = GTK_DIRECTION_RIGHT;
-          else
-            priv->submenu_direction = GTK_DIRECTION_LEFT;
-        }
-
-      switch (priv->submenu_direction)
-        {
-        case GTK_DIRECTION_LEFT:
-          if (tx - twidth - parent_padding.left - horizontal_offset >= workarea.x ||
-              available_left >= available_right)
-            tx -= twidth + parent_padding.left + horizontal_offset;
-          else
-            {
-              priv->submenu_direction = GTK_DIRECTION_RIGHT;
-              tx += allocation.width + parent_padding.right + horizontal_offset;
-            }
-          break;
-
-        case GTK_DIRECTION_RIGHT:
-          if (tx + allocation.width + parent_padding.right + horizontal_offset + twidth <= workarea.x + 
workarea.width ||
-              available_right >= available_left)
-            tx += allocation.width + parent_padding.right + horizontal_offset;
-          else
-            {
-              priv->submenu_direction = GTK_DIRECTION_LEFT;
-              tx -= twidth + parent_padding.left + horizontal_offset;
-            }
-          break;
-        }
-
-      ty += vertical_offset;
-
-      /* If the height of the menu doesn't fit we move it upward. */
-      ty = CLAMP (ty, workarea.y, MAX (workarea.y, workarea.y + workarea.height - theight));
-      break;
-    }
-
-  /* If we have negative, tx, here it is because we can't get
-   * the menu all the way on screen. Favor the left portion.
-   */
-  *x = CLAMP (tx, workarea.x, MAX (workarea.x, workarea.x + workarea.width - twidth));
-  *y = ty;
-
-  gtk_menu_place_on_monitor (menu, monitor);
-
-  if (!gtk_widget_get_visible (menu->priv->toplevel))
-    {
-      gtk_window_set_type_hint (GTK_WINDOW (menu->priv->toplevel), priv->from_menubar?
-                                GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU : GDK_WINDOW_TYPE_HINT_POPUP_MENU);
-    }
-}
-
 /**
  * gtk_menu_item_set_right_justified:
  * @menu_item: a #GtkMenuItem.
diff --git a/gtk/gtkmenuitemprivate.h b/gtk/gtkmenuitemprivate.h
index 108a188..cb213d9 100644
--- a/gtk/gtkmenuitemprivate.h
+++ b/gtk/gtkmenuitemprivate.h
@@ -47,7 +47,6 @@ struct _GtkMenuItemPrivate
   guint submenu_placement      : 1;
   guint submenu_direction      : 1;
   guint right_justify          : 1;
-  guint timer_from_keypress    : 1;
   guint from_menubar           : 1;
   guint use_action_appearance  : 1;
   guint reserve_indicator      : 1;
diff --git a/gtk/gtknotebook.c b/gtk/gtknotebook.c
index 77a8a8b..a7291a7 100644
--- a/gtk/gtknotebook.c
+++ b/gtk/gtknotebook.c
@@ -2750,8 +2750,7 @@ gtk_notebook_button_press (GtkWidget      *widget,
 
   if (priv->menu && gdk_event_triggers_context_menu ((GdkEvent *) event))
     {
-      gtk_menu_popup (GTK_MENU (priv->menu), NULL, NULL,
-                      NULL, NULL, 3, event->time);
+      gtk_menu_popup_at_pointer (GTK_MENU (priv->menu), (GdkEvent *) event);
       return TRUE;
     }
 
@@ -2794,58 +2793,50 @@ gtk_notebook_button_press (GtkWidget      *widget,
   return TRUE;
 }
 
-static void
-popup_position_func (GtkMenu  *menu,
-                     gint     *x,
-                     gint     *y,
-                     gboolean *push_in,
-                     gpointer  data)
-{
-  GtkNotebook *notebook = data;
-  GtkNotebookPrivate *priv = notebook->priv;
-  GtkAllocation allocation;
-  GtkWidget *w;
-  GtkRequisition requisition;
-
-  if (priv->focus_tab)
-    {
-      GtkNotebookPage *page;
-
-      page = priv->focus_tab->data;
-      w = page->tab_label;
-    }
-  else
-   {
-     w = GTK_WIDGET (notebook);
-   }
-
-  gdk_window_get_origin (gtk_widget_get_window (w), x, y);
-
-  gtk_widget_get_allocation (w, &allocation);
-  gtk_widget_get_preferred_size (GTK_WIDGET (menu),
-                                 &requisition, NULL);
-
-  if (gtk_widget_get_direction (w) == GTK_TEXT_DIR_RTL)
-    *x += allocation.x + allocation.width - requisition.width;
-  else
-    *x += allocation.x;
-
-  *y += allocation.y + allocation.height;
-
-  *push_in = FALSE;
-}
-
 static gboolean
 gtk_notebook_popup_menu (GtkWidget *widget)
 {
   GtkNotebook *notebook = GTK_NOTEBOOK (widget);
   GtkNotebookPrivate *priv = notebook->priv;
+  GtkNotebookPage *page;
+  GtkWidget *tab_label = NULL;
 
   if (priv->menu)
     {
-      gtk_menu_popup (GTK_MENU (priv->menu), NULL, NULL,
-                      popup_position_func, notebook,
-                      0, gtk_get_current_event_time ());
+      if (priv->focus_tab)
+        {
+          page = priv->focus_tab->data;
+          tab_label = page->tab_label;
+        }
+
+      if (tab_label)
+        {
+          g_object_set (priv->menu,
+                        "anchor-hints", GDK_ANCHOR_FLIP_Y |
+                                        GDK_ANCHOR_SLIDE |
+                                        GDK_ANCHOR_RESIZE,
+                        NULL);
+
+          gtk_menu_popup_at_widget (GTK_MENU (priv->menu),
+                                    tab_label,
+                                    GDK_GRAVITY_SOUTH_WEST,
+                                    GDK_GRAVITY_NORTH_WEST,
+                                    NULL);
+        }
+      else
+        {
+          g_object_set (priv->menu,
+                        "anchor-hints", GDK_ANCHOR_SLIDE |
+                                        GDK_ANCHOR_RESIZE,
+                        NULL);
+
+          gtk_menu_popup_at_widget (GTK_MENU (priv->menu),
+                                    widget,
+                                    GDK_GRAVITY_NORTH_WEST,
+                                    GDK_GRAVITY_NORTH_WEST,
+                                    NULL);
+        }
+
       gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->menu), FALSE);
       return TRUE;
     }
diff --git a/gtk/gtktextview.c b/gtk/gtktextview.c
index 22b3a59..50b30f4 100644
--- a/gtk/gtktextview.c
+++ b/gtk/gtktextview.c
@@ -9347,91 +9347,10 @@ popup_menu_detach (GtkWidget *attach_widget,
   GTK_TEXT_VIEW (attach_widget)->priv->popup_menu = NULL;
 }
 
-static void
-popup_position_func (GtkMenu   *menu,
-                     gint      *x,
-                     gint      *y,
-                     gboolean  *push_in,
-                     gpointer  user_data)
-{
-  GtkAllocation allocation;
-  GtkTextView *text_view;
-  GtkWidget *widget;
-  GdkRectangle cursor_rect;
-  GdkRectangle onscreen_rect;
-  gint root_x, root_y;
-  GtkTextIter iter;
-  GtkRequisition req;
-  GdkDisplay *display;
-  GdkMonitor *monitor;
-  GdkRectangle workarea;
-
-  text_view = GTK_TEXT_VIEW (user_data);
-  widget = GTK_WIDGET (text_view);
-
-  g_return_if_fail (gtk_widget_get_realized (widget));
-
-  display = gtk_widget_get_display (widget);
-
-  gdk_window_get_origin (gtk_widget_get_window (widget),
-                         &root_x, &root_y);
-
-  gtk_text_buffer_get_iter_at_mark (get_buffer (text_view),
-                                    &iter,
-                                    gtk_text_buffer_get_insert (get_buffer (text_view)));
-
-  gtk_text_view_get_iter_location (text_view,
-                                   &iter,
-                                   &cursor_rect);
-
-  gtk_text_view_get_visible_rect (text_view, &onscreen_rect);
-
-  gtk_widget_get_preferred_size (text_view->priv->popup_menu,
-                                 &req, NULL);
-
-  gtk_widget_get_allocation (widget, &allocation);
-
-  /* can't use rectangle_intersect since cursor rect can have 0 width */
-  if (cursor_rect.x >= onscreen_rect.x &&
-      cursor_rect.x < onscreen_rect.x + onscreen_rect.width &&
-      cursor_rect.y >= onscreen_rect.y &&
-      cursor_rect.y < onscreen_rect.y + onscreen_rect.height)
-    {
-      gtk_text_view_buffer_to_window_coords (text_view,
-                                             GTK_TEXT_WINDOW_WIDGET,
-                                             cursor_rect.x, cursor_rect.y,
-                                             &cursor_rect.x, &cursor_rect.y);
-
-      *x = root_x + cursor_rect.x + cursor_rect.width;
-      *y = root_y + cursor_rect.y + cursor_rect.height;
-    }
-  else
-    {
-      /* Just center the menu, since cursor is offscreen. */
-      *x = root_x + (allocation.width / 2 - req.width / 2);
-      *y = root_y + (allocation.height / 2 - req.height / 2);
-    }
-
-  /* Ensure sanity */
-  *x = CLAMP (*x, root_x, (root_x + allocation.width));
-  *y = CLAMP (*y, root_y, (root_y + allocation.height));
-
-  monitor = gdk_display_get_monitor_at_point (display, *x, *y);
-  gtk_menu_place_on_monitor (menu, monitor);
-  gdk_monitor_get_workarea (monitor, &workarea);
-
-  *x = CLAMP (*x, workarea.x, workarea.x + MAX (0, workarea.width - req.width));
-  *y = CLAMP (*y, workarea.y, workarea.y + MAX (0, workarea.height - req.height));
-
-  *push_in = FALSE;
-}
-
 typedef struct
 {
   GtkTextView *text_view;
-  guint button;
-  guint time;
-  GdkDevice *device;
+  GdkEvent *trigger_event;
 } PopupInfo;
 
 static gboolean
@@ -9475,6 +9394,9 @@ popup_targets_received (GtkClipboard     *clipboard,
       gboolean can_insert;
       GtkTextIter iter;
       GtkTextIter sel_start, sel_end;
+      GdkRectangle iter_location;
+      GdkRectangle visible_rect;
+      gboolean is_visible;
 
       clipboard_contains_text = gtk_selection_data_targets_include_text (data);
 
@@ -9532,19 +9454,48 @@ popup_targets_received (GtkClipboard     *clipboard,
       g_signal_emit (text_view, signals[POPULATE_POPUP],
                     0, priv->popup_menu);
 
-      if (info->device)
-       gtk_menu_popup_for_device (GTK_MENU (priv->popup_menu),
-                                   info->device, NULL, NULL, NULL, NULL, NULL,
-                                  info->button, info->time);
+      if (info->trigger_event && gdk_event_triggers_context_menu (info->trigger_event))
+        gtk_menu_popup_at_pointer (GTK_MENU (priv->popup_menu), info->trigger_event);
       else
-       {
-         gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
-                         popup_position_func, text_view,
-                         0, gtk_get_current_event_time ());
-         gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->popup_menu), FALSE);
-       }
+        {
+          gtk_text_view_get_iter_location (text_view, &iter, &iter_location);
+          gtk_text_view_get_visible_rect (text_view, &visible_rect);
+
+          is_visible = (iter_location.x + iter_location.width > visible_rect.x &&
+                        iter_location.x < visible_rect.x + visible_rect.width &&
+                        iter_location.y + iter_location.height > visible_rect.y &&
+                        iter_location.y < visible_rect.y + visible_rect.height);
+
+          if (is_visible)
+            {
+              gtk_text_view_buffer_to_window_coords (text_view,
+                                                     GTK_TEXT_WINDOW_WIDGET,
+                                                     iter_location.x,
+                                                     iter_location.y,
+                                                     &iter_location.x,
+                                                     &iter_location.y);
+
+              gtk_menu_popup_at_rect (GTK_MENU (priv->popup_menu),
+                                      gtk_widget_get_window (GTK_WIDGET (text_view)),
+                                      &iter_location,
+                                      GDK_GRAVITY_SOUTH_EAST,
+                                      GDK_GRAVITY_NORTH_WEST,
+                                      NULL);
+            }
+          else
+            gtk_menu_popup_at_widget (GTK_MENU (priv->popup_menu),
+                                      GTK_WIDGET (text_view),
+                                      GDK_GRAVITY_CENTER,
+                                      GDK_GRAVITY_CENTER,
+                                      NULL);
+
+          gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->popup_menu), FALSE);
+        }
     }
 
+  if (info->trigger_event)
+    gdk_event_free (info->trigger_event);
+
   g_object_unref (text_view);
   g_slice_free (PopupInfo, info);
 }
@@ -9560,19 +9511,7 @@ gtk_text_view_do_popup (GtkTextView    *text_view,
    * we get them, then we actually pop up the menu.
    */
   info->text_view = g_object_ref (text_view);
-  
-  if (event)
-    {
-      gdk_event_get_button (event, &info->button);
-      info->time = gdk_event_get_time (event);
-      info->device = gdk_event_get_device (event);
-    }
-  else
-    {
-      info->button = 0;
-      info->time = gtk_get_current_event_time ();
-      info->device = NULL;
-    }
+  info->trigger_event = event ? gdk_event_copy (event) : gtk_get_current_event ();
 
   gtk_clipboard_request_contents (gtk_widget_get_clipboard (GTK_WIDGET (text_view),
                                                            GDK_SELECTION_CLIPBOARD),
diff --git a/gtk/gtktoolbar.c b/gtk/gtktoolbar.c
index d77fef3..6924c03 100644
--- a/gtk/gtktoolbar.c
+++ b/gtk/gtktoolbar.c
@@ -2618,79 +2618,51 @@ gtk_toolbar_real_style_changed (GtkToolbar     *toolbar,
 }
 
 static void
-menu_position_func (GtkMenu  *menu,
-                   gint     *x,
-                   gint     *y,
-                   gboolean *push_in,
-                   gpointer  user_data)
+show_menu (GtkToolbar     *toolbar,
+          GdkEventButton *event)
 {
-  GtkAllocation allocation;
-  GtkToolbar *toolbar = GTK_TOOLBAR (user_data);
   GtkToolbarPrivate *priv = toolbar->priv;
-  GtkRequisition req;
-  GtkRequisition menu_req;
-  GdkRectangle workarea;
-  GdkMonitor *monitor;
-  GdkDisplay *display;
+  GtkRequisition minimum_size;
 
-  gtk_widget_get_preferred_size (priv->arrow_button,
-                                 &req, NULL);
-  gtk_widget_get_preferred_size (GTK_WIDGET (menu),
-                                 &menu_req, NULL);
-
-  display = gtk_widget_get_display (GTK_WIDGET (menu));
-  monitor = gdk_display_get_monitor_at_window (display,
-                                               gtk_widget_get_window (priv->arrow_button));
-  gdk_monitor_get_workarea (monitor, &workarea);
+  rebuild_menu (toolbar);
 
-  gtk_widget_get_allocation (priv->arrow_button, &allocation);
+  gtk_widget_show_all (GTK_WIDGET (priv->menu));
 
-  gdk_window_get_origin (gtk_button_get_event_window (GTK_BUTTON (priv->arrow_button)), x, y);
-  if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
-    {
-      if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_LTR)
-       *x += allocation.width - req.width;
-      else
-       *x += req.width - menu_req.width;
-
-      if ((*y + allocation.height + menu_req.height) <= workarea.y + workarea.height)
-       *y += allocation.height;
-      else if ((*y - menu_req.height) >= workarea.y)
-       *y -= menu_req.height;
-      else if (workarea.y + workarea.height - (*y + allocation.height) > *y)
-       *y += allocation.height;
-      else
-       *y -= menu_req.height;
-    }
-  else
+  switch (priv->orientation)
     {
-      if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_LTR)
-       *x += allocation.width;
-      else
-       *x -= menu_req.width;
+    case GTK_ORIENTATION_HORIZONTAL:
+      gtk_widget_get_preferred_size (priv->arrow_button, &minimum_size, NULL);
 
-      if (*y + menu_req.height > workarea.y + workarea.height &&
-         *y + allocation.height - workarea.y > workarea.y + workarea.height - *y)
-       *y += allocation.height - menu_req.height;
-    }
+      g_object_set (priv->menu,
+                    "anchor-hints", GDK_ANCHOR_FLIP_Y |
+                                    GDK_ANCHOR_SLIDE |
+                                    GDK_ANCHOR_RESIZE,
+                    "rect-anchor-dx", -minimum_size.width,
+                    NULL);
 
-  *push_in = FALSE;
-}
+      gtk_menu_popup_at_widget (priv->menu,
+                                priv->arrow_button,
+                                GDK_GRAVITY_SOUTH_EAST,
+                                GDK_GRAVITY_NORTH_WEST,
+                                (GdkEvent *) event);
 
-static void
-show_menu (GtkToolbar     *toolbar,
-          GdkEventButton *event)
-{
-  GtkToolbarPrivate *priv = toolbar->priv;
+      break;
 
-  rebuild_menu (toolbar);
+    case GTK_ORIENTATION_VERTICAL:
+      g_object_set (priv->menu,
+                    "anchor-hints", GDK_ANCHOR_FLIP_X |
+                                    GDK_ANCHOR_SLIDE |
+                                    GDK_ANCHOR_RESIZE,
+                    NULL);
 
-  gtk_widget_show_all (GTK_WIDGET (priv->menu));
+      gtk_menu_popup_at_widget (priv->menu,
+                                priv->arrow_button,
+                                GDK_GRAVITY_NORTH_EAST,
+                                GDK_GRAVITY_NORTH_WEST,
+                                (GdkEvent *) event);
 
-  gtk_menu_popup (priv->menu, NULL, NULL,
-                 menu_position_func, toolbar,
-                 event? event->button : 0,
-                 event? event->time : gtk_get_current_event_time());
+      break;
+    }
 }
 
 static void
diff --git a/gtk/gtkwindow.c b/gtk/gtkwindow.c
index 95beb5d..008c197 100644
--- a/gtk/gtkwindow.c
+++ b/gtk/gtkwindow.c
@@ -8825,15 +8825,6 @@ popup_menu_detach (GtkWidget *widget,
   GTK_WINDOW (widget)->priv->popup_menu = NULL;
 }
 
-static void
-popup_position_func (GtkMenu   *menu,
-                     gint      *x,
-                     gint      *y,
-                     gboolean  *push_in,
-                     gpointer   user_data)
-{
-}
-
 static GdkWindowState
 gtk_window_get_state (GtkWindow *window)
 {
@@ -9046,17 +9037,7 @@ gtk_window_do_popup_fallback (GtkWindow      *window,
   g_signal_connect (G_OBJECT (menuitem), "activate",
                     G_CALLBACK (close_window_clicked), window);
   gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menuitem);
-
-  if (event)
-    gtk_menu_popup (GTK_MENU (priv->popup_menu),
-                    NULL, NULL,
-                    NULL, NULL,
-                    event->button, event->time);
-  else
-    gtk_menu_popup (GTK_MENU (priv->popup_menu),
-                    NULL, NULL,
-                    popup_position_func, window,
-                    0, gtk_get_current_event_time ());
+  gtk_menu_popup_at_pointer (GTK_MENU (priv->popup_menu), (GdkEvent *) event);
 }
 
 static void



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]