]> git.saurik.com Git - wxWidgets.git/blame - src/os2/dataobj.cpp
Distrib corrections,
[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
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"
e6ebb514
DW
34#include "wx/mstream.h"
35#include "wx/image.h"
6dddc146
DW
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
e6ebb514 52wxDataFormat::wxDataFormat()
6dddc146 53{
6dddc146
DW
54 m_vType = wxDF_INVALID;
55 m_vFormat = 0;
56}
57
58wxDataFormat::wxDataFormat(
59 wxDataFormatId vType
60)
61{
62 PrepareFormats();
63 SetType(vType);
64}
65
66wxDataFormat::wxDataFormat(
67 const wxChar* zId
68)
69{
70 PrepareFormats();
71 SetId(zId);
72}
73
74wxDataFormat::wxDataFormat(
75 const wxString& rId
76)
77{
78 PrepareFormats();
79 SetId(rId);
80}
81
82wxDataFormat::wxDataFormat(
83 NativeFormat vFormat
84)
85{
86 PrepareFormats();
87 SetId(vFormat);
88}
89
90void 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
110wxDataFormatId wxDataFormat::GetType() const
111{
112 return m_vType;
113}
114
115wxString wxDataFormat::GetId() const
116{
117 wxString sRet(""); // TODO: gdk_atom_name( m_format ) );
118 return sRet;
119}
120
121void 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
141void 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
151void 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
168wxDataObject::wxDataObject()
169{
170}
171
172bool 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];
e6ebb514 186 GetAllFormats( pFormats
6dddc146
DW
187 ,vDir
188 );
189
190 size_t n;
191
192 for (n = 0; n < nFormatCount; n++)
193 {
e6ebb514 194 if (pFormats[n] == rFormat)
6dddc146
DW
195 break;
196 }
197
e6ebb514 198 delete [] pFormats;
6dddc146
DW
199
200 // found?
201 return n < nFormatCount;
202 }
203}
204
205// ----------------------------------------------------------------------------
206// wxFileDataObject
207// ----------------------------------------------------------------------------
208
209bool wxFileDataObject::GetDataHere(
210 void* pBuf
211) const
212{
213 wxString sFilenames;
214
215 for (size_t i = 0; i < m_filenames.GetCount(); i++)
216 {
e6ebb514
DW
217 sFilenames += m_filenames[i];
218 sFilenames += (wxChar)0;
6dddc146
DW
219 }
220
e6ebb514 221 memcpy(pBuf, sFilenames.mbc_str(), sFilenames.Len() + 1);
6dddc146
DW
222 return TRUE;
223}
224
225size_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
238bool 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
252void wxFileDataObject::AddFile(
253 const wxString& rFilename
254)
255{
256 m_filenames.Add(rFilename);
257}
258
259// ----------------------------------------------------------------------------
260// wxBitmapDataObject
261// ----------------------------------------------------------------------------
262
263wxBitmapDataObject::wxBitmapDataObject()
264{
265 Init();
266}
267
268wxBitmapDataObject::wxBitmapDataObject(
269 const wxBitmap& rBitmap
270)
271: wxBitmapDataObjectBase(rBitmap)
272{
273 Init();
274
275 DoConvertToPng();
276}
277
278wxBitmapDataObject::~wxBitmapDataObject()
279{
280 Clear();
281}
282
283void wxBitmapDataObject::SetBitmap(
284 const wxBitmap& rBitmap
285)
286{
287 ClearAll();
288 wxBitmapDataObjectBase::SetBitmap(rBitmap);
289 DoConvertToPng();
290}
291
292bool 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
305bool 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 wxMemoryInputStream vMstream((char*)m_pngData, m_pngSize);
317 wxImage vImage;
318 wxPNGHandler vHandler;
319
320 if (!vHandler.LoadFile(&vImage, vMstream))
321 {
322 return FALSE;
323 }
324
325 m_bitmap = vImage.ConvertToBitmap();
326 return m_bitmap.Ok();
327}
328
329void wxBitmapDataObject::DoConvertToPng()
330{
331 if (!m_bitmap.Ok())
332 return;
333
334 wxImage vImage(m_bitmap);
335 wxPNGHandler vHandler;
336 wxCountingOutputStream vCount;
337
e6ebb514 338 vHandler.SaveFile(&vImage, vCount);
6dddc146
DW
339
340 m_pngSize = vCount.GetSize() + 100; // sometimes the size seems to vary ???
341 m_pngData = malloc(m_pngSize);
342
e6ebb514
DW
343 wxMemoryOutputStream vMstream((char*) m_pngData, m_pngSize);
344
6dddc146
DW
345 vHandler.SaveFile(&vImage, vMstream );
346}
347