\label{Tcl/Tk}
Tcl/Tk
The language Tcl was first presented
in [Ousterhout90].
Tcl was announced as a flexible cshell-like
language, intended to be used for developing
an X11-based toolkit.
A year later, the Tk toolkit (based on Tcl)
was presented in [Ousterhout91].
From the start Tcl/Tk has received a lot of attention,
since it provides a flexible and convenient way in which
to develop quite powerful window applications.
The Tcl language offers variables, assignment
and a procedure construct.
Also it provides a number of control constructs,
facilities for manipulating strings
and built-in primitives giving access to the underlying
operating system.
The basic Tcl language may easily be extended
by associating a function written in C
with a command name.
Arguments given to the command are passed
as strings to the function defining the command.
The Tk toolkit is an extension of Tcl
with commands to create and configure widgets
for displaying text and graphics,
and providing facilities for window management.
The Tk toolkit, and the wish interpreter
based on Tk, provides a convenient way
to program X-window based applications.
Example
\zline{\fboxwish}
\hspace*{2.5cm}
\epsfbox{hello.eps}
Scripts
-- hello world
button .b -text "Hello, world" -command {
puts stdout "hello world"
}
pack .b
slide: A Wish example
The wish program is an interpreter for executing
Tcl/Tk scripts.
As an example of a wish script, look
at the hello world program in slide [tcl-example].
The hello world script defines a button that
displays Hello, world, and prints hello world
to standard output when it is
activated by pressing the left mouse button.
The language used to write this script
is simply Tcl with the commands defined by Tk,
such as the button command (needed to create
a button) and the pack command (that is used to map
the button to the screen).
The wish program actually provides an example
of a simple application based on Tcl/Tk.
It may easily be extended to include, for example,
3D-graphics by linking the appropriate
C libraries and defining the functions making
this functionality available as (new) Tcl commands.
To define Tcl commands in C,
the programmer has to define a command
function
and declare the function
to be a command in Tcl by
invoking the {\em Tcl_CreateCommand}
function.
Creating a command is done with reference
to an interpreter, which accounts for the
first argument of {\em Tcl_CreateCommand}.
The name of the command, as may be used
in a Tcl script must be given as a second argument,
and the C/C++ function defining the command as a third argument.
Finally, when declaring a command, the address
of a structure containing client data
may be stored, which may
be (the address of) the root window, for example.
When the function is invoked
as the result of executing the Tcl command,
the client data stored at
declaration time is passed as the first
argument to the function.
Since the type ClientData is actually defined
to be , the function must first
cast the client data argument to an appropriate
type.
Evidently, casting is error-prone.
Another problem with command functions
as used in the Tcl C API is that
permanent data are possible only
in the form of client data,
global variables or static local
variables.
Both client data and global variables
are unsafe by being too visible
and static local data are simply inelegant.