[planner: 26/61] resource-view: Port popup menu to existing GtkUIManager




commit a43ca479ed69321f33a14d5dda5ee11c92030a5f
Author: Mart Raudsepp <leio gentoo org>
Date:   Sat Dec 26 01:55:46 2020 +0200

    resource-view: Port popup menu to existing GtkUIManager
    
    resource-view already makes use of the global GtkUIManager, so port
    the GTK2 deprecated GtkItemFactory to that as well, making use of
    already existing GtkActions.
    Compared to before, now the icons and menu item names match with what
    is used elsewhere (e.g. in the menubar Actions menu) - all icons
    automatically change to match and "Edit resource..." changes to
    "Edit resource properties...".

 data/ui/resource-view.ui    |   6 ++
 src/planner-resource-view.c | 139 ++++++--------------------------------------
 2 files changed, 23 insertions(+), 122 deletions(-)
---
diff --git a/data/ui/resource-view.ui b/data/ui/resource-view.ui
index 8fbf74ef..c3578359 100644
--- a/data/ui/resource-view.ui
+++ b/data/ui/resource-view.ui
@@ -32,5 +32,11 @@
     <toolitem action="RemoveResource"/>
     <toolitem action="EditGroups"/>
   </toolbar>
+
+  <popup name="ResourceViewPopup">
+    <menuitem action="InsertResource"/>
+    <menuitem action="RemoveResource"/>
+    <menuitem action="EditResource"/>
+  </popup>
 </ui>
 
diff --git a/src/planner-resource-view.c b/src/planner-resource-view.c
index a5916e6e..273523e1 100644
--- a/src/planner-resource-view.c
+++ b/src/planner-resource-view.c
@@ -41,9 +41,9 @@
 #include "planner-property-dialog.h"
 #include "planner-resource-cmd.h"
 #include "planner-column-dialog.h"
+#include "planner-util.h"
 
 struct _PlannerResourceViewPriv {
-       GtkItemFactory         *popup_factory;
        GtkTreeView            *tree_view;
        GHashTable             *property_to_column;
 
@@ -76,15 +76,6 @@ static void           resource_view_remove_resource_cb       (GtkAction
                                                              gpointer                 data);
 static void           resource_view_edit_resource_cb         (GtkAction               *action,
                                                              gpointer                 data);
-static void           resource_view_popup_insert_resource_cb (gpointer                 callback_data,
-                                                             guint                    action,
-                                                             GtkWidget               *widget);
-static void           resource_view_popup_remove_resource_cb (gpointer                 callback_data,
-                                                             guint                    action,
-                                                             GtkWidget               *widget);
-static void           resource_view_popup_edit_resource_cb   (gpointer                 callback_data,
-                                                             guint                    action,
-                                                             GtkWidget               *widget);
 static void           resource_view_edit_columns_cb           (GtkAction              *action,
                                                               gpointer                data);
 static void           resource_view_select_all_cb            (GtkAction               *action,
@@ -256,26 +247,6 @@ static const GtkActionEntry entries[] = {
          G_CALLBACK (resource_view_edit_columns_cb) }
 };
 
-enum {
-       POPUP_NONE,
-       POPUP_INSERT,
-       POPUP_REMOVE,
-       POPUP_EDIT
-};
-
-#define GIF_CB(x) ((GtkItemFactoryCallback)(x))
-
-static GtkItemFactoryEntry popup_menu_items[] = {
-       { N_("/_Insert resource"),   NULL, GIF_CB (resource_view_popup_insert_resource_cb),
-         POPUP_INSERT,  "<Item>",       NULL
-       },
-       { N_("/_Remove resource"),   NULL, GIF_CB (resource_view_popup_remove_resource_cb),
-         POPUP_REMOVE,  "<StockItem>",  GTK_STOCK_DELETE
-       },
-       { N_("/_Edit resource..."),  NULL, GIF_CB (resource_view_popup_edit_resource_cb),
-         POPUP_EDIT,    "<Item>",       NULL }
-};
-
 /*
  * Commands
  */
@@ -362,11 +333,6 @@ resource_view_finalize (GObject *object)
                resource_view_deactivate (PLANNER_VIEW (view));
        }
 
-       if (view->priv->popup_factory) {
-               g_object_unref (view->priv->popup_factory);
-               view->priv->popup_factory = NULL;
-       }
-
        if (view->priv->property_to_column) {
                g_hash_table_unref (view->priv->property_to_column);
                view->priv->property_to_column = NULL;
@@ -406,12 +372,6 @@ resource_view_activate (PlannerView *view)
        gtk_widget_grab_focus (GTK_WIDGET (priv->tree_view));
 }
 
-static char *
-resource_view_item_factory_trans (const char *path, gpointer data)
-{
-       return _((gchar*) path);
-}
-
 static void
 resource_view_deactivate (PlannerView *view)
 {
@@ -434,20 +394,6 @@ resource_view_setup (PlannerView *view, PlannerWindow *main_window)
 
        priv->property_to_column = g_hash_table_new (NULL, NULL);
 
-       priv->popup_factory = gtk_item_factory_new (GTK_TYPE_MENU,
-                                                   "<main>",
-                                                   NULL);
-
-       gtk_item_factory_set_translate_func (priv->popup_factory,
-                                            resource_view_item_factory_trans,
-                                            NULL,
-                                            NULL);
-
-       gtk_item_factory_create_items (priv->popup_factory,
-                                      G_N_ELEMENTS (popup_menu_items),
-                                      popup_menu_items,
-                                      view);
-
        priv->ui_manager = planner_window_get_ui_manager (main_window);
 }
 
