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

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

	IMPORT	W_CalcDocRect
	IMPORT	W_CalcLineHeight
	IMPORT	W_CalcScrollVals
	IMPORT	W_CheckWidow
	IMPORT	D_Deref
	IMPORT	W_GetPageSize
	IMPORT	W_GetParRec
	IMPORT	W_GetTopSpace
	IMPORT	W_GotNewCtlVals
	IMPORT	lastline
	IMPORT	W_LastP
	
	IMPORT	W_LineToTopPixel
	IMPORT	W_NumPgs
	IMPORT	W_OverRide
	IMPORT	W_PaperSize
	
	IMPORT	W_ReadRulerP
	IMPORT	W_ScrollBar
	IMPORT	W_SetFullREct
	IMPORT	W_TopParHeight
	IMPORT	W_TopPg
	IMPORT	W_TopPgLine
	IMPORT	W_TopPgPar
	IMPORT	W_TopPgPixel
	IMPORT	D_UnLock
	IMPORT	W_ValidLines
	
;-----------------------------------------------
;
;   Forward addresses and entries
;
;-----------------------------------------------

	ENTRY	W_NewNumPgs

********************************************************************
*
*	includes...
*
* Wednesday, May 3, 1989 1:23:24 AM
********************************************************************




****************************************************************
* W_CalcPages
W_CalcPages	PROC	EXPORT
		;Using WPGlobals

	local CurPar:w,ParPtr:l,Page:w,Pixel:w,LineHand:l,LinePtr:l
	local temp:w,line:w,lastline:w,PagePixs:w
	begin

	moveword #1,CurPar
	sta Page
	stz Pixel

	lda CurPar
	jsl W_GetParRec
	movelong ax,ParPtr

	lda Page
	jsl W_GetPageSize
	sta PagePixs

; this loop is for normal pars, drops into subloop for pars that cross line
addloop
	moveword [ParPtr]:#W_pAttr,a
	beq okpar
	and #W_PgBrk
	beq okpar
	inc Page
	lda Page
	jsl W_GetPageSize
	sta PagePixs
	stz Pixel
	brl nextpar

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

	lda curpar
	jsl W_CheckWidow
	bcc NoWidow

	lda pixel
	beq NoWidow

	inc Page
	lda Page
	jsl W_GetPageSize
	sta PagePixs
	stz Pixel
	brl addloop

NoWidow
	pushword CurPar
	jsl W_ValidLines

	movelong ParPtr,ax
	jsl W_ReadRulerP

	movelong [ParPtr]:#W_pLineHand,LineHand
	movelong LineHand,ax
	jsl D_Deref
	movelong ax,LinePtr
	addlong #W_LineHeader,LinePtr,LinePtr
	stz line

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

GotLine
	movelong LinePtr,ax
	jsl W_CalcLineHeight
	sta Pixel
	inc Page
	lda Page
	jsl W_GetPageSize
	sta PagePixs
	moveword [ParPtr]:#W_pLastLine,lastline
	bra EndLoop

LineLoop2
	movelong LinePtr,ax
	jsl W_CalcLineHeight
	clc
	adc Pixel
	cmp PagePixs
	bcs GotLine

RegPix
	sta Pixel
EndLoop
	inc line
	addlong #W_lBytes,LinePtr,LinePtr
	cmpw line,lastline
	bcc LineLoop2

Linenextpar
	movelong LineHand,ax
	jsl D_UnLock

	bra nextpar


gotnewpixel	sta Pixel

nextpar	inc CurPar
	lda W_LastP
	cmp Curpar
	bcc AllDone
	addlong #W_pBytes,ParPtr,ParPtr
	brl addloop

AllDone
	cmpw Page,W_NumPgs
	beq W_AllSet

	pushword Page
	jsl W_NewNumPgs

W_AllSet

	return

	ENDP




****************************************************************
* W_NewNumPgs
W_NewNumPgs	PROC	EXPORT
		;Using WPGlobals

	input Pages:w
	begin

	moveword Pages,W_NumPgs

	jsl W_SetFullREct

	pha
	pha
	pha
	jsl W_CalcScrollVals
	pla
	ply
	pha
	phy
	pushlong W_ScrollBar
	_SetCtlParams
	pla

	return

	ENDP




****************************************************************
* W_SetTopPage
W_SetTopPage	PROC	EXPORT
		;Using WPGlobals

	local ParPtr:l,Page:w,Pixel:w,LineHand:l,LinePtr:l
	local temp:w,line:w,lastline:w,StartPixel:w,PagePixs:w
	local TParHt:w
	begin

	moveword #1,W_TopPgPar
	sta Page
	stz W_TopPgLine
	stz W_TopParHeight
	stz Pixel

	lda Page
	jsl W_GetPageSize
	sta PagePixs

	lda W_TopPgPar
	jsl W_GetParRec
	movelong ax,ParPtr

	cmpw #1,W_TopPg
	jeq AllDone

