Scribus
Open source desktop publishing at your fingertips
undostack.h
1 /*
2 For general Scribus (>=1.3.2) copyright and licensing information please refer
3 to the COPYING file provided with the program. Following this notice may exist
4 a copyright and/or license notice that predates the release of Scribus 1.3.2
5 for which a new license (GPL+exception) is in place.
6 */
7 /***************************************************************************
8  * Copyright (C) 2005 by Riku Leino *
9  * riku@scribus.info *
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  * This program is distributed in the hope that it will be useful, *
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19  * GNU General Public License for more details. *
20  * *
21  * You should have received a copy of the GNU General Public License *
22  * along with this program; if not, write to the *
23  * Free Software Foundation, Inc., *
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
25  ***************************************************************************/
26 
27 #ifndef UNDOSTACK_H
28 #define UNDOSTACK_H
29 
30 #include <vector>
31 
32 class UndoState;
33 class TransactionState;
34 
35 typedef std::vector<UndoState*> StateList;
36 
37 class SCRIBUS_API UndoStack
38 {
39 public:
40  explicit UndoStack(int maxSize = 100);
41  ~UndoStack();
42 
43  /* Used to push a new action to the stack. UndoState in the parameter will then
44  * become the first undo action in the stack and all the redo actions will be
45  * cleared. If maximum size of the stack is hit and an action needs to be removed
46  * this function returns true. */
47  bool action(UndoState *state);
48 
49  /* undo number of steps actions (these will then become redo actions) */
50  bool undo(uint steps, int objectId);
51  /* redo number of steps actions (these will then become undo actions) */
52  bool redo(uint steps, int objectId);
53 
54  /* number of actions stored in the stack mostly for testing */
55  uint size() const;
56  uint undoItems() const;
57  uint redoItems() const;
58 
59  /* maximum number of actions stored in the stack */
60  uint maxSize() const;
61  /* Change the maximum number of actions stored in the stack. If there are
62  * both undo and redo actions available and stack size is decreased redo
63  * actions will be popped out first starting from the oldest one.
64  * 0 is used to mark infinite stack size. If one wants to disable undo/redo
65  * function setUndoEnabled(bool) from UndoManager should be used */
66  void setMaxSize(uint maxSize);
67 
68  void clear();
69 
70  UndoState* getNextUndo(int objectId);
71  UndoState* getNextRedo(int objectId);
72 
73 private:
74  /* When an action happens it is pushed to the undoActions_ and the redoActions_
75  * is cleared. When undo is requested action is popped from undoActions_ and
76  * pushed to the redoActions_ (and vice versa). */
77  StateList undoActions_; /* stack would probably be enough for this but vector */
78  StateList redoActions_; /* will give more options in future */
79 
80  /* maximum amount of actions stored, 0 for no limit */
81  uint maxSize_;
82 
83  /* returns true if an action was popped from the stack */
84  /* assures that we only hold the maxSize_ number of UndoStates */
85  bool checkSize();
86 
87  friend class UndoManager; // UndoManager needs access to undoActions_ and redoActions_
88  // for updating the attached UndoGui widgets
89 
90 };
91 
92 #endif // UNDOSTACK_H
Definition: undostack.h:37
UndoState describes an undoable state (action).
Definition: undostate.h:59
TransactionState provides a container where multiple UndoStates can be stored.
Definition: undostate.h:326
UndoManager handles the undo stack.
Definition: undomanager.h:81