6

I try to implement a GType interface in C++ using Glibmm (part of Gtkmm). The object will be passed to an API in C. Unfortunately, the documentation for gtkmm does not cover many details of how it wraps the GObject system.

What I have so far:

class MonaCompletionProvider : public gtksourceview::SourceCompletionProvider, public Glib::Object
{
    public:
        MonaCompletionProvider();
        virtual ~MonaCompletionProvider();

        Glib::ustring get_name_vfunc() const;
        // ... and some more
}

All method and constructor implementations are empty. The code is used like this:

Glib::RefPtr<MonaCompletionProvider> provider(new MonaCompletionProvider());
bool success = completion->add_provider(provider);

success will be false after executing this code and the following message appears in the command line:

(monagui:24831): GtkSourceView-CRITICAL **: gtk_source_completion_add_provider: assertion `GTK_IS_SOURCE_COMPLETION_PROVIDER (provider)' failed

It seems that the underlying gobj() is not aware that it is supposed to implement this interface. If the class does not derive from Glib::Object, gobj() even returns null. I hope that I do not have to write a GObject implementing this interface in C manually.

So what is the correct way to do this? Thanks in advance.

PS: For those who are interested: SourceCompletionProvider

Meinersbur
  • 7,881
  • 1
  • 27
  • 29
  • Maybe [this](https://developer.gnome.org/gtkmm-tutorial/stable/chapter-wrapping-c-libraries.html.en) is what you've been looking for about how gtkmm wraps the GObject system – user Apr 07 '14 at 11:47
  • I have a related issue trying to subclass Gst::AudioSink in gstreamermm - see https://stackoverflow.com/questions/49986814/interfacing-gobject-with-c Is there anything that better describes the mapping gmmproc is trying to create? – Bruce Adams Apr 23 '18 at 17:51

1 Answers1

5

Finally, I found a solution.

Class definition (order of subclasses matters):

class MonaCompletionProvider : public Glib::Object, public gtksourceview::SourceCompletionProvider {
...

Constructor (again, order matters):

MonaCompletionProvider::MonaCompletionProvider() :
    Glib::ObjectBase(typeid(MonaCompletionProvider)),
    Glib::Object(),
    gtksourceview::SourceCompletionProvider() {
...

Solution found by inspecting how it has been done in Guikachu.

bstpierre
  • 30,042
  • 15
  • 70
  • 103
Meinersbur
  • 7,881
  • 1
  • 27
  • 29