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 #include "iterators.h"
29 using boost::shared_ptr;
32 /// the flag used by FinishUndo();
39 /// utility to return the cursor
40 LyXCursor const & undoCursor(BufferView * bv)
42 if (bv->theLockingInset())
43 return bv->theLockingInset()->cursor(bv);
44 return bv->text->cursor;
48 * returns a pointer to the very first Paragraph depending of where we are
49 * so it will return the first paragraph of the buffer or the first paragraph
50 * of the textinset we're in.
52 Paragraph * firstUndoParagraph(BufferView * bv, int inset_id)
54 Inset * inset = bv->buffer()->getInsetFromID(inset_id);
56 Paragraph * result = inset->getFirstParagraph(0);
60 return bv->text->ownerParagraph();
65 * Finish the undo operation in the case there was no entry
66 * on the stack to perform.
68 void finishNoUndo(BufferView * bv)
71 bv->unlockInset(bv->theLockingInset());
73 bv->text->status(bv, LyXText::NEED_MORE_REFRESH);
78 // returns false if no undo possible
79 bool textHandleUndo(BufferView * bv, Undo & undo)
81 Buffer * b = bv->buffer();
84 b->getParFromID(undo.number_of_before_par);
86 b->getParFromID(undo.number_of_behind_par);
90 // if there's no before take the beginning
91 // of the document for redoing
93 LyXText * t = bv->text;
94 int num = undo.number_of_inset_id;
95 if (undo.number_of_inset_id >= 0) {
96 Inset * in = bv->buffer()->getInsetFromID(num);
98 t = in->getLyXText(bv);
103 t->setCursorIntern(bv, firstUndoParagraph(bv, num), 0);
106 // replace the paragraphs with the undo informations
108 Paragraph * tmppar3 = undo.par;
109 undo.par = 0; /* otherwise the undo destructor would
110 delete the paragraph */
112 // get last undo par and set the right(new) inset-owner of the
113 // paragraph if there is any. This is not needed if we don't have
114 // a paragraph before because then in is automatically done in the
115 // function which assigns the first paragraph to an InsetText. (Jug)
116 Paragraph * tmppar4 = tmppar3;
120 in = before->inInset();
121 else if (undo.number_of_inset_id >= 0)
122 in = bv->buffer()->getInsetFromID(undo.number_of_inset_id);
123 tmppar4->setInsetOwner(in);
124 while (tmppar4->next()) {
125 tmppar4 = tmppar4->next();
126 tmppar4->setInsetOwner(in);
130 vector<Paragraph *> deletelist;
132 Paragraph * deletepar;
134 // now add old paragraphs to be deleted
135 if (before != behind || (!behind && !before)) {
137 deletepar = before->next();
139 deletepar = firstUndoParagraph(bv, undo.number_of_inset_id);
141 while (deletepar && deletepar != behind) {
142 deletelist.push_back(deletepar);
144 deletepar = deletepar->next();
146 // a memory optimization for edit:
147 // Only layout information
148 // is stored in the undo. So restore
149 // the text informations.
150 if (undo.kind == Undo::EDIT) {
151 tmppar2->setContentsFromPar(tmppar);
152 tmppar2 = tmppar2->next();
157 // put the new stuff in the list if there is one
160 before->next(tmppar3);
162 bv->text->ownerParagraph(firstUndoParagraph(bv, undo.number_of_inset_id)->id(),
165 tmppar3->previous(before);
167 // We enter here on DELETE undo operations where we have to
168 // substitue the second paragraph with the first if the removed
170 if (!before && behind) {
171 bv->text->ownerParagraph(firstUndoParagraph(bv, undo.number_of_inset_id)->id(),
177 tmppar4->next(behind);
179 behind->previous(tmppar4);
183 // Set the cursor for redoing
185 Inset * it = before->inInset();
187 it->getLyXText(bv)->setCursorIntern(bv, before, 0);
189 bv->text->setCursorIntern(bv, before, 0);
192 Paragraph * endpar = 0;
193 // calculate the endpar for redoing the paragraphs.
195 endpar = behind->next();
197 tmppar = bv->buffer()->getParFromID(undo.number_of_cursor_par);
198 UpdatableInset* it = 0;
200 it = static_cast<UpdatableInset*>(tmppar3->inInset());
202 it->getLyXText(bv)->redoParagraphs(bv,
203 it->getLyXText(bv)->cursor,
206 it = static_cast<UpdatableInset*>(tmppar->inInset());
210 t = it->getLyXText(bv);
214 t->setCursorIntern(bv, tmppar, undo.cursor_pos);
215 // clear any selection and set the selection cursor
216 // for an evt. new selection.
218 t->selection.cursor = t->cursor;
219 t->updateCounters(bv, t->cursor.row());
222 bv->updateInset(it, false);
223 bv->text->setCursorIntern(bv, bv->text->cursor.par(),
224 bv->text->cursor.pos());
226 bv->text->redoParagraphs(bv, bv->text->cursor, endpar);
229 Inset * it = tmppar->inInset();
232 t = it->getLyXText(bv);
236 t->setCursorIntern(bv, tmppar, undo.cursor_pos);
237 // clear any selection and set the selection cursor
238 // for an evt. new selection.
240 t->selection.cursor = t->cursor;
241 t->updateCounters(bv, t->cursor.row());
245 // And here it's safe enough to delete all removed paragraphs
246 vector<Paragraph *>::iterator pit = deletelist.begin();
247 if (pit != deletelist.end()) {
248 for(;pit != deletelist.end(); ++pit) {
256 bv->text->status(bv, LyXText::NEED_MORE_REFRESH);
261 bool createUndo(BufferView * bv, Undo::undo_kind kind,
262 Paragraph const * first, Paragraph const * behind, shared_ptr<Undo> & u)
266 int before_number = -1;
267 int behind_number = -1;
270 if (first->previous())
271 before_number = first->previous()->id();
273 behind_number = behind->id();
274 if (first->inInset())
275 inset_id = first->inInset()->id();
277 Buffer * b = bv->buffer();
279 // Undo::EDIT and Undo::FINISH are
280 // always finished. (no overlapping there)
281 // overlapping only with insert and delete inside one paragraph:
282 // Nobody wants all removed character
283 // appear one by one when undoing.
284 // EDIT is special since only layout information, not the
285 // contents of a paragaph are stored.
286 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
287 // check whether storing is needed
288 if (!b->undostack.empty() &&
289 b->undostack.top()->kind == kind &&
290 b->undostack.top()->number_of_before_par == before_number &&
291 b->undostack.top()->number_of_behind_par == behind_number) {
300 Paragraph * start = const_cast<Paragraph *>(first);
304 end = const_cast<Paragraph*>(behind->previous());
310 if (start && end && (start != end->next()) &&
311 ((before_number != behind_number) ||
312 ((before_number < 0) && (behind_number < 0))))
314 Paragraph * tmppar = start;
315 Paragraph * tmppar2 = new Paragraph(*tmppar, true);
317 // a memory optimization: Just store the layout information
319 if (kind == Undo::EDIT) {
320 tmppar2->clearContents();
325 while (tmppar != end && tmppar->next()) {
326 tmppar = tmppar->next();
327 // FIXME: what does this #if 0 mean ?
329 tmppar2->next(new Paragraph(*tmppar, true));
331 Paragraph * ptmp = new Paragraph(*tmppar, true);
334 // a memory optimization: Just store the layout
335 // information when only edit
336 if (kind == Undo::EDIT) {
337 tmppar2->clearContents();
339 tmppar2->next()->previous(tmppar2);
341 tmppar2 = tmppar2->next();
345 undopar = 0; // nothing to replace (undo of delete maybe)
347 int cursor_par = undoCursor(bv).par()->id();
348 int cursor_pos = undoCursor(bv).pos();
350 u.reset(new Undo(kind, inset_id,
351 before_number, behind_number,
352 cursor_par, cursor_pos, undopar));
354 undo_finished = false;
362 // makes sure the next operation will be stored
363 undo_finished = true;
369 // this is dangerous and for internal use only
376 // this is dangerous and for internal use only
381 // returns false if no undo possible
382 bool textUndo(BufferView * bv)
384 Buffer * b = bv->buffer();
386 if (b->undostack.empty()) {
391 shared_ptr<Undo> undo = b->undostack.top();
396 Paragraph * first = b->getParFromID(undo->number_of_before_par);
397 if (first && first->next())
398 first = first->next();
400 first = firstUndoParagraph(bv, undo->number_of_inset_id);
403 if (createUndo(bv, undo->kind, first,
404 b->getParFromID(undo->number_of_behind_par), u))
405 b->redostack.push(u);
409 // now we can unlock the inset for saftey because the inset pointer could
410 // be changed during the undo-function. Anyway if needed we have to lock
411 // the right inset/position if this is requested.
413 bv->unlockInset(bv->theLockingInset());
414 bool const ret = textHandleUndo(bv, *undo.get());
420 // returns false if no redo possible
421 bool textRedo(BufferView * bv)
423 Buffer * b = bv->buffer();
425 if (b->redostack.empty()) {
430 shared_ptr<Undo> undo = b->redostack.top();
435 Paragraph * first = bv->buffer()->getParFromID(undo->number_of_before_par);
436 if (first && first->next())
437 first = first->next();
439 first = firstUndoParagraph(bv, undo->number_of_inset_id);
442 if (createUndo(bv, undo->kind, first,
443 bv->buffer()->getParFromID(undo->number_of_behind_par), u))
444 bv->buffer()->undostack.push(u);
448 // now we can unlock the inset for saftey because the inset pointer could
449 // be changed during the undo-function. Anyway if needed we have to lock
450 // the right inset/position if this is requested.
452 bv->unlockInset(bv->theLockingInset());
453 bool ret = textHandleUndo(bv, *undo.get());
459 void setUndo(BufferView * bv, Undo::undo_kind kind,
460 Paragraph const * first, Paragraph const * behind)
464 if (createUndo(bv, kind, first, behind, u))
465 bv->buffer()->undostack.push(u);
466 bv->buffer()->redostack.clear();
471 void setRedo(BufferView * bv, Undo::undo_kind kind,
472 Paragraph const * first, Paragraph const * behind)
475 if (createUndo(bv, kind, first, behind, u))
476 bv->buffer()->redostack.push(u);
480 void setCursorParUndo(BufferView * bv)
482 setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
483 bv->text->cursor.par()->next());