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

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

	IMPORT	D_BeachBall
	IMPORT	W_CalcDocRect
	IMPORT	W_CalcFBytes
	IMPORT	W_CalcPages
	IMPORT	D_CheckMenuFID
	IMPORT	W_ColorFl
	IMPORT	W_CurDoc
	IMPORT	W_DoRoom
	IMPORT	EFont
	IMPORT	W_EndLine
	IMPORT	W_EndOffset
	IMPORT	W_EndPar
	IMPORT	W_FindFont
	IMPORT	W_FindLine
	IMPORT	W_FontFl
	IMPORT	W_GetAddr
	IMPORT	W_GetParRec
	IMPORT	W_JunkLeft
	IMPORT	W_JunkRight
	IMPORT	D_KillFont
	IMPORT	W_MakeLines
	IMPORT	W_NewColor
	IMPORT	W_NewFont
	IMPORT	W_NewInvStyle
	IMPORT	W_NewSize
	IMPORT	W_NewStyle
	
	IMPORT	W_ParRec
	IMPORT	W_PutOnScreen
	IMPORT	W_ReadFont
	IMPORT	SFont
	IMPORT	W_SetDocRect
	IMPORT	D_SetFileChanged
	IMPORT	W_SetFullREct
	IMPORT	W_SizeFl
	IMPORT	W_StartLine
	IMPORT	W_StartOffset
	IMPORT	W_StartPar
	IMPORT	W_StyleFl
	
	
	IMPORT	W_UndoHandle
	IMPORT	W_UndoOn
	IMPORT	W_UpdateText
	IMPORT	W_UpdateDoc
	IMPORT	W_WPColor
	IMPORT	W_WpFont
	IMPORT	W_WPFontFlag
	IMPORT	W_WPSize
	IMPORT	W_WpStyle
	IMPORT	W_WPUndoType
	IMPORT	W_WpCopy
	IMPORT	W_WriteFBytes
	
	
	IMPORT	Q_Rect
	IMPORT	W_Selected

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

	ENTRY	W_CalcFonts
	ENTRY	W_CalcNewLines
	ENTRY	W_DoChange
	ENTRY	W_KillChanges
	ENTRY	W_NoSC

********************************************************************
*	Includes...
*
* Monday, May 1, 1989 12:36:36 AM
********************************************************************
		



****************************************************************
W_TextChange	PROC		EXPORT
			;Using wpglobals
		EXPORT	W_NoSC

		local ChrPtr:l,CurPar:w,W_ParRec:l,Q_Rect:r,EOffset2:w,SFlag:w
		local SFont1:l,SColor1:w,SFont2:l,SColor2:w,EFont1:l,EColor1:w
		local EFont2:l,EColor2:w,SOffset1:w,SOffset2:w,EOffset1:w,EFlag:w
		begin
; SStyle	equ SFont+2
; SSize	equ SStyle+1
; EStyle	equ EFont+2
; ESize	equ EStyle+1

		lda W_Selected
		jeq TCExit

		pushlong W_CurDoc
		jsl D_SetFileChanged

		pushword W_StartPar
		pushword W_StartLine
		pushlong #0
		jsl W_PutOnScreen

		cmpw #W_StyleUndo,W_WPUndoType
		beq UndoBeOn

		pushword #W_StyleUndo
		jsl W_UndoOn

		spacelong
		pushword W_StartPar
		pushword W_StartLine
		pushword W_StartOffset
		pushword W_EndPar
		pushword W_EndLine
		pushword W_EndOffset
		jsl W_WpCopy
		pulllong W_UndoHandle
		bcc UndoBeOn

		stz W_WPUndoType

UndoBeOn
		stz SFlag
		stz EFlag

		pha
		pushword W_StartPar
		pushword W_StartLine
		pushword W_StartOffset
		jsl W_JunkLeft
		pullword SOffset1

		pha
		pushword W_StartPar
		pushword W_StartLine
		pushword W_StartOffset
		jsl W_JunkRight
		pullword SOffset2

		pha
		pushword W_EndPar
		pushword W_EndLine
		pushword W_EndOffset
		jsl W_JunkLeft
		pullword EOffset1

		pha
		pushword W_EndPar
		pushword W_EndLine
		pushword W_EndOffset
		jsl W_JunkRight
		pullword EOffset2

		spacelong
		spaceword
		pushword W_StartPar
		pushword W_StartLine
		pushword SOffset1
		jsl W_FindFont
		pullword SColor1
		pulllong SFont1

		spacelong
		spaceword
		pushword W_StartPar
		pushword W_StartLine
		pushword SOffset2
		jsl W_FindFont
		pullword SColor2
		pulllong SFont2

		spacelong
		spaceword
		pushword W_EndPar
		pushword W_EndLine
		pushword EOffset1
		jsl W_FindFont
		pullword EColor1
		pulllong EFont1

		spacelong
		spaceword
		pushword W_EndPar
		pushword W_EndLine
		pushword EOffset2
		jsl W_FindFont
		pullword EColor2
		pulllong EFont2

		moveword SOffset2,W_StartOffset
		moveword EOffset1,W_EndOffset

		lda W_EndPar
		ldx EOffset2
		jsl W_GetAddr
		movelong ax,ChrPtr
		cmpb [ChrPtr],#cr
		bne NotEnd
		inc EFlag
