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