1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/platinfo.cpp
3 // Purpose: implements wxPlatformInfo class
4 // Author: Francesco Montorsi
6 // Created: 07.07.2006 (based on wxToolkitInfo)
8 // Copyright: (c) 2006 Francesco Montorsi
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/platinfo.h"
34 #include "wx/apptrait.h"
37 // VERY IMPORTANT: do not use the default constructor since it would
38 // try to init the wxPlatformInfo instance using
39 // gs_platInfo itself!
40 static wxPlatformInfo
gs_platInfo(wxPORT_UNKNOWN
);
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 static const wxChar
* const wxOperatingSystemIdNames
[] =
51 _T("Microsoft Windows 9X"),
52 _T("Microsoft Windows NT"),
53 _T("Microsoft Windows Micro"),
54 _T("Microsoft Windows CE"),
69 static const wxChar
* const wxPortIdNames
[] =
85 static const wxChar
* const wxArchitectureNames
[] =
91 static const wxChar
* const wxEndiannessNames
[] =
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 // returns log in base 2 of the value, this maps the enum values to the
103 // corresponding indices
104 static unsigned wxGetIndexFromEnumValue(int value
)
106 wxCHECK_MSG( value
, (unsigned)-1, _T("invalid enum value") );
109 while ( !(value
& 1) )
115 wxASSERT_MSG( value
== 1, _T("more than one bit set in enum value") );
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 wxPlatformInfo::wxPlatformInfo()
126 // just copy platform info for currently running platform
130 wxPlatformInfo::wxPlatformInfo(wxPortId pid
, int tkMajor
, int tkMinor
,
131 wxOperatingSystemId id
, int osMajor
, int osMinor
,
136 m_tkVersionMajor
= tkMajor
;
137 m_tkVersionMinor
= tkMinor
;
139 m_usingUniversal
= usingUniversal
;
142 m_osVersionMajor
= osMajor
;
143 m_osVersionMinor
= osMinor
;
149 bool wxPlatformInfo::operator==(const wxPlatformInfo
&t
) const
151 return m_tkVersionMajor
== t
.m_tkVersionMajor
&&
152 m_tkVersionMinor
== t
.m_tkVersionMinor
&&
153 m_osVersionMajor
== t
.m_osVersionMajor
&&
154 m_osVersionMinor
== t
.m_osVersionMinor
&&
156 m_port
== t
.m_port
&&
157 m_usingUniversal
== t
.m_usingUniversal
&&
158 m_arch
== t
.m_arch
&&
159 m_endian
== t
.m_endian
;
162 void wxPlatformInfo::InitForCurrentPlatform()
164 // autodetect all informations
165 const wxAppTraits
* const traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
168 wxFAIL_MSG( _T("failed to initialize wxPlatformInfo") );
170 m_port
= wxPORT_UNKNOWN
;
171 m_usingUniversal
= false;
173 m_tkVersionMinor
= 0;
177 m_port
= traits
->GetToolkitVersion(&m_tkVersionMajor
, &m_tkVersionMinor
);
178 m_usingUniversal
= traits
->IsUsingUniversalWidgets();
181 m_os
= wxGetOsVersion(&m_osVersionMajor
, &m_osVersionMinor
);
182 m_endian
= wxIsPlatformLittleEndian() ? wxENDIAN_LITTLE
: wxENDIAN_BIG
;
183 m_arch
= wxIsPlatform64Bit() ? wxARCH_64
: wxARCH_32
;
187 const wxPlatformInfo
& wxPlatformInfo::Get()
189 static bool initialized
= false;
192 gs_platInfo
.InitForCurrentPlatform();
201 // ----------------------------------------------------------------------------
202 // wxPlatformInfo - enum -> string conversions
203 // ----------------------------------------------------------------------------
205 wxString
wxPlatformInfo::GetOperatingSystemFamilyName(wxOperatingSystemId os
)
207 const wxChar
* string
= _T("Unknown");
209 string
= _T("Macintosh");
210 else if ( os
& wxOS_WINDOWS
)
211 string
= _T("Windows");
212 else if ( os
& wxOS_UNIX
)
214 else if ( os
== wxOS_DOS
)
216 else if ( os
== wxOS_OS2
)
222 wxString
wxPlatformInfo::GetOperatingSystemIdName(wxOperatingSystemId os
)
224 const unsigned idx
= wxGetIndexFromEnumValue(os
);
226 wxCHECK_MSG( idx
< WXSIZEOF(wxOperatingSystemIdNames
), wxEmptyString
,
227 _T("invalid OS id") );
229 return wxOperatingSystemIdNames
[idx
];
232 wxString
wxPlatformInfo::GetPortIdName(wxPortId port
, bool usingUniversal
)
234 const unsigned idx
= wxGetIndexFromEnumValue(port
);
236 wxCHECK_MSG( idx
< WXSIZEOF(wxPortIdNames
), wxEmptyString
,
237 _T("invalid port id") );
239 wxString ret
= wxPortIdNames
[idx
];
241 if ( usingUniversal
)
242 ret
+= wxT("/wxUniversal");
247 wxString
wxPlatformInfo::GetPortIdShortName(wxPortId port
, bool usingUniversal
)
249 const unsigned idx
= wxGetIndexFromEnumValue(port
);
251 wxCHECK_MSG( idx
< WXSIZEOF(wxPortIdNames
), wxEmptyString
,
252 _T("invalid port id") );
254 wxString ret
= wxPortIdNames
[idx
];
255 ret
= ret
.Mid(2).Lower(); // remove 'wx' prefix
257 if ( usingUniversal
)
263 wxString
wxPlatformInfo::GetArchName(wxArchitecture arch
)
265 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxArchitectureNames
) == wxARCH_MAX
,
266 wxArchitectureNamesMismatch
);
268 return wxArchitectureNames
[arch
];
271 wxString
wxPlatformInfo::GetEndiannessName(wxEndianness end
)
273 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxEndiannessNames
) == wxENDIAN_MAX
,
274 wxEndiannessNamesMismatch
);
276 return wxEndiannessNames
[end
];
280 // ----------------------------------------------------------------------------
281 // wxPlatformInfo - string -> enum conversions
282 // ----------------------------------------------------------------------------
284 wxOperatingSystemId
wxPlatformInfo::GetOperatingSystemId(const wxString
&str
)
286 for ( size_t i
= 0; i
< WXSIZEOF(wxOperatingSystemIdNames
); i
++ )
288 if ( wxString(wxOperatingSystemIdNames
[i
]).CmpNoCase(str
) == 0 )
289 return (wxOperatingSystemId
)(1 << i
);
295 wxPortId
wxPlatformInfo::GetPortId(const wxString
&str
)
297 // recognize both short and long port names
298 for ( size_t i
= 0; i
< WXSIZEOF(wxPortIdNames
); i
++ )
300 wxPortId current
= (wxPortId
)(1 << i
);
302 if ( wxString(wxPortIdNames
[i
]).CmpNoCase(str
) == 0 ||
303 GetPortIdShortName(current
, true).CmpNoCase(str
) == 0 ||
304 GetPortIdShortName(current
, false).CmpNoCase(str
) == 0 )
308 return wxPORT_UNKNOWN
;
311 wxArchitecture
wxPlatformInfo::GetArch(const wxString
&arch
)
313 if ( arch
.Contains(wxT("32")) )
316 if ( arch
.Contains(wxT("64")) )
319 return wxARCH_INVALID
;
322 wxEndianness
wxPlatformInfo::GetEndianness(const wxString
& end
)
324 wxString
endl(end
.Lower());
325 if ( end
.StartsWith(wxT("little")) )
326 return wxENDIAN_LITTLE
;
328 if ( end
.StartsWith(wxT("big")) )
331 return wxENDIAN_INVALID
;