]> git.saurik.com Git - wxWidgets.git/blame - src/common/platinfo.cpp
Forgot this
[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
449090b5
VZ
36// global object
37// VERY IMPORTANT: do not use the default constructor since it would
38// try to init the wxPlatformInfo instance using
39// gs_platInfo itself!
40static wxPlatformInfo gs_platInfo(wxPORT_UNKNOWN);
41
8bb6b2c0
VZ
42// ----------------------------------------------------------------------------
43// constants
44// ----------------------------------------------------------------------------
45
8f493187 46static const wxChar* const wxOperatingSystemIdNames[] =
8bb6b2c0
VZ
47{
48 _T("Apple Mac OS"),
49 _T("Apple Mac OS X"),
50
51 _T("Microsoft Windows 9X"),
52 _T("Microsoft Windows NT"),
53 _T("Microsoft Windows Micro"),
54 _T("Microsoft Windows CE"),
55
56 _T("Linux"),
57 _T("FreeBSD"),
58 _T("OpenBSD"),
59 _T("NetBSD"),
60
61 _T("SunOS"),
62 _T("AIX"),
63 _T("HPUX"),
64
7528971e
SN
65 _T("Other Unix"),
66 _T("Other Unix"),
67
8bb6b2c0 68 _T("DOS"),
027fe16c
VZ
69 _T("OS/2"),
70
71 _T("PalmOS"),
72 _T("PalmOS(Over Linux)"),
8bb6b2c0
VZ
73};
74
8f493187 75static const wxChar* const wxPortIdNames[] =
8bb6b2c0
VZ
76{
77 _T("wxBase"),
78 _T("wxMSW"),
79 _T("wxMotif"),
80 _T("wxGTK"),
81 _T("wxMGL"),
82 _T("wxX11"),
83 _T("wxOS2"),
84 _T("wxMac"),
85 _T("wxCocoa"),
86 _T("wxWinCE"),
b3c86150
VS
87 _T("wxPalmOS"),
88 _T("wxDFB")
8bb6b2c0
VZ
89};
90
8f493187 91static const wxChar* const wxArchitectureNames[] =
8bb6b2c0
VZ
92{
93 _T("32 bit"),
94 _T("64 bit")
95};
96
8f493187 97static const wxChar* const wxEndiannessNames[] =
8bb6b2c0
VZ
98{
99 _T("Big endian"),
100 _T("Little endian"),
101 _T("PDP endian")
102};
103
104// ----------------------------------------------------------------------------
105// local functions
106// ----------------------------------------------------------------------------
107
108// returns log in base 2 of the value, this maps the enum values to the
109// corresponding indices
78e9b418 110static unsigned wxGetIndexFromEnumValue(int value)
8bb6b2c0 111{
78e9b418 112 wxCHECK_MSG( value, (unsigned)-1, _T("invalid enum value") );
8bb6b2c0
VZ
113
114 int n = 0;
115 while ( !(value & 1) )
116 {
117 value >>= 1;
118 n++;
119 }
120
121 wxASSERT_MSG( value == 1, _T("more than one bit set in enum value") );
122
123 return n;
124}
125
126// ----------------------------------------------------------------------------
127// wxPlatformInfo
128// ----------------------------------------------------------------------------
129
130wxPlatformInfo::wxPlatformInfo()
131{
449090b5
VZ
132 // just copy platform info for currently running platform
133 *this = Get();
8bb6b2c0
VZ
134}
135
136wxPlatformInfo::wxPlatformInfo(wxPortId pid, int tkMajor, int tkMinor,
137 wxOperatingSystemId id, int osMajor, int osMinor,
138 wxArchitecture arch,
b98bd6af
VS
139 wxEndianness endian,
140 bool usingUniversal)
8bb6b2c0
VZ
141{
142 m_tkVersionMajor = tkMajor;
143 m_tkVersionMinor = tkMinor;
144 m_port = pid;
b98bd6af 145 m_usingUniversal = usingUniversal;
8bb6b2c0
VZ
146
147 m_os = id;
148 m_osVersionMajor = osMajor;
149 m_osVersionMinor = osMinor;
150
151 m_endian = endian;
152 m_arch = arch;
153}
154
155bool wxPlatformInfo::operator==(const wxPlatformInfo &t) const
156{
157 return m_tkVersionMajor == t.m_tkVersionMajor &&
158 m_tkVersionMinor == t.m_tkVersionMinor &&
159 m_osVersionMajor == t.m_osVersionMajor &&
160 m_osVersionMinor == t.m_osVersionMinor &&
161 m_os == t.m_os &&
162 m_port == t.m_port &&
b98bd6af 163 m_usingUniversal == t.m_usingUniversal &&
8bb6b2c0
VZ
164 m_arch == t.m_arch &&
165 m_endian == t.m_endian;
166}
167
449090b5
VZ
168void wxPlatformInfo::InitForCurrentPlatform()
169{
170 // autodetect all informations
171 const wxAppTraits * const traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
172 if ( !traits )
173 {
174 wxFAIL_MSG( _T("failed to initialize wxPlatformInfo") );
175
176 m_port = wxPORT_UNKNOWN;
177 m_usingUniversal = false;
178 m_tkVersionMajor =
179 m_tkVersionMinor = 0;
180 }
181 else
182 {
183 m_port = traits->GetToolkitVersion(&m_tkVersionMajor, &m_tkVersionMinor);
184 m_usingUniversal = traits->IsUsingUniversalWidgets();
185 }
186
187 m_os = wxGetOsVersion(&m_osVersionMajor, &m_osVersionMinor);
188 m_endian = wxIsPlatformLittleEndian() ? wxENDIAN_LITTLE : wxENDIAN_BIG;
189 m_arch = wxIsPlatform64Bit() ? wxARCH_64 : wxARCH_32;
190}
191
192/* static */
193const wxPlatformInfo& wxPlatformInfo::Get()
194{
195 static bool initialized = false;
196 if ( !initialized )
197 {
198 gs_platInfo.InitForCurrentPlatform();
199 initialized = true;
200 }
201
202 return gs_platInfo;
203}
204
205
206
8bb6b2c0
VZ
207// ----------------------------------------------------------------------------
208// wxPlatformInfo - enum -> string conversions
209// ----------------------------------------------------------------------------
210
211wxString wxPlatformInfo::GetOperatingSystemFamilyName(wxOperatingSystemId os)
212{
8f493187 213 const wxChar* string = _T("Unknown");
8bb6b2c0 214 if ( os & wxOS_MAC )
8f493187 215 string = _T("Macintosh");
8bb6b2c0 216 else if ( os & wxOS_WINDOWS )
8f493187 217 string = _T("Windows");
8bb6b2c0 218 else if ( os & wxOS_UNIX )
8f493187 219 string = _T("Unix");
8bb6b2c0 220 else if ( os == wxOS_DOS )
8f493187 221 string = _T("DOS");
8bb6b2c0 222 else if ( os == wxOS_OS2 )
8f493187 223 string = _T("OS/2");
8bb6b2c0 224
8f493187 225 return string;
8bb6b2c0
VZ
226}
227
228wxString wxPlatformInfo::GetOperatingSystemIdName(wxOperatingSystemId os)
229{
78e9b418 230 const unsigned idx = wxGetIndexFromEnumValue(os);
8bb6b2c0
VZ
231
232 wxCHECK_MSG( idx < WXSIZEOF(wxOperatingSystemIdNames), wxEmptyString,
233 _T("invalid OS id") );
234
235 return wxOperatingSystemIdNames[idx];
236}
237
b98bd6af 238wxString wxPlatformInfo::GetPortIdName(wxPortId port, bool usingUniversal)
8bb6b2c0 239{
78e9b418 240 const unsigned idx = wxGetIndexFromEnumValue(port);
8bb6b2c0
VZ
241
242 wxCHECK_MSG( idx < WXSIZEOF(wxPortIdNames), wxEmptyString,
243 _T("invalid port id") );
244
245 wxString ret = wxPortIdNames[idx];
246
b98bd6af 247 if ( usingUniversal )
8bb6b2c0
VZ
248 ret += wxT("/wxUniversal");
249
250 return ret;
251}
252
b98bd6af 253wxString wxPlatformInfo::GetPortIdShortName(wxPortId port, bool usingUniversal)
8bb6b2c0 254{
78e9b418 255 const unsigned idx = wxGetIndexFromEnumValue(port);
8bb6b2c0
VZ
256
257 wxCHECK_MSG( idx < WXSIZEOF(wxPortIdNames), wxEmptyString,
258 _T("invalid port id") );
259
260 wxString ret = wxPortIdNames[idx];
261 ret = ret.Mid(2).Lower(); // remove 'wx' prefix
262
b98bd6af 263 if ( usingUniversal )
8bb6b2c0
VZ
264 ret += wxT("univ");
265
266 return ret;
267}
268
269wxString wxPlatformInfo::GetArchName(wxArchitecture arch)
270{
271 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxArchitectureNames) == wxARCH_MAX,
272 wxArchitectureNamesMismatch );
273
274 return wxArchitectureNames[arch];
275}
276
277wxString wxPlatformInfo::GetEndiannessName(wxEndianness end)
278{
279 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxEndiannessNames) == wxENDIAN_MAX,
280 wxEndiannessNamesMismatch );
281
282 return wxEndiannessNames[end];
283}
284
285
286// ----------------------------------------------------------------------------
287// wxPlatformInfo - string -> enum conversions
288// ----------------------------------------------------------------------------
289
290wxOperatingSystemId wxPlatformInfo::GetOperatingSystemId(const wxString &str)
291{
292 for ( size_t i = 0; i < WXSIZEOF(wxOperatingSystemIdNames); i++ )
293 {
8f493187 294 if ( wxString(wxOperatingSystemIdNames[i]).CmpNoCase(str) == 0 )
8bb6b2c0
VZ
295 return (wxOperatingSystemId)(1 << i);
296 }
297
298 return wxOS_UNKNOWN;
299}
300
301wxPortId wxPlatformInfo::GetPortId(const wxString &str)
302{
303 // recognize both short and long port names
304 for ( size_t i = 0; i < WXSIZEOF(wxPortIdNames); i++ )
305 {
306 wxPortId current = (wxPortId)(1 << i);
307
8f493187
PC
308 if ( wxString(wxPortIdNames[i]).CmpNoCase(str) == 0 ||
309 GetPortIdShortName(current, true).CmpNoCase(str) == 0 ||
b98bd6af 310 GetPortIdShortName(current, false).CmpNoCase(str) == 0 )
8bb6b2c0
VZ
311 return current;
312 }
313
314 return wxPORT_UNKNOWN;
315}
316
317wxArchitecture wxPlatformInfo::GetArch(const wxString &arch)
318{
319 if ( arch.Contains(wxT("32")) )
320 return wxARCH_32;
321
322 if ( arch.Contains(wxT("64")) )
323 return wxARCH_64;
324
325 return wxARCH_INVALID;
326}
327
328wxEndianness wxPlatformInfo::GetEndianness(const wxString& end)
329{
523a54d9
VZ
330 const wxString endl(end.Lower());
331 if ( endl.StartsWith(wxT("little")) )
8bb6b2c0
VZ
332 return wxENDIAN_LITTLE;
333
523a54d9 334 if ( endl.StartsWith(wxT("big")) )
8bb6b2c0
VZ
335 return wxENDIAN_BIG;
336
337 return wxENDIAN_INVALID;
338}
339