	load 'macros.dump'
	include 'driver.equ'
	include 'wp.equ'
 	include	'WP.Macros'

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

	IMPORT	W_AvailEvent
	IMPORT	W_BotLine
	IMPORT	W_BotPar
	IMPORT	W_CalcDocRect
	IMPORT	W_CalcLineHeight
	IMPORT	W_Caret
	IMPORT	W_CutLine
	IMPORT	W_DateStr
	IMPORT	W_DrawCaret
	IMPORT	D_DrawString
	IMPORT	D_DrawText
	IMPORT	W_EndOffset
	IMPORT	W_EndPtr
	IMPORT	W_FOnPage
	IMPORT	W_FindFont
	
	IMPORT	W_FtrHt
	IMPORT	W_GetAddr
	IMPORT	W_GetLRecPtr
	IMPORT	W_GetLeft
	IMPORT	W_GetLineRec
	IMPORT	W_GetParRec
	IMPORT	W_GetRgn
	IMPORT	W_GetSpaceToFooter
	IMPORT	D_GetStats
	IMPORT	W_GetTopSpace
	IMPORT	W_HOnPage
	IMPORT	W_HdrHt
	
	
	IMPORT	W_InvSelectUC
	IMPORT	W_JustBits
	IMPORT	W_JustExtra
	IMPORT	W_JustPtr
	IMPORT	W_lastline
	IMPORT	W_LastP
	
	IMPORT	W_LineToPixel
	IMPORT	W_MakeLines
	IMPORT	W_MakePageStr
	IMPORT	W_MaxAscent
	IMPORT	W_MaxDescent
	IMPORT	W_PageStr
	IMPORT	W_PaperSize
	IMPORT	W_PaperTop
	IMPORT	W_ParLock
	IMPORT	W_ParRec
	IMPORT	W_ParUnLock
	
	IMPORT	W_PixelToLine
	IMPORT	W_PrintFl
	IMPORT	W_ReadRuler
	IMPORT	W_RulBits
	IMPORT	W_STFont
	IMPORT	W_STPtr
	IMPORT	W_STColor
	IMPORT	W_STStyle
	IMPORT	D_SelectFont
	IMPORT	W_SetBounds
	IMPORT	W_ShowRuler
	IMPORT	W_STLIne
	IMPORT	W_Stuff
	IMPORT	W_SwitchData
	import	W_TimeStr
	IMPORT	W_TopPg
	IMPORT	W_TopPgPixel
	IMPORT	W_UseTab
	IMPORT	W_ValidLines
	IMPORT	W_Descent
	IMPORT	W_View

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

	ENTRY	W_DrawLine
	ENTRY	W_NoDraw
	ENTRY	W_UpdateTextP
	ENTRY	W_DrawHeader

****************************************************************
W_UpdateText	PROC		EXPORT
			;Using wpglobals
			;Using SCreenData

		begin

		pha
		pha
		pha
		lda #-1
		pha
		pha
		pushword #1
		jsl W_UpdateTextP
		pla
		pla
		pla

		return

		ENDP


****************************************************************
* W_UpdateText
W_UpdateTextP	PROC		EXPORT
			;Using wpglobals
			;Using SCreenData
			;Using W_ToolData
		EXPORT	W_NoDraw

		input EPar:w,ELine:w,Force:w
		output BottomPixel:w,Par:w,line:w
		local rect:r,rect2:r,BaseLine:w,RgnHand:l,HPixel:w
		local W_ParRec:l,LineHand:l,LinePtr:l,W_lastline:w,MyClip:r
		local PBLine:l,TempPtr:l,Quit:w,HPage:w,ChrPtr:l
		begin

		stz BottomPixel
		stz Quit

		lda W_Caret
		beq StayOff
		jsl W_DrawCaret
