Tracing Changes to Preference Variables
Suppose, for example, we want to repack the scrollbars when the user changes their scrollside setting from left to right. This is done by setting a trace on the win(scrollside) variable. When the user changes that via the user interface, the trace routine is called. The trace command and its associated procedure are shown in the next example. The variable must be declared global before setting up the trace, which is not otherwise required if Pref_Add is the only command using the variable.
Example 45-10 Tracing a Tcl variable in a preference item
Pref_Add {
{win(scrollside) scrollbarSide {CHOICE left right}
"Scrollbar placement"
"Scrollbars can be positioned on either the left or
right side of the text and canvas widgets."}
}
global win
set win(lastscrollside) $win(scrollside)
trace variable win(scrollside) w ScrollFixup
# Assume win(scrollbar) identifies the scrollbar widget
proc ScrollFixup { name1 name2 op } {
global win
if {$win(scrollside) != $win(lastscrollside)} {
set parent [lindex [pack info $win(scrollbar)] 1]
pack forget $win(scrollbar)
set firstchild [lindex [pack slaves $parent] 0]
pack $win(scrollbar) -in $parent -before $firstchild \
-side $win(scrollside) -fill y
set win(lastscrollside) $win(scrollside)
}
}
 |