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