]> git.saurik.com Git - wxWidgets.git/blob - src/msw/utils.cpp
Must strip menu codes from labels before measuring (fixes bug 1400552)
[wxWidgets.git] / src / msw / utils.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/utils.h"
29 #include "wx/app.h"
30 #include "wx/intl.h"
31 #include "wx/log.h"
32 #endif //WX_PRECOMP
33
34 #include "wx/msw/registry.h"
35 #include "wx/apptrait.h"
36 #include "wx/dynlib.h"
37 #include "wx/dynload.h"
38 #include "wx/scopeguard.h"
39
40 #include "wx/confbase.h" // for wxExpandEnvVars()
41
42 #include "wx/msw/private.h" // includes <windows.h>
43 #include "wx/msw/missing.h" // CHARSET_HANGUL
44
45 #if defined(__CYGWIN__)
46 //CYGWIN gives annoying warning about runtime stuff if we don't do this
47 # define USE_SYS_TYPES_FD_SET
48 # include <sys/types.h>
49 #endif
50
51 // Doesn't work with Cygwin at present
52 #if wxUSE_SOCKETS && (defined(__GNUWIN32_OLD__) || defined(__WXWINCE__) || defined(__CYGWIN32__))
53 // apparently we need to include winsock.h to get WSADATA and other stuff
54 // used in wxGetFullHostName() with the old mingw32 versions
55 #include <winsock.h>
56 #endif
57
58 #include "wx/timer.h"
59
60 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
61 #include <direct.h>
62
63 #ifndef __MWERKS__
64 #include <dos.h>
65 #endif
66 #endif //GNUWIN32
67
68 #if defined(__CYGWIN__)
69 #include <sys/unistd.h>
70 #include <sys/stat.h>
71 #include <sys/cygwin.h> // for cygwin_conv_to_full_win32_path()
72 #endif //GNUWIN32
73
74 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
75 // this (3.1 I believe) and how to test for it.
76 // If this works for Borland 4.0 as well, then no worries.
77 #include <dir.h>
78 #endif
79
80 // VZ: there is some code using NetXXX() functions to get the full user name:
81 // I don't think it's a good idea because they don't work under Win95 and
82 // seem to return the same as wxGetUserId() under NT. If you really want
83 // to use them, just #define USE_NET_API
84 #undef USE_NET_API
85
86 #ifdef USE_NET_API
87 #include <lm.h>
88 #endif // USE_NET_API
89
90 #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
91 #ifndef __UNIX__
92 #include <io.h>
93 #endif
94
95 #ifndef __GNUWIN32__
96 #include <shellapi.h>
97 #endif
98 #endif
99
100 #ifndef __WATCOMC__
101 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
102 #include <errno.h>
103 #endif
104 #endif
105
106 // For wxKillAllChildren
107 #include <tlhelp32.h>
108
109 // ----------------------------------------------------------------------------
110 // constants
111 // ----------------------------------------------------------------------------
112
113 // In the WIN.INI file
114 #if (!defined(USE_NET_API) && !defined(__WXWINCE__)) || defined(__WXMICROWIN__)
115 static const wxChar WX_SECTION[] = wxT("wxWindows");
116 #endif
117
118 #if (!defined(USE_NET_API) && !defined(__WXWINCE__))
119 static const wxChar eUSERNAME[] = wxT("UserName");
120 #endif
121
122 // ============================================================================
123 // implementation
124 // ============================================================================
125
126 // ----------------------------------------------------------------------------
127 // get host name and related
128 // ----------------------------------------------------------------------------
129
130 // Get hostname only (without domain name)
131 bool wxGetHostName(wxChar *WXUNUSED_IN_WINCE(buf),
132 int WXUNUSED_IN_WINCE(maxSize))
133 {
134 #if defined(__WXWINCE__)
135 // TODO-CE
136 return false;
137 #elif defined(__WIN32__) && !defined(__WXMICROWIN__)
138 DWORD nSize = maxSize;
139 if ( !::GetComputerName(buf, &nSize) )
140 {
141 wxLogLastError(wxT("GetComputerName"));
142
143 return false;
144 }
145
146 return true;
147 #else
148 wxChar *sysname;
149 const wxChar *default_host = wxT("noname");
150
151 if ((sysname = wxGetenv(wxT("SYSTEM_NAME"))) == NULL) {
152 GetProfileString(WX_SECTION, eHOSTNAME, default_host, buf, maxSize - 1);
153 } else
154 wxStrncpy(buf, sysname, maxSize - 1);
155 buf[maxSize] = wxT('\0');
156 return *buf ? true : false;
157 #endif
158 }
159
160 // get full hostname (with domain name if possible)
161 bool wxGetFullHostName(wxChar *buf, int maxSize)
162 {
163 #if !defined( __WXMICROWIN__) && wxUSE_DYNAMIC_LOADER && wxUSE_SOCKETS
164 // TODO should use GetComputerNameEx() when available
165
166 // we don't want to always link with Winsock DLL as we might not use it at
167 // all, so load it dynamically here if needed (and don't complain if it is
168 // missing, we handle this)
169 wxLogNull noLog;
170
171 wxDynamicLibrary dllWinsock(_T("ws2_32.dll"), wxDL_VERBATIM);
172 if ( dllWinsock.IsLoaded() )
173 {
174 typedef int (PASCAL *WSAStartup_t)(WORD, WSADATA *);
175 typedef int (PASCAL *gethostname_t)(char *, int);
176 typedef hostent* (PASCAL *gethostbyname_t)(const char *);
177 typedef hostent* (PASCAL *gethostbyaddr_t)(const char *, int , int);
178 typedef int (PASCAL *WSACleanup_t)(void);
179
180 #define LOAD_WINSOCK_FUNC(func) \
181 func ## _t \
182 pfn ## func = (func ## _t)dllWinsock.GetSymbol(_T(#func))
183
184 LOAD_WINSOCK_FUNC(WSAStartup);
185
186 WSADATA wsa;
187 if ( pfnWSAStartup && pfnWSAStartup(MAKEWORD(1, 1), &wsa) == 0 )
188 {
189 LOAD_WINSOCK_FUNC(gethostname);
190
191 wxString host;
192 if ( pfngethostname )
193 {
194 char bufA[256];
195 if ( pfngethostname(bufA, WXSIZEOF(bufA)) == 0 )
196 {
197 // gethostname() won't usually include the DNS domain name,
198 // for this we need to work a bit more
199 if ( !strchr(bufA, '.') )
200 {
201 LOAD_WINSOCK_FUNC(gethostbyname);
202
203 struct hostent *pHostEnt = pfngethostbyname
204 ? pfngethostbyname(bufA)
205 : NULL;
206
207 if ( pHostEnt )
208 {
209 // Windows will use DNS internally now
210 LOAD_WINSOCK_FUNC(gethostbyaddr);
211
212 pHostEnt = pfngethostbyaddr
213 ? pfngethostbyaddr(pHostEnt->h_addr,
214 4, AF_INET)
215 : NULL;
216 }
217
218 if ( pHostEnt )
219 {
220 host = wxString::FromAscii(pHostEnt->h_name);
221 }
222 }
223 }
224 }
225
226 LOAD_WINSOCK_FUNC(WSACleanup);
227 if ( pfnWSACleanup )
228 pfnWSACleanup();
229
230
231 if ( !host.empty() )
232 {
233 wxStrncpy(buf, host, maxSize);
234
235 return true;
236 }
237 }
238 }
239 #endif // !__WXMICROWIN__
240
241 return wxGetHostName(buf, maxSize);
242 }
243
244 // Get user ID e.g. jacs
245 bool wxGetUserId(wxChar *WXUNUSED_IN_WINCE(buf),
246 int WXUNUSED_IN_WINCE(maxSize))
247 {
248 #if defined(__WXWINCE__)
249 // TODO-CE
250 return false;
251 #elif defined(__WIN32__) && !defined(__WXMICROWIN__)
252 DWORD nSize = maxSize;
253 if ( ::GetUserName(buf, &nSize) == 0 )
254 {
255 // actually, it does happen on Win9x if the user didn't log on
256 DWORD res = ::GetEnvironmentVariable(wxT("username"), buf, maxSize);
257 if ( res == 0 )
258 {
259 // not found
260 return false;
261 }
262 }
263
264 return true;
265 #else // __WXMICROWIN__
266 wxChar *user;
267 const wxChar *default_id = wxT("anonymous");
268
269 // Can't assume we have NIS (PC-NFS) or some other ID daemon
270 // So we ...
271 if ( (user = wxGetenv(wxT("USER"))) == NULL &&
272 (user = wxGetenv(wxT("LOGNAME"))) == NULL )
273 {
274 // Use wxWidgets configuration data (comming soon)
275 GetProfileString(WX_SECTION, eUSERID, default_id, buf, maxSize - 1);
276 }
277 else
278 {
279 wxStrncpy(buf, user, maxSize - 1);
280 }
281
282 return *buf ? true : false;
283 #endif
284 }
285
286 // Get user name e.g. Julian Smart
287 bool wxGetUserName(wxChar *buf, int maxSize)
288 {
289 wxCHECK_MSG( buf && ( maxSize > 0 ), false,
290 _T("empty buffer in wxGetUserName") );
291 #if defined(__WXWINCE__)
292 wxLogNull noLog;
293 wxRegKey key(wxRegKey::HKCU, wxT("ControlPanel\\Owner"));
294 if(!key.Open(wxRegKey::Read))
295 return false;
296 wxString name;
297 if(!key.QueryValue(wxT("Owner"),name))
298 return false;
299 wxStrncpy(buf, name.c_str(), maxSize-1);
300 buf[maxSize-1] = _T('\0');
301 return true;
302 #elif defined(USE_NET_API)
303 CHAR szUserName[256];
304 if ( !wxGetUserId(szUserName, WXSIZEOF(szUserName)) )
305 return false;
306
307 // TODO how to get the domain name?
308 CHAR *szDomain = "";
309
310 // the code is based on the MSDN example (also see KB article Q119670)
311 WCHAR wszUserName[256]; // Unicode user name
312 WCHAR wszDomain[256];
313 LPBYTE ComputerName;
314
315 USER_INFO_2 *ui2; // User structure
316
317 // Convert ANSI user name and domain to Unicode
318 MultiByteToWideChar( CP_ACP, 0, szUserName, strlen(szUserName)+1,
319 wszUserName, WXSIZEOF(wszUserName) );
320 MultiByteToWideChar( CP_ACP, 0, szDomain, strlen(szDomain)+1,
321 wszDomain, WXSIZEOF(wszDomain) );
322
323 // Get the computer name of a DC for the domain.
324 if ( NetGetDCName( NULL, wszDomain, &ComputerName ) != NERR_Success )
325 {
326 wxLogError(wxT("Can not find domain controller"));
327
328 goto error;
329 }
330
331 // Look up the user on the DC
332 NET_API_STATUS status = NetUserGetInfo( (LPWSTR)ComputerName,
333 (LPWSTR)&wszUserName,
334 2, // level - we want USER_INFO_2
335 (LPBYTE *) &ui2 );
336 switch ( status )
337 {
338 case NERR_Success:
339 // ok
340 break;
341
342 case NERR_InvalidComputer:
343 wxLogError(wxT("Invalid domain controller name."));
344
345 goto error;
346
347 case NERR_UserNotFound:
348 wxLogError(wxT("Invalid user name '%s'."), szUserName);
349
350 goto error;
351
352 default:
353 wxLogSysError(wxT("Can't get information about user"));
354
355 goto error;
356 }
357
358 // Convert the Unicode full name to ANSI
359 WideCharToMultiByte( CP_ACP, 0, ui2->usri2_full_name, -1,
360 buf, maxSize, NULL, NULL );
361
362 return true;
363
364 error:
365 wxLogError(wxT("Couldn't look up full user name."));
366
367 return false;
368 #else // !USE_NET_API
369 // Could use NIS, MS-Mail or other site specific programs
370 // Use wxWidgets configuration data
371 bool ok = GetProfileString(WX_SECTION, eUSERNAME, wxEmptyString, buf, maxSize - 1) != 0;
372 if ( !ok )
373 {
374 ok = wxGetUserId(buf, maxSize);
375 }
376
377 if ( !ok )
378 {
379 wxStrncpy(buf, wxT("Unknown User"), maxSize);
380 }
381
382 return true;
383 #endif // Win32/16
384 }
385
386 const wxChar* wxGetHomeDir(wxString *pstr)
387 {
388 wxString& strDir = *pstr;
389
390 // first branch is for Cygwin
391 #if defined(__UNIX__)
392 const wxChar *szHome = wxGetenv("HOME");
393 if ( szHome == NULL ) {
394 // we're homeless...
395 wxLogWarning(_("can't find user's HOME, using current directory."));
396 strDir = wxT(".");
397 }
398 else
399 strDir = szHome;
400
401 // add a trailing slash if needed
402 if ( strDir.Last() != wxT('/') )
403 strDir << wxT('/');
404
405 #ifdef __CYGWIN__
406 // Cygwin returns unix type path but that does not work well
407 static wxChar windowsPath[MAX_PATH];
408 cygwin_conv_to_full_win32_path(strDir, windowsPath);
409 strDir = windowsPath;
410 #endif
411 #elif defined(__WXWINCE__)
412 strDir = wxT("\\");
413 #else
414 strDir.clear();
415
416 // If we have a valid HOME directory, as is used on many machines that
417 // have unix utilities on them, we should use that.
418 const wxChar *szHome = wxGetenv(wxT("HOME"));
419
420 if ( szHome != NULL )
421 {
422 strDir = szHome;
423 }
424 else // no HOME, try HOMEDRIVE/PATH
425 {
426 szHome = wxGetenv(wxT("HOMEDRIVE"));
427 if ( szHome != NULL )
428 strDir << szHome;
429 szHome = wxGetenv(wxT("HOMEPATH"));
430
431 if ( szHome != NULL )
432 {
433 strDir << szHome;
434
435 // the idea is that under NT these variables have default values
436 // of "%systemdrive%:" and "\\". As we don't want to create our
437 // config files in the root directory of the system drive, we will
438 // create it in our program's dir. However, if the user took care
439 // to set HOMEPATH to something other than "\\", we suppose that he
440 // knows what he is doing and use the supplied value.
441 if ( wxStrcmp(szHome, wxT("\\")) == 0 )
442 strDir.clear();
443 }
444 }
445
446 if ( strDir.empty() )
447 {
448 // If we have a valid USERPROFILE directory, as is the case in
449 // Windows NT, 2000 and XP, we should use that as our home directory.
450 szHome = wxGetenv(wxT("USERPROFILE"));
451
452 if ( szHome != NULL )
453 strDir = szHome;
454 }
455
456 if ( !strDir.empty() )
457 {
458 // sometimes the value of HOME may be "%USERPROFILE%", so reexpand the
459 // value once again, it shouldn't hurt anyhow
460 strDir = wxExpandEnvVars(strDir);
461 }
462 else // fall back to the program directory
463 {
464 // extract the directory component of the program file name
465 wxSplitPath(wxGetFullModuleName(), &strDir, NULL, NULL);
466 }
467 #endif // UNIX/Win
468
469 return strDir.c_str();
470 }
471
472 wxChar *wxGetUserHome(const wxString& WXUNUSED(user))
473 {
474 // VZ: the old code here never worked for user != "" anyhow! Moreover, it
475 // returned sometimes a malloc()'d pointer, sometimes a pointer to a
476 // static buffer and sometimes I don't even know what.
477 static wxString s_home;
478
479 return (wxChar *)wxGetHomeDir(&s_home);
480 }
481
482 bool wxGetDiskSpace(const wxString& WXUNUSED_IN_WINCE(path),
483 wxLongLong *WXUNUSED_IN_WINCE(pTotal),
484 wxLongLong *WXUNUSED_IN_WINCE(pFree))
485 {
486 #ifdef __WXWINCE__
487 // TODO-CE
488 return false;
489 #else
490 if ( path.empty() )
491 return false;
492
493 // old w32api don't have ULARGE_INTEGER
494 #if defined(__WIN32__) && \
495 (!defined(__GNUWIN32__) || wxCHECK_W32API_VERSION( 0, 3 ))
496 // GetDiskFreeSpaceEx() is not available under original Win95, check for
497 // it
498 typedef BOOL (WINAPI *GetDiskFreeSpaceEx_t)(LPCTSTR,
499 PULARGE_INTEGER,
500 PULARGE_INTEGER,
501 PULARGE_INTEGER);
502
503 GetDiskFreeSpaceEx_t
504 pGetDiskFreeSpaceEx = (GetDiskFreeSpaceEx_t)::GetProcAddress
505 (
506 ::GetModuleHandle(_T("kernel32.dll")),
507 #if wxUSE_UNICODE
508 "GetDiskFreeSpaceExW"
509 #else
510 "GetDiskFreeSpaceExA"
511 #endif
512 );
513
514 if ( pGetDiskFreeSpaceEx )
515 {
516 ULARGE_INTEGER bytesFree, bytesTotal;
517
518 // may pass the path as is, GetDiskFreeSpaceEx() is smart enough
519 if ( !pGetDiskFreeSpaceEx(path,
520 &bytesFree,
521 &bytesTotal,
522 NULL) )
523 {
524 wxLogLastError(_T("GetDiskFreeSpaceEx"));
525
526 return false;
527 }
528
529 // ULARGE_INTEGER is a union of a 64 bit value and a struct containing
530 // two 32 bit fields which may be or may be not named - try to make it
531 // compile in all cases
532 #if defined(__BORLANDC__) && !defined(_ANONYMOUS_STRUCT)
533 #define UL(ul) ul.u
534 #else // anon union
535 #define UL(ul) ul
536 #endif
537 if ( pTotal )
538 {
539 *pTotal = wxLongLong(UL(bytesTotal).HighPart, UL(bytesTotal).LowPart);
540 }
541
542 if ( pFree )
543 {
544 *pFree = wxLongLong(UL(bytesFree).HighPart, UL(bytesFree).LowPart);
545 }
546 }
547 else
548 #endif // Win32
549 {
550 // there's a problem with drives larger than 2GB, GetDiskFreeSpaceEx()
551 // should be used instead - but if it's not available, fall back on
552 // GetDiskFreeSpace() nevertheless...
553
554 DWORD lSectorsPerCluster,
555 lBytesPerSector,
556 lNumberOfFreeClusters,
557 lTotalNumberOfClusters;
558
559 // FIXME: this is wrong, we should extract the root drive from path
560 // instead, but this is the job for wxFileName...
561 if ( !::GetDiskFreeSpace(path,
562 &lSectorsPerCluster,
563 &lBytesPerSector,
564 &lNumberOfFreeClusters,
565 &lTotalNumberOfClusters) )
566 {
567 wxLogLastError(_T("GetDiskFreeSpace"));
568
569 return false;
570 }
571
572 wxLongLong lBytesPerCluster = lSectorsPerCluster;
573 lBytesPerCluster *= lBytesPerSector;
574
575 if ( pTotal )
576 {
577 *pTotal = lBytesPerCluster;
578 *pTotal *= lTotalNumberOfClusters;
579 }
580
581 if ( pFree )
582 {
583 *pFree = lBytesPerCluster;
584 *pFree *= lNumberOfFreeClusters;
585 }
586 }
587
588 return true;
589 #endif
590 // __WXWINCE__
591 }
592
593 // ----------------------------------------------------------------------------
594 // env vars
595 // ----------------------------------------------------------------------------
596
597 bool wxGetEnv(const wxString& WXUNUSED_IN_WINCE(var),
598 wxString *WXUNUSED_IN_WINCE(value))
599 {
600 #ifdef __WXWINCE__
601 // no environment variables under CE
602 return false;
603 #else // Win32
604 // first get the size of the buffer
605 DWORD dwRet = ::GetEnvironmentVariable(var, NULL, 0);
606 if ( !dwRet )
607 {
608 // this means that there is no such variable
609 return false;
610 }
611
612 if ( value )
613 {
614 (void)::GetEnvironmentVariable(var, wxStringBuffer(*value, dwRet),
615 dwRet);
616 }
617
618 return true;
619 #endif // WinCE/32
620 }
621
622 bool wxSetEnv(const wxString& WXUNUSED_IN_WINCE(var),
623 const wxChar *WXUNUSED_IN_WINCE(value))
624 {
625 // some compilers have putenv() or _putenv() or _wputenv() but it's better
626 // to always use Win32 function directly instead of dealing with them
627 #ifdef __WXWINCE__
628 // no environment variables under CE
629 return false;
630 #else
631 if ( !::SetEnvironmentVariable(var, value) )
632 {
633 wxLogLastError(_T("SetEnvironmentVariable"));
634
635 return false;
636 }
637
638 return true;
639 #endif
640 }
641
642 // ----------------------------------------------------------------------------
643 // process management
644 // ----------------------------------------------------------------------------
645
646 // structure used to pass parameters from wxKill() to wxEnumFindByPidProc()
647 struct wxFindByPidParams
648 {
649 wxFindByPidParams() { hwnd = 0; pid = 0; }
650
651 // the HWND used to return the result
652 HWND hwnd;
653
654 // the PID we're looking from
655 DWORD pid;
656
657 DECLARE_NO_COPY_CLASS(wxFindByPidParams)
658 };
659
660 // wxKill helper: EnumWindows() callback which is used to find the first (top
661 // level) window belonging to the given process
662 BOOL CALLBACK wxEnumFindByPidProc(HWND hwnd, LPARAM lParam)
663 {
664 DWORD pid;
665 (void)::GetWindowThreadProcessId(hwnd, &pid);
666
667 wxFindByPidParams *params = (wxFindByPidParams *)lParam;
668 if ( pid == params->pid )
669 {
670 // remember the window we found
671 params->hwnd = hwnd;
672
673 // return FALSE to stop the enumeration
674 return FALSE;
675 }
676
677 // continue enumeration
678 return TRUE;
679 }
680
681 int wxKillAllChildren(long pid, wxSignal sig, wxKillError *krc);
682
683 int wxKill(long pid, wxSignal sig, wxKillError *krc, int flags)
684 {
685 if (flags & wxKILL_CHILDREN)
686 wxKillAllChildren(pid, sig, krc);
687
688 // get the process handle to operate on
689 HANDLE hProcess = ::OpenProcess(SYNCHRONIZE |
690 PROCESS_TERMINATE |
691 PROCESS_QUERY_INFORMATION,
692 FALSE, // not inheritable
693 (DWORD)pid);
694 if ( hProcess == NULL )
695 {
696 if ( krc )
697 {
698 // recognize wxKILL_ACCESS_DENIED as special because this doesn't
699 // mean that the process doesn't exist and this is important for
700 // wxProcess::Exists()
701 *krc = ::GetLastError() == ERROR_ACCESS_DENIED
702 ? wxKILL_ACCESS_DENIED
703 : wxKILL_NO_PROCESS;
704 }
705
706 return -1;
707 }
708
709 wxON_BLOCK_EXIT1(::CloseHandle, hProcess);
710
711 bool ok = true;
712 switch ( sig )
713 {
714 case wxSIGKILL:
715 // kill the process forcefully returning -1 as error code
716 if ( !::TerminateProcess(hProcess, (UINT)-1) )
717 {
718 wxLogSysError(_("Failed to kill process %d"), pid);
719
720 if ( krc )
721 {
722 // this is not supposed to happen if we could open the
723 // process
724 *krc = wxKILL_ERROR;
725 }
726
727 ok = false;
728 }
729 break;
730
731 case wxSIGNONE:
732 // do nothing, we just want to test for process existence
733 if ( krc )
734 *krc = wxKILL_OK;
735 return 0;
736
737 default:
738 // any other signal means "terminate"
739 {
740 wxFindByPidParams params;
741 params.pid = (DWORD)pid;
742
743 // EnumWindows() has nice semantics: it returns 0 if it found
744 // something or if an error occurred and non zero if it
745 // enumerated all the window
746 if ( !::EnumWindows(wxEnumFindByPidProc, (LPARAM)&params) )
747 {
748 // did we find any window?
749 if ( params.hwnd )
750 {
751 // tell the app to close
752 //
753 // NB: this is the harshest way, the app won't have an
754 // opportunity to save any files, for example, but
755 // this is probably what we want here. If not we
756 // can also use SendMesageTimeout(WM_CLOSE)
757 if ( !::PostMessage(params.hwnd, WM_QUIT, 0, 0) )
758 {
759 wxLogLastError(_T("PostMessage(WM_QUIT)"));
760 }
761 }
762 else // it was an error then
763 {
764 wxLogLastError(_T("EnumWindows"));
765
766 ok = false;
767 }
768 }
769 else // no windows for this PID
770 {
771 if ( krc )
772 *krc = wxKILL_ERROR;
773
774 ok = false;
775 }
776 }
777 }
778
779 // the return code
780 DWORD rc wxDUMMY_INITIALIZE(0);
781 if ( ok )
782 {
783 // as we wait for a short time, we can use just WaitForSingleObject()
784 // and not MsgWaitForMultipleObjects()
785 switch ( ::WaitForSingleObject(hProcess, 500 /* msec */) )
786 {
787 case WAIT_OBJECT_0:
788 // process terminated
789 if ( !::GetExitCodeProcess(hProcess, &rc) )
790 {
791 wxLogLastError(_T("GetExitCodeProcess"));
792 }
793 break;
794
795 default:
796 wxFAIL_MSG( _T("unexpected WaitForSingleObject() return") );
797 // fall through
798
799 case WAIT_FAILED:
800 wxLogLastError(_T("WaitForSingleObject"));
801 // fall through
802
803 case WAIT_TIMEOUT:
804 if ( krc )
805 *krc = wxKILL_ERROR;
806
807 rc = STILL_ACTIVE;
808 break;
809 }
810 }
811
812
813 // the return code is the same as from Unix kill(): 0 if killed
814 // successfully or -1 on error
815 if ( !ok || rc == STILL_ACTIVE )
816 return -1;
817
818 if ( krc )
819 *krc = wxKILL_OK;
820
821 return 0;
822 }
823
824 typedef HANDLE (WINAPI *CreateToolhelp32Snapshot_t)(DWORD,DWORD);
825 typedef BOOL (WINAPI *Process32_t)(HANDLE,LPPROCESSENTRY32);
826
827 CreateToolhelp32Snapshot_t lpfCreateToolhelp32Snapshot;
828 Process32_t lpfProcess32First, lpfProcess32Next;
829
830 static void InitToolHelp32()
831 {
832 static bool s_initToolHelpDone = false;
833
834 if (s_initToolHelpDone)
835 return;
836
837 s_initToolHelpDone = true;
838
839 lpfCreateToolhelp32Snapshot = NULL;
840 lpfProcess32First = NULL;
841 lpfProcess32Next = NULL;
842
843 #if wxUSE_DYNLIB_CLASS
844
845 wxDynamicLibrary dllKernel(_T("kernel32.dll"), wxDL_VERBATIM);
846
847 // Get procedure addresses.
848 // We are linking to these functions of Kernel32
849 // explicitly, because otherwise a module using
850 // this code would fail to load under Windows NT,
851 // which does not have the Toolhelp32
852 // functions in the Kernel 32.
853 lpfCreateToolhelp32Snapshot =
854 (CreateToolhelp32Snapshot_t)dllKernel.RawGetSymbol(_T("CreateToolhelp32Snapshot"));
855
856 lpfProcess32First =
857 (Process32_t)dllKernel.RawGetSymbol(_T("Process32First"));
858
859 lpfProcess32Next =
860 (Process32_t)dllKernel.RawGetSymbol(_T("Process32Next"));
861
862 #endif // wxUSE_DYNLIB_CLASS
863 }
864
865 // By John Skiff
866 int wxKillAllChildren(long pid, wxSignal sig, wxKillError *krc)
867 {
868 InitToolHelp32();
869
870 if (krc)
871 *krc = wxKILL_OK;
872
873 // If not implemented for this platform (e.g. NT 4.0), silently ignore
874 if (!lpfCreateToolhelp32Snapshot || !lpfProcess32First || !lpfProcess32Next)
875 return 0;
876
877 // Take a snapshot of all processes in the system.
878 HANDLE hProcessSnap = lpfCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
879 if (hProcessSnap == INVALID_HANDLE_VALUE) {
880 if (krc)
881 *krc = wxKILL_ERROR;
882 return -1;
883 }
884
885 //Fill in the size of the structure before using it.
886 PROCESSENTRY32 pe;
887 wxZeroMemory(pe);
888 pe.dwSize = sizeof(PROCESSENTRY32);
889
890 // Walk the snapshot of the processes, and for each process,
891 // kill it if its parent is pid.
892 if (!lpfProcess32First(hProcessSnap, &pe)) {
893 // Can't get first process.
894 if (krc)
895 *krc = wxKILL_ERROR;
896 CloseHandle (hProcessSnap);
897 return -1;
898 }
899
900 do {
901 if (pe.th32ParentProcessID == (DWORD) pid) {
902 if (wxKill(pe.th32ProcessID, sig, krc))
903 return -1;
904 }
905 } while (lpfProcess32Next (hProcessSnap, &pe));
906
907
908 return 0;
909 }
910
911 // Execute a program in an Interactive Shell
912 bool wxShell(const wxString& command)
913 {
914 wxString cmd;
915
916 #ifdef __WXWINCE__
917 cmd = command;
918 #else
919 wxChar *shell = wxGetenv(wxT("COMSPEC"));
920 if ( !shell )
921 shell = (wxChar*) wxT("\\COMMAND.COM");
922
923 if ( !command )
924 {
925 // just the shell
926 cmd = shell;
927 }
928 else
929 {
930 // pass the command to execute to the command processor
931 cmd.Printf(wxT("%s /c %s"), shell, command.c_str());
932 }
933 #endif
934
935 return wxExecute(cmd, wxEXEC_SYNC) == 0;
936 }
937
938 // Shutdown or reboot the PC
939 bool wxShutdown(wxShutdownFlags WXUNUSED_IN_WINCE(wFlags))
940 {
941 #ifdef __WXWINCE__
942 // TODO-CE
943 return false;
944 #elif defined(__WIN32__)
945 bool bOK = true;
946
947 if ( wxGetOsVersion(NULL, NULL) == wxWINDOWS_NT ) // if is NT or 2K
948 {
949 // Get a token for this process.
950 HANDLE hToken;
951 bOK = ::OpenProcessToken(GetCurrentProcess(),
952 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
953 &hToken) != 0;
954 if ( bOK )
955 {
956 TOKEN_PRIVILEGES tkp;
957
958 // Get the LUID for the shutdown privilege.
959 ::LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
960 &tkp.Privileges[0].Luid);
961
962 tkp.PrivilegeCount = 1; // one privilege to set
963 tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
964
965 // Get the shutdown privilege for this process.
966 ::AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
967 (PTOKEN_PRIVILEGES)NULL, 0);
968
969 // Cannot test the return value of AdjustTokenPrivileges.
970 bOK = ::GetLastError() == ERROR_SUCCESS;
971 }
972 }
973
974 if ( bOK )
975 {
976 UINT flags = EWX_SHUTDOWN | EWX_FORCE;
977 switch ( wFlags )
978 {
979 case wxSHUTDOWN_POWEROFF:
980 flags |= EWX_POWEROFF;
981 break;
982
983 case wxSHUTDOWN_REBOOT:
984 flags |= EWX_REBOOT;
985 break;
986
987 default:
988 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
989 return false;
990 }
991
992 bOK = ::ExitWindowsEx(flags, 0) != 0;
993 }
994
995 return bOK;
996 #endif // Win32/16
997 }
998
999 wxPowerType wxGetPowerType()
1000 {
1001 // TODO
1002 return wxPOWER_UNKNOWN;
1003 }
1004
1005 wxBatteryState wxGetBatteryState()
1006 {
1007 // TODO
1008 return wxBATTERY_UNKNOWN_STATE;
1009 }
1010
1011 // ----------------------------------------------------------------------------
1012 // misc
1013 // ----------------------------------------------------------------------------
1014
1015 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
1016 wxMemorySize wxGetFreeMemory()
1017 {
1018 #if defined(__WIN64__)
1019 MEMORYSTATUSEX memStatex;
1020 memStatex.dwLength = sizeof (memStatex);
1021 ::GlobalMemoryStatusEx (&memStatex);
1022 return (wxMemorySize)memStatex.ullAvailPhys;
1023 #else /* if defined(__WIN32__) */
1024 MEMORYSTATUS memStatus;
1025 memStatus.dwLength = sizeof(MEMORYSTATUS);
1026 ::GlobalMemoryStatus(&memStatus);
1027 return (wxMemorySize)memStatus.dwAvailPhys;
1028 #endif
1029 }
1030
1031 unsigned long wxGetProcessId()
1032 {
1033 return ::GetCurrentProcessId();
1034 }
1035
1036 // Emit a beeeeeep
1037 void wxBell()
1038 {
1039 ::MessageBeep((UINT)-1); // default sound
1040 }
1041
1042 bool wxIsDebuggerRunning()
1043 {
1044 #if wxUSE_DYNLIB_CLASS
1045 // IsDebuggerPresent() is not available under Win95, so load it dynamically
1046 wxDynamicLibrary dll(_T("kernel32.dll"), wxDL_VERBATIM);
1047
1048 typedef BOOL (WINAPI *IsDebuggerPresent_t)();
1049 if ( !dll.HasSymbol(_T("IsDebuggerPresent")) )
1050 {
1051 // no way to know, assume no
1052 return false;
1053 }
1054
1055 return (*(IsDebuggerPresent_t)dll.GetSymbol(_T("IsDebuggerPresent")))() != 0;
1056 #else
1057 return false;
1058 #endif
1059 }
1060
1061 // ----------------------------------------------------------------------------
1062 // OS version
1063 // ----------------------------------------------------------------------------
1064
1065 wxString wxGetOsDescription()
1066 {
1067 wxString str;
1068
1069 OSVERSIONINFO info;
1070 wxZeroMemory(info);
1071
1072 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1073 if ( ::GetVersionEx(&info) )
1074 {
1075 switch ( info.dwPlatformId )
1076 {
1077 case VER_PLATFORM_WIN32s:
1078 str = _("Win32s on Windows 3.1");
1079 break;
1080
1081 case VER_PLATFORM_WIN32_WINDOWS:
1082 switch (info.dwMinorVersion)
1083 {
1084 case 0:
1085 if ( info.szCSDVersion[1] == 'B' ||
1086 info.szCSDVersion[1] == 'C' )
1087 {
1088 str = _("Windows 95 OSR2");
1089 }
1090 else
1091 {
1092 str = _("Windows 95");
1093 }
1094 break;
1095 case 10:
1096 if ( info.szCSDVersion[1] == 'B' ||
1097 info.szCSDVersion[1] == 'C' )
1098 {
1099 str = _("Windows 98 SE");
1100 }
1101 else
1102 {
1103 str = _("Windows 98");
1104 }
1105 break;
1106 case 90:
1107 str = _("Windows ME");
1108 break;
1109 default:
1110 str.Printf(_("Windows 9x (%d.%d)"),
1111 info.dwMajorVersion,
1112 info.dwMinorVersion);
1113 break;
1114 }
1115 if ( !wxIsEmpty(info.szCSDVersion) )
1116 {
1117 str << _T(" (") << info.szCSDVersion << _T(')');
1118 }
1119 break;
1120
1121 case VER_PLATFORM_WIN32_NT:
1122 if ( info.dwMajorVersion == 5 )
1123 {
1124 switch ( info.dwMinorVersion )
1125 {
1126 case 0:
1127 str.Printf(_("Windows 2000 (build %lu"),
1128 info.dwBuildNumber);
1129 break;
1130 case 1:
1131 str.Printf(_("Windows XP (build %lu"),
1132 info.dwBuildNumber);
1133 break;
1134 case 2:
1135 str.Printf(_("Windows Server 2003 (build %lu"),
1136 info.dwBuildNumber);
1137 break;
1138 }
1139 }
1140 if ( wxIsEmpty(str) )
1141 {
1142 str.Printf(_("Windows NT %lu.%lu (build %lu"),
1143 info.dwMajorVersion,
1144 info.dwMinorVersion,
1145 info.dwBuildNumber);
1146 }
1147 if ( !wxIsEmpty(info.szCSDVersion) )
1148 {
1149 str << _T(", ") << info.szCSDVersion;
1150 }
1151 str << _T(')');
1152 break;
1153 }
1154 }
1155 else
1156 {
1157 wxFAIL_MSG( _T("GetVersionEx() failed") ); // should never happen
1158 }
1159
1160 return str;
1161 }
1162
1163 wxToolkitInfo& wxAppTraits::GetToolkitInfo()
1164 {
1165 // cache the version info, it's not going to change
1166 //
1167 // NB: this is MT-safe, we may use these static vars from different threads
1168 // but as they always have the same value it doesn't matter
1169 static int s_ver = -1,
1170 s_major = -1,
1171 s_minor = -1;
1172
1173 if ( s_ver == -1 )
1174 {
1175 OSVERSIONINFO info;
1176 wxZeroMemory(info);
1177
1178 s_ver = wxWINDOWS;
1179 info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1180 if ( ::GetVersionEx(&info) )
1181 {
1182 s_major = info.dwMajorVersion;
1183 s_minor = info.dwMinorVersion;
1184
1185 #ifdef __SMARTPHONE__
1186 s_ver = wxWINDOWS_SMARTPHONE;
1187 #elif defined(__POCKETPC__)
1188 s_ver = wxWINDOWS_POCKETPC;
1189 #else
1190 switch ( info.dwPlatformId )
1191 {
1192 case VER_PLATFORM_WIN32s:
1193 s_ver = wxWIN32S;
1194 break;
1195
1196 case VER_PLATFORM_WIN32_WINDOWS:
1197 s_ver = wxWIN95;
1198 break;
1199
1200 case VER_PLATFORM_WIN32_NT:
1201 s_ver = wxWINDOWS_NT;
1202 break;
1203 #ifdef __WXWINCE__
1204 case VER_PLATFORM_WIN32_CE:
1205 s_ver = wxWINDOWS_CE;
1206 #endif
1207 }
1208 #endif
1209 }
1210 }
1211
1212 static wxToolkitInfo info;
1213 info.versionMajor = s_major;
1214 info.versionMinor = s_minor;
1215 info.os = s_ver;
1216 info.name = _T("wxBase");
1217 return info;
1218 }
1219
1220 wxWinVersion wxGetWinVersion()
1221 {
1222 int verMaj,
1223 verMin;
1224 switch ( wxGetOsVersion(&verMaj, &verMin) )
1225 {
1226 case wxWIN95:
1227 if ( verMaj == 4 )
1228 {
1229 switch ( verMin )
1230 {
1231 case 0:
1232 return wxWinVersion_95;
1233
1234 case 10:
1235 return wxWinVersion_98;
1236
1237 case 90:
1238 return wxWinVersion_ME;
1239 }
1240 }
1241 break;
1242
1243 case wxWINDOWS_NT:
1244 switch ( verMaj )
1245 {
1246 case 3:
1247 return wxWinVersion_NT3;
1248
1249 case 4:
1250 return wxWinVersion_NT4;
1251
1252 case 5:
1253 switch ( verMin )
1254 {
1255 case 0:
1256 return wxWinVersion_2000;
1257
1258 case 1:
1259 return wxWinVersion_XP;
1260
1261 case 2:
1262 return wxWinVersion_2003;
1263 }
1264 break;
1265
1266 case 6:
1267 return wxWinVersion_NT6;
1268 }
1269 break;
1270
1271 }
1272
1273 return wxWinVersion_Unknown;
1274 }
1275
1276 // ----------------------------------------------------------------------------
1277 // sleep functions
1278 // ----------------------------------------------------------------------------
1279
1280 void wxMilliSleep(unsigned long milliseconds)
1281 {
1282 ::Sleep(milliseconds);
1283 }
1284
1285 void wxMicroSleep(unsigned long microseconds)
1286 {
1287 wxMilliSleep(microseconds/1000);
1288 }
1289
1290 void wxSleep(int nSecs)
1291 {
1292 wxMilliSleep(1000*nSecs);
1293 }
1294
1295 // ----------------------------------------------------------------------------
1296 // font encoding <-> Win32 codepage conversion functions
1297 // ----------------------------------------------------------------------------
1298
1299 extern WXDLLIMPEXP_BASE long wxEncodingToCharset(wxFontEncoding encoding)
1300 {
1301 switch ( encoding )
1302 {
1303 // although this function is supposed to return an exact match, do do
1304 // some mappings here for the most common case of "standard" encoding
1305 case wxFONTENCODING_SYSTEM:
1306 return DEFAULT_CHARSET;
1307
1308 case wxFONTENCODING_ISO8859_1:
1309 case wxFONTENCODING_ISO8859_15:
1310 case wxFONTENCODING_CP1252:
1311 return ANSI_CHARSET;
1312
1313 #if !defined(__WXMICROWIN__)
1314 // The following four fonts are multi-byte charsets
1315 case wxFONTENCODING_CP932:
1316 return SHIFTJIS_CHARSET;
1317
1318 case wxFONTENCODING_CP936:
1319 return GB2312_CHARSET;
1320
1321 #ifndef __WXWINCE__
1322 case wxFONTENCODING_CP949:
1323 return HANGUL_CHARSET;
1324 #endif
1325
1326 case wxFONTENCODING_CP950:
1327 return CHINESEBIG5_CHARSET;
1328
1329 // The rest are single byte encodings
1330 case wxFONTENCODING_CP1250:
1331 return EASTEUROPE_CHARSET;
1332
1333 case wxFONTENCODING_CP1251:
1334 return RUSSIAN_CHARSET;
1335
1336 case wxFONTENCODING_CP1253:
1337 return GREEK_CHARSET;
1338
1339 case wxFONTENCODING_CP1254:
1340 return TURKISH_CHARSET;
1341
1342 case wxFONTENCODING_CP1255:
1343 return HEBREW_CHARSET;
1344
1345 case wxFONTENCODING_CP1256:
1346 return ARABIC_CHARSET;
1347
1348 case wxFONTENCODING_CP1257:
1349 return BALTIC_CHARSET;
1350
1351 case wxFONTENCODING_CP874:
1352 return THAI_CHARSET;
1353 #endif // !__WXMICROWIN__
1354
1355 case wxFONTENCODING_CP437:
1356 return OEM_CHARSET;
1357
1358 default:
1359 // no way to translate this encoding into a Windows charset
1360 return -1;
1361 }
1362 }
1363
1364 // we have 2 versions of wxCharsetToCodepage(): the old one which directly
1365 // looks up the vlaues in the registry and the new one which is more
1366 // politically correct and has more chances to work on other Windows versions
1367 // as well but the old version is still needed for !wxUSE_FONTMAP case
1368 #if wxUSE_FONTMAP
1369
1370 #include "wx/fontmap.h"
1371
1372 extern WXDLLIMPEXP_BASE long wxEncodingToCodepage(wxFontEncoding encoding)
1373 {
1374 // There don't seem to be symbolic names for
1375 // these under Windows so I just copied the
1376 // values from MSDN.
1377
1378 unsigned int ret;
1379
1380 switch (encoding)
1381 {
1382 case wxFONTENCODING_ISO8859_1: ret = 28591; break;
1383 case wxFONTENCODING_ISO8859_2: ret = 28592; break;
1384 case wxFONTENCODING_ISO8859_3: ret = 28593; break;
1385 case wxFONTENCODING_ISO8859_4: ret = 28594; break;
1386 case wxFONTENCODING_ISO8859_5: ret = 28595; break;
1387 case wxFONTENCODING_ISO8859_6: ret = 28596; break;
1388 case wxFONTENCODING_ISO8859_7: ret = 28597; break;
1389 case wxFONTENCODING_ISO8859_8: ret = 28598; break;
1390 case wxFONTENCODING_ISO8859_9: ret = 28599; break;
1391 case wxFONTENCODING_ISO8859_10: ret = 28600; break;
1392 case wxFONTENCODING_ISO8859_11: ret = 28601; break;
1393 // case wxFONTENCODING_ISO8859_12, // doesn't exist currently, but put it
1394 case wxFONTENCODING_ISO8859_13: ret = 28603; break;
1395 case wxFONTENCODING_ISO8859_14: ret = 28604; break;
1396 case wxFONTENCODING_ISO8859_15: ret = 28605; break;
1397 case wxFONTENCODING_KOI8: ret = 20866; break;
1398 case wxFONTENCODING_KOI8_U: ret = 21866; break;
1399 case wxFONTENCODING_CP437: ret = 437; break;
1400 case wxFONTENCODING_CP850: ret = 850; break;
1401 case wxFONTENCODING_CP852: ret = 852; break;
1402 case wxFONTENCODING_CP855: ret = 855; break;
1403 case wxFONTENCODING_CP866: ret = 866; break;
1404 case wxFONTENCODING_CP874: ret = 874; break;
1405 case wxFONTENCODING_CP932: ret = 932; break;
1406 case wxFONTENCODING_CP936: ret = 936; break;
1407 case wxFONTENCODING_CP949: ret = 949; break;
1408 case wxFONTENCODING_CP950: ret = 950; break;
1409 case wxFONTENCODING_CP1250: ret = 1250; break;
1410 case wxFONTENCODING_CP1251: ret = 1251; break;
1411 case wxFONTENCODING_CP1252: ret = 1252; break;
1412 case wxFONTENCODING_CP1253: ret = 1253; break;
1413 case wxFONTENCODING_CP1254: ret = 1254; break;
1414 case wxFONTENCODING_CP1255: ret = 1255; break;
1415 case wxFONTENCODING_CP1256: ret = 1256; break;
1416 case wxFONTENCODING_CP1257: ret = 1257; break;
1417 case wxFONTENCODING_EUC_JP: ret = 20932; break;
1418 case wxFONTENCODING_MACROMAN: ret = 10000; break;
1419 case wxFONTENCODING_MACJAPANESE: ret = 10001; break;
1420 case wxFONTENCODING_MACCHINESETRAD: ret = 10002; break;
1421 case wxFONTENCODING_MACKOREAN: ret = 10003; break;
1422 case wxFONTENCODING_MACARABIC: ret = 10004; break;
1423 case wxFONTENCODING_MACHEBREW: ret = 10005; break;
1424 case wxFONTENCODING_MACGREEK: ret = 10006; break;
1425 case wxFONTENCODING_MACCYRILLIC: ret = 10007; break;
1426 case wxFONTENCODING_MACTHAI: ret = 10021; break;
1427 case wxFONTENCODING_MACCHINESESIMP: ret = 10008; break;
1428 case wxFONTENCODING_MACCENTRALEUR: ret = 10029; break;
1429 case wxFONTENCODING_MACCROATIAN: ret = 10082; break;
1430 case wxFONTENCODING_MACICELANDIC: ret = 10079; break;
1431 case wxFONTENCODING_MACROMANIAN: ret = 10009; break;
1432 case wxFONTENCODING_UTF7: ret = 65000; break;
1433 case wxFONTENCODING_UTF8: ret = 65001; break;
1434 default: return -1;
1435 }
1436
1437 if (::IsValidCodePage(ret) == 0)
1438 return -1;
1439
1440 CPINFO info;
1441 if (::GetCPInfo(ret, &info) == 0)
1442 return -1;
1443
1444 return (long) ret;
1445 }
1446
1447 extern long wxCharsetToCodepage(const wxChar *name)
1448 {
1449 // first get the font encoding for this charset
1450 if ( !name )
1451 return -1;
1452
1453 wxFontEncoding enc = wxFontMapperBase::Get()->CharsetToEncoding(name, false);
1454 if ( enc == wxFONTENCODING_SYSTEM )
1455 return -1;
1456
1457 // the use the helper function
1458 return wxEncodingToCodepage(enc);
1459 }
1460
1461 #else // !wxUSE_FONTMAP
1462
1463 #include "wx/msw/registry.h"
1464
1465 // this should work if Internet Exploiter is installed
1466 extern long wxCharsetToCodepage(const wxChar *name)
1467 {
1468 if (!name)
1469 return GetACP();
1470
1471 long CP = -1;
1472
1473 wxString path(wxT("MIME\\Database\\Charset\\"));
1474 wxString cn(name);
1475
1476 // follow the alias loop
1477 for ( ;; )
1478 {
1479 wxRegKey key(wxRegKey::HKCR, path + cn);
1480
1481 if (!key.Exists())
1482 break;
1483
1484 // two cases: either there's an AliasForCharset string,
1485 // or there are Codepage and InternetEncoding dwords.
1486 // The InternetEncoding gives us the actual encoding,
1487 // the Codepage just says which Windows character set to
1488 // use when displaying the data.
1489 if (key.HasValue(wxT("InternetEncoding")) &&
1490 key.QueryValue(wxT("InternetEncoding"), &CP))
1491 break;
1492
1493 // no encoding, see if it's an alias
1494 if (!key.HasValue(wxT("AliasForCharset")) ||
1495 !key.QueryValue(wxT("AliasForCharset"), cn))
1496 break;
1497 }
1498
1499 return CP;
1500 }
1501
1502 #endif // wxUSE_FONTMAP/!wxUSE_FONTMAP
1503
1504 /*
1505 Creates a hidden window with supplied window proc registering the class for
1506 it if necesssary (i.e. the first time only). Caller is responsible for
1507 destroying the window and unregistering the class (note that this must be
1508 done because wxWidgets may be used as a DLL and so may be loaded/unloaded
1509 multiple times into/from the same process so we cna't rely on automatic
1510 Windows class unregistration).
1511
1512 pclassname is a pointer to a caller stored classname, which must initially be
1513 NULL. classname is the desired wndclass classname. If function successfully
1514 registers the class, pclassname will be set to classname.
1515 */
1516 extern "C" WXDLLIMPEXP_BASE HWND
1517 wxCreateHiddenWindow(LPCTSTR *pclassname, LPCTSTR classname, WNDPROC wndproc)
1518 {
1519 wxCHECK_MSG( classname && pclassname && wndproc, NULL,
1520 _T("NULL parameter in wxCreateHiddenWindow") );
1521
1522 // register the class fi we need to first
1523 if ( *pclassname == NULL )
1524 {
1525 WNDCLASS wndclass;
1526 wxZeroMemory(wndclass);
1527
1528 wndclass.lpfnWndProc = wndproc;
1529 wndclass.hInstance = wxGetInstance();
1530 wndclass.lpszClassName = classname;
1531
1532 if ( !::RegisterClass(&wndclass) )
1533 {
1534 wxLogLastError(wxT("RegisterClass() in wxCreateHiddenWindow"));
1535
1536 return NULL;
1537 }
1538
1539 *pclassname = classname;
1540 }
1541
1542 // next create the window
1543 HWND hwnd = ::CreateWindow
1544 (
1545 *pclassname,
1546 NULL,
1547 0, 0, 0, 0,
1548 0,
1549 (HWND) NULL,
1550 (HMENU)NULL,
1551 wxGetInstance(),
1552 (LPVOID) NULL
1553 );
1554
1555 if ( !hwnd )
1556 {
1557 wxLogLastError(wxT("CreateWindow() in wxCreateHiddenWindow"));
1558 }
1559
1560 return hwnd;
1561 }