The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Tk::AppWindow::CookBook::Plugin - Write your own plugin

OTHER RECIPIES

Tk::AppWindow::CookBook::Extension
Tk::AppWindow::CookBook::ContentManager

AN ABSTRACT PLUGIN

 package My::NameSpace::Plugins::Abstract;
 
 use strict;
 use warnings;
 
 use base qw( Tk::AppWindow::BaseClasses::Plugin );
 #or
 #use base qw( Tk::AppWindow::BaseClasses::PluginJobs );
 use Tk::MyExtraOrdinaryWidget;
 
 #The description section of your pod is shown in the plugins dialog.
 
 =head1 DESCRIPTION
 
 This is an abstract plugin

 =cut

 sub new {
    my $class = shift;
    my $self = $class->SUPER::new(@_, 'RequiredExtension');
    return undef unless defined $self;
    
    #Create your Tk stuff;
    $self->{'widg'} = $self->ExtraOrdinaryWidget->pack;
    
    #hook to a command
    $self->cmdHookBefore('command1', 'Hook1', $self);
    $self->cmdHookAfter('command1', 'Hook2', $self);

    #hook to a config variable
    $self->configHookBefore('-configvariable', 'Hook3', $self);
    $self->configHookAfter('-configvariable', 'Hook4', $self);
 
    return $self;
 }
 
 sub Hook1 {
        my $self = shift;
        print "before command1\n"
        
        #sometimes you want to keep the parameters for use in Hook2
        $self->{'safe_param'} = [@_];
        
        #when there is no parameter but you know it concerns a document
        $self->{'safe_param'} = [$self->extGet('CoditMDI)->docSelected];
        
   #use passed on parameters
   my ($first, $second) = @_;
 
   #do your thing
   
        return @_
 }
 
 sub Hook2 {
        my $self = shift;
        print "after command1\n"
   
   #use passed on parameters
   my ($first, $second) = @_;
   
   #if you have saved parameters 
   my $param = $self->{'safe_param'};
   delete $self->{'safe_param} #important!
   my ($first, $second) = @$param;
   
   #do your thing

        return @_
 }
 
 sub Hook3 {
        my $self = shift;
        print "before -configvariable\n"
        return @_
 }
 
 sub Hook4 {
        my $self = shift;
        print "after -configvariable\n"
        return @_
 }
 
 sub Unload {
    my $self = shift;
    $self->cmdUnhookBefore('command1', 'Hook1', $self);
    $self->cmdUnhookAfter('command1', 'Hook2', $self);
    $self->configUnhookBefore('configvariable', 'Hook3', $self);
    $self->configUnhookAfter('configvariable', 'Hook4', $self);
    $self->{'widg'}->destroy;
    return $self->SUPER::Unload;
 }

AUTHOR

Hans Jeuken (hanje at cpan dot org)

SEE ALSO

Tk::AppWindow
Tk::AppWindow::BaseClasses::Plugin
Tk::AppWindow::Ext::Plugins