DHList.pm untested

filter: Statementstmt 0.0% 0 / 253 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 437 CC 49 SCAR 38.3
CC
49
Cov
0%
Populate
132.0
sort
132.0
setup_view
30.0
CRAP 45.9
1 ######################################## SOH ###########################################
2 ## Function : Additional Tk Class for Listbox-type HList with Data per Item, Sorting
3 ##
4 ## Copyright (c) 2002-2013 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 ## V2.0 11-Sep-2009 Rewrite: Added multi-Column and Header support. MK
14 ## V2.1 14-Sep-2009 Bugfix: Solved problems with memory leak due Itemstyle. MK
15 ## V2.2 08-Apr-2013 Bugfix: Retrieval of data this is a ref-to-* is lost (flattened in return-list). MK
16 ##
17 ######################################## EOH ###########################################
18 package Tk::DHList;
19  
20 ##############################################
21 ### Use
22 ##############################################
23 use Tk::HList;
24 use Tk::ItemStyle;
25 use Tk qw(Ev);
26  
27 use strict;
28 use Carp;
29  
30 use vars qw ($VERSION);
31 $VERSION = '2.2';
32  
33 use base qw (Tk::Derived Tk::HList);
34  
35 use constant DEFAULT_COLUMN_SEPARATOR => '|';
36 ########################################################################
37 Construct Tk::Widget 'DHList';
38  
39 # Class Variables
40 my ($DataStyles, $HeaderStyle);
41  
42 #---------------------------------------------
43 # internal Setup function
44 #---------------------------------------------
45 sub ClassInit
46 {
47 my ($class, $window) = @_;
48  
49 $class->SUPER::ClassInit($window);
50  
51 # Note these keyboard-Keys are only usable, if the widget gets 'focus'
52 $window->bind ($class, '<Control-Key-bracketleft>', [\&viewtype, 'withdata']);
53 $window->bind ($class, '<Control-Key-bracketright>', [\&viewtype, 'normal']);
54 # Update the view after mapping
55 $window->bind ($class, '<Map>', \&map_cb);
56 }
57  
58 #---------------------------------------------
59 # internal Setup function
60 #---------------------------------------------
61 sub CreateArgs
62 {
63 my ($class, $window, $args) = @_;
64  
65 # Convenience function - calculate coulncount from the headline
66 if ($args->{-headline} and not $args->{-columns}) {
67 my $columnseparator = $args->{-columnseparator} || DEFAULT_COLUMN_SEPARATOR();
68 my $pattern = quotemeta $columnseparator;
69 @_ = split(/$pattern/, $args->{-headline});
70 $args->{-columns} = scalar @_;
71 }
72  
73 # Necessarily Set/Patch Column-Count to accept a multi-purpose data-column
74 $args->{-columns} = 1 unless $args->{-columns};
75 $args->{-columns}++;
76
77 $class->SUPER::CreateArgs($window, $args);
78 }
79  
80 #---------------------------------------------
81 # internal Setup function
82 #---------------------------------------------
83 sub Populate
84 {
85 # Parameters
86 my ($this, $args) = @_;
87  
88 # Locals
89 my ($headline, $headerforeground, $headerbackground,$headerfont, $headerrelief,
90 $data_background, $datastyle, $headerstyle);
91  
92 $headline = delete $args->{-headline}; $args->{-header} = 1 if $headline; # Convenience
93 $headerforeground = delete $args->{-headerforeground};
94 $headerbackground = delete $args->{-headerbackground};
95 $headerfont = delete $args->{-headerfont} || 'Helvetica -10';
96 $headerrelief = delete $args->{-headerrelief} || 'groove';
97  
98 $data_background = delete $args->{-databackground};
99 $data_background = $this->cget ('-background') unless defined $data_background;
100
101 # Unique Style generation
102 $datastyle = delete $args->{-datastyle};
103 $datastyle = $this->toplevel->ItemStyle('text', -anchor => 'e', -background => $data_background) unless $datastyle;
104 $headerstyle = delete $args->{-headerstyle};
105 $headerstyle = $this->toplevel->ItemStyle('window', -padx => 0, -pady => 0) unless $headerstyle;
106  
107 # Check whether we want numeric sorting
108 $this->{m_numeric_primary_sort} = delete $args->{-numeric_primary_sort} || 0;
109 $this->{m_numeric_secondary_sort} = delete $args->{-numeric_secondary_sort} || 0;
110
111 # Reroute any size_call_back
112 my $sizecmd = delete $args->{-sizecmd} || sub { 1 };
113 $args->{-sizecmd} = [\&resize_cb, $this ];
114  
115 #INvoke Superclass fill func
116 $this->SUPER::Populate($args);
117  
118 $this->ConfigSpecs(
119 #default listbox options
120 -highlightthickness => [['SELF', 'PASSIVE'], 'HighlightThickness', 'highlightthickness', '2'],
121 -pady => [['SELF', 'PASSIVE'], 'pady', 'Pad', '0'],
122 -sizecmd_cb => ['CALLBACK',undef, undef, $sizecmd],
123 # new, additional optiona
124 -viewtype => ['METHOD', 'ViewType', 'viewType', undef],
125 -datastyle => [['SELF', 'PASSIVE'], 'DataStyle', 'datastyle', $datastyle],
126 -columnseparator => [['SELF', 'PASSIVE'], 'ColumnSeparator', 'columnseparator', DEFAULT_COLUMN_SEPARATOR],
127 -databackground => [['SELF','METHOD'], 'databackground', 'dataBackground', undef],
128 # define a compound setting to adress BG-changes to both, the normal AND the data column
129 -bg => [{-background => $this, -databackground => $this}, 'background', 'Background', undef],
130 );
131  
132 if ($headline) {
133 ### NOTE: For whatever reasons the filling of an appropriate Header-BG (and some ->cget())
134 ### will only work if it is done deferred ...
135 $this->afterIdle( sub {
136 my ($columnseparator, $pattern, $data_pos, @line, $col);
137 $columnseparator = $this->cget('-columnseparator');
138 $pattern = quotemeta $columnseparator;
139 $data_pos = $this->cget('-columns') - 1;
140 @line = split(/$pattern/, $headline, $data_pos);
141 if (@line) {
142 # Take care, if the headline is too short
143 if (@line < $data_pos+1) {
144 for ($col = @line; $col <= $data_pos; $col++) {
145 $line[$col] = '';
146 }
147 }
148 # Add a dummy Header Label for the DataColumn
149 $line[-1] = 'DATA'; $col = 0;
150  
151 # now add ALL columns to the header-line
152 foreach $pattern (@line) {
153 my $headerlabel = $this->Label( -text => $pattern, -font => $headerfont,
154 (-foreground => $headerforeground ? $headerforeground : $this->cget('-background')),
155 $headerbackground ? (-background => $headerbackground) : () );
156 $this->headerCreate($col++,
157 -style => $headerstyle,
158 -itemtype => 'window',
159 -widget => $headerlabel, -relief => $headerrelief,
160 $headerbackground ? (-headerbackground => $headerbackground) : () );
161 }
162 }
163 });
164 }
165
166 # Internal Presets
167 $this->{m_viewtype} = 'none';
168 $this->{m_backlist} = {};
169 $this->{m_index} = 0;
170 }
171  
172 #---------------------------------------------
173 # OVERRIDE: new ADD function
174 #---------------------------------------------
175 sub add
176 {
177 # Parameters
178 my ($this, $path, %args) = @_;
179  
180 # Locals
181 my ($data, $datastyle, @line, $col, $data_pos, $pattern);
182
183 # Do we have anything at all to insert ?
184 return unless defined $path;
185  
186 $this->{m_backlist}{$path} = {
187 args => { %args },
188 index => ++$this->{m_index},
189 };
190  
191 # Prepare the data and it's style
192 $data = delete $args{-data};
193 $datastyle = delete $args{-datastyle} || $this->cget('-datastyle');
194 # Some local shortcuts
195 $data_pos = $this->cget('-columns') - 1;
196 $pattern = quotemeta $this->cget('-columnseparator');
197 $col = 0;
198  
199 # Eventually split into additional columns
200 @line = split(/$pattern/, $args{-text}||'', $data_pos);
201  
202 # Create a new entry
203 $this->SUPER::add($path, -data => $data);
204 # now add ALL columns to the entry
205 foreach $pattern (@line) {
206 $args{-text} = $pattern;
207 if ($col > 0) {
208 delete $args{-image};
209 $args{-itemtype} = 'text';
210 }
211 $this->SUPER::itemCreate($path, $col++, %args);
212 }
213  
214 # and a trailing column for the data
215 $this->SUPER::itemCreate($path, $data_pos,
216 -itemtype => 'text',
217 -text => '' . ($data||''), # Note the first ''. which is necessary to cast the data ptr to text
218 -style => $datastyle,
219 );
220
221 # Install the 'normal view after we have something on the screen
222 $this->viewtype($this->{m_viewtype});
223 }
224  
225 #---------------------------------------------
226 # OVERRIDE: new DELETE function
227 #---------------------------------------------
228 sub delete
229 {
230 # Parameters
231 my ($this, $what, $path) = @_;
232  
233 if ($what eq 'all') {
234 # Clear the internal storage
235 $this->{m_index} = 0;
236 # Delete it
237 $this->SUPER::delete($what);
238 }
239 else {
240 # Delete it from internal storage list
241 delete $this->{m_backlist}{$path};
242 # Delete it
243 $this->SUPER::delete($what, $path);
244 }
245 }
246  
247 #---------------------------------------------
248 # ADD-ON: reordering function
249 #---------------------------------------------
250 sub reverse
251 {
252 # Parameters
253 my $this = shift;
254 $this->_rebuild_list('reverse');
255 }
256  
257 #---------------------------------------------
258 # ADD-ON: sorting function
259 #---------------------------------------------
260 sub sort
261 {
262 # Parameters
263 my ($this, $mode) = @_;
264  
265 # Locals
266 my ($sort_func, $backlist, $i, $path);
267  
268 # safety check
269 return unless $this->infoChildren;
270 $mode = 'ascending' unless $mode;
271  
272 # Shortcut for speed
273 $backlist = $this->{m_backlist};
274  
275 # sort it
276 if ($mode =~ /ascending/i) {
277 if ($mode =~ /data|secondary/i) {
278 if ($this->{m_numeric_secondary_sort}) {
279 $sort_func = sub { $backlist->{$a}{args}{-data} <=> $backlist->{$b}{args}{-data} };
280 }
281 else {
282 $sort_func = sub { $backlist->{$a}{args}{-data} cmp $backlist->{$b}{args}{-data} };
283 }
284 }
285 else {
286 if ($this->{m_numeric_primary_sort}) {
287 $sort_func = sub { $backlist->{$a}{args}{-text} <=> $backlist->{$b}{args}{-text} };
288 }
289 else {
290 $sort_func = sub { $backlist->{$a}{args}{-text} cmp $backlist->{$b}{args}{-text} };
291 }
292 }
293 }
294 elsif ($mode =~ /descending/i) {
295 if ($mode =~ /data|secondary/i) {
296 if ($this->{m_numeric_secondary_sort}) {
297 $sort_func = sub { $backlist->{$b}{args}{-data} <=> $backlist->{$a}{args}{-data} };
298 }
299 else {
300 $sort_func = sub { $backlist->{$b}{args}{-data} cmp $backlist->{$a}{args}{-data} };
301 }
302 }
303 else {
304 if ($this->{m_numeric_primary_sort}) {
305 $sort_func = sub { $backlist->{$b}{args}{-text} <=> $backlist->{$a}{args}{-text} };
306 }
307 else {
308 $sort_func = sub { $backlist->{$b}{args}{-text} cmp $backlist->{$a}{args}{-text} };
309 }
310 }
311 }
312 else {
313 return;
314 }
315
316 # sort it
317 foreach $path (sort $sort_func keys %$backlist) {
318 $backlist->{$path}{index} = ++$i;
319 }
320  
321 # apply sorting to the list
322 $this->_rebuild_list();
323 }
324  
325 #---------------------------------------------
326 # INTERNAL: rebuild list function
327 #---------------------------------------------
328 sub _rebuild_list
329 {
330 # Parameters
331 my ($this, $reverse_mode) = @_;
332  
333 # Locals
334 my ($path, $backlist, $sort_func);
335  
336 # safety check
337 return unless $this->infoChildren;
338  
339 # Store the backlist
340 $backlist = $this->{m_backlist};
341 $this->{m_backlist} = {};
342  
343 # Retrieve the current visability statets
344 foreach $path (keys %$backlist) {
345 $backlist->{$path}{hidden} = $this->infoHidden($path);
346 }
347  
348 # delete it
349 $this->delete('all');
350  
351 # Define a reverse-sort func
352 if ($reverse_mode) {
353 $sort_func = sub { $backlist->{$b}{index} <=> $backlist->{$a}{index} }
354 }
355 else { # just follow the indexing
356 $sort_func = sub { $backlist->{$a}{index} <=> $backlist->{$b}{index} }
357 }
358  
359 # Std/Reverse refill it
360 foreach $path (sort $sort_func keys %$backlist) {
361 $this->add($path, %{$backlist->{$path}{args}});
362 $this->hide('entry', $path) if $backlist->{$path}{hidden};
363 }
364 }
365  
366  
367 #---------------------------------------------
368 # ADD-ON: do add. background changes -
369 # optionally update the data background too
370 #---------------------------------------------
371 sub databackground
372 {
373 # Parameters
374 my ($this, $new_background) = @_;
375  
376 # Fetch the existing background
377 my $datastyle = $this->_cget('-datastyle');
378 my $background = $datastyle->cget('-background');
379 $datastyle->configure('-background', $new_background) if $new_background;
380 return $background;
381 }
382  
383  
384 #---------------------------------------------
385 # ADD-ON: trace any viewtype updates
386 # supported values are 'normal', 'withdata'
387 #---------------------------------------------
388 sub viewtype
389 {
390 # Parameters
391 my ($this, $viewtype) = @_;
392  
393 # if we're not in a cget we might consider changing the value
394 if (defined $viewtype) {
395 # allow only 'normal' & 'withdata'...
396 if ($viewtype =~ /withdata/i ) {
397 $viewtype = 'withdata';
398 }
399 else {
400 $viewtype = 'normal';
401 }
402 #print "internal viewtype is $this->{m_viewtype}\n";
403 if ($this->{m_viewtype} ne $viewtype) {
404 $this->{m_viewtype} = $viewtype;
405 $this->setup_view();
406 }
407 }
408 # needed for cget
409 return $this->{m_viewtype};
410 }
411  
412 #---------------------------------------------
413 # ADD-ON: setup_view: display function
414 #---------------------------------------------
415 sub setup_view
416 {
417 # Parameters
418 my $this = shift;
419  
420 # Locals
421 my (@bb, $needsize, $data_col, $col_size, $col);
422  
423 return unless $this->infoChildren;
424 $data_col = $this->cget('-columns') - 1;
425  
426 if ($this->{m_viewtype} eq 'withdata' ) {
427 $this->columnWidth($data_col, '');
428 $needsize = $this->columnWidth($data_col);
429 @bb = $this->infoBbox(($this->infoChildren)[0]);
430 if (@bb) {
431 $col_size = 0;
432 for ($col = 1; $col < $data_col; $col++) {
433 $col_size += $this->columnWidth($col);
434 }
435 $col_size = $bb[2] - $bb[0] - $col_size - $needsize;
436 $col_size = 20 if $col_size < 20;
437 $this->columnWidth(0, $col_size);
438 }
439 }
440 else {
441 $this->columnWidth($data_col, 0);
442 $this->columnWidth(0, '');
443 }
444 }
445  
446  
447 #---------------------------------------------
448 # ADD-ON: new resizxing function
449 #---------------------------------------------
450 sub resize_cb
451 {
452 # Parameters
453 my $this = shift;
454 # Locals
455 my (@bb, $needsize, $data_col, $col_size, $col);
456  
457 return unless $this->viewable;
458 return unless $this->infoChildren;
459  
460 # Adopt the column Widths
461 $this->setup_view();
462  
463 # invoke any given callback
464 $this->Callback(-sizecmd_cb => $this);
465 }
466  
467  
468 #---------------------------------------------
469 # CALLBACK: update the view (type 'normal')
470 # means Data-Column is invisible
471 #---------------------------------------------
472 sub map_cb
473 {
474 # Parameters
475 my $this = shift;
476 $this->setup_view();
477 }
478  
479 #---------------------------------------------
480 # ADD-ON: get Item and / or ItemData
481 # returns a scalar(first only) or an array
482 #---------------------------------------------
483 sub get_item
484 {
485 # Parameters
486 my ($this, $path) = @_;
487  
488 # get all information
489 my @items_out = $this->_get_item(3, $path);
490  
491 return wantarray ? @items_out : $items_out[0];
492 }
493 #---------------------------------------------
494 sub get_item_text
495 {
496 # Parameters
497 my ($this, $path) = @_;
498  
499 # get all information
500 my @items_out = $this->_get_item(1, $path);
501  
502 return wantarray ? @items_out : $items_out[0];
503 }
504 #---------------------------------------------
505 sub get_item_value
506 {
507 # Parameters
508 my ($this, $path) = @_;
509  
510 # get all information
511 my @items_out = $this->_get_item(2, $path);
512  
513 return wantarray ? @items_out : $items_out[0];
514 }
515 #---------------------------------------------
516 sub _get_item
517 {
518 # Parameters
519 my ($this, $mode, $path) = @_;
520  
521 # Locals
522 my (@items_out);
523  
524 if ($mode & 1) {
525 push @items_out, $this->{m_backlist}{$path}{args}{-text};
526 }
527 if ($mode & 2) {
528 #push @items_out, $this->infoData($path);
529 ### NOTE: IF we use above code an existing REF in data is LOST! MKr. 2013-04-08
530 my $data = $this->infoData($path);
531 push @items_out, $data;
532 }
533
534 return wantarray ? @items_out : $items_out[0];
535 }
536  
537 #---------------------------------------------
538 # ADD-ON: Get Current Selected Text AND/OR associated Data
539 #---------------------------------------------
540 sub getcurselection
541 {
542 # Parameters
543 my $this = shift;
544  
545 # get all information
546 my @items_out = $this->_getcurselection(3);
547  
548 return wantarray ? @items_out : $items_out[0];
549 }
550 #---------------------------------------------
551 sub getcurselection_text
552 {
553 # Parameters
554 my $this = shift;
555  
556 # get all information
557 my @items_out = $this->_getcurselection(1);
558  
559 return wantarray ? @items_out : $items_out[0];
560 }
561 #---------------------------------------------
562 sub getcurselection_value
563 {
564 # Parameters
565 my $this = shift;
566  
567 # get all information
568 my @items_out = $this->_getcurselection(2);
569  
570 return wantarray ? @items_out : $items_out[0];
571 }
572  
573 #---------------------------------------------
574 sub _getcurselection
575 {
576 # Parameters
577 my ($this, $mode) = @_;
578  
579 # Locals
580 my (@items_out, @selitems, $path);
581  
582 # get index information
583 @selitems = $this->infoSelection;
584 foreach $path (@selitems) {
585 push @items_out, $this->_get_item($mode, $path);
586 }
587 return wantarray ? @items_out : $items_out[0];
588 }
589  
590  
591 ########################################################################
592 1;
593 __END__