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