wxMemoryDC constructor now optionally accepts a wxBitmap parameter,
[wxWidgets.git] / src / os2 / dcmemory.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/dcmemory.cpp
3 // Purpose: wxMemoryDC class
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/14/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 #include "wx/dcmemory.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/utils.h"
19 #include "wx/app.h"
20 #include "wx/log.h"
21 #endif
22
23 #include "wx/os2/private.h"
24
25 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC, wxDC)
26
27 /////////////////////////////////////////////////////////////////////////////
28 // Memory DC
29 /////////////////////////////////////////////////////////////////////////////
30
31 wxMemoryDC::wxMemoryDC( const wxBitmap& bitmap )
32 {
33 CreateCompatible(NULL);
34 Init();
35
36 if ( bitmap.IsOk() )
37 SelectObject(bitmap);
38 } // end of wxMemoryDC::wxMemoryDC
39
40 wxMemoryDC::wxMemoryDC(
41 wxDC* pOldDC
42 )
43 {
44 CreateCompatible(pOldDC);
45 Init();
46 } // end of wxMemoryDC::wxMemoryDC
47
48 void wxMemoryDC::Init()
49 {
50 if (m_ok)
51 {
52 SetBrush(*wxWHITE_BRUSH);
53 SetPen(*wxBLACK_PEN);
54
55 // the background mode is only used for text background and is set in
56 // DrawText() to OPAQUE as required, otherwise always TRANSPARENT
57 ::GpiSetBackMix( GetHPS(), BM_LEAVEALONE );
58 }
59 memset(&m_vRclPaint, 0, sizeof(m_vRclPaint));
60 } // end of wxMemoryDC::Init
61
62 bool wxMemoryDC::CreateCompatible( wxDC* WXUNUSED(pDC) )
63 {
64 HDC hDC;
65 HPS hPS;
66 DEVOPENSTRUC vDOP = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
67 SIZEL vSize = {0, 0};
68
69 //
70 // Create a memory device context
71 //
72 hDC = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDOP, NULLHANDLE);
73 if (hDC != DEV_ERROR)
74 {
75 hPS = ::GpiCreatePS(vHabmain, hDC, &vSize, PU_PELS | GPIT_MICRO | GPIA_ASSOC);
76 if (hPS != GPI_ERROR)
77 {
78 m_hPS = hPS;
79 m_hDC = hDC;
80 m_ok = true;
81 m_bOwnsDC = true;
82 //
83 // Set the wxWidgets color table
84 //
85 ::GpiCreateLogColorTable( m_hPS
86 ,0L
87 ,LCOLF_CONSECRGB
88 ,0L
89 ,(LONG)wxTheColourDatabase->m_nSize
90 ,(PLONG)wxTheColourDatabase->m_palTable
91 );
92 ::GpiCreateLogColorTable( m_hPS
93 ,0L
94 ,LCOLF_RGB
95 ,0L
96 ,0L
97 ,NULL
98 );
99 }
100 else
101 {
102 m_hPS = NULLHANDLE;
103 m_hDC = NULLHANDLE;
104 m_ok = false;
105 m_bOwnsDC = false;
106 }
107 }
108 else
109 {
110 m_hPS = NULLHANDLE;
111 m_hDC = NULLHANDLE;
112 m_ok = false;
113 m_bOwnsDC = false;
114 }
115
116 //
117 // As we created the DC, we must delete it in the dtor
118 //
119 m_bOwnsDC = true;
120 m_ok = m_hDC != 0;
121 return m_ok;
122 } // end of wxMemoryDC::CreateCompatible
123
124 void wxMemoryDC::SelectObject(
125 const wxBitmap& rBitmap
126 )
127 {
128 //
129 // Select old bitmap out of the device context
130 //
131 if (m_hOldBitmap)
132 {
133 ::GpiSetBitmap(m_hPS, NULLHANDLE);
134 if (m_vSelectedBitmap.Ok())
135 {
136 m_vSelectedBitmap.SetSelectedInto(NULL);
137 m_vSelectedBitmap = wxNullBitmap;
138 }
139 }
140
141 //
142 // Check for whether the bitmap is already selected into a device context
143 //
144 wxCHECK_RET( !rBitmap.GetSelectedInto() ||
145 (rBitmap.GetSelectedInto() == this),
146 wxT("Bitmap is selected in another wxMemoryDC, delete the first wxMemoryDC or use SelectObject(NULL)") );
147
148 WXHBITMAP hBmp = rBitmap.GetHBITMAP();
149
150 if (!hBmp)
151 {
152 //
153 // Bmps drawn to are upside down, so flip it before committing
154 //
155 POINTL vPoint[4] = { {0, m_vSelectedBitmap.GetHeight()}
156 ,{m_vSelectedBitmap.GetWidth(), 0}
157 ,{0, 0}
158 ,{m_vSelectedBitmap.GetWidth(), m_vSelectedBitmap.GetHeight()}
159 };
160
161
162 ::GpiBitBlt( m_hPS
163 ,m_hPS
164 ,4
165 ,vPoint
166 ,ROP_SRCCOPY
167 ,BBO_IGNORE
168 );
169 m_vSelectedBitmap.SetSelectedInto(NULL);
170 }
171 m_vSelectedBitmap = rBitmap;
172
173
174 if (!hBmp)
175 {
176
177 m_hOldBitmap = (WXHBITMAP)::GpiSetBitmap(m_hPS, NULLHANDLE);
178 return;
179 }
180 m_vSelectedBitmap.SetSelectedInto(this);
181 m_hOldBitmap = (WXHBITMAP)::GpiSetBitmap(m_hPS, (HBITMAP)hBmp);
182
183 if (m_hOldBitmap == HBM_ERROR)
184 {
185 wxLogLastError(wxT("SelectObject(memDC, bitmap)"));
186 wxFAIL_MSG(wxT("Couldn't select a bitmap into wxMemoryDC"));
187 }
188 } // end of wxMemoryDC::SelectObject
189
190 void wxMemoryDC::DoGetSize(
191 int* pWidth
192 , int* pHeight
193 ) const
194 {
195 if (!m_vSelectedBitmap.Ok())
196 {
197 *pWidth = 0;
198 *pHeight = 0;
199 return;
200 }
201 *pWidth = m_vSelectedBitmap.GetWidth();
202 *pHeight = m_vSelectedBitmap.GetHeight();
203 } // end of wxMemoryDC::DoGetSize