
/* Generated by Interface Builder */

#import <appkit/appkit.h> 	
#import "StereoController.h"
#import "StereoView.h"
#import "DefaultHandler.h"
#import "SterOptikonApp.h"
int	isCleared;

@implementation StereoController

id _defaults;
extern char TmpDir[];
extern char EyeOffset[];
extern char Cube[];
extern char Axes[];
#import "monitor.m"

- (BOOL)appAcceptsAnotherFile:sender
{
	return YES;
}

- appDidInit:sender
{
	float floatVal;
	int	intVal;

		inputText = [theScrollView docView];
		[inputText setDelegate:self];
		[[NXApp appListener] setServicesDelegate:self];

		[[theStereoView window] makeKeyAndOrderFront:self];
/*
	Get default values and send them to theStereoView
*/
    _defaults = [DefaultHandler new];

	if (strcmp(EyeOffset,""))	{
		sscanf(EyeOffset,"%f",&floatVal);
		[theStereoView setEyeOffset:floatVal];
		}
	if (strcmp(Axes,""))	{
		sscanf(Axes,"%d",&intVal);
		[theStereoView setAxes:intVal];
		}
	if (strcmp(Cube,""))	{
		sscanf(Cube,"%d",&intVal);
		[theStereoView setCube:intVal];
		}

	strcpy(selectedFile,(char *)[NXApp getInitialFile]);
	[theStereoView readSData:selectedFile];
	[theStereoView setCube:YES];
	isCleared = NO;
	return self;
}


- requestPlot:(id)pasteboard userData:(const char *)userData error: (char **)msg
/*
 * Responds to a request from another application for the plotting service.
 * PlotController copies the data from the supplied pasteboard into the Text object
 * and then sends a plot: message to the StereoView.
 */
{
	char	*data, *scratch;
	int	 length;

	[NXApp activateSelf:NO]; 
	[pasteboard types];
	if ([pasteboard readType:NXAsciiPboardType data:&data length:&length]) {
			scratch = malloc(length+1);
			strncpy(scratch, data, length);
			scratch[length]='\0';
			[[inputText selectAll:self] replaceSel:scratch];
			free(scratch);
/*		vm_deallocate(task_self(), (vm_address_t)data, (vm_size_t)length);
*/
		[theStereoView S_Reset:self];
		[theStereoView textChanged:self];
		[theStereoView plot:self];
	}
	return self;
}

- quit:sender
{
/*
	sprintf(EyeOffset,"%f",[theStereoView getEyeOffset]);
	sprintf(Axes,"%d",[theStereoView getAxes]);
	sprintf(Cube,"%d",[theStereoView getCube]);
	[_defaults free];
*/
	[theStereoView closeRequest:self];
	[NXApp terminate:self];
	return self;
}

- showInfoPanel:sender
{
	if(!infoPanel)	{
		[NXApp loadNibSection:"Info.nib" owner:self];
	}
	[infoPanel makeKeyAndOrderFront:self];
	return self;
}

- showPrefPanel:sender
{
	[prefPanel makeKeyAndOrderFront:self];
	return self;
}

- windowDidResize:sender
{
/*	This is received after resizing */
/*	printf("windowDidResize in controller\n");
*/
	return self;
}

- windowWillResize:sender toSize:(NXSize *)aSize
{
/*	This is received upon mouse down in resize bar */
/*	printf("windowWillResize for controller\n");
*/
	return self;
}

- showError:(char *)errorMessage
{
  NXRunAlertPanel("Error", errorMessage, "OK", NULL, NULL);
  return self;
}

- textDidGetKeys:theText isEmpty:(BOOL)flag
/*
 * Responds to a message the Text object sends when its text changes.
 * StereoController causes the StereoView to clear itself when the text changes.
 */
{
	if ( isCleared ) return self;
	isCleared = YES;
	[theStereoView textEdited:self];
	return [theStereoView clear:self];
}

- stereoView:sender providePoints:(NXStream **)stream
/*
 * Responds to a message the PlotView sends requesting the points to plot.  
 * PlotController responds by giving the PlotView access to the Text 
 * object's stream.
 */
{
	int	endPos;

	endPos = [inputText byteLength];
	[inputText setSel:endPos :endPos];
	*stream = [inputText stream];
	[[theStereoView window] makeFirstResponder:theStereoView];
	isCleared = NO;
	return self;
}

- stereoView:sender pointDidChange:(char *)aLine
/*
 * Responds to a message the StereoView sends notifying its delegate 
 * that a line has been read.  Responds by adding the line to
 * the end of the list in the Text object.
 */
{
	int	endPos;
	char	buffer[100];

	sprintf(buffer, "%s\n", aLine);
	endPos = [inputText byteLength];
	[inputText setSel:endPos :endPos];
	[inputText scrollSelToVisible];
	[inputText replaceSel:buffer];
	return self;
}

- stereoView:sender clearText:(char *)aLine
/*
 * Responds to a message the StereoView sends notifying its delegate 
 * to clear the text.
 */
{
	int	endPos, beginPos;

	endPos = [inputText byteLength];
	beginPos = 0;
	[inputText setSel:beginPos :endPos];
	[inputText scrollSelToVisible];
	[inputText replaceSel:""];
	[[theStereoView window] makeFirstResponder:theStereoView];
	return self;
}

@end
