← Back to team overview

simple-scan-team team mailing list archive

[Merge] lp:~moimael/simple-scan/app-menu into lp:simple-scan

 

moimael has proposed merging lp:~moimael/simple-scan/app-menu into lp:simple-scan.

Requested reviews:
  Robert Ancell (robert-ancell)

For more details, see:
https://code.launchpad.net/~moimael/simple-scan/app-menu/+merge/200076

Add an app menu for gnome-shell.
-- 
https://code.launchpad.net/~moimael/simple-scan/app-menu/+merge/200076
Your team Simple Scan Development Team is subscribed to branch lp:simple-scan.
=== modified file 'data/Makefile.am'
--- data/Makefile.am	2013-09-03 00:57:48 +0000
+++ data/Makefile.am	2013-12-26 16:07:01 +0000
@@ -16,6 +16,7 @@
 EXTRA_DIST = simple-scan.desktop.in \
              org.gnome.SimpleScan.gschema.xml.in \
              simple-scan.ui \
+             simple-scan-menu.ui \
              simple-scan.gresource.xml
 
 DISTCLEANFILES = \

=== added file 'data/simple-scan-menu.ui'
--- data/simple-scan-menu.ui	1970-01-01 00:00:00 +0000
+++ data/simple-scan-menu.ui	2013-12-26 16:07:01 +0000
@@ -0,0 +1,56 @@
+<?xml version="1.0"?>
+<interface>
+  <!-- interface-requires gtk+ 3.8 -->
+  <menu id="appmenu">
+    <section>
+      <item>
+        <attribute name="label" translatable="yes">_New Document</attribute>
+        <attribute name="action">app.new_document</attribute>
+      </item>
+    </section>
+    <section>
+      <submenu>
+        <attribute name="label" translatable="yes">_Document</attribute>
+        <section>
+          <item>
+            <attribute name="label" translatable="yes">_Save</attribute>
+            <attribute name="action">app.save</attribute>
+          </item>
+          <item>
+            <attribute name="label" translatable="yes">_Save As...</attribute>
+            <attribute name="action">app.save_as</attribute>
+          </item>
+          <item>
+            <attribute name="label" translatable="yes">_Email...</attribute>
+            <attribute name="action">app.email</attribute>
+          </item>
+          <item>
+            <attribute name="label" translatable="yes">_Print...</attribute>
+            <attribute name="action">app.print</attribute>
+          </item>
+        </section>
+      </submenu>
+    </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>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">_About Simple Scan</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"><![CDATA[<Ctrl>Q]]></attribute>
+      </item>
+    </section>
+  </menu>
+</interface>

=== modified file 'data/simple-scan.gresource.xml'
--- data/simple-scan.gresource.xml	2013-04-18 08:49:12 +0000
+++ data/simple-scan.gresource.xml	2013-12-26 16:07:01 +0000
@@ -2,5 +2,6 @@
 <gresources>
   <gresource prefix="/org/gnome/SimpleScan">
     <file preprocess="xml-stripblanks">simple-scan.ui</file>
+    <file preprocess="xml-stripblanks">simple-scan-menu.ui</file>
   </gresource>
 </gresources>

=== modified file 'data/simple-scan.ui'
--- data/simple-scan.ui	2013-12-17 21:00:01 +0000
+++ data/simple-scan.ui	2013-12-26 16:07:01 +0000
@@ -240,7 +240,7 @@
     <property name="step_increment">1</property>
     <property name="page_increment">10</property>
   </object>
-  <object class="GtkWindow" id="simple_scan_window">
+  <object class="GtkApplicationWindow" id="simple_scan_window">
     <property name="can_focus">False</property>
     <property name="title" translatable="yes" comments="Title of scan window">Simple Scan</property>
     <property name="icon_name">scanner</property>

=== modified file 'src/ui.vala'
--- src/ui.vala	2013-11-24 21:28:01 +0000
+++ src/ui.vala	2013-12-26 16:07:01 +0000
@@ -14,11 +14,26 @@
     private const int DEFAULT_TEXT_DPI = 150;
     private const int DEFAULT_PHOTO_DPI = 300;
 
