]> git.saurik.com Git - wxWidgets.git/commitdiff
1. renamed wxFontMapper::IsWxFontMapper() to IsDummy() (with reverse semantics)
authorVadim Zeitlin <vadim@wxwidgets.org>
Wed, 7 Jun 2006 17:49:27 +0000 (17:49 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Wed, 7 Jun 2006 17:49:27 +0000 (17:49 +0000)
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

include/wx/fontmap.h
src/common/fmapbase.cpp
src/common/fontmap.cpp
src/common/init.cpp

index 881adb6ee550133996ddd51a2b1a69366935c7e7..d057c16bd6d4d8b252cff0f61906cb14e9cf574f 100644 (file)
@@ -49,8 +49,6 @@ class WXDLLIMPEXP_CORE wxFontMapper;
 
 class WXDLLIMPEXP_BASE wxFontMapperBase
 {
-    // For IsWxFontMapper()
-    friend class WXDLLIMPEXP_CORE wxFontMapper;
 public:
     // constructtor and such
     // ---------------------
@@ -70,6 +68,10 @@ public:
     // 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
     // --------------------------------------
 
@@ -124,6 +126,10 @@ public:
 #endif // wxUSE_CONFIG
 
 
+    // 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
@@ -159,9 +165,6 @@ protected:
     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;
 
@@ -239,6 +242,9 @@ public:
     // 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
@@ -258,9 +264,6 @@ protected:
     wxWindow *m_windowParent;
 
 private:
-    // pseudo-RTTI since we aren't a wxObject.
-    virtual bool IsWxFontMapper();
-
     DECLARE_NO_COPY_CLASS(wxFontMapper)
 };
 
index f148a5cd210ab6884654515f7b3431a24eaa98dc..ebc2bd3666f9742394df386c0ede9291bb50a60d 100644 (file)
@@ -233,8 +233,25 @@ class wxFontMapperModule : public 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)
 };
@@ -267,9 +284,6 @@ wxFontMapperBase::~wxFontMapperBase()
 #endif // wxUSE_CONFIG
 }
 
-bool wxFontMapperBase::IsWxFontMapper()
-{   return false; }
-
 /* static */
 wxFontMapperBase *wxFontMapperBase::Get()
 {
@@ -303,6 +317,19 @@ wxFontMapper *wxFontMapperBase::Set(wxFontMapper *mapper)
     return old;
 }
 
+/* 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
 
 // ----------------------------------------------------------------------------
index b0ba68445bcb68d854b6c121e953ba9e94051e84..cedea6789d65533757bbc51bc4ab2972c25513bb 100644 (file)
@@ -160,17 +160,17 @@ wxFontMapper::~wxFontMapper()
 {
 }
 
-bool wxFontMapper::IsWxFontMapper()
-{   return true; }
-
 /* 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
-    // 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;
 }
 
 wxFontEncoding
index 86741978f9c881db6934a61acf383785a20c098d..697aea665658ad9f2097b6a9791beb56e6185873 100644 (file)
@@ -36,9 +36,6 @@
 #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"
@@ -452,13 +449,6 @@ int wxEntry(int& argc, char **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);
 }