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