From 59b7da02ff62a33862accc13158870f2a9a23630 Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Wed, 29 Sep 2010 13:46:09 +0000
Subject: [PATCH] Return wxFONTFAMILY_DEFAULT, not UNKNOWN, from
 wxFont::GetFamily().

Although returning wxFONTFAMILY_UNKNOWN when the font family is not recognized
makes more sense, it breaks a lot of existing code including all the
font-related code generated by DialogBlocks, so prefer to return
wxFONTFAMILY_DEFAULT instead -- which can't be confused for a valid font
family neither but can be passed to wxFont ctor or SetFamily() without
problems.

To ensure that this behaviour is correctly implemented by all ports, rename
the existing wxFont::GetFamily() to DoGetFamily() and call the new method from
wxFontBase::GetFamily() which adjusts the return value if needed.

Closes #12330.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65670 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 include/wx/cocoa/font.h  |  3 ++-
 include/wx/dfb/font.h    |  3 ++-
 include/wx/font.h        |  6 +++++-
 include/wx/gtk/font.h    |  3 ++-
 include/wx/gtk1/font.h   |  2 +-
 include/wx/mgl/font.h    |  3 ++-
 include/wx/motif/font.h  |  2 +-
 include/wx/msw/font.h    |  2 +-
 include/wx/os2/font.h    |  2 +-
 include/wx/osx/font.h    |  2 +-
 include/wx/palmos/font.h |  2 +-
 include/wx/x11/font.h    |  2 +-
 interface/wx/font.h      | 19 +++++++++----------
 src/cocoa/font.mm        |  3 +--
 src/common/fontcmn.cpp   | 14 ++++++++++++++
 src/dfb/font.cpp         |  4 +---
 src/gtk/font.cpp         |  4 +---
 src/gtk1/font.cpp        |  4 +---
 src/mgl/font.cpp         |  4 +---
 src/motif/font.cpp       |  4 +---
 src/msw/font.cpp         |  4 +---
 src/os2/font.cpp         |  6 ++----
 src/osx/carbon/font.cpp  |  4 +---
 src/palmos/font.cpp      |  2 +-
 src/x11/font.cpp         |  4 +---
 25 files changed, 55 insertions(+), 53 deletions(-)

diff --git a/include/wx/cocoa/font.h b/include/wx/cocoa/font.h
index 304a72db8d..b671f95bcb 100644
--- a/include/wx/cocoa/font.h
+++ b/include/wx/cocoa/font.h
@@ -103,7 +103,6 @@ public:
 
     // implement base class pure virtuals
     virtual int GetPointSize() const;
-    virtual wxFontFamily GetFamily() const;
     virtual wxFontStyle GetStyle() const;
     virtual wxFontWeight GetWeight() const;
     virtual bool GetUnderlined() const;
@@ -149,6 +148,8 @@ protected:
     virtual wxGDIRefData *CreateGDIRefData() const;
     virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const;
 
+    virtual wxFontFamily DoGetFamily() const;
+
 private:
     DECLARE_DYNAMIC_CLASS(wxFont)
 };
diff --git a/include/wx/dfb/font.h b/include/wx/dfb/font.h
index f3c6c485b1..ad0e586e1e 100644
--- a/include/wx/dfb/font.h
+++ b/include/wx/dfb/font.h
@@ -71,7 +71,6 @@ public:
 
     // implement base class pure virtuals
     virtual int GetPointSize() const;
-    virtual wxFontFamily GetFamily() const;
     virtual wxFontStyle GetStyle() const;
     virtual wxFontWeight GetWeight() const;
     virtual wxString GetFaceName() const;
@@ -97,6 +96,8 @@ protected:
     virtual wxGDIRefData *CreateGDIRefData() const;
     virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const;
 
+    virtual wxFontFamily DoGetFamily() const;
+
 private:
     DECLARE_DYNAMIC_CLASS(wxFont)
 };
diff --git a/include/wx/font.h b/include/wx/font.h
index 96efd04965..6d7b46a31a 100644
--- a/include/wx/font.h
+++ b/include/wx/font.h
@@ -214,7 +214,7 @@ public:
     virtual int GetPointSize() const = 0;
     virtual wxSize GetPixelSize() const;
     virtual bool IsUsingSizeInPixels() const;
-    virtual wxFontFamily GetFamily() const = 0;
+    wxFontFamily GetFamily() const;
     virtual wxFontStyle GetStyle() const = 0;
     virtual wxFontWeight GetWeight() const = 0;
     virtual bool GetUnderlined() const = 0;
