]> git.saurik.com Git - wxWidgets.git/blob - src/mac/dataobj.cpp
Fix by Milan Babuskov <albis@eunet.yu> in MovePageUp
[wxWidgets.git] / src / mac / dataobj.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: os2/dataobj.cpp
3 // Purpose: implementation of wx[I]DataObject class
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/21/99
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 David Webster
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
38 // ----------------------------------------------------------------------------
39 // functions
40 // ----------------------------------------------------------------------------
41
42 // ----------------------------------------------------------------------------
43 // wxDataFormat
44 // ----------------------------------------------------------------------------
45
46 wxDataFormat::wxDataFormat()
47 {
48 m_type = wxDF_INVALID;
49 m_format = 0;
50 }
51
52 wxDataFormat::wxDataFormat( wxDataFormatId vType )
53 {
54 SetType(vType);
55 }
56
57 wxDataFormat::wxDataFormat( const wxChar* zId)
58 {
59 SetId(zId);
60 }
61
62 wxDataFormat::wxDataFormat( const wxString& rId)
63 {
64 SetId(rId);
65 }
66
67 wxDataFormat::wxDataFormat( NativeFormat vFormat)
68 {
69 SetId(vFormat);
70 }
71
72 void wxDataFormat::SetType( wxDataFormatId Type )
73 {
74 m_type = Type;
75
76 if (m_type == wxDF_TEXT)
77 m_format = 'TEXT';
78 else if (m_type == wxDF_BITMAP || m_type == wxDF_METAFILE )
79 m_format = 'PICT';
80 else if (m_type == wxDF_FILENAME)
81 m_format = kDragFlavorTypeHFS ;
82 else
83 {
84 wxFAIL_MSG( wxT("invalid dataformat") );
85 }
86 }
87
88 wxDataFormatId wxDataFormat::GetType() const
89 {
90 return m_type;
91 }
92
93 wxString wxDataFormat::GetId() const
94 {
95 wxString sRet(""); // TODO: to name of ( m_format ) );
96 return sRet;
97 }
98
99 void wxDataFormat::SetId( NativeFormat format )
100 {
101 m_format = format;
102
103 if (m_format == 'TEXT')
104 m_type = wxDF_TEXT;
105 else
106 if (m_format == 'PICT')
107 m_type = wxDF_BITMAP;
108 else
109 if (m_format == kDragFlavorTypeHFS )
110 m_type = wxDF_FILENAME;
111 else
112 m_type = wxDF_PRIVATE;
113 }
114
115 void wxDataFormat::SetId( const wxChar* zId )
116 {
117 wxString tmp(zId);
118
119 m_type = wxDF_PRIVATE;
120 m_format = 0;// TODO: get the format gdk_atom_intern( wxMBSTRINGCAST tmp.mbc_str(), FALSE );
121 }
122
123 //-------------------------------------------------------------------------
124 // wxDataObject
125 //-------------------------------------------------------------------------
126
127 wxDataObject::wxDataObject()
128 {
129 }
130
131 bool wxDataObject::IsSupportedFormat(
132 const wxDataFormat& rFormat
133 , Direction vDir
134 ) const
135 {
136 size_t nFormatCount = GetFormatCount(vDir);
137
138 if (nFormatCount == 1)
139 {
140 return rFormat == GetPreferredFormat();
141 }
142 else
143 {
144 wxDataFormat* pFormats = new wxDataFormat[nFormatCount];
145 GetAllFormats( pFormats
146 ,vDir
147 );
148
149 size_t n;
150
151 for (n = 0; n < nFormatCount; n++)
152 {
153 if (pFormats[n] == rFormat)
154 break;
155 }
156
157 delete [] pFormats;
158
159 // found?
160 return n < nFormatCount;
161 }
162 }
163
164 // ----------------------------------------------------------------------------
165 // wxFileDataObject
166 // ----------------------------------------------------------------------------
167
168 bool wxFileDataObject::GetDataHere(
169 void* pBuf
170 ) const
171 {
172 wxString sFilenames;
173
174 for (size_t i = 0; i < m_filenames.GetCount(); i++)
175 {
176 sFilenames += m_filenames[i];
177 sFilenames += (wxChar)0;
178 }
179
180 memcpy(pBuf, sFilenames.mbc_str(), sFilenames.Len() + 1);
181 return TRUE;
182 }
183
184 size_t wxFileDataObject::GetDataSize() const
185 {
186 size_t nRes = 0;
187
188 for (size_t i = 0; i < m_filenames.GetCount(); i++)
189 {
190 nRes += m_filenames[i].Len();
191 nRes += 1;
192 }
193
194 return nRes + 1;
195 }
196
197 bool wxFileDataObject::SetData(
198 size_t WXUNUSED(nSize)
199 , const void* pBuf
200 )
201 {
202 m_filenames.Empty();
203
204 wxString sFile( (const char *)pBuf); /* char, not wxChar */
205
206 AddFile(sFile);
207
208 return TRUE;
209 }
210
211 void wxFileDataObject::AddFile(
212 const wxString& rFilename
213 )
214 {
215 m_filenames.Add(rFilename);
216 }
217
218 // ----------------------------------------------------------------------------
219 // wxBitmapDataObject
220 // ----------------------------------------------------------------------------
221
222 wxBitmapDataObject::wxBitmapDataObject()
223 {
224 Init();
225 }
226
227 wxBitmapDataObject::wxBitmapDataObject(
228 const wxBitmap& rBitmap
229 )
230 : wxBitmapDataObjectBase(rBitmap)
231 {
232 Init();
233 if ( m_bitmap.Ok() )
234 {
235 m_pictHandle = m_bitmap.GetPict( &m_pictCreated ) ;
236 }
237 }
238
239 wxBitmapDataObject::~wxBitmapDataObject()
240 {
241 Clear();
242 }
243
244 void wxBitmapDataObject::SetBitmap(
245 const wxBitmap& rBitmap
246 )
247 {
248 Clear();
249 wxBitmapDataObjectBase::SetBitmap(rBitmap);
250 if ( m_bitmap.Ok() )
251 {
252 m_pictHandle = m_bitmap.GetPict( &m_pictCreated ) ;
253 }
254 }
255
256 void wxBitmapDataObject::Init()
257 {
258 m_pictHandle = NULL ;
259 m_pictCreated = false ;
260 }
261
262 void wxBitmapDataObject::Clear()
263 {
264 if ( m_pictCreated && m_pictHandle )
265 {
266 KillPicture( (PicHandle) m_pictHandle ) ;
267 }
268 m_pictHandle = NULL ;
269 }
270
271 bool wxBitmapDataObject::GetDataHere(
272 void* pBuf
273 ) const
274 {
275 if (!m_pictHandle)
276 {
277 wxFAIL_MSG(wxT("attempt to copy empty bitmap failed"));
278 return FALSE;
279 }
280 memcpy(pBuf, *(Handle)m_pictHandle, GetHandleSize((Handle)m_pictHandle));
281 return TRUE;
282 }
283
284 size_t wxBitmapDataObject::GetDataSize() const
285 {
286 return GetHandleSize((Handle)m_pictHandle) ;
287 }
288
289 bool wxBitmapDataObject::SetData(
290 size_t nSize
291 , const void* pBuf
292 )
293 {
294 Clear();
295 PicHandle picHandle = (PicHandle) NewHandle( nSize ) ;
296 memcpy( *picHandle , pBuf , nSize ) ;
297 m_pictHandle = picHandle ;
298 m_pictCreated = false ;
299 Rect frame = (**picHandle).picFrame ;
300
301 m_bitmap.SetPict( picHandle ) ;
302 m_bitmap.SetWidth( frame.right - frame.left ) ;
303 m_bitmap.SetHeight( frame.bottom - frame.top ) ;
304 return m_bitmap.Ok();
305 }