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