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

	IMPORT	D_AlertBox
	IMPORT	X_BHand
	IMPORT	X_BOffset
	IMPORT	X_BRuler
	IMPORT	D_BeachBall
	IMPORT	X_CopyThread
	IMPORT	D_CurCursor
	IMPORT	X_CurrentParRecord
	IMPORT	X_DBtoPLScrap
	IMPORT	X_DisposeScrap
	IMPORT	X_DisposeThread
	IMPORT	D_GrowHandle
	IMPORT	X_MakeStandardRuler
	IMPORT	D_NeedHand
	IMPORT	D_NeedHandle
	IMPORT	X_OldCursor
	IMPORT	X_PLtoWPScrap
	IMPORT	X_ParBlockSize
	IMPORT	X_RulerSize
	IMPORT	X_ScrStdRuler
	IMPORT	D_SetCursor
	import	X_MassageWPScrap
	import	X_StdTabRuler
	import	X_DisposeWPScrap
	import	X_SwitchFontID
	import	X_ClipData


;***********************************************************************
;
;   X_PLtoWPScrap
;
;***********************************************************************
X_ScPLtoWPScrap	PROC	EXPORT
	;Using D_GlobalData
	;Using X_ClipData
	;Using X_WPScrapData
	;Using X_WPFileData
	;Using D_CursorData
	;Using D_IOData

	input Src:l

	local Ssize:l,Sptr:l,Dsize:l,Dptr:l,Doff:l
	local Pars:l,EndPtr:l
	local Ruler:l,Rptr:l,Block:l,Bptr:l,Bsize:l
	local Boff:l,RealBptr:l,Cptr:l,ParSize:l
	local NewBlockSize:l,CRFlag:l

	output Dest:l

	error err

	begin

	MoveWord >D_CurCursor,X_OldCursor

;Set up everything.  Check for empty scrap.

	stz err

	SpaceLong
	PushLong Src
	_GetHandlesize
	PullLong Ssize

	PushLong Src
	_HLock
	MoveLong [Src],Sptr
	AddLong Sptr,Ssize,EndPtr

;Get block for Q_Text.
	SpaceLong
	PushLong #X_Blocksize
	jsl D_NeedHand
	sta err
	PullLong Block
	jcs exit

	Movelong [Block],RealBptr
	AddLong RealBptr,#4,Bptr

	MoveWord #4,[RealBptr]
	MoveWord #4,[RealBptr]:#2

	MoveLong #4,Boff
	MoveLong #4,Bsize

	MoveLong Block,X_BHand

;Get ruler.  This is ;easy*.
	SpaceLong
	PushLong #X_RulerSize
	jsl D_NeedHand
	sta err
	PullLong Ruler
	jcc rulerok
	PushLong Block
	_DisposeHandle
	brl exit

rulerok	MoveLong [Ruler],Rptr

	jsl X_MakeStandardRuler

	PushLong #X_ScrStdRuler
	PushLong Rptr
	PushLong #X_RulerSize
	_BlockMove

	MoveLong Ruler,X_BRuler

;Get paragraph array.
	SpaceLong
	_MaxBlock
	PullLong Dsize

	SpaceLong
	PushLong Dsize
	jsl D_NeedHand
	sta err
	PullLong Dest
	jcc pararrayok
	PushLong Block
	_DisposeHandle
	PushLong Ruler
	_DisposeHandle
	brl exit

pararrayok
	MoveLong #2,Dsize
	PushLong Dsize
	PushLong Dest
	jsl D_GrowHandle
	sta	err
	bcc	@1
	tool	_DisposeHandle,in=Block:l
	tool	_DisposeHandle,in=Ruler:l
	tool	_DisposeHandle,in=Dest:l
	brl	exit

@1	MoveLong #2,Dsize

	MoveLong [Dest],Dptr
	MoveWord #0,[Dptr]

	PushLong Bsize
	PushLong Block
	jsl D_GrowHandle

	stz ParSize+2

loop	jsl D_BeachBall
	jsr GetNextCR
	sta ParSize
	jcs through

;check the ruler
	SpaceWord
	jsr GetRulerAttr
	pla
	cmp X_ScrStdRuler+2
	beq nonewruler

	sta X_ScrStdRuler+2

	MoveLong [Ruler],Rptr
	lda [Rptr]
	bne nothrowaway

	PushLong Ruler
	_DisposeHandle

