Defining a new composite widget class
package Whatever; @ISA = qw(Tk::Frame); # or Tk::Toplevel Tk::Widget->Construct('Whatever'); sub Poplulate { my ($cw,$args) = @_; my $flag = delete $args->{-flag}; if (defined $flag) { # handle -flag => xxx which can only be done at create time # the delete above ensures that new() does not try and # do $cw->configure(-flag => xxx) } $w = $cw->Component(...); $cw->Delegates(...); $cw->ConfigSpecs('-cursor' => [SELF,cursor,Cursor,undef], '-something' => [METHOD,dbName,dbClass,'default'], '-text' => [$label,dbName,dbClass,'default'], '-heading' => [Tk::Config->new($head,-text),heading,Heading,'My Heading'] ); } sub something { my ($cw,$value) = @_; if (@_ > 1) { # set it } return # current value } 1; __END__
A composite should normaly inherit new() from Tk::Widget. Tk::Widget::new() will call $cw-InitObject(\%args)> which a compoiste will normally inherit from Tk::Frame.
new()
Tk::Widget
Tk::Widget::new()
$cw-
Tk::Frame
Tk::Frame::InitObject() will call Populate(), which should be defined to create the characteristic subwidgets of the class.
Tk::Frame::InitObject()
Populate()
Populate may call Delegates to direct calls to methods to subwidgets. Typically most of not all methods are directed to a single subwidget - e.g. ScrolledListbox directs all methods to the core Listbox so that $composite->get(...) calls $listbox->get(...).
Populate
Delegates
Populate should also call ConfigSpecs() once Populate returns Tk::Frame::ConfigDefault walks through the ConfigSpecs entries and populates %$args hash with defaults for options from .Xdefaults etc. When InitObject() returns to Tk::Widget::new(), a call to $cw->configure(%$args) and sets *all* the options.
ConfigSpecs()
Tk::Frame::ConfigDefault
InitObject()
To install Tk, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Tk
CPAN shell
perl -MCPAN -e shell install Tk
For more information on module installation, please visit the detailed CPAN module installation guide.