/*  compiler directives to fetch the necessary header files */
#include <exec/types.h>
#include <exec/exec.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <graphics/gfxbase.h>
#include <graphics/gfx.h>
#include <graphics/text.h>
#include <devices/serial.h>
#include <stdio.h>
#include <ctype.h>
#include <libraries/dos.h>

extern struct Window *mywindow;
extern char phone[10][82];
extern char pname[10][82];
extern char macro[10][82];

struct IntuiText ntext = {2,2,JAM1, 10 , -11, NULL,
  (UBYTE *) "Filename", NULL};
 
struct IntuiText bool_str = {2,2,JAM1, 20, 1, NULL,
  (UBYTE *) "OK", NULL };

struct IntuiText bool2_str = {2,2,JAM1, 9, 1, NULL,
  (UBYTE *) "Cancel", NULL };
 
#define STRINGSIZE 512
UBYTE DefString[STRINGSIZE];
UBYTE Undo [STRINGSIZE];
 
struct StringInfo TexString = {
   DefString,                      /* Buffer - Pointer to Buffer */
   Undo,                           /* UndoBuffer - Undo buf ptr  */
   0,                              /* BufferPos - Init Chr Posn */
   STRINGSIZE,                     /* MaxChars - Max number of Chars */
   0, 0,                           /* DispPos - First Disp Chr */
   0,                              /* NumChars - Number of Characters */
   0, 0, 0,                        /* Posn Vars calc by Intuition */
   NULL,                           /* No pointer to Rasport */
   0,                              /* Longint Value */
   NULL                            /* No pointer to alt Keyboard */
};
 
USHORT Pairs[] = {
-1,  -1,                            /* Information describing the */
165, -1,                           /* border around the gadget */
165, 9,
-1, 9,
-1, -1
 };
 
USHORT Pairs1[] = {
 0,  0,
66,  0,
66, 11,
0,  11,
0,  0
};
 
#define NUM_PAIRS 5                /* There are Four pairs above */
 
struct Border StrBorder = {
 -1, -1,                           /* LeftEdge, TopEdge */
 1, 0, JAM1,                       /* FrontPen,  BackPen  DrawMode  */
 NUM_PAIRS,                        /* Number of XY Pairs */
(APTR)&Pairs,                      /* XY, Pointer to XY Pairs */
 NULL                              /* No more borders */
};
 
struct Border butt_border = {
 -1, -1,
 1, 0, JAM1,
 NUM_PAIRS,
 (APTR)&Pairs1,
 NULL
};

#define TEX_GAD 0
struct Gadget tex_gad = {
   NULL, 10, 15, 160 , 11, GADGHCOMP, RELVERIFY,
   STRGADGET, (APTR)&StrBorder, NULL,
   &ntext, NULL, (APTR)&TexString, TEX_GAD, NULL
};
 
#define BOOL_GAD1 1
struct Gadget bool_gad = {
   &tex_gad, 12, 30, 65, 10, GADGHCOMP, RELVERIFY,
   BOOLGADGET, (APTR)&butt_border, NULL,
   &bool_str, NULL, NULL, BOOL_GAD1, NULL
};
 
#define BOOL_GAD2 2
struct Gadget bool2_gad = {
  &bool_gad, 102, 30, 65, 10, GADGHCOMP, RELVERIFY,
  BOOLGADGET, (APTR)&butt_border, NULL,
  &bool2_str,  NULL, NULL, BOOL_GAD2, NULL
};
 
#define PLX 190      /* Palette Window Size  */
#define PLY 50

/* Used to open a Window   */
struct NewWindow NewCmod = {
   40,30, PLX,PLY, 0, 1, NULL,    /* IDCMP set up AFTER CALL */
   ACTIVATE | SMART_REFRESH,
   &bool2_gad, NULL, NULL,    /* FirstGadget, CheckMark, Title  */
   NULL, NULL,              /* MUST SET SCREEN AFTER OPENSCREEN!!! */
   PLX, PLY, PLX, PLY, WBENCHSCREEN }; /* MinW, MinH, MaxW, MaxH */


/********************************************************/
/* declarations for the edit macro's window and gadgets */
/********************************************************/

unsigned char mnum[10][4];
struct IntuiText mtext[10];
unsigned char mDefString[10][82];
unsigned char mUndo[10][82];
 
struct StringInfo mString[10];
 
