]>
Commit | Line | Data |
---|---|---|
0e320a79 DW |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: utils.cpp | |
3 | // Purpose: Various utilities | |
45fcbf3b | 4 | // Author: David Webster |
0e320a79 | 5 | // Modified by: |
45fcbf3b | 6 | // Created: 09/17/99 |
0e320a79 | 7 | // RCS-ID: $Id$ |
45fcbf3b DW |
8 | // Copyright: (c) David Webster |
9 | // Licence: wxWindows license | |
0e320a79 DW |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
45fcbf3b DW |
12 | // For compilers that support precompilation, includes "wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifndef WX_PRECOMP | |
16 | #include "wx/setup.h" | |
17 | #include "wx/utils.h" | |
18 | #include "wx/app.h" | |
19 | #include "wx/cursor.h" | |
20 | #endif //WX_PRECOMP | |
21 | ||
22 | #include "wx/os2/private.h" | |
23 | #include "wx/timer.h" | |
24 | #include "wx/intl.h" | |
25 | ||
0e320a79 | 26 | #include <ctype.h> |
06298235 SN |
27 | #ifdef __EMX__ |
28 | #include <dirent.h> | |
06298235 | 29 | #endif |
45fcbf3b DW |
30 | |
31 | #include "wx/log.h" | |
32 | ||
33 | #include <io.h> | |
0e320a79 DW |
34 | |
35 | #include <stdio.h> | |
36 | #include <stdlib.h> | |
37 | #include <string.h> | |
29d83fc1 DW |
38 | // |
39 | // already defined via nerror.h in app.h so undef them | |
40 | // | |
41 | #ifdef EEXIST | |
42 | #undef EEXIST | |
43 | #endif | |
44 | #ifdef ENOENT | |
45 | #undef ENOENT | |
46 | #endif | |
47 | #ifdef EMFILE | |
48 | #undef EMFILE | |
49 | #endif | |
50 | #ifdef EINTR | |
51 | #undef EINTR | |
52 | #endif | |
53 | #ifdef EINVAL | |
54 | #undef EINVAL | |
55 | #endif | |
56 | #ifdef ENOMEM | |
57 | #undef ENOMEM | |
58 | #endif | |
59 | #ifdef EACCES | |
60 | #undef EACCES | |
61 | #endif | |
45fcbf3b | 62 | #include <errno.h> |
0e320a79 DW |
63 | #include <stdarg.h> |
64 | ||
d90895ac | 65 | #define PURE_32 |
468e327a | 66 | |
06298235 | 67 | #ifndef __EMX__ |
7cdc2f1e DW |
68 | #include <upm.h> |
69 | #include <netcons.h> | |
70 | #include <netbios.h> | |
06298235 | 71 | #endif |
45fcbf3b | 72 | |
86de7616 DW |
73 | static const wxChar WX_SECTION[] = _T("wxWindows"); |
74 | static const wxChar eHOSTNAME[] = _T("HostName"); | |
75 | static const wxChar eUSERID[] = _T("UserId"); | |
76 | static const wxChar eUSERNAME[] = _T("UserName"); | |
45fcbf3b DW |
77 | |
78 | // For the following functions we SHOULD fill in support | |
79 | // for Windows-NT (which I don't know) as I assume it begin | |
80 | // a POSIX Unix (so claims MS) that it has some special | |
81 | // functions beyond those provided by WinSock | |
82 | ||
0e320a79 | 83 | // Get full hostname (eg. DoDo.BSn-Germany.crg.de) |
10e5b930 DW |
84 | bool wxGetHostName( |
85 | wxChar* zBuf | |
86 | , int nMaxSize | |
87 | ) | |
0e320a79 | 88 | { |
d90895ac | 89 | #if wxUSE_NET_API |
10e5b930 DW |
90 | char zServer[256]; |
91 | char zComputer[256]; | |
909b4f08 | 92 | unsigned long ulLevel = 0; |
9dea36ef DW |
93 | unsigned char* zBuffer = NULL; |
94 | unsigned long ulBuffer = 256; | |
95 | unsigned long* pulTotalAvail = NULL; | |
10e5b930 | 96 | |
dde11e60 DW |
97 | NetBios32GetInfo( (const unsigned char*)zServer |
98 | ,(const unsigned char*)zComputer | |
909b4f08 | 99 | ,ulLevel |
dde11e60 | 100 | ,zBuffer |
909b4f08 DW |
101 | ,ulBuffer |
102 | ,pulTotalAvail | |
dde11e60 | 103 | ); |
10e5b930 | 104 | strcpy(zBuf, zServer); |
45fcbf3b | 105 | #else |
10e5b930 DW |
106 | wxChar* zSysname; |
107 | const wxChar* zDefaultHost = _T("noname"); | |
45fcbf3b | 108 | |
10e5b930 DW |
109 | if ((zSysname = wxGetenv(_T("SYSTEM_NAME"))) == NULL) |
110 | { | |
111 | ULONG n = ::PrfQueryProfileString( HINI_PROFILE | |
112 | ,(PSZ)WX_SECTION | |
113 | ,(PSZ)eHOSTNAME | |
114 | ,(PSZ)zDefaultHost | |
115 | ,(void*)zBuf | |
116 | ,(ULONG)nMaxSize - 1 | |
117 | ); | |
118 | } | |
119 | else | |
120 | wxStrncpy(zBuf, zSysname, nMaxSize - 1); | |
121 | zBuf[nMaxSize] = _T('\0'); | |
45fcbf3b | 122 | #endif |
10e5b930 | 123 | return *zBuf ? TRUE : FALSE; |
0e320a79 DW |
124 | } |
125 | ||
126 | // Get user ID e.g. jacs | |
10e5b930 DW |
127 | bool wxGetUserId( |
128 | wxChar* zBuf | |
2bd5bbc9 | 129 | , int nType |
10e5b930 | 130 | ) |
0e320a79 | 131 | { |
06298235 | 132 | #ifndef __EMX__ |
2bd5bbc9 DW |
133 | long lrc; |
134 | // UPM procs return 0 on success | |
135 | lrc = U32ELOCU((unsigned char*)zBuf, (unsigned long *)&nType); | |
136 | if (lrc == 0) return TRUE; | |
06298235 | 137 | #endif |
2bd5bbc9 | 138 | return FALSE; |
0e320a79 DW |
139 | } |
140 | ||
10e5b930 DW |
141 | bool wxGetUserName( |
142 | wxChar* zBuf | |
143 | , int nMaxSize | |
144 | ) | |
0e320a79 | 145 | { |
45fcbf3b | 146 | #ifdef USE_NET_API |
10e5b930 DW |
147 | wxGetUserId( zBuf |
148 | ,nMaxSize | |
149 | ); | |
45fcbf3b | 150 | #else |
10e5b930 | 151 | wxStrncpy(zBuf, _T("Unknown User"), nMaxSize); |
45fcbf3b | 152 | #endif |
10e5b930 | 153 | return TRUE; |
0e320a79 DW |
154 | } |
155 | ||
10e5b930 DW |
156 | int wxKill( |
157 | long lPid | |
158 | , int nSig | |
159 | ) | |
0e320a79 | 160 | { |
10e5b930 | 161 | return((int)::DosKillProcess(0, (PID)lPid)); |
0e320a79 DW |
162 | } |
163 | ||
164 | // | |
165 | // Execute a program in an Interactive Shell | |
166 | // | |
10e5b930 DW |
167 | bool wxShell( |
168 | const wxString& rCommand | |
169 | ) | |
0e320a79 | 170 | { |
c5fb56c0 DW |
171 | wxChar* zShell = _T("CMD.EXE"); |
172 | wxString sInputs; | |
10e5b930 | 173 | wxChar zTmp[255]; |
c5fb56c0 DW |
174 | STARTDATA SData = {0}; |
175 | PSZ PgmTitle = "Command Shell"; | |
176 | APIRET rc; | |
177 | PID vPid = 0; | |
178 | ULONG ulSessID = 0; | |
179 | UCHAR achObjBuf[256] = {0}; //error data if DosStart fails | |
180 | RESULTCODES vResult; | |
181 | ||
182 | SData.Length = sizeof(STARTDATA); | |
183 | SData.Related = SSF_RELATED_INDEPENDENT; | |
184 | SData.FgBg = SSF_FGBG_FORE; | |
185 | SData.TraceOpt = SSF_TRACEOPT_NONE; | |
186 | SData.PgmTitle = PgmTitle; | |
187 | SData.PgmName = zShell; | |
188 | ||
909b4f08 | 189 | sInputs = "/C " + rCommand; |
9ac6ff7b | 190 | SData.PgmInputs = (BYTE*)sInputs.c_str(); |
c5fb56c0 DW |
191 | SData.TermQ = 0; |
192 | SData.Environment = 0; | |
193 | SData.InheritOpt = SSF_INHERTOPT_SHELL; | |
194 | SData.SessionType = SSF_TYPE_WINDOWABLEVIO; | |
195 | SData.IconFile = 0; | |
196 | SData.PgmHandle = 0; | |
197 | SData.PgmControl = SSF_CONTROL_VISIBLE | SSF_CONTROL_MAXIMIZE; | |
198 | SData.InitXPos = 30; | |
199 | SData.InitYPos = 40; | |
200 | SData.InitXSize = 200; | |
201 | SData.InitYSize = 140; | |
202 | SData.Reserved = 0; | |
203 | SData.ObjectBuffer = (char*)achObjBuf; | |
204 | SData.ObjectBuffLen = (ULONG)sizeof(achObjBuf); | |
205 | ||
206 | rc = ::DosStartSession(&SData, &ulSessID, &vPid); | |
9ac6ff7b | 207 | if (rc == 0 || rc == 457) // NO_ERROR or SMG_START_IN_BACKGROUND |
c5fb56c0 DW |
208 | { |
209 | PTIB ptib; | |
210 | PPIB ppib; | |
10e5b930 | 211 | |
c5fb56c0 | 212 | ::DosGetInfoBlocks(&ptib, &ppib); |
10e5b930 | 213 | |
c5fb56c0 DW |
214 | ::DosWaitChild( DCWA_PROCESS |
215 | ,DCWW_WAIT | |
216 | ,&vResult | |
217 | ,&ppib->pib_ulpid | |
218 | ,vPid | |
219 | ); | |
220 | } | |
221 | return (rc != 0); | |
0e320a79 DW |
222 | } |
223 | ||
224 | // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX) | |
5d4b632b | 225 | long wxGetFreeMemory() |
0e320a79 | 226 | { |
9dea36ef | 227 | void* pMemptr = NULL; |
78d50441 DW |
228 | ULONG lSize; |
229 | ULONG lMemFlags; | |
230 | APIRET rc; | |
231 | ||
232 | lMemFlags = PAG_FREE; | |
10e5b930 | 233 | rc = ::DosQueryMem(pMemptr, &lSize, &lMemFlags); |
78d50441 DW |
234 | if (rc != 0) |
235 | return -1L; | |
236 | return (long)lSize; | |
45fcbf3b DW |
237 | } |
238 | ||
8269a903 SN |
239 | // ---------------------------------------------------------------------------- |
240 | // env vars | |
241 | // ---------------------------------------------------------------------------- | |
242 | ||
243 | bool wxGetEnv(const wxString& var, wxString *value) | |
244 | { | |
245 | // wxGetenv is defined as getenv() | |
246 | wxChar *p = wxGetenv(var); | |
247 | if ( !p ) | |
248 | return FALSE; | |
249 | ||
250 | if ( value ) | |
251 | { | |
252 | *value = p; | |
253 | } | |
254 | ||
255 | return TRUE; | |
256 | } | |
257 | ||
258 | bool wxSetEnv(const wxString& variable, const wxChar *value) | |
259 | { | |
260 | #if defined(HAVE_SETENV) | |
261 | return setenv(variable.mb_str(), value ? wxString(value).mb_str().data() | |
262 | : NULL, 1 /* overwrite */) == 0; | |
263 | #elif defined(HAVE_PUTENV) | |
264 | wxString s = variable; | |
265 | if ( value ) | |
266 | s << _T('=') << value; | |
267 | ||
268 | // transform to ANSI | |
269 | const char *p = s.mb_str(); | |
270 | ||
271 | // the string will be free()d by libc | |
272 | char *buf = (char *)malloc(strlen(p) + 1); | |
273 | strcpy(buf, p); | |
274 | ||
275 | return putenv(buf) == 0; | |
276 | #else // no way to set an env var | |
277 | return FALSE; | |
278 | #endif | |
279 | } | |
280 | ||
281 | ||
45fcbf3b DW |
282 | // Sleep for nSecs seconds. Attempt a Windows implementation using timers. |
283 | static bool inTimer = FALSE; | |
284 | ||
285 | class wxSleepTimer: public wxTimer | |
286 | { | |
10e5b930 DW |
287 | public: |
288 | inline void Notify() | |
289 | { | |
290 | inTimer = FALSE; | |
291 | Stop(); | |
292 | } | |
45fcbf3b DW |
293 | }; |
294 | ||
10e5b930 | 295 | static wxTimer* wxTheSleepTimer = NULL; |
45fcbf3b | 296 | |
10e5b930 DW |
297 | void wxUsleep( |
298 | unsigned long ulMilliseconds | |
299 | ) | |
45fcbf3b | 300 | { |
468e327a | 301 | ::DosSleep(ulMilliseconds/1000l); |
0e320a79 DW |
302 | } |
303 | ||
10e5b930 DW |
304 | void wxSleep( |
305 | int nSecs | |
306 | ) | |
0e320a79 | 307 | { |
10e5b930 | 308 | ::DosSleep(1000 * nSecs); |
0e320a79 DW |
309 | } |
310 | ||
311 | // Consume all events until no more left | |
312 | void wxFlushEvents() | |
313 | { | |
45fcbf3b | 314 | // wxYield(); |
0e320a79 DW |
315 | } |
316 | ||
45fcbf3b | 317 | // Output a debug mess., in a system dependent fashion. |
10e5b930 DW |
318 | void wxDebugMsg( |
319 | const wxChar* zFmt ... | |
320 | ) | |
321 | { | |
322 | va_list vAp; | |
323 | static wxChar zBuffer[512]; | |
324 | ||
325 | if (!wxTheApp->GetWantDebugOutput()) | |
326 | return ; | |
327 | va_start(vAp, zFmt); | |
328 | sprintf(zBuffer, zFmt, vAp) ; | |
329 | va_end(vAp); | |
0e320a79 DW |
330 | } |
331 | ||
332 | // Non-fatal error: pop up message box and (possibly) continue | |
10e5b930 DW |
333 | void wxError( |
334 | const wxString& rMsg | |
335 | , const wxString& rTitle | |
336 | ) | |
337 | { | |
13a4ea8d | 338 | wxBuffer = new wxChar[256]; |
10e5b930 DW |
339 | wxSprintf(wxBuffer, "%s\nContinue?", WXSTRINGCAST rMsg); |
340 | if (::WinMessageBox( HWND_DESKTOP | |
341 | ,NULL | |
342 | ,(PSZ)wxBuffer | |
343 | ,(PSZ)WXSTRINGCAST rTitle | |
344 | ,0 | |
345 | ,MB_ICONEXCLAMATION | MB_YESNO | |
346 | ) == MBID_YES) | |
13a4ea8d | 347 | delete[] wxBuffer; |
0e320a79 DW |
348 | wxExit(); |
349 | } | |
350 | ||
351 | // Fatal error: pop up message box and abort | |
10e5b930 DW |
352 | void wxFatalError( |
353 | const wxString& rMsg | |
354 | , const wxString& rTitle | |
355 | ) | |
356 | { | |
357 | unsigned long ulRc; | |
358 | ||
359 | ulRc = ::WinMessageBox( HWND_DESKTOP | |
360 | ,NULL | |
361 | ,WXSTRINGCAST rMsg | |
362 | ,WXSTRINGCAST rTitle | |
363 | ,0 | |
364 | ,MB_NOICON | MB_OK | |
365 | ); | |
366 | DosExit(EXIT_PROCESS, ulRc); | |
0e320a79 DW |
367 | } |
368 | ||
369 | // Emit a beeeeeep | |
370 | void wxBell() | |
371 | { | |
45fcbf3b | 372 | DosBeep(1000,1000); // 1kHz during 1 sec. |
0e320a79 DW |
373 | } |
374 | ||
45fcbf3b DW |
375 | // Chris Breeze 27/5/98: revised WIN32 code to |
376 | // detect WindowsNT correctly | |
10e5b930 DW |
377 | int wxGetOsVersion( |
378 | int* pMajorVsn | |
379 | , int* pMinorVsn | |
380 | ) | |
381 | { | |
382 | ULONG ulSysInfo[QSV_MAX] = {0}; | |
5d4b632b DW |
383 | APIRET ulrc; |
384 | ||
385 | ulrc = ::DosQuerySysInfo( 1L | |
386 | ,QSV_MAX | |
387 | ,(PVOID)ulSysInfo | |
388 | ,sizeof(ULONG) * QSV_MAX | |
389 | ); | |
390 | if (ulrc == 0L) | |
10e5b930 DW |
391 | { |
392 | *pMajorVsn = ulSysInfo[QSV_VERSION_MAJOR]; | |
5d4b632b | 393 | *pMajorVsn = *pMajorVsn/10; |
10e5b930 DW |
394 | *pMinorVsn = ulSysInfo[QSV_VERSION_MINOR]; |
395 | return wxWINDOWS_OS2; | |
396 | } | |
397 | return wxWINDOWS; // error if we get here, return generic value | |
0e320a79 DW |
398 | } |
399 | ||
400 | // Reading and writing resources (eg WIN.INI, .Xdefaults) | |
401 | #if wxUSE_RESOURCES | |
10e5b930 DW |
402 | bool wxWriteResource( |
403 | const wxString& rSection | |
404 | , const wxString& rEntry | |
405 | , const wxString& rValue | |
406 | , const wxString& rFile | |
407 | ) | |
0e320a79 | 408 | { |
9dea36ef DW |
409 | HAB hab = 0; |
410 | HINI hIni = 0; | |
78d50441 | 411 | |
10e5b930 | 412 | if (rFile != "") |
78d50441 | 413 | { |
10e5b930 | 414 | hIni = ::PrfOpenProfile(hab, (PSZ)WXSTRINGCAST rFile); |
78d50441 DW |
415 | if (hIni != 0L) |
416 | { | |
417 | return (::PrfWriteProfileString( hIni | |
10e5b930 DW |
418 | ,(PSZ)WXSTRINGCAST rSection |
419 | ,(PSZ)WXSTRINGCAST rEntry | |
420 | ,(PSZ)WXSTRINGCAST rValue | |
78d50441 DW |
421 | )); |
422 | } | |
423 | } | |
424 | else | |
425 | return (::PrfWriteProfileString( HINI_PROFILE | |
10e5b930 DW |
426 | ,(PSZ)WXSTRINGCAST rSection |
427 | ,(PSZ)WXSTRINGCAST rEntry | |
428 | ,(PSZ)WXSTRINGCAST rValue | |
78d50441 | 429 | )); |
d90895ac | 430 | return FALSE; |
0e320a79 DW |
431 | } |
432 | ||
10e5b930 DW |
433 | bool wxWriteResource( |
434 | const wxString& rSection | |
435 | , const wxString& rEntry | |
436 | , float fValue | |
437 | , const wxString& rFile | |
438 | ) | |
439 | { | |
440 | wxChar zBuf[50]; | |
441 | ||
442 | wxSprintf(zBuf, "%.4f", fValue); | |
443 | return wxWriteResource( rSection | |
444 | ,rEntry | |
445 | ,zBuf | |
446 | ,rFile | |
447 | ); | |
0e320a79 DW |
448 | } |
449 | ||
1be7f92a DW |
450 | bool wxWriteResource( |
451 | const wxString& rSection | |
452 | , const wxString& rEntry | |
453 | , long lValue | |
454 | , const wxString& rFile | |
455 | ) | |
0e320a79 | 456 | { |
1be7f92a DW |
457 | wxChar zBuf[50]; |
458 | ||
459 | wxSprintf(zBuf, "%ld", lValue); | |
460 | return wxWriteResource( rSection | |
461 | ,rEntry | |
462 | ,zBuf | |
463 | ,rFile | |
464 | ); | |
0e320a79 DW |
465 | } |
466 | ||
1be7f92a DW |
467 | bool wxWriteResource( |
468 | const wxString& rSection | |
469 | , const wxString& rEntry | |
470 | , int lValue | |
471 | , const wxString& rFile | |
472 | ) | |
0e320a79 | 473 | { |
1be7f92a DW |
474 | wxChar zBuf[50]; |
475 | ||
476 | wxSprintf(zBuf, "%d", lValue); | |
477 | return wxWriteResource( rSection | |
478 | ,rEntry | |
479 | ,zBuf | |
480 | ,rFile | |
481 | ); | |
0e320a79 DW |
482 | } |
483 | ||
1be7f92a DW |
484 | bool wxGetResource( |
485 | const wxString& rSection | |
486 | , const wxString& rEntry | |
487 | , wxChar** ppValue | |
488 | , const wxString& rFile | |
489 | ) | |
0e320a79 | 490 | { |
9dea36ef DW |
491 | HAB hab = 0; |
492 | HINI hIni = 0; | |
e8fd750b DW |
493 | wxChar zDefunkt[] = _T("$$default"); |
494 | char zBuf[1000]; | |
78d50441 | 495 | |
1be7f92a | 496 | if (rFile != "") |
78d50441 | 497 | { |
1be7f92a | 498 | hIni = ::PrfOpenProfile(hab, (PSZ)WXSTRINGCAST rFile); |
78d50441 DW |
499 | if (hIni != 0L) |
500 | { | |
501 | ULONG n = ::PrfQueryProfileString( hIni | |
1be7f92a DW |
502 | ,(PSZ)WXSTRINGCAST rSection |
503 | ,(PSZ)WXSTRINGCAST rEntry | |
504 | ,(PSZ)zDefunkt | |
e8fd750b | 505 | ,(PVOID)zBuf |
78d50441 DW |
506 | ,1000 |
507 | ); | |
e8fd750b | 508 | if (zBuf == NULL) |
78d50441 | 509 | return FALSE; |
e8fd750b DW |
510 | if (n == 0L || wxStrcmp(zBuf, zDefunkt) == 0) |
511 | return FALSE; | |
512 | zBuf[n-1] = '\0'; | |
78d50441 DW |
513 | } |
514 | else | |
515 | return FALSE; | |
516 | } | |
517 | else | |
518 | { | |
519 | ULONG n = ::PrfQueryProfileString( HINI_PROFILE | |
1be7f92a DW |
520 | ,(PSZ)WXSTRINGCAST rSection |
521 | ,(PSZ)WXSTRINGCAST rEntry | |
522 | ,(PSZ)zDefunkt | |
e8fd750b | 523 | ,(PVOID)zBuf |
78d50441 DW |
524 | ,1000 |
525 | ); | |
e8fd750b DW |
526 | if (zBuf == NULL) |
527 | return FALSE; | |
528 | if (n == 0L || wxStrcmp(zBuf, zDefunkt) == 0) | |
78d50441 | 529 | return FALSE; |
e8fd750b | 530 | zBuf[n-1] = '\0'; |
78d50441 | 531 | } |
e8fd750b | 532 | strcpy((char*)*ppValue, zBuf); |
78d50441 | 533 | return TRUE; |
d90895ac | 534 | } |
0e320a79 | 535 | |
1be7f92a DW |
536 | bool wxGetResource( |
537 | const wxString& rSection | |
538 | , const wxString& rEntry | |
539 | , float* pValue | |
540 | , const wxString& rFile | |
541 | ) | |
0e320a79 | 542 | { |
1be7f92a | 543 | wxChar* zStr = NULL; |
13a4ea8d DW |
544 | |
545 | zStr = new wxChar[1000]; | |
1be7f92a DW |
546 | bool bSucc = wxGetResource( rSection |
547 | ,rEntry | |
548 | ,(wxChar **)&zStr | |
549 | ,rFile | |
550 | ); | |
551 | ||
552 | if (bSucc) | |
553 | { | |
554 | *pValue = (float)wxStrtod(zStr, NULL); | |
555 | delete[] zStr; | |
556 | return TRUE; | |
557 | } | |
13a4ea8d DW |
558 | else |
559 | { | |
560 | delete[] zStr; | |
561 | return FALSE; | |
562 | } | |
0e320a79 DW |
563 | } |
564 | ||
1be7f92a DW |
565 | bool wxGetResource( |
566 | const wxString& rSection | |
567 | , const wxString& rEntry | |
568 | , long* pValue | |
569 | , const wxString& rFile | |
570 | ) | |
0e320a79 | 571 | { |
1be7f92a | 572 | wxChar* zStr = NULL; |
13a4ea8d DW |
573 | |
574 | zStr = new wxChar[1000]; | |
1be7f92a DW |
575 | bool bSucc = wxGetResource( rSection |
576 | ,rEntry | |
577 | ,(wxChar **)&zStr | |
578 | ,rFile | |
579 | ); | |
580 | ||
581 | if (bSucc) | |
582 | { | |
583 | *pValue = wxStrtol(zStr, NULL, 10); | |
584 | delete[] zStr; | |
585 | return TRUE; | |
586 | } | |
13a4ea8d DW |
587 | else |
588 | { | |
589 | delete[] zStr; | |
590 | return FALSE; | |
591 | } | |
0e320a79 DW |
592 | } |
593 | ||
1be7f92a DW |
594 | bool wxGetResource( |
595 | const wxString& rSection | |
596 | , const wxString& rEntry | |
597 | , int* pValue | |
598 | , const wxString& rFile | |
599 | ) | |
0e320a79 | 600 | { |
1be7f92a | 601 | wxChar* zStr = NULL; |
13a4ea8d DW |
602 | |
603 | zStr = new wxChar[1000]; | |
1be7f92a DW |
604 | bool bSucc = wxGetResource( rSection |
605 | ,rEntry | |
606 | ,(wxChar **)&zStr | |
607 | ,rFile | |
608 | ); | |
609 | ||
610 | if (bSucc) | |
611 | { | |
612 | *pValue = (int)wxStrtol(zStr, NULL, 10); | |
613 | delete[] zStr; | |
614 | return TRUE; | |
615 | } | |
13a4ea8d DW |
616 | else |
617 | { | |
618 | delete[] zStr; | |
619 | return FALSE; | |
620 | } | |
0e320a79 DW |
621 | } |
622 | #endif // wxUSE_RESOURCES | |
623 | ||
45fcbf3b DW |
624 | // --------------------------------------------------------------------------- |
625 | // helper functions for showing a "busy" cursor | |
626 | // --------------------------------------------------------------------------- | |
627 | ||
628 | HCURSOR gs_wxBusyCursor = 0; // new, busy cursor | |
629 | HCURSOR gs_wxBusyCursorOld = 0; // old cursor | |
630 | static int gs_wxBusyCursorCount = 0; | |
0e320a79 DW |
631 | |
632 | // Set the cursor to the busy cursor for all windows | |
1be7f92a DW |
633 | void wxBeginBusyCursor( |
634 | wxCursor* pCursor | |
635 | ) | |
0e320a79 | 636 | { |
45fcbf3b DW |
637 | if ( gs_wxBusyCursorCount++ == 0 ) |
638 | { | |
1be7f92a | 639 | gs_wxBusyCursor = (HCURSOR)pCursor->GetHCURSOR(); |
45fcbf3b DW |
640 | ::WinSetPointer(HWND_DESKTOP, (HPOINTER)gs_wxBusyCursor); |
641 | } | |
642 | //else: nothing to do, already set | |
0e320a79 DW |
643 | } |
644 | ||
645 | // Restore cursor to normal | |
646 | void wxEndBusyCursor() | |
647 | { | |
1be7f92a DW |
648 | wxCHECK_RET( gs_wxBusyCursorCount > 0 |
649 | ,_T("no matching wxBeginBusyCursor() for wxEndBusyCursor()") | |
650 | ); | |
45fcbf3b | 651 | |
1be7f92a | 652 | if (--gs_wxBusyCursorCount == 0) |
45fcbf3b DW |
653 | { |
654 | ::WinSetPointer(HWND_DESKTOP, (HPOINTER)gs_wxBusyCursorOld); | |
655 | gs_wxBusyCursorOld = 0; | |
656 | } | |
0e320a79 DW |
657 | } |
658 | ||
659 | // TRUE if we're between the above two calls | |
660 | bool wxIsBusy() | |
661 | { | |
1be7f92a | 662 | return (gs_wxBusyCursorCount > 0); |
45fcbf3b DW |
663 | } |
664 | ||
665 | // --------------------------------------------------------------------------- | |
1be7f92a DW |
666 | const wxChar* wxGetHomeDir( |
667 | wxString* pStr | |
668 | ) | |
45fcbf3b | 669 | { |
1be7f92a | 670 | wxString& rStrDir = *pStr; |
0e320a79 | 671 | |
1be7f92a DW |
672 | // OS/2 has no idea about home, |
673 | // so use the working directory instead? | |
45fcbf3b | 674 | |
1be7f92a | 675 | // 256 was taken from os2def.h |
45fcbf3b DW |
676 | #ifndef MAX_PATH |
677 | # define MAX_PATH 256 | |
678 | #endif | |
679 | ||
1be7f92a DW |
680 | char zDirName[256]; |
681 | ULONG ulDirLen; | |
45fcbf3b | 682 | |
1be7f92a DW |
683 | ::DosQueryCurrentDir(0, zDirName, &ulDirLen); |
684 | rStrDir = zDirName; | |
685 | return rStrDir.c_str(); | |
45fcbf3b DW |
686 | } |
687 | ||
10e5b930 | 688 | // Hack for OS/2 |
1be7f92a DW |
689 | wxChar* wxGetUserHome ( |
690 | const wxString& rUser | |
691 | ) | |
692 | { | |
693 | wxChar* zHome; | |
694 | wxString sUser1(rUser); | |
695 | ||
13a4ea8d | 696 | wxBuffer = new wxChar[256]; |
06298235 | 697 | #ifndef __EMX__ |
1be7f92a | 698 | if (sUser1 != _T("")) |
45fcbf3b | 699 | { |
1be7f92a DW |
700 | wxChar zTmp[64]; |
701 | ||
702 | if (wxGetUserId( zTmp | |
703 | ,sizeof(zTmp)/sizeof(char) | |
704 | )) | |
705 | { | |
706 | // Guests belong in the temp dir | |
707 | if (wxStricmp(zTmp, _T("annonymous")) == 0) | |
708 | { | |
709 | if ((zHome = wxGetenv(_T("TMP"))) != NULL || | |
710 | (zHome = wxGetenv(_T("TMPDIR"))) != NULL || | |
711 | (zHome = wxGetenv(_T("TEMP"))) != NULL) | |
13a4ea8d | 712 | delete[] wxBuffer; |
1be7f92a DW |
713 | return *zHome ? zHome : (wxChar*)_T("\\"); |
714 | } | |
715 | if (wxStricmp(zTmp, WXSTRINGCAST sUser1) == 0) | |
716 | sUser1 = _T(""); | |
717 | } | |
45fcbf3b | 718 | } |
06298235 | 719 | #endif |
1be7f92a | 720 | if (sUser1 == _T("")) |
13a4ea8d | 721 | { |
1be7f92a DW |
722 | if ((zHome = wxGetenv(_T("HOME"))) != NULL) |
723 | { | |
724 | wxStrcpy(wxBuffer, zHome); | |
725 | Unix2DosFilename(wxBuffer); | |
13a4ea8d DW |
726 | wxStrcpy(zHome, wxBuffer); |
727 | delete[] wxBuffer; | |
728 | return zHome; | |
1be7f92a | 729 | } |
13a4ea8d DW |
730 | } |
731 | delete[] wxBuffer; | |
732 | return NULL; // No home known! | |
0e320a79 DW |
733 | } |
734 | ||
735 | // Check whether this window wants to process messages, e.g. Stop button | |
736 | // in long calculations. | |
1be7f92a DW |
737 | bool wxCheckForInterrupt( |
738 | wxWindow* pWnd | |
739 | ) | |
0e320a79 | 740 | { |
1be7f92a DW |
741 | if(pWnd) |
742 | { | |
743 | QMSG vMsg; | |
9dea36ef DW |
744 | HAB hab = 0; |
745 | HWND hwndFilter = NULLHANDLE; | |
1be7f92a | 746 | HWND hwndWin= (HWND) pWnd->GetHWND(); |
45fcbf3b | 747 | |
1be7f92a DW |
748 | while(::WinPeekMsg(hab, &vMsg, hwndFilter, 0, 0, PM_REMOVE)) |
749 | { | |
750 | ::WinDispatchMsg(hab, &vMsg); | |
751 | } | |
752 | return TRUE;//*** temporary? | |
753 | } | |
754 | else | |
45fcbf3b | 755 | { |
1be7f92a DW |
756 | wxFAIL_MSG(_T("pWnd==NULL !!!")); |
757 | return FALSE;//*** temporary? | |
45fcbf3b | 758 | } |
45fcbf3b DW |
759 | } |
760 | ||
1be7f92a DW |
761 | void wxGetMousePosition( |
762 | int* pX | |
763 | , int* pY | |
764 | ) | |
0e320a79 | 765 | { |
1be7f92a DW |
766 | POINTL vPt; |
767 | ||
768 | ::WinQueryPointerPos(HWND_DESKTOP, &vPt); | |
769 | *pX = vPt.x; | |
770 | *pY = vPt.y; | |
0e320a79 DW |
771 | }; |
772 | ||
773 | // Return TRUE if we have a colour display | |
774 | bool wxColourDisplay() | |
775 | { | |
1be7f92a DW |
776 | HPS hpsScreen; |
777 | HDC hdcScreen; | |
778 | LONG lColors; | |
779 | ||
780 | hpsScreen = ::WinGetScreenPS(HWND_DESKTOP); | |
781 | hdcScreen = ::GpiQueryDevice(hpsScreen); | |
782 | ::DevQueryCaps(hdcScreen, CAPS_COLORS, 1L, &lColors); | |
783 | return(lColors > 1L); | |
0e320a79 DW |
784 | } |
785 | ||
786 | // Returns depth of screen | |
787 | int wxDisplayDepth() | |
788 | { | |
1be7f92a DW |
789 | HPS hpsScreen; |
790 | HDC hdcScreen; | |
791 | LONG lPlanes; | |
792 | LONG lBitsPerPixel; | |
793 | LONG nDepth; | |
794 | ||
795 | hpsScreen = ::WinGetScreenPS(HWND_DESKTOP); | |
796 | hdcScreen = ::GpiQueryDevice(hpsScreen); | |
797 | ::DevQueryCaps(hdcScreen, CAPS_COLOR_PLANES, 1L, &lPlanes); | |
798 | ::DevQueryCaps(hdcScreen, CAPS_COLOR_BITCOUNT, 1L, &lBitsPerPixel); | |
799 | ||
800 | nDepth = (int)(lPlanes * lBitsPerPixel); | |
801 | DevCloseDC(hdcScreen); | |
d90895ac | 802 | return (nDepth); |
0e320a79 DW |
803 | } |
804 | ||
805 | // Get size of display | |
1be7f92a DW |
806 | void wxDisplaySize( |
807 | int* pWidth | |
808 | , int* pHeight | |
809 | ) | |
0e320a79 | 810 | { |
1be7f92a DW |
811 | HPS hpsScreen; |
812 | HDC hdcScreen; | |
892b89f3 DW |
813 | LONG lWidth; |
814 | LONG lHeight; | |
45fcbf3b | 815 | |
1be7f92a DW |
816 | hpsScreen = ::WinGetScreenPS(HWND_DESKTOP); |
817 | hdcScreen = ::GpiQueryDevice(hpsScreen); | |
892b89f3 DW |
818 | ::DevQueryCaps(hdcScreen, CAPS_WIDTH, 1L, &lWidth); |
819 | ::DevQueryCaps(hdcScreen, CAPS_HEIGHT, 1L, &lHeight); | |
1be7f92a | 820 | DevCloseDC(hdcScreen); |
892b89f3 DW |
821 | *pWidth = (int)lWidth; |
822 | *pHeight = (int)lHeight; | |
45fcbf3b DW |
823 | } |
824 | ||
5b3ed311 DW |
825 | void wxDisplaySizeMM( |
826 | int* pWidth | |
827 | , int* pHeight | |
828 | ) | |
829 | { | |
830 | HPS hpsScreen; | |
831 | HDC hdcScreen; | |
832 | ||
833 | hpsScreen = ::WinGetScreenPS(HWND_DESKTOP); | |
834 | hdcScreen = ::GpiQueryDevice(hpsScreen); | |
835 | ||
836 | if (pWidth) | |
837 | ::DevQueryCaps( hdcScreen | |
838 | ,CAPS_HORIZONTAL_RESOLUTION | |
839 | ,1L | |
840 | ,(PLONG)pWidth | |
841 | ); | |
842 | if (pHeight) | |
843 | ::DevQueryCaps( hdcScreen | |
844 | ,CAPS_VERTICAL_RESOLUTION | |
845 | ,1L | |
846 | ,(PLONG)pHeight | |
847 | ); | |
848 | } | |
849 | ||
ec5d7799 RD |
850 | void wxClientDisplayRect(int *x, int *y, int *width, int *height) |
851 | { | |
852 | // This is supposed to return desktop dimensions minus any window | |
853 | // manager panels, menus, taskbars, etc. If there is a way to do that | |
854 | // for this platform please fix this function, otherwise it defaults | |
855 | // to the entire desktop. | |
856 | if (x) *x = 0; | |
857 | if (y) *y = 0; | |
858 | wxDisplaySize(width, height); | |
859 | } | |
860 | ||
861 | ||
1be7f92a DW |
862 | bool wxDirExists( |
863 | const wxString& rDir | |
864 | ) | |
45fcbf3b | 865 | { |
1be7f92a | 866 | return (::DosSetCurrentDir(WXSTRINGCAST rDir)); |
45fcbf3b DW |
867 | } |
868 | ||
869 | // --------------------------------------------------------------------------- | |
870 | // window information functions | |
871 | // --------------------------------------------------------------------------- | |
872 | ||
1be7f92a DW |
873 | wxString WXDLLEXPORT wxGetWindowText( |
874 | WXHWND hWnd | |
875 | ) | |
45fcbf3b | 876 | { |
1be7f92a DW |
877 | wxString vStr; |
878 | long lLen = ::WinQueryWindowTextLength((HWND)hWnd) + 1; | |
879 | ||
880 | ::WinQueryWindowText((HWND)hWnd, lLen, vStr.GetWriteBuf((int)lLen)); | |
881 | vStr.UngetWriteBuf(); | |
45fcbf3b | 882 | |
1be7f92a | 883 | return vStr; |
45fcbf3b DW |
884 | } |
885 | ||
1be7f92a DW |
886 | wxString WXDLLEXPORT wxGetWindowClass( |
887 | WXHWND hWnd | |
888 | ) | |
45fcbf3b | 889 | { |
1be7f92a DW |
890 | wxString vStr; |
891 | int nLen = 256; // some starting value | |
45fcbf3b DW |
892 | |
893 | for ( ;; ) | |
894 | { | |
1be7f92a | 895 | int nCount = ::WinQueryClassName((HWND)hWnd, nLen, vStr.GetWriteBuf(nLen)); |
45fcbf3b | 896 | |
1be7f92a DW |
897 | vStr.UngetWriteBuf(); |
898 | if (nCount == nLen ) | |
45fcbf3b DW |
899 | { |
900 | // the class name might have been truncated, retry with larger | |
901 | // buffer | |
1be7f92a | 902 | nLen *= 2; |
45fcbf3b DW |
903 | } |
904 | else | |
905 | { | |
906 | break; | |
907 | } | |
908 | } | |
1be7f92a | 909 | return vStr; |
45fcbf3b DW |
910 | } |
911 | ||
1be7f92a DW |
912 | WXWORD WXDLLEXPORT wxGetWindowId( |
913 | WXHWND hWnd | |
914 | ) | |
45fcbf3b DW |
915 | { |
916 | return ::WinQueryWindowUShort((HWND)hWnd, QWS_ID); | |
0e320a79 DW |
917 | } |
918 | ||
914589c2 DW |
919 | wxString WXDLLEXPORT wxPMErrorToStr( |
920 | ERRORID vError | |
921 | ) | |
922 | { | |
923 | wxString sError; | |
924 | ||
925 | // | |
926 | // Remove the high order byte -- it is useless | |
927 | // | |
928 | vError &= 0x0000ffff; | |
929 | switch(vError) | |
930 | { | |
931 | case PMERR_INVALID_HWND: | |
932 | sError = wxT("Invalid window handle specified"); | |
933 | break; | |
934 | ||
935 | case PMERR_INVALID_FLAG: | |
936 | sError = wxT("Invalid flag bit set"); | |
937 | break; | |
938 | ||
939 | case PMERR_NO_MSG_QUEUE: | |
940 | sError = wxT("No message queue available"); | |
941 | break; | |
942 | ||
943 | case PMERR_INVALID_PARM: | |
944 | sError = wxT("Parameter contained invalid data"); | |
945 | break; | |
946 | ||
947 | case PMERR_INVALID_PARAMETERS: | |
948 | sError = wxT("Parameter value is out of range"); | |
949 | break; | |
950 | ||
951 | case PMERR_PARAMETER_OUT_OF_RANGE: | |
952 | sError = wxT("Parameter value is out of range"); | |
953 | break; | |
954 | ||
955 | case PMERR_INVALID_INTEGER_ATOM: | |
956 | sError = wxT("Not a valid atom"); | |
957 | break; | |
958 | ||
959 | case PMERR_INVALID_HATOMTBL: | |
960 | sError = wxT("Atom table handle is invalid"); | |
961 | break; | |
962 | ||
963 | case PMERR_INVALID_ATOM_NAME: | |
964 | sError = wxT("Not a valid atom name"); | |
965 | break; | |
966 | ||
967 | case PMERR_ATOM_NAME_NOT_FOUND: | |
968 | sError = wxT("Valid name format, but cannot find name in atom table"); | |
969 | break; | |
970 | ||
971 | default: | |
972 | sError = wxT("Unknown error"); | |
973 | } | |
974 | return(sError); | |
975 | } // end of wxPMErrorToStr | |
976 | ||
977 |