Present Perfect


Picture Gallery
Present Perfect

GTK question

Filed under: Flumotion,GNOME,Hacking — Thomas @ 17:23


We have a lot of text labels in Flumotion where we put mid-sentence newlines just so that once in a while the line gets broken and doesn't push the containing widget out wider than we want it to be.

Obviously this is a bogus way of doing things, but even our resident GTK+ wizard, Johan Dahlin, didn't have a ready answer for what the right way to do this is. So - how should we do it so that text gets flown correctly without mid-paragraph newlines and without ultra-wide widgets ?

The answer is probably superobvious.


  1. This needs the height-for-width/width-for-height/natural-size fixes in Mathias Hasselmann’s extended layout patch, which might get into GTK+ 2.16.

    Comment by Murray Cumming — 2008-07-24 @ 17:53

  2. gtk_label_set_wrap()?

    Comment by eosten — 2008-07-24 @ 18:03

  3. Maybe Murray was trying to point this out already :-)

    The WrapLabel widget in libview does this. Might have to translate it if gtkmm isn’t acceptable, but it’s there.

    Comment by Philip Langdale — 2008-07-24 @ 18:09

  4. Another option, instead of waiting for the fixes is to set the line_wrap for the label to TRUE, and attach to the size-allocate callback for the label’s container. Inside the callback, you need to get the label’s current allocation, and validate it against the container’s location, updating the label’s allocation in the process by issuing a gtk_widget_size_request() for the label.

    I do this in evolution-webcal in evolution-webcal-notify.c. Look at the e_webcal_label_size_allocate_cb() method there. It does exactly what you need.

    Comment by Rodney Dawes — 2008-07-24 @ 18:16

  5. http://log.ometer.com/2006-10.html#14

    Comment by Havoc — 2008-07-24 @ 18:24

  6. For gbrainy I end up using Gtk.TextView to fix a similar problem. I works like a charm and it is in the default Gtk toolbox.


    Comment by Jordi Mas — 2008-07-24 @ 19:58

  7. The quickest way to get what you want is to set a forced width request to the too-wide label, and set Wrap = on too. Expand = no and Fill = no may also be helpful. You may need to trap window realize to see what the calculated widths/heights have been for the labels, and iterate among them to force them to resize before the window is shown — that would be the most generic way of solving the problem.

    Tested my theory on Glade 3.

    But what are you using multiline labels for?!

    Comment by Rudd-O — 2008-07-24 @ 21:04

  8. I wrote a very basic label to do height-for-width wrapping. The label will take up only the width allocation given to it and compute its height accordingly. If wrap is turned off, the label will be a single line, ellipsizing at the end.

    Very basic, certainly gets the job done for us though:

    Whatever your approach, I do not recommend using a GtkTextView since it is quite resource intensive for simple labels (for instance, it creates its own internal windows).

    Comment by Aaron Bockover — 2008-07-25 @ 15:54

  9. look into gtk_label_set_width_char() and gtk_label_set_max_width_chars(). they help determine the approximate innitial width of a wrapping label for lack of other information about how big it ought to be.

    Comment by desrt — 2008-07-26 @ 17:04

RSS feed for comments on this post. TrackBack URL

Leave a comment