User Tools

Site Tools


wiki:wxstring

Using strings in FlameRobin

FlameRobin uses two kinds of strings: standard C++ std::string provided by STL and wxString class provided by wxWidgets.

Please note that wxString is the prefered class to use in entire system. It has a lot of features and provides good support for various character sets. It is easy to use non-ascii characters with it. IBPP library that we use for database access uses std::string. So, when we retrieve and set string data, we need to convert wxString to std::string and vice versa. To do this properly, we use functions wx2std and std2wx. That way strings are translated properly in both Unicode builds.

NOTE: When the second parameter to wx2std() and std2wx() (charset conversion object) is not given, then the default system converter is used. This will break when converting identifiers and other text read from the database or intended to be sent to the database, as the conversion object for the database character set is not necessarily the same as the conversion object for the system encoding.

String constants

When you use std::string, you use standard “string constant” notation. You will rarely use this, as most of the code should use wxString.

For wxString, the “string constant” has to be enclosed by a macro. There are two macros. If string is meant to be translated (for example: a message to the user in English), then it should be enclosed with _(), so we'll get _(“string constant”). If is just a string (for example: CREATE TABLE statement which doesn't need to be translated), we use wxT(), so we'll get wxT(“string constant”). In short:

what how
std::string“string”
wxStringwxT(“string”)
translatable wxString_(“string”)

String formatting

wxString has two useful functions, similar to standard sprintf() from C. These are Printf and Format. Printf changes the string object, while Format is static function. It takes wxString as format specifier, and variable number of parameters. Here's usage example with number:

wxString to_format;
int number;
 
to_format.Printf(wxT("%d"), number);               // Printf
to_format = wxString::Format(wxT("%d"), number);   // Format

Let's assume we have some text to translate as well:

wxString to_format;
int number;
to_format.Printf(_("You got %d tables in database."), number);       // good

Note that _() is used. This way we can make some sentences translatable even if language requires that number is placed in particular place in sentence. Therefore, don't use constructs like this:

wxString to_format;
int number;
to_format.Printf(wxT("%d"), number);
wxMessageBox(_("You got ")+to_format+_(" tables in database."));      // bad

One of the seemingly problematic issues is formatting the string parameters (via %s). We use .c_str() member function of wxString for that.

wxString s(_("great"));
wxString to_format;
to_format.Printf(_("FlameRobin is %s."), s.c_str());

Copyright © Milan Babuskov 2006.

wiki/wxstring.txt · Last modified: 2014/12/09 09:32 by mariuz