]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/formulabase.C
architectural changes to tex2lyx
[lyx.git] / src / mathed / formulabase.C
index e4e308a40e16df0687c4870f69b991bd53099005..1e51f0c4470f1e31f4188f8eb72c7b1279e1fdab 100644 (file)
 */
 
 #include <config.h>
-#include <fstream>
 
 #include "Lsstream.h"
 #include "support/LAssert.h"
-
-
 #include "formula.h"
 #include "formulamacro.h"
 #include "lyxrc.h"
 #include "debug.h"
 #include "math_support.h"
 #include "metricsinfo.h"
+#include "math_data.h"
 #include "support/lstrings.h"
+#include "support/lyxlib.h"
 #include "frontends/LyXView.h"
 #include "frontends/font_metrics.h"
 #include "frontends/mouse_state.h"
-#include "Lsstream.h"
 #include "math_arrayinset.h"
 #include "math_charinset.h"
 #include "math_deliminset.h"
 #include "intl.h"
 #include "ref_inset.h"
 
+#include <fstream>
+
+using namespace lyx::support;
+
 using std::endl;
 using std::ostream;
 using std::vector;
 using std::abs;
 using std::max;
 
-MathCursor * mathcursor = 0;
 
+MathCursor * mathcursor = 0;
 
 namespace {
 
@@ -68,15 +70,13 @@ namespace {
 int first_x;
 int first_y;
 
-
-
 bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
 {
        if (!bv->insertInset(new_inset)) {
                delete new_inset;
                return false;
        }
-       new_inset->edit(bv, true);
+       new_inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
        return true;
 }
 
@@ -86,14 +86,14 @@ bool openNewInset(BufferView * bv, UpdatableInset * new_inset)
 
 
 InsetFormulaBase::InsetFormulaBase()
-       : font_(), xo_(0), yo_(0)
+       : xo_(0), yo_(0)
 {
        // This is needed as long the math parser is not re-entrant
        initMath();
-       //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << "\n";
-       //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << "\n";
-       //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << "\n";
-       //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << "\n";
+       //lyxerr << "sizeof MathInset: " << sizeof(MathInset) << endl;
+       //lyxerr << "sizeof MetricsInfo: " << sizeof(MetricsInfo) << endl;
+       //lyxerr << "sizeof MathCharInset: " << sizeof(MathCharInset) << endl;
+       //lyxerr << "sizeof LyXFont: " << sizeof(LyXFont) << endl;
 }
 
 
@@ -124,8 +124,9 @@ void InsetFormulaBase::handleFont
 {
        // this whole function is a hack and won't work for incremental font
        // changes...
-       bv->lockedInsetStoreUndo(Undo::EDIT);
-       if (mathcursor->par()->name() == font)
+       recordUndo(bv, Undo::ATOMIC);
+
+       if (mathcursor->inset()->name() == font)
                mathcursor->handleFont(font);
        else {
                mathcursor->handleNest(createMathInset(font));
@@ -136,7 +137,7 @@ void InsetFormulaBase::handleFont
 
 BufferView * InsetFormulaBase::view() const
 {
-       return view_.lock().get();
+       return view_;
 }
 
 
@@ -144,56 +145,12 @@ void InsetFormulaBase::validate(LaTeXFeatures &) const
 {}
 
 
-void InsetFormulaBase::metrics(BufferView * bv, LyXFont const & f) const
-{
-       font_ = f;
-       metrics(bv);
-}
-
-
-void InsetFormulaBase::metrics(BufferView * bv) const
-{
-       if (bv)
-               view_ = bv->owner()->view();
-       MetricsInfo mi;
-       mi.base.style = LM_ST_TEXT;
-       mi.base.font  = font_;
-       mi.base.font.setColor(LColor::math);
-       par()->metrics(mi);
-}
-
-
 string const InsetFormulaBase::editMessage() const
 {
        return _("Math editor mode");
 }
 
 
-void InsetFormulaBase::edit(BufferView * bv, int x, int y, mouse_button::state)
-{
-       if (!bv->lockInset(this))
-               lyxerr[Debug::MATHED] << "Cannot lock inset!!!" << endl;
-       releaseMathCursor(bv);
-       mathcursor = new MathCursor(this, true);
-       metrics(bv);
-       mathcursor->setPos(x + xo_, y + yo_);
-       // if that is removed, we won't get the magenta box when entering an
-       // inset for the first time
-       bv->updateInset(this);
-}
-
-
-void InsetFormulaBase::edit(BufferView * bv, bool front)
-{
-       if (!bv->lockInset(this))
-               lyxerr << "Cannot lock math inset in edit call!\n";
-       releaseMathCursor(bv);
-       mathcursor = new MathCursor(this, front);
-       metrics(bv);
-       bv->updateInset(this);
-}
-
-
 void InsetFormulaBase::insetUnlock(BufferView * bv)
 {
        if (mathcursor) {
@@ -208,6 +165,12 @@ void InsetFormulaBase::insetUnlock(BufferView * bv)
 }
 
 
+void InsetFormulaBase::getCursor(BufferView &, int & x, int & y) const
+{
+       mathcursor->getPos(x, y);
+}
+
+
 void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
 {
        // calling metrics here destroys the cached xo,yo positions e.g. in
@@ -226,55 +189,14 @@ void InsetFormulaBase::getCursorPos(BufferView *, int & x, int & y) const
 }
 
 
-void InsetFormulaBase::toggleInsetCursor(BufferView * bv)
-{
-       if (!mathcursor) {
-               lyxerr[Debug::MATHED] << "toggleInsetCursor impossible" << endl;
-               return;
-       }
-       //lyxerr << "toggleInsetCursor: " << isCursorVisible() << endl;
-       if (isCursorVisible())
-               hideInsetCursor(bv);
-       else
-               showInsetCursor(bv);
-}
-
-
-void InsetFormulaBase::showInsetCursor(BufferView * bv, bool)
-{
-       if (!mathcursor) {
-               lyxerr << "showInsetCursor impossible" << endl;
-               return;
-       }
-       if (isCursorVisible())
-               return;
-       int x, y, asc, des;
-       mathcursor->getPos(x, y);
-       math_font_max_dim(font_, asc, des);
-       bv->showLockedInsetCursor(x, y - yo_, asc, des);
-       setCursorVisible(true);
-       //lyxerr << "showInsetCursor: " << x << ' ' << y << endl;
-}
-
-
-void InsetFormulaBase::hideInsetCursor(BufferView * bv)
-{
-       if (!mathcursor)
-               return;
-       if (!isCursorVisible())
-               return;
-       bv->hideLockedInsetCursor();
-       setCursorVisible(false);
-       //lyxerr << "hideInsetCursor: " << endl;
-}
-
-
 void InsetFormulaBase::fitInsetCursor(BufferView * bv) const
 {
        if (!mathcursor)
                return;
        int x, y, asc, des;
-       math_font_max_dim(font_, asc, des);
+       asc = 10;
+       des = 2;
+       //math_font_max_dim(font_, asc, des);
        getCursorPos(bv, x, y);
        //y += yo_;
        //lyxerr << "fitInsetCursor: x: " << x << " y: " << y << " yo: " << yo_ << endl;
@@ -289,20 +211,12 @@ void InsetFormulaBase::toggleInsetSelection(BufferView * bv)
 }
 
 
-vector<string> const InsetFormulaBase::getLabelList() const
-{
-  return vector<string>();
-}
-
-
 dispatch_result InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
 {
        if (!mathcursor)
                return UNDISPATCHED;
 
        BufferView * bv = cmd.view();
-       hideInsetCursor(bv);
-       showInsetCursor(bv);
        bv->updateInset(this);
        //lyxerr << "lfunMouseRelease: buttons: " << cmd.button() << endl;
 
@@ -311,15 +225,17 @@ dispatch_result InsetFormulaBase::lfunMouseRelease(FuncRequest const & cmd)
                if (mathcursor->dispatch(cmd) == UNDISPATCHED) {
                        // launch math panel for right mouse button
                        lyxerr << "lfunMouseRelease: undispatched: " << cmd.button() << endl;
-                       bv->owner()->getDialogs().showMathPanel();
+                       bv->owner()->getDialogs().show("mathpanel");
                }
                return DISPATCHED;
        }
 
        if (cmd.button() == mouse_button::button2) {
+               MathArray ar;
+               asArray(bv->getClipboard(), ar);
                mathcursor->selClear();
                mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
-               mathcursor->insert(asArray(bv->getClipboard()));
+               mathcursor->insert(ar);
                bv->updateInset(this);
                return DISPATCHED;
        }
@@ -349,7 +265,7 @@ dispatch_result InsetFormulaBase::lfunMousePress(FuncRequest const & cmd)
                lyxerr[Debug::MATHED] << "re-create cursor" << endl;
                releaseMathCursor(bv);
                mathcursor = new MathCursor(this, cmd.x == 0);
-               metrics(bv);
+               //metrics(bv);
                mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
        }
 
@@ -394,9 +310,7 @@ dispatch_result InsetFormulaBase::lfunMouseMotion(FuncRequest const & cmd)
                mathcursor->selStart();
 
        BufferView * bv = cmd.view();
-       hideInsetCursor(bv);
        mathcursor->setPos(cmd.x + xo_, cmd.y + yo_);
-       showInsetCursor(bv);
        bv->updateInset(this);
        return DISPATCHED;
 }
@@ -410,21 +324,41 @@ dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
        //      << " y: '" << cmd.y
        //      << "' button: " << cmd.button() << endl;
 
+       BufferView * bv = cmd.view();
+
        // delete empty mathbox (LFUN_BACKSPACE and LFUN_DELETE)
        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 DISPATCHED;
+
                case LFUN_MOUSE_PRESS:
-                       //lyxerr << "Mouse single press\n";
+                       //lyxerr << "Mouse single press" << endl;
                        return lfunMousePress(cmd);
                case LFUN_MOUSE_MOTION:
-                       //lyxerr << "Mouse motion\n";
+                       //lyxerr << "Mouse motion" << endl;
                        return lfunMouseMotion(cmd);
                case LFUN_MOUSE_RELEASE:
-                       //lyxerr << "Mouse single release\n";
+                       //lyxerr << "Mouse single release" << endl;
                        return lfunMouseRelease(cmd);
                case LFUN_MOUSE_DOUBLE:
-                       //lyxerr << "Mouse double\n";
+                       //lyxerr << "Mouse double" << endl;
                        return localDispatch(FuncRequest(LFUN_WORDSEL));
                default:
                        break;
@@ -433,15 +367,12 @@ dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
        if (!mathcursor)
                return UNDISPATCHED;
 
-       BufferView * bv    = cmd.view();
        string argument    = cmd.argument;
        RESULT result      = DISPATCHED;
        bool sel           = false;
        bool was_macro     = mathcursor->inMacroMode();
        bool was_selection = mathcursor->selection();
 
-       hideInsetCursor(bv);
-
        mathcursor->normalize();
        mathcursor->touch();
 
@@ -459,7 +390,7 @@ dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
        case LFUN_TABULAR_FEATURE:
        case LFUN_PASTESELECTION:
        case LFUN_MATH_LIMITS:
-               bv->lockedInsetStoreUndo(Undo::EDIT);
+               recordUndo(bv, Undo::ATOMIC);
                mathcursor->dispatch(cmd);
                break;
 
@@ -467,7 +398,7 @@ dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
                sel = true; // fall through...
        case LFUN_RIGHT:
                result = mathcursor->right(sel) ? DISPATCHED : FINISHED_RIGHT;
-               //lyxerr << "calling scroll 20\n";
+               //lyxerr << "calling scroll 20" << endl;
                //scroll(bv, 20);
                // write something to the minibuffer
                //bv->owner()->message(mathcursor->info());
@@ -543,7 +474,7 @@ dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
 
        case LFUN_DELETE_WORD_BACKWARD:
        case LFUN_BACKSPACE:
-               bv->lockedInsetStoreUndo(Undo::EDIT);
+               recordUndo(bv, Undo::ATOMIC);
                if (!mathcursor->backspace()) {
                        result = FINISHED;
                        remove_inset = true;
@@ -552,7 +483,7 @@ dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
 
        case LFUN_DELETE_WORD_FORWARD:
        case LFUN_DELETE:
-               bv->lockedInsetStoreUndo(Undo::EDIT);
+               recordUndo(bv, Undo::ATOMIC);
                if (!mathcursor->erase()) {
                        result = FINISHED;
                        remove_inset = true;
@@ -564,7 +495,7 @@ dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
        //      dispatch_result = dispatch_buffer;
        //      break;
        case LFUN_SETXY: {
-               lyxerr << "LFUN_SETXY broken!\n";
+               lyxerr << "LFUN_SETXY broken!" << endl;
                int x = 0;
                int y = 0;
                istringstream is(cmd.argument.c_str());
@@ -573,15 +504,19 @@ dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
                break;
        }
 
-       case LFUN_PASTE:
+       case LFUN_PASTE: {
+               size_t n = 0;
+               istringstream is(cmd.argument.c_str());
+               is >> n;
                if (was_macro)
                        mathcursor->macroModeClose();
-               bv->lockedInsetStoreUndo(Undo::EDIT);
-               mathcursor->selPaste();
+               recordUndo(bv, Undo::ATOMIC);
+               mathcursor->selPaste(n);
                break;
+       }
 
        case LFUN_CUT:
-               bv->lockedInsetStoreUndo(Undo::DELETE);
+               recordUndo(bv, Undo::DELETE);
                mathcursor->selCut();
                break;
 
@@ -596,7 +531,7 @@ dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
                if (cmd.argument.empty()) {
                        // do superscript if LyX handles
                        // deadkeys
-                       bv->lockedInsetStoreUndo(Undo::EDIT);
+                       recordUndo(bv, Undo::ATOMIC);
                        mathcursor->script(true);
                }
                break;
@@ -640,14 +575,14 @@ dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
        case LFUN_MATH_SIZE:
 #if 0
                if (!arg.empty()) {
-                       bv->lockedInsetStoreUndo(Undo::EDIT);
+                       recordUndo(bv, Undo::ATOMIC);
                        mathcursor->setSize(arg);
                }
 #endif
                break;
 
        case LFUN_INSERT_MATRIX: {
-               bv->lockedInsetStoreUndo(Undo::EDIT);
+               recordUndo(bv, Undo::ATOMIC);
                unsigned int m = 1;
                unsigned int n = 1;
                string v_align;
@@ -665,14 +600,14 @@ dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
        case LFUN_SUPERSCRIPT:
        case LFUN_SUBSCRIPT:
        {
-               bv->lockedInsetStoreUndo(Undo::EDIT);
+               recordUndo(bv, Undo::ATOMIC);
                mathcursor->script(cmd.action == LFUN_SUPERSCRIPT);
                break;
        }
 
        case LFUN_MATH_DELIM:
        {
-               //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'\n";
+               //lyxerr << "formulabase::LFUN_MATH_DELIM, arg: '" << arg << "'" << endl;
                string ls;
                string rs = split(cmd.argument, ls, ' ');
                // Reasonable default values
@@ -681,14 +616,14 @@ dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
                if (rs.empty())
                        rs = ')';
 
-               bv->lockedInsetStoreUndo(Undo::EDIT);
+               recordUndo(bv, Undo::ATOMIC);
                mathcursor->handleNest(MathAtom(new MathDelimInset(ls, rs)));
                break;
        }
 
-       case LFUN_PROTECTEDSPACE:
+       case LFUN_SPACE_INSERT:
        case LFUN_MATH_SPACE:
-               bv->lockedInsetStoreUndo(Undo::EDIT);
+               recordUndo(bv, Undo::ATOMIC);
                mathcursor->insert(MathAtom(new MathSpaceInset(",")));
                break;
 
@@ -703,7 +638,7 @@ dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
 
        case LFUN_INSET_ERT:
                // interpret this as if a backslash was typed
-               bv->lockedInsetStoreUndo(Undo::EDIT);
+               recordUndo(bv, Undo::ATOMIC);
                mathcursor->interpret('\\');
                break;
 
@@ -717,25 +652,21 @@ dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
 // handling such that "self-insert" works on "arbitrary stuff" too, and
 // math-insert only handles special math things like "matrix".
        case LFUN_INSERT_MATH:
-               bv->lockedInsetStoreUndo(Undo::EDIT);
+               recordUndo(bv, Undo::ATOMIC);
                mathcursor->niceInsert(argument);
                break;
 
        case -1:
        case LFUN_SELFINSERT:
                if (!argument.empty()) {
-                       bv->lockedInsetStoreUndo(Undo::EDIT);
+                       recordUndo(bv, Undo::ATOMIC);
                        if (argument.size() == 1)
                                result = mathcursor->interpret(argument[0]) ? DISPATCHED : FINISHED_RIGHT;
                        else
-                               mathcursor->insert(asArray(argument));
+                               mathcursor->insert(argument);
                }
                break;
 
-       case LFUN_MATH_PANEL:
-               result = UNDISPATCHED;
-               break;
-
        case LFUN_ESCAPE:
                if (mathcursor->selection())
                        mathcursor->selClear();
@@ -747,6 +678,10 @@ dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
                mathcursor->insetToggle();
                break;
 
+       case LFUN_DIALOG_SHOW:
+               result = UNDISPATCHED;
+               break;
+
        case LFUN_DIALOG_SHOW_NEW_INSET: {
                string const & name = argument;
                string data;
@@ -793,7 +728,7 @@ dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
        mathcursor->normalize();
        mathcursor->touch();
 
-       lyx::Assert(mathcursor);
+       Assert(mathcursor);
 
        if (mathcursor->selection() || was_selection)
                toggleInsetSelection(bv);
@@ -801,7 +736,6 @@ dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd)
        if (result == DISPATCHED || result == DISPATCHED_NOUPDATE ||
            result == UNDISPATCHED) {
                fitInsetCursor(bv);
-               showInsetCursor(bv);
                revealCodes(bv);
                cmd.view()->stuffClipboard(mathcursor->grabSelection());
        } else {
@@ -850,21 +784,21 @@ void InsetFormulaBase::revealCodes(BufferView * bv) const
 }
 
 
-Inset::Code InsetFormulaBase::lyxCode() const
+InsetOld::Code InsetFormulaBase::lyxCode() const
 {
-       return Inset::MATH_CODE;
+       return InsetOld::MATH_CODE;
 }
 
 
 int InsetFormulaBase::ylow() const
 {
-       return yo_ - ascent(view(), font_);
+       return yo_ - dim_.asc;
 }
 
 
 int InsetFormulaBase::yhigh() const
 {
-       return yo_ + descent(view(), font_);
+       return yo_ + dim_.des;
 }
 
 
@@ -876,7 +810,7 @@ int InsetFormulaBase::xlow() const
 
 int InsetFormulaBase::xhigh() const
 {
-       return xo_ + width(view(), font_);
+       return xo_ + dim_.wid;
 }
 
 
@@ -895,7 +829,7 @@ bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
        static string laststr;
 
        if (lastformula != this || laststr != str) {
-               //lyxerr << "reset lastformula to " << this << "\n";
+               //lyxerr << "reset lastformula to " << this << endl;
                lastformula = this;
                laststr = str;
                current = ibegin(par().nucleus());
@@ -915,7 +849,7 @@ bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
                        }
                        delete mathcursor;
                        mathcursor = new MathCursor(this, true);
-                       metrics(bv);
+                       //metrics(bv);
                        mathcursor->setSelection(it, ar.size());
                        current = it;
                        it.jump(ar.size());
@@ -924,7 +858,7 @@ bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
                }
        }
 
-       //lyxerr << "not found!\n";
+       //lyxerr << "not found!" << endl;
        lastformula = 0;
        return false;
 }
@@ -933,7 +867,7 @@ bool InsetFormulaBase::searchForward(BufferView * bv, string const & str,
 bool InsetFormulaBase::searchBackward(BufferView * bv, string const & what,
                                      bool a, bool b)
 {
-       lyxerr[Debug::MATHED] << "searching backward not implemented in mathed\n";
+       lyxerr[Debug::MATHED] << "searching backward not implemented in mathed" << endl;
        return searchForward(bv, what, a, b);
 }
 
@@ -984,7 +918,7 @@ void mathDispatchCreation(FuncRequest const & cmd, bool display)
                        f = new InsetFormula(sel);
                else
                        f = new InsetFormulaMacro(sel);
-               bv->getLyXText()->cutSelection(bv);
+               bv->getLyXText()->cutSelection(true, false);
                openNewInset(bv, f);
        }
        cmd.message(N_("Math editor mode"));
@@ -1017,8 +951,10 @@ void mathDispatch(FuncRequest const & cmd)
                        else {
                                string s = cmd.argument;
                                string const s1 = token(s, ' ', 1);
-                               int const na = s1.empty() ? 0 : lyx::atoi(s1);
-                               openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), na));
+                               int const nargs = s1.empty() ? 0 : atoi(s1);
+                               string const s2 = token(s, ' ', 2);
+                               string const type = s2.empty() ? "newcommand" : s2;
+                               openNewInset(bv, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
                        }
                        break;