]> git.saurik.com Git - wxWidgets.git/blob - include/wx/dataobj.h
Tweaking some of the new wxPython stuff for wxGTK
[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 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
151 // ----------------------------------------------------------------------------
152 // include the platform-specific declarations of wxDataObject
153 // ----------------------------------------------------------------------------
154
155 #if defined(__WXMSW__)
156 #include "wx/msw/ole/dataobj.h"
157 #elif defined(__WXMOTIF__)
158 #include "wx/motif/dataobj.h"
159 #elif defined(__WXGTK__)
160 #include "wx/gtk/dataobj.h"
161 #elif defined(__WXQT__)
162 #include "wx/qt/dnd.h"
163 #elif defined(__WXMAC__)
164 #include "wx/mac/dataobj.h"
165 #elif defined(__WXPM__)
166 #include "wx/os2/dataobj.h"
167 #elif defined(__WXSTUBS__)
168 #include "wx/stubs/dnd.h"
169 #endif
170
171 // ----------------------------------------------------------------------------
172 // wxDataObjectSimple is a wxDataObject which only supports one format (in
173 // both Get and Set directions, but you may return FALSE from GetDataHere() or
174 // SetData() if one of them is not supported). This is the simplest possible
175 // wxDataObject implementation.
176 //
177 // This is still an "abstract base class" (although it doesn't have any pure
178 // virtual functions), to use it you should derive from it and implement
179 // GetDataSize(), GetDataHere() and SetData() functions because the base class
180 // versions don't do anything - they just return "not implemented".
181 //
182 // This class should be used when you provide data in only one format (no
183 // conversion to/from other formats), either a standard or a custom one.
184 // Otherwise, you should use wxDataObjectComposite or wxDataObject directly.
185 // ----------------------------------------------------------------------------
186
187 class WXDLLEXPORT wxDataObjectSimple : public wxDataObject
188 {
189 public:
190 // ctor takes the format we support, but it can also be set later with
191 // SetFormat()
192 wxDataObjectSimple(const wxDataFormat& format = wxFormatInvalid)
193 : m_format(format)
194 {
195 }
196
197 // get/set the format we support
198 const wxDataFormat& GetFormat() const { return m_format; }
199 void SetFormat(const wxDataFormat& format) { m_format = format; }
200
201 // virtual functions to override in derived class (the base class versions
202 // just return "not implemented")
203 // -----------------------------------------------------------------------
204
205 // get the size of our data
206 virtual size_t GetDataSize() const
207 { return 0; }
208
209 // copy our data to the buffer
210 virtual bool GetDataHere(void *WXUNUSED(buf)) const
211 { return FALSE; }
212
213 // copy data from buffer to our data
214 virtual bool SetData(size_t WXUNUSED(len), const void *WXUNUSED(buf))
215 { return FALSE; }
216
217 // implement base class pure virtuals
218 // ----------------------------------
219 virtual wxDataFormat GetPreferredFormat(wxDataObjectBase::Direction WXUNUSED(dir) = Get) const
220 { return m_format; }
221 virtual size_t GetFormatCount(wxDataObjectBase::Direction WXUNUSED(dir) = Get) const
222 { return 1; }
223 virtual void GetAllFormats(wxDataFormat *formats,
224 wxDataObjectBase::Direction WXUNUSED(dir) = Get) const
225 { *formats = m_format; }
226 virtual size_t GetDataSize(const wxDataFormat& WXUNUSED(format)) const
227 { return GetDataSize(); }
228 virtual bool GetDataHere(const wxDataFormat& WXUNUSED(format),
229 void *buf) const
230 { return GetDataHere(buf); }
231 virtual bool SetData(const wxDataFormat& WXUNUSED(format),
232 size_t len, const void *buf)
233 { return SetData(len, buf); }
234
235 private:
236 // the one and only format we support
237 wxDataFormat m_format;
238 };
239
240 // ----------------------------------------------------------------------------
241 // wxDataObjectComposite is the simplest way to implement wxDataObject
242 // supporting multiple formats. It contains several wxDataObjectSimple and
243 // supports all formats supported by any of them.
244 //
245 // This class shouldn't be (normally) derived from, but may be used directly.
246 // If you need more flexibility than what it provides, you should probably use
247 // wxDataObject directly.
248 // ----------------------------------------------------------------------------
249
250 WX_DECLARE_LIST(wxDataObjectSimple, wxSimpleDataObjectList);
251
252 class WXDLLEXPORT wxDataObjectComposite : public wxDataObject
253 {
254 public:
255 // ctor
256 wxDataObjectComposite() { m_preferred = 0; }
257
258 // add data object (it will be deleted by wxDataObjectComposite, hence it
259 // must be allocated on the heap) whose format will become the preferred
260 // one if preferred == TRUE
261 void Add(wxDataObjectSimple *dataObject, bool preferred = FALSE);
262
263 // implement base class pure virtuals
264 // ----------------------------------
265 virtual wxDataFormat GetPreferredFormat(wxDataObjectBase::Direction dir = Get) const;
266 virtual size_t GetFormatCount(wxDataObjectBase::Direction dir = Get) const;
267 virtual void GetAllFormats(wxDataFormat *formats, wxDataObjectBase::Direction dir = Get) const;
268 virtual size_t GetDataSize(const wxDataFormat& format) const;
269 virtual bool GetDataHere(const wxDataFormat& format, void *buf) const;
270 virtual bool SetData(const wxDataFormat& format, size_t len, const void *buf);
271
272 protected:
273 // returns the pointer to the object which supports this format or NULL
274 wxDataObjectSimple *GetObject(const wxDataFormat& format) const;
275
276 private:
277 // the list of all (simple) data objects whose formats we support
278 wxSimpleDataObjectList m_dataObjects;
279
280 // the index of the preferred one (0 initially, so by default the first
281 // one is the preferred)
282 size_t m_preferred;
283 };
284
285 // ============================================================================
286 // Standard implementations of wxDataObjectSimple which can be used directly
287 // (i.e. without having to derive from them) for standard data type transfers.
288 //
289 // Note that although all of them can work with provided data, you can also
290 // override their virtual GetXXX() functions to only provide data on demand.
291 // ============================================================================
292
293 // ----------------------------------------------------------------------------
294 // wxTextDataObject contains text data
295 // ----------------------------------------------------------------------------
296
297 class WXDLLEXPORT wxTextDataObject : public wxDataObjectSimple
298 {
299 public:
300 // ctor: you can specify the text here or in SetText(), or override
301 // GetText()
302 wxTextDataObject(const wxString& text = wxEmptyString)
303 : wxDataObjectSimple(wxDF_TEXT), m_text(text)
304 {
305 }
306
307 // virtual functions which you may override if you want to provide text on
308 // demand only - otherwise, the trivial default versions will be used
309 virtual size_t GetTextLength() const { return m_text.Len() + 1; }
310 virtual wxString GetText() const { return m_text; }
311 virtual void SetText(const wxString& text) { m_text = text; }
312
313 // implement base class pure virtuals
314 // ----------------------------------
315 virtual size_t GetDataSize() const;
316 virtual bool GetDataHere(void *buf) const;
317 virtual bool SetData(size_t len, const void *buf);
318
319 private:
320 wxString m_text;
321
322 // virtual function hiding supression
323 size_t GetDataSize(const wxDataFormat& format) const
324 { return(wxDataObjectSimple::GetDataSize(format)); }
325 bool GetDataHere(const wxDataFormat& format, void *pBuf) const
326 { return(wxDataObjectSimple::GetDataHere(format, pBuf)); }
327 bool SetData(const wxDataFormat& format, size_t nLen, const void* pBuf)
328 { return(wxDataObjectSimple::SetData(format, nLen, pBuf)); }
329 };
330
331 // ----------------------------------------------------------------------------
332 // wxBitmapDataObject contains a bitmap
333 // ----------------------------------------------------------------------------
334
335 class WXDLLEXPORT wxBitmapDataObjectBase : public wxDataObjectSimple
336 {
337 public:
338 // ctor: you can specify the bitmap here or in SetBitmap(), or override
339 // GetBitmap()
340 wxBitmapDataObjectBase(const wxBitmap& bitmap = wxNullBitmap)
341 : wxDataObjectSimple(wxDF_BITMAP), m_bitmap(bitmap)
342 {
343 }
344
345 // virtual functions which you may override if you want to provide data on
346 // demand only - otherwise, the trivial default versions will be used
347 virtual wxBitmap GetBitmap() const { return m_bitmap; }
348 virtual void SetBitmap(const wxBitmap& bitmap) { m_bitmap = bitmap; }
349
350 protected:
351 wxBitmap m_bitmap;
352 };
353
354 // ----------------------------------------------------------------------------
355 // wxFileDataObject contains a list of filenames
356 //
357 // NB: notice that this is a "write only" object, it can only be filled with
358 // data from drag and drop operation.
359 // ----------------------------------------------------------------------------
360
361 class WXDLLEXPORT wxFileDataObjectBase : public wxDataObjectSimple
362 {
363 public:
364 // ctor: use AddFile() later to fill the array
365 wxFileDataObjectBase() : wxDataObjectSimple(wxDF_FILENAME) { }
366
367 // get a reference to our array
368 const wxArrayString& GetFilenames() const { return m_filenames; }
369
370 // the Get() functions do nothing for us
371 virtual size_t GetDataSize() const { return 0; }
372 virtual bool GetDataHere(void *WXUNUSED(buf)) const { return FALSE; }
373
374 protected:
375 wxArrayString m_filenames;
376
377 private:
378 // Virtual function hiding supression
379 size_t GetDataSize(const wxDataFormat& format) const
380 { return(wxDataObjectSimple::GetDataSize(format)); }
381 bool GetDataHere(const wxDataFormat& format, void* pBuf) const
382 { return(wxDataObjectSimple::GetDataHere(format, pBuf)); }
383 };
384
385 // ----------------------------------------------------------------------------
386 // wxCustomDataObject contains arbitrary untyped user data.
387 //
388 // It is understood that this data can be copied bitwise.
389 // ----------------------------------------------------------------------------
390
391 class WXDLLEXPORT wxCustomDataObject : public wxDataObjectSimple
392 {
393 public:
394 // if you don't specify the format in the ctor, you can still use
395 // SetFormat() later
396 wxCustomDataObject(const wxDataFormat& format = wxFormatInvalid);
397
398 // the dtor calls Free()
399 virtual ~wxCustomDataObject();
400
401 // you can call SetData() to set m_data: it will make a copy of the data
402 // you pass - or you can use TakeData() which won't copy anything, but
403 // will take ownership of data (i.e. will call Free() on it later)
404 void TakeData(size_t size, void *data);
405
406 // this function is called to allocate "size" bytes of memory from
407 // SetData(). The default version uses operator new[].
408 virtual void *Alloc(size_t size);
409
410 // this function is called when the data is freed, you may override it to
411 // anything you want (or may be nothing at all). The default version calls
412 // operator delete[] on m_data
413 virtual void Free();
414
415 // get data: you may override these functions if you wish to provide data
416 // only when it's requested
417 virtual size_t GetSize() const { return m_size; }
418 virtual void *GetData() const { return m_data; }
419
420 // implement base class pure virtuals
421 // ----------------------------------
422 virtual size_t GetDataSize() const;
423 virtual bool GetDataHere(void *buf) const;
424 virtual bool SetData(size_t size, const void *buf);
425
426 private:
427 size_t m_size;
428 void *m_data;
429
430 // virtual function hiding supression
431 size_t GetDataSize(const wxDataFormat& format) const
432 { return(wxDataObjectSimple::GetDataSize(format)); }
433 bool GetDataHere(const wxDataFormat& format, void* pBuf) const
434 { return(wxDataObjectSimple::GetDataHere(format, pBuf)); }
435 bool SetData(const wxDataFormat& format, size_t nLen, const void* pBuf)
436 { return(wxDataObjectSimple::SetData(format, nLen, pBuf)); }
437 };
438
439 // ----------------------------------------------------------------------------
440 // include platform-specific declarations of wxXXXBase classes
441 // ----------------------------------------------------------------------------
442
443 #if defined(__WXMSW__)
444 #include "wx/msw/ole/dataobj2.h"
445 #elif defined(__WXMOTIF__)
446 // #include "wx/motif/dataobj2.h" -- not yet
447 #elif defined(__WXGTK__)
448 #include "wx/gtk/dataobj2.h"
449 #elif defined(__WXPM__)
450 #include "wx/os2/dataobj2.h"
451 #endif
452
453 #endif // _WX_DATAOBJ_H_BASE_