Don't recurse into MacVST bundle-folders during plugin-scan
[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         , _clearing(false)
42 {
43         _timestamp = rhs._timestamp;
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 cmd)
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         cmd->DropReferences.connect_same_thread (*this, boost::bind (&command_death, this, cmd));
87         actions.push_back (cmd);
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     node->set_property("tv-sec", (int64_t)_timestamp.tv_sec);
139     node->set_property("tv-usec", (int64_t)_timestamp.tv_usec);
140     node->set_property("name", _name);
141
142     list<Command*>::iterator it;
143     for (it=actions.begin(); it!=actions.end(); it++)
144         node->add_child_nocopy((*it)->get_state());
145
146     return *node;
147 }
148
149 class UndoRedoSignaller {
150 public:
151     UndoRedoSignaller (UndoHistory& uh)
152             : _history (uh) {
153             _history.BeginUndoRedo();
154     }
155     ~UndoRedoSignaller() {
156             _history.EndUndoRedo();
157     }
158
159 private:
160     UndoHistory& _history;
161 };
162
163 UndoHistory::UndoHistory ()
164 {
165         _clearing = false;
166         _depth = 0;
167 }
168
169 void
170 UndoHistory::set_depth (uint32_t d)
171 {
172         UndoTransaction* ut;
173         uint32_t current_depth = UndoList.size();
174
175         _depth = d;
176
177         if (d > current_depth) {
178                 /* not even transactions to meet request */
179                 return;
180         }
181
182         if (_depth > 0) {
183
184                 uint32_t cnt = current_depth - d;
185
186                 while (cnt--) {
187                         ut = UndoList.front();
188                         UndoList.pop_front ();
189                         delete ut;
190                 }
191         }
192 }
193
194 void
195 UndoHistory::add (UndoTransaction* const ut)
196 {
197         uint32_t current_depth = UndoList.size();
198
199         ut->DropReferences.connect_same_thread (*this, boost::bind (&UndoHistory::remove, this, ut));
200
201         /* if the current undo history is larger than or equal to the currently
202            requested depth, then pop off at least 1 element to make space
203            at the back for new one.
204         */
205
206         if ((_depth > 0) && current_depth && (current_depth >= _depth)) {
207
208                 uint32_t cnt = 1 + (current_depth - _depth);
209
210                 while (cnt--) {
211                         UndoTransaction* ut;
212                         ut = UndoList.front ();
213                         UndoList.pop_front ();
214                         delete ut;
215                 }
216         }
217
218         UndoList.push_back (ut);
219         /* Adding a transacrion makes the redo list meaningless. */
220         _clearing = true;
221         for (std::list<UndoTransaction*>::iterator i = RedoList.begin(); i != RedoList.end(); ++i) {
222                 delete *i;
223         }
224         RedoList.clear ();
225         _clearing = false;
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         for (std::list<UndoTransaction*>::iterator i = RedoList.begin(); i != RedoList.end(); ++i) {
301                 delete *i;
302         }
303         RedoList.clear ();
304         _clearing = false;
305
306         Changed (); /* EMIT SIGNAL */
307
308 }
309
310 void
311 UndoHistory::clear_undo ()
312 {
313         _clearing = true;
314         for (std::list<UndoTransaction*>::iterator i = UndoList.begin(); i != UndoList.end(); ++i) {
315                 delete *i;
316         }
317         UndoList.clear ();
318         _clearing = false;
319
320         Changed (); /* EMIT SIGNAL */
321 }
322
323 void
324 UndoHistory::clear ()
325 {
326         clear_undo ();
327         clear_redo ();
328
329         Changed (); /* EMIT SIGNAL */
330 }
331
332 XMLNode&
333 UndoHistory::get_state (int32_t depth)
334 {
335     XMLNode *node = new XMLNode ("UndoHistory");
336
337     if (depth == 0) {
338
339             return (*node);
340
341     } else if (depth < 0) {
342
343             /* everything */
344
345             for (list<UndoTransaction*>::iterator it = UndoList.begin(); it != UndoList.end(); ++it) {
346                     node->add_child_nocopy((*it)->get_state());
347             }
348
349     } else {
350
351             /* just the last "depth" transactions */
352
353             list<UndoTransaction*> in_order;
354
355             for (list<UndoTransaction*>::reverse_iterator it = UndoList.rbegin(); it != UndoList.rend() && depth; ++it, depth--) {
356                     in_order.push_front (*it);
357             }
358
359             for (list<UndoTransaction*>::iterator it = in_order.begin(); it != in_order.end(); it++) {
360                     node->add_child_nocopy((*it)->get_state());
361             }
362     }
363
364     return *node;
365 }
366
367