From 329471b4666d6fdc16aaf0879f71c84e197e20aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Mon, 14 Apr 2003 06:17:19 +0000 Subject: [PATCH] some cursor movement improvements to MathTextInset git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@6790 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/mathed/math_cursor.C | 13 +++++++++---- src/mathed/math_data.C | 17 ++++++++--------- src/mathed/math_data.h | 4 ++-- src/mathed/math_inset.C | 6 ++++++ src/mathed/math_inset.h | 3 +++ src/mathed/math_parser.C | 9 ++++++--- src/mathed/math_textinset.C | 19 +++++++++++++++---- src/mathed/math_textinset.h | 2 +- 8 files changed, 50 insertions(+), 23 deletions(-) diff --git a/src/mathed/math_cursor.C b/src/mathed/math_cursor.C index b5032bde44..95dd31f494 100644 --- a/src/mathed/math_cursor.C +++ b/src/mathed/math_cursor.C @@ -102,7 +102,7 @@ void MathCursor::pushRight(MathAtom & t) bool MathCursor::popLeft() { - //cerr << "Leaving atom to the left\n"; + //lyxerr << "Leaving atom to the left\n"; if (depth() <= 1) { if (depth() == 1) par()->notifyCursorLeaves(idx()); @@ -116,7 +116,7 @@ bool MathCursor::popLeft() bool MathCursor::popRight() { - //cerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n"; + //lyxerr << "Leaving atom "; par()->write(cerr, false); cerr << " right\n"; if (depth() <= 1) { if (depth() == 1) par()->notifyCursorLeaves(idx()); @@ -965,7 +965,10 @@ bool MathCursor::goUpDown(bool up) } } - // try current cell + // try current cell for e.g. text insets + if (par()->idxUpDown2(idx(), pos(), up, targetx_)) + return true; + //xarray().boundingBox(xlow, xhigh, ylow, yhigh); //if (up) // yhigh = yo - 4; @@ -1022,6 +1025,7 @@ bool MathCursor::bruteFind it.back().getPos(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) { @@ -1050,12 +1054,13 @@ void MathCursor::bruteFind2(int x, int y) it.back().setPos(0); MathIterator et = Cursor_; et.back().setPos(it.cell().size()); - while (1) { + for (int i = 0; ; ++i) { int xo, yo; it.back().getPos(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; diff --git a/src/mathed/math_data.C b/src/mathed/math_data.C index 34a27220ee..4e0c68db48 100644 --- a/src/mathed/math_data.C +++ b/src/mathed/math_data.C @@ -286,15 +286,15 @@ void MathArray::drawT(TextPainter & pain, int x, int y) const int MathArray::pos2x(size_type pos) const { - return pos2x(0, pos, 0); + return pos2x(pos, 0); } -int MathArray::pos2x(size_type pos1, size_type pos2, int glue) const +int MathArray::pos2x(size_type pos, int glue) const { int x = 0; - size_type target = min(pos2, size()); - for (size_type i = pos1; i < target; ++i) { + size_type target = min(pos, size()); + for (size_type i = 0; i < target; ++i) { const_iterator it = begin() + i; if ((*it)->getChar() == ' ') x += glue; @@ -306,14 +306,13 @@ int MathArray::pos2x(size_type pos1, size_type pos2, int glue) const MathArray::size_type MathArray::x2pos(int targetx) const { - return x2pos(0, targetx, 0); + return x2pos(targetx, 0); } -MathArray::size_type MathArray::x2pos(size_type startpos, int targetx, - int glue) const +MathArray::size_type MathArray::x2pos(int targetx, int glue) const { - const_iterator it = begin() + startpos; + const_iterator it = begin(); int lastx = 0; int currx = 0; for (; currx < targetx && it < end(); ++it) { @@ -322,7 +321,7 @@ MathArray::size_type MathArray::x2pos(size_type startpos, int targetx, currx += glue; currx += (*it)->width(); } - if (abs(lastx - targetx) < abs(currx - targetx) && it != begin() + startpos) + if (abs(lastx - targetx) < abs(currx - targetx) && it != begin()) --it; return it - begin(); } diff --git a/src/mathed/math_data.h b/src/mathed/math_data.h index 1749c3da74..e66b7ca9d6 100644 --- a/src/mathed/math_data.h +++ b/src/mathed/math_data.h @@ -128,11 +128,11 @@ public: /// returns x coordinate of given position in the array int pos2x(size_type pos) const; /// returns position of given x coordinate - int pos2x(size_type pos1, size_type pos2, int glue) const; + int pos2x(size_type pos, int glue) const; /// returns position of given x coordinate size_type x2pos(int pos) const; /// returns position of given x coordinate fstarting from a certain pos - size_type x2pos(size_type startpos, int targetx, int glue) const; + size_type x2pos(int targetx, int glue) const; /// returns distance of this cell to the point given by x and y // assumes valid position and size cache int dist(int x, int y) const; diff --git a/src/mathed/math_inset.C b/src/mathed/math_inset.C index 48763a7526..7b4fd1fdb8 100644 --- a/src/mathed/math_inset.C +++ b/src/mathed/math_inset.C @@ -120,6 +120,12 @@ bool MathInset::idxUpDown(idx_type &, pos_type &, bool, int) const } +bool MathInset::idxUpDown2(idx_type &, pos_type &, bool, int) const +{ + return false; +} + + bool MathInset::idxFirst(idx_type &, pos_type &) const { return false; diff --git a/src/mathed/math_inset.h b/src/mathed/math_inset.h index fa8d1a98a3..2bd1a23233 100644 --- a/src/mathed/math_inset.h +++ b/src/mathed/math_inset.h @@ -132,6 +132,9 @@ public: /// Where should we go when we press the up or down cursor key? virtual bool idxUpDown(idx_type & idx, pos_type & pos, bool up, int targetx) const; + /// Where should we go when we press the up or down cursor key? + virtual bool idxUpDown2(idx_type & idx, pos_type & pos, bool up, + int targetx) const; /// The left key virtual bool idxLeft(idx_type & idx, pos_type & pos) const; /// The right key diff --git a/src/mathed/math_parser.C b/src/mathed/math_parser.C index 44071e9001..61e2290f31 100644 --- a/src/mathed/math_parser.C +++ b/src/mathed/math_parser.C @@ -694,8 +694,10 @@ void Parser::parse1(MathGridInset & grid, unsigned flags, cell->push_back(MathAtom(new MathCharInset(t.character()))); } - else if (t.cat() == catNewline && mode != MathInset::MATH_MODE) - cell->push_back(MathAtom(new MathCharInset(t.character()))); + else if (t.cat() == catNewline && mode != MathInset::MATH_MODE) { + if (cell->empty() || cell->back()->getChar() != ' ') + cell->push_back(MathAtom(new MathCharInset(' '))); + } else if (t.cat() == catParameter) { Token const & n = getToken(); @@ -1209,7 +1211,8 @@ void Parser::parse1(MathGridInset & grid, unsigned flags, else { MathAtom at = createMathInset(t.cs()); MathInset::mode_type m = mode; - if (m == MathInset::UNDECIDED_MODE) + //if (m == MathInset::UNDECIDED_MODE) + if (at->currentMode() != MathInset::UNDECIDED_MODE) m = at->currentMode(); MathInset::idx_type start = 0; // this fails on \bigg[...\bigg] diff --git a/src/mathed/math_textinset.C b/src/mathed/math_textinset.C index 2e32ea9e21..d67dbb19d7 100644 --- a/src/mathed/math_textinset.C +++ b/src/mathed/math_textinset.C @@ -25,21 +25,23 @@ MathInset::idx_type MathTextInset::pos2row(pos_type pos) const } -void MathTextInset::getPos(idx_type, pos_type pos, int & x, int & y) const +void MathTextInset::getPos(idx_type /*idx*/, pos_type pos, int & x, int & y) const { idx_type const i = pos2row(pos); pos_type const p = pos - cache_.cellinfo_[i].begin_; cache_.getPos(i, p, x, y); + y = cache_.cell(i).yo(); } -bool MathTextInset::idxUpDown(idx_type &, pos_type & pos, bool up, +bool MathTextInset::idxUpDown2(idx_type &, pos_type & pos, bool up, int /*targetx*/) const { // try to move only one screen row up or down if possible idx_type i = pos2row(pos); + //lyxerr << "\nMathTextInset::idxUpDown() i: " << i << endl; MathGridInset::CellInfo const & cell1 = cache_.cellinfo_[i]; - int const x = cells_[0].pos2x(cell1.begin_, pos, cell1.glue_); + int const x = cache_.cell(i).pos2x(pos - cell1.begin_, cell1.glue_); if (up) { if (i == 0) return false; @@ -50,7 +52,7 @@ bool MathTextInset::idxUpDown(idx_type &, pos_type & pos, bool up, return false; } MathGridInset::CellInfo const & cell2 = cache_.cellinfo_[i]; - pos = cell(0).x2pos(cell2.begin_, x, cell2.glue_); + pos = cell2.begin_ + cache_.cell(i).x2pos(x, cell2.glue_); return true; } @@ -60,6 +62,10 @@ void MathTextInset::metrics(MetricsInfo & mi) const cell(0).metrics(mi); // we do our own metrics fiddling + // save old positional information + int const old_xo = cache_.cell(0).xo(); + int const old_yo = cache_.cell(0).yo(); + // delete old cache cache_ = MathGridInset(1, 0); @@ -148,6 +154,11 @@ void MathTextInset::metrics(MetricsInfo & mi) const cache_.metrics(mi); dim_ = cache_.dimensions(); //lyxerr << "outer dim: " << dim_ << endl; + + // reset position cache + for (idx_type i = 0; i < cache_.nargs(); ++i) + cache_.cell(i).setXY(old_xo, old_yo); + } diff --git a/src/mathed/math_textinset.h b/src/mathed/math_textinset.h index 25458e97ba..c5fb02531f 100644 --- a/src/mathed/math_textinset.h +++ b/src/mathed/math_textinset.h @@ -22,7 +22,7 @@ public: void drawSelection(PainterInfo & pi, idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const; /// moves cursor up or down - bool idxUpDown(idx_type &, pos_type & pos, bool up, int targetx) const; + bool idxUpDown2(idx_type &, pos_type & pos, bool up, int targetx) const; protected: /// row corresponding to given position idx_type pos2row(pos_type pos) const;