From: Abdelrazak Younes Date: Mon, 1 Jan 2007 11:36:30 +0000 (+0000) Subject: Transfer x2pos() and pos2x from LyXText to TextMetrics. X-Git-Tag: 1.6.10~11395 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=a64d5ba3627e3667e43423019ac011992f032d73;p=features.git Transfer x2pos() and pos2x from LyXText to TextMetrics. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@16458 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/TextMetrics.C b/src/TextMetrics.C index 7cc99b0905..9b9876d3b7 100644 --- a/src/TextMetrics.C +++ b/src/TextMetrics.C @@ -919,6 +919,26 @@ pos_type TextMetrics::getColumnNearX(pit_type const pit, } +pos_type TextMetrics::x2pos(pit_type pit, int row, int x) const +{ + ParagraphMetrics const & pm = parMetrics(pit); + BOOST_ASSERT(!pm.rows().empty()); + BOOST_ASSERT(row < int(pm.rows().size())); + bool bound = false; + Row const & r = pm.rows()[row]; + return r.pos() + getColumnNearX(pit, r, x, bound); +} + + +//int LyXText::pos2x(pit_type pit, pos_type pos) const +//{ +// ParagraphMetrics const & pm = parMetrics(pit); +// Row const & r = pm.rows()[row]; +// int x = 0; +// pos -= r.pos(); +//} + + int defaultRowHeight() { return int(theFontMetrics(LyXFont(LyXFont::ALL_SANE)).maxHeight() * 1.2); diff --git a/src/TextMetrics.h b/src/TextMetrics.h index 4af380ebe5..77fc11a4b7 100644 --- a/src/TextMetrics.h +++ b/src/TextMetrics.h @@ -97,6 +97,13 @@ public: /// of this column. pos_type getColumnNearX(pit_type pit, Row const & row, int & x, bool & boundary) const; + + /// returns pos in given par at given x coord. + pos_type x2pos(pit_type pit, int row, int x) const; + + // FIXME: is there a need for this? + //int pos2x(pit_type pit, pos_type pos) const; + private: /// The BufferView owner. diff --git a/src/insets/insettabular.C b/src/insets/insettabular.C index a7b76c62aa..04ff0940fe 100644 --- a/src/insets/insettabular.C +++ b/src/insets/insettabular.C @@ -580,8 +580,9 @@ void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd) if (tabular.row_of_cell(cur.idx()) != tabular.rows() - 1) { cur.idx() = tabular.getCellBelow(cur.idx()); cur.pit() = 0; - cur.pos() = cell(cur.idx())->getText(0)->x2pos( - cur.bv(), cur.pit(), 0, cur.targetX()); + TextMetrics const & tm = + cur.bv().textMetrics(cell(cur.idx())->getText(0)); + cur.pos() = tm.x2pos(cur.pit(), 0, cur.targetX()); } if (sl == cur.top()) { // we trick it to go to the RIGHT after leaving the @@ -603,13 +604,10 @@ void InsetTabular::doDispatch(LCursor & cur, FuncRequest & cmd) cur.idx() = tabular.getCellAbove(cur.idx()); cur.pit() = cur.lastpit(); LyXText const * text = cell(cur.idx())->getText(0); + TextMetrics const & tm = cur.bv().textMetrics(text); ParagraphMetrics const & pm = - cur.bv().parMetrics(text, cur.lastpit()); - cur.pos() = text->x2pos( - cur.bv(), - cur.pit(), - pm.rows().size()-1, - cur.targetX()); + tm.parMetrics(cur.lastpit()); + cur.pos() = tm.x2pos(cur.pit(), pm.rows().size()-1, cur.targetX()); } if (sl == cur.top()) { cmd = FuncRequest(LFUN_FINISHED_UP); diff --git a/src/lyxtext.h b/src/lyxtext.h index cfa42e1d94..feb0b273ff 100644 --- a/src/lyxtext.h +++ b/src/lyxtext.h @@ -94,11 +94,6 @@ public: /// FIXME: replace LCursor with DocIterator. void setFont(LCursor & cur, LyXFont const &, bool toggleall = false); - /// returns pos in given par at given x coord. - /// FIXME: move to TextMetrics. - pos_type x2pos(BufferView const &, pit_type pit, int row, int x) const; - int pos2x(pit_type pit, pos_type pos) const; - /// void toggleFree(LCursor & cur, LyXFont const &, bool toggleall = false); diff --git a/src/text.C b/src/text.C index 2ed8a3ca85..73f3c27148 100644 --- a/src/text.C +++ b/src/text.C @@ -1661,45 +1661,6 @@ docstring LyXText::getPossibleLabel(LCursor & cur) const } -//pos_type LyXText::x2pos(pit_type pit, int row, int x) const -//{ -// int lastx = 0; -// int currx = 0; -// Paragraph const & par = pars_[pit]; -// Row const & r = par.rows()[row]; -// int pos = r.pos(); -// for (; currx < x && pos < r.endpos(); ++pos) { -// lastx = currx; -// currx += singleWidth(buffer, par, pos); -// } -// if (abs(lastx - x) < abs(currx - x) && pos != r.pos()) -// --pos; -// return pos; -//} - - -pos_type LyXText::x2pos(BufferView const & bv, pit_type pit, int row, - int x) const -{ - TextMetrics const & tm = bv.textMetrics(this); - ParagraphMetrics const & pm = tm.parMetrics(pit); - BOOST_ASSERT(!pm.rows().empty()); - BOOST_ASSERT(row < int(pm.rows().size())); - bool bound = false; - Row const & r = pm.rows()[row]; - return r.pos() + tm.getColumnNearX(pit, r, x, bound); -} - - -//int LyXText::pos2x(pit_type pit, pos_type pos) const -//{ -// Paragraph const & par = pars_[pit]; -// Row const & r = par.rows()[row]; -// int x = 0; -// pos -= r.pos(); -//} - - void LyXText::setCursorFromCoordinates(LCursor & cur, int const x, int const y) { BOOST_ASSERT(this == cur.text()); diff --git a/src/text2.C b/src/text2.C index e47be66fd6..4f919f6638 100644 --- a/src/text2.C +++ b/src/text2.C @@ -981,7 +981,8 @@ bool LyXText::cursorUp(LCursor & cur) // Tell BufferView to test for FitCursor in any case! cur.updateFlags(Update::FitCursor); - ParagraphMetrics const & pm = cur.bv().parMetrics(this, cur.pit()); + TextMetrics const & tm = cur.bv().textMetrics(this); + ParagraphMetrics const & pm = tm.parMetrics(cur.pit()); int row; int const x = cur.targetX(); @@ -1015,13 +1016,13 @@ bool LyXText::cursorUp(LCursor & cur) if (row > 0) { updateNeeded |= setCursor(cur, cur.pit(), - x2pos(cur.bv(), cur.pit(), row - 1, x)); + tm.x2pos(cur.pit(), row - 1, x)); } else if (cur.pit() > 0) { --cur.pit(); //cannot use 'par' now ParagraphMetrics const & pmcur = cur.bv().parMetrics(this, cur.pit()); updateNeeded |= setCursor(cur, cur.pit(), - x2pos(cur.bv(), cur.pit(), pmcur.rows().size() - 1, x)); + tm.x2pos(cur.pit(), pmcur.rows().size() - 1, x)); } cur.x_target() = x; @@ -1035,7 +1036,8 @@ bool LyXText::cursorDown(LCursor & cur) // Tell BufferView to test for FitCursor in any case! cur.updateFlags(Update::FitCursor); - ParagraphMetrics const & pm = cur.bv().parMetrics(this, cur.pit()); + TextMetrics const & tm = cur.bv().textMetrics(this); + ParagraphMetrics const & pm = tm.parMetrics(cur.pit()); int row; int const x = cur.targetX(); @@ -1074,11 +1076,11 @@ bool LyXText::cursorDown(LCursor & cur) if (row + 1 < int(pm.rows().size())) { updateNeeded |= setCursor(cur, cur.pit(), - x2pos(cur.bv(), cur.pit(), row + 1, x)); + tm.x2pos(cur.pit(), row + 1, x)); } else if (cur.pit() + 1 < int(paragraphs().size())) { ++cur.pit(); updateNeeded |= setCursor(cur, cur.pit(), - x2pos(cur.bv(), cur.pit(), 0, x)); + tm.x2pos(cur.pit(), 0, x)); } cur.x_target() = x;