// Legendre.m
// By Charles G. Fleming, Educational Computing Services, Allegheny College.
// Copyright 1993, Allegheny College.
// You may freely copy, distribute and reuse this code. 
// Allegheny College and the author disclaim any warranty of any kind, 
// expressed or implied, as to its fitness for any particular use.

#import "Legendre.h"

@implementation Legendre

// If the object has an inspector, we'll load it here.  Initialize variables
// that will be displayed by the inspector.
- init
{
	NXBundle *bundle;
	char path[MAXPATHLEN+1], inspectorName[MAXPATHLEN+1];
	
	[super init];

	strcpy(inspectorName, [[self class] name]);
	strcat(inspectorName, "Inspector");
	 
	bundle = [NXBundle bundleForClass:[self class]];

	// If the Inspector nib file exists, load it.  Otherwise, free the bundle.	
	if([bundle getPath:path forResource:inspectorName ofType:"nib"])
		[NXApp loadNibFile:path owner:self withNames:NO fromZone:[self zone]];
	else
		[bundle free];

	// Initialize variables that will be used by the inspector.
	legendreN = 1;
	legendreM = 0;
	plotRequested = YES;
	functionRequested = NO;
	plotMin = -1.0;
	plotMax = 1.0;
	inputForm = YES;
	return self;
}

- setMathConnector:anObject
{
	mathConnector = anObject;
	return self;
}

// Implement methods called by the inspector's ok: method.
- setLegendreN:(int)n
{
	legendreN = n;
	return self;
}	

- setLegendreM:(int)m
{
	legendreM = m;
	return self;
}	

- setPlotRequested:(BOOL)flag;
{
	plotRequested = flag;
	return self;
}	

- setFunctionRequested:(BOOL)flag;
{
	functionRequested = flag;
	return self;
}	

- setPlotMin:(float)min;
{
	plotMin = min;
	return self;
}	

- setPlotMax:(float)max;
{
	plotMax = max;
	return self;
}	

- setInputForm:(BOOL)flag;
{
	inputForm = flag;
	return self;
}	

// Implement methods called by the inspector's revert method.
- (int)legendreN
{
	return legendreN;
}
	
- (int)legendreM
{
	return legendreM;
}
	
- (BOOL)plotRequested
{
	return plotRequested;
}
	
- (BOOL)functionRequested
{
	return functionRequested;
}
	
- (float)plotMin
{
	return plotMin;
}
	
- (float)plotMax
{
	return plotMax;
}
	
- (BOOL)inputForm
{
	return inputForm;
}
	
// ****************************************************************************
// ****************************************************************************
// WBTool protocol methods.
// ****************************************************************************
// ****************************************************************************

- evaluate:sender
{
	char *commandString, *greaterSign;
	void *result;
	BOOL success = NO;
	int type;

	if(plotRequested)
	{
		// Build the Mathematica command doing the plot.
		commandString = malloc(100); 
		sprintf(commandString, "Plot[LegendreP[%d, %d, x], {x, %f, %f}]",
				legendreN, legendreM, plotMin, plotMax);		
		type = GRAPHICS;
		// Ask Mathematica to execute the command.
		success = [mathConnector evaluateExpression:commandString
				resultType:type result:&result];
		free(commandString);
		
		// Display the result if Mathematica successfully executed the command.
		if(success)
		{
			[outputText setText:(char *)result];
			free(result);
		}	
		else
			[outputText setText:""];
	}	

	if(functionRequested)
	{
		// Build the Mathematica command doing the plot.
		commandString = malloc(120); 
		sprintf(commandString, "wbPreviousResult = LegendreP[%d, %d, x]",
				legendreN, legendreM);		
		type = STRING;

		// Ask Mathematica to execute the command.
		success = [mathConnector evaluateExpression:commandString
				resultType:type result:&result];
		free(commandString);
		
		// Display the result if Mathematica successfully executed the command.
		if(success)
		{
			[outputText setText:(char *)result];
			[[outputText window] makeKeyAndOrderFront:self];
			free(result);
			
			if(inputForm)
			{
				commandString = malloc(30);
				strcpy(commandString, "InputForm[wbPreviousResult]");
				type = STRING;
				
				success = [mathConnector evaluateExpression:commandString
						resultType:type result:&result];
				free(commandString);
				
				if(success)
				{
					while(greaterSign = index((char *)result, '>'))
					*greaterSign = ' ';
					[inputText setText:(char *)result];
					free(result);
				}
			}
		}	
		else
			[outputText setText:""];
	}	
	return self;
}

// Set up the null terminated list of connectors that the tool will need.
- (char **)requiredConnectors
{
	static char *connectors[] = {"MathConnector", NULL};
	return connectors;
}

// Return the inspector.
- inspector
{
	return inspector;
}

- setInputText:text
{
	inputText = text;
	return self;
}

- setOutputText:text
{
	outputText = text;
	return self;
}

- setOutputView:view
{
	outputView = view;
	return self;
}

@end
