#include"wbios.h"	/* bios */
#include"lcdbios.h"	/* LCD drawing */
#include"rupsys.h"	/* rupsys */
#include"stdio.h"	/* rupsys */
#include"math.h"	/* rupsys */
#include"string.h"	/* rupsys */
#include"stdlib.h"	/* rupsys */
int counter;
char foreign[4];
char home[4];

float cur,tempcur,rate,transcur;
int counterlcd,xpos=1,first=1,ypos=1,xpix,ypix,xpixplus,len=0;
void countlcd(void)
{
  counterlcd++;
	if (counterlcd==20){
	  counterlcd--;
		lcdon(0);
	}else{
	  lcdon(1);
	}
}


paintlogo()
{
  char str[10],str2[10],buffer[20];
	gv_kput(2,31,"0",2,0,1);
	gv_kput(6,31,"0",2,0,1);
  gv_line( 0, 0,35, 0,0,0xFF);
	gv_line( 0,10,35,10,0,0xFF);
	gv_line( 0,20,35,20,0,0xFF);
	gv_line( 0,30,35,30,0,0xFF);
	gv_line( 0,40,35,40,0,0xFF);
	gv_line( 0,50,35,50,0,0xFF);
	gv_line( 0,60,35,60,0,0xFF);
	gv_line( 0, 0, 0,60,0,0xFF);
	gv_line(12, 0,12,40,0,0xFF);
	gv_line(24, 0,24,40,0,0xFF);
	gv_line(36, 0,36,60,0,0xFF);
	/*gv_line(1,1,32,1,0,0xFF);*/
	
	gv_kput(3,1,"7",2,0,0);
	gv_kput(15,1,"8",2,0,0);
	gv_kput(27,1,"9",2,0,0);
	gv_kput(3,11,"4",2,0,0);
	gv_kput(15,11,"5",2,0,0);
	gv_kput(27,11,"6",2,0,0);
	gv_kput(3,21,"1",2,0,0);
	gv_kput(15,21,"2",2,0,0);
	gv_kput(27,21,"3",2,0,0);
	gv_kput(60,56,"exit ->",2,0,0);
	gv_line(36,49,100,49,0,0xFF); 
	gv_line(36,50,100,50,0,0xF0);
	gv_line(36,51,100,51,0,0xFF);
	gv_line(36,19,100,19,0,0xFF);
	gv_line(36,20,100,20,0,0xF0);
	gv_line(36,21,100,21,0,0xFF);
	gv_kput(40, 0,"Currency",1,0,0);
	gv_kput(40, 9,"Converter",1,0,1);
	gv_kput(15,31,"0",2,0,0);
	gv_kput(27,31,"<",2,0,0);
	sprintf(buffer,"->%s",home);
	gv_kput(3,41,buffer,2,0,0);
  sprintf(buffer,"->%s",foreign);
	gv_kput(3,51,buffer,2,0,0);

	return 1;
}
wechsler()
{
  switch(xpos) {
	  case 1:
		  xpix=1;
		break;
		case 2:
		  xpix=13;
		break;
		case 3:
		  xpix=25;
		break;
		default:
	}
	switch(ypos) {
	  case 1:
		  ypix=1;
			xpixplus=10;
		break;
		case 2:
		  ypix=11;
			xpixplus=10;
		break;
		case 3:
		  ypix=21;
			xpixplus=10;
		break;
		case 4:
		  ypix=31;
			xpixplus=10;
		break;
		case 5:
		  ypix=41;
			xpix=1;
			xpixplus=34;
		break;
		case 6:
		  ypix=51;
			xpix=1;
			xpixplus=34;
		break;
		default:
	}
}

wait()
{
	int Key=0;
	char buffer[20];
	again:
	bi_clrbtn();
	Key=bi_getbtn();
	if (pSystemCtl->EventManager->event&EVENT_DBLCLICK) { Keyin(0,-1);}
	while( (!(Key&Bkey_B))&&(!(Key&Bkey_D))&&(!(Key&Bkey_dw))&&(!(Key&Bkey_up))&&(!(Key&Bkey_lf))&&(!(Key&Bkey_rg))){
		 Key=bi_getbtn();
		 bi_clrbtn();
  }
	if (counterlcd==19){
		counterlcd=0;
	  lcdon(1);
		goto again;
	}
	counterlcd=0;
	gv_reverse(xpix,ypix,xpix+xpixplus,ypix+8);
  if ((Key&Bkey_up)){if(ypos!=1){ypos--;}}
	if ((Key&Bkey_dw)){if(ypos!=6){ypos++;}}
	if ((Key&Bkey_lf)){if(xpos!=1){xpos--;}}
	if ((Key&Bkey_rg)){if(xpos!=3){xpos++;}}
	if (first==1){
	  gv_square(37,22,100,48,4,0x00);
		first=0;
	}
	if ((Key&Bkey_B)){
	  if (ypos<4){
		  tempcur=((((4-ypos)*3)-2)+(xpos-1));
	    cur=(cur*10)+tempcur/100;
			len=len+10;
	  }
		if (ypos==4&&xpos==3){
		  len=len-10;
			cur=(floor(cur*10)/100);
		}
		if (ypos==4&&xpos==2){
	    cur=(cur*10);
			len=len+10;
		}
		if (ypos==4&&xpos==1){
	    cur=(cur*100);
			len=len+20;
		}
		if (ypos==5){
	    transcur=rate*cur;
			sprintf(buffer,"%s->%s",foreign,home);
			gv_kput(40,31,buffer,2,0,0);
		}
		if (ypos==6){
	    transcur=(1/rate)*cur;
			sprintf(buffer,"%s->%s",home,foreign);
			gv_kput(40,31,buffer,2,0,0);
		}
		
	}
	wechsler();
	gv_reverse(xpix,ypix,xpix+xpixplus,ypix+8);
	
	sprintf(buffer,"%10.2f",cur);
	gv_kput(40,22,buffer,2,0,0);
	sprintf(buffer,"%10.2f",transcur);
	gv_kput(40,40,buffer,2,0,0);
	if (!(Key&Bkey_D)){goto again;}
	if ((Key&Bkey_D)){return 2;}else{return 1;}
}

int main(void)
{
	int *timeidlcd,filehdl;
	float dis;
	unsigned int rat[6];
  char buff[30],*buf;
 char temprat[5];
	filehdl = dos_fopen("rate.txt",0);
	dos_fread( filehdl, home,3     );
	dos_fread( filehdl, buf,2      );
	dos_fread( filehdl, foreign,3  );
	dos_fread( filehdl, buf,2      );
	dos_fread( filehdl, temprat,6  );
	dos_fclose(filehdl);
	rate=atof(temprat);
	
	/*sprintf(home,"%s","EUR");
	sprintf(foreign,"%s","TRL");*/
	screen(1);
	loop1:
	cls(4);
	sprintf(buff,"%5.6s",temprat);
	rate=atof(buff)*1;
	paintlogo();
	gv_kput(40, 20,"Welcome!",0,0,1);
	gv_kput(40, 32,"(c) 2002",2,0,1);
	gv_kput(40, 40,"Hanno Rein",1,0,1);
	wechsler();
	gv_reverse(xpix,ypix,xpix+xpixplus,ypix+8);
	counterlcd=0;
	timeidlcd = bi_tmset(0,255,1,countlcd);
	if (wait()!=2)
	{
 	 bi_tmdel(timeidlcd);
	 goto loop1;
	}
	bi_tmdel(timeidlcd);
  cls(4);

}


