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

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

	IMPORT	W_BotLine
	IMPORT	W_BotPB
	IMPORT	W_BotPar
	IMPORT	W_CalcDocRect
	IMPORT	W_CalcFonts
	IMPORT	W_CalcLineHeight
	IMPORT	W_CaretBot
	IMPORT	W_CaretTop
	IMPORT	W_CheckWidow
	IMPORT	W_CutLine
	IMPORT	W_DateStr
	IMPORT	D_Deref
	IMPORT	W_GetAddr
	IMPORT	W_GetLRecPtr
	IMPORT	W_GetLineRec
	IMPORT	W_GetPageSize
	IMPORT	W_GetParRec
	IMPORT	W_GetTopSpace
	IMPORT	D_GetWdth
	IMPORT	W_Indent
	IMPORT	W_JustBits
	IMPORT	W_JustExtra
	IMPORT	W_JustPtr
	IMPORT	W_lastline
	IMPORT	W_LastP
	IMPORT	W_LeftMargin
	IMPORT	W_MakeVisRuler
	IMPORT	W_PageStr
	IMPORT	W_PaperSize
	IMPORT	W_ParLock
	IMPORT	W_ParRec
	IMPORT	W_ParUnLock
	
	IMPORT	Q_Point
	IMPORT	W_Ptr
	IMPORT	W_PutOnScreen
	IMPORT	W_ReadRulerP
	IMPORT	W_RulBits
	IMPORT	W_STFont
	IMPORT	W_STPtr
	IMPORT	W_STColor
	IMPORT	W_STStyle
	IMPORT	D_SelectFont
	IMPORT	W_SetFullREct
	IMPORT	W_ShowRuler
	IMPORT	W_STLIne
	IMPORT	W_StartLine
	IMPORT	W_StartOffset
	IMPORT	W_StartPar
	IMPORT	W_StartRuler
	IMPORT	W_TimeStr
	IMPORT	W_TopLine
	IMPORT	W_TopPB
	IMPORT	W_TopPar
	IMPORT	W_TopParHeight
	IMPORT	W_TopPg
	IMPORT	W_TopPgLine
	IMPORT	W_TopPgPar
	IMPORT	W_TopPgPixel
	IMPORT	D_UnLock
	IMPORT	W_UseTab
	IMPORT	W_ValidLines
	IMPORT	W_Height	
	IMPORT	Q_Line
	IMPORT	W_offset
	IMPORT	Q_Rect
	IMPORT	W_Selected

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

	ENTRY	W_GetHPos
	ENTRY	JustCheck
	ENTRY	W_LineToPixel
	ENTRY	W_MakeCaretGuts
	ENTRY	W_PixelToLine

********************************************************************
*
*	IncludesÉ
* Wednesday, May 3, 1989 12:47:59 AM
********************************************************************



****************************************************************
* W_SetBounds
W_SetBounds	PROC		EXPORT
			;Using wpglobals

		local Q_Rect:r
		begin

		pushlong !Q_Rect
		jsl W_CalcDocRect

		pha
		pha
		pha
		pushword Q_Rect
		jsl W_PixelToLine
		pla
		sta W_TopPB
		cmp #1
		bne NoProb2
		lda #$ffff
		sta 1,s
		sta 3,s
NoProb2
		pullword W_TopLine
		pullword W_TopPar

		pha
		pha
		pha
		pushword Q_Rect+4
		jsl W_PixelToLine
		pla
		sta W_BotPB
		cmp #1
		bne NoProb
		lda #$ffff
		sta 1,s
		sta 3,s
NoProb
		pullword W_BotLine
		pullword W_BotPar

		return

		ENDP




****************************************************************
* W_MakeCaretNS - sets the W_Caret positions and menu for a D_New selection
* The D_New selection is the W_StartPar,W_StartLine,W_StartOffset
* Does not put W_Caret on screen
W_MakeCaretNS	PROC		EXPORT
		begin

		jsl W_MakeCaretGuts

		jsl W_SetBounds

		return

		ENDP


