AppWindow/Ext/StatusBar.pm untested

filter: Statementstmt 0.0% 0 / 136 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 199 CC 13 SCAR 20.2
CC
13
Cov
0%
Add
20.0
InitMsgItem
20.0
Delete
12.0
CRAP 7.6
1 package Tk::AppWindow::Ext::StatusBar;
2  
3 =head1 NAME
4  
5 Tk::AppWindow::Ext::StatusBar - adding a status bar
6  
7 =cut
8  
9 use strict;
10 use warnings;
11 use vars qw($VERSION);
12 $VERSION="0.15";
13 use Tk;
14 require Tk::Frame;
15 require Tk::Balloon;
16 require Tk::AppWindow::Ext::StatusBar::SImageItem;
17 require Tk::AppWindow::Ext::StatusBar::SMessageItem;
18 require Tk::AppWindow::Ext::StatusBar::SProgressItem;
19 require Tk::AppWindow::Ext::StatusBar::STextItem;
20  
21 use base qw( Tk::AppWindow::BaseClasses::Extension );
22  
23 my %types = (
24 image => {
25 class => 'SImageItem',
26 pack => [],
27 },
28 message => {
29 class => 'SMessageItem',
30 pack => [-expand => 1, -fill => 'x'],
31 },
32 progress => {
33 class => 'SProgressItem',
34 pack => [],
35 },
36 text => {
37 class => 'STextItem',
38 pack => [],
39 },
40 );
41  
42 =head1 SYNOPSIS
43  
44 my $app = new Tk::AppWindow(@options,
45 -extensions => ['StatusBar'],
46 );
47 $app->MainLoop;
48  
49 =head1 DESCRIPTION
50  
51 Add a status bar to your application.
52  
53 =head1 CONFIG VARIABLES
54  
55 =over 4
56  
57 =item B<-statusbarpanel>
58  
59 Default value 'BOTTOM'. Sets the name of the panel home to B<StatusBar>.
60  
61 =item B<-statusbarvisible>
62  
63 Default value 1. Show or hide status bar.
64  
65 =item B<-statusitemborderwidth>
66  
67 Default value 2.
68  
69 =item B<-statusitempadding>
70  
71 Default value 2.
72  
73 =item B<-statusitemrelief>
74  
75 Default value 'groove'.
76  
77 =item B<-statusmsgitemoninit>
78  
79 Default value 1.
80  
81 =item B<-statusupdatecycle>
82  
83 Default value 500. Repeat time for updating the items on the status bar.
84  
85 =back
86  
87 =cut
88  
89 sub new {
90 my $class = shift;
91  
92 my $self = $class->SUPER::new(@_);
93  
94 $self->Require('Panels');
95  
96 my $args = $self->GetArgsRef;
97 my $panel = delete $args->{'-statusbarpanel'};
98 $panel = 'BOTTOM' unless defined $panel;
99 my $pn = $self->extGet('Panels');
100 $pn->panelAssign('status bar', $panel);
101  
102 $self->addPreConfig(
103 -errorcolor => ['PASSIVE', 'errorColor', 'ErrorColor', '#FF0000'],
104 -warningcolor => ['PASSIVE', 'warningColor', 'WarningColor', '#0000FF'],
105 -statusitemrelief => ['PASSIVE', undef, undef, 'groove'],
106 -statusitemborderwidth => ['PASSIVE', undef, undef, 2],
107 -statusitempadding => ['PASSIVE', undef, undef, 2],
108 -statusupdatecycle =>['PASSIVE', undef, undef, 500],
109 -statusmsgitemoninit =>['PASSIVE', undef, undef, 1],
110 );
111  
112 $self->{BALLOON} = $self->Balloon;
113 $self->{ITEMS} = {};
114  
115
116 $self->addPostConfig('InitMsgItem', $self);
117 $self->addPostConfig('Cycle', $self);
118 return $self;
119 }
120  
121 =head1 METHODS
122  
123 =over 4
124  
125 =item B<Add>I<($type, $name, @options)>
126  
127 Adds an item to the status bar.
128 $type can have the values I<image>, I<message>, I<progress>, I<text>
129  
130 @options is a paired (switch => value) list.
131 General options are listed here. See type methods below for type specific options.
132  
133 =over 4
134  
135 =item B<-label>
136  
137 Specify the text of the label.
138 If this option is set it will create a label next to the item on the statusbar.
139  
140 =item B<-itempack>
141  
142 Default value [-side=> 'left', -padx => 2, -pady => 2].
143  
144 =item B<-position>
145  
146 Specify the numerical position the item should be placed.
147  
148 =item B<-updatecommand>
149  
150 Specify a callback that returns the value for this item.
151  
152 =back
153  
154 =cut
155  
156 sub Add {
157 my $self = shift;
158 my $type = shift;
159 my $name = shift;
160 unless (exists $types{$type}) {
161 warn "undefined statusbar type: $type";
162 return
163 }
164 my %params = (@_);
165 my $pos = delete $params{'-position'};
166 my $class = $types{$type}->{class};
167 my $pack = $types{$type}->{pack};
168 my $itempadding = $self->configGet('-statusitempadding');
169  
170 my $pn = $self->extGet('Panels');
171 my $panel = $pn->panelAssign('status bar');
172 if (defined $pos) {
173 my @items = $self->Subwidget($panel)->children;
174 my $b = $items[$pos];
175 push @$pack, -before => $b if defined $b;
176 }
177 my $i = $self->Subwidget($panel)->$class(%params,
178 -relief => $self->configGet('-statusitemrelief'),
179 -borderwidth => $self->configGet('-statusitemborderwidth'),
180 )->pack(@$pack, -padx => $itempadding, -pady => $itempadding, -side => 'left');
181 $self->{ITEMS}->{$name} = $i;
182 return $i
183 }
184  
185 =item B<AddImageItem>I<($name, @options)>
186  
187 Almost the same as Add('image', $name, @options).
188 In the options B<-valueimages> you specify icon names.
189 Extension B<Art> must be loaded for this.
190 You can specify all the options for a Tk::Label and the following:
191  
192 =over 4
193  
194 =item B<-valueimages>
195  
196 Specify a hash ref. Example;
197  
198 {
199 0 => $w->Bitmap('error'),
200 1 -> $w->Bitmap('transparent')
201 }
202  
203 =back
204  
205 =cut
206  
207 sub AddImageItem {
208 my $self = shift;
209 my $name = shift;
210 my %options = (@_);
211 my $imgo = delete $options{'-valueimages'};
212 my %vi = ();
213 if (defined $imgo) {
214 for (keys %$imgo) {
215 my $img = $imgo->{$_};
216 $vi{$_} = $self->getArt($img);
217 }
218 $options{'-valueimages'} = \%vi;
219 }
220 return $self->Add('image', $name, %options);
221 }
222  
223 =item B<AddMessageItem>I<($name, @options)>
224  
225 Same as Add('message', $name, @options)
226 You can specify all the options for a Tk::Label.
227  
228 =cut
229  
230 sub AddMessageItem {
231 my $self = shift;
232 return $self->Add('message', @_);
233 }
234  
235 =item B<AddProgressItem>I<($name, @options)>
236  
237 Same as Add('progress', $name, @options)
238 You can specify all the options for a Tk::ProgressBar.
239  
240 =cut
241  
242 sub AddProgressItem {
243 my $self = shift;
244 return $self->Add('progress', @_);
245 }
246  
247 =item B<AddTextItem>I<($name, @options)>
248  
249 Same as Add('text', $name, @options)
250 You can specify all the options for a Tk::Label.
251  
252 =cut
253  
254 sub AddTextItem {
255 my $self = shift;
256 return $self->Add('text', @_);
257 }
258  
259 sub Attach {
260 my ($self, $widget, $text) = @_;
261 $self->{BALLOON}->attach($widget, -statusmsg => $text)
262 }
263  
264 sub Cycle {
265 my $self = shift;
266 my $time = $self->configGet('-statusupdatecycle');
267 $self->after($time, ['Update', $self]) unless $time eq 0;
268 }
269  
270 =item B<Delete>I<($name)>
271  
272 Removes $name from the status bar and destroys the item object.
273  
274 =cut
275  
276 sub Delete {
277 my ($self, $name) = @_;
278 unless ($name eq 'msg') {
279 if ($self->ItemExists($name)) {
280 my $it = $self->Item($name);
281 $it->Remove;
282 $it->destroy;
283 my $ih = $self->{ITEMS};
284 delete $ih->{$name}
285 }
286 }
287 }
288  
289 sub InitMsgItem {
290 my $self = shift;
291  
292 #show the statusbar if it should be visible
293 if ($self->configGet('-status barvisible')) {
294 my $pn = $self->extGet('Panels');
295 my $panel = $pn->panelAssign('status bar');
296 $pn->panelShow($panel);
297 }
298  
299 if ($self->configGet('-statusmsgitemoninit')) {
300 unless (exists $self->{MI}) {
301 my $mi = $self->AddMessageItem('msg', -position => 0);
302 $self->{MI} = $mi;
303 $self->{BALLOON}->configure(-statusbar => $mi);
304 $self->configPut(-logcall => sub { $mi->Message(shift) });
305 $self->configPut(-logerrorcall => sub { $mi->Message(shift, $self->configGet('-errorcolor')) });
306 $self->configPut(-logwarningcall => sub { $mi->Message(shift, $self->configGet('-warningcolor')) });
307 return $mi;
308 }
309 }
310 }
311  
312 =item B<Item>I<($name)>
313  
314 Returnes the item object for $name.
315  
316 =cut
317  
318 sub Item {
319 my ($self, $name) = @_;
320 return $self->{ITEMS}->{$name}
321 }
322  
323 =item B<ItemExists>I<($name)>
324  
325 Returnes true if $name exists.
326  
327 =cut
328  
329 sub ItemExists {
330 my ($self, $name) = @_;
331 return exists $self->{ITEMS}->{$name}
332 }
333  
334 =item B<Message>I<($text)>
335  
336 Display $text on the message item in the status bar if it exists.
337 The message will be deleted upon the first key stroke or mouse click.
338  
339 =cut
340  
341 sub Message {
342 my $self = shift;
343 my $msg = $self->{MI};
344 $msg->Message(@_) if defined $msg;
345 }
346  
347 sub Update {
348 my $self = shift;
349 my $items = $self->{ITEMS};
350 for (keys %$items) {
351 $self->Item($_)->Update
352 }
353 $self->Cycle;
354 }
355  
356 =back
357  
358 =head1 AUTHOR
359  
360 Hans Jeuken (hanje at cpan dot org)
361  
362 =head1 BUGS
363  
364 Unknown. If you find any, please contact the author.
365  
366 =head1 SEE ALSO
367  
368 =over 4
369  
370 =item L<Tk::AppWindow>
371  
372 =item L<Tk::AppWindow::BaseClasses::Extension>
373  
374 =item L<Tk::AppWindow::BaseClasses::PanelExtension>
375  
376 =back
377  
378 =cut
379  
380 1;
381