NotEnd	cmpw SOffset1,#W_TextHeader
		bne NotSt
		inc SFlag
		stz W_StartOffset
NotSt

;first change the font

		lda W_FontFl
		beq W_NoFC

		lda W_NewFont
		sta SFont2
		sta EFont1

		pushword #0
		pushword #1
		pushword W_NewFont
		jsl W_DoChange

W_NoFC
;now check for a size change

		lda W_SizeFl
		jeq W_NoSC

		shortm
		moveword W_NewSize,SFont2+3
		sta EFont1+3
		longm

		pushword #1
		pushword #3
		pushword W_NewSize
		jsl W_DoChange

;here's where I change style
W_NoSC		;		

		lda W_StyleFl
		beq NoSCSt

		shortm
		lda W_NewInvStyle
		ora W_NewStyle
		beq DoSto
		lda W_NewInvStyle
		eor #$ff
		and SFont2+2
		ora W_NewStyle
DoSto	sta SFont2+2

		lda W_NewInvStyle
		ora W_NewStyle
		beq DoSto2
		lda W_NewInvStyle
		eor #$ff
		and EFont1+2
		ora W_NewStyle
DoSto2	sta EFont1+2

		lda W_NewInvStyle
		xba
		lda W_NewStyle
		longm
		tay

		pushword #2
		pushword #2
		phy
		jsl W_DoChange

;here's where I change color

NoSCSt

		lda W_ColorFl
		beq NoClCh

		moveword W_NewColor,SColor2
		sta EColor1

		pushword #1
		pushword #4
		pushword W_NewColor
		jsl W_DoChange

;Now D_Write the beginning and end bytes
NoClCh
		lda EFlag
		bne DoneEnd

		pushword W_EndPar
		pushword EOffset1
		pha
		pushlong EFont1
		pushword EColor1
		pushlong EFont2
		pushword EColor2
		jsl W_CalcFBytes		   			;room = bytes-(EOffset2-EOffset1)
		addword s,EOffset1,W_EndOffset
		subword a,EOffset2,s
		jsl W_DoRoom

		lda W_EndPar
		ldx EOffset1
		jsl W_GetAddr
		phx
		pha
		pushlong EFont1
		pushword EColor1
		pushlong EFont2
		pushword EColor2
		jsl W_WriteFBytes

DoneEnd
		moveword #W_TextHeader,W_StartOffset

		lda SFlag
		bne Valid

		pushword W_StartPar
		pushword SOffset1
		pha
		pushlong SFont1
		pushword SColor1
		pushlong SFont2
		pushword SColor2
		jsl W_CalcFBytes		   ;room = bytes-(SOffset2-SOffset1)
		addword s,SOffset1,W_StartOffset
		subword a,SOffset2,s
		sta SFlag
		jsl W_DoRoom

		cmpw W_StartPar,W_EndPar
		bne NotSame
		addword SFlag,W_EndOffset,W_EndOffset
NotSame

		lda W_StartPar
		ldx SOffset1
		jsl W_GetAddr
		phx
		pha
		pushlong SFont1
		pushword SColor1
		pushlong SFont2
		pushword SColor2
		jsl W_WriteFBytes

Valid
		moveword W_StartPar,CurPar
ValidLoop
		jsl D_BeachBall

		lda CurPar
		jsl W_GetParRec
		movelong ax,W_ParRec

		pushword Curpar
		jsl W_MakeLines

		cmpw [W_ParRec]:#W_pAttr,#W_PgBrk
		beq	@next
		
		pushlong [W_ParRec]:#W_pLineHand
		_Disposehandle

@next	movelong #0,[W_ParRec]:#W_pLineHand

		inc CurPar
		cmpw W_EndPar,CurPar
		bcs ValidLoop

