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

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

	IMPORT	P_PIconRect
	IMPORT	P_IconPalette
	IMPORT	P_GrowRect

	IMPORT	P_ActionProc
	IMPORT	P_ContentRect
	IMPORT	P_CurrentWin
	IMPORT	P_DblPage
	IMPORT	P_DeselectAll
	IMPORT	P_DrawHandles
	IMPORT	P_Entry
	IMPORT	P_FindObject
	IMPORT	P_FixMenus
	IMPORT	P_FixRect
	IMPORT	P_GrewDoc
	IMPORT	P_HRulRect
	IMPORT	P_ImportHdl
	IMPORT	P_ImportType
	IMPORT	P_KillUndo
	IMPORT	P_Local2PL
	IMPORT	P_MDInTools
	IMPORT	P_MakeFigure
	IMPORT	P_MakeObj
	IMPORT	P_MoveGuide
	IMPORT	P_MoveObject
	IMPORT	P_MoveTabs
	IMPORT	P_NewGuide
	IMPORT	P_NewTool
	IMPORT	P_ObjList
	IMPORT	P_Page
	IMPORT	P_PageMode
	IMPORT	P_PastePt
	IMPORT	P_Placing
	IMPORT	P_PortRect
	IMPORT	P_ScrollPages
	IMPORT	P_SelectRect
	IMPORT	P_StretchObj
	IMPORT	P_ThreadDir
	IMPORT	P_ThreadHdl
	IMPORT	P_ToolType
	IMPORT	P_TrackCursor
	IMPORT	P_VRulRect
	IMPORT	P_VisPalette
	IMPORT	P_VisRuler
	IMPORT	T_Activate
	IMPORT	T_Click
	IMPORT	T_CurParHdl
	IMPORT	T_Deactivate
	IMPORT	T_DotObj
	IMPORT	T_Editing

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

	ENTRY	P_FindTextObj
	ENTRY	P_MouseContent


;------------------------------------------------------------------------------
;
; P_MouseDown (Click:w,TaskRec:l,CLickPt2:l,ClickPt3:l)
;
; Click - the number of the click 1 - single click, 2 for double, 3 for triple
; TaskRec - a pointer to an Event Record for you to look at.
; ClickPt2 - the Q_Point(global) where the first click of this sequence was
;
; Single click (click=1) look at the TaskRec as before, ClickPts are undefined.
; Double click (click=2) look at the pt in the TaskRec(global) and see if it
; is D_Close enough to ClickPt2(global) to be a double click. ClickPt3 is undefin
; Triple click (click=3) check that ClickPt3 is D_Close enough to ClickPt2
; and the pt in TaskRec.
; If you don't support a type of click, or it isn't D_Close enough together
; just do the same thing you would do for single clicking

P_MouseDown	PROC		EXPORT
			;Using P_IconData
			;Using P_Data
			;Using P_ObjData
			;Using P_RulerData
			;Using T_Data
			;Using T_TextEqu

		INPUT Click:w,TaskRec:l,ClickPt2:l,ClickPt3:l
		LOCAL ThePt:l,tmprect:r,tmpctl:l

		BEGIN +b


		cpzl P_CurrentWin
		jeq Exit		   ; Just in case


		jsl P_Entry
		movelong [TaskRec]:#owhere,ThePt
		tool _GlobalToLocal,in=(!ThePt:l)


