]> git.saurik.com Git - wxWidgets.git/blame - include/wx/unichar.h
Fix compilation of wxIconBundle in some particular minimal builds.
[wxWidgets.git] / include / wx / unichar.h
CommitLineData
e3f6cbd9
VS
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
dbecee02 14#include "wx/defs.h"
e3f6cbd9 15#include "wx/chartype.h"
a7ea63e2 16#include "wx/stringimpl.h"
a962cdf4 17
b5dbe15d 18class WXDLLIMPEXP_FWD_BASE wxUniCharRef;
22188d73 19class WXDLLIMPEXP_FWD_BASE wxString;
e3f6cbd9
VS
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.
23class WXDLLIMPEXP_BASE wxUniChar
24{
25public:
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)
dbecee02 29 typedef wxUint32 value_type;
e3f6cbd9
VS
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); }
9f1b1b78 36 wxUniChar(unsigned char c) { m_value = From8bit((char)c); }
e3f6cbd9
VS
37
38 // Create the character from a wchar_t character value.
c5cf8663 39#if wxWCHAR_T_IS_REAL_TYPE
e3f6cbd9 40 wxUniChar(wchar_t c) { m_value = c; }
436bb9f5 41#endif
e3f6cbd9 42
e3f6cbd9 43 wxUniChar(int c) { m_value = c; }
21f0762a 44 wxUniChar(unsigned int c) { m_value = c; }
4e5b3a4e 45 wxUniChar(long int c) { m_value = c; }
21f0762a
VS
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; }
e3f6cbd9
VS
49
50 wxUniChar(const wxUniCharRef& c);
51
52 // Returns Unicode code point value of the character
d1b7ed67 53 value_type GetValue() const { return m_value; }
e3f6cbd9 54
1fc10687
VS
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
81727065
VS
68 // Returns true if the character is an ASCII character:
69 bool IsAscii() const { return m_value < 0x80; }
70
874dbd3a
VZ
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
2a686bd3
VZ
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
e3f6cbd9 95 operator char() const { return To8bit(m_value); }
21f0762a 96 operator unsigned char() const { return (unsigned char)To8bit(m_value); }
c5cf8663 97#if wxWCHAR_T_IS_REAL_TYPE
d4e59879 98 operator wchar_t() const { return (wchar_t)m_value; }
21f0762a 99#endif
d4e59879
VS
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; }
e3f6cbd9
VS
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); }
8019a673
VZ
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
e3f6cbd9 117 bool operator&&(bool v) const { return (bool)*this && v; }
e3f6cbd9
VS
118
119 // Assignment operators:
162e998c 120 wxUniChar& operator=(const wxUniChar& c) { if (&c != this) m_value = c.m_value; return *this; }
c2d122aa 121 wxUniChar& operator=(const wxUniCharRef& c);
e3f6cbd9 122 wxUniChar& operator=(char c) { m_value = From8bit(c); return *this; }
db18be57 123 wxUniChar& operator=(unsigned char c) { m_value = From8bit((char)c); return *this; }
c5cf8663 124#if wxWCHAR_T_IS_REAL_TYPE
e3f6cbd9 125 wxUniChar& operator=(wchar_t c) { m_value = c; return *this; }
21f0762a 126#endif
171d6132 127 wxUniChar& operator=(int c) { m_value = c; return *this; }
21f0762a 128 wxUniChar& operator=(unsigned int c) { m_value = c; return *this; }
4e5b3a4e 129 wxUniChar& operator=(long int c) { m_value = c; return *this; }
21f0762a
VS
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; }
e3f6cbd9 133
fb52c2b6 134 // Comparison operators:
e3f6cbd9 135
fb52c2b6 136 // define the given comparison operator for all the types
98c4eb39 137#define wxDEFINE_UNICHAR_OPERATOR(op) \
fb52c2b6
VZ
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); } \
db18be57 140 bool operator op(unsigned char c) const { return m_value op From8bit((char)c); } \
21f0762a 141 wxIF_WCHAR_T_TYPE( bool operator op(wchar_t c) const { return m_value op (value_type)c; } ) \
db18be57 142 bool operator op(int c) const { return m_value op (value_type)c; } \
21f0762a
VS
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; } \
4e5b3a4e 146 bool operator op(long int c) const { return m_value op (value_type)c; } \
21f0762a 147 bool operator op(unsigned long int c) const { return m_value op (value_type)c; }
e3f6cbd9 148
fb52c2b6 149 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR)
e3f6cbd9 150
fb52c2b6 151#undef wxDEFINE_UNICHAR_OPERATOR
e3f6cbd9 152
fb52c2b6 153 // this is needed for expressions like 'Z'-c
e3f6cbd9
VS
154 int operator-(const wxUniChar& c) const { return m_value - c.m_value; }
155 int operator-(char c) const { return m_value - From8bit(c); }
db18be57 156 int operator-(unsigned char c) const { return m_value - From8bit((char)c); }
d1b7ed67 157 int operator-(wchar_t c) const { return m_value - (value_type)c; }
21f0762a 158
e3f6cbd9
VS
159
160private:
ce65118e
VZ
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 {
a4535b9f 165#if wxUSE_UNICODE
ce65118e
VZ
166 if ( (unsigned char)c < 0x80 )
167 return c;
168
169 return FromHi8bit(c);
a4535b9f
VS
170#else
171 return c;
172#endif
ce65118e
VZ
173 }
174
175 static char To8bit(value_type c)
176 {
a4535b9f 177#if wxUSE_UNICODE
ce65118e 178 if ( c < 0x80 )
5c69ef61 179 return wx_truncate_cast(char, c);
ce65118e
VZ
180
181 return ToHi8bit(c);
a4535b9f
VS
182#else
183 return c;
184#endif
ce65118e
VZ
185 }
186
187 // helpers of the functions above called to deal with non-ASCII chars
188 static value_type FromHi8bit(char c);
874dbd3a
VZ
189 static char ToHi8bit(value_type v);
190 static bool GetAsHi8bit(value_type v, char *c);
e3f6cbd9
VS
191
192private:
d1b7ed67 193 value_type m_value;
e3f6cbd9
VS
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.
201class WXDLLIMPEXP_BASE wxUniCharRef
202{
203private:
a962cdf4
VS
204 typedef wxStringImpl::iterator iterator;
205
e3f6cbd9 206 // create the reference
81727065 207#if wxUSE_UNICODE_UTF8
6bd4f281 208 wxUniCharRef(wxString& str, iterator pos) : m_str(str), m_pos(pos) {}
81727065 209#else
a962cdf4 210 wxUniCharRef(iterator pos) : m_pos(pos) {}
81727065 211#endif
e3f6cbd9
VS
212
213public:
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!):
81727065 219#if wxUSE_UNICODE_UTF8
6bd4f281
VS
220 static wxUniCharRef CreateForString(wxString& str, iterator pos)
221 { return wxUniCharRef(str, pos); }
81727065 222#else
a962cdf4 223 static wxUniCharRef CreateForString(iterator pos)
e3f6cbd9 224 { return wxUniCharRef(pos); }
81727065 225#endif
e3f6cbd9 226
d1b7ed67 227 wxUniChar::value_type GetValue() const { return UniChar().GetValue(); }
1fc10687
VS
228
229#if wxUSE_UNICODE_UTF8
230 wxUniChar::Utf8CharBuffer AsUTF8() const { return UniChar().AsUTF8(); }
231#endif // wxUSE_UNICODE_UTF8
232
bc5443ba 233 bool IsAscii() const { return UniChar().IsAscii(); }
874dbd3a 234 bool GetAsChar(char *c) const { return UniChar().GetAsChar(c); }
e3f6cbd9
VS
235
236 // Assignment operators:
81727065
VS
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
e3f6cbd9 242
81727065 243 wxUniCharRef& operator=(const wxUniCharRef& c)
162e998c 244 { if (&c != this) *this = c.UniChar(); return *this; }
e3f6cbd9
VS
245
246 wxUniCharRef& operator=(char c) { return *this = wxUniChar(c); }
db18be57 247 wxUniCharRef& operator=(unsigned char c) { return *this = wxUniChar(c); }
c5cf8663 248#if wxWCHAR_T_IS_REAL_TYPE
e3f6cbd9 249 wxUniCharRef& operator=(wchar_t c) { return *this = wxUniChar(c); }
21f0762a 250#endif
171d6132 251 wxUniCharRef& operator=(int c) { return *this = wxUniChar(c); }
21f0762a
VS
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); }
4e5b3a4e 255 wxUniCharRef& operator=(long int c) { return *this = wxUniChar(c); }
21f0762a 256 wxUniCharRef& operator=(unsigned long int c) { return *this = wxUniChar(c); }
e3f6cbd9 257
2a686bd3 258 // Conversions to the same types as wxUniChar is convertible too:
e3f6cbd9 259 operator char() const { return UniChar(); }
2a686bd3 260 operator unsigned char() const { return UniChar(); }
c5cf8663 261#if wxWCHAR_T_IS_REAL_TYPE
436bb9f5 262 operator wchar_t() const { return UniChar(); }
c961c0cf 263#endif
21f0762a 264 operator int() const { return UniChar(); }
153bc803 265 operator unsigned int() const { return UniChar(); }
21f0762a
VS
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(); }
e3f6cbd9
VS
270
271 // see wxUniChar::operator bool etc. for explanation
272 operator bool() const { return (bool)UniChar(); }
273 bool operator!() const { return !UniChar(); }
e3f6cbd9 274 bool operator&&(bool v) const { return UniChar() && v; }
e3f6cbd9 275
fb52c2b6 276 // Comparison operators:
98c4eb39 277#define wxDEFINE_UNICHARREF_OPERATOR(op) \
fb52c2b6
VZ
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; } \
db18be57 281 bool operator op(unsigned char c) const { return UniChar() op c; } \
21f0762a 282 wxIF_WCHAR_T_TYPE( bool operator op(wchar_t c) const { return UniChar() op c; } ) \
db18be57 283 bool operator op(int c) const { return UniChar() op c; } \
21f0762a
VS
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; } \
4e5b3a4e 287 bool operator op(long int c) const { return UniChar() op c; } \
21f0762a 288 bool operator op(unsigned long int c) const { return UniChar() op c; }
e3f6cbd9 289
fb52c2b6 290 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR)
e3f6cbd9 291
fb52c2b6 292#undef wxDEFINE_UNICHARREF_OPERATOR
e3f6cbd9
VS
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; }
db18be57 298 int operator-(unsigned char c) const { return UniChar() - c; }
e3f6cbd9 299 int operator-(wchar_t c) const { return UniChar() - c; }
e3f6cbd9
VS
300
301private:
81727065 302#if wxUSE_UNICODE_UTF8
467175ab 303 wxUniChar UniChar() const;
81727065 304#else
467175ab 305 wxUniChar UniChar() const { return *m_pos; }
81727065
VS
306#endif
307
b5dbe15d 308 friend class WXDLLIMPEXP_FWD_BASE wxUniChar;
e3f6cbd9
VS
309
310private:
81727065
VS
311 // reference to the string and pointer to the character in string
312#if wxUSE_UNICODE_UTF8
6bd4f281 313 wxString& m_str;
81727065 314#endif
a962cdf4 315 iterator m_pos;
e3f6cbd9
VS
316};
317
318inline wxUniChar::wxUniChar(const wxUniCharRef& c)
319{
320 m_value = c.UniChar().m_value;
321}
322
c2d122aa
VS
323inline wxUniChar& wxUniChar::operator=(const wxUniCharRef& c)
324{
325 m_value = c.UniChar().m_value;
326 return *this;
327}
328
fb52c2b6
VZ
329// Comparison operators for the case when wxUniChar(Ref) is the second operand
330// implemented in terms of member comparison functions
e3f6cbd9 331
fb52c2b6 332#define wxCMP_REVERSE(c1, c2, op) c2 op c1
e3f6cbd9 333
fb52c2b6
VZ
334wxDEFINE_COMPARISONS(char, const wxUniChar&, wxCMP_REVERSE)
335wxDEFINE_COMPARISONS(char, const wxUniCharRef&, wxCMP_REVERSE)
e3f6cbd9 336
fb52c2b6
VZ
337wxDEFINE_COMPARISONS(wchar_t, const wxUniChar&, wxCMP_REVERSE)
338wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef&, wxCMP_REVERSE)
e3f6cbd9 339
fb52c2b6 340wxDEFINE_COMPARISONS(const wxUniChar&, const wxUniCharRef&, wxCMP_REVERSE)
e3f6cbd9 341
fb52c2b6 342#undef wxCMP_REVERSE
e3f6cbd9
VS
343
344// for expressions like c-'A':
345inline int operator-(char c1, const wxUniCharRef& c2) { return -(c2 - c1); }
e3f6cbd9 346inline int operator-(const wxUniChar& c1, const wxUniCharRef& c2) { return -(c2 - c1); }
436bb9f5 347inline int operator-(wchar_t c1, const wxUniCharRef& c2) { return -(c2 - c1); }
e3f6cbd9
VS
348
349#endif /* _WX_UNICHAR_H_ */