/************************************************************************/
/* QCal V1.0. Written by Malcolm McDonald.  29/06/90.                   */
/*                                                                      */
/*      QCal displays a calendar of the current day, month and year.    */
/*      It then allows the user to display any day/month/year by using  */
/*      The mouse, and recall or input an entry for that day.           */
/*                                                                      */
/************************************************************************/

#include <stdio.h>
#include <exec/types.h>
#include <intuition/intuition.h>

extern LONG OpenLibrary();
extern struct Window *OpenWindow();

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Window *WindowOne;
struct IntuiMessage  *message;
struct RastPort *MyWindowsRastPort;

#define INTUITION_REV 0
#define GRAPHICS_REV  0
#define STRINGSIZE    49
#define LEFTEDGE      0 
#define TOPEDGE       11
#define WIDTH         450
#define HIGHT         173 

unsigned char StringBuffer[STRINGSIZE] = "";
unsigned char UndoBuffer[STRINGSIZE];
unsigned char StringBuffer2[STRINGSIZE] = "";
unsigned char UndoBuffer2[STRINGSIZE];

struct StringInfo StringInfo2 =
{
	StringBuffer2,
	UndoBuffer2,
	0,
	STRINGSIZE,
	0,
	0,
	0,
	0,
	0,0,
	NULL,
	0,
	NULL,
};

struct Gadget StringGadget2 =
{
	NULL,
	21, 140,
	405, 10,
	GADGHCOMP,
	RELVERIFY ,
	STRGADGET,
	NULL,
	NULL,
	NULL,
	NULL,
	(APTR)&StringInfo2,
	1,
	NULL,
};

struct StringInfo StringInfo =
{
	StringBuffer,
	UndoBuffer,
	0,
	STRINGSIZE,
	0,
	0,
	0,
	0,
	0,0,
	NULL,
	0,
	NULL,
};

struct Gadget StringGadget =
{
	&StringGadget2,
	21, 130,
	405, 10,
	GADGHCOMP,
	RELVERIFY ,
	STRGADGET,
	NULL,
	NULL,
	NULL,
	NULL,
	(APTR)&StringInfo,
	1,
	NULL,
};

struct NewWindow NewWindow1 =
{
   LEFTEDGE, TOPEDGE,
   WIDTH, HIGHT, 
   0,1,     
   CLOSEWINDOW | MOUSEBUTTONS | VANILLAKEY | GADGETUP | GADGETDOWN |
   ACTIVEWINDOW  ,
   SMART_REFRESH | WINDOWDEPTH | WINDOWCLOSE | WINDOWDRAG | 
   ACTIVATE | RMBTRAP,
   &StringGadget,
   NULL,
   "QCal V1.0",
   NULL,
   NULL,
   176, 10,
   424, 178, 
   WBENCHSCREEN
};

extern int GetDateStr(char *, char *, int *, int *, int *, int *);
extern int GetDateStr2(char *, char *, int, int, int, int *);
extern void AddEntry(void);
extern void PrintEntry(void);
extern void Purge(void);
extern void DelEntry(void);
extern SetupFile(void);
extern CloseFile(void);
extern NextEntry(void);
extern PrevEntry(void);
extern FindEntry(void);
extern info(void);
extern Get_Which(char *);
extern PrintTime(struct Window *);
extern GetDate(char *);
void Time(void);
void DateStrings(void);
void Buttons(USHORT);
void GetDetails(int);
void DisplayBox(int, int, int);
void WeekBox(int, int, int);
void DateBox(int, int, int);
int WhichDateBox(int, int);
void MonthBox(int, int, int);
int WhichMonthBox(int, int);
void ClockBox(int);
void OnClock(void);
void OffClock();
void YearBox(int, int, int);
int WhichyearBox(int);
void HighlightYearBox(int, int);
void DragBox(int, int, int);
void GadgetBox(int, int, int);
int WhichGadgetBox(int);
void highlightbox(int, int);
void UpDate(void);
void PrintAll(void);
void PrintWeekDays(void);
void PrintGadgets(void);
void PrintTodaysDate(void);
void PrintUpDate(void);
void PrintYear(void);
void PrintMonth(void);
void PrintDayNums(void);
void DateBoxMove(int, int, int, int);
void PrintDayBox(int, int);
int GetMonthMax(void);
void PrintMonthBox(void);
void PrintNewDate(void);
void DrawWin(void);
void DrawABox(int, int, int, int, int);
void text(struct Window *, char *, int, int);
void QuitPrg(void);
void CloseAll(void);
void OpenAll(void);    


