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