Next: Declaring Public Methods Up: Tix Object Oriented Previous: Using the tixWidgetClass


Writing Methods

After we have declared the new widget class, we can write methods for this class to define its behavior. Methods are just a special type of TCL procedures and they are created by the proc command. There are, however, two requirements for methods. First, their names must be prefixed by the command name of their class. Second, they must accept at least one argument and the first argument that they accept must be called w.

For example, the following is an implementation of the invert method for the class TixArrowButton:

proc tixArrowButton:invert {w} {
    upvar #0 $w data

    set curDirection $data(-direction)
    case $curDirection {
        n {
            set newDirection s
        s {
            set newDirection n
        # ....

Notice that the name of the method is prefixed by the command name of the class (tixArrowButton). Also, the first and only argument that it accepts is w and the first line it executes is upvar #0 $wdata.

The argument w specifies which widget instance this method should act upon. The invert method is used to invert the direction of the arrow. Therefore, it should examine the variable .up(-direction), which stores the current direction of the instance .up, and modify it appropriately. The upvar #0 $wdata tells the intepreter that the array data should be an alias for the global array whose name is stored in $w. We will soon see how the widget's methods use the data array.