
	LOAD	'Macros.dump'

	INCLUDE	'SS.equ'

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

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

	IMPORT	D_AlertBox
	IMPORT	D_BLForgetLists
	IMPORT	D_BLGetSel
	IMPORT	D_BLMakeList
	IMPORT	D_BLModalDialog
	IMPORT	D_BLSelect
	IMPORT	D_CloseDialog
	IMPORT	D_CopyStrings
	IMPORT	D_DrawString
	IMPORT	D_EscFilter
	IMPORT	D_GetNewModalDialog
	IMPORT	D_SendMessage
	IMPORT	D_SetCursor
	IMPORT	D_HeartbeatIdle
	IMPORT	D_BlinkButton

	IMPORT	X_DialogFromScrap
	IMPORT	X_DialogToScrap
	IMPORT	X_FormatValue

	IMPORT	S_ChartCount
	IMPORT	S_ChartGRWind
	IMPORT	S_ChartListRec
	IMPORT	S_ChartMsgRec
	IMPORT	S_ChartNameStr
	IMPORT	S_ChartPict
	IMPORT	S_ChartSSWind
	IMPORT	S_DlogClipRgn
	IMPORT	S_DoPtPlot
	IMPORT	S_ListDlog
	IMPORT	S_ListRect
	IMPORT	S_NoSelectStr

	IMPORT	S_Cell2Str
	IMPORT	S_Ch1stTxt
	IMPORT	S_ChIconTable
	IMPORT	S_ChLastTxt
	IMPORT	S_ChMaxTxt
	IMPORT	S_ChMinTxt
	IMPORT	S_ChNameNum
	IMPORT	S_ChTooMany
	IMPORT	S_ChUntitle
	IMPORT	S_ChooseChartDlog
	IMPORT	S_CopyIn
	IMPORT	S_CopyOut
	IMPORT	S_Cur2BRSelect
	IMPORT	S_Cur2ChartCount
	IMPORT	S_Cur2ChartList
	IMPORT	S_Cur2Heap
	IMPORT	S_Cur2TLSelect
	IMPORT	S_Cur2Window
	IMPORT	S_CurrentData2
	IMPORT	S_LoMem
	IMPORT	S_MakeBar
	IMPORT	S_MakePie
	IMPORT	S_ModifyChartDlog
	IMPORT	S_PieZero
	IMPORT	S_Plot3DBar
	IMPORT	S_PlotBar
	IMPORT	S_PlotLine
	IMPORT	S_PlotPt
	IMPORT	S_SetUndoOff
	IMPORT	S_SwapOut
	IMPORT	S_Text2Cell
	IMPORT	S_drec2

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

	ENTRY	S_CalcChart
	ENTRY	S_DoChartDlog
	ENTRY	S_DoLinkBreakage
	ENTRY	S_KillChart
	ENTRY	S_ChartFilter

;---------------------------------------------------------------------------
;
; S_DoNewChart  (menu - New Chart)
;

