/*
 *  SCREEN/WINDOW Demo  in Manx C   Brian Jackson
 *
 *  Butchered By Allen - No custom screen now, just a window & gadgets
 *
 *   compile with:
 *        cc Loan
 *   link with:
 *        ln +Cdb +Q Loan -lm -lc
 *
 *   Exit by clicking on the window's CLOSE gadget.
 *
 **********************************************************************/

 #include <stdio.h>
 #include <ctype.h>
 #include <intuition/intuition.h>
 #include <functions.h>

 #define  RN          33L             /* the current library revision # */
 #define  RECALC        1
 #define  PRINTIT       2
 #define  YES           1
 #define  NO            2

 struct IntuitionBase *IntuitionBase = NULL ;
 struct GfxBase       *GfxBase       = NULL ;
 struct Window        *MW            = NULL ;
 struct IntuiMessage  *message ;


UBYTE UNDOBUFFER[8];

SHORT GadBorderVector1[] = {0,0,83,0,83,13,0,13,0,0};

struct Border GadBorder1 = {
	-2,-1,	/* Right & Down offset from top left */
	3,0,JAM1,	/* Front & Back pen & drawmode */
	5,	/* vector count */
	GadBorderVector1,	/* pointer to XY vectors */
	NULL	/* next border in list */
};

