[ease] Added redo to UndoController.



commit 0c38773211c950432fb6e2428bef04eca666d9ed
Author: Nate Stedman <natesm gmail com>
Date:   Tue Jun 1 17:58:01 2010 -0400

    Added redo to UndoController.

 src/EditorWindow.vala   |   10 +++++++++-
 src/UndoActions.vala    |   11 ++++++++---
 src/UndoController.vala |   46 +++++++++++++++++++++++++++++++++++++++-------
 3 files changed, 56 insertions(+), 11 deletions(-)
---
diff --git a/src/EditorWindow.vala b/src/EditorWindow.vala
index 29fb5d6..c597ae8 100644
--- a/src/EditorWindow.vala
+++ b/src/EditorWindow.vala
@@ -174,6 +174,13 @@ public class Ease.EditorWindow : Gtk.Window
 			embed.reposition_group();
 		});
 		
+		main_toolbar.redo.clicked.connect(() => {
+			undo.redo();
+			update_undo();
+			embed.slide_actor.relayout();
+			embed.reposition_group();
+		});
+		
 		// TODO: export HTML in a proper place
 		main_toolbar.fonts.clicked.connect(() => {
 			document.export_to_html(this);
@@ -216,13 +223,14 @@ public class Ease.EditorWindow : Gtk.Window
 	public void add_undo_action(UndoAction action)
 	{
 		undo.add_action(action);
+		undo.clear_redo();
 		update_undo();
 	}
 	
 	private void update_undo()
 	{
 		main_toolbar.undo.sensitive = undo.can_undo();
-		main_toolbar.redo.sensitive = false;
+		main_toolbar.redo.sensitive = undo.can_redo();
 	}
 	
 	// signal handlers
diff --git a/src/UndoActions.vala b/src/UndoActions.vala
index 0688f36..ee17d42 100644
--- a/src/UndoActions.vala
+++ b/src/UndoActions.vala
@@ -27,9 +27,9 @@ public abstract class Ease.UndoAction : Object
 	 * Applies the { link UndoAction}.
 	 *
 	 * This method should be overriden by subclasses to undo the appropriate
-	 * action.
+	 * action. It should return an UndoAction that will redo the UndoAction.
 	 */
-	public abstract void apply();
+	public abstract UndoAction apply();
 }
 
 /**
@@ -61,12 +61,17 @@ public class Ease.MoveUndoAction : UndoAction
 		height = h;
 	}
 	
-	public override void apply()
+	public override UndoAction apply()
 	{
+		var ret = new MoveUndoAction(element, element.x, element.y,
+		                             element.width, element.height);
+		
 		element.x = x_pos;
 		element.y = y_pos;
 		element.width = width;
 		element.height = height;
+		
+		return ret;
 	}
 }
 
diff --git a/src/UndoController.vala b/src/UndoController.vala
index 389818b..ead57a4 100644
--- a/src/UndoController.vala
+++ b/src/UndoController.vala
@@ -22,12 +22,10 @@
  */
 public class Ease.UndoController : Object
 {
-	private Gee.LinkedList<UndoAction> undos;
+	private Gee.LinkedList<UndoAction> undos = new Gee.LinkedList<UndoAction>();
+	private Gee.LinkedList<UndoAction> redos = new Gee.LinkedList<UndoAction>();
 	
-	public UndoController()
-	{
-		undos = new Gee.LinkedList<UndoAction>();
-	}
+	public UndoController() { }
 	
 	/**
 	 * Returns true if there is an action available to undo.
@@ -38,11 +36,35 @@ public class Ease.UndoController : Object
 	}
 	
 	/**
-	 * Undoes the first available { link UndoAction}.
+	 * Returns true if there is an action available to redo.
+	 */
+	public bool can_redo()
+	{
+		return redos.size > 0;
+	}
+	
+	/**
+	 * Undoes the first available { link UndoAction} in the undo queue.
 	 */
 	public void undo()
 	{
-		undos.poll_head().apply();
+		add_redo_action(undos.poll_head().apply());
+	}
+	
+	/**
+	 * Redoes the first available { link UndoAction} in the redo queue.
+	 */
+	public void redo()
+	{
+		add_action(redos.poll_head().apply());
+	}
+	
+	/**
+	 * Clears the redo queue.
+	 */
+	public void clear_redo()
+	{
+		redos.clear();
 	}
 	
 	/**
@@ -54,4 +76,14 @@ public class Ease.UndoController : Object
 	{
 		undos.offer_head(action);
 	}
+	
+	/**
+	 * Adds a new { link UndoAction} as the first action.
+	 *
+	 * @param action The new { link UndoAction}.
+	 */
+	private void add_redo_action(UndoAction action)
+	{
+		redos.offer_head(action);
+	}
 }



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