]> git.lyx.org Git - lyx.git/blob - src/undo_funcs.C
dd064886687e56994423e06da8290ace1d35d978
[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                                         tmppar->clearContents();
134                                         tmppar2 = tmppar2->next();
135                                 }
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                                 bv->text->ownerParagraph(tmppar3->id(),
145                                                          tmppar3);
146
147                         tmppar3->previous(before);
148                 } else {
149                         // Do we really enter here ??? (Jug)
150                         if (!before)
151                                 bv->text->ownerParagraph(behind);
152                 }
153                 if (tmppar4) {
154                         tmppar4->next(behind);
155                         if (behind)
156                                 behind->previous(tmppar4);
157                 }
158     
159     
160                 // Set the cursor for redoing
161                 if (before) {
162                         bv->text->setCursorIntern(bv, before, 0);
163                 }
164
165                 // calculate the endpar for redoing the paragraphs.
166                 if (behind) {
167                                 endpar = behind->next();
168                 } else
169                         endpar = behind;
170     
171                 tmppar = bv->buffer()->getParFromID(undo->number_of_cursor_par);
172                 UpdatableInset* it = static_cast<UpdatableInset*>(tmppar3->inInset());
173                 if (it) {
174                         it->getLyXText(bv)->redoParagraphs(bv, it->getLyXText(bv)->cursor,
175                                                            endpar);
176                         if (tmppar){
177                                 it->getLyXText(bv)->setCursorIntern(bv, tmppar, undo->cursor_pos);
178                                 it->getLyXText(bv)->updateCounters(bv, it->getLyXText(bv)->cursor.row());
179                         }
180                         LyXFont font;
181                         it->update(bv, font, false);
182 #ifdef THIS_DOES_NOT_WORK
183                         // we need this anyway as also if the undo was
184                         // inside an inset we have to redo the
185                         // paragraph breaking
186                         bv->text->redoParagraphs(bv, bv->text->cursor,
187                                                  bv->text->cursor.par());
188 #endif
189                 } else {
190                         bv->text->redoParagraphs(bv, bv->text->cursor, endpar);
191                         if (tmppar) {
192                                 bv->text->setCursorIntern(bv, tmppar, undo->cursor_pos);
193                                 bv->text->updateCounters(bv, bv->text->cursor.row());
194                         }
195                 }
196                 result = true;
197                 delete undo;
198         }
199         finishUndo();
200         bv->text->status(bv, LyXText::NEED_MORE_REFRESH);
201         return result;
202 }
203
204
205 void finishUndo()
206 {
207         // makes sure the next operation will be stored
208         undo_finished = true;
209 }
210
211
212 void freezeUndo()
213 {
214         // this is dangerous and for internal use only
215         undo_frozen = true;
216 }
217
218
219 void unFreezeUndo()
220 {
221         // this is dangerous and for internal use only
222         undo_frozen = false;
223 }
224
225
226 void setUndo(BufferView * bv, Undo::undo_kind kind,
227              Paragraph const * first, Paragraph const * behind)
228 {
229         if (!undo_frozen)
230                 bv->buffer()->undostack.push(createUndo(bv, kind, first, behind));
231         bv->buffer()->redostack.clear();
232 }
233
234
235 void setRedo(BufferView * bv, Undo::undo_kind kind,
236              Paragraph const * first, Paragraph const * behind)
237 {
238         bv->buffer()->redostack.push(createUndo(bv, kind, first, behind));
239 }
240
241
242 Undo * createUndo(BufferView * bv, Undo::undo_kind kind,
243                   Paragraph const * first, Paragraph const * behind)
244 {
245         lyx::Assert(first);
246
247         int before_number = -1;
248         int behind_number = -1;
249         int inset_id = -1;
250
251         if (first->previous())
252                 before_number = first->previous()->id();
253         if (behind)
254                 behind_number = behind->id();
255         if (first->inInset())
256                 inset_id = first->inInset()->id();
257
258         // Undo::EDIT  and Undo::FINISH are
259         // always finished. (no overlapping there)
260         // overlapping only with insert and delete inside one paragraph: 
261         // Nobody wants all removed  character
262         // appear one by one when undoing. 
263         // EDIT is special since only layout information, not the
264         // contents of a paragaph are stored.
265         if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)){
266                 // check wether storing is needed
267                 if (!bv->buffer()->undostack.empty() && 
268                     bv->buffer()->undostack.top()->kind == kind &&
269                     bv->buffer()->undostack.top()->number_of_before_par == before_number &&
270                     bv->buffer()->undostack.top()->number_of_behind_par == behind_number) {
271                         // no undo needed
272                         return 0;
273                 }
274         }
275         // create a new Undo
276         Paragraph * undopar;
277
278         Paragraph * start = const_cast<Paragraph *>(first);
279         Paragraph * end = 0;
280
281         if (behind)
282                 end = const_cast<Paragraph*>(behind->previous());
283         else {
284                 end = start;
285                 while (end->next())
286                         end = end->next();
287         }
288         if (start && end && (start != end->next()) &&
289             ((before_number != behind_number) ||
290                  ((before_number < 0) && (behind_number < 0)))) {
291                 Paragraph * tmppar = start;
292                 Paragraph * tmppar2 = new Paragraph(*tmppar, true);
293                 tmppar2->id(tmppar->id());
294                 
295                 // a memory optimization: Just store the layout information
296                 // when only edit
297                 if (kind == Undo::EDIT) {
298                         tmppar2->clearContents();
299                 }
300
301                 undopar = tmppar2;
302   
303                 while (tmppar != end && tmppar->next()) {
304                         tmppar = tmppar->next();
305                         tmppar2->next(new Paragraph(*tmppar, false));
306                         tmppar2->next()->id(tmppar->id());
307                         // a memory optimization: Just store the layout
308                         // information when only edit
309                         if (kind == Undo::EDIT) {
310                                 tmppar2->clearContents();
311                         }
312                         tmppar2->next()->previous(tmppar2);
313                         tmppar2 = tmppar2->next();
314                 }
315                 tmppar2->next(0);
316         } else
317                 undopar = 0; // nothing to replace (undo of delete maybe)
318
319         int cursor_par = undoCursor(bv).par()->id();
320         int cursor_pos =  undoCursor(bv).pos();
321         
322         Undo * undo = new Undo(kind, inset_id,
323                                before_number, behind_number,  
324                                cursor_par, cursor_pos, undopar);
325   
326         undo_finished = false;
327         return undo;
328 }
329
330
331 void setCursorParUndo(BufferView * bv)
332 {
333         setUndo(bv, Undo::FINISH,
334                 bv->text->cursor.par(),
335                         bv->text->cursor.par()->next());
336 }
337
338
339 Paragraph * firstUndoParagraph(BufferView * bv, int inset_id)
340 {
341         Inset * inset = bv->buffer()->getInsetFromID(inset_id);
342         if (inset) {
343                 Paragraph * result = inset->firstParagraph();
344                 if (result)
345                         return result;
346         }
347         return bv->text->ownerParagraph();
348 }
349
350
351 LyXCursor const & undoCursor(BufferView * bv)
352 {
353         if (bv->theLockingInset())
354                 return bv->theLockingInset()->cursor(bv);
355         return bv->text->cursor;
356 }