/* asltest.c							*/

#include <dos/dosasl.h>
#include <dos/dosextens.h>

#include <exec/execbase.h>

#include <utility/hooks.h>

#include <libraries/asl.h>

#include "clib/dos_protos.h"
#include "clib/asl_protos.h"

#include "pragma/dos_lib.h"
#include "pragma/graphics_lib.h"
#include "pragma/intuition_lib.h"
#include "pragma/exec_lib.h"
#include "pragma/asl_lib.h"

struct Library *AslBase;
struct IntuitionBase *IntuitionBase;
struct GfxBase	*GfxBase;
struct Library	*DiskfontBase;

struct Screen	*MyScreen;
struct Window	*MyWindow;

struct TextFont	*MyFont,*OldFont;

struct FileRequester *FileRequester;
struct FontRequester *FontRequester;

void punt( int );

/************************************************************************/

struct TextAttr	MyTextAttr = { "topaz.font",TOPAZ_EIGHTY,0,0 };

struct NewScreen MyNewScreen = 
	{
	0,50,320,200,5,
	1,2,
	0,CUSTOMSCREEN,&MyTextAttr,
	"My Custom Screen",
	0,0
	};

struct NewWindow MyNewWindow =
	{
	10,10,300,150,
	1,2,
	REFRESHWINDOW+NEWSIZE+ACTIVEWINDOW+CLOSEWINDOW+VANILLAKEY,
	ACTIVEWINDOW+SMART_REFRESH+WINDOWDEPTH+WINDOWSIZING+WINDOWDRAG+WINDOWCLOSE,
	0,0,
	"My Window",
	0,			/* Custom screen later...	*/
	0,
	20,20,1000,1000,
	CUSTOMSCREEN
	};

/************************************************************************/



struct MeTags {
	LONG	tag;
	VOID	*tagitem;
	};

struct MeTags FileTags[] = {
	ASL_Hail,"Hail Tags!",
	ASL_FontStyle,(void *)FSF_ITALIC,
	ASL_FrontPen,(void *)3,
	ASL_BackPen,(void *)2,
	TAG_DONE,
	};

struct MeTags FontTags[] = {
	ASL_FuncFlags,(void *)(FONF_FixedWidth),
	ASL_MinHeight,(void *)8,
	ASL_MaxHeight,(void *)9,
	TAG_DONE
	};

char *ModeList[] = {
	"MyModes",
	"MyJam1",
	"MyJam2",
	"MyComp",
	"MyInverse1",
	"MyInverse2",
	NULL
	};
	

struct MeTags FontTags2[] = {
	ASL_Window,NULL,
	ASL_Hail,"ASLib Font Test",
	ASL_OKText,"MyOK",
	ASL_MaxHeight,(void *)30,
	ASL_FuncFlags,(void *)(FONF_Style+FONF_BackColor+FONF_FrontColor+FONF_DrawMode),
	ASL_ModeList,&ModeList,
	TAG_DONE
	};

