#include <intuition/intuisup.h>
#include <intuition/intuitionbase.h>
#include <graphics/gfxmacros.h>
#include <proto/all.h>


extern struct  IntuitionBase  *IntuitionBase;


#define  GREY     0
#define  WHITE    1
#define  BLACK    2
#define  YELLW    3

#define  MAX_GADGETS          5
#define  TITLEBAR_HEIGHT      10


static struct TextAttr safefont = { "topaz.font", 8, 0, 0 };

static USHORT chip Close1Data[] =
   {
   0xffff,    /* ################ */
   0xc003,    /* ##............## */
   0xcff3,    /* ##..########..## */
   0xcff3,    /* ##..########..## */
   0xcff3,    /* ##..########..## */
   0xcff3,    /* ##..########..## */
   0xc003,    /* ##............## */
   0xffff,    /* ################ */

   0x0000,    /* ................ */
   0x3ffc,    /* ..############.. */
   0x300c,    /* ..##........##.. */
   0x300c,    /* ..##........##.. */
   0x300c,    /* ..##........##.. */
   0x300c,    /* ..##........##.. */
   0x3ffc,    /* ..############.. */
   0x0000,    /* ................ */
   };

static USHORT chip Close2Data[] =
   {
   0xffff,    /* ################ */
   0xc003,    /* ##............## */
   0xc3c3,    /* ##....####....## */
   0xcdb3,    /* ##..##.##.##..## */
   0xcdb3,    /* ##..##.##.##..## */
   0xc3c3,    /* ##....####....## */
   0xc003,    /* ##............## */
   0xffff,    /* ################ */

   0x0000,    /* ................ */
   0x3ffc,    /* ..############.. */
   0x3c3c,    /* ..####....####.. */
   0x324c,    /* ..##..#..#..##.. */
   0x324c,    /* ..##..#..#..##.. */
   0x3c3c,    /* ..####....####.. */
   0x3ffc,    /* ..############.. */
   0x0000,    /* ................ */
   };

static USHORT chip Depth1Data[] =
   {
   0xffff,    /* ################ */
   0xc03f,    /* ##........###### */
   0xcf3f,    /* ##..####..###### */
   0xcc03,    /* ##..##........## */
   0xc003,    /* ##............## */
   0xfc03,    /* ######........## */
   0xfc03,    /* ######........## */
   0xffff,    /* ################ */

   0x0000,    /* ................ */
   0x3fc0,    /* ..########...... */
   0x30c0,    /* ..##....##...... */
   0x33fc,    /* ..##..########.. */
   0x3ffc,    /* ..############.. */
   0x03fc,    /* ......########.. */
   0x03fc,    /* ......########.. */
   0x0000,    /* ................ */
   };

static USHORT chip Depth2Data[] =
   {
   0xffff,    /* ################ */
   0xc03f,    /* ##........###### */
   0xc03f,    /* ##........###### */
   0xc003,    /* ##............## */
   0xc0f3,    /* ##......####..## */
   0xfcf3,    /* ######..####..## */
   0xfc03,    /* ######........## */
   0xffff,    /* ################ */

   0x0000,    /* ................ */
   0x3fc0,    /* ..########...... */
   0x3fc0,    /* ..########...... */
   0x3ffc,    /* ..############.. */
   0x3f0c,    /* ..######....##.. */
   0x030c,    /* ......##....##.. */
   0x03fc,    /* ......########.. */
   0x0000,    /* ................ */
   };

static USHORT chip SizeData[] =
   {
   0xfcff,    /* ######..######## */
   0xf3ff,    /* ####..########## */
   0xccff,    /* ##..##..######## */
   0x30ff,    /* ..##....######## */
   0xffff,    /* ################ */

   0x0300,    /* ......##........ */
   0x0c00,    /* ....##.......... */
   0x3300,    /* ..##..##........ */
   0xcf00,    /* ##..####........ */
   0x0000,    /* ................ */
   };

static   struct   NewWindow   nw =
   {
   10,20, 200,100, 0,1, NULL, SMART_REFRESH | BORDERLESS | ACTIVATE,
   NULL, NULL, NULL, NULL, NULL, 120,60, 2000,2000, CUSTOMSCREEN
   };

static SHORT    WBorderData0[] = { 1,0, 1,0, 0,0, 0,0 };
static SHORT    WBorderData1[] = { 0,0, 0,0, 0,0, 0,0, 0,0 };
static struct   Border  WBorder[] =
   {
      { 0, 0, BLACK, 0, JAM1, 4, WBorderData0, &WBorder[1] },
      { 0, 0, BLACK, 0, JAM1, 5, WBorderData1, NULL }
   };
static struct   Box     WBox = { NULL, 1,0, 0,TITLEBAR_HEIGHT, BLACK, BLACK, WHITE, 0, NULL, NULL };

