/********************* Get filename "requestor" **********************/
#define HEIGHTOFGADG 96
#define RRp FileNameWdw->RPort

struct NewWindow NewFileNameWdw=
   {0,0,302,60,2,1,GADGETUP,
      SMART_REFRESH|ACTIVATE|WINDOWDRAG,
      NULL,NULL,"Program Request",NULL,NULL,1,1,1,1,CUSTOMSCREEN};

struct Window *FileNameWdw;

USHORT WaitForName();
char UndoFileNameBuffer[100];
UWORD *OKChip,*CancelChip;
UWORD OKData[] = {
   /* BitPlane 0 */
   0xFFFF,0xFFFF,0xFFFF,0xFFFF,
   0x8000,0x0000,0x0000,0x0001,
   0x8000,0x001C,0x7300,0x0001,
   0x8000,0x0036,0x3300,0x0001,
   0x8000,0x0063,0x3600,0x0001,
   0x8000,0x0063,0x3C00,0x0001,
   0x8000,0x0063,0x3600,0x0001,
   0x8000,0x0036,0x3300,0x0001,
   0x8000,0x001C,0x7300,0x0001,
   0x8000,0x0000,0x0000,0x0001,
   0x8000,0x0000,0x0000,0x0001,
   0xFFFF,0xFFFF,0xFFFF,0xFFFF,
   /* BitPlane 1 */
   0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0000,0x0000,0x0000
};

struct Image OKGadImage = {
   0, 0,      /* LeftEdge, TopEdge */
   64, 12, 2,   /* Width, Height, Depth */
   &OKData[0],   /* ImageData */
   0x0F, 0x00,   /* PlanePick, PlaneOnOff */
   NULL      /* NextImage */
};

struct Gadget OKGadg = {
   NULL,      /* NextGadget */
   20, 45,      /* LeftEdge, TopEdge */
   64, 12,      /* Width, Height */
   GADGIMAGE|GADGHCOMP,   /* Flags */
   RELVERIFY,   /* Activation */
   BOOLGADGET,   /* GadgetType */
   (APTR)&OKGadImage,   /* GadgetRender */
   NULL,      /* SelectRender */
   NULL,      /* GadgetText */
   0L,      /* MutualExclude */
   NULL,      /* SpecialInfo */
   TRUE,      /* GadgetID */
   NULL,      /* UserData */
};


UWORD CancelData[] = {
   0xFFFF,0xFFFF,0xFFFF,0xFFFF,
   0x8000,0x0000,0x0000,0x0001,
   0x801E,0x0000,0x0000,0x1C01,
   0x8033,0x0000,0x0000,0x0C01,
   0x8060,0x1E3E,0x1E1E,0x0C01,
   0x8060,0x0333,0x3333,0x0C01,
   0x8060,0x0F33,0x303F,0x0C01,
   0x8033,0x3333,0x3330,0x0C01,
   0x801E,0x1DB3,0x1E1E,0x1E01,
   0x8000,0x0000,0x0000,0x0001,
   0x8000,0x0000,0x0000,0x0001,
   0xFFFF,0xFFFF,0xFFFF,0xFFFF,
   0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0000,0x0000,0x0000,
   0x0000,0x0000,0x0000,0x0000
};

struct Image CancelGadImage = {
   0, 0,      /* LeftEdge, TopEdge */
   64, 12, 4,   /* Width, Height, Depth */
   &CancelData[0],   /* ImageData */
   0x0F, 0x00,   /* PlanePick, PlaneOnOff */
   NULL      /* NextImage */
};

struct Gadget CancelGadg = {
   NULL,      /* NextGadget */
   220, 45,      /* LeftEdge, TopEdge */
   64, 12,      /* Width, Height */
   GADGIMAGE|GADGHCOMP,   /* Flags */
   RELVERIFY,   /* Activation */
   BOOLGADGET,   /* GadgetType */
   (APTR)&CancelGadImage,   /* GadgetRender */
   NULL,      /* SelectRender */
   NULL,      /* GadgetText */
   0L,      /* MutualExclude */
   NULL,      /* SpecialInfo */
   FALSE,      /* GadgetID */
   NULL,      /* UserData */
};

struct StringInfo FileNameSI=
   {
   NULL,
   UndoFileNameBuffer,
   0,REQUESTERCHARS,0
   };

struct Gadget FileNameG=
   {
   NULL,
   24,30,260,9,GADGHCOMP,RELVERIFY,STRGADGET,NULL,NULL,NULL,
   NULL,&FileNameSI,TRUE
   };

GetFileName(FN,TopStuff) /*Opens a window 'requestor' to get a */
char *FN,*TopStuff;        /*file name for an open or save*/
{
   int State;

   State=Setup(FN,TopStuff);
   CloseWdwNStuff();
   return(State);
}

Setup(Buffer,TopString) /*Setup string, OK, and CANCEL gadgets*/
char *Buffer,*TopString;
{
   NewFileNameWdw.FirstGadget=&OKGadg;
   CancelGadg.NextGadget=&FileNameG;
   OKGadg.NextGadget=&CancelGadg;
   FileNameSI.Buffer=Buffer;
   strcpy(UndoFileNameBuffer,Buffer);

   Memory();
   OpenWdw();     /*Open the window*/

   SetAPen(RRp,1);   /*Draw the lines around the string gadget*/
   Move(RRp,20,29);
   Draw(RRp,20,39);
   Draw(RRp,284,39);
   Draw(RRp,284,29);
   Draw(RRp,20,29);

   Move(RRp,20,20);
   Text(RRp,TopString,strlen(TopString));

   return(WaitForName());
}

USHORT WaitForName()  /*Now wait for user to press RETURN, or select OK or CANCEL*/
{
   struct IntuiMessage *Mesg;
   struct Gadget *address;

   Wait(1<<FileNameWdw->UserPort->mp_SigBit);

   Mesg=(struct IntuiMessage *)GetMsg(FileNameWdw->UserPort);
   address=(struct Gadget *)Mesg->IAddress;
   ReplyMsg(Mesg);
   return(address->GadgetID);
}

OpenWdw()   /*Open the window*/
{
   NewFileNameWdw.Screen=Screen;

   if((FileNameWdw=(struct Window *)OpenWindow(&NewFileNameWdw))==NULL)
      exit();
}

Memory()    /*Get the chip memory for the OK and CANCEL gadget's images*/
{
   int c;

   if((OKChip=(UWORD *)AllocMem(196,MEMF_CHIP|MEMF_CLEAR))==NULL)
      return(FALSE);

   if((CancelChip=(UWORD *)AllocMem(196,MEMF_CHIP|MEMF_CLEAR))==NULL)
      return(FALSE);

   for(c=0;c<HEIGHTOFGADG;c++)
      {
      OKChip[c]=OKData[c];
      CancelChip[c]=CancelData[c];
      }

   OKGadImage.ImageData=OKChip;
   CancelGadImage.ImageData=CancelChip;
}

CloseWdwNStuff()  /*Free memory and close the window*/
{
   FreeMem(OKChip,196);
   FreeMem(CancelChip,196);
   CloseWindow(FileNameWdw);
}

