
		LOAD		'Macros.dump'

		INCLUDE	'SS.equ'

		INCLUDE	'Driver.equ'
		INCLUDE	'Heap.aii.i'

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

		IMPORT	D_AlertBox
		IMPORT	D_DisableMItems
		IMPORT	D_EnableMItems
		IMPORT	D_FastMult
		IMPORT	D_MemoryError

		IMPORT	S_ActiveWindow
		IMPORT	S_BuildColWdUndo
		IMPORT	S_CalcAutoStr
		IMPORT	S_CalcManStr
		IMPORT	S_CalculateSheet
		IMPORT	S_CellWidth
		IMPORT	S_ChangeDef
		IMPORT	S_ColLabRect
		IMPORT	S_CurBRSelect
		IMPORT	S_CurBRTitle
		IMPORT	S_CurBScrl
		IMPORT	S_CurContHt
		IMPORT	S_CurContRect
		IMPORT	S_CurContWd
		IMPORT	S_CurContXpt
		IMPORT	S_CurContYpt
		IMPORT	S_CurDefColWd
		IMPORT	S_CurEditFlag
		IMPORT	S_CurMaxTLCell
		IMPORT	S_CurRScrl
		IMPORT	S_CurTLCell
		IMPORT	S_CurTLSelect
		IMPORT	S_CurTLTitle
		IMPORT	S_CurVPageSize
		IMPORT	S_DoDefaultWidthDlog
		IMPORT	S_HideGridStr
		IMPORT	S_RestoreWidth
		IMPORT	S_RowLabRect
		IMPORT	S_ScrlUpdate
		IMPORT	S_SetMaxTLCell
		IMPORT	S_SetUndoOff
		IMPORT	S_SetUndoOn
		IMPORT	S_SetVertLines
		IMPORT	S_ShowGridStr
		IMPORT	S_SwapIn
		IMPORT	S_TitleDialog
		IMPORT	S_TitleTooBigStr
		IMPORT	S_TraverseRange
		IMPORT	S_ViewDataStr
		IMPORT	S_ViewFormStr

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

	ENTRY	S_ActivateScrlBars
	ENTRY	S_AdjustSheetColumns
	ENTRY	S_AdjustTitleOffset
	ENTRY	S_RefreshScreen

;---------------------------------------------------------------------------
;
;

