![]() |
Houdini Development Toolkit - Version 6.5Side Effects Software Inc. 2004 |
This example shows how to embed Tcl/Tk into Houdini. Tcl/Tk (pronounced "tickle-TK") is a unix scripting language / GUI builder originally written by John K. Ousterhout at the University of California at Berkeley. Note that Tcl/Tk is already embedded into Houdini. This case study is provided to show a real-world example of extending the command library.
In order to embed Tcl/Tk, two new Houdini commands need to be added: tcl and tk. The tcl command will cause hscript to start a Tcl interpreter. Similarly, the tk command will cause Houdini to start a Tcl interpreter and open a Tk widget window for UI gadgets.
The Tcl/Tk language also needs to be extended with two new commands: hscript and exit. The Tcl/Tk hscript will run the remainder of its command line (i.e. it's argument list) as a Houdini hscript command. The Tcl/Tk exit command will quit the Tcl interpreter, bringing back the hscript interpreter prompt.
The example source code is fairly lengthy and has been placed on a separate page. It may be easiest to open a separate browser window so the source code can be referred to while reading this section.
The Adding a New Command section discusses how to add new commands to Houdini. To add the tcl and tk commands, the same procedure is followed.
Within the callbacks for the tcl and tk commands, Tcl_Main and Tk_Main are respectively called which runs the Tcl/Tk command interpreter loop. The command interpreter loop will return when Tcl/Tk exits. At this point, control is returned to the callback, which in turn exits to hscript.
The Tcl_Main and Tk_Main functions each take an initialization function as one of its inputs. In this case Tcl_AppInit is used. This function is used to add the two extension commands hscript and exit to Tcl/Tk. This is accomplished using the Tcl/Tk C API function Tcl_CreateCommand (see the Tcl/Tk documentation for more details).
The Tcl/Tk library requires some extra work that is not shown in this example. When Tcl/Tk runs into certain errors, it will call exit() within its C code. With the embedded Tcl/Tk, this is undesirable because it causes hscript to exit without giving the user a chance to save his work.
Fortunately, the C source code for the Tcl/Tk language and interpreter is shipped with its distribution. All of the C calls to exit need to be replaced with return statements so that Tcl_Main or Tk_Main will return properly.
To compile Tcl/Tk and the C++ code into a shared library, use the hcustom command. First compile Tcl/Tk (modified so that the exits are replaced with returns) to produce the static library files libtcl.a and libtk.a. Move these files into the same directory as the C++ code which glues Houdini and Tcl/Tk together. You will need to link the Tcl/Tk libraries along with the X11 and Xt libraries into a shared library. Use the -L and -l options in hcustom to do this.
Assuming that the C++ code is in a file called tcl_tk_glue.C, the hcustomcommand line would be as follows:
hcustom -L. -ltk -ltcl -lX11 -lXt tcl_tk_glue.C