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 * const before = &*b->getParFromID(undo.number_of_before_par);
81 Paragraph * const behind = &*b->getParFromID(undo.number_of_behind_par);
83 // if there's no before take the beginning
84 // of the document for redoing
86 LyXText * t = bv->text;
87 int num = undo.number_of_inset_id;
88 if (undo.number_of_inset_id >= 0) {
89 Inset * in = bv->buffer()->getInsetFromID(num);
91 t = in->getLyXText(bv);
96 t->setCursorIntern(firstUndoParagraph(bv, num), 0);
99 // replace the paragraphs with the undo informations
101 Paragraph * undopar = undo.par;
102 undo.par = 0; /* otherwise the undo destructor would
103 delete the paragraph */
105 // get last undo par and set the right(new) inset-owner of the
106 // paragraph if there is any. This is not needed if we don't have
107 // a paragraph before because then in is automatically done in the
108 // function which assigns the first paragraph to an InsetText. (Jug)
109 Paragraph * lastundopar = undopar;
113 in = before->inInset();
114 else if (undo.number_of_inset_id >= 0)
115 in = bv->buffer()->getInsetFromID(undo.number_of_inset_id);
116 lastundopar->setInsetOwner(in);
117 while (lastundopar->next()) {
118 lastundopar = lastundopar->next();
119 lastundopar->setInsetOwner(in);
123 vector<Paragraph *> deletelist;
125 // now add old paragraphs to be deleted
126 if (before != behind || (!behind && !before)) {
127 Paragraph * deletepar;
129 deletepar = before->next();
131 deletepar = firstUndoParagraph(bv, undo.number_of_inset_id);
132 Paragraph * tmppar2 = undopar;
133 while (deletepar && deletepar != behind) {
134 deletelist.push_back(deletepar);
135 Paragraph * tmppar = deletepar;
136 deletepar = deletepar->next();
138 // a memory optimization for edit:
139 // Only layout information
140 // is stored in the undo. So restore
141 // the text informations.
142 if (undo.kind == Undo::EDIT) {
143 tmppar2->setContentsFromPar(*tmppar);
144 tmppar2 = tmppar2->next();
149 // The order here is VERY IMPORTANT. We have to set the right
150 // next/prev pointer in the paragraphs so that a rebuild of
151 // the LyXText works!!!
153 // thread the end of the undo onto the par in front if any
155 lastundopar->next(behind);
157 behind->previous(lastundopar);
160 // put the new stuff in the list if there is one
162 undopar->previous(before);
164 before->next(undopar);
166 int id = firstUndoParagraph(bv, undo.number_of_inset_id)->id();
167 Paragraph * op = &*bv->buffer()->getParFromID(id);
168 if (op && op->inInset()) {
169 static_cast<InsetText*>(op->inInset())->paragraph(undopar);
171 bv->buffer()->paragraphs.set(undopar);
175 // We enter here on DELETE undo operations where we have to
176 // substitue the second paragraph with the first if the removed
178 if (!before && behind) {
179 int id = firstUndoParagraph(bv, undo.number_of_inset_id)->id();
180 Paragraph * op = &*bv->buffer()->getParFromID(id);
181 if (op && op->inInset()) {
182 static_cast<InsetText*>(op->inInset())->paragraph(behind);
184 bv->buffer()->paragraphs.set(behind);
192 // Set the cursor for redoing
193 if (before) { // if we have a par before the undopar
194 Inset * it = before->inInset();
196 it->getLyXText(bv)->setCursorIntern(before, 0);
198 bv->text->setCursorIntern(before, 0);
200 // we are not ready for this we cannot set the cursor for a paragraph
201 // which is not already in a row of LyXText!!!
203 else { // otherwise this is the first one and we start here
204 Inset * it = undopar->inInset();
206 it->getLyXText(bv)->setCursorIntern(bv, undopar, 0);
208 bv->text->setCursorIntern(bv, undopar, 0);
212 Paragraph * endpar = 0;
213 // calculate the endpar for redoing the paragraphs.
215 endpar = behind->next();
217 UpdatableInset * it = 0;
219 it = static_cast<UpdatableInset*>(undopar->inInset());
221 it->getLyXText(bv)->redoParagraphs(
222 it->getLyXText(bv)->cursor,
225 &*bv->buffer()->getParFromID(undo.number_of_cursor_par);
227 it = static_cast<UpdatableInset*>(tmppar->inInset());
231 t = it->getLyXText(bv);
235 t->setCursorIntern(tmppar, undo.cursor_pos);
236 // clear any selection and set the selection cursor
237 // for an evt. new selection.
239 t->selection.cursor = t->cursor;
244 bv->text->setCursorIntern(bv->text->cursor.par(),
245 bv->text->cursor.pos());
247 bv->text->redoParagraphs(bv->text->cursor, endpar);
249 &*bv->buffer()->getParFromID(undo.number_of_cursor_par);
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;
371 // returns false if no undo possible
372 bool textUndoOrRedo(BufferView * bv,
373 limited_stack<boost::shared_ptr<Undo> > & stack,
374 limited_stack<boost::shared_ptr<Undo> > & otherstack)
376 Buffer * b = bv->buffer();
383 shared_ptr<Undo> undo = stack.top();
388 Paragraph * first = &*b->getParFromID(undo->number_of_before_par);
389 if (first && first->next())
390 first = first->next();
392 first = firstUndoParagraph(bv, undo->number_of_inset_id);
395 if (createUndo(bv, undo->kind, first,
396 b->getParFromID(undo->number_of_behind_par), u))
401 // now we can unlock the inset for saftey because the inset pointer could
402 // be changed during the undo-function. Anyway if needed we have to lock
403 // the right inset/position if this is requested.
405 bv->unlockInset(bv->theLockingInset());
406 bool const ret = textHandleUndo(bv, *undo.get());
415 // makes sure the next operation will be stored
416 undo_finished = true;
422 // this is dangerous and for internal use only
429 // this is dangerous and for internal use only
434 bool textUndo(BufferView * bv)
436 return textUndoOrRedo(bv, bv->buffer()->undostack, bv->buffer()->redostack);
440 bool textRedo(BufferView * bv)
442 return textUndoOrRedo(bv, bv->buffer()->redostack, bv->buffer()->undostack);
446 void setUndo(BufferView * bv, Undo::undo_kind kind,
447 ParagraphList::iterator first, ParagraphList::iterator behind)
451 if (createUndo(bv, kind, first, behind, u))
452 bv->buffer()->undostack.push(u);
453 bv->buffer()->redostack.clear();
458 void setRedo(BufferView * bv, Undo::undo_kind kind,
459 ParagraphList::iterator first, ParagraphList::iterator behind)
462 if (createUndo(bv, kind, first, behind, u))
463 bv->buffer()->redostack.push(u);
467 void setCursorParUndo(BufferView * bv)
469 setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
470 boost::next(bv->text->cursor.par()));