Merged with trunk R776
[ardour.git] / libs / pbd / pbd / undo.h
1 /* 
2    Copyright (C) 2002 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 #ifndef __lib_pbd_undo_h__
22 #define __lib_pbd_undo_h__
23
24 #include <string>
25 #include <list>
26 #include <map>
27 #include <sigc++/slot.h>
28 #include <sigc++/bind.h>
29 #include <sys/time.h>
30 #include <pbd/command.h>
31
32 using std::string;
33 using std::list;
34
35 typedef sigc::slot<void> UndoAction;
36
37 class UndoTransaction : public Command
38 {
39   public:
40         UndoTransaction ();
41         UndoTransaction (const UndoTransaction&);
42         UndoTransaction& operator= (const UndoTransaction&);
43
44         void clear ();
45
46         void add_command (Command *const);
47
48         void operator() ();
49         void undo();
50         void redo();
51
52         XMLNode &get_state();
53         
54         void set_name (const string& str) {
55                 _name = str;
56         }
57         const string& name() const { return _name; }
58
59         void set_timestamp (struct timeval &t) {
60                 _timestamp = t;
61         }
62
63         const struct timeval& timestamp() const {
64                 return _timestamp;
65         }
66
67   private:
68         list<Command*>   actions;
69         struct timeval   _timestamp;
70         string           _name;
71 };
72
73 class UndoHistory
74 {
75   public:
76         UndoHistory() {}
77         ~UndoHistory() {}
78         
79         void add (UndoTransaction ut);
80         void undo (unsigned int n);
81         void redo (unsigned int n);
82         
83         unsigned long undo_depth() const { return UndoList.size(); }
84         unsigned long redo_depth() const { return RedoList.size(); }
85         
86         string next_undo() const { return (UndoList.empty() ? string("") : UndoList.back().name()); }
87         string next_redo() const { return (RedoList.empty() ? string("") : RedoList.back().name()); }
88
89         void clear ();
90         void clear_undo ();
91         void clear_redo ();
92
93         XMLNode &get_state();
94         void save_state();
95   private:
96         list<UndoTransaction> UndoList;
97         list<UndoTransaction> RedoList;
98 };
99
100
101 #endif /* __lib_pbd_undo_h__ */