Tag Archives: gtk+

GTK+: Aligning / Justification in text widgets

There are 3 GTK+ text widgets: GtkEntry, GtkTextView, and GtkLabel. I noticed recently that they are a little inconsistent in how they offer alignment of their text, and the API confuses the terms justification and alignment slightly. This is highly unlikely to change, because that would be disruptive to existing code, but maybe this is helpful to people trying to use the API as it is now.

I’ve put some test_gtk_text_widgets_align.c example code in github. It shows two GtkLabels (single-line and multi-line) two GtkTextViews (single line and multi-line) and a GtkEntry (single-line only). I tried this with GTK+ 3.15.9. I’ve mostly ignored deprecated API, such as GtkMisc and GtkAlignment, to simplify things.

I’ve referred to the API via the *_set_*() functions, but this API is available as properties too. There are also vertical equivalents though I only talk about horizontal alignment here.

Default

This is how it looks by default. It looks fine, though the GtkLabel, and only the GtkLabel, defaults to center alignment – not a very useful default.

Justification

Now, if you want to justify the text to the right, you might call gtk_label_set_justify() on the GtkLabels and call gtk_text_view_set_justification() on the GtkTextViews, though you’ll need to call gtk_text_view_set_wrap_mode() for that to take effect. Then you’d see something like the screenshot below.

You’d notice

  • The justification had no effect on the single-line GtkLabel (as per the gtk_label_set_justify() documentation) but it did have an effect on the single-line GtkTextView.
  • The text in the GtkLabel remains aligned in the center (the GtkLabel default) even though it’s justified within that central part.
  • We didn’t call any justification method on the GtkEntry, because it has none. However, see below about gtk_entry_set_alignment().

Justification and Alignment

To fix that center-alignment of the right-justified text in the GtkLabel, you could call gtk_widget_set_halign(). Then you’d see this:

But, strangely:

  • The center-aligned right-justified text in the multi-line GtkLabel is unchanged. It remains center-aligned – it is not flush with the right side of the widget. But see below about gtk_label_set_xalign().
  • The single-line GtkLabel now appears to be justified/aligned to the right, which is nice. But see below.
  • The GtkTextViews have changed their word wrapping for some reason. I have no idea why.
  • The GtkTextViews and GtkEntry now use only just enough space at the right for their entire widgets, not just the text that they contain. In fact, the GtkEntry is now too small to show all of its text and, strangely, that happens even if I use gtk_widget_set_hexpand() on the GtkEntry.

This shows us that gtk_widget_set_halign() affects the whole widget (as per the halign documentation), not just the text. That is more obvious if we set a background color. Then we see that that’s why the single-line GtkLabel now looks justified – it’s really just the whole widget that has been aligned to the right:

Obviously, I don’t want this effect with the GtkTextViews, and it would only be useful on a single-line GtkLabel. Luckily we don’t need to use gtk_widget_set_halign() at all to justify text:

gtk_label_set_xalign

The GTK+ developers noticed this problem with GtkWidget’s halign so in the very latest GTK+ (3.15/3.16), they added gtk_label_set_xalign(). This aligns the text in the GtkLabel, rather than the GtkLabel itself. Like so:

I just pushed an improvement to the gtk_misc_set_alignment() deprecation documentation to make that clearer.

gtk_misc_set_alignment (deprecated)

If you can’t use the last GTK+ API, you can use the deprecated gtk_misc_set_alignment() method to get the same effect as gtk_label_set_xalign(). (Ignore the GtkTextViews here – they don’t derive from GtkMisc).

screenshot_test_gtk_text_widgets_set_alignment

Entry alignment

Likewise, GtkEntry has gtk_entry_set_alignment(), which has been in GTK+ since 2.4. Like gtk_labeL_set_xalign(), it affects the text in the widget, not widget itself.

screenshot_test_gtk_text_widgets_entry_alignment

Conclusion

So, if I wanted to justify my widget’s text, without changing the size of the widget itself, I’d decide what to do like this:

  • For a GtkLabel: Use both gtk_label_set_xalign() and gtk_label_set_justify(). Use gtk_misc_set_alignment() instead of gtk_label_set_xalign() if you can’t use GTK+ 3.16.
  • For a GtkTextView: Use gtk_text_view_set_justification() with gtk_text_view_set_wrap_mode().
  • For a GtkEntry: Use gtk_entry_set_alignment().

I could be wrong, so do please tell me how.