]> git.saurik.com Git - wxWidgets.git/commitdiff
merged wxFont related fix (operator==) and optimization (cache default GUI font)
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 21 Apr 2001 16:03:10 +0000 (16:03 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 21 Apr 2001 16:03:10 +0000 (16:03 +0000)
from 2.2

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@9827 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/msw/settings.h
include/wx/tbarbase.h
src/common/fontcmn.cpp
src/common/wincmn.cpp
src/generic/panelg.cpp
src/msw/dc.cpp
src/msw/dialog.cpp
src/msw/settings.cpp
src/msw/window.cpp

index 1f16e900c2bd2ef703432781d73f22b4e265efed..5dad2084f592914b014cd050b85d16e857d71192 100644 (file)
@@ -1,12 +1,12 @@
 /////////////////////////////////////////////////////////////////////////////
-// Name:        settings.h
+// Name:        wx/msw/settings.h
 // Purpose:     wxSystemSettings class
 // Author:      Julian Smart
 // Modified by:
 // Created:     01/02/97
 // RCS-ID:      $Id$
 // Copyright:   (c) Julian Smart
-// Licence:    wxWindows licence
+// Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 #ifndef _WX_SETTINGS_H_
 #pragma interface "settings.h"
 #endif
 
-#include "wx/setup.h"
 #include "wx/colour.h"
 #include "wx/font.h"
 
-class WXDLLEXPORT wxSystemSettings: public wxObject
+class WXDLLEXPORT wxSystemSettings : public wxObject
 {
 public:
-    inline wxSystemSettings(void) {}
+    wxSystemSettings() { }
 
     // Get a system colour
-    static wxColour    GetSystemColour(int index);
+    static wxColour GetSystemColour(int index);
 
     // Get a system font
-    static wxFont      GetSystemFont(int index);
+    static wxFont GetSystemFont(int index);
 
     // Get a system metric, e.g. scrollbar size
-    static int         GetSystemMetric(int index);
+    static int GetSystemMetric(int index);
 };
 
 #endif
index cf7eeac27cb6c91257fb913dd60603d3295edbb7..ea970821bfde7446893af0a688c27dfabe0371ba 100644 (file)
@@ -411,7 +411,7 @@ public:
     virtual void DoToolbarUpdates();
 
     // Don't want toolbars to accept the focus
-    bool AcceptsFocus() const { return FALSE; }
+    virtual bool AcceptsFocus() const { return FALSE; }
 
 protected:
     // to implement in derived classes
index e973ffbc0d0924395040bc25efbdca8402298fb7..44cca93b72ba2254feb4fb44232735958674d962 100644 (file)
@@ -128,15 +128,25 @@ wxFont& wxFont::operator=(const wxFont& font)
     return (wxFont &)*this;
 }
 
-// VZ: is it correct to compare pointers and not the contents? (FIXME)
 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
 {
-    return GetFontData() != font.GetFontData();
+    return !(*this == font);
 }
 
 wxString wxFontBase::GetFamilyString() const
index 1e8541a0b0caa70e7e9b39c3ddc469ed3d71dc25..4e57c5fb9dd2c841ecfb860e03db77e9eb19d044 100644 (file)
@@ -130,8 +130,12 @@ void wxWindowBase::InitBase()
     m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_BTNFACE);
     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);
+#endif // __WXMSW__
 
     // no style bits
     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
     const wxFont& fontOk = font.Ok() ? font : *wxSWISS_FONT;
 
