
	load 'macros.dump'
	include 'driver.equ'
	include 'pl.equ'
;------------------------------------------
;
; Equates from procedure T_WPRulData
;
;------------------------------------------
T_WPParsUsing	EQU  0
T_WPRulBits	EQU  2
T_WPLeftMarg	EQU  4
T_WPIndent	EQU  6
T_WPRightMarg	EQU  8
T_WPTabCount	EQU  10
T_WPTabArray	EQU  12
T_WPDefLM	EQU  80			  ;Defualt margins for D_WP
T_WPDefRM	EQU  600

;-----------------------------------------------
;
;   Imported addresses
;
;-----------------------------------------------

	IMPORT	D_FastMult
	IMPORT	D_GrowHandle
	IMPORT	P_FixRect
	IMPORT	T_MulParag
	IMPORT	T_NewRuler

;-----------------------------------------------
;
;   Forward addresses and entries
;
;-----------------------------------------------

	ENTRY	T_ClearMarks





;----------------------------------------------------------------------------
;
; T_ClearBottom (ObjHdl:l,VertPos:w,CurPage:w)
;
; Clears the bottom of a Q_Text thread, whatever that means


T_ClearBottom	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData
			;Using T_TextEqu

		INPUT ObjHdl:l,VertPos:w,CurPage:w
		LOCAL ObjPtr:l,Q_Rect:r

		BEGIN


		movelong [ObjHdl],ObjPtr
		cmpw [ObjPtr]:#P_PageNo,CurPage
		bne ObjLoop

		moverect [ObjPtr]:#P_TRect,Q_Rect
		addword Q_Rect,VertPos,Q_Rect
		cmpw Q_Rect,Q_Rect+4
		bge ObjLoop

		call P_FixRect,in=(!Q_Rect:l,#P_RECTANGLE:w,#0:w,#1:w)
		tool _EraseRect,in=(!Q_Rect:l)


ObjLoop
		movelong [ObjHdl],ObjPtr
		movelong [ObjPtr]:#P_NThread,ObjHdl
		cpzl ObjHdl
		beq Exit

		movelong [ObjHdl],ObjPtr
		cmpw [ObjPtr]:#P_PageNo,CurPage
		bne ObjLoop
		moverect [ObjPtr]:#P_TRect,Q_Rect
		call P_FixRect,in=(!Q_Rect:l,#P_RECTANGLE:w,#0:w,#1:w)
		tool _EraseRect,in=(!Q_Rect:l)
		bra ObjLoop


Exit	RETURN
		ENDP





;------------------------------------------------------------------------------
;
; T_WP2PLText (ParagHdl:l)
;
; Convert D_WP scrap to my scrap.  Expand each Parag record by 4 bytes
; and convert the rulers.

T_WP2PLText	PROC		EXPORT
			;Using P_Data
			;Using T_Data
			;Using T_TextEqu
			;Using T_WPRulData


		INPUT ParagHdl:l
		ERROR Err

		LOCAL ParagPtr:l,RulerHdl:l,RulerPtr:l
		LOCAL ParagCount:w,SrcPtr:l,DestPtr:l
		LOCAL LeftMarg:w,TabCount:w,Indent:w

		BEGIN

; Grow handle
		movelong [ParagHdl],ParagPtr
		moveword [ParagPtr],ParagCount

		lda ParagCount
		jsl T_MulParag
		pushword #0
		addword a,#2,s
		pushlong ParagHdl
		jsl D_GrowHandle
		sta Err
		jcs Exit


		movelong [ParagHdl],ParagPtr
		addwl #2,ParagPtr

		ldx ParagCount
		dex
		ldy #T_ParInfoSize-4
		jsl D_FastMult
		addlong a,ParagPtr,SrcPtr

		lda ParagCount
		dec a
		jsl T_MulParag
		addlong a,ParagPtr,DestPtr



ParLoop
		tool _BlockMove,in=(SrcPtr:l,DestPtr:l,#T_ParInfoSize-4:l)

		moveword [DestPtr]:#T_ParStart-2,a
		beq Normal

; Paragraph is a page break.  Get it a valid ruler

		call T_NewRuler,out=(RulerHdl:l),err=(Err)
		jcs Exit
		movelong RulerHdl,[DestPtr]:#T_RulerHdl-2
		moveword #0,[DestPtr]:#T_ParStart-2
		brl DoneTabs


Normal
		movelong [DestPtr]:#T_RulerHdl-2,RulerHdl
		movelong [RulerHdl],RulerPtr

		ldy #T_WPRulBits
		moveword [RulerPtr]:y,a
		and #T_MarkBit
		bne DoneTabs

		moveword [RulerPtr]:y,a
		ora #T_MarkBit
		and #T_KillWidow
		moveword a,[RulerPtr]:y

		moveword [RulerPtr]:#T_WPLeftMarg,LeftMarg
		moveword [RulerPtr]:#T_WPTabCount,TabCount
		moveword [RulerPtr]:#T_WPIndent,Indent
		moveword TabCount,[RulerPtr]:#T_TabCount
		subword  Indent,LeftMarg,a
		bpl NotNeg
		lda #0
NotNeg	moveword a,[RulerPtr]:#T_Indent
		moveword #P_IndentTab,[RulerPtr]:#T_Indent+2

		ldy #T_TabArray

TabLoop	ldx TabCount
		beq DoneTabs

		subword [RulerPtr]:y,LeftMarg,[RulerPtr]:y
		iny
		iny
		iny
		iny
		dec TabCount
		bra TabLoop
DoneTabs


		sublong DestPtr,#T_ParInfoSize,DestPtr
		sublong SrcPtr,#T_ParInfoSize-4,SrcPtr

		dec ParagCount
		lda ParagCount
		jne ParLoop


		call T_ClearMarks,in=(ParagHdl:l,#T_ParInfoSize:w)

Exit	RETURN
		ENDP




;------------------------------------------------------------------------------
;
; T_PL2WPText (ParagHdl:l)
;
; Convert my scrap to D_WP scrap.  Shrink each Parag record by 4 bytes
; and convert the rulers.


T_PL2WPText	PROC		EXPORT
			;Using T_Data
			;Using T_TextEqu
			;Using T_WPRulData

		INPUT ParagHdl:l

		LOCAL ParagPtr:l,RulerHdl:l,RulerPtr:l
		LOCAL SrcPtr:l,DestPtr:l
		LOCAL ParagCount:w,Count:w,TabCount:w,Indent:w

		BEGIN


		movelong [ParagHdl],ParagPtr
		moveword [ParagPtr],ParagCount

		stz Count
		addwl #2,ParagPtr
		movelong ParagPtr,SrcPtr
		movelong ParagPtr,DestPtr


ParLoop
		tool _BlockMove,in=(SrcPtr:l,DestPtr:l,#T_ParInfoSize-4:l)

		lda #0
		moveword a,[DestPtr]:#T_ParStart-2
		moveword a,[DestPtr]:#T_ParHeight-2

		movelong [DestPtr]:#T_RulerHdl-2,RulerHdl
		movelong [RulerHdl],RulerPtr


		ldy #T_RulBits
		moveword [RulerPtr]:y,a
		and #T_MarkBit
		bne DoneTabs
		moveword [RulerPtr]:y,a
		ora #T_MarkBit
		moveword a,[RulerPtr]:y

		moveword [RulerPtr]:#T_Indent,Indent
		moveword [RulerPtr]:#T_TabCount,TabCount
		moveword TabCount,[RulerPtr]:#T_WPTabCount
		addword Indent,#T_WPDefLM,[RulerPtr]:#T_WPIndent
		moveword #T_WPDefLM,[RulerPtr]:#T_WPLeftMarg
		moveword #T_WPDefRM,[RulerPtr]:#T_WPRightMarg


;Convert tabs

		ldy #T_TabArray

TabLoop	ldx TabCount
		beq DoneTabs

		addword [RulerPtr]:y,#T_WPDefLM,[RulerPtr]:y
		iny
		iny
		iny
		iny
		dec TabCount
		bra TabLoop
DoneTabs


		addlong SrcPtr,#T_ParInfoSize,SrcPtr
		addlong DestPtr,#T_ParInfoSize-4,DestPtr

		inc Count
		cmpw Count,ParagCount
		jlt ParLoop



; Shrink the handle

		ldx ParagCount
		ldy #T_ParInfoSize-4
		jsl D_FastMult
		pushword #0
		addword a,#2,s
		pushlong ParagHdl
		jsl D_GrowHandle

		call T_ClearMarks,in=(ParagHdl:l,#T_ParInfoSize-4:w)

		RETURN
		ENDP




;-----------------------------------------------------------------------------
;
; T_ClearMarks (ParagHdl:l,ParRecSize:w)
;

T_ClearMarks	PROC		EXPORT
			;Using T_Data
			;Using T_TextEqu

		INPUT ParagHdl:l,ParRecSize:w
		LOCAL ParagPtr:l,RulerHdl:l,RulerPtr:l,ParagCount:w

		BEGIN

		movelong [ParagHdl],ParagPtr
		moveword [ParagPtr],ParagCount

ParLoop	movelong [ParagPtr]:#T_RulerHdl,RulerHdl
		movelong [RulerHdl],RulerPtr
		ldy #T_RulBits
		moveword [RulerPtr]:y,a
		and #T_ClearMark
		moveword a,[RulerPtr]:y
		dec ParagCount
		addwl ParRecSize,ParagPtr
		lda ParagCount
		bne ParLoop

		RETURN
		ENDP




;------------------------------------------------------------------------------
;
; T_WPRulData

T_WPRulData	PROC		EXPORT



		ENDP
		END

