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