ConvertXYtoChar
by Sharon Zakhour, NeXT Developer Support Team
valid for 3.0

Overview

This example shows how to convert from an X,Y coordinate location in the Text object to the nearest character position in that object.  This solution uses a bit of fakery -- posting some fake events to the event queue such that the Text object believes a selection has been made.  The NXSelPt structure contains character position and X,Y coordinate information.  So if the Text object can be made to select the desired location, then the corresponding character position is available from that structure.

Program Organization

How to build the nib files
There is one nib file in this example:

ConvertXY.nib		The main nib file contains the main menu, the main window and the classes.  The main window contains a ScrollView (which contains a standard Text object as it's docView) and some Matrices of TextFields and a Button which are connected to the Controller instance via several outlets.

Classes in the Application

Controller 	Subclass of Object.   This class serves as the application delegate.  This class is also responsible for swapping out the standard Text object from the ScrollView with my own subclass of Text -- TurboText.  This is done in the appDidInit: method after which the window is made visible.  (The initial display of the window is suppressed in IB so that we can make these changes without seeing any flicker on screen.)  The convert: method is tied to the "Convert To" Button in the window.  This action causes the X,Y coordinates to be read and converted to a character position and the resulting position and adjusted coordinates to be printed back into the window.  The new character position is also echoed with the blinking cursor.

TurboText	Subclass of Text.  TurboText maintains two instance variables which are used to keep track of character position.  TurboText overrides mouseDown: as well as implements a new method convertPoint: which is given an X,Y coordinate and returns a character position.  The convertPoint: method posts a fake mouse up event to the event queue, sets an internal boolean and calls mouseDown: with a mouse down event.  This faked sequence of events will lead the Text object to believe that the user clicked on the desired X,Y location and will cause a selection at that location.  After the character position has been extracted, the previous selection is restored.  In the process of calculating the nearest character position, the X,Y coordinates are adjusted accordingly.  The new coordinates are displayed in the main window.

Topics Of Interest

How to swap a new Text object into an existing ScrollView
This is also documented in the NeXTanswer appkit.555.  An additional feature shown in this example is the saving of the RTF text in the original Text object and writing it to the new TurboText object via streams.  This is done in the replaceText method of Controller.

Other Files
ConvertXY_main.m,		Created by Interface Builder.
PB.project,
Makefile,
ConvertXY.iconheader

Acknowledgments
Thanks to Dick Phillips at Los Alamos National Labs for giving me the idea for this elegant little hack!