]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dataobj.cpp
Move HID stuff into both OSX builds. Add preliminary joystick for OSX
[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/dcmemory.h"
34 #include "wx/mstream.h"
35 #include "wx/image.h"
36 #include "wx/metafile.h"
37 #include "wx/mac/private.h"
38 #include <Scrap.h>
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 // note that m_format is not a pointer to string, it *is* itself a 4
99 // character string
100 char text[5] ;
101 strncpy( text , (char*) &m_format , 4 ) ;
102 text[4] = 0 ;
103
104 return wxString::FromAscii( text ) ;
105 }
106
107 void wxDataFormat::SetId( NativeFormat format )
108 {
109 m_format = format;
110
111 if (m_format == kScrapFlavorTypeText)
112 m_type = wxDF_TEXT;
113 else if (m_format == kScrapFlavorTypeUnicode )
114 m_type = wxDF_UNICODETEXT;
115 else if (m_format == kScrapFlavorTypePicture)
116 m_type = wxDF_BITMAP;
117 else if (m_format == kDragFlavorTypeHFS )
118 m_type = wxDF_FILENAME;
119 else
120 m_type = wxDF_PRIVATE;
121 }
122
123 void wxDataFormat::SetId( const wxChar* zId )
124 {
125 m_type = wxDF_PRIVATE;
126 m_format = 0;// TODO: get the format gdk_atom_intern( wxMBSTRINGCAST tmp.mbc_str(), FALSE );
127 }
128
129 //-------------------------------------------------------------------------
130 // wxDataObject
131 //-------------------------------------------------------------------------
132
133 wxDataObject::wxDataObject()
134 {
135 }
136
137 bool wxDataObject::IsSupportedFormat(
138 const wxDataFormat& rFormat
139 , Direction vDir
140 ) const
141 {
142 size_t nFormatCount = GetFormatCount(vDir);
143
144 if (nFormatCount == 1)
145 {
146 return rFormat == GetPreferredFormat();
147 }
148 else
149 {
150 wxDataFormat* pFormats = new wxDataFormat[nFormatCount];
151 GetAllFormats( pFormats
152 ,vDir
153 );
154
155 size_t n;
156
157 for (n = 0; n < nFormatCount; n++)
158 {
159 if (pFormats[n] == rFormat)
160 break;
161 }
162
163 delete [] pFormats;
164
165 // found?
166 return n < nFormatCount;
167 }
168 }
169
170 // ----------------------------------------------------------------------------
171 // wxTextDataObject
172 // ----------------------------------------------------------------------------
173
174 #if wxUSE_UNICODE
175 void wxTextDataObject::GetAllFormats(wxDataFormat *formats, wxDataObjectBase::Direction dir) const
176 {
177 *formats++ = wxDataFormat( wxDF_TEXT );
178 *formats = wxDataFormat( wxDF_UNICODETEXT );
179 }
180
181 #endif
182
183 // ----------------------------------------------------------------------------
184 // wxFileDataObject
185 // ----------------------------------------------------------------------------
186
187 bool wxFileDataObject::GetDataHere(
188 void* pBuf
189 ) const
190 {
191 wxString sFilenames;
192
193 for (size_t i = 0; i < m_filenames.GetCount(); i++)
194 {
195 sFilenames += m_filenames[i];
196 sFilenames += (wxChar)0;
197 }
198
199 memcpy(pBuf, sFilenames.mbc_str(), sFilenames.Len() + 1);
200 return TRUE;
201 }
202
203 size_t wxFileDataObject::GetDataSize() const
204 {
205 size_t nRes = 0;
206
207 for (size_t i = 0; i < m_filenames.GetCount(); i++)
208 {
209 nRes += m_filenames[i].Len();
210 nRes += 1;
211 }
212
213 return nRes + 1;
214 }
215
216 bool wxFileDataObject::SetData(
217 size_t WXUNUSED(nSize)
218 , const void* pBuf
219 )
220 {
221 m_filenames.Empty();
222
223 // only add if this is not an empty string
224 // we can therefore clear the list by just setting an empty string
225 if ( (*(char*)pBuf) != 0 )
226 AddFile(wxString::FromAscii((char*)pBuf));
227
228 return TRUE;
229 }
230
231 void wxFileDataObject::AddFile(
232 const wxString& rFilename
233 )
234 {
235 m_filenames.Add(rFilename);
236 }
237
238 // ----------------------------------------------------------------------------
239 // wxBitmapDataObject
240 // ----------------------------------------------------------------------------
241
242 wxBitmapDataObject::wxBitmapDataObject()
243 {
244 Init();
245 }
246
247 wxBitmapDataObject::wxBitmapDataObject(
248 const wxBitmap& rBitmap
249 )
250 : wxBitmapDataObjectBase(rBitmap)
251 {
252 Init();
253 if ( m_bitmap.Ok() )
254 {
255 m_pictHandle = wxMacCreatePicHandle( rBitmap ) ;
256 m_pictCreated = true ;
257 }
258 }
259
260 wxBitmapDataObject::~wxBitmapDataObject()
261 {
262 Clear();
263 }
264
265 void wxBitmapDataObject::SetBitmap(
266 const wxBitmap& rBitmap
267 )
268 {
269 Clear();
270 wxBitmapDataObjectBase::SetBitmap(rBitmap);
271 if ( m_bitmap.Ok() )
272 {
273 m_pictHandle = wxMacCreatePicHandle( rBitmap ) ;
274 m_pictCreated = true ;
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 wxMetafile mf ;
325 mf.SetHMETAFILE( (WXHMETAFILE) m_pictHandle ) ;
326 wxMemoryDC mdc ;
327 m_bitmap.Create( frame.right - frame.left ,frame.bottom - frame.top ) ;
328 mdc.SelectObject(m_bitmap ) ;
329 mf.Play( &mdc ) ;
330 mdc.SelectObject( wxNullBitmap ) ;
331
332 return m_bitmap.Ok();
333 }