]> git.saurik.com Git - wxWidgets.git/blobdiff - src/os2/utils.cpp
compile fix for BC++
[wxWidgets.git] / src / os2 / utils.cpp
index 940bdc21bc70afca29338f5312137ea167ba7d5a..10100cb58ae40cd742873087dd5bfa565ac4bd27 100644 (file)
 /////////////////////////////////////////////////////////////////////////////
 // Name:        utils.cpp
 // Purpose:     Various utilities
-// Author:      AUTHOR
+// Author:      David Webster
 // Modified by:
-// Created:     ??/??/98
+// Created:     09/17/99
 // RCS-ID:      $Id$
-// Copyright:   (c) AUTHOR
-// Licence:    wxWindows licence
+// Copyright:   (c) David Webster
+// Licence:     wxWindows license
 /////////////////////////////////////////////////////////////////////////////
 
 #ifdef __GNUG__
-// Note: this is done in utilscmn.cpp now.
-// #pragma implementation
-// #pragma implementation "utils.h"
+// #pragma implementation "utils.h"   // Note: this is done in utilscmn.cpp now.
 #endif
 
-#include "wx/setup.h"
-#include "wx/utils.h"
-#include "wx/app.h"
+// For compilers that support precompilation, includes "wx.h".
+#include "wx/wxprec.h"
+
+#ifndef WX_PRECOMP
+    #include "wx/setup.h"
+    #include "wx/utils.h"
+    #include "wx/app.h"
+    #include "wx/cursor.h"
+#endif  //WX_PRECOMP
+
+#include "wx/os2/private.h"
+#include "wx/timer.h"
+#include "wx/intl.h"
 
 #include <ctype.h>
+#include <direct.h>
+
+#include "wx/log.h"
+
+#include <io.h>
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 #include <stdarg.h>
 
+#define INCL_OS2
+#define INCL_PM
+#include <os2.h>
+#include<netdb.h>
+#include<upm.h>
+
+// In the WIN.INI file
+static const wxChar WX_SECTION[] = _T("wxWindows");
+static const wxChar eHOSTNAME[]  = _T("HostName");
+static const wxChar eUSERID[]    = _T("UserId");
+static const wxChar eUSERNAME[]  = _T("UserName");
+
+// For the following functions we SHOULD fill in support
+// for Windows-NT (which I don't know) as I assume it begin
+// a POSIX Unix (so claims MS) that it has some special
+// functions beyond those provided by WinSock
+
 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
-bool wxGetHostName(char *buf, int maxSize)
+bool wxGetHostName(wxChar *buf, int maxSize)
 {
-    // TODO
-    return FALSE;
+#ifdef USE_NET_API
+  char                               server[256];
+  char                               computer[256];
+  unsigned long                      ulLevel;
+  unsigned char*                     pbBuffer;
+  unsigned long                      ulBuffer;
+  unsigned long*                     pulTotalAvail;
+
+  NetBios32GetInfo( server
+                   ,computer
+                   ,ulLevel
+                   ,pbBuffer
+                   ,ulBuffer
+                   ,pulTotalAvail
+                  );
+  strcpy(buf, server);
+#else
+  wxChar *sysname;
+  const wxChar *default_host = _T("noname");
+
+  if ((sysname = wxGetenv(_T("SYSTEM_NAME"))) == NULL) {
+     GetProfileString(WX_SECTION, eHOSTNAME, default_host, buf, maxSize - 1);
+  } else
+    wxStrncpy(buf, sysname, maxSize - 1);
+  buf[maxSize] = _T('\0');
+#endif
+  return *buf ? TRUE : FALSE;
 }
 
 // Get user ID e.g. jacs
-bool wxGetUserId(char *buf, int maxSize)
+bool wxGetUserId(wxChar *buf, int maxSize)
 {
-    // TODO
-    return FALSE;
+  return(U32ELOCL(bub, maxSize));
 }
 
-// Get user name e.g. AUTHOR
-bool wxGetUserName(char *buf, int maxSize)
+bool wxGetUserName(wxChar *buf, int maxSize)
 {
-    // TODO
-    return FALSE;
+#ifdef USE_NET_API
+    wxGetUserId(buf, maxSize);
+#else
+    bool ok = GetProfileString(WX_SECTION, eUSERNAME, _T(""), buf, maxSize - 1) != 0;
+    if ( !ok )
+    {
+        ok = wxGetUserId(buf, maxSize);
+    }
+
+    if ( !ok )
+    {
+        wxStrncpy(buf, _T("Unknown User"), maxSize);
+    }
+#endif
+  return TRUE;
 }
 
 int wxKill(long pid, int sig)
 {
-    // TODO
-    return 0;
+  return 0;
 }
 
 //
