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