Skip to content

Return concrete type from widget constructors instead of GtkWidget*

Robert Ancell requested to merge robert.ancell/gtk:widget-types into master

GTK has an odd behaviour in that the widget constructors return the GtkWidget* type. This results in C users of GTK frequently downcasting to access objects. e.g.

GtkWidget *label = gtk_label_new (NULL);
gtk_label_set_label (GTK_LABEL (label), "foo");

instead of:

GtkLabel *label = gtk_label_new (NULL);
gtk_label_set_label (label, "foo");

The downside of all this casting is we lose the ability for the compiler to do any type checking. In the above example if the label was replaced with another widget this would only be detected at run-time. The casting macros also tend to make GTK lines very wide and hard to read, though the alternative requires more casting on functions that take GtkWidget* arguments.

With GTK 4 we have an opportunity to change the constructors in an API (but not ABI) break. This MR shows what changes are required to convert one common widget.

What do others think? Is this a change worth considering?

Edited by Matthias Clasen

Merge request reports