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"
35 #include "wx/dataobj.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 #include "wx/listimpl.cpp"
43 WX_DEFINE_LIST(wxSimpleDataObjectList
);
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 static wxDataFormat dataFormatInvalid
;
50 WXDLLEXPORT
const wxDataFormat
& wxFormatInvalid
= dataFormatInvalid
;
52 // ============================================================================
54 // ============================================================================
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 wxDataObjectBase::~wxDataObjectBase()
64 bool wxDataObjectBase::IsSupported(const wxDataFormat
& format
,
67 size_t nFormatCount
= GetFormatCount(dir
);
68 if ( nFormatCount
== 1 )
70 return format
== GetPreferredFormat(dir
);
74 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
75 GetAllFormats(formats
, dir
);
78 for ( n
= 0; n
< nFormatCount
; n
++ )
80 if ( formats
[n
] == format
)
87 return n
< nFormatCount
;
91 // ----------------------------------------------------------------------------
92 // wxDataObjectComposite
93 // ----------------------------------------------------------------------------
96 wxDataObjectComposite::GetObject(const wxDataFormat
& format
) const
98 wxSimpleDataObjectList::Node
*node
= m_dataObjects
.GetFirst();
101 wxDataObjectSimple
*dataObj
= node
->GetData();
103 if ( dataObj
->GetFormat() == format
)
108 node
= node
->GetNext();
111 return (wxDataObjectSimple
*)NULL
;
114 void wxDataObjectComposite::Add(wxDataObjectSimple
*dataObject
, bool preferred
)
117 m_preferred
= m_dataObjects
.GetCount();
119 m_dataObjects
.Append( dataObject
);
123 wxDataObjectComposite::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
125 wxSimpleDataObjectList::Node
*node
= m_dataObjects
.Item( m_preferred
);
127 wxCHECK_MSG( node
, wxFormatInvalid
, wxT("no preferred format") );
129 wxDataObjectSimple
* dataObj
= node
->GetData();
131 return dataObj
->GetFormat();
134 size_t wxDataObjectComposite::GetFormatCount(Direction
WXUNUSED(dir
)) const
136 // TODO what about the Get/Set only formats?
137 return m_dataObjects
.GetCount();
140 void wxDataObjectComposite::GetAllFormats(wxDataFormat
*formats
,
141 Direction
WXUNUSED(dir
)) const
144 wxSimpleDataObjectList::Node
*node
;
145 for ( node
= m_dataObjects
.GetFirst(); node
; node
= node
->GetNext() )
147 // TODO if ( !outputOnlyToo ) && this one counts ...
148 formats
[n
++] = node
->GetData()->GetFormat();
152 size_t wxDataObjectComposite::GetDataSize(const wxDataFormat
& format
) const
154 wxDataObjectSimple
*dataObj
= GetObject(format
);
156 wxCHECK_MSG( dataObj
, 0,
157 wxT("unsupported format in wxDataObjectComposite"));
159 return dataObj
->GetDataSize();
162 bool wxDataObjectComposite::GetDataHere(const wxDataFormat
& format
,
165 wxDataObjectSimple
*dataObj
= GetObject(format
);
167 wxCHECK_MSG( dataObj
, FALSE
,
168 wxT("unsupported format in wxDataObjectComposite"));
170 return dataObj
->GetDataHere(buf
);
173 bool wxDataObjectComposite::SetData(const wxDataFormat
& format
,
177 wxDataObjectSimple
*dataObj
= GetObject(format
);
179 wxCHECK_MSG( dataObj
, FALSE
,
180 wxT("unsupported format in wxDataObjectComposite"));
182 return dataObj
->SetData(len
, buf
);
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
189 size_t wxTextDataObject::GetDataSize() const
191 return GetTextLength();
194 bool wxTextDataObject::GetDataHere(void *buf
) const
196 strcpy((char *)buf
, GetText().mb_str());
201 bool wxTextDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
203 SetText(wxString((const char *)buf
));
208 // ----------------------------------------------------------------------------
209 // wxFileDataObjectBase
210 // ----------------------------------------------------------------------------
212 // VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should
213 // be moved to gtk/dataobj.cpp
216 wxString
wxFileDataObjectBase::GetFilenames() const
219 size_t count
= m_filenames
.GetCount();
220 for ( size_t n
= 0; n
< count
; n
++ )
222 str
<< m_filenames
[n
] << wxT('\0');
228 void wxFileDataObjectBase::SetFilenames(const wxChar
* filenames
)
233 for ( const wxChar
*pc
= filenames
; ; pc
++ )
243 // 2 consecutive NULs - this is the end of the string
247 m_filenames
.Add(current
);
255 // ----------------------------------------------------------------------------
256 // wxCustomDataObject
257 // ----------------------------------------------------------------------------
259 wxCustomDataObject::wxCustomDataObject(const wxDataFormat
& format
)
260 : wxDataObjectSimple(format
)
262 m_data
= (void *)NULL
;
265 wxCustomDataObject::~wxCustomDataObject()
270 void wxCustomDataObject::TakeData(size_t size
, void *data
)
278 void *wxCustomDataObject::Alloc(size_t size
)
280 return (void *)new char[size
];
283 void wxCustomDataObject::Free()
285 delete [] (char *)m_data
;
287 m_data
= (void *)NULL
;
290 size_t wxCustomDataObject::GetDataSize() const
295 bool wxCustomDataObject::GetDataHere(void *buf
) const
297 void *data
= GetData();
301 memcpy(buf
, data
, GetSize());
306 bool wxCustomDataObject::SetData(size_t size
, const void *buf
)
310 m_data
= Alloc(size
);
314 memcpy(m_data
, buf
, m_size
= size
);
319 // ============================================================================
320 // some common dnd related code
321 // ============================================================================
323 #if wxUSE_DRAG_AND_DROP
327 // ----------------------------------------------------------------------------
329 // ----------------------------------------------------------------------------
331 // NB: we can't use "new" in ctor initializer lists because this provokes an
332 // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!),
333 // so use SetDataObject() instead
335 wxTextDropTarget::wxTextDropTarget()
337 SetDataObject(new wxTextDataObject
);
340 wxDragResult
wxTextDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
345 wxTextDataObject
*dobj
= (wxTextDataObject
*)m_dataObject
;
346 return OnDropText(x
, y
, dobj
->GetText()) ? def
: wxDragNone
;
349 // ----------------------------------------------------------------------------
351 // ----------------------------------------------------------------------------
353 wxFileDropTarget::wxFileDropTarget()
355 SetDataObject(new wxFileDataObject
);
358 wxDragResult
wxFileDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
363 wxFileDataObject
*dobj
= (wxFileDataObject
*)m_dataObject
;
364 return OnDropFiles(x
, y
, dobj
->GetFilenames()) ? def
: wxDragNone
;