/*

	$VER: DigiKlok.c 3.0 (25.02.1997) Copyright 1991, 1992, 1997 by Dalibor Kezele

	First version:  Thursday, October 10, 1991
	Middle version: Friday, January 3, 1992
	Last verstion:  Tuesday, February 25, 1997

	Source code compiled with Aztec C 5.0a.
	e-mail: dkezele@mia.os.carnet.hr

*/

#include <ctype.h>
#include <stdarg.h>
#include <exec/types.h>
#include <libraries/dos.h>

#include <graphics/gfx.h>
#include <intuition/intuitionbase.h>
#include <intuition/intuition.h>

/* -------------------------------------------------------------------- */

char version[] =
"$VER: DigiKlok 3.0 (25.02.1997) Copyright 1991, 1992, 1997 by Dalibor Kezele";

struct GfxBase *GfxBase;
struct IntuitionBase	*IntuitionBase;

struct Window *window;
struct RastPort *rastport;
struct IntuiMessage *intuimessage;

struct NewWindow newwindow = {
	0, 0, 210, 65, 0, 1, CLOSEWINDOW|NEWSIZE|VANILLAKEY,
	WINDOWCLOSE|WINDOWSIZING|WINDOWDRAG|WINDOWDEPTH
	|NOCAREREFRESH|SMART_REFRESH|RMBTRAP,
	0L, 0L, (UBYTE *)"DigiKlok 3.0", 0L, 0L, 200, 60,
       1024, 1024, CUSTOMSCREEN };

/* -------------------------------------------------------------------- */

ULONG class;
BOOL quit = FALSE;

int horsize, versize;
int beginx, beginy;
int spacex, spacey, space;

int PosX, PosY, wsize_x=0, wsize_y=0;

struct DateStamp now;
unsigned short hrs, min;
unsigned short oldhrs_hi, oldhrs_lo, oldmin_hi, oldmin_lo;

/* -------------------------------------------------------------------- */

int main(void)
{

if(!(IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 0L)))
	return 0;

if(!(GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 0L))) {
	CloseLibrary(IntuitionBase);
	return 0;
	}

newwindow.Screen = IntuitionBase -> ActiveScreen;
newwindow.LeftEdge = ((newwindow.Screen -> Width) - (newwindow.Width)) / 2;

if(!(window = (struct Window *) OpenWindow(&newwindow))) {
	CloseLibrary(GfxBase);
	CloseLibrary(IntuitionBase);
	return 0;
	}

rastport=window->RPort;

ChangeSize();

while (!quit) {
	
	Delay(10L);
	DateStamp(&now);

	hrs = now.ds_Minute / 60;
	min = now.ds_Minute % 60;

	if (hrs / 10 != oldhrs_hi) {
		DrawNumber(beginx + 4, beginy +  4, hrs / 10);
		DrawPoints((window -> Width - 12) / 2,
			window -> Height / 2 - versize / 2 + 4);
		}

	if (hrs % 10 != oldhrs_lo)
		DrawNumber(beginx + horsize * 2 + 4, beginy + 4, hrs % 10);
	if (min / 10 != oldmin_hi)
		DrawNumber(beginx + horsize * 5 + 4, beginy + 4, min / 10);
	if (min % 10 != oldmin_lo)
		DrawNumber(beginx + horsize * 7 + 4, beginy + 4, min % 10);

	oldhrs_hi = hrs / 10;
	oldhrs_lo = hrs % 10;
	oldmin_hi = min / 10;
	oldmin_lo = min % 10;

	intuimessage = (struct IntuiMessage *) GetMsg(window->UserPort);

	if (intuimessage == 0L) continue;

	switch (intuimessage -> Class) {
		case NEWSIZE:
			ChangeSize();
			break;

		case CLOSEWINDOW:
			quit = TRUE;
			break;

		case VANILLAKEY:
			if (intuimessage -> Code == 27)
				quit=TRUE;
		}

	ReplyMsg(intuimessage);
	}

CloseWindow(window);
CloseLibrary(GfxBase);
CloseLibrary(IntuitionBase);

return 0;
}

/* -------------------------------------------------------------------- */

