PLab/ImageApp.pm untested

filter: Statementstmt 0.0% 0 / 900 Branchbran 0.0% 0 / 286 Conditioncond 0.0% 0 / 59 MC/DCmcdc 0.0% 0 / 35 Subroutinesub 0.0% 0 / 88 Podpod 0.0% 0 / 75 totaltotal 0.0% 0 / 1443 CC 144 SCAR 36.0
CC
144
Cov
0%
opt_propcreate
182.0
dlg_file
90.0
on_create
90.0
CRAP 36.5
1 package App::PLab::ImageApp;
2 use vars qw($testing $VERSION);
3  
4 $VERSION = '1.00';
5  
6 use strict;
7 use Carp;
8 use Cwd qw(abs_path);
9  
10 use Prima qw( StartupWindow StdBitmap Widgets StdDlg ImageViewer MsgBox
11 IniFile Sliders Utils Notebooks ComboBox Buttons Label Outlines
12 ColorDialog KeySelector ImageDialog);
13 use App::PLab::ButtonGlyphs;
14 use App::PLab::ImageAppGlyphs;
15 no Prima::StartupWindow;
16  
17 package App::PLab::SerOpenDialog;
18 use vars qw(@ISA);
19 @ISA = qw(Prima::ImageOpenDialog);
20  
21 sub init
22 {
23 my $self = shift;
24 my %profile = $self-> SUPER::init(@_);
25  
26 my $dl = $self-> Files;
27  
28 my %pf = (
29 name => 'Files',
30 rect => [ $dl-> rect],
31 items => $dl-> items,
32 designScale => undef,
33 delegations => [ @{$dl-> delegations}, $self, 'DrawItem', 'MeasureItem'],
34 );
35 $dl-> destroy;
36 $self-> insert( ListViewer => %pf);
37  
38 return %profile;
39 }
40  
41 sub Dir_Change
42 {
43 my ( $self, $dir) = @_;
44 my $w = $self-> owner;
45 my $mask = $self-> {mask};
46 my @files = grep { /$mask/i; } $dir-> files( 'reg');
47 @files = sort {uc($a) cmp uc($b)} @files if $self->{sorted};
48  
49 my %vecs = ();
50 my $nums = $w-> {cypherMask};
51 for (@files)
52 {
53 next unless /^(.+)(\d{$nums})\.([^\.]*)$/;
54 my ($sername, $num, $ext) = ($1, $2, $3);
55 $vecs{ $sername} = [ pack( "b1000", "0" x 100), $ext] unless exists $vecs{ $sername};
56 vec( $vecs{ $sername}->[0], $num, 1) = 1;
57 }
58 my @series = ();
59 my $max = 10 ** $nums - 1;
60 for my $sername (sort keys %vecs)
61 {
62 my ($j, $k);
63 my $s = unpack( b1000 => $vecs{ $sername}->[0]); # to exploit string functions
64 $j = index( $s, '1');
65 while ( $j >= 0)
66 {
67 $k = index( $s, '0', $j);
68 if ( $k < 0)
69 {
70 $s = '';
71 push @series, [ $sername , $vecs{ $sername}->[1],
72 sprintf("%0${nums}d",$j), sprintf("%0${nums}d",$max)] if $j < $max;
73 }
74 else
75 {
76 $s = ('0' x $k) . substr( $s, $k);
77 push @series, [ $sername , $vecs{ $sername}->[1],
78 sprintf("%0${nums}d",$j), sprintf("%0${nums}d",$k - 1)] if $j < $k - 1;
79 }
80 $j = index( $s, '1');
81 }
82 }
83  
84 $self-> {series} = [@series];
85 $self-> Files-> items([ map { "$$_[0]$$_[2].$$_[1]"} @series]);
86 $self-> Directory_FontChanged( $self-> Directory);
87 }
88  
89 sub Files_DrawItem
90 {
91 my ( $dlg, $me, $canvas, $index, $left, $bottom, $right, $top, $hilite, $focused) = @_;
92 return unless $dlg-> {series};
93 my $backColor = $hilite ? $me-> hiliteBackColor : $me-> backColor;
94 my $color = $hilite ? $me-> hiliteColor : cl::Fore | wc::ListBox;
95 $canvas-> color($backColor);
96 $canvas-> bar( $left, $bottom, $right, $top);
97 $canvas-> color($color);
98 my @ser = @{$dlg->{series}->[$index]};
99 my $text = "$ser[0]$ser[2].$ser[1]";
100 my $series = "$ser[2]-$ser[3]";
101 my $font = $canvas-> font;
102 my $h = $canvas-> font-> height;
103 my $w = $canvas-> get_text_width( $text);
104 $canvas-> text_out( $text, $left + 2, ($top + $bottom + 1 - $h) / 2);
105 $canvas-> font( height => $canvas-> font-> height - 2, pitch => fp::Fixed, style => fs::Bold);
106 $h = $canvas-> font-> height;
107 my $x = $left + $canvas-> get_text_width(' ') + $w;
108 $w = $canvas-> get_text_width( "9");
109 $canvas-> text_out( $series, $x + 1, ($top + $bottom + 1 - $h) / 2);
110 $canvas-> rectangle( $x, ($top + $bottom + 1 - $h) / 2, $x + $w * length( $series) + 2, ($top + $bottom + $h) / 2);
111 $canvas-> font( $font);
112 }
113  
114 sub Files_MeasureItem
115 {
116 my ( $dlg, $self, $index, $sref) = @_;
117 $$sref = $self->get_text_width( $self-> get_item_text( $index)) + $self->get_text_width('m') * 8;
118 $self-> clear_event;
119 }
120  
121 package App::PLab::TrDeviceBitmap;
122 use vars qw(@ISA);
123 @ISA = qw(Prima::DeviceBitmap);
124  
125 sub translate
126 {
127 unless ( $#_) {
128 my @a = $_[0]-> SUPER::translate;
129 if ( $_[0]-> {override}) {
130 $a[$_] -= $_[0]-> {override}->[$_] for 0,1;
131 }
132 return @a;
133 }
134 my ( $self, $trx, $try) = @_;
135 if ( $self-> {override}) {
136 $trx += $self-> {override}-> [0];
137 $try += $self-> {override}-> [1];
138 }
139 $self-> SUPER::translate( $trx, $try);
140 }
141  
142 sub clipRect
143 {
144 unless ( $#_) {
145 my @a = $_[0]-> SUPER::clipRect;
146 if ( $_[0]-> {override}) {
147 $a[$_] -= $_[0]-> {override}->[$_] for 0,1;
148 $a[$_+2] -= $_[0]-> {override}->[$_] for 0,1;
149 }
150 return @a;
151 }
152 my ( $self, $trx, $try, $tax, $tay) = @_;
153 if ( $self-> {override}) {
154 $trx += $self-> {override}-> [0];
155 $try += $self-> {override}-> [1];
156 $tax += $self-> {override}-> [0];
157 $tay += $self-> {override}-> [1];
158 }
159 $self-> SUPER::clipRect( $trx, $try, $tax, $tay);
160 }
161  
162 package App::PLab::MagnifyingGlassWidget;
163 use vars qw(@ISA);
164 @ISA = qw(Prima::Widget);
165  
166 sub profile_default
167 {
168 my $def = $_[ 0]-> SUPER::profile_default;
169 my %prf = (
170 width => 154,
171 height => 102,
172 syncPaint => 0,
173 );
174 @$def{keys %prf} = values %prf;
175 return $def;
176 }
177  
178 sub on_create
179 {
180 $_[0]-> {canvas} = App::PLab::TrDeviceBitmap-> create(
181 width => 76,
182 height => 50,
183 backColor => $_[0]-> owner-> backColor,
184 );
185 }
186  
187 sub on_paint
188 {
189 $_[1]-> rectangle( 0, 0, 153, 101);
190 $_[1]-> stretch_image( 1, 1, 152, 100, $_[0]-> {canvas});
191 }
192  
193  
194 package App::PLab::TrImageViewer;
195 use vars qw(@ISA);
196 @ISA = qw(Prima::ImageViewer);
197  
198 sub invalidate_rect
199 {
200 my ( $self, @r) = @_;
201 my $w = $self-> owner;
202 $w-> ivm_repaint( $self) if $w-> {magnify};
203 $self-> SUPER::invalidate_rect( @r);
204 }
205  
206 sub end_paint
207 {
208 my $self = $_[0];
209 $self-> SUPER::end_paint;
210 my $w = $self-> owner;
211 $w-> ivm_repaint( $self) if $w-> {magnify};
212 }
213  
214 sub capture
215 {
216 return $_[0]-> SUPER::capture unless $#_;
217 my ( $self, $capFlag, @rest) = @_;
218 $self-> owner-> iv_cancelmagnify( $self)
219 if !$capFlag && $self-> owner-> {magnify};
220 $self-> SUPER::capture( $capFlag, @rest);
221 }
222  
223 sub screen2point
224 {
225 return map { int } shift-> SUPER::screen2point( @_);
226 }
227  
228 sub point2screen
229 {
230 return map { int } shift-> SUPER::point2screen( @_);
231 }
232  
233 package App::PLab::ImageAppWindow;
234 use vars qw(@ISA %dlgProfile $ico $pointClickTolerance);
235 @ISA = qw(Prima::Window);
236 $pointClickTolerance = 8;
237  
238 %dlgProfile = (
239 centered => 1,
240 visible => 0,
241 designScale => [7, 16],
242 );
243 $ico = Prima::Icon-> create;
244 $ico-> combine(
245 Prima::Image-> create(
246 width => 32,
247 height => 32,
248 type => im::Mono,
249 palette => [0,0,0,255,255,255],
250 data => pack( 'C*' ,
251 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0,15,244,0,0,3,194,0,0,15,250,
252 0,0,31,253,0,0,63,253,0,0,127,254,128,0,127,254,128,0,231,255,64,0,215,255,64,1,182,219,64,1,
253 86,219,64,0,142,219,64,0,13,183,0,0,29,182,0,0,59,180,0,0,55,104,0,0,11,96,0,0,0,128,0,0,0,0,
254 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
255 ),
256 ),
257 Prima::Image-> create(
258 width => 32,
259 height => 32,
260 type => im::Mono,
261 palette => [0,0,0,255,255,255],
262 data => pack( 'C*',
263 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
264 255,255,255,255,255,240,7,255,255,224,3,255,255,224,1,255,255,224,1,255,255,192,0,255,255,128,0,
265 255,255,0,0,127,255,0,0,127,254,0,0,63,254,0,0,63,252,0,0,63,252,32,0,63,254,96,0,63,255,224,0,
266 127,255,192,0,255,255,128,1,255,255,128,3,255,255,192,15,255,255,252,31,255,255,255,255,255,255,
267 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
268 ),
269 ),
270 );
271  
272 sub dlg_okcancel
273 {
274 my ( $w, $self) = @_;
275 $self-> insert( Button =>
276 origin => [ 10, 20],
277 size => [ 96, 36],
278 text => '~Ok',
279 name => 'OK',
280 default => 1,
281 modalResult => mb::Ok,
282 );
283 $self-> insert( Button =>
284 origin => [ 116, 20],
285 size => [ 96, 36],
286 text => 'Cancel',
287 modalResult => mb::Cancel,
288 );
289 }
290  
291 sub dlg_file
292 {
293 my ( $w, %profile) = @_;
294 my $d = $w-> {fileDlg} ? $w-> {fileDlg} : Prima::OpenDialog-> create(
295 owner => $w,
296 );
297 if ( $profile{cwd}) {
298 my $dir = exists($profile{directory}) ? $profile{directory} : '.';
299 $dir = eval { Cwd::abs_path( $dir)};
300 $dir = '.' if $@;
301 $dir = '' unless -d $dir;
302 delete $profile{cwd};
303 $profile{directory} = $dir;
304 }
305 if ( exists $profile{directory}) {
306 my $xd = eval { Cwd::abs_path( $d-> directory)};
307 $xd = '.' if $@;
308 $d-> directory( $profile{directory}) if $xd ne $profile{directory};
309 delete $profile{directory};
310 }
311 $d-> set( %profile);
312 $w-> {fileDlg} = $d;
313 return $d;
314 }
315  
316 sub open_help
317 {
318 my ( $self, $address) = @_;
319 $address = 'index' unless defined $address;
320 $::application->open_help('App::PLab::' . $address);
321 }
322  
323 # WIN
324  
325 sub modified
326 {
327 return $_[0]-> {modified} unless $#_;
328 $_[0]-> {modified} = $_[1];
329 }
330  
331 sub on_keydown
332 {
333 my ( $self, $code, $key, $mod) = @_;
334 my $iv = $self->IV;
335 if ( $key == kb::Esc) {
336 if ( $iv-> {transaction}) {
337 $self-> iv_cancelmode( $iv);
338 $self-> clear_event;
339 return;
340 }
341 }
342 $self-> clear_event if $iv-> {transaction};
343 }
344  
345 sub on_deactivate
346 {
347 my ( $self) = @_;
348 $self-> iv_cancelmode( $self-> IV);
349 }
350  
351  
352 sub on_mousewheel
353 {
354 my ( $self, $mod, $x, $y, $z) = @_;
355 $self = $self-> IV;
356 return if $self->{transaction};
357 $z = int( $z / 120);
358 my $xv = ( $mod & km::Shift) ? $self-> VScroll : $self-> HScroll;
359 $z *= ( $mod & km::Ctrl) ? $xv-> step : $xv-> pageStep;
360 if ( $mod & km::Shift) {
361 $self-> deltaX( $self-> deltaX - $z);
362 } else {
363 $self-> deltaY( $self-> deltaY - $z);
364 }
365 }
366  
367 sub on_create
368 {
369 my $w = $_[0];
370 my $uname = lc(Prima::Utils::username() || '');
371 $uname .= '.' if $uname;
372 my @iniDefaults = $w-> win_inidefaults();
373 my $keys = $w-> opt_keys;
374 my $colors = $w-> opt_colors;
375 push ( @iniDefaults,
376 map {
377 ( "Key_$_", "'" . $keys->{$_}->[0] . "'")
378 } keys %$keys);
379 push( @iniDefaults,
380 map {
381 ( "Color_$_", $w-> ini_makecolor( $colors->{$_}->[0]))
382 } keys %$colors);
383 $w->{iniFile} = Prima::IniFile-> create(
384 file => Prima::Utils::path('PLabApps'),
385 default => [
386 $uname.$::application->name => \@iniDefaults,
387 ]
388 );
389 my $i;
390 $w-> {uname} = $uname;
391 $i = $w-> {ini} = $w-> {iniFile}-> section( $uname.$::application->name);
392 $w-> {keyMappings} = { map { $_ => Prima::AbstractMenu-> translate_shortcut( eval($i-> {$_})); }
393 grep { m/^Key_/ } keys %$i };
394 $i->{"Color_$_"} = eval( $i->{"Color_$_"}) for keys %$colors;
395 my @rc = split ' ', $i->{WindowRect};
396 my @ac = $::application-> rect;
397 $ac[3] -= $::application-> get_system_value( sv::YMenu) * 3 + $::application-> get_system_value( sv::YTitleBar);
398 $rc[0] = 0 if $rc[0] >= $ac[2];
399 $rc[1] = 0 if $rc[1] >= $ac[3];
400 $rc[3] = $ac[3] if $rc[3] >= $ac[3];
401 $w-> rect(@rc);
402 $w-> {cypherMask} = ( lc $w->{ini}->{SerType} eq 'long') ? 3 : 2;
403 -d $w-> {ini}-> {path} or $w-> {ini}-> {path} = '.';
404 $w-> {ini}-> {path} = eval {&Cwd::abs_path($w-> {ini}-> {path})};
405 $w-> {ini}-> {path} = '.' if $@;
406 $w-> {ini}-> {path} = '' unless -d $w-> {ini}-> {path};
407  
408 $w-> maximize if $i->{MaxState};
409 $w-> insert( Popup =>
410 auto => 0,
411 selected => 0,
412 name => 'LoadSeriesPopup',
413 items => [['' => '']],
414 );
415 $::application-> showHint( $w-> {ini}-> {showHint});
416 $w-> winmenu_updatemappings;
417 }
418  
419 sub on_destroy
420 {
421 my ($w,$i) = ($_[0],$_[0]->{ini});
422 $i->{MaxState} = $w-> windowState == ws::Maximized;
423 $w-> windowState(ws::Normal) if $i->{MaxState};
424 my @rc = $w-> rect;
425 $i->{WindowRect} = "@rc";
426 $i->{SerType} = ( $w->{cypherMask} == 2) ? 'Short' : 'Long';
427 $i-> {$_} = Prima::KeySelector::shortcut( $w-> {keyMappings}->{$_})
428 for keys %{$w->{keyMappings}};
429 my $optColors = $w-> opt_colors;
430 $i-> {"Color_$_"} = $w-> ini_makecolor( $i-> {"Color_$_"}) for keys %$optColors;
431 $::application-> close;
432 }
433  
434 sub on_close
435 {
436 my ($w,$i) = ($_[0],$_[0]->{ini});
437 $w-> clear_event unless $w-> win_closefile;
438 }
439  
440  
441 sub win_inidefaults
442 {
443 my $w = $_[0];
444 my @rc = $w-> rect;
445 return (
446 path => '.',
447 WindowRect => "@rc",
448 MaxState => 0,
449 SerType => 'Short',
450 extSaveDir => '',
451 silentExtDir => 0,
452 dirTimeout => 120,
453 statusDelay => 5,
454 showHint => 1,
455 );
456 }
457  
458 sub win_extname
459 {
460 my ($w, $extname) = @_;
461 my $ext = $w-> {dataExt};
462 $extname =~ /([^\\\/]*)$/;
463 $extname = $1;
464 if ( $extname =~ /\.[^.]*$/) {
465 $extname =~ s/\.[^.]*$/\.$ext/
466 } else {
467 $extname .= ".$ext";
468 }
469 my $extsave = length($w-> {ini}-> {extSaveDir}) ? $w-> {ini}-> {extSaveDir} : $w-> {ini}-> {path};
470 return "$extsave/$extname";
471 }
472  
473 sub win_extwarn
474 {
475 my $w = $_[0];
476 return unless length $w->{ini}->{extSaveDir};
477 return if $w->{ini}->{silentExtDir};
478 my $ret = Prima::MsgBox::message_box( $::application-> name, ".".$w-> {dataExt}." save path is ".
479 $w->{ini}->{extSaveDir}.".\nDo you want to set it to the current directory, as default?",
480 mb::YesNo | mb::Abort | mb::Warning, {
481 buttons => { mb::Abort, { text => 'Ne~ver' }},
482 });
483 if ( $ret == mb::Yes) {
484 $w->{ini}->{extSaveDir} = '';
485 $w-> win_extpathchanged;
486 } elsif ( $ret == mb::Abort) {
487 Prima::MsgBox::message("You will be never asked again, until you manually re-set the save path to the default", mb::Information);
488 $w->{ini}->{silentExtDir} = 1;
489 }
490 }
491  
492  
493 sub win_extpathchanged
494 {
495 }
496  
497 sub win_newframe
498 {
499 $_[0]-> modified( 0);
500 }
501  
502  
503 sub win_saveframe
504 {
505 return 1;
506 }
507  
508 sub win_closeframe
509 {
510 }
511  
512 sub win_framechanged
513 {
514 my $w = $_[0];
515 $w-> menu-> FileCloseImage-> enabled( defined $w-> {file});
516 }
517  
518 sub win_newextras
519 {
520 my $w = $_[0];
521 my $num = $w-> {cypherMask};
522 my $file = $w->{file};
523 return unless $file;
524 my ($fileNum, $fileBeg, $fileEnd, $ff);
525 if ( $file =~ /(.*)(\d{$num})(\.(?:tif|gif|jpg|bmp|pcx|png))$/i) {
526 ($fileBeg,$fileNum,$fileEnd) = ($1,$2,$3);
527 $w-> {prevFile} = $ff
528 if ( $fileNum > 0) && ( -f ($ff = sprintf("%s%0${num}d%s",$fileBeg,$fileNum-1,$fileEnd)));
529 $w-> {nextFile} = $ff
530 if ( $fileNum < 10 ** $num - 1) && ( -f ($ff = sprintf("%s%0${num}d%s",$fileBeg,$fileNum+1,$fileEnd)));
531 $w-> {fileBeg} = $fileBeg;
532 $w-> {fileEnd} = $fileEnd;
533 $w-> {fileNum} = int($fileNum);
534 }
535 }
536  
537 sub win_closeextras
538 {
539 my $w = $_[0];
540 $w-> {$_} = undef for qw( nextFile prevFile fileNum fileBeg fileEnd);
541 }
542  
543 sub win_extraschanged
544 {
545 my $w = $_[0];
546 my $tb = $w-> ToolBar;
547 $w-> menu-> FileNextImage-> enabled( defined $w-> {nextFile});
548 $tb-> NextImage-> enabled( defined $w-> {nextFile});
549 $w-> menu-> FilePrevImage-> enabled( defined $w-> {prevFile});
550 $tb-> PrevImage-> enabled( defined $w-> {prevFile});
551 $w-> menu-> FileNext5Image-> enabled( defined $w-> {nextFile});
552 $w-> menu-> FilePrev5Image-> enabled( defined $w-> {prevFile});
553 $w-> menu-> FileLastImage -> enabled( defined $w-> {nextFile});
554 $w-> menu-> FileFirstImage-> enabled( defined $w-> {prevFile});
555 }
556  
557 sub win_openfile
558 {
559 my $w = $_[0];
560 my $d = $w-> dlg_file(
561 cwd => 1,
562 directory => $w->{ini}->{path},
563 filterIndex => 0,
564 multiSelect => 0,
565 filter => [
566 ['Images' => '*.bmp;*.pcx;*.gif;*.jpg;*.png;*.tif'],
567 ['All files' => '*.*'],
568 ]
569 );
570 if ( defined $w->{file} && $w-> {file} =~ /([^\\\/]*)$/) {
571 my $fname = $1;
572 my $i = 0;
573 my @items = @{$d-> Files-> items};
574 for ( @items) {
575 last if $fname eq $items[ $i];
576 $i++;
577 }
578 if ( $i <= @items) {
579 $d-> Files-> focusedItem( $i);
580 $d-> fileName( $fname);
581 }
582 }
583 $w-> iv_cancelmagnify( $w-> IV);
584 return $w-> win_loadfile( $d->fileName) if $d-> execute;
585 return 0;
586 }
587  
588 sub win_openserfile
589 {
590 my $w = $_[0];
591 my $dir = eval { Cwd::abs_path( $w-> {ini}-> {path})};
592 $dir = '.' if $@;
593 $dir = '' unless -d $dir;
594 my $d = $w->{fileserDlg} ? $w->{fileserDlg} : App::PLab::SerOpenDialog-> create(
595 owner => $w,
596 directory => $dir,
597 filter => [
598 ['Images' => '*.bmp;*.pcx;*.gif;*.jpg;*.png;*.tif'],
599 ['All files' => '*.*'],
600 ]
601 );
602 $w-> iv_cancelmode( $w-> IV);
603 if ( $d-> execute) {
604 $w-> win_loadfile( $d-> fileName);
605 }
606 $w->{fileserDlg} = $d;
607 }
608  
609  
610 sub win_loadfile
611 {
612 my ($w,$file) = @_;
613 return 0 unless defined $file;
614 my $path = $file;
615 $path =~ s{[/\\][^/\\]*$}{};
616 $w-> {preloadfile} = $file;
617  
618 if ( defined $w-> {file}) {
619 return unless $w-> win_saveframe;
620 }
621  
622 my $self = $w-> IV;
623 $w-> iv_cancelmode( $self);
624 $self-> {savePointer} = $self->pointer;
625 $self-> pointer(cr::Wait);
626 my $i = Prima::Image-> create;
627 unless ( $i-> load( $file)) {
628 $self->pointer($self-> {savePointer});
629 Prima::MsgBox::message_box( $::application-> name, "Error loading file $file", mb::OK|mb::Error);
630 return 0;
631 }
632 $w-> {preloadfile} = undef;
633  
634 $w-> win_closeextras;
635 $w-> win_closeframe;
636 $file =~ m{[/\\]([^/\\]*)$};
637 $w-> text( $::application-> name . " - [$1]");
638 $w-> {ini}-> {path} = $path;
639 $w-> {file} = $file;
640 $w-> IV-> image( $i);
641 ($w-> {IVx}, $w->{IVy}) = $w-> IV-> image-> size;
642  
643 $w-> win_newframe;
644 $w-> win_newextras;
645 $w-> win_framechanged;
646 $w-> win_extraschanged;
647  
648 $w-> IV-> repaint;
649  
650 $w-> sb_text( "$file loaded OK");
651 $self-> pointer($self-> {savePointer});
652  
653 return 1;
654 }
655  
656 sub win_closefile
657 {
658 my $w = $_[0];
659 return 1 unless defined $w->{file};
660 return 0 if !$w-> win_saveframe;
661 $w-> win_closeframe;
662 $w-> win_closeextras;
663  
664 $w-> text( $::application-> name);
665 $w-> {file} = undef;
666 $w-> IV-> image( undef);
667 $w-> {IVx} = $w-> {IVy} = 0;
668 $w-> IV-> repaint;
669 $w-> win_framechanged;
670 $w-> win_extraschanged;
671 return 1;
672 }
673  
674  
675 sub win_nextfile
676 {
677 $_[0]-> win_loadfile( $_[0]-> {nextFile}) if defined $_[0]-> {nextFile};
678 }
679  
680 sub win_getseriesrange
681 {
682 my $w = $_[0];
683  
684 return (0,0) unless defined $w->{fileNum};
685 my $path = $w-> {ini}-> {path};
686 my @d;
687  
688 if ( exists $w-> {cachedDir} && ( $w-> {cachedDir} eq $path) &&
689 (( time - $w-> {cachedDirTime}) < $w-> {ini}-> {dirTimeout})) {
690 @d = @{$w-> {cachedDirContent}};
691 } else {
692 warn("Cannot read directory $path:$!"), return (0,0) unless opendir DIR, $path;
693 @d = readdir DIR;
694 closedir DIR;
695 $w-> {cachedDir} = $path;
696 $w-> {cachedDirTime} = time;
697 $w-> {cachedDirContent} = \@d;
698 }
699  
700 my ( $min, $max) = ( 0, 10 ** $w-> {cypherMask} - 1);
701 my ( $fmin, $fmax, $i) = ( $w->{fileNum}, $w->{fileNum});
702 my @bix = ();
703 my $num = $w->{cypherMask};
704 my $fbeg = $w->{fileBeg};
705 $fbeg =~ s{.*[/\\]([^/\\]*)$}{$1};
706  
707 for (@d) {
708 next unless /(.*)(\d{$num})(\.(?:tif|gif|jpg|bmp|pcx|png))$/i;
709 next unless $1 eq $fbeg and $3 eq $w->{fileEnd};
710 $bix[ $2] = 1;
711 }
712  
713 for ( $i = $w->{fileNum}; $i >= $min; $i--) {
714 last unless $bix[ $i];
715 $fmin = $i;
716 }
717  
718 for ( $i = $w->{fileNum}; $i <= $max; $i++) {
719 last unless $bix[ $i];
720 $fmax = $i;
721 }
722  
723 return $fmin, $fmax;
724 }
725  
726 sub win_formfilename
727 {
728 my ( $w, $fnum) = @_;
729 my $num = $w-> {cypherMask};
730 return sprintf("%s%0${num}d%s",$w->{fileBeg}, $fnum, $w->{fileEnd});
731 }
732  
733 sub win_next5file
734 {
735 my $w = $_[0];
736 return unless defined $w-> {nextFile};
737 my ( $min, $max) = $w-> win_getseriesrange;
738 $w-> win_loadfile( $w-> win_formfilename(( $w-> {fileNum} + 5 > $max) ? $max : ( $w-> {fileNum} + 5)));
739 }
740  
741 sub win_lastfile
742 {
743 my $w = $_[0];
744 return unless defined $w-> {nextFile};
745 my ( $min, $max) = $w-> win_getseriesrange;
746 $w-> win_loadfile( $w-> win_formfilename( $max));
747 }
748  
749 sub win_prevfile
750 {
751 $_[0]-> win_loadfile( $_[0]-> {prevFile}) if defined $_[0]-> {prevFile};
752 }
753  
754 sub win_prev5file
755 {
756 my $w = $_[0];
757 return unless defined $w-> {prevFile};
758 my ( $min, $max) = $w-> win_getseriesrange;
759 $w-> win_loadfile( $w-> win_formfilename(( $w-> {fileNum} - 5 < $min) ? $min : ( $w-> {fileNum} - 5)));
760 }
761  
762 sub win_firstfile
763 {
764 my $w = $_[0];
765 return unless defined $w-> {prevFile};
766 my ( $min, $max) = $w-> win_getseriesrange;
767 $w-> win_loadfile( $w-> win_formfilename( $min));
768 }
769  
770 sub winmenu_file
771 {
772 return
773 ["~File" => [
774 [ FileOpen => "~Open" => q(win_openfile)],
775 [ FileSerOpen => "Open ~series..." => q(win_openserfile)],
776 [ '-FileNextImage' => "~Next File" => q(win_nextfile)],
777 [ '-FilePrevImage' => "~Prev File" => q(win_prevfile)],
778 [ '-FileFirstImage' => "~First File" => q(win_firstfile)],
779 [ '-FileLastImage' => "~Last File" => q(win_lastfile)],
780 [ '-FileNext5Image' => "Next 5 files" => q(win_next5file)],
781 [ '-FilePrev5Image' => "Prev 5 files" => q(win_prev5file)],
782 [ '-FileCloseImage' => "~Close" => q(win_closefile)],
783 [],
784 [ FileExit => "E~xit" => sub{ $::application-> close }],
785 ]];
786 }
787  
788 sub winmenu_view
789 {
790 return
791 ['~View' => [
792 [ ViewNormal => '~Normal ( 100%)' => sub{$_[0]->IV->zoom(1.0)}],
793 [ ViewBestFit => '~Best fit' => sub { $_[0]-> iv_zbestfit($_[0]-> IV);} ],
794 [],
795 [ ViewAutoBestFit => '~Auto best fit' => sub{
796 $_[0]-> iv_zbestfit($_[0]->IV) if $_[0]->IV->{autoBestFit} = $_[0]->menu->ViewAutoBestFit-> toggle;
797 }],
798 [],
799 [ View25 => '25%' => sub{$_[0]->IV->zoom(0.25)}],
800 [ View50 => '50%' => sub{$_[0]->IV->zoom(0.5)}],
801 [ View75 => '75%' => sub{$_[0]->IV->zoom(0.75)}],
802 [ View150 => '150%' => sub{$_[0]->IV->zoom(1.5)}],
803 [ View200 => '200%' => sub{$_[0]->IV->zoom(2)}],
804 [ View300 => '300%' => sub{$_[0]->IV->zoom(3)}],
805 [ View400 => '400%' => sub{$_[0]->IV->zoom(4)}],
806 [ View600 => '600%' => sub{$_[0]->IV->zoom(6)}],
807 [ View1600 => '1600%' => sub{$_[0]->IV->zoom(16)}],
808 [],
809 [ ViewIncrease => '~Increase' => sub{$_[0]->IV->zoom( $_[0]->IV->zoom * 1.1); $_[0]-> sb_text("Zoom: ".$_[0]->IV->zoom);}],
810 [ ViewDecrease => '~Decrease' => sub{$_[0]->IV->zoom( $_[0]->IV->zoom / 1.1); $_[0]-> sb_text("Zoom: ".$_[0]->IV->zoom);}],
811 ]];
812 }
813  
814 sub win_emulatemouse
815 {
816 my ( $self, $button) = @_;
817 my @pos = $self-> IV-> pointerPos;
818 my $state = $self-> IV-> get_shift_state;
819 $self-> IV-> notify( 'MouseDown', $button, $state, @pos);
820 $self-> IV-> notify( 'MouseUp', $button, $state, @pos);
821 $self-> IV-> notify( 'MouseClick', $button, $state, @pos, 0);
822 }
823  
824 sub winmenu_edit
825 {
826 return
827 [ edit => '~Edit' => [
828 [ EditEmulation => '~Emulation' => [
829 [ 'EditEmulation1' => "Left mouse button" => sub { $_[0]-> win_emulatemouse(mb::Left)}],
830 [ 'EditEmulation2' => "Right mouse button" => sub { $_[0]-> win_emulatemouse(mb::Right)}],
831 [ 'EditEmulation3' => "Middle mouse button" => sub { $_[0]-> win_emulatemouse(mb::Middle)}],
832 ]],
833 [ EditProperties => "~Properties..." => q(opt_properties),],
834 ]];
835 }
836  
837 sub winmenu_options
838 {
839 return
840 ['~Options' => [
841 ]];
842 }
843  
844 sub winmenu_updatemappings
845 {
846 my $w = $_[0];
847 my $menu = $w-> menu;
848 for ( keys %{$w-> {keyMappings}}) {
849 next unless m/^Key_(.*)$/;
850 my ( $item, $value) = ( $1, $w-> {keyMappings}->{$_});
851 $menu-> key( $item, $value);
852 $menu-> accel( $item, ($value == kb::NoKey) ? '' : Prima::KeySelector::describe( $value));
853 }
854 }
855  
856 sub ini_makecolor
857 {
858 return sprintf( "0x%02x%02x%02x", ( $_[1] >> 16) & 0xFF, ( $_[1] >> 8) & 0xFF, $_[1] & 0xFF);
859 }
860  
861 # WIN_END
862 # OPT
863 sub opt_colormount
864 {
865 }
866  
867 sub opt_colors
868 {
869 return {};
870 }
871  
872 sub opt_keys
873 {
874 return {
875 'FileOpen' => [ kb::F3, "Open an image file"],
876 'FileSerOpen' => [ kb::NoKey, "Open series of files"],
877 'FileNextImage' => [ kb::Right, "Load next file in series"],
878 'FilePrevImage' => [ kb::Left, "Load previous file in series"],
879 'FileFirstImage' => [ kb::Home, "Load first in series"],
880 'FileLastImage' => [ kb::End, "Load last file in series"],
881 'FileNext5Image' => [ km::Ctrl|kb::Right, "Skip five files forward"],
882 'FilePrev5Image' => [ km::Ctrl|kb::Left, "Skip five files backward"],
883 'FileCloseImage' => [ kb::NoKey, "Close the opened file"],
884 'FileExit' => [ '@X', "Exit the program"],
885 'EditProperties' => [ kb::NoKey , "Edit program properties"],
886 'EditEmulation1' => [ 'a' , "Emulate left mouse button"],
887 'EditEmulation2' => [ 'd' , "Emulate right mouse button"],
888 'EditEmulation3' => [ 's' , "Emulate bottom mouse button"],
889 'ViewNormal' => [ '^1' , "Set image zoom factor to 1:1"],
890 'ViewBestFit' => [ '^Z' , "View image so it fits best to the window"],
891 'ViewAutoBestFit' => [ kb::NoKey , "Toggle automatic 'best fitting' options"],
892 'View50' => [ '^5' , "Set image zoom factor to 1:2"],
893 'View200' => [ '^2', "Set image zoom factor to 2:1"],
894 'View300' => [ '^3', "Set image zoom factor to 3:1"],
895 'View400' => [ '^4', "Set image zoom factor to 4:1"],
896 'View600' => [ '^6', "Set image zoom factor to 6:1"],
897 'View25' => [ kb::NoKey , "Set image zoom factor to 1:4"],
898 'View75' => [ kb::NoKey , "Set image zoom factor to 3:4"],
899 'View150' => [ kb::NoKey , "Set image zoom factor to 3:2"],
900 'View1600' => [ kb::NoKey , "Set image zoom factor to 16:1"],
901 'ViewIncrease' => [ '+' , "Increase image zoom factor by 10%"],
902 'ViewDecrease' => [ '-' , "Decrease image zoom factor by 10%"],
903 }
904 }
905  
906  
907 sub opt_propcreate
908 {
909 my ( $w, $dlg, $nb, $nbpages) = @_;
910 # General
911 my $nbgrp = $nb-> insert_to_page( 0, RadioGroup =>
912 name => 'RG_SeriesType',
913 origin => [ 10, 70],
914 size => [ 197, 53],
915 text => 'Series type',
916 );
917 $nbgrp-> insert( [ Radio =>
918 origin => [ 9, 5],
919 name => 'RG_Short',
920 size => [ 89, 28],
921 text => '~Short',
922 hint => 'Sets file grouping mask as file00.ext, from 0 to 99',
923 ], [ Radio =>
924 origin => [ 102, 5],
925 name => 'RG_Long',
926 size => [ 89, 28],
927 text => '~Long',
928 hint => 'Sets file grouping mask as file000.ext, from 0 to 999',
929 ]);
930  
931  
932 $nb-> insert_to_page( 0,
933 [ Label =>
934 origin => [ 10, 28],
935 size => [ 100, 36],
936 autoWidth => 0,
937 text => 'Save path:',
938 valignment => ta::Middle,
939 ], [ CheckBox =>
940 origin => [ 110, 28],
941 size => [ 374, 36],
942 name => 'UseDef',
943 text => '~Use default save path',
944 onClick => sub {
945 $_[0]-> owner-> Path-> enabled( !$_[0]-> checked);
946 },
947 hint => 'Default path is where image file located',
948 ], [ InputLine =>
949 origin => [ 10, 10],
950 size => [ 341, 20],
951 name => 'Path',
952 ], [ SpeedButton =>
953 origin => [ 354, 10],
954 name => 'VB::Button1',
955 size => [ 26, 20],
956 text => '...',
957 borderWidth => 1,
958 hint => 'Select custom directory where additional files will be saved',
959 onClick => sub {
960 my $d = defined $w->{dirpt} ? $w->{dirpt} : Prima::ChDirDialog-> create;
961 $w->{dirpt} = $d;
962 $d-> directory( $_[0]-> owner-> Path-> text);
963 if ( $d-> execute != mb::Cancel) {
964 $_[0]-> owner-> UseDef-> uncheck;
965 $_[0]-> owner-> Path-> enabled(1);
966 $_[0]-> owner-> Path-> text( $d-> directory);
967 }
968 },
969 ]);
970  
971 $nb-> insert_to_page( 0,
972 [ Label =>
973 origin => [ 230, 94],
974 size => [ 135, 20],
975 text => 'Status line delay (sec)',
976 ], [ SpinEdit =>
977 origin => [ 230, 72],
978 size => [ 135, 20],
979 min => 1,
980 max => 60,
981 step => 1,
982 name => 'StatusDelay',
983 ]);
984  
985 # Colors and appearance
986 $nb-> insert_to_page( 1, CheckBox =>
987 origin => [ 10, 60],
988 text => 'Show ~hints',
989 name => 'ShowHint',
990 hint => 'Enables these little pop-ups like the one you are looking at right now',
991 );
992  
993 my $optColors = $w-> opt_colors;
994 my @colorNames = map { $optColors->{$_}->[1] } sort keys %$optColors;
995 my @colorKeys = map { "Color_$_" } sort keys %$optColors;
996 if ( scalar keys %$optColors) {
997 my $x1 = $nb-> insert_to_page( 1, ComboBox =>
998 origin => [ 10, 10],
999 size => [ 170, $nb-> font-> height + 2],
1000 style => cs::DropDownList,
1001 name => 'NameSel',
1002 items => \@colorNames,
1003 onChange => sub {
1004 my $colors = $dlg-> {page2}-> {colors};
1005 $nbpages-> {deprecate} = 1;
1006 $nbpages-> ColorSel-> value( $$colors[ $w->{nameSelFoc} = $_[0]-> focusedItem]);
1007 $nbpages-> {deprecate} = undef;
1008 },
1009 );
1010 $nb-> insert_to_page( 1, Label =>
1011 origin => [ 10, 12 + $x1-> height],
1012 size => [ 300, 28],
1013 text => 'Color setup',
1014 focusLink => $x1,
1015 );
1016 $nb-> insert_to_page( 1, ColorComboBox =>
1017 name => 'ColorSel',
1018 origin => [ 190, 10],
1019 size => [ 56, $nb-> font-> height + 2],
1020 onChange => sub {
1021 my $self = shift;
1022 if ( !$nbpages-> {deprecate} && $self->enabled) {
1023 my $colors = $dlg-> {page2}-> {colors};
1024 $$colors[ $nbpages-> NameSel-> focusedItem] = $self-> value;
1025 my $i;
1026 for ( $i = 0; $i < scalar @colorKeys; $i++) {
1027 $w->{ini}->{$colorKeys[$i]} = $colors->[$i];
1028 }
1029 $w-> opt_colormount;
1030 $w-> IV-> repaint;
1031 }
1032 },
1033 );
1034 }
1035  
1036 # Keys
1037 my $optKeys = $w-> opt_keys;
1038 my %o_items;
1039 for ( keys %$optKeys) {
1040 m/^([A-Z][a-z]*)/;
1041 push( @{$o_items{$1}}, [$_]);
1042 }
1043 $nb-> insert_to_page( 2, [ StringOutline =>
1044 origin => [ 10, 58],
1045 size => [ 200, 222],
1046 name => 'KeyList',
1047 items => [ map {[ $_, $o_items{$_}]} sort keys %o_items ],
1048 onSelectItem => sub {
1049 my ( $me, $foc) = @_;
1050 my ( $item, $lev) = $_[0]-> get_item( $foc);
1051 return unless $item;
1052 $w-> {keyMappings_change} = 1;
1053 unless ( ref($item->[1])) {
1054 my $key = $_[0]-> get_item_text( $item);
1055 my $x = $w-> {keyMappings}-> {"Key_$key"};
1056 $nbpages-> KeySelector-> enabled(1);
1057 $nbpages-> KeySelector-> key( $w-> {keyMappings}-> {"Key_$key"} );
1058 $nbpages-> KeyDescription-> text( $optKeys-> {$key}-> [1] );
1059 $nbpages-> KeySelector-> show;
1060 } else {
1061 $nbpages-> KeySelector-> hide;
1062 $nbpages-> KeySelector-> enabled(0);
1063 $nbpages-> KeyDescription-> text( '');
1064 }
1065 delete $w-> {keyMappings_change};
1066 },
1067 ], [ KeySelector =>
1068 origin => [ 220, 110],
1069 size => [ 150, 170],
1070 name => 'KeySelector',
1071 visible => 0,
1072 onChange => sub {
1073 return if $w-> {keyMappings_change};
1074 my $kl = $nbpages-> KeyList;
1075 my ( $item, $lev) = $kl-> get_item( $kl-> focusedItem);
1076 return unless $item;
1077 my $okey = $kl-> get_item_text( $item);
1078 my $key = "Key_$okey";
1079 my $value = $_[0]-> key;
1080 if ( $value != kb::NoKey) {
1081 for ( keys %{$w-> {keyMappings}}) {
1082 next if $_ eq $key;
1083 next unless $value == $w-> {keyMappings}->{$_};
1084 s/^Key_//;
1085 $w-> {keyMappings_change} = 1;
1086 $_[0]-> key( $w-> {keyMappings}->{$key});
1087 delete $w-> {keyMappings_change};
1088 my $l = $nbpages-> KeyDescription;
1089 $l-> backColor( cl::LightRed);
1090 $l-> color( cl::Yellow);
1091 $l-> text( "This key combination is already occupied by $_ and cannot be used");
1092 $l-> insert( Timer => timeout => 100 => onTick => sub {
1093 $l-> backColor( cl::Back);
1094 $l-> color( cl::Fore);
1095 $_[0]-> destroy;
1096 })-> start;
1097 return;
1098 }
1099 }
1100 $w-> {keyMappings}->{$key} = $value;
1101 $nbpages-> KeyDescription-> text( $optKeys-> {$okey}-> [1] );
1102 },
1103 ], [ Label =>
1104 origin => [ 220, 10],
1105 size => [ 150, 100],
1106 autoWidth => 0,
1107 autoHeight => 0,
1108 text => '',
1109 name => 'KeyDescription',
1110 wordWrap => 1,
1111 ], [ Button =>
1112 origin => [ 10, 10],
1113 size => [96, 36],
1114 text => '~Clear',
1115 hint => 'Clears the key',
1116 onClick => sub {
1117 $nbpages-> KeySelector-> key( kb::NoKey);
1118 },
1119 ] , [ Button =>
1120 origin => [ 114, 10],
1121 size => [96, 36],
1122 text => '~Default',
1123 hint => 'Set default value for a key',
1124 onClick => sub {
1125 my $kl = $nbpages-> KeyList;
1126 my ( $item, $lev) = $kl-> get_item( $kl-> focusedItem);
1127 return unless $item;
1128 $nbpages-> KeySelector-> key( Prima::AbstractMenu-> translate_shortcut(
1129 $w-> opt_keys()-> {$kl-> get_item_text( $item)}-> [0]));
1130 },
1131 ] );
1132 $nbpages-> KeyList-> focusedItem(0);
1133 }
1134  
1135 sub opt_proppush
1136 {
1137 my ( $w, $dlg, $nb, $nbpages) = @_;
1138 # General
1139 $nbpages->RG_SeriesType-> RG_Short-> checked( $w-> {cypherMask} == 2);
1140 $nbpages->RG_SeriesType-> RG_Long -> checked( $w-> {cypherMask} == 3);
1141 $dlg->{page0}->{extSaveDir} = $w->{ini}->{extSaveDir};
1142 $nbpages-> UseDef-> checked( length($w->{ini}->{extSaveDir}) ? 0 : 1);
1143 $nbpages-> Path-> enabled(( length( $w->{ini}->{extSaveDir}) > 0) ? 1 : 0);
1144 $nbpages-> Path-> text( length($w->{ini}->{extSaveDir}) ? $w->{ini}->{extSaveDir} : '.');
1145 $nbpages-> StatusDelay-> value( $w->{ini}->{statusDelay});
1146 # Colors
1147 $nbpages-> ShowHint-> checked( $w-> {ini}-> {showHint});
1148 my $optColors = $w-> opt_colors;
1149 my @colors = map { $w-> {ini}->{"Color_$_"}} sort keys %$optColors;
1150 $dlg-> {page2}-> {csave} = { map { ( "Color_$_", $w-> {ini}->{"Color_$_"}) } keys %$optColors};
1151 $dlg-> {page2}-> {colors} = [ @colors ];
1152 if ( scalar @colors) {
1153 $nbpages-> NameSel-> focusedItem( defined $w->{nameSelFoc} ? $w->{nameSelFoc} : 0);
1154 $nbpages-> ColorSel-> value( $colors[ $nbpages-> NameSel-> focusedItem]);
1155 }
1156 # Keys
1157 $w-> {keyMappings_save} = { %{ $w-> {keyMappings}} };
1158 }
1159  
1160 sub opt_propvalid
1161 {
1162 my ( $w, $dlg, $nb, $nbpages) = @_;
1163 # General
1164 unless ( $nbpages-> UseDef-> checked) {
1165 unless ( -d $nbpages-> Path-> text) {
1166 $nb-> pageIndex(0);
1167 return 0 if Prima::MsgBox::message_box( "External save path",
1168 "Unexistent directory: ".$nbpages-> Path-> text.
1169 "\nDo you want to use it anyway?",
1170 mb::YesNo|mb::Warning) != mb::Yes;
1171 }
1172 }
1173  
1174 return 1;
1175 }
1176  
1177 sub opt_proppop
1178 {
1179 my ( $w, $dlg, $nb, $nbpages, $mr) = @_;
1180  
1181 if ( $mr) {
1182 # General
1183 my $x = $nbpages-> Path-> text;
1184 $x =~ s/^\s+//;
1185 $x =~ s/\s+$//;
1186 if ( $x eq '.' || $x eq '..' || length( $x) == 0) {
1187 $nbpages-> UseDef-> check;
1188 }
1189 $w->{ini}->{extSaveDir} = $nbpages-> UseDef-> checked ?
1190 '' : $nbpages-> Path-> text;
1191 $w->{ini}->{silentExtDir} = 0 unless length $w->{ini}->{extSaveDir};
1192 $w-> win_extpathchanged if $w->{ini}->{extSaveDir} ne $dlg->{page0}->{extSaveDir};
1193  
1194 my $newmask = $nbpages-> RG_SeriesType-> RG_Long-> checked ? 3 : 2;
1195 if ( $newmask != $w-> {cypherMask}) {
1196 $w-> {cypherMask} = $newmask;
1197 $w-> win_closeextras;
1198 $w-> win_newextras;
1199 $w-> win_extraschanged;
1200 }
1201 $w->{ini}->{statusDelay} = $nbpages-> StatusDelay-> value;
1202 # Hints
1203 $::application-> showHint( $w-> {ini}-> {showHint} = $nbpages-> ShowHint-> checked);
1204 # Keys
1205 $w-> winmenu_updatemappings;
1206 } else {
1207 # Colors
1208 if ( $dlg-> {page2}-> {csave}) {
1209 $w-> {ini}-> {$_} = $dlg->{page2}->{csave}-> {$_} for keys %{$dlg->{page2}->{csave}};
1210 $w-> opt_colormount;
1211 $w-> IV-> repaint;
1212 }
1213 # Keys
1214 $w-> {keyMappings} = $w-> {keyMappings_save};
1215 $nbpages-> KeyList-> notify(q(SelectItem), $nbpages-> KeyList-> focusedItem);
1216 }
1217 delete $w-> {keyMappings_save};
1218 }
1219  
1220 sub opt_properties
1221 {
1222 my $w = $_[0];
1223 unless ( $w-> {propertySheet}) {
1224 my $dlg = Prima::Window-> create(
1225 size => [ 420, 460],
1226 text => 'Properties',
1227 owner => $w,
1228 ownerShowHint => 0,
1229 # font => $::application-> get_message_font,
1230 %dlgProfile,
1231 );
1232 my $nb;
1233  
1234 $w-> dlg_okcancel( $dlg);
1235 $dlg-> OK-> set(
1236 modalResult => 0,
1237 onClick => sub {
1238 $dlg-> ok if $w-> opt_propvalid( $dlg, $nb, $nb-> Notebook);
1239 },
1240 );
1241 $nb = $dlg-> insert( TabbedNotebook =>
1242 origin => [ 5, 66],
1243 size => [ 410, 392],
1244 growMode => gm::Client,
1245 pageCount => 2,
1246 tabs => ['General', 'Appearance', 'Keys'],
1247 name => 'Notebook',
1248 );
1249 $w-> opt_propcreate( $dlg, $nb, $nb-> Notebook);
1250 $w-> {propertySheet} = $dlg;
1251 }
1252 my $dlg = $w-> {propertySheet};
1253 my $nb = $dlg-> Notebook;
1254 my $nbpages = $nb-> Notebook;
1255  
1256 $w-> opt_proppush( $dlg, $nb, $nbpages);
1257 $w-> iv_cancelmode( $w-> IV);
1258 $w-> opt_proppop( $dlg, $nb, $nbpages, $dlg-> execute == mb::OK);
1259 }
1260  
1261 # OPT_END
1262 # IV
1263  
1264  
1265 sub IV_MouseDown
1266 {
1267 my ( $w, $self, $btn, $mod, $x, $y) = @_;
1268  
1269 {
1270 my @r = $self-> get_active_area;
1271 $self-> clear_event, return if $x < $r[0] or $x >= $r[2] or $y < $r[1] or $y >= $r[3];
1272 }
1273  
1274 $self-> clear_event, return if !$ImageApp::testing and !defined $w-> IV-> image;
1275  
1276 my $ms = $self-> get_mouse_state;
1277 my $lr = mb::Left | mb::Right;
1278 if ((( $ms & $lr) == $lr) or ( $btn == mb::Middle)) {
1279 defined $w-> {magnify} ? $w-> iv_cancelmagnify( $self) : $w-> iv_magnify( $self),
1280 return;
1281 }
1282  
1283 if (( $btn == mb::Right) and ( !defined $self->{transaction})) {
1284 $self-> {dragData} = [ $x, $y, $self-> deltas];
1285 $w-> iv_entermode( $self, 3);
1286 $self-> {savePointer} = $self->pointer;
1287 $self-> pointer( $ico);
1288 $self-> clear_event;
1289 return;
1290 }
1291 }
1292  
1293  
1294 sub IV_MouseUp
1295 {
1296 my ( $w, $self, $btn, $mod, $x, $y) = @_;
1297 return unless $self->{transaction};
1298 if ( $btn == mb::Right and $self-> {transaction} == 3) {
1299 $self-> pointer( $self-> {savePointer});
1300 $self-> {savePointer} = undef;
1301 $self-> capture(0);
1302 $self-> {transaction} = undef;
1303 $self-> clear_event;
1304 }
1305 }
1306  
1307 sub IV_MouseMove
1308 {
1309 my ( $w, $self, $mod, $x, $y) = @_;
1310 if ( $w-> {magnify} && !$w-> {magnifyLock}) {
1311 $w-> {magnifyLock} = 1;
1312 $w-> ivm_repaint( $self, $x, $y);
1313 $w-> {magnify}-> origin( $x - 77, $y - 51);
1314 $w-> {magnify}-> update_view;
1315 $self-> update_view;
1316 $w-> {magnifyLock} = 0;
1317 }
1318
1319 return unless $self->{transaction};
1320 if ( $self-> {transaction} == 3) {
1321 my @dd = @{$self->{dragData}};
1322 my ($dx,$dy) = ($x - $dd[0], $y - $dd[1]);
1323 $self-> deltas( $dd[2] - $dx, $dd[3] + $dy);
1324 $self-> clear_event;
1325 }
1326 }
1327  
1328 sub iv_entermode
1329 {
1330 my ( $w, $self, $mode) = @_;
1331 # $w-> iv_cancelmode( $self);
1332 if ( $self->{transaction}) {
1333 $self-> {transaction} = undef;
1334 $self-> capture(0);
1335 $self-> repaint;
1336 $self-> pointer( $self-> {savePointer}) if $self-> {savePointer};
1337 $self-> {savePointer} = undef;
1338 }
1339 $self-> {transaction} = $mode;
1340 $self-> capture(1);
1341 }
1342  
1343 sub iv_cancelmode
1344 {
1345 my ( $w, $self) = @_;
1346 $w-> iv_cancelmagnify( $self) if $w-> {magnify};
1347 return unless $self->{transaction};
1348 $self-> {transaction} = undef;
1349 $self-> capture(0);
1350 $self-> repaint;
1351 $self-> pointer( $self-> {savePointer}) if $self-> {savePointer};
1352 $self-> {savePointer} = undef;
1353 $w-> sb_text("Action cancelled");
1354 }
1355  
1356  
1357 sub iv_zbestfit
1358 {
1359 my ( $w, $self) = @_;
1360 return unless $self->image;
1361 my @szA = $self->image->size;
1362 my @szB = $self->get_active_area(2);
1363 my $x = $szB[0]/$szA[0];
1364 my $y = $szB[1]/$szA[1];
1365 $self-> zoom( $x < $y ? $x : $y);
1366 $w-> sb_text("Zoom: ".$self->zoom);
1367 }
1368  
1369 sub IV_Size
1370 {
1371 my ( $w, $self) = @_;
1372 $w-> iv_zbestfit( $self) if $self->{autoBestFit};
1373 }
1374  
1375 sub ivm_repaint
1376 {
1377 my ( $w, $self) = @_;
1378 return unless $w-> {magnify};
1379 my $j = $w-> {magnify}-> {canvas};
1380 $j-> {override} = [ 0, 0];
1381 $j-> translate( 0, 0);
1382 $j-> clear;
1383 my @o = $self-> pointerPos;
1384 $j-> {override} = [ -$o[0] + 38, -$o[1] + 25];
1385 $j-> translate( 0, 0);
1386 $w-> IV_Paint( $self, $j);
1387 $j-> {override} = [ 0, 0];
1388 $j-> translate( 0, 0);
1389 $w-> {magnify}-> repaint;
1390 }
1391  
1392 sub iv_magnify
1393 {
1394 my ( $w, $self) = @_;
1395 return if $w-> {magnify};
1396 my $x = $self-> insert( 'App::PLab::MagnifyingGlassWidget' );
1397 $x-> focus;
1398 $w-> IV-> capture(1);
1399 $w-> {magnify} = $x;
1400 $w-> ivm_repaint( $self, $self-> pointerPos);
1401 }
1402  
1403 sub iv_cancelmagnify
1404 {
1405 my ( $w, $self) = @_;
1406 return unless $w->{magnify};
1407 $w->{magnify}-> destroy;
1408 $w->{magnify} = undef;
1409 $w-> IV-> capture(0);
1410 }
1411  
1412 # IV_END
1413 # SB
1414  
1415 sub sb_text
1416 {
1417 my ( $w, $text, $color) = @_;
1418 my $self = $w-> StatusBar;
1419 $self-> color( defined $color ? cl::LightRed : cl::Fore);
1420 $self-> set(
1421 text => $text,
1422 raise => 1,
1423 );
1424 $self-> update_view;
1425 $self-> { timer} = $self-> insert( Timer =>
1426 onTick => sub {
1427 $_[0]-> stop;
1428 $_[0]-> owner-> set(
1429 text => '',
1430 raise => 0,
1431 );
1432 },
1433 ) unless $self-> { timer};
1434 $self-> { timer}-> stop;
1435 $self-> { timer}-> timeout( $w-> {ini}-> {statusDelay} * 1000);
1436 $self-> { timer}-> start;
1437 }
1438  
1439 # SB_END
1440  
1441 sub profile_default
1442 {
1443 my $def = $_[ 0]-> SUPER::profile_default;
1444 my %prf = (
1445 name => 'MainWindow',
1446 size => [ 531, 594],
1447 text => $::application-> name,
1448 widgetClass=> wc::Dialog,
1449 );
1450 @$def{keys %prf} = values %prf;
1451 return $def;
1452 }
1453  
1454 sub init
1455 {
1456 my $self = shift;
1457 my %profile = $self-> SUPER::init(@_);
1458  
1459 my $w = $self;
1460 my ($x, $y) = $w-> size;
1461  
1462 my $scale = $::application-> uiScaling;
1463 $scale = 1 if $scale < 1;
1464  
1465 $w-> insert( "Panel",
1466 text => "",
1467 name => "ToolBar",
1468 origin => [ 2, $y - 2 - 38 * $scale],
1469 size => [ $x - 4, 38 * $scale],
1470 raise => 1,
1471 image => App::PLab::ImageAppGlyphs::image( bga::tile_m),
1472 growMode => gm::GrowHiX | gm::GrowLoY,
1473 );
1474  
1475 my %btn_profile = (
1476 glyphs => 2,
1477 text => "",
1478 selectable => 0,
1479 transparent => 1,
1480 flat => 1,
1481 size => [ map { $_ * $scale } 36, 36],
1482 borderWidth => 1,
1483 );
1484  
1485 $w-> ToolBar-> insert(
1486 [ SpeedButton =>
1487 origin => [1, 1],
1488 image => App::PLab::ButtonGlyphs::icon( bg::fldropen),
1489 hint => 'Open file',
1490 onClick => sub { $w-> win_openfile; },
1491 %btn_profile,
1492 ],
1493 [ SpeedButton =>
1494 name => "PrevImage",
1495 origin => [42 * $scale, 1],
1496 image => App::PLab::ImageAppGlyphs::icon( bga::prev),
1497 enabled => 0,
1498 hint => 'Previous image',
1499 onClick => sub { $w-> win_prevfile; },
1500 %btn_profile,
1501 ],
1502 [ SpeedButton =>
1503 name => "NextImage",
1504 origin => [78 * $scale, 1],
1505 image => App::PLab::ImageAppGlyphs::icon( bga::next),
1506 enabled => 0,
1507 hint => 'Next image',
1508 onClick => sub { $w-> win_nextfile; },
1509 %btn_profile,
1510 ],
1511 );
1512  
1513 $w-> insert( "Panel",
1514 text => "",
1515 name => "StatusBar",
1516 rect => [0, 0, $w-> width, 22],
1517 indent => 2,
1518 raise => 0,
1519 buffered => 1,
1520 growMode => gm::GrowHiX,
1521 font => { name => "System VIO", height => 18,},
1522 );
1523  
1524  
1525 $w-> insert( 'App::PLab::TrImageViewer' =>
1526 name => "IV",
1527 rect => [ 2, $w-> StatusBar-> height + 2, $x - 2, $y - $w-> ToolBar-> height - 8],
1528 hScroll => 1,
1529 vScroll => 1,
1530 growMode => gm::Client,
1531 delegations => [qw(Size MouseUp MouseDown MouseMove Paint)],
1532 widgetClass => wc::Window,
1533 );
1534 return %profile;
1535 }
1536  
1537 1;
1538  
1539 =pod
1540  
1541 =head1 NAME
1542  
1543 App::PLab::ImageApp - ancient stuff, please move along, nothing to see here
1544  
1545 =cut