]> git.lyx.org Git - lyx.git/blob - src/undo.h
fa3d0bc087bbf3bb7a14092dc1806eaaff80593c
[lyx.git] / src / undo.h
1 // -*- C++ -*-
2 /* This file is part of
3  * ======================================================
4  * 
5  *           LyX, The Document Processor
6  *       
7  *          Copyright (C) 1995 Matthias Ettrich, 1995, 1996 LyX Team
8  *
9  *======================================================*/
10 #ifndef _UNDO_H
11 #define _UNDO_H
12
13 #ifdef __GNUG__
14 #pragma interface
15 #endif
16
17 #include "definitions.h"
18 #include "lyxparagraph.h"
19
20
21 ///
22 class Undo {
23 public:
24         /// The undo kinds
25         enum undo_kind {
26                 ///
27                 INSERT,
28                 ///
29                 DELETE,
30                 ///
31                 EDIT,
32                 ///
33                 FINISH
34         };
35         ///
36         undo_kind kind;
37         ///
38         int number_of_before_par;
39         ///
40         int number_of_behind_par;
41         ///
42         int number_of_cursor_par;
43         ///
44         int cursor_pos; // valid if >= 0
45         ///
46         LyXParagraph* par;
47         ///
48         Undo(undo_kind kind_arg,
49              int number_before_arg, int number_behind_arg,
50              int cursor_par_arg, int cursor_pos_arg,
51              LyXParagraph* par_arg)
52         {
53                 kind = kind_arg;
54                 number_of_before_par = number_before_arg;
55                 number_of_behind_par = number_behind_arg;
56                 number_of_cursor_par = cursor_par_arg;
57                 cursor_pos = cursor_pos_arg;
58                 par = par_arg;
59         }
60         ///
61         ~Undo(){
62                 LyXParagraph* tmppar;
63                 while (par) {
64                         tmppar = par;
65                         par = par->next;
66                         delete tmppar;
67                 }
68         }
69 };
70
71
72 /// A limited Stack for the undo informations. Matthias 290496
73 class UndoStack{
74 private:
75         ///
76         struct StackAtom{
77                 ///
78                 StackAtom* previous;
79                 ///
80                 Undo* undo;
81         };
82         ///
83         StackAtom* current;
84         ///
85         StackAtom *tmp;
86         ///
87         int size;
88         ///
89         int limit;
90 public:
91         ///
92         UndoStack(){
93                 current = 0;
94                 // size must be initialised (thornley)
95                 size = 0;
96                 limit = 100; // the maximum number of undo steps stored. 0 means NO LIMIT. 
97                 // Limit can be changed with UndoStack::SetStackLimit(int) 
98         }
99         ///
100         Undo *Pop(){
101                 Undo* result = 0;
102                 if (current){
103                         result = current->undo;
104                         tmp = current;
105                         current = current->previous;
106                         delete tmp;
107                         size--;
108                 }
109                 else {
110                         size = 0; // for safety...
111                 }
112                 return result;
113         }
114         ///
115         Undo* Top(){
116                 if (current)
117                         return current->undo;
118                 else
119                         return 0;
120         }
121         ///
122         ~UndoStack(){
123                 Clear();
124         }
125         ///
126         void Clear(){
127                 Undo* tmp_undo = Pop();
128                 while (tmp_undo){
129                         delete tmp_undo;
130                         tmp_undo = Pop();
131                 }
132         }
133         ///
134         void SetStackLimit(int limit_arg) {
135                 limit = limit_arg;
136         }
137     
138         ///
139         void Push(Undo* undo_arg){
140                 int i;
141                 StackAtom* tmp2;
142                 if (undo_arg){
143                         tmp = new StackAtom;
144                         tmp->undo = undo_arg;
145                         tmp->previous = current;
146                         current = tmp;
147                         size++;
148                         if (limit && size > limit){
149                                 for (i=0; i<limit && tmp; i++)
150                                         tmp = tmp->previous;
151                                 while(tmp && tmp->previous){
152                                         tmp2 = tmp->previous->previous;
153                                         delete tmp->previous;
154                                         size--;
155                                         tmp->previous = tmp2;
156                                 }
157                         }
158                 }
159         }
160 };
161
162 #endif