StayOff

		jsl W_SetBounds

		jsl W_GetRgn
		movelong ax,RgnHand

		spacelong
		_GetVisHandle
		spacelong
		_GetClipHandle
		pushlong RgnHand
		_SectRgn

		addlong [RgnHand],#2,LinePtr

		pushlong !rect2
		jsl W_CalcDocRect

		pha
		pushlong !rect2
		pushlong LinePtr
		pushlong !rect
		_SectRect
		pla
		jeq UTExit

		lda W_TopPg
		jsl W_FOnPage
		bcs NoFooter

		lda W_TopPg
		jsl W_GetSpaceToFooter
		subword a,W_TopPgPixel,a
		ldy W_ShowRuler
		beq NoAdd1
		addword a,#W_RulerHt,a
NoAdd1
		sta HPixel
		cmp rect2+4
		bcs NoFooter
		addword a,W_FtrHt,a
		cmp rect2
		bcc NoFooter
		pushword HPixel
		pushword W_TopPg
		pushword #W_FooterOn
		jsl W_DrawHeader

		addword HPixel,W_FtrHt,HPixel
		bmi NoFooter
		PutifMax HPixel,BottomPixel

NoFooter
		lda W_View
		bne IsOnTop
		lda W_TopPg
		jsl W_HOnPage
		bcs NotOnTop
IsOnTop

		lda W_TopPg
		sta HPage
		jsl W_GetTopSpace
		cmp W_TopPgPixel
		bcc NotOnTop

		subword W_PaperTop,W_TopPgPixel,a
		ldy W_ShowRuler
		beq NoAdd2b
		addword a,#W_RulerHt,a
NoAdd2b
		bra DoHeader

NotOnTop
		subword rect2+4,rect2,a
		addword a,W_TopPgPixel,a
		cmp W_PaperSize
		jcc W_NoDraw

		lda W_TopPg
		inc a
		sta HPage
		ldy W_View
		bne IsHead
		jsl W_HOnPage
		bcs NoHeader
IsHead

		subword W_PaperSize,W_TopPgPixel,a
		ldy W_ShowRuler
		beq NoAdd2
		addword a,#W_RulerHt,a
NoAdd2
		addword a,W_PaperTop,a

DoHeader
		ldy W_View
		bne NODH
		sta HPixel
		pha
		pushword HPage
		pushword #W_HeaderOn
		jsl W_DrawHeader

NODH
		addword HPixel,W_HdrHt,HPixel
		bmi NoHeader
		PutIfMax Hpixel,BottomPixel

NoHeader
		subword rect2+4,rect2,a
		addword a,W_TopPgPixel,a
		cmp W_PaperSize
		bcc W_NoDraw

		subword W_PaperSize,W_TopPgPixel,a
		ldy W_ShowRuler
		beq NoAdd
		addword a,#W_RulerHt,PBLine
NoAdd
		sta PbLine
		inc PBLine

		_PenNormal

		stz PBLine+2
		pushlong PBLine
		_MoveTo

		moveword #640,PBLine+2

		pushlong PBLine
		_LineTo

		inc PBLine

		PutIfMax PBLine,BottomPixel

W_NoDraw		;		
		pha
		pha
		pha
		pushword rect
		jsl W_PixelToLine
		ply
		pullword line
		pullword par
		jeq UTExit
		cpy #1
		jeq UTExit

		bra StartDraw

GotNewPar

		stz line

StartDraw
		lda par
		jsl W_GetParRec
		movelong ax,W_ParRec

		cmpw [W_ParRec]:#W_pAttr,#W_PgBrk
		jeq NextPar

		pushword par
		jsl W_ValidLines

		pushlong [W_ParRec]:#W_pBlockHand
		_HLock

		movelong [W_ParRec]:#W_pLineHand,LineHand
		pushlong LineHand
		_HLock
		movelong [LineHand],LinePtr

		lda Par
		ldx #0
		jsl W_GetAddr
		movelong ax,ChrPtr

		movelong [ChrPtr],[LinePtr]:#W_lFont+W_LineHeader
		moveword [ChrPtr]:#4,[LinePtr]:#W_lColor+W_LineHeader

		ldy #W_pRulerHand+2
		lda [W_ParRec],y
		tax
		ldy #W_pRulerHand
		lda [W_ParRec],y
		jsl W_ReadRuler

		moveword [W_ParRec]:#W_pLastLine,W_lastline

