
#include "psm.h"

// ----------------------------------------------------------------
// Lock a GadOutline.  go_TransHookData must be free to use.
// ----------------------------------------------------------------

/* Pointer from Steve Tibbett's PointerX */

static USHORT __chip BusyPointerData[] =
    {
    0x0000,0x0000,
    0x0400,0x07C0,0x0000,0x07C0,0x0100,0x0380,0x0000,0x07E0,
    0x07C0,0x1FF8,0x1FF0,0x3FEC,0x3FF8,0x7FDE,0x3FF8,0x7FBE,
    0x7FFC,0xFF7F,0x7EFC,0xFFFF,0x7FFC,0xFFFF,0x3FF8,0x7FFE,
    0x3FF8,0x7FFE,0x1FF0,0x3FFC,0x07C0,0x1FF8,0x0000,0x07E0,
    0x0000,0x0000,
    };

void LockGadOutline(struct GadOutline *go)
{
    if(!go || go->go_TransHookData != NULL || !go->go_Window) return;
    
    SetPointer(go->go_Window,BusyPointerData,16,16,-6,0);
    if( (go->go_TransHookData=AllocVec(sizeof(struct Requester),
        MEMF_PUBLIC|MEMF_CLEAR)) != NULL ) {

        InitRequester(go->go_TransHookData);
        if(!Request(go->go_TransHookData,go->go_Window)) {
            FreeVec(go->go_TransHookData);
            go->go_TransHookData = NULL;
        }
    }
}

void UnlockGadOutline(struct GadOutline *go)
{
    if(!go || !go->go_TransHookData || !go->go_Window) return;
    
    EndRequest(go->go_TransHookData,go->go_Window);
    FreeVec(go->go_TransHookData);
    go->go_TransHookData = NULL;
    ClearPointer(go->go_Window);
}


// ----------------------------------------------------------------
// Window backfill support.
// ----------------------------------------------------------------

struct BackFillMsg
{
    struct Layer     *bf_Layer;
    struct Rectangle  bf_Bounds;
    LONG              bf_OffsetX;
    LONG              bf_OffsetY;
};


static ULONG __asm __saveds __interrupt
backfill_code(register __a0 struct Hook        *hook,
              register __a2 struct RastPort    *rp,
              register __a1 struct BackFillMsg *bfm)
{
    struct RastPort crp;
    struct GadOutline *go;
    UWORD pen = 0;

    go = hook->h_Data;
    if(go) {
        if(go->go_DrI) {
            if(go->go_DrI->dri_Version >= 1) {
                if(go->go_DrI->dri_NumPens >= BACKGROUNDPEN) {

                    pen = go->go_DrI->dri_Pens[BACKGROUNDPEN];
                }
            }
        }
    }
    
    crp       = *rp;            /* copy the rastport                      */
    crp.Layer = NULL;           /* eliminate bogus clipping from our copy */

    SetWrMsk(&crp,0xff);

    SetAPen(&crp,pen);                   /* set the pen to background color */
    SetDrMd(&crp,JAM2);                  /* set the rendering mode we need  */
    RectFill(&crp,bfm->bf_Bounds.MinX,   /* clear the whole area            */
                  bfm->bf_Bounds.MinY,
                  bfm->bf_Bounds.MaxX,
                  bfm->bf_Bounds.MaxY);
   return 0;
}

void SetupBackFillHook(struct Hook *hk,struct GadOutline *go)
{
    hk->h_Entry = (ULONG (*)())backfill_code;
    hk->h_SubEntry = NULL;
    hk->h_Data = go;
}
