]> git.saurik.com Git - wxWidgets.git/blob - src/os2/dataobj.cpp
use correct way to get a pointer to vector elements in DoGetPartialTextExtents()
[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 #if wxUSE_UNICODE
177 case wxDF_UNICODETEXT:
178 ulSize = ::wcslen((const wchar_t *)pzBuffer);
179 break;
180 #endif
181
182 case wxDF_BITMAP:
183 case wxDF_METAFILE:
184 case wxDF_ENHMETAFILE:
185 case wxDF_TIFF:
186 case wxDF_DIB:
187 ulSize = 0; // pass via a handle
188 break;
189
190
191 case wxDF_SYLK:
192 case wxDF_DIF:
193 case wxDF_PALETTE:
194 case wxDF_PENDATA:
195 case wxDF_RIFF:
196 case wxDF_WAVE:
197 case wxDF_LOCALE:
198 //PUNT
199 break;
200
201 case wxDF_PRIVATE:
202 size_t* p = (size_t *)pzBuffer;
203
204 ulSize = *p++;
205 pzBuffer = (char*)p;
206 break;
207 }
208 m_pDataObject->SetData( rFormat
209 ,ulSize
210 ,(void*)pzBuffer
211 );
212 } // end of CIDataObject::SetData
213
214 //-------------------------------------------------------------------------
215 // wxDataObject
216 //-------------------------------------------------------------------------
217
218 wxDataObject::wxDataObject ()
219 {
220 m_pDataObject = new DRAGITEM;
221 } // end of wxDataObject::wxDataObject
222
223 wxDataObject::~wxDataObject ()
224 {
225 delete m_pDataObject;
226 } // end of wxDataObject::~wxDataObject
227
228 // ----------------------------------------------------------------------------
229 // wxFileDataObject
230 // ----------------------------------------------------------------------------
231
232 bool wxFileDataObject::GetDataHere(
233 void* pBuf
234 ) const
235 {
236 wxString sFilenames;
237
238 for (size_t i = 0; i < m_filenames.GetCount(); i++)
239 {
240 sFilenames += m_filenames[i];
241 sFilenames += (wxChar)0;
242 }
243
244 memcpy(pBuf, sFilenames.mbc_str(), sFilenames.Len() + 1);
245 return TRUE;
246 }
247
248 size_t wxFileDataObject::GetDataSize() const
249 {
250 size_t nRes = 0;
251
252 for (size_t i = 0; i < m_filenames.GetCount(); i++)
253 {
254 nRes += m_filenames[i].Len();
255 nRes += 1;
256 }
257
258 return nRes + 1;
259 }
260
261 bool wxFileDataObject::SetData(
262 size_t WXUNUSED(nSize)
263 , const void* pBuf
264 )
265 {
266 /* TODO */
267
268 wxString sFile( (const char *)pBuf); /* char, not wxChar */
269
270 AddFile(sFile);
271
272 return TRUE;
273 }
274
275 void wxFileDataObject::AddFile(
276 const wxString& rFilename
277 )
278 {
279 m_filenames.Add(rFilename);
280 }
281
282 // ----------------------------------------------------------------------------
283 // wxBitmapDataObject
284 // ----------------------------------------------------------------------------
285
286 wxBitmapDataObject::wxBitmapDataObject()
287 {
288 Init();
289 }
290
291 wxBitmapDataObject::wxBitmapDataObject(
292 const wxBitmap& rBitmap
293 )
294 : wxBitmapDataObjectBase(rBitmap)
295 {
296 Init();
297
298 DoConvertToPng();
299 }
300
301 wxBitmapDataObject::~wxBitmapDataObject()
302 {
303 Clear();
304 }
305
306 void wxBitmapDataObject::SetBitmap(
307 const wxBitmap& rBitmap
308 )
309 {
310 ClearAll();
311 wxBitmapDataObjectBase::SetBitmap(rBitmap);
312 DoConvertToPng();
313 }
314
315 bool wxBitmapDataObject::GetDataHere(
316 void* pBuf
317 ) const
318 {
319 if (!m_pngSize)
320 {
321 wxFAIL_MSG(wxT("attempt to copy empty bitmap failed"));
322 return FALSE;
323 }
324 memcpy(pBuf, m_pngData, m_pngSize);
325 return TRUE;
326 }
327
328 bool wxBitmapDataObject::SetData(
329 size_t nSize
330 , const void* pBuf
331 )
332 {
333 Clear();
334 m_pngSize = nSize;
335 m_pngData = malloc(m_pngSize);
336
337 memcpy(m_pngData, pBuf, m_pngSize);
338
339 #if wxUSE_STREAMS
340 wxMemoryInputStream vMstream((char*)m_pngData, m_pngSize);
341 wxImage vImage;
342 wxPNGHandler vHandler;
343
344 if (!vHandler.LoadFile(&vImage, vMstream))
345 {
346 return FALSE;
347 }
348
349 m_bitmap = wxBitmap(vImage);
350 #endif //wxUSE_STREAMS
351
352 return m_bitmap.Ok();
353 }
354
355 void wxBitmapDataObject::DoConvertToPng()
356 {
357 if (!m_bitmap.Ok())
358 return;
359
360 #if wxUSE_STREAMS
361 wxImage vImage = m_bitmap.ConvertToImage();
362 wxPNGHandler vHandler;
363 wxCountingOutputStream vCount;
364
365 vHandler.SaveFile(&vImage, vCount);
366
367 m_pngSize = vCount.GetSize() + 100; // sometimes the size seems to vary ???
368 m_pngData = malloc(m_pngSize);
369
370 wxMemoryOutputStream vMstream((char*) m_pngData, m_pngSize);
371
372 vHandler.SaveFile(&vImage, vMstream );
373 #endif
374 }
375