#include "ezlib.h"

extern struct GfxBase *GfxBase;
void *AllocMem();

/* These functions deal with gadgets and adding them to your window */

struct Gadget *create_boolgadget(l_edge, t_edge, flags, activation, text, id)
  SHORT l_edge, t_edge;
  USHORT flags, activation;
  UBYTE *text;
  USHORT id;
{
 struct Gadget	  *gadg   = NULL;
 struct IntuiText *i_text = NULL;
 struct Border	  *bord   = NULL;
 int i,j;

 if ( (flags & GADGHIMAGE) != NULL)
   return NULL;

 if ( (flags & GADGHBOX) == NULL && (flags & GADGHCOMP) == NULL)
    flags |= GADGHCOMP;

 /* get everything allocated first, and fail if any of this does */
 gadg	= (struct Gadget *)   AllocMem(sizeof(struct Gadget),    MEMF_CLEAR);
 i_text = (struct IntuiText*) AllocMem(sizeof(struct IntuiText), MEMF_CLEAR);
 bord	= (struct Border *)   AllocMem(sizeof(struct Border),    MEMF_CLEAR);
 if (bord)
   bord->XY  = (SHORT *)AllocMem(10 * sizeof(SHORT), 0L);

 if (gadg == NULL || i_text == NULL || bord == NULL || bord->XY == NULL){
   if (gadg)
     FreeMem(gadg, sizeof(struct Gadget));
   if (i_text)
     FreeMem(i_text, sizeof(struct IntuiText));
   if (bord) {
     if (bord->XY)
       FreeMem(bord->XY, 10*sizeof(SHORT));
     FreeMem(bord, sizeof(struct Border));
   }
   return NULL;
 }

 /* do some generic setup stuff */
 gadg->LeftEdge   = l_edge;	    gadg->TopEdge      = t_edge;
 gadg->Flags	  = flags;	    gadg->Activation   = activation;
 gadg->GadgetID   = id; 	    gadg->GadgetType   = BOOLGADGET;
 gadg->GadgetText = i_text;	    gadg->GadgetRender = (APTR)bord;

 /* fill in the Itext struct */
 i_text->FrontPen   =	2;	    i_text->DrawMode   =  JAM1;
 i_text->LeftEdge   =	10;	    i_text->TopEdge    =  3;
 i_text->IText	    =	text;

 /* calculate where the box should be and fill in the border struct */
 bord->LeftEdge    = -1;	    bord->TopEdge     = -1;
 bord->FrontPen    = 1; 	    bord->BackPen     = 0;
 bord->DrawMode    = JAM1;	    bord->Count       = 5;

 /* here we calculate where everything should go... */
 i = IntuiTextLength(i_text) + 22;
 if (i < 30)
   i = 30;
 j = GfxBase->DefaultFont->tf_YSize + 6;

 bord->XY[0] = 0;		    bord->XY[1]  = 0;
 bord->XY[2] = i;		    bord->XY[3]  = 0;
 bord->XY[4] = i;		    bord->XY[5]  = j;
 bord->XY[6] = 0;		    bord->XY[7]  = j;
 bord->XY[8] = 0;		    bord->XY[9]  = 0;
 gadg->Width = i - 1;		    gadg->Height = j - 1;

 return gadg;
}


struct Gadget *makeboolgadget(window, l_edge, t_edge, text, id)
 struct Window *window;
 SHORT l_edge, t_edge;
 UBYTE *text;
 USHORT id;
{
 USHORT flags, activation;
 struct Gadget *gadg;

 flags	    = GADGHCOMP;
 activation = RELVERIFY;

 gadg = create_boolgadget(l_edge, t_edge, flags, activation, text, id);
 if (gadg == NULL)
   return NULL;

 AddGadget(window, gadg, (USHORT)~0);
 RefreshGList(gadg, window, NULL, 1);

 return gadg;
}


/* this routine will free up the gadget for you */

killgadget(win, gadg)
  struct Window *win;
  struct Gadget *gadg;
{
 int i;

 RemoveGadget(win, gadg);
 RefreshGadgets( win->FirstGadget, win, NULL);

 FreeMem(((struct Border *)gadg->GadgetRender)->XY, 10*sizeof(SHORT));
 FreeMem(gadg->GadgetRender, sizeof(struct Border));
 FreeMem(gadg->GadgetText, sizeof(struct IntuiText));
 FreeMem(gadg, sizeof(struct Gadget));
}

