[devhelp/wip/stuff: 1/2] app: improve app menu handling



commit e35f79338e34a568dd02085b0c3534ec60886e05
Author: Ignacio Casal Quinteiro <ignacio casal nice-software com>
Date:   Tue Jul 30 17:33:24 2013 +0200

    app: improve app menu handling
    
    Move the declaration to its own file.
    Handle correctly whether or not to have an app menu.

 src/devhelp-menu.ui  |   51 ++++++++++++++++++++++++++++++++
 src/dh-app.c         |   80 ++++++++++++++++++++++++++++++--------------------
 src/dh-app.h         |    2 +
 src/dh.gresource.xml |    1 +
 4 files changed, 102 insertions(+), 32 deletions(-)
---
diff --git a/src/devhelp-menu.ui b/src/devhelp-menu.ui
new file mode 100644
index 0000000..aa85bed
--- /dev/null
+++ b/src/devhelp-menu.ui
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+  Copyright (C) 2010 Imendio AB
+  Copyright (C) 2012 Aleksander Morgado <aleksander gnu 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 licence, 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 02110-1301
+  USA
+-->
+
+<interface>
+  <requires lib="gtk+" version="3.0"/>
+  <menu id="app-menu">
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">New _Window</attribute>
+        <attribute name="action">app.new-window</attribute>
+        <attribute name="accel">&lt;Primary&gt;n</attribute>
+      </item>
+    </section>
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">_Preferences</attribute>
+        <attribute name="action">app.preferences</attribute>
+      </item>
+    </section>
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">_About Devhelp</attribute>
+        <attribute name="action">app.about</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">_Quit</attribute>
+        <attribute name="action">app.quit</attribute>
+        <attribute name="accel">&lt;Primary&gt;q</attribute>
+      </item>
+    </section>
+  </menu>
+</interface>
diff --git a/src/dh-app.c b/src/dh-app.c
index f22719d..81ebb0a 100644
--- a/src/dh-app.c
+++ b/src/dh-app.c
@@ -86,6 +86,29 @@ dh_app_peek_assistant (DhApp *app)
         return NULL;
 }
 
+gboolean
+_dh_app_has_app_menu (DhApp *app)
+{
+        GtkSettings *gtk_settings;
+        gboolean show_app_menu;
+        gboolean show_menubar;
+
+        g_return_val_if_fail (DH_IS_APP (app), FALSE);
+
+        /* We have three cases:
+         * - GNOME 3: show-app-menu true, show-menubar false -> use the app menu
+         * - Unity, OSX: show-app-menu and show-menubar true -> use the normal menu
+         * - Other WM, Windows: show-app-menu and show-menubar false -> use the normal menu
+         */
+        gtk_settings = gtk_settings_get_default ();
+        g_object_get (G_OBJECT (gtk_settings),
+                      "gtk-shell-shows-app-menu", &show_app_menu,
+                      "gtk-shell-shows-menubar", &show_menubar,
+                      NULL);
+
+        return show_app_menu && !show_menubar;
+}
+
 /******************************************************************************/
 /* Application action activators */
 
@@ -267,14 +290,6 @@ static GActionEntry app_entries[] = {
         { "raise",            raise_cb,            NULL, NULL, NULL },
 };
 
-static void
-setup_actions (DhApp *app)
-{
-        g_action_map_add_action_entries (G_ACTION_MAP (app),
-                                         app_entries, G_N_ELEMENTS (app_entries),
-                                         app);
-}
-
 /******************************************************************************/
 
 static void
@@ -298,26 +313,6 @@ setup_accelerators (DhApp *self)
 /******************************************************************************/
 
 static void
-setup_menu (DhApp *app)
-{
-        GtkBuilder *builder;
-        GMenuModel *model;
-        GError *error = NULL;
-
-        builder = gtk_builder_new ();
-
-        if (!gtk_builder_add_from_resource (builder, "/org/gnome/devhelp/devhelp.ui", &error)) {
-                g_error ("%s",error ? error->message : "unknown error");
-                g_clear_error (&error);
-        }
-
-        model = G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu"));
-        gtk_application_set_app_menu (GTK_APPLICATION (app), model);
-
-        g_object_unref (builder);
-}
-
-static void
 startup (GApplication *application)
 {
         DhApp *app = DH_APP (application);
@@ -327,16 +322,37 @@ startup (GApplication *application)
         G_APPLICATION_CLASS (dh_app_parent_class)->startup (application);
 
         /* Setup actions */
-        setup_actions (app);
+        g_action_map_add_action_entries (G_ACTION_MAP (app),
+                                         app_entries, G_N_ELEMENTS (app_entries),
+                                         app);
+
+        if (_dh_app_has_app_menu (app)) {
+                GtkBuilder *builder;
+                GError *error = NULL;
+
+                /* Setup menu */
+                builder = gtk_builder_new ();
+
+                if (!gtk_builder_add_from_resource (builder,
+                                                    "/org/gnome/devhelp/devhelp-menu.ui",
+                                                    &error)) {
+                        g_warning ("loading menu builder file: %s", error->message);
+                        g_error_free (error);
+                } else {
+                        GMenuModel *app_menu;
 
-        /* Setup menu */
-        setup_menu (app);
+                        app_menu = G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu"));
+                        gtk_application_set_app_menu (GTK_APPLICATION (application),
+                                                      app_menu);
+                }
+
+                g_object_unref (builder);
+        }
 
         /* Setup accelerators */
         setup_accelerators (app);
 
         /* Load the book manager */
-        g_assert (priv->book_manager == NULL);
         priv->book_manager = dh_book_manager_new ();
         dh_book_manager_populate (priv->book_manager);
 }
diff --git a/src/dh-app.h b/src/dh-app.h
index e6deed0..735dae8 100644
--- a/src/dh-app.h
+++ b/src/dh-app.h
@@ -61,6 +61,8 @@ void           dh_app_search_assistant  (DhApp *self,
                                          const gchar *keyword);
 void           dh_app_raise             (DhApp *self);
 
+gboolean      _dh_app_has_app_menu      (DhApp *app);
+
 G_END_DECLS
 
 #endif /* __DH_APP_H__ */
diff --git a/src/dh.gresource.xml b/src/dh.gresource.xml
index 8ff708c..71674ab 100644
--- a/src/dh.gresource.xml
+++ b/src/dh.gresource.xml
@@ -2,6 +2,7 @@
 <gresources>
   <gresource prefix="/org/gnome/devhelp">
     <file>devhelp.ui</file>
+    <file preprocess="xml-stripblanks">devhelp-menu.ui</file>
     <file preprocess="xml-stripblanks">dh-assistant.ui</file>
     <file preprocess="xml-stripblanks">dh-window.ui</file>
   </gresource>


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