wxChoice with fixed width?

150 views
Skip to first unread message

Frédéric

unread,
Jan 15, 2018, 10:13:50 AM1/15/18
to wx-u...@googlegroups.com
Hi,

wxChoice control adapts its width to the width of the largest item(1).
For a particular application, this is too large and I want a fixed
width instead.
I saw in the code (1) that the width of the control is set with DoGetBestSize():

wxSize wxChoiceBase::DoGetBestSize() const
{
// a reasonable width for an empty choice list
wxSize best(80, -1);
const unsigned int nItems = GetCount();
if ( nItems > 0 )
{
wxTextMeasure txm(this);
best.x = txm.GetLargestStringExtent(GetStrings()).x;
}
return best;
}

So I tried to derive a control from wxChoice and override DoGetBestSize():
wxSize DoGetBestSize() const override { return {15, -1}; }

But this does not change anything (see attached minimal example).

How can change the width of a wxChoice?

Regards,

F

(1): src/common/choiccmn.cpp:107
choice.cpp

Manolo

unread,
Jan 15, 2018, 11:14:10 AM1/15/18
to wx-users
You don't need to overrride DoGetBestSize().

Just pass a size in your wxChoice constructor (see http://docs.wxwidgets.org/trunk/classwx_choice.html).

Also, if you use it with a wxSizer (you likely do, right?) set its proportion to 0 and don't use wxEXPAND flag.


Frédéric

unread,
Jan 15, 2018, 1:51:40 PM1/15/18
to wx-u...@googlegroups.com
> You don't need to overrride DoGetBestSize().
> Just pass a size in your wxChoice constructor
> Also, if you use it with a wxSizer (you likely do, right?) set its
> proportion to 0 and don't use wxEXPAND flag.


Oh, I did try that first but it did not work. Look at the attached
modified minimal example.
In fact, I have now checked a bit more: it works on Windows and OSX
but not on GTK.

The GTK version is bugged: the control is full width and shifted to
the left (see attached screencast)!

F
choice.cpp
choice.png

Igor Korot

unread,
Jan 15, 2018, 3:10:36 PM1/15/18
to wx-u...@googlegroups.com
Hi,
What is your OS?
Are you compiling against GTK2 or GTK3? Which version exactly?

Thank you.

>
> F
>
> --
> Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.
>
> To unsubscribe, send email to wx-users+u...@googlegroups.com
> or visit http://groups.google.com/group/wx-users

Vadim Zeitlin

unread,
Jan 15, 2018, 4:13:39 PM1/15/18
to wx-u...@googlegroups.com
On Mon, 15 Jan 2018 19:51:07 +0100 Frédéric wrote:

F> In fact, I have now checked a bit more: it works on Windows and OSX
F> but not on GTK.
F>
F> The GTK version is bugged: the control is full width and shifted to
F> the left (see attached screencast)!

This is indeed a bug in wxGTK3 (only, things work fine with GTK+ 2). It
was previously alluded to in http://trac.wxwidgets.org/ticket/17972 but
without any idea for the fix. I've created the new
http://trac.wxwidgets.org/ticket/18043 to track it, but can't do anything
else right now, sorry.

VZ

--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/

Frédéric

unread,
Jan 15, 2018, 4:50:59 PM1/15/18
to wx-u...@googlegroups.com
>> The GTK version is bugged: the control is full width and shifted to
>> the left (see attached screencast)!
>
> What is your OS?
> Are you compiling against GTK2 or GTK3? Which version exactly?

Fedora 26
GTK 3.22.21
Reply all
Reply to author
Forward
0 new messages