
/* Generated by Interface Builder */

#import "Boss.h"
#import <appkit/Text.h>
#import <dpsclient/event.h>
#import <appkit/Button.h>


@implementation Boss:Object

- init
{
//	printf("\niniting\n");
//	printf("appDirectory is %s\n",[NXApp appDirectory]);
	sprintf(groupDir,"%s/%s",[NXApp appDirectory],"group");
    sprintf(noGroupFile,"%s/%s",groupDir,"No Group.rtf");
	sprintf(dataFile,"%s/byAtomNumber",[NXApp appDirectory]);
	dataRead = 0;
	return self;
}

- keyEvent:(NXEvent *)event;
{
int theCell = [[tableMatrix selectedCell] tag];
int nextCell;
id theNextCell;
	switch (event->data.key.keyCode){
		case 22:	// printf("UP\n");
		  if (theCell == 26) nextCell = 76;
		  else if ((theCell <= 71) && (theCell >= 58)) nextCell = theCell + 32;
		  else if (theCell > 109) nextCell = 1;
		  else if (theCell >= 72) nextCell = theCell - 32;
		  else if (theCell >= 31) nextCell = theCell - 18;
		  else if (theCell >= 28) nextCell = theCell + 50;
		  else if (theCell >= 22) nextCell = theCell + 82;
		  else if (theCell == 21) nextCell = 89;
		  else if (theCell >= 10) nextCell = theCell - 8;
		  else if (theCell >= 5) nextCell = theCell + 76;
		  else if (theCell == 4) nextCell = 88;
		  else if (theCell == 3) nextCell = 1;
		  else if (theCell == 2) nextCell = 86;
		  else if (theCell == 1) nextCell = 87;
		  else nextCell = 1;
		  break;
		case 15:	// printf("DOWN\n");
		  if (theCell == 76) nextCell = 26;
		  else if (theCell == 1) nextCell = 3;
		  else if (theCell <= 12) nextCell = theCell + 8;
		  else if (theCell <= 39) nextCell = theCell + 18;
		  else if (theCell <= 77) nextCell = theCell + 32;
		  else if (theCell <= 80) nextCell = theCell - 50;
		  else if (theCell <= 85) nextCell = theCell - 76;
		  else if (theCell == 86) nextCell = 2;
		  else if (theCell == 87) nextCell = 1;
		  else if (theCell == 88) nextCell = 4;
		  else if (theCell == 89) nextCell = 21;
		  else if (theCell <= 103) nextCell = theCell - 32;
		  else if (theCell <= 109) nextCell = theCell - 82;
		  else nextCell = 1;
		  break;
		case 9: 	  // printf("LEFT\n");
		  switch(theCell){	
		    case 1:
				nextCell = 109;		break;
			case 109:
				nextCell = 107;		break;
			default:
		       nextCell = theCell - 1;}
		  break;
		case 16: 	  // printf("RIGHT\n");
		  switch(theCell){
		    case 107:
				nextCell = 109;		break;
			case 109:
			    nextCell = 1;		break;
			default:
		       nextCell = theCell + 1;}
		  break;
		default:	//  not an arrow key
		  NXBeep();
		  return self;}
	[tableMatrix selectCellWithTag:nextCell];
	theNextCell = [tableMatrix selectedCell];
	[theNextCell performClick:self];
	//  [theNextCell highlight:TRUE];
	//  [[tableMatrix findCellWithTag:nextCell] performClick:self];
	return self;
}

- readData
{
int j=-1;
int i=0;
char tokensep[] = "\t";
char buf[500], *token;
	if (( infile = fopen(dataFile,"r")) == NULL)
	{
		  printf("%s didn't open\n",dataFile);	
	}
	else
	{
//		  printf("%s successfully opened\n",dataFile);

		  while (fgets(buf,500,infile)!=NULL)
		  {	// for every line in the file 
			 j++;
			 i = 0;
			 token = strtok(buf,tokensep);
			 while (token != NULL)
			 {	// for every item in the line
				i++;
				strncpy(theElements[j][i],token,NUMCHAR);
			 token = strtok(NULL, tokensep);
			 }
		  }
	}
	detCell[1]=name;
	detCell[2]=symbol;
	detCell[3]=atomicNumber;
	for (i=4;i<=20;i++)
		detCell[i]=[details findCellWithTag:i];
	dataRead = 1;
	return self;
}


- showElement:sender
{
int i;
int AN = [[sender selectedCell] tag];
	if (!dataRead) [self readData];
	[elementWin disableFlushWindow];
	for (i=1;i<=20;i++){
		[detCell[i] setStringValue:theElements[AN][i]];
		}
	[[elementWin reenableFlushWindow] flushWindow];
	[elementWin orderFront:self];
    return self;
}


- (char *)getGroupName:(int)theTag
{
//	printf("%i is the tag\n",theTag);
	switch(theTag){
	  	case 10: return("IA");
	  		break;
		case 20: return("IIA");
			break;
		case 30: return("IIIA");
			break;
		case 40: return("IVA");
			break;
		case 50: return("VA");
			break;
		case 60: return("VIA");
			break;
		case 70: return("VIIA");
			break;
		case 80: return("VIIIA");
			break;
		case 11: return("IB");
	  		break;
		case 21: return("IIB");
			break;
		case 31: return("IIIB");
			break;
		case 41: return("IVB");
			break;
		case 51: return("VB");
			break;
		case 61: return("VIB");
			break;
		case 71: return("VIIB");
			break;
		case 81: return("VIIIB");
			break;}
	return("No Group.rtf");
} 


- showGroup:sender
{
	char groupName[MAXPATHLEN];
	NXStream *stream;
	static NXPoint origin = {0.0,0.0};
 
//	printf("\nGroupDir = %s\n",groupDir); 
//	printf("GroupName = %s\n",groupName); 

	sprintf(groupName,"%s/%s",groupDir,[self getGroupName:[[sender selectedCell] tag]]);
//	printf("Showing group file %s\n",groupName);
    if ((stream = NXMapFile(groupName,NX_READONLY)) == NULL)
        stream = NXMapFile(noGroupFile,NX_READONLY);
    if (stream != NULL) {
    	[groupPanel disableFlushWindow];
        [[groupText docView] readRichText:stream]; 
	[[groupText docView] scrollPoint:&origin];
	[[groupPanel reenableFlushWindow] flushWindow];
    	NXCloseMemory(stream,NX_FREEBUFFER);
    }
    [groupPanel orderFront:self];
    return self;
}

- (int)appPowerOffIn:(int)ms andSave:(int)aFlag
{
	[filezowner terminate];
}

@end
