Handling mouse clicks

Handling a mouse-down event in a displayed line of text requires two actions:

  1. Mapping the physical position of the mouse event to an insertion offset within the text
  2. Determining the visual caret location(s) for that insertion offset
To map the mouse position to a
TInsertionOffset, use the TTextLineLayout function GetInsertionPoint. GetInsertionPoint also takes a Boolean value, UseVisualEndOfLine, that specifies whether the function should select the leftmost or rightmost character (as appropriate) when the mouse position is before or after the line. This value is True by default.

When you have the TInsertionOffset, use the GetCaretPositions function to compute the visual location for the carets. GetCaretPositions returns both a primary and a secondary caret location. Normally, the insertion point requires only one caret, and the primary and secondary positions will be the same. For bidirectional text, which may require two carets, the primary caret is always closest to the character backed by the text index in the TInsertionOffset.

So, for example, to draw a caret based on a mouse click, a text editing application might do the following:

  1. Get the position of the mouse click.
  2. Use the Y coordinate of the mouse-click position to determine what line was hit, based on a line table.
  3. Create a TTextLineLayout object for the line, or retrieve it from the cache.
  4. Convert the mouse position to a TInsertionOffset with the GetInsertionPoint function.
  5. Determine the visual position of the caret with the GetCaretPositions function.
  6. Check to see if the primary and secondary caret positions are equal. If they are, draw a single caret; if not, draw dual carets.
TTextLineLayout also provides some special purpose functions for mapping mouse positions to text that you won't normally need to use. These functions are:

See the online class and member function descriptions for more information on these functions and their usage.


[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker