remove theme-specific names from RC.in files, substitute them during build
[ardour2.git] / libs / pbd / undo.cc
blobc5c1cb238ad27f210cb892d8b38e29c27aecb3fb
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 _timestamp = rhs._timestamp;
45 clear ();
46 actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
49 UndoTransaction::~UndoTransaction ()
51 drop_references ();
52 clear ();
55 void
56 command_death (UndoTransaction* ut, Command* c)
58 if (ut->clearing()) {
59 return;
62 ut->remove_command (c);
64 if (ut->empty()) {
65 delete ut;
69 UndoTransaction&
70 UndoTransaction::operator= (const UndoTransaction& rhs)
72 if (this == &rhs) return *this;
73 _name = rhs._name;
74 clear ();
75 actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
76 return *this;
79 void
80 UndoTransaction::add_command (Command *const cmd)
82 /* catch death of command (e.g. caused by death of object to
83 which it refers. command_death() is a normal static function
84 so there is no need to manage this connection.
87 cmd->DropReferences.connect_same_thread (*this, boost::bind (&command_death, this, cmd));
88 actions.push_back (cmd);
91 void
92 UndoTransaction::remove_command (Command* const action)
94 actions.remove (action);
97 bool
98 UndoTransaction::empty () const
100 return actions.empty();
103 void
104 UndoTransaction::clear ()
106 _clearing = true;
107 for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
108 delete *i;
110 actions.clear ();
111 _clearing = false;
114 void
115 UndoTransaction::operator() ()
117 for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
118 (*(*i))();
122 void
123 UndoTransaction::undo ()
125 for (list<Command*>::reverse_iterator i = actions.rbegin(); i != actions.rend(); ++i) {
126 (*i)->undo();
130 void
131 UndoTransaction::redo ()
133 (*this)();
136 XMLNode &UndoTransaction::get_state()
138 XMLNode *node = new XMLNode ("UndoTransaction");
139 stringstream ss;
140 ss << _timestamp.tv_sec;
141 node->add_property("tv_sec", ss.str());
142 ss.str("");
143 ss << _timestamp.tv_usec;
144 node->add_property("tv_usec", ss.str());
145 node->add_property("name", _name);
147 list<Command*>::iterator it;
148 for (it=actions.begin(); it!=actions.end(); it++)
149 node->add_child_nocopy((*it)->get_state());
151 return *node;
154 class UndoRedoSignaller {
155 public:
156 UndoRedoSignaller (UndoHistory& uh)
157 : _history (uh) {
158 _history.BeginUndoRedo();
160 ~UndoRedoSignaller() {
161 _history.EndUndoRedo();
164 private:
165 UndoHistory& _history;
168 UndoHistory::UndoHistory ()
170 _clearing = false;
171 _depth = 0;
174 void
175 UndoHistory::set_depth (uint32_t d)
177 UndoTransaction* ut;
178 uint32_t current_depth = UndoList.size();
180 _depth = d;
182 if (d > current_depth) {
183 /* not even transactions to meet request */
184 return;
187 if (_depth > 0) {
189 uint32_t cnt = current_depth - d;
191 while (cnt--) {
192 ut = UndoList.front();
193 UndoList.pop_front ();
194 delete ut;
199 void
200 UndoHistory::add (UndoTransaction* const ut)
202 uint32_t current_depth = UndoList.size();
204 ut->DropReferences.connect_same_thread (*this, boost::bind (&UndoHistory::remove, this, ut));
206 /* if the current undo history is larger than or equal to the currently
207 requested depth, then pop off at least 1 element to make space
208 at the back for new one.
211 if ((_depth > 0) && current_depth && (current_depth >= _depth)) {
213 uint32_t cnt = 1 + (current_depth - _depth);
215 while (cnt--) {
216 UndoTransaction* ut;
217 ut = UndoList.front ();
218 UndoList.pop_front ();
219 delete ut;
223 UndoList.push_back (ut);
225 /* we are now owners of the transaction and must delete it when finished with it */
227 Changed (); /* EMIT SIGNAL */
230 void
231 UndoHistory::remove (UndoTransaction* const ut)
233 if (_clearing) {
234 return;
237 UndoList.remove (ut);
238 RedoList.remove (ut);
240 Changed (); /* EMIT SIGNAL */
243 /** Undo some transactions.
244 * @param n Number of transactions to undo.
246 void
247 UndoHistory::undo (unsigned int n)
249 if (n == 0) {
250 return;
254 UndoRedoSignaller exception_safe_signaller (*this);
256 while (n--) {
257 if (UndoList.size() == 0) {
258 return;
260 UndoTransaction* ut = UndoList.back ();
261 UndoList.pop_back ();
262 ut->undo ();
263 RedoList.push_back (ut);
267 Changed (); /* EMIT SIGNAL */
270 void
271 UndoHistory::redo (unsigned int n)
273 if (n == 0) {
274 return;
278 UndoRedoSignaller exception_safe_signaller (*this);
280 while (n--) {
281 if (RedoList.size() == 0) {
282 return;
284 UndoTransaction* ut = RedoList.back ();
285 RedoList.pop_back ();
286 ut->redo ();
287 UndoList.push_back (ut);
291 Changed (); /* EMIT SIGNAL */
294 void
295 UndoHistory::clear_redo ()
297 _clearing = true;
298 for (std::list<UndoTransaction*>::iterator i = RedoList.begin(); i != RedoList.end(); ++i) {
299 delete *i;
301 RedoList.clear ();
302 _clearing = false;
304 Changed (); /* EMIT SIGNAL */
308 void
309 UndoHistory::clear_undo ()
311 _clearing = true;
312 for (std::list<UndoTransaction*>::iterator i = UndoList.begin(); i != UndoList.end(); ++i) {
313 delete *i;
315 UndoList.clear ();
316 _clearing = false;
318 Changed (); /* EMIT SIGNAL */
321 void
322 UndoHistory::clear ()
324 clear_undo ();
325 clear_redo ();
327 Changed (); /* EMIT SIGNAL */
330 XMLNode&
331 UndoHistory::get_state (int32_t depth)
333 XMLNode *node = new XMLNode ("UndoHistory");
335 if (depth == 0) {
337 return (*node);
339 } else if (depth < 0) {
341 /* everything */
343 for (list<UndoTransaction*>::iterator it = UndoList.begin(); it != UndoList.end(); ++it) {
344 node->add_child_nocopy((*it)->get_state());
347 } else {
349 /* just the last "depth" transactions */
351 list<UndoTransaction*> in_order;
353 for (list<UndoTransaction*>::reverse_iterator it = UndoList.rbegin(); it != UndoList.rend() && depth; ++it, depth--) {
354 in_order.push_front (*it);
357 for (list<UndoTransaction*>::iterator it = in_order.begin(); it != in_order.end(); it++) {
358 node->add_child_nocopy((*it)->get_state());
362 return *node;