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

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

	IMPORT	D_CheckMenuFID
	IMPORT	D_IsLetter
	IMPORT	D_ToLowerTable
	IMPORT	D_ToUpperTable
	IMPORT	P_BuildUndo
	IMPORT	P_SetFormat
	IMPORT	T_BumpParags
	IMPORT	T_CalcFBytes
	IMPORT	T_CurColor
	IMPORT	T_CurFont
	IMPORT	T_CurSize
	IMPORT	T_CurStyle
	IMPORT	T_DoRoom
	IMPORT	T_DotLine
	IMPORT	T_DotObj
	IMPORT	T_DotOffset
	IMPORT	T_DotParag
	IMPORT	T_FontFlags
	IMPORT	T_GetParPtr
	IMPORT	T_GetParSize
	IMPORT	T_GetRulBits
	IMPORT	T_GetTextPtr
	IMPORT	T_JunkLeft
	IMPORT	T_JunkRight
	IMPORT	T_MarkLine
	IMPORT	T_MarkObj
	IMPORT	T_MarkOffset
	IMPORT	T_MarkParag
	IMPORT	T_NewAddStyle
	IMPORT	T_NewColor
	IMPORT	T_NewFont
	IMPORT	T_NewSize
	IMPORT	T_NewSubStyle
	IMPORT	T_NextChar
	IMPORT	T_Offset2Line
	IMPORT	T_PrevChar
	IMPORT	T_RecutParag
	IMPORT	T_Refresh
	IMPORT	T_TextSelect
	IMPORT	T_TrackFont
	IMPORT	T_WriteFBytes

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

	ENTRY	T_ChangeCase
	ENTRY	T_IsSpace




;------------------------------------------------------------------------------
;
; T_UpdatePars (SPar:w,EPar:w)
;

T_UpdatePars	PROC		EXPORT
			;Using T_Data
			;Using T_TextEqu

		input SPar:w,EPar:w
		local ParagPtr:l,ObjHdl:l,VertPos:w

		begin

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


ParLoop
		in ObjHdl:l,SPar:w,VertPos:w
		out ObjHdl:l,VertPos:w
		xcall T_RecutParag

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

		inc SPar
		cmpw EPar,SPar
		bge ParLoop

