/*
 *  CREATEARROWBUTTON.C
 */

#include "newlook.h"

static SHORT northarrow_pairs[]= {
0,3,  /* ...**... */
3,0,  /* ..*..*.. */
4,0,  /* .*....*. */
7,3,  /* *......* */

6,3,  /* ...**... */
4,1,  /* ..****.. */
3,1,  /* .**..**. */
1,3   /* **....** */
};

static SHORT southarrow_pairs[]= {
0,0,  /* *......* */
3,3,  /* .*....*. */
4,3,  /* ..*..*.. */
7,0,  /* ...**... */

6,0,  /* **....** */
4,2,  /* .**..**. */
3,2,  /* ..****.. */
1,0   /* ...**... */
};

static SHORT westarrow_pairs[]= {
5,0,  /* ....** */
0,2,  /* ..**.. */
5,4,  /* **.... */
      /* ..**.. */
      /* ....** */

7,0, 5,0,      /* .....*** */
5,1, 2,1,      /* ..****.. */
2,2, 0,2, 2,2, /* ***..... */
2,3, 5,3,      /* ..****.. */
5,4, 7,4       /* .....*** */
};

static SHORT eastarrow_pairs[]= {
0,0,  /* **.... */
5,2,  /* ..**.. */
0,4,  /* ....** */
      /* ..**.. */
      /* **.... */

0,0, 2,0,      /* ***..... */
2,1, 5,1,      /* ..****.. */
5,2, 7,2, 5,2, /* .....*** */
5,3, 2,3,      /* ..****.. */
2,4, 0,4       /* ***..... */
};


struct Gadget *CreateArrowButton(x,y,nswe,id)
SHORT x,y;
STRPTR nswe;
USHORT id;
{
  struct Gadget *g;
  struct Border *b, *a;
  SHORT w=16, h=8;

  ULONG UserHandle= SetNewLookHandle(PRIVATE_HANDLE);

  if(g= (struct Gadget *)SmartAllocate(GADGETSIZE))
  {
    if(a= (struct Border *)SmartAllocate(BORDERSIZE))
    {
      a->TopEdge    = 1+1;
      a->FrontPen   = 1;
      a->BackPen    = 0;
      a->DrawMode   = JAM1;
      a->NextBorder = (struct Border *)NULL;

      switch(*nswe)
      {
        case 'n': case 'u':
          a->LeftEdge = 2+2;
          a->Count    = 4;
          a->XY       = &northarrow_pairs[0];
          break;

        case 'N': case 'U':
          a->LeftEdge = 2+2;
          a->Count    = 8;
          a->XY       = &northarrow_pairs[0];
          break;

        case 's': case 'd':
          a->LeftEdge = 2+2;
          a->Count    = 4;
          a->XY       = &southarrow_pairs[0];
          break;

        case 'S': case 'D':
          a->LeftEdge = 2+2;
          a->Count    = 8;
          a->XY       = &southarrow_pairs[0];
          break;

        case 'w': case 'l':
          a->LeftEdge = 2+3;
          a->Count    = 3;
          a->XY       = &westarrow_pairs[0];
          ++w;
          ++h;
          break;

        case 'W': case 'L':
          a->LeftEdge = 2+2;
          a->Count    = 11;
          a->XY       = &westarrow_pairs[6];
          ++h;
          break;

        case 'e': case 'r':
          a->LeftEdge = 2+4;
          a->Count    = 3;
          a->XY       = &eastarrow_pairs[0];
          ++w;
          ++h;
          break;

        case 'E': case 'R':
          a->LeftEdge = 2+2;
          a->Count    = 11;
          a->XY       = &eastarrow_pairs[6];
          ++h;
          break;

        default:
          a= (struct Border *)SmartFree(a);
          break;
      }

      if(b= CreateBorder(0,0,w,h,FALSE))
      {
        if(a) a->NextBorder= b;
        else a= b;

        g->NextGadget    = (struct Gadget *)NULL;
        g->LeftEdge      = x;
        g->TopEdge       = y;
        g->Width         = w;
        g->Height        = h;
        g->Flags         = GADGHCOMP;
        g->Activation    = GADGIMMEDIATE|RELVERIFY;
        g->GadgetType    = BOOLGADGET;
        g->GadgetRender  = (APTR)a;
        g->SelectRender  = (APTR)NULL;
        g->GadgetText    = (struct IntuiText *)NULL;
        g->MutualExclude = (LONG)0L;
        g->SpecialInfo   = (APTR)NULL;
        g->GadgetID      = id;
        g->UserData      = (APTR)NULL;

        MakePrivateHandlePublic(UserHandle);
        return g;
      }
    }
  }
  if(UserHandle != PRIVATE_HANDLE)
  { SmartFreeAll(PRIVATE_HANDLE);
    (void)SetNewLookHandle(UserHandle);
  }
  return (struct Gadget *)NULL;
}
