#include <OSIncludes.h>

#pragma header

extern struct IntuitionBase *IntuitionBase;
extern struct GfxBase *GfxBase;

void CalcVisibleSize(struct Screen *scr,WORD *width, WORD *height)
{
	struct TagItem cmtags[]={VTAG_VIEWPORTEXTRA_GET,0,
									 TAG_DONE};

	struct Rectangle myrect;
	struct ViewPortExtra *vpe;
	ULONG displayid;
	WORD x,y;

	if(!VideoControl(scr->ViewPort.ColorMap,cmtags))
	{
		vpe=(struct ViewPortExtra *)cmtags[0].ti_Data;
	} else {
		vpe=scr->ViewPort.ColorMap->cm_vpe;
		if(!vpe)
		{
			vpe=(struct ViewPortExtra *)GfxLookUp(&scr->ViewPort);
		}
	}

	if(vpe && ((displayid=GetVPModeID(&scr->ViewPort)) != INVALID_ID))
	{
		QueryOverscan(displayid,&myrect,OSCAN_TEXT);

		x=vpe->DisplayClip.MaxX - vpe->DisplayClip.MinX + 1;
		y=vpe->DisplayClip.MaxY - vpe->DisplayClip.MinY + 1;

		if(x < (myrect.MaxX - myrect.MinX + 1))
		{
			x=myrect.MaxX - myrect.MinX + 1;
		}
		
		if(y < (myrect.MaxY - myrect. MinY + 1))
		{
			y=myrect.MaxY - myrect.MinY + 1;
		}
		
	} else {

		x=scr->Width;
		y=scr->Height;

	}
	
	*width = x;
	*height = y;

}

void CalcCenteredWin(struct Screen *scr,WORD winwidth,WORD winheight,WORD *left,WORD *top)
{
	WORD x,y;

	CalcVisibleSize(scr,&x,&y);
	
	x=(x - winwidth) / 2;
	y=(y - winheight) / 2;

	x -= scr->LeftEdge;
	y -= scr->TopEdge;

	if (x<0) x=0;
	if (y<0) y=0;
	
	*left=x;
	*top=y;
}

