wxColour(const char *) ctor related modifications
[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 wxBitmap& stipple)
88 {
89 m_refData = new wxBrushRefData;
90
91 M_BRUSHDATA->m_style = wxSTIPPLE;
92 M_BRUSHDATA->m_stipple = stipple;
93 M_BRUSHDATA->m_hBrush = 0;
94
95 RealizeResource();
96
97 if ( wxTheBrushList )
98 wxTheBrushList->AddBrush(this);
99 }
100
101 bool wxBrush::RealizeResource(void)
102 {
103 if (M_BRUSHDATA && (M_BRUSHDATA->m_hBrush == 0))
104 {
105 if (M_BRUSHDATA->m_style==wxTRANSPARENT)
106 {
107 M_BRUSHDATA->m_hBrush = (WXHBRUSH) ::GetStockObject(NULL_BRUSH);
108 return TRUE;
109 }
110 COLORREF ms_colour = 0 ;
111
112 ms_colour = M_BRUSHDATA->m_colour.GetPixel() ;
113
114 switch (M_BRUSHDATA->m_style)
115 {
116 /****
117 // Don't reset cbrush, wxTRANSPARENT is handled by wxBrush::SelectBrush()
118 // this could save (many) time if frequently switching from
119 // wxSOLID to wxTRANSPARENT, because Create... is not always called!!
120 //
121 // NB August 95: now create and select a Null brush instead.
122 // This could be optimized as above.
123 case wxTRANSPARENT:
124 M_BRUSHDATA->m_hBrush = NULL; // Must always select a suitable background brush
125 // - could choose white always for a quick solution
126 break;
127 ***/
128 case wxBDIAGONAL_HATCH:
129 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_BDIAGONAL,ms_colour) ;
130 break ;
131 case wxCROSSDIAG_HATCH:
132 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_DIAGCROSS,ms_colour) ;
133 break ;
134 case wxFDIAGONAL_HATCH:
135 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_FDIAGONAL,ms_colour) ;
136 break ;
137 case wxCROSS_HATCH:
138 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_CROSS,ms_colour) ;
139 break ;
140 case wxHORIZONTAL_HATCH:
141 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_HORIZONTAL,ms_colour) ;
142 break ;
143 case wxVERTICAL_HATCH:
144 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateHatchBrush(HS_VERTICAL,ms_colour) ;
145 break ;
146 case wxSTIPPLE:
147 if (M_BRUSHDATA->m_stipple.Ok())
148 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreatePatternBrush((HBITMAP) M_BRUSHDATA->m_stipple.GetHBITMAP()) ;
149 else
150 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateSolidBrush(ms_colour) ;
151 break ;
152 case wxSOLID:
153 default:
154 M_BRUSHDATA->m_hBrush = (WXHBRUSH) CreateSolidBrush(ms_colour) ;
155 break;
156 }
157 #ifdef WXDEBUG_CREATE
158 if (M_BRUSHDATA->m_hBrush==NULL) wxError("Cannot create brush","Internal error") ;
159 #endif
160 return TRUE;
161 }
162 else
163 return FALSE;
164 }
165
166 WXHANDLE wxBrush::GetResourceHandle(void)
167 {
168 return (WXHANDLE) M_BRUSHDATA->m_hBrush;
169 }
170
171 bool wxBrush::FreeResource(bool force)
172 {
173 if (M_BRUSHDATA && (M_BRUSHDATA->m_hBrush != 0))
174 {
175 DeleteObject((HBRUSH) M_BRUSHDATA->m_hBrush);
176 M_BRUSHDATA->m_hBrush = 0;
177 return TRUE;
178 }
179 else return FALSE;
180 }
181
182 bool wxBrush::IsFree(void)
183 {
184 return (M_BRUSHDATA && (M_BRUSHDATA->m_hBrush == 0));
185 }
186
187 void wxBrush::Unshare()
188 {
189 // Don't change shared data
190 if (!m_refData)
191 {
192 m_refData = new wxBrushRefData();
193 }
194 else
195 {
196 wxBrushRefData* ref = new wxBrushRefData(*(wxBrushRefData*)m_refData);
197 UnRef();
198 m_refData = ref;
199 }
200 }
201
202
203 void wxBrush::SetColour(const wxColour& col)
204 {
205 Unshare();
206
207 M_BRUSHDATA->m_colour = col;
208
209 RealizeResource();
210 }
211
212 void wxBrush::SetColour(unsigned char r, unsigned char g, unsigned char b)
213 {
214 Unshare();
215
216 M_BRUSHDATA->m_colour.Set(r, g, b);
217
218 RealizeResource();
219 }
220
221 void wxBrush::SetStyle(int Style)
222 {
223 Unshare();
224
225 M_BRUSHDATA->m_style = Style;
226
227 RealizeResource();
228 }
229
230 void wxBrush::SetStipple(const wxBitmap& Stipple)
231 {
232 Unshare();
233
234 M_BRUSHDATA->m_stipple = Stipple;
235
236 RealizeResource();
237 }
238
239