2001-07-06 15:57:54 +00:00
|
|
|
/* This file is part of
|
2002-03-21 17:27:08 +00:00
|
|
|
* ======================================================
|
|
|
|
*
|
2001-07-06 15:57:54 +00:00
|
|
|
* LyX, The Document Processor
|
2002-03-21 17:27:08 +00:00
|
|
|
*
|
2001-07-06 15:57:54 +00:00
|
|
|
* Copyright 1995-2001 The LyX Team.
|
|
|
|
*
|
|
|
|
* ====================================================== */
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "undo_funcs.h"
|
|
|
|
#include "lyxtext.h"
|
|
|
|
#include "BufferView.h"
|
|
|
|
#include "buffer.h"
|
2003-02-20 17:39:48 +00:00
|
|
|
#include "insets/updatableinset.h"
|
2003-04-16 00:02:38 +00:00
|
|
|
#include "insets/insettext.h"
|
2001-07-06 15:57:54 +00:00
|
|
|
#include "debug.h"
|
2001-07-29 15:34:18 +00:00
|
|
|
#include "support/LAssert.h"
|
2001-07-06 15:57:54 +00:00
|
|
|
|
2001-12-19 08:59:15 +00:00
|
|
|
#include "iterators.h"
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2002-02-16 15:59:55 +00:00
|
|
|
using std::vector;
|
2002-05-26 19:40:17 +00:00
|
|
|
using boost::shared_ptr;
|
2002-02-16 15:59:55 +00:00
|
|
|
|
|
|
|
|
2003-05-01 23:24:30 +00:00
|
|
|
/// The flag used by FinishUndo().
|
2001-07-06 15:57:54 +00:00
|
|
|
bool undo_finished;
|
2003-05-01 23:24:30 +00:00
|
|
|
/// Whether actions are not added to the undo stacks.
|
2001-07-06 15:57:54 +00:00
|
|
|
bool undo_frozen;
|
|
|
|
|
2002-05-26 19:40:17 +00:00
|
|
|
namespace {
|
2001-12-13 17:19:53 +00:00
|
|
|
|
2003-05-01 23:24:30 +00:00
|
|
|
/// Utility to return the cursor.
|
2002-05-26 19:40:17 +00:00
|
|
|
LyXCursor const & undoCursor(BufferView * bv)
|
2001-07-06 15:57:54 +00:00
|
|
|
{
|
2002-05-26 19:40:17 +00:00
|
|
|
if (bv->theLockingInset())
|
|
|
|
return bv->theLockingInset()->cursor(bv);
|
|
|
|
return bv->text->cursor;
|
|
|
|
}
|
2002-06-24 20:28:12 +00:00
|
|
|
|
2003-05-01 23:24:30 +00:00
|
|
|
|
2002-05-26 19:40:17 +00:00
|
|
|
/**
|
2003-05-01 23:24:30 +00:00
|
|
|
* Returns a pointer to the very first Paragraph depending of where
|
|
|
|
* we are so it will return the first paragraph of the buffer or the
|
|
|
|
* first paragraph of the textinset we're in.
|
2002-05-26 19:40:17 +00:00
|
|
|
*/
|
|
|
|
Paragraph * firstUndoParagraph(BufferView * bv, int inset_id)
|
|
|
|
{
|
|
|
|
Inset * inset = bv->buffer()->getInsetFromID(inset_id);
|
|
|
|
if (inset) {
|
|
|
|
Paragraph * result = inset->getFirstParagraph(0);
|
|
|
|
if (result)
|
|
|
|
return result;
|
2001-07-06 15:57:54 +00:00
|
|
|
}
|
2003-04-02 17:11:38 +00:00
|
|
|
return &*bv->text->ownerParagraphs().begin();
|
2001-07-06 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-26 19:40:17 +00:00
|
|
|
/**
|
|
|
|
* Finish the undo operation in the case there was no entry
|
|
|
|
* on the stack to perform.
|
|
|
|
*/
|
|
|
|
void finishNoUndo(BufferView * bv)
|
2001-07-06 15:57:54 +00:00
|
|
|
{
|
2001-12-14 11:55:58 +00:00
|
|
|
freezeUndo();
|
|
|
|
bv->unlockInset(bv->theLockingInset());
|
2002-05-26 19:40:17 +00:00
|
|
|
finishUndo();
|
2003-03-17 16:25:00 +00:00
|
|
|
bv->text->postPaint(0);
|
2001-12-14 11:55:58 +00:00
|
|
|
unFreezeUndo();
|
2001-07-06 15:57:54 +00:00
|
|
|
}
|
2002-06-24 20:28:12 +00:00
|
|
|
|
|
|
|
|
2003-05-01 23:24:30 +00:00
|
|
|
// Returns false if no undo possible.
|
2002-05-26 19:40:17 +00:00
|
|
|
bool textHandleUndo(BufferView * bv, Undo & undo)
|
2001-07-06 15:57:54 +00:00
|
|
|
{
|
2002-05-26 19:40:17 +00:00
|
|
|
Buffer * b = bv->buffer();
|
2002-06-24 20:28:12 +00:00
|
|
|
|
2003-04-29 15:24:37 +00:00
|
|
|
Paragraph * const before = &*b->getParFromID(undo.number_of_before_par);
|
|
|
|
Paragraph * const behind = &*b->getParFromID(undo.number_of_behind_par);
|
2002-05-26 19:40:17 +00:00
|
|
|
|
2003-05-01 23:24:30 +00:00
|
|
|
// If there's no before take the beginning
|
|
|
|
// of the document for redoing.
|
2002-05-26 19:40:17 +00:00
|
|
|
if (!before) {
|
|
|
|
LyXText * t = bv->text;
|
|
|
|
int num = undo.number_of_inset_id;
|
|
|
|
if (undo.number_of_inset_id >= 0) {
|
|
|
|
Inset * in = bv->buffer()->getInsetFromID(num);
|
|
|
|
if (in) {
|
|
|
|
t = in->getLyXText(bv);
|
|
|
|
} else {
|
|
|
|
num = -1;
|
2001-07-06 15:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
2003-03-17 16:25:00 +00:00
|
|
|
t->setCursorIntern(firstUndoParagraph(bv, num), 0);
|
2002-05-26 19:40:17 +00:00
|
|
|
}
|
2001-07-06 15:57:54 +00:00
|
|
|
|
2003-05-01 23:24:30 +00:00
|
|
|
// Replace the paragraphs with the undo informations.
|
2002-05-26 19:40:17 +00:00
|
|
|
|
2003-05-02 13:11:39 +00:00
|
|
|
Paragraph * undopar = undo.pars.empty() ? 0 : undo.pars[0];
|
2003-05-01 23:24:30 +00:00
|
|
|
// Otherwise the undo destructor would
|
|
|
|
// delete the paragraph.
|
2003-05-02 13:11:39 +00:00
|
|
|
undo.pars.resize(0);
|
2003-05-01 23:24:30 +00:00
|
|
|
|
|
|
|
// Get last undo par and set the right(new) inset-owner of the
|
|
|
|
// paragraph if there is any. This is not needed if we don't
|
|
|
|
// have a paragraph before because then in is automatically
|
|
|
|
// done in the function which assigns the first paragraph to
|
|
|
|
// an InsetText. (Jug)
|
2002-08-24 22:02:30 +00:00
|
|
|
Paragraph * lastundopar = undopar;
|
|
|
|
if (lastundopar) {
|
2002-05-26 19:40:17 +00:00
|
|
|
Inset * in = 0;
|
|
|
|
if (before)
|
|
|
|
in = before->inInset();
|
|
|
|
else if (undo.number_of_inset_id >= 0)
|
|
|
|
in = bv->buffer()->getInsetFromID(undo.number_of_inset_id);
|
2002-08-24 22:02:30 +00:00
|
|
|
lastundopar->setInsetOwner(in);
|
|
|
|
while (lastundopar->next()) {
|
|
|
|
lastundopar = lastundopar->next();
|
|
|
|
lastundopar->setInsetOwner(in);
|
2001-12-19 08:59:15 +00:00
|
|
|
}
|
2002-05-26 19:40:17 +00:00
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2002-05-26 19:40:17 +00:00
|
|
|
vector<Paragraph *> deletelist;
|
2002-06-24 20:28:12 +00:00
|
|
|
|
2003-05-01 23:24:30 +00:00
|
|
|
// Now add old paragraphs to be deleted.
|
2002-05-26 19:40:17 +00:00
|
|
|
if (before != behind || (!behind && !before)) {
|
2003-04-29 15:24:37 +00:00
|
|
|
Paragraph * deletepar;
|
2002-05-26 19:40:17 +00:00
|
|
|
if (before)
|
|
|
|
deletepar = before->next();
|
|
|
|
else
|
|
|
|
deletepar = firstUndoParagraph(bv, undo.number_of_inset_id);
|
2003-04-29 15:24:37 +00:00
|
|
|
Paragraph * tmppar2 = undopar;
|
2002-05-26 19:40:17 +00:00
|
|
|
while (deletepar && deletepar != behind) {
|
|
|
|
deletelist.push_back(deletepar);
|
2003-04-29 15:24:37 +00:00
|
|
|
Paragraph * tmppar = deletepar;
|
2002-05-26 19:40:17 +00:00
|
|
|
deletepar = deletepar->next();
|
2002-06-24 20:28:12 +00:00
|
|
|
|
2003-05-01 23:24:30 +00:00
|
|
|
// A memory optimization for edit:
|
2002-05-26 19:40:17 +00:00
|
|
|
// Only layout information
|
|
|
|
// is stored in the undo. So restore
|
|
|
|
// the text informations.
|
|
|
|
if (undo.kind == Undo::EDIT) {
|
2003-04-29 10:39:08 +00:00
|
|
|
tmppar2->setContentsFromPar(*tmppar);
|
2002-05-26 19:40:17 +00:00
|
|
|
tmppar2 = tmppar2->next();
|
2001-07-06 15:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
2002-05-26 19:40:17 +00:00
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2002-10-14 14:59:50 +00:00
|
|
|
// The order here is VERY IMPORTANT. We have to set the right
|
|
|
|
// next/prev pointer in the paragraphs so that a rebuild of
|
|
|
|
// the LyXText works!!!
|
|
|
|
|
2003-05-01 23:24:30 +00:00
|
|
|
// Thread the end of the undo onto the par in front if any.
|
2002-10-14 14:59:50 +00:00
|
|
|
if (lastundopar) {
|
|
|
|
lastundopar->next(behind);
|
|
|
|
if (behind)
|
|
|
|
behind->previous(lastundopar);
|
|
|
|
}
|
|
|
|
|
2003-05-01 23:24:30 +00:00
|
|
|
// Put the new stuff in the list if there is one.
|
2002-08-24 22:02:30 +00:00
|
|
|
if (undopar) {
|
2002-10-14 14:59:50 +00:00
|
|
|
undopar->previous(before);
|
2002-05-26 19:40:17 +00:00
|
|
|
if (before)
|
2002-08-24 22:02:30 +00:00
|
|
|
before->next(undopar);
|
2003-04-16 00:02:38 +00:00
|
|
|
else {
|
|
|
|
int id = firstUndoParagraph(bv, undo.number_of_inset_id)->id();
|
2003-04-28 18:20:31 +00:00
|
|
|
Paragraph * op = &*bv->buffer()->getParFromID(id);
|
2003-04-16 00:02:38 +00:00
|
|
|
if (op && op->inInset()) {
|
|
|
|
static_cast<InsetText*>(op->inInset())->paragraph(undopar);
|
|
|
|
} else {
|
|
|
|
bv->buffer()->paragraphs.set(undopar);
|
|
|
|
}
|
|
|
|
}
|
2002-05-26 19:40:17 +00:00
|
|
|
} else {
|
2003-05-01 23:24:30 +00:00
|
|
|
// We enter here on DELETE undo operations where we
|
|
|
|
// have to substitue the second paragraph with the
|
|
|
|
// first if the removed one is the first.
|
2002-05-26 19:40:17 +00:00
|
|
|
if (!before && behind) {
|
2003-04-16 00:02:38 +00:00
|
|
|
int id = firstUndoParagraph(bv, undo.number_of_inset_id)->id();
|
2003-04-28 18:20:31 +00:00
|
|
|
Paragraph * op = &*bv->buffer()->getParFromID(id);
|
2003-04-16 00:02:38 +00:00
|
|
|
if (op && op->inInset()) {
|
|
|
|
static_cast<InsetText*>(op->inInset())->paragraph(behind);
|
|
|
|
} else {
|
|
|
|
bv->buffer()->paragraphs.set(behind);
|
|
|
|
}
|
|
|
|
|
2002-08-24 22:02:30 +00:00
|
|
|
undopar = behind;
|
2001-07-06 15:57:54 +00:00
|
|
|
}
|
2002-05-26 19:40:17 +00:00
|
|
|
}
|
2002-08-24 22:02:30 +00:00
|
|
|
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2003-05-01 23:24:30 +00:00
|
|
|
// Set the cursor for redoing.
|
|
|
|
// If we have a par before the undopar.
|
|
|
|
if (before) {
|
2002-05-26 19:40:17 +00:00
|
|
|
Inset * it = before->inInset();
|
|
|
|
if (it)
|
2003-03-17 16:25:00 +00:00
|
|
|
it->getLyXText(bv)->setCursorIntern(before, 0);
|
2002-05-26 19:40:17 +00:00
|
|
|
else
|
2003-03-17 16:25:00 +00:00
|
|
|
bv->text->setCursorIntern(before, 0);
|
2002-11-08 14:51:03 +00:00
|
|
|
}
|
2003-05-01 23:24:30 +00:00
|
|
|
|
2002-11-08 14:51:03 +00:00
|
|
|
// we are not ready for this we cannot set the cursor for a paragraph
|
|
|
|
// which is not already in a row of LyXText!!!
|
|
|
|
#if 0
|
|
|
|
else { // otherwise this is the first one and we start here
|
2002-10-14 14:59:50 +00:00
|
|
|
Inset * it = undopar->inInset();
|
|
|
|
if (it)
|
|
|
|
it->getLyXText(bv)->setCursorIntern(bv, undopar, 0);
|
|
|
|
else
|
|
|
|
bv->text->setCursorIntern(bv, undopar, 0);
|
2002-05-26 19:40:17 +00:00
|
|
|
}
|
2002-11-08 14:51:03 +00:00
|
|
|
#endif
|
2001-07-06 15:57:54 +00:00
|
|
|
|
2002-05-26 19:40:17 +00:00
|
|
|
Paragraph * endpar = 0;
|
2003-05-01 23:24:30 +00:00
|
|
|
|
|
|
|
// Calculate the endpar for redoing the paragraphs.
|
2002-05-26 19:40:17 +00:00
|
|
|
if (behind)
|
|
|
|
endpar = behind->next();
|
|
|
|
|
2003-04-29 15:24:37 +00:00
|
|
|
UpdatableInset * it = 0;
|
2002-08-24 22:02:30 +00:00
|
|
|
if (undopar)
|
|
|
|
it = static_cast<UpdatableInset*>(undopar->inInset());
|
2002-05-26 19:40:17 +00:00
|
|
|
if (it) {
|
2003-03-17 16:25:00 +00:00
|
|
|
it->getLyXText(bv)->redoParagraphs(
|
2002-05-26 19:40:17 +00:00
|
|
|
it->getLyXText(bv)->cursor,
|
|
|
|
endpar);
|
2003-04-29 15:24:37 +00:00
|
|
|
Paragraph * tmppar =
|
|
|
|
&*bv->buffer()->getParFromID(undo.number_of_cursor_par);
|
2002-05-26 19:40:17 +00:00
|
|
|
if (tmppar) {
|
|
|
|
it = static_cast<UpdatableInset*>(tmppar->inInset());
|
|
|
|
LyXText * t;
|
|
|
|
if (it) {
|
|
|
|
it->edit(bv);
|
|
|
|
t = it->getLyXText(bv);
|
|
|
|
} else {
|
|
|
|
t = bv->text;
|
2001-07-06 15:57:54 +00:00
|
|
|
}
|
2003-03-17 16:25:00 +00:00
|
|
|
t->setCursorIntern(tmppar, undo.cursor_pos);
|
2003-05-01 23:24:30 +00:00
|
|
|
// Clear any selection and set the selection
|
|
|
|
// cursor for an evt. new selection.
|
2002-05-26 19:40:17 +00:00
|
|
|
t->clearSelection();
|
2002-06-24 20:28:12 +00:00
|
|
|
t->selection.cursor = t->cursor;
|
2003-03-17 16:25:00 +00:00
|
|
|
t->updateCounters();
|
2002-05-26 19:40:17 +00:00
|
|
|
bv->fitCursor();
|
|
|
|
}
|
2003-03-19 14:45:22 +00:00
|
|
|
bv->updateInset(it);
|
2003-03-17 16:25:00 +00:00
|
|
|
bv->text->setCursorIntern(bv->text->cursor.par(),
|
2002-05-26 19:40:17 +00:00
|
|
|
bv->text->cursor.pos());
|
|
|
|
} else {
|
2003-03-17 16:25:00 +00:00
|
|
|
bv->text->redoParagraphs(bv->text->cursor, endpar);
|
2003-04-29 15:24:37 +00:00
|
|
|
Paragraph * tmppar =
|
|
|
|
&*bv->buffer()->getParFromID(undo.number_of_cursor_par);
|
2002-05-26 19:40:17 +00:00
|
|
|
if (tmppar) {
|
|
|
|
LyXText * t;
|
|
|
|
Inset * it = tmppar->inInset();
|
|
|
|
if (it) {
|
|
|
|
it->edit(bv);
|
|
|
|
t = it->getLyXText(bv);
|
|
|
|
} else {
|
|
|
|
t = bv->text;
|
2001-07-06 15:57:54 +00:00
|
|
|
}
|
2003-03-17 16:25:00 +00:00
|
|
|
t->setCursorIntern(tmppar, undo.cursor_pos);
|
2003-05-01 23:24:30 +00:00
|
|
|
// Clear any selection and set the selection
|
|
|
|
// cursor for an evt. new selection.
|
2002-05-26 19:40:17 +00:00
|
|
|
t->clearSelection();
|
2002-06-24 20:28:12 +00:00
|
|
|
t->selection.cursor = t->cursor;
|
2003-03-17 16:25:00 +00:00
|
|
|
t->updateCounters();
|
2001-07-06 15:57:54 +00:00
|
|
|
}
|
2002-05-26 19:40:17 +00:00
|
|
|
}
|
|
|
|
|
2003-05-01 23:24:30 +00:00
|
|
|
// And here it's safe enough to delete all removed paragraphs.
|
2002-05-26 19:40:17 +00:00
|
|
|
vector<Paragraph *>::iterator pit = deletelist.begin();
|
2003-05-01 23:24:30 +00:00
|
|
|
for(; pit != deletelist.end(); ++pit) {
|
|
|
|
(*pit)->previous(0);
|
|
|
|
(*pit)->next(0);
|
|
|
|
delete (*pit);
|
|
|
|
}
|
2002-06-24 20:28:12 +00:00
|
|
|
|
2001-07-06 15:57:54 +00:00
|
|
|
finishUndo();
|
2003-03-17 16:25:00 +00:00
|
|
|
bv->text->postPaint(0);
|
2002-05-26 19:40:17 +00:00
|
|
|
return true;
|
2001-07-06 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
2002-06-24 20:28:12 +00:00
|
|
|
|
2002-05-26 19:40:17 +00:00
|
|
|
bool createUndo(BufferView * bv, Undo::undo_kind kind,
|
2003-04-29 14:05:54 +00:00
|
|
|
ParagraphList::iterator itfirst, ParagraphList::iterator itbehind,
|
|
|
|
shared_ptr<Undo> & u)
|
2001-07-06 15:57:54 +00:00
|
|
|
{
|
2003-04-29 14:05:54 +00:00
|
|
|
Paragraph * first = &*itfirst;
|
|
|
|
Paragraph * behind = &*itbehind;
|
2001-07-06 15:57:54 +00:00
|
|
|
lyx::Assert(first);
|
|
|
|
|
|
|
|
int before_number = -1;
|
|
|
|
int behind_number = -1;
|
|
|
|
int inset_id = -1;
|
|
|
|
|
|
|
|
if (first->previous())
|
|
|
|
before_number = first->previous()->id();
|
|
|
|
if (behind)
|
|
|
|
behind_number = behind->id();
|
2001-07-11 12:10:46 +00:00
|
|
|
if (first->inInset())
|
|
|
|
inset_id = first->inInset()->id();
|
2001-07-06 15:57:54 +00:00
|
|
|
|
2002-05-26 19:40:17 +00:00
|
|
|
Buffer * b = bv->buffer();
|
2002-06-24 20:28:12 +00:00
|
|
|
|
2001-07-06 15:57:54 +00:00
|
|
|
// Undo::EDIT and Undo::FINISH are
|
|
|
|
// always finished. (no overlapping there)
|
2002-03-21 17:27:08 +00:00
|
|
|
// overlapping only with insert and delete inside one paragraph:
|
2001-07-06 15:57:54 +00:00
|
|
|
// Nobody wants all removed character
|
2002-03-21 17:27:08 +00:00
|
|
|
// appear one by one when undoing.
|
2001-07-06 15:57:54 +00:00
|
|
|
// EDIT is special since only layout information, not the
|
|
|
|
// contents of a paragaph are stored.
|
2002-02-16 15:59:55 +00:00
|
|
|
if (!undo_finished && (kind != Undo::EDIT) && (kind != Undo::FINISH)) {
|
2003-05-01 23:24:30 +00:00
|
|
|
// Check whether storing is needed.
|
2002-05-26 19:40:17 +00:00
|
|
|
if (!b->undostack.empty() &&
|
|
|
|
b->undostack.top()->kind == kind &&
|
|
|
|
b->undostack.top()->number_of_before_par == before_number &&
|
|
|
|
b->undostack.top()->number_of_behind_par == behind_number) {
|
2003-05-01 23:24:30 +00:00
|
|
|
// No undo needed.
|
2002-05-26 19:40:17 +00:00
|
|
|
return false;
|
2001-07-06 15:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
2002-06-24 20:28:12 +00:00
|
|
|
|
2003-05-01 23:24:30 +00:00
|
|
|
// Create a new Undo.
|
2003-05-02 13:11:39 +00:00
|
|
|
std::vector<Paragraph *> undo_pars;
|
2001-07-06 15:57:54 +00:00
|
|
|
|
2003-04-29 14:10:13 +00:00
|
|
|
Paragraph * start = first;
|
2003-04-30 07:04:33 +00:00
|
|
|
Paragraph * end = 0;
|
2001-07-06 15:57:54 +00:00
|
|
|
|
2003-04-30 07:04:33 +00:00
|
|
|
if (behind)
|
|
|
|
end = const_cast<Paragraph*>(behind->previous());
|
|
|
|
else {
|
|
|
|
end = start;
|
|
|
|
while (end->next())
|
|
|
|
end = end->next();
|
|
|
|
}
|
2001-07-06 15:57:54 +00:00
|
|
|
if (start && end && (start != end->next()) &&
|
|
|
|
((before_number != behind_number) ||
|
2001-12-14 11:55:58 +00:00
|
|
|
((before_number < 0) && (behind_number < 0))))
|
|
|
|
{
|
2001-07-06 15:57:54 +00:00
|
|
|
Paragraph * tmppar = start;
|
2003-05-02 13:11:39 +00:00
|
|
|
undo_pars.push_back(new Paragraph(*tmppar, true));
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2003-05-01 23:24:30 +00:00
|
|
|
// A memory optimization: Just store the layout
|
|
|
|
// information when only edit.
|
2001-07-06 15:57:54 +00:00
|
|
|
if (kind == Undo::EDIT) {
|
2003-05-02 13:11:39 +00:00
|
|
|
undo_pars.back()->clearContents();
|
2001-07-06 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (tmppar != end && tmppar->next()) {
|
|
|
|
tmppar = tmppar->next();
|
2003-05-02 13:11:39 +00:00
|
|
|
undo_pars.push_back(new Paragraph(*tmppar, true));
|
|
|
|
size_t const n = undo_pars.size();
|
|
|
|
undo_pars[n - 2]->next(undo_pars[n - 1]);
|
|
|
|
undo_pars[n - 1]->previous(undo_pars[n - 2]);
|
2001-07-06 15:57:54 +00:00
|
|
|
// a memory optimization: Just store the layout
|
|
|
|
// information when only edit
|
|
|
|
if (kind == Undo::EDIT) {
|
2003-05-02 13:11:39 +00:00
|
|
|
undo_pars.back()->clearContents();
|
2001-07-06 15:57:54 +00:00
|
|
|
}
|
|
|
|
}
|
2003-05-02 13:11:39 +00:00
|
|
|
undo_pars.back()->next(0);
|
2003-04-29 14:31:53 +00:00
|
|
|
}
|
2001-07-06 15:57:54 +00:00
|
|
|
|
|
|
|
int cursor_par = undoCursor(bv).par()->id();
|
2003-04-29 14:31:53 +00:00
|
|
|
int cursor_pos = undoCursor(bv).pos();
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2002-05-26 19:40:17 +00:00
|
|
|
u.reset(new Undo(kind, inset_id,
|
|
|
|
before_number, behind_number,
|
2003-05-02 13:11:39 +00:00
|
|
|
cursor_par, cursor_pos, undo_pars));
|
2002-03-21 17:27:08 +00:00
|
|
|
|
2001-07-06 15:57:54 +00:00
|
|
|
undo_finished = false;
|
2002-05-26 19:40:17 +00:00
|
|
|
return true;
|
2001-07-06 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
2002-05-26 19:40:17 +00:00
|
|
|
|
2003-05-01 23:24:30 +00:00
|
|
|
// Returns false if no undo possible.
|
2003-04-29 14:50:11 +00:00
|
|
|
bool textUndoOrRedo(BufferView * bv,
|
|
|
|
limited_stack<boost::shared_ptr<Undo> > & stack,
|
|
|
|
limited_stack<boost::shared_ptr<Undo> > & otherstack)
|
2002-05-26 19:40:17 +00:00
|
|
|
{
|
|
|
|
Buffer * b = bv->buffer();
|
2002-06-24 20:28:12 +00:00
|
|
|
|
2003-04-29 14:50:11 +00:00
|
|
|
if (stack.empty()) {
|
2002-05-26 19:40:17 +00:00
|
|
|
finishNoUndo(bv);
|
|
|
|
return false;
|
2001-07-06 15:57:54 +00:00
|
|
|
}
|
2002-06-24 20:28:12 +00:00
|
|
|
|
2003-04-29 14:50:11 +00:00
|
|
|
shared_ptr<Undo> undo = stack.top();
|
|
|
|
stack.pop();
|
2002-05-26 19:40:17 +00:00
|
|
|
finishUndo();
|
2002-06-24 20:28:12 +00:00
|
|
|
|
2002-05-26 19:40:17 +00:00
|
|
|
if (!undo_frozen) {
|
2003-04-28 18:20:31 +00:00
|
|
|
Paragraph * first = &*b->getParFromID(undo->number_of_before_par);
|
2002-05-26 19:40:17 +00:00
|
|
|
if (first && first->next())
|
|
|
|
first = first->next();
|
|
|
|
else if (!first)
|
|
|
|
first = firstUndoParagraph(bv, undo->number_of_inset_id);
|
|
|
|
if (first) {
|
|
|
|
shared_ptr<Undo> u;
|
|
|
|
if (createUndo(bv, undo->kind, first,
|
2003-04-29 14:50:11 +00:00
|
|
|
b->getParFromID(undo->number_of_behind_par), u))
|
|
|
|
otherstack.push(u);
|
2002-05-26 19:40:17 +00:00
|
|
|
}
|
|
|
|
}
|
2002-06-24 20:28:12 +00:00
|
|
|
|
2003-05-01 23:24:30 +00:00
|
|
|
// Now we can unlock the inset for saftey because the inset
|
|
|
|
// pointer could be changed during the undo-function. Anyway
|
|
|
|
// if needed we have to lock the right inset/position if this
|
|
|
|
// is requested.
|
2002-05-26 19:40:17 +00:00
|
|
|
freezeUndo();
|
|
|
|
bv->unlockInset(bv->theLockingInset());
|
|
|
|
bool const ret = textHandleUndo(bv, *undo.get());
|
|
|
|
unFreezeUndo();
|
|
|
|
return ret;
|
2001-07-06 15:57:54 +00:00
|
|
|
}
|
|
|
|
|
2003-04-29 14:50:11 +00:00
|
|
|
} // namespace anon
|
2001-12-13 17:19:53 +00:00
|
|
|
|
2003-05-01 23:24:30 +00:00
|
|
|
|
2003-04-29 14:50:11 +00:00
|
|
|
void finishUndo()
|
2001-07-06 15:57:54 +00:00
|
|
|
{
|
2003-05-01 23:24:30 +00:00
|
|
|
// Makes sure the next operation will be stored.
|
2003-04-29 14:50:11 +00:00
|
|
|
undo_finished = true;
|
|
|
|
}
|
2002-05-26 19:40:17 +00:00
|
|
|
|
2002-06-24 20:28:12 +00:00
|
|
|
|
2003-04-29 14:50:11 +00:00
|
|
|
void freezeUndo()
|
|
|
|
{
|
2003-05-01 23:24:30 +00:00
|
|
|
// This is dangerous and for internal use only.
|
2003-04-29 14:50:11 +00:00
|
|
|
undo_frozen = true;
|
|
|
|
}
|
2002-06-24 20:28:12 +00:00
|
|
|
|
|
|
|
|
2003-04-29 14:50:11 +00:00
|
|
|
void unFreezeUndo()
|
|
|
|
{
|
2003-05-01 23:24:30 +00:00
|
|
|
// This is dangerous and for internal use only.
|
2003-04-29 14:50:11 +00:00
|
|
|
undo_frozen = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool textUndo(BufferView * bv)
|
|
|
|
{
|
2003-05-01 23:24:30 +00:00
|
|
|
return textUndoOrRedo(bv, bv->buffer()->undostack,
|
|
|
|
bv->buffer()->redostack);
|
2003-04-29 14:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool textRedo(BufferView * bv)
|
|
|
|
{
|
2003-05-01 23:24:30 +00:00
|
|
|
return textUndoOrRedo(bv, bv->buffer()->redostack,
|
|
|
|
bv->buffer()->undostack);
|
2002-05-26 19:40:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void setUndo(BufferView * bv, Undo::undo_kind kind,
|
2003-04-29 14:05:54 +00:00
|
|
|
ParagraphList::iterator first, ParagraphList::iterator behind)
|
2002-05-26 19:40:17 +00:00
|
|
|
{
|
|
|
|
if (!undo_frozen) {
|
|
|
|
shared_ptr<Undo> u;
|
2003-04-29 14:10:13 +00:00
|
|
|
if (createUndo(bv, kind, first, behind, u))
|
2002-05-26 19:40:17 +00:00
|
|
|
bv->buffer()->undostack.push(u);
|
|
|
|
bv->buffer()->redostack.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void setRedo(BufferView * bv, Undo::undo_kind kind,
|
2003-04-29 14:05:54 +00:00
|
|
|
ParagraphList::iterator first, ParagraphList::iterator behind)
|
2002-05-26 19:40:17 +00:00
|
|
|
{
|
|
|
|
shared_ptr<Undo> u;
|
2003-04-29 14:10:13 +00:00
|
|
|
if (createUndo(bv, kind, first, behind, u))
|
2002-05-26 19:40:17 +00:00
|
|
|
bv->buffer()->redostack.push(u);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void setCursorParUndo(BufferView * bv)
|
|
|
|
{
|
2003-04-29 23:59:39 +00:00
|
|
|
setUndo(bv, Undo::FINISH, bv->text->cursor.par(),
|
|
|
|
boost::next(bv->text->cursor.par()));
|
2001-07-06 15:57:54 +00:00
|
|
|
}
|