Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gtk entry set_position doesn't move the cursor

Tags:

perl

gtk

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;
like image 869
ophidion Avatar asked Oct 28 '25 23:10

ophidion


1 Answers

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 );

like image 75
Phillip Wood Avatar answered Oct 30 '25 12:10

Phillip Wood



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!