2006-07-05 15:47:25 -04:00
|
|
|
/*
|
|
|
|
Copyright (C) 2001 Brett Viren & Paul Davis
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
$Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
2006-08-29 17:21:48 -04:00
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
2006-07-05 15:47:25 -04:00
|
|
|
|
|
|
|
#include <pbd/undo.h>
|
2006-08-09 21:22:45 -04:00
|
|
|
#include <pbd/xml++.h>
|
2006-08-29 17:21:48 -04:00
|
|
|
|
|
|
|
#include <sigc++/bind.h>
|
2006-07-05 15:47:25 -04:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace sigc;
|
|
|
|
|
2006-10-21 15:01:50 -04:00
|
|
|
/* grrr, strict C++ says that static member functions are not C functions, but we also want
|
|
|
|
to be able to pack this into a sigc::ptr_fun and not sigc::mem_fun, so we have to make
|
|
|
|
it a genuine function rather than a member.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void command_death (UndoTransaction* ut, Command* c)
|
|
|
|
{
|
|
|
|
if (ut->clearing()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ut->remove_command (c);
|
|
|
|
|
|
|
|
if (ut->empty()) {
|
|
|
|
delete ut;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-09 21:22:45 -04:00
|
|
|
UndoTransaction::UndoTransaction ()
|
2006-07-05 15:47:25 -04:00
|
|
|
{
|
2006-10-21 15:01:50 -04:00
|
|
|
_clearing = false;
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
2006-08-09 21:22:45 -04:00
|
|
|
UndoTransaction::UndoTransaction (const UndoTransaction& rhs)
|
2006-07-05 15:47:25 -04:00
|
|
|
{
|
|
|
|
_name = rhs._name;
|
2006-10-21 15:01:50 -04:00
|
|
|
_clearing = false;
|
2006-07-05 15:47:25 -04:00
|
|
|
clear ();
|
2006-08-09 21:22:45 -04:00
|
|
|
actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
2006-08-29 17:21:48 -04:00
|
|
|
UndoTransaction::~UndoTransaction ()
|
|
|
|
{
|
|
|
|
GoingAway ();
|
|
|
|
clear ();
|
|
|
|
}
|
|
|
|
|
2006-08-09 21:22:45 -04:00
|
|
|
UndoTransaction&
|
|
|
|
UndoTransaction::operator= (const UndoTransaction& rhs)
|
2006-07-05 15:47:25 -04:00
|
|
|
{
|
|
|
|
if (this == &rhs) return *this;
|
|
|
|
_name = rhs._name;
|
|
|
|
clear ();
|
2006-08-09 21:22:45 -04:00
|
|
|
actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
|
2006-07-05 15:47:25 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-08-09 21:22:45 -04:00
|
|
|
UndoTransaction::add_command (Command *const action)
|
2006-07-05 15:47:25 -04:00
|
|
|
{
|
2006-10-21 15:01:50 -04:00
|
|
|
action->GoingAway.connect (bind (sigc::ptr_fun (command_death), this, const_cast<Command*>(action)));
|
2006-08-09 21:22:45 -04:00
|
|
|
actions.push_back (action);
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
2006-08-29 17:21:48 -04:00
|
|
|
void
|
|
|
|
UndoTransaction::remove_command (Command* const action)
|
|
|
|
{
|
|
|
|
actions.remove (action);
|
2006-10-21 15:01:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
UndoTransaction::empty () const
|
|
|
|
{
|
|
|
|
return actions.empty();
|
2006-08-29 17:21:48 -04:00
|
|
|
}
|
|
|
|
|
2006-07-05 15:47:25 -04:00
|
|
|
void
|
2006-08-09 21:22:45 -04:00
|
|
|
UndoTransaction::clear ()
|
2006-07-05 15:47:25 -04:00
|
|
|
{
|
2006-10-21 15:01:50 -04:00
|
|
|
_clearing = true;
|
2006-08-29 17:21:48 -04:00
|
|
|
for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
|
|
|
|
delete *i;
|
|
|
|
}
|
2006-08-09 21:22:45 -04:00
|
|
|
actions.clear ();
|
2006-10-21 15:01:50 -04:00
|
|
|
_clearing = false;
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-08-09 21:22:45 -04:00
|
|
|
UndoTransaction::operator() ()
|
2006-07-05 15:47:25 -04:00
|
|
|
{
|
2006-08-09 21:22:45 -04:00
|
|
|
for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
|
|
|
|
(*(*i))();
|
|
|
|
}
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-08-09 21:22:45 -04:00
|
|
|
UndoTransaction::undo ()
|
2006-07-05 15:47:25 -04:00
|
|
|
{
|
2006-08-09 21:22:45 -04:00
|
|
|
for (list<Command*>::reverse_iterator i = actions.rbegin(); i != actions.rend(); ++i) {
|
|
|
|
(*i)->undo();
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-08-09 21:22:45 -04:00
|
|
|
UndoTransaction::redo ()
|
2006-07-05 15:47:25 -04:00
|
|
|
{
|
2006-08-09 21:22:45 -04:00
|
|
|
(*this)();
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLNode &UndoTransaction::get_state()
|
|
|
|
{
|
|
|
|
XMLNode *node = new XMLNode ("UndoTransaction");
|
2006-08-24 03:37:17 -04:00
|
|
|
stringstream ss;
|
|
|
|
ss << _timestamp.tv_sec;
|
|
|
|
node->add_property("tv_sec", ss.str());
|
|
|
|
ss.str("");
|
|
|
|
ss << _timestamp.tv_usec;
|
|
|
|
node->add_property("tv_usec", ss.str());
|
|
|
|
node->add_property("name", _name);
|
2006-08-09 21:22:45 -04:00
|
|
|
|
|
|
|
list<Command*>::iterator it;
|
|
|
|
for (it=actions.begin(); it!=actions.end(); it++)
|
|
|
|
node->add_child_nocopy((*it)->get_state());
|
|
|
|
|
|
|
|
return *node;
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
2006-08-29 17:21:48 -04:00
|
|
|
UndoHistory::UndoHistory ()
|
|
|
|
{
|
2006-10-21 15:01:50 -04:00
|
|
|
_clearing = false;
|
2006-08-29 17:21:48 -04:00
|
|
|
}
|
|
|
|
|
2006-07-05 15:47:25 -04:00
|
|
|
void
|
2006-08-29 17:21:48 -04:00
|
|
|
UndoHistory::add (UndoTransaction* const ut)
|
2006-07-05 15:47:25 -04:00
|
|
|
{
|
2006-08-29 17:21:48 -04:00
|
|
|
ut->GoingAway.connect (bind (mem_fun (*this, &UndoHistory::remove), ut));
|
2006-08-09 21:22:45 -04:00
|
|
|
UndoList.push_back (ut);
|
2006-08-29 17:21:48 -04:00
|
|
|
|
|
|
|
/* we are now owners of the transaction */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UndoHistory::remove (UndoTransaction* const ut)
|
|
|
|
{
|
2006-10-21 15:01:50 -04:00
|
|
|
if (_clearing) {
|
2006-08-29 17:21:48 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UndoList.remove (ut);
|
|
|
|
RedoList.remove (ut);
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UndoHistory::undo (unsigned int n)
|
|
|
|
{
|
|
|
|
while (n--) {
|
|
|
|
if (UndoList.size() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2006-08-29 17:21:48 -04:00
|
|
|
UndoTransaction* ut = UndoList.back ();
|
2006-07-05 15:47:25 -04:00
|
|
|
UndoList.pop_back ();
|
2006-08-29 17:21:48 -04:00
|
|
|
ut->undo ();
|
2006-08-09 21:22:45 -04:00
|
|
|
RedoList.push_back (ut);
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UndoHistory::redo (unsigned int n)
|
|
|
|
{
|
|
|
|
while (n--) {
|
|
|
|
if (RedoList.size() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2006-08-29 17:21:48 -04:00
|
|
|
UndoTransaction* ut = RedoList.back ();
|
2006-07-05 15:47:25 -04:00
|
|
|
RedoList.pop_back ();
|
2006-08-29 17:21:48 -04:00
|
|
|
ut->redo ();
|
2006-08-09 21:22:45 -04:00
|
|
|
UndoList.push_back (ut);
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UndoHistory::clear_redo ()
|
|
|
|
{
|
2006-10-21 15:01:50 -04:00
|
|
|
_clearing = true;
|
2006-07-05 15:47:25 -04:00
|
|
|
RedoList.clear ();
|
2006-10-21 15:01:50 -04:00
|
|
|
_clearing = false;
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UndoHistory::clear_undo ()
|
|
|
|
{
|
2006-10-21 15:01:50 -04:00
|
|
|
_clearing = true;
|
2006-07-05 15:47:25 -04:00
|
|
|
UndoList.clear ();
|
2006-10-21 15:01:50 -04:00
|
|
|
_clearing = false;
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UndoHistory::clear ()
|
|
|
|
{
|
2006-08-29 17:21:48 -04:00
|
|
|
clear_undo ();
|
|
|
|
clear_redo ();
|
2006-07-05 15:47:25 -04:00
|
|
|
}
|
2006-08-09 21:22:45 -04:00
|
|
|
|
|
|
|
XMLNode & UndoHistory::get_state()
|
|
|
|
{
|
|
|
|
XMLNode *node = new XMLNode ("UndoHistory");
|
|
|
|
|
2006-08-29 17:21:48 -04:00
|
|
|
list<UndoTransaction*>::iterator it;
|
|
|
|
for (it = UndoList.begin(); it != UndoList.end(); it++) {
|
|
|
|
node->add_child_nocopy((*it)->get_state());
|
|
|
|
}
|
2006-08-09 21:22:45 -04:00
|
|
|
|
|
|
|
return *node;
|
|
|
|
}
|