	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_CantImportStr
	IMPORT	D_CurCursor
	IMPORT	X_CurrentParRecord
	IMPORT	X_DisposeWPScrap
	IMPORT	D_GrowHandle
	IMPORT	D_GrowLHandle
	IMPORT	X_MakeStandardRuler
	IMPORT	X_MassageWPScrap
	IMPORT	D_Message1
	IMPORT	D_Message2
	IMPORT	D_MessageThere
	IMPORT	D_NeedHand
	IMPORT	D_NeedHandle
	IMPORT	X_OldCursor
	IMPORT	X_ParBlockSize
	IMPORT	X_RulerSize
	IMPORT	X_ScrStdRuler
	IMPORT	D_SetCursor
	IMPORT	X_ClipData

	ENTRY	X_DoMSGStoWPScrap

;************************************************************************
;
;   X_MSGStoWPScrap
;
;************************************************************************
X_ScMSGStoWPScrap	PROC	EXPORT
	;Using D_OpenData
	;Using X_ClipData

	input Src:l

	local Sptr:l,Sstart:l,Send:l,Temp:l,Temp2:l

	output Dest:l

	error err

	begin
	
	lda #0
	sta >D_MessageThere
	sta >D_Message1
	sta >D_Message1+2
	sta >D_Message2
	sta >D_Message2+2

	MoveLong [Src],Sptr
	MoveLong [Sptr]:#$18,Sstart
	MoveLong [Sptr]:#$1c,Send

	SpaceLong
	PushLong Src
	PushLong Sstart
	PushLong Send
	jsl X_DoMSGStoWPScrap
	sta err
	PullLong Dest
	jcs exit

	MoveLong [Dest],Temp2
	Cmpw [Temp2],#2
	bge notnull

	MoveLong [Temp2]:#2,Temp
	MoveLong [Temp],Temp2
	Cmpw [Temp2]:#2,#13
	bge notnull

	SpaceWord
	PushWord #OKBox
	PushLong #X_CantImportStr
	jsl D_AlertBox
	pla

	PushLong Dest
	jsl X_DisposeWPScrap

	MoveWord #-1,err

	brl exit

notnull
	MoveLong [Src],Sptr
	MoveWord [Sptr]:#$1c,Sstart
	MoveWord [Sptr]:#$20,Send

	Cmpw Sstart,Send
	jeq dofooter

	Addwl #$20,Sstart

	SpaceLong
	PushLong Src
	PushLong Sstart
	PushLong Send
	jsl X_DoMSGStoWPScrap
	sta err
	PullLong >D_Message1
	bcc noerr

	PushLong Dest
	_DisposeHandle

	lda #0
	sta >D_Message1
	sta >D_Message1+2
	sta >D_MessageThere
	brl exit

noerr	MoveWord #1,>D_MessageThere

dofooter	MoveWord [Sptr]:#$20,Sstart
	MoveWord [Sptr]:#$24,Send
	Cmpw Sstart,Send
	jeq exit

	Addwl #$20,Sstart

	SpaceLong
	PushLong Src
	PushLong Sstart
	PushLong Send
	jsl X_DoMSGStoWPScrap
	sta err
	PullLong >D_Message2
	bcc noerr2

	PushLong Dest
	_DisposeHandle

	lda >D_Message1+2
	pha
	lda >D_Message1
	pha
	_DisposeHandle

	lda #0
	sta >D_MessageThere
	sta >D_Message1
	sta >D_Message1+2
	sta >D_Message2
	sta >D_Message2+2
	bra exit

noerr2	MoveWord #1,>D_MessageThere

exit	return

	ENDP

