cleanup up cleanup at session destruction; clarify the meaning of 3 signals (DropRefe...
[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 <string>
22 #include <sstream>
23 #include <time.h>
24
25 #include "pbd/undo.h"
26 #include "pbd/xml++.h"
27
28 #include <sigc++/bind.h>
29
30 using namespace std;
31 using namespace sigc;
32
33 UndoTransaction::UndoTransaction ()
34         : _clearing(false)
35 {
36         gettimeofday (&_timestamp, 0);
37 }
38
39 UndoTransaction::UndoTransaction (const UndoTransaction& rhs)
40         : Command(rhs._name)
41         , PBD::ScopedConnectionList ()
42         , _clearing(false)
43 {
44         clear ();
45         actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
46 }
47
48 UndoTransaction::~UndoTransaction ()
49 {
50         drop_references ();
51         clear ();
52 }
53
54 void 
55 command_death (UndoTransaction* ut, Command* c)
56 {
57         if (ut->clearing()) {
58                 return;
59         }
60
61         ut->remove_command (c);
62
63         if (ut->empty()) {
64                 delete ut;
65         }
66 }
67
68 UndoTransaction& 
69 UndoTransaction::operator= (const UndoTransaction& rhs)
70 {
71         if (this == &rhs) return *this;
72         _name = rhs._name;
73         clear ();
74         actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
75         return *this;
76 }
77
78 void
79 UndoTransaction::add_command (Command *const action)
80 {
81         /* catch death of command (e.g. caused by death of object to
82            which it refers. command_death() is a normal static function
83            so there is no need to manage this connection.
84          */
85
86         action->DropReferences.connect_same_thread (*this, boost::bind (&command_death, this, action));
87         actions.push_back (action);
88 }
89
90 void
91 UndoTransaction::remove_command (Command* const action)
92 {
93         actions.remove (action);
94 }
95
96 bool
97 UndoTransaction::empty () const
98 {
99         return actions.empty();
100 }
101
102 void
103 UndoTransaction::clear ()
104 {
105         _clearing = true;
106         for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
107                 delete *i;
108         }
109         actions.clear ();
110         _clearing = false;
111 }
112
113 void
114 UndoTransaction::operator() ()
115 {
116         for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
117                 (*(*i))();
118         }
119 }
120
121 void
122 UndoTransaction::undo ()
123 {
124         for (list<Command*>::reverse_iterator i = actions.rbegin(); i != actions.rend(); ++i) {
125                 (*i)->undo();
126         }
127 }
128
129 void
130 UndoTransaction::redo ()
131 {
132         (*this)();
133 }
134
135 XMLNode &UndoTransaction::get_state()
136 {
137     XMLNode *node = new XMLNode ("UndoTransaction");
138     stringstream ss;
139     ss << _timestamp.tv_sec;
140     node->add_property("tv_sec", ss.str());
141     ss.str("");
142     ss << _timestamp.tv_usec;
143     node->add_property("tv_usec", ss.str());
144     node->add_property("name", _name);
145
146     list<Command*>::iterator it;
147     for (it=actions.begin(); it!=actions.end(); it++)
148         node->add_child_nocopy((*it)->get_state());
149
150     return *node;
151 }
152
153 UndoHistory::UndoHistory ()
154 {
155         _clearing = false;
156         _depth = 0;
157 }
158
159 void
160 UndoHistory::set_depth (uint32_t d)
161 {
162         UndoTransaction* ut;
163         uint32_t current_depth = UndoList.size();
164
165         _depth = d;
166
167         if (d > current_depth) {
168                 /* not even transactions to meet request */
169                 return;
170         }
171
172         if (_depth > 0) {
173
174                 uint32_t cnt = current_depth - d;
175
176                 while (cnt--) {
177                         ut = UndoList.front();
178                         UndoList.pop_front ();
179                         delete ut;
180                 }
181         }
182 }
183
184 void
185 UndoHistory::add (UndoTransaction* const ut)
186 {
187         uint32_t current_depth = UndoList.size();
188
189         ut->DropReferences.connect_same_thread (*this, boost::bind (&UndoHistory::remove, this, ut));
190
191         /* if the current undo history is larger than or equal to the currently
192            requested depth, then pop off at least 1 element to make space
193            at the back for new one.
194         */
195
196         if ((_depth > 0) && current_depth && (current_depth >= _depth)) {
197
198                 uint32_t cnt = 1 + (current_depth - _depth);
199
200                 while (cnt--) {
201                         UndoTransaction* ut;
202                         ut = UndoList.front ();
203                         UndoList.pop_front ();
204                         delete ut;
205                 }
206         }
207
208         UndoList.push_back (ut);
209
210         /* we are now owners of the transaction and must delete it when finished with it */
211
212         Changed (); /* EMIT SIGNAL */
213 }
214
215 void
216 UndoHistory::remove (UndoTransaction* const ut)
217 {
218         if (_clearing) {
219                 return;
220         }
221
222         UndoList.remove (ut);
223         RedoList.remove (ut);
224
225         Changed (); /* EMIT SIGNAL */
226 }
227
228 /** Undo some transactions.
229  * @param n Number of transactions to undo.
230  */
231 void
232 UndoHistory::undo (unsigned int n)
233 {
234         while (n--) {
235                 if (UndoList.size() == 0) {
236                         return;
237                 }
238                 UndoTransaction* ut = UndoList.back ();
239                 UndoList.pop_back ();
240                 ut->undo ();
241                 RedoList.push_back (ut);
242         }
243
244         Changed (); /* EMIT SIGNAL */
245 }
246
247 void
248 UndoHistory::redo (unsigned int n)
249 {
250         while (n--) {
251                 if (RedoList.size() == 0) {
252                         return;
253                 }
254                 UndoTransaction* ut = RedoList.back ();
255                 RedoList.pop_back ();
256                 ut->redo ();
257                 UndoList.push_back (ut);
258         }
259
260         Changed (); /* EMIT SIGNAL */
261 }
262
263 void
264 UndoHistory::clear_redo ()
265 {
266         _clearing = true;
267         RedoList.clear ();
268         _clearing = false;
269
270         Changed (); /* EMIT SIGNAL */
271
272 }
273
274 void
275 UndoHistory::clear_undo ()
276 {
277         _clearing = true;
278         UndoList.clear ();
279         _clearing = false;
280
281         Changed (); /* EMIT SIGNAL */
282 }
283
284 void
285 UndoHistory::clear ()
286 {
287         clear_undo ();
288         clear_redo ();
289
290         Changed (); /* EMIT SIGNAL */
291 }
292
293 XMLNode& 
294 UndoHistory::get_state (int32_t depth)
295 {
296     XMLNode *node = new XMLNode ("UndoHistory");
297
298     if (depth == 0) {
299
300             return (*node);
301
302     } else if (depth < 0) {
303
304             /* everything */
305
306             for (list<UndoTransaction*>::iterator it = UndoList.begin(); it != UndoList.end(); ++it) {
307                     node->add_child_nocopy((*it)->get_state());
308             }
309
310     } else {
311
312             /* just the last "depth" transactions */
313
314             list<UndoTransaction*> in_order;
315
316             for (list<UndoTransaction*>::reverse_iterator it = UndoList.rbegin(); it != UndoList.rend() && depth; ++it, depth--) {
317                     in_order.push_front (*it);
318             }
319
320             for (list<UndoTransaction*>::iterator it = in_order.begin(); it != in_order.end(); it++) {
321                     node->add_child_nocopy((*it)->get_state());
322             }
323     }
324
325     return *node;
326 }
327
328