gtkd-developers / GtkD

GtkD is a D binding and OO wrapper of GTK+ originally created by Antonio Monteiro
http://gtkd.org
Other
322 stars 71 forks source link

Generate GInterfaceInfo for D interfaces #322

Open Rogni opened 3 years ago

Rogni commented 3 years ago

I suggest to add generated static field GInterfaceInfo to glib interfaces for register GType from D class.

Example for ListModelIF:

interface ListModelIF {
    GInterfaceInfo * getInterfaceInfo() {
        static GInterfaceInfo info = {
                cast(GInterfaceInitFunc)&init_list_model_interface,
                null, 
                null
        };
        return &info; 
    }

    private extern (C) void init_list_model_interface(
                                    GListModelInterface* list,
                                    void* iface_data) {
        list.getItem = &getItemImpl;
        list.getNItems = &getNItemsImpl;
        list.getItemType = &getItemTypeImpl;
    } 

    private extern (C) GType getItemTypeImpl(GListModel* list) 
    {
        return ObjectG.getDObject!(ListModelIF)(cast(GObject*)list).getItemType();
    }

    private extern (C) uint getNItemsImpl(GListModel* list) {
        return ObjectG.getDObject!(ListModelIF)(cast(GObject*)list).getNItems();
    }

    private extern (C) void* getItemImpl(GListModel* list, uint position) {
        return ObjectG.getDObject!(ListModelIF)(cast(GObject*)list).getItem(position);
    }
} 
MikeWey commented 3 years ago

Could you elaborate a bit more on the use case of the addition of GInterfaceInfo, and the added functions.

To implement a GTK interface in a D class there is also: gtkd.Implement.ImplementInterface https://github.com/gtkd-developers/GtkD/blob/master/generated/gtkd/gtkd/Implement.d#L78 https://github.com/gtkd-developers/GtkD/blob/master/demos/gtkD/DemoCustomList/CustomList.d

Rogni commented 3 years ago

for register custom GTypes from D. i didn't find gtkd.Implement.ImplementInterface and started writing my solution. https://github.com/Rogni/ExampleContactsBook/blob/master/src/utils/GObjectType.d#L47 https://github.com/Rogni/ExampleContactsBook/blob/2e1a8ed192b7716bbda1b8bb6bd5166206ab0091/src/controllers/ContactListController.d#L28