I have the following GTK 3 layout (here in Python for brevity):
from gi.repository import Gtk, Pango
win = Gtk.Window()
win.connect("destroy", Gtk.main_quit)
win.set_default_size(500, 25)
win.add(box := Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL))
box.pack_start(label := Gtk.Label(), expand=True, fill=True, padding=6)
label.set_text("Some text")
label.set_xalign(0)
box.pack_end(bar := Gtk.ProgressBar(), expand=False, fill=False, padding=6)
bar.set_ellipsize(Pango.EllipsizeMode.MIDDLE)
bar.set_size_request(150, -1)
bar.set_text("This text sometimes can and will get long")
bar.set_show_text(True)
bar.set_fraction(0.3)
win.show_all()
Gtk.main()
I want the progress bar to stay at some fixed width (e.g. at its width request of 150px) and ellipsize any overflowing text. But instead, it grows to fit the text:
How can I achieve what I want?
So far my best idea is to compute the text’s size with Pango and ellipsize it on my own (before set_text) to fit into the width request. Seems like there should be a better way.
But GtkProgressBar has built-in ellipsization, too, and it works just fine if I clamp the bar to some width. The problem is how to do this clamping, and it’s the same problem with GtkLabel, as far as I see.
Actually, I’ve come up with one way to clamp: pack the ProgressBar into one of 4 homogeneous columns of a GtkGrid. Then it is fixed to 1/4 of the grid’s (window’s) width, and I can pack the rest to span the other 3 columns. I had tried this before and it had some nasty side effects, but just tried again and it seems to work, so I’ll stick with this for now.