Commit 29f0f78e by PotatoGim

Wx::PlValidator 예제 동작하도록 수정

parent 6d749c5b
=begin comment
Dear Mark
Thank you for your prompt reply. This program I'm running is not small
(~580 lines), and I didn't want to clog the forum.
I any case, I went through the program now, and sifted out the irrelevant
parts ( irrelevant for this question) (even so it's ~140 lines). Here it is
- it's runnable.
Please note the following: the program contains 4 packages, the relevant
ones are "TC_test" and "MyValidator".
The program logic (trivially simple) goes like this: package "main" calls
package "MyForm", which displays a frame. Clicking the button "select_btn"
calls sub "prep_query", which calls package "TC_test".
package "TC_test" creates a text control called "$TCtrl", to which
"TCtrl_v": a "MyValidator" validator is attached.
Like I said in the OP, before leaving sub "new TC_test" it tries to
executes:
$TCtrl_v->TransferToWindow;
and breaks saying: "Can't call method "SetValue" on an undefined value at
... .pl line 150."
Many TIA
Helen
=end comment
=cut
use strict;
use warnings;
use Wx;
package MyForm;
use strict;
use warnings;
use parent -norequire, qw/Wx::Frame/;
use Wx::Event qw/EVT_BUTTON/;
use Wx qw/:everything/;
sub new
{
my $class = shift;
my $self = $class->SUPER::new(undef, -1, 'Frame class: MyForm', wxDefaultPosition, wxDefaultSize);
my $panel = Wx::Panel->new($self, -1, wxDefaultPosition, wxDefaultSize);
my $select_btn = Wx::Button->new($panel, -1, 'DB Select query', wxDefaultPosition, wxDefaultSize);
EVT_BUTTON($self, $select_btn, sub { prep_query(); });
my $sizer = Wx::BoxSizer->new(wxVERTICAL);
my $sizer1_h = Wx::BoxSizer->new(wxHORIZONTAL);
$sizer1_h->Add($select_btn, 0, wxALL, 5);
$sizer->Add($sizer1_h, 1, wxEXPAND);
$panel->SetSizer($sizer);
$panel->Fit;
return $self;
}
sub prep_query
{
my $select_query_frame = TC_test->new;
$select_query_frame->Show(1);
}
1;
package TC_test;
use strict;
use warnings;
use Wx qw/wxDefaultPosition wxDefaultSize wxTE_PROCESS_ENTER/;
use Wx::Event qw/EVT_BUTTON EVT_CLOSE/;
use parent -norequire, qw/Wx::Frame/;
sub new
{
my $class = shift;
my $self = $class->SUPER::new(undef, -1, 'Frame class: TC_test', wxDefaultPosition, wxDefaultSize);
my $panel2 = Wx::Panel->new($self, -1, wxDefaultPosition, wxDefaultSize);
my $TCtrl = Wx::TextCtrl->new($panel2, -1, q{}, wxDefaultPosition, [20,-1], wxTE_PROCESS_ENTER);
my $TCtrl_v = MyValidator->new(\$main::text_value);
$TCtrl->SetValidator($TCtrl_v);
my $sizer_main = Wx::FlexGridSizer->new(7, 7, 5, 5);
$sizer_main->Add($TCtrl, 0);
$sizer_main->Fit($self);
$sizer_main->SetSizeHints($self);
EVT_CLOSE($self, sub {
if (not $TCtrl->GetValidator->Validate)
{
Wx::LogMessage("Data is invalid");
return;
}
if (not $TCtrl->GetValidator->TransferFromWindow)
{
Wx::LogMessage("Error in data transfer");
return;
}
$self->Wx::Window::Destroy;
});
$TCtrl->GetValidator->TransferToWindow;
return $self;
}
1;
package MyValidator;
use strict;
use Wx qw/wxOK wxICON_ERROR/;
use parent -norequire, qw/Wx::PlValidator/;
sub new
{
my ($class, $var) = shift;
my $self = $class->SUPER::new;
if ($var && $var->isa($class))
{
$$self{$_} = $$var{$_} foreach (keys %$var);
}
else
{
$$self{var} = $var;
}
return $self;
}
sub Clone
{
my $self = shift;
my $class = ref($self);
return $class->new($self);
}
sub Validate
{
my ($self, $window) = @_;
my $V_var = $self->GetWindow->GetValue;
if ($V_var !~ /^[a-zA-Z ]+$/)
{
Wx::MessageBox("Error: $V_var is not proper value.", "Error", wxOK|wxICON_ERROR, undef);
return 0;
}
1;
}
sub TransferFromWindow
{
my $self = shift;
${$self->{var}} = $self->GetWindow->GetValue;
1;
}
sub TransferToWindow
{
my $self = shift;
$self->GetWindow->SetValue(${$self->{var}}) if defined($self->{var});
1;
}
1;
package main;
use strict;
use warnings;
$main::text_value = 'FOO BAR';
my $app = Wx::SimpleApp->new;
my $frame = MyForm->new;
$frame->Show(1);
$app->MainLoop;
1;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment