[libpeas] libpeas-gtk: fix gtk_menu_popup() deprecations



commit 2656794bb7133cef0fbb66c9da3416926f4c43b2
Author: Christian Hergert <chergert redhat com>
Date:   Tue Aug 6 17:08:57 2019 -0700

    libpeas-gtk: fix gtk_menu_popup() deprecations
    
    As libpeas-gtk only requires GTK 3.0, this attempts to fix the menu
    positioning code in a backwards compatible way (at compile time). If we
    have at least GTK 3.22, we can use the new popup APIs which can vastly
    simplify this code and also fix menus displayed at monitor boundaries
    on wayland.
    
    The existing code was left mostly untouched to reduce the chances of
    breaking things on older systems and also make it convenient to delete
    once libpeas moves into the GTK 4.0 territory if/when that happens.

 libpeas-gtk/peas-gtk-plugin-manager-view.c | 49 ++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
---
diff --git a/libpeas-gtk/peas-gtk-plugin-manager-view.c b/libpeas-gtk/peas-gtk-plugin-manager-view.c
index ef6d3d4..5d3059a 100644
--- a/libpeas-gtk/peas-gtk-plugin-manager-view.c
+++ b/libpeas-gtk/peas-gtk-plugin-manager-view.c
@@ -383,6 +383,35 @@ popup_menu_detach (PeasGtkPluginManagerView *view,
   priv->popup_menu = NULL;
 }
 
+static void
+get_selected_area (GtkTreeView  *tree_view,
+                   GdkRectangle *area)
+{
+  GtkTreeSelection *selection;
+  GtkTreeIter iter;
+
+  selection = gtk_tree_view_get_selection (tree_view);
+
+  if (gtk_tree_selection_get_selected (selection, NULL, &iter))
+    {
+      GtkTreeModel *model;
+      GtkTreePath *path;
+
+      model = gtk_tree_view_get_model (tree_view);
+      path = gtk_tree_model_get_path (model, &iter);
+      gtk_tree_view_get_cell_area (tree_view,
+                                   path,
+                                   gtk_tree_view_get_column (tree_view, 0),
+                                   area);
+      gtk_tree_path_free (path);
+    }
+  else
+    {
+      gtk_widget_get_allocation (GTK_WIDGET (tree_view), area);
+    }
+}
+
+#if !GTK_CHECK_VERSION(3, 22, 0)
 static void
 menu_position_under_tree_view (GtkMenu     *menu,
                                gint        *x,
@@ -446,6 +475,7 @@ menu_position_under_tree_view (GtkMenu     *menu,
 
   *push_in = TRUE;
 }
+#endif
 
 static gboolean
 show_popup_menu (GtkTreeView              *tree_view,
@@ -468,14 +498,33 @@ show_popup_menu (GtkTreeView              *tree_view,
 
   if (event != NULL)
     {
+#if GTK_CHECK_VERSION(3, 22, 0)
+      gtk_menu_popup_at_pointer (GTK_MENU (priv->popup_menu),
+                                (const GdkEvent *)event);
+#else
       gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
                       NULL, NULL, event->button, event->time);
+#endif
     }
   else
     {
+#if GTK_CHECK_VERSION(3, 22, 0)
+      GdkRectangle cell_area;
+
+      get_selected_area (GTK_TREE_VIEW (view), &cell_area);
+
+      gtk_menu_popup_at_rect (GTK_MENU (priv->popup_menu),
+                              gtk_widget_get_window (GTK_WIDGET (view)),
+                              &cell_area,
+                              GDK_GRAVITY_SOUTH_WEST,
+                              GDK_GRAVITY_NORTH_WEST,
+                              (const GdkEvent *)event);
+
+#else
       gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
                       (GtkMenuPositionFunc) menu_position_under_tree_view,
                       view, 0, gtk_get_current_event_time ());
+#endif
 
       gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->popup_menu),
                                    FALSE);


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