1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/dobjcmn.cpp
3 // Purpose: implementation of data object methods common to all platforms
4 // Author: Vadim Zeitlin, Robert Roebling
8 // Copyright: (c) wxWidgets Team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
26 #include "wx/dataobj.h"
28 // ----------------------------------------------------------------------------
30 // ----------------------------------------------------------------------------
32 #include "wx/listimpl.cpp"
34 WX_DEFINE_LIST(wxSimpleDataObjectList
)
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 static wxDataFormat dataFormatInvalid
;
41 WXDLLEXPORT
const wxDataFormat
& wxFormatInvalid
= dataFormatInvalid
;
43 // ============================================================================
45 // ============================================================================
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 wxDataObjectBase::~wxDataObjectBase()
55 bool wxDataObjectBase::IsSupported(const wxDataFormat
& format
,
58 size_t nFormatCount
= GetFormatCount( dir
);
59 if ( nFormatCount
== 1 )
61 return format
== GetPreferredFormat( dir
);
65 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
66 GetAllFormats( formats
, dir
);
69 for ( n
= 0; n
< nFormatCount
; n
++ )
71 if ( formats
[n
] == format
)
78 return n
< nFormatCount
;
82 // ----------------------------------------------------------------------------
83 // wxDataObjectComposite
84 // ----------------------------------------------------------------------------
86 wxDataObjectComposite::wxDataObjectComposite()
91 wxDataObjectComposite::~wxDataObjectComposite()
93 WX_CLEAR_LIST( wxSimpleDataObjectList
, m_dataObjects
);
97 wxDataObjectComposite::GetObject(const wxDataFormat
& format
) const
99 wxSimpleDataObjectList::compatibility_iterator node
= m_dataObjects
.GetFirst();
102 wxDataObjectSimple
*dataObj
= node
->GetData();
104 if ( dataObj
->GetFormat() == format
)
109 node
= node
->GetNext();
112 return (wxDataObjectSimple
*)NULL
;
115 void wxDataObjectComposite::Add(wxDataObjectSimple
*dataObject
, bool preferred
)
118 m_preferred
= m_dataObjects
.GetCount();
120 m_dataObjects
.Append( dataObject
);
124 wxDataObjectComposite::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
126 wxSimpleDataObjectList::compatibility_iterator node
= m_dataObjects
.Item( m_preferred
);
128 wxCHECK_MSG( node
, wxFormatInvalid
, wxT("no preferred format") );
130 wxDataObjectSimple
* dataObj
= node
->GetData();
132 return dataObj
->GetFormat();
135 #if defined(__WXMSW__)
137 size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat
& format
)
139 wxDataObjectSimple
*dataObj
= GetObject(format
);
141 wxCHECK_MSG( dataObj
, 0,
142 wxT("unsupported format in wxDataObjectComposite"));
144 return dataObj
->GetBufferOffset( format
);
148 const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer
,
150 const wxDataFormat
& format
)
152 wxDataObjectSimple
*dataObj
= GetObject(format
);
154 wxCHECK_MSG( dataObj
, NULL
,
155 wxT("unsupported format in wxDataObjectComposite"));
157 return dataObj
->GetSizeFromBuffer( buffer
, size
, format
);
161 void* wxDataObjectComposite::SetSizeInBuffer( void* buffer
, size_t size
,
162 const wxDataFormat
& format
)
164 wxDataObjectSimple
*dataObj
= GetObject( format
);
166 wxCHECK_MSG( dataObj
, NULL
,
167 wxT("unsupported format in wxDataObjectComposite"));
169 return dataObj
->SetSizeInBuffer( buffer
, size
, format
);
174 size_t wxDataObjectComposite::GetFormatCount(Direction
WXUNUSED(dir
)) const
176 // TODO what about the Get/Set only formats?
177 return m_dataObjects
.GetCount();
180 void wxDataObjectComposite::GetAllFormats(wxDataFormat
*formats
,
181 Direction
WXUNUSED(dir
)) const
184 wxSimpleDataObjectList::compatibility_iterator node
;
185 for ( node
= m_dataObjects
.GetFirst(); node
; node
= node
->GetNext() )
187 // TODO if ( !outputOnlyToo ) && this one counts ...
188 formats
[n
++] = node
->GetData()->GetFormat();
192 size_t wxDataObjectComposite::GetDataSize(const wxDataFormat
& format
) const
194 wxDataObjectSimple
*dataObj
= GetObject(format
);
196 wxCHECK_MSG( dataObj
, 0,
197 wxT("unsupported format in wxDataObjectComposite"));
199 return dataObj
->GetDataSize();
202 bool wxDataObjectComposite::GetDataHere(const wxDataFormat
& format
,
205 wxDataObjectSimple
*dataObj
= GetObject( format
);
207 wxCHECK_MSG( dataObj
, false,
208 wxT("unsupported format in wxDataObjectComposite"));
210 return dataObj
->GetDataHere( buf
);
213 bool wxDataObjectComposite::SetData(const wxDataFormat
& format
,
217 wxDataObjectSimple
*dataObj
= GetObject( format
);
219 wxCHECK_MSG( dataObj
, false,
220 wxT("unsupported format in wxDataObjectComposite"));
222 return dataObj
->SetData( len
, buf
);
225 // ----------------------------------------------------------------------------
227 // ----------------------------------------------------------------------------
229 #if defined(__WXGTK20__) && wxUSE_UNICODE
231 static inline wxMBConv
& GetConv(const wxDataFormat
& format
)
233 // use UTF8 for wxDF_UNICODETEXT and UCS4 for wxDF_TEXT
234 return format
== wxDF_UNICODETEXT
? wxConvUTF8
: wxConvLibc
;
237 size_t wxTextDataObject::GetDataSize(const wxDataFormat
& format
) const
239 wxCharBuffer buffer
= GetConv(format
).cWX2MB( GetText().c_str() );
241 return buffer
? strlen( buffer
) : 0;
244 bool wxTextDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
249 wxCharBuffer buffer
= GetConv(format
).cWX2MB( GetText().c_str() );
251 strcpy( (char*)buf
, buffer
);
256 bool wxTextDataObject::SetData(const wxDataFormat
& format
,
257 size_t WXUNUSED(len
), const void *buf
)
262 wxWCharBuffer buffer
= GetConv(format
).cMB2WX( (const char*)buf
);
269 #elif wxUSE_UNICODE && defined(__WXMAC__)
271 static wxMBConvUTF16 sUTF16Converter
;
273 static inline wxMBConv
& GetConv(const wxDataFormat
& format
)
276 format
== wxDF_UNICODETEXT
277 ? (wxMBConv
&) sUTF16Converter
278 : (wxMBConv
&) wxConvLocal
;
281 size_t wxTextDataObject::GetDataSize(const wxDataFormat
& format
) const
283 size_t len
= GetConv(format
).WC2MB( NULL
, GetText().c_str(), 0 );
284 len
+= (format
== wxDF_UNICODETEXT
? 2 : 1);
289 bool wxTextDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
294 wxCharBuffer buffer
= GetConv(format
).cWX2MB( GetText().c_str() );
296 size_t len
= GetConv(format
).WC2MB( NULL
, GetText().c_str(), 0 );
297 len
+= (format
== wxDF_UNICODETEXT
? 2 : 1);
299 // trailing (uni)char 0
300 memcpy( (char*)buf
, (const char*)buffer
, len
);
305 bool wxTextDataObject::SetData(const wxDataFormat
& format
,
306 size_t WXUNUSED(len
), const void *buf
)
311 wxWCharBuffer buffer
= GetConv(format
).cMB2WX( (const char*)buf
);
320 size_t wxTextDataObject::GetDataSize() const
322 return GetTextLength() * sizeof(wxChar
);
325 bool wxTextDataObject::GetDataHere(void *buf
) const
327 wxStrcpy( (wxChar
*)buf
, GetText().c_str() );
332 bool wxTextDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
334 SetText( wxString((const wxChar
*)buf
) );
341 // ----------------------------------------------------------------------------
342 // wxFileDataObjectBase
343 // ----------------------------------------------------------------------------
345 // VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should
346 // be moved to gtk/dataobj.cpp
349 wxString
wxFileDataObjectBase::GetFilenames() const
352 size_t count
= m_filenames
.GetCount();
353 for ( size_t n
= 0; n
< count
; n
++ )
355 str
<< m_filenames
[n
] << wxT('\0');
361 void wxFileDataObjectBase::SetFilenames(const wxChar
* filenames
)
366 for ( const wxChar
*pc
= filenames
; ; pc
++ )
376 // 2 consecutive NULs - this is the end of the string
380 m_filenames
.Add(current
);
388 // ----------------------------------------------------------------------------
389 // wxCustomDataObject
390 // ----------------------------------------------------------------------------
392 wxCustomDataObject::wxCustomDataObject(const wxDataFormat
& format
)
393 : wxDataObjectSimple(format
)
399 wxCustomDataObject::~wxCustomDataObject()
404 void wxCustomDataObject::TakeData(size_t size
, void *data
)
412 void *wxCustomDataObject::Alloc(size_t size
)
414 return (void *)new char[size
];
417 void wxCustomDataObject::Free()
419 delete [] (char*)m_data
;
421 m_data
= (void*)NULL
;
424 size_t wxCustomDataObject::GetDataSize() const
429 bool wxCustomDataObject::GetDataHere(void *buf
) const
434 void *data
= GetData();
438 memcpy( buf
, data
, GetSize() );
443 bool wxCustomDataObject::SetData(size_t size
, const void *buf
)
447 m_data
= Alloc(size
);
448 if ( m_data
== NULL
)
452 memcpy( m_data
, buf
, m_size
);
457 // ============================================================================
458 // some common dnd related code
459 // ============================================================================
461 #if wxUSE_DRAG_AND_DROP
465 // ----------------------------------------------------------------------------
467 // ----------------------------------------------------------------------------
469 // NB: we can't use "new" in ctor initializer lists because this provokes an
470 // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!),
471 // so use SetDataObject() instead
473 wxTextDropTarget::wxTextDropTarget()
475 SetDataObject(new wxTextDataObject
);
478 wxDragResult
wxTextDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
483 wxTextDataObject
*dobj
= (wxTextDataObject
*)m_dataObject
;
484 return OnDropText( x
, y
, dobj
->GetText() ) ? def
: wxDragNone
;
487 // ----------------------------------------------------------------------------
489 // ----------------------------------------------------------------------------
491 wxFileDropTarget::wxFileDropTarget()
493 SetDataObject(new wxFileDataObject
);
496 wxDragResult
wxFileDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
501 wxFileDataObject
*dobj
= (wxFileDataObject
*)m_dataObject
;
502 return OnDropFiles( x
, y
, dobj
->GetFilenames() ) ? def
: wxDragNone
;
505 #endif // wxUSE_DRAG_AND_DROP
507 #endif // wxUSE_DATAOBJ