| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/msw/brush.cpp |
| 3 | // Purpose: wxBrush |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 04/01/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart and Markus Holzem |
| 9 | // Licence: wxWindows license |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | #ifdef __GNUG__ |
| 17 | #pragma implementation "brush.h" |
| 18 | #endif |
| 19 | |
| 20 | // ---------------------------------------------------------------------------- |
| 21 | // headers |
| 22 | // ---------------------------------------------------------------------------- |
| 23 | |
| 24 | // For compilers that support precompilation, includes "wx.h". |
| 25 | #include "wx/wxprec.h" |
| 26 | |
| 27 | #ifdef __BORLANDC__ |
| 28 | #pragma hdrstop |
| 29 | #endif |
| 30 | |
| 31 | #ifndef WX_PRECOMP |
| 32 | #include "wx/list.h" |
| 33 | #include "wx/utils.h" |
| 34 | #include "wx/app.h" |
| 35 | #include "wx/brush.h" |
| 36 | #endif // WX_PRECOMP |
| 37 | |
| 38 | #include "wx/msw/private.h" |
| 39 | |
| 40 | // ---------------------------------------------------------------------------- |
| 41 | // private classes |
| 42 | // ---------------------------------------------------------------------------- |
| 43 | |
| 44 | class WXDLLEXPORT wxBrushRefData: public wxGDIRefData |
| 45 | { |
| 46 | public: |
| 47 | wxBrushRefData(const wxColour& colour = wxNullColour, int style = wxSOLID); |
| 48 | wxBrushRefData(const wxBitmap& stipple); |
| 49 | wxBrushRefData(const wxBrushRefData& data); |
| 50 | virtual ~wxBrushRefData(); |
| 51 | |
| 52 | bool operator==(const wxBrushRefData& data) const; |
| 53 | |
| 54 | HBRUSH GetHBRUSH(); |
| 55 | void Free(); |
| 56 | |
| 57 | const wxColour& GetColour() const { return m_colour; } |
| 58 | int GetStyle() const { return m_style; } |
| 59 | wxBitmap *GetStipple() { return &m_stipple; } |
| 60 | |
| 61 | void SetColour(const wxColour& colour) { Free(); m_colour = colour; } |
| 62 | void SetStyle(int style) { Free(); m_style = style; } |
| 63 | void SetStipple(const wxBitmap& stipple) { Free(); DoSetStipple(stipple); } |
| 64 | |
| 65 | private: |
| 66 | void DoSetStipple(const wxBitmap& stipple); |
| 67 | |
| 68 | int m_style; |
| 69 | wxBitmap m_stipple; |
| 70 | wxColour m_colour; |
| 71 | HBRUSH m_hBrush; |
| 72 | |
| 73 | // no assignment operator, the objects of this class are shared and never |
| 74 | // assigned after being created once |
| 75 | wxBrushRefData& operator=(const wxBrushRefData&); |
| 76 | }; |
| 77 | |
| 78 | #define M_BRUSHDATA ((wxBrushRefData *)m_refData) |
| 79 | |
| 80 | // ============================================================================ |
| 81 | // wxBrushRefData implementation |
| 82 | // ============================================================================ |
| 83 | |
| 84 | IMPLEMENT_DYNAMIC_CLASS(wxBrush, wxGDIObject) |
| 85 | |
| 86 | // ---------------------------------------------------------------------------- |
| 87 | // wxBrushRefData ctors/dtor |
| 88 | // ---------------------------------------------------------------------------- |
| 89 | |
| 90 | wxBrushRefData::wxBrushRefData(const wxColour& colour, int style) |
| 91 | : m_colour(colour) |
| 92 | { |
| 93 | m_style = style; |
| 94 | |
| 95 | m_hBrush = NULL; |
| 96 | } |
| 97 | |
| 98 | wxBrushRefData::wxBrushRefData(const wxBitmap& stipple) |
| 99 | { |
| 100 | DoSetStipple(stipple); |
| 101 | |
| 102 | m_hBrush = NULL; |
| 103 | } |
| 104 | |
| 105 | wxBrushRefData::wxBrushRefData(const wxBrushRefData& data) |
| 106 | : m_stipple(data.m_stipple), |
| 107 | m_colour(data.m_colour) |
| 108 | { |
| 109 | m_style = data.m_style; |
| 110 | |
| 111 | // we can't share HBRUSH, we'd need to create another one ourselves |
| 112 | m_hBrush = NULL; |
| 113 | } |
| 114 | |
| 115 | wxBrushRefData::~wxBrushRefData() |
| 116 | { |
| 117 | Free(); |
| 118 | } |
| 119 | |
| 120 | // ---------------------------------------------------------------------------- |
| 121 | // wxBrushRefData accesors |
| 122 | // ---------------------------------------------------------------------------- |
| 123 | |
| 124 | bool wxBrushRefData::operator==(const wxBrushRefData& data) const |
| 125 | { |
| 126 | // don't compare HBRUSHes |
| 127 | return m_style == data.m_style && |
| 128 | m_colour == data.m_colour && |
| 129 | m_stipple == data.m_stipple; |
| 130 | } |
| 131 | |
| 132 | void wxBrushRefData::DoSetStipple(const wxBitmap& stipple) |
| 133 | { |
| 134 | m_stipple = stipple; |
| 135 | m_style = stipple.GetMask() ? wxSTIPPLE_MASK_OPAQUE : wxSTIPPLE; |
| 136 | } |
| 137 | |
| 138 | // ---------------------------------------------------------------------------- |
| 139 | // wxBrushRefData resource handling |
| 140 | // ---------------------------------------------------------------------------- |
| 141 | |
| 142 | void wxBrushRefData::Free() |
| 143 | { |
| 144 | if ( m_hBrush ) |
| 145 | { |
| 146 | ::DeleteObject(m_hBrush); |
| 147 | |
| 148 | m_hBrush = NULL; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | static int TransllateHatchStyle(int style) |
| 153 | { |
| 154 | switch ( style ) |
| 155 | { |
| 156 | #ifndef __WXMICROWIN__ |
| 157 | case wxBDIAGONAL_HATCH: return HS_BDIAGONAL; |
| 158 | case wxCROSSDIAG_HATCH: return HS_DIAGCROSS; |
| 159 | case wxFDIAGONAL_HATCH: return HS_FDIAGONAL; |
| 160 | case wxCROSS_HATCH: return HS_CROSS; |
| 161 | case wxHORIZONTAL_HATCH:return HS_HORIZONTAL; |
| 162 | case wxVERTICAL_HATCH: return HS_VERTICAL; |
| 163 | #endif // __WXMICROWIN__ |
| 164 | default: return -1; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | HBRUSH wxBrushRefData::GetHBRUSH() |
| 169 | { |
| 170 | if ( !m_hBrush ) |
| 171 | { |
| 172 | int hatchStyle = TransllateHatchStyle(m_style); |
| 173 | if ( hatchStyle == -1 ) |
| 174 | { |
| 175 | switch ( m_style ) |
| 176 | { |
| 177 | case wxTRANSPARENT: |
| 178 | m_hBrush = (HBRUSH)::GetStockObject(NULL_BRUSH); |
| 179 | break; |
| 180 | |
| 181 | case wxSTIPPLE: |
| 182 | m_hBrush = ::CreatePatternBrush(GetHbitmapOf(m_stipple)); |
| 183 | break; |
| 184 | |
| 185 | case wxSTIPPLE_MASK_OPAQUE: |
| 186 | m_hBrush = ::CreatePatternBrush((HBITMAP)m_stipple.GetMask() |
| 187 | ->GetMaskBitmap()); |
| 188 | break; |
| 189 | |
| 190 | default: |
| 191 | wxFAIL_MSG( _T("unknown brush style") ); |
| 192 | // fall through |
| 193 | |
| 194 | case wxSOLID: |
| 195 | m_hBrush = ::CreateSolidBrush(m_colour.GetPixel()); |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | else // create a hatched brush |
| 200 | { |
| 201 | m_hBrush = ::CreateHatchBrush(hatchStyle, m_colour.GetPixel()); |
| 202 | } |
| 203 | |
| 204 | if ( !m_hBrush ) |
| 205 | { |
| 206 | wxLogLastError(_T("CreateXXXBrush()")); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return m_hBrush; |
| 211 | } |
| 212 | |
| 213 | // ============================================================================ |
| 214 | // wxBrush implementation |
| 215 | // ============================================================================ |
| 216 | |
| 217 | // ---------------------------------------------------------------------------- |
| 218 | // wxBrush ctors/dtor |
| 219 | // ---------------------------------------------------------------------------- |
| 220 | |
| 221 | wxBrush::wxBrush() |
| 222 | { |
| 223 | } |
| 224 | |
| 225 | wxBrush::wxBrush(const wxColour& col, int style) |
| 226 | { |
| 227 | m_refData = new wxBrushRefData(col, style); |
| 228 | } |
| 229 | |
| 230 | wxBrush::wxBrush(const wxBitmap& stipple) |
| 231 | { |
| 232 | m_refData = new wxBrushRefData(stipple); |
| 233 | } |
| 234 | |
| 235 | wxBrush::~wxBrush() |
| 236 | { |
| 237 | } |
| 238 | |
| 239 | // ---------------------------------------------------------------------------- |
| 240 | // wxBrush house keeping stuff |
| 241 | // ---------------------------------------------------------------------------- |
| 242 | |
| 243 | wxBrush& wxBrush::operator=(const wxBrush& brush) |
| 244 | { |
| 245 | if ( *this != brush ) |
| 246 | { |
| 247 | Ref(brush); |
| 248 | } |
| 249 | |
| 250 | return *this; |
| 251 | } |
| 252 | |
| 253 | bool wxBrush::operator==(const wxBrush& brush) const |
| 254 | { |
| 255 | const wxBrushRefData *brushData = (wxBrushRefData *)brush.m_refData; |
| 256 | |
| 257 | // an invalid brush is considered to be only equal to another invalid brush |
| 258 | return m_refData ? (brushData && *M_BRUSHDATA == *brushData) : !brushData; |
| 259 | } |
| 260 | |
| 261 | wxObjectRefData *wxBrush::CreateRefData() const |
| 262 | { |
| 263 | return new wxBrushRefData; |
| 264 | } |
| 265 | |
| 266 | wxObjectRefData *wxBrush::CloneRefData(const wxObjectRefData *data) const |
| 267 | { |
| 268 | return new wxBrushRefData(*(const wxBrushRefData *)data); |
| 269 | } |
| 270 | |
| 271 | // ---------------------------------------------------------------------------- |
| 272 | // wxBrush accessors |
| 273 | // ---------------------------------------------------------------------------- |
| 274 | |
| 275 | wxColour wxBrush::GetColour() const |
| 276 | { |
| 277 | wxCHECK_MSG( Ok(), wxNullColour, _T("invalid brush") ); |
| 278 | |
| 279 | return M_BRUSHDATA->GetColour(); |
| 280 | } |
| 281 | |
| 282 | int wxBrush::GetStyle() const |
| 283 | { |
| 284 | wxCHECK_MSG( Ok(), 0, _T("invalid brush") ); |
| 285 | |
| 286 | return M_BRUSHDATA->GetStyle(); |
| 287 | } |
| 288 | |
| 289 | wxBitmap *wxBrush::GetStipple() const |
| 290 | { |
| 291 | wxCHECK_MSG( Ok(), NULL, _T("invalid brush") ); |
| 292 | |
| 293 | return M_BRUSHDATA->GetStipple(); |
| 294 | } |
| 295 | |
| 296 | WXHBRUSH wxBrush::GetResourceHandle() const |
| 297 | { |
| 298 | wxCHECK_MSG( Ok(), FALSE, _T("invalid brush") ); |
| 299 | |
| 300 | return (WXHBRUSH)M_BRUSHDATA->GetHBRUSH(); |
| 301 | } |
| 302 | |
| 303 | // ---------------------------------------------------------------------------- |
| 304 | // wxBrush setters |
| 305 | // ---------------------------------------------------------------------------- |
| 306 | |
| 307 | void wxBrush::SetColour(const wxColour& col) |
| 308 | { |
| 309 | AllocExclusive(); |
| 310 | |
| 311 | M_BRUSHDATA->SetColour(col); |
| 312 | } |
| 313 | |
| 314 | void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b) |
| 315 | { |
| 316 | AllocExclusive(); |
| 317 | |
| 318 | M_BRUSHDATA->SetColour(wxColour(r, g, b)); |
| 319 | } |
| 320 | |
| 321 | void wxBrush::SetStyle(int style) |
| 322 | { |
| 323 | AllocExclusive(); |
| 324 | |
| 325 | M_BRUSHDATA->SetStyle(style); |
| 326 | } |
| 327 | |
| 328 | void wxBrush::SetStipple(const wxBitmap& stipple) |
| 329 | { |
| 330 | AllocExclusive(); |
| 331 | |
| 332 | M_BRUSHDATA->SetStipple(stipple); |
| 333 | } |
| 334 | |
| 335 | |