      SUBROUTINE DEC240(X,Y,IND)
      CHARACTER*1 ESC, ASCII
      INTEGER PIXROW, PIXCOL, PIXEL, YMARGN
      COMMON/ OUTPUT/ IPAPER,IPAGE
      COMMON /DEVICE/ ITYPE, ISCRN, KOROFF
      COMMON /ASCIIC/ ASCII( 0: 255)
      COMMON /TERM/ IMAXR, IMAXC, PIXROW, PIXCOL, PIXEL, LCOUNT, INGRAF,
     .              IXL, IXR, IYT, IYB, NCOLOR, MCOLOR( 64 ), ITTRM,
     .              MARGX, MARGY
* VARIABLES IN COMMON TERM:
*  IMAXR = MAX NUMBER OF ROWS
*  IMAXC = MAX NUMBER OF COLUMNS
*  PIXROW = NUMBER OF PIXELS PER ROW
*  PIXCOL = NUMBER OF PIXELS PER COLUMN
*  PIXEL  = LESSER OF PIXROW OR PIXCOL
*  LCOUNT = NUMBER OF LINES CURRENTLY WRITTEN TO DIALOG AREA
*  INGRAF = 0=> HOST TEXT GOES TO MONITOR SPACE\\ 1=> TEXT TO GRAPHICS
*  IXL    = LEFT MOST VALUE OF X
*  IXR    = RIGHT MOST VALUE OF X
*  IYT    = VALUE OF Y AT TOP OF SCREEN
*  IYB    = VALUE OF Y AT BOTTOM OF SCREEN
*  NCOLOR = NUMBER OF COLORS IN MCOLOR MAP
*  MCOLOR = MAP OF COLORS
*  ITTRM  = UNIQUE MODEL NUMBER OF GENERAL TERMINAL TYPE
*  MARGX  = NUMBER OF PIXELS PADDED TO X-COORDINATE
*  MARGY  = NUMBER OF PIXELS PADDED TO Y-COORDINATE
*
*

      EQUIVALENCE ( ESC, ASCII(27) )
C
C   CODES FOR IND:
C
C     IND = 0   =>  REMOVE ALL GRAPHICS TO QUIT
C     IND = 1   =>  ANY ONE TIME ONLY INITIALIZATION
C     IND = 2   =>  MOVE GRAPHICS TO POINT (X,Y) -- DO NOT DRAW LINE
C     IND = 3   =>  DRAW LINE FROM LAST POINT TO NEW (X,Y)
C     IND = 4   =>  DRAW DARK LINE
C     IND = 5   =>  DRAW BRIGHT SOLID LINE
C     IND = 6   =>  INITIALIZE & SET-UP FOR NEXT PICTURE
C     IND = 8   =>  TEMPORARY RELEASE FROM GRAPHICS
C     IND = 9   =>  RETURN TO GRAPHICS FROM TEMP RELEASE
C     IND =10   =>  DONE DRAWING PICTURE
C     IND =99   =>  SET COLOR AS SPECIFIED IN X
C

*
*  DEFAULT COLORS          MY COLORS
*
*    MAP    COLOR         MAP     COLOR
*    0      DARK          0       DARK
*    1      BLUE          1       RED
*    2      RED           2       BLUE
*    3      GREEN         3       WHITE

      PIXROW = 479/23
      PIXCOL = 799/75.
      YMARGN = 6 * PIXROW
      PIXEL = ( 24. - 6. ) * PIXROW * IPAGE / 100.

      IF (IND.EQ.0) THEN
C  ALL DONE -- CLEAR TERMINAL AND WE WILL QUIT
         WRITE (*,*) ESC//'[2J'
         WRITE (*,*) ESC//'[1;24r'
         INGRAF = 0
      ELSEIF (IND.EQ.1) THEN
C  ONE TIME ONLY INITIALIZATION OF TERMINAL  (IF NEEDED)
         ISCRN = 28
         IXL = 0
         IXR = 799
         IYT = 0
         IYB = 499
         IMAXC = 80
         IMAXR = 19
         NCOLOR = 4
         MCOLOR( 1) = 1
         MCOLOR( 2) = 2
         MCOLOR( 3) = 3
         DO 10 I= 4, 64
  10     MCOLOR( I) = 1
*  ORIGIN AT TOP LEFT CORNER (0,0)
         WRITE (*,*) ESC//'[19;24r' //ESC//'[19;1H'
         WRITE ( *, *) ESC//'7'
         WRITE (*,*) ESC//'PpS(EI(D))W(I(W))'
         WRITE (*,*) 'S(A[0,0][799,499])'
         INGRAF = 1
         WRITE (*,*) 'S(M0(AD)1(AB)2(AR)3(AW))'
      ELSEIF (IND.EQ.2 ) THEN
