| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/mgl/brush.cpp |
| 3 | // Purpose: |
| 4 | // Author: Vaclav Slavik |
| 5 | // Id: $Id$ |
| 6 | // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com) |
| 7 | // Licence: wxWindows licence |
| 8 | ///////////////////////////////////////////////////////////////////////////// |
| 9 | |
| 10 | // For compilers that support precompilation, includes "wx.h". |
| 11 | #include "wx/wxprec.h" |
| 12 | |
| 13 | #ifdef __BORLANDC__ |
| 14 | #pragma hdrstop |
| 15 | #endif |
| 16 | |
| 17 | #include "wx/brush.h" |
| 18 | #include "wx/mgl/private.h" |
| 19 | #include "wx/dcmemory.h" |
| 20 | |
| 21 | |
| 22 | // --------------------------------------------------------------------------- |
| 23 | // helper functions |
| 24 | // --------------------------------------------------------------------------- |
| 25 | |
| 26 | // This function converts wxBitmap into pixpattern24_t representation |
| 27 | // (used by wxBrush and wxPen) |
| 28 | |
| 29 | void wxBitmapToPixPattern(const wxBitmap& bitmap, |
| 30 | pixpattern24_t *pix, pattern_t *mask) |
| 31 | { |
| 32 | wxMemoryDC mem; |
| 33 | MGLDevCtx *dc; |
| 34 | int x, y; |
| 35 | |
| 36 | if ( pix != NULL ) |
| 37 | { |
| 38 | mem.SelectObjectAsSource(bitmap); |
| 39 | dc = mem.GetMGLDC(); |
| 40 | wxCurrentDCSwitcher curDC(dc); |
| 41 | dc->beginPixel(); |
| 42 | for (y = 0; y < 8; y++) |
| 43 | for (x = 0; x < 8; x++) |
| 44 | dc->unpackColorFast(dc->getPixelFast(x, y), |
| 45 | pix->p[y][x][2], |
| 46 | pix->p[y][x][1], |
| 47 | pix->p[y][x][0]); |
| 48 | dc->endPixel(); |
| 49 | } |
| 50 | |
| 51 | if ( mask && bitmap.GetMask() ) |
| 52 | { |
| 53 | mem.SelectObjectAsSource(bitmap.GetMask()->GetBitmap()); |
| 54 | dc = mem.GetMGLDC(); |
| 55 | wxCurrentDCSwitcher curDC(dc); |
| 56 | dc->beginPixel(); |
| 57 | for (y = 0; y < 8; y++) |
| 58 | { |
| 59 | mask->p[y] = 0; |
| 60 | for (x = 0; x < 8; x++) |
| 61 | if ( dc->getPixelFast(x, y) != 0 ) |
| 62 | mask->p[y] = (uchar)(mask->p[y] | (1 << (7 - x))); |
| 63 | } |
| 64 | dc->endPixel(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | |
| 69 | //----------------------------------------------------------------------------- |
| 70 | // wxBrush |
| 71 | //----------------------------------------------------------------------------- |
| 72 | |
| 73 | class wxBrushRefData : public wxGDIRefData |
| 74 | { |
| 75 | public: |
| 76 | wxBrushRefData(); |
| 77 | wxBrushRefData(const wxBrushRefData& data); |
| 78 | |
| 79 | virtual bool IsOk() const { return m_colour.IsOk(); } |
| 80 | |
| 81 | bool operator==(const wxBrushRefData& data) const |
| 82 | { |
| 83 | return (m_style == data.m_style && |
| 84 | m_stipple.IsSameAs(data.m_stipple) && |
| 85 | m_colour == data.m_colour); |
| 86 | } |
| 87 | |
| 88 | wxBrushStyle m_style; |
| 89 | wxColour m_colour; |
| 90 | wxBitmap m_stipple; |
| 91 | pixpattern24_t m_pixPattern; |
| 92 | pattern_t m_maskPattern; |
| 93 | }; |
| 94 | |
| 95 | wxBrushRefData::wxBrushRefData() |
| 96 | { |
| 97 | m_style = 0; |
| 98 | |
| 99 | int x, y, c; |
| 100 | for (y = 0; y < 8; y++) |
| 101 | for (x = 0; x < 8; x++) |
| 102 | for (c = 0; c < 3; c++) |
| 103 | m_pixPattern.p[y][x][c] = 0; |
| 104 | for (y = 0; y < 8; y++) |
| 105 | m_maskPattern.p[y] = 0; |
| 106 | } |
| 107 | |
| 108 | wxBrushRefData::wxBrushRefData(const wxBrushRefData& data) |
| 109 | { |
| 110 | m_style = data.m_style; |
| 111 | m_stipple = data.m_stipple; |
| 112 | m_colour = data.m_colour; |
| 113 | |
| 114 | int x, y, c; |
| 115 | for (y = 0; y < 8; y++) |
| 116 | for (x = 0; x < 8; x++) |
| 117 | for (c = 0; c < 3; c++) |
| 118 | m_pixPattern.p[y][x][c] = data.m_pixPattern.p[y][x][c]; |
| 119 | for (y = 0; y < 8; y++) |
| 120 | m_maskPattern.p[y] = data.m_maskPattern.p[y]; |
| 121 | } |
| 122 | |
| 123 | //----------------------------------------------------------------------------- |
| 124 | |
| 125 | #define M_BRUSHDATA ((wxBrushRefData *)m_refData) |
| 126 | |
| 127 | IMPLEMENT_DYNAMIC_CLASS(wxBrush,wxGDIObject) |
| 128 | |
| 129 | wxBrush::wxBrush(const wxColour &colour, wxBrushStyle style) |
| 130 | { |
| 131 | m_refData = new wxBrushRefData(); |
| 132 | M_BRUSHDATA->m_style = style; |
| 133 | M_BRUSHDATA->m_colour = colour; |
| 134 | } |
| 135 | |
| 136 | #if FUTURE_WXWIN_COMPATIBILITY_3_0 |
| 137 | wxBrush::wxBrush(const wxColour& col, int style) |
| 138 | { |
| 139 | m_refData = new wxBrushRefData; |
| 140 | M_BRUSHDATA->m_style = (wxBrushStyle)style; |
| 141 | M_BRUSHDATA->m_colour = colour; |
| 142 | } |
| 143 | #endif |
| 144 | |
| 145 | wxBrush::wxBrush(const wxBitmap &stippleBitmap) |
| 146 | { |
| 147 | wxCHECK_RET( stippleBitmap.IsOk(), wxT("invalid bitmap") ); |
| 148 | wxCHECK_RET( stippleBitmap.GetWidth() == 8 && stippleBitmap.GetHeight() == 8, |
| 149 | wxT("stipple bitmap must be 8x8") ); |
| 150 | |
| 151 | m_refData = new wxBrushRefData(); |
| 152 | M_BRUSHDATA->m_colour = *wxBLACK; |
| 153 | |
| 154 | M_BRUSHDATA->m_stipple = stippleBitmap; |
| 155 | wxBitmapToPixPattern(stippleBitmap, &(M_BRUSHDATA->m_pixPattern), |
| 156 | &(M_BRUSHDATA->m_maskPattern)); |
| 157 | |
| 158 | if (M_BRUSHDATA->m_stipple.GetMask()) |
| 159 | M_BRUSHDATA->m_style = wxSTIPPLE_MASK_OPAQUE; |
| 160 | else |
| 161 | M_BRUSHDATA->m_style = wxSTIPPLE; |
| 162 | } |
| 163 | |
| 164 | bool wxBrush::operator == (const wxBrush& brush) const |
| 165 | { |
| 166 | if (m_refData == brush.m_refData) return true; |
| 167 | |
| 168 | if (!m_refData || !brush.m_refData) return false; |
| 169 | |
| 170 | return *(wxBrushRefData*)m_refData == *(wxBrushRefData*)brush.m_refData; |
| 171 | } |
| 172 | |
| 173 | bool wxBrush::operator != (const wxBrush& brush) const |
| 174 | { |
| 175 | return m_refData != brush.m_refData; |
| 176 | } |
| 177 | |
| 178 | wxBrushStyle wxBrush::GetStyle() const |
| 179 | { |
| 180 | wxCHECK_MSG( IsOk(), wxBRUSHSTYLE_INVALID, wxT("invalid brush") ); |
| 181 | |
| 182 | return M_BRUSHDATA->m_style; |
| 183 | } |
| 184 | |
| 185 | wxColour wxBrush::GetColour() const |
| 186 | { |
| 187 | wxCHECK_MSG( IsOk(), wxNullColour, wxT("invalid brush") ); |
| 188 | |
| 189 | return M_BRUSHDATA->m_colour; |
| 190 | } |
| 191 | |
| 192 | wxBitmap *wxBrush::GetStipple() const |
| 193 | { |
| 194 | wxCHECK_MSG( IsOk(), NULL, wxT("invalid brush") ); |
| 195 | |
| 196 | return &M_BRUSHDATA->m_stipple; |
| 197 | } |
| 198 | |
| 199 | void* wxBrush::GetMaskPattern() const |
| 200 | { |
| 201 | wxCHECK_MSG( IsOk(), NULL, wxT("invalid brush") ); |
| 202 | |
| 203 | return (void*)&(M_BRUSHDATA->m_maskPattern); |
| 204 | } |
| 205 | |
| 206 | void* wxBrush::GetPixPattern() const |
| 207 | { |
| 208 | wxCHECK_MSG( IsOk(), NULL, wxT("invalid brush") ); |
| 209 | |
| 210 | return (void*)&(M_BRUSHDATA->m_pixPattern); |
| 211 | } |
| 212 | |
| 213 | void wxBrush::SetColour(const wxColour& col) |
| 214 | { |
| 215 | AllocExclusive(); |
| 216 | M_BRUSHDATA->m_colour = col; |
| 217 | } |
| 218 | |
| 219 | void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b) |
| 220 | { |
| 221 | AllocExclusive(); |
| 222 | M_BRUSHDATA->m_colour.Set(r, g, b); |
| 223 | } |
| 224 | |
| 225 | void wxBrush::SetStyle( wxBrushStyle style ) |
| 226 | { |
| 227 | AllocExclusive(); |
| 228 | M_BRUSHDATA->m_style = style; |
| 229 | } |
| 230 | |
| 231 | void wxBrush::SetStipple(const wxBitmap& stipple) |
| 232 | { |
| 233 | AllocExclusive(); |
| 234 | |
| 235 | wxCHECK_RET( stipple.IsOk(), wxT("invalid bitmap") ); |
| 236 | wxCHECK_RET( stipple.GetWidth() == 8 && stipple.GetHeight() == 8, |
| 237 | wxT("stipple bitmap must be 8x8") ); |
| 238 | |
| 239 | M_BRUSHDATA->m_stipple = stipple; |
| 240 | wxBitmapToPixPattern(stipple, &(M_BRUSHDATA->m_pixPattern), |
| 241 | &(M_BRUSHDATA->m_maskPattern)); |
| 242 | |
| 243 | if (M_BRUSHDATA->m_stipple.GetMask()) |
| 244 | M_BRUSHDATA->m_style = wxSTIPPLE_MASK_OPAQUE; |
| 245 | else |
| 246 | M_BRUSHDATA->m_style = wxSTIPPLE; |
| 247 | } |
| 248 | |
| 249 | wxGDIRefData *wxBrush::CreateGDIRefData() const |
| 250 | { |
| 251 | return new wxBrushRefData; |
| 252 | } |
| 253 | |
| 254 | wxGDIRefData *wxBrush::CloneGDIRefData(const wxGDIRefData *data) const |
| 255 | { |
| 256 | return new wxBrushRefData(*(wxBrushRefData *)data); |
| 257 | } |