]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dcmemory.cpp
new sample
[wxWidgets.git] / src / msw / dcmemory.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dcmemory.cpp
3 // Purpose: wxMemoryDC class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "dcmemory.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/utils.h"
25 #endif
26
27 #include "wx/msw/private.h"
28
29 #include "wx/dcmemory.h"
30
31 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_DYNAMIC_CLASS(wxMemoryDC, wxDC)
33 #endif
34
35 /*
36 * Memory DC
37 *
38 */
39
40 wxMemoryDC::wxMemoryDC(void)
41 {
42 m_hDC = (WXHDC) ::CreateCompatibleDC((HDC) NULL);
43 m_ok = (m_hDC != 0);
44 m_bOwnsDC = TRUE;
45
46 SetBrush(*wxWHITE_BRUSH);
47 SetPen(*wxBLACK_PEN);
48
49 // the background mode is only used for text background
50 // and is set in DrawText() to OPAQUE as required, other-
51 // wise always TRANSPARENT, RR
52 ::SetBkMode( GetHdc(), TRANSPARENT );
53
54 }
55
56 wxMemoryDC::wxMemoryDC(wxDC *old_dc)
57 {
58 old_dc->BeginDrawing();
59
60 m_hDC = (WXHDC) ::CreateCompatibleDC((HDC) old_dc->GetHDC());
61 m_ok = (m_hDC != 0);
62
63 old_dc->EndDrawing();
64
65 SetBrush(*wxWHITE_BRUSH);
66 SetPen(*wxBLACK_PEN);
67
68 // the background mode is only used for text background
69 // and is set in DrawText() to OPAQUE as required, other-
70 // wise always TRANSPARENT, RR
71 ::SetBkMode( GetHdc(), TRANSPARENT );
72
73 }
74
75 wxMemoryDC::~wxMemoryDC(void)
76 {
77 }
78
79 void wxMemoryDC::SelectObject(const wxBitmap& bitmap)
80 {
81 // Select old bitmap out of the device context
82 if (m_oldBitmap)
83 {
84 ::SelectObject((HDC) m_hDC, (HBITMAP) m_oldBitmap);
85 if (m_selectedBitmap.Ok())
86 {
87 m_selectedBitmap.SetSelectedInto(NULL);
88 m_selectedBitmap = wxNullBitmap;
89 }
90 }
91
92 // Do own check for whether the bitmap is already selected into
93 // a device context
94 if (bitmap.GetSelectedInto() && (bitmap.GetSelectedInto() != this))
95 {
96 wxFatalError(_T("Error in wxMemoryDC::SelectObject\nBitmap is selected in another wxMemoryDC.\nDelete the first wxMemoryDC or use SelectObject(NULL)"));
97 return;
98 }
99
100 // Check if the bitmap has the correct depth for this device context
101 // if (bitmap.Ok() && (bitmap.GetDepth() != GetDepth()))
102 // JACS 11/12/98: disabling this since the Forty Thieves sample
103 // shows this not working properly. In fact, if loading from a resource,
104 // the depth should become the screen depth, so why was it being called?
105 // if (0)
106 // {
107 // // Make a new bitmap that has the correct depth.
108 // wxBitmap newBitmap = bitmap.GetBitmapForDC(* this);
109 //
110 // m_selectedBitmap = newBitmap ;
111 // }
112 // else
113 // {
114 m_selectedBitmap = bitmap;
115 // }
116
117 if (!m_selectedBitmap.Ok())
118 return;
119
120 m_selectedBitmap.SetSelectedInto(this);
121 HBITMAP bm = (HBITMAP) ::SelectObject((HDC) m_hDC, (HBITMAP) m_selectedBitmap.GetHBITMAP());
122
123 if (bm == ERROR)
124 {
125 wxFatalError(_T("Error in wxMemoryDC::SelectObject\nBitmap may not be loaded, or may be selected in another wxMemoryDC.\nDelete the first wxMemoryDC to deselect bitmap."));
126 }
127 else if (!m_oldBitmap)
128 m_oldBitmap = (WXHBITMAP) bm;
129 }
130
131 void wxMemoryDC::DoGetSize(int *width, int *height) const
132 {
133 if (!m_selectedBitmap.Ok())
134 {
135 *width = 0; *height = 0;
136 return;
137 }
138 *width = m_selectedBitmap.GetWidth();
139 *height = m_selectedBitmap.GetHeight();
140 }
141