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 tmppar->clearContents();
134 tmppar2 = tmppar2->next();
139 // put the new stuff in the list if there is one
142 before->next(tmppar3);
144 bv->text->ownerParagraph(tmppar3->id(),
147 tmppar3->previous(before);
149 // Do we really enter here ??? (Jug)
151 bv->text->ownerParagraph(behind);
154 tmppar4->next(behind);
156 behind->previous(tmppar4);
160 // Set the cursor for redoing
162 bv->text->setCursorIntern(bv, before, 0);
165 // calculate the endpar for redoing the paragraphs.
167 endpar = behind->next();
171 tmppar = bv->buffer()->getParFromID(undo->number_of_cursor_par);
172 UpdatableInset* it = static_cast<UpdatableInset*>(tmppar3->inInset());
174 it->getLyXText(bv)->redoParagraphs(bv, it->getLyXText(bv)->cursor,
177 it->getLyXText(bv)->setCursorIntern(bv, tmppar, undo->cursor_pos);
178 it->getLyXText(bv)->updateCounters(bv, it->getLyXText(bv)->cursor.row());
181 it->update(bv, font, false);
182 #ifdef THIS_DOES_NOT_WORK
183 // we need this anyway as also if the undo was
184 // inside an inset we have to redo the
185 // paragraph breaking
186 bv->text->redoParagraphs(bv, bv->text->cursor,
187 bv->text->cursor.par());
190 bv->text->redoParagraphs(bv, bv->text->cursor, endpar);
192 bv->text->setCursorIntern(bv, tmppar, undo->cursor_pos);
193 bv->text->updateCounters(bv, bv->text->cursor.row());
200 bv->text->status(bv, LyXText::NEED_MORE_REFRESH);
207 // makes sure the next operation will be stored
208 undo_finished = true;
214 // this is dangerous and for internal use only
221 // this is dangerous and for internal use only
226 void setUndo(BufferView * bv, Undo::undo_kind kind,
227 Paragraph const * first, Paragraph const * behind)
230 bv->buffer()->undostack.push(createUndo(bv, kind, first, behind));
231 bv->buffer()->redostack.clear();
235 void setRedo(BufferView * bv, Undo::undo_kind kind,
236 Paragraph const * first, Paragraph const * behind)
238 bv->buffer()->redostack.push(createUndo(bv, kind, first, behind));
242 Undo * createUndo(BufferView * bv, Undo::undo_kind kind,
243 Paragraph const * first, Paragraph const * behind)
247 int before_number = -1;
248 int behind_number = -1;
251 if (first->previous())
252 before_number = first->previous()->id();
254 behind_number = behind->id();
255 if (first->inInset())
256 inset_id = first->inInset()->id();
258 // Undo::EDIT and Undo::FINISH are
259 // always finished. (no overlapping there)
260 // overlapping only with insert and delete inside one paragraph:
261 // Nobody wants all removed character
262 // appear one by one when undoing.
263 // EDIT is special since only layout information, not the
264 // contents of a paragaph are stored.
265 if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)){
266 // check wether storing is needed
267 if (!bv->buffer()->undostack.empty() &&
268 bv->buffer()->undostack.top()->kind == kind &&
269 bv->buffer()->undostack.top()->number_of_before_par == before_number &&
270 bv->buffer()->undostack.top()->number_of_behind_par == behind_number) {
278 Paragraph * start = const_cast<Paragraph *>(first);
282 end = const_cast<Paragraph*>(behind->previous());
288 if (start && end && (start != end->next()) &&
289 ((before_number != behind_number) ||
290 ((before_number < 0) && (behind_number < 0)))) {
291 Paragraph * tmppar = start;
292 Paragraph * tmppar2 = new Paragraph(*tmppar, true);
293 tmppar2->id(tmppar->id());
295 // a memory optimization: Just store the layout information
297 if (kind == Undo::EDIT) {
298 tmppar2->clearContents();
303 while (tmppar != end && tmppar->next()) {
304 tmppar = tmppar->next();
305 tmppar2->next(new Paragraph(*tmppar, false));
306 tmppar2->next()->id(tmppar->id());
307 // a memory optimization: Just store the layout
308 // information when only edit
309 if (kind == Undo::EDIT) {
310 tmppar2->clearContents();
312 tmppar2->next()->previous(tmppar2);
313 tmppar2 = tmppar2->next();
317 undopar = 0; // nothing to replace (undo of delete maybe)
319 int cursor_par = undoCursor(bv).par()->id();
320 int cursor_pos = undoCursor(bv).pos();
322 Undo * undo = new Undo(kind, inset_id,
323 before_number, behind_number,
324 cursor_par, cursor_pos, undopar);
326 undo_finished = false;
331 void setCursorParUndo(BufferView * bv)
333 setUndo(bv, Undo::FINISH,
334 bv->text->cursor.par(),
335 bv->text->cursor.par()->next());
339 Paragraph * firstUndoParagraph(BufferView * bv, int inset_id)
341 Inset * inset = bv->buffer()->getInsetFromID(inset_id);
343 Paragraph * result = inset->firstParagraph();
347 return bv->text->ownerParagraph();
351 LyXCursor const & undoCursor(BufferView * bv)
353 if (bv->theLockingInset())
354 return bv->theLockingInset()->cursor(bv);
355 return bv->text->cursor;