File Coverage

blib/lib/Wx/Perl/BrowseButton.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package Wx::Perl::BrowseButton;
2              
3             =head1 NAME
4              
5             Wx::Perl::BrowseButton - a file/directory browse control
6              
7             =head1 SYNOPSIS
8              
9             use Wx::Perl::BrowseButton qw(:everything);
10              
11             my $browse = Wx::Perl::BrowseButton->new
12             ( $parent, $id, '/home/mbarbon', $position, $size, wxPL_BROWSE_FILE,
13             $validator );
14             $browse->SetPath( 'C:\\Program Files' );
15             my $path = $browse->GetPath
16              
17             EVT_PL_BROWSE_PATH_CHANGED( $handler, $browse->GetId, sub {
18             my( $self, $event ) = @_;
19             print 'New path: ', $event->GetPath;
20             };
21              
22             =head1 DESCRIPTION
23              
24             This simple control displays a text input field asociated with a browse
25             button. The user can either type the path inside the text field or click
26             the browse button to open a file/directory browser.
27              
28             The control sends a 'path changed' event when either the path is set using
29             the browse dialog or the control loses focus after the contents of the input
30             field have been changed using the keyboard.
31              
32             =cut
33              
34 1     1   867 use strict;
  1         1  
  1         34  
35 1     1   523 use Wx 0.26;
  0            
  0            
36             use Wx qw(:filedialog :dirdialog wxID_OK wxDefaultValidator);
37             use Wx::Locale qw(:default);
38             use Wx::Event qw(EVT_BUTTON EVT_KILL_FOCUS);
39             use File::Spec 0.80;
40              
41             use base qw(Wx::PlWindow Exporter);
42              
43             our $VERSION = 0.01;
44             our @EXPORT_OK = qw(wxPL_BROWSE_DIR wxPL_BROWSE_FILE
45             EVT_PL_BROWSE_PATH_CHANGED);
46             our %EXPORT_TAGS = ( 'everything' => \@EXPORT_OK,
47             'event' => [ qw(EVT_PL_BROWSE_PATH_CHANGED) ],
48             );
49              
50             =head1 CONSTANTS
51              
52             =over 4
53              
54             =item wxPL_BROWSE_DIR
55              
56             browse for a directory
57              
58             =item wxPL_BROWSE_FILE browse for a file
59              
60             =back
61              
62             =cut
63              
64             sub wxPL_BROWSE_DIR() { 0x4000 }
65             sub wxPL_BROWSE_FILE() { 0x2000 }
66              
67             my $mask_out = ~( wxPL_BROWSE_DIR | wxPL_BROWSE_FILE );
68              
69             =head1 EVENTS
70              
71             =over 4
72              
73             =item EVT_PL_BROWSE_PATH_CHANGED( $handler, $id, $function )
74              
75             Called when the path is changed using the browse button or the path
76             is typed directly in the input field and the field loses focus.
77              
78             =back
79              
80             =cut
81              
82             my $evt_change = Wx::NewEventType;
83              
84             sub EVT_PL_BROWSE_PATH_CHANGED($$$) { $_[0]->Connect( $_[1], -1, $evt_change, $_[2] ) }
85              
86             =head1 METHODS
87              
88             =head2 new
89              
90             my $browse = Wx::Perl::BrowseButton->new
91             ( $parent, $id, $initial_path, $position, $size, $style, $validator );
92              
93             Creates a new browse button.
94              
95             =cut
96              
97             sub new {
98             my( $class, $parent, $id, $path, $pos, $size, $style, $validator ) = @_;
99             my $self = $class->SUPER::new( $parent, $id, $pos || [-1, -1],
100             $size || [-1, -1], 0 );
101              
102             $self->SetValidator( $validator || wxDefaultValidator );
103              
104             $self->{style} = $style;
105             $self->{input} = Wx::TextCtrl->new( $self, -1, $path );
106             $self->{browse} = Wx::Button->new( $self, -1, gettext( "&Browse" ) );
107             $self->{old_path} = '';
108              
109             EVT_BUTTON( $self, $self->{browse}, \&_OnBrowse );
110             EVT_KILL_FOCUS( $self->{input}, \&_OnFocus );
111              
112             return $self;
113             }
114              
115             sub DoMoveWindow {
116             my( $self, $x, $y, $w, $h ) = @_;
117             my $browse_x = $w - $self->{browse}->GetSize->x;
118              
119             $self->SUPER::DoMoveWindow( $x, $y, $w, $h );
120             $self->{browse}->Move( $browse_x, 0 );
121             $self->{input}->SetSize( 0, 0, $browse_x - 5, -1 );
122             }
123              
124             sub DoGetBestSize {
125             my( $self ) = @_;
126             my( $bro_bs, $in_bs ) = map { $_->GetBestSize } @{$self}{qw(browse input)};
127              
128             return Wx::Size->new( $bro_bs->x + $in_bs->x + 5,
129             $bro_bs->y > $in_bs->y ? $bro_bs->y : $in_bs->y );
130             }
131              
132             sub Enable {
133             my( $self, $enable ) = @_;
134              
135             $self->{browse}->Enable( $enable );
136             $self->{input}->Enable( $enable );
137              
138             return $self->SUPER::Enable( $enable );
139             }
140              
141             =head2 GetPath
142              
143             my $path = $browse->GetPath;
144              
145             Returns the path currently displayed in the input field.
146              
147             =head2 SetPath
148              
149             $browse->SetPath( $path );
150              
151             Sets the path displayed in the input field. It does not send a 'path changed'
152             event.
153              
154             =cut
155              
156             sub GetPath {
157             my $self = shift;
158              
159             return $self->{input}->GetValue;
160             }
161              
162             sub SetPath {
163             my( $self, $value ) = @_;
164              
165             $self->{input}->SetValue( $value );
166             $self->{old_path} = $value;
167             }
168              
169             sub _OnFocus {
170             my( $input, $event ) = @_;
171             my $self = $input->GetParent;
172              
173             if( $self->GetPath ne $self->{old_path} ) {
174             my $event =
175             Wx::Perl::BrowseButton::Event->new( $evt_change, $self->GetId );
176              
177             $event->SetPath( $self->GetPath );
178             $self->GetEventHandler->ProcessEvent( $event );
179             $self->{old_path} = $self->GetPath;
180             }
181             }
182              
183             sub _OnBrowse {
184             my( $self, $event ) = @_;
185             my( $dir, $file ) = ( '', '' );
186              
187             if( length $self->GetPath ) {
188             if( $self->{style} & wxPL_BROWSE_DIR ) {
189             $dir = $self->GetPath;
190             } else {
191             my( $v, $d, $f ) =
192             File::Spec->splitpath( $self->GetPath );
193             $file = $f;
194             $dir = File::Spec->catpath( $d, $f, '' );
195             }
196             }
197              
198             my $dialog;
199              
200             if( $self->{style} & wxPL_BROWSE_DIR ) {
201             $dialog = Wx::DirDialog->new( $self, gettext( "Choose a directory" ),
202             $dir, $self->{style} & $mask_out );
203             } else {
204             $dialog = Wx::FileDialog->new( $self, gettext( "Choose a file" ),
205             $dir, $file,
206             wxFileSelectorDefaultWildcardStr,
207             $self->{style} & $mask_out );
208             }
209              
210             if( $dialog->ShowModal == wxID_OK ) {
211             my $event =
212             Wx::Perl::BrowseButton::Event->new( $evt_change, $self->GetId );
213              
214             $self->SetPath( $dialog->GetPath );
215             $event->SetPath( $dialog->GetPath );
216             $self->GetEventHandler->ProcessEvent( $event );
217             }
218             }
219              
220             package Wx::Perl::BrowseButton::Event;
221              
222             use strict;
223             use base qw(Wx::PlCommandEvent);
224              
225             sub new {
226             my( $class, $type, $id ) = @_;
227             my $self = $class->SUPER::new( $type, $id );
228              
229             return $self;
230             }
231              
232             sub SetPath { $_[0]->{path} = $_[1] }
233             sub GetPath { $_[0]->{path} }
234              
235             1;
236              
237             __END__