Tcl utilities available to the programmer

A number of Tcl global variables and procedures that will be useful to the Tcl programmer have been incorporated into MLDesigner. Any of these can be used in any Tcl script associated with an instance of the TclScript primitive.

For instance, in Example 1, the global variable ptkControlPanel specifies the control panel that is used to run the system. Below is a list of the useful global variables that have been set by the MLDesigner graphical interface when the Tcl script is sourced or when the goTcl_$starID procedure is invoked.

$ptkControlPanel

A string giving the name of the control panel window associated with a given run. This variable is set by MLDesigner.

$ptkControlPanel.high

The uppermost panel in the control panel that is intended for user-defined entries.

$ptkControlPanel.middle

The middle panel in the control panel that is intended for user-defined entries.

$ptkControlPanel.low

The lowest panel in the control panel that is intended for user-defined entries.

In addition to these global variables, a number of procedures have been supplied. Using these procedures can ensure a consistent look-and-feel across a variety of MLDesigner applications. The complete set of procedures can be found in $MLD/lib/tcl. Only the most useful commands are listed here. Note also that the entire set of commands defined in the Tcl-based textual interpreter for MLDesigner, PTcl, are also available. So for example, the command cursystem will return the name of the current system (see Modeling→Modeling using PTcl for an overview of available commands).

ptkExpandEnvVar

 

Procedure to expand a string that begins with an environment variable reference. For example, ptkExpandEnvVar $MLD/src will return something like /opt/MLDesign-Technologies/MLDesigner/src (depending on the installation of MLDesigner).

ptkImportantMessage

 

Procedure to put a warning message into the log window.

 

win

obsolete argument

 

text

text of the warning message

ptkMakeButton

 

Procedure to make a pushbutton in a window. A callback procedure must be defined by the programmer. It will be called whenever the user pushes the button, and takes no arguments.

 

win

name of window to contain the button

 

name

name to use for the button itself

 

desc

description to be put into the display

 

callback

name of callback procedure to register changes

ptkMakeEntry

 

Procedure to make a text entry box in a window. A callback procedure must be defined by the programmer. It will be called whenever the user changes the value in the entry box and presses <Return>. Its single argument will be the new value of the entry.

 

win

name of window to contain the entry box

 

name

name to use for the entry box itself

 

desc

description to be put into the display

 

default

the initial value of the entry

 

callback

name of callback procedure to register changes

ptkMakeMeter

 

Procedure to make a bar-type meter in a window.

 

win

name of window to contain the meter

 

name

name to use for the meter itself

 

desc

description to be put into the display

 

low

the value of the low end of the scale

 

high

the value of the high end of the scale

ptkSetMeter

 

Procedure to set the value of a bar-type meter created with ptkMakeMeter.

 

win

name of window to contain the entry box

 

name

name to use for the entry box itself

 

value

the new value to display in the meter

ptkMakeScale

 

Procedure to make a sliding scale. All scales in the control panel range from 0 to 100. A callback procedure must be defined by the programmer. It will be called whenever the user moves the control on the scale. Its single argument will be the new position of the control, between 0 and 100.

 

win

name of window to contain the scale

 

name

name to use for the scale itself

 

desc

description to be put into the display

 

position

initial integer position between 0 and 100

 

callback

name of callback procedure to register changes

A widget is created with name $win.$name.value that should be used by the programmer to display the current value of the slider. Thus, the callback procedure should contain a command like: $win.$name.value configure -text $new_value to display the new value after the slider has been moved. This is not performed automatically because the fixed range from 0 to 100 may be correct from the user's perspective. So, for example, if the programmer divides the scale value by 100 before displaying it, it will appear to the user as if the scale ranges from 0.0 to 1.0. It is also possible to control the position of the slider from Tcl (overriding the user actions) using a command like $win.$name.scale set $position, where position is an number in the range of 0 to 100.

Example 3

The following Tcl script can be used with the TclScript primitive in the system configuration given in Example 1.

ptkMakeMeter $ptkControlPanel.high meter_$starID "meter tracking scale" 0 100

proc scale_update_$starID {new_value} \
    "ptkSetMeter $ptkControlPanel.high meter_$starID \$new_value
    $ptkControlPanel.high.scale_$starID.value configure -text \$new_value"
ptkMakeScale $ptkControlPanel.high scale_$starID "my scale" 50 scale_update_$starID

proc button_update {} {ptkImportantMessage .msg "Hello"}
ptkMakeButton $ptkControlPanel.middle button_$starID "my button" button_update

proc entry_update_$starID {new_value} \
    "setOutputs_$starID \$new_value"
ptkMakeEntry $ptkControlPanel.low entry_$starID "my entry" 10 entry_update_$starID

It will create the following control panel. The commands are explained individually below.

Simulation control window for the TclScript demo

1
ptkMakeMeter $ptkControlPanel.high meter_$starID "meter tracking scale" 0 100

calls the method ptkMakeMeter to create a meter display with the label "meter tracking scale" in the upper part of the control panel with range from 0 to 100. The next script lines

3
proc scale_update_$starID {new_value} \
4
"ptkSetMeter $ptkControlPanel.high meter_$starID \$new_value
5
$ptkControlPanel.high.scale_$starID.value configure -text \$new_value"

define the callback function to be used for the slider (scale) shown below the meter. The callback function sets the meter and updates the numeric display to the left of the slider. Notice that the body of the procedure is enclosed in quotation marks rather than the usual braces. This ensures that the variables ptkControlPanel and starID will be evaluated at the time the procedure is defined, rather than at the time it is invoked. To make sure that new_value is not evaluated until the procedure is invoked, a preceding backslash is being used, as in \$new_value. The ptkControlPanel and starID values could have been alternatively passed as arguments.

6
ptkMakeScale $ptkControlPanel.high scale_$starID "my scale" 50 scale_update_$starID

This creates the slider itself, and sets its initial value to 50, half of full scale.

8
proc button_update {} {ptkImportantMessage .msg "Hello"}
9
ptkMakeButton $ptkControlPanel.middle button_$starID "my button" button_update

This defines a callback function, creates a button labeled "my button" and connects the button to the callback function. The callback function writes the message "Hello" to the Log window.

11
proc entry_update_$starID {new_value} \
12
"setOutputs_$starID \$new_value"
13
ptkMakeEntry $ptkControlPanel.low entry_$starID "my entry" 10 entry_update_$starID

This defines a callback function, creates the entry box with initial value "10" and connects the entry box to the callback function. Again notice that the procedure body is enclosed in quotation marks.