X_DoMSGStoWPScrap	PROC	EXPORT
	;Using X_WPScrapData
	;Using X_MSGSOffsets
	;Using X_SWScrapEquates
	;Using X_ClipData
	;Using X_WPFileData
	;Using D_CursorData

	input Src:l,Sstart:l,Send:l

	local Sptr:l,Soff:l,Ssize:l
	local Dptr:l,Block:l,Bptr:l,Bsize:w,Dsize:l
	local Ruler:l,Rptr:l,RulerFlag:w,Bpars:w
	local Offset:w,CurPar:l,RealDptr:l,Doff:l,CurParSize:w

	output Dest:l

	error err

	begin

	MoveWord >D_CurCursor,X_OldCursor

	stz err

	PushLong Src
	_HLock

	MoveLong [Src],Sptr
	SubLong Send,Sstart,Ssize
	AddLong Sptr,Sstart,Sptr

	Cmpl Ssize,#$20
	jeq blankdoc

	SpaceLong
	_MaxBlock
	PullLong Dsize

	SpaceLong
	PushLong Dsize
	PushWord #X_Locked
	jsl D_NeedHandle
	PullLong Dest
	MoveLong #2,Doff

	MoveLong #(X_ParBlockSize+2),Dsize

	SpaceLong
	PushLong Dsize
	PushLong Dest
	jsl D_GrowLHandle
	PullLong RealDptr

	AddLong RealDptr,#2,Dptr
	lda #0
	sta [RealDptr]

	SpaceLong
	PushLong #X_RulerSize
	PushWord #X_Locked
	jsl D_NeedHandle
	PullLong Ruler

	MoveLong [Ruler],Rptr
	MoveLong Ruler,X_BRuler

	SpaceLong
	PushLong #X_Blocksize
	PushWord #X_Locked
	jsl D_NeedHandle
	PullLong Block

	MoveWord #4,Offset
	MoveWord #4,X_BOffset
	MoveLong [Block],Bptr
	MoveWord #X_Blocksize,[Bptr]
	MoveWord Offset,[Bptr]:#2
	MoveLong Block,X_BHand

	MoveWord #X_Geneva,>CurHeader
	lda	#0
	sta	>CurHeader+2
	sta	>CurHeader+4
	shortm
	sta	>CurHeader+6
	lda	#12
	sta	>CurHeader+3
	longm

	PushLong #CurHeader
	lda Bptr
	clc
	adc Offset
	tax
	lda Bptr+2
	adc #0
	pha
	phx
	PushLong #7
	_BlockMove

	AddLong Offset,#7,Offset

	PushLong #X_CurrentParRecord
	PushLong Dptr
	PushLong #X_ParBlockSize
	_BlockMove

	stz Bpars

;Initialize ruler
	jsl X_MakeStandardRuler
	jsr SetUpRuler

	PushLong #X_ScrStdRuler
	PushLong Rptr
	PushLong #X_RulerSize
	_BlockMove

	AddLong Sptr,#X_MSRulerSize,Sptr
	MoveLong #X_MSRulerSize,Soff

	MoveLong Sptr,CurPar
	MoveWord #0,CurParsize
	MoveWord #X_Blocksize,Bsize

charloop	shortm
	lda [Sptr]
	cmp #CR+1
	jge normal
	cmp #9
	jeq normal
	cmp #CR
	jne notcr
	ldy Offset
	sta [Bptr],y
	longm
	jsl D_BeachBall
	inc Offset
	lda Offset
	cmp Bsize
	blt noprob

	lda Bpars
	cmp #1
	bne startagain
	jsr ResizeBlock
	bra noprob

startagain
	jsr NewParBlock
	brl charloop

noprob
	MoveWord Offset,[Bptr]:#2
	AddWord Offset,#7,a
	cmp Bsize
	jge toosmall

	PushLong #CurHeader
	lda Bptr
	clc
	adc Offset
	tax
	lda Bptr+2
	adc #0
	pha
	phx
	PushLong #7
	_BlockMove

growdest	AddLong Dsize,#X_ParBlockSize,Dsize
	AddLong Doff,#X_ParBlockSize,Doff
	SpaceLong
	PushLong Dsize
	PushLong Dest
	jsl D_GrowLHandle
	PullLong RealDptr
	AddLong RealDptr,DOff,Dptr

	MoveWord Offset,X_BOffset
	PushLong #X_CurrentParRecord
	PushLong Dptr
	PushLong #X_ParBlockSize
	_BlockMove

	AddWord Offset,#7,Offset
	lda [RealDptr]
	inc a
	sta [RealDptr]
	lda [Rptr]
	inc a
	sta [Rptr]

	inc Sptr
	bne x3
	inc Sptr+2
x3	inc Soff
	bne crendofscrap
	inc Soff+2

crendofscrap
	Cmpl Soff,Ssize
	stz CurParSize
	jge done

checkruler
	shortm
	lda [Sptr]
	cmp #X_MSRuler
	jeq doruler
	cmp #X_PageBreak
	jeq X_DoPageBreak
	longm

	MoveLong Sptr,CurPar
	Stz CurParSize
	inc Bpars

	brl charloop

doruler	longm
	jsr SetUpRuler

	PushLong Ruler
	_HUnlock

	SpaceLong
	PushLong #X_RulerSize
	PushWord #X_Locked
	jsl D_NeedHandle
	PullLong Ruler
	MoveLong [Ruler],Rptr

	PushLong #X_ScrStdRuler
	PushLong Rptr
	PushLong #X_RulerSize
	_BlockMove

	MoveLong Ruler,X_BRuler
	MoveLong Ruler,[Dptr]:#X_WPRulerHand

	AddLong Sptr,#X_MSRulerSize,Sptr
	AddLong Soff,#X_MSRulerSize,Soff
	stz CurParSize
	brl crendofscrap


	LONGA Off
