[gedit] Add app menu



commit 6d19a44951c9d772efb3dcfa73227f6d42985bb6
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Sun Dec 30 22:10:28 2012 +0100

    Add app menu
    
    Detect if we are running under gnome-shell and add items to the appmenu,
    otherwise merge them in the normal uimanager menu

 gedit/Makefile.am           |    1 +
 gedit/gedit-app.c           |  132 +++++++++++++++++++++++++++++++++++++++++++
 gedit/gedit-app.h           |    2 +
 gedit/gedit-menu.ui         |   36 ++++++++++++
 gedit/gedit-ui-fallback.xml |   52 +++++++++++++++++
 gedit/gedit-ui.xml          |   10 ++-
 gedit/gedit-window.c        |   13 ++++
 gedit/gedit.gresource.xml   |    2 +
 po/POTFILES.in              |    1 +
 9 files changed, 245 insertions(+), 4 deletions(-)
---
diff --git a/gedit/Makefile.am b/gedit/Makefile.am
index 8a99a29..db07461 100644
--- a/gedit/Makefile.am
+++ b/gedit/Makefile.am
@@ -264,6 +264,7 @@ EXTRA_DIST = 				\
 	gedit.rc			\
 	gedit.gresource.xml		\
 	gedit-ui.xml			\
+	gedit-ui-fallback.xml		\
 	gedit-encodings-dialog.ui	\
 	gedit-preferences-dialog.ui	\
 	gedit-replace-dialog.ui		\
diff --git a/gedit/gedit-app.c b/gedit/gedit-app.c
index b14c847..a457de8 100644
--- a/gedit/gedit-app.c
+++ b/gedit/gedit-app.c
@@ -55,6 +55,8 @@
 #include "gedit-settings.h"
 #include "gedit-app-activatable.h"
 #include "gedit-plugins-engine.h"
+#include "gedit-commands.h"
+#include "gedit-preferences-dialog.h"
 
 #ifndef ENABLE_GVFS_METADATA
 #include "gedit-metadata-manager.h"
@@ -307,6 +309,88 @@ gedit_app_set_window_title_impl (GeditApp    *app,
 }
 
 static void
