Page 1 of 2
dgVisibleLines for "normal" fields
Posted: Wed Dec 14, 2022 9:48 pm
by bamakojeff
Do fields in livecode have any way to tell which lines are visible in the viewport? Like the dgVisibleLines property for a datagrid.
I mean lines here as the livecode chunk objects, text separated by a return. "Lines" as in 'the number of lines in fld "X"'.
Re: dgVisibleLines for "normal" fields
Posted: Thu Dec 15, 2022 4:45 am
by Emily-Elizabeth
Code: Select all
put (the height of field 1 / the effective textHeight of field 1)
This will let you know how many lines are visible. Replace
field 1 with your field information.
Re: dgVisibleLines for "normal" fields
Posted: Thu Dec 15, 2022 4:54 am
by bamakojeff
Thank you. But I want to know WHICH lines are visible. Your code tells me how many lines can be seen in the viewport, but not which lines. I want to know that lines 3 to 5 are visible or that line 7 is visible.
The dgVisibleLines property of a datagrid gives you this information. It returns a comma separated list of the line at the top of the viewport and the line at the bottom. So in my above example, it would return "3,5" and "7,7".
Re: dgVisibleLines for "normal" fields
Posted: Thu Dec 15, 2022 10:04 am
by Klaus
Hi Jeff,
welcome to the forum!
Each line in a "normal" field can have a HIDDEN property!
Looks like we have to set this for each line in a field separately, but no problem with a repeat loop:
Code: Select all
...
put "1,3,5,42" into tLines2Hide
lock screen
## important for speed!
repeat for each item tLine in tLines2Hide
set the hidden of line tLine of fld "your field here" to TRUE
end repeat
unlock screen
...
Similar for getting info about which lines are hidden in a field.
If you need this info more than once you could write a little function for that.
Best
Klaus
P.S.
Personal note:
A little "Hello" or something would not have hurt for the very first posting.
Re: dgVisibleLines for "normal" fields
Posted: Thu Dec 15, 2022 3:08 pm
by bamakojeff
Thank you Klaus, for the technical info as well as the etiquette info. My apologies for being brusque. I've been coding for 40 years, but am entirely new to livecode. It's been a long time since I joined a new forum, and I'd forgotten my manners.
I know about the "visible" property, but didn't know there was also a "hidden" property. I will look into that.
Thank you again.
Jeff
Re: dgVisibleLines for "normal" fields
Posted: Thu Dec 15, 2022 3:21 pm
by Klaus
Hi Jeff,
my pleasure!
Please come here if you get stuck, we have a lot of answers!
Best
Klaus
Re: dgVisibleLines for "normal" fields
Posted: Thu Dec 15, 2022 10:05 pm
by jacque
The hidden property doesn't apply to lines that are scrolled out of view, it only designates lines that should not be rendered into a field. The lines that are visible after scrolling can be calculated, starting with Emily-Elizabeth's calculation.
Code: Select all
on mouseUp
put (the height of field 1 / the effective textHeight of field 1) into tNumVis -- visible lines
put round((the vScroll of fld 1 + the topmargin of fld 1) / the effective textHeight of field 1) into tStartLine
put tStartLine + tNumVis - 1 into tEndLine
get the formattedText of fld 1
put line tStartLine to tEndLine of it into tContent
end mouseUp
LC defines a line as any text delimited by a carriage return; basically, a paragraph. So if you want to get the actual text of each wrapped line instead of each paragraph, use the formattedText to add returns to the end of each visible line temporarily. That doesn't change the actual field wrap, the returns only exist in the variable.
The handler only works if the field's fixedLineHeight property is set to true.
Re: dgVisibleLines for "normal" fields
Posted: Thu Dec 15, 2022 11:07 pm
by jiml
or this:
Code: Select all
go url "https://netrin.on-rev.com/misc/VisibleLines.livecode"
JimL
Re: dgVisibleLines for "normal" fields
Posted: Fri Dec 16, 2022 9:11 am
by Klaus
Hi Jacque,
jacque wrote: ↑Thu Dec 15, 2022 10:05 pm
The hidden property doesn't apply to lines that are scrolled out of view, it only designates lines that should not be rendered into a field...
really?
The dictionary does not mention this at all!?
Best
Klaus
Re: dgVisibleLines for "normal" fields
Posted: Fri Dec 16, 2022 3:32 pm
by bn
jiml wrote: ↑Thu Dec 15, 2022 11:07 pm
or this:
Code: Select all
go url "https://netrin.on-rev.com/misc/VisibleLines.livecode"
JimL
Thanks Jim for the example stack. For those who are not familiar with "go url" it means you copy that line into the message box and hit return and the stack will be loaded into LC.
I was intrigued by the problem and tried to speed it up for longer texts. The technique I used is similar to Jim's approach of checking the formatted top and bottom of a line but I do a logarithmic search beforehand to find a line that is currently visible and then proceed to top and bottom of the lines to determine the visible lines.
It is considerably faster for longer texts because logarthmic search only uses a couple of tries to find a suitable starting line.
It was fun coding.
Kind regards
Bernd
Re: dgVisibleLines for "normal" fields
Posted: Fri Dec 16, 2022 7:03 pm
by jacque
Klaus wrote: ↑Fri Dec 16, 2022 9:11 am
jacque wrote: ↑Thu Dec 15, 2022 10:05 pm
The hidden property doesn't apply to lines that are scrolled out of view, it only designates lines that should not be rendered into a field...
really?
The dictionary does not mention this at all!?
Before I wrote, I had to test it to be sure. If you scroll down in the field until line 1 is out of view and then put this in the message box:
LC says it's false.
Re: dgVisibleLines for "normal" fields
Posted: Fri Dec 16, 2022 7:13 pm
by Klaus
Thanks for the confirmation.
Re: dgVisibleLines for "normal" fields
Posted: Fri Dec 16, 2022 10:54 pm
by jacque
@Bernd, very nice as usual.

But both yours and Jim's functions only count lines as defined by LC; that is, they count paragraphs. I'm not sure if the OP needs formatted lines, which is how I interpreted "visible lines", or if paragraphs are enough. Both yours and Jim's could easily work either way though, by getting the formattedText if that's what's wanted.
My method is a little flawed in that it counts partial lines sometimes, so in that respect these are better.
Re: dgVisibleLines for "normal" fields
Posted: Sat Dec 17, 2022 1:26 pm
by bn
Hi Jacque,
Thanks.
I'm not sure if the OP needs formatted lines, which is how I interpreted "visible lines", or if paragraphs are enough
The original poster wanted paragraphs not "wrapped" lines.
bamakojeff wrote: ↑Wed Dec 14, 2022 9:48 pm
Do fields in livecode have any way to tell which lines are visible in the viewport? Like the dgVisibleLines property for a datagrid.
I mean lines here as the livecode chunk objects, text separated by a return. "Lines" as in 'the number of lines in fld "X"'.
Kind regards
Bernd
Re: dgVisibleLines for "normal" fields
Posted: Sat Dec 17, 2022 5:52 pm
by jacque
Oh! In that case you definitely answered the question.