/* Controller.m
 * Purpose:  A subclass of Object and the main controlling class of
 *	the application.  Controller is the application delegate.
 *
 * 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 <stdio.h>
#import "TurboText.h"
#import "Controller.h"

@implementation Controller

/* Since we have subclassed the Text object but we have used */
/* The ScrollView from IB we need to replace the Text object in  */
/* the IB-generated ScrollView with our own subclass.  At the */
/* same time we want to preserve the text that was inserted into */
/* the view.  The replaceText method performs this swap.  This */
/* is also covered in the NeXTanswer  appkit.555. */
- appDidInit:sender
{
	/* Replace the Text object in the ScrollView with my */
	/* own TurboText object */
	[self replaceText];
	
	/* Now that we're ready, bring the window up */
	/* In IB, we've instructed the window NOT to become */
	/* visible at launch time so that we can make this swap */
	/* invisibly. */
	[[myScrollView window] makeKeyAndOrderFront: nil];
	return self;
}

/* This method is called when the user selects the ConvertTo */
/* button in the window.  The x,y coordinates are extracted from */
/* two form fields and the result is placed into yet-another */
/* form field.  At the same time, the character position is echoed */
/* in the ScrollView with an insertion cursor at the new position. */
- convert:sender
{
	NXPoint	myPoint;
	int	charPos;
	NXSelPt	start, end;
	NXRect  myRect;
	
	myPoint.x = [[charCoordinate cellAt: 0:0] floatValue];
	myPoint.y = [[charCoordinate cellAt: 1:0] floatValue];
	
	charPos = [[myScrollView docView] convertPoint: &myPoint];
	[[charPosition cellAt: 0: 0] setFloatValue: (float)charPos];
	
	/* Set the selection at the character position so that */
	/* we can see it.  This will also make the ScrollView the */
	/* first responder which is necessary to see the blinking */
	/* insertion point. */
	[[myScrollView docView] setSel: charPos :charPos];
	
	/* Now let's get the selection point back.  The actual x,y */
	/* coordinates have been adjusted by the Text object */
	/* and we want to update our textfields */
	[[myScrollView docView] getSel: &start :&end];

	/* scroll to the new location */
	myRect.origin.x = start.x;
	myRect.origin.y = start.y;
	myRect.size.width = 1.0;
	myRect.size.height = 20.0;
	[[myScrollView docView] scrollRectToVisible:&myRect];

	[[charPosition cellAt: 1:0] setFloatValue: start.x];
	[[charPosition cellAt: 2:0] setFloatValue: start.y];
	return self;
}

/* Swaps a standard Text object from IB with my own.  See */
/* NeXTanswer appkit.555 for more information. */
- replaceText
{
	NXStream *s = (NXStream *)nil;
	id		stdDoc, newDoc;
	NXRect	r;
		
	/* Measure the old doc view, then trash it. */
	stdDoc = [myScrollView docView];
	[stdDoc getFrame:&r];

	/* Get the entire text, using the rich RTF format */
	s = NXOpenMemory(NULL, 0, NX_READWRITE);
	if (s)   [stdDoc writeRichText: s];

	[myScrollView setVertScrollerRequired:YES];
	[myScrollView setHorizScrollerRequired:NO];
	[myScrollView setDynamicScrolling:YES]; 
	
	newDoc = [[TurboText alloc] initFrame:&r];
	[newDoc moveTo:0.0:0.0];
	[newDoc notifyAncestorWhenFrameChanged: YES];
	[newDoc setVertResizable:YES];
	[newDoc setSelectable:YES];
	[newDoc setEditable: YES];
	[newDoc setAutosizing:NX_WIDTHSIZABLE];
	[newDoc setMinSize:&r.size];
	[newDoc setMonoFont:NO];
	r.size.height = 1.0e30;
	[newDoc setMaxSize:&r.size];
	[myScrollView setDocView:newDoc];
	[stdDoc free];
	
	/* Stick the text from the original doc into the new doc */
	if (s)
	{
		/* Rewind to the beginning of the stream */
		NXSeek(s, 0L, NX_FROMSTART);
		[newDoc readRichText:s];
		NXCloseMemory(s, NX_FREEBUFFER);
	}

	return self;
}

@end
