AppWindow/Ext/ToolBar.pm untested

filter: Statementstmt 0.0% 0 / 215 Branchbran 0.0% 0 / 64 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 324 CC 33 SCAR 34.0
CC
33
Cov
0%
ConfButton
90.0
AddItem
72.0
CreateItems
20.0
CRAP 30.0
1 package Tk::AppWindow::Ext::ToolBar;
2  
3 =head1 NAME
4  
5 Tk::AppWindow::Ext::ToolBar - add a tool bar
6  
7 =cut
8  
9 use strict;
10 use warnings;
11 use vars qw($VERSION);
12 $VERSION="0.11";
13 use Tk;
14 require Tk::Compound;
15 require Tk::Poplevel;
16  
17 my $down_arrow = '#define down_width 10
18 #define down_height 10
19 static unsigned char down_bits[] = {
20 0x00, 0x00, 0x00, 0x00, 0xff, 0x03, 0xfe, 0x01, 0xfc, 0x00, 0x78, 0x00,
21 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
22 ';
23  
24  
25 use base qw( Tk::AppWindow::BaseClasses::Extension );
26  
27 =head1 SYNOPSIS
28  
29 my $app = new Tk::AppWindow(@options,
30 -extensions => ['ToolBar'],
31 );
32 $app->MainLoop;
33  
34 =head1 DESCRIPTION
35  
36 Add a toolbar to your application.
37  
38 =head1 CONFIG VARIABLES
39  
40 =over 4
41  
42 =item B<-autotool>
43  
44 Default value 1.
45  
46 Specifies if the toolbar items of all extensions should be loaded automatically.
47  
48 =item B<-toolbarpanel>
49  
50 Default value 'TOP'. Sets the name of the panel home to B<ToolBar>.
51  
52 =item B<-toolbarvisible>
53  
54 Default value 1. Show or hide tool bar.
55  
56 =item B<-tooliconsize>
57  
58 Default value 16
59  
60 =item B<-toolitems>
61  
62 Default value [].
63  
64 Configure your tool bar here. Example:
65  
66 [ #type #label #command #icon #help
67 [ 'tool_button', 'New', 'doc_new', 'document-new', 'Create a new document' ],
68
69 [ 'tool_list', 'popcommand' ],
70 [ 'tool_button', 'Save', 'doc_save', 'document-save', 'Save current document' ],
71 [ 'tool_button', 'Save as', 'doc_save_as', 'document-save-as', 'Rename and save current document' ],
72 [ 'tool_separator' ],
73 [ 'tool_button', 'Save all','doc_save_all','document-save-as', 'Save all modified documents' ],
74 [ 'tool_list_end' ],
75
76 [ 'tool_separator' ],
77
78 #type #label, #class
79 [ 'tool_widget', 'Widget', 'MyWidget', @options ],
80 [ 'tool_widget', '*Nolabel,'MyWidget', @options ],
81 ]
82  
83 'MyWidget', must be the class name of a packable Tk widget.
84  
85 =item B<-tooltextposition>
86  
87 Specifies where text should be displayed in tool buttons.
88 Default value I<right>. Can be I<top>, I<left>, I<bottom>, I<right> or I<none>.
89  
90 =back
91  
92 =cut
93  
94 sub new {
95 my $class = shift;
96 my $self = $class->SUPER::new(@_);
97  
98 $self->Require('Art', 'Panels');
99
100 my $args = $self->GetArgsRef;
101 my $panel = delete $args->{'-toolbarpanel'};
102 $panel = 'TOP' unless defined $panel;
103 my $pn = $self->extGet('Panels');
104 $pn->panelAssign('tool bar', $panel);
105  
106 $self->addPreConfig(
107 -autotool => ['PASSIVE', undef, undef, 1],
108 -tooliconsize => ['PASSIVE', 'ToolIconSize', 'toolIconSize', 16],
109 -toolitems => ['PASSIVE', undef, undef, []],
110 -tooltextposition => ['PASSIVE', undef, undef, 'right'],
111 );
112 $self->{BASE} = undef;
113 $self->{MODE} = 0;
114 $self->{POPLEVELS} = [];
115 $self->{TYPESTABLE} = {};
116 $self->{ITEMLIST} = [];
117 $self->{WIDGETS} = {};
118 $self->ConfigureTypes(
119 tool_button => ['ConfButton', $self],
120 tool_list => ['ConfList', $self],
121 tool_list_end => ['ConfListEnd', $self],
122 tool_separator => ['ConfSeparator', $self],
123 tool_widget => ['ConfWidget', $self],
124 );
125  
126 $self->addPostConfig('DoPostConfig', $self);
127 return $self;
128 }
129  
130 =head1 METHODS
131  
132 =over 4
133  
134 =cut
135  
136 sub _base {
137 my $self = shift;
138 $self->{BASE} = shift if @_;
139 my $b = $self->{BASE};
140 return $b if defined $b;
141 my $pn = $self->extGet('Panels');
142 my $panel = $pn->panelAssign('tool bar');
143 return $self->Subwidget($panel);
144 }
145  
146 sub _mode {
147 my $self = shift;
148 $self->{MODE} = shift if @_;
149 return $self->{MODE};
150 }
151  
152 =item B<AddItem>I<$item, ?$position?);>
153  
154 Adds an item to the toolbar. The item must be a valid tk widget.
155 Your addition will be lost after a call to B<ReConfigure>.
156  
157 =cut
158  
159 sub AddItem {
160 my ($self, $item) = @_;
161 my $mode = $self->_mode;
162 $self->_mode(0);
163 my $base = $self->_base;
164 my @pack = ();
165 if (ref $base eq 'Tk::Poplevel') {
166 push @pack, -fill => 'x';
167 } else {
168 push @pack, -side => 'left', -fill => 'y';
169 }
170 push @pack, -padx => 2, -pady => 2 unless $mode;
171 $item->pack(@pack);
172 my $list = $self->{ITEMLIST};
173 push @$list, $item;
174 if ($mode) {
175 my $prop = $self->{LISTPROP};
176 delete $self->{LISTPROP};
177 my ($name, $call) = @$prop if defined $prop;
178 my $p;
179 $base->Button(
180 -highlightthickness => 0,
181 -relief => 'flat',
182 -image => $self->Bitmap(
183 -data => $down_arrow,
184 -foreground => $self->configGet('-foreground'),
185 ),
186 -command => sub {
187 unless ($p->ismapped) {
188 $self->cmdExecute($call) if defined $call
189 }
190 $p->popFlip
191 },
192 )->pack(-side => 'left', -fill => 'y');
193 $p = $base->Poplevel(-widget => $base);
194 $self->{WIDGETS}->{$name} = $p if defined $name;
195 my $pl = $self->{POPLEVELS};
196 push @$pl, $p;
197 $self->_base($p);
198 }
199 }
200  
201 sub ClearTools {
202 my $self = shift;
203 my $list = $self->{ITEMLIST};
204 my @removed = @$list;
205 while (@$list) {
206 my $t = shift @$list;
207 $t->packForget;
208 }
209 return @removed;
210 }
211  
212 sub Configure {
213 my $self = shift;
214 my $uitypes = $self->{TYPESTABLE};
215 while (@_) {
216 my $i = shift;
217 my @item = @$i;
218 my $type = shift @item;
219 if (defined $type) {
220 if (my $p = $uitypes->{$type}) {
221 $p->execute(@item);
222 } else {
223 warn "invalid type: $type"
224 }
225 } else {
226 warn "undefined type"
227 }
228 }
229 }
230  
231 =item B<ConfigureTypes>I<($type => $call, ...);
232  
233 Call this method before MainLoop runs.
234 Configure additional types for your toolbar.
235 Already defined types are i<tool_button> and I<-tool_separator>.
236 I<$call> can be any valid Tk callback. Just make sure the callback
237 returns a valid Tk widget.
238  
239 =cut
240  
241 sub ConfigureTypes {
242 my $self = shift;
243 my $tab = $self->{TYPESTABLE};
244 while (@_) {
245 my $type = shift;
246 my $call = shift;
247 $tab->{$type} = $self->CreateCallback(@$call);
248 }
249 }
250  
251 sub ConfButton {
252 my ($self, $label, $cmd, $icon, $help, $padding) = @_;
253 my $tb = $self->_base;
254  
255 my $bmp;
256 if (defined $icon) {
257 $bmp = $self->getArt($icon, $self->configGet('-tooliconsize'));
258 }
259  
260 my @balloon = ();
261 push @balloon, -statusmsg => $help if defined $help;
262 my $textpos = $self->configGet('-tooltextposition');
263 my $but;
264 my @bo = (-highlightthickness => 0, -relief => 'flat');
265  
266 if (defined $bmp) {
267 my $art = $self->extGet('Art');
268 my $compound = $art->createCompound(
269 -text => $label,
270 -image => $bmp,
271 -textside => $textpos,
272 );
273 push @bo, -image => $compound;
274 } else {
275 push @bo, -text => $label;
276 }
277  
278 my $call;
279 if ($cmd =~ /^<.+>/) { #matching an event
280 $call = sub {
281 $self->PopDown;
282 $self->eventGenerate($cmd);
283 }
284 } else {
285 $call = sub {
286 $self->PopDown;
287 $self->cmdExecute($cmd);
288 }
289 }
290 push @bo, -command => $call;
291  
292 $but = $tb->Button(@bo);
293 $self->BalloonAttach($but, $label) if $textpos eq 'none';
294 if ($self->extExists('StatusBar')) {
295 $self->StatusAttach($but, $help) if defined $help;
296 } else {
297 $self->BalloonAttach($but, $help) if defined $help;
298 }
299 $self->{WIDGETS}->{$label} = $but;
300 $self->AddItem($but, $padding);
301 }
302  
303 sub ConfList {
304 my ($self, $name, $call) = @_;
305 my $base = $self->_base;
306 my $f = $base->Frame;
307 $self->AddItem($f);
308 $self->_base($f);
309 $self->{LISTPROP} = [ $name, $call ];
310 $self->_mode(1);
311 }
312  
313 sub ConfListEnd {
314 my $self = shift;
315 $self->_base(undef);
316 }
317  
318 sub ConfSeparator {
319 my $self = shift;
320 my $tb = $self->_base;
321 my $s;
322 if (ref $tb eq 'Tk::Poplevel') {
323 $s = $tb->Frame(-relief => 'sunken', -borderwidth => 1, -height => 2, @_)
324 } else {
325 $s = $tb->Frame(-relief => 'sunken', -borderwidth => 1, -width => 2, @_)
326 }
327 $self->AddItem($s);
328 }
329  
330 sub ConfWidget {
331 my ($self, $label, $class, @options) = @_;
332 my $tb = $self->_base;
333 my $f = $tb->Frame;
334 unless ($label =~ /^\*/) {
335 my $l = "$label:";
336 $f->Label(-text => $l)->pack(-side => 'left');
337 }
338 my $w = $f->$class(@options)->pack(-side => 'left');
339 $self->{WIDGETS}->{$label} = $w;
340 $self->AddItem($f);
341  
342 }
343  
344 sub CreateItems {
345 my $self = shift;
346 my @u = ();
347 my @plugins = ();
348 my $w = $self->GetAppWindow;
349 if ($self->configGet('-autotool')) {
350 my @p = $self->extList;
351 my @l = ($w);
352 for (@p) { push @l, $w->extGet($_) }
353 for (@l) {
354 #we want toolbar items from plugins loaded last
355 push @u, $_->ToolItems if $_->Name ne 'Plugins';
356 push @plugins, $_->ToolItems if $_->Name eq 'Plugins';
357 }
358 }
359 my $m = $self->configGet('-toolitems');
360 push @u, @$m, @plugins;
361 $self->Configure(@u);
362 }
363  
364 sub DeleteAll {
365 my $self = shift;
366 my @removed = $self->ClearTools;
367 for (@removed) {
368 $_->destroy if Exists($_);
369 };
370 $self->{POPLEVELS} = [];
371 $self->{WIDGETS} = {};
372 }
373  
374 sub DoPostConfig {
375 my $self = shift;
376  
377 #fixing possible mismatch of iconsize at launch
378 my $art = $self->extGet('Art');
379 my $size = $self->configGet('-tooliconsize');
380 $size = $art->getAlternateSize($size) if defined $art;
381 $self->configPut(-tooliconsize => $size);
382
383 #show the toolbar if it should be visible
384 if ($self->configGet('-tool barvisible')) {
385 my $pn = $self->extGet('Panels');
386 my $panel = $pn->panelAssign('tool bar');
387 $pn->panelShow($panel);
388 }
389  
390 $self->CreateItems;
391 }
392  
393 sub GetItem {
394 my ($self, $item) = @_;
395 return $self->{WIDGETS}->{$item}
396 }
397  
398 sub PopDown {
399 my $self = shift;
400 my $pl = $self->{POPLEVELS};
401 for (@$pl) { $_->popDown }
402 }
403  
404 sub ReConfigure {
405 my $self = shift;
406 $self->DeleteAll;
407 $self->CreateItems;
408 }
409  
410 =item B<RemoveItem>I<$position);>
411  
412 Removes the item at $position from the tool bar.
413 The item will re-appear after a call to B<ReConfigure> if the item is included in the B<-toolitems> option.
414 Returns the removed item.
415  
416 =cut
417  
418 sub RemoveItem {
419 my ($self, $position) = @_;
420 my $list = $self->{ITEMLIST};
421 if (defined $position) {
422 $position = @$list if $position > @$list;
423 my $item = $list->[$position];
424 $item->packForget if defined $item;
425 return $item;
426 }
427 }
428  
429 =back
430  
431 =head1 AUTHOR
432  
433 Hans Jeuken (hanje at cpan dot org)
434  
435 =head1 BUGS
436  
437 Unknown. If you find any, please contact the author.
438  
439 =head1 SEE ALSO
440  
441 =over 4
442  
443 =item L<Tk::AppWindow>
444  
445 =item L<Tk::AppWindow::BaseClasses::Extension>
446  
447 =item L<Tk::AppWindow::BaseClasses::PanelExtension>
448  
449 =back
450  
451 =cut
452  
453 1;
454  
455  
456