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