]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/carbon/dataobj.cpp
Include wx/bitmap.h according to precompiled headers of wx/wx.h (with other minor...
[wxWidgets.git] / src / mac / carbon / dataobj.cpp
... / ...
CommitLineData
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/mac/carbon/dataobj.cpp
3// Purpose: implementation of wxDataObject class
4// Author: Stefan Csomor
5// Modified by:
6// Created: 10/21/99
7// RCS-ID: $Id$
8// Copyright: (c) 1999 Stefan Csomor
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#if wxUSE_DATAOBJ
16
17#include "wx/dataobj.h"
18
19#ifndef WX_PRECOMP
20 #include "wx/intl.h"
21 #include "wx/log.h"
22 #include "wx/dcmemory.h"
23#endif
24
25#include "wx/mstream.h"
26#include "wx/image.h"
27#include "wx/metafile.h"
28#include "wx/tokenzr.h"
29
30#include "wx/mac/private.h"
31
32#ifndef __DARWIN__
33#include <Scrap.h>
34#endif
35
36
37// ----------------------------------------------------------------------------
38// wxDataFormat
39// ----------------------------------------------------------------------------
40
41wxDataFormat::wxDataFormat()
42{
43 m_type = wxDF_INVALID;
44 m_format = 0;
45}
46
47wxDataFormat::wxDataFormat( wxDataFormatId vType )
48{
49 SetType( vType );
50}
51
52wxDataFormat::wxDataFormat( const wxChar *zId )
53{
54 SetId( zId );
55}
56
57wxDataFormat::wxDataFormat( const wxString& rId )
58{
59 SetId( rId );
60}
61
62wxDataFormat::wxDataFormat( NativeFormat vFormat )
63{
64 SetId( vFormat );
65}
66
67void wxDataFormat::SetType( wxDataFormatId dataType )
68{
69 m_type = dataType;
70
71 switch (m_type)
72 {
73 case wxDF_TEXT:
74 m_format = kScrapFlavorTypeText;
75 break;
76
77 case wxDF_UNICODETEXT:
78 m_format = kScrapFlavorTypeUnicode;
79 break;
80
81 case wxDF_BITMAP:
82 case wxDF_METAFILE:
83 m_format = kScrapFlavorTypePicture;
84 break;
85
86 case wxDF_FILENAME:
87 m_format = kDragFlavorTypeHFS;
88 break;
89
90 default:
91 wxFAIL_MSG( wxT("invalid data format") );
92
93 // NB: this translates to '????' ASCII but it can't be used in the code
94 // because '??' will get parsed as a trigraph!
95 m_format = 0x3f3f3f3f;
96 break;
97 }
98}
99
100wxString wxDataFormat::GetId() const
101{
102 wxCHECK_MSG( !IsStandard(), wxEmptyString,
103 wxT("name of predefined format cannot be retrieved") );
104
105 return m_id;
106}
107
108void wxDataFormat::SetId( NativeFormat format )
109{
110 m_format = format;
111
112 switch (m_format)
113 {
114 case kScrapFlavorTypeText:
115 m_type = wxDF_TEXT;
116 break;
117
118 case kScrapFlavorTypeUnicode:
119 m_type = wxDF_UNICODETEXT;
120 break;
121
122 case kScrapFlavorTypePicture:
123 m_type = wxDF_BITMAP;
124 break;
125
126 case kDragFlavorTypeHFS:
127 m_type = wxDF_FILENAME;
128 break;
129
130 default:
131 m_type = wxDF_PRIVATE;
132 char text[5];
133 memcpy( text, (const char*)&format, 4 );
134 text[4] = 0;
135 m_id = wxString::FromAscii( text );
136 break;
137 }
138}
139
140void wxDataFormat::SetId( const wxChar* zId )
141{
142 m_type = wxDF_PRIVATE;
143 m_id = zId;
144 m_format = 'WXPR';
145}
146
147bool wxDataFormat::operator==(const wxDataFormat& format) const
148{
149 if (IsStandard() || format.IsStandard())
150 return (format.m_type == m_type);
151 else
152 return (m_id == format.m_id);
153}
154
155//-------------------------------------------------------------------------
156// wxDataObject
157//-------------------------------------------------------------------------
158
159wxDataObject::wxDataObject()
160{
161}
162
163bool wxDataObject::IsSupportedFormat( const wxDataFormat& rFormat, Direction vDir ) const
164{
165 size_t nFormatCount = GetFormatCount( vDir );
166 bool found = false;
167
168 if (nFormatCount == 1)
169 {
170 found = (rFormat == GetPreferredFormat());
171 }
172 else
173 {
174 wxDataFormat *pFormats = new wxDataFormat[nFormatCount];
175 GetAllFormats( pFormats, vDir );
176
177 for (size_t n = 0; n < nFormatCount; n++)
178 {
179 if (pFormats[n] == rFormat)
180 {
181 found = true;
182 break;
183 }
184 }
185
186 delete [] pFormats;
187 }
188
189 return found;
190}
191
192// ----------------------------------------------------------------------------
193// wxTextDataObject
194// ----------------------------------------------------------------------------
195
196#if wxUSE_UNICODE
197void wxTextDataObject::GetAllFormats( wxDataFormat *formats, wxDataObjectBase::Direction dir ) const
198{
199 *formats++ = wxDataFormat( wxDF_TEXT );
200 *formats = wxDataFormat( wxDF_UNICODETEXT );
201}
202#endif
203
204// ----------------------------------------------------------------------------
205// wxFileDataObject
206// ----------------------------------------------------------------------------
207
208void wxFileDataObject::GetFileNames( wxCharBuffer &buf ) const
209{
210 wxString filenames;
211
212 for (size_t i = 0; i < m_filenames.GetCount(); i++)
213 {
214 filenames += m_filenames[i];
215 filenames += wxT('\n');
216 }
217
218 buf = filenames.fn_str();
219}
220
221bool wxFileDataObject::GetDataHere( void *pBuf ) const
222{
223 if (pBuf == NULL)
224 return false;
225
226 wxCharBuffer buf;
227 size_t buffLength;
228
229 GetFileNames( buf );
230 buffLength = strlen( buf );
231 memcpy( pBuf, (const char*)buf, buffLength + 1 );
232
233 return true;
234}
235
236size_t wxFileDataObject::GetDataSize() const
237{
238 wxCharBuffer buf;
239 size_t buffLength;
240
241 GetFileNames( buf );
242 buffLength = strlen( buf );
243
244 return buffLength + 1;
245}
246
247bool wxFileDataObject::SetData( size_t nSize, const void *pBuf )
248{
249 wxString filenames;
250
251#if wxUSE_UNICODE
252 filenames = wxString( (const char*)pBuf, *wxConvFileName );
253#else
254 filenames = wxString( wxConvFileName->cMB2WX( (const char*)pBuf ), wxConvLocal );
255#endif
256
257 m_filenames = wxStringTokenize( filenames, wxT("\n"), wxTOKEN_STRTOK );
258
259 return true;
260}
261
262void wxFileDataObject::AddFile( const wxString& rFilename )
263{
264 m_filenames.Add( rFilename );
265}
266
267// ----------------------------------------------------------------------------
268// wxBitmapDataObject
269// ----------------------------------------------------------------------------
270
271wxBitmapDataObject::wxBitmapDataObject()
272{
273 Init();
274}
275
276wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& rBitmap )
277: wxBitmapDataObjectBase( rBitmap )
278{
279 Init();
280
281 if (m_bitmap.Ok())
282 {
283 m_pictHandle = m_bitmap.GetBitmapData()->GetPictHandle();
284 m_pictCreated = false;
285 }
286}
287
288wxBitmapDataObject::~wxBitmapDataObject()
289{
290 Clear();
291}
292
293void wxBitmapDataObject::SetBitmap( const wxBitmap& rBitmap )
294{
295 Clear();
296 wxBitmapDataObjectBase::SetBitmap( rBitmap );
297 if (m_bitmap.Ok())
298 {
299 m_pictHandle = m_bitmap.GetBitmapData()->GetPictHandle();
300 m_pictCreated = false;
301 }
302}
303
304void wxBitmapDataObject::Init()
305{
306 m_pictHandle = NULL;
307 m_pictCreated = false;
308}
309
310void wxBitmapDataObject::Clear()
311{
312 if (m_pictHandle != NULL)
313 {
314 if (m_pictCreated)
315 KillPicture( (PicHandle)m_pictHandle );
316 m_pictHandle = NULL;
317 }
318
319 m_pictCreated = false;
320}
321
322bool wxBitmapDataObject::GetDataHere( void *pBuf ) const
323{
324 if (m_pictHandle == NULL)
325 {
326 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
327 return false;
328 }
329
330 if (pBuf == NULL)
331 return false;
332
333 memcpy( pBuf, *(Handle)m_pictHandle, GetHandleSize( (Handle)m_pictHandle ) );
334
335 return true;
336}
337
338size_t wxBitmapDataObject::GetDataSize() const
339{
340 if (m_pictHandle != NULL)
341 return GetHandleSize( (Handle)m_pictHandle );
342 else
343 return 0;
344}
345
346bool wxBitmapDataObject::SetData( size_t nSize, const void *pBuf )
347{
348 Clear();
349
350 if ((pBuf == NULL) || (nSize == 0))
351 return false;
352
353 PicHandle picHandle = (PicHandle)NewHandle( nSize );
354 memcpy( *picHandle, pBuf, nSize );
355 m_pictHandle = picHandle;
356
357 // ownership is transferred to the bitmap
358 m_pictCreated = false;
359 Rect frame;
360 wxMacGetPictureBounds( picHandle, &frame );
361
362 wxMetafile mf;
363 mf.SetHMETAFILE( (WXHMETAFILE)m_pictHandle );
364 wxMemoryDC mdc;
365 m_bitmap.Create( frame.right - frame.left, frame.bottom - frame.top );
366 mdc.SelectObject( m_bitmap );
367 mf.Play( &mdc );
368 mdc.SelectObject( wxNullBitmap );
369
370 return m_bitmap.Ok();
371}
372
373#endif