;========================================================================
;
; Project...: Window
; Version...: 1.3
; Date......: 12 June 1991
; Author....: P. de Boer (VECTOR/Vision)
; Assembler.: AsmOne V1.01/DevPac 2.14
;
;========================================================================

_LVOWrite		EQU	-48		;DOS
_LVOOutput		EQU	-60

_LVOMoveWindow		EQU	-168		;INT
_LVOSizeWindow		EQU	-288

			rsreset
Window			rs.b	0
wd_NextWindow		rs.l	1
wd_LeftEdge		rs.w	1
wd_TopEdge		rs.w	1
wd_Width		rs.w	1
wd_Height		rs.w	1
wd_MouseY		rs.w	1
wd_MouseX		rs.w	1
wd_MinWidth		rs.w	1
wd_MinHeight		rs.w	1
wd_MaxWidth		rs.w	1
wd_MaxHeight		rs.w	1
;etc...

_LVOCloseLibrary	EQU	-414		;EXEC
_LVOOpenLibrary		EQU	-552

ExecBase		EQU	$4

gb_NormalDisplayRows	EQU	$D8		;GFX
gb_NormalDisplayColumns	EQU	$DA

CALL		MACRO				;MACROS
		IFC	'EXEC','\1'
		MOVEA.L	(ExecBase).W,A6
		ENDC
		IFNC	'EXEC','\1'
		MOVEA.L	\1Base,A6
		ENDC
		JSR	_LVO\2(A6)
		ENDM

OPENLIB		MACRO
		LEA	\1Name(PC),A1
		CLR.L	D0
		CALL	EXEC,OpenLibrary
		MOVE.L	D0,\1Base
		BEQ	\2
		ENDM

CLOSELIB	MACRO
		MOVEA.L	\1Base,A1
		CALL	EXEC,CloseLibrary
		ENDM

;========================================================================
		opt	o+

Start		movea.l	a0,a5
		OPENLIB	DOS,NoDOS
		OPENLIB	INT,NoINT
		OPENLIB	GFX,NoGFX
		CALL	DOS,Output
		move.l	d0,OutputHandle

		move.l	#Hello,d2
		move.l	#Hello.LEN,d3
		BSR	Print

		movea.l	a5,a0

		BSR	GetNumber
		beq.w	NoArg
		move.w	d0,LeftEdge

		BSR	GetNumber
		beq.w	NoArg
		move.w	d0,TopEdge

		BSR	GetNumber
		beq	NoArg
		move.w	d0,Width

		BSR	GetNumber
		beq	NoArg
		move.w	d0,Height
		
		move.l	INTBase(pc),a6
		move.l	$34(a6),a2		;active window
		
		move.l	GFXBase(pc),a6

		move.w	Width(pc),d0		;smaller than min ?
		cmp.w	wd_MinWidth(a2),d0
		blt	Fout			;fail !
		add.w	LeftEdge(pc),d0			;larger than max
		cmp.w	gb_NormalDisplayColumns(a6),d0
		bgt	Fout				;then fail

		move.w	Height(pc),d0		;smaller than min
		cmp.w	wd_MinHeight(a2),d0
		blt	Fout			;fail !
		add.w	TopEdge(pc),d0
		cmp.w	gb_NormalDisplayRows(a6),d0
		bgt	Fout

MoveToZZ	move.w	wd_LeftEdge(a2),d0	;first move the window
		move.w	wd_TopEdge(a2),d1	;to 0,0
		neg.w	d0
		neg.w	d1
		ext.l	d0
		ext.l	d1
		move.l	a2,a0
		CALL	INT,MoveWindow

.Wait		tst.w	wd_LeftEdge(a2)		;wait for intuition
		bne.s	.Wait			;to move the window
		tst.w	wd_TopEdge(a2)		;I know that there must be
		bne.s	.Wait			;a much more elegant way
						;to wait...
SizeWindow	move.w	Width(pc),d0		;resize the window
		move.w	Height(pc),d1
		sub.w	wd_Width(a2),d0
		sub.w	wd_Height(a2),d1
		ext.l	d0
		ext.l	d1
		move.l	a2,a0
		CALL	INT,SizeWindow

		move.w	Width(pc),d0		;wait again
		move.w	Height(pc),d1
.Wait		cmp.w	wd_Width(a2),d0
		bne.s	.Wait
		cmp.w	wd_Height(a2),d1
		bne.s	.Wait

MoveWindow	move.w	LeftEdge(pc),d0
		move.w	TopEdge(pc),d1
		sub.w	wd_LeftEdge(a2),d0
		sub.w	wd_TopEdge(a2),d1
		ext.l	d0
		ext.l	d1
		move.l	a2,a0			;move the window
		CALL	INT,MoveWindow

		move.w	LeftEdge(pc),d0		;wait again
		move.w	TopEdge(pc),d1
.Wait		cmp.w	wd_LeftEdge(a2),d0
		bne.s	.Wait
		cmp.w	wd_TopEdge(a2),d1
		bne.s	.Wait

		bra.s	Exit			;and exit

Fout		move.l	#Illegal,d2		;hmm illegal parameters ?
		move.l	#Illegal.LEN,d3
		BSR.S	Print

Exit		CLOSELIB	GFX

NoGFX		CLOSELIB	INT

NoINT		CLOSELIB	DOS

NoDOS		clr.l	d0
		rts

NoArg		move.l	#Syntax,d2
		move.l	#Syntax.LEN,d3
		BSR.S	Print
		bra.s	Exit
		
;========================================================================
Print		move.l	OutputHandle(pc),d1
		beq.s	.Exit			;for some reason....
		CALL	DOS,Write
.Exit		rts

;========================================================================
GetNumber	BSR.S	SkipSpaces
		beq.s	.Exit

		moveq	#-2,d1

.Test		addq.w	#1,d1
		cmp.b	#$0A,(a0)
		beq.s	.Ok
		cmp.b	#" ",(a0)+
		bne.s	.Test
		subq.w	#1,a0

.Ok		moveq	#0,d4
		moveq	#1,d5
		movea.l	a0,a1

.Loop		clr.w	d0
		move.b	-(a1),d0
		sub.w	#"0",d0
		mulu	d5,d0
		add.l	d0,d4
		mulu	#10,d5
		dbf	d1,.Loop		

		move.l	d4,d0
		moveq	#-1,d1
.Exit		rts

;========================================================================
SkipSpaces	cmp.b	#" ",(a0)+
		beq.s	SkipSpaces
		subq.w	#1,a0
		cmp.b	#$0A,(a0)
		rts

;========================================================================

OutputHandle	DC.L	0

LeftEdge	DC.W	0
TopEdge		DC.W	0
Width		DC.W	0
Height		DC.W	0

DOSBase		DC.L	0
INTBase		DC.L	0
GFXBase		DC.L	0

DOSName		DC.B	"dos.library",0
INTName		DC.B	"intuition.library",0
GFXName		DC.B	"graphics.library",0

Hello		DC.B	$9B,"1;33mWindow",$9B,"0m V1.3 by P. de Boer"
		DC.B	" (VECTOR/Vision)",$0A
Hello.LEN	EQU	*-Hello
Syntax		DC.B	$9B,"0;33mSYNTAX:"
		DC.B	$9B,"0m Window [LeftEdge] [TopEdge] [Width] [Height]",$0A,$0
Syntax.LEN	EQU	*-Syntax
Illegal		DC.B	"Illegal operand(s)",$0A
Illegal.LEN	EQU	*-Illegal

;========================================================================
