[gucharmap] Add an application menu



commit 6e2a43851c9dfd65deb67caff3e06dd059ee40a0
Author: Florian MÃllner <fmuellner gnome org>
Date:   Wed May 9 05:19:31 2012 +0200

    Add an application menu
    
    Merge "File" and "Help" into the application menu. When the menu is
    displayed by the Shell, items from "View" are also shown in the app
    menu and the in-window menubar is hidden.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=674952

 gucharmap/gucharmap-menus.ui |   35 +++------
 gucharmap/gucharmap-window.c |   17 ++++
 gucharmap/main.c             |  185 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 212 insertions(+), 25 deletions(-)
---
diff --git a/gucharmap/gucharmap-menus.ui b/gucharmap/gucharmap-menus.ui
index 8083cd7..170f8dc 100644
--- a/gucharmap/gucharmap-menus.ui
+++ b/gucharmap/gucharmap-menus.ui
@@ -22,19 +22,18 @@
 -->
 
 <interface>
+  <menu id="app-menu">
+    <item><link name="section" id="shell-view-by"/>
+      <attribute name="label" translatable="yes">View By</attribute>
+    </item>
+    <item><link name="section" id="shell-view"></link></item>
+    <item><link name="section" id="shell-zoom"></link></item>
+    <item><link name="section" id="shell-find"></link></item>
+    <item><link name="section" id="general"></link></item>
+  </menu>
+
   <menu id="menubar">
     <submenu>
-      <attribute name="label" translatable="yes">_File</attribute>
-      <item><link name="section" id="printing"/></item>
-      <section>
-        <item>
-          <attribute name="label" translatable="yes">_Close</attribute>
-          <attribute name="action">win.close</attribute>
-          <attribute name="accel">&lt;Primary&gt;w</attribute>
-        </item>
-      </section>
-    </submenu>
-    <submenu>
       <attribute name="label" translatable="yes">_View</attribute>
       <section>
         <item>
@@ -112,19 +111,5 @@
       </section>
       <item><link name="section" id="go-chapter"/></item>
     </submenu>
-    <submenu>
-      <attribute name="label" translatable="yes">_Help</attribute>
-      <section>
-        <item>
-          <attribute name="label" translatable="yes">_Contents</attribute>
-          <attribute name="action">win.help</attribute>
-          <attribute name="accel">F1</attribute>
-        </item>
-        <item>
-          <attribute name="label" translatable="yes">_About</attribute>
-          <attribute name="action">win.about</attribute>
-        </item>
-      </section>
-    </submenu>
   </menu>
 </interface>
diff --git a/gucharmap/gucharmap-window.c b/gucharmap/gucharmap-window.c
index e547842..b0b7783 100644
--- a/gucharmap/gucharmap-window.c
+++ b/gucharmap/gucharmap-window.c
@@ -939,11 +939,28 @@ gucharmap_window_finalize (GObject *object)
   G_OBJECT_CLASS (gucharmap_window_parent_class)->finalize (object);
 }
 
+static GObject *
+gucharmap_window_constructor (GType                  type,
+                              guint                  n_construct_properties,
+                              GObjectConstructParam *construct_params)
+{
+  GObject *object;
+
+  object = G_OBJECT_CLASS (gucharmap_window_parent_class)->constructor (type, n_construct_properties, construct_params);
+  g_object_bind_property (gtk_settings_get_default (),
+                          "gtk-shell-shows-app-menu",
+                          object, "show-menubar",
+                          G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE | G_BINDING_INVERT_BOOLEAN);
+
+  return object;
+}
+
 static void
 gucharmap_window_class_init (GucharmapWindowClass *klass)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
+  object_class->constructor = gucharmap_window_constructor;
   object_class->finalize = gucharmap_window_finalize;
 }
 
