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