/* asltest.c							*/

#include <exec/types.h>
#include <exec/execbase.h>
#include <dos/dosasl.h>
#include <dos/dosextens.h>

#include <utility/hooks.h>

#include <libraries/aslbase.h>
#include <clib/dos_protos.h>

#include "proto/graphics.h"
#include "proto/intuition.h"
#include "proto/exec.h"

#define LISTGADID	2222	/* Random gadget ID for DList */

struct AslBase *AslBase;

struct IntuitionBase *IntuitionBase;

struct GfxBase	*GfxBase;

struct NewWindow TWindow = {
    160,45,320,100,		/* LeftEdge,TopEdge,Width,Height */
    0,1,			/* DetailPen,BlockPen	*/
    MENUPICK| NEWSIZE| REFRESHWINDOW| CLOSEWINDOW| ACTIVEWINDOW| GADGETUP| GADGETDOWN|
	MOUSEBUTTONS | VANILLAKEY,
    WINDOWDRAG| WINDOWSIZING |WINDOWCLOSE | SMART_REFRESH| ACTIVATE| WINDOWDEPTH,
    NULL,NULL,"DList Test",	/* FirstGadget, CheckMark, Title  */
    NULL,			/* Screen ( Null)	*/
    NULL,			/* BitMap		*/
    100,50,32767,32767,		/* MinW, MinH, MaxW, MaxH */
    WBENCHSCREEN };		/* Type			*/



/* ULONG __saveds __asm
DLHookFunc(	register __a0 struct Hook	*hook, 
		register __a1 struct DLHookMsg	*msg );
*/

ULONG	DLHookFunc();

struct Hook DL_Hook;

struct NewDList NDL = {
	2,10,-2,-11,
	1,0,1,0,
	8,DLF_SIZEGAD,LISTGADID,
	&DL_Hook
	};

char *Names[] = {
	"One",
	"Two",
	"Three",
	"Four",
	"Five",
	"Six",
	"Seven",
	"Eight",
	"Nine",
	"Ten",
	"Eleven",
	"Twelve",
	"Thirteen",
	"Fourteen",
	"Fifteen",
	"Sixteen",
	"Seventeen",
	"Eighteen",
	"Nineteen",
	"Twenty",
	"Twenty One",
	"Twenty Two",
	"Twenty Three",
	"Twenty Four",
	"Twenty Five",
	"Twenty Six",
	"Twenty Seven",
	"Twenty Eight",
	"Twenty Nine (And around again...)",
	};