nothrowaway
	SpaceLong
	PushLong #X_RulerSize
	jsl D_NeedHand
	sta err
	PullLong X_BRuler
	bcc wehavenewruler
	PushLong Dest
	jsl X_DisposeWPScrap
	brl exit

wehavenewruler
	PushLong #X_ScrStdRuler
	PushLong X_BRuler
	PushLong #X_RulerSize
	_PtrToHand

	MoveLong X_BRuler,Ruler

nonewruler
	AddLong ParSize,Bsize,NewBlockSize
	Cmpl NewBlockSize,#X_Blocksize
	jlt docopypar

	MoveLong [Block],Bptr
	lda [Bptr]
	cmp #4
	jeq docopypar

	SpaceLong
	AddLong ParSize,#4,Bsize
	PushLong Bsize
	jsl D_NeedHand
	sta err
	PullLong Block
	jcs exit

	MoveLong Block,X_BHand
	MoveWord #4,X_BOffset
	MoveLong #4,Boff
	MoveLong #4,Bsize

	MoveLong [Block],Bptr
	MoveWord #4,[Bptr]
	MoveWord #4,[Bptr]:#2

docopypar
	Addwl ParSize,Bsize
	PushLong Bsize
	PushLong Block
	jsl D_GrowHandle
	MoveLong [Block],Bptr
	lda [Bptr]
	sta X_BOffset
	MoveWord Bsize,[Bptr]
	MoveWord Bsize,[Bptr]:#2
	Addwl Boff,Bptr

	Addwl ParSize,Boff

	PushLong Sptr
	PushLong Bptr
	PushLong ParSize
	_BlockMove

	Addwl ParSize,Sptr

	Addwl #X_ParBlockSize,Dsize

	PushLong Dsize
	PushLong Dest
	jsl D_GrowHandle

	MoveLong [Dest],Dptr
	lda [Dptr]
	inc a
	sta [Dptr]
	AddLong [Dest],Dsize,Dptr
	SubLong Dptr,#X_ParBlockSize,Dptr

	PushLong #X_CurrentParRecord
	PushLong Dptr
	PushLong #X_ParBlockSize
	_BlockMove

	AddWord X_BOffset,ParSize,X_BOffset

	MoveLong [Ruler],Rptr
	lda [Rptr]
	inc a
	sta [Rptr]

	brl loop

through
done
	PushLong Dest
	jsl X_MassageWPScrap

	brl exit

partoobig
	SpaceWord
	PushLong #ParTooBigMess
	jsl D_AlertBox
	pla
	MoveWord #-1,err
	PushWord #WPText
	PushLong Dest
	jsl X_DisposeScrap
	brl exit

ParTooBigMess	STR 'There is a paragraph larger than 64K.'

exit	PushWord X_OldCursor
	jsl D_SetCursor
	return

Header	DC.L	-1
	DC.A	0
	DC.B	CR

GetNextCR
	Cmpl Sptr,EndPtr
	blt doit
	sec
	rts

doit
	AddLong Sptr,#7,Cptr
	ldx #7
crloop	shortm
	lda [Cptr]
	cmp #CR
	beq foundcr
	cmp #1
	beq dofontchange
	cmp #5
	blt dootherchange
	longm
	inx
	IncLong Cptr
docompare
	brl crloop

foundcr	longm
	txa
	inc a
	clc
	rts

dofontchange
	longm
	Addwl #3,Cptr
	txa
	clc
	adc #3
	tax
	brl crloop

dootherchange
	longm
	Addwl #2,Cptr
	inx
	inx
	brl docompare

JustTable
	DC.W	X_scr_r_left
	DC.W	X_scr_r_center
	DC.W	X_scr_r_right
	DC.W	X_scr_r_full

SpaceTable
	DC.W	X_scr_r_single
	DC.W	X_scr_r_double
	DC.W	X_scr_r_triple
	DC.W	X_scr_r_single	;custom

GetRulerAttr
	ldx #0
	ldy #5
	lda [Sptr],y
	and #$ff
	asl a
	tax
	lda >JustTable,x
	sta 3,s
	ldy #6
	lda [Sptr],y
	and #$ff
	asl a
	tax
	lda >SpaceTable,x
	ora 3,s
	sta 3,s
	rts

	ENDP

