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


	IMPORT	X_AsciitoWPScrap
	IMPORT	D_BeachBall
	IMPORT	D_CurCursor
	IMPORT	X_DBtoPLScrap
	IMPORT	D_GrowHandle
	IMPORT	D_GrowLHandle
	IMPORT	D_NeedHand
	IMPORT	D_NeedHandle
	IMPORT	X_OldCursor
	IMPORT	X_ParBlockSize
	IMPORT	D_SetCursor
	import	X_AsciiFilter
	IMPORT	CellBody
	IMPORT	D_Deref
	IMPORT	EndCellBody
	IMPORT	X_FSScratch
	IMPORT	X_InitFieldScanner
	IMPORT	X_NextField
	IMPORT	X_FormatValue
	IMPORT	D_UnLock
	import	X_ClipData


X_ScPLtoAsciiScrap	PROC	EXPORT
	;Using D_CursorData
	;Using X_ClipData

	input Src:l

	local Sptr:l,Ssize:l,Dptr:l,Dsize:l,Doff:l
	local EndDptr:l,EndSptr:l

	output Dest:l

	error err

	begin
	
	MoveWord >D_CurCursor,X_OldCursor
	stz err

	SpaceLong
	PushLong Src
	_GetHandleSize
	sta err
	PullLong Ssize
	jcs exit

	SpaceLong
	PushLong Ssize
	jsl D_NeedHand
	sta err
	PullLong Dest
	jcs exit

	MoveLong [Dest],Dptr
	Stzl Doff

	PushLong Src
	_HLock

	AddLong [Src],#7,Sptr
	AddLong [Src],Ssize,EndSptr

loop	Cmpl Sptr,EndSptr
	jge done
	shortm
	lda [Sptr]
	cmp #$20
	bge normal
	cmp #$9
	beq normal
	cmp #CR
	bne notcr
	longm
	Addwl #7,Sptr
	jsl D_BeachBall
	shortm
	lda #CR
	brl normal

	LONGA Off
notcr	cmp #1
	bne notfont
	longm
	Addwl #3,Sptr
	brl loop

	LONGA Off
notfont	longm
	Addwl #2,Sptr
	brl loop

	LONGA Off
normal	sta [Dptr]
	longm

	IncLong Sptr
	IncLong Doff
	IncLong Dptr
	brl loop

mem_err	PushLong Dest
	_DisposeHandle

	PushLong Src
	_HUnlock
	brl exit

done	PushLong Src
	_HUnlock

	PushLong Doff
	PushLong Dest
	jsl D_GrowHandle

exit	PushWord X_OldCursor
	jsl D_SetCursor
	return err

	ENDP

;************************************************************************
;
;   X_WPtoAsciiScrap
;
;************************************************************************

X_ScWPtoAsciiScrap	PROC	EXPORT
	;Using X_SWScrapEquates
	;Using X_WPScrapData
	;Using D_CursorData
	;Using X_ClipData

	input Src:l

	local Sptr:l,Pars:w,Block:l,Bptr:l,Bsize:l
	local Dptr:l,Dsize:l,FirstPar:w,Offset:w,NextPar:w
	local Doff:l,Pptr:l

	output Dest:l

	error err

	begin
	
	MoveWord >D_CurCursor,X_OldCursor

	stz err

	Spacelong
	_MaxBlock
	PullLong Dsize

	SpaceLong
	PushLong Dsize
	PushWord #X_Locked
	jsl D_NeedHandle
	sta err
	PullLong Dest
	jcs exit1

	PushLong Src
	_HLock

	MoveLong [Src],Sptr
	MoveWord [Sptr],Pars
	AddLong Sptr,#2,Pptr
	MoveLong [Pptr],Block
	PushLong Block
	_HLock

	MoveLong [Block],Bptr
	MoveWord [Bptr],Bsize
	MoveLong [Dest],Dptr
	stz Dsize+2
	MoveWord Bsize,Dsize

	stzl Doff

