c************
c  Description:	Library for XTX X-Windows textual display utilities
c
c  Author:	JG Kralles
c
c  $Revision: 1.3 $
c  $Modtime:   29 Oct 1997 22:27:04  $
c
c************
C-----------------------------------------------------------------------
      SUBROUTINE XTX$INIT_ROW (FONT,XW,XH,XL,CMAP,
     +                         BARTEXT,PSFILE,KEYMENU)
      IMPLICIT NONE

      INTEGER*4 FONT
      INTEGER*4 XW
      INTEGER*4 XH
      INTEGER*4 XL
      INTEGER*4 CMAP
      CHARACTER*(*) BARTEXT
      CHARACTER*(*) PSFILE
      CHARACTER*(*) KEYMENU

      include 'xtxinclude.inc'

      CALL XTX$INIT (FONT,XW,XH,XL,CMAP,BARTEXT,PSFILE,KEYMENU)
      XTX_ROW     = .TRUE.
      XTX_SCROLL  = .FALSE.
      XTX_REWRITE = .FALSE.


      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      SUBROUTINE XTX$INIT_SCROLL (FONT,XW,XH,XL,CMAP,
     +                            BARTEXT,PSFILE,KEYMENU)
      IMPLICIT NONE

      INTEGER*4 FONT
      INTEGER*4 XW
      INTEGER*4 XH
      INTEGER*4 XL
      INTEGER*4 CMAP
      CHARACTER*(*) BARTEXT
      CHARACTER*(*) PSFILE
      CHARACTER*(*) KEYMENU

      include 'xtxinclude.inc'

      CALL XTX$INIT (FONT,XW,XH,XL,CMAP,BARTEXT,PSFILE,KEYMENU)
      XTX_ROW     = .FALSE.
      XTX_SCROLL  = .TRUE.
      XTX_REWRITE = .FALSE.


      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      SUBROUTINE XTX$INIT_REWRITE (FONT,XW,XH,XL,CMAP,
     +                             BARTEXT,PSFILE,KEYMENU)
      IMPLICIT NONE

      INTEGER*4 FONT
      INTEGER*4 XW
      INTEGER*4 XH
      INTEGER*4 XL
      INTEGER*4 CMAP
      CHARACTER*(*) BARTEXT
      CHARACTER*(*) PSFILE
      CHARACTER*(*) KEYMENU

      include 'xtxinclude.inc'

      CALL XTX$INIT (FONT,XW,XH,XL,CMAP,BARTEXT,PSFILE,KEYMENU)
      XTX_ROW     = .FALSE.
      XTX_SCROLL  = .FALSE.
      XTX_REWRITE = .TRUE.


      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      SUBROUTINE XTX$INIT (FONT,XW,XH,XL,CMAP,BARTEXT,PSFILE,KEYMENU)
      IMPLICIT NONE

      INTEGER*4 FONT
      INTEGER*4 XW
      INTEGER*4 XH
      INTEGER*4 XL
      INTEGER*4 CMAP                      ! 0 means use defualt or switch
      CHARACTER*(*) BARTEXT
      CHARACTER*(*) PSFILE
      CHARACTER*(*) KEYMENU

      include 'primitive.inc'
      include 'qmessages.inc'
      include 'xtxinclude.inc'

      INTEGER*4 I
      INTEGER*4 W,H


      IF (BARTEXT .EQ. ' ') THEN
         BARTEXT = 'Text'
      ENDIF
      IF (PSFILE .EQ. ' ') THEN
         PSFILE  = 'POSTSCRIPT.PS'
      ENDIF
      XTX_PSFILE  = PSFILE

      IF (KEYMENU .NE. ' ') THEN
         XTX_KEYMENU = KEYMENU
      ELSE
         XTX_KEYMENU = 'Options'
      ENDIF

      XTX_HSIZE = 0
      XTX_NOEXIT  = M$GET_PSWITCH ('NOEXIT')
      XTX_XI      = M$GET_PSWITCH ('XI')
      XTX_TAG     = M$GET_PSWITCH ('TIMETAG')
      XTX_ROW     = M$GET_PSWITCH ('ROW')
      XTX_SCROLL  = M$GET_PSWITCH ('SCROLL')
      XTX_REWRITE = M$GET_PSWITCH ('REWRITE')
      XTX_XCNT = M$GET_SWITCH ('XCNT')
      IF ((XTX_XCNT .LT. 0) .OR. (XTX_XCNT .GT. 2)) THEN
         IF (M$GET_PSWITCH('MTAG')) THEN
            XTX_XCNT = 1
         ELSE
            XTX_XCNT = 0
         ENDIF
      ENDIF

      XTX_MASK   = MAX (0,M$GET_SWITCH ('MASK'))
      XTX_MSGID  = MAX (0,M$GET_SWITCH ('MSGID'))

      XTX_DBC_ROW = 0
      XTX_DBC_SELECT_ROW = 0
      XTX_DBC_COL = 0
      XTX_DBC_SELECT_COL = 0
      XTX_DBC_TIME = SECNDS (0.0) - XTX_P_DBC_DELTA - 1.0

      XTX_XCMAX = M$GET_SWITCH ('XCMAX')
      IF (XTX_XCMAX .LT. 0) THEN
         IF (CMAP .EQ. 0) THEN
            XTX_XCMAX = XTX_P_DEF_COLORS
         ELSEIF (CMAP .LT. 0) THEN
            XTX_XCMAX = 0
         ELSE
            XTX_XCMAX = CMAP
         ENDIF
      ENDIF
      IF (XTX_XCMAX .GT. XTX_P_MAX_COLORS) THEN
         CALL M$WARNING ('Invalid Colormap size, using maximum')
      ENDIF

      XTX_MAX_HEIGHT = M$GET_SWITCH  ('XLINES')
      IF (XTX_MAX_HEIGHT .GT. 0) THEN
