;
; buttons.s
;
; Handle buttons definitions
;
;  1995-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:dbox
		GET	sapphire:msgs

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

		AREA	|Sapphire$$Code|,CODE,READONLY

; --- buttons_setup ---
;
; On entry:	R0 == dialogue box handle
;		R1 == pointer to buttons block
;		R2 == buttons base icon
;		R3 == number of buttons to allow
;
; On exit:	R2 == buttons flag mask
;		R3 == cancel button icon, or -1
;
; Use:		Sets up a dialogue box's buttons according to a buttons
;		block.

		EXPORT	buttons_setup
buttons_setup	ROUT

		STMFD	R13!,{R0,R1,R4-R9,R14}	;Save some registers
		MOV	R9,R1			;Look after the button table
		ADD	R8,R2,R3		;Get the icon limit value
		MOV	R7,R2			;Start an icon counter
		MOV	R6,#0			;Keep track of the bits
		MOV	R5,R0			;And get the dialogue handle
		MOV	R4,#-1			;No cancel as yet
		BL	dbox_window		;And turn it into a window
		STR	R0,[R13,#-16]!		;Save it on the stack
		MOV	R14,#&00800000		;Put deleted flag in BIC word
		STR	R14,[R13,#12]		;Store that away too

05buttons_setup	LDR	R3,[R9],#4		;Load the flags word
		MOV	R0,#0			;No text for this icon

		TST	R3,#bFlag_cancel	;Is this a cancel button?
		MOVNE	R4,R7			;Yes -- we have cancel
		ADRNE	R0,but__cancelTag	;Point to the message
		BNE	%07buttons_setup	;And skip onwards
		TST	R3,#bFlag_ok		;Is this an OK button?
		ADRNE	R0,but__okTag		;Point to the message
		BNE	%10buttons_setup	;And skip onwards
		TST	R3,#bFlag_help		;Is this a help button?
		ADRNE	R0,but__helpTag		;Point to the message
		BNE	%07buttons_setup	;And skip onwards

07buttons_setup	TST	R3,#bFlag_text		;Does this icon contain text?
		BEQ	%10buttons_setup	;Skip forwards to that bit
		MOV	R0,R9			;Point to button message

00		LDRB	R14,[R9],#1		;Load a byte from the block
		CMP	R14,#&20		;Reached the end yet?
		BCS	%b00			;No -- keep looping
		ADD	R9,R9,#3		;Word-align pointer
		BIC	R9,R9,#3

		; --- Fill an icon with text ---

10buttons_setup	CMP	R0,#0			;Is there text defined?
		MOVEQ	R14,#&00800000		;No -- delete the icon then
		BEQ	%20buttons_setup	;So go and do that
		BL	msgs_lookup		;Get the actual message
		MOV	R2,R0			;Get the string in R2
		MOV	R1,R7			;Get the icon handle
		MOV	R0,R5			;And the dialogue handle
		BL	dbox_setField		;Write that in nicely
		MOV	R14,#1			;Set this icon's bit
		ORR	R6,R6,R14,LSL R7	;Remember this icon exists

		; --- Delete or undelete an icon ---

		MOV	R14,#0			;We want to undelete here
20buttons_setup	STMIB	R13,{R7,R14}		;Save icon and EOR mask
		MOV	R1,R13			;Point to the block
		SWI	Wimp_SetIconState	;And make sure of that

		; --- See what to do next ---

		ADD	R7,R7,#1		;Move on to next icon
		CMP	R7,R8			;Have we finished yet?
		BCS	%30buttons_setup	;Finished them all -- skip
		TST	R3,#bFlag_last		;Was that the last of them?
		BEQ	%05buttons_setup	;No -- go round again then

		; --- Delete the rest of the icons ---

		MOV	R14,#&00800000		;Get the deleted flag
		STR	R14,[R13,#8]		;Save it in the block
		MOV	R1,R13			;Point at the block
00		STR	R7,[R13,#4]		;Save the icon number
		SWI	Wimp_SetIconState	;Delete the icon
		ADD	R7,R7,#1		;Move on to next icon
		CMP	R7,R8			;Finished yet?
		BCC	%b00			;More to go -- loop

		; --- That's it -- return ---

30buttons_setup	MOV	R2,R6			;Get the button bits
		MOV	R3,R4			;And get the cancel button
		ADD	R13,R13,#16		;Restore the stack pointer
		LDMFD	R13!,{R0,R1,R4-R9,PC}^	;And return to caller

but__okTag	DCB	"ok",0
but__cancelTag	DCB	"can",0
but__helpTag	DCB	"help",0

		LTORG

bFlag_cancel	EQU	(1<<0)			;This is the cancel button
bFlag_ok	EQU	(1<<1)			;This is the OK button
bFlag_help	EQU	(1<<2)			;This is the help button
bFlag_text	EQU	(1<<3)			;This button contains text
bFlag_last	EQU	(1<<31)			;This is the last item

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

		END
