diff --git a/src/Cursor.cpp b/src/Cursor.cpp index f22b3f1099..378a138da6 100644 --- a/src/Cursor.cpp +++ b/src/Cursor.cpp @@ -785,9 +785,8 @@ void Cursor::getSurroundingPos(pos_type & left_pos, pos_type & right_pos) const right_pos = -1; Row const & row = textRow(); - TextMetrics const & tm = bv_->textMetrics(text()); double dummy = 0; - Row::const_iterator cit = tm.findRowElement(row, pos(), boundary(), dummy); + Row::const_iterator cit = row.findElement(pos(), boundary(), dummy); // Handle the case of empty row if (cit == row.end()) { if (row.isRTL()) diff --git a/src/Row.cpp b/src/Row.cpp index eab4df51ba..70e3dcafc3 100644 --- a/src/Row.cpp +++ b/src/Row.cpp @@ -569,4 +569,53 @@ void Row::reverseRTL(bool const rtl_par) rtl_ = rtl_par; } +Row::const_iterator const +Row::findElement(pos_type const pos, bool const boundary, double & x) const +{ + /** + * When boundary is true, position i is in the row element (pos, endpos) + * if + * pos < i <= endpos + * whereas, when boundary is false, the test is + * pos <= i < endpos + * The correction below allows to handle both cases. + */ + int const boundary_corr = (boundary && pos) ? -1 : 0; + + x = left_margin; + + /** Early return in trivial cases + * 1) the row is empty + * 2) the position is the left-most position of the row; there + * is a quirk here however: if the first element is virtual + * (end-of-par marker for example), then we have to look + * closer + */ + if (empty() + || (pos == begin()->left_pos() && !boundary + && !begin()->isVirtual())) + return begin(); + + Row::const_iterator cit = begin(); + for ( ; cit != end() ; ++cit) { + /** Look whether the cursor is inside the element's + * span. Note that it is necessary to take the + * boundary into account, and to accept virtual + * elements, which have pos == endpos. + */ + if (pos + boundary_corr >= cit->pos + && (pos + boundary_corr < cit->endpos || cit->isVirtual())) { + x += cit->pos2x(pos); + break; + } + x += cit->full_width(); + } + + if (cit == end()) + --cit; + + return cit; +} + + } // namespace lyx diff --git a/src/Row.h b/src/Row.h index 695c81a95e..498fd07d7f 100644 --- a/src/Row.h +++ b/src/Row.h @@ -266,6 +266,8 @@ public: void reverseRTL(bool rtl_par); /// bool isRTL() const { return rtl_; } + /// Find row element that contains \c pos, and compute x offset. + const_iterator const findElement(pos_type pos, bool boundary, double & x) const; friend std::ostream & operator<<(std::ostream & os, Row const & row); diff --git a/src/TextMetrics.cpp b/src/TextMetrics.cpp index c337e8f81f..94594ede2c 100644 --- a/src/TextMetrics.cpp +++ b/src/TextMetrics.cpp @@ -1471,56 +1471,6 @@ Inset * TextMetrics::checkInsetHit(int x, int y) } -Row::const_iterator const -TextMetrics::findRowElement(Row const & row, pos_type const pos, - bool const boundary, double & x) const -{ - /** - * When boundary is true, position i is in the row element (pos, endpos) - * if - * pos < i <= endpos - * whereas, when boundary is false, the test is - * pos <= i < endpos - * The correction below allows to handle both cases. - */ - int const boundary_corr = (boundary && pos) ? -1 : 0; - - x = row.left_margin; - - /** Early return in trivial cases - * 1) the row is empty - * 2) the position is the left-most position of the row; there - * is a quirk here however: if the first element is virtual - * (end-of-par marker for example), then we have to look - * closer - */ - if (row.empty() - || (pos == row.begin()->left_pos() && !boundary - && !row.begin()->isVirtual())) - return row.begin(); - - Row::const_iterator cit = row.begin(); - for ( ; cit != row.end() ; ++cit) { - /** Look whether the cursor is inside the element's - * span. Note that it is necessary to take the - * boundary into account, and to accept virtual - * elements, which have pos == endpos. - */ - if (pos + boundary_corr >= cit->pos - && (pos + boundary_corr < cit->endpos || cit->isVirtual())) { - x += cit->pos2x(pos); - break; - } - x += cit->full_width(); - } - - if (cit == row.end()) - --cit; - - return cit; -} - - int TextMetrics::cursorX(CursorSlice const & sl, bool boundary) const { @@ -1533,7 +1483,7 @@ int TextMetrics::cursorX(CursorSlice const & sl, pos_type const pos = sl.pos(); double x = 0; - findRowElement(row, pos, boundary, x); + row.findElement(pos, boundary, x); return int(x); } diff --git a/src/TextMetrics.h b/src/TextMetrics.h index b854cbebd4..2f6181d0b0 100644 --- a/src/TextMetrics.h +++ b/src/TextMetrics.h @@ -190,12 +190,6 @@ public: /// x,y are screen coordinates void setCursorFromCoordinates(Cursor & cur, int x, int y); - /// Helper function: find row element that contains pos, and - /// compute x offset. - Row::const_iterator const - findRowElement(Row const & row, pos_type const pos, - bool const boundary, double & x) const; - /// int cursorX(CursorSlice const & cursor, bool boundary) const; ///