Add EVT_WINDOW_MODAL_DIALOG_CLOSED() event table macro.
[wxWidgets.git] / include / wx / unichar.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/unichar.h
3 // Purpose: wxUniChar and wxUniCharRef classes
4 // Author: Vaclav Slavik
5 // Created: 2007-03-19
6 // RCS-ID: $Id$
7 // Copyright: (c) 2007 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_UNICHAR_H_
12 #define _WX_UNICHAR_H_
13
14 #include "wx/defs.h"
15 #include "wx/chartype.h"
16 #include "wx/stringimpl.h"
17
18 class WXDLLIMPEXP_FWD_BASE wxUniCharRef;
19 class WXDLLIMPEXP_FWD_BASE wxString;
20
21 // This class represents single Unicode character. It can be converted to
22 // and from char or wchar_t and implements commonly used character operations.
23 class WXDLLIMPEXP_BASE wxUniChar
24 {
25 public:
26 // NB: this is not wchar_t on purpose, it needs to represent the entire
27 // Unicode code points range and wchar_t may be too small for that
28 // (e.g. on Win32 where wchar_t* is encoded in UTF-16)
29 typedef wxUint32 value_type;
30
31 wxUniChar() : m_value(0) {}
32
33 // Create the character from 8bit character value encoded in the current
34 // locale's charset.
35 wxUniChar(char c) { m_value = From8bit(c); }
36 wxUniChar(unsigned char c) { m_value = From8bit((char)c); }
37
38 // Create the character from a wchar_t character value.
39 #if wxWCHAR_T_IS_REAL_TYPE
40 wxUniChar(wchar_t c) { m_value = c; }
41 #endif
42
43 wxUniChar(int c) { m_value = c; }
44 wxUniChar(unsigned int c) { m_value = c; }
45 wxUniChar(long int c) { m_value = c; }
46 wxUniChar(unsigned long int c) { m_value = c; }
47 wxUniChar(short int c) { m_value = c; }
48 wxUniChar(unsigned short int c) { m_value = c; }
49
50 wxUniChar(const wxUniCharRef& c);
51
52 // Returns Unicode code point value of the character
53 value_type GetValue() const { return m_value; }
54
55 #if wxUSE_UNICODE_UTF8
56 // buffer for single UTF-8 character
57 struct Utf8CharBuffer
58 {
59 char data[5];
60 operator const char*() const { return data; }
61 };
62
63 // returns the character encoded as UTF-8
64 // (NB: implemented in stringops.cpp)
65 Utf8CharBuffer AsUTF8() const;
66 #endif // wxUSE_UNICODE_UTF8
67
68 // Returns true if the character is an ASCII character:
69 bool IsAscii() const { return m_value < 0x80; }
70
71 // Returns true if the character is representable as a single byte in the
72 // current locale encoding and return this byte in output argument c (which
73 // must be non-NULL)
74 bool GetAsChar(char *c) const
75 {
76 #if wxUSE_UNICODE
77 if ( !IsAscii() )
78 {
79 #if !wxUSE_UTF8_LOCALE_ONLY
80 if ( GetAsHi8bit(m_value, c) )
81 return true;
82 #endif // !wxUSE_UTF8_LOCALE_ONLY
83
84 return false;
85 }
86 #endif // wxUSE_UNICODE
87
88 *c = wx_truncate_cast(char, m_value);
89 return true;
90 }
91
92 // Conversions to char and wchar_t types: all of those are needed to be
93 // able to pass wxUniChars to verious standard narrow and wide character
94 // functions
95 operator char() const { return To8bit(m_value); }
96 operator unsigned char() const { return (unsigned char)To8bit(m_value); }
97 #if wxWCHAR_T_IS_REAL_TYPE
98 operator wchar_t() const { return (wchar_t)m_value; }
99 #endif
100 operator int() const { return (int)m_value; }
101 operator unsigned int() const { return (unsigned int)m_value; }
102 operator long int() const { return (long int)m_value; }
103 operator unsigned long int() const { return (unsigned long)m_value; }
104 operator short int() const { return (short int)m_value; }
105 operator unsigned short int() const { return (unsigned short int)m_value; }
106
107 // We need this operator for the "*p" part of expressions like "for (
108 // const_iterator p = begin() + nStart; *p; ++p )". In this case,
109 // compilation would fail without it because the conversion to bool would
110 // be ambiguous (there are all these int types conversions...). (And adding
111 // operator unspecified_bool_type() would only makes the ambiguity worse.)
112 operator bool() const { return m_value != 0; }
113 bool operator!() const { return !((bool)*this); }
114
115 // And this one is needed by some (not all, but not using ifdefs makes the
116 // code easier) compilers to parse "str[0] && *p" successfully
117 bool operator&&(bool v) const { return (bool)*this && v; }
118
119 // Assignment operators:
120 wxUniChar& operator=(const wxUniChar& c) { if (&c != this) m_value = c.m_value; return *this; }
121 wxUniChar& operator=(const wxUniCharRef& c);
122 wxUniChar& operator=(char c) { m_value = From8bit(c); return *this; }
123 wxUniChar& operator=(unsigned char c) { m_value = From8bit((char)c); return *this; }
124 #if wxWCHAR_T_IS_REAL_TYPE
125 wxUniChar& operator=(wchar_t c) { m_value = c; return *this; }
126 #endif
127 wxUniChar& operator=(int c) { m_value = c; return *this; }
128 wxUniChar& operator=(unsigned int c) { m_value = c; return *this; }
129 wxUniChar& operator=(long int c) { m_value = c; return *this; }
130 wxUniChar& operator=(unsigned long int c) { m_value = c; return *this; }
131 wxUniChar& operator=(short int c) { m_value = c; return *this; }
132 wxUniChar& operator=(unsigned short int c) { m_value = c; return *this; }
133
134 // Comparison operators:
135
136 // define the given comparison operator for all the types
137 #define wxDEFINE_UNICHAR_OPERATOR(op) \
138 bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\
139 bool operator op(char c) const { return m_value op From8bit(c); } \
140 bool operator op(unsigned char c) const { return m_value op From8bit((char)c); } \
141 wxIF_WCHAR_T_TYPE( bool operator op(wchar_t c) const { return m_value op (value_type)c; } ) \
142 bool operator op(int c) const { return m_value op (value_type)c; } \
143 bool operator op(unsigned int c) const { return m_value op (value_type)c; } \
144 bool operator op(short int c) const { return m_value op (value_type)c; } \
145 bool operator op(unsigned short int c) const { return m_value op (value_type)c; } \
146 bool operator op(long int c) const { return m_value op (value_type)c; } \
147 bool operator op(unsigned long int c) const { return m_value op (value_type)c; }
148
149 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR)
150
151 #undef wxDEFINE_UNICHAR_OPERATOR
152
153 // this is needed for expressions like 'Z'-c
154 int operator-(const wxUniChar& c) const { return m_value - c.m_value; }
155 int operator-(char c) const { return m_value - From8bit(c); }
156 int operator-(unsigned char c) const { return m_value - From8bit((char)c); }
157 int operator-(wchar_t c) const { return m_value - (value_type)c; }
158
159
160 private:
161 // notice that we implement these functions inline for 7-bit ASCII
162 // characters purely for performance reasons
163 static value_type From8bit(char c)
164 {
165 #if wxUSE_UNICODE
166 if ( (unsigned char)c < 0x80 )
167 return c;
168
169 return FromHi8bit(c);
170 #else
171 return c;
172 #endif
173 }
174
175 static char To8bit(value_type c)
176 {
177 #if wxUSE_UNICODE
178 if ( c < 0x80 )
179 return wx_truncate_cast(char, c);
180
181 return ToHi8bit(c);
182 #else
183 return c;
184 #endif
185 }
186
187 // helpers of the functions above called to deal with non-ASCII chars
188 static value_type FromHi8bit(char c);
189 static char ToHi8bit(value_type v);
190 static bool GetAsHi8bit(value_type v, char *c);
191
192 private:
193 value_type m_value;
194 };
195
196
197 // Writeable reference to a character in wxString.
198 //
199 // This class can be used in the same way wxChar is used, except that changing
200 // its value updates the underlying string object.
201 class WXDLLIMPEXP_BASE wxUniCharRef
202 {
203 private:
204 typedef wxStringImpl::iterator iterator;
205
206 // create the reference
207 #if wxUSE_UNICODE_UTF8
208 wxUniCharRef(wxString& str, iterator pos) : m_str(str), m_pos(pos) {}
209 #else
210 wxUniCharRef(iterator pos) : m_pos(pos) {}
211 #endif
212
213 public:
214 // NB: we have to make this public, because we don't have wxString
215 // declaration available here and so can't declare wxString::iterator
216 // as friend; so at least don't use a ctor but a static function
217 // that must be used explicitly (this is more than using 'explicit'
218 // keyword on ctor!):
219 #if wxUSE_UNICODE_UTF8
220 static wxUniCharRef CreateForString(wxString& str, iterator pos)
221 { return wxUniCharRef(str, pos); }
222 #else
223 static wxUniCharRef CreateForString(iterator pos)
224 { return wxUniCharRef(pos); }
225 #endif
226
227 wxUniChar::value_type GetValue() const { return UniChar().GetValue(); }
228
229 #if wxUSE_UNICODE_UTF8
230 wxUniChar::Utf8CharBuffer AsUTF8() const { return UniChar().AsUTF8(); }
231 #endif // wxUSE_UNICODE_UTF8
232
233 bool IsAscii() const { return UniChar().IsAscii(); }
234 bool GetAsChar(char *c) const { return UniChar().GetAsChar(c); }
235
236 // Assignment operators:
237 #if wxUSE_UNICODE_UTF8
238 wxUniCharRef& operator=(const wxUniChar& c);
239 #else
240 wxUniCharRef& operator=(const wxUniChar& c) { *m_pos = c; return *this; }
241 #endif
242
243 wxUniCharRef& operator=(const wxUniCharRef& c)
244 { if (&c != this) *this = c.UniChar(); return *this; }
245
246 wxUniCharRef& operator=(char c) { return *this = wxUniChar(c); }
247 wxUniCharRef& operator=(unsigned char c) { return *this = wxUniChar(c); }
248 #if wxWCHAR_T_IS_REAL_TYPE
249 wxUniCharRef& operator=(wchar_t c) { return *this = wxUniChar(c); }
250 #endif
251 wxUniCharRef& operator=(int c) { return *this = wxUniChar(c); }
252 wxUniCharRef& operator=(unsigned int c) { return *this = wxUniChar(c); }
253 wxUniCharRef& operator=(short int c) { return *this = wxUniChar(c); }
254 wxUniCharRef& operator=(unsigned short int c) { return *this = wxUniChar(c); }
255 wxUniCharRef& operator=(long int c) { return *this = wxUniChar(c); }
256 wxUniCharRef& operator=(unsigned long int c) { return *this = wxUniChar(c); }
257
258 // Conversions to the same types as wxUniChar is convertible too:
259 operator char() const { return UniChar(); }
260 operator unsigned char() const { return UniChar(); }
261 #if wxWCHAR_T_IS_REAL_TYPE
262 operator wchar_t() const { return UniChar(); }
263 #endif
264 operator int() const { return UniChar(); }
265 operator unsigned int() const { return UniChar(); }
266 operator short int() const { return UniChar(); }
267 operator unsigned short int() const { return UniChar(); }
268 operator long int() const { return UniChar(); }
269 operator unsigned long int() const { return UniChar(); }
270
271 // see wxUniChar::operator bool etc. for explanation
272 operator bool() const { return (bool)UniChar(); }
273 bool operator!() const { return !UniChar(); }
274 bool operator&&(bool v) const { return UniChar() && v; }
275
276 // Comparison operators:
277 #define wxDEFINE_UNICHARREF_OPERATOR(op) \
278 bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\
279 bool operator op(const wxUniChar& c) const { return UniChar() op c; } \
280 bool operator op(char c) const { return UniChar() op c; } \
281 bool operator op(unsigned char c) const { return UniChar() op c; } \
282 wxIF_WCHAR_T_TYPE( bool operator op(wchar_t c) const { return UniChar() op c; } ) \
283 bool operator op(int c) const { return UniChar() op c; } \
284 bool operator op(unsigned int c) const { return UniChar() op c; } \
285 bool operator op(short int c) const { return UniChar() op c; } \
286 bool operator op(unsigned short int c) const { return UniChar() op c; } \
287 bool operator op(long int c) const { return UniChar() op c; } \
288 bool operator op(unsigned long int c) const { return UniChar() op c; }
289
290 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR)
291
292 #undef wxDEFINE_UNICHARREF_OPERATOR
293
294 // for expressions like c-'A':
295 int operator-(const wxUniCharRef& c) const { return UniChar() - c.UniChar(); }
296 int operator-(const wxUniChar& c) const { return UniChar() - c; }
297 int operator-(char c) const { return UniChar() - c; }
298 int operator-(unsigned char c) const { return UniChar() - c; }
299 int operator-(wchar_t c) const { return UniChar() - c; }
300
301 private:
302 #if wxUSE_UNICODE_UTF8
303 wxUniChar UniChar() const;
304 #else
305 wxUniChar UniChar() const { return *m_pos; }
306 #endif
307
308 friend class WXDLLIMPEXP_FWD_BASE wxUniChar;
309
310 private:
311 // reference to the string and pointer to the character in string
312 #if wxUSE_UNICODE_UTF8
313 wxString& m_str;
314 #endif
315 iterator m_pos;
316 };
317
318 inline wxUniChar::wxUniChar(const wxUniCharRef& c)
319 {
320 m_value = c.UniChar().m_value;
321 }
322
323 inline wxUniChar& wxUniChar::operator=(const wxUniCharRef& c)
324 {
325 m_value = c.UniChar().m_value;
326 return *this;
327 }
328
329 // Comparison operators for the case when wxUniChar(Ref) is the second operand
330 // implemented in terms of member comparison functions
331
332 #define wxCMP_REVERSE(c1, c2, op) c2 op c1
333
334 wxDEFINE_COMPARISONS(char, const wxUniChar&, wxCMP_REVERSE)
335 wxDEFINE_COMPARISONS(char, const wxUniCharRef&, wxCMP_REVERSE)
336
337 wxDEFINE_COMPARISONS(wchar_t, const wxUniChar&, wxCMP_REVERSE)
338 wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef&, wxCMP_REVERSE)
339
340 wxDEFINE_COMPARISONS(const wxUniChar&, const wxUniCharRef&, wxCMP_REVERSE)
341
342 #undef wxCMP_REVERSE
343
344 // for expressions like c-'A':
345 inline int operator-(char c1, const wxUniCharRef& c2) { return -(c2 - c1); }
346 inline int operator-(const wxUniChar& c1, const wxUniCharRef& c2) { return -(c2 - c1); }
347 inline int operator-(wchar_t c1, const wxUniCharRef& c2) { return -(c2 - c1); }
348
349 #endif /* _WX_UNICHAR_H_ */