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