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>
28 #include <pbd/shiva.h>
30 #include <sigc++/bind.h>
35 UndoTransaction::UndoTransaction ()
38 gettimeofday (&_timestamp, 0);
41 UndoTransaction::UndoTransaction (const UndoTransaction& rhs)
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)
83 new PBD::ProxyShiva<Command,UndoTransaction> (*action, *this, &command_death);
84 actions.push_back (action);
88 UndoTransaction::remove_command (Command* const action)
90 actions.remove (action);
94 UndoTransaction::empty () const
96 return actions.empty();
100 UndoTransaction::clear ()
103 for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
111 UndoTransaction::operator() ()
113 for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
119 UndoTransaction::undo ()
121 for (list<Command*>::reverse_iterator i = actions.rbegin(); i != actions.rend(); ++i) {
127 UndoTransaction::redo ()
132 XMLNode &UndoTransaction::get_state()
134 XMLNode *node = new XMLNode ("UndoTransaction");
136 ss << _timestamp.tv_sec;
137 node->add_property("tv_sec", ss.str());
139 ss << _timestamp.tv_usec;
140 node->add_property("tv_usec", ss.str());
141 node->add_property("name", _name);
143 list<Command*>::iterator it;
144 for (it=actions.begin(); it!=actions.end(); it++)
145 node->add_child_nocopy((*it)->get_state());
150 UndoHistory::UndoHistory ()
157 UndoHistory::set_depth (int32_t d)
161 while (_depth > 0 && UndoList.size() > (uint32_t) _depth) {
162 UndoList.pop_front ();
167 UndoHistory::add (UndoTransaction* const ut)
169 ut->GoingAway.connect (bind (mem_fun (*this, &UndoHistory::remove), ut));
171 while (_depth > 0 && UndoList.size() > (uint32_t) _depth) {
172 UndoList.pop_front ();
175 UndoList.push_back (ut);
177 /* we are now owners of the transaction */
179 Changed (); /* EMIT SIGNAL */
183 UndoHistory::remove (UndoTransaction* const ut)
189 UndoList.remove (ut);
190 RedoList.remove (ut);
192 Changed (); /* EMIT SIGNAL */
195 /** Undo some transactions.
196 * @param n Number of transactions to undo.
199 UndoHistory::undo (unsigned int n)
202 if (UndoList.size() == 0) {
205 UndoTransaction* ut = UndoList.back ();
206 UndoList.pop_back ();
208 RedoList.push_back (ut);
211 Changed (); /* EMIT SIGNAL */
215 UndoHistory::redo (unsigned int n)
218 if (RedoList.size() == 0) {
221 UndoTransaction* ut = RedoList.back ();
222 RedoList.pop_back ();
224 UndoList.push_back (ut);
227 Changed (); /* EMIT SIGNAL */
231 UndoHistory::clear_redo ()
237 Changed (); /* EMIT SIGNAL */
242 UndoHistory::clear_undo ()
248 Changed (); /* EMIT SIGNAL */
252 UndoHistory::clear ()
257 Changed (); /* EMIT SIGNAL */
261 UndoHistory::get_state (int32_t depth)
263 XMLNode *node = new XMLNode ("UndoHistory");
269 } else if (depth < 0) {
273 for (list<UndoTransaction*>::iterator it = UndoList.begin(); it != UndoList.end(); ++it) {
274 node->add_child_nocopy((*it)->get_state());
279 /* just the last "depth" transactions */
281 list<UndoTransaction*> in_order;
283 for (list<UndoTransaction*>::reverse_iterator it = UndoList.rbegin(); it != UndoList.rend() && depth; ++it, depth--) {
284 in_order.push_front (*it);
287 for (list<UndoTransaction*>::iterator it = in_order.begin(); it != in_order.end(); it++) {
288 node->add_child_nocopy((*it)->get_state());