/* Auto: make
*/

IMPORT struct ArpBase *ArpBase;

struct Window *ControlWindow = NULL;
IMPORT struct MsgPort *Sharedport;
IMPORT WORD Sharedrefs;

IMPORT LONG TitleBarHeight;
IMPORT LONG ContentsFontHeight;
IMPORT struct RastPort MyRP;

#define IDCMPFLAGS (MOUSEMOVE|NEWSIZE|CLOSEWINDOW|GADGETUP|ACTIVEWINDOW|INACTIVEWINDOW|MOUSEBUTTONS)

UBYTE *WindowTitle = (UBYTE *)"Snap (c) 1989 Mikael Karlsson";

struct Image DiskImage = {
    0, -1,
    16, 8, 1,
    NULL,
    0x01, 0x00,
    NULL
};

struct Gadget DiskGad = {
    NULL,
    0, 1,
    16, 8,
    GADGIMAGE|GADGHCOMP|GRELRIGHT,
    RELVERIFY,
    BOOLGADGET,
    (APTR)&DiskImage,
    NULL,
    NULL,
    0L,
    NULL,
    DISKGAD,
    NULL,
};

struct Gadget VProp = {
    NULL,
    -15, 10,
    16, -10,
    GADGHCOMP|GRELRIGHT|GRELHEIGHT,
    GADGIMMEDIATE|FOLLOWMOUSE|RELVERIFY,
    PROPGADGET,
    NULL,
    NULL,
    NULL,
    0L,
    NULL,
    VPROP,
    NULL
};

struct Gadget HProp = {
    NULL,
    1, -8,
    -15, 9,
    GADGHCOMP|GRELBOTTOM|GRELWIDTH,
    GADGIMMEDIATE|FOLLOWMOUSE|RELVERIFY,
    PROPGADGET,
    NULL,
    NULL,
    NULL,
    0L,
    NULL,
    HPROP,
    NULL
};

struct PropInfo VInfo = {
    AUTOKNOB|FREEVERT,
    0, 0,
    MAXBODY, MAXBODY,
    0, 0, 0, 0, 0, 0
};

struct PropInfo HInfo = {
    AUTOKNOB|FREEHORIZ,
    0, 0,
    MAXBODY, MAXBODY,
    0, 0, 0, 0, 0, 0
};

struct Image VImage = {
    0, 0, 0, 0, 0, NULL, 0, 0, NULL
};
struct Image HImage = {
    0, 0, 0, 0, 0, NULL, 0, 0, NULL
};

/* Window structure for snapped gfx */
struct NewWindow Nw = {
    0, 1,             /* LeftEdge, TopEdge  */
    0, 0,             /* Width, Height */
    -1, -1,           /* DetailPen, BlockPen */
    NULL,             /* IDCMPFlags */
    WINDOWDRAG|WINDOWDEPTH|WINDOWCLOSE|WINDOWSIZING|SMART_REFRESH|NOCAREREFRESH|RMBTRAP,
    NULL, NULL,       /* FirstGadget, CheckMark */
    NULL,
    NULL, NULL,       /* Screen, BitMap */
    32, 32,           /* MinWidth, MinHeight */
    0, 0,           /* MaxWidth, MaxHeight */
    WBENCHSCREEN      /* Type */
};

SHORT Coords3[5][2] = {
    {  0,  0},
    { 74,  0},
    { 74, 10},
    {  0, 10},
    {  0,  0}
};

SHORT Coords2[5][2] = {
    {  0,  0},
    { 74,  0},
    { 74, 10},
    {  0, 10},
    {  0,  0}
};

SHORT Coords1[5][2] = {
    {  0,  0},
    { 34,  0},
    { 34, 10},
    {  0, 10},
    {  0,  0}
};

struct Border Border3 = {
    42, 0,
    1, 0,
    JAM1,
    5,
    (SHORT *)&Coords3,
    NULL
};

struct Border Border2 = {
    42, 0,
    1, 0,
    JAM1,
    5,
    (SHORT *)&Coords2,
    &Border3
};

struct Border Border1 = {
    4, 0,
    1, 0,
    JAM1,
    5,
    (SHORT *)&Coords1,
    &Border2
};

UBYTE TranspBuf[5];

struct StringInfo TranspSI = {
    TranspBuf,  /* Buf */
    NULL,       /* UndoBuf */
    0,          /* BufferPos */
    4,          /* MaxChars */
    0,          /* DispPos */
    0,          /* UndoPos */
    0,          /* NumChars */
    0,          /* DispCount */
    0, 0,       /* CLeft, CTop */
    NULL,       /* Layer */
    0L,         /* LongInt */
    NULL        /* AltKeyMap */
};

struct Gadget TranspGad = {
    NULL,
    5, 16,
    32, 8,
    LONGINT,
    RELVERIFY,
    STRGADGET,
    NULL,
    NULL,
    NULL,
    0L,
    (APTR)&TranspSI,
    0,
    NULL,
};

UBYTE SaveName[256] = "";
UBYTE SaveDirName[256] = "";

