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

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

	IMPORT	D_Deref
	IMPORT	D_DrawIcon
	IMPORT	P_ContentRect
	IMPORT	P_CtlBoxIcon
	IMPORT	P_CurrentWin
	IMPORT	P_DblPage
	IMPORT	P_DoScritch
	IMPORT	P_DrawGuides
	IMPORT	P_DrawObjList
	IMPORT	P_DrawRulers
	IMPORT	P_DrawTemplate
	IMPORT	P_DrawTools
	IMPORT	P_Entry
	IMPORT	P_FirstPOff
	IMPORT	P_FullPageRect
	IMPORT	P_FullPaperRect
	IMPORT	P_GrowIcon
	IMPORT	P_GrowRect
	IMPORT	P_H
	IMPORT	P_HScroll
	IMPORT	P_IconPalette
	IMPORT	P_LIcon
	IMPORT	P_LPageIcon
	IMPORT	P_L_ArrHollowIcon
	IMPORT	P_LastObj
	IMPORT	P_LoadWindowStuff
	IMPORT	P_NewClip
	IMPORT	P_NumberTab
	IMPORT	P_OldClip
	IMPORT	P_OldIdlePt
	IMPORT	P_PIconRect
	IMPORT	P_Page
	IMPORT	P_PageCount
	IMPORT	P_PageMode
	IMPORT	P_PageRect
	IMPORT	P_PageStart
	IMPORT	P_PaperRect
	IMPORT	P_PortRect
	IMPORT	P_RIcon
	IMPORT	P_RPageIcon
	IMPORT	P_R_ArrHollowIcon
	IMPORT	P_TIcon
	IMPORT	P_TrackOn
	IMPORT	P_UseTemplate
	IMPORT	P_V
	IMPORT	P_VScroll
	IMPORT	P_VisGuides
	IMPORT	P_VisPalette
	IMPORT	P_VisRuler
	IMPORT	P__LPIcon
	IMPORT	P__RPIcon
	IMPORT	D_RectSect
	IMPORT	D_Set4Pat
	IMPORT	T_BlinkCaret
	IMPORT	T_CaretDrawn
	IMPORT	T_Editing
	IMPORT	T_InvSelect
	IMPORT	T_TextSelect
	IMPORT	T_ThreadRgn
	IMPORT	D_UnLock

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

	ENTRY	P_DrawAPage
	ENTRY	P_DrawContent
	ENTRY	P_PageIcons
	ENTRY	P_UpdateDoc

;		   pupdate.s




;-------------------------------------------------------------------------------
;
; P_ForceUpdate (WindowPtr:l)
;
; Forces an D_Update of the current window.

P_ForceUpdate	PROC		EXPORT

		INPUT WindowPtr:l

		BEGIN

		tool _BeginUpdate,in=(WindowPtr:l)
		call P_UpdateDoc,in=(WindowPtr:l)
		tool _EndUpdate,in=(WindowPtr:l)

		RETURN
		ENDP



;------------------------------------------------------------------------------
;
; P_UpdateDoc (Window:l)
;
; Do not call BeginUpdate, EndUpdate or set your own origin.
;

P_UpdateDoc	PROC		EXPORT
			;Using P_RulerData
			;Using P_Data
			;Using P_IconDATA
			;Using P_ObjData
			;Using T_Data

		input window:l
		local rectptr:l,rgn:l,OldWind:l,OldEdit:w,NotActive:w
		local tmp:l,ObjHdl:l,ObjPtr:l,tmprect:r

		begin +b

		stz NotActive
		cmpl Window,P_CurrentWin
		beq DoUpdate
		inc NotActive
		movelong P_CurrentWin,OldWind
		moveword T_Editing,OldEdit
		stz T_Editing
		call P_LoadWindowStuff,in=(window:l)


