The widget
class encapsulates the mechanism required to
wrap an Itcl object around a Tk widget. Itcl compound widgets
subclassed from the widget
class can be written more easily
than those written from scratch.
widget
class encapsulates the mechanism required to
wrap an Itcl object around a Tk widget. This mechanism involves these
basic steps:
widget
class, you
never have to worry about all of that!
The widget
class also provides methods for testing whether
the object's root widget still exists (this is useful for the code
invoked when public variables are configured) for setting the values of
public variables from the X resource database and for creating new
sub-widgets without having to worry about their unique names (this is
useful for popping up dialog boxes or creating icon-displays)
The widget
class doesn't provide any mechansism for
subclassing existing
Tk widgets. Although such Itcl widgets can be written, I didn't think there
was much point, since the WigWam library provides that already. Unlike the
WigWam library, the widget
superclass aims to allow the
programmer to encapsulate the behaviour of a new compound widget,
rather than only extend the behaviour of an existing widget.
This makes it easier to write Itcl widgets which can be reimplemented in
C/C++ without breaking any existing scripts which use them.
widget
class. These
methods are for use by classes derived from widget
.
widget::constructor
widgetType options...
widget::win
.
The constructor of a derived class should call the constructor of
the widget
class, passing it the Tcl command used to
create the widget to be wrapped and any options to be passed to that
command. For instance:
widget::destructor
widget
class destroys the
object's Tk widgets. This triggers the cleaning up of the hidden
commands created by the constructor.widget::exists
... public background {} { if [widget::exists] { $win configure -background $background ... } } ...
widget::option
varName optionName optionClass default
This is useful for associating public variables with X resources.
The public variables are initialised with the empty string and
given their values in the constructor using the Itcl
config
parameter specifier in the constructors arguments
and calls to widget::option
after the inital widget has
been created by calling widget::constructor
. For
instance:
itcl_class warnlabel { inherit widget public foreground {} { if [widget::exists] { $win configure -foreground $foreground } } public background {} { if [widget::exists] { $win configure -background $background } } constructor {config} { widget::constructor label -text "Danger!!!" widget::option foreground foreground Foreground black widget::option background background Background red $win configure -foreground $foreground -background $background } method configure {config} {} }
widget::subwidget
widgetType options...
There is only one instance variable defined by the widget
class:
widget::win