; end of valid loop
		pushlong !Q_Rect
		jsl W_CalcDocRect

		jsl W_SetDocRect

		pushlong !Q_Rect
		_EraseRect

		jsl W_CalcNewLines

		jsl W_UpdateText

		jsl W_CalcPages

		jsl W_SetFullREct

		jsl W_KillChanges

		jsl W_CalcFonts

TCExit
		return

		ENDP


****************************************************************
W_CalcNewLines	PROC		EXPORT
			;Using wpglobals

		begin

		pha
		pushword W_StartPar
		pushword W_StartOffset
		jsl W_FindLine
		pullword W_StartLine

		lda W_Selected
		beq NoSel

		pha
		pushword W_EndPar
		pushword W_EndOffset
		jsl W_FindLine
		pullword W_EndLine

NoSel
		return

		ENDP

****************************************************************
W_CalcFonts	PROC		EXPORT
			;Using wpglobals

		local CurPar:w,W_ParRec:l,ChrPtr:l,EndY:w
		local StartPar2:w,StartLine2:w,StartOffset2:w
		local PBTop:w,PbParRec:l,Style2:w,OffTemp:w
		begin

		stz Style2

		lda W_Selected
		bne Doselstuff

		spacelong
		spaceword
		pushword W_StartPar
		pushword W_StartLine
		pha
		pushword W_StartPar
		pushword W_StartLine
		pushword W_StartOffset
		jsl W_JunkRight
		jsl W_FindFont
		pullword W_WPColor
		pullLong W_WpFont

		brl GotFont

DoSelStuff
		stz PBTop

		moveword W_StartPar,StartPar2
		moveword W_StartLine,StartLine2
		moveword W_StartOffset,StartOffset2

		lda StartPar2
		jsl W_GetParRec
		movelong ax,PBParRec

		cmpw [PBParRec]:#W_pAttr,#W_PgBrk
		bne NoPBTop

		inc StartPar2
		stz StartLine2
		moveword #W_TextHeader,StartOffset2

		inc PBTop

NoPBTop
		pha
		pushword StartPar2
		pushword StartLine2
		pushword StartOffset2
		jsl W_JunkLeft
		pullword OffTemp

		pha
		pushword StartPar2
		pushword StartLine2
		pushword StartOffset2
		jsl W_JunkRight
		pullword StartOffset2

		spacelong
		spaceword
		spacelong
		spaceword
		pushword StartPar2
		pushword StartLine2
		pushword OffTemp
		jsl W_FindFont
		lda StartPar2
		ldx OffTemp
		jsl W_GetAddr
		phx
		pha
		lda StartPar2
		ldx StartOffset2
		jsl W_GetAddr
		phx
		pha
		jsl W_ReadFont
		pullword W_WPColor
		pulllong W_WpFont

		stz Style2
		movebyte W_WpStyle,Style2

		pha
		pushword W_EndPar
		pushword W_EndLine
		pushword W_EndOffset
		jsl W_JunkLeft
		pullword W_EndOffset

		moveword StartPar2,CurPar
		ldx #0
		jsl W_GetAddr
		movelong ax,ChrPtr

		ldy StartOffset2
		bra CheckEnd

ParLoop
		longm
		cmpw CurPar,W_EndPar
		jcs GotFont
		inc a
		sta CurPar
		jsl W_GetParRec
		movelong ax,PBParRec

		cmpw [PBParRec]:#W_pAttr,#W_PgBrk
		beq ParLoop

		lda CurPar
		ldx #0
		jsl W_GetAddr
		movelong ax,ChrPtr

		shortm
		ldy #$ffff
		jsr FChange
		jsr StyleChange
		jsr SizeChange
		jsr ColorChange
		longm

		ldy #W_TextHeader		  ;be careful!!!!!!!

		moveword #$ffff,EndY
CheckEnd	cmpw CurPar,W_EndPar
		bne StChrLoop
		moveword W_EndOffset,EndY

StChrLoop
		shortm
ChrLoop
		lda [ChrPtr],y
		cmp #sp
		bcs SkipChar
		cmp #cr
		beq ParLoop
		xba
		lda #0
		xba
		asl a
		tax
		jsr (ChrTable,x)

SkipChar	iny
		cpy EndY
		bcc ChrLoop

GotFont
		shortm

		lda W_WpStyle
		bne W_GotStyle
		lda Style2
		beq W_GotStyle
		moveword #$ff,W_WpStyle
W_GotStyle
		longm

		lda PBTop
		bne NoPut

		moveword StartOffset2,W_StartOffset

