;
; saveWarn.s
;
; Warn the user about saving a document
;
;  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:errorBox
		GET	sapphire:msgs
		GET	sapphire:sapphire
		GET	sapphire:string
		GET	sapphire:warning

		GET	sapphire:xfer.saveAs

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

		AREA	|Sapphire$$Code|,CODE,READONLY

; --- saveWarn ---
;
; On entry:	R0 == estimated size of data
;		R1 == file type of the data
;		R2 == pointer to name of the file
;		R3 == pointer to handler block
;		R4 == value to pass to handlers in R10
;		R5 == value to pass to handlers in R12
;		R6 == flags (in bottom two bits)
;
; On exit:	--
;
; Use:		Displays a warning box allowing the user to save a modified
;		document.  The flags in R6 are as follows:
;
;		Bit 0	File is safe; don't give a warning
;		Bit 1	File's name is sensible; display it in the warning
;
;		The handler block is the same as that passed to saveAs (q.v.)
;		with an extra entry point on the very beginning, which is
;		expected to remove the document from memory.  This entry
;		point is not passed any arguments except for R10 and R12.
;
;		In order for the system to work, you must call various
;		saveWarn routines from your saveAs entry points:
;
;		saveWarn_saved from saEntry__success
;		saveWarn_close from saEntry__closed

		EXPORT	saveWarn
saveWarn	ROUT

		STMFD	R13!,{R0-R5,R12,R14}	;Save some registers
		WSPACE	sw__wSpace		;Find my workspace address

		; --- Save some data in workspace ---

		MOV	R0,#0			;Clear flags word now
		STMIA	R12,{R0,R3-R5}		;Save values in workspace

		; --- Work out whether we need to warn the user ---

		TST	R6,#1			;Is the file safe?
		BNE	%80saveWarn		;Yes -- just trash it then

		; --- Give the user a warning ---

		TST	R6,#2			;Is the file sensibly named?
		ADREQ	R0,sw__noName		;No -- point to general msg
		ADRNE	R0,sw__named		;Yes -- point to name msg
		BL	msgs_lookup		;Translate the message
		BLNE	str_buffer		;Get a nice string buffer
		BLNE	str_subst		;And substitute the name

		ADR	R1,sw__warnBlk		;Point to button definition
		BL	warning			;Ask the big question
		BCC	%50saveWarn		;If not `Save...' skip on

		; --- User wants to try to save the file ---

		LDMIA	R13,{R0-R5}		;Load saveAs arguments
		ADD	R3,R3,#4		;Point past our entry point
		MOV	R14,#swFlag__saving	;Just about to open save box
		STR	R14,sw__flags		;Save this flags word
		BL	saveAs			;Try to save the file
		BVC	%90saveWarn		;If it's OK, return

		MOV	R14,#0			;Clear the saving flag
		STR	R14,sw__flags		;Save the flags again
		MOV	R1,#1			;Display error with 1 button
		BL	errorBox		;Tell user we failed
		B	%90saveWarn		;And now return to caller

		; --- User chose `Cancel' or `Discard' ---

50saveWarn	CMP	R0,#1			;Did we choose `Discard'?
		BNE	%90saveWarn		;No -- then do nothing at all

80saveWarn	BL	sw__discard		;Discard the document

90saveWarn	LDMFD	R13!,{R0-R5,R12,PC}^	;And return to caller

sw__noName	DCB	"swSDC0",0
sw__named	DCB	"swSDC1",0

sw__warnBlk	BUTTON	"swSAVE"
		BUTTON	"swDISC"
		BCANCEL
		BUTEND

		LTORG

; --- sw__discard ---
;
; On entry:	--
;
; On exit:	--
;
; Use:		Discards the document.

sw__discard	ROUT

		STMFD	R13!,{R0,R10,R12,R14}	;Save some registers
		LDMIB	R12,{R0,R10,R12}	;Load user call arguments
		MOV	R14,PC			;Set up return address
		MOV	PC,R0			;Call discard routine
		LDMFD	R13!,{R0,R10,R12,PC}^	;And return to caller

		LTORG

; --- saveWarn_saved ---
;
; On entry:	--
;
; On exit:	--
;
; Use:		Informs saveWarn that the document has been saved.  If
;		saveWarn is not operating, this call is ignored.  You should
;		only call this routine if the document is *safe*, rather than
;		RAM transferred to another application, for example.

		EXPORT	saveWarn_saved
saveWarn_saved	ROUT

		STMFD	R13!,{R12,R14}		;Save some registers
		WSPACE	sw__wSpace		;Find my workspace
		LDR	R14,sw__flags		;Load the flags word
		ORR	R14,R14,#swFlag__saved	;Say that it's been saved
		STR	R14,sw__flags		;Save the flags back
		LDMFD	R13!,{R12,PC}^		;And return to caller

		LTORG

; --- saveWarn_close ---
;
; On entry:	--
;
; On exit:	--
;
; Use:		Informs saveWarn that the save dialogue box has been closed.
;		If the document is now saved, then it is removed from
;		memory.

		EXPORT	saveWarn_close
saveWarn_close	ROUT

		STMFD	R13!,{R12,R14}		;Save some registers
		WSPACE	sw__wSpace		;Find my workspace
		LDR	R14,sw__flags		;Load the flags word
		CMP	R14,#3			;Are both bits set?
		BLEQ	sw__discard		;Yes -- discard the document
		MOVEQ	R14,#0			;Clear the flags
		STREQ	R14,sw__flags		;And save them back
		LDMFD	R13!,{R12,PC}^		;And return to caller

		LTORG

sw__wSpace	DCD	0

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

		^	0,R12
sw__wStart	#	0

sw__flags	#	4			;Various useful flags
sw__table	#	4			;Address of client's table
sw__R10		#	4			;Value to pass in R10
sw__R12		#	4			;Value to pass in R12

sw__wSize	EQU	{VAR}-sw__wStart

swFlag__saving	EQU	(1<<0)			;We have a save box open
swFlag__saved	EQU	(1<<1)			;User has saved the document

		AREA	|Sapphire$$LibData|,CODE,READONLY

		DCD	sw__wSize
		DCD	sw__wSpace
		DCD	0
		DCD	0

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

		END
