From 02a625161081ec2c0be59aa19858cffd1aa7bb8c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Lars=20Gullik=20Bj=C3=B8nnes?= Date: Sat, 12 Apr 2003 23:03:05 +0000 Subject: [PATCH] cached y positions patch from Alfredo git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@6785 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/ChangeLog | 8 +++++ src/frontends/ChangeLog | 5 ++++ src/frontends/screen.C | 26 ++++++++-------- src/lyxrow.C | 16 ++++++++-- src/lyxrow.h | 6 ++++ src/lyxtext.h | 2 ++ src/text.C | 66 ++++++++++++++++++++++++++++------------- src/text2.C | 2 +- 8 files changed, 93 insertions(+), 38 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 3ee0e7681a..869b8a3d37 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,11 @@ +2003-04-11 Alfredo Braunstein + + * lyxrow.[Ch]: add a cached y position to a Row and Row::y() + methods to access it. + * lyxtext.h: + * text.C: Added updateRowPositions to compute all row positions. + Make top_y and getRowNearY() to use the cached y position + 2003-04-11 John Levon * text.C (rowBreakPoint): reintroduce the labelEnd diff --git a/src/frontends/ChangeLog b/src/frontends/ChangeLog index e9f60c1164..5e9906ce3f 100644 --- a/src/frontends/ChangeLog +++ b/src/frontends/ChangeLog @@ -1,3 +1,8 @@ +2003-04-11 Alfredo Braunstein + + * screen.C (update): add calls to updateRowPositions() before + drawOneRow and drawFromTo. + 2003-04-10 John Levon * Toolbar.h: diff --git a/src/frontends/screen.C b/src/frontends/screen.C index 8e7d2cd872..c6e7b3d323 100644 --- a/src/frontends/screen.C +++ b/src/frontends/screen.C @@ -257,6 +257,7 @@ void LyXScreen::update(BufferView & bv, int yo, int xo) switch (text->refreshStatus()) { case LyXText::REFRESH_AREA: { + text->updateRowPositions(); int const y = max(int(text->refresh_y - text->top_y()), 0); drawFromTo(text, &bv, y, vheight, yo, xo); expose(0, y, vwidth, vheight - y); @@ -264,6 +265,7 @@ void LyXScreen::update(BufferView & bv, int yo, int xo) break; case LyXText::REFRESH_ROW: { + text->updateRowPositions(); // ok I will update the current cursor row drawOneRow(text, &bv, text->refresh_row, text->refresh_y, yo, xo); @@ -412,22 +414,18 @@ void LyXScreen::drawFromTo(LyXText * text, BufferView * bv, { lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << '-' << y2 << endl; - int y_text = text->top_y() + y1; - - // get the first needed row - RowList::iterator row = text->getRowNearY(y_text); - RowList::iterator end = text->rows().end(); - - // y_text is now the real beginning of the row - - int y = y_text - text->top_y(); + int const topy = text->top_y(); + int y_text = topy + y1; + RowList::iterator rit = text->getRowNearY(y_text); + int y = y_text - topy; // y1 is now the real beginning of row on the screen - while (row != end && y < y2) { - RowPainter rp(*bv, *text, row); - rp.paint(y + yo, xo, y + text->top_y()); - y += row->height(); - ++row; + RowList::iterator const rend = text->rows().end(); + while (rit != rend && y < y2) { + RowPainter rp(*bv, *text, rit); + rp.paint(y + yo, xo, y + topy); + y += rit->height(); + ++rit; } // maybe we have to clear the screen at the bottom diff --git a/src/lyxrow.C b/src/lyxrow.C index 33356ec0d7..682186c71f 100644 --- a/src/lyxrow.C +++ b/src/lyxrow.C @@ -23,17 +23,29 @@ using std::max; using std::min; Row::Row() - : pos_(0), fill_(0), height_(0), width_(0), + : pos_(0), fill_(0), height_(0), width_(0), y_(0), ascent_of_text_(0), baseline_(0) {} Row::Row(ParagraphList::iterator pit, pos_type po) - : pit_(pit), pos_(po), fill_(0), height_(0), width_(0), + : pit_(pit), pos_(po), fill_(0), height_(0), width_(0), y_(0), ascent_of_text_(0), baseline_(0) {} +void Row::y(unsigned int newy) +{ + y_ = newy; +} + + +unsigned int Row::y() const +{ + return y_; +} + + ParagraphList::iterator Row::par() { return pit_; diff --git a/src/lyxrow.h b/src/lyxrow.h index cbb72cec7c..7a81db1b2f 100644 --- a/src/lyxrow.h +++ b/src/lyxrow.h @@ -61,6 +61,10 @@ public: unsigned int baseline() const; /// return true if this row is the start of a paragraph bool isParStart() const; + /// return the cached y position + unsigned int y() const; + /// cache the y position + void y(unsigned int newy); private: /// ParagraphList::iterator pit_; @@ -73,6 +77,8 @@ private: unsigned short height_; /// unsigned int width_; + /// cached y position + unsigned int y_; /// ascent from baseline including prelude space unsigned short ascent_of_text_; /// the top of the real text in the row diff --git a/src/lyxtext.h b/src/lyxtext.h index 3d45e28352..2a1ec35843 100644 --- a/src/lyxtext.h +++ b/src/lyxtext.h @@ -88,6 +88,8 @@ private: */ int anchor_row_offset_; public: + /// update all cached row positions + void updateRowPositions(); /// get the y coord. of the top of the screen (relative to doc start) int top_y() const; /// set the y coord. of the top of the screen (relative to doc start) diff --git a/src/text.C b/src/text.C index d3a665d4eb..ff1b144c26 100644 --- a/src/text.C +++ b/src/text.C @@ -74,25 +74,28 @@ BufferView * LyXText::bv() const } -int LyXText::top_y() const +void LyXText::updateRowPositions() { - if (anchor_row_ == rowlist_.end()) - return 0; - - int y = 0; - - RowList::iterator rit = rowlist_.begin(); - RowList::iterator end = rowlist_.end(); - for (; rit != end && rit != anchor_row_; ++rit) { + RowList::iterator rit = rows().begin(); + RowList::iterator rend = rows().end(); + for (int y = 0; rit != rend ; ++rit) { + rit->y(y); y += rit->height(); } - return y + anchor_row_offset_; +} + + +int LyXText::top_y() const +{ + if (isInInset() || anchor_row_ == rowlist_.end() ) + return 0; + return anchor_row_->y() + anchor_row_offset_; } void LyXText::top_y(int newy) { - if (rows().empty()) + if (rows().empty() || isInInset()) return; lyxerr[Debug::GUI] << "setting top y = " << newy << endl; @@ -2767,20 +2770,41 @@ LyXText::getRow(ParagraphList::iterator pit, pos_type pos, int & y) const RowList::iterator LyXText::getRowNearY(int & y) const { - // If possible we should optimize this method. (Lgb) - int tmpy = 0; - RowList::iterator rit = rowlist_.begin(); - RowList::iterator end = rowlist_.end(); + RowList::iterator rit = anchor_row_; + RowList::iterator const beg = rows().begin(); + RowList::iterator const end = rows().end(); - while (rit != end && - boost::next(rit) != end && - tmpy + rit->height() <= y) { - tmpy += rit->height(); - ++rit; + if (rows().empty()) { + y = 0; + return end; + } + if (rit == end) + rit = beg; + + int tmpy = rit->y(); + + if (tmpy <= y) { + while (rit != end && tmpy <= y) { + tmpy += rit->height(); + ++rit; + } + if (rit != beg) { + --rit; + tmpy -= rit->height(); + } + } else { + while (rit != beg && tmpy > y) { + --rit; + tmpy -= rit->height(); + } + } + if (tmpy < 0 || rit == end) { + tmpy = 0; + rit = beg; } - // return the real y + // return the rel y y = tmpy; return rit; diff --git a/src/text2.C b/src/text2.C index ed770d93b0..f87adbb30c 100644 --- a/src/text2.C +++ b/src/text2.C @@ -283,7 +283,7 @@ void LyXText::removeRow(RowList::iterator rit) if (anchor_row_ == rit) { if (rit != rows().begin()) { anchor_row_ = boost::prior(rit); - anchor_row_offset_ += boost::prior(rit)->height(); + anchor_row_offset_ += anchor_row_->height(); } else { anchor_row_ = boost::next(rit); anchor_row_offset_ -= rit->height(); -- 2.39.5