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

	IMPORT	D_BeachBall
	IMPORT	CellBody
	IMPORT	D_CurCursor
	IMPORT	D_Deref
	IMPORT	EndCellBody
	IMPORT	X_FSScratch
	IMPORT	D_GrowHandle
	IMPORT	X_InitFieldScanner
	IMPORT	D_NeedHand
	IMPORT	X_NextField
	IMPORT	X_OldCursor
	IMPORT	X_FormatValue
	IMPORT	D_SetCursor
	IMPORT	D_UnLock
	import	X_ClipData
	import	X_AsciiFilter
	import	X_Blurb1
	import	X_FSBlurbString
	import	X_SSButton1Text
	import	X_Button1Text
	import	X_SSButton2Text
	import	X_Button2Text
	import	X_SSButton3Text
	import	X_Button3Text
	import	X_SSContText
	import	X_Conttext
	import	X_FSDialogTemplate
	import	D_GetNewModalDialog
	import	D_DigitsOnly
	import	D_AlertBox
	import	X_DoAsciitoSSScrap
	import	D_CloseDialog
	import	FSDialogTemplate
	import	X_NumberStr


;*********************************************************************
;
;   X_DoAsciitoSSScrap
;
;*********************************************************************
X_ScDoAsciitoSSScrap	PROC	EXPORT
	;Using X_SSScrapData
	;Using X_FieldScannerData
	;Using D_CursorData
	;Using X_ClipData

	input src:l,sepflag:w,scanfields:w
	output dest:l
	local dptr:l,rowindex:w,colindex:w,dsize:l
	local maxcol:w,tmpsize:l,newsize:l,result:w
	local crflag:w
	local Sptr:l,ScrapSize:l
	error err

	begin

	MoveWord >D_CurCursor,X_OldCursor

	SpaceLong
	PushLong Src
	_GetHandleSize
	PullLong ScrapSize
	AddLong [Src],ScrapSize,Sptr

	short
	lda [Sptr]
	cmp #CR
	bne nocr
	long

	PushLong ScrapSize
	PushLong Src
	jsl D_GrowHandle
	bne go

nocr	long
go

	stz maxcol
	moveword #1,rowindex
	sta colindex

	call X_InitFieldScanner,in=(Src:l,sepflag:w,scanfields:w)

	tool _MaxBlock,out=(ScrapSize:l)
	call D_NeedHand,in=(ScrapSize:l),out=(dest:l),err=(err)
	jcs exit

	call D_GrowHandle,in=(#HeaderSize:l,dest:l),err=(err)
	jcs exit

	movelong #HeaderSize,dsize

	stz tmpsize+2
	stz crflag
cellloop	call D_BeachBall
	call X_NextField,out=(Result:w)

	lda X_FSScratch
	and #$FF
	sta tmpsize
	jeq contloop

	addword a,#12,a

	addlong a,dsize,newsize
	call D_GrowHandle,in=(newsize:l,dest:l),err=(err)
	jcs exit

	addlong [dest],dsize,dptr

	moveword rowindex,[dptr]:#X_SSScrapCellLoc
	moveword colindex,[dptr]:#X_SSScrapCellLoc+2
	addword tmpsize,#12,[dptr]:#X_SSScrapCellSize
	movelong #0,[dptr]:#X_SSScrapFormat
	moveword #0,[dptr]:#X_SSScrapValueSize

	inc tmpsize

	pushlong #X_FSScratch
	addlong dptr,#X_SSScrapValue,s
	tool _BlockMove,in=(:l,:l,tmpsize:l)

	movelong newsize,dsize

contloop	lda result	  ; 0=cell/tab, 1=cell/cr, -1=X_EOF
	beq docol
	cmp #-1
	beq exitloop

dorow	inc rowindex
	inc crflag	  ; have just seen a cr.
	cmpw colindex,maxcol
	blt didmax
	sta maxcol
didmax	stz colindex

docol	inc colindex
	stz crflag	  ; have just seen a tab.
	brl cellloop

exitloop	tool _GetHandleSize,in=(dest:l),out=(>scrsize:l)

	lda crflag
	beq didrows
	dec rowindex
DidRows	moveword rowindex,>ScrRows
	cmpw maxcol,colindex
	bge DidCols
	moveword colindex,maxcol
DidCols	moveword maxcol,>ScrCols

	movelong [dest],dptr
	tool _BlockMove,in=(#ScrapHeader:l,dptr:l,#HeaderSize:l)

exit	call D_SetCursor,in=(X_OldCursor:w)
	return
abort	tool _DisposeHandle,in=(dest:l)
	brl exit

ScrapHeader
ScrRows	DS.B 2
ScrCols	DS.B 2
ScrParse	DC.W	2
ScrSize	DS.B 4
EndHeader
HeaderSize	equ EndHeader-ScrapHeader
	ENDP



X_ScImportAsciitoSSScrap	PROC	EXPORT
	;Using X_FieldScannerData
	;Using X_FSDialogData

	input Src:l

	local Dialog:l,ButtonValue:w,Fields:w

	output Dest:l

	error err

	begin

	PushLong Src
	jsl X_AsciiFilter

	MoveLong #X_Blurb1,X_FSBlurbString
	MoveLong #X_SSButton1Text,X_Button1Text
	MoveLong #X_SSButton2Text,X_Button2Text
	MoveLong #X_SSButton3Text,X_Button3Text
	MoveLong #X_SSContText,X_ContText

	stz ButtonValue

	SpaceLong
	PushLong #FSDialogTemplate
	jsl D_GetNewModalDialog
	sta err
	PullLong Dialog
	jcs exit

	PushWord #ArrowCursor
	jsl D_SetCursor

dialogloop
	SpaceWord
	PushLong #D_DigitsOnly+$80000000
	_ModalDialog
	pla
	beq dialogloop
	cmp #Cancel
	jeq docancel
	cmp #OK
	jeq dook
	sec
	sbc #200
	asl a
	tax
	jmp (buttontable,x)

buttontable
	DC.W	dobuttonone,dobuttontwo,dobuttonthree,dobuttonthree
	DC.W	dobuttonthree

dobuttonone
	PushWord #1
	PushLong Dialog
	PushWord #200
	_SetDItemValue

	stz ButtonValue
	brl dialogloop

dobuttontwo
	PushWord #1
	PushLong Dialog
	PushWord #201
	_SetDItemValue

	MoveWord #1,ButtonValue
	brl dialogloop

dobuttonthree
	PushWord #1
	PushLong Dialog
	PushWord #202
	_SetDItemValue

	MoveWord #2,ButtonValue
	brl dialogloop

docancel	MoveWord #-1,err
	brl exit

dook	lda ButtonValue
	cmp #2
	jne notcrnum

	PushLong Dialog
	PushWord #203
	PushLong #X_NumberStr
	_GetIText

	lda X_NumberStr
	and #$ff
	beq dostringerr

	SpaceWord
	PushLong #X_NumberStr+1
	lda X_NumberStr
	and #$ff
	pha
	pea 0
	_Dec2Int
	PullWord Fields
	bpl cont

dostringerr
	SpaceWord
	PushWord #OKBox
	PushLong #FieldErrorString
	jsl D_AlertBox
	pla
	brl dialogloop

notcrnum	stz Fields

cont	PushWord #WatchCursor
	jsl D_SetCursor

	SpaceLong
	PushLong Src
	PushWord ButtonValue
	PushWord Fields
	jsl X_DoAsciitoSSScrap
	sta err
	PullLong Dest

exit	PushLong Dialog
	jsl D_CloseDialog

	return err

FieldErrorString	STR 'Please type the number of columns.'

	ENDP

	END

