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 /* grrr, strict C++ says that static member functions are not C functions, but we also want
34 to be able to pack this into a sigc::ptr_fun and not sigc::mem_fun, so we have to make
35 it a genuine function rather than a member.
38 static void command_death (UndoTransaction* ut, Command* c)
44 ut->remove_command (c);
52 UndoTransaction::UndoTransaction ()
57 UndoTransaction::UndoTransaction (const UndoTransaction& rhs)
62 actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
65 UndoTransaction::~UndoTransaction ()
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 action)
84 action->GoingAway.connect (bind (sigc::ptr_fun (command_death), this, const_cast<Command*>(action)));
85 actions.push_back (action);
89 UndoTransaction::remove_command (Command* const action)
91 actions.remove (action);
95 UndoTransaction::empty () const
97 return actions.empty();
101 UndoTransaction::clear ()
104 for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
112 UndoTransaction::operator() ()
114 for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
120 UndoTransaction::undo ()
122 for (list<Command*>::reverse_iterator i = actions.rbegin(); i != actions.rend(); ++i) {
128 UndoTransaction::redo ()
133 XMLNode &UndoTransaction::get_state()
135 XMLNode *node = new XMLNode ("UndoTransaction");
137 ss << _timestamp.tv_sec;
138 node->add_property("tv_sec", ss.str());
140 ss << _timestamp.tv_usec;
141 node->add_property("tv_usec", ss.str());
142 node->add_property("name", _name);
144 list<Command*>::iterator it;
145 for (it=actions.begin(); it!=actions.end(); it++)
146 node->add_child_nocopy((*it)->get_state());
151 UndoHistory::UndoHistory ()
157 UndoHistory::add (UndoTransaction* const ut)
159 ut->GoingAway.connect (bind (mem_fun (*this, &UndoHistory::remove), ut));
160 UndoList.push_back (ut);
162 /* we are now owners of the transaction */
166 UndoHistory::remove (UndoTransaction* const ut)
172 UndoList.remove (ut);
173 RedoList.remove (ut);
177 UndoHistory::undo (unsigned int n)
180 if (UndoList.size() == 0) {
183 UndoTransaction* ut = UndoList.back ();
184 UndoList.pop_back ();
186 RedoList.push_back (ut);
191 UndoHistory::redo (unsigned int n)
194 if (RedoList.size() == 0) {
197 UndoTransaction* ut = RedoList.back ();
198 RedoList.pop_back ();
200 UndoList.push_back (ut);
205 UndoHistory::clear_redo ()
213 UndoHistory::clear_undo ()
221 UndoHistory::clear ()
227 XMLNode & UndoHistory::get_state()
229 XMLNode *node = new XMLNode ("UndoHistory");
231 list<UndoTransaction*>::iterator it;
232 for (it = UndoList.begin(); it != UndoList.end(); it++) {
233 node->add_child_nocopy((*it)->get_state());