[five-or-more] Replace the menu bar with an application menu



commit 24c534d3a8b1ab068dd510a6b1cbec75cf3fb325
Author: Michael Catanzaro <mike catanzaro gmail com>
Date:   Sun Aug 4 18:22:09 2013 -0500

    Replace the menu bar with an application menu

 data/Makefile.am     |    3 +-
 data/five-or-more.ui |  165 +-------------------------------------------------
 src/glines.c         |   66 ++++++++++++++++----
 src/glines.h         |   26 ++++++---
 4 files changed, 77 insertions(+), 183 deletions(-)
---
diff --git a/data/Makefile.am b/data/Makefile.am
index a7ccc9c..1709282 100644
--- a/data/Makefile.am
+++ b/data/Makefile.am
@@ -3,7 +3,8 @@ SUBDIRS = icons
 uidir = $(datadir)/five-or-more/
 ui_DATA = \
        five-or-more.ui \
-       five-or-more-preferences.ui
+       five-or-more-preferences.ui \
+       menu.ui
 
 themedir = $(datadir)/five-or-more/themes
 theme_DATA = \
diff --git a/data/five-or-more.ui b/data/five-or-more.ui
index 859169d..b6209cc 100644
--- a/data/five-or-more.ui
+++ b/data/five-or-more.ui
@@ -2,9 +2,9 @@
 <interface>
   <!-- interface-requires gtk+ 3.0 -->
   <object class="GtkAccelGroup" id="accelgroup"/>
-  <object class="GtkWindow" id="glines_window">
+  <object class="GtkApplicationWindow" id="glines_window">
     <property name="can_focus">False</property>
-    <property name="title" translatable="yes">Five or more</property>
+    <property name="title" translatable="yes">Five or More</property>
     <property name="default_width">320</property>
     <property name="default_height">400</property>
     <property name="icon_name">glines</property>
@@ -17,161 +17,7 @@
         <property name="can_focus">False</property>
         <property name="orientation">vertical</property>
         <child>
