4.6. Integrating SpecTcl in your experiment

Now let's look at how to wrap SpecTcl in a nice package. In this section we will describe:

Most serious experiments will be involved in one or more of these activities. The intent of this section is to introduce you to some of the things that are possible with SpecTcl. The reference section will contain a great deal more information about SpecTcl.

4.6.1. SpecTcl's startup sequence

SpecTcl's startup sequence allows you to tailor several aspects of the program. Each phase of the startup sources one or more Tcl scripts that allow you to tailor the behavior and appearance of SpecTcl. Startup has three phases:

  1. Pre-display initialization. The SpecTclInit.tcl files are located and read in. For most users, the main purpose of this phase is to set the value of the Tcl global variable DisplayMegabytes. This variable controls the size of the display shared memory region in Mbytes. (An MByte is 1024*1024 bytes).

  2. Tcl/Tk interpreter initialization. In this phase, the Tcl interpreter has been created, but not completely initialized. In this phase, SpecTclRC.tcl scripts are sourced. SpecTclRC.tcl scripts have available almost all of Tcl/Tk and therefore can be used to create a SpecTcl graphical user interface. Tcl/Tk's concept of stderr has not yet been established however so error output from these scripts will not appear. We recommend that you debug SpecTclRC.tcl scripts by first sourcing them into a completely initialized SpecTcl so that you can see error messages from your scripts as you debug them.

  3. Stdin redirection. Many users run SpecTcl with stdin redirected to a setup script. This script is run in the full Tcl/Tk initialized interpreter, so error messages will be visible on the SpecTcl TkCon console if that is used or on the terminal window from which SpecTcl was started if not.

Pre display initialization scripts are called SpecTclInit.tcl While these are Tcl scripts, they are run in a very early stage of the Tcl/Tk interpreter startup, and therefore should really only be used to set values of SpecTcl global Tcl variables. The following global variables may be of interest:

DisplayMegabytes

Number of Mbytes of shared display memory SpecTcl will create. This determines what Xamine can display (SpecTcl can create any number of spectra limited only by virtual memory. The sbind and unbind commands can be used to determine which are actually visible at any given time.

TKConsoleHistory

The number of lines of history that will be retained by tkcon.

TKConsoleBufferSize

Number of characters in the tkcon scroll back buffer.

In addition, SpecTcl itself creates a few global variables. The most important of which for startup scripts is SpecTclHome which is the path to the SpecTcl installation that is being run.

SpecTcl's scripts are cumulative. This means that there are several locations in which scripts are searched for and all matching scripts are run. Configuration scripts are searched for in order:

  1. A directory in the SpecTcl installation.

  2. The user's home directory

  3. The current working directory at the time SpecTcl is started

The idea of this cumulative initialization script is that for any given run of spectcl there may be system specific, user specific, and case specific initialization that needs to be done.

4.6.2. Writing a SpecTcl Startup scripts

In addition to setting up a common graphical user interface, users of SpecTcl often have a file that loads an initial set of definitions. These definitions may include:

4.6.3. Adding GUI elements to SpecTcl

When SpecTcl starts, several Tk toplevel widgets are displayed:

You can customize SpecTcl's appearance by adding Tk widgets to any of these windows, or you can create a new toplevel widget of your own and build a Gui within it. Consider the following script:

Example 4-13. Putting a button on all of SpecTcl's windows


proc openEventFile {} {
    set name [tk_getOpenFile -title {Choose an event file} \
		  -defaultextension .evt                    \
		  -filetypes {                             \
		      {{Event files} .evt             }    \
		      {{All Files}    *               }}]
    if {$name ne ""} {
	catch {stop}
	attach -file $name
	start
    }
}
proc fileButton top {
    return [button $top.readfile -text {Read Event File} -command openEventFile]

}

toplevel .newtop


foreach toplevel [list . .tkcon .gui .newtop] {
    set button [fileButton $toplevel]
    pack $button
}
                

This example builds a button for each of the SpecTcl GUI windows, and a new toplevel as well. The button, when clicked will graphically prompt for an event file that, when selected, will be analyzed by SpecTcl.