/*
 *  CREATESLIDER.C
 */

#include "newlook.h"

struct Gadget *CreateSlider(x,y, n, type, id)
SHORT x,y, n;
STRPTR type;
USHORT id;
{
  struct Gadget *s, *a[2];
  struct PropInfo *pi;
  struct Border *b;   /* will always get chained to a[0] */
  struct Image *i;    /* does not need to be initialized for s */
  STRPTR atype[2];

  SHORT bx,by,  /* Border position (relative to a[0]) */
        bw,bh;  /* Border width and height (not allways used) */

  ULONG UserHandle= SetNewLookHandle(PRIVATE_HANDLE);

  /* arrow types */

  atype[0]= atype[1]= (STRPTR)NULL;

  if(type)
  {
    switch(*type)
    {
      case 'L': case 'R':
        atype[0]= "W";
        atype[1]= "E";
        break;
      case 'l': case 'r':
        atype[0]= "w";
        atype[1]= "e";
        break;
      case 'T': case 'B':
        atype[0]= "N";
        atype[1]= "S";
        break;
      case 't': case 'b':
        atype[0]= "n";
        atype[1]= "s";
        break;
    }
  }

  if(atype[0] && atype[1])
  {
    if(pi= (struct PropInfo *)SmartAllocate(PROPINFOSIZE))
    {
      if(i= (struct Image *)SmartAllocate(IMAGESIZE))
      {
        if(s= (struct Gadget *)SmartAllocate(GADGETSIZE))
        {
          if(a[0]= CreateArrowButton(x,y, atype[0], id+1))
          {
            if(a[1]= CreateArrowButton(x,y, atype[1], id+2))
            {
              pi->Flags      = AUTOKNOB|PROPBORDERLESS;
              pi->HorizPot   = 0x0000;
              pi->VertPot    = 0x0000;
              pi->HorizBody  = 0x0000;
              pi->VertBody   = 0x0000;

              /* rest set and maintained by Intuition */
              pi->CWidth     =
              pi->CHeight    =
              pi->HPotRes    =
              pi->VPotRes    =
              pi->LeftBorder =
              pi->TopBorder  = (UWORD)0L;

              switch(*type)
              {
/* h */         case 'R': case 'r':
                  pi->Flags |= FREEHORIZ;
                  bw= n - a[0]->Width - a[1]->Width;
                  bh= a[0]->Height;  /* == a[1]->Height */
                  bx= -bw;
                  by= 0;
                  break;
                case 'L': case 'l':
                  pi->Flags |= FREEHORIZ;
                  bx= a[0]->Width + a[1]->Width;
                  bw= n - bx;
                  bh= a[0]->Height;  /* == a[1]->Height */
                  by= 0;
                  break;
/* v */         case 'B': case 'b':
                  pi->Flags |= FREEVERT;
                  bw= a[0]->Width; /* == a[1]->Width */
                  bh= n - a[0]->Height - a[1]->Height;
                  bx= 0;
                  by= -bh;
                  break;
                case 'T': case 't':
                  pi->Flags |= FREEVERT;
                  bx= 0;
                  by= a[0]->Height + a[1]->Height;
                  bw= a[0]->Width; /* == a[1]->Width */
                  bh= n - by;
                  break;
              }

              if(bw < (KNOBHMIN+8))
                bw= KNOBHMIN+8;
              if(bh < (KNOBVMIN+4))
                bh= KNOBVMIN+4;

              if(b= CreateBorder(bx,by, bw,bh,FALSE))
              {
                AddGBorder(a[0],b);

                s->LeftEdge      = x+4;
                s->TopEdge       = y+2;
                s->Width         = bw-8;
                s->Height        = bh-4;
                s->Flags         = GADGHCOMP;
                s->Activation    = RELVERIFY|FOLLOWMOUSE;
                s->GadgetType    = PROPGADGET;
                s->GadgetRender  = (APTR)i;
                s->SelectRender  = (APTR)NULL;
                s->GadgetText    = (struct IntuiText *)NULL;
                s->MutualExclude = (LONG)0L;
                s->SpecialInfo   = (APTR)pi;
                s->GadgetID      = id;
                s->UserData      = (APTR)NULL;

                switch(*type)
                {
                  case 'R': case 'r':
                    a[0]->LeftEdge+= bw;
                    a[1]->LeftEdge+= bw + a[0]->Width;
                    break;
                  case 'L': case 'l':
                    s->LeftEdge+= a[0]->Width + a[1]->Width;
                    a[1]->LeftEdge+= a[0]->Width;
                    break;
                  case 'B': case 'b':
                    a[0]->TopEdge+= bh;
                    a[1]->TopEdge+= bh + a[0]->Height;
                    break;
                  case 'T': case 't':
                    s->TopEdge+= a[0]->Height + a[1]->Height;
                    a[1]->TopEdge+= a[0]->Height;
                    break;
                }

                /* chain slider and arrows */

                s->NextGadget    = a[0];
                a[0]->NextGadget = a[1];
                a[1]->NextGadget = (struct Gadget *)NULL;

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