Merged with trunk R992.
[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 typedef sigc::slot<void> UndoAction;
33
34 class UndoTransaction : public Command
35 {
36   public:
37         UndoTransaction ();
38         UndoTransaction (const UndoTransaction&);
39         UndoTransaction& operator= (const UndoTransaction&);
40         ~UndoTransaction ();
41
42         void clear ();
43         bool empty() const;
44         bool clearing () const { return _clearing; }
45
46         void add_command (Command* const);
47         void remove_command (Command* const);
48
49         void operator() ();
50         void undo();
51         void redo();
52
53         XMLNode &get_state();
54         
55         void set_name (const std::string& str) {
56                 _name = str;
57         }
58         const std::string& name() const { return _name; }
59
60         void set_timestamp (struct timeval &t) {
61                 _timestamp = t;
62         }
63
64         const struct timeval& timestamp() const {
65                 return _timestamp;
66         }
67
68   private:
69         std::list<Command*>    actions;
70         struct timeval        _timestamp;
71         std::string           _name;
72         bool                  _clearing;
73 };
74
75 class UndoHistory
76 {
77   public:
78         UndoHistory();
79         ~UndoHistory() {}
80         
81         void add (UndoTransaction* ut);
82         void undo (unsigned int n);
83         void redo (unsigned int n);
84         
85         unsigned long undo_depth() const { return UndoList.size(); }
86         unsigned long redo_depth() const { return RedoList.size(); }
87         
88         std::string next_undo() const { return (UndoList.empty() ? std::string("") : UndoList.back()->name()); }
89         std::string next_redo() const { return (RedoList.empty() ? std::string("") : RedoList.back()->name()); }
90
91         void clear ();
92         void clear_undo ();
93         void clear_redo ();
94
95         XMLNode &get_state();
96         void save_state();
97
98   private:
99         bool _clearing;
100         std::list<UndoTransaction*> UndoList;
101         std::list<UndoTransaction*> RedoList;
102
103         void remove (UndoTransaction*);
104 };
105
106
107 #endif /* __lib_pbd_undo_h__ */