]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/dataobj.cpp
Test commit
[wxWidgets.git] / src / mac / classic / 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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/intl.h"
25 #endif
26 #include "wx/defs.h"
27
28 #include "wx/log.h"
29 #include "wx/dataobj.h"
30 #include "wx/mstream.h"
31 #include "wx/image.h"
32 #include "wx/mac/private.h"
33 #include <Scrap.h>
34
35 // ----------------------------------------------------------------------------
36 // functions
37 // ----------------------------------------------------------------------------
38
39 // ----------------------------------------------------------------------------
40 // wxDataFormat
41 // ----------------------------------------------------------------------------
42
43 wxDataFormat::wxDataFormat()
44 {
45 m_type = wxDF_INVALID;
46 m_format = 0;
47 }
48
49 wxDataFormat::wxDataFormat( wxDataFormatId vType )
50 {
51 SetType(vType);
52 }
53
54 wxDataFormat::wxDataFormat( const wxChar* zId)
55 {
56 SetId(zId);
57 }
58
59 wxDataFormat::wxDataFormat( const wxString& rId)
60 {
61 SetId(rId);
62 }
63
64 wxDataFormat::wxDataFormat( NativeFormat vFormat)
65 {
66 SetId(vFormat);
67 }
68
69 void wxDataFormat::SetType( wxDataFormatId Type )
70 {
71 m_type = Type;
72
73 if (m_type == wxDF_TEXT )
74 m_format = kScrapFlavorTypeText;
75 else if (m_type == wxDF_UNICODETEXT )
76 m_format = kScrapFlavorTypeUnicode ;
77 else if (m_type == wxDF_BITMAP || m_type == wxDF_METAFILE )
78 m_format = kScrapFlavorTypePicture;
79 else if (m_type == wxDF_FILENAME)
80 m_format = kDragFlavorTypeHFS ;
81 else
82 {
83 wxFAIL_MSG( wxT("invalid dataformat") );
84
85 // this is '????' but it can't be used in the code because ??' is
86 // parsed as a trigraph!
87 m_format = 0x3f3f3f3f;
88 }
89 }
90
91 wxString wxDataFormat::GetId() const
92 {
93 // note that m_format is not a pointer to string, it *is* itself a 4
94 // character string
95 char text[5] ;
96 strncpy( text , (char*) &m_format , 4 ) ;
97 text[4] = 0 ;
98
99 return wxString::FromAscii( text ) ;
100 }
101
102 void wxDataFormat::SetId( NativeFormat format )
103 {
104 m_format = format;
105
106 if (m_format == kScrapFlavorTypeText)
107 m_type = wxDF_TEXT;
108 else if (m_format == kScrapFlavorTypeUnicode )
109 m_type = wxDF_UNICODETEXT;
110 else if (m_format == kScrapFlavorTypePicture)
111 m_type = wxDF_BITMAP;
112 else if (m_format == kDragFlavorTypeHFS )
113 m_type = wxDF_FILENAME;
114 else
115 m_type = wxDF_PRIVATE;
116 }
117
118 void wxDataFormat::SetId( const wxChar* zId )
119 {
120 m_type = wxDF_PRIVATE;
121 m_format = 0;// TODO: get the format gdk_atom_intern( wxMBSTRINGCAST tmp.mbc_str(), FALSE );
122 }
123
124 //-------------------------------------------------------------------------
125 // wxDataObject
126 //-------------------------------------------------------------------------
127
128 wxDataObject::wxDataObject()
129 {
130 }
131
132 bool wxDataObject::IsSupportedFormat(
133 const wxDataFormat& rFormat
134 , Direction vDir
135 ) const
136 {
137 size_t nFormatCount = GetFormatCount(vDir);
138
139 if (nFormatCount == 1)
140 {
141 return rFormat == GetPreferredFormat();
142 }
143 else
144 {
145 wxDataFormat* pFormats = new wxDataFormat[nFormatCount];
146 GetAllFormats( pFormats
147 ,vDir
148 );
149
150 size_t n;
151
152 for (n = 0; n < nFormatCount; n++)
153 {
154 if (pFormats[n] == rFormat)
155 break;
156 }
157
158 delete [] pFormats;
159
160 // found?
161 return n < nFormatCount;
162 }
163 }
164
165 // ----------------------------------------------------------------------------
166 // wxFileDataObject
167 // ----------------------------------------------------------------------------
168
169 bool wxFileDataObject::GetDataHere(
170 void* pBuf
171 ) 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(
271 void* pBuf
272 ) const
273 {
274 if (!m_pictHandle)
275 {
276 wxFAIL_MSG(wxT("attempt to copy empty bitmap failed"));
277 return FALSE;
278 }
279 memcpy(pBuf, *(Handle)m_pictHandle, GetHandleSize((Handle)m_pictHandle));
280 return TRUE;
281 }
282
283 size_t wxBitmapDataObject::GetDataSize() const
284 {
285 return GetHandleSize((Handle)m_pictHandle) ;
286 }
287
288 bool wxBitmapDataObject::SetData(
289 size_t nSize
290 , const void* pBuf
291 )
292 {
293 Clear();
294 PicHandle picHandle = (PicHandle) NewHandle( nSize ) ;
295 memcpy( *picHandle , pBuf , nSize ) ;
296 m_pictHandle = picHandle ;
297 m_pictCreated = false ;
298 Rect frame = (**picHandle).picFrame ;
299
300 m_bitmap.SetPict( picHandle ) ;
301 m_bitmap.SetWidth( frame.right - frame.left ) ;
302 m_bitmap.SetHeight( frame.bottom - frame.top ) ;
303 return m_bitmap.Ok();
304 }