]> git.saurik.com Git - wxWidgets.git/blob - docs/latex/wx/tunicode.tex
added SpinCtrl,
[wxWidgets.git] / docs / latex / wx / tunicode.tex
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 %% Name: tunicode.tex
3 %% Purpose: Overview of the Unicode support in wxWindows
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 wxWindows}\label{unicode}
13
14 This section briefly describes the state of the Unicode support in wxWindows.
15 Read it if you want to know more about how to write programs able to work with
16 characters from languages other than English.
17
18 \subsection{What is Unicode?}
19
20 Starting with release 2.1 wxWindows has support for compiling in Unicode mode
21 on the platforms which support it. Unicode is a standard for character
22 encoding which addreses the shortcomings of the previous, 8 bit standards, by
23 using 16 bit for encoding each character. This allows to have 65536 characters
24 instead of the usual 256 and is sufficient to encode all of the world
25 languages at once. More details about Unicode may be found at {\tt www.unicode.org}.
26
27 % TODO expand on it, say that Unicode extends ASCII, mention ISO8859, ...
28
29 As this solution is obviously preferable to the previous ones (think of
30 incompatible encodings for the same language, locale chaos and so on), many
31 modern ooperating systems support it. The probably first example is Windows NT
32 which uses only Unicode internally since its very first version.
33
34 Writing internationalized programs is much easier with Unicode and, as the
35 support for it improves, it should become more and more so. Moreover, in the
36 Windows NT/2000 case, even the program which uses only standard ASCII can profit
37 from using Unicode because they will work more efficiently - there will be no
38 need for the system to convert all strings hte program uses to/from Unicode
39 each time a system call is made.
40
41 \subsection{Unicode and ANSI modes}
42
43 As not all platforms supported by wxWindows support Unicode (fully) yet, in
44 many cases it is unwise to write a program which can only work in Unicode
45 environment. A better solution is to write programs in such way that they may
46 be compiled either in ANSI (traditional) mode or in the Unicode one.
47
48 This can be achieved quite simply by using the means provided by wxWindows.
49 Basicly, there are only a few things to watch out for:
50 \begin{itemize}
51 \item Character type ({\tt char} or {\tt wchar\_t})
52 \item Literal strings (i.e. {\tt "Hello, world!"} or {\tt '*'})
53 \item String functions ({\tt strlen()}, {\tt strcpy()}, ...)
54 \end{itemize}
55
56 Let's look at them in order. First of all, each character in an Unicode
57 program takes 2 bytes instead of usual one, so another type should be used to
58 store the characters ({\tt char} only holds 1 byte usually). This type is
59 called {\tt wchar\_t} which stands for {\it wide-character type}.
60
61 Also, the string and character constants should be encoded on 2 bytes instead
62 of one. This is achieved by using the standard C (and C++) way: just put the
63 letter {\tt 'L'} after any string constant and it becomes a {\it long}
64 constant, i.e. a wide character one. To make things a bit more readable, you
65 are also allowed to prefix the constant with {\tt 'L'} instead of putting it
66 after it.
67
68 Finally, the standard C functions don't work with {\tt wchar\_t} strings, so
69 another set of functions exists which do the same thing but accept
70 {\tt wchar\_t *} instead of {\tt char *}. For example, a function to get the
71 length of a wide-character string is called {\tt wcslen()} (compare with
72 {\tt strlen()} - you see that the only difference is that the "str" prefix
73 standing for "string" has been replaced with "wcs" standing for
74 "wide-character string").
75
76 To summarize, here is a brief example of how a program which can be compiled
77 in both ANSI and Unicode modes could look like:
78
79 \begin{verbatim}
80 #ifdef __UNICODE__
81 wchar_t wch = L'*';
82 const wchar_t *ws = L"Hello, world!";
83 int len = wcslen(ws);
84 #else // ANSI
85 char ch = '*';
86 const char *s = "Hello, world!";
87 int len = strlen(s);
88 #endif // Unicode/ANSI
89 \end{verbatim}
90
91 Of course, it would be nearly impossibly to write such programs if it had to
92 be done this way (try to imagine the number of {\tt #ifdef UNICODE} an average
93 program would have had!). Luckily, there is another way - see the next
94 section.
95
96 \subsection{Unicode support in wxWindows}
97
98 In wxWindows, the code fragment froim above should be written instead:
99
100 \begin{verbatim}
101 wxChar ch = T('*');
102 wxString s = T("Hello, world!");
103 int len = s.Len();
104 \end{verbatim}
105
106 What happens here? First of all, you see that there are no more {\tt #ifdef}s
107 at all. Instead, we define some types and macros which behave differently in
108 the Unicode and ANSI builds and allows us to avoid using conditional
109 compilation in the program itself.
110
111 We have a {\tt wxChar} type which maps either on {\tt char} or {\tt wchar\_t}
112 depending on the mode in which program is being compiled. There is no need for
113 a separate type for strings though, because the standard
114 \helpref{wxString}{wxstring} supports Unicode, i.e. it stores iether ANSI or
115 Unicode strings depending on the mode.
116
117 Finally, there is a special {\tt T()} macro which should enclose all literal
118 strings in the program. As it's easy to see comparing the last fragment with
119 the one above, this macro expands to nothing in the (usual) ANSI mode and
120 prefixes {\tt 'L'} to its argument in the Unicode mode.
121
122 The important conclusion is that if you use {\tt wxChar} instead of
123 {\tt char}, avoid using C style strings and use {\tt wxString} instead and
124 don't forget to enclose all string literals inside {\tt T()} macro, your
125 program automatically becomes (almost) Unicode compliant!
126
127 Just let us state once again the rules:
128 \begin{itemize}
129 \item Always use {\tt wxChar} instead of {\tt char}
130 \item Always enclose literal string constants in {\tt T()} macro unless
131 they're already converted to the right representation (another standard
132 wxWindows macro {\tt \_()} does it, so there is no need for {\tt T()} in this
133 case) or you intend to pass the constant directly to an external function
134 which doesn't accept wide-character strings.
135 \item Use {\tt wxString} instead of C style strings.
136 \end{itemize}
137
138 \subsection{Unicode and the outside world}
139
140 We have seen that it was easy to write Unicode programs using wxWindows types
141 and macros, but it has been also mentioned that it isn't quite enough.
142 Although everything works fine inside the program, things can get nasty when
143 it tries to communicate with the outside world which, sadly, often expects
144 ANSI strings (a notable exception is the entire Win32 API which accepts either
145 Unicode or ANSI strings and which thus makes it unnecessary to ever perform
146 any convertions in the program).
147
148 To get a ANSI string from a wxString, you may use
149 \helpref{mb\_str()}{wxstringmbstr} function which always returns an ANSI
150 string (independently of the mode - while the usual
151 \helpref{c\_str()}{wxstringcstr} returns a pointer to the internal
152 representation which is either ASCII or Unicode). More rarely used, but still
153 useful, is \helpref{wc\_str()}{wxstringwcstr} function which always returns
154 the Unicode string.
155
156 % TODO describe fn_str(), wx_str(), wxCharBuf classes, ...