NEW
  AUTO 3,1
				.LIST OFF
*--------------------------------------
SCRN.Init		>LDYAI 510+5+5+4+1		potentially 255*\\ NORM,INV,CEOL & \0
				>SYSCALL Getmem
				bcs .9
				>STYA LineBufPtr
				txa
				>STA.G hLineBuffer

				>LDA.G ScreenH
				dec
				>PUSHA
				>PUSHBI 1
				>LDYA L.SEQ.INIT
				>SYSCALL printf
				bcs .9
				
				lda #0

				ldy #FileX
				sta (pData),y

				ldy #FileY
				sta (pData),y
				iny
				sta (pData),y
		
				ldy #ScreenX
				sta (pData),y

				ldy #ScreenY
				sta (pData),y
				iny
				sta (pData),y

				jsr SCRN.UpdateTopBar
				bcs .9
				jsr SCRN.UpdateViewPort
				bcs .9
				jsr SCRN.UpdateMain
.9				rts
*--------------------------------------
SCRN.UpdateTopBar	
				lda #1
				jsr SCRN.InvLineA
				bcs .9
				
				>LDA.G hFileName
				beq .1
				>SYSCALL GetMemPtr
				bra .2
				
.1				>LDYA L.MSG.NEWFILE

.2				>PUSHYA
				>PUSHBI 2
				>LDYA L.MSG.TOPBAR
				>SYSCALL printf
				
.9				rts
*--------------------------------------
SCRN.UpdateMain	>LDA.G ScreenY
				sta TmpIndex
				iny
				lda (pData),y
				sta TmpIndex+1

				lda #0
				sta TmpByte
				
				>LDA.G ViewPortH
				sta TmpCount
				
.1				lda TmpIndex
				ldx TmpIndex+1
				
				jsr BUF.GetLineAX		Y = line length
				bcs .5

				lda TmpByte
				jsr SCRN.UpdateLineAtALenY
				bcs SCRN.UpdateMain.9
				
.3				inc TmpByte
				inc TmpIndex
				bne .4
				inc TmpIndex+1
				
.4				dec TmpCount
				bne .1
				jmp SCRN.GotoCurXCurY
				
.5				ldx #0
				ldy TmpByte
				jsr GotoXY
				bcs SCRN.UpdateMain.9
				
				>PUSHBI 0
				>LDYA L.SEQ.CEOL
				>SYSCALL printf
				bcs SCRN.UpdateMain.9
				
				inc TmpByte
				dec TmpCount
				bne .5
				
				jmp SCRN.GotoCurXCurY

SCRN.UpdateMain.9
				rts
*--------------------------------------
SCRN.UpdateCurrentLine
				>LDA.G ScreenY+1
				tax
				dey
				lda (pData),y
				clc
				>ADC.G CurY
				bcc .1
				inx
.1				jsr BUF.GetLineAX
				bcs SCRN.UpdateMain.9

				phy
				>LDA.G CurY
				ply
*--------------------------------------
* A = Target Screen Y position
* BufPtr = Text line start, Y = len
*--------------------------------------
SCRN.UpdateLineAtALenY
				stz bSelected
				sty TmpLen
				
				ldx #0
				tay
				jsr GotoXY
				bcs .9
				
				>LDYA LineBufPtr
				>STYA ZPPTR1
				
				>LDA.G ScreenW
				tax
				
				>LDA.G ScreenX
				cmp TmpLen
				bcs .8
				
				tay
				
				clc
				adc BUF.LOffset
				sta BUF.COffset
				lda #0
				adc BUF.LOffset+1
				sta BUF.COffset+1
				
				tya
*				clc
				adc BufPtr
				sta BufPtr
				bcc .1
				
				inc BufPtr+1

.1				lda (BufPtr)
				beq .8
				
				cmp #C.CR
				beq .8

				jsr BUF.CheckSelected
				eor bSelected
				bpl .5
				
				jsr SCRN.ToggleSel
				
.5				lda (BufPtr)
				jsr SCRN.ToLineBuf
				cmp #'\'
				bne .51
				
				jsr SCRN.ToLineBuf
				
.51				inc BufPtr
				bne .6
				inc BufPtr+1
				
.6				inc BUF.COffset
				bne .7
				inc BUF.COffset+1
				
.7				dex
				bne .1
				