C        Use switch for scroll depth
      ELSEIF (XL .LE. 0) THEN
         XTX_MAX_HEIGHT = XTX_P_DEF_HEIGHT
         IF (XL .LT. 0) THEN
            CALL M$WARNING ('Invalid scroll size, setting to default')
         ENDIF
      ELSE
         XTX_MAX_HEIGHT = XL
      ENDIF
      CALL M$MALLOC (XTX_MAX_HEIGHT*XTX_P_BUF_SZ,XTX_BUF_PTR)

      XTX_DISABLE_SCROLLBAR = .FALSE.

      IF     (XTX_ROW .AND. XTX_SCROLL) THEN
         CALL M$ERROR ('/SCROLL and /ROW are mutually exclusive')
      ELSEIF (XTX_ROW .AND. XTX_REWRITE) THEN
         CALL M$ERROR ('/REWRITE and /ROW are mutually exclusive')
      ELSEIF (XTX_SCROLL .AND. XTX_REWRITE) THEN
         CALL M$ERROR ('/REWRITE and /SCROLL are mutually exclusive')
      ELSEIF ((.NOT. XTX_ROW) .AND. (.NOT. XTX_SCROLL)) THEN
         XTX_REWRITE = .TRUE.
      ENDIF

      I = M$LENGTH(XTX_KEYMENU)
      CALL XTX$SET_MENU (XTX_KEYMENU(1:I)//';')

      CALL MX$GRAPHICSINIT ()

      W = 1
      H = 1
      CALL MX$SETUP (W,H)

      IF (FONT .NE. 0 ) THEN
         CALL MX$SETFONT (FONT)
      ENDIF

      IF (MX.WIDTH .EQ. W) THEN
         IF (XW .EQ. 0) THEN
            MX.WIDTH = 300
         ELSEIF (XW .LT. 0) THEN
            MX.WIDTH = (-XW+2) * MX.TEXT_W
         ELSE
            MX.WIDTH = XW
         ENDIF
      ENDIF

      IF (MX.HEIGHT .EQ. H) THEN
         IF (XH .EQ. 0) THEN
            MX.HEIGHT = 100
         ELSEIF (XH .LT. 0) THEN
            MX.HEIGHT = (-XH) * MX.TEXT_H
         ELSE
            MX.HEIGHT = XH
         ENDIF
      ENDIF

      MX.TEXT_SIZE_LOCK = 0
      CALL MX$OPEN (BARTEXT)

      MX.ORIGIN = 4

      CALL MX$COLORMAP (MC.COLORMAP(4),XTX_XCMAX)

      MQD.COLOR.POSITION = XTX_P_INFO
      MQD.COLOR.RED      = XTX_P_INFO_R
      MQD.COLOR.GREEN    = XTX_P_INFO_G
      MQD.COLOR.BLUE     = XTX_P_INFO_B
      CALL MX$COLORTAB (XTX_P_INFO,MQD.COLOR,-2)
      MQD.COLOR.POSITION = XTX_P_WARN
      MQD.COLOR.RED      = XTX_P_WARN_R
      MQD.COLOR.GREEN    = XTX_P_WARN_G
      MQD.COLOR.BLUE     = XTX_P_WARN_B
      CALL MX$COLORTAB (XTX_P_WARN,MQD.COLOR,-2)
      MQD.COLOR.POSITION = XTX_P_ERROR
      MQD.COLOR.RED      = XTX_P_ERROR_R
      MQD.COLOR.GREEN    = XTX_P_ERROR_G
      MQD.COLOR.BLUE     = XTX_P_ERROR_B
      CALL MX$COLORTAB (XTX_P_ERROR,MQD.COLOR,-2)


      XTX_TOP        = MAX (0,M$GET_SWITCH ('TOP'))

      XTX_FIRST_LINE = 1
      XTX_FIRST_CHAR = 1
      XTX_W_NEXT     = 0
      XTX_TOTAL      = 0
      XTX_H_TOTAL    = 0
      XTX_SCROLLBAR  = .FALSE.
      CALL XTX$SET_WINDOW_PARMS ()

      DO I = 1 , XTX_MAX_HEIGHT
         XTX_BUF(I).C = XTX_P_DEF_COLOR
         XTX_BUF(I).T = ' '
      ENDDO


      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      SUBROUTINE XTX$SETBAR (TEXT)
      IMPLICIT NONE

      CHARACTER*(*) TEXT

      include 'primitive.inc'

      INTEGER*4 LEN


      LEN = M$LENGTH (TEXT)
      IF (LEN .GT. 0) THEN
         CALL MX$SETBAR (TEXT)
      ELSE
         CALL MX$SETBAR (' ')
      ENDIF


      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      SUBROUTINE XTX$SET_MENU (TEXT)
      IMPLICIT NONE

      CHARACTER*(*) TEXT

      include 'primitive.inc'
      include 'xtxinclude.inc'

      INTEGER*4     I,J,K


      XTX_MENU = XTX_P_DEF_MENU
      XTX_MENU_LEN = M$LENGTH (XTX_MENU)
      J = 0
      DO I = 1 , XTX_MENU_LEN
         IF (XTX_MENU(I:I) .EQ. ',') THEN
            J = J + 1
         ENDIF
      ENDDO
      XTX_MENU = TEXT//XTX_P_DEF_MENU
      XTX_MENU_LEN = M$LENGTH (XTX_MENU)
      K = 0
      DO I = 1 , XTX_MENU_LEN
         IF (XTX_MENU(I:I) .EQ. ',') THEN
            K = K + 1
         ENDIF
      ENDDO
      XTX_MENU_FIRST = K - J + 1


      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      SUBROUTINE XTX$TERM ()
      IMPLICIT NONE

      include 'xtxinclude.inc'

      CALL MX$CLOSE ()

      CALL M$FREE (XTX_BUF_PTR)


      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      FUNCTION XTX$MESSAGE (MQH,MQ)
      IMPLICIT NONE

      include 'primitive.inc'
      include 'qmessages.inc'
      include 'xtxinclude.inc'

      integer*4 L_BBUF			! jdh added
      PARAMETER (L_BBUF = 65535)	! jdh modified
      INCLUDE 'BUFFERS.INC'

      STRUCTURE /MQ/
         UNION
            MAP
               RECORD /BUFFER/  DATA
            END MAP
            MAP
               RECORD /MQ_DATA/ MQD
            END MAP
         END UNION
      END STRUCTURE
      RECORD /MQ/ MQ

      LOGICAL*4    MESSAGE
      INTEGER*4    MSINFO,MSNDATA
      CHARACTER*8  MSNAME
      CHARACTER*86 MSTYPE

      INTEGER*4    I,COLOR,ROW,EINDEX,ELENGTH,ENELEM,SIZE,START
      CHARACTER*1  ETYPE
      INTEGER*4    RPOS_ROW,RPOS_POS
      CHARACTER*(XTX_P_BBUF) TEXT

      XTX_CLEAR    = .FALSE.
      XTX_EXIT     = .FALSE.
      XTX$MESSAGE = XTX_P_NO_MSG

      MESSAGE = MX$GRAPHICS_MSG (XTX_MX,MQH,MQ,XTX_MSGID,XTX_MASK)

      IF (MESSAGE .EQ. 0) THEN

         XTX$MESSAGE = XTX_P_MSG_PROC

         MSNAME  = MQH.NAME
         MSINFO  = MQH.INFO
         MSNDATA = (MQH.NSLOTS - MQH.NHSLOTS) * BYTES_PER_SLOT
         IF (MQH.TYPE .EQ. 'H') THEN
            MSTYPE  = MQH.TYPES
         ELSE
            MSTYPE  = MQH.TYPE
         ENDIF

         IF (MSINFO .EQ. 0) THEN
            COLOR = XTX_P_DEF_COLOR
         ELSE
            COLOR = MSINFO
         ENDIF
         ROW = 1

         CALL M$UPPERCASE (MSNAME)

         IF (MSNAME .EQ. MQH_XTEXTA.NAME) THEN
            XTX_APPEND = .TRUE.
         ELSE
            XTX_APPEND = .FALSE.
         ENDIF

         IF (MSNAME .EQ. 'TEXT') THEN
C           Compatiblity with earlier versions
            MSNAME = MQH_XTEXT.NAME
         ENDIF

         IF (MSNAME .EQ. MQH_XTEXTHDR.NAME) THEN

            XTX_HCOLOR = COLOR
            CALL XTX$CLEAR_WINDOW ()
            IF (MSNDATA .EQ. 0) THEN
               SIZE = 1
               TEXT = ' '
               CALL XTX$WRITE_HEAD (XTX_P_DEF_COLOR,TEXT(1:SIZE))
            ELSE
               ELENGTH = M$LENGTH (MSTYPE)
               EINDEX = 1
               CALL M$EXPAND_HTYPE (MSTYPE,EINDEX,ELENGTH,ENELEM,
     +                              ETYPE,SIZE)
               IF (ETYPE .EQ. 'S') THEN
                  CALL XTX$WRITE_HEAD (XTX_P_DEF_COLOR,
     +                                 MQ.DATA.SBUF(1:SIZE))
               ELSEIF (ETYPE .EQ. 'A') THEN
                  SIZE = M$LENGTH(MQ.DATA.SBUF(1:MSNDATA))
                  CALL XTX$WRITE_HEAD (XTX_P_DEF_COLOR,
     +                                 MQ.DATA.SBUF(1:SIZE))
               ELSE
                  GOTO 9998
               ENDIF
            ENDIF
            CALL XTX$REFRESH ()

         ELSEIF (MSNAME(1:5) .EQ. MQH_XTEXT.NAME(1:5)) THEN

            IF (MSNAME .EQ. MQH_XTEXTCLR.NAME) THEN
               CALL XTX$CLEAR_WINDOW ()
               IF (XTX_HSIZE .GT. 0) THEN
                  CALL XTX$REWRITE_HEAD ()
               ENDIF
               DO I = 1 , XTX_TOTAL
                  XTX_BUF(I).C = XTX_P_DEF_COLOR
                  XTX_BUF(I).T = ' '
               ENDDO
               XTX_W_NEXT     = 0
               XTX_TOTAL      = 0
               XTX_FIRST_LINE = 1
               XTX_LAST_LINE  = XTX_FIRST_LINE + XTX_DISP_LINES - 1
               XTX_LAST_CHAR  = XTX_FIRST_CHAR + XTX_W_WIDTH - 1
               EINDEX = 1
               START  = 1

            ELSEIF (MSNAME .EQ. MQH_XTEXTROW.NAME) THEN
               EINDEX = 3
               START  = 9
               ROW = MQ.DATA.LBUF(1)
               IF (ROW .LT. 1) THEN
                  ROW = 1
                  CALL M$WARNING ('Invalid ROW, using first row')
               ELSEIF (ROW .GT. XTX_MAX_HEIGHT) THEN
                  ROW = XTX_MAX_HEIGHT
                  CALL M$WARNING ('Invalid ROW, using last row')
               ENDIF

            ELSE             ! 'XTEXT' , 'XTEXTA'
               EINDEX = 1
               START  = 1
            ENDIF

            IF (MSNDATA .EQ. 0) THEN
               SIZE = 1
               TEXT(1:SIZE) = ' '
               CALL XTX$WRITE_TEXT (ROW,COLOR,TEXT(1:SIZE))
            ELSE
               ELENGTH = M$LENGTH (MSTYPE)
               CALL M$EXPAND_HTYPE (MSTYPE,EINDEX,ELENGTH,ENELEM,
     +                              ETYPE,SIZE)
               IF (ETYPE .EQ. 'S') THEN
                  TEXT(1:SIZE) = MQ.DATA.SBUF(START:START+SIZE-1)
                  CALL XTX$WRITE_TEXT (ROW,COLOR,TEXT(1:SIZE))
               ELSEIF (ETYPE .EQ. 'A') THEN
                  SIZE = MSNDATA-START+1
                  TEXT(1:SIZE) = MQ.DATA.SBUF(START:START+SIZE-1)
                  SIZE = M$LENGTH(TEXT(1:SIZE))
                  CALL XTX$WRITE_TEXT (ROW,COLOR,TEXT(1:SIZE))
               ELSE
                  GOTO 9998
               ENDIF
            ENDIF

         ELSEIF (MSNAME .EQ. MQH_XTXTRPOS.NAME) THEN
C
C           Row Position
C
            RPOS_ROW = MAX (1,MQ.MQD.XTXTRPOS.ROW)
            RPOS_POS = MQ.MQD.XTXTRPOS.POSITION
            CALL XTX$POSITION_ROW (RPOS_POS,RPOS_ROW)

         ELSEIF (MSNAME .EQ. MQH_REFRESH.NAME) THEN
C
C           Refresh
C
            CALL XTX$CLEAR_WINDOW ()
            CALL XTX$REFRESH ()

         ELSEIF (MSNAME .EQ. MQH_XTCLEAR.NAME) THEN
C
C           Clear
C
            CALL XTX$CLEAR ()

         ELSEIF (MSNAME .EQ. MQH_COLOR.NAME) THEN
C
C           Colormap
C
            IF ((MSINFO .GE. 1) .AND. (MSINFO .LE. XTX_XCMAX)) THEN
               CALL XTX$COLOR (MSINFO,MQ.MQD.COLOR.RED,
     +                                MQ.MQD.COLOR.GREEN,
     +                                MQ.MQD.COLOR.BLUE)
            ENDIF

         ELSEIF (MSNAME .EQ. MQH_HARDCOPY.NAME) THEN
C
C           Hardcopy
C
            IF (MX$HARDCOPY_OPEN(MQD.HARDCOPY.FILE, MSINFO))THEN
               CALL XTX$REFRESH ()
               CALL MX$HARDCOPY_CLOSE ()
            ENDIF

         ELSEIF (MSNAME .EQ. MQH_EXIT.NAME) THEN

            XTX_EXIT    = .TRUE.
            XTX$MESSAGE = XTX_P_MSG_EXIT

         ELSE

            XTX$MESSAGE = XTX_P_MSG_NOT_PROC

         ENDIF

      ENDIF

      GOTO 9999
9998  CONTINUE
      TEXT = 'Invalid '//MSNAME//' text message received'
      SIZE = M$LENGTH (TEXT)
      CALL XTX$WRITE_TEXT (1,0,TEXT(1:SIZE))
9999  CONTINUE
      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      FUNCTION XTX$MX_EVENTS ()
      IMPLICIT NONE

      include 'primitive.inc'
      include 'qmessages.inc'
      include 'xtxinclude.inc'

      RECORD  /MQD_COLORINFO/ COLORMAP(XTX_P_MAX_COLORS)

      INTEGER*4 STATUS
      INTEGER*4 ITEM
      INTEGER*4 OLD
      INTEGER*4 CHOICE
      INTEGER*4 ROW
      INTEGER*4 COL
      REAL*4    DELTA
      INTEGER*4 COUNT
      REAL*4    PAUSE_TIME


      XTX$MX_EVENTS = 0
      XTX_HOT_KEY   = ' '

      XTX_RESIZE_EXPOSE = .FALSE.
      XTX_CLEAR    = .FALSE.
      XTX_FONT     = .FALSE.
      XTX_COLORMAP = .FALSE.
      XTX_EXIT     = .FALSE.
      XTX_DBC_ROW  = 0
      XTX_DBC_COL  = 0

      IF (MX.MOTION_NOTIFY) THEN

         IF (XTX_XCNT .EQ. 2) THEN
            ROW = ((MX.YPOS + MX.TEXT_H - 1) / MX.TEXT_H) +
     +                                      XTX_FIRST_LINE - 1 - XTX_TOP
            IF ((ROW .GE. XTX_FIRST_LINE) .AND.
     +          (ROW .LE. XTX_LAST_LINE)) THEN
               COL = (MX.XPOS + MX.TEXT_W - 1) / MX.TEXT_W
               IF ((COL .GE. 1) .AND. (COL .LE. XTX_W_WIDTH)) THEN
                  CALL M$DRSLT (MX.XTAG,DBLE(COL))
                  CALL M$DRSLT (MX.YTAG,DBLE(ROW))
                  CALL M$DRSLT (MX.ZTAG,DBLE(XTX_BUF(ROW).C))
               ENDIF
            ENDIF
         ENDIF

      ELSEIF (MX.BUTTON_PRESS .EQ. 1) THEN

         IF ((MX.YPOS .GE. XTX_TP) .AND.
     +       (MX.YPOS .LE. XTX_BP) .AND.
     +       (MX.XPOS .GE. XTX_LP) .AND.
     +       (MX.XPOS .LE. XTX_RP)) THEN
            COUNT = 0
            PAUSE_TIME = 0.1
            DO WHILE (MX.BUTTON_RELEASE .NE. 1)
               MX.BUTTON_PRESS = 1  ! allows scrolling until button release
               STATUS = XTX$DRAW_SCROLLBAR (-3)
               IF (STATUS .GT. 0) THEN
                  CALL XTX$REFRESH ()
               ENDIF
               COUNT = COUNT + 1
               IF (COUNT .EQ. 5) THEN
                  PAUSE_TIME = 0.025
               ELSEIF (COUNT .EQ. 15) THEN
                  PAUSE_TIME = 0.01
               ENDIF
               CALL M$PAUSE (PAUSE_TIME)
               IF (MX$IFEVENT()) CONTINUE
            ENDDO

         ELSEIF ((MX.YPOS .GE. XTX_H_TP) .AND.
     +           (MX.YPOS .LE. XTX_H_BP) .AND.
     +           (MX.XPOS .GE. XTX_H_LP) .AND.
     +           (MX.XPOS .LE. XTX_H_RP)) THEN
            IF (XTX_ROW) THEN
              COUNT = 0
              PAUSE_TIME = 0.1
              DO WHILE (MX.BUTTON_RELEASE .NE. 1)
                 MX.BUTTON_PRESS = 1  ! allows scrolling until button release
                 STATUS = XTX$DRAW_H_SCROLLBAR (-3)
                 IF (STATUS .GT. 0) THEN
                    CALL XTX$REFRESH ()
                 ENDIF
                 COUNT = COUNT + 1
                 IF (COUNT .EQ. 5) THEN
                    PAUSE_TIME = 0.025
                 ELSEIF (COUNT .EQ. 15) THEN
                    PAUSE_TIME = 0.01
                 ENDIF
                 CALL M$PAUSE (PAUSE_TIME)
                 IF (MX$IFEVENT()) CONTINUE
              ENDDO
            ENDIF

         ELSE

            IF (XTX_XCNT .EQ. 1) THEN
               ROW = ((MX.YPOS + MX.TEXT_H - 1) / MX.TEXT_H) +
     +                                      XTX_FIRST_LINE - 1 - XTX_TOP
               IF ((ROW .GE. XTX_FIRST_LINE) .AND.
     +             (ROW .LE. XTX_LAST_LINE)) THEN
                  COL = (MX.XPOS + MX.TEXT_W - 1) / MX.TEXT_W
                  IF ((COL .GE. 1) .AND. (COL .LE. XTX_W_WIDTH)) THEN
                     CALL M$DRSLT (MX.XTAG,DBLE(COL))
                     CALL M$DRSLT (MX.YTAG,DBLE(ROW))
                     CALL M$DRSLT (MX.ZTAG,DBLE(XTX_BUF(ROW).C))
                  ENDIF
               ENDIF
            ENDIF

            IF (XTX_ROW) THEN
               DELTA = SECNDS (XTX_DBC_TIME)
               IF (DELTA .LE. XTX_P_DBC_DELTA) THEN

                  ROW = (MX.YPOS + MX.TEXT_H - 1) / MX.TEXT_H +
     +                                      XTX_FIRST_LINE - 1 - XTX_TOP
                  IF (ROW .GT. 0) THEN
                     XTX_DBC_ROW = ROW
                     XTX_DBC_SELECT_ROW = XTX_DBC_ROW
                     XTX_DBC_COL = (MX.XPOS + MX.TEXT_W - 1) / MX.TEXT_W
                     XTX_DBC_SELECT_COL = XTX_DBC_COL
                  ENDIF
                  XTX_DBC_TIME = SECNDS (0.0) - XTX_P_DBC_DELTA - 
     +                                                1.0
               ELSE
                  XTX_DBC_TIME = SECNDS (0.0)
               ENDIF
            ENDIF

         ENDIF

      ELSEIF (MX.EXPOSURE .OR. MX.RESIZE) THEN

         XTX_RESIZE_EXPOSE = .TRUE.
         IF (MX.RESIZE) THEN
            CALL XTX$SET_WINDOW_PARMS ()
            CALL XTX$CLEAR_WINDOW ()
         ENDIF
         CALL XTX$REFRESH ()

      ELSEIF ((MX.BUTTON_PRESS .EQ. 2)    .OR.
     +        (MX.KEY_UPCHAR   .EQ. 'M')) THEN

         ITEM = MX$MENU (0,XTX_MENU(1:XTX_MENU_LEN))
         XTX$MX_EVENTS = ITEM

         IF (ITEM .EQ. XTX_MENU_FIRST) THEN
C
C           Refresh
C
            CALL XTX$CLEAR_WINDOW ()
            CALL XTX$REFRESH ()

         ELSEIF (ITEM .EQ. XTX_MENU_FIRST+1) THEN
C
C           Clear
C
            CALL XTX$CLEAR ()

         ELSEIF (ITEM .EQ. XTX_MENU_FIRST+2) THEN
C
C           Hardcopy
C
            IF (MX$HARDCOPY_OPEN (XTX_PSFILE,-1)) THEN
               CALL XTX$REFRESH ()
               CALL MX$HARDCOPY_CLOSE ()
            ENDIF

         ELSEIF (ITEM .EQ. XTX_MENU_FIRST+3) THEN

            ITEM = MX$MENU (XTX_XCNT+1, 'Controls Options;'//
     +                                  'Off,'//
     +                                  'LM Click,'//
     +                                  'Continuous,'//
     +                                  'Assign...') - 1
            IF (ITEM .EQ. 3) THEN
               CALL MX$PROMPT ('Mouse X location -> ', MX.XTAG)
               CALL M$UPPERCASE (MX.XTAG)
               CALL MX$PROMPT ('Mouse Y location -> ', MX.YTAG)
               CALL M$UPPERCASE (MX.YTAG)
               CALL MX$PROMPT ('Mouse Z location -> ', MX.ZTAG)
               CALL M$UPPERCASE (MX.ZTAG)
            ELSE
               XTX_XCNT = ITEM
            ENDIF

         ELSEIF (ITEM .EQ. XTX_MENU_FIRST+4) THEN
C
C           Fontsize
C
            OLD    = MX.TEXT_W
            CHOICE = OLD
            CALL MX$LPROMPT ('Font Width: ',CHOICE)
            IF (CHOICE .NE. OLD) THEN
               XTX_FONT = .TRUE.
               CALL MX$SETFONT (CHOICE)
               CALL XTX$SET_WINDOW_PARMS ()
               CALL XTX$CLEAR_WINDOW ()
               CALL XTX$REFRESH ()
            ENDIF

         ELSEIF (ITEM .EQ. XTX_MENU_FIRST+5) THEN
C
C           Colormap
C
            XTX_COLORMAP = .TRUE.
            CALL MX$COLORTAB (0,COLORMAP,-3)
            CALL MX$COLORTAB (1,MC.COLORMAP(4),-1)

         ELSEIF (ITEM .EQ. XTX_MENU_FIRST+6) THEN
C
C           Keypress Information
C
            CALL MX$MHELP (XTX_KEYMENU,'Keypress Info')

         ELSEIF (ITEM .EQ. XTX_MENU_FIRST+7) THEN
C
C           Exit
C
            IF (XTX_NOEXIT) THEN
               CALL MX$MESSAGE ('EXIT disabled',2.0)
            ELSE
               XTX_EXIT = .TRUE.
            ENDIF

         ENDIF

      ELSEIF (MX.BUTTON_PRESS .EQ. 3) THEN
         XTX_DBC_SELECT_ROW = 0
         XTX_DBC_SELECT_COL = 0

      ELSEIF (MX.KEY_UPCHAR .EQ. '?') THEN
         CALL MX$MHELP (XTX_KEYMENU,'Primitive')

      ELSEIF ((MX.KEY_UPCHAR .GE. ' ') .AND.
     +        (MX.KEY_UPCHAR .LE. '~')) THEN
         XTX_HOT_KEY = MX.KEY_UPCHAR

         IF (XTX_HOT_KEY .EQ. 'H') THEN
            XTX_HOT_KEY = ' '
            IF (MX$HARDCOPY_OPEN (XTX_PSFILE,-1)) THEN
               CALL XTX$REFRESH ()
               CALL MX$HARDCOPY_CLOSE ()
            ENDIF
         ENDIF

      ENDIF


      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      SUBROUTINE XTX$SET_WINDOW_PARMS ()
      IMPLICIT NONE
      include 'xtxinclude.inc'

      XTX_W_WIDTH    = MIN (XTX_P_MAX_WIDTH,MAX(2,MX.WIDTH/MX.TEXT_W))
      XTX_W_WIDTH    = MAX (2,XTX_W_WIDTH-2)
      XTX_E_WIDTH    = MIN (XTX_W_WIDTH+1,XTX_P_MAX_WIDTH)
      XTX_W_HEIGHT   = MIN (XTX_MAX_HEIGHT,MX.HEIGHT/MX.TEXT_H)
      XTX_LP         = MX.WIDTH-NINT(REAL(MX.TEXT_W)*1.5)
      XTX_RP         = MX.WIDTH-NINT(REAL(MX.TEXT_W)*0.5)
      XTX_TP         = (XTX_TOP*MX.TEXT_H)+2
      XTX_BP         = XTX_W_HEIGHT*MX.TEXT_H
      XTX_H_LP       = 0
      XTX_H_RP       = MX.WIDTH - (XTX_RP - XTX_LP)
      XTX_H_BP       = MX.HEIGHT
      XTX_H_TP       = MX.HEIGHT-(.7 * MX.TEXT_H)
      XTX_H_TP       = MAX(0,XTX_H_TP)
c     XTX_H_TP       = ((MX.HEIGHT/MX.TEXT_H)-.8) * MX.TEXT_H
c     XTX_H_TP       = XTX_W_HEIGHT*MX.TEXT_H
c     XTX_H_BP       = (XTX_W_HEIGHT+.8)*MX.TEXT_H 
      XTX_DISP_LINES = XTX_W_HEIGHT - XTX_TOP
      XTX_FIRST_LINE = MAX (1,
     +                  MIN (XTX_FIRST_LINE,XTX_TOTAL-XTX_DISP_LINES+1))
      XTX_LAST_LINE  = XTX_FIRST_LINE + XTX_DISP_LINES - 1
      XTX_FIRST_CHAR = MAX (1,MIN (XTX_FIRST_CHAR,XTX_H_TOTAL -
     +                 XTX_W_WIDTH+1))
      XTX_LAST_CHAR  = XTX_FIRST_CHAR + XTX_W_WIDTH - 1

      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      SUBROUTINE XTX$WRITE_HEAD (COLOR,TEXT)
      IMPLICIT NONE

      INTEGER*4 COLOR
      CHARACTER*(*) TEXT

      include 'primitive.inc'
      include 'xtxinclude.inc'

      XTX_HCOLOR = COLOR
      XTX_HSIZE  = M$LENGTH (TEXT)
      IF (XTX_HSIZE .GT. 0) THEN
         XTX_HEAD(1:XTX_HSIZE) = TEXT
      ELSE
         XTX_HSIZE  = 1
         XTX_HEAD(1:XTX_HSIZE) = ' '
      ENDIF
     
      CALL XTX$WRITE_OUTPUT (.TRUE.,1,COLOR,TEXT)


      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      SUBROUTINE XTX$REWRITE_HEAD ()
      IMPLICIT NONE

      include 'xtxinclude.inc'

      CHARACTER*(XTX_P_BBUF) TEXT

      TEXT = XTX_HEAD(1:XTX_HSIZE)
      CALL XTX$WRITE_OUTPUT (.TRUE.,1,XTX_HCOLOR,TEXT(1:XTX_HSIZE))


      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      SUBROUTINE XTX$WRITE_TEXT (ROW,COLOR,TEXT)
      IMPLICIT NONE
      INTEGER*4     ROW,COLOR
      CHARACTER*(*) TEXT

      include 'primitive.inc'
      include 'xtxinclude.inc'

      INTEGER*4     ALEN,TEXTALEN
      INTEGER*4     STATUS
      INTEGER*4     SIZE,CR, CR2
      INTEGER*4     START
C     Use 2*XTX_P_BBUF because of APPEND
      CHARACTER*(65535) TEXTA

      SIZE = M$LENGTH (TEXT)
      IF (XTX_ROW) THEN
        IF (XTX_TAG) THEN
          XTX_H_TOTAL   = MAX (XTX_H_TOTAL,SIZE+9)
        ELSE
          XTX_H_TOTAL   = MAX (XTX_H_TOTAL,SIZE)
        ENDIF
      ELSE
        START = 1
        DO WHILE (START .LT. SIZE)
          CR = INDEX (TEXT(START:SIZE),'/&')
          IF (CR.LT.1) THEN
            XTX_H_TOTAL   = MAX (XTX_H_TOTAL,(SIZE-START+1)) 
            START = SIZE
          ELSE 
            XTX_H_TOTAL = MAX(XTX_H_TOTAL, CR)
c DR 13510
c            START = CR + 1
            START = START + CR + 1
          ENDIF
        ENDDO
      ENDIF
      ALEN = 0

      IF (XTX_APPEND .AND. XTX_SCROLL) THEN
         IF (XTX_W_NEXT .GT. 0) THEN
            ALEN = M$LENGTH (XTX_BUF(XTX_W_NEXT).T(1:XTX_P_MAX_WIDTH))
         ELSEIF (XTX_W_NEXT .EQ. 0) THEN
C           Don't append if there is nothing written to the screen
            XTX_APPEND = .FALSE.
         ENDIF
      ENDIF

      IF (ALEN .GT. 0) THEN
         TEXTALEN = ALEN + 1 + SIZE
         TEXTA(1:TEXTALEN) = XTX_BUF(XTX_W_NEXT).T(1:ALEN) //' '//
     +                       TEXT(1:SIZE)
         CALL XTX$WRITE_OUTPUT (.FALSE.,ROW,COLOR,TEXTA(1:TEXTALEN))
      ELSE
         IF (SIZE .GT. 0) THEN
            CALL XTX$WRITE_OUTPUT (.FALSE.,ROW,COLOR,TEXT(1:SIZE))
         ELSE
            CALL XTX$WRITE_OUTPUT (.FALSE.,ROW,COLOR,' ')
         ENDIF
      ENDIF

      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      SUBROUTINE XTX$WRITE_TEXTA (ROW,COLOR,TEXT)
      IMPLICIT NONE

      include 'primitive.inc'
      include 'xtxinclude.inc'

      INTEGER*4     ROW,COLOR
      CHARACTER*(*) TEXT
      LOGICAL*4     SAVE_APPEND


      SAVE_APPEND = XTX_APPEND
      XTX_APPEND  = .TRUE.
      CALL XTX$WRITE_TEXT (ROW,COLOR,TEXT)
      XTX_APPEND  = SAVE_APPEND


      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      SUBROUTINE XTX$WRITE_OUTPUT (HEADER,ROW,COLOR,TEXT)
      IMPLICIT NONE

      LOGICAL*4     HEADER
      INTEGER*4     ROW
      INTEGER*4     COLOR
      CHARACTER*(*) TEXT

      include 'primitive.inc'
      include 'xtxinclude.inc'

      INTEGER*4     STATUS
      INTEGER*4     I,CR
      INTEGER*4     WROW
      INTEGER*4     SIZE,START,STOP,CHARS,BEGIN,HLINE
      LOGICAL*4     FIRST,DONE,LEADING,INCREMENT,SAVEINC
      CHARACTER*8   TOD
      integer*4     INDENT		! jdh added
      PARAMETER     (INDENT = 9)	! jdh modified
      CHARACTER*(INDENT) INDENT_TEXT
      CHARACTER*(XTX_P_MAX_WIDTH) LINE
      INTEGER*4     OLD_FIRST_LINE
      INTEGER*4     OLD_TOTAL

      OLD_FIRST_LINE = XTX_FIRST_LINE
      OLD_TOTAL      = XTX_TOTAL
      LEADING = .TRUE.
      SIZE = LEN(TEXT)
      STOP = 0
      BEGIN = 1
      CHARS = XTX_W_WIDTH
      IF (XTX_SCROLL) THEN
         INCREMENT = .NOT. XTX_APPEND
      ELSE
         INCREMENT = .TRUE.
      ENDIF

      IF (HEADER) THEN
         HLINE = 0
      ELSE
         IF (XTX_TAG) THEN
            FIRST = .TRUE.
            CALL TIME (TOD)
            INDENT_TEXT = TOD
            BEGIN = INDENT + 1
            CHARS = XTX_W_WIDTH - INDENT
         ENDIF
         IF (XTX_REWRITE) THEN
            CALL XTX$CLEAR_WINDOW ()
            IF (XTX_HSIZE .GT. 0) THEN
               CALL XTX$REWRITE_HEAD ()
            ENDIF
            DO I = 1 , XTX_TOTAL
               XTX_BUF(I).C = XTX_P_DEF_COLOR
               XTX_BUF(I).T = ' '
            ENDDO
            XTX_W_NEXT     = 0
            XTX_TOTAL      = 0
            XTX_FIRST_LINE = 1
            XTX_LAST_LINE  = XTX_FIRST_LINE + XTX_DISP_LINES - 1
         ENDIF
      ENDIF

      DO WHILE (STOP .LT. SIZE)

         IF ((.NOT. HEADER) .AND. XTX_ROW) THEN

            STOP = SIZE
            XTX_BUF(ROW).C = COLOR
            XTX_BUF(ROW).T(BEGIN:XTX_P_MAX_WIDTH) = TEXT(1:STOP)

         ELSE

            START = STOP
            DONE = .FALSE.
            IF (LEADING) THEN
               LEADING = .FALSE.
               START = STOP + 1
            ELSE
C              Find first non blank character
               DO WHILE (.NOT. DONE)
                  START = START + 1
                  IF ((TEXT(START:START) .NE. ' ') .OR.
     +                (START .GE. SIZE)) THEN
                     DONE = .TRUE.
                  ENDIF
               ENDDO
            ENDIF
            IF (HEADER .AND. XTX_ROW) THEN
               STOP = SIZE
            ELSE
               STOP = MIN(SIZE,START+CHARS-1)
            ENDIF

            CR = INDEX (TEXT(START:STOP),'/&')
            IF (CR .GT. 0) THEN
               STOP = START + CR - 2
               IF (HEADER) THEN
                  HLINE = HLINE + 1
                  IF (STOP .GE. START) THEN
                     LINE = TEXT(START:STOP)
                     CALL MX$COLORTEXT (COLOR,1,HLINE*MX.TEXT_H,
     +                               LINE(1:XTX_W_WIDTH))
                  ELSE
                     CALL MX$COLORTEXT (COLOR,1,HLINE*MX.TEXT_H,' ')
                  ENDIF
               ELSE
                  SAVEINC = INCREMENT
                  IF (STOP .GE. START) THEN
                     CALL XTX$SET_NEXT_TEXT (COLOR,BEGIN,INCREMENT,
     +                                    TEXT(START:STOP))
                  ELSE
                     CALL XTX$SET_NEXT_TEXT (COLOR,BEGIN,INCREMENT,' ')
                  ENDIF
               ENDIF
               STOP = STOP + 2
               LEADING = .TRUE.
            ELSEIF ((STOP .EQ. SIZE)                                .OR.
     +          (TEXT(STOP:STOP) .EQ. ' ')                          .OR.
     +          ((STOP.LT.SIZE).AND.(TEXT(STOP+1:STOP+1).EQ.' '))) THEN
               IF (HEADER) THEN
                  HLINE = HLINE + 1
                  LINE = TEXT(START:STOP)
                  CALL MX$COLORTEXT (COLOR,1,HLINE*MX.TEXT_H,
     +                               LINE(1:XTX_W_WIDTH))
               ELSE
                  SAVEINC = INCREMENT
                  CALL XTX$SET_NEXT_TEXT (COLOR,BEGIN,INCREMENT,
     +                                    TEXT(START:STOP))
               ENDIF
            ELSE
               DO I = STOP , START , -1
                  IF (TEXT(I:I) .EQ. ' ') THEN
                     STOP = I
                     GOTO 100
                  ENDIF
               ENDDO
100            CONTINUE
               IF (HEADER) THEN
                  HLINE = HLINE + 1
                  LINE = TEXT(START:STOP)
                  CALL MX$COLORTEXT (COLOR,1,HLINE*MX.TEXT_H,
     +                               LINE(1:XTX_W_WIDTH))
               ELSE
                  SAVEINC = INCREMENT
                  CALL XTX$SET_NEXT_TEXT (COLOR,BEGIN,INCREMENT,
     +                                    TEXT(START:STOP))
               ENDIF
            ENDIF

         ENDIF


         IF (.NOT. HEADER) THEN

            IF (XTX_TAG) THEN
               IF (XTX_ROW) THEN
                  XTX_BUF(ROW).T(1:INDENT) = INDENT_TEXT
               ELSE
                  XTX_BUF(XTX_W_NEXT).T(1:INDENT) = INDENT_TEXT
                  IF (FIRST) THEN
                     INDENT_TEXT = ' '
                     FIRST = .FALSE.
                  ENDIF
               ENDIF
            ENDIF

            IF (XTX_ROW) THEN
               XTX_TOTAL = MIN (MAX(ROW,XTX_TOTAL),XTX_MAX_HEIGHT)
               XTX_W_NEXT = XTX_TOTAL
               IF ((ROW .GE. XTX_FIRST_LINE) .AND.
     +             (ROW .LE. XTX_LAST_LINE)) THEN
                  WROW = ROW + XTX_TOP - XTX_FIRST_LINE + 1
c                 CALL MX$COLORTEXT (COLOR,1,WROW*MX.TEXT_H,
c    +                               XTX_BUF(ROW).T(1:XTX_W_WIDTH))
                  CALL MX$COLORTEXT (COLOR,1,WROW*MX.TEXT_H,
     +                    XTX_BUF(ROW).T(XTX_FIRST_CHAR:XTX_LAST_CHAR))
               ENDIF
            ELSE
               IF (SAVEINC) THEN
                  XTX_TOTAL      = MIN (XTX_TOTAL+1,XTX_MAX_HEIGHT)
                  IF (XTX_SCROLL) THEN
                     XTX_FIRST_LINE = MAX (XTX_TOTAL-XTX_DISP_LINES+1,1)
                     XTX_LAST_LINE  = XTX_FIRST_LINE + XTX_DISP_LINES -1
                  ENDIF
               ENDIF
            ENDIF

         ENDIF

      ENDDO


      IF (HEADER) THEN
         XTX_TOP        = MAX (XTX_TOP,HLINE)
         XTX_TP         = (XTX_TOP*MX.TEXT_H)+2
         XTX_DISP_LINES = XTX_W_HEIGHT - XTX_TOP + 1
      ELSE
         IF (.NOT. XTX_ROW) THEN
            CALL XTX$SCROLL_TEXT ()
         ENDIF
      ENDIF


      IF ((.NOT. XTX_ROW)                       .OR.
     +    (XTX_FIRST_LINE .NE. OLD_FIRST_LINE)  .OR.
     +    (XTX_TOTAL      .NE. OLD_TOTAL     )) THEN
         STATUS = XTX$DRAW_SCROLLBAR (0)
      ENDIF
      IF (XTX_ROW .AND. (XTX_W_WIDTH .LT. XTX_H_TOTAL)) THEN
         STATUS = XTX$DRAW_H_SCROLLBAR (0)
      ENDIF

      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      SUBROUTINE XTX$SET_NEXT_TEXT (COLOR,START,INCREMENT,TEXT)
      IMPLICIT NONE

      INTEGER*4     COLOR
      INTEGER*4     START
      LOGICAL*4     INCREMENT
      CHARACTER*(*) TEXT

      include 'xtxinclude.inc'


      IF (INCREMENT) THEN
         XTX_W_NEXT = XTX_W_NEXT + 1
      ELSE
         INCREMENT = .TRUE.
      ENDIF
      IF (XTX_W_NEXT .GT. XTX_MAX_HEIGHT) THEN
         IF (XTX_SCROLL) THEN
            XTX_W_NEXT = 1
         ELSE
            XTX_W_NEXT = MIN (XTX_W_NEXT,XTX_MAX_HEIGHT)
         ENDIF
      ENDIF
      XTX_BUF(XTX_W_NEXT).C = COLOR
      XTX_BUF(XTX_W_NEXT).T(START:XTX_E_WIDTH) = TEXT


      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      SUBROUTINE XTX$COLOR (ID,R,G,B)
      IMPLICIT NONE

      INTEGER*2 ID,R,G,B
      INTEGER*4 temp		! DR 12215

      include 'qmessages.inc'


      MQD.COLOR.POSITION = ID
      MQD.COLOR.RED      = R
      MQD.COLOR.GREEN    = G
      MQD.COLOR.BLUE     = B
      TEMP = ID 				! DR 12215
c      CALL MX$COLORTAB (ID,MQD.COLOR,-2)	! DR 12215
      CALL MX$COLORTAB (TEMP,MQD.COLOR,-2)	! DR 12215


      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      SUBROUTINE XTX$SCROLL_TEXT ()
      IMPLICIT NONE

      include 'xtxinclude.inc'

      INTEGER*4     STATUS
      INTEGER*4     I,J,K
      INTEGER*4     B1,E1,B2,E2


      K = XTX_W_NEXT - (XTX_TOTAL - XTX_FIRST_LINE)
      IF (K .LT. 1) THEN
         B1 = K + XTX_TOTAL
         E1 = XTX_TOTAL
         B2 = 1
         E2 = XTX_DISP_LINES + K - 1
      ELSE
         B1 = 1
         E1 = 0
         B2 = K
         E2 = B2 + XTX_DISP_LINES - 1
      ENDIF

      J = XTX_TOP
      DO I = B1 , E1
         J = J + 1
c        CALL MX$COLORTEXT (XTX_BUF(I).C,1,J*MX.TEXT_H,
c    +                      XTX_BUF(I).T(1:XTX_W_WIDTH)) 
         CALL MX$COLORTEXT (XTX_BUF(I).C,1,J*MX.TEXT_H,
     +                      XTX_BUF(I).T(XTX_FIRST_CHAR:XTX_LAST_CHAR))
      ENDDO 
      DO I = B2 , E2
         J = J + 1
c        CALL MX$COLORTEXT (XTX_BUF(I).C,1,J*MX.TEXT_H,
c    +                      XTX_BUF(I).T(1:XTX_W_WIDTH))
         CALL MX$COLORTEXT (XTX_BUF(I).C,1,J*MX.TEXT_H,
     +                      XTX_BUF(I).T(XTX_FIRST_CHAR:XTX_LAST_CHAR))
      ENDDO

      CALL XTX$XFLUSH ()


      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      FUNCTION XTX$DRAW_SCROLLBAR (MODE)
      IMPLICIT NONE

      INTEGER*4 MODE

      include 'primitive.inc'
      include 'xtxinclude.inc'

      INTEGER*4 STATUS
      LOGICAL*4 DOIT
      REAL*8    FIRST_LINE,LAST_LINE,SAVE_FIRST_LINE
      INTEGER*4 FIRST_LINE_I4

      IF (.NOT. XTX_DISABLE_SCROLLBAR) THEN
         IF (XTX_TOTAL .GT. XTX_DISP_LINES) THEN
            FIRST_LINE_I4 = XTX_FIRST_LINE
            FIRST_LINE = DBLE(XTX_FIRST_LINE)
            SAVE_FIRST_LINE = FIRST_LINE
            LAST_LINE  = DBLE(XTX_LAST_LINE)
            XTX_SCROLLBAR  = .TRUE.
            STATUS = MX$SCROLLBAR (MODE,XTX_LP,XTX_RP,XTX_TP,XTX_BP,
     +                             FIRST_LINE,
     +                             LAST_LINE,
     +                             1.0d0,
     +                             DBLE(XTX_TOTAL))
            IF (MODE .NE. 0) THEN
               IF (STATUS .NE. 0) THEN
                  IF (SAVE_FIRST_LINE .GT. FIRST_LINE) THEN
                     XTX_FIRST_LINE = MAX (NINT(FIRST_LINE),1)
                  ELSE
                     XTX_FIRST_LINE = MIN (NINT(FIRST_LINE),
     +                             MAX(XTX_TOTAL-XTX_DISP_LINES+1,1))
                  ENDIF
                  XTX_LAST_LINE = XTX_FIRST_LINE + XTX_DISP_LINES -1
                  IF (XTX_FIRST_LINE .EQ. FIRST_LINE_I4) THEN
                     STATUS = 0
                  ENDIF
               ENDIF
            ENDIF
         ELSE
            STATUS = -1
         ENDIF
      ELSE
         STATUS = 0
      ENDIF


9999  CONTINUE
      XTX$DRAW_SCROLLBAR = STATUS
      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      FUNCTION XTX$DRAW_H_SCROLLBAR (MODE)
      IMPLICIT NONE

      INTEGER*4 MODE

      include 'primitive.inc'
      include 'xtxinclude.inc'

      INTEGER*4 STATUS
      LOGICAL*4 DOIT
      REAL*8    FIRST_CHAR,LAST_CHAR,SAVE_FIRST_CHAR
      INTEGER*4 FIRST_CHAR_I4

      IF (XTX_H_TOTAL .GT. XTX_W_WIDTH) THEN
         FIRST_CHAR_I4 = XTX_FIRST_CHAR
         FIRST_CHAR = DBLE(XTX_FIRST_CHAR)
         SAVE_FIRST_CHAR = FIRST_CHAR
         LAST_CHAR  = DBLE(XTX_LAST_CHAR)
         XTX_SCROLLBAR  = .TRUE.
         STATUS = MX$SCROLLBAR (MODE,XTX_H_LP,XTX_H_RP,
     +                          XTX_H_TP,XTX_H_BP,
     +                          FIRST_CHAR,
     +                          LAST_CHAR,
     +                          1.0d0,
     +                          DBLE(XTX_H_TOTAL))
         IF (MODE .NE. 0) THEN
            IF (STATUS .NE. 0) THEN
               IF (SAVE_FIRST_CHAR .GT. FIRST_CHAR) THEN
                  XTX_FIRST_CHAR = MAX (NINT(FIRST_CHAR),1)
               ELSE
                  XTX_FIRST_CHAR = MIN (NINT(FIRST_CHAR),
     +                             MAX(XTX_H_TOTAL-XTX_W_WIDTH+1,1))
               ENDIF
               XTX_LAST_CHAR = XTX_FIRST_CHAR + XTX_W_WIDTH -1
               IF (XTX_FIRST_CHAR .EQ. FIRST_CHAR_I4) THEN
                  STATUS = 0
               ENDIF
            ENDIF
         ENDIF
      ELSE
         STATUS = -1
      ENDIF

9999  CONTINUE
      XTX$DRAW_H_SCROLLBAR = STATUS
      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      SUBROUTINE XTX$CLEAR_SCROLLBAR ()
      IMPLICIT NONE

      include 'xtxinclude.inc'

      XTX_SCROLLBAR  = .FALSE.
      CALL MX$CLEAR_AREA (XTX_LP,XTX_TP,XTX_RP,XTX_BP)


      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      SUBROUTINE XTX$REFRESH ()
      IMPLICIT NONE

      include 'xtxinclude.inc'

      INTEGER*4 STATUS


      IF (XTX_HSIZE .GT. 0) THEN
         CALL XTX$REWRITE_HEAD ()
      ENDIF
      CALL XTX$SCROLL_TEXT ()

      CALL XTX$XFLUSH ()

      STATUS = XTX$DRAW_SCROLLBAR (0)
      IF (XTX_ROW) STATUS = XTX$DRAW_H_SCROLLBAR (0)


      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      SUBROUTINE XTX$CLEAR_WINDOW ()
      IMPLICIT NONE

      include 'xtxinclude.inc'

      XTX_SCROLLBAR  = .FALSE.
      CALL MX$CLEAR_WINDOW ()


      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      SUBROUTINE XTX$CLEAR ()
      IMPLICIT NONE

      include 'xtxinclude.inc'

      INTEGER*4 I,LAST


      IF (XTX_ROW) THEN
         LAST = XTX_TOTAL
      ELSE
         LAST = XTX_MAX_HEIGHT
      ENDIF
      DO I = 1 , LAST
         XTX_BUF(I).C = XTX_P_DEF_COLOR
         XTX_BUF(I).T = ' '
      ENDDO
      XTX_CLEAR    = .TRUE.
      XTX_W_NEXT   = 0
      XTX_TOTAL    = 0
      XTX_H_TOTAL    = 0
      XTX_FIRST_LINE = 1
      XTX_LAST_LINE  = XTX_FIRST_LINE + XTX_DISP_LINES - 1
      XTX_FIRST_CHAR = 1
      XTX_LAST_CHAR  = XTX_FIRST_CHAR + XTX_W_WIDTH - 1
      CALL XTX$XFLUSH ()
      CALL XTX$CLEAR_WINDOW ()
      IF (XTX_HSIZE .GT. 0) THEN
         CALL XTX$REWRITE_HEAD ()
      ENDIF


      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      SUBROUTINE XTX$CLEAR_ROWS (FROM,TO)
      IMPLICIT NONE

      INTEGER*4 FROM
      INTEGER*4 TO

      include 'xtxinclude.inc'

      INTEGER*4 STATUS
      INTEGER*4 ROW
      INTEGER*4 WROW
      INTEGER*4 OLD_FIRST
      LOGICAL*4 DOBAR
      LOGICAL*4 DOLOOP


      IF (.NOT. XTX_ROW) THEN
         GOTO 9999
      ELSEIF (FROM .GT. TO) THEN
         GOTO 9999
      ELSEIF (FROM .GT. XTX_TOTAL) THEN
         GOTO 9999
      ELSE

         FROM = MAX (1,FROM)
         TO   = MIN (XTX_TOTAL,TO)

         DO ROW = FROM , TO
            XTX_BUF(ROW).C = XTX_P_DEF_COLOR
            XTX_BUF(ROW).T = ' '
         ENDDO

         DOBAR  = .FALSE.
         DOLOOP = .FALSE.

         IF (TO .EQ. XTX_TOTAL) THEN

            XTX_TOTAL = FROM - 1
            XTX_W_NEXT = XTX_TOTAL
            OLD_FIRST = XTX_FIRST_LINE
            XTX_FIRST_LINE = MAX (1,
     +                  MIN (XTX_FIRST_LINE,XTX_TOTAL-XTX_DISP_LINES+1))
            IF (OLD_FIRST .NE. XTX_FIRST_LINE) THEN
               XTX_LAST_LINE  = XTX_FIRST_LINE + XTX_DISP_LINES - 1
               CALL XTX$REFRESH ()
            ELSE
               DOLOOP = .TRUE.
               DOBAR = .TRUE.
            ENDIF

         ELSE
            DOLOOP = .TRUE.
         ENDIF

         IF (DOLOOP) THEN
            CALL XTX$XFLUSH ()
            DO ROW = FROM , TO
               IF ((ROW .GE. XTX_FIRST_LINE) .AND.
     +             (ROW .LE. XTX_LAST_LINE)) THEN
                  WROW = ROW + XTX_TOP - XTX_FIRST_LINE + 1
                  DOBAR = .TRUE.
                  CALL MX$COLORTEXT (XTX_BUF(ROW).C,1,WROW*MX.TEXT_H,
     +                               XTX_BUF(ROW).T(1:XTX_W_WIDTH))
               ENDIF
            ENDDO
         ENDIF

         IF (DOBAR) THEN
            STATUS = XTX$DRAW_SCROLLBAR (0)
            IF (XTX_SCROLLBAR) THEN
               IF (STATUS .EQ. -1) THEN
                  CALL XTX$CLEAR_SCROLLBAR ()
               ENDIF
            ENDIF
         ENDIF

      ENDIF


9999  CONTINUE

      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      SUBROUTINE XTX$POSITION_ROW (POSITION,ROW)
      IMPLICIT NONE

      INTEGER*4    ROW
      INTEGER*4    POSITION

      include 'xtxinclude.inc'

      INTEGER*4    FROW


      IF (POSITION .EQ. 1) THEN
C
C        Top Row
C
         FROW = MAX (1, MIN (ROW,XTX_TOTAL-XTX_DISP_LINES+1))

      ELSEIF (POSITION .EQ. 3) THEN
C
C        Bottom Row
C
         FROW = MAX (1, MIN (ROW-XTX_DISP_LINES+1,
     +                       XTX_TOTAL-XTX_DISP_LINES+1))
      ELSE
C
C        Middle Row
C
         FROW = MAX (1, MIN (ROW-((XTX_DISP_LINES+1)/2)+1,
     +                       XTX_TOTAL-XTX_DISP_LINES+1))
      ENDIF

      IF (FROW .NE. XTX_FIRST_LINE) THEN
         XTX_FIRST_LINE = FROW
         XTX_LAST_LINE  = XTX_FIRST_LINE + XTX_DISP_LINES - 1
         CALL XTX$REFRESH ()
      ENDIF


      RETURN
      END
C-----------------------------------------------------------------------
C
C-----------------------------------------------------------------------
      SUBROUTINE XTX$XFLUSH ()
      IMPLICIT NONE

      CALL MX$XFLUSH ()

      RETURN
      END
C-----------------------------------------------------------------------
