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