// -------------------------------------------------------------------------------------
//  SimpleTableView app
//  This software is without warranty of any kind.  Use at your own risk.
// -------------------------------------------------------------------------------------

#import <appkit/appkit.h>
#import <objc/objc.h>
#import <libc.h>
#import <stdlib.h>
#import <string.h>
#import <c.h>
#import <ctype.h>
#import <sys/param.h>
#import <sys/types.h>
#import <sys/time.h>
#import <sys/dir.h>
#import "SimpleTableView.h"
#import "DataEditor.h"

// -------------------------------------------------------------------------------------
@implementation SimpleTableView

// -------------------------------------------------------------------------------------
// menu actions

/* open table file */
- openTable:sender
{
	id			pOpen;
	char		fName[MAXPATHLEN+1], *dir, **files, *fTypes[] = { "simpleTbl", 0 };

	/* build/display open panel */
	pOpen = [OpenPanel new];
	[pOpen setTitle:"Open Simple DataTable"];
	[pOpen setPrompt:"File:"];
	[pOpen setRequiredFileType:fTypes[0]];
	[pOpen allowMultipleFiles:YES];
	[pOpen setDirectory:pathToApp];
	if (![pOpen runModalForTypes:fTypes]) return self;

	/* open the list of selected files */
	if (!(files = (char**)[pOpen filenames])) return self;
	dir = (char*)[pOpen directory];
	while (*files) {
		sprintf(fName, "%s/%s", dir, *files++);
		[[DataEditor alloc] initFromFile:fName];
	}
	
	return self;
}

/* terminate */
- terminate:sender
{
	[DataEditor terminateAllEditors];
	return [super terminate:sender];
}

// -------------------------------------------------------------------------------------
// application initialization 

/* app did initialize */
- appDidInit:sender
{
	char *p, path[MAXPATHLEN + 1];
    if (NXArgv[0][0] == '/') strcpy(path, NXArgv[0]);
	else { 
		getwd(path);
		strcat(path, "/");
		strcat(path, NXArgv[0]);
	}
	if ((p = rindex(path, '/')) && (p != path)) *p = 0;
	pathToApp = NXCopyStringBuffer(path);
	return self;
}

// -------------------------------------------------------------------------------------
@end
