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

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

	IMPORT	X_CopyOneHandle
	IMPORT	D_MainZPage
	IMPORT	D_NeedHand
	IMPORT	P_BuildUndo
	IMPORT	P_DefJust
	IMPORT	P_DefSpacing
	IMPORT	P_DrawTabs
	IMPORT	P_EraseTabs
	IMPORT	T_DotObj
	IMPORT	T_DotParag
	IMPORT	T_Editing
	IMPORT	T_EraseCaret
	IMPORT	T_GetParPtr
	IMPORT	T_MarkParag
	IMPORT	T_RulDrawn
	IMPORT	T_RulObj
	IMPORT	T_TextSelect
	IMPORT	T_UpdatePars

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

	ENTRY	T_ClearRulers
	ENTRY	T_SetRulers




;------------------------------------------------------------------------------
;
; T_UseRuler (RulerHdl:ax)
;

T_UseRuler	PROC		EXPORT
			;Using D_GlobalData

RulerHdl	equ  0
RulerPtr	equ  4

		phd
		pha
		lda >D_MainZPage
		tcd

		pla
		movelong ax,RulerHdl
		movelong [RulerHdl],RulerPtr
		lda [RulerPtr]
		inc a
		sta [RulerPtr]

		pld
		rtl
		ENDP




;------------------------------------------------------------------------------
;
; T_UsUseRuler (RulerHdl:ax)
;

T_UnUseRuler	PROC		EXPORT
			;Using D_GlobalData
			;Using T_Data

RulerHdl	equ  0
RulerPtr	equ  4

		phd
		pha
		lda >D_MainZPage
		tcd

		pla
		movelong ax,RulerHdl
		movelong [RulerHdl],RulerPtr
		lda [RulerPtr]
		dec a
		sta [RulerPtr]
		bne Exit

		cmpl RulerHdl,T_RulDrawn
		bne Okay
		stzl T_RulDrawn
Okay	tool _DisposeHandle,in=(RulerHdl:l)

Exit	pld
		rtl
		ENDP




;------------------------------------------------------------------------------
;
; T_NewRuler() : RulerHdl:l
;

