
		LOAD		'Macros.dump'

		INCLUDE	'SS.equ'

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

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

;		IMPORT	KeyPad
;		IMPORT	appleKey

		IMPORT	S_ActiveWindow
		IMPORT	S_CellWidth
		IMPORT	S_Clear
		IMPORT	S_CurBRSelect
		IMPORT	S_CurBScrl
		IMPORT	S_CurContHt
		IMPORT	S_CurContRect
		IMPORT	S_CurContWd
		IMPORT	S_CurContXpt
		IMPORT	S_CurContYpt
		IMPORT	S_CurEditFlag
		IMPORT	S_CurLEHandle
		IMPORT	S_CurMaxTLCell
		IMPORT	S_CurRScrl
		IMPORT	S_CurTLCell
		IMPORT	S_CurTLSelect
		IMPORT	S_DoChecks
		IMPORT	S_DrawLocation
		IMPORT	S_EndEdit
		IMPORT	S_GetCellIndex
		IMPORT	S_HiliteCells
		IMPORT	S_NewEditCell
		IMPORT	S_NewLineEdit
		IMPORT	S_SetCurFormat
		IMPORT	S_SetEdittingMenus
		IMPORT	S_SwapIn
		IMPORT	S_Thumb
		IMPORT	S_WhereCell
		IMPORT	S_WhichCell

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

		ENTRY	S_DoPageDown
		ENTRY	S_DoPageLeft
		ENTRY	S_DoPageRight
		ENTRY	S_DoPageUp
		ENTRY	S_PageOver

;-------------------------------------------------------------------------;
; S_KeyDown ( TaskRecordPtr:l )
;
;	S_KeyDown is called by the driver to handle a key down event in the
; currently active window.

S_KeyDown		PROC		EXPORT
			;Using	S_CurrentData

			input	TaskRecordPtr:l
			local	Key:w,Mods:w,NotArrow:w
			BEGIN	+b

			stz		NotArrow

			Call		S_SwapIn,in=(S_ActiveWindow:l)
			Tool		_SetPort,in=(S_ActiveWindow:l)

			lda		S_CurEditFlag
			and		#S_KeysInactive
			jne		Exit

			ldy		#2
			lda		[TaskRecordPtr],y
			and		#$00FF
			sta		Key

			ldy		#14
			lda		[TaskRecordPtr],y
			sta		Mods

			CmpLong	S_CurTLSelect,S_CurBRSelect
			jne		rangeSelected

			ldx		#MaxCellTable
			lda		Key
loop
			cmp		CellKeyTable,x
			beq		foundkey
			dex
			dex
			bne		loop

