/* TurboText.m
 * Purpose:  A subclass of the Text object.  This is necessary in order
 * 	to over-ride mouseDown.  The -convertPoint: method posts a fake
 *	mouse up event at the desired x,y coordinate location.  It then sets
 *	an internal flag and calls mouseDown with a fake mouse down event.
 *	The end result is that the Text object will believe that the user has
 *	clicked the mouse on the desired location and will cause a selection
 *	at the appropriate point.
 *
 * You may freely copy, distribute, and reuse the code in this example.
 * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
 * fitness for any particular use.
 *
 * Written by: Sharon Zakhour
 * Created: 22/July/91
 *
 */
 
#import <dpsclient/event.h>
#import "TurboText.h"

@implementation TurboText

- initFrame: (const NXRect *)frameRect
{
	[super initFrame:frameRect];
	convert = NO;
	charPos = -1;
	return self;
}

- mouseDown:(NXEvent *)theEvent
{
	NXSelPt	origStart, origEnd;
	
	if (convert)
	{
		/* Squirrel away the original selection */
		[self getSel: &origStart  :&origEnd];
		
		/* This is to hide the modified selection from the user */
		/* We want to avoid any flickering the user may see */
		[window disableFlushWindow];
	}
	
	/* Let the mouseDown in the text object do it's stuff... */
	[super mouseDown:theEvent];
	
	if (convert)
	{
		NXSelPt	newStart, newEnd;
		
		/* Get the new character position and snarf it away */
		[self getSel: &newStart  : &newEnd];
		charPos = newStart.cp;
		
		/* Restore the original selection */
		[self setSel: origStart.cp  :origEnd.cp];
		[window reenableFlushWindow];
		
		convert = NO;
	}
	return self;
}

- (int)convertPoint: (NXPoint *)myPoint
{
	NXEvent postEvent = (NXEvent)*[NXApp currentEvent]; 
	
	/* Post the mouse up event to the queue */
	convert = YES;	
	[self convertPoint: myPoint toView:nil];
	postEvent.location.x = myPoint->x;
	postEvent.location.y = myPoint->y;
	postEvent.type = NX_MOUSEUP;
	DPSPostEvent(&postEvent, YES);
	
	/* Call the Text object with the mouse down event...  */
	/* It will process the mouse down event followed by the */
	/* mouse up event which it will pluck off the queue.  */
	/* These fake events will cause the mouseDown code */
	/* to make a selection at the desired location.  The character */
	/* position can then be extracted from the start point of the */
	/* selection.   */
	postEvent.type = NX_MOUSEDOWN;
	postEvent.data.mouse.click = 1;
	[self mouseDown:&postEvent];
	return charPos;
}


@end
