GTK 3 file chooser label alignment

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;

mydialog = gtk_file_chooser_dialog_new("Chooser_test", NULL,
                                  GTK_FILE_CHOOSER_ACTION_OPEN,
                                  "Cancel", GTK_RESPONSE_CANCEL,
                                  "Open", GTK_RESPONSE_ACCEPT,
                                  NULL);

mybox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
gtk_widget_set_halign(mybox, GTK_ALIGN_END);

mylabel=gtk_label_new("My_Label");

// gtk_widget_set_halign(mylabel, GTK_ALIGN_END);

gtk_box_pack_end(GTK_BOX(mybox), mylabel, TRUE, TRUE, 0);
gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(mydialog), mybox);

gtk_widget_show_all(mydialog);

gtk_main();

return 1;

}

Try gtk_label_set_xalign (mylabel, 1.0).

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);

Any other ideas, anyone?

Create another horizontal box (hbox), set its hexpand property to true and use it as the chooser’s extra widget. Right align mybox and add it to hbox.

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?

  1. hbox was meant as the name of the instance.
  2. I suggested another box as a container, in case you wanted to add more than one aligned widget.
  3. I guess setting expand in gtk_box_pack_end is different from setting hexpand on the label itself.

hbox was meant as the name of the instance.

I see.

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’.

Thanks again, Sigma and Phil, for your help.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.