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