-    if ( (wxFont&)fontOk == m_font )
+    if ( fontOk == m_font )
     {
         // no change
         return FALSE;
index 3cab12dccb1516b1b8260365664bcff2b34fe35b..3052aabd7b1786988a8f0378f32801659548ef50 100644 (file)
@@ -74,15 +74,7 @@ bool wxPanel::Create(wxWindow *parent, wxWindowID id,
                      long style,
                      const wxString& name)
 {
-    bool ret = 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;
+    return wxWindow::Create(parent, id, pos, size, style, name);
 }
 
 // ----------------------------------------------------------------------------
index 1863a0d1da6daf2dd4d0a87a6112768c110c0b1d..86e1a772cf018899a575d3be305a9a98db7bc6f2 100644 (file)
@@ -1213,22 +1213,35 @@ wxCoord wxDC::GetCharWidth() const
 
 void wxDC::DoGetTextExtent(const wxString& string, wxCoord *x, wxCoord *y,
                            wxCoord *descent, wxCoord *externalLeading,
-                           wxFont *theFont) const
+                           wxFont *font) const
 {
-    wxFont *fontToUse = (wxFont*) theFont;
-    if (!fontToUse)
-        fontToUse = (wxFont*) &m_font;
+    HFONT hfontOld;
+    if ( 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;
     TEXTMETRIC tm;
 
-    GetTextExtentPoint(GetHdc(), WXSTRINGCAST string, wxStrlen(WXSTRINGCAST string), &sizeRect);
+    GetTextExtentPoint(GetHdc(), string, string.length(), &sizeRect);
     GetTextMetrics(GetHdc(), &tm);
 
     if (x) *x = XDEV2LOGREL(sizeRect.cx);
     if (y) *y = YDEV2LOGREL(sizeRect.cy);
     if (descent) *descent = tm.tmDescent;
     if (externalLeading) *externalLeading = tm.tmExternalLeading;
+
+    if ( hfontOld )
+    {
+        ::SelectObject(GetHdc(), hfontOld);
+    }
 }
 
 void wxDC::SetMapMode(int mode)
index 889355a4b8f38d0428e1e8b0d731939b95566358..f38a3e9c06f353c4ea5ba4070589393ca13cc31b 100644 (file)
@@ -188,7 +188,6 @@ bool wxDialog::Create(wxWindow *parent,
     SubclassWin(GetHWND());
 
     SetWindowText(hwnd, title);
-    SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
 
     return TRUE;
 }
index bfdb3d75061331660a7af9e0c09c5eb10ee8473f..b636a80883f7c256df11390f2965265092f39df4 100644 (file)
@@ -1,6 +1,6 @@
 /////////////////////////////////////////////////////////////////////////////
-// Name:        settings.cpp
-// Purpose:     wxSettings
+// Name:        msw/settings.cpp
+// Purpose:     wxSystemSettings
 // Author:      Julian Smart
 // Modified by:
 // Created:     04/01/98
@@ -9,8 +9,16 @@
 // Licence:     wxWindows license
 /////////////////////////////////////////////////////////////////////////////
 
+// ============================================================================
+// declarations
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// headers
+// ----------------------------------------------------------------------------
+
 #ifdef __GNUG__
-#pragma implementation "settings.h"
+    #pragma implementation "settings.h"
 #endif
 
 // For compilers that support precompilation, includes "wx.h".
 #endif
 
 #ifndef WX_PRECOMP
-#include <stdio.h>
-#include "wx/defs.h"
-#include "wx/pen.h"
-#include "wx/brush.h"
-#include "wx/gdicmn.h"
+    #include <stdio.h>
+    #include "wx/defs.h"
+    #include "wx/pen.h"
+    #include "wx/brush.h"
+    #include "wx/gdicmn.h"
 #endif
 
 #include "wx/settings.h"
 #include "wx/window.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.
 // Different args are required depending on the id. How does this differ
 // from GetSystemMetric, and should it? Perhaps call it GetSystemParameter
@@ -45,7 +99,7 @@ wxColour wxSystemSettings::GetSystemColour(int index)
     {
         case wxSYS_COLOUR_LISTBOX:
             return *wxWHITE;
-    
+
         default:
             COLORREF ref = ::GetSysColor(index);
             wxColour col(GetRValue(ref), GetGValue(ref), GetBValue(ref));
@@ -55,24 +109,41 @@ wxColour wxSystemSettings::GetSystemColour(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);
-    if ( hFont != (HFONT) NULL )
+    if ( hFont )
     {
         LOGFONT lf;
         if ( ::GetObject(hFont, sizeof(LOGFONT), &lf) != 0 )
         {
-            // In fontdlg.cpp
-            return wxCreateFontFromLogFont(&lf);
+            font = wxCreateFontFromLogFont(&lf);
         }
         else
         {
-            return wxNullFont;
+            wxFAIL_MSG( _T("failed to get LOGFONT") );
         }
     }
-    else
+    else // GetStockObject() failed
+    {
+        wxFAIL_MSG( _T("stock font not found") );
+    }
+
+    if ( isDefaultRequested )
     {
-        return wxNullFont;
+        // 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
index f58179b4c3f260467ee1a7e9aa2c6fcc774fcd50..4ff1428eaab1585e885da39493d2b7d41b46f961 100644 (file)
@@ -342,12 +342,10 @@ bool wxWindow::Create(wxWindow *parent, wxWindowID id,
                      DLGC_WANTTAB | DLGC_WANTMESSAGE;
     }
 
-    MSWCreate(m_windowId, parent, wxCanvasClassName, this, NULL,
-              pos.x, pos.y,
-              WidthDefault(size.x), HeightDefault(size.y),
-              msflags, NULL, exStyle);
-
-    return TRUE;
+    return MSWCreate(m_windowId, parent, wxCanvasClassName, this, NULL,
+                     pos.x, pos.y,
+                     WidthDefault(size.x), HeightDefault(size.y),
+                     msflags, NULL, exStyle);
 }
 
 // ---------------------------------------------------------------------------
@@ -2512,6 +2510,8 @@ bool wxWindow::MSWCreate(int id,
 
     wxAssociateWinWithHandle((HWND) m_hWnd, this);
 
+    SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
+
     return TRUE;
 }