]> git.saurik.com Git - wxWidgets.git/blob - src/mac/dataobj.cpp
Always use XPMs in this sample
[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 // this is '????' but it can't be used in the code because ??' is
90 // parsed as a trigraph!
91 m_format = 0x3f3f3f3f;
92 }
93 }
94
95 wxString wxDataFormat::GetId() const
96 {
97 // note that m_format is not a pointer to string, it *is* itself a 4
98 // character string
99 char text[5] ;
100 strncpy( text , (char*) &m_format , 4 ) ;
101 text[4] = 0 ;
102
103 return wxString::FromAscii( text ) ;
104 }
105
106 void wxDataFormat::SetId( NativeFormat format )
107 {
108 m_format = format;
109
110 if (m_format == kScrapFlavorTypeText)
111 m_type = wxDF_TEXT;
112 else if (m_format == kScrapFlavorTypeUnicode )
113 m_type = wxDF_UNICODETEXT;
114 else if (m_format == kScrapFlavorTypePicture)
115 m_type = wxDF_BITMAP;
116 else if (m_format == kDragFlavorTypeHFS )
117 m_type = wxDF_FILENAME;
118 else
119 m_type = wxDF_PRIVATE;
120 }
121
122 void wxDataFormat::SetId( const wxChar* zId )
123 {
124 m_type = wxDF_PRIVATE;
125 m_format = 0;// TODO: get the format gdk_atom_intern( wxMBSTRINGCAST tmp.mbc_str(), FALSE );
126 }
127
128 //-------------------------------------------------------------------------
129 // wxDataObject
130 //-------------------------------------------------------------------------
131
132 wxDataObject::wxDataObject()
133 {
134 }
135
136 bool wxDataObject::IsSupportedFormat(
137 const wxDataFormat& rFormat
138 , Direction vDir
139 ) const
140 {
141 size_t nFormatCount = GetFormatCount(vDir);
142
143 if (nFormatCount == 1)
144 {
145 return rFormat == GetPreferredFormat();
146 }
147 else
148 {
149 wxDataFormat* pFormats = new wxDataFormat[nFormatCount];
150 GetAllFormats( pFormats
151 ,vDir
152 );
153
154 size_t n;
155
156 for (n = 0; n < nFormatCount; n++)
157 {
158 if (pFormats[n] == rFormat)
159 break;
160 }
161
162 delete [] pFormats;
163
164 // found?
165 return n < nFormatCount;
166 }
167 }
168
169 // ----------------------------------------------------------------------------
170 // wxFileDataObject
171 // ----------------------------------------------------------------------------
172
173 bool wxFileDataObject::GetDataHere(
174 void* pBuf
175 ) const
176 {
177 wxString sFilenames;
178
179 for (size_t i = 0; i < m_filenames.GetCount(); i++)
180 {
181 sFilenames += m_filenames[i];
182 sFilenames += (wxChar)0;
183 }
184
185 memcpy(pBuf, sFilenames.mbc_str(), sFilenames.Len() + 1);
186 return TRUE;
187 }
188
189 size_t wxFileDataObject::GetDataSize() const
190 {
191 size_t nRes = 0;
192
193 for (size_t i = 0; i < m_filenames.GetCount(); i++)
194 {
195 nRes += m_filenames[i].Len();
196 nRes += 1;
197 }
198
199 return nRes + 1;
200 }
201
202 bool wxFileDataObject::SetData(
203 size_t WXUNUSED(nSize)
204 , const void* pBuf
205 )
206 {
207 m_filenames.Empty();
208
209 AddFile(wxString::FromAscii((char*)pBuf));
210
211 return TRUE;
212 }
213
214 void wxFileDataObject::AddFile(
215 const wxString& rFilename
216 )
217 {
218 m_filenames.Add(rFilename);
219 }
220
221 // ----------------------------------------------------------------------------
222 // wxBitmapDataObject
223 // ----------------------------------------------------------------------------
224
225 wxBitmapDataObject::wxBitmapDataObject()
226 {
227 Init();
228 }
229
230 wxBitmapDataObject::wxBitmapDataObject(
231 const wxBitmap& rBitmap
232 )
233 : wxBitmapDataObjectBase(rBitmap)
234 {
235 Init();
236 if ( m_bitmap.Ok() )
237 {
238 m_pictHandle = m_bitmap.GetPict( &m_pictCreated ) ;
239 }
240 }
241
242 wxBitmapDataObject::~wxBitmapDataObject()
243 {
244 Clear();
245 }
246
247 void wxBitmapDataObject::SetBitmap(
248 const wxBitmap& rBitmap
249 )
250 {
251 Clear();
252 wxBitmapDataObjectBase::SetBitmap(rBitmap);
253 if ( m_bitmap.Ok() )
254 {
255 m_pictHandle = m_bitmap.GetPict( &m_pictCreated ) ;
256 }
257 }
258
259 void wxBitmapDataObject::Init()
260 {
261 m_pictHandle = NULL ;
262 m_pictCreated = false ;
263 }
264
265 void wxBitmapDataObject::Clear()
266 {
267 if ( m_pictCreated && m_pictHandle )
268 {
269 KillPicture( (PicHandle) m_pictHandle ) ;
270 }
271 m_pictHandle = NULL ;
272 }
273
274 bool wxBitmapDataObject::GetDataHere(
275 void* pBuf
276 ) const
277 {
278 if (!m_pictHandle)
279 {
280 wxFAIL_MSG(wxT("attempt to copy empty bitmap failed"));
281 return FALSE;
282 }
283 memcpy(pBuf, *(Handle)m_pictHandle, GetHandleSize((Handle)m_pictHandle));
284 return TRUE;
285 }
286
287 size_t wxBitmapDataObject::GetDataSize() const
288 {
289 return GetHandleSize((Handle)m_pictHandle) ;
290 }
291
292 bool wxBitmapDataObject::SetData(
293 size_t nSize
294 , const void* pBuf
295 )
296 {
297 Clear();
298 PicHandle picHandle = (PicHandle) NewHandle( nSize ) ;
299 memcpy( *picHandle , pBuf , nSize ) ;
300 m_pictHandle = picHandle ;
301 m_pictCreated = false ;
302 Rect frame = (**picHandle).picFrame ;
303
304 m_bitmap.SetPict( picHandle ) ;
305 m_bitmap.SetWidth( frame.right - frame.left ) ;
306 m_bitmap.SetHeight( frame.bottom - frame.top ) ;
307 return m_bitmap.Ok();
308 }