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