@@ -264,6 +264,10 @@ protected:
     // the function called by both overloads of SetNativeFontInfo()
     virtual void DoSetNativeFontInfo(const wxNativeFontInfo& info);
 
+    // The function called by public GetFamily(): it can return
+    // wxFONTFAMILY_UNKNOWN unlike the public method (see comment there).
+    virtual wxFontFamily DoGetFamily() const = 0;
+
 private:
     // the currently default encoding: by default, it's the default system
     // encoding, but may be changed by the application using
diff --git a/include/wx/gtk/font.h b/include/wx/gtk/font.h
index adbecdadee..6bc37e9eff 100644
--- a/include/wx/gtk/font.h
+++ b/include/wx/gtk/font.h
@@ -78,7 +78,6 @@ public:
 
     // implement base class pure virtuals
     virtual int GetPointSize() const;
-    virtual wxFontFamily GetFamily() const;
     virtual wxFontStyle GetStyle() const;
     virtual wxFontWeight GetWeight() const;
     virtual wxString GetFaceName() const;
@@ -111,6 +110,8 @@ protected:
     virtual wxGDIRefData* CreateGDIRefData() const;
     virtual wxGDIRefData* CloneGDIRefData(const wxGDIRefData* data) const;
 
+    virtual wxFontFamily DoGetFamily() const;
+
 private:
     DECLARE_DYNAMIC_CLASS(wxFont)
 };
diff --git a/include/wx/gtk1/font.h b/include/wx/gtk1/font.h
index c619aad35e..b5e7e4ea20 100644
--- a/include/wx/gtk1/font.h
+++ b/include/wx/gtk1/font.h
@@ -92,7 +92,6 @@ public:
 
     // implement base class pure virtuals
     virtual int GetPointSize() const;
-    virtual wxFontFamily GetFamily() const;
     virtual wxFontStyle GetStyle() const;
     virtual wxFontWeight GetWeight() const;
     virtual wxString GetFaceName() const;
@@ -121,6 +120,7 @@ protected:
     virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const;
 
     virtual void DoSetNativeFontInfo( const wxNativeFontInfo& info );
+    virtual wxFontFamily DoGetFamily() const;
 
 private:
     DECLARE_DYNAMIC_CLASS(wxFont)
diff --git a/include/wx/mgl/font.h b/include/wx/mgl/font.h
index 6ef7f521df..bc2bff6b8a 100644
--- a/include/wx/mgl/font.h
+++ b/include/wx/mgl/font.h
@@ -87,7 +87,6 @@ public:
 
     // implement base class pure virtuals
     virtual int GetPointSize() const;
-    virtual wxFontFamily GetFamily() const;
     virtual wxFontStyle GetStyle() const;
     virtual wxFontWeight GetWeight() const;
     virtual wxString GetFaceName() const;
@@ -113,6 +112,8 @@ protected:
     virtual wxGDIRefData *CreateGDIRefData() const;
     virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const;
 
+    virtual wxFontFamily DoGetFamily() const;
+
 private:
     DECLARE_DYNAMIC_CLASS(wxFont)
 };
diff --git a/include/wx/motif/font.h b/include/wx/motif/font.h
index 53f0c3a4d5..e00eb848d3 100644
--- a/include/wx/motif/font.h
+++ b/include/wx/motif/font.h
@@ -83,7 +83,6 @@ public:
 
     // implement base class pure virtuals
     virtual int GetPointSize() const;
-    virtual wxFontFamily GetFamily() const;
     virtual wxFontStyle GetStyle() const;
     virtual wxFontWeight GetWeight() const;
     virtual bool GetUnderlined() const;
@@ -140,6 +139,7 @@ protected:
     virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const;
 
     virtual void DoSetNativeFontInfo( const wxNativeFontInfo& info );
+    virtual wxFontFamily DoGetFamily() const;
 
     void Unshare();
 
diff --git a/include/wx/msw/font.h b/include/wx/msw/font.h
index 05edfdf958..3f49e65e78 100644
--- a/include/wx/msw/font.h
+++ b/include/wx/msw/font.h
@@ -114,7 +114,6 @@ public:
     virtual int GetPointSize() const;
     virtual wxSize GetPixelSize() const;
     virtual bool IsUsingSizeInPixels() const;
-    virtual wxFontFamily GetFamily() const;
     virtual wxFontStyle GetStyle() const;
     virtual wxFontWeight GetWeight() const;
     virtual bool GetUnderlined() const;
@@ -159,6 +158,7 @@ protected:
                   wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
 
     virtual void DoSetNativeFontInfo(const wxNativeFontInfo& info);
