]> git.saurik.com Git - wxWidgets.git/blame - src/os2/utils.cpp
Canvas: added some DECLARE_CLASS macros to stop it failing
[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
73deed44
VZ
294#if WXWIN_COMPATIBILITY_2_2
295
45fcbf3b 296// Output a debug mess., in a system dependent fashion.
10e5b930
DW
297void wxDebugMsg(
298 const wxChar* zFmt ...
299)
300{
301 va_list vAp;
302 static wxChar zBuffer[512];
303
304 if (!wxTheApp->GetWantDebugOutput())
305 return ;
306 va_start(vAp, zFmt);
307 sprintf(zBuffer, zFmt, vAp) ;
308 va_end(vAp);
0e320a79
DW
309}
310
311// Non-fatal error: pop up message box and (possibly) continue
10e5b930
DW
312void wxError(
313 const wxString& rMsg
314, const wxString& rTitle
315)
316{
13a4ea8d 317 wxBuffer = new wxChar[256];
10e5b930
DW
318 wxSprintf(wxBuffer, "%s\nContinue?", WXSTRINGCAST rMsg);
319 if (::WinMessageBox( HWND_DESKTOP
320 ,NULL
321 ,(PSZ)wxBuffer
322 ,(PSZ)WXSTRINGCAST rTitle
323 ,0
324 ,MB_ICONEXCLAMATION | MB_YESNO
325 ) == MBID_YES)
13a4ea8d 326 delete[] wxBuffer;
0e320a79
DW
327 wxExit();
328}
329
330// Fatal error: pop up message box and abort
10e5b930
DW
331void wxFatalError(
332 const wxString& rMsg
333, const wxString& rTitle
334)
335{
336 unsigned long ulRc;
337
338 ulRc = ::WinMessageBox( HWND_DESKTOP
339 ,NULL
340 ,WXSTRINGCAST rMsg
341 ,WXSTRINGCAST rTitle
342 ,0
343 ,MB_NOICON | MB_OK
344 );
345 DosExit(EXIT_PROCESS, ulRc);
0e320a79
DW
346}
347
73deed44
VZ
348#endif // WXWIN_COMPATIBILITY_2_2
349
0e320a79
DW
350// Emit a beeeeeep
351void wxBell()
352{
45fcbf3b 353 DosBeep(1000,1000); // 1kHz during 1 sec.
0e320a79
DW
354}
355
45fcbf3b
DW
356// Chris Breeze 27/5/98: revised WIN32 code to
357// detect WindowsNT correctly
10e5b930
DW
358int wxGetOsVersion(
359 int* pMajorVsn
360, int* pMinorVsn
361)
362{
363 ULONG ulSysInfo[QSV_MAX] = {0};
5d4b632b
DW
364 APIRET ulrc;
365
366 ulrc = ::DosQuerySysInfo( 1L
367 ,QSV_MAX
368 ,(PVOID)ulSysInfo
369 ,sizeof(ULONG) * QSV_MAX
370 );
371 if (ulrc == 0L)
10e5b930
DW
372 {
373 *pMajorVsn = ulSysInfo[QSV_VERSION_MAJOR];
5d4b632b 374 *pMajorVsn = *pMajorVsn/10;
10e5b930
DW
375 *pMinorVsn = ulSysInfo[QSV_VERSION_MINOR];
376 return wxWINDOWS_OS2;
377 }
378 return wxWINDOWS; // error if we get here, return generic value
0e320a79
DW
379}
380
381// Reading and writing resources (eg WIN.INI, .Xdefaults)
382#if wxUSE_RESOURCES
10e5b930
DW
383bool wxWriteResource(
384 const wxString& rSection
385, const wxString& rEntry
386, const wxString& rValue
387, const wxString& rFile
388)
0e320a79 389{
9dea36ef
DW
390 HAB hab = 0;
391 HINI hIni = 0;
78d50441 392
10e5b930 393 if (rFile != "")
78d50441 394 {
10e5b930 395 hIni = ::PrfOpenProfile(hab, (PSZ)WXSTRINGCAST rFile);
78d50441
DW
396 if (hIni != 0L)
397 {
398 return (::PrfWriteProfileString( hIni
10e5b930
DW
399 ,(PSZ)WXSTRINGCAST rSection
400 ,(PSZ)WXSTRINGCAST rEntry
401 ,(PSZ)WXSTRINGCAST rValue
78d50441
DW
402 ));
403 }
404 }
405 else
406 return (::PrfWriteProfileString( HINI_PROFILE
10e5b930
DW
407 ,(PSZ)WXSTRINGCAST rSection
408 ,(PSZ)WXSTRINGCAST rEntry
409 ,(PSZ)WXSTRINGCAST rValue
78d50441 410 ));
d90895ac 411 return FALSE;
0e320a79
DW
412}
413
10e5b930
DW
414bool wxWriteResource(
415 const wxString& rSection
416, const wxString& rEntry
417, float fValue
418, const wxString& rFile
419)
420{
421 wxChar zBuf[50];
422
423 wxSprintf(zBuf, "%.4f", fValue);
424 return wxWriteResource( rSection
425 ,rEntry
426 ,zBuf
427 ,rFile
428 );
0e320a79
DW
429}
430
1be7f92a
DW
431bool wxWriteResource(
432 const wxString& rSection
433, const wxString& rEntry
434, long lValue
435, const wxString& rFile
436)
0e320a79 437{
1be7f92a
DW
438 wxChar zBuf[50];
439
440 wxSprintf(zBuf, "%ld", lValue);
441 return wxWriteResource( rSection
442 ,rEntry
443 ,zBuf
444 ,rFile
445 );
0e320a79
DW
446}
447
1be7f92a
DW
448bool wxWriteResource(
449 const wxString& rSection
450, const wxString& rEntry
451, int lValue
452, const wxString& rFile
453)
0e320a79 454{
1be7f92a
DW
455 wxChar zBuf[50];
456
457 wxSprintf(zBuf, "%d", lValue);
458 return wxWriteResource( rSection
459 ,rEntry
460 ,zBuf
461 ,rFile
462 );
0e320a79
DW
463}
464
1be7f92a
DW
465bool wxGetResource(
466 const wxString& rSection
467, const wxString& rEntry
468, wxChar** ppValue
469, const wxString& rFile
470)
0e320a79 471{
9dea36ef
DW
472 HAB hab = 0;
473 HINI hIni = 0;
e8fd750b
DW
474 wxChar zDefunkt[] = _T("$$default");
475 char zBuf[1000];
78d50441 476
1be7f92a 477 if (rFile != "")
78d50441 478 {
1be7f92a 479 hIni = ::PrfOpenProfile(hab, (PSZ)WXSTRINGCAST rFile);
78d50441
DW
480 if (hIni != 0L)
481 {
482 ULONG n = ::PrfQueryProfileString( hIni
1be7f92a
DW
483 ,(PSZ)WXSTRINGCAST rSection
484 ,(PSZ)WXSTRINGCAST rEntry
485 ,(PSZ)zDefunkt
e8fd750b 486 ,(PVOID)zBuf
78d50441
DW
487 ,1000
488 );
e8fd750b 489 if (zBuf == NULL)
78d50441 490 return FALSE;
e8fd750b
DW
491 if (n == 0L || wxStrcmp(zBuf, zDefunkt) == 0)
492 return FALSE;
493 zBuf[n-1] = '\0';
78d50441
DW
494 }
495 else
496 return FALSE;
497 }
498 else
499 {
500 ULONG n = ::PrfQueryProfileString( HINI_PROFILE
1be7f92a
DW
501 ,(PSZ)WXSTRINGCAST rSection
502 ,(PSZ)WXSTRINGCAST rEntry
503 ,(PSZ)zDefunkt
e8fd750b 504 ,(PVOID)zBuf
78d50441
DW
505 ,1000
506 );
e8fd750b
DW
507 if (zBuf == NULL)
508 return FALSE;
509 if (n == 0L || wxStrcmp(zBuf, zDefunkt) == 0)
78d50441 510 return FALSE;
e8fd750b 511 zBuf[n-1] = '\0';
78d50441 512 }
e8fd750b 513 strcpy((char*)*ppValue, zBuf);
78d50441 514 return TRUE;
d90895ac 515}
0e320a79 516
1be7f92a
DW
517bool wxGetResource(
518 const wxString& rSection
519, const wxString& rEntry
520, float* pValue
521, const wxString& rFile
522)
0e320a79 523{
1be7f92a 524 wxChar* zStr = NULL;
13a4ea8d
DW
525
526 zStr = new wxChar[1000];
1be7f92a
DW
527 bool bSucc = wxGetResource( rSection
528 ,rEntry
529 ,(wxChar **)&zStr
530 ,rFile
531 );
532
533 if (bSucc)
534 {
535 *pValue = (float)wxStrtod(zStr, NULL);
536 delete[] zStr;
537 return TRUE;
538 }
13a4ea8d
DW
539 else
540 {
541 delete[] zStr;
542 return FALSE;
543 }
0e320a79
DW
544}
545
1be7f92a
DW
546bool wxGetResource(
547 const wxString& rSection
548, const wxString& rEntry
549, long* pValue
550, const wxString& rFile
551)
0e320a79 552{
1be7f92a 553 wxChar* zStr = NULL;
13a4ea8d
DW
554
555 zStr = new wxChar[1000];
1be7f92a
DW
556 bool bSucc = wxGetResource( rSection
557 ,rEntry
558 ,(wxChar **)&zStr
559 ,rFile
560 );
561
562 if (bSucc)
563 {
564 *pValue = wxStrtol(zStr, NULL, 10);
565 delete[] zStr;
566 return TRUE;
567 }
13a4ea8d
DW
568 else
569 {
570 delete[] zStr;
571 return FALSE;
572 }
0e320a79
DW
573}
574
1be7f92a
DW
575bool wxGetResource(
576 const wxString& rSection
577, const wxString& rEntry
578, int* pValue
579, const wxString& rFile
580)
0e320a79 581{
1be7f92a 582 wxChar* zStr = NULL;
13a4ea8d
DW
583
584 zStr = new wxChar[1000];
1be7f92a
DW
585 bool bSucc = wxGetResource( rSection
586 ,rEntry
587 ,(wxChar **)&zStr
588 ,rFile
589 );
590
591 if (bSucc)
592 {
593 *pValue = (int)wxStrtol(zStr, NULL, 10);
594 delete[] zStr;
595 return TRUE;
596 }
13a4ea8d
DW
597 else
598 {
599 delete[] zStr;
600 return FALSE;
601 }
0e320a79
DW
602}
603#endif // wxUSE_RESOURCES
604
45fcbf3b
DW
605// ---------------------------------------------------------------------------
606// helper functions for showing a "busy" cursor
607// ---------------------------------------------------------------------------
608
609HCURSOR gs_wxBusyCursor = 0; // new, busy cursor
610HCURSOR gs_wxBusyCursorOld = 0; // old cursor
611static int gs_wxBusyCursorCount = 0;
0e320a79
DW
612
613// Set the cursor to the busy cursor for all windows
1be7f92a
DW
614void wxBeginBusyCursor(
615 wxCursor* pCursor
616)
0e320a79 617{
45fcbf3b
DW
618 if ( gs_wxBusyCursorCount++ == 0 )
619 {
1be7f92a 620 gs_wxBusyCursor = (HCURSOR)pCursor->GetHCURSOR();
45fcbf3b
DW
621 ::WinSetPointer(HWND_DESKTOP, (HPOINTER)gs_wxBusyCursor);
622 }
623 //else: nothing to do, already set
0e320a79
DW
624}
625
626// Restore cursor to normal
627void wxEndBusyCursor()
628{
1be7f92a
DW
629 wxCHECK_RET( gs_wxBusyCursorCount > 0
630 ,_T("no matching wxBeginBusyCursor() for wxEndBusyCursor()")
631 );
45fcbf3b 632
1be7f92a 633 if (--gs_wxBusyCursorCount == 0)
45fcbf3b
DW
634 {
635 ::WinSetPointer(HWND_DESKTOP, (HPOINTER)gs_wxBusyCursorOld);
636 gs_wxBusyCursorOld = 0;
637 }
0e320a79
DW
638}
639
640// TRUE if we're between the above two calls
641bool wxIsBusy()
642{
1be7f92a 643 return (gs_wxBusyCursorCount > 0);
45fcbf3b
DW
644}
645
646// ---------------------------------------------------------------------------
1be7f92a
DW
647const wxChar* wxGetHomeDir(
648 wxString* pStr
649)
45fcbf3b 650{
1be7f92a 651 wxString& rStrDir = *pStr;
0e320a79 652
1be7f92a
DW
653 // OS/2 has no idea about home,
654 // so use the working directory instead?
45fcbf3b 655
1be7f92a 656 // 256 was taken from os2def.h
45fcbf3b
DW
657#ifndef MAX_PATH
658# define MAX_PATH 256
659#endif
660
1be7f92a
DW
661 char zDirName[256];
662 ULONG ulDirLen;
45fcbf3b 663
1be7f92a
DW
664 ::DosQueryCurrentDir(0, zDirName, &ulDirLen);
665 rStrDir = zDirName;
666 return rStrDir.c_str();
45fcbf3b
DW
667}
668
10e5b930 669// Hack for OS/2
1be7f92a
DW
670wxChar* wxGetUserHome (
671 const wxString& rUser
672)
673{
674 wxChar* zHome;
675 wxString sUser1(rUser);
676
13a4ea8d 677 wxBuffer = new wxChar[256];
06298235 678#ifndef __EMX__
1be7f92a 679 if (sUser1 != _T(""))
45fcbf3b 680 {
1be7f92a
DW
681 wxChar zTmp[64];
682
683 if (wxGetUserId( zTmp
684 ,sizeof(zTmp)/sizeof(char)
685 ))
686 {
687 // Guests belong in the temp dir
688 if (wxStricmp(zTmp, _T("annonymous")) == 0)
689 {
690 if ((zHome = wxGetenv(_T("TMP"))) != NULL ||
691 (zHome = wxGetenv(_T("TMPDIR"))) != NULL ||
692 (zHome = wxGetenv(_T("TEMP"))) != NULL)
13a4ea8d 693 delete[] wxBuffer;
1be7f92a
DW
694 return *zHome ? zHome : (wxChar*)_T("\\");
695 }
696 if (wxStricmp(zTmp, WXSTRINGCAST sUser1) == 0)
697 sUser1 = _T("");
698 }
45fcbf3b 699 }
06298235 700#endif
1be7f92a 701 if (sUser1 == _T(""))
13a4ea8d 702 {
1be7f92a
DW
703 if ((zHome = wxGetenv(_T("HOME"))) != NULL)
704 {
705 wxStrcpy(wxBuffer, zHome);
706 Unix2DosFilename(wxBuffer);
13a4ea8d
DW
707 wxStrcpy(zHome, wxBuffer);
708 delete[] wxBuffer;
709 return zHome;
1be7f92a 710 }
13a4ea8d
DW
711 }
712 delete[] wxBuffer;
713 return NULL; // No home known!
0e320a79
DW
714}
715
716// Check whether this window wants to process messages, e.g. Stop button
717// in long calculations.
1be7f92a
DW
718bool wxCheckForInterrupt(
719 wxWindow* pWnd
720)
0e320a79 721{
1be7f92a
DW
722 if(pWnd)
723 {
724 QMSG vMsg;
9dea36ef
DW
725 HAB hab = 0;
726 HWND hwndFilter = NULLHANDLE;
1be7f92a 727 HWND hwndWin= (HWND) pWnd->GetHWND();
45fcbf3b 728
1be7f92a
DW
729 while(::WinPeekMsg(hab, &vMsg, hwndFilter, 0, 0, PM_REMOVE))
730 {
731 ::WinDispatchMsg(hab, &vMsg);
732 }
733 return TRUE;//*** temporary?
734 }
735 else
45fcbf3b 736 {
1be7f92a
DW
737 wxFAIL_MSG(_T("pWnd==NULL !!!"));
738 return FALSE;//*** temporary?
45fcbf3b 739 }
45fcbf3b
DW
740}
741
1be7f92a
DW
742void wxGetMousePosition(
743 int* pX
744, int* pY
745)
0e320a79 746{
1be7f92a
DW
747 POINTL vPt;
748
749 ::WinQueryPointerPos(HWND_DESKTOP, &vPt);
750 *pX = vPt.x;
751 *pY = vPt.y;
0e320a79
DW
752};
753
754// Return TRUE if we have a colour display
755bool wxColourDisplay()
756{
19193a2c 757#if 0
1be7f92a
DW
758 HPS hpsScreen;
759 HDC hdcScreen;
760 LONG lColors;
761
762 hpsScreen = ::WinGetScreenPS(HWND_DESKTOP);
763 hdcScreen = ::GpiQueryDevice(hpsScreen);
764 ::DevQueryCaps(hdcScreen, CAPS_COLORS, 1L, &lColors);
765 return(lColors > 1L);
19193a2c
KB
766#else
767 // I don't see how the PM display could not be color. Besides, this
768 // was leaking DCs and PSs!!! MN
987da0d4 769 return TRUE;
19193a2c 770#endif
0e320a79
DW
771}
772
773// Returns depth of screen
774int wxDisplayDepth()
775{
1be7f92a
DW
776 HPS hpsScreen;
777 HDC hdcScreen;
778 LONG lPlanes;
779 LONG lBitsPerPixel;
19193a2c
KB
780 static LONG nDepth = 0;
781
782 // The screen colordepth ain't gonna change. No reason to query
783 // it over and over!
784 if (!nDepth) {
785 hpsScreen = ::WinGetScreenPS(HWND_DESKTOP);
786 hdcScreen = ::GpiQueryDevice(hpsScreen);
787 ::DevQueryCaps(hdcScreen, CAPS_COLOR_PLANES, 1L, &lPlanes);
788 ::DevQueryCaps(hdcScreen, CAPS_COLOR_BITCOUNT, 1L, &lBitsPerPixel);
789
790 nDepth = (int)(lPlanes * lBitsPerPixel);
791 ::DevCloseDC(hdcScreen);
792 ::WinReleasePS(hpsScreen);
793 }
d90895ac 794 return (nDepth);
0e320a79
DW
795}
796
797// Get size of display
1be7f92a
DW
798void wxDisplaySize(
799 int* pWidth
800, int* pHeight
801)
0e320a79 802{
1be7f92a
DW
803 HPS hpsScreen;
804 HDC hdcScreen;
19193a2c
KB
805 static LONG lWidth = 0;
806 static LONG lHeight = 0;
807
808 // The screen size ain't gonna change either so just cache the values
809 if (!lWidth) {
810 hpsScreen = ::WinGetScreenPS(HWND_DESKTOP);
811 hdcScreen = ::GpiQueryDevice(hpsScreen);
812 ::DevQueryCaps(hdcScreen, CAPS_WIDTH, 1L, &lWidth);
813 ::DevQueryCaps(hdcScreen, CAPS_HEIGHT, 1L, &lHeight);
814 ::DevCloseDC(hdcScreen);
815 ::WinReleasePS(hpsScreen);
816 }
892b89f3
DW
817 *pWidth = (int)lWidth;
818 *pHeight = (int)lHeight;
45fcbf3b
DW
819}
820
5b3ed311
DW
821void wxDisplaySizeMM(
822 int* pWidth
823, int* pHeight
824)
825{
826 HPS hpsScreen;
827 HDC hdcScreen;
828
829 hpsScreen = ::WinGetScreenPS(HWND_DESKTOP);
830 hdcScreen = ::GpiQueryDevice(hpsScreen);
831
832 if (pWidth)
833 ::DevQueryCaps( hdcScreen
834 ,CAPS_HORIZONTAL_RESOLUTION
835 ,1L
836 ,(PLONG)pWidth
837 );
838 if (pHeight)
839 ::DevQueryCaps( hdcScreen
840 ,CAPS_VERTICAL_RESOLUTION
841 ,1L
842 ,(PLONG)pHeight
843 );
19193a2c
KB
844 ::DevCloseDC(hdcScreen);
845 ::WinReleasePS(hpsScreen);
5b3ed311
DW
846}
847
ec5d7799
RD
848void wxClientDisplayRect(int *x, int *y, int *width, int *height)
849{
850 // This is supposed to return desktop dimensions minus any window
851 // manager panels, menus, taskbars, etc. If there is a way to do that
852 // for this platform please fix this function, otherwise it defaults
853 // to the entire desktop.
854 if (x) *x = 0;
855 if (y) *y = 0;
856 wxDisplaySize(width, height);
857}
858
859
1be7f92a
DW
860bool wxDirExists(
861 const wxString& rDir
862)
45fcbf3b 863{
1be7f92a 864 return (::DosSetCurrentDir(WXSTRINGCAST rDir));
45fcbf3b
DW
865}
866
867// ---------------------------------------------------------------------------
868// window information functions
869// ---------------------------------------------------------------------------
870
1be7f92a
DW
871wxString WXDLLEXPORT wxGetWindowText(
872 WXHWND hWnd
873)
45fcbf3b 874{
1be7f92a
DW
875 wxString vStr;
876 long lLen = ::WinQueryWindowTextLength((HWND)hWnd) + 1;
877
878 ::WinQueryWindowText((HWND)hWnd, lLen, vStr.GetWriteBuf((int)lLen));
879 vStr.UngetWriteBuf();
45fcbf3b 880
1be7f92a 881 return vStr;
45fcbf3b
DW
882}
883
1be7f92a
DW
884wxString WXDLLEXPORT wxGetWindowClass(
885 WXHWND hWnd
886)
45fcbf3b 887{
1be7f92a
DW
888 wxString vStr;
889 int nLen = 256; // some starting value
45fcbf3b
DW
890
891 for ( ;; )
892 {
1be7f92a 893 int nCount = ::WinQueryClassName((HWND)hWnd, nLen, vStr.GetWriteBuf(nLen));
45fcbf3b 894
1be7f92a
DW
895 vStr.UngetWriteBuf();
896 if (nCount == nLen )
45fcbf3b
DW
897 {
898 // the class name might have been truncated, retry with larger
899 // buffer
1be7f92a 900 nLen *= 2;
45fcbf3b
DW
901 }
902 else
903 {
904 break;
905 }
906 }
1be7f92a 907 return vStr;
45fcbf3b
DW
908}
909
1be7f92a
DW
910WXWORD WXDLLEXPORT wxGetWindowId(
911 WXHWND hWnd
912)
45fcbf3b
DW
913{
914 return ::WinQueryWindowUShort((HWND)hWnd, QWS_ID);
0e320a79
DW
915}
916
914589c2
DW
917wxString WXDLLEXPORT wxPMErrorToStr(
918 ERRORID vError
919)
920{
921 wxString sError;
922
923 //
924 // Remove the high order byte -- it is useless
925 //
926 vError &= 0x0000ffff;
927 switch(vError)
928 {
929 case PMERR_INVALID_HWND:
930 sError = wxT("Invalid window handle specified");
931 break;
932
933 case PMERR_INVALID_FLAG:
934 sError = wxT("Invalid flag bit set");
935 break;
936
937 case PMERR_NO_MSG_QUEUE:
938 sError = wxT("No message queue available");
939 break;
940
941 case PMERR_INVALID_PARM:
942 sError = wxT("Parameter contained invalid data");
943 break;
944
945 case PMERR_INVALID_PARAMETERS:
946 sError = wxT("Parameter value is out of range");
947 break;
948
949 case PMERR_PARAMETER_OUT_OF_RANGE:
950 sError = wxT("Parameter value is out of range");
951 break;
952
953 case PMERR_INVALID_INTEGER_ATOM:
954 sError = wxT("Not a valid atom");
955 break;
956
957 case PMERR_INVALID_HATOMTBL:
958 sError = wxT("Atom table handle is invalid");
959 break;
960
961 case PMERR_INVALID_ATOM_NAME:
962 sError = wxT("Not a valid atom name");
963 break;
964
965 case PMERR_ATOM_NAME_NOT_FOUND:
966 sError = wxT("Valid name format, but cannot find name in atom table");
967 break;
968
969 default:
970 sError = wxT("Unknown error");
971 }
972 return(sError);
973} // end of wxPMErrorToStr
974
008089f6
DW
975void wxDrawBorder(
976 HPS hPS
977, RECTL& rRect
978, WXDWORD dwStyle
979)
980{
981 POINTL vPoint[2];
982
983 vPoint[0].x = rRect.xLeft;
984 vPoint[0].y = rRect.yBottom;
985 ::GpiMove(hPS, &vPoint[0]);
986 if (dwStyle & wxSIMPLE_BORDER ||
987 dwStyle & wxSTATIC_BORDER)
988 {
ad7f3189
DW
989 vPoint[1].x = rRect.xRight - 1;
990 vPoint[1].y = rRect.yTop - 1;
008089f6
DW
991 ::GpiBox( hPS
992 ,DRO_OUTLINE
993 ,&vPoint[1]
994 ,0L
995 ,0L
996 );
997 }
998 if (dwStyle & wxSUNKEN_BORDER)
999 {
1000 LINEBUNDLE vLineBundle;
1001
ad7f3189 1002 vLineBundle.lColor = 0x00FFFFFF; // WHITE
008089f6
DW
1003 vLineBundle.usMixMode = FM_OVERPAINT;
1004 vLineBundle.fxWidth = 2;
1005 vLineBundle.lGeomWidth = 2;
1006 vLineBundle.usType = LINETYPE_SOLID;
1007 vLineBundle.usEnd = 0;
1008 vLineBundle.usJoin = 0;
1009 ::GpiSetAttrs( hPS
1010 ,PRIM_LINE
1011 ,LBB_COLOR | LBB_MIX_MODE | LBB_WIDTH | LBB_GEOM_WIDTH | LBB_TYPE
1012 ,0L
1013 ,&vLineBundle
1014 );
ad7f3189
DW
1015 vPoint[1].x = rRect.xRight - 1;
1016 vPoint[1].y = rRect.yTop - 1;
008089f6
DW
1017 ::GpiBox( hPS
1018 ,DRO_OUTLINE
1019 ,&vPoint[1]
1020 ,0L
1021 ,0L
1022 );
ad7f3189
DW
1023 vPoint[0].x = rRect.xLeft + 1;
1024 vPoint[0].y = rRect.yBottom + 1;
1025 ::GpiMove(hPS, &vPoint[0]);
1026 vPoint[1].x = rRect.xRight - 2;
1027 vPoint[1].y = rRect.yTop - 2;
1028 ::GpiBox( hPS
1029 ,DRO_OUTLINE
1030 ,&vPoint[1]
1031 ,0L
1032 ,0L
1033 );
1034
008089f6
DW
1035 vLineBundle.lColor = 0x00000000; // BLACK
1036 vLineBundle.usMixMode = FM_OVERPAINT;
1037 vLineBundle.fxWidth = 2;
1038 vLineBundle.lGeomWidth = 2;
1039 vLineBundle.usType = LINETYPE_SOLID;
1040 vLineBundle.usEnd = 0;
1041 vLineBundle.usJoin = 0;
1042 ::GpiSetAttrs( hPS
1043 ,PRIM_LINE
1044 ,LBB_COLOR | LBB_MIX_MODE | LBB_WIDTH | LBB_GEOM_WIDTH | LBB_TYPE
1045 ,0L
1046 ,&vLineBundle
1047 );
1048 vPoint[0].x = rRect.xLeft + 2;
1049 vPoint[0].y = rRect.yBottom + 2;
1050 ::GpiMove(hPS, &vPoint[0]);
1051 vPoint[1].x = rRect.xLeft + 2;
ad7f3189 1052 vPoint[1].y = rRect.yTop - 3;
008089f6 1053 ::GpiLine(hPS, &vPoint[1]);
ad7f3189
DW
1054 vPoint[1].x = rRect.xRight - 3;
1055 vPoint[1].y = rRect.yTop - 3;
1056 ::GpiLine(hPS, &vPoint[1]);
1057
1058 vPoint[0].x = rRect.xLeft + 3;
1059 vPoint[0].y = rRect.yBottom + 3;
1060 ::GpiMove(hPS, &vPoint[0]);
1061 vPoint[1].x = rRect.xLeft + 3;
1062 vPoint[1].y = rRect.yTop - 4;
1063 ::GpiLine(hPS, &vPoint[1]);
1064 vPoint[1].x = rRect.xRight - 4;
1065 vPoint[1].y = rRect.yTop - 4;
008089f6
DW
1066 ::GpiLine(hPS, &vPoint[1]);
1067 }
1068 if (dwStyle & wxDOUBLE_BORDER)
1069 {
ad7f3189
DW
1070 LINEBUNDLE vLineBundle;
1071
1072 vLineBundle.lColor = 0x00FFFFFF; // 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[1].x = rRect.xRight - 1;
1086 vPoint[1].y = rRect.yTop - 1;
1087 ::GpiBox( hPS
1088 ,DRO_OUTLINE
1089 ,&vPoint[1]
1090 ,0L
1091 ,0L
1092 );
1093 vLineBundle.lColor = 0x00000000; // WHITE
1094 vLineBundle.usMixMode = FM_OVERPAINT;
1095 vLineBundle.fxWidth = 2;
1096 vLineBundle.lGeomWidth = 2;
1097 vLineBundle.usType = LINETYPE_SOLID;
1098 vLineBundle.usEnd = 0;
1099 vLineBundle.usJoin = 0;
1100 ::GpiSetAttrs( hPS
1101 ,PRIM_LINE
1102 ,LBB_COLOR | LBB_MIX_MODE | LBB_WIDTH | LBB_GEOM_WIDTH | LBB_TYPE
1103 ,0L
1104 ,&vLineBundle
1105 );
1106 vPoint[0].x = rRect.xLeft + 2;
1107 vPoint[0].y = rRect.yBottom + 2;
1108 ::GpiMove(hPS, &vPoint[0]);
1109 vPoint[1].x = rRect.xRight - 2;
1110 vPoint[1].y = rRect.yTop - 2;
1111 ::GpiBox( hPS
1112 ,DRO_OUTLINE
1113 ,&vPoint[1]
1114 ,0L
1115 ,0L
1116 );
1117 vLineBundle.lColor = 0x00FFFFFF; // BLACK
1118 vLineBundle.usMixMode = FM_OVERPAINT;
1119 vLineBundle.fxWidth = 2;
1120 vLineBundle.lGeomWidth = 2;
1121 vLineBundle.usType = LINETYPE_SOLID;
1122 vLineBundle.usEnd = 0;
1123 vLineBundle.usJoin = 0;
1124 ::GpiSetAttrs( hPS
1125 ,PRIM_LINE
1126 ,LBB_COLOR | LBB_MIX_MODE | LBB_WIDTH | LBB_GEOM_WIDTH | LBB_TYPE
1127 ,0L
1128 ,&vLineBundle
1129 );
1130 vPoint[0].x = rRect.xLeft + 3;
1131 vPoint[0].y = rRect.yBottom + 3;
1132 ::GpiMove(hPS, &vPoint[0]);
1133 vPoint[1].x = rRect.xRight - 3;
1134 vPoint[1].y = rRect.yTop - 3;
1135 ::GpiBox( hPS
1136 ,DRO_OUTLINE
1137 ,&vPoint[1]
1138 ,0L
1139 ,0L
1140 );
008089f6
DW
1141 }
1142 if (dwStyle & wxRAISED_BORDER)
1143 {
ad7f3189
DW
1144 LINEBUNDLE vLineBundle;
1145
1146 vLineBundle.lColor = 0x00000000; // BLACK
1147 vLineBundle.usMixMode = FM_OVERPAINT;
1148 vLineBundle.fxWidth = 2;
1149 vLineBundle.lGeomWidth = 2;
1150 vLineBundle.usType = LINETYPE_SOLID;
1151 vLineBundle.usEnd = 0;
1152 vLineBundle.usJoin = 0;
1153 ::GpiSetAttrs( hPS
1154 ,PRIM_LINE
1155 ,LBB_COLOR | LBB_MIX_MODE | LBB_WIDTH | LBB_GEOM_WIDTH | LBB_TYPE
1156 ,0L
1157 ,&vLineBundle
1158 );
1159 vPoint[1].x = rRect.xRight - 1;
1160 vPoint[1].y = rRect.yTop - 1;
1161 ::GpiBox( hPS
1162 ,DRO_OUTLINE
1163 ,&vPoint[1]
1164 ,0L
1165 ,0L
1166 );
1167 vPoint[0].x = rRect.xLeft + 1;
1168 vPoint[0].y = rRect.yBottom + 1;
1169 ::GpiMove(hPS, &vPoint[0]);
1170 vPoint[1].x = rRect.xRight - 2;
1171 vPoint[1].y = rRect.yTop - 2;
1172 ::GpiBox( hPS
1173 ,DRO_OUTLINE
1174 ,&vPoint[1]
1175 ,0L
1176 ,0L
1177 );
1178
1179 vLineBundle.lColor = 0x00FFFFFF; // WHITE
1180 vLineBundle.usMixMode = FM_OVERPAINT;
1181 vLineBundle.fxWidth = 2;
1182 vLineBundle.lGeomWidth = 2;
1183 vLineBundle.usType = LINETYPE_SOLID;
1184 vLineBundle.usEnd = 0;
1185 vLineBundle.usJoin = 0;
1186 ::GpiSetAttrs( hPS
1187 ,PRIM_LINE
1188 ,LBB_COLOR | LBB_MIX_MODE | LBB_WIDTH | LBB_GEOM_WIDTH | LBB_TYPE
1189 ,0L
1190 ,&vLineBundle
1191 );
1192 vPoint[0].x = rRect.xLeft + 2;
1193 vPoint[0].y = rRect.yBottom + 2;
1194 ::GpiMove(hPS, &vPoint[0]);
1195 vPoint[1].x = rRect.xLeft + 2;
1196 vPoint[1].y = rRect.yTop - 3;
1197 ::GpiLine(hPS, &vPoint[1]);
1198 vPoint[1].x = rRect.xRight - 3;
1199 vPoint[1].y = rRect.yTop - 3;
1200 ::GpiLine(hPS, &vPoint[1]);
1201
1202 vPoint[0].x = rRect.xLeft + 3;
1203 vPoint[0].y = rRect.yBottom + 3;
1204 ::GpiMove(hPS, &vPoint[0]);
1205 vPoint[1].x = rRect.xLeft + 3;
1206 vPoint[1].y = rRect.yTop - 4;
1207 ::GpiLine(hPS, &vPoint[1]);
1208 vPoint[1].x = rRect.xRight - 4;
1209 vPoint[1].y = rRect.yTop - 4;
1210 ::GpiLine(hPS, &vPoint[1]);
008089f6
DW
1211 }
1212} // end of wxDrawBorder
914589c2 1213
3c299c3a
DW
1214void wxOS2SetFont(
1215 HWND hWnd
1216, const wxFont& rFont
1217)
1218{
1219 char zFont[128];
1220 char zFacename[30];
1221 char zWeight[30];
1222 char zStyle[30];
1223
1224 if (hWnd == NULLHANDLE)
e1146627 1225 return;
3c299c3a
DW
1226
1227 //
1228 // The fonts available for Presentation Params are just three
1229 // outline fonts, the rest are available to the GPI, so we must
1230 // map the families to one of these three
1231 //
1232 switch(rFont.GetFamily())
1233 {
1234 case wxSCRIPT:
e1146627
DW
1235 strcpy(zFacename, "Script");
1236 break;
1237
3c299c3a
DW
1238 case wxDECORATIVE:
1239 case wxROMAN:
e1146627 1240 strcpy(zFacename,"Tms Rmn");
3c299c3a
DW
1241 break;
1242
1243 case wxTELETYPE:
3c299c3a
DW
1244 strcpy(zFacename, "Courier");
1245 break;
1246
e1146627 1247 case wxMODERN:
2c1e8f2e 1248 strcpy(zFacename, "System VIO");
e1146627
DW
1249 break;
1250
3c299c3a 1251 case wxSWISS:
e1146627
DW
1252 strcpy(zFacename, "Helv");
1253 break;
1254
3c299c3a
DW
1255 case wxDEFAULT:
1256 default:
2c1e8f2e 1257 strcpy(zFacename, "System VIO");
3c299c3a
DW
1258 break;
1259 }
1260
1261 switch(rFont.GetWeight())
1262 {
1263 default:
1264 case wxNORMAL:
1265 case wxLIGHT:
1266 zWeight[0] = '\0';
1267 break;
1268
1269 case wxBOLD:
1270 case wxFONTWEIGHT_MAX:
1271 strcpy(zWeight, "Bold");
1272 break;
1273 }
1274
1275 switch(rFont.GetStyle())
1276 {
1277 case wxITALIC:
1278 case wxSLANT:
1279 strcpy(zStyle, "Italic");
1280 break;
1281
1282 default:
1283 zStyle[0] = '\0';
1284 break;
1285 }
1286 sprintf(zFont, "%d.%s", rFont.GetPointSize(), zFacename);
1287 if (zWeight[0] != '\0')
1288 {
1289 strcat(zFont, " ");
1290 strcat(zFont, zWeight);
1291 }
1292 if (zStyle[0] != '\0')
1293 {
1294 strcat(zFont, " ");
1295 strcat(zFont, zStyle);
1296 }
1297 ::WinSetPresParam(hWnd, PP_FONTNAMESIZE, strlen(zFont) + 1, (PVOID)zFont);
1298} // end of wxOS2SetFont