T_NewRuler	PROC		EXPORT
			;Using P_Data
			;Using T_TextEqu
			;Using D_GlobalData

		OUTPUT RulerHdl:l
		LOCAL  RulerPtr:l
		ERROR  Err

		BEGIN

		call D_NeedHand,in=(#T_RulerInfoSize:l),out=(RulerHdl:l),err=(Err)

		movelong [RulerHdl],RulerPtr
		moveword #1,[RulerPtr]:#T_ParsUsing
		lda P_DefJust
		ora P_DefSpacing
		moveword a,[RulerPtr]:#T_RulBits

		moveword #0,[RulerPtr]:#T_TabCount
		moveword #0,[RulerPtr]:#T_Indent
		moveword #P_IndentTab,[RulerPtr]:#T_Indent+2

		RETURN
		ENDP




;------------------------------------------------------------------------------
;
; T_DrawRuler ()
;

T_DrawRuler	PROC		EXPORT
			;Using T_Data
			;Using T_TextEqu

		LOCAL ParagPtr:l,RulerHdl:l
		BEGIN

		lda T_Editing
		beq Exit

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

		cmpl T_DotObj,T_RulObj
		bne DrawIt
		cmpl RulerHdl,T_RulDrawn
		beq Exit

DrawIt
		movelong T_DotObj,T_RulObj
		movelong RulerHdl,T_RulDrawn
		call P_EraseTabs
		call P_DrawTabs

Exit	RETURN
		ENDP



;------------------------------------------------------------------------------
;
; T_ChangeRuls (ModType:w,Data:w,TabList:l)
;

T_ChangeRuls	PROC		EXPORT
			;Using P_Data
			;Using T_TextEqu
			;Using T_Data

		INPUT ModType:w,Data:w,TabList:l
		LOCAL CurPar:w,ParagPtr:l,NewRuler:l,NewPtr:l,TabPtr:l
		LOCAL RulerHdl:l

		BEGIN

		call P_BuildUndo,in=(#0:w)

		lda T_TextSelect
		bne Range
		call T_EraseCaret
		moveword T_DotParag,T_MarkParag
Range

		moveword T_DotParag,CurPar

ParLoop
		rcall T_GetParPtr,in=(CurPar:a),out=(ParagPtr:ax)
		movelong [ParagPtr]:#T_RulerHdl,RulerHdl
		lda RulerHdl+2
		and #T_MarkBit
		bne NextParag


		call X_CopyOneHandle,in=(RulerHdl:l),out=(NewRuler:l)
		movelong [NewRuler],NewPtr
		moveword #0,[NewPtr]	  ;ParsUsing set by T_SetRulers

		lda ModType
		asl a
		tax
		jsr (ModTable,x)

		in NewRuler:l,RulerHdl:l,CurPar:w,T_MarkParag:w,#1:w
		xcall T_SetRulers


NextParag
		inc CurPar
		cmpw T_MarkParag,CurPar
		bge ParLoop

		call T_ClearRulers,in=(T_DotParag:w,T_MarkParag:w)
		call T_UpdatePars,in=(T_DotParag:w,T_MarkParag:w)

		RETURN


ModTable
	DC.W	Spacing
	DC.W	Justif
	DC.W	Indent
	DC.W	Tabs

Spacing
		ldy #T_RulBits
		lda [NewPtr],y
		and #T_NoSpacing
		ora Data
		sta [NewPtr],y
		rts

Justif
		ldy #T_RulBits
		lda [NewPtr],y
		and #T_NoJust
		ora Data
		sta [NewPtr],y
		rts

Indent
		moveword Data,[NewPtr]:#T_Indent
		rts

Tabs
		movelong [TabList],TabPtr
		ldy #T_TabCount
		moveword [TabPtr]:y,[NewPtr]:y
		addlong #T_TabArray,TabPtr,s
		addlong #T_TabArray,NewPtr,s
		pushlong #P_MaxTabCount*4
		_BlockMove
		rts


		ENDP




;------------------------------------------------------------------------------
;
; T_SetRulers (NewRuler:l,OldRuler:l,FirstPar:w,LastPar:w,UnUse:w)
;
; The NewRuler is marked in every paragraph record that uses its.
; DisposeRuler is called for every paragraph that is no longer using
; OldRuler if UnUse is set.

T_SetRulers	PROC		EXPORT
			;Using T_TextEqu

		INPUT NewRuler:l,OldRuler:l,FirstPar:w,LastPar:w,UnUse:w
		LOCAL ParagPtr:l,ParsUsing:w,RulerHdl:l,RPtr:l

		BEGIN


		lda NewRuler+2
		ora #T_MarkBit
		sta NewRuler+2
		stz ParsUsing

ParLoop
		rcall T_GetParPtr,in=(FirstPar:a),out=(ParagPtr:ax)
		movelong [ParagPtr]:#T_RulerHdl,RulerHdl
		cmpl RulerHdl,OldRuler
		bne NextPar

		inc ParsUsing
		lda UnUse
		beq SetIt
		rcall T_UnUseRuler,in=(RulerHdl:ax)
SetIt	movelong NewRuler,[ParagPtr]:#T_RulerHdl

NextPar
		inc FirstPar
		cmpw LastPar,FirstPar
		bge ParLoop

		movelong [NewRuler],RPtr
		moveword ParsUsing,[RPtr]

		RETURN
		ENDP




;------------------------------------------------------------------------------
;
; T_ClearRulers (FirstPar:w,LastPar:w)
;
; Clears the marks in the ruler hdls.

T_ClearRulers	PROC		EXPORT
			;Using T_TextEqu

		INPUT FirstPar:w,LastPar:w
		LOCAL ParagPtr:l

		BEGIN

ParLoop
		rcall T_GetParPtr,in=(FirstPar:a),out=(ParagPtr:ax)
		ldy #T_RulerHdl+2
		moveword [ParagPtr]:y,a
		and #$7FFF
		moveword a,[ParagPtr]:y

		inc FirstPar
		cmpw LastPar,FirstPar
		bge ParLoop

		RETURN
		ENDP
		END

