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