DoUpdate
		tool _GetVisHandle,out=(rgn:l)

		rcall D_Deref,in=(rgn:ax),out=(tmp:ax)
		addlong tmp,#2,rectptr
		tool _EraseRect,in=(RectPtr:l)


		lda P_VisPalette
		beq NoPalette

		call D_RectSect,in=(rectptr:l,#P_IconPalette:l),out=(a:w)
		beq NoPalette


		moverect P_IconPalette,tmprect
		moveword P_ContentRect+4,tmprect+4
		tool _ClipRect,in=(!tmprect:l)

		call P_DrawTools
NoPalette


DoRulers	lda P_VisRuler
		beq DoneRulers
		call P_DrawRulers,in=(rgn:l)
DoneRulers
		tool _ClipRect,in=(#P_PortRect:l)

; Redraw scritches.
		lda P_TrackOn
		beq CkGrow

		call P_DoScritch,in=(P_OldIdlePt:l)


CkGrow
		call D_RectSect,in=(rectptr:l,#P_GrowRect:l),out=(a:w)
		beq CkScroll
		pushlong #P_GrowIcon
		subword P_GrowRect+2,#1,s
		pushword P_GrowRect
		pushword #modeCopy
		jsl D_DrawIcon



CkScroll
		movelong P_HScroll,tmp
		spaceword
		pushlong [tmp]:#0
		addlong 1:s,#8,1:s		  ; get address of Q_Rect
		call D_RectSect,in=(rectptr:l)
		pla
		beq CkVert

		tool _DrawOneCtl,in=(P_HScroll:l)

CkVert
		movelong P_VScroll,tmp
		spaceword
		pushlong [tmp]:#0
		addlong 1:s,#8,1:s		  ; get address of Q_Rect
		call D_RectSect,in=(rectptr:l)
		pla
		beq CkIcons

		tool _DrawOneCtl,in=(P_VScroll:l)


CkIcons
		call D_RectSect,in=(rectptr:l,#P_PIconRect:l),out=(a:w)
		beq CkContent
		call P_PageIcons

CkContent
		call D_RectSect,in=(rectptr:l,#P_ContentRect:l),out=(a:w)
		beq Done

		tool _ClipRect,in=(#P_ContentRect:l)
		call P_DrawContent,in=(rectptr:l,#0:w)


Done
		rcall D_UnLock,in=(rgn:ax)
		lda NotActive
		beq Exit

		call P_LoadWindowStuff,in=(OldWind:l)
		moveword OldEdit,T_Editing

Exit	RETURN
		ENDP





;---------------------------------------------------------------------------
;
; P_PageIcons 
;
;
; invariants:   P_PageCount = # of user pages
;			   P_PageStart = 1st user page that is displayed as an icon
;			   P_Page	  = current page's index into page array
;						   = user page # + 1
;			   P_DblPage   = bool. F=1 template, T=L/R template
;
;
; variables:	xpos		= current icon x coordinate
;			   ypos		=	'	 '   y	 "
;			   xnum		= current number icon x coordinate
;			   ynum		=	'	  '	 '   y	 '

P_PageIcons	PROC		EXPORT
			;Using P_Data
			;Using P_IconData

		local xpos:w,ypos:w
		local count:w,pagenum:w

		begin


		moveword P_ContentRect+4,ypos
		moveword #-1,xpos


		; Draw the template page(s)
		lda P_DblPage
		beq DoRPage
		call P_DrawAPage,in=(xpos:w,ypos:w,P_FirstPOff:w,#1:w)
		addword #P_PageWidth-2,xpos,xpos

DoRPage	addword P_FirstPOff,#1,a
		call P_DrawAPage,in=(xpos:w,ypos:w,a:w,#1:w)

		addword xpos,#P_PageWidth,a
		addword a,#P_SpaceWidth,xpos

DoPages
		; Draw the user page(s)
		; Draw the left control if necess.
		cmpw P_PageCount,#P_MaxVisPages+1
		blt DrawVis

		call D_DrawIcon,in=(#P_CtlBoxIcon:l,xpos:w,ypos:w,#notBIC:w)
		addword xpos,#4,xpos
		inc ypos
		call D_DrawIcon,in=(#P_L_ArrHollowIcon:l,xpos:w,ypos:w,#modeCOPY:w)
		dec ypos
		addword xpos,#P_ControlWidth-6,xpos

DrawVis
		; Draw the visible pages
		cmpw P_PageCount,#6
		blt GotCount
		lda #5
GotCount	sta count						   ; this is the # of icons
		moveword P_PageStart,pagenum

loop	call P_DrawAPage,in=(xpos:w,ypos:w,pagenum:w,#1:w)

		addword xpos,#P_PageWidth-2,xpos
		inc pagenum
		dec count
		bne loop


DrawRCtl
		; Draw the right control if necess.
		cmpw P_PageCount,#P_MaxVisPages+1
		blt exit

		call D_DrawIcon,in=(#P_CtlBoxIcon:l,xpos:w,ypos:w,#notBIC:w)
		addword xpos,#4,xpos
		inc ypos
		call D_DrawIcon,in=(#P_R_ArrHollowIcon:l,xpos:w,ypos:w,#modeCOPY:w)

exit	return

		ENDP






;---------------------------------------------------------------------------
;
; P_DrawAPage   ; need to be able to draw page '0'.. set templates to p-2,-1.
;

P_DrawAPage	PROC		EXPORT
			;Using P_Data
			;Using P_IconData

		input xpos:w,ypos:w,pagenum:w,showpage:w
		local mode:w,icon:l,ptr:l,tmppage:w,tmprect:l

		begin

		lda showpage
		bne GoAhead

		; erase the old page
		moveword ypos,tmprect
		addword xpos,#2,tmprect+2
		addword xpos,#P_PageWidth,tmprect+6
		addword ypos,#11,tmprect+4

		tool _EraseRect,in=(!tmprect:l)


GoAhead	addword P_Page,P_FirstPOff,a
		cmp pagenum
		beq InvertMode

		moveword #modeCOPY,mode
		lda P_DblPage
		beq DoRight

		subword pagenum,P_FirstPOff,a	   ; actual page #
		cmp #2
		bge One
		bit #1
		bne DoRight
		bra DoLeft
One	lda pagenum
		bit #1
		bne DoRight

DoLeft	movelong #P_LPageIcon,icon
		bra DoCall

DoRight	movelong #P_RPageIcon,icon
		bra DoCall

InvertMode	moveword #notCOPY,mode
		lda P_DblPage
		beq Do_Right

		subword pagenum,P_FirstPOff,a
		cmp #2
		bge _One
		bit #1
		bne Do_Right
		bra Do_Left
_One	lda pagenum
		bit #1
		bne Do_Right

Do_Left	movelong #P__LPIcon,icon
		bra DoCall

Do_Right	movelong #P__RPIcon,icon

DoCall	call D_DrawIcon,in=(icon:l,xpos:w,ypos:w,#notBIC:w)

		; Do the page numbers
		addword #5,ypos,ypos

		subword pagenum,P_FirstPOff,a
		cmp #1
		bge CheckRightPage

		call D_DrawIcon,in=(#P_LIcon:l,#8:w,ypos:w,mode:w)
		brl exit

CheckRightPage	cmp #2
		bge TheNum
		lda P_DblPage
		beq DoTemplate

		call D_DrawIcon,in=(#P_RIcon:l,#P_PageWidth+7:w,ypos:w,mode:w)
		brl exit

DoTemplate	call D_DrawIcon,in=(#P_TIcon:l,#8:w,ypos:w,mode:w)
		brl exit


;		 ; xpos: + #5, #9, #14: left, Q_center, right...
TheNum	tool _UDivide,in=(pagenum:w,#10:w),out=(tmppage:w,x:w)
		txa
		beq NoLeading

		cmp #10
		blt DoLeading

		tool _UDivide,in=(a:w,#10:w),out=(a:w,x:w)

DoLeading	asl a
		asl a
		addlong a,#P_NumberTab,ptr
		addword #5,xpos,xpos
		call D_DrawIcon,in=([ptr]:l,xpos:w,ypos:w,mode:w)

NoLeading	addword #9,xpos,xpos

		lda tmppage
		asl a
		asl a
		addlong a,#P_NumberTab,ptr
		call D_DrawIcon,in=([ptr]:l,xpos:w,ypos:w,mode:w)

exit	return
		ENDP





;---------------------------------------------------------------------------
;
; P_DrawContent (RectPtr:l,Printing:w)
;

P_DrawContent	PROC		EXPORT
			;Using P_Data
			;Using T_Data

		input rectptr:l,printing:w
		local tmprect:r,tmp:l

		begin

		jsl P_Entry


		lda T_Editing
		beq NotEdit
		lda Printing
		bne NotEdit

		tool _GetClipHandle,out=(P_OldClip:l)
		tool _NewRgn,out=(P_NewClip:l)
		tool _DiffRgn,in=(P_OldClip:l,T_ThreadRgn:l,P_NewClip:l)
		tool _SetClipHandle,in=(P_NewClip:l)
NotEdit



		lda Printing
		jne DoContent

		lda P_PageMode
		bne DoPageMode

		moverect P_PageRect,tmprect
;			 ; offset to the content Q_Rect & offset by paper Q_Rect into content Q_Rect
;			 with H,V scrolling offsets
		subword P_ContentRect,P_V,a
		subword a,P_PaperRect,tmp

		subword P_ContentRect+2,P_H,a
		subword a,P_PaperRect+2,tmp+2

		addword tmprect,tmp,tmprect
		addword tmprect+4,tmp,tmprect+4
		addword tmprect+2,tmp+2,tmprect+2
		addword tmprect+6,tmp+2,tmprect+6
		brl DoDotted

DoPageMode
		call D_RectSect,in=(rectptr:l,#P_FullPaperRect:l),out=(a:w)
		jeq DoneRects

		tool _SetPenSize,in=(#2:w,#1:w)
		tool _FrameRect,in=(#P_FullPaperRect:l)

;	 Shadow...
		addword P_FullPaperRect+2,#2,s
		pushword P_FullPaperRect+4
		_MoveTo

		tool _LineTo,in=(P_FullPaperRect+6:w,P_FullPaperRect+4:w)
		pushword P_FullPaperRect+6
		lda P_FullPaperRect
		inc a
		pha
		_LineTo

		call D_RectSect,in=(rectptr:l,#P_FullPageRect:l),out=(a:w)
		beq DoneRects
		moverect P_FullPageRect,tmprect

DoDotted
		tool _SetPenSize,in=(#2:w,#1:w)
		call D_Set4Pat,in=(#LightGray:w)
		tool _FrameRect,in=(!tmprect:l)

DoneRects	_PenNormal
		lda P_VisGuides
		beq DoContent
		call P_DrawGuides,in=(rectptr:l)



DoContent
		cmpw P_Page,#2
		blt DidTemplate

		lda P_UseTemplate
		beq DidTemplate
		call P_DrawTemplate,in=(P_Page:w,rectptr:l)
DidTemplate


		in P_LastObj:l,RectPtr:l,Printing:w
		xcall P_DrawObjList




		lda T_Editing
		beq Exit
		lda Printing
		bne Exit

		tool _SetClipHandle,in=(P_OldClip:l)
		tool _DisposeRgn,in=(P_NewClip:l)

		lda T_TextSelect
		bne DoInvert
		lda T_CaretDrawn
		beq Exit
		jsl T_BlinkCaret
		bra Exit
DoInvert	jsl T_InvSelect


Exit
		RETURN
		ENDP
		END

