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