Re: IP address input



On Wed, 20 Dec 2006 16:11:05 +0700
beast <beast ldap or id> wrote:


I have a text entry which was require user to type an IP address. 
Ideally it should have this following format:

IP Address:   [     .       .      .     ]

Please note that "." is not user editable. Is it possible?

I got this idea from the ActiveState-Gtk2-cookbook.
This has only been moderately tested, and probably
needs a bit more tuning, but it's a start.

#!/usr/bin/perl
use warnings;
use strict;
use Gtk2 -init;

my $window = new Gtk2::Window;
$window->signal_connect (delete_event => sub { Gtk2->main_quit }); 

my $entry = Gtk2::Entry->new_with_max_length (15);

# we store the signal connection id in the entry for later
$entry->{signalid} = $entry->signal_connect( insert_text => \&validate );

$window->add($entry);

$window->set_focus_child($entry); # works 

$window->show_all;
Gtk2->main;

sub validate {
    my ( $entry, $text, $len, $pos ) = @_;
    print "@_\n";

    if ( $text =~ /\d/ ) {
        if(($pos == 3)||($pos == 7)||($pos == 11)){ $text = '.' }
    
        # we temporarily block this handler to avoid recursion
        $entry->signal_handler_block( $entry->{signalid} );
        $pos = $entry->insert_text( $text, $pos );
        $entry->signal_handler_unblock( $entry->{signalid} );
    }

    # we already inserted the text if it was valid: no need
    # for the entry to process this signal emission
     $entry->signal_stop_emission_by_name('insert-text');

     return '',$pos;  #
}
__END__



-- 
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]