#include    <stdio.h>
#include    <stdlib.h>
#include    "defs.h"

typedef struct _EP {
    struct _EP  *next;
    int         actno;
    int         actpos;
    long        pos;
    long        line;
} EDT_PTR;

typedef struct _WINDFP {
    struct _WINDFP  *nxt_wfp;
    EDT_PTR  *edt_top_wfp;
    LONG    top_ptr_wfp;
    LONG    lin_ptr_wfp;
    LONG    ent_ptr_wfp;
    LONG    btm_ptr_wfp;
    int     Cur_X_wfp,Cur_Y_wfp;
    BOOL    Wrt_flg_wfp;
    int     Line_no_wfp;
    short   Line_X_wfp,Line_Y_wfp;
    short   Woff_X_wfp,Woff_Y_wfp; 
    short   Wsiz_X_wfp,Wsiz_Y_wfp;
    short   Wscr_X_wfp,Wscr_Y_wfp;
    short   Wind_flg_wfp,dlp_flg_wfp;
    short   WCol_wfp;
    char    name_wfp[80];
} WINDFP;

extern EDT_PTR  *edt_top;
extern int      Line_no;
extern char     *file_name;
extern UCHAR    vram[];
extern short    Line_X,Line_Y;
extern short	Woff_X,Woff_Y; 
extern short	Wsiz_X,Wsiz_Y;
extern short	Wscr_X,Wscr_Y;
extern short	Wind_flg,dlp_flg;
extern short    COLOR;
extern char     *SPCSTR;

extern void  strcpy();
extern void  memcpy();
extern void  memset();
extern void  Buf_init();
extern void  Set_file_name();
extern void  Set2_file_name();
extern int   New_link();
extern void  Kill_link();
extern void  Set_vram();
extern void  Dsp_vram();
extern void  Dsp_wrt_flg();
extern void  Dsp_free();
extern void  wrtstr();
extern int   Get_key();
extern int   Sel_menu();

static WINDFP   *topwfp=NULL;
static short    ReSize_flg=FALSE;

