******************************************************************
*  FTEST.FOR                                                     *
*                                                                *
*  Demonstrates use of the Microsoft Mouse from FORTRAN 4.1      *
*                                                                *
*  Assumes ANSI.SYS and MOUSE.COM have been loaded by MS-DOS.    *
*  Compile using large model (default), and link with MOUSE.LIB  *
*  Example:   fl /FPc ftest.for -link mouse                      *
******************************************************************

      PROGRAM FTEST
      IMPLICIT INTEGER*2 (A-Z)

* Clear the display using ANSI.SYS escape-code sequence
      WRITE(*,*) CHAR(27),'[2J'

* Display simple instructions for user
      WRITE(*,*) 'FTEST - Mouse demonstration using FORTRAN 4.1'
      WRITE(*,*) ' '
      WRITE(*,*) 'Use mouse to highlight a menu option.'
      WRITE(*,*) 'Press either button to select option.'

* Check for mouse, resetting it in the process
      M1 = 0
      CALL MOUSEL(M1, M2, M3, M4)

* Quit if mouse wasn't found
      IF (M1.EQ.0) THEN
          WRITE(*,*) 'Error: Mouse not found'
          GOTO 999
      END IF

* Initialize menu pointer to first option
      MENPTR = 1

* Initialize count of accumulated vertical mouse motion
      MOTION = 0

* Set flag to update menu first time
      WFLAG = 1

* Main loop starts here
   10 CONTINUE

* Update the menu only when necessary
      IF (WFLAG.EQ.1) THEN
          WFLAG = 0

* Write first line of the menu, highlighted if selected
          IF (MENPTR.EQ.1) THEN
              WRITE(*,*) CHAR(27),'[7m'
          ELSE
              WRITE(*,*) CHAR(27),'[0m'
          END IF
          WRITE(*,*) CHAR(27),'[10;29f 1. First menu option '

* Write second line of the menu, highlighted if selected
          IF (MENPTR.EQ.2) THEN
              WRITE(*,*) CHAR(27),'[7m'
          ELSE
              WRITE(*,*) CHAR(27),'[0m'
          END IF
          WRITE(*,*) CHAR(27),'[11;29f 2. Second option     '

* Write third line of the menu, highlighted if selected
          IF (MENPTR.EQ.3) THEN
              WRITE(*,*) CHAR(27),'[7m'
          ELSE
              WRITE(*,*) CHAR(27),'[0m'
          END IF
          WRITE(*,*) CHAR(27),'[12;29f 3. Third option      '

* Be sure highlighting is turned off
          WRITE(*,*) CHAR(27),'[0m'

* End of updating the menu
      END IF

* Accumulate vertical mouse motion counts
      M1 = 11
      CALL MOUSEL(M1, M2, M3, M4)
      MOTION = MOTION + M4

* Move up the menu if enough mouse motion
      IF (MOTION.LT.-17) THEN
          MOTION = 0
          IF (MENPTR.GT.1) THEN
              MENPTR = MENPTR - 1
              WFLAG = 1
          END IF
      END IF

* Move down the menu if enough mouse motion
      IF (MOTION.GT.17) THEN
          MOTION = 0
          IF (MENPTR.LT.3) THEN
              MENPTR = MENPTR + 1
              WFLAG = 1
          END IF
      END IF

* Check if left button pressed
      M1 = 5
      M2 = 0
      CALL MOUSEL(M1, M2, M3, M4)
      IF (M2.NE.0) THEN
          WRITE(*,*) 'Left button used to select option',MENPTR
          GOTO 999
      END IF

* Check if right button pressed
      M1 = 5
      M2 = 1
      CALL MOUSEL(M1, M2, M3, M4)
      IF (M2.NE.0) THEN
          WRITE(*,*) 'Right button used to select option',MENPTR
          GOTO 999
      END IF

* Loop back until one of the buttons is pressed
      GOTO 10

* All done
  999 CONTINUE
      END
