]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathNest.cpp
simplify GuiToc / TocWidget interaction. Much can still be simplified...
[lyx.git] / src / mathed / InsetMathNest.cpp
index d8c83652798ff1ed45ea172773c38dafaf8b70cb..d63f03f34138d23976bff9b5bd0cfa49e8c2ae9c 100644 (file)
@@ -20,7 +20,6 @@
 #include "InsetMathComment.h"
 #include "InsetMathDelim.h"
 #include "InsetMathHull.h"
-//#include "InsetMathMBox.h"
 #include "InsetMathRef.h"
 #include "InsetMathScript.h"
 #include "InsetMathSpace.h"
 #include "CoordCache.h"
 #include "Cursor.h"
 #include "CutAndPaste.h"
-#include "debug.h"
+#include "support/debug.h"
 #include "DispatchResult.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
 #include "LyXFunc.h"
-#include "gettext.h"
+#include "LyXRC.h"
+#include "support/gettext.h"
 #include "Text.h"
 #include "OutputParams.h"
 
 #include "support/lstrings.h"
 #include "support/textutils.h"
+#include "support/docstream.h"
 
 #include "frontends/Clipboard.h"
 #include "frontends/Painter.h"
@@ -59,6 +60,8 @@
 
 #include <sstream>
 
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 
@@ -68,10 +71,6 @@ using cap::cutSelection;
 using cap::replaceSelection;
 using cap::selClearOrDel;
 
-using std::endl;
-using std::string;
-using std::istringstream;
-
 
 InsetMathNest::InsetMathNest(idx_type nargs)
        : cells_(nargs), lock_(false), mouse_hover_(false)
