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"
84 // ----------------------------------------------------------------------------
85 // wxDataObject represents a piece of data which knows which formats it
86 // supports and knows how to render itself in each of them - GetDataHere(),
87 // and how to restore data from the buffer (SetData()).
89 // Although this class may be used directly (i.e. custom classes may be
90 // derived from it), in many cases it might be simpler to use either
91 // wxDataObjectSimple or wxDataObjectComposite classes.
93 // A data object may be "read only", i.e. support only GetData() functions or
94 // "read-write", i.e. support both GetData() and SetData() (in principle, it
95 // might be "write only" too, but this is rare). Moreover, it doesn't have to
96 // support the same formats in Get() and Set() directions: for example, a data
97 // object containing JPEG image might accept BMPs in GetData() because JPEG
98 // image may be easily transformed into BMP but not in SetData(). Accordingly,
99 // all methods dealing with formats take an additional "direction" argument
100 // which is either SET or GET and which tells the function if the format needs
101 // to be supported by SetData() or GetDataHere().
102 // ----------------------------------------------------------------------------
104 class WXDLLEXPORT wxDataObjectBase
109 Get
= 0x01, // format is supported by GetDataHere()
110 Set
= 0x02, // format is supported by SetData()
111 Both
= 0x03 // format is supported by both (unused currently)
114 // this class is polymorphic, hence it needs a virtual dtor
115 virtual ~wxDataObjectBase();
117 // get the best suited format for rendering our data
118 virtual wxDataFormat
GetPreferredFormat(Direction dir
= Get
) const = 0;
120 // get the number of formats we support
121 virtual size_t GetFormatCount(Direction dir
= Get
) const = 0;
123 // return all formats in the provided array (of size GetFormatCount())
124 virtual void GetAllFormats(wxDataFormat
*formats
,
125 Direction dir
= Get
) const = 0;
127 // get the (total) size of data for the given format
128 virtual size_t GetDataSize(const wxDataFormat
& format
) const = 0;
130 // copy raw data (in the specified format) to the provided buffer, return
131 // TRUE if data copied successfully, FALSE otherwise
132 virtual bool GetDataHere(const wxDataFormat
& format
, void *buf
) const = 0;
134 // get data from the buffer of specified length (in the given format),
135 // return TRUE if the data was read successfully, FALSE otherwise
136 virtual bool SetData(const wxDataFormat
& format
,
137 size_t len
, const void *buf
) = 0;
140 // ----------------------------------------------------------------------------
141 // include the platform-specific declarations of wxDataObject
142 // ----------------------------------------------------------------------------
144 #if defined(__WXMSW__)
145 #include "wx/msw/ole/dataobj.h"
146 #elif defined(__WXMOTIF__)
147 #include "wx/motif/dataobj.h"
148 #elif defined(__WXGTK__)
149 #include "wx/gtk/dataobj.h"
150 #elif defined(__WXQT__)
151 #include "wx/qt/dnd.h"
152 #elif defined(__WXMAC__)
153 #include "wx/mac/dnd.h"
154 #elif defined(__WXPM__)
155 #include "wx/os2/dnd.h"
156 #elif defined(__WXSTUBS__)
157 #include "wx/stubs/dnd.h"
160 // ----------------------------------------------------------------------------
161 // wxDataObjectSimple is a wxDataObject which only supports one format (in
162 // both Get and Set directions, but you may return FALSE from GetDataHere() or
163 // SetData() if one of them is not supported). This is the simplest possible
164 // wxDataObject implementation.
166 // This is still an "abstract base class" (although it doesn't have any pure
167 // virtual functions), to use it you should derive from it and implement
168 // GetDataSize(), GetDataHere() and SetData() functions because the base class
169 // versions don't do anything - they just return "not implemented".
171 // This class should be used when you provide data in only one format (no
172 // conversion to/from other formats), either a standard or a custom one.
173 // Otherwise, you should use wxDataObjectComposite or wxDataObject directly.
174 // ----------------------------------------------------------------------------
176 class WXDLLEXPORT wxDataObjectSimple
: public wxDataObject
179 // ctor takes the format we support, but it can also be set later with
181 wxDataObjectSimple(const wxDataFormat
& format
= wxDF_INVALID
)
186 // get/set the format we support
187 const wxDataFormat
& GetFormat() const { return m_format
; }
188 void SetFormat(const wxDataFormat
& format
) { m_format
= format
; }
190 // virtual functions to override in derived class (the base class versions
191 // just return "not implemented")
192 // -----------------------------------------------------------------------
194 // get the size of our data
195 virtual size_t GetDataSize() const
198 // copy our data to the buffer
199 virtual bool GetDataHere(void *WXUNUSED(buf
)) const
202 // copy data from buffer to our data
203 virtual bool SetData(size_t len
, const void *WXUNUSED(buf
))
206 // implement base class pure virtuals
207 // ----------------------------------
208 virtual wxDataFormat
GetPreferredFormat(Direction
WXUNUSED(dir
) = Get
) const
210 virtual size_t GetFormatCount(Direction
WXUNUSED(dir
) = Get
) const
212 virtual void GetAllFormats(wxDataFormat
*formats
,
213 Direction
WXUNUSED(dir
) = Get
) const
214 { *formats
= m_format
; }
215 virtual size_t GetDataSize(const wxDataFormat
& WXUNUSED(format
)) const
216 { return GetDataSize(); }
217 virtual bool GetDataHere(const wxDataFormat
& WXUNUSED(format
),
219 { return GetDataHere(buf
); }
220 virtual bool SetData(const wxDataFormat
& WXUNUSED(format
),
221 size_t len
, const void *buf
)
222 { return SetData(len
, buf
); }
225 // the one and only format we support
226 wxDataFormat m_format
;
229 // ----------------------------------------------------------------------------
230 // wxDataObjectComposite is the simplest way to implement wxDataObject
231 // supporting multiple formats. It contains several wxDataObjectSimple and
232 // supports all formats supported by any of them.
234 // This class shouldn't be (normally) derived from, but may be used directly.
235 // If you need more flexibility than what it provides, you should probably use
236 // wxDataObject directly.
237 // ----------------------------------------------------------------------------
239 WX_DECLARE_LIST(wxDataObjectSimple
, wxSimpleDataObjectList
);
241 class WXDLLEXPORT wxDataObjectComposite
: public wxDataObject
245 wxDataObjectComposite() { m_preferred
= 0; }
247 // add data object (it will be deleted by wxDataObjectComposite, hence it
248 // must be allocated on the heap) whose format will become the preferred
249 // one if preferred == TRUE
250 void Add(wxDataObjectSimple
*dataObject
, bool preferred
= FALSE
);
252 // implement base class pure virtuals
253 // ----------------------------------
254 virtual wxDataFormat
GetPreferredFormat(Direction dir
= Get
) const;
255 virtual size_t GetFormatCount(Direction dir
= Get
) const;
256 virtual void GetAllFormats(wxDataFormat
*formats
, Direction dir
= Get
) const;
257 virtual size_t GetDataSize(const wxDataFormat
& format
) const;
258 virtual bool GetDataHere(const wxDataFormat
& format
, void *buf
) const;
259 virtual bool SetData(const wxDataFormat
& format
, size_t len
, const void *buf
);
262 // returns the pointer to the object which supports this format or NULL
263 wxDataObjectSimple
*GetObject(const wxDataFormat
& format
) const;
266 // the list of all (simple) data objects whose formats we support
267 wxSimpleDataObjectList m_dataObjects
;
269 // the index of the preferred one (0 initially, so by default the first
270 // one is the preferred)
274 // ============================================================================
275 // Standard implementations of wxDataObjectSimple which can be used directly
276 // (i.e. without having to derive from them) for standard data type transfers.
278 // Note that although all of them can work with provided data, you can also
279 // override their virtual GetXXX() functions to only provide data on demand.
280 // ============================================================================
282 // ----------------------------------------------------------------------------
283 // wxTextDataObject contains text data
284 // ----------------------------------------------------------------------------
286 class WXDLLEXPORT wxTextDataObject
: public wxDataObjectSimple
289 // ctor: you can specify the text here or in SetText(), or override
291 wxTextDataObject(const wxString
& text
= wxEmptyString
)
292 : wxDataObjectSimple(wxDF_TEXT
), m_text(text
)
296 // virtual functions which you may override if you want to provide text on
297 // demand only - otherwise, the trivial default versions will be used
298 virtual size_t GetTextLength() const { return m_text
.Len() + 1; }
299 virtual wxString
GetText() const { return m_text
; }
300 virtual void SetText(const wxString
& text
) { m_text
= text
; }
302 // implement base class pure virtuals
303 // ----------------------------------
304 virtual size_t GetDataSize() const;
305 virtual bool GetDataHere(void *buf
) const;
306 virtual bool SetData(size_t len
, const void *buf
);
312 // ----------------------------------------------------------------------------
313 // wxBitmapDataObject contains a bitmap
314 // ----------------------------------------------------------------------------
316 class WXDLLEXPORT wxBitmapDataObjectBase
: public wxDataObjectSimple
319 // ctor: you can specify the bitmap here or in SetBitmap(), or override
321 wxBitmapDataObjectBase(const wxBitmap
& bitmap
= wxNullBitmap
)
322 : wxDataObjectSimple(wxDF_BITMAP
), m_bitmap(bitmap
)
326 // virtual functions which you may override if you want to provide data on
327 // demand only - otherwise, the trivial default versions will be used
328 virtual wxBitmap
GetBitmap() const { return m_bitmap
; }
329 virtual void SetBitmap(const wxBitmap
& bitmap
) { m_bitmap
= bitmap
; }
335 // ----------------------------------------------------------------------------
336 // wxFileDataObject contains a list of filenames
337 // ----------------------------------------------------------------------------
339 class WXDLLEXPORT wxFileDataObjectBase
: public wxDataObjectSimple
342 // ctor: you can specify the bitmap here or in SetBitmap(), or override
344 wxFileDataObjectBase() : wxDataObjectSimple(wxDF_FILENAME
) { }
346 // get a reference to our array - you may modify it (i.e. add/remove
347 // filenames to it then)
348 wxArrayString
& GetFilenames() { return m_filenames
; }
351 void AddFile(const wxString
& filename
) { m_filenames
.Add(filename
); }
353 // virtual functions which you may override if you want to provide data on
354 // demand only - otherwise, the trivial default versions will be used.
356 // they work with a NUL-separated string of filenames, the base class
357 // versions concatenate/extract filenames from this string
358 virtual wxString
GetFilenames() const;
359 virtual void SetFilenames(const wxChar
* filenames
);
362 wxArrayString m_filenames
;
365 // ----------------------------------------------------------------------------
366 // wxCustomDataObject contains arbitrary untyped user data.
368 // It is understood that this data can be copied bitwise.
369 // ----------------------------------------------------------------------------
371 class WXDLLEXPORT wxCustomDataObject
: public wxDataObjectSimple
374 // if you don't specify the format in the ctor, you can still use
376 wxCustomDataObject(const wxDataFormat
& format
= wxDF_INVALID
);
378 // the dtor calls Free()
379 virtual ~wxCustomDataObject();
381 // you can call SetData() to set m_data: it will make a copy of the data
382 // you pass - or you can use TakeData() which won't copy anything, but
383 // will take ownership of data (i.e. will call Free() on it later)
384 void TakeData(size_t size
, void *data
);
386 // this function is called to allocate "size" bytes of memory from
387 // SetData(). The default version uses operator new[].
388 virtual void *Alloc(size_t size
);
390 // this function is called when the data is freed, you may override it to
391 // anything you want (or may be nothing at all). The default version calls
392 // operator delete[] on m_data
395 // get data: you may override these functions if you wish to provide data
396 // only when it's requested
397 virtual size_t GetSize() const { return m_size
; }
398 virtual void *GetData() const { return m_data
; }
400 // implement base class pure virtuals
401 // ----------------------------------
402 virtual size_t GetDataSize() const;
403 virtual bool GetDataHere(void *buf
) const;
404 virtual bool SetData(size_t size
, const void *buf
);
411 // ----------------------------------------------------------------------------
412 // include platform-specific declarations of wxXXXBase classes
413 // ----------------------------------------------------------------------------
415 #if defined(__WXMSW__)
416 #include "wx/msw/ole/dataobj2.h"
417 #elif defined(__WXMOTIF__)
418 #include "wx/motif/dataobj2.h"
419 #elif defined(__WXGTK__)
420 #include "wx/gtk/dataobj2.h"
423 #endif // _WX_DATAOBJ_H_BASE_