]> git.saurik.com Git - wxWidgets.git/blob - src/common/platinfo.cpp
correct bug with using uninitialized end variable (unused variable warnings are not...
[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 the logarithm in base 2 of 'value'; this maps the enum values to the
109 // corresponding indexes of the string arrays above
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_osDesc == t.m_osDesc &&
163 m_ldi == t.m_ldi &&
164 m_desktopEnv == t.m_desktopEnv &&
165 m_port == t.m_port &&
166 m_usingUniversal == t.m_usingUniversal &&
167 m_arch == t.m_arch &&
168 m_endian == t.m_endian;
169 }
170
171 void wxPlatformInfo::InitForCurrentPlatform()
172 {
173 // autodetect all informations
174 const wxAppTraits * const traits = wxTheApp ? wxTheApp->GetTraits() : NULL;
175 if ( !traits )
176 {
177 wxFAIL_MSG( _T("failed to initialize wxPlatformInfo") );
178
179 m_port = wxPORT_UNKNOWN;
180 m_usingUniversal = false;
181 m_tkVersionMajor =
182 m_tkVersionMinor = 0;
183 }
184 else
185 {
186 m_port = traits->GetToolkitVersion(&m_tkVersionMajor, &m_tkVersionMinor);
187 m_usingUniversal = traits->IsUsingUniversalWidgets();
188 m_desktopEnv = traits->GetDesktopEnvironment();
189 }
190
191 m_os = wxGetOsVersion(&m_osVersionMajor, &m_osVersionMinor);
192 m_osDesc = wxGetOsDescription();
193 m_endian = wxIsPlatformLittleEndian() ? wxENDIAN_LITTLE : wxENDIAN_BIG;
194 m_arch = wxIsPlatform64Bit() ? wxARCH_64 : wxARCH_32;
195
196 #ifdef __LINUX__
197 m_ldi = wxGetLinuxDistributionInfo();
198 #endif
199 // else: leave m_ldi empty
200 }
201
202 /* static */
203 const wxPlatformInfo& wxPlatformInfo::Get()
204 {
205 static bool initialized = false;
206 if ( !initialized )
207 {
208 gs_platInfo.InitForCurrentPlatform();
209 initialized = true;
210 }
211
212 return gs_platInfo;
213 }
214
215 /* static */
216 wxString wxPlatformInfo::GetOperatingSystemDirectory()
217 {
218 return wxGetOSDirectory();
219 }
220
221
222
223 // ----------------------------------------------------------------------------
224 // wxPlatformInfo - enum -> string conversions
225 // ----------------------------------------------------------------------------
226
227 wxString wxPlatformInfo::GetOperatingSystemFamilyName(wxOperatingSystemId os)
228 {
229 const wxChar* string = _T("Unknown");
230 if ( os & wxOS_MAC )
231 string = _T("Macintosh");
232 else if ( os & wxOS_WINDOWS )
233 string = _T("Windows");
234 else if ( os & wxOS_UNIX )
235 string = _T("Unix");
236 else if ( os == wxOS_DOS )
237 string = _T("DOS");
238 else if ( os == wxOS_OS2 )
239 string = _T("OS/2");
240
241 return string;
242 }
243
244 wxString wxPlatformInfo::GetOperatingSystemIdName(wxOperatingSystemId os)
245 {
246 const unsigned idx = wxGetIndexFromEnumValue(os);
247
248 wxCHECK_MSG( idx < WXSIZEOF(wxOperatingSystemIdNames), wxEmptyString,
249 _T("invalid OS id") );
250
251 return wxOperatingSystemIdNames[idx];
252 }
253
254 wxString wxPlatformInfo::GetPortIdName(wxPortId port, bool usingUniversal)
255 {
256 const unsigned idx = wxGetIndexFromEnumValue(port);
257
258 wxCHECK_MSG( idx < WXSIZEOF(wxPortIdNames), wxEmptyString,
259 _T("invalid port id") );
260
261 wxString ret = wxPortIdNames[idx];
262
263 if ( usingUniversal )
264 ret += wxT("/wxUniversal");
265
266 return ret;
267 }
268
269 wxString wxPlatformInfo::GetPortIdShortName(wxPortId port, bool usingUniversal)
270 {
271 const unsigned idx = wxGetIndexFromEnumValue(port);
272
273 wxCHECK_MSG( idx < WXSIZEOF(wxPortIdNames), wxEmptyString,
274 _T("invalid port id") );
275
276 wxString ret = wxPortIdNames[idx];
277 ret = ret.Mid(2).Lower(); // remove 'wx' prefix
278
279 if ( usingUniversal )
280 ret += wxT("univ");
281
282 return ret;
283 }
284
285 wxString wxPlatformInfo::GetArchName(wxArchitecture arch)
286 {
287 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxArchitectureNames) == wxARCH_MAX,
288 wxArchitectureNamesMismatch );
289
290 return wxArchitectureNames[arch];
291 }
292
293 wxString wxPlatformInfo::GetEndiannessName(wxEndianness end)
294 {
295 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxEndiannessNames) == wxENDIAN_MAX,
296 wxEndiannessNamesMismatch );
297
298 return wxEndiannessNames[end];
299 }
300
301
302 // ----------------------------------------------------------------------------
303 // wxPlatformInfo - string -> enum conversions
304 // ----------------------------------------------------------------------------
305
306 wxOperatingSystemId wxPlatformInfo::GetOperatingSystemId(const wxString &str)
307 {
308 for ( size_t i = 0; i < WXSIZEOF(wxOperatingSystemIdNames); i++ )
309 {
310 if ( wxString(wxOperatingSystemIdNames[i]).CmpNoCase(str) == 0 )
311 return (wxOperatingSystemId)(1 << i);
312 }
313
314 return wxOS_UNKNOWN;
315 }
316
317 wxPortId wxPlatformInfo::GetPortId(const wxString &str)
318 {
319 // recognize both short and long port names
320 for ( size_t i = 0; i < WXSIZEOF(wxPortIdNames); i++ )
321 {
322 wxPortId current = (wxPortId)(1 << i);
323
324 if ( wxString(wxPortIdNames[i]).CmpNoCase(str) == 0 ||
325 GetPortIdShortName(current, true).CmpNoCase(str) == 0 ||
326 GetPortIdShortName(current, false).CmpNoCase(str) == 0 )
327 return current;
328 }
329
330 return wxPORT_UNKNOWN;
331 }
332
333 wxArchitecture wxPlatformInfo::GetArch(const wxString &arch)
334 {
335 if ( arch.Contains(wxT("32")) )
336 return wxARCH_32;
337
338 if ( arch.Contains(wxT("64")) )
339 return wxARCH_64;
340
341 return wxARCH_INVALID;
342 }
343
344 wxEndianness wxPlatformInfo::GetEndianness(const wxString& end)
345 {
346 const wxString endl(end.Lower());
347 if ( endl.StartsWith(wxT("little")) )
348 return wxENDIAN_LITTLE;
349
350 if ( endl.StartsWith(wxT("big")) )
351 return wxENDIAN_BIG;
352
353 return wxENDIAN_INVALID;
354 }
355