X_DoPageBreak
	lda #CR
	ldy Offset
	sta [Bptr],y
	longm
	inc Offset
	lda Offset
	cmp Bsize
	blt noprobpb

	lda Bpars
	cmp #1
	bne startpb
	jsr ResizeBlock
	bra nottoosmall

startpb	jsr NewParBlock
	brl charloop

noprobpb
	AddWord Offset,#7,a
	cmp Bsize
	jlt nottoosmall

	jsr GetNewBlock

nottoosmall
	PushLong #CurHeader
	lda Bptr
	clc
	adc Offset
	tax
	lda Bptr+2
	adc #0
	pha
	phx
	PushLong #7
	_BlockMove

	longm
	MoveWord #1,[Dptr]:#X_scpAttr
	MoveLong #0,[Dptr]:#X_scpRulerHand

	AddLong Doff,#X_ParBlockSize,Doff
	AddLong Dsize,#X_ParBlockSize,Dsize
	SpaceLong
	PushLong Dsize
	PushLong Dest
	jsl D_GrowLHandle
	PullLong RealDptr
	AddLong RealDptr,Doff,Dptr

	lda [RealDptr]
	inc a
	sta [RealDptr]

	MoveWord Offset,X_BOffset

	PushLong #X_CurrentParRecord
	PushLong Dptr
	PushLong #X_ParBlockSize
	_BlockMove

	MoveWord Offset,[Bptr]:#2
	AddWord Offset,#7,Offset

	inc Sptr
	bne x100
	inc Sptr+2
x100	inc Soff
	jne crendofscrap
	inc Soff+2
	stz CurParSize
	brl crendofscrap

toosmall	jsr GetNewBlock
	brl growdest

	LONGA Off

notcr	cmp #X_FontRecord
	jne normal

	longm
	ldy #1
	lda >CurHeader
	cmp [Sptr],y
	beq nonewfont

	AddWord Offset,#3,a
	cmp Bsize
	jlt dofontchange
	lda Bpars
	cmp #1
	bne fontnewpar
	jsr ResizeBlock
	bra dofontchange
fontnewpar	jsr NewParBlock
	brl charloop

dofontchange
	shortm
	lda #X_NewFamily
	ldy Offset
	sta [Bptr],y
	longm
	inc Offset
	ldy #1
	lda [Sptr],y
	ldy Offset
	sta [Bptr],y
	sta >CurHeader
	AddWord Offset,#2,Offset

nonewfont	shortm
	ldy #X_MSFontFlags
	ldx #X_FontStyle
	lda >CurHeader,x
	cmp [Sptr],y
	beq nonewstyle

	longm
	AddWord Offset,#2,a
	cmp Bsize
	jlt dostylechange
	lda Bpars
	cmp #1
	bne stylenewpar
	jsr ResizeBlock
	bra dostylechange
stylenewpar	jsr NewParBlock
	brl charloop

dostylechange
	shortm
	lda #X_FontStyle
	ldy Offset
	sta [Bptr],y
	longm
	inc Offset
	shortm
	ldy #X_MSFontFlags
	lda [Sptr],y
	ldy Offset
	sta [Bptr],y
	ldx #X_FontStyle
	sta >CurHeader,x
	longm
	inc Offset
	shortm

nonewstyle
	ldy #X_MSFontSize
	ldx #X_FontSize
	lda >CurHeader,x
	cmp [Sptr],y
	beq nonewsize

	longm
	AddWord Offset,#2,a
	cmp Bsize
	jlt dosizechange
	lda Bpars
	cmp #1
	bne sizenewpar
	jsr ResizeBlock
	bra dosizechange
sizenewpar	jsr NewParBlock
	brl charloop

dosizechange
	shortm
	lda #X_FontSize
	ldy Offset
	sta [Bptr],y
	longm
	inc Offset
	shortm
	ldy #X_MSFontSize
	lda [Sptr],y
	ldy Offset
	sta [Bptr],y
	ldx #X_FontSize
	sta >CurHeader,x
	longm
	inc Offset
	shortm

nonewsize
	ldy #X_MSFontColor
	ldx #X_FontColor
	lda >CurHeader,x
	cmp [Sptr],y
	beq nochange

	longm
	AddWord Offset,#2,a
	cmp Bsize
	jlt docolorchange
	lda Bpars
	cmp #1
	bne colornewpar
	jsr ResizeBlock
	bra docolorchange
