From e52ac5df2b50469954a3f28b661a4d0a0231e2c1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Tue, 11 Dec 2001 18:51:47 +0000 Subject: [PATCH] more cursor up/down tuning... git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@3194 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/mathed/math_cursor.C | 113 ++++++++++++++++++------------------ src/mathed/math_cursor.h | 8 +-- src/mathed/math_gridinset.C | 6 -- 3 files changed, 60 insertions(+), 67 deletions(-) diff --git a/src/mathed/math_cursor.C b/src/mathed/math_cursor.C index 79176cae1d..b6f5cc6f85 100644 --- a/src/mathed/math_cursor.C +++ b/src/mathed/math_cursor.C @@ -316,6 +316,22 @@ void MathCursor::last() } +bool positionable(MathCursor::cursor_type const & cursor, + MathCursor::cursor_type const & anchor) +{ + // avoid deeper nested insets when selecting + if (cursor.size() > anchor.size()) + return false; + + // anchor might be deeper, should have same path then + for (MathCursor::cursor_type::size_type i = 0; i < cursor.size(); ++i) + if (cursor[i].par_ != anchor[i].par_) + return false; + + // position should be ok. + return true; +} + void MathCursor::setPos(int x, int y) { @@ -327,17 +343,9 @@ void MathCursor::setPos(int x, int y) MathIterator et = iend(formula()->par().nucleus()); for ( ; it != et; ++it) { //lyxerr << "*it: " << *it << " *et: " << *et << "\n"; - if (selection_) { - // avoid deeper nested insets when selecting - if (it.cursor().size() > Anchor_.size()) - continue; - // anchor might be deeper! - if (it.cursor().size() == Anchor_.size()) - if (it.par() != Anchor_.back().par_) - continue; - //if (it.par() != Anchor_[it.cursor().size()].par_) - // continue; - } + // avoid invalid nesting hen selecting + if (selection_ && !positionable(it.cursor(), Anchor_)) + continue; //lyxerr << it.position() << endl; int xo = it.position().xpos(); int yo = it.position().ypos(); @@ -557,7 +565,11 @@ bool MathCursor::up(bool sel) } } - return goUp() || selection_; + cursor_type save = Cursor_; + if (goUpDown(true)) + return true; + Cursor_ = save; + return selection_; } @@ -587,7 +599,11 @@ bool MathCursor::down(bool sel) } } - return goDown() || selection_; + cursor_type save = Cursor_; + if (goUpDown(false)) + return true; + Cursor_ = save; + return selection_; } @@ -1116,57 +1132,42 @@ MathCursorPos const & MathCursor::cursor() const } -bool MathCursor::goUp() +bool MathCursor::goUpDown(bool up) { - // first ask the inset if it knows better then we - if (par()->idxUp(idx(), pos())) { - //lyxerr << "ask cell\n"; - int xlow, xhigh, ylow, yhigh; - xarray().boundingBox(xlow, xhigh, ylow, yhigh); - bruteFind(xlow, xhigh, ylow, yhigh); - return true; - } - - // if not, apply brute force. - //lyxerr << "brute force\n"; - return - bruteFind( - formula()->xlow(), - formula()->xhigh(), - formula()->ylow(), - xarray().yo() - 4 - xarray().ascent() - ); -} + int xo, yo; + getPos(xo, yo); + // try to find an inset that knows better then we + while (1) { + // we found a cell that think something "below" us. + if (up) { + if (par()->idxUp(idx(), pos())) + break; + } else { + if (par()->idxDown(idx(), pos())) + break; + } -bool MathCursor::goDown() -{ - // first ask the inset if it knows better then we - if (par()->idxDown(idx(), pos())) { - //lyxerr << "ask cell\n"; - int xlow, xhigh, ylow, yhigh; - xarray().boundingBox(xlow, xhigh, ylow, yhigh); - bruteFind(xlow, xhigh, ylow, yhigh); - return true; + if (!popLeft()) { + // have reached hull + return + bruteFind(xo, yo, + formula()->xlow(), + formula()->xhigh(), + up ? formula()->ylow() : yo + 4, + up ? yo - 4 : formula()->yhigh() + ); + } } - - // if not, apply brute force. - //lyxerr << "brute force\n"; - return - bruteFind( - formula()->xlow(), - formula()->xhigh(), - xarray().yo() + 4 + xarray().descent(), - formula()->yhigh() - ); + int xlow, xhigh, ylow, yhigh; + xarray().boundingBox(xlow, xhigh, ylow, yhigh); + bruteFind(xo, yo, xlow, xhigh, ylow, yhigh); + return true; } -bool MathCursor::bruteFind(int xlow, int xhigh, int ylow, int yhigh) +bool MathCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh) { - int x; - int y; - getPos(x, y); //lyxerr << "looking at range: " // << "[" << xlow << "..." << xhigh << "]" // << " x [" << ylow << "..." << yhigh << "]" diff --git a/src/mathed/math_cursor.h b/src/mathed/math_cursor.h index ec6ae8934b..23c4f21308 100644 --- a/src/mathed/math_cursor.h +++ b/src/mathed/math_cursor.h @@ -242,12 +242,10 @@ private: bool idxLeft(); /// moves cursor index one cell to the right bool idxRight(); - /// moves position somehow up - bool goUp(); - /// moves position somehow down - bool goDown(); + /// moves position somehow up or down + bool goUpDown(bool up); /// moves position into box - bool bruteFind(int xlow, int xhigh, int ylow, int yhigh); + bool bruteFind(int xo, int yo, int xlow, int xhigh, int ylow, int yhigh); /// string macroName() const; diff --git a/src/mathed/math_gridinset.C b/src/mathed/math_gridinset.C index ce2d94bee5..6d06cf4a6e 100644 --- a/src/mathed/math_gridinset.C +++ b/src/mathed/math_gridinset.C @@ -489,24 +489,18 @@ int MathGridInset::cellYOffset(idx_type idx) const bool MathGridInset::idxUp(idx_type & idx, pos_type & pos) const { - //return false; if (idx < ncols()) return false; - int x = cellXOffset(idx) + xcell(idx).pos2x(pos); idx -= ncols(); - pos = xcell(idx).x2pos(x - cellXOffset(idx)); return true; } bool MathGridInset::idxDown(idx_type & idx, pos_type & pos) const { - //return false; if (idx >= ncols() * (nrows() - 1)) return false; - int x = cellXOffset(idx) + xcell(idx).pos2x(pos); idx += ncols(); - pos = xcell(idx).x2pos(x - cellXOffset(idx)); return true; } -- 2.39.2