#include <dos.h>
#include <conio.h>
#include <ctype.h>
#include <stdio.h>
#include <alloc.h>
#include <string.h>
#include <stdarg.h>

#define BLANCO EGA_WHITE
#define ENCUADRE       3
#define SEC_OPTAR      5

#include "B-T.h"

void BEEP(void);

 /**************************************************************************\
|*                                                                          *|
|*  InputCadenaG                                                            *|
|*                                                                          *|
|*  Descripci¢n:                                                            *|
|*              Edita una cadena en formato gr fico                         *|
|*                                                                          *|
|*  Entradas:                                                               *|
|*            Puntero a los datos editados                                  *|
|*            0 alfanumerico   1 numerico                                   *|
|*            longitud de la cadena (TEXTO/NUMERO)                          *|
|*            color del texto                                               *|
|*            color de fondo                                                *|
|*            Limites de acotacion                                          *|
|*                                                                          *|
|*  Salidas:  (ninguna)                                                     *|
|*                                                                          *|
|*                                                                          *|
 \**************************************************************************/
int InputCadenaG(char *s, int numalp, int lmax, int cc, int cf, int X0, int Y0, int X1, int Y1)
{
  // A todas las y les sumaba antes +RoW*12 parametro que indica la linea

  int  ls;                                      // longitud cadena
  char Status = 0;
  char *s1;                                     // puntero a cadena inicial
  int c, ok, i;

  s1 = s;                                       // inicio cadena

                textbackground( cf );

                gotoxy( X0, Y0 );
                for ( i = 0; i < X1; i++ )
                                        putch( 32 );
                textcolor( cc );
                gotoxy( X0, Y0 );
                cprintf( "%s", s1 );

  _setcursortype( _SOLIDCURSOR );

  ls = strlen ( s );                            // Longitud de actual

  s += ls;                                      // se coloca en el final

  do{
	c = getch();                            // obtiene tecla

        if ( c == 27 ) Status = 1;

	ok = ( c == 27 || c == 13 || c == 0);              // 13 = INTRO || Especiales

	if ( c == 8  && ls > 0 && !ok ) {       //  8 = Back Space
		ls--;
		s--;

		*s = '\0';

                gotoxy( X0, Y0 );
                for ( i = 0; i < X1; i++ )
                                         putch( 32 );
                textcolor( cc );
                gotoxy( X0, Y0 );
                cprintf( "%s", s1 );


	} else {
		if ( !numalp && c >= 32 && c <= 254  && ls < lmax) {

			*s++ = c;
			ls++;

			*s = '\0';

                gotoxy( X0, Y0 );
                for ( i = 0; i < X1; i++ )
                                         putch( 32 );
                textcolor( cc );
                gotoxy( X0, Y0 );
                cprintf( "%s", s1 );

		} else {
			if ( numalp && isdigit(c)  && ls < lmax) {
				*s++ = c;
				ls++;

				*s = '\0';                              // Cero final

                gotoxy( X0, Y0 );
                for ( i = 0; i < X1; i++ )
                                         putch( 32 );
                textcolor( cc );
                gotoxy( X0, Y0 );
                cprintf( "%s", s1 );

		}
		/*ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ*/
		else if( c == 27 ) {*s='\0'; ok = 1; }
		/*ÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛÛ*/
		else BEEP();
	}
     }

  }while(!ok);

  _setcursortype( _NOCURSOR );

  ok = 1;
  *s = ' ';
  while( ok && ls >= 0 ) {
		if(*s==' ') { *s = '\0'; s--; ls--;
		} else { s++; ok = 0; }
  }

  *s = '\0';

  while(kbhit()) getch();              // Vacia Buffer impidiendo falsas
					//  pulsaciones...

  return ( (Status<<8) + (ls&0x00FF) );
}

void BEEP(void)
{

	sound(440);
	delay(50);
	nosound();

}


void formatea_u_long( unsigned long Numero, char *Inter_Chg)
{
 char Buffer1[80];

 int c;

 sprintf(Buffer1, "%lu", Numero);

 Inter_Chg[0]='\0';

 strrev(Buffer1);

 c = strlen( Buffer1 );

 while( c >= 3 )
 {

    c -= 3;
    strncat( Inter_Chg, Buffer1, 3);

    strrev(Buffer1);

    Buffer1[c] = '\0';

    strrev(Buffer1);

    if(strlen(Buffer1)!=0)
			strcat( Inter_Chg, "." );
 }

 strcat( Inter_Chg, Buffer1);
 strrev(Inter_Chg);


/*// return Buffer2;*/
}


int Optar( int optar, ... )
{
struct Boton_Texto BT[6];
int devolver = 0;

BT[0].x = 22; BT[0].y = 15; strcpy( BT[0].Texto, " acep&Tar "); BT[0].ID_T1 = 84; BT[0].ID_T2 = 20;
BT[1].x = 51; BT[1].y = 15; strcpy( BT[1].Texto, " &Cancelar"); BT[1].ID_T1 = 67; BT[1].ID_T2 = 46;

 static char fondo[4096];
 static char Memoria = 0;

 int DEV = 0, ok = 0, linea = 0;
 int Center; char *buff;

 va_list ap;
 va_start( ap, &optar );

 if ( Memoria == 1 )
 {
  puttext(  21,  5, 63, 17, (void *)fondo );  Memoria = 0;
  if ( optar == ENCUADRE ) return DEV;
 }

 Memoria = 1;

 gettext( 21,  5,  63, 17, (void *)fondo      );

textbackground( BLACK );
textcolor( CYAN );
gotoxy( 21,  5 );    cprintf("ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿");
gotoxy( 21,  6 );    cprintf("³                                         ³");
gotoxy( 21,  7 );    cprintf("ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´");
gotoxy( 21,  8 );    cprintf("³                                         ³");
gotoxy( 21,  9 );    cprintf("³                                         ³");
gotoxy( 21, 10 );    cprintf("³                                         ³");
gotoxy( 21, 11 );    cprintf("³                                         ³");
gotoxy( 21, 12 );    cprintf("³                                         ³");
gotoxy( 21, 13 );    cprintf("³                                         ³");
gotoxy( 21, 14 );    cprintf("³                                         ³");
gotoxy( 21, 15 );    cprintf("³                                         ³");
gotoxy( 21, 16 );    cprintf("³                                         ³");
gotoxy( 21, 17 );    cprintf("ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ");



   textcolor( RED );
   linea = 0;
   while ( (buff = va_arg(ap, char *)) != NULL )
   {
     Center = (42 - strlen( buff ) ) / 2;
     gotoxy( 21+Center+1, ( (linea == 0 ) ? 6 : 8 + linea ) );
     cprintf( "%s", buff );
     linea++;
   }

   va_end(ap);

   if ( optar != ENCUADRE )
   {
    ok = 0;   	       while( kbhit() ) getch();  // Limpiamos posibles teclas en BUFFER
    if ( optar == 1 )
    do {
      switch( Funcion_Botones(STD_BY, 2, BT) )
      {
        case  0:
        				  // No se pulso ningun BOTON
        case -2:
        case -1:
	       while( kbhit() ) getch();  // Limpiamos posibles teclas en BUFFER
               if ( optar  == 0 ) ok = 1;
	       break;
        case  1:
                DEV = 1; ok = 1;
                break;
        case  2:
                DEV = 0; ok = 1;
                break;
      }
    }while( !ok );
    else { espera_pulsar(); espera_soltar(); }

   puttext(  21,  5, 63, 17, (void *)fondo );

   Memoria = 0;

   }

 return DEV;
}