switch to using boost::signals2 instead of sigc++, at least for libardour. not finish...
[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 <string>
22 #include <sstream>
23 #include <time.h>
24
25 #include "pbd/undo.h"
26 #include "pbd/xml++.h"
27
28 #include <sigc++/bind.h>
29
30 using namespace std;
31 using namespace sigc;
32
33 UndoTransaction::UndoTransaction ()
34         : _clearing(false)
35 {
36         gettimeofday (&_timestamp, 0);
37 }
38
39 UndoTransaction::UndoTransaction (const UndoTransaction& rhs)
40         : Command(rhs._name)
41         , _clearing(false)
42 {
43         clear ();
44         actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
45 }
46
47 UndoTransaction::~UndoTransaction ()
48 {
49         drop_references ();
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 of command (e.g. caused by death of object to
81            which it refers. command_death() is a normal static function
82            so there is no need to manage this connection.
83          */
84
85         scoped_connect (action->GoingAway, boost::bind (&command_death, this, action));
86         actions.push_back (action);
87 }
88
89 void
90 UndoTransaction::remove_command (Command* const action)
91 {
92         actions.remove (action);
93 }
94
95 bool
96 UndoTransaction::empty () const
97 {
98         return actions.empty();
99 }
100
101 void
102 UndoTransaction::clear ()
103 {
104         _clearing = true;
105         for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
106                 delete *i;
107         }
108         actions.clear ();
109         _clearing = false;
110 }
111
112 void
113 UndoTransaction::operator() ()
114 {
115         for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
116                 (*(*i))();
117         }
118 }
119
120 void
121 UndoTransaction::undo ()
122 {
123         for (list<Command*>::reverse_iterator i = actions.rbegin(); i != actions.rend(); ++i) {
124                 (*i)->undo();
125         }
126 }
127
128 void
129 UndoTransaction::redo ()
130 {
131         (*this)();
132 }
133
134 XMLNode &UndoTransaction::get_state()
135 {
136     XMLNode *node = new XMLNode ("UndoTransaction");
137     stringstream ss;
138     ss << _timestamp.tv_sec;
139     node->add_property("tv_sec", ss.str());
140     ss.str("");
141     ss << _timestamp.tv_usec;
142     node->add_property("tv_usec", ss.str());
143     node->add_property("name", _name);
144
145     list<Command*>::iterator it;
146     for (it=actions.begin(); it!=actions.end(); it++)
147         node->add_child_nocopy((*it)->get_state());
148
149     return *node;
150 }
151
152 UndoHistory::UndoHistory ()
153 {
154         _clearing = false;
155         _depth = 0;
156 }
157
158 void
159 UndoHistory::set_depth (uint32_t d)
160 {
161         UndoTransaction* ut;
162         uint32_t current_depth = UndoList.size();
163
164         _depth = d;
165
166         if (d > current_depth) {
167                 /* not even transactions to meet request */
168                 return;
169         }
170
171         if (_depth > 0) {
172
173                 uint32_t cnt = current_depth - d;
174
175                 while (cnt--) {
176                         ut = UndoList.front();
177                         UndoList.pop_front ();
178                         delete ut;
179                 }
180         }
181 }
182
183 void
184 UndoHistory::add (UndoTransaction* const ut)
185 {
186         uint32_t current_depth = UndoList.size();
187
188         scoped_connect (ut->GoingAway, boost::bind (&UndoHistory::remove, this, ut));
189
190         /* if the current undo history is larger than or equal to the currently
191            requested depth, then pop off at least 1 element to make space
192            at the back for new one.
193         */
194
195         if ((_depth > 0) && current_depth && (current_depth >= _depth)) {
196
197                 uint32_t cnt = 1 + (current_depth - _depth);
198
199                 while (cnt--) {
200                         UndoTransaction* ut;
201                         ut = UndoList.front ();
202                         UndoList.pop_front ();
203                         delete ut;
204                 }
205         }
206
207         UndoList.push_back (ut);
208
209         /* we are now owners of the transaction and must delete it when finished with it */
210
211         Changed (); /* EMIT SIGNAL */
212 }
213
214 void
215 UndoHistory::remove (UndoTransaction* const ut)
216 {
217         if (_clearing) {
218                 return;
219         }
220
221         UndoList.remove (ut);
222         RedoList.remove (ut);
223
224         Changed (); /* EMIT SIGNAL */
225 }
226
227 /** Undo some transactions.
228  * @param n Number of transactions to undo.
229  */
230 void
231 UndoHistory::undo (unsigned int n)
232 {
233         while (n--) {
234                 if (UndoList.size() == 0) {
235                         return;
236                 }
237                 UndoTransaction* ut = UndoList.back ();
238                 UndoList.pop_back ();
239                 ut->undo ();
240                 RedoList.push_back (ut);
241         }
242
243         Changed (); /* EMIT SIGNAL */
244 }
245
246 void
247 UndoHistory::redo (unsigned int n)
248 {
249         while (n--) {
250                 if (RedoList.size() == 0) {
251                         return;
252                 }
253                 UndoTransaction* ut = RedoList.back ();
254                 RedoList.pop_back ();
255                 ut->redo ();
256                 UndoList.push_back (ut);
257         }
258
259         Changed (); /* EMIT SIGNAL */
260 }
261
262 void
263 UndoHistory::clear_redo ()
264 {
265         _clearing = true;
266         RedoList.clear ();
267         _clearing = false;
268
269         Changed (); /* EMIT SIGNAL */
270
271 }
272
273 void
274 UndoHistory::clear_undo ()
275 {
276         _clearing = true;
277         UndoList.clear ();
278         _clearing = false;
279
280         Changed (); /* EMIT SIGNAL */
281 }
282
283 void
284 UndoHistory::clear ()
285 {
286         clear_undo ();
287         clear_redo ();
288
289         Changed (); /* EMIT SIGNAL */
290 }
291
292 XMLNode& 
293 UndoHistory::get_state (int32_t depth)
294 {
295     XMLNode *node = new XMLNode ("UndoHistory");
296
297     if (depth == 0) {
298
299             return (*node);
300
301     } else if (depth < 0) {
302
303             /* everything */
304
305             for (list<UndoTransaction*>::iterator it = UndoList.begin(); it != UndoList.end(); ++it) {
306                     node->add_child_nocopy((*it)->get_state());
307             }
308
309     } else {
310
311             /* just the last "depth" transactions */
312
313             list<UndoTransaction*> in_order;
314
315             for (list<UndoTransaction*>::reverse_iterator it = UndoList.rbegin(); it != UndoList.rend() && depth; ++it, depth--) {
316                     in_order.push_front (*it);
317             }
318
319             for (list<UndoTransaction*>::iterator it = in_order.begin(); it != in_order.end(); it++) {
320                     node->add_child_nocopy((*it)->get_state());
321             }
322     }
323
324     return *node;
325 }
326
327