From e207b0250f4a6f55782aefcfc5a80d070620c855 Mon Sep 17 00:00:00 2001 From: Vincent van Ravesteijn Date: Fri, 3 Apr 2009 21:39:06 +0000 Subject: [PATCH] Make sure that the document is only scrolled to the cursor when the cursor is/was visible before resizing . Resizing can occur by toggling toolbars (bug 4733), or by creating or closing buffers which causes the tabbar to toggle (e.g. bug 4174, comment 5). git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@29066 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/BufferView.cpp | 23 ++++++++++++++++++++++ src/BufferView.h | 4 ++++ src/frontends/qt4/GuiWorkArea.cpp | 32 ++++++++++++------------------- 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/BufferView.cpp b/src/BufferView.cpp index 53cd4d09a8..c75b115fad 100644 --- a/src/BufferView.cpp +++ b/src/BufferView.cpp @@ -2278,6 +2278,29 @@ bool BufferView::paragraphVisible(DocIterator const & dit) const } +void BufferView::cursorPosAndHeight(Point & p, int & h) const +{ + Cursor const & cur = cursor(); + Font const font = cur.getFont(); + frontend::FontMetrics const & fm = theFontMetrics(font); + int const asc = fm.maxAscent(); + int const des = fm.maxDescent(); + h = asc + des; + p = getPos(cur, cur.boundary()); + p.y_ -= asc; +} + + +bool BufferView::cursorInView(Point const & p, int h) const +{ + Cursor const & cur = cursor(); + // does the cursor touch the screen ? + if (p.y_ + h < 0 || p.y_ >= workHeight() || !paragraphVisible(cur)) + return false; + return true; +} + + void BufferView::draw(frontend::Painter & pain) { if (height_ == 0 || width_ == 0) diff --git a/src/BufferView.h b/src/BufferView.h index 0105275eae..b5de004c3d 100644 --- a/src/BufferView.h +++ b/src/BufferView.h @@ -261,6 +261,10 @@ public: Point getPos(DocIterator const & dit, bool boundary) const; /// is the paragraph of the cursor visible ? bool paragraphVisible(DocIterator const & dit) const; + /// is the cursor currently visible in the view + bool cursorInView(Point const & p, int h) const; + /// get the position and height of the cursor + void cursorPosAndHeight(Point & p, int & h) const; /// diff --git a/src/frontends/qt4/GuiWorkArea.cpp b/src/frontends/qt4/GuiWorkArea.cpp index 29789c7a25..344b63044c 100644 --- a/src/frontends/qt4/GuiWorkArea.cpp +++ b/src/frontends/qt4/GuiWorkArea.cpp @@ -517,8 +517,13 @@ void GuiWorkArea::resizeBufferView() // WARNING: Please don't put any code that will trigger a repaint here! // We are already inside a paint event. lyx_view_->setBusy(true); + Point p; + int h = 0; + buffer_view_->cursorPosAndHeight(p, h); + bool const cursor_in_view = buffer_view_->cursorInView(p, h); buffer_view_->resize(viewport()->width(), viewport()->height()); - buffer_view_->scrollToCursor(); + if (cursor_in_view) + buffer_view_->scrollToCursor(); updateScreen(); // Update scrollbars which might have changed due different @@ -552,31 +557,18 @@ void GuiWorkArea::showCursor() if (realfont.language() == latex_language) l_shape = false; - Font const font = buffer_view_->cursor().getFont(); - FontMetrics const & fm = theFontMetrics(font); - int const asc = fm.maxAscent(); - int const des = fm.maxDescent(); - int h = asc + des; - int x = 0; - int y = 0; - Cursor & cur = buffer_view_->cursor(); - cur.getPos(x, y); - y -= asc; - - // if it doesn't touch the screen, don't try to show it - bool cursorInView = true; - if (y + h < 0 || y >= viewport()->height() - || !cur.bv().paragraphVisible(cur)) - cursorInView = false; - + Point p; + int h = 0; + buffer_view_->cursorPosAndHeight(p, h); // show cursor on screen + Cursor & cur = buffer_view_->cursor(); bool completable = cur.inset().showCompletionCursor() && completer_->completionAvailable() && !completer_->popupVisible() && !completer_->inlineVisible(); - if (cursorInView) { + if (buffer_view_->cursorInView(p, h)) { cursor_visible_ = true; - showCursor(x, y, h, l_shape, isrtl, completable); + showCursor(p.x_, p.y_, h, l_shape, isrtl, completable); } } -- 2.39.2