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