@@ -58,40 +124,81 @@ int wxKill(long pid, int sig)
 //
 bool wxShell(const wxString& command)
 {
-    // TODO
-    return FALSE;
+  wxChar *shell;
+  if ((shell = wxGetenv(_T("COMSPEC"))) == NULL)
+    shell = _T("\\CMD.EXE");
+
+  wxChar tmp[255];
+  if (command != "")
+    wxSprintf(tmp, "%s /c %s", shell, WXSTRINGCAST command);
+  else
+    wxStrcpy(tmp, shell);
+
+  return (wxExecute((wxChar *)tmp, FALSE) != 0);
 }
 
 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
 long wxGetFreeMemory()
 {
-    // TODO
-    return 0;
+  return (long)GetFreeSpace(0);
+}
+
+// Sleep for nSecs seconds. Attempt a Windows implementation using timers.
+static bool inTimer = FALSE;
+
+class wxSleepTimer: public wxTimer
+{
+ public:
+  inline void Notify()
+  {
+    inTimer = FALSE;
+    Stop();
+  }
+};
+
+static wxTimer *wxTheSleepTimer = NULL;
+
+void wxUsleep(unsigned long milliseconds)
+{
+    ::DosSleep(milliseconds);
 }
 
 void wxSleep(int nSecs)
 {
-    // TODO
+  if (inTimer)
+    return;
+
+  wxTheSleepTimer = new wxSleepTimer;
+  inTimer = TRUE;
+  wxTheSleepTimer->Start(nSecs*1000);
+  while (inTimer)
+  {
+    if (wxTheApp->Pending())
+      wxTheApp->Dispatch();
+  }
+  delete wxTheSleepTimer;
+  wxTheSleepTimer = NULL;
 }
 
 // Consume all events until no more left
 void wxFlushEvents()
 {
+//  wxYield();
 }
 
-// Output a debug message, in a system dependent fashion.
-void wxDebugMsg(const char *fmt ...)
+// Output a debug mess., in a system dependent fashion.
+void wxDebugMsg(const wxChar *fmt ...)
 {
   va_list ap;
-  static char buffer[512];
+  static wxChar buffer[512];
 
   if (!wxTheApp->GetWantDebugOutput())
     return ;
 
   va_start(ap, fmt);
 
-  // wvsprintf(buffer,fmt,ap) ;
-  // TODO: output buffer
+   sprintf(buffer,fmt,ap) ;
+   fflush(buffer) ;
 
   va_end(ap);
 }
@@ -99,70 +206,117 @@ void wxDebugMsg(const char *fmt ...)
 // Non-fatal error: pop up message box and (possibly) continue
 void wxError(const wxString& msg, const wxString& title)
 {
-    // TODO
+  wxSprintf(wxBuffer, "%s\nContinue?", WXSTRINGCAST msg);
+  if (::WinMessageBox( HWND_DESKTOP
+                      ,NULL
+                      ,(PSZ)wxBuffer
+                      ,(PSZ)WXSTRINGCAST title
+                      ,MB_ICONEXCLAMATION | MB_YESNO
+                     ) == IDNO)
     wxExit();
 }
 
 // Fatal error: pop up message box and abort
 void wxFatalError(const wxString& msg, const wxString& title)
 {
-    // TODO
+    YUint32                         rc;
+    HWND                            hWnd;
+
+    WinMessageBox( HWND_DESKTOP
+                  ,hWnd
+                  ,rMsg.Data()
+                  ,rTitle.Data()
+                  ,0
+                  ,MB_NOICON | MB_OK
+                 );
+    DosExit(EXIT_PROCESS, rc);
 }
 
 // Emit a beeeeeep
 void wxBell()
 {
-    // TODO
+    DosBeep(1000,1000); // 1kHz during 1 sec.
 }
 
