]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dataobj.cpp
use wxString::Format(), calling string.Format() has no effect patch 1267345)
[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 wxCHECK_MSG( !IsStandard(), wxEmptyString ,
103 wxT("name of predefined format cannot be retrieved") );
104
105 return m_id ;
106 }
107
108 void wxDataFormat::SetId( NativeFormat format )
109 {
110 m_format = format;
111
112 if (m_format == kScrapFlavorTypeText)
113 m_type = wxDF_TEXT;
114 else if (m_format == kScrapFlavorTypeUnicode )
115 m_type = wxDF_UNICODETEXT;
116 else if (m_format == kScrapFlavorTypePicture)
117 m_type = wxDF_BITMAP;
118 else if (m_format == kDragFlavorTypeHFS )
119 m_type = wxDF_FILENAME;
120 else
121 {
122 m_type = wxDF_PRIVATE;
123 char text[5] ;
124 strncpy( text , (char*) &format , 4 ) ;
125 text[4] = 0 ;
126 m_id = wxString::FromAscii( text ) ;
127 }
128 }
129
130 void wxDataFormat::SetId( const wxChar* zId )
131 {
132 m_type = wxDF_PRIVATE;
133 m_id = zId ;
134 m_format = 'WXPR' ;
135 }
136
137 bool wxDataFormat::operator==(const wxDataFormat& format) const
138 {
139 if ( IsStandard() || format.IsStandard() )
140 {
141 return ( format.m_type == m_type ) ;
142 }
143 else
144 {
145 return ( m_id == format.m_id ) ;
146 }
147 }
148
149 //-------------------------------------------------------------------------
150 // wxDataObject
151 //-------------------------------------------------------------------------
152
153 wxDataObject::wxDataObject()
154 {
155 }
156
157 bool wxDataObject::IsSupportedFormat(
158 const wxDataFormat& rFormat
159 , Direction vDir
160 ) const
161 {
162 size_t nFormatCount = GetFormatCount(vDir);
163
164 if (nFormatCount == 1)
165 {
166 return rFormat == GetPreferredFormat();
167 }
168 else
169 {
170 wxDataFormat* pFormats = new wxDataFormat[nFormatCount];
171 GetAllFormats( pFormats
172 ,vDir
173 );
174
175 size_t n;
176
177 for (n = 0; n < nFormatCount; n++)
178 {
179 if (pFormats[n] == rFormat)
180 break;
181 }
182
183 delete [] pFormats;
184
185 // found?
186 return n < nFormatCount;
187 }
188 }
189
190 // ----------------------------------------------------------------------------
191 // wxTextDataObject
192 // ----------------------------------------------------------------------------
193
194 #if wxUSE_UNICODE
195 void wxTextDataObject::GetAllFormats(wxDataFormat *formats, wxDataObjectBase::Direction dir) const
196 {
197 *formats++ = wxDataFormat( wxDF_TEXT );
198 *formats = wxDataFormat( wxDF_UNICODETEXT );
199 }
200
201 #endif
202
203 // ----------------------------------------------------------------------------
204 // wxFileDataObject
205 // ----------------------------------------------------------------------------
206
207 bool wxFileDataObject::GetDataHere(
208 void* pBuf
209 ) const
210 {
211 wxString sFilenames;
212
213 for (size_t i = 0; i < m_filenames.GetCount(); i++)
214 {
215 sFilenames += m_filenames[i];
216 sFilenames += (wxChar)0;
217 }
218
219 memcpy(pBuf, sFilenames.mbc_str(), sFilenames.Len() + 1);
220 return TRUE;
221 }
222
223 size_t wxFileDataObject::GetDataSize() const
224 {
225 size_t nRes = 0;
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
236 bool wxFileDataObject::SetData(
237 size_t WXUNUSED(nSize)
238 , const void* pBuf
239 )
240 {
241 m_filenames.Empty();
242
243 // only add if this is not an empty string
244 // we can therefore clear the list by just setting an empty string
245 if ( (*(char*)pBuf) != 0 )
246 AddFile(wxString::FromAscii((char*)pBuf));
247
248 return TRUE;
249 }
250
251 void wxFileDataObject::AddFile(
252 const wxString& rFilename
253 )
254 {
255 m_filenames.Add(rFilename);
256 }
257
258 // ----------------------------------------------------------------------------
259 // wxBitmapDataObject
260 // ----------------------------------------------------------------------------
261
262 wxBitmapDataObject::wxBitmapDataObject()
263 {
264 Init();
265 }
266
267 wxBitmapDataObject::wxBitmapDataObject(
268 const wxBitmap& rBitmap
269 )
270 : wxBitmapDataObjectBase(rBitmap)
271 {
272 Init();
273 if ( m_bitmap.Ok() )
274 {
275 m_pictHandle = m_bitmap.GetBitmapData()->GetPictHandle() ;
276 m_pictCreated = false ;
277 }
278 }
279
280 wxBitmapDataObject::~wxBitmapDataObject()
281 {
282 Clear();
283 }
284
285 void wxBitmapDataObject::SetBitmap(
286 const wxBitmap& rBitmap
287 )
288 {
289 Clear();
290 wxBitmapDataObjectBase::SetBitmap(rBitmap);
291 if ( m_bitmap.Ok() )
292 {
293 m_pictHandle = m_bitmap.GetBitmapData()->GetPictHandle() ;
294 m_pictCreated = false ;
295 }
296 }
297
298 void wxBitmapDataObject::Init()
299 {
300 m_pictHandle = NULL ;
301 m_pictCreated = false ;
302 }
303
304 void wxBitmapDataObject::Clear()
305 {
306 if ( m_pictCreated && m_pictHandle )
307 {
308 KillPicture( (PicHandle) m_pictHandle ) ;
309 }
310 m_pictHandle = NULL ;
311 }
312
313 bool wxBitmapDataObject::GetDataHere(
314 void* pBuf
315 ) const
316 {
317 if (!m_pictHandle)
318 {
319 wxFAIL_MSG(wxT("attempt to copy empty bitmap failed"));
320 return FALSE;
321 }
322 memcpy(pBuf, *(Handle)m_pictHandle, GetHandleSize((Handle)m_pictHandle));
323 return TRUE;
324 }
325
326 size_t wxBitmapDataObject::GetDataSize() const
327 {
328 return GetHandleSize((Handle)m_pictHandle) ;
329 }
330
331 bool wxBitmapDataObject::SetData(
332 size_t nSize
333 , const void* pBuf
334 )
335 {
336 Clear();
337 PicHandle picHandle = (PicHandle) NewHandle( nSize ) ;
338 memcpy( *picHandle , pBuf , nSize ) ;
339 m_pictHandle = picHandle ;
340 // ownership is transferred to the bitmap
341 m_pictCreated = false ;
342 Rect frame = (**picHandle).picFrame ;
343
344 wxMetafile mf ;
345 mf.SetHMETAFILE( (WXHMETAFILE) m_pictHandle ) ;
346 wxMemoryDC mdc ;
347 m_bitmap.Create( frame.right - frame.left ,frame.bottom - frame.top ) ;
348 mdc.SelectObject(m_bitmap ) ;
349 mf.Play( &mdc ) ;
350 mdc.SelectObject( wxNullBitmap ) ;
351
352 return m_bitmap.Ok();
353 }
354
355 #endif