From ae0667c112b1ca209821ae480eee439555eac994 Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Thu, 17 Jan 2008 17:06:55 +0000 Subject: [PATCH] More scrolling work. Now the scrollbar parameters are normalized against the screen geometry and coordinates. This simplifies the logic and the code significantly. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22614 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/BufferView.cpp | 53 +++++++++++++++---------------- src/BufferView.h | 17 ++++------ src/frontends/qt4/GuiWorkArea.cpp | 7 ++-- 3 files changed, 36 insertions(+), 41 deletions(-) diff --git a/src/BufferView.cpp b/src/BufferView.cpp index 6778a03dc6..4884fe9993 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -417,6 +417,11 @@ void BufferView::updateScrollbar() if (height_ == 0) return; + // We prefer fixed size line scrolling. + d->scrollbarParameters_.single_step = defaultRowHeight(); + // We prefer full screen page scrolling. + d->scrollbarParameters_.page_step = height_; + Text & t = buffer_.text(); TextMetrics & tm = d->text_metrics_[&t]; @@ -433,9 +438,6 @@ void BufferView::updateScrollbar() d->par_height_.resize(parsize, defaultRowHeight() * 2); } - // It would be better to fix the scrollbar to understand - // values in [0..1] and divide everything by wh - // Look at paragraph heights on-screen pair first = tm.first(); pair last = tm.last(); @@ -445,31 +447,26 @@ void BufferView::updateScrollbar() << d->par_height_[pit]); } - - // Build temporary cursor. - Cursor cur(*this); - cur.push(buffer_.inset()); - /*Inset * inset =*/ tm.editXY(cur, 0, 10); - pit_type const first_visible_pit = cur.bottom().pit(); - d->scrollbarParameters_.position = coordOffset(cur, cur.boundary()).y_; - ParagraphMetrics const & visible_pm = tm.parMetrics(first_visible_pit); - d->scrollbarParameters_.position -= visible_pm.position(); - d->scrollbarParameters_.position += visible_pm.ascent(); - // FIXME: The screen position (0, 10) works reasonably well for most texts - // but the bottom position is still not correct. - LYXERR(Debug::SCROLLING, "first visible pit : " << first_visible_pit); - LYXERR(Debug::SCROLLING, "offset from top : " - << d->scrollbarParameters_.position); - - d->scrollbarParameters_.height = 0; - for (size_t i = 0; i != d->par_height_.size(); ++i) { - if (i == first_visible_pit) - d->scrollbarParameters_.position += d->scrollbarParameters_.height; - d->scrollbarParameters_.height += d->par_height_[i]; + int top_pos = first.second->position() - first.second->ascent(); + int bottom_pos = last.second->position() + last.second->descent(); + bool first_visible = first.first == 0 && top_pos >= 0; + bool last_visible = last.first == parsize - 1 && bottom_pos <= height_; + if (first_visible && last_visible) { + d->scrollbarParameters_.min = 0; + d->scrollbarParameters_.max = 0; + return; } - // We prefer fixed size line scrolling. - d->scrollbarParameters_.lineScrollHeight = defaultRowHeight(); + d->scrollbarParameters_.min = top_pos; + for (size_t i = 0; i != first.first; ++i) + d->scrollbarParameters_.min -= d->par_height_[i]; + d->scrollbarParameters_.max = bottom_pos; + for (size_t i = last.first + 1; i != parsize; ++i) + d->scrollbarParameters_.max += d->par_height_[i]; + + d->scrollbarParameters_.position = 0; + // The reference is the top position so we remove one page. + d->scrollbarParameters_.max -= d->scrollbarParameters_.page_step; } @@ -1427,8 +1424,8 @@ void BufferView::lfunScroll(FuncRequest const & cmd) { string const scroll_type = cmd.getArg(0); int const scroll_step = - (scroll_type == "line")? d->scrollbarParameters_.lineScrollHeight - : (scroll_type == "page")? height_ : 0; + (scroll_type == "line")? d->scrollbarParameters_.single_step + : (scroll_type == "page")? d->scrollbarParameters_.page_step : 0; if (scroll_step == 0) return; string const scroll_quantity = cmd.getArg(1); diff --git a/src/BufferView.h b/src/BufferView.h index 765267b421..faac2f6c62 100644 --- a/src/BufferView.h +++ b/src/BufferView.h @@ -52,19 +52,16 @@ enum CursorStatus { /// Scrollbar Parameters. struct ScrollbarParameters { - void reset(int h = 0, int p = 0, int l = 0) - { - height = h; - position = p; - lineScrollHeight = l; - } - - /// Total document height in pixels. - int height; + /// Minimum scrollbar position in pixels. + int min; + /// Maximum scrollbar position in pixels. + int max; /// Current position in the document in pixels. int position; /// Line-scroll amount in pixels. - int lineScrollHeight; + int single_step; + /// Page-scroll amount in pixels. + int page_step; }; /// Screen view of a Buffer. diff --git a/src/frontends/qt4/GuiWorkArea.cpp b/src/frontends/qt4/GuiWorkArea.cpp index 3d2fab30e1..f01c5bc6ca 100644 --- a/src/frontends/qt4/GuiWorkArea.cpp +++ b/src/frontends/qt4/GuiWorkArea.cpp @@ -463,10 +463,11 @@ void GuiWorkArea::updateScrollbar() verticalScrollBar()->setTracking(false); ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters(); - verticalScrollBar()->setRange(0, scroll_.height); - verticalScrollBar()->setSliderPosition(scroll_.position); - verticalScrollBar()->setSingleStep(scroll_.lineScrollHeight); + verticalScrollBar()->setRange(scroll_.min, scroll_.max); + verticalScrollBar()->setPageStep(scroll_.page_step); + verticalScrollBar()->setSingleStep(scroll_.single_step); verticalScrollBar()->setValue(scroll_.position); +// verticalScrollBar()->setSliderPosition(scroll_.position); verticalScrollBar()->setTracking(true); }