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;
46 * Returns a pointer to the very first Paragraph depending of where
47 * we are so it will return the first paragraph of the buffer or the
48 * first paragraph of the textinset we're in.
50 Paragraph * firstUndoParagraph(BufferView * bv, int inset_id)
52 Inset * inset = bv->buffer()->getInsetFromID(inset_id);
54 Paragraph * result = inset->getFirstParagraph(0);
58 return &*bv->text->ownerParagraphs().begin();
63 * Finish the undo operation in the case there was no entry
64 * on the stack to perform.
66 void finishNoUndo(BufferView * bv)
69 bv->unlockInset(bv->theLockingInset());
71 bv->text->postPaint(0);
76 // Returns false if no undo possible.
77 bool textHandleUndo(BufferView * bv, Undo & undo)
79 Buffer * b = bv->buffer();
81 Paragraph * const before = &*b->getParFromID(undo.number_of_before_par);
82 Paragraph * const behind = &*b->getParFromID(undo.number_of_behind_par);
84 // If there's no before take the beginning
85 // of the document for redoing.
87 LyXText * t = bv->text;
88 int num = undo.number_of_inset_id;
89 if (undo.number_of_inset_id >= 0) {
90 Inset * in = bv->buffer()->getInsetFromID(num);
92 t = in->getLyXText(bv);
97 t->setCursorIntern(firstUndoParagraph(bv, num), 0);
100 // Replace the paragraphs with the undo informations.
101 Paragraph * undopar = undo.pars.empty() ? 0 : undo.pars.front();
102 Paragraph * lastundopar = undo.pars.empty() ? 0 : undo.pars.back();
104 // Get last undo par and set the right(new) inset-owner of the
105 // paragraph if there is any. This is not needed if we don't
106 // have a paragraph before because then in is automatically
107 // done in the function which assigns the first paragraph to
108 // an InsetText. (Jug)
109 if (!undo.pars.empty()) {
112 in = before->inInset();
113 else if (undo.number_of_inset_id >= 0)
114 in = bv->buffer()->getInsetFromID(undo.number_of_inset_id);
115 for (size_t i = 0, n = undo.pars.size(); i < n; ++i)
116 undo.pars[i]->setInsetOwner(in);
119 // Otherwise the undo destructor would
120 // delete the paragraph.
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
176 // have to substitue the second paragraph with the
177 // first if the removed one is the first.
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 we have a par before the undopar.
195 Inset * it = before->inInset();
197 it->getLyXText(bv)->setCursorIntern(before, 0);
199 bv->text->setCursorIntern(before, 0);
202 // we are not ready for this we cannot set the cursor for a paragraph
203 // which is not already in a row of LyXText!!!
205 else { // otherwise this is the first one and we start here
206 Inset * it = undopar->inInset();
208 it->getLyXText(bv)->setCursorIntern(bv, undopar, 0);
210 bv->text->setCursorIntern(bv, undopar, 0);
214 Paragraph * endpar = 0;
216 // Calculate the endpar for redoing the paragraphs.
218 endpar = behind->next();
220 UpdatableInset * it = 0;
222 it = static_cast<UpdatableInset*>(undopar->inInset());
224 it->getLyXText(bv)->redoParagraphs(
225 it->getLyXText(bv)->cursor,
228 &*bv->buffer()->getParFromID(undo.number_of_cursor_par);
230 it = static_cast<UpdatableInset*>(tmppar->inInset());
234 t = it->getLyXText(bv);
238 t->setCursorIntern(tmppar, undo.cursor_pos);
239 // Clear any selection and set the selection
240 // cursor for an evt. new selection.
242 t->selection.cursor = t->cursor;
247 bv->text->setCursorIntern(bv->text->cursor.par(),
248 bv->text->cursor.pos());
250 bv->text->redoParagraphs(bv->text->cursor, endpar);
252 &*bv->buffer()->getParFromID(undo.number_of_cursor_par);
255 Inset * it = tmppar->inInset();
258 t = it->getLyXText(bv);
262 t->setCursorIntern(tmppar, undo.cursor_pos);
263 // Clear any selection and set the selection
264 // cursor for an evt. new selection.
266 t->selection.cursor = t->cursor;
271 // And here it's safe enough to delete all removed paragraphs.
272 vector<Paragraph *>::iterator pit = deletelist.begin();
273 for(; pit != deletelist.end(); ++pit) {
280 bv->text->postPaint(0);
285 bool createUndo(BufferView * bv, Undo::undo_kind kind,
286 ParagraphList::iterator itfirst, ParagraphList::iterator itbehind,
287 shared_ptr<Undo> & u)
289 Paragraph * const first = &*itfirst;
290 Paragraph * const behind = &*itbehind;
293 int before_number = -1;
294 int behind_number = -1;
297 if (first->previous())
298 before_number = first->previous()->id();
300 behind_number = behind->id();
301 if (first->inInset())
302 inset_id = first->inInset()->id();
304 Buffer * b = bv->buffer();
306 // Undo::EDIT and Undo::FINISH are
307 // always finished. (no overlapping there)
308 // overlapping only with insert and delete inside one paragraph:
309 // Nobody wants all removed character
310 // appear one by one when undoing.
311 // EDIT is special since only layout information, not the
312 // contents of a paragaph are stored.
313 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
314 // Check whether storing is needed.
315 if (!b->undostack.empty() &&
316 b->undostack.top()->kind == kind &&
317 b->undostack.top()->number_of_before_par == before_number &&
318 b->undostack.top()->number_of_behind_par == behind_number) {
324 // Create a new Undo.
325 std::vector<Paragraph *> undo_pars;
327 Paragraph const * end = 0;
330 end = behind->previous();
337 if (first && end && (first != end->next()) &&
338 ((before_number != behind_number) ||
339 ((before_number < 0) && (behind_number < 0))))
341 undo_pars.push_back(new Paragraph(*first, true));
342 for (Paragraph * tmppar = first; tmppar != end && tmppar->next(); ) {
343 tmppar = tmppar->next();
344 undo_pars.push_back(new Paragraph(*tmppar, true));
345 size_t const n = undo_pars.size();
346 undo_pars[n - 2]->next(undo_pars[n - 1]);
347 undo_pars[n - 1]->previous(undo_pars[n - 2]);
349 undo_pars.back()->next(0);
352 // A memory optimization: Just store the layout
353 // information when only edit.
354 if (kind == Undo::EDIT) {
355 for (size_t i = 0, n = undo_pars.size(); i < n; ++i)
356 undo_pars[i]->clearContents();
359 int cursor_par = undoCursor(bv).par()->id();
360 int cursor_pos = undoCursor(bv).pos();
362 //lyxerr << "createUndo: inset_id: " << inset_id << " before_number: "
363 // << before_number << " behind_number: " << behind_number << "\n";
364 u.reset(new Undo(kind, inset_id,
365 before_number, behind_number,
366 cursor_par, cursor_pos, undo_pars));
368 undo_finished = false;
373 // Returns false if no undo possible.
374 bool textUndoOrRedo(BufferView * bv,
375 limited_stack<boost::shared_ptr<Undo> > & stack,
376 limited_stack<boost::shared_ptr<Undo> > & otherstack)
378 Buffer * b = bv->buffer();
385 shared_ptr<Undo> undo = stack.top();
390 Paragraph * first = &*b->getParFromID(undo->number_of_before_par);
391 if (first && first->next())
392 first = first->next();
394 first = firstUndoParagraph(bv, undo->number_of_inset_id);
397 if (createUndo(bv, undo->kind, first,
398 b->getParFromID(undo->number_of_behind_par), u))
403 // Now we can unlock the inset for saftey because the inset
404 // pointer could be changed during the undo-function. Anyway
405 // if needed we have to lock the right inset/position if this
408 bv->unlockInset(bv->theLockingInset());
409 bool const ret = textHandleUndo(bv, *undo.get());
419 // Makes sure the next operation will be stored.
420 undo_finished = true;
426 // This is dangerous and for internal use only.
433 // This is dangerous and for internal use only.
438 bool textUndo(BufferView * bv)
440 return textUndoOrRedo(bv, bv->buffer()->undostack,
441 bv->buffer()->redostack);
445 bool textRedo(BufferView * bv)
447 return textUndoOrRedo(bv, bv->buffer()->redostack,
448 bv->buffer()->undostack);
452 void setUndo(BufferView * bv, Undo::undo_kind kind,
453 ParagraphList::iterator first, ParagraphList::iterator behind)
457 if (createUndo(bv, kind, first, behind, u))
458 bv->buffer()->undostack.push(u);
459 bv->buffer()->redostack.clear();
464 void setRedo(BufferView * bv, Undo::undo_kind kind,
465 ParagraphList::iterator first, ParagraphList::iterator behind)
468 if (createUndo(bv, kind, first, behind, u))
469 bv->buffer()->redostack.push(u);
473 void setCursorParUndo(BufferView * bv)
475 setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
476 boost::next(bv->text->cursor.par()));