[gtk+/wip/attach-params: 15/28] gtkmenubutton: use gtk_menu_popup_with_params ()



commit 96da4c6a19c0f50a0f5898fac017ed37f2bcf7a7
Author: William Hua <william hua canonical com>
Date:   Tue Feb 2 09:00:00 2016 -0500

    gtkmenubutton: use gtk_menu_popup_with_params ()
    
    https://bugzilla.gnome.org/show_bug.cgi?id=756579

 gtk/gtkmenubutton.c |  289 ++++++++++++++++++++++----------------------------
 1 files changed, 127 insertions(+), 162 deletions(-)
---
diff --git a/gtk/gtkmenubutton.c b/gtk/gtkmenubutton.c
index 79aece0..7e146d5 100644
--- a/gtk/gtkmenubutton.c
+++ b/gtk/gtkmenubutton.c
@@ -241,153 +241,16 @@ gtk_menu_button_state_flags_changed (GtkWidget    *widget,
 }
 
 static void
-menu_position_up_down_func (GtkMenu       *menu,
-                            gint          *x,
-                            gint          *y,
-                            gboolean      *push_in,
-                            GtkMenuButton *menu_button)
-{
-  GtkMenuButtonPrivate *priv = menu_button->priv;
-  GtkWidget *widget = GTK_WIDGET (menu_button);
-  GtkWidget *toplevel;
-  GtkTextDirection direction;
-  GdkRectangle monitor;
-  gint monitor_num;
-  GdkScreen *screen;
-  GdkWindow *window;
-  GtkAllocation menu_allocation, allocation, arrow_allocation;
-  GtkAlign align;
-
-  /* In the common case the menu button is showing a dropdown menu, set the
-   * corresponding type hint on the toplevel, so the WM can omit the top side
-   * of the shadows.
-   */
-  if (priv->arrow_type == GTK_ARROW_DOWN)
-    {
-      toplevel = gtk_widget_get_toplevel (priv->menu);
-      gtk_window_set_type_hint (GTK_WINDOW (toplevel), GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU);
-    }
-
-  align = gtk_widget_get_halign (priv->menu);
-  direction = gtk_widget_get_direction (widget);
-  window = gtk_widget_get_window (priv->align_widget ? priv->align_widget : widget);
-
-  screen = gtk_widget_get_screen (GTK_WIDGET (menu));
-  monitor_num = gdk_screen_get_monitor_at_window (screen, window);
-  if (monitor_num < 0)
-    monitor_num = 0;
-  gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
-
-  gtk_widget_get_allocation (priv->align_widget ? priv->align_widget : widget, &allocation);
-  gtk_widget_get_allocation (widget, &arrow_allocation);
-  gtk_widget_get_allocation (priv->menu, &menu_allocation);
-
-  gdk_window_get_origin (window, x, y);
-  *x += allocation.x;
-  *y += allocation.y;
-
-  /* treat the default align value like START */
-  if (align == GTK_ALIGN_FILL)
-    align = GTK_ALIGN_START;
-
-  if (align == GTK_ALIGN_CENTER)
-    *x -= (menu_allocation.width - allocation.width) / 2;
-  else if ((align == GTK_ALIGN_START && direction == GTK_TEXT_DIR_LTR) ||
-           (align == GTK_ALIGN_END && direction == GTK_TEXT_DIR_RTL))
-    *x += MAX (allocation.width - menu_allocation.width, 0);
-  else if (menu_allocation.width > allocation.width)
-    *x -= menu_allocation.width - allocation.width;
-
-  if (priv->arrow_type == GTK_ARROW_UP && *y - menu_allocation.height >= monitor.y)
-    {
-      *y -= menu_allocation.height;
-    }
-  else
-    {
-      if ((*y + arrow_allocation.height + menu_allocation.height) <= monitor.y + monitor.height)
-        *y += arrow_allocation.height;
-      else if ((*y - menu_allocation.height) >= monitor.y)
-        *y -= menu_allocation.height;
-      else if (monitor.y + monitor.height - (*y + arrow_allocation.height) > *y)
-        *y += arrow_allocation.height;
-      else
-        *y -= menu_allocation.height;
-    }
-
-  *push_in = FALSE;
-}
-
-static void
-menu_position_side_func (GtkMenu       *menu,
-                         gint          *x,
-                         gint          *y,
-                         gboolean      *push_in,
-                         GtkMenuButton *menu_button)
-{
-  GtkMenuButtonPrivate *priv = menu_button->priv;
-  GtkAllocation allocation;
-  GtkAllocation menu_allocation;
-  GtkWidget *widget = GTK_WIDGET (menu_button);
-  GdkRectangle monitor;
-  gint monitor_num;
-  GdkScreen *screen;
-  GdkWindow *window;
-  GtkAlign align;
-  GtkTextDirection direction;
-
-  window = gtk_widget_get_window (widget);
-
-  direction = gtk_widget_get_direction (widget);
-  align = gtk_widget_get_valign (GTK_WIDGET (menu));
-  screen = gtk_widget_get_screen (GTK_WIDGET (menu));
-  monitor_num = gdk_screen_get_monitor_at_window (screen, window);
-  if (monitor_num < 0)
-    monitor_num = 0;
-  gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
-
-  gdk_window_get_origin (gtk_button_get_event_window (GTK_BUTTON (menu_button)), x, y);
-
-  gtk_widget_get_allocation (widget, &allocation);
-  gtk_widget_get_allocation (priv->menu, &menu_allocation);
-
-  if ((priv->arrow_type == GTK_ARROW_RIGHT && direction == GTK_TEXT_DIR_LTR) ||
-      (priv->arrow_type == GTK_ARROW_LEFT && direction == GTK_TEXT_DIR_RTL))
-
-    {
-      if (*x + allocation.width + menu_allocation.width <= monitor.x + monitor.width)
-        *x += allocation.width;
-      else
-        *x -= menu_allocation.width;
-    }
-  else
-    {
-      if (*x - menu_allocation.width >= monitor.x)
-        *x -= menu_allocation.width;
-      else
-        *x += allocation.width;
-    }
-
-  /* treat the default align value like START */
-  if (align == GTK_ALIGN_FILL)
-    align = GTK_ALIGN_START;
-
-  if (align == GTK_ALIGN_CENTER)
-    *y -= (menu_allocation.height - allocation.height) / 2;
-  else if (align == GTK_ALIGN_END)
-    *y -= menu_allocation.height - allocation.height;
-
-  *push_in = FALSE;
-}
-
-static void
 popup_menu (GtkMenuButton *menu_button,
             GdkEvent      *event)
 {
   GtkMenuButtonPrivate *priv = menu_button->priv;
-  GtkMenuPositionFunc func;
-  GdkDevice *device;
+  GdkSeat *seat;
   guint button;
   guint32 time;
+  GdkWindowTypeHint type_hint;
+  GdkAttachParams *params;
+  GtkWidget *attach_widget;
 
   if (priv->func)
     priv->func (priv->user_data);
@@ -395,39 +258,141 @@ popup_menu (GtkMenuButton *menu_button,
   if (!priv->menu)
     return;
 
-  switch (priv->arrow_type)
-    {
-      case GTK_ARROW_LEFT:
-      case GTK_ARROW_RIGHT:
-        func = (GtkMenuPositionFunc) menu_position_side_func;
-        break;
-      default:
-        func = (GtkMenuPositionFunc) menu_position_up_down_func;
-        break;
-  }
-
   if (event != NULL &&
       gdk_event_get_screen (event) == gtk_widget_get_screen (GTK_WIDGET (menu_button)))
     {
-      device = gdk_event_get_device (event);
+      seat = gdk_event_get_seat (event);
       gdk_event_get_button (event, &button);
       time = gdk_event_get_time (event);
     }
   else
     {
-      device = NULL;
+      seat = NULL;
       button = 0;
       time = gtk_get_current_event_time ();
     }
 
-  gtk_menu_popup_for_device (GTK_MENU (priv->menu),
-                             device,
-                             NULL, NULL,
-                             func,
-                             GTK_WIDGET (menu_button),
-                             NULL,
-                             button,
-                             time);
+  params = gdk_attach_params_new ();
+  attach_widget = GTK_WIDGET (menu_button);
+
+  /* GTK_ALIGN_FILL and GTK_ALIGN_BASELINE are supposed to behave as
+   * GTK_ALIGN_CENTER, but for backwards compatibility, we treat them as
+   * GTK_ALIGN_START.
+   */
+
+  switch (priv->arrow_type)
+    {
+    case GTK_ARROW_UP:
+    case GTK_ARROW_DOWN:
+    case GTK_ARROW_NONE:
+      if (priv->align_widget)
+        attach_widget = priv->align_widget;
+
+      /* In the common case the menu button is showing a dropdown menu, set the
+       * corresponding type hint on the toplevel, so the WM can omit the top side
+       * of the shadows.
+       */
+
+      type_hint = GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU;
+
+      if (priv->arrow_type == GTK_ARROW_UP)
+        {
+          switch (gtk_widget_get_halign (priv->menu))
+            {
+            case GTK_ALIGN_FILL:
+            case GTK_ALIGN_START:
+            case GTK_ALIGN_BASELINE:
+              gdk_attach_params_set_anchors (params, GDK_ATTACH_TOP_LEFT, GDK_ATTACH_BOTTOM_LEFT);
+              break;
+
+            case GTK_ALIGN_END:
+              gdk_attach_params_set_anchors (params, GDK_ATTACH_TOP_RIGHT, GDK_ATTACH_BOTTOM_RIGHT);
+              break;
+
+            case GTK_ALIGN_CENTER:
+              gdk_attach_params_set_anchors (params, GDK_ATTACH_TOP, GDK_ATTACH_BOTTOM);
+              break;
+            }
+        }
+      else
+        {
+          switch (gtk_widget_get_halign (priv->menu))
+            {
+            case GTK_ALIGN_FILL:
+            case GTK_ALIGN_START:
+            case GTK_ALIGN_BASELINE:
+              gdk_attach_params_set_anchors (params, GDK_ATTACH_BOTTOM_LEFT, GDK_ATTACH_TOP_LEFT);
+              break;
+
+            case GTK_ALIGN_END:
+              gdk_attach_params_set_anchors (params, GDK_ATTACH_BOTTOM_RIGHT, GDK_ATTACH_TOP_RIGHT);
+              break;
+
+            case GTK_ALIGN_CENTER:
+              gdk_attach_params_set_anchors (params, GDK_ATTACH_BOTTOM, GDK_ATTACH_TOP);
+              break;
+            }
+        }
+
+      gdk_attach_params_set_attach_hints (params, GDK_ATTACH_FLIP_TOP_BOTTOM);
+      break;
+
+    case GTK_ARROW_LEFT:
+    case GTK_ARROW_RIGHT:
+      type_hint = GDK_WINDOW_TYPE_HINT_POPUP_MENU;
+
+      if (priv->arrow_type == GTK_ARROW_LEFT)
+        {
+          switch (gtk_widget_get_valign (priv->menu))
+            {
+            case GTK_ALIGN_FILL:
+            case GTK_ALIGN_START:
+            case GTK_ALIGN_BASELINE:
+              gdk_attach_params_set_anchors (params, GDK_ATTACH_TOP_LEFT, GDK_ATTACH_TOP_RIGHT);
+              break;
+
+            case GTK_ALIGN_END:
+              gdk_attach_params_set_anchors (params, GDK_ATTACH_BOTTOM_LEFT, GDK_ATTACH_BOTTOM_RIGHT);
+              break;
+
+            case GTK_ALIGN_CENTER:
+              gdk_attach_params_set_anchors (params, GDK_ATTACH_LEFT, GDK_ATTACH_RIGHT);
+              break;
+            }
+        }
+      else
+        {
+          switch (gtk_widget_get_valign (priv->menu))
+            {
+            case GTK_ALIGN_FILL:
+            case GTK_ALIGN_START:
+            case GTK_ALIGN_BASELINE:
+              gdk_attach_params_set_anchors (params, GDK_ATTACH_TOP_RIGHT, GDK_ATTACH_TOP_LEFT);
+              break;
+
+            case GTK_ALIGN_END:
+              gdk_attach_params_set_anchors (params, GDK_ATTACH_BOTTOM_RIGHT, GDK_ATTACH_BOTTOM_LEFT);
+              break;
+
+            case GTK_ALIGN_CENTER:
+              gdk_attach_params_set_anchors (params, GDK_ATTACH_RIGHT, GDK_ATTACH_LEFT);
+              break;
+            }
+        }
+
+      gdk_attach_params_set_attach_hints (params, GDK_ATTACH_FLIP_LEFT_RIGHT);
+      break;
+    }
+
+  gtk_menu_popup_with_params (GTK_MENU (priv->menu),
+                              seat,
+                              NULL,
+                              attach_widget,
+                              button,
+                              time,
+                              TRUE,
+                              type_hint,
+                              params);
 }
 
 static void


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