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