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 license
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
);
155 const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer
,
157 const wxDataFormat
& format
)
159 wxDataObjectSimple
*dataObj
= GetObject(format
);
161 wxCHECK_MSG( dataObj
, NULL
,
162 wxT("unsupported format in wxDataObjectComposite"));
164 return dataObj
->GetSizeFromBuffer( buffer
, size
, format
);
167 void* wxDataObjectComposite::SetSizeInBuffer( void* buffer
, size_t size
,
168 const wxDataFormat
& format
)
170 wxDataObjectSimple
*dataObj
= GetObject(format
);
172 wxCHECK_MSG( dataObj
, NULL
,
173 wxT("unsupported format in wxDataObjectComposite"));
175 return dataObj
->SetSizeInBuffer( buffer
, size
, format
);
180 size_t wxDataObjectComposite::GetFormatCount(Direction
WXUNUSED(dir
)) const
182 // TODO what about the Get/Set only formats?
183 return m_dataObjects
.GetCount();
186 void wxDataObjectComposite::GetAllFormats(wxDataFormat
*formats
,
187 Direction
WXUNUSED(dir
)) const
190 wxSimpleDataObjectList::Node
*node
;
191 for ( node
= m_dataObjects
.GetFirst(); node
; node
= node
->GetNext() )
193 // TODO if ( !outputOnlyToo ) && this one counts ...
194 formats
[n
++] = node
->GetData()->GetFormat();
198 size_t wxDataObjectComposite::GetDataSize(const wxDataFormat
& format
) const
200 wxDataObjectSimple
*dataObj
= GetObject(format
);
202 wxCHECK_MSG( dataObj
, 0,
203 wxT("unsupported format in wxDataObjectComposite"));
205 return dataObj
->GetDataSize();
208 bool wxDataObjectComposite::GetDataHere(const wxDataFormat
& format
,
211 wxDataObjectSimple
*dataObj
= GetObject(format
);
213 wxCHECK_MSG( dataObj
, FALSE
,
214 wxT("unsupported format in wxDataObjectComposite"));
216 return dataObj
->GetDataHere(buf
);
219 bool wxDataObjectComposite::SetData(const wxDataFormat
& format
,
223 wxDataObjectSimple
*dataObj
= GetObject(format
);
225 wxCHECK_MSG( dataObj
, FALSE
,
226 wxT("unsupported format in wxDataObjectComposite"));
228 return dataObj
->SetData(len
, buf
);
231 // ----------------------------------------------------------------------------
233 // ----------------------------------------------------------------------------
235 size_t wxTextDataObject::GetDataSize() const
237 #if defined(__WXGTK20__) && wxUSE_UNICODE
239 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( GetText().c_str() );
240 return strlen( (const char*) buffer
) + 1;
242 return GetTextLength() * sizeof(wxChar
);
246 bool wxTextDataObject::GetDataHere(void *buf
) const
248 #if defined(__WXGTK20__) && wxUSE_UNICODE
250 wxCharBuffer buffer
= wxConvUTF8
.cWX2MB( GetText().c_str() );
251 strcpy( (char*) buf
, (const char*) buffer
);
253 wxStrcpy((wxChar
*)buf
, GetText().c_str());
259 bool wxTextDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
261 #if defined(__WXGTK20__) && wxUSE_UNICODE
263 SetText( wxConvUTF8
.cMB2WX( (const char*) buf
) );
265 SetText(wxString((const wxChar
*)buf
));
271 // ----------------------------------------------------------------------------
272 // wxFileDataObjectBase
273 // ----------------------------------------------------------------------------
275 // VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should
276 // be moved to gtk/dataobj.cpp
279 wxString
wxFileDataObjectBase::GetFilenames() const
282 size_t count
= m_filenames
.GetCount();
283 for ( size_t n
= 0; n
< count
; n
++ )
285 str
<< m_filenames
[n
] << wxT('\0');
291 void wxFileDataObjectBase::SetFilenames(const wxChar
* filenames
)
296 for ( const wxChar
*pc
= filenames
; ; pc
++ )
306 // 2 consecutive NULs - this is the end of the string
310 m_filenames
.Add(current
);
318 // ----------------------------------------------------------------------------
319 // wxCustomDataObject
320 // ----------------------------------------------------------------------------
322 wxCustomDataObject::wxCustomDataObject(const wxDataFormat
& format
)
323 : wxDataObjectSimple(format
)
325 m_data
= (void *)NULL
;
328 wxCustomDataObject::~wxCustomDataObject()
333 void wxCustomDataObject::TakeData(size_t size
, void *data
)
341 void *wxCustomDataObject::Alloc(size_t size
)
343 return (void *)new char[size
];
346 void wxCustomDataObject::Free()
348 delete [] (char *)m_data
;
350 m_data
= (void *)NULL
;
353 size_t wxCustomDataObject::GetDataSize() const
358 bool wxCustomDataObject::GetDataHere(void *buf
) const
360 void *data
= GetData();
364 memcpy(buf
, data
, GetSize());
369 bool wxCustomDataObject::SetData(size_t size
, const void *buf
)
373 m_data
= Alloc(size
);
377 memcpy(m_data
, buf
, m_size
= size
);
382 // ============================================================================
383 // some common dnd related code
384 // ============================================================================
386 #if wxUSE_DRAG_AND_DROP
390 // ----------------------------------------------------------------------------
392 // ----------------------------------------------------------------------------
394 // NB: we can't use "new" in ctor initializer lists because this provokes an
395 // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!),
396 // so use SetDataObject() instead
398 wxTextDropTarget::wxTextDropTarget()
400 SetDataObject(new wxTextDataObject
);
403 wxDragResult
wxTextDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
408 wxTextDataObject
*dobj
= (wxTextDataObject
*)m_dataObject
;
409 return OnDropText(x
, y
, dobj
->GetText()) ? def
: wxDragNone
;
412 // ----------------------------------------------------------------------------
414 // ----------------------------------------------------------------------------
416 wxFileDropTarget::wxFileDropTarget()
418 SetDataObject(new wxFileDataObject
);
421 wxDragResult
wxFileDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
426 wxFileDataObject
*dobj
= (wxFileDataObject
*)m_dataObject
;
427 return OnDropFiles(x
, y
, dobj
->GetFilenames()) ? def
: wxDragNone
;
430 #endif // wxUSE_DRAG_AND_DROP
432 #endif // wxUSE_DATAOBJ