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