]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_nestinset.C
Andreas' patch to prevent crash on click on previewd inset
[lyx.git] / src / mathed / math_nestinset.C
index 79ca87f7799b544ca00724d277bca077742ad453..a71327a84e455bbe11473dd3744f4dc27cc79d51 100644 (file)
@@ -15,6 +15,7 @@
 #include "math_arrayinset.h"
 #include "math_boxinset.h"
 #include "math_braceinset.h"
+#include "math_colorinset.h"
 #include "math_commentinset.h"
 #include "math_data.h"
 #include "math_deliminset.h"
@@ -87,7 +88,7 @@ MathArray const & MathNestInset::cell(idx_type i) const
 }
 
 
-void MathNestInset::getCursorPos(CursorSlice const & sl,
+void MathNestInset::cursorPos(CursorSlice const & sl, bool boundary,
        int & x, int & y) const
 {
 // FIXME: This is a hack. Ideally, the coord cache should not store
@@ -394,9 +395,8 @@ void MathNestInset::handleFont2(LCursor & cur, string const & arg)
        bool b;
        bv_funcs::string2font(arg, font, b);
        if (font.color() != LColor::inherit) {
-               MathAtom at = createMathInset("color");
-               asArray(lcolor.getGUIName(font.color()), at.nucleus()->cell(0));
-               cur.handleNest(at, 1);
+               MathAtom at = MathAtom(new MathColorInset(true, font.color()));
+               cur.handleNest(at, 0);
        }
 }
 
@@ -458,6 +458,7 @@ void MathNestInset::doDispatch(LCursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_FINISHED_DOWN:
+               ++cur.pos();
                cur.bv().cursor() = cur;
                break;
 
@@ -473,8 +474,10 @@ void MathNestInset::doDispatch(LCursor & cur, FuncRequest & cmd)
                } else if (cur.posRight() || idxRight(cur)
                        || cur.popRight() || cur.selection())
                        ;
-               else
+               else {
                        cmd = FuncRequest(LFUN_FINISHED_RIGHT);
+                       cur.undispatched();
+               }
                break;
 
        case LFUN_LEFTSEL:
@@ -490,8 +493,10 @@ void MathNestInset::doDispatch(LCursor & cur, FuncRequest & cmd)
                } else if (cur.posLeft() || idxLeft(cur)
                        || cur.popLeft() || cur.selection())
                        ;
-               else
+               else {
                        cmd = FuncRequest(LFUN_FINISHED_LEFT);
+                       cur.undispatched();
+               }
                break;
 
        case LFUN_UPSEL:
@@ -503,8 +508,10 @@ void MathNestInset::doDispatch(LCursor & cur, FuncRequest & cmd)
                        break;
                }
                cur.selHandle(cmd.action == LFUN_UPSEL);
-               if (!cur.up())
+               if (!cur.up()) {
                        cmd = FuncRequest(LFUN_FINISHED_UP);
+                       cur.undispatched();
+               }
                // fixes bug 1598. Please check!
                cur.normalize();
                break;
@@ -516,8 +523,10 @@ void MathNestInset::doDispatch(LCursor & cur, FuncRequest & cmd)
                        break;
                }
                cur.selHandle(cmd.action == LFUN_DOWNSEL);
-               if (!cur.down())
+               if (!cur.down()) {
                        cmd = FuncRequest(LFUN_FINISHED_DOWN);
+                       cur.undispatched();
+               }
                // fixes bug 1598. Please check!
                cur.normalize();
                break;
@@ -555,6 +564,7 @@ void MathNestInset::doDispatch(LCursor & cur, FuncRequest & cmd)
                        cur.pos() = 0;
                } else {
                        cmd = FuncRequest(LFUN_FINISHED_LEFT);
+                       cur.undispatched();
                }
                break;
 
