HP.pm untested

filter: Statementstmt 0.0% 0 / 436 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 48 Podpod 0.0% 0 / 34 totaltotal 0.0% 0 / 616 CC 41 SCAR 26.3
CC
41
Cov
0%
key
42.0
math3
42.0
Populate
30.0
CRAP 13.8
1 $Tk::Calculator::RPN::HP::VERSION = '1.2';
2  
3 package Tk::Calculator::RPN::HP;
4  
5 # OO base class and Exporter module for HP RPN calculators.
6  
7 use Exporter;
8 use base qw/Exporter/;
9 push @EXPORT, qw/
10 $BLUE $BLUE_DARKER
11 $GRAY $GRAY_LIGHTER $GRAY_LIGHTEST
12 $ORANGE $PI $ROLD $ROLU $SILVER $STACKM $SWAP
13 /;
14 push @EXPORT, qw/$AC $AS $AT $CO $EX $LG $LN $RP $SI $SQ $TA $TX/;
15 push @EXPORT, qw/$AD $AN $DD $DM $DV $IO $ML $SB $XR $YX/;
16  
17 use POSIX;
18 use Tk::widgets qw /Compound/;
19 use base qw/Tk::Frame/;
20 Construct Tk::Widget 'Calculator';
21 use subs qw/mode/;
22 use strict;
23  
24 use constant E => 2.7182818;# e
25 use constant PI => 3.1415926;# pi
26 use constant D2R => PI / 180; # degrees per radian
27 our $ROOT = 'Calculator/RPN/images';
28  
29 # Exported global constants and mathematical functions.
30  
31 our $BLUE; # color
32 our $BLUE_DARKER; # color
33 our $GRAY; # color
34 our $GRAY_LIGHTER; # color
35 our $GRAY_LIGHTEST; # color
36 our $ORANGE; # color
37 our $PI; # image of pi
38 our $ROLD; # roll stack up image
39 our $ROLU; # roll stack down image
40 our $SILVER; # color
41 our $STACKM; # maximum stack length
42 our $SWAP; # swap X/Y image
43  
44 our $AC; # arccosine
45 our $AS; # arcsine
46 our $AT; # arctangent
47 our $CO; # cosine
48 our $EX; # e**x
49 our $LG; # log10
50 our $LN; # natural logarithm
51 our $RP; # reciprocal
52 our $SI; # sine
53 our $SQ; # square root
54 our $TA; # tangent
55 our $TX; # 10**x
56  
57 our $AD; # add
58 our $AN; # AND
59 our $DD; # double divide
60 our $DM; # double multiply
61 our $DV; # divide
62 our $IO; # inclusive OR
63 our $ML; # multiply
64 our $SB; # subtract
65 our $XR; # exclusive OR
66 our $YX; # exponentiation
67  
68 sub ClassInit {
69  
70 my ($class, $mw) = @_;
71  
72 # Exported global constants.
73  
74 $BLUE = 'steelblue1';
75 $BLUE_DARKER = 'steelblue';
76 $GRAY = 'gray25';
77 $GRAY_LIGHTER = 'gray31';
78 $GRAY_LIGHTEST = 'gray40';
79 $ORANGE = 'orange';
80 $SILVER = '#ef5bef5bef5b';
81 $STACKM = 4 - 1;
82  
83 # Exported mathematical functions expecting one operand,
84 # X from the RPN stack.
85  
86 $AC = sub {acos mode(@_)}; # arccosine
87 $AS = sub {asin mode(@_)}; # arcsine
88 $AT = sub {atan mode(@_)}; # arctangent
89 $CO = sub {sin mode(@_)}; # cosine
90 $EX = sub {E ** $_[0]}; # e**x
91 $LG = sub {log10 $_[0]}; # log10
92 $LN = sub {log $_[0]}; # natural logarithm
93 $RP = sub {1 / $_[0]}; # reciprocal
94 $SI = sub {sin mode(@_)}; # sine
95 $SQ = sub {sqrt $_[0]}; # square root
96 $TA = sub {tan mode(@_)}; # tangent
97 $TX = sub {10 ** $_[0]}; # 10**x
98  
99 # Exported mathematical functions expecting two operands,
100 # X and Y from the RPN stack.
101  
102 $AD = sub {$_[1] + $_[0]}; # addition
103 $AN = sub {$_[1] & $_[0]}; # AND
104 $DD = sub {$_[1] / $_[0]}; # double divide
105 $DM = sub {$_[1] * $_[0]}; # double multiply
106 $DV = sub {$_[1] / $_[0]}; # division
107 $IO = sub {$_[1] | $_[0]}; # inclusive OR
108 $ML = sub {$_[1] * $_[0]}; # multiplication
109 $SB = sub {$_[1] - $_[0]}; # subtraction
110 $XR = sub {$_[1] ^ $_[0]}; # exclusive OR
111 $YX = sub {$_[1] ** $_[0]}; # exponentiation
112  
113 # Exported images.
114  
115 my (@cargs) = (-foreground => $BLUE, -background => $GRAY_LIGHTER);
116  
117 $PI = $mw->Bitmap(-file => Tk->findINC("$ROOT/pi.xbm"), @cargs);
118  
119 $ROLU = $mw->Compound;
120 $ROLU->Text(-text => 'R', -foreground => $BLUE);
121 $ROLU->Image(-image => $mw->Bitmap(-file => Tk->findINC("$ROOT/rolu.xbm"), @cargs));
122  
123 @cargs = (-foreground => 'white', -background => $GRAY);
124  
125 $ROLD = $mw->Compound;
126 $ROLD->Text(-text => 'R', -foreground => 'white');
127 $ROLD->Image(-image => $mw->Bitmap(-file => Tk->findINC("$ROOT/rold.xbm"), @cargs));
128  
129 $SWAP = $mw->Compound;
130 $SWAP->Text(-text => 'X', -foreground => 'white');
131 $SWAP->Image(-image => $mw->Bitmap(-file => Tk->findINC("$ROOT/swap.xbm"), @cargs));
132 $SWAP->Text(-text => 'Y', -foreground => 'white');
133  
134 $class->SUPER::ClassInit($mw);
135  
136 } # end ClassInit
137  
138 sub Populate {
139  
140 # Populate() for this base class is rather tricky because it re-blesses
141 # the incoming object (whether this is good style is another question).
142 # It does this so that method lookups are dispatched to the new class
143 # based on the type of calculator. This also means that superclass
144 # lookups are found here, the HP RPN base class.
145 #
146 # This presents one problem - ClassInit() is never called for the new
147 # subclass. We handle this situation just like a C widget, and invoke
148 # InitClass on the re-blessed class, which eventually calls ClassInit.
149  
150 my ($self, $args) = @_;
151  
152 $self->SUPER::Populate($args);
153  
154 $self->{TYPE} = delete $args->{-type};
155 die "HP calculator type not specified." unless $self->{TYPE};
156  
157 mkdir $^O eq "MSWin32" ? 'C:\.hp' : "$ENV{HOME}/.hp", 0755;
158  
159 my $calc = 'Tk::Calculator::RPN::HP_' . uc($self->{TYPE});
160 eval "require $calc";
161 die "Tk::Calculator::RPN::HP::HP.pm, error loading '$calc': $@" if $@;
162 bless $self, $calc;
163  
164 $calc->InitClass($self->MainWindow); # called once/class/MainWindow
165  
166 # Instance pre-initialization.
167  
168 $self->{CLRX} = 0; # 1 IFF clear X before inserting next key
169 $self->{F_PRESSED} = 0; # F-key modifier pressed
170 $self->{G_PRESSED} = 0; # G-key modifier pressed
171 $self->{ONOFF} = 0; # 1 IFF calculator on
172 $self->{PUSHX} = 0; # 1 IFF push X before inserting next key
173 $self->{MODE} = 0; # 0 = degress, 1 = radians
174 $self->{PB} = delete $args->{-progressbar}; # possible ProgressBar
175 $self->{XV} = ' '; # current display (X) value
176 $self->clr; # clear entire stack
177  
178 # Build the calculator.
179  
180 $self->{PB_PERCENT} = 0;
181 $self->Populate($args);
182 $self->{PB}->set($self->{PB_PERCENT} = 100) if $self->{PB};
183  
184 } # end Populate
185  
186 sub build_help_button {
187  
188 # Create the ? Button common to all calculator types.
189  
190 my ($self, $parent, $help) = @_;
191  
192 my $quest = $parent->Button(
193 -text => '?',
194 -font => '6x9',
195 -relief => 'flat',
196 -highlightthickness => 0,
197 -background => $BLUE,
198 -borderwidth => 0,
199 -pady => 0,
200 -command => sub {$help->deiconify},
201 );
202 $quest->bind('<2>' => sub {
203 my (@register) = ('(X)', '(Y)', '(Z)', '(T)');
204 print "\n";
205 for (my $i = $STACKM; $i >= 0; $i--) {
206 print "stack+$i $register[$i] : '", $self->{STACK}[$i], "'\n";
207 }
208 });
209  
210 $self->{PB}->set($self->{PB_PERCENT} += 10) if $self->{PB};
211  
212 return $quest;
213  
214 } # end build_help_button
215  
216 sub build_help_window {
217  
218 # Called by ClassInit() to build a help window shared by all subclass instances.
219  
220 my ($class, $mw) = @_;
221  
222 my $help = $mw->Toplevel;
223 $help->withdraw;
224 $help->title('HP 21 Help');
225 $help->protocol('WM_DELETE_WINDOW' => sub {});
226  
227 my $frame = $help->Frame->pack;
228  
229 my ($type) = $class =~ /.*::(.*)/;
230 $frame->Label(
231 -image => $mw->Photo(-file => Tk->findINC("$ROOT/" . lc($type) . '-back.gif')),
232 )->pack;
233  
234 $frame->Label(
235 -text => '? <B1> displays this window',
236 -relief => 'ridge',
237 )->pack(qw/-expand 1 -fill both/);
238  
239 $frame->Label(
240 -text => '? <B2> prints the stack',
241 -relief => 'ridge',
242 )->pack(qw/-expand 1 -fill both/);
243 $frame->Button(
244 -text => 'Close',
245 -command => sub {$help->withdraw},
246 )->pack(qw/-expand 1 -fill both/);
247  
248 return $help;
249  
250 } # end build_help_window
251  
252 sub mode {
253  
254 # Convert an argument from degees to radians, if required.
255  
256 my ($f, $mode) = @_;
257  
258 return $f if $mode; # already in radians
259 return $f * D2R; # if degrees
260  
261 } # end mode
262  
263 sub set_keypad_bindings {
264  
265 # Now establish key bindings for the digits and common arithmetic
266 # operations, including keypad keys.
267  
268 my ($self) = @_;
269  
270 my $mw = $self->MainWindow;
271  
272 foreach my $key ( qw/0 1 2 3 4 5 6 7 8 9/ ) {
273 $mw->bind( "<Key-$key>" => [$self => 'key', $key] );
274 $mw->bind( "<KP_$key>" => [$self => 'key', $key] );
275 }
276  
277 foreach my $key ( qw/period KP_Decimal/ ) {
278 $mw->bind( "<$key>" => [$self => 'key', '.'] );
279 }
280  
281 foreach my $key ( qw/Return KP_Enter/ ) {
282 $mw->bind( "<$key>" => [$self => 'enter'] );
283 }
284  
285 foreach my $key ( qw/plus KP_Add/ ) {
286 $mw->bind( "<$key>" => [$self => 'math3', $AD, undef, undef] );
287 }
288  
289 foreach my $key ( qw/minus KP_Subtract/ ) {
290 $mw->bind( "<$key>" => [$self => 'math3', $SB, undef, undef] );
291 }
292  
293 foreach my $key ( qw/asterisk KP_Multiply/ ) {
294 $mw->bind( "<$key>" => [$self => 'math3', $ML, undef, undef] );
295 }
296  
297 foreach my $key ( qw/slash KP_Divide/ ) {
298 $mw->bind( "<$key>" => [$self => 'math3', $DV, undef, undef] );
299 }
300  
301 } # end set_keypad_bindings
302  
303 # Function key processors common to all classes.
304  
305 sub chs { # change sign
306  
307 my ($self) = @_;
308  
309 my $s = substr($self->{STACK}[0], 0, 1);
310 substr($self->{STACK}[0], 0, 1) = ($s eq '-') ? ' ' : '-';
311 $self->end;
312  
313 } # end chs
314  
315 sub clr { # clear stack
316  
317 my ($self) = @_;
318  
319 $self->{STACK}[$_] = ' 0.00' foreach (0 .. $STACKM);
320 $self->end;
321  
322 } # end clr
323  
324 sub clx { # clear x
325  
326 my ($self) = @_;
327  
328 $self->{STACK}[0] = 0;
329 $self->{CLRX} = 1;
330 $self->{PUSHX} = 0;
331 $self->end;
332  
333 } # end clx
334  
335 sub end { # key and display cleanup
336  
337 my ($self) = @_;
338  
339 $self->{F_PRESSED} = $self->{G_PRESSED} = 0;
340 $self->{XV} = $self->{STACK}[0];
341  
342 } # end end
343  
344 sub enter { # enter key
345  
346 my ($self) = @_;
347  
348 unshift @{$self->{STACK}}, $self->{STACK}[0];
349 $#{$self->{STACK}} = $STACKM if $#{$self->{STACK}} > $STACKM;
350 $self->{CLRX} = 1;
351 $self->{PUSHX} = 0;
352 $self->end;
353  
354 } # end enter
355
356 sub err { # error
357  
358 my ($self) = @_;
359  
360 $self->bell if $self->{ONOFF};
361  
362 } # end err
363  
364 sub f { # F key
365  
366 my ($self) = @_;
367  
368 $self->{F_PRESSED} = 1;
369  
370 } # end f
371  
372 sub g { # G key
373  
374 my ($self) = @_;
375  
376 $self->{G_PRESSED} = 1;
377  
378 } # end g
379  
380 sub hpshift { # empty HP stack
381  
382 my ($self) = @_;
383  
384 $#{$self->{STACK}} = $STACKM if $#{$self->{STACK}} > $STACKM;
385 my $v = shift @{$self->{STACK}};
386 $self->{STACK}[$STACKM] = $self->{STACK}[$STACKM - 1] if
387 $#{$self->{STACK}} == ($STACKM - 1);
388  
389 $self->end;
390 return $v;
391  
392 } # end hpshift
393  
394 sub key { # process generic key clicks
395  
396 my ($self) = @_;
397  
398 shift if ref $_[0]; # toss bind() object
399 my $key = $_[0];
400 return unless $self->{ONOFF};
401 if ($self->{F_PRESSED} or $self->{G_PRESSED}) {
402 $self->bell;
403 $self->end;
404 return;
405 }
406  
407 $self->enter if $self->{PUSHX};
408 $self->{STACK}[0] = ' ' if $self->{CLRX};
409  
410 $self->{STACK}[0] .= $key;
411 $self->{CLRX} = $self->{PUSHX} = 0;
412 $self->end;
413  
414 } # end key
415  
416 sub math { # non-G key arithmetic operations
417  
418 # math() expects one code reference to an anonymous subroutine, which
419 # expects one argument, X from the RPN stack.
420  
421 my $self = shift;
422  
423 $self->{STACK}[0] = &{$_[0]}($self->{STACK}[0]);
424 $self->{STACK}[0] = ' ' . $self->{STACK}[0] if
425 substr($self->{STACK}[0], 0, 1) ne '-';
426 $self->{CLRX} = $self->{PUSHX} = 1;
427 $self->end;
428  
429 } # end math
430  
431 sub math3 { # tri-arithmetic keys
432  
433 # math3() expects three code references to anonymous subroutines, each
434 # of which expects two arguments, X and Y from the RPN stack.
435 #
436 # $_[0] = normal button press
437 # $_[1] = "f" qualified button press
438 # $_[2] = "g" qualified button press
439  
440 my ($self) = @_;
441  
442 shift if ref $_[0]; # toss bind() object
443 my $math = $_[0];
444 $math = $_[1] if $self->{F_PRESSED};
445 $math = $_[2] if $self->{G_PRESSED};
446 if (not defined $math) {
447 $self->bell;
448 $self->end;
449 return;
450 }
451  
452 my $x = $self->hpshift;
453 my $y = $self->{STACK}[0];
454 $self->{STACK}[0] = &{$math}($x, $y);
455 $self->{STACK}[0] = ' ' . $self->{STACK}[0]
456 if substr($self->{STACK}[0], 0, 1) ne '-';
457 $self->{CLRX} = $self->{PUSHX} = 1;
458 $self->end;
459  
460 } # end math3
461  
462 sub on { # power on/off
463  
464 my ($self, $val) = @_;
465  
466 my $rc = $self->{RCFILE};
467 if ($self->{ONOFF}) {
468 $self->{ONOFF} = 0;
469 if (open(RC, ">$rc") or die"open write failed for '$rc': $!") {
470 foreach (reverse @{$self->{STACK}}) {
471 print RC "$_\n";
472 }
473 close RC;
474 }
475 $self->end;
476 $self->{XV} = '';
477 } else {
478 $self->{ONOFF} = 1;
479 if (open(RC, $rc)) {
480 $self->{STACK} = [] if -s $rc;
481 while ($_ = <RC>) {
482 chomp;
483 unshift @{$self->{STACK}}, $_;
484 }
485 close RC;
486 }
487 $self->{CLRX} = $self->{PUSHX} = 1;
488 $self->end;
489 }
490  
491 } # end on
492  
493 sub pi { # return pi
494  
495 my ($self) = @_;
496  
497 $self->enter;
498 $self->{STACK}[0] = PI;
499 $self->end;
500  
501 } # end pi
502  
503 sub roll_down { # roll stack down
504  
505 my ($self) = @_;
506  
507 return unless $self->{ONOFF};
508 push @{$self->{STACK}}, shift @{$self->{STACK}};
509 $self->end;
510  
511 } # end roll_down
512  
513 sub roll_up { # roll stack up
514  
515 my ($self) = @_;
516  
517 return unless $self->{ONOFF};
518 unshift @{$self->{STACK}}, pop @{$self->{STACK}};
519 $self->end;
520  
521 } # end roll_up
522  
523 sub swapxy { # swap x and y
524  
525 my ($self) = @_;
526  
527 return unless $self->{ONOFF};
528 if ($self->{F_PRESSED} or $self->{G_PRESSED}) {
529 $self->bell;
530 $self->end;
531 return;
532 }
533  
534 (@{$self->{STACK}}[0, 1]) = (@{$self->{STACK}}[1, 0]);
535 $self->end;
536  
537 } # end swapxy
538  
539 sub trig_math { # with degree to radian conversion
540  
541 # trig_math() expects one code reference to an anonymous subroutine, which
542 # expects one argument, X from the RPN stack. Convert degrees to radians
543 # as appropriate.
544  
545 my $self = shift;
546  
547 $self->{STACK}[0] = &{$_[0]}($self->{STACK}[0], $self->{MODE});
548 $self->{STACK}[0] = ' ' . $self->{STACK}[0] if
549 substr($self->{STACK}[0], 0, 1) ne '-';
550 $self->{CLRX} = $self->{PUSHX} = 1;
551 $self->end;
552  
553 } # end trig_math
554  
555 package Tk::Calculator::RPN::HP::Key3_16C;
556  
557 # Composite mega-widget Key3 - 3 operators per key.
558  
559 use Tk::widgets qw/Frame/;
560 use base qw/Tk::Frame/;
561 Construct Tk::Calculator::RPN::HP 'Key3_16C';
562  
563 sub Populate {
564  
565 my ($self, $args) = @_;
566  
567 my $topl = delete $args->{-topl};
568 my $butl = delete $args->{-butl};
569 my $botl = delete $args->{-botl};
570  
571 $self->SUPER::Populate($args);
572  
573 my (@pl) = qw/-side top -expand yes -fill both/;
574 $self->{topl} = $self->Label(-text => $topl)->pack(@pl);
575 $self->{topl}->configure( -image => $topl) if ref($topl);
576  
577 $self->{butl} = $self->Button(
578 -text => $butl,
579 -borderwidth => 2,
580 )->pack(@pl);
581 $self->{butl}->configure( -image => $butl) if ref($butl);
582  
583 $self->{botl} = $self->Label(-text => $botl)->pack(@pl);
584 $self->{botl}->configure( -image => $botl) if ref($botl);
585  
586 $self->pack(qw/-side left -expand 1 -fill both -padx 3 -pady 3/);
587  
588 $self->ConfigSpecs(
589 -background => [qw/METHOD background background yellow/],
590 -command => [$self->{butl}, qw/command Command/, undef],
591 -foreground => [qw/METHOD foreground Foreground red/],
592 -font => [qw/METHOD font Font fixed/],
593 -width => [qw/METHOD width Width 20/],
594 -height => [$self->{butl}, qw/height Height 0/],
595 );
596  
597 } # end Populate
598  
599 sub background {
600 my ($self, $bg) = @_;
601 $self->{topl}->configure(-background => $GRAY_LIGHTER);
602 $self->{butl}->configure(-background => $bg);
603 $self->{botl}->configure(-background => $GRAY_LIGHTER);
604 }
605  
606 sub font {
607 my ($self) = @_;
608 $self->{topl}->configure(-font => [qw/arial 9 bold/]);
609 $self->{butl}->configure(-font => [qw/arial 10 bold/]);
610 $self->{botl}->configure(-font => [qw/arial 9 bold/]);
611 }
612  
613 sub foreground {
614 my ($self) = @_;
615 $self->{topl}->configure(-foreground => $ORANGE);
616 my $text = $self->{butl}->cget(-text);
617 my $fg = ($text =~ /^[fg]{1}$/) ? 'black' : 'white';
618 $self->{butl}->configure(-foreground => $fg);
619 $self->{botl}->configure(-foreground => $BLUE);
620 }
621  
622 sub width {
623 my ($self) = @_;
624 $self->{topl}->configure(-width => 6);
625 $self->{butl}->configure(-width => 3);
626 $self->{botl}->configure(-width => 4);
627 }
628  
629 package Tk::Calculator::RPN::HP::Key2_21;
630  
631 # Composite mega-widget Key2 - 2 operators per key.
632  
633 use Tk::widgets qw/Frame/;
634 use base qw/Tk::Frame/;
635 Construct Tk::Calculator::RPN::HP 'Key2_21';
636  
637 sub Populate {
638  
639 my ($self, $args) = @_;
640  
641 my $butl = delete $args->{-butl};
642 my $botl = delete $args->{-botl};
643  
644 $self->SUPER::Populate($args);
645  
646 my (@pl) = qw/-side top -expand yes -fill both/;
647  
648 $self->{butl} = $self->Button(
649 -text => $butl,
650 -borderwidth => 2,
651 )->pack(@pl);
652 $self->{butl}->configure( -image => $butl) if ref($butl);
653  
654 $self->{botl} = $self->Label(-text => $botl)->pack(@pl);
655 $self->{botl}->configure( -image => $botl) if ref($botl);
656  
657 $self->pack(qw/-side left -expand 1 -fill both -padx 3 -pady 3/);
658  
659 $self->ConfigSpecs(
660 -background => [qw/METHOD background background yellow/],
661 -command => [$self->{butl}, qw/command Command/, undef],
662 -foreground => [qw/METHOD foreground Foreground red/],
663 -font => [qw/METHOD font Font fixed/],
664 -width => [qw/METHOD width Width 20/],
665 -height => [$self->{butl}, qw/height Height 0/],
666 );
667  
668 } # end Populate
669  
670 sub background {
671 my ($self, $bg) = @_;
672 my $text = $self->{butl}->cget(-text);
673 $bg = $BLUE if $text eq 'BLUE';
674 $self->{butl}->configure(-background => $bg);
675 $self->{botl}->configure(-background => $GRAY_LIGHTER);
676 }
677  
678 sub font {
679 my ($self) = @_;
680 $self->{butl}->configure(-font => [qw/arial 10 bold/]);
681 $self->{botl}->configure(-font => [qw/arial 9 bold/]);
682 }
683  
684 sub foreground {
685 my ($self) = @_;
686 my $text = $self->{butl}->cget(-text);
687 my $fg = ($text =~ /^[\.\-\+\/x0123456789]$/ or
688 $text eq 'DSP') ? 'black' : 'white';
689 $fg = $BLUE if $text eq 'BLUE';
690 $self->{butl}->configure(-foreground => $fg);
691 $self->{botl}->configure(-foreground => $BLUE);
692 }
693  
694 sub width {
695 my ($self) = @_;
696 $self->{butl}->configure(-width => 3);
697 $self->{botl}->configure(-width => 4);
698 }
699  
700 1;
701 __END__