]> git.saurik.com Git - wxWidgets.git/blob - include/wx/unichar.h
added alpha channel support to wxDFB's wxBitmap
[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 #ifndef wxWCHAR_T_IS_SEPARATE_TYPE
19 // older versions of VC++ have wchar_t as typedef by default; this is
20 // configurable, so we have to check which behaviour is enabled
21 #if defined(__VISUALC__) && !defined(_NATIVE_WCHAR_T_DEFINED)
22 #define wxWCHAR_T_IS_SEPARATE_TYPE 0
23 #else
24 #define wxWCHAR_T_IS_SEPARATE_TYPE 1
25 #endif
26 #endif
27
28 // helper macro for doing something dependent on whether wchar_t is or isn't a
29 // typedef inside another macro
30 #if wxWCHAR_T_IS_SEPARATE_TYPE
31 #define wxIF_WCHAR_T_TYPE(x) x
32 #else // !wxWCHAR_T_IS_SEPARATE_TYPE
33 #define wxIF_WCHAR_T_TYPE(x)
34 #endif // wxWCHAR_T_IS_SEPARATE_TYPE/!wxWCHAR_T_IS_SEPARATE_TYPE
35
36 class WXDLLIMPEXP_BASE wxUniCharRef;
37 class WXDLLIMPEXP_BASE wxStringIteratorNode;
38
39 // This class represents single Unicode character. It can be converted to
40 // and from char or wchar_t and implements commonly used character operations.
41 class WXDLLIMPEXP_BASE wxUniChar
42 {
43 public:
44 // NB: this is not wchar_t on purpose, it needs to represent the entire
45 // Unicode code points range and wchar_t may be too small for that
46 // (e.g. on Win32 where wchar_t* is encoded in UTF-16)
47 typedef wxUint32 value_type;
48
49 wxUniChar() : m_value(0) {}
50
51 // Create the character from 8bit character value encoded in the current
52 // locale's charset.
53 wxUniChar(char c) { m_value = From8bit(c); }
54 wxUniChar(unsigned char c) { m_value = From8bit((char)c); }
55
56 // Create the character from a wchar_t character value.
57 #if wxWCHAR_T_IS_SEPARATE_TYPE
58 wxUniChar(wchar_t c) { m_value = c; }
59 #endif
60
61 wxUniChar(int c) { m_value = c; }
62 wxUniChar(unsigned int c) { m_value = c; }
63 wxUniChar(long int c) { m_value = c; }
64 wxUniChar(unsigned long int c) { m_value = c; }
65 wxUniChar(short int c) { m_value = c; }
66 wxUniChar(unsigned short int c) { m_value = c; }
67
68 wxUniChar(const wxUniCharRef& c);
69
70 // Returns Unicode code point value of the character
71 value_type GetValue() const { return m_value; }
72
73 #if wxUSE_UNICODE_UTF8
74 // buffer for single UTF-8 character
75 struct Utf8CharBuffer
76 {
77 char data[5];
78 operator const char*() const { return data; }
79 };
80
81 // returns the character encoded as UTF-8
82 // (NB: implemented in stringops.cpp)
83 Utf8CharBuffer AsUTF8() const;
84 #endif // wxUSE_UNICODE_UTF8
85
86 // Returns true if the character is an ASCII character:
87 bool IsAscii() const { return m_value < 0x80; }
88
89 // Conversions to char and wchar_t types: all of those are needed to be
90 // able to pass wxUniChars to verious standard narrow and wide character
91 // functions
92 operator char() const { return To8bit(m_value); }
93 operator unsigned char() const { return (unsigned char)To8bit(m_value); }
94 #if wxWCHAR_T_IS_SEPARATE_TYPE
95 operator wchar_t() const { return (wchar_t)m_value; }
96 #endif
97 operator int() const { return (int)m_value; }
98 operator unsigned int() const { return (unsigned int)m_value; }
99 operator long int() const { return (long int)m_value; }
100 operator unsigned long int() const { return (unsigned long)m_value; }
101 operator short int() const { return (short int)m_value; }
102 operator unsigned short int() const { return (unsigned short int)m_value; }
103
104 // We need this operator for the "*p" part of expressions like "for (
105 // const_iterator p = begin() + nStart; *p; ++p )". In this case,
106 // compilation would fail without it because the conversion to bool would
107 // be ambiguous (there are all these int types conversions...). (And adding
108 // operator unspecified_bool_type() would only makes the ambiguity worse.)
109 operator bool() const { return m_value != 0; }
110 bool operator!() const { return !((bool)*this); }
111
112 // And this one is needed by some (not all, but not using ifdefs makes the
113 // code easier) compilers to parse "str[0] && *p" successfully
114 bool operator&&(bool v) const { return (bool)*this && v; }
115
116 // Assignment operators:
117 wxUniChar& operator=(const wxUniChar& c) { m_value = c.m_value; return *this; }
118 wxUniChar& operator=(const wxUniCharRef& c);
119 wxUniChar& operator=(char c) { m_value = From8bit(c); return *this; }
120 wxUniChar& operator=(unsigned char c) { m_value = From8bit((char)c); return *this; }
121 #if wxWCHAR_T_IS_SEPARATE_TYPE
122 wxUniChar& operator=(wchar_t c) { m_value = c; return *this; }
123 #endif
124 wxUniChar& operator=(int c) { m_value = c; return *this; }
125 wxUniChar& operator=(unsigned int c) { m_value = c; return *this; }
126 wxUniChar& operator=(long int c) { m_value = c; return *this; }
127 wxUniChar& operator=(unsigned long int c) { m_value = c; return *this; }
128 wxUniChar& operator=(short int c) { m_value = c; return *this; }
129 wxUniChar& operator=(unsigned short int c) { m_value = c; return *this; }
130
131 // Comparison operators:
132
133 // define the given comparison operator for all the types
134 #define wxDEFINE_UNICHAR_OPERATOR(op) \
135 bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\
136 bool operator op(char c) const { return m_value op From8bit(c); } \
137 bool operator op(unsigned char c) const { return m_value op From8bit((char)c); } \
138 wxIF_WCHAR_T_TYPE( bool operator op(wchar_t c) const { return m_value op (value_type)c; } ) \
139 bool operator op(int c) const { return m_value op (value_type)c; } \
140 bool operator op(unsigned int c) const { return m_value op (value_type)c; } \
141 bool operator op(short int c) const { return m_value op (value_type)c; } \
142 bool operator op(unsigned short int c) const { return m_value op (value_type)c; } \
143 bool operator op(long int c) const { return m_value op (value_type)c; } \
144 bool operator op(unsigned long int c) const { return m_value op (value_type)c; }
145
146 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR)
147
148 #undef wxDEFINE_UNICHAR_OPERATOR
149
150 // this is needed for expressions like 'Z'-c
151 int operator-(const wxUniChar& c) const { return m_value - c.m_value; }
152 int operator-(char c) const { return m_value - From8bit(c); }
153 int operator-(unsigned char c) const { return m_value - From8bit((char)c); }
154 int operator-(wchar_t c) const { return m_value - (value_type)c; }
155
156
157 private:
158 static value_type From8bit(char c);
159 static char To8bit(value_type c);
160
161 private:
162 value_type m_value;
163 };
164
165
166 // Writeable reference to a character in wxString.
167 //
168 // This class can be used in the same way wxChar is used, except that changing
169 // its value updates the underlying string object.
170 class WXDLLIMPEXP_BASE wxUniCharRef
171 {
172 private:
173 typedef wxStringImpl::iterator iterator;
174
175 // create the reference
176 #if wxUSE_UNICODE_UTF8
177 wxUniCharRef(wxStringIteratorNode& node, iterator pos) : m_node(node), m_pos(pos) {}
178 #else
179 wxUniCharRef(iterator pos) : m_pos(pos) {}
180 #endif
181
182 public:
183 // NB: we have to make this public, because we don't have wxString
184 // declaration available here and so can't declare wxString::iterator
185 // as friend; so at least don't use a ctor but a static function
186 // that must be used explicitly (this is more than using 'explicit'
187 // keyword on ctor!):
188 #if wxUSE_UNICODE_UTF8
189 static wxUniCharRef CreateForString(wxStringIteratorNode& node, iterator pos)
190 { return wxUniCharRef(node, pos); }
191 #else
192 static wxUniCharRef CreateForString(iterator pos)
193 { return wxUniCharRef(pos); }
194 #endif
195
196 wxUniChar::value_type GetValue() const { return UniChar().GetValue(); }
197
198 #if wxUSE_UNICODE_UTF8
199 wxUniChar::Utf8CharBuffer AsUTF8() const { return UniChar().AsUTF8(); }
200 #endif // wxUSE_UNICODE_UTF8
201
202 bool IsAscii() const { return UniChar().IsAscii(); }
203
204 // Assignment operators:
205 #if wxUSE_UNICODE_UTF8
206 wxUniCharRef& operator=(const wxUniChar& c);
207 #else
208 wxUniCharRef& operator=(const wxUniChar& c) { *m_pos = c; return *this; }
209 #endif
210
211 wxUniCharRef& operator=(const wxUniCharRef& c)
212 { return *this = c.UniChar(); }
213
214 wxUniCharRef& operator=(char c) { return *this = wxUniChar(c); }
215 wxUniCharRef& operator=(unsigned char c) { return *this = wxUniChar(c); }
216 #if wxWCHAR_T_IS_SEPARATE_TYPE
217 wxUniCharRef& operator=(wchar_t c) { return *this = wxUniChar(c); }
218 #endif
219 wxUniCharRef& operator=(int c) { return *this = wxUniChar(c); }
220 wxUniCharRef& operator=(unsigned int c) { return *this = wxUniChar(c); }
221 wxUniCharRef& operator=(short int c) { return *this = wxUniChar(c); }
222 wxUniCharRef& operator=(unsigned short int c) { return *this = wxUniChar(c); }
223 wxUniCharRef& operator=(long int c) { return *this = wxUniChar(c); }
224 wxUniCharRef& operator=(unsigned long int c) { return *this = wxUniChar(c); }
225
226 // Conversions to the same types as wxUniChar is convertible too:
227 operator char() const { return UniChar(); }
228 operator unsigned char() const { return UniChar(); }
229 #if wxWCHAR_T_IS_SEPARATE_TYPE
230 operator wchar_t() const { return UniChar(); }
231 #endif
232 operator int() const { return UniChar(); }
233 operator unsigned int() const { return UniChar(); }
234 operator short int() const { return UniChar(); }
235 operator unsigned short int() const { return UniChar(); }
236 operator long int() const { return UniChar(); }
237 operator unsigned long int() const { return UniChar(); }
238
239 // see wxUniChar::operator bool etc. for explanation
240 operator bool() const { return (bool)UniChar(); }
241 bool operator!() const { return !UniChar(); }
242 bool operator&&(bool v) const { return UniChar() && v; }
243
244 // Comparison operators:
245 #define wxDEFINE_UNICHARREF_OPERATOR(op) \
246 bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\
247 bool operator op(const wxUniChar& c) const { return UniChar() op c; } \
248 bool operator op(char c) const { return UniChar() op c; } \
249 bool operator op(unsigned char c) const { return UniChar() op c; } \
250 wxIF_WCHAR_T_TYPE( bool operator op(wchar_t c) const { return UniChar() op c; } ) \
251 bool operator op(int c) const { return UniChar() op c; } \
252 bool operator op(unsigned int c) const { return UniChar() op c; } \
253 bool operator op(short int c) const { return UniChar() op c; } \
254 bool operator op(unsigned short int c) const { return UniChar() op c; } \
255 bool operator op(long int c) const { return UniChar() op c; } \
256 bool operator op(unsigned long int c) const { return UniChar() op c; }
257
258 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR)
259
260 #undef wxDEFINE_UNICHARREF_OPERATOR
261
262 // for expressions like c-'A':
263 int operator-(const wxUniCharRef& c) const { return UniChar() - c.UniChar(); }
264 int operator-(const wxUniChar& c) const { return UniChar() - c; }
265 int operator-(char c) const { return UniChar() - c; }
266 int operator-(unsigned char c) const { return UniChar() - c; }
267 int operator-(wchar_t c) const { return UniChar() - c; }
268
269 private:
270 #if wxUSE_UNICODE_UTF8
271 wxUniChar UniChar() const;
272 #else
273 wxUniChar UniChar() const { return *m_pos; }
274 #endif
275
276 friend class WXDLLIMPEXP_BASE wxUniChar;
277
278 private:
279 // reference to the string and pointer to the character in string
280 #if wxUSE_UNICODE_UTF8
281 wxStringIteratorNode& m_node;
282 #endif
283 iterator m_pos;
284 };
285
286 inline wxUniChar::wxUniChar(const wxUniCharRef& c)
287 {
288 m_value = c.UniChar().m_value;
289 }
290
291 inline wxUniChar& wxUniChar::operator=(const wxUniCharRef& c)
292 {
293 m_value = c.UniChar().m_value;
294 return *this;
295 }
296
297 // Comparison operators for the case when wxUniChar(Ref) is the second operand
298 // implemented in terms of member comparison functions
299
300 #define wxCMP_REVERSE(c1, c2, op) c2 op c1
301
302 wxDEFINE_COMPARISONS(char, const wxUniChar&, wxCMP_REVERSE)
303 wxDEFINE_COMPARISONS(char, const wxUniCharRef&, wxCMP_REVERSE)
304
305 wxDEFINE_COMPARISONS(wchar_t, const wxUniChar&, wxCMP_REVERSE)
306 wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef&, wxCMP_REVERSE)
307
308 wxDEFINE_COMPARISONS(const wxUniChar&, const wxUniCharRef&, wxCMP_REVERSE)
309
310 #undef wxCMP_REVERSE
311
312 // for expressions like c-'A':
313 inline int operator-(char c1, const wxUniCharRef& c2) { return -(c2 - c1); }
314 inline int operator-(const wxUniChar& c1, const wxUniCharRef& c2) { return -(c2 - c1); }
315 inline int operator-(wchar_t c1, const wxUniCharRef& c2) { return -(c2 - c1); }
316
317 #endif /* _WX_UNICHAR_H_ */