/* 
    This module contain all of the routines that interface directly with 
    the display.

    Written by Steve (Raz) Berry.
*/

#include "flist.h"
#include <libraries/arpbase.h>
#include <stdio.h>

UWORD coltbl[8] = {
    0x047, /* background color */
    0xBBB, /* title text, inutuition boarders */
    0xB15, /* gadget, title bar */
    0x09B, /* Auto text */
    0x0a0, /* Auto text */
    0xC97, /* screen text */
    0xfff, /* gadget selected*/
    0x000  /* cursor */
};

static struct Image   cc_img;
static struct PropInfo   cc_prop = {AUTOKNOB | FREEVERT, 0,0, 0,0x1000,0,0,0,0,0,0 };
struct Gadget pg = { NL,RWDTH-28,20,15,170,      /* Scroll Bar   */
    GADGHCOMP,RELVERIFY | GADGIMMEDIATE | FOLLOWMOUSE, PROPGADGET,
    (APTR)&cc_img,NL,NL,NL,(APTR)&cc_prop,FGAD,NL };

struct NewScreen scr = {
    0,0,                /* x,y start */
    640,200,            /* width, height */
    2,                  /* depth */
    2,1,                /* detail, block pens */
    HIRES,              /* view mode */
    WBENCHSCREEN,       /* screen type */
    NULL,               /* font */
    (UBYTE *) "Flist V1.2 - By Steve (Raz) Berry",         /* title */
    NULL,               /* gadgets */
    NULL                /* bitmap pntr */
    };

struct NewWindow mywin = {
    0,0,                        /* x,y start */
    640,200,                    /* width, height */
    2,1,                        /* detail, block pens */
    RAWKEY | REFRESHWINDOW | GADGETDOWN | GADGETUP | MOUSEMOVE | 
                                /* IDCMP flags */
    MOUSEBUTTONS | MENUPICK | REQCLEAR | SELECTDOWN | SELECTUP,
    NORMALFLAGS,                /* window flags */
    NULL, NULL,                 /* pntr gadget, checkmark */
    (UBYTE *) "Flist",          /* title */
    NULL,                   /* screen pointer filled in later (run time) */
    NULL,                   /* bitmap pntr */
    0, 0, 8000, 8000,       /* default x,y, &size */
    CUSTOMSCREEN
    };

/* Globals */

extern long numfiles, remember;
extern long (*finfo[MAXDIR])[4];  /* miscelaneous info -- file size,blocks,dir type etc... */
extern char (*fname[MAXDIR])[FCHARS];     /* file name and comment */
extern char (*comm[MAXDIR])[FCHARS];
extern char (*fstring[MAXDIR])[LEN_DATSTRING];
extern char (*ftime[MAXDIR])[LEN_DATSTRING];
extern struct Image cursor_image;
extern long row,collum,line,first;
extern struct Window *winptr;
extern struct Screen *scrptr;

/* Globals defined here */

struct RastPort *rp;

/* Scrprt - print text to a screen (actually a rastport) */

scrprt(numb,top)
short numb,top;
{
    struct IntuiText itext;
    register long temp,i;

    if (top == remember )
        return;
        
    /* for FAST movements of the screen... use the hardware */
        
    if ((top-remember) == 1 || (top-remember) == -1)
        doBlit(top); 
    else{
        Move(rp,0,0);
        ClearScreen(rp); 
        if (numfiles != 0) {    /* Watch for empty directories! */
            if (line > numfiles)
                temp = numfiles;
            else
                temp = top;

            for(i=0;(i<numb)&&(i<NUMLINES);i++) {
                prtline(temp,i);
                if (temp < numfiles)
                        temp++;
            }
        }
    }
    RefreshGadgets(&pg,winptr,NULL);
    doborder();
    remember = top;
}

struct IntuiText itext = {
        TEXTCOLOR, 0,
        JAM2,
        0,0,                    /* x, y */
        NULL,                   /* font */
        NULL,                   /* &text */
        NULL,                   /* &next */
};

/* Print a line associated with a file to the screen */

/* **** This could be optimized in assembly for speed **** */

prtline(pline,i)
register long pline,i;
{
     char buffer[FCHARS];
     struct BitMap *bmptr;

     if (pline >= numfiles)
        return;
     
     bmptr = &scrptr->BitMap;

     itext.LeftEdge = 0;
     itext.TopEdge = i * LINEH; 
     strcpy(&buffer[0],fname[pline]);
     buffer[13] = 0;
     itext.IText = buffer; 
     PrintIText(rp,&itext,3L,14L); 
     itext.LeftEdge = 368; 
     itext.IText = fstring[pline];
     PrintIText(rp,&itext,3L,14L); 
     itext.LeftEdge = 456; 
     itext.IText = ftime[pline];
     PrintIText(rp,&itext,3L,14L); 

     if (*finfo[pline][0] < 0)
        sprintf(buffer,"%d         ",(*finfo[pline])[2]);
     else
        strcpy(buffer," Dir");

     itext.LeftEdge = 528; 
     buffer[7] = 0;
     itext.IText = buffer; 
     PrintIText(rp,&itext,3L,14L); 
}

doBlit(stop)
long stop;
{
        long inter,i;        
        struct BitMap *bmptr;
        
        inter = stop - remember;
        bmptr = &scrptr->BitMap;

        WaitTOF();
        if (inter > 0)
                ScrollRaster(rp,0L,LINEH,0L,(LONG)TOP+2,600L,189L);
/*                    BltBitMap(bmptr,0L,22L,bmptr,0L,12L,600L,170L,
                      (LONG)0x0c0,(LONG)0x0ff,tempblit); */
        else
                ScrollRaster(rp,0L,-(LINEH),0L,(LONG)TOP+2,600L,189L);
/*                    BltBitMap(bmptr,0L,12L,bmptr,0L,22L,600L,170L,
                    (LONG)0x0c0,(LONG)0x0ff,tempblit); */
        
        if (inter > 0)
                prtline(stop+NLMO,NLMO);
        else
                prtline(stop,0); 
}

/* draw the border around the console display */

doborder()
{
        SetAPen(rp,3);
        Move(rp,LEFT-2,TOP-1);
        Draw(rp,RIGHT,TOP-1);
        Draw(rp,RIGHT,BOT+1);
        Draw(rp,LEFT-2,BOT+1);
        Draw(rp,LEFT-2,TOP-1);
}

/* this function captures the abort from cntrl c */

_abort()
{
    int ret;
    ret = auto_req("User abort!!!");

    if(scrptr->FirstWindow->NextWindow != NULL) 
        auto_req("Kill all other Applications first!");
    else
        if (ret) {
            Cleanup();
            exit(1);
        }
    menu();     /* no abort, go to the menu again! */
}

openstuff()
{
    int i;
    struct ViewPort *vp;

    scrptr = OpenScreen(&scr);
    if (scrptr == NULL) {
        fprintf(stderr,"Couldn't open Screen!\n");
        FreeFlistMem();
        exit(30L);
    }

    mywin.FirstGadget = &pg;   /* prop gadget attached to the window */
    mywin.Screen = scrptr;
    winptr = OpenWindow(&mywin);
    if (winptr == NULL) {
        fprintf(stderr,"Couldn't open Window!\n");
        FreeFlistMem();
        CloseScreen(scrptr);
        exit(40L);
    }

    OpenConsole();
    
    rp = winptr->RPort;
    vp = &scrptr->ViewPort;

/*     LoadRGB4(vp,&coltbl,4L); */

    refresh();
}