struct IntuiText IntuiText1 = {
	3,0,JAM2,	/* front and back text pens, drawmode and fill byte */
	17,2,	/* Right & Down offset from top left */
	NULL,	/* font pointer or NULL for default */
	(UBYTE *)"RECALC",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

struct Gadget Gadget5 = {
	NULL,	/* next gadget */
	115,64,	/* origin XY of hit box relative to window TopLeft */
	80,12,	/* hit box width and height */
	GADGHCOMP,	/* gadget flags */
	RELVERIFY,	/* activation flags */
	BOOLGADGET,	/* gadget type flags */
	(APTR)&GadBorder1,	/* gadget border or image to be rendered */
	NULL,	/* alternate imagery for selection */
	&IntuiText1,	/* first IntuiText structure */
	NULL,	/* gadget mutual-exclude long word */
	NULL,	/* SpecialInfo structure */
	RECALC,	/* user-definable data */
	NULL	/* pointer to user-definable data */
};


SHORT GadBorderVector2[] = {0,0,83,0,83,13,0,13,0,0};

struct Border GadBorder2 = {
	-2,-1,	/* Right & Down offset from top left */
	3,0,JAM1,	/* Front & Back pen & drawmode */
	5,	/* vector count */
	GadBorderVector2,	/* pointer to XY vectors */
	NULL	/* next member of linked list */
};

struct IntuiText IntuiText2 = {
	3,0,JAM2,	/* front and back text pens, drawmode and fill byte */
	20,2,	/* Right & Down offset from top left */
	NULL,	/* font pointer or NULL for default */
	(UBYTE *)"PRINT",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

struct Gadget Gadget4 = {
	&Gadget5,	/* next gadget */
	21,64,	/* origin XY of hit box relative to window TopLeft */
	80,12,	/* hit box width and height */
	GADGHCOMP,	/* gadget flags */
	RELVERIFY,	/* activation flags */
	BOOLGADGET,	/* gadget type flags */
	(APTR)&GadBorder2,	/* gadget border or image to be rendered */
	NULL,	/* alternate imagery for selection */
	&IntuiText2,	/* first IntuiText structure */
	NULL,	/* gadget mutual-exclude long word */
	NULL,	/* SpecialInfo structure */
	PRINTIT,	/* user-definable data */
	NULL	/* pointer to user-definable data */
};

UBYTE Gadget3SIBuff[3] = "4";

struct StringInfo Gadget3SInfo = {
	Gadget3SIBuff,	/* buffer where text will be edited */
	UNDOBUFFER,	/* optional undo buffer */
	0,	/* character position in buffer */
	3,	/* maximum number of characters to allow */
	0,	/* first displayed character buffer position */
	0,0,0,0,0,	/* Intuition initialized and maintained variables */
	0,	/* Rastport of gadget */
	0,	/* initial value for integer gadgets */
	NULL	/* alternate keymap (fill in if you set the flag) */
};

SHORT GadBorderVector3[] = {0,0,71,0,71,9,0,9,0,0};

struct Border GadBorder3 = {
	-2,-1,	/* Right & Down offset from top left */
	3,0,JAM1,	/* Front & Back pen & drawmode */
	5,	/* vector count */
	GadBorderVector3,	/* pointer to XY vectors */
	NULL	/* next member of linked list */
};

struct Gadget Gadget3 = {
	&Gadget4,	/* next gadget */
	127,49,	/* origin XY of hit box relative to window TopLeft */
	68,8,	/* hit box width and height */
	NULL,	/* gadget flags */
	RELVERIFY+STRINGCENTER,	/* activation flags */
	STRGADGET,	/* gadget type flags */
	(APTR)&GadBorder3,	/* gadget border or image to be rendered */
	NULL,	/* alternate imagery for selection */
	NULL,	/* first IntuiText structure */
	NULL,	/* gadget mutual-exclude long word */
	(APTR)&Gadget3SInfo,	/* SpecialInfo structure */
	NULL,	/* user-definable data */
	NULL	/* pointer to user-definable data */
};

UBYTE Gadget2SIBuff[8] = "13000";

struct StringInfo Gadget2SInfo = {
	Gadget2SIBuff,	/* buffer where text will be edited */
	UNDOBUFFER,	/* optional undo buffer */
	0,	/* character position in buffer */
	8,	/* maximum number of characters to allow */
	0,	/* first displayed character buffer position */
	0,0,0,0,0,	/* Intuition initialized and maintained variables */
	0,	/* Rastport of gadget */
	0,	/* initial value for integer gadgets */
	NULL	/* alternate keymap (fill in if you set the flag) */
};

SHORT GadBorderVector4[] = {0,0,71,0,71,9,0,9,0,0};

struct Border GadBorder4 = {
	-2,-1,	/* Right & Down offset from top left */
	3,0,JAM1,	/* Front & Back pen & drawmode */
	5,	/* vector count */
	GadBorderVector4,	/* pointer to XY vectors */
	NULL	/* next member of linked list */
};

struct Gadget Gadget2 = {
	&Gadget3,	/* next gadget */
	127,35,	/* origin XY of hit box relative to window TopLeft */
	68,8,	/* hit box width and height */
	NULL,	/* gadget flags */
	RELVERIFY+STRINGCENTER,	/* activation flags */
	STRGADGET,	/* gadget type flags */
	(APTR)&GadBorder4,	/* gadget border or image to be rendered */
	NULL,	/* alternate imagery for selection */
	NULL,	/* first IntuiText structure */
	NULL,	/* gadget mutual-exclude long word */
	(APTR)&Gadget2SInfo,	/* SpecialInfo structure */
	NULL,	/* user-definable data */
	NULL	/* pointer to user-definable data */
};

UBYTE Gadget1SIBuff[6] ="13.5";

struct StringInfo Gadget1SInfo = {
	Gadget1SIBuff,	/* buffer where text will be edited */
	UNDOBUFFER,	/* optional undo buffer */
	0,	/* character position in buffer */
	6,	/* maximum number of characters to allow */
	0,	/* first displayed character buffer position */
	0,0,0,0,0,	/* Intuition initialized and maintained variables */
	0,	/* Rastport of gadget */
	0,	/* initial value for integer gadgets */
	NULL	/* alternate keymap (fill in if you set the flag) */
};

SHORT GadBorderVector5[] = {0,0,71,0,71,9,0,9,0,0};

struct Border GadBorder5 = {
	-2,-1,	/* Right & Down offset from top left */
	3,0,JAM1,	/* Front & Back pen & drawmode */
	5,	/* vector count */
	GadBorderVector5,	/* pointer to XY vectors */
	NULL	/* next member of linked list */
};

struct Gadget Gadget1 = {
	&Gadget2,	/* next gadget */
	127,21,	/* origin XY of hit box relative to window TopLeft */
	68,8,	/* hit box width and height */
	NULL,	/* gadget flags */
	RELVERIFY+STRINGCENTER,	/* activation flags */
	STRGADGET,	/* flags */
	(APTR)&GadBorder5,	/* border */
	NULL,	/* alternate imagery for selection */
	NULL,	/* first IntuiText structure */
	NULL,	/* gadget mutual-exclude long word */
	(APTR)&Gadget1SInfo,	/* SpecialInfo structure */
	NULL,	/* user-definable data */
	NULL	/* pointer to user-definable data */
};

UBYTE UpdateBuff[30];

struct IntuiText IntuiText8 = {
	3,0,JAM2,	/* front and back text pens, drawmode and fill byte */
	25,92,	/* Right & Down offset from top left */
	NULL,	/* font pointer or NULL for default */
	UpdateBuff,	/* pointer to text */
	NULL	/* next IntuiText structure */
};

struct IntuiText IntuiText7 = {
	3,0,JAM2,	/* front and back text pens, drawmode and fill byte */
	25,82,	/* Right & Down offset from top left */
	NULL,	/* font pointer or NULL for default */
	(UBYTE *)"Your Payments Will Be",	/* pointer to text */
	&IntuiText8	/* next IntuiText structure */
};

struct IntuiText IntuiText6 = {
	3,0,JAM2,	/* front and back text pens, drawmode and fill byte */
	75,49,	/* Right & Down offset from top left */
	NULL,	/* font pointer or NULL for default */
	(UBYTE *)"Years",	/* pointer to text */
	&IntuiText7	/* next IntuiText structure */
};

struct IntuiText IntuiText5 = {
	3,0,
JAM2,	/* front and back text pens, drawmode and fill byte */
	35,35,	/* Right & Down offset from top left */
	NULL,	/* font pointer or NULL for default */
	(UBYTE *)"Loan Value",	/* pointer to text */
	&IntuiText6	/* next IntuiText structure */
};

struct IntuiText IntuiText4 = {
	3,0,JAM2,	/* front and back text pens, drawmode and fill byte */
	186,21,	/* Right & Down offset from top left */
	NULL,	/* font pointer or NULL for default */
	(UBYTE *)"%",	/* pointer to text */
	&IntuiText5	/* next IntuiText structure */
};

struct IntuiText IntuiText3 = {
	3,0,JAM2,	/* front and back text pens, drawmode and fill byte */
	10,21,	/* Right & Down offset from top left */
	NULL,	/* font pointer or NULL for default */
	(UBYTE *)"Interest Rate",	/* pointer to text */
	&IntuiText4	/* next IntuiText structure */
};

