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);
196 } else { // otherwise this is the first one and we start here
197 Inset * it = undopar->inInset();
199 it->getLyXText(bv)->setCursorIntern(bv, undopar, 0);
201 bv->text->setCursorIntern(bv, undopar, 0);
204 Paragraph * endpar = 0;
205 // calculate the endpar for redoing the paragraphs.
207 endpar = behind->next();
209 tmppar = bv->buffer()->getParFromID(undo.number_of_cursor_par);
210 UpdatableInset* it = 0;
212 it = static_cast<UpdatableInset*>(undopar->inInset());
214 it->getLyXText(bv)->redoParagraphs(bv,
215 it->getLyXText(bv)->cursor,
218 it = static_cast<UpdatableInset*>(tmppar->inInset());
222 t = it->getLyXText(bv);
226 t->setCursorIntern(bv, tmppar, undo.cursor_pos);
227 // clear any selection and set the selection cursor
228 // for an evt. new selection.
230 t->selection.cursor = t->cursor;
231 t->updateCounters(bv);
234 bv->updateInset(it, false);
235 bv->text->setCursorIntern(bv, bv->text->cursor.par(),
236 bv->text->cursor.pos());
238 bv->text->redoParagraphs(bv, bv->text->cursor, endpar);
241 Inset * it = tmppar->inInset();
244 t = it->getLyXText(bv);
248 t->setCursorIntern(bv, tmppar, undo.cursor_pos);
249 // clear any selection and set the selection cursor
250 // for an evt. new selection.
252 t->selection.cursor = t->cursor;
253 t->updateCounters(bv);
257 // And here it's safe enough to delete all removed paragraphs
258 vector<Paragraph *>::iterator pit = deletelist.begin();
259 if (pit != deletelist.end()) {
260 for(;pit != deletelist.end(); ++pit) {
268 bv->text->status(bv, LyXText::NEED_MORE_REFRESH);
273 bool createUndo(BufferView * bv, Undo::undo_kind kind,
274 Paragraph const * first, Paragraph const * behind, shared_ptr<Undo> & u)
278 int before_number = -1;
279 int behind_number = -1;
282 if (first->previous())
283 before_number = first->previous()->id();
285 behind_number = behind->id();
286 if (first->inInset())
287 inset_id = first->inInset()->id();
289 Buffer * b = bv->buffer();
291 // Undo::EDIT and Undo::FINISH are
292 // always finished. (no overlapping there)
293 // overlapping only with insert and delete inside one paragraph:
294 // Nobody wants all removed character
295 // appear one by one when undoing.
296 // EDIT is special since only layout information, not the
297 // contents of a paragaph are stored.
298 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
299 // check whether storing is needed
300 if (!b->undostack.empty() &&
301 b->undostack.top()->kind == kind &&
302 b->undostack.top()->number_of_before_par == before_number &&
303 b->undostack.top()->number_of_behind_par == behind_number) {
312 Paragraph * start = const_cast<Paragraph *>(first);
316 end = const_cast<Paragraph*>(behind->previous());
322 if (start && end && (start != end->next()) &&
323 ((before_number != behind_number) ||
324 ((before_number < 0) && (behind_number < 0))))
326 Paragraph * tmppar = start;
327 Paragraph * tmppar2 = new Paragraph(*tmppar, true);
329 // a memory optimization: Just store the layout information
331 if (kind == Undo::EDIT) {
332 tmppar2->clearContents();
337 while (tmppar != end && tmppar->next()) {
338 tmppar = tmppar->next();
339 tmppar2->next(new Paragraph(*tmppar, true));
340 // a memory optimization: Just store the layout
341 // information when only edit
342 if (kind == Undo::EDIT) {
343 tmppar2->clearContents();
345 tmppar2->next()->previous(tmppar2);
347 tmppar2 = tmppar2->next();
351 undopar = 0; // nothing to replace (undo of delete maybe)
353 int cursor_par = undoCursor(bv).par()->id();
354 int cursor_pos = undoCursor(bv).pos();
356 u.reset(new Undo(kind, inset_id,
357 before_number, behind_number,
358 cursor_par, cursor_pos, undopar));
360 undo_finished = false;
368 // makes sure the next operation will be stored
369 undo_finished = true;
375 // this is dangerous and for internal use only
382 // this is dangerous and for internal use only
387 // returns false if no undo possible
388 bool textUndo(BufferView * bv)
390 Buffer * b = bv->buffer();
392 if (b->undostack.empty()) {
397 shared_ptr<Undo> undo = b->undostack.top();
402 Paragraph * first = b->getParFromID(undo->number_of_before_par);
403 if (first && first->next())
404 first = first->next();
406 first = firstUndoParagraph(bv, undo->number_of_inset_id);
409 if (createUndo(bv, undo->kind, first,
410 b->getParFromID(undo->number_of_behind_par), u))
411 b->redostack.push(u);
415 // now we can unlock the inset for saftey because the inset pointer could
416 // be changed during the undo-function. Anyway if needed we have to lock
417 // the right inset/position if this is requested.
419 bv->unlockInset(bv->theLockingInset());
420 bool const ret = textHandleUndo(bv, *undo.get());
426 // returns false if no redo possible
427 bool textRedo(BufferView * bv)
429 Buffer * b = bv->buffer();
431 if (b->redostack.empty()) {
436 shared_ptr<Undo> undo = b->redostack.top();
441 Paragraph * first = bv->buffer()->getParFromID(undo->number_of_before_par);
442 if (first && first->next())
443 first = first->next();
445 first = firstUndoParagraph(bv, undo->number_of_inset_id);
448 if (createUndo(bv, undo->kind, first,
449 bv->buffer()->getParFromID(undo->number_of_behind_par), u))
450 bv->buffer()->undostack.push(u);
454 // now we can unlock the inset for saftey because the inset pointer could
455 // be changed during the undo-function. Anyway if needed we have to lock
456 // the right inset/position if this is requested.
458 bv->unlockInset(bv->theLockingInset());
459 bool ret = textHandleUndo(bv, *undo.get());
465 void setUndo(BufferView * bv, Undo::undo_kind kind,
466 Paragraph const * first, Paragraph const * behind)
470 if (createUndo(bv, kind, first, behind, u))
471 bv->buffer()->undostack.push(u);
472 bv->buffer()->redostack.clear();
477 void setRedo(BufferView * bv, Undo::undo_kind kind,
478 Paragraph const * first, Paragraph const * behind)
481 if (createUndo(bv, kind, first, behind, u))
482 bv->buffer()->redostack.push(u);
486 void setCursorParUndo(BufferView * bv)
488 setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
489 bv->text->cursor.par()->next());