[2597] | 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; |
---|
[4504] | 24 | use POSIX qw(ceil floor); |
---|
[2597] | 25 | use Wx qw/ |
---|
| 26 | wxDefaultPosition |
---|
| 27 | wxDefaultSize |
---|
| 28 | wxLC_LIST |
---|
| 29 | wxNO_BORDER |
---|
| 30 | wxLC_EDIT_LABELS |
---|
| 31 | wxLC_ICON |
---|
[4568] | 32 | wxLC_REPORT |
---|
[4152] | 33 | wxLC_SMALL_ICON |
---|
[2597] | 34 | wxIMAGE_LIST_NORMAL |
---|
| 35 | wxBITMAP_TYPE_JPEG |
---|
| 36 | wxBITMAP_TYPE_PNG |
---|
| 37 | wxBLACK |
---|
| 38 | wxLC_VIRTUAL |
---|
| 39 | wxLC_REPORT |
---|
[4568] | 40 | wxLC_AUTOARRANGE |
---|
[2597] | 41 | wxBORDER_THEME |
---|
| 42 | wxLIST_HITTEST_ONITEM |
---|
| 43 | wxLIST_NEXT_ALL |
---|
[3359] | 44 | wxLIST_STATE_SELECTED |
---|
[3362] | 45 | wxLIST_STATE_DONTCARE |
---|
| 46 | wxLIST_STATE_FOCUSED |
---|
[4568] | 47 | wxIMAGE_LIST_SMALL |
---|
[4632] | 48 | wxTheApp |
---|
[2597] | 49 | /; |
---|
| 50 | use Wx::Event qw/ |
---|
| 51 | EVT_LIST_BEGIN_DRAG |
---|
| 52 | EVT_LIST_ITEM_SELECTED |
---|
[4568] | 53 | EVT_SIZE |
---|
[2597] | 54 | /; |
---|
| 55 | |
---|
| 56 | use base qw/Wx::ListCtrl Class::Accessor::Fast/; |
---|
| 57 | __PACKAGE__->mk_accessors( |
---|
| 58 | qw/ |
---|
| 59 | prevItemCount |
---|
| 60 | imagenames |
---|
| 61 | imagelist |
---|
[4504] | 62 | wx_imagelist |
---|
[4568] | 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 |
---|
[2597] | 72 | / |
---|
| 73 | ); |
---|
| 74 | use Data::Dumper; |
---|
[4504] | 75 | |
---|
| 76 | |
---|
[2597] | 77 | sub new { |
---|
[4504] | 78 | my( $class, $params ) = @_; |
---|
[2597] | 79 | |
---|
[4504] | 80 | |
---|
| 81 | my( $self ) = $class->SUPER::new( |
---|
| 82 | $params->{parentwnd}, |
---|
| 83 | -1, |
---|
| 84 | wxDefaultPosition, |
---|
| 85 | wxDefaultSize, |
---|
[4568] | 86 | wxNO_BORDER |
---|
[4504] | 87 | ); |
---|
[4568] | 88 | |
---|
| 89 | $self->wx_img( |
---|
[4504] | 90 | Wx::ImageList->new( 16, 16, 1 ) |
---|
[4568] | 91 | ); |
---|
| 92 | $self->wx_img->Add( |
---|
[4632] | 93 | Wx::Bitmap->new( wxTheApp->resource_path('image.png'), wxBITMAP_TYPE_PNG ) |
---|
[4568] | 94 | ); |
---|
[2597] | 95 | |
---|
[4504] | 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 | ); |
---|
[2597] | 103 | |
---|
[4504] | 104 | $self->image_size( |
---|
| 105 | $params->{image_size} |
---|
| 106 | ); |
---|
[2597] | 107 | |
---|
[4568] | 108 | $self->columns( |
---|
| 109 | $params->{columns} |
---|
| 110 | ); |
---|
[4588] | 111 | |
---|
[4504] | 112 | $self->prevItemCount(-1); |
---|
[2597] | 113 | |
---|
[4504] | 114 | EVT_LIST_BEGIN_DRAG( $self, $self, \&OnBeginDrag); |
---|
| 115 | |
---|
| 116 | EVT_SIZE($self, sub { |
---|
| 117 | my ( $this, $event ) = @_; |
---|
| 118 | |
---|
[4568] | 119 | $this->arrange_items(1); |
---|
[4504] | 120 | $event->Skip(); |
---|
| 121 | } |
---|
| 122 | ); |
---|
[4568] | 123 | |
---|
| 124 | Wx::Event::EVT_IDLE( |
---|
| 125 | $self, |
---|
| 126 | sub { |
---|
| 127 | my ($self, $event)=@_; |
---|
[4504] | 128 | |
---|
[4568] | 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; |
---|
[4504] | 137 | $self->change_display_mode(0); |
---|
[4568] | 138 | } |
---|
| 139 | $event->Skip; |
---|
| 140 | } |
---|
| 141 | ); |
---|
| 142 | |
---|
| 143 | $self->initialize( |
---|
| 144 | { |
---|
[4504] | 145 | |
---|
| 146 | 'Thumbnail' => sub { my ( $self ) = @_; |
---|
[4568] | 147 | $self->Freeze; |
---|
| 148 | $self->ClearAll; |
---|
| 149 | $self->SetSingleStyle(wxLC_ICON); |
---|
| 150 | $self->SetSingleStyle(wxLC_EDIT_LABELS, 0); |
---|
[4504] | 151 | $self->SetImageList( $self->wx_imagelist, wxIMAGE_LIST_NORMAL ) ; |
---|
[4568] | 152 | $self->Thaw; |
---|
| 153 | }, |
---|
[4504] | 154 | 'Thumbnail and caption' => sub { my ( $self ) = @_; |
---|
[4568] | 155 | $self->Freeze; |
---|
| 156 | $self->ClearAll; |
---|
| 157 | $self->SetSingleStyle(wxLC_ICON); |
---|
| 158 | $self->SetSingleStyle(wxLC_EDIT_LABELS); |
---|
[4504] | 159 | $self->SetImageList( $self->wx_imagelist, wxIMAGE_LIST_NORMAL ) ; |
---|
[4568] | 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); |
---|
[4504] | 169 | $self->SetImageList( $self->wx_img, wxIMAGE_LIST_SMALL ) ; |
---|
[4568] | 170 | map { |
---|
| 171 | $self->InsertColumn($i, $_->{label}); |
---|
| 172 | $i++; |
---|
| 173 | }@{$self->columns}; |
---|
| 174 | $self->Thaw; |
---|
| 175 | }, |
---|
| 176 | } |
---|
| 177 | ); |
---|
[4504] | 178 | |
---|
[4568] | 179 | $self->item_refresh( |
---|
| 180 | { |
---|
| 181 | 'Thumbnail' => sub {}, |
---|
| 182 | 'Thumbnail and caption' => sub { my ( $self, $index ) = @_; |
---|
[4504] | 183 | my $image = $self->imagelist->GetImage($index); |
---|
[4568] | 184 | $self->SetItem( |
---|
| 185 | $index, |
---|
| 186 | 0, |
---|
| 187 | $image->site_name, |
---|
[4504] | 188 | ) if defined $image; |
---|
[4568] | 189 | }, |
---|
[4504] | 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 | }, |
---|
[4568] | 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( |
---|
[4504] | 211 | $index, |
---|
| 212 | $self->imagelist->GetImage($index)->site_name, |
---|
| 213 | $index, |
---|
| 214 | ); |
---|
| 215 | }, |
---|
| 216 | 'Property list' => sub { my ( $self, $index ) = @_; |
---|
[4568] | 217 | |
---|
| 218 | $self->InsertImageStringItem( |
---|
[4504] | 219 | $index, |
---|
| 220 | "", |
---|
[4568] | 221 | 0 |
---|
| 222 | ); |
---|
| 223 | $self->ItemRefresh($index); |
---|
[4504] | 224 | |
---|
[4568] | 225 | }, |
---|
| 226 | } |
---|
| 227 | ); |
---|
[4504] | 228 | |
---|
[4568] | 229 | |
---|
| 230 | $self->initialize->{$self->imagelist->display_mode}->($self) if exists $self->initialize->{$self->imagelist->display_mode}; |
---|
[4504] | 231 | |
---|
| 232 | $self; |
---|
[2597] | 233 | } |
---|
| 234 | |
---|
[4504] | 235 | sub ItemRefresh { |
---|
| 236 | my ( $self, $index ) = @_; |
---|
[4568] | 237 | |
---|
| 238 | $self->item_refresh->{$self->imagelist->display_mode}->($self, $index) if exists $self->item_refresh->{$self->imagelist->display_mode}; |
---|
[4504] | 239 | } |
---|
| 240 | |
---|
[2597] | 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 { |
---|
[4568] | 256 | my ( $self ) = @_; |
---|
[2597] | 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 | |
---|
[4568] | 269 | # item is selected |
---|
[2597] | 270 | push @$items, $item |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | $items; |
---|
| 274 | } |
---|
| 275 | |
---|
[3359] | 276 | sub GetAllItems { |
---|
[4568] | 277 | my ( $self ) = @_; |
---|
[2597] | 278 | |
---|
[3359] | 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 | |
---|
[4568] | 290 | # item is selected |
---|
[3359] | 291 | push @$items, $item |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | $items; |
---|
[4568] | 295 | |
---|
[3359] | 296 | } |
---|
[2597] | 297 | |
---|
[3359] | 298 | |
---|
[2597] | 299 | sub Refresh { |
---|
[4568] | 300 | my ( $self ) = @_; |
---|
[2597] | 301 | |
---|
[4504] | 302 | $self->Freeze(); |
---|
[2597] | 303 | $self->DeleteAllItems; |
---|
| 304 | eval { |
---|
[4504] | 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; |
---|
[4507] | 311 | for( my $i = 0 ; $i < $self->wx_imagelist->GetImageCount ; $i++){ |
---|
[4568] | 312 | # call the method corresponding to the display mode |
---|
[4504] | 313 | my $indx = $self->insert_items->{$self->imagelist->display_mode}->($self, $i); |
---|
[4568] | 314 | if( 'Thumbnail' eq $self->imagelist->display_mode){ |
---|
[4504] | 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 | ); |
---|
[4568] | 324 | } |
---|
[2597] | 325 | } |
---|
| 326 | }; |
---|
[4504] | 327 | |
---|
| 328 | map{ |
---|
| 329 | $self->SelectItem( |
---|
| 330 | $_ |
---|
| 331 | ); |
---|
[4568] | 332 | |
---|
| 333 | $self->EnsureVisible($_); |
---|
[4504] | 334 | } @{$self->imagelist->image_selection||[]}; |
---|
| 335 | |
---|
| 336 | $self->Thaw(); |
---|
| 337 | $self->SetFocus; |
---|
[2597] | 338 | } |
---|
| 339 | |
---|
[3362] | 340 | sub SelectItem { |
---|
| 341 | my ( $self, $index ) = @_; |
---|
[2597] | 342 | |
---|
[3362] | 343 | $self->SetItemState( |
---|
| 344 | $index, |
---|
[4324] | 345 | wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED, |
---|
| 346 | wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED |
---|
[4568] | 347 | ); |
---|
[3362] | 348 | } |
---|
[2597] | 349 | |
---|
| 350 | 1; |
---|
| 351 | |
---|