[swell-foop/arnaudb/code-improvements: 1/9] Split main file.



commit 9904fa64f6b9988e061a20dc03b3f45e6ed0c245
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Fri May 1 22:36:13 2020 +0200

    Split main file.

 src/game-view.vala  |   2 +-
 src/meson.build     |   1 +
 src/swell-foop.vala | 317 +++--------------------------------------------
 src/window.vala     | 348 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 365 insertions(+), 303 deletions(-)
---
diff --git a/src/game-view.vala b/src/game-view.vala
index 5ca9b4b..262d2a2 100644
--- a/src/game-view.vala
+++ b/src/game-view.vala
@@ -322,7 +322,7 @@ public class GameView : Clutter.Group
         var play_again_button = new Gtk.Button.with_mnemonic (_("_Play Again"));
         play_again_button.width_request = 130;
         play_again_button.height_request = 40;
-        play_again_button.action_name = "app.new-game";
+        play_again_button.action_name = "win.new-game";
         play_again_button.show ();
 
         var style = play_again_button.get_style_context ();
diff --git a/src/meson.build b/src/meson.build
index 35865e7..12750a9 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -4,6 +4,7 @@ sources = [
   'game-view.vala',
   'score-dialog.vala',
   'swell-foop.vala',
+  'window.vala',
 ]
 
 deps = [
diff --git a/src/swell-foop.vala b/src/swell-foop.vala
index a565b51..90d1a5a 100644
--- a/src/swell-foop.vala
+++ b/src/swell-foop.vala
@@ -16,33 +16,17 @@ public class SwellFoop : Gtk.Application
     private Settings settings;
 
     /* Main window */
-    private Gtk.Window window;
-
-    /* Game history */
-    private History history;
-
-    /* Game being played */
-    private Game? game = null;
-
-    /* Rendering of game */
-    private GameView view;
-
-    private Clutter.Stage stage;
-    private GtkClutter.Embed clutter_embed;
+    private SwellFoopWindow window;
 
     private Gtk.Dialog? preferences_dialog = null;
 
-    private Gtk.HeaderBar headerbar;
-
-    private bool game_in_progress = false;
-
     /* Store size options */
-    public Size[] sizes;
+    internal Size [] sizes = {{ "small",  _("Small"),   6,  5 },
+                              { "normal", _("Normal"), 15, 10 },
+                              { "large",  _("Large"),  20, 15 }};
 
     private const GLib.ActionEntry[] action_entries =
     {
-        { "new-game",      new_game_cb    },
-        { "scores",        scores_cb      },
         { "preferences",   preferences_cb },
         { "help",          help_cb        },
         { "about",         about_cb       },
@@ -55,238 +39,28 @@ public class SwellFoop : Gtk.Application
         Object (application_id: "org.gnome.SwellFoop", flags: ApplicationFlags.FLAGS_NONE);
     }
 
-    private void load_css ()
-    {
-        var css_provider = new Gtk.CssProvider ();
-        var css_path = Path.build_filename (Config.DATADIR, "swell-foop.css");
-        try
-        {
-            css_provider.load_from_path (css_path);
-        }
-        catch (GLib.Error e)
-        {
-            warning ("Error loading css styles from %s: %s", css_path, e.message);
-        }
-        Gtk.StyleContext.add_provider_for_screen (Gdk.Screen.get_default (), css_provider, 
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
-    }
-
-    private Gtk.Stack build_first_run_stack ()
-    {
-        var stack = new Gtk.Stack ();
-        var first_vbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 12);
-        load_css ();
-        var logo = new Gtk.Image.from_icon_name ("org.gnome.SwellFoop", Gtk.IconSize.DIALOG);
-        logo.set_pixel_size (96);
-        first_vbox.pack_start (logo, false);
-        var label = new Gtk.Label (_("Welcome to Swell Foop"));
-        label.get_style_context ().add_class ("welcome");
-        first_vbox.pack_start (label, false);
-        label = new Gtk.Label (_("Clear as many blocks as you can.\nFewer clicks means more points."));
-        label.get_style_context ().add_class ("tip");
-        first_vbox.pack_start (label, false);
-        var play_button = new Gtk.Button.with_mnemonic (_("Let’s _Play"));
-        play_button.get_style_context ().add_class ("play");
-        play_button.get_style_context ().add_class ("suggested-action");
-        play_button.valign = Gtk.Align.CENTER;
-        play_button.halign = Gtk.Align.CENTER;
-        play_button.clicked.connect (() => {
-            /* FIXME: Currently, on Wayland, the game frame is displayed outside
-             * the window if there's a transition set. Uncomment these 2 lines
-             * when that's no longer a problem.
-             *
-             * stack.set_transition_type (Gtk.StackTransitionType.SLIDE_UP);
-             * stack.set_transition_duration (500);
-             */
-            stack.set_visible_child_name ("game");
-            window.key_press_event.connect (key_press_event_cb);
-            settings.set_boolean ("first-run", false);
-        });
-        first_vbox.pack_start (play_button, false);
-        first_vbox.halign = Gtk.Align.CENTER;
-        first_vbox.valign = Gtk.Align.CENTER;
-        stack.add_named (first_vbox, "first-run");
-        stack.set_visible_child_name ("first-run");
-        stack.show_all ();
-        return stack;
-    }
-
     protected override void startup ()
     {
         base.startup ();
 
-        Gtk.Settings.get_default ().set ("gtk-application-prefer-dark-theme", true);
+        Gtk.Settings.get_default ().@set ("gtk-application-prefer-dark-theme", true);
 
         settings = new Settings ("org.gnome.swell-foop");
 
         add_action_entries (action_entries, this);
-        set_accels_for_action ("app.new-game", {"<Primary>n"});
-        set_accels_for_action ("app.help", {"F1"});
-        set_accels_for_action ("app.quit", {"<Primary>q"});
+        set_accels_for_action ("win.new-game",  { "<Primary>n"  });
+        set_accels_for_action ("app.help",      {          "F1" });
+        set_accels_for_action ("app.quit",      { "<Primary>q"  });
 
         /* Create the main window */
-        window = new Gtk.ApplicationWindow (this);
-        window.set_title (_("Swell Foop"));
-        window.icon_name = "org.gnome.SwellFoop";
-        window.resizable = false;
-        window.set_events (window.get_events () | Gdk.EventMask.KEY_PRESS_MASK | 
Gdk.EventMask.KEY_RELEASE_MASK);
-
-        var vbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
-        vbox.show ();
-        window.add (vbox);
-
-        /* Create the menus */
-        var menu = new Menu ();
-        var section = new Menu ();
-        menu.append_section (null, section);
-        section.append (_("_New Game"), "app.new-game");
-        section = new Menu ();
-        menu.append_section (null, section);
-        section.append (_("High _Scores"), "app.scores");
-        section = new Menu ();
-        menu.append_section (null, section);
-        section.append (_("_Preferences"), "app.preferences");
-        section.append (_("_Help"), "app.help");
-        section.append (_("_About Swell Foop"), "app.about");
-
-        /* Create a headerbar */
-        headerbar = new Gtk.HeaderBar ();
-        headerbar.show ();
-        headerbar.title = _("Swell Foop");
-        headerbar.show_close_button = true;
-        window.set_titlebar (headerbar);
-
-        /* Add the primary menu button */
-        var primary_menu = new Gtk.MenuButton ();
-        primary_menu.show ();
-        primary_menu.set_image (new Gtk.Image.from_icon_name ("open-menu-symbolic", Gtk.IconSize.BUTTON));
-        primary_menu.set_menu_model (menu);
-        headerbar.pack_end (primary_menu);
-
-        /* show the current score */
-        update_score_cb (0);
-
-        /* Create a clutter renderer widget */
-        clutter_embed = new GtkClutter.Embed ();
-        clutter_embed.show ();
-        var first_run = settings.get_boolean ("first-run");
-
-        if (first_run)
-        {
-            var stack = build_first_run_stack ();
-            stack.add_named (clutter_embed, "game");
-            vbox.pack_start (stack, true, true);
-        }
-        else
-        {
-            vbox.pack_start (clutter_embed, true, true);
-            window.key_press_event.connect (key_press_event_cb);
-        }
-
-        stage = (Clutter.Stage) clutter_embed.get_stage ();
-        stage.background_color = Clutter.Color.from_string ("#000000");  /* background color is black */
-
-        /* Initialize the options for sizes */
-        sizes = new Size[3];
-        sizes[0] = { "small", _("Small"), 6, 5 };
-        sizes[1] = { "normal", _("Normal"), 15, 10 };
-        sizes[2] = { "large", _("Large"), 20, 15 };
-
-        /* Create an instance of game with initial values for row, column and color */
-        game = new Game (get_size ().rows, get_size ().columns, settings.get_int ("colors"));
-
-        /* Game score change will be sent to the main window and show in the score label */
-        game.update_score.connect (update_score_cb);
-        game.complete.connect (complete_cb);
-
-        /* Create an instance of game view. This follow the Model-View-Controller paradigm */
-        view = new GameView ();
-        /* Initialize the themes needed by actors */
-        view.theme_name = settings.get_string ("theme");
-        view.is_zealous = settings.get_boolean ("zealous");
-        view.game = game;
-        stage.add_child (view);
-        /* Request an appropriate size for the game view */
-        stage.set_size (view.width, view.height);
-        clutter_embed.set_size_request ((int) stage.width, (int) stage.height);
-
-        /* When the mouse leaves the window we need to update the view */
-        clutter_embed.leave_notify_event.connect (view.board_left_cb);
-
-        history = new History (Path.build_filename (Environment.get_user_data_dir (), "swell-foop", 
"history"));
-        history.load ();
-    }
-
-    private bool key_press_event_cb (Gtk.Widget widget, Gdk.EventKey event)
-    {
-        switch (event.keyval)
-        {
-            case Gdk.Key.F2:
-                new_game ();
-                break;
-            case Gdk.Key.Up:
-                view.cursor_move (0, 1);
-                break;
-            case Gdk.Key.Down:
-                view.cursor_move (0, -1);
-                break;
-            case Gdk.Key.Left:
-                view.cursor_move (-1, 0);
-                break;
-            case Gdk.Key.Right:
-                view.cursor_move (1, 0);
-                break;
-            case Gdk.Key.space:
-            case Gdk.Key.Return:
-                view.cursor_click ();
-                return true; //handle this one to avoid activating the toolbar button
-            default:
-                break;
-        }
-
-        return false;
-    }
-
-    private Size get_size ()
-    {
-        for (var i = 0; i < sizes.length; i++)
-        {
-            if (sizes[i].id == settings.get_string ("size"))
-                return sizes[i];
-        }
-
-        return sizes[0];
-    }
-
-    private void update_score_cb (int points_awarded)
-    {
-        var score = 0;
-        if (game != null)
-            score = game.score;
-
-        headerbar.subtitle = _("Score: %u").printf (score);
-    }
-
-    private void complete_cb ()
-    {
-        var date = new DateTime.now_local ();
-        var entry = new HistoryEntry (date, game.columns, game.rows, game.color_num, game.score);
-        history.add (entry);
-        history.save ();
-        game_in_progress = false;
-    }
-
-    private void started_cb ()
-    {
-        game_in_progress = true;
+        window = new SwellFoopWindow (this, settings);
+        add_window (window);
     }
 
     protected override void shutdown ()
     {
+        window.on_shutdown ();
         base.shutdown ();
-
-        /* Record the score if the game isn't over. */
-        if (game != null && !game.has_completed () && game.score > 0)
-            complete_cb ();
     }
 
     protected override void activate ()
@@ -366,14 +140,14 @@ public class SwellFoop : Gtk.Application
 
         settings.set_string ("theme", new_theme);
 
-        view.theme_name = new_theme;
+        window.set_theme_name (new_theme);
     }
 
     [CCode (cname = "G_MODULE_EXPORT set_zealous_animation", instance_pos = -1)]
     public void set_zealous_animation (Gtk.CheckButton button)
     {
         settings.set_boolean ("zealous", button.active);
-        view.is_zealous = settings.get_boolean ("zealous");
+        window.set_is_zealous (settings.get_boolean ("zealous"));
     }
 
     [CCode (cname = "G_MODULE_EXPORT update_size", instance_pos = -1)]
