
/* Generated by Interface Builder */

#import "Neko.h"
#import <libc.h>
#import "NekoView.h"
#import <appkit/defaults.h>
#import <appkit/Application.h>
#import <appkit/ButtonCell.h>
#import <appkit/Matrix.h>
#import <appkit/MenuCell.h>
#import <appkit/NXCursor.h>
#import <appkit/NXImage.h>
#import <appkit/Panel.h>
#import <appkit/Window.h>
#import <dpsclient/wraps.h>

static char sccsid[]="@(#)Neko.m	1.3 (SFSU) 1/3/92";

// Everything here is my code.  The ex-X code is in the NekoView class.  --EPS

@interface Window(Neko)
- counterpart;
@end

@implementation Neko

+ initialize
{
    static NXDefaultsVector NekoDefaults = {
	{ "Interval", "125000" },
	{ "Speed", "16" },
	{ "Title", "Neko" },
	{ "Tora", "NO" },
	{ "ViewH", "256" },
	{ "ViewW", "320" },
	{ "ViewX", "400" },
	{ "ViewY", "288" },
	{ (char *)NULL, (char *)NULL }
    };
    NXRegisterDefaults("Neko", NekoDefaults);
    return self;
}

- appDidInit:sender
{
    const NXPoint hot={ 8.0, 8.0 };
    const char *appName;
    const char *title;
    const char *g;
    NXRect cr, fr;
    NXCoord m;
    id w;
    int tora;

    w=[NXApp appIcon];
    if ([w style]==NX_MINIWORLDSTYLE) {
	const NXPoint ipos={ 6.0, 0.0 };
	w = [[w disableDisplay] contentView];
	[w lockFocus];
	[[NXImage findImageNamed:"app"] composite:NX_SOVER toPoint:&ipos];
	PSflushgraphics();
	[w unlockFocus];

    }
    w = [NXApp windowsMenu];
    [windowsMiniaturize setUpdateAction:@selector(updateWindowsMiniaturize:)
	forMenu:w];
    [windowsClose setUpdateAction:@selector(updateWindowsClose:) forMenu:w];

    NXSetRect(&cr, (NXCoord)2, (NXCoord)12,
	(NXCoord)BITMAP_WIDTH, (NXCoord)BITMAP_HEIGHT);
    [Window getFrameRect:&minFrame forContentRect:&cr style:NX_RESIZEBARSTYLE];
    appName=[NXApp appName];
    title=NXGetDefaultValue(appName, "Title");
    m=[Window minFrameWidth:title forStyle:NX_RESIZEBARSTYLE
	buttonMask:(NX_RESIZEBUTTONMASK|NX_MINIATURIZEBUTTONMASK)];
    if (NX_WIDTH(&minFrame)<m) minFrame.size.width=m;
    NXSetRect(&cr,
	(NXCoord)atof(NXGetDefaultValue(appName, "ViewX")),
	(NXCoord)atof(NXGetDefaultValue(appName, "ViewY")),
	(NXCoord)atof(NXGetDefaultValue(appName, "ViewW")),
	(NXCoord)atof(NXGetDefaultValue(appName, "ViewH")));
    [Window getFrameRect:&fr forContentRect:&cr style:NX_RESIZEBARSTYLE];
    if (NX_WIDTH(&fr)<NX_WIDTH(&minFrame)) fr.size.width=NX_WIDTH(&minFrame);
    if (NX_HEIGHT(&fr)<NX_HEIGHT(&minFrame)) 
	fr.size.width=NX_HEIGHT(&minFrame);
    [Window getContentRect:&cr forFrameRect:&fr style:NX_RESIZEBARSTYLE];
    w=[[Window alloc] initContent:&cr style:NX_RESIZEBARSTYLE
	backing:NX_BUFFERED
	buttonMask:(NX_RESIZEBUTTONMASK|NX_MINIATURIZEBUTTONMASK)
	defer:YES
	screen:(const NXScreen *)NULL];
    [w useOptimizedDrawing:YES];
    [w setDepthLimit:NX_TwoBitGrayDepth];
    [w setOneShot:YES];
    [w setBackgroundGray:NX_WHITE];
    [w setExcludedFromWindowsMenu:YES];
    [w setTitle:title];
    [w setDelegate:self];
    g=NXGetDefaultValue(appName, "Tora");
    tora=g&&(*g=='\0'||*g=='y'||*g=='Y'||*g=='T'||*g=='t');
    [moyou selectCellWithTag:tora];
    [[w setContentView:[[NekoView alloc] initFrame:&cr tora:tora]] free];
    [w makeKeyAndOrderFront:self];
    [[w contentView] start];

    // this is a really sick hack...
    cursor=NXArrow;
    NXArrow=[[[NXCursor alloc]
	initFromImage:[NXImage findImageNamed:"cursor.tiff"]] setHotSpot:&hot];
    [NXArrow push];
    // What would be really cool would be to have cursor rectangles covering
    // the menu windows and preferences window that would switch back to
    // the original NXArrow!
    return self;
}

