mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 13:06:59 -04:00
scrollbar in text editor
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@154 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -229,6 +229,66 @@ double TextViewer::heightOfLastLine() const {
|
||||
return lines.back().line_height;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Scrolling
|
||||
|
||||
size_t TextViewer::lineCount() const {
|
||||
return lines.size();
|
||||
}
|
||||
size_t TextViewer::visibleLineCount(double height) const {
|
||||
size_t count = 0;
|
||||
FOR_EACH_CONST(l, lines) {
|
||||
if (l.top + l.line_height > height) return count;
|
||||
if (l.top >= 0) ++count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
size_t TextViewer::firstVisibleLine() const {
|
||||
size_t i = 0;
|
||||
FOR_EACH_CONST(l, lines) {
|
||||
if (l.top >= 0) return i;
|
||||
i++;
|
||||
}
|
||||
return 0; //no visible lines
|
||||
}
|
||||
|
||||
void TextViewer::scrollTo(size_t line_id) {
|
||||
scrollBy(-lines.at(line_id).top);
|
||||
}
|
||||
void TextViewer::scrollBy(double delta) {
|
||||
if (delta == 0) return;
|
||||
FOR_EACH(l, lines) {
|
||||
l.top += delta;
|
||||
}
|
||||
}
|
||||
|
||||
bool TextViewer::ensureVisible(double height, size_t char_id) {
|
||||
const Line& line = findLine(char_id);
|
||||
if (line.top < 0) {
|
||||
// scroll up
|
||||
scrollBy(-line.top);
|
||||
return true;
|
||||
} else if (line.bottom() > height) {
|
||||
// scroll down
|
||||
FOR_EACH(l, lines) {
|
||||
if (l.top > 0) scrollBy(-l.line_height); // scroll down a single line ...
|
||||
if (line.bottom() <= height) break; // ... until we can see the current line
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false; // line was already visible
|
||||
}
|
||||
}
|
||||
|
||||
double TextViewer::getExactScrollPosition() const {
|
||||
if (lines.empty()) return 0;
|
||||
return -lines.front().top;
|
||||
}
|
||||
void TextViewer::setExactScrollPosition(double pos) {
|
||||
if (lines.empty()) return; // no scrolling is needed
|
||||
pos += lines.front().top;
|
||||
scrollBy(-pos);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------- : Elements
|
||||
|
||||
void TextViewer::prepareElements(const String& text, const TextStyle& style, Context& ctx) {
|
||||
|
||||
@@ -90,6 +90,31 @@ class TextViewer {
|
||||
/// Return the height of the last line
|
||||
double heightOfLastLine() const;
|
||||
|
||||
// --------------------------------------------------- : Lines/scrolling
|
||||
|
||||
/// The total number of lines
|
||||
size_t lineCount() const;
|
||||
/// number of fully visible lines, height gives the height of the box
|
||||
size_t visibleLineCount(double height) const;
|
||||
/// the index of the first visible line
|
||||
size_t firstVisibleLine() const;
|
||||
// scroll so line_id becomes the first visible line
|
||||
void scrollTo(size_t line_id);
|
||||
/// Ensure the specified character is fully visible
|
||||
/* Always scrolls by a whole line.
|
||||
* Returns true if the editor has scrolled.
|
||||
*/
|
||||
bool ensureVisible(double height, size_t char_id);
|
||||
|
||||
/// Get exact scroll position
|
||||
double getExactScrollPosition() const;
|
||||
/// Set exact scroll position
|
||||
void setExactScrollPosition(double pos);
|
||||
|
||||
private:
|
||||
/// Scroll all lines a given amount
|
||||
void scrollBy(double delta);
|
||||
|
||||
private:
|
||||
// --------------------------------------------------- : More drawing
|
||||
double scale; /// < Scale when drawing
|
||||
|
||||
Reference in New Issue
Block a user