notfound
			lda		Mods
			and		#appleKey
			jne		Exit

			lda		S_CurEditFlag
			and		#S_EditingBit
			bne		doEdit

			lda		S_CurEditFlag
			ora		#S_EditingBit
			sta		S_CurEditFlag

			Tool		_LEActivate,in=(S_CurLEHandle:l)
			Tool		_LEGetTextLen,in=(S_CurLEHandle:l),out=(x:w)
			Tool		_LESetSelect,in=(#0:w,x:w,S_CurLEHandle:l)

			Call		S_SetCurFormat,in=(S_CurTLSelect:l)
			Call		S_SetEdittingMenus

doEdit
			Tool		_LEKey,in=(Key:w,Mods:w,S_CurLEHandle:l)
			bra		Exit

foundkey
			lda		Mods
			and		#appleKey
			bne		appledown

			lda		S_CurEditFlag
			and		#S_EditingBit
			bne		editing

			jsr		(CellTable,x)
			bra		Exit

editing
			jsr		(CellEdit,x)
			bra		Exit

appledown
			jsr		(CellApple,x)
			bra		Exit

rangeSelected
			ldx		#MaxRangeTable
			lda		Key

Rloop
			cmp		RangeKeyTable,x
			beq		foundRkey
			dex
			dex
			bne		Rloop

RKeyNotFound
			bra		Exit

foundRkey
			lda		Mods
			and		#appleKey
			bne		Rappledown
			jsr		(RangeTable,x)
			bra		Exit

Rappledown
			jsr		(RangeApple,x)

Exit
			RETURN

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

maxCellTable	equ		16
maxRangeTable	equ		10

CellKeyTable	DC.W		0
			DC.W		8			; left arrow
			DC.W		21			; right arrow
			DC.W		10			; down arrow
			DC.W		11			; up arrow
			DC.W		13			; return
			DC.W		9			; tab
			DC.W		27			; esc
			DC.W		127			; delete

CellTable		DC.W		0
			DC.W		cellLeft		; left arrow
			DC.W		cellRight		; right arrow
			DC.W		cellDown		; down arrow
			DC.W		cellUp		; up arrow
			DC.W		cellReturn	; return
			DC.W		cellTab		; tab
			DC.W		cellEsc		; esc
			DC.W		rangeDelete	; delete

CellEdit		DC.W		0
			DC.W		enterLE		; left arrow
			DC.W		enterLE		; right arrow
			DC.W		cellDown		; down arrow
			DC.W		cellUp		; up arrow
			DC.W		cellReturn	; return
			DC.W		cellTab		; tab
			DC.W		cellEsc		; esc
			DC.W		enterLE		; delete

CellApple		DC.W		0
			DC.W		doPageLeft	; apple-left arrow
			DC.W		doPageRight	; apple-right arrow
			DC.W		doPageDown	; apple-down arrow
			DC.W		doPageUp		; apple-up arrow
			DC.W		cellAppleReturn ; apple-return
			DC.W		cellAppleTab	; apple-tab
			DC.W		cellEsc		; esc
			DC.W		nothing		; delete

RangeKeyTable
			DC.W		0
			DC.W		8			; left arrow
			DC.W		21			; right arrow
			DC.W		10			; down arrow
			DC.W		11			; up arrow
			DC.W		127			; delete

RangeTable
			DC.W		0
			DC.W		nothing		; left arrow
			DC.W		nothing		; right arrow
			DC.W		nothing		; down arrow
			DC.W		nothing		; up arrow
			DC.W		rangeDelete	; delete

RangeApple	DC.W		0
			DC.W		nothing		; apple-left arrow
			DC.W		nothing		; apple-right arrow
			DC.W		nothing		; apple-down arrow
			DC.W		nothing		; apple-up arrow
			DC.W		nothing		; apple-delete

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

nothing
			rts

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

enterLE
			Tool		_LEKey,in=(Key:w,Mods:w,S_CurLEHandle:l)
			rts

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

cellesc
			lda		S_CurEditFlag
			and		#S_EditingBit
			beq		done

			Call		S_EndEdit,in=(#0:w)

			lda		S_CurEditFlag
			and		#-1-S_EditingBit
			sta		S_CurEditFlag

			Call		S_DoChecks
done			rts

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

rangeDelete
			Call		S_Clear
			rts

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

cellAppleTab
			inc		NotArrow

cellLeft
			pha
			PushWord	S_CurTLSelect
			SubWord	S_CurTLSelect+2,#1,3:s

			bne		OKleft

			lda		NotArrow
			beq		Invalid

			MoveWord	#$02BE,3:s			 ; hex for #ZZ
			SubWord	1:s,#1,1:s

			bne		OKleft

			MoveWord	#$270F,1:s			 ; hex for #9999

OKleft
			Call		S_NewEditCell,in=(:l)
			brl		setLocation

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

cellTab
			inc		NotArrow

cellRight
			pha
			PushWord	S_CurTLSelect
			AddWord	S_CurTLSelect+2,#1,3:s

			cmp		#$02BF				; hex for #ZZ + 1
			blt		OKright

			lda		NotArrow
			beq		Invalid

			MoveWord	#1,3:s
			AddWord	1:s,#1,1:s

			cmp		#$2710				; hex for 10000
			blt		OKright

			MoveWord	#1,1:s

OKright
			Call		S_NewEditCell,in=(:l)
			brl		setLocation

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

Invalid
			pla
			pla
			rts

;---------------------------
cellAppleReturn
			lda		Mods
			and		#KeyPad
			bne		cellEnter

			inc		NotArrow

cellUp
			PushWord	S_CurTLSelect+2
			SubWord	S_CurTLSelect,#1,s

			bne		OKup

			lda		NotArrow
			beq		Invalid

			MoveWord	#$270F,1:s			 ; hex for #9999
			SubWord	3:s,#1,3:s

			bne		OKup

			MoveWord	#$02BE,3:s			 ; hex for #ZZ

OKup
			Call		S_NewEditCell,in=(:l)
			brl		setLocation

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

cellReturn
			lda		Mods
			and		#KeyPad
			bne		cellEnter

			inc		NotArrow

cellDown
			PushWord	S_CurTLSelect+2
			AddWord	S_CurTLSelect,#1,s

			cmp		#$2710				; hex for 10000
			blt		OKdown

			lda		NotArrow
			beq		Invalid

			MoveWord	#1,1:s
			AddWord	3:s,#1,3:s

			cmp		#$02BF				; hex for #ZZ + 1
			blt		OKdown

			MoveWord	#1,3:s

OKdown
			Call		S_NewEditCell,in=(:l)
			bra		setLocation

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

cellEnter
			Call		S_NewEditCell,in=(S_CurTLSelect:l)
			bra		setLocation

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

doPageLeft
			Call		S_PageOver,in=(#S_DoPageLeft:l)
			bra		setLocation

doPageRight
			Call		S_PageOver,in=(#S_DoPageRight:l)
			bra		setLocation

doPageUp
			Call		S_PageOver,in=(#S_DoPageUp:l)
			bra		setLocation

doPageDown
			Call		S_PageOver,in=(#S_DoPageDown:l)

setLocation
			Call		S_DoChecks
			Call		S_DrawLocation
			rts

			ENDP

;-----------------------------------------------------------------------------
; S_PageOver( ShiftRoutine:l )
;
;	Direction =	$0000FFFF	means	up
;				$FFFF0000			left
;				$00000001			down
;				$00010000			right

S_PageOver	PROC		EXPORT
			;Using	S_CurrentData

			input	ShiftRoutine:l
			local	NewCell:l
			local	Value:w,Width:w
			local	OldClipRgn:l,Col:w
			local	ErrorFlag:w
			BEGIN

	;	The old Cell needs to be accepted, if edited.	;

			lda		S_CurEditFlag
			and		#S_EditingBit
			beq		doShift

			Call		S_EndEdit,in=(#-1:w),err=ErrorFlag
			jcs		Exit

clrEdit
			lda		S_CurEditFlag
			and		#-1-S_EditingBit
			sta		S_CurEditFlag

	;	Bump the window until the New cell is completely in view	;

doShift
			MoveWord	ShiftRoutine,CallIt+1
			MoveWord	ShiftRoutine+1,CallIt+2

			SpaceLong

callIt
			Call		*

			PullLong	NewCell

			CmpLong	NewCell,S_CurTLSelect
			jeq		Exit

	;	The Cell is completely in view	;

			Tool		_NewRgn,out=(:l)
			MoveLong	1:s,OldClipRgn
			Tool		_GetClip,in=(:l)
			Tool		_ClipRect,in=(#S_CurContRect:l)

			Call		S_HiliteCells

			MoveWord	NewCell,S_CurTLSelect
			sta		S_CurBRSelect

			MoveWord	NewCell+2,S_CurTLSelect+2
			sta		S_CurBRSelect+2

			Call		S_HiliteCells

			Tool		_SetClip,in=(OldClipRgn:l)
			Tool		_DisposeRgn,in=(OldClipRgn:l)

	;	The New cell needs to be displayed in the LE field.	;

			in		NewCell:l
			out		:l
			XCall	S_GetCellIndex

			Call		S_NewLineEdit,in=(:l)

Exit
			RETURN
			ENDP

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

S_DoPageLeft	PROC		EXPORT
			;Using	S_CurrentData

			output	NewCell:l
			local	WorkWidth:w,dh:w
			BEGIN

			MoveLong	S_CurTLSelect,NewCell

			Call		S_WhereCell,in=(NewCell:l),out=(y:w,y:w,a:w,y:w)

			tay
			beq		leftSide1

			cmp		S_CurContXpt
			beq		leftSidePage

			cmp		S_CurContWd
			bge		leftSide1

leftSideHere
			MoveWord	S_CurTLCell+2,NewCell+2
			bra		doLeftSide

leftSide1
			SubWord	S_CurTLSelect+2,#1,NewCell+2
			bne		doLeftSide

			MoveWord	#1,NewCell+2
			bra		doLeftSide

leftSidePage
			SubWord	S_CurContWd,S_CurContXpt,WorkWidth

			stz		dh
			bra		cond

loop
			Call		S_CellWidth,in=(NewCell+2:w),out=(a:w)
			AddWord	a,dh,a
			cmp		WorkWidth
			bge		full
			sta		dh

cond
			dec		NewCell+2
			bne		loop

			inc		NewCell+2
			lda		dh
			beq		Exit
			bra		doLeftSide

full
			inc		NewCell+2
			lda		dh
			bne		doLeftSide

			dec		NewCell+2

doLeftSide
			CmpWord	S_CurMaxTLCell+2,NewCell+2
			bge		setThumb

			sta		NewCell+2

setThumb
			lda		NewCell+2
			dec		a

			Tool		_SetCtlValue,in=(a:w,S_CurBScrl:l)
			Call		S_Thumb,in=(S_CurBScrl:l)

Exit
			RETURN
			ENDP

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

S_DoPageRight	PROC		EXPORT
			;Using	S_CurrentData

			output	NewCell:l
			local	WorkWidth:w,dh:w,NewTLCol:l
			BEGIN

			MoveLong	S_CurTLSelect,NewCell
			SubWord	S_CurContWd,S_CurContXpt,WorkWidth
			stz		dh

			Call		S_WhereCell,in=(NewCell:l),out=(x:w,y:w,a:w,y:w)

			tay
			beq		rightSide1

			cmp		S_CurContWd
			bge		rightSide1

			cpx		S_CurContWd
			bge		rightSidePage

rightSideHere
			in		S_CurContWd:w,S_CurContHt:w
			out		NewCell+2:w,a:w,x:w,y:w
			XCall	S_WhichCell

			cmp		#702+1
			jlt		Exit

			MoveWord	#702,NewCell+2
			brl		Exit

rightSide1
			MoveWord	NewCell+2,a
			inc		a
			sta		NewTLCol
			sta		NewCell+2

			cmp		#702+1
			blt		start1

			MoveWord	S_CurMaxTLCell+2,NewTLCol
			MoveWord	#702,NewCell+2
			brl		gotTLCol

loop1
			sta		dh

start1
			dec		NewTLCol
			beq		atZero

			Call		S_CellWidth,in=(NewTLCol:w),out=(a:w)
			AddWord	a,dh,a
			cmp		WorkWidth
			blt		loop1

			inc		NewTLCol
			brl		gotTLCol

atZero
			inc		NewTLCol
			bra		start2

loop2
			sta		dh
			inc		NewCell+2

start2
			Call		S_CellWidth,in=(NewCell+2:w),out=(a:w)
			AddWord	a,dh,a
			cmp		WorkWidth
			blt		loop2

			bra		gotTLCol

rightSidePage
			MoveWord	S_CurTLSelect+2,NewTLCol
			cmp		S_CurMaxTLCell+2
			blt		pageCond

			MoveWord	S_CurMaxTLCell+2,NewTLCol
			bra		pageCond

pageLoop
			inc		NewCell+2

			Call		S_CellWidth,in=(NewCell+2:w),out=(a:w)
			AddWord	a,dh,a
			cmp		WorkWidth
			bge		full
			sta		dh

pageCond
			CmpWord	NewCell+2,#702
			bne		pageLoop

			bra		gotTLCol

full
			dec		NewCell+2
			lda		dh
			bne		gotTLCol

			inc		NewCell+2

gotTLCol
			lda		NewTLCol
			dec		a

			Tool		_SetCtlValue,in=(a:w,S_CurBScrl:l)
			Call		S_Thumb,in=(S_CurBScrl:l)

Exit
			RETURN
			ENDP

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

S_DoPageUp	PROC		EXPORT
			;Using	S_CurrentData

			output	NewCell:l
			local	WorkHeight:w,dv:w
			BEGIN

			MoveLong	S_CurTLSelect,NewCell

			Call		S_WhereCell,in=(NewCell:l),out=(y:w,y:w,y:w,a:w)

			tax
			beq		up1

			cmp		S_CurContYpt
			beq		upPage

			cmp		S_CurContHt
			bge		up1

upHere
			MoveWord	S_CurTLCell,NewCell
			bra		doUp

up1
			SubWord	S_CurTLSelect,#1,NewCell
			bne		doUp

			MoveWord	#1,NewCell
			bra		doUp

upPage
			SubWord	S_CurContHt,S_CurContYpt,WorkHeight

			stz		dv
			bra		cond

loop
			AddWord	#S_RowHeight,dv,a
			cmp		WorkHeight
			bge		full
			sta		dv

cond
			dec		NewCell
			bne		loop

			inc		NewCell
			lda		dv
			beq		Exit
			bra		doUp

full
			inc		NewCell
			lda		dv
			bne		doUp

			dec		NewCell

doUp
			CmpWord	S_CurMaxTLCell,NewCell
			bge		setThumb

			sta		NewCell

setThumb
			lda		NewCell
			dec		a

			Tool		_SetCtlValue,in=(a:w,S_CurRScrl:l)
			Call		S_Thumb,in=(S_CurRScrl:l)

Exit
			RETURN
			ENDP

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

S_DoPageDown	PROC		EXPORT
			;Using	S_CurrentData

			output	NewCell:l
			local	WorkHeight:w,dv:w,NewTLRow:l
			BEGIN

			MoveLong	S_CurTLSelect,NewCell
			SubWord	S_CurContHt,S_CurContYpt,WorkHeight
			stz		dv

			Call		S_WhereCell,in=(NewCell:l),out=(y:w,x:w,y:w,a:w)

			tay
			beq		down1

			cmp		S_CurContHt
			bge		down1

			cpx		S_CurContHt
			bge		downPage

downHere
			in		S_CurContWd:w,S_CurContHt:w
			out		x:w,NewCell:w,x:w,y:w
			XCall	S_WhichCell

			cmp		#9999+1
			jlt		Exit

			MoveWord	#9999,NewCell
			brl		Exit

down1
			MoveWord	NewCell,a
			inc		a
			sta		NewTLRow
			sta		NewCell

			cmp		#9999+1
			blt		start1

			MoveWord	S_CurMaxTLCell,NewTLRow
			MoveWord	#9999,NewCell
			brl		gotTLRow

loop1
			sta		dv

start1
			dec		NewTLRow
			beq		atZero

			AddWord	#S_RowHeight,dv,a
			cmp		WorkHeight
			blt		loop1

			inc		NewTLRow
			brl		gotTLRow

atZero
			inc		NewTLRow
			bra		start2

loop2
			sta		dv
			inc		NewCell

start2
			AddWord	#S_RowHeight,dv,a
			cmp		WorkHeight
			blt		loop2

			bra		gotTLRow

downPage
			MoveWord	S_CurTLSelect,NewTLRow
			cmp		S_CurMaxTLCell
			blt		pageCond

			MoveWord	S_CurMaxTLCell,NewTLRow
			bra		pageCond

pageLoop
			inc		NewCell

			AddWord	#S_RowHeight,dv,a
			cmp		WorkHeight
			bge		full
			sta		dv

pageCond
			CmpWord	NewCell,#9999
			bne		pageLoop

			bra		gotTLRow

full
			dec		NewCell
			lda		dv
			bne		gotTLRow

			inc		NewCell

gotTLRow
			lda		NewTLRow
			dec		a

			Tool		_SetCtlValue,in=(a:w,S_CurRScrl:l)
			Call		S_Thumb,in=(S_CurRScrl:l)

Exit
			RETURN
			ENDP
			END

