DHList_v1.pm untested

filter: Statementstmt 0.0% 0 / 234 Branchbran 0.0% 0 / 100 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 34 Podpod 0.0% 0 / 27 totaltotal 0.0% 0 / 426 CC 51 SCAR 38.6
CC
51
Cov
0%
sort
210.0
add
30.0
reverse
30.0
CRAP 47.5
1 ######################################## SOH ###########################################
2 ## Function : Additional Tk Class for Listbox-type HList with Data per Item, Sorting
3 ##
4 ## Copyright (c) 2002-2005 Michael Krause. All rights reserved.
5 ## This program is free software; you can redistribute it and/or modify it
6 ## under the same terms as Perl itself.
7 ##
8 ## History : V1.00 10-Dec-2002 Class adopted from ExtListbox. MK
9 ## V1.01 10-Jan-2003 Added -databackground(color). MK
10 ## V1.02 19-Jan-2004 Added numeric sorting for column 2. MK
11 ## V1.1 13-May-2005 Added missing data for sorting in column 2. MK
12 ## V1.2 23-Oct-2008 Bugfix: Deleting the first entry messed the reverse func. MK
13 ##
14 ######################################## EOH ###########################################
15 package Tk::DHList_v1;
16  
17 ##############################################
18 ### Use
19 ##############################################
20 #use Tk::HList;
21 use Tk::HListplus;
22 use Tk::ItemStyle;
23 use Tk qw(Ev);
24  
25 use strict;
26 use Carp;
27  
28 use vars qw ($VERSION);
29 $VERSION = '1.2';
30  
31 #use base qw (Tk::Derived Tk::HList);
32 use base qw (Tk::Derived Tk::HListplus);
33  
34 ########################################################################
35 Construct Tk::Widget 'DHList_v1';
36  
37 #---------------------------------------------
38 # internal Setup function
39 #---------------------------------------------
40 sub ClassInit
41 {
42 my ($class, $window) = @_;
43  
44 $class->SUPER::ClassInit($window);
45  
46 # Note these keyboard-Keys are only usable, if the widget gets 'focus'
47 $window->bind ($class, '<Control-Key-bracketleft>', [\&viewtype, 'withdata']);
48 $window->bind ($class, '<Control-Key-bracketright>', [\&viewtype, 'normal']);
49 # Update the view after mapping
50 $window->bind ($class, '<Map>', \&map_cb);
51 }
52  
53 #---------------------------------------------
54 # internal Setup function
55 #---------------------------------------------
56 sub CreateArgs
57 {
58 my ($class, $window, $args) = @_;
59
60 # Necessarily Patch Columns
61 my %args;
62 (%args) = ( -columns => '2') unless defined $args->{-columns};
63 ($class->SUPER::CreateArgs($window, $args), %args);
64 }
65  
66 #---------------------------------------------
67 # internal Setup function
68 #---------------------------------------------
69 sub Populate
70 {
71 my ($this, $args) = @_;
72  
73 my $data_background = delete $args->{-databackground};
74 $data_background = $this->cget ('-background') unless defined $data_background;
75 my $style = delete $args->{-datastyle};
76 $style = $this->toplevel->ItemStyle ('text' ,
77 -anchor => 'e',
78 -background => $data_background,
79 ) unless defined $style;
80  
81 # Check whether we want numeric sorting
82 $this->{m_numeric_primary_sort} = delete $args->{-numeric_primary_sort} || 0;
83 $this->{m_numeric_secondary_sort} = delete $args->{-numeric_secondary_sort} || 0;
84
85 # Reroute any size_call_back
86 my $sizecmd = delete $args->{-sizecmd};
87 $sizecmd = sub { return 1 } unless defined $sizecmd;
88 $args->{-sizecmd} = [\&resize_cb, $this ];
89  
90 #INvoke Superclass fill func
91 $this->SUPER::Populate($args);
92  
93 #
94 $this->ConfigSpecs(
95 #default listbox options
96 -pady => [['SELF', 'PASSIVE'], 'pady', 'Pad', '0'],
97 -sizecmd_cb => ['CALLBACK',undef, undef, $sizecmd],
98 # new, additional optiona
99 -viewtype => ['METHOD', 'ViewType', 'viewType', undef],
100 -datastyle => [['SELF', 'PASSIVE'], 'datastyle', 'datastyle', $style],
101 );
102
103 # Internal Presets
104 $this->{m_firstpath} = undef;
105 $this->{m_datastyle} = $style;
106 $this->{m_viewtype} = '---';
107 }
108  
109 #---------------------------------------------
110 # OVERRIDE: new ADD function
111 #---------------------------------------------
112 sub add
113 {
114 # Locals
115 my ($this, $path, %args, $data, $datastyle);
116  
117 # Parameters
118 $this = shift;
119 $path = shift;
120 %args = @_;
121
122 # Do we have anything at all to insert ?
123 return unless @_;
124  
125 # Prepare the data and it's style
126 $data = (defined $args{-data}) ? delete $args{-data} : 'undef';
127 $datastyle = (defined $args{-datastyle}) ? delete $args{-datastyle} : $this->{m_datastyle};
128  
129 # now add it as desired
130 $this->SUPER::add($path, -data => $data);
131 $this->SUPER::itemCreate($path, 0, %args);
132 # and a second column for the data
133 $this->SUPER::itemCreate($path, 1,
134 -itemtype => 'text',
135 -text => $data,
136 -style => $datastyle,
137 );
138 # store it internally
139 $this->{m_firstpath} = $path unless defined $this->{m_firstpath};
140 # store it internally
141 $this->{m_datastyle}{$path} = $datastyle;
142
143 # Install the 'normal view after we have something on the screen..
144 $this->viewtype($this->{m_viewtype});
145 }
146  
147 #---------------------------------------------
148 # OVERRIDE: new DELETE function
149 #---------------------------------------------
150 sub delete
151 {
152 # Parameters
153 my ($this, $what, $path) = @_;
154  
155 if ($what eq 'all') {
156 # Clear the internal storage
157 $this->{m_firstpath} = undef;
158 # Delete it
159 $this->SUPER::delete($what);
160 }
161 else {
162 # Clear the internal storage
163 if (defined $this->{m_firstpath}) {
164 if ($path eq $this->{m_firstpath}) {
165 $this->{m_firstpath} = $this->infoNext($path);
166 }
167 }
168 # Delete it
169 $this->SUPER::delete($what, $path);
170 }
171 }
172  
173 #---------------------------------------------
174 # ADD-ON: reordering function
175 #---------------------------------------------
176 sub reverse
177 {
178 # Locals
179 my ($this, @paths, $path, @allitems, $opt, $value, %items, %data, %hidden);
180  
181 # Parameters
182 $this = shift;
183  
184 # safety check
185 return unless defined $this->{m_firstpath};
186 # Retrieve the contents
187 $path = $this->{m_firstpath};
188 while (defined $path && $path ne "") {
189 push @paths, $path;
190 #----------------------------------
191 @allitems = $this->itemConfigure($path, 0);
192 my %args = ();
193 foreach (@allitems) {
194 $opt = $_->[0]; $value = $_->[4];
195 if (defined $value && $value ne "") {
196 $args{$opt} = $value;
197 }
198 }
199 $items{$path} = \%args;
200 #----------------------------------
201 my $data = $this->infoData($path);
202 #print "retrieved data $data<\n";
203 $data{$path} = $data;
204 #----------------------------------
205 my $hidden = $this->infoHidden($path);
206 #print "retrieved hidden $hidden<\n";
207 $hidden{$path} = $hidden;
208 #----------------------------------
209 $path = $this->infoNext($path);
210 }
211 # delete it
212 $this->delete('all');
213  
214 $this->{m_firstpath} = undef;
215
216 # Reverse, refill it
217 foreach ( reverse @paths ) {
218 $this->add( $_, -data => $data{$_},
219 %{$items{$_}} );
220 if ($hidden{$_} == 1) {
221 $this->hide('entry', $_);
222 }
223 $this->{m_firstpath} = $_ unless defined $this->{m_firstpath};
224 }
225 }
226  
227 #---------------------------------------------
228 # ADD-ON: sorting function
229 #---------------------------------------------
230 sub sort
231 {
232 # Locals
233 my ($this, $mode, @paths, $path, @allitems, $opt, $value, %items, %data, %hidden,
234 @sortarray, @tmparray);
235 #print "begin of sort\n";
236  
237 # Parameters
238 $this = shift;
239 $mode = shift; $mode = 'ascending' unless defined $mode;
240  
241 # safety check
242 return unless defined $this->{m_firstpath};
243  
244 # Retrieve the contents
245 $path = $this->{m_firstpath};
246 while (defined $path && $path ne "") {
247 push @paths, $path;
248 #----------------------------------
249 @allitems = $this->itemConfigure($path, 0);
250 my %args = ();
251 foreach (@allitems) {
252 $opt = $_->[0]; $value = $_->[4];
253 #$opt = 'undef' unless defined $opt;
254 #$value = 'undef' unless defined $value;
255 #print "retrieved \$opt = >$opt<, \$value = >", defined $value ? $value : 'undef', ,"<\n";
256 if (defined $value && $value ne "") {
257 $args{$opt} = $value;
258 }
259 }
260 $items{$path} = \%args;
261 #----------------------------------
262 my $data = $this->infoData($path);
263 #print "retrieved data $data<\n";
264 $data{$path} = $data;
265 #----------------------------------
266 my $hidden = $this->infoHidden($path);
267 #print "retrieved hidden $hidden<\n";
268 $hidden{$path} = $hidden;
269 #----------------------------------
270 $path = $this->infoNext($path);
271 }
272 # delete it
273 $this->delete('all');
274
275 foreach (@paths) {
276 push @sortarray, [ $_, $items{$_}->{-text}, $data{$_} ];
277 }
278  
279 # sort it
280 if ($mode =~ /ascending/i) {
281 if ($mode =~ /secondary/i) {
282 if ($this->{m_numeric_secondary_sort}) {
283 @tmparray = sort secondary_numeric @sortarray;
284 }
285 else {
286 @tmparray = sort secondary @sortarray;
287 }
288 }
289 else {
290 if ($this->{m_numeric_primary_sort}) {
291 @tmparray = sort primary_numeric @sortarray;
292 }
293 else {
294 @tmparray = sort primary @sortarray;
295 }
296 }
297 }
298 elsif ($mode =~ /descending/i) {
299 if ($mode =~ /secondary/i) {
300 if ($this->{m_numeric_secondary_sort}) {
301 @tmparray = sort rev_secondary_numeric @sortarray;
302 }
303 else {
304 @tmparray = sort rev_secondary @sortarray;
305 }
306 }
307 else {
308 if ($this->{m_numeric_primary_sort}) {
309 @tmparray = sort rev_primary_numeric @sortarray;
310 }
311 else {
312 @tmparray = sort rev_primary @sortarray;
313 }
314 }
315 }
316 else {
317 return;
318 }
319
320 #
321 $this->{m_firstpath} = undef;
322
323 # refill it
324 foreach ( @tmparray ) {
325 $path = $_->[0];
326 $this->add( $path, -data => $data{$path},
327 %{$items{$path}}, -datastyle => $this->{m_datastyle}{$path} );
328 if ($hidden{$path} == 1) {
329 $this->hide('entry', $path);
330 }
331 $this->{m_firstpath} = $path unless defined $this->{m_firstpath};
332 }
333 }
334 sub secondary
335 {
336 $a->[2] cmp $b->[2];
337 }
338 sub rev_secondary
339 {
340 $b->[2] cmp $a->[2];
341 }
342 sub secondary_numeric
343 {
344 $a->[2] <=> $b->[2];
345 }
346 sub rev_secondary_numeric
347 {
348 $b->[2] <=> $a->[2];
349 }
350 sub primary
351 {
352 $a->[1] cmp $b->[1];
353 }
354 sub rev_primary
355 {
356 $b->[1] cmp $a->[1];
357 }
358 sub primary_numeric
359 {
360 $a->[1] <=> $b->[1];
361 }
362 sub rev_primary_numeric
363 {
364 $b->[1] <=> $a->[1];
365 }
366  
367 #---------------------------------------------
368 # ADD-ON: trace any viewtype updates
369 # supported values are 'normal', 'withdata'
370 #---------------------------------------------
371 sub viewtype
372 {
373 # Parameters
374 my ($this, $viewtype) = @_;
375 # if we're not in a cget we might consider changing the value
376 if (defined $viewtype) {
377 #print "we're in viewtype with $viewtype\n";
378 # allow only 'normal' & 'withdata'...
379 if ($viewtype =~ /withdata/i ) {
380 $viewtype = 'withdata';
381 }
382 else {
383 $viewtype = 'normal';
384 }
385 #
386 #print "internal viewtype is $this->{m_viewtype}\n";
387 if ($this->{m_viewtype} ne $viewtype) {
388 $this->{m_viewtype} = $viewtype;
389 $this->setup_view();
390 }
391 }
392 # needed for cget
393 return $this->{m_viewtype};
394 }
395  
396 #---------------------------------------------
397 # ADD-ON: setup_view: display function
398 #---------------------------------------------
399 sub setup_view
400 {
401 # Parameters
402 my $this = shift;
403  
404 # Locals
405 my (@bb, $needsize);
406 #print "firstpath = ", (defined $this->{m_firstpath}) ? $this->{m_firstpath} : "undef";
407 return unless defined $this->{m_firstpath};
408 #print "in setupview\n";
409 if ($this->{m_viewtype} eq 'withdata') {
410 $this->columnWidth(1, '');
411 $needsize = $this->columnWidth(1);
412 }
413 else {
414 $this->columnWidth(1, 0);
415 $needsize = 0;
416 }
417 @bb = $this->infoBbox($this->{m_firstpath});
418 # Fast hack to avoid empty bbox due to invisible first item
419 if (@bb == 0) {
420 my $path = $this->nearest(10);
421 @bb = $this->infoBbox($path);
422 }
423 #print "bbox is @bb<, needsize is $needsize\n";
424 if (@bb) {
425 $this->columnWidth(0, $bb[2] - $bb[0] - $needsize);
426 }
427 }
428  
429 #---------------------------------------------
430 # ADD-ON: new resizxing function
431 #---------------------------------------------
432 sub resize_cb
433 {
434 my ($this) = shift;
435 return unless defined $this->{m_firstpath};
436 return unless $this->viewable;
437
438 my $path0 = $this->{m_firstpath};
439 my @bb = $this->infoBbox($path0);
440 #print "bbox is >@bb<\n";
441 return if ((scalar @bb == 0) || ($bb[2] - $bb[0] == 0));
442 my $needsize = $this->columnWidth(1);
443 $this->columnWidth(0, $bb[2] - $bb[0] - $needsize);
444  
445 # invoke any given callback
446 my @args = ( $this );
447 $this->Callback(-sizecmd_cb => @args);
448 }
449  
450  
451 #---------------------------------------------
452 # CALLBACK: update the view (type 'normal')
453 # means Data-Column is invisible
454 #---------------------------------------------
455 sub map_cb
456 {
457 # Parameters
458 my $this = shift;
459 $this->setup_view();
460 }
461  
462 #---------------------------------------------
463 # ADD-ON: get Item and / or ItemData
464 # returns a scalar(first only) or an array
465 #---------------------------------------------
466 sub get_item
467 {
468 # Parameters
469 my ($this, $path) = @_;
470  
471 # get all information
472 my @items_out = $this->_get_item(3, $path);
473  
474 return wantarray ? @items_out : $items_out[0];
475 }
476 #---------------------------------------------
477 sub get_item_text
478 {
479 # Parameters
480 my ($this, $path) = @_;
481  
482 # get all information
483 my @items_out = $this->_get_item(1, $path);
484  
485 return wantarray ? @items_out : $items_out[0];
486 }
487 #---------------------------------------------
488 sub get_item_value
489 {
490 # Parameters
491 my ($this, $path) = @_;
492  
493 # get all information
494 my @items_out = $this->_get_item(2, $path);
495  
496 return wantarray ? @items_out : $items_out[0];
497 }
498 #---------------------------------------------
499 sub _get_item
500 {
501 # Parameters
502 my ($this, $mode, $path) = @_;
503  
504 # Locals
505 my (@items_out);
506  
507 if ($mode & 1) {
508 push @items_out, $this->itemCget($path, 0, '-text');
509 }
510 if ($mode & 2) {
511 push @items_out, $this->infoData($path);
512 }
513 return wantarray ? @items_out : $items_out[0];
514 }
515  
516 #---------------------------------------------
517 # ADD-ON: Get Current Selected Text AND/OR associated Data
518 #---------------------------------------------
519 sub getcurselection
520 {
521 # Parameters
522 my $this = shift;
523  
524 # get all information
525 my @items_out = $this->_getcurselection(3);
526  
527 return wantarray ? @items_out : $items_out[0];
528 }
529 #---------------------------------------------
530 sub getcurselection_text
531 {
532 # Parameters
533 my $this = shift;
534  
535 # get all information
536 my @items_out = $this->_getcurselection(1);
537  
538 return wantarray ? @items_out : $items_out[0];
539 }
540 #---------------------------------------------
541 sub getcurselection_value
542 {
543 # Parameters
544 my $this = shift;
545  
546 # get all information
547 my @items_out = $this->_getcurselection(2);
548  
549 return wantarray ? @items_out : $items_out[0];
550 }
551  
552 #---------------------------------------------
553 sub _getcurselection
554 {
555 # Parameters
556 my ($this) = shift;
557 my ($mode) = shift;
558  
559 # Locals
560 my (@items_out, @selitems, $path);
561  
562 # get index information
563 @selitems = $this->infoSelection;
564 foreach $path (@selitems) {
565 if ($mode & 1) {
566 push @items_out, $this->itemCget($path, 0, '-text' );
567 }
568 if ($mode & 2) {
569 push @items_out, $this->infoData($path);
570 }
571 }
572 return wantarray ? @items_out : $items_out[0];
573 }
574  
575  
576 ########################################################################
577 1;
578 __END__