 /*
  * UAE - The Un*x Amiga Emulator
  *
  * DosUAE Interface - 07-17-96 02:42pm
  *
  * (c) 1995 Bernd Schmidt, Ed Hanway
  *          Interface by Tim Gunn
  */

#include "sysconfig.h"
#include "sysdeps.h"

#include <stdio.h>
#include <ctype.h>
#include <dpmi.h>
#include <dos.h>
#include <conio.h>
#include <grx20.h>

#include "config.h"
#include "options.h"
#include "autoconf.h"
#include "dos-ui.h"
#include "gui.h"
#include "memory.h"

#define TB textbackground
#define TC textcolor
#define CURSON _setcursortype(_NORMALCURSOR);
#define CURSOFF _setcursortype(_NOCURSOR);
#define SCRON _wscroll = 1;
#define SCROFF _wscroll = 0;

int SETTC=15;
int SETTB=1;
int _CWINX = 1,_CWINY = 1,_CWINX2 = 80,_CWINY2 = 25;
int dx,dy,dx2,dy2,dlx,dly,dlx2,dly2;
char vbuff[4096];
struct find_t sfile;

char *tui_filereq(char *spec)
{
    struct find_t *files;
    struct find_t ffblk;

    int i,done,key=0,zx,zy,zx2,zy2;
    int ysize=8,cur=0,xpos=10,ypos,y=0,z,count=0;
    char buff2[4096];

    int speccount=0,sc2=0,speclen;
    char specbuff[12];
    specbuff[0]=0;
    speclen = strlen(spec);

    done = _dos_findfirst("*.*",_A_NORMAL,&ffblk);
    while(!done) { count++; done = _dos_findnext(&ffblk); }
    files = (struct find_t *)calloc(count , sizeof(struct find_t));

    count=1; // starts at 1 because the first entry is 'none'.
    while(speccount<=speclen) {

        if(spec[speccount]!=' ' || speccount!=speclen)
        {
            specbuff[sc2]=spec[speccount];
        }
        else
        {
            specbuff[sc2]=0; sc2=-1; //sc2=-1 so the count makes it 0;
            done = _dos_findfirst(specbuff,_A_NORMAL,&ffblk);
            files[count]=ffblk;
            while(!done) {
                count++;
                done = _dos_findnext(&ffblk);
                files[count]=ffblk;
            }
            specbuff[0]=0;
        }
        speccount++; sc2++;

    }
    specbuff[sc2]=0; //repeated coz of no space, bad code rewrite it!
    done = _dos_findfirst(specbuff,_A_NORMAL,&ffblk);
    files[count]=ffblk;
    while(!done) {
        count++;
        done = _dos_findnext(&ffblk);
        files[count]=ffblk;
    }
    strcpy(files[0].name,"none");


    if(count<9) { ysize=count; }
    if(ysize>15) { ysize=15; }
    ypos=12-(ysize/2);

    zx=xpos; zy=ypos; zx2=xpos+20; zy2=ypos+ysize+1;
    gettext(zx-1,zy-1,zx2+1,zy2+1,buff2);
    window(1,1,80,25); drawbox(zx-1,zy-1,zx2+1,zy2+1);
    gotoxy(zx+1,zy-1); cprintf("Select a file.");
    window(zx,zy,zx2,zy2); clrscr();

    gotoxy(1,ysize+1); cprintf("-------------");
    while(key!=13) {
	switch(key) {
	 case 72: cur--; break;
	 case 80: cur++; break;
	 case 27:
	    TB(SETTB); TC(SETTC);
	    window(_CWINX,_CWINY,_CWINX2,_CWINY2);
	    puttext(zx-1,zy-1,zx2+1,zy2+1,buff2);
	    sfile.name[0]=0;
	    return sfile.name;
        }
        if(count-y<ysize) z=count; else z=ysize;
	if(cur>z-1) { cur=z-1; y++; }
	if(cur<0) { cur=0; y--; }
	if(y<0) y=0;
	if(y>count-z) y=count-z;
	for(i=y;i<y+z;i++) {
            if(i==y+cur) { TB(SETTC); TC(SETTB); }
	    else { TB(SETTB); TC(SETTC); }
	    gotoxy(1,i-y+1);
	    cprintf("            ");
	    gotoxy(1,i-y+1);
	    cputs(files[i].name);
	}
        gotoxy(1,ysize+2);
	cprintf("             ");
	gotoxy(1,ysize+2);
	cprintf("%s",files[y+cur].name);

	key = getch();
    }

    TB(SETTB); TC(SETTC);

    if(y+cur!=0) {
        sfile=files[y+cur];
    } else {
        sfile.name[0]=0;
    }

    window(_CWINX,_CWINY,_CWINX2,_CWINY2);
    puttext(zx-1,zy-1,zx2+1,zy2+1,buff2);

    return sfile.name;
}

void tui_dlog(int xcoord,int ycoord,int xcoord2,int ycoord2) 
{
    dx = xcoord; dy = ycoord; dx2 = xcoord2; dy2 = ycoord2;
    gettext(dx-1,dy-1,dx2+1,dy2+1,vbuff);
    window(1,1,80,25); drawbox(dx-1,dy-1,dx2+1,dy2+1);
    window(dx,dy,dx2,dy2);
    dlx = _CWINX; dly = _CWINY; dlx2 = _CWINX2; dly2 = _CWINY2;
    _CWINX = dx; _CWINY = dy; _CWINX2 = dx2 ;_CWINY2 = dy2;
    clrscr();
}

void tui_dlogdie() 
{
    window(dlx,dly,dlx2,dly2);
    _CWINX = dlx; _CWINY = dly; _CWINX2 = dlx2; _CWINY2 = dly2;
    puttext(dx-1,dy-1,dx2+1,dy2+1,vbuff);
}

void tui_drawbox(int x, int y, int x2, int y2) 
{
    int i;
    gotoxy(x,y); cprintf("É");
    for(i=0;i<x2-x-1;i++){ cprintf("Í"); }
    cprintf("»");
    gotoxy(x,y2); cprintf("È");
    for(i=0;i<x2-x-1;i++){ cprintf("Í"); }
    cprintf("¼");
    for(i=y+1;i<y2;i++) {
        gotoxy(x,i); cprintf("º");
        gotoxy(x2,i); cprintf("º");
    }
}

void tui_errorbox(char *errortxt) 
{
    int half,zx,zy,zx2,zy2;
    char buff[2048];
    half = strlen(errortxt)/2;

    TC(SETTC); TB(SETTB);

    zx=35-half; zy=10; zx2=45+half; zy2=15;

    gettext(zx-1,zy-1,zx2+1,zy2+1,buff);
    window(1,1,80,25); drawbox(zx-1,zy-1,zx2+1,zy2+1);
    window(zx,zy,zx2,zy2); clrscr();

    gotoxy(5,3); cprintf(errortxt);
    gotoxy(half+1,5); cprintf("HIT A KEY");
    getch();

    window(_CWINX,_CWINY,_CWINX2,_CWINY2);
    puttext(zx-1,zy-1,zx2+1,zy2+1,buff);
}
