Chapter 16. Building an Extended WISH

Table of Contents
Introduction to Tcl
Introduction to Tk
Introduction to [incr Tcl]
PLplot Extensions to Tcl
Custom Extensions to Tcl

Beginning with PLplot 5.0, a new and powerful paradigm for interaction with PLplot is introduced. This new paradigm consists of an integration of PLplot with a powerful scripting language (Tcl), and extensions to that language to support X Windows interface development (Tk) and object oriented programming ([incr Tcl]). Taken together, these four software systems (Tcl/Tk/itcl/PLplot) comprise a powerful environment for the rapid prototyping and development of sophisticated, flexible, X Windows applications with access to the PLplot API. Yet that is only the beginning—Tcl was born to be extended. The true power of this paradigm is achieved when you add your own, powerful, application specific extensions to the above quartet, thus creating an environment for the development of wholly new applications with only a few keystrokes of shell programming ...

Introduction to Tcl

The Tool Command Language, or just Tcl (pronounced "tickle") is an embeddable script language which can be used to control a wide variety of applications. Designed by John Ousterhout of UC Berkeley, Tcl is freely available under the standard Berkeley copyright. Tcl and Tk (described below) are extensively documented in a new book published by Addison Wesley, entitled "Tcl and the Tk toolkit" by John Ousterhout. This book is a must have for those interested in developing powerful extensible applications with high quality X Windows user interfaces. The discussion in this chapter cannot hope to approach the level of introduction provided by that book. Rather we will concentrate on trying to convey some of the excitement, and show the nuts and bolts of using Tcl and some extensions to provide a powerful and flexible interface to the PLplot library within your application.

Motivation for Tcl

The central observation which led Ousterhout to create Tcl was the realization that many applications require the use of some sort of a special purpose, application specific, embedded "macro language". Application programmers cobble these "tiny languages" into their codes in order to provide flexibility and some modicum of high level control. But the end result is frequently a quirky and fragile language. And each application has a different "tiny language" associated with it. The idea behind Tcl, then, was to create a single "core language" which could be easily embedded into a wide variety of applications. Further, it should be easily extensible so that individual applications can easily provide application specific capabilities available in the macro language itself, while still providing a robust, uniform syntax across a variety of applications. To say that Tcl satisfies these requirements would be a spectacular understatement.

Capabilities of Tcl

The mechanics of using Tcl are very straightforward. Basically you just have to include the file tcl.h, issue some API calls to create a Tcl interpreter, and then evaluate a script file or perform other operations supported by the Tcl API. Then just link against libtcl.a and off you go.

Having done this, you have essentially created a shell. That is, your program can now execute shell scripts in the Tcl language. Tcl provides support for basic control flow, variable substitution file i/o and subroutines. In addition to the builtin Tcl commands, you can define your own subroutines as Tcl procedures which effectively become new keywords.

But the real power of this approach is to add new commands to the interpreter which are realized by compiled C code in your application. Tcl provides a straightforward API call which allows you to register a function in your code to be called whenever the interpreter comes across a specific keyword of your choosing in the shell scripts it executes.

This facility allows you with tremendous ease, to endow your application with a powerful, robust and full featured macro language, trivially extend that macro language with new keywords which trigger execution of compiled application specific commands, and thereby raise the level of interaction with your code to one of essentially shell programming via script editing.

Acquiring Tcl

There are several important sources of info and code for Tcl. Definitely get the book mentioned above. The Tcl and Tk toolkits are distributed by anonymous ftp at There are several files in there corresponding to Tcl, Tk, and various forms of documentation. At the time of this writing, the current versions of Tcl and Tk are 7.3 and 3.6 respectively. Retrieve those files, and install using the instructions provided therein.

The other major anonymous ftp site for Tcl is Harbor contains a mirror of sprite as well as innumerable extensions, Tcl/Tk packages, tutorials, documentation, etc. The level of excitement in the Tcl community is extraordinarily high, and this is reflected by the great plethora of available, high quality, packages and extensions available for use with Tcl and Tk. Explore—there is definitely something for everyone.

Additionally there is a newsgroup, comp.lang.tcl which is well read, and an excellent place for people to get oriented, find help, etc. Highly recommended.

In any event, in order to use the Tk driver in PLplot, you will need Tcl-8.2 and Tk-8.2 (or higher versions). Additionally, in order to use the extended WISH paradigm (described below) you will need iTcl-3.1 (or a higher version).

However, you will quite likely find Tcl/Tk to be very addictive, and the great plethora of add-ons available at harbor will undoubtedly attract no small amount of your attention. It has been our experience that all of these extensions fit together very well. You will find that there are large sectors of the Tcl user community which create so-called "MegaWishes" which combine many of the available extensions into a single, heavily embellished, shell interpreter. The benefits of this approach will become apparent as you gain experience with Tcl and Tk.