]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/app.cpp
make GetUsageString() public, this is useful at least for the tests
[wxWidgets.git] / src / msw / app.cpp
index ef5379e82b6e03d39e20da5fa3b8673dd571fb74..c4f599a5075d83baf365722a027805c800b17172 100644 (file)
@@ -52,6 +52,7 @@
 #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"
 
@@ -61,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
@@ -117,13 +118,13 @@ extern void wxSetKeyboardHook(bool doIt);
 WXDLLIMPEXP_CORE       wxChar *wxCanvasClassName;
 WXDLLIMPEXP_CORE       wxChar *wxCanvasClassNameNR;
 #else
-WXDLLIMPEXP_CORE const wxChar *wxCanvasClassName        = wxT("wxWindowClass");
-WXDLLIMPEXP_CORE const wxChar *wxCanvasClassNameNR      = wxT("wxWindowClassNR");
+WXDLLIMPEXP_CORE const wxChar *wxCanvasClassName = NULL;
+WXDLLIMPEXP_CORE const wxChar *wxCanvasClassNameNR = NULL;
 #endif
-WXDLLIMPEXP_CORE const wxChar *wxMDIFrameClassName      = wxT("wxMDIFrameClass");
-WXDLLIMPEXP_CORE const wxChar *wxMDIFrameClassNameNoRedraw = wxT("wxMDIFrameClassNR");
-WXDLLIMPEXP_CORE const wxChar *wxMDIChildFrameClassName = wxT("wxMDIChildFrameClass");
-WXDLLIMPEXP_CORE const wxChar *wxMDIChildFrameClassNameNoRedraw = wxT("wxMDIChildFrameClassNR");
+WXDLLIMPEXP_CORE const wxChar *wxMDIFrameClassName = NULL;
+WXDLLIMPEXP_CORE const wxChar *wxMDIFrameClassNameNoRedraw = NULL;
+WXDLLIMPEXP_CORE const wxChar *wxMDIChildFrameClassName = NULL;
+WXDLLIMPEXP_CORE const wxChar *wxMDIChildFrameClassNameNoRedraw = NULL;
 
 // ----------------------------------------------------------------------------
 // private functions
@@ -354,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
@@ -369,6 +368,54 @@ bool wxApp::Initialize(int& argc, wxChar **argv)
 // RegisterWindowClasses
 // ---------------------------------------------------------------------------
 
+// This function registers the given class name and stores a pointer to a
+// heap-allocated copy of it at the specified location, it must be deleted
+// later.
+static void RegisterAndStoreClassName(const wxString& uniqueClassName,
+                                      const wxChar **className,
+                                      WNDCLASS *lpWndClass)
+{
+    const size_t length = uniqueClassName.length() + 1; // for trailing NUL
+    wxChar *newChars = new wxChar[length];
+    wxStrncpy(newChars, uniqueClassName, length);
+    *className = newChars;
+    lpWndClass->lpszClassName = *className;
+
+    if ( !::RegisterClass(lpWndClass) )
+    {
+        wxLogLastError(wxString::Format(wxT("RegisterClass(%s)"), newChars));
+    }
+}
+
+// This function registers the class defined by the provided WNDCLASS struct
+// contents using a unique name constructed from the specified base name and
+// and a suffix unique to this library instance. It also stores the generated
+// unique names for normal and "no redraw" versions of the class in the
+// provided variables, caller must delete their contents later.
+static void RegisterClassWithUniqueNames(const wxString& baseName,
+                                         const wxChar **className,
+                                         const wxChar **classNameNR,
+                                         WNDCLASS *lpWndClass)
+{
+    // for each class we register one with CS_(V|H)REDRAW style and one
+    // without for windows created with wxNO_FULL_REDRAW_ON_REPAINT flag
+    static const long styleNormal = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
+    static const long styleNoRedraw = CS_DBLCLKS;
+
+    const wxString uniqueSuffix(wxString::Format(wxT("@%p"), className));
+
+    wxString uniqueClassName(baseName + uniqueSuffix);
+    lpWndClass->style = styleNormal;
+    RegisterAndStoreClassName(uniqueClassName, className, lpWndClass);
+
+    // NB: remember that code elsewhere supposes that no redraw class names
+    //     use the same names as normal classes with "NR" suffix so we must put
+    //     "NR" at the end instead of using more natural baseName+"NR"+suffix
+    wxString uniqueClassNameNR(uniqueClassName + wxT("NR"));
+    lpWndClass->style = styleNoRedraw;
+    RegisterAndStoreClassName(uniqueClassNameNR, classNameNR, lpWndClass);
+}
+
 // TODO we should only register classes really used by the app. For this it
 //      would be enough to just delay the class registration until an attempt
 //      to create a window of this class is made.
