;
; warning.s
;
; Displays warning boxes (MDW)
;
;  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

		GET	libs:stream

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

		GET	sapphire:buttons
		GET	sapphire:dbox
		GET	sapphire:errorBox
		GET	sapphire:msgs
		GET	sapphire:nopoll
		GET	sapphire:sapphire
		GET	sapphire:string
		GET	sapphire:template
		GET	sapphire:wimp

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

		AREA	|Sapphire$$Code|,CODE,READONLY

; --- warning ---
;
; On entry:	R0 == pointer to warning text to display
;		R1 == pointer to buttons block
;
; On exit:	R0 == button that was clicked
;		CS if this was default, CC otherwise
;
; Use:		Displays a warning to the user.  The warning box can have up
;		to five buttons (because it's too small for any more than
;		that).  These are placed in a column on the right hand side
;		of the dialogue.  The buttons are numbered from 0 up to 4
;		from the bottom upwards, 0 being the default.  You can
;		choose one button to be `Cancel', in which case pressing
;		escape will activate it.
;
;		Note that a call to this routine cannot fail, since the
;		dialogue is created at initialisation time.

		EXPORT	warning
warning		ROUT

		STMFD	R13!,{R1-R4,R12,R14}	;Save some registers
		WSPACE	warn__wSpace		;Find my workspace address
		MOV	R4,R0			;Look after the warning text

		; --- Set up the buttons ---

		LDR	R0,warn__dbox		;Load the dbox handle
		MOV	R2,#wIcon__buttons	;Find the buttons base
		MOV	R3,#warn__buttons	;Find the button count
		BL	buttons_setup		;Set all that lot up
		STR	R2,warn__butFlags	;Save the defined buttons
		STR	R3,warn__cancel		;Save the cancel button too
		MOV	R1,#wIcon__text		;Get the text icon handle
		MOV	R2,R4			;Get the text string too
		BL	dbox_setField		;Write that into the icon

		; --- Display the window in its glory ---

		BL	errorBox_beep		;Make a nice noise
		MOV	R1,#dbOpen_persist+dbOpen_centre+dbOpen_nonSub
		BL	dbox_open		;Open the dialogue

		BL	dbox_window		;Get its window handle
		BL	nopoll_open		;Let nopoll take over a bit
		BL	nopoll_process		;Get a reply from the dbox
		LDMFD	R13!,{R1-R4,R12,R14}	;Reload some registers
		SUBS	R0,R0,#wIcon__buttons	;Was it the default button?
		ORREQS	PC,R14,#C_flag		;Yes -- set C flag on exit
		BICNES	PC,R14,#C_flag		;No -- clear the C flag

		LTORG

; --- warn__dbHandler ---
;
; On entry:	R0 == dialogue box event code
;		R1-R7 depend on the event
;
; On exit:	--
;
; Use:		Handles events for a warning box.

warn__dbHandler	ROUT

		STMFD	R13!,{R14}		;Save a register
		CMP	R0,#dbEvent_OK		;Is it a return press?
		MOVEQ	R0,#wIcon__buttons	;Yes -- set to be default
		CMP	R0,#dbEvent_cancel	;Was it a cancel?
		LDREQ	R0,warn__cancel		;Yes -- load cancel button
		CMPEQ	R0,#-1			;Was it defined by user?
		MOVEQ	R0,#wIcon__buttons	;No -- make it do OK then

		SUB	R14,R0,#wIcon__buttons	;Subtract the buttons base
		CMP	R14,#warn__buttons	;Is it in range?
		LDMCSFD	R13!,{PC}^		;Yes -- then return

		STMFD	R13!,{R0,R1}		;Save another register
		LDR	R1,warn__butFlags	;Which buttons are created?
		MOV	R14,#1			;Use a bit to test
		TST	R1,R14,LSL R0		;Test the correct bit
		LDMEQFD	R13!,{R0,R1,PC}^	;Not created -- return then

		MOV	R1,R0			;Put icon into R1
		LDR	R0,warn__dbox		;Get dialogue in R0
		BL	dbox_slab		;Slab the icon nicely
		BL	dbox_close		;Close the window
		BL	dbox_unslab		;Unslab the icon again
		MOV	R0,R1			;Put icon back in R0
		BL	nopoll_close		;Return this to main code
		LDMFD	R13!,{R0,R1,PC}^	;And return to caller

		LTORG

; --- warn_init ---
;
; On entry:	R0 == program name
;
; On exit:	--
;
; Use:		Sets up the Warning dialogue box for use.

		EXPORT	warn_init
warn_init	ROUT

		STMFD	R13!,{R12,R14}		;Save some registers
		WSPACE	warn__wSpace		;Load my workspace address
		LDR	R14,warn__dbox		;Load the dialogue handle
		CMP	R14,#0			;Is it defined yet?
		LDMNEFD	R13!,{R12,PC}^		;Yes -- then return to caller

		; --- Ensure other bits are initialised ---

		BL	wimp_init		;We need to create windows
		BL	template_init		;Make sure we can get these
		BL	dbox_init		;We want to create dialogues
		BL	nopoll_init		;We may need this

		; --- Create a dialogue box ---

		STMFD	R13!,{R0-R3}		;Save some more registers
		MOV	R2,R0			;Keep the application name
		ADR	R0,warn__dbName		;Point at my dialogue name
		BL	dbox_create		;Try to create it
		SWIVS	OS_GenerateError	;Don't take errors lying down
		STR	R0,warn__dbox		;Save my dialogue handle

		ADR	R0,warn__titleSkel	;Point to skeleton title
		BL	msgs_lookup		;Translate it nicely
		MOV	R1,R11			;Build in scratchpad
		BL	str_subst		;Do the stuff required
		MOV	R2,R0			;Make it ready for dbox
		LDR	R0,warn__dbox		;Get the dialogue back again
		MOV	R1,#-1			;Put it in the title bar
		BL	dbox_setField		;Fill in the title bar

		MOV	R1,#wIcon__title	;Set up the title icon
		BL	dbox_setEmbeddedTitle	;Write title in group box

		; --- Set up the event handler ---

		ADR	R1,warn__dbHandler	;Point to handler routine
		MOV	R2,R10			;Pass (bogus) R10
		MOV	R3,R12			;Pass it my workspace
		BL	dbox_eventHandler	;Set that up nicely
		LDMFD	R13!,{R0-R3,R12,PC}^	;Return to caller

warn__dbName	DCB	"warning",0
warn__titleSkel	DCB	"warnFROM",0

		LTORG

warn__wSpace	DCD	0

;----- Icon handles ---------------------------------------------------------

wIcon__text	EQU	2			;The actual text icon
wIcon__title	EQU	1			;Embedded title icon
wIcon__buttons	EQU	3			;Buttons from here on up

warn__buttons	EQU	5			;Number of buttons allowed

;----- Warning button table layout ------------------------------------------

wFlag_cancel	EQU	(1<<0)			;This is the cancel button
						;+0

wFlag_text	EQU	(1<<1)			;This button contains text
						;+0 == message tag
						;+n

wFlag_last	EQU	(1<<31)			;This is the last item
						;+0

;----- Workspace ------------------------------------------------------------

		^	0,R12
warn__wStart	#	0

warn__dbox	#	4			;The warning dialogue handle
warn__cancel	#	4			;The cancel icon, or -1
warn__butFlags	#	4			;Which buttons are available

warn__wSize	EQU	{VAR}-warn__wStart

		AREA	|Sapphire$$LibData|,CODE,READONLY

		DCD	warn__wSize
		DCD	warn__wSpace
		DCD	0
		DCD	warn_init

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

		END
