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)
45 actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
48 UndoTransaction::~UndoTransaction ()
50 cerr << "UndoTransaction destroyed\n";
56 command_death (UndoTransaction* ut, Command* c)
58 cerr << "Command drop ref\n";
64 ut->remove_command (c);
72 UndoTransaction::operator= (const UndoTransaction& rhs)
74 if (this == &rhs) return *this;
77 actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
82 UndoTransaction::add_command (Command *const cmd)
84 /* catch death of command (e.g. caused by death of object to
85 which it refers. command_death() is a normal static function
86 so there is no need to manage this connection.
89 cmd->DropReferences.connect_same_thread (*this, boost::bind (&command_death, this, cmd));
90 actions.push_back (cmd);
94 UndoTransaction::remove_command (Command* const action)
96 actions.remove (action);
100 UndoTransaction::empty () const
102 return actions.empty();
106 UndoTransaction::clear ()
109 for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
117 UndoTransaction::operator() ()
119 for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
125 UndoTransaction::undo ()
127 for (list<Command*>::reverse_iterator i = actions.rbegin(); i != actions.rend(); ++i) {
133 UndoTransaction::redo ()
138 XMLNode &UndoTransaction::get_state()
140 XMLNode *node = new XMLNode ("UndoTransaction");
142 ss << _timestamp.tv_sec;
143 node->add_property("tv_sec", ss.str());
145 ss << _timestamp.tv_usec;
146 node->add_property("tv_usec", ss.str());
147 node->add_property("name", _name);
149 list<Command*>::iterator it;
150 for (it=actions.begin(); it!=actions.end(); it++)
151 node->add_child_nocopy((*it)->get_state());
156 class UndoRedoSignaller {
158 UndoRedoSignaller (UndoHistory& uh)
160 _history.BeginUndoRedo();
162 ~UndoRedoSignaller() {
163 _history.EndUndoRedo();
167 UndoHistory& _history;
170 UndoHistory::UndoHistory ()
177 UndoHistory::set_depth (uint32_t d)
180 uint32_t current_depth = UndoList.size();
184 if (d > current_depth) {
185 /* not even transactions to meet request */
191 uint32_t cnt = current_depth - d;
194 ut = UndoList.front();
195 UndoList.pop_front ();
202 UndoHistory::add (UndoTransaction* const ut)
204 uint32_t current_depth = UndoList.size();
206 ut->DropReferences.connect_same_thread (*this, boost::bind (&UndoHistory::remove, this, ut));
208 /* if the current undo history is larger than or equal to the currently
209 requested depth, then pop off at least 1 element to make space
210 at the back for new one.
213 if ((_depth > 0) && current_depth && (current_depth >= _depth)) {
215 uint32_t cnt = 1 + (current_depth - _depth);
219 ut = UndoList.front ();
220 UndoList.pop_front ();
225 UndoList.push_back (ut);
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 ()
303 Changed (); /* EMIT SIGNAL */
308 UndoHistory::clear_undo ()
314 Changed (); /* EMIT SIGNAL */
318 UndoHistory::clear ()
323 Changed (); /* EMIT SIGNAL */
327 UndoHistory::get_state (int32_t depth)
329 XMLNode *node = new XMLNode ("UndoHistory");
335 } else if (depth < 0) {
339 for (list<UndoTransaction*>::iterator it = UndoList.begin(); it != UndoList.end(); ++it) {
340 node->add_child_nocopy((*it)->get_state());
345 /* just the last "depth" transactions */
347 list<UndoTransaction*> in_order;
349 for (list<UndoTransaction*>::reverse_iterator it = UndoList.rbegin(); it != UndoList.rend() && depth; ++it, depth--) {
350 in_order.push_front (*it);
353 for (list<UndoTransaction*>::iterator it = in_order.begin(); it != in_order.end(); it++) {
354 node->add_child_nocopy((*it)->get_state());