
/* Generated by Interface Builder */

#import "ClockView.h"
#import <dpsclient/psops.h>

@implementation ClockView

- initFrame:(const NXRect *)frameRect
{
    long now;

    [super initFrame:frameRect];
    [self setOpaque:YES];
    [self setClipping:NO];
NX_ASSERT(sizeof (int)==sizeof (long),
	"NeXT never should have assumed ints would always be 32 bits");
    (void)time(&now);
    contents = *localtime((time_t *)&now);
    return self;
}

- setLongValue:(long)aLong
{
    contents = *localtime((time_t *)&aLong);
    if([window isDisplayEnabled]) [self display];
    return self;
}

- setTmValue:(struct tm *)tmp
{
    contents = *tmp;
    if([window isDisplayEnabled]) [self display];
    return self;
}

- drawSelf:(const NXRect *)rects :(int)rectCount
{
    const NXRect bkgd={ { 191.0, 9.0 }, { 64.0, 71.0 } },
	am={ { 179.0, 0.0 }, { 12.0, 6.0 } },
	pm={ { 191.0, 0.0 }, { 12.0, 6.0 } },
	colon={ { 175.0, 2.0 }, { 3.0, 6.0 } },
	ydig[10]={ { { 12.0, 8.0 }, { 5.0, 5.0 } },
	{ { 0.0, 20.0 }, { 1.0, 5.0 } },
	{ { 2.0, 20.0 }, { 4.0, 5.0 } },
	{ { 7.0, 20.0 }, { 4.0, 5.0 } },
	{ { 12.0, 20.0 }, { 5.0, 5.0 } },
	{ { 0.0, 14.0 }, { 4.0, 5.0 } },
	{ { 5.0, 14.0 }, { 5.0, 5.0 } },
	{ { 11.0, 14.0 }, { 4.0, 5.0 } },
	{ { 0.0, 8.0 }, { 5.0, 5.0 } },
	{ { 6.0, 8.0 }, { 5.0, 5.0 } } };
    const NXPoint ampm={ 43.0, 55.0 }, colpos={ 20.0, 55.0},
	wdapos={ 21.0, 41.0 }, monpos={ 19.0, 16.0 };
    const float dpos[]={ 166.0, 89.0, 93.0, 104.0, 112.0, 121.0, 130.0,
	140.0, 148.0, 157.0 },
	dwid[]={ 9.0, 4.0, 9.0, 8.0, 9.0, 9.0, 8.0, 8.0, 9.0, 9.0 },
	mpos[]={ 172.0, 65.0, 76.0, 88.0, 100.0, 112.0, 124.0,
	136.0, 148.0, 160.0 },
	mwid[]={ 10.0, 8.0, 10.0, 9.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0 };
    static NXRect r;
    static NXPoint p;
    register struct tm *tm;
    register int u, t;

// you'd think the following could be done in initFrame:, but
// that doesn't work in loadable palettes, grr...
    if (!clockbits) clockbits = [NXImage findImageNamed:"clockbits"];
    [clockbits composite:NX_SOVER fromRect:&bkgd toPoint:&bounds.origin];

    tm= &contents;
    if (tm->tm_hour>=0&&tm->tm_min>=0) {
	[clockbits composite:NX_SOVER fromRect:&colon toPoint:&colpos];
    
	// hours
	if (tm->tm_hour>=12) {
	    if (tm->tm_hour>12) {
		t=(tm->tm_hour-12)/10; u=(tm->tm_hour-12)%10;
	    }
	    else {
		t=tm->tm_hour/10; u=tm->tm_hour%10;
	    }
	    [clockbits composite:NX_SOVER fromRect:&pm toPoint:&ampm];
	}
	else {
	    if (tm->tm_hour==0) {
		t=1; u=2;
	    }
	    else {
		t=tm->tm_hour/10; u=tm->tm_hour%10;
	    }
	    [clockbits composite:NX_SOVER fromRect:&am toPoint:&ampm];
	}
	r.origin.x = dpos[u];
	r.origin.y = 0.0;
	r.size.width = dwid[u];
	r.size.height = 11.0;
	p.x = colpos.x - r.size.width;
	p.y = colpos.y - 2.0;
	[clockbits composite:NX_SOVER fromRect:&r toPoint:&p];
	if (t>0) {
	    r.origin.x = dpos[t];
	    r.size.width = dwid[t];
	    p.x -= r.size.width;
	    [clockbits composite:NX_SOVER fromRect:&r toPoint:&p];
	}

	// minutes
	t=tm->tm_min/10; u=tm->tm_min%10;
	r.origin.x = dpos[t];
	r.size.width = dwid[t];
	p.x = colpos.x + colon.size.width;
	[clockbits composite:NX_SOVER fromRect:&r toPoint:&p];
	p.x += r.size.width;
	r.origin.x = dpos[u];
	r.size.width = dwid[u];
	[clockbits composite:NX_SOVER fromRect:&r toPoint:&p];
    }
    else {
	const NXRect timerect={ 4.0, 49.0, 55.0, 18.0 };

	PSsetgray(NX_LTGRAY);
	NXRectFill(&timerect);
    }

    // day of week
    if (tm->tm_wday>=0) {
	r.origin.x = 0.0;
	r.origin.y = (float)(65-((tm->tm_wday+6)%7)*6);	// barf
	r.size.width = 19.0;
	r.size.height = 5.0;
	[clockbits composite:NX_SOVER fromRect:&r toPoint:&wdapos];
    }

    // day of month
    r.origin.y = 14.0;
    r.size.height = 17.0;
    p.y = 22.0;
    t = tm->tm_mday/10; u = tm->tm_mday%10;
    if (t > 0) {
	r.origin.x = mpos[t];
	r.size.width = mwid[t];
	p.x = 30.0-(mwid[t]+mwid[u]+1.0)/2.0;
	[clockbits composite:NX_SOVER fromRect:&r toPoint:&p];
	p.x += r.size.width + 1.0;
    }
    else p.x = 30.0-mwid[u]/2.0;
    r.origin.x = mpos[u];
    r.size.width = mwid[u];
    [clockbits composite:NX_SOVER fromRect:&r toPoint:&p];

    // month
    r.origin.x = 40.0;
    r.origin.y = (float)((11-tm->tm_mon)*6);
    r.size.width = 22.0;
    r.size.height = 5.0;
    [clockbits composite:NX_SOVER fromRect:&r toPoint:&monpos];

    // year
    if (tm->tm_year>=0) {
	p.y = 2.0;
	t = tm->tm_year/10; u = tm->tm_year%10;
	if (t>9) {
	    t-=10;	// time runs out for UNIX in 2038
	    p.x = 21.0;
	    r = ydig[2];
	    [clockbits composite:NX_SOVER fromRect:&r toPoint:&p];
	    p.x += r.size.width + 1.0;
	    r = ydig[0];
	}
	else {
	    p.x = 23.0;
	    r = ydig[1];
	    [clockbits composite:NX_SOVER fromRect:&r toPoint:&p];
	    p.x += r.size.width + 1.0;
	    r = ydig[9];
	}
	[clockbits composite:NX_SOVER fromRect:&r toPoint:&p];
	p.x += r.size.width + 1.0;
	r = ydig[t];
	[clockbits composite:NX_SOVER fromRect:&r toPoint:&p];
	p.x += r.size.width + 1.0;
	r = ydig[u];
	[clockbits composite:NX_SOVER fromRect:&r toPoint:&p];
    }
    return self;
}


@end
