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

	IMPORT	D_BitMapPtr
	IMPORT	W_CalcDocRect
	IMPORT	W_CalcFBytes
	IMPORT	D_ClearBitMap
	IMPORT	W_CurDoc
	IMPORT	W_DoRoom
	IMPORT	W_FindFont
	IMPORT	W_GetAddr
	IMPORT	W_GetLRecPtr
	IMPORT	W_GetLineRec
	IMPORT	W_GetParRec
	IMPORT	W_JunkLeft
	IMPORT	W_JunkRight
	IMPORT	W_lastline
	IMPORT	W_LineToTopPixel
	IMPORT	W_MaxAscent
	IMPORT	W_MaxDescent
	IMPORT	W_NextLine
	IMPORT	W_ParRec
	IMPORT	W_Ptr
	IMPORT	W_ReadFont
	IMPORT	D_RectClear
	IMPORT	D_RectZap
	IMPORT	W_SetDocRect
	IMPORT	W_StartLine
	IMPORT	W_StartOffset
	IMPORT	W_StartPar
	IMPORT	W_UpdateTextP
	IMPORT	W_ValidLines
	IMPORT	W_WPColor
	IMPORT	W_WpFont
	IMPORT	W_WPFontFlag
	IMPORT	W_WriteFBytesNB
	IMPORT	W_WriteFBytesNE
	IMPORT	W_elColor
	IMPORT	W_elOffset	
	IMPORT	W_LastP	
	ENTRY	W_CheckCol
	ENTRY	W_MyClearRect

****************************************************************
* W_IsJunk(Offset1,Offset2):answer
W_IsJunk	PROC		EXPORT
			;Using W_EditData
			;Using WPGlobals

		input Offset1:w,Offset2:w
		local color:w,ChrPtr:l,LinePtr:l
		output answer:w
		begin


		moveword #1,answer

		lda W_StartPar
		ldx #0
		jsl W_GetAddr
		movelong ax,ChrPtr

		moveword W_elColor,color

		shortm

		ldy W_elOffset
		bra DoComp

Loop1	lda [ChrPtr],y
		cmpw a,#sp
		bcs NotSpec

		asl a
		tax
		jmp (ChrTable1,x)
ChrTable1
	DC.W	0,SkipTwo	;0,1
	DC.W	SkipOne,SkipOne	;2,3
	DC.W	ColorChange,NotSpec	;4,5
	DC.W	NotSpec,NotSpec	;6,7
	DC.W	0,IJExit	;8,9

ColorChange
		iny
		lda [ChrPtr],y
		and #$ff
		sta color
		bra NotSpec

SkipTwo	iny
SkipOne	iny
NotSpec	iny
DoComp	cpy Offset1
		bcc Loop1

		longm



		lda color
		jne IJExit



		shortm

		ldy Offset1
		bra DoComp2

Loop2	lda [ChrPtr],y
		cmp #sp
		bcs NotSpec2

		asl a
		tax
		jmp (ChrTable2,x)
ChrTable2
	DC.W	0,SkipTwo2					;0,1
	DC.W	SkipOne2,SkipOne2			;2,3
	DC.W	ColorChange2,NotSpec2		;4,5
	DC.W	NotSpec2,NotSpec2			;6,7
	DC.W	0,IJExit					;8,9

ColorChange2
		iny
		lda [ChrPtr],y
		bne IJExit
		bra NotSpec2

SkipTwo2	iny
SkipOne2	iny
NotSpec2	iny
DoComp2	cpy Offset2
		bcc Loop2

		longm

		stz answer

IJExit	longm
		return

		ENDP




