1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: common data object classes
4 // Author: Vadim Zeitlin, Robert Roebling
8 // Copyright: (c) wxWindows Team
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_DATAOBJ_H_BASE_
13 #define _WX_DATAOBJ_H_BASE_
16 #pragma interface "dataobjbase.h"
19 // ----------------------------------------------------------------------------
21 // ----------------------------------------------------------------------------
24 #include "wx/string.h"
25 #include "wx/bitmap.h"
28 // ============================================================================
30 Generic data transfer related classes. The class hierarchy is as follows:
35 wxDataObjectSimple wxDataObjectComposite
38 wxTextDataObject | wxBitmapDataObject
43 // ============================================================================
45 // ----------------------------------------------------------------------------
46 // wxDataFormat class is declared in platform-specific headers: it represents
47 // a format for data which may be either one of the standard ones (text,
48 // bitmap, ...) or a custom one which is then identified by a unique string.
49 // ----------------------------------------------------------------------------
51 /* the class interface looks like this (pseudo code):
56 typedef <integral type> NativeFormat;
58 wxDataFormat(NativeFormat format = wxDF_INVALID);
59 wxDataFormat(const wxChar *format);
61 wxDataFormat& operator=(NativeFormat format);
62 wxDataFormat& operator=(const wxDataFormat& format);
64 bool operator==(NativeFormat format) const;
65 bool operator!=(NativeFormat format) const;
67 void SetType(NativeFormat format);
68 NativeFormat GetType() const;
70 wxString GetId() const;
71 void SetId(const wxChar *format);
76 #if defined(__WXMSW__)
77 #include "wx/msw/ole/dataform.h"
78 #elif defined(__WXMOTIF__)
79 #include "wx/motif/dataform.h"
80 #elif defined(__WXGTK__)
81 #include "wx/gtk/dataform.h"
82 #elif defined(__WXMAC__)
83 #include "wx/mac/dataform.h"
84 #elif defined(__WXPM__)
85 #include "wx/os2/dataform.h"
88 // the value for default argument to some functions (corresponds to
90 extern const wxDataFormat
& wxFormatInvalid
;
92 // ----------------------------------------------------------------------------
93 // wxDataObject represents a piece of data which knows which formats it
94 // supports and knows how to render itself in each of them - GetDataHere(),
95 // and how to restore data from the buffer (SetData()).
97 // Although this class may be used directly (i.e. custom classes may be
98 // derived from it), in many cases it might be simpler to use either
99 // wxDataObjectSimple or wxDataObjectComposite classes.
101 // A data object may be "read only", i.e. support only GetData() functions or
102 // "read-write", i.e. support both GetData() and SetData() (in principle, it
103 // might be "write only" too, but this is rare). Moreover, it doesn't have to
104 // support the same formats in Get() and Set() directions: for example, a data
105 // object containing JPEG image might accept BMPs in GetData() because JPEG
106 // image may be easily transformed into BMP but not in SetData(). Accordingly,
107 // all methods dealing with formats take an additional "direction" argument
108 // which is either SET or GET and which tells the function if the format needs
109 // to be supported by SetData() or GetDataHere().
110 // ----------------------------------------------------------------------------
112 class WXDLLEXPORT wxDataObjectBase
117 Get
= 0x01, // format is supported by GetDataHere()
118 Set
= 0x02, // format is supported by SetData()
119 Both
= 0x03 // format is supported by both (unused currently)
122 // this class is polymorphic, hence it needs a virtual dtor
123 virtual ~wxDataObjectBase();
125 // get the best suited format for rendering our data
126 virtual wxDataFormat
GetPreferredFormat(Direction dir
= Get
) const = 0;
128 // get the number of formats we support
129 virtual size_t GetFormatCount(Direction dir
= Get
) const = 0;
131 // return all formats in the provided array (of size GetFormatCount())
132 virtual void GetAllFormats(wxDataFormat
*formats
,
133 Direction dir
= Get
) const = 0;
135 // get the (total) size of data for the given format
136 virtual size_t GetDataSize(const wxDataFormat
& format
) const = 0;
138 // copy raw data (in the specified format) to the provided buffer, return
139 // TRUE if data copied successfully, FALSE otherwise
140 virtual bool GetDataHere(const wxDataFormat
& format
, void *buf
) const = 0;
142 // get data from the buffer of specified length (in the given format),
143 // return TRUE if the data was read successfully, FALSE otherwise
144 virtual bool SetData(const wxDataFormat
& WXUNUSED(format
),
145 size_t WXUNUSED(len
), const void * WXUNUSED(buf
))
151 // ----------------------------------------------------------------------------
152 // include the platform-specific declarations of wxDataObject
153 // ----------------------------------------------------------------------------
155 #if defined(__WXMSW__)
156 #include "wx/msw/ole/dataobj.h"
157 #elif defined(__WXMOTIF__)
158 #include "wx/motif/dataobj.h"
159 #elif defined(__WXGTK__)
160 #include "wx/gtk/dataobj.h"
161 #elif defined(__WXQT__)
162 #include "wx/qt/dnd.h"
163 #elif defined(__WXMAC__)
164 #include "wx/mac/dataobj.h"
165 #elif defined(__WXPM__)
166 #include "wx/os2/dataobj.h"
167 #elif defined(__WXSTUBS__)
168 #include "wx/stubs/dnd.h"
171 // ----------------------------------------------------------------------------
172 // wxDataObjectSimple is a wxDataObject which only supports one format (in
173 // both Get and Set directions, but you may return FALSE from GetDataHere() or
174 // SetData() if one of them is not supported). This is the simplest possible
175 // wxDataObject implementation.
177 // This is still an "abstract base class" (although it doesn't have any pure
178 // virtual functions), to use it you should derive from it and implement
179 // GetDataSize(), GetDataHere() and SetData() functions because the base class
180 // versions don't do anything - they just return "not implemented".
182 // This class should be used when you provide data in only one format (no
183 // conversion to/from other formats), either a standard or a custom one.
184 // Otherwise, you should use wxDataObjectComposite or wxDataObject directly.
185 // ----------------------------------------------------------------------------
187 class WXDLLEXPORT wxDataObjectSimple
: public wxDataObject
190 // ctor takes the format we support, but it can also be set later with
192 wxDataObjectSimple(const wxDataFormat
& format
= wxFormatInvalid
)
197 // get/set the format we support
198 const wxDataFormat
& GetFormat() const { return m_format
; }
199 void SetFormat(const wxDataFormat
& format
) { m_format
= format
; }
201 // virtual functions to override in derived class (the base class versions
202 // just return "not implemented")
203 // -----------------------------------------------------------------------
205 // get the size of our data
206 virtual size_t GetDataSize() const
209 // copy our data to the buffer
210 virtual bool GetDataHere(void *WXUNUSED(buf
)) const
213 // copy data from buffer to our data
214 virtual bool SetData(size_t WXUNUSED(len
), const void *WXUNUSED(buf
))
217 // implement base class pure virtuals
218 // ----------------------------------
219 virtual wxDataFormat
GetPreferredFormat(wxDataObjectBase::Direction
WXUNUSED(dir
) = Get
) const
221 virtual size_t GetFormatCount(wxDataObjectBase::Direction
WXUNUSED(dir
) = Get
) const
223 virtual void GetAllFormats(wxDataFormat
*formats
,
224 wxDataObjectBase::Direction
WXUNUSED(dir
) = Get
) const
225 { *formats
= m_format
; }
226 virtual size_t GetDataSize(const wxDataFormat
& WXUNUSED(format
)) const
227 { return GetDataSize(); }
228 virtual bool GetDataHere(const wxDataFormat
& WXUNUSED(format
),
230 { return GetDataHere(buf
); }
231 virtual bool SetData(const wxDataFormat
& WXUNUSED(format
),
232 size_t len
, const void *buf
)
233 { return SetData(len
, buf
); }
236 // the one and only format we support
237 wxDataFormat m_format
;
240 // ----------------------------------------------------------------------------
241 // wxDataObjectComposite is the simplest way to implement wxDataObject
242 // supporting multiple formats. It contains several wxDataObjectSimple and
243 // supports all formats supported by any of them.
245 // This class shouldn't be (normally) derived from, but may be used directly.
246 // If you need more flexibility than what it provides, you should probably use
247 // wxDataObject directly.
248 // ----------------------------------------------------------------------------
250 WX_DECLARE_LIST(wxDataObjectSimple
, wxSimpleDataObjectList
);
252 class WXDLLEXPORT wxDataObjectComposite
: public wxDataObject
256 wxDataObjectComposite() { m_preferred
= 0; }
258 // add data object (it will be deleted by wxDataObjectComposite, hence it
259 // must be allocated on the heap) whose format will become the preferred
260 // one if preferred == TRUE
261 void Add(wxDataObjectSimple
*dataObject
, bool preferred
= FALSE
);
263 // implement base class pure virtuals
264 // ----------------------------------
265 virtual wxDataFormat
GetPreferredFormat(wxDataObjectBase::Direction dir
= Get
) const;
266 virtual size_t GetFormatCount(wxDataObjectBase::Direction dir
= Get
) const;
267 virtual void GetAllFormats(wxDataFormat
*formats
, wxDataObjectBase::Direction dir
= Get
) const;
268 virtual size_t GetDataSize(const wxDataFormat
& format
) const;
269 virtual bool GetDataHere(const wxDataFormat
& format
, void *buf
) const;
270 virtual bool SetData(const wxDataFormat
& format
, size_t len
, const void *buf
);
273 // returns the pointer to the object which supports this format or NULL
274 wxDataObjectSimple
*GetObject(const wxDataFormat
& format
) const;
277 // the list of all (simple) data objects whose formats we support
278 wxSimpleDataObjectList m_dataObjects
;
280 // the index of the preferred one (0 initially, so by default the first
281 // one is the preferred)
285 // ============================================================================
286 // Standard implementations of wxDataObjectSimple which can be used directly
287 // (i.e. without having to derive from them) for standard data type transfers.
289 // Note that although all of them can work with provided data, you can also
290 // override their virtual GetXXX() functions to only provide data on demand.
291 // ============================================================================
293 // ----------------------------------------------------------------------------
294 // wxTextDataObject contains text data
295 // ----------------------------------------------------------------------------
297 class WXDLLEXPORT wxTextDataObject
: public wxDataObjectSimple
300 // ctor: you can specify the text here or in SetText(), or override
302 wxTextDataObject(const wxString
& text
= wxEmptyString
)
303 : wxDataObjectSimple(wxDF_TEXT
), m_text(text
)
307 // virtual functions which you may override if you want to provide text on
308 // demand only - otherwise, the trivial default versions will be used
309 virtual size_t GetTextLength() const { return m_text
.Len() + 1; }
310 virtual wxString
GetText() const { return m_text
; }
311 virtual void SetText(const wxString
& text
) { m_text
= text
; }
313 // implement base class pure virtuals
314 // ----------------------------------
315 virtual size_t GetDataSize() const;
316 virtual bool GetDataHere(void *buf
) const;
317 virtual bool SetData(size_t len
, const void *buf
);
322 // virtual function hiding supression
323 size_t GetDataSize(const wxDataFormat
& format
) const
324 { return(wxDataObjectSimple::GetDataSize(format
)); }
325 bool GetDataHere(const wxDataFormat
& format
, void *pBuf
) const
326 { return(wxDataObjectSimple::GetDataHere(format
, pBuf
)); }
327 bool SetData(const wxDataFormat
& format
, size_t nLen
, const void* pBuf
)
328 { return(wxDataObjectSimple::SetData(format
, nLen
, pBuf
)); }
331 // ----------------------------------------------------------------------------
332 // wxBitmapDataObject contains a bitmap
333 // ----------------------------------------------------------------------------
335 class WXDLLEXPORT wxBitmapDataObjectBase
: public wxDataObjectSimple
338 // ctor: you can specify the bitmap here or in SetBitmap(), or override
340 wxBitmapDataObjectBase(const wxBitmap
& bitmap
= wxNullBitmap
)
341 : wxDataObjectSimple(wxDF_BITMAP
), m_bitmap(bitmap
)
345 // virtual functions which you may override if you want to provide data on
346 // demand only - otherwise, the trivial default versions will be used
347 virtual wxBitmap
GetBitmap() const { return m_bitmap
; }
348 virtual void SetBitmap(const wxBitmap
& bitmap
) { m_bitmap
= bitmap
; }
354 // ----------------------------------------------------------------------------
355 // wxFileDataObject contains a list of filenames
357 // NB: notice that this is a "write only" object, it can only be filled with
358 // data from drag and drop operation.
359 // ----------------------------------------------------------------------------
361 class WXDLLEXPORT wxFileDataObjectBase
: public wxDataObjectSimple
364 // ctor: use AddFile() later to fill the array
365 wxFileDataObjectBase() : wxDataObjectSimple(wxDF_FILENAME
) { }
367 // get a reference to our array
368 const wxArrayString
& GetFilenames() const { return m_filenames
; }
370 // the Get() functions do nothing for us
371 virtual size_t GetDataSize() const { return 0; }
372 virtual bool GetDataHere(void *WXUNUSED(buf
)) const { return FALSE
; }
375 wxArrayString m_filenames
;
378 // Virtual function hiding supression
379 size_t GetDataSize(const wxDataFormat
& format
) const
380 { return(wxDataObjectSimple::GetDataSize(format
)); }
381 bool GetDataHere(const wxDataFormat
& format
, void* pBuf
) const
382 { return(wxDataObjectSimple::GetDataHere(format
, pBuf
)); }
385 // ----------------------------------------------------------------------------
386 // wxCustomDataObject contains arbitrary untyped user data.
388 // It is understood that this data can be copied bitwise.
389 // ----------------------------------------------------------------------------
391 class WXDLLEXPORT wxCustomDataObject
: public wxDataObjectSimple
394 // if you don't specify the format in the ctor, you can still use
396 wxCustomDataObject(const wxDataFormat
& format
= wxFormatInvalid
);
398 // the dtor calls Free()
399 virtual ~wxCustomDataObject();
401 // you can call SetData() to set m_data: it will make a copy of the data
402 // you pass - or you can use TakeData() which won't copy anything, but
403 // will take ownership of data (i.e. will call Free() on it later)
404 void TakeData(size_t size
, void *data
);
406 // this function is called to allocate "size" bytes of memory from
407 // SetData(). The default version uses operator new[].
408 virtual void *Alloc(size_t size
);
410 // this function is called when the data is freed, you may override it to
411 // anything you want (or may be nothing at all). The default version calls
412 // operator delete[] on m_data
415 // get data: you may override these functions if you wish to provide data
416 // only when it's requested
417 virtual size_t GetSize() const { return m_size
; }
418 virtual void *GetData() const { return m_data
; }
420 // implement base class pure virtuals
421 // ----------------------------------
422 virtual size_t GetDataSize() const;
423 virtual bool GetDataHere(void *buf
) const;
424 virtual bool SetData(size_t size
, const void *buf
);
430 // virtual function hiding supression
431 size_t GetDataSize(const wxDataFormat
& format
) const
432 { return(wxDataObjectSimple::GetDataSize(format
)); }
433 bool GetDataHere(const wxDataFormat
& format
, void* pBuf
) const
434 { return(wxDataObjectSimple::GetDataHere(format
, pBuf
)); }
435 bool SetData(const wxDataFormat
& format
, size_t nLen
, const void* pBuf
)
436 { return(wxDataObjectSimple::SetData(format
, nLen
, pBuf
)); }
439 // ----------------------------------------------------------------------------
440 // include platform-specific declarations of wxXXXBase classes
441 // ----------------------------------------------------------------------------
443 #if defined(__WXMSW__)
444 #include "wx/msw/ole/dataobj2.h"
445 #elif defined(__WXMOTIF__)
446 // #include "wx/motif/dataobj2.h" -- not yet
447 #elif defined(__WXGTK__)
448 #include "wx/gtk/dataobj2.h"
449 #elif defined(__WXPM__)
450 #include "wx/os2/dataobj2.h"
453 #endif // _WX_DATAOBJ_H_BASE_