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