]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/app.cpp
[ 1936700 ] wxCAL_SHOW_WEEK_NUMBERS, slightly modified
[wxWidgets.git] / src / msw / app.cpp
index 537b447d18b39b03329cc172a0b0a7b784f08bce..95d953241ce2c6b1671a5f9a278f4be32a480638 100644 (file)
 #include "wx/filename.h"
 #include "wx/dynlib.h"
 #include "wx/evtloop.h"
+#include "wx/thread.h"
 
 #include "wx/msw/private.h"
+#include "wx/msw/dc.h"
 #include "wx/msw/ole/oleutils.h"
 #include "wx/msw/private/timer.h"
 
@@ -60,7 +62,7 @@
 
 // OLE is used for drag-and-drop, clipboard, OLE Automation..., but some
 // compilers don't support it (missing headers, libs, ...)
-#if defined(__GNUWIN32_OLD__) || defined(__SYMANTEC__) || defined(__SALFORDC__)
+#if defined(__GNUWIN32_OLD__) || defined(__SYMANTEC__)
     #undef wxUSE_OLE
 
     #define  wxUSE_OLE 0
@@ -353,8 +355,6 @@ bool wxApp::Initialize(int& argc, wxChar **argv)
 
     RegisterWindowClasses();
 
-    wxWinHandleHash = new wxWinHashTable(wxKEY_INTEGER, 100);
-
 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
     wxSetKeyboardHook(true);
 #endif
@@ -508,10 +508,10 @@ bool wxApp::UnregisterWindowClasses()
 
 void wxApp::CleanUp()
 {
-    // all objects pending for deletion must be deleted first, otherwise we
-    // would crash when they use wxWinHandleHash (and UnregisterWindowClasses()
-    // call wouldn't succeed as long as any windows still exist), so call the
-    // base class method first and only then do our clean up
+    // all objects pending for deletion must be deleted first, otherwise
+    // UnregisterWindowClasses() call wouldn't succeed (because windows
+    // using the classes being unregistered still exist), so call the base
+    // class method first and only then do our clean up
     wxAppBase::CleanUp();
 
 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
@@ -526,9 +526,6 @@ void wxApp::CleanUp()
     // unregister the classes now
     UnregisterWindowClasses();
 
-    delete wxWinHandleHash;
-    wxWinHandleHash = NULL;
-
 #ifdef __WXWINCE__
     free( wxCanvasClassName );
     free( wxCanvasClassNameNR );
@@ -559,7 +556,7 @@ void wxApp::OnIdle(wxIdleEvent& WXUNUSED(event))
     // if it's likely that the app has finished with them, that is, we
     // get an idle event and we're not dragging anything.
     if (!::GetKeyState(MK_LBUTTON) && !::GetKeyState(MK_MBUTTON) && !::GetKeyState(MK_RBUTTON))
-        wxDC::ClearCache();
+        wxMSWDCImpl::ClearCache();
 #endif // wxUSE_DC_CACHEING
 }
 
@@ -569,13 +566,28 @@ void wxApp::WakeUpIdle()
     // start up again.  Doing it this way ensures that the idle handler
     // wakes up in the right thread (see also wxWakeUpMainThread() which does
     // the same for the main app thread only)
-    wxWindow *topWindow = wxTheApp->GetTopWindow();
+    wxWindow * const topWindow = wxTheApp->GetTopWindow();
     if ( topWindow )
     {
-        if ( !::PostMessage(GetHwndOf(topWindow), WM_NULL, 0, 0) )
+        HWND hwndTop = GetHwndOf(topWindow);
+
+        // Do not post WM_NULL if there's already a pending WM_NULL to avoid
+        // overflowing the message queue.
+        //
+        // Notice that due to a limitation of PeekMessage() API (which handles
+        // 0,0 range specially), we have to check the range from 0-1 instead.
+        // This still makes it possible to overflow the queue with WM_NULLs by
+        // interspersing the calles to WakeUpIdle() with windows creation but
+        // it should be rather hard to do it accidentally.
+        MSG msg;
+        if ( !::PeekMessage(&msg, hwndTop, 0, 1, PM_NOREMOVE) ||
+              ::PeekMessage(&msg, hwndTop, 1, 1, PM_NOREMOVE) )
         {
-            // should never happen
-            wxLogLastError(wxT("PostMessage(WM_NULL)"));
+            if ( !::PostMessage(hwndTop, WM_NULL, 0, 0) )
+            {
+                // should never happen
+                wxLogLastError(wxT("PostMessage(WM_NULL)"));
+            }
         }
     }
 }
