
#import <appkit/appkit.h>

#import "XQ_InterchangeCell.h"
#import "XQ_UserFunction.h"
#import "XQ_InterfaceObject.h"

#import "Questor_API_Demo3.h"

@implementation Questor_API_Demo3

//
// Write some fancy comments here
//

- sendToQuestor:sender;
{
	int fr = [sendRow intValue];
	int fc = [sendCol intValue];
	int tr = fr + 4;
	int tc = fc + 2;
	id valueList = nil;
	int i,j;
	id cell, val, interface;
	int delivered = 0;
	int ok = YES;
	
    // check range values
    
	if(fr < 0 || tc < 0) {
		NXBeep();
		return self;
	}
	
    // set up interface
	
	interface = [[XQ_Interface alloc] initForNewSheet];
	if(interface == nil) return self;
	
    // create a list of value cells
    
	valueList = [List new];
	for(i=0; i<5; i++) {
		for(j=0; j<3; j++) {
		
			cell = [fieldMatrix cellAt: i : j];
			
			val = [[XQ_InterchangeCell alloc] init];
			[val setCellType: XQ_DOUBLE_ARG];
			[val setDoubleValue: [cell doubleValue]];
		
			[valueList addObject: val];
		}
	}
	
	delivered = [interface XQ_sendValues: valueList
			fromRow: fr
			fromCol: fc
			toRow: tr
			toCol: tc
			ok: &ok];
	if(delivered != 0) {
		NXRunAlertPanel("Communication Problem",
		    "Could not communicate with Questor. Error code was %d",
		    "OK", NULL, NULL,
		    delivered);
	}
	if(!ok) {
		NXRunAlertPanel("Communication Problem",
		    "Xanthus Questor could not handle the sent data!",
		    "OK", NULL, NULL);
	}
	
	delivered = [interface XQ_recalculate];
	if(delivered != 0) {
		NXRunAlertPanel("Communication Problem",
		    "Could not tell sheet to recalculate itself. Error code was %d",
		    "OK", NULL, NULL,
		    delivered);
	}
	
	delivered = [interface XQ_redisplay];
	if(delivered != 0) {
		NXRunAlertPanel("Communication Problem",
		    "Could not tell sheet to redisplay itself. Error code was %d",
		    "OK", NULL, NULL,
		    delivered);
	}
	
	[[valueList freeObjects] free];
	[interface free];
	
	return self;
}
@end
