/*========================================================================
  reqn.c - General purpose requestor code.  Contains the following functions:

  Init_all_req() - Initializes all requestors
  initreq        - Initializes a particular requestor
  getrname       - Brings up requestor that returns pointer to a name
                   also accepts an input string thats displayed on the
                   requestor.
==========================================================================*/

/*  The usual header files inserted here */
#include <intuition/intuition.h>
#include <libraries/dosextens.h>
#include <graphics/gfxbase.h>
#include <graphics/gfx.h>
#include <graphics/display.h>
#include <exec/memory.h>
#include <workbench/workbench.h>
#include <workbench/startup.h>
#include <devices/narrator.h>
#include <devices/audio.h>
#include <libraries/translator.h>

/*=========================================
  Important requestor constants wich might
  eventually get moved into "gad.h" file,
  but for now,  we keep it here.
=========================================*/

#define OK_BUT    1
#define CAN_BUT   2
#define NAM_GAD   3

/*=========================================
  External declarations
==========================================*/

extern struct Window *w;
extern struct NewWindow nw;
extern struct IntuiMessage *GetMsg();

/*=========================================
  Border around the GetName requestor
==========================================*/

SHORT req_pairs[] = {
  0,     0,
  257,   0,
  257,  60,
  0,    60,
  0,     0
};

struct Border req_bord = {
  -1,  -1,
  1,  3,  JAM2,
  5,
  (SHORT *)&req_pairs,
  NULL
};


/*=========================================
  Gadgets, Borders, Etc to the getname
  requestor
==========================================*/

SHORT Pairs_1[] = {
  0,     0,
  65,    0,
  65,   10,
  0,    10,
  0,     0
};

struct Border bord_1 = {
  -1,  -1,
  1,  3,  JAM2,
  5,
  (SHORT *)&Pairs_1,
  NULL
};

struct IntuiText OKtext = {2,2,JAM1, 34, 1, NULL,
  (UBYTE *) "OK", NULL};
 
struct Gadget ok_but = {
  NULL, 121, 39, 65, 10,
  GADGHCOMP,
  GADGIMMEDIATE | ENDGADGET | RELVERIFY,
  BOOLGADGET | REQGADGET,
  (APTR)&bord_1,  (APTR)&bord_1,
  &OKtext, NULL,
  NULL,   OK_BUT, NULL
};
 
SHORT Pairs_2[] = {
  0,     0,
  63,     0,
  63,     10,
  0,     10,
  0,     0
};
struct Border bord_2 = {
  -1,  -1,
  1,  3,  JAM2,
  5,
  (SHORT *)&Pairs_2,
  NULL
};

struct IntuiText CANCELtext = {2,2,JAM1, 14, 1, NULL,
  (UBYTE *) "CANCEL", NULL};
 
struct Gadget can_but = {
 &ok_but, 37, 39, 63, 10,
 GADGHCOMP,
 GADGIMMEDIATE | ENDGADGET | RELVERIFY,
  BOOLGADGET | REQGADGET,
  (APTR)&bord_2,  (APTR)&bord_2,
  &CANCELtext, NULL,
  NULL,   CAN_BUT, NULL,
};
 
SHORT Pairs_3[] = {
  0,     0,
  152,     0,
  152,     10,
  0,     10,
  0,     0
};
struct Border bord_3 = {
  -1,  -1,
  1,  3,  JAM2,
  5,
  (SHORT *)&Pairs_3,
  NULL
};
 
UBYTE sbuf_1[256] = '\0';
 
struct StringInfo txstr_1 = {
  sbuf_1, NULL,
  0, 256, 0,
  0, 0,
  0, 0, 0,
  0, 0,
  0
};

struct IntuiText req_name = {2,2, JAM1, 10, 10, NULL, NULL, NULL};

struct Gadget nametext = {
 &can_but, 36, 21, 152, 10,
 GADGHCOMP,
  RELVERIFY | ENDGADGET,
  STRGADGET | REQGADGET,
  (APTR)&bord_3,  (APTR)&bord_3,
  NULL,
  NULL,
  (APTR)&txstr_1,   NAM_GAD, NULL
};
 
struct Requester Save_req;