parloop	jsl D_BeachBall
	MoveWord [Pptr]:#4,Offset
	AddWord Offset,#7,Offset
charloop	shortm
	ldy Offset
	lda [Bptr],y
	cmp #CR+1
	jge notspecial

	cmp #9
	jeq notspecial

	cmp #CR
	jeq X_DoCR

	cmp #1
	jeq dofont

	longm
	AddWord Offset,#2,Offset
	brl charloop

dofont	longm
	AddWord Offset,#3,Offset
	brl charloop

X_DoCR	sta [Dptr]
	longm
	inc Doff
	bne x4
	inc Doff+2
x4	inc Dptr
	bne x5
	inc Dptr+2
x5	inc Offset
	dec Pars
	jeq exit

	AddLong Pptr,#X_ParBlockSize,Pptr
	Cmpl Block,[Pptr]
	jeq parloop
	MoveLong [Pptr],Block
	MoveLong [Block],Bptr
	MoveWord [Bptr]:#2,Bsize
	stz Bsize+2
	lda Dsize
	clc
	adc Bsize
	sta Dsize
	bcc noadd1
	inc Dsize+2
noadd1	SpaceLong
	PushLong Dsize
	PushLong Dest
	jsl D_GrowLHandle
	sta err
	PullLong Dptr
	jcs Exit
	AddLong Dptr,Doff,Dptr
	brl parloop

	LONGA Off
notspecial
	sta [Dptr]
	longm
	inc Doff
	bne x1
	inc Doff+2
x1	inc Dptr
	bne x2
	inc Dptr+2
x2	inc Offset
	brl charloop

exit	PushLong Block
	_HUnlock

	PushLong Src
	_HUnlock

	PushLong Dest
	_HUnlock

	lda err
	jeq NoErrors
	pushlong Dest
	_DisposeHandle
	bra Exitcursor

NoErrors
	lda Doff
	dec a
	tax
	bne nohistuff
	lda Doff+2
	dec a
	bra histuff
nohistuff
	lda Doff+2 
histuff	pha
	phx
	PushLong Dest
	jsl D_GrowHandle
	sta err

ExitCursor
	PushWord X_OldCursor
	jsl D_SetCursor
Exit1
	return err
	ENDP

;***********************************************************************
;
;   X_SStoAsciiScrap
;	   From a D_SS-type scrap, generate an ascii textblock (handle)
;	   s.t. cells are seperated by tabs and rows by carriage returns.
;
;X_SSScrapRows
;X_SSScrapcols
;X_SSScrapParseCode
;X_SSScrapSize	 ;Size of handle
;X_SSScrapCells	;Start of data
;
;X_SSScrapCellLoc
;X_SSScrapCellSize
;X_SSScrapFormat
;X_SSScrapValueSize
;X_SSScrapValue
***********************************************************************

X_ScSStoAsciiScrap	PROC	EXPORT
	;Using X_SSScrapData
	;Using D_CursorData
	;Using X_ClipData


	input src:l
	output dest:l
	local sptr:l,cptr:l,cellcount:l,ascptr:l,colcount:w,colindex:w
	local dsize:l,tmp:l,strptr:l,newsize:l,rowindex:w,mysize:l
	error err

	begin

	MoveWord >D_CurCursor,X_OldCursor

	cpzl src
	bne GetPtr
	moveword #-1,err
	brl quit

GetPtr	rcall D_Deref,in=(src:ax),out=(sptr:ax)

	; initial approximation of the size
	tool _MaxBlock,out=(mysize:l)
	call D_NeedHand,in=(mysize:l),out=(dest:l),err=(err)
	jcs exit

	moveword [sptr]:#X_SSScrapcols,colcount
	stzl dsize

	in [sptr]:#X_SSScrapRows:w,[sptr]:#X_SSScrapcols:w
	out cellcount:l
	xtool _Multiply

	addwl #X_SSScrapCells,sptr
	moveword #1,rowindex
	sta colindex

