X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/c47addef107f756f1a342d2f3f8d432e784691a2..4a624f6ed375e4dbf126743ff1068486cb5241a2:/src/msw/font.cpp diff --git a/src/msw/font.cpp b/src/msw/font.cpp index c3ed95c514..024e43d3ae 100644 --- a/src/msw/font.cpp +++ b/src/msw/font.cpp @@ -17,10 +17,6 @@ // headers // ---------------------------------------------------------------------------- -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) - #pragma implementation "font.h" -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -28,12 +24,12 @@ #pragma hdrstop #endif +#include "wx/font.h" + #ifndef WX_PRECOMP - #include "wx/setup.h" #include "wx/list.h" #include "wx/utils.h" #include "wx/app.h" - #include "wx/font.h" #include "wx/log.h" #include "wx/encinfo.h" #endif // WX_PRECOMP @@ -43,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 @@ -149,7 +149,7 @@ public: virtual ~wxFontRefData(); // operations - bool Alloc(wxFont *font); + bool Alloc(const wxFont *font); void Free(); @@ -211,11 +211,26 @@ public: : 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); @@ -229,6 +244,8 @@ public: void SetPixelSize(const wxSize& pixelSize) { + Free(); + if ( m_nativeFontInfoOk ) { m_nativeFontInfo.SetPixelSize(pixelSize); @@ -242,11 +259,15 @@ public: void SetFamily(int family) { + Free(); + m_family = family; } void SetStyle(int style) { + Free(); + if ( m_nativeFontInfoOk ) m_nativeFontInfo.SetStyle((wxFontStyle)style); else @@ -255,22 +276,29 @@ public: void SetWeight(int weight) { + Free(); + if ( m_nativeFontInfoOk ) m_nativeFontInfo.SetWeight((wxFontWeight)weight); else m_weight = weight; } - void SetFaceName(const wxString& faceName) + bool SetFaceName(const wxString& faceName) { + Free(); + if ( m_nativeFontInfoOk ) - m_nativeFontInfo.SetFaceName(faceName); - else - m_faceName = faceName; + return m_nativeFontInfo.SetFaceName(faceName); + + m_faceName = faceName; + return true; } void SetUnderlined(bool underlined) { + Free(); + if ( m_nativeFontInfoOk ) m_nativeFontInfo.SetUnderlined(underlined); else @@ -279,18 +307,28 @@ 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, @@ -305,7 +343,7 @@ protected: void Init(const wxNativeFontInfo& info, WXHFONT hFont = 0); - // font characterstics + // font characteristics int m_pointSize; wxSize m_pixelSize; bool m_sizeUsingPixels; @@ -316,14 +354,16 @@ protected: 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; bool m_nativeFontInfoOk; }; +#define M_FONTDATA ((wxFontRefData*)m_refData) + // ============================================================================ // implementation // ============================================================================ @@ -364,7 +404,7 @@ 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; @@ -378,7 +418,7 @@ wxFontRefData::~wxFontRefData() Free(); } -bool wxFontRefData::Alloc(wxFont *font) +bool wxFontRefData::Alloc(const wxFont *font) { if ( !m_nativeFontInfoOk ) { @@ -386,16 +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; } @@ -403,7 +440,7 @@ void wxFontRefData::Free() { if ( m_hFont ) { - if ( !::DeleteObject((HFONT) m_hFont) ) + if ( !::DeleteObject(m_hFont) ) { wxLogLastError(wxT("DeleteObject(font)")); } @@ -419,6 +456,18 @@ void wxFontRefData::Free() void wxNativeFontInfo::Init() { wxZeroMemory(lf); + + // 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 = wxSystemOptions::GetOptionInt(_T("msw.font.no-proof-quality")) + ? DEFAULT_QUALITY + : PROOF_QUALITY; +#endif } int wxNativeFontInfo::GetPointSize() const @@ -427,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; } @@ -566,48 +616,86 @@ void wxNativeFontInfo::SetUnderlined(bool underlined) lf.lfUnderline = underlined; } -void wxNativeFontInfo::SetFaceName(wxString facename) +bool wxNativeFontInfo::SetFaceName(const wxString& facename) { - wxStrncpy(lf.lfFaceName, facename, WXSIZEOF(lf.lfFaceName)); + size_t len = WXSIZEOF(lf.lfFaceName); + wxStrncpy(lf.lfFaceName, facename, len); + lf.lfFaceName[len - 1] = '\0'; // truncate the face name + return true; } void wxNativeFontInfo::SetFamily(wxFontFamily family) { BYTE ff_family; - wxString facename; + wxArrayString facename; + + // the list of fonts associated with a family was partially + // taken from http://www.codestyle.org/css/font-family switch ( family ) { case wxSCRIPT: ff_family = FF_SCRIPT; - facename = _T("Script"); + facename.Add(_T("Script")); + facename.Add(_T("Brush Script MT")); + facename.Add(_T("Comic Sans MS")); + facename.Add(_T("Lucida Handwriting")); break; case wxDECORATIVE: ff_family = FF_DECORATIVE; - facename = _T("Old English Text MT"); + facename.Add(_T("Old English Text MT")); + facename.Add(_T("Comic Sans MS")); + facename.Add(_T("Lucida Handwriting")); break; case wxROMAN: ff_family = FF_ROMAN; - facename = _T("Times New Roman"); + facename.Add(_T("Times New Roman")); + facename.Add(_T("Georgia")); + facename.Add(_T("Garamond")); + facename.Add(_T("Bookman Old Style")); + facename.Add(_T("Book Antiqua")); break; case wxTELETYPE: case wxMODERN: ff_family = FF_MODERN; - facename = _T("Courier New"); + facename.Add(_T("Courier New")); + facename.Add(_T("Lucida Console")); + facename.Add(_T("Andale Mono")); + facename.Add(_T("OCR A Extended")); + facename.Add(_T("Terminal")); break; case wxSWISS: ff_family = FF_SWISS; - facename = _T("Arial"); + facename.Add(_T("Arial")); + facename.Add(_T("Century Gothic")); + facename.Add(_T("Lucida Sans Unicode")); + facename.Add(_T("Tahoma")); + facename.Add(_T("Trebuchet MS")); + facename.Add(_T("Verdana")); break; case wxDEFAULT: default: + { + // We want Windows 2000 or later to have new fonts even MS Shell Dlg + // is returned as default GUI font for compatibility + int verMaj; ff_family = FF_SWISS; - facename = _T("MS Sans Serif"); + if(wxGetOsVersion(&verMaj) == wxOS_WINDOWS_NT && verMaj >= 5) + facename.Add(_T("MS Shell Dlg 2")); + else + facename.Add(_T("MS Shell Dlg")); + + // Quoting the MSDN: + // "MS Shell Dlg is a mapping mechanism that enables + // U.S. English Microsoft Windows NT, and Microsoft Windows 2000 to + // support locales that have characters that are not contained in code + // page 1252. It is not a font but a face name for a nonexistent font." + } } lf.lfPitchAndFamily = (BYTE)(DEFAULT_PITCH) | ff_family; @@ -748,7 +836,7 @@ wxString wxNativeFontInfo::ToString() const lf.lfClipPrecision, lf.lfQuality, lf.lfPitchAndFamily, - lf.lfFaceName); + (const wxChar*)lf.lfFaceName); return s; } @@ -757,19 +845,13 @@ wxString wxNativeFontInfo::ToString() const // wxFont // ---------------------------------------------------------------------------- -void wxFont::Init() -{ -} - bool wxFont::Create(const wxNativeFontInfo& info, WXHFONT hFont) { UnRef(); m_refData = new wxFontRefData(info, hFont); - RealizeResource(); - - return true; + return RealizeResource(); } wxFont::wxFont(const wxString& fontdesc) @@ -779,9 +861,6 @@ wxFont::wxFont(const wxString& fontdesc) (void)Create(info); } -/* Constructor for a font. Note that the real construction is done - * in wxDC::SetFont, when information is available about scaling etc. - */ bool wxFont::DoCreate(int pointSize, const wxSize& pixelSize, bool sizeUsingPixels, @@ -805,9 +884,7 @@ bool wxFont::DoCreate(int pointSize, family, style, weight, underlined, faceName, encoding); - RealizeResource(); - - return true; + return RealizeResource(); } wxFont::~wxFont() @@ -818,28 +895,33 @@ wxFont::~wxFont() // real implementation // ---------------------------------------------------------------------------- +wxObjectRefData *wxFont::CreateRefData() const +{ + return new wxFontRefData(); +} + +wxObjectRefData *wxFont::CloneRefData(const wxObjectRefData *data) const +{ + return new wxFontRefData(*wx_static_cast(const wxFontRefData *, data)); +} + bool wxFont::RealizeResource() { - if ( GetResourceHandle() ) - { - // VZ: the old code returned false in this case, but it doesn't seem - // to make sense because the font _was_ created + // don't do anything if we already have a valid font + if ( GetHFONT() ) return true; - } return M_FONTDATA->Alloc(this); } bool wxFont::FreeResource(bool WXUNUSED(force)) { - if ( GetResourceHandle() ) - { - M_FONTDATA->Free(); + if ( !GetHFONT() ) + return false; - return true; - } + M_FONTDATA->Free(); - return false; + return true; } WXHANDLE wxFont::GetResourceHandle() const @@ -849,27 +931,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); -} - -void wxFont::Unshare() -{ - // Don't change shared data - if ( !m_refData ) - { - m_refData = new wxFontRefData(); - } - else - { - wxFontRefData* ref = new wxFontRefData(*M_FONTDATA); - UnRef(); - m_refData = ref; - } + return M_FONTDATA && !M_FONTDATA->HasHFONT(); } // ---------------------------------------------------------------------------- @@ -878,85 +945,76 @@ void wxFont::Unshare() void wxFont::SetPointSize(int pointSize) { - Unshare(); + AllocExclusive(); + M_FONTDATA->Free(); M_FONTDATA->SetPointSize(pointSize); - - RealizeResource(); } void wxFont::SetPixelSize(const wxSize& pixelSize) { - Unshare(); + AllocExclusive(); M_FONTDATA->SetPixelSize(pixelSize); - - RealizeResource(); } void wxFont::SetFamily(int family) { - Unshare(); + AllocExclusive(); M_FONTDATA->SetFamily(family); - - RealizeResource(); } void wxFont::SetStyle(int style) { - Unshare(); + AllocExclusive(); M_FONTDATA->SetStyle(style); - - RealizeResource(); } void wxFont::SetWeight(int weight) { - Unshare(); + AllocExclusive(); M_FONTDATA->SetWeight(weight); - - RealizeResource(); } -void wxFont::SetFaceName(const wxString& faceName) +bool wxFont::SetFaceName(const wxString& faceName) { - Unshare(); + AllocExclusive(); - M_FONTDATA->SetFaceName(faceName); + if ( !M_FONTDATA->SetFaceName(faceName) ) + return false; - RealizeResource(); + // NB: using win32's GetObject() API on M_FONTDATA->GetHFONT() + // to retrieve a LOGFONT and then compare lf.lfFaceName + // with given facename is not reliable at all: + // Windows copies the facename given to ::CreateFontIndirect() + // without any validity check. + // Thus we use wxFontBase::SetFaceName to check if facename + // is valid... + return wxFontBase::SetFaceName(faceName); } void wxFont::SetUnderlined(bool underlined) { - Unshare(); + AllocExclusive(); M_FONTDATA->SetUnderlined(underlined); - - RealizeResource(); } void wxFont::SetEncoding(wxFontEncoding encoding) { - Unshare(); + AllocExclusive(); M_FONTDATA->SetEncoding(encoding); - - RealizeResource(); } void wxFont::DoSetNativeFontInfo(const wxNativeFontInfo& info) { - Unshare(); - - FreeResource(); - - *M_FONTDATA = wxFontRefData(info); + AllocExclusive(); - RealizeResource(); + M_FONTDATA->SetNativeFontInfo(info); } // ---------------------------------------------------------------------------- @@ -972,6 +1030,8 @@ int wxFont::GetPointSize() const wxSize wxFont::GetPixelSize() const { + wxCHECK_MSG( Ok(), wxDefaultSize, wxT("invalid font") ); + return M_FONTDATA->GetPixelSize(); } @@ -1026,10 +1086,28 @@ wxFontEncoding wxFont::GetEncoding() const const wxNativeFontInfo *wxFont::GetNativeFontInfo() const { - return M_FONTDATA->HasNativeFontInfo() ? &(M_FONTDATA->GetNativeFontInfo()) + return Ok() && M_FONTDATA->HasNativeFontInfo() ? &(M_FONTDATA->GetNativeFontInfo()) : NULL; } +wxString wxFont::GetNativeFontInfoDesc() const +{ + wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid font") ); + + // be sure we have an HFONT associated... + wxConstCast(this, wxFont)->RealizeResource(); + return wxFontBase::GetNativeFontInfoDesc(); +} + +wxString wxFont::GetNativeFontInfoUserDesc() const +{ + wxCHECK_MSG( Ok(), wxEmptyString, wxT("invalid font") ); + + // be sure we have an HFONT associated... + wxConstCast(this, wxFont)->RealizeResource(); + return wxFontBase::GetNativeFontInfoUserDesc(); +} + bool wxFont::IsFixedWidth() const { if ( M_FONTDATA->HasNativeFontInfo() ) @@ -1044,4 +1122,3 @@ bool wxFont::IsFixedWidth() const return wxFontBase::IsFixedWidth(); } -