wxPlatformInfo (patch 1532064)
[wxWidgets.git] / src / common / platinfo.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/platinfo.cpp
3 // Purpose: implements wxPlatformInfo class
4 // Author: Francesco Montorsi
5 // Modified by:
6 // Created: 07.07.2006 (based on wxToolkitInfo)
7 // RCS-ID: $Id$
8 // Copyright: (c) 2006 Francesco Montorsi
9 // License: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/utils.h"
29 #endif //WX_PRECOMP
30
31 #include "wx/platinfo.h"
32 #include "wx/apptrait.h"
33
34 // ----------------------------------------------------------------------------
35 // constants
36 // ----------------------------------------------------------------------------
37
38 static wxString wxOperatingSystemIdNames[] =
39 {
40 _T("Apple Mac OS"),
41 _T("Apple Mac OS X"),
42
43 _T("Microsoft Windows 9X"),
44 _T("Microsoft Windows NT"),
45 _T("Microsoft Windows Micro"),
46 _T("Microsoft Windows CE"),
47
48 _T("Linux"),
49 _T("FreeBSD"),
50 _T("OpenBSD"),
51 _T("NetBSD"),
52
53 _T("SunOS"),
54 _T("AIX"),
55 _T("HPUX"),
56
57 _T("DOS"),
58 _T("OS/2")
59 };
60
61 static wxString wxPortIdNames[] =
62 {
63 _T("wxBase"),
64 _T("wxMSW"),
65 _T("wxMotif"),
66 _T("wxGTK"),
67 _T("wxMGL"),
68 _T("wxX11"),
69 _T("wxOS2"),
70 _T("wxMac"),
71 _T("wxCocoa"),
72 _T("wxWinCE"),
73 _T("wxPalmOS")
74 };
75
76 static wxString wxArchitectureNames[] =
77 {
78 _T("32 bit"),
79 _T("64 bit")
80 };
81
82 static wxString wxEndiannessNames[] =
83 {
84 _T("Big endian"),
85 _T("Little endian"),
86 _T("PDP endian")
87 };
88
89 // ----------------------------------------------------------------------------
90 // local functions
91 // ----------------------------------------------------------------------------
92
93 // returns log in base 2 of the value, this maps the enum values to the
94 // corresponding indices
95 static int wxGetIndexFromEnumValue(int value)
96 {
97 wxCHECK_MSG( value, -1, _T("invalid enum value") );
98
99 int n = 0;
100 while ( !(value & 1) )
101 {
102 value >>= 1;
103 n++;
104 }
105
106 wxASSERT_MSG( value == 1, _T("more than one bit set in enum value") );
107
108 return n;
109 }
110
111 // ----------------------------------------------------------------------------
112 // wxPlatformInfo
113 // ----------------------------------------------------------------------------
114
115 wxPlatformInfo::wxPlatformInfo()
116 {
117 // autodetect all informations
118 const wxAppTraits * const traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
119 if ( !traits )
120 {
121 wxFAIL_MSG( _T("failed to initialize wxPlatformInfo") );
122
123 m_port = wxPORT_UNKNOWN;
124 m_tkVersionMajor =
125 m_tkVersionMinor = 0;
126 }
127 else
128 {
129 m_port = traits->GetToolkitVersion(&m_tkVersionMajor, &m_tkVersionMinor);
130 }
131
132 m_os = wxGetOsVersion(&m_osVersionMajor, &m_osVersionMinor);
133 m_endian = wxIsPlatformLittleEndian() ? wxENDIAN_LITTLE : wxENDIAN_BIG;
134 m_arch = wxIsPlatform64Bit() ? wxARCH_64 : wxARCH_32;
135 }
136
137 wxPlatformInfo::wxPlatformInfo(wxPortId pid, int tkMajor, int tkMinor,
138 wxOperatingSystemId id, int osMajor, int osMinor,
139 wxArchitecture arch,
140 wxEndianness endian)
141 {
142 m_tkVersionMajor = tkMajor;
143 m_tkVersionMinor = tkMinor;
144 m_port = pid;
145
146 m_os = id;
147 m_osVersionMajor = osMajor;
148 m_osVersionMinor = osMinor;
149
150 m_endian = endian;
151 m_arch = arch;
152 }
153
154 bool wxPlatformInfo::operator==(const wxPlatformInfo &t) const
155 {
156 return m_tkVersionMajor == t.m_tkVersionMajor &&
157 m_tkVersionMinor == t.m_tkVersionMinor &&
158 m_osVersionMajor == t.m_osVersionMajor &&
159 m_osVersionMinor == t.m_osVersionMinor &&
160 m_os == t.m_os &&
161 m_port == t.m_port &&
162 m_arch == t.m_arch &&
163 m_endian == t.m_endian;
164 }
165
166 // ----------------------------------------------------------------------------
167 // wxPlatformInfo - enum -> string conversions
168 // ----------------------------------------------------------------------------
169
170 wxString wxPlatformInfo::GetOperatingSystemFamilyName(wxOperatingSystemId os)
171 {
172 if ( os & wxOS_MAC )
173 return _T("Macintosh");
174 else if ( os & wxOS_WINDOWS )
175 return _T("Windows");
176 else if ( os & wxOS_UNIX )
177 return _T("Unix");
178 else if ( os == wxOS_DOS )
179 return _T("DOS");
180 else if ( os == wxOS_OS2 )
181 return _T("OS/2");
182
183 return _T("Unknown");
184 }
185
186 wxString wxPlatformInfo::GetOperatingSystemIdName(wxOperatingSystemId os)
187 {
188 const int idx = wxGetIndexFromEnumValue(os);
189
190 wxCHECK_MSG( idx < WXSIZEOF(wxOperatingSystemIdNames), wxEmptyString,
191 _T("invalid OS id") );
192
193 return wxOperatingSystemIdNames[idx];
194 }
195
196 wxString wxPlatformInfo::GetPortIdName(wxPortId port)
197 {
198 const int idx = wxGetIndexFromEnumValue(port);
199
200 wxCHECK_MSG( idx < WXSIZEOF(wxPortIdNames), wxEmptyString,
201 _T("invalid port id") );
202
203 wxString ret = wxPortIdNames[idx];
204
205 if ( IsUsingUniversalWidgets() )
206 ret += wxT("/wxUniversal");
207
208 return ret;
209 }
210
211 wxString wxPlatformInfo::GetPortIdShortName(wxPortId port)
212 {
213 const int idx = wxGetIndexFromEnumValue(port);
214
215 wxCHECK_MSG( idx < WXSIZEOF(wxPortIdNames), wxEmptyString,
216 _T("invalid port id") );
217
218 wxString ret = wxPortIdNames[idx];
219 ret = ret.Mid(2).Lower(); // remove 'wx' prefix
220
221 if ( IsUsingUniversalWidgets() )
222 ret += wxT("univ");
223
224 return ret;
225 }
226
227 wxString wxPlatformInfo::GetArchName(wxArchitecture arch)
228 {
229 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxArchitectureNames) == wxARCH_MAX,
230 wxArchitectureNamesMismatch );
231
232 return wxArchitectureNames[arch];
233 }
234
235 wxString wxPlatformInfo::GetEndiannessName(wxEndianness end)
236 {
237 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxEndiannessNames) == wxENDIAN_MAX,
238 wxEndiannessNamesMismatch );
239
240 return wxEndiannessNames[end];
241 }
242
243
244 // ----------------------------------------------------------------------------
245 // wxPlatformInfo - string -> enum conversions
246 // ----------------------------------------------------------------------------
247
248 wxOperatingSystemId wxPlatformInfo::GetOperatingSystemId(const wxString &str)
249 {
250 for ( size_t i = 0; i < WXSIZEOF(wxOperatingSystemIdNames); i++ )
251 {
252 if ( wxOperatingSystemIdNames[i].CmpNoCase(str) == 0 )
253 return (wxOperatingSystemId)(1 << i);
254 }
255
256 return wxOS_UNKNOWN;
257 }
258
259 wxPortId wxPlatformInfo::GetPortId(const wxString &str)
260 {
261 // recognize both short and long port names
262 for ( size_t i = 0; i < WXSIZEOF(wxPortIdNames); i++ )
263 {
264 wxPortId current = (wxPortId)(1 << i);
265
266 if ( wxPortIdNames[i].CmpNoCase(str) == 0 )
267 return current;
268 if ( GetPortIdShortName(current).CmpNoCase(str) == 0 )
269 return current;
270 }
271
272 return wxPORT_UNKNOWN;
273 }
274
275 wxArchitecture wxPlatformInfo::GetArch(const wxString &arch)
276 {
277 if ( arch.Contains(wxT("32")) )
278 return wxARCH_32;
279
280 if ( arch.Contains(wxT("64")) )
281 return wxARCH_64;
282
283 return wxARCH_INVALID;
284 }
285
286 wxEndianness wxPlatformInfo::GetEndianness(const wxString& end)
287 {
288 wxString endl(end.Lower());
289 if ( end.StartsWith(wxT("little")) )
290 return wxENDIAN_LITTLE;
291
292 if ( end.StartsWith(wxT("big")) )
293 return wxENDIAN_BIG;
294
295 return wxENDIAN_INVALID;
296 }
297