/*procedure PGRSTR, X, Y, ANGLE, FJUST, TEXT, LSTR, BCI {
      CI = 0
      XBOX = YBOX = floatarray(4)
      CH = " "

      CALL PGQCI(CI)
      done = 0

      while (done == 0) {
//     -- Draw current string
          if (LSTR > 0) {
             pgptxt X, Y, ANGLE, FJUST, TEXT[1:LSTR]
             pgqtxt X, Y, ANGLE, FJUST, TEXT[1:LSTR], XBOX, YBOX
             XCUR = XBOX[4]
             YCUR = YBOX[4]
          } else {
             XCUR = X
             YCUR = Y
          }
//         -- Read a character
          JUNK = pgband(0, 1, XCUR, YCUR, XCUR, YCUR, CH)
//         -- Erase old string
          pgsci BCI
          if (LSTR > 0) pgptxt X, Y, ANGLE, FJUST, TEXT[1:LSTR]
          pgsci CI
//         -- Avoid problem with PGPLOT escape character
          if (CH == char(92)) CH = "*"
//         -- Backspace (ctrl H) or delete removes last character
          if (CH == char(8) || CH == char(127)) {
             if (LSTR > 0) TEXT[LSTR] = " "
             if (LSTR > 0) LSTR = LSTR-1
          }
//         -- Ctrl U removes entire string
          if (CH == char(21)) {
             TEXT = " "
             LSTR = 0
          }
//         -- Any other non-printing character terminates input
          if (CH < char(32)) {
             if (LSTR > 0) pgptxt X, Y, ANGLE, FJUST, TEXT[1:LSTR]
             done = 1
//         -- Otherwise, add character to string if there is room
          } else {
             LSTR = LSTR+1
             TEXT += CH
          }
      }
}
*/
//      PROGRAM PGDE14
//-----------------------------------------------------------------------
// Demonstration program for PGPLOT: text input with PGRSTR.
//
// This program illustrates how an interactive program can be written
// using PGPLOT. The program displays a number of active fields. Select
// one of these fields using the cursor (e.g., click the mouse) to 
// activate it; then use the keyboard keys to edit the string displayed
// in the field. Two of the fields have immediate action: 'DRAW' draws
// a simple picture using the parameters specified in the input fields;
// 'EXIT' terminates the program.
//
// A version of the subroutine used here, PGRSTR, may be included in a
// future release of the PGPLOT library.
//-----------------------------------------------------------------------
      NBOX=5
      BOX = floatarray(4,NBOX)
      XV = YV = floatarray(100)
      IVAL = longarray(NBOX)
//      REAL BOX(4,NBOX), X, Y, XX, YY, A, D, XV(100), YV(100)
//      INTEGER IVAL(NBOX)
//      INTEGER PGOPEN, LSTR, I, JUNK, PGCURS, J, NV, BC, FC, CTOI
//      INTEGER II, JJ
//      CHARACTER CH
//      CHARACTER*30 LABEL(NBOX), VALUE(NBOX), RESULT(NBOX)
     RESULT = stringarray(NBOX)

     BOX[*,1] = [0.44, 0.8, 0.79, 0.83]
     BOX[*,2] = [0.44, 0.8, 0.69, 0.73]
     BOX[*,3] = [0.44, 0.8, 0.59, 0.63]
     BOX[*,4] = [0.44, 0.7, 0.29, 0.33]
     BOX[*,5] = [0.44, 0.7, 0.19, 0.23]

     LABEL = ["Number of vertices:","Background Color:","Foreground Color:", " ", " "]
     VALUE  = ["13","0","1","DRAW","EXIT"]
//-----------------------------------------------------------------------
      print "This program requires an interactive device."
      print "It presents a menu with editable fields which can be"
      print "used to set parameters controlling a graph displayed"
      print "beside the menu. To edit a field, first select it"
      print "with the cursor (e.g., click mouse button) then use"
      print "keyboard keys and DEL or ^U. TAB or CR terminates"
      print "editing Click on DRAW to display the graph or EXIT"
      print "to terminate the program."
      print ""

// Open device for graphics.

      if (pgopen("?") <= 0) break
      pgpap 10.0,0.5
      IVAL[1] = 13
      IVAL[2] = 0
      IVAL[3] = 1

