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>
27 #include <pbd/shiva.h>
29 #include <sigc++/bind.h>
34 UndoTransaction::UndoTransaction ()
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)
81 new PBD::ProxyShiva<Command,UndoTransaction> (*action, *this, &command_death);
82 actions.push_back (action);
86 UndoTransaction::remove_command (Command* const action)
88 actions.remove (action);
92 UndoTransaction::empty () const
94 return actions.empty();
98 UndoTransaction::clear ()
101 for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
109 UndoTransaction::operator() ()
111 for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
117 UndoTransaction::undo ()
119 for (list<Command*>::reverse_iterator i = actions.rbegin(); i != actions.rend(); ++i) {
125 UndoTransaction::redo ()
130 XMLNode &UndoTransaction::get_state()
132 XMLNode *node = new XMLNode ("UndoTransaction");
134 ss << _timestamp.tv_sec;
135 node->add_property("tv_sec", ss.str());
137 ss << _timestamp.tv_usec;
138 node->add_property("tv_usec", ss.str());
139 node->add_property("name", _name);
141 list<Command*>::iterator it;
142 for (it=actions.begin(); it!=actions.end(); it++)
143 node->add_child_nocopy((*it)->get_state());
148 UndoHistory::UndoHistory ()
154 UndoHistory::add (UndoTransaction* const ut)
156 ut->GoingAway.connect (bind (mem_fun (*this, &UndoHistory::remove), ut));
157 UndoList.push_back (ut);
159 /* we are now owners of the transaction */
161 Changed (); /* EMIT SIGNAL */
165 UndoHistory::remove (UndoTransaction* const ut)
171 UndoList.remove (ut);
172 RedoList.remove (ut);
174 Changed (); /* EMIT SIGNAL */
177 /** Undo some transactions.
178 * @param n Number of transactions to undo.
181 UndoHistory::undo (unsigned int n)
184 if (UndoList.size() == 0) {
187 UndoTransaction* ut = UndoList.back ();
188 UndoList.pop_back ();
190 RedoList.push_back (ut);
193 Changed (); /* EMIT SIGNAL */
197 UndoHistory::redo (unsigned int n)
200 if (RedoList.size() == 0) {
203 UndoTransaction* ut = RedoList.back ();
204 RedoList.pop_back ();
206 UndoList.push_back (ut);
209 Changed (); /* EMIT SIGNAL */
213 UndoHistory::clear_redo ()
219 Changed (); /* EMIT SIGNAL */
224 UndoHistory::clear_undo ()
230 Changed (); /* EMIT SIGNAL */
234 UndoHistory::clear ()
239 Changed (); /* EMIT SIGNAL */
243 UndoHistory::get_state (uint32_t depth)
245 XMLNode *node = new XMLNode ("UndoHistory");
250 for (list<UndoTransaction*>::iterator it = UndoList.begin(); it != UndoList.end(); ++it) {
251 node->add_child_nocopy((*it)->get_state());
256 /* just the last "depth" transactions */
258 list<UndoTransaction*> in_order;
260 for (list<UndoTransaction*>::reverse_iterator it = UndoList.rbegin(); it != UndoList.rend() && depth; ++it, depth--) {
261 in_order.push_front (*it);
264 for (list<UndoTransaction*>::iterator it = in_order.begin(); it != in_order.end(); it++) {
265 node->add_child_nocopy((*it)->get_state());