UpdateLoop
		cmpw par,W_BotPar
		bcc NProb
		bne DoEx
		cmpw W_BotLine,line
		bcs NProb
DoEx	moveword #1,Quit
		brl PastBot

NProb
		pha
		pushword par
		pushword line
		jsl W_LineToPixel
		pullword BaseLine

		pushword par
		pushword line
		movelong LinePtr,ax
		ldy line
		jsl W_GetLineRec
		movelong ax,TempPtr
		phx
		pha
		subword BaseLine,[TempPtr]:#W_lAscent,a
		scmpw a,rect+4
		bcc NoAbort
		pla
		pla
		pla
		pla
		bra PastBot

NoAbort
		lda BaseLine
		pha
		addword a,[TempPtr]:#W_lDescent,Hpixel
		PutIfMax HPixel,BottomPixel
		jsl W_DrawLine

		lda Force
		bne KeepGoin

		pha
		pushword #%101000		;W_Key down and auto
		pushlong #W_AvailEvent
		_EventAvail
		pla
		beq KeepGoin

		moveword #1,Quit

		cmpw par,EPar
		bcc KeepGoin
		bne PastBot
		cmpw line,ELine
		bcs PastBot

KeepGoin
		stz Quit
		scmpw BaseLine,rect+4
		bcs PastBot

		inc line
		lda line
		cmp W_lastline
		jcc UpDateLoop

PastBot
		pushlong [W_ParRec]:#W_pBlockHand
		_HUnLock

		pushlong LineHand
		_HUnLock

		lda Quit
		bne UTExit

		scmpw BaseLine,rect+4
		bcs UTExit

NextPar
		inc par
		lda W_LastP
		cmp Par
		jcs GotNewPar

UTExit
		lda Quit
		bne NotDone

		stz par
		stz line

NotDone
		jsl W_InvSelectUC

		return

		ENDP




****************************************************************
* W_DrawLine(Par:w,line:w,LineRecPtr:l,Baseline:w)
* will draw line in par at BaseLine
* assumes ruler is current, linehand and textblock are X_Locked
W_DrawLine	PROC		EXPORT
			;Using WPGlobals
			;Using D_GlobalData

		input Par:w,line:w,LineRecPtr:l,Baseline:w
		local YStop:w,Ptr2:l,Point:l,ChrPtr:l,CurH:w,Changed:w
		local SetExtra:w
		begin

		stz SetExtra

		moveword [LineRecPtr]:#W_lFont,W_STFont
		moveword [LineRecPtr]:#W_lStyle,W_STStyle
		moveword [LineRecPtr]:#W_lColor,W_STColor
		moveword [LineRecPtr]:#W_lOffset,a
		tax
		lda par
		jsl W_GetAddr
		movelong ax,W_STPtr
		movelong ax,ChrPtr
		moveword line,W_STLIne
		moveword [LineRecPtr]:#W_lOffset+W_lBytes,a
		tax
		lda par
		jsl W_GetAddr
		movelong ax,W_JustPtr
		movelong ax,W_EndPtr

;			  lda W_RulBits
;			  and #W_r_left
;			  bne NoCut

		jsl W_CutLine

NoCut

		lda line
		jsl W_GetLeft
		sta CurH

		lda W_RulBits
		and #W_r_left+W_r_full
		bne GotCurH

		lda W_RulBits
		and #W_r_center
		bne ItsCenter

		lda W_JustBits
		bmi GotCurH
		addword a,CurH,CurH
		bra GotCurH

ItsCenter
		lda W_JustBits
		bmi GotCurH
		lsr a
		clc
		adc CurH
		sta CurH

GotCurH
		addword CurH,W_PrintFl,s
		pushword BaseLine
		_MoveTo

NewChrPtr
		moveword #1,Changed
		subword W_EndPtr,ChrPtr,YStop
		jeq DLExit
		ldy #0
