Sorry, I went and removed consts as per the style guide :-)
[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 #if !USE_SHARED_LIBRARIES
37 IMPLEMENT_DYNAMIC_CLASS(wxBrush, wxGDIObject)
38 #endif
39
40 wxBrushRefData::wxBrushRefData(void)
41 {
42 m_style = wxSOLID;
43 // m_stipple = NULL ;
44 m_hBrush = 0;
45 }
46
47 wxBrushRefData::~wxBrushRefData(void)
48 {
49 if ( m_hBrush )
50 ::DeleteObject((HBRUSH) m_hBrush);
51 }
52
53 // Brushes
54 wxBrush::wxBrush(void)
55 {
56 if ( wxTheBrushList )
57 wxTheBrushList->AddBrush(this);
58 }
59
60 wxBrush::~wxBrush()
61 {
62 if (wxTheBrushList)
63 wxTheBrushList->RemoveBrush(this);
64 }
65
66 wxBrush::wxBrush(const wxColour& col, int Style)
67 {
68 m_refData = new wxBrushRefData;
69
70 M_BRUSHDATA->m_colour = col;
71 M_BRUSHDATA->m_style = Style;
72 M_BRUSHDATA->m_hBrush = 0;
73
74 RealizeResource();
75
76 if ( wxTheBrushList )
77 wxTheBrushList->AddBrush(this);
78 }
79
80 wxBrush::wxBrush(const wxString& col, int Style)
81 {
82 m_refData = new wxBrushRefData;
83
84 M_BRUSHDATA->m_colour = col;
85 M_BRUSHDATA->m_style = Style;
86 M_BRUSHDATA->m_hBrush = 0;
87
88 RealizeResource();
89
90 if ( wxTheBrushList )
91 wxTheBrushList->AddBrush(this);
92 }
93
94 wxBrush::wxBrush(const wxBitmap& stipple)
95 {
96 m_refData = new wxBrushRefData;
97
98 M_BRUSHDATA->m_style = wxSTIPPLE;
99 M_BRUSHDATA->m_stipple = stipple;
100 M_BRUSHDATA->m_hBrush = 0;
101
102 RealizeResource();
103
104 if ( wxTheBrushList )
105 wxTheBrushList->AddBrush(this);
106 }
107
108 bool wxBrush::RealizeResource(void)
109 {
110 if (M_BRUSHDATA && (M_BRUSHDATA->m_hBrush == 0))
111 {
112 if (M_BRUSHDATA->m_style==wxTRANSPARENT)
113 {
114 M_BRUSHDATA->m_hBrush = (WXHBRUSH) ::GetStockObject(NULL_BRUSH);
115 return TRUE;
116 }
117 COLORREF ms_colour = 0 ;
118
119 ms_colour = M_BRUSHDATA->m_colour.GetPixel() ;
120
121 switch (M_BRUSHDATA->m_style)
122 {
123 /****
124 // Don't reset cbrush, wxTRANSPARENT is handled by wxBrush::SelectBrush()
125 // this could save (many) time if frequently switching from
126 // wxSOLID to wxTRANSPARENT, because Create... is not always called!!
127 //
128 // NB August 95: now create and select a Null brush instead.
129 // This could be optimized as above.
130 case wxTRANSPARENT:
131 M_BRUSHDATA->m_hBrush = NULL; // Must always select a suitable background brush
132 // - could choose white always for a quick solution
133 break;
134 ***/
135 case wxBDIAGONAL_HATCH:
136 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_BDIAGONAL,ms_colour) ;
137 break ;
138 case wxCROSSDIAG_HATCH:
139 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_DIAGCROSS,ms_colour) ;
140 break ;
141 case wxFDIAGONAL_HATCH:
142 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_FDIAGONAL,ms_colour) ;
143 break ;
144 case wxCROSS_HATCH:
145 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_CROSS,ms_colour) ;
146 break ;
147 case wxHORIZONTAL_HATCH:
148 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_HORIZONTAL,ms_colour) ;
149 break ;
150 case wxVERTICAL_HATCH:
151 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_VERTICAL,ms_colour) ;
152 break ;
153 case wxSTIPPLE:
154 if (M_BRUSHDATA->m_stipple.Ok())
155 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreatePatternBrush((HBITMAP) M_BRUSHDATA->m_stipple.GetHBITMAP()) ;
156 else
157 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateSolidBrush(ms_colour) ;
158 break ;
159 case wxSOLID:
160 default:
161 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateSolidBrush(ms_colour) ;
162 break;
163 }
164 #ifdef DEBUG_CREATE
165 if (M_BRUSHDATA->m_hBrush==NULL) wxError("Cannot create brush","Internal error") ;
166 #endif
167 return TRUE;
168 }
169 else
170 return FALSE;
171 }
172
173 WXHANDLE wxBrush::GetResourceHandle(void)
174 {
175 return (WXHANDLE) M_BRUSHDATA->m_hBrush;
176 }
177
178 bool wxBrush::FreeResource(bool force)
179 {
180 if (M_BRUSHDATA && (M_BRUSHDATA->m_hBrush != 0))
181 {
182 DeleteObject((HBRUSH) M_BRUSHDATA->m_hBrush);
183 M_BRUSHDATA->m_hBrush = 0;
184 return TRUE;
185 }
186 else return FALSE;
187 }
188
189 /*
190 bool wxBrush::UseResource(void)
191 {
192 IncrementResourceUsage();
193 return TRUE;
194 }
195
196 bool wxBrush::ReleaseResource(void)
197 {
198 DecrementResourceUsage();
199 return TRUE;
200 }
201 */
202
203 bool wxBrush::IsFree(void)
204 {
205 return (M_BRUSHDATA && (M_BRUSHDATA->m_hBrush == 0));
206 }
207
208 void wxBrush::SetColour(const wxColour& col)
209 {
210 if ( !M_BRUSHDATA )
211 m_refData = new wxBrushRefData;
212
213 M_BRUSHDATA->m_colour = col;
214
215 if (FreeResource())
216 RealizeResource();
217 }
218
219 void wxBrush::SetColour(const wxString& col)
220 {
221 if ( !M_BRUSHDATA )
222 m_refData = new wxBrushRefData;
223
224 M_BRUSHDATA->m_colour = col;
225
226 if (FreeResource())
227 RealizeResource();
228 }
229
230 void wxBrush::SetColour(const unsigned char r, const unsigned char g, const unsigned char b)
231 {
232 if ( !M_BRUSHDATA )
233 m_refData = new wxBrushRefData;
234
235 M_BRUSHDATA->m_colour.Set(r, g, b);
236
237 if (FreeResource())
238 RealizeResource();
239 }
240
241 void wxBrush::SetStyle(int Style)
242 {
243 if ( !M_BRUSHDATA )
244 m_refData = new wxBrushRefData;
245
246 M_BRUSHDATA->m_style = Style;
247
248 if (FreeResource())
249 RealizeResource();
250 }
251
252 void wxBrush::SetStipple(const wxBitmap& Stipple)
253 {
254 if ( !M_BRUSHDATA )
255 m_refData = new wxBrushRefData;
256
257 M_BRUSHDATA->m_stipple = Stipple;
258
259 if (FreeResource())
260 RealizeResource();
261 }
262
263