newly created files for use in recording appear in a .stubs folder, and are moved...
[ardour2.git] / libs / pbd / undo.cc
blob120f62c3512c78d263ae48a3aa6e9990b6eea30b
1 /*
2 Copyright (C) 2001 Brett Viren & Paul Davis
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.
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.
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.
18 $Id$
21 #include <iostream>
22 #include <string>
23 #include <sstream>
24 #include <time.h>
26 #include "pbd/undo.h"
27 #include "pbd/xml++.h"
29 #include <sigc++/bind.h>
31 using namespace std;
32 using namespace sigc;
34 UndoTransaction::UndoTransaction ()
35 : _clearing(false)
37 gettimeofday (&_timestamp, 0);
40 UndoTransaction::UndoTransaction (const UndoTransaction& rhs)
41 : Command(rhs._name)
42 , _clearing(false)
44 clear ();
45 actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
48 UndoTransaction::~UndoTransaction ()
50 drop_references ();
51 clear ();
54 void
55 command_death (UndoTransaction* ut, Command* c)
57 if (ut->clearing()) {
58 return;
61 ut->remove_command (c);
63 if (ut->empty()) {
64 delete ut;
68 UndoTransaction&
69 UndoTransaction::operator= (const UndoTransaction& rhs)
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;
78 void
79 UndoTransaction::add_command (Command *const cmd)
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.
86 cmd->DropReferences.connect_same_thread (*this, boost::bind (&command_death, this, cmd));
87 actions.push_back (cmd);
90 void
91 UndoTransaction::remove_command (Command* const action)
93 actions.remove (action);
96 bool
97 UndoTransaction::empty () const
99 return actions.empty();
102 void
103 UndoTransaction::clear ()
105 _clearing = true;
106 for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
107 delete *i;
109 actions.clear ();
110 _clearing = false;
113 void
114 UndoTransaction::operator() ()
116 for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
117 (*(*i))();
121 void
122 UndoTransaction::undo ()
124 for (list<Command*>::reverse_iterator i = actions.rbegin(); i != actions.rend(); ++i) {
125 (*i)->undo();
129 void
130 UndoTransaction::redo ()
132 (*this)();
135 XMLNode &UndoTransaction::get_state()
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);
146 list<Command*>::iterator it;
147 for (it=actions.begin(); it!=actions.end(); it++)
148 node->add_child_nocopy((*it)->get_state());
150 return *node;
153 class UndoRedoSignaller {
154 public:
155 UndoRedoSignaller (UndoHistory& uh)
156 : _history (uh) {
157 _history.BeginUndoRedo();
159 ~UndoRedoSignaller() {
160 _history.EndUndoRedo();
163 private:
164 UndoHistory& _history;
167 UndoHistory::UndoHistory ()
169 _clearing = false;
170 _depth = 0;
173 void
174 UndoHistory::set_depth (uint32_t d)
176 UndoTransaction* ut;
177 uint32_t current_depth = UndoList.size();
179 _depth = d;
181 if (d > current_depth) {
182 /* not even transactions to meet request */
183 return;
186 if (_depth > 0) {
188 uint32_t cnt = current_depth - d;
190 while (cnt--) {
191 ut = UndoList.front();
192 UndoList.pop_front ();
193 delete ut;
198 void
199 UndoHistory::add (UndoTransaction* const ut)
201 uint32_t current_depth = UndoList.size();
203 ut->DropReferences.connect_same_thread (*this, boost::bind (&UndoHistory::remove, this, ut));
205 /* if the current undo history is larger than or equal to the currently
206 requested depth, then pop off at least 1 element to make space
207 at the back for new one.
210 if ((_depth > 0) && current_depth && (current_depth >= _depth)) {
212 uint32_t cnt = 1 + (current_depth - _depth);
214 while (cnt--) {
215 UndoTransaction* ut;
216 ut = UndoList.front ();
217 UndoList.pop_front ();
218 delete ut;
222 UndoList.push_back (ut);
224 /* we are now owners of the transaction and must delete it when finished with it */
226 Changed (); /* EMIT SIGNAL */
229 void
230 UndoHistory::remove (UndoTransaction* const ut)
232 if (_clearing) {
233 return;
236 UndoList.remove (ut);
237 RedoList.remove (ut);
239 Changed (); /* EMIT SIGNAL */
242 /** Undo some transactions.
243 * @param n Number of transactions to undo.
245 void
246 UndoHistory::undo (unsigned int n)
248 if (n == 0) {
249 return;
253 UndoRedoSignaller exception_safe_signaller (*this);
255 while (n--) {
256 if (UndoList.size() == 0) {
257 return;
259 UndoTransaction* ut = UndoList.back ();
260 UndoList.pop_back ();
261 ut->undo ();
262 RedoList.push_back (ut);
266 Changed (); /* EMIT SIGNAL */
269 void
270 UndoHistory::redo (unsigned int n)
272 if (n == 0) {
273 return;
277 UndoRedoSignaller exception_safe_signaller (*this);
279 while (n--) {
280 if (RedoList.size() == 0) {
281 return;
283 UndoTransaction* ut = RedoList.back ();
284 RedoList.pop_back ();
285 ut->redo ();
286 UndoList.push_back (ut);
290 Changed (); /* EMIT SIGNAL */
293 void
294 UndoHistory::clear_redo ()
296 _clearing = true;
297 RedoList.clear ();
298 _clearing = false;
300 Changed (); /* EMIT SIGNAL */
304 void
305 UndoHistory::clear_undo ()
307 _clearing = true;
308 UndoList.clear ();
309 _clearing = false;
311 Changed (); /* EMIT SIGNAL */
314 void
315 UndoHistory::clear ()
317 clear_undo ();
318 clear_redo ();
320 Changed (); /* EMIT SIGNAL */
323 XMLNode&
324 UndoHistory::get_state (int32_t depth)
326 XMLNode *node = new XMLNode ("UndoHistory");
328 if (depth == 0) {
330 return (*node);
332 } else if (depth < 0) {
334 /* everything */
336 for (list<UndoTransaction*>::iterator it = UndoList.begin(); it != UndoList.end(); ++it) {
337 node->add_child_nocopy((*it)->get_state());
340 } else {
342 /* just the last "depth" transactions */
344 list<UndoTransaction*> in_order;
346 for (list<UndoTransaction*>::reverse_iterator it = UndoList.rbegin(); it != UndoList.rend() && depth; ++it, depth--) {
347 in_order.push_front (*it);
350 for (list<UndoTransaction*>::iterator it = in_order.begin(); it != in_order.end(); it++) {
351 node->add_child_nocopy((*it)->get_state());
355 return *node;