#include<bios.h>
#include<dos.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>

#define DIBUJA       1
#define DEPRIME      2
#define Dibuja_Todos 3
#define COMPRUEBA    4
#define STD_BY       5

void Edit_Item(void);
void espera_soltar(void);
void espera_pulsar(void);
void activa_raton(void);
void desactiva_raton(void);
void inicializa_raton_texto(void);
void Pantalla_Leer(void);
void Pantalla_Escribir(void);
void dibuja_pantalla(void);
struct Boton_Texto{
  char x; char y;
  char Texto[80];

  int ID_T1;
  int ID_T2;
};

int Funcion_Botones(int Funcion, int nBotones, struct Boton_Texto *Boton_Texto);

  struct byte_de_long { unsigned char P3;	unsigned char P2;
			unsigned char P1;	unsigned char P0;
  };
  union lngchr{  long Numero;  struct byte_de_long Byte; } Division;
	//////////////////////////////
	//  long -->  FF FF FF FF	-->  3  34  65  59
	//            P0 P1 P2 P3       Dec.89 101  52   3
	//////////////////////////////  Hex.59  65  34   3
int   boton_izq; int   boton_der;
int x_raton, y_raton, RATON = 1;
int SWAP = 0;
int Dir, Dato;

void main(void){

struct Boton_Texto BT[6];
int devolver = 0, i;

BT[0].x = 58; BT[0].y = 3; strcpy( BT[0].Texto, "  P. &Leer           "); BT[0].ID_T1 = 76; BT[0].ID_T2 = 0;
BT[1].x = 58; BT[1].y = 6; strcpy( BT[1].Texto, "  Iniciar Lec&tura   "); BT[1].ID_T1 = 84; BT[1].ID_T2 = 0;

BT[2].x = 58; BT[2].y = 9; strcpy( BT[2].Texto, "  P. &Escribir       "); BT[2].ID_T1 = 69; BT[2].ID_T2 = 0;
BT[3].x = 58; BT[3].y =12; strcpy( BT[3].Texto, "  Iniciar E&scritura "); BT[3].ID_T1 = 83; BT[3].ID_T2 = 0;

BT[4].x = 58; BT[4].y =15; strcpy( BT[4].Texto, "   &C R E D I T O S  "); BT[4].ID_T1 = 67; BT[4].ID_T2 = 0;
BT[5].x = 58; BT[5].y =18; strcpy( BT[5].Texto, "  S&ALIR de PDE ...  "); BT[5].ID_T1 = 65; BT[5].ID_T2 = 0;

 inicializa_raton_texto();
 if( RATON ) desactiva_raton();
 dibuja_pantalla();
 _setcursortype(_NOCURSOR);
 Pantalla_Leer();
// while( !kbhit() );
// Pantalla_Escribir();

while(devolver != 6) {
  devolver = Funcion_Botones(STD_BY, 6, BT);
  switch(devolver){
   case 1: Pantalla_Leer(); SWAP = 0; break;
   case 2:
     Pantalla_Leer(); SWAP = 0;
    _setcursortype(_NORMALCURSOR);
    		Edit_Item();
                 _setcursortype(_NOCURSOR);

		break;
   case 3: Pantalla_Escribir(); SWAP = 1; break;
   case 4:
     Pantalla_Escribir(); SWAP = 1;
    _setcursortype(_NORMALCURSOR);
    		Edit_Item();
                 _setcursortype(_NOCURSOR);

		break;
   case 6: break;
  default:
		while( kbhit() ) getch();
		break;
  }

}

  textbackground(BLACK);
 _setcursortype(_NORMALCURSOR);
clrscr();

}


void dibuja_pantalla(void){

 int i;
 clrscr();
 highvideo();

 window(1, 1, 80, 25);
 textcolor(9);
 textbackground(7);
 for(i=0; i<27; i++)                                   //		"-.-.-.-.-.-.-.-.-.-.-.-."
	cprintf("��������������������������������������������������������                        ");

 textcolor(BLACK);
 gotoxy(1,25);
 cprintf("Utilidad para "); textcolor(RED);
 cprintf("PDE _ EPROM2.EXE");  textcolor(BLACK);
 gotoxy( (80-22), 25); cprintf("Jos� David & Francisco");

}

