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