add new sigc++2 directory
[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         : _clearing(false)
36 {
37 }
38
39 UndoTransaction::UndoTransaction (const UndoTransaction& rhs)
40         : Command(rhs._name)
41         , _clearing(false)
42 {
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         _depth = 0;
152 }
153
154 void
155 UndoHistory::set_depth (int32_t d)
156 {
157         _depth = d;
158
159         while (_depth > 0 && UndoList.size() > (uint32_t) _depth) {
160                 UndoList.pop_front ();
161         }
162 }
163
164 void
165 UndoHistory::add (UndoTransaction* const ut)
166 {
167         ut->GoingAway.connect (bind (mem_fun (*this, &UndoHistory::remove), ut));
168
169         while (_depth > 0 && UndoList.size() > (uint32_t) _depth) {
170                 UndoList.pop_front ();
171         }
172
173         UndoList.push_back (ut);
174
175         /* we are now owners of the transaction */
176
177         Changed (); /* EMIT SIGNAL */
178 }
179
180 void
181 UndoHistory::remove (UndoTransaction* const ut)
182 {
183         if (_clearing) {
184                 return;
185         }
186
187         UndoList.remove (ut);
188         RedoList.remove (ut);
189
190         Changed (); /* EMIT SIGNAL */
191 }
192
193 /** Undo some transactions.
194  * @param n Number of transactions to undo.
195  */
196 void
197 UndoHistory::undo (unsigned int n)
198 {
199         while (n--) {
200                 if (UndoList.size() == 0) {
201                         return;
202                 }
203                 UndoTransaction* ut = UndoList.back ();
204                 UndoList.pop_back ();
205                 ut->undo ();
206                 RedoList.push_back (ut);
207         }
208
209         Changed (); /* EMIT SIGNAL */
210 }
211
212 void
213 UndoHistory::redo (unsigned int n)
214 {
215         while (n--) {
216                 if (RedoList.size() == 0) {
217                         return;
218                 }
219                 UndoTransaction* ut = RedoList.back ();
220                 RedoList.pop_back ();
221                 ut->redo ();
222                 UndoList.push_back (ut);
223         }
224
225         Changed (); /* EMIT SIGNAL */
226 }
227
228 void
229 UndoHistory::clear_redo ()
230 {
231         _clearing = true;
232         RedoList.clear ();
233         _clearing = false;
234
235         Changed (); /* EMIT SIGNAL */
236
237 }
238
239 void
240 UndoHistory::clear_undo ()
241 {
242         _clearing = true;
243         UndoList.clear ();
244         _clearing = false;
245
246         Changed (); /* EMIT SIGNAL */
247 }
248
249 void
250 UndoHistory::clear ()
251 {
252         clear_undo ();
253         clear_redo ();
254
255         Changed (); /* EMIT SIGNAL */
256 }
257
258 XMLNode& 
259 UndoHistory::get_state (int32_t depth)
260 {
261     XMLNode *node = new XMLNode ("UndoHistory");
262
263     if (depth == 0) {
264
265             return (*node);
266
267     } else if (depth < 0) {
268
269             /* everything */
270
271             for (list<UndoTransaction*>::iterator it = UndoList.begin(); it != UndoList.end(); ++it) {
272                     node->add_child_nocopy((*it)->get_state());
273             }
274
275     } else {
276
277             /* just the last "depth" transactions */
278
279             list<UndoTransaction*> in_order;
280
281             for (list<UndoTransaction*>::reverse_iterator it = UndoList.rbegin(); it != UndoList.rend() && depth; ++it, depth--) {
282                     in_order.push_front (*it);
283             }
284
285             for (list<UndoTransaction*>::iterator it = in_order.begin(); it != in_order.end(); it++) {
286                     node->add_child_nocopy((*it)->get_state());
287             }
288     }
289
290     return *node;
291 }
292
293