]> git.saurik.com Git - wxWidgets.git/blame - docs/doxygen/overviews/mbconvclasses.h
Use flat generic status bar by default and add wxSB_SUNKEN.
[wxWidgets.git] / docs / doxygen / overviews / mbconvclasses.h
CommitLineData
15b6757b 1/////////////////////////////////////////////////////////////////////////////
9715cf42 2// Name: mbconvclasses.h
15b6757b
FM
3// Purpose: topic overview
4// Author: wxWidgets team
5// RCS-ID: $Id$
526954c5 6// Licence: wxWindows licence
15b6757b
FM
7/////////////////////////////////////////////////////////////////////////////
8
880efa2a 9/**
36c9828f 10
880efa2a 11@page overview_mbconv wxMBConv Overview
36c9828f 12
831e1028 13@tableofcontents
36c9828f 14
9715cf42
BP
15The wxMBConv classes in wxWidgets enable an Unicode-aware application to easily
16convert between Unicode and the variety of 8-bit encoding systems still in use.
36c9828f 17
831e1028 18@see @ref group_class_conv
36c9828f 19
36c9828f 20
36c9828f 21
9715cf42
BP
22@section overview_mbconv_need Background: The Need for Conversion
23
24As programs are becoming more and more globalized, and users exchange documents
25across country boundaries as never before, applications increasingly need to
26take into account all the different character sets in use around the world. It
27is no longer enough to just depend on the default byte-sized character set that
28computers have traditionally used.
29
30A few years ago, a solution was proposed: the Unicode standard. Able to contain
31the complete set of characters in use in one unified global coding system, it
32would resolve the character set problems once and for all.
33
34But it hasn't happened yet, and the migration towards Unicode has created new
35challenges, resulting in "compatibility encodings" such as UTF-8. A large
36number of systems out there still depends on the old 8-bit encodings, hampered
37by the huge amounts of legacy code still widely deployed. Even sending Unicode
38data from one Unicode-aware system to another may need encoding to an 8-bit
39multibyte encoding (UTF-7 or UTF-8 is typically used for this purpose), to pass
40unhindered through any traditional transport channels.
41
42
43@section overview_mbconv_string Background: The wxString Class
44
727aa906
FM
45@todo rewrite this overview; it's not up2date with wxString changes
46
9715cf42
BP
47If you have compiled wxWidgets in Unicode mode, the wxChar type will become
48identical to wchar_t rather than char, and a wxString stores wxChars. Hence,
49all wxString manipulation in your application will then operate on Unicode
50strings, and almost as easily as working with ordinary char strings (you just
51need to remember to use the wxT() macro to encapsulate any string literals).
52
53But often, your environment doesn't want Unicode strings. You could be sending
54data over a network, or processing a text file for some other application. You
55need a way to quickly convert your easily-handled Unicode data to and from a
56traditional 8-bit encoding. And this is what the wxMBConv classes do.
57
58
59@section overview_mbconv_classes wxMBConv Classes
60
61The base class for all these conversions is the wxMBConv class (which itself
62implements standard libc locale conversion). Derived classes include
63wxMBConvLibc, several different wxMBConvUTFxxx classes, and wxCSConv, which
64implement different kinds of conversions. You can also derive your own class
65for your own custom encoding and use it, should you need it. All you need to do
66is override the MB2WC and WC2MB methods.
67
68
69@section overview_mbconv_objects wxMBConv Objects
70
71Several of the wxWidgets-provided wxMBConv classes have predefined instances
72(wxConvLibc, wxConvFileName, wxConvUTF7, wxConvUTF8, wxConvLocal). You can use
73these predefined objects directly, or you can instantiate your own objects.
74
75A variable, wxConvCurrent, points to the conversion object that the user
76interface is supposed to use, in the case that the user interface is not
77Unicode-based (like with GTK+ 1.2). By default, it points to wxConvLibc or
78wxConvLocal, depending on which works best on the current platform.
79
80
81@section overview_mbconv_csconv wxCSConv
82
83The wxCSConv class is special because when it is instantiated, you can tell it
84which character set it should use, which makes it meaningful to keep many
85instances of them around, each with a different character set (or you can
86create a wxCSConv instance on the fly).
87
88The predefined wxCSConv instance, wxConvLocal, is preset to use the default
89user character set, but you should rarely need to use it directly, it is better
90to go through wxConvCurrent.
91
92
93@section overview_mbconv_converting Converting Strings
94
95Once you have chosen which object you want to use to convert your text, here is
96how you would use them with wxString. These examples all assume that you are
97using a Unicode build of wxWidgets, although they will still compile in a
98non-Unicode build (they just won't convert anything).
99
100Example 1: Constructing a wxString from input in current encoding.
101
102@code
103wxString str(input_data, *wxConvCurrent);
104@endcode
105
106Example 2: Input in UTF-8 encoding.
107
108@code
109wxString str(input_data, wxConvUTF8);
110@endcode
111
112Example 3: Input in KOI8-R. Construction of wxCSConv instance on the fly.
113
114@code
115wxString str(input_data, wxCSConv(wxT("koi8-r")));
116@endcode
117
118Example 4: Printing a wxString to stdout in UTF-8 encoding.
119
120@code
121puts(str.mb_str(wxConvUTF8));
122@endcode
123
124Example 5: Printing a wxString to stdout in custom encoding. Using
125preconstructed wxCSConv instance.
126
127@code
128wxCSConv cust(user_encoding);
129printf("Data: %s\n", (const char*) str.mb_str(cust));
130@endcode
131
132@note Since mb_str() returns a temporary wxCharBuffer to hold the result of the
133conversion, you need to explicitly cast it to const char* if you use it in a
134vararg context (like with printf).
135
136
137@section overview_mbconv_buffers Converting Buffers
138
139If you have specialized needs, or just don't want to use wxString, you can also
140use the conversion methods of the conversion objects directly. This can even be
141useful if you need to do conversion in a non-Unicode build of wxWidgets;
142converting a string from UTF-8 to the current encoding should be possible by
143doing this:
144
145@code
146wxString str(wxConvUTF8.cMB2WC(input_data), *wxConvCurrent);
147@endcode
148
149Here, cMB2WC of the UTF8 object returns a wxWCharBuffer containing a Unicode
150string. The wxString constructor then converts it back to an 8-bit character
151set using the passed conversion object, *wxConvCurrent. (In a Unicode build of
152wxWidgets, the constructor ignores the passed conversion object and retains the
153Unicode data.)
154
155This could also be done by first making a wxString of the original data:
156
157@code
158wxString input_str(input_data);
159wxString str(input_str.wc_str(wxConvUTF8), *wxConvCurrent);
160@endcode
161
162To print a wxChar buffer to a non-Unicode stdout:
163
164@code
165printf("Data: %s\n", (const char*) wxConvCurrent->cWX2MB(unicode_data));
166@endcode
167
168If you need to do more complex processing on the converted data, you may want
169to store the temporary buffer in a local variable:
170
171@code
172const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(unicode_data);
173const char *tmp_str = (const char*) tmp_buf;
174printf("Data: %s\n", tmp_str);
175process_data(tmp_str);
176@endcode
177
178If a conversion had taken place in cWX2MB (i.e. in a Unicode build), the buffer
179will be deallocated as soon as tmp_buf goes out of scope. The macro wxWX2MBbuf
180reflects the correct return value of cWX2MB (either char* or wxCharBuffer),
181except for the const.
182
183*/