/////////////////////////////////////////////////////////////////////////////
-// Name: mse/utils.cpp
+// Name: msw/utils.cpp
// Purpose: Various utilities
// Author: Julian Smart
// Modified by:
#include "wx/utils.h"
#include "wx/app.h"
#include "wx/cursor.h"
+ #include "wx/intl.h"
+ #include "wx/log.h"
#endif //WX_PRECOMP
+// In some mingws there is a missing extern "C" int the winsock header,
+// so we put it here just to be safe. Note that this must appear _before_
+// #include "wx/msw/private.h" which itself includes <windows.h>, as this
+// one in turn includes <winsock.h> unless we define WIN32_LEAN_AND_MEAN.
+//
+#if defined(__WIN32__) && !defined(__TWIN32__) && ! (defined(__GNUWIN32__) && !defined(__MINGW32__))
+extern "C" {
+ #include <winsock.h> // we use socket functions in wxGetFullHostName()
+}
+#endif
+
#include "wx/msw/private.h" // includes <windows.h>
#include "wx/timer.h"
-#include "wx/intl.h"
#include <ctype.h>
#include <sys/stat.h>
#endif //GNUWIN32
-#include "wx/log.h"
-
#ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
// this (3.1 I believe) and how to test for it.
// If this works for Borland 4.0 as well, then no worries.
#endif
# if defined(__WXDEBUG__) && wxUSE_GLOBAL_MEMORY_OPERATORS && wxUSE_DEBUG_NEW_ALWAYS
-# define new new(__FILE__,__LINE__)
+# define new new(__TFILE__,__LINE__)
# endif
#endif
// get host name and related
// ----------------------------------------------------------------------------
-// Get full hostname (eg. DoDo.BSn-Germany.crg.de)
+// Get hostname only (without domain name)
bool wxGetHostName(wxChar *buf, int maxSize)
{
#if defined(__WIN32__) && !defined(__TWIN32__)
- // TODO should use GetComputerNameEx() when available
-
DWORD nSize = maxSize;
if ( !::GetComputerName(buf, &nSize) )
{
- wxLogLastError("GetComputerName");
+ wxLogLastError(wxT("GetComputerName"));
return FALSE;
}
#endif
}
+// get full hostname (with domain name if possible)
bool wxGetFullHostName(wxChar *buf, int maxSize)
{
+#if defined(__WIN32__) && !defined(__TWIN32__) && ! (defined(__GNUWIN32__) && !defined(__MINGW32__))
+ // TODO should use GetComputerNameEx() when available
+ WSADATA wsa;
+ if ( WSAStartup(MAKEWORD(1, 1), &wsa) == 0 )
+ {
+ wxString host;
+ char bufA[256];
+ if ( gethostname(bufA, WXSIZEOF(bufA)) == 0 )
+ {
+ // gethostname() won't usually include the DNS domain name, for
+ // this we need to work a bit more
+ if ( !strchr(bufA, '.') )
+ {
+ struct hostent *pHostEnt = gethostbyname(bufA);
+
+ if ( pHostEnt )
+ {
+ // Windows will use DNS internally now
+ pHostEnt = gethostbyaddr(pHostEnt->h_addr, 4, PF_INET);
+ }
+
+ if ( pHostEnt )
+ {
+ host = pHostEnt->h_name;
+ }
+ }
+ }
+
+ WSACleanup();
+
+ if ( !!host )
+ {
+ wxStrncpy(buf, host, maxSize);
+
+ return TRUE;
+ }
+ }
+#endif // Win32
+
return wxGetHostName(buf, maxSize);
}
return strDir.c_str();
}
-wxChar *wxGetUserHome(const wxString& user)
+wxChar *wxGetUserHome(const wxString& WXUNUSED(user))
{
// VZ: the old code here never worked for user != "" anyhow! Moreover, it
// returned sometimes a malloc()'d pointer, sometimes a pointer to a
bool wxDirExists(const wxString& dir)
{
#if defined(__WIN32__)
- WIN32_FIND_DATA fileInfo;
+ DWORD attribs = GetFileAttributes(dir);
+ return ((attribs != (DWORD)-1) && (attribs & FILE_ATTRIBUTE_DIRECTORY));
#else // Win16
#ifdef __BORLANDC__
struct ffblk fileInfo;
#else
struct find_t fileInfo;
#endif
-#endif // Win32/16
-
-#if defined(__WIN32__)
- HANDLE h = ::FindFirstFile(dir, &fileInfo);
-
- if ( h == INVALID_HANDLE_VALUE )
- {
- wxLogLastError("FindFirstFile");
-
- return FALSE;
- }
-
- ::FindClose(h);
-
- return (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
-#else // Win16
// In Borland findfirst has a different argument
// ordering from _dos_findfirst. But _dos_findfirst
// _should_ be ok in both MS and Borland... why not?
#endif // Win32/16
}
+// ----------------------------------------------------------------------------
+// env vars
+// ----------------------------------------------------------------------------
+
+bool wxGetEnv(const wxString& var, wxString *value)
+{
+#ifdef __WIN16__
+ const wxChar* ret = wxGetenv(var);
+ if (ret)
+ {
+ *value = ret;
+ return TRUE;
+ }
+ else
+ return FALSE;
+#else
+ // first get the size of the buffer
+ DWORD dwRet = ::GetEnvironmentVariable(var, NULL, 0);
+ if ( !dwRet )
+ {
+ // this means that there is no such variable
+ return FALSE;
+ }
+
+ if ( value )
+ {
+ (void)::GetEnvironmentVariable(var, value->GetWriteBuf(dwRet), dwRet);
+ value->UngetWriteBuf();
+ }
+
+ return TRUE;
+#endif
+}
+
+bool wxSetEnv(const wxString& var, const wxChar *value)
+{
+ // some compilers have putenv() or _putenv() or _wputenv() but it's better
+ // to always use Win32 function directly instead of dealing with them
+#if defined(__WIN32__)
+ if ( !::SetEnvironmentVariable(var, value) )
+ {
+ wxLogLastError(_T("SetEnvironmentVariable"));
+
+ return FALSE;
+ }
+
+ return TRUE;
+#else // no way to set env vars
+ return FALSE;
+#endif
+}
+
// ----------------------------------------------------------------------------
// process management
// ----------------------------------------------------------------------------
-int wxKill(long pid, int sig)
+int wxKill(long WXUNUSED(pid), int WXUNUSED(sig))
{
// TODO use SendMessage(WM_QUIT) and TerminateProcess() if needed
cmd.Printf(wxT("%s /c %s"), shell, command.c_str());
}
- return wxExecute(cmd, FALSE) != 0;
+ return wxExecute(cmd, TRUE /* sync */) != 0;
}
// ----------------------------------------------------------------------------
::MessageBeep((UINT)-1); // default sound
}
-// Chris Breeze 27/5/98: revised WIN32 code to
-// detect WindowsNT correctly
-int wxGetOsVersion(int *majorVsn, int *minorVsn)
+wxString wxGetOsDescription()
{
- if (majorVsn) *majorVsn = 0;
- if (minorVsn) *minorVsn = 0;
+#ifdef __WIN32__
+ wxString str;
+
+ OSVERSIONINFO info;
+ wxZeroMemory(info);
+ info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
+ if ( ::GetVersionEx(&info) )
+ {
+ switch ( info.dwPlatformId )
+ {
+ case VER_PLATFORM_WIN32s:
+ str = _("Win32s on Windows 3.1");
+ break;
+
+ case VER_PLATFORM_WIN32_WINDOWS:
+ str.Printf(_("Windows 9%c"),
+ info.dwMinorVersion == 0 ? _T('5') : _T('8'));
+ if ( !wxIsEmpty(info.szCSDVersion) )
+ {
+ str << _T(" (") << info.szCSDVersion << _T(')');
+ }
+ break;
+
+ case VER_PLATFORM_WIN32_NT:
+ str.Printf(_T("Windows NT %lu.%lu (build %lu"),
+ info.dwMajorVersion,
+ info.dwMinorVersion,
+ info.dwBuildNumber);
+ if ( !wxIsEmpty(info.szCSDVersion) )
+ {
+ str << _T(", ") << info.szCSDVersion;
+ }
+ str << _T(')');
+ break;
+ }
+ }
+ else
+ {
+ wxFAIL_MSG( _T("GetVersionEx() failed") ); // should never happen
+ }
+
+ return str;
+#else // Win16
+ return _("Windows 3.1");
+#endif // Win32/16
+}
+
+int wxGetOsVersion(int *majorVsn, int *minorVsn)
+{
#if defined(__WIN32__) && !defined(__SC__)
- OSVERSIONINFO info;
- memset(&info, 0, sizeof(OSVERSIONINFO));
- info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
- if (GetVersionEx(&info))
- {
- if (majorVsn) *majorVsn = info.dwMajorVersion;
- if (minorVsn) *minorVsn = info.dwMinorVersion;
- switch (info.dwPlatformId)
- {
- case VER_PLATFORM_WIN32s:
- return wxWIN32S;
- break;
- case VER_PLATFORM_WIN32_WINDOWS:
- return wxWIN95;
- break;
- case VER_PLATFORM_WIN32_NT:
- return wxWINDOWS_NT;
- break;
- }
- }
- return wxWINDOWS; // error if we get here, return generic value
-#else
- // Win16 code...
- int retValue = 0;
-# ifdef __WINDOWS_386__
- retValue = wxWIN386;
-# else
-# if !defined(__WATCOMC__) && !defined(GNUWIN32) && wxUSE_PENWINDOWS
- extern HANDLE g_hPenWin;
- retValue = g_hPenWin ? wxPENWINDOWS : wxWINDOWS ;
-# endif
-# endif
- // @@@@ To be completed. I don't have the manual here...
- if (majorVsn) *majorVsn = 3 ;
- if (minorVsn) *minorVsn = 1 ;
- return retValue ;
+ OSVERSIONINFO info;
+ wxZeroMemory(info);
+
+ info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
+ if ( ::GetVersionEx(&info) )
+ {
+ if (majorVsn)
+ *majorVsn = info.dwMajorVersion;
+ if (minorVsn)
+ *minorVsn = info.dwMinorVersion;
+
+ switch ( info.dwPlatformId )
+ {
+ case VER_PLATFORM_WIN32s:
+ return wxWIN32S;
+
+ case VER_PLATFORM_WIN32_WINDOWS:
+ return wxWIN95;
+
+ case VER_PLATFORM_WIN32_NT:
+ return wxWINDOWS_NT;
+ }
+ }
+
+ return wxWINDOWS; // error if we get here, return generic value
+#else // Win16
+ int retValue = wxWINDOWS;
+ #ifdef __WINDOWS_386__
+ retValue = wxWIN386;
+ #else
+ #if !defined(__WATCOMC__) && !defined(GNUWIN32) && wxUSE_PENWINDOWS
+ extern HANDLE g_hPenWin;
+ retValue = g_hPenWin ? wxPENWINDOWS : wxWINDOWS;
+ #endif
+ #endif
+
+ if (majorVsn)
+ *majorVsn = 3;
+ if (minorVsn)
+ *minorVsn = 1;
+
+ return retValue;
#endif
}
// helper functions for showing a "busy" cursor
// ---------------------------------------------------------------------------
-HCURSOR gs_wxBusyCursor = 0; // new, busy cursor
-HCURSOR gs_wxBusyCursorOld = 0; // old cursor
+static HCURSOR gs_wxBusyCursor = 0; // new, busy cursor
+static HCURSOR gs_wxBusyCursorOld = 0; // old cursor
static int gs_wxBusyCursorCount = 0;
+extern HCURSOR wxGetCurrentBusyCursor()
+{
+ return gs_wxBusyCursor;
+}
+
// Set the cursor to the busy cursor for all windows
void wxBeginBusyCursor(wxCursor *cursor)
{
return TRUE;
}
-#endif // wxUSE_GUI
-
// MSW only: get user-defined resource from the .res file.
// Returns NULL or newly-allocated memory, so use delete[] to clean up.
if ( !theText )
return NULL;
- wxChar *s = copystring(theText);
+ // Not all compilers put a zero at the end of the resource (e.g. BC++ doesn't).
+ // so we need to find the length of the resource.
+ int len = ::SizeofResource(wxGetInstance(), hResource);
+ wxChar *s = new wxChar[len+1];
+ wxStrncpy(s,theText,len);
+ s[len]=0;
+
+ // wxChar *s = copystring(theText);
// Obsolete in WIN32
#ifndef __WIN32__
// get display info
// ----------------------------------------------------------------------------
+// See also the wxGetMousePosition in window.cpp
+// Deprecated: use wxPoint wxGetMousePosition() instead
void wxGetMousePosition( int* x, int* y )
{
POINT pt;
// Return TRUE if we have a colour display
bool wxColourDisplay()
{
- ScreenHDC dc;
- int noCols = GetDeviceCaps(dc, NUMCOLORS);
+ // this function is called from wxDC ctor so it is called a *lot* of times
+ // hence we optimize it a bit but doign the check only once
+ //
+ // this should be MT safe as only the GUI thread (holding the GUI mutex)
+ // can call us
+ static int s_isColour = -1;
+
+ if ( s_isColour == -1 )
+ {
+ ScreenHDC dc;
+ int noCols = ::GetDeviceCaps(dc, NUMCOLORS);
- return (noCols == -1) || (noCols > 2);
+ s_isColour = (noCols == -1) || (noCols > 2);
+ }
+
+ return s_isColour != 0;
}
// Returns depth of screen
if ( height ) *height = GetDeviceCaps(dc, VERTRES);
}
+void wxDisplaySizeMM(int *width, int *height)
+{
+ ScreenHDC dc;
+
+ if ( width ) *width = GetDeviceCaps(dc, HORZSIZE);
+ if ( height ) *height = GetDeviceCaps(dc, VERTSIZE);
+}
+
+void wxClientDisplayRect(int *x, int *y, int *width, int *height)
+{
+#ifdef __WIN16__
+ *x = 0; *y = 0;
+ wxDisplaySize(width, height);
+#else
+ // Determine the desktop dimensions minus the taskbar and any other
+ // special decorations...
+ RECT r;
+
+ SystemParametersInfo(SPI_GETWORKAREA, 0, &r, 0);
+ if (x) *x = r.left;
+ if (y) *y = r.top;
+ if (width) *width = r.right - r.left;
+ if (height) *height = r.bottom - r.top;
+#endif
+}
+
+
// ---------------------------------------------------------------------------
// window information functions
// ---------------------------------------------------------------------------
WXWORD WXDLLEXPORT wxGetWindowId(WXHWND hWnd)
{
#ifndef __WIN32__
- return GetWindowWord((HWND)hWnd, GWW_ID);
+ return (WXWORD)GetWindowWord((HWND)hWnd, GWW_ID);
#else // Win32
- return GetWindowLong((HWND)hWnd, GWL_ID);
+ return (WXWORD)GetWindowLong((HWND)hWnd, GWL_ID);
#endif // Win16/32
}
+#endif // wxUSE_GUI
+
#if 0
//------------------------------------------------------------------------
// wild character routines
return ((*str == '\0') && (*pattern == '\0'));
};
-#endif
+#endif // 0
#if 0