;==========================================================================
; An assembly application to demo the use of the dissidents ColorTool.
; This example code was written by Jeff Glatt and is in the public domain.
; The actual "color.library" is copyrighted by dissidents software. The
; color.library may be used by and distributed with any commercial software
; as long as the library is not modified in any way.

	OPTIMON					;CAPE directives
	OBJFILE	"rad:ColorTest.o"
	ADDSYM
	SMALLOBJ

_LVODoColor equ	-30

	XREF	_SysBase

	XREF	_LVOOpenLibrary,_LVOCloseLibrary,_LVOOpenWindow,_LVOCloseWindow
	XREF	_LVOWait,_LVOGetMsg,_LVOReplyMsg
	XREF	_LVOOpenScreen,_LVOCloseScreen,_LVODisplayBeep

	XDEF	_main
_main:
;-----Open Graphics. Since this is in ROM, let's assume it will open.
;      If the user doesn't have at least 1.2, screw him.
		moveq		#33,d0
		lea		GfxName,a1
		movea.l	_SysBase,a6
		jsr		_LVOOpenLibrary(a6)
		move.l	d0,_GfxBase
;---------Open Intuition---------
		moveq		#33,d0
		lea		IntuiName,a1
		jsr		_LVOOpenLibrary(a6)
		move.l	d0,_IntuitionBase
;---------Open Color Lib---------
		moveq		#0,d0
		lea		ColorName,a1
		jsr		_LVOOpenLibrary(a6)
		move.l	d0,_ColorBase
		beq		clselibs
;---------Open screen----------
		lea		newWindowHi,a3
		lea      newScreenHi,a0
		moveq		#4,d2				;hires depth limit
	;---If passed an arg from CLI, get the screen resolution
		move.l	4(sp),d0
		subq.l	#1,d0
		beq.s		Hires	;no arg
		subq.l	#1,d0
		bcs.s		Hires
		movea.l  8(sp),a2
		addq.l	#4,a2
		movea.l	(a2)+,a1
		cmpi.b	#'h',(a1)
		beq.s		nlow
		lea		newWindowLo,a3
		lea		newScreenLo,a0
		moveq		#5,d2
	;---If another arg, use it as the default depth
nlow	subq.l	#1,d0
		bcs.s		Hires
		movea.l	(a2),a1
		moveq		#0,d0
		move.b	(a1),d0
		subi.b	#'0',d0
		bls.s		Hires
		cmp.b		d2,d0
		bhi.s		Hires
		move.w	d0,8(a0)
Hires	movea.l	_IntuitionBase,a6
		jsr		_LVOOpenScreen(a6)
		move.l	d0,30(a3)
		beq		clsecolor
;--------Open window on our screen-------
		movea.l	a3,a0
		jsr		_LVOOpenWindow(a6)
		move.l	d0,WindowPtr
		beq		clsescr
;-------------------set up and do IDCMP loop--------------
		movea.l	d0,a0
		movea.l	86(a0),a2
		moveq		#0,d7
		move.b	15(a2),d1
		bset.l	d1,d7
   ;---Get IntuiMsg
idcmp:
		movea.l	a2,a0
		movea.l	_SysBase,a6
		jsr		_LVOGetMsg(a6)
		move.l	d0,d1
		bne.s		chkmsg
   ;---wait for an IntuiMsg
		move.l	d7,d0
		jsr		_LVOWait(a6)
		bra.s		idcmp
   ;---Get needed fields and reply to msg
chkmsg:
		moveq		#20,d1
		movea.l	d0,a1
		adda.l	d1,a1      ;CLASS field
		move.l	(a1)+,d2
		move.w	(a1)+,d3
		movea.l	d0,a1
		jsr		_LVOReplyMsg(a6)
		bclr.l	#9,d2
		bne.s		close_up
		cmpi.w	#$68,d3
		bne.s		idcmp
		movea.l	30(a3),a1		;get Screen address
		suba.l	a0,a0				;use original color table for default gadget
		movea.l	_ColorBase,a6
		jsr		_LVODoColor(a6)
		bpl.s		idcmp				;must be SAVE (0) or CANCEL (1)
	;---If negative return value, then an error
		movea.l	30(a3),a0
		movea.l	_IntuitionBase,a6
		jsr		_LVODisplayBeep(a6)
		bra.s		idcmp
close_up:
;=============== drain the window IDCMP and close it ================
		movea.l	_SysBase,a6
		move.l	WindowPtr,d0
		beq.s		clsescr
empit	movea.l	WindowPtr,a0
		movea.l	86(a0),a0
		jsr		_LVOGetMsg(a6)
		move.l	d0,d1
		beq.s		empty
		movea.l	d0,a1
		jsr		_LVOReplyMsg(a6)
		bra.s		empit
empty	movea.l	WindowPtr,a0
		movea.l	_IntuitionBase,a6
		jsr		_LVOCloseWindow(a6)
;-------Close the screen--------
clsescr:
		move.l	30(a3),d0
		beq.s		clsecolor
		movea.l	d0,a0
		jsr		_LVOCloseScreen(a6)
;----------Close libs----------
clsecolor:
		movea.l	_SysBase,a6
		movea.l	_ColorBase,a1
		jsr		_LVOCloseLibrary(a6)
clselibs:
		movea.l	_GfxBase,a1
		jsr		_LVOCloseLibrary(a6)
		movea.l	_IntuitionBase,a1
		jsr		_LVOCloseLibrary(a6)
		moveq		#0,d0
		rts

DEPTH		equ 4
HIRES		equ $8000

DEPTH2	equ 5
LORES		equ 0

	XDEF	_GfxBase,_IntuitionBase,_ColorBase
_GfxBase			dc.l	0
_IntuitionBase	dc.l	0
_ColorBase		dc.l	0

	XDEF   newScreenHi
newScreenHi:
	dc.w	0,0,640,200
	dc.w	DEPTH
	dc.b	0,1
	dc.w	HIRES
	dc.w	15				;CUSTOMSCREEN
	dc.l	TextAttr
	dc.l	ScreenTitle,0,0

   XDEF   newWindowHi
newWindowHi:
	dc.w	0,12,640,188
	dc.b	-1,-1
	dc.l	$208
	dc.l	$110F
	dc.l	0,0,WindowTitle
	dc.l	0,0
	dc.w	640,188,640,188
	dc.w	15

	XDEF   newScreenLo
newScreenLo:
	dc.w	0,0,320,200
	dc.w	DEPTH2
	dc.b	0,1
	dc.w	LORES
	dc.w	15				;CUSTOMSCREEN
	dc.l	TextAttr
	dc.l	ScreenTitle,0,0

   XDEF   newWindowLo
newWindowLo:
	dc.w	0,12,320,188
	dc.b	-1,-1
	dc.l	$208
	dc.l	$110F
	dc.l	0,0,WindowTitle
	dc.l	0,0
	dc.w	320,188,320,188
	dc.w	15

	XDEF	WindowPtr
WindowPtr	dc.l	0

TextAttr:
	dc.l	FontName
	dc.w	8,0

WindowTitle	dc.b	'Click Close Gadget to Exit',0
ScreenTitle	dc.b	'Color Requester Demo',0
GfxName		dc.b	'graphics.library',0
IntuiName	dc.b	'intuition.library',0
FontName		dc.b	'topaz.font',0
ColorName	dc.b	'color.library',0
