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



commit d15cc8fe40f5e4a1d9bc706e881c8914b7ddc013
Author: William Hua <william hua canonical com>
Date:   Wed Jan 6 12:00:00 2016 -0500

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

 gtk/gtktoolbar.c |  114 +++++++++++++++++++++++++-----------------------------
 1 files changed, 53 insertions(+), 61 deletions(-)
---
diff --git a/gtk/gtktoolbar.c b/gtk/gtktoolbar.c
index c185216..9a96449 100644
--- a/gtk/gtktoolbar.c
+++ b/gtk/gtktoolbar.c
@@ -2618,81 +2618,73 @@ 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);
+
+  gtk_widget_show_all (GTK_WIDGET (priv->menu));
 
-  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);
+  params = gtk_menu_create_params (priv->menu);
 
   gtk_widget_get_allocation (priv->arrow_button, &allocation);
+  gdk_window_get_origin (gtk_button_get_event_window (GTK_BUTTON (priv->arrow_button)), &allocation.x, 
&allocation.y);
 
-  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_VERTICAL:
+      type_hint = GDK_WINDOW_TYPE_HINT_POPUP_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;
-    }
+      gdk_attach_params_add_primary_rules (params,
+                                           GDK_ATTACH_AXIS_X | GDK_ATTACH_RECT_MAX | GDK_ATTACH_WINDOW_MIN,
+                                           GDK_ATTACH_AXIS_X | GDK_ATTACH_RECT_MIN | GDK_ATTACH_WINDOW_MAX,
+                                           NULL);
 
-  *push_in = FALSE;
-}
+      gdk_attach_params_add_secondary_rules (params,
+                                             GDK_ATTACH_AXIS_Y | GDK_ATTACH_RECT_MIN | GDK_ATTACH_WINDOW_MIN,
+                                             GDK_ATTACH_AXIS_Y | GDK_ATTACH_RECT_MAX | GDK_ATTACH_WINDOW_MAX,
+                                             NULL);
 
-static void
-show_menu (GtkToolbar     *toolbar,
-          GdkEventButton *event)
-{
-  GtkToolbarPrivate *priv = toolbar->priv;
+      break;
 
-  rebuild_menu (toolbar);
+    default:
+      type_hint = GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU;
 
-  gtk_widget_show_all (GTK_WIDGET (priv->menu));
+      gtk_widget_get_preferred_size (priv->arrow_button, &minimum_size, NULL);
+
+      allocation.x += minimum_size.width;
+      allocation.width -= 2 * minimum_size.width;
+
+      gdk_attach_params_add_primary_rules (params,
+                                           GDK_ATTACH_AXIS_Y | GDK_ATTACH_RECT_MAX | GDK_ATTACH_WINDOW_MIN,
+                                           GDK_ATTACH_AXIS_Y | GDK_ATTACH_RECT_MIN | GDK_ATTACH_WINDOW_MAX,
+                                           NULL);
+
+      gdk_attach_params_add_secondary_rules (params,
+                                             GDK_ATTACH_AXIS_X | GDK_ATTACH_RECT_MAX | GDK_ATTACH_WINDOW_MIN,
+                                             GDK_ATTACH_AXIS_X | GDK_ATTACH_RECT_MIN | GDK_ATTACH_WINDOW_MAX,
+                                             NULL);
+
+      break;
+    }
+
+  gdk_attach_params_set_attach_rect (params, &allocation);
 
-  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]