+    virtual wxFontFamily DoGetFamily() const;
 
     // implement wxObject virtuals which are used by AllocExclusive()
     virtual wxGDIRefData *CreateGDIRefData() const;
diff --git a/include/wx/os2/font.h b/include/wx/os2/font.h
index 3c14bf884b..7060847d66 100644
--- a/include/wx/os2/font.h
+++ b/include/wx/os2/font.h
@@ -93,7 +93,6 @@ public:
     // Implement base class pure virtuals
     //
     virtual int               GetPointSize(void) const;
-    virtual wxFontFamily GetFamily() const;
     virtual wxFontStyle GetStyle() const;
     virtual wxFontWeight GetWeight() const;
     virtual bool              GetUnderlined(void) const;
@@ -131,6 +130,7 @@ public:
 
 protected:
     virtual void DoSetNativeFontInfo(const wxNativeFontInfo& rInfo);
+    virtual wxFontFamily DoGetFamily() const;
 
     // implement wxObject virtuals which are used by AllocExclusive()
     virtual wxGDIRefData *CreateGDIRefData() const;
diff --git a/include/wx/osx/font.h b/include/wx/osx/font.h
index 4d6f07fbc1..3450dbc349 100644
--- a/include/wx/osx/font.h
+++ b/include/wx/osx/font.h
@@ -101,7 +101,6 @@ public:
     // implement base class pure virtuals
     virtual int GetPointSize() const;
     virtual wxSize GetPixelSize() const;
-    virtual wxFontFamily GetFamily() const;
     virtual wxFontStyle GetStyle() const;
     virtual wxFontWeight GetWeight() const;
     virtual bool GetUnderlined() const;
@@ -168,6 +167,7 @@ public:
 
 protected:
     virtual void DoSetNativeFontInfo(const wxNativeFontInfo& info);
+    virtual wxFontFamily DoGetFamily() const;
 
     virtual wxGDIRefData *CreateGDIRefData() const;
     virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const;
diff --git a/include/wx/palmos/font.h b/include/wx/palmos/font.h
index 736e7bd812..407cef5d08 100644
--- a/include/wx/palmos/font.h
+++ b/include/wx/palmos/font.h
@@ -99,7 +99,6 @@ public:
     virtual int GetPointSize() const;
     virtual wxSize GetPixelSize() const;
     virtual bool IsUsingSizeInPixels() const;
-    virtual wxFontFamily GetFamily() const;
     virtual wxFontStyle GetStyle() const;
     virtual wxFontWeight GetWeight() const;
     virtual bool GetUnderlined() const;
@@ -141,6 +140,7 @@ protected:
                   wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
 
     virtual void DoSetNativeFontInfo(const wxNativeFontInfo& info);
+    virtual wxFontFamily DoGetFamily() const;
 
     // implement wxObject virtuals which are used by AllocExclusive()
     virtual wxGDIRefData *CreateGDIRefData() const;
diff --git a/include/wx/x11/font.h b/include/wx/x11/font.h
index a7197b878b..b2c8718392 100644
--- a/include/wx/x11/font.h
+++ b/include/wx/x11/font.h
@@ -80,7 +80,6 @@ public:
 
     // implement base class pure virtuals
     virtual int GetPointSize() const;
-    virtual wxFontFamily GetFamily() const;
     virtual wxFontStyle GetStyle() const;
     virtual wxFontWeight GetWeight() const;
     virtual bool GetUnderlined() const;
@@ -129,6 +128,7 @@ protected:
     virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const;
 
     virtual void DoSetNativeFontInfo( const wxNativeFontInfo& info );
+    virtual wxFontFamily DoGetFamily() const;
 
     void Unshare();
 
diff --git a/interface/wx/font.h b/interface/wx/font.h
index f9e22b306b..ffa5505743 100644
--- a/interface/wx/font.h
+++ b/interface/wx/font.h
@@ -36,11 +36,9 @@ enum wxFontFamily
     /// See also wxFont::IsFixedWidth() for an easy way to test for monospace property.
     wxFONTFAMILY_TELETYPE = wxTELETYPE,
 