USHORT Pairs2[] = {
-1,  -1,                            /* Information describing the */
361, -1,                           /* border around the gadget */
361, 9,
-1, 9,
-1, -1
 };
 
struct Border mBorder = {
 -1, -1,                           /* LeftEdge, TopEdge */
 1, 0, JAM1,                       /* FrontPen,  BackPen  DrawMode  */
 NUM_PAIRS,                        /* Number of XY Pairs */
(APTR)&Pairs2,                      /* XY, Pointer to XY Pairs */
 NULL                              /* No more borders */
};
 
struct Gadget m_gad[10];
 
#define BOOL_GAD10 10
struct Gadget bool3_gad = {
   &m_gad[9], 60, 163, 65, 10, GADGHCOMP, RELVERIFY,
   BOOLGADGET, (APTR)&butt_border, NULL,
   &bool_str, NULL, NULL, BOOL_GAD10, NULL
};
 
#define BOOL_GAD11 11
struct Gadget bool4_gad = {
  &bool3_gad, 300, 163, 65, 10, GADGHCOMP, RELVERIFY,
  BOOLGADGET, (APTR)&butt_border, NULL,
  &bool2_str,  NULL, NULL, BOOL_GAD11, NULL
};
 
#define EX 435      /* Palette Window Size  */
#define EY 180

/* Used to open a Window   */
struct NewWindow cmac = {
   40,15, EX,EY, 0, 1, NULL,    /* IDCMP set up AFTER CALL */
   ACTIVATE | SMART_REFRESH,
   &bool4_gad, NULL, NULL,    /* FirstGadget, CheckMark, Title  */
   NULL, NULL,              /* MUST SET SCREEN AFTER OPENSCREEN!!! */
   EX, EY, EX, EY, WBENCHSCREEN }; /* MinW, MinH, MaxW, MaxH */


/********************************************************/
/* declarations for the phone book                      */
/********************************************************/
struct Gadget phone_gad[10];
struct Gadget name_gad[10];
struct Gadget dial_gad[10];

unsigned char Defphone[10][82];
unsigned char Defname[10][82];
unsigned char Undophone[10][82];
unsigned char Undoname[10][82];
 
USHORT Pairs3[] = {
-1,  -1,                            /* Information describing the */
302, -1,                           /* border around the gadget */
302, 9,
-1, 9,
-1, -1
 };
 
struct Border nameBorder = {
 -1, -1,                           /* LeftEdge, TopEdge */
 1, 0, JAM1,                       /* FrontPen,  BackPen  DrawMode  */
 5,                                /* Number of XY Pairs */
(APTR)&Pairs3,                     /* XY, Pointer to XY Pairs */
 NULL                              /* No more borders */
};
 
USHORT Pairs4[] = {
-1,  -1,                           /* Information describing the */
162, -1,                           /* border around the gadget */
162, 9,
-1, 9,
-1, -1
 };
 
struct Border phoneBorder = {
 -1, -1,                          /* LeftEdge, TopEdge */
 1, 0, JAM1,                      /* FrontPen,  BackPen  DrawMode  */
 5,                               /* Number of XY Pairs */
(APTR)&Pairs4,                    /* XY, Pointer to XY Pairs */
 NULL                             /* No more borders */
};
 
USHORT Pairs5[] = {
0,  0,                           /* Information describing the */
66, 0,                           /* border around the gadget */
66, 10,
0, 10,
0, 0
 };
 
struct Border dial_Border = {
 -1, -1,                          /* LeftEdge, TopEdge */
 1, 0, JAM1,                      /* FrontPen,  BackPen  DrawMode  */
 5,                               /* Number of XY Pairs */
(APTR)&Pairs5,                    /* XY, Pointer to XY Pairs */
 NULL                             /* No more borders */
};
 
struct IntuiText bool3_str = {2,2,JAM1, 15, 1, NULL,
  (UBYTE *) "Dial", NULL };
 
struct StringInfo phoneString[10];
struct StringInfo nameString[10];
 
struct Gadget bool5_gad = {
   &phone_gad[9], 80, 163, 65, 10, GADGHCOMP, RELVERIFY,
   BOOLGADGET, (APTR)&butt_border, NULL,
   &bool_str, NULL, NULL, 30, NULL
};
 
struct Gadget bool6_gad = {
  &bool5_gad, 420, 163, 65, 10, GADGHCOMP, RELVERIFY,
  BOOLGADGET, (APTR)&butt_border, NULL,
  &bool2_str,  NULL, NULL, 31, NULL
};
 
