0a09ffd1e784edfd92cbd8df6348a9535e10af79
[ardour.git] / libs / pbd3 / 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 (const UndoAction& 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<UndoAction>::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<UndoAction>::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 void
86 UndoHistory::add (UndoTransaction ut)
87 {
88         UndoList.push_back (ut);
89 }
90
91 void
92 UndoHistory::undo (unsigned int n)
93 {
94         while (n--) {
95                 if (UndoList.size() == 0) {
96                         return;
97                 }
98                 UndoTransaction ut = UndoList.back ();
99                 UndoList.pop_back ();
100                 ut.undo ();
101                 RedoList.push_back (ut);
102         }
103 }
104
105 void
106 UndoHistory::redo (unsigned int n)
107 {
108         while (n--) {
109                 if (RedoList.size() == 0) {
110                         return;
111                 }
112                 UndoTransaction ut = RedoList.back ();
113                 RedoList.pop_back ();
114                 ut.redo ();
115                 UndoList.push_back (trans);
116         }
117 }
118
119 void
120 UndoHistory::clear_redo ()
121 {
122         RedoList.clear ();
123 }
124
125 void
126 UndoHistory::clear_undo ()
127 {
128         UndoList.clear ();
129 }
130
131 void
132 UndoHistory::clear ()
133 {
134         RedoList.clear ();
135         UndoList.clear ();
136 }