
#import "app.h"

#define GRAPH_NAME	"MyGraph"

@implementation GraphityAPIDemo1

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

- initSpeaker;
{
	port_t aPort;
	
	if(theSpeaker == nil) 
		theSpeaker = [[Graphity_APISpeaker allocFromZone:[self zone]]
					init];
	
	if([theSpeaker sendPort] != PORT_NULL) return self;
	
	aPort = NXPortFromName("Graphity", NULL);
	if(aPort == PORT_NULL) {
		[self reportError: "Could not launch Graphity!"];
		return nil;
	}
	[theSpeaker setSendPort:aPort];
	return self;
}

- newDocument:sender
{
	int x, error;
	const char *name = [theNameField stringValue];
	
	if([self initSpeaker] == nil)
		return nil;
		
	x = [theSpeaker API_newDocument: (char *)name error:&error];
	if(x != 0) {
		[self reportError: "Communication problem!"];
		return nil;
	}
	
	if(error != GRAPHITY_OK) {
		[self reportError: "Graphity could not create new doc!"];
		return nil;
	}
	
	[theNameField setEnabled:NO];
	[theAddButton setEnabled:YES];
	[theShapeButton setEnabled:YES];
	
    	return self;
}

- addGraph:sender
{
	int x, error;
	const char *name = GRAPH_NAME;
	const char *docname = [theNameField stringValue];
	
	if([self initSpeaker] == nil)
		return nil;
		
	x = [theSpeaker API_addGraph: GRAPHITY_BAR_GRAPH
			with: (char *)name
			in: (char *)docname 
			error:&error];
	if(x != 0) {
		[self reportError: "Communication problem!"];
		return nil;
	}
	
	if(error != GRAPHITY_OK) {
		[self reportError: "Graphity could not create new graph!"];
		return nil;
	}

	[theSpeaker API_redisplay:(char *)name
			in: (char *)docname 
			error:&error];
	
	[theAddSerieButton setEnabled:YES];
	
    	return self;
}

- addSerie:sender
{
	int x, error;
	const char *name = GRAPH_NAME;
	const char *docname = [theNameField stringValue];
	const char *serieTitle = [theTitle stringValue];
	
	if([self initSpeaker] == nil)
		return nil;
		
	x = [theSpeaker API_addSerie:(char *)serieTitle
			for:(char *)name
			in:(char *)docname
			error: &error];
	if(x != 0) {
		[self reportError: "Communication problem!"];
		return nil;
	}
	
	if(error != GRAPHITY_OK) {
		[self reportError: "Graphity could not add serie!"];
		return nil;
	}

	[theSpeaker API_recalculate:(char *)name
			in: (char *)docname 
			error:&error];
	
	[theAddRowButton setEnabled:YES];
	
    	return self;
}

- addRow:sender
{
	int x, error;
	const char *name = GRAPH_NAME;
	const char *docname = [theNameField stringValue];
	const char *label = [theLabel stringValue];
	
	if([self initSpeaker] == nil)
		return nil;
		
	x = [theSpeaker API_addRow:(char *)label
			for:(char *)name
			in:(char *)docname
			error: &error];
	if(x != 0) {
		[self reportError: "Communication problem!"];
		return nil;
	}
	
	if(error != GRAPHITY_OK) {
		[self reportError: "Graphity could not add row!"];
		return nil;
	}

	[theSpeaker API_recalculate:(char *)name
			in: (char *)docname 
			error:&error];
	
	[theSetValueButton setEnabled:YES];
	
    	return self;
}


- setGraphValue:sender
{
	int x, error;
	const char *name = GRAPH_NAME;
	const char *docname = [theNameField stringValue];
	double value = [theValue doubleValue];
	int row = [[thePlace findCellWithTag: 0] intValue];
	int serie = [[thePlace findCellWithTag: 1] intValue];
	
	if([self initSpeaker] == nil)
		return nil;
		
	x = [theSpeaker API_setValueAtRow:row
		    serie:serie
		    to:value
		    for:(char *)name
		    in: (char *)docname
		    display:GRAPHITY_RECALCULATE	
		    error: &error];
	if(x != 0) {
		[self reportError: "Communication problem!"];
		return nil;
	}
	
	if(error != GRAPHITY_OK) {
		[self reportError: "Graphity could not set value!"];
		return nil;
	}
	
    	return self;
}

- shapeDocument:sender
{
	int a, error;
	const char *docname = [theNameField stringValue];
	float x = [[theShapeForm findCellWithTag:0] floatValue];
	float y = [[theShapeForm findCellWithTag:1] floatValue];
	float w = [[theShapeForm findCellWithTag:2] floatValue];
	float h = [[theShapeForm findCellWithTag:3] floatValue];
	
	if([self initSpeaker] == nil)
		return nil;
		
	a = [theSpeaker API_shapeDocument:(char *)docname
		    x:x
		    y: y
		    width:w
		    height:h
		    error: &error];
	if(a != 0) {
		[self reportError: "Communication problem!"];
		return nil;
	}
	
	if(error != GRAPHITY_OK) {
		[self reportError: "Graphity could not shape document!"];
		return nil;
	}
	
    	return self;
}


@end
