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

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

	IMPORT	D_BitMapChanged
	IMPORT	D_BitMapPtr
	IMPORT	D_ClearBitMap
	IMPORT	D_Deref
	IMPORT	P_AddThreadRgn
	IMPORT	P_CheckHand
	IMPORT	P_ContentRect
	IMPORT	P_CurrentWin
	IMPORT	P_DoThread
	IMPORT	P_DrawContent
	IMPORT	P_DrawHandles
	IMPORT	P_ErrorAlert
	IMPORT	P_FixMenus
	IMPORT	P_FixSpecRect
	IMPORT	P_HideTabs
	IMPORT	P_KillUndo
	IMPORT	P_Page
	IMPORT	P_PageMode
	IMPORT	P_Placing
	IMPORT	P_ThreadDir
	IMPORT	P_ThreadHdl
	IMPORT	D_RectZap
	IMPORT	T_BlinkCaret
	IMPORT	T_Click
	IMPORT	T_GetParPtr
	IMPORT	T_InvSelect
	IMPORT	T_NewParHdl
	IMPORT	T_OffThread
	IMPORT	T_PadThread
	IMPORT	T_Pt2Offset
	IMPORT	T_RecutParag

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

	ENTRY	T_CInterval
	ENTRY	T_CaretDrawn
	ENTRY	T_CaretTime
	ENTRY	T_ClickCount
	ENTRY	T_ClipThread
	ENTRY	T_CurParHdl
	ENTRY	T_Editing
	ENTRY	T_FontFlags
	ENTRY	T_ReThread
	ENTRY	T_Refresh
	ENTRY	T_RulDrawn
	ENTRY	T_TextSelect
	ENTRY	T_ThreadRgn




;-----------------------------------------------------------------------------
;
; T_New (ObjHdl:l,ParagHdl:l)
;
; Returns any errors.  ObjHdl should be X_Locked when T_New is called.


T_New	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData
			;Using T_Data
			;Using T_TextEqu

		input  ObjHdl:l,ParagHdl:l
		local  ObjPtr:l,ParagPtr:l,PThread:l,ReThreadHdl:l,TempPtr:l

		error  Err

		begin


		stz Err

		movelong [ObjHdl],ObjPtr
		call P_FixSpecRect,in=(ObjPtr:l)


; Make a D_New parag array if neccessary

		cpzl ParagHdl
		bne ThreadExists
		call T_NewParHdl,out=(ParagHdl:l),err=(Err)
		jcs Exit
ThreadExists	movelong ParagHdl,[ObjPtr]:#P_ParagHdl


		moveword P_Page,[ObjPtr]:#P_PageNo
		moveword #0,[ObjPtr]:#P_NThread
		moveword a,[ObjPtr]:#P_NThread+2
		moveword a,[ObjPtr]:#P_PThread
		moveword a,[ObjPtr]:#P_PThread+2


; Do threading

		lda P_Placing
		beq NoThread
		cpzl P_ThreadHdl
		beq NoThread

		rcall D_Deref,in=(P_ThreadHdl:ax),out=(TempPtr:ax)
		cmpw [TempPtr]:#P_PageNo,P_Page
		bne NoHand1
		call P_DrawHandles,in=(TempPtr:l)
NoHand1

		call P_DoThread,in=(P_ThreadHdl:l,ObjHdl:l,P_ThreadDir:w)

		cmpw [TempPtr]:#P_PageNo,P_Page
		bne NoHand2
		call P_DrawHandles,in=(TempPtr:l)
NoHand2

		tool _HUnlock,in=(P_ThreadHdl:l)
NoThread




; Setup and rethread

		movelong [ObjPtr]:#P_PThread,PThread
		cpzl PThread
		bne PrevExists

		movelong ObjHdl,ReThreadHdl
		movelong [ParagHdl],ParagPtr
		movelong ObjHdl,[ParagPtr]:#T_ObjHdl

		lda #0
		moveword a,[ParagPtr]:#T_ParStart
		moveword a,[ObjPtr]:#P_StartParag
		moveword a,[ObjPtr]:#P_StartLine
		lda #$FFFF
		moveword a,[ObjPtr]:#P_EndParag
		moveword a,[ObjPtr]:#P_EndLine
		bra Done


PrevExists
		movelong PThread,ReThreadHdl

Done
		call T_ReThread,in=(ReThreadHdl:l)


