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