PLoop
		lda [ChrPtr],y
		and #$ff
		cmp #sp
		jcc DumpIt

		jsr CheckChange

NoChand
		iny
		cpy YStop
		bcs DumpIt
		brl PLoop

DumpIt
		tya
		clc
		adc ChrPtr
		sta Ptr2
		lda ChrPtr+2
		adc #0
		sta Ptr2+2

		cmpl ChrPtr,W_JustPtr
		bcc NoExtra

		lda SetExtra
		bne NoExtra
		inc SetExtra

		phy
		pushlong W_JustExtra
		_SetSpaceExtra
		ply

NoExtra
		cpy #1
		beq DrawOne

		pushlong ChrPtr
		phy
		jsl D_DrawText
		bra DoneDraw

DrawOne	lda [ChrPtr]
		and #$ff
		pha
		_DrawChar

DoneDraw
		cmpl Ptr2,W_EndPtr
		bcs DLExit

		lda [Ptr2]			   ;change or cr
		and #$ff
		asl a
		tax
		jmp (ChrTable,x)

DLExit
		lda W_RulBits
		and #W_r_full
		beq NoFix

		pushlong #0
		_SetSpaceExtra

NoFix
		return

;--------------------------------------------

AdjustTab
		pushlong !Point
		_GetPen
		subword Point+2,W_PrintFl,Point+2

		pha
		pushword W_STFont
		pushword W_STStyle
		pushword W_STColor
		pushword Point+2
		pushlong Ptr2
		jsl W_UseTab
		pla
		sta Point+2

		addword Point+2,W_PrintFl,s
		pushword Point
		_MoveTo

		addlong Ptr2,#1,ChrPtr

		brl NewChrPtr


PageWidth
		pushlong #W_PageStr
		bra DoStr

DateWidth
		pushlong #W_DateStr
		bra DoStr

TimeWidth
		pushlong #W_TimeStr
DoStr
		jsr CheckChange
		jsl D_DrawString

		addlong Ptr2,#1,ChrPtr

		brl NewChrPtr



FChange
		ldy #1
		lda [Ptr2],y
		sta W_STFont
		addlong #3,Ptr2,ChrPtr
		brl NewChrPtr


SizeChange
		ldy #1
		lda [Ptr2],y
		shortm
		sta W_STStyle+1
		longm
		addlong #2,Ptr2,ChrPtr
		brl NewChrPtr



StyleChange
		ldy #1
		lda [Ptr2],y
		shortm
		sta W_STStyle
		longm
		addlong #2,Ptr2,ChrPtr
		brl NewChrPtr



ColorChange
		ldy #1
		lda [Ptr2],y
		stz W_STColor
		shortm
		sta W_STColor
		longm
		addlong #2,Ptr2,ChrPtr
		brl NewChrPtr


;--------------------------
CheckChange
		ldx Changed
		jeq CCexit
		stz Changed
		phy

		pushlong !Point
		_GetPen
		subword Point+2,W_PrintFl,Point+2

		lda W_STStyle
		and #SuperScript+SubScript
		beq NoScript
		eor #SuperScript+SubScript
		beq NoScript

		lda W_STStyle
		and #$ffff-SuperScript-SubScript
		tax
		lda W_STFont
		ldy W_STColor
		jsl D_SelectFont

		lda W_STStyle
		and #SuperScript
		beq DoSub

		jsl D_GetStats
		addword a,BaseLine,a		   ;BaseLine-(W_MaxAscent-ascent)
		subword a,W_MaxAscent,Point

		addword Point+2,W_PrintFl,s
		pushword Point
		_MoveTo
		bra DoneScript

DoSub
		jsl D_GetStats
		phx					  ;BaseLine+W_MaxDescent-W_Descent
		addword BaseLine,W_MaxDescent,a
		subword a,1:s,Point
		plx

		addword Point+2,W_PrintFl,s
		pushword Point
		_MoveTo
		bra DoneScript


NoScript
		addword Point+2,W_PrintFl,s
		pushword BaseLine
		_MoveTo

