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 _T("PalmOS(Over Linux)"),
75 static const wxChar
* const wxPortIdNames
[] =
91 static const wxChar
* const wxArchitectureNames
[] =
97 static const wxChar
* const wxEndiannessNames
[] =
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 // returns log in base 2 of the value, this maps the enum values to the
109 // corresponding indices
110 static unsigned wxGetIndexFromEnumValue(int value
)
112 wxCHECK_MSG( value
, (unsigned)-1, _T("invalid enum value") );
115 while ( !(value
& 1) )
121 wxASSERT_MSG( value
== 1, _T("more than one bit set in enum value") );
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 wxPlatformInfo::wxPlatformInfo()
132 // just copy platform info for currently running platform
136 wxPlatformInfo::wxPlatformInfo(wxPortId pid
, int tkMajor
, int tkMinor
,
137 wxOperatingSystemId id
, int osMajor
, int osMinor
,
142 m_tkVersionMajor
= tkMajor
;
143 m_tkVersionMinor
= tkMinor
;
145 m_usingUniversal
= usingUniversal
;
148 m_osVersionMajor
= osMajor
;
149 m_osVersionMinor
= osMinor
;
155 bool wxPlatformInfo::operator==(const wxPlatformInfo
&t
) const
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
&&
162 m_port
== t
.m_port
&&
163 m_usingUniversal
== t
.m_usingUniversal
&&
164 m_arch
== t
.m_arch
&&
165 m_endian
== t
.m_endian
;
168 void wxPlatformInfo::InitForCurrentPlatform()
170 // autodetect all informations
171 const wxAppTraits
* const traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
174 wxFAIL_MSG( _T("failed to initialize wxPlatformInfo") );
176 m_port
= wxPORT_UNKNOWN
;
177 m_usingUniversal
= false;
179 m_tkVersionMinor
= 0;
183 m_port
= traits
->GetToolkitVersion(&m_tkVersionMajor
, &m_tkVersionMinor
);
184 m_usingUniversal
= traits
->IsUsingUniversalWidgets();
187 m_os
= wxGetOsVersion(&m_osVersionMajor
, &m_osVersionMinor
);
188 m_endian
= wxIsPlatformLittleEndian() ? wxENDIAN_LITTLE
: wxENDIAN_BIG
;
189 m_arch
= wxIsPlatform64Bit() ? wxARCH_64
: wxARCH_32
;
193 const wxPlatformInfo
& wxPlatformInfo::Get()
195 static bool initialized
= false;
198 gs_platInfo
.InitForCurrentPlatform();
207 // ----------------------------------------------------------------------------
208 // wxPlatformInfo - enum -> string conversions
209 // ----------------------------------------------------------------------------
211 wxString
wxPlatformInfo::GetOperatingSystemFamilyName(wxOperatingSystemId os
)
213 const wxChar
* string
= _T("Unknown");
215 string
= _T("Macintosh");
216 else if ( os
& wxOS_WINDOWS
)
217 string
= _T("Windows");
218 else if ( os
& wxOS_UNIX
)
220 else if ( os
== wxOS_DOS
)
222 else if ( os
== wxOS_OS2
)
228 wxString
wxPlatformInfo::GetOperatingSystemIdName(wxOperatingSystemId os
)
230 const unsigned idx
= wxGetIndexFromEnumValue(os
);
232 wxCHECK_MSG( idx
< WXSIZEOF(wxOperatingSystemIdNames
), wxEmptyString
,
233 _T("invalid OS id") );
235 return wxOperatingSystemIdNames
[idx
];
238 wxString
wxPlatformInfo::GetPortIdName(wxPortId port
, bool usingUniversal
)
240 const unsigned idx
= wxGetIndexFromEnumValue(port
);
242 wxCHECK_MSG( idx
< WXSIZEOF(wxPortIdNames
), wxEmptyString
,
243 _T("invalid port id") );
245 wxString ret
= wxPortIdNames
[idx
];
247 if ( usingUniversal
)
248 ret
+= wxT("/wxUniversal");
253 wxString
wxPlatformInfo::GetPortIdShortName(wxPortId port
, bool usingUniversal
)
255 const unsigned idx
= wxGetIndexFromEnumValue(port
);
257 wxCHECK_MSG( idx
< WXSIZEOF(wxPortIdNames
), wxEmptyString
,
258 _T("invalid port id") );
260 wxString ret
= wxPortIdNames
[idx
];
261 ret
= ret
.Mid(2).Lower(); // remove 'wx' prefix
263 if ( usingUniversal
)
269 wxString
wxPlatformInfo::GetArchName(wxArchitecture arch
)
271 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxArchitectureNames
) == wxARCH_MAX
,
272 wxArchitectureNamesMismatch
);
274 return wxArchitectureNames
[arch
];
277 wxString
wxPlatformInfo::GetEndiannessName(wxEndianness end
)
279 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxEndiannessNames
) == wxENDIAN_MAX
,
280 wxEndiannessNamesMismatch
);
282 return wxEndiannessNames
[end
];
286 // ----------------------------------------------------------------------------
287 // wxPlatformInfo - string -> enum conversions
288 // ----------------------------------------------------------------------------
290 wxOperatingSystemId
wxPlatformInfo::GetOperatingSystemId(const wxString
&str
)
292 for ( size_t i
= 0; i
< WXSIZEOF(wxOperatingSystemIdNames
); i
++ )
294 if ( wxString(wxOperatingSystemIdNames
[i
]).CmpNoCase(str
) == 0 )
295 return (wxOperatingSystemId
)(1 << i
);
301 wxPortId
wxPlatformInfo::GetPortId(const wxString
&str
)
303 // recognize both short and long port names
304 for ( size_t i
= 0; i
< WXSIZEOF(wxPortIdNames
); i
++ )
306 wxPortId current
= (wxPortId
)(1 << i
);
308 if ( wxString(wxPortIdNames
[i
]).CmpNoCase(str
) == 0 ||
309 GetPortIdShortName(current
, true).CmpNoCase(str
) == 0 ||
310 GetPortIdShortName(current
, false).CmpNoCase(str
) == 0 )
314 return wxPORT_UNKNOWN
;
317 wxArchitecture
wxPlatformInfo::GetArch(const wxString
&arch
)
319 if ( arch
.Contains(wxT("32")) )
322 if ( arch
.Contains(wxT("64")) )
325 return wxARCH_INVALID
;
328 wxEndianness
wxPlatformInfo::GetEndianness(const wxString
& end
)
330 const wxString
endl(end
.Lower());
331 if ( endl
.StartsWith(wxT("little")) )
332 return wxENDIAN_LITTLE
;
334 if ( endl
.StartsWith(wxT("big")) )
337 return wxENDIAN_INVALID
;