]> git.saurik.com Git - wxWidgets.git/commitdiff
total wxBrush cleanup: made private stuff private, fixed data (un)sharing, create...
authorVadim Zeitlin <vadim@wxwidgets.org>
Mon, 4 Feb 2002 01:35:30 +0000 (01:35 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Mon, 4 Feb 2002 01:35:30 +0000 (01:35 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@13987 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/msw/brush.h
src/msw/brush.cpp
src/msw/dc.cpp

index 1ec1b80b7e5f7513006575f6b0ac1f739e0935b1..ccc307e1188d231e9f10edee67a2e9053d282ef7 100644 (file)
@@ -1,19 +1,19 @@
 /////////////////////////////////////////////////////////////////////////////
-// Name:        brush.h
+// Name:        wx/msw/brush.h
 // Purpose:     wxBrush 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_BRUSH_H_
 #define _WX_BRUSH_H_
 
 #ifdef __GNUG__
-#pragma interface "brush.h"
+    #pragma interface "brush.h"
 #endif
 
 #include "wx/gdicmn.h"
 
 class WXDLLEXPORT wxBrush;
 
-class WXDLLEXPORT wxBrushRefData: public wxGDIRefData
+// ----------------------------------------------------------------------------
+// wxBrush
+// ----------------------------------------------------------------------------
+
+class WXDLLEXPORT wxBrush : public wxGDIObject
 {
-    friend class WXDLLEXPORT wxBrush;
 public:
-    wxBrushRefData(void);
-    wxBrushRefData(const wxBrushRefData& data);
-    ~wxBrushRefData(void);
-
-protected:
-  int           m_style;
-  wxBitmap      m_stipple ;
-  wxColour      m_colour;
-  WXHBRUSH      m_hBrush;
-};
+    wxBrush();
+    wxBrush(const wxColour& col, int style);
+    wxBrush(const wxBitmap& stipple);
+    wxBrush(const wxBrush& brush) { Ref(brush); }
+    virtual ~wxBrush();
 
-#define M_BRUSHDATA ((wxBrushRefData *)m_refData)
+    virtual void SetColour(const wxColour& col);
+    virtual void SetColour(unsigned char r, unsigned char g, unsigned char b);
+    virtual void SetStyle(int style);
+    virtual void SetStipple(const wxBitmap& stipple);
 
-// Brush
-class WXDLLEXPORT wxBrush: public wxGDIObject
-{
-  DECLARE_DYNAMIC_CLASS(wxBrush)
-
-public:
-  wxBrush(void);
-  wxBrush(const wxColour& col, int style);
-  wxBrush(const wxBitmap& stipple);
-  inline wxBrush(const wxBrush& brush) { Ref(brush); }
-  ~wxBrush(void);
+    wxBrush& operator=(const wxBrush& brush);
+    bool operator==(const wxBrush& brush) const;
+    bool operator!=(const wxBrush& brush) const { return !(*this == brush); }
 
-  virtual void SetColour(const wxColour& col)  ;
-  virtual void SetColour(unsigned char r, unsigned char g, unsigned char b)  ;
-  virtual void SetStyle(int style)  ;
-  virtual void SetStipple(const wxBitmap& stipple)  ;
+    wxColour GetColour() const;
+    int GetStyle() const;
+    wxBitmap *GetStipple() const;
 
-  inline wxBrush& operator = (const wxBrush& brush) { if (*this == brush) return (*this); Ref(brush); return *this; }
-  inline bool operator == (const wxBrush& brush) const { return m_refData == brush.m_refData; }
-  inline bool operator != (const wxBrush& brush) const { return m_refData != brush.m_refData; }
+    bool Ok() const { return m_refData != NULL; }
 
-  inline wxColour& GetColour(void) const { return (M_BRUSHDATA ? M_BRUSHDATA->m_colour : wxNullColour); };
-  inline int GetStyle(void) const { return (M_BRUSHDATA ? M_BRUSHDATA->m_style : 0); };
-  inline wxBitmap *GetStipple(void) const { return (M_BRUSHDATA ? & M_BRUSHDATA->m_stipple : 0); };
+    // create (if necessary) and return the HBRUSH for this brush
+    WXHBRUSH GetResourceHandle() const;
 
-  virtual bool Ok(void) const { return (m_refData != NULL) ; }
+protected:
+    virtual wxObjectRefData *CreateRefData() const;
+    virtual wxObjectRefData *CloneRefData(const wxObjectRefData *data) const;
 
-  // Internal
-  bool RealizeResource(void);
-  WXHANDLE GetResourceHandle(void) ;
-  bool FreeResource(bool force = FALSE);
-  bool IsFree() const;
-  void Unshare();
+private:
+    DECLARE_DYNAMIC_CLASS(wxBrush)
 };
 
 #endif
index 66206940e6eeb252df6dd25eb9ab459d4945eff5..c201a5574854d3ffa4be0b3f0e3e7c2f77ebffb1 100644 (file)
 /////////////////////////////////////////////////////////////////////////////
-// Name:        brush.cpp
+// Name:        src/msw/brush.cpp
 // Purpose:     wxBrush
 // Author:      Julian Smart
 // Modified by:
 // Created:     04/01/98
 // RCS-ID:      $Id$
 // Copyright:   (c) Julian Smart and Markus Holzem
-// Licence:    wxWindows license
+// Licence:     wxWindows license
 /////////////////////////////////////////////////////////////////////////////
 
+// ============================================================================
+// declarations
+// ============================================================================
+
 #ifdef __GNUG__
 #pragma implementation "brush.h"
 #endif
 
+// ----------------------------------------------------------------------------
+// headers
+// ----------------------------------------------------------------------------
+
 // For compilers that support precompilation, includes "wx.h".
 #include "wx/wxprec.h"
 
 #ifdef __BORLANDC__
-#pragma hdrstop
+    #pragma hdrstop
 #endif
 
 #ifndef WX_PRECOMP
-#include <stdio.h>
-#include "wx/setup.h"
-#include "wx/list.h"
-#include "wx/utils.h"
-#include "wx/app.h"
-#include "wx/brush.h"
-#endif
+    #include "wx/list.h"
+    #include "wx/utils.h"
+    #include "wx/app.h"
+    #include "wx/brush.h"
+#endif // WX_PRECOMP
 
 #include "wx/msw/private.h"
 
-#include "assert.h"
+// ----------------------------------------------------------------------------
+// private classes
+// ----------------------------------------------------------------------------
+
+class WXDLLEXPORT wxBrushRefData: public wxGDIRefData
+{
+public:
+    wxBrushRefData(const wxColour& colour = wxNullColour, int style = wxSOLID);
+    wxBrushRefData(const wxBitmap& stipple);
+    wxBrushRefData(const wxBrushRefData& data);
+    virtual ~wxBrushRefData();
+
+    bool operator==(const wxBrushRefData& data) const;
+
+    HBRUSH GetHBRUSH();
+    void Free();
+
+    const wxColour& GetColour() const { return m_colour; }
+    int GetStyle() const { return m_style; }
+    wxBitmap *GetStipple() { return &m_stipple; }
+
+    void SetColour(const wxColour& colour) { Free(); m_colour = colour; }
+    void SetStyle(int style) { Free(); m_style = style; }
+    void SetStipple(const wxBitmap& stipple) { Free(); DoSetStipple(stipple); }
+
+private:
+    void DoSetStipple(const wxBitmap& stipple);
+
+    int           m_style;
+    wxBitmap      m_stipple;
+    wxColour      m_colour;
+    HBRUSH        m_hBrush;
+
+    // no assignment operator, the objects of this class are shared and never
+    // assigned after being created once
+    wxBrushRefData& operator=(const wxBrushRefData&);
+};
+
+#define M_BRUSHDATA ((wxBrushRefData *)m_refData)
+
+// ============================================================================
+// wxBrushRefData implementation
+// ============================================================================
 
 IMPLEMENT_DYNAMIC_CLASS(wxBrush, wxGDIObject)
 
-wxBrushRefData::wxBrushRefData(void)
+// ----------------------------------------------------------------------------
+// wxBrushRefData ctors/dtor
+// ----------------------------------------------------------------------------
+
+wxBrushRefData::wxBrushRefData(const wxColour& colour, int style)
+              : m_colour(colour)
 {
-  m_style = wxSOLID;
-  m_hBrush = 0;
+    m_style = style;
+
+    m_hBrush = NULL;
 }
 
-wxBrushRefData::wxBrushRefData(const wxBrushRefData& data)
+wxBrushRefData::wxBrushRefData(const wxBitmap& stipple)
 {
-  m_style = data.m_style;
-  m_stipple = data.m_stipple;
-  m_colour = data.m_colour;
-  m_hBrush = 0;
+    DoSetStipple(stipple);
+
+    m_hBrush = NULL;
 }
 
-wxBrushRefData::~wxBrushRefData(void)
+wxBrushRefData::wxBrushRefData(const wxBrushRefData& data)
+              : m_stipple(data.m_stipple),
+                m_colour(data.m_colour)
 {
-       if ( m_hBrush )
-               ::DeleteObject((HBRUSH) m_hBrush);
+    m_style = data.m_style;
+
+    // we can't share HBRUSH, we'd need to create another one ourselves
+    m_hBrush = NULL;
 }
 
-// Brushes
-wxBrush::wxBrush(void)
+wxBrushRefData::~wxBrushRefData()
 {
+    Free();
 }
 
-wxBrush::~wxBrush()
+// ----------------------------------------------------------------------------
+// wxBrushRefData accesors
+// ----------------------------------------------------------------------------
+
+bool wxBrushRefData::operator==(const wxBrushRefData& data) const
 {
+    // don't compare HBRUSHes
+    return m_style == data.m_style &&
+           m_colour == data.m_colour &&
+           m_stipple == data.m_stipple;
 }
 
-wxBrush::wxBrush(const wxColour& col, int Style)
+void wxBrushRefData::DoSetStipple(const wxBitmap& stipple)
 {
-  m_refData = new wxBrushRefData;
+    m_stipple = stipple;
+    m_style = stipple.GetMask() ? wxSTIPPLE_MASK_OPAQUE : wxSTIPPLE;
+}
 
-  M_BRUSHDATA->m_colour = col;
-  M_BRUSHDATA->m_style = Style;
-  M_BRUSHDATA->m_hBrush = 0;
+// ----------------------------------------------------------------------------
+// wxBrushRefData resource handling
+// ----------------------------------------------------------------------------
 
-  RealizeResource();
+void wxBrushRefData::Free()
+{
+    if ( m_hBrush )
+    {
+        ::DeleteObject(m_hBrush);
 
+        m_hBrush = NULL;
+    }
 }
 
-wxBrush::wxBrush(const wxBitmap& stipple)
+static int TransllateHatchStyle(int style)
+{
+    switch ( style )
+    {
+#ifndef __WXMICROWIN__
+        case wxBDIAGONAL_HATCH: return HS_BDIAGONAL;
+        case wxCROSSDIAG_HATCH: return HS_DIAGCROSS;
+        case wxFDIAGONAL_HATCH: return HS_FDIAGONAL;
+        case wxCROSS_HATCH:     return HS_CROSS;
+        case wxHORIZONTAL_HATCH:return HS_HORIZONTAL;
+        case wxVERTICAL_HATCH:  return HS_VERTICAL;
+#endif // __WXMICROWIN__
+        default:                return -1;
+    }
+}
+
+HBRUSH wxBrushRefData::GetHBRUSH()
 {
-  m_refData = new wxBrushRefData;
+    if ( !m_hBrush )
+    {
+        int hatchStyle = TransllateHatchStyle(m_style);
+        if ( hatchStyle == -1 )
+        {
+            switch ( m_style )
+            {
+                case wxTRANSPARENT:
+                    m_hBrush = (HBRUSH)::GetStockObject(NULL_BRUSH);
+                    break;
+
+                case wxSTIPPLE:
+                    m_hBrush = ::CreatePatternBrush(GetHbitmapOf(m_stipple));
+                    break;
+
+                case wxSTIPPLE_MASK_OPAQUE:
+                    m_hBrush = ::CreatePatternBrush((HBITMAP)m_stipple.GetMask()
+                                                        ->GetMaskBitmap());
+                    break;
+
+                default:
+                    wxFAIL_MSG( _T("unknown brush style") );
+                    // fall through
+
+                case wxSOLID:
+                    m_hBrush = ::CreateSolidBrush(m_colour.GetPixel());
+                    break;
+            }
+        }
+        else // create a hatched brush
+        {
+            m_hBrush = ::CreateHatchBrush(hatchStyle, m_colour.GetPixel());
+        }
+
+        if ( !m_hBrush )
+        {
+            wxLogLastError(_T("CreateXXXBrush()"));
+        }
+    }
+
+    return m_hBrush;
+}
 
-  M_BRUSHDATA->m_stipple = stipple;
-  if (M_BRUSHDATA->m_stipple.GetMask())
-         M_BRUSHDATA->m_style = wxSTIPPLE_MASK_OPAQUE;
-  else
-         M_BRUSHDATA->m_style = wxSTIPPLE;
+// ============================================================================
+// wxBrush implementation
+// ============================================================================
 
-  M_BRUSHDATA->m_hBrush = 0;
+// ----------------------------------------------------------------------------
+// wxBrush ctors/dtor
+// ----------------------------------------------------------------------------
 
-  RealizeResource();
+wxBrush::wxBrush()
+{
+}
 
+wxBrush::wxBrush(const wxColour& col, int style)
+{
+    m_refData = new wxBrushRefData(col, style);
 }
 
-bool wxBrush::RealizeResource(void)
+wxBrush::wxBrush(const wxBitmap& stipple)
 {
-  if (M_BRUSHDATA && (M_BRUSHDATA->m_hBrush == 0))
-  {
-    if (M_BRUSHDATA->m_style==wxTRANSPARENT)
-    {
-      M_BRUSHDATA->m_hBrush = (WXHBRUSH) ::GetStockObject(NULL_BRUSH);
-      return TRUE;
-    }
-    COLORREF ms_colour = 0 ;
+    m_refData = new wxBrushRefData(stipple);
+}
 
-    ms_colour = M_BRUSHDATA->m_colour.GetPixel() ;
+wxBrush::~wxBrush()
+{
+}
 
-    switch (M_BRUSHDATA->m_style)
+// ----------------------------------------------------------------------------
+// wxBrush house keeping stuff
+// ----------------------------------------------------------------------------
+
+wxBrush& wxBrush::operator=(const wxBrush& brush)
+{
+    if ( *this != brush )
     {
-/****
-    // Don't reset cbrush, wxTRANSPARENT is handled by wxBrush::SelectBrush()
-    // this could save (many) time if frequently switching from
-    // wxSOLID to wxTRANSPARENT, because Create... is not always called!!
-    //
-    // NB August 95: now create and select a Null brush instead.
-    // This could be optimized as above.
-    case wxTRANSPARENT:
-      M_BRUSHDATA->m_hBrush = NULL;  // Must always select a suitable background brush
-                      // - could choose white always for a quick solution
-      break;
-***/
-#ifndef __WXMICROWIN__
-      case wxBDIAGONAL_HATCH:
-        M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_BDIAGONAL,ms_colour) ;
-        break ;
-      case wxCROSSDIAG_HATCH:
-        M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_DIAGCROSS,ms_colour) ;
-        break ;
-      case wxFDIAGONAL_HATCH:
-        M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_FDIAGONAL,ms_colour) ;
-        break ;
-      case wxCROSS_HATCH:
-        M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_CROSS,ms_colour) ;
-        break ;
-      case wxHORIZONTAL_HATCH:
-        M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_HORIZONTAL,ms_colour) ;
-        break ;
-      case wxVERTICAL_HATCH:
-        M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_VERTICAL,ms_colour) ;
-        break ;
-      case wxSTIPPLE:
-        if (M_BRUSHDATA->m_stipple.Ok())
-          M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreatePatternBrush((HBITMAP) M_BRUSHDATA->m_stipple.GetHBITMAP()) ;
-        else
-          M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateSolidBrush(ms_colour) ;
-        break ;
-      case wxSTIPPLE_MASK_OPAQUE:
-        if (M_BRUSHDATA->m_stipple.Ok() && M_BRUSHDATA->m_stipple.GetMask())
-          M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreatePatternBrush((HBITMAP) M_BRUSHDATA->m_stipple.GetMask()->GetMaskBitmap());
-        else
-          M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateSolidBrush(ms_colour) ;
-        break ;
-#endif
-      case wxSOLID:
-      default:
-        M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateSolidBrush(ms_colour) ;
-        break;
+        Ref(brush);
     }
