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


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

	IMPORT	P_PageRect
	IMPORT	P_FullPageRect

	IMPORT	D_Deref
	IMPORT	D_DrawPicture
	IMPORT	D_MemoryError
	IMPORT	D_NeedHand
	IMPORT	P_AddThreadRgn
	IMPORT	P_AppleFuck
	IMPORT	P_BuildUndo
	IMPORT	P_DefColor
	IMPORT	P_DefFont
	IMPORT	P_DefJust
	IMPORT	P_DefSize
	IMPORT	P_DefStyle
	IMPORT	P_DrawHandles
	IMPORT	P_DrawRgn
	IMPORT	P_DrawSpec
	IMPORT	P_ExplodeRect
	IMPORT	P_FillMode
	IMPORT	P_FixRect
	IMPORT	P_FixSpecRect
	IMPORT	P_GetPictSize
	IMPORT	P_LastObj
	IMPORT	P_LineAdjust
	IMPORT	P_LineSize
	IMPORT	P_ListOp
	IMPORT	P_MagGuides
	IMPORT	P_MagValue
	IMPORT	P_MakeFigure
	IMPORT	P_NewClip
	IMPORT	P_ObjList
	IMPORT	P_OldClip
	IMPORT	P_Page
	IMPORT	P_PageMode
	IMPORT	P_SetDelFlag
	IMPORT	P_SetPen
	IMPORT	P_SetSpecStr
	IMPORT	P_SmallRect
	IMPORT	P_SpecStr
	IMPORT	P_ValidRect
	IMPORT	D_RectSect
	IMPORT	D_SectClipRect
	IMPORT	D_SetCursor
	IMPORT	T_Activate
	IMPORT	T_CurParHdl
	IMPORT	T_Editing
	IMPORT	T_New
	IMPORT	T_Update
	IMPORT	P_GetModifiers

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

	ENTRY	P_NewObj
	ENTRY	P_ObjSizes


		include 'pl.equ'



;------------------------------------------------------------------------------
;
; P_InsertObj (ObjHdl:l,PlaceHdl:l)
;
; Inserts the object after PlaceHdl.

P_InsertObj	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData

		INPUT ObjHdl:l,PlaceHdl:l
		LOCAL ObjPtr:l,NextObj:l,NextPtr:l,PlacePtr:l

		BEGIN

		cmpl PlaceHdl,P_LastObj
		bne NotLast
		movelong ObjHdl,P_LastObj
NotLast

		movelong [ObjHdl],ObjPtr
		movelong PlaceHdl,[ObjPtr]:#P_Prev
		cpzl PlaceHdl
		bne NotFirst
		movelong P_ObjList,[ObjPtr]:#P_Next
		movelong P_ObjList,NextObj
		movelong ObjHdl,P_ObjList
		cpzl NextObj
		beq Done
		movelong [NextObj],NextPtr
		movelong ObjHdl,[NextPtr]:#P_Prev
		bra Done

NotFirst
		movelong [PlaceHdl],PlacePtr
		movelong [PlacePtr],NextObj
		movelong ObjHdl,[PlacePtr]:#P_Next
		movelong NextObj,[ObjPtr]:#P_Next
		cpzl NextObj
		beq Done
		movelong [NextObj],NextPtr
		movelong ObjHdl,[NextPtr]:#P_Prev

Done
		RETURN
		ENDP




;-----------------------------------------------------------------------------
;
; P_RemoveObj (ObjHdl:l)
;

P_RemoveObj	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData

		INPUT ObjHdl:l
		LOCAL ObjPtr:l,NextObj:l,PrevObj:l,Temp:l

		BEGIN

		movelong [ObjHdl],ObjPtr
		movelong [ObjPtr],NextObj
		movelong [ObjPtr]:#P_Prev,PrevObj

DoPrev	cpzl PrevObj
		bne Normal
		movelong NextObj,P_ObjList
		bra DoNext
Normal	movelong [PrevObj],Temp
		movelong NextObj,[Temp]


