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