colornewpar	jsr NewParBlock
	brl charloop

docolorchange
	shortm
	lda #X_FontColor
	ldy Offset
	sta [Bptr],y
	longm
	inc Offset
	shortm
	ldy #X_MSFontColor
	lda [Sptr],y
	ldy Offset
	sta [Bptr],y
	ldx #X_FontColor
	sta >CurHeader,x
	longm
	inc Offset

nochange	longm
	AddLong Sptr,#X_MSFontRecSize,Sptr
	AddLong Soff,#X_MSFontRecSize,Soff
	AddWord CurParSize,#X_MSFontRecSize,CurParSize
	brl endofscraptest

	LONGA Off
normal	ldy Offset
	sta [Bptr],y
	longm
	inc Offset
	lda Offset
	cmp Bsize
	bge overflow
cont	inc Sptr
	bne x1
	inc Sptr+2
x1	inc Soff
	bne x2
	inc Soff+2
x2	inc CurParSize
endofscraptest
	Cmpl Soff,Ssize
	jlt charloop
	brl done

overflow	lda Bpars
	bne noresize
	jsr ResizeBlock
	brl cont

noresize	jsr NewParBlock
	brl charloop

done	shortm
	lda #CR
	ldy Offset
	sta [Bptr],y
	longm
	inc Offset
	lda Offset
	sta [Bptr]
	ldy #2
	sta [Bptr],y

	PushLong Block
	_Hunlock

	pea 0
	PushWord Offset
	PushLong Block
	_SetHandleSize

	lda [Rptr]
	inc a
	sta [Rptr]

	PushLong Ruler
	_HUnlock

	lda [RealDptr]
	inc a
	sta [RealDptr]

	PushLong Dest
	_HUnlock

	PushLong Dest
	jsl X_MassageWPScrap

exit	PushWord X_OldCursor
	jsl D_SetCursor
	return err

blankdoc
	SpaceLong
	PushLong #X_ParBlockSize+2
	jsl D_NeedHand
	sta err
	PullLong Dest
	jcs exit

	Spacelong
	PushLong #12
	jsl D_NeedHand
	sta err
	PullLong Block
	bcc noblankerr
	PushLong Dest
	_DisposeHandle
	brl exit

noblankerr
	SpaceLong
	PushLong #X_RulerSize
	jsl D_NeedHand
	sta err
	PullLong Ruler
	bcc noblankrulererr
	PushLong Dest
	_DisposeHandle
	PushLong Block
	_DisposeHandle
	brl exit

noblankrulererr
	MoveLong [Dest],Dptr
	MoveWord #1,[Dptr]
	MoveLong Block,[Dptr]:#2
	MoveWord #4,[Dptr]:#X_scpOffset+2
	MoveWord #0,[Dptr]:#X_scpAttr+2
	MoveLong Ruler,[Dptr]:#X_scpRulerHand+2
	MoveWord #0,[Dptr]:#X_scpPixels+2
	MoveLong #0,[Dptr]:#X_scpLinehand+2
	MoveWord #0,[Dptr]:#X_scpLastLine+2

	jsr SetUpRuler

	PushLong #X_ScrStdRuler
	PushLong Ruler
	PushLong #X_RulerSize
	_PtrToHand

	PushLong #BlankTextBlock
	PushLong Block
	PushLong #12
	_PtrToHand

	brl exit

BlankTextBlock
	DC.W	12,12,3
	DC.B	0,$c,0,0,0,$d

CurHeader
	DS.B 7

ResizeBlock

	SpaceLong

	AddWord Bsize,#X_Blocksize,Bsize
	pea 0
	PushWord Bsize
	PushLong Block
	jsl D_GrowLHandle
	PullLong Bptr
	MoveWord Bsize,[Bptr]
	rts

NewParBlock

	jsr GetNewBlock

	AddWord Offset,#7,Offset

	PushLong #X_CurrentParRecord
	PushLong Dptr
	PushLong #X_ParBlockSize
	_BlockMove

	MoveLong CurPar,Sptr
	sec
	lda Soff
	sbc CurParSize
	sta Soff
	lda Soff+2
	sbc #0
	sta Soff+2
	stz CurParSize
	stz Bpars
	rts

