#import "DView.h"
#import "Localization.h"
#import <appkit/NXImage.h>
#import <appkit/TextField.h>
#import <libc.h>

@implementation DView

static NXPoint			origin		= {0.0, 0.0};
static BOOL				flag		= YES;

#define STEPS		120
#define IMAGE_FILE	LocalString("BigCase")

- initFrame:(const NXRect *)frameRect;
{
	const NXSize 	aSize		= {64.0,64.0};
	
	[super initFrame:frameRect];
	theImage = [NXImage findImageNamed:IMAGE_FILE];
	buffer = [[NXImage allocFromZone:[self zone]] initSize:&aSize];
	return self;
}

- free
{
	if ([theImage isKindOfClassNamed:"NXImage"])	[theImage free];
	if ([buffer isKindOfClassNamed:"NXImage"])		[buffer free];
	return [super free];
}

- _doDissolve:i1 :i2
{
	float delta = 0.0;
	int i = 0;
	for (i = 0; i <= STEPS; i++) {
	    delta = (float)i/(float)STEPS;
	    [i1 composite:NX_SOVER toPoint:&origin];
	    [i2 dissolve:delta toPoint:&origin];
	    [self display];
	}
	[i2 dissolve:1.0 toPoint:&origin];
    return self;
}

- drawSelf:(NXRect *)rects :(int)rectCount;
{
	if (flag) [theImage composite:NX_SOVER toPoint:&origin];
	else [buffer composite:NX_SOVER toPoint:&origin];
	return self;
}
	
- mouseDown:(NXEvent *)event
{
	NXImage *oldImage = NULL, *newImage = NULL;
	
    if ((event->type == NX_LMOUSEDOWN) &&
		(event->flags & NX_COMMANDMASK) &&
		(event->flags & NX_ALTERNATEMASK)) {
		flag = NO;
		[buffer lockFocus];
		oldImage = [NXImage findImageNamed:IMAGE_FILE];
		newImage = [NXImage findImageNamed:LocalString("me")];
		[nameField setStringValueNoCopy:LocalString("Subrata Sircar")];
		[self _doDissolve:oldImage :newImage];
		NXPing();
		sleep(3);
		[oldImage free];
		oldImage = newImage;
		newImage = [NXImage findImageNamed:IMAGE_FILE];
		[nameField setStringValueNoCopy:""];
		[self _doDissolve:oldImage :newImage];
    	[buffer unlockFocus];
		flag = YES;
		[oldImage free];
		[newImage free];
    }
	return self;
}

@end
