]> git.saurik.com Git - wxWidgets.git/blob - src/os2/utils.cpp
added wxUSE_NOTEBOOK to include/wx/msw/setup0.h, removed wxUSE_RADIOBUTTON (we
[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 = NULL;
69 unsigned long ulBuffer = 256;
70 unsigned long* pulTotalAvail = NULL;
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 = NULL;
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 wxBuffer = new wxChar[256];
269 wxSprintf(wxBuffer, "%s\nContinue?", WXSTRINGCAST rMsg);
270 if (::WinMessageBox( HWND_DESKTOP
271 ,NULL
272 ,(PSZ)wxBuffer
273 ,(PSZ)WXSTRINGCAST rTitle
274 ,0
275 ,MB_ICONEXCLAMATION | MB_YESNO
276 ) == MBID_YES)
277 delete[] wxBuffer;
278 wxExit();
279 }
280
281 // Fatal error: pop up message box and abort
282 void wxFatalError(
283 const wxString& rMsg
284 , const wxString& rTitle
285 )
286 {
287 unsigned long ulRc;
288
289 ulRc = ::WinMessageBox( HWND_DESKTOP
290 ,NULL
291 ,WXSTRINGCAST rMsg
292 ,WXSTRINGCAST rTitle
293 ,0
294 ,MB_NOICON | MB_OK
295 );
296 DosExit(EXIT_PROCESS, ulRc);
297 }
298
299 // Emit a beeeeeep
300 void wxBell()
301 {
302 DosBeep(1000,1000); // 1kHz during 1 sec.
303 }
304
305 // Chris Breeze 27/5/98: revised WIN32 code to
306 // detect WindowsNT correctly
307 int wxGetOsVersion(
308 int* pMajorVsn
309 , int* pMinorVsn
310 )
311 {
312 ULONG ulSysInfo[QSV_MAX] = {0};
313 APIRET ulrc;
314
315 ulrc = ::DosQuerySysInfo( 1L
316 ,QSV_MAX
317 ,(PVOID)ulSysInfo
318 ,sizeof(ULONG) * QSV_MAX
319 );
320 if (ulrc == 0L)
321 {
322 *pMajorVsn = ulSysInfo[QSV_VERSION_MAJOR];
323 *pMajorVsn = *pMajorVsn/10;
324 *pMinorVsn = ulSysInfo[QSV_VERSION_MINOR];
325 return wxWINDOWS_OS2;
326 }
327 return wxWINDOWS; // error if we get here, return generic value
328 }
329
330 // Reading and writing resources (eg WIN.INI, .Xdefaults)
331 #if wxUSE_RESOURCES
332 bool wxWriteResource(
333 const wxString& rSection
334 , const wxString& rEntry
335 , const wxString& rValue
336 , const wxString& rFile
337 )
338 {
339 HAB hab = 0;
340 HINI hIni = 0;
341
342 if (rFile != "")
343 {
344 hIni = ::PrfOpenProfile(hab, (PSZ)WXSTRINGCAST rFile);
345 if (hIni != 0L)
346 {
347 return (::PrfWriteProfileString( hIni
348 ,(PSZ)WXSTRINGCAST rSection
349 ,(PSZ)WXSTRINGCAST rEntry
350 ,(PSZ)WXSTRINGCAST rValue
351 ));
352 }
353 }
354 else
355 return (::PrfWriteProfileString( HINI_PROFILE
356 ,(PSZ)WXSTRINGCAST rSection
357 ,(PSZ)WXSTRINGCAST rEntry
358 ,(PSZ)WXSTRINGCAST rValue
359 ));
360 return FALSE;
361 }
362
363 bool wxWriteResource(
364 const wxString& rSection
365 , const wxString& rEntry
366 , float fValue
367 , const wxString& rFile
368 )
369 {
370 wxChar zBuf[50];
371
372 wxSprintf(zBuf, "%.4f", fValue);
373 return wxWriteResource( rSection
374 ,rEntry
375 ,zBuf
376 ,rFile
377 );
378 }
379
380 bool wxWriteResource(
381 const wxString& rSection
382 , const wxString& rEntry
383 , long lValue
384 , const wxString& rFile
385 )
386 {
387 wxChar zBuf[50];
388
389 wxSprintf(zBuf, "%ld", lValue);
390 return wxWriteResource( rSection
391 ,rEntry
392 ,zBuf
393 ,rFile
394 );
395 }
396
397 bool wxWriteResource(
398 const wxString& rSection
399 , const wxString& rEntry
400 , int lValue
401 , const wxString& rFile
402 )
403 {
404 wxChar zBuf[50];
405
406 wxSprintf(zBuf, "%d", lValue);
407 return wxWriteResource( rSection
408 ,rEntry
409 ,zBuf
410 ,rFile
411 );
412 }
413
414 bool wxGetResource(
415 const wxString& rSection
416 , const wxString& rEntry
417 , wxChar** ppValue
418 , const wxString& rFile
419 )
420 {
421 HAB hab = 0;
422 HINI hIni = 0;
423 wxChar zDefunkt[] = _T("$$default");
424 char zBuf[1000];
425
426 if (rFile != "")
427 {
428 hIni = ::PrfOpenProfile(hab, (PSZ)WXSTRINGCAST rFile);
429 if (hIni != 0L)
430 {
431 ULONG n = ::PrfQueryProfileString( hIni
432 ,(PSZ)WXSTRINGCAST rSection
433 ,(PSZ)WXSTRINGCAST rEntry
434 ,(PSZ)zDefunkt
435 ,(PVOID)zBuf
436 ,1000
437 );
438 if (zBuf == NULL)
439 return FALSE;
440 if (n == 0L || wxStrcmp(zBuf, zDefunkt) == 0)
441 return FALSE;
442 zBuf[n-1] = '\0';
443 }
444 else
445 return FALSE;
446 }
447 else
448 {
449 ULONG n = ::PrfQueryProfileString( HINI_PROFILE
450 ,(PSZ)WXSTRINGCAST rSection
451 ,(PSZ)WXSTRINGCAST rEntry
452 ,(PSZ)zDefunkt
453 ,(PVOID)zBuf
454 ,1000
455 );
456 if (zBuf == NULL)
457 return FALSE;
458 if (n == 0L || wxStrcmp(zBuf, zDefunkt) == 0)
459 return FALSE;
460 zBuf[n-1] = '\0';
461 }
462 strcpy((char*)*ppValue, zBuf);
463 return TRUE;
464 }
465
466 bool wxGetResource(
467 const wxString& rSection
468 , const wxString& rEntry
469 , float* pValue
470 , const wxString& rFile
471 )
472 {
473 wxChar* zStr = NULL;
474
475 zStr = new wxChar[1000];
476 bool bSucc = wxGetResource( rSection
477 ,rEntry
478 ,(wxChar **)&zStr
479 ,rFile
480 );
481
482 if (bSucc)
483 {
484 *pValue = (float)wxStrtod(zStr, NULL);
485 delete[] zStr;
486 return TRUE;
487 }
488 else
489 {
490 delete[] zStr;
491 return FALSE;
492 }
493 }
494
495 bool wxGetResource(
496 const wxString& rSection
497 , const wxString& rEntry
498 , long* pValue
499 , const wxString& rFile
500 )
501 {
502 wxChar* zStr = NULL;
503
504 zStr = new wxChar[1000];
505 bool bSucc = wxGetResource( rSection
506 ,rEntry
507 ,(wxChar **)&zStr
508 ,rFile
509 );
510
511 if (bSucc)
512 {
513 *pValue = wxStrtol(zStr, NULL, 10);
514 delete[] zStr;
515 return TRUE;
516 }
517 else
518 {
519 delete[] zStr;
520 return FALSE;
521 }
522 }
523
524 bool wxGetResource(
525 const wxString& rSection
526 , const wxString& rEntry
527 , int* pValue
528 , const wxString& rFile
529 )
530 {
531 wxChar* zStr = NULL;
532
533 zStr = new wxChar[1000];
534 bool bSucc = wxGetResource( rSection
535 ,rEntry
536 ,(wxChar **)&zStr
537 ,rFile
538 );
539
540 if (bSucc)
541 {
542 *pValue = (int)wxStrtol(zStr, NULL, 10);
543 delete[] zStr;
544 return TRUE;
545 }
546 else
547 {
548 delete[] zStr;
549 return FALSE;
550 }
551 }
552 #endif // wxUSE_RESOURCES
553
554 // ---------------------------------------------------------------------------
555 // helper functions for showing a "busy" cursor
556 // ---------------------------------------------------------------------------
557
558 HCURSOR gs_wxBusyCursor = 0; // new, busy cursor
559 HCURSOR gs_wxBusyCursorOld = 0; // old cursor
560 static int gs_wxBusyCursorCount = 0;
561
562 // Set the cursor to the busy cursor for all windows
563 void wxBeginBusyCursor(
564 wxCursor* pCursor
565 )
566 {
567 if ( gs_wxBusyCursorCount++ == 0 )
568 {
569 gs_wxBusyCursor = (HCURSOR)pCursor->GetHCURSOR();
570 ::WinSetPointer(HWND_DESKTOP, (HPOINTER)gs_wxBusyCursor);
571 }
572 //else: nothing to do, already set
573 }
574
575 // Restore cursor to normal
576 void wxEndBusyCursor()
577 {
578 wxCHECK_RET( gs_wxBusyCursorCount > 0
579 ,_T("no matching wxBeginBusyCursor() for wxEndBusyCursor()")
580 );
581
582 if (--gs_wxBusyCursorCount == 0)
583 {
584 ::WinSetPointer(HWND_DESKTOP, (HPOINTER)gs_wxBusyCursorOld);
585 gs_wxBusyCursorOld = 0;
586 }
587 }
588
589 // TRUE if we're between the above two calls
590 bool wxIsBusy()
591 {
592 return (gs_wxBusyCursorCount > 0);
593 }
594
595 // ---------------------------------------------------------------------------
596 const wxChar* wxGetHomeDir(
597 wxString* pStr
598 )
599 {
600 wxString& rStrDir = *pStr;
601
602 // OS/2 has no idea about home,
603 // so use the working directory instead?
604
605 // 256 was taken from os2def.h
606 #ifndef MAX_PATH
607 # define MAX_PATH 256
608 #endif
609
610 char zDirName[256];
611 ULONG ulDirLen;
612
613 ::DosQueryCurrentDir(0, zDirName, &ulDirLen);
614 rStrDir = zDirName;
615 return rStrDir.c_str();
616 }
617
618 // Hack for OS/2
619 wxChar* wxGetUserHome (
620 const wxString& rUser
621 )
622 {
623 wxChar* zHome;
624 wxString sUser1(rUser);
625
626 wxBuffer = new wxChar[256];
627 if (sUser1 != _T(""))
628 {
629 wxChar zTmp[64];
630
631 if (wxGetUserId( zTmp
632 ,sizeof(zTmp)/sizeof(char)
633 ))
634 {
635 // Guests belong in the temp dir
636 if (wxStricmp(zTmp, _T("annonymous")) == 0)
637 {
638 if ((zHome = wxGetenv(_T("TMP"))) != NULL ||
639 (zHome = wxGetenv(_T("TMPDIR"))) != NULL ||
640 (zHome = wxGetenv(_T("TEMP"))) != NULL)
641 delete[] wxBuffer;
642 return *zHome ? zHome : (wxChar*)_T("\\");
643 }
644 if (wxStricmp(zTmp, WXSTRINGCAST sUser1) == 0)
645 sUser1 = _T("");
646 }
647 }
648 if (sUser1 == _T(""))
649 {
650 if ((zHome = wxGetenv(_T("HOME"))) != NULL)
651 {
652 wxStrcpy(wxBuffer, zHome);
653 Unix2DosFilename(wxBuffer);
654 wxStrcpy(zHome, wxBuffer);
655 delete[] wxBuffer;
656 return zHome;
657 }
658 }
659 delete[] wxBuffer;
660 return NULL; // No home known!
661 }
662
663 // Check whether this window wants to process messages, e.g. Stop button
664 // in long calculations.
665 bool wxCheckForInterrupt(
666 wxWindow* pWnd
667 )
668 {
669 if(pWnd)
670 {
671 QMSG vMsg;
672 HAB hab = 0;
673 HWND hwndFilter = NULLHANDLE;
674 HWND hwndWin= (HWND) pWnd->GetHWND();
675
676 while(::WinPeekMsg(hab, &vMsg, hwndFilter, 0, 0, PM_REMOVE))
677 {
678 ::WinDispatchMsg(hab, &vMsg);
679 }
680 return TRUE;//*** temporary?
681 }
682 else
683 {
684 wxFAIL_MSG(_T("pWnd==NULL !!!"));
685 return FALSE;//*** temporary?
686 }
687 }
688
689 void wxGetMousePosition(
690 int* pX
691 , int* pY
692 )
693 {
694 POINTL vPt;
695
696 ::WinQueryPointerPos(HWND_DESKTOP, &vPt);
697 *pX = vPt.x;
698 *pY = vPt.y;
699 };
700
701 // Return TRUE if we have a colour display
702 bool wxColourDisplay()
703 {
704 HPS hpsScreen;
705 HDC hdcScreen;
706 LONG lColors;
707
708 hpsScreen = ::WinGetScreenPS(HWND_DESKTOP);
709 hdcScreen = ::GpiQueryDevice(hpsScreen);
710 ::DevQueryCaps(hdcScreen, CAPS_COLORS, 1L, &lColors);
711 return(lColors > 1L);
712 }
713
714 // Returns depth of screen
715 int wxDisplayDepth()
716 {
717 HPS hpsScreen;
718 HDC hdcScreen;
719 LONG lPlanes;
720 LONG lBitsPerPixel;
721 LONG nDepth;
722
723 hpsScreen = ::WinGetScreenPS(HWND_DESKTOP);
724 hdcScreen = ::GpiQueryDevice(hpsScreen);
725 ::DevQueryCaps(hdcScreen, CAPS_COLOR_PLANES, 1L, &lPlanes);
726 ::DevQueryCaps(hdcScreen, CAPS_COLOR_BITCOUNT, 1L, &lBitsPerPixel);
727
728 nDepth = (int)(lPlanes * lBitsPerPixel);
729 DevCloseDC(hdcScreen);
730 return (nDepth);
731 }
732
733 // Get size of display
734 void wxDisplaySize(
735 int* pWidth
736 , int* pHeight
737 )
738 {
739 HPS hpsScreen;
740 HDC hdcScreen;
741 LONG lWidth;
742 LONG lHeight;
743
744 hpsScreen = ::WinGetScreenPS(HWND_DESKTOP);
745 hdcScreen = ::GpiQueryDevice(hpsScreen);
746 ::DevQueryCaps(hdcScreen, CAPS_WIDTH, 1L, &lWidth);
747 ::DevQueryCaps(hdcScreen, CAPS_HEIGHT, 1L, &lHeight);
748 DevCloseDC(hdcScreen);
749 *pWidth = (int)lWidth;
750 *pHeight = (int)lHeight;
751 }
752
753 bool wxDirExists(
754 const wxString& rDir
755 )
756 {
757 return (::DosSetCurrentDir(WXSTRINGCAST rDir));
758 }
759
760 // ---------------------------------------------------------------------------
761 // window information functions
762 // ---------------------------------------------------------------------------
763
764 wxString WXDLLEXPORT wxGetWindowText(
765 WXHWND hWnd
766 )
767 {
768 wxString vStr;
769 long lLen = ::WinQueryWindowTextLength((HWND)hWnd) + 1;
770
771 ::WinQueryWindowText((HWND)hWnd, lLen, vStr.GetWriteBuf((int)lLen));
772 vStr.UngetWriteBuf();
773
774 return vStr;
775 }
776
777 wxString WXDLLEXPORT wxGetWindowClass(
778 WXHWND hWnd
779 )
780 {
781 wxString vStr;
782 int nLen = 256; // some starting value
783
784 for ( ;; )
785 {
786 int nCount = ::WinQueryClassName((HWND)hWnd, nLen, vStr.GetWriteBuf(nLen));
787
788 vStr.UngetWriteBuf();
789 if (nCount == nLen )
790 {
791 // the class name might have been truncated, retry with larger
792 // buffer
793 nLen *= 2;
794 }
795 else
796 {
797 break;
798 }
799 }
800 return vStr;
801 }
802
803 WXWORD WXDLLEXPORT wxGetWindowId(
804 WXHWND hWnd
805 )
806 {
807 return ::WinQueryWindowUShort((HWND)hWnd, QWS_ID);
808 }
809