@@ -111,7 +110,7 @@ void InsetMathNest::cursorPos(BufferView const & bv,
 // to touch all (math)inset's draw() methods. Right now, we'll store
 // absolute value, and make them here relative, only to make them
 // absolute again when actually drawing the cursor. What a mess.
-       BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
+       BOOST_ASSERT(&sl.inset() == this);
        MathData const & ar = sl.cell();
        CoordCache const & coord_cache = bv.coordCache();
        if (!coord_cache.getArrays().has(&ar)) {
@@ -132,8 +131,8 @@ void InsetMathNest::cursorPos(BufferView const & bv,
        }
        Point const pt2 = coord_cache.getInsets().xy(this);
        //lyxerr << "retrieving position cache for MathData "
-       //      << pt.x_ << ' ' << pt.y_ << std::endl;
-       x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
+       //      << pt.x_ << ' ' << pt.y_ << endl;
+       x = pt.x_ - pt2.x_ + ar.pos2x(&bv, sl.pos());
        y = pt.y_ - pt2.y_;
 //     lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
 //             << " asc: " << ascent() << "  des: " << descent()
@@ -156,7 +155,7 @@ void InsetMathNest::metrics(MetricsInfo const & mi) const
 
 bool InsetMathNest::idxNext(Cursor & cur) const
 {
-       BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
+       BOOST_ASSERT(&cur.inset() == this);
        if (cur.idx() == cur.lastidx())
                return false;
        ++cur.idx();
@@ -165,7 +164,7 @@ bool InsetMathNest::idxNext(Cursor & cur) const
 }
 
 
-bool InsetMathNest::idxRight(Cursor & cur) const
+bool InsetMathNest::idxForward(Cursor & cur) const
 {
        return idxNext(cur);
 }
@@ -173,7 +172,7 @@ bool InsetMathNest::idxRight(Cursor & cur) const
 
 bool InsetMathNest::idxPrev(Cursor & cur) const
 {
-       BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
+       BOOST_ASSERT(&cur.inset() == this);
        if (cur.idx() == 0)
                return false;
        --cur.idx();
@@ -182,7 +181,7 @@ bool InsetMathNest::idxPrev(Cursor & cur) const
 }
 
 
-bool InsetMathNest::idxLeft(Cursor & cur) const
+bool InsetMathNest::idxBackward(Cursor & cur) const
 {
        return idxPrev(cur);
 }
@@ -190,7 +189,7 @@ bool InsetMathNest::idxLeft(Cursor & cur) const
 
 bool InsetMathNest::idxFirst(Cursor & cur) const
 {
-       BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
+       BOOST_ASSERT(&cur.inset() == this);
        if (nargs() == 0)
                return false;
        cur.idx() = 0;
@@ -201,7 +200,7 @@ bool InsetMathNest::idxFirst(Cursor & cur) const
 
 bool InsetMathNest::idxLast(Cursor & cur) const
 {
-       BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
+       BOOST_ASSERT(&cur.inset() == this);
        if (nargs() == 0)
                return false;
        cur.idx() = cur.lastidx();
@@ -242,13 +241,14 @@ void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
        Cursor & cur = bv.cursor();
        if (!cur.selection())
                return;
-       if (!ptr_cmp(&cur.inset(), this))
+       if (&cur.inset() != this)
                return;
 
        // FIXME: hack to get position cache warm
+       bool const original_drawing_state = pi.pain.isDrawingEnabled();
        pi.pain.setDrawingEnabled(false);
        draw(pi, x, y);
-       pi.pain.setDrawingEnabled(true);
+       pi.pain.setDrawingEnabled(original_drawing_state);
 
        CursorSlice s1 = cur.selBegin();
        CursorSlice s2 = cur.selEnd();
@@ -258,9 +258,9 @@ void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
        if (s1.idx() == s2.idx()) {
                MathData const & c = cell(s1.idx());
                Geometry const & g = bv.coordCache().getArrays().geometry(&c);
-               int x1 = g.pos.x_ + c.pos2x(s1.pos());
+               int x1 = g.pos.x_ + c.pos2x(pi.base.bv, s1.pos());
                int y1 = g.pos.y_ - g.dim.ascent();
-               int x2 = g.pos.x_ + c.pos2x(s2.pos());
+               int x2 = g.pos.x_ + c.pos2x(pi.base.bv, s2.pos());
                int y2 = g.pos.y_ + g.dim.descent();
                pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
        //lyxerr << "InsetMathNest::drawing selection 3: "
@@ -441,7 +441,7 @@ void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
 
 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
-       //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
+       //lyxerr << "InsetMathNest: request: " << cmd << endl;
        //CursorSlice sl = cur.current();
 
        switch (cmd.action) {
@@ -491,79 +491,79 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                lfunMouseRelease(cur, cmd);
                break;
 
+       case LFUN_FINISHED_LEFT: // in math, left is backwards
        case LFUN_FINISHED_BACKWARD:
                cur.bv().cursor() = cur;
                break;
 
+       case LFUN_FINISHED_RIGHT: // in math, right is forward
        case LFUN_FINISHED_FORWARD:
                ++cur.pos();
                cur.bv().cursor() = cur;
                break;
 
+       case LFUN_CHAR_RIGHT:
+       case LFUN_CHAR_LEFT:
+       case LFUN_CHAR_BACKWARD:
        case LFUN_CHAR_FORWARD:
                cur.updateFlags(Update::Decoration | Update::FitCursor);
-       case LFUN_CHAR_FORWARD_SELECT:
-               cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
-               cur.autocorrect() = false;
-               cur.clearTargetX();
-               cur.macroModeClose();
-               if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
-                       cur.pushLeft(*cur.nextAtom().nucleus());
-                       cur.inset().idxFirst(cur);
-               } else if (cur.posRight() || idxRight(cur)
-                       || cur.popRight() || cur.selection())
-                       ;
+       case LFUN_CHAR_RIGHT_SELECT:
+       case LFUN_CHAR_LEFT_SELECT:
+       case LFUN_CHAR_BACKWARD_SELECT:
+       case LFUN_CHAR_FORWARD_SELECT: {
+               // are we in a selection?
+               bool select = (cmd.action == LFUN_CHAR_RIGHT_SELECT 
+                                          || cmd.action == LFUN_CHAR_LEFT_SELECT
+                                          || cmd.action == LFUN_CHAR_BACKWARD_SELECT
+                                          || cmd.action == LFUN_CHAR_FORWARD_SELECT);
+               // are we moving forward or backwards? 
+               // If the command was RIGHT or LEFT, then whether we're moving forward
+               // or backwards depends on the cursor movement mode (logical or visual):
+               //  * in visual mode, since math is always LTR, right -> forward, 
+               //    left -> backwards
+               //  * in logical mode, the mapping is determined by the
+               //    reverseDirectionNeeded() function
+               
+               bool forward;
+               kb_action finish_lfun;
+
+               if (cmd.action == LFUN_CHAR_FORWARD 
+                               || cmd.action == LFUN_CHAR_FORWARD_SELECT) {
+                       forward = true;
+                       finish_lfun = LFUN_FINISHED_FORWARD;
+               }
+               else if (cmd.action == LFUN_CHAR_BACKWARD
+                               || cmd.action == LFUN_CHAR_BACKWARD_SELECT) {
+                       forward = false;
+                       finish_lfun = LFUN_FINISHED_BACKWARD;
+               }
                else {
-                       cmd = FuncRequest(LFUN_FINISHED_FORWARD);
-                       cur.undispatched();
+                       bool right = (cmd.action == LFUN_CHAR_RIGHT_SELECT
+                                                 || cmd.action == LFUN_CHAR_RIGHT);
+                       if (lyxrc.visual_cursor || !reverseDirectionNeeded(cur))
+                               forward = right;
+                       else 
+                               forward = !right;
+
+                       if (right)
+                               finish_lfun = LFUN_FINISHED_RIGHT;
+                       else
+                               finish_lfun = LFUN_FINISHED_LEFT;
                }
-               break;
-
-       case LFUN_CHAR_BACKWARD:
-               cur.updateFlags(Update::Decoration | Update::FitCursor);
-       case LFUN_CHAR_BACKWARD_SELECT:
-               cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
+               // Now that we know exactly what we want to do, let's do it!
+               cur.selHandle(select);
                cur.autocorrect() = false;
                cur.clearTargetX();
                cur.macroModeClose();
-               if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
-                       cur.posLeft();
-                       cur.push(*cur.nextAtom().nucleus());
-                       cur.inset().idxLast(cur);
-               } else if (cur.posLeft() || idxLeft(cur)
-                       || cur.popLeft() || cur.selection())
-                       ;
-               else {
-                       cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
+               // try moving forward or backwards as necessary...
+               if (!(forward ? cursorMathForward(cur) : cursorMathBackward(cur))) {
+                       // ... and if movement failed, then finish forward or backwards
+                       // as necessary
+                       cmd = FuncRequest(finish_lfun);
                        cur.undispatched();
                }
                break;
-
-       case LFUN_CHAR_RIGHT:
-       case LFUN_CHAR_RIGHT_SELECT:
-               //FIXME: for visual cursor, really move right
-               if (reverseDirectionNeeded(cur))
-                       lyx::dispatch(FuncRequest(
-                               cmd.action == LFUN_CHAR_RIGHT_SELECT ? 
-                                       LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
-               else 
-                       lyx::dispatch(FuncRequest(
-                               cmd.action == LFUN_CHAR_RIGHT_SELECT ? 
-                                       LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
-               break;
-
-       case LFUN_CHAR_LEFT:
-       case LFUN_CHAR_LEFT_SELECT:
-               //FIXME: for visual cursor, really move left
-               if (reverseDirectionNeeded(cur))
-                       lyx::dispatch(FuncRequest(
-                               cmd.action == LFUN_CHAR_LEFT_SELECT ? 
-                                       LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
-               else 
-                       lyx::dispatch(FuncRequest(
-                               cmd.action == LFUN_CHAR_LEFT_SELECT ? 
-                                       LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
-               break;
+       }
 
        case LFUN_DOWN:
        case LFUN_UP:
@@ -727,10 +727,12 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                }
                break;
 
+       // 'Locks' the math inset. A 'locked' math inset behaves as a unit
+       // that is traversed by a single <CursorLeft>/<CursorRight>.
        case LFUN_INSET_TOGGLE:
                cur.recordUndo();
                lock(!lock());
-               cur.popRight();
+               cur.popForward();
                break;
 
        case LFUN_SELF_INSERT:
@@ -761,8 +763,8 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                    && cur.macroModeClose()) {
                        MathAtom const atom = cur.prevAtom();
                        if (atom->asNestInset() && atom->isActive()) {
-                               cur.posLeft();
-                               cur.pushLeft(*cur.nextInset());
+                               cur.posBackward();
+                               cur.pushBackward(*cur.nextInset());
                        }
                } else if (!interpretChar(cur, cmd.argument()[0])) {
                        cmd = FuncRequest(LFUN_FINISHED_FORWARD);
@@ -882,8 +884,8 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                selClearOrDel(cur);
                //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
                cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
-               cur.posLeft();
-               cur.pushLeft(*cur.nextInset());
+               cur.posBackward();
+               cur.pushBackward(*cur.nextInset());
                cur.niceInsert(save_selection);
 #else
                if (currentMode() == Inset::TEXT_MODE) {
@@ -924,7 +926,7 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_MATH_DELIM: {
                docstring ls;
-               docstring rs = support::split(cmd.argument(), ls, ' ');
+               docstring rs = split(cmd.argument(), ls, ' ');
                // Reasonable default values
                if (ls.empty())
                        ls = '(';
@@ -995,12 +997,13 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                bool fold = cmd.action == LFUN_MATH_MACRO_FOLD;
                bool found = findMacroToFoldUnfold(it, fold);
                if (found) {
-                       cur.recordUndo();
+                       MathMacro * macro = it.nextInset()->asInsetMath()->asMacro();
+                       cur.recordUndoInset();
                        if (fold)
-                               it.nextInset()->asInsetMath()->asMacro()->fold(cur);
+                               macro->fold(cur);
                        else
-                               it.nextInset()->asInsetMath()->asMacro()->unfold(cur);
-               }\v
+                               macro->unfold(cur);
+               }
                break;
        }
 
@@ -1029,7 +1032,7 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                        InsetMathRef tmp(name);
                        data = tmp.createDialogStr(to_utf8(name));
                }
-               cur.bv().showInsetDialog(to_utf8(name), data, 0);
+               cur.bv().showDialog(to_utf8(name), data);
                break;
        }
 
@@ -1069,8 +1072,11 @@ bool InsetMathNest::findMacroToFoldUnfold(Cursor & it, bool fold) const {
                                if (macro->folded() != fold)
                                        return true;
                                
-                               // wrong folding state -> go up one level
-                               break;
+                               // Wrong folding state.
+                               // If this was the first we see in this slice, look further left,
+                               // otherwise go up.
+                               if (inset != it.nextInset())
+                                       break;
                        }
                        
                        // go up if this was the left most position
@@ -1138,8 +1144,6 @@ bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
        case LFUN_MATH_SIZE:
        case LFUN_MATH_SPACE:
        case LFUN_MATH_LIMITS:
-       case LFUN_MATH_NONUMBER:
-       case LFUN_MATH_NUMBER:
        case LFUN_MATH_EXTERN:
                flag.enabled(true);
                break;
@@ -1187,11 +1191,7 @@ bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
                break;
        }
                
-       case LFUN_HYPHENATION_POINT_INSERT:
-       case LFUN_LIGATURE_BREAK_INSERT:
-       case LFUN_MENU_SEPARATOR_INSERT:
-       case LFUN_DOTS_INSERT:
-       case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
+       case LFUN_SPECIALCHAR_INSERT:
                // FIXME: These would probably make sense in math-text mode
                flag.enabled(false);
                break;
@@ -1208,11 +1208,13 @@ bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
 }
 
 
-void InsetMathNest::edit(Cursor & cur, bool left)
+void InsetMathNest::edit(Cursor & cur, bool front, EntryDirection entry_from)
 {
        cur.push(*this);
-       cur.idx() = left ? 0 : cur.lastidx();
-       cur.pos() = left ? 0 : cur.lastpos();
+       bool enter_front = (entry_from == Inset::ENTRY_DIRECTION_RIGHT || 
+               (entry_from == Inset::ENTRY_DIRECTION_IGNORE && front));
+       cur.idx() = enter_front ? 0 : cur.lastidx();
+       cur.pos() = enter_front ? 0 : cur.lastpos();
        cur.resetAnchor();
        //lyxerr << "InsetMathNest::edit, cur:\n" << cur << endl;
 }
@@ -1232,7 +1234,7 @@ Inset * InsetMathNest::editXY(Cursor & cur, int x, int y)
        MathData & ar = cell(idx_min);
        cur.push(*this);
        cur.idx() = idx_min;
-       cur.pos() = ar.x2pos(x - ar.xo(cur.bv()));
+       cur.pos() = ar.x2pos(&cur.bv(), x - ar.xo(cur.bv()));
 
        //lyxerr << "found cell : " << idx_min << " pos: " << cur.pos() << endl;
        if (dist_min == 0) {
@@ -1461,7 +1463,7 @@ bool InsetMathNest::interpretChar(Cursor & cur, char_type c)
                        return true;
                }
 
-               if (cur.popRight()) {
+               if (cur.popForward()) {
                        // FIXME: we have to enable full redraw here because of the
                        // visual box corners that define the inset. If we know for
                        // sure that we stay within the same cell we can optimize for
@@ -1585,4 +1587,41 @@ bool InsetMathNest::script(Cursor & cur, bool up,
 }
 
 
+bool InsetMathNest::cursorMathForward(Cursor & cur)
+{
+       if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
+               cur.pushBackward(*cur.nextAtom().nucleus());
+               cur.inset().idxFirst(cur);
+               return true;
+       } 
+       if (cur.posForward() || idxForward(cur) || cur.selection())
+               return true;
+       // try to pop forwards --- but don't pop out of math! leave that to
+       // the FINISH lfuns
+       int s = cur.depth() - 2;
+       if (s >= 0 && cur[s].inset().asInsetMath())
+               return cur.popForward();
+       return false;
+}
+
+
+bool InsetMathNest::cursorMathBackward(Cursor & cur)
+{
+       if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
+               cur.posBackward();
+               cur.push(*cur.nextAtom().nucleus());
+               cur.inset().idxLast(cur);
+               return true;
+       } 
+       if (cur.posBackward() || idxBackward(cur) || cur.selection())
+               return true;
+       // try to pop backwards --- but don't pop out of math! leave that to 
+       // the FINISH lfuns
+       int s = cur.depth() - 2;
+       if (s >= 0 && cur[s].inset().asInsetMath())
+               return cur.popBackward();
+       return false;
+}
+
+
 } // namespace lyx