-          <object class="GtkMenuBar" id="menubar">
-            <property name="visible">True</property>
-            <property name="can_focus">False</property>
-            <child>
-              <object class="GtkMenuItem" id="game_menu_item">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="use_action_appearance">False</property>
-                <property name="label" translatable="yes">_Game</property>
-                <property name="use_underline">True</property>
-                <child type="submenu">
-                  <object class="GtkMenu" id="menu1">
-                    <property name="visible">True</property>
-                    <property name="can_focus">False</property>
-                    <child>
-                      <object class="GtkImageMenuItem" id="game_new_menuitem">
-                        <property name="label">gtk-new</property>
-                        <property name="visible">True</property>
-                        <property name="can_focus">False</property>
-                        <property name="use_action_appearance">False</property>
-                        <property name="use_underline">True</property>
-                        <property name="use_stock">True</property>
-                        <property name="accel_group">accelgroup</property>
-                        <accelerator key="N" signal="activate" modifiers="GDK_CONTROL_MASK"/>
-                        <signal name="activate" handler="game_new_callback" swapped="no"/>
-                      </object>
-                    </child>
-                    <child>
-                      <object class="GtkSeparatorMenuItem" id="separatormenuitem1">
-                        <property name="visible">True</property>
-                        <property name="can_focus">False</property>
-                        <property name="use_action_appearance">False</property>
-                      </object>
-                    </child>
-                    <child>
-                      <object class="GtkImageMenuItem" id="scores_menuitem">
-                        <property name="label" translatable="yes">Scores</property>
-                        <property name="visible">True</property>
-                        <property name="can_focus">False</property>
-                        <property name="use_action_appearance">False</property>
-                        <property name="image">image1</property>
-                        <property name="use_stock">False</property>
-                        <property name="accel_group">accelgroup</property>
-                        <signal name="activate" handler="game_top_ten_callback" swapped="no"/>
-                      </object>
-                    </child>
-                    <child>
-                      <object class="GtkSeparatorMenuItem" id="separatormenuitem2">
-                        <property name="visible">True</property>
-                        <property name="can_focus">False</property>
-                        <property name="use_action_appearance">False</property>
-                      </object>
-                    </child>
-                    <child>
-                      <object class="GtkImageMenuItem" id="quit_menuitem">
-                        <property name="label">gtk-quit</property>
-                        <property name="visible">True</property>
-                        <property name="can_focus">False</property>
-                        <property name="use_action_appearance">False</property>
-                        <property name="use_underline">True</property>
-                        <property name="use_stock">True</property>
-                        <property name="accel_group">accelgroup</property>
-                        <accelerator key="Q" signal="activate" modifiers="GDK_CONTROL_MASK"/>
-                        <signal name="activate" handler="game_quit_callback" swapped="no"/>
-                      </object>
-                    </child>
-                  </object>
-                </child>
-              </object>
-            </child>
-            <child>
-              <object class="GtkMenuItem" id="settings_menu_item">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="use_action_appearance">False</property>
-                <property name="label" translatable="yes">_Settings</property>
-                <property name="use_underline">True</property>
-                <child type="submenu">
-                  <object class="GtkMenu" id="menu2">
-                    <property name="visible">True</property>
-                    <property name="can_focus">False</property>
-                    <child>
-                      <object class="GtkImageMenuItem" id="fullscreen_menuitem">
-                        <property name="label">gtk-fullscreen</property>
-                        <property name="visible">True</property>
-                        <property name="can_focus">False</property>
-                        <property name="use_action_appearance">False</property>
-                        <property name="use_underline">True</property>
-                        <property name="use_stock">True</property>
-                        <property name="accel_group">accelgroup</property>
-                        <accelerator key="F11" signal="activate"/>
-                      </object>
-                    </child>
-                    <child>
-                      <object class="GtkImageMenuItem" id="preferences_menuitem">
-                        <property name="label">gtk-preferences</property>
-                        <property name="visible">True</property>
-                        <property name="can_focus">False</property>
-                        <property name="use_action_appearance">False</property>
-                        <property name="use_underline">True</property>
-                        <property name="use_stock">True</property>
-                        <property name="accel_group">accelgroup</property>
-                        <signal name="activate" handler="game_props_callback" swapped="no"/>
-                      </object>
-                    </child>
-                  </object>
-                </child>
-              </object>
-            </child>
-            <child>
-              <object class="GtkMenuItem" id="help_menu_item">
-                <property name="visible">True</property>
-                <property name="can_focus">False</property>
-                <property name="use_action_appearance">False</property>
-                <property name="label" translatable="yes">_Help</property>
-                <property name="use_underline">True</property>
-                <child type="submenu">
-                  <object class="GtkMenu" id="menu3">
-                    <property name="visible">True</property>
-                    <property name="can_focus">False</property>
-                    <child>
-                      <object class="GtkImageMenuItem" id="help_item">
-                        <property name="label" translatable="yes" comments="Help contents menu 
item">_Contents</property>
-                        <property name="visible">True</property>
-                        <property name="can_focus">False</property>
-                        <property name="use_action_appearance">False</property>
-                        <property name="use_underline">True</property>
-                        <property name="use_stock">False</property>
-                        <property name="accel_group">accelgroup</property>
-                        <accelerator key="F1" signal="activate"/>
-                        <signal name="activate" handler="game_help_callback" swapped="no"/>
-                      </object>
-                    </child>
-                    <child>
-                      <object class="GtkImageMenuItem" id="about_menuitem">
-                        <property name="label">gtk-about</property>
-                        <property name="visible">True</property>
-                        <property name="can_focus">False</property>
-                        <property name="use_action_appearance">False</property>
-                        <property name="use_underline">True</property>
-                        <property name="use_stock">True</property>
-                        <property name="accel_group">accelgroup</property>
-                        <signal name="activate" handler="game_about_callback" swapped="no"/>
-                      </object>
-                    </child>
-                  </object>
-                </child>
-              </object>
-            </child>
-          </object>
-          <packing>
-            <property name="expand">False</property>
-            <property name="fill">True</property>
-            <property name="position">0</property>
-          </packing>
+          <placeholder/>
         </child>
         <child>
           <object class="GtkBox" id="top_box">
@@ -253,9 +99,4 @@
       </object>
     </child>
   </object>
-  <object class="GtkImage" id="image1">
-    <property name="visible">True</property>
-    <property name="can_focus">False</property>
-    <property name="stock">gtk-missing-image</property>
-  </object>
 </interface>
diff --git a/src/glines.c b/src/glines.c
index 1e106bb..51956ad 100644
--- a/src/glines.c
+++ b/src/glines.c
@@ -455,14 +455,16 @@ draw_preview (void)
 }
 
 void
-game_new_callback (void)
+game_new_callback (GSimpleAction *action,
+                   GVariant *parameter,
+                   gpointer user_data)
 {
   reset_game ();
   start_game ();
 }
 
 static void
-show_scores (gint pos, gboolean new_game)
+show_scores (gint pos)
 {
   static GtkWidget *dialog;
 
@@ -488,7 +490,7 @@ game_over (void)
 
   set_statusbar_message (_("Game Over!"));
   pos = games_scores_add_plain_score (highscores, score);
-  show_scores (pos, TRUE);
+  show_scores (pos);
   return;
 }
 
@@ -1150,13 +1152,17 @@ animate (gpointer gp)
 }
 
 void
-game_top_ten_callback (GtkAction * action, gpointer data)
+game_top_ten_callback (GSimpleAction *action,
+                       GVariant *parameter,
+                       gpointer user_data)
 {
-  show_scores (0, FALSE);
+  show_scores (0);
 }
 
 void
