*******************************************************************************
* WB2.x+ ASL Font-Requester Demonstration	   Copyright İ 1994 2-Cool/LSd!
* ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
* $Release	: 0.1 $
* $Revision	: 1.01 $
* $Date		: 12-Jan-94 $
*
* $Author(s)	: Written by 2-Cool/LSd! (In MC680x0 Assembly under Asm-One)
* $Note(s)	: Position Independacy off a4 - (WARNING: DON`T TRASH A4!)
*		: Execbase Ptr is cached so that if running in fastmem access
*		: to execbase ptr is faster. Also included a Version String.
*
* $Purpose	: Quick and simple demonstration of using the Amiga ASL Font-
*		: Requester to promp the user for fontname, sizes, styles and
*		: colours. Example for both loading and saving of files...
*******************************************************************************
*-------------- Define Variables...

_RSStart:	rsreset
_ExecBase	rs.l	1			;ExecBase base
_AslBase	rs.l	1			;Asl lib base
_DOSBase	rs.l	1			;DOS lib base
Asl_Request	rs.l	1			;Requester struct
PathBuffer	rs.b	200			;filepath string buffer
_RSLen		rs.b	0

AbsExecBase	=	4

*-------------- Definition Includes...

		incdir	'include:'
		include	intuition/intuition.i
		include	libraries/dosextens.i
		include	libraries/intuition_lib.i
		include	libraries/dos_lib.i
		include	libraries/graphics_lib.i
		include	exec/exec.i
		include	libraries/exec_lib.i
		include	libraries/asl_lib.i
		include	libraries/asl.i
		
CALL		macro
		jsr	_LVO\1(a6)
		endm

;		opt	o+,c+,a+,p+		;options for devpac users
		
*******************************************************************************
*-------------- Allocate our Variables (RS.) Area

ProgStart:	move.l	#_RSLen,d0		;length for our "RS.Var" store
		move.l	#MEMF_PUBLIC+MEMF_CLEAR,d1 ;we want public mem, cleared
		move.l	(AbsExecBase).w,a6	;get execbase
		CALL	AllocMem		;allocate it...
		move.l	d0,a4			;ptr to our RS.Variables base
		bne.s	RSOkay			;did the alloc fail, if so exit
		moveq	#-1,d0			;memory failed...
		rts				;peaceful exit..

*-------------- Place Version String here so it can be quickly found.

Version:	dc.b	"$VER: ASL-Reqtest v1.00 (12-Jan-94)",0
		even

RSOkay		move.l	a6,(a4)			;cache execptr in Public so if
						;we are in fastmem were faster
*-------------- Open DOS Library

		moveq	#36,d0			;set lib version
  		lea	Dosname(pc),a1		;lib name in a1
  		CALL	OpenLibrary		;try to open library
  		move.l	d0,_DOSBase(a4)		;store lib base
  		beq.b	ShutDown		;cleanup and quit if fail

*-------------- Open ASL Library

		moveq	#36,d0			;set lib version
  		lea	Aslname(pc),a1		;lib name in a1
  		CALL	OpenLibrary		;try to open library
  		move.l	d0,_AslBase(a4)		;store lib base
  		beq.b	ShutDown		;cleanup and quit if fail

*-------------- Allocate ASL Requester

		move.l	d0,a6			;get asl base in a6
		moveq	#ASL_FontRequest,d0	;requester type
		lea	MainTags(pc),a0		;taglist
		CALL	AllocAslRequest		;allocate asl request struct
		move.l	d0,Asl_Request(a4)	;save request struct
		beq.b	ShutDown

*-------------- Example ASL Requester 'LOAD'

		lea	LoadTags(pc),a1		;address of taglist
		lea	LoadText(pc),a2		;ptr to requester hail text
		bsr.b	RequestASL
;		beq.s	Cancelled

*-------------- Example ASL Requester 'SAVE'

		lea	SaveTags(pc),a1		;address of taglist
		lea	SaveText(pc),a2		;ptr to requester hail text
		bsr.b	RequestASL
;		beq.s	Cancelled

*******************************************************************************
* Shutdown() - Free all allocated resources & exit program
* ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
* Frees all previously allocated resources, sanity checks are made so only
* allocated memory/open libraries will be removed.
*******************************************************************************
*-------------- Free Allocated ASL File-Requester

ShutDown	move.l	Asl_Request(a4),d0	;get request struct
		beq.s	FreeASL	
		move.l	d0,a0
		move.l	_AslBase(a4),a6
		CALL	FreeAslRequest

*-------------- Free ASL Library

FreeASL:	move.l	_AslBase(a4),d0		;asl lib base in a1
		beq.s	FreeDOS
		move.l	d0,a1
		move.l	(a4),a6			;get execbase
 		CALL	CloseLibrary		;close asl

*-------------- Free DOS Library

FreeDOS:	move.l	_DOSBase(a4),d0		;dos lib base in a1
		beq.s	RSFreeMem
		move.l	d0,a1
 	 	CALL	CloseLibrary		;close intuition

*-------------- Free RS Variables Area

RSFreeMem:	move.l	a4,a1			;ptr to our RS.Variables base
		move.l	#_RSLen,d0		;no. of bytes to free
		move.l	(a4),a6			;get execbase
		CALL	FreeMem			;free the memory

RSFailed:	moveq	#0,d0			;no return code
		rts				;exit...


*******************************************************************************
* RequestASL <NON MODIFIABLE>
* ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
* Prompts user to select a filename using the ASL Requester
*
* $Inputs	: a1.l=Requester Tag Structure (PTR)
*		: a2.l=Requester Hail Text-String (PTR)
* $Outputs	: a0.l=Pathname of selected file, or Points to ZERO if failed
*		: d0.b=Flags 0,cancelled, -1,selected
*
* $SR-Flags	: Z-Flag Set if requester cancelled.
*******************************************************************************

RequestASL:	move.l	a2,4(a1)		;hail textptr into tag struct
		move.l	Asl_Request(a4),a0	;address of requester struc
		move.l	_AslBase(a4),a6		;get asl base
		CALL	AslRequest		;do request
		tst.l	d0			;test d0
		beq.s	cancelled		;branch if cancelled

		lea	PathBuffer(a4),a5
		move.l	Asl_Request(a4),a0	;address of requester struc
		move.l	rf_Dir(a0),a1		;get dir string
		move.l	a5,a2			;get output buffer
		moveq	#56,d0			;set max size
copy		move.b	(a1)+,(a2)+		;copy bytes
		dbeq	d0,copy			;until end of string or buffer
	
		move.l	a5,d1			;get dir name (copy)
		move.l	rf_File(a0),d2		;get file name
		move.l	#200,d3			;size of buffer
		move.l	_DOSBase(a4),a6		;get dos base
		CALL	AddPart			;create full pathname
		tst.l	d0			;test for overflow
		beq.s	cancelled		;branch on error
	
		moveq	#-1,d0			;initialise count
		move.l	a5,a0			;address of buffer	
checksize:	addq.w	#1,d0			;decrement counter
		tst.b	0(a0,d0.w)		;test for zero terminator
		bne.s	checksize		;branch if not found
	
		cmpi.w	#37,d0			;is count too large
		ble.s	sizeok			;branch if not
		sub.w	#37,d0			;subtract count from numchars
		add.w	d0,a0			;add to buff adr so eostr shown
sizeok		st.b	d0			;d0=$FF.b (Clear Z-Flag)
		rts

cancelled	suba.l	a0,a0			;no filename, clear ptr
		sf.b	d0			;d0=$00.b (Set Z-Flag)
		rts

*******************************************************************************

MainTags	dc.l	ASL_Height,200		;requester height tag
		dc.l	ASL_Width,340		;width tag
		dc.l	TAG_DONE		;end of taglist tag

LoadTags	dc.l	ASL_Hail,0		;name tag
		dc.l	ASL_FuncFlags,FONF_FRONTCOLOR+FONF_BACKCOLOR+FONF_STYLES+FONF_DRAWMODE		;flag add pattern gadget
		dc.l	TAG_DONE		;end of taglist tag
		
SaveTags	dc.l	ASL_Hail,0		;pointer to hail text
		dc.l	ASL_FuncFlags,0
		dc.l	TAG_DONE		;end of taglist tag
	
LoadText	dc.b	'Load Font',0	
		even
SaveText	dc.b	'Save Font',0
		even

Aslname:	dc.b	'asl.library',0				;asl lib name
		even
Dosname:	dc.b	'dos.library',0				;dos lib name

		end
