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