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