.8				bit bSelected
				bpl .80
				jsr SCRN.ToggleSel

.80				ldy #0

.81				lda SEQ.CEOL,y
				beq .82
				jsr SCRN.ToLineBuf
				iny
				bra .81
				
.82				sta (ZPPTR1)
				
				>PUSHBI 0
				>LDYA LineBufPtr
				>SYSCALL printf
.9				rts			
*--------------------------------------
SCRN.ToggleSel	ldy #0
				
				lda bSelected
				eor #$ff
				sta bSelected
				
				bpl .1
				
				ldy #6

.1				lda SEQ.NORM,y
				beq .8
				
				jsr SCRN.ToLineBuf
				iny
				bra .1

.8				rts
*--------------------------------------
SCRN.ToLineBuf	sta (ZPPTR1)
				inc ZPPTR1
				bne .8
				inc ZPPTR1+1
.8				rts
*--------------------------------------
SCRN.UpdateStatusBar 
				>PUSHBI 1
				>PUSHB.G ScreenH
				>PUSHBI 2
				>LDYA L.MSG.GOTOXY
				>SYSCALL printf
				bcs .9
				
				>PUSHBI 0
				>LDYA L.SEQ.INV
				>SYSCALL printf
				bcs .9

				>LDA.G PromptMode
				bne .2
				
				>LDA.G FileY
				tax
				iny
				lda (pData),y
				inx
				bne .1
				
				inc
.1				>PUSHA
				txa
				>PUSHA
				
				>LDA.G FileX
				inc
				>PUSHA
				
				>PUSHW BufLen

				>PUSHBI 5
				>LDYA L.MSG.STATUSBAR
				>SYSCALL printf
				bcs .9
				
				lda #58
				jsr SCRN.ClearEOLA
				bcs .9
				>PUSHBI 0
				>LDYA L.SEQ.NORM
				>SYSCALL printf
				bcs .9

				jmp SCRN.GotoCurXCurY
.9				rts
*--------------------------------------
.2				eor #PromptModeHelp
				beq .3
				>PUSHW pData
				lda #2
				
.3				>PUSHA

				>LDA.G ScreenH
				jsr SCRN.InvLineA
				bcs .9
				
				ldy #PromptMode
				lda (pData),y
				tax
				
				>LDYA L.MSG.STATUSBAR,x
				>SYSCALL printf
				bcs .9
				rts
*--------------------------------------
SCRN.UpdateStatusBarErrA
				pha
				ldy #ScreenH
				lda (pData),y
				dec
				jsr SCRN.InvLineA
				
				pla
				>PUSHA
				>PUSHBI 1
				>LDYA L.MSG.STATUSBAR.E
				>SYSCALL printf
				rts	
*--------------------------------------
SCRN.CursorAdjust
.1				jsr BUF.ComputeCOffset
				bcc .8
				
				>DECW.G FileY
								
				>STZ.G FileX

				bra .1

.8				rts
*--------------------------------------
SCRN.ClearEOLA	pha

.1				pla
				ldy #ScreenW
				cmp (pData),y
				bcs .8
				inc
				pha
				
				lda #' '
				>SYSCALL PutChar
				bcc .1
				plx
				rts
				
.8				clc
.9				rts
*--------------------------------------
SCRN.InvLineA	sta .2+1
				lda #1
				>PUSHA
				lda .2+1
				>PUSHA
				>PUSHBI 2
				>LDYA L.MSG.GOTOXY
				>SYSCALL printf
				bcs .9
				>PUSHBI 0
				>LDYA L.SEQ.INV
				>SYSCALL printf
				bcs .9
				
				ldy #ScreenW
				lda (pData),y

.1				pha
				
				lda #' '
				>SYSCALL PutChar
				pla
				bcs .9
				
				dec
				bne .1
				
				lda #1
				>PUSHA
.2				lda #$ff
				>PUSHA
				>PUSHBI 2
				>LDYA L.MSG.GOTOXY
				>SYSCALL printf
				
.9				rts				
*--------------------------------------
SCRN.GotoCurXCurY	
				ldy #CurX
				lda (pData),y
				tax
				ldy #CurY
				lda (pData),y
				tay
				
GotoXY			txa
				inc
				>PUSHA
				tya
				inc
				inc
				>PUSHA
				>PUSHBI 2
				>LDYA L.MSG.GOTOXY
				>SYSCALL printf
