Wx::Validator

From WxPerl Wiki
Jump to: navigation, search

This was originally posted to wxperl-users by Mattia Barbon on 24 April 2004; I (John Ralls) have made a couple of modifications to a) not put the package in Wx::Perl, b) make the package name a little more normal for examples, c) change functions new and Clone to make it reusable by derived classes and independent of the package name, and d) add the very important return values to TransferFromWindow and TransferToWindow.

package MyValidator;

use strict;
use Wx;
use base 'Wx::PlValidator';

sub new {
    my ($class, $var) = @_;
    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 $validator = $self->validator;

    # check for validity and return true or false
    return 1;
}

sub TransferFromWindow {
    my $self = shift;

    ${$self->{var}} = $self->GetWindow->GetValue;
    return 1;
}

sub TransferToWindow {
    my $self = shift;

    $self->GetWindow->SetValue( ${$self->{var}} );
    return 1;
}

1;

# sample use
use MyValidator;
my $var;

$win->SetValidator(MyValidator->new( \$var ) );