]> git.saurik.com Git - wxWidgets.git/blob - src/common/platinfo.cpp
Forgot to commit the header change too.
[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 #include "wx/platinfo.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/app.h"
31 #include "wx/utils.h"
32 #endif //WX_PRECOMP
33
34 #include "wx/apptrait.h"
35
36 // global object
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);
41
42 // ----------------------------------------------------------------------------
43 // constants
44 // ----------------------------------------------------------------------------
45
46 static const wxChar* const wxOperatingSystemIdNames[] =
47 {
48 _T("Apple Mac OS"),
49 _T("Apple Mac OS X"),
50
51 _T("Microsoft Windows 9X"),
52 _T("Microsoft Windows NT"),
53 _T("Microsoft Windows Micro"),
54 _T("Microsoft Windows CE"),
55
56 _T("Linux"),
57 _T("FreeBSD"),
58 _T("OpenBSD"),
59 _T("NetBSD"),
60
61 _T("SunOS"),
62 _T("AIX"),
63 _T("HPUX"),
64
65 _T("Other Unix"),
66 _T("Other Unix"),
67
68 _T("DOS"),
69 _T("OS/2"),
70
71 _T("PalmOS"),
72 _T("PalmOS(Over Linux)"),
73 };
74
75 static const wxChar* const wxPortIdNames[] =
76 {
77 _T("wxBase"),
78 _T("wxMSW"),
79 _T("wxMotif"),
80 _T("wxGTK"),
81 _T("wxMGL"),
82 _T("wxX11"),
83 _T("wxOS2"),
84 _T("wxMac"),
85 _T("wxCocoa"),
86 _T("wxWinCE"),
87 _T("wxPalmOS"),
88 _T("wxDFB")
89 };
90
91 static const wxChar* const wxArchitectureNames[] =
92 {
93 _T("32 bit"),
94 _T("64 bit")
95 };
96
97 static const wxChar* const wxEndiannessNames[] =
98 {
99 _T("Big endian"),
100 _T("Little endian"),
101 _T("PDP endian")
102 };
103
104 // ----------------------------------------------------------------------------
105 // local functions
106 // ----------------------------------------------------------------------------
107
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)
111 {
112 wxCHECK_MSG( value, (unsigned)-1, _T("invalid enum value") );
113
114 int n = 0;
115 while ( !(value & 1) )
116 {
117 value >>= 1;
118 n++;
119 }
120
121 wxASSERT_MSG( value == 1, _T("more than one bit set in enum value") );
122
123 return n;
124 }
125
126 // ----------------------------------------------------------------------------
127 // wxPlatformInfo
128 // ----------------------------------------------------------------------------
129
130 wxPlatformInfo::wxPlatformInfo()
131 {
132 // just copy platform info for currently running platform
133 *this = Get();
134 }
135
136 wxPlatformInfo::wxPlatformInfo(wxPortId pid, int tkMajor, int tkMinor,
137 wxOperatingSystemId id, int osMajor, int osMinor,
138 wxArchitecture arch,
139 wxEndianness endian,
140 bool usingUniversal)
141 {
142 m_tkVersionMajor = tkMajor;
143 m_tkVersionMinor = tkMinor;
144 m_port = pid;
145 m_usingUniversal = usingUniversal;
146
147 m_os = id;
148 m_osVersionMajor = osMajor;
149 m_osVersionMinor = osMinor;
150
151 m_endian = endian;
152 m_arch = arch;
153 }
154
155 bool wxPlatformInfo::operator==(const wxPlatformInfo &t) const
156 {
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 &&
161 m_os == t.m_os &&
162 m_port == t.m_port &&
163 m_usingUniversal == t.m_usingUniversal &&
164 m_arch == t.m_arch &&
165 m_endian == t.m_endian;
166 }
167
168 void wxPlatformInfo::InitForCurrentPlatform()
169 {
170 // autodetect all informations
171 const wxAppTraits * const traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
172 if ( !traits )
173 {
174 wxFAIL_MSG( _T("failed to initialize wxPlatformInfo") );
175
176 m_port = wxPORT_UNKNOWN;
177 m_usingUniversal = false;
178 m_tkVersionMajor =
179 m_tkVersionMinor = 0;
180 }
181 else
182 {
183 m_port = traits->GetToolkitVersion(&m_tkVersionMajor, &m_tkVersionMinor);
184 m_usingUniversal = traits->IsUsingUniversalWidgets();
185 }
186
187 m_os = wxGetOsVersion(&m_osVersionMajor, &m_osVersionMinor);
188 m_endian = wxIsPlatformLittleEndian() ? wxENDIAN_LITTLE : wxENDIAN_BIG;
189 m_arch = wxIsPlatform64Bit() ? wxARCH_64 : wxARCH_32;
190 }
191
192 /* static */
193 const wxPlatformInfo& wxPlatformInfo::Get()
194 {
195 static bool initialized = false;
196 if ( !initialized )
197 {
198 gs_platInfo.InitForCurrentPlatform();
199 initialized = true;
200 }
201
202 return gs_platInfo;
203 }
204
205
206
207 // ----------------------------------------------------------------------------
208 // wxPlatformInfo - enum -> string conversions
209 // ----------------------------------------------------------------------------
210
211 wxString wxPlatformInfo::GetOperatingSystemFamilyName(wxOperatingSystemId os)
212 {
213 const wxChar* string = _T("Unknown");
214 if ( os & wxOS_MAC )
215 string = _T("Macintosh");
216 else if ( os & wxOS_WINDOWS )
217 string = _T("Windows");
218 else if ( os & wxOS_UNIX )
219 string = _T("Unix");
220 else if ( os == wxOS_DOS )
221 string = _T("DOS");
222 else if ( os == wxOS_OS2 )
223 string = _T("OS/2");
224
225 return string;
226 }
227
228 wxString wxPlatformInfo::GetOperatingSystemIdName(wxOperatingSystemId os)
229 {
230 const unsigned idx = wxGetIndexFromEnumValue(os);
231
232 wxCHECK_MSG( idx < WXSIZEOF(wxOperatingSystemIdNames), wxEmptyString,
233 _T("invalid OS id") );
234
235 return wxOperatingSystemIdNames[idx];
236 }
237
238 wxString wxPlatformInfo::GetPortIdName(wxPortId port, bool usingUniversal)
239 {
240 const unsigned idx = wxGetIndexFromEnumValue(port);
241
242 wxCHECK_MSG( idx < WXSIZEOF(wxPortIdNames), wxEmptyString,
243 _T("invalid port id") );
244
245 wxString ret = wxPortIdNames[idx];
246
247 if ( usingUniversal )
248 ret += wxT("/wxUniversal");
249
250 return ret;
251 }
252
253 wxString wxPlatformInfo::GetPortIdShortName(wxPortId port, bool usingUniversal)
254 {
255 const unsigned idx = wxGetIndexFromEnumValue(port);
256
257 wxCHECK_MSG( idx < WXSIZEOF(wxPortIdNames), wxEmptyString,
258 _T("invalid port id") );
259
260 wxString ret = wxPortIdNames[idx];
261 ret = ret.Mid(2).Lower(); // remove 'wx' prefix
262
263 if ( usingUniversal )
264 ret += wxT("univ");
265
266 return ret;
267 }
268
269 wxString wxPlatformInfo::GetArchName(wxArchitecture arch)
270 {
271 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxArchitectureNames) == wxARCH_MAX,
272 wxArchitectureNamesMismatch );
273
274 return wxArchitectureNames[arch];
275 }
276
277 wxString wxPlatformInfo::GetEndiannessName(wxEndianness end)
278 {
279 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxEndiannessNames) == wxENDIAN_MAX,
280 wxEndiannessNamesMismatch );
281
282 return wxEndiannessNames[end];
283 }
284
285
286 // ----------------------------------------------------------------------------
287 // wxPlatformInfo - string -> enum conversions
288 // ----------------------------------------------------------------------------
289
290 wxOperatingSystemId wxPlatformInfo::GetOperatingSystemId(const wxString &str)
291 {
292 for ( size_t i = 0; i < WXSIZEOF(wxOperatingSystemIdNames); i++ )
293 {
294 if ( wxString(wxOperatingSystemIdNames[i]).CmpNoCase(str) == 0 )
295 return (wxOperatingSystemId)(1 << i);
296 }
297
298 return wxOS_UNKNOWN;
299 }
300
301 wxPortId wxPlatformInfo::GetPortId(const wxString &str)
302 {
303 // recognize both short and long port names
304 for ( size_t i = 0; i < WXSIZEOF(wxPortIdNames); i++ )
305 {
306 wxPortId current = (wxPortId)(1 << i);
307
308 if ( wxString(wxPortIdNames[i]).CmpNoCase(str) == 0 ||
309 GetPortIdShortName(current, true).CmpNoCase(str) == 0 ||
310 GetPortIdShortName(current, false).CmpNoCase(str) == 0 )
311 return current;
312 }
313
314 return wxPORT_UNKNOWN;
315 }
316
317 wxArchitecture wxPlatformInfo::GetArch(const wxString &arch)
318 {
319 if ( arch.Contains(wxT("32")) )
320 return wxARCH_32;
321
322 if ( arch.Contains(wxT("64")) )
323 return wxARCH_64;
324
325 return wxARCH_INVALID;
326 }
327
328 wxEndianness wxPlatformInfo::GetEndianness(const wxString& end)
329 {
330 const wxString endl(end.Lower());
331 if ( endl.StartsWith(wxT("little")) )
332 return wxENDIAN_LITTLE;
333
334 if ( endl.StartsWith(wxT("big")) )
335 return wxENDIAN_BIG;
336
337 return wxENDIAN_INVALID;
338 }
339