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 size_t wxDataObjectComposite::GetFormatCount(Direction
WXUNUSED(dir
)) const
138 // TODO what about the Get/Set only formats?
139 return m_dataObjects
.GetCount();
142 void wxDataObjectComposite::GetAllFormats(wxDataFormat
*formats
,
143 Direction
WXUNUSED(dir
)) const
146 wxSimpleDataObjectList::Node
*node
;
147 for ( node
= m_dataObjects
.GetFirst(); node
; node
= node
->GetNext() )
149 // TODO if ( !outputOnlyToo ) && this one counts ...
150 formats
[n
++] = node
->GetData()->GetFormat();
154 size_t wxDataObjectComposite::GetDataSize(const wxDataFormat
& format
) const
156 wxDataObjectSimple
*dataObj
= GetObject(format
);
158 wxCHECK_MSG( dataObj
, 0,
159 wxT("unsupported format in wxDataObjectComposite"));
161 return dataObj
->GetDataSize();
164 bool wxDataObjectComposite::GetDataHere(const wxDataFormat
& format
,
167 wxDataObjectSimple
*dataObj
= GetObject(format
);
169 wxCHECK_MSG( dataObj
, FALSE
,
170 wxT("unsupported format in wxDataObjectComposite"));
172 return dataObj
->GetDataHere(buf
);
175 bool wxDataObjectComposite::SetData(const wxDataFormat
& format
,
179 wxDataObjectSimple
*dataObj
= GetObject(format
);
181 wxCHECK_MSG( dataObj
, FALSE
,
182 wxT("unsupported format in wxDataObjectComposite"));
184 return dataObj
->SetData(len
, buf
);
187 // ----------------------------------------------------------------------------
189 // ----------------------------------------------------------------------------
191 size_t wxTextDataObject::GetDataSize() const
193 return GetTextLength();
196 bool wxTextDataObject::GetDataHere(void *buf
) const
198 strcpy((char *)buf
, GetText().mb_str());
203 bool wxTextDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
205 SetText(wxString((const char *)buf
));
210 // ----------------------------------------------------------------------------
211 // wxFileDataObjectBase
212 // ----------------------------------------------------------------------------
214 // VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should
215 // be moved to gtk/dataobj.cpp
218 wxString
wxFileDataObjectBase::GetFilenames() const
221 size_t count
= m_filenames
.GetCount();
222 for ( size_t n
= 0; n
< count
; n
++ )
224 str
<< m_filenames
[n
] << wxT('\0');
230 void wxFileDataObjectBase::SetFilenames(const wxChar
* filenames
)
235 for ( const wxChar
*pc
= filenames
; ; pc
++ )
245 // 2 consecutive NULs - this is the end of the string
249 m_filenames
.Add(current
);
257 // ----------------------------------------------------------------------------
258 // wxCustomDataObject
259 // ----------------------------------------------------------------------------
261 wxCustomDataObject::wxCustomDataObject(const wxDataFormat
& format
)
262 : wxDataObjectSimple(format
)
264 m_data
= (void *)NULL
;
267 wxCustomDataObject::~wxCustomDataObject()
272 void wxCustomDataObject::TakeData(size_t size
, void *data
)
280 void *wxCustomDataObject::Alloc(size_t size
)
282 return (void *)new char[size
];
285 void wxCustomDataObject::Free()
287 delete [] (char *)m_data
;
289 m_data
= (void *)NULL
;
292 size_t wxCustomDataObject::GetDataSize() const
297 bool wxCustomDataObject::GetDataHere(void *buf
) const
299 void *data
= GetData();
303 memcpy(buf
, data
, GetSize());
308 bool wxCustomDataObject::SetData(size_t size
, const void *buf
)
312 m_data
= Alloc(size
);
316 memcpy(m_data
, buf
, m_size
= size
);
321 // ============================================================================
322 // some common dnd related code
323 // ============================================================================
325 #if wxUSE_DRAG_AND_DROP
329 // ----------------------------------------------------------------------------
331 // ----------------------------------------------------------------------------
333 // NB: we can't use "new" in ctor initializer lists because this provokes an
334 // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!),
335 // so use SetDataObject() instead
337 wxTextDropTarget::wxTextDropTarget()
339 SetDataObject(new wxTextDataObject
);
342 wxDragResult
wxTextDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
347 wxTextDataObject
*dobj
= (wxTextDataObject
*)m_dataObject
;
348 return OnDropText(x
, y
, dobj
->GetText()) ? def
: wxDragNone
;
351 // ----------------------------------------------------------------------------
353 // ----------------------------------------------------------------------------
355 wxFileDropTarget::wxFileDropTarget()
357 SetDataObject(new wxFileDataObject
);
360 wxDragResult
wxFileDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
365 wxFileDataObject
*dobj
= (wxFileDataObject
*)m_dataObject
;
366 return OnDropFiles(x
, y
, dobj
->GetFilenames()) ? def
: wxDragNone
;
369 #endif // wxUSE_DRAG_AND_DROP
371 #endif // wxUSE_DATAOBJ