simplify a bit the convoluted implementation of wxFontRefData::Alloc;

add some comment, regarding the auto-allocation feature of GetHFONT();
use const_cast<>() instead of wxConstCast();
protect wxFont::IsFixedWidth with a proper wxCHECK_MSG.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@59615 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Francesco Montorsi
2009-03-18 21:51:22 +00:00
parent b7d70f7626
commit d014871de8

View File

@@ -408,6 +408,7 @@ void wxFontRefData::Init(const wxNativeFontInfo& info, WXHFONT hFont)
m_nativeFontInfoOk = true; m_nativeFontInfoOk = true;
m_nativeFontInfo = info; m_nativeFontInfo = info;
// This is the best we can do since we don't have the // This is the best we can do since we don't have the
// correct information at this point. // correct information at this point.
m_family = wxFONTFAMILY_SWISS; m_family = wxFONTFAMILY_SWISS;
@@ -422,7 +423,11 @@ bool wxFontRefData::Alloc(const wxFont *font)
{ {
if ( !m_nativeFontInfoOk ) if ( !m_nativeFontInfoOk )
{ {
wxFillLogFont(&m_nativeFontInfo.lf, font); // NOTE: we use wxNativeInfo::InitFromFont to avoid code duplication:
// it results in using our m_* variables (except for m_hFont and
// for m_nativeFontInfo obviously) for the initialization
// of the wxNativeInfo::lf member.
m_nativeFontInfo.InitFromFont(*font);
m_nativeFontInfoOk = true; m_nativeFontInfoOk = true;
} }
@@ -848,6 +853,13 @@ wxString wxNativeFontInfo::ToString() const
// wxFont // wxFont
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
wxFont::wxFont(const wxString& fontdesc)
{
wxNativeFontInfo info;
if ( info.FromString(fontdesc) )
(void)Create(info);
}
bool wxFont::Create(const wxNativeFontInfo& info, WXHFONT hFont) bool wxFont::Create(const wxNativeFontInfo& info, WXHFONT hFont)
{ {
UnRef(); UnRef();
@@ -857,13 +869,6 @@ bool wxFont::Create(const wxNativeFontInfo& info, WXHFONT hFont)
return RealizeResource(); return RealizeResource();
} }
wxFont::wxFont(const wxString& fontdesc)
{
wxNativeFontInfo info;
if ( info.FromString(fontdesc) )
(void)Create(info);
}
bool wxFont::DoCreate(int pointSize, bool wxFont::DoCreate(int pointSize,
const wxSize& pixelSize, const wxSize& pixelSize,
bool sizeUsingPixels, bool sizeUsingPixels,
@@ -910,16 +915,15 @@ wxGDIRefData *wxFont::CloneGDIRefData(const wxGDIRefData *data) const
bool wxFont::RealizeResource() bool wxFont::RealizeResource()
{ {
// don't do anything if we already have a valid font // NOTE: the GetHFONT() call automatically triggers a reallocation of
if ( GetHFONT() ) // the HFONT if necessary (will do nothing if we already have the resource);
return true; // it returns NULL only if there is a failure in wxFontRefData::Alloc()...
return GetHFONT() != NULL;
return M_FONTDATA->Alloc(this);
} }
bool wxFont::FreeResource(bool WXUNUSED(force)) bool wxFont::FreeResource(bool WXUNUSED(force))
{ {
if ( !GetHFONT() ) if ( !M_FONTDATA )
return false; return false;
M_FONTDATA->Free(); M_FONTDATA->Free();
@@ -934,6 +938,8 @@ WXHANDLE wxFont::GetResourceHandle() const
WXHFONT wxFont::GetHFONT() const WXHFONT wxFont::GetHFONT() const
{ {
// NOTE: wxFontRefData::GetHFONT() will automatically call
// wxFontRefData::Alloc() if necessary
return M_FONTDATA ? M_FONTDATA->GetHFONT(this) : 0; return M_FONTDATA ? M_FONTDATA->GetHFONT(this) : 0;
} }
@@ -1026,93 +1032,95 @@ void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo& info)
int wxFont::GetPointSize() const int wxFont::GetPointSize() const
{ {
wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); wxCHECK_MSG( IsOk(), 0, wxT("invalid font") );
return M_FONTDATA->GetPointSize(); return M_FONTDATA->GetPointSize();
} }
wxSize wxFont::GetPixelSize() const wxSize wxFont::GetPixelSize() const
{ {
wxCHECK_MSG( Ok(), wxDefaultSize, wxT("invalid font") ); wxCHECK_MSG( IsOk(), wxDefaultSize, wxT("invalid font") );
return M_FONTDATA->GetPixelSize(); return M_FONTDATA->GetPixelSize();
} }
bool wxFont::IsUsingSizeInPixels() const bool wxFont::IsUsingSizeInPixels() const
{ {
wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); wxCHECK_MSG( IsOk(), 0, wxT("invalid font") );
return M_FONTDATA->IsUsingSizeInPixels(); return M_FONTDATA->IsUsingSizeInPixels();
} }
wxFontFamily wxFont::GetFamily() const wxFontFamily wxFont::GetFamily() const
{ {
wxCHECK_MSG( Ok(), wxFONTFAMILY_MAX, wxT("invalid font") ); wxCHECK_MSG( IsOk(), wxFONTFAMILY_MAX, wxT("invalid font") );
return M_FONTDATA->GetFamily(); return M_FONTDATA->GetFamily();
} }
wxFontStyle wxFont::GetStyle() const wxFontStyle wxFont::GetStyle() const
{ {
wxCHECK_MSG( Ok(), wxFONTSTYLE_MAX, wxT("invalid font") ); wxCHECK_MSG( IsOk(), wxFONTSTYLE_MAX, wxT("invalid font") );
return M_FONTDATA->GetStyle(); return M_FONTDATA->GetStyle();
} }
wxFontWeight wxFont::GetWeight() const wxFontWeight wxFont::GetWeight() const
{ {
wxCHECK_MSG( Ok(), wxFONTWEIGHT_MAX, wxT("invalid font") ); wxCHECK_MSG( IsOk(), wxFONTWEIGHT_MAX, wxT("invalid font") );
return M_FONTDATA->GetWeight(); return M_FONTDATA->GetWeight();
} }
bool wxFont::GetUnderlined() const bool wxFont::GetUnderlined() const
{ {
wxCHECK_MSG( Ok(), false, wxT("invalid font") ); wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
return M_FONTDATA->GetUnderlined(); return M_FONTDATA->GetUnderlined();
} }
wxString wxFont::GetFaceName() const wxString wxFont::GetFaceName() const
{ {
wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid font") ); wxCHECK_MSG( IsOk(), wxEmptyString, wxT("invalid font") );
return M_FONTDATA->GetFaceName(); return M_FONTDATA->GetFaceName();
} }
wxFontEncoding wxFont::GetEncoding() const wxFontEncoding wxFont::GetEncoding() const
{ {
wxCHECK_MSG( Ok(), wxFONTENCODING_DEFAULT, wxT("invalid font") ); wxCHECK_MSG( IsOk(), wxFONTENCODING_DEFAULT, wxT("invalid font") );
return M_FONTDATA->GetEncoding(); return M_FONTDATA->GetEncoding();
} }
const wxNativeFontInfo *wxFont::GetNativeFontInfo() const const wxNativeFontInfo *wxFont::GetNativeFontInfo() const
{ {
return Ok() && M_FONTDATA->HasNativeFontInfo() ? &(M_FONTDATA->GetNativeFontInfo()) return IsOk() && M_FONTDATA->HasNativeFontInfo() ? &(M_FONTDATA->GetNativeFontInfo())
: NULL; : NULL;
} }
wxString wxFont::GetNativeFontInfoDesc() const wxString wxFont::GetNativeFontInfoDesc() const
{ {
wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid font") ); wxCHECK_MSG( IsOk(), wxEmptyString, wxT("invalid font") );
// be sure we have an HFONT associated... // be sure we have an HFONT associated...
wxConstCast(this, wxFont)->RealizeResource(); const_cast<wxFont*>(this)->RealizeResource();
return wxFontBase::GetNativeFontInfoDesc(); return wxFontBase::GetNativeFontInfoDesc();
} }
wxString wxFont::GetNativeFontInfoUserDesc() const wxString wxFont::GetNativeFontInfoUserDesc() const
{ {
wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid font") ); wxCHECK_MSG( IsOk(), wxEmptyString, wxT("invalid font") );
// be sure we have an HFONT associated... // be sure we have an HFONT associated...
wxConstCast(this, wxFont)->RealizeResource(); const_cast<wxFont*>(this)->RealizeResource();
return wxFontBase::GetNativeFontInfoUserDesc(); return wxFontBase::GetNativeFontInfoUserDesc();
} }
bool wxFont::IsFixedWidth() const bool wxFont::IsFixedWidth() const
{ {
wxCHECK_MSG( IsOk(), false, wxT("invalid font") );
if ( M_FONTDATA->HasNativeFontInfo() ) if ( M_FONTDATA->HasNativeFontInfo() )
{ {
// the two low-order bits specify the pitch of the font, the rest is // the two low-order bits specify the pitch of the font, the rest is