Deriving from Gtk::EntryBuffer

Hi, I am trying to derive and use Gtk::EntryBuffer with the following code:

class TimeBuffer : Gtk::EntryBuffer{
	void on_inserted_text (guint position, const char * chars, guint n_chars){
		if ( (position == 2) || (position == 5) ){
			gtk_entry_buffer_insert_text(gobj(), (position +1), ((const char*) chars[position]), -1 /* see docs */); // reffer to EntryBuffer::insert_text
			gtk_entry_buffer_insert_text(gobj(), position, (const char*) ':', -1);
		if ( ( chars[position] < 48) || ( chars[position] > 57) ) gtk_entry_buffer_insert_text(gobj(), position, (const char*) '0', -1);
	Glib::RefPtr<Gtk::EntryBuffer> get_entry_buffer(){
		return Glib::RefPtr<Gtk::EntryBuffer>(dynamic_cast<Gtk::EntryBuffer*>(this));

It does even compile and I am able to attribute a variable to it. However I am having issue on getting the described behaviour of time formatting out of it. I believe my problem is that Gtk::Entry::set_buffer expects a shared pointer of Gtk::EntryBuffer type, however as it does it, it ignores the overloading of the default handler for inserted text

Regarding your get_entry_buffer() method, see

See the api documentation for Glib::make_refptr_for_instance() at glibmm: Glib Namespace Reference

I’d recommend making a ::create() method, and then whatever object you’ve composed this EntryBuffer into should have a member variable holding the refptr, rather than using the xxx_from_this approach.

As for your on_inserted_text method, I’m not sure that chars[position] is valid; the size of chars is n_chars. I think position is not related to chars at all, so using it as an index seems wrong.

right but anyway I should override the set_buffer function to work with the other type of parameter right?

No, I wouldn’t derive a Gtk::Entry just to take your TimeBuffer. As the documentation for Glib::RefPtr points out, if you have a Glib::RefPtr<Derived> you can std::dynamic_pointer_cast<Base>, e.g. entry->set_buffer(std::dynamic_pointer_cast<Gtk::EntryBuffer>(ptr_timeBuffer))

In this case I would std::static_pointer_cast, but maybe while you are exploring polymorphism it might be more helpful to use the dynamic error checking version

thanks, now it is using the function and indeed there is problem with the gtk_entry_buffer_insert_text function that I was using

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