Precept 1: Tcl/Tk

Tcl is a simple scripting language.

Tk is a toolkit for the X window system: it contains a set of user I/O components, called widgets (another word for "a window with certain attributes", e.g. a button).

Tcl controls your program's response to user I/O events, for example what happens when the user clicks on a button, or moves the mouse. You can call C functions from Tcl.


Source files

The simplest program will consist of the following files:

When your program is started, the initialization function in tkAppInit.c calls your initialization function in myprogram_tcl.c. What you should do there is create Tcl commands, which are "linked" to C callback functions in myprogram_tcl.c.

For example:

Tcl_CreateCommand(interp, "myprog:init", initCB, NULL, NULL);

Now you can use the command myprog:init in your Tcl script, like this:

myprog:init;

This will call the function initCB, which looks like this:

static int
initCB(ClientData notUsed, Tcl_Interp *interp, int argc, char **argv) {
    myprog_Init();
    return TCL_OK;
}

As you can see, the only thing this function does is call another function, called myprog_Init(). This is because the standard Tcl callback functions have all those awkward parameters. The only extra code you will see in these functions, is code which converts parameters that Tcl passes to the callback to common C types (example follows in next precept).


Makefile, compiling

An example makefile:

LIBS = -ltkGLx -ltk4.0 -ltcl7.4 -lX11 -lGL

CFLAGS = -g -I/u/cs426/include
LDFLAGS = -L/u/cs426/lib

all: myprog_wish

OBJECTS=tkAppInit.o myprogram.o myprogram_tcl.o

myprog_wish:$(OBJECTS)
	$(CC) $(CFLAGS) -o $@ $(OBJECTS) $(LDFLAGS) $(LIBS)

An executable called myprog_wish is created: this is the interpreter for the Tcl script. This is a nice feature of Tcl: whenever you want to change something in your user interface, you just edit the script, and run your program straight away.


Examples

The following example programs are available in /u/cs426/Examples/Tcl+OpenGL/ex* (where * is 1...6):

Feel free to use the example Tcl/Tk code in your first assignment. If you do, please say so in your source or in a README file.


Patrick Min, CS Department, Princeton University
Last modified: Mon Sep 23 11:34:13 1996