****************************************************************
* W_MakeCaretGuts
W_MakeCaretGuts	PROC		EXPORT
			;Using wpglobals
		local W_Ptr:l,tempv:w
		begin

		lda W_Selected
		bne NotOnScreen

		stz W_CaretTop
		stz W_CaretBot

		pha
		pushword W_StartPar
		pushword W_StartLine
		jsl W_LineToPixel
		pla
		bmi NotOnScreen
		sta TempV

		spacelong
		pushword W_StartPar
		pushword W_StartLine
		jsl W_GetLRecPtr
		PullLong W_Ptr

		addword [W_Ptr]:#W_lDescent,TempV,W_CaretBot
		subword TempV,[W_Ptr]:#W_lAscent,W_CaretTop

		pha
		pushword W_StartPar
		pushword W_StartLine
		pushword W_StartOffset
		jsl W_GetHPos
		pla
		sta W_CaretTop+2
		sta W_CaretBot+2

NotOnScreen

		return

		ENDP


****************************************************************
* W_MakeCaret - sets the W_Caret positions and menu for a D_New selection
* The D_New selection is the W_StartPar,W_StartLine,W_StartOffset
* Does put W_Caret on screen
W_MakeCaret	PROC		EXPORT
			;Using wpglobals

		local W_ParRec:l
		begin +b

		pushword W_StartPar
		pushword W_StartLine
		pushlong #0
		jsl W_PutOnScreen

		lda W_Selected
		jne IsSel

		jsl W_MakeCaretGuts

IsSel
		lda W_StartPar
		jsl W_GetParRec
		movelong ax,W_ParRec

		cmpw [W_ParRec]:#W_pAttr,#W_PgBrk
		bne NoPB

		addlong #W_pBytes,W_ParRec,W_ParRec

NoPB
		jsl W_SetFullREct
		movelong [W_ParRec]:#W_pRulerHand,W_StartRuler

		lda W_ShowRuler
		beq NoDrawVis

		pushlong [W_ParRec]:#W_pRulerHand
		jsl W_MakeVisRuler

NoDrawVis
		jsl W_CalcFonts

		jsl W_SetBounds

		return

		ENDP



****************************************************************
* W_LineToPixel(par,Q_Line)
* Possible bugs with W_TopParHeight,W_TopPgLine
* Not yet done: widowing and pictures
* Out of bounds checking
W_LineToPixel	PROC		EXPORT
			;Using WPGlobals
			;Using ScreenData

		input par:w,Q_Line:w
		local ParPtr:l,Page:w,LineHand:l,LinePtr:l,CurPar:w,CurLine:w
		local StartPixel:w,PagePixs:w,CrossPage:w,TopParPix:w,SPixel:w
		output pixel:w
		begin

		moveword #-1,pixel

		cmpw Par,W_TopPar
		bcc NoGood
		bne NextCheck
		cmpw Q_Line,W_TopLine
		bcc NoGood
NextCheck	cmpw W_BotPar,par
		bcc NoGood
		bne IsGood
		cmpw W_BotLine,Q_Line
		bcs IsGood
NoGood	brl ErrExit
IsGood

		moveword W_TopPg,Page

		moveword W_TopPgPar,CurPar
		jsl W_GetParRec
		movelong ax,ParPtr
		moveword [ParPtr]:#W_pPixels,TopParPix
		sec
		sbc W_TopParHeight
		moveword a,[ParPtr]:#W_pPixels

		stz CrossPage

		lda Page
		jsl W_GetTopSpace
		sta pixel
		sta SPixel
		lda Page
		jsl W_GetPageSize
		addword a,Pixel,PagePixs

		lda CurPar
		jsl W_GetParRec
		movelong ax,ParPtr

; this loop is for normal pars, drops into subloop for pars that cross Q_Line
AddLoop
		cmpw Par,CurPar
		jeq W_GotPar

		moveword [ParPtr]:#W_pAttr,a
		beq OkPar
		and #W_PgBrk
		beq OkPar
		inc Page

		lda CrossPage
		beq TryMo

		moveword #-1,pixel
		brl LPExit

