OS/2 PM Fixeups for fonts, validators, and html
[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 #include <direct.h>
28
29 #include "wx/log.h"
30
31 #include <io.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <stdarg.h>
38
39 #define INCL_DOS
40 #define INCL_PM
41 #define INCL_GPI
42 #include <os2.h>
43 #define PURE_32
44 #include <upm.h>
45 #include <netcons.h>
46 #include <netbios.h>
47
48 static const wxChar WX_SECTION[] = _T("wxWindows");
49 static const wxChar eHOSTNAME[] = _T("HostName");
50 static const wxChar eUSERID[] = _T("UserId");
51 static const wxChar eUSERNAME[] = _T("UserName");
52
53 // For the following functions we SHOULD fill in support
54 // for Windows-NT (which I don't know) as I assume it begin
55 // a POSIX Unix (so claims MS) that it has some special
56 // functions beyond those provided by WinSock
57
58 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
59 bool wxGetHostName(
60 wxChar* zBuf
61 , int nMaxSize
62 )
63 {
64 #if wxUSE_NET_API
65 char zServer[256];
66 char zComputer[256];
67 unsigned long ulLevel = 0;
68 unsigned char* zBuffer;
69 unsigned long ulBuffer;
70 unsigned long* pulTotalAvail;
71
72 NetBios32GetInfo( (const unsigned char*)zServer
73 ,(const unsigned char*)zComputer
74 ,ulLevel
75 ,zBuffer
76 ,ulBuffer
77 ,pulTotalAvail
78 );
79 strcpy(zBuf, zServer);
80 #else
81 wxChar* zSysname;
82 const wxChar* zDefaultHost = _T("noname");
83
84 if ((zSysname = wxGetenv(_T("SYSTEM_NAME"))) == NULL)
85 {
86 ULONG n = ::PrfQueryProfileString( HINI_PROFILE
87 ,(PSZ)WX_SECTION
88 ,(PSZ)eHOSTNAME
89 ,(PSZ)zDefaultHost
90 ,(void*)zBuf
91 ,(ULONG)nMaxSize - 1
92 );
93 }
94 else
95 wxStrncpy(zBuf, zSysname, nMaxSize - 1);
96 zBuf[nMaxSize] = _T('\0');
97 #endif
98 return *zBuf ? TRUE : FALSE;
99 }
100
101 // Get user ID e.g. jacs
102 bool wxGetUserId(
103 wxChar* zBuf
104 , int nType
105 )
106 {
107 long lrc;
108 // UPM procs return 0 on success
109 lrc = U32ELOCU((unsigned char*)zBuf, (unsigned long *)&nType);
110 if (lrc == 0) return TRUE;
111 return FALSE;
112 }
113
114 bool wxGetUserName(
115 wxChar* zBuf
116 , int nMaxSize
117 )
118 {
119 #ifdef USE_NET_API
120 wxGetUserId( zBuf
121 ,nMaxSize
122 );
123 #else
124 wxStrncpy(zBuf, _T("Unknown User"), nMaxSize);
125 #endif
126 return TRUE;
127 }
128
129 int wxKill(
130 long lPid
131 , int nSig
132 )
133 {
134 return((int)::DosKillProcess(0, (PID)lPid));
135 }
136
137 //
138 // Execute a program in an Interactive Shell
139 //
140 bool wxShell(
141 const wxString& rCommand
142 )
143 {
144 wxChar* zShell = _T("CMD.EXE");
145 wxString sInputs;
146 wxChar zTmp[255];
147 STARTDATA SData = {0};
148 PSZ PgmTitle = "Command Shell";
149 APIRET rc;
150 PID vPid = 0;
151 ULONG ulSessID = 0;
152 UCHAR achObjBuf[256] = {0}; //error data if DosStart fails
153 RESULTCODES vResult;
154
155 SData.Length = sizeof(STARTDATA);
156 SData.Related = SSF_RELATED_INDEPENDENT;
157 SData.FgBg = SSF_FGBG_FORE;
158 SData.TraceOpt = SSF_TRACEOPT_NONE;
159 SData.PgmTitle = PgmTitle;
160 SData.PgmName = zShell;
161
162 sInputs = "/C " + rCommand;
163 SData.PgmInputs = (BYTE*)sInputs.c_str();
164 SData.TermQ = 0;
165 SData.Environment = 0;
166 SData.InheritOpt = SSF_INHERTOPT_SHELL;
167 SData.SessionType = SSF_TYPE_WINDOWABLEVIO;
168 SData.IconFile = 0;
169 SData.PgmHandle = 0;
170 SData.PgmControl = SSF_CONTROL_VISIBLE | SSF_CONTROL_MAXIMIZE;
171 SData.InitXPos = 30;
172 SData.InitYPos = 40;
173 SData.InitXSize = 200;
174 SData.InitYSize = 140;
175 SData.Reserved = 0;
176 SData.ObjectBuffer = (char*)achObjBuf;
177 SData.ObjectBuffLen = (ULONG)sizeof(achObjBuf);
178
179 rc = ::DosStartSession(&SData, &ulSessID, &vPid);
180 if (rc == 0 || rc == 457) // NO_ERROR or SMG_START_IN_BACKGROUND
181 {
182 PTIB ptib;
183 PPIB ppib;
184
185 ::DosGetInfoBlocks(&ptib, &ppib);
186
187 ::DosWaitChild( DCWA_PROCESS
188 ,DCWW_WAIT
189 ,&vResult
190 ,&ppib->pib_ulpid
191 ,vPid
192 );
193 }
194 return (rc != 0);
195 }
196
197 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
198 long wxGetFreeMemory()
199 {
200 void* pMemptr;
201 ULONG lSize;
202 ULONG lMemFlags;
203 APIRET rc;
204
205 lMemFlags = PAG_FREE;
206 rc = ::DosQueryMem(pMemptr, &lSize, &lMemFlags);
207 if (rc != 0)
208 return -1L;
209 return (long)lSize;
210 }
211
212 // Sleep for nSecs seconds. Attempt a Windows implementation using timers.
213 static bool inTimer = FALSE;
214
215 class wxSleepTimer: public wxTimer
216 {
217 public:
218 inline void Notify()
219 {
220 inTimer = FALSE;
221 Stop();
222 }
223 };
224
225 static wxTimer* wxTheSleepTimer = NULL;
226
227 void wxUsleep(
228 unsigned long ulMilliseconds
229 )
230 {
231 ::DosSleep(ulMilliseconds);
232 }
233
234 void wxSleep(
235 int nSecs
236 )
237 {
238 ::DosSleep(1000 * nSecs);
239 }
240
241 // Consume all events until no more left
242 void wxFlushEvents()
243 {
244 // wxYield();
245 }
246
247 // Output a debug mess., in a system dependent fashion.
248 void wxDebugMsg(
249 const wxChar* zFmt ...
250 )
251 {
252 va_list vAp;
253 static wxChar zBuffer[512];
254
255 if (!wxTheApp->GetWantDebugOutput())
256 return ;
257 va_start(vAp, zFmt);
258 sprintf(zBuffer, zFmt, vAp) ;
259 va_end(vAp);
260 }
261
262 // Non-fatal error: pop up message box and (possibly) continue
263 void wxError(
264 const wxString& rMsg
265 , const wxString& rTitle
266 )
267 {
268 wxSprintf(wxBuffer, "%s\nContinue?", WXSTRINGCAST rMsg);
269 if (::WinMessageBox( HWND_DESKTOP
270 ,NULL
271 ,(PSZ)wxBuffer
272 ,(PSZ)WXSTRINGCAST rTitle
273 ,0
274 ,MB_ICONEXCLAMATION | MB_YESNO
275 ) == MBID_YES)
276 wxExit();
277 }
278
279 // Fatal error: pop up message box and abort
280 void wxFatalError(
281 const wxString& rMsg
282 , const wxString& rTitle
283 )
284 {
285 unsigned long ulRc;
286
287 ulRc = ::WinMessageBox( HWND_DESKTOP
288 ,NULL
289 ,WXSTRINGCAST rMsg
290 ,WXSTRINGCAST rTitle
291 ,0
292 ,MB_NOICON | MB_OK
293 );
294 DosExit(EXIT_PROCESS, ulRc);
295 }
296
297 // Emit a beeeeeep
298 void wxBell()
299 {
300 DosBeep(1000,1000); // 1kHz during 1 sec.
301 }
302
303 // Chris Breeze 27/5/98: revised WIN32 code to
304 // detect WindowsNT correctly
305 int wxGetOsVersion(
306 int* pMajorVsn
307 , int* pMinorVsn
308 )
309 {
310 ULONG ulSysInfo[QSV_MAX] = {0};
311 APIRET ulrc;
312
313 ulrc = ::DosQuerySysInfo( 1L
314 ,QSV_MAX
315 ,(PVOID)ulSysInfo
316 ,sizeof(ULONG) * QSV_MAX
317 );
318 if (ulrc == 0L)
319 {
320 *pMajorVsn = ulSysInfo[QSV_VERSION_MAJOR];
321 *pMajorVsn = *pMajorVsn/10;
322 *pMinorVsn = ulSysInfo[QSV_VERSION_MINOR];
323 return wxWINDOWS_OS2;
324 }
325 return wxWINDOWS; // error if we get here, return generic value
326 }
327
328 // Reading and writing resources (eg WIN.INI, .Xdefaults)
329 #if wxUSE_RESOURCES
330 bool wxWriteResource(
331 const wxString& rSection
332 , const wxString& rEntry
333 , const wxString& rValue
334 , const wxString& rFile
335 )
336 {
337 HAB hab;
338 HINI hIni;
339
340 if (rFile != "")
341 {
342 hIni = ::PrfOpenProfile(hab, (PSZ)WXSTRINGCAST rFile);
343 if (hIni != 0L)
344 {
345 return (::PrfWriteProfileString( hIni
346 ,(PSZ)WXSTRINGCAST rSection
347 ,(PSZ)WXSTRINGCAST rEntry
348 ,(PSZ)WXSTRINGCAST rValue
349 ));
350 }
351 }
352 else
353 return (::PrfWriteProfileString( HINI_PROFILE
354 ,(PSZ)WXSTRINGCAST rSection
355 ,(PSZ)WXSTRINGCAST rEntry
356 ,(PSZ)WXSTRINGCAST rValue
357 ));
358 return FALSE;
359 }
360
361 bool wxWriteResource(
362 const wxString& rSection
363 , const wxString& rEntry
364 , float fValue
365 , const wxString& rFile
366 )
367 {
368 wxChar zBuf[50];
369
370 wxSprintf(zBuf, "%.4f", fValue);
371 return wxWriteResource( rSection
372 ,rEntry
373 ,zBuf
374 ,rFile
375 );
376 }
377
378 bool wxWriteResource(
379 const wxString& rSection
380 , const wxString& rEntry
381 , long lValue
382 , const wxString& rFile
383 )
384 {
385 wxChar zBuf[50];
386
387 wxSprintf(zBuf, "%ld", lValue);
388 return wxWriteResource( rSection
389 ,rEntry
390 ,zBuf
391 ,rFile
392 );
393 }
394
395 bool wxWriteResource(
396 const wxString& rSection
397 , const wxString& rEntry
398 , int lValue
399 , const wxString& rFile
400 )
401 {
402 wxChar zBuf[50];
403
404 wxSprintf(zBuf, "%d", lValue);
405 return wxWriteResource( rSection
406 ,rEntry
407 ,zBuf
408 ,rFile
409 );
410 }
411
412 bool wxGetResource(
413 const wxString& rSection
414 , const wxString& rEntry
415 , wxChar** ppValue
416 , const wxString& rFile
417 )
418 {
419 HAB hab;
420 HINI hIni;
421 static const wxChar zDefunkt[] = _T("$$default");
422
423 if (rFile != "")
424 {
425 hIni = ::PrfOpenProfile(hab, (PSZ)WXSTRINGCAST rFile);
426 if (hIni != 0L)
427 {
428 ULONG n = ::PrfQueryProfileString( hIni
429 ,(PSZ)WXSTRINGCAST rSection
430 ,(PSZ)WXSTRINGCAST rEntry
431 ,(PSZ)zDefunkt
432 ,(void*)*ppValue
433 ,1000
434 );
435 if (n != 0L || wxStrcmp(*ppValue, zDefunkt) == 0)
436 return FALSE;
437 }
438 else
439 return FALSE;
440 }
441 else
442 {
443 ULONG n = ::PrfQueryProfileString( HINI_PROFILE
444 ,(PSZ)WXSTRINGCAST rSection
445 ,(PSZ)WXSTRINGCAST rEntry
446 ,(PSZ)zDefunkt
447 ,(void*)*ppValue
448 ,1000
449 );
450 if (n != 0L || wxStrcmp(*ppValue, zDefunkt) == 0)
451 return FALSE;
452 }
453 return TRUE;
454 }
455
456 bool wxGetResource(
457 const wxString& rSection
458 , const wxString& rEntry
459 , float* pValue
460 , const wxString& rFile
461 )
462 {
463 wxChar* zStr = NULL;
464 bool bSucc = wxGetResource( rSection
465 ,rEntry
466 ,(wxChar **)&zStr
467 ,rFile
468 );
469
470 if (bSucc)
471 {
472 *pValue = (float)wxStrtod(zStr, NULL);
473 delete[] zStr;
474 return TRUE;
475 }
476 else return FALSE;
477 }
478
479 bool wxGetResource(
480 const wxString& rSection
481 , const wxString& rEntry
482 , long* pValue
483 , const wxString& rFile
484 )
485 {
486 wxChar* zStr = NULL;
487 bool bSucc = wxGetResource( rSection
488 ,rEntry
489 ,(wxChar **)&zStr
490 ,rFile
491 );
492
493 if (bSucc)
494 {
495 *pValue = wxStrtol(zStr, NULL, 10);
496 delete[] zStr;
497 return TRUE;
498 }
499 else return FALSE;
500 }
501
502 bool wxGetResource(
503 const wxString& rSection
504 , const wxString& rEntry
505 , int* pValue
506 , const wxString& rFile
507 )
508 {
509 wxChar* zStr = NULL;
510 bool bSucc = wxGetResource( rSection
511 ,rEntry
512 ,(wxChar **)&zStr
513 ,rFile
514 );
515
516 if (bSucc)
517 {
518 *pValue = (int)wxStrtol(zStr, NULL, 10);
519 delete[] zStr;
520 return TRUE;
521 }
522 else return FALSE;
523 }
524 #endif // wxUSE_RESOURCES
525
526 // ---------------------------------------------------------------------------
527 // helper functions for showing a "busy" cursor
528 // ---------------------------------------------------------------------------
529
530 HCURSOR gs_wxBusyCursor = 0; // new, busy cursor
531 HCURSOR gs_wxBusyCursorOld = 0; // old cursor
532 static int gs_wxBusyCursorCount = 0;
533
534 // Set the cursor to the busy cursor for all windows
535 void wxBeginBusyCursor(
536 wxCursor* pCursor
537 )
538 {
539 if ( gs_wxBusyCursorCount++ == 0 )
540 {
541 gs_wxBusyCursor = (HCURSOR)pCursor->GetHCURSOR();
542 ::WinSetPointer(HWND_DESKTOP, (HPOINTER)gs_wxBusyCursor);
543 }
544 //else: nothing to do, already set
545 }
546
547 // Restore cursor to normal
548 void wxEndBusyCursor()
549 {
550 wxCHECK_RET( gs_wxBusyCursorCount > 0
551 ,_T("no matching wxBeginBusyCursor() for wxEndBusyCursor()")
552 );
553
554 if (--gs_wxBusyCursorCount == 0)
555 {
556 ::WinSetPointer(HWND_DESKTOP, (HPOINTER)gs_wxBusyCursorOld);
557 gs_wxBusyCursorOld = 0;
558 }
559 }
560
561 // TRUE if we're between the above two calls
562 bool wxIsBusy()
563 {
564 return (gs_wxBusyCursorCount > 0);
565 }
566
567 // ---------------------------------------------------------------------------
568 const wxChar* wxGetHomeDir(
569 wxString* pStr
570 )
571 {
572 wxString& rStrDir = *pStr;
573
574 // OS/2 has no idea about home,
575 // so use the working directory instead?
576
577 // 256 was taken from os2def.h
578 #ifndef MAX_PATH
579 # define MAX_PATH 256
580 #endif
581
582 char zDirName[256];
583 ULONG ulDirLen;
584
585 ::DosQueryCurrentDir(0, zDirName, &ulDirLen);
586 rStrDir = zDirName;
587 return rStrDir.c_str();
588 }
589
590 // Hack for OS/2
591 wxChar* wxGetUserHome (
592 const wxString& rUser
593 )
594 {
595 wxChar* zHome;
596 wxString sUser1(rUser);
597
598 if (sUser1 != _T(""))
599 {
600 wxChar zTmp[64];
601
602 if (wxGetUserId( zTmp
603 ,sizeof(zTmp)/sizeof(char)
604 ))
605 {
606 // Guests belong in the temp dir
607 if (wxStricmp(zTmp, _T("annonymous")) == 0)
608 {
609 if ((zHome = wxGetenv(_T("TMP"))) != NULL ||
610 (zHome = wxGetenv(_T("TMPDIR"))) != NULL ||
611 (zHome = wxGetenv(_T("TEMP"))) != NULL)
612 return *zHome ? zHome : (wxChar*)_T("\\");
613 }
614 if (wxStricmp(zTmp, WXSTRINGCAST sUser1) == 0)
615 sUser1 = _T("");
616 }
617 }
618 if (sUser1 == _T(""))
619 if ((zHome = wxGetenv(_T("HOME"))) != NULL)
620 {
621 wxStrcpy(wxBuffer, zHome);
622 Unix2DosFilename(wxBuffer);
623 return wxBuffer;
624 }
625 return NULL; // No home known!
626 }
627
628 // Check whether this window wants to process messages, e.g. Stop button
629 // in long calculations.
630 bool wxCheckForInterrupt(
631 wxWindow* pWnd
632 )
633 {
634 if(pWnd)
635 {
636 QMSG vMsg;
637 HAB hab;
638 HWND hwndFilter;
639 HWND hwndWin= (HWND) pWnd->GetHWND();
640
641 while(::WinPeekMsg(hab, &vMsg, hwndFilter, 0, 0, PM_REMOVE))
642 {
643 ::WinDispatchMsg(hab, &vMsg);
644 }
645 return TRUE;//*** temporary?
646 }
647 else
648 {
649 wxFAIL_MSG(_T("pWnd==NULL !!!"));
650 return FALSE;//*** temporary?
651 }
652 }
653
654 void wxGetMousePosition(
655 int* pX
656 , int* pY
657 )
658 {
659 POINTL vPt;
660
661 ::WinQueryPointerPos(HWND_DESKTOP, &vPt);
662 *pX = vPt.x;
663 *pY = vPt.y;
664 };
665
666 // Return TRUE if we have a colour display
667 bool wxColourDisplay()
668 {
669 HPS hpsScreen;
670 HDC hdcScreen;
671 LONG lColors;
672
673 hpsScreen = ::WinGetScreenPS(HWND_DESKTOP);
674 hdcScreen = ::GpiQueryDevice(hpsScreen);
675 ::DevQueryCaps(hdcScreen, CAPS_COLORS, 1L, &lColors);
676 return(lColors > 1L);
677 }
678
679 // Returns depth of screen
680 int wxDisplayDepth()
681 {
682 HPS hpsScreen;
683 HDC hdcScreen;
684 LONG lPlanes;
685 LONG lBitsPerPixel;
686 LONG nDepth;
687
688 hpsScreen = ::WinGetScreenPS(HWND_DESKTOP);
689 hdcScreen = ::GpiQueryDevice(hpsScreen);
690 ::DevQueryCaps(hdcScreen, CAPS_COLOR_PLANES, 1L, &lPlanes);
691 ::DevQueryCaps(hdcScreen, CAPS_COLOR_BITCOUNT, 1L, &lBitsPerPixel);
692
693 nDepth = (int)(lPlanes * lBitsPerPixel);
694 DevCloseDC(hdcScreen);
695 return (nDepth);
696 }
697
698 // Get size of display
699 void wxDisplaySize(
700 int* pWidth
701 , int* pHeight
702 )
703 {
704 HPS hpsScreen;
705 HDC hdcScreen;
706
707 hpsScreen = ::WinGetScreenPS(HWND_DESKTOP);
708 hdcScreen = ::GpiQueryDevice(hpsScreen);
709 ::DevQueryCaps(hdcScreen, CAPS_WIDTH, 1L, (PLONG)pWidth);
710 ::DevQueryCaps(hdcScreen, CAPS_HEIGHT, 1L, (PLONG)pHeight);
711 DevCloseDC(hdcScreen);
712 }
713
714 bool wxDirExists(
715 const wxString& rDir
716 )
717 {
718 return (::DosSetCurrentDir(WXSTRINGCAST rDir));
719 }
720
721 // ---------------------------------------------------------------------------
722 // window information functions
723 // ---------------------------------------------------------------------------
724
725 wxString WXDLLEXPORT wxGetWindowText(
726 WXHWND hWnd
727 )
728 {
729 wxString vStr;
730 long lLen = ::WinQueryWindowTextLength((HWND)hWnd) + 1;
731
732 ::WinQueryWindowText((HWND)hWnd, lLen, vStr.GetWriteBuf((int)lLen));
733 vStr.UngetWriteBuf();
734
735 return vStr;
736 }
737
738 wxString WXDLLEXPORT wxGetWindowClass(
739 WXHWND hWnd
740 )
741 {
742 wxString vStr;
743 int nLen = 256; // some starting value
744
745 for ( ;; )
746 {
747 int nCount = ::WinQueryClassName((HWND)hWnd, nLen, vStr.GetWriteBuf(nLen));
748
749 vStr.UngetWriteBuf();
750 if (nCount == nLen )
751 {
752 // the class name might have been truncated, retry with larger
753 // buffer
754 nLen *= 2;
755 }
756 else
757 {
758 break;
759 }
760 }
761 return vStr;
762 }
763
764 WXWORD WXDLLEXPORT wxGetWindowId(
765 WXHWND hWnd
766 )
767 {
768 return ::WinQueryWindowUShort((HWND)hWnd, QWS_ID);
769 }
770