+}
+
+bool wxBitmapDataObject::SetData(size_t WXUNUSED(len), const void *buf)
+{
+#if wxUSE_WXDIB && !defined(__WXWINCE__)
+ const BITMAPINFO * const pbmi = (const BITMAPINFO *)buf;
+
+ HBITMAP hbmp = wxDIB::ConvertToBitmap(pbmi);
+
+ wxCHECK_MSG( hbmp, FALSE, wxT("pasting/dropping invalid bitmap") );
+
+ const BITMAPINFOHEADER * const pbmih = &pbmi->bmiHeader;
+ wxBitmap bitmap(pbmih->biWidth, pbmih->biHeight, pbmih->biBitCount);
+ bitmap.SetHBITMAP((WXHBITMAP)hbmp);
+
+ // TODO: create wxPalette if the bitmap has any
+
+ SetBitmap(bitmap);
+
+ return true;
+#else
+ wxUnusedVar(buf);
+ return false;
+#endif
+}
+
+// ----------------------------------------------------------------------------
+// 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.IsOk() ) {
+ 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.IsOk(), 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.IsOk(), wxT("pasting invalid bitmap") );
+
+ return true;
+}
+
+#endif // 0
+
+// ----------------------------------------------------------------------------
+// wxFileDataObject
+// ----------------------------------------------------------------------------
+
+bool wxFileDataObject::SetData(size_t WXUNUSED(size),
+ const void *WXUNUSED_IN_WINCE(pData))
+{
+#ifndef __WXWINCE__
+ 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, wxStringBuffer(str, len), len);
+ m_filenames.Add(str);
+
+ if ( len2 != len - 1 ) {
+ wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned\
+ %d characters, %d expected."), len2, len - 1);
+ }
+ }
+
+ return true;
+#else
+ return false;
+#endif
+}
+
+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
+{
+#ifndef __WXWINCE__
+ // 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.empty() )
+ return 0;
+
+#if wxUSE_UNICODE_MSLU
+ size_t sizeOfChar;
+ if ( wxGetOsVersion() == wxOS_WINDOWS_9X )
+ {
+ // Win9x always uses ANSI file names and MSLU doesn't help with this
+ sizeOfChar = 1;
+ }
+ else
+ {
+ sizeOfChar = sizeof(wxChar);
+ }
+#else // !wxUSE_UNICODE_MSLU
+ static const size_t sizeOfChar = sizeof(wxChar);
+#endif // wxUSE_UNICODE_MSLU/!wxUSE_UNICODE_MSLU
+
+ // initial size of DROPFILES struct + null byte
+ size_t sz = sizeof(DROPFILES) + sizeOfChar;
+
+ const size_t count = m_filenames.size();
+ for ( size_t i = 0; i < count; i++ )
+ {
+ // add filename length plus null byte
+ size_t len;
+#if wxUSE_UNICODE_MSLU
+ if ( sizeOfChar == 1 )
+ len = strlen(m_filenames[i].mb_str(*wxConvFileName));
+ else
+#endif // wxUSE_UNICODE_MSLU
+ len = m_filenames[i].length();
+
+ sz += (len + 1) * sizeOfChar;
+ }
+
+ return sz;
+#else
+ return 0;
+#endif
+}
+
+bool wxFileDataObject::GetDataHere(void *WXUNUSED_IN_WINCE(pData)) const
+{
+#ifndef __WXWINCE__
+ // 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.empty() )
+ 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_MSLU
+ pDrop->fWide = wxGetOsVersion() != wxOS_WINDOWS_9X ? TRUE : FALSE;
+#else
+ pDrop->fWide = wxUSE_UNICODE;
+#endif
+
+ const size_t sizeOfChar = pDrop->fWide ? sizeof(wchar_t) : 1;
+
+ // set start of filenames list (null separated)
+ BYTE *pbuf = (BYTE *)(pDrop + 1);
+
+ const size_t count = m_filenames.size();
+ for ( size_t i = 0; i < count; i++ )
+ {
+ // copy filename to pbuf and add null terminator
+ size_t len;
+#if wxUSE_UNICODE_MSLU
+ if ( sizeOfChar == 1 )
+ {
+ wxCharBuffer buf(m_filenames[i].mb_str(*wxConvFileName));
+ len = strlen(buf);
+ memcpy(pbuf, buf, len*sizeOfChar);
+ }
+ else
+#endif // wxUSE_UNICODE_MSLU
+ {
+ len = m_filenames[i].length();
+ memcpy(pbuf, m_filenames[i].t_str(), len*sizeOfChar);
+ }
+
+ pbuf += len*sizeOfChar;
+
+ memset(pbuf, 0, sizeOfChar);
+ pbuf += sizeOfChar;
+ }
+
+ // add final null terminator
+ memset(pbuf, 0, sizeOfChar);
+
+ return true;
+#else
+ return false;
+#endif
+}
+
+// ----------------------------------------------------------------------------
+// wxURLDataObject
+// ----------------------------------------------------------------------------
+
+// Work around bug in Wine headers
+#if defined(__WINE__) && defined(CFSTR_SHELLURL) && wxUSE_UNICODE
+#undef CFSTR_SHELLURL
+#define CFSTR_SHELLURL wxT("CFSTR_SHELLURL")
+#endif
+
+class CFSTR_SHELLURLDataObject : public wxCustomDataObject
+{
+public:
+ CFSTR_SHELLURLDataObject() : wxCustomDataObject(CFSTR_SHELLURL) {}
+
+ virtual size_t GetBufferOffset( const wxDataFormat& WXUNUSED(format) )
+ {
+ return 0;
+ }
+
+ virtual const void* GetSizeFromBuffer( const void* buffer, size_t* size,
+ const wxDataFormat& WXUNUSED(format) )
+ {
+ // CFSTR_SHELLURL is _always_ ANSI text
+ *size = strlen( (const char*)buffer );
+
+ return buffer;
+ }
+
+ virtual void* SetSizeInBuffer( void* buffer, size_t WXUNUSED(size),
+ const wxDataFormat& WXUNUSED(format) )
+ {
+ return buffer;
+ }
+
+ wxDECLARE_NO_COPY_CLASS(CFSTR_SHELLURLDataObject);
+};
+
+
+
+wxURLDataObject::wxURLDataObject(const wxString& url)
+{
+ // we support CF_TEXT and CFSTR_SHELLURL formats which are basically the
+ // same but it seems that some browsers only provide one of them so we have
+ // to support both
+ Add(new wxTextDataObject);
+ Add(new CFSTR_SHELLURLDataObject());
+
+ // we don't have any data yet
+ m_dataObjectLast = NULL;
+
+ if ( !url.empty() )
+ SetURL(url);
+}
+
+bool wxURLDataObject::SetData(const wxDataFormat& format,
+ size_t len,
+ const void *buf)
+{
+ m_dataObjectLast = GetObject(format);
+
+ wxCHECK_MSG( m_dataObjectLast, FALSE,
+ wxT("unsupported format in wxURLDataObject"));
+
+ return m_dataObjectLast->SetData(len, buf);
+}
+
+wxString wxURLDataObject::GetURL() const
+{
+ wxString url;
+ wxCHECK_MSG( m_dataObjectLast, url, wxT("no data in wxURLDataObject") );
+
+ if ( m_dataObjectLast->GetPreferredFormat() == CFSTR_SHELLURL )
+ {
+ const size_t len = m_dataObjectLast->GetDataSize();
+ if ( !len )
+ return wxString();
+
+ // CFSTR_SHELLURL is always ANSI so we need to convert it from it in
+ // Unicode build
+#if wxUSE_UNICODE
+ wxCharBuffer buf(len);
+
+ if ( m_dataObjectLast->GetDataHere(buf.data()) )
+ url = buf;
+#else // !wxUSE_UNICODE
+ // in ANSI build no conversion is necessary
+ m_dataObjectLast->GetDataHere(wxStringBuffer(url, len));
+#endif // wxUSE_UNICODE/!wxUSE_UNICODE
+ }
+ else // must be wxTextDataObject
+ {
+ url = static_cast<wxTextDataObject *>(m_dataObjectLast)->GetText();
+ }
+
+ return url;
+}
+
+void wxURLDataObject::SetURL(const wxString& url)
+{
+ wxCharBuffer urlMB(url.mb_str());
+ if ( urlMB )
+ {
+ const size_t len = strlen(urlMB);
+
+#if !wxUSE_UNICODE
+ // wxTextDataObject takes the number of characters in the string, not
+ // the size of the buffer (which would be len+1)
+ SetData(wxDF_TEXT, len, urlMB);
+#endif // !wxUSE_UNICODE
+
+ // however CFSTR_SHELLURLDataObject doesn't append NUL automatically
+ // but we probably still want to have it on the clipboard (just to be
+ // safe), so do append it
+ SetData(wxDataFormat(CFSTR_SHELLURL), len + 1, urlMB);
+ }
+
+#if wxUSE_UNICODE
+ SetData(wxDF_UNICODETEXT, url.length()*sizeof(wxChar), url.wc_str());
+#endif
+}
+
+// ----------------------------------------------------------------------------
+// private functions
+// ----------------------------------------------------------------------------
+
+#if wxDEBUG_LEVEL
+
+static const wxChar *GetTymedName(DWORD tymed)
+{
+ static wxChar s_szBuf[128];
+ switch ( tymed ) {
+ case TYMED_HGLOBAL: return wxT("TYMED_HGLOBAL");
+ case TYMED_FILE: return wxT("TYMED_FILE");
+ case TYMED_ISTREAM: return wxT("TYMED_ISTREAM");
+ case TYMED_ISTORAGE: return wxT("TYMED_ISTORAGE");
+ case TYMED_GDI: return wxT("TYMED_GDI");
+ case TYMED_MFPICT: return wxT("TYMED_MFPICT");
+ case TYMED_ENHMF: return wxT("TYMED_ENHMF");
+ default:
+ wxSprintf(s_szBuf, wxT("type of media format %ld (unknown)"), tymed);
+ return s_szBuf;
+ }
+}
+
+#endif // Debug
+
+#else // not using OLE at all
+
+// ----------------------------------------------------------------------------
+// wxDataObject
+// ----------------------------------------------------------------------------
+
+#if wxUSE_DATAOBJ
+
+wxDataObject::wxDataObject()
+{
+}
+
+wxDataObject::~wxDataObject()
+{
+}
+
+void wxDataObject::SetAutoDelete()
+{
+}
+
+const wxChar *wxDataObject::GetFormatName(wxDataFormat WXUNUSED(format))
+{
+ return NULL;
+}
+
+#endif // wxUSE_DATAOBJ
+
+#endif // wxUSE_OLE/!wxUSE_OLE
+