
/* Generated by Interface Builder */

#import <rpcsvc/bootparam.h>
#import <stdio.h>
#import <strings.h>
#import <defaults.h>
#import <appkit/SavePanel.h>
#import <appkit/OpenPanel.h>
#import <appkit/Application.h>
#import <appkit/Button.h>
#import <appkit/ScrollView.h>
#import <appkit/Text.h>
#import <objc/typedstream.h>
#import "Car_main.h"
#import "AppDelegate.h"
#import "Cycle.h"
#import "Road.h"
#import "Inspector.h"
#import "Car.h"

@implementation AppDelegate

- init
{
    appDelegate = self;
    openFromWorkspace = YES;
    return self;
}

- appDidInit:sender
{
char filename[MAX_PATH_LEN];
int i;

    strcpy(filename,NXArgv[0]);
    i = strlen(filename);
    while ( filename[i--] != '/' );
    i++;

    if ( cycleFilename[0] == 0 )
        {
        strcpy(&filename[i],"/default.cycle");
        [self loadCycleFile:filename];
        }
    if ( carFilename[0] == 0 )
        {
        strcpy(&filename[i],"/default.car");
        [self loadCarFile:filename];
        }
    if ( roadFilename[0] == 0 )
        {
        strcpy(&filename[i],"/default.environment");
        [self loadRoadFile:filename];
        }
    return self;
}

- (BOOL)appAcceptsAnotherFile:sender
{
    return YES;
}

- (char *)openFileType:(char *)type
{
char *fileTypes[2];
id openPanel;
static char fullpath[MAX_PATH_LEN];

    openFromWorkspace = NO;
    fileTypes[0] = type;
    fileTypes[1] = NULL;
    openPanel = [OpenPanel new];
    [openPanel allowMultipleFiles:NO];
    if ( [openPanel runModalForTypes:fileTypes] )
        {
        strcpy(fullpath,[openPanel directory]);
        strcat(fullpath,"/");
        strcat(fullpath,[openPanel filenames][0]);
        if ( [self app:NXApp openFile:fullpath type:type] )
            return fullpath;
        }
    openFromWorkspace = YES;
    return NULL;
}

- openCarFile:sender
{
    [self openFileType:"car"];
    return self;
}

- saveCarFile:sender
{
NXTypedStream *typedstream;

    if ( carFilename[0] == '\000' )
        [self saveCarFileAs:self];
    if ( (typedstream = NXOpenTypedStreamForFile(carFilename,NX_WRITEONLY)) == NULL )
        return self;

    NXWriteObject(typedstream,car);
    NXWriteObject(typedstream,controller);
    NXWriteObject(typedstream,transmission);
    NXWriteObject(typedstream,engine);
    NXWriteObject(typedstream,gasTank);
    NXWriteObject(typedstream,motor);
    NXWriteObject(typedstream,battery);
    NXWriteObject(typedstream,wheel);

    NXCloseTypedStream(typedstream);
    [inspector updateCarBox:carFilename];
    return self;
}

- saveCarFileAs:sender
{
id savePanel;

    savePanel = [SavePanel new];
    [savePanel setRequiredFileType:"car"];
    if ( ![savePanel runModal] )
        return self;
    strcpy(carFilename,[savePanel filename]);
    [self saveCarFile:self];
    return self;
}

- (int)app:sender openFile:(const char *)filename type:(const char *)aType
{
    if ( !strcmp(aType,"car") )
        return [self loadCarFile:filename];
    if ( !strcmp(aType,"environment") )
        return [self loadRoadFile:filename];
    if ( !strcmp(aType,"cycle") )
        return [self loadCycleFile:filename];
    return NO;
}

- newGraph
{
static NXCoord x = 100;
static NXCoord y = 750;

    [NXApp loadNibSection:"Graph.nib" owner:self];
    [[newGraph window] moveTopLeftTo:x :y];
    x += 50;
    y -= 50;
    if ( y < 100 )
        y = 750;
    if ( x > 900 )
        x = 100;
    return newGraph;
}

- simulationEnded
{
NXStream *stream;

    if ( reportScrollView == nil )
        {
        [NXApp loadNibSection:"Report.nib" owner:self];
        reportText = [reportScrollView docView];
        }
    
    stream = NXOpenMemory(NULL,0,NX_READWRITE);

    [car report:stream];
    [road report:stream];
    [battery report:stream];
    [gasTank report:stream];

    NXSeek(stream,0,NX_FROMSTART);
    [reportText readText:stream];
    NXCloseMemory(stream,NX_FREEBUFFER);     
    return self;
}

- (BOOL)loadCarFile:(const char *)filename
{
NXTypedStream *stream;

    if ( (stream = NXOpenTypedStreamForFile(filename,NX_READONLY)) == NULL )
        return NO;

    strcpy(carFilename,filename);
    [car free];
    car = NXReadObject(stream);
    [runButton setTarget:car];
    [controller free];
    controller = NXReadObject(stream);
    [transmission free];
    transmission = NXReadObject(stream);
    [engine free];
    engine = NXReadObject(stream);
    [gasTank free];
    gasTank = NXReadObject(stream);
    [motor free];
    motor = NXReadObject(stream);
    [battery free];
    battery = NXReadObject(stream);
    [wheel free];
    wheel = NXReadObject(stream);

    if ( openFromWorkspace )
        {
        [inspector updateCarBox:filename];
        [inspector updateBoxes];
        }
    NXCloseTypedStream(stream);
    return YES;
}

- (BOOL)loadRoadFile:(const char *)filename
{
NXTypedStream *stream;

    if ( (stream = NXMapFile(filename,NX_READONLY)) == NULL )
        return NO;
    strcpy(roadFilename,filename);
    [road loadRoadFromStream:stream];
    if ( openFromWorkspace )
        {
        [inspector updateRoadGraph:filename];
        [inspector updateBoxes];
        }
    NXClose(stream);
    return YES;
}

- (BOOL)loadCycleFile:(const char *)filename
{
NXStream *stream;

    if ( (stream = NXMapFile(filename,NX_READONLY)) == NULL )
        return NO;
    strcpy(cycleFilename,filename);
    [cycle loadCycleFromStream:stream];
    if ( openFromWorkspace )
        {
        [inspector updateCycleGraph:filename];
        [inspector updateBoxes];
        }
    NXClose(stream);
    return YES;
}

@end