@@ -575,17 +585,20 @@ void MathNestInset::doDispatch(LCursor & cur, FuncRequest & cmd)
                        cur.pos() = cur.lastpos();
                } else {
                        cmd = FuncRequest(LFUN_FINISHED_RIGHT);
+                       cur.undispatched();
                }
                break;
 
        case LFUN_PRIORSEL:
        case LFUN_PRIOR:
                cmd = FuncRequest(LFUN_FINISHED_LEFT);
+               cur.undispatched();
                break;
 
        case LFUN_NEXTSEL:
        case LFUN_NEXT:
                cmd = FuncRequest(LFUN_FINISHED_RIGHT);
+               cur.undispatched();
                break;
 
        case LFUN_CELL_FORWARD:
@@ -607,13 +620,16 @@ void MathNestInset::doDispatch(LCursor & cur, FuncRequest & cmd)
                recordUndo(cur);
                cur.erase();
                cmd = FuncRequest(LFUN_FINISHED_LEFT);
+               cur.undispatched();
                break;
 
        case LFUN_ESCAPE:
                if (cur.selection())
                        cur.clearSelection();
-               else
+               else  {
                        cmd = FuncRequest(LFUN_FINISHED_LEFT);
+                       cur.undispatched();
+               }
                break;
 
        case LFUN_INSET_TOGGLE:
@@ -635,8 +651,10 @@ void MathNestInset::doDispatch(LCursor & cur, FuncRequest & cmd)
                        cur.insert(cmd.argument);
                        break;
                }
-               if (!interpret(cur, cmd.argument[0]))
+               if (!interpret(cur, cmd.argument[0])) {
                        cmd = FuncRequest(LFUN_FINISHED_RIGHT);
+                       cur.undispatched();
+               }
                break;
 
        //case LFUN_GETXY:
