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