+new_window_activated (GSimpleAction *action,
+                      GVariant      *parameter,
+                      gpointer       user_data)
+{
+	GeditApp *app;
+	GeditWindow *window;
+
+	app = GEDIT_APP (user_data);
+	window = gedit_app_create_window (app, NULL);
+
+	gedit_debug_message (DEBUG_APP, "Show window");
+	gtk_widget_show (GTK_WIDGET (window));
+
+	gedit_debug_message (DEBUG_APP, "Create tab");
+	gedit_window_create_tab (window, TRUE);
+
+	gtk_window_present (GTK_WINDOW (window));
+}
+
+static void
+preferences_activated (GSimpleAction  *action,
+                       GVariant       *parameter,
+                       gpointer        user_data)
+{
+	GtkApplication *app;
+	GeditWindow *window;
+
+	app = GTK_APPLICATION (user_data);
+	window = GEDIT_WINDOW (gtk_application_get_active_window (app));
+
+	gedit_show_preferences_dialog (window);
+}
+
+static void
+help_activated (GSimpleAction *action,
+                GVariant      *parameter,
+                gpointer       user_data)
+{
+	GtkApplication *app;
+	GeditWindow *window;
+
+	app = GTK_APPLICATION (user_data);
+	window = GEDIT_WINDOW (gtk_application_get_active_window (app));
+
+	_gedit_cmd_help_contents (NULL, window);
+}
+
+static void
+about_activated (GSimpleAction  *action,
+                 GVariant       *parameter,
+                 gpointer        user_data)
+{
+	GtkApplication *app;
+	GeditWindow *window;
+
+	app = GTK_APPLICATION (user_data);
+	window = GEDIT_WINDOW (gtk_application_get_active_window (app));
+
+	_gedit_cmd_help_about (NULL, window);
+}
+
+static void
+quit_activated (GSimpleAction *action,
+                GVariant      *parameter,
+                gpointer       user_data)
+{
+	GApplication *app;
+
+	app = G_APPLICATION (user_data);
+
+	g_application_quit (app);
+}
+
+static GActionEntry app_entries[] = {
+	{ "new_window", new_window_activated, NULL, NULL, NULL },
+	{ "preferences", preferences_activated, NULL, NULL, NULL },
+	{ "help", help_activated, NULL, NULL, NULL },
+	{ "about", about_activated, NULL, NULL, NULL },
+	{ "quit", quit_activated, NULL, NULL, NULL }
+};
+
+static void
 extension_added (PeasExtensionSet *extensions,
 		 PeasPluginInfo   *info,
 		 PeasExtension    *exten,
@@ -379,6 +463,37 @@ gedit_app_startup (GApplication *application)
 	/* initial lockdown state */
 	app->priv->lockdown = gedit_settings_get_lockdown (GEDIT_SETTINGS (app->priv->settings));
 
+	/* app menu */
+	if (_gedit_app_has_app_menu (app))
+	{
+		GtkBuilder *builder;
+		GError *error = NULL;
+
+		g_action_map_add_action_entries (G_ACTION_MAP (app),
+			                         app_entries,
+			                         G_N_ELEMENTS (app_entries),
+			                         app);
+
+		builder = gtk_builder_new ();
+		if (!gtk_builder_add_from_resource (builder,
+		                                    "/org/gnome/gedit/ui/gedit-menu.ui",
+		                                    &error))
+		{
+			g_warning ("loading menu builder file: %s", error->message);
+			g_error_free (error);
+		}
+		else
+		{
+			GMenuModel *app_menu;
+
+			app_menu = G_MENU_MODEL (gtk_builder_get_object (builder, "appmenu"));
+			gtk_application_set_app_menu (GTK_APPLICATION (application),
+			                              app_menu);
+		}
+
+		g_object_unref (builder);
+	}
+
 	/*
 	 * We use the default gtksourceview style scheme manager so that plugins
 	 * can obtain it easily without a gedit specific api, but we need to
@@ -1420,4 +1535,21 @@ _gedit_app_get_settings (GeditApp *app)
 	return app->priv->settings;
 }
 
+gboolean
+_gedit_app_has_app_menu (GeditApp *app)
+{
+	GtkSettings *gtk_settings;
+	gboolean show_app_menu;
+
+	g_return_val_if_fail (GEDIT_IS_APP (app), FALSE);
+
+	gtk_settings = gtk_settings_get_default ();
+	g_object_get (G_OBJECT (gtk_settings),
+	              "gtk-shell-shows-app-menu",
+	              &show_app_menu,
+	              NULL);
+
+	return show_app_menu;
+}
+
 /* ex:set ts=8 noet: */
diff --git a/gedit/gedit-app.h b/gedit/gedit-app.h
index fd9d53d..03baa3e 100644
--- a/gedit/gedit-app.h
+++ b/gedit/gedit-app.h
@@ -144,6 +144,8 @@ void			 _gedit_app_set_default_print_settings	(GeditApp         *app,
 
 GObject			*_gedit_app_get_settings		(GeditApp  *app);
 
+gboolean		 _gedit_app_has_app_menu		(GeditApp  *app);
+
 G_END_DECLS
 
 #endif  /* __GEDIT_APP_H__  */
diff --git a/gedit/gedit-menu.ui b/gedit/gedit-menu.ui
new file mode 100644
index 0000000..11da064
--- /dev/null
+++ b/gedit/gedit-menu.ui
@@ -0,0 +1,36 @@
+<?xml version="1.0"?>
+<interface>
+  <!-- interface-requires gtk+ 3.0 -->
+  <menu id="appmenu">
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">_New Window</attribute>
+        <attribute name="action">app.new_window</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">_Help</attribute>
+        <attribute name="action">app.help</attribute>
+        <attribute name="accel">F1</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">_About</attribute>
+        <attribute name="action">app.about</attribute>
+      </item>
+    </section>
+    <section>
+      <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/gedit/gedit-ui-fallback.xml b/gedit/gedit-ui-fallback.xml
new file mode 100644
index 0000000..5ff2eda
--- /dev/null
+++ b/gedit/gedit-ui-fallback.xml
@@ -0,0 +1,52 @@
+<!--
+ * gedit-ui.xml
+ * This file is part of gedit
+ *
+ * Copyright (C) 2005 - Paolo Maggi 
+ *
+ * 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.
+ *
+ * Modified by the gedit Team, 2005. See the AUTHORS file for a 
+ * list of people on the gedit Team.  
+ * See the ChangeLog files for a list of changes. 
+ *
+ * $Id$
+-->
+
+<ui>
+
+  <menubar name="MenuBar">
+    <menu name="FileMenu" action="File">
+      <placeholder name="FileQuitMenuHolder">
+        <menuitem name="FileQuitMenu" action="FileQuit"/>
+      </placeholder>
+    </menu>
+
+    <menu name="EditMenu" action="Edit">
+      <placeholder name="EditPreferencesMenuHolder">
+        <menuitem name="EditPreferencesMenu" action="EditPreferences"/>
+      </placeholder>
+    </menu>
+
+    <menu name="HelpMenu" action="Help">
+      <placeholder name="HelpHolder">
+        <menuitem name="HelpContentsMenu" action="HelpContents"/>
+        <menuitem name="HelpAboutMenu" action="HelpAbout"/>
+      </placeholder>
+    </menu>
+  </menubar>
+
+</ui>
diff --git a/gedit/gedit-ui.xml b/gedit/gedit-ui.xml
index 2569172..19a8f7f 100644
--- a/gedit/gedit-ui.xml
+++ b/gedit/gedit-ui.xml
@@ -49,7 +49,7 @@
       </placeholder>
       <separator/>
       <menuitem name="FileCloseMenu" action="FileClose"/>
-      <menuitem name="FileQuitMenu" action="FileQuit"/>
+      <placeholder name="FileQuitMenuHolder" />
     </menu>
 
     <menu name="EditMenu" action="Edit">
@@ -72,7 +72,7 @@
       <separator/>
       <placeholder name="EditOps_6" />
       <separator/>
-      <menuitem name="EditPreferencesMenu" action="EditPreferences"/>
+      <placeholder name="EditPreferencesMenuHolder" />
     </menu>
 
     <menu name="ViewMenu" action="View">
@@ -150,9 +150,11 @@
     </menu>
 
     <menu name="HelpMenu" action="Help">
-      <menuitem name="HelpContentsMenu" action="HelpContents"/>
-      <menuitem name="HelpAboutMenu" action="HelpAbout"/>
+     <placeholder name="HelpOps_1" />
+     <separator/>
+     <placeholder name="HelpHolder" />
     </menu>
+
   </menubar>
 
   <toolbar name="ToolBar">
diff --git a/gedit/gedit-window.c b/gedit/gedit-window.c
index cf9fa73..8a1a627 100644
--- a/gedit/gedit-window.c
+++ b/gedit/gedit-window.c
@@ -1576,6 +1576,19 @@ create_menu_bar_and_toolbar (GeditWindow *window,
 		return;
 	}
 
+	if (!_gedit_app_has_app_menu (GEDIT_APP (g_application_get_default ())))
+	{
+		gtk_ui_manager_add_ui_from_resource (manager,
+						     "/org/gnome/gedit/ui/gedit-ui-fallback.xml",
+						     &error);
+		if (error != NULL)
+		{
+			g_warning ("Could not add fallback ui definition: %s", error->message);
+			g_error_free (error);
+			return;
+		}
+	}
+
 	/* show tooltips in the statusbar */
 	g_signal_connect (manager,
 			  "connect_proxy",
diff --git a/gedit/gedit.gresource.xml b/gedit/gedit.gresource.xml
index 11db256..d391d3a 100644
--- a/gedit/gedit.gresource.xml
+++ b/gedit/gedit.gresource.xml
@@ -2,6 +2,8 @@
 <gresources>
   <gresource prefix="/org/gnome/gedit/ui">
     <file preprocess="xml-stripblanks">gedit-ui.xml</file>
+    <file preprocess="xml-stripblanks">gedit-ui-fallback.xml</file>
+    <file preprocess="xml-stripblanks">gedit-menu.ui</file>
     <file preprocess="xml-stripblanks">gedit-encodings-dialog.ui</file>
     <file preprocess="xml-stripblanks">gedit-preferences-dialog.ui</file>
     <file preprocess="xml-stripblanks">gedit-replace-dialog.ui</file>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 8df7b08..597151d 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -20,6 +20,7 @@ gedit/gedit-encodings-dialog.c
 [type: gettext/glade]gedit/gedit-encodings-dialog.ui
 gedit/gedit-file-chooser-dialog.c
 gedit/gedit-io-error-info-bar.c
+[type: gettext/glade]gedit/gedit-menu.ui
 gedit/gedit-notebook.c
 gedit/gedit-notebook-popup-menu.c
 gedit/gedit-open-tool-button.c



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