/************************************************************************

vJet V1.0 © 1992 Jan Pfeil · fileviewer for AMIGA
Last changes:   15.04.92

vJet requester routines

************************************************************************/

#include <dos.h>
#include <stdio.h>
#include <string.h>

#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include <libraries/asl.h>

#include <proto/asl.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/intuition.h>

#include "extern.h"

struct AslBase *AslBase = NULL;

struct EasyStruct openFail = {
    sizeof(struct EasyStruct),0,
    "Open Request ",                /*0xA0 at end of string for arq*/
    "Couldn't open\n%s",
    "Retry|Cancel"
    };

struct EasyStruct About = {
    sizeof(struct EasyStruct),0,
    "About...",
    "vJet V1.0\n© 1992 Jan Pfeil\nfreely distributable\nfile viewing utility",
    "OK"
    };


BOOL arqEasyRequest(struct Window *w,struct EasyStruct es,WORD id,ULONG *ip,APTR arg1,...)
{
struct ExtEasyStruct eir;
BOOL ret = FALSE;
eir.Image = NULL;
eir.Sound = NULL;
eir.AnimID = id;
eir.Flags = ARQ_ID_ANIM;
eir.Magic = ARQ_MAGIC;
eir.Reserved[0] = NULL;
eir.Reserved[1] = NULL;
eir.Reserved[2] = NULL;
eir.Easy = es;
ret = EasyRequestArgs(w,&eir.Easy,ip,&arg1);
return(ret);
}

/****************************************************************
definition: fileReq

win         -   launched from window
hail        -   requester title
fileName    -   buffer must be allocated by user
                input : current path or file name
                output: file path
mode        -   FREQ_ALL  all files
                FREQ_TEXT all but #?.info
                FREQ_CH   only #?.c or #?.h files
                FREQ_ASM  only #?.a or #?.asm or #?.s files
*****************************************************************/
BOOL fileReq(struct vJet *vjet,struct Window *win,char *hail,char *fileName,UWORD mode)
{
struct FileRequester *fR = NULL;
BOOL res = FALSE;
char dir[FMSIZE-1]="",file[FMSIZE-1]="",pat[FMSIZE-1];


if(!(AslBase = (APTR)OpenLibrary("asl.library",ASL_VER)))
    return(ERROR_INVALID_RESIDENT_LIBRARY);

if(fileName[0] == '\0')             /*no path specified*/
    res = GetCurrentDirName(dir,FMSIZE-1);         /*get current directory*/
else {
    res = AddPart(dir,fileName,FMSIZE-1);
    strcpy(file,FilePart(dir));
    strcpy(PathPart(dir),"");
    }

switch(mode) {
    case FREQ_ALL:               /*include all pattern*/
                    strcpy(pat,"#?");
                    break;
    case FREQ_TEXT:              /*exclude icons*/
                    strcpy(pat,"~(#?.info)");
                    break;
    case FREQ_CH:                /*display #?.c or #?.h files */
                    strcpy(pat,"(#?.c|#?.h)");
                    break;
    case FREQ_ASM:               /*display assembler code files */
                    strcpy(pat,"(#?.a|#?.asm|#?.s");
                    break;
    default:        strcpy(pat,"");         //security
                    break;
    }

fR = AllocAslRequestTags(ASL_FileRequest,
            ASL_Hail,hail,
            ASL_Window,win,
            ASL_LeftEdge,win->LeftEdge+40,
            ASL_TopEdge,win->TopEdge+20,
            ASL_Width,400,
            ASL_Height,max(200,vjet->win->Height*3/4),
            ASL_File,file,
            ASL_Dir,dir,
            ASL_OKText,"Load",
            ASL_FuncFlags,FILF_PATGAD,
            ASL_ExtFlags1,0,
            ASL_Pattern,pat,
            TAG_END);

if(fR) {
    res = RequestFile(fR);
    if(res) {                         /*OK selected*/
        strcpy(fileName,fR->rf_Dir);
        AddPart(fileName,fR->rf_File,FMSIZE-1);
        }
    FreeFileRequest(fR);
    }
else {
    SetWindowTitles(vjet->win,"Couldn't open file requester",(UBYTE *)-1);
    res = FALSE;
    }

if(AslBase) CloseLibrary((struct Library *)AslBase);
return(res);
}
/*end of fileReq()*/



VOID about(struct vJet *vjet)
{
LONG chipmem,fastmem,totalmem;
chipmem = AvailMem(MEMF_CHIP)/1024;
fastmem = AvailMem(MEMF_FAST)/1024;
totalmem = AvailMem(MEMF_PUBLIC)/1024;
arqEasyRequest(vjet->win,About,ARQ_ID_INFO,NULL,NULL);
}

/*end of vFR.c*/