
#import "app.h"

#define GRAPH_NAME	"MyGraph4"

@implementation GraphityAPIDemo4

- reportError: (const char *)message;
{
	NXRunAlertPanel("Problem!", message, "OK", NULL, NULL);
	return self;
}

- initServer;
{
	BOOL x;
	
	if(theServerProxy != nil) return theServerProxy;
	
    // Check if Graphity launched
	
	x = [[Application workspace]
			launchApplication: GRAPHITY_NAME];

	if(x == NO) {
		[self reportError: "Could not launch Graphity!"];
		return nil;
	}

	theServerProxy = [NXConnection connectToName: GRAPHITY_SERVER];
	
	if(theServerProxy == nil) {
		[self reportError: 
			"Could not connect to the Graphity Server!"];
		return nil;
	}
	
	[theServerProxy setProtocolForProxy: @protocol(GraphityRootInterface)];
	
	return theServerProxy;
}

- newDocument:sender;
{
	if([self initServer] == nil) return nil;
	
	theDocumentProxy = [theServerProxy API_newDocument];
	
	if(theDocumentProxy == nil) {
		[self reportError: "Graphity could not create new doc!"];
		return nil;
	}
	
	[theDocumentProxy setProtocolForProxy: 
				@protocol(GraphityDocumentInterface)];

	[theAddButton setEnabled:YES];
	[theShapeButton setEnabled:YES];
	
    	return self;
}

- addGraph:sender;
{
	int error;
	const char *name = GRAPH_NAME;
	
	theGraphProxy = [theDocumentProxy API_addGraph: GRAPHITY_BAR_GRAPH
				with: (char *)name];
	
	if(theGraphProxy == nil) {
		[self reportError: "Graphity could not create new graph!"];
		return nil;
	}

	[theGraphProxy setProtocolForProxy: 
				@protocol(GraphityGraphInterface)];

	[theGraphProxy API_redisplay];
	
	[theAddSerieButton setEnabled:YES];
	
    	return self;
}

- addSerie:sender;
{
	id r;
	const char *name = GRAPH_NAME;
	const char *serieTitle = [theTitle stringValue];
	
	r = [theGraphProxy API_addSerie:serieTitle];
	
	if(r == nil) {
		[self reportError: "Graphity could not add serie!"];
		return nil;
	}

	[theGraphProxy API_recalculate];
	
	[theAddRowButton setEnabled:YES];
	
    	return self;
}

- addRow:sender;
{
	id r;
	const char *name = GRAPH_NAME;
	const char *label = [theLabel stringValue];
	
	r = [theGraphProxy API_addRow:label];
	if(r == nil) {
		[self reportError: "Graphity could not add row!"];
		return nil;
	}

	[theGraphProxy API_recalculate];
	
	[theSetValueButton setEnabled:YES];
	
    	return self;
}


- setGraphValue:sender;
{
	id r;
	const char *name = GRAPH_NAME;
	double value = [theValue doubleValue];
	int row = [[thePlace findCellWithTag: 0] intValue];
	int serie = [[thePlace findCellWithTag: 1] intValue];
	
	r = [theGraphProxy API_setValueAt:row :serie
		    	to:value];
		    
	if(r == nil) {
		[self reportError: "Graphity could not set value!"];
		return nil;
	}
	
 	[theGraphProxy API_recalculate];

   	return self;
}

- shapeDocument:sender;
{
	id r = nil;
	float x = [[theShapeForm findCellWithTag:0] floatValue];
	float y = [[theShapeForm findCellWithTag:1] floatValue];
	float w = [[theShapeForm findCellWithTag:2] floatValue];
	float h = [[theShapeForm findCellWithTag:3] floatValue];
	NXRect aRect;
	
	NX_X(&aRect) = x;
	NX_Y(&aRect) = y;
	NX_WIDTH(&aRect) = w;
	NX_HEIGHT(&aRect) = h;
	
	r = [theDocumentProxy API_shapeDocument: &aRect];
	
	if(r == nil) {
		[self reportError: "Graphity could not shape document!"];
		return nil;
	}
	
    	return self;
}


@end
