Strange behavior with wxString

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
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Jul 26, 2016 2:00 pm

Strange behavior with wxString

Post by Wanderer82 »

Hi

Until now I have always used a fixed wxString in my app that I hard-coded. I then split up the wxString into all the words and they are then shown in StaticTexts with ComboBoxes underneath.

Now I have tried to use a TexCtrl to let the user input some text which is then used to split the words and create the StaticTexts etc. But always when I write text that is longer than one line, there is a biiiiig space between two lines (FlexGridSizers) of text. I use:

Code: Select all

TextCtrl1->GetValue();
to read the user input in the TextCtrl. What could be the reason that I get so big spaces?
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Strange behavior with wxString

Post by doublemax »

Is it a single- or multiline text control? If the string contains line breaks (0x0a and/or 0x0d), you probably need to filter them out before processing them.
Use the source, Luke!
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Jul 26, 2016 2:00 pm

Re: Strange behavior with wxString

Post by Wanderer82 »

Actually I guess I tried both versions. One without the "multiline" option and one with it. I won't have to put a special option if I want a single line control, right? Why would the string contain a line break if there isn't even a line break in the TxtCtrl window while typing the text? At least I could see wrapping inside my wxMessageBox but maybe this happens independently. So there is no way of getting text input without these line breaks? It's no problem to filter them out but if there's another option...
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Strange behavior with wxString

Post by doublemax »

I assumed they were line breaks, because that's the only reason i could think of to explain the behavior.

There shouldn't be any difference between a hardcoded string and one taken from a wxTextCtrl. Did you actually type the text, or copy-any-paste it from somewhere?
Use the source, Luke!
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Jul 26, 2016 2:00 pm

Re: Strange behavior with wxString

Post by Wanderer82 »

I typed it myself. Also converting the wxString to string won't change the behavior. Is there an option to print out the wxString somehow in a "raw" version? Like, so that I could see for example the line breaks? But I can just test filtering these out. If there are any and they are the culprit, I'll notice.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Strange behavior with wxString

Post by doublemax »

Wanderer82 wrote: Thu Feb 02, 2023 9:08 pm I typed it myself. Also converting the wxString to string won't change the behavior. Is there an option to print out the wxString somehow in a "raw" version? Like, so that I could see for example the line breaks? But I can just test filtering these out. If there are any and they are the culprit, I'll notice.
You could print the values of every character in the string:

Code: Select all

    wxString s = wxT("abc äöü\tÄÖÜ ß\r\n");
    for( int i = 0; i < s.length(); i++ ) {
      const wxUniChar c = s[i];
      wxLogMessage("%c - %d - %x", c, c, c);
    }
Use the source, Luke!
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Jul 26, 2016 2:00 pm

Re: Strange behavior with wxString

Post by Wanderer82 »

I tried that but unfortunately I can't see a special character or a line break where the big space happens. It's just a normal space. This is very strange. And it's especially strange as in case I paste a long text (instead of typing it) into the TextCtrl, everything works fine, there are no big spaces.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Strange behavior with wxString

Post by doublemax »

Maybe it has nothing to do with the textctrl, it's just about the text itself. What happens if you type the exact same text that you pasted before?

Can you show a screenshot?
Use the source, Luke!
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Jul 26, 2016 2:00 pm

Re: Strange behavior with wxString

Post by Wanderer82 »

Oh, now it's getting interesting. I found out that: the longer the text (the more lines), the smaller the space. And it doesn't matter if I paste the text or write it myself. So it might have something to do with my code as well. As I don't have time this week-end, this will have to wait a bit.
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Jul 26, 2016 2:00 pm

Re: Strange behavior with wxString

Post by Wanderer82 »

I've found it but I'm not sure what the problem is.

In my "real" app I didn't use a wxNotebook where the StaticTexts and ComboBoxes were placed inside FlexGridSizers. I used this line:

Code: Select all

BoxSizer5->Add(FlexGridSizer[a], 1, wxLEFT|wxRIGHT, 20);
This lead to a very big space when not the entire page was filled with contents. Changing it to:

Code: Select all

BoxSizer5->Add(FlexGridSizer[a], 0, wxLEFT|wxRIGHT, 20);
did the trick. It has something to do with proportion but I don't understand why this leads to a different result inside the wxNotebook.
Post Reply