virtual ~wxFontRefData();
// operations
- bool Alloc(wxFont *font);
+ bool Alloc(const wxFont *font);
void Free();
: m_encoding;
}
- WXHFONT GetHFONT() const { return m_hFont; }
+ WXHFONT GetHFONT(const wxFont *font) const
+ {
+ if ( !m_hFont )
+ wx_const_cast(wxFontRefData *, this)->Alloc(font);
+
+ return (WXHFONT)m_hFont;
+ }
+
+ bool HasHFONT() const
+ {
+ return m_hFont != 0;
+ }
- // ... and setters
+ // ... 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)
{
+ Free();
+
if ( m_nativeFontInfoOk )
{
m_nativeFontInfo.SetPointSize(pointSize);
void SetPixelSize(const wxSize& pixelSize)
{
+ Free();
+
if ( m_nativeFontInfoOk )
{
m_nativeFontInfo.SetPixelSize(pixelSize);
void SetFamily(int family)
{
+ Free();
+
m_family = family;
}
void SetStyle(int style)
{
+ Free();
+
if ( m_nativeFontInfoOk )
m_nativeFontInfo.SetStyle((wxFontStyle)style);
else
void SetWeight(int weight)
{
+ Free();
+
if ( m_nativeFontInfoOk )
m_nativeFontInfo.SetWeight((wxFontWeight)weight);
else
bool SetFaceName(const wxString& faceName)
{
+ Free();
+
if ( m_nativeFontInfoOk )
return m_nativeFontInfo.SetFaceName(faceName);
void SetUnderlined(bool underlined)
{
+ Free();
+
if ( m_nativeFontInfoOk )
m_nativeFontInfo.SetUnderlined(underlined);
else
void SetEncoding(wxFontEncoding encoding)
{
+ Free();
+
if ( m_nativeFontInfoOk )
m_nativeFontInfo.SetEncoding(encoding);
else
m_encoding = encoding;
}
- // native font info tests
+ // native font info
bool HasNativeFontInfo() const { return m_nativeFontInfoOk; }
const wxNativeFontInfo& GetNativeFontInfo() const
{ return m_nativeFontInfo; }
+ void SetNativeFontInfo(const wxNativeFontInfo& nativeFontInfo)
+ { Free(); m_nativeFontInfo = nativeFontInfo; }
+
protected:
// common part of all ctors
void Init(int size,
void Init(const wxNativeFontInfo& info, WXHFONT hFont = 0);
- // font characterstics
+ // font characteristics
int m_pointSize;
wxSize m_pixelSize;
bool m_sizeUsingPixels;
wxString m_faceName;
wxFontEncoding m_encoding;
- // Windows font handle
- WXHFONT m_hFont;
+ // Windows font handle, created on demand in GetHFONT()
+ HFONT m_hFont;
// Native font info
wxNativeFontInfo m_nativeFontInfo;
// use the exact font created in the underlying system
// (for example where we can't guarantee conversion from HFONT
// to LOGFONT back to HFONT)
- m_hFont = hFont;
+ m_hFont = (HFONT)hFont;
m_nativeFontInfoOk = true;
m_nativeFontInfo = info;
Free();
}
-bool wxFontRefData::Alloc(wxFont *font)
+bool wxFontRefData::Alloc(const wxFont *font)
{
if ( !m_nativeFontInfoOk )
{
m_nativeFontInfoOk = true;
}
- HFONT hfont = ::CreateFontIndirect(&m_nativeFontInfo.lf);
- if ( !hfont )
+ m_hFont = ::CreateFontIndirect(&m_nativeFontInfo.lf);
+ if ( !m_hFont )
{
wxLogLastError(wxT("CreateFont"));
return false;
}
- m_hFont = (WXHFONT)hfont;
return true;
}
{
if ( m_hFont )
{
- if ( !::DeleteObject((HFONT) m_hFont) )
+ if ( !::DeleteObject(m_hFont) )
{
wxLogLastError(wxT("DeleteObject(font)"));
}
lf.lfClipPrecision,
lf.lfQuality,
lf.lfPitchAndFamily,
- lf.lfFaceName);
+ (const wxChar*)lf.lfFaceName);
return s;
}
WXHFONT wxFont::GetHFONT() const
{
- return M_FONTDATA ? M_FONTDATA->GetHFONT() : 0;
+ return M_FONTDATA ? M_FONTDATA->GetHFONT(this) : 0;
}
bool wxFont::IsFree() const
{
- return M_FONTDATA && (M_FONTDATA->GetHFONT() == 0);
-}
-
-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);
+ return M_FONTDATA && !M_FONTDATA->HasHFONT();
}
// ----------------------------------------------------------------------------
{
AllocExclusive();
+ M_FONTDATA->Free();
M_FONTDATA->SetPointSize(pointSize);
-
- Recreate();
}
void wxFont::SetPixelSize(const wxSize& pixelSize)
AllocExclusive();
M_FONTDATA->SetPixelSize(pixelSize);
-
- Recreate();
}
void wxFont::SetFamily(int family)
AllocExclusive();
M_FONTDATA->SetFamily(family);
-
- Recreate();
}
void wxFont::SetStyle(int style)
AllocExclusive();
M_FONTDATA->SetStyle(style);
-
- Recreate();
}
void wxFont::SetWeight(int weight)
AllocExclusive();
M_FONTDATA->SetWeight(weight);
-
- Recreate();
}
bool wxFont::SetFaceName(const wxString& faceName)
{
AllocExclusive();
- bool refdataok = M_FONTDATA->SetFaceName(faceName);
-
- Recreate();
+ if ( !M_FONTDATA->SetFaceName(faceName) )
+ return false;
// NB: using win32's GetObject() API on M_FONTDATA->GetHFONT()
// to retrieve a LOGFONT and then compare lf.lfFaceName
// without any validity check.
// Thus we use wxFontBase::SetFaceName to check if facename
// is valid...
- return refdataok && wxFontBase::SetFaceName(faceName);
+ return wxFontBase::SetFaceName(faceName);
}
void wxFont::SetUnderlined(bool underlined)
AllocExclusive();
M_FONTDATA->SetUnderlined(underlined);
-
- Recreate();
}
void wxFont::SetEncoding(wxFontEncoding encoding)
AllocExclusive();
M_FONTDATA->SetEncoding(encoding);
-
- Recreate();
}
void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo& info)
{
AllocExclusive();
- *M_FONTDATA = wxFontRefData(info);
-
- Recreate();
+ M_FONTDATA->SetNativeFontInfo(info);
}
// ----------------------------------------------------------------------------