TryMo
		inc CrossPage
		lda Page
		jsl W_GetTopSpace
		sta pixel
		lda Page
		jsl W_GetPageSize
		addword a,Pixel,PagePixs

		addword Pixel,W_PaperSize,Pixel
		sta SPixel
		brl NextPar

OkPar
		lda pixel
		clc
		ldy #W_pPixels
		adc [ParPtr],y
		cmp PagePixs
		jcc GotNewPixel

		ldy CrossPage
		jne GotNewPixel

		lda curpar
		jsl W_CheckWidow
		bcc NoWidow

		cmpw pixel,SPixel
		beq NoWidow

		inc CrossPage
		inc Page
		lda Page
		jsl W_GetTopSpace
		sta pixel
		lda Page
		jsl W_GetPageSize
		addword a,Pixel,PagePixs

		addword Pixel,W_PaperSize,Pixel
		sta SPixel
		brl addloop

NoWidow

		inc CrossPage

		pushword CurPar
		jsl W_ValidLines

		movelong ParPtr,ax
		jsl W_ReadRulerP

		movelong [ParPtr]:#W_pLineHand,LineHand
		movelong LineHand,ax
		jsl D_Deref
		ldy CurPar
		cpy W_TopPgPar
		bne NoTop
		ldy W_TopPgLine
		bra IsTop
NoTop	ldy #0
IsTop	jsl W_GetLineRec
		movelong ax,LinePtr
		moveword Pixel,StartPixel

LineLoop
		movelong LinePtr,ax
		jsl W_CalcLineHeight
		clc
		adc Pixel
		cmp PagePixs
		bcs GotLine
		sta Pixel
		addlong #W_lBytes,LinePtr,LinePtr
		bra LineLoop

GotLine
		addword [ParPtr]:#W_pPixels,StartPixel,a
		subword a,Pixel,StartPixel	; SP = [par]:#pix-(pixel-sp)
		inc Page

		lda Page
		jsl W_GetTopSpace
		sta pixel
		lda Page
		jsl W_GetPageSize
		addword a,Pixel,PagePixs
		lda pixel
		clc
		adc W_PaperSize
		adc StartPixel
		sta pixel
		sta SPixel

		movelong LineHand,ax
		jsl D_UnLock
		bra NextPar


GotNewPixel	sta Pixel

NextPar	inc CurPar
		addlong #W_pBytes,ParPtr,ParPtr
		brl AddLoop

; got the par check that is not widowed
W_GotPar

		ldy CrossPage
		jne NoWidow2

		lda pixel
		clc
		ldy #W_pPixels
		adc [ParPtr],y
		cmp PagePixs
		jcc NoWidow2

		cmpw pixel,spixel
		beq NoWidow2

		lda curpar
		jsl W_CheckWidow
		bcc NoWidow2

		cmpw pixel,spixel
		beq NoWidow2

		inc CrossPage
		inc Page
		lda Page
		jsl W_GetTopSpace
		sta pixel
		lda Page
		jsl W_GetPageSize
		addword a,Pixel,PagePixs

		addword Pixel,W_PaperSize,Pixel
		sta SPixel

NoWidow2
		pushword CurPar
		jsl W_ValidLines

		movelong ParPtr,ax
		jsl W_ReadRulerP

		stz CurLine
		cmpw CurPar,W_TopPgPar
		bne NoTTop2
		moveword W_TopPgLine,CurLine
NotTop2

		movelong [ParPtr]:#W_pLineHand,LineHand
		movelong LineHand,ax
		jsl D_Deref
		ldy CurLine
		jsl W_GetLineRec
		movelong ax,LinePtr

LLoop
		movelong LinePtr,ax
		jsl W_CalcLineHeight
		clc
		adc Pixel
		cmp PagePixs
		bcc NoProb

		ldy CrossPage
		bne NoProb
		inc CrossPage

		inc Page

		lda Page
		jsl W_GetTopSpace
		sta pixel
		lda Page
		jsl W_GetPageSize
		addword a,Pixel,PagePixs
		addword W_PaperSize,pixel,pixel
		sta SPixel
		bra LLoop

