Lots of OS/2 fixes mostly to do with painting
[wxWidgets.git] / src / os2 / brush.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: brush.cpp
3 // Purpose: wxBrush
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/13/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifndef WX_PRECOMP
16 #include <stdio.h>
17 #include "wx/setup.h"
18 #include "wx/list.h"
19 #include "wx/utils.h"
20 #include "wx/app.h"
21 #include "wx/brush.h"
22 #endif
23
24 #include "wx/os2/private.h"
25
26 #include "assert.h"
27
28 IMPLEMENT_DYNAMIC_CLASS(wxBrush, wxGDIObject)
29
30 wxBrushRefData::wxBrushRefData()
31 {
32 m_nStyle = wxSOLID;
33 m_hBrush = 0;
34 memset(&m_vBundle, '\0', sizeof(AREABUNDLE));
35 } // end of wxBrushRefData::wxBrushRefData
36
37 wxBrushRefData::wxBrushRefData(
38 const wxBrushRefData& rData
39 )
40 {
41 m_nStyle = rData.m_nStyle;
42 m_vStipple = rData.m_vStipple;
43 m_vColour = rData.m_vColour;
44 m_hBrush = 0;
45 memcpy(&m_vBundle, &rData.m_vBundle, sizeof(AREABUNDLE));
46 } // end of wxBrushRefData::wxBrushRefData
47
48 wxBrushRefData::~wxBrushRefData()
49 {
50 } // end of wxBrushRefData::~wxBrushRefData
51
52 //
53 // Brushes
54 //
55 wxBrush::wxBrush()
56 {
57 if ( wxTheBrushList )
58 wxTheBrushList->AddBrush(this);
59 } // end of wxBrush::wxBrush
60
61 wxBrush::~wxBrush()
62 {
63 if ( wxTheBrushList )
64 wxTheBrushList->RemoveBrush(this);
65 } // end of wxBrush::~wxBrush
66
67 wxBrush::wxBrush(
68 const wxColour& rColour
69 , int nStyle
70 )
71 {
72 m_refData = new wxBrushRefData;
73
74 M_BRUSHDATA->m_vColour = rColour;
75 M_BRUSHDATA->m_nStyle = nStyle;
76 M_BRUSHDATA->m_hBrush = 0;
77 memset(&M_BRUSHDATA->m_vBundle, '\0', sizeof(AREABUNDLE));
78
79 RealizeResource();
80
81 if ( wxTheBrushList )
82 wxTheBrushList->AddBrush(this);
83 } // end of wxBrush::wxBrush
84
85 wxBrush::wxBrush(
86 const wxBitmap& rStipple
87 )
88 {
89 m_refData = new wxBrushRefData;
90
91 M_BRUSHDATA->m_nStyle = wxSTIPPLE;
92 M_BRUSHDATA->m_vStipple = rStipple;
93 M_BRUSHDATA->m_hBrush = 0;
94 memset(&M_BRUSHDATA->m_vBundle, '\0', sizeof(AREABUNDLE));
95
96 RealizeResource();
97
98 if ( wxTheBrushList )
99 wxTheBrushList->AddBrush(this);
100 } // end of wxBrush::wxBrush
101
102 bool wxBrush::RealizeResource()
103 {
104 BOOL bOk;
105 ERRORID vError;
106 wxString sError;
107
108 if (M_BRUSHDATA && M_BRUSHDATA->m_hBrush == 0L)
109 {
110 SIZEL vSize = {0, 0};
111 DEVOPENSTRUC vDop = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
112 HDC hDC = ::DevOpenDC( vHabmain
113 ,OD_MEMORY
114 ,"*"
115 ,5L
116 ,(PDEVOPENDATA)&vDop
117 ,NULLHANDLE
118 );
119 M_BRUSHDATA->m_hBrush = (WXHPEN)::GpiCreatePS( vHabmain
120 ,hDC
121 ,&vSize
122 ,PU_PELS | GPIT_MICRO | GPIA_ASSOC
123 );
124 }
125 if (M_BRUSHDATA)
126 {
127 //
128 // Set the color table to RGB mode
129 //
130 if (!::GpiCreateLogColorTable( (HPS)M_BRUSHDATA->m_hBrush
131 ,0L
132 ,LCOLF_RGB
133 ,0L
134 ,0L
135 ,NULL
136 ))
137 {
138 vError = ::WinGetLastError(vHabmain);
139 sError = wxPMErrorToStr(vError);
140 wxLogError("Unable to set current color table to RGB mode. Error: %s\n", sError);
141 return FALSE;
142 }
143
144 if (M_BRUSHDATA->m_nStyle==wxTRANSPARENT)
145 {
146 return TRUE;
147 }
148 COLORREF vPmColour = 0L;
149
150 vPmColour = M_BRUSHDATA->m_vColour.GetPixel() ;
151
152 M_BRUSHDATA->m_vBundle.usSet = LCID_DEFAULT;
153 switch (M_BRUSHDATA->m_nStyle)
154 {
155 case wxTRANSPARENT:
156 M_BRUSHDATA->m_hBrush = NULL; // Must always select a suitable background brush
157 break; // - could choose white always for a quick solution
158
159 case wxBDIAGONAL_HATCH:
160 M_BRUSHDATA->m_vBundle.usSymbol = PATSYM_DIAG3;
161 break;
162
163 case wxCROSSDIAG_HATCH:
164 M_BRUSHDATA->m_vBundle.usSymbol = PATSYM_DIAGHATCH;
165 break;
166
167 case wxFDIAGONAL_HATCH:
168 M_BRUSHDATA->m_vBundle.usSymbol = PATSYM_DIAG1;
169 break;
170
171 case wxCROSS_HATCH:
172 M_BRUSHDATA->m_vBundle.usSymbol = PATSYM_HATCH;
173 break;
174
175 case wxHORIZONTAL_HATCH:
176 M_BRUSHDATA->m_vBundle.usSymbol = PATSYM_HORIZ;
177 break;
178
179 case wxVERTICAL_HATCH:
180 M_BRUSHDATA->m_vBundle.usSymbol = PATSYM_VERT;
181 break;
182
183 case wxSTIPPLE:
184 if (M_BRUSHDATA->m_vStipple.Ok())
185 {
186 ::GpiSetBitmapId( M_BRUSHDATA->m_hBrush
187 ,(USHORT)M_BRUSHDATA->m_vStipple.GetHBITMAP()
188 ,(USHORT)M_BRUSHDATA->m_vStipple.GetId()
189 );
190 ::GpiSetPatternSet( M_BRUSHDATA->m_hBrush
191 ,(USHORT)M_BRUSHDATA->m_vStipple.GetId()
192 );
193 }
194 else
195 M_BRUSHDATA->m_vBundle.usSymbol = PATSYM_SOLID;
196 break ;
197
198 case wxSOLID:
199 default:
200 M_BRUSHDATA->m_vBundle.usSymbol = PATSYM_SOLID;
201 break;
202 }
203 #ifdef WXDEBUG_CREATE
204 if (M_BRUSHDATA->m_hBrush==NULL) wxError("Cannot create brush","Internal error") ;
205 #endif
206 M_BRUSHDATA->m_vBundle.lColor = vPmColour;
207 M_BRUSHDATA->m_vBundle.lBackColor = RGB_WHITE;
208 M_BRUSHDATA->m_vBundle.usMixMode = FM_OVERPAINT;
209 M_BRUSHDATA->m_vBundle.usBackMixMode = BM_OVERPAINT;
210
211 bOk = ::GpiSetAttrs( M_BRUSHDATA->m_hBrush
212 ,PRIM_AREA
213 ,ABB_COLOR | ABB_BACK_COLOR | ABB_MIX_MODE | ABB_BACK_MIX_MODE |
214 ABB_SET | ABB_SYMBOL
215 ,ABB_REF_POINT
216 ,&M_BRUSHDATA->m_vBundle
217 );
218 if (!bOk)
219 {
220 vError = ::WinGetLastError(vHabmain);
221 sError = wxPMErrorToStr(vError);
222 wxLogError("Can't set Gpi attributes for an AREABUNDLE. Error: %s\n", sError);
223 }
224 return bOk;
225 }
226 return FALSE;
227 } // end of wxBrush::RealizeResource
228
229 WXHANDLE wxBrush::GetResourceHandle()
230 {
231 if (!M_BRUSHDATA)
232 return 0;
233 return (WXHANDLE)M_BRUSHDATA->m_hBrush;
234 } // end of wxBrush::GetResourceHandle
235
236 bool wxBrush::FreeResource(
237 bool WXUNUSED(bForce)
238 )
239 {
240 if (M_BRUSHDATA && (M_BRUSHDATA->m_hBrush != 0))
241 {
242 M_BRUSHDATA->m_hBrush = 0;
243 return TRUE;
244 }
245 else return FALSE;
246 } // end of wxBrush::FreeResource
247
248 bool wxBrush::IsFree() const
249 {
250 return (M_BRUSHDATA && (M_BRUSHDATA->m_hBrush == 0));
251 } // end of wxBrush::IsFree
252
253 void wxBrush::Unshare()
254 {
255 //
256 // Don't change shared data
257 //
258 if (!m_refData)
259 {
260 m_refData = new wxBrushRefData();
261 }
262 else
263 {
264 wxBrushRefData* ref = new wxBrushRefData(*(wxBrushRefData*)m_refData);
265 UnRef();
266 m_refData = ref;
267 }
268 } // end of wxBrush::Unshare
269
270 void wxBrush::SetColour(
271 const wxColour& rColour
272 )
273 {
274 Unshare();
275 M_BRUSHDATA->m_vColour = rColour;
276 RealizeResource();
277 }
278
279 void wxBrush::SetColour(
280 unsigned char cRed
281 , unsigned char cGreen
282 , unsigned char cBlue
283 )
284 {
285 Unshare();
286 M_BRUSHDATA->m_vColour.Set( cRed
287 ,cGreen
288 ,cBlue
289 );
290 RealizeResource();
291 } // end of wxBrush::SetColour
292
293 void wxBrush::SetStyle(
294 int nStyle
295 )
296 {
297 Unshare();
298 M_BRUSHDATA->m_nStyle = nStyle;
299 RealizeResource();
300 } // end of wxBrush::SetStyle
301
302 void wxBrush::SetStipple(
303 const wxBitmap& rStipple
304 )
305 {
306 Unshare();
307 M_BRUSHDATA->m_vStipple = rStipple;
308 RealizeResource();
309 } // end of wxBrush::SetStipple
310
311 void wxBrush::SetPS(
312 HPS hPS
313 )
314 {
315 Unshare();
316 if (M_BRUSHDATA->m_hBrush)
317 ::GpiDestroyPS(M_BRUSHDATA->m_hBrush);
318 M_BRUSHDATA->m_hBrush = hPS;
319 RealizeResource();
320 } // end of WxWinGdi_CPen::SetPS
321