Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: wxPerl: how to display a resizing image? (wxImagePanelDemo.pl)

by Anonymous Monk
on May 02, 2013 at 23:49 UTC ( #1031813=note: print w/ replies, xml ) Need Help??


in reply to wxPerl: how to display a resizing image? (StaticBitmap doesn't seem to work)?


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


Comment on Re: wxPerl: how to display a resizing image? (wxImagePanelDemo.pl)
Download Code
Re^2: wxPerl: how to display a resizing image? (wxImagePanelDemo.pl)
by jmlynesjr (Pilgrim) on May 03, 2013 at 02:06 UTC

    Nice! Works well with the large cosmos.jpg also.

    James

    There's never enough time to do it right, but always enough time to do it over...

Re^2: wxPerl: how to display a resizing image? (wxImagePanelDemo.pl)
by HelenCr (Monk) on May 03, 2013 at 14:32 UTC
    Thank you, AM. This is really virtuoso work.
    Lot to learn.

    Helen

    (By the way, what is the function of the "yo" buttons? When I run the program, they have no effect?)

      By the way, what is the function of the "yo" buttons? When I run the program, they have no effect?

      Yes, the buttons aren't bound to any event handlers

      The purpose is to demonstrate that buttons are drawn ontop of the panelimage, the panelimage is background image, if you resize the frame/window, the background image should resize, but the buttons wouldn't resize, and you should see the buttons remain ontop of the panelimage

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1031813]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2014-07-12 07:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (238 votes), past polls