 struct NewWindow newwindow = {
    19,19,        /* left edge, top edge */
    223,107,       /* width, height       */
    0,1,          /* detail pen, blk pen */
    CLOSEWINDOW|GADGETUP,  /* IDCMP flags         */
      WINDOWDEPTH | WINDOWDRAG	  /* flags */
      | SMART_REFRESH | ACTIVATE 
      | NOCAREREFRESH | WINDOWCLOSE,
    &Gadget1,    /* firstgadget         */
    NULL,        /* checkmark           */
    (UBYTE *)" Loan Calculator ",
    NULL,        /* Screen Pointer . See Below! */
    NULL,        /* bitmap pointer      */
    5,5,         /* minwidth, minheight */
    640,200,     /* maxwidth, maxheight */
    WBENCHSCREEN /* screentype          */
    };

  
 main()
 {

 ULONG class;
 float atof();
 int loan();
   
   if (( IntuitionBase = 
      (struct IntuitionBase *)OpenLibrary("intuition.library",RN)) == NULL)
          GetOut ("Error:  No Intuition\n");

   if ((GfxBase =
       (struct GfxBase *)OpenLibrary("graphics.library",RN)) == NULL)
          GetOut ("Error:  No Graphics.lib\n");

   if ((MW = (struct Window *)OpenWindow(&newwindow)) == NULL)
          GetOut ("Error:  No Window\n");

	   loan(atof(Gadget3SIBuff),atof(Gadget1SIBuff),
                atof(Gadget2SIBuff),UpdateBuff);

	   PrintIntuiText(MW->RPort,&IntuiText3,0L,0L);

   FOREVER  {

       Wait( 1L << MW->UserPort->mp_SigBit) ;

       while(message = (struct IntuiMessage *)GetMsg(MW->UserPort)) {
           class = message->Class;
           ReplyMsg(message);

           if (class == GADGETUP)
		{
	        switch (((struct Gadget *)(message->IAddress))->GadgetID)
			{
			case RECALC: loan(atof(Gadget3SIBuff),atof(Gadget1SIBuff),
     					  atof(Gadget2SIBuff),UpdateBuff,NO);
				      PrintIntuiText(MW->RPort,&IntuiText8,0L,0L);
				      break;

			case PRINTIT: loan(atof(Gadget3SIBuff),atof(Gadget1SIBuff),
     					   atof(Gadget2SIBuff),UpdateBuff,NO);
				      PrintIntuiText(MW->RPort,&IntuiText8,0L,0L);
				      loan(atof(Gadget3SIBuff),atof(Gadget1SIBuff),
     					   atof(Gadget2SIBuff),UpdateBuff,YES);
				      break;
			}
			break;
		}

           if (class == CLOSEWINDOW)
		{
		GetOut(UpdateBuff);
		}

           }		/* end while message     */
       }		/* end forever           */
  }			/* end main              */

