]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/formulabase.C
hot fix for spurious cursor jumps in math insets
[lyx.git] / src / mathed / formulabase.C
index f233c7851dbf1dc4b91414ff19ce48e7a41ad167..3d36585989575072d4ee786e6855e1b72dfc94e3 100644 (file)
@@ -26,7 +26,6 @@
 #include "formulamacro.h"
 #include "commandtags.h"
 #include "math_cursor.h"
-#include "math_parser.h"
 #include "BufferView.h"
 #include "lyxtext.h"
 #include "lyxfunc.h"
@@ -95,7 +94,7 @@ bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
 MathArrayInset * matrixpar(MathInset::idx_type & idx)
 {
        idx = 0;
-       return (mathcursor ? mathcursor->enclosingArray(idx) : 0)
+       return mathcursor ? mathcursor->enclosingArray(idx) : 0
 }
 
 
@@ -104,6 +103,7 @@ MathArrayInset * matrixpar(MathInset::idx_type & idx)
 
 
 InsetFormulaBase::InsetFormulaBase()
+       : view_(0), font_(), xo_(0), yo_(0)
 {
        // This is needed as long the math parser is not re-entrant
        MathMacroTable::builtinMacros();
@@ -116,6 +116,16 @@ void InsetFormulaBase::validate(LaTeXFeatures &) const
 {}
 
 
+void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const 
+{
+       if (bv)
+               view_ = bv;
+       font_ = f;
+       MathMetricsInfo mi(view_, font_, display() ? LM_ST_DISPLAY : LM_ST_TEXT);
+       par()->metrics(mi);
+}
+
+
 string const InsetFormulaBase::editMessage() const
 {
        return _("Math editor mode");
@@ -128,7 +138,7 @@ void InsetFormulaBase::edit(BufferView * bv, int x, int /*y*/, unsigned int)
                lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
 
        mathcursor = new MathCursor(this, x == 0);
-       metrics();
+       metrics(bv);
        // if that is removed, we won't get the magenta box when entering an
        // inset for the first time
        bv->updateInset(this, false);
@@ -162,9 +172,9 @@ void InsetFormulaBase::insetUnlock(BufferView * bv)
 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
 {
        mathcursor->getPos(x, y);
-       x -= par()->xo();
-       y -= par()->yo();
-       y -= 3;
+       x += xo_;
+       y += yo_ - 3;
+       //lyxerr << "getCursorPos: " << x << " " << y << "\n";
 }
 
 
@@ -176,16 +186,18 @@ void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
        if (isCursorVisible())
                bv->hideLockedInsetCursor();
        else {
+               metrics(bv);
                int x;
                int y;
                mathcursor->getPos(x, y);
-               //x -= par()->xo();
-               y -= par()->yo();
                y -= 3;
-               int asc;
-               int des;
-               math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
+               y -= yo_;
+               int asc = 0;
+               int des = 0;
+               MathMetricsInfo mi(bv, font_, LM_ST_TEXT);
+               math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
                bv->showLockedInsetCursor(x, y, asc, des);
+               //lyxerr << "toggleInsetCursor: " << x << " " << y << "\n";
        }
 
        toggleCursorVisible();
@@ -199,12 +211,13 @@ void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
                        int x;
                        int y;
                        mathcursor->getPos(x, y);
-                       x -= par()->xo();
-                       y -= par()->yo();
-                       int asc;
-                       int des;
-                       math_font_max_dim(LM_TC_TEXTRM, LM_ST_TEXT, asc, des);
-                       bv->fitLockedInsetCursor(x, y, asc, des);
+                       int asc = 0;
+                       int des = 0;
+                       MathMetricsInfo mi(bv, font_, LM_ST_TEXT);
+                       math_font_max_dim(LM_TC_TEXTRM, mi, asc, des);
+                       //bv->fitLockedInsetCursor(x, y, asc, des);
+                       //metrics(bv);
+                       //lyxerr << "showInsetCursor: " << x << " " << y << "\n";
                }
                toggleInsetCursor(bv);
        }
@@ -233,7 +246,7 @@ vector<string> const InsetFormulaBase::getLabelList() const
 
 void InsetFormulaBase::updateLocal(BufferView * bv, bool dirty)
 {
-       metrics();
+       metrics(bv);
        bv->updateInset(this, dirty);
 }
 
