luagtklgi

Is there a way to check if a GTK widget supports a property without checking version numbers?


According to the GTK API reference, the "license-type" property of GtkAboutDialog is only present in GTK >= 3.0. For compatibility, my code currently checks the GTK version before setting the "license-type" property:

-- This is Lua code binding to GTK via lgi
local dialog = Gtk.AboutDialog {
    title = "About Me",
    -- ...,
}
if Gtk.check_version(3,0,0) == nil then
    dialog.license_type = Gtk.License.MIT_X11
end

Instead of doing this, is there a way to directly ask GTK if a widget supports a certain property? I think the code would be more self-documenting and less bug prone if I could write something that looks like

if supports_property(dialog, "license-type") then
    dialog.license_type = Gtk.License.MIT_X11
end

Since this question is really about the GTK API, I'm OK with answers in any programming language. Although the examples are in Lua, I assume a similar problem should show up in other dynamic-language bindings or even in C, assuming that there is a way to set properties by name without going through the set_license_type accessor function.


Solution

  • You don't need to use the _property field like you are doing in your currently accepted answer since lgi finds all names in the type categories tables directly. Additionally, it is also possible to get type of an instance using _type accessor. So I'd recommend following solution:

    if dialog._type.license_type then
        dialog.license_type = Gtk.License.MIT_X11
    end