GetNewBlock
	MoveWord [Bptr]:#2,[Bptr]
	pea 0
	PushWord [Bptr]

	PushLong Block
	_HUnlock

	PushLong Block
	jsl D_GrowHandle

	MoveWord #X_Blocksize,Bsize
	SpaceLong
	PushLong #X_Blocksize
	PushWord #X_Locked
	jsl D_NeedHandle
	PullLong Block

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

	MoveWord #4,Offset

	PushLong #Curheader
	lda Bptr
	clc
	adc #4
	tax
	lda Bptr+2
	adc #0
	pha
	phx
	PushLong #7
	_BlockMove

	MoveLong Block,X_BHand
	MoveWord Offset,X_BOffset

	rts

MarginOffset	equ 6

SetUpRuler
	MoveWord [Sptr]:#X_MSLeftMargin,a
	and #$fffe
	clc
	adc #MarginOffset
	MoveWord a,X_ScrStdRuler:#X_scr_or_LeftM

	MoveWord [Sptr]:#X_MSRightMargin,a
	and #$fffe
	clc
	adc #MarginOffset
	MoveWord a,X_ScrStdRuler:#X_scr_or_RightM

	MoveWord [Sptr]:#X_MSIndent,a
	and #$fffe
	clc
	adc #MarginOffset
	MoveWord a,X_ScrStdRuler:#X_scr_or_IndentM

	MoveWord [Sptr]:#X_MSNumTabs,a
	and #$ff
	MoveWord a,X_ScrStdRuler:#X_scr_or_NumTabs
	MoveWord [Sptr]:#X_MSTab1,a

	MoveWord a,X_ScrStdRuler:#(X_scr_or_tab1)
	MoveWord [Sptr]:#X_MSTab2,a
	and #$fffe
	clc
	adc #MarginOffset
	MoveWord a,X_ScrStdRuler:#(X_scr_or_tab2)

	MoveWord [Sptr]:#X_MSTab3,a
	and #$fffe
	clc
	adc #MarginOffset
	MoveWord a,X_ScrStdRuler:#(X_scr_or_tab3)

	MoveWord [Sptr]:#X_MSTab4,a
	and #$fffe
	clc
	adc #MarginOffset
	MoveWord a,X_ScrStdRuler:#(X_scr_or_tab4)

	MoveWord [Sptr]:#X_MSTab5,a
	and #$fffe
	clc
	adc #MarginOffset
	MoveWord a,X_ScrStdRuler:#(X_scr_or_tab5)

	MoveWord [Sptr]:#X_MSTab6,a
	and #$fffe
	clc
	adc #MarginOffset
	MoveWord a,X_ScrStdRuler:#(X_scr_or_tab6)

	MoveWord [Sptr]:#X_MSTab7,a
	and #$fffe
	clc
	adc #MarginOffset
	MoveWord a,X_ScrStdRuler:#(X_scr_or_tab7)

	MoveWord [Sptr]:#X_MSTab8,a
	and #$fffe
	clc
	adc #MarginOffset
	MoveWord a,X_ScrStdRuler:#(X_scr_or_tab8)

	MoveWord [Sptr]:#X_MSTab9,a
	and #$fffe
	clc
	adc #MarginOffset
	MoveWord a,X_ScrStdRuler:#(X_scr_or_tab9)

	MoveWord [Sptr]:#X_MSTab10,a
	and #$fffe
	clc
	adc #MarginOffset
	MoveWord a,X_ScrStdRuler:#(X_scr_or_tab10)

	pea 0
	ldy #X_MSSpaceFlags
	lda [Sptr],y
	and #$ff
	lsr a
	bcc notnospace
	tax
	lda 1,s
	ora #X_scr_r_single
	sta 1,s
	txa
notnospace
	lsr a
	bcc notsingle
	tax
	lda 1,s
	ora #X_scr_r_single
	sta 1,s
	txa
notsingle
	lsr a
	bcc notdouble
	tax
	lda 1,s
	ora #X_scr_r_double
	sta 1,s
	txa
notdouble
	lsr a
	bcc nottriple
	tax
	lda 1,s
	ora #X_scr_r_triple
	sta 1,s
	txa
nottriple
	lsr a
	bcc notleft
	tax
	lda 1,s
	ora #X_scr_r_left
	sta 1,s
	brl stastyle

notleft	lsr a
	bcc notcenter
	tax
	lda 1,s
	ora #X_scr_r_center
	sta 1,s
	bra stastyle

notcenter
	lsr a
	bcc notright
	tax
	lda 1,s
	ora #X_scr_r_right
	sta 1,s
	bra stastyle

notright	lda 1,s
	ora #X_scr_r_full
	sta 1,s

stastyle	pla
	ldx #X_scr_or_Status
	sta X_ScrStdRuler,x

	rts

	ENDP
	END

