]> git.lyx.org Git - lyx.git/blob - src/undo_funcs.C
e010d5773dcdede4624ca91a57a538b0d82a4f9e
[lyx.git] / src / undo_funcs.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *        
6  *           Copyright 1995-2001 The LyX Team.
7  *
8  * ====================================================== */
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "undo_funcs.h"
17 #include "lyxtext.h"
18 #include "BufferView.h"
19 #include "buffer.h"
20 #include "insets/inset.h"
21 #include "debug.h"
22 #include "support/LAssert.h"
23
24 /// the flag used by FinishUndo();
25 bool undo_finished;
26 /// a flag
27 bool undo_frozen;
28
29 bool textUndo(BufferView * bv)
30 {
31         // returns false if no undo possible
32         Undo * undo = bv->buffer()->undostack.pop();
33         if (undo) {
34                 finishUndo();
35                 if (!undo_frozen) {
36                         Paragraph * first = bv->buffer()->getParFromID(undo->number_of_before_par);
37                         if (!first)
38                                 first = firstUndoParagraph(bv, undo->number_of_inset_id);
39                         if (first) {
40                                 bv->buffer()->redostack.push(
41                                         createUndo(bv, undo->kind, first,
42                                                            bv->buffer()->getParFromID(undo->number_of_behind_par)));
43                         }
44                 }
45         }
46         return textHandleUndo(bv, undo);
47 }
48
49
50 bool textRedo(BufferView * bv)
51 {
52         // returns false if no redo possible
53         Undo * undo = bv->buffer()->redostack.pop();
54         if (undo) {
55                 finishUndo();
56                 if (!undo_frozen) {
57                         Paragraph * first = bv->buffer()->getParFromID(undo->number_of_before_par);
58                         if (!first)
59                                 first = firstUndoParagraph(bv, undo->number_of_inset_id);
60                         if (first) {
61                                 bv->buffer()->undostack.push(
62                                         createUndo(bv, undo->kind, first,
63                                                    bv->buffer()->getParFromID(undo->number_of_behind_par)));
64                         }
65                 }
66         }
67         return textHandleUndo(bv, undo);
68 }
69
70
71 bool textHandleUndo(BufferView * bv, Undo * undo)
72 {
73         // returns false if no undo possible
74         bool result = false;
75         if (undo) {
76                 Paragraph * before =
77                         bv->buffer()->getParFromID(undo->number_of_before_par); 
78                 Paragraph * behind =
79                         bv->buffer()->getParFromID(undo->number_of_behind_par); 
80                 Paragraph * tmppar;
81                 Paragraph * tmppar2;
82                 Paragraph * endpar;
83                 Paragraph * tmppar5;
84
85                 // if there's no before take the beginning
86                 // of the document for redoing
87                 if (!before) {
88                         LyXText * t = bv->text;
89                         int num = undo->number_of_inset_id;
90                         if (undo->number_of_inset_id >= 0) {
91                                 Inset * in = bv->buffer()->getInsetFromID(num);
92                                 if (in) {
93                                         t = in->getLyXText(bv);
94                                 } else {
95                                         num = -1;
96                                 }
97                         }
98                         t->setCursorIntern(bv, firstUndoParagraph(bv, num), 0);
99                 }
100
101                 // replace the paragraphs with the undo informations
102
103                 Paragraph * tmppar3 = undo->par;
104                 undo->par = 0; // otherwise the undo destructor would delete the paragraph
105                 Paragraph * tmppar4 = tmppar3;
106
107                 if (tmppar4) {
108                         while (tmppar4->next())
109                                 tmppar4 = tmppar4->next();
110                 } // get last undo par
111     
112                 // now remove the old text if there is any
113                 if (before != behind || (!behind && !before)) {
114                         if (before)
115                                 tmppar5 = before->next();
116                         else
117                                 tmppar5 = firstUndoParagraph(bv, undo->number_of_inset_id);
118                         tmppar2 = tmppar3;
119                         while (tmppar5 && tmppar5 != behind) {
120                                 tmppar = tmppar5;
121                                 tmppar5 = tmppar5->next();
122                                 // a memory optimization for edit: Only layout information
123                                 // is stored in the undo. So restore the text informations.
124                                 if (undo->kind == Undo::EDIT) {
125                                         tmppar2->setContentsFromPar(tmppar);
126                                         tmppar->clearContents();
127                                         tmppar2 = tmppar2->next();
128                                 }
129                         }
130                 }
131     
132                 // put the new stuff in the list if there is one
133                 if (tmppar3){
134                         if (before)
135                                 before->next(tmppar3);
136                         else
137                                 bv->text->ownerParagraph(tmppar3->id(), tmppar3);
138                         tmppar3->previous(before);
139                 } else {
140                         // Do we really enter here ??? (Jug)
141                         if (!before)
142                                 bv->text->ownerParagraph(behind);
143                 }
144                 if (tmppar4) {
145                         tmppar4->next(behind);
146                         if (behind)
147                                 behind->previous(tmppar4);
148                 }
149     
150     
151                 // Set the cursor for redoing
152                 if (before) {
153                         bv->text->setCursorIntern(bv, before, 0);
154                 }
155
156                 // calculate the endpar for redoing the paragraphs.
157                 if (behind) {
158                                 endpar = behind->next();
159                 } else
160                         endpar = behind;
161     
162                 tmppar = bv->buffer()->getParFromID(undo->number_of_cursor_par);
163                 UpdatableInset* it = static_cast<UpdatableInset*>(tmppar3->inInset());
164                 if (it) {
165                         it->getLyXText(bv)->redoParagraphs(bv, it->getLyXText(bv)->cursor,
166                                                            endpar);
167                         if (tmppar){
168                                 it->getLyXText(bv)->setCursorIntern(bv, tmppar, undo->cursor_pos);
169                                 it->getLyXText(bv)->updateCounters(bv, it->getLyXText(bv)->cursor.row());
170                         }
171 #ifdef THIS_DOES_NOT_WORK
172                         // we need this anyway as also if the undo was inside an inset
173                         // we have to redo the paragraph breaking
174                         bv->text->redoParagraphs(bv, bv->text->cursor,
175                                                                          bv->text->cursor.par());
176 #endif
177                 } else {
178                         bv->text->redoParagraphs(bv, bv->text->cursor, endpar);
179                         if (tmppar) {
180                                 bv->text->setCursorIntern(bv, tmppar, undo->cursor_pos);
181                                 bv->text->updateCounters(bv, bv->text->cursor.row());
182                         }
183                 }
184                 result = true;
185                 delete undo;
186         }
187         finishUndo();
188         bv->text->status(bv, LyXText::NEED_MORE_REFRESH);
189         return result;
190 }
191
192
193 void finishUndo()
194 {
195         // makes sure the next operation will be stored
196         undo_finished = true;
197 }
198
199
200 void freezeUndo()
201 {
202         // this is dangerous and for internal use only
203         undo_frozen = true;
204 }
205
206
207 void unFreezeUndo()
208 {
209         // this is dangerous and for internal use only
210         undo_frozen = false;
211 }
212
213
214 void setUndo(BufferView * bv, Undo::undo_kind kind,
215              Paragraph const * first, Paragraph const * behind)
216 {
217         if (!undo_frozen)
218                 bv->buffer()->undostack.push(createUndo(bv, kind, first, behind));
219         bv->buffer()->redostack.clear();
220 }
221
222
223 void setRedo(BufferView * bv, Undo::undo_kind kind,
224              Paragraph const * first, Paragraph const * behind)
225 {
226         bv->buffer()->redostack.push(createUndo(bv, kind, first, behind));
227 }
228
229 using lyx::pos_type;
230
231 Undo * createUndo(BufferView * bv, Undo::undo_kind kind,
232                   Paragraph const * first, Paragraph const * behind)
233 {
234         lyx::Assert(first);
235
236         int before_number = -1;
237         int behind_number = -1;
238         int inset_id = -1;
239
240         if (first->previous())
241                 before_number = first->previous()->id();
242         if (behind)
243                 behind_number = behind->id();
244         if (first->inInset())
245                 inset_id = first->inInset()->id();
246
247         // Undo::EDIT  and Undo::FINISH are
248         // always finished. (no overlapping there)
249         // overlapping only with insert and delete inside one paragraph: 
250         // Nobody wants all removed  character
251         // appear one by one when undoing. 
252         // EDIT is special since only layout information, not the
253         // contents of a paragaph are stored.
254         if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)){
255                 // check wether storing is needed
256                 if (!bv->buffer()->undostack.empty() && 
257                     bv->buffer()->undostack.top()->kind == kind &&
258                     bv->buffer()->undostack.top()->number_of_before_par ==  before_number &&
259                     bv->buffer()->undostack.top()->number_of_behind_par ==  behind_number ){
260                         // no undo needed
261                         return 0;
262                 }
263         }
264         // create a new Undo
265         Paragraph * undopar;
266
267         Paragraph * start = const_cast<Paragraph *>(first);
268         Paragraph * end = 0;
269
270         if (behind)
271                 end = const_cast<Paragraph*>(behind->previous());
272         else {
273                 end = start;
274                 while (end->next())
275                         end = end->next();
276         }
277         if (start && end && (start != end->next()) &&
278             ((before_number != behind_number) ||
279                  ((before_number < 0) && (behind_number < 0)))) {
280                 Paragraph * tmppar = start;
281                 Paragraph * tmppar2 = new Paragraph(*tmppar, true);
282                 tmppar2->id(tmppar->id());
283                 
284                 // a memory optimization: Just store the layout information
285                 // when only edit
286                 if (kind == Undo::EDIT) {
287                         tmppar2->clearContents();
288                 }
289
290                 undopar = tmppar2;
291   
292                 while (tmppar != end && tmppar->next()) {
293                         tmppar = tmppar->next();
294                         tmppar2->next(new Paragraph(*tmppar, false));
295                         tmppar2->next()->id(tmppar->id());
296                         // a memory optimization: Just store the layout
297                         // information when only edit
298                         if (kind == Undo::EDIT) {
299                                 tmppar2->clearContents();
300                         }
301                         tmppar2->next()->previous(tmppar2);
302                         tmppar2 = tmppar2->next();
303                 }
304                 tmppar2->next(0);
305         } else
306                 undopar = 0; // nothing to replace (undo of delete maybe)
307
308         int cursor_par = undoCursor(bv).par()->id();
309         int cursor_pos =  undoCursor(bv).pos();
310         
311         Undo * undo = new Undo(kind, inset_id,
312                                before_number, behind_number,  
313                                cursor_par, cursor_pos, undopar);
314   
315         undo_finished = false;
316         return undo;
317 }
318
319
320 void setCursorParUndo(BufferView * bv)
321 {
322         setUndo(bv, Undo::FINISH,
323                 bv->text->cursor.par(),
324                         bv->text->cursor.par()->next());
325 }
326
327 Paragraph * firstUndoParagraph(BufferView * bv, int inset_id)
328 {
329         Inset * inset = bv->buffer()->getInsetFromID(inset_id);
330         if (inset) {
331                 Paragraph * result = inset->firstParagraph();
332                 if (result)
333                         return result;
334         }
335         return bv->text->ownerParagraph();
336 }
337
338 LyXCursor const & undoCursor(BufferView * bv)
339 {
340         if (bv->theLockingInset())
341                 return bv->theLockingInset()->cursor(bv);
342         return bv->text->cursor;
343 }