1 | # +-----------------------------------------------------------------------+ |
---|
2 | # | pLoader - a Perl photo uploader for Piwigo | |
---|
3 | # +-----------------------------------------------------------------------+ |
---|
4 | # | Copyright(C) 2008-2010 Piwigo Team http://piwigo.org | |
---|
5 | # +-----------------------------------------------------------------------+ |
---|
6 | # | This program is free software; you can redistribute it and/or modify | |
---|
7 | # | it under the terms of the GNU General Public License as published by | |
---|
8 | # | the Free Software Foundation | |
---|
9 | # | | |
---|
10 | # | This program is distributed in the hope that it will be useful, but | |
---|
11 | # | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
12 | # | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
13 | # | General Public License for more details. | |
---|
14 | # | | |
---|
15 | # | You should have received a copy of the GNU General Public License | |
---|
16 | # | along with this program; if not, write to the Free Software | |
---|
17 | # | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
18 | # | USA. | |
---|
19 | # +-----------------------------------------------------------------------+ |
---|
20 | package Uploader::GUI::wxImagePreview; |
---|
21 | use strict; |
---|
22 | use base qw/Wx::Dialog Class::Accessor::Fast/; |
---|
23 | use Wx qw/ |
---|
24 | wxDefaultPosition |
---|
25 | wxDefaultSize |
---|
26 | wxBG_STYLE_CUSTOM |
---|
27 | |
---|
28 | wxIMAGE_QUALITY_HIGH |
---|
29 | |
---|
30 | wxRESIZE_BORDER |
---|
31 | |
---|
32 | wxDIALOG_EX_METAL |
---|
33 | |
---|
34 | wxDIALOG_NO_PARENT |
---|
35 | wxDEFAULT_DIALOG_STYLE |
---|
36 | |
---|
37 | wxMAXIMIZE_BOX |
---|
38 | wxMINIMIZE_BOX |
---|
39 | |
---|
40 | wxSTAY_ON_TOP |
---|
41 | /; |
---|
42 | |
---|
43 | __PACKAGE__->mk_accessors( |
---|
44 | qw/ |
---|
45 | image |
---|
46 | image_size |
---|
47 | caption |
---|
48 | / |
---|
49 | ); |
---|
50 | use Data::Dumper; |
---|
51 | use Wx::Event qw/ |
---|
52 | EVT_PAINT |
---|
53 | EVT_SIZE |
---|
54 | /; |
---|
55 | |
---|
56 | sub new { |
---|
57 | my( $class, $params ) = @_; |
---|
58 | my( $self ) = $class->SUPER::new( |
---|
59 | undef, |
---|
60 | -1, |
---|
61 | $params->{caption}, |
---|
62 | wxDefaultPosition, |
---|
63 | [400,300], |
---|
64 | wxMAXIMIZE_BOX| |
---|
65 | wxMINIMIZE_BOX| |
---|
66 | wxRESIZE_BORDER| |
---|
67 | wxDEFAULT_DIALOG_STYLE| |
---|
68 | wxSTAY_ON_TOP| |
---|
69 | wxDIALOG_NO_PARENT |
---|
70 | ); |
---|
71 | |
---|
72 | $self->SetBackgroundStyle(wxBG_STYLE_CUSTOM); |
---|
73 | |
---|
74 | Wx::InitAllImageHandlers(); |
---|
75 | |
---|
76 | EVT_SIZE($self, sub { |
---|
77 | my ( $this, $event ) = @_; |
---|
78 | |
---|
79 | $this->Refresh; |
---|
80 | $event->Skip(); |
---|
81 | } |
---|
82 | ); |
---|
83 | |
---|
84 | |
---|
85 | EVT_PAINT( |
---|
86 | $self, |
---|
87 | sub { |
---|
88 | my ( $this, $event ) = @_; |
---|
89 | |
---|
90 | my $dc = Wx::AutoBufferedPaintDC->new($this); |
---|
91 | if($this->image){ |
---|
92 | |
---|
93 | my ($w, $h) = $this->GetSizeWH; |
---|
94 | my $p_prop = $w / $h; |
---|
95 | |
---|
96 | # calculation made to keep aspect |
---|
97 | # ratio and maximize image size |
---|
98 | |
---|
99 | my $scale; |
---|
100 | my ($img_w, $img_h) = @{$this->image_size}; |
---|
101 | return unless $img_w; |
---|
102 | return unless $img_h; |
---|
103 | |
---|
104 | my $img_prop = $img_w / $img_h; |
---|
105 | |
---|
106 | $scale = ($p_prop > $img_prop) |
---|
107 | ? ($h / $img_h) |
---|
108 | : ($w / $img_w); |
---|
109 | |
---|
110 | $scale = 1 if($scale > 1); |
---|
111 | |
---|
112 | my ($sw, $sh) = map({$_ * $scale} $img_w, $img_h); |
---|
113 | my $bmp = Wx::Bitmap->new($this->image->Scale($sw, $sh, wxIMAGE_QUALITY_HIGH)); |
---|
114 | my $xoff = ($w - $sw) / 2; |
---|
115 | my $yoff = ($h - $sh) / 2; |
---|
116 | $dc->DrawBitmap($bmp, $xoff, $yoff, $dc->Clear ? 1:1); |
---|
117 | } |
---|
118 | else{ |
---|
119 | $dc->Clear; |
---|
120 | } |
---|
121 | } |
---|
122 | ); |
---|
123 | |
---|
124 | |
---|
125 | $self; |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | 1; |
---|