]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/formulabase.C
more IU
[lyx.git] / src / mathed / formulabase.C
index 6dc422cd8c18b8bc845e28706c5b1913e387687e..3ff6ba7d170f9a37c827a6342223ded13313fdfd 100644 (file)
@@ -11,6 +11,7 @@
 
 #include <config.h>
 
+#include "cursor.h"
 #include "formulabase.h"
 #include "formula.h"
 #include "formulamacro.h"
@@ -61,13 +62,13 @@ namespace {
 int first_x;
 int first_y;
 
-bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
+bool openNewInset(BufferView * bv, UpdatableInset * inset)
 {
-       if (!bv->insertInset(new_inset)) {
-               delete new_inset;
+       if (!bv->insertInset(inset)) {
+               delete inset;
                return false;
        }
-       new_inset->dispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
+       inset->edit(bv, true);
        return true;
 }
 
@@ -77,7 +78,6 @@ bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
 
 
 InsetFormulaBase::InsetFormulaBase()
-       : xo_(0), yo_(0)
 {
        // This is needed as long the math parser is not re-entrant
        initMath();
@@ -154,15 +154,13 @@ string const InsetFormulaBase::editMessage() const
 void InsetFormulaBase::insetUnlock(BufferView * bv)
 {
        if (mathcursor) {
-               if (mathcursor->inMacroMode()) {
+               if (mathcursor->inMacroMode())
                        mathcursor->macroModeClose();
-                       bv->updateInset(this);
-               }
                releaseMathCursor(bv);
        }
        if (bv->buffer())
                generatePreview(*bv->buffer());
-       bv->updateInset(this);
+       bv->update();
 }
 
 
@@ -172,43 +170,36 @@ void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
 }
 
 
-void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
+void InsetFormulaBase::getCursorPos(int, int & x, int & y) const
 {
-       // calling metrics here destroys the cached xo,yo positions e.g. in
-       // MathParboxinset. And it would be too expensive anyway...
-       //metrics(bv);
        if (!mathcursor) {
                lyxerr << "getCursorPos - should not happen";
-               x = y = 0;
+               x = 0;
+               y = 0;
                return;
        }
        mathcursor->getPos(x, y);
        x = mathcursor->targetX();
        x -= xo_;
        y -= yo_;
-       //lyxerr << "getCursorPos: " << x << ' ' << y << endl;
+       lyxerr << "InsetFormulaBase::getCursorPos: " << x << ' ' << y << endl;
 }
 
 
-void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
+void InsetFormulaBase::getCursorDim(int & asc, int & desc) const
 {
        if (!mathcursor)
                return;
-       int x, y, asc, des;
        asc = 10;
-       des = 2;
+       desc = 2;
        //math_font_max_dim(font_, asc, des);
-       getCursorPos(bv, x, y);
-       //y += yo_;
-       //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
-       bv->fitLockedInsetCursor(x, y, asc, des);
 }
 
 
 void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
 {
        if (mathcursor)
-               bv->updateInset(this);
+               bv->update();
 }
 
 
@@ -218,7 +209,7 @@ DispatchResult InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
                return DispatchResult(false);
 
        BufferView * bv = cmd.view();
-       bv->updateInset(this);
+       bv->update();
        //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
 
        if (cmd.button() == mouse_button::button3) {
@@ -228,7 +219,7 @@ DispatchResult InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
                        lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
                        bv->owner()->getDialogs().show("mathpanel");
                }
-               return DispatchResult(true);
+               return DispatchResult(true, true);
        }
 
        if (cmd.button() == mouse_button::button2) {
@@ -237,8 +228,8 @@ DispatchResult InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
                mathcursor->selClear();
                mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
                mathcursor->insert(ar);
-               bv->updateInset(this);
-               return DispatchResult(true);
+               bv->update();
+               return DispatchResult(true, true);
        }
 
        if (cmd.button() == mouse_button::button1) {
@@ -250,7 +241,7 @@ DispatchResult InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
                //mathcursor = new MathCursor(this, x == 0);
                //metrics(bv);
                //mathcursor->setPos(x + xo_, y + yo_);
-               return DispatchResult(true);
+               return DispatchResult(true, true);
        }
 
        return DispatchResult(false);
@@ -272,7 +263,7 @@ DispatchResult InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
 
        if (cmd.button() == mouse_button::button3) {
                mathcursor->dispatch(cmd);
-               return DispatchResult(true);
+               return DispatchResult(true, true);
        }
 
        if (cmd.button() == mouse_button::button1) {
@@ -281,28 +272,28 @@ DispatchResult InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
                mathcursor->selClear();
                mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
                mathcursor->dispatch(cmd);
-               return DispatchResult(true);
+               return DispatchResult(true, true);
        }
 
-       bv->updateInset(this);
-       return DispatchResult(true);
+       bv->update();
+       return DispatchResult(true, true);
 }
 
 
 DispatchResult InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
 {
        if (!mathcursor)
-               return DispatchResult(true);
+               return DispatchResult(true, true);
 
        if (mathcursor->dispatch(FuncRequest(cmd)).dispatched())
-               return DispatchResult(true);
+               return DispatchResult(true, true);
 
        // only select with button 1
        if (cmd.button() != mouse_button::button1)
-               return DispatchResult(true);
+               return DispatchResult(true, true);
 
        if (abs(cmd.x - first_x) < 2 && abs(cmd.y - first_y) < 2)
-               return DispatchResult(true);
+               return DispatchResult(true, true);
 
        first_x = cmd.x;
        first_y = cmd.y;
@@ -312,8 +303,34 @@ DispatchResult InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
 
        BufferView * bv = cmd.view();
        mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
-       bv->updateInset(this);
-       return DispatchResult(true);
+       bv->update();
+       return DispatchResult(true, true);
+}
+
+
+void InsetFormulaBase::edit(BufferView * bv, bool left)
+{
+       lyxerr << "Called FormulaBase::edit" << endl;
+       releaseMathCursor(bv);
+       mathcursor = new MathCursor(this, left);
+       bv->fullCursor().push(this);
+       // if that is removed, we won't get the magenta box when entering an
+       // inset for the first time
+       bv->update();
+}
+
+
+void InsetFormulaBase::edit(BufferView * bv, int x, int y)
+{
+       lyxerr << "Called FormulaBase::EDIT with '" << x << ' ' << y << "'" << endl;
+       releaseMathCursor(bv);
+       mathcursor = new MathCursor(this, true);
+       //metrics(bv);
+       mathcursor->setPos(x + xo_, y + yo_);
+       bv->fullCursor().push(this);
+       // if that is removed, we won't get the magenta box when entering an
+       // inset for the first time
+       bv->update();
 }
 
 