+    private const GLib.ActionEntry[] action_entries =
+    {
+        { "new_document", new_document_activate_cb },
+        { "save", save_document_activate_cb },
+        { "save_as", save_as_document_activate_cb },
+        { "email", email_document_activate_cb },
+        { "print", print_document_activate_cb },
+        { "preferences", preferences_activate_cb },
+        { "help", help_contents_activate_cb },
+        { "about", about_activate_cb },
+        { "quit", quit_activate_cb }
+    };
+
     private Settings settings;
 
     private Gtk.Builder builder;
 
-    private Gtk.Window window;
+    private Gtk.ApplicationWindow window;
+    private GLib.MenuModel app_menu;
+    private Gtk.MenuBar menubar;
     private Gtk.Box main_vbox;
     private Gtk.InfoBar info_bar;
     private Gtk.Image info_bar_image;
@@ -607,8 +622,7 @@
         copy_to_clipboard_menuitem.set_sensitive (false);
     }
 
-    [CCode (cname = "G_MODULE_EXPORT new_button_clicked_cb", instance_pos = -1)]
-    public void new_button_clicked_cb (Gtk.Widget widget)
+    private void new_document ()
     {
         if (!prompt_to_save (/* Text in dialog warning when a document is about to be lost */
                              _("Save current document?"),
@@ -621,6 +635,17 @@
         clear_document ();
     }
 
+    [CCode (cname = "G_MODULE_EXPORT new_button_clicked_cb", instance_pos = -1)]
+    public void new_button_clicked_cb (Gtk.Widget widget)
+    {
+        new_document();
+    }
+
+    public void new_document_activate_cb ()
+    {
+        new_document();
+    }
+
     private void set_document_hint (string document_hint)
     {
         this.document_hint = document_hint;
@@ -793,6 +818,11 @@
         preferences_dialog.present ();
     }
 
+    public void preferences_activate_cb ()
+    {
+        preferences_dialog.present ();
+    }
+
     [CCode (cname = "G_MODULE_EXPORT preferences_dialog_delete_event_cb", instance_pos = -1)]
     public bool preferences_dialog_delete_event_cb (Gtk.Widget widget)
     {
@@ -1061,6 +1091,11 @@
         save_document (false);
     }
 
+    public void save_document_activate_cb ()
+    {
+        save_document (false);
+    }
+
     [CCode (cname = "G_MODULE_EXPORT copy_to_clipboard_button_clicked_cb", instance_pos = -1)]
     public void copy_to_clipboard_button_clicked_cb (Gtk.Widget widget)
     {
@@ -1075,6 +1110,11 @@
         save_document (true);
     }
 
+    public void save_as_document_activate_cb ()
+    {
+        save_document (true);
+    }
+
     private void draw_page (Gtk.PrintOperation operation,
                             Gtk.PrintContext   print_context,
                             int                page_number)
@@ -1106,8 +1146,12 @@
         email (document_hint, quality);
     }
 
-    [CCode (cname = "G_MODULE_EXPORT print_button_clicked_cb", instance_pos = -1)]
-    public void print_button_clicked_cb (Gtk.Widget widget)
+    public void email_document_activate_cb ()
+    {
+        email (document_hint, quality);
+    }
+    
+    private void print_document ()
     {
         var print = new Gtk.PrintOperation ();
         print.set_n_pages ((int) book.n_pages);
@@ -1125,8 +1169,18 @@
         print.draw_page.disconnect (draw_page);
     }
 
-    [CCode (cname = "G_MODULE_EXPORT help_contents_menuitem_activate_cb", instance_pos = -1)]
-    public void help_contents_menuitem_activate_cb (Gtk.Widget widget)
+    [CCode (cname = "G_MODULE_EXPORT print_button_clicked_cb", instance_pos = -1)]
+    public void print_button_clicked_cb (Gtk.Widget widget)
+    {
+        print_document ();
+    }
+
+    public void print_document_activate_cb ()
+    {
+        print_document ();
+    }
+
+    private void show_help ()
     {
         try
         {
@@ -1140,8 +1194,18 @@
         }
     }
 
-    [CCode (cname = "G_MODULE_EXPORT about_menuitem_activate_cb", instance_pos = -1)]
-    public void about_menuitem_activate_cb (Gtk.Widget widget)
+    [CCode (cname = "G_MODULE_EXPORT help_contents_menuitem_activate_cb", instance_pos = -1)]
+    public void help_contents_menuitem_activate_cb (Gtk.Widget widget)
+    {
+        show_help ();
+    }
+
+    public void help_contents_activate_cb ()
+    {
+        show_help ();
+    }
+
+    private void show_about ()
     {
         string[] authors = { "Robert Ancell <robert.ancell@xxxxxxxxxxxxx>" };
 
@@ -1169,6 +1233,17 @@
                                null);
     }
 
