Merged with trunk R1141
[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 #include <string>
23 #include <sstream>
24
25 #include <pbd/undo.h>
26 #include <pbd/xml++.h>
27 #include <pbd/shiva.h>
28
29 #include <sigc++/bind.h>
30
31 using namespace std;
32 using namespace sigc;
33
34 UndoTransaction::UndoTransaction ()
35 {
36         _clearing = false;
37 }
38
39 UndoTransaction::UndoTransaction (const UndoTransaction& rhs)
40 {
41         _name = rhs._name;
42         _clearing = false;
43         clear ();
44         actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
45 }
46
47 UndoTransaction::~UndoTransaction ()
48 {
49         GoingAway ();
50         clear ();
51 }
52
53 void 
54 command_death (UndoTransaction* ut, Command* c)
55 {
56         if (ut->clearing()) {
57                 return;
58         }
59
60         ut->remove_command (c);
61
62         if (ut->empty()) {
63                 delete ut;
64         }
65 }
66
67 UndoTransaction& 
68 UndoTransaction::operator= (const UndoTransaction& rhs)
69 {
70         if (this == &rhs) return *this;
71         _name = rhs._name;
72         clear ();
73         actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
74         return *this;
75 }
76
77 void
78 UndoTransaction::add_command (Command *const action)
79 {
80         /* catch death */
81         new PBD::ProxyShiva<Command,UndoTransaction> (*action, *this, &command_death);
82         actions.push_back (action);
83 }
84
85 void
86 UndoTransaction::remove_command (Command* const action)
87 {
88         actions.remove (action);
89 }
90
91 bool
92 UndoTransaction::empty () const
93 {
94         return actions.empty();
95 }
96
97 void
98 UndoTransaction::clear ()
99 {
100         _clearing = true;
101         for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
102                 delete *i;
103         }
104         actions.clear ();
105         _clearing = false;
106 }
107
108 void
109 UndoTransaction::operator() ()
110 {
111         for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
112                 (*(*i))();
113         }
114 }
115
116 void
117 UndoTransaction::undo ()
118 {
119         for (list<Command*>::reverse_iterator i = actions.rbegin(); i != actions.rend(); ++i) {
120                 (*i)->undo();
121         }
122 }
123
124 void
125 UndoTransaction::redo ()
126 {
127         (*this)();
128 }
129
130 XMLNode &UndoTransaction::get_state()
131 {
132     XMLNode *node = new XMLNode ("UndoTransaction");
133     stringstream ss;
134     ss << _timestamp.tv_sec;
135     node->add_property("tv_sec", ss.str());
136     ss.str("");
137     ss << _timestamp.tv_usec;
138     node->add_property("tv_usec", ss.str());
139     node->add_property("name", _name);
140
141     list<Command*>::iterator it;
142     for (it=actions.begin(); it!=actions.end(); it++)
143         node->add_child_nocopy((*it)->get_state());
144
145     return *node;
146 }
147
148 UndoHistory::UndoHistory ()
149 {
150         _clearing = false;
151 }
152
153 void
154 UndoHistory::add (UndoTransaction* const ut)
155 {
156         ut->GoingAway.connect (bind (mem_fun (*this, &UndoHistory::remove), ut));
157         UndoList.push_back (ut);
158
159         /* we are now owners of the transaction */
160
161         Changed (); /* EMIT SIGNAL */
162 }
163
164 void
165 UndoHistory::remove (UndoTransaction* const ut)
166 {
167         if (_clearing) {
168                 return;
169         }
170
171         UndoList.remove (ut);
172         RedoList.remove (ut);
173
174         Changed (); /* EMIT SIGNAL */
175 }
176
177 void
178 UndoHistory::undo (unsigned int n)
179 {
180         while (n--) {
181                 if (UndoList.size() == 0) {
182                         return;
183                 }
184                 UndoTransaction* ut = UndoList.back ();
185                 UndoList.pop_back ();
186                 ut->undo ();
187                 RedoList.push_back (ut);
188         }
189
190         Changed (); /* EMIT SIGNAL */
191 }
192
193 void
194 UndoHistory::redo (unsigned int n)
195 {
196         while (n--) {
197                 if (RedoList.size() == 0) {
198                         return;
199                 }
200                 UndoTransaction* ut = RedoList.back ();
201                 RedoList.pop_back ();
202                 ut->redo ();
203                 UndoList.push_back (ut);
204         }
205
206         Changed (); /* EMIT SIGNAL */
207 }
208
209 void
210 UndoHistory::clear_redo ()
211 {
212         _clearing = true;
213         RedoList.clear ();
214         _clearing = false;
215
216         Changed (); /* EMIT SIGNAL */
217
218 }
219
220 void
221 UndoHistory::clear_undo ()
222 {
223         _clearing = true;
224         UndoList.clear ();
225         _clearing = false;
226
227         Changed (); /* EMIT SIGNAL */
228 }
229
230 void
231 UndoHistory::clear ()
232 {
233         clear_undo ();
234         clear_redo ();
235
236         Changed (); /* EMIT SIGNAL */
237 }
238
239 XMLNode & UndoHistory::get_state()
240 {
241     XMLNode *node = new XMLNode ("UndoHistory");
242
243     list<UndoTransaction*>::iterator it;
244     for (it = UndoList.begin(); it != UndoList.end(); it++) {
245             node->add_child_nocopy((*it)->get_state());
246     }
247
248     return *node;
249 }