| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: os2/dataobj.cpp |
| 3 | // Purpose: implementation of wx[I]DataObject class |
| 4 | // Author: David Webster |
| 5 | // Modified by: |
| 6 | // Created: 10/21/99 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) 1999 David Webster |
| 9 | // Licence: wxWindows licence |
| 10 | /////////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 20 | #ifdef __GNUG__ |
| 21 | #pragma implementation "dataobj.h" |
| 22 | #endif |
| 23 | |
| 24 | // For compilers that support precompilation, includes "wx.h". |
| 25 | #include "wx/wxprec.h" |
| 26 | |
| 27 | #ifndef WX_PRECOMP |
| 28 | #include "wx/defs.h" |
| 29 | #include "wx/intl.h" |
| 30 | #include "wx/log.h" |
| 31 | #endif |
| 32 | |
| 33 | #if wxUSE_DATAOBJ |
| 34 | |
| 35 | #include "wx/dataobj.h" |
| 36 | #include "wx/mstream.h" |
| 37 | #include "wx/image.h" |
| 38 | |
| 39 | #define INCL_DOS |
| 40 | #include <os2.h> |
| 41 | |
| 42 | // ---------------------------------------------------------------------------- |
| 43 | // functions |
| 44 | // ---------------------------------------------------------------------------- |
| 45 | |
| 46 | // ---------------------------------------------------------------------------- |
| 47 | // wxDataFormat |
| 48 | // ---------------------------------------------------------------------------- |
| 49 | |
| 50 | wxString wxDataFormat::GetId() const |
| 51 | { |
| 52 | wxChar zBuf[256]; |
| 53 | wxString sRet; |
| 54 | |
| 55 | ::WinQueryAtomName( ::WinQuerySystemAtomTable() |
| 56 | ,m_uFormat |
| 57 | ,(PSZ)zBuf |
| 58 | ,256 |
| 59 | ); |
| 60 | sRet = zBuf; |
| 61 | return sRet; |
| 62 | } // end of wxDataFormat::GetId() |
| 63 | |
| 64 | void wxDataFormat::SetId ( |
| 65 | const wxChar* zId |
| 66 | ) |
| 67 | { |
| 68 | m_uFormat = ::WinAddAtom( ::WinQuerySystemAtomTable() |
| 69 | ,(PSZ)zId |
| 70 | ); |
| 71 | } // end of wxDataFormat::SetId |
| 72 | |
| 73 | class CIDataObject |
| 74 | { |
| 75 | public: |
| 76 | CIDataObject(wxDataObject* pDataObject); |
| 77 | ~CIDataObject(); |
| 78 | |
| 79 | // |
| 80 | // Operations on the DRAGITEM struct |
| 81 | // |
| 82 | bool GetData( const wxDataFormat& rFormat |
| 83 | ,char* pzBuffer |
| 84 | ,ULONG ulLen |
| 85 | ); |
| 86 | void GetDataHere( const wxDataFormat& rFormat |
| 87 | ,char* pzBuffer |
| 88 | ,ULONG ulLen |
| 89 | ); |
| 90 | void QueryGetData(const wxDataFormat& rFormat); |
| 91 | void SetData( const wxDataFormat& rFormat |
| 92 | ,char* pzBuffer |
| 93 | ); |
| 94 | private: |
| 95 | wxDataObject* m_pDataObject; // pointer to C++ class we belong to |
| 96 | DRAGITEM m_vDragItem; |
| 97 | }; // end of CLASS CIDataObject |
| 98 | |
| 99 | bool CIDataObject::GetData ( const wxDataFormat& rFormat, |
| 100 | char* pzBuffer, |
| 101 | ULONG ulLen ) |
| 102 | { |
| 103 | QueryGetData(rFormat); |
| 104 | if (rFormat.GetType() == wxDF_INVALID) |
| 105 | return FALSE; |
| 106 | |
| 107 | ULONG ulSize = m_pDataObject->GetDataSize(rFormat); |
| 108 | |
| 109 | if (ulSize == 0) |
| 110 | { |
| 111 | // |
| 112 | // It probably means that the method is just not implemented |
| 113 | // |
| 114 | return FALSE; |
| 115 | } |
| 116 | if (rFormat.GetType() == wxDF_PRIVATE) |
| 117 | { |
| 118 | // |
| 119 | // For custom formats, put the size with the data - alloc the |
| 120 | // space for it |
| 121 | // |
| 122 | ulSize += sizeof(ULONG); |
| 123 | } |
| 124 | |
| 125 | if (ulSize > ulLen) // not enough room to copy |
| 126 | return FALSE; |
| 127 | |
| 128 | // |
| 129 | // Copy the data |
| 130 | // |
| 131 | GetDataHere( rFormat |
| 132 | ,pzBuffer |
| 133 | ,ulSize |
| 134 | ); |
| 135 | return true; |
| 136 | } // end of CIDataObject::GetData |
| 137 | |
| 138 | void CIDataObject::GetDataHere( |
| 139 | const wxDataFormat& rFormat |
| 140 | , char* pzBuffer |
| 141 | , ULONG WXUNUSED(ulLen) |
| 142 | ) |
| 143 | { |
| 144 | m_pDataObject->GetDataHere( rFormat |
| 145 | ,(void*)pzBuffer |
| 146 | ); |
| 147 | } // end of CIDataObject::GetDataHere |
| 148 | |
| 149 | void CIDataObject::QueryGetData ( |
| 150 | const wxDataFormat& rFormat |
| 151 | ) |
| 152 | { |
| 153 | m_pDataObject->IsSupportedFormat(rFormat); |
| 154 | } // end of CIDataObject::QueryGetData |
| 155 | |
| 156 | void CIDataObject::SetData ( |
| 157 | const wxDataFormat& rFormat |
| 158 | , char* pzBuffer |
| 159 | ) |
| 160 | { |
| 161 | ULONG ulSize = 0; |
| 162 | |
| 163 | switch (rFormat.GetType()) |
| 164 | { |
| 165 | case wxDF_TEXT: |
| 166 | case wxDF_OEMTEXT: |
| 167 | case wxDF_FILENAME: |
| 168 | case wxDF_HTML: |
| 169 | ulSize = strlen((const char *)pzBuffer); |
| 170 | break; |
| 171 | |
| 172 | #if wxUSE_UNICODE |
| 173 | case wxDF_UNICODETEXT: |
| 174 | ulSize = ::wcslen((const wchar_t *)pzBuffer); |
| 175 | break; |
| 176 | #endif |
| 177 | |
| 178 | case wxDF_BITMAP: |
| 179 | case wxDF_METAFILE: |
| 180 | case wxDF_ENHMETAFILE: |
| 181 | case wxDF_TIFF: |
| 182 | case wxDF_DIB: |
| 183 | ulSize = 0; // pass via a handle |
| 184 | break; |
| 185 | |
| 186 | |
| 187 | case wxDF_SYLK: |
| 188 | case wxDF_DIF: |
| 189 | case wxDF_PALETTE: |
| 190 | case wxDF_PENDATA: |
| 191 | case wxDF_RIFF: |
| 192 | case wxDF_WAVE: |
| 193 | case wxDF_LOCALE: |
| 194 | //PUNT |
| 195 | break; |
| 196 | |
| 197 | case wxDF_PRIVATE: |
| 198 | size_t* p = (size_t *)pzBuffer; |
| 199 | |
| 200 | ulSize = *p++; |
| 201 | pzBuffer = (char*)p; |
| 202 | break; |
| 203 | } |
| 204 | m_pDataObject->SetData( rFormat |
| 205 | ,ulSize |
| 206 | ,(void*)pzBuffer |
| 207 | ); |
| 208 | } // end of CIDataObject::SetData |
| 209 | |
| 210 | //------------------------------------------------------------------------- |
| 211 | // wxDataObject |
| 212 | //------------------------------------------------------------------------- |
| 213 | |
| 214 | wxDataObject::wxDataObject () |
| 215 | { |
| 216 | m_pDataObject = new DRAGITEM; |
| 217 | } // end of wxDataObject::wxDataObject |
| 218 | |
| 219 | wxDataObject::~wxDataObject () |
| 220 | { |
| 221 | delete m_pDataObject; |
| 222 | } // end of wxDataObject::~wxDataObject |
| 223 | |
| 224 | // ---------------------------------------------------------------------------- |
| 225 | // wxFileDataObject |
| 226 | // ---------------------------------------------------------------------------- |
| 227 | |
| 228 | bool wxFileDataObject::GetDataHere( void* pBuf ) const |
| 229 | { |
| 230 | wxString sFilenames; |
| 231 | |
| 232 | for (size_t i = 0; i < m_filenames.GetCount(); i++) |
| 233 | { |
| 234 | sFilenames += m_filenames[i]; |
| 235 | sFilenames += (wxChar)0; |
| 236 | } |
| 237 | |
| 238 | memcpy(pBuf, sFilenames.mbc_str(), sFilenames.Len() + 1); |
| 239 | return true; |
| 240 | } |
| 241 | |
| 242 | size_t wxFileDataObject::GetDataSize() const |
| 243 | { |
| 244 | size_t nRes = 0; |
| 245 | |
| 246 | for (size_t i = 0; i < m_filenames.GetCount(); i++) |
| 247 | { |
| 248 | nRes += m_filenames[i].Len(); |
| 249 | nRes += 1; |
| 250 | } |
| 251 | |
| 252 | return nRes + 1; |
| 253 | } |
| 254 | |
| 255 | bool wxFileDataObject::SetData( size_t WXUNUSED(nSize), |
| 256 | const void* pBuf ) |
| 257 | { |
| 258 | /* TODO */ |
| 259 | |
| 260 | wxString sFile((const wxChar *)pBuf); /* char, not wxChar */ |
| 261 | |
| 262 | AddFile(sFile); |
| 263 | |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | void wxFileDataObject::AddFile( |
| 268 | const wxString& rFilename |
| 269 | ) |
| 270 | { |
| 271 | m_filenames.Add(rFilename); |
| 272 | } |
| 273 | |
| 274 | // ---------------------------------------------------------------------------- |
| 275 | // wxBitmapDataObject |
| 276 | // ---------------------------------------------------------------------------- |
| 277 | |
| 278 | wxBitmapDataObject::wxBitmapDataObject() |
| 279 | { |
| 280 | Init(); |
| 281 | } |
| 282 | |
| 283 | wxBitmapDataObject::wxBitmapDataObject( |
| 284 | const wxBitmap& rBitmap |
| 285 | ) |
| 286 | : wxBitmapDataObjectBase(rBitmap) |
| 287 | { |
| 288 | Init(); |
| 289 | |
| 290 | DoConvertToPng(); |
| 291 | } |
| 292 | |
| 293 | wxBitmapDataObject::~wxBitmapDataObject() |
| 294 | { |
| 295 | Clear(); |
| 296 | } |
| 297 | |
| 298 | void wxBitmapDataObject::SetBitmap( const wxBitmap& rBitmap ) |
| 299 | { |
| 300 | ClearAll(); |
| 301 | wxBitmapDataObjectBase::SetBitmap(rBitmap); |
| 302 | DoConvertToPng(); |
| 303 | } |
| 304 | |
| 305 | bool wxBitmapDataObject::GetDataHere( void* pBuf ) const |
| 306 | { |
| 307 | if (!m_pngSize) |
| 308 | { |
| 309 | wxFAIL_MSG(wxT("attempt to copy empty bitmap failed")); |
| 310 | return FALSE; |
| 311 | } |
| 312 | memcpy(pBuf, m_pngData, m_pngSize); |
| 313 | return true; |
| 314 | } |
| 315 | |
| 316 | bool wxBitmapDataObject::SetData( |
| 317 | size_t nSize |
| 318 | , const void* pBuf |
| 319 | ) |
| 320 | { |
| 321 | Clear(); |
| 322 | m_pngSize = nSize; |
| 323 | m_pngData = malloc(m_pngSize); |
| 324 | |
| 325 | memcpy(m_pngData, pBuf, m_pngSize); |
| 326 | |
| 327 | #if wxUSE_STREAMS |
| 328 | wxMemoryInputStream vMstream((char*)m_pngData, m_pngSize); |
| 329 | wxImage vImage; |
| 330 | wxPNGHandler vHandler; |
| 331 | |
| 332 | if (!vHandler.LoadFile(&vImage, vMstream)) |
| 333 | { |
| 334 | return FALSE; |
| 335 | } |
| 336 | |
| 337 | m_bitmap = wxBitmap(vImage); |
| 338 | #endif //wxUSE_STREAMS |
| 339 | |
| 340 | return m_bitmap.Ok(); |
| 341 | } |
| 342 | |
| 343 | void wxBitmapDataObject::DoConvertToPng() |
| 344 | { |
| 345 | if (!m_bitmap.Ok()) |
| 346 | return; |
| 347 | |
| 348 | #if wxUSE_STREAMS |
| 349 | wxImage vImage = m_bitmap.ConvertToImage(); |
| 350 | wxPNGHandler vHandler; |
| 351 | wxCountingOutputStream vCount; |
| 352 | |
| 353 | vHandler.SaveFile(&vImage, vCount); |
| 354 | |
| 355 | m_pngSize = vCount.GetSize() + 100; // sometimes the size seems to vary ??? |
| 356 | m_pngData = malloc(m_pngSize); |
| 357 | |
| 358 | wxMemoryOutputStream vMstream((char*) m_pngData, m_pngSize); |
| 359 | |
| 360 | vHandler.SaveFile(&vImage, vMstream ); |
| 361 | #endif |
| 362 | } |
| 363 | |
| 364 | #endif // wxUSE_DATAOBJ |