NoProb
		sta Pixel
		inc CurLine

SLloop	cmpw Q_Line,CurLine
		bcc DoneLastStuff
		addlong #W_lBytes,LinePtr,LinePtr
		bra LLoop


DoneLastStuff

		subword pixel,[LinePtr]:#W_lDescent,a
		sbc W_TopPgPixel
		sta pixel

		movelong LineHand,ax
		jsl D_UnLock

		lda W_ShowRuler
		beq LPExit

		addword pixel,#W_RulerHt,pixel

LPExit
		lda W_TopPgPar
		jsl W_GetParRec
		movelong ax,ParPtr
		moveword TopParPix,[ParPtr]:#W_pPixels

ErrExit
		return

		ENDP



****************************************************************
* W_LineToTopPixel(par,Q_Line):pixel
* returns pixel above Q_Line
* Possible bugs with W_TopParHeight,W_TopPgLine
* Not yet done: widowing and pictures
W_LineToTopPixel	PROC		EXPORT

		input par:w,Q_Line:w
		output pixel:w,W_Height:w
		begin

		pha
		pushword par
		pushword Q_Line
		jsl W_LineToPixel
		pullword pixel

		lda par
		jsl W_GetParRec
		jsl W_ReadRulerP

		spacelong
		pushword par
		pushword Q_Line
		jsl W_GetLRecPtr
		pla
		plx
		jsl W_CalcLineHeight
		sta W_Height
		txa
		clc
		adc pixel
		sec
		sbc W_Height
		sta pixel

		return

		ENDP


****************************************************************
* W_PixelToLine(V:w):par,Q_Line,dir
* dir is 1 for round down and -1 to round up, when between lines
* If par is 0 then pixel is below all Q_Text
W_PixelToLine	PROC		EXPORT
			;Using WPGlobals
			;Using ScreenData

		input EndPixel:w
		local ParPtr:l,Page:w,LineHand:l,LinePtr:l,LastPixel:w
		local W_lastline:w,StartPixel:w,PagePixs:w,CrossPage:w,pixel:w
		local TopParPix:w,CrossPage2:w,SPixel:w
		output par:w,Q_Line:w,dir:w
		begin

		addword EndPixel,W_TopPgPixel,EndPixel

		lda W_ShowRuler
		beq NoSub
		subword EndPixel,#W_RulerHt,EndPixel
NoSub

		moveword W_TopPg,Page

		moveword W_TopPgPar,par
		jsl W_GetParRec
		movelong ax,ParPtr
		moveword [ParPtr]:#W_pPixels,TopParPix
		sec
		sbc W_TopParHeight
		moveword a,[ParPtr]:#W_pPixels

		stz CrossPage
		stz CrossPage2

		lda Page
		jsl W_GetTopSpace
		sta pixel
		sta SPixel
		lda Page
		jsl W_GetPageSize
		addword a,Pixel,PagePixs

		lda par
		jsl W_GetParRec
		movelong ax,ParPtr

		moveword pixel,LastPixel

		moveword #-1,dir

		moveword W_TopPgLine,Q_Line

		cmpw Pixel,EndPixel
		jcs LPExit

; this loop is for normal pars, drops into subloop for pars that cross Q_Line
AddLoop

		moveword pixel,LastPixel
		moveword CrossPage2,CrossPage

		moveword [ParPtr]:#W_pAttr,a
		beq OkPar
		and #W_PgBrk
		beq OkPar
		inc Page

		lda CrossPage
		jne W_GotPar

		inc CrossPage
		lda Page
		jsl W_GetTopSpace
		sta pixel
		lda Page
		jsl W_GetPageSize
		addword a,Pixel,PagePixs
		addword Pixel,W_PaperSize,Pixel
		sta SPixel
		brl NextPar