-#ifdef WXDEBUG_CREATE
-    if (M_BRUSHDATA->m_hBrush==NULL) wxError("Cannot create brush","Internal error") ;
-#endif
-    return TRUE;
-  }
-  else
-    return FALSE;
-}
 
-WXHANDLE wxBrush::GetResourceHandle(void)
-{
-  return (WXHANDLE) M_BRUSHDATA->m_hBrush;
+    return *this;
 }
 
-bool wxBrush::FreeResource(bool WXUNUSED(force))
+bool wxBrush::operator==(const wxBrush& brush) const
 {
-  if (M_BRUSHDATA && (M_BRUSHDATA->m_hBrush != 0))
-  {
-    DeleteObject((HBRUSH) M_BRUSHDATA->m_hBrush);
-    M_BRUSHDATA->m_hBrush = 0;
-    return TRUE;
-  }
-  else return FALSE;
+    const wxBrushRefData *brushData = (wxBrushRefData *)brush.m_refData;
+
+    // an invalid brush is considered to be only equal to another invalid brush
+    return m_refData ? (brushData && *M_BRUSHDATA == *brushData) : !brushData;
 }
 
-bool wxBrush::IsFree() const
+wxObjectRefData *wxBrush::CreateRefData() const
 {
-  return (M_BRUSHDATA && (M_BRUSHDATA->m_hBrush == 0));
+    return new wxBrushRefData;
 }
 
