]> git.saurik.com Git - wxWidgets.git/blob - src/os2/dataobj.cpp
fixed double deletion (patch #945491)
[wxWidgets.git] / src / os2 / dataobj.cpp
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/intl.h"
29 #endif
30 #include "wx/defs.h"
31
32 #include "wx/log.h"
33 #include "wx/dataobj.h"
34 #include "wx/mstream.h"
35 #include "wx/image.h"
36
37 #define INCL_DOS
38 #include <os2.h>
39
40 // ----------------------------------------------------------------------------
41 // functions
42 // ----------------------------------------------------------------------------
43
44 // ----------------------------------------------------------------------------
45 // wxDataFormat
46 // ----------------------------------------------------------------------------
47
48 wxString wxDataFormat::GetId() const
49 {
50 char zBuf[256];
51 wxString sRet;
52
53 ::WinQueryAtomName( ::WinQuerySystemAtomTable()
54 ,m_uFormat
55 ,zBuf
56 ,256
57 );
58 sRet = zBuf;
59 return sRet;
60 } // end of wxDataFormat::GetId()
61
62 void wxDataFormat::SetId (
63 const wxChar* zId
64 )
65 {
66 m_uFormat = ::WinAddAtom( ::WinQuerySystemAtomTable()
67 ,zId
68 );
69 } // end of wxDataFormat::SetId
70
71 class CIDataObject
72 {
73 public:
74 CIDataObject(wxDataObject* pDataObject);
75 ~CIDataObject();
76
77 //
78 // Operations on the DRAGITEM struct
79 //
80 bool GetData( const wxDataFormat& rFormat
81 ,char* pzBuffer
82 ,ULONG ulLen
83 );
84 void GetDataHere( const wxDataFormat& rFormat
85 ,char* pzBuffer
86 ,ULONG ulLen
87 );
88 void QueryGetData(const wxDataFormat& rFormat);
89 void SetData( const wxDataFormat& rFormat
90 ,char* pzBuffer
91 );
92 private:
93 wxDataObject* m_pDataObject; // pointer to C++ class we belong to
94 DRAGITEM m_vDragItem;
95 }; // end of CLASS CIDataObject
96
97 bool CIDataObject::GetData (
98 const wxDataFormat& rFormat
99 , char* pzBuffer
100 , ULONG ulLen
101 )
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;
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(
229 void* pBuf
230 ) const
231 {
232 wxString sFilenames;
233
234 for (size_t i = 0; i < m_filenames.GetCount(); i++)
235 {
236 sFilenames += m_filenames[i];
237 sFilenames += (wxChar)0;
238 }
239
240 memcpy(pBuf, sFilenames.mbc_str(), sFilenames.Len() + 1);
241 return TRUE;
242 }
243
244 size_t wxFileDataObject::GetDataSize() const
245 {
246 size_t nRes = 0;
247
248 for (size_t i = 0; i < m_filenames.GetCount(); i++)
249 {
250 nRes += m_filenames[i].Len();
251 nRes += 1;
252 }
253
254 return nRes + 1;
255 }
256
257 bool wxFileDataObject::SetData(
258 size_t WXUNUSED(nSize)
259 , const void* pBuf
260 )
261 {
262 /* TODO */
263
264 wxString sFile( (const char *)pBuf); /* char, not wxChar */
265
266 AddFile(sFile);
267
268 return TRUE;
269 }
270
271 void wxFileDataObject::AddFile(
272 const wxString& rFilename
273 )
274 {
275 m_filenames.Add(rFilename);
276 }
277
278 // ----------------------------------------------------------------------------
279 // wxBitmapDataObject
280 // ----------------------------------------------------------------------------
281
282 wxBitmapDataObject::wxBitmapDataObject()
283 {
284 Init();
285 }
286
287 wxBitmapDataObject::wxBitmapDataObject(
288 const wxBitmap& rBitmap
289 )
290 : wxBitmapDataObjectBase(rBitmap)
291 {
292 Init();
293
294 DoConvertToPng();
295 }
296
297 wxBitmapDataObject::~wxBitmapDataObject()
298 {
299 Clear();
300 }
301
302 void wxBitmapDataObject::SetBitmap(
303 const wxBitmap& rBitmap
304 )
305 {
306 ClearAll();
307 wxBitmapDataObjectBase::SetBitmap(rBitmap);
308 DoConvertToPng();
309 }
310
311 bool wxBitmapDataObject::GetDataHere(
312 void* pBuf
313 ) const
314 {
315 if (!m_pngSize)
316 {
317 wxFAIL_MSG(wxT("attempt to copy empty bitmap failed"));
318 return FALSE;
319 }
320 memcpy(pBuf, m_pngData, m_pngSize);
321 return TRUE;
322 }
323
324 bool wxBitmapDataObject::SetData(
325 size_t nSize
326 , const void* pBuf
327 )
328 {
329 Clear();
330 m_pngSize = nSize;
331 m_pngData = malloc(m_pngSize);
332
333 memcpy(m_pngData, pBuf, m_pngSize);
334
335 #if wxUSE_STREAMS
336 wxMemoryInputStream vMstream((char*)m_pngData, m_pngSize);
337 wxImage vImage;
338 wxPNGHandler vHandler;
339
340 if (!vHandler.LoadFile(&vImage, vMstream))
341 {
342 return FALSE;
343 }
344
345 m_bitmap = wxBitmap(vImage);
346 #endif //wxUSE_STREAMS
347
348 return m_bitmap.Ok();
349 }
350
351 void wxBitmapDataObject::DoConvertToPng()
352 {
353 if (!m_bitmap.Ok())
354 return;
355
356 #if wxUSE_STREAMS
357 wxImage vImage = m_bitmap.ConvertToImage();
358 wxPNGHandler vHandler;
359 wxCountingOutputStream vCount;
360
361 vHandler.SaveFile(&vImage, vCount);
362
363 m_pngSize = vCount.GetSize() + 100; // sometimes the size seems to vary ???
364 m_pngData = malloc(m_pngSize);
365
366 wxMemoryOutputStream vMstream((char*) m_pngData, m_pngSize);
367
368 vHandler.SaveFile(&vImage, vMstream );
369 #endif
370 }
371