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