; this loop is for normal pars, drops into subloop for pars that cross line
addloop
	stz W_TopPgLine
	stz W_TopParHeight
	moveword [ParPtr]:#W_pAttr,a
	beq okpar
	and #W_PgBrk
	beq okpar
	inc Page
	lda Page
	jsl W_GetPageSize
	sta PagePixs
	stz Pixel
	brl nextpar

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

	lda W_TopPgPar
	jsl W_CheckWidow
	bcc NoWidow

	lda pixel
	beq NoWidow

	inc Page
	lda Page
	jsl W_GetPageSize
	sta PagePixs
	stz Pixel
	brl addloop

NoWidow

	pushword W_TopPgPar
	jsl W_ValidLines

	movelong ParPtr,ax
	jsl W_ReadRulerP

	movelong [ParPtr]:#W_pLineHand,LineHand
	movelong LineHand,ax
	jsl D_Deref
	movelong ax,LinePtr
	addlong #W_LineHeader,LinePtr,LinePtr
	stz line
	stz TParHt
	stz W_TopParHeight

LineLoop
	movelong LinePtr,ax
	jsl W_CalcLineHeight
	tay
	clc
	adc Pixel
	cmp PagePixs			;watch YYYYY!!!!!!
	bcs GotLine
	sta Pixel
	addword y,TParHt,TParHt

	inc line
	addlong #W_lBytes,LinePtr,LinePtr
	bra LineLoop

GotLine
	cmpw Page,W_TopPg
	bcs NoPutds

	moveword line,W_TopPgLine
	addword TParHt,W_TopParHeight,W_TopParHeight

NoPutds
	movelong LinePtr,ax
	jsl W_CalcLineHeight
	sta Pixel
	sta TParHt
	inc Page
	lda Page
	jsl W_GetPageSize
	sta PagePixs
	moveword [ParPtr]:#W_pLastLine,lastline
	bra EndLoop

LineLoop2
	movelong LinePtr,ax
	jsl W_CalcLineHeight
	tay
	clc
	adc Pixel		;Watch YYY!!!!!!
	cmp PagePixs
	bcs GotLine

RegPix
	sta Pixel
	addword y,TParHt,TParHt
EndLoop
	inc line
	addlong #W_lBytes,LinePtr,LinePtr
	cmpw line,lastline
	bcc LineLoop2

Linenextpar
	movelong LineHand,ax
	jsl D_UnLock

	bra nextpar


gotnewpixel	sta Pixel

nextpar	cmpw Page,W_TopPg
	bcs AllDone
	inc W_TopPgPar
	addlong #W_pBytes,ParPtr,ParPtr
	brl addloop

AllDone
	moveword [ParPtr]:#W_pAttr,a
	beq W_AllSet
	and #W_PgBrk
	beq W_AllSet

	inc W_TopPgPar

W_AllSet

	return

	ENDP

*****************************************************************************************************
; W_PutOnScreen ( Paragraph:word, Line:word, rectptr:long )
;
;   This routine is called to make sure that the line for the given paragraph is contained in the
; given rect on the screen. If it is then just return; else move things around so top pixel of line
; is assured to be on screen in the given rect. 
;
; Note: if the override flag is set then will redo lines anyway.

W_PutOnScreen PROC	EXPORT
	;Using 	WPGlobals

	input 	par:w,line:w,rectptr:l
	local 	CurPar:w,ParPtr:l,Page:w,Pixel:w,LineHand:l,LinePtr:l
	local 	temp:w,Curline:w,lastline:w,PagePixs:w,rect:r,AddTemp:w
	local 	OldPage:l,ScreenSize:w,LastPage:w,LastPixel:w,PageMod:w
	error 	didsome
	begin

	stz 	didsome

; Get current document rect. If rectptr is NIL then point to current document rect instead.

	call	W_CalcDocRect,in=(!rect:l)
	cpzl 	rectptr
	bne 	gettoppix
	moveword	!rect,rectptr

; Get pixel before the given line in A-reg and height of line in X-reg. If line starts before top
; of screen, or the override flag is set, or pixel above the line is before the top of the specified
; rect then proceed. Else add height of line to get top pixel for next line. If top pixel of next
; line is within rect then exit.
;
; Note: what this is really doing is determining whether the given line is fully contained within
; the specified rect. If so nothing needs to be updated (unless the overide flag has been set).

gettoppix	call	W_LineToTopPixel,in=(par:w,line:w),out=(a:w,x:w)
	bmi 	GoOn
	ldy 	W_OverRide
	bne 	GoOn
	cmpw 	a,[rectptr]
	bcc 	GoOn

	phx				
	clc
	adc 	1,s
	plx
	inc 	a
	cmpw 	a,[rectptr]:#4
	jcc 	exit

; Line was not contained in the given rect (or overide set). Remember current top page. Initialize
; local variables to start @ page 1, paragraph 1, pixel offset 0. Get pointer to paragraph and size
; of page in pixels.

GoOn	movelong 	W_TopPg,OldPage
	moveword 	#1,CurPar
	sta 	Page
	stz 	Pixel
	rcall	W_GetParRec,in=(CurPar:a),out=(ParPtr:ax)
	rcall	W_GetPageSize,in=(Page:a),out=(PagePixs:a)

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