void Pantalla_Leer(void){
 highvideo();
 textcolor(BLACK);
 textbackground(7);
 gotoxy(3,3);  cprintf("�");
 gotoxy(17,3);	     	     cprintf("�");
 gotoxy(3,4);  cprintf("� Leer     ");
 textcolor(WHITE); cprintf("�"); textcolor(BLACK);
 gotoxy(17,4);	     	     cprintf("� Escribir ");
 textcolor(WHITE); cprintf("�"); textcolor(BLACK);
 gotoxy(3,5);  cprintf("�          �� ����������������������������Ŀ");
 gotoxy(3,6);  cprintf("�                                          �");
 gotoxy(3,7);  cprintf("� Direcci�n:                               �");
 gotoxy(3,8);  cprintf("�                                          �");
 gotoxy(3,9);  cprintf("�                                          �");
 gotoxy(3,10);  cprintf("�                                          �");
 gotoxy(3,11);  cprintf("�                                          �");
	       textcolor(RED);
 gotoxy(5,11);  cprintf("Dato Leido:");
	       textcolor(BLACK);
 gotoxy(3,12);  cprintf("�                                          �");
 gotoxy(3,13);  cprintf("�������������������������������������������");

 textcolor(WHITE);
 gotoxy(4,3);   cprintf("�����������");
 gotoxy(18,3);   	      cprintf("�����������");

 gotoxy(14,5);  cprintf("���������������������������������");
 gotoxy(46,6);  cprintf("�");
 gotoxy(46,7);  cprintf("�");
 gotoxy(46,8);  cprintf("�");
 gotoxy(46,9);  cprintf("�");
 gotoxy(46,10);  cprintf("�");
 gotoxy(46,11);  cprintf("�");
 gotoxy(46,12);  cprintf("�");
 gotoxy(46,13);  cprintf("�");

}


void Pantalla_Escribir(void){
 highvideo();
 textcolor(BLACK);
 textbackground(7);
 gotoxy(3,3);  cprintf("�");
 gotoxy(17,3);	     	     cprintf("�");
 gotoxy(3,4);  cprintf("� Leer     ");
 textcolor(WHITE); cprintf("�"); textcolor(BLACK);
 gotoxy(17,4);	     	     cprintf("� Escribir ");
 textcolor(WHITE); cprintf("�"); textcolor(BLACK);
 gotoxy(3,5);  cprintf("�          �� ����������������������������Ŀ");
 gotoxy(3,6);  cprintf("�                                          �");
 gotoxy(3,7);  cprintf("� Direcci�n:                               �");
 gotoxy(3,8);  cprintf("�                                          �");
 gotoxy(3,9);  cprintf("� Dato:                                    �");
 gotoxy(3,10);  cprintf("�                                          �");
 gotoxy(3,11);  cprintf("�                                          �");
	       textcolor(RED);
 gotoxy(5,11);  cprintf("Dato escrito:");
	       textcolor(BLACK);
 gotoxy(3,12);  cprintf("�                                          �");
 gotoxy(3,13);  cprintf("�������������������������������������������");

 textcolor(WHITE);
 gotoxy(4,3);   cprintf("�����������");
 gotoxy(18,3);   	      cprintf("�����������");

 gotoxy(4,5);   cprintf("��������������          �������������������");
 gotoxy(46,6);  cprintf("�");
 gotoxy(46,7);  cprintf("�");
 gotoxy(46,8);  cprintf("�");
 gotoxy(46,9);  cprintf("�");
 gotoxy(46,10);  cprintf("�");
 gotoxy(46,11);  cprintf("�");
 gotoxy(46,12);  cprintf("�");
 gotoxy(46,13);  cprintf("�");

}