*  MOVE TO POINT ( NO DRAW )
         IXLX = X * PIXEL
         IYLY = (1.0 - Y ) * PIXEL 
         CALL CLP240(IXLX, IYLY)
         IF (ABS(IXLX-IXLAST)+ABS(IYLY-IYLAST) .GT. 0 )
     .        WRITE ( *, '('' P['',I3,'','',I3,'']'')') IXLX, IYLY
         IXLAST = IXLX
         IYLAST = IYLY
      ELSEIF (IND.EQ.3) THEN
C DRAW LINE FROM LAST POINT TO NEW POINT
         IXLX = X * PIXEL
         IYLY = (1.0 - Y ) * PIXEL 
         CALL CLP240(IXLX, IYLY)
         IF (ABS(IXLX-IXLAST)+ABS(IYLY-IYLAST) .GT. 0 )
     .        WRITE ( *, '('' V['',I3,'','',I3,'']'')') IXLX, IYLY
         IXLAST = IXLX
         IYLAST = IYLY
      ELSEIF (IND.EQ.4) THEN
* DRAW DARK LINE ( ERASES PREVIOUS LINES )
*   SET FOR REGIS ERASE WRITING
         WRITE ( *, '('' W(I(D))'')')
      ELSEIF (IND.EQ.5) THEN
* DRAW BRIGHT (I.E. WHITE LINE)
         WRITE ( *, '('' W(I(W))'')')
      ELSEIF (IND.EQ.6) THEN
C  INITIALIZE FOR DRAWING NEXT PICTURE
         WRITE (*,*) ESC//'[19;24r'
         WRITE (*,*) ESC//'PpS(EI(D))W(I(W))S(C0)'
         WRITE (*,*) 'S(A[0,0][799,499])'
         INGRAF = 1
      ELSEIF (IND.EQ.8) THEN
C TEMPORARY RELEASE FROM GRAPHICS  (USUALLY FOR TEXT)
*  SEND STRING TERMINATOR (WE USE ESC \ )
*    THEN ANSI CURSOR JUNK TO ESTABLISH SCROLLING
         IF ( INGRAF .EQ. 1 )
     .       WRITE ( *, *) ESC//'\\'//ESC// '8'
c?     .       WRITE ( *, *) ESC//'\'//ESC// '[19;1H'//ESC//'[J'
         INGRAF = 0
      ELSEIF (IND.EQ.9) THEN
C RETURN TO GRAPHICS FROM TEMPORARY RELEASE
*  SEND DCS TO ENTER REGIS MODE (WE USE ESC P )
         IF (INGRAF.EQ.0) WRITE ( *, *) ESC//'7'//ESC//'Pp'
c?         IF (INGRAF.EQ.0) WRITE ( *, *) ESC//'Pp'
         INGRAF = 1
      ELSEIF (IND.EQ.10) THEN
C DONE DRAWING PICTURE
         WRITE ( *, *) ESC//'\\'
         INGRAF = 0
      ELSEIF (IND.EQ.99) THEN
C REQUEST FOR NEW COLOR
         IF( INT(X) .EQ. 1) THEN
           WRITE(*,*) 'W(I(W))'
         ELSEIF( INT(X) .EQ. 2) THEN
           WRITE(*,*) 'W(I(R))'
         ELSEIF( INT(X) .EQ. 3) THEN
C?           WRITE(*,*) 'W(I(G))'
C?         ELSEIF( INT(X) .EQ. 4) THEN
           WRITE(*,*) 'W(I(B))'
         ELSE
           WRITE(*,*) 'W(I(W))'
         ENDIF
      ELSE
C WE HAVE AN ERROR IN IND
         WRITE ( *, *) ' ERROR IN PLOT, IND=',IND
      ENDIF
      RETURN
      END

      SUBROUTINE CLP240( INX, INY )
*
      INTEGER PIXROW, PIXCOL, PIXEL
      COMMON /TERM/ IMAXR, IMAXC, PIXROW, PIXCOL, PIXEL, LCOUNT, INGRAF,
     .              IXL, IXR, IYT, IYB, NCOLOR, MCOLOR( 64 ), ITTRM,
     .              MARGX, MARGY
