      PROGRAM TWIM11
C        a trivial Fortran wimp program which 
C        uses a sprite for drawing
C        needs the Wimp and Graphics libraries
C
      COMMON /GRAFIX/MXS(2),IWH,JKB,IBP
C        initialise the wimp
      CALL WPINIT('TWIMP11')
C        set up the desk-top
      CALL SETUP
C        go into the wimp loop with null calls
      CALL WPLOOP(1)
C        that's all we can do here
      END
C
      SUBROUTINE SETUP
C             common for menu
      COMMON /NAMES/WINDOW
      CHARACTER WINDOW*20
      COMMON /MMENU/ MENU(7+6*4)
      COMMON /GRAFIX/MXS(2),IWH,NCOLR
C            make menu
      CALL WPMKMB('Plot,Circle,Rectangle,axes,quit',MENU)
C            make window with no close icon
      CALL WPCHWF(25,.FALSE.)
      WINDOW = 'Test plotting'//CHAR(0)
      MXS(1)=800
      MXS(2)=800
      CALL WPMKNW(MXS(1),MXS(2),0,0,MXS(1),MXS(2),0,0,WINDOW,IWH)
      CALL SETWIN
      RETURN
      END
C
      SUBROUTINE SETWIN
C            set up the window with a sprite
      COMMON /GRAFIX/MXS(2),IWH,NCOLR
C             make sprite in current mode
      CALL WPMAKS(IWH,MXS(1),MXS(2),-1)
C            get number of colours - 1
      CALL GRRMV(-1,3,NCOLR)
      IF(NCOLR.EQ.-1) NCOLR=255
C             initialise sprite
      CALL WPBEGS(IWH)
C             make background black
      CALL GRSETT(256,0,0)
      CALL GRCLRT
C            explain what to do in cyan text
      CALL GRSETT(0,255,255)
      CALL GRTAB(0,1)
      PRINT 101
  101 FORMAT(10X,'Click with <Menu> over this window'//
     + 15X,'and try the examples')
C            restore output to screen
      CALL WPENDS(-1)
      CALL WPOPNW(IWH)
      RETURN
      END
C
      SUBROUTINE WQCLIK(IWHAN,ICON,IX,IY,IB)
C            open menu if menu button
      COMMON /MMENU/ MENU(7+6*4)
      IF(IB.EQ.2) CALL WPSHWM(MENU,IX,IY)
      RETURN
      END
C
      SUBROUTINE WQMENU(MBLOC,ITEM)
C             click over menu
      COMMON /MMENU/ MENU(7+6*4)
      COMMON /GRAFIX/MXS(2),IWH,NCOLR
      IF(ITEM.EQ.3) THEN
C               finished if 'quit' is clicked
        CALL WPQUIT
        RETURN
      ENDIF
C             here we want to draw something
C             direct output to sprite
      CALL WPBEGS(IWH)
C
      IF(ITEM.EQ.0) THEN
C             plot red circle
C (note inverting colours works differently in <256 colour modes)
        IF(NCOLR.LE.15) THEN
          CALL GRGCOL(3,12)
        ELSE
          CALL GRSETC(3,255,0,0)
        ENDIF
        CALL GRCIRC(128,288,96,.TRUE.)
C               return output to screen but only update
C               the rectangle containing the circle
C               allow 4 extra at the top and right because
C               this edge is not inclusive
        CALL WPENDS(32,160,228,388)
      ELSEIF(ITEM.EQ.1) THEN
C             plot green rectangle (EOR colour)
        IF(NCOLR.LE.15) THEN
          CALL GRGCOL(3,13)
        ELSE
          CALL GRSETC(3,0,255,0)
        ENDIF
        CALL GRRECT(128,32,384,288,.TRUE.)
C               return output to screen but only update
C               the rectangle (allowing 4 extra as for the circle)
        CALL WPENDS(128,32,388,292)
      ELSE
C             draw white axes
        IF(NCOLR.LE.15) THEN
          CALL GRGCOL(3,7)
        ELSE
          CALL GRSETC(3,255,255,255)
        ENDIF
        CALL GRMOVE(64,MXS(2)-64)
        CALL GRDRAW(64,64)
        CALL GRDRAW(MXS(1)-64,64)
C              return output to screen and update the whole window
        CALL WPENDS(-1)
      ENDIF
      RETURN
      END
C
      SUBROUTINE WQMODC
C             mode change, must delete and recreate window
      CALL SETWIN
      RETURN
      END
