Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a GObject interface in C++

Tags:

c++

glib

gtkmm

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

like image 646
Meinersbur Avatar asked Feb 13 '11 16:02

Meinersbur


1 Answers

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.

like image 115
Meinersbur Avatar answered Sep 27 '22 20:09

Meinersbur