1 /* Copyright 2009 Yorba Foundation
3 * This software is licensed under the GNU Lesser General Public License
4 * (version 2.1 or later). See the COPYING file in this distribution.
8 public class UndoManager {
10 bool _in_undo = false;
11 Gee.ArrayList<Command> command_list = new Gee.ArrayList<Command>();
12 public bool is_dirty { get { return saved_index != command_list.size; } }
13 public bool can_undo { get { return command_list.size > 0; } }
14 public bool in_undo { get { return _in_undo; } private set { _in_undo = value; } }
16 public signal void undo_changed(bool can_undo);
17 public signal void dirty_changed(bool is_dirty);
19 public class UndoManager() {
28 public void mark_clean() {
29 saved_index = command_list.size;
33 public void start_transaction(string description) {
34 TransactionCommand command = new TransactionCommand(false, description);
35 command_list.add(command);
39 public void end_transaction(string description) {
40 TransactionCommand command = new TransactionCommand(true, description);
41 command_list.add(command);
45 public void do_command(Command the_command) {
48 Command? current_command = get_current_command();
49 if (current_command == null || !current_command.merge(the_command)) {
50 command_list.add(the_command);
53 undo_changed(can_undo);
57 Command? get_current_command() {
58 int index = command_list.size - 1;
60 return command_list[index];
67 assert(in_undo == false);
69 int in_transaction = 0;
71 Command? the_command = get_current_command();
72 if (the_command != null) {
73 command_list.remove(the_command);
74 TransactionCommand transaction_command = the_command as TransactionCommand;
75 if (transaction_command != null) {
76 if (transaction_command.in_transaction()) {
87 } while (in_transaction > 0);
88 dirty_changed(is_dirty);
89 undo_changed(can_undo);
93 public string get_undo_title() {
94 Command? the_command = get_current_command();
95 if (the_command != null) {
96 return the_command.description();