GotoXY.RTS		rts
*--------------------------------------
SCRN.UpdateViewPort	
				lda #$ff
				sta bNotRepaint
				
.1				ldy #FileX
				lda (pData),y
				ldy #ScreenX
				cmp (pData),y
				bcs .2					out of left border ?
				
				ldy #ScreenX			yes
				lda (pData),y
				dec
				sta (pData),y
				stz bNotRepaint
				bra .1
				
.2				clc
				ldy #ScreenX
				lda (pData),y
				
				ldy #ScreenW
				adc (pData),y
				dec
				ldy #FileX
				cmp (pData),y
				bcs .3					out of right border ?
				ldy #ScreenX
				lda (pData),y
				inc
				sta (pData),y
				stz bNotRepaint
				bra .2
				
.3				ldy #FileY+1
				lda (pData),y
				tax
				dey
				lda (pData),y			A,X = FileY
				
				ldy #ScreenY
				cmp (pData),y
				iny
				txa
				sbc (pData),y
				bcs .4					out of top border ?
				
				ldy #ScreenY			move viewport up one line....
				lda (pData),y
				sbc #0					CC from BCS
				sta (pData),y
				iny
				lda (pData),y
				sbc #0
				sta (pData),y
				
				bit bNotRepaint
				bpl .3
				jsr SCRN.scrollDn		...and move content one line down	
				bcs GotoXY.RTS
				
.4				ldy #ScreenY+1
				lda (pData),y
				tax
				dey
				lda (pData),y
				
				ldy #ViewPortHm1
				clc
				adc (pData),y
				bcc .41
				inx
				
.41				ldy #FileY				A,X = screen bottom
				cmp (pData),y
				txa
				iny						FileY > screen bottom ? 
				sbc (pData),y
				bcs .5					CS : fileY>= screen bottom
				
*				clc						from BCS
				ldy #ScreenY			move viewport down one line....
				lda (pData),y
				adc #1
				sta (pData),y
				iny
				lda (pData),y
				adc #0
				sta (pData),y
				
				bit bNotRepaint
				bpl .4
				
				jsr SCRN.scrollUp		...and move content one line up		
				bcs .9
				bra .4 
				
.5				sec
				ldy #FileX
				lda (pData),y
				ldy #ScreenX
				sbc (pData),y
				ldy #CurX
				sta (pData),y
				
				sec
				ldy #FileY
				lda (pData),y
				ldy #ScreenY
				sbc (pData),y
				ldy #CurY
				sta (pData),y
				
				bit bNotRepaint
				bmi .6
				
				jsr SCRN.UpdateMain
				bcs .9
				
.6				jsr SCRN.UpdateStatusBar			
				bcs .9
				
				jmp SCRN.GotoCurXCurY
.9				rts
*--------------------------------------
SCRN.scrollUp	ldy #CurX
				lda (pData),y
				inc
				>PUSHA
				
				ldy #CurY
				lda (pData),y
				inc
				inc
				>PUSHA
				>PUSHBI 2
				>LDYA L.SEQ.SCROLLUP
				>SYSCALL printf
				bcs .9
				
				ldy #ScreenY+1
				lda (pData),y
				tax
				dey
				lda (pData),y
				clc
				ldy #ViewPortHm1
				adc (pData),y
				bcc .1
				inx
.1				jsr BUF.GetLineAX
				bcs .9
				
				ldy #ViewPortHm1
				lda (pData),y
				jmp SCRN.UpdateLineAtALenY

.9				rts
*--------------------------------------
SCRN.scrollDn	ldy #CurX
				lda (pData),y
				inc
				>PUSHA
				
				ldy #CurY
				lda (pData),y
				inc
				inc
				>PUSHA
				>PUSHBI 2
				>LDYA L.SEQ.SCROLLDN
				>SYSCALL printf
				bcs .9

				ldy #ScreenY+1
				lda (pData),y
				tax
				dey
				lda (pData),y
				jsr BUF.GetLineAX
				bcs .9
				
				lda #0
				jmp SCRN.UpdateLineAtALenY
.9				rts
*--------------------------------------
MAN
SAVE USR/SRC/BIN/EDIT.S.SCRN
LOAD USR/SRC/BIN/EDIT.S
ASM
