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