[telegnome] Port menu action handling to GAction/GMenu



commit b25b9014a56920823af766e5af8e997e06d68c99
Author: Colin Watson <cjwatson debian org>
Date:   Sat Feb 6 02:31:30 2016 +0000

    Port menu action handling to GAction/GMenu
    
    * po/POTFILES.in: Replace src/menu.h with src/menus.ui.
    * src/Makefile.am (telegnome_SOURCES): Remove menu.h.
    * src/app.c (app_entries): New structure.
    (tg_app_startup): New function.
    (tg_app_class_init): Add startup method.
    * src/gui.c (struct _TgGui): Change channel_menu to GMenu *.
    (tg_gui_channel_menu_item_activate): Rename to ...
    (tg_gui_change_state_set_channel): ... this, now a GSimpleAction
    method.
    (tg_gui_populate_channel_menu): Populate a GMenu instead of a
    GtkMenu.
    (tg_gui_new): Drop manual action and menu setup, now handled
    automatically by GtkApplication.  Get channel_menu from
    GtkApplication.  Drop manual handling of delete-event.
    (tg_gui_cb_quit): Rename to ...
    (tg_gui_activate_quit): ... this, now a GSimpleAction method.
    (tg_gui_cb_help_contents): Rename to ...
    (tg_gui_activate_help_contents): ... this, now a GSimpleAction
    method.
    (tg_gui_cb_about): Rename to ...
    (tg_gui_activate_about): ... this, now a GSimpleAction method.
    (tg_gui_cb_preferences): Rename to ...
    (tg_gui_activate_preferences): ... this, now a GSimpleAction method.
    * src/gui.h: Update prototypes.
    * src/main.h (TG_MENU_XML): Remove.
    * src/menu.h, src/menu.xml: Remove.
    * src/menus.ui: New file.
    * src/telegnome.gresource.xml: Remove menu.xml.  Add menus.ui.

 NEWS                        |    1 +
 po/POTFILES.in              |    2 +-
 src/Makefile.am             |    1 -
 src/app.c                   |   19 ++++++++
 src/gui.c                   |  101 ++++++++++++------------------------------
 src/gui.h                   |   14 ++++--
 src/main.h                  |    1 -
 src/menu.h                  |   34 --------------
 src/menu.xml                |   15 ------
 src/menus.ui                |   54 +++++++++++++++++++++++
 src/telegnome.gresource.xml |    2 +-
 11 files changed, 115 insertions(+), 129 deletions(-)
---
diff --git a/NEWS b/NEWS
index 00fc65a..f920b45 100644
--- a/NEWS
+++ b/NEWS
@@ -10,6 +10,7 @@ Changes in TeleGNOME 0.2.1
  * Port from GdkDrawable to Cairo.
  * Port to GTK+ 3.
  * Basic port to GtkApplication.
+ * Port menu action handling from GtkUIManager/GtkAction to GAction/GMenu.
 
 Changes in TeleGNOME 0.2.0
 ==========================
diff --git a/po/POTFILES.in b/po/POTFILES.in
index a737068..c9ac0a0 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -2,7 +2,7 @@
 # Please keep this file sorted alphabetically.
 data/org.gnome.telegnome.gschema.xml
 src/gui.c
-src/menu.h
+[type: gettext/glade] src/menus.ui
 src/prefs.c
 src/view.c
 telegnome.desktop.in
diff --git a/src/Makefile.am b/src/Makefile.am
index b3eaef5..b2fc309 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -13,7 +13,6 @@ telegnome_SOURCES = \
        gui.c \
        prefs.h \
        prefs.c \
-       menu.h \
        view.h \
        view.c \
        pixpack.c \
