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 const wxDataFormat
& wxFormatInvalid
= dataFormatInvalid
;
52 // ============================================================================
54 // ============================================================================
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 wxDataObjectBase::~wxDataObjectBase()
64 // ----------------------------------------------------------------------------
65 // wxDataObjectComposite
66 // ----------------------------------------------------------------------------
69 wxDataObjectComposite::GetObject(const wxDataFormat
& format
) const
71 wxSimpleDataObjectList::Node
*node
= m_dataObjects
.GetFirst();
74 wxDataObjectSimple
*dataObj
= node
->GetData();
76 if ( dataObj
->GetFormat() == format
)
81 node
= node
->GetNext();
84 return (wxDataObjectSimple
*)NULL
;
87 void wxDataObjectComposite::Add(wxDataObjectSimple
*dataObject
, bool preferred
)
90 m_preferred
= m_dataObjects
.GetCount();
92 m_dataObjects
.Append( dataObject
);
96 wxDataObjectComposite::GetPreferredFormat(Direction
WXUNUSED(dir
)) const
98 wxSimpleDataObjectList::Node
*node
= m_dataObjects
.Item( m_preferred
);
100 wxCHECK_MSG( node
, wxFormatInvalid
, wxT("no preferred format") );
102 wxDataObjectSimple
* dataObj
= node
->GetData();
104 return dataObj
->GetFormat();
107 size_t wxDataObjectComposite::GetFormatCount(Direction
WXUNUSED(dir
)) const
109 // TODO what about the Get/Set only formats?
110 return m_dataObjects
.GetCount();
113 void wxDataObjectComposite::GetAllFormats(wxDataFormat
*formats
,
114 Direction
WXUNUSED(dir
)) const
117 wxSimpleDataObjectList::Node
*node
;
118 for ( node
= m_dataObjects
.GetFirst(); node
; node
= node
->GetNext() )
120 // TODO if ( !outputOnlyToo ) && this one counts ...
121 formats
[n
++] = node
->GetData()->GetFormat();
125 size_t wxDataObjectComposite::GetDataSize(const wxDataFormat
& format
) const
127 wxDataObjectSimple
*dataObj
= GetObject(format
);
129 wxCHECK_MSG( dataObj
, 0,
130 wxT("unsupported format in wxDataObjectComposite"));
132 return dataObj
->GetDataSize();
135 bool wxDataObjectComposite::GetDataHere(const wxDataFormat
& format
,
138 wxDataObjectSimple
*dataObj
= GetObject(format
);
140 wxCHECK_MSG( dataObj
, FALSE
,
141 wxT("unsupported format in wxDataObjectComposite"));
143 return dataObj
->GetDataHere(buf
);
146 bool wxDataObjectComposite::SetData(const wxDataFormat
& format
,
150 wxDataObjectSimple
*dataObj
= GetObject(format
);
152 wxCHECK_MSG( dataObj
, FALSE
,
153 wxT("unsupported format in wxDataObjectComposite"));
155 return dataObj
->SetData(len
, buf
);
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 size_t wxTextDataObject::GetDataSize() const
164 return GetTextLength();
167 bool wxTextDataObject::GetDataHere(void *buf
) const
169 strcpy((char *)buf
, GetText().mb_str());
174 bool wxTextDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
176 SetText(wxString((const char *)buf
));
181 // ----------------------------------------------------------------------------
182 // wxFileDataObjectBase
183 // ----------------------------------------------------------------------------
185 // VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should
186 // be moved to gtk/dataobj.cpp
189 wxString
wxFileDataObjectBase::GetFilenames() const
192 size_t count
= m_filenames
.GetCount();
193 for ( size_t n
= 0; n
< count
; n
++ )
195 str
<< m_filenames
[n
] << wxT('\0');
201 void wxFileDataObjectBase::SetFilenames(const wxChar
* filenames
)
206 for ( const wxChar
*pc
= filenames
; ; pc
++ )
216 // 2 consecutive NULs - this is the end of the string
220 m_filenames
.Add(current
);
228 // ----------------------------------------------------------------------------
229 // wxCustomDataObject
230 // ----------------------------------------------------------------------------
232 wxCustomDataObject::wxCustomDataObject(const wxDataFormat
& format
)
233 : wxDataObjectSimple(format
)
238 wxCustomDataObject::~wxCustomDataObject()
243 void wxCustomDataObject::TakeData(size_t size
, void *data
)
251 void *wxCustomDataObject::Alloc(size_t size
)
253 return (void *)new char[size
];
256 void wxCustomDataObject::Free()
260 m_data
= (void *)NULL
;
263 size_t wxCustomDataObject::GetDataSize() const
268 bool wxCustomDataObject::GetDataHere(void *buf
) const
270 void *data
= GetData();
274 memcpy(buf
, data
, GetSize());
279 bool wxCustomDataObject::SetData(size_t size
, const void *buf
)
283 m_data
= Alloc(size
);
287 memcpy(m_data
, buf
, m_size
= size
);
292 // ============================================================================
293 // some common dnd related code
294 // ============================================================================
296 #if wxUSE_DRAG_AND_DROP
300 // ----------------------------------------------------------------------------
302 // ----------------------------------------------------------------------------
304 wxTextDropTarget::wxTextDropTarget()
305 : wxDropTarget(new wxTextDataObject
)
309 wxDragResult
wxTextDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
314 wxTextDataObject
*dobj
= (wxTextDataObject
*)m_dataObject
;
315 return OnDropText(x
, y
, dobj
->GetText()) ? def
: wxDragNone
;
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
322 wxFileDropTarget::wxFileDropTarget()
323 : wxDropTarget(new wxFileDataObject
)
327 wxDragResult
wxFileDropTarget::OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
332 wxFileDataObject
*dobj
= (wxFileDataObject
*)m_dataObject
;
333 return OnDropFiles(x
, y
, dobj
->GetFilenames()) ? def
: wxDragNone
;