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