1 | # +-----------------------------------------------------------------------+ |
---|
2 | # | pLoader - a Perl photo uploader for Piwigo | |
---|
3 | # +-----------------------------------------------------------------------+ |
---|
4 | # | Copyright(C) 2008 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::wxImageListCtrl; |
---|
21 | use strict; |
---|
22 | use Wx; |
---|
23 | use Wx::DND; |
---|
24 | use POSIX qw(ceil floor); |
---|
25 | use Wx qw/ |
---|
26 | wxDefaultPosition |
---|
27 | wxDefaultSize |
---|
28 | wxLC_LIST |
---|
29 | wxNO_BORDER |
---|
30 | wxLC_EDIT_LABELS |
---|
31 | wxLC_ICON |
---|
32 | wxLC_REPORT |
---|
33 | wxLC_SMALL_ICON |
---|
34 | wxIMAGE_LIST_NORMAL |
---|
35 | wxBITMAP_TYPE_JPEG |
---|
36 | wxBITMAP_TYPE_PNG |
---|
37 | wxBLACK |
---|
38 | wxLC_VIRTUAL |
---|
39 | wxLC_REPORT |
---|
40 | wxLC_AUTOARRANGE |
---|
41 | wxBORDER_THEME |
---|
42 | wxLIST_HITTEST_ONITEM |
---|
43 | wxLIST_NEXT_ALL |
---|
44 | wxLIST_STATE_SELECTED |
---|
45 | wxLIST_STATE_DONTCARE |
---|
46 | wxLIST_STATE_FOCUSED |
---|
47 | wxIMAGE_LIST_SMALL |
---|
48 | wxTheApp |
---|
49 | /; |
---|
50 | use Wx::Event qw/ |
---|
51 | EVT_LIST_BEGIN_DRAG |
---|
52 | EVT_LIST_ITEM_SELECTED |
---|
53 | EVT_SIZE |
---|
54 | /; |
---|
55 | |
---|
56 | use base qw/Wx::ListCtrl Class::Accessor::Fast/; |
---|
57 | __PACKAGE__->mk_accessors( |
---|
58 | qw/ |
---|
59 | prevItemCount |
---|
60 | imagenames |
---|
61 | imagelist |
---|
62 | wx_imagelist |
---|
63 | image_size |
---|
64 | arrange_items |
---|
65 | styles |
---|
66 | insert_items |
---|
67 | initialize |
---|
68 | columns |
---|
69 | change_display_mode |
---|
70 | wx_img |
---|
71 | item_refresh |
---|
72 | / |
---|
73 | ); |
---|
74 | use Data::Dumper; |
---|
75 | |
---|
76 | |
---|
77 | sub new { |
---|
78 | my( $class, $params ) = @_; |
---|
79 | |
---|
80 | |
---|
81 | my( $self ) = $class->SUPER::new( |
---|
82 | $params->{parentwnd}, |
---|
83 | -1, |
---|
84 | wxDefaultPosition, |
---|
85 | wxDefaultSize, |
---|
86 | wxNO_BORDER |
---|
87 | ); |
---|
88 | |
---|
89 | $self->wx_img( |
---|
90 | Wx::ImageList->new( 16, 16, 1 ) |
---|
91 | ); |
---|
92 | $self->wx_img->Add( |
---|
93 | Wx::Bitmap->new( wxTheApp->resource_path('image.png'), wxBITMAP_TYPE_PNG ) |
---|
94 | ); |
---|
95 | |
---|
96 | $self->imagelist( |
---|
97 | $params->{imagelist} |
---|
98 | ); |
---|
99 | # bitmap container for photo selection |
---|
100 | $self->wx_imagelist( |
---|
101 | $params->{wx_imagelist}||$self->imagelist->wx_thumb_imglist |
---|
102 | ); |
---|
103 | |
---|
104 | $self->image_size( |
---|
105 | $params->{image_size} |
---|
106 | ); |
---|
107 | |
---|
108 | $self->columns( |
---|
109 | $params->{columns} |
---|
110 | ); |
---|
111 | |
---|
112 | $self->prevItemCount(-1); |
---|
113 | |
---|
114 | EVT_LIST_BEGIN_DRAG( $self, $self, \&OnBeginDrag); |
---|
115 | |
---|
116 | EVT_SIZE($self, sub { |
---|
117 | my ( $this, $event ) = @_; |
---|
118 | |
---|
119 | $this->arrange_items(1); |
---|
120 | $event->Skip(); |
---|
121 | } |
---|
122 | ); |
---|
123 | |
---|
124 | Wx::Event::EVT_IDLE( |
---|
125 | $self, |
---|
126 | sub { |
---|
127 | my ($self, $event)=@_; |
---|
128 | |
---|
129 | if($self->arrange_items){ |
---|
130 | $self->Refresh; |
---|
131 | $self->arrange_items(0); |
---|
132 | } |
---|
133 | if($self->change_display_mode){ |
---|
134 | my $dm = $self->imagelist->display_mode; |
---|
135 | $self->initialize->{$dm}->($self) if exists $self->initialize->{$dm}; |
---|
136 | $self->Refresh; |
---|
137 | $self->change_display_mode(0); |
---|
138 | } |
---|
139 | $event->Skip; |
---|
140 | } |
---|
141 | ); |
---|
142 | |
---|
143 | $self->initialize( |
---|
144 | { |
---|
145 | |
---|
146 | 'Thumbnail' => sub { my ( $self ) = @_; |
---|
147 | $self->Freeze; |
---|
148 | $self->ClearAll; |
---|
149 | $self->SetSingleStyle(wxLC_ICON); |
---|
150 | $self->SetSingleStyle(wxLC_EDIT_LABELS, 0); |
---|
151 | $self->SetImageList( $self->wx_imagelist, wxIMAGE_LIST_NORMAL ) ; |
---|
152 | $self->Thaw; |
---|
153 | }, |
---|
154 | 'Thumbnail and caption' => sub { my ( $self ) = @_; |
---|
155 | $self->Freeze; |
---|
156 | $self->ClearAll; |
---|
157 | $self->SetSingleStyle(wxLC_ICON); |
---|
158 | $self->SetSingleStyle(wxLC_EDIT_LABELS); |
---|
159 | $self->SetImageList( $self->wx_imagelist, wxIMAGE_LIST_NORMAL ) ; |
---|
160 | $self->Thaw; |
---|
161 | }, |
---|
162 | 'Property list' => sub { my ( $self ) = @_; |
---|
163 | my $i=0; |
---|
164 | |
---|
165 | $self->Freeze; |
---|
166 | $self->ClearAll; |
---|
167 | $self->SetSingleStyle(wxLC_REPORT); |
---|
168 | $self->SetSingleStyle(wxLC_EDIT_LABELS); |
---|
169 | $self->SetImageList( $self->wx_img, wxIMAGE_LIST_SMALL ) ; |
---|
170 | map { |
---|
171 | $self->InsertColumn($i, $_->{label}); |
---|
172 | $i++; |
---|
173 | }@{$self->columns}; |
---|
174 | $self->Thaw; |
---|
175 | }, |
---|
176 | } |
---|
177 | ); |
---|
178 | |
---|
179 | $self->item_refresh( |
---|
180 | { |
---|
181 | 'Thumbnail' => sub {}, |
---|
182 | 'Thumbnail and caption' => sub { my ( $self, $index ) = @_; |
---|
183 | my $image = $self->imagelist->GetImage($index); |
---|
184 | $self->SetItem( |
---|
185 | $index, |
---|
186 | 0, |
---|
187 | $image->site_name, |
---|
188 | ) if defined $image; |
---|
189 | }, |
---|
190 | 'Property list' => sub { my ( $self, $index ) = @_; |
---|
191 | my $col = 0; |
---|
192 | my $image = $self->imagelist->GetImage($index); |
---|
193 | map{ |
---|
194 | $self->SetItem( |
---|
195 | $index, |
---|
196 | $col, |
---|
197 | $image->$_ |
---|
198 | ) if defined $image; |
---|
199 | $col++; |
---|
200 | } |
---|
201 | qw/site_name site_comment site_author file create_date/; |
---|
202 | }, |
---|
203 | } |
---|
204 | ); |
---|
205 | |
---|
206 | $self->insert_items( |
---|
207 | { |
---|
208 | 'Thumbnail' => sub { my ( $self, $index ) = @_; $self->InsertImageItem($index, $index);}, |
---|
209 | 'Thumbnail and caption' => sub { my ( $self, $index ) = @_; |
---|
210 | $self->InsertImageStringItem( |
---|
211 | $index, |
---|
212 | $self->imagelist->GetImage($index)->site_name, |
---|
213 | $index, |
---|
214 | ); |
---|
215 | }, |
---|
216 | 'Property list' => sub { my ( $self, $index ) = @_; |
---|
217 | |
---|
218 | $self->InsertImageStringItem( |
---|
219 | $index, |
---|
220 | "", |
---|
221 | 0 |
---|
222 | ); |
---|
223 | $self->ItemRefresh($index); |
---|
224 | |
---|
225 | }, |
---|
226 | } |
---|
227 | ); |
---|
228 | |
---|
229 | |
---|
230 | $self->initialize->{$self->imagelist->display_mode}->($self) if exists $self->initialize->{$self->imagelist->display_mode}; |
---|
231 | |
---|
232 | $self; |
---|
233 | } |
---|
234 | |
---|
235 | sub ItemRefresh { |
---|
236 | my ( $self, $index ) = @_; |
---|
237 | |
---|
238 | $self->item_refresh->{$self->imagelist->display_mode}->($self, $index) if exists $self->item_refresh->{$self->imagelist->display_mode}; |
---|
239 | } |
---|
240 | |
---|
241 | sub OnBeginDrag { |
---|
242 | my( $self, $event ) = @_; |
---|
243 | |
---|
244 | |
---|
245 | my $data = Wx::TextDataObject->new( |
---|
246 | Dumper $self->GetSelectedItems |
---|
247 | ); |
---|
248 | my $source = Wx::DropSource->new( $self ); |
---|
249 | $source->SetData( $data ); |
---|
250 | |
---|
251 | $source->DoDragDrop( 1 ); |
---|
252 | } |
---|
253 | |
---|
254 | |
---|
255 | sub GetSelectedItems { |
---|
256 | my ( $self ) = @_; |
---|
257 | |
---|
258 | # find selected items |
---|
259 | my $item = -1; |
---|
260 | my $items = []; |
---|
261 | while(1) { |
---|
262 | $item = $self->GetNextItem( |
---|
263 | $item, |
---|
264 | wxLIST_NEXT_ALL, |
---|
265 | wxLIST_STATE_SELECTED |
---|
266 | ); |
---|
267 | last if(-1 == $item); |
---|
268 | |
---|
269 | # item is selected |
---|
270 | push @$items, $item |
---|
271 | } |
---|
272 | |
---|
273 | $items; |
---|
274 | } |
---|
275 | |
---|
276 | sub GetAllItems { |
---|
277 | my ( $self ) = @_; |
---|
278 | |
---|
279 | # find selected items |
---|
280 | my $item = -1; |
---|
281 | my $items = []; |
---|
282 | while(1) { |
---|
283 | $item = $self->GetNextItem( |
---|
284 | $item, |
---|
285 | wxLIST_NEXT_ALL, |
---|
286 | wxLIST_STATE_DONTCARE |
---|
287 | ); |
---|
288 | last if(-1 == $item); |
---|
289 | |
---|
290 | # item is selected |
---|
291 | push @$items, $item |
---|
292 | } |
---|
293 | |
---|
294 | $items; |
---|
295 | |
---|
296 | } |
---|
297 | |
---|
298 | |
---|
299 | sub Refresh { |
---|
300 | my ( $self ) = @_; |
---|
301 | |
---|
302 | $self->Freeze(); |
---|
303 | $self->DeleteAllItems; |
---|
304 | eval { |
---|
305 | my $y0 = 0; |
---|
306 | my $x0 = 0; |
---|
307 | |
---|
308 | my $x_spacing = $self->image_size + 2; |
---|
309 | my $y_spacing = $self->image_size + 2; |
---|
310 | my $ipr = floor(($self->GetClientSize->GetWidth - 0.20*$x_spacing)/$x_spacing)||1; |
---|
311 | for( my $i = 0 ; $i < $self->wx_imagelist->GetImageCount ; $i++){ |
---|
312 | # call the method corresponding to the display mode |
---|
313 | my $indx = $self->insert_items->{$self->imagelist->display_mode}->($self, $i); |
---|
314 | if( 'Thumbnail' eq $self->imagelist->display_mode){ |
---|
315 | my $row = floor($i/$ipr); |
---|
316 | my $col = $i%$ipr; |
---|
317 | $self->SetItemPosition( |
---|
318 | $i, |
---|
319 | Wx::Point->new( |
---|
320 | $x0 + $x_spacing*$col, |
---|
321 | $y0 + $y_spacing*$row, |
---|
322 | ), |
---|
323 | ); |
---|
324 | } |
---|
325 | } |
---|
326 | }; |
---|
327 | |
---|
328 | map{ |
---|
329 | $self->SelectItem( |
---|
330 | $_ |
---|
331 | ); |
---|
332 | |
---|
333 | $self->EnsureVisible($_); |
---|
334 | } @{$self->imagelist->image_selection||[]}; |
---|
335 | |
---|
336 | $self->Thaw(); |
---|
337 | $self->SetFocus; |
---|
338 | } |
---|
339 | |
---|
340 | sub SelectItem { |
---|
341 | my ( $self, $index ) = @_; |
---|
342 | |
---|
343 | $self->SetItemState( |
---|
344 | $index, |
---|
345 | wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED, |
---|
346 | wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED |
---|
347 | ); |
---|
348 | } |
---|
349 | |
---|
350 | 1; |
---|
351 | |
---|