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


#import <math.h>

#import "XQ_UserFunction.h"


@interface ref_demo2:XQ_UserFunction
@end




@implementation ref_demo2

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

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

- (const char *)argumentNameNo:(int)anIndex;
{
	switch(anIndex) {
	case 0 : 
		return "number";
	case 1 : 
		return "output_cell_1";
	case 2 : 
		return "output_cell_2";
	}
	
	return "<should never be seen>";
}

- (int)minArg;
{
	return 3;
}

- (int)maxArg;
{
	return 3;
}

- (int)formalCount;
{
	return 3;
}

- (int)argType:(int)index;
{
	switch(index) {
	
	case 0 : 
		return XQ_EVAL;		
			
	case 1 : 
		return XQ_DONT_EVAL;		// NOTE!
						// This causes the reference
						// to be passed to the function
						// Otherwise, the Questor
						// runtime passes the value
						// in the referenced cell 
	case 2 : 
		return XQ_DONT_EVAL;		// NOTE!
						// This causes the reference
						// to be passed to the function
						// Otherwise, the Questor
						// runtime passes the value
						// in the referenced cell 
	}
	return XQ_EVAL;
}

- (void *)runFunction: (void *)frame;
{
	int row1 = 0, col1 = 0;
	int row2 = 0, col2 = 0;
	int argType;
	double x;
	
    // Check arg type for first arg (should be a number)
    
	argType = XQ_ArgType(frame, 0);
	
	if(argType != XQ_DOUBLE_ARG) {
		XQ_RaiseArgTypeError(frame, 0, XQ_DOUBLE_ARG);
	}
	
    // Check arg type for second arg (should be a cell ref)
    
	argType = XQ_ArgType(frame, 1);
	
	if(argType != XQ_REF_ARG) {
		XQ_RaiseArgTypeError(frame, 0, XQ_REF_ARG);
	}
	
    // Check arg type for third arg (should be a cell ref)
    
	argType = XQ_ArgType(frame, 2);
	
	if(argType != XQ_REF_ARG) {
		XQ_RaiseArgTypeError(frame, 0, XQ_REF_ARG);
	}
	
    // Get the input number from the first argument
    
	x = XQ_GetDoubleValue(frame, 0);
	
    // Get output cell 1 indeces
    
	XQ_GetRangeValues(frame, 1, &row1, &col1, &row1, &col1);
	
    // Get output cell 2 indeces
    
	XQ_GetRangeValues(frame, 2, &row2, &col2, &row2, &col2);
	
    // Set the value of output cell 1 to ceil(x) 
    
    	XQ_setCellValue(frame, 
			row1, 
			col1, 
			XQ_CreateDoubleValue(frame, ceil(x)));

    // Set the value of output cell 2 to floor(x) 
    
    	XQ_setCellValue(frame, 
			row2, 
			col2, 
			XQ_CreateDoubleValue(frame, floor(x)));

    // Always return true
    
	return XQ_TrueValue();
}

@end
