add begin/end undo/redo signals so that playlist can freeze/thaw itself around potent...
[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
30 #include "pbd/signals.h"
31 #include "pbd/command.h"
32
33 typedef sigc::slot<void> UndoAction;
34
35 class UndoTransaction : public Command, public PBD::ScopedConnectionList
36 {
37   public:
38         UndoTransaction ();
39         UndoTransaction (const UndoTransaction&);
40         UndoTransaction& operator= (const 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_timestamp (struct timeval &t) {
56                 _timestamp = t;
57         }
58
59         const struct timeval& timestamp() const {
60                 return _timestamp;
61         }
62
63   private:
64         std::list<Command*>    actions;
65         struct timeval        _timestamp;
66         bool                  _clearing;
67
68         friend void command_death (UndoTransaction*, Command *);
69         
70         friend class UndoHistory;
71
72         ~UndoTransaction ();
73         void about_to_explicitly_delete ();
74 };
75
76 class UndoHistory : public PBD::ScopedConnectionList
77 {
78   public:
79         UndoHistory();
80         ~UndoHistory() {}
81         
82         void add (UndoTransaction* ut);
83         void undo (unsigned int n);
84         void redo (unsigned int n);
85         
86         unsigned long undo_depth() const { return UndoList.size(); }
87         unsigned long redo_depth() const { return RedoList.size(); }
88         
89         std::string next_undo() const { return (UndoList.empty() ? std::string() : UndoList.back()->name()); }
90         std::string next_redo() const { return (RedoList.empty() ? std::string() : RedoList.back()->name()); }
91
92         void clear ();
93         void clear_undo ();
94         void clear_redo ();
95
96         /* returns all or part of the history.
97            If depth==0 it returns just the top
98            node. If depth<0, it returns everything.
99            If depth>0, it returns state for that
100            many elements of the history, or 
101            the full history, whichever is smaller.
102         */
103
104         XMLNode &get_state(int32_t depth = 0);
105         void save_state();
106
107         void set_depth (uint32_t);
108
109         PBD::Signal0<void> Changed;
110         PBD::Signal0<void> BeginUndoRedo;
111         PBD::Signal0<void> EndUndoRedo;
112         
113   private:
114         bool _clearing;
115         uint32_t _depth;
116         std::list<UndoTransaction*> UndoList;
117         std::list<UndoTransaction*> RedoList;
118
119         void remove (UndoTransaction*);
120 };
121
122
123 #endif /* __lib_pbd_undo_h__ */