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

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

	IMPORT	W_CalcLineHeight
	IMPORT	W_CalcPages
	IMPORT	CaretOff
	IMPORT	W_CheckWidow
	IMPORT	W_CurDoc
	IMPORT	W_DrawHeader
	IMPORT	W_DrawLine
	IMPORT	W_FOnPage
	IMPORT	W_FirstPage
	
	IMPORT	W_GetDoc
	IMPORT	W_GetLineRec
	IMPORT	W_GetPageSize
	IMPORT	W_GetParRec
	IMPORT	D_GetPrintHandle
	IMPORT	W_GetTopSpace
	IMPORT	W_HOnPage
	
	IMPORT	D_KillFont
	IMPORT	W_lastline
	IMPORT	W_LastP
	IMPORT	W_MakeCaret
	IMPORT	W_NewNumPgs
	
	IMPORT	W_NumPgs
	IMPORT	W_OverRide
	IMPORT	W_PaperTop
	IMPORT	W_ParRec
	
	IMPORT	W_PrintFl
	IMPORT	W_ReadPrintRec
	IMPORT	W_ReadRuler
	IMPORT	W_ReadRulerP
	IMPORT	W_RulOffset
	IMPORT	W_SetFullREct
	IMPORT	W_SwitchData
	IMPORT	W_ValidLines
	IMPORT	W_View
	IMPORT	W_GetSpaceToFooter
	IMPORT	D_PrintToLW
	IMPORT	D_Deref
	IMPORT	D_PrOpenDoc

********************************************************************
*
*	includes...
*
*
*
*
* Thursday, May 4, 1989 6:04:50 AM
********************************************************************





*****************************************************************
* The print record for Window changed. You should adjust all of your
* W_PageSize dependent W_Stuff accordingly.
* input - window, the one whose print record changed.
W_PrintChanged	PROC		EXPORT
			;Using wpglobals

		input window:l
		begin +b
		pushlong W_CurDoc

		pushlong window
		jsl W_GetDoc

		pushlong window
		_SetPort

		spacelong
		pushlong window
		jsl D_GetPrintHandle
		jsl W_ReadPrintRec

		jsl W_SetFullREct

		jsl CaretOff

		jsl W_CalcPages

		pushword W_NumPgs
		jsl W_NewNumPgs

		moveword #1,W_OverRide

		jsl W_MakeCaret

		jsl W_GetDoc
		return

		ENDP




