Source

App::Harmonograph / lib / Wx / Perl / DrawMap.pm

Full commit

use v5.12;
use warnings;
use Wx;

package Wx::Perl::DrawMap;
use base qw(Wx::StaticBitmap);
use Scalar::Util qw(blessed looks_like_number);

sub new {
	my ($class, $parent, $x, $y) = @_;
	$y = $x unless defined $y;

	my ($self) = $class->SUPER::new($parent, -1, &Wx::wxNullBitmap);
	$self->SetMinSize([$x, $y]);
	$self->{'bitmap'} = Wx::Bitmap->new( $x, $y);
	$self->{'dc'}  = Wx::MemoryDC->new();
	$self->{'dc'}->SelectObject( $self->{'bitmap'} );
	$self->{'colour'} = Wx::Colour->new(0,0,0,0);
	$self->{'pen'} = Wx::Pen->new($self->{'colour'}, 2, &Wx::wxSOLID);
	$self;
}

sub NewDrawing { $_[0]->{'dc'}->Clear(); $_[0] }
sub PublishDrawing {
	my ($self) = shift;
	$self->SetBitmap( $self->{'bitmap'} );
	$self->{'dc'}->SelectObject( $self->{'bitmap'} );
	$self->Refresh();
	$self;
}

sub Colour {
	my ($self, $r, $g, $b) = @_;
	$self->{'colour'}->Set( $r, $g, $b, 0 );
	$self->{'pen'}->SetColour( $self->{'colour'} );
	$self->{'dc'}->SetPen( $self->{'pen'} );
	$self;
}

sub Point  {$_[0]->{'dc'}->DrawPoint( $_[1], $_[2] ); $_[0] }

sub SaveAsFile {
	my ($self, $file, $format) = @_;
	return warn ((caller(0))[3].': need a file name as first parameter') unless defined $file;
	if (not defined $format)           { $format = &Wx::wxBITMAP_TYPE_PNG }
	elsif (looks_like_number($format) ){
		return warn ((caller(0))[3].": unknown file format constant $format")
			unless $format == &Wx::wxBITMAP_TYPE_PNG
				or $format == &Wx::wxBITMAP_TYPE_JPEG
				or $format == &Wx::wxBITMAP_TYPE_TIFF
				or $format == &Wx::wxBITMAP_TYPE_BMP
				or $format == &Wx::wxBITMAP_TYPE_XPM;
	} else {
		$format = lc $format;
		if    ($format eq 'png') {$format = &Wx::wxBITMAP_TYPE_PNG}
		elsif ($format eq 'jpg') {$format = &Wx::wxBITMAP_TYPE_JPEG}
		elsif ($format eq 'jpeg'){$format = &Wx::wxBITMAP_TYPE_JPEG}
		elsif ($format eq 'tif') {$format = &Wx::wxBITMAP_TYPE_TIFF}
		elsif ($format eq 'tiff'){$format = &Wx::wxBITMAP_TYPE_TIFF}
		elsif ($format eq 'bmp') {$format = &Wx::wxBITMAP_TYPE_BMP}
		elsif ($format eq 'xpm') {$format = &Wx::wxBITMAP_TYPE_XPM}
		else {return warn ((caller(0))[3].": unknown file format: $format")}
	}
	# in later versions use: ->ConvertToImage() 
	Wx::Image->new( $self->GetBitmap() )->SaveFile($file, $format);  
	$self;
}
1;