| 1 | package Wx::Perl::DirTree; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use Exporter; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 0 | ▶ | use Wx qw( |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | wxOK wxID_ABOUT wxID_EXIT wxICON_INFORMATION wxTOP wxVERTICAL | ||
| 10 | wxNO_FULL_REPAINT_ON_RESIZE wxSYSTEM_MENU wxCAPTION wxMINIMIZE_BOX | ||
| 11 | wxCLOSE_BOX wxDefaultPosition | ||
| 12 | 0 | ); | |
| 13 | |||
| 14 | use Wx::Event qw( | ||
| 15 | EVT_MENU EVT_CLOSE EVT_SIZE EVT_UPDATE_UI EVT_KEY_DOWN | ||
| 16 | EVT_TREE_SEL_CHANGING EVT_TREE_SEL_CHANGED | ||
| 17 | ); | ||
| 18 | |||
| 19 | use Wx::Perl::VirtualTreeCtrl qw(EVT_POPULATE_TREE_ITEM); | ||
| 20 | |||
| 21 | our $VERSION = 0.07; | ||
| 22 | |||
| 23 | our @ISA = qw(Exporter); | ||
| 24 | |||
| 25 | our @EXPORT_OK = qw(wxPDT_DIR wxPDT_FILE); | ||
| 26 | our %EXPORT_TAGS = ( | ||
| 27 | 'const' => \@EXPORT_OK, | ||
| 28 | ); | ||
| 29 | |||
| 30 | use constant wxPDT_DIR => 2; | ||
| 31 | use constant wxPDT_FILE => 4; | ||
| 32 | |||
| 33 | sub new { | ||
| 34 | my ($class,$parent,$size,$args) = @_; | ||
| 35 | |||
| 36 | my $self = bless {}, $class; | ||
| 37 | |||
| 38 | _load_subs(); | ||
| 39 | $self->_tree( $parent, $size, $args ); | ||
| 40 | |||
| 41 | return $self; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub _tree { | ||
| 45 | my ($self, $parent, $size, $args) = @_; | ||
| 46 | |||
| 47 | if( !$self->{tree} and $parent and $size ){ | ||
| 48 | $self->{treectrl} = Wx::TreeCtrl->new( | ||
| 49 | $parent, -1, wxDefaultPosition, $size | ||
| 50 | ); | ||
| 51 | |||
| 52 | $self->{tree} = Wx::Perl::VirtualTreeCtrl->new( | ||
| 53 | $self->{treectrl}, -1, wxDefaultPosition, $size | ||
| 54 | ); | ||
| 55 | |||
| 56 | EVT_POPULATE_TREE_ITEM( $parent, $self->{tree}, \&AddChildren ); | ||
| 57 | |||
| 58 | # if user wants to restrict the items allowed to be selected | ||
| 59 | # add another event handler | ||
| 60 | if ( exists $args->{allowed} ) { | ||
| 61 | EVT_TREE_SEL_CHANGING( $parent, $self->{tree}->GetTree, sub{ | ||
| 62 | my ($self,$event) = @_; | ||
| 63 | CheckSelection( $event, $args->{allowed} ); | ||
| 64 | } ); | ||
| 65 | } | ||
| 66 | |||
| 67 | add_root( $self->{tree}, $args ); | ||
| 68 | } | ||
| 69 | |||
| 70 | return $self->{tree}; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub CheckSelection { | ||
| 74 | my ($event,$allowed) = @_; | ||
| 75 | |||
| 76 | my $tree = $event->GetEventObject; | ||
| 77 | my $item = $event->GetItem; | ||
| 78 | my $data = $tree->GetPlData( $item ); | ||
| 79 | |||
| 80 | return if $allowed & wxPDT_FILE && $allowed & wxPDT_DIR; | ||
| 81 | |||
| 82 | if ( ( $allowed & wxPDT_FILE ) && -d $data ) { | ||
| 83 | $event->Veto; | ||
| 84 | } | ||
| 85 | if ( ( $allowed & wxPDT_DIR ) && -f $data ) { | ||
| 86 | $event->Veto; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | sub GetTree { | ||
| 91 | my ($self) = @_; | ||
| 92 | |||
| 93 | return $self->_tree->GetTree; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub GetSelectedPath { | ||
| 97 | my ($self) = @_; | ||
| 98 | |||
| 99 | my $tree = $self->_tree; | ||
| 100 | my $path = $tree->GetPlData( $tree->GetSelection ); | ||
| 101 | return $path; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub _load_subs { | ||
| 105 | my $os = $^O; | ||
| 106 | |||
| 107 | if( $os =~ /win32/i ){ | ||
| 108 | require Wx::Perl::DirTree::Win32; | ||
| 109 | Wx::Perl::DirTree::Win32->import(); | ||
| 110 | } | ||
| 111 | else{ | ||
| 112 | require Wx::Perl::DirTree::Linux; | ||
| 113 | Wx::Perl::DirTree::Linux->import(); | ||
| 114 | } | ||
| 115 | |||
| 116 | } | ||
| 117 | |||
| 118 | 1; | ||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | =pod | ||
| 123 | |||
| 124 | =head1 NAME | ||
| 125 | |||
| 126 | Wx::Perl::DirTree - A directory tree widget for wxPerl | ||
| 127 | |||
| 128 | =head1 VERSION | ||
| 129 | |||
| 130 | version 0.07 | ||
| 131 | |||
| 132 | =head1 SYNOPSIS | ||
| 133 | |||
| 134 | use Wx::Perl::DirTree; | ||
| 135 | |||
| 136 | my $panel = Wx::Panel->new; | ||
| 137 | my $tree = Wx::Perl::DirTree->new( $panel, [100,100] ); | ||
| 138 | |||
| 139 | my $main_sizer = Wx::BoxSizer->new( wxVERTICAL ); | ||
| 140 | $main_sizer->Add( $tree->GetTree, 0, wxTOP, 0 ); | ||
| 141 | |||
| 142 | # in a subroutine | ||
| 143 | print $tree->GetSelectedPath; | ||
| 144 | |||
| 145 | =head1 DESCRIPTION | ||
| 146 | |||
| 147 | Many widgets that display directory trees are dialogs or can't handle drives on | ||
| 148 | Windows. This module aims to fill the gap. It can be integrated in any frame or | ||
| 149 | dialog and it handles drives under Windows. | ||
| 150 | |||
| 151 | =head1 METHODS | ||
| 152 | |||
| 153 | =head2 GetSelectedPath | ||
| 154 | |||
| 155 | $tree->GetSelectedPath | ||
| 156 | |||
| 157 | This method returns the path of the item that is selected. | ||
| 158 | |||
| 159 | =head2 GetTree | ||
| 160 | |||
| 161 | $tree->GetTree | ||
| 162 | |||
| 163 | This is just a convenience method that wraps the GetTree method | ||
| 164 | of Wx::Perl::VirtualTree. | ||
| 165 | |||
| 166 | =head2 new | ||
| 167 | |||
| 168 | Creates a new object | ||
| 169 | |||
| 170 | my $tree = Wx::Perl::DirTree->new( $panel, [100,100] ); | ||
| 171 | |||
| 172 | my $tree2 = Wx::Perl::DirTree->new( | ||
| 173 | $panel, | ||
| 174 | [100,100], | ||
| 175 | { | ||
| 176 | dir => $path_to_dir, | ||
| 177 | } | ||
| 178 | ); | ||
| 179 | |||
| 180 | my $tree2 = Wx::Perl::DirTree->new( | ||
| 181 | $panel, | ||
| 182 | [100,100], | ||
| 183 | { | ||
| 184 | dir => $path_to_dir, | ||
| 185 | is_root => 1, | ||
| 186 | } | ||
| 187 | ); | ||
| 188 | |||
| 189 | Parameters: | ||
| 190 | |||
| 191 | =over 4 | ||
| 192 | |||
| 193 | =item 1 $parent | ||
| 194 | |||
| 195 | A parent widget. | ||
| 196 | |||
| 197 | =item 2 $size | ||
| 198 | |||
| 199 | The size of the tree widget. This has to be an array reference. | ||
| 200 | |||
| 201 | =item 3 $hashref | ||
| 202 | |||
| 203 | In this hash reference you can specifiy more parameters: | ||
| 204 | |||
| 205 | =over 4 | ||
| 206 | |||
| 207 | =item * dir | ||
| 208 | |||
| 209 | If you want to "open" a specific directory, you can specify "dir" | ||
| 210 | |||
| 211 | =item * is_root | ||
| 212 | |||
| 213 | If set to a true value, the dir tree starts at the specified directory. If you | ||
| 214 | want to provide a directory tree that shows only the directories below the | ||
| 215 | home directory of a user you can do this: | ||
| 216 | |||
| 217 | Wx::Perl::DirTree->new( | ||
| 218 | $panel, | ||
| 219 | $size, | ||
| 220 | { | ||
| 221 | dir => File::HomeDir->my_home, | ||
| 222 | is_root => 1, | ||
| 223 | } | ||
| 224 | ); | ||
| 225 | |||
| 226 | =item * allowed | ||
| 227 | |||
| 228 | With that option you can specify whether only directories or only files can | ||
| 229 | be selected. If this option is ommitted, both types can be selected. | ||
| 230 | |||
| 231 | use Wx::Perl::DirTree qw(:const); # loads two constants | ||
| 232 | |||
| 233 | my $tree = Wx::Perl::DirTree->new( | ||
| 234 | $panel, | ||
| 235 | $size, | ||
| 236 | { | ||
| 237 | dir => File::HomeDir->my_home, | ||
| 238 | allowed => wxPDT_DIR, # only directories can be selected | ||
| 239 | } | ||
| 240 | ); | ||
| 241 | |||
| 242 | =back | ||
| 243 | |||
| 244 | =back | ||
| 245 | |||
| 246 | See also the scripts in the example dir. | ||
| 247 | |||
| 248 | =head1 AUTHOR | ||
| 249 | |||
| 250 | Renee Baecker <module@renee-baecker.de> | ||
| 251 | |||
| 252 | =head1 COPYRIGHT AND LICENSE | ||
| 253 | |||
| 254 | This software is Copyright (c) 2010 by Renee Baecker. | ||
| 255 | |||
| 256 | This is free software, licensed under: | ||
| 257 | |||
| 258 | The Artistic License 2.0 | ||
| 259 | |||
| 260 | =cut | ||
| 261 | |||
| 262 | |||
| 263 | __END__ | ||