Problem with wxFileDialog and umlauts 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
beedaddy
In need of some credit
In need of some credit
Posts: 3
Joined: Thu Jan 26, 2023 7:23 am

Problem with wxFileDialog and umlauts

Post by beedaddy »

Dear all,

currently, I'm trying getting acquainted with wxWidgets. So far, everything was fine, but now I'm having a problem with wxFileDialog (wxWidgets 3.2.1).

I would like to open a file in order to parse it. And for reasons I have a function that expects a std::filesystem::path for this. I quickly noticed, that it doesn't work for file names with umlauts if I use openFileDialog.GetPath().ToStdString() to get the path to the file. So I switched to .ToStdWstring(). It looks like this:

Code: Select all

if (openFileDialog.ShowModal() == wxID_CANCEL)
        return;

std::wstring pathString{openFileDialog.GetPath().ToStdWstring()};

std::filesystem::path path(pathString);

auto numSellersImported = readSellersFromCsvFile(path, m_market);
This works on my Arch Linux (GNOME) machine as well as in my Windows 10 VM. But in a Linux Mint 21.1 VM, it crashes: "filesystem error: Cannot convert character sequence: Invalid or incomplete multibyte or wide character"

So maybe I misunderstood something and I do it the wrong way. Let me put the question this way: How should I use the wxFileDialog in order to open any file, even if the file name containts some special characters like umlauts?
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Problem with wxFileDialog and umlauts

Post by ONEEYEMAN »

Hi,
Can you run it under gdb (you should have a Debug build of both the library and application) and see where does it crash?
Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Problem with wxFileDialog and umlauts

Post by doublemax »

The code looks ok at first sight.

Code: Select all

std::wstring pathString{openFileDialog.GetPath().ToStdWstring()};
Please check what exactly "pathString" contains after this line.
Use the source, Luke!
beedaddy
In need of some credit
In need of some credit
Posts: 3
Joined: Thu Jan 26, 2023 7:23 am

Re: Problem with wxFileDialog and umlauts

Post by beedaddy »

Code: Select all

cout << pathString << endl;
shows me "/home/martin/Verkäufer.csv", which looks ok.

The crash happens here:

Code: Select all

std::filesystem::path path(pathString);
That puzzles me. Doesn't seem to be wxWidgets related at all. :oops:

(Edit: I would not be surprised if this had something to do with this GCC bug. My Arch and Windows MSYS2 are running GCC 12.2.1/12.2.0. Linux Mint is on 11.3.0.)
Last edited by beedaddy on Thu Jan 26, 2023 3:46 pm, edited 2 times in total.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Problem with wxFileDialog and umlauts

Post by doublemax »

beedaddy wrote: Thu Jan 26, 2023 2:01 pm

Code: Select all

cout << pathString << endl;
shows me "/home/myuser/Verkäufer.csv", which looks ok
Sorry, that's not what i meant, as both cout and "<<" could perform some kind of decoding.

Can you print the hex value of every single char, i want to check if it's already UTF-8 decoded (as it should).
Use the source, Luke!
beedaddy
In need of some credit
In need of some credit
Posts: 3
Joined: Thu Jan 26, 2023 7:23 am

Re: Problem with wxFileDialog and umlauts

Post by beedaddy »

You mean something like that?

Code: Select all

auto s = pathString.c_str();

while (*s) {
    printf("%02x ", (unsigned int)*s++);
}
printf("\n");
The result is: 2f 68 6f 6d 65 2f 6d 61 72 74 69 6e 2f 56 65 72 6b e4 75 66 65 72 2e 63 73 76
Last edited by beedaddy on Thu Jan 26, 2023 3:34 pm, edited 1 time in total.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Problem with wxFileDialog and umlauts

Post by doublemax »

Unfortunately the c_str() is another potential obfuscation of the result.

Can you try if any of these works?

Code: Select all

std::filesystem::path path( openFileDialog.GetPath() );
std::filesystem::path path( openFileDialog.GetPath().ToUTF8() );
std::filesystem::path path( (const char *)openFileDialog.GetPath().ToUTF8() );      // in case the previous line does not compile
I don't work under Linux, but i always assumed they use UTF8 everywhere, but i could be wrong.
Use the source, Luke!
beedaddy
In need of some credit
In need of some credit
Posts: 3
Joined: Thu Jan 26, 2023 7:23 am

Re: Problem with wxFileDialog and umlauts

Post by beedaddy »

Only the last one compiles. But this helps me. Something like this seems to work:

Code: Select all

#if defined(_WIN32)
    std::filesystem::path path(openFileDialog.GetPath().ToStdWstring());
#else
    std::filesystem::path path(static_cast<const char *>(openFileDialog.GetPath().ToUTF8()));
#endif
At least, this works on both Linuxes and the Windows VM.

Thanks!
Post Reply