« Return to Thread: emit_stop_by_name

Re: emit_stop_by_name

by kksou :: Rate this Message:

Reply to Author | View in Thread

Steph Fox wrote:
What happens if you simply have the function/method return TRUE where you
would've called emit_stop? This should propagate, so long as it's an event
signal you're dealing with.

That technique seems to have been used successfully in place of both
emit_stop_by_name and its newer cousin throughout the GTK+ source, so
depending on the nature of the signal you're dealing with, it could be worth
a try.
Hi Steph,

Yes, return TRUE works for most of the signals. But when it comes to 'insert-text' for GtkEntry. It doesn't seem to work. Here's an example.

Scenario: To set up a GtkEntry that accepts only hexadecimal numbers.

With php-gtk1, the code is short and sweet with the use of emit_stop_by_name()

<?php
if( !extension_loaded('gtk')) dl( 'php_gtk.' . PHP_SHLIB_SUFFIX);

$window = &new GtkWindow();
$window->connect('destroy', array('Gtk','main_quit'));

$entry = &new GtkEntry();
$entry->connect('insert-text', 'on_insert');

$window->add($entry);
$window->show_all();
Gtk::main();

function on_insert($widget, $text, $len, $position) {
        $text = substr($text, 0, $len);
        if (!preg_match("/[0-9a-fA-F]/", $text)) {
                # not hexadecimal number - just gobble it up
                $widget->emit_stop_by_name('insert-text');
        }
}
?>

With php-gtk2, return TRUE doesn't work as expected. Thought it would stop the signal propagation. But the non-hexadecimal character still gets displayed in GtkEntry.

<?php
$window = new GtkWindow();
$window->set_size_request(400, 200);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->connect('destroy', array('Gtk','main_quit'));

$entry = new GtkEntry();
$entry->connect('insert-text', 'on_insert');

$window->add($entry);
$window->show_all();
Gtk::main();

function on_insert($widget, $text, $len, $position) {
        $text = substr($text, 0, $len);
        if (!preg_match("/[0-9a-fA-F]/", $text)) {
                echo "not hexadecimal number!\n";
                #$widget->emit_stop_by_name('insert-text');
                return true;
        }
        return false;
}

?>

I have done some trial and errors, and managed to get the same effect in php-gtk2 by capturing 'key-press-event' in $windows and then manually insert the characters into the GtkEntry. Though the effect is the same, the solution, if emit_stop_by_name() is available, would be more straightforward.

Regards,
/kksou

 « Return to Thread: emit_stop_by_name