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