/*
 *  Source generated with GadToolsBox V1.4
 *  which is (c) Copyright 1991,92 Jaba Development
 */

#include <exec/types.h>
#include <intuition/intuition.h>
#include <intuition/classes.h>
#include <intuition/classusr.h>
#include <intuition/imageclass.h>
#include <intuition/gadgetclass.h>
#include <libraries/gadtools.h>
#include <graphics/displayinfo.h>
#include <graphics/gfxbase.h>
#include <reqtools/reqtools.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/graphics_protos.h>
#include <clib/utility_protos.h>
#include <string.h>
#include <clib/diskfont_protos.h>

#include "SetAlarm.h"


int SetHours, SetMinutes, SetSeconds;

struct Window         *SetAlarmWnd = NULL;
struct Gadget         *SetAlarmGList = NULL;
struct Gadget         *SetAlarmGadgets[5];
UWORD                  SetAlarmLeft = 101;
UWORD                  SetAlarmTop = 60;
UWORD                  SetAlarmWidth = 214;
UWORD                  SetAlarmHeight = 92;
UBYTE                 *SetAlarmWdt = NULL;

UWORD SetAlarmGTypes[] = {
	 SLIDER_KIND,
	 SLIDER_KIND,
	 SLIDER_KIND,
	 BUTTON_KIND,
	 BUTTON_KIND
};

struct NewGadget SetAlarmNGad[] = {
	 77, 13, 101, 12, (UBYTE *)"Hours", NULL, GD_SetHours, PLACETEXT_LEFT ,NULL, NULL,
	 77, 29, 101, 12, (UBYTE *)"Minutes", NULL, GD_SetMinutes, PLACETEXT_LEFT ,NULL, NULL,
	 77, 45, 101, 12, (UBYTE *)"Seconds", NULL, GD_SetSeconds, PLACETEXT_LEFT ,NULL, NULL,
	 3, 71, 103, 17, (UBYTE *)"OK", NULL, GD_SetOK, PLACETEXT_IN ,NULL, NULL,
	 107, 71, 103, 17, (UBYTE *)"Cancel", NULL, GD_SetCancel, PLACETEXT_IN ,NULL, NULL
};

ULONG SetAlarmGTags[] = {
	 (GTSL_Level), 0, (GTSL_Max), 23, (GA_RelVerify), TRUE, (GTSL_LevelFormat), (ULONG)"%2ld", 
		 (GTSL_MaxLevelLen), 2, (GTSL_LevelPlace), (PLACETEXT_RIGHT), (PGA_Freedom), LORIENT_HORIZ,
		 (GA_RelVerify), TRUE, (TAG_DONE),
	 (GTSL_Level), 0, (GTSL_Max), 59, (GA_RelVerify), TRUE, (GTSL_LevelFormat), (ULONG)"%2ld",
		 (GTSL_MaxLevelLen), 2, (GTSL_LevelPlace), (PLACETEXT_RIGHT), (PGA_Freedom), LORIENT_HORIZ,
		 (GA_RelVerify), TRUE, (TAG_DONE),
	 (GTSL_Level), 0, (GTSL_Max), 59, (GA_RelVerify), TRUE, (GTSL_LevelFormat), (ULONG)"%2ld", 
		 (GTSL_MaxLevelLen), 2, (GTSL_LevelPlace), (PLACETEXT_RIGHT), (PGA_Freedom), LORIENT_HORIZ,
		 (GA_RelVerify), TRUE, (TAG_DONE),
	 (TAG_DONE),
	 (TAG_DONE)
};

UWORD             DriPens[] = {
	 ~0 };


void SetAlarmRender( void )
{
	 UWORD        offx, offy;

	 offx = SetAlarmWnd->BorderLeft;
	 offy = SetAlarmWnd->BorderTop;


	 DrawBevelBox( SetAlarmWnd->RPort, offx + 2, offy + 70, 209, 19, GT_VisualInfo, VisualInfo, TAG_DONE );
	 DrawBevelBox( SetAlarmWnd->RPort, offx + 0, offy + 70, 213, 21, GT_VisualInfo, VisualInfo, GTBB_Recessed, TRUE, TAG_DONE );
	 DrawBevelBox( SetAlarmWnd->RPort, offx + 2, offy + 1, 209, 68, GT_VisualInfo, VisualInfo, TAG_DONE );
	 DrawBevelBox( SetAlarmWnd->RPort, offx + 0, offy + 0, 213, 70, GT_VisualInfo, VisualInfo, GTBB_Recessed, TRUE, TAG_DONE );
}