@@ -243,9 +256,8 @@ void InsetFormulaBase::insetButtonRelease(BufferView * bv,
 {
        if (mathcursor) {
                hideInsetCursor(bv);
-               x += par()->xo();
-               y += par()->yo();
-               mathcursor->setPos(x, y);
+               mathcursor->setPos(x + xo_, y + yo_);
+               //lyxerr << "insetButtonRelease: " << x + xo_ << " " << y + yo_ << "\n";
                showInsetCursor(bv);
                if (sel_flag) {
                        sel_flag = false;
@@ -276,14 +288,12 @@ void InsetFormulaBase::insetMotionNotify(BufferView * bv,
        if (sel_x && sel_y && abs(x-sel_x) > 4 && !sel_flag) {
                sel_flag = true;
                hideInsetCursor(bv);
-               mathcursor->setPos(sel_x + par()->xo(), sel_y + par()->yo());
+               mathcursor->setPos(sel_x, sel_y);
                mathcursor->selStart();
                showInsetCursor(bv);
                mathcursor->getPos(sel_x, sel_y);
        } else if (sel_flag) {
                hideInsetCursor(bv);
-               x += par()->xo();
-               y += par()->yo();
                mathcursor->setPos(x, y);
                showInsetCursor(bv);
                mathcursor->getPos(x, y);
@@ -311,6 +321,11 @@ InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
        if (!mathcursor) 
                return UNDISPATCHED;
 
+       if (mathcursor->asHyperActiveInset()) {
+               lyxerr << " uurr.... getting dificult now\n";
+               return mathcursor->asHyperActiveInset()->localDispatch(bv, action, arg);
+       }
+
        RESULT result      = DISPATCHED;
        bool sel           = false;
        bool was_macro     = mathcursor->inMacroMode();
@@ -397,12 +412,14 @@ InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
                updateLocal(bv, true);
                break;
 
+       case LFUN_DELETE_WORD_BACKWARD:
        case LFUN_BACKSPACE:
                bv->lockedInsetStoreUndo(Undo::DELETE);
                mathcursor->backspace();
                bv->updateInset(this, true);
                break;
 
+       case LFUN_DELETE_WORD_FORWARD:
        case LFUN_DELETE:
                bv->lockedInsetStoreUndo(Undo::DELETE);
                mathcursor->erase();
@@ -415,14 +432,11 @@ InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
                //      break;
        case LFUN_SETXY: {
                lyxerr << "LFUN_SETXY broken!\n";
-               int x;
-               int y;
-               int x1;
-               int y1;
+               int x = 0;
+               int y = 0;
                istringstream is(arg.c_str());
                is >> x >> y;
-               par()->getXY(x1, y1);
-               mathcursor->setPos(x1 + x, y1 + y);
+               mathcursor->setPos(x, y);
                updateLocal(bv, false);
                break;
        }
@@ -475,7 +489,7 @@ InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
        case LFUN_GREEK: 
                handleFont(bv, LM_TC_GREEK1);
                if (arg.size())
-                       mathcursor->interpret(arg[0]);
+                       mathcursor->interpret(arg);
                break;
 
        case LFUN_MATH_MODE:
@@ -490,12 +504,13 @@ InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
                break;
 
        case LFUN_MATH_SIZE:
+#if 0
                if (!arg.empty()) {
                        bv->lockedInsetStoreUndo(Undo::INSERT);
-                       latexkeys const * l = in_word_set(arg);
-                       mathcursor->setSize(MathStyles(l ? l->id : static_cast<unsigned int>(-1)));
+                       mathcursor->setSize(arg);
                        updateLocal(bv, true);
                }
+#endif
                break;
 
        case LFUN_INSERT_MATRIX:
@@ -624,6 +639,11 @@ InsetFormulaBase::localDispatch(BufferView * bv, kb_action action,
                result = UNDISPATCHED;
                break;
 
+       case LFUN_BREAKPARAGRAPH:
+       case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
+               //lyxerr << "LFUN ignored\n";
+               break;
+
        case -1:
        case LFUN_INSERT_MATH:
        case LFUN_SELFINSERT:
@@ -668,6 +688,21 @@ Inset::Code InsetFormulaBase::lyxCode() const
 }
 
 
+int InsetFormulaBase::upperY() const
+{
+       return yo_ - ascent(view_, font_);
+}
+
+
+int InsetFormulaBase::lowerY() const
+{
+       return yo_ + descent(view_, font_);
+}
+
+
+/////////////////////////////////////////////////////////////////////
+
+
 void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
 {
        if (bv->available()) {
@@ -677,8 +712,7 @@ void mathDispatchCreation(BufferView * bv, string const & arg, bool display)
                //      sel = "";
                //else
 
-               string sel = bv->getLyXText()->selectionAsString(bv->buffer(),
-                                                                false);
+               string sel = bv->getLyXText()->selectionAsString(bv->buffer(), false);
 
                InsetFormulaBase * f;
                if (sel.empty()) {
@@ -781,3 +815,4 @@ void mathDispatchGreek(BufferView * bv, string const & arg)
        }
 }         
 
+