]> git.saurik.com Git - wxWidgets.git/blame_incremental - docs/latex/wx/tunicode.tex
Applied patch [ 1747059 ] wxAUIDefaultTabArt wxAUI_NB_BOTTOM
[wxWidgets.git] / docs / latex / wx / tunicode.tex
... / ...
CommitLineData
1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2%% Name: tunicode.tex
3%% Purpose: Overview of the Unicode support in wxWidgets
4%% Author: Vadim Zeitlin
5%% Modified by:
6%% Created: 22.09.99
7%% RCS-ID: $Id$
8%% Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9%% Licence: wxWindows license
10%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
11
12\section{Unicode support in wxWidgets}\label{unicode}
13
14This section briefly describes the state of the Unicode support in wxWidgets.
15Read it if you want to know more about how to write programs able to work with
16characters from languages other than English.
17
18\subsection{What is Unicode?}\label{whatisunicode}
19
20wxWidgets has support for compiling in Unicode mode
21on the platforms which support it. Unicode is a standard for character
22encoding which addresses the shortcomings of the previous, 8 bit standards, by
23using at least 16 (and possibly 32) bits for encoding each character. This
24allows to have at least 65536 characters (what is called the BMP, or basic
25multilingual plane) and possible $2^{32}$ of them instead of the usual 256 and
26is sufficient to encode all of the world languages at once. More details about
27Unicode may be found at {\tt www.unicode.org}.
28
29% TODO expand on it, say that Unicode extends ASCII, mention ISO8859, ...
30
31As this solution is obviously preferable to the previous ones (think of
32incompatible encodings for the same language, locale chaos and so on), many
33modern operating systems support it. The probably first example is Windows NT
34which uses only Unicode internally since its very first version.
35
36Writing internationalized programs is much easier with Unicode and, as the
37support for it improves, it should become more and more so. Moreover, in the
38Windows NT/2000 case, even the program which uses only standard ASCII can profit
39from using Unicode because they will work more efficiently - there will be no
40need for the system to convert all strings the program uses to/from Unicode
41each time a system call is made.
42
43\subsection{Unicode and ANSI modes}\label{unicodeandansi}
44
45As not all platforms supported by wxWidgets support Unicode (fully) yet, in
46many cases it is unwise to write a program which can only work in Unicode
47environment. A better solution is to write programs in such way that they may
48be compiled either in ANSI (traditional) mode or in the Unicode one.
49
50This can be achieved quite simply by using the means provided by wxWidgets.
51Basically, there are only a few things to watch out for:
52
53\begin{itemize}
54\item Character type ({\tt char} or {\tt wchar\_t})
55\item Literal strings (i.e. {\tt "Hello, world!"} or {\tt '*'})
56\item String functions ({\tt strlen()}, {\tt strcpy()}, ...)
57\item Special preprocessor tokens ({\tt \_\_FILE\_\_}, {\tt \_\_DATE\_\_}
58and {\tt \_\_TIME\_\_})
59\end{itemize}
60
61Let's look at them in order. First of all, each character in an Unicode
62program takes 2 bytes instead of usual one, so another type should be used to
63store the characters ({\tt char} only holds 1 byte usually). This type is
64called {\tt wchar\_t} which stands for {\it wide-character type}.
65
66Also, the string and character constants should be encoded using wide
67characters ({\tt wchar\_t} type) which typically take $2$ or $4$ bytes instead
68of {\tt char} which only takes one. This is achieved by using the standard C
69(and C++) way: just put the letter {\tt 'L'} after any string constant and it
70becomes a {\it long} constant, i.e. a wide character one. To make things a bit
71more readable, you are also allowed to prefix the constant with {\tt 'L'}
72instead of putting it after it.
73
74Of course, the usual standard C functions don't work with {\tt wchar\_t}
75strings, so another set of functions exists which do the same thing but accept
76{\tt wchar\_t *} instead of {\tt char *}. For example, a function to get the
77length of a wide-character string is called {\tt wcslen()} (compare with
78{\tt strlen()} - you see that the only difference is that the "str" prefix
79standing for "string" has been replaced with "wcs" standing for "wide-character
80string").
81
82And finally, the standard preprocessor tokens enumerated above expand to ANSI
83strings but it is more likely that Unicode strings are wanted in the Unicode
84build. wxWidgets provides the macros {\tt \_\_TFILE\_\_}, {\tt \_\_TDATE\_\_}
85and {\tt \_\_TTIME\_\_} which behave exactly as the standard ones except that
86they produce ANSI strings in ANSI build and Unicode ones in the Unicode build.
87
88To summarize, here is a brief example of how a program which can be compiled
89in both ANSI and Unicode modes could look like:
90
91\begin{verbatim}
92#ifdef __UNICODE__
93 wchar_t wch = L'*';
94 const wchar_t *ws = L"Hello, world!";
95 int len = wcslen(ws);
96
97 wprintf(L"Compiled at %s\n", __TDATE__);
98#else // ANSI
99 char ch = '*';
100 const char *s = "Hello, world!";
101 int len = strlen(s);
102
103 printf("Compiled at %s\n", __DATE__);
104#endif // Unicode/ANSI
105\end{verbatim}
106
107Of course, it would be nearly impossibly to write such programs if it had to
108be done this way (try to imagine the number of {\tt \#ifdef UNICODE} an average
109program would have had!). Luckily, there is another way - see the next
110section.
111
112\subsection{Unicode support in wxWidgets}\label{unicodeinsidewxw}
113
114In wxWidgets, the code fragment from above should be written instead:
115
116\begin{verbatim}
117 wxChar ch = wxT('*');
118 wxString s = wxT("Hello, world!");
119 int len = s.Len();
120\end{verbatim}
121
122What happens here? First of all, you see that there are no more {\tt \#ifdef}s
123at all. Instead, we define some types and macros which behave differently in
124the Unicode and ANSI builds and allow us to avoid using conditional
125compilation in the program itself.
126
127We have a {\tt wxChar} type which maps either on {\tt char} or {\tt wchar\_t}
128depending on the mode in which program is being compiled. There is no need for
129a separate type for strings though, because the standard
130\helpref{wxString}{wxstring} supports Unicode, i.e. it stores either ANSI or
131Unicode strings depending on the compile mode.
132
133Finally, there is a special \helpref{wxT()}{wxt} macro which should enclose all
134literal strings in the program. As it is easy to see comparing the last
135fragment with the one above, this macro expands to nothing in the (usual) ANSI
136mode and prefixes {\tt 'L'} to its argument in the Unicode mode.
137
138The important conclusion is that if you use {\tt wxChar} instead of
139{\tt char}, avoid using C style strings and use {\tt wxString} instead and
140don't forget to enclose all string literals inside \helpref{wxT()}{wxt} macro, your
141program automatically becomes (almost) Unicode compliant!
142
143Just let us state once again the rules:
144
145\begin{itemize}
146\item Always use {\tt wxChar} instead of {\tt char}
147\item Always enclose literal string constants in \helpref{wxT()}{wxt} macro
148unless they're already converted to the right representation (another standard
149wxWidgets macro \helpref{\_()}{underscore} does it, for example, so there is no
150need for {\tt wxT()} in this case) or you intend to pass the constant directly
151to an external function which doesn't accept wide-character strings.
152\item Use {\tt wxString} instead of C style strings.
153\end{itemize}
154
155\subsection{Unicode and the outside world}\label{unicodeoutsidewxw}
156
157We have seen that it was easy to write Unicode programs using wxWidgets types
158and macros, but it has been also mentioned that it isn't quite enough.
159Although everything works fine inside the program, things can get nasty when
160it tries to communicate with the outside world which, sadly, often expects
161ANSI strings (a notable exception is the entire Win32 API which accepts either
162Unicode or ANSI strings and which thus makes it unnecessary to ever perform
163any conversions in the program). GTK 2.0 only accepts UTF-8 strings.
164
165To get an ANSI string from a wxString, you may use the
166mb\_str() function which always returns an ANSI
167string (independently of the mode - while the usual
168\helpref{c\_str()}{wxstringcstr} returns a pointer to the internal
169representation which is either ASCII or Unicode). More rarely used, but still
170useful, is wc\_str() function which always returns
171the Unicode string.
172
173Sometimes it is also necessary to go from ANSI strings to wxStrings.
174In this case, you can use the converter-constructor, as follows:
175
176\begin{verbatim}
177 const char* ascii_str = "Some text";
178 wxString str(ascii_str, wxConvUTF8);
179\end{verbatim}
180
181This code also compiles fine under a non-Unicode build of wxWidgets,
182but in that case the converter is ignored.
183
184For more information about converters and Unicode see
185the \helpref{wxMBConv classes overview}{mbconvclasses}.
186
187% TODO describe fn_str(), wx_str(), wxCharBuf classes, ...
188
189\subsection{Unicode-related compilation settings}\label{unicodesettings}
190
191You should define {\tt wxUSE\_UNICODE} to $1$ to compile your program in
192Unicode mode. This currently works for wxMSW, wxGTK, wxMac and wxX11. If you
193compile your program in ANSI mode you can still define {\tt wxUSE\_WCHAR\_T}
194to get some limited support for {\tt wchar\_t} type.
195
196This will allow your program to perform conversions between Unicode strings and
197ANSI ones (using \helpref{wxMBConv classes}{mbconvclasses})
198and construct wxString objects from Unicode strings (presumably read
199from some external file or elsewhere).
200