/* save.c -- open a window to get filename,
close it, and proceed.... */

#define PLX2 240      /* Save Window Size  */
#define PLY2 100

#define CN_GAD2 3
#define OK_GAD2 4

extern char filename[32];

/* define ok and cancel gadgets */
 
struct IntuiText oktxt2 = {3,2,JAM2,8,2,NL,(UBYTE *)" OK ",NL};
struct IntuiText cntxt2 = {3,2,JAM2,0,2,NL,(UBYTE *)"Cancel",NL};
 
struct Gadget cn_gad2 = {
   NL, 170,65, 54,11, GADGHCOMP, RELVERIFY,
   BOOLGADGET, NL, NL,
   &cntxt2, NL,NL, CN_GAD2, NL };

struct Gadget ok_gad2 = {
   &cn_gad2, 170,50, 54,11, GADGHCOMP, RELVERIFY,
   BOOLGADGET, NL, NL,
   &oktxt2, NL,NL, OK_GAD2, NL };

/* define filename gadget */

#define ESTRINGSIZE 32
UBYTE UndoBuffer[ESTRINGSIZE];
USHORT StrVectors[] = {
   -1,-1,
   150,-1,
   150, 12,
   -1, 12,
   -1, -1,
};

struct Border StrBorder3 = {
   -1, -1,           /* initial offsets, gadget relative */
   1, 0, JAM1, /* pens (fore, back) and drawmode */
   5,                /* number of vectors */
   StrVectors,     /* pointer to the actual array of vectors */
   NULL       /* no next Border, can point to another border */
};

/* default text */
UBYTE EnglBuffer[ESTRINGSIZE] = "temp.pic";
struct StringInfo EnglInfo = {
   EnglBuffer,    /* pointer to I/O buffer */
   UndoBuffer,    /* pointer to undo buffer */
   0,             /* buffer position */
   ESTRINGSIZE,   /* max number of chars, including NULL */
   0, 0,          /* first char in display, undo positions */
   9,          /* number of chars (currently) in the buffer */
   0, 0, 0,    /* position variables calculated by Intuition */
   NULL,          /* no pointer to RastPort */
   0,            /* not a LongInt string gadget */
   NULL           /* no pointer to alternate keymap */
};
struct IntuiText EnglText = {
   2, 2,    /* FrontPen, BackPen */
   JAM1,          /* DrawMode */
   0, -11,        /* LeftEdge, TopEdge (relative to gadget) */
   &TestFont,     /* pointer to TextFont */
   (UBYTE *) "Filename: ",    /* pointer to Text */
   NL           /* no pointer to NextText */
};

#define ENG_GAD 5

struct Gadget EnglStrGadget = {
   &ok_gad2,             /* pointer to Next Gadget */
   10, 50, 140, 11,  /* (Left Top Width Height) Hit Box */
   GADGHCOMP,        /* Flags */
   RELVERIFY,        /* Activation flags */
   STRGADGET,        /* Type */
   (APTR)&StrBorder3, /* pointer to Border Image */
   NL,             /* no pointer to SelectRender */
   &EnglText,        /* pointer to GadgetText */
   NL,                /* no MutualExclude */
   (APTR)&EnglInfo,  /* pointer to SpecialInfo */
   ENG_GAD,                /*  ID */
   NL              /* no pointer to special data */
};

/* Used to open a Window   */
struct NewWindow NewSwin = {
   10,10, PLX2,PLY2, 2,BCOL, NL,    /* IDCMP set up AFTER CALL */
   ACTIVATE | SMART_REFRESH,
   &EnglStrGadget,NL, NL,    /* FirstGadget, CheckMark, Title  */
   NL,NL,              /* MUST SET SCREEN AFTER OPENSCREEN!!! */
   PLX2,PLY2,PLX2,PLY2, CUSTOMSCREEN }; /* MinW, MinH, MaxW, MaxH */
 
 
/********************************************************************
* save(window)
*    This is the meat. This routine opens the save window and
* retains control until the user selects the OK or CANCEL gadget.
*     The calling arguement is a window pointer.  That window
* is expected to have opened an IDCMP port, which save uses.
********************************************************************/
int save(calling_window)
struct Window *calling_window;
{
struct Window *cW;      /* save window handle   */
 
FAST struct IntuiMessage *imsg;
FAST struct Gadget *igad;
 
FAST int class;
int munge;
BOOL keepon;
 
   /* Get screen from calling window   */
   NewSwin.Screen = calling_window->WScreen;  /* NEED TO SET SCREEN!!! */
   if ( ! (cW = (struct Window *)OpenWindow(&NewSwin)) ) {
      return(FALSE); /* Oops...  */
      }
 
   cW->UserPort = calling_window->UserPort;
   ModifyIDCMP(cW, GADGETUP | GADGETDOWN);
   keepon=TRUE;
   while (keepon == TRUE) {
      while (imsg=(struct IntuiMessage *)GetMsg(cW->UserPort)) {
         class = imsg->Class;
         ReplyMsg(imsg);
         switch ( class ) {
            case GADGETUP:
            case GADGETDOWN:
               igad =(struct Gadget *) imsg->IAddress;
               switch ( igad->GadgetID ) {
                  case CN_GAD2:
                     munge=0;
                     keepon=FALSE;
                     break;
                  case OK_GAD2:
                     strcpy(filename,EnglBuffer);
                     munge=1;
                     keepon=FALSE;
                     break;
                  case ENG_GAD:
                     strcpy(filename,EnglBuffer);
                     break;
               }
         }
      }
   }
   cW->UserPort = NL;
   CloseWindow(cW);
   if (munge==1)
      return(1);
   else
      return(0);
}
