'===========================================================================
' Subject: STRING EDIT/INPUT ROUTINE          Date: 04-21-96 (21:25)       
'  Author: Naushir Patuck                     Code: QB, QBasic, PDS        
'  Origin: naushir@pl.jaring.my             Packet: TEXT.ABC
'===========================================================================
'This is a useful string Editing/Input routine I wrote
'to get rid of the horrors of Input forever :-)

DECLARE SUB enter (text$, min!, max!, permitted$, caps!, justify!, password!, insert!, cursorvisible!, fillchar$)

CLS
LOCATE 1, 4
PRINT "Name : [" + STRING$(20, " ") + "]";
LOCATE , 12

'Text to be edited.  If left as "", a new string will be created
text$ = "Jack"

'Minimum number of characters
min = 2

'Maximum number of characters
max = 20

'All the characters which are
'allowed to be entered
'NOTE : Space should also be included here (if needed).
permitted$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "

'Set case of text$
'0 = As is entered
'1 = All characters changed are upper case
caps = 0

'Justification of text$
'0 = None
'1 = Left Justify
'2 = Right Justify
justify = 0

'Set password type display
'0 = Off
'1 = On
password = 0

'Set insert state
'0 = Off
'1 = On
'Note : Insert can be toggled on and off with the
'       INSERT Key when the subroutine is running
insert = 1

'Is cursor visible or not
'0 = No
'1 = Yes
cursorvisible = 1

'This fills the rest of the string (the empty part)
'with the character provided by fillchar$ during display.
'NOTE : This is just for the display of the string on the screen
'       and will not actually be added text$
fillchar$ = "ù"

'Calling routine
enter text$, min, max, permitted$, caps, justify, password, insert, cursorvisible, fillchar$

LOCATE 3, 4
PRINT "The Name you entered was '" + text$ + "'"
LOCATE 4, 4
PRINT "Length of string :"; LEN(text$)
                
'Pls send comments/suggestions to :
'Naushir Patuck
'naushir@pl.jaring.my

SUB enter (text$, min, max, permitted$, caps, justify, password, insert, cursorvisible, fillchar$)

oldxpos = POS(0)
oldypos = CSRLIN
cursorposn = LEN(text$) + 1
ended = 0
xpos = oldxpos
ypos = oldypos

IF caps THEN text$ = UCASE$(text$)

IF cursorvisible = 1 THEN
  IF insert = 1 THEN
    LOCATE ypos, xpos, 1, 7, 7
  ELSE
    LOCATE ypos, xpos, 1, 0, 7
  END IF
ELSE
  cursorvisible = 0
END IF

IF password = 1 THEN
  PRINT STRING$(LEN(text$), 254) + (STRING$(max - LEN(text$), fillchar$))
ELSE
  PRINT text$ + STRING$(max - LEN(text$), fillchar$)
END IF

xpos = xpos + LEN(text$)
oldtext$ = text$

