;
; defHandler.s
;
; Default event handler (TMA)
;
;  1994-1998 Straylight
;

;----- Licensing note -------------------------------------------------------
;
; This file is part of Straylight's Sapphire library.
;
; Sapphire is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2, or (at your option)
; any later version.
;
; Sapphire is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with Sapphire.  If not, write to the Free Software Foundation,
; 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

;----- Standard header ------------------------------------------------------

		GET	libs:header
		GET	libs:swis

;----- External dependencies ------------------------------------------------

		GET	sapphire:resspr

;----- Main code ------------------------------------------------------------

		AREA	|Sapphire$$Code|,CODE,READONLY

; --- defHandler ---
;
; On entry:	R0 == Wimp_Poll reason code
;		R1 == pointer to Wimp_Poll event block
;
; On exit:	CS if we handled the event, CC otherwise.
;
; Use:		Handles events no-one else is interested in.  It's basically
;		a catch-all for things that probably should have been
;		handled elsewhere, and does some useful tidying up
;		operations (like calling Wimp_ProcessKey on key events).

		EXPORT	defHandler
defHandler	ROUT

		ORR	R14,R14,#C_flag		;Set carry flag on return
		CMP	R0,#9			;Is it a low-numbered event?
		ADDLO	PC,PC,R0,LSL #2		;Yes -- dispatch
		B	%10defHandler		;No -- handle specially

		; --- The event dispatch table ---

		BICS	PC,R14,#C_flag		;Null events are ignored
		B	def__redraw		;Do a dummy redraw op
		B	def__open		;Open unopened windows
		B	def__close		;Close unclosed windows
		BICS	PC,R14,#C_flag		;So pointer's left a window
		BICS	PC,R14,#C_flag		;Gone back inside again
		BICS	PC,R14,#C_flag		;Mouse click -- yawn
		B	def__drag		;Turn off DragASprite
		B	def__key		;Pass keypresses onwards

		; --- Handle high-numbered events ---

10defHandler	CMP	R0,#17			;Is it a message?
		CMPNE	R0,#18			;Or another message?
		BICNES	PC,R14,#C_flag		;No -- nothing we can do

		; --- Handle Message_Quit properly ---

		STMFD	R13!,{R14}		;Save the link register
		LDR	R14,[R1,#16]		;Get the message action
		CMP	R14,#0			;Is it a quit message?
		LDMFD	R13!,{R14}		;Restore link register
		SWIEQ	OS_Exit			;Yes -- kill everything off
		BICS	PC,R14,#C_flag		;Couldn't understand message

; --- def__<event> ---
;
; Entry/exit:	As for defHandler above
;
; Use:		Handles default behaviour for an individual event type

def__redraw	ROUT

		STMFD	R13!,{R0,R1,R14}	;Save some registers
		BL	resspr_area		;Find the sprite area
		SWI	XSculptrix_SetSpriteArea ;Register it with Sculptrix
		SWI	Wimp_RedrawWindow	;Start the redraw operation
00def__redraw	CMP	R0,#0			;Is this the end of the line?
		SWINE	XSculptrix_RedrawWindow	;Use Sculptrix if it's there
		SWINE	Wimp_GetRectangle	;No -- get another rectangle
		BNE	%00def__redraw		;And go back again
		LDMFD	R13!,{R0,R1,PC}^	;Yes -- return

def__open	SWI	Wimp_OpenWindow		;Open the window then
		MOVS	PC,R14

def__close	SWI	Wimp_CloseWindow	;Close the window
		MOVS	PC,R14

def__drag	SWI	XDragASprite_Stop	;Stop any spritely drags
		MOVS	PC,R14

def__key	STMFD	R13!,{R0,R14}		;Save some registers
		LDR	R0,[R1,#24]		;Get the key number out
		SWI	Wimp_ProcessKey		;Pass it to other apps
		LDMFD	R13!,{R0,PC}^		;Return to the caller

;----- That's all, folks ----------------------------------------------------

		END
