1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxMemoryDC class
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 #include "wx/os2/private.h"
23 #include "wx/dcmemory.h"
25 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC
, wxDC
)
27 /////////////////////////////////////////////////////////////////////////////
29 /////////////////////////////////////////////////////////////////////////////
31 wxMemoryDC::wxMemoryDC(void)
33 CreateCompatible(NULL
);
35 } // end of wxMemoryDC::wxMemoryDC
37 wxMemoryDC::wxMemoryDC(
41 pOldDC
->BeginDrawing();
42 CreateCompatible(pOldDC
);
45 } // end of wxMemoryDC::wxMemoryDC
47 void wxMemoryDC::Init()
51 SetBrush(*wxWHITE_BRUSH
);
54 // the background mode is only used for text background and is set in
55 // DrawText() to OPAQUE as required, otherwise always TRANSPARENT
56 ::GpiSetBackMix( GetHPS(), BM_LEAVEALONE
);
58 memset(&m_vRclPaint
, 0, sizeof(m_vRclPaint
));
59 } // end of wxMemoryDC::Init
61 bool wxMemoryDC::CreateCompatible(
67 DEVOPENSTRUC vDOP
= {0L, "DISPLAY", NULL
, 0L, 0L, 0L, 0L, 0L, 0L};
71 // Create a memory device context
73 hDC
= ::DevOpenDC(vHabmain
, OD_MEMORY
, "*", 5L, (PDEVOPENDATA
)&vDOP
, NULLHANDLE
);
76 hPS
= ::GpiCreatePS(vHabmain
, hDC
, &vSize
, PU_PELS
| GPIT_MICRO
| GPIA_ASSOC
);
84 // Set the wxWindows color table
86 ::GpiCreateLogColorTable( m_hPS
90 ,(LONG
)wxTheColourDatabase
->m_nSize
91 ,(PLONG
)wxTheColourDatabase
->m_palTable
93 ::GpiCreateLogColorTable( m_hPS
118 // As we created the DC, we must delete it in the dtor
123 } // end of wxMemoryDC::CreateCompatible
125 void wxMemoryDC::SelectObject(
126 const wxBitmap
& rBitmap
130 // Select old bitmap out of the device context
134 ::GpiSetBitmap(m_hPS
, NULLHANDLE
);
135 if (m_vSelectedBitmap
.Ok())
137 m_vSelectedBitmap
.SetSelectedInto(NULL
);
138 m_vSelectedBitmap
= wxNullBitmap
;
143 // Check for whether the bitmap is already selected into a device context
145 wxCHECK_RET( !rBitmap
.GetSelectedInto() ||
146 (rBitmap
.GetSelectedInto() == this),
147 wxT("Bitmap is selected in another wxMemoryDC, delete the first wxMemoryDC or use SelectObject(NULL)") );
149 m_vSelectedBitmap
= rBitmap
;
151 WXHBITMAP hBmp
= rBitmap
.GetHBITMAP();
156 m_vSelectedBitmap
.SetSelectedInto(this);
157 hBmp
= (WXHBITMAP
)::GpiSetBitmap(m_hPS
, (HBITMAP
)hBmp
);
159 if (hBmp
== HBM_ERROR
)
161 wxLogLastError(wxT("SelectObject(memDC, bitmap)"));
162 wxFAIL_MSG(wxT("Couldn't select a bitmap into wxMemoryDC"));
164 else if (!m_hOldBitmap
)
168 } // end of wxMemoryDC::SelectObject
170 void wxMemoryDC::DoGetSize(
175 if (!m_vSelectedBitmap
.Ok())
181 *pWidth
= m_vSelectedBitmap
.GetWidth();
182 *pHeight
= m_vSelectedBitmap
.GetHeight();
183 } // end of wxMemoryDC::DoGetSize
185 void wxMemoryDC::DoDrawRectangle(
192 wxDC::DoDrawRectangle(vX
, vY
, vWidth
, vHeight
);
197 if (m_vSelectedBitmap
.GetHBITMAP() != NULLHANDLE
)
199 BITMAPINFOHEADER2 vHeader
;
203 if (::GpiQueryBitmapInfoHeader(m_vSelectedBitmap
.GetHBITMAP(), &vHeader
))
205 unsigned char* pucData
= NULL
;
206 unsigned char* pucBits
;
207 int nBytesPerLine
= m_vSelectedBitmap
.GetWidth() * 3;
211 LONG alFormats
[24]; // Max formats OS/2 PM supports
214 ::GpiQueryDeviceBitmapFormats(m_hPS
, 24, alFormats
);
215 ulBitcount
= alFormats
[1]; // the best one for the device
217 ulBitcount
= 24; // MAX bits supported by PM
219 vInfo
.cx
= vHeader
.cx
;
220 vInfo
.cy
= vHeader
.cy
;
221 vInfo
.cPlanes
= vHeader
.cPlanes
;
222 vInfo
.cBitCount
= ulBitcount
;
223 pucData
= (unsigned char*)malloc(nBytesPerLine
* m_vSelectedBitmap
.GetHeight());
224 if ((lScans
= ::GpiQueryBitmapBits( m_hPS
226 ,(LONG
)m_vSelectedBitmap
.GetHeight()
234 vError
= ::WinGetLastError(vHabmain
);
235 sError
= wxPMErrorToStr(vError
);
238 for (int i
= 0; i
< m_vSelectedBitmap
.GetHeight(); i
++)
240 for (int j
= 0; j
< m_vSelectedBitmap
.GetWidth(); j
++)
242 vPoint
.x
= j
; vPoint
.y
= i
;
243 lColor
= ::GpiQueryPel(m_hPS
, &vPoint
);
244 *(pucBits
++) = (unsigned char)lColor
;
245 *(pucBits
++) = (unsigned char)(lColor
>> 8);
246 *(pucBits
++) = (unsigned char)(lColor
>> 16);
249 if ((lScans
= ::GpiSetBitmapBits( m_hPS
251 ,(LONG
)m_vSelectedBitmap
.GetHeight()
259 vError
= ::WinGetLastError(vHabmain
);
260 sError
= wxPMErrorToStr(vError
);
265 } // end of wxMemoryDC::DoDrawRectangle
267 void wxMemoryDC::DoDrawRoundedRectangle(
275 wxDC::DoDrawRoundedRectangle(vX
, vY
, vWidth
, vHeight
, dRadius
);
280 if (m_vSelectedBitmap
.GetHBITMAP() != NULLHANDLE
)
282 BITMAPINFOHEADER2 vHeader
;
286 if (::GpiQueryBitmapInfoHeader(m_vSelectedBitmap
.GetHBITMAP(), &vHeader
))
288 unsigned char* pucData
= NULL
;
289 unsigned char* pucBits
;
290 int nBytesPerLine
= m_vSelectedBitmap
.GetWidth() * 3;
294 LONG alFormats
[24]; // Max formats OS/2 PM supports
297 ::GpiQueryDeviceBitmapFormats(m_hPS
, 24, alFormats
);
298 ulBitcount
= alFormats
[1]; // the best one for the device
300 ulBitcount
= 24; // MAX bits supported by PM
302 vInfo
.cx
= vHeader
.cx
;
303 vInfo
.cy
= vHeader
.cy
;
304 vInfo
.cPlanes
= vHeader
.cPlanes
;
305 vInfo
.cBitCount
= ulBitcount
;
306 pucData
= (unsigned char*)malloc(nBytesPerLine
* m_vSelectedBitmap
.GetHeight());
307 if ((lScans
= ::GpiQueryBitmapBits( m_hPS
309 ,(LONG
)m_vSelectedBitmap
.GetHeight()
317 vError
= ::WinGetLastError(vHabmain
);
318 sError
= wxPMErrorToStr(vError
);
321 for (int i
= 0; i
< m_vSelectedBitmap
.GetHeight(); i
++)
323 for (int j
= 0; j
< m_vSelectedBitmap
.GetWidth(); j
++)
325 vPoint
.x
= j
; vPoint
.y
= i
;
326 lColor
= ::GpiQueryPel(m_hPS
, &vPoint
);
327 *(pucBits
++) = (unsigned char)lColor
;
328 *(pucBits
++) = (unsigned char)(lColor
>> 8);
329 *(pucBits
++) = (unsigned char)(lColor
>> 16);
332 if ((lScans
= ::GpiSetBitmapBits( m_hPS
334 ,(LONG
)m_vSelectedBitmap
.GetHeight()
342 vError
= ::WinGetLastError(vHabmain
);
343 sError
= wxPMErrorToStr(vError
);
348 } // end of wxMemoryDC::DoDrawRectangle