2005-05-13 16:47:18 -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-24 21:07:15 -04:00
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
2005-05-13 16:47:18 -04:00
|
|
|
|
|
|
|
#include <pbd/undo.h>
|
2006-08-09 10:14:17 -04:00
|
|
|
#include <pbd/xml++.h>
|
2006-10-25 16:11:42 -04:00
|
|
|
#include <pbd/shiva.h>
|
2006-08-24 21:07:15 -04:00
|
|
|
|
|
|
|
#include <sigc++/bind.h>
|
2005-05-13 16:47:18 -04:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace sigc;
|
|
|
|
|
2006-06-15 14:21:31 -04:00
|
|
|
UndoTransaction::UndoTransaction ()
|
2005-05-13 16:47:18 -04:00
|
|
|
{
|
2006-10-17 17:15:41 -04:00
|
|
|
_clearing = false;
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
|
|
|
|
2006-06-15 14:21:31 -04:00
|
|
|
UndoTransaction::UndoTransaction (const UndoTransaction& rhs)
|
2005-05-13 16:47:18 -04:00
|
|
|
{
|
|
|
|
_name = rhs._name;
|
2006-10-17 17:15:41 -04:00
|
|
|
_clearing = false;
|
2005-05-13 16:47:18 -04:00
|
|
|
clear ();
|
2006-06-29 14:49:03 -04:00
|
|
|
actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
|
|
|
|
2006-08-24 21:07:15 -04:00
|
|
|
UndoTransaction::~UndoTransaction ()
|
|
|
|
{
|
|
|
|
GoingAway ();
|
|
|
|
clear ();
|
|
|
|
}
|
|
|
|
|
2006-10-25 16:11:42 -04:00
|
|
|
void
|
|
|
|
command_death (UndoTransaction* ut, Command* c)
|
|
|
|
{
|
|
|
|
if (ut->clearing()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ut->remove_command (c);
|
|
|
|
|
|
|
|
if (ut->empty()) {
|
|
|
|
delete ut;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-15 14:21:31 -04:00
|
|
|
UndoTransaction&
|
|
|
|
UndoTransaction::operator= (const UndoTransaction& rhs)
|
2005-05-13 16:47:18 -04:00
|
|
|
{
|
|
|
|
if (this == &rhs) return *this;
|
|
|
|
_name = rhs._name;
|
|
|
|
clear ();
|
2006-06-29 14:49:03 -04:00
|
|
|
actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
|
2005-05-13 16:47:18 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-07-28 23:17:11 -04:00
|
|
|
UndoTransaction::add_command (Command *const action)
|
2005-05-13 16:47:18 -04:00
|
|
|
{
|
2006-10-25 16:11:42 -04:00
|
|
|
/* catch death */
|
|
|
|
new PBD::ProxyShiva<Command,UndoTransaction> (*action, *this, &command_death);
|
2006-06-29 14:49:03 -04:00
|
|
|
actions.push_back (action);
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
|
|
|
|
2006-08-24 21:07:15 -04:00
|
|
|
void
|
|
|
|
UndoTransaction::remove_command (Command* const action)
|
|
|
|
{
|
|
|
|
actions.remove (action);
|
2006-10-17 17:15:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
UndoTransaction::empty () const
|
|
|
|
{
|
|
|
|
return actions.empty();
|
2006-08-24 21:07:15 -04:00
|
|
|
}
|
|
|
|
|
2005-05-13 16:47:18 -04:00
|
|
|
void
|
2006-06-29 14:49:03 -04:00
|
|
|
UndoTransaction::clear ()
|
2005-05-13 16:47:18 -04:00
|
|
|
{
|
2006-10-17 17:15:41 -04:00
|
|
|
_clearing = true;
|
2006-08-24 21:07:15 -04:00
|
|
|
for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
|
|
|
|
delete *i;
|
|
|
|
}
|
2006-06-29 14:49:03 -04:00
|
|
|
actions.clear ();
|
2006-10-17 17:15:41 -04:00
|
|
|
_clearing = false;
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-06-29 14:49:03 -04:00
|
|
|
UndoTransaction::operator() ()
|
2005-05-13 16:47:18 -04:00
|
|
|
{
|
2006-07-28 23:17:11 -04:00
|
|
|
for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
|
|
|
|
(*(*i))();
|
2006-06-29 14:49:03 -04:00
|
|
|
}
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-06-15 14:21:31 -04:00
|
|
|
UndoTransaction::undo ()
|
2005-05-13 16:47:18 -04:00
|
|
|
{
|
2006-07-28 23:17:11 -04:00
|
|
|
for (list<Command*>::reverse_iterator i = actions.rbegin(); i != actions.rend(); ++i) {
|
|
|
|
(*i)->undo();
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2006-06-15 14:21:31 -04:00
|
|
|
UndoTransaction::redo ()
|
2005-05-13 16:47:18 -04:00
|
|
|
{
|
2006-06-29 14:49:03 -04:00
|
|
|
(*this)();
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
|
|
|
|
2006-08-09 10:14:17 -04:00
|
|
|
XMLNode &UndoTransaction::get_state()
|
2006-08-03 17:54:14 -04:00
|
|
|
{
|
|
|
|
XMLNode *node = new XMLNode ("UndoTransaction");
|
2006-08-12 17:49:20 -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 10:15:05 -04:00
|
|
|
|
|
|
|
list<Command*>::iterator it;
|
|
|
|
for (it=actions.begin(); it!=actions.end(); it++)
|
|
|
|
node->add_child_nocopy((*it)->get_state());
|
|
|
|
|
2006-08-03 17:54:14 -04:00
|
|
|
return *node;
|
|
|
|
}
|
|
|
|
|
2006-08-24 21:07:15 -04:00
|
|
|
UndoHistory::UndoHistory ()
|
|
|
|
{
|
2006-10-17 17:15:41 -04:00
|
|
|
_clearing = false;
|
2006-08-24 21:07:15 -04:00
|
|
|
}
|
|
|
|
|
2005-05-13 16:47:18 -04:00
|
|
|
void
|
2006-08-24 21:07:15 -04:00
|
|
|
UndoHistory::add (UndoTransaction* const ut)
|
2005-05-13 16:47:18 -04:00
|
|
|
{
|
2006-08-24 21:07:15 -04:00
|
|
|
ut->GoingAway.connect (bind (mem_fun (*this, &UndoHistory::remove), ut));
|
2006-06-15 14:21:31 -04:00
|
|
|
UndoList.push_back (ut);
|
2006-08-24 21:07:15 -04:00
|
|
|
|
|
|
|
/* we are now owners of the transaction */
|
2006-11-16 13:42:48 -05:00
|
|
|
|
|
|
|
Changed (); /* EMIT SIGNAL */
|
2006-08-24 21:07:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UndoHistory::remove (UndoTransaction* const ut)
|
|
|
|
{
|
2006-10-17 17:15:41 -04:00
|
|
|
if (_clearing) {
|
2006-08-24 21:07:15 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UndoList.remove (ut);
|
|
|
|
RedoList.remove (ut);
|
2006-11-16 13:42:48 -05:00
|
|
|
|
|
|
|
Changed (); /* EMIT SIGNAL */
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UndoHistory::undo (unsigned int n)
|
|
|
|
{
|
|
|
|
while (n--) {
|
|
|
|
if (UndoList.size() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2006-08-24 21:07:15 -04:00
|
|
|
UndoTransaction* ut = UndoList.back ();
|
2005-05-13 16:47:18 -04:00
|
|
|
UndoList.pop_back ();
|
2006-08-24 21:07:15 -04:00
|
|
|
ut->undo ();
|
2006-06-15 14:21:31 -04:00
|
|
|
RedoList.push_back (ut);
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
2006-11-16 13:42:48 -05:00
|
|
|
|
|
|
|
Changed (); /* EMIT SIGNAL */
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UndoHistory::redo (unsigned int n)
|
|
|
|
{
|
|
|
|
while (n--) {
|
|
|
|
if (RedoList.size() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2006-08-24 21:07:15 -04:00
|
|
|
UndoTransaction* ut = RedoList.back ();
|
2005-05-13 16:47:18 -04:00
|
|
|
RedoList.pop_back ();
|
2006-08-24 21:07:15 -04:00
|
|
|
ut->redo ();
|
2006-07-28 23:17:11 -04:00
|
|
|
UndoList.push_back (ut);
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
2006-11-16 13:42:48 -05:00
|
|
|
|
|
|
|
Changed (); /* EMIT SIGNAL */
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UndoHistory::clear_redo ()
|
|
|
|
{
|
2006-10-17 17:15:41 -04:00
|
|
|
_clearing = true;
|
2005-05-13 16:47:18 -04:00
|
|
|
RedoList.clear ();
|
2006-10-17 17:15:41 -04:00
|
|
|
_clearing = false;
|
2006-11-16 13:42:48 -05:00
|
|
|
|
|
|
|
Changed (); /* EMIT SIGNAL */
|
|
|
|
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UndoHistory::clear_undo ()
|
|
|
|
{
|
2006-10-17 17:15:41 -04:00
|
|
|
_clearing = true;
|
2005-05-13 16:47:18 -04:00
|
|
|
UndoList.clear ();
|
2006-10-17 17:15:41 -04:00
|
|
|
_clearing = false;
|
2006-11-16 13:42:48 -05:00
|
|
|
|
|
|
|
Changed (); /* EMIT SIGNAL */
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UndoHistory::clear ()
|
|
|
|
{
|
2006-08-24 21:07:15 -04:00
|
|
|
clear_undo ();
|
|
|
|
clear_redo ();
|
2006-11-16 13:42:48 -05:00
|
|
|
|
|
|
|
Changed (); /* EMIT SIGNAL */
|
2005-05-13 16:47:18 -04:00
|
|
|
}
|
2006-08-09 10:15:05 -04:00
|
|
|
|
2006-12-11 07:52:40 -05:00
|
|
|
XMLNode&
|
|
|
|
UndoHistory::get_state (uint32_t depth)
|
2006-08-09 10:15:05 -04:00
|
|
|
{
|
|
|
|
XMLNode *node = new XMLNode ("UndoHistory");
|
|
|
|
|
2006-12-11 07:52:40 -05:00
|
|
|
if (depth == 0) {
|
|
|
|
/* everything */
|
|
|
|
|
|
|
|
for (list<UndoTransaction*>::iterator it = UndoList.begin(); it != UndoList.end(); ++it) {
|
|
|
|
node->add_child_nocopy((*it)->get_state());
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* just the last "depth" transactions */
|
|
|
|
|
|
|
|
list<UndoTransaction*> in_order;
|
|
|
|
|
|
|
|
for (list<UndoTransaction*>::reverse_iterator it = UndoList.rbegin(); it != UndoList.rend() && depth; ++it, depth--) {
|
|
|
|
in_order.push_front (*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (list<UndoTransaction*>::iterator it = in_order.begin(); it != in_order.end(); it++) {
|
|
|
|
node->add_child_nocopy((*it)->get_state());
|
|
|
|
}
|
2006-08-24 21:07:15 -04:00
|
|
|
}
|
2006-08-09 10:15:05 -04:00
|
|
|
|
|
|
|
return *node;
|
|
|
|
}
|