]> git.saurik.com Git - wxWidgets.git/blob - src/common/platinfo.cpp
compilation fix for !wxUSE_BUTTON
[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 const wxChar* const 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 const wxChar* const 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 const wxChar* const wxArchitectureNames[] =
79 {
80 _T("32 bit"),
81 _T("64 bit")
82 };
83
84 static const wxChar* const 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_usingUniversal = false;
127 m_tkVersionMajor =
128 m_tkVersionMinor = 0;
129 }
130 else
131 {
132 m_port = traits->GetToolkitVersion(&m_tkVersionMajor, &m_tkVersionMinor);
133 m_usingUniversal = traits->IsUsingUniversalWidgets();
134 }
135
136 m_os = wxGetOsVersion(&m_osVersionMajor, &m_osVersionMinor);
137 m_endian = wxIsPlatformLittleEndian() ? wxENDIAN_LITTLE : wxENDIAN_BIG;
138 m_arch = wxIsPlatform64Bit() ? wxARCH_64 : wxARCH_32;
139 }
140
141 wxPlatformInfo::wxPlatformInfo(wxPortId pid, int tkMajor, int tkMinor,
142 wxOperatingSystemId id, int osMajor, int osMinor,
143 wxArchitecture arch,
144 wxEndianness endian,
145 bool usingUniversal)
146 {
147 m_tkVersionMajor = tkMajor;
148 m_tkVersionMinor = tkMinor;
149 m_port = pid;
150 m_usingUniversal = usingUniversal;
151
152 m_os = id;
153 m_osVersionMajor = osMajor;
154 m_osVersionMinor = osMinor;
155
156 m_endian = endian;
157 m_arch = arch;
158 }
159
160 bool wxPlatformInfo::operator==(const wxPlatformInfo &t) const
161 {
162 return m_tkVersionMajor == t.m_tkVersionMajor &&
163 m_tkVersionMinor == t.m_tkVersionMinor &&
164 m_osVersionMajor == t.m_osVersionMajor &&
165 m_osVersionMinor == t.m_osVersionMinor &&
166 m_os == t.m_os &&
167 m_port == t.m_port &&
168 m_usingUniversal == t.m_usingUniversal &&
169 m_arch == t.m_arch &&
170 m_endian == t.m_endian;
171 }
172
173 // ----------------------------------------------------------------------------
174 // wxPlatformInfo - enum -> string conversions
175 // ----------------------------------------------------------------------------
176
177 wxString wxPlatformInfo::GetOperatingSystemFamilyName(wxOperatingSystemId os)
178 {
179 const wxChar* string = _T("Unknown");
180 if ( os & wxOS_MAC )
181 string = _T("Macintosh");
182 else if ( os & wxOS_WINDOWS )
183 string = _T("Windows");
184 else if ( os & wxOS_UNIX )
185 string = _T("Unix");
186 else if ( os == wxOS_DOS )
187 string = _T("DOS");
188 else if ( os == wxOS_OS2 )
189 string = _T("OS/2");
190
191 return string;
192 }
193
194 wxString wxPlatformInfo::GetOperatingSystemIdName(wxOperatingSystemId os)
195 {
196 const unsigned idx = wxGetIndexFromEnumValue(os);
197
198 wxCHECK_MSG( idx < WXSIZEOF(wxOperatingSystemIdNames), wxEmptyString,
199 _T("invalid OS id") );
200
201 return wxOperatingSystemIdNames[idx];
202 }
203
204 wxString wxPlatformInfo::GetPortIdName(wxPortId port, bool usingUniversal)
205 {
206 const unsigned idx = wxGetIndexFromEnumValue(port);
207
208 wxCHECK_MSG( idx < WXSIZEOF(wxPortIdNames), wxEmptyString,
209 _T("invalid port id") );
210
211 wxString ret = wxPortIdNames[idx];
212
213 if ( usingUniversal )
214 ret += wxT("/wxUniversal");
215
216 return ret;
217 }
218
219 wxString wxPlatformInfo::GetPortIdShortName(wxPortId port, bool usingUniversal)
220 {
221 const unsigned idx = wxGetIndexFromEnumValue(port);
222
223 wxCHECK_MSG( idx < WXSIZEOF(wxPortIdNames), wxEmptyString,
224 _T("invalid port id") );
225
226 wxString ret = wxPortIdNames[idx];
227 ret = ret.Mid(2).Lower(); // remove 'wx' prefix
228
229 if ( usingUniversal )
230 ret += wxT("univ");
231
232 return ret;
233 }
234
235 wxString wxPlatformInfo::GetArchName(wxArchitecture arch)
236 {
237 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxArchitectureNames) == wxARCH_MAX,
238 wxArchitectureNamesMismatch );
239
240 return wxArchitectureNames[arch];
241 }
242
243 wxString wxPlatformInfo::GetEndiannessName(wxEndianness end)
244 {
245 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxEndiannessNames) == wxENDIAN_MAX,
246 wxEndiannessNamesMismatch );
247
248 return wxEndiannessNames[end];
249 }
250
251
252 // ----------------------------------------------------------------------------
253 // wxPlatformInfo - string -> enum conversions
254 // ----------------------------------------------------------------------------
255
256 wxOperatingSystemId wxPlatformInfo::GetOperatingSystemId(const wxString &str)
257 {
258 for ( size_t i = 0; i < WXSIZEOF(wxOperatingSystemIdNames); i++ )
259 {
260 if ( wxString(wxOperatingSystemIdNames[i]).CmpNoCase(str) == 0 )
261 return (wxOperatingSystemId)(1 << i);
262 }
263
264 return wxOS_UNKNOWN;
265 }
266
267 wxPortId wxPlatformInfo::GetPortId(const wxString &str)
268 {
269 // recognize both short and long port names
270 for ( size_t i = 0; i < WXSIZEOF(wxPortIdNames); i++ )
271 {
272 wxPortId current = (wxPortId)(1 << i);
273
274 if ( wxString(wxPortIdNames[i]).CmpNoCase(str) == 0 ||
275 GetPortIdShortName(current, true).CmpNoCase(str) == 0 ||
276 GetPortIdShortName(current, false).CmpNoCase(str) == 0 )
277 return current;
278 }
279
280 return wxPORT_UNKNOWN;
281 }
282
283 wxArchitecture wxPlatformInfo::GetArch(const wxString &arch)
284 {
285 if ( arch.Contains(wxT("32")) )
286 return wxARCH_32;
287
288 if ( arch.Contains(wxT("64")) )
289 return wxARCH_64;
290
291 return wxARCH_INVALID;
292 }
293
294 wxEndianness wxPlatformInfo::GetEndianness(const wxString& end)
295 {
296 wxString endl(end.Lower());
297 if ( end.StartsWith(wxT("little")) )
298 return wxENDIAN_LITTLE;
299
300 if ( end.StartsWith(wxT("big")) )
301 return wxENDIAN_BIG;
302
303 return wxENDIAN_INVALID;
304 }
305