CkContent
		tool _PtInRect,in=(!ThePt:l,#P_ContentRect:l),out=(a:w)
		beq CkPages
		call P_MouseContent,in=(TaskRec:l,click:w)
		brl Exit

CkPages
		tool _ClipRect,in=(#P_PortRect:l)
		tool _PtInRect,in=(!ThePt:l,#P_PIconRect:l),out=(a:w)
		beq CkTools
		call P_ScrollPages,in=(ThePt+2:w)
		brl SetClip

CkTools
		lda P_VisPalette
		beq CkControls

		tool _PtInRect,in=(!ThePt:l,#P_IconPalette:l),out=(a:w)
		beq CkControls
		call P_MDInTools,in=(ThePt:l)
		brl SetClip


CkControls
		in !tmpctl:l,[TaskRec]:#owhere:l,P_CurrentWin:l
		out a:w
		xtool _FindControl
		beq CkGrow

		in [TaskRec]:#owhere:l,#P_ActionProc:l,tmpctl:l
		out a:w
		xtool _TrackControl
		brl SetClip


CkGrow
		tool _PtInRect,in=(!ThePt:l,#P_GrowRect:l),out=(a:w)
		beq CkTabs

		spacelong
		addword #11,P_DblPage,a
		asl a
		asl a
		asl a
		asl a
		asl a
		pushword a
		pushword #P_MinHeight
		pushlong [TaskRec]:#owhere
		pushlong P_CurrentWin
		xtool _GrowWindow

		lda 1,s
		bne CleanUp

		lda 3,s
		bne CleanUp

		pla
		pla
		brl SetClip

CleanUp	lda 3,s
		and #$FFFE  ; Q_Mask odd bit off.
		sta 3,s
		tool _SizeWindow,in=(P_CurrentWin:l)

		call P_GrewDoc,in=(P_CurrentWin:l)
		brl SetClip


CkTabs
		lda P_VisRuler
		jeq SetClip


; Check if in tab area

		moverect P_HRulRect,tmprect
		moveword #10,tmprect
		tool _PtInRect,in=(!ThePt:l,!tmprect:l),out=(a:w)
		bne DoTabs

; Check if in tab well

		moveword P_HRulRect,tmprect
		moveword P_VRulRect+2,tmprect+2
		movelong P_ContentRect,tmprect+4
		tool _PtInRect,in=(!ThePt:l,!tmprect:l),out=(a:w)
		beq CkHGuide


DoTabs
		lda T_Editing
		beq CkVGuide

		call P_MoveTabs,in=(ThePt:l),out=(a:w)
		brl SetClip


CkHGuide
		tool _PtInRect,in=(!ThePt:l,#P_HRulRect:l),out=(a:w)
		beq CkVGuide

		call P_TrackCursor,in=(!ThePt:l)
		call P_NewGuide,in=(#0:w)
		brl SetClip


CkVGuide
		tool _PtInRect,in=(!ThePt:l,#P_VRulRect:l),out=(a:w)
		beq SetClip

		call P_TrackCursor,in=(!ThePt:l)
		call P_NewGuide,in=(#1:w)


SetClip
		tool _ClipRect,in=(#P_ContentRect:l)

Exit
		call P_FixMenus

		RETURN
		ENDP




;---------------------------------------------------------------------------
;
; P_MouseUp	  
;

P_MouseUp	PROC		EXPORT
		input TaskRec:l
		begin


		return
		ENDP




;--------------------------------------------------------------------------
;
; P_MouseContent (TaskRecPtr:l,Click:w)
;

P_MouseContent	PROC		EXPORT
			;Using P_ObjData
			;Using P_Data
			;Using T_Data

		INPUT TaskRecPtr:l,Click:w
		LOCAL ObjHdl:l,ObjPtr:l,ScrPt:l,Pt:l
		LOCAL ObjType:w,ModFlags:w,Q_Rect:r,Handle:w,Flag:w

		BEGIN

		movelong [TaskRecPtr]:#10,ScrPt
		moveword [TaskRecPtr]:#14,ModFlags

		tool _GlobalToLocal,in=(!ScrPt:l)
		rcall P_Local2PL,in=(ScrPt:ax),out=(Pt:ax)
		movelong Pt,P_PastePt


		lda P_Placing
		beq NotPlacing

		lda P_ImportType
		bne PlacePict
		moveword #P_ALPHA,ObjType
		bra DoPlace
PlacePict	moveword #P_PICTURE,ObjType

DoPlace
		call P_MakeObj,in=(ObjType:w,Pt:l,P_ImportHdl:l,#0:w)
		call P_NewTool,in=(#P_ARROW:w,#0:w)
		stzl P_ImportHdl
		stz P_Placing
		brl Exit



NotPlacing
		lda P_ToolType
		cmp #P_Arrow
		beq DoArrow
		cmp #P_Alpha
		jeq DoAlpha


;-------------------------------------------------------------------------------

NewObject
		call P_MakeObj,in=(P_ToolType:w,Pt:l,#0:l,#0:w)
		brl Exit


;-------------------------------------------------------------------------------



DoArrow
		call P_FindObject,in=(P_ObjList:l,ScrPt:l),out=(ObjHdl:l,Handle:w)
		cpzl ObjHdl
		bne Hit


; Check guides here

		call P_MoveGuide,in=(Pt:l),out=(a:w)
		bne ToExit


; Multiple selection code

		call P_MakeFigure,in=(#0:w,Pt:l),out=(Q_Rect+4:l)
		movelong Pt,Q_Rect

		lda ModFlags
		and #shiftKey
		bne ShiftDown
		jsl P_DeselectAll
ShiftDown

		call P_SelectRect,in=(Q_Rect+4:l,Q_Rect:l)
ToExit	brl Exit



Hit
		lda Handle
		bne InHandle

		movelong [ObjHdl],ObjPtr
		lda ModFlags
		and #shiftKey
		bne ShiftDown2

; Shift is up
		moveword [ObjPtr]:#P_Selected,a
		bne DoMove

		jsl P_DeselectAll
		call P_DrawHandles,in=(ObjPtr:l)
		bra DoMove

ShiftDown2
		ldy #P_Selected
		moveword [ObjPtr]:y,a
		eor #1
		moveword a,[ObjPtr]:y
		call P_DrawHandles,in=(ObjPtr:l)
		moveword [ObjPtr]:#P_Selected,a
		beq ToExit2

DoMove	moveword #1,[ObjPtr]:#P_Selected
		call P_MoveObject,in=(ObjHdl:l,Pt:l)
ToExit2	brl Exit



InHandle
		cmpw Handle,#5
		blt NotThread
		jsl P_KillUndo
		inc P_Placing
		stz P_ImportType
		movelong ObjHdl,P_ThreadHdl
		movelong [ObjHdl],ObjPtr
		movelong [ObjPtr]:#P_ParagHdl,P_ImportHdl
		subword Handle,#5,P_ThreadDir
		brl Exit

NotThread
		jsl P_DeselectAll
		call P_StretchObj,in=(ObjHdl:l,Handle:w,Pt:l,ModFlags:w)
		brl Exit



;-------------------------------------------------------------------------------


DoAlpha
		lda T_Editing
		beq NotSame

		movelong T_DotObj,ObjHdl
		movelong [ObjHdl],ObjPtr
		cmpw [ObjPtr]:#P_PageNo,P_Page
		bne NotSame
		moverect [ObjPtr]:#P_ObjRect,Q_Rect
		call P_FixRect,in=(!Q_Rect:l,#P_RECTANGLE:w,P_PageMode:w,#0:w)
		tool _PtInRect,in=(!Pt:l,!Q_Rect:l),out=(a:w)
		bne AlphaClick
NotSame

		call P_FindTextObj,in=(Pt:l),out=(ObjHdl:l)
		cpzl ObjHdl
		bne AlphaClick

		moveword T_Editing,Flag
		call T_Deactivate,in=(#1:w,#1:w)
		call P_MakeObj,in=(P_ToolType:w,Pt:l,#0:l,Flag:w)
		bra Exit


AlphaClick
		lda P_PageMode
		bne Exit

		movelong [ObjHdl],ObjPtr
		moveword [ObjPtr]:#P_StartParag,a
		bmi Exit

		cmpl [ObjPtr]:#P_ParagHdl,T_CurParHdl
		bne D_Activate

		call T_Click,in=(ObjHdl:l,Pt:l,ModFlags:w,Click:w)
		bra Exit

D_Activate
		call T_Deactivate,in=(#1:w,#1:w)
		call T_Activate,in=(ObjHdl:l,Pt:l),out=(a:w)


Exit	RETURN
		ENDP





;------------------------------------------------------------------------------
;
; P_FindTextObj (Pt:l) : ObjHdl:l
;

P_FindTextObj	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData

		INPUT  Pt:l
		OUTPUT ObjHdl:l
		LOCAL  ObjPtr:l,Q_Rect:r

		BEGIN

		lda P_PageMode
		beq NormalMode
		addword Pt,P_ContentRect,Pt
		addword Pt+2,P_ContentRect+2,Pt+2
NormalMode

		movelong P_ObjList,ObjHdl

While	cpzl ObjHdl
		jeq Exit
		movelong [ObjHdl],ObjPtr
		moveword [ObjPtr]:#P_Deleted,a
		bne TryNext
		cmpw [ObjPtr]:#P_Type,#P_ALPHA
		bne TryNext

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

		tool _PtInRect,in=(!Pt:l,!Q_Rect:l),out=(a:w)
		bne Exit
		movelong [ObjHdl],ObjPtr

TryNext	movelong [ObjPtr]:#P_Next,ObjHdl
		brl While


Exit	RETURN
		ENDP
		END

