11

I am trying to write an application that contains a GtkBox (Horizontal) where I add a dynamic number of buttons (with labels) depending on various conditions. Now I want to prevent the GtkBox to grow more than (for example) 600px. The button labels can be ellipsized.

So my question is, is there any common way to solve this problem? If not, I think i would have to create a new Container Class that will watch its size.

I am using the C API for GTK (gtk+-3.0)

mame98
  • 1,271
  • 12
  • 26

2 Answers2

8

This is not directly possible with the current version of GTK. A max-width CSS property may be implemented in a future version, as there have been plenty of requests for it.

What you would do is indeed create a new container subclass. You could probably inherit from GtkBin to keep things simple, and in your size_allocate handler just clamp the width to the maximum value before passing it on to the child widget.

You can also try out Emeus, the constraint layout library for GTK, similar to how iOS would do this.

ptomato
  • 56,175
  • 13
  • 112
  • 165
3

There is no simple way to limit the size of a widget, at least as far as I can tell. What you can do is attach to the size-allocate signal and call set_size_request when received.

Since size requests are only requests, not commands, depending on the circumstances you may not be able to shrink the widget as much as you would like, but this should be rare.

oldtechaa
  • 1,463
  • 1
  • 14
  • 26
  • 2
    It's generally bad for performance to do something that forces a relayout inside the `size-allocate` signal handler. It usually works, but can cause ugly "stuttering" where the widgets jump through a few size iterations before landing on a solution that's stable. – ptomato Oct 23 '16 at 23:09
  • 1
    @ptomato, your method could definitely be better, yes, especially in cases with slow GPUs. Either way, it looks like we really need a CSS property for it. – oldtechaa Oct 23 '16 at 23:20