How to set the size of widgets taking into account the size of UI elements? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
randomuser
In need of some credit
In need of some credit
Posts: 9
Joined: Mon May 03, 2021 12:20 pm

How to set the size of widgets taking into account the size of UI elements?

Post by randomuser »

I would like to set the width of the textboxes of some wxSpinCtrl widgets to an appropriate value such that they can display every valid number without any cutoff and without too much empty space. Given a four digit maximum, I tried to set the width to GetTextExtent(wxString("9999")). However, this sets the total size including UI elements and different UI frameworks have different sizes for these. For example there are two small arrow boxes with gtk2 and windows, but two much larger +/- boxes with gtk3. I suspect that their size also changes depending on display resolution and themes. This means I can't just measure the size of the UI elements and add a fixed amount to the size of the text.

Any suggestions for how to handle this situation?
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to set the size of widgets taking into account the size of UI elements?

Post by doublemax »

wxWindow::GetBestSize() would be the "official" way, that's the value the sizers use for their calculations. But not all controls on all platforms implement it correctly.
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: How to set the size of widgets taking into account the size of UI elements?

Post by ONEEYEMAN »

Hi,
Also keep in mind that some control simply don't have best size (such as wxGrid).

Thank you.
randomuser
In need of some credit
In need of some credit
Posts: 9
Joined: Mon May 03, 2021 12:20 pm

Re: How to set the size of widgets taking into account the size of UI elements?

Post by randomuser »

Thanks for the answers. It looks like wxSpinCtrl::GetBestSize() doesn't take min/max values into account with gtk2/3. I guess this is a bug? Is there anything I can do to work around this?
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to set the size of widgets taking into account the size of UI elements?

Post by doublemax »

randomuser wrote: Thu Jul 29, 2021 7:22 pm It looks like wxSpinCtrl::GetBestSize() doesn't take min/max values into account with gtk2/3.
Why should it? The control just tell you its "personal" preferred best size when no restrictions are set. min/max sizes values are only taken into account by sizers.

So, is the reported size correct or not?
Use the source, Luke!
randomuser
In need of some credit
In need of some credit
Posts: 9
Joined: Mon May 03, 2021 12:20 pm

Re: How to set the size of widgets taking into account the size of UI elements?

Post by randomuser »

It appears I wasn't clear enough. By min/max values, I mean the values that the spin control is allowed to have, not its minimum/maximum size. With gtk2/3, wxSpinCtrl::GetBestSize() always returns the same size independently of these values. So the sizer has the same size if only two-digit numbers are allowed as when seven-digits are allowed, and waste a lot of space as a consequence since the allowed values in my application are much smaller.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to set the size of widgets taking into account the size of UI elements?

Post by doublemax »

GetBestSize() should use the current value. So if you set the value to "9999" (or whatever) and then call GetBestSize() it should return something reasonable.
Use the source, Luke!
randomuser
In need of some credit
In need of some credit
Posts: 9
Joined: Mon May 03, 2021 12:20 pm

Re: How to set the size of widgets taking into account the size of UI elements?

Post by randomuser »

Thank you! This works in 3.1.5 as expected.
Post Reply