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

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

	IMPORT	D_CheckMenuFID
	IMPORT	D_CurColor
	IMPORT	D_CurMenuFont
	IMPORT	D_DisableMItems
	IMPORT	D_EnableMItems
	IMPORT	P_BuildUndo
	IMPORT	P_CurrentWin
	IMPORT	P_DPTempl
	IMPORT	P_DblPage
	IMPORT	P_DefColor
	IMPORT	P_DefFont
	IMPORT	P_DefJust
	IMPORT	P_DefSize
	IMPORT	P_DefSpacing
	IMPORT	P_DefStyle
	IMPORT	P_DoSpacingDlog
	IMPORT	P_ForceUpdate
	IMPORT	P_HGuides
	IMPORT	P_HPalette
	IMPORT	P_HRulers
	IMPORT	P_IsObjSel
	IMPORT	P_LGuides
	IMPORT	P_LockGuides
	IMPORT	P_MagGuides
	IMPORT	P_NPage
	IMPORT	P_NTemplate
	IMPORT	P_NotActive
	IMPORT	P_Page
	IMPORT	P_PageCount
	IMPORT	P_PageMode
	IMPORT	P_SGuides
	IMPORT	P_SPTempl
	IMPORT	P_SPalette
	IMPORT	P_SRulers
	IMPORT	P_SetObjAttr
	IMPORT	P_ULGuides
	IMPORT	P_UTemplate
	IMPORT	P_UseTemplate
	IMPORT	P_VPage
	IMPORT	P_VisGuides
	IMPORT	P_VisPalette
	IMPORT	P_VisRuler
	IMPORT	T_ChangeFont
	IMPORT	T_ChangeRuls
	IMPORT	T_DotParag
	IMPORT	T_Editing
	IMPORT	T_FontFlags
	IMPORT	T_GetRulBits
	IMPORT	T_NewAddStyle
	IMPORT	T_NewColor
	IMPORT	T_NewFont
	IMPORT	T_NewSize
	IMPORT	T_NewSubStyle
	IMPORT	T_RangeFont
	IMPORT	T_TextSelect
	IMPORT	D_TextSelectOff
	IMPORT	D_TextSelectOn

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

	ENTRY	P_ChangeJust
	ENTRY	P_ChangeSpace
	ENTRY	P_GetMJust
	ENTRY	P_GetMSpacing
	ENTRY	P_SetFormat





;---------------------------------------------------------------------------
;
; P_FixSpecMenu()
;
; Do miscellaneous menu checking / hilighting / Q_Text changing
;

P_FixSpecMenu	PROC		EXPORT
			;Using P_AttrData
			;Using P_Data
		begin

		; Fit in window / Normal view
		lda P_PageMode
		bne GoFull
		pushlong #P_VPage
		bra DoTempl
GoFull	pushlong #P_NPage

