Line Numbers in Text Editor
massung at gmail.com
Sat Jun 26 15:17:31 CDT 2010
I gotta say, my name is Jeff Massung, and I disapprove of that solution. ;-)
I love everyone sharing their ideas, but this is one that's been a solved
problem, well... for a very long time, and with a little math there's no
performance issue what-so-ever, and there's no need to keep around a field
with thousands of numbers in it.
Think of what we know:
* The size of our editor.
* Where the scrollbar is.
* The size of the font we're using.
>From this, there's very little we actually need to do. Even with the largest
monitor on the planet and using the smallest (readable) font, the number of
average lines visible to any user is well < 100. So, generally speaking, we
should never need to do much more than count to 100 at any given point in
time to update our "gutter" area containing line numbers.
So, here's a little script taking from my editor. It assumes that the the
gutter area is always sized to the same height as the editor, that the line
height is fixed (read: we're not embedding images or changing the font
size), and that the editor has line wrapping turned off. With a few tweaks
it could work just fine with those features on, but for the sake of this
discussion, we'll leave them off.
-- wipe the gutter clean
put empty into fld "Line Numbers"
-- snag what we care about from the editor
put the vScroll of fld "Editor" into tScroll
put the effective textHeight of fld "Editor" into tTextHeight
put the height of fld "Editor" into tHeight
-- calculate the first visible line and the total number of visible lines
put tScroll div tTextHeight into tFirstRow
put tHeight div tTextHeight into tRows
-- fill in the gutter with the line numbers
repeat with tLine = tFirstRow to tFirstRow + tRows
put tLine + 1 & cr after fld "Line Numbers"
-- scroll the gutter (every so slightly) to match the editor
set the textSize of fld "Line Numbers" to the textSize of fld "Editor"
set the textFont of fld "Line Numbers" to the textFont of fld "Editor"
set the vScroll of fld "Line Numbers" to tScroll mod tTextHeight
That's pretty much it. Now we need to know when to update the gutter area.
We need to update it when:
* on scrollbarDrag (the editor)
* when the we reset the editor to display something new
* when we resize the editor (and subsequently the gutter)
More information about the use-livecode