int Funcion_Botones(int Funcion, int nBotones, struct Boton_Texto *Boton_Texto){

 char i, DEVOLVER = -1;
 int longitud, key;

 switch( Funcion ) {
  case DIBUJA:
		 highvideo();
		 textcolor(BLACK);
		 textbackground(11);
		 gotoxy(Boton_Texto[nBotones].x, Boton_Texto[nBotones].y);
	      DEVOLVER = 0;
	      longitud = strlen( Boton_Texto[nBotones].Texto );
	      while( DEVOLVER <= longitud  ) {
	       if( Boton_Texto[nBotones].Texto[DEVOLVER] == '&' ) {
		DEVOLVER ++;
	      textcolor(RED);
		cprintf("%c", Boton_Texto[nBotones].Texto[DEVOLVER]);
	      textcolor(BLACK);
	       } else cprintf("%c", Boton_Texto[nBotones].Texto[DEVOLVER]);
	       DEVOLVER ++;
	      }

 //		      cprintf("%s", Texto);

	      textbackground(7);   			  // �SALIR��
				cprintf("�");		  //  �������
	      gotoxy(Boton_Texto[nBotones].x+1, Boton_Texto[nBotones].y+1);
	      for(i = 0; i < longitud; i ++ )             // ��������
				cprintf("�");             //  �������
	      break;
  case DEPRIME:
		 highvideo();
		 textcolor(BLACK);
		 textbackground(11);
	      gotoxy(Boton_Texto[nBotones].x, Boton_Texto[nBotones].y);
	      DEVOLVER = 0;
	      longitud = strlen( Boton_Texto[nBotones].Texto );
	      textbackground(7);
	      while( DEVOLVER <= (longitud+1)  ) {
	       if( Boton_Texto[nBotones].Texto[DEVOLVER] == '&' ) {
		DEVOLVER ++;
	       }
	       cprintf(" ");
	       DEVOLVER ++;
	      }
	      textbackground(11);
	      gotoxy(Boton_Texto[nBotones].x+1, Boton_Texto[nBotones].y+1);
	      DEVOLVER = 0;
	      while( DEVOLVER <= longitud  ) {

	       if( Boton_Texto[nBotones].Texto[DEVOLVER] == '&' ) {
		DEVOLVER ++;
	      textcolor(RED);
		cprintf("%c", Boton_Texto[nBotones].Texto[DEVOLVER]);
	      textcolor(BLACK);
	       } else cprintf("%c", Boton_Texto[nBotones].Texto[DEVOLVER]);
	       DEVOLVER ++;
	      }
//	      cprintf("%s", Texto);                       //
							  //  �������
	      break;
  case Dibuja_Todos:
	      for( i=0; i < nBotones; i++)
		Funcion_Botones( DIBUJA, i, Boton_Texto);
	      break;
  case COMPRUEBA:
	      if( bioskey(1)!=0 ) {
		      key = toupper( getch() );
		      for( i=0; i < nBotones && DEVOLVER <= -1; i++){
			if( key == Boton_Texto[i].ID_T1 ) {
			     if( key == 0 ) {
			       key = toupper( getch() );
			       if( key == Boton_Texto[i].ID_T2 ){
								 DEVOLVER = (i + 1);
								 Funcion_Botones( DEPRIME, (DEVOLVER-1), Boton_Texto);
								 delay(150);
								 //sleep(1);
								 Funcion_Botones( DIBUJA, (DEVOLVER-1), Boton_Texto);
			       } else DEVOLVER = -2;
			     } else
					{ DEVOLVER =  (i + 1);
					 Funcion_Botones( DEPRIME, (DEVOLVER-1), Boton_Texto);
					 delay(150);
					 //sleep(1);
					 Funcion_Botones( DIBUJA, (DEVOLVER-1), Boton_Texto);
					}
			}
		      }
		      if( DEVOLVER == -1 )
					{ ungetch(key); }

		      if( DEVOLVER == -2 )
					{ ungetch(0); ungetch(key); DEVOLVER = -1; }

	      } else if( RATON ) {
		      for( i=0; i < nBotones && DEVOLVER == -1; i++) {
			longitud = strlen( Boton_Texto[i].Texto );
			if ( (x_raton+1) >= Boton_Texto[i].x && (x_raton+1) <= ( Boton_Texto[i].x + longitud -1) &&
			     y_raton == (Boton_Texto[i].y-1) ) { DEVOLVER = (i + 1);
							     Funcion_Botones( DEPRIME, (DEVOLVER-1), Boton_Texto);
							     if( RATON ) activa_raton();
							     espera_soltar();
							     if( RATON ) desactiva_raton();
//			     				     delay(150);
							     Funcion_Botones( DIBUJA, (DEVOLVER-1), Boton_Texto);
			}
		      }
	      }
	      break;
  case STD_BY:
	      Funcion_Botones(Dibuja_Todos, 6, Boton_Texto);
	      do{
		if( RATON ) activa_raton();
		if( RATON ) espera_pulsar(); else while( bioskey(1)==0 );
		if( RATON ) desactiva_raton();
		DEVOLVER = Funcion_Botones(COMPRUEBA, 6, Boton_Texto);
	      }while(DEVOLVER==-1 && bioskey(1)==0 );

 }
 return DEVOLVER;
}