****************************************************************
* W_PrintDoc - please print this W_Document
* input - window, the one belonging to the W_Document
W_PrintDoc	PROC		EXPORT
			;Using wpglobals
			;Using W_RulerData
		EXPORT	ChecOffset

		input window:l
		local W_ParRec:l,par:w,line_:w,LineHand:l,LinePtr:l,W_lastline:w
		local PrintHandle:l,PrintPort:l,CurPage:w,Pixel:w,PagePixels:w
		local LineRec:l,temp:w,IFstPage:w,ILstPage:w,iCopies:w
		local PageOpened:w,NumCopies:w,docOpened:w
		error Ouch
		begin +b
		
		call W_GetDoc,in=(W_CurDoc:l,window:l)
		
		pushword W_View

		stz docOpened

		moveword #W_NormalOn,W_View
		call W_SwitchData,in=(#W_NormalOn:w)

		jsl W_CalcPages

		stz Ouch
		tool _PrSetError,in=(#0:w)	; zero it out...

		tool _SetPort,in=(window:l)

		call D_GetPrintHandle,in=(window:l),out=(PrintHandle:l)
		movelong [PrintHandle],PrintPort

; formula is amount to add = ;W_RulOffset>>(PageRect-PaperRect)
ChecOffset		;		
		moveword >W_RulOffset,temp

		subword [PrintPort]:#oRPaper,[PrintPort]:#oPrInfo+oRPage,a
		subword a,temp,W_PrintFl	;ruler amount offset

		moveword [PrintPort]:#oPrJob+oIFstPage,iFstPage
		moveword [PrintPort]:#oPrJob+oILstPage,iLstPage
		moveword [PrintPort]:#oPrJob+oICopies,iCopies
		sta NumCopies									; this is what we restore the printrecord to.

; 	  * Reset print record to be pp. 1-x, so we don't waste time on pages
;		that aren't being printed.
		moveword #1,[PrintPort]:#oPrJob+oIFstPage
;	  * Pretend like only one copy, but really do it iCopies times.  That
;		way, the copies are collated.
		moveword #1,[PrintPort]:#oPrJob+oICopies
		
		lda >D_PrintToLW
		bne @normalMuck
		
		moveword #1,[PrintPort]:#oPrJob+oILstPage	; IW, print 1:1 page:doc
		bra @didMuck

@normalMuck											; LW, print all pages on one doc.
		subword iLstPage,iFstPage,a
		ina
		moveword a,[PrintPort]:#oPrJob+oILstPage
@didMuck
		
		cmpw iLstPage,#999
		bne @cont
		cmpw iFstPage,#1
		bne @cont
		cmpw W_FirstPage,#-1
		bne @cont
		stz iFstPage
@cont

CopyLoop

		moveword #1,Par
		sta CurPage
		stz line_

		jsr StartNewPage
		jcs EndThePage

GotNewPar
		jsr	chkerr
		jne EndThePage

		stz line_

		rcall W_GetParRec,in=(par:a),out=(W_ParRec:ax)

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

		jsr GetNewPage
		jcs EndThePage

		brl SkipPar

NoPB
		call W_ValidLines,in=(par:w)

		tool _HLock,in=([W_ParRec]:#W_pBlockHand:l)

		movelong [W_ParRec]:#W_pLineHand,LineHand
		rcall D_Deref,in=(LineHand:ax),out=(LinePtr:ax)

		rcall W_ReadRuler,in=([W_ParRec]:#W_pRulerHand:ax)

		moveword [W_ParRec]:#W_pLastLine,W_lastline

		rcall W_CheckWidow,in=(par:a)
		bcc UpdateLoop

		addword pixel,[W_ParRec]:#W_pPixels,a
		cmp PagePixels
		bcc UpdateLoop

		jsr getNewPage
		jcs EndThePage

UpdateLoop
		rcall W_GetLineRec,in=(LinePtr:ax,line_:y),out=(LineRec:ax)
		jsl W_CalcLineHeight
		addword a,pixel,a
		cmp PagePixels
		bcc NotPast
		jsr GetNewPage
		jcs EndThePage
		bra UpdateLoop

NotPast
		sta pixel
		stx temp

		lda PageOpened
		beq SkipDraw

		pushword par
		pushword line_
		pushlong LineRec
		subword pixel,temp,s
		jsl W_DrawLine
SkipDraw

		inc line_
		lda line_
		cmp W_lastline
		bcc UpDateLoop

PastBot
		tool _HUnLock,in=([W_ParRec]:#W_pBlockHand:l)

		tool _HUnLock,in=(LineHand:l)

SkipPar
		inc par
		lda W_LastP
		cmp Par
		jcs GotNewPar

		jsr EndOldPage
		bcs EndThePage			   ; Added by MRH (3/17/89)

		lda iCopies
		beq PDocExit
		dec iCopies
		beq PDocExit
		brl CopyLoop

EndThePage
		lda docOpened				; changed from PageOpened (10/4/89) <MRH>
		beq PDocExit

		lda PageOpened				; changed from docOpened (10/4/89) <MRH>
		beq EndTheDoc

		tool _PrClosePage,in=(PrintPort:l)

EndTheDoc
		tool _PrCloseDoc,in=(PrintPort:l)
		stz docOpened

PDocExit
		lda >D_PrintToLW
		beq @done
		
		lda docOpened	; in case we never did an OpenDoc.
		beq @done
		
		tool _PrCloseDoc,in=(PrintPort:l)

		stz docOpened

		jsr	chkerr
		bne @done	

		tool _PrPicFile,in=(PrintHandle:l,#0:l,#0:l)	; not really needed...

		jsr	chkerr
		
@done	movelong [PrintHandle],PrintPort	; <-- this too

		moveword iFstPage,[PrintPort]:#oPrJob
		iny
		iny
		moveword iLstPage,[PrintPort]:y
		iny
		iny
		moveword NumCopies,[PrintPort]:y

		moveword 1:s,W_View
		jsl W_SwitchData
		jsl W_GetDoc
		stz W_PrintFl
		
		return


StartNewPage
		stz PageOpened

		addword CurPage,W_FirstPage,a
		tax
		scmpw a,iFstPage
		jcc NoFoot

		txa
		dec a
		scmpw a,iLstPage
		jcs NoFoot

		lda docOpened
		bne @opened
		
		moveword #-1,docOpened
		call D_PrOpenDoc,in=(PrintHandle:l,#0:l),out=(PrintPort:l),err=(Ouch)
		jcs SPExit

@opened
		inc PageOpened				; move INC before _PrOpenPage (10/4/89) MRH
		
		tool _PrOpenPage,in=(PrintPort:l,#0:l),err=(Ouch)
		jcs SPExit

		tool _SetFontFlags,in=(#%00000100:w)

		jsl D_KillFont

		rcall W_HOnPage,in=(CurPage:a)
		bcs NoHead

		call W_DrawHeader,in=(#0:w,CurPage:w,#W_HeaderOn:w)

NoHead

		rcall W_FOnPage,in=(CurPage:a)
		bcs NoFoot

		rcall W_GetSpaceToFooter,in=(CurPage:a)
		subword a,W_PaperTop,a
		call W_DrawHeader,in=(a:w,CurPage:w,#W_FooterOn:w)

NoFoot

		rcall W_GetTopSpace,in=(CurPage:a)
		subword a,W_PaperTop,pixel

		rcall W_GetPageSize,in=(CurPage:a)
		addword a,pixel,PagePixels
		clc

SPExit
		php

		rcall W_GetParRec,in=(par:a)
		jsl W_ReadRulerP

		plp
		rts


GetNewPage
		jsr EndOldPage
		bcs GNPExit

		inc CurPage
		jsr StartNewPage


GNPExit
		rts


EndOldPage
		lda PageOpened
		beq NoEnd

		tool _PrClosePage,in=(PrintPort:l)

		stz PageOpened

		jsr	chkerr
;		bne NoEnd				; removed 10/4/89 <MRH>
;								; if error didn't happen, doc might be closed

		lda >D_PrintToLW
		beq @closeit
		lda #0
		bra NoEnd

@closeit
		tool _PrCloseDoc,in=(PrintPort:l)

		stz docOpened

		jsr	chkerr
		bne NoEnd

		tool _PrPicFile,in=(PrintHandle:l,#0:l,#0:l)

		jsr	chkerr

NoEnd	cmp #1
		rts
;...................................................................................................;
; Accumulate error status so if any errors occur they stay remembered. ie keep Print Manager from
; clearing error once its found.

chkerr	tool	_PrError,out=(a:w)
		ora		Ouch
		sta		Ouch
		rts
;...................................................................................................;
		
		ENDP
		END