S_ChangeCalcMode	PROC		EXPORT
				;Using	S_CurrentData
				;Using	S_AttrData

			BEGIN	+b

			Call		S_SwapIn,in=(S_ActiveWindow:l)
			Call		S_SetUndoOff

			lda		S_CurEditFlag
			eor		#S_ManCalcBit
			sta		S_CurEditFlag

			and		#S_ManCalcBit
			beq		resetCalc

			Tool		_SetMItemName,in=(#S_CalcAutoStr:l,#S_CalculateItem:w)
			Call		D_EnableMItems,in=(#S_RecalcMI:w,#5:w)
			bra		Exit

resetCalc
			Call		S_CalculateSheet

			Tool		_SetMItemName,in=(#S_CalcManStr:l,#S_CalculateItem:w)
			Call		D_DisableMItems,in=(#S_RecalcMI:w,#5:w)

Exit
			RETURN
			ENDP

;---------------------------------------------------------------------------
;
;

S_Recalc		PROC		EXPORT
			;Using	S_CurrentData

			BEGIN	+b

			Call		S_SwapIn,in=(S_ActiveWindow:l)
			Call		S_SetUndoOff

			Call		S_CalculateSheet

			RETURN
			ENDP

;---------------------------------------------------------------------------
;
;

S_ChangeView	PROC		EXPORT
			;Using	S_CurrentData
			;Using	S_AttrData

			BEGIN	+b

			Call		S_SwapIn,in=(S_ActiveWindow:l)
			Call		S_SetUndoOff

			lda		S_CurEditFlag
			eor		#S_ViewFormulaBit
			sta		S_CurEditFlag

			and		#S_ViewFormulaBit
			beq		resetView

			Tool		_SetMItemName,in=(#S_ViewDataStr:l,#S_ViewItem:w)
			bra		redraw

resetView
			Tool		_SetMItemName,in=(#S_ViewFormStr:l,#S_ViewItem:w)

redraw
			Call		S_RefreshScreen,in=(#0:w)

			RETURN
			ENDP

;---------------------------------------------------------------------------
;
;

S_ChangeDefWidth	PROC		EXPORT
				;Using	S_CurrentData

			local	NewDefColWd:w
			error	ErrFlag
			BEGIN	+b

			stz		ErrFlag

			Call		S_SwapIn,in=(S_ActiveWindow:l)
			Call		S_DoDefaultWidthDlog,in=(S_CurDefColWd:w),out=(a:w)
			bcs		Exit

			sta		NewDefColWd

			Call		S_BuildColWdUndo,in=(#0:w),err=ErrFlag
			bcs		memError

			MoveWord	NewDefColWd,S_CurDefColWd

			in		#1:w,#1:w,#702:w,#1:w
			in		#S_ChangeDef:l
			XCall	S_TraverseRange,err=ErrFlag
			bcc		redraw

memError
			Call		D_MemoryError

redraw
			Call		S_AdjustTitleOffset
			Call		S_ActivateScrlBars

			Call		S_RefreshScreen,in=(#1:w)
			Call		S_SetUndoOn,in=(#S_UndoColWdType:w)

Exit
			RETURN
			ENDP

;---------------------------------------------------------------------------
;
;

S_RestoreColWidth	PROC		EXPORT
				;Using	S_CurrentData

			BEGIN	+b

			Call		S_SwapIn,in=(S_ActiveWindow:l)

;			Call		D_SetCursor,in=(#WatchCursor:w)

			in		S_CurTLSelect+2:w,#1:w,S_CurBRSelect+2:w,#1:w
			in		#S_RestoreWidth:l
			XCall	S_TraverseRange
			bcc		fixThings

			Call		D_MemoryError

fixThings
			Call		S_AdjustSheetColumns
			Call		S_ActivateScrlBars

			Call		S_SetUndoOn,in=(#S_UndoColWdType:w)

			Call		S_RefreshScreen,in=(#1:w)

			RETURN
			ENDP

;----------------------------------------------------------------------------
;
;

S_SetTitles	PROC		EXPORT
			;Using	S_CurrentData
			;Using	S_ErrorData

			local	Rect:r,RectPtr:l,TLTitle:l,BRTitle:l
			local	LeftThere:w,TopThere:w
			BEGIN	+b

			Call		S_SwapIn,in=(S_ActiveWindow:l)

			lda		#0
			sta		TLTitle
			sta		TLTitle+2
			sta		BRTitle
			sta		BRTitle+2

			sta		LeftThere
			sta		TopThere

			CmpWord	#S_EditHeight+S_ColLabelHeight,S_CurContYpt
			bne		setOldTopTitles

			CmpWord	S_CurTLSelect+2,#1
			bne		cmpLeftTitles

			CmpWord	S_CurBRSelect+2,#702
			bne		cmpLeftTitles

			MoveWord	S_CurTLSelect,TLTitle
			MoveWord	S_CurBRSelect,BRTitle
			bra		cmpLeftTitles

setOldTopTitles
			MoveWord	#1,TopThere

			MoveWord	S_CurTLTitle,TLTitle
			MoveWord	S_CurBRTitle,BRTitle

cmpLeftTitles
			CmpWord	#S_NumLabelWidth,S_CurContXpt
			bne		setOldLeftTitles

			CmpWord	S_CurTLSelect,#1
			bne		doDialog

			CmpWord	S_CurBRSelect,#9999
			bne		doDialog

			MoveWord	S_CurTLSelect+2,TLTitle+2
			MoveWord	S_CurBRSelect+2,BRTitle+2
			bra		doDialog

setOldLeftTitles
			MoveWord	#1,LeftThere

			MoveWord	S_CurTLTitle+2,TLTitle+2
			MoveWord	S_CurBRTitle+2,BRTitle+2

doDialog
			in		TLTitle:l,BRTitle:l
			in		LeftThere:w,TopThere:w
			out		TLTitle:l,BRTitle:l
			XCall	S_TitleDialog

			bcs		Exit

			SubWord	BRTitle,TLTitle,a
			cmp		#35
			bge		tooBig

			SubWord	BRTitle+2,TLTitle+2,a
			cmp		#35
			blt		OKTitle

tooBig
			Call		D_AlertBox,in=(#OKBox:w,#S_TitleTooBigStr:l),out=(a:w)
			bra		Exit

OKtitle
			MoveLong	TLTitle,S_CurTLTitle
			MoveLong	BRTitle,S_CurBRTitle

			Call		S_SetUndoOff
			Call		S_AdjustTitleOffset
			Call		S_ActivateScrlBars

drawScreen
			Call		S_RefreshScreen,in=(#2:w)

Exit
			RETURN
			ENDP

;---------------------------------------------------------------------------
;
;

S_ChangeGrid	PROC		EXPORT
			;Using	S_CurrentData
			;Using	S_AttrData

			BEGIN	+b

			Call		S_SwapIn,in=(S_ActiveWindow:l)
			Call		S_SetUndoOff

			lda		S_CurEditFlag
			eor		#S_GridOff
			sta		S_CurEditFlag

			and		#S_GridOff
			beq		resetGrid

			Tool		_SetMItemName,in=(#S_ShowGridStr:l,#S_GridItem:w)
			bra		redraw

resetGrid
			Tool		_SetMItemName,in=(#S_HideGridStr:l,#S_GridItem:w)

redraw
			Call		S_RefreshScreen,in=(#0:w)

Exit
			RETURN
			ENDP

;----------------------------------------------------------------------------
;
;

S_AdjustTitleOffset	PROC		EXPORT
				;Using	S_CurrentData

			local	NumCols:w,XOffset:w
			BEGIN

			lda		S_CurBRTitle
			beq		gotYOffset

			inc		a
			SubWord	a,S_CurTLTitle,a
			tay
			ldx		#S_RowHeight
			Call		D_FastMult

gotYOffset
			AddWord	a,#S_EditHeight+S_ColLabelHeight,S_CurContYpt

			lda		S_CurBRTitle+2
			beq		gotXOffset

			inc		a
			SubWord	a,S_CurTLTitle+2,NumCols
			stz		XOffset

loop
			dec		NumCols

			SpaceWord
			AddWord	S_CurTLTitle+2,NumCols,s
			Call		S_CellWidth				; in=(:w),out=(:w)

			AddWord	s,XOffset,XOffset

			lda		NumCols
			bne		loop

			lda		XOffset

gotXOffset
			AddWord	a,#S_NumLabelWidth,S_CurContXpt

			MoveWord	S_CurContYpt,S_RowLabRect
			MoveWord	S_CurContXpt,S_ColLabRect+2

			Call		S_AdjustSheetColumns
			RETURN
			ENDP

;----------------------------------------------------------------------------
;
;

S_AdjustSheetColumns	PROC		EXPORT
					;Using	S_CurrentData

			BEGIN

			Call		S_SetMaxTLCell

	;	Check if adjust of TLCell is needed	;

			CmpWord	S_CurMaxTLCell,S_CurTLCell
			bge		chkCol

			sta		S_CurTLCell

chkCol
			CmpWord	S_CurMaxTLCell+2,S_CurTLCell+2
			bge		fixPadCells

			sta		S_CurTLCell+2

fixPadCells
			Call		S_SetVertLines

			RETURN
			ENDP

;-------------------------------------------------------------------------
;
;

S_RefreshScreen	PROC		EXPORT
				;Using	S_CurrentData

			input	RefreshType:w
			local	UpdateRgn:l
			BEGIN

			PushWord	S_CurContRect+2
			PushWord	S_CurContRect

			lda		RefreshType
			bne		colLabelSet

			MoveWord	#S_EditHeight+S_ColLabelHeight+1,S_CurContRect

colLabelSet
			CmpWord	RefreshType,#2
			beq		rowLabelSet

			MoveWord	#S_NumLabelWidth+2,S_CurContRect+2

rowLabelSet
			Tool		_NewRgn,out=(UpdateRgn:l)
			Tool		_RectRgn,in=(UpdateRgn:l,#S_CurContRect:l)
			Tool		_EraseRect,in=(#S_CurContRect:l)
			Call		S_ScrlUpdate,in=(UpdateRgn:l)
			Tool		_DisposeRgn,in=(UpdateRgn:l)

			PullWord	S_CurContRect
			PullWord	S_CurContRect+2

			RETURN
			ENDP

;---------------------------------------------------------------------------
;
;

S_ActivateScrlBars	PROC		EXPORT
				;Using	S_CurrentData

			local	RightOK:w
			BEGIN

			stz		RightOK

			AddWord	S_CurContYpt,#S_RowHeight,a
			cmp		S_CurContHt
			blt		activateVBar

			Tool		_HiliteControl,in=(#$FF:w,S_CurRScrl:l)

			lda		#S_KeysInactive
			tsb		S_CurEditFlag
			bra		chkWidth

activateVBar
			inc		RightOK
			Tool		_HiliteControl,in=(#0:w,S_CurRScrl:l)

			lda		S_CurVPageSize
			inc		a
			tax

			in		#-1:w,x:w,S_CurRScrl:l
			XTool	_SetCtlParams

chkWidth
			AddWord	S_CurContXPt,#S_MinCellWidth,a
			cmp		S_CurContWd
			blt		activateHBar

			Tool		_HiliteControl,in=(#$FF:w,S_CurBScrl:l)

			lda		#S_KeysInactive
			tsb		S_CurEditFlag
			bra		Exit

activateHBar
			Tool		_HiliteControl,in=(#0:w,S_CurBScrl:l)

			in		S_CurMaxTLCell+2:w,#S_BViewSize:w,S_CurBScrl:l
			XTool	_SetCtlParams


			lda		RightOK
			beq		Exit

			lda		#S_KeysInactive
			trb		S_CurEditFlag

Exit
			RETURN
			ENDP
			END