S_DoNewChart	PROC	EXPORT
	;Using	S_ChartData
	;Using	S_CurrentData2
	;Using	S_PieData

	local	first:l,last:l
	local	type:w,cancelled:w
	local	ptr:l,hdl:l
	local	icon:w,chartname:l
	local	foo:l,min:r,foo2:l,max:r
	error	err
	BEGIN	+b

	stz	err
	call	S_CopyIn,in=(#S_CurrentData2:l)

	cmpw	S_Cur2ChartCount,#S_MaxChartCount
	blt	NotTooMany

	call	D_AlertBox,in=(#OKBox:w,#S_ChTooMany:l),out=(a:w)
	brl	Exit

NotTooMany
	H_NewBlock	#S_CSize,hdl,ptr,err=err
	jcs	exit	; added (6/8/89) MRH

	inc	S_Cur2ChartCount

	in	S_Cur2ChartCount:w,#S_ChNameNum-2:l,#2:w,#0:w
	xtool	_Int2Dec

	moveword	#S_BarType,[ptr]:#S_CType

	movelong	S_Cur2TLSelect,[ptr]:#S_CRange	; Selection range
	movelong	S_Cur2BRSelect,[ptr]:#S_CRange+4

	moveword	#1,[ptr]:#S_SpaceBar
	moveword	#0,[ptr]:#S_CIsMax
	movelong	#0,[ptr]:#S_CWind

	pushlong	#S_ChUntitle
	lda	#S_CName
	addlong	a,ptr,s
	call	D_CopyStrings,in=(:l,:l)


	moveX	S_PieZero,[ptr]:#S_CMin
	moveX	S_PieZero,[ptr]:#S_CMax

	call	S_DoChartDlog,in=(hdl:l),out=(a:w)
	beq	GoAhead

killnewchart
	H_DisposeBlock	hdl
	dec	S_Cur2ChartCount
	brl	Exit


GoAhead
	call	S_SetUndoOff

	cmpw	S_Cur2ChartCount,#2
	bge	NotFirst

	H_NewBlock	#4,S_Cur2ChartList,ptr,err=err
	bra	addChart	; added (6/8/89) MRH

NotFirst
;	lda	S_Cur2ChartCount	; already in a
	asl	a
	asl	a
	tax
	ldy	#0

	H_ResizeBlock	S_Cur2ChartList,xy,ptr,err=err

addChart
	bcs	killnewchart	; added (6/8/89) MRH

	lda	S_Cur2ChartCount
	dec	a
	asl	a
	asl	a
	tay	
	movelong	hdl,[ptr]:y

	stzl	S_ChartGRWind
	call	S_CalcChart,in=(hdl:l),err=(err)
	bcc	exit


	lda	S_Cur2ChartCount
	dec	a
	call	S_KillChart,in=(a:w)

exit	call	S_CopyOut,in=(#S_CurrentData2:l)
	call	S_SwapOut	; If CalcChart has lost me.

	RETURN
	ENDP	

;---------------------------------------------------------------------------
;
; S_DoChartDlog 
;

S_DoChartDlog	PROC	EXPORT
	;Using	SANEEQUS
	;Using	S_ChartData
	;Using	S_CurrentData2
	;Using	S_PieData
	;Using	S_SaneData2

	input	block:l
	output	cancelled:w
	local	chart:l
	local	icon:w,first:l,last:l
	local	TheDlog:l,result:w,ptr:l,hdl:l
	local	tmprect:r,tmp:l,olditem:w
	local	oldtime:l,newtime:l,time:l
	local	range1:r,range2:r,mode:w
	local	h1:l,min:r,h2:l,max:r
	local	valid:w,lptr:w,space:w
	error	err
	BEGIN	+b

	stz	cancelled

	call	D_SetCursor,in=(#WatchCursor:w)

	; Set up chart name string.

	H_GetBlockPtr	block,chart

	lda	#S_CName
	addlong	a,chart,s
	call	D_CopyStrings,in=(:l,#S_ChartNameStr:l)


	; Set up the strings for the Max/Min values.

	moveX	[chart]:#S_CMin,MinVal
	moveX	[chart]:#S_CMax,MaxVal


	in	#200:w,#0:l,#MinVal:l
	out	a:w,ptr:l,a:w
	xcall	X_FormatValue

	cmpw	[ptr],#$3001
	bne	DoCopy1
	stz	S_ChMinTxt
	bra	DoMaxTxt

DoCopy1	call	D_CopyStrings,in=(ptr:l,#S_ChMinTxt:l)

DoMaxTxt	in	#200:w,#0:l,#MaxVal:l
	out	a:w,ptr:l,a:w
	xcall	X_FormatValue

	moveword	[chart]:#S_CIsMax,a
	bne	DoCopy2
	stz	S_ChMaxTxt
	bra	DoType

DoCopy2	call	D_CopyStrings,in=(ptr:l,#S_ChMaxTxt:l)

	; Do the other init's.

DoType	subword	[chart]:#S_CType,#1,icon	; Icon selected

	movelong	[chart]:#S_CRange,first
	movelong	[chart]:#S_CRange+4,last

	cmpw	first,last
	beq	Singlized

	cmpw	first+2,last+2
	beq	Singlized

	moveword	first,last

Singlized	call	S_Cell2Str,in=(#S_Ch1stTxt:l,first:l)
	call	S_Cell2Str,in=(#S_ChLastTxt:l,last:l)

	call	D_GetNewModalDialog,in=(#S_ChooseChartDlog:l),out=(TheDlog:l)

	H_GetBlockPtr	block,chart
	moveword	[chart]:#S_SpaceBar,space
	tool	_SetDItemValue,in=(a:w,TheDlog:l,#8:w)

	jsl	X_DialogFromScrap

	tool	_SetPort,in=(TheDlog:l)
	tool	_SelectIText,in=(TheDlog:l,#20:w,#0:w,#255:w)
	tool	_ShowWindow,in=(TheDlog:l)
	call	D_SetCursor,in=(#ArrowCursor:w)

	tool	_GetDblTime,out=(time:l)
	tool	_GetTick,out=(newtime:l)

	addword	icon,#3,result

DlogLoop	moveword	#modeCopy,mode
	jsr	FrameIcon

	stz	ismax

	moveword	result,olditem
	movelong	newtime,oldtime
	tool	_ModalDialog,in=(#$80000000+S_ChartFilter:l),out=(result:w)
	tool	_GetTick,out=(newtime:l)

	lda	result
	cmp	#OK
	jeq	DoneDlog
	cmp	#Cancel
	jeq	DoneDlog

	cmp	#8
	jeq	doCheckBox

	cmp	#9
	bge	DlogLoop

	subword	result,#3,a
	cmp	icon
	bne	DoFrame
	cmpw	result,olditem
	bne	DlogLoop
	sublong	newtime,oldtime,oldtime
	cmpl	time,oldtime
	jlt	DlogLoop

	lda itsOK
	jne DoneDlog
	brl DlogLoop

DoFrame	moveword	#notXOR,mode
	jsr	FrameIcon
	subword	result,#3,icon
	jsr	FrameIcon


	brl	DlogLoop

	; toggle the checkbox

doCheckBox	tool	_GetDItemValue,in=(TheDlog:l,result:w),out=(a:w)
	eor	#1
	tool	_SetDItemValue,in=(a:w,TheDlog:l,result:w)

	brl	DlogLoop


DoneDlog	cmpw	result,#Cancel
	jeq	DoClose

	tool	_GetDItemValue,in=(TheDlog:l,#8:w),out=(space:w)

OKMinMax	tool	_GetIText,in=(TheDlog:l,#16:w,!range1:l)
	tool	_GetIText,in=(TheDlog:l,#17:w,!range2:l)

	; Do conversion.

	lda	range1
	and	#$FF
	sta	tmp
	call	S_Text2Cell,in=(!range1+1:l,tmp:w),out=(first:l)

	lda	range2
	and	#$FF
	sta	tmp
	call	S_Text2Cell,in=(!range2+1:l,tmp:w),out=(last:l)

	H_GetBlockPtr	block,chart
	addword	icon,#1,[chart]:#S_CType
	movelong	first,[chart]:#S_CRange
	movelong	last,[chart]:#S_CRange+4
	moveword	space,[chart]:#S_SpaceBar
	moveword	ismax,[chart]:#S_CIsMax

	pushlong	#S_ChartNameStr
	lda	#S_CName
	addlong	a,chart,s
	jsl	D_CopyStrings

	moveX	MinVal,[chart]:#S_CMin
	moveX	MaxVal,[chart]:#S_CMax

DoClose	jsl	X_DialogToScrap
	call	D_CloseDialog,in=(TheDlog:l)

	tool	_SetPort,in=(S_Cur2Window:l)

	cmpw	result,#Cancel
	bne	exit
	inc	cancelled

exit	RETURN

FrameIcon	cmpw	result,#8	; if it's not an
	bge	DidHilite	; icon that was
	cmp	#3	; chosen, don't change
	blt	DidHilite	; the check boxes.

	cmp	#5
	beq	Do0
	cmp	#4
	bne	DoFF

Do0	pushword	#0
	bra	DoHilite

DoFF	pushword	#$FF

DoHilite	tool	_GetControlDItem,in=(TheDlog:l,#8:w),out=(:l)
	_HiliteControl	

DidHilite	movelong	#S_ChIconTable,tmp
	lda	icon
	mul4	
	tay	
	movelong	[tmp]:y,ptr

	moverect	[ptr]:#2,tmprect

	subword	tmprect,#4,tmprect
	subword	tmprect+2,#8,tmprect+2
	addword	tmprect+4,#4,tmprect+4
	addword	tmprect+6,#9,tmprect+6

	tool	_SetPenMode,in=(mode:w)
	tool	_SetPenSize,in=(#5:w,#2:w)
	tool	_FrameRect,in=(!tmprect:l)
	_PenNormal	

	rts	

	EXPORT MaxVal,MinVal,isMax,itsOK

MaxVal	DS.B	10
MinVal	DS.B	10
isMax	DS.W	1
itsOK	DS.W	1
	ENDP	



;---------------------------------------------------------------------------
;
; S_ChartFilter
;

S_ChartFilter	PROC
	input	TheDlog:l,TheEvent:l,ItemHit:l
	output	TheResult:w
	local	TheCtl:l,range1:r,range2:r,tmp:l
	local	foo:l,min:r,foo2:l,max:r,ptr:l,valid:w
	local	lptr:w,tmp2:l
	begin +b

	stz theResult

	jsl	D_HeartBeatIdle

	tool	_GetIText,in=(TheDlog:l,#20:w,#S_ChartNameStr:l)
	lda	S_ChartNameStr
	and	#$FF
	jeq	baddata

	tool	_GetIText,in=(TheDlog:l,#16:w,!range1:l)
	tool	_GetIText,in=(TheDlog:l,#17:w,!range2:l)

	; Do conversion.

	lda	range1
	and	#$FF
	sta	tmp
	call	S_Text2Cell,in=(!range1+1:l,tmp:w),out=(tmp:l)
	jcs	baddata

	lda	range2
	and	#$FF
	sta	tmp2
	call	S_Text2Cell,in=(!range2+1:l,tmp2:w),out=(tmp2:l)
	jcs	baddata

;See if we have one row or one column
	Cmpw	tmp,tmp2
	beq	rangeok

	Cmpw	tmp+2,tmp2+2
	jne	baddata

	; check max/min values
rangeok
	tool	_GetIText,in=(TheDlog:l,#18:w,!min:l)

	lda	min
	and	#$FF
	jeq	NoMin

	moveword	#1,lptr
	tool	FPSTR2DEC,in=(!min:l,!lptr:l,#S_drec2:l,!valid:l)
	lda	min
	and	#$FF
	cmp	lptr
	jge	baddata

FinishMin	tool	FDEC2X,in=(#S_drec2:l,#MinVal:l)
	bra	DoMax
NoMin	moveX	S_PieZero,MinVal

DoMax	tool	_GetIText,in=(TheDlog:l,#19:w,!max:l)
	lda	max
	and	#$FF
	jeq	NoMax

	moveword	#1,lptr
	tool	FPSTR2DEC,in=(!max:l,!lptr:l,#S_drec2:l,!valid:l)
	lda	max
	and	#$FF
	cmp	lptr
	jge	baddata

FinishMax	tool	FDEC2X,in=(#S_drec2:l,#MaxVal:l)

	bra	DidMaxMin
NoMax	moveX	S_PieZero,MaxVal

DidMaxMin	tool	FCMPX,in=(#MaxVal:l,#S_PieZero:l)
	bne	CkNonZero
	tool	FCMPX,in=(#MinVal:l,#S_PieZero:l)
	bne	CkNonZero
	bra	notSame	; both are 0, so auto-scaling is enabled.

CkNonZero	inc	ismax
	tool	FCMPX,in=(#MinVal:l,#MaxVal:l)
	fbge	baddata
notSame

	pushword #noHilite
	moveword #1,itsOK
	bra DoHilite
baddata	pushword #inactiveHilite
	stz itsOK
DoHilite	tool _GetControlDItem,in=(TheDlog:l,#1:w),out=(:l)
	tool _HiliteControl,in=(:w,:l)

	cmpw [TheEvent],#keyDownEvt
	beq @aKey
	cmpw [TheEvent],#autoKeyEvt
	bne exit
@aKey
	moveword [TheEvent]:#omessage,a
	and #$FF
@ckPeriod	cmp #'.'
	bne @ckCancel
	moveword [TheEvent]:#oModifiers,a
	and #appleKey
	beq exit
@cancel	lda #Cancel
	bra @sta

@ckCancel	cmp #$1B
	beq @cancel

@ckReturn	cmp #$D
	bne exit
	ldy itsOK
	bne @OK
	moveword #0,[TheEvent]	; throw away the event.
	bra exit

@OK	lda #OK
@sta	inc theResult
	sta [itemHit]
	call D_BlinkButton,in=(TheDlog:l,a:w)

exit	return

	ENDP


;---------------------------------------------------------------------------
;
; S_ModifyChart
;

S_DoModifyChart	PROC	EXPORT
	;Using	S_ChartData
	;Using	S_CurrentData2

	local	TheDlog:l,result:w,item:w,ptr:l,hdl:l,tmp:l
	error	err
	BEGIN	+b

	call	S_CopyIn,in=(#S_CurrentData2:l)

	tool	_NewRgn,out=(S_DlogClipRgn:l)

	call	D_GetNewModalDialog,in=(#S_ModifyChartDlog:l),out=(TheDlog:l)

	movelong	TheDlog,S_ListDlog
	moveword	S_Cur2ChartCount,S_ChartCount

	call	D_BLMakeList,in=(#S_ChartListRec:l)
DlogLoop	call	D_BLSelect,in=(#S_ChartListRec:l,#0:w)

	call	D_BLModalDialog,in=(#D_EscFilter+$80000000:l),out=(result:w)
	call	D_BLGetSel,in=(#S_ChartListRec:l),out=(item:w)
	cmpw	result,#Cancel
	beq	CloseIt

	lda	item
	bpl	CloseIt

	call	D_AlertBox,in=(#OKBox:w,#S_NoSelectStr:l),out=(a:w)
	bra	DlogLoop

CloseIt	call	D_CloseDialog,in=(TheDlog:l)

	tool	_DisposeRgn,in=(S_DlogClipRgn:l)
	call	D_BLForgetLists

	H_GetBlockPtr	S_Cur2ChartList,ptr

	cmpw	result,#Cancel
	jeq	exit

	cmp	#OK
	bne	CkKill

	call	S_SetUndoOff

	lda	item
	asl	a
	asl	a
	tay

	; this is a blockptr.

	call	S_CalcChart,in=([ptr]:y:l),err=(err)
	brl	exit

CkKill	cmp	#3
	bne	DoMod

	call	S_SetUndoOff

	call	S_KillChart,in=(item:w)
	brl	exit

DoMod	H_GetBlockPtr	S_Cur2ChartList,ptr
	lda	item
	asl	a
	asl	a
	tay	
	movelong	[ptr]:y,hdl

	call	S_DoChartDlog,in=(hdl:l),out=(a:w)
	bne	exit

	call	S_SetUndoOff

	call	S_CalcChart,in=(hdl:l),err=(err)

exit	call	S_CopyOut,in=(#S_CurrentData2:l)
	call	S_SwapOut	; Just in case ReCalcChart has lost me.

	RETURN
	ENDP

;---------------------------------------------------------------------------
;
; S_ChartListProc 
;

S_ChartListProc	PROC	EXPORT
	;Using	S_ChartData
	;Using	S_CurrentData2

	input	listrec:l,item:w
	local	hdl:l,ptr:l,tmprect:r
	BEGIN	+b

	tool	_GetClip,in=(S_DlogClipRgn:l)

	tool	_ClipRect,in=(#S_ListRect:l)

	tool	_Move,in=(#2:w,#8:w)

	H_GetBlockPtr	S_Cur2ChartList,ptr

	lda	item
	asl	a
	asl	a
	addlong	a,ptr,ptr
	movelong	[ptr],hdl
	H_GetBlockPtr	hdl,ptr
	lda	#S_CName
	addlong	a,ptr,s

	call	D_DrawString

	tool	_SetClip,in=(S_DlogClipRgn:l)

	RETURN	
	ENDP	

;---------------------------------------------------------------------------
;
; S_ChartDummy  
;

S_ChartDummy	PROC	EXPORT

	input	ptr:l,item:w
	BEGIN

	RETURN
	ENDP

;---------------------------------------------------------------------------
;
; S_CalcChart 
;

S_CalcChart	PROC	EXPORT
	;Using	S_ChartData
	;Using	S_CurrentData2

	input	charthdl:l
	local	chart:l,tmprect:r
	error	err
	BEGIN	

	H_GetBlockPtr	charthdl,chart

	cmpw	[chart]:#S_CType,#S_PieType
	bne	CkBar
	call	S_MakePie,in=(charthdl:l),out=(S_ChartPict:l),err=(err)
	jcs	whoops
	brl	GotPict

CkBar	cmp	#S_BarType
	bne	Ck3D
	moveaddr	#S_PlotBar,S_DoPtPlot+1
	call	S_MakeBar,in=(charthdl:l),out=(S_ChartPict:l),err=(err)
	jcs	whoops
	brl	GotPict


Ck3D	cmp	#S_3DBarType
	bne	CkLine
	moveaddr	#S_Plot3DBar,S_DoPtPlot+1
	call	S_MakeBar,in=(charthdl:l),out=(S_ChartPict:l),err=(err)
	jcs	whoops
	bra	GotPict

CkLine	cmp	#S_LineType
	bne	CkPt
	moveaddr	#S_PlotLine,S_DoPtPlot+1
	call	S_MakeBar,in=(charthdl:l),out=(S_ChartPict:l),err=(err)
	jcs	whoops
	bra	GotPict

CkPt	cmp	#S_PtType
	moveaddr	#S_PlotPt,S_DoPtPlot+1
	call	S_MakeBar,in=(charthdl:l),out=(S_ChartPict:l),err=(err)
	jcs	whoops

GotPict
;	pushword	#Pict
;	call	X_CopyOneHandle,in=(S_ChartPict:l),out=(:l)
;	call	X_PostScrap,in=(:w,:l)

	H_GetBlockPtr	charthdl,chart
	movelong	[chart]:#S_CWind,S_ChartGRWind
	movelong	S_Cur2Window,S_ChartSSWind

	; GR

	in	#5:w,#S_ChartMsg:w,#S_ChartMsgRec:l
	out	:l
	xerr	err
	xcall	D_SendMessage
	bcc	GetWind

	tax
	pulllong	a
	cpx	#-1
	beq	@1
	call	D_AlertBox,in=(#OKBox:w,#S_LoMem:l),out=(a:w)
@1	H_GetBlockPtr	charthdl,chart
	movelong	#0,[chart]:#S_CWind
	bra	KillP

GetWind	H_GetBlockPtr	charthdl,chart
	cpzl	[chart]:#S_CWind
	beq	GrabWind
	pulllong	a
	bra	KillP
GrabWind	pulllong	[chart]:#S_CWind

KillP
	tool	_KillPicture,in=(S_ChartPict:l)

whoops	call	D_SetCursor,in=(#ArrowCursor:w)

	RETURN	
	ENDP	

;---------------------------------------------------------------------------
;
; S_KillChart (num:w) where num is the array position [0..n]
; dispose of a chart

S_KillChart	PROC	EXPORT
	;Using	S_ChartData
	;Using	S_CurrentData2

	input	chartnum:w
	local	ptr:l,size:w,offset:w
	BEGIN	

	H_GetBlockPtr	S_Cur2ChartList,ptr

	lda	chartnum
	asl	a
	asl	a
	sta	offset

	call	S_DoLinkBreakage,in=([ptr]:offset:l)

	H_GetBlockPtr	S_Cur2ChartList,ptr
	H_DisposeBlock	[ptr]:offset

	subword	S_Cur2ChartCount,chartnum,a
	dec	a
	sta	size
	beq	CleanUp

; {
	lda	chartnum
	ina	
	asl	a
	asl	a
	addlong	a,ptr,s	; src
	;
	lda	chartnum	;
	asl	a	;
	asl	a	;
	addlong	a,ptr,s	; dest
	;
	lda	size	;
	asl	a	;
	asl	a	;
	pea	0	;
	pha	; size

	tool	_BlockMove,in=(:l,:l,:l)
; }

CleanUp	lda	S_Cur2ChartCount
	dec	a
	sta	S_Cur2ChartCount
	beq	exit

	asl	a
	asl	a
	tax
	ldy	#0

	H_ResizeBlock	S_Cur2ChartList,xy

exit	RETURN
	ENDP

;---------------------------------------------------------------------------
;
; S_BreakLink(message:l)
;
;	   handle a break-link message from GR (or myself...)
;

S_BreakLink	PROC	EXPORT
	;Using	S_ChartData
	;Using	S_CurrentData2

	input	message:l
	local	count:w,ptr:l
	BEGIN	+b

	call	S_CopyIn,in=(#S_CurrentData2:l)

	; go to a particular chart & zero its CWind

	moveword	S_Cur2ChartCount,count

loop	dec	count
	bmi	exit
	H_GetBlockPtr	S_Cur2ChartList,ptr

	lda	count
	mul4	a
	tay	

	H_GetBlockPtr	[ptr]:y,ptr

	cmpl	[ptr]:#S_CWind,[message]
	bne	loop

	movelong	#0,[ptr]:#S_CWind

exit	call	S_CopyOut,in=(#S_CurrentData2:l)

	RETURN
	ENDP

;---------------------------------------------------------------------------
;
; S_DoLinkBreakage(chart:l)
;
;
;	tell GR to break link & break it myself
;	... S_BreakGraphLink

S_DoLinkBreakage	PROC	EXPORT
	;Using	S_ChartData
	;Using	S_CurrentData2

	input	charthdl:l
	local	chart:l,ptr:l
	BEGIN	

	H_GetBlockPtr	charthdl,chart
	movelong	[chart]:#S_CWind,ptr
	cpzl	ptr
	beq	KillIt

	movelong	ptr,S_ChartGRWind
	movelong	S_Cur2Window,S_ChartSSWind
	call	D_SendMessage,in=(#5:w,#S_BreakGraphLink:w,#S_ChartMsgRec:l),out=(a:l)

KillIt	call	S_CopyOut,in=(#S_CurrentData2:l)
	call	S_BreakLink,in=(#S_ChartMsgRec:l)
	call	S_CopyIn,in=(#S_CurrentData2:l)

exit	RETURN
	ENDP

;---------------------------------------------------------------------------
;
; S_KillLinks()
;

S_KillLinks	PROC	EXPORT
	;Using	S_ChartData
	;Using	S_CurrentData2

	local	count:w,ptr:l
	BEGIN	+b

	call	S_CopyIn,in=(#S_CurrentData2:l)

	; Traverse charts, calling DoLinkBreakage

	moveword	S_Cur2ChartCount,count

loop	dec	count
	bmi	exit
	H_GetBlockPtr	S_Cur2ChartList,ptr

	lda	count
	mul4	a
	tay	

	call	S_DoLinkBreakage,in=([ptr]:y:l)
	bra	loop

exit	call	S_CopyOut,in=(#S_CurrentData2:l)

	RETURN	
	ENDP	

;---------------------------------------------------------------------------
;
; S_SaveCharts 
;

S_SaveCharts	PROC	EXPORT
	;Using	S_ChartData
	;Using	S_CurrentData2

	input	destptr:l
	output	size:l
	local	ptr:l,offset:l,tmp:l
	BEGIN	+b

	stzl	size
	stz	offset+2

	call	S_CopyIn,in=(#S_CurrentData2:l)

	moveword	S_Cur2ChartCount,offset
	jeq	GotSize

	; Get the final size of all the chartdata.

	tool	_Multiply,in=(S_Cur2ChartCount:w,#S_CSize:w),out=(size:l)
	movelong	size,[destptr]
	addwl	#4,destptr

	; Get the ptr to the array of blockptrs.

	H_GetBlockPtr	S_Cur2ChartList,ptr

	dec	offset
	asl	offset
	asl	offset

	; Loop through the array, Deref'ing and saving out all the charts.

SaveLoop	addlong	ptr,offset,tmp
	H_GetBlockPtr	[tmp],s
	tool	_BlockMove,in=(:l,destptr:l,#S_CSize:l)

	movelong	#0,[destptr]:#S_CWind	; zero out the GR wind ptr.

	subword	offset,#4,offset
	bmi	GotSize
	addwl	#S_CSize,destptr
	bra	SaveLoop

	; Allow for the size longword.

GotSize	addwl	#4,size

exit	RETURN
	ENDP	

;---------------------------------------------------------------------------
;
; S_LoadCharts 
;

S_LoadCharts	PROC	EXPORT
	;Using	S_ChartData
	;Using	S_CurrentData2

	input	srcptr:l
	output	size:l
	local	ptr:l,offset:l,chartindex:l,chartptr:l
	error	err
	BEGIN	+b

	stz	err
	stzl	size

	stz	offset+2
	call	S_CopyIn,in=(#S_CurrentData2:l)

	moveword	S_Cur2ChartCount,offset
	jeq	GotSize

	; Size of all chart data.

	movelong	[srcptr],size

	asl	offset
	asl	offset

	; Alloc an array blockptr

	ldx	offset
	ldy	#0
	H_NewBlock	xy,S_Cur2ChartList,err=err
	jcs	exit

	subword	offset,#4,offset
	addwl	#4,srcptr	; allow for size of size

LoadLoop
	H_NewBlock	#S_CSize,chartindex,chartptr,err=err
	bcs	exit

	H_GetBlockPtr	S_Cur2ChartList,ptr
	movelong	chartindex,[ptr]:offset
	tool	_BlockMove,in=(srcptr:l,chartptr:l,#S_CSize:l)

	subword	offset,#4,offset
	bmi	GotSize
	addwl	#S_CSize,srcptr
	bra	LoadLoop

GotSize	addwl	#4,size

exit	call	S_CopyOut,in=(#S_CurrentData2:l)

	RETURN
	ENDP
	END

