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__)
145 size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat
& format
)
147 wxDataObjectSimple
*dataObj
= GetObject(format
);
149 wxCHECK_MSG( dataObj
, 0,
150 wxT("unsupported format in wxDataObjectComposite"));
152 return dataObj
->GetBufferOffset( format
);
156 const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer
,
158 const wxDataFormat
& format
)
160 wxDataObjectSimple
*dataObj
= GetObject(format
);
162 wxCHECK_MSG( dataObj
, NULL
,
163 wxT("unsupported format in wxDataObjectComposite"));
165 return dataObj
->GetSizeFromBuffer( buffer
, size
, format
);
169 void* wxDataObjectComposite::SetSizeInBuffer( void* buffer
, size_t size
,
170 const wxDataFormat
& format
)
172 wxDataObjectSimple
*dataObj
= GetObject(format
);
174 wxCHECK_MSG( dataObj
, NULL
,
175 wxT("unsupported format in wxDataObjectComposite"));
177 return dataObj
->SetSizeInBuffer( buffer
, size
, format
);
182 size_t wxDataObjectComposite::GetFormatCount(Direction
WXUNUSED(dir
)) const
184 // TODO what about the Get/Set only formats?
185 return m_dataObjects
.GetCount();
188 void wxDataObjectComposite::GetAllFormats(wxDataFormat
*formats
,
189 Direction
WXUNUSED(dir
)) const
192 wxSimpleDataObjectList::Node
*node
;
193 for ( node
= m_dataObjects
.GetFirst(); node
; node
= node
->GetNext() )
195 // TODO if ( !outputOnlyToo ) && this one counts ...
196 formats
[n
++] = node
->GetData()->GetFormat();
200 size_t wxDataObjectComposite::GetDataSize(const wxDataFormat
& format
) const
202 wxDataObjectSimple
*dataObj
= GetObject(format
);
204 wxCHECK_MSG( dataObj
, 0,
205 wxT("unsupported format in wxDataObjectComposite"));
207 return dataObj
->GetDataSize();
210 bool wxDataObjectComposite::GetDataHere(const wxDataFormat
& format
,
213 wxDataObjectSimple
*dataObj
= GetObject(format
);
215 wxCHECK_MSG( dataObj
, FALSE
,
216 wxT("unsupported format in wxDataObjectComposite"));
218 return dataObj
->GetDataHere(buf
);
221 bool wxDataObjectComposite::SetData(const wxDataFormat
& format
,
225 wxDataObjectSimple
*dataObj
= GetObject(format
);
227 wxCHECK_MSG( dataObj
, FALSE
,
228 wxT("unsupported format in wxDataObjectComposite"));
230 return dataObj
->SetData(len
, buf
);
233 // ----------------------------------------------------------------------------
235 // ----------------------------------------------------------------------------
237 size_t wxTextDataObject::GetDataSize() const
239 #if defined(__WXGTK20__) && wxUSE_UNICODE
241 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( GetText().c_str() );
242 return strlen( (const char*) buffer
) + 1;
244 return GetTextLength() * sizeof(wxChar
);
248 bool wxTextDataObject::GetDataHere(void *buf
) const
250 #if defined(__WXGTK20__) && wxUSE_UNICODE
252 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( GetText().c_str() );
253 strcpy( (char*) buf
, (const char*) buffer
);
255 wxStrcpy((wxChar
*)buf
, GetText().c_str());
261 bool wxTextDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
263 #if defined(__WXGTK20__) && wxUSE_UNICODE
265 SetText( wxConvUTF8
.cMB2WX( (const char*) buf
) );
267 SetText(wxString((const wxChar
*)buf
));
273 // ----------------------------------------------------------------------------
274 // wxFileDataObjectBase
275 // ----------------------------------------------------------------------------
277 // VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should
278 // be moved to gtk/dataobj.cpp
281 wxString
wxFileDataObjectBase::GetFilenames() const
284 size_t count
= m_filenames
.GetCount();
285 for ( size_t n
= 0; n
< count
; n
++ )
287 str
<< m_filenames
[n
] << wxT('\0');
293 void wxFileDataObjectBase::SetFilenames(const wxChar
* filenames
)
298 for ( const wxChar
*pc
= filenames
; ; pc
++ )
308 // 2 consecutive NULs - this is the end of the string
312 m_filenames
.Add(current
);
320 // ----------------------------------------------------------------------------
321 // wxCustomDataObject
322 // ----------------------------------------------------------------------------
324 wxCustomDataObject::wxCustomDataObject(const wxDataFormat
& format
)
325 : wxDataObjectSimple(format
)
327 m_data
= (void *)NULL
;
330 wxCustomDataObject::~wxCustomDataObject()
335 void wxCustomDataObject::TakeData(size_t size
, void *data
)
343 void *wxCustomDataObject::Alloc(size_t size
)
345 return (void *)new char[size
];
348 void wxCustomDataObject::Free()
350 delete [] (char *)m_data
;
352 m_data
= (void *)NULL
;
355 size_t wxCustomDataObject::GetDataSize() const
360 bool wxCustomDataObject::GetDataHere(void *buf
) const
362 void *data
= GetData();
366 memcpy(buf
, data
, GetSize());
371 bool wxCustomDataObject::SetData(size_t size
, const void *buf
)
375 m_data
= Alloc(size
);
379 memcpy(m_data
, buf
, m_size
= size
);
384 // ============================================================================
385 // some common dnd related code
386 // ============================================================================
388 #if wxUSE_DRAG_AND_DROP
392 // ----------------------------------------------------------------------------
394 // ----------------------------------------------------------------------------
396 // NB: we can't use "new" in ctor initializer lists because this provokes an
397 // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!),
398 // so use SetDataObject() instead
400 wxTextDropTarget::wxTextDropTarget()
402 SetDataObject(new wxTextDataObject
);
405 wxDragResult
wxTextDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
410 wxTextDataObject
*dobj
= (wxTextDataObject
*)m_dataObject
;
411 return OnDropText(x
, y
, dobj
->GetText()) ? def
: wxDragNone
;
414 // ----------------------------------------------------------------------------
416 // ----------------------------------------------------------------------------
418 wxFileDropTarget::wxFileDropTarget()
420 SetDataObject(new wxFileDataObject
);
423 wxDragResult
wxFileDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
428 wxFileDataObject
*dobj
= (wxFileDataObject
*)m_dataObject
;
429 return OnDropFiles(x
, y
, dobj
->GetFilenames()) ? def
: wxDragNone
;
432 #endif // wxUSE_DRAG_AND_DROP
434 #endif // wxUSE_DATAOBJ