1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: common data object classes
4 // Author: Vadim Zeitlin, Robert Roebling
8 // Copyright: (c) wxWidgets Team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_DATAOBJ_H_BASE_
13 #define _WX_DATAOBJ_H_BASE_
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
22 #include "wx/string.h"
23 #include "wx/bitmap.h"
25 #include "wx/arrstr.h"
27 // ============================================================================
29 Generic data transfer related classes. The class hierarchy is as follows:
34 wxDataObjectSimple wxDataObjectComposite
37 wxTextDataObject | wxBitmapDataObject
42 // ============================================================================
44 // ----------------------------------------------------------------------------
45 // wxDataFormat class is declared in platform-specific headers: it represents
46 // a format for data which may be either one of the standard ones (text,
47 // bitmap, ...) or a custom one which is then identified by a unique string.
48 // ----------------------------------------------------------------------------
50 /* the class interface looks like this (pseudo code):
55 typedef <integral type> NativeFormat;
57 wxDataFormat(NativeFormat format = wxDF_INVALID);
58 wxDataFormat(const wxChar *format);
60 wxDataFormat& operator=(NativeFormat format);
61 wxDataFormat& operator=(const wxDataFormat& format);
63 bool operator==(NativeFormat format) const;
64 bool operator!=(NativeFormat format) const;
66 void SetType(NativeFormat format);
67 NativeFormat GetType() const;
69 wxString GetId() const;
70 void SetId(const wxChar *format);
75 #if defined(__WXMSW__)
76 #include "wx/msw/ole/dataform.h"
77 #elif defined(__WXMOTIF__)
78 #include "wx/motif/dataform.h"
79 #elif defined(__WXGTK__)
80 #include "wx/gtk/dataform.h"
81 #elif defined(__WXX11__)
82 #include "wx/x11/dataform.h"
83 #elif defined(__WXMAC__)
84 #include "wx/mac/dataform.h"
85 #elif defined(__WXCOCOA__)
86 #include "wx/cocoa/dataform.h"
87 #elif defined(__WXPM__)
88 #include "wx/os2/dataform.h"
91 // the value for default argument to some functions (corresponds to
93 extern WXDLLEXPORT
const wxDataFormat
& wxFormatInvalid
;
95 // ----------------------------------------------------------------------------
96 // wxDataObject represents a piece of data which knows which formats it
97 // supports and knows how to render itself in each of them - GetDataHere(),
98 // and how to restore data from the buffer (SetData()).
100 // Although this class may be used directly (i.e. custom classes may be
101 // derived from it), in many cases it might be simpler to use either
102 // wxDataObjectSimple or wxDataObjectComposite classes.
104 // A data object may be "read only", i.e. support only GetData() functions or
105 // "read-write", i.e. support both GetData() and SetData() (in principle, it
106 // might be "write only" too, but this is rare). Moreover, it doesn't have to
107 // support the same formats in Get() and Set() directions: for example, a data
108 // object containing JPEG image might accept BMPs in GetData() because JPEG
109 // image may be easily transformed into BMP but not in SetData(). Accordingly,
110 // all methods dealing with formats take an additional "direction" argument
111 // which is either SET or GET and which tells the function if the format needs
112 // to be supported by SetData() or GetDataHere().
113 // ----------------------------------------------------------------------------
115 class WXDLLEXPORT wxDataObjectBase
120 Get
= 0x01, // format is supported by GetDataHere()
121 Set
= 0x02, // format is supported by SetData()
122 Both
= 0x03 // format is supported by both (unused currently)
125 // this class is polymorphic, hence it needs a virtual dtor
126 virtual ~wxDataObjectBase();
128 // get the best suited format for rendering our data
129 virtual wxDataFormat
GetPreferredFormat(Direction dir
= Get
) const = 0;
131 // get the number of formats we support
132 virtual size_t GetFormatCount(Direction dir
= Get
) const = 0;
134 // return all formats in the provided array (of size GetFormatCount())
135 virtual void GetAllFormats(wxDataFormat
*formats
,
136 Direction dir
= Get
) const = 0;
138 // get the (total) size of data for the given format
139 virtual size_t GetDataSize(const wxDataFormat
& format
) const = 0;
141 // copy raw data (in the specified format) to the provided buffer, return
142 // true if data copied successfully, false otherwise
143 virtual bool GetDataHere(const wxDataFormat
& format
, void *buf
) const = 0;
145 // get data from the buffer of specified length (in the given format),
146 // return true if the data was read successfully, false otherwise
147 virtual bool SetData(const wxDataFormat
& WXUNUSED(format
),
148 size_t WXUNUSED(len
), const void * WXUNUSED(buf
))
153 // returns true if this format is supported
154 bool IsSupported(const wxDataFormat
& format
, Direction dir
= Get
) const;
157 // ----------------------------------------------------------------------------
158 // include the platform-specific declarations of wxDataObject
159 // ----------------------------------------------------------------------------
161 #if defined(__WXMSW__)
162 #include "wx/msw/ole/dataobj.h"
163 #elif defined(__WXMOTIF__)
164 #include "wx/motif/dataobj.h"
165 #elif defined(__WXX11__)
166 #include "wx/x11/dataobj.h"
167 #elif defined(__WXGTK__)
168 #include "wx/gtk/dataobj.h"
169 #elif defined(__WXMAC__)
170 #include "wx/mac/dataobj.h"
171 #elif defined(__WXCOCOA__)
172 #include "wx/cocoa/dataobj.h"
173 #elif defined(__WXPM__)
174 #include "wx/os2/dataobj.h"
177 // ----------------------------------------------------------------------------
178 // wxDataObjectSimple is a wxDataObject which only supports one format (in
179 // both Get and Set directions, but you may return false from GetDataHere() or
180 // SetData() if one of them is not supported). This is the simplest possible
181 // wxDataObject implementation.
183 // This is still an "abstract base class" (although it doesn't have any pure
184 // virtual functions), to use it you should derive from it and implement
185 // GetDataSize(), GetDataHere() and SetData() functions because the base class
186 // versions don't do anything - they just return "not implemented".
188 // This class should be used when you provide data in only one format (no
189 // conversion to/from other formats), either a standard or a custom one.
190 // Otherwise, you should use wxDataObjectComposite or wxDataObject directly.
191 // ----------------------------------------------------------------------------
193 class WXDLLEXPORT wxDataObjectSimple
: public wxDataObject
196 // ctor takes the format we support, but it can also be set later with
198 wxDataObjectSimple(const wxDataFormat
& format
= wxFormatInvalid
)
203 // get/set the format we support
204 const wxDataFormat
& GetFormat() const { return m_format
; }
205 void SetFormat(const wxDataFormat
& format
) { m_format
= format
; }
207 // virtual functions to override in derived class (the base class versions
208 // just return "not implemented")
209 // -----------------------------------------------------------------------
211 // get the size of our data
212 virtual size_t GetDataSize() const
215 // copy our data to the buffer
216 virtual bool GetDataHere(void *WXUNUSED(buf
)) const
219 // copy data from buffer to our data
220 virtual bool SetData(size_t WXUNUSED(len
), const void *WXUNUSED(buf
))
223 // implement base class pure virtuals
224 // ----------------------------------
225 virtual wxDataFormat
GetPreferredFormat(wxDataObjectBase::Direction
WXUNUSED(dir
) = Get
) const
227 virtual size_t GetFormatCount(wxDataObjectBase::Direction
WXUNUSED(dir
) = Get
) const
229 virtual void GetAllFormats(wxDataFormat
*formats
,
230 wxDataObjectBase::Direction
WXUNUSED(dir
) = Get
) const
231 { *formats
= m_format
; }
232 virtual size_t GetDataSize(const wxDataFormat
& WXUNUSED(format
)) const
233 { return GetDataSize(); }
234 virtual bool GetDataHere(const wxDataFormat
& WXUNUSED(format
),
236 { return GetDataHere(buf
); }
237 virtual bool SetData(const wxDataFormat
& WXUNUSED(format
),
238 size_t len
, const void *buf
)
239 { return SetData(len
, buf
); }
242 // the one and only format we support
243 wxDataFormat m_format
;
245 DECLARE_NO_COPY_CLASS(wxDataObjectSimple
)
248 // ----------------------------------------------------------------------------
249 // wxDataObjectComposite is the simplest way to implement wxDataObject
250 // supporting multiple formats. It contains several wxDataObjectSimple and
251 // supports all formats supported by any of them.
253 // This class shouldn't be (normally) derived from, but may be used directly.
254 // If you need more flexibility than what it provides, you should probably use
255 // wxDataObject directly.
256 // ----------------------------------------------------------------------------
258 WX_DECLARE_EXPORTED_LIST(wxDataObjectSimple
, wxSimpleDataObjectList
);
260 class WXDLLEXPORT wxDataObjectComposite
: public wxDataObject
264 wxDataObjectComposite();
265 virtual ~wxDataObjectComposite();
267 // add data object (it will be deleted by wxDataObjectComposite, hence it
268 // must be allocated on the heap) whose format will become the preferred
269 // one if preferred == true
270 void Add(wxDataObjectSimple
*dataObject
, bool preferred
= false);
272 // implement base class pure virtuals
273 // ----------------------------------
274 virtual wxDataFormat
GetPreferredFormat(wxDataObjectBase::Direction dir
= Get
) const;
275 virtual size_t GetFormatCount(wxDataObjectBase::Direction dir
= Get
) const;
276 virtual void GetAllFormats(wxDataFormat
*formats
, wxDataObjectBase::Direction dir
= Get
) const;
277 virtual size_t GetDataSize(const wxDataFormat
& format
) const;
278 virtual bool GetDataHere(const wxDataFormat
& format
, void *buf
) const;
279 virtual bool SetData(const wxDataFormat
& format
, size_t len
, const void *buf
);
282 // returns the pointer to the object which supports this format or NULL
283 wxDataObjectSimple
*GetObject(const wxDataFormat
& format
) const;
284 #if defined(__WXMSW__)
285 virtual const void* GetSizeFromBuffer( const void* buffer
, size_t* size
,
286 const wxDataFormat
& format
);
287 virtual void* SetSizeInBuffer( void* buffer
, size_t size
,
288 const wxDataFormat
& format
);
289 virtual size_t GetBufferOffset( const wxDataFormat
& format
);
293 // the list of all (simple) data objects whose formats we support
294 wxSimpleDataObjectList m_dataObjects
;
296 // the index of the preferred one (0 initially, so by default the first
297 // one is the preferred)
300 DECLARE_NO_COPY_CLASS(wxDataObjectComposite
)
303 // ============================================================================
304 // Standard implementations of wxDataObjectSimple which can be used directly
305 // (i.e. without having to derive from them) for standard data type transfers.
307 // Note that although all of them can work with provided data, you can also
308 // override their virtual GetXXX() functions to only provide data on demand.
309 // ============================================================================
311 // ----------------------------------------------------------------------------
312 // wxTextDataObject contains text data
313 // ----------------------------------------------------------------------------
315 class WXDLLEXPORT wxTextDataObject
: public wxDataObjectSimple
318 // ctor: you can specify the text here or in SetText(), or override
320 wxTextDataObject(const wxString
& text
= wxEmptyString
)
321 : wxDataObjectSimple(wxUSE_UNICODE
?wxDF_UNICODETEXT
:wxDF_TEXT
),
326 // virtual functions which you may override if you want to provide text on
327 // demand only - otherwise, the trivial default versions will be used
328 virtual size_t GetTextLength() const { return m_text
.Len() + 1; }
329 virtual wxString
GetText() const { return m_text
; }
330 virtual void SetText(const wxString
& text
) { m_text
= text
; }
332 // implement base class pure virtuals
333 // ----------------------------------
335 #if wxUSE_UNICODE && defined(__WXGTK20__)
336 virtual size_t GetFormatCount(Direction
WXUNUSED(dir
) = Get
) const { return 2; }
337 virtual void GetAllFormats(wxDataFormat
*formats
,
338 wxDataObjectBase::Direction
WXUNUSED(dir
) = Get
) const;
340 virtual size_t GetDataSize() const { return GetDataSize(GetPreferredFormat()); }
341 virtual bool GetDataHere(void *buf
) const { return GetDataHere(GetPreferredFormat(), buf
); }
342 virtual bool SetData(size_t len
, const void *buf
) { return SetData(GetPreferredFormat(), len
, buf
); }
344 size_t GetDataSize(const wxDataFormat
& format
) const;
345 bool GetDataHere(const wxDataFormat
& format
, void *pBuf
) const;
346 bool SetData(const wxDataFormat
& format
, size_t nLen
, const void* pBuf
);
347 #elif wxUSE_UNICODE && defined(__WXMAC__)
348 virtual size_t GetFormatCount(Direction
WXUNUSED(dir
) = Get
) const { return 2; }
349 virtual void GetAllFormats(wxDataFormat
*formats
,
350 wxDataObjectBase::Direction
WXUNUSED(dir
) = Get
) const;
352 virtual size_t GetDataSize() const { return GetDataSize(GetPreferredFormat()); }
353 virtual bool GetDataHere(void *buf
) const { return GetDataHere(GetPreferredFormat(), buf
); }
354 virtual bool SetData(size_t len
, const void *buf
) { return SetData(GetPreferredFormat(), len
, buf
); }
356 size_t GetDataSize(const wxDataFormat
& format
) const;
357 bool GetDataHere(const wxDataFormat
& format
, void *pBuf
) const;
358 bool SetData(const wxDataFormat
& format
, size_t nLen
, const void* pBuf
);
360 virtual size_t GetDataSize() const;
361 virtual bool GetDataHere(void *buf
) const;
362 virtual bool SetData(size_t len
, const void *buf
);
364 size_t GetDataSize(const wxDataFormat
& format
) const
365 { return(wxDataObjectSimple::GetDataSize(format
)); }
366 bool GetDataHere(const wxDataFormat
& format
, void *pBuf
) const
367 { return(wxDataObjectSimple::GetDataHere(format
, pBuf
)); }
368 bool SetData(const wxDataFormat
& format
, size_t nLen
, const void* pBuf
)
369 { return(wxDataObjectSimple::SetData(format
, nLen
, pBuf
)); }
375 DECLARE_NO_COPY_CLASS(wxTextDataObject
)
378 // ----------------------------------------------------------------------------
379 // wxBitmapDataObject contains a bitmap
380 // ----------------------------------------------------------------------------
382 class WXDLLEXPORT wxBitmapDataObjectBase
: public wxDataObjectSimple
385 // ctor: you can specify the bitmap here or in SetBitmap(), or override
387 wxBitmapDataObjectBase(const wxBitmap
& bitmap
= wxNullBitmap
)
388 : wxDataObjectSimple(wxDF_BITMAP
), m_bitmap(bitmap
)
392 // virtual functions which you may override if you want to provide data on
393 // demand only - otherwise, the trivial default versions will be used
394 virtual wxBitmap
GetBitmap() const { return m_bitmap
; }
395 virtual void SetBitmap(const wxBitmap
& bitmap
) { m_bitmap
= bitmap
; }
400 DECLARE_NO_COPY_CLASS(wxBitmapDataObjectBase
)
403 // ----------------------------------------------------------------------------
404 // wxFileDataObject contains a list of filenames
406 // NB: notice that this is a "write only" object, it can only be filled with
407 // data from drag and drop operation.
408 // ----------------------------------------------------------------------------
410 class WXDLLEXPORT wxFileDataObjectBase
: public wxDataObjectSimple
413 // ctor: use AddFile() later to fill the array
414 wxFileDataObjectBase() : wxDataObjectSimple(wxDF_FILENAME
) { }
416 // get a reference to our array
417 const wxArrayString
& GetFilenames() const { return m_filenames
; }
419 // the Get() functions do nothing for us
420 virtual size_t GetDataSize() const { return 0; }
421 virtual bool GetDataHere(void *WXUNUSED(buf
)) const { return false; }
424 wxArrayString m_filenames
;
427 // Virtual function hiding supression
428 size_t GetDataSize(const wxDataFormat
& format
) const
429 { return(wxDataObjectSimple::GetDataSize(format
)); }
430 bool GetDataHere(const wxDataFormat
& format
, void* pBuf
) const
431 { return(wxDataObjectSimple::GetDataHere(format
, pBuf
)); }
433 DECLARE_NO_COPY_CLASS(wxFileDataObjectBase
)
436 // ----------------------------------------------------------------------------
437 // wxCustomDataObject contains arbitrary untyped user data.
439 // It is understood that this data can be copied bitwise.
440 // ----------------------------------------------------------------------------
442 class WXDLLEXPORT wxCustomDataObject
: public wxDataObjectSimple
445 // if you don't specify the format in the ctor, you can still use
447 wxCustomDataObject(const wxDataFormat
& format
= wxFormatInvalid
);
449 // the dtor calls Free()
450 virtual ~wxCustomDataObject();
452 // you can call SetData() to set m_data: it will make a copy of the data
453 // you pass - or you can use TakeData() which won't copy anything, but
454 // will take ownership of data (i.e. will call Free() on it later)
455 void TakeData(size_t size
, void *data
);
457 // this function is called to allocate "size" bytes of memory from
458 // SetData(). The default version uses operator new[].
459 virtual void *Alloc(size_t size
);
461 // this function is called when the data is freed, you may override it to
462 // anything you want (or may be nothing at all). The default version calls
463 // operator delete[] on m_data
466 // get data: you may override these functions if you wish to provide data
467 // only when it's requested
468 virtual size_t GetSize() const { return m_size
; }
469 virtual void *GetData() const { return m_data
; }
471 // implement base class pure virtuals
472 // ----------------------------------
473 virtual size_t GetDataSize() const;
474 virtual bool GetDataHere(void *buf
) const;
475 virtual bool SetData(size_t size
, const void *buf
);
481 // virtual function hiding supression
482 size_t GetDataSize(const wxDataFormat
& format
) const
483 { return(wxDataObjectSimple::GetDataSize(format
)); }
484 bool GetDataHere(const wxDataFormat
& format
, void* pBuf
) const
485 { return(wxDataObjectSimple::GetDataHere(format
, pBuf
)); }
486 bool SetData(const wxDataFormat
& format
, size_t nLen
, const void* pBuf
)
487 { return(wxDataObjectSimple::SetData(format
, nLen
, pBuf
)); }
489 DECLARE_NO_COPY_CLASS(wxCustomDataObject
)
492 // ----------------------------------------------------------------------------
493 // include platform-specific declarations of wxXXXBase classes
494 // ----------------------------------------------------------------------------
496 #if defined(__WXMSW__)
497 #include "wx/msw/ole/dataobj2.h"
499 // wxURLDataObject defined in msw/ole/dataobj2.h
501 #if defined(__WXGTK__)
502 #include "wx/gtk/dataobj2.h"
503 #elif defined(__WXX11__)
504 #include "wx/x11/dataobj2.h"
505 #elif defined(__WXMOTIF__)
506 #include "wx/motif/dataobj2.h"
507 #elif defined(__WXMAC__)
508 #include "wx/mac/dataobj2.h"
509 #elif defined(__WXCOCOA__)
510 #include "wx/cocoa/dataobj2.h"
511 #elif defined(__WXPM__)
512 #include "wx/os2/dataobj2.h"
515 // wxURLDataObject is simply wxTextDataObject with a different name
516 class WXDLLEXPORT wxURLDataObject
: public wxTextDataObject
519 wxString
GetURL() const { return GetText(); }
520 void SetURL(const wxString
& url
) { SetText(url
); }
522 #endif // __WXMSW__/!__WXMSW__
524 #endif // wxUSE_DATAOBJ
526 #endif // _WX_DATAOBJ_H_BASE_