f3759b7cbdfb0cb32c03fb4d6be0c134f286b097
[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) wxWidgets Team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_DATAOBJ_H_BASE_
13 #define _WX_DATAOBJ_H_BASE_
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18 #include "wx/defs.h"
19
20 #if wxUSE_DATAOBJ
21
22 #include "wx/string.h"
23 #include "wx/bitmap.h"
24 #include "wx/list.h"
25 #include "wx/arrstr.h"
26
27 // ============================================================================
28 /*
29 Generic data transfer related classes. The class hierarchy is as follows:
30
31 - wxDataObject-
32 / \
33 / \
34 wxDataObjectSimple wxDataObjectComposite
35 / | \
36 / | \
37 wxTextDataObject | wxBitmapDataObject
38 |
39 wxCustomDataObject
40
41 */
42 // ============================================================================
43
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 // ----------------------------------------------------------------------------
49
50 /* the class interface looks like this (pseudo code):
51
52 class wxDataFormat
53 {
54 public:
55 typedef <integral type> NativeFormat;
56
57 wxDataFormat(NativeFormat format = wxDF_INVALID);
58 wxDataFormat(const wxChar *format);
59
60 wxDataFormat& operator=(NativeFormat format);
61 wxDataFormat& operator=(const wxDataFormat& format);
62
63 bool operator==(NativeFormat format) const;
64 bool operator!=(NativeFormat format) const;
65
66 void SetType(NativeFormat format);
67 NativeFormat GetType() const;
68
69 wxString GetId() const;
70 void SetId(const wxChar *format);
71 };
72
73 */
74
75 #if defined(__WXMSW__)
76 #include "wx/msw/ole/dataform.h"
77 #elif defined(__WXMOTIF__)
78 #include "wx/motif/dataform.h"
79 #elif defined(__WXGTK20__)
80 #include "wx/gtk/dataform.h"
81 #elif defined(__WXGTK__)
82 #include "wx/gtk1/dataform.h"
83 #elif defined(__WXX11__)
84 #include "wx/x11/dataform.h"
85 #elif defined(__WXMAC__)
86 #include "wx/mac/dataform.h"
87 #elif defined(__WXCOCOA__)
88 #include "wx/cocoa/dataform.h"
89 #elif defined(__WXPM__)
90 #include "wx/os2/dataform.h"
91 #endif
92
93 // the value for default argument to some functions (corresponds to
94 // wxDF_INVALID)
95 extern WXDLLEXPORT const wxDataFormat& wxFormatInvalid;
96
97 // ----------------------------------------------------------------------------
98 // wxDataObject represents a piece of data which knows which formats it
99 // supports and knows how to render itself in each of them - GetDataHere(),
100 // and how to restore data from the buffer (SetData()).
101 //
102 // Although this class may be used directly (i.e. custom classes may be
103 // derived from it), in many cases it might be simpler to use either
104 // wxDataObjectSimple or wxDataObjectComposite classes.
105 //
106 // A data object may be "read only", i.e. support only GetData() functions or
107 // "read-write", i.e. support both GetData() and SetData() (in principle, it
108 // might be "write only" too, but this is rare). Moreover, it doesn't have to
109 // support the same formats in Get() and Set() directions: for example, a data
110 // object containing JPEG image might accept BMPs in GetData() because JPEG
111 // image may be easily transformed into BMP but not in SetData(). Accordingly,
112 // all methods dealing with formats take an additional "direction" argument
113 // which is either SET or GET and which tells the function if the format needs
114 // to be supported by SetData() or GetDataHere().
115 // ----------------------------------------------------------------------------
116
117 class WXDLLEXPORT wxDataObjectBase
118 {
119 public:
120 enum Direction
121 {
122 Get = 0x01, // format is supported by GetDataHere()
123 Set = 0x02, // format is supported by SetData()
124 Both = 0x03 // format is supported by both (unused currently)
125 };
126
127 // this class is polymorphic, hence it needs a virtual dtor
128 virtual ~wxDataObjectBase();
129
130 // get the best suited format for rendering our data
131 virtual wxDataFormat GetPreferredFormat(Direction dir = Get) const = 0;
132
133 // get the number of formats we support
134 virtual size_t GetFormatCount(Direction dir = Get) const = 0;
135
136 // return all formats in the provided array (of size GetFormatCount())
137 virtual void GetAllFormats(wxDataFormat *formats,
138 Direction dir = Get) const = 0;
139
140 // get the (total) size of data for the given format
141 virtual size_t GetDataSize(const wxDataFormat& format) const = 0;
142
143 // copy raw data (in the specified format) to the provided buffer, return
144 // true if data copied successfully, false otherwise
145 virtual bool GetDataHere(const wxDataFormat& format, void *buf) const = 0;
146
147 // get data from the buffer of specified length (in the given format),
148 // return true if the data was read successfully, false otherwise
149 virtual bool SetData(const wxDataFormat& WXUNUSED(format),
150 size_t WXUNUSED(len), const void * WXUNUSED(buf))
151 {
152 return false;
153 }
154
155 // returns true if this format is supported
156 bool IsSupported(const wxDataFormat& format, Direction dir = Get) const;
157 };
158
159 // ----------------------------------------------------------------------------
160 // include the platform-specific declarations of wxDataObject
161 // ----------------------------------------------------------------------------
162
163 #if defined(__WXMSW__)
164 #include "wx/msw/ole/dataobj.h"
165 #elif defined(__WXMOTIF__)
166 #include "wx/motif/dataobj.h"
167 #elif defined(__WXX11__)
168 #include "wx/x11/dataobj.h"
169 #elif defined(__WXGTK__)
170 #include "wx/gtk/dataobj.h"
171 #elif defined(__WXMAC__)
172 #include "wx/mac/dataobj.h"
173 #elif defined(__WXCOCOA__)
174 #include "wx/cocoa/dataobj.h"
175 #elif defined(__WXPM__)
176 #include "wx/os2/dataobj.h"
177 #endif
178
179 // ----------------------------------------------------------------------------
180 // wxDataObjectSimple is a wxDataObject which only supports one format (in
181 // both Get and Set directions, but you may return false from GetDataHere() or
182 // SetData() if one of them is not supported). This is the simplest possible
183 // wxDataObject implementation.
184 //
185 // This is still an "abstract base class" (although it doesn't have any pure
186 // virtual functions), to use it you should derive from it and implement
187 // GetDataSize(), GetDataHere() and SetData() functions because the base class
188 // versions don't do anything - they just return "not implemented".
189 //
190 // This class should be used when you provide data in only one format (no
191 // conversion to/from other formats), either a standard or a custom one.
192 // Otherwise, you should use wxDataObjectComposite or wxDataObject directly.
193 // ----------------------------------------------------------------------------
194
195 class WXDLLEXPORT wxDataObjectSimple : public wxDataObject
196 {
197 public:
198 // ctor takes the format we support, but it can also be set later with
199 // SetFormat()
200 wxDataObjectSimple(const wxDataFormat& format = wxFormatInvalid)
201 : m_format(format)
202 {
203 }
204
205 // get/set the format we support
206 const wxDataFormat& GetFormat() const { return m_format; }
207 void SetFormat(const wxDataFormat& format) { m_format = format; }
208
209 // virtual functions to override in derived class (the base class versions
210 // just return "not implemented")
211 // -----------------------------------------------------------------------
212
213 // get the size of our data
214 virtual size_t GetDataSize() const
215 { return 0; }
216
217 // copy our data to the buffer
218 virtual bool GetDataHere(void *WXUNUSED(buf)) const
219 { return false; }
220
221 // copy data from buffer to our data
222 virtual bool SetData(size_t WXUNUSED(len), const void *WXUNUSED(buf))
223 { return false; }
224
225 // implement base class pure virtuals
226 // ----------------------------------
227 virtual wxDataFormat GetPreferredFormat(wxDataObjectBase::Direction WXUNUSED(dir) = Get) const
228 { return m_format; }
229 virtual size_t GetFormatCount(wxDataObjectBase::Direction WXUNUSED(dir) = Get) const
230 { return 1; }
231 virtual void GetAllFormats(wxDataFormat *formats,
232 wxDataObjectBase::Direction WXUNUSED(dir) = Get) const
233 { *formats = m_format; }
234 virtual size_t GetDataSize(const wxDataFormat& WXUNUSED(format)) const
235 { return GetDataSize(); }
236 virtual bool GetDataHere(const wxDataFormat& WXUNUSED(format),
237 void *buf) const
238 { return GetDataHere(buf); }
239 virtual bool SetData(const wxDataFormat& WXUNUSED(format),
240 size_t len, const void *buf)
241 { return SetData(len, buf); }
242
243 private:
244 // the one and only format we support
245 wxDataFormat m_format;
246
247 DECLARE_NO_COPY_CLASS(wxDataObjectSimple)
248 };
249
250 // ----------------------------------------------------------------------------
251 // wxDataObjectComposite is the simplest way to implement wxDataObject
252 // supporting multiple formats. It contains several wxDataObjectSimple and
253 // supports all formats supported by any of them.
254 //
255 // This class shouldn't be (normally) derived from, but may be used directly.
256 // If you need more flexibility than what it provides, you should probably use
257 // wxDataObject directly.
258 // ----------------------------------------------------------------------------
259
260 WX_DECLARE_EXPORTED_LIST(wxDataObjectSimple, wxSimpleDataObjectList);
261
262 class WXDLLEXPORT wxDataObjectComposite : public wxDataObject
263 {
264 public:
265 // ctor
266 wxDataObjectComposite();
267 virtual ~wxDataObjectComposite();
268
269 // add data object (it will be deleted by wxDataObjectComposite, hence it
270 // must be allocated on the heap) whose format will become the preferred
271 // one if preferred == true
272 void Add(wxDataObjectSimple *dataObject, bool preferred = false);
273
274 // implement base class pure virtuals
275 // ----------------------------------
276 virtual wxDataFormat GetPreferredFormat(wxDataObjectBase::Direction dir = Get) const;
277 virtual size_t GetFormatCount(wxDataObjectBase::Direction dir = Get) const;
278 virtual void GetAllFormats(wxDataFormat *formats, wxDataObjectBase::Direction dir = Get) const;
279 virtual size_t GetDataSize(const wxDataFormat& format) const;
280 virtual bool GetDataHere(const wxDataFormat& format, void *buf) const;
281 virtual bool SetData(const wxDataFormat& format, size_t len, const void *buf);
282
283 protected:
284 // returns the pointer to the object which supports this format or NULL
285 wxDataObjectSimple *GetObject(const wxDataFormat& format) const;
286 #if defined(__WXMSW__)
287 virtual const void* GetSizeFromBuffer( const void* buffer, size_t* size,
288 const wxDataFormat& format );
289 virtual void* SetSizeInBuffer( void* buffer, size_t size,
290 const wxDataFormat& format );
291 virtual size_t GetBufferOffset( const wxDataFormat& format );
292 #endif
293
294 private:
295 // the list of all (simple) data objects whose formats we support
296 wxSimpleDataObjectList m_dataObjects;
297
298 // the index of the preferred one (0 initially, so by default the first
299 // one is the preferred)
300 size_t m_preferred;
301
302 DECLARE_NO_COPY_CLASS(wxDataObjectComposite)
303 };
304
305 // ============================================================================
306 // Standard implementations of wxDataObjectSimple which can be used directly
307 // (i.e. without having to derive from them) for standard data type transfers.
308 //
309 // Note that although all of them can work with provided data, you can also
310 // override their virtual GetXXX() functions to only provide data on demand.
311 // ============================================================================
312
313 // ----------------------------------------------------------------------------
314 // wxTextDataObject contains text data
315 // ----------------------------------------------------------------------------
316
317 class WXDLLEXPORT wxTextDataObject : public wxDataObjectSimple
318 {
319 public:
320 // ctor: you can specify the text here or in SetText(), or override
321 // GetText()
322 wxTextDataObject(const wxString& text = wxEmptyString)
323 : wxDataObjectSimple(
324 #if wxUSE_UNICODE
325 wxDF_UNICODETEXT
326 #else
327 wxDF_TEXT
328 #endif
329 ),
330 m_text(text)
331 {
332 }
333
334 // virtual functions which you may override if you want to provide text on
335 // demand only - otherwise, the trivial default versions will be used
336 virtual size_t GetTextLength() const { return m_text.Len() + 1; }
337 virtual wxString GetText() const { return m_text; }
338 virtual void SetText(const wxString& text) { m_text = text; }
339
340 // implement base class pure virtuals
341 // ----------------------------------
342
343 // some platforms have 2 and not 1 format for text data
344 #if wxUSE_UNICODE && (defined(__WXGTK20__) || defined(__WXMAC__))
345 virtual size_t GetFormatCount(Direction WXUNUSED(dir) = Get) const { return 2; }
346 virtual void GetAllFormats(wxDataFormat *formats,
347 wxDataObjectBase::Direction WXUNUSED(dir) = Get) const;
348
349 virtual size_t GetDataSize() const { return GetDataSize(GetPreferredFormat()); }
350 virtual bool GetDataHere(void *buf) const { return GetDataHere(GetPreferredFormat(), buf); }
351 virtual bool SetData(size_t len, const void *buf) { return SetData(GetPreferredFormat(), len, buf); }
352
353 size_t GetDataSize(const wxDataFormat& format) const;
354 bool GetDataHere(const wxDataFormat& format, void *pBuf) const;
355 bool SetData(const wxDataFormat& format, size_t nLen, const void* pBuf);
356 #else
357 virtual size_t GetDataSize() const;
358 virtual bool GetDataHere(void *buf) const;
359 virtual bool SetData(size_t len, const void *buf);
360
361 size_t GetDataSize(const wxDataFormat& format) const
362 { return(wxDataObjectSimple::GetDataSize(format)); }
363 bool GetDataHere(const wxDataFormat& format, void *pBuf) const
364 { return(wxDataObjectSimple::GetDataHere(format, pBuf)); }
365 bool SetData(const wxDataFormat& format, size_t nLen, const void* pBuf)
366 { return(wxDataObjectSimple::SetData(format, nLen, pBuf)); }
367 #endif
368
369 private:
370 wxString m_text;
371
372 DECLARE_NO_COPY_CLASS(wxTextDataObject)
373 };
374
375 // ----------------------------------------------------------------------------
376 // wxBitmapDataObject contains a bitmap
377 // ----------------------------------------------------------------------------
378
379 class WXDLLEXPORT wxBitmapDataObjectBase : public wxDataObjectSimple
380 {
381 public:
382 // ctor: you can specify the bitmap here or in SetBitmap(), or override
383 // GetBitmap()
384 wxBitmapDataObjectBase(const wxBitmap& bitmap = wxNullBitmap)
385 : wxDataObjectSimple(wxDF_BITMAP), m_bitmap(bitmap)
386 {
387 }
388
389 // virtual functions which you may override if you want to provide data on
390 // demand only - otherwise, the trivial default versions will be used
391 virtual wxBitmap GetBitmap() const { return m_bitmap; }
392 virtual void SetBitmap(const wxBitmap& bitmap) { m_bitmap = bitmap; }
393
394 protected:
395 wxBitmap m_bitmap;
396
397 DECLARE_NO_COPY_CLASS(wxBitmapDataObjectBase)
398 };
399
400 // ----------------------------------------------------------------------------
401 // wxFileDataObject contains a list of filenames
402 //
403 // NB: notice that this is a "write only" object, it can only be filled with
404 // data from drag and drop operation.
405 // ----------------------------------------------------------------------------
406
407 class WXDLLEXPORT wxFileDataObjectBase : public wxDataObjectSimple
408 {
409 public:
410 // ctor: use AddFile() later to fill the array
411 wxFileDataObjectBase() : wxDataObjectSimple(wxDF_FILENAME) { }
412
413 // get a reference to our array
414 const wxArrayString& GetFilenames() const { return m_filenames; }
415
416 // the Get() functions do nothing for us
417 virtual size_t GetDataSize() const { return 0; }
418 virtual bool GetDataHere(void *WXUNUSED(buf)) const { return false; }
419
420 protected:
421 wxArrayString m_filenames;
422
423 private:
424 // Virtual function hiding supression
425 size_t GetDataSize(const wxDataFormat& format) const
426 { return(wxDataObjectSimple::GetDataSize(format)); }
427 bool GetDataHere(const wxDataFormat& format, void* pBuf) const
428 { return(wxDataObjectSimple::GetDataHere(format, pBuf)); }
429
430 DECLARE_NO_COPY_CLASS(wxFileDataObjectBase)
431 };
432
433 // ----------------------------------------------------------------------------
434 // wxCustomDataObject contains arbitrary untyped user data.
435 //
436 // It is understood that this data can be copied bitwise.
437 // ----------------------------------------------------------------------------
438
439 class WXDLLEXPORT wxCustomDataObject : public wxDataObjectSimple
440 {
441 public:
442 // if you don't specify the format in the ctor, you can still use
443 // SetFormat() later
444 wxCustomDataObject(const wxDataFormat& format = wxFormatInvalid);
445
446 // the dtor calls Free()
447 virtual ~wxCustomDataObject();
448
449 // you can call SetData() to set m_data: it will make a copy of the data
450 // you pass - or you can use TakeData() which won't copy anything, but
451 // will take ownership of data (i.e. will call Free() on it later)
452 void TakeData(size_t size, void *data);
453
454 // this function is called to allocate "size" bytes of memory from
455 // SetData(). The default version uses operator new[].
456 virtual void *Alloc(size_t size);
457
458 // this function is called when the data is freed, you may override it to
459 // anything you want (or may be nothing at all). The default version calls
460 // operator delete[] on m_data
461 virtual void Free();
462
463 // get data: you may override these functions if you wish to provide data
464 // only when it's requested
465 virtual size_t GetSize() const { return m_size; }
466 virtual void *GetData() const { return m_data; }
467
468 // implement base class pure virtuals
469 // ----------------------------------
470 virtual size_t GetDataSize() const;
471 virtual bool GetDataHere(void *buf) const;
472 virtual bool SetData(size_t size, const void *buf);
473
474 private:
475 size_t m_size;
476 void *m_data;
477
478 // virtual function hiding supression
479 size_t GetDataSize(const wxDataFormat& format) const
480 { return(wxDataObjectSimple::GetDataSize(format)); }
481 bool GetDataHere(const wxDataFormat& format, void* pBuf) const
482 { return(wxDataObjectSimple::GetDataHere(format, pBuf)); }
483 bool SetData(const wxDataFormat& format, size_t nLen, const void* pBuf)
484 { return(wxDataObjectSimple::SetData(format, nLen, pBuf)); }
485
486 DECLARE_NO_COPY_CLASS(wxCustomDataObject)
487 };
488
489 // ----------------------------------------------------------------------------
490 // include platform-specific declarations of wxXXXBase classes
491 // ----------------------------------------------------------------------------
492
493 #if defined(__WXMSW__)
494 #include "wx/msw/ole/dataobj2.h"
495
496 // wxURLDataObject defined in msw/ole/dataobj2.h
497 #else // !__WXMSW__
498 #if defined(__WXGTK__)
499 #include "wx/gtk/dataobj2.h"
500 #elif defined(__WXX11__)
501 #include "wx/x11/dataobj2.h"
502 #elif defined(__WXMOTIF__)
503 #include "wx/motif/dataobj2.h"
504 #elif defined(__WXMAC__)
505 #include "wx/mac/dataobj2.h"
506 #elif defined(__WXCOCOA__)
507 #include "wx/cocoa/dataobj2.h"
508 #elif defined(__WXPM__)
509 #include "wx/os2/dataobj2.h"
510 #endif
511
512 // wxURLDataObject is simply wxTextDataObject with a different name
513 class WXDLLEXPORT wxURLDataObject : public wxTextDataObject
514 {
515 public:
516 wxString GetURL() const { return GetText(); }
517 void SetURL(const wxString& url) { SetText(url); }
518 };
519 #endif // __WXMSW__/!__WXMSW__
520
521 #endif // wxUSE_DATAOBJ
522
523 #endif // _WX_DATAOBJ_H_BASE_