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