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