-}
-
-
-bool LCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh)
-{
- CursorBase best_cursor;
- double best_dist = 1e10;
-
- CursorBase it = ibegin(formula());
- CursorBase et = iend(formula());
- while (1) {
- // avoid invalid nesting when selecting
- if (!selection() || positionable(it, anchor_)) {
- int xo, yo;
- CursorSlice & cur = it.back();
- cur.inset()->getCursorPos(cur, xo, yo);
- if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) {
- double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
- //lyxerr << "x: " << x << " y: " << y << " d: " << endl;
- // '<=' in order to take the last possible position
- // this is important for clicking behind \sum in e.g. '\sum_i a'
- if (d <= best_dist) {
- best_dist = d;
- best_cursor = it;
- }
- }
- }
-
- if (it == et)
- break;
- increment(it);
- }
-
- if (best_dist < 1e10)
- cursor_ = best_cursor;
- return best_dist < 1e10;
-}
-
-
-void LCursor::bruteFind2(int x, int y)
-{
- double best_dist = 1e10;
-
- CursorBase it = cursor_;
- it.back().pos(0);
- CursorBase et = cursor_;
- int n = et.back().asMathInset()->cell(et.back().idx_).size();
- et.back().pos(n);
- for (int i = 0; ; ++i) {
- int xo, yo;
- CursorSlice & cur = it.back();
- cur.inset()->getCursorPos(cur, xo, yo);
- double d = (x - xo) * (x - xo) + (y - yo) * (y - yo);
- // '<=' in order to take the last possible position
- // this is important for clicking behind \sum in e.g. '\sum_i a'
- lyxerr << "i: " << i << " d: " << d << " best: " << best_dist << endl;
- if (d <= best_dist) {
- best_dist = d;
- cursor_ = it;
- }
- if (it == et)
- break;
- increment(it);
- }
-}
-
-
-bool LCursor::idxLineLast()
-{
- idx() -= idx() % ncols();
- idx() += ncols() - 1;
- pos() = lastpos();
- return true;
-}
-
-
-bool LCursor::idxLeft()
-{
- return inset()->idxLeft(*this);
-}
-
-
-bool LCursor::idxRight()
-{
- return inset()->idxRight(*this);
-}
-
-
-bool LCursor::script(bool up)
-{
- // Hack to get \\^ and \\_ working
- lyxerr << "handling script: up: " << up << endl;
- if (inMacroMode() && macroName() == "\\") {
- if (up)
- niceInsert(createMathInset("mathcircumflex"));
- else
- interpret('_');
- return true;
- }
-
- macroModeClose();
- string safe = grabAndEraseSelection();
- if (inNucleus()) {
- // we are in a nucleus of a script inset, move to _our_ script
- inset()->asMathInset()->asScriptInset()->ensure(up);
- idx() = up;
- pos() = 0;
- } else if (pos() != 0 && prevAtom()->asScriptInset()) {
- --pos();
- nextAtom().nucleus()->asScriptInset()->ensure(up);
- push(nextInset());
- idx() = up;
- pos() = lastpos();
- } else if (pos() != 0) {
- --pos();
- cell()[pos()] = MathAtom(new MathScriptInset(nextAtom(), up));
- push(nextInset());
- idx() = up;
- pos() = 0;
- } else {
- plainInsert(MathAtom(new MathScriptInset(up)));
- --pos();
- nextAtom().nucleus()->asScriptInset()->ensure(up);
- push(nextInset());
- idx() = up;
- pos() = 0;
- }
- paste(safe);
- return true;
-}
-
-
-bool LCursor::interpret(char c)
-{
- //lyxerr << "interpret 2: '" << c << "'" << endl;
- clearTargetX();
- if (inMacroArgMode()) {
- posLeft();
- plainErase();
-#warning FIXME
-#if 0
- int n = c - '0';
- MathMacroTemplate const * p = formula()->asMacroTemplate();
- if (p && 1 <= n && n <= p->numargs())
- insert(MathAtom(new MathMacroArgument(c - '0')));
- else {
- insert(createMathInset("#"));
- interpret(c); // try again
- }
-#endif
- return true;
- }
-
- // handle macroMode
- if (inMacroMode()) {
- string name = macroName();
- //lyxerr << "interpret name: '" << name << "'" << endl;
-
- if (isalpha(c)) {
- activeMacro()->setName(activeMacro()->name() + c);
- return true;
- }
-
- // handle 'special char' macros
- if (name == "\\") {
- // remove the '\\'
- backspace();
- if (c == '\\') {
- if (currentMode() == MathInset::TEXT_MODE)
- niceInsert(createMathInset("textbackslash"));
- else
- niceInsert(createMathInset("backslash"));
- } else if (c == '{') {
- niceInsert(MathAtom(new MathBraceInset));
- } else {
- niceInsert(createMathInset(string(1, c)));
- }
- return true;
- }
-
- // leave macro mode and try again if necessary
- macroModeClose();
- if (c == '{')
- niceInsert(MathAtom(new MathBraceInset));
- else if (c != ' ')
- interpret(c);
- return true;
- }
-
- // This is annoying as one has to press <space> far too often.
- // Disable it.
-
- if (0) {
- // leave autocorrect mode if necessary
- if (autocorrect() && c == ' ') {
- autocorrect() = false;
- return true;
- }
- }
-
- // just clear selection on pressing the space bar
- if (selection() && c == ' ') {
- selection() = false;
- return true;
- }
-
- selClearOrDel();
-
- if (c == '\\') {
- //lyxerr << "starting with macro" << endl;
- insert(MathAtom(new MathUnknownInset("\\", false)));
- return true;
- }
-
- if (c == '\n') {
- if (currentMode() == MathInset::TEXT_MODE)
- insert(c);
- return true;
- }
-
- if (c == ' ') {
- if (currentMode() == MathInset::TEXT_MODE) {
- // insert spaces in text mode,
- // but suppress direct insertion of two spaces in a row
- // the still allows typing '<space>a<space>' and deleting the 'a', but
- // it is better than nothing...
- if (!pos() != 0 || prevAtom()->getChar() != ' ')
- insert(c);
- return true;
- }
- if (pos() != 0 && prevAtom()->asSpaceInset()) {
- prevAtom().nucleus()->asSpaceInset()->incSpace();
- return true;
- }
- if (popRight())
- return true;
- // if are at the very end, leave the formula
- return pos() != lastpos();
- }
-
- if (c == '_') {
- script(false);
- return true;
- }
-
- if (c == '^') {
- script(true);
- return true;
- }
-
- if (c == '{' || c == '}' || c == '#' || c == '&' || c == '$') {
- niceInsert(createMathInset(string(1, c)));
- return true;
- }
-
- if (c == '%') {
- niceInsert(MathAtom(new MathCommentInset));
- return true;
- }
-
- // try auto-correction
- //if (autocorrect() && hasPrevAtom() && math_autocorrect(prevAtom(), c))
- // return true;
-
- // no special circumstances, so insert the character without any fuss
- insert(c);
- autocorrect() = true;
- return true;
-}
-
-
-void LCursor::lockToggle()
-{
- if (pos() != lastpos()) {
- // toggle previous inset ...
- nextAtom().nucleus()->lock(!nextAtom()->lock());
- } else if (popLeft() && pos() != lastpos()) {
- // ... or enclosing inset if we are in the last inset position
- nextAtom().nucleus()->lock(!nextAtom()->lock());
- ++pos();
- }
-}
-
-
-CursorSlice LCursor::normalAnchor()
-{
- if (anchor_.size() < depth()) {
- resetAnchor();
- lyxerr << "unusual Anchor size" << endl;
- }
- //lyx::BOOST_ASSERT(Anchor_.size() >= cursor.depth());
- // use Anchor on the same level as Cursor
- CursorSlice normal = anchor_[current_];
-#if 0
- if (depth() < anchor_.size() && !(normal < xx())) {
- // anchor is behind cursor -> move anchor behind the inset
- ++normal.pos_;
- }
-#endif
- return normal;
-}