DoNext	cpzl NextObj
		bne Normal2
		movelong PrevObj,P_LastObj
		bra Exit
Normal2	movelong [NextObj],Temp
		movelong PrevObj,[Temp]:#P_Prev

Exit	RETURN
		ENDP




;------------------------------------------------------------------------------
; P_DeselectAll ()
;

P_DeselectAll	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData

		BEGIN

		call P_ListOp,in=(P_ObjList:l,#PL_DeselectAll:w,#0:l,#0:l)

		RETURN
		ENDP




;------------------------------------------------------------------------------
;
; P_SelectRect (Q_Rect:r)
;
; Adds objects completely inside the rectangle to the selected
; list using eor.

P_SelectRect	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData

		INPUT Q_Rect:r

		BEGIN

		call P_ValidRect,in=(!Q_Rect:l),out=(a:w)
		call P_ListOp,in=(P_ObjList:l,#PL_SelRect:w,!Q_Rect:l,#0:l)

		RETURN
		ENDP





;-----------------------------------------------------------------------------
;
; P_DelObjects (Type:w)
;
; Marks all the selected objects as deleted and updates the screen.

P_DelObjects	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData

		INPUT Type:w
		LOCAL RgnHdl:l

		BEGIN

		tool _NewRgn,out=(RgnHdl:l)
		call P_ListOp,in=(P_ObjList:l,#PL_AddUpdate:w,RgnHdl:l,#0:l)
		call P_SetDelFlag,in=(#1:w)
		call P_DrawRgn,in=(RgnHdl:l)

Exit	RETURN
		ENDP





;------------------------------------------------------------------------------
;
; P_DrawObject (ObjHdl:l; Printing:w)
;

P_DrawObject	PROC		EXPORT
			;Using P_ObjData
			;Using P_Data

		INPUT ObjHdl:l,Printing:w
		LOCAL ObjPtr:l,ObjType:w,Q_Rect:r,PictRect:r
		LOCAL ClipRgn:l,PictHdl:l,PictPtr:l,Q_OvalSize:l
		ERROR err
		BEGIN

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

		call P_FixRect,in=(!Q_Rect:l,ObjType:w,P_PageMode:w,#1:w)


Draw	lda ObjType
		asl a
		tax
		jmp (JumpTable,x)

JumpTable	DC.W	Done	; should not happen
	DC.W	D_DrawText
	DC.W	DrawPage
	DC.W	DrawDate
	DC.W	DrawLine
	DC.W	DrawLine
	DC.W	D_DrawRect
	DC.W	DrawRRect
	DC.W	DrawOval
	DC.W	DrawPict


D_DrawText
		jsl P_AppleFuck
		call T_Update,in=(ObjHdl:l,Printing:w)
		brl Done


DrawPage
DrawDate
		jsl P_AppleFuck
		call P_SetSpecStr,in=(ObjPtr:l,#0:w)
		call P_DrawSpec,in=(ObjPtr:l)
		brl Done


DrawLine
		jsr PSetPen
		tool _MoveTo,in=(Q_Rect:l)
		tool _LineTo,in=(Q_Rect+4:l)
		brl Done


D_DrawRect
		jsr PSetPen
		pushlong !Q_Rect
		moveword [ObjPtr]:#P_ObjFilled,a
		bne ColorRect
		_FrameRect
		brl Done
ColorRect	_PaintRect
		brl Done


DrawRRect
		jsr PSetPen

		moveword #20,Q_OvalSize
		moveword #40,Q_OvalSize+2
		lda P_PageMode
		beq NoScale
		tool _ScalePt,in=(!Q_OvalSize:l,#P_PageRect:l,#P_FullPageRect:l)
NoScale

		pushlong !Q_Rect
		pushlong Q_OvalSize
		moveword [ObjPtr]:#P_ObjFilled,a
		bne ColorRRect
		_FrameRRect
		brl Done
ColorRRect	_PaintRRect
		brl Done


DrawOval
		jsr PSetPen
		pushlong !Q_Rect
		moveword [ObjPtr]:#P_ObjFilled,a
		bne ColorOval
		_FrameOval
		brl Done
ColorOval	_PaintOval
		brl Done


DrawPict
		call D_SetCursor,in=(#WatchCursor:w)

;			 D_Save and set the clip

		spacelong
		_NewRgn
		movelong 1:s,ClipRgn
		_GetClip

		call D_SectClipRect,in=(!Q_Rect:l)

		moverect [ObjPtr]:#P_PictRect,PictRect

		movelong [ObjPtr]:#P_PictHandle,PictHdl
		movelong [PictHdl],PictPtr
		moveword [PictPtr],a
		and #$80
		bne Not320
		addword PictRect+2,PictRect+6,a
		lsr a
		moveword a,PictRect+6
Not320

		call P_FixRect,in=(!PictRect:l,#P_RECTANGLE:w,P_PageMode:w,#1:w)

		call D_DrawPicture,in=(PictHdl:l,!PictRect:l),err=err

; restore the clip
		tool _SetClip,in=(ClipRgn:l)
		tool _DisposeHandle,in=(ClipRgn:l)



Done
		_PenNormal
		tool _HUnlock,in=(ObjHdl:l)

		RETURN

PSetPen
		call P_SetPen,in=([ObjPtr]:#P_PenSize:w,[ObjPtr]:#P_ObjColor:w)
		rts

		ENDP





;------------------------------------------------------------------------------
;
; P_DrawObjList (LastObj:l,RectPtr:l,Printing:w)
;

P_DrawObjList	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData
			;Using T_Data

		INPUT LastObj:l,RectPtr:l,Printing:w
		LOCAL ObjHdl:l,ObjPtr:l,ObjType:w,TempRect:r,Q_PenSize:w

		BEGIN


		movelong LastObj,ObjHdl

While1	cpzl ObjHdl
		jeq EndWhile1

		movelong [ObjHdl],ObjPtr
		moveword [ObjPtr]:#P_Type,ObjType
		moverect [ObjPtr]:#P_ObjRect,TempRect
		call P_LineAdjust,in=(ObjPtr:l,!TempRect:l)
		moveword [ObjPtr]:#P_Deleted,a
		bne NextObject

		lda T_Editing
		beq @NoEdit
		lda Printing
		bne @NoEdit

		cmpw ObjType,#P_ALPHA
		bne @NoEdit
		cmpl [ObjPtr]:#P_ParagHdl,T_CurParHdl
		bne @NoEdit

		tool _SetClipHandle,in=(P_OldClip:l)
		jsr DrawIt
		tool _SetClipHandle,in=(P_NewClip:l)
		bra NextObject
@NoEdit


		jsr DrawIt


NextObject	movelong [ObjHdl],ObjPtr
		movelong [ObjPtr]:#P_Prev,ObjHdl
		brl While1
EndWhile1




DrawHandles
		lda Printing
		bne Exit
		movelong LastObj,ObjHdl

While2
		cpzl ObjHdl
		beq EndWhile2

		movelong [ObjHdl],ObjPtr
		moveword [ObjPtr]:#P_Deleted,a
		bne NoDraw
		moveword [ObjPtr]:#P_Selected,a
		beq NoDraw
		call P_DrawHandles,in=(ObjPtr:l)
NoDraw

		movelong [ObjHdl],ObjPtr
		movelong [ObjPtr]:#P_Prev,ObjHdl
		bra While2
EndWhile2


Exit	RETURN


DrawIt
		call P_FixRect,in=(!TempRect:l,#P_RECTANGLE:w,P_PageMode:w,#1:w)
		call D_RectSect,in=(RectPtr:l,!TempRect:l),out=(a:w)
		beq No
		call P_DrawObject,in=(ObjHdl:l,Printing:w)
No	rts


		ENDP




;-----------------------------------------------------------------------------
;
; P_MakeObj (ObjType:w,Pt:l,DataHdl:l,Flags:w)
;
; Makes a D_New object after defining its rectangle.


P_MakeObj	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData

		INPUT ObjType:w,Pt:l,DataHdl:l,Flags:w
		LOCAL Q_Rect:r,ObjHdl:l,AnchorPt:l,mods:w

		BEGIN

		movelong Pt,AnchorPt

		lda ObjType
		beq NoMag
		lda P_MagGuides
		beq NoMag

		call P_MagValue,in=(AnchorPt:w,#1:w),out=(AnchorPt:w)
		call P_MagValue,in=(AnchorPt+2:w,#0:w),out=(AnchorPt+2:w)
NoMag

		call P_GetModifiers,out=(mods:w)
		call P_MakeFigure,in=(ObjType:w,AnchorPt:l),out=(Q_Rect+4:l)
		movelong AnchorPt,Q_Rect

;	if the option key is down
		lda mods
		and #optionKey
		beq @cont
		
;	and if it's a Pict
		cmpw ObjType,#P_PICTURE
		bne @cont

;	then double the vertical size (down the page).
		cmpl Q_Rect,Q_Rect+4
		bne @diff
		call P_GetPictSize,in=(DataHdl:l),out=(Q_Rect+4:l)
		addword Q_Rect,Q_Rect+4,Q_Rect+4
		addword Q_Rect+2,Q_Rect+6,Q_Rect+6
		
@diff	subword Q_Rect+4,Q_Rect,a
		asl a
		addword a,Q_Rect,Q_Rect+4

@cont	cmpw ObjType,#P_ALPHA
		bne NotAlpha

		call P_SmallRect,in=(!Q_Rect:l),out=(a:w)
		beq NotSmall
		lda Flags
		bne Exit
		call P_ExplodeRect,in=(Pt:l,!Q_Rect:l)
		bcs Exit
NotSmall
NotAlpha


		call P_NewObj,in=(ObjType:w,!Q_Rect:l,DataHdl:l),out=(ObjHdl:l)
		cpzl ObjHdl
		beq Exit

		cmpw ObjType,#P_ALPHA
		bne Exit
		cpzl DataHdl
		bne Exit

		call T_Activate,in=(ObjHdl:l,AnchorPt:l),out=(a:w)
		bne Exit
		call P_DrawObject,in=(ObjHdl:l,#0:w)

Exit	RETURN
		ENDP




;------------------------------------------------------------------------------
;
; P_NewObj (ObjType:w,RectPtr:l,DataHdl:l) : ObjHdl:l
;

P_NewObj	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData

		INPUT  ObjType:w,RectPtr:l,DataHdl:l
		OUTPUT ObjHdl:l

		LOCAL ObjPtr:l,ObjRectPtr:l,UpdateRgn:l
		LOCAL BRect:r,LCorner:l,Same:w

		BEGIN

		lda ObjType
		cmp #P_Line
		beq Valid
		cmp #P_STRLINE
		beq Valid
		call P_ValidRect,in=(RectPtr:l),out=(a:w)
Valid

		stz Same
		cmpl [RectPtr],[RectPtr]:#4
		bne Diff
		inc Same
Diff


		stzl ObjHdl

		lda ObjType
		dec a
		asl a
		asl a
		tax
		lda P_ObjSizes,x

		call D_NeedHand,in=(#0:w,a:w),out=(ObjHdl:l)
		bcc NoErr
		jsl D_MemoryError
		brl Exit
NoErr

		rcall D_Deref,in=(ObjHdl:ax),out=(ObjPtr:ax)
		addlong ObjPtr,#P_ObjRect,ObjRectPtr

		moveword ObjType,[ObjPtr]:#P_Type
		moverect [RectPtr],[ObjRectPtr]
		movelong P_ObjList,[ObjPtr]:#P_Next
		lda #0
		moveword a,[ObjPtr]:#P_Prev
		moveword a,[ObjPtr]:#P_Prev+2
		moveword a,[ObjPtr]:#P_Selected
		moveword a,[ObjPtr]:#P_Deleted
		moveword a,[ObjPtr]:#P_UndoData


		lda ObjType
		asl a
		tax
		jmp (JumpTable,x)

JumpTable
	DC.W	Exit
	DC.W	NewAlpha
	DC.W	NewSpecial
	DC.W	NewSpecial
	DC.W	NewGraphic
	DC.W	NewGraphic
	DC.W	NewGraphic
	DC.W	NewGraphic
	DC.W	NewGraphic
	DC.W	NewPict


NewAlpha
		call T_New,in=(ObjHdl:l,DataHdl:l)
		brl MadeObj



NewSpecial
		moveword P_DefFont,[ObjPtr]:#P_ItemFontID
		shortm
		moveword P_DefStyle,[ObjPtr]:#P_ItemFontID+2
		moveword P_DefSize,[ObjPtr]:#P_ItemFontID+3
		longm
		moveword P_DefColor,[ObjPtr]:#P_ItemColor
		moveword P_DefJust,[ObjPtr]:#P_ItemJust
		moveword P_Page,[ObjPtr]:#P_CopyPage

		call P_SetSpecStr,in=(ObjPtr:l,#1:w)
		tool _StringBounds,in=(#P_SpecStr:l,!BRect:l)

		subword BRect+4,BRect,a
		addword a,#2,a
		addword a,[ObjRectPtr],[ObjRectPtr]:#4
		subword BRect+6,BRect+2,a
		addword a,#2,a
		addword a,[ObjRectPtr]:#2,[ObjRectPtr]:#6
		brl MadeObj



NewGraphic
		lda Same
		jne KillObj
		moveword P_LineSize,[ObjPtr]:#P_PenSize
		moveword P_FillMode,[ObjPtr]:#P_ObjFilled
		moveword P_DefColor,[ObjPtr]:#P_ObjColor
		brl MadeObj


NewPict
		call P_SmallRect,in=(ObjRectPtr:l),out=(a:w)
		beq RectSet

		movelong [ObjRectPtr],LCorner
		movelong #0,[ObjRectPtr]
		call P_GetPictSize,in=(DataHdl:l),out=([ObjRectPtr]:#4:l)
		tool _OffsetRect,in=(ObjRectPtr:l,LCorner:l)

RectSet
		movelong DataHdl,[ObjPtr]:#P_PictHandle
		moverect [ObjRectPtr],[ObjPtr]:#P_PictRect
		bra MadeObj


KillObj
		tool _DisposeHandle,in=(ObjHdl:l)
		stzl ObjHdl
		brl Exit



MadeObj
		call P_FixSpecRect,in=(ObjPtr:l)
		tool _HUnlock,in=(ObjHdl:l)
		call P_BuildUndo,in=(#P_NoUndo:w)
		call P_InsertObj,in=(ObjHdl:l,#0:l)


		cmpw ObjType,#P_ALPHA
		bne NotText
		cpzl DataHdl
		beq Exit

		tool _NewRgn,out=(UpdateRgn:l)
		call P_AddThreadRgn,in=(ObjHdl:l,UpdateRgn:l)
		call P_DrawRgn,in=(UpdateRgn:l)
		bra Exit

NotText
		call P_DrawObject,in=(ObjHdl:l,#0:w)

Exit	RETURN
		ENDP





;-----------------------------------------------------------------------------
;
; This data segment contains declarations for object stuff.
;

P_ObjData	PROC		EXPORT
		EXPORT	P_OBJSIZES




; Types of tools and objects



P_OBJSIZES
	DC.L	P_TextObjSize
	DC.L	36
	DC.L	36
	DC.L	36
	DC.L	36
	DC.L	36
	DC.L	36
	DC.L	36
	DC.L	36



; Common Object Part




; Q_Text Object Part




; Lines, Q_Rects, Rounded Q_Rects, Ovals




; Page #, Date



; Pictures




; List Operations



; Object attribute changes



; Object handles



		ENDP
		END

