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 // Set the right(new) inset-owner of the paragraph if there is any.
101 if (!undo.pars.empty()) {
104 in = before->inInset();
105 else if (undo.number_of_inset_id >= 0)
106 in = bv->buffer()->getInsetFromID(undo.number_of_inset_id);
107 for (size_t i = 0, n = undo.pars.size(); i < n; ++i)
108 undo.pars[i]->setInsetOwner(in);
111 // Replace the paragraphs with the undo informations.
112 vector<Paragraph *> deletelist;
114 // Now add old paragraphs to be deleted.
115 if (before != behind || (!behind && !before)) {
116 Paragraph * deletepar;
118 deletepar = before->next();
120 deletepar = firstUndoParagraph(bv, undo.number_of_inset_id);
121 Paragraph * tmppar2 = undo.pars.front();
122 while (deletepar && deletepar != behind) {
123 deletelist.push_back(deletepar);
124 Paragraph * tmppar = deletepar;
125 deletepar = deletepar->next();
127 // A memory optimization for edit:
128 // Only layout information
129 // is stored in the undo. So restore
130 // the text informations.
131 if (undo.kind == Undo::EDIT) {
132 tmppar2->setContentsFromPar(*tmppar);
133 tmppar2 = tmppar2->next();
138 // The order here is VERY IMPORTANT. We have to set the right
139 // next/prev pointer in the paragraphs so that a rebuild of
140 // the LyXText works!!!
142 // Thread the end of the undo onto the par in front if any.
143 if (!undo.pars.empty()) {
144 undo.pars.back()->next(behind);
146 behind->previous(undo.pars.back());
149 // Put the new stuff in the list if there is one.
150 Paragraph * undopar = undo.pars.empty() ? 0 : undo.pars.front();
152 undopar->previous(before);
154 before->next(undopar);
156 int id = firstUndoParagraph(bv, undo.number_of_inset_id)->id();
157 Paragraph * op = &*bv->buffer()->getParFromID(id);
158 if (op && op->inInset()) {
159 static_cast<InsetText*>(op->inInset())->paragraph(undopar);
161 bv->buffer()->paragraphs.set(undopar);
165 // We enter here on DELETE undo operations where we
166 // have to substitue the second paragraph with the
167 // first if the removed one is the first.
168 if (!before && behind) {
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(behind);
174 bv->buffer()->paragraphs.set(behind);
182 // Set the cursor for redoing.
183 // If we have a par before the undopar.
185 Inset * it = before->inInset();
187 it->getLyXText(bv)->setCursorIntern(before, 0);
189 bv->text->setCursorIntern(before, 0);
192 // we are not ready for this we cannot set the cursor for a paragraph
193 // which is not already in a row of LyXText!!!
195 else { // otherwise this is the first one and we start here
196 Inset * it = undopar->inInset();
198 it->getLyXText(bv)->setCursorIntern(bv, undopar, 0);
200 bv->text->setCursorIntern(bv, undopar, 0);
204 Paragraph * endpar = 0;
206 // Calculate the endpar for redoing the paragraphs.
208 endpar = behind->next();
210 UpdatableInset * it = 0;
212 it = static_cast<UpdatableInset*>(undopar->inInset());
214 it->getLyXText(bv)->redoParagraphs(
215 it->getLyXText(bv)->cursor,
218 &*bv->buffer()->getParFromID(undo.number_of_cursor_par);
220 it = static_cast<UpdatableInset*>(tmppar->inInset());
224 t = it->getLyXText(bv);
228 t->setCursorIntern(tmppar, undo.cursor_pos);
229 // Clear any selection and set the selection
230 // cursor for an evt. new selection.
232 t->selection.cursor = t->cursor;
237 bv->text->setCursorIntern(bv->text->cursor.par(),
238 bv->text->cursor.pos());
240 bv->text->redoParagraphs(bv->text->cursor, endpar);
242 &*bv->buffer()->getParFromID(undo.number_of_cursor_par);
245 Inset * it = tmppar->inInset();
248 t = it->getLyXText(bv);
252 t->setCursorIntern(tmppar, undo.cursor_pos);
253 // Clear any selection and set the selection
254 // cursor for an evt. new selection.
256 t->selection.cursor = t->cursor;
261 // And here it's safe enough to delete all removed paragraphs.
262 vector<Paragraph *>::iterator pit = deletelist.begin();
263 for(; pit != deletelist.end(); ++pit) {
269 // Otherwise the undo destructor would delete the paragraphs
273 bv->text->postPaint(0);
278 bool createUndo(BufferView * bv, Undo::undo_kind kind,
279 ParagraphList::iterator itfirst, ParagraphList::iterator itbehind,
280 shared_ptr<Undo> & u)
282 Paragraph * const first = &*itfirst;
283 Paragraph * const behind = &*itbehind;
286 int before_number = -1;
287 int behind_number = -1;
290 if (first->previous())
291 before_number = first->previous()->id();
293 behind_number = behind->id();
294 if (first->inInset())
295 inset_id = first->inInset()->id();
297 Buffer * b = bv->buffer();
299 // Undo::EDIT and Undo::FINISH are
300 // always finished. (no overlapping there)
301 // overlapping only with insert and delete inside one paragraph:
302 // Nobody wants all removed character
303 // appear one by one when undoing.
304 // EDIT is special since only layout information, not the
305 // contents of a paragaph are stored.
306 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
307 // Check whether storing is needed.
308 if (!b->undostack.empty() &&
309 b->undostack.top()->kind == kind &&
310 b->undostack.top()->number_of_before_par == before_number &&
311 b->undostack.top()->number_of_behind_par == behind_number) {
317 // Create a new Undo.
318 std::vector<Paragraph *> undo_pars;
320 Paragraph const * end = 0;
323 end = behind->previous();
330 if (first && end && (first != end->next()) &&
331 ((before_number != behind_number) ||
332 ((before_number < 0) && (behind_number < 0))))
334 undo_pars.push_back(new Paragraph(*first, true));
335 for (Paragraph * tmppar = first; tmppar != end && tmppar->next(); ) {
336 tmppar = tmppar->next();
337 undo_pars.push_back(new Paragraph(*tmppar, true));
338 size_t const n = undo_pars.size();
339 undo_pars[n - 2]->next(undo_pars[n - 1]);
340 undo_pars[n - 1]->previous(undo_pars[n - 2]);
342 undo_pars.back()->next(0);
345 // A memory optimization: Just store the layout
346 // information when only edit.
347 if (kind == Undo::EDIT) {
348 for (size_t i = 0, n = undo_pars.size(); i < n; ++i)
349 undo_pars[i]->clearContents();
352 int cursor_par = undoCursor(bv).par()->id();
353 int cursor_pos = undoCursor(bv).pos();
355 //lyxerr << "createUndo: inset_id: " << inset_id << " before_number: "
356 // << before_number << " behind_number: " << behind_number << "\n";
357 u.reset(new Undo(kind, inset_id,
358 before_number, behind_number,
359 cursor_par, cursor_pos, undo_pars));
361 undo_finished = false;
366 // Returns false if no undo possible.
367 bool textUndoOrRedo(BufferView * bv,
368 limited_stack<boost::shared_ptr<Undo> > & stack,
369 limited_stack<boost::shared_ptr<Undo> > & otherstack)
371 Buffer * b = bv->buffer();
378 shared_ptr<Undo> undo = stack.top();
383 Paragraph * first = &*b->getParFromID(undo->number_of_before_par);
384 if (first && first->next())
385 first = first->next();
387 first = firstUndoParagraph(bv, undo->number_of_inset_id);
390 if (createUndo(bv, undo->kind, first,
391 b->getParFromID(undo->number_of_behind_par), u))
396 // Now we can unlock the inset for saftey because the inset
397 // pointer could be changed during the undo-function. Anyway
398 // if needed we have to lock the right inset/position if this
401 bv->unlockInset(bv->theLockingInset());
402 bool const ret = textHandleUndo(bv, *undo.get());
412 // Makes sure the next operation will be stored.
413 undo_finished = true;
419 // This is dangerous and for internal use only.
426 // This is dangerous and for internal use only.
431 bool textUndo(BufferView * bv)
433 return textUndoOrRedo(bv, bv->buffer()->undostack,
434 bv->buffer()->redostack);
438 bool textRedo(BufferView * bv)
440 return textUndoOrRedo(bv, bv->buffer()->redostack,
441 bv->buffer()->undostack);
445 void setUndo(BufferView * bv, Undo::undo_kind kind,
446 ParagraphList::iterator first, ParagraphList::iterator behind)
450 if (createUndo(bv, kind, first, behind, u))
451 bv->buffer()->undostack.push(u);
452 bv->buffer()->redostack.clear();
457 void setRedo(BufferView * bv, Undo::undo_kind kind,
458 ParagraphList::iterator first, ParagraphList::iterator behind)
461 if (createUndo(bv, kind, first, behind, u))
462 bv->buffer()->redostack.push(u);
466 void setCursorParUndo(BufferView * bv)
468 setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
469 boost::next(bv->text->cursor.par()));