      PROGRAM TWIMP2
C
C        A trivial Fortran wimp program which makes a window with 
C        selectable icons and also shows how to interact with mouse clicks
C        and make on-line help using !Help.
C
C        The routines to read and write to text icons ought to have 
C        their error returns checked but this complicates the logic.
C
C        Needs only the Wimp library
C
C        initialise the wimp
      CALL WPINIT('TWIMP2')
C        set up the desk-top
      CALL SETUP
C        go into the wimp loop (without null calls)
      CALL WPLOOP(1)
C        that's all we can do here
      END
C
      SUBROUTINE SETUP
C        here we make the windows and icons
C        keep their handles in common
      COMMON/HANDLE/ IWH,IHS,IHB,ICS,IHT
      
      COMMON TEXT1,TEXT2,TITLE
      CHARACTER*20 TEXT1,TEXT2,TITLE
C        make the window, but without any drawing needs
      CALL WPCHWF(4,.TRUE.)
C        and without a 'close' and 'back' icon
      CALL WPCHWF(25,.FALSE.)
      CALL WPCHWF(24,.FALSE.)
      TITLE = 'My window'//CHAR(0)
      CALL WPMKNW(400,400,200,200,400,400,0,0,TITLE,IWH)
C        check we can read thie window title
      CALL WPRDTX(IWH,-1,TEXT1,LGTH)
      IF(TEXT1.NE.'My window')
     + CALL WPERR(1,'wrong text in window title: '//TEXT1,IDUM)
C        make an icon to stop the program
      CALL WPADTI(IWH,40,-80,80,48,'Stop',0,IHS)
C        make a big indirected writeable icon
      CALL WPCHTF(8,.TRUE.)
      CALL WPCHTF(115)
      TEXT1=CHAR(0)
      CALL WPADTI(IWH,40,-140,320,48,TEXT1,0,IHT)
C        store some real text in it
      CALL WPWRTX(IWH,IHT,'Type some text here',IERR)
C        make another big selectable icon with ESG 1      
      CALL WPCHTF(111)
      TEXT2='A much bigger icon'//CHAR(0)
C        in ESG 1
      CALL WPCHTF(201)
      CALL WPADTI(IWH,40,-200,320,48,TEXT2,0,IHB)
C        and a sprite icon (the one for a text file) also in ESG 1
      CALL WPCHSF(201)
      CALL WPADSI(IWH,40,-300,'file_ffd',ICS)
C        open the window
      CALL WPOPNW(IWH)
      RETURN
      END
C
      SUBROUTINE WQCLIK(IWHAN,ICONH,IX,IY,IBUTT)
C         here we receive a mouse button click
      COMMON/HANDLE/ IWH,IHS,IHB,ICS,IHT
C         make sure it is our window (it must be)
      IF(IWHAN.NE.IWH) RETURN
C         make sure it is the 'Stop' icon
      IF(ICONH.NE.IHS) RETURN
C         which button?
C         if 'select' then stop
      IF(IBUTT.EQ.4) CALL WPQUIT
C         if 'adjust' then complain
      IF(IBUTT.EQ.1) THEN
        CALL WPERR(3,'Do you really want to stop? OK to continue',IRES)
        IF(IRES.NE.1) CALL WPQUIT
      ENDIF
      RETURN
      END
C
      SUBROUTINE WQHELP(IWHAN,ICONH,IX,IY,SHELP)
C            this routine supplies help information
C            for the !Help application
      CHARACTER*(*) SHELP
      COMMON/HANDLE/ IWH,IHS,IHB,ICS,IHT
C            it must be our window, so we don't need to
C            check IWHAN against IWH
C
C            check if over icon
      IF(ICONH .EQ. IHS) THEN
C            '\S' is translated to 'Click Select to '
        SHELP='\Sstop the task'
      ELSE IF(ICONH .EQ. IHB) THEN
C             note the '|M' forces a new line in the !Help text
        SHELP='This is an icon with indirected text|Mit can be '
     +  //'selected exclusively with the sprite icon below'
      ELSE IF(ICONH .EQ. ICS) THEN
        SHELP='This is a sprite icon|Mit can be '
     +  //'selected exclusively with the text icon above'
      ELSE IF(ICONH .EQ. IHT) THEN
        SHELP='This is an icon where you can type text|M'//
     +  '\Senter any text or numbers'
      ELSE
        SHELP='The pointer is over the \w at'
        WRITE(SHELP(34:43),101)IX,IY
        SHELP(44:59)='|M screen coords'
        CALL WPXY2S(IWHAN,IX,IY,JX,JY)
        WRITE(SHELP(61:70),101)JX,JY
  101   FORMAT(2I5)
      ENDIF
      RETURN
      END
C
      SUBROUTINE WQKEYP(IWHAN,ICONH,IX,IY,IC,KEY)
C             called when user types into the window
      COMMON/HANDLE/ IWH,IHS,IHB,ICS,IHT
C             temporary space for text
      CHARACTER*20 TEMP
C             check that it is in our window and is <return>
      IF(IWHAN.EQ.IWH .AND. KEY.EQ.13) THEN
C             check it is our writeable icon
        IF(ICONH.EQ.IHT) THEN
C             see if it is a number
          CALL WPRDIN(IWH,IHT,INTG,IERR)
          IF(IERR.EQ.0) THEN
            CALL WPWRIN(IWH,IHB,INTG,IERR)
C             and in title
            CALL WPWRIN(IWH,-1,INTG,IERR)
          ELSE
C             not integer, so get as text
            CALL WPRDTX(IWH,IHT,TEMP(2:),LGTH)
C             put quotes around it
            L = LNBLNK(TEMP)+1
            TEMP(1:1) = '"'
            TEMP(L:L) = '"'
C             store text in other icon
            CALL WPWRTX(IWH,IHB,TEMP,LGTH)
C             and in title
            CALL WPWRTX(IWH,-1,TEMP,LGTH)
          ENDIF
C             remove text 'caret' from window
            CALL WPSTCP(-1)
        ENDIF
      ELSE
C             if we don't want this key press, pass it on...
        CALL WPKEYP(KEY)
      ENDIF
      RETURN
      END

