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();
34 /// whether actions are not added to the undo stacks
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 * undopar = 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 * lastundopar = undopar;
120 in = before->inInset();
121 else if (undo.number_of_inset_id >= 0)
122 in = bv->buffer()->getInsetFromID(undo.number_of_inset_id);
123 lastundopar->setInsetOwner(in);
124 while (lastundopar->next()) {
125 lastundopar = lastundopar->next();
126 lastundopar->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 // The order here is VERY IMPORTANT. We have to set the right
158 // next/prev pointer in the paragraphs so that a rebuild of
159 // the LyXText works!!!
161 // thread the end of the undo onto the par in front if any
163 lastundopar->next(behind);
165 behind->previous(lastundopar);
168 // put the new stuff in the list if there is one
170 undopar->previous(before);
172 before->next(undopar);
174 bv->text->ownerParagraph(firstUndoParagraph(bv, undo.number_of_inset_id)->id(),
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 bv->text->ownerParagraph(firstUndoParagraph(bv, undo.number_of_inset_id)->id(),
189 // Set the cursor for redoing
190 if (before) { // if we have a par before the undopar
191 Inset * it = before->inInset();
193 it->getLyXText(bv)->setCursorIntern(bv, before, 0);
195 bv->text->setCursorIntern(bv, before, 0);
197 // we are not ready for this we cannot set the cursor for a paragraph
198 // which is not already in a row of LyXText!!!
200 else { // otherwise this is the first one and we start here
201 Inset * it = undopar->inInset();
203 it->getLyXText(bv)->setCursorIntern(bv, undopar, 0);
205 bv->text->setCursorIntern(bv, undopar, 0);
209 Paragraph * endpar = 0;
210 // calculate the endpar for redoing the paragraphs.
212 endpar = behind->next();
214 tmppar = bv->buffer()->getParFromID(undo.number_of_cursor_par);
215 UpdatableInset* it = 0;
217 it = static_cast<UpdatableInset*>(undopar->inInset());
219 it->getLyXText(bv)->redoParagraphs(bv,
220 it->getLyXText(bv)->cursor,
223 it = static_cast<UpdatableInset*>(tmppar->inInset());
227 t = it->getLyXText(bv);
231 t->setCursorIntern(bv, tmppar, undo.cursor_pos);
232 // clear any selection and set the selection cursor
233 // for an evt. new selection.
235 t->selection.cursor = t->cursor;
236 t->updateCounters(bv);
239 bv->updateInset(it, false);
240 bv->text->setCursorIntern(bv, bv->text->cursor.par(),
241 bv->text->cursor.pos());
243 bv->text->redoParagraphs(bv, bv->text->cursor, endpar);
246 Inset * it = tmppar->inInset();
249 t = it->getLyXText(bv);
253 t->setCursorIntern(bv, tmppar, undo.cursor_pos);
254 // clear any selection and set the selection cursor
255 // for an evt. new selection.
257 t->selection.cursor = t->cursor;
258 t->updateCounters(bv);
262 // And here it's safe enough to delete all removed paragraphs
263 vector<Paragraph *>::iterator pit = deletelist.begin();
264 if (pit != deletelist.end()) {
265 for(;pit != deletelist.end(); ++pit) {
273 bv->text->status(bv, LyXText::NEED_MORE_REFRESH);
278 bool createUndo(BufferView * bv, Undo::undo_kind kind,
279 Paragraph const * first, Paragraph const * behind, shared_ptr<Undo> & u)
283 int before_number = -1;
284 int behind_number = -1;
287 if (first->previous())
288 before_number = first->previous()->id();
290 behind_number = behind->id();
291 if (first->inInset())
292 inset_id = first->inInset()->id();
294 Buffer * b = bv->buffer();
296 // Undo::EDIT and Undo::FINISH are
297 // always finished. (no overlapping there)
298 // overlapping only with insert and delete inside one paragraph:
299 // Nobody wants all removed character
300 // appear one by one when undoing.
301 // EDIT is special since only layout information, not the
302 // contents of a paragaph are stored.
303 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
304 // check whether storing is needed
305 if (!b->undostack.empty() &&
306 b->undostack.top()->kind == kind &&
307 b->undostack.top()->number_of_before_par == before_number &&
308 b->undostack.top()->number_of_behind_par == behind_number) {
317 Paragraph * start = const_cast<Paragraph *>(first);
321 end = const_cast<Paragraph*>(behind->previous());
327 if (start && end && (start != end->next()) &&
328 ((before_number != behind_number) ||
329 ((before_number < 0) && (behind_number < 0))))
331 Paragraph * tmppar = start;
332 Paragraph * tmppar2 = new Paragraph(*tmppar, true);
334 // a memory optimization: Just store the layout information
336 if (kind == Undo::EDIT) {
337 tmppar2->clearContents();
342 while (tmppar != end && tmppar->next()) {
343 tmppar = tmppar->next();
344 tmppar2->next(new Paragraph(*tmppar, true));
345 // a memory optimization: Just store the layout
346 // information when only edit
347 if (kind == Undo::EDIT) {
348 tmppar2->clearContents();
350 tmppar2->next()->previous(tmppar2);
352 tmppar2 = tmppar2->next();
356 undopar = 0; // nothing to replace (undo of delete maybe)
358 int cursor_par = undoCursor(bv).par()->id();
359 int cursor_pos = undoCursor(bv).pos();
361 u.reset(new Undo(kind, inset_id,
362 before_number, behind_number,
363 cursor_par, cursor_pos, undopar));
365 undo_finished = false;
373 // makes sure the next operation will be stored
374 undo_finished = true;
380 // this is dangerous and for internal use only
387 // this is dangerous and for internal use only
392 // returns false if no undo possible
393 bool textUndo(BufferView * bv)
395 Buffer * b = bv->buffer();
397 if (b->undostack.empty()) {
402 shared_ptr<Undo> undo = b->undostack.top();
407 Paragraph * first = b->getParFromID(undo->number_of_before_par);
408 if (first && first->next())
409 first = first->next();
411 first = firstUndoParagraph(bv, undo->number_of_inset_id);
414 if (createUndo(bv, undo->kind, first,
415 b->getParFromID(undo->number_of_behind_par), u))
416 b->redostack.push(u);
420 // now we can unlock the inset for saftey because the inset pointer could
421 // be changed during the undo-function. Anyway if needed we have to lock
422 // the right inset/position if this is requested.
424 bv->unlockInset(bv->theLockingInset());
425 bool const ret = textHandleUndo(bv, *undo.get());
431 // returns false if no redo possible
432 bool textRedo(BufferView * bv)
434 Buffer * b = bv->buffer();
436 if (b->redostack.empty()) {
441 shared_ptr<Undo> undo = b->redostack.top();
446 Paragraph * first = bv->buffer()->getParFromID(undo->number_of_before_par);
447 if (first && first->next())
448 first = first->next();
450 first = firstUndoParagraph(bv, undo->number_of_inset_id);
453 if (createUndo(bv, undo->kind, first,
454 bv->buffer()->getParFromID(undo->number_of_behind_par), u))
455 bv->buffer()->undostack.push(u);
459 // now we can unlock the inset for saftey because the inset pointer could
460 // be changed during the undo-function. Anyway if needed we have to lock
461 // the right inset/position if this is requested.
463 bv->unlockInset(bv->theLockingInset());
464 bool ret = textHandleUndo(bv, *undo.get());
470 void setUndo(BufferView * bv, Undo::undo_kind kind,
471 Paragraph const * first, Paragraph const * behind)
475 if (createUndo(bv, kind, first, behind, u))
476 bv->buffer()->undostack.push(u);
477 bv->buffer()->redostack.clear();
482 void setRedo(BufferView * bv, Undo::undo_kind kind,
483 Paragraph const * first, Paragraph const * behind)
486 if (createUndo(bv, kind, first, behind, u))
487 bv->buffer()->redostack.push(u);
491 void setCursorParUndo(BufferView * bv)
493 setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
494 bv->text->cursor.par()->next());