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
[] =
78 static const wxChar
* const wxArchitectureNames
[] =
84 static const wxChar
* const wxEndiannessNames
[] =
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 // returns log in base 2 of the value, this maps the enum values to the
96 // corresponding indices
97 static unsigned wxGetIndexFromEnumValue(int value
)
99 wxCHECK_MSG( value
, (unsigned)-1, _T("invalid enum value") );
102 while ( !(value
& 1) )
108 wxASSERT_MSG( value
== 1, _T("more than one bit set in enum value") );
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
117 wxPlatformInfo::wxPlatformInfo()
119 // autodetect all informations
120 const wxAppTraits
* const traits
= wxTheApp
? wxTheApp
->GetTraits() : NULL
;
123 wxFAIL_MSG( _T("failed to initialize wxPlatformInfo") );
125 m_port
= wxPORT_UNKNOWN
;
126 m_usingUniversal
= false;
128 m_tkVersionMinor
= 0;
132 m_port
= traits
->GetToolkitVersion(&m_tkVersionMajor
, &m_tkVersionMinor
);
133 m_usingUniversal
= traits
->IsUsingUniversalWidgets();
136 m_os
= wxGetOsVersion(&m_osVersionMajor
, &m_osVersionMinor
);
137 m_endian
= wxIsPlatformLittleEndian() ? wxENDIAN_LITTLE
: wxENDIAN_BIG
;
138 m_arch
= wxIsPlatform64Bit() ? wxARCH_64
: wxARCH_32
;
141 wxPlatformInfo::wxPlatformInfo(wxPortId pid
, int tkMajor
, int tkMinor
,
142 wxOperatingSystemId id
, int osMajor
, int osMinor
,
147 m_tkVersionMajor
= tkMajor
;
148 m_tkVersionMinor
= tkMinor
;
150 m_usingUniversal
= usingUniversal
;
153 m_osVersionMajor
= osMajor
;
154 m_osVersionMinor
= osMinor
;
160 bool wxPlatformInfo::operator==(const wxPlatformInfo
&t
) const
162 return m_tkVersionMajor
== t
.m_tkVersionMajor
&&
163 m_tkVersionMinor
== t
.m_tkVersionMinor
&&
164 m_osVersionMajor
== t
.m_osVersionMajor
&&
165 m_osVersionMinor
== t
.m_osVersionMinor
&&
167 m_port
== t
.m_port
&&
168 m_usingUniversal
== t
.m_usingUniversal
&&
169 m_arch
== t
.m_arch
&&
170 m_endian
== t
.m_endian
;
173 // ----------------------------------------------------------------------------
174 // wxPlatformInfo - enum -> string conversions
175 // ----------------------------------------------------------------------------
177 wxString
wxPlatformInfo::GetOperatingSystemFamilyName(wxOperatingSystemId os
)
179 const wxChar
* string
= _T("Unknown");
181 string
= _T("Macintosh");
182 else if ( os
& wxOS_WINDOWS
)
183 string
= _T("Windows");
184 else if ( os
& wxOS_UNIX
)
186 else if ( os
== wxOS_DOS
)
188 else if ( os
== wxOS_OS2
)
194 wxString
wxPlatformInfo::GetOperatingSystemIdName(wxOperatingSystemId os
)
196 const unsigned idx
= wxGetIndexFromEnumValue(os
);
198 wxCHECK_MSG( idx
< WXSIZEOF(wxOperatingSystemIdNames
), wxEmptyString
,
199 _T("invalid OS id") );
201 return wxOperatingSystemIdNames
[idx
];
204 wxString
wxPlatformInfo::GetPortIdName(wxPortId port
, bool usingUniversal
)
206 const unsigned idx
= wxGetIndexFromEnumValue(port
);
208 wxCHECK_MSG( idx
< WXSIZEOF(wxPortIdNames
), wxEmptyString
,
209 _T("invalid port id") );
211 wxString ret
= wxPortIdNames
[idx
];
213 if ( usingUniversal
)
214 ret
+= wxT("/wxUniversal");
219 wxString
wxPlatformInfo::GetPortIdShortName(wxPortId port
, bool usingUniversal
)
221 const unsigned idx
= wxGetIndexFromEnumValue(port
);
223 wxCHECK_MSG( idx
< WXSIZEOF(wxPortIdNames
), wxEmptyString
,
224 _T("invalid port id") );
226 wxString ret
= wxPortIdNames
[idx
];
227 ret
= ret
.Mid(2).Lower(); // remove 'wx' prefix
229 if ( usingUniversal
)
235 wxString
wxPlatformInfo::GetArchName(wxArchitecture arch
)
237 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxArchitectureNames
) == wxARCH_MAX
,
238 wxArchitectureNamesMismatch
);
240 return wxArchitectureNames
[arch
];
243 wxString
wxPlatformInfo::GetEndiannessName(wxEndianness end
)
245 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxEndiannessNames
) == wxENDIAN_MAX
,
246 wxEndiannessNamesMismatch
);
248 return wxEndiannessNames
[end
];
252 // ----------------------------------------------------------------------------
253 // wxPlatformInfo - string -> enum conversions
254 // ----------------------------------------------------------------------------
256 wxOperatingSystemId
wxPlatformInfo::GetOperatingSystemId(const wxString
&str
)
258 for ( size_t i
= 0; i
< WXSIZEOF(wxOperatingSystemIdNames
); i
++ )
260 if ( wxString(wxOperatingSystemIdNames
[i
]).CmpNoCase(str
) == 0 )
261 return (wxOperatingSystemId
)(1 << i
);
267 wxPortId
wxPlatformInfo::GetPortId(const wxString
&str
)
269 // recognize both short and long port names
270 for ( size_t i
= 0; i
< WXSIZEOF(wxPortIdNames
); i
++ )
272 wxPortId current
= (wxPortId
)(1 << i
);
274 if ( wxString(wxPortIdNames
[i
]).CmpNoCase(str
) == 0 ||
275 GetPortIdShortName(current
, true).CmpNoCase(str
) == 0 ||
276 GetPortIdShortName(current
, false).CmpNoCase(str
) == 0 )
280 return wxPORT_UNKNOWN
;
283 wxArchitecture
wxPlatformInfo::GetArch(const wxString
&arch
)
285 if ( arch
.Contains(wxT("32")) )
288 if ( arch
.Contains(wxT("64")) )
291 return wxARCH_INVALID
;
294 wxEndianness
wxPlatformInfo::GetEndianness(const wxString
& end
)
296 wxString
endl(end
.Lower());
297 if ( end
.StartsWith(wxT("little")) )
298 return wxENDIAN_LITTLE
;
300 if ( end
.StartsWith(wxT("big")) )
303 return wxENDIAN_INVALID
;