; Set last page and pixel counts. If current paragraph is a page break then bump page count and set
; page modified flag. ( to say page break was found ) Get new page size and clear the pixel count.

addloop	stz 	PageMod
	moveword 	pixel,LastPixel
	moveword 	page,LastPage
	cmpw	[ParPtr]:#W_pAttr,#W_PgBrk
	bne	okpar
	inc	PageMod
	inc 	Page
	rcall	W_GetPageSize,in=(Page:a),out=(PagePixs:a)
	stz 	Pixel
	brl 	nextpar

; Add current paragraph's pixel height to current pixel offset to see if paragraph is fully
; contained on the page. If so branch.

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

; Paragraph goes over page bounds. Check if paragraph has paragraph lock flag on (widow flag). If it
; is a locked paragraph go to next if not already at top of page.

	rcall	W_CheckWidow,in=(curpar:a)
	bcc 	NoWidow
	lda 	pixel
	beq 	NoWidow
	inc 	Page
	rcall	W_GetPageSize,in=(Page:a),out=(PagePixs:a)
	stz 	Pixel
	brl 	addloop

NoWidow	call	W_ValidLines,in=(curpar:w)

	movelong ParPtr,ax
	jsl W_ReadRulerP

	movelong [ParPtr]:#W_pLineHand,LineHand
	movelong LineHand,ax
	jsl D_Deref
	movelong ax,LinePtr
	addlong #W_LineHeader,LinePtr,LinePtr
	stz CurLine

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

GotLine
	movelong LinePtr,ax
	jsl W_CalcLineHeight
	sta Pixel
	inc Page
	lda Page
	jsl W_GetPageSize
	sta PagePixs
	moveword [ParPtr]:#W_pLastLine,lastline
	bra EndLoop

LineLoop2
	movelong LinePtr,ax
	jsl W_CalcLineHeight
	clc
	adc Pixel
	cmp PagePixs
	bcs GotLine

RegPix
	sta Pixel
EndLoop
	inc CurLine
	addlong #W_lBytes,LinePtr,LinePtr
	cmpw CurLine,lastline
	bcc LineLoop2

; Unlock the line handle and go to next paragraph.

linenextpar	rcall	D_Unlock,in=(LineHand:ax)
	bra 	nextpar

; Save new pixel position and fall through to next paragraph.

gotnewpixel	sta 	Pixel

; Increment current paragraph count. If we have passed the paragraph we are trying to put on screen
; then branch; else bump paragraph pointer to next paragraph and go back for more.

nextpar	inc 	curpar
	cmpw	par,curpar
	bcc 	gotpar
	addwl	#W_pBytes,parptr
	brl 	addloop

gotpar	dec CurPar
	moveword LastPage,page
	moveword LastPixel,pixel

	pushword CurPar
	jsl W_ValidLines

	movelong ParPtr,ax
	jsl W_ReadRulerP

	movelong [ParPtr]:#W_pLineHand,LineHand
	movelong LineHand,ax
	jsl D_Deref
	movelong ax,LinePtr
	addlong #W_LineHeader,LinePtr,LinePtr
	stz CurLine

LLineLoop
	movelong LinePtr,ax
	jsl W_CalcLineHeight
	clc
	adc Pixel
	sta Pixel
	cmp PagePixs
	bcs IsPPage
	sta Pixel
W_NextLine
	inc CurLine
	cmpw line,CurLine
	jcc DoneLines
	addlong #W_lBytes,LinePtr,LinePtr
	bra LLineLoop

IsPPage
	movelong LinePtr,ax
	jsl W_CalcLineHeight
	sta Pixel
	inc Page
	lda Page
	jsl W_GetPageSize
	sta PagePixs
	bra W_NextLine


DoneLines
	movelong LineHand,ax
	jsl D_UnLock
	
	lda PageMod
	jne exit
	
	lda Page
	sta W_TopPg
	jsl W_GetTopSpace
	clc
	adc pixel
	sta pixel

	subword rect+4,rect,ScreenSize
;W_TopPgPixel = pixel - (([rectptr]-rect) + (([rectptr]:#4-[rectptr])/2))

	subword [rectptr]:#4,[rectptr],a
	lsr a
	addword a,[rectptr],a
	subword a,rect,a
	sta AddTemp
	lda pixel
	sec
	sbc AddTemp
	bpl GotTPPix
	clc
	adc W_PaperSize
	dec W_TopPg
	ldy W_TopPg
	bne GotTPPix
	inc W_TopPg
	lda #0

GotTPPix	sta W_TopPgPixel

	cmpw W_TopPg,W_NumPgs
	bne W_AllSet
	addword W_TopPgPixel,screensize,a
	cmp W_PaperSize
	bcc W_AllSet

	subword W_PaperSize,screensize,W_TopPgPixel

W_AllSet
	inc DidSome
	jsl W_SetFullREct
	

	pha
	pha
	pha
	jsl W_CalcScrollVals
	pla
	pla
	pushlong W_ScrollBar
	_SetCtlValue

	pushlong OldPage
	jsl W_GotNewCtlVals

exit	return

	ENDP
	END