struct StringInfo NameSI = {
    SaveName,   /* Buf */
    NULL,       /* UndoBuf */
    0,          /* BufferPos */
    255,        /* MaxChars */
    0,          /* DispPos */
    0,          /* UndoPos */
    0,          /* NumChars */
    0,          /* DispCount */
    0, 0,       /* CLeft, CTop */
    NULL,       /* Layer */
    0L,         /* LongInt */
    NULL        /* AltKeyMap */
};

struct Gadget NameGad = {
    &TranspGad,
    5, 16,
    32, 8,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    0L,
    NULL,
    NAMEGAD,
    NULL,
};

struct Gadget SaveGad = {
    &NameGad,
    5, 16,
    32, 8,
    GADGHCOMP,
    RELVERIFY,
    BOOLGADGET,
    NULL,
    NULL,
    NULL,
    0L,
    NULL,
    SAVEGAD,
    NULL,
};

/* Window structure for control window */
struct NewWindow NewCW = {
  10, 10,           /* LeftEdge, TopEdge  */
  320, 40,          /* Width, Height */
  -1, -1,           /* DetailPen, BlockPen */
  NULL,             /* IDCMPFlags */
  WINDOWDRAG|WINDOWDEPTH|WINDOWCLOSE|SMART_REFRESH|ACTIVATE|NOCAREREFRESH,
  NULL, NULL,       /* FirstGadget, CheckMark */
  (UBYTE *)"Snap Control Window",
  NULL, NULL,       /* Screen, BitMap */
  32, 32,           /* MinWidth, MinHeight */
  -1, -1,           /* MaxWidth, MaxHeight */
  WBENCHSCREEN      /* Type */
};

struct FileRequester NameFR = {
    "Save picture as...",
    (BYTE *)SaveName,
    (BYTE *)SaveDirName,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL
};

struct Window *opensharedwindow(nw)
struct NewWindow *nw;
{
    struct Window *win;
    struct Screen scr;

    if (Sharedport) {
        nw->IDCMPFlags = NULL;
    } else {
        nw->IDCMPFlags = IDCMPFLAGS;
    }
    if (!GetScreenData((char *)&scr, (LONG)sizeof(struct Screen),
      WBENCHSCREEN, NULL)) {
        return NULL;     /* No WB */
    }
    if (nw->TopEdge+nw->Height > scr.Height) {
        nw->TopEdge = scr.Height-nw->Height;
    }
    if (nw->LeftEdge+nw->Width > scr.Width) {
        nw->LeftEdge = scr.Width-nw->Width;
    }
    if (nw->TopEdge < 0) {
        nw->TopEdge = 0;
    }
    if (nw->LeftEdge < 0) {
        nw->LeftEdge = 0;
    }
    if (nw->TopEdge+nw->Height > scr.Height) {
        nw->Height = scr.Height-nw->TopEdge;
    }
    if (nw->LeftEdge+nw->Width > scr.Width) {
        nw->Width = scr.Width-nw->LeftEdge;
    }

    if (win = OpenWindow(nw)) {
        if (Sharedport) {
            win->UserPort = Sharedport;
            ModifyIDCMP(win, IDCMPFLAGS);
        } else {
            Sharedport = win->UserPort;
        }
        ++Sharedrefs;
    }
    return(win);
}


VOID closesharedwindow(win)
struct Window *win;
{
    Forbid();
	Sharedrefs--;
	if (Sharedrefs > 0) {
	    REGISTER struct IntuiMessage *im, *succ;
        im = (struct IntuiMessage *)win->UserPort->mp_MsgList.lh_Head;
        while (succ = (struct IntuiMessage *)im->ExecMessage.mn_Node.ln_Succ) {
            if (im->IDCMPWindow == win) {
                Remove(im);
                ReplyMsg(im);
            }
            im = succ;
        }
	    win->UserPort = NULL;
	    ModifyIDCMP(win, MENUVERIFY);     /* NEVER occurs */
	} else
		Sharedport = NULL;
    Permit();
    CloseWindow(win);
}

VOID SetUpBorder(Border, Gad)
struct Border *Border;
struct Gadget *Gad;
{
    Border->LeftEdge = Gad->LeftEdge-1;
    Border->TopEdge = Gad->TopEdge-1;
    Border->XY[2] = Border->XY[4] = Gad->Width+1;
    Border->XY[5] = Border->XY[7] = Gad->Height+1;
}

VOID AdjustSize(GS)
struct GfxSnap *GS;
{
    ULONG w = GS->window->Width - 18;
    ULONG h = GS->window->Height - GS->VProp.TopEdge - 9;
    ULONG vb, hb;

    vb = hb = MAXBODY;

