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.
26 #include "pbd/xml++.h"
28 #include <sigc++/bind.h>
33 UndoTransaction::UndoTransaction ()
36 gettimeofday (&_timestamp, 0);
39 UndoTransaction::UndoTransaction (const UndoTransaction& rhs)
44 actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
47 UndoTransaction::~UndoTransaction ()
54 command_death (UndoTransaction* ut, Command* c)
60 ut->remove_command (c);
68 UndoTransaction::operator= (const UndoTransaction& rhs)
70 if (this == &rhs) return *this;
73 actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
78 UndoTransaction::add_command (Command *const action)
80 /* catch death of command (e.g. caused by death of object to
81 which it refers. command_death() is a normal static function
82 so there is no need to manage this connection.
85 scoped_connect (action->GoingAway, boost::bind (&command_death, this, action));
86 actions.push_back (action);
90 UndoTransaction::remove_command (Command* const action)
92 actions.remove (action);
96 UndoTransaction::empty () const
98 return actions.empty();
102 UndoTransaction::clear ()
105 for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
113 UndoTransaction::operator() ()
115 for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
121 UndoTransaction::undo ()
123 for (list<Command*>::reverse_iterator i = actions.rbegin(); i != actions.rend(); ++i) {
129 UndoTransaction::redo ()
134 XMLNode &UndoTransaction::get_state()
136 XMLNode *node = new XMLNode ("UndoTransaction");
138 ss << _timestamp.tv_sec;
139 node->add_property("tv_sec", ss.str());
141 ss << _timestamp.tv_usec;
142 node->add_property("tv_usec", ss.str());
143 node->add_property("name", _name);
145 list<Command*>::iterator it;
146 for (it=actions.begin(); it!=actions.end(); it++)
147 node->add_child_nocopy((*it)->get_state());
152 UndoHistory::UndoHistory ()
159 UndoHistory::set_depth (uint32_t d)
162 uint32_t current_depth = UndoList.size();
166 if (d > current_depth) {
167 /* not even transactions to meet request */
173 uint32_t cnt = current_depth - d;
176 ut = UndoList.front();
177 UndoList.pop_front ();
184 UndoHistory::add (UndoTransaction* const ut)
186 uint32_t current_depth = UndoList.size();
188 scoped_connect (ut->GoingAway, boost::bind (&UndoHistory::remove, this, ut));
190 /* if the current undo history is larger than or equal to the currently
191 requested depth, then pop off at least 1 element to make space
192 at the back for new one.
195 if ((_depth > 0) && current_depth && (current_depth >= _depth)) {
197 uint32_t cnt = 1 + (current_depth - _depth);
201 ut = UndoList.front ();
202 UndoList.pop_front ();
207 UndoList.push_back (ut);
209 /* we are now owners of the transaction and must delete it when finished with it */
211 Changed (); /* EMIT SIGNAL */
215 UndoHistory::remove (UndoTransaction* const ut)
221 UndoList.remove (ut);
222 RedoList.remove (ut);
224 Changed (); /* EMIT SIGNAL */
227 /** Undo some transactions.
228 * @param n Number of transactions to undo.
231 UndoHistory::undo (unsigned int n)
234 if (UndoList.size() == 0) {
237 UndoTransaction* ut = UndoList.back ();
238 UndoList.pop_back ();
240 RedoList.push_back (ut);
243 Changed (); /* EMIT SIGNAL */
247 UndoHistory::redo (unsigned int n)
250 if (RedoList.size() == 0) {
253 UndoTransaction* ut = RedoList.back ();
254 RedoList.pop_back ();
256 UndoList.push_back (ut);
259 Changed (); /* EMIT SIGNAL */
263 UndoHistory::clear_redo ()
269 Changed (); /* EMIT SIGNAL */
274 UndoHistory::clear_undo ()
280 Changed (); /* EMIT SIGNAL */
284 UndoHistory::clear ()
289 Changed (); /* EMIT SIGNAL */
293 UndoHistory::get_state (int32_t depth)
295 XMLNode *node = new XMLNode ("UndoHistory");
301 } else if (depth < 0) {
305 for (list<UndoTransaction*>::iterator it = UndoList.begin(); it != UndoList.end(); ++it) {
306 node->add_child_nocopy((*it)->get_state());
311 /* just the last "depth" transactions */
313 list<UndoTransaction*> in_order;
315 for (list<UndoTransaction*>::reverse_iterator it = UndoList.rbegin(); it != UndoList.rend() && depth; ++it, depth--) {
316 in_order.push_front (*it);
319 for (list<UndoTransaction*>::iterator it = in_order.begin(); it != in_order.end(); it++) {
320 node->add_child_nocopy((*it)->get_state());