wxBitmapRefData::~wxBitmapRefData()
{
- if (m_selectedInto)
- {
- wxChar buf[200];
- wxSprintf(buf, _T("Bitmap was deleted without selecting out of wxMemoryDC %X."), (unsigned int) m_selectedInto);
- wxFatalError(buf);
- }
- if (m_hBitmap)
- {
- DeleteObject((HBITMAP) m_hBitmap);
- }
- m_hBitmap = 0 ;
+ wxASSERT_MSG( !m_selectedInto,
+ wxT("deleting bitmap still selected into wxMemoryDC") );
- if (m_bitmapMask)
- delete m_bitmapMask;
- m_bitmapMask = NULL;
+ if ( m_hBitmap)
+ DeleteObject((HBITMAP) m_hBitmap);
+ if ( m_bitmapMask )
+ delete m_bitmapMask;
}
// ----------------------------------------------------------------------------
wxList wxBitmap::sm_handlers;
-wxBitmap::wxBitmap()
+// this function should be called from all wxBitmap ctors
+void wxBitmap::Init()
{
- if ( wxTheBitmapList )
- wxTheBitmapList->AddBitmap(this);
+ // m_refData = NULL; done in the base class ctor
+
+ if ( wxTheBitmapList )
+ wxTheBitmapList->AddBitmap(this);
}
-wxBitmap::wxBitmap(const wxBitmap& bitmap)
+bool wxBitmap::CopyFromIcon(const wxIcon& icon)
{
- wxIcon *icon = wxDynamicCast(&bitmap, wxIcon);
- if ( icon )
- {
- HDC hdc = ::CreateCompatibleDC(NULL); // screen DC
- HBITMAP hbitmap = ::CreateCompatibleBitmap(hdc,
- icon->GetWidth(),
- icon->GetHeight());
- ::SelectObject(hdc, hbitmap);
- ::DrawIcon(hdc, 0, 0, (HICON)icon->GetHICON());
+ UnRef();
- ::DeleteDC(hdc);
+ if ( !icon.Ok() )
+ return FALSE;
- SetHBITMAP((WXHBITMAP)hbitmap);
- }
- else
+ int width = icon.GetWidth(),
+ height = icon.GetHeight();
+
+ HICON hicon = (HICON) icon.GetHICON();
+
+ // GetIconInfo() doesn't exist under Win16 and I don't know any other way
+ // to create a bitmap from icon there - but using this way we won't have
+ // the mask (FIXME)
+#ifdef __WIN16__
+ // copy the icon to the bitmap
+ HDC hdcScreen = ::GetDC((HWND)NULL);
+ HDC hdc = ::CreateCompatibleDC(hdcScreen);
+ HBITMAP hbitmap = ::CreateCompatibleBitmap(hdcScreen, width, height);
+ HBITMAP hbmpOld = (HBITMAP)::SelectObject(hdc, hbitmap);
+
+ ::DrawIcon(hdc, 0, 0, hicon);
+
+ ::SelectObject(hdc, hbmpOld);
+ ::DeleteDC(hdc);
+ ::ReleaseDC((HWND)NULL, hdcScreen);
+#else // Win32
+ ICONINFO iconInfo;
+ if ( !GetIconInfo(hicon, &iconInfo) )
{
- Ref(bitmap);
+ wxLogLastError("GetIconInfo");
+
+ return FALSE;
}
- if ( wxTheBitmapList )
- wxTheBitmapList->AddBitmap(this);
+ HBITMAP hbitmap = iconInfo.hbmColor;
+
+ wxMask *mask = new wxMask;
+ mask->SetMaskBitmap((WXHBITMAP)iconInfo.hbmMask);
+#endif // Win16/32
+
+ m_refData = new wxBitmapRefData;
+
+ M_BITMAPDATA->m_width = width;
+ M_BITMAPDATA->m_height = height;
+ M_BITMAPDATA->m_depth = wxDisplayDepth();
+
+ M_BITMAPDATA->m_hBitmap = (WXHBITMAP)hbitmap;
+ M_BITMAPDATA->m_ok = TRUE;
+
+#ifndef __WIN16__
+ SetMask(mask);
+#endif // !Win16
+
+ return TRUE;
}
wxBitmap::~wxBitmap()
bool wxBitmap::FreeResource(bool WXUNUSED(force))
{
if ( !M_BITMAPDATA )
- return FALSE;
+ return FALSE;
+
+ wxASSERT_MSG( !M_BITMAPDATA->m_selectedInto,
+ wxT("freeing bitmap still selected into wxMemoryDC") );
- if (M_BITMAPDATA->m_selectedInto)
- {
- wxChar buf[200];
- wxSprintf(buf, _T("Bitmap %X was deleted without selecting out of wxMemoryDC %X."), (unsigned int) this, (unsigned int) M_BITMAPDATA->m_selectedInto);
- wxFatalError(buf);
- }
if (M_BITMAPDATA->m_hBitmap)
{
DeleteObject((HBITMAP) M_BITMAPDATA->m_hBitmap);
wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
{
- m_refData = new wxBitmapRefData;
+ Init();
- M_BITMAPDATA->m_width = the_width ;
- M_BITMAPDATA->m_height = the_height ;
- M_BITMAPDATA->m_depth = no_bits ;
- M_BITMAPDATA->m_numColors = 0;
+ m_refData = new wxBitmapRefData;
- M_BITMAPDATA->m_hBitmap = (WXHBITMAP) CreateBitmap(the_width, the_height, 1, no_bits, bits);
+ M_BITMAPDATA->m_width = the_width ;
+ M_BITMAPDATA->m_height = the_height ;
+ M_BITMAPDATA->m_depth = no_bits ;
+ M_BITMAPDATA->m_numColors = 0;
- if (M_BITMAPDATA->m_hBitmap)
- M_BITMAPDATA->m_ok = TRUE;
- else
- M_BITMAPDATA->m_ok = FALSE;
+ M_BITMAPDATA->m_hBitmap = (WXHBITMAP) CreateBitmap(the_width, the_height, 1, no_bits, bits);
- M_BITMAPDATA->m_selectedInto = NULL;
+ if (M_BITMAPDATA->m_hBitmap)
+ M_BITMAPDATA->m_ok = TRUE;
+ else
+ M_BITMAPDATA->m_ok = FALSE;
- if ( wxTheBitmapList )
- wxTheBitmapList->AddBitmap(this);
+ M_BITMAPDATA->m_selectedInto = NULL;
}
// Create from XPM data
wxBitmap::wxBitmap(char **data, wxControl *WXUNUSED(anItem))
{
- (void) Create((void *)data, wxBITMAP_TYPE_XPM_DATA, 0, 0, 0);
+ Init();
+
+ (void)Create((void *)data, wxBITMAP_TYPE_XPM_DATA, 0, 0, 0);
}
wxBitmap::wxBitmap(int w, int h, int d)
{
- (void)Create(w, h, d);
+ Init();
- if ( wxTheBitmapList )
- wxTheBitmapList->AddBitmap(this);
+ (void)Create(w, h, d);
}
wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth)
{
- (void) Create(data, type, width, height, depth);
+ Init();
- if ( wxTheBitmapList )
- wxTheBitmapList->AddBitmap(this);
+ (void) Create(data, type, width, height, depth);
}
wxBitmap::wxBitmap(const wxString& filename, long type)
{
- LoadFile(filename, (int)type);
+ Init();
- if ( wxTheBitmapList )
- wxTheBitmapList->AddBitmap(this);
+ LoadFile(filename, (int)type);
}
bool wxBitmap::Create(int w, int h, int d)
wxBitmapHandler *handler = FindHandler(type);
if ( handler == NULL ) {
- wxLogWarning(_T("no bitmap handler for type %d defined."), type);
+ wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
return FALSE;
}
wxBitmapHandler *handler = FindHandler(type);
if ( handler == NULL ) {
- wxLogWarning(_T("no bitmap handler for type %d defined."), type);
+ wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
return FALSE;
}
wxBitmapHandler *handler = FindHandler(type);
if ( handler == NULL ) {
- wxLogWarning(_T("no bitmap handler for type %d defined."), type);
+ wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
return FALSE;
}
void wxBitmap::SetHBITMAP(WXHBITMAP bmp)
{
if (!M_BITMAPDATA)
- m_refData = new wxBitmapRefData;
+ m_refData = new wxBitmapRefData;
M_BITMAPDATA->m_hBitmap = bmp;
+ M_BITMAPDATA->m_ok = bmp != 0;
}
void wxBitmap::AddHandler(wxBitmapHandler *handler)
}
// it's probably not found
- wxLogError(_T("Can't load bitmap '%s' from resources! Check .rc file."), name.c_str());
+ wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."), name.c_str());
return FALSE;
}