void inicializa_raton_texto(void)
{
	union REGS ent, sal;

	ent.x.ax =   0;
        int86(0x33, &ent, &sal);          /* averigua si hay rat�n conectado */

	if(sal.x.ax==0) { RATON = 0; return; }

        ent.x.ax =  15;
	ent.x.cx =   8;
	ent.x.dx =  16;
        int86(0x33, &ent, &sal);               /* fija la raz�n mickey/pixel */

	ent.x.ax =   7;
        ent.x.cx =   0;
        ent.x.dx = 639;
        int86(0x33, &ent, &sal);  /* fija la posici�n m�x. y m�n. horizontal */

        ent.x.ax =   8;
	ent.x.cx =   0;
	ent.x.dx = 199;
	int86(0x33, &ent, &sal);    /* fija la posici�n m�x. y m�n. vertical */

	ent.x.ax =   4;
	ent.x.cx = 320;
	ent.x.dx = 100;
	int86(0x33, &ent, &sal);               /* fija la posici�n del rat�n */

	ent.x.ax =   1;
	int86(0x33, &ent, &sal);             /* muestra el puntero del rat�n */
}
void activa_raton(void)
{
	union REGS ent, sal;

	ent.x.ax = 1;
	int86(0x33, &ent, &sal);
}

void desactiva_raton(void)
{
        union REGS ent, sal;

	ent.x.ax = 2;
	int86(0x33, &ent, &sal);
}

void espera_pulsar(void)
{
	union REGS ent, sal;

	do{
	  if(bioskey(1)!=0) break;
	  ent.x.ax = 3;
	  int86(0x33, &ent, &sal);       /* lee posici�n y estados del bot�n */
	}while((sal.x.bx & 3)==0);
	boton_izq=sal.x.bx & 1;
	boton_der=(sal.x.bx >> 1) & 1;
	x_raton= (sal.x.cx >> 1) / 4; // / 8
	y_raton= (sal.x.dx)/8; //  /8
}

void espera_soltar(void)
{
	union REGS ent, sal;

	do{
	  ent.x.ax = 3;
	  int86(0x33, &ent, &sal);       /* lee posici�n y estados del bot�n */
	}while((sal.x.bx & 3)!=0);
	boton_izq=sal.x.bx & 1;
	boton_der=(sal.x.bx >> 1) & 1;
	x_raton=sal.x.cx >> 1;
	y_raton=sal.x.dx;
}


void Edit_Item(void){

char buffer[80];
char *endptr;

 if( !SWAP ) {

   do{
		strcpy( buffer, "\0");
		gotoxy(16,7);	textcolor(RED);
                cprintf("                         ");
                gotoxy(16,7);
		cscanf("%10s", buffer );

		Dir = strtol(buffer, &endptr, 16);
   }while(*endptr!=NULL);

 // LEER
 } else {
 // ESCRIBIR

   textcolor(RED);
   gotoxy(16,7);
   do{
		strcpy( buffer, "\0");
		gotoxy(16,7);	textcolor(RED);
                cprintf("                         ");
                gotoxy(16,7);
		cscanf("%10s", buffer );
		Dir = strtol(buffer, &endptr, 16);
   }while(*endptr!=NULL);

   do{
		strcpy( buffer, "\0");
		gotoxy(16,9);	textcolor(RED);
                cprintf("                         ");
                gotoxy(16,9);
		cscanf("%10s", buffer );
		Dato = strtol(buffer, &endptr, 16);
   }while(*endptr!=NULL);


 }

}