main(argc,argv)
int	argc;
char	*argv[];
{
struct FileRequester *foo;
struct FontRequester *bar;

struct Window *Window;
struct IntuiMessage *msg;
struct DList *DList;

struct Gadget *tgad;

struct DListInfo MyDLI;

IntuitionBase = OpenLibrary("intuition.library",34L);
if ( IntuitionBase == NULL ) {
	printf("Your amiga is toast.  Goodnight!\n");
	exit(30);
	}

GfxBase = OpenLibrary("graphics.library",34L);
if ( GfxBase == NULL ) {
	printf("Your amiga is toast.  Goodnight!\n");
	exit(30);
	}

if ( (AslBase = (struct AslBase *)OpenLibrary(AslName,AslVersion)) == 0 ) {
	printf("Can't open asl.library\n");
	exit(20);
	}

printf("Opened asl.library!!!!!\n");

foo = AllocFileRequest();
if ( foo ) {
	printf("Allocated File Request!\n");

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

	foo->rf_Hail = "SAVE FILE AS...";
	foo->rf_FuncFlags |= RFF_SAVE;
	if ( RequestFile(foo) )
		printf("User requested dir (%s) file (%s)\n",foo->rf_Dir,foo->rf_File);
	else
		printf("User cancelled filerequest\n");

	FreeFileRequest(foo);
	printf("Freed File Request!\n");
	}
else {
	printf("Failed to allocate file request\n");
	}

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


bar = AllocFontRequest();
if ( bar ) {
	printf("Allocated Font Requester!\n");

	if ( RequestFont(bar) ) {
		printf("User requested font (%s) size %d style %d flags %d\n",
			bar->fo_Name,bar->fo_YSize,bar->fo_Style,bar->fo_Flags);
		printf("	FrontPen: %d BackPen: %d\n",bar->fo_FrontPen,bar->fo_BackPen);
		}
	else
		printf("User cancelled fontrequest\n");


	FreeFontRequest(bar);
	printf("Freed Font Request!\n");
	}
else {
	printf("Failed to allocate font request\n");
	}

/******* Finally play with DLists *******************************/

printf("Testing DLists\n");

Window = (struct Window *)OpenWindow( &TWindow );

if ( Window ) {
	printf("Opened Window\n");

	SetUpHook( &DL_Hook, DLHookFunc, NULL );
	
	DList = CreateDList( &NDL, Window, NULL );

	if ( DList == NULL ) {
		printf("Failed to allocate DList!\n");
		goto DLFail;
		}

	AddDListItems( DList, 29);
	DrawDList( DList );
	RefreshDList( DList );

	while ( TRUE ) {
		WaitPort( Window->UserPort );
		msg = (struct IntuiMessage *)GetMsg((struct MsgPort *)Window->UserPort);
		switch ( msg->Class ) {
			case CLOSEWINDOW:
				goto eloop;
			case REFRESHWINDOW:
				BeginRefresh( Window );
				RefreshDList( DList );
				EndRefresh( Window, TRUE );
				break;

			case VANILLAKEY:
	switch ( toupper(msg->Code) ) {
		case 'U':	ScrollDList( DList,ST_UPONE );
				break;
		case 'D':	ScrollDList( DList,ST_DOWNONE );
				break;
		case 'T':	ScrollDList( DList,ST_TOP );
				break;
		case 'B':	ScrollDList( DList,ST_BOTTOM );
				break;
		case 'S':	ScrollDList( DList,ST_UPDISPLAY );
				break;
		case 'W':	ScrollDList( DList,ST_DOWNDISPLAY );
				break;
		case 'Q':	goto eloop;

		case '+':	AddDListItems(DList,1);
				break;

		case '-':	AddDListItems(DList,-1);
				break;
		}
	RefreshDList( DList );
	break;

			case GADGETDOWN:
	tgad = (struct Gadget *)msg->IAddress;

	if ( tgad->GadgetID == LISTGADID ) {
		printf("Gad event %d\n", tgad->UserData);
		HandleDListEvent( DList, msg );
		}
	break;		
			default:
				printf("Message class %ld\n",msg->Class);
			}
		ReplyMsg( msg );
		GetDListInfo( DList, &MyDLI );
		printf("NumItems %d TopItem %d DispItems %d\n",MyDLI.NumItems, MyDLI.TopItem,MyDLI.DispItems);
		}
eloop:
	FreeDList( DList );
DLFail:
	CloseWindow( Window );
	}

CloseLibrary( AslBase );
}

int	SelectedItem = -1;

ULONG DLHookFunc( hook, msg )
struct Hook *hook;
struct DLHookMsg *msg;
{
struct RastPort *rp = msg->RPort;
char *curstr;

if ( msg->Type == DH_SELECT ) {
	if (msg->Item == SelectedItem )
		SelectedItem = -1;
	else
		SelectedItem = msg->Item;

	printf("SELECT ID %d	",msg->Item);
	printf("Left %d Top %d W %d H %d\n",msg->LeftEdge,msg->TopEdge,msg->Width,msg->Height);
	printf("MouseX %d MouseY %d\n",msg->MouseX,msg->MouseY);
	}

else if ( msg->Type == DH_RENDER ) {
/*
	printf("RENDER %d	L %d T %d W %d H %d\n",msg->Item,msg->LeftEdge,msg->TopEdge,msg->Width,msg->Height);
*/
	if ( msg->Item == SelectedItem ) {
		SetAPen(rp,rp->FgPen ^ 3);
		SetBPen(rp,rp->BgPen ^ 3);
		}

	curstr = Names[ (msg->Item % 29) ];
	Move(rp,msg->LeftEdge+2,msg->TopEdge+6);
	Text( rp,curstr,TextFit(rp,curstr, msg->Width - 2) );

	SetAPen(rp,rp->BgPen);
	RectFill(rp,msg->LeftEdge,msg->TopEdge,msg->LeftEdge+1,msg->TopEdge+msg->Height-1);
	RectFill(rp,rp->cp_x,msg->TopEdge,msg->LeftEdge+msg->Width-1,msg->TopEdge+msg->Height-1);
	}
return TRUE;
}


TextFit(rp,text, width)
struct RastPort *rp;
char	*text;
int	width;
{
int	len;

len = strlen(text);
while ( len > 0 && (TextLength(rp,text,len) > width) )
	len--;

return len;
}


SetUpHook( hook, c_func, userdata )
struct Hook *hook;
ULONG (*c_func)();
VOID	*userdata;
{
	hook->h_Entry = c_func;
	hook->h_Data = userdata;
}