NoPut

		stz W_WPFontFlag
		pushlong W_WpFont
		pushword W_WPColor
		jsl D_CheckMenuFID

		return




ChrTable
	DC.W	0,fChange				;0,1
	DC.W	StyleChange,SizeChange	;2,3
	DC.W	ColorChange,GS			;4,5
	DC.W	GS,GS					;6,7
	DC.W	0,GS					;8,9

FChange
		iny
		longm
		lda [ChrPtr],y
		cmp W_WpFont
		beq GF
		moveword #$ffff,W_WpFont
GF	shortm
		iny
		rts

SizeChange
		iny
		lda [ChrPtr],y
		cmp W_WPSize
		beq GS
		moveword #$ff,W_WPSize
GS	rts

StyleChange
		iny
		lda [ChrPtr],y
		and W_WpStyle
		sta W_WpStyle
		lda [ChrPtr],y
		ora Style2
		sta Style2
GSt	rts

ColorChange
		iny
		lda [ChrPtr],y
		cmp W_WPColor
		beq GSC
		longm
		moveword #$ff,W_WPColor
		shortm
GSC	rts

			longa on
			longi on

		ENDP




****************************************************************
* W_ChangeAll - the font dialog was chosen
W_ChangeAll	PROC		EXPORT
			;Using wpglobals

		input FontID:l
		begin +b

		pushlong W_CurDoc
		_SetPort

		jsl D_KillFont

		lda W_Selected
		bne IsSel

		moveword #1,W_WPFontFlag
		movelong FontID,W_WpFont

		pushlong W_WpFont
		pushword W_WPColor
		jsl D_CheckMenuFID

		bra WDCExit

IsSel

		moveword #1,W_FontFl
		sta W_StyleFl
		sta W_SizeFl
		moveword FontID,W_NewFont
		stz W_NewSize
		stz W_NewStyle
		shortm
		moveword FontID+2,W_NewStyle
		moveword FontID+3,W_NewSize
		longm

		jsl W_TextChange

WDCExit
		pushlong W_CurDoc
		_BeginUpdate

		pushlong W_CurDoc
		jsl W_UpdateDoc

		pushlong W_CurDoc
		_EndUpdate

		return

		ENDP


****************************************************************
* W_DoFonts - changes to the font
W_DoFonts	PROC		EXPORT
			;Using wpglobals

		input font:w
		begin +b

		pushlong W_CurDoc
		_SetPort

		lda W_Selected
		bne IsSel

		moveword #1,W_WPFontFlag
		moveword font,W_WpFont

		pushlong W_WpFont
		pushword W_WPColor
		jsl D_CheckMenuFID

		bra WDCExit

IsSel

		moveword #1,W_FontFl
		moveword Font,W_NewFont

		jsl W_TextChange

WDCExit
		return

		ENDP



****************************************************************
* W_GotSize - a D_New size was chosen on the size menu
W_GotSize	PROC		EXPORT
			;Using wpglobals
		input size:w
		begin +b

		pushlong W_CurDoc
		_SetPort

		lda W_Selected
		bne IsSel

		moveword #1,W_WPFontFlag
		movebyte size,W_WPSize

		pushlong W_WpFont
		pushword W_WPColor
		jsl D_CheckMenuFID

		bra WDCExit

IsSel

		moveword #1,W_SizeFl
		moveword size,W_NewSize

		jsl W_TextChange

WDCExit
		return

		ENDP


****************************************************************
* W_GotStyle - a D_New size was chosen on the size menu
W_GotStyle	PROC		EXPORT
			;Using wpglobals
		input style:w
		begin +b

		pushlong W_CurDoc
		_SetPort

		lda W_Selected
		bne IsSel

		moveword #1,W_WPFontFlag
		shortm
		lda style
		beq DoSto
		eor W_WpStyle
DoSto	sta W_WpStyle
		lda style
		and W_WpStyle
		and #SuperScript
		beq NoSup

		lda W_WpStyle
		and #$ff-SubScript
		sta W_WpStyle
		bra NoSub

NoSup
		lda style
		and W_WpStyle
		and #SubScript
		beq NoSub

		lda W_WpStyle
		and #$ff-SuperScript
		sta W_WpStyle
NoSub
		longm

		pushlong W_WpFont
		pushword W_WPColor
		jsl D_CheckMenuFID

		bra WDCExit

