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