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() );
253 memcpy( (char*) buf
, buffer
, GetDataSize(format
) );
254 // strcpy( (char*) buf, buffer );
259 bool wxTextDataObject::SetData(const wxDataFormat
& format
,
260 size_t WXUNUSED(len
), const void *buf
)
265 wxWCharBuffer buffer
= GetConv(format
).cMB2WX( (const char*)buf
);
272 #elif wxUSE_UNICODE && defined(__WXMAC__)
274 static wxMBConvUTF16 sUTF16Converter
;
276 static inline wxMBConv
& GetConv(const wxDataFormat
& format
)
279 format
== wxDF_UNICODETEXT
280 ? (wxMBConv
&) sUTF16Converter
281 : (wxMBConv
&) wxConvLocal
;
284 size_t wxTextDataObject::GetDataSize(const wxDataFormat
& format
) const
286 size_t len
= GetConv(format
).WC2MB( NULL
, GetText().c_str(), 0 );
287 len
+= (format
== wxDF_UNICODETEXT
? 2 : 1);
292 bool wxTextDataObject::GetDataHere(const wxDataFormat
& format
, void *buf
) const
297 wxCharBuffer buffer
= GetConv(format
).cWX2MB( GetText().c_str() );
299 size_t len
= GetConv(format
).WC2MB( NULL
, GetText().c_str(), 0 );
300 len
+= (format
== wxDF_UNICODETEXT
? 2 : 1);
302 // trailing (uni)char 0
303 memcpy( (char*)buf
, (const char*)buffer
, len
);
308 bool wxTextDataObject::SetData(const wxDataFormat
& format
,
309 size_t WXUNUSED(len
), const void *buf
)
314 wxWCharBuffer buffer
= GetConv(format
).cMB2WX( (const char*)buf
);
323 size_t wxTextDataObject::GetDataSize() const
325 return GetTextLength() * sizeof(wxChar
);
328 bool wxTextDataObject::GetDataHere(void *buf
) const
330 wxStrcpy( (wxChar
*)buf
, GetText().c_str() );
335 bool wxTextDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
337 SetText( wxString((const wxChar
*)buf
) );
344 // ----------------------------------------------------------------------------
345 // wxFileDataObjectBase
346 // ----------------------------------------------------------------------------
348 // VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should
349 // be moved to gtk/dataobj.cpp
352 wxString
wxFileDataObjectBase::GetFilenames() const
355 size_t count
= m_filenames
.GetCount();
356 for ( size_t n
= 0; n
< count
; n
++ )
358 str
<< m_filenames
[n
] << wxT('\0');
364 void wxFileDataObjectBase::SetFilenames(const wxChar
* filenames
)
369 for ( const wxChar
*pc
= filenames
; ; pc
++ )
379 // 2 consecutive NULs - this is the end of the string
383 m_filenames
.Add(current
);
391 // ----------------------------------------------------------------------------
392 // wxCustomDataObject
393 // ----------------------------------------------------------------------------
395 wxCustomDataObject::wxCustomDataObject(const wxDataFormat
& format
)
396 : wxDataObjectSimple(format
)
402 wxCustomDataObject::~wxCustomDataObject()
407 void wxCustomDataObject::TakeData(size_t size
, void *data
)
415 void *wxCustomDataObject::Alloc(size_t size
)
417 return (void *)new char[size
];
420 void wxCustomDataObject::Free()
422 delete [] (char*)m_data
;
424 m_data
= (void*)NULL
;
427 size_t wxCustomDataObject::GetDataSize() const
432 bool wxCustomDataObject::GetDataHere(void *buf
) const
437 void *data
= GetData();
441 memcpy( buf
, data
, GetSize() );
446 bool wxCustomDataObject::SetData(size_t size
, const void *buf
)
450 m_data
= Alloc(size
);
451 if ( m_data
== NULL
)
455 memcpy( m_data
, buf
, m_size
);
460 // ============================================================================
461 // some common dnd related code
462 // ============================================================================
464 #if wxUSE_DRAG_AND_DROP
468 // ----------------------------------------------------------------------------
470 // ----------------------------------------------------------------------------
472 // NB: we can't use "new" in ctor initializer lists because this provokes an
473 // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!),
474 // so use SetDataObject() instead
476 wxTextDropTarget::wxTextDropTarget()
478 SetDataObject(new wxTextDataObject
);
481 wxDragResult
wxTextDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
486 wxTextDataObject
*dobj
= (wxTextDataObject
*)m_dataObject
;
487 return OnDropText( x
, y
, dobj
->GetText() ) ? def
: wxDragNone
;
490 // ----------------------------------------------------------------------------
492 // ----------------------------------------------------------------------------
494 wxFileDropTarget::wxFileDropTarget()
496 SetDataObject(new wxFileDataObject
);
499 wxDragResult
wxFileDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
504 wxFileDataObject
*dobj
= (wxFileDataObject
*)m_dataObject
;
505 return OnDropFiles( x
, y
, dobj
->GetFilenames() ) ? def
: wxDragNone
;
508 #endif // wxUSE_DRAG_AND_DROP
510 #endif // wxUSE_DATAOBJ