IsSel
		stz W_NewInvStyle
		stz W_NewStyle

		moveword #1,W_StyleFl
		shortm
		lda W_WpStyle
		cmp #$ff
		beq DoOr
		and style
		beq DoOr
		moveword style,W_NewInvStyle
		bra SetPars

DoOr
		moveword style,W_NewStyle

SetPars
		lda W_NewStyle
		and #SuperScript
		beq NoSup2
		lda W_NewInvStyle
		ora #SubScript
		sta W_NewInvStyle
		bra NoSub2

NoSup2
		lda W_NewStyle
		and #SubScript
		beq NoSub2
		lda W_NewInvStyle
		ora #SuperScript
		sta W_NewInvStyle

NoSub2
		longm
		jsl W_TextChange

WDCExit
		return

		ENDP



****************************************************************
* DoColor - does nothing with the color
W_WpDoColors	PROC		EXPORT
			;Using wpglobals

		input color:w
		begin +b

		pushlong W_CurDoc
		_SetPort

		lda W_Selected
		bne IsSel

		moveword #1,W_WPFontFlag
		moveword color,W_WPColor

		pushlong W_WpFont
		pushword W_WPColor
		jsl D_CheckMenuFID

		bra WDCExit

IsSel
		moveword #1,W_ColorFl
		moveword color,W_NewColor

		jsl W_TextChange

WDCExit
		return

		ENDP





****************************************************************
W_DoChange	PROC		EXPORT
			;Using wpglobals

		input byteFl:w,flag:w,data:w
		local CurPar:w,W_ParRec:l,ChrPtr:l,EndY:w,OldRt:w,ParRec2:l
		begin

		lda flag
		asl a
		tax
		moveword ChrTable:x,OldRt

		lda #ByteMod
		ldy ByteFl
		bne GotRt
		lda #WordMod
GotRt	sta ChrTable,x

		moveword W_StartPar,CurPar
		ldx #0
		jsl W_GetAddr
		movelong ax,ChrPtr

		ldy W_StartOffset
		beq atst
		bra CheckEnd

ParLoop
		longm
		inc CurPar
atst	cmpw W_EndPar,CurPar
		jcc GotFont
		lda CurPar
		jsl W_GetParRec
		movelong ax,ParRec2

		cmpw [ParRec2]:#W_pAttr,#W_PgBrk
		jeq ParLoop

		lda CurPar
		ldx #0
		jsl W_GetAddr
		movelong ax,ChrPtr

		lda flag
		asl a
		tax
		lda HdTable,x
		tay

		lda data
		ldx bytefl
		beq NoByte
		shortm
		cpx #1
		beq NoByte
		lda data+1
		ora data
		beq NoByte
		lda data+1
		eor #$ff
		and [ChrPtr],y
		ora data
NoByte	sta [ChrPtr],y
		longm

		ldy #W_TextHeader		  ;be careful!!!!!!!

CheckEnd	moveword #$ffff,EndY
		cmpw CurPar,W_EndPar
		bne StChrLoop
		moveword W_EndOffset,EndY

StChrLoop
		shortm
ChrLoop
		lda [ChrPtr],y
		cmp #sp
		bcs SkipChar
		cmp #cr
		jeq ParLoop
		xba
		lda #0
		xba
		asl a
		tax
		jsr (ChrTable,x)

SkipChar	iny
		cpy EndY
		bcc ChrLoop

GotFont
		longm

		lda flag
		asl a
		tax
		moveword OldRt,ChrTable:x

		return


HdTable
	DC.W	0,0,2,3,4


ChrTable
	DC.W	0,Skip2	;0,1
	DC.W	Skip1,Skip1	;2,3
	DC.W	Skip1,NoSkip	;4,5
	DC.W	NoSkip,NoSkip	;6,7
	DC.W	0,NoSkip	;8,9

Skip2	iny
Skip1	iny
NoSkip	rts

ByteMod
		shortm
		iny
		lda bytefl
		cmp #2
		bne StStore
		lda data
		ora data+1
		beq StStore

		lda data+1
		eor #$ff
		and [ChrPtr],y
		ora data
		sta [ChrPtr],y
		rts

StStore
		lda data
		sta [ChrPtr],y
		rts


WordMod
		iny
		longm
		lda data
		sta [ChrPtr],y
		shortm
		iny
		rts

			longa on
			longi on

		ENDP



****************************************************************
W_KillChanges	PROC		EXPORT
			;Using wpglobals
		begin

		stz W_WPFontFlag
		stz W_FontFl
		stz W_SizeFl
		stz W_StyleFl
		stz W_ColorFl
		stz W_NewInvStyle

		return

		ENDP
		END

