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"),
72 static const wxChar
* const wxPortIdNames
[] =
88 static const wxChar
* const wxArchitectureNames
[] =
94 static const wxChar
* const wxEndiannessNames
[] =
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 // returns log in base 2 of the value, this maps the enum values to the
106 // corresponding indices
107 static unsigned wxGetIndexFromEnumValue(int value
)
109 wxCHECK_MSG( value
, (unsigned)-1, _T("invalid enum value") );
112 while ( !(value
& 1) )
118 wxASSERT_MSG( value
== 1, _T("more than one bit set in enum value") );
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 wxPlatformInfo::wxPlatformInfo()
129 // just copy platform info for currently running platform
133 wxPlatformInfo::wxPlatformInfo(wxPortId pid
, int tkMajor
, int tkMinor
,
134 wxOperatingSystemId id
, int osMajor
, int osMinor
,
139 m_tkVersionMajor
= tkMajor
;
140 m_tkVersionMinor
= tkMinor
;
142 m_usingUniversal
= usingUniversal
;
145 m_osVersionMajor
= osMajor
;
146 m_osVersionMinor
= osMinor
;
152 bool wxPlatformInfo::operator==(const wxPlatformInfo
&t
) const
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
&&
159 m_port
== t
.m_port
&&
160 m_usingUniversal
== t
.m_usingUniversal
&&
161 m_arch
== t
.m_arch
&&
162 m_endian
== t
.m_endian
;
165 void wxPlatformInfo::InitForCurrentPlatform()
167 // autodetect all informations
168 const wxAppTraits
* const traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
171 wxFAIL_MSG( _T("failed to initialize wxPlatformInfo") );
173 m_port
= wxPORT_UNKNOWN
;
174 m_usingUniversal
= false;
176 m_tkVersionMinor
= 0;
180 m_port
= traits
->GetToolkitVersion(&m_tkVersionMajor
, &m_tkVersionMinor
);
181 m_usingUniversal
= traits
->IsUsingUniversalWidgets();
184 m_os
= wxGetOsVersion(&m_osVersionMajor
, &m_osVersionMinor
);
185 m_endian
= wxIsPlatformLittleEndian() ? wxENDIAN_LITTLE
: wxENDIAN_BIG
;
186 m_arch
= wxIsPlatform64Bit() ? wxARCH_64
: wxARCH_32
;
190 const wxPlatformInfo
& wxPlatformInfo::Get()
192 static bool initialized
= false;
195 gs_platInfo
.InitForCurrentPlatform();
204 // ----------------------------------------------------------------------------
205 // wxPlatformInfo - enum -> string conversions
206 // ----------------------------------------------------------------------------
208 wxString
wxPlatformInfo::GetOperatingSystemFamilyName(wxOperatingSystemId os
)
210 const wxChar
* string
= _T("Unknown");
212 string
= _T("Macintosh");
213 else if ( os
& wxOS_WINDOWS
)
214 string
= _T("Windows");
215 else if ( os
& wxOS_UNIX
)
217 else if ( os
== wxOS_DOS
)
219 else if ( os
== wxOS_OS2
)
225 wxString
wxPlatformInfo::GetOperatingSystemIdName(wxOperatingSystemId os
)
227 const unsigned idx
= wxGetIndexFromEnumValue(os
);
229 wxCHECK_MSG( idx
< WXSIZEOF(wxOperatingSystemIdNames
), wxEmptyString
,
230 _T("invalid OS id") );
232 return wxOperatingSystemIdNames
[idx
];
235 wxString
wxPlatformInfo::GetPortIdName(wxPortId port
, bool usingUniversal
)
237 const unsigned idx
= wxGetIndexFromEnumValue(port
);
239 wxCHECK_MSG( idx
< WXSIZEOF(wxPortIdNames
), wxEmptyString
,
240 _T("invalid port id") );
242 wxString ret
= wxPortIdNames
[idx
];
244 if ( usingUniversal
)
245 ret
+= wxT("/wxUniversal");
250 wxString
wxPlatformInfo::GetPortIdShortName(wxPortId port
, bool usingUniversal
)
252 const unsigned idx
= wxGetIndexFromEnumValue(port
);
254 wxCHECK_MSG( idx
< WXSIZEOF(wxPortIdNames
), wxEmptyString
,
255 _T("invalid port id") );
257 wxString ret
= wxPortIdNames
[idx
];
258 ret
= ret
.Mid(2).Lower(); // remove 'wx' prefix
260 if ( usingUniversal
)
266 wxString
wxPlatformInfo::GetArchName(wxArchitecture arch
)
268 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxArchitectureNames
) == wxARCH_MAX
,
269 wxArchitectureNamesMismatch
);
271 return wxArchitectureNames
[arch
];
274 wxString
wxPlatformInfo::GetEndiannessName(wxEndianness end
)
276 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxEndiannessNames
) == wxENDIAN_MAX
,
277 wxEndiannessNamesMismatch
);
279 return wxEndiannessNames
[end
];
283 // ----------------------------------------------------------------------------
284 // wxPlatformInfo - string -> enum conversions
285 // ----------------------------------------------------------------------------
287 wxOperatingSystemId
wxPlatformInfo::GetOperatingSystemId(const wxString
&str
)
289 for ( size_t i
= 0; i
< WXSIZEOF(wxOperatingSystemIdNames
); i
++ )
291 if ( wxString(wxOperatingSystemIdNames
[i
]).CmpNoCase(str
) == 0 )
292 return (wxOperatingSystemId
)(1 << i
);
298 wxPortId
wxPlatformInfo::GetPortId(const wxString
&str
)
300 // recognize both short and long port names
301 for ( size_t i
= 0; i
< WXSIZEOF(wxPortIdNames
); i
++ )
303 wxPortId current
= (wxPortId
)(1 << i
);
305 if ( wxString(wxPortIdNames
[i
]).CmpNoCase(str
) == 0 ||
306 GetPortIdShortName(current
, true).CmpNoCase(str
) == 0 ||
307 GetPortIdShortName(current
, false).CmpNoCase(str
) == 0 )
311 return wxPORT_UNKNOWN
;
314 wxArchitecture
wxPlatformInfo::GetArch(const wxString
&arch
)
316 if ( arch
.Contains(wxT("32")) )
319 if ( arch
.Contains(wxT("64")) )
322 return wxARCH_INVALID
;
325 wxEndianness
wxPlatformInfo::GetEndianness(const wxString
& end
)
327 wxString
endl(end
.Lower());
328 if ( end
.StartsWith(wxT("little")) )
329 return wxENDIAN_LITTLE
;
331 if ( end
.StartsWith(wxT("big")) )
334 return wxENDIAN_INVALID
;