]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dataobj.cpp
process pending wx events before sending idle events
[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
51d4293d 192void wxTextDataObject::GetAllFormats( wxDataFormat *formats, wxDataObjectBase::Direction dir ) const
25758297
SC
193{
194 *formats++ = wxDataFormat( wxDF_TEXT );
195 *formats = wxDataFormat( wxDF_UNICODETEXT );
196}
25758297
SC
197#endif
198
72e7876b
SC
199// ----------------------------------------------------------------------------
200// wxFileDataObject
201// ----------------------------------------------------------------------------
202
5aa67d66 203void wxFileDataObject::GetFileNames( wxCharBuffer &buf ) const
d7b284e5
SC
204{
205 wxString filenames;
5aa67d66 206
d7b284e5
SC
207 for (size_t i = 0; i < m_filenames.GetCount(); i++)
208 {
209 filenames += m_filenames[i];
210 filenames += wxT('\n');
211 }
5aa67d66 212
d7b284e5
SC
213 buf = filenames.fn_str();
214}
215
51d4293d 216bool wxFileDataObject::GetDataHere( void *pBuf ) const
72e7876b 217{
51d4293d
DS
218 if (pBuf == NULL)
219 return false;
220
d7b284e5 221 wxCharBuffer buf;
5aa67d66 222 size_t buffLength;
72e7876b 223
5aa67d66
DS
224 GetFileNames( buf );
225 buffLength = strlen( buf );
226 memcpy( pBuf, (const char*)buf, buffLength + 1 );
51d4293d
DS
227
228 return true;
72e7876b
SC
229}
230
231size_t wxFileDataObject::GetDataSize() const
232{
d7b284e5 233 wxCharBuffer buf;
5aa67d66
DS
234 size_t buffLength;
235
d7b284e5 236 GetFileNames( buf );
5aa67d66 237 buffLength = strlen( buf );
72e7876b 238
5aa67d66 239 return buffLength + 1;
72e7876b
SC
240}
241
d7b284e5 242bool wxFileDataObject::SetData( size_t nSize, const void *pBuf )
72e7876b 243{
d7b284e5 244 wxString filenames;
5aa67d66 245
d7b284e5 246#if wxUSE_UNICODE
5aa67d66 247 filenames = wxString( (const char*)pBuf, *wxConvFileName );
d7b284e5 248#else
5088ab3f 249 filenames = wxString (wxConvLocal.cWC2WX(wxConvFileName->cMB2WC( (const char*)pBuf)));
d7b284e5 250#endif
72e7876b 251
5aa67d66
DS
252 m_filenames = wxStringTokenize( filenames, wxT("\n"), wxTOKEN_STRTOK );
253
51d4293d 254 return true;
72e7876b
SC
255}
256
51d4293d 257void wxFileDataObject::AddFile( const wxString& rFilename )
72e7876b 258{
51d4293d 259 m_filenames.Add( rFilename );
72e7876b
SC
260}
261
262// ----------------------------------------------------------------------------
263// wxBitmapDataObject
264// ----------------------------------------------------------------------------
265
266wxBitmapDataObject::wxBitmapDataObject()
267{
268 Init();
269}
270
51d4293d
DS
271wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& rBitmap )
272: wxBitmapDataObjectBase( rBitmap )
72e7876b
SC
273{
274 Init();
51d4293d
DS
275
276 if (m_bitmap.Ok())
2a391f87 277 {
51d4293d
DS
278 m_pictHandle = m_bitmap.GetBitmapData()->GetPictHandle();
279 m_pictCreated = false;
e40298d5 280 }
72e7876b
SC
281}
282
283wxBitmapDataObject::~wxBitmapDataObject()
284{
285 Clear();
286}
287
51d4293d 288void wxBitmapDataObject::SetBitmap( const wxBitmap& rBitmap )
72e7876b 289{
2a391f87 290 Clear();
51d4293d
DS
291 wxBitmapDataObjectBase::SetBitmap( rBitmap );
292 if (m_bitmap.Ok())
2a391f87 293 {
51d4293d
DS
294 m_pictHandle = m_bitmap.GetBitmapData()->GetPictHandle();
295 m_pictCreated = false;
e40298d5 296 }
2a391f87
SC
297}
298
51d4293d
DS
299void wxBitmapDataObject::Init()
300{
301 m_pictHandle = NULL;
302 m_pictCreated = false;
303}
2a391f87 304
51d4293d 305void wxBitmapDataObject::Clear()
2a391f87 306{
51d4293d 307 if (m_pictHandle != NULL)
e40298d5 308 {
7cd7bc23 309#ifndef __LP64__
51d4293d
DS
310 if (m_pictCreated)
311 KillPicture( (PicHandle)m_pictHandle );
7cd7bc23 312#endif
51d4293d 313 m_pictHandle = NULL;
e40298d5 314 }
51d4293d
DS
315
316 m_pictCreated = false;
72e7876b
SC
317}
318
51d4293d 319bool wxBitmapDataObject::GetDataHere( void *pBuf ) const
72e7876b 320{
51d4293d 321 if (m_pictHandle == NULL)
72e7876b 322 {
51d4293d
DS
323 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
324 return false;
72e7876b 325 }
51d4293d
DS
326
327 if (pBuf == NULL)
328 return false;
329
330 memcpy( pBuf, *(Handle)m_pictHandle, GetHandleSize( (Handle)m_pictHandle ) );
331
332 return true;
72e7876b
SC
333}
334
2a391f87
SC
335size_t wxBitmapDataObject::GetDataSize() const
336{
51d4293d
DS
337 if (m_pictHandle != NULL)
338 return GetHandleSize( (Handle)m_pictHandle );
339 else
340 return 0;
2a391f87
SC
341}
342
51d4293d 343bool wxBitmapDataObject::SetData( size_t nSize, const void *pBuf )
72e7876b
SC
344{
345 Clear();
51d4293d
DS
346
347 if ((pBuf == NULL) || (nSize == 0))
348 return false;
349
350 PicHandle picHandle = (PicHandle)NewHandle( nSize );
351 memcpy( *picHandle, pBuf, nSize );
352 m_pictHandle = picHandle;
353
20b69855 354 // ownership is transferred to the bitmap
51d4293d 355 m_pictCreated = false;
7cd7bc23 356#ifndef __LP64__
e542ecc6
DS
357 Rect frame;
358 wxMacGetPictureBounds( picHandle, &frame );
72c1ba98 359#if wxUSE_METAFILE
51d4293d
DS
360 wxMetafile mf;
361 mf.SetHMETAFILE( (WXHMETAFILE)m_pictHandle );
72c1ba98 362#endif
51d4293d
DS
363 wxMemoryDC mdc;
364 m_bitmap.Create( frame.right - frame.left, frame.bottom - frame.top );
365 mdc.SelectObject( m_bitmap );
72c1ba98 366#if wxUSE_METAFILE
51d4293d 367 mf.Play( &mdc );
72c1ba98 368#endif
51d4293d 369 mdc.SelectObject( wxNullBitmap );
7cd7bc23 370#endif
51d4293d 371
72e7876b
SC
372 return m_bitmap.Ok();
373}
179e085f 374
51d4293d 375#endif