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
);
101 wxCHECK_MSG( node
, wxDataFormat(wxDF_INVALID
), wxT("no preferred format") );
103 wxCHECK_MSG( node
, wxDataFormat((unsigned short) wxDF_INVALID
), wxT("no preferred format") );
106 wxDataObjectSimple
* dataObj
= node
->GetData();
108 return dataObj
->GetFormat();
111 size_t wxDataObjectComposite
::GetFormatCount(Direction
WXUNUSED(dir
)) const
113 // TODO what about the Get/Set only formats?
114 return m_dataObjects
.GetCount();
117 void wxDataObjectComposite
::GetAllFormats(wxDataFormat
*formats
,
118 Direction
WXUNUSED(dir
)) const
121 wxSimpleDataObjectList
::Node
*node
;
122 for ( node
= m_dataObjects
.GetFirst(); node
; node
= node
->GetNext() )
124 // TODO if ( !outputOnlyToo ) && this one counts ...
125 formats
[n
++] = node
->GetData()->GetFormat();
129 size_t wxDataObjectComposite
::GetDataSize(const wxDataFormat
& format
) const
131 wxDataObjectSimple
*dataObj
= GetObject(format
);
133 wxCHECK_MSG( dataObj
, 0,
134 wxT("unsupported format in wxDataObjectComposite"));
136 return dataObj
->GetDataSize();
139 bool wxDataObjectComposite
::GetDataHere(const wxDataFormat
& format
,
142 wxDataObjectSimple
*dataObj
= GetObject(format
);
144 wxCHECK_MSG( dataObj
, FALSE
,
145 wxT("unsupported format in wxDataObjectComposite"));
147 return dataObj
->GetDataHere(buf
);
150 bool wxDataObjectComposite
::SetData(const wxDataFormat
& format
,
154 wxDataObjectSimple
*dataObj
= GetObject(format
);
156 wxCHECK_MSG( dataObj
, FALSE
,
157 wxT("unsupported format in wxDataObjectComposite"));
159 return dataObj
->SetData(len
, buf
);
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
166 size_t wxTextDataObject
::GetDataSize() const
168 return GetTextLength();
171 bool wxTextDataObject
::GetDataHere(void *buf
) const
173 strcpy((char *)buf
, GetText().mb_str());
178 bool wxTextDataObject
::SetData(size_t WXUNUSED(len
), const void *buf
)
180 SetText(wxString((const char *)buf
));
185 // ----------------------------------------------------------------------------
186 // wxFileDataObjectBase
187 // ----------------------------------------------------------------------------
189 // VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should
190 // be moved to gtk/dataobj.cpp
193 wxString wxFileDataObjectBase
::GetFilenames() const
196 size_t count
= m_filenames
.GetCount();
197 for ( size_t n
= 0; n
< count
; n
++ )
199 str
<< m_filenames
[n
] << wxT('\0');
205 void wxFileDataObjectBase
::SetFilenames(const wxChar
* filenames
)
210 for ( const wxChar
*pc
= filenames
; ; pc
++ )
220 // 2 consecutive NULs - this is the end of the string
224 m_filenames
.Add(current
);
232 // ----------------------------------------------------------------------------
233 // wxCustomDataObject
234 // ----------------------------------------------------------------------------
236 wxCustomDataObject
::~wxCustomDataObject()
241 void wxCustomDataObject
::TakeData(size_t size
, void *data
)
249 void *wxCustomDataObject
::Alloc(size_t size
)
251 return (void *)new char[size
];
254 void wxCustomDataObject
::Free()
258 m_data
= (void *)NULL
;
261 size_t wxCustomDataObject
::GetDataSize() const
266 bool wxCustomDataObject
::GetDataHere(void *buf
) const
268 void *data
= GetData();
272 memcpy(buf
, data
, GetSize());
277 bool wxCustomDataObject
::SetData(size_t size
, const void *buf
)
281 m_data
= Alloc(size
);
285 memcpy(m_data
, buf
, m_size
= size
);