6 Tix Object Oriented Programming
This chapter is intended for experienced programmers who want to
create new Tix widgets. If you just want use the Tix widgets in your
applications, you can skip this chapter.
6.1 Introduction to Tix Object Oriented Programming
Tix Intrinsics
[incr Tcl]
6.1.1 Widget Classes and Widget Instances
widget classes
oop.tex.html#6-1
6-1
(Figure 6-1) Arrow Buttons
widget
instances
tixArrowButton .up    -direction n
tixArrowButton .left  -direction e
tixArrowButton .right -direction w
tixArrowButton .down  -direction s
6.1.2 What is in a Widget Instance
Variables
configure
methods
-
.up
.up:
-direction
.up(-direction)
count
.up(count)
.down
.down(-direction)
.down(count)
Methods
methods
Public methods
invoke
invert
.up
.up invert
.up invoke
Private methods
Component Widgets
root
widget
subwidgets
.up
.up
.up
.up.button
subwidget
intro.tex.html#1.3.1
1.3.1
6.2 Widget Class Declaration
6.2.1 Using the tixWidgetClass Command
tixWidgetClass
tixWidgetClass classCommandName {
-switch value
-switch value
....
}
tixWidgetClass tixArrowButton {
-classname  TixArrowButton
-superclass tixPrimitive
-method {
flash invoke invert
}
-flag {
-direction -state
}
-configspec {
{-direction direction Direction e}
{-state state State normal}
}
-alias {
{-dir -direction}
}
-default {
{*Button.anchor         c}
{*Button.padX           5}
}
}
(Figure 6-2) declaration of the TixArrowButton Class
tixWidgetClass
command
name
tixArrowButton
tixArrowButton .arrow
.arrow
Foo
Bar
tixArrowButton:Foo
tixArrowButton:Bar
class name
TixArrowButton
-classname
up
-direction
normal
-state
option add *TixArrowButton.direction up
option add *TixArrowButton.state     normal
tixArrowButton
TixArrowButton
-superclass
tixPrimitive
6.3 Writing Methods
proc
w
upvar #0 $w data
proc tixArrowButton:invert {w} {
upvar #0 $w data
set curDirection $data(-direction)
case $curDirection {
n {
set newDirection s
}
s {
set newDirection n
}
# ....
}
}
tixArrowButton
w
upvar #0 $w data
w
.up invert
.up
tixArrowButton:invert
w
.up
invert
.up(-direction)
.up
upvar #0 $w data
$w
upvar #0 $w data
tixArrowButton:invert
$data(-direction)
6.3.1 Declaring Public Methods
-method
flash
invert
invoke
6.4 Standard Initialization Methods
InitWidgetRec
ConstructWidget
SetBindings
6.4.1 The InitWidgetRec Method
InitWidgetRec
tixArrowButton:InitWidgetRec
count
proc tixArrowButton:InitWidgetRec {w} {
upvar #0 $w data
set data(count) 0
}
data
Chaining Methods
is-a
is a
tixPrimitive:InitWidgetRec
chaining
proc tixArrowButton:InitWidgetRec {w} {
upvar #0 $w data
tixPrimitive:InitWidgetRec $w
set data(count) 0
}
tixPrimitive:InitWidgetRec
The tixChainMethod call
tixArrowButton:InitWidgetRec
tixWidgetClass tixArrowWidget {
-superclass tixPrimitive
...
}
tixWidgetClass tixArrowButton {
-superclass tixArrowWidget
...
}
tixPrimitive:SomeMethod
tixArrowWidget:SomeMethod
tixChainMethod
tixArrowButton:InitWidgetRec
tixChainMethod
tixPrimitive:InitWidgetRec
proc tixArrowButton:InitWidgetRec {w} {
upvar #0 $w data
tixChainMethod $w InitWidgetRec
set data(count) 0
}
$w
InitWidgetRec
proc tixPrimitive:MethodToChain {w arg1 arg2 ... argn} {
...
}
tixChainMethod $w MethodToChain $arg1 $arg2 ... $argn
tixChainMethod
tixChainMethod
InitWidgetRec
ConstructWidget
SetBindings
6.4.2 The ConstructWidget Method
ConstructWidget
button
up.xbm
down.xbm
left.xbm
right.xbm
@$data(-direction).xbm
proc tixArrowButton:ConstructWidget {w} {
upvar #0 $w data
tixChainMethod $w ConstructWidget
set data(w:button) [button $w.button -bitmap @$data(-direction).xbm]
pack $data(w:button) -expand yes -fill both
}
tixArrowButton:ConstructWidget
data(w:button)
button
swid
data(w:
swid
)
6.4.3 The SetBindings Method
SetBindings
tixArrowButton:IncrCount
proc tixArrowButton:SetBindings {w} {
upvar #0 $w data
tixChainMethod $w SetBindings
bind $data(w:button) <1> "tixArrowButton:IncrCount $w"
}
proc tixArrowButton:IncrCount {w} {
upvar #0 $w data
incr data(count)
}
6.5 Declaring and Using Variables
InitWidgetRec
ConstructWidget
tixArrowButton:InitWidgetRec
data(count)
data(w:button)
tixArrowButton:ConstructWidget
tixArrowButton:SetBindings
-flag
: As shown in the class declaration in figure
oop.tex.html#6-2
6-2
, the
-flag
argument declares all the public
variables of the TixArrowButton class,
-direction
and
-state
-configspec
: We can use the
-configspec
argument to
specify the details of each public variable. For example, the
following declaration
-configspec {
{-direction direction Direction e}
{-state state State normal}
}
specifies that the
-direction
variable has the resource
name
direction
and resource class
Direction
; its default
value is
e
. The application programmer can assign value to
this variable by using the
-direction
option in the command
line or by specifying resources in the Tk option database with its
resource name or class. The declaration of
-state
installs
similar definitions for that variable.
-alias
: The
-alias
argument is used to specify
alternative names for public variables. In our example, the setting
-alias {
{-dir -direction}
}
specifies that
-dir
is the same variable as
-direction
. Therefore, when the application issue the command
.up config -dir w
it is the same as issuing
.up config -direction w
The
-alias
option provides only an alternative name for
the application programmer. Inside the widget's implementation code,
the variable is still accessed as
data(-direction)
,
not
data(-dir)
.
6.5.1 Initialization of Public Variables
InitWidgetRec
InitWidgetRec
Step 1: If the value of the variable is specified by the
creation command, this value is used. For example, if the
application programmer has created an instance in the following way:
tixArrowButton .arr -direction n
The value
n
will be used for the -direction variable.
Step 2: if step 1 fails but the value of the variable is
specified in the options database, that value is used. For example,
if the user has created an instance in the following way:
option add *TixArrowButton.direction w
tixArrowButton .arr
The value
w
will be used for the
-direction
variable.
Step3: if step 2 also fails, the default value specified in
the
-configspec
secton of the class declaration will be used.
Type Checker
type ckecker procedure
-configspec
CheckDirection
-direction
-configspec {
{-direction direction Direction e CheckDirection}
{-state state State normal}
}
...
}
proc CheckDirection {dir} {
if {[lsearch {n s w e} $dir] != -1} {
return $dir
} else {
error "wrong direction value \"$dir\""
}
-state
6.5.2 Public Variable Configuration Methods
-direction
.arr
n
.arr configure -direction n
:config
-direction
tixArrowButton:config-direction
proc tixArrowButton:config-direction {w value} {
upvar #0 $w data
$data(w:button) config -bitmap @$value.xbm
}
tixArrowButton:config-direction
value
-direction
data(-direction)
old
value
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)
}
n
s
Explicitly set the instance variable inside the configuration
method (the
set data(-direction) $value
line).
Return the modified value from the configuration method.
Configuration Methods and Public Variable Initialization
-forcecall
-direction
-forcecall {
-direction
}
6.6 Summary of Widget Instance Initialization
When the user creates an instance, the public variables are
intilized as discussed in section
oop.tex.html#6.5.1
6.5.1
. Type checkers
are always called if they are specified. Configuration methods are
called only if they are specified in the
-forcecall
section.
The
InitWidgetRec
method is called. It should initialize
private variable, possibly according to the values the public
variables.
The
ConstructWidget
method is called. It should create the
component widgets. It should also store the names of public
subwidgets into the subwidget variables.
The
SetBinding
method is called. It should create bindings for
the component widgets.
6.7 Loading the New Classes
tclIndex
/usr/my/tix/classes
tclIndex
tools/tixindex
cd /usr/my/tix/classes
/usr/my/Tix4.0/tools/tixindex *.tcl
tclIndex
tixindex
auto_mkindex
tclIndex
#!/usr/local/bin/tixwish
lappend auto_path /usr/my/tix/classes
# Now I can use my TixArrowButton class!
#
tixArrowButton .arr -direction n
pack .arr
