dgVisibleLines for "normal" fields
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 33
- Joined: Tue Dec 06, 2022 6:53 pm
dgVisibleLines for "normal" fields
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"'.
I mean lines here as the livecode chunk objects, text separated by a return. "Lines" as in 'the number of lines in fld "X"'.
-
- Posts: 101
- Joined: Mon Jan 03, 2022 7:10 pm
Re: dgVisibleLines for "normal" fields
Code: Select all
put (the height of field 1 / the effective textHeight of field 1)
-
- Posts: 33
- Joined: Tue Dec 06, 2022 6:53 pm
Re: dgVisibleLines for "normal" fields
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".
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
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:
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.
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
...
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.
-
- Posts: 33
- Joined: Tue Dec 06, 2022 6:53 pm
Re: dgVisibleLines for "normal" fields
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

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
Hi Jeff,
my pleasure!
Please come here if you get stuck, we have a lot of answers!
Best
Klaus
my pleasure!
Please come here if you get stuck, we have a lot of answers!

Best
Klaus
Re: dgVisibleLines for "normal" fields
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.
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.
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
The handler only works if the field's fixedLineHeight property is set to true.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: dgVisibleLines for "normal" fields
or this:
JimL
Code: Select all
go url "https://netrin.on-rev.com/misc/VisibleLines.livecode"
Re: dgVisibleLines for "normal" fields
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.jiml wrote: ↑Thu Dec 15, 2022 11:07 pmor this:JimLCode: Select all
go url "https://netrin.on-rev.com/misc/VisibleLines.livecode"
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
- Attachments
-
- testVisibleLinesLogarithmic.livecode.zip
- (5.37 KiB) Downloaded 144 times
Re: dgVisibleLines for "normal" fields
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:
Code: Select all
put the hidden of line 1 of fld 1
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: dgVisibleLines for "normal" fields
Thanks for the confirmation.
Re: dgVisibleLines for "normal" fields
@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.

My method is a little flawed in that it counts partial lines sometimes, so in that respect these are better.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: dgVisibleLines for "normal" fields
Hi Jacque,
Thanks.
Bernd
Thanks.
The original poster wanted paragraphs not "wrapped" lines.I'm not sure if the OP needs formatted lines, which is how I interpreted "visible lines", or if paragraphs are enough
Kind regardsbamakojeff wrote: ↑Wed Dec 14, 2022 9:48 pmDo 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"'.
Bernd
Re: dgVisibleLines for "normal" fields
Oh! In that case you definitely answered the question.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com