/* 

	○テキストエディター○

*/

#include "apilib.h"
#include <string.h>

/* draw_editbox内で使うから */
const int win_x = 402, win_y = 302;

#define KEY_TABLECODE_ECODE	'\n'
#define KEY_TABLECODE_BS	8
#define KEY_TABLECODE_ENTER	10

/*#define _DEBUG*/

void draw_editbox(int win, 
				  int x0, int y0, 
				  int sx, int sy,
				  char *buf, unsigned long size) {
	int GetKey_Code = 0; 
	unsigned long cy = 0, i = 0, current = 0, current2 = 0, cx = 0;
	char s[2], str[40], n[40];
	
	/* ボックスを描写 */
	api_linewin(win, x0-2, y0-2, x0-2, y0+sy, COL_WHITE);
	api_linewin(win, x0-2, y0-2, x0+sx, y0-2, COL_WHITE);
	api_linewin(win, x0-1, y0-1, x0-1, y0+sy, COL_BLACK);
	api_linewin(win, x0-1, y0-1, x0+sx, y0-1, COL_BLACK);
	api_linewin(win, x0, y0, x0+sx, y0, COL_WHITE);
	api_linewin(win, x0-1, y0+sy, x0+sx, y0+sy, COL_BLACK);
	api_linewin(win, x0, y0, x0, y0+sy-1, COL_WHITE);
	api_linewin(win, x0+sx, y0, x0+sx, y0+sy, COL_BLACK);
	api_boxfilwin(win, x0+1, y0+1, (x0+sx)-1, (y0+sy)-1, COL_WHITE);

	/* バイト数 */
    sprintf(str, "%d Bytes ", i);
    api_putstrwin(win, win_x - 300, win_y - 25, 
                       COL_BLACK, 40, str);

	/* 文字入力処理 */
	api_boxfilwin(win, x0+1, y0+1, x0+1+8, y0+1+16, 0);
	for (;;) {
		GetKey_Code = api_getkey(1);		

		/* バッファ追加 */
		switch (GetKey_Code) {
			case KEY_TABLECODE_BS:			/* バックスペース */
				if (i != 0) {
					api_boxfilwin(win, (x0+1)+cx, y0+1+cy,
								(x0+1)+cx+8, y0+1+cy+16, 7);
					if (cx != 0) { 
						cx -= 8;	
						api_boxfilwin(win, (x0+1)+cx, (y0+1)+cy,
									(x0+1)+cx+8, (y0+1)+cy+16, 0);
						buf[i] = 0;
						i--;
						break;
					} else if (cx == 0) {
						if (cy != 0) {
							for (current = 0;current < i;current++) {
								if (buf[current] == KEY_TABLECODE_ECODE) {
							#ifdef _DEBUG										
									sprintf(n, "get down %d\n", current);							
									api_putstr0(n);
							#endif
									cx = (8 * 46 - (46 - current) * 8);
									if (cy > 32) {
										cx = /*i * 8 -*/ 8 * current;
									}
									goto back_line;
								}
							}
							break;
						back_line:
							cy -= 16;
							api_boxfilwin(win, (x0+1)+cx, (y0+1)+cy,
										(x0+1)+cx+8, (y0+1)+cy+16, 0);
							buf[i] = 0;
							i--;
							break;
						}
					}
				}
				api_boxfilwin(win, (x0+1)+cx, y0+1+cy,
							(x0+1)+cx+8, y0+1+cy+16, 0);
				break;
			case KEY_TABLECODE_ENTER:	/* エンターキー */
				api_boxfilwin(win, (x0+1)+cx, y0+1+cy,
								(x0+1)+cx+8, y0+1+cy+16, 7);
				buf[i] = KEY_TABLECODE_ECODE;
				i++;
				cy += 16;
				cx = 0;
				api_boxfilwin(win, (x0+1)+cx, (y0+1)+cy,
								(x0+1)+cx+8, (y0+1)+cy+16, 0);
				break;
			default:	/* 通常文字 */
				api_boxfilwin(win, (x0+1)+cx, y0+1+cy, 
							(x0+1)+cx+8, y0+1+cy+16, 7);
				s[0] = GetKey_Code; 
				s[1] = 0;
				if ((sx - 16) < cx) {
					cy += 16;
					cx = 0;
				}
				api_putstrwin(win, (x0+1)+cx, (y0+1)+cy, 0, 2, s);
				cx += 8;
				api_boxfilwin(win, (x0+1)+cx, y0+1+cy, 
								(x0+1)+cx+8, y0+1+cy+16, 0);
				*(buf + i) = (char)s[0];
				i++;
				break;
		}

		/* バイト数表示 */
		api_boxfilwin(win, win_x - 300, win_y - 25, 
						win_x - 180, win_y - 5, COL_L_GRAY);
		sprintf(str, "%d Bytes ", i);
		api_putstrwin(win, win_x - 300, win_y - 25, 
						COL_BLACK, 40, str);
	}
}

void HariMain(void) {
	HWND win;
	int i;
	char *wbuf, *editbuf, cline[40];

	api_cmdline(cline, 40);

	api_initmalloc();
	wbuf = api_malloc(win_x * win_y);
	editbuf = api_malloc(1048);
	for (i = 0;i < 1048;i++) { editbuf[i] = 0; }
	win = api_openwin(wbuf, win_x, win_y, -1, "Negi writer");
	api_putstrwin(win, 10, win_y - 25, 0, 9, "[F3] Save");
	api_putstrwin(win, win_x - 90, win_y - 25, 0, 10, "[ESC] Exit");
	draw_editbox(win, 8, 28, win_x - 16, win_y - 56, editbuf, 1048);
	api_free(wbuf, win_x * win_y);
	api_free(editbuf, 1048);
	api_end();
}