Exit	return
		ENDP





;-------------------------------------------------------------------------------
;
; T_ReThread (ObjHdl:l)
;

T_ReThread	PROC		EXPORT
			;Using P_ObjData
			;Using T_Data
			;Using T_TextEqu

		input ObjHdl:l
		local ObjPtr:l,ParagHdl:l,ParagPtr:l
		local ParagNo:w,ParagCount:w,VertPos:w
		error Err

		begin +b


		stz Err

; D_Save on stack
		pushlong T_CurParHdl

		movelong [ObjHdl],ObjPtr
		movelong [ObjPtr]:#P_ParagHdl,ParagHdl

		movelong ParagHdl,T_CurParHdl
		rcall T_GetParPtr,in=(#0:a),out=(ParagPtr:ax)
		moveword [ParagPtr],ParagCount



		cpzl [ObjPtr]:#P_PThread
		bne NotStart

		lda #0
		moveword a,[ObjPtr]:#P_StartParag
		moveword a,[ObjPtr]:#P_StartLine
		sta ParagNo
		sta VertPos
		bra ParagLoop


NotStart
		moveword [ObjPtr]:#P_StartParag,ParagNo
		bpl NotEnd
		moveword ParagCount,ParagNo
NotEnd
		lda ParagNo
		beq NoDec
		dec ParagNo
NoDec

		rcall T_GetParPtr,in=(ParagNo:a),out=(ParagPtr:ax)
		movelong [ParagPtr]:#T_ObjHdl,ObjHdl
		moveword [ParagPtr]:#T_ParStart,VertPos



ParagLoop
		in ObjHdl:l,ParagNo:w,VertPos:w
		out ObjHdl:l,VertPos:w
		xcall T_RecutParag

		rcall T_GetParPtr,in=(ParagNo:a),out=(ParagPtr:ax)
		tool _DisposeHandle,in=([ParagPtr]:#T_LinesHdl:l)
		movelong #0,[ParagPtr]:#T_LinesHdl
		moveword [ParagPtr]:#T_LineCount,a

		inc ParagNo
		cmpw ParagNo,ParagCount
		blt ParagLoop


		call T_PadThread,in=(ObjHdl:l,VertPos:w)


; Restore from stack

Done
		pulllong T_CurParHdl

		return
		ENDP






;-----------------------------------------------------------------------------
;
; T_Activate (ObjHdl:l,Pt:l) : Drawn:w
;
; ThePoint should be in local coordinates.
;

T_Activate	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData
			;Using T_Data

		input  ObjHdl:l,Pt:l
		output Drawn:w

		local ObjPtr:l,Start:l,End:l
		local NewParag:w,NewOffset:w,NewLine:w,NewPixs:w

		begin


		stz Drawn

		lda P_PageMode
		jne Exit

		call P_CheckHand,in=(#$8000:l)
		bcc Cont
		call P_ErrorAlert,in=(#1:w)
		brl Exit
Cont

		movelong [ObjHdl],ObjPtr
		movelong [ObjPtr]:#P_StartParag,Start
		movelong [ObjPtr]:#P_EndParag,End
		movelong [ObjPtr]:#P_ParagHdl,T_CurParHdl

		inc T_Editing
		stz T_ClickCount
		stz T_TextSelect
		stzl T_RulDrawn

		tool _GetCaretTime,out=(T_CInterval:l)
		tool _SetPort,in=(P_CurrentWin:l)
		call P_KillUndo

		cmpl Start,End
		beq Deactivate

		in ObjHdl:l,Pt:l
		out NewParag:w,NewOffset:w,NewLine:w,NewPixs:w
		xcall T_Pt2Offset

		call T_OffThread,in=(NewParag:w,NewLine:w),out=(a:w)
		bne Deactivate

		tool _NewRgn,out=(T_ThreadRgn:l)
		call T_ClipThread,in=(ObjHdl:l)
		jsl T_Refresh

		call T_Click,in=(ObjHdl:l,Pt:l,#0:w,#0:w)
		inc Drawn

Exit	return

Deactivate
		stz T_Editing
		stzl T_CurParHdl
		bra Exit

		ENDP




;-----------------------------------------------------------------------------
;
; T_Deactivate (Refresh:w,OffScreen:w)
;
; Refresh - Refreshes the screen if true.

T_Deactivate	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData
			;Using T_Data

		input Refresh:w,OffScreen:w

		begin

		lda T_Editing
		beq Exit

		jsl P_KillUndo
		jsl T_InvSelect
		jsl P_HideTabs

		stz T_Editing
		stz T_FontFlags
		stzl T_CurParHdl

		tool _DisposeRgn,in=(T_ThreadRgn:l)

		lda Refresh
		beq DoMenus
		lda OffScreen
		beq On
		jsl T_Refresh
		bra DoMenus
On	call P_DrawContent,in=(#P_ContentRect:l,#0:w)
DoMenus	jsl P_FixMenus


Exit	return
		ENDP




;------------------------------------------------------------------------------
;
; T_ClipThread (ObjHdl:l)
;

T_ClipThread	PROC		EXPORT
			;Using P_ObjData
			;Using T_Data

		input ObjHdl:l
		local ThreadHdl:l,ThreadPtr:l,PThread:l

		begin

		lda T_Editing
		beq Exit
		
		cpzl [ObjHdl]
		bne DoIt						;if zero not valid
		tool _SetEmptyRgn,in=(T_ThreadRgn:l)
		bra Exit	

DoIt		
		movelong ObjHdl,ThreadHdl

Loop
		movelong [ThreadHdl],ThreadPtr
		movelong [ThreadPtr]:#P_PThread,PThread
		cpzl PThread
		beq EndLoop
		movelong PThread,ThreadHdl
		bra Loop
EndLoop

		tool _SetEmptyRgn,in=(T_ThreadRgn:l)
		call P_AddThreadRgn,in=(ThreadHdl:l,T_ThreadRgn:l)

Exit	return
		ENDP



;-----------------------------------------------------------------------------
;
; T_Refresh ()
;

T_Refresh	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData
			;Using T_Data
			;Using D_GlobalData


		local Port:l,Q_Rect:r

		begin

		movelong P_CurrentWin,Port
		pushlong [Port]:#2			   ;D_Save BitMap Ptr
		movelong >D_BitMapPtr,[Port]:#2

		moverect P_ContentRect,Q_Rect
		tool _LocalToGlobal,in=(!Q_Rect:l)
		tool _LocalToGlobal,in=(!Q_Rect+4:l)
		call D_ClearBitMap,in=(!Q_Rect:l)
		call P_DrawContent,in=(#P_ContentRect:l,#0:w)
		tool _HideCursor
		call D_RectZap,in=(!Q_Rect:l)
		tool _ShowCursor
		pulllong [Port]:#2			   ;Restore BitMap Ptr
		moveword #1,>D_BitMapChanged

		return
		ENDP




;-----------------------------------------------------------------------------
;
; T_Idle (TickCount:l)
;

T_Idle	PROC		EXPORT
			;Using P_Data
			;Using T_Data

		input TickCount:l
		local Diff:l,CTime:l

		begin

		lda T_TextSelect
		bne Exit


		sublong TickCount,T_CaretTime,Diff
		cmpl Diff,T_CInterval
		bcc Exit
		movelong TickCount,T_CaretTime

		jsl T_BlinkCaret
		lda T_CaretDrawn
		eor #1
		sta T_CaretDrawn

Exit	return
		ENDP




;------------------------------------------------------------------------------
;
; T_Data
;

T_Data	PROC		EXPORT
		EXPORT	T_Editing
		EXPORT	T_CurParHdl
		EXPORT	T_TextSelect
		EXPORT	T_RulDrawn
		EXPORT	T_RulObj
		EXPORT	T_ThreadRgn
		EXPORT	T_CurRulHdl
		EXPORT	T_CurRulPtr
		EXPORT	T_CurRulBits
		EXPORT	T_CurIndent
		EXPORT	T_Dot
		EXPORT	T_DotParag
		EXPORT	T_DotOffset
		EXPORT	T_DotLine
		EXPORT	T_DotPixs
		EXPORT	T_DotObj
		EXPORT	T_Mark
		EXPORT	T_MarkParag
		EXPORT	T_MarkOffset
		EXPORT	T_MarkLine
		EXPORT	T_MarkPixs
		EXPORT	T_MarkObj
		EXPORT	T_FontFlags
		EXPORT	T_NewFont
		EXPORT	T_NewAddStyle
		EXPORT	T_NewSubStyle
		EXPORT	T_NewSize
		EXPORT	T_NewColor
		EXPORT	T_CurFont
		EXPORT	T_CurStyleSize
		EXPORT	T_CurStyle
		EXPORT	T_CurSize
		EXPORT	T_CurColor
		EXPORT	T_MaxAscent
		EXPORT	T_MaxDescent
		EXPORT	T_ScalePort
		EXPORT	T_ScaleLocInfo
		EXPORT	T_ScaleRect
		EXPORT	T_LastPos
		EXPORT	T_LastParag
		EXPORT	T_LastOffset
		EXPORT	T_CInterval
		EXPORT	T_CaretTime
		EXPORT	T_CaretDrawn
		EXPORT	T_ClickCount
		EXPORT	T_BrkFlag

T_Editing		DS.B 2			   ; True if editing
T_CurParHdl		DS.B 4			   ; Current thread being operated on
T_TextSelect		DS.B 2			   ; True if Q_Text is selected
T_RulDrawn		DS.B 4			   ; Current ruler drawn
T_RulObj		DS.B 4			   ; Object T_RulDraw is in
T_ThreadRgn		DS.B 4			   ; Rgn for thread being edited

T_CurRulHdl		DS.B 4			   ;Vars set by T_LockRuler
T_CurRulPtr		DS.B 4
T_CurRulBits		DS.B 2
T_CurIndent		DS.B 2


T_Dot
T_DotParag		DS.B 2
T_DotOffset		DS.B 2
T_DotLine		DS.B 2
T_DotPixs		DS.B 2
T_DotObj		DS.B 4

T_Mark
T_MarkParag		DS.B 2
T_MarkOffset		DS.B 2
T_MarkLine		DS.B 2
T_MarkPixs		DS.B 2
T_MarkObj		DS.B 4



T_FontFlags		DS.B 2



T_NewFont		DS.B 2	;The D_New font
T_NewAddStyle		DS.B 2	;D_New styles to add
T_NewSubStyle		DS.B 2	;D_New styles to subtract
T_NewSize		DS.B 2	;D_New Size
T_NewColor		DS.B 2	;NewColor


T_CurFont		DS.B 2
T_CurStyleSize
T_CurStyle		DS.B 1
T_CurSize		DS.B 1
T_CurColor		DS.B 2

T_MaxAscent		DS.B 2
T_MaxDescent		DS.B 2


T_ScalePort		DS.B 4
T_ScaleLocInfo	DC.W	$80	;PortSCB
	DC.L	0	;PixelImagePtr
	DC.W	320	;Width
T_ScaleRect	DC.W	0,0,100,1280	;BoundsRect



T_LastPos	; ; Last caret position
T_LastParag		DS.B 2
T_LastOffset		DS.B 2


T_CInterval		DS.B 4					; Time between caret blinks
T_CaretTime	DC.L	0	; Last time caret was drawn
T_CaretDrawn	DC.W	0
T_ClickCount		DS.B 2





T_BrkFlag	DC.W	0


		ENDP





;-----------------------------------------------------------------------------
; T_TextEqu

T_TextEqu	PROC		EXPORT



; Paragraph Info





; RulerInfo





; Q_Line Info






; Q_Text Block Header








; RulBits stuff















		ENDP





;-----------------------------------------------------------------------------
;
; ParagHdl -->  ParagCount
;			   Array [1..ParagCount] of ParagInfo
;
; ParagInfo
;			 TextBlock  :  long;  --  Handle to Q_Text block
;			 ParOffset  :  word;  --  Offset within Q_Text block
;			 ParStart   :  word;  --  Top pixel count of paragraph
;			 RulerHdl   :  long;  --  Handle to ruler
;			 ParHeight  :  word;  --  Pixel height of paragraph
;			 LinesHdl   :  long;  --  Handle to lines array
;			 LineCount  :  word;  --  Number of lines
;			 ObjHdl	 :  long;  --  Object with first Q_Line of parag.
;
;
; RulerInfo
;			 ParsUsing  : word;
;			 RulBits	: word;
;			 NotUsed	: word;
;			 TabCount   : word;	; Does not include Indent.
;			 Indent	 : long;
;			 TabArray   : 40 bytes
;
;
;
; LineInfo
;			 Offset	 : word;
;			 Length	 : word;
;			 LineWidth  : word;
;			 Ascent	 : byte;
;			 Descent	: byte;
;
		END

