| 1 | ############################################################################# | ||
| 2 | ## Name: Wx::Perl::VirtualTreeCtrl | ||
| 3 | ## Purpose: TreeCtrl that populates it's children on demand | ||
| 4 | ## Author: Simon Flack | ||
| 5 | ## Modified by: $Author: andreww $ on $Date: 2006/10/05 14:22:34 $ | ||
| 6 | ## Created: 08/10/2002 | ||
| 7 | ## RCS-ID: $Id: VirtualTreeCtrl.pm,v 1.17 2006/10/05 14:22:34 andreww Exp $ | ||
| 8 | ############################################################################# | ||
| 9 | |||
| 10 | package Wx::Perl::VirtualTreeCtrl; | ||
| 11 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use Exporter; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 0 | ▶ | use Wx; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | use Wx::Event 'EVT_TREE_ITEM_EXPANDING'; | ||
| 15 | use Carp; | ||
| 16 | |||
| 17 | use vars qw($VERSION @EXPORT_OK @ISA $AUTOLOAD); | ||
| 18 | |||
| 19 | @ISA = qw(Wx::EvtHandler Exporter); | ||
| 20 | @EXPORT_OK = 'EVT_POPULATE_TREE_ITEM'; | ||
| 21 | $VERSION = sprintf'%d.%03d', q$Revision: 1.17 $ =~ /: (\d+)\.(\d+)/; | ||
| 22 | |||
| 23 | use constant POPULATE_TREE_ITEM_EVENT => Wx::NewEventType; | ||
| 24 | |||
| 25 | sub new { | ||
| 26 | my $class = shift; | ||
| 27 | my $tree; | ||
| 28 | if (@_ == 1) { | ||
| 29 | $tree = shift; | ||
| 30 | croak "Supplied tree control is not a Wx::TreeCtrl" | ||
| 31 | unless ref $tree && $tree->isa('Wx::TreeCtrl'); | ||
| 32 | } elsif (@_ > 1) { | ||
| 33 | $tree = Wx::TreeCtrl->new(@_); | ||
| 34 | } else { | ||
| 35 | croak "USAGE: $class->new(\$parent, \$id, \$pos, \$size, ...)\n" | ||
| 36 | . "or $class->new(\$treectrl)"; | ||
| 37 | } | ||
| 38 | |||
| 39 | my $self = $class->SUPER::new(); # Wx::EvtHandler | ||
| 40 | $self->{_tree_ctrl} = $tree; | ||
| 41 | $self->{_tree_ctrl}->PushEventHandler($self); | ||
| 42 | return $self; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub EVT_POPULATE_TREE_ITEM ($$$) { | ||
| 46 | my ($evt_handler, $self, $callable) = @_; | ||
| 47 | my $real_tree = $self->{_tree_ctrl}; | ||
| 48 | $evt_handler->Connect($real_tree, -1, POPULATE_TREE_ITEM_EVENT, $callable); | ||
| 49 | EVT_TREE_ITEM_EXPANDING($self, $real_tree, \&_onExpandItem); | ||
| 50 | } | ||
| 51 | |||
| 52 | # Proxy Wx::TreeCtrl methods to the aggregated tree control | ||
| 53 | sub AUTOLOAD { | ||
| 54 | my $self = shift; | ||
| 55 | my ($method) = $AUTOLOAD =~ /.*:(.*)/; | ||
| 56 | my $tctl = $self->{_tree_ctrl}; | ||
| 57 | my $class = ref $tctl; | ||
| 58 | croak "Undefined method '$method' in class '$class'" | ||
| 59 | unless $tctl->can($method); | ||
| 60 | return $tctl->$method(@_); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub AddRoot { | ||
| 64 | my $self = shift; | ||
| 65 | my $id = $self->GetTree->AddRoot(@_); | ||
| 66 | $self->SetItemHasChildren($id, 1); | ||
| 67 | return $id; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub GetTree { | ||
| 71 | my $self = shift; | ||
| 72 | return $self->{_tree_ctrl}; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub GetPath { | ||
| 76 | my $self = shift; | ||
| 77 | my ($item) = @_; | ||
| 78 | |||
| 79 | my @path; | ||
| 80 | push @path, $self->{_tree_ctrl}->GetItemText($item); | ||
| 81 | |||
| 82 | my $parent = $self->{_tree_ctrl}->GetItemParent($item); | ||
| 83 | while ($parent && $parent->IsOk) { | ||
| 84 | push @path, $self->{_tree_ctrl}->GetItemText($parent); | ||
| 85 | $parent = $self->{_tree_ctrl}->GetItemParent($parent); | ||
| 86 | } | ||
| 87 | return reverse @path; | ||
| 88 | } | ||
| 89 | |||
| 90 | # When a Tree item is expanded, trigger a POPULATE_TREE_ITEM_EVENT | ||
| 91 | sub _onExpandItem { | ||
| 92 | my $self = shift; | ||
| 93 | my ($event) = @_; | ||
| 94 | |||
| 95 | my $item = $event->GetItem; | ||
| 96 | my $populate_evt = $self->_populate_event($item); | ||
| 97 | $self->ProcessEvent($populate_evt); | ||
| 98 | |||
| 99 | $event->Skip; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub _populate_event { | ||
| 103 | my $self = shift; | ||
| 104 | my $item = shift; | ||
| 105 | |||
| 106 | my $event = Wx::Perl::VirtualTreeCtrl::Event->new( | ||
| 107 | $self->GetId, POPULATE_TREE_ITEM_EVENT, $item | ||
| 108 | ); | ||
| 109 | $event->SetEventObject($self); | ||
| 110 | |||
| 111 | return $event; | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | ############################################################################ | ||
| 116 | |||
| 117 | package Wx::Perl::VirtualTreeCtrl::Event; | ||
| 118 | |||
| 119 | use vars '@ISA'; | ||
| 120 | @ISA = 'Wx::PlCommandEvent'; | ||
| 121 | |||
| 122 | sub new { | ||
| 123 | my $class = shift; | ||
| 124 | my ($id, $event_id, $item) = @_; | ||
| 125 | my $self = $class->SUPER::new($event_id, $id); | ||
| 126 | $self->{_item} = $item; | ||
| 127 | return bless $self, $class; | ||
| 128 | } | ||
| 129 | |||
| 130 | # Implement things missing from Wx::PlCommandEvent | ||
| 131 | |||
| 132 | sub SetEventObject { | ||
| 133 | my $self = shift; | ||
| 134 | $self->{_event_object} = shift; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub GetEventObject { | ||
| 138 | my $self = shift; | ||
| 139 | return $self->{_event_object}; | ||
| 140 | } | ||
| 141 | |||
| 142 | sub GetItem { | ||
| 143 | my $self = shift; | ||
| 144 | return ($self->{_item} || -1); | ||
| 145 | } | ||
| 146 | |||
| 147 | 1; | ||
| 148 | |||
| 149 | =pod | ||
| 150 | |||
| 151 | =head1 NAME | ||
| 152 | |||
| 153 | Wx::Perl::VirtualTreeCtrl - Build a tree control on demand | ||
| 154 | |||
| 155 | =head1 DERIVED FROM | ||
| 156 | |||
| 157 | Wx::EvtHandler | ||
| 158 | |||
| 159 | Standard C<Wx::TreeCtrl> and C<Wx::Window> methods can be used with a virtual | ||
| 160 | tree control. | ||
| 161 | |||
| 162 | =head1 SYNOPSIS | ||
| 163 | |||
| 164 | use Wx::Perl::VirtualTreeCtrl 'EVT_POPULATE_TREE_ITEM'; | ||
| 165 | |||
| 166 | my $tree = new Wx::Perl::VirtualTreeCtrl($tree_ctrl); | ||
| 167 | EVT_POPULATE_TREE_ITEM($self, $tree, \&AddChildren); | ||
| 168 | my $root = $tree->AddRoot($name, $data); | ||
| 169 | $tree->Expand($root); | ||
| 170 | |||
| 171 | sub AddChildren { | ||
| 172 | my ($self, $event) = @_; | ||
| 173 | |||
| 174 | my $tree = $event->GetEventObject; | ||
| 175 | my $item = $event->GetItem; | ||
| 176 | my $item_data = $tree->GetPlData($item); | ||
| 177 | |||
| 178 | if ($tree->GetChildrenCount($item, 0)) { | ||
| 179 | # update existing children ... | ||
| 180 | |||
| 181 | my ($child, $cookie) = $tree->GetFirstChild($item); | ||
| 182 | while ($child && $child->IsOk) { | ||
| 183 | my $child_data = $tree->GetPlData($child); | ||
| 184 | # synchronise deletions | ||
| 185 | if (child_was_deleted($child_data)) { | ||
| 186 | $tree->Delete($child); | ||
| 187 | } | ||
| 188 | |||
| 189 | ($child, $cookie) = $tree->GetNextChild($child, $cookie); | ||
| 190 | } | ||
| 191 | |||
| 192 | } else { | ||
| 193 | # add children for the first time | ||
| 194 | |||
| 195 | my @child_data = expensive_process_to_get_children($item_data); | ||
| 196 | foreach (@child_data) { | ||
| 197 | my $child = $tree->AppendItem($item, $_->{name}); | ||
| 198 | # make item expandable if it's a folder | ||
| 199 | $tree->SetItemHasChildren($child, 1) if ...; | ||
| 200 | } | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | =head2 DESCRIPTION | ||
| 205 | |||
| 206 | This module implements a tree like the Wx::TreeCtrl except that it populates | ||
| 207 | its items dynamically when nodes in the tree are expanded. You may prefer this | ||
| 208 | control over the standard tree control when you are populating your tree from a | ||
| 209 | remote source such as a database, or when your tree is very large. | ||
| 210 | |||
| 211 | This module implements the same interface as a standard C<Wx::TreeCtrl>. | ||
| 212 | |||
| 213 | =head2 METHODS | ||
| 214 | |||
| 215 | =over 4 | ||
| 216 | |||
| 217 | =item new ($parent, $id, $pos = wxDefaultPosition, $size = wxDefaultSize, ...) | ||
| 218 | |||
| 219 | Returns a new C<Wx::Perl::VirtualTreeCtrl> object. The parameters are the same | ||
| 220 | as those required by a standard Wx::TreeCtrl. | ||
| 221 | |||
| 222 | =item new ($tree_ctrl) | ||
| 223 | |||
| 224 | Returns a C<Wx::Perl::VirtualTreeCtrl> object that uses an existing | ||
| 225 | C<Wx::TreeCtrl> instead of creating its own. You may use this method when your | ||
| 226 | interface is built using C<Wx::XRC> resources or a third-party tool like | ||
| 227 | wxGlade. | ||
| 228 | |||
| 229 | =item GetPath($item) | ||
| 230 | |||
| 231 | Returns a list of item labels from the root item down the provided path | ||
| 232 | |||
| 233 | =item GetTree() | ||
| 234 | |||
| 235 | Returns a Wx::TreeCtrl suitable for regular C<wxTreeEvent>s and C<Wx::Sizer> | ||
| 236 | operations | ||
| 237 | |||
| 238 | # Add a virtual tree to a sizer | ||
| 239 | $sizer->Add($vtree->GetTree, 1, wxALL|wxGROW, 4); | ||
| 240 | |||
| 241 | # Attach event listener for tree item activation (e.g. double click) | ||
| 242 | EVT_TREE_ITEM_ACTIVATED($win, $vtree->GetTree, \&onActivateItem); | ||
| 243 | |||
| 244 | =back | ||
| 245 | |||
| 246 | =head2 EVENTS | ||
| 247 | |||
| 248 | =over 4 | ||
| 249 | |||
| 250 | =item EVT_POPULATE_TREE_ITEM($event_handler, $virtual_tree, \&event_callback) | ||
| 251 | |||
| 252 | The item is being expanded and the control is requesting that part of the tree | ||
| 253 | be populated. | ||
| 254 | |||
| 255 | Your event callback will be passed the C<$event_handler> and event object, as | ||
| 256 | with other wxCommand events. The event object has the following accessors: | ||
| 257 | |||
| 258 | $event->GetEventObject() # returns the virtual tree control | ||
| 259 | $event->GetItem() # returns the tree item id that is being populated | ||
| 260 | |||
| 261 | See the example in L<"SYNOPSIS">. | ||
| 262 | |||
| 263 | For further information, see the wxCommandEvent documentation, and the Event | ||
| 264 | handling overview from the wxWidgets documentation. | ||
| 265 | |||
| 266 | =item EVT_TREE_* | ||
| 267 | |||
| 268 | Standard C<wxTreeEvent>s can be used with a virtual tree control. See | ||
| 269 | L<"GetTree()"> for examples. | ||
| 270 | |||
| 271 | =back | ||
| 272 | |||
| 273 | =head2 SEE ALSO | ||
| 274 | |||
| 275 | =over | ||
| 276 | |||
| 277 | =item L<Wx::TreeCtrl> | ||
| 278 | |||
| 279 | The standard tree control from which this object is derived. | ||
| 280 | |||
| 281 | =item L<Wx::Perl::TreeChecker> | ||
| 282 | |||
| 283 | A tree control with checkboxes that is compatible with this module (you can | ||
| 284 | have a virtual tree checker) | ||
| 285 | |||
| 286 | =item wxWidgets | ||
| 287 | |||
| 288 | L<http:E<sol>E<sol>www.wxwidgets.org> | ||
| 289 | |||
| 290 | =item wxPerl | ||
| 291 | |||
| 292 | L<http:E<sol>E<sol>wxperl.sourceforge.net> | ||
| 293 | |||
| 294 | =back | ||
| 295 | |||
| 296 | =head2 AUTHOR | ||
| 297 | |||
| 298 | Simon Flack <cpan _at_ bbc _dot_ co _dot_ uk> | ||
| 299 | |||
| 300 | =head1 COPYRIGHT | ||
| 301 | |||
| 302 | (c) BBC 2005. This program is free software; you can redistribute it and/or modify it under the GNU GPL. | ||
| 303 | |||
| 304 | See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt | ||
| 305 | |||
| 306 | |||
| 307 | =cut | ||