+
+// ----------------------------------------------------------------------------
+// wxBitmapDataObject supports CF_DIB format
+// ----------------------------------------------------------------------------
+
+size_t wxBitmapDataObject::GetDataSize() const
+{
+ return wxConvertBitmapToDIB(NULL, GetBitmap());
+}
+
+bool wxBitmapDataObject::GetDataHere(void *buf) const
+{
+ return wxConvertBitmapToDIB((LPBITMAPINFO)buf, GetBitmap()) != 0;
+}
+
+bool wxBitmapDataObject::SetData(size_t WXUNUSED(len), const void *buf)
+{
+ wxBitmap bitmap(wxConvertDIBToBitmap((const LPBITMAPINFO)buf));
+
+ if ( !bitmap.Ok() ) {
+ wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
+
+ return FALSE;
+ }
+
+ SetBitmap(bitmap);
+
+ return TRUE;
+}
+
+// ----------------------------------------------------------------------------
+// wxBitmapDataObject2 supports CF_BITMAP format
+// ----------------------------------------------------------------------------
+
+// the bitmaps aren't passed by value as other types of data (i.e. by copying
+// the data into a global memory chunk and passing it to the clipboard or
+// another application or whatever), but by handle, so these generic functions
+// don't make much sense to them.
+
+size_t wxBitmapDataObject2::GetDataSize() const
+{
+ return 0;
+}
+
+bool wxBitmapDataObject2::GetDataHere(void *pBuf) const
+{
+ // we put a bitmap handle into pBuf
+ *(WXHBITMAP *)pBuf = GetBitmap().GetHBITMAP();
+
+ return TRUE;
+}
+
+bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len), const void *pBuf)
+{
+ HBITMAP hbmp = *(HBITMAP *)pBuf;
+
+ BITMAP bmp;
+ if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
+ {
+ wxLogLastError(wxT("GetObject(HBITMAP)"));
+ }
+
+ wxBitmap bitmap(bmp.bmWidth, bmp.bmHeight, bmp.bmPlanes);
+ bitmap.SetHBITMAP((WXHBITMAP)hbmp);
+
+ if ( !bitmap.Ok() ) {
+ wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
+
+ return FALSE;
+ }
+
+ SetBitmap(bitmap);
+
+ return TRUE;
+}
+
+#if 0
+
+size_t wxBitmapDataObject::GetDataSize(const wxDataFormat& format) const
+{
+ if ( format.GetFormatId() == CF_DIB )
+ {
+ // create the DIB
+ ScreenHDC hdc;
+
+ // shouldn't be selected into a DC or GetDIBits() would fail
+ wxASSERT_MSG( !m_bitmap.GetSelectedInto(),
+ wxT("can't copy bitmap selected into wxMemoryDC") );
+
+ // first get the info
+ BITMAPINFO bi;
+ if ( !GetDIBits(hdc, (HBITMAP)m_bitmap.GetHBITMAP(), 0, 0,
+ NULL, &bi, DIB_RGB_COLORS) )
+ {
+ wxLogLastError(wxT("GetDIBits(NULL)"));
+
+ return 0;
+ }
+
+ return sizeof(BITMAPINFO) + bi.bmiHeader.biSizeImage;
+ }
+ else // CF_BITMAP
+ {
+ // no data to copy - we don't pass HBITMAP via global memory
+ return 0;
+ }
+}
+
+bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format,
+ void *pBuf) const
+{
+ wxASSERT_MSG( m_bitmap.Ok(), wxT("copying invalid bitmap") );
+
+ HBITMAP hbmp = (HBITMAP)m_bitmap.GetHBITMAP();
+ if ( format.GetFormatId() == CF_DIB )
+ {
+ // create the DIB
+ ScreenHDC hdc;
+
+ // shouldn't be selected into a DC or GetDIBits() would fail
+ wxASSERT_MSG( !m_bitmap.GetSelectedInto(),
+ wxT("can't copy bitmap selected into wxMemoryDC") );
+
+ // first get the info
+ BITMAPINFO *pbi = (BITMAPINFO *)pBuf;
+ if ( !GetDIBits(hdc, hbmp, 0, 0, NULL, pbi, DIB_RGB_COLORS) )
+ {
+ wxLogLastError(wxT("GetDIBits(NULL)"));
+
+ return 0;
+ }
+
+ // and now copy the bits
+ if ( !GetDIBits(hdc, hbmp, 0, pbi->bmiHeader.biHeight, pbi + 1,
+ pbi, DIB_RGB_COLORS) )
+ {
+ wxLogLastError(wxT("GetDIBits"));
+
+ return FALSE;
+ }
+ }
+ else // CF_BITMAP
+ {
+ // we put a bitmap handle into pBuf
+ *(HBITMAP *)pBuf = hbmp;
+ }
+
+ return TRUE;
+}
+
+bool wxBitmapDataObject::SetData(const wxDataFormat& format,
+ size_t size, const void *pBuf)
+{
+ HBITMAP hbmp;
+ if ( format.GetFormatId() == CF_DIB )
+ {
+ // here we get BITMAPINFO struct followed by the actual bitmap bits and
+ // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
+ ScreenHDC hdc;
+
+ BITMAPINFO *pbmi = (BITMAPINFO *)pBuf;
+ BITMAPINFOHEADER *pbmih = &pbmi->bmiHeader;
+ hbmp = CreateDIBitmap(hdc, pbmih, CBM_INIT,
+ pbmi + 1, pbmi, DIB_RGB_COLORS);
+ if ( !hbmp )
+ {
+ wxLogLastError(wxT("CreateDIBitmap"));
+ }
+
+ m_bitmap.SetWidth(pbmih->biWidth);
+ m_bitmap.SetHeight(pbmih->biHeight);
+ }
+ else // CF_BITMAP
+ {
+ // it's easy with bitmaps: we pass them by handle
+ hbmp = *(HBITMAP *)pBuf;
+
+ BITMAP bmp;
+ if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
+ {
+ wxLogLastError(wxT("GetObject(HBITMAP)"));
+ }
+
+ m_bitmap.SetWidth(bmp.bmWidth);
+ m_bitmap.SetHeight(bmp.bmHeight);
+ m_bitmap.SetDepth(bmp.bmPlanes);
+ }
+
+ m_bitmap.SetHBITMAP((WXHBITMAP)hbmp);
+
+ wxASSERT_MSG( m_bitmap.Ok(), wxT("pasting invalid bitmap") );
+
+ return TRUE;
+}
+
+#endif // 0
+
+// ----------------------------------------------------------------------------
+// wxFileDataObject
+// ----------------------------------------------------------------------------
+
+bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *pData)
+{
+ m_filenames.Empty();
+
+ // the documentation states that the first member of DROPFILES structure is
+ // a "DWORD offset of double NUL terminated file list". What they mean by
+ // this (I wonder if you see it immediately) is that the list starts at
+ // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
+ // to use DragQueryFile to work with this structure, but not told where and
+ // how to get HDROP.
+ HDROP hdrop = (HDROP)pData; // NB: it works, but I'm not sure about it
+
+ // get number of files (magic value -1)
+ UINT nFiles = ::DragQueryFile(hdrop, (unsigned)-1, NULL, 0u);
+
+ wxCHECK_MSG ( nFiles != (UINT)-1, FALSE, wxT("wrong HDROP handle") );
+
+ // for each file get the length, allocate memory and then get the name
+ wxString str;
+ UINT len, n;
+ for ( n = 0; n < nFiles; n++ ) {
+ // +1 for terminating NUL
+ len = ::DragQueryFile(hdrop, n, NULL, 0) + 1;
+
+ UINT len2 = ::DragQueryFile(hdrop, n, str.GetWriteBuf(len), len);
+ str.UngetWriteBuf();
+ m_filenames.Add(str);
+
+ if ( len2 != len - 1 ) {
+ wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned\
+ %d characters, %d expected."), len2, len - 1);
+ }
+ }
+
+ return TRUE;
+}
+
+void wxFileDataObject::AddFile(const wxString& file)
+{
+ // just add file to filenames array
+ // all useful data (such as DROPFILES struct) will be
+ // created later as necessary
+ m_filenames.Add(file);
+}
+
+size_t wxFileDataObject::GetDataSize() const
+{
+ // size returned will be the size of the DROPFILES structure,
+ // plus the list of filesnames (null byte separated), plus
+ // a double null at the end
+
+ // if no filenames in list, size is 0
+ if ( m_filenames.GetCount() == 0 )
+ return 0;
+
+ // inital size of DROPFILES struct + null byte
+ size_t sz = sizeof(DROPFILES) + 1;
+
+ size_t count = m_filenames.GetCount();
+ for ( size_t i = 0; i < count; i++ )
+ {
+ // add filename length plus null byte
+ sz += m_filenames[i].Len() + 1;
+ }
+
+ return sz;
+}
+
+bool wxFileDataObject::GetDataHere(void *pData) const
+{
+ // pData points to an externally allocated memory block
+ // created using the size returned by GetDataSize()
+
+ // if pData is NULL, or there are no files, return
+ if ( !pData || m_filenames.GetCount() == 0 )
+ return FALSE;
+
+ // convert data pointer to a DROPFILES struct pointer
+ LPDROPFILES pDrop = (LPDROPFILES) pData;
+
+ // initialize DROPFILES struct
+ pDrop->pFiles = sizeof(DROPFILES);
+ pDrop->fNC = FALSE; // not non-client coords
+#if wxUSE_UNICODE
+ pDrop->fWide = TRUE;
+#else // ANSI
+ pDrop->fWide = FALSE;
+#endif // Unicode/Ansi
+
+ // set start of filenames list (null separated)
+ wxChar *pbuf = (wxChar*) ((BYTE *)pDrop + sizeof(DROPFILES));
+
+ size_t count = m_filenames.GetCount();
+ for (size_t i = 0; i < count; i++ )
+ {
+ // copy filename to pbuf and add null terminator
+ size_t len = m_filenames[i].Len();
+ memcpy(pbuf, m_filenames[i], len);
+ pbuf += len;
+ *pbuf++ = wxT('\0');
+ }
+
+ *pbuf = wxT('\0'); // add final null terminator
+
+ return TRUE;