1
votes

I am writing an application using gtkmm 3 (running Ubuntu 12.04 LTS) and working right now with the Gtk::Entry control.

I cannot find the correct signal to capture so that I can grab the Gtk::Entry buffer text before it is changed, and persist it to maintain a record of changes. I know that in some other tool-kits, there is a hook provided that facilitates such. (I believe using a "shadow buffer".)

What signal do I have to grab to do this? What is the slot's signature for this signal? Is this functionality supported at all?

2

2 Answers

4
votes

Since you are changing the behaviour, it's better to inherit from Gtk::Entry:

class ValidatedEntry : public Gtk::Entry {

    Glib::ustring last_valid;

    virtual void on_changed()
    {
        Glib::ustring text = get_text();
        if (... validation here ...)
            set_text(last_valid); // WARNING: will call this on_changed() again
        else
            last_valid = text;
        Gtk::Entry::on_changed(); // propagate down
    }
};

BUT

This goes against usability, that's why it's not a built-in behaviour. Users won't like the text reverting back just because they miss-typed something; they might hit backspace before they realize the entry threw the wrong character away.

You should at least wait until the user presses the Enter key (i.e. signal_activate or override on_activate()), or do something less drastic, like showing a warning icon.

2
votes

You could give a try to GObject's "notify" signal. It is used in conjunction with the property to spy. Connecting to "notify::text" will call your callback for each modification of the "text" property, but the first change may be the setter that will set the initial value, that you could then store. Worth a try.

Otherwise, you could try to store it on the first triggering of the "insert-text" or "delete-text" signals. Please give use some feedback if that seems to work.

I also agree with DanielKO: on an usability point of view, modifying user input is just annoying and bad practice. Better to tell her which field is wrong, put the focus there, and/or have a button to reset to defaults, but not enforce any change on user input.