VirtualDirSelector.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
↑ Summary VirtualTreeCtrl.pm »
1 #############################################################################
2 ## Name: Wx::Perl::VirtualDirSelector
3 ## Purpose: A wxDirSelector clone driven by a virtual tree control
4 ## Author: Simon Flack
5 ## Modified by: $Author: simonf $ on $Date: 2005/08/30 14:05:10 $
6 ## Created: 27/08/2004
7 ## RCS-ID: $Id: VirtualDirSelector.pm,v 1.8 2005/08/30 14:05:10 simonf Exp $
8 #############################################################################
9  
10 package Wx::Perl::VirtualDirSelector;
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 0 ▶use Wx qw/:id :misc :sizer :dialog/;
Subroutine BEGIN: called CC=1 SCAR=2.6
13 use Wx::Event 'EVT_UPDATE_UI';
14 use Wx::Perl::VirtualTreeCtrl 'EVT_POPULATE_TREE_ITEM';
15 use Exporter;
16 use Carp;
17  
18 use vars qw($VERSION @ISA);
19  
20 @ISA = qw(Exporter Wx::Dialog);
21 $VERSION = sprintf'%d.%03d', q$Revision: 1.8 $ =~ /: (\d+)\.(\d+)/;
22  
23 sub new {
24 my $class = shift;
25 my ($parent, $id, $dir_populator, $message, $root_data, $style, $pos) = @_;
26 croak("VirtualDirSelector: Invalid window id") unless $id;
27 croak("VirtualDirSelector: Invalid populator") unless $dir_populator;
28 $parent ||= Wx::wxTheApp->GetTopWindow;
29 $message ||= "Select a directory";
30 $root_data ||= "";
31 $style ||= wxDEFAULT_DIALOG_STYLE;
32 $pos ||= wxDefaultPosition;
33  
34 # Set up the dialog
35 my $self = $class->SUPER::new($parent, $id, '', $pos, wxDefaultSize, $style);
36 $self->_load_dialog($message);
37 $self->SetImageList(_image_list());
38  
39 # Add the custom populator
40 EVT_POPULATE_TREE_ITEM($self, $self->{tree}, $dir_populator);
41 my $root = $self->{tree}->AddRoot('/');
42 $self->{tree}->SetPlData($root, $root_data);
43 $self->{tree}->SetItemHasChildren($root, 1);
44 $self->SetRootItemSelectable(1);
45  
46 return $self;
47 }
48  
49 sub ExpandRoot {
50 my $self = shift;
51 my $root = $self->{tree}->GetRootItem;
52 $self->{tree}->Expand($root);
53 }
54  
55 sub SetRootItemSelectable {
56 my $self = shift;
57 my ($is_selectable) = @_;
58 $self->{root_item_selectable} = !!$is_selectable;
59 }
60  
61 sub SetRootLabel {
62 my $self = shift;
63 my ($label) = @_;
64 my $root = $self->{tree}->GetRootItem;
65 $self->{tree}->SetItemText($root, $label);
66 }
67  
68 sub SetRootImage {
69 my $self = shift;
70 my ($image, $which) = @_;
71 my $root = $self->{tree}->GetRootItem;
72 $self->{tree}->SetItemImage($root, $image, $which);
73 }
74  
75 sub SetImageList {
76 my $self = shift;
77 my ($image_list) = @_;
78 $self->{image_list} = $image_list;
79 $self->{tree}->SetImageList($self->{image_list});
80 }
81  
82 sub GetSelection {
83 my $self = shift;
84 my $selection = $self->{tree}->GetSelection();
85 return $self->{tree}->GetPlData($selection);
86 }
87  
88 sub GetSelectedPath {
89 my $self = shift;
90 my $selection = $self->{tree}->GetSelection();
91 return $self->{tree}->GetPath($selection);
92 }
93  
94 sub _load_dialog {
95 my $self = shift;
96 my ($label) = @_;
97  
98 $self->SetTitle('Browse for folder');
99 $self->SetSize([318,308]);
100  
101 my $dialog_message = new Wx::StaticText(
102 $self, -1, $label, wxDefaultPosition, [-1,40]
103 );
104  
105 $self->{tree} = new Wx::Perl::VirtualTreeCtrl(
106 $self, -1, wxDefaultPosition, [300,180]
107 );
108  
109 my $sizer = new Wx::BoxSizer(wxVERTICAL);
110 $sizer->Add($dialog_message, 0, wxALL, 5);
111 $sizer->Add($self->{tree}->GetTree, 0, wxALL, 5);
112  
113 my $btn_sizer = new Wx::BoxSizer(wxHORIZONTAL);
114 $btn_sizer->Add(new Wx::Button($self, wxID_OK, 'OK'), 0, wxALL, 2);
115 $btn_sizer->Add(new Wx::Button($self, wxID_CANCEL, 'Cancel'), 0, wxALL, 2);
116  
117 $sizer->Add($btn_sizer, 0, wxALL|wxALIGN_RIGHT, 5);
118  
119 $self->SetSizer($sizer);
120 $self->Layout;
121  
122 EVT_UPDATE_UI($self, wxID_OK, \&_on_update_ui);
123  
124 return $self;
125 }
126  
127 sub _on_update_ui {
128 my $self = shift;
129 my ($event) = @_;
130  
131 my $enable = 0;
132 if ($self->{root_item_selectable}) {
133 $enable = 1 if $self->{tree}->GetSelection();
134 } else {
135 my $root = $self->{tree}->GetRootItem();
136 $enable = 1 unless $self->{tree}->IsSelected($root);
137 }
138 $event->Enable($enable);
139 }
140  
141 my $icon;
142 sub _icon {
143 return $icon if $icon;
144 $icon = [ map { m/^"(.*)"/ ? ( $1 ) : () } <DATA> ];
145 }
146  
147 sub _image_list {
148 my $image_list = new Wx::ImageList(16,16);
149 $image_list->Add(Wx::Icon -> newFromXPM(_icon()));
150 return $image_list;
151 }
152  
153 1;
154  
155 =pod
156  
157 =head1 NAME
158  
159 Wx::Perl::VirtualDirSelector - A "virtual" clone of the standard DirSelector
160  
161 =head1 DERIVED FROM
162  
163 Wx::Dialog
164  
165 =head1 SYNOPSIS
166  
167 use Wx::Perl::VirtualDirSelector;
168  
169 my $dirsel = new Wx::Perl::VirtualDirSelector(
170 undef, -1, \&OnDirPopulate, 'Select a folder', 'c:\\'
171 );
172  
173 $dirsel->SetRootLabel('/');
174 $dirsel->ExpandRoot();
175  
176 if ($dirsel->ShowModal() == wxID_OK) {
177 Wx::MessageBox(sprintf "you selected '%s'", $dirsel->GetSelection);
178 }
179  
180  
181 =head2 DESCRIPTION
182  
183 A clone of the standard windows DirSelector (wxDirSelector()), driven by a
184 Wx::Perl::VirtualTreeCtrl.
185  
186 =head2 METHODS
187  
188 =over 4
189  
190 =item new ($parent, $id, \E<amp>dir_populator, $message, $root_data, $style,
191 $pos)
192  
193 Returns a virtual dir selector (C<@ISA Wx::Dialog>)
194  
195 $parent - parent window (default: TopWindow)
196 $id - window id
197 \&dir_populator - coderef to populate virtual tree control
198 $message - Selection prompt (default: 'Select a directory')
199 $root_data - Data assigned to the root item (default: '')
200 $style - dialog style (default: wxDEFAULT_DIALOG_STYLE)
201 $pos - dialog position (default: wxDefaultPosition)
202  
203 C<E<amp>dir_populator> is a Wx::Perl::VirtualTreeCtrl event callback.
204  
205 =item ExpandRoot()
206  
207 Expand the root Item
208  
209 =item GetSelection()
210  
211 Returns the data for the selected tree item.
212  
213 =item GetSelectedPath()
214  
215 Returns a list of path elements from the root node to the selected node.
216  
217 See the documentation for C<Wx::Perl::VirtualTreeCtrl::GetPath> for more
218 information.
219  
220 =item SetImageList($wx_imagelist)
221  
222 Set a custom Wx::ImageList for the tree control
223  
224 =item SetRootItemSelectable(bool)
225  
226 If FALSE, the C<OK> button will be greyed-out when the root item is selected.
227  
228 Default: TRUE
229  
230 =item SetRootLabel($label)
231  
232 Set the root item's text label
233  
234 =item SetRootImage($image_id, [$which])
235  
236 Set the root item's image. See Wx::TreeCtrl::GetItemImage for an explanation of
237 the optional C<$which> parameter.
238  
239 =back
240  
241 =head2 SEE ALSO
242  
243 =over 4
244  
245 =item L<Wx::TreeCtrl>
246  
247 The standard tree control from which this object is derived.
248  
249 =item L<Wx::Perl::VirtualTreeCtrl>
250  
251 A tree control that is populated dynamically.
252  
253 =item wxWidgets
254  
255 L<http://www.wxwidgets.org>
256  
257 =item wxPerl
258  
259 L<http://wxperl.sourceforge.net>
260  
261 =back
262  
263 =head2 AUTHOR
264  
265 Simon Flack <cpan _at_ bbc _dot_ co _dot_ uk>
266  
267 =head1 COPYRIGHT
268  
269 (c) BBC 2005. This program is free software; you can redistribute it and/or modify it under the GNU GPL.
270  
271 See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt
272  
273 =cut
274  
275 __DATA__
↑ Summary VirtualTreeCtrl.pm »