X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b2aef89b7a675805b601e757492c982031f38d21..43c42c18d36c703a88b1b7b697bac27fe5608eca:/src/msw/brush.cpp diff --git a/src/msw/brush.cpp b/src/msw/brush.cpp index f9043c2e1d..2d266918b7 100644 --- a/src/msw/brush.cpp +++ b/src/msw/brush.cpp @@ -1,263 +1,336 @@ ///////////////////////////////////////////////////////////////////////////// -// 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 +// Copyright: (c) Julian Smart +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#ifdef __GNUG__ -#pragma implementation "brush.h" -#endif +// ============================================================================ +// declarations +// ============================================================================ + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" #ifdef __BORLANDC__ -#pragma hdrstop + #pragma hdrstop #endif -#ifndef WX_PRECOMP -#include -#include "wx/setup.h" -#include "wx/list.h" -#include "wx/utils.h" -#include "wx/app.h" #include "wx/brush.h" -#endif + +#ifndef WX_PRECOMP + #include "wx/list.h" + #include "wx/utils.h" + #include "wx/app.h" + #include "wx/bitmap.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, wxBrushStyle style = wxBRUSHSTYLE_SOLID); + 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; } + wxBrushStyle GetStyle() const { return m_style; } + wxBitmap *GetStipple() { return &m_stipple; } + + void SetColour(const wxColour& colour) { Free(); m_colour = colour; } + void SetStyle(wxBrushStyle style) { Free(); m_style = style; } + void SetStipple(const wxBitmap& stipple) { Free(); DoSetStipple(stipple); } + +private: + void DoSetStipple(const wxBitmap& stipple); + + wxBrushStyle 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 +// ============================================================================ -#if !USE_SHARED_LIBRARIES IMPLEMENT_DYNAMIC_CLASS(wxBrush, wxGDIObject) -#endif -wxBrushRefData::wxBrushRefData(void) -{ - m_style = wxSOLID; -// m_stipple = NULL ; - m_hBrush = 0; -} +// ---------------------------------------------------------------------------- +// wxBrushRefData ctors/dtor +// ---------------------------------------------------------------------------- -wxBrushRefData::~wxBrushRefData(void) +wxBrushRefData::wxBrushRefData(const wxColour& colour, wxBrushStyle style) + : m_colour(colour) { - if ( m_hBrush ) - ::DeleteObject((HBRUSH) m_hBrush); -} + m_style = style; -// Brushes -wxBrush::wxBrush(void) -{ - if ( wxTheBrushList ) - wxTheBrushList->AddBrush(this); + m_hBrush = NULL; } -wxBrush::~wxBrush() +wxBrushRefData::wxBrushRefData(const wxBitmap& stipple) { - if (wxTheBrushList) - wxTheBrushList->RemoveBrush(this); + DoSetStipple(stipple); + + m_hBrush = NULL; } -wxBrush::wxBrush(const wxColour& col, int Style) +wxBrushRefData::wxBrushRefData(const wxBrushRefData& data) + : wxGDIRefData(), + m_stipple(data.m_stipple), + m_colour(data.m_colour) { - m_refData = new wxBrushRefData; - - M_BRUSHDATA->m_colour = col; - M_BRUSHDATA->m_style = Style; - M_BRUSHDATA->m_hBrush = 0; + m_style = data.m_style; - RealizeResource(); - - if ( wxTheBrushList ) - wxTheBrushList->AddBrush(this); + // we can't share HBRUSH, we'd need to create another one ourselves + m_hBrush = NULL; } -wxBrush::wxBrush(const wxString& col, int Style) +wxBrushRefData::~wxBrushRefData() { - m_refData = new wxBrushRefData; - - M_BRUSHDATA->m_colour = col; - M_BRUSHDATA->m_style = Style; - M_BRUSHDATA->m_hBrush = 0; + Free(); +} - RealizeResource(); +// ---------------------------------------------------------------------------- +// wxBrushRefData accesors +// ---------------------------------------------------------------------------- - if ( wxTheBrushList ) - wxTheBrushList->AddBrush(this); +bool wxBrushRefData::operator==(const wxBrushRefData& data) const +{ + // don't compare HBRUSHes + return m_style == data.m_style && + m_colour == data.m_colour && + m_stipple.IsSameAs(data.m_stipple); } -wxBrush::wxBrush(const wxBitmap& stipple) +void wxBrushRefData::DoSetStipple(const wxBitmap& stipple) { - m_refData = new wxBrushRefData; + m_stipple = stipple; + m_style = stipple.GetMask() ? wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE + : wxBRUSHSTYLE_STIPPLE; +} - M_BRUSHDATA->m_style = wxSTIPPLE; - M_BRUSHDATA->m_stipple = stipple; - M_BRUSHDATA->m_hBrush = 0; +// ---------------------------------------------------------------------------- +// wxBrushRefData resource handling +// ---------------------------------------------------------------------------- - RealizeResource(); +void wxBrushRefData::Free() +{ + if ( m_hBrush ) + { + ::DeleteObject(m_hBrush); - if ( wxTheBrushList ) - wxTheBrushList->AddBrush(this); + m_hBrush = NULL; + } } -bool wxBrush::RealizeResource(void) +#if !defined(__WXMICROWIN__) && !defined(__WXWINCE__) + +static int TranslateHatchStyle(int style) { - if (M_BRUSHDATA && (M_BRUSHDATA->m_hBrush == 0)) - { - if (M_BRUSHDATA->m_style==wxTRANSPARENT) + switch ( style ) { - M_BRUSHDATA->m_hBrush = (WXHBRUSH) ::GetStockObject(NULL_BRUSH); - return TRUE; + case wxBRUSHSTYLE_BDIAGONAL_HATCH: return HS_BDIAGONAL; + case wxBRUSHSTYLE_CROSSDIAG_HATCH: return HS_DIAGCROSS; + case wxBRUSHSTYLE_FDIAGONAL_HATCH: return HS_FDIAGONAL; + case wxBRUSHSTYLE_CROSS_HATCH: return HS_CROSS; + case wxBRUSHSTYLE_HORIZONTAL_HATCH:return HS_HORIZONTAL; + case wxBRUSHSTYLE_VERTICAL_HATCH: return HS_VERTICAL; + default: return -1; } - COLORREF ms_colour = 0 ; +} - ms_colour = M_BRUSHDATA->m_colour.GetPixel() ; +#endif // !__WXMICROWIN__ && !__WXWINCE__ - switch (M_BRUSHDATA->m_style) +HBRUSH wxBrushRefData::GetHBRUSH() +{ + if ( !m_hBrush ) { -/**** - // 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; -***/ - 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 wxSOLID: - default: - M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateSolidBrush(ms_colour) ; - break; - } -#ifdef WXDEBUG_CREATE - if (M_BRUSHDATA->m_hBrush==NULL) wxError("Cannot create brush","Internal error") ; +#if !defined(__WXMICROWIN__) && !defined(__WXWINCE__) + int hatchStyle = TranslateHatchStyle(m_style); + if ( hatchStyle == -1 ) +#endif // !__WXMICROWIN__ && !__WXWINCE__ + { + switch ( m_style ) + { + case wxBRUSHSTYLE_TRANSPARENT: + m_hBrush = (HBRUSH)::GetStockObject(NULL_BRUSH); + break; + + case wxBRUSHSTYLE_STIPPLE: + m_hBrush = ::CreatePatternBrush(GetHbitmapOf(m_stipple)); + break; + + case wxBRUSHSTYLE_STIPPLE_MASK_OPAQUE: + m_hBrush = ::CreatePatternBrush((HBITMAP)m_stipple.GetMask() + ->GetMaskBitmap()); + break; + + default: + wxFAIL_MSG( wxT("unknown brush style") ); + // fall through + + case wxBRUSHSTYLE_SOLID: + m_hBrush = ::CreateSolidBrush(m_colour.GetPixel()); + break; + } + } +#ifndef __WXWINCE__ + else // create a hatched brush + { + m_hBrush = ::CreateHatchBrush(hatchStyle, m_colour.GetPixel()); + } #endif - return TRUE; - } - else - return FALSE; + + if ( !m_hBrush ) + { + wxLogLastError(wxT("CreateXXXBrush()")); + } + } + + return m_hBrush; } -WXHANDLE wxBrush::GetResourceHandle(void) +// ============================================================================ +// wxBrush implementation +// ============================================================================ + +// ---------------------------------------------------------------------------- +// wxBrush ctors/dtor +// ---------------------------------------------------------------------------- + +wxBrush::wxBrush() { - return (WXHANDLE) M_BRUSHDATA->m_hBrush; } -bool wxBrush::FreeResource(bool force) +wxBrush::wxBrush(const wxColour& col, wxBrushStyle style) { - if (M_BRUSHDATA && (M_BRUSHDATA->m_hBrush != 0)) - { - DeleteObject((HBRUSH) M_BRUSHDATA->m_hBrush); - M_BRUSHDATA->m_hBrush = 0; - return TRUE; - } - else return FALSE; + m_refData = new wxBrushRefData(col, style); } -/* -bool wxBrush::UseResource(void) +#if FUTURE_WXWIN_COMPATIBILITY_3_0 +wxBrush::wxBrush(const wxColour& col, int style) { - IncrementResourceUsage(); - return TRUE; + m_refData = new wxBrushRefData(col, (wxBrushStyle)style); } +#endif -bool wxBrush::ReleaseResource(void) +wxBrush::wxBrush(const wxBitmap& stipple) { - DecrementResourceUsage(); - return TRUE; + m_refData = new wxBrushRefData(stipple); } -*/ -bool wxBrush::IsFree(void) +wxBrush::~wxBrush() { - return (M_BRUSHDATA && (M_BRUSHDATA->m_hBrush == 0)); } -void wxBrush::SetColour(const wxColour& col) +// ---------------------------------------------------------------------------- +// wxBrush house keeping stuff +// ---------------------------------------------------------------------------- + +bool wxBrush::operator==(const wxBrush& brush) const { - if ( !M_BRUSHDATA ) - m_refData = new wxBrushRefData; + const wxBrushRefData *brushData = (wxBrushRefData *)brush.m_refData; - M_BRUSHDATA->m_colour = col; + // an invalid brush is considered to be only equal to another invalid brush + return m_refData ? (brushData && *M_BRUSHDATA == *brushData) : !brushData; +} - if (FreeResource()) - RealizeResource(); +wxGDIRefData *wxBrush::CreateGDIRefData() const +{ + return new wxBrushRefData; } -void wxBrush::SetColour(const wxString& col) +wxGDIRefData *wxBrush::CloneGDIRefData(const wxGDIRefData *data) const { - if ( !M_BRUSHDATA ) - m_refData = new wxBrushRefData; + return new wxBrushRefData(*(const wxBrushRefData *)data); +} + +// ---------------------------------------------------------------------------- +// wxBrush accessors +// ---------------------------------------------------------------------------- - M_BRUSHDATA->m_colour = col; +wxColour wxBrush::GetColour() const +{ + wxCHECK_MSG( IsOk(), wxNullColour, wxT("invalid brush") ); - if (FreeResource()) - RealizeResource(); + return M_BRUSHDATA->GetColour(); } -void wxBrush::SetColour(const unsigned char r, const unsigned char g, const unsigned char b) +wxBrushStyle wxBrush::GetStyle() const { - if ( !M_BRUSHDATA ) - m_refData = new wxBrushRefData; + wxCHECK_MSG( IsOk(), wxBRUSHSTYLE_INVALID, wxT("invalid brush") ); + + return M_BRUSHDATA->GetStyle(); +} - M_BRUSHDATA->m_colour.Set(r, g, b); +wxBitmap *wxBrush::GetStipple() const +{ + wxCHECK_MSG( IsOk(), NULL, wxT("invalid brush") ); - if (FreeResource()) - RealizeResource(); + return M_BRUSHDATA->GetStipple(); } -void wxBrush::SetStyle(int Style) +WXHANDLE wxBrush::GetResourceHandle() const { - if ( !M_BRUSHDATA ) - m_refData = new wxBrushRefData; + wxCHECK_MSG( IsOk(), FALSE, wxT("invalid brush") ); + + return (WXHANDLE)M_BRUSHDATA->GetHBRUSH(); +} - M_BRUSHDATA->m_style = Style; +// ---------------------------------------------------------------------------- +// wxBrush setters +// ---------------------------------------------------------------------------- - if (FreeResource()) - RealizeResource(); +void wxBrush::SetColour(const wxColour& col) +{ + AllocExclusive(); + + M_BRUSHDATA->SetColour(col); } -void wxBrush::SetStipple(const wxBitmap& Stipple) +void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b) { - if ( !M_BRUSHDATA ) - m_refData = new wxBrushRefData; + AllocExclusive(); + + M_BRUSHDATA->SetColour(wxColour(r, g, b)); +} - M_BRUSHDATA->m_stipple = Stipple; +void wxBrush::SetStyle(wxBrushStyle style) +{ + AllocExclusive(); - if (FreeResource()) - RealizeResource(); + M_BRUSHDATA->SetStyle(style); } +void wxBrush::SetStipple(const wxBitmap& stipple) +{ + AllocExclusive(); + M_BRUSHDATA->SetStipple(stipple); +}