]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/dataobj.cpp
b3b42de40a6a06b28e7ec9df98682745215e551c
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 wxString
& rId
)
57 wxDataFormat::wxDataFormat( NativeFormat vFormat
)
62 void wxDataFormat::SetType( wxDataFormatId dataType
)
69 m_format
= kScrapFlavorTypeText
;
72 case wxDF_UNICODETEXT
:
73 m_format
= kScrapFlavorTypeUnicode
;
78 m_format
= kScrapFlavorTypePicture
;
82 m_format
= kDragFlavorTypeHFS
;
86 wxFAIL_MSG( wxT("invalid data format") );
88 // NB: this translates to '????' ASCII but it can't be used in the code
89 // because '??' will get parsed as a trigraph!
90 m_format
= 0x3f3f3f3f;
95 wxString
wxDataFormat::GetId() const
97 wxCHECK_MSG( !IsStandard(), wxEmptyString
,
98 wxT("name of predefined format cannot be retrieved") );
103 void wxDataFormat::SetId( NativeFormat format
)
109 case kScrapFlavorTypeText
:
113 case kScrapFlavorTypeUnicode
:
114 m_type
= wxDF_UNICODETEXT
;
117 case kScrapFlavorTypePicture
:
118 m_type
= wxDF_BITMAP
;
121 case kDragFlavorTypeHFS
:
122 m_type
= wxDF_FILENAME
;
126 m_type
= wxDF_PRIVATE
;
128 memcpy( text
, (const char*)&format
, 4 );
130 m_id
= wxString::FromAscii( text
);
135 void wxDataFormat::SetId( const wxString
& zId
)
137 m_type
= wxDF_PRIVATE
;
142 bool wxDataFormat::operator==(const wxDataFormat
& format
) const
144 if (IsStandard() || format
.IsStandard())
145 return (format
.m_type
== m_type
);
147 return (m_id
== format
.m_id
);
150 //-------------------------------------------------------------------------
152 //-------------------------------------------------------------------------
154 wxDataObject::wxDataObject()
158 bool wxDataObject::IsSupportedFormat( const wxDataFormat
& rFormat
, Direction vDir
) const
160 size_t nFormatCount
= GetFormatCount( vDir
);
163 if (nFormatCount
== 1)
165 found
= (rFormat
== GetPreferredFormat());
169 wxDataFormat
*pFormats
= new wxDataFormat
[nFormatCount
];
170 GetAllFormats( pFormats
, vDir
);
172 for (size_t n
= 0; n
< nFormatCount
; n
++)
174 if (pFormats
[n
] == rFormat
)
187 // ----------------------------------------------------------------------------
189 // ----------------------------------------------------------------------------
192 void wxTextDataObject::GetAllFormats(wxDataFormat
*formats
,
193 wxDataObjectBase::Direction
WXUNUSED(dir
)) const
195 *formats
++ = wxDataFormat( wxDF_TEXT
);
196 *formats
= wxDataFormat( wxDF_UNICODETEXT
);
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
204 void wxFileDataObject::GetFileNames( wxCharBuffer
&buf
) const
208 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
210 filenames
+= m_filenames
[i
];
211 filenames
+= wxT('\n');
214 buf
= filenames
.fn_str();
217 bool wxFileDataObject::GetDataHere( void *pBuf
) const
226 buffLength
= strlen( buf
);
227 memcpy( pBuf
, (const char*)buf
, buffLength
+ 1 );
232 size_t wxFileDataObject::GetDataSize() const
238 buffLength
= strlen( buf
);
240 return buffLength
+ 1;
243 bool wxFileDataObject::SetData( size_t WXUNUSED(nSize
), const void *pBuf
)
248 filenames
= wxString( (const char*)pBuf
, *wxConvFileName
);
250 filenames
= wxString (wxConvLocal
.cWC2WX(wxConvFileName
->cMB2WC( (const char*)pBuf
)));
253 m_filenames
= wxStringTokenize( filenames
, wxT("\n"), wxTOKEN_STRTOK
);
258 void wxFileDataObject::AddFile( const wxString
& rFilename
)
260 m_filenames
.Add( rFilename
);
263 // ----------------------------------------------------------------------------
264 // wxBitmapDataObject
265 // ----------------------------------------------------------------------------
267 wxBitmapDataObject::wxBitmapDataObject()
272 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& rBitmap
)
273 : wxBitmapDataObjectBase( rBitmap
)
279 m_pictHandle
= m_bitmap
.GetBitmapData()->GetPictHandle();
280 m_pictCreated
= false;
284 wxBitmapDataObject::~wxBitmapDataObject()
289 void wxBitmapDataObject::SetBitmap( const wxBitmap
& rBitmap
)
292 wxBitmapDataObjectBase::SetBitmap( rBitmap
);
295 m_pictHandle
= m_bitmap
.GetBitmapData()->GetPictHandle();
296 m_pictCreated
= false;
300 void wxBitmapDataObject::Init()
303 m_pictCreated
= false;
306 void wxBitmapDataObject::Clear()
308 if (m_pictHandle
!= NULL
)
312 KillPicture( (PicHandle
)m_pictHandle
);
317 m_pictCreated
= false;
320 bool wxBitmapDataObject::GetDataHere( void *pBuf
) const
322 if (m_pictHandle
== NULL
)
324 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
331 memcpy( pBuf
, *(Handle
)m_pictHandle
, GetHandleSize( (Handle
)m_pictHandle
) );
336 size_t wxBitmapDataObject::GetDataSize() const
338 if (m_pictHandle
!= NULL
)
339 return GetHandleSize( (Handle
)m_pictHandle
);
344 bool wxBitmapDataObject::SetData( size_t nSize
, const void *pBuf
)
348 if ((pBuf
== NULL
) || (nSize
== 0))
351 PicHandle picHandle
= (PicHandle
)NewHandle( nSize
);
352 memcpy( *picHandle
, pBuf
, nSize
);
353 m_pictHandle
= picHandle
;
355 // ownership is transferred to the bitmap
356 m_pictCreated
= false;
359 wxMacGetPictureBounds( picHandle
, &frame
);
362 mf
.SetHMETAFILE( (WXHMETAFILE
)m_pictHandle
);
365 m_bitmap
.Create( frame
.right
- frame
.left
, frame
.bottom
- frame
.top
);
366 mdc
.SelectObject( m_bitmap
);
370 mdc
.SelectObject( wxNullBitmap
);
373 return m_bitmap
.Ok();