[balsa/gtk4: 249/311] application-helpers: Build with gtk4




commit 0aa82c05004dead15084411308e7378bf892e2da
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Sat Oct 31 12:31:47 2020 -0400

    application-helpers: Build with gtk4

 libbalsa/application-helpers.c | 200 +++++++++++++----------------------------
 1 file changed, 64 insertions(+), 136 deletions(-)
---
diff --git a/libbalsa/application-helpers.c b/libbalsa/application-helpers.c
index 137f4eb2d..493ba4e3d 100644
--- a/libbalsa/application-helpers.c
+++ b/libbalsa/application-helpers.c
@@ -28,122 +28,76 @@
 #include <string.h>
 #include "application-helpers.h"
 
-typedef struct {
-    GAction  *action;
-    GVariant *parameter;
-} AccelInfo;
-
-static void
-accel_info_free(AccelInfo * info)
-{
-    if (info->parameter)
-        g_variant_unref(info->parameter);
-    g_free(info);
-}
-
-static gboolean
-accel_activate(GtkAccelGroup * accel_group,
-               GObject       * acceleratable,
-               guint           keyval,
-               GdkModifierType modifier,
-               gpointer        user_data)
-{
-    AccelInfo *info = user_data;
-    gboolean block_accels;
-
-    block_accels = GPOINTER_TO_INT(g_object_get_data(acceleratable, "block-accels"));
-    if (block_accels)
-        return FALSE;
-
-    g_action_activate(info->action, info->parameter);
-
-    return TRUE;
-}
-
 static void
-extract_accel_from_menu_item(GMenuModel    * model,
-                             gint            item,
-                             GActionMap    * action_map,
-                             GtkAccelGroup * accel_group)
+extract_accel_from_menu_item(GMenuModel            *model,
+                             int                    item,
+                             GtkShortcutController *controller)
 {
     GMenuAttributeIter *iter;
-    const gchar *key;
+    const char *key;
     GVariant *value;
-    const gchar *accel = NULL;
-    const gchar *action = NULL;
+    const char *accel = NULL;
+    const char *action_name = NULL;
     GVariant *target = NULL;
 
     iter = g_menu_model_iterate_item_attributes(model, item);
     while (g_menu_attribute_iter_get_next(iter, &key, &value)) {
-        if (g_str_equal(key, "action")
-            && g_variant_is_of_type(value, G_VARIANT_TYPE_STRING))
-            action = g_variant_get_string(value, NULL);
-        else if (g_str_equal(key, "accel")
-                 && g_variant_is_of_type(value, G_VARIANT_TYPE_STRING))
+        if (g_str_equal(key, "action") &&
+            g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
+            action_name = g_variant_get_string(value, NULL);
+        } else if (g_str_equal(key, "accel") &&
+                   g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
             accel = g_variant_get_string(value, NULL);
-        else if (g_str_equal(key, "target"))
+        } else if (g_str_equal(key, "target")) {
             target = g_variant_ref(value);
+        }
         g_variant_unref(value);
     }
     g_object_unref(iter);
 
-    if (accel && action) {
-        guint accel_key;
-        GdkModifierType accel_mods;
-        AccelInfo *info;
-        const gchar *basename;
-        GClosure *closure;
-
-        gtk_accelerator_parse(accel, &accel_key, &accel_mods);
-        basename = strchr(action, '.');
-        basename = basename ? basename + 1 : action;
-        info = g_new(AccelInfo, 1);
-        info->action = g_action_map_lookup_action(action_map, basename);
-        info->parameter = target ? g_variant_ref(target) : NULL;
-        closure = g_cclosure_new(G_CALLBACK(accel_activate), info,
-                                 (GClosureNotify) accel_info_free);
-        gtk_accel_group_connect(accel_group, accel_key, accel_mods, 0,
-                                closure);
-    }
+    if (accel != NULL && action_name != NULL) {
+        const char *basename;
+        GtkShortcutTrigger *trigger;
+        GtkShortcutAction *action;
+        GtkShortcut *shortcut;
+
+        trigger = gtk_shortcut_trigger_parse_string(accel);
 
-    if (target)
+        basename = strchr(action_name, '.');
+        basename = basename != NULL ? basename + 1 : action_name;
+        action = gtk_named_action_new(basename);
+
+        shortcut = gtk_shortcut_new(trigger, action);
+        gtk_shortcut_set_arguments(shortcut, target);
+
+        gtk_shortcut_controller_add_shortcut(controller, shortcut);
+    } else if (target != NULL) {
         g_variant_unref(target);
+    }
 }
 
 static void