int ChangeSize(void)
{
	if(window->Width != wsize_x || window->Height != wsize_y) {

		DeleteAll();
	
		oldhrs_hi = oldhrs_lo = oldmin_hi = oldmin_lo = 99;

		spacex = (window -> Width) / 50;
		spacey = (window -> Height) / 30;

		beginx = spacex + 4;
		beginy = spacey + 12;

		horsize = (window -> Width - (spacex * 2 + beginx * 2)) / 8 - 2;
		versize = (window -> Height - (spacey * 2 + beginy * 2)) / 2 - 1;

		wsize_x = window -> Width;
		wsize_y = window -> Height;
	}
}

int DrawNumber(int x_pos, int y_pos, unsigned short number)
{

	PosX = x_pos;
	PosY = y_pos;

	DeleteNumber(PosX, PosY);
	SetAPen(rastport, 2);
	switch(number) {
		case 0:	DrawPart(6, 1, 2, 3, 5, 6, 7);
			break;
		case 1: DrawPart(2, 3, 6);
			break;
		case 2:	DrawPart(5, 1, 3, 4, 5, 7);
			break;
		case 3:	DrawPart(5, 1, 3, 4, 6, 7);
			break;
		case 4:	DrawPart(4, 2, 3, 4, 6);
			break;
		case 5:	DrawPart(5, 1, 2, 4, 6, 7);
			break;
		case 6:	DrawPart(6, 1, 2, 4, 5, 6, 7);
			break;
		case 7:	DrawPart(3, 1, 3, 6);
			break;
		case 8:	DrawPart(7, 1, 2, 3, 4, 5, 6, 7);
			break;
		case 9:	DrawPart(6, 1, 2, 3, 4, 6, 7);
		}
}

int DrawPart(int max, ...)
{
	va_list arg;
	int number, count;

	va_start(arg, max);
	for(count = 0; count < max; ++count) {
		number = va_arg(arg, int);
		switch(number) {
			case 1: DrawHorz(PosX, PosY);
				break;
			case 2: DrawVert(PosX, PosY);
				break;
			case 3: DrawVert(PosX + spacex + horsize, PosY);
				break;
			case 4: DrawHorz(PosX, PosY + spacey + versize);
				break;
			case 5: DrawVert(PosX, PosY + spacey + versize);
				break;
			case 6: DrawVert(PosX + spacex + horsize, PosY + spacey + versize);
				break;
			case 7: DrawHorz(PosX, PosY + spacey * 2 + versize * 2);
			}
		}
	va_end(arg);
}

int DrawHorz(int BeginX, int BeginY)
{
	Move(rastport, BeginX, BeginY);
	Draw(rastport, BeginX + spacex, BeginY - spacey);
	Draw(rastport, BeginX + horsize, BeginY - spacey);
	Draw(rastport, BeginX + horsize + spacex, BeginY);
	Draw(rastport, BeginX + horsize, BeginY + spacey);
	Draw(rastport, BeginX + spacex, BeginY + spacey);
	Draw(rastport, BeginX, BeginY);
}

int DrawVert(int BeginX, int BeginY)
{
	Move(rastport, BeginX, BeginY);
	Draw(rastport, BeginX + spacex, BeginY + spacey);
	Draw(rastport, BeginX + spacex, BeginY + versize);
	Draw(rastport, BeginX, BeginY + spacey + versize);
	Draw(rastport, BeginX - spacex, BeginY + versize);
	Draw(rastport, BeginX - spacex, BeginY + spacey);
	Draw(rastport, BeginX, BeginY);

}

int DeleteNumber(int x_pos, int y_pos)
{
	SetAPen(rastport, 0);
	RectFill(rastport, x_pos - spacex, y_pos - spacey,
		x_pos + horsize + spacex * 2, y_pos + versize * 2 + spacey * 3);
}

int DeleteAll(void)
{
	SetAPen(rastport, 0);
	RectFill(rastport, 4, 12, window -> Width - 19, window -> Height - 3);
}

int DrawPoints(int x, int y)
{
	SetAPen(rastport, 2);
	DrawOnePoint(x, y);
	DrawOnePoint(x, y + versize);
}

int DrawOnePoint(int px, int py)
{
	Move(rastport, px + spacex, py - spacey);
	Draw(rastport, px + spacex, py + spacey);
	Draw(rastport, px - spacex, py + spacey);
	Draw(rastport, px - spacex, py - spacey);
	Draw(rastport, px + spacex, py - spacey);
}

