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