-    /// Returned by wxFont::GetFamily() when the face name of the font cannot
-    /// be classified into one of the previous wxFontFamily values.
-    wxFONTFAMILY_UNKNOWN = wxFONTFAMILY_MAX,
-
-    wxFONTFAMILY_MAX
+    /// Invalid font family value, returned by wxFont::GetFamily() when the
+    /// font is invalid for example.
+    wxFONTFAMILY_UNKNOWN
 };
 
 /**
@@ -422,16 +420,17 @@ public:
     virtual wxString GetFaceName() const;
 
     /**
-        Gets the font family.
+        Gets the font family if possible.
+
         As described in ::wxFontFamily docs the returned value acts as a rough,
         basic classification of the main font properties (look, spacing).
 
         If the current font face name is not recognized by wxFont or by the
-        underlying system, @c wxFONTFAMILY_UNKNOWN is returned.
+        underlying system, @c wxFONTFAMILY_DEFAULT is returned.
 
-        Note that currently this function is rather unreliable (@c wxFONTFAMILY_UNKNOWN
-        is returned in too many cases) and not particularly useful.
-        Font families mostly make sense only for font creation; see SetFamily().
+        Note that currently this function is not very precise and so not
+        particularly useful. Font families mostly make sense only for font
+        creation, see SetFamily().
 
         @see SetFamily()
     */
diff --git a/src/cocoa/font.mm b/src/cocoa/font.mm
index 9b67dbb3aa..2e2be8b5af 100644
--- a/src/cocoa/font.mm
+++ b/src/cocoa/font.mm
@@ -370,9 +370,8 @@ wxFontStyle wxFont::GetStyle() const
     return M_FONTDATA->m_info.style;
 }
 
-wxFontFamily wxFont::GetFamily() const
+wxFontFamily wxFont::DoGetFamily() const
 {
-    wxCHECK_MSG( Ok(), 0, wxT("invalid font") );
     return M_FONTDATA->m_info.family;
 }
 
diff --git a/src/common/fontcmn.cpp b/src/common/fontcmn.cpp
index a59e671c5b..27154e9953 100644
--- a/src/common/fontcmn.cpp
+++ b/src/common/fontcmn.cpp
@@ -367,6 +367,20 @@ bool wxFontBase::operator==(const wxFont& font) const
            );
 }
 
+wxFontFamily wxFontBase::GetFamily() const
+{
+    wxCHECK_MSG( IsOk(), wxFONTFAMILY_UNKNOWN, wxS("invalid font") );
+
+    // Don't return wxFONTFAMILY_UNKNOWN from here because it prevents the code
+    // like wxFont(size, wxNORMAL_FONT->GetFamily(), ...) from working (see
+    // #12330). This is really just a hack but it allows to keep compatibility
+    // and doesn't really have any bad drawbacks so do this until someone comes
+    // up with a better idea.
+    const wxFontFamily family = DoGetFamily();
+
+    return family == wxFONTFAMILY_UNKNOWN ? wxFONTFAMILY_DEFAULT : family;
+}
+
 wxString wxFontBase::GetFamilyString() const
 {
     wxCHECK_MSG( IsOk(), "wxFONTFAMILY_DEFAULT", "invalid font" );
diff --git a/src/dfb/font.cpp b/src/dfb/font.cpp
index 05a83676f0..7f780bdba7 100644
--- a/src/dfb/font.cpp
+++ b/src/dfb/font.cpp
@@ -98,10 +98,8 @@ wxString wxFont::GetFaceName() const
     return M_FONTDATA->GetFaceName();
 }
 
-wxFontFamily wxFont::GetFamily() const
+wxFontFamily wxFont::DoGetFamily() const
 {
-    wxCHECK_MSG( Ok(), wxFONTFAMILY_MAX, wxT("invalid font") );
-
     return M_FONTDATA->GetFamily();
 }
 
diff --git a/src/gtk/font.cpp b/src/gtk/font.cpp
index d011c26b35..adeb1354f7 100644
--- a/src/gtk/font.cpp
+++ b/src/gtk/font.cpp
@@ -333,10 +333,8 @@ wxString wxFont::GetFaceName() const
     return M_FONTDATA->m_nativeFontInfo.GetFaceName();
 }
 
-wxFontFamily wxFont::GetFamily() const
+wxFontFamily wxFont::DoGetFamily() const
 {
-    wxCHECK_MSG( IsOk(), wxFONTFAMILY_MAX, wxT("invalid font") );
-
     return M_FONTDATA->m_nativeFontInfo.GetFamily();
 }
 
diff --git a/src/gtk1/font.cpp b/src/gtk1/font.cpp
index 9970976eff..2d97fcd52f 100644
--- a/src/gtk1/font.cpp
+++ b/src/gtk1/font.cpp
@@ -534,10 +534,8 @@ wxString wxFont::GetFaceName() const
     return M_FONTDATA->m_faceName;
 }
 
-wxFontFamily wxFont::GetFamily() const
+wxFontFamily wxFont::DoGetFamily() const
 {
-    wxCHECK_MSG( Ok(), wxFONTFAMILY_MAX, wxT("invalid font") );
-
     return M_FONTDATA->m_family;
 }
 