-extract_accels_from_menu(GMenuModel    * model,
-                         GActionMap    * action_map,
-                         GtkAccelGroup * accel_group)
+extract_accels_from_menu(GMenuModel            *model,
+                         GtkShortcutController *controller)
 {
-    gint i, n = g_menu_model_get_n_items(model);
-    GMenuLinkIter *iter;
-    const gchar *key;
-    GMenuModel *m;
+    int n = g_menu_model_get_n_items(model);
+    int i;
 
     for (i = 0; i < n; i++) {
-        extract_accel_from_menu_item(model, i, action_map, accel_group);
+        GMenuLinkIter *iter;
+        GMenuModel *linked_model;
+
+        extract_accel_from_menu_item(model, i, controller);
 
         iter = g_menu_model_iterate_item_links(model, i);
-        while (g_menu_link_iter_get_next(iter, &key, &m)) {
-            extract_accels_from_menu(m, action_map, accel_group);
-            g_object_unref(m);
+        while (g_menu_link_iter_get_next(iter, NULL, &linked_model)) {
+            extract_accels_from_menu(linked_model, controller);
+            g_object_unref(linked_model);
         }
         g_object_unref(iter);
     }
 }
 
-static GtkAccelGroup *
-get_accel_group(GMenuModel * model,
-                GActionMap * action_map)
-{
-    GtkAccelGroup *accel_group;
-
-    accel_group = gtk_accel_group_new();
-    extract_accels_from_menu(model, action_map, accel_group);
-
-    return accel_group;
-}
-
 /*
  * libbalsa_window_get_menu_bar
  *
@@ -164,8 +118,8 @@ get_accel_group(GMenuModel * model,
 GtkWidget *
 libbalsa_window_get_menu_bar(GtkApplicationWindow * window,
                              const GActionEntry   * entries,
-                             gint                   n_entries,
-                             const gchar          * resource_path,
+                             int                    n_entries,
+                             const char           * resource_path,
                              GError              ** error,
                              gpointer               cb_data)
 {
@@ -182,7 +136,7 @@ libbalsa_window_get_menu_bar(GtkApplicationWindow * window,
         menu_model =
             G_MENU_MODEL(gtk_builder_get_object(builder, "menubar"));
 
-        menu_bar = gtk_menu_bar_new_from_model(menu_model);
+        menu_bar = gtk_popover_menu_bar_new_from_model(menu_model);
 
         libbalsa_window_set_accels(window, menu_model);
         gtk_application_window_set_show_menubar(window, FALSE);
@@ -206,19 +160,11 @@ void
 libbalsa_window_set_accels(GtkApplicationWindow * window,
                            GMenuModel           * menu_model)
 {
-    GSList *accel_groups;
-    GtkAccelGroup *accel_group;
-
-    /* Remove current accelerators: */
-    accel_groups = gtk_accel_groups_from_object(G_OBJECT(window));
-    if (accel_groups)
-        /* Last is first... */
-        gtk_window_remove_accel_group(GTK_WINDOW(window),
-                                      accel_groups->data);
-
-    accel_group = get_accel_group(menu_model, G_ACTION_MAP(window));
-    gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);
-    g_object_unref(accel_group);
+    GtkEventController *controller;
+
+    controller = gtk_shortcut_controller_new();
+    extract_accels_from_menu(menu_model, GTK_SHORTCUT_CONTROLLER(controller));
+    gtk_widget_add_controller(GTK_WIDGET(window), controller);
 }
 
 void
@@ -240,42 +186,24 @@ libbalsa_window_block_accels(GtkApplicationWindow * window,
 
 void
 libbalsa_window_add_accelerator(GtkApplicationWindow * window,
-                                const gchar          * accel,
-                                const gchar          * action_name)
+                                const char           * accel,
+                                const char           * action_name)
 {
-    GActionMap *action_map = G_ACTION_MAP(window);
-    guint accel_key;
-    GdkModifierType accel_mods;
-    const gchar *basename;
-    GAction *action;
-    AccelInfo *info;
-    GClosure *closure;
-    GtkAccelGroup *accel_group;
-
-    gtk_accelerator_parse(accel, &accel_key, &accel_mods);
-    if (!accel_key) {
-        g_warning("%s: could not parse accelerator ā€œ%sā€", __func__,
-                accel);
-        return;
-    }
+    const char *basename;
+    GtkShortcutTrigger *trigger;
+    GtkShortcutAction *action;
+    GtkShortcut *shortcut;
+    GtkEventController *controller;
+
+    trigger = gtk_shortcut_trigger_parse_string(accel);
 
     basename = strchr(action_name, '.');
-    basename = basename ? basename + 1 : action_name;
-    action = g_action_map_lookup_action(action_map, basename);
-    if (!action) {
-       g_warning("%s: could not lookup action ā€œ%sā€", __func__,
-                action_name);
-        return;
-    }
+    basename = basename != NULL ? basename + 1 : action_name;
+    action = gtk_named_action_new(basename);
 
-    info = g_new(AccelInfo, 1);
-    info->action = action;
-    info->parameter = NULL;
-    closure = g_cclosure_new(G_CALLBACK(accel_activate), info,
-                             (GClosureNotify) accel_info_free);
+    shortcut = gtk_shortcut_new(trigger, action);
 
-    accel_group = gtk_accel_group_new();
-    gtk_accel_group_connect(accel_group, accel_key, accel_mods, 0,
-                            closure);
-    gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);
+    controller = gtk_shortcut_controller_new();
+    gtk_shortcut_controller_add_shortcut(GTK_SHORTCUT_CONTROLLER(controller), shortcut);
+    gtk_widget_add_controller(GTK_WIDGET(window), controller);
 }


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