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