+    [CCode (cname = "G_MODULE_EXPORT about_menuitem_activate_cb", instance_pos = -1)]
+    public void about_menuitem_activate_cb (Gtk.Widget widget)
+    {
+        show_about ();
+    }
+
+    public void about_activate_cb ()
+    {
+        show_about ();
+    }
+
     private bool on_quit ()
     {
         if (!prompt_to_save (/* Text in dialog warning when a document is about to be lost */
@@ -1213,6 +1288,11 @@
         on_quit ();
     }
 
+    public void quit_activate_cb ()
+    {
+        on_quit ();
+    }
+
     [CCode (cname = "G_MODULE_EXPORT simple_scan_window_configure_event_cb", instance_pos = -1)]
     public bool simple_scan_window_configure_event_cb (Gtk.Widget widget, Gdk.EventConfigure event)
     {
@@ -1333,16 +1413,40 @@
         copy_to_clipboard_menuitem.set_sensitive (true);
     }
 
+    private bool simple_scan_has_app_menu (Gtk.Application app)
+    {
+        Gtk.Settings gtk_settings;
+        bool show_app_menu = false;
+        bool show_menubar = true;
+
+        /* 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 ();
+
+        gtk_settings.get ("gtk-shell-shows-app-menu", &show_app_menu, "gtk-shell-shows-menubar", &show_menubar, null);
+
+        return show_app_menu && !show_menubar;
+    }
+
     private void load ()
     {
         Gtk.IconTheme.get_default ().append_search_path (ICON_DIR);
 
         Gtk.Window.set_default_icon_name ("scanner");
 
+        var app = Application.get_default () as Gtk.Application;
+
         builder = new Gtk.Builder ();
         try
         {
             builder.add_from_resource ("/org/gnome/SimpleScan/simple-scan.ui");
+            if (simple_scan_has_app_menu (app))
+            {
+                builder.add_from_resource ("/org/gnome/SimpleScan/simple-scan-menu.ui");
+            }
         }
         catch (Error e)
         {
@@ -1355,9 +1459,16 @@
         }
         builder.connect_signals (this);
 
-        window = (Gtk.Window) builder.get_object ("simple_scan_window");
-        var app = Application.get_default () as Gtk.Application;
+        window = (Gtk.ApplicationWindow) builder.get_object ("simple_scan_window");
         app.add_window (window);
+        menubar = (Gtk.MenuBar) builder.get_object ("menubar");
+        if (simple_scan_has_app_menu (app))
+        {
+            app_menu = (GLib.MenuModel) builder.get_object ("appmenu");
+            app.add_action_entries (action_entries, this);
+            app.set_app_menu (app_menu);
+            menubar.set_visible(false);
+        }
         main_vbox = (Gtk.Box) builder.get_object ("main_vbox");
         page_move_left_menuitem = (Gtk.MenuItem) builder.get_object ("page_move_left_menuitem");
         page_move_right_menuitem = (Gtk.MenuItem) builder.get_object ("page_move_right_menuitem");
@@ -1590,7 +1701,7 @@
 {
     Gtk.ProgressBar bar;
 
-    public ProgressBarDialog (Gtk.Window parent, string title)
+    public ProgressBarDialog (Gtk.ApplicationWindow parent, string title)
     {
         bar = new Gtk.ProgressBar ();
         var hbox = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 5);


Follow ups