/* subs.c - originally modified from a designer produced file - designer (c) William Connor
 */

#include "defs.h"

ULONG BevelTags[] = 
	{
	GTBB_Recessed, TRUE,
	GT_VisualInfo, 0,
	TAG_DONE
	};

struct Window *Win0 = NULL;
APTR Win0VisualInfo;
UWORD Win0ZoomInfo[4] = { 300, 20, 200, 25 };

struct TextAttr topazattr = { (STRPTR)"topaz.font", 8, 0, 65 };
struct TextFont *textfont;

struct Library *DiskfontBase = NULL;
struct Library *GadToolsBase = NULL;
struct GfxBase *GfxBase = NULL;
struct IntuitionBase *IntuitionBase = NULL;

void RendWindowWin0( struct Window *Win )
{
	UWORD offx = Win->BorderLeft;
	UWORD offy = Win->BorderTop;
	if (Win != NULL) 
	{
		SetAPen(Win->RPort, 0);
		RectFill(Win->RPort, offx+1, offy+1, Win->Width-19, Win->Height-3);
		DrawBevelBoxA( Win->RPort, 5+offx,3+offy,Win->Width-offx-27,Win->Height-offy-8, (struct TagItem *) &BevelTags[0]);
	}
}

int OpenWindowWin0( void )
{
struct Screen *Scr;
UWORD offx, offy;
if (Win0 == NULL)
{
	if (NULL != (Scr = LockPubScreen(NULL)))
	{
		offx = Scr->WBorLeft;
		offy = Scr->WBorTop + Scr->Font->ta_YSize+1;
		Win0ZoomInfo[3] = offy + 25;
		if (NULL != ( Win0VisualInfo = GetVisualInfoA( Scr, NULL)))
		{
			if (NULL != (Win0 = OpenWindowTags( NULL, WA_Left, 300,
							WA_Top, 20,
							WA_Width, 300+offx,
							WA_Height, 150+offy,
							WA_Title, "Little Arexx proggy (LARP)",
							WA_MinWidth, offx+200,
							WA_MinHeight, offy+25,
							WA_MaxWidth, 65535,
							WA_MaxHeight, 65535,
							WA_SizeGadget, TRUE,
							WA_DragBar, TRUE,
							WA_DepthGadget, TRUE,
							WA_CloseGadget, TRUE,
							WA_Activate, TRUE,
							WA_SmartRefresh, TRUE,
							WA_AutoAdjust, TRUE,
							WA_Zoom, Win0ZoomInfo,
							WA_IDCMP,518,
							TAG_END)))
			{
				UnlockPubScreen( NULL, Scr);
				BevelTags[3] = (ULONG)Win0VisualInfo;
				textfont = OpenFont( &topazattr);
				SetFont(Win0->RPort, textfont);
				RendWindowWin0( Win0 );
				return( 0L );
			}
			FreeVisualInfo( Win0VisualInfo );
		}
		UnlockPubScreen( NULL, Scr);
	}
}
else
{
	WindowToFront(Win0);
	ActivateWindow(Win0);
	return( 0L );
}
return( 1L );
}

void CloseWindowWin0( void )
{
	if (Win0 != NULL)
	{
		CloseWindowSafely( Win0);
		Win0 = NULL;
		if(Win0VisualInfo!=0)
		{
			FreeVisualInfo( Win0VisualInfo);
		}
	}
}

int OpenLibs( void )
{
if ( NULL != (DiskfontBase = OpenLibrary("diskfont.library" , 36)))
	if ( NULL != (GadToolsBase = OpenLibrary("gadtools.library" , 37)))
		if ( NULL != (GfxBase = (struct GfxBase * )OpenLibrary("graphics.library" , 37)))
			if ( NULL != (IntuitionBase = (struct IntuitionBase * )OpenLibrary("intuition.library" , 37)))
				return( 0L );
CloseLibs();
return( 1L );
}

void CloseLibs( void )
{
if (NULL != DiskfontBase )
	CloseLibrary( DiskfontBase );
if (NULL != GadToolsBase )
	CloseLibrary( GadToolsBase );
if (NULL != GfxBase )
	CloseLibrary( ( struct Library * )GfxBase );
if (NULL != IntuitionBase )
	CloseLibrary( ( struct Library * )IntuitionBase );
}

void StripIntuiMessages( struct MsgPort *mp, struct Window *win)
{
struct IntuiMessage *msg;
struct Node *succ;
msg = (struct IntuiMessage *)mp->mp_MsgList.lh_Head;
while (succ = msg->ExecMessage.mn_Node.ln_Succ)
	{
	if (msg->IDCMPWindow == win)
		{
		Remove((struct Node *)msg);
		ReplyMsg((struct Message *)msg);
		}
	msg = (struct IntuiMessage *)succ;
	}
}

void CloseWindowSafely( struct Window *win)
{
	Forbid();
	StripIntuiMessages( win->UserPort, win);
	win->UserPort = NULL;
	ModifyIDCMP( win, 0L);
	Permit();
	CloseWindow( win);
}
