Better MidiModel command framework, ready to go for all your canvas editing needs.
[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 */
19
20 #ifndef __lib_pbd_undo_h__
21 #define __lib_pbd_undo_h__
22
23 #include <string>
24 #include <list>
25 #include <map>
26 #include <sigc++/slot.h>
27 #include <sigc++/bind.h>
28 #include <sys/time.h>
29 #include <pbd/command.h>
30
31 typedef sigc::slot<void> UndoAction;
32
33 class UndoTransaction : public Command
34 {
35   public:
36         UndoTransaction ();
37         UndoTransaction (const UndoTransaction&);
38         UndoTransaction& operator= (const UndoTransaction&);
39         ~UndoTransaction ();
40
41         void clear ();
42         bool empty() const;
43         bool clearing () const { return _clearing; }
44
45         void add_command (Command* const);
46         void remove_command (Command* const);
47
48         void operator() ();
49         void undo();
50         void redo();
51
52         XMLNode &get_state();
53
54         void set_timestamp (struct timeval &t) {
55                 _timestamp = t;
56         }
57
58         const struct timeval& timestamp() const {
59                 return _timestamp;
60         }
61
62   private:
63         std::list<Command*>    actions;
64         struct timeval        _timestamp;
65         bool                  _clearing;
66
67         friend void command_death (UndoTransaction*, Command *);
68 };
69
70 class UndoHistory : public sigc::trackable
71 {
72   public:
73         UndoHistory();
74         ~UndoHistory() {}
75         
76         void add (UndoTransaction* ut);
77         void undo (unsigned int n);
78         void redo (unsigned int n);
79         
80         unsigned long undo_depth() const { return UndoList.size(); }
81         unsigned long redo_depth() const { return RedoList.size(); }
82         
83         std::string next_undo() const { return (UndoList.empty() ? std::string("") : UndoList.back()->name()); }
84         std::string next_redo() const { return (RedoList.empty() ? std::string("") : RedoList.back()->name()); }
85
86         void clear ();
87         void clear_undo ();
88         void clear_redo ();
89
90         XMLNode &get_state(uint32_t depth = 0);
91         void save_state();
92
93         sigc::signal<void> Changed;
94
95   private:
96         bool _clearing;
97         std::list<UndoTransaction*> UndoList;
98         std::list<UndoTransaction*> RedoList;
99
100         void remove (UndoTransaction*);
101 };
102
103
104 #endif /* __lib_pbd_undo_h__ */