/* formula2.c -- open a window to get formula,
close it, and proceed as directed.... */

#define FWX 300      /* Formula Window Size  */
#define FWY 70

#define CAN_GAD 0
#define OK_GAD 1

extern char z[30];
extern struct TextAttr TestFont;
/* define ok and cancel gadgets */
struct IntuiText ok_text = {3,2,JAM2,0,2,NL,(UBYTE *) "  OK  ",NL};
struct IntuiText can_text = {3,2,JAM2,0,2,NL,(UBYTE *) "Cancel",NL};
struct Gadget can_gadget = {
   NL, 230,50, 60,11, GADGHCOMP, RELVERIFY,
   BOOLGADGET, NL, NL,
   &can_text, NL,NL, CAN_GAD, NL };

struct Gadget ok_gadget = {
   &can_gadget, 230,30, 60,11, GADGHCOMP, RELVERIFY,
   BOOLGADGET, NL, NL,
   &ok_text, NL,NL, OK_GAD, NL };

/* define formula gadget */

#define FORMULASIZE 30
USHORT FormulaVectors[] = {
   -1,-1,
   182,-1,
   182, 12,
   -1, 12,
   -1, -1,
};

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

/* default text */
UBYTE FormulaBuffer[FORMULASIZE] = "COS(x)*COS(y)";
UBYTE UndoFormulaBuffer[FORMULASIZE];
struct StringInfo FormulaInfo = {
   FormulaBuffer,    /* pointer to I/O buffer */
   UndoFormulaBuffer,    /* pointer to undo buffer */
   0,             /* buffer position */
   FORMULASIZE,   /* max number of chars, including NULL */
   0, 0,          /* first char in display, undo positions */
   14,          /* 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 FormulaText = {
   2, 2,    /* FrontPen, BackPen */
   JAM1,          /* DrawMode */
   -101, 0,        /* LeftEdge, TopEdge (relative to gadget) */
   &TestFont,     /* pointer to TextFont */
   (UBYTE *) "Formula: ",    /* pointer to Text */
   NL           /* no pointer to NextText */
};

#define FORMULA_GAD 2

struct Gadget FormulaGadget = {
   &ok_gadget,             /* pointer to Next Gadget */
   110, 10, 180, 11,  /* (Left Top Width Height) Hit Box */
   GADGHCOMP,        /* Flags */
   RELVERIFY,        /* Activation flags */
   STRGADGET,        /* Type */
   (APTR)&FormulaBorder, /* pointer to Border Image */
   NL,             /* no pointer to SelectRender */
   &FormulaText,        /* pointer to GadgetText */
   NL,                /* no MutualExclude */
   (APTR)&FormulaInfo,  /* pointer to SpecialInfo */
   FORMULA_GAD,                /*  ID */
   NL              /* no pointer to special data */
};

/* Used to open a Window   */
struct NewWindow NewFwin = {
   10,65, FWX,FWY, 2,BCOL, NL,    /* IDCMP set up AFTER CALL */
   ACTIVATE | SMART_REFRESH,
   &FormulaGadget,NL, NL,    /* FirstGadget, CheckMark, Title  */
   NL,NL,              /* MUST SET SCREEN AFTER OPENSCREEN!!! */
   FWX,FWY,FWX,FWY, CUSTOMSCREEN }; /* MinW, MinH, MaxW, MaxH */
 
 
/********************************************************************
* formula(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 formula(calling_window)
struct Window *calling_window;
{
struct Window *cW;      /* save window handle   */
 
FAST struct IntuiMessage *imsg;
FAST struct Gadget *igad;
 
FAST int class;
int confirmed;
BOOL stayhere;
 
   /* Get screen from calling window   */
   NewFwin.Screen = calling_window->WScreen;  /* NEED TO SET SCREEN!!! */
   if ( ! (cW = (struct Window *)OpenWindow(&NewFwin)) ) {
      return(FALSE); /* Oops...  */
      }
 
   cW->UserPort = calling_window->UserPort;
   ModifyIDCMP(cW, GADGETUP | GADGETDOWN);
   stayhere=TRUE;
   while (stayhere == 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 CAN_GAD:
                     confirmed=0;
                     stayhere=FALSE;
                     break;
                  case OK_GAD:
                     strcpy(z,FormulaBuffer);
                     confirmed=1;
                     stayhere=FALSE;
                     break;
                  case FORMULA_GAD:
                     strcpy(z,FormulaBuffer);
                     break;
               }
         }
      }
   }
   cW->UserPort = NL;
   CloseWindow(cW);
   if (confirmed==1)
      return(1);
   else
      return(0);
}