OkPar
		lda pixel
		clc
		ldy #W_pPixels
		adc [ParPtr],y
		cmp PagePixs
		jcc GotNewPixel

		ldy CrossPage
		jne GotNewPixel

		lda par
		jsl W_CheckWidow
		bcc NoWidow

		cmpw pixel,SPixel
		beq NoWidow

		inc CrossPage2
		inc Page
		lda Page
		jsl W_GetTopSpace
		sta pixel
		lda Page
		jsl W_GetPageSize
		addword a,Pixel,PagePixs
		addword Pixel,W_PaperSize,Pixel
		sta SPixel
		brl AddLoop

NoWidow
		inc CrossPage2

		pushword par
		jsl W_ValidLines

		movelong ParPtr,ax
		jsl W_ReadRulerP

		movelong [ParPtr]:#W_pLineHand,LineHand
		movelong LineHand,ax
		jsl D_Deref
		ldy Par
		cpy W_TopPgPar
		bne NoTop
		ldy W_TopPgLine
		bra IsTop
NoTop	ldy #0
IsTop	jsl W_GetLineRec
		movelong ax,LinePtr
		moveword Pixel,StartPixel

LineLoop
		movelong LinePtr,ax
		jsl W_CalcLineHeight
		clc
		adc Pixel
		cmp PagePixs
		bcs GotLine
		sta Pixel
		addlong #W_lBytes,LinePtr,LinePtr
		bra LineLoop

GotLine
		addword [ParPtr]:#W_pPixels,StartPixel,a
		subword a,Pixel,StartPixel	; SP = [par]:#pix-(pixel-sp)
		inc Page

		lda Page
		jsl W_GetTopSpace
		sta pixel
		lda Page
		jsl W_GetPageSize
		addword a,Pixel,PagePixs
		lda pixel
		clc
		adc W_PaperSize
		adc StartPixel
		sta pixel
		sta SPixel

		movelong LineHand,ax
		jsl D_UnLock
		bra NextPar


GotNewPixel	sta Pixel

NextPar
		cmpw Pixel,EndPixel
		bcs W_GotPar
		inc Par
		cmpw W_LastP,par
		bcc PastAll
		stz Q_Line
		addlong #W_pBytes,ParPtr,ParPtr
		brl AddLoop

PastAll
		dec par
		moveword #1,dir
		moveword [ParPtr]:#W_pLastLine,a
		dec a
		sta Q_Line
		brl LPExit

W_GotPar
		stz Q_Line

		cmpw [ParPtr]:#W_pAttr,#W_PgBrk
		bne NoPB

		inc par
		moveword #2,dir
		brl LPExit

NoPB
		moveword LastPixel,pixel
		moveword #-1,dir

		pushword par
		jsl W_ValidLines

		movelong ParPtr,ax
		jsl W_ReadRulerP

		cmpw Par,W_TopPgPar
		bne NoTTop2
		moveword W_TopPgLine,Q_Line
NotTop2

		movelong [ParPtr]:#W_pLineHand,LineHand
		movelong LineHand,ax
		jsl D_Deref
		ldy Q_Line
		jsl W_GetLineRec
		movelong ax,LinePtr

LLoop
		movelong LinePtr,ax
		jsl W_CalcLineHeight
		clc
		adc Pixel
		cmp PagePixs
		bcc NoProb

		ldy CrossPage
		bne NoProb
		inc CrossPage

		inc Page

		lda Page
		jsl W_GetTopSpace
		sta pixel
		lda Page
		jsl W_GetPageSize
		addword a,Pixel,PagePixs
		addword W_PaperSize,pixel,pixel
		sta SPixel
		cmp EndPixel
		bcs LPExit
		brl LLoop

NoProb
		sta Pixel
		cmp EndPixel
		bcs DoneLastStuff
		inc Q_Line
		addlong #W_lBytes,LinePtr,LinePtr
		bra LLoop


DoneLastStuff
		stz dir

		movelong LineHand,ax
		jsl D_UnLock

LPExit

		lda W_TopPgPar
		jsl W_GetParRec
		movelong ax,ParPtr
		moveword TopParPix,[ParPtr]:#W_pPixels

		return

		ENDP



