[balsa/wip/gtk4] ab-main: Use GMenu API instead of GtkBuilder



commit 524e82c26fdae8d3d79834f8c45cd3f9de2c3f03
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Tue Sep 24 20:32:25 2019 -0400

    ab-main: Use GMenu API instead of GtkBuilder
    
    * src/ab-main.c (bab_cleanup), (set_address_book_menu_items),
      (file_delete_activated):
    * ui/ab-main.ui: no need for place-holder

 ChangeLog     |   8 +++
 src/ab-main.c | 162 +++++++++++++++++++++++++++++++++++++++++-----------------
 ui/ab-main.ui |   3 --
 3 files changed, 123 insertions(+), 50 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index fac81ce50..b5acccdd0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2019-09-18  Peter Bloomfield  <pbloomfield bellsouth net>
+
+       ab-main: Use GMenu API instead of GtkBuilder
+
+       * src/ab-main.c (bab_cleanup), (set_address_book_menu_items),
+       (file_delete_activated):
+       * ui/ab-main.ui: no need for place-holder
+
 2019-02-24  Pawel Salek  <pawsa0 gmail com>
 
        Fix some issues raised by scan-build, the clang static analyzer
diff --git a/src/ab-main.c b/src/ab-main.c
index b710ae2fb..bb2837f0d 100644
--- a/src/ab-main.c
+++ b/src/ab-main.c
@@ -68,12 +68,14 @@ struct ABMainWindow {
     LibBalsaAddress *displayed_address;
 
     GMenu *file_menu;
+    GMenu *books_menu;
 } contacts_app;
 
 
 static void
 bab_cleanup(void)
 {
+    g_object_unref(contacts_app.books_menu);
     gtk_main_quit();
 }
 
@@ -261,64 +263,47 @@ address_changed_cb(struct ABMainWindow *aw)
 static void
 set_address_book_menu_items(void)
 {
-    GString *string;
     GList *l;
     guint pos;
-    gchar *s;
-    GtkBuilder *builder;
-    GMenuModel *menu_model;
+    GMenu *menu = contacts_app.books_menu;
 
-    pos = g_menu_model_get_n_items(G_MENU_MODEL (contacts_app.file_menu));
-    g_menu_remove(contacts_app.file_menu, --pos);
+    if (menu == NULL) {
+        contacts_app.books_menu = menu = g_menu_new();
+        g_menu_append_section(contacts_app.file_menu, NULL, G_MENU_MODEL(menu));
+    } else {
+        g_menu_remove_all(menu);
+    }
 
     pos = 0;
-    string = g_string_new(NULL);
-    g_string_append(string, "<interface>");
-    g_string_append(string, "<menu id='address-book-menu'>");
-    g_string_append(string, "<section>");
-    for (l = contacts_app.address_book_list; l; l = l->next) {
+    for (l = contacts_app.address_book_list; l != NULL; l = l->next) {
         LibBalsaAddressBook *address_book = l->data;
         const gchar *name;
+        gchar *label;
+        gchar *detailed_action;
+        gchar *accel;
+        GMenuItem *item;
 
-        if (!address_book)
+        if (address_book == NULL)
             continue;
-        name = libbalsa_address_book_get_name(address_book);
-
-        g_string_append(string, "<item>");
 
-        g_string_append(string, "<attribute name='label'>");
-        g_string_append_printf(string, "_%d:%s", ++pos, name);
-        g_string_append(string, "</attribute>");
-
-        g_string_append(string, "<attribute name='action'>");
-        g_string_append(string, "win.address-book");
-        g_string_append(string, "</attribute>");
+        name = libbalsa_address_book_get_name(address_book);
 
-        g_string_append(string, "<attribute name='target'>");
-        g_string_append(string, name);
-        g_string_append(string, "</attribute>");
+        label = g_strdup_printf("_%d:%s", ++pos, name);
+        detailed_action = g_strdup_printf("win.address-book::%s", name);
+        item = g_menu_item_new(label, detailed_action);
+        g_free(detailed_action);
+        g_free(label);
 
-        g_string_append(string, "<attribute name='accel'>");
-        g_string_append_printf(string, "&lt;Primary&gt;%d", pos);
-        g_string_append(string, "</attribute>");
+        accel = g_strdup_printf("<Primary>%d", pos);
+        g_menu_item_set_attribute(item, "accel", "s", accel);
+        g_free(accel);
 
-        g_string_append(string, "</item>");
+        g_menu_append_item(menu, item);
+        g_object_unref(item);
     }
-    g_string_append(string, "</section>");
-    g_string_append(string, "</menu>");
-    g_string_append(string, "</interface>");
-    s = g_string_free(string, FALSE);
-
-    builder = gtk_builder_new_from_string(s, -1);
-    g_free(s);
 
-    menu_model =
-        G_MENU_MODEL(gtk_builder_get_object(builder, "address-book-menu"));
-    g_menu_append_section(contacts_app.file_menu, NULL, menu_model);
     libbalsa_window_set_accels(GTK_APPLICATION_WINDOW(contacts_app.window),
-                               menu_model);
-
-    g_object_unref(builder);
+                               G_MENU_MODEL(menu));
 }
 
 static gboolean
