aa27f95789a6e378b77828b25273781561efb602
[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
25 using namespace std;
26 using namespace sigc;
27
28 UndoTransaction::UndoTransaction ()
29 {
30 }
31
32 UndoTransaction::UndoTransaction (const UndoTransaction& rhs)
33 {
34         _name = rhs._name;
35         clear ();
36         actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
37 }
38
39 UndoTransaction& 
40 UndoTransaction::operator= (const UndoTransaction& rhs)
41 {
42         if (this == &rhs) return *this;
43         _name = rhs._name;
44         clear ();
45         actions.insert(actions.end(),rhs.actions.begin(),rhs.actions.end());
46         return *this;
47 }
48
49 void
50 UndoTransaction::add_command (Command *const action)
51 {
52         actions.push_back (action);
53 }
54
55 void
56 UndoTransaction::clear ()
57 {
58         actions.clear ();
59 }
60
61 void
62 UndoTransaction::operator() ()
63 {
64         for (list<Command*>::iterator i = actions.begin(); i != actions.end(); ++i) {
65                 (*(*i))();
66         }
67 }
68
69 void
70 UndoTransaction::undo ()
71 {
72         cerr << "Undo " << _name << endl;
73         for (list<Command*>::reverse_iterator i = actions.rbegin(); i != actions.rend(); ++i) {
74                 (*i)->undo();
75         }
76 }
77
78 void
79 UndoTransaction::redo ()
80 {
81         cerr << "Redo " << _name << endl;
82         (*this)();
83 }
84
85 XMLNode &UndoTransaction::serialize()
86 {
87     XMLNode *node = new XMLNode ("UndoTransaction");
88     // TODO
89     return *node;
90 }
91
92 void
93 UndoHistory::add (UndoTransaction ut)
94 {
95         UndoList.push_back (ut);
96 }
97
98 void
99 UndoHistory::undo (unsigned int n)
100 {
101         while (n--) {
102                 if (UndoList.size() == 0) {
103                         return;
104                 }
105                 UndoTransaction ut = UndoList.back ();
106                 UndoList.pop_back ();
107                 ut.undo ();
108                 RedoList.push_back (ut);
109         }
110 }
111
112 void
113 UndoHistory::redo (unsigned int n)
114 {
115         while (n--) {
116                 if (RedoList.size() == 0) {
117                         return;
118                 }
119                 UndoTransaction ut = RedoList.back ();
120                 RedoList.pop_back ();
121                 ut.redo ();
122                 UndoList.push_back (ut);
123         }
124 }
125
126 void
127 UndoHistory::clear_redo ()
128 {
129         RedoList.clear ();
130 }
131
132 void
133 UndoHistory::clear_undo ()
134 {
135         UndoList.clear ();
136 }
137
138 void
139 UndoHistory::clear ()
140 {
141         RedoList.clear ();
142         UndoList.clear ();
143 }