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