@@ -333,24 +350,6 @@ InsetFormulaBase::priv_dispatch(FuncRequest const & cmd,
        bool remove_inset = false;
 
        switch (cmd.action) {
-               case LFUN_INSET_EDIT:
-                       lyxerr << "Called EDIT with '" << cmd.argument << "'" << endl;
-                       if (!bv->lockInset(this))
-                               lyxerr << "Cannot lock math inset in edit call!" << endl;
-                       releaseMathCursor(bv);
-                       if (!cmd.argument.empty()) {
-                               mathcursor = new MathCursor(this, cmd.argument == "left");
-                               //metrics(bv);
-                       } else {
-                               mathcursor = new MathCursor(this, true);
-                               //metrics(bv);
-                               mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
-                       }
-                       // if that is removed, we won't get the magenta box when entering an
-                       // inset for the first time
-                       bv->updateInset(this);
-                       return DispatchResult(true);
-
                case LFUN_MOUSE_PRESS:
                        //lyxerr << "Mouse single press" << endl;
                        return lfunMousePress(cmd);
@@ -400,7 +399,8 @@ InsetFormulaBase::priv_dispatch(FuncRequest const & cmd,
        case LFUN_RIGHTSEL:
                sel = true; // fall through...
        case LFUN_RIGHT:
-               result = mathcursor->right(sel) ? DispatchResult(true) : DispatchResult(false, FINISHED_RIGHT);
+               result = mathcursor->right(sel) ?
+                       DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
                //lyxerr << "calling scroll 20" << endl;
                //scroll(bv, 20);
                // write something to the minibuffer
@@ -410,19 +410,22 @@ InsetFormulaBase::priv_dispatch(FuncRequest const & cmd,
        case LFUN_LEFTSEL:
                sel = true; // fall through
        case LFUN_LEFT:
-               result = mathcursor->left(sel) ? DispatchResult(true) : DispatchResult(true, FINISHED);
+               result = mathcursor->left(sel) ?
+                       DispatchResult(true, true) : DispatchResult(false, FINISHED);
                break;
 
        case LFUN_UPSEL:
                sel = true; // fall through
        case LFUN_UP:
-               result = mathcursor->up(sel) ? DispatchResult(true) : DispatchResult(false, FINISHED_UP);
+               result = mathcursor->up(sel) ?
+                       DispatchResult(true, true) : DispatchResult(false, FINISHED_UP);
                break;
 
        case LFUN_DOWNSEL:
                sel = true; // fall through
        case LFUN_DOWN:
-               result = mathcursor->down(sel) ? DispatchResult(true) : DispatchResult(false, FINISHED_DOWN);
+               result = mathcursor->down(sel) ?
+                       DispatchResult(true, true) : DispatchResult(false, FINISHED_DOWN);
                break;
 
        case LFUN_WORDSEL:
@@ -442,7 +445,7 @@ InsetFormulaBase::priv_dispatch(FuncRequest const & cmd,
                sel = true; // fall through
        case LFUN_HOME:
        case LFUN_WORDLEFT:
-               result = mathcursor->home(sel) ? DispatchResult(true) : DispatchResult(true, FINISHED);
+               result = mathcursor->home(sel) ? DispatchResult(true, true) : DispatchResult(true, FINISHED);
                break;
 
        case LFUN_ENDSEL:
@@ -450,7 +453,7 @@ InsetFormulaBase::priv_dispatch(FuncRequest const & cmd,
                sel = true; // fall through
        case LFUN_END:
        case LFUN_WORDRIGHT:
-               result = mathcursor->end(sel) ? DispatchResult(true) : DispatchResult(false, FINISHED_RIGHT);
+               result = mathcursor->end(sel) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
                break;
 
        case LFUN_PRIORSEL:
@@ -605,16 +608,7 @@ InsetFormulaBase::priv_dispatch(FuncRequest const & cmd,
                break;
        }
 
-       case LFUN_SUPERSCRIPT:
-       case LFUN_SUBSCRIPT:
-       {
-               recordUndo(bv, Undo::ATOMIC);
-               mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
-               break;
-       }
-
-       case LFUN_MATH_DELIM:
-       {
+       case LFUN_MATH_DELIM: {
                //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
                string ls;
                string rs = split(cmd.argument, ls, ' ');
@@ -669,7 +663,7 @@ InsetFormulaBase::priv_dispatch(FuncRequest const & cmd,
                if (!argument.empty()) {
                        recordUndo(bv, Undo::ATOMIC);
                        if (argument.size() == 1)
-                               result = mathcursor->interpret(argument[0]) ? DispatchResult(true) : DispatchResult(false, FINISHED_RIGHT);
+                               result = mathcursor->interpret(argument[0]) ? DispatchResult(true, true) : DispatchResult(false, FINISHED_RIGHT);
                        else
                                mathcursor->insert(argument);
                }
@@ -700,11 +694,10 @@ InsetFormulaBase::priv_dispatch(FuncRequest const & cmd,
 
                if (data.empty())
                        result = DispatchResult(false);
-               else {
+               else
                        bv->owner()->getDialogs().show(name, data, 0);
-               }
+               break;
        }
-       break;
 
        case LFUN_INSET_APPLY: {
                string const name = cmd.getArg(0);
@@ -718,20 +711,28 @@ InsetFormulaBase::priv_dispatch(FuncRequest const & cmd,
                        MathArray ar;
                        if (createMathInset_fromDialogStr(cmd.argument, ar)) {
                                mathcursor->insert(ar);
-                               result = DispatchResult(true);
+                               result = DispatchResult(true, true);
                        } else {
                                result = DispatchResult(false);
                        }
                }
+               break;
+       }
+
+       case LFUN_WORD_REPLACE:
+       case LFUN_WORD_FIND: {
+               result = 
+                       searchForward(cmd.view(), cmd.getArg(0), false, false)
+                               ? DispatchResult(true, true) : DispatchResult(false);
+               break;
        }
-       break;
 
        default:
                result = DispatchResult(false);
        }
 
-       if (result == DispatchResult(true))
-               bv->updateInset(this);
+       if (result == DispatchResult(true, true))
+               bv->update();
 
        mathcursor->normalize();
        mathcursor->touch();
@@ -741,13 +742,11 @@ InsetFormulaBase::priv_dispatch(FuncRequest const & cmd,
        if (mathcursor->selection() || was_selection)
                toggleInsetSelection(bv);
 
-       if (result.val() <= NOUPDATE) {
-               fitInsetCursor(bv);
+       if (result.dispatched()) {
                revealCodes(bv);
                cmd.view()->stuffClipboard(mathcursor->grabSelection());
        } else {
                releaseMathCursor(bv);
-               bv->unlockInset(this);
                if (remove_inset)
                        bv->owner()->dispatch(FuncRequest(LFUN_DELETE));
        }
@@ -831,7 +830,7 @@ bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
 #warning pretty ugly
 #endif
        static InsetFormulaBase * lastformula = 0;
-       static MathIterator current = MathIterator(ibegin(par().nucleus()));
+       static CursorBase current = CursorBase(ibegin(par().nucleus()));
        static MathArray ar;
        static string laststr;
 
@@ -843,24 +842,21 @@ bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
                ar.clear();
                mathed_parse_cell(ar, str);
        } else {
-               ++current;
+               increment(current);
        }
        //lyxerr << "searching '" << str << "' in " << this << ar << endl;
 
-       for (MathIterator it = current; it != iend(par().nucleus()); ++it) {
-               if (it.cell().matchpart(ar, it.back().pos_)) {
-                       bv->unlockInset(bv->theLockingInset());
-                       if (!bv->lockInset(this)) {
-                               lyxerr << "Cannot lock inset" << endl;
-                               return false;
-                       }
+       for (CursorBase it = current; it != iend(par().nucleus()); increment(it)) {
+               CursorSlice & top = it.back();
+               MathArray const & a = top.asMathInset()->cell(top.idx_);
+               if (a.matchpart(ar, top.pos_)) {
                        delete mathcursor;
                        mathcursor = new MathCursor(this, true);
                        //metrics(bv);
                        mathcursor->setSelection(it, ar.size());
                        current = it;
-                       it.jump(ar.size());
-                       bv->updateInset(this);
+                       top.pos_ += ar.size();
+                       bv->update();
                        return true;
                }
        }
@@ -907,7 +903,7 @@ void mathDispatchCreation(FuncRequest const & cmd, bool display)
        if (sel.empty()) {
                InsetFormula * f = new InsetFormula(bv);
                if (openNewInset(bv, f)) {
-                       bv->theLockingInset()->
+                       bv->fullCursor().innerInset()->
                                dispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
                        // don't do that also for LFUN_MATH_MODE unless you want end up with
                        // always changing to mathrm when opening an inlined inset
@@ -970,9 +966,9 @@ void mathDispatch(FuncRequest const & cmd)
                case LFUN_MATH_DELIM: {
                        InsetFormula * f = new InsetFormula(bv);
                        if (openNewInset(bv, f)) {
-                               bv->theLockingInset()->
-                                       dispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
-                               bv->theLockingInset()->dispatch(cmd);
+                               UpdatableInset * inset = bv->fullCursor().innerInset();
+                               inset->dispatch(FuncRequest(bv, LFUN_MATH_MUTATE, "simple"));
+                               inset->dispatch(cmd);
                        }
                        break;
                }