[gtk+/wip/attach-params: 8/20] gtktoolbar: use gtk_menu_popup_with_params ()



commit c8180200047f48bcaaabd71c4285059022a46817
Author: William Hua <william hua canonical com>
Date:   Wed Feb 3 10:20:30 2016 -0500

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

 gtk/gtktoolbar.c |   98 ++++++++++++++++++++---------------------------------
 1 files changed, 37 insertions(+), 61 deletions(-)
---
diff --git a/gtk/gtktoolbar.c b/gtk/gtktoolbar.c
index a3c7778..f633b9f 100644
--- a/gtk/gtktoolbar.c
+++ b/gtk/gtktoolbar.c
@@ -2618,81 +2618,57 @@ 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 monitor;
-  gint monitor_num;
-  GdkScreen *screen;
+  GdkAttachParams *params;
+  GtkRequisition minimum_size;
+  GtkAllocation allocation;
+  GdkWindowTypeHint type_hint;
 
-  gtk_widget_get_preferred_size (priv->arrow_button,
-                                 &req, NULL);
-  gtk_widget_get_preferred_size (GTK_WIDGET (menu),
-                                 &menu_req, NULL);
+  rebuild_menu (toolbar);
 
-  screen = gtk_widget_get_screen (GTK_WIDGET (menu));
-  monitor_num = gdk_screen_get_monitor_at_window (screen,
-                                                  gtk_widget_get_window (priv->arrow_button));
-  if (monitor_num < 0)
-    monitor_num = 0;
-  gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
+  gtk_widget_show_all (GTK_WIDGET (priv->menu));
 
+  params = gdk_attach_params_new ();
+
+  gtk_widget_realize (priv->arrow_button);
   gtk_widget_get_allocation (priv->arrow_button, &allocation);
 
-  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) <= monitor.y + monitor.height)
-       *y += allocation.height;
-      else if ((*y - menu_req.height) >= monitor.y)
-       *y -= menu_req.height;
-      else if (monitor.y + monitor.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:
+      type_hint = GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU;
 
-      if (*y + menu_req.height > monitor.y + monitor.height &&
-         *y + allocation.height - monitor.y > monitor.y + monitor.height - *y)
-       *y += allocation.height - menu_req.height;
-    }
+      gtk_widget_get_preferred_size (priv->arrow_button, &minimum_size, NULL);
 
-  *push_in = FALSE;
-}
+      allocation.x += minimum_size.width;
+      allocation.width -= 2 * minimum_size.width;
 
-static void
-show_menu (GtkToolbar     *toolbar,
-          GdkEventButton *event)
-{
-  GtkToolbarPrivate *priv = toolbar->priv;
+      gdk_attach_params_set_anchors (params, GDK_ATTACH_BOTTOM_RIGHT, GDK_ATTACH_TOP_LEFT);
+      gdk_attach_params_set_attach_hints (params, GDK_ATTACH_FLIP_TOP_BOTTOM);
+      break;
 
-  rebuild_menu (toolbar);
+    case GTK_ORIENTATION_VERTICAL:
+      type_hint = GDK_WINDOW_TYPE_HINT_POPUP_MENU;
 
-  gtk_widget_show_all (GTK_WIDGET (priv->menu));
+      gdk_attach_params_set_anchors (params, GDK_ATTACH_TOP_RIGHT, GDK_ATTACH_TOP_LEFT);
+      gdk_attach_params_set_attach_hints (params, GDK_ATTACH_FLIP_LEFT_RIGHT);
+      break;
+    }
+
+  gdk_attach_params_set_attach_rect (params, &allocation, gtk_widget_get_window (priv->arrow_button));
 
-  gtk_menu_popup (priv->menu, NULL, NULL,
-                 menu_position_func, toolbar,
-                 event? event->button : 0,
-                 event? event->time : gtk_get_current_event_time());
+  gtk_menu_popup_with_params (priv->menu,
+                              NULL,
+                              NULL,
+                              NULL,
+                              event ? event->button : 0,
+                              event ? event->time : gtk_get_current_event_time (),
+                              TRUE,
+                              type_hint,
+                              params);
 }
 
 static void


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