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)
43 _timestamp = rhs._timestamp;
45 actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
48 UndoTransaction::~UndoTransaction ()
55 command_death (UndoTransaction* ut, Command* c)
61 ut->remove_command (c);
69 UndoTransaction::operator= (const UndoTransaction& rhs)
71 if (this == &rhs) return *this;
74 actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
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);
91 UndoTransaction::remove_command (Command* const action)
93 actions.remove (action);
97 UndoTransaction::empty () const
99 return actions.empty();
103 UndoTransaction::clear ()
106 for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
114 UndoTransaction::operator() ()
116 for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
122 UndoTransaction::undo ()
124 for (list<Command*>::reverse_iterator i = actions.rbegin(); i != actions.rend(); ++i) {
130 UndoTransaction::redo ()
135 XMLNode &UndoTransaction::get_state()
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);
142 list<Command*>::iterator it;
143 for (it=actions.begin(); it!=actions.end(); it++)
144 node->add_child_nocopy((*it)->get_state());
149 class UndoRedoSignaller {
151 UndoRedoSignaller (UndoHistory& uh)
153 _history.BeginUndoRedo();
155 ~UndoRedoSignaller() {
156 _history.EndUndoRedo();
160 UndoHistory& _history;
163 UndoHistory::UndoHistory ()
170 UndoHistory::set_depth (uint32_t d)
173 uint32_t current_depth = UndoList.size();
177 if (d > current_depth) {
178 /* not even transactions to meet request */
184 uint32_t cnt = current_depth - d;
187 ut = UndoList.front();
188 UndoList.pop_front ();
195 UndoHistory::add (UndoTransaction* const ut)
197 uint32_t current_depth = UndoList.size();
199 ut->DropReferences.connect_same_thread (*this, boost::bind (&UndoHistory::remove, this, ut));
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.
206 if ((_depth > 0) && current_depth && (current_depth >= _depth)) {
208 uint32_t cnt = 1 + (current_depth - _depth);
212 ut = UndoList.front ();
213 UndoList.pop_front ();
218 UndoList.push_back (ut);
219 /* Adding a transacrion makes the redo list meaningless. */
221 for (std::list<UndoTransaction*>::iterator i = RedoList.begin(); i != RedoList.end(); ++i) {
227 /* we are now owners of the transaction and must delete it when finished with it */
229 Changed (); /* EMIT SIGNAL */
233 UndoHistory::remove (UndoTransaction* const ut)
239 UndoList.remove (ut);
240 RedoList.remove (ut);
242 Changed (); /* EMIT SIGNAL */
245 /** Undo some transactions.
246 * @param n Number of transactions to undo.
249 UndoHistory::undo (unsigned int n)
256 UndoRedoSignaller exception_safe_signaller (*this);
259 if (UndoList.size() == 0) {
262 UndoTransaction* ut = UndoList.back ();
263 UndoList.pop_back ();
265 RedoList.push_back (ut);
269 Changed (); /* EMIT SIGNAL */
273 UndoHistory::redo (unsigned int n)
280 UndoRedoSignaller exception_safe_signaller (*this);
283 if (RedoList.size() == 0) {
286 UndoTransaction* ut = RedoList.back ();
287 RedoList.pop_back ();
289 UndoList.push_back (ut);
293 Changed (); /* EMIT SIGNAL */
297 UndoHistory::clear_redo ()
300 for (std::list<UndoTransaction*>::iterator i = RedoList.begin(); i != RedoList.end(); ++i) {
306 Changed (); /* EMIT SIGNAL */
311 UndoHistory::clear_undo ()
314 for (std::list<UndoTransaction*>::iterator i = UndoList.begin(); i != UndoList.end(); ++i) {
320 Changed (); /* EMIT SIGNAL */
324 UndoHistory::clear ()
329 Changed (); /* EMIT SIGNAL */
333 UndoHistory::get_state (int32_t depth)
335 XMLNode *node = new XMLNode ("UndoHistory");
341 } else if (depth < 0) {
345 for (list<UndoTransaction*>::iterator it = UndoList.begin(); it != UndoList.end(); ++it) {
346 node->add_child_nocopy((*it)->get_state());
351 /* just the last "depth" transactions */
353 list<UndoTransaction*> in_order;
355 for (list<UndoTransaction*>::reverse_iterator it = UndoList.rbegin(); it != UndoList.rend() && depth; ++it, depth--) {
356 in_order.push_front (*it);
359 for (list<UndoTransaction*>::iterator it = in_order.begin(); it != in_order.end(); it++) {
360 node->add_child_nocopy((*it)->get_state());