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

;-----------------------------------------------
;
;   Imported addresses
;
;-----------------------------------------------
	IMPORT	P_PageRect
	IMPORT	P_FullPageRect
	IMPORT	P_PrintHand

	IMPORT	D_BitMapChanged
	IMPORT	D_Deref
	IMPORT	X_DoAsciitoPLText
	IMPORT	D_DrawIcon
	IMPORT	X_DrawPLText
	IMPORT	D_NeedHand
	IMPORT	P_AddObjRgn
	IMPORT	P_BtmHdl
	IMPORT	P_Constrain
	IMPORT	P_ContentRect
	IMPORT	P_CurrentWin
	IMPORT	P_DefColor
	IMPORT	P_DoHandles
	IMPORT	P_DrawContent
	IMPORT	P_FixRect
	IMPORT	P_GetPageMode
	IMPORT	P_LineSize
	IMPORT	P_Local2PL
	IMPORT	P_MagPt
	IMPORT	P_Mask
	IMPORT	P_MinHdl
	IMPORT	P_ObjList
	IMPORT	P_Page
	IMPORT	P_PageMode
	IMPORT	P_PlsHdl
	IMPORT	P_SpecStr
	IMPORT	P_TopHdl
	IMPORT	P_TrackCursor
	IMPORT	D_SectRgnRect
	IMPORT	D_Set4Pat
	IMPORT	T_ScalePort

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

	ENTRY	P_SetPen





;-----------------------------------------------------------------------------
;
; P_DrawFigure (Figure:w,Pt1:l,Pt2:l)
;

