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