merged wxFont related fix (operator==) and optimization (cache default GUI font)

from 2.2


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@9827 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2001-04-21 16:03:10 +00:00
parent f4929e864e
commit 8bf30fe9b0
9 changed files with 138 additions and 50 deletions

View File

@@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// Name: settings.h // Name: wx/msw/settings.h
// Purpose: wxSystemSettings class // Purpose: wxSystemSettings class
// Author: Julian Smart // Author: Julian Smart
// Modified by: // Modified by:
@@ -16,14 +16,13 @@
#pragma interface "settings.h" #pragma interface "settings.h"
#endif #endif
#include "wx/setup.h"
#include "wx/colour.h" #include "wx/colour.h"
#include "wx/font.h" #include "wx/font.h"
class WXDLLEXPORT wxSystemSettings: public wxObject class WXDLLEXPORT wxSystemSettings : public wxObject
{ {
public: public:
inline wxSystemSettings(void) {} wxSystemSettings() { }
// Get a system colour // Get a system colour
static wxColour GetSystemColour(int index); static wxColour GetSystemColour(int index);

View File

@@ -411,7 +411,7 @@ public:
virtual void DoToolbarUpdates(); virtual void DoToolbarUpdates();
// Don't want toolbars to accept the focus // Don't want toolbars to accept the focus
bool AcceptsFocus() const { return FALSE; } virtual bool AcceptsFocus() const { return FALSE; }
protected: protected:
// to implement in derived classes // to implement in derived classes

View File

@@ -128,15 +128,25 @@ wxFont& wxFont::operator=(const wxFont& font)
return (wxFont &)*this; return (wxFont &)*this;
} }
// VZ: is it correct to compare pointers and not the contents? (FIXME)
bool wxFontBase::operator==(const wxFont& font) const bool wxFontBase::operator==(const wxFont& font) const
{ {
return GetFontData() == font.GetFontData(); // either it is the same font, i.e. they share the same common data or they
// have different ref datas but still describe the same font
return GetFontData() == font.GetFontData() ||
(
Ok() == font.Ok() &&
GetPointSize() == font.GetPointSize() &&
GetFamily() == font.GetFamily() &&
GetStyle() == font.GetStyle() &&
GetUnderlined() == font.GetUnderlined() &&
GetFaceName() == font.GetFaceName() &&
GetEncoding() == font.GetEncoding()
);
} }
bool wxFontBase::operator!=(const wxFont& font) const bool wxFontBase::operator!=(const wxFont& font) const
{ {
return GetFontData() != font.GetFontData(); return !(*this == font);
} }
wxString wxFontBase::GetFamilyString() const wxString wxFontBase::GetFamilyString() const

View File

