]> git.saurik.com Git - wxWidgets.git/blame - src/os2/utils.cpp
Allow wxPGProperty::IsChildSelected() to work recursively
[wxWidgets.git] / src / os2 / utils.cpp
CommitLineData
0e320a79 1/////////////////////////////////////////////////////////////////////////////
0e2a14e5 2// Name: src/os2/utils.cpp
0e320a79 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 8// Copyright: (c) David Webster
65571936 9// Licence: wxWindows licence
0e320a79
DW
10/////////////////////////////////////////////////////////////////////////////
11
45fcbf3b
DW
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
e4db172a
WS
15#include "wx/utils.h"
16
45fcbf3b 17#ifndef WX_PRECOMP
88a7a4e1 18 #include "wx/intl.h"
e4db172a 19 #include "wx/log.h"
45fcbf3b
DW
20#endif //WX_PRECOMP
21
22#include "wx/os2/private.h"
cd478a95 23#include "wx/apptrait.h"
783388d5 24#include "wx/filename.h"
45fcbf3b 25
0e320a79 26#include <ctype.h>
06298235
SN
27#ifdef __EMX__
28#include <dirent.h>
06298235 29#endif
45fcbf3b 30
45fcbf3b
DW
31
32#include <io.h>
0e320a79
DW
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
45fcbf3b 37#include <errno.h>
0e320a79
DW
38#include <stdarg.h>
39
d90895ac 40#define PURE_32
468e327a 41
6670f564
WS
42#if defined(__WATCOMC__)
43extern "C"
44{
45 #include <upm.h>
46}
47#elif !defined(__EMX__)
48 #include <upm.h>
49 #include <netcons.h>
50 #include <netbios.h>
06298235 51#endif
45fcbf3b 52
9a83f860
VZ
53static const wxChar WX_SECTION[] = wxT("wxWidgets");
54static const wxChar eHOSTNAME[] = wxT("HostName");
45fcbf3b
DW
55
56// For the following functions we SHOULD fill in support
57// for Windows-NT (which I don't know) as I assume it begin
58// a POSIX Unix (so claims MS) that it has some special
59// functions beyond those provided by WinSock
60
0e320a79 61// Get full hostname (eg. DoDo.BSn-Germany.crg.de)
63a3cd7a 62bool wxGetHostName( wxChar* zBuf, int nMaxSize )
0e320a79 63{
63a3cd7a
WS
64 if (!zBuf) return false;
65
66#if defined(wxUSE_NET_API) && wxUSE_NET_API
67 char zServer[256];
68 char zComputer[256];
69 unsigned long ulLevel = 0;
70 unsigned char* zBuffer = NULL;
71 unsigned long ulBuffer = 256;
72 unsigned long* pulTotalAvail = NULL;
10e5b930 73
dde11e60
DW
74 NetBios32GetInfo( (const unsigned char*)zServer
75 ,(const unsigned char*)zComputer
909b4f08 76 ,ulLevel
dde11e60 77 ,zBuffer
909b4f08
DW
78 ,ulBuffer
79 ,pulTotalAvail
dde11e60 80 );
10e5b930 81 strcpy(zBuf, zServer);
45fcbf3b 82#else
63a3cd7a 83 wxChar* zSysname;
9a83f860 84 const wxChar* zDefaultHost = wxT("noname");
45fcbf3b 85
9a83f860 86 if ((zSysname = wxGetenv(wxT("SYSTEM_NAME"))) == NULL &&
03647350 87 (zSysname = wxGetenv(wxT("HOSTNAME"))) == NULL)
10e5b930 88 {
9923c37d
DW
89 ::PrfQueryProfileString( HINI_PROFILE
90 ,(PSZ)WX_SECTION
91 ,(PSZ)eHOSTNAME
92 ,(PSZ)zDefaultHost
93 ,(void*)zBuf
94 ,(ULONG)nMaxSize - 1
95 );
9a83f860 96 zBuf[nMaxSize] = wxT('\0');
10e5b930
DW
97 }
98 else
63a3cd7a 99 {
e408bf52 100 wxStrlcpy(zBuf, zSysname, nMaxSize);
63a3cd7a 101 }
45fcbf3b 102#endif
63a3cd7a 103
9d8aca48 104 return *zBuf ? true : false;
0e320a79
DW
105}
106
107// Get user ID e.g. jacs
6670f564 108bool wxGetUserId(wxChar* zBuf, int nType)
0e320a79 109{
6670f564 110#if defined(__VISAGECPP__) || defined(__WATCOMC__)
2bd5bbc9 111 // UPM procs return 0 on success
6670f564 112 long lrc = U32ELOCU((PUCHAR)zBuf, (PULONG)&nType);
9d8aca48 113 if (lrc == 0) return true;
06298235 114#endif
9d8aca48 115 return false;
0e320a79
DW
116}
117
6670f564 118bool wxGetUserName( wxChar* zBuf, int nMaxSize )
0e320a79 119{
45fcbf3b 120#ifdef USE_NET_API
6670f564 121 wxGetUserId( zBuf, nMaxSize );
45fcbf3b 122#else
9a83f860 123 wxStrlcpy(zBuf, wxT("Unknown User"), nMaxSize);
45fcbf3b 124#endif
9d8aca48 125 return true;
0e320a79
DW
126}
127
6670f564
WS
128int wxKill(long lPid,
129 wxSignal WXUNUSED(eSig),
130 wxKillError* WXUNUSED(peError),
131 int WXUNUSED(flags))
0e320a79 132{
10e5b930 133 return((int)::DosKillProcess(0, (PID)lPid));
0e320a79
DW
134}
135
136//
137// Execute a program in an Interactive Shell
138//
e4db172a 139bool wxShell( const wxString& rCommand )
0e320a79 140{
9a83f860 141 wxChar* zShell = wxT("CMD.EXE");
e4db172a
WS
142 wxString sInputs;
143 STARTDATA SData = {0};
144 PSZ PgmTitle = "Command Shell";
145 APIRET rc;
146 PID vPid = 0;
147 ULONG ulSessID = 0;
148 UCHAR achObjBuf[256] = {0}; //error data if DosStart fails
149 RESULTCODES vResult;
c5fb56c0
DW
150
151 SData.Length = sizeof(STARTDATA);
152 SData.Related = SSF_RELATED_INDEPENDENT;
153 SData.FgBg = SSF_FGBG_FORE;
154 SData.TraceOpt = SSF_TRACEOPT_NONE;
155 SData.PgmTitle = PgmTitle;
0fba44b4 156 SData.PgmName = (char*)zShell;
c5fb56c0 157
9a83f860 158 sInputs = wxT("/C ") + rCommand;
404aba09 159 SData.PgmInputs = (BYTE*)sInputs.wx_str();
c5fb56c0
DW
160 SData.TermQ = 0;
161 SData.Environment = 0;
162 SData.InheritOpt = SSF_INHERTOPT_SHELL;
163 SData.SessionType = SSF_TYPE_WINDOWABLEVIO;
164 SData.IconFile = 0;
165 SData.PgmHandle = 0;
166 SData.PgmControl = SSF_CONTROL_VISIBLE | SSF_CONTROL_MAXIMIZE;
167 SData.InitXPos = 30;
168 SData.InitYPos = 40;
169 SData.InitXSize = 200;
170 SData.InitYSize = 140;
171 SData.Reserved = 0;
172 SData.ObjectBuffer = (char*)achObjBuf;
173 SData.ObjectBuffLen = (ULONG)sizeof(achObjBuf);
174
175 rc = ::DosStartSession(&SData, &ulSessID, &vPid);
9ac6ff7b 176 if (rc == 0 || rc == 457) // NO_ERROR or SMG_START_IN_BACKGROUND
c5fb56c0
DW
177 {
178 PTIB ptib;
179 PPIB ppib;
10e5b930 180
c5fb56c0 181 ::DosGetInfoBlocks(&ptib, &ppib);
10e5b930 182
c5fb56c0
DW
183 ::DosWaitChild( DCWA_PROCESS
184 ,DCWW_WAIT
185 ,&vResult
186 ,&ppib->pib_ulpid
187 ,vPid
188 );
189 }
190 return (rc != 0);
0e320a79
DW
191}
192
a23692f0 193// Shutdown or reboot the PC
6670f564 194bool wxShutdown(wxShutdownFlags WXUNUSED(wFlags))
f6ba47d9
VZ
195{
196 // TODO
9d8aca48 197 return false;
f6ba47d9
VZ
198}
199
0e320a79 200// Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
9d8aca48 201wxMemorySize wxGetFreeMemory()
0e320a79 202{
5a23a495
WS
203 void* pMemptr = NULL;
204 LONG lSize;
205 ULONG lMemFlags;
206 APIRET rc;
78d50441
DW
207
208 lMemFlags = PAG_FREE;
0d598bae 209 rc = ::DosQueryMem(pMemptr, (PULONG)&lSize, &lMemFlags);
78d50441 210 if (rc != 0)
9d8aca48
WS
211 lSize = -1L;
212 return (wxMemorySize)lSize;
45fcbf3b
DW
213}
214
264f00e7
SN
215// Get Process ID
216unsigned long wxGetProcessId()
217{
218 return (unsigned long)getpid();
219}
220
8269a903
SN
221// ----------------------------------------------------------------------------
222// env vars
223// ----------------------------------------------------------------------------
224
225bool wxGetEnv(const wxString& var, wxString *value)
226{
227 // wxGetenv is defined as getenv()
ce903b6f 228 wxChar *p = wxGetenv((const wxChar *)var);
8269a903 229 if ( !p )
9d8aca48 230 return false;
8269a903
SN
231
232 if ( value )
233 {
234 *value = p;
235 }
236
9d8aca48 237 return true;
8269a903
SN
238}
239
ce903b6f 240static bool wxDoSetEnv(const wxString& variable, const char *value)
8269a903
SN
241{
242#if defined(HAVE_SETENV)
03647350
VZ
243 if ( !value )
244 {
245#ifdef HAVE_UNSETENV
246 return unsetenv(variable.mb_str()) == 0;
247#else
248 value = wxT(""); // mustn't pass NULL to setenv()
249#endif
250 }
90a77e64 251 return setenv(variable.mb_str(), value, 1 /* overwrite */) == 0;
8269a903
SN
252#elif defined(HAVE_PUTENV)
253 wxString s = variable;
254 if ( value )
9a83f860 255 s << wxT('=') << value;
8269a903
SN
256
257 // transform to ANSI
258 const char *p = s.mb_str();
259
260 // the string will be free()d by libc
261 char *buf = (char *)malloc(strlen(p) + 1);
262 strcpy(buf, p);
263
264 return putenv(buf) == 0;
265#else // no way to set an env var
6670f564
WS
266 wxUnusedVar(variable);
267 wxUnusedVar(value);
9d8aca48 268 return false;
8269a903
SN
269#endif
270}
271
90a77e64
VS
272bool wxSetEnv(const wxString& variable, const wxString& value)
273{
274 return wxDoSetEnv(variable, value.mb_str());
275}
276
277bool wxUnsetEnv(const wxString& variable)
278{
279 return wxDoSetEnv(variable, NULL);
280}
281
d3a919bd 282void wxMilliSleep(
10e5b930
DW
283 unsigned long ulMilliseconds
284)
45fcbf3b 285{
f53a0d32 286 ::DosSleep(ulMilliseconds);
0e320a79
DW
287}
288
d3a919bd
SN
289void wxMicroSleep(
290 unsigned long ulMicroseconds
291)
292{
293 ::DosSleep(ulMicroseconds/1000);
294}
295
10e5b930
DW
296void wxSleep(
297 int nSecs
298)
0e320a79 299{
10e5b930 300 ::DosSleep(1000 * nSecs);
0e320a79
DW
301}
302
303// Consume all events until no more left
304void wxFlushEvents()
305{
45fcbf3b 306// wxYield();
0e320a79
DW
307}
308
0e320a79
DW
309// Emit a beeeeeep
310void wxBell()
311{
45fcbf3b 312 DosBeep(1000,1000); // 1kHz during 1 sec.
0e320a79 313}
621ccd8a 314
5a23a495
WS
315wxString wxGetOsDescription()
316{
9a83f860 317 wxString strVer(wxT("OS/2"));
930a9efa 318 ULONG ulSysInfo = 0;
5a23a495 319
930a9efa
SN
320 if (::DosQuerySysInfo( QSV_VERSION_MINOR,
321 QSV_VERSION_MINOR,
322 (PVOID)&ulSysInfo,
323 sizeof(ULONG)
5a23a495
WS
324 ) == 0L )
325 {
326 wxString ver;
9a83f860 327 ver.Printf( wxT(" ver. %d.%d"),
930a9efa
SN
328 int(ulSysInfo / 10),
329 int(ulSysInfo % 10)
5a23a495
WS
330 );
331 strVer += ver;
332 }
333
334 return strVer;
335}
8329e6c7 336
4cc49137
SN
337bool wxIsPlatform64Bit()
338{
339 // FIXME: No idea how to test for 64 bit processor
340 // (Probably irrelevant anyhow, though).
341 return false;
342}
343
6670f564 344void wxAppTraits::InitializeGui(unsigned long &WXUNUSED(ulHab))
8329e6c7
SN
345{
346}
347
6670f564 348void wxAppTraits::TerminateGui(unsigned long WXUNUSED(ulHab))
8329e6c7
SN
349{
350}
351
8bb6b2c0 352wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin)
10e5b930 353{
930a9efa 354 ULONG ulSysInfo = 0;
9d8aca48 355 APIRET ulrc;
5d4b632b 356
930a9efa
SN
357 ulrc = ::DosQuerySysInfo( QSV_VERSION_MINOR,
358 QSV_VERSION_MINOR,
359 (PVOID)&ulSysInfo,
360 sizeof(ULONG)
5d4b632b 361 );
8bb6b2c0 362
5d4b632b 363 if (ulrc == 0L)
10e5b930 364 {
8bb6b2c0 365 if ( verMaj )
930a9efa 366 *verMaj = ulSysInfo / 10;
8bb6b2c0 367 if ( verMin )
930a9efa 368 *verMin = ulSysInfo % 10;
10e5b930 369 }
8bb6b2c0
VZ
370
371 return wxOS_OS2;
0e320a79 372}
621ccd8a 373
8bb6b2c0 374
45fcbf3b 375// ---------------------------------------------------------------------------
1be7f92a
DW
376const wxChar* wxGetHomeDir(
377 wxString* pStr
378)
45fcbf3b 379{
1be7f92a 380 wxString& rStrDir = *pStr;
0e320a79 381
1be7f92a 382 // OS/2 has no idea about home,
b08c90ca
SN
383 // so use the working directory instead.
384 // However, we might have a valid HOME directory,
385 // as is used on many machines that have unix utilities
386 // on them, so we should use that, if available.
45fcbf3b 387
1be7f92a 388 // 256 was taken from os2def.h
45fcbf3b
DW
389#ifndef MAX_PATH
390# define MAX_PATH 256
391#endif
392
0fba44b4 393 const wxChar *szHome = wxGetenv((wxChar*)"HOME");
b08c90ca
SN
394 if ( szHome == NULL ) {
395 // we're homeless, use current directory.
396 rStrDir = wxT(".");
397 }
398 else
399 rStrDir = szHome;
45fcbf3b 400
1be7f92a 401 return rStrDir.c_str();
45fcbf3b
DW
402}
403
14d63513 404wxString wxGetUserHome ( const wxString &rUser )
1be7f92a 405{
5a23a495 406 wxChar* zHome;
14d63513
VZ
407 wxString sUser(rUser);
408
409 wxString home;
1be7f92a 410
06298235 411#ifndef __EMX__
14d63513 412 if (!sUser.empty())
45fcbf3b 413 {
14d63513 414 const wxString currentUser = wxGetUserId();
1be7f92a 415
14d63513
VZ
416 // Guests belong in the temp dir
417 if ( currentUser == "annonymous" )
1be7f92a 418 {
9a83f860 419 zHome = wxGetenv(wxT("TMP"));
14d63513 420 if ( !zHome )
9a83f860 421 zHome = wxGetenv(wxT("TMPDIR"));
14d63513 422 if ( !zHome )
9a83f860 423 zHome = wxGetenv(wxT("TEMP"));
14d63513
VZ
424
425 if ( zHome && *zHome )
426 return zHome;
1be7f92a 427 }
14d63513
VZ
428
429 if ( sUser == currentUser )
430 sUser.clear();
45fcbf3b 431 }
06298235 432#endif
14d63513 433 if (sUser.empty())
13a4ea8d 434 {
9a83f860 435 if ((zHome = wxGetenv(wxT("HOME"))) != NULL)
1be7f92a 436 {
14d63513
VZ
437 home = zHome;
438 home.Replace("/", "\\");
1be7f92a 439 }
13a4ea8d 440 }
14d63513
VZ
441
442 return home;
0e320a79
DW
443}
444
783388d5
SN
445bool wxGetDiskSpace(const wxString& path,
446 wxDiskspaceSize_t *pTotal,
447 wxDiskspaceSize_t *pFree)
448{
449 if (path.empty())
450 return false;
451
452 wxFileName fn(path);
453 FSALLOCATE fsaBuf = {0};
454 APIRET rc = NO_ERROR;
455 ULONG disknum = 0;
456
457 fn.MakeAbsolute();
458
459 if (wxDirExists(fn.GetFullPath()) == false)
460 return false;
461
9a83f860 462 disknum = wxToupper(fn.GetVolume().GetChar(0)) - wxT('A') + 1;
783388d5
SN
463
464 rc = ::DosQueryFSInfo(disknum, // 1 = A, 2 = B, 3 = C, ...
465 FSIL_ALLOC, // allocation info
466 (PVOID)&fsaBuf,
467 sizeof(FSALLOCATE));
468
469 if (rc != NO_ERROR)
470 return false;
471 else
472 {
473 if(pTotal)
474 {
475 // to try to avoid 32-bit overflow, let's not multiply right away
476 // (num of alloc units)
03647350 477 *pTotal = fsaBuf.cUnit;
783388d5
SN
478 // * (num of sectors per alloc unit) * (num of bytes per sector)
479 (*pTotal) *= fsaBuf.cSectorUnit * fsaBuf.cbSector;
480 }
481 if(pFree)
482 {
483 *pFree = fsaBuf.cUnitAvail;
484 (*pFree) *= fsaBuf.cSectorUnit * fsaBuf.cbSector;
485 }
486 return true;
487 }
488}
03647350 489
0e2a14e5 490wxString wxPMErrorToStr(ERRORID vError)
914589c2 491{
0e2a14e5 492 wxString sError;
914589c2
DW
493
494 //
495 // Remove the high order byte -- it is useless
496 //
497 vError &= 0x0000ffff;
498 switch(vError)
499 {
500 case PMERR_INVALID_HWND:
501 sError = wxT("Invalid window handle specified");
502 break;
503
504 case PMERR_INVALID_FLAG:
505 sError = wxT("Invalid flag bit set");
506 break;
507
508 case PMERR_NO_MSG_QUEUE:
509 sError = wxT("No message queue available");
510 break;
511
512 case PMERR_INVALID_PARM:
513 sError = wxT("Parameter contained invalid data");
514 break;
515
516 case PMERR_INVALID_PARAMETERS:
517 sError = wxT("Parameter value is out of range");
518 break;
519
520 case PMERR_PARAMETER_OUT_OF_RANGE:
521 sError = wxT("Parameter value is out of range");
522 break;
523
524 case PMERR_INVALID_INTEGER_ATOM:
525 sError = wxT("Not a valid atom");
526 break;
527
528 case PMERR_INVALID_HATOMTBL:
529 sError = wxT("Atom table handle is invalid");
530 break;
531
532 case PMERR_INVALID_ATOM_NAME:
533 sError = wxT("Not a valid atom name");
534 break;
535
536 case PMERR_ATOM_NAME_NOT_FOUND:
537 sError = wxT("Valid name format, but cannot find name in atom table");
538 break;
539
da51aebe
SN
540 case PMERR_INV_HPS:
541 sError = wxT("PMERR_INV_HPS");
542 break;
543
544 case PMERR_PS_BUSY:
545 sError = wxT("PMERR_PS_BUSY");
546 break;
547
548 case PMERR_INV_PRIMITIVE_TYPE:
549 sError = wxT("PMERR_INV_PRIMITIVE_TYPE");
550 break;
551
552 case PMERR_UNSUPPORTED_ATTR:
553 sError = wxT("PMERR_UNSUPPORTED_ATTR");
554 break;
555
556 case PMERR_INV_COLOR_ATTR:
557 sError = wxT("PMERR_INV_COLOR_ATTR");
558 break;
559
560 case PMERR_INV_BACKGROUND_COL_ATTR:
561 sError = wxT("PMERR_INV_BACKGROUND_COL_ATTR");
562 break;
563
564 case PMERR_INV_MIX_ATTR:
565 sError = wxT("PMERR_INV_MIX_ATTR");
566 break;
567
568 case PMERR_INV_LINE_WIDTH_ATTR:
569 sError = wxT("PMERR_INV_LINE_WIDTH_ATTR");
570 break;
571
572 case PMERR_INV_GEOM_LINE_WIDTH_ATTR:
573 sError = wxT("PMERR_INV_GEOM_LINE_WIDTH_ATTR");
574 break;
575
576 case PMERR_INV_LINE_TYPE_ATTR:
577 sError = wxT("PMERR_INV_LINE_TYPE_ATTR");
578 break;
579
580 case PMERR_INV_LINE_END_ATTR:
581 sError = wxT("PMERR_INV_LINE_END_ATTR");
582 break;
583
584 case PMERR_INV_LINE_JOIN_ATTR:
585 sError = wxT("PMERR_INV_LINE_JOIN_ATTR");
586 break;
587
588 case PMERR_INV_CHAR_SET_ATTR:
589 sError = wxT("PMERR_INV_CHAR_SET_ATTR");
590 break;
591
592 case PMERR_INV_CHAR_MODE_ATTR:
593 sError = wxT("PMERR_INV_CHAR_MODE_ATTR");
594 break;
595
596 case PMERR_INV_CHAR_DIRECTION_ATTR:
597 sError = wxT("PMERR_INV_CHAR_DIRECTION_ATTR");
598 break;
599
600 case PMERR_INV_CHAR_SHEAR_ATTR:
601 sError = wxT("PMERR_INV_CHAR_SHEAR_ATTR");
602 break;
603
604 case PMERR_INV_CHAR_ANGLE_ATTR:
605 sError = wxT("PMERR_INV_CHAR_ANGLE_ATTR");
606 break;
607
608 case PMERR_INV_MARKER_SET_ATTR:
609 sError = wxT("PMERR_INV_MARKER_SET_ATTR");
610 break;
611
612 case PMERR_INV_MARKER_SYMBOL_ATTR:
613 sError = wxT("PMERR_INV_MARKER_SYMBOL_ATTR");
614 break;
615
616 case PMERR_INV_PATTERN_SET_ATTR:
617 sError = wxT("PMERR_INV_PATTERN_SET_ATTR");
618 break;
619
620 case PMERR_INV_PATTERN_ATTR:
621 sError = wxT("PMERR_INV_PATTERN_ATTR");
622 break;
623
624 case PMERR_INV_COORDINATE:
625 sError = wxT("PMERR_INV_COORDINATE");
626 break;
627
628 case PMERR_UNSUPPORTED_ATTR_VALUE:
629 sError = wxT("PMERR_UNSUPPORTED_ATTR_VALUE");
630 break;
631
632 case PMERR_INV_PATTERN_SET_FONT:
633 sError = wxT("PMERR_INV_PATTERN_SET_FONT");
634 break;
635
636 case PMERR_HUGE_FONTS_NOT_SUPPORTED:
637 sError = wxT("PMERR_HUGE_FONTS_NOT_SUPPORTED");
638 break;
639
914589c2
DW
640 default:
641 sError = wxT("Unknown error");
642 }
0e2a14e5 643 return sError;
914589c2 644} // end of wxPMErrorToStr
621ccd8a
SN
645
646// replacement for implementation in unix/utilsunx.cpp,
647// to be used by all X11 based ports.
648struct wxEndProcessData;
649
6670f564 650void wxHandleProcessTermination(wxEndProcessData *WXUNUSED(proc_data))
621ccd8a
SN
651{
652 // For now, just do nothing. To be filled in as needed.
653}