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