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

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

	IMPORT	X_CopyOneHandle
	IMPORT	D_Deref
	IMPORT	Local2TextObj
	IMPORT	P_ContentRect
	IMPORT	P_DrawATab
	IMPORT	P_HRulRect
	IMPORT	P_PL2Local
	IMPORT	P_PortRect
	IMPORT	P_Traverse
	IMPORT	P_VRulRect
	IMPORT	P_VisRuler
	IMPORT	D_Set4Pat
	IMPORT	T_ChangeRuls
	IMPORT	T_DotObj
	IMPORT	T_DotParag
	IMPORT	T_Editing
	IMPORT	T_GetParPtr
	IMPORT	TextObj2Local
	IMPORT	D_UnLock

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

	ENTRY	P_DragTab
	ENTRY	P_DrawTabwell




;---------------------------------------------------------------------------
;
; P_EraseTabs
;
;		   erase all of the tabs

P_EraseTabs	PROC		EXPORT
			;Using P_Data
			;Using P_RulerData

		local tmprect:r
		begin

		lda P_VisRuler
		beq Exit

		call D_Set4Pat,in=(#LightYellow:w)

		moveword #P_TabWBottom-6,tmprect
		moveword #P_TabWBottom-1,tmprect+4
		moveword P_ContentRect+2,tmprect+2
		moveword P_ContentRect+6,tmprect+6

		tool _ClipRect,in=(!tmprect:l)
		tool _PaintRect,in=(!tmprect:l)

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

Exit	RETURN
		ENDP




;---------------------------------------------------------------------------
;
; P_EraseTabwell 
;

P_EraseTabwell	PROC		EXPORT
			;Using P_Data
			;Using P_RulerData

		local tmprect:r
		begin

		lda P_VisRuler
		beq Exit

		call D_Set4Pat,in=(#LightYellow:w)

		moveword #P_TabWTop,tmprect
		moveword P_VRulRect+2,tmprect+2
		moveword #P_TabWBottom-1,tmprect+4
		subword P_VRulRect+6,#2,tmprect+6

		tool _ClipRect,in=(!tmprect:l)
		tool _PaintRect,in=(!tmprect:l)

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

Exit	RETURN
		ENDP




;---------------------------------------------------------------------------
;
; P_HideTabs (erase the Tabwell and any active tabs)
;

P_HideTabs	PROC		EXPORT

		call P_EraseTabwell
		call P_EraseTabs
		rtl

		ENDP





;---------------------------------------------------------------------------
;
; P_DrawTabs
;

P_DrawTabs	PROC		EXPORT
			;Using P_RulerData
			;Using P_Data
			;Using T_Data
			;Using T_TextEqu

		local count:w,ptr:l,offset:w,tmp:l,objhdl:l,hoffset:w
		local rulerhdl:l
		begin

		lda P_VisRuler
		jeq exit
		lda T_Editing
		jeq exit

		rcall T_GetParPtr,in=(T_DotParag:a),out=(ptr:ax)
		movelong [ptr]:#T_RulerHdl,RulerHdl

		rcall D_Deref,in=(RulerHdl:ax),out=(ptr:ax)

		cmpw [ptr]:#T_TabCount,#P_MaxTabcount
		bge DidTabwell

		call P_DrawTabwell

DidTabwell	tool _ClipRect,in=(#P_HRulRect:l)

		moveword [ptr]:#T_TabCount,count

		addwl #T_Indent,ptr

		stz offset
tabloop	pushlong #-1
		call TextObj2Local,in=([ptr]:offset:w,#0:w),out=(:w,y:w)
		pushword #P_HRulTop+P_HRulWdth-6
		inc offset
		inc offset
		pushword [ptr]:offset
		call P_DrawATab,in=(:l,:l,:w)

		addword offset,#2,offset
		dec count
		bpl tabloop

		tool _ClipRect,in=(#P_ContentRect:l)

		rcall D_UnLock,in=(RulerHdl:ax)

exit	return
		ENDP





;---------------------------------------------------------------------------
;
; P_DrawTabwell ()
;

P_DrawTabwell	PROC		EXPORT
			;Using P_RulerData
			;Using P_Data
			;Using T_Data
			;Using T_TextEqu

		begin

		tool _ClipRect,in=(#P_PortRect:l)

		; Draw the 3 in the Tabwell.

		addword P_VRulRect+2,#8,a
		and #$FFFE
		call P_DrawATab,in=(#-1:l,a:w,#P_TabWTop+5:w,#P_LeftTab:w)

		addword P_VRulRect+2,#18,a
		and #$FFFE
		call P_DrawATab,in=(#-1:l,a:w,#P_TabWTop+5:w,#P_DecimalTab:w)

		addword P_VRulRect+2,#28,a
		and #$FFFE
		call P_DrawATab,in=(#-1:l,a:w,#P_TabWTop+5:w,#P_RightTab:w)

		tool _ClipRect,in=(#P_ContentRect:l)

		RETURN
		ENDP




;---------------------------------------------------------------------------
;
; P_MoveTabs (Pt:l) : Moved:w
;
;		   pt is local

P_MoveTabs	PROC		EXPORT
			;Using T_Data
			;Using P_Data
			;Using T_TextEqu
			;Using P_ObjData
			;Using P_RulerData

		input ThePt:l
		output moved:w

		local ParagPtr:l,RulerHdl:l,RulerPtr:l
		local TabsHdl:l,TabsPtr:l,TabArray:l,TabType:w
		local tmprect:r,tabcount:w,tmp:l,ptr:l
		local tmptab:l,tmpptr:l,arrayhdl:l,hoffset:w
		local leftside:w,rightside:w,offset:w
		error err

		begin

		moverect P_ContentRect,tmprect
		moveword P_PortRect,tmprect
		moveword P_VRulRect+2,tmprect+2

		tool _ClipRect,in=(!tmprect:l)


		stz moved

		rcall T_GetParPtr,in=(T_DotParag:a),out=(ParagPtr:ax)
		movelong [ParagPtr]:#T_RulerHdl,RulerHdl
		call X_CopyOneHandle,in=(RulerHdl:l),out=(TabsHdl:l)
		rcall D_Deref,in=(TabsHdl:ax),out=(TabsPtr:ax)
		moveword [TabsPtr]:#T_TabCount,tabcount
		addlong #T_Indent,TabsPtr,TabArray


		call TextObj2Local,in=(#0:w,#0:w),out=(leftside:w,y:w)
		bpl DoRightSide

		moveword #0,leftside

DoRightSide

		movelong T_DotObj,tmp
		movelong [tmp],ptr

		rcall P_PL2Local,in=([ptr]:#P_RLeft:x)
		call Local2TextObj,in=(x:w,#0:w),out=(hoffset:w,a:w)

		rcall P_PL2Local,in=([ptr]:#P_RRight:x),out=(a:x)
		and #$FFFE
		sta rightside

		movelong !tabarray,arrayhdl



		; Check for D_MouseDown in tab-Q_Rect

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

		; See if mouse was down on a tab

		call Local2TextObj,in=(ThePt:l),out=(ThePt:l)

		subword ThePt+2,#4,tmp

		lda tabcount
		ina
		sta tmp+2
		in arrayhdl:l,tmp:w,#4:w,tmp+2:w
		out offset:w
		xcall P_Traverse

		lsr a
		lsr a
		cmp tmp+2
		jge Exit

		movelong [tabarray]:offset,tmptab

		addword #5,ThePt+2,a
		and #$FFFE
		cmp tmptab
		jlt Exit

		; Foundit!!
		lda offset
		bne DoNormTab

		lda tabcount
		beq CheckMax

		; use the first tab as the rightmost bounds.
		call TextObj2Local,in=([tabarray]:#4:w,#0:w),out=(a:w,y:w)
		subword a,#P_MinTabDist,rightside

CheckMax	cmpw P_ContentRect+6,rightside
		bge CheckMin
		sta rightside

CheckMin	cmpw P_ContentRect+2,leftside
		blt DoOldDrag
		sta leftside
		bra DoOldDrag

DoNormTab	addlong a,tabarray,ptr
		addlong ptr,#4,s
		pushlong ptr
		lda Tabcount
		asl a
		asl a
		pushword #0
		subword a,offset,s
		_BlockMove
		dec TabCount

		addword P_VRulRect+2,#2,leftside
		subword P_ContentRect+6,#2,rightside

DoOldDrag	call TextObj2Local,in=(tmptab:w,#0:w),out=(ThePt:l)
		moveword #P_TabWBottom-6,ThePt
		; Erase the old one...
		call P_DrawATab,in=(ThePt:l,#-1:l,#0:w)

		moveword tabcount,tmp

		lda offset
		not a

		in a:w,leftside:w,rightside:w,ThePt:l
		in tabarray:l,tabcount:w,tmptab+2:w
		out tabcount:w
		xcall P_DragTab


		lda tabcount
		cmp #P_MaxTabCount-1
		bne NoDraw
		cmp tmp
		bne NoDraw
		call P_DrawTabwell			  ; We've removed the 10th tab
NoDraw	brl DoStore



CkTabwell	cmpw tabcount,#P_MaxTabCount
		jge Exit

		addword P_VRulRect+2,#2,leftside
		subword P_ContentRect+6,#2,rightside

		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)
		jeq Exit

		moveword #2,tmprect
		moveword #11,tmprect+4

		addword P_VRulRect+2,#3,tmprect+2
		addword P_VRulRect+2,#13,tmprect+6
		tool _PtInRect,in=(!ThePt:l,!tmprect:l),out=(a:w)
		beq CkDec

		moveword #P_LeftTab,TabType
		bra DoDrag


CkDec	moveword tmprect+6,tmprect+2
		addword P_VRulRect+2,#23,tmprect+6
		tool _PtInRect,in=(!ThePt:l,!tmprect:l),out=(a:w)
		beq CkRight

		moveword #P_DecimalTab,TabType
		bra DoDrag


CkRight	moveword tmprect+6,tmprect+2
		addword P_VRulRect+2,#33,tmprect+6
		tool _PtInRect,in=(!ThePt:l,!tmprect:l),out=(a:w)
		beq Exit

		moveword #P_RightTab,TabType

DoDrag
		in #0:w,leftside:w,rightside:w,ThePt:l
		in tabarray:l,tabcount:w,TabType:w
		out a:w
		xcall P_DragTab

		cmp tabcount
		beq Exit
		sta tabcount


DoStore	moveword tabcount,[TabsPtr]:#T_Tabcount
		inc moved

		cmpw tabcount,#P_MaxTabcount
		blt StillThere
		call P_EraseTabwell
StillThere


		tool _ClipRect,in=(#P_ContentRect:l)

		movelong [RulerHdl],RulerPtr
		ldy #T_Indent
		cmpw [RulerPtr]:y,[TabsPtr]:y
		beq ChTabs
		call T_ChangeRuls,in=(#T_ChangeIndent:w,[TabsPtr]:#T_Indent:w,#0:l)
		bra Exit

ChTabs
		call T_ChangeRuls,in=(#T_ChangeTabs:w,#0:w,TabsHdl:l)


Exit
		tool _DisposeHandle,in=(TabsHdl:l)
		tool _ClipRect,in=(#P_ContentRect:l)

		return
		ENDP




;---------------------------------------------------------------------------
;
; P_DragTab   
;

P_DragTab	PROC		EXPORT
			;Using P_RulerData
			;Using P_Data
			;Using T_Data
			;Using T_TextEqu

		input izitindent:w,minhorz:w,maxhorz:w,oldpt:l
		input array:l,count:w,type:w
		output newcount:w

		local ThePt:l,TabPt:l,arrayhdl:l,tmprect:r
		local offset:w,ptr:l,tmp:l
		begin

		moveword count,newcount
		movelong !array,arrayhdl

		_HideCursor

		moveword #-50,oldpt

dragloop	tool _GetMouse,in=(!ThePt:l)
		lda ThePt+2
		bpl CheckMax
		lda #0

CheckMax	cmp maxhorz
		blt CheckMin
		moveword maxhorz,ThePt+2

CheckMin	cmp minhorz
		bge DoConvert
		moveword minhorz,ThePt+2

DoConvert	jsr FixPt
		call Local2TextObj,in=(ThePt:l),out=(TabPt:l)

		lda izitindent
		bne DrawIt

		cmpw #P_TabWBottom-6,ThePt			  ; Not in the ruler.
		bne DrawIt

		; Do nearness checking for other tabs, incl indent
		lda TabPt+2							 ; Not in the textobj
		bmi DrawIt

DoSearch	addword count,#1,tmp+2
		subword TabPt+2,#P_MinTabDist,tmp

		in arrayhdl:l,tmp:w,#4:w,tmp+2:w
		out offset:w
		xcall P_Traverse

		lsr a
		lsr a
		cmp tmp+2
		bge DrawIt

		subword [array]:offset,TabPt+2,a
		abs a
		cmp #P_MinTabDist
		bge DrawIt
		movelong oldpt,ThePt				; There's a tab to the right
		bra D_SkipIt

DrawIt	cmpl ThePt,oldpt
		beq ContLoop
		call P_DrawATab,in=(oldpt:l,ThePt:l,type:w)
ContLoop	movelong ThePt,oldpt
D_SkipIt	tool _StillDown,in=(#0:w),out=(a:w)
		jne dragloop

		call Local2TextObj,in=(ThePt:l),out=(TabPt:l)

		; Check to see if it's in a valid location & accept or reject it.
		lda izitindent
		beq CkValid

		moveword TabPt+2,[array]
		brl Accept

CkValid	lda TabPt+2					 ; if it's to the left of the obj.
		jmi Reject

		cmpw #P_TabWBottom-6,ThePt	  ; if it's out of the ruler.
		jne Reject

		cmpw TabPt+2,[array]			; It's to the left of the indent
		jlt Reject


		subword a,#P_MinTabDist,tmp	 ; In the indent's 'space'
		jmi Reject


		; See if it's on top of another tab's space (incl the indent).
		addword count,#1,tmp+2

		in arrayhdl:l,tmp:w,#4:w,tmp+2:w
		out offset:w
		xcall P_Traverse

		lsr a
		lsr a
		cmp tmp+2						   ; It's the last tab.
		bge DoInsert

		subword [array]:offset,TabPt+2,a
		abs a
		cmp #P_MinTabDist
		blt Reject						  ; There's a tab too D_Close

		; insert it.
DoInsert	inc newcount
		in arrayhdl:l,TabPt+2:w,#4:w,newcount:w
		out a:w
		xcall P_Traverse
		bne DoMove
		lda #4
DoMove	sta offset

		addlong a,array,ptr
		pushlong ptr
		addlong #4,ptr,s
		pushword #0
		lda newcount
		asl a
		asl a
		subword a,offset,s
		_BlockMove

		moveword TabPt+2,[ptr]
		moveword type,[ptr]:#2

		bra accept

Reject	call P_DrawATab,in=(ThePt:l,#-50:w,#-50:w,type:w)
		bra exit



Accept	call P_DrawATab,in=(ThePt:l,#-1:l,type:w)

		moverect P_ContentRect,tmprect
		stz tmprect
		tool _ClipRect,in=(!tmprect:l)
		call P_DrawATab,in=(#-1:l,ThePt:l,type:w)

exit	_ShowCursor

		return


;------------------------
; FixPt
;

FixPt	lda ThePt+2
		and #$FFFE
		sta ThePt+2

		lda izitindent
		bne FixDrag

		lda ThePt
		bpl CkVert

		stz ThePt

CkVert	subword ThePt,#3,a
		bmi FixDrag
		cmp P_HRulRect+4
		bge GotIt

FixDrag	moveword #P_TabWBottom-6,ThePt
GotIt	rts

		ENDP
		END

