Ardour session files specify UTF-8.
[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 #include <string>
23 #include <sstream>
24
25 #include <pbd/undo.h>
26 #include <pbd/xml++.h>
27 #include <pbd/shiva.h>
28
29 #include <sigc++/bind.h>
30
31 using namespace std;
32 using namespace sigc;
33
34 UndoTransaction::UndoTransaction ()
35 {
36         _clearing = false;
37 }
38
39 UndoTransaction::UndoTransaction (const UndoTransaction& rhs)
40 {
41         _name = rhs._name;
42         _clearing = false;
43         clear ();
44         actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
45 }
46
47 UndoTransaction::~UndoTransaction ()
48 {
49         GoingAway ();
50         clear ();
51 }
52
53 void 
54 command_death (UndoTransaction* ut, Command* c)
55 {
56         if (ut->clearing()) {
57                 return;
58         }
59
60         ut->remove_command (c);
61
62         if (ut->empty()) {
63                 delete ut;
64         }
65 }
66
67 UndoTransaction& 
68 UndoTransaction::operator= (const UndoTransaction& rhs)
69 {
70         if (this == &rhs) return *this;
71         _name = rhs._name;
72         clear ();
73         actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
74         return *this;
75 }
76
77 void
78 UndoTransaction::add_command (Command *const action)
79 {
80         /* catch death */
81         new PBD::ProxyShiva<Command,UndoTransaction> (*action, *this, &command_death);
82         actions.push_back (action);
83 }
84
85 void
86 UndoTransaction::remove_command (Command* const action)
87 {
88         actions.remove (action);
89 }
90
91 bool
92 UndoTransaction::empty () const
93 {
94         return actions.empty();
95 }
96
97 void
98 UndoTransaction::clear ()
99 {
100         _clearing = true;
101         for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
102                 delete *i;
103         }
104         actions.clear ();
105         _clearing = false;
106 }
107
108 void
109 UndoTransaction::operator() ()
110 {
111         for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
112                 (*(*i))();
113         }
114 }
115
116 void
117 UndoTransaction::undo ()
118 {
119         for (list<Command*>::reverse_iterator i = actions.rbegin(); i != actions.rend(); ++i) {
120                 (*i)->undo();
121         }
122 }
123
124 void
125 UndoTransaction::redo ()
126 {
127         (*this)();
128 }
129
130 XMLNode &UndoTransaction::get_state()
131 {
132     XMLNode *node = new XMLNode ("UndoTransaction");
133     stringstream ss;
134     ss << _timestamp.tv_sec;
135     node->add_property("tv_sec", ss.str());
136     ss.str("");
137     ss << _timestamp.tv_usec;
138     node->add_property("tv_usec", ss.str());
139     node->add_property("name", _name);
140
141     list<Command*>::iterator it;
142     for (it=actions.begin(); it!=actions.end(); it++)
143         node->add_child_nocopy((*it)->get_state());
144
145     return *node;
146 }
147
148 UndoHistory::UndoHistory ()
149 {
150         _clearing = false;
151 }
152
153 void
154 UndoHistory::add (UndoTransaction* const ut)
155 {
156         ut->GoingAway.connect (bind (mem_fun (*this, &UndoHistory::remove), ut));
157         UndoList.push_back (ut);
158
159         /* we are now owners of the transaction */
160 }
161
162 void
163 UndoHistory::remove (UndoTransaction* const ut)
164 {
165         if (_clearing) {
166                 return;
167         }
168
169         UndoList.remove (ut);
170         RedoList.remove (ut);
171 }
172
173 void
174 UndoHistory::undo (unsigned int n)
175 {
176         while (n--) {
177                 if (UndoList.size() == 0) {
178                         return;
179                 }
180                 UndoTransaction* ut = UndoList.back ();
181                 UndoList.pop_back ();
182                 ut->undo ();
183                 RedoList.push_back (ut);
184         }
185 }
186
187 void
188 UndoHistory::redo (unsigned int n)
189 {
190         while (n--) {
191                 if (RedoList.size() == 0) {
192                         return;
193                 }
194                 UndoTransaction* ut = RedoList.back ();
195                 RedoList.pop_back ();
196                 ut->redo ();
197                 UndoList.push_back (ut);
198         }
199 }
200
201 void
202 UndoHistory::clear_redo ()
203 {
204         _clearing = true;
205         RedoList.clear ();
206         _clearing = false;
207 }
208
209 void
210 UndoHistory::clear_undo ()
211 {
212         _clearing = true;
213         UndoList.clear ();
214         _clearing = false;
215 }
216
217 void
218 UndoHistory::clear ()
219 {
220         clear_undo ();
221         clear_redo ();
222 }
223
224 XMLNode & UndoHistory::get_state()
225 {
226     XMLNode *node = new XMLNode ("UndoHistory");
227
228     list<UndoTransaction*>::iterator it;
229     for (it = UndoList.begin(); it != UndoList.end(); it++) {
230             node->add_child_nocopy((*it)->get_state());
231     }
232
233     return *node;
234 }