]> git.saurik.com Git - wxWidgets.git/blob - src/os2/utils.cpp
workaround for MGL's weirdness: won't render rectangles of w=1 or h=1
[wxWidgets.git] / src / os2 / utils.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: utils.cpp
3 // Purpose: Various utilities
4 // Author: David Webster
5 // Modified by:
6 // Created: 09/17/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
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
26 #include <ctype.h>
27 #ifdef __EMX__
28 #include <dirent.h>
29 #endif
30
31 #include "wx/log.h"
32
33 #include <io.h>
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <stdarg.h>
40
41 #define PURE_32
42
43 #ifndef __EMX__
44 #include <upm.h>
45 #include <netcons.h>
46 #include <netbios.h>
47 #endif
48
49 static const wxChar WX_SECTION[] = _T("wxWindows");
50 static const wxChar eHOSTNAME[] = _T("HostName");
51 static const wxChar eUSERID[] = _T("UserId");
52 static const wxChar eUSERNAME[] = _T("UserName");
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
59 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
60 bool wxGetHostName(
61 wxChar* zBuf
62 , int nMaxSize
63 )
64 {
65 #if wxUSE_NET_API
66 char zServer[256];
67 char zComputer[256];
68 unsigned long ulLevel = 0;
69 unsigned char* zBuffer = NULL;
70 unsigned long ulBuffer = 256;
71 unsigned long* pulTotalAvail = NULL;
72
73 NetBios32GetInfo( (const unsigned char*)zServer
74 ,(const unsigned char*)zComputer
75 ,ulLevel
76 ,zBuffer
77 ,ulBuffer
78 ,pulTotalAvail
79 );
80 strcpy(zBuf, zServer);
81 #else
82 wxChar* zSysname;
83 const wxChar* zDefaultHost = _T("noname");
84
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');
98 #endif
99 return *zBuf ? TRUE : FALSE;
100 }
101
102 // Get user ID e.g. jacs
103 bool wxGetUserId(
104 wxChar* zBuf
105 , int nType
106 )
107 {
108 #if defined(__VISAGECPP__)
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;
113 #endif
114 return FALSE;
115 }
116
117 bool wxGetUserName(
118 wxChar* zBuf
119 , int nMaxSize
120 )
121 {
122 #ifdef USE_NET_API
123 wxGetUserId( zBuf
124 ,nMaxSize
125 );
126 #else
127 wxStrncpy(zBuf, _T("Unknown User"), nMaxSize);
128 #endif
129 return TRUE;
130 }
131
132 int wxKill(
133 long lPid
134 , wxSignal eSig
135 , wxKillError* peError
136 )
137 {
138 return((int)::DosKillProcess(0, (PID)lPid));
139 }
140
141 //
142 // Execute a program in an Interactive Shell
143 //
144 bool wxShell(
145 const wxString& rCommand
146 )
147 {
148 wxChar* zShell = _T("CMD.EXE");
149 wxString sInputs;
150 wxChar zTmp[255];
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
166 sInputs = "/C " + rCommand;
167 SData.PgmInputs = (BYTE*)sInputs.c_str();
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);
184 if (rc == 0 || rc == 457) // NO_ERROR or SMG_START_IN_BACKGROUND
185 {
186 PTIB ptib;
187 PPIB ppib;
188
189 ::DosGetInfoBlocks(&ptib, &ppib);
190
191 ::DosWaitChild( DCWA_PROCESS
192 ,DCWW_WAIT
193 ,&vResult
194 ,&ppib->pib_ulpid
195 ,vPid
196 );
197 }
198 return (rc != 0);
199 }
200
201 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
202 long wxGetFreeMemory()
203 {
204 void* pMemptr = NULL;
205 ULONG lSize;
206 ULONG lMemFlags;
207 APIRET rc;
208
209 lMemFlags = PAG_FREE;
210 rc = ::DosQueryMem(pMemptr, &lSize, &lMemFlags);
211 if (rc != 0)
212 return -1L;
213 return (long)lSize;
214 }
215
216 // ----------------------------------------------------------------------------
217 // env vars
218 // ----------------------------------------------------------------------------
219
220 bool 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
235 bool 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
259 // Sleep for nSecs seconds. Attempt a Windows implementation using timers.
260 static bool inTimer = FALSE;
261
262 class wxSleepTimer: public wxTimer
263 {
264 public:
265 inline void Notify()
266 {
267 inTimer = FALSE;
268 Stop();
269 }
270 };
271
272 static wxTimer* wxTheSleepTimer = NULL;
273
274 void wxUsleep(
275 unsigned long ulMilliseconds
276 )
277 {
278 ::DosSleep(ulMilliseconds/1000l);
279 }
280
281 void wxSleep(
282 int nSecs
283 )
284 {
285 ::DosSleep(1000 * nSecs);
286 }
287
288 // Consume all events until no more left
289 void wxFlushEvents()
290 {
291 // wxYield();
292 }
293
294 // Output a debug mess., in a system dependent fashion.
295 void 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);
307 }
308
309 // Non-fatal error: pop up message box and (possibly) continue
310 void wxError(
311 const wxString& rMsg
312 , const wxString& rTitle
313 )
314 {
315 wxBuffer = new wxChar[256];
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)
324 delete[] wxBuffer;
325 wxExit();
326 }
327
328 // Fatal error: pop up message box and abort
329 void 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);
344 }
345
346 // Emit a beeeeeep
347 void wxBell()
348 {
349 DosBeep(1000,1000); // 1kHz during 1 sec.
350 }
351
352 // Chris Breeze 27/5/98: revised WIN32 code to
353 // detect WindowsNT correctly
354 int wxGetOsVersion(
355 int* pMajorVsn
356 , int* pMinorVsn
357 )
358 {
359 ULONG ulSysInfo[QSV_MAX] = {0};
360 APIRET ulrc;
361
362 ulrc = ::DosQuerySysInfo( 1L
363 ,QSV_MAX
364 ,(PVOID)ulSysInfo
365 ,sizeof(ULONG) * QSV_MAX
366 );
367 if (ulrc == 0L)
368 {
369 *pMajorVsn = ulSysInfo[QSV_VERSION_MAJOR];
370 *pMajorVsn = *pMajorVsn/10;
371 *pMinorVsn = ulSysInfo[QSV_VERSION_MINOR];
372 return wxWINDOWS_OS2;
373 }
374 return wxWINDOWS; // error if we get here, return generic value
375 }
376
377 // Reading and writing resources (eg WIN.INI, .Xdefaults)
378 #if wxUSE_RESOURCES
379 bool wxWriteResource(
380 const wxString& rSection
381 , const wxString& rEntry
382 , const wxString& rValue
383 , const wxString& rFile
384 )
385 {
386 HAB hab = 0;
387 HINI hIni = 0;
388
389 if (rFile != "")
390 {
391 hIni = ::PrfOpenProfile(hab, (PSZ)WXSTRINGCAST rFile);
392 if (hIni != 0L)
393 {
394 return (::PrfWriteProfileString( hIni
395 ,(PSZ)WXSTRINGCAST rSection
396 ,(PSZ)WXSTRINGCAST rEntry
397 ,(PSZ)WXSTRINGCAST rValue
398 ));
399 }
400 }
401 else
402 return (::PrfWriteProfileString( HINI_PROFILE
403 ,(PSZ)WXSTRINGCAST rSection
404 ,(PSZ)WXSTRINGCAST rEntry
405 ,(PSZ)WXSTRINGCAST rValue
406 ));
407 return FALSE;
408 }
409
410 bool 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 );
425 }
426
427 bool wxWriteResource(
428 const wxString& rSection
429 , const wxString& rEntry
430 , long lValue
431 , const wxString& rFile
432 )
433 {
434 wxChar zBuf[50];
435
436 wxSprintf(zBuf, "%ld", lValue);
437 return wxWriteResource( rSection
438 ,rEntry
439 ,zBuf
440 ,rFile
441 );
442 }
443
444 bool wxWriteResource(
445 const wxString& rSection
446 , const wxString& rEntry
447 , int lValue
448 , const wxString& rFile
449 )
450 {
451 wxChar zBuf[50];
452
453 wxSprintf(zBuf, "%d", lValue);
454 return wxWriteResource( rSection
455 ,rEntry
456 ,zBuf
457 ,rFile
458 );
459 }
460
461 bool wxGetResource(
462 const wxString& rSection
463 , const wxString& rEntry
464 , wxChar** ppValue
465 , const wxString& rFile
466 )
467 {
468 HAB hab = 0;
469 HINI hIni = 0;
470 wxChar zDefunkt[] = _T("$$default");
471 char zBuf[1000];
472
473 if (rFile != "")
474 {
475 hIni = ::PrfOpenProfile(hab, (PSZ)WXSTRINGCAST rFile);
476 if (hIni != 0L)
477 {
478 ULONG n = ::PrfQueryProfileString( hIni
479 ,(PSZ)WXSTRINGCAST rSection
480 ,(PSZ)WXSTRINGCAST rEntry
481 ,(PSZ)zDefunkt
482 ,(PVOID)zBuf
483 ,1000
484 );
485 if (zBuf == NULL)
486 return FALSE;
487 if (n == 0L || wxStrcmp(zBuf, zDefunkt) == 0)
488 return FALSE;
489 zBuf[n-1] = '\0';
490 }
491 else
492 return FALSE;
493 }
494 else
495 {
496 ULONG n = ::PrfQueryProfileString( HINI_PROFILE
497 ,(PSZ)WXSTRINGCAST rSection
498 ,(PSZ)WXSTRINGCAST rEntry
499 ,(PSZ)zDefunkt
500 ,(PVOID)zBuf
501 ,1000
502 );
503 if (zBuf == NULL)
504 return FALSE;
505 if (n == 0L || wxStrcmp(zBuf, zDefunkt) == 0)
506 return FALSE;
507 zBuf[n-1] = '\0';
508 }
509 strcpy((char*)*ppValue, zBuf);
510 return TRUE;
511 }
512
513 bool wxGetResource(
514 const wxString& rSection
515 , const wxString& rEntry
516 , float* pValue
517 , const wxString& rFile
518 )
519 {
520 wxChar* zStr = NULL;
521
522 zStr = new wxChar[1000];
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 }
535 else
536 {
537 delete[] zStr;
538 return FALSE;
539 }
540 }
541
542 bool wxGetResource(
543 const wxString& rSection
544 , const wxString& rEntry
545 , long* pValue
546 , const wxString& rFile
547 )
548 {
549 wxChar* zStr = NULL;
550
551 zStr = new wxChar[1000];
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 }
564 else
565 {
566 delete[] zStr;
567 return FALSE;
568 }
569 }
570
571 bool wxGetResource(
572 const wxString& rSection
573 , const wxString& rEntry
574 , int* pValue
575 , const wxString& rFile
576 )
577 {
578 wxChar* zStr = NULL;
579
580 zStr = new wxChar[1000];
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 }
593 else
594 {
595 delete[] zStr;
596 return FALSE;
597 }
598 }
599 #endif // wxUSE_RESOURCES
600
601 // ---------------------------------------------------------------------------
602 // helper functions for showing a "busy" cursor
603 // ---------------------------------------------------------------------------
604
605 HCURSOR gs_wxBusyCursor = 0; // new, busy cursor
606 HCURSOR gs_wxBusyCursorOld = 0; // old cursor
607 static int gs_wxBusyCursorCount = 0;
608
609 // Set the cursor to the busy cursor for all windows
610 void wxBeginBusyCursor(
611 wxCursor* pCursor
612 )
613 {
614 if ( gs_wxBusyCursorCount++ == 0 )
615 {
616 gs_wxBusyCursor = (HCURSOR)pCursor->GetHCURSOR();
617 ::WinSetPointer(HWND_DESKTOP, (HPOINTER)gs_wxBusyCursor);
618 }
619 //else: nothing to do, already set
620 }
621
622 // Restore cursor to normal
623 void wxEndBusyCursor()
624 {
625 wxCHECK_RET( gs_wxBusyCursorCount > 0
626 ,_T("no matching wxBeginBusyCursor() for wxEndBusyCursor()")
627 );
628
629 if (--gs_wxBusyCursorCount == 0)
630 {
631 ::WinSetPointer(HWND_DESKTOP, (HPOINTER)gs_wxBusyCursorOld);
632 gs_wxBusyCursorOld = 0;
633 }
634 }
635
636 // TRUE if we're between the above two calls
637 bool wxIsBusy()
638 {
639 return (gs_wxBusyCursorCount > 0);
640 }
641
642 // ---------------------------------------------------------------------------
643 const wxChar* wxGetHomeDir(
644 wxString* pStr
645 )
646 {
647 wxString& rStrDir = *pStr;
648
649 // OS/2 has no idea about home,
650 // so use the working directory instead?
651
652 // 256 was taken from os2def.h
653 #ifndef MAX_PATH
654 # define MAX_PATH 256
655 #endif
656
657 char zDirName[256];
658 ULONG ulDirLen;
659
660 ::DosQueryCurrentDir(0, zDirName, &ulDirLen);
661 rStrDir = zDirName;
662 return rStrDir.c_str();
663 }
664
665 // Hack for OS/2
666 wxChar* wxGetUserHome (
667 const wxString& rUser
668 )
669 {
670 wxChar* zHome;
671 wxString sUser1(rUser);
672
673 wxBuffer = new wxChar[256];
674 #ifndef __EMX__
675 if (sUser1 != _T(""))
676 {
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)
689 delete[] wxBuffer;
690 return *zHome ? zHome : (wxChar*)_T("\\");
691 }
692 if (wxStricmp(zTmp, WXSTRINGCAST sUser1) == 0)
693 sUser1 = _T("");
694 }
695 }
696 #endif
697 if (sUser1 == _T(""))
698 {
699 if ((zHome = wxGetenv(_T("HOME"))) != NULL)
700 {
701 wxStrcpy(wxBuffer, zHome);
702 Unix2DosFilename(wxBuffer);
703 wxStrcpy(zHome, wxBuffer);
704 delete[] wxBuffer;
705 return zHome;
706 }
707 }
708 delete[] wxBuffer;
709 return NULL; // No home known!
710 }
711
712 // Check whether this window wants to process messages, e.g. Stop button
713 // in long calculations.
714 bool wxCheckForInterrupt(
715 wxWindow* pWnd
716 )
717 {
718 if(pWnd)
719 {
720 QMSG vMsg;
721 HAB hab = 0;
722 HWND hwndFilter = NULLHANDLE;
723 HWND hwndWin= (HWND) pWnd->GetHWND();
724
725 while(::WinPeekMsg(hab, &vMsg, hwndFilter, 0, 0, PM_REMOVE))
726 {
727 ::WinDispatchMsg(hab, &vMsg);
728 }
729 return TRUE;//*** temporary?
730 }
731 else
732 {
733 wxFAIL_MSG(_T("pWnd==NULL !!!"));
734 return FALSE;//*** temporary?
735 }
736 }
737
738 void wxGetMousePosition(
739 int* pX
740 , int* pY
741 )
742 {
743 POINTL vPt;
744
745 ::WinQueryPointerPos(HWND_DESKTOP, &vPt);
746 *pX = vPt.x;
747 *pY = vPt.y;
748 };
749
750 // Return TRUE if we have a colour display
751 bool wxColourDisplay()
752 {
753 #if 0
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);
762 #else
763 // I don't see how the PM display could not be color. Besides, this
764 // was leaking DCs and PSs!!! MN
765 return TRUE;
766 #endif
767 }
768
769 // Returns depth of screen
770 int wxDisplayDepth()
771 {
772 HPS hpsScreen;
773 HDC hdcScreen;
774 LONG lPlanes;
775 LONG lBitsPerPixel;
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 }
790 return (nDepth);
791 }
792
793 // Get size of display
794 void wxDisplaySize(
795 int* pWidth
796 , int* pHeight
797 )
798 {
799 HPS hpsScreen;
800 HDC hdcScreen;
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 }
813 *pWidth = (int)lWidth;
814 *pHeight = (int)lHeight;
815 }
816
817 void 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 );
840 ::DevCloseDC(hdcScreen);
841 ::WinReleasePS(hpsScreen);
842 }
843
844 void 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
856 bool wxDirExists(
857 const wxString& rDir
858 )
859 {
860 return (::DosSetCurrentDir(WXSTRINGCAST rDir));
861 }
862
863 // ---------------------------------------------------------------------------
864 // window information functions
865 // ---------------------------------------------------------------------------
866
867 wxString WXDLLEXPORT wxGetWindowText(
868 WXHWND hWnd
869 )
870 {
871 wxString vStr;
872 long lLen = ::WinQueryWindowTextLength((HWND)hWnd) + 1;
873
874 ::WinQueryWindowText((HWND)hWnd, lLen, vStr.GetWriteBuf((int)lLen));
875 vStr.UngetWriteBuf();
876
877 return vStr;
878 }
879
880 wxString WXDLLEXPORT wxGetWindowClass(
881 WXHWND hWnd
882 )
883 {
884 wxString vStr;
885 int nLen = 256; // some starting value
886
887 for ( ;; )
888 {
889 int nCount = ::WinQueryClassName((HWND)hWnd, nLen, vStr.GetWriteBuf(nLen));
890
891 vStr.UngetWriteBuf();
892 if (nCount == nLen )
893 {
894 // the class name might have been truncated, retry with larger
895 // buffer
896 nLen *= 2;
897 }
898 else
899 {
900 break;
901 }
902 }
903 return vStr;
904 }
905
906 WXWORD WXDLLEXPORT wxGetWindowId(
907 WXHWND hWnd
908 )
909 {
910 return ::WinQueryWindowUShort((HWND)hWnd, QWS_ID);
911 }
912
913 wxString 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
971 void 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 {
985 vPoint[1].x = rRect.xRight - 1;
986 vPoint[1].y = rRect.yTop - 1;
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
998 vLineBundle.lColor = 0x00FFFFFF; // WHITE
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 );
1011 vPoint[1].x = rRect.xRight - 1;
1012 vPoint[1].y = rRect.yTop - 1;
1013 ::GpiBox( hPS
1014 ,DRO_OUTLINE
1015 ,&vPoint[1]
1016 ,0L
1017 ,0L
1018 );
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
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;
1048 vPoint[1].y = rRect.yTop - 3;
1049 ::GpiLine(hPS, &vPoint[1]);
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;
1062 ::GpiLine(hPS, &vPoint[1]);
1063 }
1064 if (dwStyle & wxDOUBLE_BORDER)
1065 {
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 );
1137 }
1138 if (dwStyle & wxRAISED_BORDER)
1139 {
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]);
1207 }
1208 } // end of wxDrawBorder
1209
1210 void 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 return;
1222
1223 //
1224 // The fonts available for Presentation Params are just three
1225 // outline fonts, the rest are available to the GPI, so we must
1226 // map the families to one of these three
1227 //
1228 switch(rFont.GetFamily())
1229 {
1230 case wxSCRIPT:
1231 strcpy(zFacename, "Script");
1232 break;
1233
1234 case wxDECORATIVE:
1235 case wxROMAN:
1236 strcpy(zFacename,"Tms Rmn");
1237 break;
1238
1239 case wxTELETYPE:
1240 strcpy(zFacename, "Courier");
1241 break;
1242
1243 case wxMODERN:
1244 strcpy(zFacename, "System VIO");
1245 break;
1246
1247 case wxSWISS:
1248 strcpy(zFacename, "Helv");
1249 break;
1250
1251 case wxDEFAULT:
1252 default:
1253 strcpy(zFacename, "System VIO");
1254 break;
1255 }
1256
1257 switch(rFont.GetWeight())
1258 {
1259 default:
1260 case wxNORMAL:
1261 case wxLIGHT:
1262 zWeight[0] = '\0';
1263 break;
1264
1265 case wxBOLD:
1266 case wxFONTWEIGHT_MAX:
1267 strcpy(zWeight, "Bold");
1268 break;
1269 }
1270
1271 switch(rFont.GetStyle())
1272 {
1273 case wxITALIC:
1274 case wxSLANT:
1275 strcpy(zStyle, "Italic");
1276 break;
1277
1278 default:
1279 zStyle[0] = '\0';
1280 break;
1281 }
1282 sprintf(zFont, "%d.%s", rFont.GetPointSize(), zFacename);
1283 if (zWeight[0] != '\0')
1284 {
1285 strcat(zFont, " ");
1286 strcat(zFont, zWeight);
1287 }
1288 if (zStyle[0] != '\0')
1289 {
1290 strcat(zFont, " ");
1291 strcat(zFont, zStyle);
1292 }
1293 ::WinSetPresParam(hWnd, PP_FONTNAMESIZE, strlen(zFont) + 1, (PVOID)zFont);
1294 } // end of wxOS2SetFont