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