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);
40 first = firstUndoParagraph(bv, undo->number_of_inset_id);
42 bv->buffer()->redostack.push(
43 createUndo(bv, undo->kind, first,
44 bv->buffer()->getParFromID(undo->number_of_behind_par)));
48 return textHandleUndo(bv, undo);
52 bool textRedo(BufferView * bv)
54 // returns false if no redo possible
55 Undo * undo = bv->buffer()->redostack.top();
56 bv->buffer()->redostack.pop();
60 Paragraph * first = bv->buffer()->getParFromID(undo->number_of_before_par);
62 first = firstUndoParagraph(bv, undo->number_of_inset_id);
64 bv->buffer()->undostack.push(
65 createUndo(bv, undo->kind, first,
66 bv->buffer()->getParFromID(undo->number_of_behind_par)));
70 return textHandleUndo(bv, undo);
74 bool textHandleUndo(BufferView * bv, Undo * undo)
76 // returns false if no undo possible
80 bv->buffer()->getParFromID(undo->number_of_before_par);
82 bv->buffer()->getParFromID(undo->number_of_behind_par);
88 // if there's no before take the beginning
89 // of the document for redoing
91 LyXText * t = bv->text;
92 int num = undo->number_of_inset_id;
93 if (undo->number_of_inset_id >= 0) {
94 Inset * in = bv->buffer()->getInsetFromID(num);
96 t = in->getLyXText(bv);
101 t->setCursorIntern(bv, firstUndoParagraph(bv, num), 0);
104 // replace the paragraphs with the undo informations
106 Paragraph * tmppar3 = undo->par;
107 undo->par = 0; // otherwise the undo destructor would
108 // delete the paragraph
111 Paragraph * tmppar4 = tmppar3;
113 while (tmppar4->next())
114 tmppar4 = tmppar4->next();
117 // now remove the old text if there is any
118 if (before != behind || (!behind && !before)) {
120 tmppar5 = before->next();
122 tmppar5 = firstUndoParagraph(bv, undo->number_of_inset_id);
124 while (tmppar5 && tmppar5 != behind) {
126 tmppar5 = tmppar5->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();
139 // put the new stuff in the list if there is one
142 before->next(tmppar3);
144 #warning Juergen, why is this needed?? (JMarc)
145 // since tmppar3 is not yet inserted in the document, I do not see why
146 // the getParFromID which is done by the function below makes sense.
147 // OTOH, since you wrote the method just for this instance, I guess you
148 // have something in mind
150 bv->text->ownerParagraph(tmppar3->id(),
153 // in this case, since getParFromID is not called, the program does
154 // not crash on trying to access buffer()->paragraph, which does not
155 // exist anymore if we undid the first par f the document. (JMarc)
156 bv->text->ownerParagraph(tmppar3);
159 tmppar3->previous(before);
161 // Do we really enter here ??? (Jug)
163 bv->text->ownerParagraph(behind);
166 tmppar4->next(behind);
168 behind->previous(tmppar4);
172 // Set the cursor for redoing
174 bv->text->setCursorIntern(bv, before, 0);
177 // calculate the endpar for redoing the paragraphs.
179 endpar = behind->next();
183 tmppar = bv->buffer()->getParFromID(undo->number_of_cursor_par);
184 UpdatableInset* it = static_cast<UpdatableInset*>(tmppar3->inInset());
186 it->getLyXText(bv)->redoParagraphs(bv, it->getLyXText(bv)->cursor,
189 it->getLyXText(bv)->setCursorIntern(bv, tmppar, undo->cursor_pos);
190 it->getLyXText(bv)->updateCounters(bv, it->getLyXText(bv)->cursor.row());
193 it->update(bv, font, false);
194 #ifdef THIS_DOES_NOT_WORK
195 // we need this anyway as also if the undo was
196 // inside an inset we have to redo the
197 // paragraph breaking
198 bv->text->redoParagraphs(bv, bv->text->cursor,
199 bv->text->cursor.par());
202 bv->text->redoParagraphs(bv, bv->text->cursor, endpar);
204 bv->text->setCursorIntern(bv, tmppar, undo->cursor_pos);
205 bv->text->updateCounters(bv, bv->text->cursor.row());
212 bv->text->status(bv, LyXText::NEED_MORE_REFRESH);
219 // makes sure the next operation will be stored
220 undo_finished = true;
226 // this is dangerous and for internal use only
233 // this is dangerous and for internal use only
238 void setUndo(BufferView * bv, Undo::undo_kind kind,
239 Paragraph const * first, Paragraph const * behind)
242 bv->buffer()->undostack.push(createUndo(bv, kind, first, behind));
243 bv->buffer()->redostack.clear();
247 void setRedo(BufferView * bv, Undo::undo_kind kind,
248 Paragraph const * first, Paragraph const * behind)
250 bv->buffer()->redostack.push(createUndo(bv, kind, first, behind));
254 Undo * createUndo(BufferView * bv, Undo::undo_kind kind,
255 Paragraph const * first, Paragraph const * behind)
259 int before_number = -1;
260 int behind_number = -1;
263 if (first->previous())
264 before_number = first->previous()->id();
266 behind_number = behind->id();
267 if (first->inInset())
268 inset_id = first->inInset()->id();
270 // Undo::EDIT and Undo::FINISH are
271 // always finished. (no overlapping there)
272 // overlapping only with insert and delete inside one paragraph:
273 // Nobody wants all removed character
274 // appear one by one when undoing.
275 // EDIT is special since only layout information, not the
276 // contents of a paragaph are stored.
277 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)){
278 // check wether storing is needed
279 if (!bv->buffer()->undostack.empty() &&
280 bv->buffer()->undostack.top()->kind == kind &&
281 bv->buffer()->undostack.top()->number_of_before_par == before_number &&
282 bv->buffer()->undostack.top()->number_of_behind_par == behind_number) {
290 Paragraph * start = const_cast<Paragraph *>(first);
294 end = const_cast<Paragraph*>(behind->previous());
300 if (start && end && (start != end->next()) &&
301 ((before_number != behind_number) ||
302 ((before_number < 0) && (behind_number < 0)))) {
303 Paragraph * tmppar = start;
304 Paragraph * tmppar2 = new Paragraph(*tmppar, true);
305 tmppar2->id(tmppar->id());
307 // a memory optimization: Just store the layout information
309 if (kind == Undo::EDIT) {
310 tmppar2->clearContents();
315 while (tmppar != end && tmppar->next()) {
316 tmppar = tmppar->next();
317 tmppar2->next(new Paragraph(*tmppar, false));
318 tmppar2->next()->id(tmppar->id());
319 // a memory optimization: Just store the layout
320 // information when only edit
321 if (kind == Undo::EDIT) {
322 tmppar2->clearContents();
324 tmppar2->next()->previous(tmppar2);
325 tmppar2 = tmppar2->next();
329 undopar = 0; // nothing to replace (undo of delete maybe)
331 int cursor_par = undoCursor(bv).par()->id();
332 int cursor_pos = undoCursor(bv).pos();
334 Undo * undo = new Undo(kind, inset_id,
335 before_number, behind_number,
336 cursor_par, cursor_pos, undopar);
338 undo_finished = false;
343 void setCursorParUndo(BufferView * bv)
345 setUndo(bv, Undo::FINISH,
346 bv->text->cursor.par(),
347 bv->text->cursor.par()->next());
351 Paragraph * firstUndoParagraph(BufferView * bv, int inset_id)
353 Inset * inset = bv->buffer()->getInsetFromID(inset_id);
355 Paragraph * result = inset->firstParagraph();
359 return bv->text->ownerParagraph();
363 LyXCursor const & undoCursor(BufferView * bv)
365 if (bv->theLockingInset())
366 return bv->theLockingInset()->cursor(bv);
367 return bv->text->cursor;