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(__WXPM__)
83 #include "wx/os2/dataform.h"
86 // the value for default argument to some functions (corresponds to
88 extern const wxDataFormat
& wxFormatInvalid
;
90 // ----------------------------------------------------------------------------
91 // wxDataObject represents a piece of data which knows which formats it
92 // supports and knows how to render itself in each of them - GetDataHere(),
93 // and how to restore data from the buffer (SetData()).
95 // Although this class may be used directly (i.e. custom classes may be
96 // derived from it), in many cases it might be simpler to use either
97 // wxDataObjectSimple or wxDataObjectComposite classes.
99 // A data object may be "read only", i.e. support only GetData() functions or
100 // "read-write", i.e. support both GetData() and SetData() (in principle, it
101 // might be "write only" too, but this is rare). Moreover, it doesn't have to
102 // support the same formats in Get() and Set() directions: for example, a data
103 // object containing JPEG image might accept BMPs in GetData() because JPEG
104 // image may be easily transformed into BMP but not in SetData(). Accordingly,
105 // all methods dealing with formats take an additional "direction" argument
106 // which is either SET or GET and which tells the function if the format needs
107 // to be supported by SetData() or GetDataHere().
108 // ----------------------------------------------------------------------------
110 class WXDLLEXPORT wxDataObjectBase
115 Get
= 0x01, // format is supported by GetDataHere()
116 Set
= 0x02, // format is supported by SetData()
117 Both
= 0x03 // format is supported by both (unused currently)
120 // this class is polymorphic, hence it needs a virtual dtor
121 virtual ~wxDataObjectBase();
123 // get the best suited format for rendering our data
124 virtual wxDataFormat
GetPreferredFormat(Direction dir
= Get
) const = 0;
126 // get the number of formats we support
127 virtual size_t GetFormatCount(Direction dir
= Get
) const = 0;
129 // return all formats in the provided array (of size GetFormatCount())
130 virtual void GetAllFormats(wxDataFormat
*formats
,
131 Direction dir
= Get
) const = 0;
133 // get the (total) size of data for the given format
134 virtual size_t GetDataSize(const wxDataFormat
& format
) const = 0;
136 // copy raw data (in the specified format) to the provided buffer, return
137 // TRUE if data copied successfully, FALSE otherwise
138 virtual bool GetDataHere(const wxDataFormat
& format
, void *buf
) const = 0;
140 // get data from the buffer of specified length (in the given format),
141 // return TRUE if the data was read successfully, FALSE otherwise
142 virtual bool SetData(const wxDataFormat
& WXUNUSED(format
),
143 size_t WXUNUSED(len
), const void * WXUNUSED(buf
))
149 // ----------------------------------------------------------------------------
150 // include the platform-specific declarations of wxDataObject
151 // ----------------------------------------------------------------------------
153 #if defined(__WXMSW__)
154 #include "wx/msw/ole/dataobj.h"
155 #elif defined(__WXMOTIF__)
156 #include "wx/motif/dataobj.h"
157 #elif defined(__WXGTK__)
158 #include "wx/gtk/dataobj.h"
159 #elif defined(__WXQT__)
160 #include "wx/qt/dnd.h"
161 #elif defined(__WXMAC__)
162 #include "wx/mac/dnd.h"
163 #elif defined(__WXPM__)
164 #include "wx/os2/dataobj.h"
165 #elif defined(__WXSTUBS__)
166 #include "wx/stubs/dnd.h"
169 // ----------------------------------------------------------------------------
170 // wxDataObjectSimple is a wxDataObject which only supports one format (in
171 // both Get and Set directions, but you may return FALSE from GetDataHere() or
172 // SetData() if one of them is not supported). This is the simplest possible
173 // wxDataObject implementation.
175 // This is still an "abstract base class" (although it doesn't have any pure
176 // virtual functions), to use it you should derive from it and implement
177 // GetDataSize(), GetDataHere() and SetData() functions because the base class
178 // versions don't do anything - they just return "not implemented".
180 // This class should be used when you provide data in only one format (no
181 // conversion to/from other formats), either a standard or a custom one.
182 // Otherwise, you should use wxDataObjectComposite or wxDataObject directly.
183 // ----------------------------------------------------------------------------
185 class WXDLLEXPORT wxDataObjectSimple
: public wxDataObject
188 // ctor takes the format we support, but it can also be set later with
190 wxDataObjectSimple(const wxDataFormat
& format
= wxFormatInvalid
)
195 // get/set the format we support
196 const wxDataFormat
& GetFormat() const { return m_format
; }
197 void SetFormat(const wxDataFormat
& format
) { m_format
= format
; }
199 // virtual functions to override in derived class (the base class versions
200 // just return "not implemented")
201 // -----------------------------------------------------------------------
203 // get the size of our data
204 virtual size_t GetDataSize() const
207 // copy our data to the buffer
208 virtual bool GetDataHere(void *WXUNUSED(buf
)) const
211 // copy data from buffer to our data
212 virtual bool SetData(size_t WXUNUSED(len
), const void *WXUNUSED(buf
))
215 // implement base class pure virtuals
216 // ----------------------------------
217 virtual wxDataFormat
GetPreferredFormat(wxDataObjectBase::Direction
WXUNUSED(dir
) = Get
) const
219 virtual size_t GetFormatCount(wxDataObjectBase::Direction
WXUNUSED(dir
) = Get
) const
221 virtual void GetAllFormats(wxDataFormat
*formats
,
222 wxDataObjectBase::Direction
WXUNUSED(dir
) = Get
) const
223 { *formats
= m_format
; }
224 virtual size_t GetDataSize(const wxDataFormat
& WXUNUSED(format
)) const
225 { return GetDataSize(); }
226 virtual bool GetDataHere(const wxDataFormat
& WXUNUSED(format
),
228 { return GetDataHere(buf
); }
229 virtual bool SetData(const wxDataFormat
& WXUNUSED(format
),
230 size_t len
, const void *buf
)
231 { return SetData(len
, buf
); }
234 // the one and only format we support
235 wxDataFormat m_format
;
238 // ----------------------------------------------------------------------------
239 // wxDataObjectComposite is the simplest way to implement wxDataObject
240 // supporting multiple formats. It contains several wxDataObjectSimple and
241 // supports all formats supported by any of them.
243 // This class shouldn't be (normally) derived from, but may be used directly.
244 // If you need more flexibility than what it provides, you should probably use
245 // wxDataObject directly.
246 // ----------------------------------------------------------------------------
248 WX_DECLARE_LIST(wxDataObjectSimple
, wxSimpleDataObjectList
);
250 class WXDLLEXPORT wxDataObjectComposite
: public wxDataObject
254 wxDataObjectComposite() { m_preferred
= 0; }
256 // add data object (it will be deleted by wxDataObjectComposite, hence it
257 // must be allocated on the heap) whose format will become the preferred
258 // one if preferred == TRUE
259 void Add(wxDataObjectSimple
*dataObject
, bool preferred
= FALSE
);
261 // implement base class pure virtuals
262 // ----------------------------------
263 virtual wxDataFormat
GetPreferredFormat(wxDataObjectBase::Direction dir
= Get
) const;
264 virtual size_t GetFormatCount(wxDataObjectBase::Direction dir
= Get
) const;
265 virtual void GetAllFormats(wxDataFormat
*formats
, wxDataObjectBase::Direction dir
= Get
) const;
266 virtual size_t GetDataSize(const wxDataFormat
& format
) const;
267 virtual bool GetDataHere(const wxDataFormat
& format
, void *buf
) const;
268 virtual bool SetData(const wxDataFormat
& format
, size_t len
, const void *buf
);
271 // returns the pointer to the object which supports this format or NULL
272 wxDataObjectSimple
*GetObject(const wxDataFormat
& format
) const;
275 // the list of all (simple) data objects whose formats we support
276 wxSimpleDataObjectList m_dataObjects
;
278 // the index of the preferred one (0 initially, so by default the first
279 // one is the preferred)
283 // ============================================================================
284 // Standard implementations of wxDataObjectSimple which can be used directly
285 // (i.e. without having to derive from them) for standard data type transfers.
287 // Note that although all of them can work with provided data, you can also
288 // override their virtual GetXXX() functions to only provide data on demand.
289 // ============================================================================
291 // ----------------------------------------------------------------------------
292 // wxTextDataObject contains text data
293 // ----------------------------------------------------------------------------
295 class WXDLLEXPORT wxTextDataObject
: public wxDataObjectSimple
298 // ctor: you can specify the text here or in SetText(), or override
300 wxTextDataObject(const wxString
& text
= wxEmptyString
)
301 : wxDataObjectSimple(wxDF_TEXT
), m_text(text
)
305 // virtual functions which you may override if you want to provide text on
306 // demand only - otherwise, the trivial default versions will be used
307 virtual size_t GetTextLength() const { return m_text
.Len() + 1; }
308 virtual wxString
GetText() const { return m_text
; }
309 virtual void SetText(const wxString
& text
) { m_text
= text
; }
311 // implement base class pure virtuals
312 // ----------------------------------
313 virtual size_t GetDataSize() const;
314 virtual bool GetDataHere(void *buf
) const;
315 virtual bool SetData(size_t len
, const void *buf
);
320 #if defined(__VISAGECPP__)
321 // Virtual function hiding supression
322 size_t GetDataSize(const wxDataFormat
& rFormat
) const
323 { return(wxDataObjectSimple::GetDataSize(rFormat
)); }
324 bool GetDataHere(const wxDataFormat
& WXUNUSED(rFormat
), void *pBuf
) const
325 { return(GetDataHere(pBuf
)); }
326 bool SetData(const wxDataFormat
& rFormat
, size_t nLen
, const void* pBuf
)
327 { return(wxDataObjectSimple::SetData(rFormat
, 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
;
377 #if defined(__VISAGECPP__)
379 // Virtual function hiding supression
380 size_t GetDataSize(const wxDataFormat
& rFormat
) const
381 { return(wxDataObjectSimple::GetDataSize(rFormat
)); }
382 bool GetDataHere(const wxDataFormat
& WXUNUSED(rformat
), void* pBuf
) const
383 { return(GetDataHere(pBuf
)); }
387 // ----------------------------------------------------------------------------
388 // wxCustomDataObject contains arbitrary untyped user data.
390 // It is understood that this data can be copied bitwise.
391 // ----------------------------------------------------------------------------
393 class WXDLLEXPORT wxCustomDataObject
: public wxDataObjectSimple
396 // if you don't specify the format in the ctor, you can still use
398 wxCustomDataObject(const wxDataFormat
& format
= wxFormatInvalid
);
400 // the dtor calls Free()
401 virtual ~wxCustomDataObject();
403 // you can call SetData() to set m_data: it will make a copy of the data
404 // you pass - or you can use TakeData() which won't copy anything, but
405 // will take ownership of data (i.e. will call Free() on it later)
406 void TakeData(size_t size
, void *data
);
408 // this function is called to allocate "size" bytes of memory from
409 // SetData(). The default version uses operator new[].
410 virtual void *Alloc(size_t size
);
412 // this function is called when the data is freed, you may override it to
413 // anything you want (or may be nothing at all). The default version calls
414 // operator delete[] on m_data
417 // get data: you may override these functions if you wish to provide data
418 // only when it's requested
419 virtual size_t GetSize() const { return m_size
; }
420 virtual void *GetData() const { return m_data
; }
422 // implement base class pure virtuals
423 // ----------------------------------
424 virtual size_t GetDataSize() const;
425 virtual bool GetDataHere(void *buf
) const;
426 virtual bool SetData(size_t size
, const void *buf
);
432 #if defined(__VISAGECPP__)
433 // Virtual function hiding supression
434 size_t GetDataSize(const wxDataFormat
& rFormat
) const
435 { return(wxDataObjectSimple::GetDataSize(rFormat
)); }
436 bool GetDataHere(const wxDataFormat
& rFormat
, void* pBuf
) const
437 { return(wxDataObjectSimple::GetDataHere(rFormat
, pBuf
)); }
438 bool SetData(const wxDataFormat
& rFormat
, size_t nLen
, const void* pBuf
)
439 { return(wxDataObjectSimple::SetData(rFormat
, nLen
, pBuf
)); }
443 // ----------------------------------------------------------------------------
444 // include platform-specific declarations of wxXXXBase classes
445 // ----------------------------------------------------------------------------
447 #if defined(__WXMSW__)
448 #include "wx/msw/ole/dataobj2.h"
449 #elif defined(__WXMOTIF__)
450 // #include "wx/motif/dataobj2.h" -- not yet
451 #elif defined(__WXGTK__)
452 #include "wx/gtk/dataobj2.h"
453 #elif defined(__WXPM__)
454 #include "wx/os2/dataobj2.h"
457 #endif // _WX_DATAOBJ_H_BASE_