home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 432b.lha / EzLib / src / gadget.c < prev    next >
C/C++ Source or Header  |  1990-11-10  |  3KB  |  118 lines

  1. #include "ezlib.h"
  2.  
  3. extern struct GfxBase *GfxBase;
  4. void *AllocMem();
  5.  
  6. /* These functions deal with gadgets and adding them to your window */
  7.  
  8. struct Gadget *create_boolgadget(l_edge, t_edge, flags, activation, text, id)
  9.   SHORT l_edge, t_edge;
  10.   USHORT flags, activation;
  11.   UBYTE *text;
  12.   USHORT id;
  13. {
  14.  struct Gadget      *gadg   = NULL;
  15.  struct IntuiText *i_text = NULL;
  16.  struct Border      *bord   = NULL;
  17.  int i,j;
  18.  
  19.  if ( (flags & GADGHIMAGE) != NULL)
  20.    return NULL;
  21.  
  22.  if ( (flags & GADGHBOX) == NULL && (flags & GADGHCOMP) == NULL)
  23.     flags |= GADGHCOMP;
  24.  
  25.  /* get everything allocated first, and fail if any of this does */
  26.  gadg    = (struct Gadget *)   AllocMem(sizeof(struct Gadget),    MEMF_CLEAR);
  27.  i_text = (struct IntuiText*) AllocMem(sizeof(struct IntuiText), MEMF_CLEAR);
  28.  bord    = (struct Border *)   AllocMem(sizeof(struct Border),    MEMF_CLEAR);
  29.  if (bord)
  30.    bord->XY  = (SHORT *)AllocMem(10 * sizeof(SHORT), 0L);
  31.  
  32.  if (gadg == NULL || i_text == NULL || bord == NULL || bord->XY == NULL){
  33.    if (gadg)
  34.      FreeMem(gadg, sizeof(struct Gadget));
  35.    if (i_text)
  36.      FreeMem(i_text, sizeof(struct IntuiText));
  37.    if (bord) {
  38.      if (bord->XY)
  39.        FreeMem(bord->XY, 10*sizeof(SHORT));
  40.      FreeMem(bord, sizeof(struct Border));
  41.    }
  42.    return NULL;
  43.  }
  44.  
  45.  /* do some generic setup stuff */
  46.  gadg->LeftEdge   = l_edge;        gadg->TopEdge      = t_edge;
  47.  gadg->Flags      = flags;        gadg->Activation   = activation;
  48.  gadg->GadgetID   = id;         gadg->GadgetType   = BOOLGADGET;
  49.  gadg->GadgetText = i_text;        gadg->GadgetRender = (APTR)bord;
  50.  
  51.  /* fill in the Itext struct */
  52.  i_text->FrontPen   =    2;        i_text->DrawMode   =  JAM1;
  53.  i_text->LeftEdge   =    10;        i_text->TopEdge    =  3;
  54.  i_text->IText        =    text;
  55.  
  56.  /* calculate where the box should be and fill in the border struct */
  57.  bord->LeftEdge    = -1;        bord->TopEdge     = -1;
  58.  bord->FrontPen    = 1;         bord->BackPen     = 0;
  59.  bord->DrawMode    = JAM1;        bord->Count       = 5;
  60.  
  61.  /* here we calculate where everything should go... */
  62.  i = IntuiTextLength(i_text) + 22;
  63.  if (i < 30)
  64.    i = 30;
  65.  j = GfxBase->DefaultFont->tf_YSize + 6;
  66.  
  67.  bord->XY[0] = 0;            bord->XY[1]  = 0;
  68.  bord->XY[2] = i;            bord->XY[3]  = 0;
  69.  bord->XY[4] = i;            bord->XY[5]  = j;
  70.  bord->XY[6] = 0;            bord->XY[7]  = j;
  71.  bord->XY[8] = 0;            bord->XY[9]  = 0;
  72.  gadg->Width = i - 1;            gadg->Height = j - 1;
  73.  
  74.  return gadg;
  75. }
  76.  
  77.  
  78. struct Gadget *makeboolgadget(window, l_edge, t_edge, text, id)
  79.  struct Window *window;
  80.  SHORT l_edge, t_edge;
  81.  UBYTE *text;
  82.  USHORT id;
  83. {
  84.  USHORT flags, activation;
  85.  struct Gadget *gadg;
  86.  
  87.  flags        = GADGHCOMP;
  88.  activation = RELVERIFY;
  89.  
  90.  gadg = create_boolgadget(l_edge, t_edge, flags, activation, text, id);
  91.  if (gadg == NULL)
  92.    return NULL;
  93.  
  94.  AddGadget(window, gadg, (USHORT)~0);
  95.  RefreshGList(gadg, window, NULL, 1);
  96.  
  97.  return gadg;
  98. }
  99.  
  100.  
  101. /* this routine will free up the gadget for you */
  102.  
  103. killgadget(win, gadg)
  104.   struct Window *win;
  105.   struct Gadget *gadg;
  106. {
  107.  int i;
  108.  
  109.  RemoveGadget(win, gadg);
  110.  RefreshGadgets( win->FirstGadget, win, NULL);
  111.  
  112.  FreeMem(((struct Border *)gadg->GadgetRender)->XY, 10*sizeof(SHORT));
  113.  FreeMem(gadg->GadgetRender, sizeof(struct Border));
  114.  FreeMem(gadg->GadgetText, sizeof(struct IntuiText));
  115.  FreeMem(gadg, sizeof(struct Gadget));
  116. }
  117.  
  118.