next up previous contents index
Next: Graphical Interface, Commands and Up: Xic Configuration and Startup Previous: Python Support   Contents   Index


Tcl/Tk Support

This interface is presently not available under Microsoft Windows.

Xic provides a plug-in interface to Tcl/Tk. Tcl (Tool control language) is a popular open source scripting language, and Tk is a graphical package addition. The language syntax is provided in documentation supplied with Tcl/Tk, and is described in several books.

Since this capability is dynamically loaded, Xic can use this capability if it has been installed, but does not require the installation. Support is provided for Tcl, with and without Tk.

If Tcl/Tk have been installed via a standard distribution file on the system, which is common for Linux, the plug-in should be able to locate the shared libraries automatically. If the installation is non-standard, the user may need to inform the system dynamic linker of the shared library location. This is generally accomplished by setting the LD_LIBRARY_PATH variable in the environment, before running Xic. This would normally be done in the user's shell startup file.

There are two text-mode commands that can be used to run a Tcl/Tk script.

!tcl
This command will exist only if the Tcl language support plug-in is loaded, which will occur on program startup if the Tcl shared libraries are found. The script should contain only Tcl commands, not Tk.

!tk
This command will exist only if the Tcl and Tk language support plug-in is loaded, which will occur on program startup if both Tcl and Tk shared libraries are found. The script may contain any combination of Tcl and Tk commands.

In either case, the first argument is a path to a file containing the script body. Additional arguments are taken as arguments to the script. The script will be executed as if by the wish shell supplied with Tcl/Tk.

The startup file, which can be used to set defaults, is named ``.xic-wishrc'' in the user's home directory. The contents is analogous to the .wishrc file normally used with Tcl/Tk. The user must create this file if needed.

All of the Xic script functions are exported to Tcl/Tk and can be called by name from a Tcl/Tl script. However, only the basic data types are supported. There is also a function named ``xic'' which can be used in the following manner:

xic function arguments...

The function xic is a Tcl function which loads the interface function or user-defined function given in the first argument (a string). User defined functions can be accessed if they are already known to Xic, i.e., they were defined in a library file or were defined in a previously-run Xic script. The arguments to the function follow, and should match the arguments expected by the function. This form must be used when executing a user-defined function.

The variable type of an argument is inferred as follows:

To explicitly coerce a numeric token into a string, backslash escaped double quotes should be used to delimit the token. For example, \ "1.234 \ " is taken as a string. The backslash prevents tcl from removing the double quotes before passing the token.

Arrays passed to interface functions must use ``0'', ``1'', etc. as indices, and are ordered accordingly (in tcl, array indices can be any text token and have no natural order). The ``0'' element (at least) must be set before the array can be passed to a function. If the array is dynamically expanded, new tcl elements will be created. The initial size of the array is implied by the largest contiguous index assigned. Thus, for example, if the interface function requires an array of size 4, the following tcl code could be used

set array(0) 0
set array(1) 0
set array(2) 0
set array(3) 0
xic Function &array()

When the function returns, the array values will be updated. Only one-dimensional arrays are available.

There is an additional special tcl function which has been added.

xwin win_name
This function returns the X window id of the tk window given as a widget path in win_name. This is used to obtain the window id of a tk window to be used for Xic graphics through the GRopen interface. A suggested way to use a tk window for exported drawing from Xic is given in the example below. The xwin procedure is used to obtain the window id. This window should be configured with `-background ""' which allows redraws to be handled through a procedure bound to the window with the bind command which responds to expose events. Otherwise, expose events will cause the window to be redrawn in gray after the event handler is called. A pixmap is used to store the image for redraws.

Example

# This is the window used for drawing by Xic.
# Note the '-background ""' directive.  This
# is necessary for proper redrawing after expose
# events.
frame .f -width 8c -height 8c -background ""
pack .f

set win_id [xwin .f]
set ghandle [xic GRopen ":0" $win_id]
# The win_id is the X id of the drawing window,
# the ghandle is the handle value returned from
# Xic upon opening graphics on this window.

set size(0) 0
set size(1) 0
set size(2) 0
set size(3) 0
xic GetWindowView 0 &size()
# The size array contains the displayed area of the
# cell in the main Xic window, in order L, B, R, T

xic GRdraw $ghandle $size(0) $size(1) $size(2) $size(3)
# This draws the Xic view into the Tk window

xic GRupdate $ghandle
# Due to the way Tk (and X) works, unless GRupdate is
# called after drawing, the drawing won't be visible.
# The operations are stuck in a cache somewhere waiting.
# GRupdate flushes the operations.

set dsize(0) 0
set dsize(1) 0
xic GRgetDrawableSize $ghandle $win_id &dsize()
# The dsize array contains the size in pixels of the
# Tk drawing area.

set pixm [xic GRcreatePixmap $ghandle $dsize(0) $dsize(1)]
xic GRcopyDrawable $ghandle $pixm $win_id 0 0 $dsize(0) $dsize(1) 0 0
xic GRupdate $ghandle
# We have created a pixmap of the same size and depth as
# the drawing area, and copied the drawing area into it.
# This will be used to redraw the drawing area after an
# expose event.

bind .f <Expose> {
    # This sets up a handler for expose events.  Expose
    # events are received when a previously obscured part
    # of the window is uncovered.  The pixmap is copied
    # into the Tk window.
    xic GRcopyDrawable $ghandle $win_id $pixm 0 0 $dsize(0) $dsize(1) 0 0
    xic GRupdate $ghandle
}

The TextCmd script function can be used to launch a tcl/tk script. At present, tcl/tk scripts are not recognized in the script path, but one can use a native language wrapper to include tck/tk scripts in the User Menu.

The following native script functions can also be used to run Tcl/Tk scripts, or perform other related manipulations related to the Tcl/Tk interpreter.

RunTcl Run a Tcl or Tk script.
ResetTcl Reset the Tcl/Tl interpreter.


next up previous contents index
Next: Graphical Interface, Commands and Up: Xic Configuration and Startup Previous: Python Support   Contents   Index
Stephen R. Whiteley 2024-09-29