#include "wsubform.h"
#include <locale.h>

#define Textbox	1
#define Grid	2

#define Tret	199
#define Gret	299

extern int wmode;
extern char gridtitles[MAX_POS][MAX_LEN];

extern TextBox	 *begin;
extern TextBox	 *tp;

typedef struct stat Dir;
static Dir dirptr;
static Dir  *fileptr = &dirptr;

char tmp[256];

int subform(void)
{
	int i,j,k;
	int ret;
	char f[128];
	char s[256];

	setlocale(LC_ALL,"");

	strcpy(f,"_ega_subform.def");

	initscr();

	cbreak();
	noecho();
	keypad(stdscr, TRUE);
	meta(stdscr, TRUE);

	if(stat(f,fileptr) != 0)
	{
		echo();
		endwin();
		printf("canot find %s\n",f);
		exit(0);
	}

//Automatic Subform Editor v3.0.0

	static int xcol = 79;
	static int rows = 19;

	static int label_length[] = {14,14,14,14,14,14,14,0};
	static int label_no[] = {0,1,2,3,4,5,6,0};
	static int label_align[] = {1,1,1,1,1,1,1,0};
	static char *label_text[8][128] = {"Label_1","Label_2","Label_3","Label_4","Label_5","Label_6","Label_7",""};
	static int label_x[] = {1,1,1,1,1,1,1,0};
	static int label_y[] = {1,2,3,4,5,6,7,0};
	static int textbox_length[] = {3,7,14,10,11,11,14,0};
	static int textbox_no[] = {0,1,2,3,4,5,6,0};
	static int textbox_align[] = {1,1,1,1,1,1,1,0};
	static int textbox_action[] = {0,0,0,0,0,0,0,0};
	static int textbox_skipstep[] = {1,1,1,1,1,1,1,0};
	static int textbox_field[] = {1,1,1,1,1,1,1,0};
	static int textbox_x[] = {15,15,15,15,15,15,15,0};
	static int textbox_y[] = {1,2,3,4,5,6,7,0};


	static int cell_length[] = {3,7,16,10,10,10,15,0};
	static int cell_align[] = {1,1,1,1,2,2,2,0};
	static int cell_action[] = {0,0,0,0,0,0,0,0};
	static int grid_field[] = {1,1,1,1,2,4,4,0};
	static int grid_skipstep[] = {1,1,1,1,1,1,1,0};
	char *grid_titles[8][128] = {"Grid_1","Grid_2","Grid_3","Grid_4","Grid_5","Grid_6","Grid_7",""};
//	static char *db_titles[8][128] = {0};
	static int grid_sx = 0;
	static int grid_ex = 76;
	static int grid_sy = 10;
	static int grid_ey = 18;

	static int maxtext;
	static int no = 0;

	char **gt;
	char **lt;

	gt = (char **)&grid_titles;

	xcol = MakeWsubForm(no,xcol,rows,0,2,grid_sx,grid_sy,grid_ex,grid_ey,cell_length,cell_align,cell_action,grid_field,grid_skipstep,gt,7);

	lt = (char **)&label_text;

	maxtext = MakeTextBox(no,0,2,xcol,rows,label_no,lt,label_x,label_y,label_length,textbox_no,textbox_x,textbox_y,textbox_length,textbox_align,textbox_action,textbox_field,textbox_skipstep, 7);


	wrefresh(xt[0].form);

	for(i=0;i<MAX_POS;i++)
		memset(gridtitles[i],'\0',MAX_LEN);

	sf_tbegin();
	for(i=0;i<maxtext;i++)
	{
		sprintf(s,"%d",i+1);
		sf_tput(0,i,s);
		wrefresh(ft[0].pt[i].panel);
		wrefresh(ft[0].pos[i].vs);
		sf_tnext();
	}

	for(i=0,k=1;i<ft[0].lcol;i++)
	{
		for(j=0;j<(ft[0].ey-ft[0].sy)+1;j++)
		{
			sprintf(s,"%d",k);
			sf_gput(0,i,j,s);
			k++;
		}
	}

	sf_startcell(0);

	ret = Gret;
//	sf_tbegin();
	while(TRUE)
	{
	//	ret = sf_DBgrid(0,maxtext,140,0,DBcount,maxtext);	//データベース連結用グリッド。*splitDBbuff[][]を表示する。
		if(ret == Tret)
			ret = sf_grid(0,maxtext,xcol);
		else
			ret = sf_formtext(0,maxtext, xcol);
		switch(ret)
		{
		case -1:
			echo();
			endwin();
			exit(0);
			break;

	//for textbox
		case 100: //ESC
			if(popmsg(2+8,(LINES/2)-4,"Exit textbox area in Subform?") == 1)
				goto end;
			ret = Gret;
			break;
		case 101: //F1
			ret = Gret;
			break;
		case 102: //F2
			ret = Gret;
			break;
		case 103: //F3
			ret = Gret;
			break;
		case 104: //F4
			ret = Gret;
			break;
		case 105: //F5
			ret = Gret;
			break;
		case 106: //F6
			ret = Gret;
			break;
		case 107: //F7
			ret = Gret;
			break;
		case 108: //F8
			ret = Gret;
			break;
		case 109: //F9
			ret = Gret;
			break;
		case 110: //F10
			ret = Gret;
			break;
		case 111: //F11
			ret = Gret;
			break;

	//for grid
		case 200: //ESC
			if(popmsg(2+8,(LINES/2)-4,"Exit grid area in Subform?") == 1)
				goto end;
			ret =Tret;
			break;
		case 201: //F1
			ret = Tret;
			break;
		case 202: //F2
			ret = Tret;
			break;
		case 203: //F3
			ret = Tret;
			break;
		case 204: //F4
			ret = Tret;
			break;
		case 205: //F5
			ret = Tret;
			break;
		case 206: //F6
			ret = Tret;
			break;
		case 207: //F7
			ret = Tret;
			break;
		case 208: //F8
			ret = Tret;
			break;
		case 209: //F9
			ret = Tret;
			break;
		case 210: //F10
			ret = Tret;
			break;
		case 211: //F11
			ret = Tret;
			break;
		default:
			break;
		}
	}
end:
	echo();
	endwin();
}

int main(void)
{
	subform();
}

