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

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

	IMPORT	D_Deref
	IMPORT	D_LoadSegment
	IMPORT	Q_ManglePict
	IMPORT	Q_ManglePoint
	IMPORT	D_MemoryError
	IMPORT	P_AddObjRgn
	IMPORT	P_BoundsRect
	IMPORT	P_BuildUndo
	IMPORT	P_DrawRgn
	IMPORT	P_FixRect
	IMPORT	P_FixSpecRect
	IMPORT	P_InsertObj
	IMPORT	P_LastObj
	IMPORT	P_ObjList
	IMPORT	P_PrintHand
	IMPORT	P_RemoveObj
	IMPORT	T_ReThread
	IMPORT	D_UnloadSegment

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

	ENTRY	P_DoRotate
	ENTRY	P_RotateRect





;-------------------------------------------------------------------------
;
; P_BringFront ()
;

P_BringFront	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData

		local ObjHdl:l,PlaceHdl:l,ObjPtr:l,NextObj:l,UpdateRgn:l

		begin +b

		call P_BuildUndo,in=(#P_NoUndo:w)
		tool _NewRgn,out=(UpdateRgn:l)

		movelong P_ObjList,ObjHdl
		stzl PlaceHdl


Loop	cpzl ObjHdl
		beq EndLoop

		movelong [ObjHdl],ObjPtr
		movelong [ObjPtr],NextObj
		moveword [ObjPtr]:#P_Selected,a
		beq NoChange

		tool _HLock,in=(ObjHdl:l)
		call P_AddObjRgn,in=(ObjPtr:l,UpdateRgn:l)
		tool _HUnlock,in=(ObjHdl:l)

		call P_RemoveObj,in=(ObjHdl:l)
		call P_InsertObj,in=(ObjHdl:l,PlaceHdl:l)

		movelong ObjHdl,PlaceHdl
NoChange

		movelong NextObj,ObjHdl
		bra Loop
EndLoop

		call P_DrawRgn,in=(UpdateRgn:l)

		return
		ENDP




;-------------------------------------------------------------------------
;
; P_SendBack ()

P_SendBack	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData

		local ObjHdl:l,ObjPtr:l,NextObj:l,UpdateRgn:l,LastObj:l

		begin +b

		call P_BuildUndo,in=(#P_NoUndo:w)
		tool _NewRgn,out=(UpdateRgn:l)

		movelong P_ObjList,ObjHdl
		movelong P_LastObj,LastObj

		cpzl ObjHdl
		beq EndLoop

Loop	movelong [ObjHdl],ObjPtr
		movelong [ObjPtr],NextObj
		moveword [ObjPtr]:#P_Selected,a
		beq NoChange

		tool _HLock,in=(ObjHdl:l)
		call P_AddObjRgn,in=(ObjPtr:l,UpdateRgn:l)
		call P_RemoveObj,in=(ObjHdl:l)
		call P_InsertObj,in=(ObjHdl:l,P_LastObj:l)

NoChange
		cmpl ObjHdl,LastObj
		beq EndLoop
		movelong NextObj,ObjHdl
		bra Loop
EndLoop

		call P_DrawRgn,in=(UpdateRgn:l)

Exit	return
		ENDP





;---------------------------------------------------------------------------
;
; P_FlipHoriz ()
;

P_FlipHoriz	PROC		EXPORT
		begin

		call P_DoRotate,in=(#0:w)

		return
		ENDP



;---------------------------------------------------------------------------
;
; P_FlipVert ()
;

P_FlipVert	PROC		EXPORT
		begin

		call P_DoRotate,in=(#1:w)

		return
		ENDP



;---------------------------------------------------------------------------
;
; P_RotateLeft 
;

P_RotateLeft	PROC		EXPORT
		begin

		call P_DoRotate,in=(#3:w)

		return
		ENDP



;---------------------------------------------------------------------------
;
; P_RotateRight 
;

P_RotateRight	PROC		EXPORT
		begin

		call P_DoRotate,in=(#2:w)

		return
		ENDP



;------------------------------------------------------------------------------
;
; P_DoRotate (Verb:w)
;

P_DoRotate	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData

		input Verb:w

		local ObjHdl:l,ObjPtr:l,UpdateRgn:l,UnionRect:r
		local PrintHdl:l,PrintPtr:l,Scale:l,ObjType:w
		local PictHdl1:l,PictHdl2:l

		begin +b

		call D_LoadSegment,in=(#segPict:w,#4:w),out=(a:l)
		jcs _MemErr

		call P_BuildUndo,in=(#P_NoUndo:w)
		movelong P_PrintHand,PrintHdl
		movelong [PrintHdl],PrintPtr
		tool _FixRatio,in=([PrintPtr]:#6:w,[PrintPtr]:#4:w),out=(Scale:l)


		movelong P_ObjList,ObjHdl

		call P_BoundsRect,in=(P_ObjList:l,!UnionRect:l),out=(a:w)
		tool _NewRgn,out=(UpdateRgn:l)


		movelong P_ObjList,ObjHdl

Loop	cpzl ObjHdl
		jeq EndLoop

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

		moveword [ObjPtr]:#P_Selected,a
		jeq NextObj

		call P_AddObjRgn,in=(ObjPtr:l,UpdateRgn:l)
		moveword [ObjPtr]:#P_Type,ObjType
		cmp #P_PICTURE
		jne NotPict

		movelong [ObjPtr]:#P_PictHandle,PictHdl1
		call Q_ManglePict,in=(PictHdl1:l,Verb:w),out=(PictHdl2:l)
		bcc NoErr
		jsl D_MemoryError
		brl NextObj
NoErr

		tool _KillPicture,in=(PictHdl1:l)
		movelong PictHdl2,[ObjPtr]:#P_PictHandle

		addlong ObjPtr,#P_PictRect,s
		pushlong !UnionRect
		pushword Verb
		pushword ObjType
		jsl P_RotateRect
NotPict


		addlong ObjPtr,#P_ObjRect,s
		pushlong !UnionRect
		pushword Verb
		pushword ObjType
		jsl P_RotateRect

		call P_FixSpecRect,in=(ObjPtr:l)

		cmpw ObjType,#P_ALPHA
		bne NotAlpha
		call T_ReThread,in=(ObjHdl:l)
NotAlpha

		call P_AddObjRgn,in=(ObjPtr:l,UpdateRgn:l)


NextObj	tool _HUnlock,in=(ObjHdl:l)
		movelong [ObjPtr],ObjHdl
		brl Loop
EndLoop

		call D_UnloadSegment,in=(#segPict:w,#4:w)
		call P_DrawRgn,in=(UpdateRgn:l)


Exit	return

_MemErr
		jsl D_MemoryError
		bra Exit

		ENDP




;------------------------------------------------------------------------------
;
; P_RotateRect (RectPtr:l,RectPtr2:l,Opcode:w)
;

P_RotateRect	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData

		input RectPtr:l,RectPtr2:l,Opcode:w,ObjType:w

		begin

		in [RectPtr]:l,RectPtr2:l,OpCode:w,P_PrintHand:l
		out [RectPtr]:l
		xcall Q_ManglePoint

		in [RectPtr]:#4:l,RectPtr2:l,OpCode:w,P_PrintHand:l
		out [RectPtr]:#4:l
		xcall Q_ManglePoint

		call P_FixRect,in=(RectPtr:l,ObjType:w,#0:w,#0:w)

		return
		ENDP
		END

