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"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 static const wxChar
* const wxOperatingSystemIdNames
[] =
45 _T("Microsoft Windows 9X"),
46 _T("Microsoft Windows NT"),
47 _T("Microsoft Windows Micro"),
48 _T("Microsoft Windows CE"),
63 static const wxChar
* const wxPortIdNames
[] =
79 static const wxChar
* const wxArchitectureNames
[] =
85 static const wxChar
* const wxEndiannessNames
[] =
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 // returns log in base 2 of the value, this maps the enum values to the
97 // corresponding indices
98 static unsigned wxGetIndexFromEnumValue(int value
)
100 wxCHECK_MSG( value
, (unsigned)-1, _T("invalid enum value") );
103 while ( !(value
& 1) )
109 wxASSERT_MSG( value
== 1, _T("more than one bit set in enum value") );
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 wxPlatformInfo::wxPlatformInfo()
120 // autodetect all informations
121 const wxAppTraits
* const traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
124 wxFAIL_MSG( _T("failed to initialize wxPlatformInfo") );
126 m_port
= wxPORT_UNKNOWN
;
127 m_usingUniversal
= false;
129 m_tkVersionMinor
= 0;
133 m_port
= traits
->GetToolkitVersion(&m_tkVersionMajor
, &m_tkVersionMinor
);
134 m_usingUniversal
= traits
->IsUsingUniversalWidgets();
137 m_os
= wxGetOsVersion(&m_osVersionMajor
, &m_osVersionMinor
);
138 m_endian
= wxIsPlatformLittleEndian() ? wxENDIAN_LITTLE
: wxENDIAN_BIG
;
139 m_arch
= wxIsPlatform64Bit() ? wxARCH_64
: wxARCH_32
;
142 wxPlatformInfo::wxPlatformInfo(wxPortId pid
, int tkMajor
, int tkMinor
,
143 wxOperatingSystemId id
, int osMajor
, int osMinor
,
148 m_tkVersionMajor
= tkMajor
;
149 m_tkVersionMinor
= tkMinor
;
151 m_usingUniversal
= usingUniversal
;
154 m_osVersionMajor
= osMajor
;
155 m_osVersionMinor
= osMinor
;
161 bool wxPlatformInfo::operator==(const wxPlatformInfo
&t
) const
163 return m_tkVersionMajor
== t
.m_tkVersionMajor
&&
164 m_tkVersionMinor
== t
.m_tkVersionMinor
&&
165 m_osVersionMajor
== t
.m_osVersionMajor
&&
166 m_osVersionMinor
== t
.m_osVersionMinor
&&
168 m_port
== t
.m_port
&&
169 m_usingUniversal
== t
.m_usingUniversal
&&
170 m_arch
== t
.m_arch
&&
171 m_endian
== t
.m_endian
;
174 // ----------------------------------------------------------------------------
175 // wxPlatformInfo - enum -> string conversions
176 // ----------------------------------------------------------------------------
178 wxString
wxPlatformInfo::GetOperatingSystemFamilyName(wxOperatingSystemId os
)
180 const wxChar
* string
= _T("Unknown");
182 string
= _T("Macintosh");
183 else if ( os
& wxOS_WINDOWS
)
184 string
= _T("Windows");
185 else if ( os
& wxOS_UNIX
)
187 else if ( os
== wxOS_DOS
)
189 else if ( os
== wxOS_OS2
)
195 wxString
wxPlatformInfo::GetOperatingSystemIdName(wxOperatingSystemId os
)
197 const unsigned idx
= wxGetIndexFromEnumValue(os
);
199 wxCHECK_MSG( idx
< WXSIZEOF(wxOperatingSystemIdNames
), wxEmptyString
,
200 _T("invalid OS id") );
202 return wxOperatingSystemIdNames
[idx
];
205 wxString
wxPlatformInfo::GetPortIdName(wxPortId port
, bool usingUniversal
)
207 const unsigned idx
= wxGetIndexFromEnumValue(port
);
209 wxCHECK_MSG( idx
< WXSIZEOF(wxPortIdNames
), wxEmptyString
,
210 _T("invalid port id") );
212 wxString ret
= wxPortIdNames
[idx
];
214 if ( usingUniversal
)
215 ret
+= wxT("/wxUniversal");
220 wxString
wxPlatformInfo::GetPortIdShortName(wxPortId port
, bool usingUniversal
)
222 const unsigned idx
= wxGetIndexFromEnumValue(port
);
224 wxCHECK_MSG( idx
< WXSIZEOF(wxPortIdNames
), wxEmptyString
,
225 _T("invalid port id") );
227 wxString ret
= wxPortIdNames
[idx
];
228 ret
= ret
.Mid(2).Lower(); // remove 'wx' prefix
230 if ( usingUniversal
)
236 wxString
wxPlatformInfo::GetArchName(wxArchitecture arch
)
238 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxArchitectureNames
) == wxARCH_MAX
,
239 wxArchitectureNamesMismatch
);
241 return wxArchitectureNames
[arch
];
244 wxString
wxPlatformInfo::GetEndiannessName(wxEndianness end
)
246 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxEndiannessNames
) == wxENDIAN_MAX
,
247 wxEndiannessNamesMismatch
);
249 return wxEndiannessNames
[end
];
253 // ----------------------------------------------------------------------------
254 // wxPlatformInfo - string -> enum conversions
255 // ----------------------------------------------------------------------------
257 wxOperatingSystemId
wxPlatformInfo::GetOperatingSystemId(const wxString
&str
)
259 for ( size_t i
= 0; i
< WXSIZEOF(wxOperatingSystemIdNames
); i
++ )
261 if ( wxString(wxOperatingSystemIdNames
[i
]).CmpNoCase(str
) == 0 )
262 return (wxOperatingSystemId
)(1 << i
);
268 wxPortId
wxPlatformInfo::GetPortId(const wxString
&str
)
270 // recognize both short and long port names
271 for ( size_t i
= 0; i
< WXSIZEOF(wxPortIdNames
); i
++ )
273 wxPortId current
= (wxPortId
)(1 << i
);
275 if ( wxString(wxPortIdNames
[i
]).CmpNoCase(str
) == 0 ||
276 GetPortIdShortName(current
, true).CmpNoCase(str
) == 0 ||
277 GetPortIdShortName(current
, false).CmpNoCase(str
) == 0 )
281 return wxPORT_UNKNOWN
;
284 wxArchitecture
wxPlatformInfo::GetArch(const wxString
&arch
)
286 if ( arch
.Contains(wxT("32")) )
289 if ( arch
.Contains(wxT("64")) )
292 return wxARCH_INVALID
;
295 wxEndianness
wxPlatformInfo::GetEndianness(const wxString
& end
)
297 wxString
endl(end
.Lower());
298 if ( end
.StartsWith(wxT("little")) )
299 return wxENDIAN_LITTLE
;
301 if ( end
.StartsWith(wxT("big")) )
304 return wxENDIAN_INVALID
;