/***   [wconf.c]
*
*	確認ウィンドウ		(C)ささがわ
*
*	For GNU C Compiler (GCC)   Version 1.39
*
***/

#include <stdlib.h>
#include "graph.h"
#include "mos.h"
#include "window.h"
#include "others.h"

#define WH_TITLE	1
#define WH_ON		2
#define WH_CAN		3
#define WH_OTHER	0

extern int	PAL_Black;
static int	wx, wy;

static int	SUB_cancel(void);
static void	SUB_move(int, int, const char *, const char **);
static int	SUB_on(void);
static void	Draw_window(const char *, const char **);
static int	Where(int, int);

int WIND_confirm(const char *title, const char **phrase) {
	int		ret = 0;
	struct RECT	a, b;
	
	wx = 210;
	wy = 179;
	a.x1 = 319;	a.y1 = 259;
	a.x2 = 320;	a.y2 = 260;
	b.x1 = wx;	b.y1 = wy;
	b.x2 = wx + 219;	b.y2 = wy + 121;
	afterImage(&a, &b);
	
	Draw_window(title, phrase);
	while (!ret) {
		char	mb;
		int		mx, my;
		
		CLOCK(0);
		if (MOS_rdpos(&mb, &mx, &my), !(mb & 1))
			continue;
		
		switch (Where(mx, my)) {
			case WH_CAN:
				if (SUB_cancel())	ret = -1;
				break;
			
			case WH_TITLE:
				SUB_move(mx, my, title, phrase);
				break;
			
			case WH_ON:
				if (SUB_on())	ret = 1;
				break;
			
			default:
				while (MOS_rdpos(&mb, &mx, &my), mb & 1);
				break;
		}
	}
	
	return (ret > 0 ? 1 : 0);
}

static int SUB_cancel(void) {
	return Button(wx + 6, wy + 6, wx + 25, wy + 25);
}

static void SUB_move(int x, int y, const char *t, const char **p) {
	struct RECT	s, w;
	
	w.x1 = wx;			w.y1 = wy;
	w.x2 = wx + 219;	w.y2 = wy + 121;
	s.x1 = 0;	s.y1 = 40;
	s.x2 = 639;	s.y2 = 463;
	if (dragWindow(x, y, &w, &s, 0, 0)) {
		wx = w.x1;	wy = w.y1;
		MOS_disp(0);
		EGB_cls(0);
		MOS_disp(1);
		Draw_window(t, p);
	}
}

static int SUB_on(void) {
	return Button(wx + 123, wy + 88, wx + 202, wy + 109);
}

static void Draw_window(const char *t, const char **p) {
	int		i;
	struct opnwin_t	opw;
	
	opw.title = t;
	opw.x1 = wx;
	opw.y1 = wy;
	opw.x2 = opw.x1 + 219;
	opw.y2 = opw.y1 + 121;
	opw.shdw = 1;
	opw.canb = 1;
	opw.nopt = 0;
	opw.wopt = NULL;
	opw.expb = 0;
	opw.ord = 0;
	
	MOS_disp(0);
	drawWindow(&opw);
	if (p[1][0] == '\0')
		EGB_str2(p[0], wx + 14, wy + 64, PAL_Black);
	else {
		EGB_str2(p[0], wx + 14, wy + 54, PAL_Black);
		EGB_str2(p[1], wx + 14, wy + 74, PAL_Black);
	}
	DrawButton(1, wx + 122, wy + 87, wx + 203, wy + 110);
	for (i = 0; i < 2; i++)	EGB_str2(" 実    行 ", wx + 123 + i, wy + 106, PAL_Black);
	MOS_disp(1);
}

static int Where(int x, int y) {
	int		ret;
	
	x -= wx;
	y -= wy;
	if (5 < x && x < 26 && 5 < y && y < 26)
		ret = WH_CAN;
	else if (26 < x && x < 214 && 5 < y && y < 26)
		ret = WH_TITLE;
	else if (122 < x && x < 203 && 87 < y && y < 110)
		ret = WH_ON;
	else
		ret = WH_OTHER;
	
	return ret;
}
