]> git.saurik.com Git - wxWidgets.git/blob - src/os2/dataobj.cpp
another segfault fix
[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 license
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 wxDataFormat::wxDataFormat()
53 {
54 m_vType = wxDF_INVALID;
55 m_vFormat = 0;
56 }
57
58 wxDataFormat::wxDataFormat(
59 wxDataFormatId vType
60 )
61 {
62 PrepareFormats();
63 SetType(vType);
64 }
65
66 wxDataFormat::wxDataFormat(
67 const wxChar* zId
68 )
69 {
70 PrepareFormats();
71 SetId(zId);
72 }
73
74 wxDataFormat::wxDataFormat(
75 const wxString& rId
76 )
77 {
78 PrepareFormats();
79 SetId(rId);
80 }
81
82 wxDataFormat::wxDataFormat(
83 NativeFormat vFormat
84 )
85 {
86 PrepareFormats();
87 SetId(vFormat);
88 }
89
90 void wxDataFormat::SetType(
91 wxDataFormatId vType
92 )
93 {
94 m_vType = vType;
95
96 if (m_vType == wxDF_TEXT)
97 m_vFormat = 0;
98 else
99 if (m_vType == wxDF_BITMAP)
100 m_vFormat = 0;
101 else
102 if (m_vType == wxDF_FILENAME)
103 m_vFormat = 0;
104 else
105 {
106 wxFAIL_MSG( wxT("invalid dataformat") );
107 }
108 }
109
110 wxDataFormatId wxDataFormat::GetType() const
111 {
112 return m_vType;
113 }
114
115 wxString wxDataFormat::GetId() const
116 {
117 wxString sRet(""); // TODO: gdk_atom_name( m_format ) );
118 return sRet;
119 }
120
121 void wxDataFormat::SetId(
122 NativeFormat vFormat
123 )
124 {
125 m_vFormat = vFormat;
126 // TODO:
127 /*
128 if (m_format == g_textAtom)
129 m_type = wxDF_TEXT;
130 else
131 if (m_format == g_pngAtom)
132 m_type = wxDF_BITMAP;
133 else
134 if (m_format == g_fileAtom)
135 m_type = wxDF_FILENAME;
136 else
137 m_type = wxDF_PRIVATE;
138 */
139 }
140
141 void wxDataFormat::SetId(
142 const wxChar* zId
143 )
144 {
145 wxString tmp(zId);
146
147 m_vType = wxDF_PRIVATE;
148 m_vFormat = 0;// TODO: get the format gdk_atom_intern( wxMBSTRINGCAST tmp.mbc_str(), FALSE );
149 }
150
151 void wxDataFormat::PrepareFormats()
152 {
153 // TODO:
154 /*
155 if (!g_textAtom)
156 g_textAtom = gdk_atom_intern( "STRING", FALSE );
157 if (!g_pngAtom)
158 g_pngAtom = gdk_atom_intern( "image/png", FALSE );
159 if (!g_fileAtom)
160 g_fileAtom = gdk_atom_intern( "file:ALL", FALSE );
161 */
162 }
163
164 //-------------------------------------------------------------------------
165 // wxDataObject
166 //-------------------------------------------------------------------------
167
168 wxDataObject::wxDataObject()
169 {
170 }
171
172 bool wxDataObject::IsSupportedFormat(
173 const wxDataFormat& rFormat
174 , Direction vDir
175 ) const
176 {
177 size_t nFormatCount = GetFormatCount(vDir);
178
179 if (nFormatCount == 1)
180 {
181 return rFormat == GetPreferredFormat();
182 }
183 else
184 {
185 wxDataFormat* pFormats = new wxDataFormat[nFormatCount];
186 GetAllFormats( pFormats
187 ,vDir
188 );
189
190 size_t n;
191
192 for (n = 0; n < nFormatCount; n++)
193 {
194 if (pFormats[n] == rFormat)
195 break;
196 }
197
198 delete [] pFormats;
199
200 // found?
201 return n < nFormatCount;
202 }
203 }
204
205 // ----------------------------------------------------------------------------
206 // wxFileDataObject
207 // ----------------------------------------------------------------------------
208
209 bool wxFileDataObject::GetDataHere(
210 void* pBuf
211 ) const
212 {
213 wxString sFilenames;
214
215 for (size_t i = 0; i < m_filenames.GetCount(); i++)
216 {
217 sFilenames += m_filenames[i];
218 sFilenames += (wxChar)0;
219 }
220
221 memcpy(pBuf, sFilenames.mbc_str(), sFilenames.Len() + 1);
222 return TRUE;
223 }
224
225 size_t wxFileDataObject::GetDataSize() const
226 {
227 size_t nRes = 0;
228
229 for (size_t i = 0; i < m_filenames.GetCount(); i++)
230 {
231 nRes += m_filenames[i].Len();
232 nRes += 1;
233 }
234
235 return nRes + 1;
236 }
237
238 bool wxFileDataObject::SetData(
239 size_t WXUNUSED(nSize)
240 , const void* pBuf
241 )
242 {
243 /* TODO */
244
245 wxString sFile( (const char *)pBuf); /* char, not wxChar */
246
247 AddFile(sFile);
248
249 return TRUE;
250 }
251
252 void wxFileDataObject::AddFile(
253 const wxString& rFilename
254 )
255 {
256 m_filenames.Add(rFilename);
257 }
258
259 // ----------------------------------------------------------------------------
260 // wxBitmapDataObject
261 // ----------------------------------------------------------------------------
262
263 wxBitmapDataObject::wxBitmapDataObject()
264 {
265 Init();
266 }
267
268 wxBitmapDataObject::wxBitmapDataObject(
269 const wxBitmap& rBitmap
270 )
271 : wxBitmapDataObjectBase(rBitmap)
272 {
273 Init();
274
275 DoConvertToPng();
276 }
277
278 wxBitmapDataObject::~wxBitmapDataObject()
279 {
280 Clear();
281 }
282
283 void wxBitmapDataObject::SetBitmap(
284 const wxBitmap& rBitmap
285 )
286 {
287 ClearAll();
288 wxBitmapDataObjectBase::SetBitmap(rBitmap);
289 DoConvertToPng();
290 }
291
292 bool wxBitmapDataObject::GetDataHere(
293 void* pBuf
294 ) const
295 {
296 if (!m_pngSize)
297 {
298 wxFAIL_MSG(wxT("attempt to copy empty bitmap failed"));
299 return FALSE;
300 }
301 memcpy(pBuf, m_pngData, m_pngSize);
302 return TRUE;
303 }
304
305 bool wxBitmapDataObject::SetData(
306 size_t nSize
307 , const void* pBuf
308 )
309 {
310 Clear();
311 m_pngSize = nSize;
312 m_pngData = malloc(m_pngSize);
313
314 memcpy(m_pngData, pBuf, m_pngSize);
315
316 #if wxUSE_STREAMS
317 wxMemoryInputStream vMstream((char*)m_pngData, m_pngSize);
318 wxImage vImage;
319 wxPNGHandler vHandler;
320
321 if (!vHandler.LoadFile(&vImage, vMstream))
322 {
323 return FALSE;
324 }
325
326 m_bitmap = vImage.ConvertToBitmap();
327 #endif //wxUSE_STREAMS
328
329 return m_bitmap.Ok();
330 }
331
332 void wxBitmapDataObject::DoConvertToPng()
333 {
334 if (!m_bitmap.Ok())
335 return;
336
337 #if wxUSE_STREAMS
338 wxImage vImage(m_bitmap);
339 wxPNGHandler vHandler;
340 wxCountingOutputStream vCount;
341
342 vHandler.SaveFile(&vImage, vCount);
343
344 m_pngSize = vCount.GetSize() + 100; // sometimes the size seems to vary ???
345 m_pngData = malloc(m_pngSize);
346
347 wxMemoryOutputStream vMstream((char*) m_pngData, m_pngSize);
348
349 vHandler.SaveFile(&vImage, vMstream );
350 #endif
351 }
352