****************************************************************
* W_GetLeft(a:Q_Line number)a:left margin or W_Indent
W_GetLeft	PROC		EXPORT
			;Using WPGlobals

		tay
		lda W_LeftMargin
		tyx
		bne GLExit
		lda W_Indent

GLExit
		rtl

		ENDP



****************************************************************
* W_GetHPos(par,Q_Line,W_offset)
W_GetHPos	PROC		EXPORT
			;Using wpglobals
		EXPORT	JustCheck
		input par:w,Q_Line:w,W_offset:w
		local W_ParRec:l,LineRecPtr:l,LineHand:l,changed:w,SetJust:w
		local YStop2:w,YStop:w,Ptr2:l,Q_Point:l,ChrPtr:l,StopPtr:l
		local FracBits:w,StringPtr:l
		output CurH:w
		begin

		stz SetJust
		stz FracBits

		lda par
		jsl W_GetParRec
		jsl W_ReadRulerP

		pushword par
		jsl W_ParLock

		pushword par
		jsl W_ValidLines

		lda par
		jsl W_GetParRec
		movelong ax,W_ParRec

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

		movelong LineRecPtr,ax
		ldy Q_Line
		jsl W_GetLineRec
		movelong ax,LineRecPtr

		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 Q_Line,W_STLIne
		ldx W_offset
		lda par
		jsl W_GetAddr
		movelong ax,W_JustPtr
		movelong ax,StopPtr

		lda W_RulBits
		and #W_r_left
		bne NoCut

		jsl W_CutLine

NoCut

		lda Q_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
NewChrPtr
		moveword #1,changed
		subword W_JustPtr,ChrPtr,YStop2
		subword StopPtr,ChrPtr,YStop
		jeq DLExit
		ldy #0
PLoop
		lda [ChrPtr],y
		and #$ff
		cmp #sp
		bcc DumpIt
		phy
		pha

		lda changed
		beq NoChange

		lda W_STFont
		ldx W_STStyle
		ldy W_STColor
		jsl D_SelectFont

		stz changed

NoChange
		pla
		tay	  ;get character
		cmp #sp
		bne DoReg
		lda W_RulBits
		and #W_r_full
		beq DoReg
		lda SetJust
		beq DoReg

		addword W_JustExtra,FracBits,FracBits
		bcc NoBit
		inc CurH
NoBit

		addword W_JustExtra+2,CurH,CurH

		ldy #sp

DoReg	tya
		jsl D_GetWdth

GotWidth
		clc
		adc CurH
		sta CurH
		ply

		iny
		cpy YStop2
		bcc NoCheck
		jsr JustCheck
NoCheck
		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

		jsr JustCheck

NoExtra

		cmpl Ptr2,StopPtr
		bcs DLExit

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

DLExit

		pushword par
		jsl W_ParUnLock

		return

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

AdjustTab
		pha
		pushword W_STFont
		pushword W_STStyle
		pushword W_STColor
		pushword CurH
		pushlong Ptr2
		jsl W_UseTab
		pla
		sta CurH

		addlong Ptr2,#1,ChrPtr

		brl NewChrPtr


PageWidth
		movelong #W_PageStr,StringPtr
		bra DoWidth

DateWidth
		movelong #W_DateStr,StringPtr
		bra DoWidth

TimeWidth
		movelong #W_TimeStr,StringPtr

DoWidth
		pha
		pushlong StringPtr
		_StringWidth
		pla
		addword a,CurH,CurH

		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
		and #$ff
		sta W_STColor
		addlong #2,Ptr2,ChrPtr
		brl NewChrPtr

JustCheck		;		
		phy
		lda SetJust
		bne JCExit
		inc SetJust

JCExit	ply
		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_PixelToOffset(par,Q_Line,pixel) : W_offset