DoneScript
		lda W_STFont
		ldx W_STStyle
		ldy W_STColor
		jsl D_SelectFont
		ply

CCExit	rts



ChrTable	DC.W	0,FChange	;0,1
	DC.W	StyleChange,SizeChange	;2,3
	DC.W	ColorChange,PageWidth	;4,5
	DC.W	DateWidth,TimeWidth	;6,7
	DC.W	0,AdjustTab	;8,9

		ENDP





****************************************************************
* W_DrawPartLine(Par:w,line:w,Pt:l,W_StartOffset:w,W_EndOffset:w)
* will draw partial line in par at Pt
W_DrawPartLine	PROC		EXPORT
			;Using WPGlobals
			;Using D_GlobalData

		input Par:w,line:w,Pt:l,SOffset:w,W_EndOffset:w
		local LineRecPtr:l,Baseline:w,Point:l
		local YStop:w,Ptr2:l,TempPoint:l,ChrPtr:l,CurH:w,Changed:w
		begin

		pushword Par
		jsl W_ParLock

		spacelong
		pushword Par
		pushword line
		jsl W_GetLRecPtr
		pullLong LineRecPtr

		lda par
		ldx SOffset
		jsl W_GetAddr
		movelong ax,ChrPtr

		lda par
		ldx W_EndOffset
		jsl W_GetAddr
		movelong ax,W_EndPtr

		moveword Pt+2,CurH
		subword Pt,[LineRecPtr]:#W_lDescent,BaseLine

		pha
		pha
		pha
		pushword Par
		pushword line
		pushword Soffset
		jsl W_FindFont
		pullword W_STColor
		pullword W_STFont
		pullword W_STStyle

		pushword CurH
		pushword BaseLine
		_MoveTo

NewChrPtr
		moveword #1,Changed
		subword W_EndPtr,ChrPtr,YStop
		jeq DLExit
		ldy #0
PLoop
		lda [ChrPtr],y
		and #$ff
		cmp #sp
		jcc DumpIt
		ldx Changed
		jeq NoChand
		stz Changed
		phy

		pushlong !Point
		_GetPen

		lda W_STStyle
		and #SuperScript+SubScript
		beq NoScript
		eor #SuperScript+SubScript
		beq NoScript

		lda W_STStyle
		and #$ffff-SuperScript-SubScript
		tax
		lda W_STFont
		ldy W_STColor
		jsl D_SelectFont

		lda W_STStyle
		and #SuperScript
		beq DoSub

		jsl D_GetStats
		addword a,BaseLine,a		   ;BaseLine-(W_MaxAscent-ascent)
		subword a,W_MaxAscent,Point

		pushlong Point
		_MoveTo
		bra DoneScript

DoSub
		jsl D_GetStats
		phx					  ;BaseLine+W_MaxDescent-W_Descent
		addword BaseLine,W_MaxDescent,a
		subword a,1:s,Point
		plx

		pushlong Point
		_MoveTo
		bra DoneScript


NoScript
		pushword Point+2
		pushword BaseLine
		_MoveTo

DoneScript
		lda W_STFont
		ldx W_STStyle
		ldy W_STColor
		jsl D_SelectFont
		ply
NoChand
		iny
		cpy YStop
		bcs DumpIt
		brl PLoop

DumpIt
		tya
		clc
		adc ChrPtr
		sta Ptr2
		lda ChrPtr+2
		adc #0
		sta Ptr2+2

		cpy #1
		beq DrawOne

		pushlong ChrPtr
		phy
		jsl D_DrawText
		bra DoneDraw

DrawOne	lda [ChrPtr]
		and #$ff
		pha
		_DrawChar

DoneDraw

		cmpl Ptr2,W_EndPtr
		bcs DLExit

		lda [Ptr2]			   ;change or cr
		and #$ff
		asl a
		tax
		jmp (ChrTable,x)

DLExit

NoFix

		pushword Par
		jsl W_ParUnLock

		return

;--------------------------------------------

