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