****************************************************************
;This is W_Stuff that changes font when inserting
W_MakeFontChange	PROC		EXPORT
			;Using wpglobals

		local O1:w,O2:w,font1:l,col1:w,font2:l,col2:w
		local bytes1:w,bytes2:w,W_Ptr:l,cbytes1:w,cbytes2:w
		local LineRec:l,ChrPtr:l
		output ChangeBytes:w
		begin

		stz ChangeBytes

		pha
		pushword W_StartPar
		pushword W_StartLine
		pushword W_StartOffset
		jsl W_JunkLeft
		pullword O1

		pha
		pushword W_StartPar
		pushword W_StartLine
		pushword W_StartOffset
		jsl W_JunkRight
		pullword O2

		spacelong
		pha
		spacelong
		pha
		pushword W_StartPar
		pushword W_StartLine
		pushword O1
		jsl W_FindFont
		moveword 1:s,col1
		movelong 3:s,font1
		lda W_StartPar
		ldx O1
		jsl W_GetAddr
		phx
		pha
		lda W_StartPar
		ldx O2
		jsl W_GetAddr
		phx
		pha
		jsl W_ReadFont
		pullword col2
		pullLong font2

		pha
		pushlong font1
		pushword col1
		pushlong W_WpFont
		pushword W_WPColor
		jsl W_CalcFBytes
		pullword bytes1

		stz bytes2

		pha
		pushlong W_WpFont
		pushword W_WPColor
		pushlong font2
		pushword col2
		jsl W_CalcFBytes
		pullword bytes2
		ora bytes1
		jeq MCExit

		pushword W_StartPar
		pushword O1
		addword bytes1,bytes2,a		 ;bytes=bytes1+bytes2-(O2-O1)
		addword a,O1,a
		subword a,O2,s
		sta ChangeBytes
		jsl W_DoRoom

		spaceword
		pushword W_StartPar
		pushword O1
		pushlong font1
		pushword col1				;no check end of par
		pushlong W_WpFont
		pushword W_WPColor
		jsl W_WriteFBytesNE
		pullword cbytes1
		subword bytes1,cbytes1,bytes1

		lda cbytes1
		beq NoPutInLine

		pha
		jsl W_CheckCol
		pla

NoPutInLine
		spaceword
		lda W_StartPar
		pha
		addword O1,bytes1,W_StartOffset
		pha
		pushlong W_WpFont
		pushword W_WPColor
		pushlong font2
		pushword col2
		jsl W_WriteFBytesNB
		pullword cbytes2

		subword changebytes,cbytes1,a
		subword a,cbytes2,changebytes

MCExit
		stz W_WPFontFlag
		return

		ENDP


****************************************************************

W_OSUpdateText	PROC		EXPORT
			;Using wpglobals
			;Using D_GlobalData

		input par:w,line:w,Force:w
		local rect:r,Port:l,rect2:r,Pt:l,W_lastline:w,W_ParRec:l
		local temp:w,ClearPt:w
		output Opar:w,OLine:w

		begin

		pushlong !rect
		jsl W_CalcDocRect

		lda W_LastP
		jsl W_GetParRec
		movelong ax,W_ParRec

		cmpw par,W_LastP
		bcc lineOk
		bne NoneBelow

		cmpw line,[W_ParRec]:#W_pLastLine
		bcc LineOk

NoneBelow

		pha
		pha
		pushword W_LastP
		moveword [W_ParRec]:#W_pLastLine,a
		dec a
		pha
		jsl W_LineToTopPixel
		pla
		plx
		sta temp
		addword x,temp,temp
		jmi AllDone

		cmp rect+4
		jcs AllDone

		PutIfMax temp,rect

		pushlong !rect
		jsl W_MyClearRect

		brl AllDone

LineOk
		pha
		pha
		pushword Par
		pushword line
		jsl W_LineToTopPixel
		pla
		pla
		sta temp
		bmi setdr						; TJH (8/9/89) - if negative then just use 0 for top.

		cmp rect+4
		jcs NoClear

		putIfmax temp,rect

setdr	jsl W_SetDocRect

		movelong rect,rect2
		movelong rect+4,rect2+4

		pushlong !rect2
		_LocalToGlobal

		pushlong !rect2+4
		_LocalToGlobal

;start off screen drawing

		movelong W_CurDoc,port

		pushlong [Port]:#2

		movelong >D_BitMapPtr,[Port]:#2

		pushlong !rect
		_ClipRect

		pushlong !rect2
		jsl D_ClearBitMap

		pha					  ;space for pixel
		pha					  ;space for par
		pha					  ;space for line
		pushword par
		addword line,#1,s
		pushword Force
		jsl W_UpdateTextP
		pullword OLine
		pullword OPar
		pullword Pt
		stz Pt+2

		pushlong !Pt
		_LocalToGlobal

		moveword rect2+4,ClearPt

		pha
		pha
		pushword OPar
		pushword OLine
		jsl W_NextLine
		pla
		pla
		bcs NoPut

		PutIfMin Pt,rect2+4

NoPut
		pullLong [Port]:#2

		pushlong !rect2
		jsl D_RectZap

		lda OPar
		bne NoClear

		moveword rect2+4,rect2
		moveword ClearPt,rect2+4

		pushlong !rect2
		jsl D_RectClear

NoClear
		jsl W_SetDocRect

;done off screen drawing

AllDone
		return

		ENDP



