X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/adbd3cbc71cde23d1a12b76e820aa3fd33a6707f..79cb759dc8580da2e82e880cd94a08cd38bb814e:/src/msw/font.cpp?ds=sidebyside diff --git a/src/msw/font.cpp b/src/msw/font.cpp index 6189f9f505..ed8728be50 100644 --- a/src/msw/font.cpp +++ b/src/msw/font.cpp @@ -39,6 +39,10 @@ #include "wx/fontutil.h" #include "wx/fontmap.h" +#ifndef __WXWINCE__ + #include "wx/sysopt.h" +#endif + #include "wx/tokenzr.h" #if wxUSE_EXTENDED_RTTI @@ -112,9 +116,9 @@ public: wxFontRefData(int size, const wxSize& pixelSize, bool sizeUsingPixels, - int family, - int style, - int weight, + wxFontFamily family, + wxFontStyle style, + wxFontWeight weight, bool underlined, const wxString& faceName, wxFontEncoding encoding) @@ -145,7 +149,7 @@ public: virtual ~wxFontRefData(); // operations - bool Alloc(wxFont *font); + bool Alloc(const wxFont *font); void Free(); @@ -167,18 +171,18 @@ public: return m_nativeFontInfoOk ? true : m_sizeUsingPixels; } - int GetFamily() const + wxFontFamily GetFamily() const { return m_family; } - int GetStyle() const + wxFontStyle GetStyle() const { return m_nativeFontInfoOk ? m_nativeFontInfo.GetStyle() : m_style; } - int GetWeight() const + wxFontWeight GetWeight() const { return m_nativeFontInfoOk ? m_nativeFontInfo.GetWeight() : m_weight; @@ -207,11 +211,26 @@ public: : m_encoding; } - WXHFONT GetHFONT() const { return m_hFont; } + WXHFONT GetHFONT(const wxFont *font) const + { + if ( !m_hFont ) + const_cast(this)->Alloc(font); + + return (WXHFONT)m_hFont; + } - // ... and setters + 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) { + Free(); + if ( m_nativeFontInfoOk ) { m_nativeFontInfo.SetPointSize(pointSize); @@ -225,6 +244,8 @@ public: void SetPixelSize(const wxSize& pixelSize) { + Free(); + if ( m_nativeFontInfoOk ) { m_nativeFontInfo.SetPixelSize(pixelSize); @@ -236,21 +257,27 @@ public: } } - void SetFamily(int family) + void SetFamily(wxFontFamily family) { + Free(); + m_family = family; } - void SetStyle(int style) + void SetStyle(wxFontStyle style) { + Free(); + if ( m_nativeFontInfoOk ) m_nativeFontInfo.SetStyle((wxFontStyle)style); else m_style = style; } - void SetWeight(int weight) + void SetWeight(wxFontWeight weight) { + Free(); + if ( m_nativeFontInfoOk ) m_nativeFontInfo.SetWeight((wxFontWeight)weight); else @@ -259,6 +286,8 @@ public: bool SetFaceName(const wxString& faceName) { + Free(); + if ( m_nativeFontInfoOk ) return m_nativeFontInfo.SetFaceName(faceName); @@ -268,6 +297,8 @@ public: void SetUnderlined(bool underlined) { + Free(); + if ( m_nativeFontInfoOk ) m_nativeFontInfo.SetUnderlined(underlined); else @@ -276,45 +307,55 @@ public: 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; + m_nativeFontInfoOk = true; + } + protected: // common part of all ctors void Init(int size, const wxSize& pixelSize, bool sizeUsingPixels, - int family, - int style, - int weight, + wxFontFamily family, + wxFontStyle style, + wxFontWeight weight, bool underlined, const wxString& faceName, wxFontEncoding encoding); void Init(const wxNativeFontInfo& info, WXHFONT hFont = 0); - // font characterstics + // font characteristics int m_pointSize; wxSize m_pixelSize; bool m_sizeUsingPixels; - int m_family; - int m_style; - int m_weight; + wxFontFamily m_family; + wxFontStyle m_style; + wxFontWeight m_weight; bool m_underlined; 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; @@ -334,9 +375,9 @@ protected: void wxFontRefData::Init(int pointSize, const wxSize& pixelSize, bool sizeUsingPixels, - int family, - int style, - int weight, + wxFontFamily family, + wxFontStyle style, + wxFontWeight weight, bool underlined, const wxString& faceName, wxFontEncoding encoding) @@ -363,13 +404,13 @@ void wxFontRefData::Init(const wxNativeFontInfo& info, WXHFONT hFont) // 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; // This is the best we can do since we don't have the // correct information at this point. - m_family = wxSWISS; + m_family = wxFONTFAMILY_SWISS; } wxFontRefData::~wxFontRefData() @@ -377,7 +418,7 @@ wxFontRefData::~wxFontRefData() Free(); } -bool wxFontRefData::Alloc(wxFont *font) +bool wxFontRefData::Alloc(const wxFont *font) { if ( !m_nativeFontInfoOk ) { @@ -385,14 +426,13 @@ bool wxFontRefData::Alloc(wxFont *font) 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; } @@ -400,7 +440,7 @@ void wxFontRefData::Free() { if ( m_hFont ) { - if ( !::DeleteObject((HFONT) m_hFont) ) + if ( !::DeleteObject(m_hFont) ) { wxLogLastError(wxT("DeleteObject(font)")); } @@ -417,12 +457,16 @@ void wxNativeFontInfo::Init() { wxZeroMemory(lf); - // we get better font quality if we use this instead of DEFAULT_QUALITY - // apparently without any drawbacks + // we get better font quality if we use PROOF_QUALITY instead of + // DEFAULT_QUALITY but some fonts (e.g. "Terminal 6pt") are not available + // then so we allow to set a global option to choose between quality and + // wider font selection #ifdef __WXWINCE__ lf.lfQuality = CLEARTYPE_QUALITY; #else - lf.lfQuality = PROOF_QUALITY; + lf.lfQuality = wxSystemOptions::GetOptionInt(_T("msw.font.no-proof-quality")) + ? DEFAULT_QUALITY + : PROOF_QUALITY; #endif } @@ -432,13 +476,14 @@ int wxNativeFontInfo::GetPointSize() const // for printing! const int ppInch = ::GetDeviceCaps(ScreenHDC(), LOGPIXELSY); - return (int) (((72.0*(double)abs(lf.lfHeight)) / (double) ppInch) + 0.5); + // BC++ 2007 doesn't provide abs(long) overload, hence the cast + return (int) (((72.0*abs((int)lf.lfHeight)) / (double) ppInch) + 0.5); } wxSize wxNativeFontInfo::GetPixelSize() const { wxSize ret; - ret.SetHeight(lf.lfHeight); + ret.SetHeight(abs((int)lf.lfHeight)); ret.SetWidth(lf.lfWidth); return ret; } @@ -573,9 +618,7 @@ void wxNativeFontInfo::SetUnderlined(bool underlined) bool wxNativeFontInfo::SetFaceName(const wxString& facename) { - size_t len = WXSIZEOF(lf.lfFaceName); - wxStrncpy(lf.lfFaceName, facename, len); - lf.lfFaceName[len - 1] = '\0'; // truncate the face name + wxStrlcpy(lf.lfFaceName, facename.c_str(), WXSIZEOF(lf.lfFaceName)); return true; } @@ -791,7 +834,7 @@ wxString wxNativeFontInfo::ToString() const lf.lfClipPrecision, lf.lfQuality, lf.lfPitchAndFamily, - lf.lfFaceName); + (const wxChar*)lf.lfFaceName); return s; } @@ -819,9 +862,9 @@ wxFont::wxFont(const wxString& fontdesc) bool wxFont::DoCreate(int pointSize, const wxSize& pixelSize, bool sizeUsingPixels, - int family, - int style, - int weight, + wxFontFamily family, + wxFontStyle style, + wxFontWeight weight, bool underlined, const wxString& faceName, wxFontEncoding encoding) @@ -850,14 +893,14 @@ wxFont::~wxFont() // real implementation // ---------------------------------------------------------------------------- -wxObjectRefData *wxFont::CreateRefData() const +wxGDIRefData *wxFont::CreateGDIRefData() const { return new wxFontRefData(); } -wxObjectRefData *wxFont::CloneRefData(const wxObjectRefData *data) const +wxGDIRefData *wxFont::CloneGDIRefData(const wxGDIRefData *data) const { - return new wxFontRefData(*wx_static_cast(const wxFontRefData *, data)); + return new wxFontRefData(*static_cast(data)); } bool wxFont::RealizeResource() @@ -886,23 +929,12 @@ WXHANDLE wxFont::GetResourceHandle() const 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(); } // ---------------------------------------------------------------------------- @@ -913,9 +945,8 @@ void wxFont::SetPointSize(int pointSize) { AllocExclusive(); + M_FONTDATA->Free(); M_FONTDATA->SetPointSize(pointSize); - - Recreate(); } void wxFont::SetPixelSize(const wxSize& pixelSize) @@ -923,44 +954,35 @@ void wxFont::SetPixelSize(const wxSize& pixelSize) AllocExclusive(); M_FONTDATA->SetPixelSize(pixelSize); - - Recreate(); } -void wxFont::SetFamily(int family) +void wxFont::SetFamily(wxFontFamily family) { AllocExclusive(); M_FONTDATA->SetFamily(family); - - Recreate(); } -void wxFont::SetStyle(int style) +void wxFont::SetStyle(wxFontStyle style) { AllocExclusive(); M_FONTDATA->SetStyle(style); - - Recreate(); } -void wxFont::SetWeight(int weight) +void wxFont::SetWeight(wxFontWeight 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 @@ -969,7 +991,7 @@ bool wxFont::SetFaceName(const wxString& faceName) // 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) @@ -977,8 +999,6 @@ void wxFont::SetUnderlined(bool underlined) AllocExclusive(); M_FONTDATA->SetUnderlined(underlined); - - Recreate(); } void wxFont::SetEncoding(wxFontEncoding encoding) @@ -986,17 +1006,13 @@ 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); } // ---------------------------------------------------------------------------- @@ -1024,23 +1040,23 @@ bool wxFont::IsUsingSizeInPixels() const return M_FONTDATA->IsUsingSizeInPixels(); } -int wxFont::GetFamily() const +wxFontFamily wxFont::GetFamily() const { - wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); + wxCHECK_MSG( Ok(), wxFONTFAMILY_MAX, wxT("invalid font") ); return M_FONTDATA->GetFamily(); } -int wxFont::GetStyle() const +wxFontStyle wxFont::GetStyle() const { - wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); + wxCHECK_MSG( Ok(), wxFONTSTYLE_MAX, wxT("invalid font") ); return M_FONTDATA->GetStyle(); } -int wxFont::GetWeight() const +wxFontWeight wxFont::GetWeight() const { - wxCHECK_MSG( Ok(), 0, wxT("invalid font") ); + wxCHECK_MSG( Ok(), wxFONTWEIGHT_MAX, wxT("invalid font") ); return M_FONTDATA->GetWeight(); }