added wxURLDataObject which unfortunately doesn't seem to work
[wxWidgets.git] / include / wx / dataobj.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/dataobj.h
3 // Purpose: common data object classes
4 // Author: Vadim Zeitlin, Robert Roebling
5 // Modified by:
6 // Created: 26.05.99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWindows Team
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_DATAOBJ_H_BASE_
13 #define _WX_DATAOBJ_H_BASE_
14
15 #ifdef __GNUG__
16 #pragma interface "dataobjbase.h"
17 #endif
18
19 // ----------------------------------------------------------------------------
20 // headers
21 // ----------------------------------------------------------------------------
22
23 #include "wx/defs.h"
24 #include "wx/string.h"
25 #include "wx/bitmap.h"
26 #include "wx/list.h"
27
28 // ============================================================================
29 /*
30 Generic data transfer related classes. The class hierarchy is as follows:
31
32 - wxDataObject-
33 / \
34 / \
35 wxDataObjectSimple wxDataObjectComposite
36 / | \
37 / | \
38 wxTextDataObject | wxBitmapDataObject
39 |
40 wxCustomDataObject
41
42 */
43 // ============================================================================
44
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 // ----------------------------------------------------------------------------
50
51 /* the class interface looks like this (pseudo code):
52
53 class wxDataFormat
54 {
55 public:
56 typedef <integral type> NativeFormat;
57
58 wxDataFormat(NativeFormat format = wxDF_INVALID);
59 wxDataFormat(const wxChar *format);
60
61 wxDataFormat& operator=(NativeFormat format);
62 wxDataFormat& operator=(const wxDataFormat& format);
63
64 bool operator==(NativeFormat format) const;
65 bool operator!=(NativeFormat format) const;
66
67 void SetType(NativeFormat format);
68 NativeFormat GetType() const;
69
70 wxString GetId() const;
71 void SetId(const wxChar *format);
72 };
73
74 */
75
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"
86 #endif
87
88 // the value for default argument to some functions (corresponds to
89 // wxDF_INVALID)
90 extern WXDLLEXPORT const wxDataFormat& wxFormatInvalid;
91
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()).
96 //
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.
100 //
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 // ----------------------------------------------------------------------------
111
112 class WXDLLEXPORT wxDataObjectBase
113 {
114 public:
115 enum Direction
116 {
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)
120 };
121
122 // this class is polymorphic, hence it needs a virtual dtor
123 virtual ~wxDataObjectBase();
124
125 // get the best suited format for rendering our data
126 virtual wxDataFormat GetPreferredFormat(Direction dir = Get) const = 0;
127
128 // get the number of formats we support
129 virtual size_t GetFormatCount(Direction dir = Get) const = 0;
130
131 // return all formats in the provided array (of size GetFormatCount())
132 virtual void GetAllFormats(wxDataFormat *formats,
133 Direction dir = Get) const = 0;
134
135 // get the (total) size of data for the given format
136 virtual size_t GetDataSize(const wxDataFormat& format) const = 0;
137
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;
141
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))
146 {
147 return FALSE;
148 }
149
150 // returns TRUE if this format is supported
151 bool IsSupported(const wxDataFormat& format, Direction dir = Get) const;
152 };
153
154 // ----------------------------------------------------------------------------
155 // include the platform-specific declarations of wxDataObject
156 // ----------------------------------------------------------------------------
157
158 #if defined(__WXMSW__)
159 #include "wx/msw/ole/dataobj.h"
160 #elif defined(__WXMOTIF__)
161 #include "wx/motif/dataobj.h"
162 #elif defined(__WXGTK__)
163 #include "wx/gtk/dataobj.h"
164 #elif defined(__WXQT__)
165 #include "wx/qt/dnd.h"
166 #elif defined(__WXMAC__)
167 #include "wx/mac/dataobj.h"
168 #elif defined(__WXPM__)
169 #include "wx/os2/dataobj.h"
170 #elif defined(__WXSTUBS__)
171 #include "wx/stubs/dnd.h"
172 #endif
173
174 // ----------------------------------------------------------------------------
175 // wxDataObjectSimple is a wxDataObject which only supports one format (in
176 // both Get and Set directions, but you may return FALSE from GetDataHere() or
177 // SetData() if one of them is not supported). This is the simplest possible
178 // wxDataObject implementation.
179 //
180 // This is still an "abstract base class" (although it doesn't have any pure
181 // virtual functions), to use it you should derive from it and implement
182 // GetDataSize(), GetDataHere() and SetData() functions because the base class
183 // versions don't do anything - they just return "not implemented".
184 //
185 // This class should be used when you provide data in only one format (no
186 // conversion to/from other formats), either a standard or a custom one.
187 // Otherwise, you should use wxDataObjectComposite or wxDataObject directly.
188 // ----------------------------------------------------------------------------
189
190 class WXDLLEXPORT wxDataObjectSimple : public wxDataObject
191 {
192 public:
193 // ctor takes the format we support, but it can also be set later with
194 // SetFormat()
195 wxDataObjectSimple(const wxDataFormat& format = wxFormatInvalid)
196 : m_format(format)
197 {
198 }
199
200 // get/set the format we support
201 const wxDataFormat& GetFormat() const { return m_format; }
202 void SetFormat(const wxDataFormat& format) { m_format = format; }
203
204 // virtual functions to override in derived class (the base class versions
205 // just return "not implemented")
206 // -----------------------------------------------------------------------
207
208 // get the size of our data
209 virtual size_t GetDataSize() const
210 { return 0; }
211
212 // copy our data to the buffer
213 virtual bool GetDataHere(void *WXUNUSED(buf)) const
214 { return FALSE; }
215
216 // copy data from buffer to our data
217 virtual bool SetData(size_t WXUNUSED(len), const void *WXUNUSED(buf))
218 { return FALSE; }
219
220 // implement base class pure virtuals
221 // ----------------------------------
222 virtual wxDataFormat GetPreferredFormat(wxDataObjectBase::Direction WXUNUSED(dir) = Get) const
223 { return m_format; }
224 virtual size_t GetFormatCount(wxDataObjectBase::Direction WXUNUSED(dir) = Get) const
225 { return 1; }
226 virtual void GetAllFormats(wxDataFormat *formats,
227 wxDataObjectBase::Direction WXUNUSED(dir) = Get) const
228 { *formats = m_format; }
229 virtual size_t GetDataSize(const wxDataFormat& WXUNUSED(format)) const
230 { return GetDataSize(); }
231 virtual bool GetDataHere(const wxDataFormat& WXUNUSED(format),
232 void *buf) const
233 { return GetDataHere(buf); }
234 virtual bool SetData(const wxDataFormat& WXUNUSED(format),
235 size_t len, const void *buf)
236 { return SetData(len, buf); }
237
238 private:
239 // the one and only format we support
240 wxDataFormat m_format;
241 };
242
243 // ----------------------------------------------------------------------------
244 // wxDataObjectComposite is the simplest way to implement wxDataObject
245 // supporting multiple formats. It contains several wxDataObjectSimple and
246 // supports all formats supported by any of them.
247 //
248 // This class shouldn't be (normally) derived from, but may be used directly.
249 // If you need more flexibility than what it provides, you should probably use
250 // wxDataObject directly.
251 // ----------------------------------------------------------------------------
252
253 WX_DECLARE_EXPORTED_LIST(wxDataObjectSimple, wxSimpleDataObjectList);
254
255 class WXDLLEXPORT wxDataObjectComposite : public wxDataObject
256 {
257 public:
258 // ctor
259 wxDataObjectComposite() { m_preferred = 0; }
260
261 // add data object (it will be deleted by wxDataObjectComposite, hence it
262 // must be allocated on the heap) whose format will become the preferred
263 // one if preferred == TRUE
264 void Add(wxDataObjectSimple *dataObject, bool preferred = FALSE);
265
266 // implement base class pure virtuals
267 // ----------------------------------
268 virtual wxDataFormat GetPreferredFormat(wxDataObjectBase::Direction dir = Get) const;
269 virtual size_t GetFormatCount(wxDataObjectBase::Direction dir = Get) const;
270 virtual void GetAllFormats(wxDataFormat *formats, wxDataObjectBase::Direction dir = Get) const;
271 virtual size_t GetDataSize(const wxDataFormat& format) const;
272 virtual bool GetDataHere(const wxDataFormat& format, void *buf) const;
273 virtual bool SetData(const wxDataFormat& format, size_t len, const void *buf);
274
275 protected:
276 // returns the pointer to the object which supports this format or NULL
277 wxDataObjectSimple *GetObject(const wxDataFormat& format) const;
278
279 private:
280 // the list of all (simple) data objects whose formats we support
281 wxSimpleDataObjectList m_dataObjects;
282
283 // the index of the preferred one (0 initially, so by default the first
284 // one is the preferred)
285 size_t m_preferred;
286 };
287
288 // ============================================================================
289 // Standard implementations of wxDataObjectSimple which can be used directly
290 // (i.e. without having to derive from them) for standard data type transfers.
291 //
292 // Note that although all of them can work with provided data, you can also
293 // override their virtual GetXXX() functions to only provide data on demand.
294 // ============================================================================
295
296 // ----------------------------------------------------------------------------
297 // wxTextDataObject contains text data
298 // ----------------------------------------------------------------------------
299
300 class WXDLLEXPORT wxTextDataObject : public wxDataObjectSimple
301 {
302 public:
303 // ctor: you can specify the text here or in SetText(), or override
304 // GetText()
305 wxTextDataObject(const wxString& text = wxEmptyString)
306 : wxDataObjectSimple(wxDF_TEXT), m_text(text)
307 {
308 }
309
310 // virtual functions which you may override if you want to provide text on
311 // demand only - otherwise, the trivial default versions will be used
312 virtual size_t GetTextLength() const { return m_text.Len() + 1; }
313 virtual wxString GetText() const { return m_text; }
314 virtual void SetText(const wxString& text) { m_text = text; }
315
316 // implement base class pure virtuals
317 // ----------------------------------
318 virtual size_t GetDataSize() const;
319 virtual bool GetDataHere(void *buf) const;
320 virtual bool SetData(size_t len, const void *buf);
321
322 private:
323 wxString m_text;
324
325 // virtual function hiding supression
326 size_t GetDataSize(const wxDataFormat& format) const
327 { return(wxDataObjectSimple::GetDataSize(format)); }
328 bool GetDataHere(const wxDataFormat& format, void *pBuf) const
329 { return(wxDataObjectSimple::GetDataHere(format, pBuf)); }
330 bool SetData(const wxDataFormat& format, size_t nLen, const void* pBuf)
331 { return(wxDataObjectSimple::SetData(format, nLen, pBuf)); }
332 };
333
334 // ----------------------------------------------------------------------------
335 // wxBitmapDataObject contains a bitmap
336 // ----------------------------------------------------------------------------
337
338 class WXDLLEXPORT wxBitmapDataObjectBase : public wxDataObjectSimple
339 {
340 public:
341 // ctor: you can specify the bitmap here or in SetBitmap(), or override
342 // GetBitmap()
343 wxBitmapDataObjectBase(const wxBitmap& bitmap = wxNullBitmap)
344 : wxDataObjectSimple(wxDF_BITMAP), m_bitmap(bitmap)
345 {
346 }
347
348 // virtual functions which you may override if you want to provide data on
349 // demand only - otherwise, the trivial default versions will be used
350 virtual wxBitmap GetBitmap() const { return m_bitmap; }
351 virtual void SetBitmap(const wxBitmap& bitmap) { m_bitmap = bitmap; }
352
353 protected:
354 wxBitmap m_bitmap;
355 };
356
357 // ----------------------------------------------------------------------------
358 // wxFileDataObject contains a list of filenames
359 //
360 // NB: notice that this is a "write only" object, it can only be filled with
361 // data from drag and drop operation.
362 // ----------------------------------------------------------------------------
363
364 class WXDLLEXPORT wxFileDataObjectBase : public wxDataObjectSimple
365 {
366 public:
367 // ctor: use AddFile() later to fill the array
368 wxFileDataObjectBase() : wxDataObjectSimple(wxDF_FILENAME) { }
369
370 // get a reference to our array
371 const wxArrayString& GetFilenames() const { return m_filenames; }
372
373 // the Get() functions do nothing for us
374 virtual size_t GetDataSize() const { return 0; }
375 virtual bool GetDataHere(void *WXUNUSED(buf)) const { return FALSE; }
376
377 protected:
378 wxArrayString m_filenames;
379
380 private:
381 // Virtual function hiding supression
382 size_t GetDataSize(const wxDataFormat& format) const
383 { return(wxDataObjectSimple::GetDataSize(format)); }
384 bool GetDataHere(const wxDataFormat& format, void* pBuf) const
385 { return(wxDataObjectSimple::GetDataHere(format, pBuf)); }
386 };
387
388 // ----------------------------------------------------------------------------
389 // wxCustomDataObject contains arbitrary untyped user data.
390 //
391 // It is understood that this data can be copied bitwise.
392 // ----------------------------------------------------------------------------
393
394 class WXDLLEXPORT wxCustomDataObject : public wxDataObjectSimple
395 {
396 public:
397 // if you don't specify the format in the ctor, you can still use
398 // SetFormat() later
399 wxCustomDataObject(const wxDataFormat& format = wxFormatInvalid);
400
401 // the dtor calls Free()
402 virtual ~wxCustomDataObject();
403
404 // you can call SetData() to set m_data: it will make a copy of the data
405 // you pass - or you can use TakeData() which won't copy anything, but
406 // will take ownership of data (i.e. will call Free() on it later)
407 void TakeData(size_t size, void *data);
408
409 // this function is called to allocate "size" bytes of memory from
410 // SetData(). The default version uses operator new[].
411 virtual void *Alloc(size_t size);
412
413 // this function is called when the data is freed, you may override it to
414 // anything you want (or may be nothing at all). The default version calls
415 // operator delete[] on m_data
416 virtual void Free();
417
418 // get data: you may override these functions if you wish to provide data
419 // only when it's requested
420 virtual size_t GetSize() const { return m_size; }
421 virtual void *GetData() const { return m_data; }
422
423 // implement base class pure virtuals
424 // ----------------------------------
425 virtual size_t GetDataSize() const;
426 virtual bool GetDataHere(void *buf) const;
427 virtual bool SetData(size_t size, const void *buf);
428
429 private:
430 size_t m_size;
431 void *m_data;
432
433 // virtual function hiding supression
434 size_t GetDataSize(const wxDataFormat& format) const
435 { return(wxDataObjectSimple::GetDataSize(format)); }
436 bool GetDataHere(const wxDataFormat& format, void* pBuf) const
437 { return(wxDataObjectSimple::GetDataHere(format, pBuf)); }
438 bool SetData(const wxDataFormat& format, size_t nLen, const void* pBuf)
439 { return(wxDataObjectSimple::SetData(format, nLen, pBuf)); }
440 };
441
442 // ----------------------------------------------------------------------------
443 // include platform-specific declarations of wxXXXBase classes
444 // ----------------------------------------------------------------------------
445
446 #if defined(__WXMSW__)
447 #include "wx/msw/ole/dataobj2.h"
448
449 // wxURLDataObject defined in msw/ole/dataobj2.h
450 #else // !__WXMSW__
451 #if defined(__WXGTK__)
452 #include "wx/gtk/dataobj2.h"
453 #elif defined(__WXMAC__)
454 #include "wx/mac/dataobj2.h"
455 #elif defined(__WXPM__)
456 #include "wx/os2/dataobj2.h"
457 #endif
458
459 // wxURLDataObject is simply wxTextDataObject with a different name
460 class WXDLLEXPORT wxURLDataObject : public wxTextDataObject
461 {
462 public:
463 wxString GetURL() const { return GetText(); }
464 };
465 #endif // __WXMSW__/!__WXMSW__
466
467 #endif // _WX_DATAOBJ_H_BASE_