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

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

	IMPORT	D_Deref
	IMPORT	P_AddObjRgn
	IMPORT	P_AddThreadRgn
	IMPORT	P_BoundsRect
	IMPORT	P_BuildUndo
	IMPORT	P_Constrain
	IMPORT	P_DrawFigure
	IMPORT	P_DrawHandles
	IMPORT	P_DrawRgn
	IMPORT	P_FixRect
	IMPORT	P_FixSpecRect
	IMPORT	P_ListOp
	IMPORT	P_Local2PL
	IMPORT	P_MagMove
	IMPORT	P_MagPt
	IMPORT	P_ObjList
	IMPORT	P_TrackCursor
	IMPORT	D_Set4Pat
	IMPORT	T_ReThread
	IMPORT 	P_PrintHand
	
;-----------------------------------------------
;
;   Forward addresses and entries
;
;-----------------------------------------------

	ENTRY	P_WaitMouse





;------------------------------------------------------------------------------
;
; P_MoveObject (ObjHdl:l, AnchorPt:l)
;

P_MoveObject	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData

		INPUT ObjHdl:l,AnchorPt:l

		LOCAL ObjPtr:l,NewPt:l,ObjType:w,ObjCount:w
		LOCAL Q_Rect:r,MultRect:r,TRect:r,TMultRect:r
		LOCAL HDiff:w,VDiff:w,UpdateRgn:l,RealPt:l


		BEGIN

		call P_WaitMouse,in=(AnchorPt:l),out=(a:w)
		jeq Exit

		rcall D_Deref,in=(ObjHdl:ax),out=(ObjPtr:ax)
		moveword [ObjPtr]:#P_Type,ObjType
		moverect [ObjPtr]:#P_ObjRect,Q_Rect

		call P_BoundsRect,in=(P_ObjList:l,!MultRect:l),out=(ObjCount:w)
		tool _SetPenMode,in=(#notXOR:w)

		tool _SetPenSize,in=(#2:w,#1:w)
		call D_Set4Pat,in=(#LeftGray:w)


WhileLoop
		tool _GetMouse,in=(!NewPt:l)
		call P_TrackCursor,in=(!NewPt:l)

		rcall P_Local2PL,in=(NewPt:ax),out=(NewPt:ax)
		movelong NewPt,RealPt


		in #P_MOVEMENT:w,AnchorPt:l,!NewPt:l,P_PrintHand:l
		xcall P_Constrain

		subword NewPt,AnchorPt,VDiff
		subword NewPt+2,AnchorPt+2,HDiff


; Change Q_Rect
		addword VDiff,Q_Rect,TRect
		addword VDiff,Q_Rect+4,TRect+4
		addword HDiff,Q_Rect+2,TRect+2
		addword HDiff,Q_Rect+6,TRect+6

		call P_MagMove,in=(!TRect:l)
		subword TRect,Q_Rect,VDiff
		subword TRect+2,Q_Rect+2,HDiff


		call P_DrawFigure,in=(ObjType:w,TRect:l,TRect+4:l)


		cmpw ObjCount,#2
		blt NoMultDraw

; Change MultRect
		addword VDiff,MultRect,TMultRect
		addword VDiff,MultRect+4,TMultRect+4
		addword HDiff,MultRect+2,TMultRect+2
		addword HDiff,MultRect+6,TMultRect+6

		call D_Set4Pat,in=(#RightGray:w)
		call P_DrawFigure,in=(#P_RECTANGLE:w,TMultRect:l,TMultRect+4:l)
		call D_Set4Pat,in=(#LeftGray:w)
NoMultDraw



		call P_WaitMouse,in=(RealPt:l),out=(a:w)
		beq EndWhile

		jsr EraseStuff
		brl WhileLoop
EndWhile


		jsr EraseStuff
		_PenNormal
		tool _HUnlock,in=(ObjHdl:l)


		call P_BuildUndo,in=(#P_MoveUndo:w)
		tool _NewRgn,out=(UpdateRgn:l)
		call P_ListOp,in=(P_ObjList:l,#PL_AddUpdate:w,UpdateRgn:l,#0:l)
		call P_ListOp,in=(P_ObjList:l,#PL_OffsetObj:w,HDiff:w,VDiff:w,#0:l)
		call P_ListOp,in=(P_ObjList:l,#PL_AddUpdate:w,UpdateRgn:l,#0:l)
		call P_DrawRgn,in=(UpdateRgn:l)

Exit	RETURN



EraseStuff
		call P_DrawFigure,in=(ObjType:w,TRect:l,TRect+4:l)

		cmpw ObjCount,#2
		blt NoMultErase

		call D_Set4Pat,in=(#RightGray:w)
		call P_DrawFigure,in=(#P_RECTANGLE:w,TMultRect:l,TMultRect+4:l)
		call D_Set4Pat,in=(#LeftGray:w)
NoMultErase
		rts

		ENDP





;------------------------------------------------------------------------------
;
; P_StretchObj (ObjHdl:l; HandleNo:w; StartPt:l; ModFlags:w)
;

P_StretchObj	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData
			;Using T_TextEqu

		INPUT ObjHdl:l,HandleNo:w,StartPt:l,ModFlags:w
		LOCAL ObjPtr:l,Q_Rect:r,NRect:r,NewPt:l,Swapped:w
		LOCAL ObjType:w,HDiff:w,VDiff:w,UpdateRgn:l
		LOCAL MousePt:l,AnchorPt:l

		BEGIN

		call P_WaitMouse,in=(StartPt:l),out=(a:w)
		jeq Exit

		rcall D_Deref,in=(ObjHdl:ax),out=(ObjPtr:ax)
		moveword [ObjPtr]:#P_Type,ObjType
		moverect [ObjPtr]:#P_ObjRect,Q_Rect


		moveword [ObjPtr]:#P_Selected,a
		beq NotSelected
		call P_DrawHandles,in=(ObjPtr:l)
NotSelected
		moveword #1,[ObjPtr]:#P_Selected



		tool _SetPenMode,in=(#notXOR:w)
		tool _SetPenSize,in=(#2:w,#1:w)
		call D_Set4Pat,in=(#LeftGray:w)

		movelong StartPt,AnchorPt	 ; bogus; should never have 0 handle

		lda HandleNo
		asl a
		tax
		jmp (AnchorTable,x)

AnchorTable	; ; Which handle?
	DC.W	Nothing
	DC.W	TopLeftA
	DC.W	BottomRightA
	DC.W	BottomLeftA
	DC.W	TopRightA

TopLeftA	movelong Q_Rect+4,AnchorPt
		movelong Q_Rect,StartPt
		bra Nothing
BottomRightA	movelong Q_Rect,AnchorPt
		movelong Q_Rect+4,StartPt
		bra Nothing
BottomLeftA	moveword Q_Rect+6,AnchorPt+2
		moveword Q_Rect,AnchorPt
		moveword Q_Rect+2,StartPt+2
		moveword Q_Rect+4,StartPt
		bra Nothing
TopRightA	moveword Q_Rect+2,AnchorPt+2
		moveword Q_Rect+4,AnchorPt
		moveword Q_Rect+6,StartPt+2
		moveword Q_Rect,StartPt
Nothing

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

		; constrain to Q_square.
		in ObjType:w,AnchorPt:l,!NewPt:l,P_PrintHand:l
		xcall P_Constrain

		; get to any guides.
		call P_MagPt,in=(!NewPt:l,!AnchorPt:l,ObjType:w)

		; keep it Q_square.
		in ObjType:w,AnchorPt:l,!NewPt:l,P_PrintHand:l
		xcall P_Constrain

		subword NewPt,StartPt,VDiff
		subword NewPt+2,StartPt+2,HDiff


; Draw It
		moverect Q_Rect,NRect
		jsr StretchRect

		call P_DrawFigure,in=(ObjType:w,NRect:l,NRect+4:l)
		call P_WaitMouse,in=(MousePt:l),out=(a:w)
		beq EndWhile

; Erase It
		call P_DrawFigure,in=(ObjType:w,NRect:l,NRect+4:l)
		brl WhileLoop
EndWhile


		_PenNormal


; Handle pictures

		cmpw ObjType,#P_PICTURE
		bne Resize
		lda ModFlags
		and #optionKey
		beq StretchPict

; Set crop undo
		call P_BuildUndo,in=(#P_CropUndo:w)
		bra FixObjRect


; Map the Pict Q_Rect on a stretch

StretchPict
		call P_BuildUndo,in=(#P_StretchUndo:w)

		addlong ObjPtr,#P_PictRect,s
		pushlong !Q_Rect
		pushlong !NRect
		_MapRect
		bra FixObjRect


Resize
		call P_BuildUndo,in=(#P_StretchUndo:w)



; Accumulate old and D_New objects into the D_Update Q_Region

FixObjRect
		tool _NewRgn,out=(UpdateRgn:l)
		call P_AddObjRgn,in=(ObjPtr:l,UpdateRgn:l)
		moverect NRect,[ObjPtr]:#P_ObjRect
		call P_FixSpecRect,in=(ObjPtr:l)
		call P_AddObjRgn,in=(ObjPtr:l,UpdateRgn:l)
		tool _HUnlock,in=(ObjHdl:l)


; Handles Q_Text objects

		cmpw ObjType,#P_Alpha
		bne NotAlpha

		call P_AddThreadRgn,in=(ObjHdl:l,UpdateRgn:l)
		call T_ReThread,in=(ObjHdl:l)
NotAlpha

		call P_DrawRgn,in=(UpdateRgn:l)


Exit	RETURN


StretchRect
		lda HandleNo
		asl a
		tax

		jmp (JumpTable,x)

JumpTable	; ;order determined by FindObject
	DC.W	RectSet
	DC.W	TopLeft
	DC.W	BottomRight
	DC.W	BottomLeft
	DC.W	TopRight

TopLeft
		addword NRect,VDiff,NRect
		addword NRect+2,HDiff,NRect+2
		bra RectSet

BottomRight
		addword NRect+4,VDiff,NRect+4
		addword NRect+6,HDiff,NRect+6
		bra RectSet

BottomLeft
		addword NRect+2,HDiff,NRect+2
		addword NRect+4,VDiff,NRect+4
		bra RectSet

TopRight
		addword NRect,VDiff,NRect
		addword NRect+6,HDiff,NRect+6

RectSet
		call P_FixRect,in=(!NRect:l,ObjType:w,#0:w,#0:w)
		rts

		ENDP




;------------------------------------------------------------------------------
;
; P_WaitMouse (Pt:l) : StillDown:w
;
; Waits until the mouse button is released or it changes position.

P_WaitMouse	PROC		EXPORT

		INPUT  Pt:l
		OUTPUT StillDown:w
		LOCAL  NewPt:l

		BEGIN

		stz StillDown

Loop
		tool _StillDown,in=(#0:w),out=(a:w)
		beq Exit

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

		cmpl NewPt,Pt
		beq Loop
		inc StillDown

Exit	RETURN
		ENDP
		END