+// Chris Breeze 27/5/98: revised WIN32 code to
+// detect WindowsNT correctly
 int wxGetOsVersion(int *majorVsn, int *minorVsn)
 {
-    // TODO
-    return 0;
+  ULONG                 aulSysInfo[QSV_MAX] = {0};
+
+  if (DosQuerySysInfo( 1L
+                      ,QSV_MAX
+                      ,(PVOID)aulSysInfo
+                      ,sizeof(ULONG) * QSV_MAX
+                     ))
+  {
+     *majorVsn = aulSysInfo[QSV_VERSION_MAJOR];
+     *minorVsn = aulSysInfo[QSV_VERSION_MINOR];
+     return wxWINDOWS_OS2;
+  }
+  return wxWINDOWS; // error if we get here, return generic value
 }
 
 // Reading and writing resources (eg WIN.INI, .Xdefaults)
 #if wxUSE_RESOURCES
 bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
 {
-    // TODO
-    return FALSE;
+  if (file != "")
+    return (WritePrivateProfileString((PCSZ)WXSTRINGCAST section, (PCSZ)WXSTRINGCAST entry, (PCSZ)value, (PCSZ)WXSTRINGCAST file) != 0);
+  else
+    return (WriteProfileString((PCSZ)WXSTRINGCAST section, (PCSZ)WXSTRINGCAST entry, (PCSZ)WXSTRINGCAST value) != 0);
 }
 
 bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
 {
-  char buf[50];
-  sprintf(buf, "%.4f", value);
+  wxChar buf[50];
+  wxSprintf(buf, "%.4f", value);
   return wxWriteResource(section, entry, buf, file);
 }
 
 bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
 {
-  char buf[50];
-  sprintf(buf, "%ld", value);
+  wxChar buf[50];
+  wxSprintf(buf, "%ld", value);
   return wxWriteResource(section, entry, buf, file);
 }
 
 bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
 {
-  char buf[50];
-  sprintf(buf, "%d", value);
+  wxChar buf[50];
+  wxSprintf(buf, "%d", value);
   return wxWriteResource(section, entry, buf, file);
 }
 
-bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
+bool wxGetResource(const wxString& section, const wxString& entry, wxChar **value, const wxString& file)
 {
-    // TODO
-    return FALSE;
-}
+  static const wxChar defunkt[] = _T("$$default");
+  if (file != "")
+  {
+    int n = GetPrivateProfileString((PCSZ)WXSTRINGCAST section, (PCSZ)WXSTRINGCAST entry, (PCSZ)defunkt,
+                                    (PSZ)wxBuffer, 1000, (PCSZ)WXSTRINGCAST file);
+    if (n == 0 || wxStrcmp(wxBuffer, defunkt) == 0)
+     return FALSE;
+  }
+  else
+  {
+    int n = GetProfileString((PCSZ)WXSTRINGCAST section, (PCSZ)WXSTRINGCAST entry, (LPCTSTR)defunkt,
+                                    (PSZ)wxBuffer, 1000);
+    if (n == 0 || wxStrcmp(wxBuffer, defunkt) == 0)
+      return FALSE;
+  }
+  if (*value) delete[] (*value);
+      *value = copystring(wxBuffer);
+      return TRUE;
+    }
 
 bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
 {
-  char *s = NULL;
-  bool succ = wxGetResource(section, entry, (char **)&s, file);
+  wxChar *s = NULL;
+  bool succ = wxGetResource(section, entry, (wxChar **)&s, file);
   if (succ)
   {
-    *value = (float)strtod(s, NULL);
+    *value = (float)wxStrtod(s, NULL);
     delete[] s;
     return TRUE;
   }
@@ -171,11 +325,11 @@ bool wxGetResource(const wxString& section, const wxString& entry, float *value,
 
 bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
 {
-  char *s = NULL;
-  bool succ = wxGetResource(section, entry, (char **)&s, file);
+  wxChar *s = NULL;
+  bool succ = wxGetResource(section, entry, (wxChar **)&s, file);
   if (succ)
   {
-    *value = strtol(s, NULL, 10);
+    *value = wxStrtol(s, NULL, 10);
     delete[] s;
     return TRUE;
   }
@@ -184,89 +338,266 @@ bool wxGetResource(const wxString& section, const wxString& entry, long *value,
 
 bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
 {
-  char *s = NULL;
-  bool succ = wxGetResource(section, entry, (char **)&s, file);
+  wxChar *s = NULL;
+  bool succ = wxGetResource(section, entry, (wxChar **)&s, file);
   if (succ)
   {
-    *value = (int)strtol(s, NULL, 10);
-    delete[] s; 
+    *value = (int)wxStrtol(s, NULL, 10);
+    delete[] s;
     return TRUE;
   }
   else return FALSE;
 }
 #endif // wxUSE_RESOURCES
 
-static int wxBusyCursorCount = 0;
+// ---------------------------------------------------------------------------
+// helper functions for showing a "busy" cursor
+// ---------------------------------------------------------------------------
+
+HCURSOR gs_wxBusyCursor = 0;     // new, busy cursor
+HCURSOR gs_wxBusyCursorOld = 0;  // old cursor
+static int gs_wxBusyCursorCount = 0;
 
 // Set the cursor to the busy cursor for all windows
 void wxBeginBusyCursor(wxCursor *cursor)
 {
-  wxBusyCursorCount ++;
-  if (wxBusyCursorCount == 1)
-  {
-        // TODO
-  }
-  else
-  {
-        // TODO
-  }
+    if ( gs_wxBusyCursorCount++ == 0 )
+    {
+        gs_wxBusyCursor = (HCURSOR)cursor->GetHCURSOR();
+        ::WinSetPointer(HWND_DESKTOP, (HPOINTER)gs_wxBusyCursor);
+    }
+    //else: nothing to do, already set
 }
 
 // Restore cursor to normal
 void wxEndBusyCursor()
 {
-  if (wxBusyCursorCount == 0)
-    return;
-    
-  wxBusyCursorCount --;
-  if (wxBusyCursorCount == 0)
-  {
-    // TODO
-  }
+    wxCHECK_RET( gs_wxBusyCursorCount > 0,
+                 _T("no matching wxBeginBusyCursor() for wxEndBusyCursor()"));
+
+    if ( --gs_wxBusyCursorCount == 0 )
+    {
+        ::WinSetPointer(HWND_DESKTOP, (HPOINTER)gs_wxBusyCursorOld);
+        gs_wxBusyCursorOld = 0;
+    }
 }
 
 // TRUE if we're between the above two calls
 bool wxIsBusy()
 {
-  return (wxBusyCursorCount > 0);
-}    
+  return (gs_wxBusyCursorCount > 0);
+}
+
+// ---------------------------------------------------------------------------
+const wxChar* wxGetHomeDir(wxString *pstr)
+{
+  wxString& strDir = *pstr;
 
-char *wxGetUserHome (const wxString& user)
+  // OS/2 has no idea about home,
+  // so use the working directory instead?
+
+  // 256 was taken from os2def.h
+#ifndef MAX_PATH
+#  define MAX_PATH  256
+#endif
+
+
+    char     DirName[256];
+    ULONG    DirLen;
+
+    ::DosQueryCurrentDir( 0, DirName, &DirLen);
+    strDir = DirName;
+  return strDir.c_str();
+}
+
+// Hack for MS-DOS
+wxChar *wxGetUserHome (const wxString& user)
 {
-    // TODO
-    return NULL;
+  wxChar *home;
+  wxString user1(user);
+
+  if (user1 != _T("")) {
+    wxChar tmp[64];
+    if (wxGetUserId(tmp, sizeof(tmp)/sizeof(char))) {
+      // Guests belong in the temp dir
+      if (wxStricmp(tmp, _T("annonymous")) == 0) {
+        if ((home = wxGetenv(_T("TMP"))) != NULL ||
+            (home = wxGetenv(_T("TMPDIR"))) != NULL ||
+            (home = wxGetenv(_T("TEMP"))) != NULL)
+            return *home ? home : (wxChar*)_T("\\");
+      }
+      if (wxStricmp(tmp, WXSTRINGCAST user1) == 0)
+        user1 = _T("");
+    }
+  }
+  if (user1 == _T(""))
+    if ((home = wxGetenv(_T("HOME"))) != NULL)
+    {
+      wxStrcpy(wxBuffer, home);
+      Unix2DosFilename(wxBuffer);
+      return wxBuffer;
+    }
+  return NULL; // No home known!
 }
 
 // Check whether this window wants to process messages, e.g. Stop button
 // in long calculations.
 bool wxCheckForInterrupt(wxWindow *wnd)
 {
-    // TODO
-    return FALSE;
+  if(wnd){
+    QMSG            msg;
+    HAB             hab;
+    HWND            hwndFilter;
+
+    HWND win= (HWND) wnd->GetHWND();
+    while(::WinPeekMsg(hab,&msg,hwndFilter,0,0,PM_REMOVE))
+    {
+      ::WinDispatchMsg( hab, &qmsg );
+    }
+    return TRUE;//*** temporary?
+  }
+  else{
+    wxFAIL_MSG(_T("wnd==NULL !!!"));
+
+    return FALSE;//*** temporary?
+  }
+}
+
+wxChar *wxLoadUserResource(const wxString& resourceName, const wxString& resourceType)
+{
+  wxChar *s = NULL;
+
+/*
+*  How to in PM?
+*
+* #if !defined(__WIN32__) || defined(__TWIN32__)
+*   HRSRC hResource = ::FindResource(wxGetInstance(), WXSTRINGCAST resourceName, WXSTRINGCAST resourceType);
+* #else
+* #ifdef UNICODE
+*   HRSRC hResource = ::FindResourceW(wxGetInstance(), WXSTRINGCAST resourceName, WXSTRINGCAST resourceType);
+* #else
+*   HRSRC hResource = ::FindResourceA(wxGetInstance(), WXSTRINGCAST resourceName, WXSTRINGCAST resourceType);
+* #endif
+* #endif
+*
+*   if (hResource == 0)
+*     return NULL;
+*   HGLOBAL hData = ::LoadResource(wxGetInstance(), hResource);
+*   if (hData == 0)
+*     return NULL;
+*   wxChar *theText = (wxChar *)LockResource(hData);
+*   if (!theText)
+*     return NULL;
+*/
+  s = copystring(theText);
+
+  return s;
 }
 
 void wxGetMousePosition( int* x, int* y )
 {
-    // TODO
+  POINT pt;
+  GetCursorPos( & pt );
+  *x = pt.x;
+  *y = pt.y;
 };
 
 // Return TRUE if we have a colour display
 bool wxColourDisplay()
 {
-    // TODO
-    return TRUE;
+  bool flag;
+  // TODO:  use DosQueryDevCaps to figure it out
+  return flag;
 }
 
 // Returns depth of screen
 int wxDisplayDepth()
 {
-    // TODO
-    return 0;
+    HDC                             hDc = ::WinOpenWindowDC((HWND)NULL);
+    long                            lArray[CAPS_COLOR_BITCOUNT];
+    int                             nPlanes;
+    int                             nBitsPerPixel;
+    int                             nDepth;
+
+    if(DevQueryCaps( hDc
+                    ,CAPS_FAMILY
+                    ,CAPS_COLOR_BITCOUNT
+                    ,lArray
+                   ))
+    {
+        nPlanes = (int)lArray[CAPS_COLOR_PLANES];
+        nBitsPerPixel = (int)lArray[CAPS_COLOR_BITCOUNT];
+        nDepth = nPlanes * nBitsPerPixel;
+    }
+    DevCloseDC(hDc);
+    return (depth);
 }
 
 // Get size of display
 void wxDisplaySize(int *width, int *height)
 {
-    // TODO
+    HDC                             hDc = ::WinOpenWindowDC((HWND)NULL);
+    long                            lArray[CAPS_HEIGHT];
+
+    if(DevQueryCaps( hDc
+                    ,CAPS_FAMILY
+                    ,CAPS_HEIGHT
+                    ,lArray
+                   ))
+    {
+        *pWidth  = (int)lArray[CAPS_WIDTH];
+        *pHeight = (int)lArray[CAPS_HEIGHT];
+    }
+    DevCloseDC(hDc);
+}
+
+bool wxDirExists(const wxString& dir)
+{
+   // TODO:  Control program file stuff
+   return TRUE;
+}
+
+// ---------------------------------------------------------------------------
+// window information functions
+// ---------------------------------------------------------------------------
+
+wxString WXDLLEXPORT wxGetWindowText(WXHWND hWnd)
+{
+    wxString str;
+    long len = ::WinQueryWindowTextLength((HWND)hWnd) + 1;
+    ::WinQueryWindowText((HWND)hWnd, str.GetWriteBuf((int)len), len);
+    str.UngetWriteBuf();
+
+    return str;
+}
+
+wxString WXDLLEXPORT wxGetWindowClass(WXHWND hWnd)
+{
+    wxString str;
+
+    int len = 256; // some starting value
+
+    for ( ;; )
+    {
+        int count = ::WinQueryClassName((HWND)hWnd, str.GetWriteBuf(len), len);
+
+        str.UngetWriteBuf();
+        if ( count == len )
+        {
+            // the class name might have been truncated, retry with larger
+            // buffer
+            len *= 2;
+        }
+        else
+        {
+            break;
+        }
+    }
+    return str;
+}
+
+WXWORD WXDLLEXPORT wxGetWindowId(WXHWND hWnd)
+{
+    return ::WinQueryWindowUShort((HWND)hWnd, QWS_ID);
 }