home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / bp_6_93 / bonus / winer / editor.bas < prev    next >
BASIC Source File  |  1992-05-12  |  4KB  |  141 lines

  1. '*********** EDITOR.BAS - polled data entry routine
  2.  
  3. 'Copyright (c) 1992 Ethan Winer
  4.  
  5. DEFINT A-Z
  6. DECLARE SUB Editor (Text$, LeftCol, RightCol, KeyCode)
  7.  
  8. COLOR 7, 1                      'clear to white on blue
  9. CLS
  10.  
  11. Text$ = "This is a test"        'make some sample text
  12. LeftCol = 20                    'set the left column
  13. RightCol = 60                   'and the right column
  14. LOCATE 10                       'set the line number
  15. COLOR 0, 7                      'set the field color
  16.  
  17. DO                              'edit until Esc or Enter
  18.    CALL Editor(Text$, LeftCol, RightCol, KeyCode)
  19. LOOP UNTIL KeyCode = 13 OR KeyCode = 27
  20.  
  21. SUB Editor (Text$, LeftCol, RightCol, KeyCode)
  22.  
  23.   '----- Find the cursor's size.
  24.   DEF SEG = 0
  25.   IF PEEK(&H463) = &HB4 THEN
  26.      CsrSize = 12               'mono uses 13 scan lines
  27.   ELSE
  28.      CsrSize = 7                'color uses 8
  29.   END IF
  30.  
  31.   '----- Work with a temporary copy.
  32.   Edit$ = SPACE$(RightCol - LeftCol + 1)
  33.   LSET Edit$ = Text$
  34.  
  35.   '----- See where to begin editing and print the string.
  36.   TxtPos = POS(0) - LeftCol + 1
  37.   IF TxtPos < 1 THEN TxtPos = 1
  38.   IF TxtPos > LEN(Edit$) THEN TxtPos = LEN(Edit$)
  39.  
  40.   LOCATE , LeftCol
  41.   PRINT Edit$;
  42.  
  43.   '----- This is the main loop for handling key presses.
  44.   DO
  45.      LOCATE , LeftCol + TxtPos - 1, 1
  46.  
  47.      DO
  48.        Ky$ = INKEY$
  49.      LOOP UNTIL LEN(Ky$)        'wait for a keypress
  50.  
  51.      IF LEN(Ky$) = 1 THEN       'create a key code
  52.        KeyCode = ASC(Ky$)       'regular character key
  53.      ELSE                       'extended key
  54.        KeyCode = -ASC(RIGHT$(Ky$, 1))
  55.      END IF
  56.  
  57.      '----- Branch according to the key pressed.
  58.      SELECT CASE KeyCode
  59.  
  60.        '----- Backspace: decrement the pointer and the
  61.        '      cursor, and ignore if in the first column.
  62.        CASE 8
  63.          TxtPos = TxtPos - 1
  64.          LOCATE , LeftCol + TxtPos - 1, 0
  65.          IF TxtPos > 0 THEN
  66.            IF InsStatus THEN
  67.              MID$(Edit$, TxtPos) = MID$(Edit$, TxtPos + 1) + " "
  68.            ELSE
  69.              MID$(Edit$, TxtPos) = " "
  70.            END IF
  71.            PRINT MID$(Edit$, TxtPos);
  72.          END IF
  73.  
  74.        '----- Enter or Escape: this block is optional in
  75.        '      case you want to handle these separately.
  76.        CASE 13, 27
  77.          EXIT DO                'exit the subprogram
  78.  
  79.        '----- Letter keys: turn off the cursor to hide
  80.        '      the printing, handle Insert mode as needed.
  81.        CASE 32 TO 254
  82.          LOCATE , , 0
  83.          IF InsStatus THEN      'expand the string
  84.            MID$(Edit$, TxtPos) = Ky$ + MID$(Edit$, TxtPos)
  85.            PRINT MID$(Edit$, TxtPos);
  86.          ELSE                   'else insert character
  87.            MID$(Edit$, TxtPos) = Ky$
  88.            PRINT Ky$;
  89.          END IF
  90.          TxtPos = TxtPos + 1    'update position counter
  91.  
  92.        '----- Left arrow: decrement the position counter.
  93.        CASE -75
  94.          TxtPos = TxtPos - 1
  95.  
  96.        '----- Right arrow: increment position counter.
  97.        CASE -77
  98.          TxtPos = TxtPos + 1
  99.  
  100.        '----- Home: jump to the first character position.
  101.        CASE -71
  102.          TxtPos = 1
  103.  
  104.        '----- End: search for the last non-blank, and
  105.        '      make that the current editing position.
  106.        CASE -79
  107.          FOR N = LEN(Edit$) TO 1 STEP -1
  108.            IF MID$(Edit$, N, 1) <> " " THEN EXIT FOR
  109.          NEXT
  110.          TxtPos = N + 1
  111.          IF TxtPos > LEN(Edit$) THEN TxtPos = LEN(Edit$)
  112.  
  113.        '----- Insert key: toggle the Insert state and
  114.        '      adjust the cursor size.
  115.        CASE -82
  116.          InsStatus = NOT InsStatus
  117.          IF InsStatus THEN
  118.            LOCATE , , , CsrSize \ 2, CsrSize
  119.          ELSE
  120.            LOCATE , , , CsrSize - 1, CsrSize
  121.          END IF
  122.  
  123.        '----- Delete: delete the current character and
  124.        '      reprint what remains in the string.
  125.        CASE -83
  126.          MID$(Edit$, TxtPos) = MID$(Edit$, TxtPos + 1) + " "
  127.          LOCATE , , 0
  128.          PRINT MID$(Edit$, TxtPos);
  129.  
  130.        '---- All other keys: exit the subprogram
  131.        CASE ELSE
  132.          EXIT DO
  133.      END SELECT
  134.  
  135.   '----- Loop until the cursor moves out of the field.
  136.   LOOP UNTIL TxtPos < 1 OR TxtPos > LEN(Edit$)
  137.  
  138.   Text$ = RTRIM$(Edit$)         'trim the text
  139.  
  140. END SUB
  141.