#include <intuition/intuition.h>
#include <graphics/gfxmacros.h>
#include <libraries/dosextens.h>
#include <exec/io.h>
#include <exec/memory.h>

long IntuitionBase, GfxBase, ConsoleDevice;

/****************************************************************
 * Application globals.
 ****************************************************************/
/*
 * This structure stores our data.
 */
struct mailData
{  char lastName[21],
	firstName[21],
	address1[31],
	address2[31],
	city[16],
	state[3],
	zipcode[10],
	phone[11];
   short age;
   char birthday[5];
   float incomeAmount;
};

/**********************************************************************/
#include "getdata.c"
/**********************************************************************/
#include "editfunctions.c"
/**********************************************************************/
struct Window *window(screen,title,left,top,width,height,pena,penb,flags)
struct Screen *screen;
UBYTE *title;
SHORT left,top,width,height;
UBYTE pena,penb;
ULONG flags;
{  static struct NewWindow new_window = { 0,0,0,0,-1,-1,NULL,NULL,NULL,NULL,
	    "Window",NULL,NULL,40,20,640,200,WBENCHSCREEN };

   new_window.LeftEdge=left;
   new_window.TopEdge=top;
   new_window.Width=width;
   new_window.Height=height;
   new_window.DetailPen=pena;
   new_window.BlockPen=penb;
   new_window.Flags=flags;
   new_window.Title=title;
   new_window.Screen=screen;
   if(screen!=NULL) new_window.Type=CUSTOMSCREEN;
   return((struct Window *)OpenWindow(&new_window));
}
/**********************************************************************/
/**********************************************************************/
/**********************************************************************/
/**********************************************************************/
main()
{
   static struct IntuiText
      lastname_IT   ={3,0,JAM1,10,20,0, "Last Name...:",0},
      firstname_IT  ={3,0,JAM1,10,30,0, "First Name..:",&lastname_IT},
      address_IT    ={3,0,JAM1,10,40,0, "Address.....:",&firstname_IT},
      city_IT	    ={3,0,JAM1,10,60,0, "City........:",&address_IT},
      state_IT	    ={3,0,JAM1,10,70,0, "State.......:",&city_IT},
      zipcode_IT    ={3,0,JAM1,10,80,0, "Zip Code....:",&state_IT},
      phone_IT	    ={3,0,JAM1,10,90,0, "Phone.......:",&zipcode_IT},
      age_IT	    ={3,0,JAM1,10,100,0,"Age.........:",&phone_IT},
      birthday_IT   ={3,0,JAM1,10,110,0,"Birth day...:",&age_IT},
      income_IT     ={3,0,JAM1,10,120,0,"Gross Income:",&birthday_IT};

   /*
    * Initialize our data structure with clear fields.
    */
   static struct mailData mailInfo={"","","","","","","","",0,"",0.0};

   /*
    * The IOField linked list.
    */
   static struct IOField
      income_IO     ={0,150,120,OUTIN|FLOAT|BORDERED,1,2,JAM2,10,
			      (APTR)&mailInfo.incomeAmount,0,0},
      birthday_IO   ={&income_IO,150,110,OUTIN|DIGITS|BORDERED,1,2,JAM2,4,
			      (APTR)mailInfo.birthday,edit_date,0},
      age_IO	    ={&birthday_IO,150,100,OUTIN|INTEGER|BORDERED,1,2,JAM2,3,
			      (APTR)&mailInfo.age,edit_age,0},
      phone_IO	    ={&age_IO,150,90,OUTIN|DIGITS|BORDERED,1,2,JAM2,10,
			      (APTR)mailInfo.phone,0,0},
      zipcode_IO    ={&phone_IO,150,80,OUTIN|DIGITS|BORDERED,1,2,JAM2,9,
			      (APTR)mailInfo.zipcode,0,0},
      state_IO	    ={&zipcode_IO,150,70,OUTIN|ANYCHAR|BORDERED,1,2,JAM2,2,
			      (APTR)&mailInfo.state,0,0},
      city_IO	    ={&state_IO,150,60,OUTIN|ANYCHAR|BORDERED,1,2,JAM2,15,
			      (APTR)mailInfo.city,0,0},
      address2_IO   ={&city_IO,150,50,OUTIN|ANYCHAR|BORDERED,1,2,JAM2,30,
			      (APTR)mailInfo.address2,0,0},
      address1_IO   ={&address2_IO,150,40,OUTIN|ANYCHAR|BORDERED,1,2,JAM2,30,
			      (APTR)mailInfo.address1,0,0},
      firstname_IO  ={&address1_IO,150,30,OUTIN|ANYCHAR|BORDERED,1,2,JAM2,20,
			      (APTR)&mailInfo.firstName,0,0},
      lastname_IO   ={&firstname_IO,150,20,OUTIN|ANYCHAR|BORDERED,1,2,JAM2,20,
			      (APTR)mailInfo.lastName,0,0};

  struct IOStdReq iosr;
  struct Window *testwin;
  struct IntuiMessage *msg;
  struct IOField *start=0;
  short NOT_EXIT=1;

   /*
    * The ConsoleDevice is needed in order to process RAWKEY events.
    */
   if(!OpenDevice("console.device",-1,&iosr,0))
      ConsoleDevice=(long)iosr.io_Device;
   else
      exit(FALSE);

   IntuitionBase=OpenLibrary("intuition.library",0);
   GfxBase=OpenLibrary("graphics.library",0);

   testwin=window(0,"Simple Mailer",30,20,400,140,0,1,ACTIVATE|WINDOWCLOSE);

   PrintIText(testwin->RPort,&income_IT,0,0);

   /*
    * Uncomment the following statement if you wish to display the fields
    * then do some other functionality before making the call to getData().
    *
    * displayIOField(testwin,&lastname_IO);
    *
    * You may place values into the mailData structure so that the user may
    * edit existing data. Do that before you call getData().
    */
   /*
    * Set the IDCMP for messages we wish to receive.
    */
    ModifyIDCMP(testwin,CLOSEWINDOW);

   /*
    * Main event loop.
    */
   while(NOT_EXIT)
   {  msg=getData(testwin,&lastname_IO,&start);
      if(msg)
      {   /*
	   * The following if() may be substituted by a case statement
	   * when handling multiple events.
	   */
	  switch(msg->Class)
	  {  case RAWKEY:
	       break;
	     case MOUSEBUTTONS:
	       break;
	     case INACTIVEWINDOW:
	       break;
	     case CLOSEWINDOW:
	       NOT_EXIT=0;
	       break;
	  }
      }
      else
      {  /*
	  * when msg=0 data entry was completed normally.
	  * This is where we process the data and do what ever we want to
	  * do with it. You may wish to make this a separte function since
	  * you may wish to process the data for other events as well.
	  */
	  printf("name: %s, %s Age=%d\n",
		 mailInfo.lastName,mailInfo.firstName,mailInfo.age);
      }
   }
   /*
    * Cleanup and exit.
    */
   CloseWindow(testwin);
   CloseLibrary(IntuitionBase);
   CloseLibrary(GfxBase);
   CloseDevice(&iosr);
}
