Merged with trunk R846
[ardour.git] / libs / pbd / undo.cc
1 /* 
2     Copyright (C) 2001 Brett Viren & Paul Davis
3
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.
8
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.
13
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.
17
18     $Id$
19 */
20
21 #include <iostream>
22
23 #include <pbd/undo.h>
24 #include <pbd/xml++.h>
25 #include <string>
26 #include <sstream>
27
28 using namespace std;
29 using namespace sigc;
30
31 UndoTransaction::UndoTransaction ()
32 {
33 }
34
35 UndoTransaction::UndoTransaction (const UndoTransaction& rhs)
36 {
37         _name = rhs._name;
38         clear ();
39         actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
40 }
41
42 UndoTransaction& 
43 UndoTransaction::operator= (const UndoTransaction& rhs)
44 {
45         if (this == &rhs) return *this;
46         _name = rhs._name;
47         clear ();
48         actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
49         return *this;
50 }
51
52 void
53 UndoTransaction::add_command (Command *const action)
54 {
55         actions.push_back (action);
56 }
57
58 void
59 UndoTransaction::clear ()
60 {
61         actions.clear ();
62 }
63
64 void
65 UndoTransaction::operator() ()
66 {
67         for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
68                 (*(*i))();
69         }
70 }
71
72 void
73 UndoTransaction::undo ()
74 {
75         cerr << "Undo " << _name << endl;
76         for (list<Command*>::reverse_iterator i = actions.rbegin(); i != actions.rend(); ++i) {
77                 (*i)->undo();
78         }
79 }
80
81 void
82 UndoTransaction::redo ()
83 {
84         cerr << "Redo " << _name << endl;
85         (*this)();
86 }
87
88 XMLNode &UndoTransaction::get_state()
89 {
90     XMLNode *node = new XMLNode ("UndoTransaction");
91     stringstream ss;
92     ss << _timestamp.tv_sec;
93     node->add_property("tv_sec", ss.str());
94     ss.str("");
95     ss << _timestamp.tv_usec;
96     node->add_property("tv_usec", ss.str());
97     node->add_property("name", _name);
98
99     list<Command*>::iterator it;
100     for (it=actions.begin(); it!=actions.end(); it++)
101         node->add_child_nocopy((*it)->get_state());
102
103     return *node;
104 }
105
106 void
107 UndoHistory::add (UndoTransaction ut)
108 {
109         UndoList.push_back (ut);
110 }
111
112 void
113 UndoHistory::undo (unsigned int n)
114 {
115         while (n--) {
116                 if (UndoList.size() == 0) {
117                         return;
118                 }
119                 UndoTransaction ut = UndoList.back ();
120                 UndoList.pop_back ();
121                 ut.undo ();
122                 RedoList.push_back (ut);
123         }
124 }
125
126 void
127 UndoHistory::redo (unsigned int n)
128 {
129         while (n--) {
130                 if (RedoList.size() == 0) {
131                         return;
132                 }
133                 UndoTransaction ut = RedoList.back ();
134                 RedoList.pop_back ();
135                 ut.redo ();
136                 UndoList.push_back (ut);
137         }
138 }
139
140 void
141 UndoHistory::clear_redo ()
142 {
143         RedoList.clear ();
144 }
145
146 void
147 UndoHistory::clear_undo ()
148 {
149         UndoList.clear ();
150 }
151
152 void
153 UndoHistory::clear ()
154 {
155         RedoList.clear ();
156         UndoList.clear ();
157 }
158
159 XMLNode & UndoHistory::get_state()
160 {
161     XMLNode *node = new XMLNode ("UndoHistory");
162
163     list<UndoTransaction>::iterator it;
164     for (it=UndoList.begin(); it != UndoList.end(); it++)
165         node->add_child_nocopy(it->get_state());
166
167     return *node;
168 }