P_DrawFigure	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData

		INPUT Figure:w,Pt1:l,Pt2:l
		LOCAL Q_Rect:r,PageMode:w,Pen:l,Q_OvalSize:w


		BEGIN

		movelong Pt1,Q_Rect
		movelong Pt2,Q_Rect+4
		rcall P_GetPageMode,out=(PageMode:a)

		call P_FixRect,in=(!Q_Rect:l,Figure:w,PageMode:w,#1:w)


		lda Figure
		asl a
		tax
		jmp (JumpTable,x)


JumpTable
	DC.W	DoRect	; 0
	DC.W	DoRect	; 1
	DC.W	DoRect	; 2
	DC.W	DoRect	; 3
	DC.W	DoLine	; 4
	DC.W	DoLine	; 5
	DC.W	DoRect	; 6
	DC.W	DoRoundRect	; 7
	DC.W	DoOval	; 8
	DC.W	DoRect	; 9


DoLine
		cmpl Q_Rect,Q_Rect+4
		beq ToExit

		tool _MoveTo,in=(Q_Rect:l)
		tool _LineTo,in=(Q_Rect+4:l)
ToExit	bra Exit


DoRect
		tool _FrameRect,in=(!Q_Rect:l)
		bra Exit


DoRoundRect
		moveword #20,Q_OvalSize
		moveword #40,Q_OvalSize+2
		lda PageMode
		beq NoScale
		tool _ScalePt,in=(!Q_OvalSize:l,#P_PageRect:l,#P_FullPageRect:l)
NoScale
		tool _FrameRRect,in=(!Q_Rect:l,Q_OvalSize:l)
		bra Exit


DoOval
		tool _FrameOval,in=(!Q_Rect:l)


Exit
		RETURN
		ENDP






;------------------------------------------------------------------------------
;
; P_DrawSpec (ObjPtr:l)
;

P_DrawSpec	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData
			;Using T_Data
			;Using D_GlobalData

		INPUT ObjPtr:l
		LOCAL AsciiHdl:l,StaticText:l,Lngth:l,Port:l
		LOCAL Q_Rect:r,ScaleRect:r

		BEGIN

		lda P_SpecStr
		and #$ff
		sta Lngth
		stz Lngth+2

		call D_NeedHand,in=(Lngth:l),out=(AsciiHdl:l)
		jcs Exit
		tool _PtrToHand,in=(#P_SpecStr+1:l,AsciiHdl:l,Lngth:l)

		in AsciiHdl:l,[ObjPtr]:#P_ItemFontId:l,[ObjPtr]:#P_ItemColor:w
		out StaticText:l
		xcall X_DoAsciitoPLText
		jcs Done2

		moverect [ObjPtr]:#P_ObjRect,Q_Rect

		lda P_PageMode
		bne OffScreen
		call P_FixRect,in=(!Q_Rect:l,#P_RECTANGLE:w,#0:w,#1:w)
		call X_DrawPLText,in=(StaticText:l,!Q_Rect:l)
		brl Done1


OffScreen
		subword Q_Rect+4,Q_Rect,a
		cmp #100
		blt VOkay
		addword Q_Rect,#100,Q_Rect+4
VOkay

		moverect Q_Rect,ScaleRect
		tool _MapRect,in=(!ScaleRect:l,#P_PageRect:l,#P_FullPageRect:l)

		subword Q_Rect+4,Q_Rect,Q_Rect+4
		subword Q_Rect+6,Q_Rect+2,Q_Rect+6
		stz Q_Rect
		stz Q_Rect+2


		tool _SetPort,in=(T_ScalePort:l)
		tool _EraseRect,in=(!Q_Rect:l)
		call X_DrawPLText,in=(StaticText:l,!Q_Rect:l)
		tool _SetPort,in=(P_CurrentWin:l)

		pushlong T_ScalePort			 ;Src LocInfo
		pushlong P_CurrentWin			;Dest LocInfo
		pushlong !Q_Rect				   ;Src Q_Rect
		pushlong !ScaleRect			  ;Dest Q_Rect
		pushword #NotBIC				 ;Mode
		pushlong #0
		_CopyPixels

		moveword #1,>D_BitMapChanged


Done1	tool _DisposeHandle,in=(StaticText:l)
Done2	tool _DisposeHandle,in=(AsciiHdl:l)

Exit	RETURN
		ENDP




;------------------------------------------------------------------------------
;
; P_IsObjSel () : Result:w
;
; Returns true if an object is selected.

P_IsObjSel	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData

		OUTPUT Result:w
		LOCAL  ObjHdl:l,ObjPtr:l

		BEGIN

		stz Result
		movelong P_ObjList,ObjHdl

Loop	cpzl ObjHdl
		beq Exit

		movelong [ObjHdl],ObjPtr
		moveword [ObjPtr]:#P_Deleted,a
		bne NextObj

		moveword [ObjPtr]:#P_Selected,a
		bne Yes

NextObj
		movelong [ObjPtr],ObjHdl
		bra Loop

Yes	inc Result

Exit	RETURN
		ENDP




;------------------------------------------------------------------------------
;
; P_MakeFigure (Figure:w,AnchorPt:l) : EndPt:l
;
; AnchorPt must be in D_PL coordinates.

P_MakeFigure	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData

		INPUT  Figure:w,AnchorPt:l
		OUTPUT EndPt:l
		LOCAL  NewPt:l

		BEGIN

		movelong AnchorPt,EndPt

		tool _SetPenMode,in=(#notXOR:w)

		lda Figure
		cmp #P_STRLINE
		blt Normal

		call P_SetPen,in=(P_LineSize:w,P_DefColor:w)
		bra WhileLoop

Normal	tool _SetPenSize,in=(#1:w,#1:w)
		tool _SetPenMask,in=(#P_Mask:l)



WhileLoop
		tool _StillDown,in=(#0:w),out=(a:w)
		jeq EndWhile

		tool _GetMouse,in=(!NewPt:l)
		call P_TrackCursor,in=(!NewPt:l)
		rcall P_Local2PL,in=(NewPt:ax),out=(NewPt:ax)

		cmpw Figure,#P_RECTANGLE
		blt NoCorrect
		cmp #P_CIRCLE+1
		bge NoCorrect
		addword P_LineSize,NewPt,NewPt
		lda P_LineSize
		asl a
		addword a,NewPt+2,NewPt+2
NoCorrect

		call P_Constrain,in=(Figure:w,AnchorPt:l,!NewPt:l,P_PrintHand:l)
		call P_MagPt,in=(!NewPt:l,!AnchorPt:l,Figure:w)
		call P_Constrain,in=(Figure:w,AnchorPt:l,!NewPt:l,P_PrintHand:l)

		cmpl EndPt,NewPt
		jeq WhileLoop


; Erase Old Figure
		call P_DrawFigure,in=(Figure:w,AnchorPt:l,EndPt:l)

; Draw D_New Figure
		call P_DrawFigure,in=(Figure:w,AnchorPt:l,NewPt:l)

		movelong NewPt,EndPt
		brl WhileLoop
EndWhile


; erase last one
		call P_DrawFigure,in=(Figure:w,AnchorPt:l,EndPt:l)

		_PenNormal

		RETURN
		ENDP





;------------------------------------------------------------------------------
;
; P_SetPen (LineSize:w,Color:w)
;

P_SetPen	PROC		EXPORT
			;Using P_Data

		INPUT LineSize:w,Color:w
		LOCAL Pt:l

		BEGIN


		lda LineSize
		sta Pt
		stz Pt+2

		lda P_PageMode
		beq SetIt

		tool _ScalePt,in=(!Pt:l,#P_PageRect:l,#P_FullPageRect:l)

SetIt	lda Pt
		bne Okay
		inc Pt
Okay	lda Pt
		asl a
		sta Pt+2

		tool _SetPenSize,in=(Pt:l)
		call D_Set4Pat,in=(Color:w)

		RETURN
		ENDP



;------------------------------------------------------------------------------
;
; P_AddThreadRgn (ObjHdl:l,RgnHdl:l)
;
; Adds the rectangles for all the Q_Text objects in the thread starting at
; ObjHdl that are on the current page.

P_AddThreadRgn	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData

		INPUT ObjHdl:l,RgnHdl:l
		LOCAL ObjPtr:l

		BEGIN

Loop	cpzl ObjHdl
		beq EndLoop

		rcall D_Deref,in=(ObjHdl:ax),out=(ObjPtr:ax)

		cmpw P_Page,[ObjPtr]:#P_PageNo
		bne Skip
		call P_AddObjRgn,in=(ObjPtr:l,RgnHdl:l)
Skip

		tool _HUnlock,in=(ObjHdl:l)

		movelong [ObjPtr]:#P_NThread,ObjHdl
		bra Loop
EndLoop

		RETURN
		ENDP




;-----------------------------------------------------------------------------
;
; P_DrawRgn (UpdateRgn:l)
;
; Disposes the UpdateRgn when done.

P_DrawRgn	PROC		EXPORT
			;Using P_Data

		INPUT UpdateRgn:l
		LOCAL ClipHdl:l,ClipPtr:l,Q_Rect:r

		BEGIN

		call D_SectRgnRect,in=(UpdateRgn:l,#P_ContentRect:l)
		tool _GetClipHandle,out=(ClipHdl:l)
		tool _EraseRgn,in=(ClipHdl:l)

		movelong [ClipHdl],ClipPtr
		moverect [ClipPtr]:#2,Q_Rect

		call P_DrawContent,in=(!Q_Rect:l,#0:w)
		tool _ClipRect,in=(#P_ContentRect:l)
		tool _DisposeRgn,in=(UpdateRgn:l)

		RETURN
		ENDP




;------------------------------------------------------------------------------
;
; P_DrawHandles (ObjPtr:l)
;

P_DrawHandles	PROC		EXPORT
			;Using P_ObjData
			;Using P_IconData
			;Using P_Data

		INPUT ObjPtr:l
		LOCAL Q_Rect:r,MidPt:w,PThread:l,NThread:l

		BEGIN

		call P_DoHandles,in=(ObjPtr:l,#0:w,#0:l),out=(a:w)

		cmpw [ObjPtr]:#P_Type,#P_ALPHA
		jne Exit

		moverect [ObjPtr]:#P_ObjRect,Q_Rect
		call P_FixRect,in=(!Q_Rect:l,[ObjPtr]:#P_Type:w,P_PageMode:w,#1:w)

		subword Q_Rect+6,Q_Rect+2,a
		lsr a
		subword a,#7,a
		addword a,Q_Rect+2,MidPt


		pushlong ObjPtr


TryPrev
		movelong [ObjPtr]:#P_PThread,PThread
		cpzl PThread
		beq NoPrev

		movelong [PThread],ObjPtr
		moveword [ObjPtr]:#P_Deleted,a
		bne TryPrev

		pushlong #P_MinHdl
		bra Cont
NoPrev	pushlong #P_TopHdl
Cont	pushword MidPt
		subword Q_Rect,#5,s
		pushword #notXOR
		jsl D_DrawIcon

		pulllong ObjPtr


TryNext
		movelong [ObjPtr]:#P_NThread,NThread
		cpzl NThread
		beq NoNext

		movelong [NThread],ObjPtr
		moveword [ObjPtr]:#P_Deleted,a
		bne TryNext

		pushlong #P_PlsHdl
		bra Cont2
NoNext	pushlong #P_BtmHdl
Cont2	pushword MidPt
		lda Q_Rect+4
		dec a
		pha
		pushword #notXOR
		jsl D_DrawIcon


Exit	RETURN
		ENDP
		END

