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

	include 'pl.equ'

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

	IMPORT	D_CheckPurge
	IMPORT	P_ContentRect
	IMPORT	P_CurrentWin
	IMPORT	P_DrawContent
	IMPORT	P_FirstPOff
	IMPORT	P_H
	IMPORT	P_LoadPage
	IMPORT	P_Page
	IMPORT	P_PageCount
	IMPORT	P_PageMode
	IMPORT	P_PageRect
	IMPORT	P_PaperRect
	IMPORT	P_PrintHand
	IMPORT	P_SavePage
	IMPORT	P_V
	IMPORT	D_PrintToLW
	IMPORT	D_PrOpenDoc

;		   pprint.s



;---------------------------------------------------------------------------
;
; P_PrintDoc (Window:l)	
;

P_PrintDoc	PROC		EXPORT
			;Using P_Data

		input	Window:l
		error	Err

		local	PHdl:l,PPtr:l,PrinterPort:l,Spool:w,Copies:w
		local	FirstPage:w,LastPage:w,CurPage:w,docOpened:w


		BEGIN +b

		stz Err
		stz docOpened

		call P_SavePage,in=(P_Page:w)

; D_Save to be restored
		pushword P_Page
		pushword P_PageMode
		pushlong P_CurrentWin
		pushword P_H
		pushword P_V
		pushlong P_ContentRect

		stz P_PageMode
		stz P_H
		stz P_V

		subword P_PaperRect,P_PageRect,P_ContentRect
		subword P_PaperRect+2,P_PageRect+2,P_ContentRect+2



		movelong P_PrintHand,PHdl
		movelong [PHdl],PPtr
		moveword [PPtr]:#oPrJob+oBJDocLoop,a
		and #$ff
		sta Spool

		moveword [PPtr]:#oPrJob+oICopies,Copies
		moveword [PPtr]:#oPrJob+oIFstPage,FirstPage
		moveword [PPtr]:#oPrJob+oILstPage,LastPage


		moveword #1,[PPtr]:#oPrJob+oIFstPage
		moveword #1,[PPtr]:#oPrJob+oICopies			; do copies *our* way: collated.

; if it's a LaserWriter, do it the ÔnormalÕ way.
		lda >D_PrintToLW
		bne @normalMuck
		
		moveword #1,[PPtr]:#oPrJob+oILstPage
		bra @didMuck
		
@normalMuck
		subword LastPage,FirstPage,a
		ina
		moveword a,[PPtr]:#oPrJob+oILstPage
@didMuck
; Adjust the page range...

; if it's 1-999, pretend like it's 0-999

		cmpw LastPage,#999
		bne @cont
		cmpw FirstPage,#1
		bne @cont
		stz FirstPage
@cont

		lda FirstPage
		bne NotZero
		cmpw P_FirstPOff,#-2
		beq NotZero
		inc FirstPage
NotZero


		subword LastPage,P_FirstPOff,LastPage
		cmpw a,P_PageCount
		blt LastOkay
		lda P_PageCount
		inc a
		sta LastPage
LastOkay


		subword FirstPage,P_FirstPOff,FirstPage
		scmpw a,#2
		bge FirstOkay
		moveword #2,FirstPage
FirstOkay



CopyLoop
		lda Copies
		jeq Exit
		dec Copies
		moveword FirstPage,CurPage


PageLoop
		cmpw LastPage,CurPage
		blt CopyLoop


; D_Open printer doc

		lda docOpened
		bne @opened
		
		moveword #-1,docOpened
		call D_PrOpenDoc,in=(PHdl:l,#0:l),out=(PrinterPort:l),err=(Err)
		jcs Abort

@opened	movelong PrinterPort,P_CurrentWin

		call P_LoadPage,in=(CurPage:w)
		tool _PrOpenPage,in=(PrinterPort:l,#0:l),err=(err)
		bcs CloseIt

		tool _SetFontFlags,in=(#4:w)
		tool _SetTextMode,in=(#modeForeCopy:w)
		call P_DrawContent,in=(#P_PaperRect:l,#1:w)

CloseIt	tool _PrClosePage,in=(PrinterPort:l)
		lda err
		jne Abort


		lda >D_PrintToLW
		bne ckMem
		
closeD	tool _PrCloseDoc,in=(PrinterPort:l),err=(Err)
		stz docOpened
		jcs Exit


didclose
		jsl D_CheckPurge
		bcc MemOkay
		tool _PRSetError,in=(#-128:w)
		lda docOpened
		jeq Exit
		brl abort

ckMem	jsl D_CheckPurge
		bcc NoSpool
		tool _PRSetError,in=(#-128:w)
		lda docOpened
		jeq Exit
		brl abort

MemOkay

; Print spooled file if neccessary

		lda Spool
		beq NoSpool
		tool _PrPicFile,in=(PHdl:l,#0:l,#StatusRecord:l),err=(Err)
		bcs Exit
NoSpool


		inc CurPage
		brl PageLoop



Exit	lda docOpened
		beq @restore
		stz Copies
		bra closeD

@restore
; Restore from stack

		pulllong P_ContentRect
		pullword P_V
		pullword P_H
		pulllong P_CurrentWin
		pullword P_PageMode

		call P_LoadPage,in=(:w)

		RETURN

Abort	tool _PrCloseDoc,in=(PrinterPort:l)
		stz docOpened
		bra Exit

StatusRecord		DS.B 28

		ENDP
		END

