subsubsection3_8_3_1.html
section3_8.html
subsubsection3_8_2_1.html
tableofcontents3_1.html
Next:
subsubsection3_8_3_1.html
Declaring Public Methods
Up:
section3_8.html
Tix Object Oriented
Previous:
subsubsection3_8_2_1.html
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, three 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
. Third, the first
command executed inside each method must be:
upvar #0 $w data
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. For example, if the user has issued the command
.up invert
on an instance
.up
of the class tixArrowButton, the method
tixArrowButton:invert
will be called and the argument
w
will have the value
.up
.
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. It turns out that in TCL,
the only clean way to access an array whose name is stored in a
variable is the ``
upvar #0 $wdata
'' technique: essentially
it 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.
Once the mysterious ``
upvar #0 $wdata
'' line is explained,
it becomes clear what the rest of the
tixArrowButton:invert
method does: it examines the current direction of the arrow, which
is stored in
$data(-direction)
and inverts it.
subsubsection3_8_3_1.html#SECTION00083100000000000000
Declaring Public Methods
http://tix.sourceforge.net
http://tix.sourceforge.net
