[gnome-panel/wip-geiger-popup-menu] libgnome-panel: add popup menu helper function



commit e6760df2afcad7fede8835b5c82eab049c917b7f
Author: Sebastian Geiger <sbastig gmx net>
Date:   Sun Apr 5 18:07:07 2020 +0200

    libgnome-panel: add popup menu helper function
    
    When applets want to popup a menu for a widget they need to know the
    current position of the applet. Then they must compute the widget and
    menu anchors that are needed by gtk_menu_popup_at_widget() to align
    the widget and the menu.
    
    To avoid that every applet needs to perform these steps we introduce
    a helper function that automatically computes the anchor parameters
    from the applet position and pops up the menu for the widget.

 libgnome-panel/gp-applet.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++
 libgnome-panel/gp-applet.h |  5 +++++
 2 files changed, 57 insertions(+)
---
diff --git a/libgnome-panel/gp-applet.c b/libgnome-panel/gp-applet.c
index b60b6d956..3100c9cac 100644
--- a/libgnome-panel/gp-applet.c
+++ b/libgnome-panel/gp-applet.c
@@ -1439,3 +1439,55 @@ gp_applet_show_help (GpApplet   *applet,
 
   gp_module_show_help (priv->module, NULL, priv->id, page);
 }
+
+/**
+ * gp_applet_popup_menu_for_widget:
+ * @applet: a #GpApplet
+ * @menu: the #GtkMenu to pop up.
+ * @widget: the #GtkWidget to align menu with.
+ * @event: the #GdkEvent that initiated this request or NULL if it's the current event.
+ *
+ * Displays menu and makes it available for selection. This is convenience function
+ * around gtk_menu_popup_at_widget() that automatically computes the widget_anchor and
+ * menu_anchor parameters based on the current applet position.
+ */
+void
+gp_applet_popup_menu_for_widget (GpApplet           *applet,
+                                 GtkMenu            *menu,
+                                 GtkWidget          *widget,
+                                 GdkEvent           *event)
+{
+  GdkGravity widget_anchor;
+  GdkGravity menu_anchor;
+
+  switch (gp_applet_get_position (GP_APPLET (applet)))
+    {
+      case GTK_POS_TOP:
+        widget_anchor = GDK_GRAVITY_SOUTH_WEST;
+        menu_anchor = GDK_GRAVITY_NORTH_WEST;
+        break;
+
+      case GTK_POS_LEFT:
+        widget_anchor = GDK_GRAVITY_NORTH_EAST;
+        menu_anchor = GDK_GRAVITY_NORTH_WEST;
+        break;
+
+      case GTK_POS_RIGHT:
+        widget_anchor = GDK_GRAVITY_NORTH_WEST;
+        menu_anchor = GDK_GRAVITY_NORTH_EAST;
+        break;
+
+      case GTK_POS_BOTTOM:
+        widget_anchor = GDK_GRAVITY_NORTH_WEST;
+        menu_anchor = GDK_GRAVITY_SOUTH_WEST;
+        break;
+
+      default:
+        g_assert_not_reached ();
+        break;
+    }
+
+  gtk_menu_popup_at_widget (menu, GTK_WIDGET (widget),
+                            widget_anchor, menu_anchor,
+                            event);
+}
diff --git a/libgnome-panel/gp-applet.h b/libgnome-panel/gp-applet.h
index 7ca35ca0b..dd25753d1 100644
--- a/libgnome-panel/gp-applet.h
+++ b/libgnome-panel/gp-applet.h
@@ -126,6 +126,11 @@ void             gp_applet_show_about                (GpApplet           *applet
 void             gp_applet_show_help                 (GpApplet           *applet,
                                                       const char         *page);
 
+void             gp_applet_popup_menu_for_widget     (GpApplet           *applet,
+                                                      GtkMenu            *menu,
+                                                      GtkWidget          *widget,
+                                                      GdkEvent           *event);
+
 G_END_DECLS
 
 #endif


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