DO
  
  LOCATE ypos, xpos, cursorvisible
  
  x$ = ""
  WHILE LEN(x$) = 0
    x$ = INKEY$
  WEND
  
  SELECT CASE x$
      
    CASE CHR$(27)                                 ' ESC Key
      IF oldtext$ <> "" THEN
        text$ = oldtext$
        cursorposn = LEN(text$) + 1
        xpos = oldxpos + LEN(text$)
      ELSE
        SOUND 600, 2
      END IF
      
    CASE CHR$(0) + CHR$(82)                       ' Insert Key
      IF cursorvisible THEN
        IF insert = 1 THEN
          LOCATE ypos, xpos, 1, 0, 7
          insert = 0
        ELSE
          LOCATE ypos, xpos, 1, 7, 7
          insert = 1
        END IF
      ELSE
        SOUND 600, 2
      END IF
      
    CASE CHR$(0) + CHR$(75)                       ' Left Key
      IF (cursorvisible) AND (password <> 1) AND (cursorposn <> 1) THEN
        cursorposn = cursorposn - 1
        xpos = xpos - 1
      ELSE
        SOUND 600, 2
      END IF
      
    CASE CHR$(0) + CHR$(77)                       ' Right Key
      IF (cursorvisible) AND (password <> 1) THEN
        IF cursorposn <= LEN(text$) THEN
          cursorposn = cursorposn + 1
          xpos = xpos + 1
        ELSE
          SOUND 600, 2
        END IF
      ELSE
        SOUND 600, 2
      END IF
      
    CASE CHR$(8)                                  ' Backspace
      IF NOT (cursorposn = 1) THEN
        IF cursorposn <= LEN(text$) THEN
          text$ = LEFT$(text$, cursorposn - 2) + RIGHT$(text$, LEN(text$) - cursorposn + 1)
        ELSEIF cursorposn = (LEN(text$) + 1) THEN
          text$ = LEFT$(text$, LEN(text$) - 1)
        END IF
        cursorposn = cursorposn - 1
        xpos = xpos - 1
      ELSE
        SOUND 600, 2
      END IF
      
    CASE CHR$(0) + CHR$(83)                       ' Delete Key
      IF (password <> 1) AND cursorposn <= LEN(text$) THEN
        text$ = LEFT$(text$, cursorposn - 1) + RIGHT$(text$, LEN(text$) - cursorposn)
      ELSE
        SOUND 600, 2
      END IF
      
    CASE CHR$(13)                                 ' Enter Key
      IF (LEN(text$) >= min) THEN
        ended = 1
      ELSE
        SOUND 600, 2
        ended = 0
      END IF
      
    CASE CHR$(0) + CHR$(71)                       ' Home Key
      IF (cursorvisible) AND (password <> 1) THEN
        cursorposn = 1
        xpos = oldxpos
      ELSE
        SOUND 600, 2
      END IF
      
    CASE CHR$(0) + CHR$(79)                       ' End Key
      IF (cursorvisible) AND (password <> 1) THEN
        cursorposn = LEN(text$) + 1
        xpos = oldxpos + LEN(text$)
      ELSE
        SOUND 600, 2
      END IF
      
    CASE ELSE                                     ' Any Other Key
      IF INSTR(permitted$, x$) THEN
        IF (insert) THEN
          IF LEN(text$) <> max THEN
            text$ = LEFT$(text$, cursorposn - 1) + x$ + RIGHT$(text$, LEN(text$) - cursorposn + 1)
            cursorposn = cursorposn + 1
            xpos = xpos + 1
          ELSE
            SOUND 600, 2
          END IF
        ELSE
          IF cursorposn - 1 <> max THEN
            IF cursorposn = LEN(text$) + 1 THEN
              text$ = text$ + x$
            ELSE
              text$ = LEFT$(text$, cursorposn - 1) + x$ + MID$(text$, cursorposn + 1, LEN(text$) - cursorposn)
            END IF
            cursorposn = cursorposn + 1
            xpos = xpos + 1
          ELSE
            SOUND 600, 2
          END IF
        END IF
      ELSE
        SOUND 600, 2
      END IF
      
  END SELECT
  
  IF caps THEN text$ = UCASE$(text$)
  
  LOCATE oldypos, oldxpos
  PRINT SPACE$(max)
  
  LOCATE oldypos, oldxpos, cursorvisible
  
  IF password = 1 THEN
    PRINT STRING$(LEN(text$), 254) + (STRING$(max - LEN(text$), fillchar$))
  ELSE
    PRINT text$ + STRING$(max - LEN(text$), fillchar$)
  END IF
  
LOOP UNTIL ended

IF justify = 2 THEN                               ' Right Justify
  text$ = STRING$(max - LEN(text$), " ") + text$
ELSEIF justify = 1 THEN                           ' Left Justify
  text$ = text$ + (STRING$(max - LEN(text$), " "))
END IF

LOCATE ypos, xpos, 1, 7, 7

END SUB
