I am trying to add a right-aligned box containing a label to the GTK 3 file chooser dialog screen. The code below looks as though it should work (including the line “gtk_widget_set_halign(mybox, GTK_ALIGN_END);”) but the label is aligned to the left. Any ideas?
// GTK file chooser test
// modified from https://developer.gnome.org/gtk3/stable/ch01s02.html
// ljn 20191222 compiles and runs OK but labels not aligned to end
// compile with gcc pkg-config --cflags gtk+-3.0 -o file_chooser_alignment_05 file_chooser_alignment_05.c pkg-config --libs gtk+-3.0
#include <gtk/gtk.h>
int
main (int argc,
char **argv)
{
gtk_init(&argc, &argv);
GtkWidget *mydialog;
GtkWidget *mybox;
GtkWidget *mylabel;
Thanks for your suggestion Phil (pw0lff), following which I tried adding the line below to my code but it made no difference:
gtk_label_set_xalign (GTK_LABEL(mylabel), 1.0);
Note that I had to add a type cast to ‘mylabel’.
I put the line in place of my commented out line below (which also did not work):
// gtk_widget_set_halign(mylabel, GTK_ALIGN_END);
Thanks for your suggestion, Sigma, which did exactly what I wanted. In fact I did not even have to create a new hbox (which are deprecated) but just set the hexpand property of ‘mybox’ to true as you suggested, using the line below:
gtk_widget_set_hexpand (mybox,TRUE);
which I added just after my existing line below:
gtk_widget_set_halign(mybox, GTK_ALIGN_END);
I’m still puzzled as to why my original code didn’t work. When I packed ‘mylabel’ into ‘mybox’ I set the ‘expand’ parameter to ‘TRUE’. The documentation for ‘gtk_widget_set_hexpand ()’ states that ‘By default, widgets automatically expand if any of their children want to expand’. Why then didn’t ‘mybox’ expand as well?
I suggested another box as a container, in case you wanted to add more than one aligned widget.
Good thinking!
I guess setting expand in gtk_box_pack_end is different from setting hexpand on the label itself.
I think I’ve now found the answer to that one in the ‘GtkBox’ entry of the ‘GTK+ 3 Reference Manual’. In the details for the ‘expand’ child property it is stated ‘In contrast to “hexpand”, the expand child property does not cause the box to expand itself’.