****************************************************************
* W_MyClearRect - clears a rect(fast)
W_MyClearRect	PROC		EXPORT
		input RectPtr:l
		local rect:r,rect2:r,rect3:r
		begin

		pushlong !rect2
		jsl W_CalcDocRect

		movelong [RectPtr],rect
		movelong [RectPtr]:#4,rect+4

		pha
		pushlong !rect
		pushlong !rect2
		pushlong !rect3
		_SectRect
		pla
		beq NoClear

		pushlong !rect3
		_LocalToGlobal

		pushlong !rect3+4
		_LocalToGlobal

		pushlong !rect3
		jsl D_RectClear

NoClear
		return

		ENDP



****************************************************************
* W_PutNewHeight
W_PutNewHeight	PROC		EXPORT
			;Using wpglobals
		local W_ParRec:l,LineHand:l,LinePtr:l,CurPtr:l
		local line:w,W_lastline:w,ParHeight:w
		begin

		lda W_StartPar
		jsl W_GetParRec
		movelong ax,W_ParRec

		pushword W_StartPar
		jsl W_ValidLines

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

		movelong LinePtr,ax
		ldy W_StartLine
		jsl W_GetLineRec
		movelong ax,CurPtr

		moveword W_MaxAscent,[CurPtr]:#W_lAscent
		moveword W_MaxDescent,[CurPtr]:#W_lDescent
		clc
		adc W_MaxAscent
		moveword a,[CurPtr]:#W_lHeight

		addlong #W_LineHeader,LinePtr,CurPtr
		stz ParHeight
		stz line
		moveword [W_ParRec]:#W_pLastLine,W_lastline

LLoop
		addword ParHeight,[CurPtr]:#W_lHeight,ParHeight
		addlong #W_lBytes,CurPtr,CurPtr
		inc line
		cmpw line,W_lastline
		bcc LLoop

		moveword ParHeight,[W_ParRec]:#W_pPixels

		return

		ENDP



****************************************************************
* W_OptionConvert(a:W_Key)a:W_Key
W_OptionConvert	PROC		EXPORT
		cmp #sp
		bne OCExit
		lda #$ca
;			  bcc OCExit
;			  sec
;			  sbc #sp
;			  tax
;			  lda >OCTable,x
;			  and #$ff
OCExit	rtl


OCTable
;	  dc i1'$ca,$ff,$ae,$ff,$ff,$ff,$ff,$be'		  ;$20-$27
;	  dc i1'$ff,$ff,$a1,$b1,$b2,$d0,$b3,$d6'		  ;$28-$2f
;	  dc i1'$bc,$c1,$aa,$a3,$a2,$b0,$a4,$a6'		  ;$30-$37
;	  dc i1'$a5,$bb,$ff,$c9,$ff,$ad,$ff,$c0'		  ;$38-$3f
;	  dc i1'$ff,$81,$ff,$82,$ff,$ff,$ff,$ff'		  ;$40-$47
;	  dc i1'$ff,$ff,$ff,$ff,$ff,$ff,$ff,$af'		  ;$48-$4f
;	  dc i1'$b8,$ce,$ff,$ff,$ff,$ff,$d7,$ff'		  ;$50-$57
;	  dc i1'$ff,$ff,$ff,$d2,$c7,$d4,$ff,$d1'		  ;$58-$5f
;	  dc i1'$60,$8c,$ba,$8d,$b6,$ab,$c4,$a9'		  ;$60-$67
;	  dc i1'$ff,$5e,$c6,$ff,$c2,$b5,$7e,$bf'		  ;$68-$6f
;	  dc i1'$b9,$cf,$a8,$a7,$a0,$ac,$c3,$b7'		  ;$70-$77
;	  dc i1'$c5,$b4,$bd,$d3,$c8,$d5,$ff,$ff'		  ;$78-$7f


		ENDP


*****************************************************************
* W_CheckCol
W_CheckCol	PROC		EXPORT
			;Using wpglobals

		local ChrPtr:l,LineRec:l
		output changed:w
		begin

		stz changed

		lda W_StartPar
		ldx #0
		jsl W_GetAddr
		movelong ax,ChrPtr

		spacelong
		pushword W_StartPar
		pushword #0
		jsl W_GetLRecPtr
		pullLong LineRec

		cmpl [ChrPtr],[LineRec]:#W_lFont
		bne DoChan

		cmpw [ChrPtr]:#4,[LineRec]:#W_lColor
		beq DoneChan

DoChan
		inc changed
		movelong [ChrPtr],[LineRec]:#W_lFont
		moveword [ChrPtr]:#4,[LineRec]:#W_lColor

DoneChan
		return

		ENDP
		END

