Automatically resize buttons?

29 views
Skip to first unread message

supsm17

unread,
Jun 21, 2021, 1:09:42 PM6/21/21
to fltk.general
I have several buttons in a scroll. I want the buttons to only be the size of the label, instead of a fixed size which could be too short or too long for the label. Currently, I set a large value as the width so the text doesn't get cut off. However, this makes the scroll bar inaccurate, and there is a lot of whitespace at the end. Is there any way to accomplish this?
temp.png

Albrecht Schlosser

unread,
Jun 21, 2021, 1:38:48 PM6/21/21
to fltkg...@googlegroups.com
You can always measure the text (label) size with fl_measure() or
fl_text_extents(), then set the sizes accordingly.

https://www.fltk.org/doc-1.4/group__fl__drawings.html#gaccd92d0c0521b0aac30dcb856d438dcd

https://www.fltk.org/doc-1.4/group__fl__attributes.html#ga14cb75a92b6cdd576f9512b38a208f8b


If this doesn't answer your question I may have misunderstood what
you're looking for.

supsm17

unread,
Jun 21, 2021, 11:46:49 PM6/21/21
to fltk.general
Thanks. I've noticed that widget->measure_label() and fl_measure() have the same value, which isn't correct. fl_text_extents() has a different value which is closer, but still isn't the correct value. Do you know why this is?

supsm17

unread,
Jun 21, 2021, 11:47:24 PM6/21/21
to fltk.general
Thanks! I've noticed that fl_measure() and widget->measure_label() aren't very accurate. fl_text_events() is better, but is it still too short. Do you know why this is?

On Monday, June 21, 2021 at 10:38:48 AM UTC-7 Albrecht Schlosser wrote:

Greg Ercolano

unread,
Jun 22, 2021, 12:01:27 AM6/22/21
to fltk.general
Thanks! I've noticed that fl_measure() and widget->measure_label() aren't very accurate. fl_text_events() is better, but is it still too short. Do you know why this is?

fl_text_extents() should be /really/ accurate. Like precise to the pixel. You'll get different sizes even on the vertical axis, depending on if the text has caps vs lowercase and if there's characters with descenders or not. It gives you literally the drawing extents of the text. Font Antialiasing /might/ add a fuzz factor to the calculations, perhaps +/- 1 pixel at each edge, not sure.

You probably should leave some margin all the way around anyway, otherwise your button's borders will be tangent to the text. I'd suggest adding at least 3 pixels all the way around minimum, so use w+6 and h+6. Keep in mind the button's borders need room too, so allow for that.

Albrecht Schlosser

unread,
Jun 22, 2021, 7:32:00 AM6/22/21
to fltkg...@googlegroups.com
On 6/21/21 11:43 PM supsm17 wrote:
Thanks. I've noticed that widget->measure_label() and fl_measure() have the same value, which isn't correct. fl_text_extents() has a different value which is closer, but still isn't the correct value. Do you know why this is?

As Greg already wrote, fl_text_extents() should be absolutely exact but it's a little more difficult to use. You can see both fl_measure() and fl_text_extents() "in action" in our test/unittests.cxx demo program in the "rendering text" and "symbol text" examples (the latter only with fl_measure()).

A potential problem with using these functions is that you need to set the font before you call the measure functions. Otherwise a random font will be used for measurement. This might explain your differences.

If you can't find a solution, I suggest that you post your code in form of a minimal, but compileable source code here. Then we can see why it doesn't work for you and help.

supsm17

unread,
Jun 22, 2021, 1:10:38 PM6/22/21
to fltk.general
I think the issue might've been that the vertical scroll bar was covering the text. I've added a little bit of extra space, it seems to be working now.
Reply all
Reply to author
Forward
0 new messages