@@ -377,72 +424,31 @@ bool wxApp::RegisterWindowClasses()
     WNDCLASS wndclass;
     wxZeroMemory(wndclass);
 
-    // for each class we register one with CS_(V|H)REDRAW style and one
-    // without for windows created with wxNO_FULL_REDRAW_ON_REPAINT flag
-    static const long styleNormal = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
-    static const long styleNoRedraw = CS_DBLCLKS;
-
     // the fields which are common to all classes
     wndclass.lpfnWndProc   = (WNDPROC)wxWndProc;
     wndclass.hInstance     = wxhInstance;
     wndclass.hCursor       = ::LoadCursor((HINSTANCE)NULL, IDC_ARROW);
 
-    // register the class for all normal windows
+    // register the class for all normal windows and "no redraw" frames
     wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
-    wndclass.lpszClassName = wxCanvasClassName;
-    wndclass.style         = styleNormal;
-
-    if ( !RegisterClass(&wndclass) )
-    {
-        wxLogLastError(wxT("RegisterClass(frame)"));
-    }
-
-    // "no redraw" frame
-    wndclass.lpszClassName = wxCanvasClassNameNR;
-    wndclass.style         = styleNoRedraw;
-
-    if ( !RegisterClass(&wndclass) )
-    {
-        wxLogLastError(wxT("RegisterClass(no redraw frame)"));
-    }
+    RegisterClassWithUniqueNames(wxT("wxWindowClass"),
+                                 &wxCanvasClassName,
+                                 &wxCanvasClassNameNR,
+                                 &wndclass);
 
-    // Register the MDI frame window class.
+    // Register the MDI frame window class and "no redraw" MDI frame
     wndclass.hbrBackground = (HBRUSH)NULL; // paint MDI frame ourselves
-    wndclass.lpszClassName = wxMDIFrameClassName;
-    wndclass.style         = styleNormal;
+    RegisterClassWithUniqueNames(wxT("wxMDIFrameClass"),
+                                 &wxMDIFrameClassName,
+                                 &wxMDIFrameClassNameNoRedraw,
+                                 &wndclass);
 
-    if ( !RegisterClass(&wndclass) )
-    {
-        wxLogLastError(wxT("RegisterClass(MDI parent)"));
-    }
-
-    // "no redraw" MDI frame
-    wndclass.lpszClassName = wxMDIFrameClassNameNoRedraw;
-    wndclass.style         = styleNoRedraw;
-
-    if ( !RegisterClass(&wndclass) )
-    {
-        wxLogLastError(wxT("RegisterClass(no redraw MDI parent frame)"));
-    }
-
-    // Register the MDI child frame window class.
+    // Register the MDI child frame window class and "no redraw" MDI child frame
     wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
-    wndclass.lpszClassName = wxMDIChildFrameClassName;
-    wndclass.style         = styleNormal;
-
-    if ( !RegisterClass(&wndclass) )
-    {
-        wxLogLastError(wxT("RegisterClass(MDI child)"));
-    }
-
-    // "no redraw" MDI child frame
-    wndclass.lpszClassName = wxMDIChildFrameClassNameNoRedraw;
-    wndclass.style         = styleNoRedraw;
-
-    if ( !RegisterClass(&wndclass) )
-    {
-        wxLogLastError(wxT("RegisterClass(no redraw MDI child)"));
-    }
+    RegisterClassWithUniqueNames(wxT("wxMDIChildFrameClass"),
+                                 &wxMDIChildFrameClassName,
+                                 &wxMDIChildFrameClassNameNoRedraw,
+                                 &wndclass);
 
     return true;
 }
@@ -451,57 +457,48 @@ bool wxApp::RegisterWindowClasses()
 // UnregisterWindowClasses
 // ---------------------------------------------------------------------------
 
