Re: Gtk::Editable::on_insert_text



[snip]

I needed a Gtk::Entry object that behaved the same way, so I derived a class I called "MaskedEntry." from Gtk::Entry. I added a gchar pointer as a protected class variable, and as a formal paramter to the contructor I passed a pointer to a gchar string containing all the characters that I wanted to mask out of the entry. The constructor makes a copy of this string with g_strdup. My class overrides the "on_key_press_event" such that it uses gdk_keyval_to_unicode on the event->keyval argument to convert it to a unicode value and then uses strchr to see if that character is in the mask string.

[snip]

But what happens when the user pastes something into the entry ? By overriding Entry::insert_text_vfunc(...) this is no problem because i always get the whole string which the user tries to insert ...

[snip]

If it is not, Gtk::Entry::on_key_press_event( event ) is called, passing the original argument along to the framework. If, however, the character does exist in the mask string, I put up a Gtk::MessageDialog informing the user that the input character is not allowed as valid entry and return without calling GtK::on_key_press_event. Because all this happens before any characters are inserted into the text of the entry, it is neat and clean.

[snip]

By the way, when i just whant to surpress a single character, this soloution may be a bit complicated. Here is basically what I have done:

class SmartEntry : public Gtk::Entry
{
   public:
   typedef sigc::slot<bool, const Glib::ustring&> SlotAccept;

//accept should return true if the string which may be inserted is legal, false otherwise
   void setCharacterAcceptFunc(const SlotAccept& accept);

  protected:
   //overriden insert_text_vfunc
};

This seems quite nice and flexible to me - maybe I should consider switching to slot<gchar, const::Glib::ustring&> so that callbacks return 0 if OK and the value of the first rejected char otherwise. And [by writing this nice ideas come to my mind] maybe I should also add

typedef sigc::slot<void, gchar> SlotReject;
void setCharacterRecectFunction(const SlotReject& reject);

and supply the class with a reasonable default[eg. emmiting a beep].

Feel free to critizise,
                                 Antonio








[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]