char *WeekDaysStr[]={"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};

char *GadgetStr[]={"Today","Next","Prev","Save","Purge","Del","Info"};
	
char *MonthStr[]={"Jan","Feb","Mar","Apr","May","Jun",
	          "Jul","Aug","Sep","Oct","Nov","Dec"};
	        
char *DayNumStr[]={"   "," 1 "," 2 "," 3 "," 4 "," 5 "," 6 "," 7 "," 8 "," 9 ","1 0",
	           "1 1","1 2","1 3","1 4","1 5","1 6","1 7","1 8","1 9",
		   "2 0","2 1","2 2","2 3","2 4","2 5","2 6","2 7","2 8",
		   "2 9","3 0","3 1"};

/*********************** A few globles !!******************************/

int year, month, day, leap, tyear, tmonth, tday, daynum, dayone;
int WindowSize=1,clock=0,find_entry=1,print_days=1,next_prev=0;
int LeftEdge,TopEdge;
int CurrentYearBox=0,ClockGadgetBox=0,CurrentGadgetBox=0; 

char YearStr[20], Date[30], DateStr[80];
char TodayDate[30], SwTodayDate[80];	
char DateIndex[15];
char temp[80];


void main(int argc, char *argv[])
{
   	ULONG MessageClass;
   	USHORT Code;
   	struct Messege *GetMsg();
   
   	if(argc >= 2)
   	{
		if((strcmp(argv[1],"-c")) == 0 ) OnClock();
   	}
   	OpenAll();	
   	DrawWin();
   	GetDate(TodayDate);
    
   	RefreshGadgets(&StringGadget,WindowOne,NULL);

   	DateStrings();
   	SetupFile();
   	PrintAll();
   
   	while(1)
	{
		while ((message = (struct IntuiMessage *)
		GetMsg(WindowOne->UserPort)) == NULL)
	        {
		 	Wait(1 << WindowOne->UserPort->mp_SigBit);
                }
                MessageClass = message->Class;
                Code = message->Code;
		
		ReplyMsg(message);
		switch (MessageClass)
		{
			case INTUITICKS	  : Time();
					    break;	    
					    
			case MOUSEBUTTONS : Buttons(Code);
			                    break;
					    
		        case RMBTRAP      : Buttons(Code);
			                    break;		   
					   
			case CLOSEWINDOW  : QuitPrg();
					   break;
					   
			case GADGETUP     : AddEntry(); 
	                                    PrintUpDate();
			                   break;
		
		        case ACTIVEWINDOW : PrintTodaysDate();
					   break;
		}
	}
}

void Time()
{
	int A=3,B=2;
	
	if(! WindowSize) { A=0;B=1; }
			
	SetAPen(WindowOne->RPort,A);
	SetBPen(WindowOne->RPort,B);
	PrintTime(WindowOne);
	SetAPen(WindowOne->RPort,1);
	SetBPen(WindowOne->RPort,0);
}



void DateStrings()
{   
	char DateBuff[80];
	
   	leap = GetDateStr(TodayDate, DateBuff, &tday, &tmonth, &tyear, &daynum);

   	sprintf(SwTodayDate," QCal V1.0. %s                         ",DateBuff);
	SwTodayDate[46]='\0';

   	year=tyear; month=tmonth; day=tday;
}

/************************************************************************/
/*        Mouse Buttons                                                 */
/************************************************************************/

void Buttons(USHORT Code)
{
	int b;
	
	switch(Code)
	{
		case 104         : b=1;                  /* left down  */
		                   GetDetails(b);
	                        	break;

		case 232         : b=3;                  /* left up    */
		                   GetDetails(b);
	                        	break;
			   
		case 105         : b=2; 	         /* right down */ 
		                   GetDetails(b);
			                break;
					
		case 233         : b=4; 	         /* right up */ 
		                   GetDetails(b);
			                break;
	}
	PrintTodaysDate();
}

/************************************************************************/
/*        BOX HANDLING                                                  */
/************************************************************************/

void GetDetails(int b)
{
	int x,y;
	
	x=(WindowOne->MouseX);
	y=(WindowOne->MouseY);
	
	if(x>14 && x<434 && y>116 && y<152)
	{
		DisplayBox(x,y,b);
	}
	else if(x>14 && x<434 && y>46 && y<96)
	{
		DateBox(x,y,b);
	}
	else if(x>14 && x<134 && y>96 && y<105)
	{
		DateBox(x,y,b);
	}
	else if(x>14 && x<434 && y>15 && y<29)
	{
		MonthBox(x,y,b);
	}
	else if(x>148 && x<434 && y>99 && y<113)
	{
		YearBox(x,y,b);
	}
	else if(x>14 && x<434 && y>156 && y<167)
	{
		GadgetBox(x,y,b);
	}
	else if(x>14 && x<434 && y>32 && y<45)
	{
		WeekBox(x,y,b);
	}
	else if(x>30 && x<373 && y>0 && y<10)
	{
		DragBox(x,y,b);
	}
	else if(x>437 && x< 449 && y>10 && y<15)
	{
		ClockBox(b);
	}
                                    /* Switch off gadgets if still on */
                 	
	if(b==3 && CurrentGadgetBox !=0) 	
	{
		highlightbox(CurrentGadgetBox, 2);
		CurrentGadgetBox=0;
	}
	if((b==3 || b==4) && CurrentYearBox !=0)
	{
		HighlightYearBox(CurrentYearBox, 2);
		CurrentYearBox=0;
	}
	if((b==3 || b==4) && ClockGadgetBox != 0)
	{
		SetAPen(WindowOne->RPort, 2);
		RectFill(WindowOne->RPort,438,11,445,14);
		ClockGadgetBox=0;
	}
}

/************************** Display Box ****************************/

void DisplayBox(int x, int y, int b)
{
	if(b==2) info();
}

/************************** Week Box ****************************/

void WeekBox(int x, int y, int b)
{
	int monthmax;
	
	print_days=0;
	if(b==1)
	{
		monthmax = GetMonthMax();
		day=day+7;
		if(day > monthmax)
		{
			day=day-monthmax;
			month++;
			if(month > 12)
			{
				month=1;
				year++;
			}
			print_days=1;
		}
	}
	else if(b==2)
	{
		day=day-7;
		if(day < 1)
		{
			month--;
			if(month < 1)
			{
				month=12;
				year--;
			}
			day=day+GetMonthMax();
			print_days=1;
		}
	}	 
	PrintUpDate();
}

/**************************** Date Box ******************************/

void DateBox(int x, int y, int b)
{
	int box,monthmax,newday;
	
	if(b != 3)
	{
		box=WhichDateBox(x,y);
		monthmax = GetMonthMax();
		newday=box-dayone+1;
		if(newday <= monthmax && newday > 0) day=newday;
		print_days=0;
		PrintUpDate();
	}
}

int WhichDateBox(int x, int y)
{
	int box,boxadd;
	
	if(y<56) boxadd=0; 
	else if(y>56 && y<66) boxadd=7; 
	else if(y>66 && y<76) boxadd=14; 
	else if(y>76 && y<86) boxadd=21; 
	else if(y>86 && y<96) boxadd=28; 
	else if(y>96) boxadd=35; 
	
	if(x<74) box=1+boxadd;
	else if(x>74 && x<134) box=2+boxadd;
	else if(x>134 && x<194) box=3+boxadd;
	else if(x>194 && x<254) box=4+boxadd;
	else if(x>254 && x<314) box=5+boxadd;
	else if(x>314 && x<374) box=6+boxadd;
	else if(x>374) box=7+boxadd; 
	
	return box;
}

/*************************** MONTH BOX ******************************/

void MonthBox(int x, int y, int b)
{
	int box;
	int monthmax;
	
	if(b != 3)
	{	
		box=WhichMonthBox(x,y);
		month = box;
		monthmax = GetMonthMax();
		while(day > monthmax) day--;
		PrintUpDate();
	}		
}

int WhichMonthBox(int x, int y)
{
	int box;
	
        if(x<49)    box=1;
	else if(x>49 && x<84)    box=2;
	else if(x>84 && x<119)   box=3;
	else if(x>119 && x<154)  box=4;
	else if(x>154 && x<189)  box=5;
	else if(x>189 && x<224)  box=6;
	else if(x>224 && x<259)  box=7;
	else if(x>259 && x<294)  box=8;
	else if(x>294 && x<329)  box=9;
	else if(x>329 && x<364)  box=10;
	else if(x>364 && x<399)  box=11;
	else if(x>399)  box=12;

	return box;
}
/**************************** CLOCK BOX ******************************/

void ClockBox(int b)
{
	static int hit=0;
	
	if(b==1 || b==2)
	{
		hit=1;
		SetAPen(WindowOne->RPort, 3);
		RectFill(WindowOne->RPort,438,11,445,14);
		ClockGadgetBox=1;	
	}	
	if((b==3 || b==4) && hit==1)
	{
		SetAPen(WindowOne->RPort, 2);
		RectFill(WindowOne->RPort,438,11,445,14);
		hit=0;
		ClockGadgetBox=0;
                if(clock)
		{ 
			OffClock();
		}
		else
		{
			OnClock();
		}
		NewWindow1.LeftEdge = WindowOne->LeftEdge;
		NewWindow1.TopEdge = WindowOne->TopEdge;
		CloseWindow(WindowOne);		
		WindowOne = OpenWindow(&NewWindow1);
		DrawWin();
		PrintAll();	
	}
}

void OnClock()
{
	NewWindow1.IDCMPFlags = NewWindow1.IDCMPFlags | INTUITICKS;
	clock=1;	
}

void OffClock()
{
	NewWindow1.IDCMPFlags = NewWindow1.IDCMPFlags & 0xFFBFFFFFL;
	clock=0;	
}

/**************************** YEAR BOX *******************************/

void YearBox(int x, int y, int b)
{
	int box;
	static int hit1=0,hit2=0;
	
	box=WhichyearBox(x);
	
	if(box==1)
	{
		if(b==1 || b==2)
		{
			hit1=1;
			HighlightYearBox(box,3);
			CurrentYearBox=box;
		}
		if((b==3 || b==4) && hit1==1)
		{
			HighlightYearBox(CurrentYearBox,2);
			if(b==3) year--;
			if(b==4) year=year-10;
		}
	}
	if(box==2)
	{
		if(b==1 || b==2)
		{
			hit2=1;
			HighlightYearBox(box,3);
			CurrentYearBox=box;
		}	
		if((b==3 || b==4) && hit2==1)
		{
			HighlightYearBox(CurrentYearBox,2);
			if(b==3) year++;
		 	if(b==4) year=year+10;
		}
		
	}

	if((b==3 || b==4) && CurrentYearBox !=0)
	{
		HighlightYearBox(CurrentYearBox, 2);
		hit1=hit2=0;
		CurrentYearBox=0;
		if(leap && month == 2 && day == 29) {while(day > GetMonthMax()) day--; UpDate();}
		if(year < 1000) year=1000;
		if(year > 9999) year=9999;
		UpDate();
	}
}

int WhichyearBox(int x)
{
	int box=0;
	
	if(x<207) box=1;
	else if(x>375) box=2;
	
	return box;
}

void HighlightYearBox(int box, int col)
{
	int xpos;
	
	if(box==1) xpos=148;
	else if(box==2) xpos=374;
	
	DrawABox(xpos,99,60,14,col);
	if(col==2) CurrentGadgetBox=0;
}

/******************************* DRAG BOX *****************************/

void DragBox(int x, int y, int b)
{
	static long Sle=LEFTEDGE,Ste=TOPEDGE,Lle=LEFTEDGE,Lte=TOPEDGE;
	
	if(b==2)
	{
		if(WindowSize==1)
		{     
			Lle = WindowOne->LeftEdge;
			Lte = WindowOne->TopEdge;
			NewWindow1.LeftEdge = Sle;
			NewWindow1.TopEdge = Ste;
			
			if(clock && Sle > 155 ) 
			{
				NewWindow1.LeftEdge = 155;
			}
			NewWindow1.Height = 10;
			if(clock) NewWindow1.Width = 485;
			WindowSize=0;
			CloseWindow(WindowOne);
			WindowOne = OpenWindow(&NewWindow1);
			PrintTodaysDate();
		}
		else
		{
			Sle = WindowOne->LeftEdge;
			Ste = WindowOne->TopEdge;
			NewWindow1.LeftEdge = Lle;
			NewWindow1.TopEdge = Lte;
						
			NewWindow1.Height = HIGHT;
			NewWindow1.Width = WIDTH;
			WindowSize=1;
			CloseWindow(WindowOne);
			WindowOne = OpenWindow(&NewWindow1);
			DrawWin();
			PrintAll();	
		}
	}
}

/*************************** GADGET BOX ********************************/

void GadgetBox(int x, int y, int b)
{
	int box;
	static int hit1=0,hit2=0,hit3=0,hit4=0,hit5=0,hit6=0,hit7=0;
	
	box=WhichGadgetBox(x);
	
	if(box==1)
	{
		if(b==1)
		{
			hit1=1;
			highlightbox(box,3);
			CurrentGadgetBox=box;
		}
		if(b==3 && hit1==1)
		{
			highlightbox(CurrentGadgetBox, 2);
			day=tday;
			month=tmonth;
			year=tyear;
			PrintUpDate();
		}
	}	

	else if(box==2)
	{
		if(b==1)
		{
			hit2=1;
			highlightbox(box,3);
			CurrentGadgetBox=box;
		}
		if(b==3 && hit2==1)
		{
			highlightbox(CurrentGadgetBox, 2);
			NextEntry();
			PrintUpDate();
		}
	}
	else if(box==3)
	{
		if(b==1)
		{
			hit3=1;
			highlightbox(box,3);
			CurrentGadgetBox=box;
		}
		if(b==3 && hit3==1)
		{
			highlightbox(CurrentGadgetBox, 2);
			PrevEntry();
			PrintUpDate();
		}
	}
	else if(box==4)
	{
		if(b==1)
		{
			hit4=1;
			highlightbox(box,3);
			CurrentGadgetBox=box;
		}
		if(b==3 && hit4==1)
		{
			highlightbox(CurrentGadgetBox, 2);
			CloseFile();
		}
	}
	else if(box==5)
	{
		if(b==1)
		{
			hit5=1;
			highlightbox(box,3);
			CurrentGadgetBox=box;
		}
		if(b==3 && hit5==1)
		{
			highlightbox(CurrentGadgetBox, 2);
			Purge();
			PrintUpDate(); 
		}
	}
	
	else if(box==6)
	{
		if(b==1)
		{
			hit6=1;
			highlightbox(box,3);
			CurrentGadgetBox=box;
		}
		if(b==3 && hit6==1)
		{
			highlightbox(CurrentGadgetBox, 2);
			DelEntry(); 
			PrintUpDate();
		}
	}
	else if(box==7)
	{
		if(b==1)
		{
			hit7=1;
			highlightbox(box,3);
			CurrentGadgetBox=box;
		}
		if(b==3 && hit7==1)
		{
			
			info();
		}
	}
	
	if(b==3 && CurrentGadgetBox !=0)
	{
		highlightbox(CurrentGadgetBox, 2);
		hit1=hit2=hit3=hit4=hit5=hit6=hit7=0;
		CurrentGadgetBox=0;
	}
}

int WhichGadgetBox(int x)
{
	int box=0;
	
	if(x<62) box=1;
	else if(x>76 && x<124) box=2;
	else if(x>138 && x<186) box=3;
	else if(x>200 && x<248) box=4;
	else if(x>262 && x<310) box=5;
	else if(x>324 && x<372) box=6;
	else if(x>386 && x<434) box=7;
	
	return box;
}

void highlightbox(int box, int col)
{
	int xpos;
	
	if(box==1) xpos=14;
	else if(box==2) xpos=76;
	else if(box==3) xpos=138;
	else if(box==4) xpos=200;
	else if(box==5) xpos=262;
	else if(box==6) xpos=324;
	else if(box==7) xpos=386;
	
	DrawABox(xpos,156,48,11,col);
	if(col==2) CurrentGadgetBox=0;
}

/*************************** PRINTING *********************************/

void UpDate()
{
	/* up date all */
	int monthmax;
	
	monthmax=GetMonthMax();
	while(day > monthmax) day--;
	PrintUpDate();	
}

void PrintAll()
{
	SetAPen(WindowOne->RPort,1);
	PrintUpDate();
	PrintWeekDays();
	PrintGadgets();
	PrintTodaysDate();
	PrintMonth();
	FindEntry();
	RefreshGadgets(&StringGadget,WindowOne,NULL);
}

void PrintWeekDays()
{
	int x,j=0;
	
	for(x=30;x <= 405; x=x+60)            /* Week box */
	{
		text(WindowOne,WeekDaysStr[j++],x,42);
	}
}

void PrintGadgets()
{
        int x,j=0;
	
	for(x=20;x <= 393; x=x+62)               /* gadget   */
	{
		text(WindowOne,GadgetStr[j++],x,164);
	}
	
	text(WindowOne,"<",168,109);
	text(WindowOne,">",414,109);	
}

void PrintTodaysDate()
{
	SetBPen(WindowOne->RPort, 1);
	SetAPen(WindowOne->RPort,0);
	text(WindowOne,SwTodayDate,28,7);
	SetAPen(WindowOne->RPort,1);
	SetBPen(WindowOne->RPort, 0);

}

void PrintUpDate()
{
	leap=GetDateStr2(Date, DateStr, day, month, year, &daynum);
	sprintf(DateIndex,"%d",(year*10000)+(month*100)+day);
	PrintYear();
	PrintDayNums();
	PrintNewDate();
	PrintMonthBox();
	if(find_entry==1)
	{ 
		FindEntry();
	}
	else
	{
		PrintEntry();
	}
	RefreshGadgets(&StringGadget,WindowOne,NULL);
	find_entry=1;
}

void PrintYear()
{
	char y[10];
	
	sprintf(YearStr,"%d",year);
        sprintf(y,"%c %c %c %c",YearStr[0],YearStr[1],YearStr[2],YearStr[3]);
	text(WindowOne,y,264,109);
}

void PrintMonth()
{
	int x,j=0;
	
	for(x=21; x<= 406; x=x+35)
	{
		text(WindowOne,MonthStr[j++],x,25);
	}

}

/************************ Print Days ************************************/

void PrintDayNums()
{
	register int j=0,jj,i=0,x,monthmax;
	static int dayoneold=0,dayold=0;
	char Dummy[80];
	char W_Day[32];
	
	GetDateStr2(Dummy, Dummy, 1, month, year, &daynum);
	if(daynum == 0) daynum=7;
	dayoneold=dayone;
	dayone=daynum;
 	if(print_days)
    	{ 	
		
		monthmax = GetMonthMax();
		
		Get_Which(W_Day);	 
		for(x=32; x<=392; x=x+60)  
		{
			j++;
			i++;
			if(i < daynum) j--;
			if(W_Day[j]=='1') SetAPen(WindowOne->RPort, 3);   
			text(WindowOne,DayNumStr[j],x,54);
			if(W_Day[j]=='1') SetAPen(WindowOne->RPort, 1);
		}
		for(x=32; x<=392; x=x+60)
		{
			j++;
			i++;
			if(W_Day[j]=='1') SetAPen(WindowOne->RPort, 3);
			text(WindowOne,DayNumStr[j],x,64);
			if(W_Day[j]=='1') SetAPen(WindowOne->RPort, 1);
		}
		for(x=32; x<=392; x=x+60)
		{
			j++;
			i++;
			if(W_Day[j]=='1') SetAPen(WindowOne->RPort, 3);
			text(WindowOne,DayNumStr[j],x,74);
			if(W_Day[j]=='1') SetAPen(WindowOne->RPort, 1);
		}
		for(x=32; x<=392; x=x+60)
		{
			j++;
			i++;
			if(W_Day[j]=='1') SetAPen(WindowOne->RPort, 3);
			text(WindowOne,DayNumStr[j],x,84);
			if(W_Day[j]=='1') SetAPen(WindowOne->RPort, 1);
		}
		jj=j;
		for(x=32; x<=392; x=x+60)
		{
			j++;jj++;
			i++;
			if(jj > monthmax) j=0; 
			if(W_Day[j]=='1') SetAPen(WindowOne->RPort, 3);
			text(WindowOne,DayNumStr[j],x,94);
			if(W_Day[j]=='1') SetAPen(WindowOne->RPort, 1);
		}
		j++;jj++;i++;
		if(jj > monthmax) j=0;
		if(W_Day[j]=='1') SetAPen(WindowOne->RPort, 3);
		text(WindowOne,DayNumStr[j],32,104);
		if(W_Day[j]=='1') SetAPen(WindowOne->RPort, 1);
	
		j++;jj++;i++;
		if(jj > monthmax) j=0;
		if(W_Day[j]=='1') SetAPen(WindowOne->RPort, 3);
		text(WindowOne,DayNumStr[j],92,104);
		if(W_Day[j]=='1') SetAPen(WindowOne->RPort, 1);
    	}
    	print_days=1;
    	DateBoxMove(day,dayone,dayold,dayoneold);
    	dayold=day;
}

void DateBoxMove(int day, int dayone, int dayold,int dayoneold)
{
	register int newbox,oldbox;
	
	newbox=day+dayone;
	oldbox=dayold+dayoneold;
	if(dayold != 0)
	{ 
		PrintDayBox(oldbox-1,2);
	}
	PrintDayBox(newbox-1,3);
	SetAPen(WindowOne->RPort, 1);
}

void PrintDayBox(int wbox, int col)
{
	register int xpos,ypos;
	
	if(wbox >= 1  && wbox <= 7 ) { xpos=14+((wbox-1)*60);  ypos=46; }
   else if(wbox >= 8  && wbox <= 14) { xpos=14+((wbox-8)*60);  ypos=56; }
   else if(wbox >= 15 && wbox <= 21) { xpos=14+((wbox-15)*60); ypos=66; }
   else if(wbox >= 22 && wbox <= 28) { xpos=14+((wbox-22)*60); ypos=76; }
   else if(wbox >= 29 && wbox <= 35) { xpos=14+((wbox-29)*60); ypos=86; }
   else if(wbox >= 36 && wbox <= 37) { xpos=14+((wbox-36)*60); ypos=96; }
	DrawABox(xpos,ypos,60,10,col);
}

int GetMonthMax()
{
	int max;
	
	if(month==1 || month==3 || month==5 ||month==7 || 
	   month==8 || month==10 || month==12) max = 31;
	if(month==4 || month==6 || month==9 || month==11) max = 30;
	if(month==2)
	{
		if(leap)
		{ 
			max = 29;
		}
		else
		{
			max = 28;
		} 
	}
	return max;
}

/*******************************************************************/

void PrintMonthBox()
{
	static int xposo=0;
	int xpos;
	
	if(month==1)  xpos = 14; 
	else if(month==2)  xpos = 49;
	else if(month==3)  xpos = 84;
	else if(month==4)  xpos = 119;
	else if(month==5)  xpos = 154;
	else if(month==6)  xpos = 189;
	else if(month==7)  xpos = 224;
	else if(month==8)  xpos = 259;
	else if(month==9)  xpos = 294;
	else if(month==10)  xpos = 329;
	else if(month==11)  xpos = 364;
	else if(month==12)  xpos = 399;

        if(xposo !=0)
	{
		DrawABox(xposo,15,35,14,2);
	}
	DrawABox(xpos,15,35,14,3);
	xposo=xpos;
}

void PrintNewDate()
{
	
	SetAPen(WindowOne->RPort,3);
	text(WindowOne,DateStr,21,126);
	SetAPen(WindowOne->RPort,1);

}

void DrawWin()
{
	int y,x;
	
	SetAPen(WindowOne->RPort, 2);
	RectFill(WindowOne->RPort,2,10,447,171);
	
	SetAPen(WindowOne->RPort, 0);
	RectFill(WindowOne->RPort,15,117,433,151);        /* main box */
	
	for(x=15;x <= 375; x=x+60)            /* Week box */
	{
		RectFill(WindowOne->RPort,x,33,x+58,44);
	}
	
	for(x=15;x <= 375; x=x+60)            /*  Day box */  
	{
		for(y=47;y <= 87; y=y+10)
		{
			RectFill(WindowOne->RPort,x,y,x+58,y+8);
		}
	}
	RectFill(WindowOne->RPort,15,97,73,105);
	RectFill(WindowOne->RPort,75,97,134,105);
	
	for(x=15;x <= 400; x=x+35)          /*   Month box */
	{
		RectFill(WindowOne->RPort,x,16,x+33,28);
	}

	RectFill(WindowOne->RPort,149,100,207,112);   /* year */  
	RectFill(WindowOne->RPort,209,100,373,112);
	RectFill(WindowOne->RPort,375,100,433,112);
	
	
	for(x=15;x <= 388; x=x+62)               /* gadget   */
	{
		RectFill(WindowOne->RPort,x,157,x+46,166);
	}         
}

void DrawABox(int xpos, int ypos, int width, int hight, int col)
{
	SetAPen(WindowOne->RPort, col);
	Move(WindowOne->RPort,xpos,ypos);  
        Draw(WindowOne->RPort,xpos+width,ypos);
	Draw(WindowOne->RPort,xpos+width,ypos+hight);
	Draw(WindowOne->RPort,xpos,ypos+hight);
	Draw(WindowOne->RPort,xpos,ypos);
	SetAPen(WindowOne->RPort, 1);	 
}

void text(struct Window *w_ptr, char *s, int x, int y)
{
   Move(w_ptr->RPort, x, y);
   Text(w_ptr->RPort, s, strlen(s));
}

/************************** Other  ********************************/

void QuitPrg()
{
	CloseFile();
	CloseAll();
	exit(TRUE);
}

void CloseAll()
{
   CloseWindow(WindowOne);
   CloseLibrary(GfxBase);
   CloseLibrary(IntuitionBase);
}

void OpenAll()    
{
   if((IntuitionBase = (struct IntuitionBase *)
     OpenLibrary("intuition.library", INTUITION_REV))
     == NULL)
     exit(FALSE);

   if ((GfxBase = (struct GfxBase *)
      OpenLibrary("graphics.library",GRAPHICS_REV))
      == NULL)
      exit(FALSE);

   if( (WindowOne = OpenWindow(&NewWindow1) ) == NULL)
     exit(FALSE);
}