@@ -586,8 +598,17 @@ void wxApp::WakeUpIdle()
 
 void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
 {
-    if (GetTopWindow())
-        GetTopWindow()->Close(true);
+    // Windows will terminate the process soon after we return from
+    // WM_ENDSESSION handler anyhow, so make sure we at least execute our
+    // cleanup code before
+    const int rc = OnExit();
+
+    wxEntryCleanup();
+
+    // calling exit() instead of ExitProcess() or not doing anything at all and
+    // being killed by Windows has the advantage of executing the dtors of
+    // global objects
+    exit(rc);
 }
 
 // Default behaviour: close the application with prompts. The
@@ -602,15 +623,45 @@ void wxApp::OnQueryEndSession(wxCloseEvent& event)
 }
 
 // ----------------------------------------------------------------------------
-// miscellaneous
+// system DLL versions
 // ----------------------------------------------------------------------------
 
+// these functions have trivial inline implementations for CE
+#ifndef __WXWINCE__
+
+#if wxUSE_DYNLIB_CLASS
+
+namespace
+{
+
+// helper function: retrieve the DLL version by using DllGetVersion(), returns
+// 0 if the DLL doesn't export such function
+int CallDllGetVersion(wxDynamicLibrary& dll)
+{
+    // now check if the function is available during run-time
+    wxDYNLIB_FUNCTION( DLLGETVERSIONPROC, DllGetVersion, dll );
+    if ( !pfnDllGetVersion )
+        return 0;
+
+    DLLVERSIONINFO dvi;
+    dvi.cbSize = sizeof(dvi);
+
+    HRESULT hr = (*pfnDllGetVersion)(&dvi);
+    if ( FAILED(hr) )
+    {
+        wxLogApiError(_T("DllGetVersion"), hr);
+
+        return 0;
+    }
+
+    return 100*dvi.dwMajorVersion + dvi.dwMinorVersion;
+}
+
+} // anonymous namespace
+
 /* static */
 int wxApp::GetComCtl32Version()
 {
-#if defined(__WXMICROWIN__) || defined(__WXWINCE__)
-    return 0;
-#else
     // cache the result
     //
     // NB: this is MT-ok as in the worst case we'd compute s_verComCtl32 twice,
@@ -619,78 +670,105 @@ int wxApp::GetComCtl32Version()
 
     if ( s_verComCtl32 == -1 )
     {
-        // initally assume no comctl32.dll at all
-        s_verComCtl32 = 0;
-
         // we're prepared to handle the errors
         wxLogNull noLog;
 
-#if wxUSE_DYNLIB_CLASS
-        // do we have it?
+        // the DLL should really be available
         wxDynamicLibrary dllComCtl32(_T("comctl32.dll"), wxDL_VERBATIM);
+        if ( !dllComCtl32.IsLoaded() )
+        {
+            s_verComCtl32 = 0;
+            return 0;
+        }
+
+        // try DllGetVersion() for recent DLLs
+        s_verComCtl32 = CallDllGetVersion(dllComCtl32);
 
-        // if so, then we can check for the version
-        if ( dllComCtl32.IsLoaded() )
+        // if DllGetVersion() is unavailable either during compile or
+        // run-time, try to guess the version otherwise
+        if ( !s_verComCtl32 )
         {
-            // now check if the function is available during run-time
-            wxDYNLIB_FUNCTION( DLLGETVERSIONPROC, DllGetVersion, dllComCtl32 );
-            if ( pfnDllGetVersion )
+            // InitCommonControlsEx is unique to 4.70 and later
+            void *pfn = dllComCtl32.GetSymbol(_T("InitCommonControlsEx"));
+            if ( !pfn )
             {
-                DLLVERSIONINFO dvi;
-                dvi.cbSize = sizeof(dvi);
-
-                HRESULT hr = (*pfnDllGetVersion)(&dvi);
-                if ( FAILED(hr) )
-                {
-                    wxLogApiError(_T("DllGetVersion"), hr);
-                }
-                else
-                {
-                    // this is incompatible with _WIN32_IE values, but
-                    // compatible with the other values returned by
-                    // GetComCtl32Version()
-                    s_verComCtl32 = 100*dvi.dwMajorVersion +
-                                        dvi.dwMinorVersion;
-                }
+                // not found, must be 4.00
+                s_verComCtl32 = 400;
             }
-
-            // if DllGetVersion() is unavailable either during compile or
-            // run-time, try to guess the version otherwise
-            if ( !s_verComCtl32 )
+            else // 4.70+
             {
-                // InitCommonControlsEx is unique to 4.70 and later
-                void *pfn = dllComCtl32.GetSymbol(_T("InitCommonControlsEx"));
+                // many symbols appeared in comctl32 4.71, could use any of
+                // them except may be DllInstall()
+                pfn = dllComCtl32.GetSymbol(_T("InitializeFlatSB"));
                 if ( !pfn )
                 {
-                    // not found, must be 4.00
-                    s_verComCtl32 = 400;
+                    // not found, must be 4.70
+                    s_verComCtl32 = 470;
                 }
-                else // 4.70+
+                else
                 {
-                    // many symbols appeared in comctl32 4.71, could use any of
-                    // them except may be DllInstall()
-                    pfn = dllComCtl32.GetSymbol(_T("InitializeFlatSB"));
-                    if ( !pfn )
-                    {
-                        // not found, must be 4.70
-                        s_verComCtl32 = 470;
-                    }
-                    else
-                    {
-                        // found, must be 4.71 or later
-                        s_verComCtl32 = 471;
-                    }
+                    // found, must be 4.71 or later
+                    s_verComCtl32 = 471;
                 }
             }
         }
-#endif
     }
 
     return s_verComCtl32;
-#endif // Microwin/!Microwin
 }
 