-game_about_callback (GtkAction * action, gpointer * data)
+game_about_callback (GSimpleAction *action,
+                     GVariant *parameter,
+                     gpointer user_data)
 {
   const gchar *authors[] = { "Robert Szokovacs <szo appaloosacorp hu>",
     "Szabolcs B\xc3\xa1n <shooby gnome hu>",
@@ -1342,7 +1348,9 @@ pref_dialog_response (GtkDialog * dialog, gint response, gpointer data)
 }
 
 void
-game_props_callback (void)
+game_props_callback (GSimpleAction *action,
+                     GVariant *parameter,
+                     gpointer user_data)
 {
   gchar *ui_path;
   GError *error = NULL;
@@ -1408,15 +1416,18 @@ game_props_callback (void)
   gtk_window_present (GTK_WINDOW (pref_dialog));
 }
 
-int
-game_quit_callback (GtkAction * action, gpointer data)
+void
+game_quit_callback (GSimpleAction *action,
+                    GVariant *parameter,
+                    gpointer user_data)
 {
   gtk_widget_destroy (app);
-  return FALSE;
 }
 
 void
-game_help_callback (GtkAction * action, gpointer data)
+game_help_callback (GSimpleAction *action,
+                    GVariant *parameter,
+                    gpointer user_data)
 {
   GError *error = NULL;
 
@@ -1525,6 +1536,19 @@ startup_cb (GApplication *application)
   guint i;
   GError *error = NULL;
 
+  GActionEntry app_actions[] = {
+    { "new", game_new_callback },
+    { "scores", game_top_ten_callback },
+    { "preferences", game_props_callback },
+    { "help", game_help_callback },
+    { "about", game_about_callback },
+    { "quit", game_quit_callback }
+  };
+
+  g_action_map_add_action_entries (G_ACTION_MAP (application),
+                                   app_actions, G_N_ELEMENTS (app_actions),
+                                   application);
+
   settings = g_settings_new ("org.gnome.five-or-more");
 
   highscores = games_scores_new ("glines",
@@ -1537,9 +1561,21 @@ startup_cb (GApplication *application)
 
   games_stock_init ();
 
+  builder = gtk_builder_new ();
+
+  ui_path = g_build_filename (DATA_DIRECTORY, "menu.ui", NULL);
+  gtk_builder_add_from_file (builder, ui_path, &error);
+  g_free (ui_path);
+
+  if (error) {
+    g_critical ("Unable to load the application menu file: %s", error->message);
+    g_error_free (error);
+  } else {
+    gtk_application_set_app_menu (GTK_APPLICATION (application),
+                                  G_MENU_MODEL (gtk_builder_get_object (builder, "appmenu")));
+  }
 
   ui_path = g_build_filename (DATA_DIRECTORY, "five-or-more.ui", NULL);
-  builder = gtk_builder_new ();
   gtk_builder_add_from_file (builder, ui_path, &error);
   g_free (ui_path);
 
@@ -1646,6 +1682,12 @@ main (int argc, char *argv[])
 
   rgen = g_rand_new ();
 
+  /* 
+   * Required for proper app menu because the binary doesn't match the desktop file.
+   * Has to be before the call to g_option_context_parse.
+   */
+  g_set_prgname ("glines");
+
   context = g_option_context_new (NULL);
   g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
   g_option_context_add_group (context, gtk_get_option_group (TRUE));
diff --git a/src/glines.h b/src/glines.h
index 1084bf3..bfdcc1b 100644
--- a/src/glines.h
+++ b/src/glines.h
@@ -28,19 +28,29 @@ void init_preview (void);
 
 void draw_preview (void);
 
-void game_props_callback (void);
-
 int init_new_balls (int num, int prev);
 
 gint animate (gpointer gp);
 
 
-void game_new_callback (void);
-void game_top_ten_callback (GtkAction * action, gpointer data);
-int game_quit_callback (GtkAction * action, gpointer data);
-void game_props_callback (void);
-void game_help_callback (GtkAction * action, gpointer data);
-void game_about_callback (GtkAction * action, gpointer * data);
+void game_new_callback (GSimpleAction *action,
+                        GVariant *parameter,
+                        gpointer user_data);
+void game_top_ten_callback (GSimpleAction *action,
+                            GVariant *parameter,
+                            gpointer user_data);
+void game_quit_callback (GSimpleAction *action,
+                        GVariant *parameter,
+                        gpointer user_data);
+void game_props_callback (GSimpleAction *action,
+                          GVariant *parameter,
+                          gpointer user_data);
+void game_help_callback (GSimpleAction *action,
+                         GVariant *parameter,
+                         gpointer user_data);
+void game_about_callback (GSimpleAction *action,
+                          GVariant *parameter,
+                          gpointer user_data);
 
 void pref_dialog_response (GtkDialog * dialog, gint response, gpointer data);
 


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