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:
twanvl
2006-12-23 12:47:08 +00:00
parent 153dab9a4f
commit 2233295cfd
13 changed files with 179 additions and 74 deletions
+60
View File
@@ -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) {
+25
View File
@@ -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