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


#import <math.h>

#import "XQ_UserFunction.h"


@interface range_demo:XQ_UserFunction
@end




@implementation range_demo

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

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

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

- (int)minArg;
{
	return 1;
}

- (int)maxArg;
{
	return 1;
}

- (int)formalCount;
{
	return 1;
}

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

- (void *)runFunction: (void *)frame;
{
	int fromRow = 0, fromCol = 0, toRow = 0, toCol = 0;
	double sum = 0;
	int row, col;
	int xType;
	
    // Get arg type
    
	xType = XQ_ArgType(frame, 0);
	
	if(xType != XQ_RANGE_ARG) {
		XQ_RaiseArgTypeError(frame, 0, XQ_RANGE_ARG);
	}
	
    // Get range values
    
	XQ_GetRangeValues(frame, 0, &fromRow, &fromCol, &toRow, &toCol);
	
    // Calculate the sum
    
    	for(row=fromRow; row<=toRow; row++) {

		for(col=fromCol; col<=toCol; col++) {
	
			sum += XQ_GetCellDoubleValue(frame, row, col);
			
		}

	}
	
    // Return
    
	return XQ_CreateDoubleValue(frame, sum);
}

@end
