]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/utils.cpp
fixed the bug with the order of 2 size events when the scrollbar[s] (dis)appear in...
[wxWidgets.git] / src / msw / utils.cpp
index f9db6a472b51f1a2914aa9e8784bf4208478bc68..d34d0ede3e952404493523bd24f5a35fcef40995 100644 (file)
 #ifndef WX_PRECOMP
     #include "wx/utils.h"
     #include "wx/app.h"
 #ifndef WX_PRECOMP
     #include "wx/utils.h"
     #include "wx/app.h"
-    #include "wx/cursor.h"
     #include "wx/intl.h"
     #include "wx/log.h"
     #include "wx/intl.h"
     #include "wx/log.h"
+#if wxUSE_GUI
+    #include "wx/cursor.h"
+#endif
 #endif  //WX_PRECOMP
 
 // In some mingws there is a missing extern "C" int the winsock header,
 #endif  //WX_PRECOMP
 
 // In some mingws there is a missing extern "C" int the winsock header,
@@ -449,6 +451,99 @@ bool wxDirExists(const wxString& dir)
 #endif // Win32/16
 }
 
 #endif // Win32/16
 }
 
+bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
+{
+    if ( path.empty() )
+        return FALSE;
+
+#ifdef __WIN32__
+    // GetDiskFreeSpaceEx() is not available under original Win95, check for
+    // it
+    typedef BOOL (*GetDiskFreeSpaceEx_t)(LPCTSTR,
+                                         PULARGE_INTEGER,
+                                         PULARGE_INTEGER,
+                                         PULARGE_INTEGER);
+
+    GetDiskFreeSpaceEx_t
+        pGetDiskFreeSpaceEx = (GetDiskFreeSpaceEx_t)::GetProcAddress
+                              (
+                                ::GetModuleHandle(_T("kernel32.dll")),
+#if wxUSE_UNICODE
+                                "GetDiskFreeSpaceExW"
+#else
+                                "GetDiskFreeSpaceExA"
+#endif
+                              );
+
+    if ( pGetDiskFreeSpaceEx )
+    {
+        ULARGE_INTEGER bytesFree, bytesTotal;
+
+        // may pass the path as is, GetDiskFreeSpaceEx() is smart enough
+        if ( !pGetDiskFreeSpaceEx(path,
+                                  &bytesFree,
+                                  &bytesTotal,
+                                  NULL) )
+        {
+            wxLogLastError(_T("GetDiskFreeSpaceEx"));
+
+            return FALSE;
+        }
+
+        if ( pTotal )
+        {
+            *pTotal = wxLongLong(bytesTotal.HighPart, bytesTotal.LowPart);
+        }
+
+        if ( pFree )
+        {
+            *pFree = wxLongLong(bytesFree.HighPart, bytesFree.LowPart);
+        }
+    }
+    else
+#endif // Win32
+    {
+        // there's a problem with drives larger than 2GB, GetDiskFreeSpaceEx()
+        // should be used instead - but if it's not available, fall back on
+        // GetDiskFreeSpace() nevertheless...
+
+        DWORD lSectorsPerCluster,
+              lBytesPerSector,
+              lNumberOfFreeClusters,
+              lTotalNumberOfClusters;
+
+        // FIXME: this is wrong, we should extract the root drive from path
+        //        instead, but this is the job for wxFileName...
+        if ( !::GetDiskFreeSpace(path,
+                                 &lSectorsPerCluster,
+                                 &lBytesPerSector,
+                                 &lNumberOfFreeClusters,
+                                 &lTotalNumberOfClusters) )
+        {
+            wxLogLastError(_T("GetDiskFreeSpace"));
+
+            return FALSE;
+        }
+
+        wxLongLong lBytesPerCluster = lSectorsPerCluster;
+        lBytesPerCluster *= lBytesPerSector;
+
+        if ( pTotal )
+        {
+            *pTotal = lBytesPerCluster;
+            *pTotal *= lTotalNumberOfClusters;
+        }
+
+        if ( pFree )
+        {
+            *pFree = lBytesPerCluster;
+            *pFree *= lNumberOfFreeClusters;
+        }
+    }
+
+    return TRUE;
+}
+
 // ----------------------------------------------------------------------------
 // env vars
 // ----------------------------------------------------------------------------
 // ----------------------------------------------------------------------------
 // env vars
 // ----------------------------------------------------------------------------