1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995-2001 The LyX Team.
8 * ====================================================== */
12 #include "undo_funcs.h"
14 #include "BufferView.h"
16 #include "insets/updatableinset.h"
18 #include "support/LAssert.h"
20 #include "iterators.h"
25 using boost::shared_ptr;
28 /// the flag used by FinishUndo();
30 /// whether actions are not added to the undo stacks
35 /// utility to return the cursor
36 LyXCursor const & undoCursor(BufferView * bv)
38 if (bv->theLockingInset())
39 return bv->theLockingInset()->cursor(bv);
40 return bv->text->cursor;
44 * returns a pointer to the very first Paragraph depending of where we are
45 * so it will return the first paragraph of the buffer or the first paragraph
46 * of the textinset we're in.
48 Paragraph * firstUndoParagraph(BufferView * bv, int inset_id)
50 Inset * inset = bv->buffer()->getInsetFromID(inset_id);
52 Paragraph * result = inset->getFirstParagraph(0);
56 return bv->text->ownerParagraph();
61 * Finish the undo operation in the case there was no entry
62 * on the stack to perform.
64 void finishNoUndo(BufferView * bv)
67 bv->unlockInset(bv->theLockingInset());
69 bv->text->postPaint(0);
74 // returns false if no undo possible
75 bool textHandleUndo(BufferView * bv, Undo & undo)
77 Buffer * b = bv->buffer();
80 b->getParFromID(undo.number_of_before_par);
82 b->getParFromID(undo.number_of_behind_par);
86 // if there's no before take the beginning
87 // of the document for redoing
89 LyXText * t = bv->text;
90 int num = undo.number_of_inset_id;
91 if (undo.number_of_inset_id >= 0) {
92 Inset * in = bv->buffer()->getInsetFromID(num);
94 t = in->getLyXText(bv);
99 t->setCursorIntern(firstUndoParagraph(bv, num), 0);
102 // replace the paragraphs with the undo informations
104 Paragraph * undopar = undo.par;
105 undo.par = 0; /* otherwise the undo destructor would
106 delete the paragraph */
108 // get last undo par and set the right(new) inset-owner of the
109 // paragraph if there is any. This is not needed if we don't have
110 // a paragraph before because then in is automatically done in the
111 // function which assigns the first paragraph to an InsetText. (Jug)
112 Paragraph * lastundopar = undopar;
116 in = before->inInset();
117 else if (undo.number_of_inset_id >= 0)
118 in = bv->buffer()->getInsetFromID(undo.number_of_inset_id);
119 lastundopar->setInsetOwner(in);
120 while (lastundopar->next()) {
121 lastundopar = lastundopar->next();
122 lastundopar->setInsetOwner(in);
126 vector<Paragraph *> deletelist;
128 Paragraph * deletepar;
130 // now add old paragraphs to be deleted
131 if (before != behind || (!behind && !before)) {
133 deletepar = before->next();
135 deletepar = firstUndoParagraph(bv, undo.number_of_inset_id);
137 while (deletepar && deletepar != behind) {
138 deletelist.push_back(deletepar);
140 deletepar = deletepar->next();
142 // a memory optimization for edit:
143 // Only layout information
144 // is stored in the undo. So restore
145 // the text informations.
146 if (undo.kind == Undo::EDIT) {
147 tmppar2->setContentsFromPar(tmppar);
148 tmppar2 = tmppar2->next();
153 // The order here is VERY IMPORTANT. We have to set the right
154 // next/prev pointer in the paragraphs so that a rebuild of
155 // the LyXText works!!!
157 // thread the end of the undo onto the par in front if any
159 lastundopar->next(behind);
161 behind->previous(lastundopar);
164 // put the new stuff in the list if there is one
166 undopar->previous(before);
168 before->next(undopar);
170 bv->text->ownerParagraph(firstUndoParagraph(bv, undo.number_of_inset_id)->id(),
174 // We enter here on DELETE undo operations where we have to
175 // substitue the second paragraph with the first if the removed
177 if (!before && behind) {
178 bv->text->ownerParagraph(firstUndoParagraph(bv, undo.number_of_inset_id)->id(),
185 // Set the cursor for redoing
186 if (before) { // if we have a par before the undopar
187 Inset * it = before->inInset();
189 it->getLyXText(bv)->setCursorIntern(before, 0);
191 bv->text->setCursorIntern(before, 0);
193 // we are not ready for this we cannot set the cursor for a paragraph
194 // which is not already in a row of LyXText!!!
196 else { // otherwise this is the first one and we start here
197 Inset * it = undopar->inInset();
199 it->getLyXText(bv)->setCursorIntern(bv, undopar, 0);
201 bv->text->setCursorIntern(bv, undopar, 0);
205 Paragraph * endpar = 0;
206 // calculate the endpar for redoing the paragraphs.
208 endpar = behind->next();
210 tmppar = bv->buffer()->getParFromID(undo.number_of_cursor_par);
211 UpdatableInset* it = 0;
213 it = static_cast<UpdatableInset*>(undopar->inInset());
215 it->getLyXText(bv)->redoParagraphs(
216 it->getLyXText(bv)->cursor,
219 it = static_cast<UpdatableInset*>(tmppar->inInset());
223 t = it->getLyXText(bv);
227 t->setCursorIntern(tmppar, undo.cursor_pos);
228 // clear any selection and set the selection cursor
229 // for an evt. new selection.
231 t->selection.cursor = t->cursor;
236 bv->text->setCursorIntern(bv->text->cursor.par(),
237 bv->text->cursor.pos());
239 bv->text->redoParagraphs(bv->text->cursor, endpar);
242 Inset * it = tmppar->inInset();
245 t = it->getLyXText(bv);
249 t->setCursorIntern(tmppar, undo.cursor_pos);
250 // clear any selection and set the selection cursor
251 // for an evt. new selection.
253 t->selection.cursor = t->cursor;
258 // And here it's safe enough to delete all removed paragraphs
259 vector<Paragraph *>::iterator pit = deletelist.begin();
260 if (pit != deletelist.end()) {
261 for(;pit != deletelist.end(); ++pit) {
269 bv->text->postPaint(0);
274 bool createUndo(BufferView * bv, Undo::undo_kind kind,
275 Paragraph const * first, Paragraph const * behind, shared_ptr<Undo> & u)
279 int before_number = -1;
280 int behind_number = -1;
283 if (first->previous())
284 before_number = first->previous()->id();
286 behind_number = behind->id();
287 if (first->inInset())
288 inset_id = first->inInset()->id();
290 Buffer * b = bv->buffer();
292 // Undo::EDIT and Undo::FINISH are
293 // always finished. (no overlapping there)
294 // overlapping only with insert and delete inside one paragraph:
295 // Nobody wants all removed character
296 // appear one by one when undoing.
297 // EDIT is special since only layout information, not the
298 // contents of a paragaph are stored.
299 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
300 // check whether storing is needed
301 if (!b->undostack.empty() &&
302 b->undostack.top()->kind == kind &&
303 b->undostack.top()->number_of_before_par == before_number &&
304 b->undostack.top()->number_of_behind_par == behind_number) {
313 Paragraph * start = const_cast<Paragraph *>(first);
317 end = const_cast<Paragraph*>(behind->previous());
323 if (start && end && (start != end->next()) &&
324 ((before_number != behind_number) ||
325 ((before_number < 0) && (behind_number < 0))))
327 Paragraph * tmppar = start;
328 Paragraph * tmppar2 = new Paragraph(*tmppar, true);
330 // a memory optimization: Just store the layout information
332 if (kind == Undo::EDIT) {
333 tmppar2->clearContents();
338 while (tmppar != end && tmppar->next()) {
339 tmppar = tmppar->next();
340 tmppar2->next(new Paragraph(*tmppar, true));
341 // a memory optimization: Just store the layout
342 // information when only edit
343 if (kind == Undo::EDIT) {
344 tmppar2->clearContents();
346 tmppar2->next()->previous(tmppar2);
348 tmppar2 = tmppar2->next();
352 undopar = 0; // nothing to replace (undo of delete maybe)
354 int cursor_par = undoCursor(bv).par()->id();
355 int cursor_pos = undoCursor(bv).pos();
357 u.reset(new Undo(kind, inset_id,
358 before_number, behind_number,
359 cursor_par, cursor_pos, undopar));
361 undo_finished = false;
369 // makes sure the next operation will be stored
370 undo_finished = true;
376 // this is dangerous and for internal use only
383 // this is dangerous and for internal use only
388 // returns false if no undo possible
389 bool textUndo(BufferView * bv)
391 Buffer * b = bv->buffer();
393 if (b->undostack.empty()) {
398 shared_ptr<Undo> undo = b->undostack.top();
403 Paragraph * first = b->getParFromID(undo->number_of_before_par);
404 if (first && first->next())
405 first = first->next();
407 first = firstUndoParagraph(bv, undo->number_of_inset_id);
410 if (createUndo(bv, undo->kind, first,
411 b->getParFromID(undo->number_of_behind_par), u))
412 b->redostack.push(u);
416 // now we can unlock the inset for saftey because the inset pointer could
417 // be changed during the undo-function. Anyway if needed we have to lock
418 // the right inset/position if this is requested.
420 bv->unlockInset(bv->theLockingInset());
421 bool const ret = textHandleUndo(bv, *undo.get());
427 // returns false if no redo possible
428 bool textRedo(BufferView * bv)
430 Buffer * b = bv->buffer();
432 if (b->redostack.empty()) {
437 shared_ptr<Undo> undo = b->redostack.top();
442 Paragraph * first = bv->buffer()->getParFromID(undo->number_of_before_par);
443 if (first && first->next())
444 first = first->next();
446 first = firstUndoParagraph(bv, undo->number_of_inset_id);
449 if (createUndo(bv, undo->kind, first,
450 bv->buffer()->getParFromID(undo->number_of_behind_par), u))
451 bv->buffer()->undostack.push(u);
455 // now we can unlock the inset for saftey because the inset pointer could
456 // be changed during the undo-function. Anyway if needed we have to lock
457 // the right inset/position if this is requested.
459 bv->unlockInset(bv->theLockingInset());
460 bool ret = textHandleUndo(bv, *undo.get());
466 void setUndo(BufferView * bv, Undo::undo_kind kind,
467 Paragraph const * first, Paragraph const * behind)
471 if (createUndo(bv, kind, first, behind, u))
472 bv->buffer()->undostack.push(u);
473 bv->buffer()->redostack.clear();
478 void setRedo(BufferView * bv, Undo::undo_kind kind,
479 Paragraph const * first, Paragraph const * behind)
482 if (createUndo(bv, kind, first, behind, u))
483 bv->buffer()->redostack.push(u);
487 void setCursorParUndo(BufferView * bv)
489 setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
490 bv->text->cursor.par()->next());