static struct   Image   Close1    = { 0,0, 16,8, 2, Close1Data, 0x03,0x00, NULL };
static struct   Image   Close2    = { 0,0, 16,8, 2, Close2Data, 0x03,0x00, NULL };
static struct   Image   Depth1    = { 0,0, 16,8, 2, Depth1Data, 0x03,0x00, NULL };
static struct   Image   Depth2    = { 0,0, 16,8, 2, Depth2Data, 0x03,0x00, NULL };
static struct   Image   Size      = { 0,0, 10,5, 2, SizeData, 0x3,0x0, NULL };
static struct   Gadget  WGadget[MAX_GADGETS] =
   {
      { NULL, -19, 1, 16, 8, GRELRIGHT | GADGIMAGE | GADGHCOMP, RELVERIFY,
        SYSGADGET | WUPFRONT, (APTR)&Depth1, NULL, NULL, NULL, NULL, 0, NULL },
      { NULL, -39, 1, 16, 8, GRELRIGHT | GADGIMAGE | GADGHCOMP, RELVERIFY,
        SYSGADGET | WDOWNBACK, (APTR)&Depth2, NULL, NULL, NULL, NULL, 0, NULL },
      { NULL, -11, -5, 10, 5, GRELRIGHT | GRELBOTTOM | GADGIMAGE | GADGHNONE, 0,
        SYSGADGET | SIZING, (APTR)&Size, NULL, NULL, NULL, NULL, 0, NULL },
      { NULL, 4, 1, 16, 8, GADGIMAGE | GADGHIMAGE, RELVERIFY,
        SYSGADGET | CLOSE, (APTR)&Close1, (APTR)&Close2, NULL, NULL, NULL, 0, NULL },
      { NULL, 23, 0, -65, TITLEBAR_HEIGHT, GRELWIDTH | GADGHNONE, 0,
        SYSGADGET | WDRAGGING, NULL, NULL, NULL, NULL, NULL, 0, NULL },
   };

static ULONG StdIDCMP = MOUSEBUTTONS | GADGETUP | GADGETDOWN | RAWKEY | CLOSEWINDOW;


VOID
CloseStdWindow(window)
struct   Window   *window;
{
   struct   Gadget   *gadget = window->FirstGadget, *g=NULL;

   while (gadget)
         {
         if (gadget->GadgetType & SYSGADGET) RemoveGadget(window, (g=gadget));
         gadget = gadget->NextGadget;
         if (g) { Delete(g,1); g=NULL; }
         }

   CloseWindow(window);
}

VOID
RemakeStdWindow(window, name, pattern)
struct   Window *window;
UBYTE    *name;
USHORT   *pattern;
{
   struct TextFont   *font = window->RPort->Font, *safe;

   safe = (struct TextFont *)OpenFont(&safefont);
   SetFont(window->RPort, safe);
   WBorderData0[3] = WBorderData0[5] =
   WBorderData1[3] = WBorderData1[5] = window->Height-1;
   WBorderData0[4] = WBorderData0[6] = window->Width -2;
   WBorderData1[4] = WBorderData1[6] = window->Width -1;
   WBox.Width = window->Width-2;
   WBox.Text  = name;
   DrawBorder(window->RPort, WBorder, 0,0);
   DrawBox   (window->RPort, &WBox,   0,0);
   SetAPen   (window->RPort, WHITE);
   SetBPen   (window->RPort, GREY);
   if (pattern) SetAfPt (window->RPort, pattern, 3);
   RectFill  (window->RPort, 2, TITLEBAR_HEIGHT, window->Width-3, window->Height-2);
   SetAfPt (window->RPort, NULL, 0);
   RefreshGList(window->FirstGadget, window, NULL, -1);
   SetAPen(window->RPort, GREY);
   SetBPen(window->RPort, WHITE);
   SetFont(window->RPort, font);
   CloseFont(safe);
}

struct Window *
OpenStdWindow(name, flags, x, y, sx, sy, pattern)
UBYTE    *name, flags;
USHORT   x, y, sx,sy, *pattern;
{
   USHORT   g;
   struct   Window   *window;
   struct   Gadget   *gadget;

   nw.LeftEdge   = x;
   nw.TopEdge    = y;
   nw.Width      = sx;
   nw.Height     = sy;
   nw.IDCMPFlags = StdIDCMP;
   nw.Screen     = IntuitionBase->FirstScreen;
   BindWindowToScreen(&nw);
   if (window = (struct Window *)OpenWindow(&nw))
      {
      for(g=0; g<MAX_GADGETS; g++)
         if (flags & (1 << g))
            {
            if (gadget = New(struct Gadget, 1))
               {
               *gadget = WGadget[g]; /* structure copy */
               AddGadget(window, gadget, 0);
               RefreshGList(gadget, window, NULL, 1);
               }
            }

      RemakeStdWindow(window, name, pattern);
      return(window);
      }
   return(NULL);
}
