]> git.saurik.com Git - wxWidgets.git/blame - src/os2/utils.cpp
*** empty log message ***
[wxWidgets.git] / src / os2 / utils.cpp
CommitLineData
0e320a79
DW
1/////////////////////////////////////////////////////////////////////////////
2// Name: utils.cpp
3// Purpose: Various utilities
45fcbf3b 4// Author: David Webster
0e320a79 5// Modified by:
45fcbf3b 6// Created: 09/17/99
0e320a79 7// RCS-ID: $Id$
45fcbf3b
DW
8// Copyright: (c) David Webster
9// Licence: wxWindows license
0e320a79
DW
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
45fcbf3b 13// #pragma implementation "utils.h" // Note: this is done in utilscmn.cpp now.
0e320a79
DW
14#endif
15
45fcbf3b
DW
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifndef WX_PRECOMP
20 #include "wx/setup.h"
21 #include "wx/utils.h"
22 #include "wx/app.h"
23 #include "wx/cursor.h"
24#endif //WX_PRECOMP
25
26#include "wx/os2/private.h"
27#include "wx/timer.h"
28#include "wx/intl.h"
29
0e320a79 30#include <ctype.h>
45fcbf3b
DW
31#include <direct.h>
32
33#include "wx/log.h"
34
35#include <io.h>
0e320a79
DW
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
45fcbf3b 40#include <errno.h>
0e320a79
DW
41#include <stdarg.h>
42
86de7616
DW
43#define INCL_OS2
44#define INCL_PM
45#include <os2.h>
45fcbf3b
DW
46#include<netdb.h>
47#include<upm.h>
48
49// In the WIN.INI file
86de7616
DW
50static const wxChar WX_SECTION[] = _T("wxWindows");
51static const wxChar eHOSTNAME[] = _T("HostName");
52static const wxChar eUSERID[] = _T("UserId");
53static const wxChar eUSERNAME[] = _T("UserName");
45fcbf3b
DW
54
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
59
0e320a79 60// Get full hostname (eg. DoDo.BSn-Germany.crg.de)
45fcbf3b 61bool wxGetHostName(wxChar *buf, int maxSize)
0e320a79 62{
45fcbf3b
DW
63#ifdef USE_NET_API
64 char server[256];
65 char computer[256];
66 unsigned long ulLevel;
67 unsigned char* pbBuffer;
68 unsigned long ulBuffer;
69 unsigned long* pulTotalAvail;
70
71 NetBios32GetInfo( server
72 ,computer
73 ,ulLevel
74 ,pbBuffer
75 ,ulBuffer
76 ,pulTotalAvail
77 );
78 strcpy(buf, server);
79#else
80 wxChar *sysname;
81 const wxChar *default_host = _T("noname");
82
83 if ((sysname = wxGetenv(_T("SYSTEM_NAME"))) == NULL) {
84 GetProfileString(WX_SECTION, eHOSTNAME, default_host, buf, maxSize - 1);
85 } else
86 wxStrncpy(buf, sysname, maxSize - 1);
87 buf[maxSize] = _T('\0');
88#endif
89 return *buf ? TRUE : FALSE;
0e320a79
DW
90}
91
92// Get user ID e.g. jacs
45fcbf3b 93bool wxGetUserId(wxChar *buf, int maxSize)
0e320a79 94{
45fcbf3b 95 return(U32ELOCL(bub, maxSize));
0e320a79
DW
96}
97
45fcbf3b 98bool wxGetUserName(wxChar *buf, int maxSize)
0e320a79 99{
45fcbf3b
DW
100#ifdef USE_NET_API
101 wxGetUserId(buf, maxSize);
102#else
103 bool ok = GetProfileString(WX_SECTION, eUSERNAME, _T(""), buf, maxSize - 1) != 0;
104 if ( !ok )
105 {
106 ok = wxGetUserId(buf, maxSize);
107 }
108
109 if ( !ok )
110 {
111 wxStrncpy(buf, _T("Unknown User"), maxSize);
112 }
113#endif
114 return TRUE;
0e320a79
DW
115}
116
117int wxKill(long pid, int sig)
118{
45fcbf3b 119 return 0;
0e320a79
DW
120}
121
122//
123// Execute a program in an Interactive Shell
124//
125bool wxShell(const wxString& command)
126{
45fcbf3b 127 wxChar *shell;
86de7616
DW
128 if ((shell = wxGetenv(_T("COMSPEC"))) == NULL)
129 shell = _T("\\CMD.EXE");
45fcbf3b
DW
130
131 wxChar tmp[255];
132 if (command != "")
133 wxSprintf(tmp, "%s /c %s", shell, WXSTRINGCAST command);
134 else
135 wxStrcpy(tmp, shell);
136
137 return (wxExecute((wxChar *)tmp, FALSE) != 0);
0e320a79
DW
138}
139
140// Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
141long wxGetFreeMemory()
142{
45fcbf3b
DW
143 return (long)GetFreeSpace(0);
144}
145
146// Sleep for nSecs seconds. Attempt a Windows implementation using timers.
147static bool inTimer = FALSE;
148
149class wxSleepTimer: public wxTimer
150{
151 public:
152 inline void Notify()
153 {
154 inTimer = FALSE;
155 Stop();
156 }
157};
158
159static wxTimer *wxTheSleepTimer = NULL;
160
161void wxUsleep(unsigned long milliseconds)
162{
163 ::DosSleep(milliseconds);
0e320a79
DW
164}
165
166void wxSleep(int nSecs)
167{
45fcbf3b
DW
168 if (inTimer)
169 return;
170
171 wxTheSleepTimer = new wxSleepTimer;
172 inTimer = TRUE;
173 wxTheSleepTimer->Start(nSecs*1000);
174 while (inTimer)
175 {
176 if (wxTheApp->Pending())
177 wxTheApp->Dispatch();
178 }
179 delete wxTheSleepTimer;
180 wxTheSleepTimer = NULL;
0e320a79
DW
181}
182
183// Consume all events until no more left
184void wxFlushEvents()
185{
45fcbf3b 186// wxYield();
0e320a79
DW
187}
188
45fcbf3b
DW
189// Output a debug mess., in a system dependent fashion.
190void wxDebugMsg(const wxChar *fmt ...)
0e320a79
DW
191{
192 va_list ap;
45fcbf3b 193 static wxChar buffer[512];
0e320a79
DW
194
195 if (!wxTheApp->GetWantDebugOutput())
196 return ;
197
198 va_start(ap, fmt);
199
45fcbf3b
DW
200 sprintf(buffer,fmt,ap) ;
201 fflush(buffer) ;
0e320a79
DW
202
203 va_end(ap);
204}
205
206// Non-fatal error: pop up message box and (possibly) continue
207void wxError(const wxString& msg, const wxString& title)
208{
45fcbf3b
DW
209 wxSprintf(wxBuffer, "%s\nContinue?", WXSTRINGCAST msg);
210 if (::WinMessageBox( HWND_DESKTOP
211 ,NULL
212 ,(PSZ)wxBuffer
213 ,(PSZ)WXSTRINGCAST title
214 ,MB_ICONEXCLAMATION | MB_YESNO
215 ) == IDNO)
0e320a79
DW
216 wxExit();
217}
218
219// Fatal error: pop up message box and abort
220void wxFatalError(const wxString& msg, const wxString& title)
221{
45fcbf3b
DW
222 YUint32 rc;
223 HWND hWnd;
224
225 WinMessageBox( HWND_DESKTOP
226 ,hWnd
227 ,rMsg.Data()
228 ,rTitle.Data()
229 ,0
230 ,MB_NOICON | MB_OK
231 );
232 DosExit(EXIT_PROCESS, rc);
0e320a79
DW
233}
234
235// Emit a beeeeeep
236void wxBell()
237{
45fcbf3b 238 DosBeep(1000,1000); // 1kHz during 1 sec.
0e320a79
DW
239}
240
45fcbf3b
DW
241// Chris Breeze 27/5/98: revised WIN32 code to
242// detect WindowsNT correctly
0e320a79
DW
243int wxGetOsVersion(int *majorVsn, int *minorVsn)
244{
45fcbf3b
DW
245 ULONG aulSysInfo[QSV_MAX] = {0};
246
247 if (DosQuerySysInfo( 1L
248 ,QSV_MAX
249 ,(PVOID)aulSysInfo
250 ,sizeof(ULONG) * QSV_MAX
251 ))
252 {
253 *majorVsn = aulSysInfo[QSV_VERSION_MAJOR];
254 *minorVsn = aulSysInfo[QSV_VERSION_MINOR];
255 return wxWINDOWS_OS2;
256 }
257 return wxWINDOWS; // error if we get here, return generic value
0e320a79
DW
258}
259
260// Reading and writing resources (eg WIN.INI, .Xdefaults)
261#if wxUSE_RESOURCES
262bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
263{
45fcbf3b
DW
264 if (file != "")
265 return (WritePrivateProfileString((PCSZ)WXSTRINGCAST section, (PCSZ)WXSTRINGCAST entry, (PCSZ)value, (PCSZ)WXSTRINGCAST file) != 0);
266 else
267 return (WriteProfileString((PCSZ)WXSTRINGCAST section, (PCSZ)WXSTRINGCAST entry, (PCSZ)WXSTRINGCAST value) != 0);
0e320a79
DW
268}
269
270bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
271{
45fcbf3b
DW
272 wxChar buf[50];
273 wxSprintf(buf, "%.4f", value);
0e320a79
DW
274 return wxWriteResource(section, entry, buf, file);
275}
276
277bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
278{
45fcbf3b
DW
279 wxChar buf[50];
280 wxSprintf(buf, "%ld", value);
0e320a79
DW
281 return wxWriteResource(section, entry, buf, file);
282}
283
284bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
285{
45fcbf3b
DW
286 wxChar buf[50];
287 wxSprintf(buf, "%d", value);
0e320a79
DW
288 return wxWriteResource(section, entry, buf, file);
289}
290
45fcbf3b 291bool wxGetResource(const wxString& section, const wxString& entry, wxChar **value, const wxString& file)
0e320a79 292{
86de7616 293 static const wxChar defunkt[] = _T("$$default");
45fcbf3b
DW
294 if (file != "")
295 {
296 int n = GetPrivateProfileString((PCSZ)WXSTRINGCAST section, (PCSZ)WXSTRINGCAST entry, (PCSZ)defunkt,
297 (PSZ)wxBuffer, 1000, (PCSZ)WXSTRINGCAST file);
298 if (n == 0 || wxStrcmp(wxBuffer, defunkt) == 0)
299 return FALSE;
300 }
301 else
302 {
303 int n = GetProfileString((PCSZ)WXSTRINGCAST section, (PCSZ)WXSTRINGCAST entry, (LPCTSTR)defunkt,
304 (PSZ)wxBuffer, 1000);
305 if (n == 0 || wxStrcmp(wxBuffer, defunkt) == 0)
306 return FALSE;
307 }
308 if (*value) delete[] (*value);
309 *value = copystring(wxBuffer);
310 return TRUE;
311 }
0e320a79
DW
312
313bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
314{
45fcbf3b
DW
315 wxChar *s = NULL;
316 bool succ = wxGetResource(section, entry, (wxChar **)&s, file);
0e320a79
DW
317 if (succ)
318 {
45fcbf3b 319 *value = (float)wxStrtod(s, NULL);
0e320a79
DW
320 delete[] s;
321 return TRUE;
322 }
323 else return FALSE;
324}
325
326bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
327{
45fcbf3b
DW
328 wxChar *s = NULL;
329 bool succ = wxGetResource(section, entry, (wxChar **)&s, file);
0e320a79
DW
330 if (succ)
331 {
45fcbf3b 332 *value = wxStrtol(s, NULL, 10);
0e320a79
DW
333 delete[] s;
334 return TRUE;
335 }
336 else return FALSE;
337}
338
339bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
340{
45fcbf3b
DW
341 wxChar *s = NULL;
342 bool succ = wxGetResource(section, entry, (wxChar **)&s, file);
0e320a79
DW
343 if (succ)
344 {
45fcbf3b
DW
345 *value = (int)wxStrtol(s, NULL, 10);
346 delete[] s;
0e320a79
DW
347 return TRUE;
348 }
349 else return FALSE;
350}
351#endif // wxUSE_RESOURCES
352
45fcbf3b
DW
353// ---------------------------------------------------------------------------
354// helper functions for showing a "busy" cursor
355// ---------------------------------------------------------------------------
356
357HCURSOR gs_wxBusyCursor = 0; // new, busy cursor
358HCURSOR gs_wxBusyCursorOld = 0; // old cursor
359static int gs_wxBusyCursorCount = 0;
0e320a79
DW
360
361// Set the cursor to the busy cursor for all windows
362void wxBeginBusyCursor(wxCursor *cursor)
363{
45fcbf3b
DW
364 if ( gs_wxBusyCursorCount++ == 0 )
365 {
366 gs_wxBusyCursor = (HCURSOR)cursor->GetHCURSOR();
367 ::WinSetPointer(HWND_DESKTOP, (HPOINTER)gs_wxBusyCursor);
368 }
369 //else: nothing to do, already set
0e320a79
DW
370}
371
372// Restore cursor to normal
373void wxEndBusyCursor()
374{
45fcbf3b 375 wxCHECK_RET( gs_wxBusyCursorCount > 0,
86de7616 376 _T("no matching wxBeginBusyCursor() for wxEndBusyCursor()"));
45fcbf3b
DW
377
378 if ( --gs_wxBusyCursorCount == 0 )
379 {
380 ::WinSetPointer(HWND_DESKTOP, (HPOINTER)gs_wxBusyCursorOld);
381 gs_wxBusyCursorOld = 0;
382 }
0e320a79
DW
383}
384
385// TRUE if we're between the above two calls
386bool wxIsBusy()
387{
45fcbf3b
DW
388 return (gs_wxBusyCursorCount > 0);
389}
390
391// ---------------------------------------------------------------------------
392const wxChar* wxGetHomeDir(wxString *pstr)
393{
394 wxString& strDir = *pstr;
0e320a79 395
45fcbf3b
DW
396 // OS/2 has no idea about home,
397 // so use the working directory instead?
398
399 // 256 was taken from os2def.h
400#ifndef MAX_PATH
401# define MAX_PATH 256
402#endif
403
404
405 char DirName[256];
406 ULONG DirLen;
407
408 ::DosQueryCurrentDir( 0, DirName, &DirLen);
409 strDir = DirName;
410 return strDir.c_str();
411}
412
413// Hack for MS-DOS
414wxChar *wxGetUserHome (const wxString& user)
0e320a79 415{
45fcbf3b
DW
416 wxChar *home;
417 wxString user1(user);
418
419 if (user1 != _T("")) {
420 wxChar tmp[64];
421 if (wxGetUserId(tmp, sizeof(tmp)/sizeof(char))) {
422 // Guests belong in the temp dir
86de7616
DW
423 if (wxStricmp(tmp, _T("annonymous")) == 0) {
424 if ((home = wxGetenv(_T("TMP"))) != NULL ||
425 (home = wxGetenv(_T("TMPDIR"))) != NULL ||
426 (home = wxGetenv(_T("TEMP"))) != NULL)
45fcbf3b
DW
427 return *home ? home : (wxChar*)_T("\\");
428 }
429 if (wxStricmp(tmp, WXSTRINGCAST user1) == 0)
430 user1 = _T("");
431 }
432 }
433 if (user1 == _T(""))
86de7616 434 if ((home = wxGetenv(_T("HOME"))) != NULL)
45fcbf3b
DW
435 {
436 wxStrcpy(wxBuffer, home);
437 Unix2DosFilename(wxBuffer);
438 return wxBuffer;
439 }
440 return NULL; // No home known!
0e320a79
DW
441}
442
443// Check whether this window wants to process messages, e.g. Stop button
444// in long calculations.
445bool wxCheckForInterrupt(wxWindow *wnd)
446{
45fcbf3b
DW
447 if(wnd){
448 QMSG msg;
449 HAB hab;
450 HWND hwndFilter;
451
452 HWND win= (HWND) wnd->GetHWND();
453 while(::WinPeekMsg(hab,&msg,hwndFilter,0,0,PM_REMOVE))
454 {
455 ::WinDispatchMsg( hab, &qmsg );
456 }
457 return TRUE;//*** temporary?
458 }
459 else{
86de7616 460 wxFAIL_MSG(_T("wnd==NULL !!!"));
45fcbf3b
DW
461
462 return FALSE;//*** temporary?
463 }
464}
465
466wxChar *wxLoadUserResource(const wxString& resourceName, const wxString& resourceType)
467{
468 wxChar *s = NULL;
469
470/*
471* How to in PM?
472*
473* #if !defined(__WIN32__) || defined(__TWIN32__)
474* HRSRC hResource = ::FindResource(wxGetInstance(), WXSTRINGCAST resourceName, WXSTRINGCAST resourceType);
475* #else
476* #ifdef UNICODE
477* HRSRC hResource = ::FindResourceW(wxGetInstance(), WXSTRINGCAST resourceName, WXSTRINGCAST resourceType);
478* #else
479* HRSRC hResource = ::FindResourceA(wxGetInstance(), WXSTRINGCAST resourceName, WXSTRINGCAST resourceType);
480* #endif
481* #endif
482*
483* if (hResource == 0)
484* return NULL;
485* HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource);
486* if (hData == 0)
487* return NULL;
488* wxChar *theText = (wxChar *)LockResource(hData);
489* if (!theText)
490* return NULL;
491*/
492 s = copystring(theText);
493
494 return s;
0e320a79
DW
495}
496
497void wxGetMousePosition( int* x, int* y )
498{
45fcbf3b
DW
499 POINT pt;
500 GetCursorPos( & pt );
501 *x = pt.x;
502 *y = pt.y;
0e320a79
DW
503};
504
505// Return TRUE if we have a colour display
506bool wxColourDisplay()
507{
45fcbf3b
DW
508 bool flag;
509 // TODO: use DosQueryDevCaps to figure it out
510 return flag;
0e320a79
DW
511}
512
513// Returns depth of screen
514int wxDisplayDepth()
515{
45fcbf3b
DW
516 HDC hDc = ::WinOpenWindowDC((HWND)NULL);
517 long lArray[CAPS_COLOR_BITCOUNT];
518 int nPlanes;
519 int nBitsPerPixel;
520 int nDepth;
521
522 if(DevQueryCaps( hDc
523 ,CAPS_FAMILY
524 ,CAPS_COLOR_BITCOUNT
525 ,lArray
526 ))
527 {
528 nPlanes = (int)lArray[CAPS_COLOR_PLANES];
529 nBitsPerPixel = (int)lArray[CAPS_COLOR_BITCOUNT];
530 nDepth = nPlanes * nBitsPerPixel;
531 }
532 DevCloseDC(hDc);
533 return (depth);
0e320a79
DW
534}
535
536// Get size of display
537void wxDisplaySize(int *width, int *height)
538{
45fcbf3b
DW
539 HDC hDc = ::WinOpenWindowDC((HWND)NULL);
540 long lArray[CAPS_HEIGHT];
541
542 if(DevQueryCaps( hDc
543 ,CAPS_FAMILY
544 ,CAPS_HEIGHT
545 ,lArray
546 ))
547 {
548 *pWidth = (int)lArray[CAPS_WIDTH];
549 *pHeight = (int)lArray[CAPS_HEIGHT];
550 }
551 DevCloseDC(hDc);
552}
553
554bool wxDirExists(const wxString& dir)
555{
556 // TODO: Control program file stuff
557 return TRUE;
558}
559
560// ---------------------------------------------------------------------------
561// window information functions
562// ---------------------------------------------------------------------------
563
564wxString WXDLLEXPORT wxGetWindowText(WXHWND hWnd)
565{
566 wxString str;
567 long len = ::WinQueryWindowTextLength((HWND)hWnd) + 1;
568 ::WinQueryWindowText((HWND)hWnd, str.GetWriteBuf((int)len), len);
569 str.UngetWriteBuf();
570
571 return str;
572}
573
574wxString WXDLLEXPORT wxGetWindowClass(WXHWND hWnd)
575{
576 wxString str;
577
578 int len = 256; // some starting value
579
580 for ( ;; )
581 {
582 int count = ::WinQueryClassName((HWND)hWnd, str.GetWriteBuf(len), len);
583
584 str.UngetWriteBuf();
585 if ( count == len )
586 {
587 // the class name might have been truncated, retry with larger
588 // buffer
589 len *= 2;
590 }
591 else
592 {
593 break;
594 }
595 }
596 return str;
597}
598
599WXWORD WXDLLEXPORT wxGetWindowId(WXHWND hWnd)
600{
601 return ::WinQueryWindowUShort((HWND)hWnd, QWS_ID);
0e320a79
DW
602}
603