Modify user input in entry with GJS

I want that text inserted to a Gtk entry is only numeric (0…9). A behavior similar to GtkSpinButton with properties Numeric set to true.

Reading documentation, I’ve found that the right signal to connect is insert-text of GtkEditable interface:

by connecting to this signal and then stopping the signal with g_signal_stop_emission() , it is possible to modify the inserted text, or prevent it from being inserted entirely.

if I do

_onInsertText (entry, a, b, c) {
    print (entry.text);
    print (a);
    print (b);
    print (c);
    if (a =="a") {
	entry.stop_emission("insert-text");
    }

}

i get

Gjs-WARNING **: 16:12:19.334: JS ERROR: TypeError: entry.stop_emission is not a function

the same code written in Python3 works, so, is it stop_emission supported in GJS? what do I do wrong?

You probably want stop_emission_by_name("insert-text").

PyGObject has an override for that function, I assume for backward compatibility. It has been deprecated, with a warning telling you to use stop_emission_by_name(), so you should change your Python code as well.

1 Like

In addition to that, GJS doesn’t have an override to add that method to GObject.Object.prototype.

It sounds like a good idea to add that, but in the meantime the following will work:

    if (a == 'a')
        GObject.signal_stop_emission_by_name(entry, 'insert-text');

Update:
I created this merge request for adding the override.

4 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.