DesktopTextArea CharacterPosition seems to be wrong

Could it be that the CharacterPosition(x,y) method in DesktopTextArea has an error?

If I include the following statements in the DesktopTextArea in the MouseDown event:

if IsContextualClick then
  
  var mouseAtCharPos as integer = me.CharacterPosition(x,y)
  var textLength     as integer = me.Text.Length

end if

then I always get the same result for the mouseAtCharPos and the textLength, although the XY position was at a different position in the text at each attempt.

I have tested this in macOS 14.4 with XOJO 2023R4.

Works fine here - also macOS 14.4 and Xojo 2023R4.

Textlength is always the same, as you ask for the length of the complete contents of the DesktopTextArea.
mouseAtCharPos is different every time when I right click at a different place in the DesktopTextArea.

Thanks for the test.

Yes, the text length is always the same.

Unfortunately, the position is also always the same for me. I can’t see what’s going wrong.

For me, mouseAtCharPos always corresponds to the text length.

Create a brand new project with only that code in it to check what happens…

How do you test this? Is there a break after “var length…” and the values are shown in the debugger?

Yes I have a Break in the Mousedown Event.

TextArea CharPosition

I’ve added a link to a movie (mp4) in MagentaCLOUD.

I have built a new project and added my extensions piece by piece. I did a test after each installation.
Now I have found the cause: It was the ruler.
I had it in the opening event of the TextArea.

Declare sub setRulerVisible lib "Cocoa" selector "setRulerVisible:" (obj_id as Ptr, value as Boolean)

setRulerVisible(ptr(documentView(me.Handle)), showRuler)

Now the Ruler is deactivated and CharacterPosition works again.
Since I can’t turn off the error, I’ll do without the Ruler.

I was curious to know (to be sure) what ```
setRulerVisible


No result, even with "Apple setRulerVisible"…

Are-you sure this is still available in 2024 macOS ?
Declare sub setRulerVisible lib "Cocoa" selector "setRulerVisible:" (obj_id as Ptr, value as Boolean)

setRulerVisible(ptr(documentView(me.Handle)), true)

These two lines display a ruler in the TextArea.
For the ruler to be visible, there must be a “true” at the end of the second line.
Because the ruler is shown in the TextArea, the TextArea.CharacterPosition will probably no longer work.

Xojo should probably fix this. Creating a report in Issue would be worth it.

1 Like

Are the returned values consistent ?

What is the difference from the real y position ?

Can’t you remove the Rule height to the y value and use that to compute the position ?

What you wrote makes sense since the function returns the y value from the top of the Control (in fact, the top writing part of the Control area).
Adding something there (in the Control’s Back) produce pertubation.

You may want to know (or remember) that CharacterPosition and related methods are broken, even without using the ruler at all. It’s been several years like that and, well, it’s not reliable.

https://tracker.xojo.com/xojoinc/xojo/-/issues/66178
https://tracker.xojo.com/xojoinc/xojo/-/issues/72962
https://tracker.xojo.com/xojoinc/xojo/-/issues/43874

(if I had access to the API1 version, I could search for more issues; here, I don’t remember the API terms)

Hello Arnaud, hello Emile!

I didn’t know that there were already issues for this problem. Since I don’t see a solution to the problem, I’ll look at the issues. Then I’ll make an upvote or a new issue.
But I can’t do that until after Easter.

Best regards, Michael