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