The gtk_widget_insert_* methods are hard to understand from bindings
Gtk4 allows to directly add a widget to another one, using the gtk_widget_insert_after()
and gtk_widget_insert_before()
methods. But the current way this methods are written makes things hard to understand when reading code in some other languages than C: python, vala, etc.
For example:
button.insert_before (box, null);
is quite hard to get: yes, that’s adding a button at the end of a box (N.B.: GtkBox has a clearer method for doing that, it’s just for the example).
That’s due to the C API being:
void gtk_widget_insert_after(GtkWidget *widget, GtkWidget *parent, GtkWidget *prev_sibling);
void gtk_widget_insert_before(GtkWidget *widget, GtkWidget *parent, GtkWidget *next_sibling);
with the name of the methods referring to their last argument, the sibling.
I see two possible solutions to this problem.
The first one is to invert the parent
and sibling
args. In C, that doesn’t read harder:
void gtk_widget_insert_after(GtkWidget *widget, GtkWidget *prev_sibling, GtkWidget *parent);
void gtk_widget_insert_before(GtkWidget *widget, GtkWidget *next_sibling, GtkWidget *parent);
but in bindings it makes things clearer:
widget.insert_after (sibling, parent);
widget.insert_before (sibling, parent);
as that can be read as “insert widget after/before sibling in parent.”
The other solution I suggest is to completely remove the parent
argument: Gtk can grab it from the sibling…
void gtk_widget_insert_after(GtkWidget *widget, GtkWidget *prev_sibling);
void gtk_widget_insert_before(GtkWidget *widget, GtkWidget *next_sibling);
But that means that prev_sibling
and next_sibling
cannot be null, and so that we need to add two methods to add in first and last positions (or else an easy way to get the current first and last children of a widget, but I dislike this option).
void gtk_widget_append_to(GtkWidget *widget, GtkWidget *parent);
void gtk_widget_prepend_to(GtkWidget *widget, GtkWidget *parent);
All of these methods are quite easily understood when using bindings.
widget.insert_after (sibling);
widget.insert_before (sibling);
widget.append_to (parent);
widget.prepend_to (parent);