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"
17 #include "insets/insettext.h"
19 #include "support/LAssert.h"
21 #include "iterators.h"
26 using boost::shared_ptr;
29 /// the flag used by FinishUndo();
31 /// whether actions are not added to the undo stacks
36 /// utility to return the cursor
37 LyXCursor const & undoCursor(BufferView * bv)
39 if (bv->theLockingInset())
40 return bv->theLockingInset()->cursor(bv);
41 return bv->text->cursor;
45 * returns a pointer to the very first Paragraph depending of where we are
46 * so it will return the first paragraph of the buffer or the first paragraph
47 * of the textinset we're in.
49 Paragraph * firstUndoParagraph(BufferView * bv, int inset_id)
51 Inset * inset = bv->buffer()->getInsetFromID(inset_id);
53 Paragraph * result = inset->getFirstParagraph(0);
57 return &*bv->text->ownerParagraphs().begin();
62 * Finish the undo operation in the case there was no entry
63 * on the stack to perform.
65 void finishNoUndo(BufferView * bv)
68 bv->unlockInset(bv->theLockingInset());
70 bv->text->postPaint(0);
75 // returns false if no undo possible
76 bool textHandleUndo(BufferView * bv, Undo & undo)
78 Buffer * b = bv->buffer();
80 Paragraph * before = &*b->getParFromID(undo.number_of_before_par);
81 Paragraph * behind = &*b->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(firstUndoParagraph(bv, num), 0);
101 // replace the paragraphs with the undo informations
103 Paragraph * undopar = undo.par;
104 undo.par = 0; /* otherwise the undo destructor would
105 delete the paragraph */
107 // get last undo par and set the right(new) inset-owner of the
108 // paragraph if there is any. This is not needed if we don't have
109 // a paragraph before because then in is automatically done in the
110 // function which assigns the first paragraph to an InsetText. (Jug)
111 Paragraph * lastundopar = undopar;
115 in = before->inInset();
116 else if (undo.number_of_inset_id >= 0)
117 in = bv->buffer()->getInsetFromID(undo.number_of_inset_id);
118 lastundopar->setInsetOwner(in);
119 while (lastundopar->next()) {
120 lastundopar = lastundopar->next();
121 lastundopar->setInsetOwner(in);
125 vector<Paragraph *> deletelist;
127 Paragraph * deletepar;
129 // now add old paragraphs to be deleted
130 if (before != behind || (!behind && !before)) {
132 deletepar = before->next();
134 deletepar = firstUndoParagraph(bv, undo.number_of_inset_id);
136 while (deletepar && deletepar != behind) {
137 deletelist.push_back(deletepar);
139 deletepar = deletepar->next();
141 // a memory optimization for edit:
142 // Only layout information
143 // is stored in the undo. So restore
144 // the text informations.
145 if (undo.kind == Undo::EDIT) {
146 tmppar2->setContentsFromPar(*tmppar);
147 tmppar2 = tmppar2->next();
152 // The order here is VERY IMPORTANT. We have to set the right
153 // next/prev pointer in the paragraphs so that a rebuild of
154 // the LyXText works!!!
156 // thread the end of the undo onto the par in front if any
158 lastundopar->next(behind);
160 behind->previous(lastundopar);
163 // put the new stuff in the list if there is one
165 undopar->previous(before);
167 before->next(undopar);
169 int id = firstUndoParagraph(bv, undo.number_of_inset_id)->id();
170 Paragraph * op = &*bv->buffer()->getParFromID(id);
171 if (op && op->inInset()) {
172 static_cast<InsetText*>(op->inInset())->paragraph(undopar);
174 bv->buffer()->paragraphs.set(undopar);
178 // We enter here on DELETE undo operations where we have to
179 // substitue the second paragraph with the first if the removed
181 if (!before && behind) {
182 int id = firstUndoParagraph(bv, undo.number_of_inset_id)->id();
183 Paragraph * op = &*bv->buffer()->getParFromID(id);
184 if (op && op->inInset()) {
185 static_cast<InsetText*>(op->inInset())->paragraph(behind);
187 bv->buffer()->paragraphs.set(behind);
195 // Set the cursor for redoing
196 if (before) { // if we have a par before the undopar
197 Inset * it = before->inInset();
199 it->getLyXText(bv)->setCursorIntern(before, 0);
201 bv->text->setCursorIntern(before, 0);
203 // we are not ready for this we cannot set the cursor for a paragraph
204 // which is not already in a row of LyXText!!!
206 else { // otherwise this is the first one and we start here
207 Inset * it = undopar->inInset();
209 it->getLyXText(bv)->setCursorIntern(bv, undopar, 0);
211 bv->text->setCursorIntern(bv, undopar, 0);
215 Paragraph * endpar = 0;
216 // calculate the endpar for redoing the paragraphs.
218 endpar = behind->next();
220 tmppar = &*bv->buffer()->getParFromID(undo.number_of_cursor_par);
221 UpdatableInset* it = 0;
223 it = static_cast<UpdatableInset*>(undopar->inInset());
225 it->getLyXText(bv)->redoParagraphs(
226 it->getLyXText(bv)->cursor,
229 it = static_cast<UpdatableInset*>(tmppar->inInset());
233 t = it->getLyXText(bv);
237 t->setCursorIntern(tmppar, undo.cursor_pos);
238 // clear any selection and set the selection cursor
239 // for an evt. new selection.
241 t->selection.cursor = t->cursor;
246 bv->text->setCursorIntern(bv->text->cursor.par(),
247 bv->text->cursor.pos());
249 bv->text->redoParagraphs(bv->text->cursor, endpar);
252 Inset * it = tmppar->inInset();
255 t = it->getLyXText(bv);
259 t->setCursorIntern(tmppar, undo.cursor_pos);
260 // clear any selection and set the selection cursor
261 // for an evt. new selection.
263 t->selection.cursor = t->cursor;
268 // And here it's safe enough to delete all removed paragraphs
269 vector<Paragraph *>::iterator pit = deletelist.begin();
270 if (pit != deletelist.end()) {
271 for(;pit != deletelist.end(); ++pit) {
279 bv->text->postPaint(0);
284 bool createUndo(BufferView * bv, Undo::undo_kind kind,
285 ParagraphList::iterator itfirst, ParagraphList::iterator itbehind,
286 shared_ptr<Undo> & u)
288 Paragraph * first = &*itfirst;
289 Paragraph * behind = &*itbehind;
292 int before_number = -1;
293 int behind_number = -1;
296 if (first->previous())
297 before_number = first->previous()->id();
299 behind_number = behind->id();
300 if (first->inInset())
301 inset_id = first->inInset()->id();
303 Buffer * b = bv->buffer();
305 // Undo::EDIT and Undo::FINISH are
306 // always finished. (no overlapping there)
307 // overlapping only with insert and delete inside one paragraph:
308 // Nobody wants all removed character
309 // appear one by one when undoing.
310 // EDIT is special since only layout information, not the
311 // contents of a paragaph are stored.
312 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
313 // check whether storing is needed
314 if (!b->undostack.empty() &&
315 b->undostack.top()->kind == kind &&
316 b->undostack.top()->number_of_before_par == before_number &&
317 b->undostack.top()->number_of_behind_par == behind_number) {
324 Paragraph * undopar = 0; // nothing to replace yet (undo of delete maybe)
326 Paragraph * start = first;
327 Paragraph * end = &*boost::prior(itbehind);
329 if (start && end && (start != end->next()) &&
330 ((before_number != behind_number) ||
331 ((before_number < 0) && (behind_number < 0))))
333 Paragraph * tmppar = start;
334 Paragraph * tmppar2 = new Paragraph(*tmppar, true);
336 // a memory optimization: Just store the layout information
338 if (kind == Undo::EDIT) {
339 tmppar2->clearContents();
344 while (tmppar != end && tmppar->next()) {
345 tmppar = tmppar->next();
346 tmppar2->next(new Paragraph(*tmppar, true));
347 // a memory optimization: Just store the layout
348 // information when only edit
349 if (kind == Undo::EDIT) {
350 tmppar2->clearContents();
352 tmppar2->next()->previous(tmppar2);
354 tmppar2 = tmppar2->next();
359 int cursor_par = undoCursor(bv).par()->id();
360 int cursor_pos = undoCursor(bv).pos();
362 u.reset(new Undo(kind, inset_id,
363 before_number, behind_number,
364 cursor_par, cursor_pos, undopar));
366 undo_finished = false;
374 // makes sure the next operation will be stored
375 undo_finished = true;
381 // this is dangerous and for internal use only
388 // this is dangerous and for internal use only
393 // returns false if no undo possible
394 bool textUndo(BufferView * bv)
396 Buffer * b = bv->buffer();
398 if (b->undostack.empty()) {
403 shared_ptr<Undo> undo = b->undostack.top();
408 Paragraph * first = &*b->getParFromID(undo->number_of_before_par);
409 if (first && first->next())
410 first = first->next();
412 first = firstUndoParagraph(bv, undo->number_of_inset_id);
415 if (createUndo(bv, undo->kind, first,
416 b->getParFromID(undo->number_of_behind_par), u))
417 b->redostack.push(u);
421 // now we can unlock the inset for saftey because the inset pointer could
422 // be changed during the undo-function. Anyway if needed we have to lock
423 // the right inset/position if this is requested.
425 bv->unlockInset(bv->theLockingInset());
426 bool const ret = textHandleUndo(bv, *undo.get());
432 // returns false if no redo possible
433 bool textRedo(BufferView * bv)
435 Buffer * b = bv->buffer();
437 if (b->redostack.empty()) {
442 shared_ptr<Undo> undo = b->redostack.top();
447 Paragraph * first = &*bv->buffer()->getParFromID(undo->number_of_before_par);
448 if (first && first->next())
449 first = first->next();
451 first = firstUndoParagraph(bv, undo->number_of_inset_id);
454 if (createUndo(bv, undo->kind, first,
455 &*bv->buffer()->getParFromID(undo->number_of_behind_par), u))
456 bv->buffer()->undostack.push(u);
460 // now we can unlock the inset for saftey because the inset pointer could
461 // be changed during the undo-function. Anyway if needed we have to lock
462 // the right inset/position if this is requested.
464 bv->unlockInset(bv->theLockingInset());
465 bool ret = textHandleUndo(bv, *undo.get());
471 void setUndo(BufferView * bv, Undo::undo_kind kind,
472 ParagraphList::iterator first, ParagraphList::iterator behind)
476 if (createUndo(bv, kind, first, behind, u))
477 bv->buffer()->undostack.push(u);
478 bv->buffer()->redostack.clear();
483 void setRedo(BufferView * bv, Undo::undo_kind kind,
484 ParagraphList::iterator first, ParagraphList::iterator behind)
487 if (createUndo(bv, kind, first, behind, u))
488 bv->buffer()->redostack.push(u);
492 void setCursorParUndo(BufferView * bv)
494 setUndo(bv, Undo::FINISH, &*bv->text->cursor.par(),
495 bv->text->cursor.par()->next());