update to make digitalmars compile/link libraries
[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 licence
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 #if wxUSE_DATAOBJ
31
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
47 // ----------------------------------------------------------------------------
48 // globals
49 // ----------------------------------------------------------------------------
50
51 static wxDataFormat dataFormatInvalid;
52 WXDLLEXPORT const wxDataFormat& wxFormatInvalid = dataFormatInvalid;
53
54 // ============================================================================
55 // implementation
56 // ============================================================================
57
58 // ----------------------------------------------------------------------------
59 // wxDataObjectBase
60 // ----------------------------------------------------------------------------
61
62 wxDataObjectBase::~wxDataObjectBase()
63 {
64 }
65
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
93 // ----------------------------------------------------------------------------
94 // wxDataObjectComposite
95 // ----------------------------------------------------------------------------
96
97 wxDataObjectComposite::wxDataObjectComposite()
98 {
99 m_preferred = 0;
100
101 m_dataObjects.DeleteContents(TRUE);
102 }
103
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
136 wxCHECK_MSG( node, wxFormatInvalid, wxT("no preferred format") );
137
138 wxDataObjectSimple* dataObj = node->GetData();
139
140 return dataObj->GetFormat();
141 }
142
143 #if defined(__WXMSW__)
144 #ifdef __DIGITALMARS__
145 extern "C"
146 #endif
147 size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat& format )
148 {
149 wxDataObjectSimple *dataObj = GetObject(format);
150
151 wxCHECK_MSG( dataObj, 0,
152 wxT("unsupported format in wxDataObjectComposite"));
153
154 return dataObj->GetBufferOffset( format );
155 }
156
157 #ifdef __DIGITALMARS__
158 extern "C"
159 #endif
160 const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer,
161 size_t* size,
162 const wxDataFormat& format )
163 {
164 wxDataObjectSimple *dataObj = GetObject(format);
165
166 wxCHECK_MSG( dataObj, NULL,
167 wxT("unsupported format in wxDataObjectComposite"));
168
169 return dataObj->GetSizeFromBuffer( buffer, size, format );
170 }
171
172 #ifdef __DIGITALMARS__
173 extern "C"
174 #endif
175 void* wxDataObjectComposite::SetSizeInBuffer( void* buffer, size_t size,
176 const wxDataFormat& format )
177 {
178 wxDataObjectSimple *dataObj = GetObject(format);
179
180 wxCHECK_MSG( dataObj, NULL,
181 wxT("unsupported format in wxDataObjectComposite"));
182
183 return dataObj->SetSizeInBuffer( buffer, size, format );
184 }
185
186 #endif
187
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 {
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
250 return GetTextLength() * sizeof(wxChar);
251 #endif
252 }
253
254 bool wxTextDataObject::GetDataHere(void *buf) const
255 {
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
261 wxStrcpy((wxChar *)buf, GetText().c_str());
262 #endif
263
264 return TRUE;
265 }
266
267 bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf)
268 {
269 #if defined(__WXGTK20__) && wxUSE_UNICODE
270 // Use UTF8 not UCS4
271 SetText( wxConvUTF8.cMB2WX( (const char*) buf ) );
272 #else
273 SetText(wxString((const wxChar *)buf));
274 #endif
275
276 return TRUE;
277 }
278
279 // ----------------------------------------------------------------------------
280 // wxFileDataObjectBase
281 // ----------------------------------------------------------------------------
282
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
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
324 #endif // 0
325
326 // ----------------------------------------------------------------------------
327 // wxCustomDataObject
328 // ----------------------------------------------------------------------------
329
330 wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format)
331 : wxDataObjectSimple(format)
332 {
333 m_data = (void *)NULL;
334 }
335
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 {
356 delete [] (char *)m_data;
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
377 bool wxCustomDataObject::SetData(size_t size, const void *buf)
378 {
379 Free();
380
381 m_data = Alloc(size);
382 if ( !m_data )
383 return FALSE;
384
385 memcpy(m_data, buf, m_size = size);
386
387 return TRUE;
388 }
389
390 // ============================================================================
391 // some common dnd related code
392 // ============================================================================
393
394 #if wxUSE_DRAG_AND_DROP
395
396 #include "wx/dnd.h"
397
398 // ----------------------------------------------------------------------------
399 // wxTextDropTarget
400 // ----------------------------------------------------------------------------
401
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
406 wxTextDropTarget::wxTextDropTarget()
407 {
408 SetDataObject(new wxTextDataObject);
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()
425 {
426 SetDataObject(new wxFileDataObject);
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
438 #endif // wxUSE_DRAG_AND_DROP
439
440 #endif // wxUSE_DATAOBJ