+/* static */
+int wxApp::GetShell32Version()
+{
+    static int s_verShell32 = -1;
+    if ( s_verShell32 == -1 )
+    {
+        // we're prepared to handle the errors
+        wxLogNull noLog;
+
+        wxDynamicLibrary dllShell32(_T("shell32.dll"), wxDL_VERBATIM);
+        if ( dllShell32.IsLoaded() )
+        {
+            s_verShell32 = CallDllGetVersion(dllShell32);
+
+            if ( !s_verShell32 )
+            {
+                // there doesn't seem to be any way to distinguish between 4.00
+                // and 4.70 (starting from 4.71 we have DllGetVersion()) so
+                // just assume it is 4.0
+                s_verShell32 = 400;
+            }
+        }
+        else // failed load the DLL?
+        {
+            s_verShell32 = 0;
+        }
+    }
+
+    return s_verShell32;
+}
+
+#else // !wxUSE_DYNLIB_CLASS
+
+/* static */
+int wxApp::GetComCtl32Version()
+{
+    return 0;
+}
+
+/* static */
+int wxApp::GetShell32Version()
+{
+    return 0;
+}
+
+#endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
+
+#endif // !__WXWINCE__
+
+// ----------------------------------------------------------------------------
 // Yield to incoming messages
+// ----------------------------------------------------------------------------
 
 bool wxApp::Yield(bool onlyIfNeeded)
 {