Disable wxString constructor and methods dealing with standard C string

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
User avatar
ardovm
In need of some credit
In need of some credit
Posts: 4
Joined: Mon Aug 22, 2016 2:26 pm

Disable wxString constructor and methods dealing with standard C string

Post by ardovm »

Hello All,

we need to migrate a multi-language multi-platform (GTK, Win32) application from wxWidgets 2.8 to wxWidgets 3.0.

The main issue we are facing at the moment is the new wxString class, that provides constructor with const char * data.
We are using standard (I believe) unicode-enabled builds on Linux and Win32. All our sources and translation files are encoded in UTF-8.

Before: all wxString objects had to be initialized with the wxT() macro, that ensured the strings were treated correctly.
It was a bit boring, but it worked.

Now: the wxT() macro is still there and works perfectly. But it is also possible to create wxString objects from const char *. Such objects are never interpreted as UTF-8 under Windows (but Latin-1 instead, at least on the systems I tested). This is unfortunate for us, because any forgotten wxT() lead to (possible!) encoding mismatches, that are hard to find and predict.

Proposal: permit developers to disable all methods of wxString, including constructors, that deal with bare const char * data. This solution is adopted by the Qt 4 library, by means of the QT_NO_CAST_FROM_ASCII and related macros.

What do you think about this proposal? Are there any alternative ways we can have the compiler help us avoid encoding errors?

Thank you in advance and best regards.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Disable wxString constructor and methods dealing with standard C string

Post by doublemax »

I think you should discuss this on the wx-dev mailing list where you can reach the core developers. This is a user forum.
https://groups.google.com/forum/#!forum/wx-dev

And i don't think there is any setting that enforces the behavior you want. It would be nice if the default conversion class "wxConvLibc" could be set externally.
Use the source, Luke!
Manolo
Can't get richer than this
Can't get richer than this
Posts: 827
Joined: Mon Apr 30, 2012 11:07 pm

Re: Disable wxString constructor and methods dealing with standard C string

Post by Manolo »

Dealing with multi-language gets tricky if you don't care from the very beginning.
The first intention is just to write in "my language" and later I will add translations. When the default language is other than English, this means that default strings are not ASCII. Perhaps a "use my language"-flag in the compiler helps.

That was more or less how wx2.8 worked. But then translations were not so easy, and passing strings around needed a lot of conversions from/to different encodings.
wx2.9 did a radical change. Internally stores the OS default encoding (but you can change this, forcing to UTF8) which makes things much more easy... with the bad thing being now the wxString constructors. Using char* onky works for ASCII. See more at http://docs.wxwidgets.org/trunk/overview_string.html and http://docs.wxwidgets.org/trunk/classwx_string.html and http://docs.wxwidgets.org/trunk/overview_unicode.html

For your problem you can warp all your char* at ctrs with FromUTF8 (as seen in the links above).

What I do is write all "default" strings in ASCII, enclosed by _() macro and use translations, even for my own language.
User avatar
ardovm
In need of some credit
In need of some credit
Posts: 4
Joined: Mon Aug 22, 2016 2:26 pm

Re: Disable wxString constructor and methods dealing with standard C string

Post by ardovm »

Thank you Manolo and doublemax,

I will ask wx-dev. My goal is to make expression such as

Code: Select all

wxString s("Hello");
detected again as compiler errors.

Best regards.
User avatar
ardovm
In need of some credit
In need of some credit
Posts: 4
Joined: Mon Aug 22, 2016 2:26 pm

Re: Disable wxString constructor and methods dealing with standard C string

Post by ardovm »

I am replying to myself just for the records. The idea was already proposed and rejected.

References:
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Disable wxString constructor and methods dealing with standard C string

Post by doublemax »

I don't know if this was already proposed, but i think an easy solution would be to replace the default parameter "wxConvLibc" with something like "wxConvDefault". And that one should be configurable by a user-call. So it would be possible to set it to wxConvUTF8.
Use the source, Luke!
Post Reply