]> git.saurik.com Git - wxWidgets.git/blob - src/common/platinfo.cpp
avoid calling wxSystemSettings::GetMetric() from wxIconBundle::GetIcon() unless the...
[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_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 if ( os & wxOS_MAC )
180 return _T("Macintosh");
181 else if ( os & wxOS_WINDOWS )
182 return _T("Windows");
183 else if ( os & wxOS_UNIX )
184 return _T("Unix");
185 else if ( os == wxOS_DOS )
186 return _T("DOS");
187 else if ( os == wxOS_OS2 )
188 return _T("OS/2");
189
190 return _T("Unknown");
191 }
192
193 wxString wxPlatformInfo::GetOperatingSystemIdName(wxOperatingSystemId os)
194 {
195 const unsigned idx = wxGetIndexFromEnumValue(os);
196
197 wxCHECK_MSG( idx < WXSIZEOF(wxOperatingSystemIdNames), wxEmptyString,
198 _T("invalid OS id") );
199
200 return wxOperatingSystemIdNames[idx];
201 }
202
203 wxString wxPlatformInfo::GetPortIdName(wxPortId port, bool usingUniversal)
204 {
205 const unsigned idx = wxGetIndexFromEnumValue(port);
206
207 wxCHECK_MSG( idx < WXSIZEOF(wxPortIdNames), wxEmptyString,
208 _T("invalid port id") );
209
210 wxString ret = wxPortIdNames[idx];
211
212 if ( usingUniversal )
213 ret += wxT("/wxUniversal");
214
215 return ret;
216 }
217
218 wxString wxPlatformInfo::GetPortIdShortName(wxPortId port, bool usingUniversal)
219 {
220 const unsigned idx = wxGetIndexFromEnumValue(port);
221
222 wxCHECK_MSG( idx < WXSIZEOF(wxPortIdNames), wxEmptyString,
223 _T("invalid port id") );
224
225 wxString ret = wxPortIdNames[idx];
226 ret = ret.Mid(2).Lower(); // remove 'wx' prefix
227
228 if ( usingUniversal )
229 ret += wxT("univ");
230
231 return ret;
232 }
233
234 wxString wxPlatformInfo::GetArchName(wxArchitecture arch)
235 {
236 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxArchitectureNames) == wxARCH_MAX,
237 wxArchitectureNamesMismatch );
238
239 return wxArchitectureNames[arch];
240 }
241
242 wxString wxPlatformInfo::GetEndiannessName(wxEndianness end)
243 {
244 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxEndiannessNames) == wxENDIAN_MAX,
245 wxEndiannessNamesMismatch );
246
247 return wxEndiannessNames[end];
248 }
249
250
251 // ----------------------------------------------------------------------------
252 // wxPlatformInfo - string -> enum conversions
253 // ----------------------------------------------------------------------------
254
255 wxOperatingSystemId wxPlatformInfo::GetOperatingSystemId(const wxString &str)
256 {
257 for ( size_t i = 0; i < WXSIZEOF(wxOperatingSystemIdNames); i++ )
258 {
259 if ( wxOperatingSystemIdNames[i].CmpNoCase(str) == 0 )
260 return (wxOperatingSystemId)(1 << i);
261 }
262
263 return wxOS_UNKNOWN;
264 }
265
266 wxPortId wxPlatformInfo::GetPortId(const wxString &str)
267 {
268 // recognize both short and long port names
269 for ( size_t i = 0; i < WXSIZEOF(wxPortIdNames); i++ )
270 {
271 wxPortId current = (wxPortId)(1 << i);
272
273 if ( wxPortIdNames[i].CmpNoCase(str) == 0 )
274 return current;
275 if ( 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