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
= vWidth
* 3;
213 vInfo
.cx
= vHeader
.cx
;
214 vInfo
.cy
= vHeader
.cy
;
215 vInfo
.cPlanes
= vHeader
.cPlanes
;
216 vInfo
.cBitCount
= 24;
217 pucData
= (unsigned char*)malloc(nBytesPerLine
* m_vSelectedBitmap
.GetHeight());
218 if ((lScans
= ::GpiQueryBitmapBits( m_hPS
220 ,(LONG
)m_vSelectedBitmap
.GetHeight()
228 vError
= ::WinGetLastError(vHabmain
);
229 sError
= wxPMErrorToStr(vError
);
232 for (int i
= 0; i
< m_vSelectedBitmap
.GetHeight(); i
++)
234 for (int j
= vX
; j
< m_vSelectedBitmap
.GetWidth(); j
++)
236 if (i
>= vY
&& j
>= vX
&& i
< vHeight
&& j
< vWidth
)
238 vPoint
.x
= j
; vPoint
.y
= i
;
239 if (i
== vY
|| j
== vX
||
240 i
== m_vSelectedBitmap
.GetWidth() -1 ||
241 j
== m_vSelectedBitmap
.GetHeight()
243 lColor
= m_pen
.GetColour().GetPixel();
245 lColor
= m_brush
.GetColour().GetPixel();
246 *(pucBits
++) = (unsigned char)lColor
;
247 *(pucBits
++) = (unsigned char)(lColor
>> 8);
248 *(pucBits
++) = (unsigned char)(lColor
>> 16);
254 if ((lScans
= ::GpiSetBitmapBits( m_hPS
256 ,(LONG
)m_vSelectedBitmap
.GetHeight()
264 vError
= ::WinGetLastError(vHabmain
);
265 sError
= wxPMErrorToStr(vError
);
270 } // end of wxMemoryDC::DoDrawRectangle