]> git.saurik.com Git - wxWidgets.git/blob - src/os2/dataobj.cpp
Removed WXWIN_COMPATIBILITY_2_2 together with code guarded by it.
[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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/defs.h"
25 #include "wx/intl.h"
26 #include "wx/log.h"
27 #endif
28
29 #if wxUSE_DATAOBJ
30
31 #include "wx/dataobj.h"
32 #include "wx/mstream.h"
33 #include "wx/image.h"
34
35 #define INCL_DOS
36 #include <os2.h>
37
38 // ----------------------------------------------------------------------------
39 // functions
40 // ----------------------------------------------------------------------------
41
42 // ----------------------------------------------------------------------------
43 // wxDataFormat
44 // ----------------------------------------------------------------------------
45
46 wxString wxDataFormat::GetId() const
47 {
48 wxChar zBuf[256];
49 wxString sRet;
50
51 ::WinQueryAtomName( ::WinQuerySystemAtomTable()
52 ,m_uFormat
53 ,(PSZ)zBuf
54 ,256
55 );
56 sRet = zBuf;
57 return sRet;
58 } // end of wxDataFormat::GetId()
59
60 void wxDataFormat::SetId (
61 const wxChar* zId
62 )
63 {
64 m_uFormat = ::WinAddAtom( ::WinQuerySystemAtomTable()
65 ,(PSZ)zId
66 );
67 } // end of wxDataFormat::SetId
68
69 class CIDataObject
70 {
71 public:
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 );
90 private:
91 wxDataObject* m_pDataObject; // pointer to C++ class we belong to
92 DRAGITEM m_vDragItem;
93 }; // end of CLASS CIDataObject
94
95 bool CIDataObject::GetData ( const wxDataFormat& rFormat,
96 char* pzBuffer,
97 ULONG ulLen )
98 {
99 QueryGetData(rFormat);
100 if (rFormat.GetType() == wxDF_INVALID)
101 return FALSE;
102
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)
113 {
114 //
115 // For custom formats, put the size with the data - alloc the
116 // space for it
117 //
118 ulSize += sizeof(ULONG);
119 }
120
121 if (ulSize > ulLen) // not enough room to copy
122 return FALSE;
123
124 //
125 // Copy the data
126 //
127 GetDataHere( rFormat
128 ,pzBuffer
129 ,ulSize
130 );
131 return true;
132 } // end of CIDataObject::GetData
133
134 void CIDataObject::GetDataHere(
135 const wxDataFormat& rFormat
136 , char* pzBuffer
137 , ULONG WXUNUSED(ulLen)
138 )
139 {
140 m_pDataObject->GetDataHere( rFormat
141 ,(void*)pzBuffer
142 );
143 } // end of CIDataObject::GetDataHere
144
145 void CIDataObject::QueryGetData (
146 const wxDataFormat& rFormat
147 )
148 {
149 m_pDataObject->IsSupportedFormat(rFormat);
150 } // end of CIDataObject::QueryGetData
151
152 void CIDataObject::SetData (
153 const wxDataFormat& rFormat
154 , char* pzBuffer
155 )
156 {
157 ULONG ulSize = 0;
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
168 #if wxUSE_UNICODE
169 case wxDF_UNICODETEXT:
170 ulSize = ::wcslen((const wchar_t *)pzBuffer);
171 break;
172 #endif
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
205
206 //-------------------------------------------------------------------------
207 // wxDataObject
208 //-------------------------------------------------------------------------
209
210 wxDataObject::wxDataObject ()
211 {
212 m_pDataObject = new DRAGITEM;
213 } // end of wxDataObject::wxDataObject
214
215 wxDataObject::~wxDataObject ()
216 {
217 delete m_pDataObject;
218 } // end of wxDataObject::~wxDataObject
219
220 // ----------------------------------------------------------------------------
221 // wxFileDataObject
222 // ----------------------------------------------------------------------------
223
224 bool wxFileDataObject::GetDataHere( void* pBuf ) const
225 {
226 wxString sFilenames;
227
228 for (size_t i = 0; i < m_filenames.GetCount(); i++)
229 {
230 sFilenames += m_filenames[i];
231 sFilenames += (wxChar)0;
232 }
233
234 memcpy(pBuf, sFilenames.mbc_str(), sFilenames.Len() + 1);
235 return true;
236 }
237
238 size_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
251 bool wxFileDataObject::SetData( size_t WXUNUSED(nSize),
252 const void* pBuf )
253 {
254 /* TODO */
255
256 wxString sFile((const wxChar *)pBuf); /* char, not wxChar */
257
258 AddFile(sFile);
259
260 return true;
261 }
262
263 void wxFileDataObject::AddFile(
264 const wxString& rFilename
265 )
266 {
267 m_filenames.Add(rFilename);
268 }
269
270 // ----------------------------------------------------------------------------
271 // wxBitmapDataObject
272 // ----------------------------------------------------------------------------
273
274 wxBitmapDataObject::wxBitmapDataObject()
275 {
276 Init();
277 }
278
279 wxBitmapDataObject::wxBitmapDataObject(
280 const wxBitmap& rBitmap
281 )
282 : wxBitmapDataObjectBase(rBitmap)
283 {
284 Init();
285
286 DoConvertToPng();
287 }
288
289 wxBitmapDataObject::~wxBitmapDataObject()
290 {
291 Clear();
292 }
293
294 void wxBitmapDataObject::SetBitmap( const wxBitmap& rBitmap )
295 {
296 ClearAll();
297 wxBitmapDataObjectBase::SetBitmap(rBitmap);
298 DoConvertToPng();
299 }
300
301 bool wxBitmapDataObject::GetDataHere( void* pBuf ) const
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);
309 return true;
310 }
311
312 bool 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
323 #if wxUSE_STREAMS
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
333 m_bitmap = wxBitmap(vImage);
334 #endif //wxUSE_STREAMS
335
336 return m_bitmap.Ok();
337 }
338
339 void wxBitmapDataObject::DoConvertToPng()
340 {
341 if (!m_bitmap.Ok())
342 return;
343
344 #if wxUSE_STREAMS
345 wxImage vImage = m_bitmap.ConvertToImage();
346 wxPNGHandler vHandler;
347 wxCountingOutputStream vCount;
348
349 vHandler.SaveFile(&vImage, vCount);
350
351 m_pngSize = vCount.GetSize() + 100; // sometimes the size seems to vary ???
352 m_pngData = malloc(m_pngSize);
353
354 wxMemoryOutputStream vMstream((char*) m_pngData, m_pngSize);
355
356 vHandler.SaveFile(&vImage, vMstream );
357 #endif
358 }
359
360 #endif // wxUSE_DATAOBJ