]> git.saurik.com Git - wxWidgets.git/blame - include/wx/unichar.h
The dataview sample compiles now, but doesn't work yet...
[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
21f0762a 18#ifndef wxWCHAR_T_IS_SEPARATE_TYPE
4d62edfb
VS
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
21f0762a 25 #endif
436bb9f5
VZ
26#endif
27
21f0762a 28// helper macro for doing something dependent on whether wchar_t is or isn't a
436bb9f5 29// typedef inside another macro
4d62edfb 30#if wxWCHAR_T_IS_SEPARATE_TYPE
21f0762a
VS
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
153bc803 35
e3f6cbd9 36class WXDLLIMPEXP_BASE wxUniCharRef;
b0c4d5d7 37class WXDLLIMPEXP_BASE wxStringIteratorNode;
e3f6cbd9
VS
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.
41class WXDLLIMPEXP_BASE wxUniChar
42{
43public:
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)
dbecee02 47 typedef wxUint32 value_type;
e3f6cbd9
VS
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); }
9f1b1b78 54 wxUniChar(unsigned char c) { m_value = From8bit((char)c); }
e3f6cbd9
VS
55
56 // Create the character from a wchar_t character value.
4d62edfb 57#if wxWCHAR_T_IS_SEPARATE_TYPE
e3f6cbd9 58 wxUniChar(wchar_t c) { m_value = c; }
436bb9f5 59#endif
e3f6cbd9 60
e3f6cbd9 61 wxUniChar(int c) { m_value = c; }
21f0762a 62 wxUniChar(unsigned int c) { m_value = c; }
4e5b3a4e 63 wxUniChar(long int c) { m_value = c; }
21f0762a
VS
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; }
e3f6cbd9
VS
67
68 wxUniChar(const wxUniCharRef& c);
69
70 // Returns Unicode code point value of the character
d1b7ed67 71 value_type GetValue() const { return m_value; }
e3f6cbd9 72
1fc10687
VS
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
81727065
VS
86 // Returns true if the character is an ASCII character:
87 bool IsAscii() const { return m_value < 0x80; }
88
2a686bd3
VZ
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
e3f6cbd9 92 operator char() const { return To8bit(m_value); }
21f0762a 93 operator unsigned char() const { return (unsigned char)To8bit(m_value); }
4d62edfb 94#if wxWCHAR_T_IS_SEPARATE_TYPE
d4e59879 95 operator wchar_t() const { return (wchar_t)m_value; }
21f0762a 96#endif
d4e59879
VS
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; }
e3f6cbd9
VS
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); }
8019a673
VZ
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
e3f6cbd9 114 bool operator&&(bool v) const { return (bool)*this && v; }
e3f6cbd9
VS
115
116 // Assignment operators:
117 wxUniChar& operator=(const wxUniChar& c) { m_value = c.m_value; return *this; }
118 wxUniChar& operator=(char c) { m_value = From8bit(c); return *this; }
db18be57 119 wxUniChar& operator=(unsigned char c) { m_value = From8bit((char)c); return *this; }
4d62edfb 120#if wxWCHAR_T_IS_SEPARATE_TYPE
e3f6cbd9 121 wxUniChar& operator=(wchar_t c) { m_value = c; return *this; }
21f0762a 122#endif
171d6132 123 wxUniChar& operator=(int c) { m_value = c; return *this; }
21f0762a 124 wxUniChar& operator=(unsigned int c) { m_value = c; return *this; }
4e5b3a4e 125 wxUniChar& operator=(long int c) { m_value = c; return *this; }
21f0762a
VS
126 wxUniChar& operator=(unsigned long int c) { m_value = c; return *this; }
127 wxUniChar& operator=(short int c) { m_value = c; return *this; }
128 wxUniChar& operator=(unsigned short int c) { m_value = c; return *this; }
e3f6cbd9 129
fb52c2b6 130 // Comparison operators:
e3f6cbd9 131
fb52c2b6 132 // define the given comparison operator for all the types
98c4eb39 133#define wxDEFINE_UNICHAR_OPERATOR(op) \
fb52c2b6
VZ
134 bool operator op(const wxUniChar& c) const { return m_value op c.m_value; }\
135 bool operator op(char c) const { return m_value op From8bit(c); } \
db18be57 136 bool operator op(unsigned char c) const { return m_value op From8bit((char)c); } \
21f0762a 137 wxIF_WCHAR_T_TYPE( bool operator op(wchar_t c) const { return m_value op (value_type)c; } ) \
db18be57 138 bool operator op(int c) const { return m_value op (value_type)c; } \
21f0762a
VS
139 bool operator op(unsigned int c) const { return m_value op (value_type)c; } \
140 bool operator op(short int c) const { return m_value op (value_type)c; } \
141 bool operator op(unsigned short int c) const { return m_value op (value_type)c; } \
4e5b3a4e 142 bool operator op(long int c) const { return m_value op (value_type)c; } \
21f0762a 143 bool operator op(unsigned long int c) const { return m_value op (value_type)c; }
e3f6cbd9 144
fb52c2b6 145 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHAR_OPERATOR)
e3f6cbd9 146
fb52c2b6 147#undef wxDEFINE_UNICHAR_OPERATOR
e3f6cbd9 148
fb52c2b6 149 // this is needed for expressions like 'Z'-c
e3f6cbd9
VS
150 int operator-(const wxUniChar& c) const { return m_value - c.m_value; }
151 int operator-(char c) const { return m_value - From8bit(c); }
db18be57 152 int operator-(unsigned char c) const { return m_value - From8bit((char)c); }
d1b7ed67 153 int operator-(wchar_t c) const { return m_value - (value_type)c; }
21f0762a 154
e3f6cbd9
VS
155
156private:
d1b7ed67
VS
157 static value_type From8bit(char c);
158 static char To8bit(value_type c);
e3f6cbd9
VS
159
160private:
d1b7ed67 161 value_type m_value;
e3f6cbd9
VS
162};
163
164
165// Writeable reference to a character in wxString.
166//
167// This class can be used in the same way wxChar is used, except that changing
168// its value updates the underlying string object.
169class WXDLLIMPEXP_BASE wxUniCharRef
170{
171private:
a962cdf4
VS
172 typedef wxStringImpl::iterator iterator;
173
e3f6cbd9 174 // create the reference
81727065 175#if wxUSE_UNICODE_UTF8
b0c4d5d7 176 wxUniCharRef(wxStringIteratorNode& node, iterator pos) : m_node(node), m_pos(pos) {}
81727065 177#else
a962cdf4 178 wxUniCharRef(iterator pos) : m_pos(pos) {}
81727065 179#endif
e3f6cbd9
VS
180
181public:
182 // NB: we have to make this public, because we don't have wxString
183 // declaration available here and so can't declare wxString::iterator
184 // as friend; so at least don't use a ctor but a static function
185 // that must be used explicitly (this is more than using 'explicit'
186 // keyword on ctor!):
81727065 187#if wxUSE_UNICODE_UTF8
b0c4d5d7
VS
188 static wxUniCharRef CreateForString(wxStringIteratorNode& node, iterator pos)
189 { return wxUniCharRef(node, pos); }
81727065 190#else
a962cdf4 191 static wxUniCharRef CreateForString(iterator pos)
e3f6cbd9 192 { return wxUniCharRef(pos); }
81727065 193#endif
e3f6cbd9 194
d1b7ed67 195 wxUniChar::value_type GetValue() const { return UniChar().GetValue(); }
1fc10687
VS
196
197#if wxUSE_UNICODE_UTF8
198 wxUniChar::Utf8CharBuffer AsUTF8() const { return UniChar().AsUTF8(); }
199#endif // wxUSE_UNICODE_UTF8
200
bc5443ba 201 bool IsAscii() const { return UniChar().IsAscii(); }
e3f6cbd9
VS
202
203 // Assignment operators:
81727065
VS
204#if wxUSE_UNICODE_UTF8
205 wxUniCharRef& operator=(const wxUniChar& c);
206#else
207 wxUniCharRef& operator=(const wxUniChar& c) { *m_pos = c; return *this; }
208#endif
e3f6cbd9 209
81727065
VS
210 wxUniCharRef& operator=(const wxUniCharRef& c)
211 { return *this = c.UniChar(); }
e3f6cbd9
VS
212
213 wxUniCharRef& operator=(char c) { return *this = wxUniChar(c); }
db18be57 214 wxUniCharRef& operator=(unsigned char c) { return *this = wxUniChar(c); }
4d62edfb 215#if wxWCHAR_T_IS_SEPARATE_TYPE
e3f6cbd9 216 wxUniCharRef& operator=(wchar_t c) { return *this = wxUniChar(c); }
21f0762a 217#endif
171d6132 218 wxUniCharRef& operator=(int c) { return *this = wxUniChar(c); }
21f0762a
VS
219 wxUniCharRef& operator=(unsigned int c) { return *this = wxUniChar(c); }
220 wxUniCharRef& operator=(short int c) { return *this = wxUniChar(c); }
221 wxUniCharRef& operator=(unsigned short int c) { return *this = wxUniChar(c); }
4e5b3a4e 222 wxUniCharRef& operator=(long int c) { return *this = wxUniChar(c); }
21f0762a 223 wxUniCharRef& operator=(unsigned long int c) { return *this = wxUniChar(c); }
e3f6cbd9 224
2a686bd3 225 // Conversions to the same types as wxUniChar is convertible too:
e3f6cbd9 226 operator char() const { return UniChar(); }
2a686bd3 227 operator unsigned char() const { return UniChar(); }
4d62edfb 228#if wxWCHAR_T_IS_SEPARATE_TYPE
436bb9f5 229 operator wchar_t() const { return UniChar(); }
c961c0cf 230#endif
21f0762a 231 operator int() const { return UniChar(); }
153bc803 232 operator unsigned int() const { return UniChar(); }
21f0762a
VS
233 operator short int() const { return UniChar(); }
234 operator unsigned short int() const { return UniChar(); }
235 operator long int() const { return UniChar(); }
236 operator unsigned long int() const { return UniChar(); }
e3f6cbd9
VS
237
238 // see wxUniChar::operator bool etc. for explanation
239 operator bool() const { return (bool)UniChar(); }
240 bool operator!() const { return !UniChar(); }
e3f6cbd9 241 bool operator&&(bool v) const { return UniChar() && v; }
e3f6cbd9 242
fb52c2b6 243 // Comparison operators:
98c4eb39 244#define wxDEFINE_UNICHARREF_OPERATOR(op) \
fb52c2b6
VZ
245 bool operator op(const wxUniCharRef& c) const { return UniChar() op c.UniChar(); }\
246 bool operator op(const wxUniChar& c) const { return UniChar() op c; } \
247 bool operator op(char c) const { return UniChar() op c; } \
db18be57 248 bool operator op(unsigned char c) const { return UniChar() op c; } \
21f0762a 249 wxIF_WCHAR_T_TYPE( bool operator op(wchar_t c) const { return UniChar() op c; } ) \
db18be57 250 bool operator op(int c) const { return UniChar() op c; } \
21f0762a
VS
251 bool operator op(unsigned int c) const { return UniChar() op c; } \
252 bool operator op(short int c) const { return UniChar() op c; } \
253 bool operator op(unsigned short int c) const { return UniChar() op c; } \
4e5b3a4e 254 bool operator op(long int c) const { return UniChar() op c; } \
21f0762a 255 bool operator op(unsigned long int c) const { return UniChar() op c; }
e3f6cbd9 256
fb52c2b6 257 wxFOR_ALL_COMPARISONS(wxDEFINE_UNICHARREF_OPERATOR)
e3f6cbd9 258
fb52c2b6 259#undef wxDEFINE_UNICHARREF_OPERATOR
e3f6cbd9
VS
260
261 // for expressions like c-'A':
262 int operator-(const wxUniCharRef& c) const { return UniChar() - c.UniChar(); }
263 int operator-(const wxUniChar& c) const { return UniChar() - c; }
264 int operator-(char c) const { return UniChar() - c; }
db18be57 265 int operator-(unsigned char c) const { return UniChar() - c; }
e3f6cbd9 266 int operator-(wchar_t c) const { return UniChar() - c; }
e3f6cbd9
VS
267
268private:
81727065 269#if wxUSE_UNICODE_UTF8
467175ab 270 wxUniChar UniChar() const;
81727065 271#else
467175ab 272 wxUniChar UniChar() const { return *m_pos; }
81727065
VS
273#endif
274
e3f6cbd9
VS
275 friend class WXDLLIMPEXP_BASE wxUniChar;
276
277private:
81727065
VS
278 // reference to the string and pointer to the character in string
279#if wxUSE_UNICODE_UTF8
b0c4d5d7 280 wxStringIteratorNode& m_node;
81727065 281#endif
a962cdf4 282 iterator m_pos;
e3f6cbd9
VS
283};
284
285inline wxUniChar::wxUniChar(const wxUniCharRef& c)
286{
287 m_value = c.UniChar().m_value;
288}
289
fb52c2b6
VZ
290// Comparison operators for the case when wxUniChar(Ref) is the second operand
291// implemented in terms of member comparison functions
e3f6cbd9 292
fb52c2b6 293#define wxCMP_REVERSE(c1, c2, op) c2 op c1
e3f6cbd9 294
fb52c2b6
VZ
295wxDEFINE_COMPARISONS(char, const wxUniChar&, wxCMP_REVERSE)
296wxDEFINE_COMPARISONS(char, const wxUniCharRef&, wxCMP_REVERSE)
e3f6cbd9 297
fb52c2b6
VZ
298wxDEFINE_COMPARISONS(wchar_t, const wxUniChar&, wxCMP_REVERSE)
299wxDEFINE_COMPARISONS(wchar_t, const wxUniCharRef&, wxCMP_REVERSE)
e3f6cbd9 300
fb52c2b6 301wxDEFINE_COMPARISONS(const wxUniChar&, const wxUniCharRef&, wxCMP_REVERSE)
e3f6cbd9 302
fb52c2b6 303#undef wxCMP_REVERSE
e3f6cbd9
VS
304
305// for expressions like c-'A':
306inline int operator-(char c1, const wxUniCharRef& c2) { return -(c2 - c1); }
e3f6cbd9 307inline int operator-(const wxUniChar& c1, const wxUniCharRef& c2) { return -(c2 - c1); }
436bb9f5 308inline int operator-(wchar_t c1, const wxUniCharRef& c2) { return -(c2 - c1); }
e3f6cbd9
VS
309
310#endif /* _WX_UNICHAR_H_ */