| 1 | package Tk::AppWindow::PluginsForm; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Tk::AppWindow::PluginsForm - Load and unload plugins. | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use vars qw($VERSION); | ||
| 12 | $VERSION = '0.07'; | ||
| 13 | |||
| 14 | use base qw(Tk::Derived Tk::Frame); | ||
| 15 | |||
| 16 | Construct Tk::Widget 'PluginsForm'; | ||
| 17 | |||
| 18 | require Tk::LabFrame; | ||
| 19 | require Tk::Pane; | ||
| 20 | use Data::Compare; | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | This package is a L<Tk::Frame> based megawidget. | ||
| 25 | It provides a for for loading and unloading plugins | ||
| 26 | to a L<Tk::AppWindow> based application. | ||
| 27 | |||
| 28 | =head1 OPTIONS | ||
| 29 | |||
| 30 | =over 4 | ||
| 31 | |||
| 32 | =item Switch: B<-pluginsext> | ||
| 33 | |||
| 34 | A reference to the B<Plugins> extension. Mandatory! | ||
| 35 | |||
| 36 | =back | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | sub Populate { | ||
| 41 | my ($self,$args) = @_; | ||
| 42 | |||
| 43 | my $ext = delete $args->{'-pluginsext'}; | ||
| 44 | die 'Please specify -pluginsext' unless defined $ext; | ||
| 45 | |||
| 46 | $self->SUPER::Populate($args); | ||
| 47 | |||
| 48 | my $lf = $self->LabFrame( | ||
| 49 | -label => 'Available plugins', | ||
| 50 | -labelside => 'acrosstop', | ||
| 51 | )->pack(-expand => 1, -fill => 'both', -padx => 2, -pady => 2); | ||
| 52 | my $pane = $lf->Scrolled('Pane', | ||
| 53 | -height => 200, | ||
| 54 | -scrollbars => 'oe', | ||
| 55 | -sticky => 'nsew', | ||
| 56 | )->pack(-expand => 1, -fill => 'both'); | ||
| 57 | |||
| 58 | my %plugins = (); | ||
| 59 | for ($ext->AvailablePlugins) { | ||
| 60 | my $plug = $_; | ||
| 61 | my $val = $ext->plugExists($plug); | ||
| 62 | $plugins{$plug} = \$val; | ||
| 63 | my $f = $pane->Frame( | ||
| 64 | -borderwidth => 2, | ||
| 65 | -relief => 'groove', | ||
| 66 | )->pack(-fill => 'x', -padx => 2, -pady => 2); | ||
| 67 | $f->Checkbutton( | ||
| 68 | # TODO reinstate this after bugfix with plug loading after main loop | ||
| 69 | -command => sub { | ||
| 70 | if ($val) { | ||
| 71 | $ext->plugLoad($plug); | ||
| 72 | $self->after(10, sub { $val = '' unless $ext->plugExists($plug) }); | ||
| 73 | } else { | ||
| 74 | $ext->plugUnload($plug); | ||
| 75 | $self->after(10, sub { $val = '' if $ext->plugExists($plug) }); | ||
| 76 | } | ||
| 77 | }, | ||
| 78 | -variable => \$val, | ||
| 79 | -text => $plug | ||
| 80 | )->pack(-padx => 2, -pady => 2, -anchor => 'w'); | ||
| 81 | $f->Label( | ||
| 82 | -text => $ext->plugDescription($plug), | ||
| 83 | -justify => 'left', | ||
| 84 | )->pack(-padx => 2, -pady => 2, -anchor => 'w'); | ||
| 85 | } | ||
| 86 | $self->{PLUGINS} = \%plugins; | ||
| 87 | |||
| 88 | my $bf = $self->Frame->pack(-fill => 'x'); | ||
| 89 | $bf->Button( | ||
| 90 | -text => 'Select all', | ||
| 91 | -command => ['LoadAll', $self], | ||
| 92 | )->pack(-side => 'left', -padx => 2, -pady => 2); | ||
| 93 | $bf->Button( | ||
| 94 | -text => 'Select none', | ||
| 95 | -command => ['UnloadAll', $self], | ||
| 96 | )->pack(-side => 'left', -padx => 2, -pady => 2); | ||
| 97 | $self->ConfigSpecs( | ||
| 98 | -pluginsext => ['PASSIVE', undef, undef, $ext], | ||
| 99 | DEFAULT => [ $self ], | ||
| 100 | ); | ||
| 101 | } | ||
| 102 | |||
| 103 | #sub Apply { | ||
| 104 | # my $self = shift; | ||
| 105 | # my $ext = $self->cget('-pluginsext'); | ||
| 106 | # my @current = $ext->plugList; | ||
| 107 | # my @new = (); | ||
| 108 | # my $plugs = $self->{PLUGINS}; | ||
| 109 | # for (sort keys %$plugs) { | ||
| 110 | # my $var = $plugs->{$_}; | ||
| 111 | # push @new, $_ if $$var; | ||
| 112 | # } | ||
| 113 | # unless (Compare(\@current, \@new)) { | ||
| 114 | # $ext->SavePlugList(@new); | ||
| 115 | # my $name = $ext->configGet('-appname'); | ||
| 116 | # $ext->popMessage("Please restart $name", 'dialog-warning'); | ||
| 117 | # } | ||
| 118 | #} | ||
| 119 | |||
| 120 | sub LoadAll { | ||
| 121 | my $self = shift; | ||
| 122 | my $ext = $self->cget('-pluginsext'); | ||
| 123 | my $plugins = $self->{PLUGINS}; | ||
| 124 | for (sort keys %$plugins) { | ||
| 125 | my $plug = $_; | ||
| 126 | # my $val = $plugins->{$plug}; | ||
| 127 | # $$val = 1; | ||
| 128 | # TODO reinstate this after bugfix with plug loading after main loop | ||
| 129 | $ext->plugLoad($plug); | ||
| 130 | $self->after(10, sub { | ||
| 131 | my $v = $plugins->{$plug}; | ||
| 132 | if ($ext->plugExists($plug)) { | ||
| 133 | $$v = 1 | ||
| 134 | } else { | ||
| 135 | $$v = '' | ||
| 136 | } | ||
| 137 | }); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | sub UnloadAll { | ||
| 142 | my $self = shift; | ||
| 143 | my $ext = $self->cget('-pluginsext'); | ||
| 144 | my $plugins = $self->{PLUGINS}; | ||
| 145 | for (sort keys %$plugins) { | ||
| 146 | my $plug = $_; | ||
| 147 | # my $val = $plugins->{$plug}; | ||
| 148 | # $$val = ''; | ||
| 149 | # TODO reinstate this after bugfix with plug loading after main loop | ||
| 150 | $ext->plugUnload($plug); | ||
| 151 | $self->after(10, sub { | ||
| 152 | my $v = $plugins->{$plug}; | ||
| 153 | if ($ext->plugExists($plug)) { | ||
| 154 | $$v = 1 | ||
| 155 | } else { | ||
| 156 | $$v = '' | ||
| 157 | } | ||
| 158 | }); | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | =head1 AUTHOR | ||
| 163 | |||
| 164 | Hans Jeuken (hanje at cpan dot org) | ||
| 165 | |||
| 166 | =head1 BUGS | ||
| 167 | |||
| 168 | Unknown. If you find any, please contact the author. | ||
| 169 | |||
| 170 | =head1 SEE ALSO | ||
| 171 | |||
| 172 | =over 4 | ||
| 173 | |||
| 174 | =item L<Tk::AppWindow::Ext::Plugins> | ||
| 175 | |||
| 176 | =back | ||
| 177 | |||
| 178 | =cut | ||
| 179 | |||
| 180 | 1; | ||
| 181 | |||
| 182 | |||
| 183 |