]>
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/tokenzr.h"
29 #include "wx/mac/private.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 wxDataFormat::wxDataFormat()
42 m_type
= wxDF_INVALID
;
46 wxDataFormat::wxDataFormat( wxDataFormatId vType
)
51 wxDataFormat::wxDataFormat( const wxChar
*zId
)
56 wxDataFormat::wxDataFormat( const wxString
& rId
)
61 wxDataFormat::wxDataFormat( NativeFormat vFormat
)
66 void wxDataFormat::SetType( wxDataFormatId dataType
)
73 m_format
= kScrapFlavorTypeText
;
76 case wxDF_UNICODETEXT
:
77 m_format
= kScrapFlavorTypeUnicode
;
82 m_format
= kScrapFlavorTypePicture
;
86 m_format
= kDragFlavorTypeHFS
;
90 wxFAIL_MSG( wxT("invalid data format") );
92 // NB: this translates to '????' ASCII but it can't be used in the code
93 // because '??' will get parsed as a trigraph!
94 m_format
= 0x3f3f3f3f;
99 wxString
wxDataFormat::GetId() const
101 wxCHECK_MSG( !IsStandard(), wxEmptyString
,
102 wxT("name of predefined format cannot be retrieved") );
107 void wxDataFormat::SetId( NativeFormat format
)
113 case kScrapFlavorTypeText
:
117 case kScrapFlavorTypeUnicode
:
118 m_type
= wxDF_UNICODETEXT
;
121 case kScrapFlavorTypePicture
:
122 m_type
= wxDF_BITMAP
;
125 case kDragFlavorTypeHFS
:
126 m_type
= wxDF_FILENAME
;
130 m_type
= wxDF_PRIVATE
;
132 memcpy( text
, (const char*)&format
, 4 );
134 m_id
= wxString::FromAscii( text
);
139 void wxDataFormat::SetId( const wxChar
* zId
)
141 m_type
= wxDF_PRIVATE
;
146 bool wxDataFormat::operator==(const wxDataFormat
& format
) const
148 if (IsStandard() || format
.IsStandard())
149 return (format
.m_type
== m_type
);
151 return (m_id
== format
.m_id
);
154 //-------------------------------------------------------------------------
156 //-------------------------------------------------------------------------
158 wxDataObject::wxDataObject()
162 bool wxDataObject::IsSupportedFormat( const wxDataFormat
& rFormat
, Direction vDir
) const
164 size_t nFormatCount
= GetFormatCount( vDir
);
167 if (nFormatCount
== 1)
169 found
= (rFormat
== GetPreferredFormat());
173 wxDataFormat
*pFormats
= new wxDataFormat
[nFormatCount
];
174 GetAllFormats( pFormats
, vDir
);
176 for (size_t n
= 0; n
< nFormatCount
; n
++)
178 if (pFormats
[n
] == rFormat
)
191 // ----------------------------------------------------------------------------
193 // ----------------------------------------------------------------------------
196 void wxTextDataObject::GetAllFormats( wxDataFormat
*formats
, wxDataObjectBase::Direction dir
) const
198 *formats
++ = wxDataFormat( wxDF_TEXT
);
199 *formats
= wxDataFormat( wxDF_UNICODETEXT
);
203 // ----------------------------------------------------------------------------
205 // ----------------------------------------------------------------------------
207 void wxFileDataObject::GetFileNames( wxCharBuffer
&buf
) const
211 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
213 filenames
+= m_filenames
[i
];
214 filenames
+= wxT('\n');
217 buf
= filenames
.fn_str();
220 bool wxFileDataObject::GetDataHere( void *pBuf
) const
229 buffLength
= strlen( buf
);
230 memcpy( pBuf
, (const char*)buf
, buffLength
+ 1 );
235 size_t wxFileDataObject::GetDataSize() const
241 buffLength
= strlen( buf
);
243 return buffLength
+ 1;
246 bool wxFileDataObject::SetData( size_t nSize
, const void *pBuf
)
251 filenames
= wxString( (const char*)pBuf
, *wxConvFileName
);
253 filenames
= wxString( wxConvFileName
->cMB2WX( (const char*)pBuf
), wxConvLocal
);
256 m_filenames
= wxStringTokenize( filenames
, wxT("\n"), wxTOKEN_STRTOK
);
261 void wxFileDataObject::AddFile( const wxString
& rFilename
)
263 m_filenames
.Add( rFilename
);
266 // ----------------------------------------------------------------------------
267 // wxBitmapDataObject
268 // ----------------------------------------------------------------------------
270 wxBitmapDataObject::wxBitmapDataObject()
275 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& rBitmap
)
276 : wxBitmapDataObjectBase( rBitmap
)
282 m_pictHandle
= m_bitmap
.GetBitmapData()->GetPictHandle();
283 m_pictCreated
= false;
287 wxBitmapDataObject::~wxBitmapDataObject()
292 void wxBitmapDataObject::SetBitmap( const wxBitmap
& rBitmap
)
295 wxBitmapDataObjectBase::SetBitmap( rBitmap
);
298 m_pictHandle
= m_bitmap
.GetBitmapData()->GetPictHandle();
299 m_pictCreated
= false;
303 void wxBitmapDataObject::Init()
306 m_pictCreated
= false;
309 void wxBitmapDataObject::Clear()
311 if (m_pictHandle
!= NULL
)
314 KillPicture( (PicHandle
)m_pictHandle
);
318 m_pictCreated
= false;
321 bool wxBitmapDataObject::GetDataHere( void *pBuf
) const
323 if (m_pictHandle
== NULL
)
325 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
332 memcpy( pBuf
, *(Handle
)m_pictHandle
, GetHandleSize( (Handle
)m_pictHandle
) );
337 size_t wxBitmapDataObject::GetDataSize() const
339 if (m_pictHandle
!= NULL
)
340 return GetHandleSize( (Handle
)m_pictHandle
);
345 bool wxBitmapDataObject::SetData( size_t nSize
, const void *pBuf
)
349 if ((pBuf
== NULL
) || (nSize
== 0))
352 PicHandle picHandle
= (PicHandle
)NewHandle( nSize
);
353 memcpy( *picHandle
, pBuf
, nSize
);
354 m_pictHandle
= picHandle
;
356 // ownership is transferred to the bitmap
357 m_pictCreated
= false;
359 wxMacGetPictureBounds( picHandle
, &frame
);
362 mf
.SetHMETAFILE( (WXHMETAFILE
)m_pictHandle
);
364 m_bitmap
.Create( frame
.right
- frame
.left
, frame
.bottom
- frame
.top
);
365 mdc
.SelectObject( m_bitmap
);
367 mdc
.SelectObject( wxNullBitmap
);
369 return m_bitmap
.Ok();