-void wxBrush::Unshare()
+wxObjectRefData *wxBrush::CloneRefData(const wxObjectRefData *data) const
 {
-       // Don't change shared data
-       if (!m_refData)
-    {
-               m_refData = new wxBrushRefData();
-       }
-    else
-    {
-               wxBrushRefData* ref = new wxBrushRefData(*(wxBrushRefData*)m_refData);
-               UnRef();
-               m_refData = ref;
-       }
+    return new wxBrushRefData(*(const wxBrushRefData *)data);
 }
 
+// ----------------------------------------------------------------------------
+// wxBrush accessors
+// ----------------------------------------------------------------------------
 
-void wxBrush::SetColour(const wxColour& col)
+wxColour wxBrush::GetColour() const
 {
-    Unshare();
+    wxCHECK_MSG( Ok(), wxNullColour, _T("invalid brush") );
 
-    M_BRUSHDATA->m_colour = col;
+    return M_BRUSHDATA->GetColour();
+}
+
+int wxBrush::GetStyle() const
+{
+    wxCHECK_MSG( Ok(), 0, _T("invalid brush") );
 
-    RealizeResource();
+    return M_BRUSHDATA->GetStyle();
 }
 
-void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
+wxBitmap *wxBrush::GetStipple() const
 {
-    Unshare();
+    wxCHECK_MSG( Ok(), NULL, _T("invalid brush") );
+
+    return M_BRUSHDATA->GetStipple();
+}
 