CellLoop	call D_BeachBall
	cpzl cellcount
	jeq EndCellLoop

	cmpw [sptr]:#X_SSScrapCellLoc,rowindex
	jne EmptyCell
	cmpw [sptr]:#X_SSScrapCellLoc+2,colindex
	jne EmptyCell

	moveword [sptr]:#X_SSScrapFormat,a
	and #X_SSCellType
	xba
	div16 a
	tax
	jmp (TypeTable,x)
TypeTable	DC.W	TextCell,ValueCell,CalcTextCell,FormulaCell
	DC.W	LpadCell,RpadCell,EmptyCell,EmptyCell

TextCell	stz tmp+2
	moveword [sptr]:#X_SSScrapValue,a
	and #$00FF
	sta tmp
	addlong a,dsize,newsize

	call D_GrowHandle,in=(newsize:l,dest:l),err=(err)
	jcs abort
	movelong [dest],ascptr

	addlong sptr,#X_SSScrapValue+1,s
	addlong ascptr,dsize,s
	tool _BlockMove,in=(:l,:l,tmp:l)
	movelong newsize,dsize
	brl DidCell

CalcTextCell	stz tmp+2
	moveword [sptr]:#X_SSScrapValueSize,a
	and #$00FF
	sta tmp
	addlong a,dsize,newsize

	call D_GrowHandle,in=(newsize:l,dest:l),err=(err)
	jcs abort
	movelong [dest],ascptr

	addlong sptr,#X_SSScrapValue,s
	addlong ascptr,dsize,s
	tool _BlockMove,in=(:l,:l,tmp:l)
	movelong newsize,dsize
	brl DidCell

FormulaCell
ValueCell	spacelong
	spacelong
	pushword #256
	pushlong [sptr]:#X_SSScrapFormat
	addlong #X_SSScrapValue,sptr,s
	call X_FormatValue	  ; in=(:w,:l,:l),out=(:w,:l,:w)
	ply
	pulllong strptr
	ply

	stz tmp+2
	lda [strptr]
	and #$00FF
	tay
	lda [strptr],y
	and #$FF
	cmp #' '
	bne GetSize
	dey
GetSize	sty tmp
GotSize	tya
	addlong a,dsize,newsize

	incl strptr

	call D_GrowHandle,in=(newsize:l,dest:l),err=(err)
	jcs abort
	movelong [dest],ascptr

	pushlong strptr
	addlong ascptr,dsize,s
	tool _BlockMove,in=(:l,:l,tmp:l)
	movelong newsize,dsize


LPadCell
RPadCell
DidCell	moveword [sptr]:#X_SSScrapCellSize,tmp
	stz tmp+2
	addlong tmp,sptr,sptr

EmptyCell	incl dsize
	call D_GrowHandle,in=(dsize:l,dest:l),err=(err)
	jcs abort
	movelong [dest],ascptr
	addlong ascptr,dsize,ascptr	 ; set ascptr to last byte
	decl ascptr	 ;

	lda colindex
	ina
	sta colindex
	cmp colcount
	ble DoTab
	moveword #1,colindex
	inc rowindex
	lda #CR
	bra DoSep

DoTab	lda #Tab
DoSep	shortm
	sta [ascptr]
	longm

ContCell
	decl cellcount
	brl CellLoop
EndCellLoop

exit	rcall D_UnLock,in=(src:ax)
	tool _GetHandleSize,in=(dest:l),out=(dsize:l)
	IncLong dsize
	call D_GrowHandle,in=(dsize:l,dest:l)
	DecLong dsize
	AddLong [Dest],dsize,cptr
	MoveByte #CR,[cptr]
quit	call D_SetCursor,in=(X_OldCursor:w)
	return
abort	tool _DisposeHandle,in=(Dest:l)
	brl exit
	ENDP

	END