]> git.saurik.com Git - wxWidgets.git/blob - src/mac/dataobj.cpp
merged the confllict resolution (I've fixed the m_format bug independently); no real...
[wxWidgets.git] / src / mac / 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 #ifdef __GNUG__
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 #include "wx/defs.h"
31
32 #include "wx/log.h"
33 #include "wx/dataobj.h"
34 #include "wx/mstream.h"
35 #include "wx/image.h"
36 #include "wx/mac/private.h"
37 #include "Scrap.h"
38
39 // ----------------------------------------------------------------------------
40 // functions
41 // ----------------------------------------------------------------------------
42
43 // ----------------------------------------------------------------------------
44 // wxDataFormat
45 // ----------------------------------------------------------------------------
46
47 wxDataFormat::wxDataFormat()
48 {
49 m_type = wxDF_INVALID;
50 m_format = 0;
51 }
52
53 wxDataFormat::wxDataFormat( wxDataFormatId vType )
54 {
55 SetType(vType);
56 }
57
58 wxDataFormat::wxDataFormat( const wxChar* zId)
59 {
60 SetId(zId);
61 }
62
63 wxDataFormat::wxDataFormat( const wxString& rId)
64 {
65 SetId(rId);
66 }
67
68 wxDataFormat::wxDataFormat( NativeFormat vFormat)
69 {
70 SetId(vFormat);
71 }
72
73 void wxDataFormat::SetType( wxDataFormatId Type )
74 {
75 m_type = Type;
76
77 if (m_type == wxDF_TEXT )
78 m_format = kScrapFlavorTypeText;
79 else if (m_type == wxDF_UNICODETEXT )
80 m_format = kScrapFlavorTypeUnicode ;
81 else if (m_type == wxDF_BITMAP || m_type == wxDF_METAFILE )
82 m_format = kScrapFlavorTypePicture;
83 else if (m_type == wxDF_FILENAME)
84 m_format = kDragFlavorTypeHFS ;
85 else
86 {
87 wxFAIL_MSG( wxT("invalid dataformat") );
88
89 m_format = '????';
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(
172 void* pBuf
173 ) const
174 {
175 wxString sFilenames;
176
177 for (size_t i = 0; i < m_filenames.GetCount(); i++)
178 {
179 sFilenames += m_filenames[i];
180 sFilenames += (wxChar)0;
181 }
182
183 memcpy(pBuf, sFilenames.mbc_str(), sFilenames.Len() + 1);
184 return TRUE;
185 }
186
187 size_t wxFileDataObject::GetDataSize() const
188 {
189 size_t nRes = 0;
190
191 for (size_t i = 0; i < m_filenames.GetCount(); i++)
192 {
193 nRes += m_filenames[i].Len();
194 nRes += 1;
195 }
196
197 return nRes + 1;
198 }
199
200 bool wxFileDataObject::SetData(
201 size_t WXUNUSED(nSize)
202 , const void* pBuf
203 )
204 {
205 m_filenames.Empty();
206
207 AddFile(wxString::FromAscii((char*)pBuf));
208
209 return TRUE;
210 }
211
212 void wxFileDataObject::AddFile(
213 const wxString& rFilename
214 )
215 {
216 m_filenames.Add(rFilename);
217 }
218
219 // ----------------------------------------------------------------------------
220 // wxBitmapDataObject
221 // ----------------------------------------------------------------------------
222
223 wxBitmapDataObject::wxBitmapDataObject()
224 {
225 Init();
226 }
227
228 wxBitmapDataObject::wxBitmapDataObject(
229 const wxBitmap& rBitmap
230 )
231 : wxBitmapDataObjectBase(rBitmap)
232 {
233 Init();
234 if ( m_bitmap.Ok() )
235 {
236 m_pictHandle = m_bitmap.GetPict( &m_pictCreated ) ;
237 }
238 }
239
240 wxBitmapDataObject::~wxBitmapDataObject()
241 {
242 Clear();
243 }
244
245 void wxBitmapDataObject::SetBitmap(
246 const wxBitmap& rBitmap
247 )
248 {
249 Clear();
250 wxBitmapDataObjectBase::SetBitmap(rBitmap);
251 if ( m_bitmap.Ok() )
252 {
253 m_pictHandle = m_bitmap.GetPict( &m_pictCreated ) ;
254 }
255 }
256
257 void wxBitmapDataObject::Init()
258 {
259 m_pictHandle = NULL ;
260 m_pictCreated = false ;
261 }
262
263 void wxBitmapDataObject::Clear()
264 {
265 if ( m_pictCreated && m_pictHandle )
266 {
267 KillPicture( (PicHandle) m_pictHandle ) ;
268 }
269 m_pictHandle = NULL ;
270 }
271
272 bool wxBitmapDataObject::GetDataHere(
273 void* pBuf
274 ) const
275 {
276 if (!m_pictHandle)
277 {
278 wxFAIL_MSG(wxT("attempt to copy empty bitmap failed"));
279 return FALSE;
280 }
281 memcpy(pBuf, *(Handle)m_pictHandle, GetHandleSize((Handle)m_pictHandle));
282 return TRUE;
283 }
284
285 size_t wxBitmapDataObject::GetDataSize() const
286 {
287 return GetHandleSize((Handle)m_pictHandle) ;
288 }
289
290 bool wxBitmapDataObject::SetData(
291 size_t nSize
292 , const void* pBuf
293 )
294 {
295 Clear();
296 PicHandle picHandle = (PicHandle) NewHandle( nSize ) ;
297 memcpy( *picHandle , pBuf , nSize ) ;
298 m_pictHandle = picHandle ;
299 m_pictCreated = false ;
300 Rect frame = (**picHandle).picFrame ;
301
302 m_bitmap.SetPict( picHandle ) ;
303 m_bitmap.SetWidth( frame.right - frame.left ) ;
304 m_bitmap.SetHeight( frame.bottom - frame.top ) ;
305 return m_bitmap.Ok();
306 }