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.
102 Paragraph * undopar = undo.pars.empty() ? 0 : undo.pars[0];
103 // Otherwise the undo destructor would
104 // delete the paragraph.
107 // Get last undo par and set the right(new) inset-owner of the
108 // paragraph if there is any. This is not needed if we don't
109 // have a paragraph before because then in is automatically
110 // done in the function which assigns the first paragraph to
111 // an InsetText. (Jug)
112 Paragraph * lastundopar = undopar;
116 in = before->inInset();
117 else if (undo.number_of_inset_id >= 0)
118 in = bv->buffer()->getInsetFromID(undo.number_of_inset_id);
119 lastundopar->setInsetOwner(in);
120 while (lastundopar->next()) {
121 lastundopar = lastundopar->next();
122 lastundopar->setInsetOwner(in);
126 vector<Paragraph *> deletelist;
128 // Now add old paragraphs to be deleted.
129 if (before != behind || (!behind && !before)) {
130 Paragraph * deletepar;
132 deletepar = before->next();
134 deletepar = firstUndoParagraph(bv, undo.number_of_inset_id);
135 Paragraph * tmppar2 = undopar;
136 while (deletepar && deletepar != behind) {
137 deletelist.push_back(deletepar);
138 Paragraph * tmppar = deletepar;
139 deletepar = deletepar->next();
141 // A memory optimization for edit:
142 // Only layout information
143 // is stored in the undo. So restore
144 // the text informations.
145 if (undo.kind == Undo::EDIT) {
146 tmppar2->setContentsFromPar(*tmppar);
147 tmppar2 = tmppar2->next();
152 // The order here is VERY IMPORTANT. We have to set the right
153 // next/prev pointer in the paragraphs so that a rebuild of
154 // the LyXText works!!!
156 // Thread the end of the undo onto the par in front if any.
158 lastundopar->next(behind);
160 behind->previous(lastundopar);
163 // Put the new stuff in the list if there is one.
165 undopar->previous(before);
167 before->next(undopar);
169 int id = firstUndoParagraph(bv, undo.number_of_inset_id)->id();
170 Paragraph * op = &*bv->buffer()->getParFromID(id);
171 if (op && op->inInset()) {
172 static_cast<InsetText*>(op->inInset())->paragraph(undopar);
174 bv->buffer()->paragraphs.set(undopar);
178 // We enter here on DELETE undo operations where we
179 // have to substitue the second paragraph with the
180 // first if the removed one is the first.
181 if (!before && behind) {
182 int id = firstUndoParagraph(bv, undo.number_of_inset_id)->id();
183 Paragraph * op = &*bv->buffer()->getParFromID(id);
184 if (op && op->inInset()) {
185 static_cast<InsetText*>(op->inInset())->paragraph(behind);
187 bv->buffer()->paragraphs.set(behind);
195 // Set the cursor for redoing.
196 // If we have a par before the undopar.
198 Inset * it = before->inInset();
200 it->getLyXText(bv)->setCursorIntern(before, 0);
202 bv->text->setCursorIntern(before, 0);
205 // we are not ready for this we cannot set the cursor for a paragraph
206 // which is not already in a row of LyXText!!!
208 else { // otherwise this is the first one and we start here
209 Inset * it = undopar->inInset();
211 it->getLyXText(bv)->setCursorIntern(bv, undopar, 0);
213 bv->text->setCursorIntern(bv, undopar, 0);
217 Paragraph * endpar = 0;
219 // Calculate the endpar for redoing the paragraphs.
221 endpar = behind->next();
223 UpdatableInset * it = 0;
225 it = static_cast<UpdatableInset*>(undopar->inInset());
227 it->getLyXText(bv)->redoParagraphs(
228 it->getLyXText(bv)->cursor,
231 &*bv->buffer()->getParFromID(undo.number_of_cursor_par);
233 it = static_cast<UpdatableInset*>(tmppar->inInset());
237 t = it->getLyXText(bv);
241 t->setCursorIntern(tmppar, undo.cursor_pos);
242 // Clear any selection and set the selection
243 // cursor for an evt. new selection.
245 t->selection.cursor = t->cursor;
250 bv->text->setCursorIntern(bv->text->cursor.par(),
251 bv->text->cursor.pos());
253 bv->text->redoParagraphs(bv->text->cursor, endpar);
255 &*bv->buffer()->getParFromID(undo.number_of_cursor_par);
258 Inset * it = tmppar->inInset();
261 t = it->getLyXText(bv);
265 t->setCursorIntern(tmppar, undo.cursor_pos);
266 // Clear any selection and set the selection
267 // cursor for an evt. new selection.
269 t->selection.cursor = t->cursor;
274 // And here it's safe enough to delete all removed paragraphs.
275 vector<Paragraph *>::iterator pit = deletelist.begin();
276 for(; pit != deletelist.end(); ++pit) {
283 bv->text->postPaint(0);
288 bool createUndo(BufferView * bv, Undo::undo_kind kind,
289 ParagraphList::iterator itfirst, ParagraphList::iterator itbehind,
290 shared_ptr<Undo> & u)
292 Paragraph * const first = &*itfirst;
293 Paragraph * const behind = &*itbehind;
296 int before_number = -1;
297 int behind_number = -1;
300 if (first->previous())
301 before_number = first->previous()->id();
303 behind_number = behind->id();
304 if (first->inInset())
305 inset_id = first->inInset()->id();
307 Buffer * b = bv->buffer();
309 // Undo::EDIT and Undo::FINISH are
310 // always finished. (no overlapping there)
311 // overlapping only with insert and delete inside one paragraph:
312 // Nobody wants all removed character
313 // appear one by one when undoing.
314 // EDIT is special since only layout information, not the
315 // contents of a paragaph are stored.
316 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
317 // Check whether storing is needed.
318 if (!b->undostack.empty() &&
319 b->undostack.top()->kind == kind &&
320 b->undostack.top()->number_of_before_par == before_number &&
321 b->undostack.top()->number_of_behind_par == behind_number) {
327 // Create a new Undo.
328 std::vector<Paragraph *> undo_pars;
330 Paragraph const * end = 0;
333 end = behind->previous();
340 if (first && end && (first != end->next()) &&
341 ((before_number != behind_number) ||
342 ((before_number < 0) && (behind_number < 0))))
344 undo_pars.push_back(new Paragraph(*first, true));
345 for (Paragraph * tmppar = first; tmppar != end && tmppar->next(); ) {
346 tmppar = tmppar->next();
347 undo_pars.push_back(new Paragraph(*tmppar, true));
348 size_t const n = undo_pars.size();
349 undo_pars[n - 2]->next(undo_pars[n - 1]);
350 undo_pars[n - 1]->previous(undo_pars[n - 2]);
352 undo_pars.back()->next(0);
355 // A memory optimization: Just store the layout
356 // information when only edit.
357 if (kind == Undo::EDIT) {
358 for (size_t i = 0, n = undo_pars.size(); i < n; ++i)
359 undo_pars[i]->clearContents();
362 int cursor_par = undoCursor(bv).par()->id();
363 int cursor_pos = undoCursor(bv).pos();
365 //lyxerr << "createUndo: inset_id: " << inset_id << " before_number: "
366 // << before_number << " behind_number: " << behind_number << "\n";
367 u.reset(new Undo(kind, inset_id,
368 before_number, behind_number,
369 cursor_par, cursor_pos, undo_pars));
371 undo_finished = false;
376 // Returns false if no undo possible.
377 bool textUndoOrRedo(BufferView * bv,
378 limited_stack<boost::shared_ptr<Undo> > & stack,
379 limited_stack<boost::shared_ptr<Undo> > & otherstack)
381 Buffer * b = bv->buffer();
388 shared_ptr<Undo> undo = stack.top();
393 Paragraph * first = &*b->getParFromID(undo->number_of_before_par);
394 if (first && first->next())
395 first = first->next();
397 first = firstUndoParagraph(bv, undo->number_of_inset_id);
400 if (createUndo(bv, undo->kind, first,
401 b->getParFromID(undo->number_of_behind_par), u))
406 // Now we can unlock the inset for saftey because the inset
407 // pointer could be changed during the undo-function. Anyway
408 // if needed we have to lock the right inset/position if this
411 bv->unlockInset(bv->theLockingInset());
412 bool const ret = textHandleUndo(bv, *undo.get());
422 // Makes sure the next operation will be stored.
423 undo_finished = true;
429 // This is dangerous and for internal use only.
436 // This is dangerous and for internal use only.
441 bool textUndo(BufferView * bv)
443 return textUndoOrRedo(bv, bv->buffer()->undostack,
444 bv->buffer()->redostack);
448 bool textRedo(BufferView * bv)
450 return textUndoOrRedo(bv, bv->buffer()->redostack,
451 bv->buffer()->undostack);
455 void setUndo(BufferView * bv, Undo::undo_kind kind,
456 ParagraphList::iterator first, ParagraphList::iterator behind)
460 if (createUndo(bv, kind, first, behind, u))
461 bv->buffer()->undostack.push(u);
462 bv->buffer()->redostack.clear();
467 void setRedo(BufferView * bv, Undo::undo_kind kind,
468 ParagraphList::iterator first, ParagraphList::iterator behind)
471 if (createUndo(bv, kind, first, behind, u))
472 bv->buffer()->redostack.push(u);
476 void setCursorParUndo(BufferView * bv)
478 setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
479 boost::next(bv->text->cursor.par()));