;Input with system monitor.
;Problems/Comments/Questions/Money? contact Dan Englender (dan@calc.org)


;This application is meant to demonstrate how to use "system input" in an
;application.  This is homescreen-like input, with full access to all TIOS
;menus, the catalog, etc.  The routine has been customized slightly, adding a
;softkey menu, to show the possibilities.  This can be removed if necessary, or
;customized further.

;GetMathInput is the main input routine.  It opens the edit buffer and starts the
;system monitor.  ContinueMathInput is used to edit an already existing variable.
;If you want to customize the routine, look at gmcxMain, this is the keypress
;handler.



 nolist
 include "ti83plus.inc"
 include "extraequ.inc"
 list
 globals on

  
;Header
 db 080h,0Fh    ;Field: Program length
 db   00h,00h,00h,00h 
 db 080h,012h    ;Field: Program type
 db   01h,04h  
 db 080h,021h    ;Field: App ID
 db   01h       
 db 080h,031h    ;Field: App Build
 db   01h    
 db 080h,048h    ;Field: App Name
 db "SysInTst"
 db 080h,081h    ;Field: App Pages
 db 01h         
 db 080h,090h    ;No default splash screen
 db 03h,026h ,09h,04h, 04h,06fh,01bh,80h     ;Field: Date stamp- 5/12/1999
 db 02h,0dh,040h                         
 db 0a1h ,06bh ,099h ,0f6h ,059h ,0bch ,067h 
 db 0f5h ,085h ,09ch ,09h ,06ch ,0fh ,0b4h ,03h ,09bh ,0c9h 
 db 03h ,032h ,02ch ,0e0h ,03h ,020h ,0e3h ,02ch ,0f4h ,02dh 
 db 073h ,0b4h ,027h ,0c4h ,0a0h ,072h ,054h ,0b9h ,0eah ,07ch 
 db 03bh ,0aah ,016h ,0f6h ,077h ,083h ,07ah ,0eeh ,01ah ,0d4h 
 db 042h ,04ch ,06bh ,08bh ,013h ,01fh ,0bbh ,093h ,08bh ,0fch 
 db 019h ,01ch ,03ch ,0ech ,04dh ,0e5h ,075h 
 db 80h,7Fh      ;Field: Program Image length
 db   0,0,0,0    
 db   0,0,0,0    
 db   0,0,0,0    
 db   0,0,0,0    
 db   0,0,0,0    
;End of header data



;Equates
TempSP        EQU appBackUpScreen

Init:
       B_CALL ForceFullScreen             ;Start of the program
       call   GetMathInput                ;Get some input!
       jp     Quit



CreateEntry:                              ;Create the temp prog used for input
       call   LookupEntry
       ret    nc
       ld     hl,0
       B_CALL CreateProg
       ret
DeleteEntry:                              ;Delete the temp prog
       call   LookupEntry
       ret    c
       B_CALL DelVar
       ret
LookupEntry:                              ;Lookup temp prog in symbol table
       ld     hl,EntryName
       rst    20h
       B_CALL ChkFindSym
       ret

Quit:
       call   DeleteEntry
       B_JUMP JForceCmdNoChar





SetupInputStuff:
                                          ;General input set-up stuff that needs
                                          ; to be run whether we're continuing
                                          ; the input or not.
       set    appAllowContext,(iy+APIFlg) ;To allow Yequ and Graph keys
       B_CALL GrBufClr
       B_CALL ClrScrnFull
       B_CALL HomeUp
       B_CALL CursorOn
       ld     hl,gmMonVectors             ;Our custom vectors!
       B_CALL AppInit
       ld     a,7
       ld     (winBtm),a                  
       ret

GetMathInput:
       call   SetupInputStuff
       call   CreateEntry
       set    cmdVirgin,(iy+cmdFlags)     ;Edit buffer is empty
       call   LookupEntry
       B_CALL SetEmptyEditPtr             ;Setup edit buffer
       ld     (TempSP),sp                 ;Save for later
       call   gmcxRedisp                  ;To get the softkey menu
       B_CALL Mon