/*======================================================================
  Code that Initializes ALL requestors in the Gadget Editor program
=======================================================================*/
init_all_req()
{
    initreq(&Save_req, &nametext, &req_bord);
}
/*======================================================================
  InitReq(req, gad, bord) - Initializes a requestor,  req is a pointer to a
  previously allocated requestor.  Later on,  perhaps we can Allocate
  one and return a pointer to it,   However,  because cleaning up
  is tricky,   I'm taking the easy way out
=======================================================================*/
initreq(req, gad, bord)
struct Requester *req;       /* Pointer to a requestor */
struct Gadget *gad;          /* First gadget in requestor */
struct Border *bord;         /* Pointer to border */
{
    InitRequester(req);      /* Init the requestor */
    req->LeftEdge  = 169;
    req->TopEdge   = 44;
    req->Width     = 257;
    req->Height    = 60;
    req->ReqGadget = gad;             /* First Gadget */
    req->ReqText   = NULL;            /* First Intuitext */
    req->BackFill  = 1;               /* Backgnd color */
    req->Flags     = 0;               /* Must have at least one */
    req->ReqBorder = bord;            /* Outside border */
    
}
/*======================================================================
  nam_info - Return a pointer to the stringinfo structure to the
  filename requester.   We do this because it's good programming
  practice to keep this module isolated as much as possible from 
  the main program.
=======================================================================*/
struct StringInfo *nam_info()
{
    return(&txstr_1);
}

/*======================================================================
 
       Brings up this requester  called: Save_req
       -----------------------------------------
      |                                         |
      |        SAVE FILENAME AS:                |
      |    ------------------------------       |
      |   | MyPresentation               |      |
      |    ------------------------------       |
      |                                         |
      |   ----------        -------------       |
      |  |    OK    |      |    CANCEL   |      |
      |   ----------        -------------       |
      |                                         |
       -----------------------------------------

In an attempt to isolate this routine from knowing the names of the
Gadget structures,  It relies on the GadgetID field to identify which
gadget got struck.   OK_BUT is the OK button gadget,  CAN_BUT is the
"CANCEL" button.
=======================================================================*/
UBYTE *getrname(str)
UBYTE *str;                /* Message string "SAVE FILENAME AS:" */
{
  int   looping=TRUE;
  struct IntuiMessage *mes;
  struct StringInfo *si = NULL;
  struct Gadget *gad;     
  ULONG class;
  ULONG iflags;

  /*-------- Set up the IntuiText for the gadget -------*/
  req_name.IText = str;
  Save_req.ReqText = &req_name;
  iflags = nw.IDCMPFlags;         /* Current flag settings */

  /*-------- Open up the requester $$$---------*/
  if ((Request(&Save_req, w)) == 1L) {
     iflags |= REQSET;
     ModifyIDCMP(w, iflags);
     while (looping) {
         if ((mes = (struct IntuiMessage *)GetMsg(w->UserPort)) == 0L) {
            Wait(1L<<w->UserPort->mp_SigBit);
            continue;
         }
         class = mes->Class;
         gad = (struct Gadget *)mes->IAddress;
         ReplyMsg(mes);
 
        /*-------- Handle the case where requester clears --------*/
        if (class == GADGETDOWN && gad->GadgetID == CAN_BUT) {
            return(NULL);
        }
        if (class == REQSET) {
           ActivateGadget(&nametext, w, &Save_req);
           txstr_1.BufferPos = txstr_1.MaxChars;
           RefreshGadgets(&nametext, w, &Save_req);
        }

        if (class == GADGETDOWN && gad->GadgetID == OK_BUT) {
            return(sbuf_1);
        }

        if (class == GADGETUP && gad == &nametext) {
           ModifyIDCMP(w, nw.IDCMPFlags);
           return(sbuf_1);
        }

        if (class == REQCLEAR) {
            looping = FALSE;
        }

     } /* while */
  } /* if request */
  else printf("Requester error...\n");
}
/* ==================================
   get_req_name - Get a string from the
   standard requester.   Return with
   the string in the given pointer.
====================================*/
get_req_name(in_str, out_str)
UBYTE *in_str;             /* Input name displayed in requester */
UBYTE *out_str;            /* Output name displayed in req */
{   UBYTE *temp;
    if (out_str != NULL) {
       strcpy(txstr_1.Buffer, out_str);
       txstr_1.BufferPos = strlen(txstr_1.Buffer) + 1;
    }
    else {
       *txstr_1.Buffer = '\0';
        txstr_1.BufferPos = 0;
    }
    if ((temp = getrname(in_str)) != NULL) {
        strcpy(out_str, temp);
        return (TRUE);
    }
   else return (FALSE);
}
