]> git.saurik.com Git - wxWidgets.git/blame - src/os2/utils.cpp
Corrected test for _vsnprintf
[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
134, int nSig
135)
0e320a79 136{
10e5b930 137 return((int)::DosKillProcess(0, (PID)lPid));
0e320a79
DW
138}
139
140//
141// Execute a program in an Interactive Shell
142//
10e5b930
DW
143bool wxShell(
144 const wxString& rCommand
145)
0e320a79 146{
c5fb56c0
DW
147 wxChar* zShell = _T("CMD.EXE");
148 wxString sInputs;
10e5b930 149 wxChar zTmp[255];
c5fb56c0
DW
150 STARTDATA SData = {0};
151 PSZ PgmTitle = "Command Shell";
152 APIRET rc;
153 PID vPid = 0;
154 ULONG ulSessID = 0;
155 UCHAR achObjBuf[256] = {0}; //error data if DosStart fails
156 RESULTCODES vResult;
157
158 SData.Length = sizeof(STARTDATA);
159 SData.Related = SSF_RELATED_INDEPENDENT;
160 SData.FgBg = SSF_FGBG_FORE;
161 SData.TraceOpt = SSF_TRACEOPT_NONE;
162 SData.PgmTitle = PgmTitle;
163 SData.PgmName = zShell;
164
909b4f08 165 sInputs = "/C " + rCommand;
9ac6ff7b 166 SData.PgmInputs = (BYTE*)sInputs.c_str();
c5fb56c0
DW
167 SData.TermQ = 0;
168 SData.Environment = 0;
169 SData.InheritOpt = SSF_INHERTOPT_SHELL;
170 SData.SessionType = SSF_TYPE_WINDOWABLEVIO;
171 SData.IconFile = 0;
172 SData.PgmHandle = 0;
173 SData.PgmControl = SSF_CONTROL_VISIBLE | SSF_CONTROL_MAXIMIZE;
174 SData.InitXPos = 30;
175 SData.InitYPos = 40;
176 SData.InitXSize = 200;
177 SData.InitYSize = 140;
178 SData.Reserved = 0;
179 SData.ObjectBuffer = (char*)achObjBuf;
180 SData.ObjectBuffLen = (ULONG)sizeof(achObjBuf);
181
182 rc = ::DosStartSession(&SData, &ulSessID, &vPid);
9ac6ff7b 183 if (rc == 0 || rc == 457) // NO_ERROR or SMG_START_IN_BACKGROUND
c5fb56c0
DW
184 {
185 PTIB ptib;
186 PPIB ppib;
10e5b930 187
c5fb56c0 188 ::DosGetInfoBlocks(&ptib, &ppib);
10e5b930 189
c5fb56c0
DW
190 ::DosWaitChild( DCWA_PROCESS
191 ,DCWW_WAIT
192 ,&vResult
193 ,&ppib->pib_ulpid
194 ,vPid
195 );
196 }
197 return (rc != 0);
0e320a79
DW
198}
199
200// Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
5d4b632b 201long wxGetFreeMemory()
0e320a79 202{
9dea36ef 203 void* pMemptr = NULL;
78d50441
DW
204 ULONG lSize;
205 ULONG lMemFlags;
206 APIRET rc;
207
208 lMemFlags = PAG_FREE;
10e5b930 209 rc = ::DosQueryMem(pMemptr, &lSize, &lMemFlags);
78d50441
DW
210 if (rc != 0)
211 return -1L;
212 return (long)lSize;
45fcbf3b
DW
213}
214
215// Sleep for nSecs seconds. Attempt a Windows implementation using timers.
216static bool inTimer = FALSE;
217
218class wxSleepTimer: public wxTimer
219{
10e5b930
DW
220public:
221 inline void Notify()
222 {
223 inTimer = FALSE;
224 Stop();
225 }
45fcbf3b
DW
226};
227
10e5b930 228static wxTimer* wxTheSleepTimer = NULL;
45fcbf3b 229
10e5b930
DW
230void wxUsleep(
231 unsigned long ulMilliseconds
232)
45fcbf3b 233{
468e327a 234 ::DosSleep(ulMilliseconds/1000l);
0e320a79
DW
235}
236
10e5b930
DW
237void wxSleep(
238 int nSecs
239)
0e320a79 240{
10e5b930 241 ::DosSleep(1000 * nSecs);
0e320a79
DW
242}
243
244// Consume all events until no more left
245void wxFlushEvents()
246{
45fcbf3b 247// wxYield();
0e320a79
DW
248}
249
45fcbf3b 250// Output a debug mess., in a system dependent fashion.
10e5b930
DW
251void wxDebugMsg(
252 const wxChar* zFmt ...
253)
254{
255 va_list vAp;
256 static wxChar zBuffer[512];
257
258 if (!wxTheApp->GetWantDebugOutput())
259 return ;
260 va_start(vAp, zFmt);
261 sprintf(zBuffer, zFmt, vAp) ;
262 va_end(vAp);
0e320a79
DW
263}
264
265// Non-fatal error: pop up message box and (possibly) continue
10e5b930
DW
266void wxError(
267 const wxString& rMsg
268, const wxString& rTitle
269)
270{
13a4ea8d 271 wxBuffer = new wxChar[256];
10e5b930
DW
272 wxSprintf(wxBuffer, "%s\nContinue?", WXSTRINGCAST rMsg);
273 if (::WinMessageBox( HWND_DESKTOP
274 ,NULL
275 ,(PSZ)wxBuffer
276 ,(PSZ)WXSTRINGCAST rTitle
277 ,0
278 ,MB_ICONEXCLAMATION | MB_YESNO
279 ) == MBID_YES)
13a4ea8d 280 delete[] wxBuffer;
0e320a79
DW
281 wxExit();
282}
283
284// Fatal error: pop up message box and abort
10e5b930
DW
285void wxFatalError(
286 const wxString& rMsg
287, const wxString& rTitle
288)
289{
290 unsigned long ulRc;
291
292 ulRc = ::WinMessageBox( HWND_DESKTOP
293 ,NULL
294 ,WXSTRINGCAST rMsg
295 ,WXSTRINGCAST rTitle
296 ,0
297 ,MB_NOICON | MB_OK
298 );
299 DosExit(EXIT_PROCESS, ulRc);
0e320a79
DW
300}
301
302// Emit a beeeeeep
303void wxBell()
304{
45fcbf3b 305 DosBeep(1000,1000); // 1kHz during 1 sec.
0e320a79
DW
306}
307
45fcbf3b
DW
308// Chris Breeze 27/5/98: revised WIN32 code to
309// detect WindowsNT correctly
10e5b930
DW
310int wxGetOsVersion(
311 int* pMajorVsn
312, int* pMinorVsn
313)
314{
315 ULONG ulSysInfo[QSV_MAX] = {0};
5d4b632b
DW
316 APIRET ulrc;
317
318 ulrc = ::DosQuerySysInfo( 1L
319 ,QSV_MAX
320 ,(PVOID)ulSysInfo
321 ,sizeof(ULONG) * QSV_MAX
322 );
323 if (ulrc == 0L)
10e5b930
DW
324 {
325 *pMajorVsn = ulSysInfo[QSV_VERSION_MAJOR];
5d4b632b 326 *pMajorVsn = *pMajorVsn/10;
10e5b930
DW
327 *pMinorVsn = ulSysInfo[QSV_VERSION_MINOR];
328 return wxWINDOWS_OS2;
329 }
330 return wxWINDOWS; // error if we get here, return generic value
0e320a79
DW
331}
332
333// Reading and writing resources (eg WIN.INI, .Xdefaults)
334#if wxUSE_RESOURCES
10e5b930
DW
335bool wxWriteResource(
336 const wxString& rSection
337, const wxString& rEntry
338, const wxString& rValue
339, const wxString& rFile
340)
0e320a79 341{
9dea36ef
DW
342 HAB hab = 0;
343 HINI hIni = 0;
78d50441 344
10e5b930 345 if (rFile != "")
78d50441 346 {
10e5b930 347 hIni = ::PrfOpenProfile(hab, (PSZ)WXSTRINGCAST rFile);
78d50441
DW
348 if (hIni != 0L)
349 {
350 return (::PrfWriteProfileString( hIni
10e5b930
DW
351 ,(PSZ)WXSTRINGCAST rSection
352 ,(PSZ)WXSTRINGCAST rEntry
353 ,(PSZ)WXSTRINGCAST rValue
78d50441
DW
354 ));
355 }
356 }
357 else
358 return (::PrfWriteProfileString( HINI_PROFILE
10e5b930
DW
359 ,(PSZ)WXSTRINGCAST rSection
360 ,(PSZ)WXSTRINGCAST rEntry
361 ,(PSZ)WXSTRINGCAST rValue
78d50441 362 ));
d90895ac 363 return FALSE;
0e320a79
DW
364}
365
10e5b930
DW
366bool wxWriteResource(
367 const wxString& rSection
368, const wxString& rEntry
369, float fValue
370, const wxString& rFile
371)
372{
373 wxChar zBuf[50];
374
375 wxSprintf(zBuf, "%.4f", fValue);
376 return wxWriteResource( rSection
377 ,rEntry
378 ,zBuf
379 ,rFile
380 );
0e320a79
DW
381}
382
1be7f92a
DW
383bool wxWriteResource(
384 const wxString& rSection
385, const wxString& rEntry
386, long lValue
387, const wxString& rFile
388)
0e320a79 389{
1be7f92a
DW
390 wxChar zBuf[50];
391
392 wxSprintf(zBuf, "%ld", lValue);
393 return wxWriteResource( rSection
394 ,rEntry
395 ,zBuf
396 ,rFile
397 );
0e320a79
DW
398}
399
1be7f92a
DW
400bool wxWriteResource(
401 const wxString& rSection
402, const wxString& rEntry
403, int lValue
404, const wxString& rFile
405)
0e320a79 406{
1be7f92a
DW
407 wxChar zBuf[50];
408
409 wxSprintf(zBuf, "%d", lValue);
410 return wxWriteResource( rSection
411 ,rEntry
412 ,zBuf
413 ,rFile
414 );
0e320a79
DW
415}
416
1be7f92a
DW
417bool wxGetResource(
418 const wxString& rSection
419, const wxString& rEntry
420, wxChar** ppValue
421, const wxString& rFile
422)
0e320a79 423{
9dea36ef
DW
424 HAB hab = 0;
425 HINI hIni = 0;
e8fd750b
DW
426 wxChar zDefunkt[] = _T("$$default");
427 char zBuf[1000];
78d50441 428
1be7f92a 429 if (rFile != "")
78d50441 430 {
1be7f92a 431 hIni = ::PrfOpenProfile(hab, (PSZ)WXSTRINGCAST rFile);
78d50441
DW
432 if (hIni != 0L)
433 {
434 ULONG n = ::PrfQueryProfileString( hIni
1be7f92a
DW
435 ,(PSZ)WXSTRINGCAST rSection
436 ,(PSZ)WXSTRINGCAST rEntry
437 ,(PSZ)zDefunkt
e8fd750b 438 ,(PVOID)zBuf
78d50441
DW
439 ,1000
440 );
e8fd750b 441 if (zBuf == NULL)
78d50441 442 return FALSE;
e8fd750b
DW
443 if (n == 0L || wxStrcmp(zBuf, zDefunkt) == 0)
444 return FALSE;
445 zBuf[n-1] = '\0';
78d50441
DW
446 }
447 else
448 return FALSE;
449 }
450 else
451 {
452 ULONG n = ::PrfQueryProfileString( HINI_PROFILE
1be7f92a
DW
453 ,(PSZ)WXSTRINGCAST rSection
454 ,(PSZ)WXSTRINGCAST rEntry
455 ,(PSZ)zDefunkt
e8fd750b 456 ,(PVOID)zBuf
78d50441
DW
457 ,1000
458 );
e8fd750b
DW
459 if (zBuf == NULL)
460 return FALSE;
461 if (n == 0L || wxStrcmp(zBuf, zDefunkt) == 0)
78d50441 462 return FALSE;
e8fd750b 463 zBuf[n-1] = '\0';
78d50441 464 }
e8fd750b 465 strcpy((char*)*ppValue, zBuf);
78d50441 466 return TRUE;
d90895ac 467}
0e320a79 468
1be7f92a
DW
469bool wxGetResource(
470 const wxString& rSection
471, const wxString& rEntry
472, float* pValue
473, const wxString& rFile
474)
0e320a79 475{
1be7f92a 476 wxChar* zStr = NULL;
13a4ea8d
DW
477
478 zStr = new wxChar[1000];
1be7f92a
DW
479 bool bSucc = wxGetResource( rSection
480 ,rEntry
481 ,(wxChar **)&zStr
482 ,rFile
483 );
484
485 if (bSucc)
486 {
487 *pValue = (float)wxStrtod(zStr, NULL);
488 delete[] zStr;
489 return TRUE;
490 }
13a4ea8d
DW
491 else
492 {
493 delete[] zStr;
494 return FALSE;
495 }
0e320a79
DW
496}
497
1be7f92a
DW
498bool wxGetResource(
499 const wxString& rSection
500, const wxString& rEntry
501, long* pValue
502, const wxString& rFile
503)
0e320a79 504{
1be7f92a 505 wxChar* zStr = NULL;
13a4ea8d
DW
506
507 zStr = new wxChar[1000];
1be7f92a
DW
508 bool bSucc = wxGetResource( rSection
509 ,rEntry
510 ,(wxChar **)&zStr
511 ,rFile
512 );
513
514 if (bSucc)
515 {
516 *pValue = wxStrtol(zStr, NULL, 10);
517 delete[] zStr;
518 return TRUE;
519 }
13a4ea8d
DW
520 else
521 {
522 delete[] zStr;
523 return FALSE;
524 }
0e320a79
DW
525}
526
1be7f92a
DW
527bool wxGetResource(
528 const wxString& rSection
529, const wxString& rEntry
530, int* pValue
531, const wxString& rFile
532)
0e320a79 533{
1be7f92a 534 wxChar* zStr = NULL;
13a4ea8d
DW
535
536 zStr = new wxChar[1000];
1be7f92a
DW
537 bool bSucc = wxGetResource( rSection
538 ,rEntry
539 ,(wxChar **)&zStr
540 ,rFile
541 );
542
543 if (bSucc)
544 {
545 *pValue = (int)wxStrtol(zStr, NULL, 10);
546 delete[] zStr;
547 return TRUE;
548 }
13a4ea8d
DW
549 else
550 {
551 delete[] zStr;
552 return FALSE;
553 }
0e320a79
DW
554}
555#endif // wxUSE_RESOURCES
556
45fcbf3b
DW
557// ---------------------------------------------------------------------------
558// helper functions for showing a "busy" cursor
559// ---------------------------------------------------------------------------
560
561HCURSOR gs_wxBusyCursor = 0; // new, busy cursor
562HCURSOR gs_wxBusyCursorOld = 0; // old cursor
563static int gs_wxBusyCursorCount = 0;
0e320a79
DW
564
565// Set the cursor to the busy cursor for all windows
1be7f92a
DW
566void wxBeginBusyCursor(
567 wxCursor* pCursor
568)
0e320a79 569{
45fcbf3b
DW
570 if ( gs_wxBusyCursorCount++ == 0 )
571 {
1be7f92a 572 gs_wxBusyCursor = (HCURSOR)pCursor->GetHCURSOR();
45fcbf3b
DW
573 ::WinSetPointer(HWND_DESKTOP, (HPOINTER)gs_wxBusyCursor);
574 }
575 //else: nothing to do, already set
0e320a79
DW
576}
577
578// Restore cursor to normal
579void wxEndBusyCursor()
580{
1be7f92a
DW
581 wxCHECK_RET( gs_wxBusyCursorCount > 0
582 ,_T("no matching wxBeginBusyCursor() for wxEndBusyCursor()")
583 );
45fcbf3b 584
1be7f92a 585 if (--gs_wxBusyCursorCount == 0)
45fcbf3b
DW
586 {
587 ::WinSetPointer(HWND_DESKTOP, (HPOINTER)gs_wxBusyCursorOld);
588 gs_wxBusyCursorOld = 0;
589 }
0e320a79
DW
590}
591
592// TRUE if we're between the above two calls
593bool wxIsBusy()
594{
1be7f92a 595 return (gs_wxBusyCursorCount > 0);
45fcbf3b
DW
596}
597
598// ---------------------------------------------------------------------------
1be7f92a
DW
599const wxChar* wxGetHomeDir(
600 wxString* pStr
601)
45fcbf3b 602{
1be7f92a 603 wxString& rStrDir = *pStr;
0e320a79 604
1be7f92a
DW
605 // OS/2 has no idea about home,
606 // so use the working directory instead?
45fcbf3b 607
1be7f92a 608 // 256 was taken from os2def.h
45fcbf3b
DW
609#ifndef MAX_PATH
610# define MAX_PATH 256
611#endif
612
1be7f92a
DW
613 char zDirName[256];
614 ULONG ulDirLen;
45fcbf3b 615
1be7f92a
DW
616 ::DosQueryCurrentDir(0, zDirName, &ulDirLen);
617 rStrDir = zDirName;
618 return rStrDir.c_str();
45fcbf3b
DW
619}
620
10e5b930 621// Hack for OS/2
1be7f92a
DW
622wxChar* wxGetUserHome (
623 const wxString& rUser
624)
625{
626 wxChar* zHome;
627 wxString sUser1(rUser);
628
13a4ea8d 629 wxBuffer = new wxChar[256];
06298235 630#ifndef __EMX__
1be7f92a 631 if (sUser1 != _T(""))
45fcbf3b 632 {
1be7f92a
DW
633 wxChar zTmp[64];
634
635 if (wxGetUserId( zTmp
636 ,sizeof(zTmp)/sizeof(char)
637 ))
638 {
639 // Guests belong in the temp dir
640 if (wxStricmp(zTmp, _T("annonymous")) == 0)
641 {
642 if ((zHome = wxGetenv(_T("TMP"))) != NULL ||
643 (zHome = wxGetenv(_T("TMPDIR"))) != NULL ||
644 (zHome = wxGetenv(_T("TEMP"))) != NULL)
13a4ea8d 645 delete[] wxBuffer;
1be7f92a
DW
646 return *zHome ? zHome : (wxChar*)_T("\\");
647 }
648 if (wxStricmp(zTmp, WXSTRINGCAST sUser1) == 0)
649 sUser1 = _T("");
650 }
45fcbf3b 651 }
06298235 652#endif
1be7f92a 653 if (sUser1 == _T(""))
13a4ea8d 654 {
1be7f92a
DW
655 if ((zHome = wxGetenv(_T("HOME"))) != NULL)
656 {
657 wxStrcpy(wxBuffer, zHome);
658 Unix2DosFilename(wxBuffer);
13a4ea8d
DW
659 wxStrcpy(zHome, wxBuffer);
660 delete[] wxBuffer;
661 return zHome;
1be7f92a 662 }
13a4ea8d
DW
663 }
664 delete[] wxBuffer;
665 return NULL; // No home known!
0e320a79
DW
666}
667
668// Check whether this window wants to process messages, e.g. Stop button
669// in long calculations.
1be7f92a
DW
670bool wxCheckForInterrupt(
671 wxWindow* pWnd
672)
0e320a79 673{
1be7f92a
DW
674 if(pWnd)
675 {
676 QMSG vMsg;
9dea36ef
DW
677 HAB hab = 0;
678 HWND hwndFilter = NULLHANDLE;
1be7f92a 679 HWND hwndWin= (HWND) pWnd->GetHWND();
45fcbf3b 680
1be7f92a
DW
681 while(::WinPeekMsg(hab, &vMsg, hwndFilter, 0, 0, PM_REMOVE))
682 {
683 ::WinDispatchMsg(hab, &vMsg);
684 }
685 return TRUE;//*** temporary?
686 }
687 else
45fcbf3b 688 {
1be7f92a
DW
689 wxFAIL_MSG(_T("pWnd==NULL !!!"));
690 return FALSE;//*** temporary?
45fcbf3b 691 }
45fcbf3b
DW
692}
693
1be7f92a
DW
694void wxGetMousePosition(
695 int* pX
696, int* pY
697)
0e320a79 698{
1be7f92a
DW
699 POINTL vPt;
700
701 ::WinQueryPointerPos(HWND_DESKTOP, &vPt);
702 *pX = vPt.x;
703 *pY = vPt.y;
0e320a79
DW
704};
705
706// Return TRUE if we have a colour display
707bool wxColourDisplay()
708{
1be7f92a
DW
709 HPS hpsScreen;
710 HDC hdcScreen;
711 LONG lColors;
712
713 hpsScreen = ::WinGetScreenPS(HWND_DESKTOP);
714 hdcScreen = ::GpiQueryDevice(hpsScreen);
715 ::DevQueryCaps(hdcScreen, CAPS_COLORS, 1L, &lColors);
716 return(lColors > 1L);
0e320a79
DW
717}
718
719// Returns depth of screen
720int wxDisplayDepth()
721{
1be7f92a
DW
722 HPS hpsScreen;
723 HDC hdcScreen;
724 LONG lPlanes;
725 LONG lBitsPerPixel;
726 LONG nDepth;
727
728 hpsScreen = ::WinGetScreenPS(HWND_DESKTOP);
729 hdcScreen = ::GpiQueryDevice(hpsScreen);
730 ::DevQueryCaps(hdcScreen, CAPS_COLOR_PLANES, 1L, &lPlanes);
731 ::DevQueryCaps(hdcScreen, CAPS_COLOR_BITCOUNT, 1L, &lBitsPerPixel);
732
733 nDepth = (int)(lPlanes * lBitsPerPixel);
734 DevCloseDC(hdcScreen);
d90895ac 735 return (nDepth);
0e320a79
DW
736}
737
738// Get size of display
1be7f92a
DW
739void wxDisplaySize(
740 int* pWidth
741, int* pHeight
742)
0e320a79 743{
1be7f92a
DW
744 HPS hpsScreen;
745 HDC hdcScreen;
892b89f3
DW
746 LONG lWidth;
747 LONG lHeight;
45fcbf3b 748
1be7f92a
DW
749 hpsScreen = ::WinGetScreenPS(HWND_DESKTOP);
750 hdcScreen = ::GpiQueryDevice(hpsScreen);
892b89f3
DW
751 ::DevQueryCaps(hdcScreen, CAPS_WIDTH, 1L, &lWidth);
752 ::DevQueryCaps(hdcScreen, CAPS_HEIGHT, 1L, &lHeight);
1be7f92a 753 DevCloseDC(hdcScreen);
892b89f3
DW
754 *pWidth = (int)lWidth;
755 *pHeight = (int)lHeight;
45fcbf3b
DW
756}
757
1be7f92a
DW
758bool wxDirExists(
759 const wxString& rDir
760)
45fcbf3b 761{
1be7f92a 762 return (::DosSetCurrentDir(WXSTRINGCAST rDir));
45fcbf3b
DW
763}
764
765// ---------------------------------------------------------------------------
766// window information functions
767// ---------------------------------------------------------------------------
768
1be7f92a
DW
769wxString WXDLLEXPORT wxGetWindowText(
770 WXHWND hWnd
771)
45fcbf3b 772{
1be7f92a
DW
773 wxString vStr;
774 long lLen = ::WinQueryWindowTextLength((HWND)hWnd) + 1;
775
776 ::WinQueryWindowText((HWND)hWnd, lLen, vStr.GetWriteBuf((int)lLen));
777 vStr.UngetWriteBuf();
45fcbf3b 778
1be7f92a 779 return vStr;
45fcbf3b
DW
780}
781
1be7f92a
DW
782wxString WXDLLEXPORT wxGetWindowClass(
783 WXHWND hWnd
784)
45fcbf3b 785{
1be7f92a
DW
786 wxString vStr;
787 int nLen = 256; // some starting value
45fcbf3b
DW
788
789 for ( ;; )
790 {
1be7f92a 791 int nCount = ::WinQueryClassName((HWND)hWnd, nLen, vStr.GetWriteBuf(nLen));
45fcbf3b 792
1be7f92a
DW
793 vStr.UngetWriteBuf();
794 if (nCount == nLen )
45fcbf3b
DW
795 {
796 // the class name might have been truncated, retry with larger
797 // buffer
1be7f92a 798 nLen *= 2;
45fcbf3b
DW
799 }
800 else
801 {
802 break;
803 }
804 }
1be7f92a 805 return vStr;
45fcbf3b
DW
806}
807
1be7f92a
DW
808WXWORD WXDLLEXPORT wxGetWindowId(
809 WXHWND hWnd
810)
45fcbf3b
DW
811{
812 return ::WinQueryWindowUShort((HWND)hWnd, QWS_ID);
0e320a79
DW
813}
814
914589c2
DW
815wxString WXDLLEXPORT wxPMErrorToStr(
816 ERRORID vError
817)
818{
819 wxString sError;
820
821 //
822 // Remove the high order byte -- it is useless
823 //
824 vError &= 0x0000ffff;
825 switch(vError)
826 {
827 case PMERR_INVALID_HWND:
828 sError = wxT("Invalid window handle specified");
829 break;
830
831 case PMERR_INVALID_FLAG:
832 sError = wxT("Invalid flag bit set");
833 break;
834
835 case PMERR_NO_MSG_QUEUE:
836 sError = wxT("No message queue available");
837 break;
838
839 case PMERR_INVALID_PARM:
840 sError = wxT("Parameter contained invalid data");
841 break;
842
843 case PMERR_INVALID_PARAMETERS:
844 sError = wxT("Parameter value is out of range");
845 break;
846
847 case PMERR_PARAMETER_OUT_OF_RANGE:
848 sError = wxT("Parameter value is out of range");
849 break;
850
851 case PMERR_INVALID_INTEGER_ATOM:
852 sError = wxT("Not a valid atom");
853 break;
854
855 case PMERR_INVALID_HATOMTBL:
856 sError = wxT("Atom table handle is invalid");
857 break;
858
859 case PMERR_INVALID_ATOM_NAME:
860 sError = wxT("Not a valid atom name");
861 break;
862
863 case PMERR_ATOM_NAME_NOT_FOUND:
864 sError = wxT("Valid name format, but cannot find name in atom table");
865 break;
866
867 default:
868 sError = wxT("Unknown error");
869 }
870 return(sError);
871} // end of wxPMErrorToStr
872
873