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();
81 b->getParFromID(undo.number_of_before_par);
83 b->getParFromID(undo.number_of_behind_par);
87 // if there's no before take the beginning
88 // of the document for redoing
90 LyXText * t = bv->text;
91 int num = undo.number_of_inset_id;
92 if (undo.number_of_inset_id >= 0) {
93 Inset * in = bv->buffer()->getInsetFromID(num);
95 t = in->getLyXText(bv);
100 t->setCursorIntern(firstUndoParagraph(bv, num), 0);
103 // replace the paragraphs with the undo informations
105 Paragraph * undopar = undo.par;
106 undo.par = 0; /* otherwise the undo destructor would
107 delete the paragraph */
109 // get last undo par and set the right(new) inset-owner of the
110 // paragraph if there is any. This is not needed if we don't have
111 // a paragraph before because then in is automatically done in the
112 // function which assigns the first paragraph to an InsetText. (Jug)
113 Paragraph * lastundopar = undopar;
117 in = before->inInset();
118 else if (undo.number_of_inset_id >= 0)
119 in = bv->buffer()->getInsetFromID(undo.number_of_inset_id);
120 lastundopar->setInsetOwner(in);
121 while (lastundopar->next()) {
122 lastundopar = lastundopar->next();
123 lastundopar->setInsetOwner(in);
127 vector<Paragraph *> deletelist;
129 Paragraph * deletepar;
131 // now add old paragraphs to be deleted
132 if (before != behind || (!behind && !before)) {
134 deletepar = before->next();
136 deletepar = firstUndoParagraph(bv, undo.number_of_inset_id);
138 while (deletepar && deletepar != behind) {
139 deletelist.push_back(deletepar);
141 deletepar = deletepar->next();
143 // a memory optimization for edit:
144 // Only layout information
145 // is stored in the undo. So restore
146 // the text informations.
147 if (undo.kind == Undo::EDIT) {
148 tmppar2->setContentsFromPar(tmppar);
149 tmppar2 = tmppar2->next();
154 // The order here is VERY IMPORTANT. We have to set the right
155 // next/prev pointer in the paragraphs so that a rebuild of
156 // the LyXText works!!!
158 // thread the end of the undo onto the par in front if any
160 lastundopar->next(behind);
162 behind->previous(lastundopar);
165 // put the new stuff in the list if there is one
167 undopar->previous(before);
169 before->next(undopar);
171 int id = firstUndoParagraph(bv, undo.number_of_inset_id)->id();
172 Paragraph * op = bv->buffer()->getParFromID(id);
173 if (op && op->inInset()) {
174 static_cast<InsetText*>(op->inInset())->paragraph(undopar);
176 bv->buffer()->paragraphs.set(undopar);
180 // We enter here on DELETE undo operations where we have to
181 // substitue the second paragraph with the first if the removed
183 if (!before && behind) {
184 int id = firstUndoParagraph(bv, undo.number_of_inset_id)->id();
185 Paragraph * op = bv->buffer()->getParFromID(id);
186 if (op && op->inInset()) {
187 static_cast<InsetText*>(op->inInset())->paragraph(behind);
189 bv->buffer()->paragraphs.set(behind);
197 // Set the cursor for redoing
198 if (before) { // if we have a par before the undopar
199 Inset * it = before->inInset();
201 it->getLyXText(bv)->setCursorIntern(before, 0);
203 bv->text->setCursorIntern(before, 0);
205 // we are not ready for this we cannot set the cursor for a paragraph
206 // which is not already in a row of LyXText!!!
208 else { // otherwise this is the first one and we start here
209 Inset * it = undopar->inInset();
211 it->getLyXText(bv)->setCursorIntern(bv, undopar, 0);
213 bv->text->setCursorIntern(bv, undopar, 0);
217 Paragraph * endpar = 0;
218 // calculate the endpar for redoing the paragraphs.
220 endpar = behind->next();
222 tmppar = bv->buffer()->getParFromID(undo.number_of_cursor_par);
223 UpdatableInset* it = 0;
225 it = static_cast<UpdatableInset*>(undopar->inInset());
227 it->getLyXText(bv)->redoParagraphs(
228 it->getLyXText(bv)->cursor,
231 it = static_cast<UpdatableInset*>(tmppar->inInset());
235 t = it->getLyXText(bv);
239 t->setCursorIntern(tmppar, undo.cursor_pos);
240 // clear any selection and set the selection cursor
241 // for an evt. new selection.
243 t->selection.cursor = t->cursor;
248 bv->text->setCursorIntern(bv->text->cursor.par(),
249 bv->text->cursor.pos());
251 bv->text->redoParagraphs(bv->text->cursor, endpar);
254 Inset * it = tmppar->inInset();
257 t = it->getLyXText(bv);
261 t->setCursorIntern(tmppar, undo.cursor_pos);
262 // clear any selection and set the selection cursor
263 // for an evt. new selection.
265 t->selection.cursor = t->cursor;
270 // And here it's safe enough to delete all removed paragraphs
271 vector<Paragraph *>::iterator pit = deletelist.begin();
272 if (pit != deletelist.end()) {
273 for(;pit != deletelist.end(); ++pit) {
281 bv->text->postPaint(0);
286 bool createUndo(BufferView * bv, Undo::undo_kind kind,
287 Paragraph const * first, Paragraph const * behind, shared_ptr<Undo> & u)
291 int before_number = -1;
292 int behind_number = -1;
295 if (first->previous())
296 before_number = first->previous()->id();
298 behind_number = behind->id();
299 if (first->inInset())
300 inset_id = first->inInset()->id();
302 Buffer * b = bv->buffer();
304 // Undo::EDIT and Undo::FINISH are
305 // always finished. (no overlapping there)
306 // overlapping only with insert and delete inside one paragraph:
307 // Nobody wants all removed character
308 // appear one by one when undoing.
309 // EDIT is special since only layout information, not the
310 // contents of a paragaph are stored.
311 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
312 // check whether storing is needed
313 if (!b->undostack.empty() &&
314 b->undostack.top()->kind == kind &&
315 b->undostack.top()->number_of_before_par == before_number &&
316 b->undostack.top()->number_of_behind_par == behind_number) {
325 Paragraph * start = const_cast<Paragraph *>(first);
329 end = const_cast<Paragraph*>(behind->previous());
335 if (start && end && (start != end->next()) &&
336 ((before_number != behind_number) ||
337 ((before_number < 0) && (behind_number < 0))))
339 Paragraph * tmppar = start;
340 Paragraph * tmppar2 = new Paragraph(*tmppar, true);
342 // a memory optimization: Just store the layout information
344 if (kind == Undo::EDIT) {
345 tmppar2->clearContents();
350 while (tmppar != end && tmppar->next()) {
351 tmppar = tmppar->next();
352 tmppar2->next(new Paragraph(*tmppar, true));
353 // a memory optimization: Just store the layout
354 // information when only edit
355 if (kind == Undo::EDIT) {
356 tmppar2->clearContents();
358 tmppar2->next()->previous(tmppar2);
360 tmppar2 = tmppar2->next();
364 undopar = 0; // nothing to replace (undo of delete maybe)
366 int cursor_par = undoCursor(bv).par()->id();
367 int cursor_pos = undoCursor(bv).pos();
369 u.reset(new Undo(kind, inset_id,
370 before_number, behind_number,
371 cursor_par, cursor_pos, undopar));
373 undo_finished = false;
381 // makes sure the next operation will be stored
382 undo_finished = true;
388 // this is dangerous and for internal use only
395 // this is dangerous and for internal use only
400 // returns false if no undo possible
401 bool textUndo(BufferView * bv)
403 Buffer * b = bv->buffer();
405 if (b->undostack.empty()) {
410 shared_ptr<Undo> undo = b->undostack.top();
415 Paragraph * first = b->getParFromID(undo->number_of_before_par);
416 if (first && first->next())
417 first = first->next();
419 first = firstUndoParagraph(bv, undo->number_of_inset_id);
422 if (createUndo(bv, undo->kind, first,
423 b->getParFromID(undo->number_of_behind_par), u))
424 b->redostack.push(u);
428 // now we can unlock the inset for saftey because the inset pointer could
429 // be changed during the undo-function. Anyway if needed we have to lock
430 // the right inset/position if this is requested.
432 bv->unlockInset(bv->theLockingInset());
433 bool const ret = textHandleUndo(bv, *undo.get());
439 // returns false if no redo possible
440 bool textRedo(BufferView * bv)
442 Buffer * b = bv->buffer();
444 if (b->redostack.empty()) {
449 shared_ptr<Undo> undo = b->redostack.top();
454 Paragraph * first = bv->buffer()->getParFromID(undo->number_of_before_par);
455 if (first && first->next())
456 first = first->next();
458 first = firstUndoParagraph(bv, undo->number_of_inset_id);
461 if (createUndo(bv, undo->kind, first,
462 bv->buffer()->getParFromID(undo->number_of_behind_par), u))
463 bv->buffer()->undostack.push(u);
467 // now we can unlock the inset for saftey because the inset pointer could
468 // be changed during the undo-function. Anyway if needed we have to lock
469 // the right inset/position if this is requested.
471 bv->unlockInset(bv->theLockingInset());
472 bool ret = textHandleUndo(bv, *undo.get());
478 void setUndo(BufferView * bv, Undo::undo_kind kind,
479 Paragraph const * first, Paragraph const * behind)
483 if (createUndo(bv, kind, first, behind, u))
484 bv->buffer()->undostack.push(u);
485 bv->buffer()->redostack.clear();
490 void setRedo(BufferView * bv, Undo::undo_kind kind,
491 Paragraph const * first, Paragraph const * behind)
494 if (createUndo(bv, kind, first, behind, u))
495 bv->buffer()->redostack.push(u);
499 void setCursorParUndo(BufferView * bv)
501 setUndo(bv, Undo::FINISH, &*bv->text->cursor.par(),
502 bv->text->cursor.par()->next());