'===========================================================================
' Subject: EDIT/INPUT STRING                  Date: 11-09-95 (11:16)       
'  Author: Alexander Podkolzin                Code: PB                     
'  Origin: app@sbank.e-burg.su              Packet: TEXT.ABC
'===========================================================================
'------------------------------ Demo here ----------------------------------
  DEFINT a-z
  CLS
  COLOR 0,7
  LOCATE 25,1
  PRINT "        You can use Backspace-,Ins-,Home-,Del-,End- keys and arrow keys.        ";
  s$=InputStr$("I am a string, edit me, please.",3,10,40,15,4)
  COLOR 14,1
  LOCATE 12,10
  PRINT s$
  END
'---------------------------------------------------------------------------
'ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
'³ FUNCTION : InputStr$                                                    ³
'³ CALL     : s$ = InputStr$(Ed$,y%,x%,n%,textcolor%,backcolor%)           ³
'³ Where    : Ed$   - string to edit,                                      ³
'³          : y%,x% - place to edit/input string from,                     ³
'³          : n%    - number of characters to edit/input.                  ³
'³ RETURNS  : edit/input string, or null-string, if <Esc> is pressed.      ³
'³ AUTHOR   : Alexander Podkolzin (app@sbank.e-burg.su).                   ³
'ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
' Source code is formatted by my program "PBB".
'---------------------------------------------------------------------------
'
  FUNCTION InputStr$(Ed$,y%,x%,n%,textcolor%,backcolor%)
'
     STATIC Ins%                        ' Switcher of Ins-mode.
'
     Enter$ = CHR$(13)                  ' List of keys, we need in
     Esc$ = CHR$(27)                    ' our function.
     BcSp$ = CHR$( 8)                   '
     Ins$ = CHR$(0,82)                  ' Equals to CHR$(0)+CHR$(82)
     Home$ = CHR$(0,71)                 '
     Del$ = CHR$(0,83)
     EndKey$ = CHR$(0,79)
     LeftKey$ = CHR$(0,75)
     RightKey$= CHR$(0,77)
'
     OldX% = POS(0)                     ' As well-bred programmers, :)
     OldY% = CSRLIN                     ' we have to save screen parameters.
     OldColor% = pbvScrnTxtAttr         ' Internal PB variables: color,
     Vis%=pbvCursorVis                  ' coursor is visiable (TRUE or FALSE),
     Sl1%=pbvCursor1                    ' top line of cursor,
     Sl2%=pbvCursor2                    ' bottom line.
'
     Ptr% = x%                          ' Current cursor position.
     COLOR textcolor%,backcolor%        ' Colors we'll use in our function.
     LOCATE y%,x%
     PRINT Ed$
'
     Edit$=GetString$(y%,x%,n%)
     LOCATE y%,x%
     PRINT Edit$
     DO                                 ' Main loop:
        IF Ptr%<x% THEN                 ' First of all we
           Ptr%=x%                      ' have
           ITERATE LOOP                 ' to
        END IF                          ' normalize
        IF Ptr%>x%+n%-1 THEN            ' the
           Ptr%=x%+n%-1                 ' cursor position.
           ITERATE LOOP
        END IF
        IF Ins%=0 THEN
           LOCATE y%,Ptr%,1,7,8         ' Usual cursor
        ELSE                            '
           LOCATE y%,Ptr%,1,5,8         ' "Ins" cursor
        END IF
        WHILE NOT INSTAT: WEND
        s$ = INKEY$
$IF 1   '----------------------------------------------------------------
'
' This < $IF...$ENDIF > block is for Russian PB-users only, as INKEY$
' does not distinguish lowercase Russian character "à". (PB3.0)
'
        IF LEN(s$)=2 AND ASC(LEFT$(s$,1))=0 AND ASC(RIGHT$(s$,1))=0 THEN
           s$=CHR$(224)
        END IF
'
$ENDIF  '----------------------------------------------------------------
        SELECT CASE s$
           CASE Esc$
              InputStr$=""
              EXIT LOOP
           CASE Enter$
              InputStr$=RTRIM$(GetString(y%,x%,n%))
              EXIT LOOP
           CASE Ins$
              Ins% = Ins% XOR 1
              ITERATE LOOP
           CASE Home$
              Ptr%=x%
              ITERATE LOOP
           CASE EndKey$
              Ptr%=x%+LEN(RTRIM$(GetString(y%,x%,n%)))
              ITERATE LOOP
           CASE LeftKey$
              DECR Ptr%
              ITERATE LOOP
           CASE RightKey$
              INCR Ptr%
              ITERATE LOOP
           CASE Del$
              m%=Ptr%-x%
              k%=x%+n%-Ptr%
              Edit$=GetString$(y%,x%,m%)+GetString$(y%,Ptr%+1,k%)
           CASE BcSp$                   ' Exception!
              DECR Ptr%                 '
              IF Ptr%<x% THEN           ' So, normalize the cursor
                 Ptr%=x%                ' position here.
                 ITERATE LOOP           '
              END IF
              m%=Ptr%-x%
              k%=x%+n%-Ptr%
              Edit$=GetString$(y%,x%,m%)+GetString$(y%,Ptr%+1,k%)
           CASE ELSE
              IF LEN(s$)=1 THEN
                 m%=Ptr%-x%             ' Symbols before cursor
                 k%=x%+n%-Ptr%-1        ' Symbols after cursor
                 IF Ins%=1 THEN         ' Editing in Ins-mode
                    Edit$=GetString$(y%,x%,m%)+ s$ +GetString$(y%,Ptr%,k%)
                    Edit$=LEFT$(Edit$,n%)
                 ELSE                   ' Editing in usual mode
                    Edit$=GetString$(y%,x%,m%)+ s$ +GetString$(y%,Ptr%+1,k%)
                 END IF
                 INCR Ptr%
              END IF
        END SELECT
        LOCATE y%,x%,0,0,0              ' Make cursor invisiable
        PRINT Edit$                     '
     LOOP                               ' End of main loop.
     IF Vis% THEN                       ' Now we have
        Vis%=0                          ' to
     ELSE                               ' restore
        Vis%=1                          ' our parameters.
     END IF                             '
     ct%=OldColor% AND 15
     cb%=OldColor%\16
     COLOR ct%,cb%
     LOCATE Oldy%,OldX%,Vis%,Sl1%,Sl2%
  END FUNCTION                          ' BYE !
'---------------------------------------------------------------------------
' This function is not fast, but we don't need speed here.
'
  FUNCTION GetString$(y%,x%,n%)         ' Reads string from the screen.
     s$=""                              ' y%,x% - string coordinates,
     FOR i%=0 TO n%-1                   ' n% - characters to read.
        s$ = s$ + CHR$(SCREEN(y%,x%+i%))
     NEXT
     GetString$ = s$
  END FUNCTION
'---------------------------------------------------------------------------



