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