Issue: formattedWidth of text in (a field & the target)

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
xeir
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 35
Joined: Thu Jul 03, 2008 5:54 am

Issue: formattedWidth of text in (a field & the target)

Post by xeir » Tue Jan 19, 2010 6:52 am

Here is a snippet of code to do something that I would think should be VERY easy, to get the formatted width of the text in a field.

This line works fine.
put the formattedWidth item 1 of fld "myField" into tFRect -- works, but is limited to that particular field!

But, I want to use a function to do this in multiple fields, so I tried this.
put the formattedWidth item 1 of the target into tFRect -- does NOT work !!

I have tried everything I can possible think of to get the correct result, but an error is returned every time. Any thoughts on why this doesn't work?

The reason for this is to make a custom handler to "show/hide the scrollbars" in a field as text goes outside the width of the field. So if anyone already happens to have such a handler and wouldn't mind sharing, by all means. ;)

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

Re: Issue: formattedWidth of text in (a field & the target)

Post by Klaus » Tue Jan 19, 2010 2:46 pm

Hi xeir,

well, it depends on how you call the function!
In a "mouseup" handler or what?


Best

Klaus

xeir
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 35
Joined: Thu Jul 03, 2008 5:54 am

Re: Issue: formattedWidth of text in (a field & the target)

Post by xeir » Tue Jan 19, 2010 6:15 pm

Klaus-

The following code is being placed in the script of a field.
The formattedWidth of the field is not being updated when RETURN is pressed.
This is why I need to get the formattedWidth of the text inside the field.

Code: Select all

on rawKeyUp keyCode
   shScroll keyCode
   --pass rawKeyUp
end rawKeyUp

on shScroll pKey
   put pKey & cr & the target into msg
   
   put the width of the target into tWidth
   put the formattedWidth of the target into tFWidth -- Not being updated properly, BUG?

   -- Return the formattedWidth of the text in the field
   put the formattedWidth of item 1 of fld "myField" into tTxtFWidth -- WORKS!
   --put the formattedWidth of item 1 of the target into tTxtFWidth -- Fail

   put cr & tWidth && tFWidth && tTxtFWidth after msg

end shScroll
Results using 'of fld "myField"':
65293
field "myField"
250 325 309

Results using 'of target':
65293
field "myField"

Error: field "myField": execution error at line 27 (Chunk: can't find object), char 27

For testing, I've just got a line of text that extends beyond the field width as such:
WWWWWWWWWWWWWWWWWWWWWW WWWWWWWWWWWW
And I'm pressing return where the space is, just for visual reference.

That's what is happening so far.

Regards

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

Re: Issue: formattedWidth of text in (a field & the target)

Post by dunbarx » Tue Jan 19, 2010 11:58 pm

I tried this code and it seems to work:

Code: Select all

   put the formattedWidth of (item 1 of the target) into tTxtFWidth
Not sure why, the original looks good.
Do you have the lockText set to true or false?

xeir
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 35
Joined: Thu Jul 03, 2008 5:54 am

Re: Issue: formattedWidth of text in (a field & the target)

Post by xeir » Wed Jan 20, 2010 4:46 am

dunbarx-

I tried with the quotes as you mentioned and it doesn't error out, YEAH!

Code: Select all

 put the formattedWidth of (item 1 of the target) into tTxtFWidth
However, there is BAD news, it's returning the same value as:

Code: Select all

 put the formattedWidth of the target into tTxtFWidth
Which is incorrect as it is returning the width of the field, not the text inside.

Very strange indeed, hmmm, if only the formattedWidth of the field properly returned the value when the field width shrinks it wouldn't be an issue at all.

Ohh, and lockText is false.

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Re: Issue: formattedWidth of text in (a field & the target)

Post by SparkOut » Wed Jan 20, 2010 11:08 am

You're trying to get the scrollbar to show if the text goes too far to the right, past the edge of the field, is that correct?

Then how about [edit][strike]simply[/strike] forcing the field to recalculate the formatted width by turning off the dontWrap setting and then turning it on again? (it wasn't as "simple" as my first code suggestion!)[/edit]:

Code: Select all

on rawKeyUp pKey
   shScroll pKey
   pass rawKeyUp
end rawKeyUp

on shScroll pKey
  --if the return key is the one pressed, then force a recalc of the formatted width 
  --by turning the dontWrap setting off and on again
   if pKey is 65293 then
      lock screen
      set the dontWrap of the target to false
      set the dontWrap of the target to true
      unlock screen
   end if
  --then use a boolean result to set the visibility of the scrollbar
   set the hScrollbar of the target to (the formattedWidth of the target > the width of the target)
end shScroll

xeir
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 35
Joined: Thu Jul 03, 2008 5:54 am

Re: Issue: formattedWidth of text in (a field & the target)

Post by xeir » Wed Jan 20, 2010 5:24 pm

SparkOut-

I knew there had to be some convolutedly easy way of doing that. :roll:

Prior, I did manage to get a result once through to the else statement when testing the formattedWidth, and it was when I changed the dontWrap, but for whatever reason, I didn't consider having to toggle it.

Much appreciated for the work around! This would have been so much easier if the the formattedWidth worked properly. Ahh well, what would have been the fun in that.

Regards!

Post Reply