[gnome-sudoku/vala-port] Add Undo and Redo buttons in Headerbar



commit e33e2b0bc36bd47a16448c1f32d52e3657fab311
Author: Parin Porecha <parinporecha gmail com>
Date:   Fri May 23 17:07:59 2014 +0530

    Add Undo and Redo buttons in Headerbar

 data/gnome-sudoku.ui  |   42 ++++++++++++++++++++++++++++++++++++++++++
 src/gnome-sudoku.vala |   18 +++++++++++++++++-
 src/sudoku-game.vala  |   10 ++++++++++
 3 files changed, 69 insertions(+), 1 deletions(-)
---
diff --git a/data/gnome-sudoku.ui b/data/gnome-sudoku.ui
index 74fe4a4..95624bf 100644
--- a/data/gnome-sudoku.ui
+++ b/data/gnome-sudoku.ui
@@ -9,6 +9,48 @@
                 <property name="can_focus">False</property>
                 <property name="show_close_button">True</property>
                 <property name="title" translatable="yes">Sudoku</property>
+                <child>
+                    <object class="GtkButton" id="undo_button">
+                        <property name="visible">True</property>
+                        <property name="sensitive">False</property>
+                        <property name="valign">center</property>
+                        <property name="tooltip-text" translatable="yes">Undo your last action</property>
+                        <property name="can_focus">True</property>
+                        <property name="focus_on_click">False</property>
+                        <property name="action-name">app.undo</property>
+                        <style>
+                            <class name="image-button"/>
+                        </style>
+                        <child>
+                            <object class="GtkImage" id="undo_image">
+                                <property name="icon_name">edit-undo-symbolic</property>
+                                <property name="visible">True</property>
+                                <property name="icon_size">1</property>
+                            </object>
+                        </child>
+                    </object>
+                </child>
+                <child>
+                    <object class="GtkButton" id="redo_button">
+                        <property name="visible">True</property>
+                        <property name="sensitive">False</property>
+                        <property name="valign">center</property>
+                        <property name="tooltip-text" translatable="yes">Redo your last action</property>
+                        <property name="can_focus">True</property>
+                        <property name="focus_on_click">False</property>
+                        <property name="action-name">app.redo</property>
+                        <style>
+                            <class name="image-button"/>
+                        </style>
+                        <child>
+                            <object class="GtkImage" id="redo_image">
+                                <property name="icon_name">edit-redo-symbolic</property>
+                                <property name="visible">True</property>
+                                <property name="icon_size">1</property>
+                            </object>
+                        </child>
+                    </object>
+                </child>
             </object>
         </child>
         <child>
diff --git a/src/gnome-sudoku.vala b/src/gnome-sudoku.vala
index ca6a459..9db2005 100644
--- a/src/gnome-sudoku.vala
+++ b/src/gnome-sudoku.vala
@@ -18,7 +18,6 @@ public class Sudoku : Gtk.Application
     private HeaderBar header_bar;
     private Box game_box; // Holds the grid and controls boxes
     private Box grid_box; // Holds the view
-
     private Box controls_box; // Holds the controls (including the number picker)
     private NumberPicker number_picker;
 
@@ -26,6 +25,9 @@ public class Sudoku : Gtk.Application
 
     private SudokuSaver saver;
 
+    private SimpleAction undo_action;
+    private SimpleAction redo_action;
+
     private const GLib.ActionEntry action_entries[] =
     {
         {"new-game", new_game_cb                                    },
@@ -77,6 +79,9 @@ public class Sudoku : Gtk.Application
         grid_box = (Box) builder.get_object ("grid_box");
         controls_box = (Box) builder.get_object ("number_picker_box");
 
+        undo_action = (SimpleAction) lookup_action ("undo");
+        redo_action = (SimpleAction) lookup_action ("redo");
+
         sudoku_store = new SudokuStore ();
         saver = new SudokuSaver ();
         //SudokuGenerator gen = new SudokuGenerator();
@@ -108,6 +113,8 @@ public class Sudoku : Gtk.Application
         var rating = rater.get_difficulty ();
         rating.pretty_print ();
         header_bar.set_subtitle ("%s".printf (rating.get_catagory ().to_string ()));
+        undo_action.set_enabled (false);
+        redo_action.set_enabled (false);
 
         var show_possibilities = false;
         var show_warnings = false;
@@ -144,6 +151,11 @@ public class Sudoku : Gtk.Application
             view.set_cell_value (view.selected_x, view.selected_y, number);
         });
 
+        game.cell_changed.connect (() => {
+            undo_action.set_enabled (!game.is_undostack_null ());
+            redo_action.set_enabled (!game.is_redostack_null ());
+        });
+
         game.board.completed.connect (() => {
             view.dance ();
 
@@ -200,11 +212,15 @@ public class Sudoku : Gtk.Application
     public void undo_cb ()
     {
         game.undo ();
+        undo_action.set_enabled (!game.is_undostack_null ());
+        view.queue_draw ();
     }
 
     public void redo_cb ()
     {
         game.redo ();
+        redo_action.set_enabled (!game.is_redostack_null ());
+        view.queue_draw ();
     }
 
     public void print_cb ()
diff --git a/src/sudoku-game.vala b/src/sudoku-game.vala
index 37bda45..fef75b7 100644
--- a/src/sudoku-game.vala
+++ b/src/sudoku-game.vala
@@ -19,6 +19,16 @@ public class SudokuGame
     private SList<UndoItem?> undostack;
     private SList<UndoItem?> redostack;
 
+    public bool is_undostack_null ()
+    {
+        return undostack == null;
+    }
+
+    public bool is_redostack_null ()
+    {
+        return redostack == null;
+    }
+
     public SudokuGame (SudokuBoard board)
     {
         this.board = board;


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