]> git.saurik.com Git - wxWidgets.git/blame - src/common/platinfo.cpp
avoiding nesting dcs on the same window concurrently
[wxWidgets.git] / src / common / platinfo.cpp
CommitLineData
8bb6b2c0
VZ
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
f566c8e2
PC
27#include "wx/platinfo.h"
28
8bb6b2c0 29#ifndef WX_PRECOMP
f566c8e2 30 #include "wx/app.h"
8bb6b2c0
VZ
31 #include "wx/utils.h"
32#endif //WX_PRECOMP
33
8bb6b2c0
VZ
34#include "wx/apptrait.h"
35
449090b5
VZ
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!
40static wxPlatformInfo gs_platInfo(wxPORT_UNKNOWN);
41
8bb6b2c0
VZ
42// ----------------------------------------------------------------------------
43// constants
44// ----------------------------------------------------------------------------
45
8f493187 46static const wxChar* const wxOperatingSystemIdNames[] =
8bb6b2c0
VZ
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
8f493187 69static const wxChar* const wxPortIdNames[] =
8bb6b2c0
VZ
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"),
b3c86150
VS
81 _T("wxPalmOS"),
82 _T("wxDFB")
8bb6b2c0
VZ
83};
84
8f493187 85static const wxChar* const wxArchitectureNames[] =
8bb6b2c0
VZ
86{
87 _T("32 bit"),
88 _T("64 bit")
89};
90
8f493187 91static const wxChar* const wxEndiannessNames[] =
8bb6b2c0
VZ
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
78e9b418 104static unsigned wxGetIndexFromEnumValue(int value)
8bb6b2c0 105{
78e9b418 106 wxCHECK_MSG( value, (unsigned)-1, _T("invalid enum value") );
8bb6b2c0
VZ
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
124wxPlatformInfo::wxPlatformInfo()
125{
449090b5
VZ
126 // just copy platform info for currently running platform
127 *this = Get();
8bb6b2c0
VZ
128}
129
130wxPlatformInfo::wxPlatformInfo(wxPortId pid, int tkMajor, int tkMinor,
131 wxOperatingSystemId id, int osMajor, int osMinor,
132 wxArchitecture arch,
b98bd6af
VS
133 wxEndianness endian,
134 bool usingUniversal)
8bb6b2c0
VZ
135{
136 m_tkVersionMajor = tkMajor;
137 m_tkVersionMinor = tkMinor;
138 m_port = pid;
b98bd6af 139 m_usingUniversal = usingUniversal;
8bb6b2c0
VZ
140
141 m_os = id;
142 m_osVersionMajor = osMajor;
143 m_osVersionMinor = osMinor;
144
145 m_endian = endian;
146 m_arch = arch;
147}
148
149bool 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 &&
b98bd6af 157 m_usingUniversal == t.m_usingUniversal &&
8bb6b2c0
VZ
158 m_arch == t.m_arch &&
159 m_endian == t.m_endian;
160}
161
449090b5
VZ
162void 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 */
187const 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
8bb6b2c0
VZ
201// ----------------------------------------------------------------------------
202// wxPlatformInfo - enum -> string conversions
203// ----------------------------------------------------------------------------
204
205wxString wxPlatformInfo::GetOperatingSystemFamilyName(wxOperatingSystemId os)
206{
8f493187 207 const wxChar* string = _T("Unknown");
8bb6b2c0 208 if ( os & wxOS_MAC )
8f493187 209 string = _T("Macintosh");
8bb6b2c0 210 else if ( os & wxOS_WINDOWS )
8f493187 211 string = _T("Windows");
8bb6b2c0 212 else if ( os & wxOS_UNIX )
8f493187 213 string = _T("Unix");
8bb6b2c0 214 else if ( os == wxOS_DOS )
8f493187 215 string = _T("DOS");
8bb6b2c0 216 else if ( os == wxOS_OS2 )
8f493187 217 string = _T("OS/2");
8bb6b2c0 218
8f493187 219 return string;
8bb6b2c0
VZ
220}
221
222wxString wxPlatformInfo::GetOperatingSystemIdName(wxOperatingSystemId os)
223{
78e9b418 224 const unsigned idx = wxGetIndexFromEnumValue(os);
8bb6b2c0
VZ
225
226 wxCHECK_MSG( idx < WXSIZEOF(wxOperatingSystemIdNames), wxEmptyString,
227 _T("invalid OS id") );
228
229 return wxOperatingSystemIdNames[idx];
230}
231
b98bd6af 232wxString wxPlatformInfo::GetPortIdName(wxPortId port, bool usingUniversal)
8bb6b2c0 233{
78e9b418 234 const unsigned idx = wxGetIndexFromEnumValue(port);
8bb6b2c0
VZ
235
236 wxCHECK_MSG( idx < WXSIZEOF(wxPortIdNames), wxEmptyString,
237 _T("invalid port id") );
238
239 wxString ret = wxPortIdNames[idx];
240
b98bd6af 241 if ( usingUniversal )
8bb6b2c0
VZ
242 ret += wxT("/wxUniversal");
243
244 return ret;
245}
246
b98bd6af 247wxString wxPlatformInfo::GetPortIdShortName(wxPortId port, bool usingUniversal)
8bb6b2c0 248{
78e9b418 249 const unsigned idx = wxGetIndexFromEnumValue(port);
8bb6b2c0
VZ
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
b98bd6af 257 if ( usingUniversal )
8bb6b2c0
VZ
258 ret += wxT("univ");
259
260 return ret;
261}
262
263wxString wxPlatformInfo::GetArchName(wxArchitecture arch)
264{
265 wxCOMPILE_TIME_ASSERT( WXSIZEOF(wxArchitectureNames) == wxARCH_MAX,
266 wxArchitectureNamesMismatch );
267
268 return wxArchitectureNames[arch];
269}
270
271wxString 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
284wxOperatingSystemId wxPlatformInfo::GetOperatingSystemId(const wxString &str)
285{
286 for ( size_t i = 0; i < WXSIZEOF(wxOperatingSystemIdNames); i++ )
287 {
8f493187 288 if ( wxString(wxOperatingSystemIdNames[i]).CmpNoCase(str) == 0 )
8bb6b2c0
VZ
289 return (wxOperatingSystemId)(1 << i);
290 }
291
292 return wxOS_UNKNOWN;
293}
294
295wxPortId 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
8f493187
PC
302 if ( wxString(wxPortIdNames[i]).CmpNoCase(str) == 0 ||
303 GetPortIdShortName(current, true).CmpNoCase(str) == 0 ||
b98bd6af 304 GetPortIdShortName(current, false).CmpNoCase(str) == 0 )
8bb6b2c0
VZ
305 return current;
306 }
307
308 return wxPORT_UNKNOWN;
309}
310
311wxArchitecture 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
322wxEndianness 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