Line numbers for soft-wrapped styled text?
hh
hh at hyperhh.de
Wed Mar 29 17:03:56 EDT 2017
> Jim L. wrote:
> Just set the liststyle of the field that contains your editable text to
> ‘decimal’. Then the lines will be automatically numbered and outdented.
> Yet conveniently, the ’text’ and the ‘styledtext’ runs will not ‘contain'
> the numbering since it is a styling.
Now I finally understood your brilliant solution. Thank you very much for that!
It beats in speed all other solutions until now.
In sum I learned very important things in this thread:
Using a styledText array (by Alex Tweedly).
Using liststyles for _any_ field (by Jim Lambert).
You could now think about making a "Sample stack" for that (livecodeshare).
One thing to remark:
One has to adjust the "indention/oudention" by using the left margin and the
first indent. One can even make this width-adaptive to the num of lines and
adjust it in a way to make the numbering looking 'right aligned':
## LineNumbering technique by Jim Lambert
## (applied below by for adaptive and right-aligned indention by -hh).
## You need a field "TEXT" of styled Text and a checkButton "ShowNumbers".
## -- btn "showNumbers
on mouseUp
if the hilite of me then showNumbers
else hideNumbers
end mouseUp
## -- fld "TEXT
on textchanged
if the hilite of btn "showNumbers" then showNumbers
else hideNumbers
end textchanged
on scrollbardrag
if the hilite of btn "showNumbers" then showNumbers
else hideNumbers
end scrollbardrag
## card script
local rg="TEXT"
on hideNumbers
lock screen; lock messages
set the liststyle of line 1 to -1 of fld rg to empty
set the firstindent of line 1 to -1 of fld rg to 0
set the margins of fld rg to 8
if there is a grc rg then hide grc rg
unlock screen; unlock messages
end hideNumbers
on showNumbers
lock screen; lock messages
set the hgrid of fld rg to true -- adjust to your taste
put the num of lines of fld rg into n0
-- as an empty last line is ignored by LC:
if char -1 of fld rg is cr then add 1 to n0
put length(n0) into L
set the borderColor of fld rg to "230,230,230"
put 10+L*8 into ww
repeat with i=1 to L
set the firstIndent of line 10^(i-1) to 10^i-1 \
of fld rg to -16-i*8
end repeat
set the margins of fld rg to (ww+10,8,8,8)
set the liststyle of line 1 to -1 of fld rg to "decimal"
if there is no grc rg then createNumBack rg
show grc rg
put the rect of fld rg into rct
set points of grc rg to \
(ww div 2 + item 1 of rct, item 2 of rct) &cr& \
(ww div 2 + item 1 of rct, item 4 of rct)
set linesize of grc rg to ww
unlock screen; unlock messages
end showNumbers
on createNumBack gg -- adust to your taste
if there is no grc gg then create grc rg
set style of grc gg to "line"
set capstyle of grc gg to "butt"
set opaque of grc gg to true
set the colorOverlay["color"] of grc rg to "230,230,230"
set blendlevel of grc rg to 70
end createNumBack
## known problem: incorrect 'rightalign' if after deleting the last line
## the new last line is an empty one and the length of the num changes.
###END_OF_SCRIPT
More information about the use-livecode
mailing list