]>
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"
17 #include "wx/dataobj.h"
22 #include "wx/dcmemory.h"
26 #include "wx/mstream.h"
27 #include "wx/metafile.h"
28 #include "wx/tokenzr.h"
30 #include "wx/mac/private.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 wxDataFormat::wxDataFormat()
43 m_type
= wxDF_INVALID
;
47 wxDataFormat::wxDataFormat( wxDataFormatId vType
)
52 wxDataFormat::wxDataFormat( const wxChar
*zId
)
57 wxDataFormat::wxDataFormat( const wxString
& rId
)
62 wxDataFormat::wxDataFormat( NativeFormat vFormat
)
67 void wxDataFormat::SetType( wxDataFormatId dataType
)
74 m_format
= kScrapFlavorTypeText
;
77 case wxDF_UNICODETEXT
:
78 m_format
= kScrapFlavorTypeUnicode
;
83 m_format
= kScrapFlavorTypePicture
;
87 m_format
= kDragFlavorTypeHFS
;
91 wxFAIL_MSG( wxT("invalid data format") );
93 // NB: this translates to '????' ASCII but it can't be used in the code
94 // because '??' will get parsed as a trigraph!
95 m_format
= 0x3f3f3f3f;
100 wxString
wxDataFormat::GetId() const
102 wxCHECK_MSG( !IsStandard(), wxEmptyString
,
103 wxT("name of predefined format cannot be retrieved") );
108 void wxDataFormat::SetId( NativeFormat format
)
114 case kScrapFlavorTypeText
:
118 case kScrapFlavorTypeUnicode
:
119 m_type
= wxDF_UNICODETEXT
;
122 case kScrapFlavorTypePicture
:
123 m_type
= wxDF_BITMAP
;
126 case kDragFlavorTypeHFS
:
127 m_type
= wxDF_FILENAME
;
131 m_type
= wxDF_PRIVATE
;
133 memcpy( text
, (const char*)&format
, 4 );
135 m_id
= wxString::FromAscii( text
);
140 void wxDataFormat::SetId( const wxChar
* zId
)
142 m_type
= wxDF_PRIVATE
;
147 bool wxDataFormat::operator==(const wxDataFormat
& format
) const
149 if (IsStandard() || format
.IsStandard())
150 return (format
.m_type
== m_type
);
152 return (m_id
== format
.m_id
);
155 //-------------------------------------------------------------------------
157 //-------------------------------------------------------------------------
159 wxDataObject::wxDataObject()
163 bool wxDataObject::IsSupportedFormat( const wxDataFormat
& rFormat
, Direction vDir
) const
165 size_t nFormatCount
= GetFormatCount( vDir
);
168 if (nFormatCount
== 1)
170 found
= (rFormat
== GetPreferredFormat());
174 wxDataFormat
*pFormats
= new wxDataFormat
[nFormatCount
];
175 GetAllFormats( pFormats
, vDir
);
177 for (size_t n
= 0; n
< nFormatCount
; n
++)
179 if (pFormats
[n
] == rFormat
)
192 // ----------------------------------------------------------------------------
194 // ----------------------------------------------------------------------------
197 void wxTextDataObject::GetAllFormats( wxDataFormat
*formats
, wxDataObjectBase::Direction dir
) const
199 *formats
++ = wxDataFormat( wxDF_TEXT
);
200 *formats
= wxDataFormat( wxDF_UNICODETEXT
);
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
208 void wxFileDataObject::GetFileNames( wxCharBuffer
&buf
) const
212 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
214 filenames
+= m_filenames
[i
];
215 filenames
+= wxT('\n');
218 buf
= filenames
.fn_str();
221 bool wxFileDataObject::GetDataHere( void *pBuf
) const
230 buffLength
= strlen( buf
);
231 memcpy( pBuf
, (const char*)buf
, buffLength
+ 1 );
236 size_t wxFileDataObject::GetDataSize() const
242 buffLength
= strlen( buf
);
244 return buffLength
+ 1;
247 bool wxFileDataObject::SetData( size_t nSize
, const void *pBuf
)
252 filenames
= wxString( (const char*)pBuf
, *wxConvFileName
);
254 filenames
= wxString (wxConvLocal
.cWC2WX(wxConvFileName
->cMB2WC( (const char*)pBuf
)));
257 m_filenames
= wxStringTokenize( filenames
, wxT("\n"), wxTOKEN_STRTOK
);
262 void wxFileDataObject::AddFile( const wxString
& rFilename
)
264 m_filenames
.Add( rFilename
);
267 // ----------------------------------------------------------------------------
268 // wxBitmapDataObject
269 // ----------------------------------------------------------------------------
271 wxBitmapDataObject::wxBitmapDataObject()
276 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& rBitmap
)
277 : wxBitmapDataObjectBase( rBitmap
)
283 m_pictHandle
= m_bitmap
.GetBitmapData()->GetPictHandle();
284 m_pictCreated
= false;
288 wxBitmapDataObject::~wxBitmapDataObject()
293 void wxBitmapDataObject::SetBitmap( const wxBitmap
& rBitmap
)
296 wxBitmapDataObjectBase::SetBitmap( rBitmap
);
299 m_pictHandle
= m_bitmap
.GetBitmapData()->GetPictHandle();
300 m_pictCreated
= false;
304 void wxBitmapDataObject::Init()
307 m_pictCreated
= false;
310 void wxBitmapDataObject::Clear()
312 if (m_pictHandle
!= NULL
)
315 KillPicture( (PicHandle
)m_pictHandle
);
319 m_pictCreated
= false;
322 bool wxBitmapDataObject::GetDataHere( void *pBuf
) const
324 if (m_pictHandle
== NULL
)
326 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
333 memcpy( pBuf
, *(Handle
)m_pictHandle
, GetHandleSize( (Handle
)m_pictHandle
) );
338 size_t wxBitmapDataObject::GetDataSize() const
340 if (m_pictHandle
!= NULL
)
341 return GetHandleSize( (Handle
)m_pictHandle
);
346 bool wxBitmapDataObject::SetData( size_t nSize
, const void *pBuf
)
350 if ((pBuf
== NULL
) || (nSize
== 0))
353 PicHandle picHandle
= (PicHandle
)NewHandle( nSize
);
354 memcpy( *picHandle
, pBuf
, nSize
);
355 m_pictHandle
= picHandle
;
357 // ownership is transferred to the bitmap
358 m_pictCreated
= false;
360 wxMacGetPictureBounds( picHandle
, &frame
);
363 mf
.SetHMETAFILE( (WXHMETAFILE
)m_pictHandle
);
365 m_bitmap
.Create( frame
.right
- frame
.left
, frame
.bottom
- frame
.top
);
366 mdc
.SelectObject( m_bitmap
);
368 mdc
.SelectObject( wxNullBitmap
);
370 return m_bitmap
.Ok();