- appWillTerminate:sender
{
    static NXDefaultsVector NewDefaults={
    	{ "ViewH", (char *)NULL },
	{ "ViewW", (char *)NULL },
	{ "ViewX", (char *)NULL },
	{ "ViewY", (char *)NULL },
	{ (char *)NULL, (char *)NULL }
    };
    char H[12], W[12], X[12], Y[12];

    NXRect fr, cr;

    [NXArrow pop];
    NXArrow=cursor;
    [NekoView shouldRun:NO];
    bzero((char *)&fr, sizeof fr);
    [[NXApp mainWindow] getFrame:&fr];
    if (NX_HEIGHT(&fr)) {
	[Window getContentRect:&cr forFrameRect:&fr style:NX_RESIZEBARSTYLE];
	(void)sprintf(H, "%d", (int)NX_HEIGHT(&cr));
	NewDefaults[0].value=H;
	(void)sprintf(W, "%d", (int)NX_WIDTH(&cr));
	NewDefaults[1].value=W;
	(void)sprintf(X, "%d", (int)NX_X(&cr));
	NewDefaults[2].value=X;
	(void)sprintf(Y, "%d", (int)NX_Y(&cr));
	NewDefaults[3].value=Y;
	(void)NXWriteDefaults([NXApp appName], NewDefaults);
    }
    return self;
}

- appDidHide:sender
{
    [NekoView shouldRun:NO];
    return self;
}

- appDidUnhide:sender
{
    [NekoView shouldRun:YES];
    return self;
}

- info:sender
{
    if (!infoPanel) {
	register char *p, *q;

	[NXApp loadNibSection:"Info.nib" owner:self withNames:NO];
	if ((p=index(sccsid, '\t'))&&(q=index(p+1, ' '))) {
	    *q='\0';
	    [versionField setStringValue:p];
	    *q=' ';
	}
    }
    return [infoPanel makeKeyAndOrderFront:sender];
}

- changeTora:sender
{
    id w;
    int flag=[[sender selectedCell] tag];
    (void)NXWriteDefault([NXApp appName], "Tora", flag ? "YES" : "NO");
    if (w=[NXApp mainWindow])
	[[[w contentView] setBitmaps:[NXImage findImageNamed:flag ?
	"tora.tiff" : "bitmaps.tiff"]] display];
    return self;
}

- (BOOL)updateWindowsMiniaturize:aMenuCell
{
    id t;

    t = [NXApp calcTargetForAction:[aMenuCell action]];
    if (!t || ([t buttonMask]&NX_MINIATURIZEBUTTONMASK)==0) {
	if ([aMenuCell isEnabled]) {
	    [aMenuCell setEnabled:NO];
	    return YES;
	}
    }
    else {
	if (![aMenuCell isEnabled]) {
	    [aMenuCell setEnabled:YES];
	    return YES;
	}
    }
    return NO;
}

- (BOOL)updateWindowsClose:aMenuCell
{
    id t;

    t = [NXApp calcTargetForAction:[aMenuCell action]];
    if (!t || ([t buttonMask]&NX_CLOSEBUTTONMASK)==0) {
	if ([aMenuCell isEnabled]) {
	    [aMenuCell setEnabled:NO];
	    return YES;
	}
    }
    else {
	if (![aMenuCell isEnabled]) {
	    [aMenuCell setEnabled:YES];
	    return YES;
	}
    }
    return NO;
}

- windowDidBecomeKey:sender
{
    if (![NXApp keyWindow]) [NXApp perform:@selector(updateWindows) with:nil
	afterDelay:1 cancelPrevious:YES];	// stupid AppKit bug
    else [NXApp updateWindows];
    return self;
}

- windowWillClose:sender
{
    [sender orderOut:self];
    if (![NXApp keyWindow]) [NXApp updateWindows];
    return self;
}

- windowWillMiniaturize:sender toMiniwindow:miniwindow
{
    if (![[miniwindow contentView] isMemberOf:[NekoView class]]) {
	NXRect cr;

	[miniwindow getFrame:&cr];
	[[miniwindow setContentView:[[NekoView alloc] initFrame:&cr tora:NO]]
	    free];
    }
    [[miniwindow contentView] setBitmaps:[[sender contentView] bitmaps]];
    return self;
}

- windowDidMiniaturize:sender
{
    [[sender contentView] stop];
    [NXApp updateWindows];
    [[[sender counterpart] contentView] start];
    return self;
}

- windowDidDeminiaturize:sender	// sender is the miniwindow, contrary to docs
{
// this won't be called if deminiaturized by arrangeInFront: (Application)
    [[sender contentView] stop];
    [[[sender counterpart] contentView] start];
    return self;
}

- windowWillResize:sender toSize:(NXSize *)frameSize;
{
    if (frameSize->width<NX_WIDTH(&minFrame))
	frameSize->width=NX_WIDTH(&minFrame);
    if (frameSize->height<NX_HEIGHT(&minFrame))
	frameSize->height=NX_HEIGHT(&minFrame);
    return self;
}


@end

@implementation Window(Neko)

- counterpart
{
    return counterpart;
}

@end
