//////////////////////////////////////////////////////////////////////////////
//
//  This file is part of the Atari graphical interface for GNU Chess,
//  and is Copyright 1992 by Warwick W. Allison.
//
//  You are free to copy and modify these sources, provided you acknowledge
//  the origin by retaining this notice, and adhere to the conditions
//  of the CHESS General Public License described in the main chess file
//  gnuchess.cc.
//
//////////////////////////////////////////////////////////////////////////////

#include <DoubleBuffer.h>
#include <bool.h>
#include <osbind.h>
#include <vt52.h>

char* const DefaultText="";
const int HelpX=128;
const int HelpY=192;
const int HelpW=192;
const int HelpH=8;

class HelpBox
{
public:
	HelpBox(class HelpBox *Next,int x,int y, int w, int h, char* const HelpText);
	char* const At(int x,int y);

private:
	int X,Y,W,H;
	char* const Text;
	class HelpBox *Next;
};

HelpBox::HelpBox(class HelpBox *next,int x,int y, int w, int h, char* const HelpText) :
		Next(next),X(x),Y(y),W(w),H(h),Text(HelpText)
{ }

char* const HelpBox::At(int x,int y)
{
	if (x>=X && y>=Y && x<X+W && y<Y+H) return Text;
	if (!Next) return DefaultText;
	return Next->At(x,y);
}

static HelpBox* HelpBoxes=0;

void AddHelp(int x,int y, int w, int h, char* Help)
{
	HelpBoxes=new HelpBox(HelpBoxes,x,y,w,h,Help);
}

static char *OldText=0;
static bool HelpShown[2];

void ShowContextHelp(int x, int y)
{
	char* const Text=HelpBoxes->At(x,y);

	if (Text!=OldText) {
		OldText=Text;

		HelpShown[0]=FALSE;
		HelpShown[1]=FALSE;
	}

	if (!HelpShown[Pages->Pulse]) {
		HelpShown[Pages->Pulse]=TRUE;
		Cconws(HOME);
		Cconws(DEL_EOL);
		Cconws(Text);
		long *From=(long*)Logbase();
		long *To=(long*)Pages->Location()+HelpX/16*2+HelpY*40;

		for (int sy=0; sy<HelpH; sy++) {
			for (int sx=0; sx<HelpW/16*2; sx++)
				To[sx]=From[sx];
			To+=40;
			From+=40;
		}
	}
}

void NoteHelpChanged(char* const Help)
{
	if (Help==OldText) {
		HelpShown[0]=FALSE;
		HelpShown[1]=FALSE;
	}
}

void NoHelp()
{
	ShowContextHelp(-1,-1);
}
