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