@@ -457,8 +442,8 @@ file_delete_activated(GSimpleAction * action,
     const gchar *config_prefix;
     GList *list;
 
-    if ((address_book = contacts_app.address_book) == NULL
-        || contacts_app.address_book_list->next == NULL)
+    address_book = contacts_app.address_book;
+    if (address_book == NULL || contacts_app.address_book_list->next == NULL)
         return;
 
     config_prefix = libbalsa_address_book_get_config_prefix(address_book);
@@ -591,12 +576,95 @@ get_main_menu(GtkApplication * application)
             address_book_change_state},
     };
     GtkBuilder *builder;
-    static const gchar resource_path[] =
-        "/org/desktop/BalsaAb/ab-main.ui";
+    static const gchar ui[] = {
+"<interface>"
+"    <menu id=\"menubar\">"
+"        <submenu id=\"file-menu\">"
+"            <attribute name='label' translatable='yes'>_File</attribute>"
+"            <section>"
+"                <submenu>"
+"                    <attribute name='label'"
+"                        translatable='yes'"
+"                        context='address book'"
+"                        comments='Translators: main menu item - New address book'>_New</attribute>"
+"                    <item>"
+"                        <attribute name='label'"
+"                            translatable='yes'>_VCard Address Book (GnomeCard)</attribute>"
+"                        <attribute name='action'>win.file-new-vcard</attribute>"
+"                    </item>"
+"                    <item>"
+"                        <attribute name='label'"
+"                            translatable='yes'>_External query (a program)</attribute>"
+"                        <attribute name='action'>win.file-new-external</attribute>"
+"                    </item>"
+"                    <item>"
+"                        <attribute name='label'"
+"                            translatable='yes'>_LDIF Address Book</attribute>"
+"                        <attribute name='action'>win.file-new-ldif</attribute>"
+"                    </item>"
+"                    <item>"
+"                        <attribute name='label'"
+"                            translatable='yes'>_LDAP Address Book</attribute>"
+"                        <attribute name='action'>win.file-new-ldap</attribute>"
+"                    </item>"
+"                    <item>"
+"                        <attribute name='label'"
+"                            translatable='yes'>_GPE Address Book</attribute>"
+"                        <attribute name='action'>win.file-new-gpe</attribute>"
+"                    </item>"
+"                    <item>"
+"                        <attribute name='label'"
+"                            translatable='yes'>_Rubrica Address Book</attribute>"
+"                        <attribute name='action'>win.file-new-rubrica</attribute>"
+"                    </item>"
+"                </submenu>"
+"                <item>"
+"                    <attribute name='label'"
+"                        translatable='yes'>_Properties</attribute>"
+"                    <attribute name='action'>win.file-properties</attribute>"
+"                </item>"
+"                <item>"
+"                    <attribute name='label'"
+"                        translatable='yes'>_Delete</attribute>"
+"                    <attribute name='action'>win.file-delete</attribute>"
+"                </item>"
+"            </section>"
+"            <section>"
+"                <item>"
+"                    <attribute name='label'"
+"                        translatable='yes'>_Quit</attribute>"
+"                    <attribute name='action'>win.file-quit</attribute>"
+"                </item>"
+"            </section>"
+"        </submenu>"
+"        <submenu>"
+"            <attribute name='label' translatable='yes'>_Entry</attribute>"
+"            <item>"
+"                <attribute name='label'"
+"                    translatable='yes'>_New Entry</attribute>"
+"                <attribute name='action'>win.entry-new</attribute>"
+"            </item>"
+"            <item>"
+"                <attribute name='label'"
+"                    translatable='yes'>_Delete Entry</attribute>"
+"                <attribute name='action'>win.entry-delete</attribute>"
+"            </item>"
+"        </submenu>"
+"        <submenu>"
+"            <attribute name='label' translatable='yes'>_Help</attribute>"
+"            <item>"
+"                <attribute name='label'"
+"                    translatable='yes'>_About</attribute>"
+"                <attribute name='action'>win.help-about</attribute>"
+"            </item>"
+"        </submenu>"
+"    </menu>"
+"</interface>"
+        };
     GError *err = NULL;
 
     builder = gtk_builder_new();
-    if (gtk_builder_add_from_resource(builder, resource_path, &err)) {
+    if (gtk_builder_add_from_string(builder, ui, -1, &err)) {
         gtk_application_set_menubar(application,
                                     G_MENU_MODEL(gtk_builder_get_object
                                                  (builder, "menubar")));
diff --git a/ui/ab-main.ui b/ui/ab-main.ui
index f530dc56b..a02f78fbd 100644
--- a/ui/ab-main.ui
+++ b/ui/ab-main.ui
@@ -57,9 +57,6 @@
                     <attribute name='action'>win.file-quit</attribute>
                 </item>
             </section>
-            <section>
-                <!-- Placeholder for the address books section -->
-            </section>
         </submenu>
         <submenu>
             <attribute name='label' translatable='yes'>_Entry</attribute>


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