1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Various utilities
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #include "wx/cursor.h"
22 #include "wx/os2/private.h"
49 // In the WIN.INI file
50 static const wxChar WX_SECTION
[] = _T("wxWindows");
51 static const wxChar eHOSTNAME
[] = _T("HostName");
52 static const wxChar eUSERID
[] = _T("UserId");
53 static const wxChar eUSERNAME
[] = _T("UserName");
55 // For the following functions we SHOULD fill in support
56 // for Windows-NT (which I don't know) as I assume it begin
57 // a POSIX Unix (so claims MS) that it has some special
58 // functions beyond those provided by WinSock
60 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
61 bool wxGetHostName(wxChar
*buf
, int maxSize
)
66 unsigned long ulLevel
;
67 unsigned char* pbBuffer
;
68 unsigned long ulBuffer
;
69 unsigned long* pulTotalAvail
;
71 NetBios32GetInfo( (const unsigned char*)server
72 ,(const unsigned char*)computer
81 const wxChar
*default_host
= _T("noname");
83 if ((sysname
= wxGetenv(_T("SYSTEM_NAME"))) == NULL
)
85 // GetProfileString(WX_SECTION, eHOSTNAME, default_host, buf, maxSize - 1);
88 wxStrncpy(buf
, sysname
, maxSize
- 1);
89 buf
[maxSize
] = _T('\0');
91 return *buf
? TRUE
: FALSE
;
94 // Get user ID e.g. jacs
95 bool wxGetUserId(wxChar
*buf
, int maxSize
)
97 return(U32ELOCL((unsigned char*)buf
, (unsigned long *)&maxSize
));
100 bool wxGetUserName(wxChar
*buf
, int maxSize
)
103 wxGetUserId(buf
, maxSize
);
105 wxStrncpy(buf
, _T("Unknown User"), maxSize
);
110 int wxKill(long pid
, int sig
)
116 // Execute a program in an Interactive Shell
118 bool wxShell(const wxString
& command
)
121 if ((shell
= wxGetenv(_T("COMSPEC"))) == NULL
)
122 shell
= _T("\\CMD.EXE");
126 wxSprintf(tmp
, "%s /c %s", shell
, WXSTRINGCAST command
);
128 wxStrcpy(tmp
, shell
);
130 return (wxExecute((wxChar
*)tmp
, FALSE
) != 0);
133 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
134 long wxGetFreeMemory()
136 // return (long)GetFreeSpace(0);
140 // Sleep for nSecs seconds. Attempt a Windows implementation using timers.
141 static bool inTimer
= FALSE
;
143 class wxSleepTimer
: public wxTimer
153 static wxTimer
*wxTheSleepTimer
= NULL
;
155 void wxUsleep(unsigned long milliseconds
)
157 ::DosSleep(milliseconds
);
160 void wxSleep(int nSecs
)
165 wxTheSleepTimer
= new wxSleepTimer
;
167 wxTheSleepTimer
->Start(nSecs
*1000);
170 if (wxTheApp
->Pending())
171 wxTheApp
->Dispatch();
173 delete wxTheSleepTimer
;
174 wxTheSleepTimer
= NULL
;
177 // Consume all events until no more left
183 // Output a debug mess., in a system dependent fashion.
184 void wxDebugMsg(const wxChar
*fmt
...)
187 static wxChar buffer
[512];
189 if (!wxTheApp
->GetWantDebugOutput())
194 sprintf(buffer
,fmt
,ap
) ;
199 // Non-fatal error: pop up message box and (possibly) continue
200 void wxError(const wxString
& msg
, const wxString
& title
)
202 wxSprintf(wxBuffer
, "%s\nContinue?", WXSTRINGCAST msg
);
203 if (::WinMessageBox( HWND_DESKTOP
206 ,(PSZ
)WXSTRINGCAST title
208 ,MB_ICONEXCLAMATION
| MB_YESNO
213 // Fatal error: pop up message box and abort
214 void wxFatalError(const wxString
& rMsg
, const wxString
& rTitle
)
218 rc
= ::WinMessageBox( HWND_DESKTOP
225 DosExit(EXIT_PROCESS
, rc
);
231 DosBeep(1000,1000); // 1kHz during 1 sec.
234 // Chris Breeze 27/5/98: revised WIN32 code to
235 // detect WindowsNT correctly
236 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
238 ULONG aulSysInfo
[QSV_MAX
] = {0};
240 if (DosQuerySysInfo( 1L
243 ,sizeof(ULONG
) * QSV_MAX
246 *majorVsn
= aulSysInfo
[QSV_VERSION_MAJOR
];
247 *minorVsn
= aulSysInfo
[QSV_VERSION_MINOR
];
248 return wxWINDOWS_OS2
;
250 return wxWINDOWS
; // error if we get here, return generic value
253 // Reading and writing resources (eg WIN.INI, .Xdefaults)
254 // TODO: Ability to read and write to an INI file
257 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
262 return (WritePrivateProfileString((PCSZ)WXSTRINGCAST section, (PCSZ)WXSTRINGCAST entry, (PCSZ)value, (PCSZ)WXSTRINGCAST file) != 0);
264 return (WriteProfileString((PCSZ)WXSTRINGCAST section, (PCSZ)WXSTRINGCAST entry, (PCSZ)WXSTRINGCAST value) != 0);
269 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
272 wxSprintf(buf
, "%.4f", value
);
273 return wxWriteResource(section
, entry
, buf
, file
);
276 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
279 wxSprintf(buf
, "%ld", value
);
280 return wxWriteResource(section
, entry
, buf
, file
);
283 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
286 wxSprintf(buf
, "%d", value
);
287 return wxWriteResource(section
, entry
, buf
, file
);
290 bool wxGetResource(const wxString
& section
, const wxString
& entry
, wxChar
**value
, const wxString
& file
)
292 static const wxChar defunkt
[] = _T("$$default");
297 int n = GetPrivateProfileString((PCSZ)WXSTRINGCAST section, (PCSZ)WXSTRINGCAST entry, (PCSZ)defunkt,
298 (PSZ)wxBuffer, 1000, (PCSZ)WXSTRINGCAST file);
299 if (n == 0 || wxStrcmp(wxBuffer, defunkt) == 0)
304 int n = GetProfileString((PCSZ)WXSTRINGCAST section, (PCSZ)WXSTRINGCAST entry, (LPCTSTR)defunkt,
305 (PSZ)wxBuffer, 1000);
306 if (n == 0 || wxStrcmp(wxBuffer, defunkt) == 0)
309 if (*value) delete[] (*value);
310 *value = copystring(wxBuffer);
316 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
319 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
322 *value
= (float)wxStrtod(s
, NULL
);
329 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
332 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
335 *value
= wxStrtol(s
, NULL
, 10);
342 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
345 bool succ
= wxGetResource(section
, entry
, (wxChar
**)&s
, file
);
348 *value
= (int)wxStrtol(s
, NULL
, 10);
354 #endif // wxUSE_RESOURCES
356 // ---------------------------------------------------------------------------
357 // helper functions for showing a "busy" cursor
358 // ---------------------------------------------------------------------------
360 HCURSOR gs_wxBusyCursor
= 0; // new, busy cursor
361 HCURSOR gs_wxBusyCursorOld
= 0; // old cursor
362 static int gs_wxBusyCursorCount
= 0;
364 // Set the cursor to the busy cursor for all windows
365 void wxBeginBusyCursor(wxCursor
*cursor
)
367 if ( gs_wxBusyCursorCount
++ == 0 )
369 gs_wxBusyCursor
= (HCURSOR
)cursor
->GetHCURSOR();
370 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursor
);
372 //else: nothing to do, already set
375 // Restore cursor to normal
376 void wxEndBusyCursor()
378 wxCHECK_RET( gs_wxBusyCursorCount
> 0,
379 _T("no matching wxBeginBusyCursor() for wxEndBusyCursor()"));
381 if ( --gs_wxBusyCursorCount
== 0 )
383 ::WinSetPointer(HWND_DESKTOP
, (HPOINTER
)gs_wxBusyCursorOld
);
384 gs_wxBusyCursorOld
= 0;
388 // TRUE if we're between the above two calls
391 return (gs_wxBusyCursorCount
> 0);
394 // ---------------------------------------------------------------------------
395 const wxChar
* wxGetHomeDir(wxString
*pstr
)
397 wxString
& strDir
= *pstr
;
399 // OS/2 has no idea about home,
400 // so use the working directory instead?
402 // 256 was taken from os2def.h
404 # define MAX_PATH 256
411 ::DosQueryCurrentDir( 0, DirName
, &DirLen
);
413 return strDir
.c_str();
417 wxChar
*wxGetUserHome (const wxString
& user
)
420 wxString
user1(user
);
422 if (user1
!= _T("")) {
424 if (wxGetUserId(tmp
, sizeof(tmp
)/sizeof(char))) {
425 // Guests belong in the temp dir
426 if (wxStricmp(tmp
, _T("annonymous")) == 0) {
427 if ((home
= wxGetenv(_T("TMP"))) != NULL
||
428 (home
= wxGetenv(_T("TMPDIR"))) != NULL
||
429 (home
= wxGetenv(_T("TEMP"))) != NULL
)
430 return *home
? home
: (wxChar
*)_T("\\");
432 if (wxStricmp(tmp
, WXSTRINGCAST user1
) == 0)
437 if ((home
= wxGetenv(_T("HOME"))) != NULL
)
439 wxStrcpy(wxBuffer
, home
);
440 Unix2DosFilename(wxBuffer
);
443 return NULL
; // No home known!
446 // Check whether this window wants to process messages, e.g. Stop button
447 // in long calculations.
448 bool wxCheckForInterrupt(wxWindow
*wnd
)
455 HWND win
= (HWND
) wnd
->GetHWND();
456 while(::WinPeekMsg(hab
,&msg
,hwndFilter
,0,0,PM_REMOVE
))
458 ::WinDispatchMsg( hab
, &msg
);
460 return TRUE
;//*** temporary?
463 wxFAIL_MSG(_T("wnd==NULL !!!"));
465 return FALSE
;//*** temporary?
469 wxChar
*wxLoadUserResource(const wxString
& resourceName
, const wxString
& resourceType
)
476 * #if !defined(__WIN32__) || defined(__TWIN32__)
477 * HRSRC hResource = ::FindResource(wxGetInstance(), WXSTRINGCAST resourceName, WXSTRINGCAST resourceType);
480 * HRSRC hResource = ::FindResourceW(wxGetInstance(), WXSTRINGCAST resourceName, WXSTRINGCAST resourceType);
482 * HRSRC hResource = ::FindResourceA(wxGetInstance(), WXSTRINGCAST resourceName, WXSTRINGCAST resourceType);
486 * if (hResource == 0)
488 * HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource);
491 * wxChar *theText = (wxChar *)LockResource(hData);
495 * s = copystring(theText);
500 void wxGetMousePosition( int* x
, int* y
)
503 ::WinQueryPointerPos( HWND_DESKTOP
, & pt
);
508 // Return TRUE if we have a colour display
509 bool wxColourDisplay()
512 // TODO: use DosQueryDevCaps to figure it out
516 // Returns depth of screen
519 HDC hDc
= ::WinOpenWindowDC((HWND
)NULL
);
520 long lArray
[CAPS_COLOR_BITCOUNT
];
531 nPlanes
= (int)lArray
[CAPS_COLOR_PLANES
];
532 nBitsPerPixel
= (int)lArray
[CAPS_COLOR_BITCOUNT
];
533 nDepth
= nPlanes
* nBitsPerPixel
;
539 // Get size of display
540 void wxDisplaySize(int *width
, int *height
)
542 HDC hDc
= ::WinOpenWindowDC((HWND
)NULL
);
543 long lArray
[CAPS_HEIGHT
];
551 *width
= (int)lArray
[CAPS_WIDTH
];
552 *height
= (int)lArray
[CAPS_HEIGHT
];
557 bool wxDirExists(const wxString
& dir
)
559 // TODO: Control program file stuff
563 // ---------------------------------------------------------------------------
564 // window information functions
565 // ---------------------------------------------------------------------------
567 wxString WXDLLEXPORT
wxGetWindowText(WXHWND hWnd
)
570 long len
= ::WinQueryWindowTextLength((HWND
)hWnd
) + 1;
571 ::WinQueryWindowText((HWND
)hWnd
, len
, str
.GetWriteBuf((int)len
));
577 wxString WXDLLEXPORT
wxGetWindowClass(WXHWND hWnd
)
581 int len
= 256; // some starting value
585 int count
= ::WinQueryClassName((HWND
)hWnd
, len
, str
.GetWriteBuf(len
));
590 // the class name might have been truncated, retry with larger
602 WXWORD WXDLLEXPORT
wxGetWindowId(WXHWND hWnd
)
604 return ::WinQueryWindowUShort((HWND
)hWnd
, QWS_ID
);