Adventures in TextInputCanvas : 3

So far in the first 2 sections we’ve dealt with all the simple bits :slight_smile:
And now we need to step back and understand how the events TextInputCanvas work, what certain parameters mean, and what we need to return to respond to the events raised.

Reading the C++ and Objective-C code probably is not something anyone wants to do - and probably why you’re reading these posts of mine.

Lets look at one event and understand what has to happen to make that even “work” the way the TextInputCanvas expects. In Xojo the event we’ll look at is BaselineAtIndex

Event BaselineAtIndex(index as integer) as integer
  system.debuglog currentmethodname
End Function

The C++ comments say

// 
//  The user should return the view-local
//  y coordinate of the baseline for the text at the given character index.
// 
// Gets: control - the control
//       index - the zero-based character index
// Returns: the y coordinate, in the control's coordinate space

What this tells us is

  • index is the position of the character and its a LINEAR index. The first character is index 0 the next 1 etc. We’ll have to be consistent in how we handle end of lines as well here.
  • the “view local y coordinate” is the position, from the top of the control, of this characters baseline (note we’ll have to take scrolled text into account as well)

So “TEXT” in the text input canvas is, from the TextInputCanvas base class, one giant long string. How we want to handle this is up to us. If we want to store it as “lines” that fine - we just have to stick to being able to return things based on zero based linear indexes.

Other events, like TextForRange, use a Range - but both are index positions in the same way as above

Event TextForRange(range as TextRange) as string
  system.debuglog currentmethodname
End Function

Other events, like TextLength also expect the return value will be the length of its content (in characters).
//

Event TextLength() as integer
  system.debuglog currentmethodname
End Function

How we implement the storage of the text remains open.
We could use a giant Xojo string and just manage it with mid, left, right and other string manipulation operations. For some things this may well be fast enough. For handling large amounts of text its unlikely to suffice.

For THIS series I am going to just use a string because the focus here is on the TextInputCanvas, and how to implement the events of it and NOT on how to implement a really fast string buffer.

5 Likes

:heart: :heart: :heart: :heart: :heart: :heart: :heart: :heart: :heart: :heart: :heart: :heart: