- // Select old bitmap out of the device context
- if (m_oldBitmap)
- {
- ::SelectObject((HDC) m_hDC, (HBITMAP) m_oldBitmap);
- if (m_selectedBitmap.Ok())
+ // select old bitmap out of the device context
+ if ( m_oldBitmap )
+ {
+ ::SelectObject(GetHdc(), (HBITMAP) m_oldBitmap);
+ if ( m_selectedBitmap.Ok() )
+ {
+ m_selectedBitmap.SetSelectedInto(NULL);
+ m_selectedBitmap = wxNullBitmap;
+ }
+ }
+
+ // check for whether the bitmap is already selected into a device context
+ wxCHECK_RET( !bitmap.GetSelectedInto() ||
+ (bitmap.GetSelectedInto() == this),
+ wxT("Bitmap is selected in another wxMemoryDC, delete the "
+ "first wxMemoryDC or use SelectObject(NULL)") );
+
+ m_selectedBitmap = bitmap;
+ WXHBITMAP hBmp = m_selectedBitmap.GetHBITMAP();
+ if ( !hBmp )
+ return;
+
+ m_selectedBitmap.SetSelectedInto(this);
+ hBmp = (WXHBITMAP)::SelectObject(GetHdc(), (HBITMAP)hBmp);
+
+ if ( !hBmp )
+ {
+ wxLogLastError("SelectObject(memDC, bitmap)");
+
+ wxFAIL_MSG(wxT("Couldn't select a bitmap into wxMemoryDC"));
+ }
+ else if ( !m_oldBitmap )