-bool wxApp::UnregisterWindowClasses()
+// This function unregisters the class with the given name and frees memory
+// allocated for it by RegisterAndStoreClassName().
+static bool UnregisterAndFreeClassName(const wxChar **ppClassName)
 {
     bool retval = true;
 
-#ifndef __WXMICROWIN__
-    // MDI frame window class.
-    if ( !::UnregisterClass(wxMDIFrameClassName, wxhInstance) )
+    if ( !::UnregisterClass(*ppClassName, wxhInstance) )
     {
-        wxLogLastError(wxT("UnregisterClass(MDI parent)"));
+        wxLogLastError(
+                wxString::Format(wxT("UnregisterClass(%s)"), *ppClassName));
 
         retval = false;
     }
 
-    // "no redraw" MDI frame
-    if ( !::UnregisterClass(wxMDIFrameClassNameNoRedraw, wxhInstance) )
-    {
-        wxLogLastError(wxT("UnregisterClass(no redraw MDI parent frame)"));
+    delete [] *ppClassName;
+    *ppClassName = NULL;
 
-        retval = false;
-    }
+    return retval;
+}
 
-    // MDI child frame window class.
-    if ( !::UnregisterClass(wxMDIChildFrameClassName, wxhInstance) )
-    {
-        wxLogLastError(wxT("UnregisterClass(MDI child)"));
+bool wxApp::UnregisterWindowClasses()
+{
+    bool retval = true;
 
+#ifndef __WXMICROWIN__
+    if ( !UnregisterAndFreeClassName(&wxMDIFrameClassName) )
         retval = false;
-    }
-
-    // "no redraw" MDI child frame
-    if ( !::UnregisterClass(wxMDIChildFrameClassNameNoRedraw, wxhInstance) )
-    {
-        wxLogLastError(wxT("UnregisterClass(no redraw MDI child)"));
 
+    if ( !UnregisterAndFreeClassName(&wxMDIFrameClassNameNoRedraw) )
         retval = false;
-    }
 
-    // canvas class name
-    if ( !::UnregisterClass(wxCanvasClassName, wxhInstance) )
-    {
-        wxLogLastError(wxT("UnregisterClass(canvas)"));
+    if ( !UnregisterAndFreeClassName(&wxMDIChildFrameClassName) )
+        retval = false;
 
+    if ( !UnregisterAndFreeClassName(&wxMDIChildFrameClassNameNoRedraw) )
         retval = false;
-    }
 
-    if ( !::UnregisterClass(wxCanvasClassNameNR, wxhInstance) )
-    {
-        wxLogLastError(wxT("UnregisterClass(no redraw canvas)"));
+    if ( !UnregisterAndFreeClassName(&wxCanvasClassName) )
+        retval = false;
 
+    if ( !UnregisterAndFreeClassName(&wxCanvasClassNameNR) )
         retval = false;
-    }
 #endif // __WXMICROWIN__
 
     return retval;
@@ -509,10 +506,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__)
@@ -527,9 +524,6 @@ void wxApp::CleanUp()
     // unregister the classes now
     UnregisterWindowClasses();
 
-    delete wxWinHandleHash;
-    wxWinHandleHash = NULL;
-
 #ifdef __WXWINCE__
     free( wxCanvasClassName );
     free( wxCanvasClassNameNR );
@@ -570,13 +564,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)"));
+            }
         }
     }
 }
@@ -587,8 +596,24 @@ 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 or when we delete our last window, so make sure we
+    // at least execute our cleanup code before
+
+    // prevent the window from being destroyed when the corresponding wxTLW is
+    // destroyed: this will result in a leak of a HWND, of course, but who
+    // cares when the process is being killed anyhow
+    if ( !wxTopLevelWindows.empty() )
+        wxTopLevelWindows[0]->SetHWND(0);
+
+    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
@@ -606,6 +631,9 @@ void wxApp::OnQueryEndSession(wxCloseEvent& event)
 // system DLL versions
 // ----------------------------------------------------------------------------
 
+// these functions have trivial inline implementations for CE
+#ifndef __WXWINCE__
+
 #if wxUSE_DYNLIB_CLASS
 
 namespace
@@ -741,6 +769,8 @@ int wxApp::GetShell32Version()
 
 #endif // wxUSE_DYNLIB_CLASS/!wxUSE_DYNLIB_CLASS
 
+#endif // !__WXWINCE__
+
 // ----------------------------------------------------------------------------
 // Yield to incoming messages
 // ----------------------------------------------------------------------------