diff --git a/gucharmap/main.c b/gucharmap/main.c
index b23ec9e..25293f2 100644
--- a/gucharmap/main.c
+++ b/gucharmap/main.c
@@ -1,6 +1,7 @@
 /*
  * Copyright  2004 Noah Levitt
  * Copyright  2007, 2008 Christian Persch
+ * Copyright  2012 Red Hat, Inc.
  *
  * 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
@@ -41,15 +42,187 @@ option_version_cb (const gchar *option_name,
   return FALSE;
 }
 
+static GAction *
+get_corresponding_window_action (GtkApplication *app,
+                                 GAction        *action)
+{
+  GList *windows = gtk_application_get_windows (app);
+  const char *name;
+
+  name = g_action_get_name (G_ACTION (action));
+  return g_action_map_lookup_action (G_ACTION_MAP (windows->data), name);
+}
+
+static void
+activate_action (GSimpleAction *action,
+                 GVariant      *parameter,
+                 gpointer       data)
+{
+  GAction *win_action = get_corresponding_window_action (GTK_APPLICATION (data),
+                                                         G_ACTION (action));
+  g_action_activate (win_action, parameter);
+
+  if (parameter)
+    g_action_change_state (G_ACTION (action), parameter);
+}
+
+static void
+activate_toggle_action (GSimpleAction *action,
+                        GVariant      *parameter,
+                        gpointer       data)
+{
+  GVariant *state = g_action_get_state (G_ACTION (action));
+  gboolean value = g_variant_get_boolean (state);
+  GAction *win_action;
+
+  win_action = get_corresponding_window_action (GTK_APPLICATION (data),
+                                                G_ACTION (action));
+  g_action_change_state (win_action, g_variant_new_boolean (!value));
+  g_action_change_state (G_ACTION (action), g_variant_new_boolean (!value));
+  g_variant_unref (state);
+}
+
+static void
+change_toggle_state (GSimpleAction *action,
+                     GVariant      *state,
+                     gpointer       data)
+{
+  g_simple_action_set_state (action, state);
+}
+
+static void
+activate_quit (GSimpleAction *action,
+               GVariant      *parameter,
+               gpointer       data)
+{
+  g_list_foreach (gtk_application_get_windows (GTK_APPLICATION (data)),
+                  (GFunc)gtk_widget_destroy, NULL);
+}
+
+static void
+update_shell_app_menu (GtkSettings *settings,
+                       GParamSpec  *pspec,
+                       gpointer data)
+{
+  GObject *app = G_OBJECT (data);
+  GMenu *menu;
+  gboolean show_app_menu;
+
+  g_object_get (G_OBJECT (settings),
+                "gtk-shell-shows-app-menu", &show_app_menu,
+                NULL);
+
+  menu = g_object_get_data (app, "shell-view-by-section");
+
+  while (g_menu_model_get_n_items (G_MENU_MODEL (menu)) > 0)
+    g_menu_remove (menu, 0);
+
+  if (show_app_menu)
+    {
+      g_menu_append (menu, _("Script"), "app.group-by::script");
+      g_menu_append (menu, _("Unicode Block"), "app.group-by::block");
+    }
+
+
+  menu = g_object_get_data (app, "shell-view-section");
+
+  while (g_menu_model_get_n_items (G_MENU_MODEL (menu)) > 0)
+    g_menu_remove (menu, 0);
+
+  if (show_app_menu)
+    {
+      g_menu_append (menu, _("Show only glyphs from this font"),
+                     "app.show-only-glyphs-in-font");
+    }
+
+
+  menu = g_object_get_data (app, "shell-zoom-section");
+
+  while (g_menu_model_get_n_items (G_MENU_MODEL (menu)) > 0)
+    g_menu_remove (menu, 0);
+
+  if (show_app_menu)
+    {
+      g_menu_append (menu, _("Zoom In"), "app.zoom-in");
+      g_menu_append (menu, _("Zoom Out"), "app.zoom-out");
+      g_menu_append (menu, _("Normal Size"), "app.normal-size");
+    }
+
+
+  menu = g_object_get_data (app, "shell-find-section");
+
+  while (g_menu_model_get_n_items (G_MENU_MODEL (menu)) > 0)
+    g_menu_remove (menu, 0);
+
+  if (show_app_menu)
+    {
+      g_menu_append (menu, _("Find\342\200\246"), "app.find");
+    }
+
+
+  menu = g_object_get_data (app, "general-section");
+
+  while (g_menu_model_get_n_items (G_MENU_MODEL (menu)) > 0)
+    g_menu_remove (menu, 0);
+
+  g_menu_append (menu, _("_Help"), "app.help");
+  g_menu_append (menu, _("_About Character Map"), "app.about");
+  g_menu_append (menu, show_app_menu ? _("_Quit") : _("_Close"), "app.quit");
+}
+
+
 static void
 startup_cb (GApplication *application,
             gpointer      data)
 {
   GtkBuilder *builder = gtk_builder_new ();
   GMenuModel *model;
+  const GActionEntry app_entries[] =
+  {
+    { "group-by", activate_action, "s", "\"script\"", NULL },
+
+    { "show-only-glyphs-in-font", activate_toggle_action, NULL, "false",
+      change_toggle_state },
+
+    { "zoom-in", activate_action, NULL, NULL, NULL },
+    { "zoom-out", activate_action, NULL, NULL, NULL },
+    { "normal-size", activate_action, NULL, NULL, NULL },
+
+    { "find", activate_action, NULL, NULL, NULL },
+
+    { "help", activate_action, NULL, NULL, NULL },
+    { "about", activate_action, NULL, NULL, NULL },
+    { "quit", activate_quit, NULL, NULL, NULL },
+  };
+
+  g_action_map_add_action_entries (G_ACTION_MAP (application),
+                                   app_entries, G_N_ELEMENTS (app_entries),
+                                   application);
 
   gtk_builder_add_from_resource (builder, UI_RESOURCE, NULL);
 
+  /* app menu */
+  model = G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu"));
+  gtk_application_set_app_menu (GTK_APPLICATION (application), model);
+
+  model = G_MENU_MODEL (gtk_builder_get_object (builder, "shell-view-by"));
+  g_object_set_data (G_OBJECT (application), "shell-view-by-section", model);
+
+  model = G_MENU_MODEL (gtk_builder_get_object (builder, "shell-view"));
+  g_object_set_data (G_OBJECT (application), "shell-view-section", model);
+
+  model = G_MENU_MODEL (gtk_builder_get_object (builder, "shell-zoom"));
+  g_object_set_data (G_OBJECT (application), "shell-zoom-section", model);
+
+  model = G_MENU_MODEL (gtk_builder_get_object (builder, "shell-find"));
+  g_object_set_data (G_OBJECT (application), "shell-find-section", model);
+
+  model = G_MENU_MODEL (gtk_builder_get_object (builder, "general"));
+  g_object_set_data (G_OBJECT (application), "general-section", model);
+
+
+  /* window menu */
+
 #ifdef ENABLE_PRINTING
   model = G_MENU_MODEL (gtk_builder_get_object (builder, "printing"));
 
@@ -70,6 +243,13 @@ startup_cb (GApplication *application,
   gtk_application_add_accelerator (GTK_APPLICATION (application),
                                    "<Primary>Page_Up", "win.previous-chapter",
                                    NULL);
+  gtk_application_add_accelerator (GTK_APPLICATION (application),
+                                   "F1", "app.help", NULL);
+  gtk_application_add_accelerator (GTK_APPLICATION (application),
+                                   "<Primary>q", "app.quit", NULL);
+  gtk_application_add_accelerator (GTK_APPLICATION (application),
+                                   "<Primary>w", "app.quit", NULL);
+
 
   g_object_unref (builder);
 }
@@ -135,6 +315,11 @@ main (int argc, char **argv)
 
   window = gucharmap_window_new (application);
 
+  g_signal_connect (gtk_widget_get_settings (window),
+                    "notify::gtk-shell-shows-app-menu",
+                    G_CALLBACK (update_shell_app_menu), application);
+  update_shell_app_menu (gtk_widget_get_settings (window), NULL, application);
+
   screen = gtk_window_get_screen (GTK_WINDOW (window));
   monitor = gdk_screen_get_monitor_at_point (screen, 0, 0);
 #if GTK_CHECK_VERSION (3, 3, 5)



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