ContinueMathInput:
       call   SetupInputStuff
       call   SetupEditProg               ;Not empty...needs to be done differently
       ld     (TempSP),sp
       B_CALL DispEOW                     ;Restore old stuff to screen
       B_CALL IsEditEmpty                 ;Don't want to display a faulty char
       jr     z,SkipInitCurUnder
       ld     hl,(editTail)
       ld     a,(hl)
       ld     (curUnder),a                ;Initialize under the cursor
       ld     (textShadow),a
SkipInitCurUnder:
       call   gmcxRedisp                  ;Display softkey menu stuff
       B_CALL Mon
       
SetupEditProg:
       call   LookupEntry
       B_CALL EditProg                    ;Insert RAM, setup iMathPtrs
       set    editOpen,(iy+editFlags)     ;Editor is running
       ld     hl,(iMathPtr1)
       inc    hl
       inc    hl
       ld     (editTop),hl                ;Top of edit session
       push   hl
       ld     hl,(iMathPtr2)
       ld     (editCursor),hl
       push   hl
       ex     de,hl
       ld     hl,(iMathPtr3)
       B_CALL CpHLDE
       jr     nc,SetupEditProgSwapSkip
       ex     de,hl
SetupEditProgSwapSkip:
       ld     (editBtm),hl
       ld     (editTail),hl
       pop    hl
       pop    de
       or     a
       sbc    hl,de
       ret    z
       ld     c,l
       ld     b,h
       ld     hl,(editCursor)
       dec    hl
       ld     de,(editTail)
       dec    de
       lddr
       inc    hl
       ld     (editCursor),hl
       inc    de
       ld     (editTail),de
       ret




gmMonVectors:
       dw     gmcxMain
       dw     gmcxPPutAway
       dw     gmcxPutAway
       dw     gmcxRedisp
       dw     gmcxErrorEP
       dw     gmcxSizeWind
       db     0Eh                         ;Process menus + auto scroll + textsave

gmcxSizeWind:
                                          ;We're not allowing Rcl in this example
                                          ;The window size wont change
       ret

gmcxErrorEP:
                                          ;Not dealing with error goto either.
       ret


gmcxRedisp:
       ld     hl,(curRow)
       push   hl
       B_CALL HomeUp
       ld     b,127
       ld     hl,textShadow
gmcxRedispLoop:                           ;Restore the screen from textShadow
       ld     a,(hl)
       B_CALL PutC
       inc    hl
       djnz   gmcxRedispLoop
       ld     a,(hl)
       B_CALL PutMap
       pop    hl
       ld     (curRow),hl
       ld     hl,SoftKeyText
       call   SoftKey                     ;Display softkey menu
       ret


gmcxPutAway:
       B_CALL CloseEditEqu                ;Close the equation
       B_CALL ReloadAppEntryVecs
       xor    a
       ld     (iy+textFlags),a
       call   DeleteEntry
       bit    monAbandon,(iy+monFlags)    ;Is calc turning off?
       jr     nz,gmcxPutAwayOff
       B_JUMP JForceCmdNoChar
gmcxPutAwayOff:
       B_JUMP PutAway



gmcxPPutAway:
       ret


gmcxMain:
       ld     hl,winBtm
       ld     (hl),7                      ;So the menu can't be overwritten
       push   hl
       call   gmcxDoMain
       pop    hl
       ld     (hl),8                      ;Need a full screen for catalog
       ret       
             

gmcxDoMain:
       cp     kGraph
       jp     z,gmcxMQuit
       cp     kYequ
       jp     z,gmcxMEval
       cp     kQuit
       jp     z,gmcxMQuit
       cp     kEnter
       jp     z,gmcxMEnter
       cp     kLeft
       jr     nz,gmcxMNotLeft
       B_CALL CursorLeft
       ret
gmcxMNotLeft:
       cp     kRight
       jp     z,gmcxMRight
       cp     kUp
       jp     z,gmcxMUp
       cp     kDown
       jr     nz,gmcxMNotDown
       B_CALL CursorDown
       ret