;***********************************************************************
;
;   X_AsciitoWPScrap
;
***********************************************************************
X_ScAsciitoWPScrap	PROC	EXPORT
	;Using D_GlobalData
	;Using X_ClipData
	;Using X_WPScrapData
	;Using X_WPFileData
	;Using D_CursorData
	;Using D_IOData

	input Src:l

	local Ssize:l,Sptr:l,Dsize:l,Dptr:l,Doff:l
	local Pars:l,EndPtr:l,NewBlock:w
	local Ruler:l,Rptr:l,Block:l,Bptr:l,Bsize:l
	local Boff:l,RealBptr:l,Cptr:l,ParSize:l
	local NewBlockSize:l,CRFlag:l

	output Dest:l

	error err

	begin
	
	MoveWord >D_CurCursor,X_OldCursor

;Set up everything.  Check for empty scrap.

	stz err

	SpaceLong
	PushLong Src
	_GetHandlesize
	PullLong Ssize

	PushLong Src
	_HLock
	MoveLong [Src],Sptr
	AddLong Sptr,Ssize,EndPtr

	Cpzl Ssize
	jeq emptyscrap

;Get block for Q_Text.
	SpaceLong
	PushLong #X_Blocksize
	jsl D_NeedHand
	sta err
	PullLong Block
	jcs exit

	Movelong [Block],RealBptr
	AddLong RealBptr,#4,Bptr

	MoveWord #4,[RealBptr]
	MoveWord #4,[RealBptr]:#2

	MoveLong #4,Boff
	MoveLong #4,Bsize

	MoveLong Block,X_BHand

;Get ruler.  This is ;easy*.
	SpaceLong
	PushLong #X_RulerSize
	jsl D_NeedHand
	sta err
	PullLong Ruler
	jcc rulerok
	PushLong Block
	_DisposeHandle
	brl exit

rulerok	MoveLong [Ruler],Rptr

	jsl X_MakeStandardRuler

	PushLong #X_ScrStdRuler
	PushLong Rptr
	PushLong #X_RulerSize
	_BlockMove

	MoveLong Ruler,X_BRuler