DoTempl	tool _SetMItem,in=(:l,#P_ViewPageItem:w)

		; Show / hide guides
DoVisG	lda P_VisGuides
		bne MakeVisG

		call D_DisableMItems,in=(#P_LockGuideBit:w,#P_MenuOptions:w)
		call D_DisableMItems,in=(#P_MagGuideBit:w,#P_MenuOptions:w)
		pushlong #P_SGuides
		bra Lock

MakeVisG	call D_EnableMItems,in=(#P_LockGuideBit:w,#P_MenuOptions:w)
		call D_EnableMItems,in=(#P_MagGuideBit:w,#P_MenuOptions:w)
		pushlong #P_HGuides


		; Lock / D_UnLock guides
Lock	tool _SetMItem,in=(:l,#P_HideGuideItem:w)
		lda P_LockGuides
		bne Lockit

		pushlong #P_LGuides
		bra DoVisR

Lockit	pushlong #P_ULGuides

		; Show / Hide ruler
DoVisR	tool _SetMItem,in=(:l,#P_LockGuideItem:w)
		lda P_VisRuler
		bne MakeVisR

		pushlong #P_SRulers
		bra DoVisP

MakeVisR	pushlong #P_HRulers

		; Show / hide palette
DoVisP	tool _SetMItem,in=(:l,#P_HideRulerItem:w)
		lda P_VisPalette
		bne MakeVisP

		pushlong #P_SPalette
		bra CkMag

MakeVisP	pushlong #P_HPalette

CkMag	tool _SetMItem,in=(:l,#P_HidePaletteItem:w)


		; Check / Uncheck magnetic guides
		lda P_MagGuides
		bne MakeMag

		pushword #0
		bra GotMag

MakeMag	pushword #''

GotMag	tool _SetMItemMark,in=(:w,#P_MagGuideItem:w)

		; Enable / disable Insert Page(s)
		cmpw P_PageCount,#P_MaxPages
		blt EnableInsert

		call D_DisableMItems,in=(#P_InsertBit:w,#P_MenuEdit:w)
		bra DidInsert
EnableInsert	call D_EnableMItems,in=(#P_InsertBit:w,#P_MenuEdit:w)


		; (Don't) Use Master Page
DidInsert	lda P_DblPage
		bne GoSingle

		pushlong #P_DPTempl
		bra DoUse

GoSingle	pushlong #P_SPTempl

DoUse	tool _SetMItem,in=(:l,#P_DblPageItem:w)

		lda P_UseTemplate
		bne DontUse

		pushlong #P_UTemplate
		bra exit

DontUse	pushlong #P_NTemplate

exit	tool _SetMItem,in=(:l,#P_UseTempItem:w)
		cmpw P_Page,#2
		bge DoEnable
		call D_DisableMItems,in=(#P_UseTempBit:w,#P_MenuOptions:w)
		bra DoCalc

DoEnable	call D_EnableMItems,in=(#P_UseTempBit:w,#P_MenuOptions:w)
DoCalc	tool _CalcMenuSize,in=(#0:w,#0:w,#4:w)

		return
		ENDP




;---------------------------------------------------------------------------
;
; P_FixMenus ()
;

P_FixMenus	PROC		EXPORT
			;Using P_Data
			;Using P_AttrData
			;Using T_Data

		LOCAL MenuFontID:l

		BEGIN

		lda P_NotActive
		jne Exit

		lda T_Editing
		beq NotActive

		lda T_TextSelect
		beq @NoSelect
		jsl D_TextSelectOn
		bra Range

@NoSelect	
		jsl D_TextSelectOff

Range	jsl T_RangeFont
		brl Exit



NotActive
		jsl D_TextSelectOff

		moveword P_DefFont,MenuFontID
		shortm
		moveword P_DefStyle,MenuFontID+2
		moveword P_DefSize,MenuFontID+3
		longm
		call D_CheckMenuFID,in=(MenuFontID:l,P_DefColor:w)

		call P_SetFormat,in=(P_DefJust:w,P_DefSpacing:w)
		call P_IsObjSel,out=(a:w)
		beq TurnOff

		call D_EnableMItems,in=(#P_ObjArrange:w,#P_MenuArrange:w)
		call D_EnableMItems,in=(#P_ObjEdit:w,#P_MenuEdit:w)
		bra exit

TurnOff
		call D_DisableMItems,in=(#P_ObjArrange:w,#P_MenuArrange:w)
		call D_DisableMItems,in=(#P_ObjEdit:w,#P_MenuEdit:w)

Exit	RETURN
		ENDP






;------------------------------------------------------------------------------
;
; P_SetFormat (Just:w,Spacing:w)
;

P_SetFormat	PROC		EXPORT
			;Using P_Data
			;Using P_AttrData

		INPUT Just:w,Spacing:w

		BEGIN

		tool _SetMItemMark,in=(#0:w,#P_LeftItem:w)
		tool _SetMItemMark,in=(#0:w,#P_CenterItem:w)
		tool _SetMItemMark,in=(#0:w,#P_RightItem:w)
		tool _SetMItemMark,in=(#0:w,#P_FullItem:w)

		lda Just
		beq NoJust
		pushword #''
		jsl P_GetMJust
		addword a,#P_LeftItem,s
		_SetMItemMark
NoJust


		tool _SetMItemMark,in=(#0:w,#P_SingleItem:w)
		tool _SetMItemMark,in=(#0:w,#P_DoubleItem:w)
		tool _SetMItemMark,in=(#0:w,#P_TripleItem:w)
		tool _SetMItemMark,in=(#0:w,#P_SetSItem:w)

		lda Spacing
		beq NoSpacing
		pushword #''
		jsl P_GetMSpacing
		addword a,#P_SingleItem,s
		_SetMItemMark
NoSpacing

		RETURN
		ENDP





;-------------------------------------------------------------------------------
;
; P_GetMJust (Just:a) : MenuJust:a
;

P_GetMJust	PROC		EXPORT
			;Using T_TextEqu


		ldx #0
		tay

		and #T_Left
		bne Exit
		inx
		tya

		and #T_Center
		bne Exit
		inx
		tya

		and #T_Right
		bne Exit
		inx

Exit	txa
		rtl
		ENDP



;-------------------------------------------------------------------------------
;
; P_GetMSpacing (Spacing:a) : MenuSpacing:a
;


P_GetMSpacing	PROC		EXPORT
			;Using T_TextEqu

		tay
		and #T_CustSpace
		bne Custom

		ldx #0
		tya
		and #T_Single
		bne Exit

		inx
		tya
		and #T_Double
		bne Exit

		inx

Exit	txa
		rtl

Custom	lda #3
		rtl

		ENDP





;---------------------------------------------------------------------------
;
; P_Left	  
;

P_Left	PROC		EXPORT
			;Using P_AttrData
			;Using T_TextEqu

		begin

		pushword #T_Left
		jsl P_ChangeJust

		return
		ENDP



;---------------------------------------------------------------------------
;
; P_Center
;

P_Center	PROC		EXPORT
			;Using P_AttrData
			;Using T_TextEqu

		begin

		pushword #T_Center
		jsl P_ChangeJust

		return
		ENDP




;---------------------------------------------------------------------------
;
; P_Right	 
;

P_Right	PROC		EXPORT
			;Using P_AttrData
			;Using T_TextEqu

		begin

		pushword #T_Right
		jsl P_ChangeJust

		return
		ENDP




;---------------------------------------------------------------------------
;
; P_Full
;

P_Full	PROC		EXPORT
			;Using P_AttrData
			;Using T_TextEqu

		begin

		pushword #T_Full
		jsl P_ChangeJust

		return
		ENDP




;-------------------------------------------------------------------------------
;
; P_ChangeJust (Just:w)
;


P_ChangeJust	PROC		EXPORT
			;Using P_DATA
			;Using T_Data
			;Using T_TextEqu

		INPUT Just:w
		BEGIN +b

		lda T_Editing
		beq SetDefault

		call T_ChangeRuls,in=(#T_ChangeJust:w,Just:w,#0:l)
		bra DoFix

SetDefault	moveword Just,P_DefJust

DoFix	jsl  P_FixMenus

		RETURN
		ENDP





;---------------------------------------------------------------------------
;
; P_SingleSpace 
;

P_SingleSpace	PROC		EXPORT
			;Using T_TextEqu

		begin

		call P_ChangeSpace,in=(#T_Single:w)

		return
		ENDP





;---------------------------------------------------------------------------
;
; P_DoubleSpace  
;

P_DoubleSpace	PROC		EXPORT
			;Using T_TextEqu

		begin

		call P_ChangeSpace,in=(#T_Double:w)

		return
		ENDP



;---------------------------------------------------------------------------
;
; P_TripleSpace 
;

P_TripleSpace	PROC		EXPORT
			;Using T_TextEqu

		begin

		call P_ChangeSpace,in=(#T_Triple:w)

		return
		ENDP




;---------------------------------------------------------------------------
;
; P_SetSpacing 
;

P_SetSpacing	PROC		EXPORT
			;Using T_TextEqu
			;Using P_Data
			;Using T_Data

		LOCAL LineHeight:w,Spacing:w

		BEGIN +b

		call P_ForceUpdate,in=(P_CurrentWin:l)

		moveword #16,LineHeight
		moveword P_DefSpacing,Spacing

		lda T_Editing
		beq UseDefault
		rcall T_GetRulBits,in=(T_DotParag:a),out=(Spacing:a)
UseDefault

		lda Spacing
		and #T_CustSpace
		beq DoCall
		lda Spacing
		xba
		and #T_GetCustSpace
		inc a
		sta LineHeight


DoCall
		call P_DoSpacingDlog,in=(LineHeight:w),out=(a:w)
		beq Exit

		dec a
		xba
		ora #T_CustSpace
		call P_ChangeSpace,in=(a:w)

Exit	return
		ENDP





;------------------------------------------------------------------------------
;
; P_ChangeSpace (Spacing:w)
;

P_ChangeSpace	PROC		EXPORT
			;Using P_Data
			;Using T_Data
			;Using T_TextEqu

		INPUT Spacing:w
		BEGIN +b

		lda T_Editing
		beq SetDefault

		call T_ChangeRuls,in=(#T_ChangeSpace:w,Spacing:w,#0:l)
		bra DoFix

SetDefault	moveword Spacing,P_DefSpacing

DoFix	jsl P_FixMenus

		RETURN
		ENDP




;-------------------------------------------------------------------------------
;
; P_DoFont (NewFont:w)
;

P_DoFont	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData
			;Using T_Data

		INPUT Font:w
		BEGIN +b

		lda T_Editing
		beq SetDefault

		lda T_FontFlags
		ora #T_FontFl
		sta T_FontFlags

		moveword Font,T_NewFont

		lda T_TextSelect
		beq FixIt

		jsl T_ChangeFont
		bra FixIt


SetDefault	moveword Font,P_DefFont
		call P_IsObjSel,out=(a:w)
		beq FixIt
		call P_BuildUndo,in=(#P_FontUndo:w)
		call P_SetObjAttr,in=(#P_FontAttr:w,P_DefFont:w)
FixIt	jsl P_FixMenus

		RETURN
		ENDP




;-------------------------------------------------------------------------------
;
; P_ChangeAll (FontID:l)
;

P_ChangeAll	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData
			;Using T_Data

		INPUT FontID:l

		BEGIN +b

		call P_ForceUpdate,in=(P_CurrentWin:l)

		lda T_Editing
		beq SetDefault

		lda T_FontFlags
		ora #T_FontFl
		ora #T_StyleFl
		ora #T_SizeFl
		sta T_FontFlags

		moveword FontID,T_NewFont
		shortm
		moveword FontID+2,T_NewAddStyle
		moveword FontID+3,T_NewSize
		longm
		stz T_NewSubStyle

		lda T_TextSelect
		beq FixIt

		jsl T_ChangeFont
		bra FixIt


SetDefault
		moveword FontID,P_DefFont
		shortm
		moveword FontID+2,P_DefStyle
		moveword FontID+3,P_DefSize
		longm

		call P_IsObjSel,out=(a:w)
		beq FixIt

		call P_BuildUndo,in=(#P_FontIDUndo:w)
		call P_SetObjAttr,in=(#P_FontAttr:w,P_DefFont:w)
		call P_SetObjAttr,in=(#P_StyleAttr:w,P_DefStyle:w)
		call P_SetObjAttr,in=(#P_SizeAttr:w,P_DefSize:w)

FixIt	jsl P_FixMenus

		RETURN
		ENDP




;------------------------------------------------------------------------------
;
; P_DoStyle (Style:w)
;

P_DoStyle	PROC		EXPORT
			;Using D_MenuData
			;Using P_Data
			;Using P_ObjData
			;Using T_Data

		INPUT Style:w
		LOCAL MenuStyle:w,NewStyle:w,StyleAdd:w,StyleSub:w

		BEGIN +b

;NoSub	equ  $FF-SubScript
;NoSuper	equ  $FF-SuperScript

		lda >D_CurMenuFont+2
		and #$ff
		sta MenuStyle


		lda T_Editing
		beq @NoEdit

		lda T_FontFlags
		ora #T_StyleFl
		sta T_FontFlags

		lda T_TextSelect
		jne Selected
@NoEdit


; No Q_Text is selected
		stz NewStyle
		lda Style
		jeq NotSub

		lda MenuStyle
		eor Style
		sta NewStyle

		lda Style
		and #SuperScript
		beq NotSuper
		lda NewStyle
;		and #NoSub
		and #$FF-SubScript
		sta NewStyle
NotSuper

		lda Style
		and #SubScript
		beq NotSub
		lda NewStyle
;		and #NoSuper
		and #$FF-SuperScript
		sta NewStyle
NotSub

		moveword NewStyle,T_NewAddStyle
		lda T_Editing
		jne FixIt


; Set default style

		moveword NewStyle,P_DefStyle
		call P_IsObjSel,out=(a:w)
		beq NoObj
		call P_BuildUndo,in=(#P_StyleUndo:w)
		call P_SetObjAttr,in=(#P_StyleAttr:w,P_DefStyle:w)
NoObj
		brl FixIt



Selected
		stz StyleAdd
		stz StyleSub

		lda Style
		beq NotSub2

		cmpw MenuStyle,#$ff
		beq SetOn
		and Style
		sta StyleSub

SetOn
		lda Style
		eor StyleSub
		sta StyleAdd

		and #SuperScript
		beq NotSuper2
		lda StyleSub
		ora #SubScript
		sta StyleSub
NotSuper2

		lda StyleAdd
		and #SubScript
		beq NotSub2
		lda StyleSub
		ora #SuperScript
		sta StyleSub
NotSub2

		moveword StyleAdd,T_NewAddStyle
		moveword StyleSub,T_NewSubStyle
		jsl T_ChangeFont


FixIt	jsl P_FixMenus

		RETURN
		ENDP





;------------------------------------------------------------------------------
;
; P_DoSize (Size:w)
;

P_DoSize	PROC		EXPORT
			;Using P_Data
			;Using P_ObjData
			;Using T_Data


		INPUT Size:w
		BEGIN +b

		lda T_Editing
		beq SetDefault

		lda T_FontFlags
		ora #T_SizeFl
		sta T_FontFlags

		movebyte Size,T_NewSize

		lda T_TextSelect
		beq FixIt

		jsl T_ChangeFont
		bra FixIt


SetDefault
		moveword Size,P_DefSize
		call P_IsObjSel,out=(a:w)
		beq FixIt
		call P_BuildUndo,in=(#P_SizeUndo:w)
		call P_SetObjAttr,in=(#P_SizeAttr:w,P_DefSize:w)

FixIt	jsl P_FixMenus

		RETURN
		ENDP




;---------------------------------------------------------------------------
;
; P_DoColor (Color:w)
;

P_DoColor	PROC		EXPORT
			;Using P_Data
			;Using T_Data
			;Using P_ObjData
			;Using D_MenuData

		LOCAL Ptr:l
		INPUT Color:w

		BEGIN +b

		moveword Color,>D_CurColor

		lda T_Editing
		beq TryObjects

		lda T_FontFlags
		ora #T_ColorFl
		sta T_FontFlags
		moveword Color,T_NewColor

		lda T_TextSelect
		beq @NoSelect

		jsl T_ChangeFont
		
@NoSelect
		bra Exit



TryObjects
		moveword Color,P_DefColor
		call P_IsObjSel,out=(a:w)
		beq Exit
		call P_BuildUndo,in=(#P_ColorUndo:w)
		call P_SetObjAttr,in=(#P_ColorAttr:w,Color:w)


Exit	RETURN
		ENDP
		END

