]> git.saurik.com Git - wxWidgets.git/blame - src/os2/utils.cpp
don't crash in UnselectAll() if the tree has no root
[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{
19193a2c 108#if defined(__VISAGECPP__)
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{
19193a2c 753#if 0
1be7f92a
DW
754 HPS hpsScreen;
755 HDC hdcScreen;
756 LONG lColors;
757
758 hpsScreen = ::WinGetScreenPS(HWND_DESKTOP);
759 hdcScreen = ::GpiQueryDevice(hpsScreen);
760 ::DevQueryCaps(hdcScreen, CAPS_COLORS, 1L, &lColors);
761 return(lColors > 1L);
19193a2c
KB
762#else
763 // I don't see how the PM display could not be color. Besides, this
764 // was leaking DCs and PSs!!! MN
987da0d4 765 return TRUE;
19193a2c 766#endif
0e320a79
DW
767}
768
769// Returns depth of screen
770int wxDisplayDepth()
771{
1be7f92a
DW
772 HPS hpsScreen;
773 HDC hdcScreen;
774 LONG lPlanes;
775 LONG lBitsPerPixel;
19193a2c
KB
776 static LONG nDepth = 0;
777
778 // The screen colordepth ain't gonna change. No reason to query
779 // it over and over!
780 if (!nDepth) {
781 hpsScreen = ::WinGetScreenPS(HWND_DESKTOP);
782 hdcScreen = ::GpiQueryDevice(hpsScreen);
783 ::DevQueryCaps(hdcScreen, CAPS_COLOR_PLANES, 1L, &lPlanes);
784 ::DevQueryCaps(hdcScreen, CAPS_COLOR_BITCOUNT, 1L, &lBitsPerPixel);
785
786 nDepth = (int)(lPlanes * lBitsPerPixel);
787 ::DevCloseDC(hdcScreen);
788 ::WinReleasePS(hpsScreen);
789 }
d90895ac 790 return (nDepth);
0e320a79
DW
791}
792
793// Get size of display
1be7f92a
DW
794void wxDisplaySize(
795 int* pWidth
796, int* pHeight
797)
0e320a79 798{
1be7f92a
DW
799 HPS hpsScreen;
800 HDC hdcScreen;
19193a2c
KB
801 static LONG lWidth = 0;
802 static LONG lHeight = 0;
803
804 // The screen size ain't gonna change either so just cache the values
805 if (!lWidth) {
806 hpsScreen = ::WinGetScreenPS(HWND_DESKTOP);
807 hdcScreen = ::GpiQueryDevice(hpsScreen);
808 ::DevQueryCaps(hdcScreen, CAPS_WIDTH, 1L, &lWidth);
809 ::DevQueryCaps(hdcScreen, CAPS_HEIGHT, 1L, &lHeight);
810 ::DevCloseDC(hdcScreen);
811 ::WinReleasePS(hpsScreen);
812 }
892b89f3
DW
813 *pWidth = (int)lWidth;
814 *pHeight = (int)lHeight;
45fcbf3b
DW
815}
816
5b3ed311
DW
817void wxDisplaySizeMM(
818 int* pWidth
819, int* pHeight
820)
821{
822 HPS hpsScreen;
823 HDC hdcScreen;
824
825 hpsScreen = ::WinGetScreenPS(HWND_DESKTOP);
826 hdcScreen = ::GpiQueryDevice(hpsScreen);
827
828 if (pWidth)
829 ::DevQueryCaps( hdcScreen
830 ,CAPS_HORIZONTAL_RESOLUTION
831 ,1L
832 ,(PLONG)pWidth
833 );
834 if (pHeight)
835 ::DevQueryCaps( hdcScreen
836 ,CAPS_VERTICAL_RESOLUTION
837 ,1L
838 ,(PLONG)pHeight
839 );
19193a2c
KB
840 ::DevCloseDC(hdcScreen);
841 ::WinReleasePS(hpsScreen);
5b3ed311
DW
842}
843
ec5d7799
RD
844void wxClientDisplayRect(int *x, int *y, int *width, int *height)
845{
846 // This is supposed to return desktop dimensions minus any window
847 // manager panels, menus, taskbars, etc. If there is a way to do that
848 // for this platform please fix this function, otherwise it defaults
849 // to the entire desktop.
850 if (x) *x = 0;
851 if (y) *y = 0;
852 wxDisplaySize(width, height);
853}
854
855
1be7f92a
DW
856bool wxDirExists(
857 const wxString& rDir
858)
45fcbf3b 859{
1be7f92a 860 return (::DosSetCurrentDir(WXSTRINGCAST rDir));
45fcbf3b
DW
861}
862
863// ---------------------------------------------------------------------------
864// window information functions
865// ---------------------------------------------------------------------------
866
1be7f92a
DW
867wxString WXDLLEXPORT wxGetWindowText(
868 WXHWND hWnd
869)
45fcbf3b 870{
1be7f92a
DW
871 wxString vStr;
872 long lLen = ::WinQueryWindowTextLength((HWND)hWnd) + 1;
873
874 ::WinQueryWindowText((HWND)hWnd, lLen, vStr.GetWriteBuf((int)lLen));
875 vStr.UngetWriteBuf();
45fcbf3b 876
1be7f92a 877 return vStr;
45fcbf3b
DW
878}
879
1be7f92a
DW
880wxString WXDLLEXPORT wxGetWindowClass(
881 WXHWND hWnd
882)
45fcbf3b 883{
1be7f92a
DW
884 wxString vStr;
885 int nLen = 256; // some starting value
45fcbf3b
DW
886
887 for ( ;; )
888 {
1be7f92a 889 int nCount = ::WinQueryClassName((HWND)hWnd, nLen, vStr.GetWriteBuf(nLen));
45fcbf3b 890
1be7f92a
DW
891 vStr.UngetWriteBuf();
892 if (nCount == nLen )
45fcbf3b
DW
893 {
894 // the class name might have been truncated, retry with larger
895 // buffer
1be7f92a 896 nLen *= 2;
45fcbf3b
DW
897 }
898 else
899 {
900 break;
901 }
902 }
1be7f92a 903 return vStr;
45fcbf3b
DW
904}
905
1be7f92a
DW
906WXWORD WXDLLEXPORT wxGetWindowId(
907 WXHWND hWnd
908)
45fcbf3b
DW
909{
910 return ::WinQueryWindowUShort((HWND)hWnd, QWS_ID);
0e320a79
DW
911}
912
914589c2
DW
913wxString WXDLLEXPORT wxPMErrorToStr(
914 ERRORID vError
915)
916{
917 wxString sError;
918
919 //
920 // Remove the high order byte -- it is useless
921 //
922 vError &= 0x0000ffff;
923 switch(vError)
924 {
925 case PMERR_INVALID_HWND:
926 sError = wxT("Invalid window handle specified");
927 break;
928
929 case PMERR_INVALID_FLAG:
930 sError = wxT("Invalid flag bit set");
931 break;
932
933 case PMERR_NO_MSG_QUEUE:
934 sError = wxT("No message queue available");
935 break;
936
937 case PMERR_INVALID_PARM:
938 sError = wxT("Parameter contained invalid data");
939 break;
940
941 case PMERR_INVALID_PARAMETERS:
942 sError = wxT("Parameter value is out of range");
943 break;
944
945 case PMERR_PARAMETER_OUT_OF_RANGE:
946 sError = wxT("Parameter value is out of range");
947 break;
948
949 case PMERR_INVALID_INTEGER_ATOM:
950 sError = wxT("Not a valid atom");
951 break;
952
953 case PMERR_INVALID_HATOMTBL:
954 sError = wxT("Atom table handle is invalid");
955 break;
956
957 case PMERR_INVALID_ATOM_NAME:
958 sError = wxT("Not a valid atom name");
959 break;
960
961 case PMERR_ATOM_NAME_NOT_FOUND:
962 sError = wxT("Valid name format, but cannot find name in atom table");
963 break;
964
965 default:
966 sError = wxT("Unknown error");
967 }
968 return(sError);
969} // end of wxPMErrorToStr
970
008089f6
DW
971void wxDrawBorder(
972 HPS hPS
973, RECTL& rRect
974, WXDWORD dwStyle
975)
976{
977 POINTL vPoint[2];
978
979 vPoint[0].x = rRect.xLeft;
980 vPoint[0].y = rRect.yBottom;
981 ::GpiMove(hPS, &vPoint[0]);
982 if (dwStyle & wxSIMPLE_BORDER ||
983 dwStyle & wxSTATIC_BORDER)
984 {
ad7f3189
DW
985 vPoint[1].x = rRect.xRight - 1;
986 vPoint[1].y = rRect.yTop - 1;
008089f6
DW
987 ::GpiBox( hPS
988 ,DRO_OUTLINE
989 ,&vPoint[1]
990 ,0L
991 ,0L
992 );
993 }
994 if (dwStyle & wxSUNKEN_BORDER)
995 {
996 LINEBUNDLE vLineBundle;
997
ad7f3189 998 vLineBundle.lColor = 0x00FFFFFF; // WHITE
008089f6
DW
999 vLineBundle.usMixMode = FM_OVERPAINT;
1000 vLineBundle.fxWidth = 2;
1001 vLineBundle.lGeomWidth = 2;
1002 vLineBundle.usType = LINETYPE_SOLID;
1003 vLineBundle.usEnd = 0;
1004 vLineBundle.usJoin = 0;
1005 ::GpiSetAttrs( hPS
1006 ,PRIM_LINE
1007 ,LBB_COLOR | LBB_MIX_MODE | LBB_WIDTH | LBB_GEOM_WIDTH | LBB_TYPE
1008 ,0L
1009 ,&vLineBundle
1010 );
ad7f3189
DW
1011 vPoint[1].x = rRect.xRight - 1;
1012 vPoint[1].y = rRect.yTop - 1;
008089f6
DW
1013 ::GpiBox( hPS
1014 ,DRO_OUTLINE
1015 ,&vPoint[1]
1016 ,0L
1017 ,0L
1018 );
ad7f3189
DW
1019 vPoint[0].x = rRect.xLeft + 1;
1020 vPoint[0].y = rRect.yBottom + 1;
1021 ::GpiMove(hPS, &vPoint[0]);
1022 vPoint[1].x = rRect.xRight - 2;
1023 vPoint[1].y = rRect.yTop - 2;
1024 ::GpiBox( hPS
1025 ,DRO_OUTLINE
1026 ,&vPoint[1]
1027 ,0L
1028 ,0L
1029 );
1030
008089f6
DW
1031 vLineBundle.lColor = 0x00000000; // BLACK
1032 vLineBundle.usMixMode = FM_OVERPAINT;
1033 vLineBundle.fxWidth = 2;
1034 vLineBundle.lGeomWidth = 2;
1035 vLineBundle.usType = LINETYPE_SOLID;
1036 vLineBundle.usEnd = 0;
1037 vLineBundle.usJoin = 0;
1038 ::GpiSetAttrs( hPS
1039 ,PRIM_LINE
1040 ,LBB_COLOR | LBB_MIX_MODE | LBB_WIDTH | LBB_GEOM_WIDTH | LBB_TYPE
1041 ,0L
1042 ,&vLineBundle
1043 );
1044 vPoint[0].x = rRect.xLeft + 2;
1045 vPoint[0].y = rRect.yBottom + 2;
1046 ::GpiMove(hPS, &vPoint[0]);
1047 vPoint[1].x = rRect.xLeft + 2;
ad7f3189 1048 vPoint[1].y = rRect.yTop - 3;
008089f6 1049 ::GpiLine(hPS, &vPoint[1]);
ad7f3189
DW
1050 vPoint[1].x = rRect.xRight - 3;
1051 vPoint[1].y = rRect.yTop - 3;
1052 ::GpiLine(hPS, &vPoint[1]);
1053
1054 vPoint[0].x = rRect.xLeft + 3;
1055 vPoint[0].y = rRect.yBottom + 3;
1056 ::GpiMove(hPS, &vPoint[0]);
1057 vPoint[1].x = rRect.xLeft + 3;
1058 vPoint[1].y = rRect.yTop - 4;
1059 ::GpiLine(hPS, &vPoint[1]);
1060 vPoint[1].x = rRect.xRight - 4;
1061 vPoint[1].y = rRect.yTop - 4;
008089f6
DW
1062 ::GpiLine(hPS, &vPoint[1]);
1063 }
1064 if (dwStyle & wxDOUBLE_BORDER)
1065 {
ad7f3189
DW
1066 LINEBUNDLE vLineBundle;
1067
1068 vLineBundle.lColor = 0x00FFFFFF; // WHITE
1069 vLineBundle.usMixMode = FM_OVERPAINT;
1070 vLineBundle.fxWidth = 2;
1071 vLineBundle.lGeomWidth = 2;
1072 vLineBundle.usType = LINETYPE_SOLID;
1073 vLineBundle.usEnd = 0;
1074 vLineBundle.usJoin = 0;
1075 ::GpiSetAttrs( hPS
1076 ,PRIM_LINE
1077 ,LBB_COLOR | LBB_MIX_MODE | LBB_WIDTH | LBB_GEOM_WIDTH | LBB_TYPE
1078 ,0L
1079 ,&vLineBundle
1080 );
1081 vPoint[1].x = rRect.xRight - 1;
1082 vPoint[1].y = rRect.yTop - 1;
1083 ::GpiBox( hPS
1084 ,DRO_OUTLINE
1085 ,&vPoint[1]
1086 ,0L
1087 ,0L
1088 );
1089 vLineBundle.lColor = 0x00000000; // WHITE
1090 vLineBundle.usMixMode = FM_OVERPAINT;
1091 vLineBundle.fxWidth = 2;
1092 vLineBundle.lGeomWidth = 2;
1093 vLineBundle.usType = LINETYPE_SOLID;
1094 vLineBundle.usEnd = 0;
1095 vLineBundle.usJoin = 0;
1096 ::GpiSetAttrs( hPS
1097 ,PRIM_LINE
1098 ,LBB_COLOR | LBB_MIX_MODE | LBB_WIDTH | LBB_GEOM_WIDTH | LBB_TYPE
1099 ,0L
1100 ,&vLineBundle
1101 );
1102 vPoint[0].x = rRect.xLeft + 2;
1103 vPoint[0].y = rRect.yBottom + 2;
1104 ::GpiMove(hPS, &vPoint[0]);
1105 vPoint[1].x = rRect.xRight - 2;
1106 vPoint[1].y = rRect.yTop - 2;
1107 ::GpiBox( hPS
1108 ,DRO_OUTLINE
1109 ,&vPoint[1]
1110 ,0L
1111 ,0L
1112 );
1113 vLineBundle.lColor = 0x00FFFFFF; // BLACK
1114 vLineBundle.usMixMode = FM_OVERPAINT;
1115 vLineBundle.fxWidth = 2;
1116 vLineBundle.lGeomWidth = 2;
1117 vLineBundle.usType = LINETYPE_SOLID;
1118 vLineBundle.usEnd = 0;
1119 vLineBundle.usJoin = 0;
1120 ::GpiSetAttrs( hPS
1121 ,PRIM_LINE
1122 ,LBB_COLOR | LBB_MIX_MODE | LBB_WIDTH | LBB_GEOM_WIDTH | LBB_TYPE
1123 ,0L
1124 ,&vLineBundle
1125 );
1126 vPoint[0].x = rRect.xLeft + 3;
1127 vPoint[0].y = rRect.yBottom + 3;
1128 ::GpiMove(hPS, &vPoint[0]);
1129 vPoint[1].x = rRect.xRight - 3;
1130 vPoint[1].y = rRect.yTop - 3;
1131 ::GpiBox( hPS
1132 ,DRO_OUTLINE
1133 ,&vPoint[1]
1134 ,0L
1135 ,0L
1136 );
008089f6
DW
1137 }
1138 if (dwStyle & wxRAISED_BORDER)
1139 {
ad7f3189
DW
1140 LINEBUNDLE vLineBundle;
1141
1142 vLineBundle.lColor = 0x00000000; // BLACK
1143 vLineBundle.usMixMode = FM_OVERPAINT;
1144 vLineBundle.fxWidth = 2;
1145 vLineBundle.lGeomWidth = 2;
1146 vLineBundle.usType = LINETYPE_SOLID;
1147 vLineBundle.usEnd = 0;
1148 vLineBundle.usJoin = 0;
1149 ::GpiSetAttrs( hPS
1150 ,PRIM_LINE
1151 ,LBB_COLOR | LBB_MIX_MODE | LBB_WIDTH | LBB_GEOM_WIDTH | LBB_TYPE
1152 ,0L
1153 ,&vLineBundle
1154 );
1155 vPoint[1].x = rRect.xRight - 1;
1156 vPoint[1].y = rRect.yTop - 1;
1157 ::GpiBox( hPS
1158 ,DRO_OUTLINE
1159 ,&vPoint[1]
1160 ,0L
1161 ,0L
1162 );
1163 vPoint[0].x = rRect.xLeft + 1;
1164 vPoint[0].y = rRect.yBottom + 1;
1165 ::GpiMove(hPS, &vPoint[0]);
1166 vPoint[1].x = rRect.xRight - 2;
1167 vPoint[1].y = rRect.yTop - 2;
1168 ::GpiBox( hPS
1169 ,DRO_OUTLINE
1170 ,&vPoint[1]
1171 ,0L
1172 ,0L
1173 );
1174
1175 vLineBundle.lColor = 0x00FFFFFF; // WHITE
1176 vLineBundle.usMixMode = FM_OVERPAINT;
1177 vLineBundle.fxWidth = 2;
1178 vLineBundle.lGeomWidth = 2;
1179 vLineBundle.usType = LINETYPE_SOLID;
1180 vLineBundle.usEnd = 0;
1181 vLineBundle.usJoin = 0;
1182 ::GpiSetAttrs( hPS
1183 ,PRIM_LINE
1184 ,LBB_COLOR | LBB_MIX_MODE | LBB_WIDTH | LBB_GEOM_WIDTH | LBB_TYPE
1185 ,0L
1186 ,&vLineBundle
1187 );
1188 vPoint[0].x = rRect.xLeft + 2;
1189 vPoint[0].y = rRect.yBottom + 2;
1190 ::GpiMove(hPS, &vPoint[0]);
1191 vPoint[1].x = rRect.xLeft + 2;
1192 vPoint[1].y = rRect.yTop - 3;
1193 ::GpiLine(hPS, &vPoint[1]);
1194 vPoint[1].x = rRect.xRight - 3;
1195 vPoint[1].y = rRect.yTop - 3;
1196 ::GpiLine(hPS, &vPoint[1]);
1197
1198 vPoint[0].x = rRect.xLeft + 3;
1199 vPoint[0].y = rRect.yBottom + 3;
1200 ::GpiMove(hPS, &vPoint[0]);
1201 vPoint[1].x = rRect.xLeft + 3;
1202 vPoint[1].y = rRect.yTop - 4;
1203 ::GpiLine(hPS, &vPoint[1]);
1204 vPoint[1].x = rRect.xRight - 4;
1205 vPoint[1].y = rRect.yTop - 4;
1206 ::GpiLine(hPS, &vPoint[1]);
008089f6
DW
1207 }
1208} // end of wxDrawBorder
914589c2 1209
3c299c3a
DW
1210void wxOS2SetFont(
1211 HWND hWnd
1212, const wxFont& rFont
1213)
1214{
1215 char zFont[128];
1216 char zFacename[30];
1217 char zWeight[30];
1218 char zStyle[30];
1219
1220 if (hWnd == NULLHANDLE)
1221
1222 //
1223 // The fonts available for Presentation Params are just three
1224 // outline fonts, the rest are available to the GPI, so we must
1225 // map the families to one of these three
1226 //
1227 switch(rFont.GetFamily())
1228 {
1229 case wxSCRIPT:
1230 case wxDECORATIVE:
1231 case wxROMAN:
1232 strcpy(zFacename,"Times New Roman");
1233 break;
1234
1235 case wxTELETYPE:
1236 case wxMODERN:
1237 strcpy(zFacename, "Courier");
1238 break;
1239
1240 case wxSWISS:
1241 case wxDEFAULT:
1242 default:
1243 strcpy(zFacename, "Helvetica");
1244 break;
1245 }
1246
1247 switch(rFont.GetWeight())
1248 {
1249 default:
1250 case wxNORMAL:
1251 case wxLIGHT:
1252 zWeight[0] = '\0';
1253 break;
1254
1255 case wxBOLD:
1256 case wxFONTWEIGHT_MAX:
1257 strcpy(zWeight, "Bold");
1258 break;
1259 }
1260
1261 switch(rFont.GetStyle())
1262 {
1263 case wxITALIC:
1264 case wxSLANT:
1265 strcpy(zStyle, "Italic");
1266 break;
1267
1268 default:
1269 zStyle[0] = '\0';
1270 break;
1271 }
1272 sprintf(zFont, "%d.%s", rFont.GetPointSize(), zFacename);
1273 if (zWeight[0] != '\0')
1274 {
1275 strcat(zFont, " ");
1276 strcat(zFont, zWeight);
1277 }
1278 if (zStyle[0] != '\0')
1279 {
1280 strcat(zFont, " ");
1281 strcat(zFont, zStyle);
1282 }
1283 ::WinSetPresParam(hWnd, PP_FONTNAMESIZE, strlen(zFont) + 1, (PVOID)zFont);
1284} // end of wxOS2SetFont