@@ -130,8 +130,12 @@ void wxWindowBase::InitBase()
m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE); m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE);
m_foregroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT); m_foregroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOWTEXT);
// GRG, changed Mar/2000 // don't set the font here for wxMSW as we don't call WM_SETFONT here and
// so the font is *not* really set - but calls to SetFont() later won't do
// anything because m_font appears to be already set!
#ifndef __WXMSW__
m_font = wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT); m_font = wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT);
#endif // __WXMSW__
// no style bits // no style bits
m_exStyle = m_exStyle =
@@ -642,7 +646,7 @@ bool wxWindowBase::SetFont(const wxFont& font)
// don't try to set invalid font, always fall back to the default // don't try to set invalid font, always fall back to the default
const wxFont& fontOk = font.Ok() ? font : *wxSWISS_FONT; const wxFont& fontOk = font.Ok() ? font : *wxSWISS_FONT;
if ( (wxFont&)fontOk == m_font ) if ( fontOk == m_font )
{ {
// no change // no change
return FALSE; return FALSE;

View File

@@ -74,15 +74,7 @@ bool wxPanel::Create(wxWindow *parent, wxWindowID id,
long style, long style,
const wxString& name) const wxString& name)
{ {
bool ret = wxWindow::Create(parent, id, pos, size, style, name); return wxWindow::Create(parent, id, pos, size, style, name);
if ( ret )
{
SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
}
return ret;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@@ -1213,22 +1213,35 @@ wxCoord wxDC::GetCharWidth() const
void wxDC::DoGetTextExtent(const wxString& string, wxCoord *x, wxCoord *y, void wxDC::DoGetTextExtent(const wxString& string, wxCoord *x, wxCoord *y,
wxCoord *descent, wxCoord *externalLeading, wxCoord *descent, wxCoord *externalLeading,
wxFont *theFont) const wxFont *font) const
{ {
wxFont *fontToUse = (wxFont*) theFont; HFONT hfontOld;
if (!fontToUse) if ( font )
fontToUse = (wxFont*) &m_font; {
wxASSERT_MSG( font->Ok(), _T("invalid font in wxDC::GetTextExtent") );
hfontOld = (HFONT)::SelectObject(GetHdc(), GetHfontOf(*font));
}
else // don't change the font
{
hfontOld = 0;
}
SIZE sizeRect; SIZE sizeRect;
TEXTMETRIC tm; TEXTMETRIC tm;
GetTextExtentPoint(GetHdc(), WXSTRINGCAST string, wxStrlen(WXSTRINGCAST string), &sizeRect); GetTextExtentPoint(GetHdc(), string, string.length(), &sizeRect);
GetTextMetrics(GetHdc(), &tm); GetTextMetrics(GetHdc(), &tm);
if (x) *x = XDEV2LOGREL(sizeRect.cx); if (x) *x = XDEV2LOGREL(sizeRect.cx);
if (y) *y = YDEV2LOGREL(sizeRect.cy); if (y) *y = YDEV2LOGREL(sizeRect.cy);
if (descent) *descent = tm.tmDescent; if (descent) *descent = tm.tmDescent;
if (externalLeading) *externalLeading = tm.tmExternalLeading; if (externalLeading) *externalLeading = tm.tmExternalLeading;
if ( hfontOld )
{
::SelectObject(GetHdc(), hfontOld);
}
} }
void wxDC::SetMapMode(int mode) void wxDC::SetMapMode(int mode)

View File

@@ -188,7 +188,6 @@ bool wxDialog::Create(wxWindow *parent,
SubclassWin(GetHWND()); SubclassWin(GetHWND());
SetWindowText(hwnd, title); SetWindowText(hwnd, title);
SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
return TRUE; return TRUE;
} }

View File

@@ -1,6 +1,6 @@
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// Name: settings.cpp // Name: msw/settings.cpp
// Purpose: wxSettings // Purpose: wxSystemSettings
// Author: Julian Smart // Author: Julian Smart
// Modified by: // Modified by:
// Created: 04/01/98 // Created: 04/01/98
@@ -9,8 +9,16 @@
// Licence: wxWindows license // Licence: wxWindows license
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#ifdef __GNUG__ #ifdef __GNUG__
#pragma implementation "settings.h" #pragma implementation "settings.h"
#endif #endif
// For compilers that support precompilation, includes "wx.h". // For compilers that support precompilation, includes "wx.h".
@@ -21,17 +29,63 @@
#endif #endif
#ifndef WX_PRECOMP #ifndef WX_PRECOMP
#include <stdio.h> #include <stdio.h>
#include "wx/defs.h" #include "wx/defs.h"
#include "wx/pen.h" #include "wx/pen.h"
#include "wx/brush.h" #include "wx/brush.h"
#include "wx/gdicmn.h" #include "wx/gdicmn.h"
#endif #endif
#include "wx/settings.h" #include "wx/settings.h"
#include "wx/window.h" #include "wx/window.h"
#include "wx/msw/private.h" #include "wx/msw/private.h"
// ----------------------------------------------------------------------------
// private classes
// ----------------------------------------------------------------------------
// the module which is used to clean up wxSystemSettings data (this is a
// singleton class so it can't be done in the dtor)
class wxSystemSettingsModule : public wxModule
{
public:
virtual bool OnInit();
virtual void OnExit();
private:
DECLARE_DYNAMIC_CLASS(wxSystemSettingsModule)
};
// ----------------------------------------------------------------------------
// global data
// ----------------------------------------------------------------------------
static wxFont *gs_fontDefault = NULL;
// ============================================================================
// implementation
// ============================================================================
// ----------------------------------------------------------------------------
// wxSystemSettingsModule
// ----------------------------------------------------------------------------
IMPLEMENT_DYNAMIC_CLASS(wxSystemSettingsModule, wxModule)
bool wxSystemSettingsModule::OnInit()
{
return TRUE;
}
void wxSystemSettingsModule::OnExit()
{
delete gs_fontDefault;
}
// ----------------------------------------------------------------------------
// wxSystemSettings
// ----------------------------------------------------------------------------
// TODO: see ::SystemParametersInfo for all sorts of Windows settings. // TODO: see ::SystemParametersInfo for all sorts of Windows settings.
// Different args are required depending on the id. How does this differ // Different args are required depending on the id. How does this differ
// from GetSystemMetric, and should it? Perhaps call it GetSystemParameter // from GetSystemMetric, and should it? Perhaps call it GetSystemParameter
@@ -55,24 +109,41 @@ wxColour wxSystemSettings::GetSystemColour(int index)
wxFont wxSystemSettings::GetSystemFont(int index) wxFont wxSystemSettings::GetSystemFont(int index)
{ {
// wxWindow ctor calls GetSystemFont(wxSYS_DEFAULT_GUI_FONT) so we're
// called fairly often - this is why we cache this particular font
bool isDefaultRequested = index == wxSYS_DEFAULT_GUI_FONT;
if ( isDefaultRequested && gs_fontDefault )
{
return *gs_fontDefault;
}
wxFont font;
HFONT hFont = (HFONT) ::GetStockObject(index); HFONT hFont = (HFONT) ::GetStockObject(index);
if ( hFont != (HFONT) NULL ) if ( hFont )
{ {
LOGFONT lf; LOGFONT lf;
if ( ::GetObject(hFont, sizeof(LOGFONT), &lf) != 0 ) if ( ::GetObject(hFont, sizeof(LOGFONT), &lf) != 0 )
{ {
// In fontdlg.cpp font = wxCreateFontFromLogFont(&lf);
return wxCreateFontFromLogFont(&lf);
} }
else else
{ {
return wxNullFont; wxFAIL_MSG( _T("failed to get LOGFONT") );
} }
} }
else else // GetStockObject() failed
{ {
return wxNullFont; wxFAIL_MSG( _T("stock font not found") );
} }
if ( isDefaultRequested )
{
// if we got here it means we hadn't cached it yet - do now
gs_fontDefault = new wxFont(font);
}
return font;
} }
// Get a system metric, e.g. scrollbar size // Get a system metric, e.g. scrollbar size

View File

@@ -342,12 +342,10 @@ bool wxWindow::Create(wxWindow *parent, wxWindowID id,
DLGC_WANTTAB | DLGC_WANTMESSAGE; DLGC_WANTTAB | DLGC_WANTMESSAGE;
} }
MSWCreate(m_windowId, parent, wxCanvasClassName, this, NULL, return MSWCreate(m_windowId, parent, wxCanvasClassName, this, NULL,
pos.x, pos.y, pos.x, pos.y,
WidthDefault(size.x), HeightDefault(size.y), WidthDefault(size.x), HeightDefault(size.y),
msflags, NULL, exStyle); msflags, NULL, exStyle);
return TRUE;
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -2512,6 +2510,8 @@ bool wxWindow::MSWCreate(int id,
wxAssociateWinWithHandle((HWND) m_hWnd, this); wxAssociateWinWithHandle((HWND) m_hWnd, this);
SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
return TRUE; return TRUE;
} }