wxDataObject changes - ok for MSW, still not for GTK/Motif/...
[wxWidgets.git] / src / common / dobjcmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: common/dobjcmn.cpp
3 // Purpose: implementation of data object methods common to all platforms
4 // Author: Vadim Zeitlin, Robert Roebling
5 // Modified by:
6 // Created: 19.10.99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWindows Team
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "dataobjbase.h"
22 #endif
23
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif
29
30 #ifndef WX_PRECOMP
31 #include "wx/app.h"
32 #include "wx/debug.h"
33 #endif // WX_PRECOMP
34
35 #include "wx/dataobj.h"
36
37 // ----------------------------------------------------------------------------
38 // lists
39 // ----------------------------------------------------------------------------
40
41 #include "wx/listimpl.cpp"
42
43 WX_DEFINE_LIST(wxSimpleDataObjectList);
44
45 // ============================================================================
46 // implementation
47 // ============================================================================
48
49 // ----------------------------------------------------------------------------
50 // wxDataObjectBase
51 // ----------------------------------------------------------------------------
52
53 wxDataObjectBase::~wxDataObjectBase()
54 {
55 }
56
57 // ----------------------------------------------------------------------------
58 // wxDataObjectComposite
59 // ----------------------------------------------------------------------------
60
61 wxDataObjectSimple *
62 wxDataObjectComposite::GetObject(const wxDataFormat& format) const
63 {
64 wxSimpleDataObjectList::Node *node = m_dataObjects.GetFirst();
65 while ( node )
66 {
67 wxDataObjectSimple *dataObj = node->GetData();
68
69 if ( dataObj->GetFormat() == format )
70 {
71 return dataObj;
72 }
73
74 node = node->GetNext();
75 }
76
77 return (wxDataObjectSimple *)NULL;
78 }
79
80 void wxDataObjectComposite::Add(wxDataObjectSimple *dataObject, bool preferred)
81 {
82 if ( preferred )
83 m_preferred = m_dataObjects.GetCount();
84
85 m_dataObjects.Append( dataObject );
86 }
87
88 wxDataFormat
89 wxDataObjectComposite::GetPreferredFormat(Direction WXUNUSED(dir)) const
90 {
91 wxSimpleDataObjectList::Node *node = m_dataObjects.Item( m_preferred );
92
93 wxCHECK_MSG( node, wxDF_INVALID, wxT("no preferred format") );
94
95 wxDataObjectSimple* dataObj = node->GetData();
96
97 return dataObj->GetFormat();
98 }
99
100 size_t wxDataObjectComposite::GetFormatCount(Direction WXUNUSED(dir)) const
101 {
102 // TODO what about the Get/Set only formats?
103 return m_dataObjects.GetCount();
104 }
105
106 void wxDataObjectComposite::GetAllFormats(wxDataFormat *formats,
107 Direction WXUNUSED(dir)) const
108 {
109 size_t n = 0;
110 wxSimpleDataObjectList::Node *node;
111 for ( node = m_dataObjects.GetFirst(); node; node = node->GetNext() )
112 {
113 // TODO if ( !outputOnlyToo ) && this one counts ...
114 formats[n++] = node->GetData()->GetFormat();
115 }
116 }
117
118 size_t wxDataObjectComposite::GetDataSize(const wxDataFormat& format) const
119 {
120 wxDataObjectSimple *dataObj = GetObject(format);
121
122 wxCHECK_MSG( dataObj, 0,
123 wxT("unsupported format in wxDataObjectComposite"));
124
125 return dataObj->GetDataSize();
126 }
127
128 bool wxDataObjectComposite::GetDataHere(const wxDataFormat& format,
129 void *buf) const
130 {
131 wxDataObjectSimple *dataObj = GetObject(format);
132
133 wxCHECK_MSG( dataObj, FALSE,
134 wxT("unsupported format in wxDataObjectComposite"));
135
136 return dataObj->GetDataHere(buf);
137 }
138
139 bool wxDataObjectComposite::SetData(const wxDataFormat& format,
140 size_t len,
141 const void *buf)
142 {
143 wxDataObjectSimple *dataObj = GetObject(format);
144
145 wxCHECK_MSG( dataObj, FALSE,
146 wxT("unsupported format in wxDataObjectComposite"));
147
148 return dataObj->SetData(len, buf);
149 }
150
151 // ----------------------------------------------------------------------------
152 // wxTextDataObject
153 // ----------------------------------------------------------------------------
154
155 size_t wxTextDataObject::GetDataSize() const
156 {
157 return GetTextLength();
158 }
159
160 bool wxTextDataObject::GetDataHere(void *buf) const
161 {
162 strcpy((char *)buf, GetText().mb_str());
163
164 return TRUE;
165 }
166
167 bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf)
168 {
169 SetText(wxString((const char *)buf));
170
171 return TRUE;
172 }
173
174 // ----------------------------------------------------------------------------
175 // wxFileDataObjectBase
176 // ----------------------------------------------------------------------------
177
178 // VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should
179 // be moved to gtk/dataobj.cpp
180 #if 0
181
182 wxString wxFileDataObjectBase::GetFilenames() const
183 {
184 wxString str;
185 size_t count = m_filenames.GetCount();
186 for ( size_t n = 0; n < count; n++ )
187 {
188 str << m_filenames[n] << wxT('\0');
189 }
190
191 return str;
192 }
193
194 void wxFileDataObjectBase::SetFilenames(const wxChar* filenames)
195 {
196 m_filenames.Empty();
197
198 wxString current;
199 for ( const wxChar *pc = filenames; ; pc++ )
200 {
201 if ( *pc )
202 {
203 current += *pc;
204 }
205 else
206 {
207 if ( !current )
208 {
209 // 2 consecutive NULs - this is the end of the string
210 break;
211 }
212
213 m_filenames.Add(current);
214 current.Empty();
215 }
216 }
217 }
218
219 #endif // 0
220
221 // ----------------------------------------------------------------------------
222 // wxCustomDataObject
223 // ----------------------------------------------------------------------------
224
225 wxCustomDataObject::~wxCustomDataObject()
226 {
227 Free();
228 }
229
230 void wxCustomDataObject::TakeData(size_t size, void *data)
231 {
232 Free();
233
234 m_size = size;
235 m_data = data;
236 }
237
238 void *wxCustomDataObject::Alloc(size_t size)
239 {
240 return (void *)new char[size];
241 }
242
243 void wxCustomDataObject::Free()
244 {
245 delete [] m_data;
246 m_size = 0;
247 m_data = (void *)NULL;
248 }
249
250 size_t wxCustomDataObject::GetDataSize() const
251 {
252 return GetSize();
253 }
254
255 bool wxCustomDataObject::GetDataHere(void *buf) const
256 {
257 void *data = GetData();
258 if ( !data )
259 return FALSE;
260
261 memcpy(buf, data, GetSize());
262
263 return TRUE;
264 }
265
266 bool wxCustomDataObject::SetData(size_t size, const void *buf)
267 {
268 Free();
269
270 m_data = Alloc(size);
271 if ( !m_data )
272 return FALSE;
273
274 memcpy(m_data, buf, m_size = size);
275
276 return TRUE;
277 }
278