]> git.saurik.com Git - wxWidgets.git/blame - src/os2/dataobj.cpp
Test for x, y in GetPosition
[wxWidgets.git] / src / os2 / dataobj.cpp
CommitLineData
6dddc146
DW
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
65571936 9// Licence: wxWindows licence
6dddc146
DW
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"
e6ebb514
DW
34#include "wx/mstream.h"
35#include "wx/image.h"
6dddc146
DW
36
37#define INCL_DOS
38#include <os2.h>
39
40// ----------------------------------------------------------------------------
41// functions
42// ----------------------------------------------------------------------------
43
6dddc146
DW
44// ----------------------------------------------------------------------------
45// wxDataFormat
46// ----------------------------------------------------------------------------
47
4fd899b6 48wxString wxDataFormat::GetId() const
6dddc146 49{
0fba44b4 50 wxChar zBuf[256];
4fd899b6
DW
51 wxString sRet;
52
53 ::WinQueryAtomName( ::WinQuerySystemAtomTable()
54 ,m_uFormat
0fba44b4 55 ,(PSZ)zBuf
4fd899b6
DW
56 ,256
57 );
58 sRet = zBuf;
59 return sRet;
60} // end of wxDataFormat::GetId()
6dddc146 61
4fd899b6 62void wxDataFormat::SetId (
6dddc146
DW
63 const wxChar* zId
64)
65{
4fd899b6 66 m_uFormat = ::WinAddAtom( ::WinQuerySystemAtomTable()
0fba44b4 67 ,(PSZ)zId
4fd899b6
DW
68 );
69} // end of wxDataFormat::SetId
6dddc146 70
4fd899b6 71class CIDataObject
6dddc146 72{
4fd899b6
DW
73public:
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 );
92private:
93 wxDataObject* m_pDataObject; // pointer to C++ class we belong to
94 DRAGITEM m_vDragItem;
95}; // end of CLASS CIDataObject
96
97bool CIDataObject::GetData (
98 const wxDataFormat& rFormat
99, char* pzBuffer
100, ULONG ulLen
6dddc146
DW
101)
102{
4fd899b6
DW
103 QueryGetData(rFormat);
104 if (rFormat.GetType() == wxDF_INVALID)
105 return FALSE;
6dddc146 106
4fd899b6
DW
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)
6dddc146 117 {
4fd899b6
DW
118 //
119 // For custom formats, put the size with the data - alloc the
120 // space for it
121 //
122 ulSize += sizeof(ULONG);
6dddc146 123 }
6dddc146 124
4fd899b6
DW
125 if (ulSize > ulLen) // not enough room to copy
126 return FALSE;
6dddc146 127
4fd899b6
DW
128 //
129 // Copy the data
130 //
131 GetDataHere( rFormat
132 ,pzBuffer
133 ,ulSize
134 );
135 return TRUE;
136} // end of CIDataObject::GetData
6dddc146 137
4fd899b6
DW
138void CIDataObject::GetDataHere(
139 const wxDataFormat& rFormat
140, char* pzBuffer
141, ULONG WXUNUSED(ulLen)
6dddc146
DW
142)
143{
4fd899b6
DW
144 m_pDataObject->GetDataHere( rFormat
145 ,(void*)pzBuffer
146 );
147} // end of CIDataObject::GetDataHere
148
149void CIDataObject::QueryGetData (
150 const wxDataFormat& rFormat
6dddc146
DW
151)
152{
4fd899b6
DW
153 m_pDataObject->IsSupportedFormat(rFormat);
154} // end of CIDataObject::QueryGetData
6dddc146 155
4fd899b6
DW
156void CIDataObject::SetData (
157 const wxDataFormat& rFormat
158, char* pzBuffer
159)
6dddc146 160{
322d45dd 161 ULONG ulSize = 0;
4fd899b6
DW
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
8681f66c 172#if wxUSE_UNICODE
4fd899b6
DW
173 case wxDF_UNICODETEXT:
174 ulSize = ::wcslen((const wchar_t *)pzBuffer);
175 break;
8681f66c 176#endif
4fd899b6
DW
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
6dddc146
DW
209
210//-------------------------------------------------------------------------
211// wxDataObject
212//-------------------------------------------------------------------------
213
4fd899b6 214wxDataObject::wxDataObject ()
6dddc146 215{
4fd899b6
DW
216 m_pDataObject = new DRAGITEM;
217} // end of wxDataObject::wxDataObject
6dddc146 218
4fd899b6 219wxDataObject::~wxDataObject ()
6dddc146 220{
4fd899b6
DW
221 delete m_pDataObject;
222} // end of wxDataObject::~wxDataObject
6dddc146
DW
223
224// ----------------------------------------------------------------------------
225// wxFileDataObject
226// ----------------------------------------------------------------------------
227
228bool wxFileDataObject::GetDataHere(
229 void* pBuf
230) const
231{
232 wxString sFilenames;
233
234 for (size_t i = 0; i < m_filenames.GetCount(); i++)
235 {
e6ebb514
DW
236 sFilenames += m_filenames[i];
237 sFilenames += (wxChar)0;
6dddc146
DW
238 }
239
e6ebb514 240 memcpy(pBuf, sFilenames.mbc_str(), sFilenames.Len() + 1);
6dddc146
DW
241 return TRUE;
242}
243
244size_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
257bool wxFileDataObject::SetData(
258 size_t WXUNUSED(nSize)
259, const void* pBuf
260)
261{
262 /* TODO */
263
0fba44b4 264 wxString sFile((const wxChar *)pBuf); /* char, not wxChar */
6dddc146
DW
265
266 AddFile(sFile);
267
268 return TRUE;
269}
270
271void wxFileDataObject::AddFile(
272 const wxString& rFilename
273)
274{
275 m_filenames.Add(rFilename);
276}
277
278// ----------------------------------------------------------------------------
279// wxBitmapDataObject
280// ----------------------------------------------------------------------------
281
282wxBitmapDataObject::wxBitmapDataObject()
283{
284 Init();
285}
286
287wxBitmapDataObject::wxBitmapDataObject(
288 const wxBitmap& rBitmap
289)
290: wxBitmapDataObjectBase(rBitmap)
291{
292 Init();
293
294 DoConvertToPng();
295}
296
297wxBitmapDataObject::~wxBitmapDataObject()
298{
299 Clear();
300}
301
302void wxBitmapDataObject::SetBitmap(
303 const wxBitmap& rBitmap
304)
305{
306 ClearAll();
307 wxBitmapDataObjectBase::SetBitmap(rBitmap);
308 DoConvertToPng();
309}
310
311bool 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
324bool 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
7e99520b 335#if wxUSE_STREAMS
6dddc146
DW
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
3437f881 345 m_bitmap = wxBitmap(vImage);
7e99520b
DW
346#endif //wxUSE_STREAMS
347
6dddc146
DW
348 return m_bitmap.Ok();
349}
350
351void wxBitmapDataObject::DoConvertToPng()
352{
353 if (!m_bitmap.Ok())
354 return;
355
7e99520b 356#if wxUSE_STREAMS
3437f881 357 wxImage vImage = m_bitmap.ConvertToImage();
6dddc146
DW
358 wxPNGHandler vHandler;
359 wxCountingOutputStream vCount;
360
e6ebb514 361 vHandler.SaveFile(&vImage, vCount);
6dddc146
DW
362
363 m_pngSize = vCount.GetSize() + 100; // sometimes the size seems to vary ???
364 m_pngData = malloc(m_pngSize);
365
e6ebb514
DW
366 wxMemoryOutputStream vMstream((char*) m_pngData, m_pngSize);
367
6dddc146 368 vHandler.SaveFile(&vImage, vMstream );
7e99520b 369#endif
6dddc146
DW
370}
371