Bump
		call T_BumpParags,in=(SPar:w,ObjHdl:l,VertPos:w,#0:w)


		lda T_TextSelect
		beq No

		in T_DotParag:w,T_DotOffset:w
		out T_DotObj:l,T_DotLine:w
		xcall T_Offset2Line

		in T_MarkParag:w,T_MarkOffset:w
		out T_MarkObj:l,T_MarkLine:w
		xcall T_Offset2Line
No

		call T_Refresh

		return
		ENDP




;-------------------------------------------------------------------------------
;
; T_DoChange (ChangeByte:w,Data:w,Data2:w)
;

T_DoChange	PROC		EXPORT
			;Using T_Data
			;Using T_TextEqu


		input ChangeByte:w,Data:w,Data2:w
		local CurPar:w,TextPtr:l

		begin

		moveword T_DotParag,CurPar

		ldy T_DotOffset
		cpy #T_ParHeader+1
		blt ParLoop

		rcall T_GetTextPtr,in=(CurPar:a),out=(TextPtr:ax)
		ldy T_DotOffset
		jsr FlipStuff
		inc CurPar

ParLoop
		lda CurPar
		cmp T_MarkParag
		bgt EndLoop

		rcall T_GetTextPtr,in=(a:a),out=(TextPtr:ax)

		ldy #T_ParHeader
		jsr FlipStuff

		lda ChangeByte
		dec a
		asl a
		tax
		lda Table,x
		tay
		jsr (JumpTable,x)


		inc CurPar
		bra ParLoop
EndLoop


		return


FlipStuff

CharLoop
		cmpw CurPar,T_MarkParag
		blt Skip
		cpy T_MarkOffset
		bge EndLoop2

Skip	lda [TextPtr],y
		iny
		and #$ff
		cmp #CR
		beq EndLoop2
		cmp ChangeByte
		beq FlipIt
		cmp #T_ColorChange+1
		bge CharLoop

		iny
		cmp #T_FontChange
		bne CharLoop
		iny
		bra CharLoop

EndLoop2
		rts




FlipIt
		lda ChangeByte
		dec a
		asl a
		tax
		jsr (JumpTable,x)
		iny
		bra CharLoop





DoFont
		moveword Data,[TextPtr]:y
		iny
		rts

DoStyle
		shortm
		lda Data
		ora Data2
		beq SetStyle

		lda Data2
		eor #$ff
		and [TextPtr],y
		ora Data
SetStyle	sta [TextPtr],y
		longm
		rts

DoSize
DoColor
		shortm
		moveword Data,[TextPtr]:y
		longm
		rts

JumpTable
	DC.W	DoFont
	DC.W	DoStyle
	DC.W	DoSize
	DC.W	DoColor


Table
	DC.W	0
	DC.W	2
	DC.W	3
	DC.W	4

		ENDP





;-----------------------------------------------------------------------------
;
; T_InsertFont (ParagNo:w,Offset:w,Mode:w) : Room:w
;
; If Mode is true, the style is set to T_NewAddStyle.  Otherwise T_NewAddStyle
; is added and T_NewSubStyle is subtracted.

T_InsertFont	PROC		EXPORT
			;Using T_Data
			;Using T_TextEqu

		input  ParagNo:w,Offset:w,Mode:w
		OUTPUT Room:w

		local  LeftOffset:w,RightOffset:w,SFlag:w,EFlag:w
		local  LeftFont:l,RightFont:l,LeftColor:w,RightColor:w
		local  TextPtr:l

		begin

		stz SFlag
		stz EFlag
		stz Room


		rcall T_JunkLeft,in=(ParagNo:a,Offset:x),out=(LeftOffset:a)
		cmp #T_ParHeader
		bne DoRight
		inc SFlag

DoRight
		rcall T_JunkRight,in=(ParagNo:a,Offset:x),out=(RightOffset:a)
		rcall T_GetParSize,in=(ParagNo:a),out=(a:a)
		dec a
		cmp RightOffset
		bne DoTrack
		inc EFlag

DoTrack
		rcall T_GetTextPtr,in=(ParagNo:a),out=(TextPtr:ax)

		call T_TrackFont,in=(TextPtr:l,LeftOffset:w)
		movelong T_CurFont,LeftFont
		moveword T_CurColor,LeftColor

		call T_TrackFont,in=(TextPtr:l,RightOffset:w)
		movelong T_CurFont,RightFont
		moveword T_CurColor,RightColor



; Check Font
		lda T_FontFlags
		and #T_FontFl
		beq DidFont
		moveword T_NewFont,RightFont
		ldy SFlag
		beq DidFont
		moveword a,[TextPtr]:#T_ParFont
DidFont


; Check Style
		lda T_FontFlags
		and #T_StyleFl
		beq DidStyle
		shortm

		lda Mode
		beq Normal
		lda T_NewAddStyle
		bra StoreStyle

Normal
		lda T_NewAddStyle
		ora T_NewSubStyle
		beq StoreStyle

		lda RightFont+2
		ora T_NewAddStyle
		sta RightFont+2
		lda T_NewSubStyle
		eor #$ff
		and RightFont+2
StoreStyle	sta RightFont+2
		ldy SFlag
		beq Long1
		moveword a,[TextPtr]:#T_ParStyle
Long1	longm
DidStyle


; Check Size
		lda T_FontFlags
		and #T_SizeFl
		beq DidSize
		shortm
		moveword T_NewSize,RightFont+3
		ldy SFlag
		beq Long2
		moveword a,[TextPtr]:#T_ParSize
Long2	longm
DidSize



; Do color
		lda T_FontFlags
		and #T_ColorFl
		beq DidColor
		shortm
		moveword T_NewColor,RightColor
		ldy SFlag
		beq Long3
		moveword a,[TextPtr]:#T_ParColor
Long3	longm
DidColor


		lda SFlag
		bne Exit
		lda EFlag
		bne Exit


		in LeftFont:l,LeftColor:w,RightFont:l,RightColor:w
		out a:w
		xcall T_CalcFBytes

		addword a,LeftOffset,a
		subword a,RightOffset,Room ;Room = Bytes-(RightOffset-LeftOffset)

		call T_DoRoom,in=(ParagNo:w,LeftOffset:w,Room:w)
		rcall T_GetTextPtr,in=(ParagNo:a),out=(TextPtr:ax)
		addwl LeftOffset,TextPtr

		in TextPtr:l,LeftFont:l,LeftColor:w,RightFont:l,RightColor:w
		xcall T_WriteFBytes


Exit	return
		ENDP




;-------------------------------------------------------------------------------
;
; T_ChangeFont ()
;

T_ChangeFont	PROC		EXPORT
			;Using T_Data
			;Using T_TextEqu

		local TextPtr:l,Offset:w,Room:w
		local EndFont:l,EndStyle:w,EndSize:w,EndColor:w

		begin

		call P_BuildUndo,in=(#0:w)

		rcall T_GetTextPtr,in=(T_MarkParag:a),out=(TextPtr:ax)
		rcall T_JunkRight,in=(T_MarkParag:a,T_MarkOffset:x),out=(Offset:a)
		call T_TrackFont,in=(TextPtr:l,Offset:w)
		moveword T_CurFont,EndFont
		shortm
		moveword T_CurStyle,EndStyle
		moveword T_CurSize,EndSize
		longm
		moveword T_CurColor,EndColor

		in T_DotParag:w,T_DotOffset:w,#0:w
		out Room:w
		xcall T_InsertFont
		addword a,T_DotOffset,T_DotOffset

		cmpw T_DotParag,T_MarkParag
		bne NotSame
		addword Room,T_MarkOffset,T_MarkOffset
NotSame


		lda T_FontFlags
		and #T_FontFl
		beq TryStyle

		call T_DoChange,in=(#T_FontChange:w,T_NewFont:w,#0:w)

TryStyle
		lda T_FontFlags
		and #T_StyleFl
		beq TrySize

		in #T_StyleChange:w,T_NewAddStyle:w,T_NewSubStyle:w
		xcall T_DoChange

TrySize
		lda T_FontFlags
		and #T_SizeFl
		beq TryColor

		call T_DoChange,in=(#T_SizeChange:w,T_NewSize:w,#0:w)

TryColor
		lda T_FontFlags
		and #T_ColorFl
		beq DidChanges

		call T_DoChange,in=(#T_ColorChange:w,T_NewColor:w,#0:w)


DidChanges
		moveword EndFont,T_NewFont
		shortm
		moveword EndStyle,T_NewAddStyle
		moveword EndSize,T_NewSize
		moveword EndColor,T_NewColor
		longm

		call T_InsertFont,in=(T_MarkParag:w,T_MarkOffset:w,#1:w),out=(a:w)

		stz T_FontFlags
		call T_UpdatePars,in=(T_DotParag:w,T_MarkParag:w)

		return
		ENDP





;-------------------------------------------------------------------------------
;
; T_RangeFont ()
;

T_RangeFont	PROC		EXPORT
			;Using T_Data
			;Using T_TextEqu

		local TextPtr:l,ParagNo:w,Offset:w,OrStyles:w,AndStyles:w
		local Just:w,Spacing:w,RulBits:w,StyleMask:w

		begin


		moveword T_DotParag,ParagNo
		rcall T_GetRulBits,in=(a:a),out=(RulBits:a)
		and #T_AllJust
		sta Just

		lda RulBits
		and #T_AllSpacing
		sta Spacing


		rcall T_GetTextPtr,in=(ParagNo:a),out=(TextPtr:ax)

		lda T_TextSelect
		jne DoSelect


		call T_TrackFont,in=(TextPtr:l,T_DotOffset:w)


; Check for any D_New font changes

		lda T_FontFlags
		jeq CheckIt

		and #T_FontFl
		beq NoFont
		moveword T_NewFont,T_CurFont
NoFont

		lda T_FontFlags
		and #T_StyleFl
		beq NoStyle
		movebyte T_NewAddStyle,T_CurStyle
NoStyle

		lda T_FontFlags
		and #T_SizeFl
		beq NoSize
		movebyte T_NewSize,T_CurSize
NoSize

		lda T_FontFlags
		and #T_ColorFl
		beq NoColor
		moveword T_NewColor,T_CurColor
NoColor
		brl CheckIt




DoSelect
		rcall T_JunkRight,in=(ParagNo:a,T_DotOffset:x),out=(Offset:a)
		call T_TrackFont,in=(TextPtr:l,Offset:w)
		shortm
		moveword T_CurStyle,OrStyles
		moveword a,AndStyles
		longm


NextChar
		cmpw ParagNo,T_MarkParag
		blt Okay
		cmpw Offset,T_MarkOffset
		jge Done
Okay

		moveword [TextPtr]:Offset,a
		inc Offset
		and #$ff
		cmp #CR
		beq NextParag

		cmp #T_ColorChange+1
		bge NextChar

		asl a
		tax
		jmp (JumpTable,x)


DoFont
		cmpw [TextPtr]:Offset,T_CurFont
		beq SameFont
		moveword #$FFFF,T_CurFont
SameFont	inc Offset
		inc Offset
		brl NextChar


DoStyle
		shortm
		ldy Offset
		moveword [TextPtr]:y,T_CurStyle
		ora OrStyles
		sta OrStyles
		moveword [TextPtr]:y,a
		and AndStyles
		sta AndStyles
		longm
		inc Offset
		brl NextChar


DoSize
		shortm
		cmpw [TextPtr]:Offset,T_CurSize
		beq SameSize
		moveword #$FF,T_CurSize
SameSize	longm
		inc Offset
		brl NextChar


DoColor
		shortm
		cmpw [TextPtr]:Offset,T_CurColor
		beq SameColor
		moveword #$FF,T_CurColor
SameColor	longm
		inc Offset
		brl NextChar



NextParag
		inc ParagNo

		cmpw ParagNo,T_MarkParag
		blt Okay2
		cmpw T_MarkOffset,#T_ParHeader
		jeq Done
Okay2


		rcall T_GetRulBits,in=(ParagNo:a),out=(RulBits:a)

		and #T_AllJust
		cmp Just
		beq JustSame
		stz Just
JustSame

		lda RulBits
		and #T_AllSpacing
		cmp Spacing
		beq SpaceSame
		stz Spacing
SpaceSame


		rcall T_GetTextPtr,in=(ParagNo:a),out=(TextPtr:ax)

		cmpw [TextPtr],T_CurFont
		beq SameFont2
		moveword #$FFFF,T_CurFont
SameFont2

		shortm
		ldy #T_ParStyle
		moveword [TextPtr]:y,T_CurStyle
		ora OrStyles
		sta OrStyles
		moveword [TextPtr]:y,a
		and AndStyles
		sta AndStyles

		cmpw [TextPtr]:#T_ParSize,T_CurSize
		beq SameSize2
		moveword #$FF,T_CurSize
SameSize2

		cmpw [TextPtr]:#T_ParColor,T_CurColor
		beq SameColor2
		moveword #$FF,T_CurColor
SameColor2
		longm

		moveword #T_ParHeader,Offset
		brl NextChar



Done
		shortm
		lda OrStyles
		beq StyleSet
		moveword #$ff,T_CurStyle
		lda AndStyles
		beq StyleSet
		sta T_CurStyle
StyleSet	longm



CheckIt
		call D_CheckMenuFID,in=(T_CurFont:l,T_CurColor:w)
		call P_SetFormat,in=(Just:w,Spacing:w)


		return

JumpTable
	DC.W	NextChar
	DC.W	DoFont
	DC.W	DoStyle
	DC.W	DoSize
	DC.W	DoColor

		ENDP





;------------------------------------------------------------------------------
;
; T_Upper ()
; T_Lower ()
; T_Capit ()
;

T_Lower	PROC		EXPORT
			;Using T_Data
			;Using T_TextEqu
		EXPORT	T_Upper
		EXPORT	T_Capit
		EXPORT	T_ChangeCase

LOWER	equ	  0
UPPER	equ	  1
CAPIT	equ	  2

		moveword #LOWER,>What
		bra T_ChangeCase

T_Upper		;		
		moveword #UPPER,>What
		bra T_ChangeCase

T_Capit		;		
		moveword #CAPIT,>What



T_ChangeCase		;		

		local TextPtr:l,CurOffset:w,CurParag:w,First:w,Char:w
		begin +b


		call P_BuildUndo,in=(#0:w)

		moveword T_DotOffset,CurOffset
		moveword T_DotParag,CurParag
		rcall T_GetTextPtr,in=(a:a),out=(TextPtr:ax)


		cmpw What,#Capit
		bne  StartLoop

		stz First

		rcall T_PrevChar,in=(T_DotParag:a,T_DotOffset:x)
		tay
		beq ItsFirst

		lda [TextPtr],y
		and #$ff
		jsl T_IsSpace
		bcc StartLoop

ItsFirst	inc First
		bra StartLoop



; Loop through the selected characters

Loop	cmpw Char,#CR
		bne SamePar

		inc CurParag
		rcall T_GetTextPtr,in=(CurParag:a),out=(TextPtr:ax)
		moveword #T_ParHeader,CurOffset
		bra StartLoop


SamePar
; BOGUS -- NextChar
		rcall T_NextChar,in=(CurParag:a,CurOffset:x),out=(CurOffset:a)


StartLoop	moveword [TextPtr]:CurOffset,a
		and #$ff
		sta Char

		cmpw CurParag,T_MarkParag
		blt CheckChar
		cmpw CurOffset,T_MarkOffset
		bge Update


CheckChar
		rcall T_IsSpace,in=(Char:a)
		bcs IsSpace
		tax

		ldy What
		cpy #UPPER
		beq Up
		cpy #LOWER
		beq Down
		ldy First
		bne Up

Down	lda	>D_ToLowerTable,x
		bra Cased
Up		lda	>D_ToUpperTable,x

Cased
		movebyte a,[TextPtr]:CurOffset

		stz First
		brl Loop


IsSpace
		moveword	#1,First
		brl Loop



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


		return

What		DS.B 2
		ENDP




;-------------------------------------------------------------------------------
;
; T_IsSpace (Char:a)
;

T_IsSpace	PROC		EXPORT
		cmp #32
		beq GSpace
		cmp #TAB
		beq GSpace
		cmp #CR
		beq GSpace
		clc
GSpace	rtl
		ENDP
		END