 GetOut( lastword )
 TEXT *lastword ;             /* Close things in REVERSE order!! */
 {                  
     if(MW)               CloseWindow(MW);
     if(GfxBase)          CloseLibrary(GfxBase);
     if(IntuitionBase)    CloseLibrary(IntuitionBase);
     if( lastword )       puts(lastword) ;
     exit(0) ;
 }      /* end function */



/********* LOAN FUNCTION *******/

int
loan(length, rate, value, spointer, ToPrinter)
float length, rate, value;
char *spointer;
int ToPrinter;

{
	float pow();
	float bal, i, m, intr, pmt, rate2;
	float w1, w2, w3, w4;
	FILE *OutPut;		/* file pointer printer */

	/* length in years */
	if(length < .1){
		strcpy(spointer," *** ENTER YEARS ***  ");
		return(-1);
		}
	length = length * 12.0;

	/* interest rate */
	if(rate < .1){
		strcpy(spointer,"*** ENTER RATE ***    ");
		return(-1);
		}
	rate2=rate;
	rate = (rate/12.0) / 100.0;

	/* loan value */
	if(value < .1){
		strcpy(spointer,"*** ENTER VALUE ***   ");
		return(-1);
		}
	w1 = (1 + rate); w2 = -length; w3 = pow(w1,w2);
	pmt = value * rate/(1 - w3);

	sprintf(spointer,"$%4.2f Per Month.    ",pmt);


	if(ToPrinter==YES)
		{

		/* try to open the printer */
		if ((OutPut = fopen ("PRT:", "w")) == NULL)
			{
			DisplayBeep(NULL);
			return(-1);
			}

		fprintf(OutPut,"The interest rate is  %2.2f.\n", rate2);
		fprintf(OutPut,"The loan value is $%.2f.\n", value);
		fprintf(OutPut,"The loan is for %f years (%.0f months).\n\n", length/12.0, length);
		fprintf(OutPut,"The monthly payment amount is %7.2f.\n\n", pmt);
		fprintf(OutPut,"Month   Payment      Balance     Interest\n");
		fprintf(OutPut,"-----------------------------------------\n");
		for (m = 1; m < length + 1; m++)
			{
			w2 = -m; w4 = pow(w1,w2);
			bal = (value / w4) * (w4-w3)/(1-w3);
			intr = m * pmt - value + bal;
			fprintf(OutPut,"%4.0f   %8.2f    %9.2f    %9.2f\n", m, pmt, bal, intr);
			}
		fprintf(OutPut,"\n");
		}

	return(0);
}

