]> git.saurik.com Git - wxWidgets.git/blob - src/os2/utils.cpp
Remove DoSetSizeHints() call from Create()
[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 // Copyright: (c) David Webster
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #include "wx/utils.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/intl.h"
18 #include "wx/log.h"
19 #endif //WX_PRECOMP
20
21 #include "wx/os2/private.h"
22 #include "wx/apptrait.h"
23 #include "wx/filename.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[] = wxT("wxWidgets");
53 static const wxChar eHOSTNAME[] = wxT("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 = wxT("noname");
84
85 if ((zSysname = wxGetenv(wxT("SYSTEM_NAME"))) == NULL &&
86 (zSysname = wxGetenv(wxT("HOSTNAME"))) == NULL)
87 {
88 ::PrfQueryProfileString( HINI_PROFILE
89 ,(PSZ)WX_SECTION
90 ,(PSZ)eHOSTNAME
91 ,(PSZ)zDefaultHost
92 ,(void*)zBuf
93 ,(ULONG)nMaxSize - 1
94 );
95 zBuf[nMaxSize] = wxT('\0');
96 }
97 else
98 {
99 wxStrlcpy(zBuf, zSysname, nMaxSize);
100 }
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 wxStrlcpy(zBuf, wxT("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 = wxT("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 = wxT("/C ") + rCommand;
158 SData.PgmInputs = (BYTE*)sInputs.wx_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((const wxChar *)var);
228 if ( !p )
229 return false;
230
231 if ( value )
232 {
233 *value = p;
234 }
235
236 return true;
237 }
238
239 static bool wxDoSetEnv(const wxString& variable, const char *value)
240 {
241 #if defined(HAVE_SETENV)
242 if ( !value )
243 {
244 #ifdef HAVE_UNSETENV
245 return unsetenv(variable.mb_str()) == 0;
246 #else
247 value = wxT(""); // mustn't pass NULL to setenv()
248 #endif
249 }
250 return setenv(variable.mb_str(), value, 1 /* overwrite */) == 0;
251 #elif defined(HAVE_PUTENV)
252 wxString s = variable;
253 if ( value )
254 s << wxT('=') << value;
255
256 // transform to ANSI
257 const char *p = s.mb_str();
258
259 // the string will be free()d by libc
260 char *buf = (char *)malloc(strlen(p) + 1);
261 strcpy(buf, p);
262
263 return putenv(buf) == 0;
264 #else // no way to set an env var
265 wxUnusedVar(variable);
266 wxUnusedVar(value);
267 return false;
268 #endif
269 }
270
271 bool wxSetEnv(const wxString& variable, const wxString& value)
272 {
273 return wxDoSetEnv(variable, value.mb_str());
274 }
275
276 bool wxUnsetEnv(const wxString& variable)
277 {
278 return wxDoSetEnv(variable, NULL);
279 }
280
281 void wxMilliSleep(
282 unsigned long ulMilliseconds
283 )
284 {
285 ::DosSleep(ulMilliseconds);
286 }
287
288 void wxMicroSleep(
289 unsigned long ulMicroseconds
290 )
291 {
292 ::DosSleep(ulMicroseconds/1000);
293 }
294
295 void wxSleep(
296 int nSecs
297 )
298 {
299 ::DosSleep(1000 * nSecs);
300 }
301
302 // Consume all events until no more left
303 void wxFlushEvents()
304 {
305 // wxYield();
306 }
307
308 wxString wxGetOsDescription()
309 {
310 wxString strVer(wxT("OS/2"));
311 ULONG ulSysInfo = 0;
312
313 if (::DosQuerySysInfo( QSV_VERSION_MINOR,
314 QSV_VERSION_MINOR,
315 (PVOID)&ulSysInfo,
316 sizeof(ULONG)
317 ) == 0L )
318 {
319 wxString ver;
320 ver.Printf( wxT(" ver. %d.%d"),
321 int(ulSysInfo / 10),
322 int(ulSysInfo % 10)
323 );
324 strVer += ver;
325 }
326
327 return strVer;
328 }
329
330 bool wxIsPlatform64Bit()
331 {
332 // FIXME: No idea how to test for 64 bit processor
333 // (Probably irrelevant anyhow, though).
334 return false;
335 }
336
337 void wxAppTraits::InitializeGui(unsigned long &WXUNUSED(ulHab))
338 {
339 }
340
341 void wxAppTraits::TerminateGui(unsigned long WXUNUSED(ulHab))
342 {
343 }
344
345 wxOperatingSystemId wxGetOsVersion(int *verMaj, int *verMin)
346 {
347 ULONG ulSysInfo = 0;
348 APIRET ulrc;
349
350 ulrc = ::DosQuerySysInfo( QSV_VERSION_MINOR,
351 QSV_VERSION_MINOR,
352 (PVOID)&ulSysInfo,
353 sizeof(ULONG)
354 );
355
356 if (ulrc == 0L)
357 {
358 if ( verMaj )
359 *verMaj = ulSysInfo / 10;
360 if ( verMin )
361 *verMin = ulSysInfo % 10;
362 }
363
364 return wxOS_OS2;
365 }
366
367
368 // ---------------------------------------------------------------------------
369 const wxChar* wxGetHomeDir(
370 wxString* pStr
371 )
372 {
373 wxString& rStrDir = *pStr;
374
375 // OS/2 has no idea about home,
376 // so use the working directory instead.
377 // However, we might have a valid HOME directory,
378 // as is used on many machines that have unix utilities
379 // on them, so we should use that, if available.
380
381 // 256 was taken from os2def.h
382 #ifndef MAX_PATH
383 # define MAX_PATH 256
384 #endif
385
386 const wxChar *szHome = wxGetenv((wxChar*)"HOME");
387 if ( szHome == NULL ) {
388 // we're homeless, use current directory.
389 rStrDir = wxT(".");
390 }
391 else
392 rStrDir = szHome;
393
394 return rStrDir.c_str();
395 }
396
397 wxString wxGetUserHome ( const wxString &rUser )
398 {
399 wxChar* zHome;
400 wxString sUser(rUser);
401
402 wxString home;
403
404 #ifndef __EMX__
405 if (!sUser.empty())
406 {
407 const wxString currentUser = wxGetUserId();
408
409 // Guests belong in the temp dir
410 if ( currentUser == "annonymous" )
411 {
412 zHome = wxGetenv(wxT("TMP"));
413 if ( !zHome )
414 zHome = wxGetenv(wxT("TMPDIR"));
415 if ( !zHome )
416 zHome = wxGetenv(wxT("TEMP"));
417
418 if ( zHome && *zHome )
419 return zHome;
420 }
421
422 if ( sUser == currentUser )
423 sUser.clear();
424 }
425 #endif
426 if (sUser.empty())
427 {
428 if ((zHome = wxGetenv(wxT("HOME"))) != NULL)
429 {
430 home = zHome;
431 home.Replace("/", "\\");
432 }
433 }
434
435 return home;
436 }
437
438 bool wxGetDiskSpace(const wxString& path,
439 wxDiskspaceSize_t *pTotal,
440 wxDiskspaceSize_t *pFree)
441 {
442 if (path.empty())
443 return false;
444
445 wxFileName fn(path);
446 FSALLOCATE fsaBuf = {0};
447 APIRET rc = NO_ERROR;
448 ULONG disknum = 0;
449
450 fn.MakeAbsolute();
451
452 if (wxDirExists(fn.GetFullPath()) == false)
453 return false;
454
455 disknum = wxToupper(fn.GetVolume().GetChar(0)) - wxT('A') + 1;
456
457 rc = ::DosQueryFSInfo(disknum, // 1 = A, 2 = B, 3 = C, ...
458 FSIL_ALLOC, // allocation info
459 (PVOID)&fsaBuf,
460 sizeof(FSALLOCATE));
461
462 if (rc != NO_ERROR)
463 return false;
464 else
465 {
466 if(pTotal)
467 {
468 // to try to avoid 32-bit overflow, let's not multiply right away
469 // (num of alloc units)
470 *pTotal = fsaBuf.cUnit;
471 // * (num of sectors per alloc unit) * (num of bytes per sector)
472 (*pTotal) *= fsaBuf.cSectorUnit * fsaBuf.cbSector;
473 }
474 if(pFree)
475 {
476 *pFree = fsaBuf.cUnitAvail;
477 (*pFree) *= fsaBuf.cSectorUnit * fsaBuf.cbSector;
478 }
479 return true;
480 }
481 }
482
483 wxString wxPMErrorToStr(ERRORID vError)
484 {
485 wxString sError;
486
487 //
488 // Remove the high order byte -- it is useless
489 //
490 vError &= 0x0000ffff;
491 switch(vError)
492 {
493 case PMERR_INVALID_HWND:
494 sError = wxT("Invalid window handle specified");
495 break;
496
497 case PMERR_INVALID_FLAG:
498 sError = wxT("Invalid flag bit set");
499 break;
500
501 case PMERR_NO_MSG_QUEUE:
502 sError = wxT("No message queue available");
503 break;
504
505 case PMERR_INVALID_PARM:
506 sError = wxT("Parameter contained invalid data");
507 break;
508
509 case PMERR_INVALID_PARAMETERS:
510 sError = wxT("Parameter value is out of range");
511 break;
512
513 case PMERR_PARAMETER_OUT_OF_RANGE:
514 sError = wxT("Parameter value is out of range");
515 break;
516
517 case PMERR_INVALID_INTEGER_ATOM:
518 sError = wxT("Not a valid atom");
519 break;
520
521 case PMERR_INVALID_HATOMTBL:
522 sError = wxT("Atom table handle is invalid");
523 break;
524
525 case PMERR_INVALID_ATOM_NAME:
526 sError = wxT("Not a valid atom name");
527 break;
528
529 case PMERR_ATOM_NAME_NOT_FOUND:
530 sError = wxT("Valid name format, but cannot find name in atom table");
531 break;
532
533 case PMERR_INV_HPS:
534 sError = wxT("PMERR_INV_HPS");
535 break;
536
537 case PMERR_PS_BUSY:
538 sError = wxT("PMERR_PS_BUSY");
539 break;
540
541 case PMERR_INV_PRIMITIVE_TYPE:
542 sError = wxT("PMERR_INV_PRIMITIVE_TYPE");
543 break;
544
545 case PMERR_UNSUPPORTED_ATTR:
546 sError = wxT("PMERR_UNSUPPORTED_ATTR");
547 break;
548
549 case PMERR_INV_COLOR_ATTR:
550 sError = wxT("PMERR_INV_COLOR_ATTR");
551 break;
552
553 case PMERR_INV_BACKGROUND_COL_ATTR:
554 sError = wxT("PMERR_INV_BACKGROUND_COL_ATTR");
555 break;
556
557 case PMERR_INV_MIX_ATTR:
558 sError = wxT("PMERR_INV_MIX_ATTR");
559 break;
560
561 case PMERR_INV_LINE_WIDTH_ATTR:
562 sError = wxT("PMERR_INV_LINE_WIDTH_ATTR");
563 break;
564
565 case PMERR_INV_GEOM_LINE_WIDTH_ATTR:
566 sError = wxT("PMERR_INV_GEOM_LINE_WIDTH_ATTR");
567 break;
568
569 case PMERR_INV_LINE_TYPE_ATTR:
570 sError = wxT("PMERR_INV_LINE_TYPE_ATTR");
571 break;
572
573 case PMERR_INV_LINE_END_ATTR:
574 sError = wxT("PMERR_INV_LINE_END_ATTR");
575 break;
576
577 case PMERR_INV_LINE_JOIN_ATTR:
578 sError = wxT("PMERR_INV_LINE_JOIN_ATTR");
579 break;
580
581 case PMERR_INV_CHAR_SET_ATTR:
582 sError = wxT("PMERR_INV_CHAR_SET_ATTR");
583 break;
584
585 case PMERR_INV_CHAR_MODE_ATTR:
586 sError = wxT("PMERR_INV_CHAR_MODE_ATTR");
587 break;
588
589 case PMERR_INV_CHAR_DIRECTION_ATTR:
590 sError = wxT("PMERR_INV_CHAR_DIRECTION_ATTR");
591 break;
592
593 case PMERR_INV_CHAR_SHEAR_ATTR:
594 sError = wxT("PMERR_INV_CHAR_SHEAR_ATTR");
595 break;
596
597 case PMERR_INV_CHAR_ANGLE_ATTR:
598 sError = wxT("PMERR_INV_CHAR_ANGLE_ATTR");
599 break;
600
601 case PMERR_INV_MARKER_SET_ATTR:
602 sError = wxT("PMERR_INV_MARKER_SET_ATTR");
603 break;
604
605 case PMERR_INV_MARKER_SYMBOL_ATTR:
606 sError = wxT("PMERR_INV_MARKER_SYMBOL_ATTR");
607 break;
608
609 case PMERR_INV_PATTERN_SET_ATTR:
610 sError = wxT("PMERR_INV_PATTERN_SET_ATTR");
611 break;
612
613 case PMERR_INV_PATTERN_ATTR:
614 sError = wxT("PMERR_INV_PATTERN_ATTR");
615 break;
616
617 case PMERR_INV_COORDINATE:
618 sError = wxT("PMERR_INV_COORDINATE");
619 break;
620
621 case PMERR_UNSUPPORTED_ATTR_VALUE:
622 sError = wxT("PMERR_UNSUPPORTED_ATTR_VALUE");
623 break;
624
625 case PMERR_INV_PATTERN_SET_FONT:
626 sError = wxT("PMERR_INV_PATTERN_SET_FONT");
627 break;
628
629 case PMERR_HUGE_FONTS_NOT_SUPPORTED:
630 sError = wxT("PMERR_HUGE_FONTS_NOT_SUPPORTED");
631 break;
632
633 default:
634 sError = wxT("Unknown error");
635 }
636 return sError;
637 } // end of wxPMErrorToStr