// Clear the screen. Draw a frame at the physical extremities of the
// plot, using full-screen viewport and standard window.

      pgpage
      pgsvp 0.0,1.0,0.0,1.0
      pgswin 0.0,2.0,0.0,1.0
      pgscr 0, 0.4, 0.4, 0.4

// Display fields

      done = 0
      while (done == 0) {
      done = 1
      VALUE[1] = sprintf("%6i", int(IVAL[1]))
      VALUE[2] = sprintf("%6i", int(IVAL[2]))
      VALUE[3] = sprintf("%6i", int(IVAL[3]))
      pgsave
      pgbbuf
      pgeras
      pgsci 1
      pgslw 1
      pgsfs 1
      pgsch 1.2
      for I=1,NBOX {
         RESULT[I] = VALUE[I]
         X = BOX[1,I] - 0.04
         Y = BOX[3,I] + 0.01
         pgsci 1
         pgptxt X, Y, 0.0, 1.0, LABEL[I]
         pgrect BOX[1,I], BOX[2,I], BOX[3,I], BOX[4,I]
         X = BOX[1,I] + 0.01
         pgsci 2
         pgptxt X, Y, 0.0, 0.0, VALUE[I]
      }
// Draw picture

      NV = int(min(100,IVAL[1]))
      BC = int(IVAL[2])
      FC = int(IVAL[3])
      pgsci BC
      pgsfs 1
      pgrect 1.05,1.95,0.05,0.95
      pgsci FC
      pgsfs 2
      pgrect 1.05,1.95,0.05,0.95
      if (NV > 3) {
         D = 360.0/NV
         A = -D
         for II=1,NV {
            A = A+D
            XV[II] = 1.5 + 0.4*cos(A/57.29577951)
            YV[II] = 0.5 + 0.4*sin(A/57.29577951)
         }
     
         for II=1,NV-1 {
            for JJ=II+1,NV {
               pgmove XV[II], YV[II]
               pgdraw XV[JJ], YV[JJ]
            }
         }
      }
      pgebuf
      pgunsa

// Cursor loop: user selects a box

      pgslw 2
      pgsfs 2
      XX = 0.5
      YY = 0.5
/*      for J=1,1000 {
         JUNK = pgcurs(XX, YY, CH)
         IF (ICHAR(CH).EQ.0) GOTO 50
C
C Find which box and highlight it
C
         DO 30 I=1,NBOX
            IF (BOX(1,I).LE.XX .AND. BOX(2,I).GE.XX .AND.
     :          BOX(3,I).LE.YY .AND. BOX(4,I).GE.YY) THEN
               CALL PGSCI(2)
               CALL PGSLW(2)
               CALL PGSCH(1.2)
               CALL PGRECT(BOX(1,I), BOX(2,I), BOX(3,I), BOX(4,I))
               CALL PGSLW(1)
               IF (I.EQ.5) THEN
C                 -- EXIT box
                  GOTO 50
               ELSE IF (I.EQ.4) THEN
C                 -- DRAW box
                  GOTO 5
               ELSE
C
C Read value
C   
                  IF (RESULT(I).EQ.' ') THEN
                     LSTR = 0
                  ELSE
                     DO 11 II=LEN(RESULT(I)),1,-1
                        LSTR = II
                        IF (RESULT(I)(II:II).NE.' ') GOTO 12
 11                  CONTINUE
                     LSTR = 0
 12                  CONTINUE
                  END IF
                  X = BOX(1,I) + 0.01
                  Y = BOX(3,I) + 0.01
                  CALL PGRSTR(X, Y, 0.0, 0.0, RESULT(I), LSTR, 1)
                  II = 1
                  IVAL(I) = CTOI(RESULT(I)(1:LSTR), II)
               END IF
               CALL PGSLW(2)
               CALL PGSCI(1)
               CALL PGRECT(BOX(1,I), BOX(2,I), BOX(3,I), BOX(4,I))
               CALL PGSLW(1)
            END IF 
 30      CONTINUE
 60   CONTINUE
C
C Close the device and exit.
C
 50   CONTINUE*/
      }
      pgclos

