/*
 * This subclass only adds a few responsibilities to the Application class.
 * It loads the info and preferences panels on demand, keeps track of the 
 * path to the app wrapper, and overrides sendEvent: to watch for 
 * control-mouseDown events which it vectors off to the help object.
 *
 * Author: Julie Zelenski, NeXT Developer Support
 * You may freely copy, distribute and reuse the code in this example.  
 * NeXT disclaims any warranty of any kind, expressed or implied, as to 
 * its fitness for any particular use.
 */


#import "SterOptikonApp.h"
#import <appkit/View.h>
#import <strings.h>
#import <defaults/defaults.h>  	// for NXArgv
#import <libc.h>		// for chdir, getwd


@implementation SterOptikonApp:Application

static char *openFile(const char *directory, const char *name, BOOL display)
/*
 * Opens a file with the given name in the specified directory.
 * If we already have that file open, it is ordered front.
 * Returns the document if successful, nil otherwise.
 */
{
    char buffer[MAXPATHLEN+1], path[MAXPATHLEN+1];

    if (name && *name) {
	if (!directory) {
	    directory = ".";
	} else if (*directory != '/') {
	    strcpy(buffer, "./");
	    strcat(buffer, directory);
	    directory = buffer;
	}
		return buffer;
    }

    return '\0';
}

static char *openDocument(const char *document, BOOL display)
{
    char *directory, *name, *ext;
    char buffer[MAXPATHLEN+1];

    strcpy(buffer, document);
    ext = strrchr(buffer, '.');
    if (!ext || strcmp(ext, ".S3d")) strcat(buffer, ".S3d");
    name = strrchr(buffer, '/');
    if (name) {
	*name++ = '\0';
	directory = buffer;
    } else {
	name = buffer;
	directory = NULL;
    }

    return openFile(directory, name, display);
}

+ new;
/* New is overridden so the SterOptikonApp gets and saves the path to executable.
 * The help object will ask the app for this, so it can find the help
 * files which are in a directory in the app wrapper. 
 */
{
    char *suffix;
    
    self = [super new];
    strcpy(appDirectory, NXArgv[0]);
    if (suffix = rindex(appDirectory,'/')) 
        *suffix  = '\0'; 	/* remove executable name */
    if (appDirectory) chdir(appDirectory); 
    getwd(appDirectory);
	strcpy(openFileName,"");
	return self;
}

- (const char *)appDirectory;
/* Returns the path to the app wrapper (for Help object).
 */
{
    return appDirectory;
}

- (int)app:sender openFile:(const char *)path type:(const char *)type
/*
 * This method is performed whenever a user double-clicks on an icon
 * in the Workspace Manager representing a StereoView program document.
 */
{
    if (type && !strcmp(type, "S3d")) {
	    strcpy(openFileName,openDocument(path, YES));
	if (openFileName) {
	    haveOpenedDocument = YES;
	    return YES;
	}
    	return NO;
    }

    return NO;
}

- (int)openFile:(const char *)filename ok:(int *)flag
{
	    strcpy(openFileName,filename);
		*flag = YES;
	    haveOpenedDocument = YES;
	    return YES;
}

 - (char *)getInitialFile;
{
	return (char *)openFileName;
}

@end