gmcxMNotDown:
       cp     kBOL                               ;Beginning of Line
       jp     z,gmcxMBOL
       cp     kEOL                               ;End of Line
       jp     z,gmcxMEOL
       cp     kIns                               ;Insert
       jp     z,gmcxMIns
       cp     kDel
       jp     z,gmcxMDel
       cp     kClear
       jp     z,gmcxMClear
       cp     kTrace+1
       ret    c
       B_CALL InsDisp                            ;Display and update buffer
       ret


gmcxMRight:
       res    textInsMode,(iy+textFlags)
       B_CALL BufRight                           ;Update edit pointers
       jr     z,gmcxMRightDone                   ;Already at the end of buffer?
       res    textScrolled,(iy+textFlags)
       B_CALL PutTokString
       bit    textScrolled,(iy+textFlags)
       jr     z,gmcxMRightDone                   ;If no scroll, we don't have to
       ld     hl,(curRow)                        ; worry about redisplaying the
       push   hl                                 ; text afterwards
       ld     hl,(editBtm)
       ld     de,(editTail)
       B_CALL DispForward
       pop    hl
       ld     (curRow),hl
gmcxMRightDone:
       ret



gmcxMClear:                                      ;Clear screen
       bit    cmdVirgin,(iy+cmdFlags)
       ret    nz
       B_CALL IsEditEmpty
       jr     nz,gmcxMClear2
       B_CALL ClrWindow
       call   gmcxMBOL
       set    cmdVirgin,(iy+cmdFlags)
       B_CALL CanAlphIns
gmcxMClear2:
       B_CALL DispHead
       B_CALL EraseEOW
       B_CALL BufClear
       set    cmdVirgin,(iy+cmdFlags)
       B_CALL CanAlphIns
       ret

gmcxMDel:
       res    textInsMode,(iy+textFlags)
       B_CALL BufDelete                   ;Delete token from buffer
       B_CALL IsEditEmpty
       jr     nz,gmcxMDelNotEmpty
       set    cmdVirgin,(iy+cmdFlags)
gmcxMDelNotEmpty:
       B_CALL DispEOW                     ;Redisplay the rest of the toks
       ret

gmcxMIns:
       ld     a,(flags+textFlags)
       xor    10h                         ;Toggle Insert flag
       ld     (flags+textFlags),a
       ret
       
gmcxMBOL:                                 ;Beginning of line
       res    textScrolled,(iy+textFlags)
gmcxMBOLLoop:
       B_CALL IsAtTop                     ;Have we reached the beginning?
       ret    z                           ;If so, we're done
       B_CALL CursorLeft
       jr     gmcxMBOLLoop


gmcxMEOL:                                 ;End of line
       res    textScrolled,(iy+textFlags)
gmcxMEOLLoop:
       B_CALL IsAtBtm
       ret    z
       call   gmcxMRight
       jr     gmcxMEOLLoop


gmcxMUp:    
       xor    a
       push   af
       res    textScrolled,(iy+textFlags)
       ld     de,(editTop)
       ld     bc,(curRow)
       inc    b
gmcxMUpLoop:
       B_CALL CursorLeft
       ld     hl,(editCursor)
       ld     a,(curRow)
       cp     c                           ;Still on same row?
       jr     nz,gmcxMUpAtNextLine        ;if not, proceed with A<>0
       bit    textScrolled,(iy+textFlags)
       jr     nz,gmcxMUpAtNextLine        ;If we scrolled, then we got to next line
       pop    af                          ;Restore A=0
       push   af
gmcxMUpAtNextLine:
       or     a                           ;If A=0, we shouldn't check the col yet
       jr     z,gmcxMUpCont
       ld     a,(curCol)
       cp     b                           ;Have we gotten back to the right column?
       jr     c,gmcxMUpDone               ;If so, we're done
       push   de
       push   hl
       B_CALL GetPrevTok
       pop    hl
       ld     (editCursor),hl
       pop    de
gmcxMUpCont:
       B_CALL CpHLDE                      ;Gotten to beginning of buffer yet?
       jr     nz,gmcxMUpLoop              ;If not, we can keep going!