#define MEX 610      /* Palette Window Size  */
#define MEY 180

/* Used to open a Window   */
struct NewWindow pwin = {
   17,15, MEX,MEY, 0, 1, NULL,    /* IDCMP set up AFTER CALL */
   ACTIVATE | SMART_REFRESH,
   &bool6_gad, NULL, NULL,    /* FirstGadget, CheckMark, Title  */
   NULL, NULL,              /* MUST SET SCREEN AFTER OPENSCREEN!!! */
   MEX, MEY, MEX, MEY, WBENCHSCREEN }; /* MinW, MinH, MaxW, MaxH */

/*****************************************************************/
/* initphonegad()                                                */
/* init phone gadgets                                            */
/*****************************************************************/
initphonegad()
{
int i;

for(i=0; i<10; i++)
   {
   dial_gad[i].NextGadget = &dial_gad[i-1];
   dial_gad[i].LeftEdge = 18;
   dial_gad[i].TopEdge  = 9 + i*15;
   dial_gad[i].Width  = 64;
   dial_gad[i].Height = 9;
   dial_gad[i].Flags = GADGHCOMP;
   dial_gad[i].Activation = RELVERIFY;
   dial_gad[i].GadgetType = BOOLGADGET;
   dial_gad[i].GadgetRender = &dial_Border;
   dial_gad[i].SelectRender = 0;
   dial_gad[i].GadgetText = &bool3_str;
   dial_gad[i].MutualExclude = 0;
   dial_gad[i].SpecialInfo = 0;
   dial_gad[i].GadgetID = i;
   dial_gad[i].UserData = 0;
   }

dial_gad[0].NextGadget = NULL;

for (i=0; i<10; i++)
    {
    nameString[i].Buffer = &Defname[i];
    nameString[i].UndoBuffer = &Undoname[i];
    nameString[i].BufferPos  = 0;
    nameString[i].MaxChars   = 82;
    nameString[i].DispPos    = 0;
    nameString[i].UndoPos    = 0;
    nameString[i].NumChars   = 0;
    nameString[i].DispCount  = 0;
    nameString[i].CLeft      = 0;
    nameString[i].CTop       = 0;
    nameString[i].LayerPtr   = 0;
    nameString[i].LongInt    = 0;
    nameString[i].AltKeyMap  = 0;
    }

for(i=0; i<10; i++)
   {
   name_gad[i].NextGadget = &name_gad[i-1];
   name_gad[i].LeftEdge = 98;
   name_gad[i].TopEdge  = 10 + i*15;
   name_gad[i].Width  = 300;
   name_gad[i].Height = 11;
   name_gad[i].Flags = GADGHCOMP;
   name_gad[i].Activation = RELVERIFY;
   name_gad[i].GadgetType = STRGADGET;
   name_gad[i].GadgetRender = &nameBorder;
   name_gad[i].SelectRender = 0;
   name_gad[i].GadgetText = 0;
   name_gad[i].MutualExclude = 0;
   name_gad[i].SpecialInfo = &nameString[i];
   name_gad[i].GadgetID = 10+i;
   name_gad[i].UserData = 0;
   }

name_gad[0].NextGadget = &dial_gad[9];


for (i=0; i<10; i++)
    {
    phoneString[i].Buffer = &Defphone[i];
    phoneString[i].UndoBuffer = &Undophone[i];
    phoneString[i].BufferPos  = 0;
    phoneString[i].MaxChars   = 82;
    phoneString[i].DispPos    = 0;
    phoneString[i].UndoPos    = 0;
    phoneString[i].NumChars   = 0;
    phoneString[i].DispCount  = 0;
    phoneString[i].CLeft      = 0;
    phoneString[i].CTop       = 0;
    phoneString[i].LayerPtr   = 0;
    phoneString[i].LongInt    = 0;
    phoneString[i].AltKeyMap  = 0;
    }

for(i=0; i<10; i++)
   {
   phone_gad[i].NextGadget = &phone_gad[i-1];
   phone_gad[i].LeftEdge = 417;
   phone_gad[i].TopEdge  = 10 + i*15;
   phone_gad[i].Width  = 160;
   phone_gad[i].Height = 11;
   phone_gad[i].Flags = GADGHCOMP;
   phone_gad[i].Activation = RELVERIFY;
   phone_gad[i].GadgetType = STRGADGET;
   phone_gad[i].GadgetRender = &phoneBorder;
   phone_gad[i].SelectRender = 0;
   phone_gad[i].GadgetText = 0;
   phone_gad[i].MutualExclude = 0;
   phone_gad[i].SpecialInfo = &phoneString[i];
   phone_gad[i].GadgetID = 20+i;
   phone_gad[i].UserData = 0;
   }

phone_gad[0].NextGadget = &name_gad[9];

} /* end of gadinit */


