]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/dataobj.cpp
multi selection test
[wxWidgets.git] / src / mac / carbon / dataobj.cpp
CommitLineData
72e7876b 1///////////////////////////////////////////////////////////////////////////////
427ff662
SC
2// Name: mac/dataobj.cpp
3// Purpose: implementation of wxDataObject class
4// Author: Stefan Csomor
72e7876b
SC
5// Modified by:
6// Created: 10/21/99
7// RCS-ID: $Id$
427ff662 8// Copyright: (c) 1999 Stefan Csomor
6aa89a22 9// Licence: wxWindows licence
72e7876b
SC
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"
76a5e5d2 36#include "wx/mac/private.h"
427ff662 37#include "Scrap.h"
72e7876b
SC
38
39// ----------------------------------------------------------------------------
40// functions
41// ----------------------------------------------------------------------------
42
43// ----------------------------------------------------------------------------
44// wxDataFormat
45// ----------------------------------------------------------------------------
46
47wxDataFormat::wxDataFormat()
48{
2f1ae414
SC
49 m_type = wxDF_INVALID;
50 m_format = 0;
72e7876b
SC
51}
52
2f1ae414 53wxDataFormat::wxDataFormat( wxDataFormatId vType )
72e7876b 54{
72e7876b
SC
55 SetType(vType);
56}
57
2f1ae414 58wxDataFormat::wxDataFormat( const wxChar* zId)
72e7876b 59{
72e7876b
SC
60 SetId(zId);
61}
62
2f1ae414 63wxDataFormat::wxDataFormat( const wxString& rId)
72e7876b 64{
72e7876b
SC
65 SetId(rId);
66}
67
2f1ae414 68wxDataFormat::wxDataFormat( NativeFormat vFormat)
72e7876b 69{
72e7876b
SC
70 SetId(vFormat);
71}
72
2f1ae414 73void wxDataFormat::SetType( wxDataFormatId Type )
72e7876b 74{
2f1ae414
SC
75 m_type = Type;
76
427ff662
SC
77 if (m_type == wxDF_TEXT )
78 m_format = kScrapFlavorTypeText;
79 else if (m_type == wxDF_UNICODETEXT )
80 m_format = kScrapFlavorTypeUnicode ;
2f1ae414 81 else if (m_type == wxDF_BITMAP || m_type == wxDF_METAFILE )
427ff662 82 m_format = kScrapFlavorTypePicture;
2f1ae414 83 else if (m_type == wxDF_FILENAME)
a07c1212 84 m_format = kDragFlavorTypeHFS ;
72e7876b
SC
85 else
86 {
87 wxFAIL_MSG( wxT("invalid dataformat") );
72e7876b 88
63b8dd39
VZ
89 m_format = '????';
90 }
72e7876b
SC
91}
92
93wxString wxDataFormat::GetId() const
94{
63b8dd39
VZ
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
427ff662 101 return wxString::FromAscii( text ) ;
72e7876b
SC
102}
103
2f1ae414 104void wxDataFormat::SetId( NativeFormat format )
72e7876b 105{
2f1ae414
SC
106 m_format = format;
107
427ff662 108 if (m_format == kScrapFlavorTypeText)
72e7876b 109 m_type = wxDF_TEXT;
63b8dd39 110 else if (m_format == kScrapFlavorTypeUnicode )
427ff662 111 m_type = wxDF_UNICODETEXT;
63b8dd39 112 else if (m_format == kScrapFlavorTypePicture)
72e7876b 113 m_type = wxDF_BITMAP;
63b8dd39 114 else if (m_format == kDragFlavorTypeHFS )
72e7876b
SC
115 m_type = wxDF_FILENAME;
116 else
117 m_type = wxDF_PRIVATE;
72e7876b
SC
118}
119
2f1ae414 120void wxDataFormat::SetId( const wxChar* zId )
72e7876b 121{
2f1ae414
SC
122 m_type = wxDF_PRIVATE;
123 m_format = 0;// TODO: get the format gdk_atom_intern( wxMBSTRINGCAST tmp.mbc_str(), FALSE );
72e7876b
SC
124}
125
126//-------------------------------------------------------------------------
127// wxDataObject
128//-------------------------------------------------------------------------
129
130wxDataObject::wxDataObject()
131{
132}
133
134bool 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 {
e40298d5 147 wxDataFormat* pFormats = new wxDataFormat[nFormatCount];
72e7876b
SC
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
171bool 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
187size_t wxFileDataObject::GetDataSize() const
188{
e40298d5 189 size_t nRes = 0;
72e7876b
SC
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
200bool wxFileDataObject::SetData(
201 size_t WXUNUSED(nSize)
202, const void* pBuf
203)
204{
a07c1212 205 m_filenames.Empty();
72e7876b 206
427ff662 207 AddFile(wxString::FromAscii((char*)pBuf));
72e7876b
SC
208
209 return TRUE;
210}
211
212void wxFileDataObject::AddFile(
213 const wxString& rFilename
214)
215{
216 m_filenames.Add(rFilename);
217}
218
219// ----------------------------------------------------------------------------
220// wxBitmapDataObject
221// ----------------------------------------------------------------------------
222
223wxBitmapDataObject::wxBitmapDataObject()
224{
225 Init();
226}
227
228wxBitmapDataObject::wxBitmapDataObject(
e40298d5 229 const wxBitmap& rBitmap
72e7876b
SC
230)
231: wxBitmapDataObjectBase(rBitmap)
232{
233 Init();
2a391f87
SC
234 if ( m_bitmap.Ok() )
235 {
e40298d5
JS
236 m_pictHandle = m_bitmap.GetPict( &m_pictCreated ) ;
237 }
72e7876b
SC
238}
239
240wxBitmapDataObject::~wxBitmapDataObject()
241{
242 Clear();
243}
244
245void wxBitmapDataObject::SetBitmap(
246 const wxBitmap& rBitmap
247)
248{
2a391f87 249 Clear();
72e7876b 250 wxBitmapDataObjectBase::SetBitmap(rBitmap);
2a391f87
SC
251 if ( m_bitmap.Ok() )
252 {
e40298d5
JS
253 m_pictHandle = m_bitmap.GetPict( &m_pictCreated ) ;
254 }
2a391f87
SC
255}
256
257void wxBitmapDataObject::Init()
258{
e40298d5
JS
259 m_pictHandle = NULL ;
260 m_pictCreated = false ;
2a391f87
SC
261}
262
263void wxBitmapDataObject::Clear()
264{
e40298d5
JS
265 if ( m_pictCreated && m_pictHandle )
266 {
267 KillPicture( (PicHandle) m_pictHandle ) ;
268 }
269 m_pictHandle = NULL ;
72e7876b
SC
270}
271
272bool wxBitmapDataObject::GetDataHere(
273 void* pBuf
274) const
275{
2a391f87 276 if (!m_pictHandle)
72e7876b
SC
277 {
278 wxFAIL_MSG(wxT("attempt to copy empty bitmap failed"));
279 return FALSE;
280 }
2a391f87 281 memcpy(pBuf, *(Handle)m_pictHandle, GetHandleSize((Handle)m_pictHandle));
72e7876b
SC
282 return TRUE;
283}
284
2a391f87
SC
285size_t wxBitmapDataObject::GetDataSize() const
286{
e40298d5 287 return GetHandleSize((Handle)m_pictHandle) ;
2a391f87
SC
288}
289
72e7876b
SC
290bool wxBitmapDataObject::SetData(
291 size_t nSize
292, const void* pBuf
293)
294{
295 Clear();
2a391f87
SC
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 ) ;
e40298d5
JS
303 m_bitmap.SetWidth( frame.right - frame.left ) ;
304 m_bitmap.SetHeight( frame.bottom - frame.top ) ;
72e7876b
SC
305 return m_bitmap.Ok();
306}