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