main(argc,argv)
int	argc;
char	*argv[];
{
struct IntuiMessage *msg;
ULONG	class,code;

IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",36);
if ( IntuitionBase == NULL ) {
	punt(30);
	}

GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",36);
if ( GfxBase == NULL ) {
	punt(31);
	}

if ( (DiskfontBase = (struct Library *)OpenLibrary("diskfont.library",34)) == 0 ) {
	printf("Can't open diskfont.library\n");
	punt(32);
	}

if ( (AslBase = (struct Library *)OpenLibrary(AslName,34)) == 0 ) {
	printf("Can't open asl.library\n");
	punt(33);
	}

FileRequester = AllocFileRequest();
if ( FileRequester == NULL ) {
	printf("Failed to allocate file request\n");
	punt(34);
	}

if ( RequestFile(FileRequester) )
	printf("User requested dir (%s) file (%s)\n",FileRequester->rf_Dir,FileRequester->rf_File);
else
	printf("User cancelled filerequest\n");

if ( AslRequest( FileRequester, &FileTags ) )
	printf("User requested dir (%s) file (%s)\n",FileRequester->rf_Dir,FileRequester->rf_File);
else
	printf("User cancelled filerequest\n");

/******** Now do the same for font requests *********/

FontRequester = (struct FontRequester *)AllocAslRequest(ASL_FontRequest,&FontTags);

if ( FontRequester == NULL ) {
	printf("Failed to allocate font request\n");
	punt(35);
	}

printf("Allocated Font Requester!\n");

if ( ! AslRequest(FontRequester, NULL) )
	printf("CANCELLED:     ");

printf("User requested font (%s) size %d style %x flags %x\n	FrontPen: %d BackPen: %d Mode %d\n",
	FontRequester->fo_Attr.ta_Name,
	FontRequester->fo_Attr.ta_YSize,
	FontRequester->fo_Attr.ta_Style,
	FontRequester->fo_Attr.ta_Flags,
	FontRequester->fo_FrontPen,
	FontRequester->fo_BackPen,
	FontRequester->fo_DrawMode );

MyScreen = (struct Screen *)OpenScreen(&MyNewScreen);
if ( MyScreen == 0L )
	{
	printf("Can't open custom screen, Oh No!!!!\n");
	punt(1);
	}

MyNewWindow.Screen = MyScreen;
MyWindow = (struct Window *)OpenWindow(&MyNewWindow);
if ( MyWindow == 0L )
	{
	printf("Can't open custom window, Oh Doubleno!!!\n");
	punt(2);
	}

OldFont = MyWindow->RPort->Font;

FontTags2[0].tagitem = MyWindow;

Again:

if ( ! AslRequest(FontRequester, &FontTags2) ) {
	printf("CANCELLED:     ");
	punt(0);
	}

printf("User requested font (%s) size %d style %x flags %x\n	FrontPen: %d BackPen: %d Mode %d\n",
	FontRequester->fo_Attr.ta_Name,
	FontRequester->fo_Attr.ta_YSize,
	FontRequester->fo_Attr.ta_Style,
	FontRequester->fo_Attr.ta_Flags,
	FontRequester->fo_FrontPen,
	FontRequester->fo_BackPen,
	FontRequester->fo_DrawMode );

ActivateWindow(MyWindow);

MyFont = (struct TextFont *)OpenFont(&FontRequester->fo_Attr);

if ( MyFont == NULL ) {
	printf("Font not in memory, checking disk...\n");
	}

else if ( MyFont->tf_YSize != FontRequester->fo_Attr.ta_YSize )
	{
	printf("Font not of requested size - reopening via disk!!!\n");
	CloseFont(MyFont);
	MyFont = NULL;
	}

if ( MyFont == NULL ) {
	MyFont = (struct TextFont *)OpenDiskFont(&FontRequester->fo_Attr);
	if ( MyFont == NULL ) {
		printf("Not on disk either.  Wieird\n");
		punt(51);
		}
	}

printf("Got font!\n");

Move(MyWindow->RPort,100,100);
SetFont(MyWindow->RPort,MyFont);
SetSoftStyle(MyWindow->RPort,FontRequester->fo_Attr.ta_Style,-1);

SetAPen(MyWindow->RPort,FontRequester->fo_FrontPen);
SetBPen(MyWindow->RPort,FontRequester->fo_BackPen);
SetDrMd(MyWindow->RPort,FontRequester->fo_DrawMode);

Text(MyWindow->RPort,"Abcdefg!!!12345",15);

CloseFont(MyFont);
MyFont = NULL;

while ( TRUE ) {
	WaitPort( MyWindow->UserPort );
	msg = (struct IntuiMessage *) GetMsg(MyWindow->UserPort);
	class = msg->Class;
	code = msg->Code;
	ReplyMsg(msg);
	if ( class == CLOSEWINDOW )
		punt( 0 );
	if ( class == VANILLAKEY ) {
		if ( code == 'Q' ) punt(0);
		else goto	Again;
		}
	}

}


void punt(rc)
int	rc;
{
if ( FileRequester ) {
	FreeAslRequest(FileRequester);
	printf("Freed File Request!\n");
	}

if ( FontRequester ) {
	FreeAslRequest(FontRequester);
	printf("Freed Font Request!\n");
	}

if (MyFont)	CloseFont(MyFont);

if (MyWindow) {
	SetFont(MyWindow->RPort,OldFont);
	CloseWindow(MyWindow);
	}
if (MyScreen)	CloseScreen(MyScreen);

if (DiskfontBase)	CloseLibrary(DiskfontBase);
if (AslBase)		CloseLibrary(AslBase);
if (GfxBase)		CloseLibrary(GfxBase);
if (IntuitionBase)	CloseLibrary(IntuitionBase);

exit (rc);
}