    if (w < GS->width) {
        hb = w * (ULONG)MAXBODY / GS->width;
    }
    if (h < GS->height) {
        vb = h * (ULONG)MAXBODY / GS->height;
    }
    NewModifyProp(&GS->VProp, GS->window, NULL, (ULONG)AUTOKNOB|FREEVERT,
      0L, 0L, (ULONG)MAXBODY, vb, 1L);
    NewModifyProp(&GS->HProp, GS->window, NULL, (ULONG)AUTOKNOB|FREEHORIZ,
      0L, 0L, hb, (ULONG)MAXBODY, 1L);
    InitRastPort(&MyRP);
    MyRP.BitMap = &GS->BM;
    ClipBlit(&MyRP, 0L, 0L, GS->window->RPort,
      2L, (LONG)GS->VProp.TopEdge, w, h, 0xC0L);
}

VOID SyncGS(GS)
struct GfxSnap *GS;
{
    ULONG w = GS->window->Width - 18;
    ULONG h = GS->window->Height - GS->VProp.TopEdge - 9;
    ULONG vpos, hpos;
    LONG temp;

    temp = GS->width - w;
    if (temp <= 0) {
        hpos = 0;
    } else {
        hpos = temp * (LONG)GS->HInfo.HorizPot / MAXBODY;
        if (hpos > temp) {
            hpos = temp;
        }
    }
    temp = GS->height - h;
    if (temp <= 0) {
        vpos = 0;
    } else {
        vpos = temp * (LONG)GS->VInfo.VertPot / MAXBODY;
        if (vpos > temp) {
            vpos = temp;
        }
    }
    InitRastPort(&MyRP);
    MyRP.BitMap = &GS->BM;
    ClipBlit(&MyRP, hpos, vpos, GS->window->RPort,
      2L, (LONG)GS->VProp.TopEdge, w, h, 0xC0L);
}

SHORT OpenCW()
{
    WORD temp;

    FixHeights();

    NewCW.Height = TitleBarHeight + ContentsFontHeight * 2 + 12;
    ControlWindow = opensharedwindow(&NewCW);
    if (!ControlWindow) {
        return 0;
    }
    SaveGad.TopEdge = NameGad.TopEdge = TitleBarHeight + 2;
    SaveGad.Height = NameGad.Height = TranspGad.Height = ContentsFontHeight+2;
    TranspGad.TopEdge = SaveGad.TopEdge + SaveGad.Height + 3;

    SaveGad.LeftEdge = 4L;
    SaveGad.Width = TextLength(ControlWindow->RPort, "Save", 4L) + 1;
    if (Sharedrefs == 1) {       /* No snap window, just control window? */
        SaveGad.Flags |= GADGDISABLED;
    } else {
        SaveGad.Flags &= ~GADGDISABLED;
    }

    NameGad.LeftEdge = SaveGad.LeftEdge +
      TextLength(ControlWindow->RPort, "Save as ", 8L);
    temp = ControlWindow->Width - 4 - NameGad.LeftEdge;
    NameGad.Width =
      temp - (temp % TextLength(ControlWindow->RPort, " ", 1L)) + 1;

    TranspGad.LeftEdge = SaveGad.LeftEdge +
      TextLength(ControlWindow->RPort, "Transparent color ", 18L);
    TranspGad.Width = TextLength(ControlWindow->RPort, "0000", 4L);

    SetUpBorder(&Border1, &SaveGad);
    SetUpBorder(&Border2, &NameGad);
    SetUpBorder(&Border3, &TranspGad);

    ++TranspGad.LeftEdge;
    ++TranspGad.TopEdge;

    if (ArpBase) {
        NameGad.Flags = GADGHCOMP;
        NameGad.Activation = RELVERIFY;
        NameGad.GadgetType = BOOLGADGET;
        NameGad.SpecialInfo = NULL;
    } else {
        NameGad.Flags = SELECTED;
        NameGad.Activation = NULL;
        NameGad.GadgetType = STRGADGET;
        NameGad.SpecialInfo = (APTR)&NameSI;
        ++NameGad.LeftEdge;         /* Adjust string gadget position */
        ++NameGad.TopEdge;          /* Adjust string gadget position */
    }

    strcpy(SaveName, "Snap.pic");
    strcpy(TranspBuf, "0");
    TranspSI.LongInt = 0L;

    SetAPen(ControlWindow->RPort, 1L);
    SetBPen(ControlWindow->RPort, 0L);
    SetDrMd(ControlWindow->RPort, JAM2);
    Move(ControlWindow->RPort,
      5L, (LONG)SaveGad.TopEdge + 1 + ControlWindow->RPort->Font->tf_Baseline);
    Text(ControlWindow->RPort, "Save as", 7L);
    Move(ControlWindow->RPort,
      5L, (LONG)TranspGad.TopEdge + ControlWindow->RPort->Font->tf_Baseline);
    Text(ControlWindow->RPort, "Transparent color", 17L);
    DrawBorder(ControlWindow->RPort, &Border1, 0L, 0L);
    AddGList(ControlWindow, &SaveGad, -1L, 3L, NULL);
    RefreshGList(&SaveGad, ControlWindow, NULL, 3L);
    if (ArpBase) {
        GadText(&NameGad, SaveName, 8L);
    } else {
        ActivateGadget(&NameGad, ControlWindow, NULL);
    }
    return 1;
}