/*************************************************
*  function to edit/dial in the phone book
*************************************************/
int dophone()
{
struct Window *cW;
struct Gadget *igad;
struct IntuiMessage *NewMessage;
ULONG class;
USHORT code;
int gadgid;
int keepon,file,i;
char ct;

for (i=0; i<10; i++)
    {
    strncpy(Defphone[i],phone[i],strlen(phone[i])-1);
    strncpy(Defname[i],pname[i],strlen(pname[i])-1);
    }

if ( ! (cW = (struct Window *)OpenWindow(&pwin)) ) {
   return(FALSE); /* Oops...  */
   }

RefreshGadgets(&dial_gad[0],cW,NULL);
cW->UserPort = mywindow->UserPort;
ModifyIDCMP(cW, GADGETUP | GADGETDOWN );
keepon = TRUE;

while (keepon)
{
Wait(1 << cW->UserPort->mp_SigBit);
while(NewMessage=(struct IntuiMessage *)GetMsg(cW->UserPort))
     {
     class = NewMessage->Class;
     code = NewMessage->Code;
     ReplyMsg(NewMessage);
     if ((class == GADGETDOWN) || (class == GADGETUP))
        {
        igad = (struct Gadget *)NewMessage->IAddress;
        gadgid = igad->GadgetID;
        switch (gadgid)
               {
               case 30:
                  for (i=0; i<10; i++)
                      {
                      strcpy(phone[i],Defphone[i]);
                      strcat(phone[i],"\n");
                      strcpy(pname[i],Defname[i]);
                      strcat(pname[i],"\n");
                      }
                  keepon = FALSE;
                  file = TRUE;
               break;
               case 31:
                  keepon =  FALSE;
                  file = FALSE;
               break;
               default:
                  if (gadgid >= 0 && gadgid <= 9)
                     {
                     file = FALSE;
                     keepon = FALSE;
                     for (i=0; i<10; i++)
                         {
                         strcpy(phone[i],Defphone[i]);
                         strcat(phone[i],"\n");
                         strcpy(pname[i],Defname[i]);
                         strcat(pname[i],"\n");
                         }
                     for (i=0; phone[gadgid][i] != '\n'; i++)
                         {
                         sendchar(phone[gadgid][i]);
                         }
                     sendchar(13);
                     }
               break;
               }   /* end switch */
        }  /* end if */
     }   /* end while */
}  /* end keepon */
cW->UserPort = NULL;
CloseWindow(cW);
return(file);
} /* end of function */

/*************************************************
*  function to edit macro keys
*************************************************/
editmacro()
{
struct Window *cW;
struct Gadget *igad;
struct IntuiMessage *NewMessage;
ULONG class;
USHORT code;
int gadgid;
int keepon,file,i;

for (i=0; i<10; i++)
    strncpy(mDefString[i],macro[i],strlen(macro[i])-1);
    

if ( ! (cW = (struct Window *)OpenWindow(&cmac)) ) {
   return(FALSE); /* Oops...  */
   }

RefreshGadgets(&m_gad[0],cW,NULL);
cW->UserPort = mywindow->UserPort;
ModifyIDCMP(cW, GADGETUP | GADGETDOWN );
keepon = TRUE;

while (keepon)
{
Wait(1 << cW->UserPort->mp_SigBit);
while(NewMessage=(struct IntuiMessage *)GetMsg(cW->UserPort))
     {
     class = NewMessage->Class;
     code = NewMessage->Code;
     ReplyMsg(NewMessage);
     if ((class == GADGETDOWN) || (class == GADGETUP))
        {
        igad = (struct Gadget *)NewMessage->IAddress;
        gadgid = igad->GadgetID;
        switch (gadgid)
               {
               case BOOL_GAD10:
                  for (i=0; i<10; i++)
                      {
                      strcpy(macro[i],mDefString[i]);
                      strcat(macro[i],"\n");
                      }
                  keepon = FALSE;
                  file = TRUE;
               break;
               case BOOL_GAD11:
                  keepon =  FALSE;
                  file = FALSE;
               break;
               default:
               break;
               }   /* end switch */
        }  /* end if */
     }   /* end while */
}  /* end keepon */
cW->UserPort = NULL;
CloseWindow(cW);
return(file);
} /* end of function */

