How to write a complex item class

The Slate tutorial ran quickly through creation of a class to implement a new complex item on the Slate. This page list the steps you need to take in more detail. (Of course, the best way is to make a copy of an existing class that is close to what you need, but this page should help you make sure that you don't miss anything.)

Inheritance
The class must inherit from ::tycho::Picture or one of its subclasses.

Methods
Every "method" is implemented as a public procedure. The first three arguments must always be the item ID, the canvas name, and the slate name.

Constructor and destructor
The class must contain a procedure called construct with the above three arguments, followed by a list of tags to be attached to components, followed by two or more coordinates, followed by option-value pairs. Unless the class has no options, it must start with this code:
foreach {option value} [concat [array get optiondefault] $args] {
	set _[string trimleft $option -]($id) $value
}
The construction procedure does not automatically call its superclass "constructors": if your class needs that, you have to do it yourself. The destructor is optional, and is needed only if the class needs to do something other than destroy its components.

Virtual method table
Picture classes simulate inheritance explicitly using an array as a "virtual method table." Every class must contain the declaration
common methodtable
(Do not change the name!) The class must (in the class body) initialize the table with the contents of the table in its superclass; assuming that the superclass is Picture, the class body then contains:
array set methodtable [array get ::tycho::Picture::methodtable]
The class must then "override" superclass "methods" with its own. The construct method is compulsory; others are optional. Suppose your class overrides coords (which many classes do), your class body contains:
set methodtable(construct)   ::tycho::Bar::construct
set methodtable(coords)      ::tycho::Bar::coords
where "Bar" is the name of your class. If your class adds options, you must also add the option update procedures (see below).

Instance variables
Every "instance variable" is implemented by a common array, indexed by item ID.

Option declarations
For each option, say -foo, the class must contain:

The common variable must be an array, indexed by item ID, containing the value of each item's -foo. The procedure must take, in addition to the usual three arguments, one argument that it assigns to _foo($id); it can then perform any actions that would normally (in a regular [incr Tcl] object) be done by the configuration body of a public variable or itk_option.

Option defaults
The class body must contain the declaration:
common optiondefault
Each option defined in this class must have its default value added to this array:
set optiondefault(-foo) bloo

Option update
The class body must contain the declaration:
array set optiondefault [array get ::tycho::Picture::optiondefault]

Each option must have its update procedure added to the "virtual method table":

set methodtable(_foo) ::tycho::Bar::_foo

Item shape
The class body must contain the declaration:
common shape rectangle
where instead of rectangle you could have oval, point, line, or polygon.
Um, OK, other than that, you can do whatever you want...

Back up
Tycho Home Page


Copyright © 1996, The Regents of the University of California. All rights reserved.
Last updated: 96/12/11, comments to: johnr@eecs.berkeley.edu