Ftcl: Calling Tcl procedures

The interface: Fortran calling Tcl

Fortran routines may use the following routines to interact with the Tcl interpreter (cf. Technical details): Note: The routines to manipulate Fortran arrays have not been implemented yet.

Creating a graphical user-interface with Tk

The Ftcl library makes it possible to create user-interfaces using the Tk extension. Here is a small example (see also the lander sources in the distribution):
      PROGRAM LANDER

      USE FTCL

      CHARACTER(80) astring

      IMPULSE = 200
      FHEIGHT = 10000.0
      SPEED = 100.0
      FUEL = 1000.0
      GROSS = 900.0

      CALL ftcl_start('config.tcl')
      CALL ftcl_script('package require Tk')
      CALL ftcl_script('inputInitialValues')
      CALL ftcl_script('update')
      CALL ftcl_script('update idle')

      CALL ftcl_get_string('burn', astring)

      CALL ftcl_get_int('ready', irdy)

      DO WHILE (irdy == 0)
        CALL ftcl_script('update')
        CALL ftcl_script('update idle')
        CALL ftcl_get_int('ready', irdy)
      ENDDO
      CALL ftcl_get_real('burn', burn)
      I = 0

      DO WHILE (FHEIGHT .GT. 0)
        CALL ftcl_get_real('burn', burn)
        CALL CALCSPEED (SPEED, FUEL, GROSS, BURN, IMPULSE, SPEED)
        I = I + 1
        FUEL = FUEL - BURN
        IF (FUEL .LE. 0)  THEN
          FUEL = 0
          BURN = 0
        CALL ftcl_put_real('burn', burn)
        ENDIF
        FHEIGHT = FHEIGHT - SPEED
        CALL ftcl_put_int('time', I)
        CALL ftcl_put_real('speed', speed)
        CALL ftcl_put_real('fuel', fuel)
        CALL ftcl_put_real('ht', FHEIGHT)
        CALL ftcl_script('showState')
        CALL ftcl_script('update idle')
      ENDDO

      DO
        CALL ftcl_script('update idle')
        CALL ftcl_script('update')
      ENDDO

      END

      SUBROUTINE CALCSPEED (finit, fuel, gross, burn, impulse, speed)
      MASS = gross + fuel
      SPEED = SPEED + 9.8 * (1 - impulse * log(mass/(mass-burn)))
      END
This program consists of the following steps: Because the Fortran program stays in control, it is necessary to use the update command, so that window and other events can be handled properly.

An alternative is to use the ftcl_make_command() routine to register a new Tcl command (implemented in Fortran) and to enter the event loop, as normally happens in applications using Tk (such as wish), via ftcl_main_loop().