r274@gandalf: fugalh | 2006-08-07 19:53:48 -0600
[ardour.git] / libs / pbd / undo.cc
1 /* 
2     Copyright (C) 2001 Brett Viren & Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id$
19 */
20
21 #include <iostream>
22
23 #include <pbd/undo.h>
24 #include <pbd/xml++.h>
25
26 using namespace std;
27 using namespace sigc;
28
29 UndoTransaction::UndoTransaction ()
30 {
31 }
32
33 UndoTransaction::UndoTransaction (const UndoTransaction& rhs)
34 {
35         _name = rhs._name;
36         clear ();
37         actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
38 }
39
40 UndoTransaction& 
41 UndoTransaction::operator= (const UndoTransaction& rhs)
42 {
43         if (this == &rhs) return *this;
44         _name = rhs._name;
45         clear ();
46         actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
47         return *this;
48 }
49
50 void
51 UndoTransaction::add_command (Command *const action)
52 {
53         actions.push_back (action);
54 }
55
56 void
57 UndoTransaction::clear ()
58 {
59         actions.clear ();
60 }
61
62 void
63 UndoTransaction::operator() ()
64 {
65         for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
66                 (*(*i))();
67         }
68 }
69
70 void
71 UndoTransaction::undo ()
72 {
73         cerr << "Undo " << _name << endl;
74         for (list<Command*>::reverse_iterator i = actions.rbegin(); i != actions.rend(); ++i) {
75                 (*i)->undo();
76         }
77 }
78
79 void
80 UndoTransaction::redo ()
81 {
82         cerr << "Redo " << _name << endl;
83         (*this)();
84 }
85
86 XMLNode &UndoTransaction::get_state()
87 {
88     XMLNode *node = new XMLNode ("UndoTransaction");
89     // TODO
90     return *node;
91 }
92
93 void
94 UndoHistory::add (UndoTransaction ut)
95 {
96         UndoList.push_back (ut);
97 }
98
99 void
100 UndoHistory::undo (unsigned int n)
101 {
102         while (n--) {
103                 if (UndoList.size() == 0) {
104                         return;
105                 }
106                 UndoTransaction ut = UndoList.back ();
107                 UndoList.pop_back ();
108                 ut.undo ();
109                 RedoList.push_back (ut);
110         }
111 }
112
113 void
114 UndoHistory::redo (unsigned int n)
115 {
116         while (n--) {
117                 if (RedoList.size() == 0) {
118                         return;
119                 }
120                 UndoTransaction ut = RedoList.back ();
121                 RedoList.pop_back ();
122                 ut.redo ();
123                 UndoList.push_back (ut);
124         }
125 }
126
127 void
128 UndoHistory::clear_redo ()
129 {
130         RedoList.clear ();
131 }
132
133 void
134 UndoHistory::clear_undo ()
135 {
136         UndoList.clear ();
137 }
138
139 void
140 UndoHistory::clear ()
141 {
142         RedoList.clear ();
143         UndoList.clear ();
144 }