From 74e37125805c4f461e312194d395ee301dda0f5c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Thu, 21 Mar 2002 09:48:46 +0000 Subject: [PATCH] fix Herbert's up/down problem git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@3795 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/mathed/math_cursor.C | 79 ++++++++++++++++++++++++++----------- src/mathed/math_fracbase.C | 16 ++------ src/mathed/math_fracbase.h | 6 +-- src/mathed/math_fracinset.C | 4 +- src/mathed/math_gridinset.C | 26 ++++++------ src/mathed/math_gridinset.h | 4 +- src/mathed/math_inset.C | 8 +--- src/mathed/math_inset.h | 6 +-- src/mathed/math_macro.C | 12 ++---- src/mathed/math_macro.h | 4 +- src/mathed/math_rootinset.C | 18 +++------ src/mathed/math_rootinset.h | 4 +- 12 files changed, 90 insertions(+), 97 deletions(-) diff --git a/src/mathed/math_cursor.C b/src/mathed/math_cursor.C index 9970f97f2f..2876e46e5c 100644 --- a/src/mathed/math_cursor.C +++ b/src/mathed/math_cursor.C @@ -1180,29 +1180,65 @@ bool MathCursor::goUpDown(bool up) int xo, yo; getPos(xo, yo); - // try current cell first - xarray().boundingBox(xlow, xhigh, ylow, yhigh); - if (up) - yhigh = yo - 4; - else - ylow = yo + 4; - if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) - return true; + // try neigbouring script insets + // try left + if (hasPrevAtom()) { + MathScriptInset * p = prevAtom()->asScriptInset(); + if (p && p->has(up)) { + --pos(); + push(nextAtom()); + idx() = up; // the superscript has index 1 + pos() = size(); + ///lyxerr << "updown: handled by scriptinset to the left\n"; + return true; + } + } + + // try right + if (hasNextAtom()) { + MathScriptInset * p = nextAtom()->asScriptInset(); + if (p && p->has(up)) { + push(nextAtom()); + idx() = up; + pos() = 0; + ///lyxerr << "updown: handled by scriptinset to the right\n"; + return true; + } + } + + // try current cell + //xarray().boundingBox(xlow, xhigh, ylow, yhigh); + //if (up) + // yhigh = yo - 4; + //else + // ylow = yo + 4; + //if (bruteFind(xo, yo, xlow, xhigh, ylow, yhigh)) { + // lyxerr << "updown: handled by brute find in the same cell\n"; + // return true; + //} // 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())) - break; - } else { - if (par()->idxDown(idx())) - break; + // we found a cell that thinks it has something "below" us. + if (par()->idxUpDown(idx(), up)) { + ///lyxerr << "updown: found inset that handles UpDown\n"; + xarray().boundingBox(xlow, xhigh, ylow, yhigh); + // project (xo,yo) onto proper box + ///lyxerr << "\n xo: " << xo << " yo: " << yo + /// << "\n xlow: " << xlow << " ylow: " << ylow + /// << "\n xhigh: " << xhigh << " yhigh: " << yhigh; + xo = min(max(xo, xlow), xhigh); + yo = min(max(yo, ylow), yhigh); + ///lyxerr << "\n xo2: " << xo << " yo2: " << yo << "\n"; + bruteFind(xo, yo, xlow, xhigh, ylow, yhigh); + ///lyxerr << "updown: handled by final brute find\n"; + return true; } if (!popLeft()) { // no such inset found, just take something "above" - return + ///lyxerr << "updown: handled by strange case\n"; + return bruteFind(xo, yo, formula()->xlow(), formula()->xhigh(), @@ -1210,14 +1246,13 @@ bool MathCursor::goUpDown(bool up) up ? yo - 4 : formula()->yhigh() ); } + ///lyxerr << "updown: looping\n"; } - xarray().boundingBox(xlow, xhigh, ylow, yhigh); - bruteFind(xo, yo, xlow, xhigh, ylow, yhigh); - return true; } -bool MathCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh) +bool MathCursor::bruteFind + (int x, int y, int xlow, int xhigh, int ylow, int yhigh) { cursor_type best_cursor; double best_dist = 1e10; @@ -1230,9 +1265,7 @@ bool MathCursor::bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhig MathCursorPos const & top = it.position(); int xo = top.xpos(); int yo = top.ypos(); - if (xlow - 2 <= xo && xo <= xhigh + 2 && - ylow - 2 <= yo && yo <= yhigh + 2) - { + if (xlow <= xo && xo <= xhigh && ylow <= yo && yo <= yhigh) { double d = (x - xo) * (x - xo) + (y - yo) * (y - yo); if (d < best_dist) { best_dist = d; diff --git a/src/mathed/math_fracbase.C b/src/mathed/math_fracbase.C index 2a6ac48d76..5889f0773e 100644 --- a/src/mathed/math_fracbase.C +++ b/src/mathed/math_fracbase.C @@ -23,19 +23,11 @@ bool MathFracbaseInset::idxLeft(idx_type &, pos_type &) const } -bool MathFracbaseInset::idxUp(idx_type & idx) const +bool MathFracbaseInset::idxUpDown(idx_type & idx, bool up) const { - if (idx == 0) + int target = !up; // up ? 0 : 1, since upper cell has idx 0 + if (idx == target) return false; - idx = 0; - return true; -} - - -bool MathFracbaseInset::idxDown(idx_type & idx) const -{ - if (idx == 1) - return false; - idx = 1; + idx = target; return true; } diff --git a/src/mathed/math_fracbase.h b/src/mathed/math_fracbase.h index 8fff7cfaf3..71d0b9418b 100644 --- a/src/mathed/math_fracbase.h +++ b/src/mathed/math_fracbase.h @@ -12,10 +12,8 @@ class MathFracbaseInset : public MathNestInset { public: /// MathFracbaseInset(); - /// - bool idxUp(idx_type &) const; - /// - bool idxDown(idx_type &) const; + /// + bool idxUpDown(idx_type &, bool up) const; /// bool idxLeft(idx_type &, pos_type &) const; /// diff --git a/src/mathed/math_fracinset.C b/src/mathed/math_fracinset.C index c99a2e0743..26af46aa5b 100644 --- a/src/mathed/math_fracinset.C +++ b/src/mathed/math_fracinset.C @@ -35,7 +35,7 @@ void MathFracInset::metrics(MathMetricsInfo const & mi) const smallerStyleFrac(m); xcell(0).metrics(m); xcell(1).metrics(m); - width_ = max(xcell(0).width(), xcell(1).width()) + 4; + width_ = max(xcell(0).width(), xcell(1).width()) + 2; ascent_ = xcell(0).height() + 2 + 5; descent_ = xcell(1).height() + 2 - 5; } @@ -47,7 +47,7 @@ void MathFracInset::draw(Painter & pain, int x, int y) const xcell(0).draw(pain, m - xcell(0).width() / 2, y - xcell(0).descent() - 2 - 5); xcell(1).draw(pain, m - xcell(1).width() / 2, y + xcell(1).ascent() + 2 - 5); if (!atop_) - pain.line(x + 2, y - 5, x + width() - 4, y - 5, LColor::math); + pain.line(x, y - 5, x + width(), y - 5, LColor::math); } diff --git a/src/mathed/math_gridinset.C b/src/mathed/math_gridinset.C index 57f750a44c..2829a14be8 100644 --- a/src/mathed/math_gridinset.C +++ b/src/mathed/math_gridinset.C @@ -585,24 +585,22 @@ int MathGridInset::cellYOffset(idx_type idx) const } -bool MathGridInset::idxUp(idx_type & idx) const +bool MathGridInset::idxUpDown(idx_type & idx, bool up) const { - if (idx < ncols()) - return false; - idx -= ncols(); - return true; + if (up) { + if (idx < ncols()) + return false; + idx -= ncols(); + return true; + } else { + if (idx >= ncols() * (nrows() - 1)) + return false; + idx += ncols(); + return true; + } } -bool MathGridInset::idxDown(idx_type & idx) const -{ - if (idx >= ncols() * (nrows() - 1)) - return false; - idx += ncols(); - return true; -} - - bool MathGridInset::idxLeft(idx_type & idx, pos_type & pos) const { // leave matrix if on the left hand edge diff --git a/src/mathed/math_gridinset.h b/src/mathed/math_gridinset.h index dfcffeea2a..69680a30c8 100644 --- a/src/mathed/math_gridinset.h +++ b/src/mathed/math_gridinset.h @@ -116,9 +116,7 @@ public: int cellYOffset(idx_type idx) const; /// - bool idxUp(idx_type &) const; - /// - bool idxDown(idx_type &) const; + bool idxUpDown(idx_type &, bool) const; /// bool idxLeft(idx_type &, pos_type &) const; /// diff --git a/src/mathed/math_inset.C b/src/mathed/math_inset.C index cddb9f2437..526e89c93f 100644 --- a/src/mathed/math_inset.C +++ b/src/mathed/math_inset.C @@ -119,13 +119,7 @@ bool MathInset::idxLeft(idx_type &, pos_type &) const } -bool MathInset::idxUp(idx_type &) const -{ - return false; -} - - -bool MathInset::idxDown(idx_type &) const +bool MathInset::idxUpDown(idx_type &, bool) const { return false; } diff --git a/src/mathed/math_inset.h b/src/mathed/math_inset.h index 98fb08ff66..2f783048c1 100644 --- a/src/mathed/math_inset.h +++ b/src/mathed/math_inset.h @@ -122,10 +122,8 @@ public: /// total height (== ascent + descent) virtual int height() const; - /// Where should we go when we press the up cursor key? - virtual bool idxUp(idx_type & idx) const; - /// The down key - virtual bool idxDown(idx_type & idx) const; + /// Where should we go when we press the up or down cursor key? + virtual bool idxUpDown(idx_type & idx, bool up) const; /// The left key virtual bool idxLeft(idx_type & idx, pos_type & pos) const; /// The right key diff --git a/src/mathed/math_macro.C b/src/mathed/math_macro.C index d849e6d485..69e14928f1 100644 --- a/src/mathed/math_macro.C +++ b/src/mathed/math_macro.C @@ -162,17 +162,11 @@ void MathMacro::dump() const } -bool MathMacro::idxUp(idx_type & idx) const +bool MathMacro::idxUpDown(idx_type & idx, bool up) const { pos_type pos; - return MathNestInset::idxLeft(idx, pos); -} - - -bool MathMacro::idxDown(idx_type & idx) const -{ - pos_type pos; - return MathNestInset::idxRight(idx, pos); + return + up ? MathNestInset::idxLeft(idx, pos) : MathNestInset::idxRight(idx, pos); } diff --git a/src/mathed/math_macro.h b/src/mathed/math_macro.h index 08b69105f1..c43ed9cd88 100644 --- a/src/mathed/math_macro.h +++ b/src/mathed/math_macro.h @@ -51,9 +51,7 @@ public: void dump() const; /// - bool idxUp(idx_type &) const; - /// - bool idxDown(idx_type &) const; + bool idxUpDown(idx_type &, bool up) const; /// bool idxLeft(idx_type &, pos_type &) const; /// diff --git a/src/mathed/math_rootinset.C b/src/mathed/math_rootinset.C index 597cf0a9ce..696e3b086c 100644 --- a/src/mathed/math_rootinset.C +++ b/src/mathed/math_rootinset.C @@ -71,24 +71,16 @@ void MathRootInset::write(WriteStream & os) const void MathRootInset::normalize(NormalStream & os) const { - os << "[root " << cell(1) << ' ' << cell(1) << ']'; + os << "[root " << cell(0) << ' ' << cell(1) << ']'; } -bool MathRootInset::idxUp(idx_type & idx) const +bool MathRootInset::idxUpDown(idx_type & idx, bool up) const { - if (idx == 0) + bool target = !up; // up ? 0 : 1; + if (idx == target) return false; - idx = 0; - return true; -} - - -bool MathRootInset::idxDown(idx_type & idx) const -{ - if (idx == 1) - return false; - idx = 1; + idx = target; return true; } diff --git a/src/mathed/math_rootinset.h b/src/mathed/math_rootinset.h index e71b4fe23f..303aba5608 100644 --- a/src/mathed/math_rootinset.h +++ b/src/mathed/math_rootinset.h @@ -32,9 +32,7 @@ public: /// MathInset * clone() const; /// - bool idxUp(idx_type & idx) const; - /// - bool idxDown(idx_type & idx) const; + bool idxUpDown(idx_type & idx, bool up) const; /// void metrics(MathMetricsInfo const & st) const; /// -- 2.39.2