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