From: Mattia B. <mb...@ds...> - 2002-04-21 20:46:14
|
You forgot platform & wxPerl version, so I won't give you the solution ;-p > I have got a wxPerl application that contains a Wx::TreeCtrl and a > Wx::ListCtrl. Whenever a Wx::TreeCtrl item is activated, new items > are inserted in the list control. The "items" are stored as > data in the tree control. > > I am attempting to supply a hashref as data for each item in the > list control. So, in my EVT_TREE_SEL_CHANGED callback function > I have the following code: <snip> > Adding the actual *items* works fine (they display properly in the list). > The only problem is that setting the item data simply *does not work*. > Bizarrely, whenever I print out $id in the above function, it is > *always* zero. > > Anyway in my EVT_LIST_ITEM_ACTIVATED callback function I have: > > my $file_details = $object->{list}->GetItemData($event->GetItem()); > > but this causes a message box to appear saying: > > "Cannot retrieve information for list control item XXXXXXXX" (i.e. it > includes the item number). I see; this: the problem is that GetItemData takes the index of the item $list->GetItemData( $ebent->GetIndex() ), or $event->GetData() works fine > I think the problem is that there is no SetPlData etc. for listctrl like > there is for treectrl. The C++ documentation seems to show that > a listctrl item can only have a 'long' associated with it. Yes, you can use the long as an index in an array ( or in an hash ) containing the hashrefs, though. > How can I associate a hashref with each item in a listctrl and how I > can access this hashref when the item is "activated"? See above HTH Mattia |
From: Nick J. <sk...@er...> - 2002-04-22 12:34:55
|
On Sun, 21 Apr 2002, Mattia Barbon wrote: > You forgot platform & wxPerl version, so I won't give you the solution ;-p > Win32, ActivePerl 5.6.1 build 628, wxPerl 0.09 -- sorry for not including before. > I see; this: the problem is that GetItemData takes the index of the item > $list->GetItemData( $ebent->GetIndex() ), or $event->GetData() works fine > > Yes, you can use the long as an index in an array ( or in an hash ) containing > the hashrefs, though. > Ok, so I should have a hash keyed on the long and then have the value for that key be a hashref? Could you provide some sample code, please? I would really appreciate it. I'm confused that the $id in my original code is always 0. I would really appreciate some sample code here. > > How can I associate a hashref with each item in a listctrl and how I > > can access this hashref when the item is "activated"? > See above > I would appreciate it if you could produce a simple snippet of code to illustrate this (inserting an item, storing its data in a global hash keyed on the long and then retrieving). I'm still pretty new to Wx and finding it pretty difficult. - Nick |
From: Mattia B. <mb...@ds...> - 2002-04-22 20:37:24
|
> On Sun, 21 Apr 2002, Mattia Barbon wrote: > > > You forgot platform & wxPerl version, so I won't give you the solution ;-p > > > > Win32, ActivePerl 5.6.1 build 628, wxPerl 0.09 -- sorry for not including > before. My guess was almost right :-) > > I see; this: the problem is that GetItemData takes the index of the item > > $list->GetItemData( $ebent->GetIndex() ), or $event->GetData() works fine > > > > Yes, you can use the long as an index in an array ( or in an hash ) containing > > the hashrefs, though. > > > > Ok, so I should have a hash keyed on the long and then have the value for > that key be a hashref? Yes, exactly > Could you provide some sample code, please? I would really appreciate it. > I'm confused that the $id in my original code is always 0. I would really > appreciate some sample code here. note that I didn't test it, but it should work ( or at leats give the idea ) # this should be in $object, but this is just an example... my @entries; # Add items to list foreach (@{$object->{tree}->GetPlData($event->GetItem())}) { my $id = $object->{list}->InsertStringItem(0, $_->{name}); my %fileDetails = ( name => $_->{name}, abspath => $_->{abspath}, volume => $_->{volume} ); # remember to update @entries when you add/remove items # from the list push @entries, \%fileDetails; my $index = scalar(@entries) - 1; $object->{list}->SetItemData($id, $index); $object->{list}->SetItem($id, 1, $_->{duration}); $object->{list}->SetItem($id, 2, $_->{title}); $object->{list}->SetItem($id, 3, $_->{artist}); $object->{list}->SetItem($id, 4, $_->{album}); $object->{list}->SetItem($id, 5, $_->{bitrate}); $object->{list}->SetItem($id, 6, (sprintf "%.2f", ($_->{size} / 1000000)) . ' MB'); } sub OnActivate { my($this, $event) = @_; my $index = $event->GetData(); # or $list->GetItemData($event->GetIndex()); my $data = $entries[$index]; #... } HTH Mattia |