1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: common/dobjcmn.cpp
3 // Purpose: implementation of data object methods common to all platforms
4 // Author: Vadim Zeitlin, Robert Roebling
8 // Copyright: (c) wxWindows Team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "dataobjbase.h"
24 #include "wx/wxprec.h"
37 #include "wx/dataobj.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 #include "wx/listimpl.cpp"
45 WX_DEFINE_LIST(wxSimpleDataObjectList
);
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 static wxDataFormat dataFormatInvalid
;
52 WXDLLEXPORT
const wxDataFormat
& wxFormatInvalid
= dataFormatInvalid
;
54 // ============================================================================
56 // ============================================================================
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 wxDataObjectBase::~wxDataObjectBase()
66 bool wxDataObjectBase::IsSupported(const wxDataFormat
& format
,
69 size_t nFormatCount
= GetFormatCount(dir
);
70 if ( nFormatCount
== 1 )
72 return format
== GetPreferredFormat(dir
);
76 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
77 GetAllFormats(formats
, dir
);
80 for ( n
= 0; n
< nFormatCount
; n
++ )
82 if ( formats
[n
] == format
)
89 return n
< nFormatCount
;
93 // ----------------------------------------------------------------------------
94 // wxDataObjectComposite
95 // ----------------------------------------------------------------------------
97 wxDataObjectComposite::wxDataObjectComposite()
101 m_dataObjects
.DeleteContents(TRUE
);
105 wxDataObjectComposite::GetObject(const wxDataFormat
& format
) const
107 wxSimpleDataObjectList::Node
*node
= m_dataObjects
.GetFirst();
110 wxDataObjectSimple
*dataObj
= node
->GetData();
112 if ( dataObj
->GetFormat() == format
)
117 node
= node
->GetNext();
120 return (wxDataObjectSimple
*)NULL
;
123 void wxDataObjectComposite::Add(wxDataObjectSimple
*dataObject
, bool preferred
)
126 m_preferred
= m_dataObjects
.GetCount();
128 m_dataObjects
.Append( dataObject
);
132 wxDataObjectComposite::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
134 wxSimpleDataObjectList::Node
*node
= m_dataObjects
.Item( m_preferred
);
136 wxCHECK_MSG( node
, wxFormatInvalid
, wxT("no preferred format") );
138 wxDataObjectSimple
* dataObj
= node
->GetData();
140 return dataObj
->GetFormat();
143 #if defined(__WXMSW__)
144 #ifdef __DIGITALMARS__
147 size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat
& format
)
149 wxDataObjectSimple
*dataObj
= GetObject(format
);
151 wxCHECK_MSG( dataObj
, 0,
152 wxT("unsupported format in wxDataObjectComposite"));
154 return dataObj
->GetBufferOffset( format
);
157 #ifdef __DIGITALMARS__
160 const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer
,
162 const wxDataFormat
& format
)
164 wxDataObjectSimple
*dataObj
= GetObject(format
);
166 wxCHECK_MSG( dataObj
, NULL
,
167 wxT("unsupported format in wxDataObjectComposite"));
169 return dataObj
->GetSizeFromBuffer( buffer
, size
, format
);
172 #ifdef __DIGITALMARS__
175 void* wxDataObjectComposite::SetSizeInBuffer( void* buffer
, size_t size
,
176 const wxDataFormat
& format
)
178 wxDataObjectSimple
*dataObj
= GetObject(format
);
180 wxCHECK_MSG( dataObj
, NULL
,
181 wxT("unsupported format in wxDataObjectComposite"));
183 return dataObj
->SetSizeInBuffer( buffer
, size
, format
);
188 size_t wxDataObjectComposite::GetFormatCount(Direction
WXUNUSED(dir
)) const
190 // TODO what about the Get/Set only formats?
191 return m_dataObjects
.GetCount();
194 void wxDataObjectComposite::GetAllFormats(wxDataFormat
*formats
,
195 Direction
WXUNUSED(dir
)) const
198 wxSimpleDataObjectList::Node
*node
;
199 for ( node
= m_dataObjects
.GetFirst(); node
; node
= node
->GetNext() )
201 // TODO if ( !outputOnlyToo ) && this one counts ...
202 formats
[n
++] = node
->GetData()->GetFormat();
206 size_t wxDataObjectComposite::GetDataSize(const wxDataFormat
& format
) const
208 wxDataObjectSimple
*dataObj
= GetObject(format
);
210 wxCHECK_MSG( dataObj
, 0,
211 wxT("unsupported format in wxDataObjectComposite"));
213 return dataObj
->GetDataSize();
216 bool wxDataObjectComposite::GetDataHere(const wxDataFormat
& format
,
219 wxDataObjectSimple
*dataObj
= GetObject(format
);
221 wxCHECK_MSG( dataObj
, FALSE
,
222 wxT("unsupported format in wxDataObjectComposite"));
224 return dataObj
->GetDataHere(buf
);
227 bool wxDataObjectComposite::SetData(const wxDataFormat
& format
,
231 wxDataObjectSimple
*dataObj
= GetObject(format
);
233 wxCHECK_MSG( dataObj
, FALSE
,
234 wxT("unsupported format in wxDataObjectComposite"));
236 return dataObj
->SetData(len
, buf
);
239 // ----------------------------------------------------------------------------
241 // ----------------------------------------------------------------------------
243 size_t wxTextDataObject::GetDataSize() const
245 #if defined(__WXGTK20__) && wxUSE_UNICODE
247 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( GetText().c_str() );
248 return strlen( (const char*) buffer
) + 1;
250 return GetTextLength() * sizeof(wxChar
);
254 bool wxTextDataObject::GetDataHere(void *buf
) const
256 #if defined(__WXGTK20__) && wxUSE_UNICODE
258 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( GetText().c_str() );
259 strcpy( (char*) buf
, (const char*) buffer
);
261 wxStrcpy((wxChar
*)buf
, GetText().c_str());
267 bool wxTextDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
269 #if defined(__WXGTK20__) && wxUSE_UNICODE
271 SetText( wxConvUTF8
.cMB2WX( (const char*) buf
) );
273 SetText(wxString((const wxChar
*)buf
));
279 // ----------------------------------------------------------------------------
280 // wxFileDataObjectBase
281 // ----------------------------------------------------------------------------
283 // VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should
284 // be moved to gtk/dataobj.cpp
287 wxString
wxFileDataObjectBase::GetFilenames() const
290 size_t count
= m_filenames
.GetCount();
291 for ( size_t n
= 0; n
< count
; n
++ )
293 str
<< m_filenames
[n
] << wxT('\0');
299 void wxFileDataObjectBase::SetFilenames(const wxChar
* filenames
)
304 for ( const wxChar
*pc
= filenames
; ; pc
++ )
314 // 2 consecutive NULs - this is the end of the string
318 m_filenames
.Add(current
);
326 // ----------------------------------------------------------------------------
327 // wxCustomDataObject
328 // ----------------------------------------------------------------------------
330 wxCustomDataObject::wxCustomDataObject(const wxDataFormat
& format
)
331 : wxDataObjectSimple(format
)
333 m_data
= (void *)NULL
;
336 wxCustomDataObject::~wxCustomDataObject()
341 void wxCustomDataObject::TakeData(size_t size
, void *data
)
349 void *wxCustomDataObject::Alloc(size_t size
)
351 return (void *)new char[size
];
354 void wxCustomDataObject::Free()
356 delete [] (char *)m_data
;
358 m_data
= (void *)NULL
;
361 size_t wxCustomDataObject::GetDataSize() const
366 bool wxCustomDataObject::GetDataHere(void *buf
) const
368 void *data
= GetData();
372 memcpy(buf
, data
, GetSize());
377 bool wxCustomDataObject::SetData(size_t size
, const void *buf
)
381 m_data
= Alloc(size
);
385 memcpy(m_data
, buf
, m_size
= size
);
390 // ============================================================================
391 // some common dnd related code
392 // ============================================================================
394 #if wxUSE_DRAG_AND_DROP
398 // ----------------------------------------------------------------------------
400 // ----------------------------------------------------------------------------
402 // NB: we can't use "new" in ctor initializer lists because this provokes an
403 // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!),
404 // so use SetDataObject() instead
406 wxTextDropTarget::wxTextDropTarget()
408 SetDataObject(new wxTextDataObject
);
411 wxDragResult
wxTextDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
416 wxTextDataObject
*dobj
= (wxTextDataObject
*)m_dataObject
;
417 return OnDropText(x
, y
, dobj
->GetText()) ? def
: wxDragNone
;
420 // ----------------------------------------------------------------------------
422 // ----------------------------------------------------------------------------
424 wxFileDropTarget::wxFileDropTarget()
426 SetDataObject(new wxFileDataObject
);
429 wxDragResult
wxFileDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
434 wxFileDataObject
*dobj
= (wxFileDataObject
*)m_dataObject
;
435 return OnDropFiles(x
, y
, dobj
->GetFilenames()) ? def
: wxDragNone
;
438 #endif // wxUSE_DRAG_AND_DROP
440 #endif // wxUSE_DATAOBJ