OwnerDrawn updates
[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 #endif
18
19 #include "wx/os2/private.h"
20
21 #include "wx/dcmemory.h"
22
23 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC, wxDC)
24
25 /////////////////////////////////////////////////////////////////////////////
26 // Memory DC
27 /////////////////////////////////////////////////////////////////////////////
28
29 wxMemoryDC::wxMemoryDC(void)
30 {
31 HDC hDC;
32 HPS hPS;
33 DEVOPENSTRUC vDOP = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
34 SIZEL vSize = {0, 0};
35
36 //
37 // Create a memory device context
38 //
39 hDC = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDOP, NULLHANDLE);
40 if (hDC != DEV_ERROR)
41 {
42 hPS = ::GpiCreatePS(vHabmain, hDC, &vSize, PU_PELS | GPIT_MICRO | GPIA_ASSOC);
43 if (hPS != GPI_ERROR)
44 {
45 m_hPS = hPS;
46 m_hDC = hDC;
47 m_ok = TRUE;
48 m_bOwnsDC = TRUE;
49 SetBrush(*wxWHITE_BRUSH);
50 SetPen(*wxBLACK_PEN);
51 }
52 else
53 {
54 m_hPS = NULLHANDLE;
55 m_hDC = NULLHANDLE;
56 m_ok = FALSE;
57 m_bOwnsDC = FALSE;
58 }
59 }
60 else
61 {
62 m_hPS = NULLHANDLE;
63 m_hDC = NULLHANDLE;
64 m_ok = FALSE;
65 m_bOwnsDC = FALSE;
66 }
67 } // end of wxMemoryDC::wxMemoryDC
68
69 wxMemoryDC::wxMemoryDC(
70 wxDC* pOldDC
71 )
72 {
73 HDC hDC;
74 HPS hPS;
75 DEVOPENSTRUC vDOP = {0L, "DISPLAY", NULL, 0L, 0L, 0L, 0L, 0L, 0L};
76 SIZEL vSize = {0, 0};
77
78 pOldDC->BeginDrawing();
79
80 //
81 // Create a memory device context
82 //
83 hDC = ::DevOpenDC(vHabmain, OD_MEMORY, "*", 5L, (PDEVOPENDATA)&vDOP, GetHdcOf(*pOldDC));
84 if (hDC != DEV_ERROR)
85 {
86 hPS = ::GpiCreatePS(vHabmain, hDC, &vSize, PU_PELS | GPIT_MICRO | GPIA_ASSOC);
87 if (hPS != GPI_ERROR)
88 {
89 m_hPS = hPS;
90 m_hDC = hDC;
91 m_ok = TRUE;
92 m_bOwnsDC = TRUE;
93 pOldDC->EndDrawing();
94 SetBrush(*wxWHITE_BRUSH);
95 SetPen(*wxBLACK_PEN);
96 }
97 else
98 {
99 pOldDC->EndDrawing();
100 m_hPS = NULLHANDLE;
101 m_hDC = NULLHANDLE;
102 m_ok = FALSE;
103 m_bOwnsDC = FALSE;
104 }
105 }
106 else
107 {
108 pOldDC->EndDrawing();
109 m_hPS = NULLHANDLE;
110 m_hDC = NULLHANDLE;
111 m_ok = FALSE;
112 m_bOwnsDC = FALSE;
113 }
114 } // end of wxMemoryDC::wxMemoryDC
115
116 wxMemoryDC::~wxMemoryDC()
117 {
118 if (m_hPS != NULLHANDLE)
119 ::GpiDestroyPS(m_hPS);
120 if (m_hDC != NULLHANDLE)
121 ::DevCloseDC(m_hDC);
122 } // end of wxMemoryDC::~wxMemoryDC
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 m_vSelectedBitmap = rBitmap;
149
150 WXHBITMAP hBmp = m_vSelectedBitmap.GetHBITMAP();
151
152 if (!hBmp)
153 return;
154
155 m_vSelectedBitmap.SetSelectedInto(this);
156 hBmp = (WXHBITMAP)::GpiSetBitmap(m_hPS, (HBITMAP)hBmp);
157
158 if (hBmp != HBM_ERROR)
159 {
160 wxLogLastError(wxT("SelectObject(memDC, bitmap)"));
161 wxFAIL_MSG(wxT("Couldn't select a bitmap into wxMemoryDC"));
162 }
163 else if (!m_hOldBitmap)
164 {
165 m_hOldBitmap = hBmp;
166 }
167 } // end of wxMemoryDC::SelectObject
168
169 void wxMemoryDC::DoGetSize(
170 int* pWidth
171 , int* pHeight
172 ) const
173 {
174 if (!m_vSelectedBitmap.Ok())
175 {
176 *pWidth = 0;
177 *pHeight = 0;
178 return;
179 }
180 *pWidth = m_vSelectedBitmap.GetWidth();
181 *pHeight = m_vSelectedBitmap.GetHeight();
182 } // end of wxMemoryDC::DoGetSize
183
184