Next: Configuration Methods and Up: Declaring and Using Previous: Type Checker

 

Public Variable Configuration Methods 

After a widget instance is created, the user can assign new values to the public variables using the configure method. For example, the following code changes the -direction variable of the .arr instance to n.

.arr configure -direction n

In order for configuration to work, you have to define a configuration method that does what the programmer expects. The configuration method of a public variable is invoked whenever the user calls the configure method to change the value of this variable. The name of a configuration method must be the name of the public variable prefixed by the creation command of the class and :config. For example, the name configuration method for the -direction variable of the TixArrowButton class is tixArrowButton:config-direction. The following code implements this method:

proc tixArrowButton:config-direction {w value} {
upvar #0 $w data

$data(w:button) config -bitmap @$value.xbm
}

Notice that when tixArrowButton:config-direction is called, the value parameter contains the new value of the -direction variable but data(-direction) contains the old value. This is useful when the configuration method needs to check the previous value of the variable before taking in the new value.

If a type checker is defined for a variable, it will be called before the configuration method is called. Therefore, the configuration method can assume that the type of the value parameter is got is always correct.

Sometimes it is necessary to override the value supplied by the user. The following code illustrates this idea:

proc tixArrowButton:config-direction {w value} {
upvar #0 $w data

if {$value == "n"} {
set value s
set data(-direction) $value
}

$data(w:button) config -bitmap @$value.xbm
return $data(-direction)
}

Notice the above code always overrides values of n to s. If you need to override the value, you must do the following two things:

If you do not need to override the value, you don't need to return anything from the configuration method. In this case, the Tix Intrinsics will assign the new value to the instance variable for you.


http://tix.sourceforge.net