]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dataobj.cpp
5de37f5e061426fa6c3444a98ec276df962a3884
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"
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 strncpy( text
, (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
229 memcpy( pBuf
, (const char*) buf
, strlen(buf
) + 1 );
234 size_t wxFileDataObject::GetDataSize() const
239 return strlen(buf
) + 1;
242 bool wxFileDataObject::SetData( size_t nSize
, const void *pBuf
)
246 filenames
= wxString( (const char*) pBuf
, *wxConvFileName
);
248 filenames
= wxString( wxConvFileName
->cMB2WX( pBuf
) , wxConvLocal
);
251 m_filenames
= wxStringTokenize( filenames
, wxT("\n") , wxTOKEN_STRTOK
);
256 void wxFileDataObject::AddFile( const wxString
& rFilename
)
258 m_filenames
.Add( rFilename
);
261 // ----------------------------------------------------------------------------
262 // wxBitmapDataObject
263 // ----------------------------------------------------------------------------
265 wxBitmapDataObject::wxBitmapDataObject()
270 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& rBitmap
)
271 : wxBitmapDataObjectBase( rBitmap
)
277 m_pictHandle
= m_bitmap
.GetBitmapData()->GetPictHandle();
278 m_pictCreated
= false;
282 wxBitmapDataObject::~wxBitmapDataObject()
287 void wxBitmapDataObject::SetBitmap( const wxBitmap
& rBitmap
)
290 wxBitmapDataObjectBase::SetBitmap( rBitmap
);
293 m_pictHandle
= m_bitmap
.GetBitmapData()->GetPictHandle();
294 m_pictCreated
= false;
298 void wxBitmapDataObject::Init()
301 m_pictCreated
= false;
304 void wxBitmapDataObject::Clear()
306 if (m_pictHandle
!= NULL
)
309 KillPicture( (PicHandle
)m_pictHandle
);
313 m_pictCreated
= false;
316 bool wxBitmapDataObject::GetDataHere( void *pBuf
) const
318 if (m_pictHandle
== NULL
)
320 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
327 memcpy( pBuf
, *(Handle
)m_pictHandle
, GetHandleSize( (Handle
)m_pictHandle
) );
332 size_t wxBitmapDataObject::GetDataSize() const
334 if (m_pictHandle
!= NULL
)
335 return GetHandleSize( (Handle
)m_pictHandle
);
340 bool wxBitmapDataObject::SetData( size_t nSize
, const void *pBuf
)
344 if ((pBuf
== NULL
) || (nSize
== 0))
347 PicHandle picHandle
= (PicHandle
)NewHandle( nSize
);
348 memcpy( *picHandle
, pBuf
, nSize
);
349 m_pictHandle
= picHandle
;
351 // ownership is transferred to the bitmap
352 m_pictCreated
= false;
354 wxMacGetPictureBounds( picHandle
, &frame
);
357 mf
.SetHMETAFILE( (WXHMETAFILE
)m_pictHandle
);
359 m_bitmap
.Create( frame
.right
- frame
.left
, frame
.bottom
- frame
.top
);
360 mdc
.SelectObject( m_bitmap
);
362 mdc
.SelectObject( wxNullBitmap
);
364 return m_bitmap
.Ok();