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