]> git.saurik.com Git - wxWidgets.git/blame - src/common/platinfo.cpp
Added pango_context_get_language to speed up pango_context_get_metrics
[wxWidgets.git] / src / common / platinfo.cpp
CommitLineData
8bb6b2c0
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/common/platinfo.cpp
3// Purpose: implements wxPlatformInfo class
4// Author: Francesco Montorsi
5// Modified by:
6// Created: 07.07.2006 (based on wxToolkitInfo)
7// RCS-ID: $Id$
8// Copyright: (c) 2006 Francesco Montorsi
9// License: wxWindows license
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// for compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
f566c8e2
PC
27#include "wx/platinfo.h"
28
8bb6b2c0 29#ifndef WX_PRECOMP
f566c8e2 30 #include "wx/app.h"
8bb6b2c0
VZ
31 #include "wx/utils.h"
32#endif //WX_PRECOMP
33
8bb6b2c0
VZ
34#include "wx/apptrait.h"
35
36// ----------------------------------------------------------------------------
37// constants
38// ----------------------------------------------------------------------------
39
8f493187 40static const wxChar* const wxOperatingSystemIdNames[] =
8bb6b2c0
VZ
41{
42 _T("Apple Mac OS"),
43 _T("Apple Mac OS X"),
44
45 _T("Microsoft Windows 9X"),
46 _T("Microsoft Windows NT"),
47 _T("Microsoft Windows Micro"),
48 _T("Microsoft Windows CE"),
49
50 _T("Linux"),
51 _T("FreeBSD"),
52 _T("OpenBSD"),
53 _T("NetBSD"),
54
55 _T("SunOS"),
56 _T("AIX"),
57 _T("HPUX"),
58
59 _T("DOS"),
60 _T("OS/2")
61};
62
8f493187 63static const wxChar* const wxPortIdNames[] =
8bb6b2c0
VZ
64{
65 _T("wxBase"),
66 _T("wxMSW"),
67 _T("wxMotif"),
68 _T("wxGTK"),
69 _T("wxMGL"),
70 _T("wxX11"),
71 _T("wxOS2"),
72 _T("wxMac"),
73 _T("wxCocoa"),
74 _T("wxWinCE"),
b3c86150
VS
75 _T("wxPalmOS"),
76 _T("wxDFB")
8bb6b2c0
VZ
77};
78
8f493187 79static const wxChar* const wxArchitectureNames[] =
8bb6b2c0
VZ
80{
81 _T("32 bit"),
82 _T("64 bit")
83};
84
8f493187 85static const wxChar* const wxEndiannessNames[] =
8bb6b2c0
VZ
86{
87 _T("Big endian"),
88 _T("Little endian"),
89 _T("PDP endian")
90};
91
92// ----------------------------------------------------------------------------
93// local functions
94// ----------------------------------------------------------------------------
95
96// returns log in base 2 of the value, this maps the enum values to the
97// corresponding indices
78e9b418 98static unsigned wxGetIndexFromEnumValue(int value)
8bb6b2c0 99{
78e9b418 100 wxCHECK_MSG( value, (unsigned)-1, _T("invalid enum value") );
8bb6b2c0
VZ
101
102 int n = 0;
103 while ( !(value & 1) )
104 {
105 value >>= 1;
106 n++;
107 }
108
109 wxASSERT_MSG( value == 1, _T("more than one bit set in enum value") );
110
111 return n;
112}
113
114// ----------------------------------------------------------------------------
115// wxPlatformInfo
116// ----------------------------------------------------------------------------
117
118wxPlatformInfo::wxPlatformInfo()
119{
120 // autodetect all informations
121 const wxAppTraits * const traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
122 if ( !traits )
123 {
124 wxFAIL_MSG( _T("failed to initialize wxPlatformInfo") );
125
126 m_port = wxPORT_UNKNOWN;
b98bd6af 127 m_usingUniversal = false;
8bb6b2c0
VZ
128 m_tkVersionMajor =
129 m_tkVersionMinor = 0;
130 }
131 else
132 {
133 m_port = traits->GetToolkitVersion(&m_tkVersionMajor, &m_tkVersionMinor);
b98bd6af 134 m_usingUniversal = traits->IsUsingUniversalWidgets();
8bb6b2c0
VZ
135 }
136
137 m_os = wxGetOsVersion(&m_osVersionMajor, &m_osVersionMinor);
138 m_endian = wxIsPlatformLittleEndian() ? wxENDIAN_LITTLE : wxENDIAN_BIG;
139 m_arch = wxIsPlatform64Bit() ? wxARCH_64 : wxARCH_32;
140}
141
142wxPlatformInfo::wxPlatformInfo(wxPortId pid, int tkMajor, int tkMinor,
143 wxOperatingSystemId id, int osMajor, int osMinor,
144 wxArchitecture arch,
b98bd6af
VS
145 wxEndianness endian,
146 bool usingUniversal)
8bb6b2c0
VZ
147{
148 m_tkVersionMajor = tkMajor;
149 m_tkVersionMinor = tkMinor;
150 m_port = pid;
b98bd6af 151 m_usingUniversal = usingUniversal;
8bb6b2c0
VZ
152
153 m_os = id;
154 m_osVersionMajor = osMajor;
155 m_osVersionMinor = osMinor;
156
157 m_endian = endian;
158 m_arch = arch;
159}
160
161bool wxPlatformInfo::operator==(const wxPlatformInfo &t) const
162{
163 return m_tkVersionMajor == t.m_tkVersionMajor &&
164 m_tkVersionMinor == t.m_tkVersionMinor &&
165 m_osVersionMajor == t.m_osVersionMajor &&
166 m_osVersionMinor == t.m_osVersionMinor &&
167 m_os == t.m_os &&
168 m_port == t.m_port &&
b98bd6af 169 m_usingUniversal == t.m_usingUniversal &&
8bb6b2c0
VZ
170 m_arch == t.m_arch &&
171 m_endian == t.m_endian;
172}
173
174// ----------------------------------------------------------------------------
175// wxPlatformInfo - enum -> string conversions
176// ----------------------------------------------------------------------------
177
178wxString wxPlatformInfo::GetOperatingSystemFamilyName(wxOperatingSystemId os)
179{
8f493187 180 const wxChar* string = _T("Unknown");
8bb6b2c0 181 if ( os & wxOS_MAC )
8f493187 182 string = _T("Macintosh");
8bb6b2c0 183 else if ( os & wxOS_WINDOWS )
8f493187 184 string = _T("Windows");
8bb6b2c0 185 else if ( os & wxOS_UNIX )
8f493187 186 string = _T("Unix");
8bb6b2c0 187 else if ( os == wxOS_DOS )
8f493187 188 string = _T("DOS");
8bb6b2c0 189 else if ( os == wxOS_OS2 )
8f493187 190 string = _T("OS/2");
8bb6b2c0 191
8f493187 192 return string;
8bb6b2c0
VZ
193}
194
195wxString wxPlatformInfo::GetOperatingSystemIdName(wxOperatingSystemId os)
196{
78e9b418 197 const unsigned idx = wxGetIndexFromEnumValue(os);
8bb6b2c0
VZ
198
199 wxCHECK_MSG( idx < WXSIZEOF(wxOperatingSystemIdNames), wxEmptyString,
200 _T("invalid OS id") );
201
202 return wxOperatingSystemIdNames[idx];
203}
204
b98bd6af 205wxString wxPlatformInfo::GetPortIdName(wxPortId port, bool usingUniversal)
8bb6b2c0 206{
78e9b418 207 const unsigned idx = wxGetIndexFromEnumValue(port);
8bb6b2c0
VZ
208
209 wxCHECK_MSG( idx < WXSIZEOF(wxPortIdNames), wxEmptyString,
210 _T("invalid port id") );
211
212 wxString ret = wxPortIdNames[idx];
213
b98bd6af 214 if ( usingUniversal )
8bb6b2c0
VZ
215 ret += wxT("/wxUniversal");
216
217 return ret;
218}
219
b98bd6af 220wxString wxPlatformInfo::GetPortIdShortName(wxPortId port, bool usingUniversal)
8bb6b2c0 221{
78e9b418 222 const unsigned idx = wxGetIndexFromEnumValue(port);
8bb6b2c0
VZ
223
224 wxCHECK_MSG( idx < WXSIZEOF(wxPortIdNames), wxEmptyString,
225 _T("invalid port id") );
226
227 wxString ret = wxPortIdNames[idx];
228 ret = ret.Mid(2).Lower(); // remove 'wx' prefix
229
b98bd6af 230 if ( usingUniversal )
8bb6b2c0
VZ
231 ret += wxT("univ");
232
233 return ret;
234}
235
236wxString wxPlatformInfo::GetArchName(wxArchitecture arch)
237{
238 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxArchitectureNames) == wxARCH_MAX,
239 wxArchitectureNamesMismatch );
240
241 return wxArchitectureNames[arch];
242}
243
244wxString wxPlatformInfo::GetEndiannessName(wxEndianness end)
245{
246 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxEndiannessNames) == wxENDIAN_MAX,
247 wxEndiannessNamesMismatch );
248
249 return wxEndiannessNames[end];
250}
251
252
253// ----------------------------------------------------------------------------
254// wxPlatformInfo - string -> enum conversions
255// ----------------------------------------------------------------------------
256
257wxOperatingSystemId wxPlatformInfo::GetOperatingSystemId(const wxString &str)
258{
259 for ( size_t i = 0; i < WXSIZEOF(wxOperatingSystemIdNames); i++ )
260 {
8f493187 261 if ( wxString(wxOperatingSystemIdNames[i]).CmpNoCase(str) == 0 )
8bb6b2c0
VZ
262 return (wxOperatingSystemId)(1 << i);
263 }
264
265 return wxOS_UNKNOWN;
266}
267
268wxPortId wxPlatformInfo::GetPortId(const wxString &str)
269{
270 // recognize both short and long port names
271 for ( size_t i = 0; i < WXSIZEOF(wxPortIdNames); i++ )
272 {
273 wxPortId current = (wxPortId)(1 << i);
274
8f493187
PC
275 if ( wxString(wxPortIdNames[i]).CmpNoCase(str) == 0 ||
276 GetPortIdShortName(current, true).CmpNoCase(str) == 0 ||
b98bd6af 277 GetPortIdShortName(current, false).CmpNoCase(str) == 0 )
8bb6b2c0
VZ
278 return current;
279 }
280
281 return wxPORT_UNKNOWN;
282}
283
284wxArchitecture wxPlatformInfo::GetArch(const wxString &arch)
285{
286 if ( arch.Contains(wxT("32")) )
287 return wxARCH_32;
288
289 if ( arch.Contains(wxT("64")) )
290 return wxARCH_64;
291
292 return wxARCH_INVALID;
293}
294
295wxEndianness wxPlatformInfo::GetEndianness(const wxString& end)
296{
297 wxString endl(end.Lower());
298 if ( end.StartsWith(wxT("little")) )
299 return wxENDIAN_LITTLE;
300
301 if ( end.StartsWith(wxT("big")) )
302 return wxENDIAN_BIG;
303
304 return wxENDIAN_INVALID;
305}
306