]> git.saurik.com Git - wxWidgets.git/blob - src/mac/dataobj.cpp
Canvas: added some DECLARE_CLASS macros to stop it failing
[wxWidgets.git] / src / mac / 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 #include "wx/mac/private.h"
37
38 // ----------------------------------------------------------------------------
39 // functions
40 // ----------------------------------------------------------------------------
41
42 // ----------------------------------------------------------------------------
43 // wxDataFormat
44 // ----------------------------------------------------------------------------
45
46 wxDataFormat::wxDataFormat()
47 {
48 m_type = wxDF_INVALID;
49 m_format = 0;
50 }
51
52 wxDataFormat::wxDataFormat( wxDataFormatId vType )
53 {
54 SetType(vType);
55 }
56
57 wxDataFormat::wxDataFormat( const wxChar* zId)
58 {
59 SetId(zId);
60 }
61
62 wxDataFormat::wxDataFormat( const wxString& rId)
63 {
64 SetId(rId);
65 }
66
67 wxDataFormat::wxDataFormat( NativeFormat vFormat)
68 {
69 SetId(vFormat);
70 }
71
72 void wxDataFormat::SetType( wxDataFormatId Type )
73 {
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)
81 m_format = kDragFlavorTypeHFS ;
82 else
83 {
84 wxFAIL_MSG( wxT("invalid dataformat") );
85 }
86 }
87
88 wxDataFormatId wxDataFormat::GetType() const
89 {
90 return m_type;
91 }
92
93 wxString wxDataFormat::GetId() const
94 {
95 wxString sRet(""); // TODO: to name of ( m_format ) );
96 return sRet;
97 }
98
99 void wxDataFormat::SetId( NativeFormat format )
100 {
101 m_format = format;
102
103 if (m_format == 'TEXT')
104 m_type = wxDF_TEXT;
105 else
106 if (m_format == 'PICT')
107 m_type = wxDF_BITMAP;
108 else
109 if (m_format == kDragFlavorTypeHFS )
110 m_type = wxDF_FILENAME;
111 else
112 m_type = wxDF_PRIVATE;
113 }
114
115 void wxDataFormat::SetId( const wxChar* zId )
116 {
117 wxString tmp(zId);
118
119 m_type = wxDF_PRIVATE;
120 m_format = 0;// TODO: get the format gdk_atom_intern( wxMBSTRINGCAST tmp.mbc_str(), FALSE );
121 }
122
123 //-------------------------------------------------------------------------
124 // wxDataObject
125 //-------------------------------------------------------------------------
126
127 wxDataObject::wxDataObject()
128 {
129 }
130
131 bool 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
168 bool 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
184 size_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
197 bool wxFileDataObject::SetData(
198 size_t WXUNUSED(nSize)
199 , const void* pBuf
200 )
201 {
202 m_filenames.Empty();
203
204 wxString sFile( (const char *)pBuf); /* char, not wxChar */
205
206 AddFile(sFile);
207
208 return TRUE;
209 }
210
211 void wxFileDataObject::AddFile(
212 const wxString& rFilename
213 )
214 {
215 m_filenames.Add(rFilename);
216 }
217
218 // ----------------------------------------------------------------------------
219 // wxBitmapDataObject
220 // ----------------------------------------------------------------------------
221
222 wxBitmapDataObject::wxBitmapDataObject()
223 {
224 Init();
225 }
226
227 wxBitmapDataObject::wxBitmapDataObject(
228 const wxBitmap& rBitmap
229 )
230 : wxBitmapDataObjectBase(rBitmap)
231 {
232 Init();
233
234 DoConvertToPng();
235 }
236
237 wxBitmapDataObject::~wxBitmapDataObject()
238 {
239 Clear();
240 }
241
242 void wxBitmapDataObject::SetBitmap(
243 const wxBitmap& rBitmap
244 )
245 {
246 ClearAll();
247 wxBitmapDataObjectBase::SetBitmap(rBitmap);
248 DoConvertToPng();
249 }
250
251 bool 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
264 bool 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 = wxBitmap( vImage ) ;
285 return m_bitmap.Ok();
286 }
287
288 void wxBitmapDataObject::DoConvertToPng()
289 {
290 if (!m_bitmap.Ok())
291 return;
292
293 wxCHECK_RET( wxImage::FindHandler(wxBITMAP_TYPE_PNG) != NULL,
294 wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") );
295
296 wxImage image = m_bitmap.ConvertToImage();
297
298 wxCountingOutputStream count;
299 image.SaveFile(count, wxBITMAP_TYPE_PNG);
300
301 m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ???
302 m_pngData = malloc(m_pngSize);
303
304 wxMemoryOutputStream mstream((char*) m_pngData, m_pngSize);
305 image.SaveFile(mstream, wxBITMAP_TYPE_PNG);
306 }
307