#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<egb.h>
#include"strin.h"

#define	ERR		1
#define	NOERR	0

#include "strindat.c"

static int	ST_x = 0;
static int	ST_y = 0;
static char	*egbwork;
static void	symbol( char *, int, int, char * );

static void	symbol( char *egbwork, int x, int y, char *str )
{
	int		l;

	l = strlen( str );

	{
		char	para[l+6];
	 	WORD( para + 0 ) = x;
		WORD( para + 2 ) = y;
		WORD( para + 4 ) = strlen( str );
		strcpy( para + 6, str );
		EGB_sjisString( egbwork, para );
	}

	return;
}

int		ST_locate( int x, int y )
{
	if( x<0 || x>367 || y>0 || y<433 )
		return ERR;

	ST_x = x;
	ST_y = y;

	return NOERR;
}

static void	strcpy2( char *s1, const char *s2, char *buf )
{
	strcpy( buf, s2 );

	while( *buf )
		*s1++ = *buf++;

	*s1 = '\0';

	return;
}

static void	strprint( char *buf, int dsp, int cur )
{
	int		i;
	char	printbuf[33];

	printbuf[32] = '\0';
	EGB_paintMode( egbwork, 0x22 );
	EGB_foreColor( egbwork, 0 );
	EGB_paintColor( egbwork, 0 );
	EGB_box( egbwork, ST_x+8, ST_y+25, ST_x+263, ST_y+40 );

	for( i=0; i<32; i++ )
	{
		printbuf[i] = *(buf+dsp+i);

		if( *(buf+dsp+i) == 0 )
			break;
	}

	EGB_foreColor( egbwork, 10 );
	EGB_singleLine( egbwork, (ST_x+8+(cur-dsp)*8), (ST_y+25), (ST_x+8+(cur-dsp)*8), (ST_y+40) );

	EGB_foreColor( egbwork, 15 );
	symbol( egbwork, ST_x+8, ST_y+40, printbuf );

	return;
}

int		ST_inputStrings( char *w, char *buf, int size, const char *mes )
{
	int		ch,ec,cur,len,dsp,ret,i;
	char	gbuf[6256],usrbuf[size],mes_[33];

	cur = 0;
	len = 0;
	dsp = 0;
	egbwork = w;

	EGB_getRect( egbwork, gbuf, ST_x, ST_y, ST_x+271, ST_y+45 );
	EGB_putRect( egbwork, 0, ST_grp_data, ST_x, ST_y, 271+ST_x, 45+ST_y );

	EGB_foreColor( egbwork, 8 );
	strcpy( mes_, mes );
	mes_[32] = '\0';
	symbol( egbwork, ST_x+8, ST_y+20, mes_ );

	for( i=0; i<size; i++ )
		buf[i] = 0;

	while(1)
	{
		ch = KYB_read( 1, &ec );
		if( ( ch & 0xff00 ) == 0xff00 )
			continue;
		ch &= 0xff;

		if( ch>0x1f && ch<0x7f )	/* 文字の判定 */
		{
			if( len+1 < size )	/* 最後に'\0'を加える事を考えての判定 */
			{
				strcpy2( buf+cur+1, buf+cur, usrbuf );
				*(buf+cur) = (char)ch;
				if( dsp+31 == cur )
					dsp ++;
				cur ++;
				len ++;
				strprint( buf, dsp, cur );
			}
		}
		else
		{
			ec &= 0xff14;
			switch( ec )
			{
				case 0x4500:
				case 0x1d00:
				case 0x7300:
					/* 入力終了 */
					*(buf+len) = '\0';
					ret = len;
					goto END;
				case 0x0100:
				case 0x7200:
					/* 入力終了(取消) */
					ret = -3;
					goto END;
				case 0x4f00:
					/* 一文字左へ */
					if( cur > 0 )
					{
						if( dsp == cur )
							dsp --;
						cur --;
						strprint( buf, dsp, cur );
					}
					break;
				case 0x4f04:
					/* 最左端へ */
					if( cur > 0 )
					{
						cur = 0;
						dsp = 0;
						strprint( buf, dsp, cur );
					}
					break;
				case 0x5100:
					/* 一文字右へ */
					if( cur < len )
					{
						if( dsp+31 == cur )
							dsp ++;
						cur ++;
						strprint( buf, dsp, cur );
					}
					break;
				case 0x5104:
					/* 最右端へ */
					if( cur < len )
					{
						cur = len;
						if( cur > dsp + 31 )
							dsp = cur - 31;
						strprint( buf, dsp, cur );
					}
					break;
				case 0x0f00:
					/* [BS] */
					if( cur > 0 )
					{
						if( dsp == cur )
							dsp --;
						strcpy2( buf+cur-1, buf+cur, usrbuf );
						len --;
						cur --;
						strprint( buf, dsp, cur );
					}
					break;
				case 0x4b00:
					/* [DEL] */
					if( cur < len )
					{
						strcpy2( buf+cur, buf+cur+1, usrbuf );
						len --;
						strprint( buf, dsp, cur );
					}
					break;
			}
		}
	}

END:

	EGB_putRect( egbwork, 0, gbuf, ST_x, ST_y, ST_x+271, ST_y+45 );

	return ret;
}
