Why this piece of code doesn't move the cursor to the last character of the entry?
use Gtk2 '-init';
my $window = Gtk2::Window->new;
my $entry = Gtk2::Entry->new;
my $handler;
$handler = $entry->signal_connect ( 'insert-text' =>
    sub {
        my (
            $entry,
            $chrs,
            $len,
            $pos_strange,
            $handler,
        ) = @_;
        $entry->signal_handler_block ( $$handler );
        $entry->set_text ( 'tryme' );
        $entry->signal_stop_emission_by_name ('insert-text');
        $entry->signal_handler_unblock ( $$handler );
        $entry->set_position ( -1 );
    },
    \$handler
);
$window->set_size_request ( 200, 100 );
$window->add ( $entry );
$window->set_position ('center_always');
$window->show_all;
$window->signal_connect ( delete_event => sub { Gtk2->main_quit; } );
Gtk2->main;
When the last insert-text handler returns Gtk updates the cursor position using the value of pos so your code is moving the cursor and then Gtk is moving it again. You could use an idle callback as suggested above but it would probably be cleaner to update pos in your handler. You want to do the perl equivalent of *pos = g_utf8_strlen ("tryme"), 0) instead of $entry->set_position ( -1 );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With