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