]> git.lyx.org Git - lyx.git/blob - src/undo_funcs.C
add warning make controllers compile and remove some alomost unused functions, commen...
[lyx.git] / src / undo_funcs.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995-2001 The LyX Team.
7  *
8  * ====================================================== */
9
10 #include <config.h>
11
12 #include "undo_funcs.h"
13 #include "lyxtext.h"
14 #include "funcrequest.h"
15 #include "BufferView.h"
16 #include "buffer.h"
17 #include "insets/updatableinset.h"
18 #include "insets/insettext.h"
19 #include "debug.h"
20 #include "support/LAssert.h"
21 #include "iterators.h"
22
23 #include <vector>
24
25 using std::vector;
26 using boost::shared_ptr;
27
28
29 /// The flag used by FinishUndo().
30 bool undo_finished;
31 /// Whether actions are not added to the undo stacks.
32 bool undo_frozen;
33
34 namespace {
35
36 /// Utility to return the cursor.
37 LyXCursor const & undoCursor(BufferView * bv)
38 {
39         if (bv->theLockingInset())
40                 return bv->theLockingInset()->cursor(bv);
41         return bv->text->cursor;
42 }
43
44
45 /**
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.
49  */
50 ParagraphList undoParagraphs(BufferView * bv, int inset_id)
51 {
52         Inset * inset = bv->buffer()->getInsetFromID(inset_id);
53         if (inset) {
54                 ParagraphList * result = inset->getParagraphs(0);
55                 if (result && !result->empty())
56                         return *result;
57         }
58         return bv->text->ownerParagraphs();
59 }
60
61
62 /**
63  * Finish the undo operation in the case there was no entry
64  * on the stack to perform.
65  */
66 void finishNoUndo(BufferView * bv)
67 {
68         freezeUndo();
69         bv->unlockInset(bv->theLockingInset());
70         finishUndo();
71         bv->text->postPaint(0);
72         unFreezeUndo();
73 }
74
75
76 // Returns false if no undo possible.
77 bool textHandleUndo(BufferView * bv, Undo & undo)
78 {
79         Buffer * b = bv->buffer();
80
81         ParIterator const before = b->getParFromID(undo.number_of_before_par);
82         ParIterator const behind = b->getParFromID(undo.number_of_behind_par);
83         ParIterator const end    = b->par_iterator_end();
84
85         // If there's no before take the beginning
86         // of the document for redoing.
87         if (before == end) {
88                 LyXText * t = bv->text;
89                 int num = undo.number_of_inset_id;
90                 if (undo.number_of_inset_id >= 0) {
91                         Inset * in = bv->buffer()->getInsetFromID(num);
92                         if (in) {
93                                 t = in->getLyXText(bv);
94                         } else {
95                                 num = -1;
96                         }
97                 }
98                 t->setCursorIntern(undoParagraphs(bv, num).begin(), 0);
99         }
100
101         // Set the right(new) inset-owner of the paragraph if there is any.
102         if (!undo.pars.empty()) {
103                 Inset * in = 0;
104                 if (before != end)
105                         in = before->inInset();
106                 else if (undo.number_of_inset_id >= 0)
107                         in = bv->buffer()->getInsetFromID(undo.number_of_inset_id);
108                 for (size_t i = 0, n = undo.pars.size(); i < n; ++i)
109                         undo.pars[i]->setInsetOwner(in);
110         }
111
112         // Replace the paragraphs with the undo informations.
113         vector<Paragraph *> deletelist;
114
115         // Now add old paragraphs to be deleted.
116         if (before != behind || (behind == end && before == end)) {
117                 ParagraphList::iterator deletepar;
118                 if (before != end) {
119                         deletepar = *before;
120                         ++deletepar;
121                 } else {
122                         deletepar = undoParagraphs(bv, undo.number_of_inset_id).begin();
123                 }
124                 // this surprisingly fills the undo! (Andre')
125                 size_t par = 0;
126                 //while (deletepar && deletepar != *behind)
127                 while (deletepar != *behind) {
128                         deletelist.push_back(&*deletepar);
129                         ++deletepar;
130
131                         // A memory optimization for edit:
132                         // Only layout information
133                         // is stored in the undo. So restore
134                         // the text informations.
135                         if (undo.kind == Undo::EDIT) {
136                                 undo.pars[par]->setContentsFromPar(*deletelist.back());
137                                 ++par;
138                         }
139                 }
140         }
141
142         // The order here is VERY IMPORTANT. We have to set the right
143         // next/prev pointer in the paragraphs so that a rebuild of
144         // the LyXText works!!!
145
146         // Thread the end of the undo onto the par in front if any.
147         if (!undo.pars.empty()) {
148                 undo.pars.back()->next(&**behind);
149                 if (behind != end)
150                         (&**behind)->previous(undo.pars.back());
151         }
152
153         // Put the new stuff in the list if there is one.
154         Paragraph * undopar = undo.pars.empty() ? 0 : undo.pars.front();
155         if (!undo.pars.empty()) {
156                 undo.pars.front()->previous(&**before);
157                 if (before != end)
158                         (&**before)->next(undopar);
159                 else {
160                         int id = undoParagraphs(bv, undo.number_of_inset_id).front().id();
161                         ParIterator op = bv->buffer()->getParFromID(id);
162                         if (op != end && op->inInset()) {
163 #warning FIXME reimplementaion needed here
164                                 //static_cast<InsetText*>(op->inInset())->paragraph(undopar);
165                         } else {
166 #warning FIXME reimplementation needed here
167                                 //bv->buffer()->paragraphs.set(undopar);
168                         }
169                 }
170         } else {
171                 // We enter here on DELETE undo operations where we
172                 // have to substitue the second paragraph with the
173                 // first if the removed one is the first.
174                 if (before == end && behind != end) {
175                         int id = undoParagraphs(bv, undo.number_of_inset_id).front().id();
176                         ParIterator op = bv->buffer()->getParFromID(id);
177                         if (op != end && op->inInset()) {
178 #warning FIXME reimplementation needed here
179                                 //static_cast<InsetText*>(op->inInset())->paragraph(&**behind);
180                         } else {
181 #warning FIXME reimplementation needed here
182                                 //bv->buffer()->paragraphs.set(&**behind);
183                         }
184                         undopar = &**behind;
185                 }
186         }
187
188
189         // Set the cursor for redoing.
190         // If we have a par before the undopar.
191         if (before != end) {
192                 Inset * it = before->inInset();
193                 if (it)
194                         it->getLyXText(bv)->setCursorIntern(*before, 0);
195                 else
196                         bv->text->setCursorIntern(*before, 0);
197         }
198
199 // we are not ready for this we cannot set the cursor for a paragraph
200 // which is not already in a row of LyXText!!!
201 #if 0
202         else { // otherwise this is the first one and we start here
203                 Inset * it = undopar->inInset();
204                 if (it)
205                         it->getLyXText(bv)->setCursorIntern(bv, undopar, 0);
206                 else
207                         bv->text->setCursorIntern(bv, undopar, 0);
208         }
209 #endif
210
211         UpdatableInset * it = 0;
212         if (undopar)
213                 it = static_cast<UpdatableInset*>(undopar->inInset());
214
215         LyXText * text = it ? it->getLyXText(bv) : bv->text;
216
217         ParagraphList::iterator endpar = text->ownerParagraphs().end();
218
219         // Calculate the endpar for redoing the paragraphs.
220         if (behind != end) {
221                 endpar = *behind;
222                 ++endpar;
223         }
224
225         text->redoParagraphs(text->cursor, endpar);
226         ParIterator tmppar =
227                 bv->buffer()->getParFromID(undo.number_of_cursor_par);
228
229         if (tmppar != end) {
230                 LyXText * t;
231                 Inset * it = tmppar->inInset();
232                 if (it) {
233                         FuncRequest cmd(bv, LFUN_INSET_EDIT, "left");
234                         it->localDispatch(cmd);
235                         t = it->getLyXText(bv);
236                 } else {
237                         t = bv->text;
238                 }
239                 t->setCursorIntern(*tmppar, undo.cursor_pos);
240                 // Clear any selection and set the selection
241                 // cursor for an evt. new selection.
242                 t->clearSelection();
243                 t->selection.cursor = t->cursor;
244                 t->updateCounters();
245         }
246
247         if (it) {
248                 bv->fitCursor();
249                 bv->updateInset(it);
250                 bv->text->setCursorIntern(bv->text->cursor.par(),
251                                           bv->text->cursor.pos());
252         }
253
254         // And here it's safe enough to delete all removed paragraphs.
255         vector<Paragraph *>::iterator pit = deletelist.begin();
256         for(; pit != deletelist.end(); ++pit) {
257                 (*pit)->previous(0);
258                 (*pit)->next(0);
259                 delete (*pit);
260         }
261
262         // Otherwise the undo destructor would delete the paragraphs
263         undo.pars.resize(0);
264
265         finishUndo();
266         bv->text->postPaint(0);
267         return true;
268 }
269
270
271 bool createUndo(BufferView * bv, Undo::undo_kind kind,
272         ParagraphList::iterator itfirst, ParagraphList::iterator itbehind,
273         shared_ptr<Undo> & u)
274 {
275         Paragraph * const first = &*itfirst;
276         Paragraph * const behind = &*itbehind;
277         lyx::Assert(first);
278
279         int before_number = -1;
280         int behind_number = -1;
281         int inset_id = -1;
282
283         if (first->previous())
284                 before_number = first->previous()->id();
285         if (behind)
286                 behind_number = behind->id();
287         if (first->inInset())
288                 inset_id = first->inInset()->id();
289
290         Buffer * b = bv->buffer();
291
292         // Undo::EDIT  and Undo::FINISH are
293         // always finished. (no overlapping there)
294         // overlapping only with insert and delete inside one paragraph:
295         // Nobody wants all removed  character
296         // appear one by one when undoing.
297         // EDIT is special since only layout information, not the
298         // contents of a paragaph are stored.
299         if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
300                 // Check whether storing is needed.
301                 if (!b->undostack.empty() &&
302                     b->undostack.top()->kind == kind &&
303                     b->undostack.top()->number_of_before_par == before_number &&
304                     b->undostack.top()->number_of_behind_par == behind_number) {
305                         // No undo needed.
306                         return false;
307                 }
308         }
309
310         // Create a new Undo.
311         std::vector<Paragraph *> undo_pars;
312
313         Paragraph const * end = 0;
314
315         if (behind)
316                 end = behind->previous();
317         else {
318                 end = first;
319                 while (end->next())
320                         end = end->next();
321         }
322
323         if (first && end && (first != end->next()) &&
324             ((before_number != behind_number) ||
325                  ((before_number < 0) && (behind_number < 0))))
326         {
327                 undo_pars.push_back(new Paragraph(*first, true));
328                 for (Paragraph * tmppar = first; tmppar != end && tmppar->next(); ) {
329                         tmppar = tmppar->next();
330                         undo_pars.push_back(new Paragraph(*tmppar, true));
331                         size_t const n = undo_pars.size();
332                         undo_pars[n - 2]->next(undo_pars[n - 1]);
333                         undo_pars[n - 1]->previous(undo_pars[n - 2]);
334                 }
335                 undo_pars.back()->next(0);
336         }
337
338         // A memory optimization: Just store the layout
339         // information when only edit.
340         if (kind == Undo::EDIT) {
341                 for (size_t i = 0, n = undo_pars.size(); i < n; ++i)
342                         undo_pars[i]->clearContents();
343         }
344
345         int cursor_par = undoCursor(bv).par()->id();
346         int cursor_pos = undoCursor(bv).pos();
347
348         //lyxerr << "createUndo: inset_id: " << inset_id << "  before_number: "
349         //      << before_number << "  behind_number: " << behind_number << "\n";
350         u.reset(new Undo(kind, inset_id,
351                 before_number, behind_number,
352                 cursor_par, cursor_pos, undo_pars));
353
354         undo_finished = false;
355         return true;
356 }
357
358
359 // Returns false if no undo possible.
360 bool textUndoOrRedo(BufferView * bv,
361         limited_stack<boost::shared_ptr<Undo> > & stack,
362         limited_stack<boost::shared_ptr<Undo> > & otherstack)
363 {
364         Buffer * b = bv->buffer();
365
366         if (stack.empty()) {
367                 finishNoUndo(bv);
368                 return false;
369         }
370
371         shared_ptr<Undo> undo = stack.top();
372         stack.pop();
373         finishUndo();
374
375         if (!undo_frozen) {
376 /*
377                 ParIterator p = b->getParFromID(undo->number_of_before_par);
378                 bool ok = false;
379                 ParagraphList::iterator first;
380                 // default constructed?
381                 ParIterator const end = b->par_iterator_end();
382                 if (p != end) {
383                         first = p.par();
384                         if (first->next())
385                                 first = first->next();
386                 } else
387                         first = undoParagraphs(bv, undo->number_of_inset_id).begin();
388                 if (first) {
389                         shared_ptr<Undo> u;
390                         ParIterator behind = b->getParFromID(undo->number_of_behind_par);
391                         if (createUndo(bv, undo->kind, first, behind.par(), u))
392                                 otherstack.push(u);
393                 }
394 */
395         }
396
397         // Now we can unlock the inset for saftey because the inset
398         // pointer could be changed during the undo-function. Anyway
399         // if needed we have to lock the right inset/position if this
400         // is requested.
401         freezeUndo();
402         bv->unlockInset(bv->theLockingInset());
403         bool const ret = textHandleUndo(bv, *undo.get());
404         unFreezeUndo();
405         return ret;
406 }
407
408 } // namespace anon
409
410
411 void finishUndo()
412 {
413         // Makes sure the next operation will be stored.
414         undo_finished = true;
415 }
416
417
418 void freezeUndo()
419 {
420         // This is dangerous and for internal use only.
421         undo_frozen = true;
422 }
423
424
425 void unFreezeUndo()
426 {
427         // This is dangerous and for internal use only.
428         undo_frozen = false;
429 }
430
431
432 bool textUndo(BufferView * bv)
433 {
434         return textUndoOrRedo(bv, bv->buffer()->undostack,
435                               bv->buffer()->redostack);
436 }
437
438
439 bool textRedo(BufferView * bv)
440 {
441         return textUndoOrRedo(bv, bv->buffer()->redostack,
442                               bv->buffer()->undostack);
443 }
444
445
446 void setUndo(BufferView * bv, Undo::undo_kind kind,
447              ParagraphList::iterator first, ParagraphList::iterator behind)
448 {
449         if (!undo_frozen) {
450                 shared_ptr<Undo> u;
451                 if (createUndo(bv, kind, first, behind, u))
452                         bv->buffer()->undostack.push(u);
453                 bv->buffer()->redostack.clear();
454         }
455 }
456
457
458 void setRedo(BufferView * bv, Undo::undo_kind kind,
459              ParagraphList::iterator first, ParagraphList::iterator behind)
460 {
461         shared_ptr<Undo> u;
462         if (createUndo(bv, kind, first, behind, u))
463                 bv->buffer()->redostack.push(u);
464 }
465
466
467 void setCursorParUndo(BufferView * bv)
468 {
469         setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
470                 boost::next(bv->text->cursor.par()));
471 }