@@ -687,28 +705,50 @@ void MathNestInset::doDispatch(LCursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_BOLD:
-               handleFont(cur, cmd.argument, "mathbf");
+               if (currentMode() == TEXT_MODE)
+                       handleFont(cur, cmd.argument, "textbf");
+               else
+                       handleFont(cur, cmd.argument, "mathbf");
                break;
        case LFUN_SANS:
-               handleFont(cur, cmd.argument, "mathsf");
+               if (currentMode() == TEXT_MODE)
+                       handleFont(cur, cmd.argument, "textsf");
+               else
+                       handleFont(cur, cmd.argument, "mathsf");
                break;
        case LFUN_EMPH:
-               handleFont(cur, cmd.argument, "mathcal");
+               if (currentMode() == TEXT_MODE)
+                       handleFont(cur, cmd.argument, "emph");
+               else
+                       handleFont(cur, cmd.argument, "mathcal");
                break;
        case LFUN_ROMAN:
-               handleFont(cur, cmd.argument, "mathrm");
+               if (currentMode() == TEXT_MODE)
+                       handleFont(cur, cmd.argument, "textrm");
+               else
+                       handleFont(cur, cmd.argument, "mathrm");
                break;
        case LFUN_CODE:
-               handleFont(cur, cmd.argument, "texttt");
+               if (currentMode() == TEXT_MODE)
+                       handleFont(cur, cmd.argument, "texttt");
+               else
+                       handleFont(cur, cmd.argument, "mathtt");
                break;
        case LFUN_FRAK:
                handleFont(cur, cmd.argument, "mathfrak");
                break;
        case LFUN_ITAL:
-               handleFont(cur, cmd.argument, "mathit");
+               if (currentMode() == TEXT_MODE)
+                       handleFont(cur, cmd.argument, "textit");
+               else
+                       handleFont(cur, cmd.argument, "mathit");
                break;
        case LFUN_NOUN:
-               handleFont(cur, cmd.argument, "mathbb");
+               if (currentMode() == TEXT_MODE)
+                       // FIXME: should be "noun"
+                       handleFont(cur, cmd.argument, "textsc");
+               else
+                       handleFont(cur, cmd.argument, "mathbb");
                break;
        //case LFUN_FREEFONT_APPLY:
                handleFont(cur, cmd.argument, "textrm");
@@ -809,8 +849,11 @@ void MathNestInset::doDispatch(LCursor & cur, FuncRequest & cmd)
                recordUndo(cur, Undo::ATOMIC);
                MathArray ar;
                asArray(cmd.argument, ar);
+               int cell(0);
+               if (cmd.argument == "\\root")
+                       cell = 1;
                if (ar.size() == 1 && (ar[0].nucleus()->asNestInset())) {
-                       cur.handleNest(ar[0]);
+                       cur.handleNest(ar[0], cell);
                } else
                        cur.niceInsert(cmd.argument);
                break;
@@ -860,7 +903,11 @@ bool MathNestInset::getStatus(LCursor & /*cur*/, FuncRequest const & cmd,
        // the font related toggles
        //string tc = "mathnormal";
        bool ret = true;
+       string const arg = cmd.argument;
        switch (cmd.action) {
+       case LFUN_TABULAR_FEATURE:
+               flag.enabled(false);
+               break;
 #if 0
        case LFUN_TABULAR_FEATURE:
                // FIXME: check temporarily disabled
@@ -880,28 +927,18 @@ bool MathNestInset::getStatus(LCursor & /*cur*/, FuncRequest const & cmd,
                }
                flag.setOnOff(cmd.argument[0] == align);
                break;
+#endif
+       /// We have to handle them since 1.4 blocks all unhandled actions
+       case LFUN_ITAL:
        case LFUN_BOLD:
-               flag.setOnOff(tc == "mathbf");
-               break;
        case LFUN_SANS:
-               flag.setOnOff(tc == "mathsf");
-               break;
        case LFUN_EMPH:
-               flag.setOnOff(tc == "mathcal");
-               break;
-       case LFUN_ROMAN:
-               flag.setOnOff(tc == "mathrm");
-               break;
        case LFUN_CODE:
-               flag.setOnOff(tc == "mathtt");
-               break;
        case LFUN_NOUN:
-               flag.setOnOff(tc == "mathbb");
-               break;
+       case LFUN_ROMAN:
        case LFUN_DEFAULT:
-               flag.setOnOff(tc == "mathnormal");
+               flag.enabled(true);
                break;
-#endif
        case LFUN_MATH_MUTATE:
                //flag.setOnOff(mathcursor::formula()->hullType() == cmd.argument);
                flag.setOnOff(false);
@@ -917,6 +954,25 @@ bool MathNestInset::getStatus(LCursor & /*cur*/, FuncRequest const & cmd,
                flag.enabled(true);
                break;
 
+       case LFUN_FRAK:
+               flag.enabled(currentMode() != TEXT_MODE);
+               break;
+
+       case LFUN_INSERT_MATH: {
+               bool const textarg =
+                       arg == "\\textbf"   || arg == "\\textsf" ||
+                       arg == "\\textrm"   || arg == "\\textmd" ||
+                       arg == "\\textit"   || arg == "\\textsc" ||
+                       arg == "\\textsl"   || arg == "\\textup" ||
+                       arg == "\\texttt"   || arg == "\\textbb" ||
+                       arg == "\\textnormal";
+               flag.enabled(currentMode() != TEXT_MODE || textarg);
+               break;
+       }
+
+       case LFUN_INSERT_MATRIX:
+               flag.enabled(currentMode() == MATH_MODE);
+               break;
        default:
                ret = false;
                break;
@@ -931,33 +987,33 @@ void MathNestInset::edit(LCursor & cur, bool left)
        cur.idx() = left ? 0 : cur.lastidx();
        cur.pos() = left ? 0 : cur.lastpos();
        cur.resetAnchor();
-       lyxerr << "MathNestInset::edit, cur:\n" << cur << endl;
+       //lyxerr << "MathNestInset::edit, cur:\n" << cur << endl;
 }
 
 
-InsetBase * MathNestInset::editXY(LCursor & cur, int x, int y) const
+InsetBase * MathNestInset::editXY(LCursor & cur, int x, int y)
 {
        int idx_min = 0;
        int dist_min = 1000000;
-       for (idx_type i = 0; i < nargs(); ++i) {
-               int d = cell(i).dist(x, y);
+       for (idx_type i = 0, n = nargs(); i != n; ++i) {
+               int const d = cell(i).dist(x, y);
                if (d < dist_min) {
                        dist_min = d;
                        idx_min = i;
                }
        }
-       MathArray const & ar = cell(idx_min);
-       cur.push(const_cast<MathNestInset&>(*this));
+       MathArray & ar = cell(idx_min);
+       cur.push(*this);
        cur.idx() = idx_min;
        cur.pos() = ar.x2pos(x - ar.xo());
-       lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
+       //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
        if (dist_min == 0) {
                // hit inside cell
                for (pos_type i = 0, n = ar.size(); i < n; ++i)
                        if (ar[i]->covers(x, y))
                                return ar[i].nucleus()->editXY(cur, x, y);
        }
-       return const_cast<MathNestInset*>(this);
+       return this;
 }
 
 
@@ -1025,7 +1081,7 @@ void MathNestInset::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
 
 bool MathNestInset::interpret(LCursor & cur, char c)
 {
-       lyxerr << "interpret 2: '" << c << "'" << endl;
+       //lyxerr << "interpret 2: '" << c << "'" << endl;
        cur.clearTargetX();
 
        // handle macroMode
@@ -1131,25 +1187,28 @@ bool MathNestInset::interpret(LCursor & cur, char c)
                return cur.pos() != cur.lastpos();
        }
 
-       if (c == '_') {
-               script(cur, false);
-               return true;
-       }
-
-       if (c == '^') {
-               script(cur, true);
-               return true;
+       // These shouldn't work in text mode:
+       if (currentMode() != MathInset::TEXT_MODE) {
+               if (c == '_') {
+                       script(cur, false);
+                       return true;
+               }
+               if (c == '^') {
+                       script(cur, true);
+                       return true;
+               }
+               if (c == '~') {
+                       cur.niceInsert(createMathInset("sim"));
+                       return true;
+               }
        }
 
-       if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' || c == '%') {
+       if (c == '{' || c == '}' || c == '&' || c == '$' || c == '#' || c == '%'
+      || c == '_' || c == '^') {
                cur.niceInsert(createMathInset(string(1, c)));
                return true;
        }
 
-       if (c == '~') {
-               cur.niceInsert(createMathInset("sim"));
-               return true;
-       }
 
        // try auto-correction
        //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
@@ -1193,10 +1252,10 @@ bool MathNestInset::script(LCursor & cur, bool up)
                // convert the thing to our left to a scriptinset or create a new
                // one if in the very first position of the array
                if (cur.pos() == 0) {
-                       lyxerr << "new scriptinset" << endl;
+                       //lyxerr << "new scriptinset" << endl;
                        cur.insert(new MathScriptInset(up));
                } else {
-                       lyxerr << "converting prev atom " << endl;
+                       //lyxerr << "converting prev atom " << endl;
                        cur.prevAtom() = MathAtom(new MathScriptInset(cur.prevAtom(), up));
                }
                --cur.pos();
@@ -1205,9 +1264,9 @@ bool MathNestInset::script(LCursor & cur, bool up)
                cur.idx() = 1;
                cur.pos() = 0;
        }
-       lyxerr << "pasting 1: safe:\n" << safe << endl;
+       //lyxerr << "pasting 1: safe:\n" << safe << endl;
        cur.paste(safe);
        cur.resetAnchor();
-       lyxerr << "pasting 2: safe:\n" << safe << endl;
+       //lyxerr << "pasting 2: safe:\n" << safe << endl;
        return true;
 }