/*
 *  CREATEFRAME.C
 */

#include "newlook.h"

struct Border *CreateFrame(x,y,w,h,dx,dy)
SHORT x,y;
USHORT w,h,dx,dy;
{
  struct Border *b0,*b1,*l;
  SHORT *xy;

  long numlines= w-dx-1;

  ULONG UserHandle= SetNewLookHandle(PRIVATE_HANDLE);

  if(b0= CreateBorder(x,y,w,h,FALSE))
  {
    if(b1= CreateBorder(x+dx,y+dy,w-2*dx,h-2*dy,TRUE))
    {
      if(l= (struct Border *)SmartAllocate( numlines * BORDERSIZE ))
      {
        if(xy= (SHORT *)SmartAllocate( 2*4*sizeof(SHORT)))
        {
          int n,xn;

          /* shared pairs for the long lines */
          xy[0]= xy[1]= xy[2]= (SHORT)0L;
          xy[3]= h-3;

          /* shared pairs for the short lines */
          xy[4]= xy[5]= xy[6]= (SHORT)0L;
          xy[7]= dy-2;

          /* place left, long lines */
          for(n=0, xn=3; xn<dx; n++, xn+=2)
          {
            l[n].LeftEdge     = x+xn;
            l[n].TopEdge      = y+1;
            l[n].FrontPen     = 2L;
            l[n].BackPen      = 0;
            l[n].DrawMode     = JAM1;
            l[n].Count        = 2;
            l[n].XY           = &xy[0];
            l[n].NextBorder   = &l[n+1];
          }

          /* place short lines */
          for(; xn<w-dx; n+=2, xn+=2)
          {
            /* top */
            l[n].LeftEdge     = x+xn;
            l[n].TopEdge      = y+1;
            l[n].FrontPen     = 2L;
            l[n].BackPen      = 0;
            l[n].DrawMode     = JAM1;
            l[n].Count        = 2;
            l[n].XY           = &xy[4];
            l[n].NextBorder   = &l[n+1];

            /* bottom */
            l[n+1].LeftEdge   = x+xn;
            l[n+1].TopEdge    = y+h-dy;
            l[n+1].FrontPen   = 2L;
            l[n+1].BackPen    = 0;
            l[n+1].DrawMode   = JAM1;
            l[n+1].Count      = 2;
            l[n+1].XY         = &xy[4];
            l[n+1].NextBorder = &l[n+2];
          }

          /* place right, long lines */
          for(; xn<w-1; n++, xn+=2)
          {
            l[n].LeftEdge     = x+xn;
            l[n].TopEdge      = y+1;
            l[n].FrontPen     = 2L;
            l[n].BackPen      = 0;
            l[n].DrawMode     = JAM1;
            l[n].Count        = 2;
            l[n].XY           = &xy[0];
            l[n].NextBorder   = (xn<w-3) ? &l[n+1] : (struct Border *)NULL;
          }

          LastBorder(b0)->NextBorder= b1;
          LastBorder(b1)->NextBorder= &l[0];

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