2. added wxFontMapper::Reset() to only do the cast needed when deleting
   the font mapper object once
3. reset the dummy font mapper created during the app initialization in
   wxFontMapperModule and not in init.cpp
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@39624 
c3d73ce0-8a6f-49c7-b76d-
6d57e0e08775
 
 class WXDLLIMPEXP_BASE wxFontMapperBase
 {
 
 class WXDLLIMPEXP_BASE wxFontMapperBase
 {
-    // For IsWxFontMapper()
-    friend class WXDLLIMPEXP_CORE wxFontMapper;
 public:
     // constructtor and such
     // ---------------------
 public:
     // constructtor and such
     // ---------------------
     // set the singleton to 'mapper' instance and return previous one
     static wxFontMapper *Set(wxFontMapper *mapper);
 
     // set the singleton to 'mapper' instance and return previous one
     static wxFontMapper *Set(wxFontMapper *mapper);
 
+    // delete the existing font mapper if any
+    static void Reset();
+
+
     // translates charset strings to encoding
     // --------------------------------------
 
     // translates charset strings to encoding
     // --------------------------------------
 
+    // returns true for the base class and false for a "real" font mapper object
+    // (implementation-only)
+    virtual bool IsDummy() { return true; }
+
 protected:
 #if wxUSE_CONFIG && wxUSE_FILECONFIG
     // get the config object we're using -- either the global config object
 protected:
 #if wxUSE_CONFIG && wxUSE_FILECONFIG
     // get the config object we're using -- either the global config object
     int NonInteractiveCharsetToEncoding(const wxString& charset);
 
 private:
     int NonInteractiveCharsetToEncoding(const wxString& charset);
 
 private:
-    // pseudo-RTTI since we aren't a wxObject.
-    virtual bool IsWxFontMapper();
-
     // the global fontmapper object or NULL
     static wxFontMapper *sm_instance;
 
     // the global fontmapper object or NULL
     static wxFontMapper *sm_instance;
 
     // are additional methods in the subclass.
     static wxFontMapper *Get();
 
     // are additional methods in the subclass.
     static wxFontMapper *Get();
 
+    // pseudo-RTTI since we aren't a wxObject.
+    virtual bool IsDummy() { return false; }
+
 protected:
     // GetAltForEncoding() helper: tests for the existence of the given
     // encoding and saves the result in config if ok - this results in the
 protected:
     // GetAltForEncoding() helper: tests for the existence of the given
     // encoding and saves the result in config if ok - this results in the
     wxWindow *m_windowParent;
 
 private:
     wxWindow *m_windowParent;
 
 private:
-    // pseudo-RTTI since we aren't a wxObject.
-    virtual bool IsWxFontMapper();
-
     DECLARE_NO_COPY_CLASS(wxFontMapper)
 };
 
     DECLARE_NO_COPY_CLASS(wxFontMapper)
 };
 
 
 {
 public:
     wxFontMapperModule() : wxModule() { }
 {
 public:
     wxFontMapperModule() : wxModule() { }
-    virtual bool OnInit() { return true; }
-    virtual void OnExit() { delete (wxFontMapperBase*)wxFontMapperBase::Set(NULL); }
+
+    virtual bool OnInit()
+    {
+        // a dummy wxFontMapperBase object could have been created during the
+        // program startup before wxApp was created, we have to delete it to
+        // allow creating the real font mapper next time it is needed now that
+        // we can create it (when the modules are initialized, wxApp object
+        // already exists)
+        wxFontMapperBase *fm = wxFontMapperBase::Get();
+        if ( fm && fm->IsDummy() )
+            wxFontMapperBase::Reset();
+
+        return true;
+    }
+
+    virtual void OnExit()
+    {
+        wxFontMapperBase::Reset();
+    }
 
     DECLARE_DYNAMIC_CLASS(wxFontMapperModule)
 };
 
     DECLARE_DYNAMIC_CLASS(wxFontMapperModule)
 };
-bool wxFontMapperBase::IsWxFontMapper()
-{   return false; }
-
 /* static */
 wxFontMapperBase *wxFontMapperBase::Get()
 {
 /* static */
 wxFontMapperBase *wxFontMapperBase::Get()
 {
+/* static */
+void wxFontMapperBase::Reset()
+{
+    if ( sm_instance )
+    {
+        // we need a cast as wxFontMapper is not fully declared here and so the
+        // compiler can't know that it derives from wxFontMapperBase (but
+        // run-time behaviour will be correct because the dtor is virtual)
+        delete sm_instance;
+        sm_instance = NULL;
+    }
+}
+
 #if wxUSE_CONFIG && wxUSE_FILECONFIG
 
 // ----------------------------------------------------------------------------
 #if wxUSE_CONFIG && wxUSE_FILECONFIG
 
 // ----------------------------------------------------------------------------
 
-bool wxFontMapper::IsWxFontMapper()
-{   return true; }
-
 /* static */
 wxFontMapper *wxFontMapper::Get()
 {
     wxFontMapperBase *fontmapper = wxFontMapperBase::Get();
 /* static */
 wxFontMapper *wxFontMapper::Get()
 {
     wxFontMapperBase *fontmapper = wxFontMapperBase::Get();
-    wxASSERT_MSG(fontmapper->IsWxFontMapper(), wxT("GUI code requested a wxFontMapper but we only have a wxFontMapperBase."));
+    wxASSERT_MSG( !fontmapper->IsDummy(),
+                 wxT("GUI code requested a wxFontMapper but we only have a wxFontMapperBase.") );
+
     // Now return it anyway because there's a chance the GUI code might just
     // Now return it anyway because there's a chance the GUI code might just
-    // only want to call wxFontMapperBase functions.
-    return (wxFontMapper*)fontmapper;
+    // only want to call wxFontMapperBase functions and it's better than
+    // crashing by returning NULL
+    return (wxFontMapper *)fontmapper;
 
 #include "wx/ptr_scpd.h"
 #include "wx/module.h"
 #include "wx/except.h"
 #include "wx/ptr_scpd.h"
 #include "wx/module.h"
 #include "wx/except.h"
-#if wxUSE_FONTMAP
-#include "wx/fontmap.h"
-#endif
 
 #if defined(__WXMSW__) && defined(__WXDEBUG__)
     #include "wx/msw/msvcrt.h"
 
 #if defined(__WXMSW__) && defined(__WXDEBUG__)
     #include "wx/msw/msvcrt.h"
 {
     ConvertArgsToUnicode(argc, argv);
 
 {
     ConvertArgsToUnicode(argc, argv);
 
-#if wxUSE_FONTMAP
-    // If we created a font mapper during the above call,
-    // it will only be the base class, so delete it to allow
-    // app traits to create mapper.
-    delete (wxFontMapperBase*) wxFontMapperBase::Set(NULL);
-#endif
-    
     return wxEntry(argc, gs_initData.argv);
 }
 
     return wxEntry(argc, gs_initData.argv);
 }