* VARIABLES IN COMMON TERM:
*  IMAXR = MAX NUMBER OF ROWS
*  IMAXC = MAX NUMBER OF COLUMNS
*  PIXROW = NUMBER OF PIXELS PER ROW
*  PIXCOL = NUMBER OF PIXELS PER COLUMN
*  PIXEL  = LESSER OF PIXROW OR PIXCOL
*  LCOUNT = NUMBER OF LINES CURRENTLY WRITTEN TO DIALOG AREA
*  INGRAF = 0=> HOST TEXT GOES TO MONITOR SPACE\\ 1=> TEXT TO GRAPHICS
*  IXL    = LEFT MOST VALUE OF X
*  IXR    = RIGHT MOST VALUE OF X
*  IYT    = VALUE OF Y AT TOP OF SCREEN
*  IYB    = VALUE OF Y AT BOTTOM OF SCREEN
*  NCOLOR = NUMBER OF COLORS IN MCOLOR MAP
*  MCOLOR = MAP OF COLORS
*  ITTRM  = UNIQUE MODEL NUMBER OF GENERAL TERMINAL TYPE
*  MARGX  = NUMBER OF PIXELS PADDED TO X-COORDINATE
*  MARGY  = NUMBER OF PIXELS PADDED TO Y-COORDINATE
*
*

      IF ( INX .LT. IXL ) THEN
         INX = IXL
      ELSEIF ( INX .GT. IXR ) THEN
         INX = IXR
      ENDIF
     
      IF ( INY .LT. IYT ) THEN
         INY = IYT
      ELSEIF ( INY .GT. IYB ) THEN
         INY = IYB
      ENDIF

      RETURN
      END

      SUBROUTINE DECSTR(IROW, ICOL, STRING)
*
*  TO PLACE TEXT SRING IN GRAPHIC AREA
*    THE STRING STARTS AT LOCATION (ROW, COL)
* RESPONSIBILITY FOR WRAPPING, ETC IS UP TO THE USER
*
      CHARACTER*(80) STRING
      CHARACTER*1 CCHAR, DELIM
      INTEGER PIXROW, PIXCOL, PIXEL
      COMMON /TERM/ IMAXR, IMAXC, PIXROW, PIXCOL, PIXEL, LCOUNT, INGRAF,
     .              IXL, IXR, IYT, IYB, NCOLOR, MCOLOR( 64 ), ITTRM,
     .              MARGX, MARGY
* VARIABLES IN COMMON TERM:
*  IMAXR = MAX NUMBER OF ROWS
*  IMAXC = MAX NUMBER OF COLUMNS
*  PIXROW = NUMBER OF PIXELS PER ROW
*  PIXCOL = NUMBER OF PIXELS PER COLUMN
*  PIXEL  = LESSER OF PIXROW OR PIXCOL
*  LCOUNT = NUMBER OF LINES CURRENTLY WRITTEN TO DIALOG AREA
*  INGRAF = 0=> HOST TEXT GOES TO MONITOR SPACE\\ 1=> TEXT TO GRAPHICS
*  IXL    = LEFT MOST VALUE OF X
*  IXR    = RIGHT MOST VALUE OF X
*  IYT    = VALUE OF Y AT TOP OF SCREEN
*  IYB    = VALUE OF Y AT BOTTOM OF SCREEN
*  NCOLOR = NUMBER OF COLORS IN MCOLOR MAP
*  MCOLOR = MAP OF COLORS
*  ITTRM  = UNIQUE MODEL NUMBER OF GENERAL TERMINAL TYPE
*  MARGX  = NUMBER OF PIXELS PADDED TO X-COORDINATE
*  MARGY  = NUMBER OF PIXELS PADDED TO Y-COORDINATE
*
*
      IF ( INGRAF .EQ. 0 ) THEN
         WRITE (*,*) 'GTEXT called when INGRAF=0.'
         RETURN
      ENDIF
* FIRST WE MAKE A COPY OF THE ROW AND COLUMN FOR CLIPPING
      LROW = IROW
      LCOL = ICOL
*  NEXT WE CHECK FOR THE USER USING OUR DELIMITER CHARACTER
      IF ( INDEX( STRING, '"') .GT. 0 ) THEN
*         A MATCH, CHECK THE FIRST ALTERNATE
         IF ( INDEX( STRING, '''') .EQ. 0 ) THEN
            DELIM = ''''
         ENDIF
      ELSE
         DELIM = '"'
      ENDIF
*
      IF ( LCOL+LROW .EQ. 0 ) THEN
         WRITE (*,'(1X,''T'',A)') DELIM//STRING//DELIM
      ELSE
         IF ( LCOL .GT. IMAXC) LCOL = IMAXC
         IF ( LROW .GT. IMAXR) LROW = IMAXR
         WRITE ( *, '('' P['',I3,'','',I3,'']'' )') 
     .      (LCOL-1)*PIXCOL+1, (LROW-1)*PIXROW
         WRITE (*,'(1X,''T'',A)') DELIM//STRING//DELIM
      ENDIF
      DELIM = '"'
      RETURN
      END
