/* Auto: make
*/

#define SCREENTOP\
   (screen->TopEdge << ((screen->ViewPort.Modes & LACE)? 0: 1))

IMPORT struct SnapRsrc *SnapRsrc;
IMPORT struct IntuitionBase *IntuitionBase;
IMPORT struct MinList CachedWindows;

struct Screen *WhichScreen()
{
    REGISTER struct Screen *screen;
    Forbid();
    screen = IntuitionBase->FirstScreen;
    while (screen && IntuitionBase->MouseY < SCREENTOP) {
        screen = screen->NextScreen;
    }
    if (screen == NULL) {     /* Shouldn't happen */
        screen = IntuitionBase->ActiveScreen;
    }
    Permit();
    return screen;
}

struct Window *WhichWindow(screen)
struct Screen *screen;
{
    struct Layer *layer;
    layer = (struct Layer *)WhichLayer(&screen->LayerInfo,
      (LONG)screen->MouseX, (LONG)screen->MouseY);
    if (layer) {
        return (struct Window *)layer->Window;
    } else {
        return NULL;
    }
}

VOID FreePlanes(bm, width, height)
struct BitMap *bm;
LONG width, height;
{
    SHORT i;
    for (i=0; i < bm->Depth; i++) {
        if (bm->Planes[i]) {
            FreeRaster(bm->Planes[i], width, height);
        }
    }
}

WORD AllocPlanes(bm, width, height)
struct BitMap *bm;
LONG width, height;
{
    WORD i;
    for (i=0; i < bm->Depth; i++) {
        bm->Planes[i] = NULL;
    }
    for (i=0; i < bm->Depth; i++) {
        if (!(bm->Planes[i] = AllocRaster(width, height))) {
            return 0;
        }
        BltClear(bm->Planes[i], RASSIZE(width, height), NULL);
    }
    return 1;
}

VOID CacheWindow(Win, xoff, yoff, fw, fh)
struct Window *Win;
LONG xoff;
LONG yoff;
SHORT fw;
SHORT fh;
{
    struct CacheWindow *cw;
    if (!(cw = AllocMem((LONG)sizeof(struct CacheWindow),
      MEMF_PUBLIC|MEMF_CLEAR))) {
        return;
    }
    cw->Window = Win;
    cw->xoff = xoff;
    cw->yoff = yoff;
    cw->fw = fw;
    cw->fh = fh;
    AddHead((struct List *)&CachedWindows, (struct Node *)cw);
    if (SnapRsrc->CacheSize > 0) {
        --SnapRsrc->CacheSize;
    } else {
        FreeMem(RemTail((struct List *)&CachedWindows),
          (LONG)sizeof(struct CacheWindow));
    }
}

struct CacheWindow *GetCachedWindow(Screen, Win)
struct Screen *Screen;
struct Window *Win;
{
    struct CacheWindow *cw = (struct CacheWindow *)CachedWindows.mlh_Head;

    while (cw->Node.mln_Succ) {
        if (cw->Window == Win) {
            LONG LockVal = LockIBase(0L);
            REGISTER struct Window *w = Screen->FirstWindow;
            while (w && w != Win) {
                w = w->NextWindow;
            }
            UnlockIBase(LockVal);
            if (!w) {
                return NULL;
            }
            Remove((struct Node *)cw);
            AddHead((struct List *)&CachedWindows, (struct Node *)cw);
            return cw;
        }
        cw = (struct CacheWindow *)cw->Node.mln_Succ;
    }
    return NULL;
}