gmcxMUpDone:
       pop    af
       or     a
       ret

    
gmcxMQuit:
       call   gmcxQuitStuff
       jp     Quit
gmcxMEnter:                               ;Current ends up going to Quit anyhow
       call   gmcxQuitStuff               ; but in real apps probably will have
       ld     sp,(TempSP)                 ; it's own function.
       ret

gmcxMEval:                                ;Parse edit buffer, and restore it
       call   gmcxQuitStuff
       ld     sp,(TempSP)
       ld     hl,EntryName
       rst    20h
       AppOnErr ParseError
       B_CALL ParseInp                    ;Parse it!
       AppOffErr
       ld     a,(OP1)
       and    1Fh                         ;Check if answer is a real
       jr     nz,TypeError
       B_CALL ClrLCDFull
       ld     a,8
       B_CALL FormReal                    ;Convert real to ASCII
       B_CALL HomeUp
       ld     hl,ResTXT
       call   PutSApp
       B_CALL NewLine
       ld     hl,OP3
       B_CALL PutS
       ld     hl,SoftKeyText2
       call   SoftKey
EvalLoop:
       B_CALL GetKey
       cp     kGraph
       jp     z,Quit
       cp     kQuit
       jp     z,Quit
       cp     kYequ
       jr     nz,EvalLoop
       jp     ContinueMathInput

ParseError:
       ld     hl,ParseErrorTXT
       jr     ErrorCont
TypeError:
       ld     hl,TypeErrorTXT
ErrorCont:
       push   hl
       B_CALL ClrLCDFull
       B_CALL HomeUp
       pop    hl
       call   PutSApp
       B_CALL GetKey
       jp     ContinueMathInput

ParseErrorTXT:
       db     "An error occured"
       db     "while parsing.",0
TypeErrorTXT:
       db     "You didn't enter"
       db     "a number!",0
ResTXT:
       db     "Result:",0
gmcxQuitStuff:
       B_CALL CanAlphIns                  ;Turn off Alpha and Ins indicators
       B_CALL DispTail                    ;Display rest of the stuff after cursor
       B_CALL CloseEditEqu                ;Close the edit buffer
       B_CALL ReloadAppEntryVecs          ;Reload default system vectors
       ld     a,8                         ;Reset window to proper size
       ld     (winBtm),a
       ret



EntryName:
       db     ProgObj,026h,"tmp",0
SoftKeyText2:
       db     "EDIT",0
       db     0,0,0
       db     "QUIT",0
SoftKeyText:
       db     "EVAL",0
       db     0,0,0
       db     "QUIT",0             

SoftKey:
				;Input
				; hl -> key text
				;Ouput
				; soft keys are displayed to screen
	push	hl
	ld	bc,0007h
	ld	de,95*256+7
	ld	h,1
	B_CALL	ILine
	ld	b,19
	ld	d,b
	ld	e,0
	B_CALL	ILine
	ld	b,38
	ld	d,b
	B_CALL	ILine
	ld	b,57
	ld	d,b
	B_CALL	ILine
	ld	b,76
	ld	d,b
	B_CALL	ILine
       ld     b,0
       ld     d,b
       B_CALL ILine
       ld     b,95
       ld     d,b
       B_CALL ILine
	pop	hl
	ld	a,57
	ld	(penRow),a
	ld	a,3
SoftKeyLoop:					;loop to display text inside boxes
	ld	(penCol),a
	push	af
	call	VPutSApp
	pop	af
	add	a,19
	cp	98
	jr	nz,SoftKeyLoop
	ret

VPutSApp:                   ;Same as VPutS, but can be used with pointer in app
       ld     a,(hl)
       inc    hl
       or     a
       ret    z
       push   hl
       B_CALL VPutMap
       pop    hl
       jr     VPutSApp

PutSApp:                    ;Same as PutS, but can be used with pointer in app
       ld     a,(hl)
       inc    hl
       or     a
       ret    z
       B_CALL PutC
       jr     PutSApp

