home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _dc31ffa23fe41fa38c12420ad092c366 < prev    next >
Encoding:
Text File  |  2004-06-01  |  5.4 KB  |  199 lines

  1.  
  2. package Plot;
  3.  
  4. # Class "Plot": constructor, methods, destructor, global class data,
  5. # etcetera.
  6. #
  7. # Because a Plot object is a composite widget all the Composite base
  8. # class methods and advertised widgets are available to you.
  9. #
  10. # Advertised Plot widgets:  canvas, entry, PostScript_button, view_button.
  11.  
  12. require 5.005_03;
  13.  
  14. use vars qw/$VERSION @ISA/;
  15. $VERSION = sprintf '4.%03d', q$Revision: #7 $ =~ /\D(\d+)\s*$/;
  16.  
  17. use Tk::Frame;
  18. use base  qw/Tk::Frame/;
  19. Construct Tk::Widget 'Plot';
  20. use strict;
  21.  
  22. sub Populate {
  23.  
  24.     # Plot composite widget constructor.
  25.  
  26.     my($cw, $args) = @_;
  27.  
  28.     $cw->SUPER::Populate($args);
  29.     my($tc, $ih, $ah) = (
  30.     delete $args->{-title_color},
  31.     delete $args->{-inactive_highlight},
  32.     delete $args->{-active_highlight},
  33.     );
  34.  
  35.     my %pinfo;            # plot information hash
  36.     $pinfo{'lastX'} = 0;
  37.     $pinfo{'lastY'} = 0;
  38.     $pinfo{'areaX2'} = -1;
  39.     $pinfo{'prcmd'} = 'lpr';
  40.  
  41.     my $plot_font = '-*-Helvetica-Medium-R-Normal--*-180-*-*-*-*-*-*';
  42.  
  43.     my $c = $cw->Canvas(
  44.         -relief => 'raised',
  45.         -width  => '450',
  46.         -height => '300',
  47.         -cursor => 'top_left_arrow',
  48.     );
  49.     $cw->Advertise('canvas' => $c);
  50.     $c->pack(-side => 'top', -fill => 'x');
  51.  
  52.     $c->createLine(100, 250, 400, 250, -width => 2);
  53.     $c->createLine(100, 250, 100, 50, -width => 2);
  54.     $c->createText(225, 20, -text => 'A Simple Plot', -font => $plot_font,
  55.            -fill => $tc);
  56.  
  57.     my($i, $x, $y, $point, $item);
  58.     for($i = 0; $i <= 10; $i++) {
  59.     $x = 100 + ($i * 30);
  60.     $c->createLine($x, 250, $x, 245, -width => 2);
  61.     $c->createText($x, 254, -text => 10 * $i, -anchor => 'n',
  62.            -font => $plot_font);
  63.     } # forend
  64.     for ($i = 0; $i <= 5; $i++) {
  65.     $y =  250 - ($i * 40);
  66.     $c->createLine(100, $y, 105, $y, -width => 2);
  67.     $c->createText(96, $y, -text => $i * 50.0, -anchor => 'e',
  68.            -font => $plot_font);
  69.     } # forend
  70.  
  71.     foreach $point ([12, 56], [20, 94], [33, 98], [32, 120], [61, 180],
  72.             [75, 160], [98, 223]) {
  73.     $x = 100 + (3 * ${$point}[0]);
  74.         $y = 250 - (4 * ${$point}[1]) / 5;
  75.         $item = $c->createOval($x-6, $y-6, $x+6, $y+6, -width => 1,
  76.                -outline => 'black', -fill => $ih);
  77.         $c->addtag('point', 'withtag', $item);
  78.     }
  79.  
  80.     $c->bind('point', '<Any-Enter>' => [sub{shift->itemconfigure(@_)},
  81.                     'current', -fill => $ah]);
  82.     $c->bind('point', '<Any-Leave>' => [sub{shift->itemconfigure(@_)},
  83.                     'current', -fill => $ih]);
  84.     $c->bind('point', '<1>' => [sub {plot_down(@_)}, \%pinfo]);
  85.     $c->bind('point', '<ButtonRelease-1>' => sub {shift->dtag('selected')});
  86.     $c->CanvasBind('<B1-Motion>' => [sub {plot_move(@_)}, \%pinfo]);
  87.     $c->CanvasBind('<2>' => [sub {area_down(@_)}, \%pinfo]);
  88.     $c->CanvasBind('<B2-Motion>' => [sub {area_move(@_)}, \%pinfo]);
  89.  
  90.     my $w_prcmd = $cw->Entry(
  91.         -textvariable => \$pinfo{'prcmd'},
  92.     );
  93.     $cw->Advertise('entry' => $w_prcmd);
  94.     $w_prcmd->pack;
  95.  
  96.     my $w_print = $cw->Button(
  97.         -text         => 'Print in PostScript Format',
  98.         -command      => [\&area_save, $c, \%pinfo],
  99.     );
  100.     $cw->Advertise('PostScript_button' => $w_print);
  101.     $w_print->pack;
  102.     $w_prcmd->bind('<Return>' => [$w_print => 'invoke']);
  103.  
  104.     my $w_view = $cw->Button(
  105.         -text    => 'View Composite Plot Widget',
  106.         -command => [\&::view_widget,
  107.                  Tk->findINC('demos/widget_lib/Plot.pm'),
  108.                     ],
  109.     );
  110.     $cw->Advertise('view_button' => $w_view);
  111.     $w_view->pack;
  112.  
  113.     return $cw;
  114.  
  115. } # end Populate, Plot constructor
  116.  
  117. # Private methods.
  118.  
  119. sub area_down {
  120.  
  121.     my($w, $pinfo) = @_;
  122.  
  123.     my $e = $w->XEvent;
  124.     my($x, $y) = ($e->x, $e->y);
  125.     $pinfo->{'areaX1'} = $x;
  126.     $pinfo->{'areaY1'} = $y;
  127.     $pinfo->{'areaX2'} = -1;
  128.     $pinfo->{'areaY2'} = -1;
  129.     eval {local $SIG{'__DIE__'}; $w->delete('area');};
  130.  
  131. } # end area_down
  132.  
  133. sub area_move {
  134.  
  135.     my($w, $pinfo) = @_;
  136.  
  137.     my $e = $w->XEvent;
  138.     my($x, $y) = ($e->x, $e->y);
  139.     if($x != $pinfo->{'areaX1'} && $y != $pinfo->{'areaY1'}) {
  140.       eval {local $SIG{'__DIE__'}; $w->delete('area');};
  141.       $w->addtag('area','withtag',$w->createRectangle($pinfo->{'areaX1'},
  142.                                            $pinfo->{'areaY1'},$x,$y));
  143.       $pinfo->{'areaX2'} = $x;
  144.       $pinfo->{'areaY2'} = $y;
  145.     }
  146. } # end area_move
  147.  
  148. sub area_save {
  149.  
  150.     my($w, $pinfo) = @_;
  151.  
  152.     my($x1, $x2, $y1, $y2, $a);
  153.  
  154.     if($pinfo->{'areaX2'} != -1) {
  155.     ($x1, $x2, $y1, $y2) =
  156.       @$pinfo{'areaX1', 'areaX2', 'areaY1', 'areaY2'}; # slice !
  157.     ($x1, $x2) = @$pinfo{'areaX2', 'areaX1'} if $x2 <= $x1;
  158.     ($y1, $y2) = @$pinfo{'areaY2', 'areaY1'} if $y2 <= $y1;
  159.     $a = $w->postscript('-x' => $x1, '-y' => $y1,
  160.                 -width => $x2 - $x1, -height => $y2 - $y1);
  161.     } else {
  162.     $a = $w->postscript;
  163.     }
  164.  
  165.     $SIG{'PIPE'} = sub {};
  166.     open(LPR, "| $pinfo->{'prcmd'}");
  167.     print LPR $a;
  168.     close(LPR);
  169.  
  170. } # end area_save
  171.  
  172. sub plot_down {
  173.  
  174.     my($w, $pinfo) = @_;
  175.  
  176.     my $e = $w->XEvent;
  177.     my($x, $y) = ($e->x, $e->y);
  178.     $w->dtag('selected');
  179.     $w->addtag('selected', 'withtag', 'current');
  180.     $w->raise('current');
  181.     $pinfo->{'lastX'} = $x;
  182.     $pinfo->{'lastY'} = $y;
  183.  
  184. } # end plot_down
  185.  
  186. sub plot_move {
  187.  
  188.     my($w, $pinfo) = @_;
  189.  
  190.     my $e = $w->XEvent;
  191.     my($x, $y) = ($e->x, $e->y);
  192.     $w->move('selected',  $x-$pinfo->{'lastX'}, $y-$pinfo->{'lastY'});
  193.     $pinfo->{'lastX'} = $x;
  194.     $pinfo->{'lastY'} = $y;
  195.  
  196. } # end plot_move
  197.  
  198. 1;
  199.