How to get the "effective lineOffset" with word wrap?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Tiemo
Posts: 31
Joined: Wed Feb 21, 2007 3:37 pm
Contact:

How to get the "effective lineOffset" with word wrap?

Post by Tiemo » Wed Feb 03, 2016 3:40 pm

Hello,
I want to create a kind of text hyperlink in a resizable scrolling field with word wrap.

I have a long text field with a table of contents at the beginning and headlines for chapters and I would like to make the table of content clickable as hyperlinks, so that the user is forwarded to the wanted chapter in the same field. I can set the textstyle of each chapter in the table of content to "link"
and handle the click in "on linkClicked pLink", no problem about that.

The question is, how to handle the field scrolling to the wanted chapter headline after clicking on the hyperlink. When using "find whole pLink in the target", the find command finds first the chapter headline in the table of content and stops at the top. I could cheat this, by doubleing the find command, so that the second find command goes on searching and finds the real chapter headline and scrolls the field to the wanted chapter.

Alternativly I also tried to work with lineOffset and setting the vScoll of the field. The lineOffset gives me the number of the found textline, but not the visible line number of a scrolling field with word wrap, which can be resized. In such a field the "effective" line number can vary, depending on the current size of the field.
Is there anything such as the "effective lineOffset", or even better a direct approach to scroll to the found chunck?

The double "find" command is the easiest, but not the nicest

Probably I don't see the obvious?
Thanks

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: How to get the "effective lineOffset" with word wrap?

Post by dunbarx » Wed Feb 03, 2016 3:58 pm

Hi.

if you do not mind cheating, like me, I have in the past prepended an invisible character in the "second" to-find line, and that distinguishes the two. I have even, and this is still cheating though not as sneaky, prepended a bullet (or something) in each line, so that it appears I am a graphic genius. So you find the first instance, and then the next.

But this is really no different than your double find method. I did it my way to be able to choose one over the other.

Craig Newman

Tiemo
Posts: 31
Joined: Wed Feb 21, 2007 3:37 pm
Contact:

Re: How to get the "effective lineOffset" with word wrap?

Post by Tiemo » Wed Feb 03, 2016 5:37 pm

nice idea Craig
Thanks
Tiemo

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

Re: How to get the "effective lineOffset" with word wrap?

Post by bn » Wed Feb 03, 2016 6:16 pm

Hi Tiemo,

here is a little stack that tries to do what you want.

It uses the formattedText of the field to find out where your found paragraph is. No need for special formatting. Text field can be changed to any width.
Once you click on the link in the table of contents then it finds the first occurrence of that text (the linkText) in the field and calculates the number of "apparent" lines = the real and wrapped lines using the formattedText and scrolls to that paragraph. I hope I understood what you were trying to do. It worked in my limited testing. No errorchecking in the script.

See comments in the script of the field.

Kind regards
Bernd
Attachments
scroll to paragraph.livecode.zip
(2.05 KiB) Downloaded 273 times

Tiemo
Posts: 31
Joined: Wed Feb 21, 2007 3:37 pm
Contact:

Re: How to get the "effective lineOffset" with word wrap?

Post by Tiemo » Wed Feb 03, 2016 6:28 pm

Hi Bernd,
works perfect!
Thanks for your ideas
Tiemo

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

Re: How to get the "effective lineOffset" with word wrap?

Post by bn » Wed Feb 03, 2016 7:49 pm

Hi Tiemo,

since Hermann mentioned selectedLoc on the list here is a version that is even shorter than the previous one. (I actually had forgotten about selectedLoc)

It uses the selectedLoc and vScroll to compute the appropriate vScroll of the field.

Kind regards
Bernd
Attachments
scroll to paragraph_2.livecode.zip
(1.81 KiB) Downloaded 237 times

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

Re: How to get the "effective lineOffset" with word wrap?

Post by bn » Wed Feb 03, 2016 8:01 pm

Hi Tiemo,

here is a solution using Mark Waddingham's suggestion on the use-list to use formattedHeight

I think Mark's solution is the easiest, using selectedLoc is 2nd place and my first solution using formattedText is the most complicated and probably slowest for long texts.

Kind regards
Bernd
Attachments
scroll to paragraph_3.livecode.zip
(1.81 KiB) Downloaded 252 times

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

Re: How to get the "effective lineOffset" with word wrap?

Post by bn » Thu Feb 04, 2016 12:36 pm

Jacque posted a version to solve the problem using a hash (#) turned invisible by setting its imagesource to the id of a 1 x 1 transparent image.

Using the hash makes the search for the chapter more robust since it is unlikely that the chapter will be in the text elsewhere.

However if the user decreases the width of the text or increases textSize word wrapping can make Jacque's solution fail.

Here is Jacque's solution

Kind regards

Bernd
Attachments
scroll to paragraph_4.livecode.zip
Jacque's solution
(1.95 KiB) Downloaded 231 times

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

Re: How to get the "effective lineOffset" with word wrap?

Post by bn » Thu Feb 04, 2016 12:39 pm

this is a version based on Jacque's suggestion but that avoids that text wrapping by changing font, width or textSize makes it fail due to text wrapping.

Kind regards

Bernd
Attachments
scroll to paragraph_5.livecode.zip
Jacque's solution enhanced
(2.14 KiB) Downloaded 262 times

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: How to get the "effective lineOffset" with word wrap?

Post by [-hh] » Fri Feb 05, 2016 12:17 am

Hi all,
if you play ping-pong with solutions here let me also come in and give yet another solution :-)

[ First of all, the solutions above are kind of valuable lib how to use effectively "effective" and "formatted" in connection to text. And how to use some more unusual things like imagesource. There is one more function that could become an item of such a collection: the formattedText, which is especially useful for line numbering.]

My solution here uses "TextMarks" (but not metadata or image sources). You may show or hide these marks by using the "hidden"-property.
  • There are also buttons for creating paragraph-headers from the beginning of the paragraphs for use by the marks-menu.
  • Also a button creates an (optional) linkList from such marks and puts it at top of your text. The links are connected, that is, you jump from top link to chapter and from chapter back to exactly that top link.
This solution is independent of textsize, textheight, textFont and resizing of the field. This is all implicitly solved by LC for us by using the selectedLoc.

It simply gets the char-offset of each mark, selects it and uses the selectedLoc to scroll the target to the vertical center of the field.
[What can arise problems is only the (optional) use of links. This needs after each edit of the number of links an update to have the correct number of link lines in front of the 'main' text.]

Scripts are very short, simple and fast (thanks to LC).

Note.
The stack is made for use with LC 6-8. If you use LC 7/8 only there are some improvements possible that could compensate the speed loss from 6 to 7/8: Use "paragraphs" and "trueWords" in the scripts.

[Edit. Removed a small bug.]
Attachments
TextMarksSimpleDemo2.livecode.zip
(14.07 KiB) Downloaded 240 times
shiftLock happens

Post Reply