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