diff --git a/src/app.c b/src/app.c
index 3d4ac20..aa9b785 100644
--- a/src/app.c
+++ b/src/app.c
@@ -38,12 +38,30 @@ struct _TgApp {
 
 G_DEFINE_TYPE (TgApp, tg_app, GTK_TYPE_APPLICATION)
 
+static GActionEntry app_entries[] = {
+    { "quit", tg_gui_activate_quit, NULL, NULL, NULL },
+    { "preferences", tg_gui_activate_preferences, NULL, NULL, NULL },
+    { "help-contents", tg_gui_activate_help_contents, NULL, NULL, NULL },
+    { "about", tg_gui_activate_about, NULL, NULL, NULL },
+    { "set-channel", NULL, "s", "''", tg_gui_change_state_set_channel }
+};
+
 static void
 tg_app_init (TgApp *app)
 {
 }
 
 static void
+tg_app_startup (GApplication *app)
+{
+    G_APPLICATION_CLASS (tg_app_parent_class)->startup (app);
+
+    g_action_map_add_action_entries (G_ACTION_MAP (app),
+                                    app_entries, G_N_ELEMENTS (app_entries),
+                                    app);
+}
+
+static void
 tg_app_activate (GApplication *app)
 {
     GSettings *settings;
@@ -59,6 +77,7 @@ tg_app_activate (GApplication *app)
 static void
 tg_app_class_init (TgAppClass *klass)
 {
+    G_APPLICATION_CLASS (klass)->startup = tg_app_startup;
     G_APPLICATION_CLASS (klass)->activate = tg_app_activate;
 }
 
diff --git a/src/gui.c b/src/gui.c
index 85e9047..2926691 100644
--- a/src/gui.c
+++ b/src/gui.c
@@ -35,7 +35,6 @@
 #include "gui.h"
 #include "main.h"
 #include "prefs.h"
-#include "menu.h"
 #include "channel.h"
 
 struct _TgGui {
@@ -54,7 +53,7 @@ struct _TgGui {
 
     GtkWidget *zoombutton;
 
-    GtkWidget *channel_menu;
+    GMenu *channel_menu;
 
     /* for timer-input */
     gint logo_timer;
@@ -158,20 +157,18 @@ tg_gui_channel_select(TgChannel *channel)
 #endif
 }
 
-static void
-tg_gui_channel_menu_item_activate(GtkWidget *w, gpointer data)
+void
+tg_gui_change_state_set_channel(GSimpleAction *action, GVariant *value,
+                               gpointer data)
 {
-    gchar *uuid;
+    const gchar *uuid;
 
-    g_assert(data != NULL);
-    g_object_get(TG_CHANNEL(data), "uuid", &uuid, NULL);
-    g_object_set(
-       gui,
-       "current-channel", uuid,
-       "current-page-number", 100,
-       "current-subpage-number", 0,
-       NULL);
-    g_free(uuid);
+    uuid = g_variant_get_string(value, NULL);
+    g_object_set(gui,
+                "current-channel", uuid,
+                "current-page-number", 100,
+                "current-subpage-number", 0,
+                NULL);
     tg_gui_get_the_page(FALSE);
 }
 
@@ -181,31 +178,22 @@ tg_gui_channel_menu_item_activate(GtkWidget *w, gpointer data)
 static void
 tg_gui_populate_channel_menu(void)
 {
-    GList *children, *iter;
-    GtkWidget *item;
     int i;
     TgChannel *channel;
 
     g_assert(gui->channels != NULL);
 
-    children = gtk_container_get_children(GTK_CONTAINER(gui->channel_menu));
-    for (iter = children; iter; iter = iter->next)
-       gtk_widget_destroy(GTK_WIDGET(iter->data));
+    g_menu_remove_all(gui->channel_menu);
 
     for (i=0; i<g_slist_length(gui->channels); i++) {
-       gchar *name;
+       gchar *name, *uuid, *action;
 
        channel = TG_CHANNEL(g_slist_nth_data(gui->channels, i));
-       g_object_get(channel, "name", &name, NULL);
-
-       item = gtk_menu_item_new_with_label(name);
-
-       g_signal_connect(G_OBJECT(item), "activate",
-                        G_CALLBACK(tg_gui_channel_menu_item_activate),
-                        (gpointer)channel);
-       gtk_menu_shell_append(GTK_MENU_SHELL(gui->channel_menu), item);
-       gtk_widget_show(item);
-
+       g_object_get(channel, "name", &name, "uuid", &uuid, NULL);
+       action = g_strdup_printf("app.set-channel::%s", uuid);
+       g_menu_append(gui->channel_menu, name, action);
+       g_free(action);
+       g_free(uuid);
        g_free(name);
     }
 }
@@ -660,11 +648,6 @@ TgGui *
 tg_gui_new (GtkApplication *app, GSettings *settings)
 {
     GtkWidget *toolbar;
-    GtkUIManager *ui_manager;
-    GtkActionGroup *action_group;
-    GtkAccelGroup *accel_group;
-    GBytes *menu_data;
-    GtkWidget *menu_bar;
     GtkWidget *status_frame;
     GtkWidget *contents;
     GdkPixbuf *pixbuf;
@@ -684,41 +667,14 @@ tg_gui_new (GtkApplication *app, GSettings *settings)
 
     gtk_widget_realize (GTK_WIDGET (gui->window));
 
-    toolbar = tg_gui_new_toolbar();
-
     /* attach a keyboard event */
     g_signal_connect (G_OBJECT (gui->window), "key-press-event",
                      G_CALLBACK (tg_cb_keypress), NULL);
     
-    /* attach the menu */
-    ui_manager = gtk_ui_manager_new ();
-    action_group = gtk_action_group_new ("TeleGNOMEActions");
-    gtk_action_group_set_translation_domain (action_group, NULL);
-    gtk_action_group_add_actions (action_group, entries,
-                                 G_N_ELEMENTS (entries), gui->window);
-    gtk_ui_manager_insert_action_group (ui_manager, action_group, 0);
-    accel_group = gtk_ui_manager_get_accel_group (ui_manager);
-    gtk_window_add_accel_group (GTK_WINDOW (gui->window), accel_group);
-
-    error = NULL;
-    menu_data = g_resources_lookup_data (TG_MENU_XML, 0, &error);
-    g_assert_no_error (error);
-    error = NULL;
-    gtk_ui_manager_add_ui_from_string (ui_manager,
-                                      g_bytes_get_data (menu_data, NULL),
-                                      g_bytes_get_size (menu_data),
-                                      &error);
-    g_assert_no_error (error);
-    g_bytes_unref (menu_data);
-
-    menu_bar = gtk_ui_manager_get_widget (ui_manager, "/MenuBar");
-    gtk_grid_attach (GTK_GRID (gui->grid), menu_bar, 0, 0, 2, 1);
+    toolbar = tg_gui_new_toolbar();
     gtk_grid_attach (GTK_GRID (gui->grid), toolbar, 0, 1, 2, 1);
 
-    gui->channel_menu = gtk_menu_item_get_submenu (
-       GTK_MENU_ITEM (gtk_ui_manager_get_widget (ui_manager,
-                                                 "/MenuBar/ChannelsMenu")));
-    g_object_unref (ui_manager);
+    gui->channel_menu = gtk_application_get_menu_by_id (app, "channels");
 
     /* the view */
     currentview = tg_view_new();
@@ -760,9 +716,6 @@ tg_gui_new (GtkApplication *app, GSettings *settings)
     gtk_widget_set_halign (status_frame, GTK_ALIGN_FILL);
     gtk_grid_attach (GTK_GRID (gui->grid), status_frame, 1, 3, 1, 1);
 
-    g_signal_connect (G_OBJECT (gui->window), "delete-event",
-                     G_CALLBACK (tg_gui_cb_quit), NULL);
-
     gtk_widget_show_all (gui->window);
 
     gui->kb_timer = -1;
@@ -871,7 +824,8 @@ tg_gui_get_the_page (gboolean redraw)
  * callbacks 
  */
 void
-tg_gui_cb_quit (GtkWidget* widget, gpointer data)
+tg_gui_activate_quit (GSimpleAction *action, GVariant *parameter,
+                     gpointer data)
 {
     /* free the channels */
     if (gui->channels != NULL) {
@@ -885,7 +839,8 @@ tg_gui_cb_quit (GtkWidget* widget, gpointer data)
 }
 
 void
-tg_gui_cb_help_contents (GtkWidget* widget, gpointer data)
+tg_gui_activate_help_contents (GSimpleAction *action, GVariant *parameter,
+                              gpointer data)
 {
     GError *error;
     gboolean ret;
@@ -900,7 +855,8 @@ tg_gui_cb_help_contents (GtkWidget* widget, gpointer data)
 }
 
 void
-tg_gui_cb_about (GtkWidget* widget, gpointer data)
+tg_gui_activate_about (GSimpleAction *action, GVariant *parameter,
+                      gpointer data)
 {
     static GtkWidget *about;
     const gchar *authors[]= { "Dirk-Jan C. Binnema <djcb dds nl>",
@@ -963,7 +919,8 @@ tg_gui_prefs_close_cb (GtkDialog *dialog, gint response_id, gpointer user_data)
 }
 
 void
-tg_gui_cb_preferences (GtkWidget* widget, gpointer data)
+tg_gui_activate_preferences (GSimpleAction *action, GVariant *parameter,
+                            gpointer data)
 {
     tg_prefs_show(GTK_WINDOW(gui->window), G_CALLBACK(tg_gui_prefs_close_cb));
 }
diff --git a/src/gui.h b/src/gui.h
index 37f81d3..bcc581a 100644
--- a/src/gui.h
+++ b/src/gui.h
@@ -43,10 +43,16 @@ void tg_gui_get_the_page (gboolean redraw);
 
 
 /* event handler callbacks */
-void tg_gui_cb_quit (GtkWidget* widget, gpointer data);
-void tg_gui_cb_help_contents (GtkWidget *widget, gpointer data);
-void tg_gui_cb_about (GtkWidget* widget, gpointer data);
-void tg_gui_cb_preferences (GtkWidget* widget, gpointer data);
+void tg_gui_activate_quit (GSimpleAction *action, GVariant *parameter,
+                          gpointer data);
+void tg_gui_activate_help_contents (GSimpleAction *action, GVariant *parameter,
+                                   gpointer data);
+void tg_gui_activate_about (GSimpleAction *action, GVariant *parameter,
+                           gpointer data);
+void tg_gui_activate_preferences (GSimpleAction *action, GVariant *parameter,
+                                 gpointer data);
+void tg_gui_change_state_set_channel (GSimpleAction *action, GVariant *value,
+                                     gpointer data);
 void tg_gui_cb_next_page (GtkWidget* widget, gpointer data);
 void tg_gui_cb_prev_page (GtkWidget* widget, gpointer data);
 void tg_gui_cb_home (GtkWidget* widget, gpointer data);
diff --git a/src/main.h b/src/main.h
index 27b6fdc..06693c2 100644
--- a/src/main.h
+++ b/src/main.h
@@ -51,7 +51,6 @@ typedef struct _TeleGnome{
 #define TG_ERR_VFS 2
 #define TG_ERR_HTTPQUERY 3     /* error getting http query */
 
-#define TG_MENU_XML            "/org/gnome/telegnome/menu.xml"
 #define TG_NOTFOUND_PIXMAP     "/org/gnome/telegnome/pixmaps/testbeeld.png"
 #define TG_LOGO_PIXMAP                 "/org/gnome/telegnome/pixmaps/telegnome-logo.png"
 
diff --git a/src/menus.ui b/src/menus.ui
new file mode 100644
index 0000000..1d0e926
--- /dev/null
+++ b/src/menus.ui
@@ -0,0 +1,54 @@
+<!--
+ * Copyright (C) 2016 Colin Watson <cjwatson debian org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+-->
+
+<?xml version="1.0"?>
+<interface>
+  <!-- interface-requires gtk+ 3.0 -->
+  <menu id="menubar">
+    <submenu>
+      <attribute name="label" translatable="yes">_File</attribute>
+      <item>
+        <attribute name="label" translatable="yes">_Quit</attribute>
+        <attribute name="action">app.quit</attribute>
+        <attribute name="accel">&lt;Control&gt;q</attribute>
+      </item>
+    </submenu>
+    <submenu>
+      <attribute name="label" translatable="yes">_Settings</attribute>
+      <item>
+        <attribute name="label" translatable="yes">_Preferences</attribute>
+        <attribute name="action">app.preferences</attribute>
+      </item>
+    </submenu>
+    <submenu id="channels">
+      <attribute name="label" translatable="yes">_Channels</attribute>
+    </submenu>
+    <submenu>
+      <attribute name="label" translatable="yes">_Help</attribute>
+      <item>
+        <attribute name="label" translatable="yes">_Contents</attribute>
+        <attribute name="action">app.help-contents</attribute>
+        <attribute name="accel">F1</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">_About</attribute>
+        <attribute name="action">app.about</attribute>
+      </item>
+    </submenu>
+  </menu>
+</interface>
diff --git a/src/telegnome.gresource.xml b/src/telegnome.gresource.xml
index b0c9994..f1cae35 100644
--- a/src/telegnome.gresource.xml
+++ b/src/telegnome.gresource.xml
@@ -20,6 +20,6 @@
   <gresource prefix="/org/gnome/telegnome">
     <file alias="pixmaps/telegnome-logo.png">../pixmaps/telegnome-logo.png</file>
     <file alias="pixmaps/testbeeld.png">../pixmaps/testbeeld.png</file>
-    <file>menu.xml</file>
+    <file alias="gtk/menus.ui">menus.ui</file>
   </gresource>
 </gresources>


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