
#import "app.h"

#define GRAPH_NAME	"MyGraph2"

@implementation GraphityAPIDemo2

- 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;
	}
	
	[self shapeDocument:nil];
	[theNameField setEnabled:NO];
	[theAddButton 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];
	
	[theGraphButton setEnabled:YES];
	
    	return self;
}


- shapeDocument:sender
{
	int a, error;
	const char *docname = [theNameField stringValue];
	float x = 0;
	float y = 0;
	float w = 550;
	float h = 700;
	
	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;
}

- saveDataToFile:(const char *)datafile;
{
	NXStream *s = NXOpenMemory(NULL, 0, NX_WRITEONLY);
	int a;
	
	[[theDataView docView] writeText:s];
	a = NXSaveToFile(s, datafile);
	
	if(a != 0) {
		[self reportError: "Could not create datafile!"];
		if(s != NULL)NXCloseMemory(s, NX_FREEBUFFER);
		return nil;
	}
	if(s != NULL)NXCloseMemory(s, NX_FREEBUFFER);
	return self;
}

- graphData:sender
{
	int x, error;
	const char *name = GRAPH_NAME;
	const char *docname = [theNameField stringValue];
	const char *tmpFile = "/tmp/GraphityDemo2.data";	// ugly
	int rowCount = -1;
	int serieCount = -1;
	
	if([self saveDataToFile: tmpFile] == nil)
		return nil;
	
	if([self initSpeaker] == nil)
		return nil;
		
	x = [theSpeaker API_readDataFrom:(char *)tmpFile
			graph:(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 parse data file!"];
		return nil;
	}
	
	[theSpeaker API_getGraphSize:(char *)name
		in:(char *)docname
		rowCount: &rowCount
		serieCount: &serieCount
		error: &error];
	[[theSizeForm findCellWithTag: 1] setIntValue: serieCount];
	[[theSizeForm findCellWithTag: 0] setIntValue: rowCount];
	
    	return self;
}

@end