-    M_BRUSHDATA->m_colour.Set(r, g, b);
+WXHBRUSH wxBrush::GetResourceHandle() const
+{
+    wxCHECK_MSG( Ok(), FALSE, _T("invalid brush") );
 
-    RealizeResource();
+    return (WXHBRUSH)M_BRUSHDATA->GetHBRUSH();
 }
 
-void wxBrush::SetStyle(int Style)
+// ----------------------------------------------------------------------------
+// wxBrush setters
+// ----------------------------------------------------------------------------
+
+void wxBrush::SetColour(const wxColour& col)
 {
-    Unshare();
+    AllocExclusive();
 
-    M_BRUSHDATA->m_style = Style;
+    M_BRUSHDATA->SetColour(col);
+}
 
-    RealizeResource();
+void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
+{
+    AllocExclusive();
+
+    M_BRUSHDATA->SetColour(wxColour(r, g, b));
 }
 
-void wxBrush::SetStipple(const wxBitmap& Stipple)
+void wxBrush::SetStyle(int style)
 {
-    Unshare();
+    AllocExclusive();
+
+    M_BRUSHDATA->SetStyle(style);
+}
 
-    M_BRUSHDATA->m_stipple = Stipple;
-    if (M_BRUSHDATA->m_stipple.GetMask())
-       M_BRUSHDATA->m_style = wxSTIPPLE_MASK_OPAQUE;
-    else
-       M_BRUSHDATA->m_style = wxSTIPPLE;
+void wxBrush::SetStipple(const wxBitmap& stipple)
+{
+    AllocExclusive();
 
-    RealizeResource();
+    M_BRUSHDATA->SetStipple(stipple);
 }
 
 
index f4aeebe02b04bc93e42b8494708019cc977e86dd..bfc738c185ff28c84610f0071570cd2474cb6792 100644 (file)
@@ -137,7 +137,8 @@ private:
 
 wxColourChanger::wxColourChanger(wxDC& dc) : m_dc(dc)
 {
-    if ( dc.GetBrush().GetStyle() == wxSTIPPLE_MASK_OPAQUE )
+    const wxBrush& brush = dc.GetBrush();
+    if ( brush.Ok() && brush.GetStyle() == wxSTIPPLE_MASK_OPAQUE )
     {
         HDC hdc = GetHdcOf(dc);
         m_colFgOld = ::GetTextColor(hdc);