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 /// the flag used by FinishUndo();
30 bool textUndo(BufferView * bv)
32 // returns false if no undo possible
33 Undo * undo = bv->buffer()->undostack.top();
34 bv->buffer()->undostack.pop();
38 Paragraph * first = bv->buffer()->getParFromID(undo->number_of_before_par);
39 if (first && first->next())
40 first = first->next();
42 first = firstUndoParagraph(bv, undo->number_of_inset_id);
44 bv->buffer()->redostack.push(
45 createUndo(bv, undo->kind, first,
46 bv->buffer()->getParFromID(undo->number_of_behind_par)));
50 // now we can unlock the inset for saftey because the inset pointer could
51 // be changed during the undo-function. Anyway if needed we have to lock
52 // the right inset/position if this is requested.
54 bv->unlockInset(bv->theLockingInset());
55 bool ret = textHandleUndo(bv, undo);
61 bool textRedo(BufferView * bv)
63 // returns false if no redo possible
64 Undo * undo = bv->buffer()->redostack.top();
65 bv->buffer()->redostack.pop();
69 Paragraph * first = bv->buffer()->getParFromID(undo->number_of_before_par);
70 if (first && first->next())
71 first = first->next();
73 first = firstUndoParagraph(bv, undo->number_of_inset_id);
75 bv->buffer()->undostack.push(
76 createUndo(bv, undo->kind, first,
77 bv->buffer()->getParFromID(undo->number_of_behind_par)));
81 // now we can unlock the inset for saftey because the inset pointer could
82 // be changed during the undo-function. Anyway if needed we have to lock
83 // the right inset/position if this is requested.
85 bv->unlockInset(bv->theLockingInset());
86 bool ret = textHandleUndo(bv, undo);
92 bool textHandleUndo(BufferView * bv, Undo * undo)
94 // returns false if no undo possible
98 bv->buffer()->getParFromID(undo->number_of_before_par);
100 bv->buffer()->getParFromID(undo->number_of_behind_par);
105 // if there's no before take the beginning
106 // of the document for redoing
108 LyXText * t = bv->text;
109 int num = undo->number_of_inset_id;
110 if (undo->number_of_inset_id >= 0) {
111 Inset * in = bv->buffer()->getInsetFromID(num);
113 t = in->getLyXText(bv);
118 t->setCursorIntern(bv, firstUndoParagraph(bv, num), 0);
121 // replace the paragraphs with the undo informations
123 Paragraph * tmppar3 = undo->par;
124 undo->par = 0; // otherwise the undo destructor would
125 // delete the paragraph
128 Paragraph * tmppar4 = tmppar3;
130 while (tmppar4->next())
131 tmppar4 = tmppar4->next();
134 // now remove the old text if there is any
135 if (before != behind || (!behind && !before)) {
137 tmppar5 = before->next();
139 tmppar5 = firstUndoParagraph(bv, undo->number_of_inset_id);
141 while (tmppar5 && tmppar5 != behind) {
143 tmppar5 = tmppar5->next();
144 // a memory optimization for edit:
145 // Only layout information
146 // is stored in the undo. So restore
147 // the text informations.
148 if (undo->kind == Undo::EDIT) {
149 tmppar2->setContentsFromPar(tmppar);
150 tmppar->clearContents();
151 tmppar2 = tmppar2->next();
156 // put the new stuff in the list if there is one
159 before->next(tmppar3);
161 bv->text->ownerParagraph(tmppar3->id(),
164 tmppar3->previous(before);
166 // Do we really enter here ??? (Jug)
168 bv->text->ownerParagraph(behind);
171 tmppar4->next(behind);
173 behind->previous(tmppar4);
177 // Set the cursor for redoing
179 Inset * it = before->inInset();
181 it->getLyXText(bv)->setCursorIntern(bv, before, 0);
183 bv->text->setCursorIntern(bv, before, 0);
186 Paragraph * endpar = 0;
187 // calculate the endpar for redoing the paragraphs.
189 endpar = behind->next();
191 tmppar = bv->buffer()->getParFromID(undo->number_of_cursor_par);
192 UpdatableInset* it = static_cast<UpdatableInset*>(tmppar3->inInset());
194 it->getLyXText(bv)->redoParagraphs(bv, it->getLyXText(bv)->cursor,
197 it->update(bv, font, false);
198 // we now would have to rebreak the whole paragraph the undo-par
199 // was in. How we do it here is not really true. We would have to
200 // save this information in the undo-struct and then we could do
201 // the right rebreak. Here we only handle the case where this
202 // was in the actual paragraph, which not always is true.
203 bv->text->redoParagraphs(bv, bv->text->cursor,
204 bv->text->cursor.par());
206 it = static_cast<UpdatableInset*>(tmppar->inInset());
208 it->getLyXText(bv)->setCursorIntern(bv, tmppar, undo->cursor_pos);
209 it->getLyXText(bv)->updateCounters(bv, it->getLyXText(bv)->cursor.row());
211 bv->text->setCursorIntern(bv, bv->text->cursor.par(),
212 bv->text->cursor.pos());
214 bv->text->redoParagraphs(bv, bv->text->cursor, endpar);
217 Inset * it = tmppar->inInset();
219 t = it->getLyXText(bv);
222 t->setCursorIntern(bv, tmppar, undo->cursor_pos);
223 t->updateCounters(bv, t->cursor.row());
230 bv->text->status(bv, LyXText::NEED_MORE_REFRESH);
237 // makes sure the next operation will be stored
238 undo_finished = true;
244 // this is dangerous and for internal use only
251 // this is dangerous and for internal use only
256 void setUndo(BufferView * bv, Undo::undo_kind kind,
257 Paragraph const * first, Paragraph const * behind)
260 bv->buffer()->undostack.push(createUndo(bv, kind, first, behind));
261 bv->buffer()->redostack.clear();
266 void setRedo(BufferView * bv, Undo::undo_kind kind,
267 Paragraph const * first, Paragraph const * behind)
269 bv->buffer()->redostack.push(createUndo(bv, kind, first, behind));
273 Undo * createUndo(BufferView * bv, Undo::undo_kind kind,
274 Paragraph const * first, Paragraph const * behind)
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 // Undo::EDIT and Undo::FINISH are
290 // always finished. (no overlapping there)
291 // overlapping only with insert and delete inside one paragraph:
292 // Nobody wants all removed character
293 // appear one by one when undoing.
294 // EDIT is special since only layout information, not the
295 // contents of a paragaph are stored.
296 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)){
297 // check wether storing is needed
298 if (!bv->buffer()->undostack.empty() &&
299 bv->buffer()->undostack.top()->kind == kind &&
300 bv->buffer()->undostack.top()->number_of_before_par == before_number &&
301 bv->buffer()->undostack.top()->number_of_behind_par == behind_number) {
309 Paragraph * start = const_cast<Paragraph *>(first);
313 end = const_cast<Paragraph*>(behind->previous());
319 if (start && end && (start != end->next()) &&
320 ((before_number != behind_number) ||
321 ((before_number < 0) && (behind_number < 0))))
323 Paragraph * tmppar = start;
324 Paragraph * tmppar2 = new Paragraph(*tmppar, true);
326 // a memory optimization: Just store the layout information
328 if (kind == Undo::EDIT) {
329 tmppar2->clearContents();
334 while (tmppar != end && tmppar->next()) {
335 tmppar = tmppar->next();
336 tmppar2->next(new Paragraph(*tmppar, true));
337 // a memory optimization: Just store the layout
338 // information when only edit
339 if (kind == Undo::EDIT) {
340 tmppar2->clearContents();
342 tmppar2->next()->previous(tmppar2);
343 tmppar2 = tmppar2->next();
347 undopar = 0; // nothing to replace (undo of delete maybe)
349 int cursor_par = undoCursor(bv).par()->id();
350 int cursor_pos = undoCursor(bv).pos();
352 Undo * undo = new Undo(kind, inset_id,
353 before_number, behind_number,
354 cursor_par, cursor_pos, undopar);
356 undo_finished = false;
361 void setCursorParUndo(BufferView * bv)
363 setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
364 bv->text->cursor.par()->next());
368 Paragraph * firstUndoParagraph(BufferView * bv, int inset_id)
370 Inset * inset = bv->buffer()->getInsetFromID(inset_id);
372 Paragraph * result = inset->getFirstParagraph(0);
376 return bv->text->ownerParagraph();
380 LyXCursor const & undoCursor(BufferView * bv)
382 if (bv->theLockingInset())
383 return bv->theLockingInset()->cursor(bv);
384 return bv->text->cursor;