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