Outlines.pm untested

filter: Statementstmt 0.0% 0 / 1379 Branchbran 0.0% 0 / 712 Conditioncond 0.0% 0 / 247 MC/DCmcdc 0.0% 0 / 142 Subroutinesub 0.0% 0 / 124 Podpod 0.0% 0 / 107 totaltotal 0.0% 0 / 2711 CC 357 SCAR 54.6
CC
357
Cov
0%
on_keydown
1406.0
on_paint
702.0
on_mousemove
420.0
CRAP 234.9
1 ################################################################################
2 # This is CodeManager
3 # Copyright 2009-2013 by Waldemar Biernacki
4 # http://codemanager.sao.pl\n" .
5 #
6 # License statement:
7 #
8 # This program/library is free software; you can redistribute it
9 # and/or modify it under the same terms as Perl itself.
10 #
11 # Last modified (DMYhms): 14-01-2013 07:17:03.
12 ################################################################################
13  
14 use strict;
15 use warnings;
16  
17 use Cwd;
18 use Prima qw(Classes IntUtils StdBitmap);
19  
20 use File::Copy;
21 use File::Path qw(make_path remove_tree);
22 use File::Copy::Recursive qw(fcopy rcopy dircopy fmove rmove dirmove);
23  
24 package Prima::CodeManager::OutlineViewer;
25 use vars qw(@ISA @images @imageSize);
26 @ISA = qw(Prima::Widget Prima::MouseScroller Prima::GroupScroller);
27  
28 use constant DATA => 0;
29 use constant DOWN => 1;
30 use constant EXPANDED => 2;
31 use constant WIDTH => 3;
32 use constant SELECTED => 4;
33  
34 # node record:
35 # user fields:
36 # 0 : item text of ID
37 # 1 : node subreference ( undef if none)
38 # 2 : expanded flag
39 # private fields
40 # 3 : item width
41 # 4 : selected flag
42  
43 {
44 my %RNT = (
45 %{Prima::Widget-> notification_types()},
46 SelectItem => nt::Default,
47 DrawItem => nt::Action,
48 Stringify => nt::Action,
49 MeasureItem => nt::Action,
50 Expand => nt::Action,
51 DragItem => nt::Default,
52 );
53  
54 sub notification_types { return \%RNT; }
55 }
56  
57 sub profile_default
58 {
59 my $def = $_[ 0]-> SUPER::profile_default;
60 my %prf = (
61 autoHeight => 1,
62 autoHScroll => 1,
63 autoVScroll => 1,
64 borderWidth => 2,
65 extendedSelect => 0,
66 dragable => 1,
67 hScroll => 0,
68 focusedItem => -1,
69 indent => 12,
70 itemHeight => $def-> {font}-> {height},
71 items => [],
72 multiSelect => 0,
73 topItem => 0,
74 offset => 0,
75 scaleChildren => 0,
76 selectable => 1,
77 showItemHint => 1,
78 vScroll => 1,
79 widgetClass => wc::ListBox,
80 );
81 @$def{keys %prf} = values %prf;
82 return $def;
83 }
84  
85 sub profile_check_in
86 {
87 my ( $self, $p, $default) = @_;
88 $self-> SUPER::profile_check_in( $p, $default);
89 $p-> {autoHeight} = 0 if exists $p-> {itemHeight} && !exists $p-> {autoHeight};
90 $p-> {autoHScroll} = 0 if exists $p-> {hScroll};
91 $p-> {autoVScroll} = 0 if exists $p-> {vScroll};
92 $p-> {multiSelect} = 1 if
93 exists $p-> { extendedSelect}
94 && $p-> {extendedSelect}
95 && !exists $p-> {multiSelect};
96 $self-> {darkColor} = $p-> {darkColor} || $self-> {backColor};
97  
98 }
99  
100 use constant STACK_FRAME => 64;
101  
102 sub init
103 {
104 my $self = shift;
105 unless ( @images) {
106 my $i = 0;
107 #this is the original method:
108 # for ( sbmp::OutlineCollaps, sbmp::OutlineExpand) {
109 # $images[$i++] = Prima::StdBitmap::image( $_ , 'sysimage.gif' );
110 # }
111  
112 #this is when you want to use your own images
113 # $images[0] = Prima::Image-> new( type => im::RGB, );
114 # $images[0]->load( 'minus.png' );
115 # $images[1] = Prima::Image-> new( type => im::RGB, );
116 # $images[1]->load( 'plus.png' );
117  
118 #this is a method of using images text represention written into a module
119 $images[0] = Prima::Image-> new( type => 24, maskColor => 1,);
120 $images[0] = Prima::CodeManager::Image::make_image( 'minus.png' );
121 $images[1] = Prima::Image-> new( type => 24, maskColor => 1,);
122 $images[1] = Prima::CodeManager::Image::make_image( 'plus.png' );
123  
124  
125 if ( $images[0]) {
126 @imageSize = $images[0]-> size;
127 } else {
128 @imageSize = (0,0);
129 }
130 }
131 for ( qw( topItem focusedItem))
132 { $self-> {$_} = -1; }
133 for ( qw( autoHScroll autoVScroll scrollTransaction dx dy hScroll vScroll
134  
135 offset count autoHeight borderWidth multiSelect extendedSelect
136 rows maxWidth hintActive showItemHint dragable))
137 { $self-> {$_} = 0; }
138 for ( qw( itemHeight indent))
139 { $self-> {$_} = 1; }
140 $self-> {items} = [];
141 my %profile = $self-> SUPER::init(@_);
142 $self-> setup_indents;
143 for ( qw( autoHScroll autoVScroll hScroll vScroll offset itemHeight autoHeight borderWidth
144 indent items focusedItem topItem showItemHint dragable multiSelect extendedSelect))
145 { $self-> $_( $profile{ $_}); }
146  
147 $self-> reset;
148 $self-> reset_scrolls;
149 return %profile;
150 }
151  
152 # iterates throughout the item tree, calling given sub for each item.
153 # sub's parameters are:
154 # 0 - current item record pointer
155 # 1 - parent item record pointer, undef if top-level
156 # 2 - index of the current item into $parent->[1] array
157 # 3 - index of the current item into items
158 # 4 - level of the item ( 0 is topmost)
159 # 5 - boolean, whether the current item is last item (e.g.$parent->[1]->[-1] == $parent->[1]->[$_[5]]).
160 # 6 - index of the current item if visible; undef otherwise. Equal to [3] if $full is 0.
161 #
162 # $full - if 0, iterates only expanded ( visible) items, if 1 - all items into the tree
163  
164 sub iterate
165 {
166 my ( $self, $sub, $full) = @_;
167 my $position = 0;
168 my $visible = 1;
169 my $visual_position = 0;
170 my $traverse;
171 $traverse = sub {
172 my ( $current, $parent, $index, $level, $lastChild) = @_;
173 return $current if $sub-> ( $current, $parent, $index, $position, $level,
174  
175 $lastChild, $visible ? $visual_position : undef);
176 $position++;
177 $level++;
178 $visual_position++ if $visible;
179 if ( $current-> [DOWN] && ( $full || $current-> [EXPANDED])) {
180 my $c = scalar @{$current-> [DOWN]};
181 my $i = 0;
182 my $dive;
183 if ( $visible && $full && !$current-> [EXPANDED]) {
184 $visible = 0;
185 $dive = 1;
186 }
187 for ( @{$current-> [DOWN]}) {
188 my $ret = $traverse-> ( $_, $current, $i++, $level, --$c ? 0 : 1);
189 return $ret if $ret;
190 }
191 $visible = 1 if $dive;
192 };
193 0;
194 };
195 my $c = scalar @{$self-> {items}};
196 my $i = 0;
197 for ( @{$self-> {items}}) {
198 my $ret = $traverse-> ( $_, undef, $i++, 0, --$c ? 0 : 1);
199 undef $traverse, return $ret if $ret;
200 }
201 undef $traverse;
202 }
203  
204 sub adjust
205 {
206 my ( $self, $index, $action) = @_;
207 return unless defined $index;
208 my ($node, $lev) = $self-> get_item( $index);
209 return unless $node;
210 return unless $node-> [DOWN];
211 return if $node-> [EXPANDED] == $action;
212 $self-> notify(q(Expand), $node, $action);
213 $node-> [EXPANDED] = $action;
214 my $c = $self-> {count};
215 my $f = $self-> {focusedItem};
216 $self-> reset_tree;
217  
218 my ( $ih, @a) = ( $self-> {itemHeight}, $self-> get_active_area );
219 $self-> scroll(
220 0, ( $c - $self-> {count}) * $ih,
221 clipRect => [ @a[0..2], $a[3] - $ih * ( $index - $self-> {topItem} + 1)]
222 );
223 $self-> invalidate_rect(
224 $a[0], $a[3] - ( $index - $self-> {topItem} + 1) * $ih,
225 $a[2], $a[3] - ( $index - $self-> {topItem}) * $ih
226 );
227 $self-> {doingExpand} = 1;
228 if ( $c > $self-> {count} && $f > $index) {
229 if ( $f <= $index + $c - $self-> {count}) {
230 $self-> focusedItem( $index);
231 } else {
232 $self-> focusedItem( $f + $self-> {count} - $c);
233 }
234 } elsif ( $c < $self-> {count} && $f > $index) {
235 $self-> focusedItem( $f + $self-> {count} - $c);
236 }
237 $self-> {doingExpand} = 0;
238 my ($ix,$l) = $self-> get_item( $self-> focusedItem);
239  
240 $self-> update_tree;
241 $self-> reset_scrolls;
242  
243 # $self-> offset( $self-> {offset} + $self-> {indent})
244 # if $action && $c != $self-> {count};
245 }
246  
247 sub expand_all
248 {
249 my ( $self, $node) = @_;
250 $node = [ 0, $self-> {items}, 1] unless $node;
251 $self-> {expandAll}++;
252 if ( $node-> [DOWN]) {
253 # - light version of adjust
254 unless ( $node-> [EXPANDED]) {
255 $node-> [EXPANDED] = 1;
256 $self-> notify(q(Expand), $node, 1);
257 }
258 $self-> expand_all( $_) for @{$node-> [DOWN]};
259 };
260 return if --$self-> {expandAll};
261 delete $self-> {expandAll};
262 $self-> reset_tree;
263 $self-> update_tree;
264 $self-> repaint;
265 $self-> reset_scrolls;
266 }
267  
268 sub on_paint
269 {
270 my ( $self, $canvas) = @_;
271 my @size = $canvas-> size;
272 my @clr = $self-> enabled ?
273 ( $self-> color, $self-> backColor) :
274 ( $self-> disabledColor, $self-> disabledBackColor);
275 my ( $ih, $iw, $indent, $foc, @a) = (
276 $self-> { itemHeight}, $self-> { maxWidth},
277 $self-> {indent}, $self-> {focusedItem}, $self-> get_active_area( 1, @size));
278 my $i;
279 my $j;
280 my $locWidth = $a[2] - $a[0] + 1;
281 my @clipRect = $canvas-> clipRect;
282 if (
283 $clipRect[0] > $a[0] &&
284 $clipRect[1] > $a[1] &&
285 $clipRect[2] < $a[2] &&
286 $clipRect[3] < $a[3]
287 ) {
288 $canvas-> clipRect( @a);
289 $canvas-> color( $clr[1]);
290 $canvas-> bar( 0, 0, @size);
291 } else {
292 $self-> draw_border( $canvas, $clr[1], @size);
293 $canvas-> clipRect( @a);
294 }
295  
296 my ( $topItem, $rows) = ( $self-> {topItem}, $self-> {rows});
297 my $lastItem = $topItem + $rows + 1;
298 my $timin = $topItem;
299 $timin += int(( $a[3] - $clipRect[3]) / $ih) if $clipRect[3] < $a[3];
300  
301 if ( $clipRect[1] >= $a[1]) {
302 my $y = $a[3] - $clipRect[1] + 1;
303 $lastItem = $topItem + int($y / $ih) + 1;
304 }
305 $lastItem = $self-> {count} - 1 if $lastItem > $self-> {count} - 1;
306 my $firstY = $a[3] + 1 + $ih * $topItem;
307 my $lineY = $a[3] + 1 - $ih * ( 1 + $timin - $topItem);
308 my $dyim = int(( $ih - $imageSize[1]) / 2) + 1;
309 my $dxim = int( $imageSize[0] / 2);
310  
311 my @lines;
312 my @marks;
313 my @texts;
314  
315 my $deltax = - $self-> {offset} + ($indent/2) + $a[0];
316 $canvas-> set(
317 fillPattern => fp::SimpleDots,
318 color => cl::White,
319 backColor => cl::Black,
320 );
321  
322 my ($array, $idx, $lim, $level) = ([['root'],$self-> {items}], 0, scalar @{$self-> {items}}, 0);
323 my @stack;
324 my $position = 0;
325  
326 # preparing stack
327 $i = int(( $timin + 1) / STACK_FRAME) * STACK_FRAME - 1;
328  
329 # $i = int( $timin / STACK_FRAME) * STACK_FRAME - 1;
330  
331 if ( $i >= 0) {
332 # if ( $i > 0) {
333 $position = $i;
334 $j = int(( $timin + 1) / STACK_FRAME) - 1;
335 # $j = int( $timin / STACK_FRAME) - 1;
336 $i = $self-> {stackFrames}-> [$j];
337 if ( $i) {
338 my $k;
339 for ( $k = 0; $k < scalar @{$i} - 1; $k++) {
340 $idx = $i-> [$k] + 1;
341 $lim = scalar @{$array-> [DOWN]};
342 push( @stack, [ $array, $idx, $lim]);
343 $array = $array-> [1]-> [$idx - 1];
344 }
345 $idx = $$i[$k];
346 $lim = scalar @{$array-> [DOWN]};
347 $level = scalar @$i - 1;
348 $i = $self-> {lineDefs}-> [$j];
349 $lines[$k] = $$i[$k] while $k--;
350 }
351 }
352  
353 # following loop is recursive call turned inside-out -
354 # so we can manipulate with stack
355 my @levels;
356 if ( $position <= $lastItem) {
357 while (1) {
358 my $node = $array-> [DOWN]-> [$idx++];
359 my $lastChild = $idx == $lim;
360  
361 # outlining part
362 my $l = int(( $level + 0.5) * $indent) + $deltax + ( 12 - $indent) * 0.00000;
363 $levels[$position]=$l;
364 if ( $lastChild) {
365 if ( defined $lines[ $level]) {
366 $canvas-> bar(
367 $l, $firstY - $ih * $lines[ $level],
368 $l, $firstY - $ih * ( $position + 0.5))
369 if $position >= $timin;
370 $lines[ $level] = undef;
371 } elsif ( $position > 0) {
372 # first and last
373 $canvas-> bar(
374 $l, $firstY - $ih * ( $position - 0.5),
375 $l, $firstY - $ih * ( $position + 0.5))
376 }
377 } elsif ( !defined $lines[$level]) {
378 $lines[$level] = $position ? $position - 0.5 : 0.5;
379 }
380 if ( $position >= $timin) {
381 $canvas-> bar( $l + 1, $lineY + $ih/2, $l + $indent - 1, $lineY + $ih/2);
382 if ( defined $node-> [DOWN]) {
383 my $i = $images[($node-> [EXPANDED] == 0) ? 1 : 0];
384 push( @marks, [$l - $dxim, $lineY + $dyim, $i]) if $i;
385 };
386 push ( @texts, [ $node, $l + $indent * 1.5, $lineY,
387 $l + $indent * 1.5 + $node-> [WIDTH] - 1, $lineY + $ih - 1,
388 $position,
389  
390 $self-> {multiSelect} ? $node-> [SELECTED] : ($foc == $position),
391 $foc == $position]);
392 $lineY -= $ih;
393 }
394  
395 last if $position >= $lastItem;
396  
397 # recursive part
398 $position++;
399  
400 if ( $node-> [DOWN] && $node-> [EXPANDED] && scalar @{$node-> [DOWN]}) {
401 $level++;
402 push ( @stack, [ $array, $idx, $lim]);
403 $idx = 0;
404 $array = $node;
405 $lim = scalar @{$node-> [DOWN]};
406 next;
407 }
408 while ( $lastChild) {
409 last unless $level--;
410 ( $array, $idx, $lim) = @{pop @stack};
411 $lastChild = $idx == $lim;
412 }
413 }}
414  
415 # drawing line ends
416 $i = 0;
417 for ( @lines) {
418 $i++;
419 next unless defined $_;
420 my $l = ( $i - 0.5) * $indent + $deltax;;
421 $canvas-> bar( $l, $firstY - $ih * $_, $l, 0);
422 }
423  
424 $canvas-> set(
425 fillPattern => fp::Solid,
426 color => $clr[0],
427 backColor => $clr[1],
428 );
429 if ( $self-> {darkColor} != $clr[0] ) {
430 for ( my $y = $topItem; $y <= $lastItem; $y++ ) {
431 if ( $y % 2 == 0 ) {
432 $canvas-> color( $self-> {darkColor} );
433 $canvas-> bar (
434 $levels[$y] + $indent + $self->{itemHeight} / 2,
435 $a[3] - $ih * ( $y - $topItem + 1 ) + 1,
436 $a[2],
437 $a[3] - $ih * ( $y - $topItem )
438 );
439 $canvas-> color( $clr[0] );
440 }
441 }
442 }
443 $canvas-> put_image( @$_) for @marks;
444 $self-> draw_items( $canvas, \@texts );
445 }
446  
447 sub on_size
448 {
449 my $self = $_[0];
450 $self-> reset;
451 $self-> reset_scrolls;
452 }
453  
454 sub on_fontchanged
455 {
456 my $self = $_[0];
457 $self-> itemHeight( $self-> font-> height), $self-> {autoHeight} = 1
458  
459 if $self-> { autoHeight};
460 $self-> calibrate;
461 }
462  
463 sub point2item
464 {
465 my ( $self, $y, $h) = @_;
466 my $i = $self-> {indents};
467 $h = $self-> height unless defined $h;
468 return $self-> {topItem} - 1 if $y >= $h - $$i[3];
469 return $self-> {topItem} + $self-> {rows} if $y <= $$i[1];
470 $y = $h - $y - $$i[3];
471 return $self-> {topItem} + int( $y / $self-> {itemHeight});
472 }
473  
474 sub on_mousedown
475 {
476 my ( $self, $btn, $mod, $x, $y) = @_;
477  
478 my $bw = $self-> { borderWidth};
479 my @size = $self-> size;
480 $self-> clear_event;
481 my ($o,$i,@a) = ( $self-> {offset}, $self-> {indent}, $self-> get_active_area(0, @size));
482 return if $btn != mb::Left;
483 return if
484  
485 defined $self-> {mouseTransaction} ||
486 $y < $a[1] ||
487 $y >= $a[3] ||
488 $x < $a[0] + ( 12 - $self->{indent}) * 0.00000 ||
489 $x >= $a[2] + ( 12 - $self->{indent}) * 0.00000 ;
490  
491 my $item = $self-> point2item( $y, $size[1]);
492 my ( $rec, $lev) = $self-> get_item( $item);
493  
494 if (
495 $rec &&
496 ( $x >= ( 1 + $lev) * $i + $a[0] - $o - $imageSize[0] / 2 + ( 12 - $self->{indent}) * 0.00000 ) &&
497 ( $x < ( 1 + $lev) * $i + $a[0] - $o + $imageSize[0] / 2 + ( 12 - $self->{indent}) * 0.00000 )
498 ) {
499 $self-> adjust( $item, $rec-> [2] ? 0 : 1) if $rec-> [1];
500 return;
501 }
502  
503 my $foc = $item >= 0 ? $item : 0;
504 if ( $self-> {multiSelect}) {
505 if ( $self-> {extendedSelect}) {
506 if ($mod & km::Shift) {
507 my $foc = $self-> focusedItem;
508 return $self-> selectedItems(( $foc < $item) ? [$foc..$item] : [ $item..$foc]);
509 } elsif ( $mod & km::Ctrl) {
510 return $self-> toggle_item( $item);
511 }
512 $self-> {anchor} = $item;
513 $self-> selectedItems([$foc]);
514 } elsif ( $mod & (km::Ctrl||km::Shift)) {
515 return $self-> toggle_item( $item);
516 }
517 }
518  
519 $self-> {mouseTransaction} =
520  
521 (( $mod & ( km::Alt | ($self-> {multiSelect} ? 0 : km::Ctrl))) && $self-> {dragable}) ? 2 : 1;
522 $self-> focusedItem( $item >= 0 ? $item : 0);
523 $self-> {mouseTransaction} = 1 if $self-> focusedItem < 0;
524 if ( $self-> {mouseTransaction} == 2) {
525 $self-> {dragItem} = $self-> focusedItem;
526 $self-> {mousePtr} = $self-> pointer;
527 $self-> pointer( cr::Move);
528 }
529 $self-> capture(1);
530 }
531  
532 sub on_mouseclick
533 {
534 my ( $self, $btn, $mod, $x, $y, $dbl) = @_;
535 $self-> clear_event;
536 return if $btn != mb::Left || !$dbl;
537 my $bw = $self-> { borderWidth};
538 my @size = $self-> size;
539 my $item = $self-> point2item( $y, $size[1]);
540 my ($o,$i) = ( $self-> {offset}, $self-> {indent});
541 my ( $rec, $lev) = $self-> get_item( $item);
542 if (
543 $rec &&
544 ( $x >= ( 1 + $lev) * $i + $self-> {indents}-> [0] - $o - $imageSize[0] / 2 ) &&
545 ( $x < ( 1 + $lev) * $i + $self-> {indents}-> [0] - $o + $imageSize[0] / 2 )
546 ) {
547 $self-> adjust( $item, $rec-> [EXPANDED] ? 0 : 1) if $rec-> [DOWN];
548 return;
549 }
550 $self-> notify( q(Click)) if $self-> {count};
551 }
552  
553 sub makehint
554 {
555 my ( $self, $show, $itemid) = @_;
556 return if !$show && !$self-> {hintActive};
557 if ( !$show) {
558 $self-> {hinter}-> hide;
559 $self-> {hintActive} = 0;
560 return;
561 }
562 return if defined $self-> {unsuccessfullId} && $self-> {unsuccessfullId} == $itemid;
563  
564 return unless $self-> {showItemHint};
565  
566 my ( $item, $lev) = $self-> get_item( $itemid);
567 unless ( $item) {
568 $self-> makehint(0);
569 return;
570 }
571  
572 my $w = $self-> get_item_width( $item);
573 my @a = $self-> get_active_area;
574 my $ofs = ( $lev + 2.5) * $self-> {indent} - $self-> {offset} + $self-> {indents}-> [0];
575  
576 if ( $w + $ofs <= $a[2] - 12) {
577 $self-> makehint(0);
578 return;
579 }
580  
581 $self-> {unsuccessfullId} = undef;
582  
583 unless ( $self-> {hinter}) {
584 $self-> {hinter} = $self-> insert( Widget =>
585 # $self-> {hinter} = $self-> insert( Label =>
586 clipOwner => 0,
587 selectable => 0,
588 ownerColor => 1,
589 # backColor => 0xffff00,
590 ownerBackColor => 1,
591 ownerFont => 1,
592 visible => 0,
593 height => $self-> {itemHeight},
594 name => 'Hinter',
595 delegations => [qw(Paint MouseDown MouseLeave)],
596 );
597 }
598 $self-> {hintActive} = 1;
599 $self-> {hinter}-> {id} = $itemid;
600 $self-> {hinter}-> {node} = $item;
601 my @org = $self-> client_to_screen(0,0);
602 $self-> {hinter}-> set(
603 origin => [
604 $org[0] + $ofs - 2,
605 $org[1] + $self-> height - $self-> {indents}-> [3] -
606 $self-> {itemHeight} * ( $itemid - $self-> {topItem} + 1),
607 ],
608 width => $w + 4 + 12,
609 text => $self-> get_item_text( $item ),
610 visible => 1,
611 );
612 $self-> {hinter}-> bring_to_front;
613 $self-> {hinter}-> repaint;
614 }
615  
616 sub Hinter_Paint
617 {
618 my ( $owner, $self, $canvas) = @_;
619 my $c = $self-> color;
620 $canvas-> color( $self-> backColor);
621 my @sz = $canvas-> size;
622 $canvas-> bar( 0, 0, @sz);
623 $canvas-> color( $c);
624 $canvas-> rectangle( 0, 0, $sz[0] - 1, $sz[1] - 1);
625 my @rec = ([ $self-> {node}, 2, 0,
626 $sz[0] - 3, $sz[1] - 1, 0, 0
627 ]);
628 $owner-> draw_items( $canvas, \@rec);
629 }
630  
631 sub Hinter_MouseDown
632 {
633 my ( $owner, $self, $btn, $mod, $x, $y) = @_;
634 $owner-> makehint(0);
635 my @ofs = $owner-> screen_to_client( $self-> client_to_screen( $x, $y));
636 $owner-> mouse_down( $btn, $mod, @ofs);
637 $owner-> {unsuccessfullId} = $self-> {id};
638 }
639  
640 sub Hinter_MouseLeave
641 {
642 $_[0]-> makehint(0);
643 }
644  
645 sub on_mousemove
646 {
647 my ( $self, $mod, $x, $y) = @_;
648 my @size = $self-> size;
649 my @a = $self-> get_active_area( 0, @size);
650 if ( !defined $self-> {mouseTransaction} && $self-> {showItemHint}) {
651 my $item = $self-> point2item( $y, $size[1]);
652 my ( $rec, $lev) = $self-> get_item( $item);
653 if (
654  
655 !$rec ||
656  
657 ( $x < -$self-> {offset} + ($lev + 2) * $self-> {indent} + $self-> {indents}-> [0])
658 ) {
659 $self-> makehint( 0);
660 return;
661 }
662 if (( $y >= $a[3]) || ( $y <= $a[1] + $self-> {itemHeight} / 2)) {
663 $self-> makehint( 0);
664 return;
665 }
666 $y = $a[3] - $y;
667 $self-> makehint( 1, $self-> {topItem} + int( $y / $self-> {itemHeight}));
668 return;
669 }
670 my $item = $self-> point2item( $y, $size[1]);
671 if ( $y >= $a[3] || $y < $a[1] || $x >= $a[2] || $x < $a[0])
672 {
673 $self-> scroll_timer_start unless $self-> scroll_timer_active;
674 return unless $self-> scroll_timer_semaphore;
675 $self-> scroll_timer_semaphore(0);
676 } else {
677 $self-> scroll_timer_stop;
678 }
679  
680 if ( $self-> {multiSelect} && $self-> {extendedSelect} && exists $self-> {anchor})
681 {
682 my ( $a, $b, $c) = ( $self-> {anchor}, $item, $self-> {focusedItem});
683 my $globSelect = 0;
684 if (( $b <= $a && $c > $a) || ( $b >= $a && $c < $a)) {
685  
686 $globSelect = 1
687 } elsif ( $b > $a) {
688 if ( $c < $b) { $self-> add_selection([$c + 1..$b], 1) }
689 elsif ( $c > $b) { $self-> add_selection([$b + 1..$c], 0) }
690 else { $globSelect = 1 }
691 } elsif ( $b < $a) {
692 if ( $c < $b) { $self-> add_selection([$c..$b], 0) }
693 elsif ( $c > $b) { $self-> add_selection([$b..$c], 1) }
694 else { $globSelect = 1 }
695 } else {
696  
697 $globSelect = 1
698  
699 }
700  
701 if ( $globSelect ) {
702 ( $a, $b) = ( $b, $a) if $a > $b;
703 $self-> selectedItems([$a..$b]);
704 }
705 }
706  
707 $self-> focusedItem( $item >= 0 ? $item : 0);
708 $self-> offset( $self-> {offset} + 5 * (( $x < $a[0]) ? -1 : 1))
709  
710 if $x >= $a[2] || $x < $a[0];
711 }
712  
713 sub on_mouseup
714 {
715 my ( $self, $btn, $mod, $x, $y) = @_;
716 return if $btn != mb::Left;
717 return unless defined $self-> {mouseTransaction};
718  
719 my @dragnotify;
720 if ( $self-> {mouseTransaction} == 2) {
721 $self-> pointer( $self-> {mousePtr});
722 my $fci = $self-> focusedItem;
723 @dragnotify = ($self-> {dragItem}, $fci) unless $fci == $self-> {dragItem};
724 }
725 delete $self-> {mouseTransaction};
726 delete $self-> {mouseHorizontal};
727  
728 $self-> capture(0);
729 $self-> clear_event;
730 $self-> notify(q(DragItem), @dragnotify) if @dragnotify;
731 }
732  
733 sub on_mousewheel
734 {
735 my ( $self, $mod, $x, $y, $z) = @_;
736 $z = int( $z/120);
737 $z *= $self-> {rows} if $mod & km::Ctrl;
738 my $newTop = $self-> topItem - $z;
739 my $maxTop = $self-> {count} - $self-> {rows};
740 $self-> topItem( $newTop > $maxTop ? $maxTop : $newTop);
741 $self-> repaint;
742 }
743  
744 sub on_enable { $_[0]-> repaint; }
745 sub on_disable { $_[0]-> repaint; }
746  
747 sub on_leave
748 {
749 my $self = $_[0];
750 if ( $self-> {mouseTransaction}) {
751 $self-> capture(0) if $self-> {mouseTransaction};
752 $self-> {mouseTransaction} = undef;
753 }
754 }
755  
756 sub on_keydown
757 {
758 my ( $self, $code, $key, $mod) = @_;
759 return if $mod & km::DeadKey;
760  
761 $mod &= ( km::Shift|km::Ctrl|km::Alt);
762 $self-> notify(q(MouseUp),0,0,0) if defined $self-> {mouseTransaction};
763  
764 return unless $self-> {count};
765  
766 if (
767 ( $key == kb::NoKey) &&
768  
769 ( $code >= ord(' '))
770 ) {
771 if ( chr($code) eq '+') {
772 $self-> adjust( $self-> {focusedItem}, 1);
773 $self-> clear_event;
774 return;
775 } elsif ( chr($code) eq '-') {
776 my ( $item, $lev) = $self-> get_item( $self-> {focusedItem});
777 if ( $item-> [DOWN] && $item-> [EXPANDED]) {
778 $self-> adjust( $self-> {focusedItem}, 0);
779 $self-> clear_event;
780 return;
781 } elsif ( $lev > 0) {
782 my $i = $self-> {focusedItem};
783 my ( $par, $parlev) = ( $item, $lev);
784 ( $par, $parlev) = $self-> get_item( --$i) while $parlev != $lev - 1;
785 $self-> adjust( $i, 0);
786 $self-> clear_event;
787 return;
788 }
789 }
790  
791 if ( !($mod & ~km::Shift)) {
792 my $i;
793 my ( $c, $hit, $items) = ( lc chr $code, undef, $self-> {items});
794 for ( $i = $self-> {focusedItem} + 1; $i < $self-> {count}; $i++)
795 {
796 my $fc = substr( $self-> get_index_text($i), 0, 1);
797 next unless defined $fc;
798 $hit = $i, last if lc $fc eq $c;
799 }
800 unless ( defined $hit) {
801 for ( $i = 0; $i < $self-> {focusedItem}; $i++) {
802 my $fc = substr( $self-> get_index_text($i), 0, 1);
803 next unless defined $fc;
804 $hit = $i, last if lc $fc eq $c;
805 }
806 }
807 if ( defined $hit) {
808 $self-> focusedItem( $hit);
809 $self-> clear_event;
810 return;
811 }
812 }
813 return;
814 }
815  
816 if ( scalar grep { $key == $_ } (
817 kb::Left,kb::Right,kb::Up,kb::Down,kb::Home,kb::End,kb::PgUp,kb::PgDn
818 )) {
819 my $doSelect = 0;
820 my $newItem = $self-> {focusedItem};
821 if (
822  
823 $mod == 0 ||
824  
825 (
826 ( $mod & km::Shift) &&
827  
828 $self-> {multiSelect} &&
829  
830 $self-> { extendedSelect}
831 )
832 ) {
833 my $pgStep = $self-> {rows} - 1;
834 $pgStep = 1 if $pgStep <= 0;
835 if ( $key == kb::Up) { $newItem--; };
836 if ( $key == kb::Down) { $newItem++; };
837 if ( $key == kb::Home) { $newItem = $self-> {topItem} };
838 if ( $key == kb::End) { $newItem = $self-> {topItem} + $pgStep; };
839 if ( $key == kb::PgDn) { $newItem += $pgStep };
840 if ( $key == kb::PgUp) { $newItem -= $pgStep};
841 $doSelect = $mod & km::Shift;
842 }
843  
844 if (
845 ( $mod & km::Ctrl) ||
846 (
847 (( $mod & ( km::Shift|km::Ctrl))==(km::Shift|km::Ctrl)) &&
848  
849 $self-> {multiSelect} &&
850  
851 $self-> { extendedSelect}
852 )
853 ) {
854 if ( $key == kb::PgUp || $key == kb::Home) { $newItem = 0};
855 if ( $key == kb::PgDn || $key == kb::End) { $newItem = $self-> {count} - 1};
856 $doSelect = $mod & km::Shift;
857 }
858  
859 if ( $doSelect ) {
860 my ( $a, $b) = (
861  
862 defined $self-> {anchor} ? $self-> {anchor} : $self-> {focusedItem},
863  
864 $newItem
865 );
866 ( $a, $b) = ( $b, $a) if $a > $b;
867 $self-> selectedItems([$a..$b]);
868 $self-> {anchor} = $self-> {focusedItem} unless defined $self-> {anchor};
869 } else {
870 $self-> selectedItems([$self-> focusedItem]) if exists $self-> {anchor};
871 delete $self-> {anchor};
872 }
873  
874 $self-> offset(
875  
876 $self-> {offset} +
877  
878 $self-> {indent} * (( $key == kb::Left) ? -1 : 1
879 )) if $key == kb::Left || $key == kb::Right;
880 $self-> focusedItem( $newItem >= 0 ? $newItem : 0);
881 $self-> clear_event;
882 return;
883 }
884  
885 if ( $mod == 0 && $key == kb::Enter) {
886 $self-> adjust( $self-> {focusedItem}, 1);
887 $self-> clear_event;
888 return;
889 }
890 }
891  
892 sub reset
893 {
894 my $self = $_[0];
895 my @size = $self-> get_active_area( 2);
896 $self-> makehint(0);
897 my $ih = $self-> {itemHeight};
898 $self-> {rows} = int( $size[1] / $ih);
899 $self-> {rows} = 0 if $self-> {rows} < 0;
900 $self-> {yedge} = ( $size[1] - $self-> {rows} * $ih) ? 1 : 0;
901 }
902  
903 sub reset_scrolls
904 {
905 my $self = $_[0];
906 $self-> makehint(0);
907 if ( $self-> {scrollTransaction} != 1) {
908 $self-> vScroll( $self-> {rows} < $self-> {count} ) if $self-> {autoVScroll};
909 $self-> {vScrollBar}-> set(
910 max => $self-> {count} - $self-> {rows},
911 pageStep => $self-> {rows},
912 whole => $self-> {count},
913 partial => $self-> {rows},
914 value => $self-> {topItem},
915 ) if $self-> {vScroll};
916 }
917  
918 if ( $self-> {scrollTransaction} != 2) {
919  
920 my @sz = $self-> get_active_area( 2);
921 my $iw = $self-> {maxWidth};
922 if ( $self-> {autoHScroll}) {
923 my $hs = ($sz[0] < $iw) ? 1 : 0;
924 if ( $hs != $self-> {hScroll}) {
925 $self-> hScroll( $hs);
926 @sz = $self-> get_active_area( 2);
927 }
928 }
929 $self-> {hScrollBar}-> set(
930 max => $iw - $sz[0],
931 whole => $iw,
932 value => $self-> {offset},
933 partial => $sz[0],
934 pageStep => $iw / 5,
935 ) if $self-> {hScroll};
936 }
937 }
938  
939 sub reset_tree
940 {
941 my ( $self, $i) = ( $_[0], 0);
942 $self-> makehint(0);
943 $self-> {stackFrames} = [];
944 $self-> {lineDefs} = [];
945 my @stack;
946 my @lines;
947 my $traverse;
948  
949 $traverse = sub {
950 my ( $node, $level, $lastChild) = @_;
951 $lines[ $level] = $lastChild ? undef : ( $i ? $i - 0.5 : 0.5);
952 if (( $i % STACK_FRAME) == STACK_FRAME - 1) {
953 push( @{$self-> {stackFrames}}, [@stack[0..$level]]);
954 push( @{$self-> {lineDefs}}, [@lines[0..$level]]);
955 }
956 $i++;
957 $level++;
958 if ( $node-> [DOWN] && $node-> [EXPANDED]) {
959 $stack[$level] = 0;
960 my $c = @{$node-> [DOWN]};
961 for ( @{$node-> [DOWN]}) {
962 $traverse-> ( $_, $level, --$c ? 0 : 1);
963 $stack[$level]++;
964 }
965 }
966 };
967  
968 $stack[0] = 0;
969 my $c = @{$self-> {items}};
970 for (@{$self-> {items}}) {
971 $traverse-> ( $_, 0, --$c ? 0 : 1);
972 $stack[0]++;
973 }
974 undef $traverse;
975  
976 $self-> {count} = $i;
977  
978 my $fullc = $self-> {fullCalibrate};
979 my ( $notifier, @notifyParms) = $self-> get_notify_sub(q(MeasureItem));
980 my $maxWidth = 0;
981 my $indent = $self-> {indent};
982 $self-> push_event;
983 $self-> begin_paint_info;
984 $self-> iterate( sub {
985 my ( $current, $parent, $index, $position, $level, $visibility) = @_;
986 my $iw = $fullc ? undef : $current-> [WIDTH];
987 unless ( defined $iw) {
988 $notifier-> ( @notifyParms, $current, $level, \$iw);
989 $current-> [WIDTH] = $iw;
990 }
991 my $iwc = $iw + ( 2.5 + $level) * $indent;
992 $maxWidth = $iwc if $maxWidth < $iwc;
993 return 0;
994 });
995 $self-> end_paint_info;
996 $self-> pop_event;
997 $self-> {maxWidth} = $maxWidth;
998 }
999  
1000 sub calibrate
1001 {
1002 my $self = $_[0];
1003 $self-> {fullCalibrate} = 1;
1004 $self-> reset_tree;
1005 delete $self-> {fullCalibrate};
1006 $self-> update_tree;
1007 }
1008  
1009 sub update_tree
1010 {
1011 my $self = $_[0];
1012 $self-> topItem( $self-> {topItem});
1013 $self-> offset( $self-> {offset});
1014 }
1015  
1016 sub draw_items
1017 {
1018 my ($self, $canvas, $paintStruc) = @_;
1019 my ( $notifier, @notifyParms) = $self-> get_notify_sub(q(DrawItem));
1020 $self-> push_event;
1021 for ( @$paintStruc) { $notifier-> ( @notifyParms, $canvas, @$_); }
1022 $self-> pop_event;
1023 }
1024  
1025 sub set_auto_height
1026 {
1027 my ( $self, $auto) = @_;
1028 $self-> itemHeight( $self-> font-> height) if $auto;
1029 $self-> {autoHeight} = $auto;
1030 }
1031  
1032 sub set_extended_select
1033 {
1034 my ( $self, $esel) = @_;
1035 $self-> {extendedSelect} = $esel;
1036 }
1037  
1038 sub set_focused_item
1039 {
1040 my ( $self, $foc) = @_;
1041 my $oldFoc = $self-> {focusedItem};
1042 $foc = $self-> {count} - 1 if $foc >= $self-> {count};
1043 $foc = -1 if $foc < -1;
1044 return if $self-> {focusedItem} == $foc;
1045 return if $foc < -1;
1046  
1047 $self-> {focusedItem} = $foc;
1048 $self-> selectedItems([$foc])
1049  
1050 if $self-> {multiSelect} && $self-> {extendedSelect} && ! exists $self-> {anchor};
1051 $self-> notify(q(SelectItem), [[$foc, undef, 1]]) if $foc >= 0;
1052 return if $self-> {doingExpand};
1053  
1054 my $topSet = undef;
1055 if ( $foc >= 0) {
1056 my $rows = $self-> {rows} ? $self-> {rows} : 1;
1057 if ( $foc < $self-> {topItem}) {
1058 $topSet = $foc;
1059 } elsif ( $foc >= $self-> {topItem} + $rows) {
1060 $topSet = $foc - $rows + 1;
1061 }
1062 }
1063 $self-> topItem( $topSet) if defined $topSet;
1064 ( $oldFoc, $foc) = ( $foc, $oldFoc) if $foc > $oldFoc;
1065 my @a = $self-> get_active_area;
1066 my $ih = $self-> {itemHeight};
1067 my $lastItem = $self-> {topItem} + $self-> {rows};
1068  
1069 $self-> invalidate_rect(
1070  
1071 $a[0], $a[3] - ( $oldFoc - $self-> {topItem} + 1) * $ih,
1072 $a[2], $a[3] - ( $oldFoc - $self-> {topItem}) * $ih
1073 ) if
1074  
1075 $oldFoc >= 0 &&
1076  
1077 $oldFoc != $foc &&
1078  
1079 $oldFoc >= $self-> {topItem} &&
1080  
1081 $oldFoc <= $self-> {topItem} + $self-> {rows};
1082  
1083 $self-> invalidate_rect(
1084  
1085 $a[0], $a[3] - ( $foc - $self-> {topItem} + 1) * $ih,
1086 $a[2], $a[3] - ( $foc - $self-> {topItem}) * $ih
1087 ) if
1088  
1089 $foc >= 0 &&
1090  
1091 $foc >= $self-> {topItem} &&
1092  
1093 $foc <= $self-> {topItem} + $self-> {rows};
1094 }
1095  
1096 sub set_indent
1097 {
1098 my ( $self, $i) = @_;
1099 return if $i == $self-> {indent};
1100 $i = 1 if $i < 1;
1101 $self-> {indent} = $i;
1102 $self-> calibrate;
1103 $self-> repaint;
1104 }
1105  
1106 sub set_item_height
1107 {
1108 my ( $self, $ih) = @_;
1109 $ih = 1 if $ih < 1;
1110 $self-> autoHeight(0);
1111 return if $ih == $self-> {itemHeight};
1112 $self-> {itemHeight} = $ih;
1113 $self-> reset;
1114 $self-> reset_scrolls;
1115 $self-> repaint;
1116 $self-> {hinter}-> height( $ih) if $self-> {hinter};
1117 }
1118  
1119 sub validate_items
1120 {
1121 my ( $self, $items) = @_;
1122 my $traverse;
1123 $traverse = sub {
1124 my $current = $_[0];
1125 my $spliceTo = 3;
1126 if ( ref $current-> [DOWN] eq 'ARRAY') {
1127 $traverse-> ( $_) for @{$current-> [DOWN]};
1128 $current-> [EXPANDED] = 0 unless defined $current-> [EXPANDED];
1129 } else {
1130 $spliceTo = 1;
1131 }
1132 splice( @$current, $spliceTo);
1133 };
1134 $traverse-> ( $items);
1135 undef $traverse;
1136 }
1137  
1138 sub set_items
1139 {
1140 my ( $self, $items) = @_;
1141 $items = [] unless defined $items;
1142 $self-> validate_items( [ 0, $items]);
1143 $self-> {items} = $items;
1144 $self-> reset_tree;
1145 $self-> update_tree;
1146 $self-> repaint;
1147 $self-> reset_scrolls;
1148 }
1149  
1150 sub insert_items
1151 {
1152 my ( $self, $where, $at, @items) = @_;
1153 return unless scalar @items;
1154  
1155 my $forceReset = 0;
1156 $where = [0, $self-> {items}], $forceReset = 1 unless $where;
1157 $self-> validate_items( $_) for @items;
1158 return unless $where-> [DOWN];
1159  
1160 my $ch = scalar @{$where-> [DOWN]};
1161 $at = 0 if $at < 0;
1162 $at = $ch if $at > $ch;
1163  
1164 my ( $x, $l) = $self-> get_index( $where);
1165 splice( @{$where-> [DOWN]}, $at, 0, @items);
1166 return if $x < 0 && !$forceReset;
1167  
1168 $self-> reset_tree;
1169 $self-> update_tree;
1170 $self-> repaint;
1171 $self-> reset_scrolls;
1172 }
1173  
1174 sub delete_items
1175 {
1176 my ( $self, $where, $at, $amount) = @_;
1177 $where = [0, $self-> {items}] unless $where;
1178 return unless $where-> [DOWN];
1179  
1180 my ( $x, $l) = $self-> get_index( $where);
1181 $at = 0 unless defined $at;
1182  
1183 $amount = scalar @{$where-> [DOWN]} unless defined $amount;
1184 splice( @{$where-> [DOWN]}, $at, $amount);
1185 return if $x < 0;
1186  
1187 my $f = $self-> {focusedItem};
1188 $self-> focusedItem( -1) if $f >= $x && $f < $x + $amount;
1189  
1190 $self-> reset_tree;
1191 $self-> update_tree;
1192 $self-> repaint;
1193 $self-> reset_scrolls;
1194 }
1195  
1196 sub delete_item
1197 {
1198 my ( $self, $item) = @_;
1199 return unless $item;
1200 my ( $x, $l) = $self-> get_index( $item);
1201  
1202 my ( $parent, $offset) = $self-> get_item_parent( $item);
1203 if ( defined $parent) {
1204 splice( @{$parent-> [DOWN]}, $offset, 1);
1205 } else {
1206 splice( @{$self-> {items}}, $offset, 1) if defined $offset;
1207 }
1208  
1209 if ( $x >= 0) {
1210 $self-> reset_tree;
1211 $self-> update_tree;
1212 $self-> focusedItem( -1) if $x == $self-> {focusedItem};
1213 $self-> repaint;
1214 $self-> reset_scrolls;
1215 }
1216 }
1217  
1218 sub get_item_parent
1219 {
1220 my ( $self, $item) = @_;
1221 my $parent;
1222 my $offset;
1223 return unless $item;
1224  
1225 $self-> iterate( sub {
1226 my ($cur,$par,$idx) = @_;
1227 $parent = $par, $offset = $idx, return 1 if $cur == $item;
1228 }, 1);
1229 return $parent, $offset;
1230 }
1231  
1232 sub set_multi_select
1233 {
1234 my ( $self, $ms) = @_;
1235 return if $ms == $self-> {multiSelect};
1236  
1237 unless ( $self-> {multiSelect} = $ms) {
1238 $self-> deselect_all(1);
1239 $self-> repaint;
1240 } else {
1241 $self-> selectedItems([$self-> focusedItem]);
1242 }
1243 }
1244  
1245 sub set_offset
1246 {
1247 my ( $self, $offset) = @_;
1248 my ( $iw, @a) = ($self-> {maxWidth}, $self-> get_active_area);
1249  
1250 my $lc = $a[2] - $a[0];
1251 if ( $iw > $lc) {
1252 $offset = $iw - $lc if $offset > $iw - $lc;
1253 $offset = 0 if $offset < 0;
1254 } else {
1255 $offset = 0;
1256 }
1257 return if $self-> {offset} == $offset;
1258  
1259 my $oldOfs = $self-> {offset};
1260 $self-> {offset} = $offset;
1261  
1262 if ( $self-> {hScroll} && $self-> {scrollTransaction} != 2) {
1263 $self-> {scrollTransaction} = 2;
1264 $self-> {hScrollBar}-> value( $offset);
1265 $self-> {scrollTransaction} = 0;
1266 }
1267  
1268 $self-> makehint(0);
1269 $self-> scroll( $oldOfs - $offset, 0,
1270 clipRect => \@a);
1271 }
1272  
1273 sub set_top_item
1274 {
1275 my ( $self, $topItem) = @_;
1276 $topItem = 0 if $topItem < 0; # first validation
1277 $topItem = $self-> {count} - 1 if $topItem >= $self-> {count};
1278 $topItem = 0 if $topItem < 0; # count = 0 case
1279 return if $topItem == $self-> {topItem};
1280  
1281 my $oldTop = $self-> {topItem};
1282 $self-> {topItem} = $topItem;
1283 my ($ih, @a) = ( $self-> {itemHeight}, $self-> get_active_area);
1284 $self-> makehint(0);
1285  
1286 if ( $self-> {scrollTransaction} != 1 && $self-> {vScroll}) {
1287 $self-> {scrollTransaction} = 1;
1288 $self-> {vScrollBar}-> value( $topItem);
1289 $self-> {scrollTransaction} = 0;
1290 }
1291  
1292 $self-> scroll( 0, ($topItem - $oldTop) * $ih,
1293 clipRect => \@a);
1294 }
1295  
1296 sub VScroll_Change
1297 {
1298 my ( $self, $scr) = @_;
1299 return if $self-> {scrollTransaction};
1300 $self-> {scrollTransaction} = 1;
1301 $self-> topItem( $scr-> value);
1302 $self-> {scrollTransaction} = 0;
1303 # $self-> repaint;
1304 }
1305  
1306 sub HScroll_Change
1307 {
1308 my ( $self, $scr) = @_;
1309 return if $self-> {scrollTransaction};
1310 $self-> {scrollTransaction} = 2;
1311 $self-> {multiColumn} ?
1312 $self-> topItem( $scr-> value) :
1313 $self-> offset( $scr-> value);
1314 $self-> {scrollTransaction} = 0;
1315 }
1316  
1317 sub reset_indents
1318 {
1319 my $self = $_[0];
1320 $self-> reset;
1321 $self-> reset_scrolls;
1322 $self-> repaint;
1323 }
1324  
1325 sub showItemHint
1326 {
1327 return $_[0]-> {showItemHint} unless $#_;
1328 my ( $self, $sh) = @_;
1329 return if $sh == $self-> {showItemHint};
1330 $self-> {showItemHint} = $sh;
1331 $self-> makehint(0) if !$sh && $self-> {hintActive};
1332 }
1333  
1334 sub dragable
1335 {
1336 return $_[0]-> {dragable} unless $#_;
1337 $_[0]-> {dragable} = $_[1];
1338 }
1339  
1340 sub get_index
1341 {
1342 my ( $self, $item) = @_;
1343 return -1, undef unless $item;
1344 my $lev;
1345 my $rec = -1;
1346 $self-> iterate( sub {
1347 my ( $current, $parent, $index, $position, $level, $lastChild, $visibility) = @_;
1348 $lev = $level, $rec = $position, return 1 if $current == $item;
1349 });
1350  
1351 return $rec, $lev;
1352 }
1353  
1354 sub get_item
1355 {
1356 my ( $self, $item) = @_;
1357 return if $item < 0 || $item >= $self-> {count};
1358  
1359 my ($array, $idx, $lim, $level) = ([['root'],$self-> {items}], 0, scalar @{$self-> {items}}, 0);
1360 my $i = int(( $item + 1) / STACK_FRAME) * STACK_FRAME - 1;
1361 my $position = 0;
1362 my @stack;
1363 if ( $i >= 0) {
1364 $position = $i;
1365 $i = $self-> {stackFrames}-> [int( $item + 1) / STACK_FRAME - 1];
1366 if ( $i) {
1367 my $k;
1368 for ( $k = 0; $k < scalar @{$i} - 1; $k++) {
1369 $idx = $i-> [$k] + 1;
1370 $lim = scalar @{$array-> [DOWN]};
1371 push( @stack, [ $array, $idx, $lim]);
1372 $array = $array-> [DOWN]-> [$idx - 1];
1373 }
1374 $idx = $$i[$k];
1375 $lim = scalar @{$array-> [DOWN]};
1376 $level = scalar @$i - 1;
1377 }
1378  
1379 }
1380  
1381 die "Internal error\n" if $position > $item;
1382 while (1) {
1383 my $node = $array-> [DOWN]-> [$idx++];
1384 my $lastChild = $idx == $lim;
1385 return $node, $level if $position == $item;
1386 $position++;
1387 if ( $node-> [DOWN] && $node-> [EXPANDED] && scalar @{$node-> [DOWN]}) {
1388 $level++;
1389 push ( @stack, [ $array, $idx, $lim]);
1390 $idx = 0;
1391 $array = $node;
1392 $lim = scalar @{$node-> [DOWN]};
1393 next;
1394 }
1395 while ( $lastChild) {
1396 last unless $level--;
1397 ( $array, $idx, $lim) = @{pop @stack};
1398 $lastChild = $idx == $lim;
1399 }
1400 }
1401  
1402 }
1403  
1404 sub get_item_text
1405 {
1406 my ( $self, $item) = @_;
1407 my $txt = '';
1408 $self-> notify(q(Stringify), $item, \$txt);
1409 return $txt;
1410 }
1411  
1412 sub get_item_width
1413 {
1414 return $_[1]-> [WIDTH];
1415 }
1416  
1417 sub get_index_text
1418 {
1419 my ( $self, $index) = @_;
1420 my $txt = '';
1421 my ( $node, $lev) = $self-> get_item( $index);
1422 $self-> notify(q(Stringify), $node, \$txt);
1423 return $txt;
1424 }
1425  
1426 sub get_index_width
1427 {
1428 my ( $self, $index) = @_;
1429 my ( $node, $lev) = $self-> get_item( $index);
1430 return $node-> [WIDTH];
1431 }
1432  
1433 sub on_drawitem
1434 {
1435 # my ( $self, $canvas, $node, $left, $bottom, $right, $top, $position, $selected, $focused) = @_;
1436 }
1437  
1438 sub on_measureitem
1439 {
1440 # my ( $self, $node, $level, $result) = @_;
1441 }
1442  
1443 sub on_stringify
1444 {
1445 # my ( $self, $node, $result) = @_;
1446 }
1447  
1448 sub on_selectitem
1449 {
1450 # my ( $self, $index_array, $flag) = @_;
1451 }
1452  
1453 sub on_expand
1454 {
1455 my ( $self, $node, $action) = @_;
1456 $self-> repaint;
1457 }
1458  
1459 #sub onMouseWheel
1460 #{
1461 # my ( $self, $node, $action) = @_;
1462 # $self-> repaint;
1463 #}
1464  
1465 sub on_dragitem
1466 {
1467 my ( $self, $from, $to) = @_;
1468 my ( $fx, $fl) = $self-> get_item( $from);
1469 my ( $tx, $tl) = $self-> get_item( $to);
1470 my ( $fpx, $fpo) = $self-> get_item_parent( $fx);
1471 return unless $fx && $tx;
1472 my $found_inv = 0;
1473  
1474 ##################################################
1475  
1476 my ( $tpx, $tpo) = $self-> get_item_parent( $tx);
1477  
1478 if ( $fx->[0]->[3] =~ /file/i && $tx->[0]->[3] =~ /file/i
1479 && $tx->[0]->[6] ne $fx->[0]->[6]
1480 ) {
1481  
1482 my $r = Prima::MsgBox::message_box (
1483  
1484 'Copying file content',
1485 'Do you want to overwrite ['.$tx->[0]->[6].'] with ['.$fx->[0]->[6].'] ?',
1486  
1487 mb::YesNo | mb::Warning
1488  
1489 );
1490  
1491 if ( $r == mb::Yes ) {
1492 eval { File::Copy::copy( $fx->[0]->[6], $tx->[0]->[6] ) };
1493 }
1494 return;
1495 }
1496  
1497 return if $tx->[0]->[3] =~ /file/i;
1498  
1499 my $of = $fx->[0]->[6];
1500  
1501 my $pf = $fpx->[0]->[6]; $pf = $fpx->[0]->[4] if $fpx->[0]->[2] == 0;
1502  
1503 $of =~ s/^$pf//;
1504 my $ot = $tx->[0]->[6]; $ot = $tx->[0]->[4] if $tx->[0]->[2] == 0;
1505  
1506 my $path_f = "$pf/$of"; $path_f =~ s/([\/]+)/\//g;
1507  
1508 my $path_t = "$ot/$of"; $path_t =~ s/([\/]+)/\//g;
1509  
1510 # return if $pf eq $ot;
1511 return if $path_f eq $path_t;
1512  
1513 if ( $fx->[0]->[3] eq 'file' ) {
1514  
1515 eval { File::Copy::move( $path_f, $path_t ) };
1516 return if $@;
1517 } else {
1518  
1519 eval { File::Copy::Recursive::dirmove( $path_f, $path_t ) } ;
1520 return if $@;
1521 }
1522  
1523 ##################################################
1524  
1525 my $traverse;
1526 $traverse = sub {
1527 my $current = $_[0];
1528 $found_inv = 1, return if $current == $tx;
1529 if ( $current-> [DOWN] && $current-> [EXPANDED]) {
1530 my $c = scalar @{$current-> [DOWN]};
1531 for ( @{$current-> [DOWN]}) {
1532 my $ret = $traverse-> ( $_);
1533 return $ret if $ret;
1534 }
1535 }
1536 };
1537 $traverse-> ( $fx);
1538 undef $traverse;
1539 return if $found_inv;
1540  
1541 if ( $fpx) {
1542 splice( @{$fpx-> [DOWN]}, $fpo, 1);
1543 } else {
1544 splice( @{$self-> {items}}, $fpo, 1);
1545 }
1546 unless ( $tx-> [DOWN]) {
1547 $tx-> [DOWN] = [$fx];
1548 $tx-> [EXPANDED] = 1;
1549 } else {
1550 splice( @{$tx-> [DOWN]}, 0, 0, $fx);
1551 }
1552 $self-> reset_tree;
1553 $self-> update_tree;
1554 $self-> repaint;
1555 $self-> clear_event;
1556  
1557 $::project-> make_tree;
1558 }
1559  
1560 #------------------------------------------------------
1561  
1562 sub is_selected
1563  
1564 {
1565  
1566 my ( $self, $index, $item, $sel) = @_;
1567 unless ( defined $item) {
1568 my ($node, $lev) = $self-> get_item( $index);
1569 return 0 unless $node;
1570 $item = $node;
1571 }
1572 return $item-> [SELECTED];
1573 }
1574  
1575 sub set_item_selected
1576 {
1577 my ( $self, $index, $item, $sel) = @_;
1578 return unless $self-> {multiSelect};
1579 unless ( defined $item) {
1580 my ($node, $lev) = $self-> get_item( $index);
1581 return unless $node;
1582 $item = $node;
1583 }
1584 $sel ||= 0;
1585 return if $sel == ( $item-> [SELECTED] ? 1 : 0);
1586 $item-> [SELECTED] = $sel;
1587  
1588 if ( !defined $index) {
1589 my ( $x, $lev) = $self-> get_index( $item);
1590 if ( $x < 0) {
1591 $self-> notify(q(SelectItem), [[ undef, $item, $sel ]]);
1592 return 0;
1593 }
1594 $index = $x;
1595 }
1596 $self-> notify(q(SelectItem), [[ $index, $item, $sel]]);
1597 my ( $ih, @a) = ( $self-> {itemHeight}, $self-> get_active_area);
1598 $self-> invalidate_rect(
1599 $a[0], $a[3] - ( $index - $self-> {topItem} + 1) * $ih,
1600 $a[2], $a[3] - ( $index - $self-> {topItem}) * $ih
1601 );
1602 }
1603  
1604 sub select_all
1605 {
1606 my ( $self, $full) = @_;
1607 $self-> iterate( sub { $_[0]-> [SELECTED] = 1; 0 }, $full);
1608 $self-> repaint;
1609 }
1610  
1611 sub deselect_all
1612 {
1613 my ( $self, $full) = @_;
1614 $self-> iterate( sub { $_[0]-> [SELECTED] = 0 }, $full);
1615 $self-> repaint;
1616 }
1617  
1618 sub add_selection
1619 {
1620 my ( $self, $array, $flag) = @_;
1621 return unless $self-> {multiSelect};
1622 my %items = map { $_ => 1 } @$array;
1623 $flag ||= 0;
1624 my ( $ih, @a) = ( $self-> {itemHeight}, $self-> get_active_area);
1625 my @sel;
1626  
1627 $self-> iterate( sub {
1628 my ( $current, $parent, $index, $position, $level, $lastChild) = @_;
1629 return 0 unless $items{$position};
1630 return 0 if $flag == ($current-> [SELECTED] ? 1 : 0);
1631 $current-> [SELECTED] = $flag;
1632 push @sel, [ $position, $current, 1];
1633 $self-> invalidate_rect(
1634 $a[0], $a[3] - ( $position - $self-> {topItem} + 1) * $ih,
1635 $a[2], $a[3] - ( $position - $self-> {topItem}) * $ih
1636 );
1637 0;
1638 });
1639 $self-> notify(q(SelectItem), \@sel) if @sel;
1640 }
1641  
1642 sub get_selected_items
1643 {
1644 my $self = $_[0];
1645 my @ret;
1646 $self-> iterate( sub { push @ret, $_[3] if $_[0]-> [SELECTED]; 0 });
1647 return @ret;
1648 }
1649  
1650 sub set_selection
1651 {
1652 my ( $self, $array, $flag) = @_;
1653 return unless $self-> {multiSelect};
1654 my %items = map { $_ => 1 } @$array;
1655 $flag ||= 0;
1656 my ( $ih, @a) = ( $self-> {itemHeight}, $self-> get_active_area);
1657 my @sel;
1658  
1659 $self-> iterate( sub {
1660 my ( $current, $parent, $index, $position, $level, $lastChild, $visibility) = @_;
1661 if ( defined $visibility) {
1662 my $new_val = $items{$visibility} ? $flag : !$flag;
1663 return 0 if $new_val == ($current-> [SELECTED] ? 1 : 0);
1664 $current-> [SELECTED] = $new_val;
1665 push @sel, [ $visibility, $current, $new_val];
1666 $self-> invalidate_rect(
1667 $a[0], $a[3] - ( $visibility - $self-> {topItem} + 1) * $ih,
1668 $a[2], $a[3] - ( $visibility - $self-> {topItem}) * $ih
1669 );
1670 } elsif ( $flag != ( $current-> [SELECTED] ? 1 : 0)) {
1671 $current-> [SELECTED] = $flag;
1672 push @sel, [ undef, $current, $flag];
1673 };
1674 0;
1675 }, 1);
1676  
1677 $self-> notify(q(SelectItem), \@sel) if @sel;
1678 }
1679  
1680 sub toggle_item
1681 {
1682  
1683 my ( $self, $index, $item) = @_;
1684 unless ( defined $item) {
1685 my ($node, $lev) = $self-> get_item( $index);
1686 return unless $node;
1687 $item = $node;
1688 }
1689 $self-> set_item_selected( $index, $item, $item-> [SELECTED] ? 0 : 1);
1690 }
1691  
1692 sub select_item { $_[0]-> set_item_selected( $_[1], $_[2], 1); }
1693 sub unselect_item { $_[0]-> set_item_selected( $_[1], $_[2], 0); }
1694  
1695 sub autoHeight {($#_)?$_[0]-> set_auto_height ($_[1]):return $_[0]-> {autoHeight} }
1696 sub extendedSelect{($#_)?$_[0]-> set_extended_select($_[1]):return $_[0]-> {extendedSelect} }
1697 sub focusedItem {($#_)?$_[0]-> set_focused_item ($_[1]):return $_[0]-> {focusedItem} }
1698 sub indent {($#_)?$_[0]-> set_indent( $_[1]) :return $_[0]-> {indent} }
1699 sub items {($#_)?$_[0]-> set_items( $_[1]) :return $_[0]-> {items} }
1700 sub itemHeight {($#_)?$_[0]-> set_item_height ($_[1]):return $_[0]-> {itemHeight} }
1701 sub multiSelect {($#_)?$_[0]-> set_multi_select ($_[1]):return $_[0]-> {multiSelect} }
1702 sub offset {($#_)?$_[0]-> set_offset ($_[1]):return $_[0]-> {offset} }
1703 sub selectedItems {($#_)?$_[0]-> set_selection ($_[1],1):return $_[0]-> get_selected_items}
1704 sub topItem {($#_)?$_[0]-> set_top_item ($_[1]):return $_[0]-> {topItem} }
1705  
1706 package Prima::CodeManager::StringOutline;
1707 use vars qw(@ISA);
1708 @ISA = qw(Prima::CodeManager::OutlineViewer);
1709  
1710 sub draw_items
1711 {
1712 return;
1713 my ($self, $canvas, $paintStruc) = @_;
1714 for ( @$paintStruc) {
1715 my ( $node, $left, $bottom, $right, $top, $position, $selected, $focused) = @$_;
1716 if ( $selected) {
1717 my $c = $canvas-> color;
1718 $canvas-> color( $self-> hiliteBackColor);
1719 $canvas-> bar( $left, $bottom, $right, $top);
1720 $canvas-> color( $self-> hiliteColor);
1721 $canvas-> text_out( $node-> [0], $left, $bottom);
1722 $canvas-> color( $c);
1723 } else {
1724 $canvas-> text_out( $node-> [0], $left, $bottom);
1725 }
1726 $canvas-> rect_focus( $left, $bottom, $right, $top) if $focused;
1727 }
1728 }
1729  
1730 sub load_icon {
1731 my ( $file ) = @_;
1732 return undef unless -e $file;
1733 my $im = Prima::Icon-> new( type=>im::RGB, ) || return undef;
1734 $im->load( $file ) || return undef;
1735 return $im;
1736 }
1737  
1738 sub on_measureitem
1739 {
1740 my ( $self, $node, $level, $result) = @_;
1741 $$result = $self-> get_text_width( $node-> [0]);
1742 }
1743  
1744 sub on_stringify
1745 {
1746 my ( $self, $node, $result) = @_;
1747 $$result = $node-> [0];
1748 }
1749  
1750 package Prima::CodeManager::Outline;
1751 use vars qw(@ISA);
1752 @ISA = qw(Prima::CodeManager::OutlineViewer);
1753  
1754 sub draw_itemsold
1755 {
1756 my ($self, $canvas, $paintStruc) = @_;
1757 for ( @$paintStruc) {
1758 my ( $node, $left, $bottom, $right, $top, $position, $selected, $focused) = @$_;
1759  
1760 if ( $selected) {
1761 my $c = $canvas-> color;
1762 $canvas-> color( $self-> hiliteBackColor);
1763 $canvas-> bar( $left, $bottom, $right, $top);
1764 $canvas-> color( $self-> hiliteColor);
1765 $canvas-> text_out( $node-> [0]-> [0], $left, $bottom);
1766 $canvas-> color( $c);
1767 } else {
1768 $canvas-> text_out( $node-> [0]-> [0], $left, $bottom);
1769 }
1770 $canvas-> rect_focus( $left, $bottom, $right, $top) if $focused;
1771 }
1772 }
1773  
1774 sub draw_items
1775 {
1776 my ($self, $canvas, $paintStruc ) = @_;
1777 my $i = 0;
1778 for ( @$paintStruc) {
1779 my ( $node, $left, $bottom, $right, $top, $position, $selected, $focused) = @$_;
1780  
1781 $left += ( 12 - $self->{indent}) * 0.00000;
1782  
1783 my $img = $node->[0]->[1];
1784 my @dime = [ 0, 0 ];
1785 if ( $img ) {
1786 @dime = $img->size;
1787 $canvas-> put_image(
1788 $left - $dime[0] - 2*0.00000,
1789 int( $bottom + ( $self-> {itemHeight} - $dime[1] ) / 2 ),
1790 $img
1791 );
1792 $left += $dime[0];
1793 }
1794  
1795 if ( $selected) {
1796 my $c;
1797 $c = $canvas-> color;
1798 $canvas-> color( $self-> hiliteBackColor);
1799 $canvas-> bar( $left - int( $dime[0]/2), $bottom, $right + $dime[0], $top);
1800 $canvas-> color( $self-> hiliteColor);
1801 $canvas-> text_out( $node-> [0]-> [0], $left - int($dime[0]/2 ), $bottom);
1802 $canvas-> color( $c)
1803  
1804 } else {
1805 $canvas-> text_out(
1806 $node-> [0]-> [0],
1807 $left - int ( $dime[0]/2 ),
1808 int ( $bottom + ( $self-> {itemHeight} - $self-> font-> height ) / 2 )
1809 );
1810 }
1811 $canvas-> rect_focus( $left - int($dime[0]/2 ), $bottom, $right + $dime[0], $top) if $focused;
1812 $i++;
1813 }
1814 }
1815  
1816 sub on_measureitem
1817 {
1818 my ( $self, $node, $level, $result) = @_;
1819 $$result = $self-> get_text_width( $node-> [0]-> [0]);
1820 }
1821  
1822 sub on_stringify
1823 {
1824 my ( $self, $node, $result) = @_;
1825 $$result = $node-> [0]-> [0];
1826 }
1827  
1828 package Prima::CodeManager::DirectoryOutline;
1829 use vars qw(@ISA);
1830 @ISA = qw(Prima::CodeManager::OutlineViewer);
1831  
1832 # node[0]:
1833 # 0 : node text
1834 # 1 : parent path, '' if none
1835 # 2 : icon width
1836 # 3 : drive icon, only for roots
1837  
1838 my $unix = Prima::Application-> get_system_info-> {apc} == apc::Unix || $^O =~ /cygwin/;
1839 my @images;
1840 my @drvImages;
1841  
1842 #{
1843 # my $i = 0;
1844 # my @idx = ( sbmp::SFolderOpened, sbmp::SFolderClosed);
1845 # $images[ $i++] = Prima::StdBitmap::icon( $_) for @idx;
1846 # unless ( $unix) {
1847 # $i = 0;
1848 # for (
1849 #
1850 # sbmp::DriveFloppy, sbmp::DriveHDD, sbmp::DriveNetwork,
1851 # sbmp::DriveCDROM, sbmp::DriveMemory, sbmp::DriveUnknown
1852 # ) {
1853 # $drvImages[ $i++] = Prima::StdBitmap::icon($_);
1854 # }
1855 # }
1856 #}
1857  
1858 sub profile_default
1859 {
1860 return {
1861 %{$_[ 0]-> SUPER::profile_default},
1862 path => '',
1863 dragable => 0,
1864 openedGlyphs => 1,
1865 closedGlyphs => 1,
1866 openedIcon => undef,
1867 closedIcon => undef,
1868 showDotDirs => 0,
1869 }
1870 }
1871  
1872 sub init_tree
1873 {
1874 my $self = $_[0];
1875 my @tree;
1876 if ( $unix) {
1877 push ( @tree, [[ '/', ''], [], 0]);
1878 } else {
1879 my @drv = split( ' ', Prima::Utils::query_drives_map('A:'));
1880 for ( @drv) {
1881 my $type = Prima::Utils::query_drive_type($_);
1882 push ( @tree, [[ $_, ''], [], 0]);
1883 }
1884 }
1885 $self-> items( \@tree);
1886 }
1887  
1888 sub init
1889 {
1890 my $self = shift;
1891 my %profile = @_;
1892 $profile{items} = [];
1893 %profile = $self-> SUPER::init( %profile);
1894 for ( qw( files filesStat items)) { $self-> {$_} = []; }
1895 for ( qw( openedIcon closedIcon openedGlyphs closedGlyphs indent showDotDirs))
1896 { $self-> {$_} = $profile{$_}}
1897 $self-> {openedIcon} = $images[0] unless $self-> {openedIcon};
1898 $self-> {closedIcon} = $images[1] unless $self-> {closedIcon};
1899 $self-> {fontHeight} = $self-> font-> height;
1900 $self-> recalc_icons;
1901 $self-> init_tree;
1902 $self-> {cPath} = $profile{path};
1903 return %profile;
1904 }
1905  
1906 sub on_create
1907 {
1908 my $self = $_[0];
1909 # path could invoke adjust(), thus calling notify(), which
1910 # fails until init() ends.
1911 $self-> path( $self-> {cPath}) if length $self-> {cPath};
1912 }
1913  
1914 sub draw_items
1915 {
1916 my ($self, $canvas, $paintStruc) = @_;
1917 for ( @$paintStruc) {
1918 my ( $node, $left, $bottom, $right, $top, $position, $selected, $focused) = @$_;
1919 my $c;
1920 my $dw = length $node-> [0]-> [1] ?
1921 $self-> {iconSizes}-> [0] :
1922 $node-> [0]-> [2];
1923 if ( $selected) {
1924 $c = $canvas-> color;
1925 $canvas-> color( $self-> hiliteBackColor);
1926 $canvas-> bar( $left - $self-> {indent} / 4, $bottom, $right, $top);
1927 $canvas-> color( $self-> hiliteColor);
1928 }
1929 my $icon = (length( $node-> [0]-> [1]) || $unix) ?
1930 ( $node-> [2] ? $self-> {openedIcon} : $self-> {closedIcon}) : $node-> [0]-> [3];
1931 $canvas-> put_image(
1932 $left - $self-> {indent} / 4,
1933 int($bottom + ( $self-> {itemHeight} - $self-> {iconSizes}-> [1]) / 2),
1934 $icon
1935 );
1936 $canvas-> text_out(
1937 $node-> [0]-> [0],
1938 $left + $dw,
1939 int( $bottom + ( $self-> {itemHeight} - $self-> {fontHeight}) / 2)
1940 );
1941 $canvas-> color( $c) if $selected;
1942 $canvas-> rect_focus( $left - $self-> {indent} / 4, $bottom, $right, $top) if $focused;
1943 }
1944 }
1945  
1946 sub recalc_icons
1947 {
1948 my $self = $_[0];
1949 my $hei = $self-> font-> height + 2;
1950 my ( $o, $c) = (
1951 $self-> {openedIcon} ? $self-> {openedIcon}-> height : 0,
1952 $self-> {closedIcon} ? $self-> {closedIcon}-> height : 0
1953 );
1954 my ( $ow, $cw) = (
1955 $self-> {openedIcon} ? ($self-> {openedIcon}-> width / $self-> {openedGlyphs}): 0,
1956 $self-> {closedIcon} ? ($self-> {closedIcon}-> width / $self-> {closedGlyphs}): 0
1957 );
1958 $hei = $o if $hei < $o;
1959 $hei = $c if $hei < $c;
1960 unless ( $unix) {
1961 for ( @drvImages) {
1962 next unless defined $_;
1963 my @s = $_-> size;
1964 $hei = $s[1] + 2 if $hei < $s[1] + 2;
1965 }
1966 }
1967 $self-> itemHeight( $hei);
1968 my ( $mw, $mh) = ( $ow, $o);
1969 $mw = $cw if $mw < $cw;
1970 $mh = $c if $mh < $c;
1971 $self-> {iconSizes} = [ $mw, $mh];
1972 }
1973  
1974 sub on_fontchanged
1975 {
1976 my $self = shift;
1977 $self-> recalc_icons;
1978 $self-> {fontHeight} = $self-> font-> height;
1979 $self-> SUPER::on_fontchanged(@_);
1980 }
1981  
1982 sub on_measureitem
1983 {
1984 my ( $self, $node, $level, $result) = @_;
1985 my $tw = $self-> get_text_width( $node-> [0]-> [0]) + $self-> {indent} / 4;
1986  
1987 unless ( length $node-> [0]-> [1]) { #i.e. root
1988 if ( $unix) {
1989 $node-> [0]-> [2] = $self-> {iconSizes}-> [0];
1990 } else {
1991 my $dt = Prima::Utils::query_drive_type($node-> [0]-> [0]) - dt::Floppy;
1992 $node-> [0]-> [2] = $drvImages[$dt] ? $drvImages[$dt]-> width : 0;
1993 $node-> [0]-> [3] = $drvImages[$dt];
1994 }
1995 $tw += $node-> [0]-> [2];
1996 } else {
1997 $tw += $self-> {iconSizes}-> [0];
1998 }
1999 $$result = $tw;
2000 }
2001  
2002 sub on_stringify
2003 {
2004 my ( $self, $node, $result) = @_;
2005 $$result = $node-> [0]-> [0];
2006 }
2007  
2008 sub get_directory_tree
2009 {
2010 my ( $self, $path) = @_;
2011 my @fs = Prima::Utils::getdir( $path);
2012 return [] unless scalar @fs;
2013 my $oldPointer = $::application-> pointer;
2014 $::application-> pointer( cr::Wait);
2015 my $i;
2016 my @fs1;
2017 my @fs2;
2018 for ( $i = 0; $i < scalar @fs; $i += 2) {
2019 push( @fs1, $fs[ $i]);
2020 push( @fs2, $fs[ $i + 1]);
2021 }
2022  
2023 $self-> {files} = \@fs1;
2024 $self-> {filesStat} = \@fs2;
2025 my @d;
2026 if ( $self-> {showDotDirs}) {
2027 @d = grep { $_ ne '.' && $_ ne '..' } $self-> files( 'dir');
2028 push @d, grep { -d "$path/$_" } $self-> files( 'lnk');
2029 } else {
2030 @d = grep { !/\./ } $self-> files( 'dir');
2031 push @d, grep { !/\./ && -d "$path/$_" } $self-> files( 'lnk');
2032 }
2033 @d = sort @d;
2034 my $ind = 0;
2035 my @lb;
2036 for (@d) {
2037 my $pathp = "$path/$_";
2038 @fs = Prima::Utils::getdir( "$path/$_");
2039 @fs1 = ();
2040 @fs2 = ();
2041 for ( $i = 0; $i < scalar @fs; $i += 2) {
2042 push( @fs1, $fs[ $i]);
2043 push( @fs2, $fs[ $i + 1]);
2044 }
2045 $self-> {files} = \@fs1;
2046 $self-> {filesStat} = \@fs2;
2047 my @dd;
2048 if ( $self-> {showDotDirs}) {
2049 @dd = grep { $_ ne '.' && $_ ne '..' } $self-> files( 'dir');
2050 push @dd, grep { -d "$pathp/$_" } $self-> files( 'lnk');
2051 } else {
2052 @dd = grep { !/\./ } $self-> files( 'dir');
2053 push @dd, grep { !/\./ && -d "$pathp/$_" } $self-> files( 'lnk');
2054 }
2055 push @lb, [[ $_, $path . ( $path eq '/' ? '' : '/')], scalar @dd ? [] : undef, 0];
2056 }
2057 $::application-> pointer( $oldPointer);
2058 return \@lb;
2059 }
2060  
2061 sub files {
2062 my ( $fn, $fs) = ( $_[0]-> {files}, $_[0]-> {filesStat});
2063 return wantarray ? @$fn : $fn unless ($#_);
2064 my @f;
2065 for ( my $i = 0; $i < scalar @$fn; $i++)
2066 {
2067 push ( @f, $$fn[$i]) if $$fs[$i] eq $_[1];
2068 }
2069 return wantarray ? @f : \@f;
2070 }
2071  
2072 sub on_expand
2073 {
2074 my ( $self, $node, $action) = @_;
2075 return unless $action;
2076 my $x = $self-> get_directory_tree( $node-> [0]-> [1].$node-> [0]-> [0]);
2077 $node-> [1] = $x;
2078 # another valid way of doing the same -
2079 # $self-> delete_items( $node);
2080 # $self-> insert_items( $node, 0, @$x); but since on_expand is never called directly,
2081 # adjust() will call necessary update functions for us.
2082 }
2083  
2084 sub path
2085 {
2086 my $self = $_[0];
2087 unless ( $#_) {
2088 my ( $n, $l) = $self-> get_item( $self-> focusedItem);
2089 return '' unless $n;
2090 return $n-> [0]-> [1].$n-> [0]-> [0];
2091 }
2092 my $p = $_[1];
2093 $p =~ s{^([^\\\/]*[\\\/][^\\\/]*)[\\\/]$}{$1};
2094 unless ( scalar( stat $p)) {
2095 $p = "";
2096 } else {
2097 $p = eval { Cwd::abs_path($p) };
2098 $p = "." if $@;
2099 $p = "" unless -d $p;
2100 $p = '' if !$self-> {showDotDirs} && $p =~ /\./;
2101 $p .= '/' unless $p =~ m{[/\\]$};
2102 }
2103  
2104 $self-> {path} = $p;
2105 if ( $p eq '/') {
2106 $self-> focusedItem(0);
2107 return;
2108 }
2109  
2110 $p = lc $p unless $unix;
2111 my @ups = split /[\/\\]/, $p;
2112 my $root;
2113 if ( $unix) {
2114 shift @ups if $ups[0] eq '';
2115 $root = $self-> {items}-> [0];
2116 } else {
2117 my $lr = shift @ups;
2118 for ( @{$self-> {items}}) {
2119 my $drive = lc $_-> [0]-> [0];
2120 $root = $_, last if $lr eq $drive;
2121 }
2122 return unless defined $root;
2123 }
2124  
2125 UPS: for ( @ups) {
2126 last UPS unless defined $root-> [1];
2127 my $subdir = $_;
2128 unless ( $root-> [2]) {
2129 my ( $idx, $lev) = $self-> get_index( $root);
2130 $self-> adjust( $idx, 1);
2131 }
2132 BRANCH: for ( @{$root-> [1]}) {
2133 next unless lc($_-> [0]-> [0]) eq lc($subdir);
2134 $root = $_;
2135 last BRANCH;
2136 }
2137 }
2138  
2139 my ( $idx, $lev) = $self-> get_index( $root);
2140 $self-> focusedItem( $idx);
2141 $self-> adjust( $idx, 1);
2142 $self-> topItem( $idx);
2143 }
2144  
2145 sub openedIcon
2146 {
2147 return $_[0]-> {openedIcon} unless $#_;
2148 $_[0]-> {openedIcon} = $_[1];
2149 $_[0]-> recalc_icons;
2150 $_[0]-> calibrate;
2151 }
2152  
2153 sub closedIcon
2154 {
2155 return $_[0]-> {closedIcon} unless $#_;
2156 $_[0]-> {closedIcon} = $_[1];
2157 $_[0]-> recalc_icons;
2158 $_[0]-> calibrate;
2159 }
2160  
2161 sub openedGlyphs
2162 {
2163 return $_[0]-> {openedGlyphs} unless $#_;
2164 $_[1] = 1 if $_[1] < 1;
2165 $_[0]-> {openedGlyphs} = $_[1];
2166 $_[0]-> recalc_icons;
2167 $_[0]-> calibrate;
2168 }
2169  
2170 sub closedGlyphs
2171 {
2172 return $_[0]-> {closedGlyphs} unless $#_;
2173 $_[1] = 1 if $_[1] < 1;
2174 $_[0]-> {closedGlyphs} = $_[1];
2175 $_[0]-> recalc_icons;
2176 $_[0]-> calibrate;
2177 }
2178  
2179 sub showDotDirs
2180 {
2181 return $_[0]-> {showDotDirs} unless $#_;
2182 my $p = $_[0]-> path;
2183 $_[0]-> {showDotDirs} = $_[1];
2184 $_[0]-> init_tree;
2185 $_[0]-> {path} = '';
2186 $_[0]-> path($p);
2187 }
2188  
2189 1;
2190  
2191 __END__