/*----------------------------------------------------------------------------------------
	Ver1.00	2001/01/07 21:47:50
	WWCTool用サンプルプログラム。
	スピード遅く、バグが有ります。
----------------------------------------------------------------------------------------*/

#include "..\WWCMain.h"
#include "TextView.h"

/*----------------------------------------------------------------------------------------
	グローバル変数
----------------------------------------------------------------------------------------*/

UCHAR TextImage[28*18*32];
UCHAR Image1Buf[MAXFNAME*10*32];
int Line;
int Column;
int TextTop[1000];

UCHAR FileList[10][MAXFNAME];
int FileListPos;

/*----------------------------------------------------------------------------------------
	WWCInitはWWCToolが管理しています。
----------------------------------------------------------------------------------------*/
/*WWCTool*/

/*--------------------------------------------------------------------------------------*/

void WWCInit(void)
{

	Form->OnCreate = &EFormCreate;
	Form->StartClose = true;
	Image0->OnKeyUp = &EImage0KeyUp;
	Image0->Image = TextImage;
	Image1->Top = 3;
	Image1->Left = 1;
	Image1->Height = 10;
	Image1->Width = MAXFNAME;
	Image1->Image = Image1Buf;
	Image1->Enabled = true;
	Image1->ImageHeight = 10;
	Image1->OnKeyUp = &EImage1KeyUp;
	Image1->ImageWidth = MAXFNAME;
	Timer->OnTimer = &ETimerTimer;
	Timer->Interval = 500;
	Timer->Enabled = true;
	Image0->Enabled = true;
}

/*--------------------------------------------------------------------------------------*/

void EFormCreate(void)
{
	chdir("/rom0/");
	Form->Focused = 1;
	EImage1KeyUp(Key->ExUp);
}

/*--------------------------------------------------------------------------------------*/

void EImage1KeyUp(UINT Keys)
{
	int Size, n;

	if(Keys & Key->ExUp){
		ImageFill(Image1, (WHITE<<4)|WHITE); 
		Size = FileGetName(FileList, "", 10);
		for(n=0;n < Size;n++){
			FileList[n][MAXFNAME-1] = 0;
			ImagePuts(Image1, 0, n, FileList[n], BLACK, WHITE);
		}
	}

	ImagePuts(Image1, 0, FileListPos, FileList[FileListPos], BLACK, WHITE);
	if(Keys & Key->Up){
		if(FileListPos > 0){
			FileListPos--;
		}
	}
	if(Keys & Key->Down){
		if(FileListPos < 10-1){
			FileListPos++;
		}
	}
	ImagePuts(Image1, 0, FileListPos, FileList[FileListPos], WHITE, BLUE);
	if(Keys & Key->Enter){
		ReadFile(FileList[FileListPos], _heap, TextTop);
		View();
		Form->Focused = 0;
		Image1->Enabled = false;
	}
}

/*--------------------------------------------------------------------------------------*/

void EImage0KeyUp(UINT Keys)
{
	if(Keys & Key->ExUp){
		Form->Focused = 1;
		Image1->Enabled = true;
		EImage1KeyUp(Key->ExUp);
	}
	if(Keys & Key->Esc){
		FormViewChange(!Form->TateView);
		View();
	}
}

/*--------------------------------------------------------------------------------------*/

void ETimerTimer(void)
{
	if(Form->Focused == 0){
		if(Form->Key & Key->Up){
			Line--;
			View();
		}
		if(Form->Key & Key->Down){
			Line++;
			View();
		}
		if(Form->Key & Key->Right){
			Column++;
			View();
		}
		if(Form->Key & Key->Left){
			Column--;
			View();
		}
	}

}

/*--------------------------------------------------------------------------------------*/
/*	ライン変換																			*/
/*--------------------------------------------------------------------------------------*/

void GetLine(UINT far *Buf, int Line)
{
	int n, x, m;

	x=0;
	for(n=TextTop[Line];n < TextTop[Line+1];n++){
		if(_heap[n] == '\t'){
			for(m=0;m < 4-(x%4);m++){ 
				Buf[x+m] = ' ';
			}
			x += 4-(x%4);
		}else if(_heap[n] == '\n'){
			Buf[x] = ' ';
			break;
		}else if(_heap[n] == '\r'){
			Buf[x] = 0x19;
			x++;
		}else if((_heap[n] >= 0x81 && _heap[n] <= 0x9f) || (_heap[n] >= 0xe0 && _heap[n] <= 0xfc)){
			Buf[x] = (_heap[n]<<8) | (_heap[n+1] & 0xff);
			n++;
			x++;
		}else{
			Buf[x] = _heap[n];
			x++;
		}
	}
	for(;x < 128;x++){
		Buf[x] = ' ';
	}
	Buf[x] = 0;
}

/*--------------------------------------------------------------------------------------*/
/*	表示																				*/
/*--------------------------------------------------------------------------------------*/

void View(void)
{
	UINT TextBuf[128+1];
	int y;
	ImageFill(Image1, (WHITE<<4)|WHITE); 
	for(y=0;y < Image0->Height;y++){
		GetLine(TextBuf, Line+y);
		ImageWPuts(Image0, 0, y, &(TextBuf[Column]), WHITE, 0);
	}
}

/*--------------------------------------------------------------------------------------*/
/*return																				*/
/*	サイズ																				*/
/*	Topに行の先頭の位置を記録															*/
/*--------------------------------------------------------------------------------------*/

int ReadFile(UCHAR *Path, UCHAR *Buf, int *Top)
{
	int fd, Size, y, Pos;

	ClrInterrupt();
	if((fd = open(Path, FMODE_R, 0)) >= 0) {
		Size = read(fd, Buf, -(int)Buf);
		close(fd);
		SetInterrupt();

		Top[0] = 0;
		y = 1;
		for(Pos=0;Pos < Size;Pos++){
			if(Buf[Pos] == '\n'){
				Top[y] = Pos+1;
				y++;
			}
		}
		Top[y] = 0;
		return(Size);
	}else{
		SetInterrupt();
		return(0);
	}
}

/*--------------------------------------------------------------------------------------*/
