]>
Commit | Line | Data |
---|---|---|
15b6757b FM |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: unicode | |
3 | // Purpose: topic overview | |
4 | // Author: wxWidgets team | |
5 | // RCS-ID: $Id$ | |
6 | // Licence: wxWindows license | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | /*! | |
36c9828f | 10 | |
15b6757b | 11 | @page unicode_overview Unicode support in wxWidgets |
36c9828f | 12 | |
15b6757b FM |
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 | |
36c9828f FM |
22 | |
23 | ||
15b6757b | 24 | @section whatisunicode What is Unicode? |
36c9828f | 25 | |
15b6757b FM |
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. | |
36c9828f | 44 | |
15b6757b | 45 | @section unicodeandansi Unicode and ANSI modes |
36c9828f | 46 | |
15b6757b FM |
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: | |
36c9828f FM |
53 | |
54 | ||
15b6757b FM |
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(), ...) | |
36c9828f | 58 | Special preprocessor tokens (@c __FILE__, @c __DATE__ |
15b6757b | 59 | and @c __TIME__) |
36c9828f FM |
60 | |
61 | ||
15b6757b FM |
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 | |
36c9828f | 76 | length of a wide-character string is called @c wcslen() (compare with |
15b6757b FM |
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 | |
36c9828f | 82 | build. wxWidgets provides the macros @c __TFILE__, @c __TDATE__ |
15b6757b FM |
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: | |
36c9828f | 87 | |
15b6757b FM |
88 | @code |
89 | #ifdef __UNICODE__ | |
90 | wchar_t wch = L'*'; | |
91 | const wchar_t *ws = L"Hello, world!"; | |
92 | int len = wcslen(ws); | |
36c9828f | 93 | |
15b6757b FM |
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); | |
36c9828f | 99 | |
15b6757b FM |
100 | printf("Compiled at %s\n", __DATE__); |
101 | #endif // Unicode/ANSI | |
102 | @endcode | |
36c9828f | 103 | |
15b6757b FM |
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. | |
36c9828f | 108 | |
15b6757b | 109 | @section unicodeinsidewxw Unicode support in wxWidgets |
36c9828f | 110 | |
15b6757b | 111 | In wxWidgets, the code fragment from above should be written instead: |
36c9828f | 112 | |
15b6757b FM |
113 | @code |
114 | wxChar ch = wxT('*'); | |
115 | wxString s = wxT("Hello, world!"); | |
116 | int len = s.Len(); | |
117 | @endcode | |
36c9828f | 118 | |
15b6757b FM |
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. | |
36c9828f | 123 | We have a @c wxChar type which maps either on @c char or @c wchar_t |
15b6757b | 124 | depending on the mode in which program is being compiled. There is no need for |
36c9828f | 125 | a separate type for strings though, because the standard |
15b6757b FM |
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. | |
36c9828f | 132 | The important conclusion is that if you use @c wxChar instead of |
15b6757b FM |
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: | |
36c9828f FM |
137 | |
138 | ||
15b6757b FM |
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. | |
36c9828f FM |
146 | |
147 | ||
148 | ||
15b6757b | 149 | @section unicodeoutsidewxw Unicode and the outside world |
36c9828f | 150 | |
15b6757b FM |
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. | |
36c9828f | 158 | To get an ANSI string from a wxString, you may use the |
15b6757b | 159 | mb_str() function which always returns an ANSI |
36c9828f | 160 | string (independently of the mode - while the usual |
15b6757b FM |
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. | |
36c9828f | 165 | Sometimes it is also necessary to go from ANSI strings to wxStrings. |
15b6757b | 166 | In this case, you can use the converter-constructor, as follows: |
36c9828f FM |
167 | |
168 | ||
15b6757b FM |
169 | @code |
170 | const char* ascii_str = "Some text"; | |
171 | wxString str(ascii_str, wxConvUTF8); | |
172 | @endcode | |
36c9828f | 173 | |
15b6757b FM |
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. | |
36c9828f | 178 | |
15b6757b | 179 | @section unicodesettings Unicode-related compilation settings |
36c9828f | 180 | |
15b6757b FM |
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 | |
36c9828f | 183 | compile your program in ANSI mode you can still define @c wxUSE_WCHAR_T |
15b6757b FM |
184 | to get some limited support for @c wchar_t type. |
185 | This will allow your program to perform conversions between Unicode strings and | |
36c9828f | 186 | ANSI ones (using @ref mbconvclasses_overview) |
15b6757b FM |
187 | and construct wxString objects from Unicode strings (presumably read |
188 | from some external file or elsewhere). | |
36c9828f | 189 | |
15b6757b | 190 | @section topic8 Traps for the unwary |
36c9828f FM |
191 | |
192 | ||
193 | ||
15b6757b FM |
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 | |
36c9828f | 197 | |
15b6757b | 198 | */ |
36c9828f FM |
199 | |
200 |