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