]>
Commit | Line | Data |
---|---|---|
3f364be8 VZ |
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 | |
55d99c7a | 9 | // Licence: wxWindows licence |
3f364be8 VZ |
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 | ||
1e6feb95 VZ |
30 | #if wxUSE_DATAOBJ |
31 | ||
3f364be8 VZ |
32 | #ifndef WX_PRECOMP |
33 | #include "wx/app.h" | |
34 | #include "wx/debug.h" | |
35 | #endif // WX_PRECOMP | |
36 | ||
37 | #include "wx/dataobj.h" | |
38 | ||
39 | // ---------------------------------------------------------------------------- | |
40 | // lists | |
41 | // ---------------------------------------------------------------------------- | |
42 | ||
43 | #include "wx/listimpl.cpp" | |
44 | ||
45 | WX_DEFINE_LIST(wxSimpleDataObjectList); | |
46 | ||
0c2b453f VZ |
47 | // ---------------------------------------------------------------------------- |
48 | // globals | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
51 | static wxDataFormat dataFormatInvalid; | |
31e78e0c | 52 | WXDLLEXPORT const wxDataFormat& wxFormatInvalid = dataFormatInvalid; |
0c2b453f | 53 | |
3f364be8 VZ |
54 | // ============================================================================ |
55 | // implementation | |
56 | // ============================================================================ | |
57 | ||
58 | // ---------------------------------------------------------------------------- | |
59 | // wxDataObjectBase | |
60 | // ---------------------------------------------------------------------------- | |
61 | ||
62 | wxDataObjectBase::~wxDataObjectBase() | |
63 | { | |
64 | } | |
65 | ||
d9317fd4 VZ |
66 | bool wxDataObjectBase::IsSupported(const wxDataFormat& format, |
67 | Direction dir) const | |
68 | { | |
69 | size_t nFormatCount = GetFormatCount(dir); | |
70 | if ( nFormatCount == 1 ) | |
71 | { | |
72 | return format == GetPreferredFormat(dir); | |
73 | } | |
74 | else | |
75 | { | |
76 | wxDataFormat *formats = new wxDataFormat[nFormatCount]; | |
77 | GetAllFormats(formats, dir); | |
78 | ||
79 | size_t n; | |
80 | for ( n = 0; n < nFormatCount; n++ ) | |
81 | { | |
82 | if ( formats[n] == format ) | |
83 | break; | |
84 | } | |
85 | ||
86 | delete [] formats; | |
87 | ||
88 | // found? | |
89 | return n < nFormatCount; | |
90 | } | |
91 | } | |
92 | ||
3f364be8 VZ |
93 | // ---------------------------------------------------------------------------- |
94 | // wxDataObjectComposite | |
95 | // ---------------------------------------------------------------------------- | |
96 | ||
e6b01b78 VZ |
97 | wxDataObjectComposite::wxDataObjectComposite() |
98 | { | |
99 | m_preferred = 0; | |
100 | ||
101 | m_dataObjects.DeleteContents(TRUE); | |
102 | } | |
103 | ||
3f364be8 VZ |
104 | wxDataObjectSimple * |
105 | wxDataObjectComposite::GetObject(const wxDataFormat& format) const | |
106 | { | |
107 | wxSimpleDataObjectList::Node *node = m_dataObjects.GetFirst(); | |
108 | while ( node ) | |
109 | { | |
110 | wxDataObjectSimple *dataObj = node->GetData(); | |
111 | ||
112 | if ( dataObj->GetFormat() == format ) | |
113 | { | |
114 | return dataObj; | |
115 | } | |
116 | ||
117 | node = node->GetNext(); | |
118 | } | |
119 | ||
120 | return (wxDataObjectSimple *)NULL; | |
121 | } | |
122 | ||
123 | void wxDataObjectComposite::Add(wxDataObjectSimple *dataObject, bool preferred) | |
124 | { | |
125 | if ( preferred ) | |
126 | m_preferred = m_dataObjects.GetCount(); | |
127 | ||
128 | m_dataObjects.Append( dataObject ); | |
129 | } | |
130 | ||
131 | wxDataFormat | |
132 | wxDataObjectComposite::GetPreferredFormat(Direction WXUNUSED(dir)) const | |
133 | { | |
134 | wxSimpleDataObjectList::Node *node = m_dataObjects.Item( m_preferred ); | |
135 | ||
2ee3ee1b | 136 | wxCHECK_MSG( node, wxFormatInvalid, wxT("no preferred format") ); |
3f364be8 VZ |
137 | |
138 | wxDataObjectSimple* dataObj = node->GetData(); | |
139 | ||
140 | return dataObj->GetFormat(); | |
141 | } | |
142 | ||
e1b435af | 143 | #if defined(__WXMSW__) |
f15dcc01 CE |
144 | #ifdef __DIGITALMARS__ |
145 | extern "C" | |
146 | #endif | |
e1b435af MB |
147 | size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat& format ) |
148 | { | |
149 | wxDataObjectSimple *dataObj = GetObject(format); | |
150 | ||
f7f50f49 | 151 | wxCHECK_MSG( dataObj, 0, |
e1b435af MB |
152 | wxT("unsupported format in wxDataObjectComposite")); |
153 | ||
154 | return dataObj->GetBufferOffset( format ); | |
155 | } | |
156 | ||
f15dcc01 CE |
157 | #ifdef __DIGITALMARS__ |
158 | extern "C" | |
159 | #endif | |
e1b435af MB |
160 | const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer, |
161 | size_t* size, | |
162 | const wxDataFormat& format ) | |
163 | { | |
164 | wxDataObjectSimple *dataObj = GetObject(format); | |
165 | ||
f7f50f49 | 166 | wxCHECK_MSG( dataObj, NULL, |
e1b435af MB |
167 | wxT("unsupported format in wxDataObjectComposite")); |
168 | ||
169 | return dataObj->GetSizeFromBuffer( buffer, size, format ); | |
170 | } | |
171 | ||
f15dcc01 CE |
172 | #ifdef __DIGITALMARS__ |
173 | extern "C" | |
174 | #endif | |
e1b435af MB |
175 | void* wxDataObjectComposite::SetSizeInBuffer( void* buffer, size_t size, |
176 | const wxDataFormat& format ) | |
177 | { | |
178 | wxDataObjectSimple *dataObj = GetObject(format); | |
179 | ||
f7f50f49 | 180 | wxCHECK_MSG( dataObj, NULL, |
e1b435af MB |
181 | wxT("unsupported format in wxDataObjectComposite")); |
182 | ||
183 | return dataObj->SetSizeInBuffer( buffer, size, format ); | |
184 | } | |
185 | ||
186 | #endif | |
187 | ||
3f364be8 VZ |
188 | size_t wxDataObjectComposite::GetFormatCount(Direction WXUNUSED(dir)) const |
189 | { | |
190 | // TODO what about the Get/Set only formats? | |
191 | return m_dataObjects.GetCount(); | |
192 | } | |
193 | ||
194 | void wxDataObjectComposite::GetAllFormats(wxDataFormat *formats, | |
195 | Direction WXUNUSED(dir)) const | |
196 | { | |
197 | size_t n = 0; | |
198 | wxSimpleDataObjectList::Node *node; | |
199 | for ( node = m_dataObjects.GetFirst(); node; node = node->GetNext() ) | |
200 | { | |
201 | // TODO if ( !outputOnlyToo ) && this one counts ... | |
202 | formats[n++] = node->GetData()->GetFormat(); | |
203 | } | |
204 | } | |
205 | ||
206 | size_t wxDataObjectComposite::GetDataSize(const wxDataFormat& format) const | |
207 | { | |
208 | wxDataObjectSimple *dataObj = GetObject(format); | |
209 | ||
210 | wxCHECK_MSG( dataObj, 0, | |
211 | wxT("unsupported format in wxDataObjectComposite")); | |
212 | ||
213 | return dataObj->GetDataSize(); | |
214 | } | |
215 | ||
216 | bool wxDataObjectComposite::GetDataHere(const wxDataFormat& format, | |
217 | void *buf) const | |
218 | { | |
219 | wxDataObjectSimple *dataObj = GetObject(format); | |
220 | ||
221 | wxCHECK_MSG( dataObj, FALSE, | |
222 | wxT("unsupported format in wxDataObjectComposite")); | |
223 | ||
224 | return dataObj->GetDataHere(buf); | |
225 | } | |
226 | ||
227 | bool wxDataObjectComposite::SetData(const wxDataFormat& format, | |
228 | size_t len, | |
229 | const void *buf) | |
230 | { | |
231 | wxDataObjectSimple *dataObj = GetObject(format); | |
232 | ||
233 | wxCHECK_MSG( dataObj, FALSE, | |
234 | wxT("unsupported format in wxDataObjectComposite")); | |
235 | ||
236 | return dataObj->SetData(len, buf); | |
237 | } | |
238 | ||
239 | // ---------------------------------------------------------------------------- | |
240 | // wxTextDataObject | |
241 | // ---------------------------------------------------------------------------- | |
242 | ||
243 | size_t wxTextDataObject::GetDataSize() const | |
244 | { | |
6a59178a RR |
245 | #if defined(__WXGTK20__) && wxUSE_UNICODE |
246 | // Use UTF8 not UCS4 | |
247 | wxCharBuffer buffer = wxConvUTF8.cWX2MB( GetText().c_str() ); | |
248 | return strlen( (const char*) buffer ) + 1; | |
249 | #else | |
e1b435af | 250 | return GetTextLength() * sizeof(wxChar); |
6a59178a | 251 | #endif |
3f364be8 VZ |
252 | } |
253 | ||
254 | bool wxTextDataObject::GetDataHere(void *buf) const | |
255 | { | |
6a59178a RR |
256 | #if defined(__WXGTK20__) && wxUSE_UNICODE |
257 | // Use UTF8 not UCS4 | |
258 | wxCharBuffer buffer = wxConvUTF8.cWX2MB( GetText().c_str() ); | |
259 | strcpy( (char*) buf, (const char*) buffer ); | |
260 | #else | |
e1b435af | 261 | wxStrcpy((wxChar *)buf, GetText().c_str()); |
6a59178a | 262 | #endif |
3f364be8 VZ |
263 | |
264 | return TRUE; | |
265 | } | |
266 | ||
267 | bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf) | |
268 | { | |
6a59178a RR |
269 | #if defined(__WXGTK20__) && wxUSE_UNICODE |
270 | // Use UTF8 not UCS4 | |
271 | SetText( wxConvUTF8.cMB2WX( (const char*) buf ) ); | |
272 | #else | |
e1b435af | 273 | SetText(wxString((const wxChar *)buf)); |
6a59178a | 274 | #endif |
3f364be8 VZ |
275 | |
276 | return TRUE; | |
277 | } | |
278 | ||
279 | // ---------------------------------------------------------------------------- | |
280 | // wxFileDataObjectBase | |
281 | // ---------------------------------------------------------------------------- | |
282 | ||
9e2896e5 VZ |
283 | // VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should |
284 | // be moved to gtk/dataobj.cpp | |
285 | #if 0 | |
286 | ||
3f364be8 VZ |
287 | wxString wxFileDataObjectBase::GetFilenames() const |
288 | { | |
289 | wxString str; | |
290 | size_t count = m_filenames.GetCount(); | |
291 | for ( size_t n = 0; n < count; n++ ) | |
292 | { | |
293 | str << m_filenames[n] << wxT('\0'); | |
294 | } | |
295 | ||
296 | return str; | |
297 | } | |
298 | ||
299 | void wxFileDataObjectBase::SetFilenames(const wxChar* filenames) | |
300 | { | |
301 | m_filenames.Empty(); | |
302 | ||
303 | wxString current; | |
304 | for ( const wxChar *pc = filenames; ; pc++ ) | |
305 | { | |
306 | if ( *pc ) | |
307 | { | |
308 | current += *pc; | |
309 | } | |
310 | else | |
311 | { | |
312 | if ( !current ) | |
313 | { | |
314 | // 2 consecutive NULs - this is the end of the string | |
315 | break; | |
316 | } | |
317 | ||
318 | m_filenames.Add(current); | |
319 | current.Empty(); | |
320 | } | |
321 | } | |
322 | } | |
323 | ||
9e2896e5 VZ |
324 | #endif // 0 |
325 | ||
3f364be8 VZ |
326 | // ---------------------------------------------------------------------------- |
327 | // wxCustomDataObject | |
328 | // ---------------------------------------------------------------------------- | |
329 | ||
775e1c62 RD |
330 | wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format) |
331 | : wxDataObjectSimple(format) | |
332 | { | |
4e16881a | 333 | m_data = (void *)NULL; |
775e1c62 RD |
334 | } |
335 | ||
3f364be8 VZ |
336 | wxCustomDataObject::~wxCustomDataObject() |
337 | { | |
338 | Free(); | |
339 | } | |
340 | ||
341 | void wxCustomDataObject::TakeData(size_t size, void *data) | |
342 | { | |
343 | Free(); | |
344 | ||
345 | m_size = size; | |
346 | m_data = data; | |
347 | } | |
348 | ||
349 | void *wxCustomDataObject::Alloc(size_t size) | |
350 | { | |
351 | return (void *)new char[size]; | |
352 | } | |
353 | ||
354 | void wxCustomDataObject::Free() | |
355 | { | |
5bcea60e | 356 | delete [] (char *)m_data; |
3f364be8 VZ |
357 | m_size = 0; |
358 | m_data = (void *)NULL; | |
359 | } | |
360 | ||
361 | size_t wxCustomDataObject::GetDataSize() const | |
362 | { | |
363 | return GetSize(); | |
364 | } | |
365 | ||
366 | bool wxCustomDataObject::GetDataHere(void *buf) const | |
367 | { | |
368 | void *data = GetData(); | |
369 | if ( !data ) | |
370 | return FALSE; | |
371 | ||
372 | memcpy(buf, data, GetSize()); | |
373 | ||
374 | return TRUE; | |
375 | } | |
376 | ||
9e2896e5 | 377 | bool wxCustomDataObject::SetData(size_t size, const void *buf) |
3f364be8 VZ |
378 | { |
379 | Free(); | |
380 | ||
381 | m_data = Alloc(size); | |
382 | if ( !m_data ) | |
383 | return FALSE; | |
384 | ||
9e2896e5 | 385 | memcpy(m_data, buf, m_size = size); |
3f364be8 VZ |
386 | |
387 | return TRUE; | |
388 | } | |
389 | ||
8ee9d618 VZ |
390 | // ============================================================================ |
391 | // some common dnd related code | |
392 | // ============================================================================ | |
393 | ||
8fb3a512 JS |
394 | #if wxUSE_DRAG_AND_DROP |
395 | ||
8ee9d618 VZ |
396 | #include "wx/dnd.h" |
397 | ||
398 | // ---------------------------------------------------------------------------- | |
399 | // wxTextDropTarget | |
400 | // ---------------------------------------------------------------------------- | |
401 | ||
f3ac12aa VZ |
402 | // NB: we can't use "new" in ctor initializer lists because this provokes an |
403 | // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!), | |
404 | // so use SetDataObject() instead | |
405 | ||
8ee9d618 | 406 | wxTextDropTarget::wxTextDropTarget() |
8ee9d618 | 407 | { |
f3ac12aa | 408 | SetDataObject(new wxTextDataObject); |
8ee9d618 VZ |
409 | } |
410 | ||
411 | wxDragResult wxTextDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def) | |
412 | { | |
413 | if ( !GetData() ) | |
414 | return wxDragNone; | |
415 | ||
416 | wxTextDataObject *dobj = (wxTextDataObject *)m_dataObject; | |
417 | return OnDropText(x, y, dobj->GetText()) ? def : wxDragNone; | |
418 | } | |
419 | ||
420 | // ---------------------------------------------------------------------------- | |
421 | // wxFileDropTarget | |
422 | // ---------------------------------------------------------------------------- | |
423 | ||
424 | wxFileDropTarget::wxFileDropTarget() | |
8ee9d618 | 425 | { |
f3ac12aa | 426 | SetDataObject(new wxFileDataObject); |
8ee9d618 VZ |
427 | } |
428 | ||
429 | wxDragResult wxFileDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def) | |
430 | { | |
431 | if ( !GetData() ) | |
432 | return wxDragNone; | |
433 | ||
434 | wxFileDataObject *dobj = (wxFileDataObject *)m_dataObject; | |
435 | return OnDropFiles(x, y, dobj->GetFilenames()) ? def : wxDragNone; | |
436 | } | |
437 | ||
1e6feb95 | 438 | #endif // wxUSE_DRAG_AND_DROP |
8fb3a512 | 439 | |
1e6feb95 | 440 | #endif // wxUSE_DATAOBJ |