]> git.saurik.com Git - wxWidgets.git/blame - src/common/xlocale.cpp
Correct weak ref usage which triggered an assert
[wxWidgets.git] / src / common / xlocale.cpp
CommitLineData
6e4ae332
VZ
1//////////////////////////////////////////////////////////////////////////////
2// Name: src/common/xlocale.cpp
3// Purpose: xlocale wrappers/impl to provide some xlocale wrappers
4// Author: Brian Vanderburg II, Vadim Zeitlin
5// Created: 2008-01-07
6// RCS-ID: $Id$
7// Copyright: (c) 2008 Brian Vanderburg II
8// 2008 Vadim Zeitlin <vadim@wxwidgets.org>
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#if wxUSE_XLOCALE
27
28#ifndef WX_PRECOMP
29 #include "wx/module.h"
30#endif
31
32#include "wx/xlocale.h"
33
34// ----------------------------------------------------------------------------
35// module globals
36// ----------------------------------------------------------------------------
37
38// This is the C locale object, it is created on demand
39static wxXLocale *gs_cLocale = NULL;
40
41// ============================================================================
42// implementation
43// ============================================================================
44
45// ----------------------------------------------------------------------------
46// Module for gs_cLocale cleanup
47// ----------------------------------------------------------------------------
48
49class wxXLocaleModule : public wxModule
50{
51public:
52 virtual bool OnInit() { return true; }
53 virtual void OnExit() { wxDELETE(gs_cLocale); }
54
55 DECLARE_DYNAMIC_CLASS(wxXLocaleModule)
56};
57
58IMPLEMENT_DYNAMIC_CLASS(wxXLocaleModule, wxModule)
59
60
61// ============================================================================
62// wxXLocale implementation
63// ============================================================================
64
65// ----------------------------------------------------------------------------
66// common parts
67// ----------------------------------------------------------------------------
68
69// Get the C locale
70wxXLocale& wxXLocale::GetCLocale()
71{
72 if ( !gs_cLocale )
73 {
74 gs_cLocale = new wxXLocale(wx_static_cast(wxXLocaleCTag *, NULL));
75 }
76
77 return *gs_cLocale;
78}
79
45f5fbd4
VZ
80#ifdef wxHAS_XLOCALE_SUPPORT
81
6e4ae332
VZ
82wxXLocale::wxXLocale(wxLanguage lang)
83{
84 const wxLanguageInfo * const info = wxLocale::GetLanguageInfo(lang);
85 if ( !info )
86 {
87 m_locale = NULL;
88 }
89 else
90 {
e340b786 91 Init(info->GetLocaleName().c_str());
6e4ae332
VZ
92 }
93}
94
6e4ae332
VZ
95#if wxCHECK_VISUALC_VERSION(8)
96
97// ----------------------------------------------------------------------------
98// implementation using MSVC locale API
99// ----------------------------------------------------------------------------
100
101void wxXLocale::Init(const char *loc)
102{
103 m_locale = _create_locale(LC_ALL, loc);
104}
105
106void wxXLocale::Free()
107{
108 if ( m_locale )
109 _free_locale(m_locale);
110}
111
112#elif defined(HAVE_LOCALE_T)
113
114// ----------------------------------------------------------------------------
115// implementation using xlocale API
116// ----------------------------------------------------------------------------
117
118void wxXLocale::Init(const char *loc)
119{
120 m_locale = newlocale(LC_ALL_MASK, loc, NULL);
121}
122
123void wxXLocale::Free()
124{
125 if ( m_locale )
126 freelocale(m_locale);
127}
128
129#else
130 #error "Unknown xlocale support."
131#endif
132
133#endif // wxHAS_XLOCALE_SUPPORT
134
135#ifndef wxHAS_XLOCALE_SUPPORT
136
137// ============================================================================
138// Implementation of wxFoo_l() functions for "C" locale without xlocale support
139// ============================================================================
140
141// ----------------------------------------------------------------------------
142// character classification and transformation functions
143// ----------------------------------------------------------------------------
144
145// lookup table and macros for character type functions
146#define CTYPE_ALNUM 0x0001
147#define CTYPE_ALPHA 0x0002
148#define CTYPE_CNTRL 0x0004
149#define CTYPE_DIGIT 0x0008
150#define CTYPE_GRAPH 0x0010
151#define CTYPE_LOWER 0x0020
152#define CTYPE_PRINT 0x0040
153#define CTYPE_PUNCT 0x0080
154#define CTYPE_SPACE 0x0100
155#define CTYPE_UPPER 0x0200
156#define CTYPE_XDIGIT 0x0400
157
158static unsigned int gs_lookup[] =
159{
160 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004,
161 0x0004, 0x0104, 0x0104, 0x0104, 0x0104, 0x0104, 0x0004, 0x0004,
162 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004,
163 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004,
164 0x0140, 0x00D0, 0x00D0, 0x00D0, 0x00D0, 0x00D0, 0x00D0, 0x00D0,
165 0x00D0, 0x00D0, 0x00D0, 0x00D0, 0x00D0, 0x00D0, 0x00D0, 0x00D0,
166 0x0459, 0x0459, 0x0459, 0x0459, 0x0459, 0x0459, 0x0459, 0x0459,
167 0x0459, 0x0459, 0x00D0, 0x00D0, 0x00D0, 0x00D0, 0x00D0, 0x00D0,
168 0x00D0, 0x0653, 0x0653, 0x0653, 0x0653, 0x0653, 0x0653, 0x0253,
169 0x0253, 0x0253, 0x0253, 0x0253, 0x0253, 0x0253, 0x0253, 0x0253,
170 0x0253, 0x0253, 0x0253, 0x0253, 0x0253, 0x0253, 0x0253, 0x0253,
171 0x0253, 0x0253, 0x0253, 0x00D0, 0x00D0, 0x00D0, 0x00D0, 0x00D0,
172 0x00D0, 0x0473, 0x0473, 0x0473, 0x0473, 0x0473, 0x0473, 0x0073,
173 0x0073, 0x0073, 0x0073, 0x0073, 0x0073, 0x0073, 0x0073, 0x0073,
174 0x0073, 0x0073, 0x0073, 0x0073, 0x0073, 0x0073, 0x0073, 0x0073,
175 0x0073, 0x0073, 0x0073, 0x00D0, 0x00D0, 0x00D0, 0x00D0, 0x0004
176};
177
178
179#define CTYPE_TEST(c, t) ( (c) <= 127 && (gs_lookup[(c)] & (t)) )
180
181
182// ctype functions
183#define GEN_ISFUNC(name, test) \
184int name(const wxUniChar& c, const wxXLocale& loc) \
185{ \
186 wxCHECK(loc.IsOk(), false); \
187 return CTYPE_TEST(c.GetValue(), test); \
188}
189
190GEN_ISFUNC(wxIsalnum_l, CTYPE_ALNUM)
191GEN_ISFUNC(wxIsalpha_l, CTYPE_ALPHA)
192GEN_ISFUNC(wxIscntrl_l, CTYPE_CNTRL)
193GEN_ISFUNC(wxIsdigit_l, CTYPE_DIGIT)
194GEN_ISFUNC(wxIsgraph_l, CTYPE_GRAPH)
195GEN_ISFUNC(wxIslower_l, CTYPE_LOWER)
196GEN_ISFUNC(wxIsprint_l, CTYPE_PRINT)
197GEN_ISFUNC(wxIspunct_l, CTYPE_PUNCT)
198GEN_ISFUNC(wxIsspace_l, CTYPE_SPACE)
199GEN_ISFUNC(wxIsupper_l, CTYPE_UPPER)
200GEN_ISFUNC(wxIsxdigit_l, CTYPE_XDIGIT)
201
a4e53d46 202int wxTolower_l(const wxUniChar& c, const wxXLocale& loc)
6e4ae332
VZ
203{
204 wxCHECK(loc.IsOk(), false);
205
45f5fbd4 206 if(CTYPE_TEST(c.GetValue(), CTYPE_UPPER))
6e4ae332
VZ
207 {
208 return c - 'A' + 'a';
209 }
210
211 return c;
212}
213
a4e53d46 214int wxToupper_l(const wxUniChar& c, const wxXLocale& loc)
6e4ae332
VZ
215{
216 wxCHECK(loc.IsOk(), false);
217
45f5fbd4 218 if(CTYPE_TEST(c.GetValue(), CTYPE_LOWER))
6e4ae332
VZ
219 {
220 return c - 'a' + 'A';
221 }
222
223 return c;
224}
225
226#endif // !defined(wxHAS_XLOCALE_SUPPORT)
227
228#endif // wxUSE_XLOCALE