如果您只想通知插入、删除和替换(所有其他修改归结为),最简单的方法是拦截insert
,delete
和replace
方法。以下是使用 TclOO 包装器的方法:
oo::class create Text {
unexport destroy
constructor {w} {
rename $w [namespace current]::realwidget
bind $w <Destroy> [namespace code {my destroy}]
}
self method create {w args} {
rename [my new [::text $w {*}$args]] ::$w
return $w
}
method DoingModification args {
# Override this method to find out
}
method delete args {
my DoingModification {*}$args
tailcall realwidget delete {*}$args
}
method insert args {
my DoingModification {*}$args
tailcall realwidget insert {*}$args
}
method replace args {
my DoingModification {*}$args
tailcall realwidget replace {*}$args
}
}
# Everything else should just be forwarded; there's a lot of methods to do
# so we loop over them all...
foreach method {
bbox cget compare configure count debug dlineinfo dump edit get image
index mark peer scan search see tag window xview yview
} {
oo::define Text forward $method realwidget $method
}
之后,您可以制作一个小部件并很容易地找出修改,否则一切都会像平常一样工作:
set w [Text create .t]
oo::objdefine $w method DoingModification {method args} {
puts "Doing a $method on $args"
}
pack $w