diff --git a/src/include/ktexteditor/view.h b/src/include/ktexteditor/view.h --- a/src/include/ktexteditor/view.h +++ b/src/include/ktexteditor/view.h @@ -215,6 +215,13 @@ }; /** + * Possible line types + */ + enum LineType { + RealLine = 0, /** < Real line. */ + VisibleLine = 1 /** < Visible line. Line that is not folded. */ + }; + /** * Get the current view mode/state. * This can be used to detect the view's current mode. For * example \NormalInputMode, \ViInputMode or whatever other input modes are @@ -652,6 +659,68 @@ const QString& templateString, const QString& script = QString()); + + /** + * Scroll view to cursor. + * + * \param cursor the cursor position to scroll to. + */ + void setScrollPosition(KTextEditor::Cursor &cursor); + + /** + * Horizontally scroll view to position. + * + * \param x the pixel position to scroll to. + */ + void setHorizontalScrollPosition(int x); + + /** + * Get the cursor corresponding to the maximum position + * the view can vertically scroll to. + * + * \return cursor position of the maximum vertical scroll position. + */ + KTextEditor::Cursor maxScrollPosition() const; + + /** + * Get the first displayed line in the view. + * + * \param real if REAL (the default), it returns the real line number + * accounting for folded regions. In that case it walks over all folded + * regions + * O(n) for n == number of folded ranges + * + * \note if code is folded, many hundred lines can be + * between firstVisibleLine() and lastVisibleLine(). + * + * \return the first visible line. + * \see lastVisibleLine() + */ + int firstDisplayedLine(LineType lineType = RealLine) const; + + /** + * Get the last displayed line in the view. + * + * \param lineType if REAL (the default), it returns the real line number + * accounting for folded regions. In that case it walks over all folded + * regions + * O(n) for n == number of folded ranges + * + * \note if code is folded, many hundred lines can be + * between firstVisibleLine() and lastVisibleLine(). + * + * \return the last visible line. + * \see firstVisibleLine() + */ + int lastDisplayedLine(LineType lineType = RealLine) const; + + /** + * Get the view's text area rectangle excluding border, scrollbars, etc. + * + * \return the view's text area rectangle + */ + QRect textAreaRect() const; + public: /** * Print the document. This should result in showing the print dialog. diff --git a/src/utils/ktexteditor.cpp b/src/utils/ktexteditor.cpp --- a/src/utils/ktexteditor.cpp +++ b/src/utils/ktexteditor.cpp @@ -249,3 +249,33 @@ void Command::processText(KTextEditor::View *, const QString &) { } + +void View::setScrollPosition(KTextEditor::Cursor &cursor) +{ + d->setScrollPositionInternal(cursor); +} + +void View::setHorizontalScrollPosition(int x) +{ + d->setHorizontalScrollPositionInternal(x); +} + +KTextEditor::Cursor View::maxScrollPosition() const +{ + return d->maxScrollPositionInternal(); +} + +int View::firstDisplayedLine(LineType lineType) const +{ + return d->firstDisplayedLineInternal(lineType); +} + +int View::lastDisplayedLine(LineType lineType) const +{ + return d->lastDisplayedLineInternal(lineType); +} + +QRect View::textAreaRect() const +{ + return d->textAreaRectInternal(); +} diff --git a/src/view/kateview.h b/src/view/kateview.h --- a/src/view/kateview.h +++ b/src/view/kateview.h @@ -970,6 +970,19 @@ // remember folding state to prevent refolding the first line if it was manually unfolded, // e.g. when saving a file or changing other config vars bool m_autoFoldedFirstLine; + +public: + void setScrollPositionInternal(KTextEditor::Cursor &cursor); + + void setHorizontalScrollPositionInternal(int x); + + KTextEditor::Cursor maxScrollPositionInternal() const; + + int firstDisplayedLineInternal(LineType lineType) const; + + int lastDisplayedLineInternal(LineType lineType) const; + + QRect textAreaRectInternal() const; }; } diff --git a/src/view/kateview.cpp b/src/view/kateview.cpp --- a/src/view/kateview.cpp +++ b/src/view/kateview.cpp @@ -2549,6 +2549,48 @@ return pt == QPoint(-1, -1) ? pt : m_viewInternal->mapToParent(pt); } +void KTextEditor::ViewPrivate::setScrollPositionInternal(KTextEditor::Cursor &cursor) +{ + m_viewInternal->scrollPos(cursor, false, true, false); +} + +void KTextEditor::ViewPrivate::setHorizontalScrollPositionInternal(int x) +{ + m_viewInternal->scrollColumns(x); +} + +KTextEditor::Cursor KTextEditor::ViewPrivate::maxScrollPositionInternal() const +{ + return m_viewInternal->maxStartPos(true); +} + + +int KTextEditor::ViewPrivate::firstDisplayedLineInternal(LineType lineType) const +{ + if (lineType == RealLine) { + return m_textFolding.visibleLineToLine(m_viewInternal->startLine()); + } else { + return m_viewInternal->startLine(); + } +} + +int KTextEditor::ViewPrivate::lastDisplayedLineInternal(LineType lineType) const +{ + if (lineType == RealLine) { + return m_textFolding.visibleLineToLine(m_viewInternal->endLine()); + } else { + return m_viewInternal->endLine(); + } +} + +QRect KTextEditor::ViewPrivate::textAreaRectInternal() const +{ + const auto sourceRect = m_viewInternal->rect(); + const auto topLeft = m_viewInternal->mapTo(this, sourceRect.topLeft()); + const auto bottomRight = m_viewInternal->mapTo(this, sourceRect.bottomRight()); + return {topLeft, bottomRight}; +} + bool KTextEditor::ViewPrivate::setCursorPositionVisual(const KTextEditor::Cursor &position) { return setCursorPositionInternal(position, m_doc->config()->tabWidth(), true);