@@ -389,7 +163,7 @@ public class SwellFoop : Gtk.Application
             return;
 
         settings.set_string ("size", new_size);
-        new_game ();
+        window.new_game ();
     }
 
     [CCode (cname = "G_MODULE_EXPORT update_colors", instance_pos = -1)]
@@ -401,7 +175,7 @@ public class SwellFoop : Gtk.Application
             return;
 
         settings.set_int ("colors", new_colors);
-        new_game ();
+        window.new_game ();
     }
 
     private void preferences_response_cb ()
@@ -410,29 +184,6 @@ public class SwellFoop : Gtk.Application
         preferences_dialog = null;
     }
 
-    public void show ()
-    {
-        window.show ();
-    }
-
-    private void new_game_cb ()
-    {
-        if (game_in_progress)
-            show_new_game_confirmation_dialog ();
-        else
-            new_game ();
-    }
-
-    private void scores_cb ()
-    {
-        var dialog = new ScoreDialog (history);
-        dialog.modal = true;
-        dialog.transient_for = window;
-
-        dialog.run ();
-        dialog.destroy ();
-    }
-
     private void quit_cb ()
     {
         window.destroy ();
@@ -472,44 +223,6 @@ public class SwellFoop : Gtk.Application
                                null);
     }
 
-    public void new_game ()
-    {
-        game = new Game (get_size ().rows,
-                         get_size ().columns,
-                         settings.get_int ("colors"));
-        game.update_score.connect (update_score_cb);
-        game.complete.connect (complete_cb);
-        game.started.connect (started_cb);
-        view.theme_name = settings.get_string ("theme");
-        view.game = game;
-        view.is_zealous = settings.get_boolean ("zealous");
-
-        stage.set_size (view.width, view.height);
-        clutter_embed.set_size_request ((int) stage.width, (int) stage.height);
-
-        game_in_progress = false;
-
-        update_score_cb (0);
-    }
-
-    private void show_new_game_confirmation_dialog ()
-    {
-        var dialog = new Gtk.MessageDialog.with_markup (window,
-                                                        Gtk.DialogFlags.MODAL,
-                                                        Gtk.MessageType.QUESTION,
-                                                        Gtk.ButtonsType.NONE,
-                                                        "<span weight=\"bold\" size=\"larger\">%s</span>",
-                                                        _("Abandon this game to start a new one?"));
-        dialog.add_button (_("_Cancel"), Gtk.ResponseType.CANCEL);
-        dialog.add_button (_("_New Game"), Gtk.ResponseType.YES);
-
-        var result = dialog.run ();
-        dialog.destroy ();
-
-        if (result == Gtk.ResponseType.YES)
-            new_game ();
-    }
-
     public static int main (string[] args)
     {
         Intl.setlocale (LocaleCategory.ALL, "");
diff --git a/src/window.vala b/src/window.vala
new file mode 100644
index 0000000..84677e4
--- /dev/null
+++ b/src/window.vala
@@ -0,0 +1,348 @@
+/*
+   This file is part of Swell-Foop.
+
+   Copyright (C) 2020 Arnaud Bonatti <arnaud bonatti gmail com>
+
+   Swell-Foop 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.
+
+   Swell-Foop 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 Swell-Foop.  If not, see <https://www.gnu.org/licenses/>.
+*/
+
+using Gtk;
+
+private class SwellFoopWindow : ApplicationWindow
+{
+    private HeaderBar headerbar;
+    public GLib.Settings settings { private get; protected construct; }
+
+    /* Game history */
+    private History history;
+
+    /* Game being played */
+    private Game? game = null;
+
+    /* Rendering of game */
+    private GameView view;
+
+    private Clutter.Stage stage;
+    private GtkClutter.Embed clutter_embed;
+
+    private bool game_in_progress = false;
+
+    private const GLib.ActionEntry[] win_actions =
+    {
+        { "new-game",   new_game_cb },
+        { "scores",     scores_cb   }
+    };
+
+    construct
+    {
+        add_action_entries (win_actions, this);
+
+        set_title (_("Swell Foop"));
+        icon_name = "org.gnome.SwellFoop";
+        resizable = false;
+        add_events (Gdk.EventMask.KEY_PRESS_MASK | Gdk.EventMask.KEY_RELEASE_MASK);
+
+        var vbox = new Box (Orientation.VERTICAL, 0);
+        vbox.show ();
+        add (vbox);
+
+        /* Create the menus */
+        var menu = new GLib.Menu ();
+        var section = new GLib.Menu ();
+        menu.append_section (null, section);
+        section.append (_("_New Game"), "win.new-game");
+        section = new GLib.Menu ();
+        menu.append_section (null, section);
+        section.append (_("High _Scores"), "win.scores");
+        section = new GLib.Menu ();
+        menu.append_section (null, section);
+        section.append (_("_Preferences"), "app.preferences");
+        section.append (_("_Help"), "app.help");
+        section.append (_("_About Swell Foop"), "app.about");
+
+        /* Create a headerbar */
+        headerbar = new HeaderBar ();
+        headerbar.show ();
+        headerbar.title = _("Swell Foop");
+        headerbar.show_close_button = true;
+        set_titlebar (headerbar);
+
+        /* Add the primary menu button */
+        var primary_menu = new MenuButton ();
+        primary_menu.show ();
+        primary_menu.set_image (new Image.from_icon_name ("open-menu-symbolic", IconSize.BUTTON));
+        primary_menu.set_menu_model (menu);
+        headerbar.pack_end (primary_menu);
+
+        /* show the current score */
+        update_score_cb (0);
+
+        /* Create a clutter renderer widget */
+        clutter_embed = new GtkClutter.Embed ();
+        clutter_embed.show ();
+        var first_run = settings.get_boolean ("first-run");
+
+        if (first_run)
+        {
+            var stack = build_first_run_stack ();
+            stack.add_named (clutter_embed, "game");
+            vbox.pack_start (stack, true, true);
+        }
+        else
+        {
+            vbox.pack_start (clutter_embed, true, true);
+            key_press_event.connect (key_press_event_cb);
+        }
+    }
+
+    internal SwellFoopWindow (Gtk.Application application, GLib.Settings settings)
+    {
+        Object (application: application, settings: settings);
+
+        stage = (Clutter.Stage) clutter_embed.get_stage ();
+        stage.background_color = Clutter.Color.from_string ("#000000");  /* background color is black */
+
+        /* Create an instance of game with initial values for row, column and color */
+        game = new Game (get_board_size ().rows, get_board_size ().columns, settings.get_int ("colors"));
+
+        /* Game score change will be sent to the main window and show in the score label */
+        game.update_score.connect (update_score_cb);
+        game.complete.connect (complete_cb);
+
+        /* Create an instance of game view. This follow the Model-View-Controller paradigm */
+        view = new GameView ();
+        /* Initialize the themes needed by actors */
+        view.theme_name = settings.get_string ("theme");
+        view.is_zealous = settings.get_boolean ("zealous");
+        view.game = game;
+        stage.add_child (view);
+        /* Request an appropriate size for the game view */
+        stage.set_size (view.width, view.height);
+        clutter_embed.set_size_request ((int) stage.width, (int) stage.height);
+
+        /* When the mouse leaves the window we need to update the view */
+        clutter_embed.leave_notify_event.connect (view.board_left_cb);
+
+        history = new History (Path.build_filename (Environment.get_user_data_dir (), "swell-foop", 
"history"));
+        history.load ();
+    }
+
+    private Stack build_first_run_stack ()
+    {
+        var stack = new Stack ();
+        var first_vbox = new Box (Orientation.VERTICAL, 12);
+        load_css ();
+        var logo = new Image.from_icon_name ("org.gnome.SwellFoop", IconSize.DIALOG);
+        logo.set_pixel_size (96);
+        first_vbox.pack_start (logo, false);
+        var label = new Label (_("Welcome to Swell Foop"));
+        label.get_style_context ().add_class ("welcome");
+        first_vbox.pack_start (label, false);
+        label = new Label (_("Clear as many blocks as you can.\nFewer clicks means more points."));
+        label.get_style_context ().add_class ("tip");
+        first_vbox.pack_start (label, false);
+        var play_button = new Button.with_mnemonic (_("Let’s _Play"));
+        play_button.get_style_context ().add_class ("play");
+        play_button.get_style_context ().add_class ("suggested-action");
+        play_button.valign = Align.CENTER;
+        play_button.halign = Align.CENTER;
+        play_button.clicked.connect (() => {
+            /* FIXME: Currently, on Wayland, the game frame is displayed outside
+             * the window if there's a transition set. Uncomment these 2 lines
+             * when that's no longer a problem.
+             *
+             * stack.set_transition_type (StackTransitionType.SLIDE_UP);
+             * stack.set_transition_duration (500);
+             */
+            stack.set_visible_child_name ("game");
+            key_press_event.connect (key_press_event_cb);
+            settings.set_boolean ("first-run", false);
+        });
+        first_vbox.pack_start (play_button, false);
+        first_vbox.halign = Align.CENTER;
+        first_vbox.valign = Align.CENTER;
+        stack.add_named (first_vbox, "first-run");
+        stack.set_visible_child_name ("first-run");
+        stack.show_all ();
+        return stack;
+    }
+
+    private void load_css ()
+    {
+        var css_provider = new CssProvider ();
+        var css_path = Path.build_filename (Config.DATADIR, "swell-foop.css");
+        try
+        {
+            css_provider.load_from_path (css_path);
+        }
+        catch (GLib.Error e)
+        {
+            warning ("Error loading css styles from %s: %s", css_path, e.message);
+        }
+        StyleContext.add_provider_for_screen (Gdk.Screen.get_default (), css_provider, 
STYLE_PROVIDER_PRIORITY_APPLICATION);
+    }
+
+    /*\
+    * * various
+    \*/
+
+    private void update_score_cb (int points_awarded)
+    {
+        var score = 0;
+        if (game != null)
+            score = game.score;
+
+        headerbar.subtitle = _("Score: %u").printf (score);
+    }
+
+    private void complete_cb ()
+    {
+        var date = new DateTime.now_local ();
+        var entry = new HistoryEntry (date, game.columns, game.rows, game.color_num, game.score);
+        history.add (entry);
+        history.save ();
+        game_in_progress = false;
+    }
+
+    private void started_cb ()
+    {
+        game_in_progress = true;
+    }
+
+    private Size get_board_size ()
+    {
+        for (var i = 0; i < ((SwellFoop) application).sizes.length; i++)
+        {
+            if (((SwellFoop) application).sizes[i].id == settings.get_string ("size"))
+                return ((SwellFoop) application).sizes[i];
+        }
+
+        return ((SwellFoop) application).sizes[0];
+    }
+
+    /*\
+    * * internal calls
+    \*/
+
+    internal void new_game ()
+    {
+        game = new Game (get_board_size ().rows,
+                         get_board_size ().columns,
+                         settings.get_int ("colors"));
+        game.update_score.connect (update_score_cb);
+        game.complete.connect (complete_cb);
+        game.started.connect (started_cb);
+        view.theme_name = settings.get_string ("theme");
+        view.game = game;
+        view.is_zealous = settings.get_boolean ("zealous");
+
+        stage.set_size (view.width, view.height);
+        clutter_embed.set_size_request ((int) stage.width, (int) stage.height);
+
+        game_in_progress = false;
+
+        update_score_cb (0);
+    }
+
+    internal void set_theme_name (string new_theme)
+    {
+        view.theme_name = new_theme;
+    }
+
+    internal void set_is_zealous (bool is_zealous)
+    {
+        view.is_zealous = is_zealous;
+    }
+
+    internal void on_shutdown ()
+    {
+        /* Record the score if the game isn't over. */
+        if (game != null && !game.has_completed () && game.score > 0)
+            complete_cb ();
+    }
+
+    /*\
+    * * actions
+    \*/
+
+    private inline void scores_cb ()
+    {
+        var dialog = new ScoreDialog (history);
+        dialog.modal = true;
+        dialog.transient_for = this;
+
+        dialog.run ();
+        dialog.destroy ();
+    }
+
+    private inline void new_game_cb ()
+    {
+        if (game_in_progress)
+            show_new_game_confirmation_dialog ();
+        else
+            new_game ();
+    }
+
+    private void show_new_game_confirmation_dialog ()
+    {
+        var dialog = new MessageDialog.with_markup (this,
+                                                    DialogFlags.MODAL,
+                                                    MessageType.QUESTION,
+                                                    ButtonsType.NONE,
+                                                    "<span weight=\"bold\" size=\"larger\">%s</span>",
+                                                    _("Abandon this game to start a new one?"));
+        dialog.add_button (_("_Cancel"),    ResponseType.CANCEL);
+        dialog.add_button (_("_New Game"),  ResponseType.YES);
+
+        var result = dialog.run ();
+        dialog.destroy ();
+
+        if (result == ResponseType.YES)
+            new_game ();
+    }
+
+    /*\
+    * * keyboard
+    \*/
+
+    private bool key_press_event_cb (Widget widget, Gdk.EventKey event)
+    {
+        switch (event.keyval)
+        {
+            case Gdk.Key.F2:
+                new_game ();
+                break;
+            case Gdk.Key.Up:
+                view.cursor_move (0, 1);
+                break;
+            case Gdk.Key.Down:
+                view.cursor_move (0, -1);
+                break;
+            case Gdk.Key.Left:
+                view.cursor_move (-1, 0);
+                break;
+            case Gdk.Key.Right:
+                view.cursor_move (1, 0);
+                break;
+            case Gdk.Key.space:
+            case Gdk.Key.Return:
+                view.cursor_click ();
+                return true; //handle this one to avoid activating the toolbar button
+            default:
+                break;
+        }
+
+        return false;
+    }
+}


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