	load	'macros.dump'
	include 'driver.equ'
	include 'scrap.equ'

	IMPORT	D_BeachBall
	IMPORT	X_CopyOneHandle
	IMPORT	X_DisposeWPScrap
	IMPORT	D_NeedHandle
	IMPORT	X_ParBlockSize

X_CopyThread	PROC	EXPORT
	;Using X_WPScrapData
	;Using X_WPFileData

	input Refcon:l,Type:w

	local Rptr:l,Cptr:l,CurBlock:l,CurBlockCopy:l
	local RulArray:l,RAPtr:l,Pars:w,ParsSeen:w
	local Rulers:w,RulSeen:w,RulOff:w,ParSize:w
	local CopyRuler:l,CRPtr:l

	output Copy:l

	error err

	begin +b

	stz err

	lda Type
	bne notwp

	MoveWord #X_ParBlockSize,ParSize
	bra cont

notwp	MoveWord #(X_ParBlockSize+4),ParSize

cont	PushLong Refcon
	_HLock

	SpaceLong
	PushLong Refcon
	jsl X_CopyOneHandle
	sta err
	PullLong Copy
	jcs exit

	PushLong Copy
	_HLock

	AddLong [Copy],#2,Cptr

	MoveLong [Refcon],Rptr
	MoveWord [Rptr],Pars
	AddWord Rptr,#2,Rptr
	stz Parsseen

	SpaceLong
	lda Pars
	asl a
	asl a
	asl a
	pea 0
	pha
	PushWord #$8000
	jsl D_NeedHandle
	sta err
	PullLong RulArray
	jcc goodrularray
	PushLong Copy
	_DisposeHandle
	brl exit

goodrularray
	MoveLong [RulArray],RAPtr
	stz Rulers

	MoveLong #0,CurBlock

parloop	jsl D_BeachBall
	lda Parsseen
	cmp Pars
	jge done

	Cmpl [Rptr],CurBlock
	beq sameblock

	SpaceLong
	PushLong [Rptr]
	jsl X_CopyOneHandle
	sta err
	PullLong CurBlockCopy
	jcs abort

	MoveLong [Rptr],CurBlock

sameblock
	MoveLong CurBlockCopy,[Cptr]

	lda Type
	jne cont2

	ldy #X_scpAttr
	lda [Rptr],y
	jne endruleloop

cont2	stz RulSeen
	MoveLong [RulArray],RAPtr

ruleloop	lda RulSeen
	cmp Rulers
	bge notfound

	Cmpl [RAPtr],[Rptr]:#X_scpRulerHand
	beq foundit

	inc RulSeen
	AddLong RAPtr,#8,RAPtr
	bra ruleloop

foundit	ldy #4
	MoveLong [RAPtr]:y,[Cptr]:#X_scpRulerHand
	MoveLong [RAPtr]:#4,CopyRuler
	MoveLong [CopyRuler],CRPtr
	lda [CRPtr]
	inc a
	sta [CRPtr]
	brl endruleloop

notfound	MoveLong [Rptr]:#X_scpRulerHand,[RAPtr]
	SpaceLong
	PushLong [Rptr]:#X_scpRulerHand
	jsl X_CopyOneHandle
	sta err
	PullLong CopyRuler
	jcc gotruler

abort
	MoveLong [Copy],Cptr
	MoveWord Parsseen,[Cptr]
	PushLong Copy
	jsl X_DisposeWPScrap
	PushLong RulArray
	_DisposeHandle
	brl exit

gotruler
	MoveLong CopyRuler,[Cptr]:#X_scpRulerHand
	MoveLong CopyRuler,[RAPtr]:#4
	MoveLong [CopyRuler],CRPtr
	MoveWord #1,[CRPtr]
	inc Rulers

endruleloop
	inc Parsseen
	MoveLong #0,[Cptr]:#X_scpLinehand
	Addwl ParSize,Rptr
	Addwl ParSize,Cptr
	brl parloop

done
	PushLong Copy
	_HUnlock

	PushLong RulArray
	_DisposeHandle

exit	PushLong Refcon
	_Hunlock
	return err
	ENDP

X_DisposeThread	PROC	EXPORT
	;Using X_WPScrapData
	;Using X_WPFileData

	input Refcon:l,Type:w

	local Rptr:l,ParSize:w,Bpars:w
	local Ruler:l,RulPtr:l

	begin +b

	lda Type
	bne notwp

	MoveWord #X_ParBlockSize,ParSize
	bra cont

notwp	MoveWord #(X_ParBlockSize+4),ParSize

cont	Movelong [Refcon],Rptr
	MoveWord [Rptr],Bpars
	Addwl #2,Rptr

loop	lda Bpars
	jeq exit

	MoveWord [Rptr]:#X_scpOffset,a
	cmp #4
	jne doruler

	PushLong [Rptr]:#X_scpBlockHand
	_DisposeHandle

doruler	lda Type
	bne disruler

	MoveWord [Rptr]:#X_scpAttr,a
	jne dolines

disruler	MoveLong [Rptr]:#X_scpRulerHand,Ruler
	MoveLong [Ruler],RulPtr
	lda [RulPtr]
	dec a
	bne dodec

	PushLong Ruler
	_DisposeHandle
	bra dolines

dodec	sta [RulPtr]

dolines	MoveLong [Rptr]:#X_scpLinehand,Ruler
	lda Ruler
	ora Ruler
	beq endloop

	PushLong Ruler
	_DisposeHandle

endloop	dec Bpars
	Addwl ParSize,Rptr
	brl loop

exit	PushLong Refcon
	_DisposeHandle

	return
	ENDP
	END