int OpenSetAlarmWindow(char *title)
{
	 struct NewGadget     ng;
	 struct Gadget       *g;
	 UWORD                lc, tc;
	 UWORD                offx = Scr->WBorLeft,offy = Scr->WBorTop + Scr->RastPort.TxHeight + 1;

	 SetAlarmGTags[1] = SetHours;
	 SetAlarmGTags[18] = SetMinutes;
	 SetAlarmGTags[35] = SetSeconds;

	 if ( ! ( g = CreateContext( &SetAlarmGList )))
		  return( 1L );

	 for( lc = 0, tc = 0; lc < SetAlarm_CNT; lc++ ) {

		  CopyMem((char * )&SetAlarmNGad[ lc ], (char * )&ng, (long)sizeof( struct NewGadget ));

		  ng.ng_VisualInfo = VisualInfo;
		  ng.ng_TextAttr   = &topaz8;
		  ng.ng_LeftEdge  += offx;
		  ng.ng_TopEdge   += offy;

		  SetAlarmGadgets[ lc ] = g = CreateGadgetA((ULONG)SetAlarmGTypes[ lc ], g, &ng, ( struct TagItem * )&SetAlarmGTags[ tc ] );

		  while( SetAlarmGTags[ tc ] ) tc += 2;
		  tc++;

		  if ( NOT g )
		  return( 2L );
	 }

	 if ( ! ( SetAlarmWnd = OpenWindowTags( NULL,
						  WA_Left,          SetAlarmLeft,
						  WA_Top,           SetAlarmTop,
						  WA_InnerWidth,    SetAlarmWidth,
						  WA_InnerHeight,   SetAlarmHeight,
						  WA_IDCMP,         IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW|IDCMP_GADGETUP|IDCMP_MOUSEMOVE|IDCMP_GADGETDOWN,
						  WA_Flags,         WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH,
						  WA_Gadgets,       SetAlarmGList,
						  WA_Title,         title,
						  WA_ScreenTitle,   "DClock V1.6 © 1995",
						  WA_CustomScreen,  Scr,
						  TAG_DONE )))
		  return( 4L );

	 GT_RefreshWindow( SetAlarmWnd, NULL );

	 SetAlarmRender();

	 return( 0L );
}

void CloseSetAlarmWindow( void )
{
	 if ( SetAlarmWnd        ) {
		  CloseWindow( SetAlarmWnd );
		  SetAlarmWnd = NULL;
	 }

	 if ( SetAlarmGList      ) {
		  FreeGadgets( SetAlarmGList );
		  SetAlarmGList = NULL;
	 }
}


int SetAlarm(int type, int preset)
{
	int timeval = SET_CANCEL;

	SetHours = preset / 10000;
	SetMinutes = (preset - SetHours * 10000) / 100;
	SetSeconds = (preset - SetHours * 10000 - SetMinutes * 100);

	switch (type)
	{
		case SET_ALARM:
			  OpenSetAlarmWindow("Set alarm");
			  break;
		case SET_TIMER:
			  OpenSetAlarmWindow("Set timer");
			  break;
		default:
			  OpenSetAlarmWindow(NULL);
			  break;
	}

	if (SetAlarmWnd)
	{
		if (SetAlarmEventLoop())
		{
			timeval = SetHours * 10000 + SetMinutes * 100 + SetSeconds;
		}
		CloseSetAlarmWindow();
		
	}
	else
		rtEZRequestA("ERROR: Can't open window\n"
						 "Close another application to free memory\n",
						 "Oops", NULL, NULL, NULL);

	return timeval;
}

BOOL SetAlarmEventLoop(void)
{
	struct IntuiMessage *msg;
	BOOL done = FALSE;
	ULONG Class;
	UWORD Code;
	ULONG windowSig = 1L << SetAlarmWnd->UserPort->mp_SigBit;
	struct Gadget *gad;
	BOOL ret = FALSE;

	while (! done)
	{
		Wait(windowSig);

		while ((! done) && (msg = GT_GetIMsg(SetAlarmWnd->UserPort)))
		{
			Class = msg->Class;
			Code = msg->Code;
			GT_ReplyIMsg(msg);
			switch (Class)
			{
				case IDCMP_CLOSEWINDOW:
					  done = TRUE;
					  break;
				case IDCMP_GADGETUP:
					  gad = (struct Gadget *)msg->IAddress;
					  switch (gad->GadgetID)
					  {
						  case GD_SetHours:
								 SetHours = Code;
								 break;
						  case GD_SetMinutes:
								 SetMinutes = Code;
								 break;
						  case GD_SetSeconds:
								 SetSeconds = Code;
								 break;
						  case GD_SetOK:
								 ret = TRUE;
						  case GD_SetCancel:
								 done = TRUE;
								 break;
					  }
					  break;
			}
		}
	}
	return ret;
}

