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