perl wxImagePanelDemo.pl
perl wxImagePanelDemo.pl foo.gif
#!/usr/bin/perl --
## http://wiki.wxwidgets.org/An_image_panel
use strict;
use warnings;
Main( @ARGV );
exit( 0 );
sub Main {
Wx::InitAllImageHandlers();
my( $file ) = @_;
$file ||= Wx::Image->new( Wx::GetWxPerlIcon() )->Scale(240,240);
my $frame = Wx::Frame->new(
undef,
-1,
"Hello wxDC",
[ 50, 50 ],
[ 500, 500 ],
);
my $filepane = wIP->new( $frame, $file );
my $iconpane = wIP->new( $frame, Wx::Image->new( Wx::GetWxPerlIcon
+() ) );
my $sizer = Wx::BoxSizer->new( Wx::wxVERTICAL() );
my $icon_sizer = Wx::BoxSizer->new( Wx::wxHORIZONTAL() );
for( 1..3){
$icon_sizer->AddSpacer( 30 * $_ );
$icon_sizer->Add( Wx::Button->new( $iconpane, -1, "yo $_" ) );
}
$iconpane->SetSizer( $icon_sizer );
$sizer->Add( $filepane, 1, Wx::wxEXPAND() );
$sizer->Add( $iconpane, 1, Wx::wxEXPAND() );
my $pthree = wIP->new( $frame, Wx::Image->new( Wx::GetWxPerlIcon()
+ ) );
my $pthree_sizer = Wx::BoxSizer->new( Wx::wxHORIZONTAL() );
$pthree_sizer->Add(
wIP->new(
$pthree,
Wx::Image->new( Wx::GetWxPerlIcon() )
)->noscale,
);
my $pfunk = wIP->new( $pthree, Wx::Image->new( Wx::GetWxPerlIcon()
+ ) );
$pthree_sizer->Add( $pfunk, 1, Wx::wxEXPAND(), );
$pthree_sizer->Add(
wIP->new(
$pthree,
Wx::Image->new( Wx::GetWxPerlIcon() )
)->noscale,
);
$pthree->SetSizer( $pthree_sizer );
$pthree_sizer->Layout;
$pthree_sizer->Show(2);
$pthree_sizer->Show(3);
$pthree_sizer->Show(4);
$sizer->Add( $pthree, 0, Wx::wxEXPAND() );
$frame->SetSizer( $sizer );
$frame->Show();
my $app = Wx::SimpleApp->new;
$app->MainLoop;
eval { wIP->new(undef,bless[],'purple::hedgehog') ; 1 } or warn $@
+;
wIP->new(); ## purple hedgehog
} ## end sub Main
package wIP;
use Wx;
use Carp qw/ croak /;
use base qw/ Wx::Panel /;
sub new {
my( $class, $parent, $fileOrImage ) = @_;
my $self = $class->SUPER::new( $parent, -1 );
if( ref $fileOrImage ) {
$fileOrImage->isa( 'Wx::Image' )
or croak "$fileOrImage not isa Wx::Image";
$self->{image} = $fileOrImage;
} elsif( defined $fileOrImage and length $fileOrImage ) {
$self->{file} = $fileOrImage;
$self->{image} = Wx::Image->new;
$self->{image}->LoadFile( $fileOrImage, Wx::wxBITMAP_TYPE_ANY(
+), -1 );
} else {
croak 'Usage: wIP::new( CLASS, $parent, $filenameOrImage ); ';
}
$self->{bitmap} = Wx::Bitmap->new( $self->{image} );
$self->{height} = -1;
$self->{width} = -1;
$self->{transparent} = !! (
$self->{image}->HasAlpha
|| $self->{image}->HasMask
);
Wx::Event::EVT_SIZE( $self, \&wIP::onSize );
Wx::Event::EVT_PAINT( $self, \&wIP::onPaint );
bless $self, $class;
return $self;
} ## end sub new
sub onSize {
my( $self, $evt ) = @_;
$self->Refresh;
$evt->Skip;
}
sub onPaint {
my( $self, $evt ) = @_;
my $dc = Wx::PaintDC->new( $self );
my( $n_width, $n_height ) = $dc->GetSizeWH;
if( $self->{width} != $n_width or $self->{height} != $n_height ) {
unless( $self->{noscale} ){
$self->{bitmap} = Wx::Bitmap->new(
$self->{image}->Scale( $n_width, $n_height, )
);
$self->{width} = $n_width;
$self->{height} = $n_height;
}
$dc->DrawBitmap( $self->{bitmap}, 0, 0, $self->{transparent} )
+;
} else {
$dc->DrawBitmap( $self->{bitmap}, 0, 0, $self->{transparent} )
+;
}
}
sub noscale {
my( $self ) = @_;
$self->{noscale} = !! 1;
$self->SetMinSize(
[ $self->{image}->GetWidth, $self->{image}->GetHeight, ]
);
return $self;
}
=head1 NAME
wIP - wxImagePanel - display image in a panel, autoscaled or not, back
+ground image
=head1 METHODS
=head2 C<<< new >>>
wIP->new( $parent, $filename );
wIP->new( $parent, $filenameOrImage );
wIP->new( $parent, Wx::Image->new( Wx::GetWxPerlIcon() ) )
=head2 C<<< noscale >>>
wIP->new( ... )->noscale
=cut
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.