W_PixelToOffset	PROC		EXPORT
			;Using wpglobals

		input par:w,Q_Line:w,pixel:w
		local W_ParRec:l,LineRecPtr:l,LineHand:l,changed:w,DoneFull:w
		local YStop:w,Ptr2:l,ChrPtr:l,StopPtr:l,CurH:w,CurWidth:w
		local YStop2:w,ExBits:w,tempCur:w,StringPtr:l
		output W_offset:w
		begin

		stz ExBits

		pushword par
		jsl W_ValidLines

		pushword par
		jsl W_ParLock

		lda par
		jsl W_GetParRec
		movelong ax,W_ParRec
		jsl W_ReadRulerP

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

		movelong LineRecPtr,ax
		ldy Q_Line
		jsl W_GetLineRec
		movelong ax,LineRecPtr

		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 Q_Line,W_STLIne
		moveword [LineRecPtr]:#W_lOffset+W_lBytes,a
		tax
		lda par
		jsl W_GetAddr
		movelong ax,W_JustPtr
		movelong ax,StopPtr

		lda W_RulBits
		and #W_r_left
		bne NoCut

		jsl W_CutLine

NoCut

		lda Q_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
		stz CurWidth			 ;last used W_Char width
		stz DoneFull
NewChrPtr
		ldy #0
		moveword #1,changed
		subword W_JustPtr,ChrPtr,YStop2
		subword StopPtr,ChrPtr,YStop
		jeq GotOffset
PLoop
		cmpw CurH,Pixel
		jcs GotOffset
		lda [ChrPtr],y
		and #$ff
		cmp #sp
		bcc DumpIt
		phy
		pha

		lda changed
		beq NoChange

		lda W_STFont
		ldx W_STStyle
		ldy W_STColor
		jsl D_SelectFont

		stz changed

NoChange
		pla
		tay
		cmp #sp
		bne DoReg
		lda W_RulBits
		and #W_r_full
		beq DoReg
		lda DoneFull
		beq DoReg

		moveword CurH,TempCur

		addword W_JustExtra+2,CurH,CurH
		addword W_JustExtra,ExBits,ExBits
		bcc NoCInc
		inc CurH
NoCInc

		lda #sp
		jsl D_GetWdth
		clc
		adc CurH
		sta CurH

		subword CurH,TempCur,CurWidth
		bra DoneAdjust

DoReg	tya
		jsl D_GetWdth
		sta CurWidth
		clc
		adc CurH
		sta CurH

DoneAdjust
		ply

		iny
		cpy YStop2
		bcc NoCheck
		jsr JustCheck
NoCheck
		cpy YStop
		bcs GotOffset
		brl PLoop

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

		cmpl ChrPtr,W_JustPtr
		bcc NoExtra

		lda DoneFull
		bne NoExtra

		jsr JustCheck

NoExtra

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


GotOffset
		tya
		clc
		adc ChrPtr
		sta ChrPtr
		lda #0
		adc ChrPtr+2
		sta ChrPtr+2

		subword CurH,CurWidth,a
		cmp Pixel
		bcs MakeOffset
		lsr CurWidth
		adc CurWidth
		cmp Pixel
		bcc MakeOffset

		decL ChrPtr

MakeOffset
		lda par
		ldx #0
		jsl W_GetAddr
		pha
		lda ChrPtr
		sec
		sbc 1,s
		sta W_offset
		pla

DLExit

		pushword par
		jsl W_ParUnLock

		return

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

AdjustTab
		pha
		pushword W_STFont
		pushword W_STStyle
		pushword W_STColor
		pushword CurH
		pushlong Ptr2
		jsl W_UseTab
		subword 1:s,CurH,CurWidth
		pla
		sta CurH

		addlong Ptr2,#1,ChrPtr

		brl NewChrPtr


PageWidth
		movelong #W_PageStr,StringPtr
		bra DoWidth

DateWidth
		movelong #W_DateStr,StringPtr
		bra DoWidth

TimeWidth
		movelong #W_TimeStr,StringPtr

DoWidth
		pha
		pushlong StringPtr
		_StringWidth
		pla
		sta CurWidth
		addword a,CurH,CurH

		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
		and #$ff
		sta W_STColor
		addlong #2,Ptr2,ChrPtr
		brl NewChrPtr


JustCheck
		lda DoneFull
		bne JCExit
		inc DoneFull
JCExit
		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
		END

