]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dataobj.cpp
5de37f5e061426fa6c3444a98ec276df962a3884
[wxWidgets.git] / src / mac / carbon / dataobj.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/dataobj.cpp
3 // Purpose: implementation of wxDataObject class
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 10/21/99
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if wxUSE_DATAOBJ
16
17 #ifndef WX_PRECOMP
18 #include "wx/intl.h"
19 #endif
20
21 #include "wx/log.h"
22 #include "wx/dataobj.h"
23 #include "wx/dcmemory.h"
24 #include "wx/mstream.h"
25 #include "wx/image.h"
26 #include "wx/metafile.h"
27 #include "wx/tokenzr.h"
28
29
30 #include "wx/mac/private.h"
31
32 #ifndef __DARWIN__
33 #include <Scrap.h>
34 #endif
35
36
37 // ----------------------------------------------------------------------------
38 // wxDataFormat
39 // ----------------------------------------------------------------------------
40
41 wxDataFormat::wxDataFormat()
42 {
43 m_type = wxDF_INVALID;
44 m_format = 0;
45 }
46
47 wxDataFormat::wxDataFormat( wxDataFormatId vType )
48 {
49 SetType( vType );
50 }
51
52 wxDataFormat::wxDataFormat( const wxChar *zId )
53 {
54 SetId( zId );
55 }
56
57 wxDataFormat::wxDataFormat( const wxString& rId )
58 {
59 SetId( rId );
60 }
61
62 wxDataFormat::wxDataFormat( NativeFormat vFormat )
63 {
64 SetId( vFormat );
65 }
66
67 void wxDataFormat::SetType( wxDataFormatId dataType )
68 {
69 m_type = dataType;
70
71 switch (m_type)
72 {
73 case wxDF_TEXT:
74 m_format = kScrapFlavorTypeText;
75 break;
76
77 case wxDF_UNICODETEXT:
78 m_format = kScrapFlavorTypeUnicode;
79 break;
80
81 case wxDF_BITMAP:
82 case wxDF_METAFILE:
83 m_format = kScrapFlavorTypePicture;
84 break;
85
86 case wxDF_FILENAME:
87 m_format = kDragFlavorTypeHFS;
88 break;
89
90 default:
91 wxFAIL_MSG( wxT("invalid data format") );
92
93 // NB: this translates to '????' ASCII but it can't be used in the code
94 // because '??' will get parsed as a trigraph!
95 m_format = 0x3f3f3f3f;
96 break;
97 }
98 }
99
100 wxString wxDataFormat::GetId() const
101 {
102 wxCHECK_MSG( !IsStandard(), wxEmptyString,
103 wxT("name of predefined format cannot be retrieved") );
104
105 return m_id;
106 }
107
108 void wxDataFormat::SetId( NativeFormat format )
109 {
110 m_format = format;
111
112 switch (m_format)
113 {
114 case kScrapFlavorTypeText:
115 m_type = wxDF_TEXT;
116 break;
117
118 case kScrapFlavorTypeUnicode:
119 m_type = wxDF_UNICODETEXT;
120 break;
121
122 case kScrapFlavorTypePicture:
123 m_type = wxDF_BITMAP;
124 break;
125
126 case kDragFlavorTypeHFS:
127 m_type = wxDF_FILENAME;
128 break;
129
130 default:
131 m_type = wxDF_PRIVATE;
132 char text[5];
133 strncpy( text, (char*)&format, 4 );
134 text[4] = 0;
135 m_id = wxString::FromAscii( text );
136 break;
137 }
138 }
139
140 void wxDataFormat::SetId( const wxChar* zId )
141 {
142 m_type = wxDF_PRIVATE;
143 m_id = zId;
144 m_format = 'WXPR';
145 }
146
147 bool wxDataFormat::operator==(const wxDataFormat& format) const
148 {
149 if (IsStandard() || format.IsStandard())
150 return (format.m_type == m_type);
151 else
152 return (m_id == format.m_id);
153 }
154
155 //-------------------------------------------------------------------------
156 // wxDataObject
157 //-------------------------------------------------------------------------
158
159 wxDataObject::wxDataObject()
160 {
161 }
162
163 bool wxDataObject::IsSupportedFormat( const wxDataFormat& rFormat, Direction vDir ) const
164 {
165 size_t nFormatCount = GetFormatCount( vDir );
166 bool found = false;
167
168 if (nFormatCount == 1)
169 {
170 found = (rFormat == GetPreferredFormat());
171 }
172 else
173 {
174 wxDataFormat* pFormats = new wxDataFormat[nFormatCount];
175 GetAllFormats( pFormats, vDir );
176
177 for (size_t n = 0; n < nFormatCount; n++)
178 {
179 if (pFormats[n] == rFormat)
180 {
181 found = true;
182 break;
183 }
184 }
185
186 delete [] pFormats;
187 }
188
189 return found;
190 }
191
192 // ----------------------------------------------------------------------------
193 // wxTextDataObject
194 // ----------------------------------------------------------------------------
195
196 #if wxUSE_UNICODE
197 void wxTextDataObject::GetAllFormats( wxDataFormat *formats, wxDataObjectBase::Direction dir ) const
198 {
199 *formats++ = wxDataFormat( wxDF_TEXT );
200 *formats = wxDataFormat( wxDF_UNICODETEXT );
201 }
202 #endif
203
204 // ----------------------------------------------------------------------------
205 // wxFileDataObject
206 // ----------------------------------------------------------------------------
207
208 void wxFileDataObject::GetFileNames(wxCharBuffer &buf) const
209 {
210 wxString filenames;
211
212 for (size_t i = 0; i < m_filenames.GetCount(); i++)
213 {
214 filenames += m_filenames[i];
215 filenames += wxT('\n');
216 }
217
218 buf = filenames.fn_str();
219 }
220
221 bool wxFileDataObject::GetDataHere( void *pBuf ) const
222 {
223 if (pBuf == NULL)
224 return false;
225
226 wxCharBuffer buf;
227 GetFileNames( buf );
228
229 memcpy( pBuf, (const char*) buf, strlen(buf) + 1 );
230
231 return true;
232 }
233
234 size_t wxFileDataObject::GetDataSize() const
235 {
236 wxCharBuffer buf;
237 GetFileNames( buf );
238
239 return strlen(buf) + 1;
240 }
241
242 bool wxFileDataObject::SetData( size_t nSize, const void *pBuf )
243 {
244 wxString filenames;
245 #if wxUSE_UNICODE
246 filenames = wxString( (const char*) pBuf , *wxConvFileName );
247 #else
248 filenames = wxString( wxConvFileName->cMB2WX( pBuf ) , wxConvLocal );
249 #endif
250
251 m_filenames = wxStringTokenize( filenames , wxT("\n") , wxTOKEN_STRTOK );
252
253 return true;
254 }
255
256 void wxFileDataObject::AddFile( const wxString& rFilename )
257 {
258 m_filenames.Add( rFilename );
259 }
260
261 // ----------------------------------------------------------------------------
262 // wxBitmapDataObject
263 // ----------------------------------------------------------------------------
264
265 wxBitmapDataObject::wxBitmapDataObject()
266 {
267 Init();
268 }
269
270 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& rBitmap )
271 : wxBitmapDataObjectBase( rBitmap )
272 {
273 Init();
274
275 if (m_bitmap.Ok())
276 {
277 m_pictHandle = m_bitmap.GetBitmapData()->GetPictHandle();
278 m_pictCreated = false;
279 }
280 }
281
282 wxBitmapDataObject::~wxBitmapDataObject()
283 {
284 Clear();
285 }
286
287 void wxBitmapDataObject::SetBitmap( const wxBitmap& rBitmap )
288 {
289 Clear();
290 wxBitmapDataObjectBase::SetBitmap( rBitmap );
291 if (m_bitmap.Ok())
292 {
293 m_pictHandle = m_bitmap.GetBitmapData()->GetPictHandle();
294 m_pictCreated = false;
295 }
296 }
297
298 void wxBitmapDataObject::Init()
299 {
300 m_pictHandle = NULL;
301 m_pictCreated = false;
302 }
303
304 void wxBitmapDataObject::Clear()
305 {
306 if (m_pictHandle != NULL)
307 {
308 if (m_pictCreated)
309 KillPicture( (PicHandle)m_pictHandle );
310 m_pictHandle = NULL;
311 }
312
313 m_pictCreated = false;
314 }
315
316 bool wxBitmapDataObject::GetDataHere( void *pBuf ) const
317 {
318 if (m_pictHandle == NULL)
319 {
320 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
321 return false;
322 }
323
324 if (pBuf == NULL)
325 return false;
326
327 memcpy( pBuf, *(Handle)m_pictHandle, GetHandleSize( (Handle)m_pictHandle ) );
328
329 return true;
330 }
331
332 size_t wxBitmapDataObject::GetDataSize() const
333 {
334 if (m_pictHandle != NULL)
335 return GetHandleSize( (Handle)m_pictHandle );
336 else
337 return 0;
338 }
339
340 bool wxBitmapDataObject::SetData( size_t nSize, const void *pBuf )
341 {
342 Clear();
343
344 if ((pBuf == NULL) || (nSize == 0))
345 return false;
346
347 PicHandle picHandle = (PicHandle)NewHandle( nSize );
348 memcpy( *picHandle, pBuf, nSize );
349 m_pictHandle = picHandle;
350
351 // ownership is transferred to the bitmap
352 m_pictCreated = false;
353 Rect frame;
354 wxMacGetPictureBounds( picHandle, &frame );
355
356 wxMetafile mf;
357 mf.SetHMETAFILE( (WXHMETAFILE)m_pictHandle );
358 wxMemoryDC mdc;
359 m_bitmap.Create( frame.right - frame.left, frame.bottom - frame.top );
360 mdc.SelectObject( m_bitmap );
361 mf.Play( &mdc );
362 mdc.SelectObject( wxNullBitmap );
363
364 return m_bitmap.Ok();
365 }
366
367 #endif