;Get paragraph array.
	SpaceLong
	_MaxBlock
	PullLong Dsize

	SpaceLong
	PushLong Dsize
	jsl D_NeedHand
	sta err
	PullLong Dest
	jcc pararrayok
	
	call	D_NeedHand,in=(#2:l),out=(Dest:l),err=err
	bcc	gotdest
	
	PushLong Block
	_DisposeHandle
	PushLong Ruler
	_DisposeHandle
	brl exit

pararrayok
	MoveLong #2,Dsize
	PushLong Dsize
	PushLong Dest
	jsl D_GrowHandle		;No error on shrinking

gotdest
	MoveLong [Dest],Dptr
	MoveWord #0,[Dptr]

	PushLong Bsize
	PushLong Block
	jsl D_GrowHandle

	stz ParSize+2
	stz	NewBlock

loop	jsl D_BeachBall
	jsr GetNextCR
	sta ParSize
	jeq through
	bcc crthere
	MoveWord #1,CRFlag
	bra cont2
crthere	stz CRFlag
cont2	lda ParSize
	clc
	adc #7
	sta ParSize
	jcs partoobig

	AddLong ParSize,Bsize,NewBlockSize
	Cmpl NewBlockSize,#X_Blocksize
	jlt docopypar

	MoveLong [Block],Bptr
	lda [Bptr]
	cmp #4
	bne	@2
	stz	NewBlock
	bra	docopypar

@2
	SpaceLong
	AddLong ParSize,#4,Bsize
	PushLong Bsize
	jsl D_NeedHand
	sta err
	PullLong Block
	jcs memory_error

	inc	NewBlock

	MoveLong Block,X_BHand
	MoveWord #4,X_BOffset
	MoveLong #4,Boff
	MoveLong #4,Bsize

	MoveLong [Block],Bptr
	MoveWord #4,[Bptr]
	MoveWord #4,[Bptr]:#2

docopypar
	Addwl ParSize,Bsize
	PushLong Bsize
	PushLong Block
	jsl D_GrowHandle
	sta	err
	jcs	memory_error
	MoveLong [Block],Bptr
	lda [Bptr]
	sta X_BOffset
	MoveWord Bsize,[Bptr]
	MoveWord Bsize,[Bptr]:#2
	Addwl Boff,Bptr

	PushLong #Header
	PushLong Bptr
	PushLong #7
	_BlockMove

	Addwl ParSize,Boff
	SubWord ParSize,#7,ParSize
	Addwl #7,Bptr

	PushLong Sptr
	PushLong Bptr
	PushLong ParSize
	_BlockMove

	Addwl ParSize,Sptr

	Addwl #X_ParBlockSize,Dsize

	PushLong Dsize
	PushLong Dest
	jsl D_GrowHandle
	sta	err
	bcc	@1
	lda	NewBlock
	jeq	exit
	tool	_DisposeHandle,in=(Block:l)
	brl	memory_error
	
@1
	MoveLong [Dest],Dptr
	lda [Dptr]
	inc a
	sta [Dptr]
	AddLong [Dest],Dsize,Dptr
	SubLong Dptr,#X_ParBlockSize,Dptr

	PushLong #X_CurrentParRecord
	PushLong Dptr
	PushLong #X_ParBlockSize
	_BlockMove

	AddWord X_BOffset,ParSize,X_BOffset

	MoveLong [Ruler],Rptr
	lda [Rptr]
	inc a
	sta [Rptr]

	lda CRFlag
	bne through
	brl loop

through
	lda ParSize
	beq blank_line

	IncLong Bsize
	PushLong Bsize
	PushLong Block
	jsl D_GrowHandle
	sta	err
	jcs	memory_error

	MoveLong [Block],Bptr
	lda [Bptr]
	tay
	shortm
	lda #CR
	sta [Bptr],y
	longm
	lda [Bptr]
	inc a
	sta [Bptr]
	ldy #2
	sta [Bptr],y
	brl done

blank_line
	SpaceLong
	PushLong #12
	jsl D_NeedHand
	sta	err
	jcs	memory_error
	PullLong Block
	MoveLong [Block],Bptr
	MoveWord #12,[Bptr]
	MoveWord #12,[Bptr]:#2
	Addwl #4,Bptr

	PushLong #Header
	PushLong Bptr
	PushLong #8
	_BlockMove

	MoveWord #4,X_BOffset

	Addwl #X_ParBlockSize,Dsize

	PushLong Dsize
	PushLong Dest
	jsl D_GrowHandle
	sta	err
	bcc	@1
	tool	_DisposeHandle,in=(Block:l)
	brl	memory_error

@1
	MoveLong [Dest],Dptr
	lda [Dptr]
	inc a
	sta [Dptr]
	AddLong [Dest],Dsize,Dptr
	SubLong Dptr,#X_ParBlockSize,Dptr

	MoveLong Block,X_BHand
	MoveWord #4,X_BOffset

	PushLong #X_CurrentParRecord
	PushLong Dptr
	PushLong #X_ParBlockSize
	_BlockMove

	MoveLong [Ruler],Rptr
	lda [Rptr]
	inc a
	sta [Rptr]

done
	PushLong Dest
	jsl X_MassageWPScrap

	brl exit

partoobig
	SpaceWord
	PushLong #ParTooBigMess
	jsl D_AlertBox
	pla
	MoveWord #-1,err
	PushWord #WPText
	PushLong Dest
	jsl X_DisposeScrap
	brl exit

ParTooBigMess	STR 'There is a paragraph larger than 64K.'

emptyscrap
	SpaceLong
	PushLong #2
	jsl D_NeedHand
	sta	err
	PullLong Dest
	bcs	exit

	MoveLong [Dest],Dptr
	MoveWord #0,[Dptr]
	bra	exit

memory_error
	call	X_DisposeWpScrap,in=(Dest:l)
	MoveLong	#-1,Dest

exit	PushWord X_OldCursor
	jsl D_SetCursor
	return

Header	DC.L	-1
	DC.A	0
	DC.B	CR

GetNextCR
	MoveLong Sptr,Cptr
	ldx #0
crloop	Cmpl Cptr,EndPtr
	bge endloop
	shortm
	lda [Cptr]
	cmp #CR
	beq foundcr
	longm
	inx
	IncLong Cptr
	bra crloop
endloop	txa
	sec
	rts

	LONGA	off
foundcr	lda	#CR
	txy
	sta	[Sptr],y
	longm
	txa
	inc a
	clc
	rts

	ENDP
	
	END
	