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 // ----------------------------------------------------------------------------
98 wxDataObjectComposite::GetObject(const wxDataFormat
& format
) const
100 wxSimpleDataObjectList::Node
*node
= m_dataObjects
.GetFirst();
103 wxDataObjectSimple
*dataObj
= node
->GetData();
105 if ( dataObj
->GetFormat() == format
)
110 node
= node
->GetNext();
113 return (wxDataObjectSimple
*)NULL
;
116 void wxDataObjectComposite::Add(wxDataObjectSimple
*dataObject
, bool preferred
)
119 m_preferred
= m_dataObjects
.GetCount();
121 m_dataObjects
.Append( dataObject
);
125 wxDataObjectComposite::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
127 wxSimpleDataObjectList::Node
*node
= m_dataObjects
.Item( m_preferred
);
129 wxCHECK_MSG( node
, wxFormatInvalid
, wxT("no preferred format") );
131 wxDataObjectSimple
* dataObj
= node
->GetData();
133 return dataObj
->GetFormat();
136 #if defined(__WXMSW__)
138 size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat
& format
)
140 wxDataObjectSimple
*dataObj
= GetObject(format
);
142 wxCHECK_MSG( dataObj
, FALSE
,
143 wxT("unsupported format in wxDataObjectComposite"));
145 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
, FALSE
,
155 wxT("unsupported format in wxDataObjectComposite"));
157 return dataObj
->GetSizeFromBuffer( buffer
, size
, format
);
160 void* wxDataObjectComposite::SetSizeInBuffer( void* buffer
, size_t size
,
161 const wxDataFormat
& format
)
163 wxDataObjectSimple
*dataObj
= GetObject(format
);
165 wxCHECK_MSG( dataObj
, FALSE
,
166 wxT("unsupported format in wxDataObjectComposite"));
168 return dataObj
->SetSizeInBuffer( buffer
, size
, format
);
173 size_t wxDataObjectComposite::GetFormatCount(Direction
WXUNUSED(dir
)) const
175 // TODO what about the Get/Set only formats?
176 return m_dataObjects
.GetCount();
179 void wxDataObjectComposite::GetAllFormats(wxDataFormat
*formats
,
180 Direction
WXUNUSED(dir
)) const
183 wxSimpleDataObjectList::Node
*node
;
184 for ( node
= m_dataObjects
.GetFirst(); node
; node
= node
->GetNext() )
186 // TODO if ( !outputOnlyToo ) && this one counts ...
187 formats
[n
++] = node
->GetData()->GetFormat();
191 size_t wxDataObjectComposite::GetDataSize(const wxDataFormat
& format
) const
193 wxDataObjectSimple
*dataObj
= GetObject(format
);
195 wxCHECK_MSG( dataObj
, 0,
196 wxT("unsupported format in wxDataObjectComposite"));
198 return dataObj
->GetDataSize();
201 bool wxDataObjectComposite::GetDataHere(const wxDataFormat
& format
,
204 wxDataObjectSimple
*dataObj
= GetObject(format
);
206 wxCHECK_MSG( dataObj
, FALSE
,
207 wxT("unsupported format in wxDataObjectComposite"));
209 return dataObj
->GetDataHere(buf
);
212 bool wxDataObjectComposite::SetData(const wxDataFormat
& format
,
216 wxDataObjectSimple
*dataObj
= GetObject(format
);
218 wxCHECK_MSG( dataObj
, FALSE
,
219 wxT("unsupported format in wxDataObjectComposite"));
221 return dataObj
->SetData(len
, buf
);
224 // ----------------------------------------------------------------------------
226 // ----------------------------------------------------------------------------
228 size_t wxTextDataObject::GetDataSize() const
230 return GetTextLength() * sizeof(wxChar
);
233 bool wxTextDataObject::GetDataHere(void *buf
) const
235 wxStrcpy((wxChar
*)buf
, GetText().c_str());
240 bool wxTextDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
242 SetText(wxString((const wxChar
*)buf
));
247 // ----------------------------------------------------------------------------
248 // wxFileDataObjectBase
249 // ----------------------------------------------------------------------------
251 // VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should
252 // be moved to gtk/dataobj.cpp
255 wxString
wxFileDataObjectBase::GetFilenames() const
258 size_t count
= m_filenames
.GetCount();
259 for ( size_t n
= 0; n
< count
; n
++ )
261 str
<< m_filenames
[n
] << wxT('\0');
267 void wxFileDataObjectBase::SetFilenames(const wxChar
* filenames
)
272 for ( const wxChar
*pc
= filenames
; ; pc
++ )
282 // 2 consecutive NULs - this is the end of the string
286 m_filenames
.Add(current
);
294 // ----------------------------------------------------------------------------
295 // wxCustomDataObject
296 // ----------------------------------------------------------------------------
298 wxCustomDataObject::wxCustomDataObject(const wxDataFormat
& format
)
299 : wxDataObjectSimple(format
)
301 m_data
= (void *)NULL
;
304 wxCustomDataObject::~wxCustomDataObject()
309 void wxCustomDataObject::TakeData(size_t size
, void *data
)
317 void *wxCustomDataObject::Alloc(size_t size
)
319 return (void *)new char[size
];
322 void wxCustomDataObject::Free()
324 delete [] (char *)m_data
;
326 m_data
= (void *)NULL
;
329 size_t wxCustomDataObject::GetDataSize() const
334 bool wxCustomDataObject::GetDataHere(void *buf
) const
336 void *data
= GetData();
340 memcpy(buf
, data
, GetSize());
345 bool wxCustomDataObject::SetData(size_t size
, const void *buf
)
349 m_data
= Alloc(size
);
353 memcpy(m_data
, buf
, m_size
= size
);
358 // ============================================================================
359 // some common dnd related code
360 // ============================================================================
362 #if wxUSE_DRAG_AND_DROP
366 // ----------------------------------------------------------------------------
368 // ----------------------------------------------------------------------------
370 // NB: we can't use "new" in ctor initializer lists because this provokes an
371 // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!),
372 // so use SetDataObject() instead
374 wxTextDropTarget::wxTextDropTarget()
376 SetDataObject(new wxTextDataObject
);
379 wxDragResult
wxTextDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
384 wxTextDataObject
*dobj
= (wxTextDataObject
*)m_dataObject
;
385 return OnDropText(x
, y
, dobj
->GetText()) ? def
: wxDragNone
;
388 // ----------------------------------------------------------------------------
390 // ----------------------------------------------------------------------------
392 wxFileDropTarget::wxFileDropTarget()
394 SetDataObject(new wxFileDataObject
);
397 wxDragResult
wxFileDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
402 wxFileDataObject
*dobj
= (wxFileDataObject
*)m_dataObject
;
403 return OnDropFiles(x
, y
, dobj
->GetFilenames()) ? def
: wxDragNone
;
406 #endif // wxUSE_DRAG_AND_DROP
408 #endif // wxUSE_DATAOBJ