diff --git a/src/mgl/font.cpp b/src/mgl/font.cpp
index e322af0f80..bb68eba936 100644
--- a/src/mgl/font.cpp
+++ b/src/mgl/font.cpp
@@ -95,10 +95,8 @@ wxString wxFont::GetFaceName() const
     return M_FONTDATA->GetFaceName();
 }
 
-wxFontFamily wxFont::GetFamily() const
+wxFontFamily wxFont::DoGetFamily() const
 {
-    wxCHECK_MSG( Ok(), wxFONTFAMILY_MAX, wxT("invalid font") );
-
     return M_FONTDATA->GetFamily();
 }
 
diff --git a/src/motif/font.cpp b/src/motif/font.cpp
index 2969f829be..3d5488cc42 100644
--- a/src/motif/font.cpp
+++ b/src/motif/font.cpp
@@ -455,10 +455,8 @@ wxString wxFont::GetFaceName() const
     return M_FONTDATA->m_faceName ;
 }
 
-wxFontFamily wxFont::GetFamily() const
+wxFontFamily wxFont::DoGetFamily() const
 {
-    wxCHECK_MSG( Ok(), wxFONTFAMILY_MAX, wxT("invalid font") );
-
     return M_FONTDATA->m_family;
 }
 
diff --git a/src/msw/font.cpp b/src/msw/font.cpp
index b1e347d674..0acbf1aae6 100644
--- a/src/msw/font.cpp
+++ b/src/msw/font.cpp
@@ -1016,10 +1016,8 @@ bool wxFont::IsUsingSizeInPixels() const
     return M_FONTDATA->IsUsingSizeInPixels();
 }
 
-wxFontFamily wxFont::GetFamily() const
+wxFontFamily wxFont::DoGetFamily() const
 {
-    wxCHECK_MSG( IsOk(), wxFONTFAMILY_MAX, wxT("invalid font") );
-
     return M_FONTDATA->GetFamily();
 }
 
diff --git a/src/os2/font.cpp b/src/os2/font.cpp
index d4be5633cb..eae1b4f880 100644
--- a/src/os2/font.cpp
+++ b/src/os2/font.cpp
@@ -1082,12 +1082,10 @@ int wxFont::GetPointSize() const
     return M_FONTDATA->GetPointSize();
 } // end of wxFont::GetPointSize
 
-wxFontFamily wxFont::GetFamily() const
+wxFontFamily wxFont::DoGetFamily() const
 {
-    wxCHECK_MSG( Ok(), wxFONTFAMILY_MAX, wxT("invalid font") );
-
     return M_FONTDATA->GetFamily();
-} // end of wxFont::GetFamily
+} // end of wxFont::DoGetFamily
 
 wxFontStyle wxFont::GetStyle() const
 {
diff --git a/src/osx/carbon/font.cpp b/src/osx/carbon/font.cpp
index 2d0ae842e2..4b477bb8cc 100644
--- a/src/osx/carbon/font.cpp
+++ b/src/osx/carbon/font.cpp
@@ -732,10 +732,8 @@ wxSize wxFont::GetPixelSize() const
 #endif
 }
 
-wxFontFamily wxFont::GetFamily() const
+wxFontFamily wxFont::DoGetFamily() const
 {
-    wxCHECK_MSG( M_FONTDATA != NULL , wxFONTFAMILY_MAX, wxT("invalid font") );
-
     return M_FONTDATA->GetFamily();
 }
 
diff --git a/src/palmos/font.cpp b/src/palmos/font.cpp
index 56bc52c737..cf39e4dada 100644
--- a/src/palmos/font.cpp
+++ b/src/palmos/font.cpp
@@ -488,7 +488,7 @@ bool wxFont::IsUsingSizeInPixels() const
     return false;
 }
 
-wxFontFamily wxFont::GetFamily() const
+wxFontFamily wxFont::DoGetFamily() const
 {
     return wxFONTFAMILY_ROMAN;
 }
diff --git a/src/x11/font.cpp b/src/x11/font.cpp
index 32aa7d6a56..6423e578ea 100644
--- a/src/x11/font.cpp
+++ b/src/x11/font.cpp
@@ -725,10 +725,8 @@ wxString wxFont::GetFaceName() const
     return M_FONTDATA->m_faceName;
 }
 
-wxFontFamily wxFont::GetFamily() const
+wxFontFamily wxFont::DoGetFamily() const
 {
-    wxCHECK_MSG( Ok(), wxFONTFAMILY_MAX, wxT("invalid font") );
-
     return M_FONTDATA->m_family;
 }
 
-- 
2.47.2