Determining What Text is in the Current Scrollview of Field

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

tjm167us
Posts: 50
Joined: Sat Dec 03, 2011 8:27 pm
Location: San Ramon, CA

Determining What Text is in the Current Scrollview of Field

Post by tjm167us » Sun Oct 27, 2013 9:33 pm

Hi all,
Is there a built-in function that returns what text is in the current view of a scrolling text field? Or do I need to write one on my own?
Thanks!
Tom

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9663
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Determining What Text is in the Current Scrollview of Fi

Post by dunbarx » Sun Oct 27, 2013 10:43 pm

Hi.

LC sometimes has just what you need, but I think in this case you need to roll your own.

The tools you will need are "the formattedHeight", the "textHeight", the "scroll", and the like. Here is a start, given a field 1 where you have set the textheight to some value:

Code: Select all

on mouseUp
   put trunc(the scroll of fld 1 /  the textHeight of fld 1) + 1
end mouseUp
Does this make sense? Can you see how to get all the exposed lines with this? Write back if you get stuck, but do try first.

Craig Newman

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

Re: Determining What Text is in the Current Scrollview of Fi

Post by [-hh] » Mon Oct 28, 2013 7:14 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 11:58 am, edited 1 time in total.
shiftLock happens

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

Re: Determining What Text is in the Current Scrollview of Fi

Post by [-hh] » Mon Oct 28, 2013 9:16 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 11:57 am, edited 1 time in total.
shiftLock happens

tjm167us
Posts: 50
Joined: Sat Dec 03, 2011 8:27 pm
Location: San Ramon, CA

Re: Determining What Text is in the Current Scrollview of Fi

Post by tjm167us » Mon Oct 28, 2013 9:25 pm

Hi guys,
Thanks for the input. To solve my problem (at least partially), I used the fact that selecting text will move a scrollable text field automatically to select individual characters until the vScroll position was no longer 0+some minimum threshold I decided empirically. The toughest part of this is really just making sure that the function can sneak in like a ninja and get this information without messing with your current selections/focused controls, etc.

Code: Select all

function theLastVisibleCharacter theField
   put 1 into theLastCharIndex 
   put 10 into minPickup
   
   --preserve the current view
   put the focusedObject into thePreservedFocusedObject
   if the selectedText is not empty then
      put the selectedChunk into thePreservedSelectedChunk
   end if
   put the scroll of field theField into thePreservedScroll
   
   lock screen
   repeat for each char theChar in field theField
      select char theLastCharIndex of field theField
      if the VScroll of field theField < minPickup then
         add 1 to theLastCharIndex
      end if
   end repeat
   
   --reset the view to the way it was before I messed with it
   focus on thePreservedFocusedObject
   if thePreservedSelectedChunk is not empty then
      select char (word 2 of thePreservedSelectedChunk) to (word 4 of thePreservedSelectedChunk) of thePreservedFocusedObject
   end if
   set the vScroll of field theField to thePreservedScroll
   unlock screen
   
   return theLastCharIndex
end theLastVisibleCharacter
Where this doesn't work, however, is in determining the text currently showing on the screen when the vScroll is not 0. But, with a little imagination and a little more time, I will get this done too! Let me know if you have another cooler way of doing this...

Thanks,
Tom
Last edited by tjm167us on Tue Mar 15, 2016 6:19 pm, edited 1 time in total.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4002
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Determining What Text is in the Current Scrollview of Fi

Post by bn » Tue Oct 29, 2013 12:59 am

Tom,

you can combine Craigs suggestion with the formattedText of a field (that is the field that has wrapping text converted to text that has a return at each APPARENT line ending.

here is a quick stack

Kind regards
Bernd
Attachments
whatIsTheVisibleText.livecode.zip
(2.22 KiB) Downloaded 251 times

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

Re: Determining What Text is in the Current Scrollview of Fi

Post by [-hh] » Tue Oct 29, 2013 11:53 am

..........
Last edited by [-hh] on Wed Aug 13, 2014 11:57 am, edited 1 time in total.
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9663
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Determining What Text is in the Current Scrollview of Fi

Post by dunbarx » Tue Oct 29, 2013 2:40 pm

Hello -hh.

Why are you negative?

Anyway, since you are a fan of the mouseCharChunk (a very nice feature), note that the dictionary gives wrong information about what that function returns when over empty space. Whether below the last char or over empty lines between text, the function returns empty, not the chunk expression given. I added a user note about this.

Craig

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4002
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Determining What Text is in the Current Scrollview of Fi

Post by bn » Tue Oct 29, 2013 3:19 pm

Hi [-HH],
You have to correct calculating height if a horizontal scrollbar is present (the fld may have one if it was non-wrapping before) by the height of the hScrollbar
not really because this is for wrapping text and you would not want a horizontal scrollbar in a text field that wraps the text.
the formattedText is experimental until now.
not really. FormattedText is in LiveCode since version 1.0, some changes in 5.5. in the dictionary the note about experimental refers to listStyles
listStyle property is experimental
one has to think about adding (and take into account) spaces, if the text is ONE long word.
That is a problem of LIvecode that can at times be tricky. You have to be aware that a line in a livecode field has a limit of
65,536 characters storage
No more than 32,786 pixels wide for display
section 2.2.9 "LiveCode Memory Limits" Usere guide.
So no pi in livecode fields...
And a question:
A similar technique may be applied to each line of a non-wrapping field (then we have to correct width calculations by the width of the vScrollbar).
That is tricky. I don't know of a command that gives me the "visible" characters.
If I want to do this (and I would think hard to come up with a use case, except maybe pi, but see above), I use the formattedRect of a character. But one has to iterate through the chars of the visible lines to get the first and last character that is visible.

I attach a little stack that shows this. It works but would need more work to use in production grade projects.

Kind regards
Bernd
Attachments
whatIsTheVisibleText_0_2.livecode.zip
(3.5 KiB) Downloaded 234 times

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

Re: Determining What Text is in the Current Scrollview of Fi

Post by [-hh] » Tue Oct 29, 2013 7:05 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 11:57 am, edited 1 time in total.
shiftLock happens

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

Re: Determining What Text is in the Current Scrollview of Fi

Post by [-hh] » Tue Oct 29, 2013 7:30 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 11:57 am, edited 1 time in total.
shiftLock happens

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4002
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Determining What Text is in the Current Scrollview of Fi

Post by bn » Tue Oct 29, 2013 8:03 pm

Hi HH,

thank you for the kind words.

knowing some about your language background (german) one guy jokingly said that the german habit of compounding nouns into one new word, which can be quite long
e.g.

Lichtwellenleiterkabelverlegung

was not that the words are too long but life is just too short for them.

As far as the numbering system of the stacks is concerned: unfortunately I can not count up to 3. ;)

Kind regards
Bernd

PS I am shure Craig did not want to say you are negative in tone or mood but alluded to the - sign in front of your signature [-HH], he made a joke, but I guess Craig will chime in to explain that better than I can.

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

Re: Determining What Text is in the Current Scrollview of Fi

Post by [-hh] » Tue Oct 29, 2013 8:53 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 11:56 am, edited 1 time in total.
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9663
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Determining What Text is in the Current Scrollview of Fi

Post by dunbarx » Tue Oct 29, 2013 11:37 pm

Dear |hh|

There, now there can be no misunderstanding.

Craig

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

Re: Determining What Text is in the Current Scrollview of Fi

Post by [-hh] » Tue Oct 29, 2013 11:54 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 11:56 am, edited 1 time in total.
shiftLock happens

Post Reply

Return to “Talking LiveCode”