File Coverage

blib/lib/Wx/DemoModules/wxValidator.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             #############################################################################
2             ## Name: lib/Wx/DemoModules/wxValidator.pm
3             ## Purpose: wxPerl demo helper
4             ## Author: Mattia Barbon
5             ## Modified by:
6             ## Created: 15/08/2005
7             ## RCS-ID: $Id: wxValidator.pm 2189 2007-08-21 18:15:31Z mbarbon $
8             ## Copyright: (c) 2005-2006 Mattia Barbon
9             ## Licence: This program is free software; you can redistribute it and/or
10             ## modify it under the same terms as Perl itself
11             #############################################################################
12              
13             package Wx::DemoModules::wxValidator;
14              
15 1     1   1397 use strict;
  1         3  
  1         85  
16 1     1   6 use base qw(Wx::Panel Class::Accessor::Fast);
  1         2  
  1         743  
17              
18             use Wx::Event qw(EVT_BUTTON);
19             use Wx::Perl::TextValidator;
20              
21             __PACKAGE__->mk_ro_accessors( qw(numeric string) );
22              
23             sub new {
24             my $class = shift;
25             my $self = $class->SUPER::new( $_[0], -1 );
26              
27             # filtered text controls
28             my $numval = Wx::Perl::TextValidator->new( '\d' );
29             my $charval = Wx::Perl::TextValidator->new( qr/[a-zA-z ]/ );
30              
31             Wx::StaticText->new( $self, -1, 'Type numbers', [10, 10] );
32             my $t1 = $self->{numeric} = Wx::TextCtrl->new( $self, -1, '', [10, 30] );
33             Wx::StaticText->new( $self, -1, 'Type spaces/letters', [10, 60] );
34             my $t2 = $self->{string} = Wx::TextCtrl->new( $self, -1, '', [10, 80] );
35              
36             $t1->SetValidator( $numval );
37             $t2->SetValidator( $charval );
38              
39             EVT_BUTTON( $self, Wx::Button->new( $self, -1, 'Validator and dialog',
40             [10, 120] ),
41             sub { Wx::DemoModules::wxValidator::Dialog
42             ->new( $self )->ShowModal } );
43             EVT_BUTTON( $self, Wx::Button->new( $self, -1, 'Validator and frame',
44             [150, 120] ),
45             sub { Wx::DemoModules::wxValidator::Frame
46             ->new( $self )->Show( 1 ) } );
47              
48             return $self;
49             }
50              
51             sub add_to_tags { qw(misc) }
52             sub title { 'wxValidator' }
53              
54             package Wx::DemoModules::wxValidator::Validator;
55              
56             use strict;
57             use base qw(Wx::Perl::TextValidator);
58              
59             # trivial class, just to log method calls
60             sub Validate {
61             my $self = shift;
62              
63             Wx::LogMessage( "In Validate(): data is '%s'",
64             $self->GetWindow->GetValue );
65              
66             return $self->SUPER::Validate( @_ );
67             }
68              
69             sub TransferFromWindow {
70             my $self = shift;
71              
72             Wx::LogMessage( "In TransferFromWindow(): data is '%s'",
73             $self->GetWindow->GetValue );
74              
75             return $self->SUPER::TransferFromWindow( @_ );
76             }
77              
78             sub TransferToWindow {
79             my $self = shift;
80              
81             # peeking at internals; naughty me...
82             Wx::LogMessage( "In TransferToWindow(): data is '%s'",
83             ${$self->{data}} );
84              
85             return $self->SUPER::TransferToWindow( @_ );
86             }
87              
88             package Wx::DemoModules::wxValidator::Dialog;
89              
90             use strict;
91             use base qw(Wx::Dialog);
92              
93             use Wx qw(wxID_OK wxID_CANCEL);
94              
95             sub new {
96             my( $class, $parent ) = @_;
97             my $self = $class->SUPER::new( $parent, -1, 'Dialog' );
98              
99             $self->{data} = $parent->numeric->GetValue;
100              
101             # simple numeric validator
102             my $numval = Wx::DemoModules::wxValidator::Validator
103             ->new( '\d', \($self->{data}) );
104              
105             Wx::StaticText->new( $self, -1, 'Type numbers', [10, 10] );
106             my $t1 = Wx::TextCtrl->new( $self, -1, '', [10, 30] );
107              
108             $t1->SetValidator( $numval );
109              
110             # the validation/data transfer phase are automatic for a
111             # dialog where the Ok button has ID wxID_OK, otherwise
112             # an explicit call to Validate/TransferDataFromWindow is required
113             # when closing the dialog
114             Wx::Button->new( $self, wxID_OK, "Ok", [10, 60] );
115             Wx::Button->new( $self, wxID_CANCEL, "Cancel", [100, 60] );
116              
117             return $self;
118             }
119              
120             sub get_data { $_[0]->{data} }
121              
122             package Wx::DemoModules::wxValidator::Frame;
123              
124             use strict;
125             use base qw(Wx::Frame);
126              
127             use Wx::Event qw(EVT_BUTTON);
128              
129             sub new {
130             my( $class, $parent ) = @_;
131             my $self = $class->SUPER::new( $parent, -1, 'Frame' );
132              
133             $self->{data} = $parent->string->GetValue;
134              
135             my $strval = Wx::DemoModules::wxValidator::Validator
136             ->new( '[a-zA-Z ]', \($self->{data}) );
137              
138             Wx::StaticText->new( $self, -1, 'Type spaces/letters', [10, 10] );
139             my $t1 = Wx::TextCtrl->new( $self, -1, '', [10, 30] );
140              
141             $t1->SetValidator( $strval );
142              
143             EVT_BUTTON( $self, Wx::Button->new( $self, -1, "Ok", [10, 60] ),
144             sub {
145             if( !$self->Validate ) {
146             Wx::LogMessage( "Data is invalid" );
147             return;
148             }
149             if( !$self->TransferDataFromWindow ) {
150             Wx::LogMessage( "Error in data transfer" );
151             return;
152             }
153             $self->Destroy;
154             } );
155              
156             EVT_BUTTON( $self, Wx::Button->new( $self, -1, "Cancel", [100, 60] ),
157             sub {
158             $self->Destroy;
159             } );
160              
161             $self->TransferDataToWindow;
162              
163             return $self;
164             }
165              
166             sub get_data { $_[0]->{data} }
167              
168             1;