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 wxString wxOperatingSystemIdNames
[] =
45 _T("Microsoft Windows 9X"),
46 _T("Microsoft Windows NT"),
47 _T("Microsoft Windows Micro"),
48 _T("Microsoft Windows CE"),
63 static wxString wxPortIdNames
[] =
78 static wxString wxArchitectureNames
[] =
84 static wxString 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
)
180 return _T("Macintosh");
181 else if ( os
& wxOS_WINDOWS
)
182 return _T("Windows");
183 else if ( os
& wxOS_UNIX
)
185 else if ( os
== wxOS_DOS
)
187 else if ( os
== wxOS_OS2
)
190 return _T("Unknown");
193 wxString
wxPlatformInfo::GetOperatingSystemIdName(wxOperatingSystemId os
)
195 const unsigned idx
= wxGetIndexFromEnumValue(os
);
197 wxCHECK_MSG( idx
< WXSIZEOF(wxOperatingSystemIdNames
), wxEmptyString
,
198 _T("invalid OS id") );
200 return wxOperatingSystemIdNames
[idx
];
203 wxString
wxPlatformInfo::GetPortIdName(wxPortId port
, bool usingUniversal
)
205 const unsigned idx
= wxGetIndexFromEnumValue(port
);
207 wxCHECK_MSG( idx
< WXSIZEOF(wxPortIdNames
), wxEmptyString
,
208 _T("invalid port id") );
210 wxString ret
= wxPortIdNames
[idx
];
212 if ( usingUniversal
)
213 ret
+= wxT("/wxUniversal");
218 wxString
wxPlatformInfo::GetPortIdShortName(wxPortId port
, bool usingUniversal
)
220 const unsigned idx
= wxGetIndexFromEnumValue(port
);
222 wxCHECK_MSG( idx
< WXSIZEOF(wxPortIdNames
), wxEmptyString
,
223 _T("invalid port id") );
225 wxString ret
= wxPortIdNames
[idx
];
226 ret
= ret
.Mid(2).Lower(); // remove 'wx' prefix
228 if ( usingUniversal
)
234 wxString
wxPlatformInfo::GetArchName(wxArchitecture arch
)
236 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxArchitectureNames
) == wxARCH_MAX
,
237 wxArchitectureNamesMismatch
);
239 return wxArchitectureNames
[arch
];
242 wxString
wxPlatformInfo::GetEndiannessName(wxEndianness end
)
244 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxEndiannessNames
) == wxENDIAN_MAX
,
245 wxEndiannessNamesMismatch
);
247 return wxEndiannessNames
[end
];
251 // ----------------------------------------------------------------------------
252 // wxPlatformInfo - string -> enum conversions
253 // ----------------------------------------------------------------------------
255 wxOperatingSystemId
wxPlatformInfo::GetOperatingSystemId(const wxString
&str
)
257 for ( size_t i
= 0; i
< WXSIZEOF(wxOperatingSystemIdNames
); i
++ )
259 if ( wxOperatingSystemIdNames
[i
].CmpNoCase(str
) == 0 )
260 return (wxOperatingSystemId
)(1 << i
);
266 wxPortId
wxPlatformInfo::GetPortId(const wxString
&str
)
268 // recognize both short and long port names
269 for ( size_t i
= 0; i
< WXSIZEOF(wxPortIdNames
); i
++ )
271 wxPortId current
= (wxPortId
)(1 << i
);
273 if ( wxPortIdNames
[i
].CmpNoCase(str
) == 0 )
275 if ( 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
;