GNOME Bugzilla – Bug 665560
gtk_label_set_width_chars() has no effect
Last modified: 2011-12-26 07:08:19 UTC
Created attachment 202777 [details] Test program In GTK+ 2.x, you can use gtk_label_set_ellipsize() and then gtk_label_set_width_chars() to make a GtkLabel fit within a certain width. In GTK+ 3.x, this no longer works. Running Arch Linux x86_64 with gtk2 2.24.8-2 and gtk3 3.2.2-2. I am attaching a simple test program. Please look at the screenshots of the same program compiled with GTK+ 2.x and with GTK+ 3.x and note the difference.
Created attachment 202778 [details] Screenshot with GTK+ 2.x
Created attachment 202779 [details] Screenshot with GTK+ 3.x
Just as a note, this is not completely true. What you experience here is that an unrealized (unmapped?) GtkWindow now tries to honor the widget's natural size rather than it's minimal one. Setting the label after the window got realized will work as you expected, either using gtk_widget_set_size_request() or gtk_label_set_width_chars(). Maybe what GtkLabel should do is to honor the GtkWidget::{width,height}-request and GtkLabel::width-chars in its natural size computing since these are explicit overrides of the defaults values. If the application did override the defaults, GtkLabel could probably safely guess the author knows what she's doing and don't try to me smarter than her.
Waiting for the window to be realized seems like an ugly workaround to me, but I will give it a try.
Created attachment 202789 [details] Test case that waits for window to be realized Waiting for the window to be realized doesn't help. I will see if waiting for the window to be mapped makes any difference.
Created attachment 202790 [details] Test case that waits for the window to be mapped Waiting for the window to be mapped before setting the text of the label seems to be a workaround.
Created attachment 202791 [details] Second test case that waits for the window to be mapped but does *not* work It gets a little more complicated: if the window is not resizable (that is, gtk_window_set_resizable (FALSE) has been called), the workaround of waiting for the window to be mapped no longer works.
Hi, this can be closed, as apparently I was misunderstanding the purpose of gtk_label_set_width_chars(). Apparently (and contrary to the documentation), gtk_label_set_width_chars() sets the *minimum* width of the label, while gtk_label_set_max_width_chars() sets the default/natural size. For more info, see this mail and the preceding thread: http://mail.gnome.org/archives/gtk-app-devel-list/2011-December/msg00023.html
Closing as an invalid entry, thanks for following up.