dgVisibleLines for "normal" fields

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

bamakojeff
Posts: 33
Joined: Tue Dec 06, 2022 6:53 pm

dgVisibleLines for "normal" fields

Post by bamakojeff » 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"'.

Emily-Elizabeth
Posts: 101
Joined: Mon Jan 03, 2022 7:10 pm

Re: dgVisibleLines for "normal" fields

Post by Emily-Elizabeth » Thu Dec 15, 2022 4:45 am

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.

bamakojeff
Posts: 33
Joined: Tue Dec 06, 2022 6:53 pm

Re: dgVisibleLines for "normal" fields

Post by bamakojeff » Thu Dec 15, 2022 4:54 am

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".

Klaus
Posts: 14190
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: dgVisibleLines for "normal" fields

Post by Klaus » Thu Dec 15, 2022 10:04 am

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.

bamakojeff
Posts: 33
Joined: Tue Dec 06, 2022 6:53 pm

Re: dgVisibleLines for "normal" fields

Post by bamakojeff » Thu Dec 15, 2022 3:08 pm

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

Klaus
Posts: 14190
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: dgVisibleLines for "normal" fields

Post by Klaus » Thu Dec 15, 2022 3:21 pm

Hi Jeff,

my pleasure!
Please come here if you get stuck, we have a lot of answers! :-)


Best

Klaus

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7390
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: dgVisibleLines for "normal" fields

Post by jacque » 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. 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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

jiml
Posts: 339
Joined: Sat Dec 09, 2006 1:27 am

Re: dgVisibleLines for "normal" fields

Post by jiml » Thu Dec 15, 2022 11:07 pm

or this:

Code: Select all

go url "https://netrin.on-rev.com/misc/VisibleLines.livecode"
JimL

Klaus
Posts: 14190
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: dgVisibleLines for "normal" fields

Post by Klaus » Fri Dec 16, 2022 9:11 am

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? :shock:
The dictionary does not mention this at all!?

Best

Klaus

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: dgVisibleLines for "normal" fields

Post by bn » Fri Dec 16, 2022 3:32 pm

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
Attachments
testVisibleLinesLogarithmic.livecode.zip
(5.37 KiB) Downloaded 144 times

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7390
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: dgVisibleLines for "normal" fields

Post by jacque » Fri Dec 16, 2022 7:03 pm

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? :shock:
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:

Code: Select all

put the hidden of line 1 of fld 1
LC says it's false.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Klaus
Posts: 14190
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: dgVisibleLines for "normal" fields

Post by Klaus » Fri Dec 16, 2022 7:13 pm

Thanks for the confirmation.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7390
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: dgVisibleLines for "normal" fields

Post by jacque » Fri Dec 16, 2022 10:54 pm

@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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: dgVisibleLines for "normal" fields

Post by bn » Sat Dec 17, 2022 1:26 pm

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

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7390
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: dgVisibleLines for "normal" fields

Post by jacque » Sat Dec 17, 2022 5:52 pm

Oh! In that case you definitely answered the question.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply