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