allocate HFONT on demand instead of doing every time a font property is modified

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@44789 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2007-03-13 21:59:55 +00:00
parent adbd3cbc71
commit 8e34db1d94
2 changed files with 51 additions and 50 deletions

View File

@@ -132,9 +132,6 @@ protected:
const wxString& face = wxEmptyString, const wxString& face = wxEmptyString,
wxFontEncoding encoding = wxFONTENCODING_DEFAULT); wxFontEncoding encoding = wxFONTENCODING_DEFAULT);
// this is the same as FreeResource() followed by RealizeResource()
bool Recreate();
virtual void DoSetNativeFontInfo(const wxNativeFontInfo& info); virtual void DoSetNativeFontInfo(const wxNativeFontInfo& info);
// implement wxObject virtuals which are used by AllocExclusive() // implement wxObject virtuals which are used by AllocExclusive()

View File

@@ -145,7 +145,7 @@ public:
virtual ~wxFontRefData(); virtual ~wxFontRefData();
// operations // operations
bool Alloc(wxFont *font); bool Alloc(const wxFont *font);
void Free(); void Free();
@@ -207,11 +207,26 @@ public:
: m_encoding; : m_encoding;
} }
WXHFONT GetHFONT() const { return m_hFont; } WXHFONT GetHFONT(const wxFont *font) const
{
if ( !m_hFont )
wx_const_cast(wxFontRefData *, this)->Alloc(font);
// ... and setters return (WXHFONT)m_hFont;
}
bool HasHFONT() const
{
return m_hFont != 0;
}
// ... and setters: notice that all of them invalidate the currently
// allocated HFONT, if any, so that the next call to GetHFONT() recreates a
// new one
void SetPointSize(int pointSize) void SetPointSize(int pointSize)
{ {
Free();
if ( m_nativeFontInfoOk ) if ( m_nativeFontInfoOk )
{ {
m_nativeFontInfo.SetPointSize(pointSize); m_nativeFontInfo.SetPointSize(pointSize);
@@ -225,6 +240,8 @@ public:
void SetPixelSize(const wxSize& pixelSize) void SetPixelSize(const wxSize& pixelSize)
{ {
Free();
if ( m_nativeFontInfoOk ) if ( m_nativeFontInfoOk )
{ {
m_nativeFontInfo.SetPixelSize(pixelSize); m_nativeFontInfo.SetPixelSize(pixelSize);
@@ -238,11 +255,15 @@ public:
void SetFamily(int family) void SetFamily(int family)
{ {
Free();
m_family = family; m_family = family;
} }
void SetStyle(int style) void SetStyle(int style)
{ {
Free();
if ( m_nativeFontInfoOk ) if ( m_nativeFontInfoOk )
m_nativeFontInfo.SetStyle((wxFontStyle)style); m_nativeFontInfo.SetStyle((wxFontStyle)style);
else else
@@ -251,6 +272,8 @@ public:
void SetWeight(int weight) void SetWeight(int weight)
{ {
Free();
if ( m_nativeFontInfoOk ) if ( m_nativeFontInfoOk )
m_nativeFontInfo.SetWeight((wxFontWeight)weight); m_nativeFontInfo.SetWeight((wxFontWeight)weight);
else else
@@ -259,6 +282,8 @@ public:
bool SetFaceName(const wxString& faceName) bool SetFaceName(const wxString& faceName)
{ {
Free();
if ( m_nativeFontInfoOk ) if ( m_nativeFontInfoOk )
return m_nativeFontInfo.SetFaceName(faceName); return m_nativeFontInfo.SetFaceName(faceName);
@@ -268,6 +293,8 @@ public:
void SetUnderlined(bool underlined) void SetUnderlined(bool underlined)
{ {
Free();
if ( m_nativeFontInfoOk ) if ( m_nativeFontInfoOk )
m_nativeFontInfo.SetUnderlined(underlined); m_nativeFontInfo.SetUnderlined(underlined);
else else
@@ -276,18 +303,23 @@ public:
void SetEncoding(wxFontEncoding encoding) void SetEncoding(wxFontEncoding encoding)
{ {
Free();
if ( m_nativeFontInfoOk ) if ( m_nativeFontInfoOk )
m_nativeFontInfo.SetEncoding(encoding); m_nativeFontInfo.SetEncoding(encoding);
else else
m_encoding = encoding; m_encoding = encoding;
} }
// native font info tests // native font info
bool HasNativeFontInfo() const { return m_nativeFontInfoOk; } bool HasNativeFontInfo() const { return m_nativeFontInfoOk; }
const wxNativeFontInfo& GetNativeFontInfo() const const wxNativeFontInfo& GetNativeFontInfo() const
{ return m_nativeFontInfo; } { return m_nativeFontInfo; }
void SetNativeFontInfo(const wxNativeFontInfo& nativeFontInfo)
{ Free(); m_nativeFontInfo = nativeFontInfo; }
protected: protected:
// common part of all ctors // common part of all ctors
void Init(int size, void Init(int size,
@@ -302,7 +334,7 @@ protected:
void Init(const wxNativeFontInfo& info, WXHFONT hFont = 0); void Init(const wxNativeFontInfo& info, WXHFONT hFont = 0);
// font characterstics // font characteristics
int m_pointSize; int m_pointSize;
wxSize m_pixelSize; wxSize m_pixelSize;
bool m_sizeUsingPixels; bool m_sizeUsingPixels;
@@ -313,8 +345,8 @@ protected:
wxString m_faceName; wxString m_faceName;
wxFontEncoding m_encoding; wxFontEncoding m_encoding;
// Windows font handle // Windows font handle, created on demand in GetHFONT()
WXHFONT m_hFont; HFONT m_hFont;
// Native font info // Native font info
wxNativeFontInfo m_nativeFontInfo; wxNativeFontInfo m_nativeFontInfo;
@@ -363,7 +395,7 @@ void wxFontRefData::Init(const wxNativeFontInfo& info, WXHFONT hFont)
// use the exact font created in the underlying system // use the exact font created in the underlying system
// (for example where we can't guarantee conversion from HFONT // (for example where we can't guarantee conversion from HFONT
// to LOGFONT back to HFONT) // to LOGFONT back to HFONT)
m_hFont = hFont; m_hFont = (HFONT)hFont;
m_nativeFontInfoOk = true; m_nativeFontInfoOk = true;
m_nativeFontInfo = info; m_nativeFontInfo = info;
@@ -377,7 +409,7 @@ wxFontRefData::~wxFontRefData()
Free(); Free();
} }
bool wxFontRefData::Alloc(wxFont *font) bool wxFontRefData::Alloc(const wxFont *font)
{ {
if ( !m_nativeFontInfoOk ) if ( !m_nativeFontInfoOk )
{ {
@@ -385,14 +417,13 @@ bool wxFontRefData::Alloc(wxFont *font)
m_nativeFontInfoOk = true; m_nativeFontInfoOk = true;
} }
HFONT hfont = ::CreateFontIndirect(&m_nativeFontInfo.lf); m_hFont = ::CreateFontIndirect(&m_nativeFontInfo.lf);
if ( !hfont ) if ( !m_hFont )
{ {
wxLogLastError(wxT("CreateFont")); wxLogLastError(wxT("CreateFont"));
return false; return false;
} }
m_hFont = (WXHFONT)hfont;
return true; return true;
} }
@@ -400,7 +431,7 @@ void wxFontRefData::Free()
{ {
if ( m_hFont ) if ( m_hFont )
{ {
if ( !::DeleteObject((HFONT) m_hFont) ) if ( !::DeleteObject(m_hFont) )
{ {
wxLogLastError(wxT("DeleteObject(font)")); wxLogLastError(wxT("DeleteObject(font)"));
} }
@@ -886,23 +917,12 @@ WXHANDLE wxFont::GetResourceHandle() const
WXHFONT wxFont::GetHFONT() const WXHFONT wxFont::GetHFONT() const
{ {
return M_FONTDATA ? M_FONTDATA->GetHFONT() : 0; return M_FONTDATA ? M_FONTDATA->GetHFONT(this) : 0;
} }
bool wxFont::IsFree() const bool wxFont::IsFree() const
{ {
return M_FONTDATA && (M_FONTDATA->GetHFONT() == 0); return M_FONTDATA && !M_FONTDATA->HasHFONT();
}
bool wxFont::Recreate()
{
// this function can be used to recreate the font after its wxFontRefData
// changes and does it unconditionally, i.e. even if already had a valid
// font before
wxCHECK_MSG( M_FONTDATA, false, _T("no font to recreate") );
M_FONTDATA->Free();
return M_FONTDATA->Alloc(this);
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -913,9 +933,8 @@ void wxFont::SetPointSize(int pointSize)
{ {
AllocExclusive(); AllocExclusive();
M_FONTDATA->Free();
M_FONTDATA->SetPointSize(pointSize); M_FONTDATA->SetPointSize(pointSize);
Recreate();
} }
void wxFont::SetPixelSize(const wxSize& pixelSize) void wxFont::SetPixelSize(const wxSize& pixelSize)
@@ -923,8 +942,6 @@ void wxFont::SetPixelSize(const wxSize& pixelSize)
AllocExclusive(); AllocExclusive();
M_FONTDATA->SetPixelSize(pixelSize); M_FONTDATA->SetPixelSize(pixelSize);
Recreate();
} }
void wxFont::SetFamily(int family) void wxFont::SetFamily(int family)
@@ -932,8 +949,6 @@ void wxFont::SetFamily(int family)
AllocExclusive(); AllocExclusive();
M_FONTDATA->SetFamily(family); M_FONTDATA->SetFamily(family);
Recreate();
} }
void wxFont::SetStyle(int style) void wxFont::SetStyle(int style)
@@ -941,8 +956,6 @@ void wxFont::SetStyle(int style)
AllocExclusive(); AllocExclusive();
M_FONTDATA->SetStyle(style); M_FONTDATA->SetStyle(style);
Recreate();
} }
void wxFont::SetWeight(int weight) void wxFont::SetWeight(int weight)
@@ -950,17 +963,14 @@ void wxFont::SetWeight(int weight)
AllocExclusive(); AllocExclusive();
M_FONTDATA->SetWeight(weight); M_FONTDATA->SetWeight(weight);
Recreate();
} }
bool wxFont::SetFaceName(const wxString& faceName) bool wxFont::SetFaceName(const wxString& faceName)
{ {
AllocExclusive(); AllocExclusive();
bool refdataok = M_FONTDATA->SetFaceName(faceName); if ( !M_FONTDATA->SetFaceName(faceName) )
return false;
Recreate();
// NB: using win32's GetObject() API on M_FONTDATA->GetHFONT() // NB: using win32's GetObject() API on M_FONTDATA->GetHFONT()
// to retrieve a LOGFONT and then compare lf.lfFaceName // to retrieve a LOGFONT and then compare lf.lfFaceName
@@ -969,7 +979,7 @@ bool wxFont::SetFaceName(const wxString& faceName)
// without any validity check. // without any validity check.
// Thus we use wxFontBase::SetFaceName to check if facename // Thus we use wxFontBase::SetFaceName to check if facename
// is valid... // is valid...
return refdataok && wxFontBase::SetFaceName(faceName); return wxFontBase::SetFaceName(faceName);
} }
void wxFont::SetUnderlined(bool underlined) void wxFont::SetUnderlined(bool underlined)
@@ -977,8 +987,6 @@ void wxFont::SetUnderlined(bool underlined)
AllocExclusive(); AllocExclusive();
M_FONTDATA->SetUnderlined(underlined); M_FONTDATA->SetUnderlined(underlined);
Recreate();
} }
void wxFont::SetEncoding(wxFontEncoding encoding) void wxFont::SetEncoding(wxFontEncoding encoding)
@@ -986,17 +994,13 @@ void wxFont::SetEncoding(wxFontEncoding encoding)
AllocExclusive(); AllocExclusive();
M_FONTDATA->SetEncoding(encoding); M_FONTDATA->SetEncoding(encoding);
Recreate();
} }
void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo& info) void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo& info)
{ {
AllocExclusive(); AllocExclusive();
*M_FONTDATA = wxFontRefData(info); M_FONTDATA->SetNativeFontInfo(info);
Recreate();
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------