int     New_wind(file)
char    *file;
{
    WINDFP  *wfp;
    char    *p;

    if ( (wfp = (WINDFP *)malloc(sizeof(WINDFP))) == NULL )
	return ERR;

    wfp->nxt_wfp = NULL;
    wfp->edt_top_wfp = edt_top;
    wfp->top_ptr_wfp = top_ptr;
    wfp->lin_ptr_wfp = lin_ptr;
    wfp->ent_ptr_wfp = ent_ptr;
    wfp->btm_ptr_wfp = btm_ptr;
    wfp->Wrt_flg_wfp = Wrt_flg;
    wfp->Line_no_wfp = Line_no;
    wfp->Cur_X_wfp = Cur_X; wfp->Cur_Y_wfp = Cur_Y;
    wfp->Line_X_wfp = Line_X; wfp->Line_Y_wfp = Line_Y;
    wfp->Woff_X_wfp = Woff_X; wfp->Woff_Y_wfp = Woff_Y;
    wfp->Wscr_X_wfp = Wscr_X; wfp->Wscr_Y_wfp = Wscr_Y;
    wfp->Wsiz_X_wfp = Wsiz_X; wfp->Wsiz_Y_wfp = Wsiz_Y;
    wfp->Wind_flg_wfp = Wind_flg; wfp->dlp_flg_wfp = dlp_flg;
    wfp->WCol_wfp = COLOR;
    if ( (p = file_name) == NULL )
	p = "DUMMY.TED";
    strcpy(wfp->name_wfp,p);

    if ( New_link() != FALSE ) {
	free(wfp);
	return ERR;
    }
    Buf_init(file);
    Set_file_name(file);
    Woff_X = Woff_Y = 0;
    Wsiz_X = MAX_X; Wsiz_Y = MAX_Y;
    Wind_flg = dlp_flg = FALSE;
    COLOR = 7;

    if ( topwfp != NULL )
	wfp->nxt_wfp = topwfp;
    topwfp = wfp;

    return FALSE;
}
void	Chng_wind()
{
    WINDFP  wfp,*p,*s;
    char    *dm;

    if ( topwfp == NULL )
	return;

    wfp.nxt_wfp = topwfp->nxt_wfp;
    wfp.edt_top_wfp = edt_top;
    wfp.top_ptr_wfp = top_ptr;
    wfp.lin_ptr_wfp = lin_ptr;
    wfp.ent_ptr_wfp = ent_ptr;
    wfp.btm_ptr_wfp = btm_ptr;
    wfp.Wrt_flg_wfp = Wrt_flg;
    wfp.Line_no_wfp = Line_no;
    wfp.Cur_X_wfp = Cur_X; wfp.Cur_Y_wfp = Cur_Y;
    wfp.Line_X_wfp = Line_X; wfp.Line_Y_wfp = Line_Y;
    wfp.Woff_X_wfp = Woff_X; wfp.Woff_Y_wfp = Woff_Y;
    wfp.Wscr_X_wfp = Wscr_X; wfp.Wscr_Y_wfp = Wscr_Y;
    wfp.Wsiz_X_wfp = Wsiz_X; wfp.Wsiz_Y_wfp = Wsiz_Y;
    wfp.Wind_flg_wfp = Wind_flg; wfp.dlp_flg_wfp = dlp_flg;
    wfp.WCol_wfp = COLOR;
    if ( (dm = file_name) == NULL )
	dm = "DUMMY.TED";
    strcpy(wfp.name_wfp,dm);

    top_ptr = topwfp->top_ptr_wfp;
    lin_ptr = topwfp->lin_ptr_wfp;
    ent_ptr = topwfp->ent_ptr_wfp;
    btm_ptr = topwfp->btm_ptr_wfp;
    Cur_X = topwfp->Cur_X_wfp;
    Cur_Y = topwfp->Cur_Y_wfp;
    Wrt_flg = topwfp->Wrt_flg_wfp;
    Line_no = topwfp->Line_no_wfp;
    Line_X = topwfp->Line_X_wfp; Line_Y = topwfp->Line_Y_wfp;
    Woff_X = topwfp->Woff_X_wfp; Woff_Y = topwfp->Woff_Y_wfp;
    Wscr_X = topwfp->Wscr_X_wfp; Wscr_Y = topwfp->Wscr_Y_wfp;
    Wsiz_X = topwfp->Wsiz_X_wfp; Wsiz_Y = topwfp->Wsiz_Y_wfp;
    Wind_flg = topwfp->Wind_flg_wfp; dlp_flg = topwfp->dlp_flg_wfp;
    COLOR = topwfp->WCol_wfp;
    if ( ReSize_flg == FALSE )
        Set_file_name(topwfp->name_wfp);
    else
        Set2_file_name(topwfp->name_wfp);
    edt_top = topwfp->edt_top_wfp;
    if ( Wind_flg == ERR )
	Wind_flg = TRUE;

    memcpy(topwfp,&wfp,sizeof(WINDFP));

    if ( topwfp->nxt_wfp != NULL ) {
        for ( s = p = topwfp->nxt_wfp ; (p = s->nxt_wfp) != NULL ; s = p );
	s->nxt_wfp = topwfp;
	p = topwfp->nxt_wfp;
	topwfp->nxt_wfp = NULL;
	topwfp = p;
    }
}
int	Del_wind()
{
    WINDFP  *p;
    EDT_PTR *bfp;

    if ( topwfp == NULL )
	return ERR;

    bfp = edt_top;

    top_ptr = topwfp->top_ptr_wfp;
    lin_ptr = topwfp->lin_ptr_wfp;
    ent_ptr = topwfp->ent_ptr_wfp;
    btm_ptr = topwfp->btm_ptr_wfp;
    Cur_X = topwfp->Cur_X_wfp;
    Cur_Y = topwfp->Cur_Y_wfp;
    Wrt_flg = topwfp->Wrt_flg_wfp;
    Line_no = topwfp->Line_no_wfp;
    Line_X = topwfp->Line_X_wfp; Line_Y = topwfp->Line_Y_wfp;
    Woff_X = topwfp->Woff_X_wfp; Woff_Y = topwfp->Woff_Y_wfp;
    Wscr_X = topwfp->Wscr_X_wfp; Wscr_Y = topwfp->Wscr_Y_wfp;
    Wsiz_X = topwfp->Wsiz_X_wfp; Wsiz_Y = topwfp->Wsiz_Y_wfp;
    Wind_flg = topwfp->Wind_flg_wfp; dlp_flg = topwfp->dlp_flg_wfp;
    COLOR = topwfp->WCol_wfp;
    Set_file_name(topwfp->name_wfp);
    edt_top = topwfp->edt_top_wfp;
    if ( Wind_flg == ERR )
	Wind_flg = TRUE;

    p = topwfp->nxt_wfp;
    free(topwfp);
    topwfp = p;

    Kill_link(bfp);

    Set_vram();
    Dsp_vram(vram);
    Dsp_wrt_flg();
    Dsp_free();

    return FALSE;
}
int	Wind_count()
{
    int     i;
    WINDFP  *p;

    for ( p = topwfp,i = 0 ; p != NULL ; i++ )
	p = p->nxt_wfp;
    return i;
}
void	ReDsp_wind()
{
    int    i;

    memset(vram,0,MAX_X*2*MAX_Y);
    ReSize_flg = TRUE;
    for ( i = Wind_count(); i > 0 ; i-- ) {
	Chng_wind();
        Set_vram();
    }
    if ( topwfp != NULL )
	Chng_wind();
    ReSize_flg = FALSE;
    if ( Wind_flg == ERR )
	Wind_flg = TRUE;
    Set_vram();
    Dsp_vram(vram);
    Dsp_wrt_flg();
    Dsp_free();
}
void	ReSize_wind()
{
    int     ch,ec;

    wrtstr("ｳｲﾝﾄﾞ･ｻｲｽﾞの変更を行います",30,1,0x15);
    for ( ; ; ) {
	ch = Get_key(&ec);
	if ( ec == 0x4D00 ) {
	    if ( --Woff_Y < 0 )
		Woff_Y = 0;
	    else
		Wsiz_Y++;
	} else if ( ec == 0x5000 ) {
	    if ( Wsiz_Y > 5 ) {
		Woff_Y++;
		Wsiz_Y--;
	    }
	} else if ( ec == 0x4F00 ) {
	    if ( (Woff_X-=4) < 0 )
		Woff_X = 0;
	    else
		Wsiz_X+=4;
	} else if ( ec == 0x5100 ) {
	    if ( Wsiz_X > 20 ) {
		Woff_X+=4;
		Wsiz_X-=4;
	    }
	} else if ( ec == 0x4D04 ) {
	    if ( --Wsiz_Y < 5 )
		Wsiz_Y = 5;
	} else if ( ec == 0x5004 ) {
	    Wsiz_Y++;
	    if ( (Woff_Y + Wsiz_Y) > MAX_Y )
		Wsiz_Y = MAX_Y - Woff_Y;
	} else if ( ec == 0x4F04 ) {
	    if ( (Wsiz_X-=4) < 20 )
		Wsiz_X = 20;
	} else if ( ec == 0x5104 ) {
	    Wsiz_X+=4;
	    if ( (Woff_X + Wsiz_X) >= MAX_X )
		Wsiz_X = MAX_X - Woff_X;
	} else if ( ec == 0x7300 || ec == 0x7200 ||
		    ec == 0x0100 || ec == 0x1D00 || ec == 0x4500 ) {
	    break;
	}
	if ( dlp_flg == FALSE &&
	     Woff_X == 0 && Woff_Y == 0 &&
	     Wsiz_X == MAX_X && Wsiz_Y == MAX_Y )
	    Wind_flg = FALSE;
	else
	    Wind_flg = TRUE;
	ReDsp_wind();
    }
    wrtstr(SPCSTR,30,1,0x1F);
}
void	ChgCol_wind()
{
    int    col;
    static char  *menu[]={
        "1   青   色  ",
        "2   赤   色  ",
        "3   紫   色  ",
        "4   緑   色  ",
        "5   水   色  ",
        "6   黄   色  ",
        "7   白   色  ",
        NULL };

    col = COLOR-1;
    if ( Sel_menu(menu,30,5,&col) != FALSE )
        return;
    COLOR = col+1;
    if ( Wind_flg == ERR )
	Wind_flg = TRUE;
}
