1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: common data object classes
4 // Author: Vadim Zeitlin, Robert Roebling
7 // Copyright: (c) wxWidgets Team
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_DATAOBJ_H_BASE_
12 #define _WX_DATAOBJ_H_BASE_
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
21 #include "wx/string.h"
22 #include "wx/bitmap.h"
24 #include "wx/arrstr.h"
26 // ============================================================================
28 Generic data transfer related classes. The class hierarchy is as follows:
33 wxDataObjectSimple wxDataObjectComposite
36 wxTextDataObject | wxBitmapDataObject
41 // ============================================================================
43 // ----------------------------------------------------------------------------
44 // wxDataFormat class is declared in platform-specific headers: it represents
45 // a format for data which may be either one of the standard ones (text,
46 // bitmap, ...) or a custom one which is then identified by a unique string.
47 // ----------------------------------------------------------------------------
49 /* the class interface looks like this (pseudo code):
54 typedef <integral type> NativeFormat;
56 wxDataFormat(NativeFormat format = wxDF_INVALID);
57 wxDataFormat(const wxString& format);
59 wxDataFormat& operator=(NativeFormat format);
60 wxDataFormat& operator=(const wxDataFormat& format);
62 bool operator==(NativeFormat format) const;
63 bool operator!=(NativeFormat format) const;
65 void SetType(NativeFormat format);
66 NativeFormat GetType() const;
68 wxString GetId() const;
69 void SetId(const wxString& format);
74 #if defined(__WXMSW__)
75 #include "wx/msw/ole/dataform.h"
76 #elif defined(__WXMOTIF__)
77 #include "wx/motif/dataform.h"
78 #elif defined(__WXGTK20__)
79 #include "wx/gtk/dataform.h"
80 #elif defined(__WXGTK__)
81 #include "wx/gtk1/dataform.h"
82 #elif defined(__WXX11__)
83 #include "wx/x11/dataform.h"
84 #elif defined(__WXMAC__)
85 #include "wx/osx/dataform.h"
86 #elif defined(__WXCOCOA__)
87 #include "wx/cocoa/dataform.h"
88 #elif defined(__WXPM__)
89 #include "wx/os2/dataform.h"
92 // the value for default argument to some functions (corresponds to
94 extern WXDLLIMPEXP_CORE
const wxDataFormat
& wxFormatInvalid
;
96 // ----------------------------------------------------------------------------
97 // wxDataObject represents a piece of data which knows which formats it
98 // supports and knows how to render itself in each of them - GetDataHere(),
99 // and how to restore data from the buffer (SetData()).
101 // Although this class may be used directly (i.e. custom classes may be
102 // derived from it), in many cases it might be simpler to use either
103 // wxDataObjectSimple or wxDataObjectComposite classes.
105 // A data object may be "read only", i.e. support only GetData() functions or
106 // "read-write", i.e. support both GetData() and SetData() (in principle, it
107 // might be "write only" too, but this is rare). Moreover, it doesn't have to
108 // support the same formats in Get() and Set() directions: for example, a data
109 // object containing JPEG image might accept BMPs in GetData() because JPEG
110 // image may be easily transformed into BMP but not in SetData(). Accordingly,
111 // all methods dealing with formats take an additional "direction" argument
112 // which is either SET or GET and which tells the function if the format needs
113 // to be supported by SetData() or GetDataHere().
114 // ----------------------------------------------------------------------------
116 class WXDLLIMPEXP_CORE wxDataObjectBase
121 Get
= 0x01, // format is supported by GetDataHere()
122 Set
= 0x02, // format is supported by SetData()
123 Both
= 0x03 // format is supported by both (unused currently)
126 // this class is polymorphic, hence it needs a virtual dtor
127 virtual ~wxDataObjectBase();
129 // get the best suited format for rendering our data
130 virtual wxDataFormat
GetPreferredFormat(Direction dir
= Get
) const = 0;
132 // get the number of formats we support
133 virtual size_t GetFormatCount(Direction dir
= Get
) const = 0;
135 // return all formats in the provided array (of size GetFormatCount())
136 virtual void GetAllFormats(wxDataFormat
*formats
,
137 Direction dir
= Get
) const = 0;
139 // get the (total) size of data for the given format
140 virtual size_t GetDataSize(const wxDataFormat
& format
) const = 0;
142 // copy raw data (in the specified format) to the provided buffer, return
143 // true if data copied successfully, false otherwise
144 virtual bool GetDataHere(const wxDataFormat
& format
, void *buf
) const = 0;
146 // get data from the buffer of specified length (in the given format),
147 // return true if the data was read successfully, false otherwise
148 virtual bool SetData(const wxDataFormat
& WXUNUSED(format
),
149 size_t WXUNUSED(len
), const void * WXUNUSED(buf
))
154 // returns true if this format is supported
155 bool IsSupported(const wxDataFormat
& format
, Direction dir
= Get
) const;
158 // ----------------------------------------------------------------------------
159 // include the platform-specific declarations of wxDataObject
160 // ----------------------------------------------------------------------------
162 #if defined(__WXMSW__)
163 #include "wx/msw/ole/dataobj.h"
164 #elif defined(__WXMOTIF__)
165 #include "wx/motif/dataobj.h"
166 #elif defined(__WXX11__)
167 #include "wx/x11/dataobj.h"
168 #elif defined(__WXGTK20__)
169 #include "wx/gtk/dataobj.h"
170 #elif defined(__WXGTK__)
171 #include "wx/gtk1/dataobj.h"
172 #elif defined(__WXMAC__)
173 #include "wx/osx/dataobj.h"
174 #elif defined(__WXCOCOA__)
175 #include "wx/cocoa/dataobj.h"
176 #elif defined(__WXPM__)
177 #include "wx/os2/dataobj.h"
180 // ----------------------------------------------------------------------------
181 // wxDataObjectSimple is a wxDataObject which only supports one format (in
182 // both Get and Set directions, but you may return false from GetDataHere() or
183 // SetData() if one of them is not supported). This is the simplest possible
184 // wxDataObject implementation.
186 // This is still an "abstract base class" (although it doesn't have any pure
187 // virtual functions), to use it you should derive from it and implement
188 // GetDataSize(), GetDataHere() and SetData() functions because the base class
189 // versions don't do anything - they just return "not implemented".
191 // This class should be used when you provide data in only one format (no
192 // conversion to/from other formats), either a standard or a custom one.
193 // Otherwise, you should use wxDataObjectComposite or wxDataObject directly.
194 // ----------------------------------------------------------------------------
196 class WXDLLIMPEXP_CORE wxDataObjectSimple
: public wxDataObject
199 // ctor takes the format we support, but it can also be set later with
201 wxDataObjectSimple(const wxDataFormat
& format
= wxFormatInvalid
)
206 // get/set the format we support
207 const wxDataFormat
& GetFormat() const { return m_format
; }
208 void SetFormat(const wxDataFormat
& format
) { m_format
= format
; }
210 // virtual functions to override in derived class (the base class versions
211 // just return "not implemented")
212 // -----------------------------------------------------------------------
214 // get the size of our data
215 virtual size_t GetDataSize() const
218 // copy our data to the buffer
219 virtual bool GetDataHere(void *WXUNUSED(buf
)) const
222 // copy data from buffer to our data
223 virtual bool SetData(size_t WXUNUSED(len
), const void *WXUNUSED(buf
))
226 // implement base class pure virtuals
227 // ----------------------------------
228 virtual wxDataFormat
GetPreferredFormat(wxDataObjectBase::Direction
WXUNUSED(dir
) = Get
) const
230 virtual size_t GetFormatCount(wxDataObjectBase::Direction
WXUNUSED(dir
) = Get
) const
232 virtual void GetAllFormats(wxDataFormat
*formats
,
233 wxDataObjectBase::Direction
WXUNUSED(dir
) = Get
) const
234 { *formats
= m_format
; }
235 virtual size_t GetDataSize(const wxDataFormat
& WXUNUSED(format
)) const
236 { return GetDataSize(); }
237 virtual bool GetDataHere(const wxDataFormat
& WXUNUSED(format
),
239 { return GetDataHere(buf
); }
240 virtual bool SetData(const wxDataFormat
& WXUNUSED(format
),
241 size_t len
, const void *buf
)
242 { return SetData(len
, buf
); }
245 // the one and only format we support
246 wxDataFormat m_format
;
248 wxDECLARE_NO_COPY_CLASS(wxDataObjectSimple
);
251 // ----------------------------------------------------------------------------
252 // wxDataObjectComposite is the simplest way to implement wxDataObject
253 // supporting multiple formats. It contains several wxDataObjectSimple and
254 // supports all formats supported by any of them.
256 // This class shouldn't be (normally) derived from, but may be used directly.
257 // If you need more flexibility than what it provides, you should probably use
258 // wxDataObject directly.
259 // ----------------------------------------------------------------------------
261 WX_DECLARE_EXPORTED_LIST(wxDataObjectSimple
, wxSimpleDataObjectList
);
263 class WXDLLIMPEXP_CORE wxDataObjectComposite
: public wxDataObject
267 wxDataObjectComposite();
268 virtual ~wxDataObjectComposite();
270 // add data object (it will be deleted by wxDataObjectComposite, hence it
271 // must be allocated on the heap) whose format will become the preferred
272 // one if preferred == true
273 void Add(wxDataObjectSimple
*dataObject
, bool preferred
= false);
275 // Report the format passed to the SetData method. This should be the
276 // format of the data object within the composite that received data from
277 // the clipboard or the DnD operation. You can use this method to find
278 // out what kind of data object was received.
279 wxDataFormat
GetReceivedFormat() const;
281 // Returns the pointer to the object which supports this format or NULL.
282 // The returned pointer is owned by wxDataObjectComposite and must
283 // therefore not be destroyed by the caller.
284 wxDataObjectSimple
*GetObject(const wxDataFormat
& format
,
285 wxDataObjectBase::Direction dir
= Get
) const;
287 // implement base class pure virtuals
288 // ----------------------------------
289 virtual wxDataFormat
GetPreferredFormat(wxDataObjectBase::Direction dir
= Get
) const;
290 virtual size_t GetFormatCount(wxDataObjectBase::Direction dir
= Get
) const;
291 virtual void GetAllFormats(wxDataFormat
*formats
, wxDataObjectBase::Direction dir
= Get
) const;
292 virtual size_t GetDataSize(const wxDataFormat
& format
) const;
293 virtual bool GetDataHere(const wxDataFormat
& format
, void *buf
) const;
294 virtual bool SetData(const wxDataFormat
& format
, size_t len
, const void *buf
);
295 #if defined(__WXMSW__)
296 virtual const void* GetSizeFromBuffer( const void* buffer
, size_t* size
,
297 const wxDataFormat
& format
);
298 virtual void* SetSizeInBuffer( void* buffer
, size_t size
,
299 const wxDataFormat
& format
);
300 virtual size_t GetBufferOffset( const wxDataFormat
& format
);
304 // the list of all (simple) data objects whose formats we support
305 wxSimpleDataObjectList m_dataObjects
;
307 // the index of the preferred one (0 initially, so by default the first
308 // one is the preferred)
311 wxDataFormat m_receivedFormat
;
313 wxDECLARE_NO_COPY_CLASS(wxDataObjectComposite
);
316 // ============================================================================
317 // Standard implementations of wxDataObjectSimple which can be used directly
318 // (i.e. without having to derive from them) for standard data type transfers.
320 // Note that although all of them can work with provided data, you can also
321 // override their virtual GetXXX() functions to only provide data on demand.
322 // ============================================================================
324 // ----------------------------------------------------------------------------
325 // wxTextDataObject contains text data
326 // ----------------------------------------------------------------------------
329 #if defined(__WXGTK20__)
330 #define wxNEEDS_UTF8_FOR_TEXT_DATAOBJ
331 #elif defined(__WXMAC__)
332 #define wxNEEDS_UTF16_FOR_TEXT_DATAOBJ
334 #endif // wxUSE_UNICODE
336 class WXDLLIMPEXP_CORE wxHTMLDataObject
: public wxDataObjectSimple
339 // ctor: you can specify the text here or in SetText(), or override
341 wxHTMLDataObject(const wxString
& html
= wxEmptyString
)
342 : wxDataObjectSimple(wxDF_HTML
),
347 // virtual functions which you may override if you want to provide text on
348 // demand only - otherwise, the trivial default versions will be used
349 virtual size_t GetLength() const { return m_html
.Len() + 1; }
350 virtual wxString
GetHTML() const { return m_html
; }
351 virtual void SetHTML(const wxString
& html
) { m_html
= html
; }
353 virtual size_t GetDataSize() const;
354 virtual bool GetDataHere(void *buf
) const;
355 virtual bool SetData(size_t len
, const void *buf
);
357 // Must provide overloads to avoid hiding them (and warnings about it)
358 virtual size_t GetDataSize(const wxDataFormat
&) const
360 return GetDataSize();
362 virtual bool GetDataHere(const wxDataFormat
&, void *buf
) const
364 return GetDataHere(buf
);
366 virtual bool SetData(const wxDataFormat
&, size_t len
, const void *buf
)
368 return SetData(len
, buf
);
375 class WXDLLIMPEXP_CORE wxTextDataObject
: public wxDataObjectSimple
378 // ctor: you can specify the text here or in SetText(), or override
380 wxTextDataObject(const wxString
& text
= wxEmptyString
)
381 : wxDataObjectSimple(
392 // virtual functions which you may override if you want to provide text on
393 // demand only - otherwise, the trivial default versions will be used
394 virtual size_t GetTextLength() const { return m_text
.Len() + 1; }
395 virtual wxString
GetText() const { return m_text
; }
396 virtual void SetText(const wxString
& text
) { m_text
= text
; }
398 // implement base class pure virtuals
399 // ----------------------------------
401 // some platforms have 2 and not 1 format for text data
402 #if defined(wxNEEDS_UTF8_FOR_TEXT_DATAOBJ) || defined(wxNEEDS_UTF16_FOR_TEXT_DATAOBJ)
403 virtual size_t GetFormatCount(Direction
WXUNUSED(dir
) = Get
) const { return 2; }
404 virtual void GetAllFormats(wxDataFormat
*formats
,
405 wxDataObjectBase::Direction
WXUNUSED(dir
) = Get
) const;
407 virtual size_t GetDataSize() const { return GetDataSize(GetPreferredFormat()); }
408 virtual bool GetDataHere(void *buf
) const { return GetDataHere(GetPreferredFormat(), buf
); }
409 virtual bool SetData(size_t len
, const void *buf
) { return SetData(GetPreferredFormat(), len
, buf
); }
411 size_t GetDataSize(const wxDataFormat
& format
) const;
412 bool GetDataHere(const wxDataFormat
& format
, void *pBuf
) const;
413 bool SetData(const wxDataFormat
& format
, size_t nLen
, const void* pBuf
);
414 #else // !wxNEEDS_UTF{8,16}_FOR_TEXT_DATAOBJ
415 virtual size_t GetDataSize() const;
416 virtual bool GetDataHere(void *buf
) const;
417 virtual bool SetData(size_t len
, const void *buf
);
418 // Must provide overloads to avoid hiding them (and warnings about it)
419 virtual size_t GetDataSize(const wxDataFormat
&) const
421 return GetDataSize();
423 virtual bool GetDataHere(const wxDataFormat
&, void *buf
) const
425 return GetDataHere(buf
);
427 virtual bool SetData(const wxDataFormat
&, size_t len
, const void *buf
)
429 return SetData(len
, buf
);
431 #endif // different wxTextDataObject implementations
436 wxDECLARE_NO_COPY_CLASS(wxTextDataObject
);
439 // ----------------------------------------------------------------------------
440 // wxBitmapDataObject contains a bitmap
441 // ----------------------------------------------------------------------------
443 class WXDLLIMPEXP_CORE wxBitmapDataObjectBase
: public wxDataObjectSimple
446 // ctor: you can specify the bitmap here or in SetBitmap(), or override
448 wxBitmapDataObjectBase(const wxBitmap
& bitmap
= wxNullBitmap
)
449 : wxDataObjectSimple(wxDF_BITMAP
), m_bitmap(bitmap
)
453 // virtual functions which you may override if you want to provide data on
454 // demand only - otherwise, the trivial default versions will be used
455 virtual wxBitmap
GetBitmap() const { return m_bitmap
; }
456 virtual void SetBitmap(const wxBitmap
& bitmap
) { m_bitmap
= bitmap
; }
461 wxDECLARE_NO_COPY_CLASS(wxBitmapDataObjectBase
);
464 // ----------------------------------------------------------------------------
465 // wxFileDataObject contains a list of filenames
467 // NB: notice that this is a "write only" object, it can only be filled with
468 // data from drag and drop operation.
469 // ----------------------------------------------------------------------------
471 class WXDLLIMPEXP_CORE wxFileDataObjectBase
: public wxDataObjectSimple
474 // ctor: use AddFile() later to fill the array
475 wxFileDataObjectBase() : wxDataObjectSimple(wxDF_FILENAME
) { }
477 // get a reference to our array
478 const wxArrayString
& GetFilenames() const { return m_filenames
; }
481 wxArrayString m_filenames
;
483 wxDECLARE_NO_COPY_CLASS(wxFileDataObjectBase
);
486 // ----------------------------------------------------------------------------
487 // wxCustomDataObject contains arbitrary untyped user data.
489 // It is understood that this data can be copied bitwise.
490 // ----------------------------------------------------------------------------
492 class WXDLLIMPEXP_CORE wxCustomDataObject
: public wxDataObjectSimple
495 // if you don't specify the format in the ctor, you can still use
497 wxCustomDataObject(const wxDataFormat
& format
= wxFormatInvalid
);
499 // the dtor calls Free()
500 virtual ~wxCustomDataObject();
502 // you can call SetData() to set m_data: it will make a copy of the data
503 // you pass - or you can use TakeData() which won't copy anything, but
504 // will take ownership of data (i.e. will call Free() on it later)
505 void TakeData(size_t size
, void *data
);
507 // this function is called to allocate "size" bytes of memory from
508 // SetData(). The default version uses operator new[].
509 virtual void *Alloc(size_t size
);
511 // this function is called when the data is freed, you may override it to
512 // anything you want (or may be nothing at all). The default version calls
513 // operator delete[] on m_data
516 // get data: you may override these functions if you wish to provide data
517 // only when it's requested
518 virtual size_t GetSize() const { return m_size
; }
519 virtual void *GetData() const { return m_data
; }
521 // implement base class pure virtuals
522 // ----------------------------------
523 virtual size_t GetDataSize() const;
524 virtual bool GetDataHere(void *buf
) const;
525 virtual bool SetData(size_t size
, const void *buf
);
526 // Must provide overloads to avoid hiding them (and warnings about it)
527 virtual size_t GetDataSize(const wxDataFormat
&) const
529 return GetDataSize();
531 virtual bool GetDataHere(const wxDataFormat
&, void *buf
) const
533 return GetDataHere(buf
);
535 virtual bool SetData(const wxDataFormat
&, size_t len
, const void *buf
)
537 return SetData(len
, buf
);
544 wxDECLARE_NO_COPY_CLASS(wxCustomDataObject
);
547 // ----------------------------------------------------------------------------
548 // include platform-specific declarations of wxXXXBase classes
549 // ----------------------------------------------------------------------------
551 #if defined(__WXMSW__)
552 #include "wx/msw/ole/dataobj2.h"
553 // wxURLDataObject defined in msw/ole/dataobj2.h
554 #elif defined(__WXGTK20__)
555 #include "wx/gtk/dataobj2.h"
556 // wxURLDataObject defined in msw/ole/dataobj2.h
559 #if defined(__WXGTK__)
560 #include "wx/gtk1/dataobj2.h"
561 #elif defined(__WXX11__)
562 #include "wx/x11/dataobj2.h"
563 #elif defined(__WXMOTIF__)
564 #include "wx/motif/dataobj2.h"
565 #elif defined(__WXMAC__)
566 #include "wx/osx/dataobj2.h"
567 #elif defined(__WXCOCOA__)
568 #include "wx/cocoa/dataobj2.h"
569 #elif defined(__WXPM__)
570 #include "wx/os2/dataobj2.h"
573 // wxURLDataObject is simply wxTextDataObject with a different name
574 class WXDLLIMPEXP_CORE wxURLDataObject
: public wxTextDataObject
577 wxURLDataObject(const wxString
& url
= wxEmptyString
)
578 : wxTextDataObject(url
)
582 wxString
GetURL() const { return GetText(); }
583 void SetURL(const wxString
& url
) { SetText(url
); }
587 #endif // wxUSE_DATAOBJ
589 #endif // _WX_DATAOBJ_H_BASE_