[gtk+/wip/attachment-parameters-3: 4/16] gtktoolbar: use gtk_menu_popup_with_params ()



commit afe1dc84fea51af3058a24cb4e4213b4fcd2fe6a
Author: William Hua <william hua canonical com>
Date:   Mon Sep 21 23:13:18 2015 -0400

    gtktoolbar: use gtk_menu_popup_with_params ()

 gtk/gtktoolbar.c |  113 +++++++++++++++++++++++++-----------------------------
 1 files changed, 52 insertions(+), 61 deletions(-)
---
diff --git a/gtk/gtktoolbar.c b/gtk/gtktoolbar.c
index 4c8674f..eaad0e3 100644
--- a/gtk/gtktoolbar.c
+++ b/gtk/gtktoolbar.c
@@ -2572,81 +2572,72 @@ 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;
+  gboolean is_right_to_left;
 
-  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);
+
+  is_right_to_left = gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_RTL;
+  gdk_attach_params_set_right_to_left (params, is_right_to_left);
 
   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)
+  switch (priv->orientation)
     {
-      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 
-    {
-      if (gtk_widget_get_direction (GTK_WIDGET (toolbar)) == GTK_TEXT_DIR_LTR) 
-       *x += allocation.width;
-      else 
-       *x -= menu_req.width;
+    case GTK_ORIENTATION_VERTICAL:
+      gdk_attach_params_add_primary_rules (params,
+                                           GDK_ATTACH_AXIS_X | GDK_ATTACH_RECT_MAX | GDK_ATTACH_WINDOW_MIN | 
GDK_ATTACH_FLIP_IF_RTL,
+                                           GDK_ATTACH_AXIS_X | GDK_ATTACH_RECT_MIN | GDK_ATTACH_WINDOW_MAX | 
GDK_ATTACH_FLIP_IF_RTL,
+                                           NULL);
 
-      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_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);
 
-  *push_in = FALSE;
-}
+      break;
 
-static void
-show_menu (GtkToolbar     *toolbar,
-          GdkEventButton *event)
-{
-  GtkToolbarPrivate *priv = toolbar->priv;
+    default:
+      gtk_widget_get_preferred_size (priv->arrow_button, &minimum_size, NULL);
 
-  rebuild_menu (toolbar);
+      allocation.x += minimum_size.width;
+      allocation.width -= 2 * minimum_size.width;
 
-  gtk_widget_show_all (GTK_WIDGET (priv->menu));
+      gdk_attach_params_set_window_type_hint (params, GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU);
+
+      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_FLIP_IF_RTL,
+                                             GDK_ATTACH_AXIS_X | GDK_ATTACH_RECT_MIN | GDK_ATTACH_WINDOW_MAX 
| GDK_ATTACH_FLIP_IF_RTL,
+                                             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 (),
+                              params);
 }
 
 static void


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