@@ -827,30 +773,6 @@ resource_view_get_type_enum (const gchar *type_str)
 
 /* Command callbacks. */
 
-static void
-resource_view_popup_insert_resource_cb  (gpointer   callback_data,
-                                        guint      action,
-                                        GtkWidget *widget)
-{
-       resource_view_insert_resource_cb (NULL, callback_data);
-}
-
-static void
-resource_view_popup_remove_resource_cb   (gpointer   callback_data,
-                                         guint      action,
-                                         GtkWidget *widget)
-{
-       resource_view_remove_resource_cb (NULL, callback_data);
-}
-
-static void
-resource_view_popup_edit_resource_cb     (gpointer   callback_data,
-                                         guint      action,
-                                         GtkWidget *widget)
-{
-       resource_view_edit_resource_cb (NULL, callback_data);
-}
-
 static void
 resource_view_insert_resource_cb (GtkAction *action,
                                  gpointer   data)
@@ -1137,10 +1059,10 @@ resource_view_button_press_event (GtkTreeView    *tv,
 {
        PlannerResourceViewPriv *priv;
        GtkTreePath             *path;
-       GtkItemFactory          *factory;
+       GtkUIManager            *ui_manager;
 
        priv = PLANNER_RESOURCE_VIEW (view)->priv;
-       factory = priv->popup_factory;
+       ui_manager = priv->ui_manager;
 
        if (event->button == 3) {
                gtk_widget_grab_focus (GTK_WIDGET (tv));
@@ -1151,22 +1073,22 @@ resource_view_button_press_event (GtkTreeView    *tv,
 
                        gtk_tree_selection_select_path (gtk_tree_view_get_selection (tv), path);
 
-                       gtk_widget_set_sensitive (
-                               gtk_item_factory_get_widget_by_action (factory, POPUP_REMOVE), TRUE);
-                       gtk_widget_set_sensitive (
-                               gtk_item_factory_get_widget_by_action (factory, POPUP_EDIT), TRUE);
+                       gtk_action_set_sensitive (
+                               gtk_ui_manager_get_action (ui_manager, "/ResourceViewPopup/RemoveResource"), 
TRUE);
+                       gtk_action_set_sensitive (
+                               gtk_ui_manager_get_action (ui_manager, "/ResourceViewPopup/EditResource"), 
TRUE);
                        gtk_tree_path_free (path);
                } else {
                        gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (tv));
 
-                       gtk_widget_set_sensitive (
-                               gtk_item_factory_get_widget_by_action (factory, POPUP_REMOVE), FALSE);
-                       gtk_widget_set_sensitive (
-                               gtk_item_factory_get_widget_by_action (factory, POPUP_EDIT), FALSE);
+                       gtk_action_set_sensitive (
+                               gtk_ui_manager_get_action (ui_manager, "/ResourceViewPopup/RemoveResource"), 
FALSE);
+                       gtk_action_set_sensitive (
+                               gtk_ui_manager_get_action (ui_manager, "/ResourceViewPopup/EditResource"), 
FALSE);
                }
 
-               gtk_item_factory_popup (factory, event->x_root, event->y_root,
-                                       event->button, event->time);
+               gtk_menu_popup (GTK_MENU (gtk_ui_manager_get_widget (ui_manager, "/ResourceViewPopup")),
+                               NULL, NULL, NULL, NULL, event->button, event->time);
                return TRUE;
        }
 
@@ -2151,41 +2073,14 @@ resource_view_popup_menu (GtkWidget   *widget,
 {
        PlannerResourceViewPriv   *priv;
        GtkTreeView       *tree;
-       GtkTreePath       *path;
-       GtkTreeViewColumn *column;
-       GdkRectangle       rect;
-       gint               x, y;
 
        priv = PLANNER_RESOURCE_VIEW (view)->priv;
        tree = GTK_TREE_VIEW (priv->tree_view);
 
-       gtk_tree_view_get_cursor (tree, &path, &column);
-       gtk_tree_view_get_cell_area (tree,
-                                    path,
-                                    column,
-                                    &rect);
-
-       x = rect.x;
-       y = rect.y;
-
-       /* Note: this is not perfect, but good enough for now. */
-       gdk_window_get_root_origin (GTK_WIDGET (tree)->window, &x, &y);
-       rect.x += x;
-       rect.y += y;
-
-       gtk_widget_translate_coordinates (GTK_WIDGET (tree),
-                                         gtk_widget_get_toplevel (GTK_WIDGET (tree)),
-                                         rect.x, rect.y,
-                                         &x, &y);
-
-       /* Offset so it's not overlapping the cell. */
-       rect.x = x + 20;
-       rect.y = y + 20;
-
-       gtk_item_factory_popup (priv->popup_factory,
-                               rect.x, rect.y,
-                               0,
-                               gtk_get_current_event_time ());
+       gtk_menu_popup (GTK_MENU (gtk_ui_manager_get_widget (priv->ui_manager, "ResourceViewPopup")),
+                       NULL, NULL,
+                       planner_util_menu_position_on_current_cell, tree,
+                       0, gtk_get_current_event_time ());
 }
 
 static void


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