
/* Generated by Interface Builder */

#import "StereoController.h"
#import "StereoView.h"
#import "Help.h"
#import "DefaultHandler.h"
#import "SterOptikonApp.h"
#import <appkit/Application.h>
#import <appkit/Listener.h>
#import <appkit/Pasteboard.h>
#import <appkit/OpenPanel.h>
#import <appkit/ScrollView.h>
#import <appkit/Text.h>
#import <appkit/TextField.h>
#import <stdlib.h>
#import <strings.h>
#import <string.h> 
#import <stdio.h>
#import <math.h>
#include <mach.h>

int	isCleared;

@implementation StereoController

id _defaults;
extern char TmpDir[];
extern char EyeOffset[];
extern char Cube[];
extern char Axes[];

#include "monitor.m"

- 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];
		}
/**/
	isCleared = NO;
	[theStereoView S_Reset:self];
	{
	char *openFileName;

	if (strcmp((openFileName = [NXApp getInitialFile]),""))	{
		[theStereoView readSData:openFileName];
		strcpy(openFileName,"");
	}
}

	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]) {
		if(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;
}

- showHelpPanel:sender
{
	if(!helpPanel)
		helpPanel = [[Help alloc] init];
	[helpPanel generalHelp: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
