Re: Testing text handler for a Gtk3::Entry



On 22.10.19 19:31, Jeff via gtk-perl-list wrote:
$dialog->{'entry-widget'}->signal_emit('insert-text', '-', 1, 0);

This signal_emit() call is the problem.  The insert-text signal does
some special pointer magic, and signal_emit() is not set up to handle
this.  In this case, however, you can simply use the insert_text()
method of Gtk3::Editable.  Adapted code:

---
package MyPackage;

use warnings;
use strict;
use Gtk3 -init;
use Glib 1.220 qw(TRUE FALSE);    # To get TRUE and FALSE

sub new {
    my $self = Gtk3::Dialog->new;
    $self->{'entry-widget'} = Gtk3::Entry->new;
    $self->{'entry-widget'}->signal_connect(
        'insert-text' => \&insert_text_handler, $self );
    return $self
}

sub insert_text_handler {
    my ( $widget, $string, $len, $position, $self ) = @_;
    # only allow integers
    if ( $string !~ /^\d+$/smx ) {
        # stopping emission
        $widget->signal_stop_emission_by_name('insert-text');
    }
    return $position;
}

package main;
use warnings;
use strict;
use Test::More tests => 3;

ok( my $dialog = MyPackage->new,  'Created dialog' );

$dialog->{'entry-widget'}->insert_text ('-', 0);
is( $dialog->{'entry-widget'}->get_text, '', 'ignored -' );

$dialog->{'entry-widget'}->insert_text ('23', 0);
is( $dialog->{'entry-widget'}->get_text, '23', 'accepted 23' );
---


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