patches for wxSTIPPLE_MASK_OPAQUE from Klass Holwerda
[wxWidgets.git] / src / msw / brush.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifdef __GNUG__
13 #pragma implementation "brush.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include <stdio.h>
25 #include "wx/setup.h"
26 #include "wx/list.h"
27 #include "wx/utils.h"
28 #include "wx/app.h"
29 #include "wx/brush.h"
30 #endif
31
32 #include "wx/msw/private.h"
33
34 #include "assert.h"
35
36 IMPLEMENT_DYNAMIC_CLASS(wxBrush, wxGDIObject)
37
38 wxBrushRefData::wxBrushRefData(void)
39 {
40 m_style = wxSOLID;
41 m_hBrush = 0;
42 }
43
44 wxBrushRefData::wxBrushRefData(const wxBrushRefData& data)
45 {
46 m_style = data.m_style;
47 m_stipple = data.m_stipple;
48 m_colour = data.m_colour;
49 m_hBrush = 0;
50 }
51
52 wxBrushRefData::~wxBrushRefData(void)
53 {
54 if ( m_hBrush )
55 ::DeleteObject((HBRUSH) m_hBrush);
56 }
57
58 // Brushes
59 wxBrush::wxBrush(void)
60 {
61 if ( wxTheBrushList )
62 wxTheBrushList->AddBrush(this);
63 }
64
65 wxBrush::~wxBrush()
66 {
67 if (wxTheBrushList)
68 wxTheBrushList->RemoveBrush(this);
69 }
70
71 wxBrush::wxBrush(const wxColour& col, int Style)
72 {
73 m_refData = new wxBrushRefData;
74
75 M_BRUSHDATA->m_colour = col;
76 M_BRUSHDATA->m_style = Style;
77 M_BRUSHDATA->m_hBrush = 0;
78
79 RealizeResource();
80
81 if ( wxTheBrushList )
82 wxTheBrushList->AddBrush(this);
83 }
84
85 wxBrush::wxBrush(const wxBitmap& stipple)
86 {
87 m_refData = new wxBrushRefData;
88
89 M_BRUSHDATA->m_stipple = stipple;
90 if (M_BRUSHDATA->m_stipple.GetMask())
91 M_BRUSHDATA->m_style = wxSTIPPLE_MASK_OPAQUE;
92 else
93 M_BRUSHDATA->m_style = wxSTIPPLE;
94
95 M_BRUSHDATA->m_hBrush = 0;
96
97 RealizeResource();
98
99 if ( wxTheBrushList )
100 wxTheBrushList->AddBrush(this);
101 }
102
103 bool wxBrush::RealizeResource(void)
104 {
105 if (M_BRUSHDATA && (M_BRUSHDATA->m_hBrush == 0))
106 {
107 if (M_BRUSHDATA->m_style==wxTRANSPARENT)
108 {
109 M_BRUSHDATA->m_hBrush = (WXHBRUSH) ::GetStockObject(NULL_BRUSH);
110 return TRUE;
111 }
112 COLORREF ms_colour = 0 ;
113
114 ms_colour = M_BRUSHDATA->m_colour.GetPixel() ;
115
116 switch (M_BRUSHDATA->m_style)
117 {
118 /****
119 // Don't reset cbrush, wxTRANSPARENT is handled by wxBrush::SelectBrush()
120 // this could save (many) time if frequently switching from
121 // wxSOLID to wxTRANSPARENT, because Create... is not always called!!
122 //
123 // NB August 95: now create and select a Null brush instead.
124 // This could be optimized as above.
125 case wxTRANSPARENT:
126 M_BRUSHDATA->m_hBrush = NULL; // Must always select a suitable background brush
127 // - could choose white always for a quick solution
128 break;
129 ***/
130 case wxBDIAGONAL_HATCH:
131 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_BDIAGONAL,ms_colour) ;
132 break ;
133 case wxCROSSDIAG_HATCH:
134 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_DIAGCROSS,ms_colour) ;
135 break ;
136 case wxFDIAGONAL_HATCH:
137 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_FDIAGONAL,ms_colour) ;
138 break ;
139 case wxCROSS_HATCH:
140 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_CROSS,ms_colour) ;
141 break ;
142 case wxHORIZONTAL_HATCH:
143 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_HORIZONTAL,ms_colour) ;
144 break ;
145 case wxVERTICAL_HATCH:
146 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_VERTICAL,ms_colour) ;
147 break ;
148 case wxSTIPPLE:
149 if (M_BRUSHDATA->m_stipple.Ok())
150 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreatePatternBrush((HBITMAP) M_BRUSHDATA->m_stipple.GetHBITMAP()) ;
151 else
152 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateSolidBrush(ms_colour) ;
153 break ;
154 case wxSTIPPLE_MASK_OPAQUE:
155 if (M_BRUSHDATA->m_stipple.Ok() && M_BRUSHDATA->m_stipple.GetMask())
156 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreatePatternBrush((HBITMAP) M_BRUSHDATA->m_stipple.GetMask()->GetMaskBitmap());
157 else
158 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateSolidBrush(ms_colour) ;
159 break ;
160 case wxSOLID:
161 default:
162 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateSolidBrush(ms_colour) ;
163 break;
164 }
165 #ifdef WXDEBUG_CREATE
166 if (M_BRUSHDATA->m_hBrush==NULL) wxError("Cannot create brush","Internal error") ;
167 #endif
168 return TRUE;
169 }
170 else
171 return FALSE;
172 }
173
174 WXHANDLE wxBrush::GetResourceHandle(void)
175 {
176 return (WXHANDLE) M_BRUSHDATA->m_hBrush;
177 }
178
179 bool wxBrush::FreeResource(bool WXUNUSED(force))
180 {
181 if (M_BRUSHDATA && (M_BRUSHDATA->m_hBrush != 0))
182 {
183 DeleteObject((HBRUSH) M_BRUSHDATA->m_hBrush);
184 M_BRUSHDATA->m_hBrush = 0;
185 return TRUE;
186 }
187 else return FALSE;
188 }
189
190 bool wxBrush::IsFree() const
191 {
192 return (M_BRUSHDATA && (M_BRUSHDATA->m_hBrush == 0));
193 }
194
195 void wxBrush::Unshare()
196 {
197 // Don't change shared data
198 if (!m_refData)
199 {
200 m_refData = new wxBrushRefData();
201 }
202 else
203 {
204 wxBrushRefData* ref = new wxBrushRefData(*(wxBrushRefData*)m_refData);
205 UnRef();
206 m_refData = ref;
207 }
208 }
209
210
211 void wxBrush::SetColour(const wxColour& col)
212 {
213 Unshare();
214
215 M_BRUSHDATA->m_colour = col;
216
217 RealizeResource();
218 }
219
220 void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
221 {
222 Unshare();
223
224 M_BRUSHDATA->m_colour.Set(r, g, b);
225
226 RealizeResource();
227 }
228
229 void wxBrush::SetStyle(int Style)
230 {
231 Unshare();
232
233 M_BRUSHDATA->m_style = Style;
234
235 RealizeResource();
236 }
237
238 void wxBrush::SetStipple(const wxBitmap& Stipple)
239 {
240 Unshare();
241
242 M_BRUSHDATA->m_stipple = Stipple;
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 RealizeResource();
249 }
250
251