
**	CODE	OPEN WINDOW	
**	CODER	MARC.B


;coded using the intuiton tutorials by Mark Meany .......



	incdir		sys:include/
	
	include		intuition/intuition.i
	include		intuition/intuition_lib.i
	include		exec/exec.i
	include		exec/exec_lib.i
	include		Source:include/hardware.i


finish		equ	999

	

	
************************************
*	OPEN INTUITION LIBRARY	   *
************************************

	lea		int_name,a1
	moveq		#0,d0
	CALLEXEC	OldOpenLibrary
	tst		d0
	beq		the_end
	move.l		d0,_IntuitionBase


***************************************
*		OPEN WINDOW	      *
***************************************

	lea		new_window,a0
	CALLINT		OpenWindow
	tst		d0
	beq		exit
	move.l		d0,window_handle
	move.l		d0,a0	
	move.l		wd_UserPort(a0),window.up
	move.l		wd_RPort(a0),window.rp
	

****************************************
*	MAIN CODE LOOP		       *
****************************************

	
	
loop
	bsr		WaitForMsg
	cmp.l		#finish,d5
	beq		close
	bra 		loop

****************************************
*	CHECK MESSAGE STATUS	       *
****************************************

WaitForMsg	move.l		window.up,a0	a0-->user port
		CALLEXEC	WaitPort	wait for something to happen
		move.l		window.up,a0	a0-->window pointer
		CALLEXEC	GetMsg		get any messages
		tst.l		d0		was there a message ?
		beq.s		WaitForMsg	if not loop back
		move.l		d0,a1		a1-->message
		move.l		im_Class(a1),d2	d2=IDCMP flags
		move.l		im_IAddress(a1),a5 a5=addr of structure
		CALLEXEC	ReplyMsg	answer os or it get angry
		move.l		d2,d0
		and.l		#GADGETUP!GADGETDOWN,d0
		beq.s		.test_win
		move.l		gg_UserData(a5),d0
		move.l		d0,a0
		jsr		(a0)
		rts
		
.test_win	
		and.l		#CLOSEWINDOW,d2
		beq		WaitForMsg
		move.w		#999,d5
		rts
		


***************************************** 
*	   CLOSE WINDOW			*
*****************************************

close
	move.l		window_handle,a0
	CALLINT		CloseWindow
	

*****************************************
*		CLOSE INTUITION		*
*****************************************

exit
	
	move.l		_IntuitionBase,a1
	CALLEXEC	CloseLibrary

the_end
	rts

*****************************************
*	    CODE DATA SECTION		*
*****************************************

	include		source:m.bateson/GADGETS/size_gadget


************************************
*	GET CURRENT PREF'S	   *
************************************

get_prefs

	lea		prefbuffer,a0		this routine loads
	move.l		#232,d0			the current pref's settings
	CALLINT		GetPrefs		into a buffer

************************************
*	SET CURRENT PREF'S	   *
************************************

	lea		prefbuffer,a0		load preferences buffer
	move.w		d2,118(a0)		alter display
	move.l		#232,d0
	move.l		#1,d1			send IDCMP flag
	CALLINT		SetPrefs
	rts

************************************
*	GADGET SUBROUTINE	   *
************************************

;this routine reads the values returned in the gadget structure
;and converts using the calculation described in the tutorials
;these are then loaded into d2 in the correct order
;vertical position in the low byte and horizontal in the hi byte
;30 is subtracted from the horizontal value in order to get a 
;result between -30 and 100 enableing the display to be moved
;far enough left
 

set_position

	move.l		gg_SpecialInfo(a5),a4
	move.w		pi_HorizPot(a4),d2
	move.w		pi_VertPot(a4),d1
	mulu		#130,d2
	mulu		#100,d1
	divu		#$ffff,d2
	sub.w		#30,d2
	divu		#$ffff,d1
	ror.w		#8,d2
	move.b		d1,d2
	bsr		get_prefs
	btst		#6,CIAAPRA		
	beq		set_position	
	rts	

***************************************
*	CODE VARIABLES		      *
***************************************

_IntuitionBase	dc.l 0
window_handle	dc.l 0
window.up	dc.l 0
window.rp	dc.l 0
h_pot		dc.w 0
v_pot		dc.w 0
prefbuffer	ds.b 232
int_name	dc.b 'intuition.library',0

	SECTION	gfx.data,CODE_C


IM1Data		incbin		Source:M.Bateson/Gadgets/GADGET6

end

