line wrapping of wxStaticText inside a vertical box sizer

10 views
Skip to first unread message

Robert Roebling

unread,
Sep 1, 2025, 6:52:02 AM (6 days ago) Sep 1
to wx-...@googlegroups.com
Hi all,

I remember there have been discussions about line wrapping in the
past, but I was just surprised that when I put a long wxStaticText
into a wxBoxSizer( wxVERTICAL ), the text would not wrap and go past
the window bounds. I then tried to call wxStaticText::Wrap() after
every change of window size, but that seems to not work under GTK+ 3
either, the text gets wrapped after every word over time (this is
likely an unrelated bug).
Looking at the GTK+ code of wxStaticText (to start with), in
DoGetBestSize(), line wrapping is explicitly shut off and the control
is supposed to return the unwrapped single line dimensions. I think
that this is wrong when called from inside a wxBoxSizer( wxVERTICAL )
when going top down, so when the size of the window is set and there
is limited horizontal space. wxWrapSizer does exactly what I am
looking for, but for individual controls, not individual words of a
wxStaticText.
I will look at why this actually works in wxWrapSizer (how does it
know its horizontal constraints during DoGetBestSize()?), but maybe we
need a DoGetBestSizeIfWidthIsLimitedTo(int width) which always just
calls DoGetBestSize(), apart from wxStaticText where it would
calculate the new height given fixed width.
Robert

Robert Roebling

unread,
Sep 1, 2025, 7:12:07 AM (6 days ago) Sep 1
to wx-...@googlegroups.com
Part 2 of my mail: wxBoxSizer( wxVERTICAL ) actually calls
wxWindow::InformFirstDirection( .., width ), but I cannot see where
this is being used within wxStaticText. I think this should be
overridden in wxStaticText and DoGetBestSize() changed to reflect it.
Once I have learnt how to use git properly, I will hopefully find time
to look at that.

Robert

Am Mo., 1. Sept. 2025 um 12:51 Uhr schrieb Robert Roebling
<robert....@googlemail.com>:

Vadim Zeitlin

unread,
Sep 1, 2025, 8:58:17 AM (6 days ago) Sep 1
to wx-...@googlegroups.com
On Mon, 1 Sep 2025 12:51:45 +0200 'Robert Roebling' via wx-dev wrote:

RR> I remember there have been discussions about line wrapping in the
RR> past, but I was just surprised that when I put a long wxStaticText
RR> into a wxBoxSizer( wxVERTICAL ), the text would not wrap and go past
RR> the window bounds. I then tried to call wxStaticText::Wrap() after
RR> every change of window size, but that seems to not work under GTK+ 3
RR> either, the text gets wrapped after every word over time (this is
RR> likely an unrelated bug).

Using Wrap() manually from wxEVT_SIZE handler does work for me (or at
least did when I tested it when I wrote this code...). One thing to note is
that you need to use wxST_NO_AUTORESIZE when using Wrap().

Of course, if we could make this work automatically, it would be much
better but I remember that this didn't seem especially simple to do when I
last looked at it...

RR> Looking at the GTK+ code of wxStaticText (to start with), in
RR> DoGetBestSize(), line wrapping is explicitly shut off and the control
RR> is supposed to return the unwrapped single line dimensions.

I think this is the only correct way to implement GetBestSize() semantics.

RR> I will look at why this actually works in wxWrapSizer (how does it
RR> know its horizontal constraints during DoGetBestSize()?), but maybe we
RR> need a DoGetBestSizeIfWidthIsLimitedTo(int width) which always just
RR> calls DoGetBestSize(), apart from wxStaticText where it would
RR> calculate the new height given fixed width.

We already have GetBest{Width,Height}() but they are only used by
wxBookCtrlBase currently and not for the generic layout.

Good luck,
VZ
Reply all
Reply to author
Forward
0 new messages