1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995-2001 The LyX Team.
8 * ====================================================== */
13 #pragma implementation
16 #include "undo_funcs.h"
18 #include "BufferView.h"
20 #include "insets/inset.h"
22 #include "support/LAssert.h"
24 /// the flag used by FinishUndo();
29 bool textUndo(BufferView * bv)
31 // returns false if no undo possible
32 Undo * undo = bv->buffer()->undostack.pop();
36 Paragraph * first = bv->buffer()->getParFromID(undo->number_of_before_par);
38 first = firstUndoParagraph(bv, undo->number_of_inset_id);
40 bv->buffer()->redostack.push(
41 createUndo(bv, undo->kind, first,
42 bv->buffer()->getParFromID(undo->number_of_behind_par)));
46 return textHandleUndo(bv, undo);
50 bool textRedo(BufferView * bv)
52 // returns false if no redo possible
53 Undo * undo = bv->buffer()->redostack.pop();
57 Paragraph * first = bv->buffer()->getParFromID(undo->number_of_before_par);
59 first = firstUndoParagraph(bv, undo->number_of_inset_id);
61 bv->buffer()->undostack.push(
62 createUndo(bv, undo->kind, first,
63 bv->buffer()->getParFromID(undo->number_of_behind_par)));
67 return textHandleUndo(bv, undo);
71 bool textHandleUndo(BufferView * bv, Undo * undo)
73 // returns false if no undo possible
77 bv->buffer()->getParFromID(undo->number_of_before_par);
79 bv->buffer()->getParFromID(undo->number_of_behind_par);
85 // if there's no before take the beginning
86 // of the document for redoing
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);
93 t = in->getLyXText(bv);
98 t->setCursorIntern(bv, firstUndoParagraph(bv, num), 0);
101 // replace the paragraphs with the undo informations
103 Paragraph * tmppar3 = undo->par;
104 undo->par = 0; // otherwise the undo destructor would
105 // delete the paragraph
108 Paragraph * tmppar4 = tmppar3;
110 while (tmppar4->next())
111 tmppar4 = tmppar4->next();
114 // now remove the old text if there is any
115 if (before != behind || (!behind && !before)) {
117 tmppar5 = before->next();
119 tmppar5 = firstUndoParagraph(bv, undo->number_of_inset_id);
121 while (tmppar5 && tmppar5 != behind) {
123 tmppar5 = tmppar5->next();
124 // a memory optimization for edit:
125 // Only layout information
126 // is stored in the undo. So restore
127 // the text informations.
128 if (undo->kind == Undo::EDIT) {
129 tmppar2->setContentsFromPar(tmppar);
130 tmppar2 = tmppar2->next();
136 // put the new stuff in the list if there is one
139 before->next(tmppar3);
141 #warning Juergen, why is this needed?? (JMarc)
142 // since tmppar3 is not yet inserted in the document, I do not see why
143 // the getParFromID which is done by the function below makes sense.
144 // OTOH, since you wrote the method just for this instance, I guess you
145 // have something in mind
147 bv->text->ownerParagraph(tmppar3->id(),
150 // in this case, since getParFromID is not called, the program does
151 // not crash on trying to access buffer()->paragraph, which does not
152 // exist anymore if we undid the first par f the document. (JMarc)
153 bv->text->ownerParagraph(tmppar3);
156 tmppar3->previous(before);
158 // Do we really enter here ??? (Jug)
160 bv->text->ownerParagraph(behind);
163 tmppar4->next(behind);
165 behind->previous(tmppar4);
169 // Set the cursor for redoing
171 bv->text->setCursorIntern(bv, before, 0);
174 // calculate the endpar for redoing the paragraphs.
176 endpar = behind->next();
180 tmppar = bv->buffer()->getParFromID(undo->number_of_cursor_par);
181 UpdatableInset* it = static_cast<UpdatableInset*>(tmppar3->inInset());
183 it->getLyXText(bv)->redoParagraphs(bv, it->getLyXText(bv)->cursor,
186 it->getLyXText(bv)->setCursorIntern(bv, tmppar, undo->cursor_pos);
187 it->getLyXText(bv)->updateCounters(bv, it->getLyXText(bv)->cursor.row());
190 it->update(bv, font, false);
191 #ifdef THIS_DOES_NOT_WORK
192 // we need this anyway as also if the undo was
193 // inside an inset we have to redo the
194 // paragraph breaking
195 bv->text->redoParagraphs(bv, bv->text->cursor,
196 bv->text->cursor.par());
199 bv->text->redoParagraphs(bv, bv->text->cursor, endpar);
201 bv->text->setCursorIntern(bv, tmppar, undo->cursor_pos);
202 bv->text->updateCounters(bv, bv->text->cursor.row());
209 bv->text->status(bv, LyXText::NEED_MORE_REFRESH);
216 // makes sure the next operation will be stored
217 undo_finished = true;
223 // this is dangerous and for internal use only
230 // this is dangerous and for internal use only
235 void setUndo(BufferView * bv, Undo::undo_kind kind,
236 Paragraph const * first, Paragraph const * behind)
239 bv->buffer()->undostack.push(createUndo(bv, kind, first, behind));
240 bv->buffer()->redostack.clear();
244 void setRedo(BufferView * bv, Undo::undo_kind kind,
245 Paragraph const * first, Paragraph const * behind)
247 bv->buffer()->redostack.push(createUndo(bv, kind, first, behind));
252 Undo * createUndo(BufferView * bv, Undo::undo_kind kind,
253 Paragraph const * first, Paragraph const * behind)
257 int before_number = -1;
258 int behind_number = -1;
261 if (first->previous())
262 before_number = first->previous()->id();
264 behind_number = behind->id();
265 if (first->inInset())
266 inset_id = first->inInset()->id();
268 // Undo::EDIT and Undo::FINISH are
269 // always finished. (no overlapping there)
270 // overlapping only with insert and delete inside one paragraph:
271 // Nobody wants all removed character
272 // appear one by one when undoing.
273 // EDIT is special since only layout information, not the
274 // contents of a paragaph are stored.
275 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)){
276 // check wether storing is needed
277 if (!bv->buffer()->undostack.empty() &&
278 bv->buffer()->undostack.top()->kind == kind &&
279 bv->buffer()->undostack.top()->number_of_before_par == before_number &&
280 bv->buffer()->undostack.top()->number_of_behind_par == behind_number ){
288 Paragraph * start = const_cast<Paragraph *>(first);
292 end = const_cast<Paragraph*>(behind->previous());
298 if (start && end && (start != end->next()) &&
299 ((before_number != behind_number) ||
300 ((before_number < 0) && (behind_number < 0)))) {
301 Paragraph * tmppar = start;
302 Paragraph * tmppar2 = new Paragraph(*tmppar, true);
303 tmppar2->id(tmppar->id());
305 // a memory optimization: Just store the layout information
307 if (kind == Undo::EDIT) {
308 tmppar2->clearContents();
313 while (tmppar != end && tmppar->next()) {
314 tmppar = tmppar->next();
315 tmppar2->next(new Paragraph(*tmppar, false));
316 tmppar2->next()->id(tmppar->id());
317 // a memory optimization: Just store the layout
318 // information when only edit
319 if (kind == Undo::EDIT) {
320 tmppar2->clearContents();
322 tmppar2->next()->previous(tmppar2);
323 tmppar2 = tmppar2->next();
327 undopar = 0; // nothing to replace (undo of delete maybe)
329 int cursor_par = undoCursor(bv).par()->id();
330 int cursor_pos = undoCursor(bv).pos();
332 Undo * undo = new Undo(kind, inset_id,
333 before_number, behind_number,
334 cursor_par, cursor_pos, undopar);
336 undo_finished = false;
341 void setCursorParUndo(BufferView * bv)
343 setUndo(bv, Undo::FINISH,
344 bv->text->cursor.par(),
345 bv->text->cursor.par()->next());
348 Paragraph * firstUndoParagraph(BufferView * bv, int inset_id)
350 Inset * inset = bv->buffer()->getInsetFromID(inset_id);
352 Paragraph * result = inset->firstParagraph();
356 return bv->text->ownerParagraph();
359 LyXCursor const & undoCursor(BufferView * bv)
361 if (bv->theLockingInset())
362 return bv->theLockingInset()->cursor(bv);
363 return bv->text->cursor;