]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dataobj.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/dataobj.cpp
3 // Purpose: implementation of wxDataObject class
4 // Author: Stefan Csomor
8 // Copyright: (c) 1999 Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
22 #include "wx/dataobj.h"
23 #include "wx/dcmemory.h"
24 #include "wx/mstream.h"
26 #include "wx/metafile.h"
27 #include "wx/mac/private.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 wxDataFormat::wxDataFormat()
40 m_type
= wxDF_INVALID
;
44 wxDataFormat::wxDataFormat( wxDataFormatId vType
)
49 wxDataFormat::wxDataFormat( const wxChar
*zId
)
54 wxDataFormat::wxDataFormat( const wxString
& rId
)
59 wxDataFormat::wxDataFormat( NativeFormat vFormat
)
64 void wxDataFormat::SetType( wxDataFormatId dataType
)
71 m_format
= kScrapFlavorTypeText
;
74 case wxDF_UNICODETEXT
:
75 m_format
= kScrapFlavorTypeUnicode
;
80 m_format
= kScrapFlavorTypePicture
;
84 m_format
= kDragFlavorTypeHFS
;
88 wxFAIL_MSG( wxT("invalid data format") );
90 // NB: this translates to '????' ASCII but it can't be used in the code
91 // because '??' will get parsed as a trigraph!
92 m_format
= 0x3f3f3f3f;
97 wxString
wxDataFormat::GetId() const
99 wxCHECK_MSG( !IsStandard(), wxEmptyString
,
100 wxT("name of predefined format cannot be retrieved") );
105 void wxDataFormat::SetId( NativeFormat format
)
111 case kScrapFlavorTypeText
:
115 case kScrapFlavorTypeUnicode
:
116 m_type
= wxDF_UNICODETEXT
;
119 case kScrapFlavorTypePicture
:
120 m_type
= wxDF_BITMAP
;
123 case kDragFlavorTypeHFS
:
124 m_type
= wxDF_FILENAME
;
128 m_type
= wxDF_PRIVATE
;
130 strncpy( text
, (char*)&format
, 4 );
132 m_id
= wxString::FromAscii( text
);
137 void wxDataFormat::SetId( const wxChar
* zId
)
139 m_type
= wxDF_PRIVATE
;
144 bool wxDataFormat::operator==(const wxDataFormat
& format
) const
146 if (IsStandard() || format
.IsStandard())
147 return (format
.m_type
== m_type
);
149 return (m_id
== format
.m_id
);
152 //-------------------------------------------------------------------------
154 //-------------------------------------------------------------------------
156 wxDataObject::wxDataObject()
160 bool wxDataObject::IsSupportedFormat( const wxDataFormat
& rFormat
, Direction vDir
) const
162 size_t nFormatCount
= GetFormatCount( vDir
);
165 if (nFormatCount
== 1)
167 found
= (rFormat
== GetPreferredFormat());
171 wxDataFormat
* pFormats
= new wxDataFormat
[nFormatCount
];
172 GetAllFormats( pFormats
, vDir
);
174 for (size_t n
= 0; n
< nFormatCount
; n
++)
176 if (pFormats
[n
] == rFormat
)
189 // ----------------------------------------------------------------------------
191 // ----------------------------------------------------------------------------
194 void wxTextDataObject::GetAllFormats( wxDataFormat
*formats
, wxDataObjectBase::Direction dir
) const
196 *formats
++ = wxDataFormat( wxDF_TEXT
);
197 *formats
= wxDataFormat( wxDF_UNICODETEXT
);
201 // ----------------------------------------------------------------------------
203 // ----------------------------------------------------------------------------
205 bool wxFileDataObject::GetDataHere( void *pBuf
) const
212 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
214 sFilenames
+= m_filenames
[i
];
215 sFilenames
+= (wxChar
)0;
218 memcpy( pBuf
, sFilenames
.mbc_str(), sFilenames
.Len() + 1 );
223 size_t wxFileDataObject::GetDataSize() const
227 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
229 nRes
+= m_filenames
[i
].Len();
236 bool wxFileDataObject::SetData( size_t WXUNUSED(nSize
), const void *pBuf
)
240 // only add if this is not an empty string
241 // we can therefore clear the list by just setting an empty string
242 if ((*(const char*)pBuf
) != 0)
243 AddFile( wxString::FromAscii( (char*)pBuf
) );
248 void wxFileDataObject::AddFile( const wxString
& rFilename
)
250 m_filenames
.Add( rFilename
);
253 // ----------------------------------------------------------------------------
254 // wxBitmapDataObject
255 // ----------------------------------------------------------------------------
257 wxBitmapDataObject::wxBitmapDataObject()
262 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& rBitmap
)
263 : wxBitmapDataObjectBase( rBitmap
)
269 m_pictHandle
= m_bitmap
.GetBitmapData()->GetPictHandle();
270 m_pictCreated
= false;
274 wxBitmapDataObject::~wxBitmapDataObject()
279 void wxBitmapDataObject::SetBitmap( const wxBitmap
& rBitmap
)
282 wxBitmapDataObjectBase::SetBitmap( rBitmap
);
285 m_pictHandle
= m_bitmap
.GetBitmapData()->GetPictHandle();
286 m_pictCreated
= false;
290 void wxBitmapDataObject::Init()
293 m_pictCreated
= false;
296 void wxBitmapDataObject::Clear()
298 if (m_pictHandle
!= NULL
)
301 KillPicture( (PicHandle
)m_pictHandle
);
305 m_pictCreated
= false;
308 bool wxBitmapDataObject::GetDataHere( void *pBuf
) const
310 if (m_pictHandle
== NULL
)
312 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
319 memcpy( pBuf
, *(Handle
)m_pictHandle
, GetHandleSize( (Handle
)m_pictHandle
) );
324 size_t wxBitmapDataObject::GetDataSize() const
326 if (m_pictHandle
!= NULL
)
327 return GetHandleSize( (Handle
)m_pictHandle
);
332 bool wxBitmapDataObject::SetData( size_t nSize
, const void *pBuf
)
336 if ((pBuf
== NULL
) || (nSize
== 0))
339 PicHandle picHandle
= (PicHandle
)NewHandle( nSize
);
340 memcpy( *picHandle
, pBuf
, nSize
);
341 m_pictHandle
= picHandle
;
343 // ownership is transferred to the bitmap
344 m_pictCreated
= false;
346 wxMacGetPictureBounds( picHandle
, &frame
);
349 mf
.SetHMETAFILE( (WXHMETAFILE
)m_pictHandle
);
351 m_bitmap
.Create( frame
.right
- frame
.left
, frame
.bottom
- frame
.top
);
352 mdc
.SelectObject( m_bitmap
);
354 mdc
.SelectObject( wxNullBitmap
);
356 return m_bitmap
.Ok();