/*****************************************************************/
/* initmgad()                                                    */
/* init macro gadgets                                            */
/*****************************************************************/
initmgad()
{
int i;

strcpy(mnum[0]," 1:");
strcpy(mnum[1]," 2:");
strcpy(mnum[2]," 3:");
strcpy(mnum[3]," 4:");
strcpy(mnum[4]," 5:");
strcpy(mnum[5]," 6:");
strcpy(mnum[6]," 7:");
strcpy(mnum[7]," 8:");
strcpy(mnum[8]," 9:");
strcpy(mnum[9],"10:");

for (i = 0; i < 10; i++)
    {
    mtext[i].FrontPen = 2;
    mtext[i].BackPen  = 2;
    mtext[i].DrawMode = JAM1;
    mtext[i].LeftEdge = -27;
    mtext[i].TopEdge  = 0;
    mtext[i].ITextFont = 0;
    mtext[i].IText = &mnum[i];
    mtext[i].NextText = 0;
    }

for (i=0; i<10; i++)
    {
    mString[i].Buffer = mDefString[i];
    mString[i].UndoBuffer = mUndo[i];
    mString[i].BufferPos  = 0;
    mString[i].MaxChars   = 82;
    mString[i].DispPos    = 0;
    mString[i].UndoPos    = 0;
    mString[i].NumChars   = 0;
    mString[i].DispCount  = 0;
    mString[i].CLeft      = 0;
    mString[i].CTop       = 0;
    mString[i].LayerPtr   = 0;
    mString[i].LongInt    = 0;
    mString[i].AltKeyMap  = 0;
    }

for(i=0; i<10; i++)
   {
   m_gad[i].NextGadget = &m_gad[i-1];
   m_gad[i].LeftEdge = 40;
   m_gad[i].TopEdge  = 10 + i*15;
   m_gad[i].Width  = 359;
   m_gad[i].Height = 11;
   m_gad[i].Flags = GADGHCOMP;
   m_gad[i].Activation = RELVERIFY;
   m_gad[i].GadgetType = STRGADGET;
   m_gad[i].GadgetRender = &mBorder;
   m_gad[i].SelectRender = 0;
   m_gad[i].GadgetText = &mtext[i];
   m_gad[i].MutualExclude = 0;
   m_gad[i].SpecialInfo = &mString[i];
   m_gad[i].GadgetID = i;
   m_gad[i].UserData = 0;
   }

m_gad[0].NextGadget = NULL;

} /* end of gadinit */


/*************************************************
*  function to get file name
*************************************************/
filename(name)
char name[];
{
struct Window *cW;
struct Gadget *igad;
struct IntuiMessage *NewMessage;

ULONG class;
USHORT code;
int gadgid;
int keepon,file;

if ( ! (cW = (struct Window *)OpenWindow(&NewCmod)) ) {
   return(FALSE); /* Oops...  */
   }

RefreshGadgets(&tex_gad,cW,NULL);
cW->UserPort = mywindow->UserPort;
ModifyIDCMP(cW, GADGETUP | GADGETDOWN );
keepon = TRUE;

while (keepon)
{
Wait(1 << cW->UserPort->mp_SigBit);
while(NewMessage=(struct IntuiMessage *)GetMsg(cW->UserPort))
     {
     class = NewMessage->Class;
     code = NewMessage->Code;
     ReplyMsg(NewMessage);
     if ((class == GADGETDOWN) || (class == GADGETUP))
        {
        igad = (struct Gadget *)NewMessage->IAddress;
        gadgid = igad->GadgetID;
        switch (gadgid)
               {
               case BOOL_GAD1:
                  strcpy(name,DefString);
                  keepon = FALSE;
                  file = TRUE;
               break;
               case BOOL_GAD2:
                  keepon =  FALSE;
                  file = FALSE;
               break;
               case TEX_GAD:
                  strcpy(name,DefString);
                  keepon = FALSE;
                  file = TRUE;
               break;
               default:
               break;
               }   /* end switch */
        }  /* end if */
     }   /* end while */
}  /* end keepon */
cW->UserPort = NULL;
CloseWindow(cW);
return(file);
} /* end of function */

