Fix algorithm that computes horizontal scroll offset

Rewrite the logic completely:
* fix cases where the offset was reset unnecessarily
* fix cases where the row was scrolled too much: as soon as a side of the row is completely visible, there is no need to scroll more.
* fix cases where offset would never reset
This commit is contained in:
Jean-Marc Lasgouttes 2015-10-27 16:11:01 +01:00
parent 1db691c2f5
commit 1f0d210ab5

View File

@ -3042,18 +3042,27 @@ void BufferView::checkCursorScrollOffset(PainterInfo & pi)
// Horizontal scroll offset of the cursor row in pixels // Horizontal scroll offset of the cursor row in pixels
int offset = d->horiz_scroll_offset_; int offset = d->horiz_scroll_offset_;
int const MARGIN = 2 * theFontMetrics(d->cursor_.real_current_font).em(); int const MARGIN = 2 * theFontMetrics(d->cursor_.real_current_font).em()
//lyxerr << "cur_x=" << cur_x << ", offset=" << offset << ", margin=" << MARGIN << endl; + row.right_margin;
if (cur_x < offset + MARGIN) { if (row.width() <= workWidth() - row.right_margin) {
// scroll right // Row is narrower than the work area, no offset needed.
offset = 0;
} else {
if (cur_x - offset < MARGIN) {
// cursor would be too far right
offset = cur_x - MARGIN; offset = cur_x - MARGIN;
} else if (cur_x > offset + workWidth() - MARGIN) { } else if (cur_x - offset > workWidth() - MARGIN) {
// scroll left // cursor would be too far left
offset = cur_x - workWidth() + MARGIN; offset = cur_x - workWidth() + MARGIN;
} }
// Correct the offset to make sure that we do not scroll too much
if (offset < row.left_margin || row.width() <= workWidth()) if (offset < 0)
offset = 0; offset = 0;
if (row.width() - offset < workWidth() - row.right_margin)
offset = row.width() - workWidth() + row.right_margin;
}
//lyxerr << "cur_x=" << cur_x << ", offset=" << offset << ", row.wid=" << row.width() << ", margin=" << MARGIN << endl;
if (offset != d->horiz_scroll_offset_) if (offset != d->horiz_scroll_offset_)
LYXERR(Debug::PAINTING, "Horiz. scroll offset changed from " LYXERR(Debug::PAINTING, "Horiz. scroll offset changed from "