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