//
//	Xanthus
//	Copyright (c) 1992, 1993, 1994 Xanthus International AB.  
//	All rights reserved. 
//


#import <appkit/appkit.h>

#import "XQ_UserFunction.h"



@protocol LibraryDemo_Protocol
+ (int)foobar: (int)x : (int)y;
@end



@interface my_addin_function:XQ_UserFunction
@end

//
// NOTE: YOU MUST CHANGE THE libFolder BELOW TO
// THE PATH WHERE YOU HAVE PREVIOUSLY INSTALLED
// LibraryDemo.bundle
//
//


@implementation my_addin_function

- libClass;
{
	const char *libFolder = "/tmp/LibraryDemo.bundle";
	id libBundle = nil;
	static id libClass = nil;
	
	if(libClass != nil) return libClass;
	
    // CHANGE libFolder TO SOMETHING USEFUL!
	
    // Get a bundle for the library folder
	
	libBundle = [[NXBundle alloc] initForDirectory: libFolder];
	
    // Force class to to loaded
	
	libClass = [libBundle classNamed: "LibraryDemo"];
	
	if(libClass == nil)
		NXLogError("ERROR: No class LibraryDemo in %s", libFolder);
		
	return libClass;
}

- (void *)runFunction: (void *)frame;
{
	double x, y, result;
	int xType, yType;
	
	xType = XQ_ArgType(frame, 0);
	yType = XQ_ArgType(frame, 1);
	
	if(xType != XQ_DOUBLE_ARG) {
		XQ_RaiseArgTypeError(frame, 0, XQ_DOUBLE_ARG);
	}
	
	if(yType != XQ_DOUBLE_ARG) {
		XQ_RaiseArgTypeError(frame, 1, XQ_DOUBLE_ARG);
	}
	
	x = XQ_GetDoubleValue(frame, 0);
	y = XQ_GetDoubleValue(frame, 1);
	
    // Call the library routine foobar
    
	result = [[self libClass] foobar: x : y];
	
	return XQ_CreateDoubleValue(frame, result);
}

+ (const char *)functionName;
{
	return "my_addin_function";
}

+ (const char *)categoryName;
{
	return "demo_category";
}

- (const char *)argumentNameNo:(int)index;
{
	switch(index) {
	case 0 : 
		return "x";
	case 1 : 
		return "y";
	}
	
	return "<should never be seen>";
}

- (int)minArg;
{
	return 2;
}

- (int)maxArg;
{
	return 2;
}

- (int)formalCount;
{
	return 2;
}

- (int)argType:(int)index;
{
	switch(index) {
	case 0 : 
		return XQ_EVAL;
	case 1 : 
		return XQ_EVAL;
	}
	return XQ_EVAL;
}


@end
