]>
Commit | Line | Data |
---|---|---|
2bda0e17 | 1 | ///////////////////////////////////////////////////////////////////////////// |
8caa4ed1 | 2 | // Name: msw/utils.cpp |
2bda0e17 KB |
3 | // Purpose: Various utilities |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
743e0a66 | 9 | // Licence: wxWindows license |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
b568d04f VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2bda0e17 | 20 | #ifdef __GNUG__ |
a3b46648 | 21 | // #pragma implementation "utils.h" // Note: this is done in utilscmn.cpp now. |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
a4f96412 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
31 | #ifndef WX_PRECOMP | |
a4f96412 VZ |
32 | #include "wx/utils.h" |
33 | #include "wx/app.h" | |
34 | #include "wx/cursor.h" | |
1f0500b3 VZ |
35 | #include "wx/intl.h" |
36 | #include "wx/log.h" | |
743e0a66 | 37 | #endif //WX_PRECOMP |
2bda0e17 | 38 | |
26993c4f GRG |
39 | // In some mingws there is a missing extern "C" int the winsock header, |
40 | // so we put it here just to be safe. Note that this must appear _before_ | |
ec5d7799 | 41 | // #include "wx/msw/private.h" which itself includes <windows.h>, as this |
26993c4f GRG |
42 | // one in turn includes <winsock.h> unless we define WIN32_LEAN_AND_MEAN. |
43 | // | |
04ef50df | 44 | #if defined(__WIN32__) && !defined(__TWIN32__) && !defined(__WXMICROWIN__) && ! (defined(__GNUWIN32__) && !defined(__MINGW32__)) |
26993c4f GRG |
45 | extern "C" { |
46 | #include <winsock.h> // we use socket functions in wxGetFullHostName() | |
47 | } | |
48 | #endif | |
49 | ||
b568d04f VZ |
50 | #include "wx/msw/private.h" // includes <windows.h> |
51 | ||
2bda0e17 KB |
52 | #include "wx/timer.h" |
53 | ||
04ef50df | 54 | #if !defined(__GNUWIN32__) && !defined(__WXWINE__) && !defined(__SALFORDC__) && !defined(__WXMICROWIN__) |
a4f96412 | 55 | #include <direct.h> |
ce3ed50d | 56 | |
a4f96412 VZ |
57 | #ifndef __MWERKS__ |
58 | #include <dos.h> | |
59 | #endif | |
743e0a66 | 60 | #endif //GNUWIN32 |
2bda0e17 | 61 | |
57c208c5 | 62 | #if defined(__GNUWIN32__) && !defined(__TWIN32__) |
a4f96412 VZ |
63 | #include <sys/unistd.h> |
64 | #include <sys/stat.h> | |
743e0a66 VZ |
65 | #endif //GNUWIN32 |
66 | ||
2bda0e17 KB |
67 | #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs |
68 | // this (3.1 I believe) and how to test for it. | |
69 | // If this works for Borland 4.0 as well, then no worries. | |
a4f96412 | 70 | #include <dir.h> |
2bda0e17 KB |
71 | #endif |
72 | ||
a4f96412 VZ |
73 | // VZ: there is some code using NetXXX() functions to get the full user name: |
74 | // I don't think it's a good idea because they don't work under Win95 and | |
75 | // seem to return the same as wxGetUserId() under NT. If you really want | |
76 | // to use them, just #define USE_NET_API | |
77 | #undef USE_NET_API | |
78 | ||
79 | #ifdef USE_NET_API | |
80 | #include <lm.h> | |
81 | #endif // USE_NET_API | |
82 | ||
04ef50df | 83 | #if defined(__WIN32__) && !defined(__WXWINE__) && !defined(__WXMICROWIN__) |
a4f96412 | 84 | #include <io.h> |
2bda0e17 | 85 | |
a4f96412 VZ |
86 | #ifndef __GNUWIN32__ |
87 | #include <shellapi.h> | |
88 | #endif | |
2bda0e17 KB |
89 | #endif |
90 | ||
2bda0e17 | 91 | #ifndef __WATCOMC__ |
3f4a0c5b VZ |
92 | #if !(defined(_MSC_VER) && (_MSC_VER > 800)) |
93 | #include <errno.h> | |
94 | #endif | |
2bda0e17 | 95 | #endif |
2bda0e17 | 96 | |
c030b70f | 97 | //// BEGIN for console support: VC++ only |
631f1bfe | 98 | #ifdef __VISUALC__ |
c030b70f | 99 | |
3f4a0c5b | 100 | #include "wx/msw/msvcrt.h" |
c030b70f | 101 | |
3f4a0c5b | 102 | #include <fcntl.h> |
c030b70f | 103 | |
3f4a0c5b | 104 | #include "wx/ioswrap.h" |
c030b70f | 105 | |
1e6feb95 | 106 | #include "wx/ioswrap.h" |
c030b70f JS |
107 | |
108 | /* Need to undef new if including crtdbg.h */ | |
109 | # ifdef new | |
110 | # undef new | |
111 | # endif | |
112 | ||
1e6d9499 | 113 | #ifndef __WIN16__ |
c030b70f | 114 | # include <crtdbg.h> |
1e6d9499 | 115 | #endif |
c030b70f JS |
116 | |
117 | # if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS | |
f6bcfd97 | 118 | # define new new(__TFILE__,__LINE__) |
c030b70f JS |
119 | # endif |
120 | ||
631f1bfe JS |
121 | #endif |
122 | // __VISUALC__ | |
c030b70f JS |
123 | /// END for console support |
124 | ||
b568d04f VZ |
125 | // ---------------------------------------------------------------------------- |
126 | // constants | |
127 | // ---------------------------------------------------------------------------- | |
128 | ||
2bda0e17 | 129 | // In the WIN.INI file |
223d09f6 | 130 | static const wxChar WX_SECTION[] = wxT("wxWindows"); |
b568d04f VZ |
131 | static const wxChar eUSERNAME[] = wxT("UserName"); |
132 | ||
133 | // these are only used under Win16 | |
04ef50df | 134 | #if !defined(__WIN32__) && !defined(__WXMICROWIN__) |
223d09f6 KB |
135 | static const wxChar eHOSTNAME[] = wxT("HostName"); |
136 | static const wxChar eUSERID[] = wxT("UserId"); | |
b568d04f VZ |
137 | #endif // !Win32 |
138 | ||
04ef50df JS |
139 | #ifndef __WXMICROWIN__ |
140 | ||
b568d04f VZ |
141 | // ============================================================================ |
142 | // implementation | |
143 | // ============================================================================ | |
2bda0e17 | 144 | |
b568d04f VZ |
145 | // ---------------------------------------------------------------------------- |
146 | // get host name and related | |
147 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 148 | |
1f0500b3 | 149 | // Get hostname only (without domain name) |
837e5743 | 150 | bool wxGetHostName(wxChar *buf, int maxSize) |
2bda0e17 | 151 | { |
04ef50df | 152 | #if defined(__WIN32__) && !defined(__TWIN32__) && !defined(__WXMICROWIN__) |
b568d04f VZ |
153 | DWORD nSize = maxSize; |
154 | if ( !::GetComputerName(buf, &nSize) ) | |
155 | { | |
f6bcfd97 | 156 | wxLogLastError(wxT("GetComputerName")); |
b568d04f VZ |
157 | |
158 | return FALSE; | |
159 | } | |
160 | ||
161 | return TRUE; | |
2bda0e17 | 162 | #else |
b568d04f VZ |
163 | wxChar *sysname; |
164 | const wxChar *default_host = wxT("noname"); | |
165 | ||
166 | if ((sysname = wxGetenv(wxT("SYSTEM_NAME"))) == NULL) { | |
167 | GetProfileString(WX_SECTION, eHOSTNAME, default_host, buf, maxSize - 1); | |
168 | } else | |
169 | wxStrncpy(buf, sysname, maxSize - 1); | |
170 | buf[maxSize] = wxT('\0'); | |
171 | return *buf ? TRUE : FALSE; | |
2bda0e17 KB |
172 | #endif |
173 | } | |
174 | ||
1f0500b3 | 175 | // get full hostname (with domain name if possible) |
b568d04f VZ |
176 | bool wxGetFullHostName(wxChar *buf, int maxSize) |
177 | { | |
04ef50df | 178 | #if defined(__WIN32__) && !defined(__TWIN32__) && !defined(__WXMICROWIN__) && ! (defined(__GNUWIN32__) && !defined(__MINGW32__)) |
1f0500b3 VZ |
179 | // TODO should use GetComputerNameEx() when available |
180 | WSADATA wsa; | |
181 | if ( WSAStartup(MAKEWORD(1, 1), &wsa) == 0 ) | |
182 | { | |
183 | wxString host; | |
184 | char bufA[256]; | |
185 | if ( gethostname(bufA, WXSIZEOF(bufA)) == 0 ) | |
186 | { | |
187 | // gethostname() won't usually include the DNS domain name, for | |
188 | // this we need to work a bit more | |
189 | if ( !strchr(bufA, '.') ) | |
190 | { | |
191 | struct hostent *pHostEnt = gethostbyname(bufA); | |
192 | ||
193 | if ( pHostEnt ) | |
194 | { | |
195 | // Windows will use DNS internally now | |
196 | pHostEnt = gethostbyaddr(pHostEnt->h_addr, 4, PF_INET); | |
197 | } | |
198 | ||
199 | if ( pHostEnt ) | |
200 | { | |
201 | host = pHostEnt->h_name; | |
202 | } | |
203 | } | |
204 | } | |
205 | ||
206 | WSACleanup(); | |
207 | ||
208 | if ( !!host ) | |
209 | { | |
210 | wxStrncpy(buf, host, maxSize); | |
211 | ||
212 | return TRUE; | |
213 | } | |
214 | } | |
215 | #endif // Win32 | |
216 | ||
b568d04f VZ |
217 | return wxGetHostName(buf, maxSize); |
218 | } | |
219 | ||
2bda0e17 | 220 | // Get user ID e.g. jacs |
837e5743 | 221 | bool wxGetUserId(wxChar *buf, int maxSize) |
2bda0e17 | 222 | { |
04ef50df | 223 | #if defined(__WIN32__) && !defined(__win32s__) && !defined(__TWIN32__) && !defined(__WXMICROWIN__) |
0a144271 VZ |
224 | DWORD nSize = maxSize; |
225 | if ( ::GetUserName(buf, &nSize) == 0 ) | |
226 | { | |
a4f96412 | 227 | // actually, it does happen on Win9x if the user didn't log on |
223d09f6 | 228 | DWORD res = ::GetEnvironmentVariable(wxT("username"), buf, maxSize); |
a4f96412 VZ |
229 | if ( res == 0 ) |
230 | { | |
231 | // not found | |
232 | return FALSE; | |
233 | } | |
0a144271 VZ |
234 | } |
235 | ||
236 | return TRUE; | |
0a144271 | 237 | #else // Win16 or Win32s |
b568d04f VZ |
238 | wxChar *user; |
239 | const wxChar *default_id = wxT("anonymous"); | |
2bda0e17 | 240 | |
b568d04f VZ |
241 | // Can't assume we have NIS (PC-NFS) or some other ID daemon |
242 | // So we ... | |
243 | if ( (user = wxGetenv(wxT("USER"))) == NULL && | |
244 | (user = wxGetenv(wxT("LOGNAME"))) == NULL ) | |
245 | { | |
246 | // Use wxWindows configuration data (comming soon) | |
247 | GetProfileString(WX_SECTION, eUSERID, default_id, buf, maxSize - 1); | |
248 | } | |
249 | else | |
250 | { | |
251 | wxStrncpy(buf, user, maxSize - 1); | |
252 | } | |
a4f96412 | 253 | |
b568d04f | 254 | return *buf ? TRUE : FALSE; |
2bda0e17 KB |
255 | #endif |
256 | } | |
257 | ||
258 | // Get user name e.g. Julian Smart | |
837e5743 | 259 | bool wxGetUserName(wxChar *buf, int maxSize) |
2bda0e17 | 260 | { |
0a144271 | 261 | #if wxUSE_PENWINDOWS && !defined(__WATCOMC__) && !defined(__GNUWIN32__) |
b568d04f VZ |
262 | extern HANDLE g_hPenWin; // PenWindows Running? |
263 | if (g_hPenWin) | |
264 | { | |
265 | // PenWindows Does have a user concept! | |
266 | // Get the current owner of the recognizer | |
267 | GetPrivateProfileString("Current", "User", default_name, wxBuffer, maxSize - 1, "PENWIN.INI"); | |
268 | strncpy(buf, wxBuffer, maxSize - 1); | |
269 | } | |
270 | else | |
2bda0e17 | 271 | #endif |
b568d04f | 272 | { |
a4f96412 | 273 | #ifdef USE_NET_API |
b568d04f VZ |
274 | CHAR szUserName[256]; |
275 | if ( !wxGetUserId(szUserName, WXSIZEOF(szUserName)) ) | |
276 | return FALSE; | |
a4f96412 | 277 | |
b568d04f VZ |
278 | // TODO how to get the domain name? |
279 | CHAR *szDomain = ""; | |
a4f96412 | 280 | |
b568d04f VZ |
281 | // the code is based on the MSDN example (also see KB article Q119670) |
282 | WCHAR wszUserName[256]; // Unicode user name | |
283 | WCHAR wszDomain[256]; | |
284 | LPBYTE ComputerName; | |
a4f96412 | 285 | |
b568d04f | 286 | USER_INFO_2 *ui2; // User structure |
a4f96412 | 287 | |
b568d04f VZ |
288 | // Convert ANSI user name and domain to Unicode |
289 | MultiByteToWideChar( CP_ACP, 0, szUserName, strlen(szUserName)+1, | |
290 | wszUserName, WXSIZEOF(wszUserName) ); | |
291 | MultiByteToWideChar( CP_ACP, 0, szDomain, strlen(szDomain)+1, | |
292 | wszDomain, WXSIZEOF(wszDomain) ); | |
a4f96412 | 293 | |
b568d04f VZ |
294 | // Get the computer name of a DC for the domain. |
295 | if ( NetGetDCName( NULL, wszDomain, &ComputerName ) != NERR_Success ) | |
296 | { | |
297 | wxLogError(wxT("Can not find domain controller")); | |
a4f96412 | 298 | |
b568d04f VZ |
299 | goto error; |
300 | } | |
a4f96412 | 301 | |
b568d04f VZ |
302 | // Look up the user on the DC |
303 | NET_API_STATUS status = NetUserGetInfo( (LPWSTR)ComputerName, | |
304 | (LPWSTR)&wszUserName, | |
305 | 2, // level - we want USER_INFO_2 | |
306 | (LPBYTE *) &ui2 ); | |
307 | switch ( status ) | |
308 | { | |
309 | case NERR_Success: | |
310 | // ok | |
311 | break; | |
a4f96412 | 312 | |
b568d04f VZ |
313 | case NERR_InvalidComputer: |
314 | wxLogError(wxT("Invalid domain controller name.")); | |
a4f96412 | 315 | |
b568d04f | 316 | goto error; |
a4f96412 | 317 | |
b568d04f VZ |
318 | case NERR_UserNotFound: |
319 | wxLogError(wxT("Invalid user name '%s'."), szUserName); | |
a4f96412 | 320 | |
b568d04f | 321 | goto error; |
a4f96412 | 322 | |
b568d04f VZ |
323 | default: |
324 | wxLogSysError(wxT("Can't get information about user")); | |
a4f96412 | 325 | |
b568d04f VZ |
326 | goto error; |
327 | } | |
a4f96412 | 328 | |
b568d04f VZ |
329 | // Convert the Unicode full name to ANSI |
330 | WideCharToMultiByte( CP_ACP, 0, ui2->usri2_full_name, -1, | |
331 | buf, maxSize, NULL, NULL ); | |
a4f96412 | 332 | |
b568d04f | 333 | return TRUE; |
a4f96412 VZ |
334 | |
335 | error: | |
b568d04f | 336 | wxLogError(wxT("Couldn't look up full user name.")); |
a4f96412 | 337 | |
b568d04f | 338 | return FALSE; |
a4f96412 | 339 | #else // !USE_NET_API |
b568d04f VZ |
340 | // Could use NIS, MS-Mail or other site specific programs |
341 | // Use wxWindows configuration data | |
342 | bool ok = GetProfileString(WX_SECTION, eUSERNAME, wxT(""), buf, maxSize - 1) != 0; | |
343 | if ( !ok ) | |
344 | { | |
345 | ok = wxGetUserId(buf, maxSize); | |
346 | } | |
0a144271 | 347 | |
b568d04f VZ |
348 | if ( !ok ) |
349 | { | |
350 | wxStrncpy(buf, wxT("Unknown User"), maxSize); | |
351 | } | |
a4f96412 | 352 | #endif // Win32/16 |
b568d04f | 353 | } |
0a144271 | 354 | |
b568d04f | 355 | return TRUE; |
2bda0e17 KB |
356 | } |
357 | ||
b568d04f | 358 | const wxChar* wxGetHomeDir(wxString *pstr) |
2bda0e17 | 359 | { |
b568d04f | 360 | wxString& strDir = *pstr; |
2bda0e17 | 361 | |
b568d04f VZ |
362 | #if defined(__UNIX__) && !defined(__TWIN32__) |
363 | const wxChar *szHome = wxGetenv("HOME"); | |
364 | if ( szHome == NULL ) { | |
365 | // we're homeless... | |
366 | wxLogWarning(_("can't find user's HOME, using current directory.")); | |
367 | strDir = wxT("."); | |
368 | } | |
369 | else | |
370 | strDir = szHome; | |
2bda0e17 | 371 | |
b568d04f VZ |
372 | // add a trailing slash if needed |
373 | if ( strDir.Last() != wxT('/') ) | |
374 | strDir << wxT('/'); | |
375 | #else // Windows | |
376 | #ifdef __WIN32__ | |
377 | const wxChar *szHome = wxGetenv(wxT("HOMEDRIVE")); | |
378 | if ( szHome != NULL ) | |
379 | strDir << szHome; | |
380 | szHome = wxGetenv(wxT("HOMEPATH")); | |
381 | if ( szHome != NULL ) { | |
382 | strDir << szHome; | |
2bda0e17 | 383 | |
b568d04f VZ |
384 | // the idea is that under NT these variables have default values |
385 | // of "%systemdrive%:" and "\\". As we don't want to create our | |
386 | // config files in the root directory of the system drive, we will | |
387 | // create it in our program's dir. However, if the user took care | |
388 | // to set HOMEPATH to something other than "\\", we suppose that he | |
389 | // knows what he is doing and use the supplied value. | |
390 | if ( wxStrcmp(szHome, wxT("\\")) != 0 ) | |
391 | return strDir.c_str(); | |
392 | } | |
2bda0e17 | 393 | |
b568d04f VZ |
394 | #else // Win16 |
395 | // Win16 has no idea about home, so use the working directory instead | |
396 | #endif // WIN16/32 | |
2bda0e17 | 397 | |
b568d04f VZ |
398 | // 260 was taken from windef.h |
399 | #ifndef MAX_PATH | |
400 | #define MAX_PATH 260 | |
401 | #endif | |
2bda0e17 | 402 | |
b568d04f VZ |
403 | wxString strPath; |
404 | ::GetModuleFileName(::GetModuleHandle(NULL), | |
405 | strPath.GetWriteBuf(MAX_PATH), MAX_PATH); | |
406 | strPath.UngetWriteBuf(); | |
2bda0e17 | 407 | |
b568d04f VZ |
408 | // extract the dir name |
409 | wxSplitPath(strPath, &strDir, NULL, NULL); | |
58a33cb4 | 410 | |
b568d04f VZ |
411 | #endif // UNIX/Win |
412 | ||
413 | return strDir.c_str(); | |
afb74891 VZ |
414 | } |
415 | ||
33ac7e6f | 416 | wxChar *wxGetUserHome(const wxString& WXUNUSED(user)) |
2bda0e17 | 417 | { |
b568d04f VZ |
418 | // VZ: the old code here never worked for user != "" anyhow! Moreover, it |
419 | // returned sometimes a malloc()'d pointer, sometimes a pointer to a | |
420 | // static buffer and sometimes I don't even know what. | |
421 | static wxString s_home; | |
2bda0e17 | 422 | |
b568d04f | 423 | return (wxChar *)wxGetHomeDir(&s_home); |
2bda0e17 KB |
424 | } |
425 | ||
b568d04f | 426 | bool wxDirExists(const wxString& dir) |
2bda0e17 | 427 | { |
04ef50df JS |
428 | #ifdef __WXMICROWIN__ |
429 | return wxPathExist(dir); | |
430 | #elif defined(__WIN32__) | |
cf1eeea3 | 431 | DWORD attribs = GetFileAttributes(dir); |
3897b707 | 432 | return ((attribs != (DWORD)-1) && (attribs & FILE_ATTRIBUTE_DIRECTORY)); |
b568d04f VZ |
433 | #else // Win16 |
434 | #ifdef __BORLANDC__ | |
435 | struct ffblk fileInfo; | |
436 | #else | |
437 | struct find_t fileInfo; | |
438 | #endif | |
b568d04f VZ |
439 | // In Borland findfirst has a different argument |
440 | // ordering from _dos_findfirst. But _dos_findfirst | |
441 | // _should_ be ok in both MS and Borland... why not? | |
442 | #ifdef __BORLANDC__ | |
443 | return (findfirst(dir, &fileInfo, _A_SUBDIR) == 0 && | |
444 | (fileInfo.ff_attrib & _A_SUBDIR) != 0); | |
445 | #else | |
446 | return (_dos_findfirst(dir, _A_SUBDIR, &fileInfo) == 0) && | |
447 | ((fileInfo.attrib & _A_SUBDIR) != 0); | |
448 | #endif | |
449 | #endif // Win32/16 | |
450 | } | |
2bda0e17 | 451 | |
eadd7bd2 VZ |
452 | bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree) |
453 | { | |
454 | if ( path.empty() ) | |
455 | return FALSE; | |
456 | ||
457 | #ifdef __WIN32__ | |
458 | // GetDiskFreeSpaceEx() is not available under original Win95, check for | |
459 | // it | |
460 | typedef BOOL (*GetDiskFreeSpaceEx_t)(LPCTSTR, | |
461 | PULARGE_INTEGER, | |
462 | PULARGE_INTEGER, | |
463 | PULARGE_INTEGER); | |
464 | ||
465 | GetDiskFreeSpaceEx_t | |
466 | pGetDiskFreeSpaceEx = ::GetProcAddress | |
467 | ( | |
468 | ::GetModuleHandle(_T("kernel32.dll")), | |
469 | #if wxUSE_UNICODE | |
470 | "GetDiskFreeSpaceExW" | |
471 | #else | |
472 | "GetDiskFreeSpaceExA" | |
473 | #endif | |
474 | ); | |
475 | ||
476 | if ( pGetDiskFreeSpaceEx ) | |
477 | { | |
478 | ULARGE_INTEGER bytesFree, bytesTotal; | |
479 | ||
480 | // may pass the path as is, GetDiskFreeSpaceEx() is smart enough | |
481 | if ( !pGetDiskFreeSpaceEx(path, | |
482 | &bytesFree, | |
483 | &bytesTotal, | |
484 | NULL) ) | |
485 | { | |
486 | wxLogLastError(_T("GetDiskFreeSpaceEx")); | |
487 | ||
488 | return FALSE; | |
489 | } | |
490 | ||
491 | if ( pTotal ) | |
492 | { | |
493 | *pTotal = wxLongLong(bytesTotal.HighPart, bytesTotal.LowPart); | |
494 | } | |
495 | ||
496 | if ( pFree ) | |
497 | { | |
498 | *pFree = wxLongLong(bytesFree.HighPart, bytesFree.LowPart); | |
499 | } | |
500 | } | |
501 | else | |
502 | #endif // Win32 | |
503 | { | |
504 | DWORD lSectorsPerCluster, | |
505 | lBytesPerSector, | |
506 | lNumberOfFreeClusters, | |
507 | lTotalNumberOfClusters; | |
508 | ||
509 | // FIXME: this is wrong, we should extract the root drive from path | |
510 | // instead, but this is the job for wxFileName... | |
511 | if ( !::GetDiskFreeSpace(path, | |
512 | &lSectorsPerCluster, | |
513 | &lBytesPerSector, | |
514 | &lNumberOfFreeClusters, | |
515 | &lTotalNumberOfClusters) ) | |
516 | { | |
517 | wxLogLastError(_T("GetDiskFreeSpace")); | |
518 | ||
519 | return FALSE; | |
520 | } | |
521 | ||
522 | // there's a problem with drives larger than 2GB on non Win32!! | |
523 | // let's calculate it, nevertheless.... | |
524 | wxLongLong lBytesPerCluster = lSectorsPerCluster; | |
525 | lBytesPerCluster *= lBytesPerSector; | |
526 | ||
527 | if ( pTotal ) | |
528 | { | |
529 | *pTotal = lBytesPerCluster; | |
530 | *pTotal *= lTotalNumberOfClusters; | |
531 | } | |
532 | ||
533 | if ( pFree ) | |
534 | { | |
535 | *pFree = lBytesPerCluster; | |
536 | *pFree *= lNumberOfFreeClusters; | |
537 | } | |
538 | } | |
539 | ||
540 | return TRUE; | |
541 | } | |
542 | ||
308978f6 VZ |
543 | // ---------------------------------------------------------------------------- |
544 | // env vars | |
545 | // ---------------------------------------------------------------------------- | |
546 | ||
547 | bool wxGetEnv(const wxString& var, wxString *value) | |
548 | { | |
788722ac JS |
549 | #ifdef __WIN16__ |
550 | const wxChar* ret = wxGetenv(var); | |
551 | if (ret) | |
552 | { | |
553 | *value = ret; | |
554 | return TRUE; | |
555 | } | |
556 | else | |
557 | return FALSE; | |
558 | #else | |
308978f6 VZ |
559 | // first get the size of the buffer |
560 | DWORD dwRet = ::GetEnvironmentVariable(var, NULL, 0); | |
561 | if ( !dwRet ) | |
562 | { | |
563 | // this means that there is no such variable | |
564 | return FALSE; | |
565 | } | |
566 | ||
567 | if ( value ) | |
568 | { | |
569 | (void)::GetEnvironmentVariable(var, value->GetWriteBuf(dwRet), dwRet); | |
570 | value->UngetWriteBuf(); | |
571 | } | |
572 | ||
573 | return TRUE; | |
788722ac | 574 | #endif |
308978f6 VZ |
575 | } |
576 | ||
1fb45475 VZ |
577 | bool wxSetEnv(const wxString& var, const wxChar *value) |
578 | { | |
579 | // some compilers have putenv() or _putenv() or _wputenv() but it's better | |
580 | // to always use Win32 function directly instead of dealing with them | |
581 | #if defined(__WIN32__) | |
582 | if ( !::SetEnvironmentVariable(var, value) ) | |
583 | { | |
584 | wxLogLastError(_T("SetEnvironmentVariable")); | |
585 | ||
586 | return FALSE; | |
587 | } | |
588 | ||
589 | return TRUE; | |
590 | #else // no way to set env vars | |
591 | return FALSE; | |
592 | #endif | |
593 | } | |
594 | ||
b568d04f VZ |
595 | // ---------------------------------------------------------------------------- |
596 | // process management | |
597 | // ---------------------------------------------------------------------------- | |
598 | ||
50567b69 VZ |
599 | #ifdef __WIN32__ |
600 | ||
601 | // structure used to pass parameters from wxKill() to wxEnumFindByPidProc() | |
602 | struct wxFindByPidParams | |
b568d04f | 603 | { |
50567b69 VZ |
604 | wxFindByPidParams() { hwnd = 0; pid = 0; } |
605 | ||
606 | // the HWND used to return the result | |
607 | HWND hwnd; | |
608 | ||
609 | // the PID we're looking from | |
610 | DWORD pid; | |
611 | }; | |
612 | ||
613 | // wxKill helper: EnumWindows() callback which is used to find the first (top | |
614 | // level) window belonging to the given process | |
615 | BOOL CALLBACK wxEnumFindByPidProc(HWND hwnd, LPARAM lParam) | |
616 | { | |
617 | DWORD pid; | |
618 | (void)::GetWindowThreadProcessId(hwnd, &pid); | |
619 | ||
620 | wxFindByPidParams *params = (wxFindByPidParams *)lParam; | |
621 | if ( pid == params->pid ) | |
622 | { | |
623 | // remember the window we found | |
624 | params->hwnd = hwnd; | |
625 | ||
626 | // return FALSE to stop the enumeration | |
627 | return FALSE; | |
628 | } | |
629 | ||
630 | // continue enumeration | |
631 | return TRUE; | |
632 | } | |
e949bf13 | 633 | |
50567b69 VZ |
634 | #endif // __WIN32__ |
635 | ||
636 | int wxKill(long pid, wxSignal sig, wxKillError *krc) | |
637 | { | |
638 | #ifdef __WIN32__ | |
639 | // get the process handle to operate on | |
640 | HANDLE hProcess = ::OpenProcess(SYNCHRONIZE | | |
641 | PROCESS_TERMINATE | | |
642 | PROCESS_QUERY_INFORMATION, | |
643 | FALSE, // not inheritable | |
644 | (DWORD)pid); | |
645 | if ( hProcess == NULL ) | |
e949bf13 | 646 | { |
50567b69 | 647 | if ( krc ) |
e949bf13 | 648 | { |
50567b69 | 649 | if ( ::GetLastError() == ERROR_ACCESS_DENIED ) |
e949bf13 | 650 | { |
50567b69 VZ |
651 | *krc = wxKILL_ACCESS_DENIED; |
652 | } | |
653 | else | |
654 | { | |
655 | *krc = wxKILL_NO_PROCESS; | |
656 | } | |
657 | } | |
658 | ||
659 | return -1; | |
660 | } | |
661 | ||
662 | bool ok = TRUE; | |
663 | switch ( sig ) | |
664 | { | |
665 | case wxSIGKILL: | |
666 | // kill the process forcefully returning -1 as error code | |
667 | if ( !::TerminateProcess(hProcess, (UINT)-1) ) | |
668 | { | |
669 | wxLogSysError(_("Failed to kill process %d"), pid); | |
670 | ||
671 | if ( krc ) | |
e949bf13 | 672 | { |
50567b69 VZ |
673 | // this is not supposed to happen if we could open the |
674 | // process | |
675 | *krc = wxKILL_ERROR; | |
e949bf13 | 676 | } |
50567b69 VZ |
677 | |
678 | ok = FALSE; | |
e949bf13 | 679 | } |
50567b69 VZ |
680 | break; |
681 | ||
682 | case wxSIGNONE: | |
683 | // do nothing, we just want to test for process existence | |
684 | break; | |
685 | ||
686 | default: | |
687 | // any other signal means "terminate" | |
688 | { | |
689 | wxFindByPidParams params; | |
690 | params.pid = (DWORD)pid; | |
691 | ||
692 | // EnumWindows() has nice semantics: it returns 0 if it found | |
693 | // something or if an error occured and non zero if it | |
694 | // enumerated all the window | |
695 | if ( !::EnumWindows(wxEnumFindByPidProc, (LPARAM)¶ms) ) | |
696 | { | |
697 | // did we find any window? | |
698 | if ( params.hwnd ) | |
699 | { | |
700 | // tell the app to close | |
701 | // | |
702 | // NB: this is the harshest way, the app won't have | |
703 | // opportunity to save any files, for example, but | |
704 | // this is probably what we want here. If not we | |
705 | // can also use SendMesageTimeout(WM_CLOSE) | |
706 | if ( !::PostMessage(params.hwnd, WM_QUIT, 0, 0) ) | |
707 | { | |
708 | wxLogLastError(_T("PostMessage(WM_QUIT)")); | |
709 | } | |
710 | } | |
711 | else // it was an error then | |
712 | { | |
713 | wxLogLastError(_T("EnumWindows")); | |
714 | ||
715 | ok = FALSE; | |
716 | } | |
717 | } | |
718 | else // no windows for this PID | |
719 | { | |
720 | if ( krc ) | |
721 | { | |
722 | *krc = wxKILL_ERROR; | |
723 | } | |
724 | ||
725 | ok = FALSE; | |
726 | } | |
727 | } | |
728 | } | |
729 | ||
730 | // the return code | |
731 | DWORD rc; | |
732 | ||
733 | if ( ok ) | |
734 | { | |
735 | // as we wait for a short time, we can use just WaitForSingleObject() | |
736 | // and not MsgWaitForMultipleObjects() | |
737 | switch ( ::WaitForSingleObject(hProcess, 500 /* msec */) ) | |
738 | { | |
739 | case WAIT_OBJECT_0: | |
740 | // process terminated | |
741 | if ( !::GetExitCodeProcess(hProcess, &rc) ) | |
742 | { | |
743 | wxLogLastError(_T("GetExitCodeProcess")); | |
744 | } | |
745 | break; | |
746 | ||
747 | default: | |
748 | wxFAIL_MSG( _T("unexpected WaitForSingleObject() return") ); | |
749 | // fall through | |
750 | ||
751 | case WAIT_FAILED: | |
752 | wxLogLastError(_T("WaitForSingleObject")); | |
753 | // fall through | |
754 | ||
755 | case WAIT_TIMEOUT: | |
756 | if ( krc ) | |
757 | { | |
758 | *krc = wxKILL_ERROR; | |
759 | } | |
760 | ||
761 | rc = STILL_ACTIVE; | |
762 | break; | |
e949bf13 JS |
763 | } |
764 | } | |
50567b69 VZ |
765 | else // !ok |
766 | { | |
767 | // just to suppress the warnings about uninitialized variable | |
768 | rc = 0; | |
769 | } | |
e949bf13 | 770 | |
50567b69 | 771 | ::CloseHandle(hProcess); |
b568d04f | 772 | |
50567b69 VZ |
773 | // the return code is the same as from Unix kill(): 0 if killed |
774 | // successfully or -1 on error | |
775 | if ( sig == wxSIGNONE ) | |
e949bf13 | 776 | { |
50567b69 VZ |
777 | if ( ok && rc == STILL_ACTIVE ) |
778 | { | |
779 | // there is such process => success | |
e949bf13 | 780 | return 0; |
50567b69 | 781 | } |
e949bf13 | 782 | } |
50567b69 | 783 | else // not SIGNONE |
e949bf13 | 784 | { |
50567b69 VZ |
785 | if ( ok && rc != STILL_ACTIVE ) |
786 | { | |
787 | // killed => success | |
e949bf13 | 788 | return 0; |
50567b69 | 789 | } |
e949bf13 | 790 | } |
50567b69 VZ |
791 | #else // Win15 |
792 | wxFAIL_MSG( _T("not implemented") ); | |
793 | #endif // Win32/Win16 | |
794 | ||
795 | // error | |
796 | return -1; | |
2bda0e17 KB |
797 | } |
798 | ||
b568d04f VZ |
799 | // Execute a program in an Interactive Shell |
800 | bool wxShell(const wxString& command) | |
2bda0e17 | 801 | { |
b568d04f VZ |
802 | wxChar *shell = wxGetenv(wxT("COMSPEC")); |
803 | if ( !shell ) | |
804 | shell = wxT("\\COMMAND.COM"); | |
805 | ||
806 | wxString cmd; | |
807 | if ( !command ) | |
808 | { | |
809 | // just the shell | |
810 | cmd = shell; | |
811 | } | |
812 | else | |
813 | { | |
814 | // pass the command to execute to the command processor | |
815 | cmd.Printf(wxT("%s /c %s"), shell, command.c_str()); | |
816 | } | |
817 | ||
0d7ea902 | 818 | return wxExecute(cmd, TRUE /* sync */) != 0; |
2bda0e17 KB |
819 | } |
820 | ||
b568d04f VZ |
821 | // ---------------------------------------------------------------------------- |
822 | // misc | |
823 | // ---------------------------------------------------------------------------- | |
824 | ||
825 | // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX) | |
826 | long wxGetFreeMemory() | |
2bda0e17 | 827 | { |
b568d04f VZ |
828 | #if defined(__WIN32__) && !defined(__BORLANDC__) && !defined(__TWIN32__) |
829 | MEMORYSTATUS memStatus; | |
830 | memStatus.dwLength = sizeof(MEMORYSTATUS); | |
831 | GlobalMemoryStatus(&memStatus); | |
832 | return memStatus.dwAvailPhys; | |
833 | #else | |
834 | return (long)GetFreeSpace(0); | |
835 | #endif | |
2bda0e17 KB |
836 | } |
837 | ||
838 | // Emit a beeeeeep | |
634903fd | 839 | void wxBell() |
2bda0e17 | 840 | { |
b568d04f | 841 | ::MessageBeep((UINT)-1); // default sound |
2bda0e17 KB |
842 | } |
843 | ||
bdc72a22 | 844 | wxString wxGetOsDescription() |
2bda0e17 | 845 | { |
bdc72a22 VZ |
846 | #ifdef __WIN32__ |
847 | wxString str; | |
848 | ||
849 | OSVERSIONINFO info; | |
850 | wxZeroMemory(info); | |
851 | ||
852 | info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | |
853 | if ( ::GetVersionEx(&info) ) | |
854 | { | |
855 | switch ( info.dwPlatformId ) | |
856 | { | |
857 | case VER_PLATFORM_WIN32s: | |
858 | str = _("Win32s on Windows 3.1"); | |
859 | break; | |
860 | ||
861 | case VER_PLATFORM_WIN32_WINDOWS: | |
862 | str.Printf(_("Windows 9%c"), | |
a46a73a6 | 863 | info.dwMinorVersion == 0 ? _T('5') : _T('8')); |
bdc72a22 VZ |
864 | if ( !wxIsEmpty(info.szCSDVersion) ) |
865 | { | |
866 | str << _T(" (") << info.szCSDVersion << _T(')'); | |
867 | } | |
868 | break; | |
869 | ||
870 | case VER_PLATFORM_WIN32_NT: | |
871 | str.Printf(_T("Windows NT %lu.%lu (build %lu"), | |
872 | info.dwMajorVersion, | |
873 | info.dwMinorVersion, | |
874 | info.dwBuildNumber); | |
875 | if ( !wxIsEmpty(info.szCSDVersion) ) | |
876 | { | |
877 | str << _T(", ") << info.szCSDVersion; | |
878 | } | |
879 | str << _T(')'); | |
880 | break; | |
881 | } | |
882 | } | |
883 | else | |
884 | { | |
885 | wxFAIL_MSG( _T("GetVersionEx() failed") ); // should never happen | |
886 | } | |
887 | ||
888 | return str; | |
889 | #else // Win16 | |
890 | return _("Windows 3.1"); | |
891 | #endif // Win32/16 | |
892 | } | |
6f65e337 | 893 | |
bdc72a22 VZ |
894 | int wxGetOsVersion(int *majorVsn, int *minorVsn) |
895 | { | |
2432b92d | 896 | #if defined(__WIN32__) && !defined(__SC__) |
bdc72a22 VZ |
897 | OSVERSIONINFO info; |
898 | wxZeroMemory(info); | |
899 | ||
900 | info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); | |
901 | if ( ::GetVersionEx(&info) ) | |
902 | { | |
903 | if (majorVsn) | |
904 | *majorVsn = info.dwMajorVersion; | |
905 | if (minorVsn) | |
906 | *minorVsn = info.dwMinorVersion; | |
ec5d7799 | 907 | |
bdc72a22 VZ |
908 | switch ( info.dwPlatformId ) |
909 | { | |
910 | case VER_PLATFORM_WIN32s: | |
911 | return wxWIN32S; | |
912 | ||
913 | case VER_PLATFORM_WIN32_WINDOWS: | |
914 | return wxWIN95; | |
915 | ||
916 | case VER_PLATFORM_WIN32_NT: | |
917 | return wxWINDOWS_NT; | |
918 | } | |
919 | } | |
920 | ||
921 | return wxWINDOWS; // error if we get here, return generic value | |
922 | #else // Win16 | |
923 | int retValue = wxWINDOWS; | |
924 | #ifdef __WINDOWS_386__ | |
925 | retValue = wxWIN386; | |
926 | #else | |
927 | #if !defined(__WATCOMC__) && !defined(GNUWIN32) && wxUSE_PENWINDOWS | |
928 | extern HANDLE g_hPenWin; | |
929 | retValue = g_hPenWin ? wxPENWINDOWS : wxWINDOWS; | |
930 | #endif | |
931 | #endif | |
932 | ||
933 | if (majorVsn) | |
934 | *majorVsn = 3; | |
935 | if (minorVsn) | |
936 | *minorVsn = 1; | |
937 | ||
938 | return retValue; | |
6f65e337 | 939 | #endif |
2bda0e17 KB |
940 | } |
941 | ||
b568d04f VZ |
942 | // ---------------------------------------------------------------------------- |
943 | // sleep functions | |
944 | // ---------------------------------------------------------------------------- | |
945 | ||
946 | #if wxUSE_GUI | |
947 | ||
1e6feb95 VZ |
948 | #if wxUSE_TIMER |
949 | ||
b568d04f VZ |
950 | // Sleep for nSecs seconds. Attempt a Windows implementation using timers. |
951 | static bool gs_inTimer = FALSE; | |
952 | ||
1e6feb95 | 953 | class wxSleepTimer : public wxTimer |
b568d04f VZ |
954 | { |
955 | public: | |
956 | virtual void Notify() | |
957 | { | |
958 | gs_inTimer = FALSE; | |
959 | Stop(); | |
960 | } | |
961 | }; | |
962 | ||
963 | static wxTimer *wxTheSleepTimer = NULL; | |
964 | ||
965 | void wxUsleep(unsigned long milliseconds) | |
966 | { | |
967 | #ifdef __WIN32__ | |
968 | ::Sleep(milliseconds); | |
1e6feb95 | 969 | #else // !Win32 |
b568d04f VZ |
970 | if (gs_inTimer) |
971 | return; | |
972 | ||
973 | wxTheSleepTimer = new wxSleepTimer; | |
974 | gs_inTimer = TRUE; | |
975 | wxTheSleepTimer->Start(milliseconds); | |
976 | while (gs_inTimer) | |
977 | { | |
978 | if (wxTheApp->Pending()) | |
979 | wxTheApp->Dispatch(); | |
980 | } | |
981 | delete wxTheSleepTimer; | |
982 | wxTheSleepTimer = NULL; | |
1e6feb95 | 983 | #endif // Win32/!Win32 |
b568d04f VZ |
984 | } |
985 | ||
986 | void wxSleep(int nSecs) | |
987 | { | |
988 | if (gs_inTimer) | |
989 | return; | |
990 | ||
991 | wxTheSleepTimer = new wxSleepTimer; | |
992 | gs_inTimer = TRUE; | |
993 | wxTheSleepTimer->Start(nSecs*1000); | |
994 | while (gs_inTimer) | |
995 | { | |
996 | if (wxTheApp->Pending()) | |
997 | wxTheApp->Dispatch(); | |
998 | } | |
999 | delete wxTheSleepTimer; | |
1000 | wxTheSleepTimer = NULL; | |
1001 | } | |
1002 | ||
1003 | // Consume all events until no more left | |
1004 | void wxFlushEvents() | |
1005 | { | |
1006 | // wxYield(); | |
1007 | } | |
1008 | ||
1e6feb95 VZ |
1009 | #endif // wxUSE_TIMER |
1010 | ||
b568d04f VZ |
1011 | #elif defined(__WIN32__) // wxUSE_GUI |
1012 | ||
1013 | void wxUsleep(unsigned long milliseconds) | |
1014 | { | |
1015 | ::Sleep(milliseconds); | |
1016 | } | |
1017 | ||
1018 | void wxSleep(int nSecs) | |
1019 | { | |
1020 | wxUsleep(1000*nSecs); | |
1021 | } | |
1022 | ||
1023 | #endif // wxUSE_GUI/!wxUSE_GUI | |
04ef50df JS |
1024 | #endif |
1025 | // __WXMICROWIN__ | |
b568d04f VZ |
1026 | |
1027 | // ---------------------------------------------------------------------------- | |
1028 | // deprecated (in favour of wxLog) log functions | |
1029 | // ---------------------------------------------------------------------------- | |
1030 | ||
1031 | #if wxUSE_GUI | |
1032 | ||
1033 | // Output a debug mess., in a system dependent fashion. | |
04ef50df | 1034 | #ifndef __WXMICROWIN__ |
b568d04f VZ |
1035 | void wxDebugMsg(const wxChar *fmt ...) |
1036 | { | |
1037 | va_list ap; | |
1038 | static wxChar buffer[512]; | |
1039 | ||
1040 | if (!wxTheApp->GetWantDebugOutput()) | |
1041 | return ; | |
1042 | ||
1043 | va_start(ap, fmt); | |
1044 | ||
1045 | wvsprintf(buffer,fmt,ap) ; | |
1046 | OutputDebugString((LPCTSTR)buffer) ; | |
1047 | ||
1048 | va_end(ap); | |
1049 | } | |
1050 | ||
1051 | // Non-fatal error: pop up message box and (possibly) continue | |
1052 | void wxError(const wxString& msg, const wxString& title) | |
1053 | { | |
1054 | wxSprintf(wxBuffer, wxT("%s\nContinue?"), WXSTRINGCAST msg); | |
1055 | if (MessageBox(NULL, (LPCTSTR)wxBuffer, (LPCTSTR)WXSTRINGCAST title, | |
1056 | MB_ICONSTOP | MB_YESNO) == IDNO) | |
1057 | wxExit(); | |
1058 | } | |
1059 | ||
1060 | // Fatal error: pop up message box and abort | |
1061 | void wxFatalError(const wxString& msg, const wxString& title) | |
1062 | { | |
1063 | wxSprintf(wxBuffer, wxT("%s: %s"), WXSTRINGCAST title, WXSTRINGCAST msg); | |
1064 | FatalAppExit(0, (LPCTSTR)wxBuffer); | |
1065 | } | |
04ef50df | 1066 | #endif // __WXMICROWIN__ |
b568d04f VZ |
1067 | |
1068 | // ---------------------------------------------------------------------------- | |
1069 | // functions to work with .INI files | |
1070 | // ---------------------------------------------------------------------------- | |
1071 | ||
2bda0e17 | 1072 | // Reading and writing resources (eg WIN.INI, .Xdefaults) |
47d67540 | 1073 | #if wxUSE_RESOURCES |
2bda0e17 KB |
1074 | bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file) |
1075 | { | |
223d09f6 | 1076 | if (file != wxT("")) |
8fb3a512 JS |
1077 | // Don't know what the correct cast should be, but it doesn't |
1078 | // compile in BC++/16-bit without this cast. | |
1079 | #if !defined(__WIN32__) | |
1080 | return (WritePrivateProfileString((const char*) section, (const char*) entry, (const char*) value, (const char*) file) != 0); | |
1081 | #else | |
837e5743 | 1082 | return (WritePrivateProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)value, (LPCTSTR)WXSTRINGCAST file) != 0); |
8fb3a512 | 1083 | #endif |
2bda0e17 | 1084 | else |
837e5743 | 1085 | return (WriteProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)WXSTRINGCAST value) != 0); |
2bda0e17 KB |
1086 | } |
1087 | ||
1088 | bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file) | |
1089 | { | |
b568d04f VZ |
1090 | wxString buf; |
1091 | buf.Printf(wxT("%.4f"), value); | |
1092 | ||
1093 | return wxWriteResource(section, entry, buf, file); | |
2bda0e17 KB |
1094 | } |
1095 | ||
1096 | bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file) | |
1097 | { | |
b568d04f VZ |
1098 | wxString buf; |
1099 | buf.Printf(wxT("%ld"), value); | |
1100 | ||
1101 | return wxWriteResource(section, entry, buf, file); | |
2bda0e17 KB |
1102 | } |
1103 | ||
1104 | bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file) | |
1105 | { | |
b568d04f VZ |
1106 | wxString buf; |
1107 | buf.Printf(wxT("%d"), value); | |
1108 | ||
1109 | return wxWriteResource(section, entry, buf, file); | |
2bda0e17 KB |
1110 | } |
1111 | ||
837e5743 | 1112 | bool wxGetResource(const wxString& section, const wxString& entry, wxChar **value, const wxString& file) |
2bda0e17 | 1113 | { |
b568d04f VZ |
1114 | static const wxChar defunkt[] = wxT("$$default"); |
1115 | if (file != wxT("")) | |
1116 | { | |
1117 | int n = GetPrivateProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)defunkt, | |
1118 | (LPTSTR)wxBuffer, 1000, (LPCTSTR)WXSTRINGCAST file); | |
1119 | if (n == 0 || wxStrcmp(wxBuffer, defunkt) == 0) | |
1120 | return FALSE; | |
1121 | } | |
1122 | else | |
1123 | { | |
1124 | int n = GetProfileString((LPCTSTR)WXSTRINGCAST section, (LPCTSTR)WXSTRINGCAST entry, (LPCTSTR)defunkt, | |
1125 | (LPTSTR)wxBuffer, 1000); | |
1126 | if (n == 0 || wxStrcmp(wxBuffer, defunkt) == 0) | |
1127 | return FALSE; | |
2bda0e17 | 1128 | } |
b568d04f VZ |
1129 | if (*value) delete[] (*value); |
1130 | *value = copystring(wxBuffer); | |
1131 | return TRUE; | |
1132 | } | |
2bda0e17 KB |
1133 | |
1134 | bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file) | |
1135 | { | |
b568d04f VZ |
1136 | wxChar *s = NULL; |
1137 | bool succ = wxGetResource(section, entry, (wxChar **)&s, file); | |
1138 | if (succ) | |
1139 | { | |
1140 | *value = (float)wxStrtod(s, NULL); | |
1141 | delete[] s; | |
1142 | return TRUE; | |
1143 | } | |
1144 | else return FALSE; | |
2bda0e17 KB |
1145 | } |
1146 | ||
1147 | bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file) | |
1148 | { | |
b568d04f VZ |
1149 | wxChar *s = NULL; |
1150 | bool succ = wxGetResource(section, entry, (wxChar **)&s, file); | |
1151 | if (succ) | |
1152 | { | |
1153 | *value = wxStrtol(s, NULL, 10); | |
1154 | delete[] s; | |
1155 | return TRUE; | |
1156 | } | |
1157 | else return FALSE; | |
2bda0e17 KB |
1158 | } |
1159 | ||
1160 | bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file) | |
1161 | { | |
b568d04f VZ |
1162 | wxChar *s = NULL; |
1163 | bool succ = wxGetResource(section, entry, (wxChar **)&s, file); | |
1164 | if (succ) | |
1165 | { | |
1166 | *value = (int)wxStrtol(s, NULL, 10); | |
1167 | delete[] s; | |
1168 | return TRUE; | |
1169 | } | |
1170 | else return FALSE; | |
2bda0e17 | 1171 | } |
47d67540 | 1172 | #endif // wxUSE_RESOURCES |
2bda0e17 | 1173 | |
634903fd | 1174 | // --------------------------------------------------------------------------- |
25889d3c | 1175 | // helper functions for showing a "busy" cursor |
634903fd VZ |
1176 | // --------------------------------------------------------------------------- |
1177 | ||
bfbd6dc1 VZ |
1178 | static HCURSOR gs_wxBusyCursor = 0; // new, busy cursor |
1179 | static HCURSOR gs_wxBusyCursorOld = 0; // old cursor | |
634903fd | 1180 | static int gs_wxBusyCursorCount = 0; |
2bda0e17 | 1181 | |
bfbd6dc1 VZ |
1182 | extern HCURSOR wxGetCurrentBusyCursor() |
1183 | { | |
1184 | return gs_wxBusyCursor; | |
1185 | } | |
1186 | ||
2bda0e17 KB |
1187 | // Set the cursor to the busy cursor for all windows |
1188 | void wxBeginBusyCursor(wxCursor *cursor) | |
1189 | { | |
634903fd VZ |
1190 | if ( gs_wxBusyCursorCount++ == 0 ) |
1191 | { | |
1192 | gs_wxBusyCursor = (HCURSOR)cursor->GetHCURSOR(); | |
04ef50df | 1193 | #ifndef __WXMICROWIN__ |
634903fd | 1194 | gs_wxBusyCursorOld = ::SetCursor(gs_wxBusyCursor); |
04ef50df | 1195 | #endif |
634903fd VZ |
1196 | } |
1197 | //else: nothing to do, already set | |
2bda0e17 KB |
1198 | } |
1199 | ||
1200 | // Restore cursor to normal | |
634903fd | 1201 | void wxEndBusyCursor() |
2bda0e17 | 1202 | { |
634903fd | 1203 | wxCHECK_RET( gs_wxBusyCursorCount > 0, |
223d09f6 | 1204 | wxT("no matching wxBeginBusyCursor() for wxEndBusyCursor()") ); |
b07135ba | 1205 | |
634903fd VZ |
1206 | if ( --gs_wxBusyCursorCount == 0 ) |
1207 | { | |
04ef50df | 1208 | #ifndef __WXMICROWIN__ |
634903fd | 1209 | ::SetCursor(gs_wxBusyCursorOld); |
04ef50df | 1210 | #endif |
634903fd VZ |
1211 | gs_wxBusyCursorOld = 0; |
1212 | } | |
2bda0e17 KB |
1213 | } |
1214 | ||
1215 | // TRUE if we're between the above two calls | |
634903fd | 1216 | bool wxIsBusy() |
2bda0e17 | 1217 | { |
634903fd | 1218 | return (gs_wxBusyCursorCount > 0); |
b07135ba | 1219 | } |
2bda0e17 | 1220 | |
2bda0e17 KB |
1221 | // Check whether this window wants to process messages, e.g. Stop button |
1222 | // in long calculations. | |
1223 | bool wxCheckForInterrupt(wxWindow *wnd) | |
1224 | { | |
b568d04f VZ |
1225 | wxCHECK( wnd, FALSE ); |
1226 | ||
743e0a66 | 1227 | MSG msg; |
b568d04f VZ |
1228 | while ( ::PeekMessage(&msg, GetHwndOf(wnd), 0, 0, PM_REMOVE) ) |
1229 | { | |
1230 | ::TranslateMessage(&msg); | |
1231 | ::DispatchMessage(&msg); | |
743e0a66 | 1232 | } |
634903fd | 1233 | |
b568d04f | 1234 | return TRUE; |
2bda0e17 KB |
1235 | } |
1236 | ||
1237 | // MSW only: get user-defined resource from the .res file. | |
1238 | // Returns NULL or newly-allocated memory, so use delete[] to clean up. | |
1239 | ||
04ef50df | 1240 | #ifndef __WXMICROWIN__ |
837e5743 | 1241 | wxChar *wxLoadUserResource(const wxString& resourceName, const wxString& resourceType) |
2bda0e17 | 1242 | { |
b568d04f VZ |
1243 | HRSRC hResource = ::FindResource(wxGetInstance(), resourceName, resourceType); |
1244 | if ( hResource == 0 ) | |
1245 | return NULL; | |
1246 | ||
1247 | HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource); | |
1248 | if ( hData == 0 ) | |
1249 | return NULL; | |
2bda0e17 | 1250 | |
b568d04f VZ |
1251 | wxChar *theText = (wxChar *)::LockResource(hData); |
1252 | if ( !theText ) | |
1253 | return NULL; | |
b07135ba | 1254 | |
8caa4ed1 JS |
1255 | // Not all compilers put a zero at the end of the resource (e.g. BC++ doesn't). |
1256 | // so we need to find the length of the resource. | |
1257 | int len = ::SizeofResource(wxGetInstance(), hResource); | |
1258 | wxChar *s = new wxChar[len+1]; | |
1259 | wxStrncpy(s,theText,len); | |
1260 | s[len]=0; | |
1261 | ||
1262 | // wxChar *s = copystring(theText); | |
2bda0e17 | 1263 | |
b568d04f | 1264 | // Obsolete in WIN32 |
2bda0e17 | 1265 | #ifndef __WIN32__ |
b568d04f | 1266 | UnlockResource(hData); |
2bda0e17 KB |
1267 | #endif |
1268 | ||
b568d04f VZ |
1269 | // No need?? |
1270 | // GlobalFree(hData); | |
2bda0e17 | 1271 | |
b568d04f | 1272 | return s; |
2bda0e17 | 1273 | } |
04ef50df | 1274 | #endif |
b568d04f VZ |
1275 | |
1276 | // ---------------------------------------------------------------------------- | |
1277 | // get display info | |
1278 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1279 | |
d3db92b2 JS |
1280 | // See also the wxGetMousePosition in window.cpp |
1281 | // Deprecated: use wxPoint wxGetMousePosition() instead | |
2bda0e17 KB |
1282 | void wxGetMousePosition( int* x, int* y ) |
1283 | { | |
b568d04f VZ |
1284 | POINT pt; |
1285 | GetCursorPos( & pt ); | |
1286 | if ( x ) *x = pt.x; | |
1287 | if ( y ) *y = pt.y; | |
2bda0e17 KB |
1288 | }; |
1289 | ||
1290 | // Return TRUE if we have a colour display | |
634903fd | 1291 | bool wxColourDisplay() |
2bda0e17 | 1292 | { |
04ef50df JS |
1293 | #ifdef __WXMICROWIN__ |
1294 | // MICROWIN_TODO | |
1295 | return TRUE; | |
1296 | #else | |
807a903e VZ |
1297 | // this function is called from wxDC ctor so it is called a *lot* of times |
1298 | // hence we optimize it a bit but doign the check only once | |
1299 | // | |
1300 | // this should be MT safe as only the GUI thread (holding the GUI mutex) | |
1301 | // can call us | |
1302 | static int s_isColour = -1; | |
1303 | ||
1304 | if ( s_isColour == -1 ) | |
1305 | { | |
1306 | ScreenHDC dc; | |
1307 | int noCols = ::GetDeviceCaps(dc, NUMCOLORS); | |
1308 | ||
1309 | s_isColour = (noCols == -1) || (noCols > 2); | |
1310 | } | |
b568d04f | 1311 | |
807a903e | 1312 | return s_isColour != 0; |
04ef50df | 1313 | #endif |
2bda0e17 KB |
1314 | } |
1315 | ||
1316 | // Returns depth of screen | |
634903fd | 1317 | int wxDisplayDepth() |
2bda0e17 | 1318 | { |
b568d04f VZ |
1319 | ScreenHDC dc; |
1320 | return GetDeviceCaps(dc, PLANES) * GetDeviceCaps(dc, BITSPIXEL); | |
2bda0e17 KB |
1321 | } |
1322 | ||
1323 | // Get size of display | |
1324 | void wxDisplaySize(int *width, int *height) | |
1325 | { | |
04ef50df | 1326 | #ifdef __WXMICROWIN__ |
54800df8 JS |
1327 | RECT rect; |
1328 | HWND hWnd = GetDesktopWindow(); | |
1329 | ::GetWindowRect(hWnd, & rect); | |
1330 | ||
1331 | *width = rect.right - rect.left; | |
1332 | *height = rect.bottom - rect.top; | |
04ef50df | 1333 | #else |
b568d04f | 1334 | ScreenHDC dc; |
3f4a0c5b | 1335 | |
b568d04f VZ |
1336 | if ( width ) *width = GetDeviceCaps(dc, HORZRES); |
1337 | if ( height ) *height = GetDeviceCaps(dc, VERTRES); | |
04ef50df | 1338 | #endif |
7f555861 JS |
1339 | } |
1340 | ||
904a68b6 RL |
1341 | void wxDisplaySizeMM(int *width, int *height) |
1342 | { | |
04ef50df JS |
1343 | #ifdef __WXMICROWIN__ |
1344 | // MICROWIN_TODO | |
1345 | *width = 0; * height = 0; | |
1346 | #else | |
904a68b6 RL |
1347 | ScreenHDC dc; |
1348 | ||
1349 | if ( width ) *width = GetDeviceCaps(dc, HORZSIZE); | |
1350 | if ( height ) *height = GetDeviceCaps(dc, VERTSIZE); | |
04ef50df | 1351 | #endif |
904a68b6 RL |
1352 | } |
1353 | ||
ec5d7799 RD |
1354 | void wxClientDisplayRect(int *x, int *y, int *width, int *height) |
1355 | { | |
04ef50df | 1356 | #if defined(__WIN16__) || defined(__WXMICROWIN__) |
788722ac JS |
1357 | *x = 0; *y = 0; |
1358 | wxDisplaySize(width, height); | |
1359 | #else | |
ec5d7799 RD |
1360 | // Determine the desktop dimensions minus the taskbar and any other |
1361 | // special decorations... | |
1362 | RECT r; | |
788722ac | 1363 | |
ec5d7799 RD |
1364 | SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0); |
1365 | if (x) *x = r.left; | |
1366 | if (y) *y = r.top; | |
1367 | if (width) *width = r.right - r.left; | |
1368 | if (height) *height = r.bottom - r.top; | |
788722ac | 1369 | #endif |
ec5d7799 RD |
1370 | } |
1371 | ||
904a68b6 | 1372 | |
cc2b7472 VZ |
1373 | // --------------------------------------------------------------------------- |
1374 | // window information functions | |
1375 | // --------------------------------------------------------------------------- | |
1376 | ||
1c4a764c VZ |
1377 | wxString WXDLLEXPORT wxGetWindowText(WXHWND hWnd) |
1378 | { | |
1379 | wxString str; | |
1380 | int len = GetWindowTextLength((HWND)hWnd) + 1; | |
1381 | GetWindowText((HWND)hWnd, str.GetWriteBuf(len), len); | |
1382 | str.UngetWriteBuf(); | |
1383 | ||
1384 | return str; | |
1385 | } | |
1386 | ||
cc2b7472 VZ |
1387 | wxString WXDLLEXPORT wxGetWindowClass(WXHWND hWnd) |
1388 | { | |
04ef50df JS |
1389 | #ifdef __WXMICROWIN__ |
1390 | // MICROWIN_TODO | |
1391 | return wxEmptyString; | |
1392 | #else | |
cc2b7472 VZ |
1393 | wxString str; |
1394 | ||
1395 | int len = 256; // some starting value | |
1396 | ||
1397 | for ( ;; ) | |
1398 | { | |
1399 | // as we've #undefined GetClassName we must now manually choose the | |
1400 | // right function to call | |
1401 | int count = | |
1402 | ||
1403 | #ifndef __WIN32__ | |
1404 | GetClassName | |
1405 | #else // Win32 | |
1406 | #ifdef UNICODE | |
1407 | GetClassNameW | |
1408 | #else // !Unicode | |
1409 | #ifdef __TWIN32__ | |
1410 | GetClassName | |
1411 | #else // !Twin32 | |
1412 | GetClassNameA | |
1413 | #endif // Twin32/!Twin32 | |
1414 | #endif // Unicode/ANSI | |
1415 | #endif // Win16/32 | |
1416 | ((HWND)hWnd, str.GetWriteBuf(len), len); | |
1417 | ||
1418 | str.UngetWriteBuf(); | |
1419 | if ( count == len ) | |
1420 | { | |
1421 | // the class name might have been truncated, retry with larger | |
1422 | // buffer | |
1423 | len *= 2; | |
1424 | } | |
1425 | else | |
1426 | { | |
1427 | break; | |
1428 | } | |
1429 | } | |
1430 | ||
1431 | return str; | |
04ef50df | 1432 | #endif |
cc2b7472 VZ |
1433 | } |
1434 | ||
42e69d6b | 1435 | WXWORD WXDLLEXPORT wxGetWindowId(WXHWND hWnd) |
cc2b7472 VZ |
1436 | { |
1437 | #ifndef __WIN32__ | |
33ac7e6f | 1438 | return (WXWORD)GetWindowWord((HWND)hWnd, GWW_ID); |
cc2b7472 | 1439 | #else // Win32 |
33ac7e6f | 1440 | return (WXWORD)GetWindowLong((HWND)hWnd, GWL_ID); |
cc2b7472 VZ |
1441 | #endif // Win16/32 |
1442 | } | |
1443 | ||
f6bcfd97 BP |
1444 | #endif // wxUSE_GUI |
1445 | ||
b0c89ddf JS |
1446 | #if wxUSE_GUI |
1447 | ||
1448 | // ---------------------------------------------------------------------------- | |
1449 | // Metafile helpers | |
1450 | // ---------------------------------------------------------------------------- | |
1451 | ||
1452 | extern void PixelToHIMETRIC(LONG *x, LONG *y) | |
1453 | { | |
1454 | ScreenHDC hdcRef; | |
1455 | ||
1456 | int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE), | |
1457 | iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE), | |
1458 | iWidthPels = GetDeviceCaps(hdcRef, HORZRES), | |
1459 | iHeightPels = GetDeviceCaps(hdcRef, VERTRES); | |
1460 | ||
1461 | *x *= (iWidthMM * 100); | |
1462 | *x /= iWidthPels; | |
1463 | *y *= (iHeightMM * 100); | |
1464 | *y /= iHeightPels; | |
1465 | } | |
1466 | ||
1467 | extern void HIMETRICToPixel(LONG *x, LONG *y) | |
1468 | { | |
1469 | ScreenHDC hdcRef; | |
1470 | ||
1471 | int iWidthMM = GetDeviceCaps(hdcRef, HORZSIZE), | |
1472 | iHeightMM = GetDeviceCaps(hdcRef, VERTSIZE), | |
1473 | iWidthPels = GetDeviceCaps(hdcRef, HORZRES), | |
1474 | iHeightPels = GetDeviceCaps(hdcRef, VERTRES); | |
1475 | ||
1476 | *x *= iWidthPels; | |
1477 | *x /= (iWidthMM * 100); | |
1478 | *y *= iHeightPels; | |
1479 | *y /= (iHeightMM * 100); | |
1480 | } | |
1481 | ||
1482 | #endif // wxUSE_GUI | |
1483 | ||
7f555861 JS |
1484 | #if 0 |
1485 | //------------------------------------------------------------------------ | |
1486 | // wild character routines | |
1487 | //------------------------------------------------------------------------ | |
1488 | ||
1489 | bool wxIsWild( const wxString& pattern ) | |
1490 | { | |
1491 | wxString tmp = pattern; | |
1492 | char *pat = WXSTRINGCAST(tmp); | |
1493 | while (*pat) { | |
3f4a0c5b VZ |
1494 | switch (*pat++) { |
1495 | case '?': case '*': case '[': case '{': | |
1496 | return TRUE; | |
1497 | case '\\': | |
1498 | if (!*pat++) | |
1499 | return FALSE; | |
1500 | } | |
7f555861 JS |
1501 | } |
1502 | return FALSE; | |
1503 | }; | |
1504 | ||
1505 | ||
1506 | bool wxMatchWild( const wxString& pat, const wxString& text, bool dot_special ) | |
1507 | { | |
1508 | wxString tmp1 = pat; | |
1509 | char *pattern = WXSTRINGCAST(tmp1); | |
1510 | wxString tmp2 = text; | |
1511 | char *str = WXSTRINGCAST(tmp2); | |
1512 | char c; | |
1513 | char *cp; | |
1514 | bool done = FALSE, ret_code, ok; | |
1515 | // Below is for vi fans | |
1516 | const char OB = '{', CB = '}'; | |
1517 | ||
1518 | // dot_special means '.' only matches '.' | |
1519 | if (dot_special && *str == '.' && *pattern != *str) | |
3f4a0c5b | 1520 | return FALSE; |
7f555861 JS |
1521 | |
1522 | while ((*pattern != '\0') && (!done) | |
1523 | && (((*str=='\0')&&((*pattern==OB)||(*pattern=='*')))||(*str!='\0'))) { | |
3f4a0c5b VZ |
1524 | switch (*pattern) { |
1525 | case '\\': | |
1526 | pattern++; | |
1527 | if (*pattern != '\0') | |
1528 | pattern++; | |
1529 | break; | |
1530 | case '*': | |
1531 | pattern++; | |
1532 | ret_code = FALSE; | |
1533 | while ((*str!='\0') | |
1534 | && (!(ret_code=wxMatchWild(pattern, str++, FALSE)))) | |
1535 | /*loop*/; | |
1536 | if (ret_code) { | |
1537 | while (*str != '\0') | |
1538 | str++; | |
1539 | while (*pattern != '\0') | |
1540 | pattern++; | |
1541 | } | |
1542 | break; | |
1543 | case '[': | |
1544 | pattern++; | |
1545 | repeat: | |
1546 | if ((*pattern == '\0') || (*pattern == ']')) { | |
1547 | done = TRUE; | |
1548 | break; | |
1549 | } | |
1550 | if (*pattern == '\\') { | |
1551 | pattern++; | |
1552 | if (*pattern == '\0') { | |
1553 | done = TRUE; | |
1554 | break; | |
1555 | } | |
1556 | } | |
1557 | if (*(pattern + 1) == '-') { | |
1558 | c = *pattern; | |
1559 | pattern += 2; | |
1560 | if (*pattern == ']') { | |
1561 | done = TRUE; | |
1562 | break; | |
1563 | } | |
1564 | if (*pattern == '\\') { | |
1565 | pattern++; | |
1566 | if (*pattern == '\0') { | |
1567 | done = TRUE; | |
1568 | break; | |
1569 | } | |
1570 | } | |
1571 | if ((*str < c) || (*str > *pattern)) { | |
1572 | pattern++; | |
1573 | goto repeat; | |
1574 | } | |
1575 | } else if (*pattern != *str) { | |
1576 | pattern++; | |
1577 | goto repeat; | |
1578 | } | |
1579 | pattern++; | |
1580 | while ((*pattern != ']') && (*pattern != '\0')) { | |
1581 | if ((*pattern == '\\') && (*(pattern + 1) != '\0')) | |
1582 | pattern++; | |
1583 | pattern++; | |
1584 | } | |
1585 | if (*pattern != '\0') { | |
1586 | pattern++, str++; | |
1587 | } | |
1588 | break; | |
1589 | case '?': | |
1590 | pattern++; | |
1591 | str++; | |
1592 | break; | |
1593 | case OB: | |
1594 | pattern++; | |
1595 | while ((*pattern != CB) && (*pattern != '\0')) { | |
1596 | cp = str; | |
1597 | ok = TRUE; | |
1598 | while (ok && (*cp != '\0') && (*pattern != '\0') | |
1599 | && (*pattern != ',') && (*pattern != CB)) { | |
1600 | if (*pattern == '\\') | |
1601 | pattern++; | |
1602 | ok = (*pattern++ == *cp++); | |
1603 | } | |
1604 | if (*pattern == '\0') { | |
1605 | ok = FALSE; | |
1606 | done = TRUE; | |
1607 | break; | |
1608 | } else if (ok) { | |
1609 | str = cp; | |
1610 | while ((*pattern != CB) && (*pattern != '\0')) { | |
1611 | if (*++pattern == '\\') { | |
1612 | if (*++pattern == CB) | |
1613 | pattern++; | |
1614 | } | |
1615 | } | |
1616 | } else { | |
1617 | while (*pattern!=CB && *pattern!=',' && *pattern!='\0') { | |
1618 | if (*++pattern == '\\') { | |
7f555861 | 1619 | if (*++pattern == CB || *pattern == ',') |
3f4a0c5b VZ |
1620 | pattern++; |
1621 | } | |
1622 | } | |
1623 | } | |
1624 | if (*pattern != '\0') | |
1625 | pattern++; | |
1626 | } | |
1627 | break; | |
1628 | default: | |
1629 | if (*str == *pattern) { | |
1630 | str++, pattern++; | |
1631 | } else { | |
1632 | done = TRUE; | |
1633 | } | |
1634 | } | |
7f555861 JS |
1635 | } |
1636 | while (*pattern == '*') | |
3f4a0c5b | 1637 | pattern++; |
7f555861 JS |
1638 | return ((*str == '\0') && (*pattern == '\0')); |
1639 | }; | |
1640 | ||
f6bcfd97 | 1641 | #endif // 0 |
7f555861 | 1642 | |
cf45d1f4 | 1643 | #if 0 |
c030b70f JS |
1644 | |
1645 | // maximum mumber of lines the output console should have | |
1646 | static const WORD MAX_CONSOLE_LINES = 500; | |
1647 | ||
1648 | BOOL WINAPI MyConsoleHandler( DWORD dwCtrlType ) { // control signal type | |
3f4a0c5b VZ |
1649 | FreeConsole(); |
1650 | return TRUE; | |
c030b70f JS |
1651 | } |
1652 | ||
1653 | void wxRedirectIOToConsole() | |
1654 | { | |
1655 | int hConHandle; | |
1656 | long lStdHandle; | |
1657 | CONSOLE_SCREEN_BUFFER_INFO coninfo; | |
1658 | FILE *fp; | |
1659 | ||
1660 | // allocate a console for this app | |
1661 | AllocConsole(); | |
1662 | ||
1663 | // set the screen buffer to be big enough to let us scroll text | |
3f4a0c5b | 1664 | GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), |
c030b70f JS |
1665 | &coninfo); |
1666 | coninfo.dwSize.Y = MAX_CONSOLE_LINES; | |
3f4a0c5b | 1667 | SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), |
c030b70f JS |
1668 | coninfo.dwSize); |
1669 | ||
1670 | // redirect unbuffered STDOUT to the console | |
1671 | lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE); | |
1672 | hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); | |
1673 | if(hConHandle <= 0) return; | |
1674 | fp = _fdopen( hConHandle, "w" ); | |
1675 | *stdout = *fp; | |
1676 | setvbuf( stdout, NULL, _IONBF, 0 ); | |
1677 | ||
1678 | // redirect unbuffered STDIN to the console | |
1679 | lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE); | |
1680 | hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); | |
1681 | if(hConHandle <= 0) return; | |
1682 | fp = _fdopen( hConHandle, "r" ); | |
1683 | *stdin = *fp; | |
1684 | setvbuf( stdin, NULL, _IONBF, 0 ); | |
1685 | ||
1686 | // redirect unbuffered STDERR to the console | |
1687 | lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE); | |
1688 | hConHandle = _open_osfhandle(lStdHandle, _O_TEXT); | |
1689 | if(hConHandle <= 0) return; | |
1690 | fp = _fdopen( hConHandle, "w" ); | |
1691 | *stderr = *fp; | |
1692 | setvbuf( stderr, NULL, _IONBF, 0 ); | |
3f4a0c5b VZ |
1693 | |
1694 | // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog | |
c030b70f JS |
1695 | // point to console as well |
1696 | ios::sync_with_stdio(); | |
1697 | ||
3f4a0c5b | 1698 | SetConsoleCtrlHandler(MyConsoleHandler, TRUE); |
c030b70f JS |
1699 | } |
1700 | #else | |
1701 | // Not supported | |
1702 | void wxRedirectIOToConsole() | |
1703 | { | |
1704 | } | |
1705 | #endif | |
1706 | ||
04ef50df JS |
1707 | #ifdef __WXMICROWIN__ |
1708 | int wxGetOsVersion(int *majorVsn, int *minorVsn) | |
1709 | { | |
1710 | // MICROWIN_TODO | |
1711 | if (majorVsn) *majorVsn = 0; | |
1712 | if (minorVsn) *minorVsn = 0; | |
1713 | return wxUNIX; | |
1714 | } | |
1715 | #endif | |
c030b70f | 1716 |