AdjustTab
		pushlong !Temppoint
		_GetPen

		pha
		pushword W_STFont
		pushword W_STStyle
		pushword W_STColor
		pushword TempPoint+2
		pushlong Ptr2
		jsl W_UseTab
		pla
		sta Temppoint+2

		pushlong TempPoint
		_MoveTo

		addlong Ptr2,#1,ChrPtr

		brl NewChrPtr


PageWidth
		pushlong #W_PageStr
		bra DoStr

DateWidth
		pushlong #W_DateStr
		bra DoStr

TimeWidth
		pushlong #W_TimeStr
DoStr
		jsl D_DrawString

		addlong Ptr2,#1,ChrPtr

		brl NewChrPtr




FChange
		ldy #1
		lda [Ptr2],y
		sta W_STFont
		addlong #3,Ptr2,ChrPtr
		brl NewChrPtr


SizeChange
		ldy #1
		lda [Ptr2],y
		shortm
		sta W_STStyle+1
		longm
		addlong #2,Ptr2,ChrPtr
		brl NewChrPtr



StyleChange
		ldy #1
		lda [Ptr2],y
		shortm
		sta W_STStyle
		longm
		addlong #2,Ptr2,ChrPtr
		brl NewChrPtr



ColorChange
		ldy #1
		lda [Ptr2],y
		stz W_STColor
		shortm
		sta W_STColor
		longm
		addlong #2,Ptr2,ChrPtr
		brl NewChrPtr





ChrTable	DC.W	0,FChange	;0,1
	DC.W	StyleChange,SizeChange	;2,3
	DC.W	ColorChange,PageWidth	;4,5
	DC.W	DateWidth,TimeWidth	;6,7
	DC.W	0,AdjustTab	;8,9

		ENDP


****************************************************************
* W_DrawHeader(type,pixel)
W_DrawHeader	PROC		EXPORT
			;Using wpglobals

		input Startpixel:w,Page:w,type:w
		local par:w,line:w,W_ParRec:l,LineHand:l,LinePtr:l
		local BaseLine:w,Pixels:w,W_lastline:w,LineRecPtr:l
		local W_Descent:w,Done:w
		begin
		pushword W_Stuff

		pushword Page
		jsl W_MakePageStr

		pushword type
		jsl W_SwitchData

		moveword #1,par
		stz Pixels
		stz Done

GotNewPar

		stz line

StartDraw
		lda par
		jsl W_GetParRec
		movelong ax,W_ParRec

		cmpw [W_ParRec]:#W_pAttr,#W_PgBrk
		jeq NextPar

		pushword par
		jsl W_MakeLines

		pushlong [W_ParRec]:#W_pBlockHand
		_HLock

		movelong [W_ParRec]:#W_pLineHand,LineHand
		pushlong LineHand
		_HLock
		movelong [LineHand],LinePtr

		ldy #W_pRulerHand+2
		lda [W_ParRec],y
		tax
		ldy #W_pRulerHand
		lda [W_ParRec],y
		jsl W_ReadRuler

		moveword [W_ParRec]:#W_pLastLine,W_lastline

UpdateLoop
		ldy line
		movelong LinePtr,ax
		jsl W_GetLineRec
		movelong ax,LineRecPtr
		jsl W_CalcLineHeight
		clc
		adc Pixels
		sta Pixels
		stx W_Descent
		cmp #W_HdrPixelLimit
		bcc NoStop

		inc Done
		bra PastBot

NoStop
		pushword par
		pushword line
		pushlong LineRecPtr
		addword Pixels,StartPixel,a
		subword a,W_Descent,s
		jsl W_DrawLine

		inc line
		lda line
		cmp W_lastline
		bcc UpDateLoop

PastBot
		pushlong [W_ParRec]:#W_pBlockHand
		_HUnLock

		pushlong LineHand
		_HUnLock

NextPar
		lda Done
		bne Exit

		inc par
		lda W_LastP
		cmp Par
		jcs GotNewPar

Exit
		jsl W_SwitchData
		return

		ENDP
		END

