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.
27 #include "pbd/xml++.h"
29 #include <sigc++/bind.h>
34 UndoTransaction::UndoTransaction ()
37 gettimeofday (&_timestamp, 0);
40 UndoTransaction::UndoTransaction (const UndoTransaction& rhs)
42 , PBD::ScopedConnectionList ()
46 actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
49 UndoTransaction::~UndoTransaction ()
56 command_death (UndoTransaction* ut, Command* c)
62 ut->remove_command (c);
70 UndoTransaction::operator= (const UndoTransaction& rhs)
72 if (this == &rhs) return *this;
75 actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
80 UndoTransaction::add_command (Command *const action)
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 action->DropReferences.connect_same_thread (*this, boost::bind (&command_death, this, action));
88 actions.push_back (action);
92 UndoTransaction::remove_command (Command* const action)
94 actions.remove (action);
98 UndoTransaction::empty () const
100 return actions.empty();
104 UndoTransaction::clear ()
107 for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
115 UndoTransaction::operator() ()
117 for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
123 UndoTransaction::undo ()
125 for (list<Command*>::reverse_iterator i = actions.rbegin(); i != actions.rend(); ++i) {
131 UndoTransaction::redo ()
136 XMLNode &UndoTransaction::get_state()
138 XMLNode *node = new XMLNode ("UndoTransaction");
140 ss << _timestamp.tv_sec;
141 node->add_property("tv_sec", 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());
154 UndoHistory::UndoHistory ()
161 UndoHistory::set_depth (uint32_t d)
164 uint32_t current_depth = UndoList.size();
168 if (d > current_depth) {
169 /* not even transactions to meet request */
175 uint32_t cnt = current_depth - d;
178 ut = UndoList.front();
179 UndoList.pop_front ();
186 UndoHistory::add (UndoTransaction* const ut)
188 uint32_t current_depth = UndoList.size();
190 ut->DropReferences.connect_same_thread (*this, boost::bind (&UndoHistory::remove, this, ut));
192 /* if the current undo history is larger than or equal to the currently
193 requested depth, then pop off at least 1 element to make space
194 at the back for new one.
197 if ((_depth > 0) && current_depth && (current_depth >= _depth)) {
199 uint32_t cnt = 1 + (current_depth - _depth);
203 ut = UndoList.front ();
204 UndoList.pop_front ();
209 UndoList.push_back (ut);
211 /* we are now owners of the transaction and must delete it when finished with it */
213 Changed (); /* EMIT SIGNAL */
217 UndoHistory::remove (UndoTransaction* const ut)
223 UndoList.remove (ut);
224 RedoList.remove (ut);
226 Changed (); /* EMIT SIGNAL */
229 /** Undo some transactions.
230 * @param n Number of transactions to undo.
233 UndoHistory::undo (unsigned int n)
236 if (UndoList.size() == 0) {
239 UndoTransaction* ut = UndoList.back ();
240 UndoList.pop_back ();
242 RedoList.push_back (ut);
245 Changed (); /* EMIT SIGNAL */
249 UndoHistory::redo (unsigned int n)
252 if (RedoList.size() == 0) {
255 UndoTransaction* ut = RedoList.back ();
256 RedoList.pop_back ();
258 UndoList.push_back (ut);
261 Changed (); /* EMIT SIGNAL */
265 UndoHistory::clear_redo ()
271 Changed (); /* EMIT SIGNAL */
276 UndoHistory::clear_undo ()
282 Changed (); /* EMIT SIGNAL */
286 UndoHistory::clear ()
291 Changed (); /* EMIT SIGNAL */
295 UndoHistory::get_state (int32_t depth)
297 XMLNode *node = new XMLNode ("UndoHistory");
303 } else if (depth < 0) {
307 for (list<UndoTransaction*>::iterator it = UndoList.begin(); it != UndoList.end(); ++it) {
308 node->add_child_nocopy((*it)->get_state());
313 /* just the last "depth" transactions */
315 list<UndoTransaction*> in_order;
317 for (list<UndoTransaction*>::reverse_iterator it = UndoList.rbegin(); it != UndoList.rend() && depth; ++it, depth--) {
318 in_order.push_front (*it);
321 for (list<UndoTransaction*>::iterator it = in_order.begin(); it != in_order.end(); it++) {
322 node->add_child_nocopy((*it)->get_state());