|
|
1.1 root 1: // This is a part of the Microsoft Foundation Classes C++ library.
2: // Copyright (C) 1992 Microsoft Corporation
3: // All rights reserved.
4: //
5: // This source code is only intended as a supplement to the
6: // Microsoft Foundation Classes Reference and Microsoft
7: // QuickHelp documentation provided with the library.
8: // See these sources for detailed information regarding the
9: // Microsoft Foundation Classes product.
10:
11:
12: #include "afxwin.h"
13: #pragma hdrstop
14:
15: #include "winhand_.h"
16:
17: #ifdef AFX_CORE_SEG
18: #pragma code_seg(AFX_CORE_SEG)
19: #endif
20:
21: #ifdef _DEBUG
22: #undef THIS_FILE
23: static char BASED_CODE THIS_FILE[] = __FILE__;
24: #define new DEBUG_NEW
25: #endif
26:
27: /////////////////////////////////////////////////////////////////////////////
28: // Standard exception processing
29:
30: IMPLEMENT_DYNAMIC(CResourceException, CException)
31: static CResourceException NEAR simpleResourceException;
32: void AfxThrowResourceException()
33: { afxExceptionContext.Throw(&simpleResourceException, TRUE); }
34:
35:
36: /////////////////////////////////////////////////////////////////////////////
37: // Diagnostic Output
38: #ifdef _DEBUG
39: CDumpContext& operator<<(CDumpContext& dc, SIZE size)
40: {
41: return dc << "(" << size.cx << " x " << size.cy << ")";
42: }
43:
44: CDumpContext& operator<<(CDumpContext& dc, POINT point)
45: {
46: return dc << "(" << point.x << ", " << point.y << ")";
47: }
48:
49: CDumpContext& operator<<(CDumpContext& dc, const RECT& rect)
50: {
51: return dc << "(L " << rect.left << ", T " << rect.top << ", R " <<
52: rect.right << ", B " << rect.bottom << ")";
53: }
54: #endif //_DEBUG
55:
56:
57:
58: /////////////////////////////////////////////////////////////////////////////
59: // CDC
60:
61: IMPLEMENT_DYNAMIC(CDC, CObject)
62:
63: // Map from HDC to CDC*
64: class NEAR CDCHandleMap : public CHandleMap
65: {
66: public:
67: CObject* NewTempObject(HANDLE h)
68: {
69: // don't add in permanent
70: CDC* p = new CDC();
71: p->m_hDC = (HDC)h; // set after constructed
72: return p;
73: }
74: void DeleteTempObject(CObject* ob)
75: {
76: // destructor doesn't do anything
77: ASSERT(ob->IsKindOf(RUNTIME_CLASS(CDC)));
78: ((CDC*)ob)->m_hDC = NULL;
79: delete ob;
80: }
81: };
82: static CDCHandleMap NEAR deviceMap;
83:
84: #ifdef _DEBUG
85: void CDC::AssertValid() const
86: {
87: CObject::AssertValid();
88: }
89:
90: void CDC::Dump(CDumpContext& dc) const
91: {
92: CObject::Dump(dc);
93: dc << "m_hDC = " << m_hDC;
94: }
95: #endif
96:
97: CDC*
98: CDC::FromHandle(HDC hDC)
99: {
100: return (CDC*)deviceMap.FromHandle(hDC);
101: }
102:
103: void
104: CDC::DeleteTempMap()
105: {
106: deviceMap.DeleteTemp();
107: }
108:
109: BOOL
110: CDC::Attach(HDC hDC)
111: {
112: ASSERT(m_hDC == NULL); // only attach once, detach on destroy
113: if (hDC == NULL)
114: return FALSE;
115: deviceMap.SetPermanent(m_hDC = hDC, this);
116: return TRUE;
117: }
118:
119: HDC CDC::Detach()
120: {
121: HDC hDC;
122: if ((hDC = m_hDC) != NULL)
123: deviceMap.RemovePermanent(m_hDC);
124: m_hDC = NULL;
125: return hDC;
126: }
127:
128: BOOL CDC::DeleteDC()
129: {
130: if (m_hDC == NULL)
131: return FALSE;
132: return ::DeleteDC(Detach());
133: }
134:
135: CDC::~CDC()
136: {
137: if (m_hDC != NULL)
138: ::DeleteDC(Detach());
139: }
140:
141: /////////////////////////////////////////////////////////////////////////////
142: // Out-of-line routines
143:
144: CGdiObject*
145: CDC::SelectGdiObject(HDC hDC, HANDLE h)
146: {
147: return CGdiObject::FromHandle(::SelectObject(hDC, h));
148: }
149:
150: CPalette*
151: CDC::SelectPalette(CPalette* pPalette, BOOL bForceBackground)
152: {
153: return (CPalette*) CGdiObject::FromHandle(
154: ::SelectPalette(m_hDC, (HPALETTE)pPalette->m_hObject, bForceBackground));
155: }
156:
1.1.1.2 ! root 157: #ifdef _NTWIN
! 158: // Win 3.0 compatible API for NT version.
! 159: int
! 160: CDC::StartDoc(LPCSTR pDocName)
! 161: {
! 162: DOCINFO di;
! 163: di.cbSize = sizeof(DOCINFO);
! 164: di.lpszDocName = pDocName;
! 165: di.lpszOutput = NULL;
! 166: return StartDoc(&di);
! 167: }
! 168: #endif
! 169:
1.1 root 170: /////////////////////////////////////////////////////////////////////////////
171: // Helper DCs
172:
173: IMPLEMENT_DYNAMIC(CClientDC, CDC)
174: IMPLEMENT_DYNAMIC(CWindowDC, CDC)
175: IMPLEMENT_DYNAMIC(CPaintDC, CDC)
176: IMPLEMENT_DYNAMIC(CMetaFileDC, CDC)
177:
178: #ifdef _DEBUG
179: void CClientDC::AssertValid() const
180: {
181: CDC::AssertValid();
182: ASSERT(IsWindow(m_hWnd));
183: }
184:
185: void CClientDC::Dump(CDumpContext& dc) const
186: {
187: CDC::Dump(dc);
188: dc << " m_hWnd = " << (UINT)m_hWnd;
189: }
190: #endif
191:
192: CClientDC::CClientDC(CWnd* pWnd)
193: {
194: if (!Attach(::GetDC(m_hWnd = pWnd->GetSafeHwnd())))
195: AfxThrowResourceException();
196: }
197:
198: CClientDC::~CClientDC()
199: {
200: ASSERT(m_hDC != NULL);
201: ::ReleaseDC(m_hWnd, Detach());
202: }
203:
204:
205: #ifdef _DEBUG
206: void CWindowDC::AssertValid() const
207: {
208: CDC::AssertValid();
209: ASSERT(::IsWindow(m_hWnd));
210: }
211:
212: void CWindowDC::Dump(CDumpContext& dc) const
213: {
214: CDC::Dump(dc);
215: dc << " m_hWnd = " << (UINT)m_hWnd;
216: }
217: #endif
218:
219: CWindowDC::CWindowDC(CWnd* pWnd)
220: {
221: if (!Attach(::GetWindowDC(m_hWnd = pWnd->GetSafeHwnd())))
222: AfxThrowResourceException();
223: }
224:
225: CWindowDC::~CWindowDC()
226: {
227: ASSERT(m_hDC != NULL);
228: ::ReleaseDC(m_hWnd, Detach());
229: }
230:
231: #ifdef _DEBUG
232: void CPaintDC::AssertValid() const
233: {
234: CDC::AssertValid();
235: ASSERT(::IsWindow(m_hWnd));
236: }
237:
238: void CPaintDC::Dump(CDumpContext& dc) const
239: {
240: CDC::Dump(dc);
241: dc << "\nm_hWnd = " << (UINT)m_hWnd;
242: dc << "\nm_ps.hdc = " << (UINT)m_ps.hdc;
243: dc << "\nm_ps.fErase = " << m_ps.fErase;
244: dc << "\nm_ps.rcPaint = " << (CRect)m_ps.rcPaint;
245: }
246: #endif
247:
248: CPaintDC::CPaintDC(CWnd* pWnd)
249: {
250: ASSERT_VALID(pWnd);
251: if (!Attach(::BeginPaint(m_hWnd = pWnd->m_hWnd, &m_ps)))
252: AfxThrowResourceException();
253: }
254:
255: CPaintDC::~CPaintDC()
256: {
257: ASSERT(m_hDC != NULL);
258: ::EndPaint(m_hWnd, &m_ps);
259: Detach();
260: }
261:
262: /////////////////////////////////////////////////////////////////////////////
263: // CGdiObject
264:
265: IMPLEMENT_DYNAMIC(CGdiObject, CObject)
266:
267: // Map from H??? to CGdiObject*
268:
269: class NEAR CGdiHandleMap : public CHandleMap
270: {
271: public:
272: CObject* NewTempObject(HANDLE hObject)
273: {
274: // don't add in permanent
275: CGdiObject* p = new CGdiObject;
276: p->m_hObject = hObject; // set after constructed
277: return p;
278: }
279: void DeleteTempObject(CObject* ob)
280: {
281: ASSERT(ob->IsKindOf(RUNTIME_CLASS(CGdiObject)));
282: ((CGdiObject*)ob)->m_hObject = NULL;
283: // clear before destructed
284: delete ob;
285: }
286: };
287: static CGdiHandleMap NEAR gdiMap;
288:
289: #ifdef _DEBUG
290: void CGdiObject::Dump(CDumpContext& dc) const
291: {
292: CObject::Dump(dc);
293: dc << " m_hObject = " << (UINT)m_hObject;
294: }
295: #endif
296:
297: CGdiObject*
298: CGdiObject::FromHandle(HANDLE h)
299: {
300: return (CGdiObject*)gdiMap.FromHandle(h);
301: }
302:
303: void
304: CGdiObject::DeleteTempMap()
305: {
306: gdiMap.DeleteTemp();
307: }
308:
309: BOOL
310: CGdiObject::Attach(HANDLE hObject)
311: {
312: ASSERT(m_hObject == NULL); // only attach once, detach on destroy
313: if (hObject == NULL)
314: return FALSE;
315: gdiMap.SetPermanent(m_hObject = hObject, this);
316: return TRUE;
317: }
318:
319: HANDLE CGdiObject::Detach()
320: {
321: HANDLE hObject;
322: if ((hObject = m_hObject) != NULL)
323: {
324: gdiMap.RemovePermanent(m_hObject);
325: // because of stock objects, it may be in temporary map too
326: gdiMap.RemoveTemporary(m_hObject);
327: }
328: m_hObject = NULL;
329: return hObject;
330: }
331:
332: BOOL CGdiObject::DeleteObject()
333: {
334: if (m_hObject == NULL)
335: return FALSE;
336: return ::DeleteObject(Detach());
337: }
338:
339: CGdiObject::~CGdiObject()
340: {
341: DeleteObject();
342: }
343:
344: /////////////////////////////////////////////////////////////////////////////
345: // Standard GDI objects
346:
347: IMPLEMENT_DYNAMIC(CPen, CGdiObject)
348: IMPLEMENT_DYNAMIC(CBrush, CGdiObject)
349: IMPLEMENT_DYNAMIC(CFont, CGdiObject)
350: IMPLEMENT_DYNAMIC(CBitmap, CGdiObject)
351: IMPLEMENT_DYNAMIC(CPalette, CGdiObject)
352: IMPLEMENT_DYNAMIC(CRgn, CGdiObject)
353:
354: /////////////////////////////////////////////////////////////////////////////
355: // Out-of-line constructors
356:
357: ////////////////////////////////////////////
358: // CPen
359:
360: CPen::CPen(int nPenStyle, int nWidth, DWORD crColor)
361: {
362: if (!Attach(::CreatePen(nPenStyle, nWidth, crColor)))
363: AfxThrowResourceException();
364: }
365:
366: ////////////////////////////////////////////
367: // CBrush
368:
369: CBrush::CBrush(DWORD crColor)
370: {
371: if (!Attach(::CreateSolidBrush(crColor)))
372: AfxThrowResourceException();
373: }
374:
375: CBrush::CBrush(int nIndex, DWORD crColor)
376: {
377: if (!Attach(::CreateHatchBrush(nIndex, crColor)))
378: AfxThrowResourceException();
379: }
380:
381: CBrush::CBrush(CBitmap* pBitmap)
382: {
383: if (!Attach(::CreatePatternBrush((HBITMAP)pBitmap->m_hObject)))
384: AfxThrowResourceException();
385: }
386:
387: /////////////////////////////////////////////////////////////////////////////
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.